block: Use bdrv_unref_child() for all children in bdrv_close()
[qemu/ar7.git] / vl.c
blob2e69c9fef2fb2da32b754f1507a4cfd6cd4cced3
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/units.h"
27 #include "qapi/error.h"
28 #include "qemu-version.h"
29 #include "qemu/cutils.h"
30 #include "qemu/help_option.h"
31 #include "qemu/uuid.h"
32 #include "sysemu/seccomp.h"
34 #ifdef CONFIG_SDL
35 #if defined(__APPLE__) || defined(main)
36 #include <SDL.h>
37 int qemu_main(int argc, char **argv, char **envp);
38 int main(int argc, char **argv)
40 return qemu_main(argc, argv, NULL);
42 #undef main
43 #define main qemu_main
44 #endif
45 #endif /* CONFIG_SDL */
47 #ifdef CONFIG_COCOA
48 #undef main
49 #define main qemu_main
50 #endif /* CONFIG_COCOA */
53 #include "qemu/error-report.h"
54 #include "qemu/sockets.h"
55 #include "hw/hw.h"
56 #include "hw/boards.h"
57 #include "sysemu/accel.h"
58 #include "hw/usb.h"
59 #include "hw/isa/isa.h"
60 #include "hw/scsi/scsi.h"
61 #include "hw/display/vga.h"
62 #include "hw/bt.h"
63 #include "sysemu/watchdog.h"
64 #include "hw/firmware/smbios.h"
65 #include "hw/acpi/acpi.h"
66 #include "hw/xen/xen.h"
67 #include "hw/qdev.h"
68 #include "hw/loader.h"
69 #include "monitor/qdev.h"
70 #include "sysemu/bt.h"
71 #include "net/net.h"
72 #include "net/slirp.h"
73 #include "monitor/monitor.h"
74 #include "ui/console.h"
75 #include "ui/input.h"
76 #include "sysemu/sysemu.h"
77 #include "sysemu/numa.h"
78 #include "exec/gdbstub.h"
79 #include "qemu/timer.h"
80 #include "chardev/char.h"
81 #include "qemu/bitmap.h"
82 #include "qemu/log.h"
83 #include "sysemu/blockdev.h"
84 #include "hw/block/block.h"
85 #include "migration/misc.h"
86 #include "migration/snapshot.h"
87 #include "migration/global_state.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "hw/audio/soundhw.h"
91 #include "audio/audio.h"
92 #include "sysemu/cpus.h"
93 #include "migration/colo.h"
94 #include "migration/postcopy-ram.h"
95 #include "sysemu/kvm.h"
96 #include "sysemu/hax.h"
97 #include "qapi/qobject-input-visitor.h"
98 #include "qemu/option.h"
99 #include "qemu/config-file.h"
100 #include "qemu-options.h"
101 #include "qemu/main-loop.h"
102 #ifdef CONFIG_VIRTFS
103 #include "fsdev/qemu-fsdev.h"
104 #endif
105 #include "sysemu/qtest.h"
107 #include "disas/disas.h"
109 #include "trace-root.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/arch_init.h"
114 #include "ui/qemu-spice.h"
115 #include "qapi/string-input-visitor.h"
116 #include "qapi/opts-visitor.h"
117 #include "qapi/clone-visitor.h"
118 #include "qom/object_interfaces.h"
119 #include "hw/semihosting/semihost.h"
120 #include "crypto/init.h"
121 #include "sysemu/replay.h"
122 #include "qapi/qapi-events-run-state.h"
123 #include "qapi/qapi-visit-block-core.h"
124 #include "qapi/qapi-visit-ui.h"
125 #include "qapi/qapi-commands-block-core.h"
126 #include "qapi/qapi-commands-misc.h"
127 #include "qapi/qapi-commands-run-state.h"
128 #include "qapi/qapi-commands-ui.h"
129 #include "qapi/qmp/qerror.h"
130 #include "sysemu/iothread.h"
131 #include "qemu/guest-random.h"
133 #define MAX_VIRTIO_CONSOLES 1
135 static const char *data_dir[16];
136 static int data_dir_idx;
137 const char *bios_name = NULL;
138 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
139 int display_opengl;
140 const char* keyboard_layout = NULL;
141 ram_addr_t ram_size;
142 const char *mem_path = NULL;
143 int mem_prealloc = 0; /* force preallocation of physical target memory */
144 bool enable_mlock = false;
145 bool enable_cpu_pm = false;
146 int nb_nics;
147 NICInfo nd_table[MAX_NICS];
148 int autostart;
149 static enum {
150 RTC_BASE_UTC,
151 RTC_BASE_LOCALTIME,
152 RTC_BASE_DATETIME,
153 } rtc_base_type = RTC_BASE_UTC;
154 static time_t rtc_ref_start_datetime;
155 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
156 static int rtc_host_datetime_offset = -1; /* valid & used only with
157 RTC_BASE_DATETIME */
158 QEMUClockType rtc_clock;
159 int vga_interface_type = VGA_NONE;
160 static DisplayOptions dpy;
161 static int num_serial_hds;
162 static Chardev **serial_hds;
163 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
164 int win2k_install_hack = 0;
165 int singlestep = 0;
166 int smp_cpus;
167 unsigned int max_cpus;
168 int smp_cores = 1;
169 int smp_threads = 1;
170 int acpi_enabled = 1;
171 int no_hpet = 0;
172 int fd_bootchk = 1;
173 static int no_reboot;
174 int no_shutdown = 0;
175 int cursor_hide = 1;
176 int graphic_rotate = 0;
177 const char *watchdog;
178 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
179 int nb_option_roms;
180 int old_param = 0;
181 const char *qemu_name;
182 int alt_grab = 0;
183 int ctrl_grab = 0;
184 unsigned int nb_prom_envs = 0;
185 const char *prom_envs[MAX_PROM_ENVS];
186 int boot_menu;
187 bool boot_strict;
188 uint8_t *boot_splash_filedata;
189 int only_migratable; /* turn it off unless user states otherwise */
190 bool wakeup_suspend_enabled;
192 int icount_align_option;
194 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
195 * little-endian "wire format" described in the SMBIOS 2.6 specification.
197 QemuUUID qemu_uuid;
198 bool qemu_uuid_set;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 bool xen_allowed;
207 uint32_t xen_domid;
208 enum xen_mode xen_mode = XEN_EMULATE;
209 bool xen_domid_restrict;
211 static int has_defaults = 1;
212 static int default_serial = 1;
213 static int default_parallel = 1;
214 static int default_monitor = 1;
215 static int default_floppy = 1;
216 static int default_cdrom = 1;
217 static int default_sdcard = 1;
218 static int default_vga = 1;
219 static int default_net = 1;
221 static struct {
222 const char *driver;
223 int *flag;
224 } default_list[] = {
225 { .driver = "isa-serial", .flag = &default_serial },
226 { .driver = "isa-parallel", .flag = &default_parallel },
227 { .driver = "isa-fdc", .flag = &default_floppy },
228 { .driver = "floppy", .flag = &default_floppy },
229 { .driver = "ide-cd", .flag = &default_cdrom },
230 { .driver = "ide-hd", .flag = &default_cdrom },
231 { .driver = "ide-drive", .flag = &default_cdrom },
232 { .driver = "scsi-cd", .flag = &default_cdrom },
233 { .driver = "scsi-hd", .flag = &default_cdrom },
234 { .driver = "VGA", .flag = &default_vga },
235 { .driver = "isa-vga", .flag = &default_vga },
236 { .driver = "cirrus-vga", .flag = &default_vga },
237 { .driver = "isa-cirrus-vga", .flag = &default_vga },
238 { .driver = "vmware-svga", .flag = &default_vga },
239 { .driver = "qxl-vga", .flag = &default_vga },
240 { .driver = "virtio-vga", .flag = &default_vga },
241 { .driver = "ati-vga", .flag = &default_vga },
244 static QemuOptsList qemu_rtc_opts = {
245 .name = "rtc",
246 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
247 .merge_lists = true,
248 .desc = {
250 .name = "base",
251 .type = QEMU_OPT_STRING,
253 .name = "clock",
254 .type = QEMU_OPT_STRING,
256 .name = "driftfix",
257 .type = QEMU_OPT_STRING,
259 { /* end of list */ }
263 static QemuOptsList qemu_option_rom_opts = {
264 .name = "option-rom",
265 .implied_opt_name = "romfile",
266 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
267 .desc = {
269 .name = "bootindex",
270 .type = QEMU_OPT_NUMBER,
271 }, {
272 .name = "romfile",
273 .type = QEMU_OPT_STRING,
275 { /* end of list */ }
279 static QemuOptsList qemu_machine_opts = {
280 .name = "machine",
281 .implied_opt_name = "type",
282 .merge_lists = true,
283 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
284 .desc = {
286 * no elements => accept any
287 * sanity checking will happen later
288 * when setting machine properties
294 static QemuOptsList qemu_accel_opts = {
295 .name = "accel",
296 .implied_opt_name = "accel",
297 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
298 .merge_lists = true,
299 .desc = {
301 .name = "accel",
302 .type = QEMU_OPT_STRING,
303 .help = "Select the type of accelerator",
306 .name = "thread",
307 .type = QEMU_OPT_STRING,
308 .help = "Enable/disable multi-threaded TCG",
310 { /* end of list */ }
314 static QemuOptsList qemu_boot_opts = {
315 .name = "boot-opts",
316 .implied_opt_name = "order",
317 .merge_lists = true,
318 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
319 .desc = {
321 .name = "order",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "once",
325 .type = QEMU_OPT_STRING,
326 }, {
327 .name = "menu",
328 .type = QEMU_OPT_BOOL,
329 }, {
330 .name = "splash",
331 .type = QEMU_OPT_STRING,
332 }, {
333 .name = "splash-time",
334 .type = QEMU_OPT_NUMBER,
335 }, {
336 .name = "reboot-timeout",
337 .type = QEMU_OPT_NUMBER,
338 }, {
339 .name = "strict",
340 .type = QEMU_OPT_BOOL,
342 { /*End of list */ }
346 static QemuOptsList qemu_add_fd_opts = {
347 .name = "add-fd",
348 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
349 .desc = {
351 .name = "fd",
352 .type = QEMU_OPT_NUMBER,
353 .help = "file descriptor of which a duplicate is added to fd set",
355 .name = "set",
356 .type = QEMU_OPT_NUMBER,
357 .help = "ID of the fd set to add fd to",
359 .name = "opaque",
360 .type = QEMU_OPT_STRING,
361 .help = "free-form string used to describe fd",
363 { /* end of list */ }
367 static QemuOptsList qemu_object_opts = {
368 .name = "object",
369 .implied_opt_name = "qom-type",
370 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
371 .desc = {
376 static QemuOptsList qemu_tpmdev_opts = {
377 .name = "tpmdev",
378 .implied_opt_name = "type",
379 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
380 .desc = {
381 /* options are defined in the TPM backends */
382 { /* end of list */ }
386 static QemuOptsList qemu_realtime_opts = {
387 .name = "realtime",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
389 .desc = {
391 .name = "mlock",
392 .type = QEMU_OPT_BOOL,
394 { /* end of list */ }
398 static QemuOptsList qemu_overcommit_opts = {
399 .name = "overcommit",
400 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
401 .desc = {
403 .name = "mem-lock",
404 .type = QEMU_OPT_BOOL,
407 .name = "cpu-pm",
408 .type = QEMU_OPT_BOOL,
410 { /* end of list */ }
414 static QemuOptsList qemu_msg_opts = {
415 .name = "msg",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
417 .desc = {
419 .name = "timestamp",
420 .type = QEMU_OPT_BOOL,
422 { /* end of list */ }
426 static QemuOptsList qemu_name_opts = {
427 .name = "name",
428 .implied_opt_name = "guest",
429 .merge_lists = true,
430 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
431 .desc = {
433 .name = "guest",
434 .type = QEMU_OPT_STRING,
435 .help = "Sets the name of the guest.\n"
436 "This name will be displayed in the SDL window caption.\n"
437 "The name will also be used for the VNC server",
438 }, {
439 .name = "process",
440 .type = QEMU_OPT_STRING,
441 .help = "Sets the name of the QEMU process, as shown in top etc",
442 }, {
443 .name = "debug-threads",
444 .type = QEMU_OPT_BOOL,
445 .help = "When enabled, name the individual threads; defaults off.\n"
446 "NOTE: The thread names are for debugging and not a\n"
447 "stable API.",
449 { /* End of list */ }
453 static QemuOptsList qemu_mem_opts = {
454 .name = "memory",
455 .implied_opt_name = "size",
456 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
457 .merge_lists = true,
458 .desc = {
460 .name = "size",
461 .type = QEMU_OPT_SIZE,
464 .name = "slots",
465 .type = QEMU_OPT_NUMBER,
468 .name = "maxmem",
469 .type = QEMU_OPT_SIZE,
471 { /* end of list */ }
475 static QemuOptsList qemu_icount_opts = {
476 .name = "icount",
477 .implied_opt_name = "shift",
478 .merge_lists = true,
479 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
480 .desc = {
482 .name = "shift",
483 .type = QEMU_OPT_STRING,
484 }, {
485 .name = "align",
486 .type = QEMU_OPT_BOOL,
487 }, {
488 .name = "sleep",
489 .type = QEMU_OPT_BOOL,
490 }, {
491 .name = "rr",
492 .type = QEMU_OPT_STRING,
493 }, {
494 .name = "rrfile",
495 .type = QEMU_OPT_STRING,
496 }, {
497 .name = "rrsnapshot",
498 .type = QEMU_OPT_STRING,
500 { /* end of list */ }
504 static QemuOptsList qemu_fw_cfg_opts = {
505 .name = "fw_cfg",
506 .implied_opt_name = "name",
507 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
508 .desc = {
510 .name = "name",
511 .type = QEMU_OPT_STRING,
512 .help = "Sets the fw_cfg name of the blob to be inserted",
513 }, {
514 .name = "file",
515 .type = QEMU_OPT_STRING,
516 .help = "Sets the name of the file from which "
517 "the fw_cfg blob will be loaded",
518 }, {
519 .name = "string",
520 .type = QEMU_OPT_STRING,
521 .help = "Sets content of the blob to be inserted from a string",
523 { /* end of list */ }
528 * Get machine options
530 * Returns: machine options (never null).
532 QemuOpts *qemu_get_machine_opts(void)
534 return qemu_find_opts_singleton("machine");
537 const char *qemu_get_vm_name(void)
539 return qemu_name;
542 static void res_free(void)
544 g_free(boot_splash_filedata);
545 boot_splash_filedata = NULL;
548 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
550 const char *driver = qemu_opt_get(opts, "driver");
551 int i;
553 if (!driver)
554 return 0;
555 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
556 if (strcmp(default_list[i].driver, driver) != 0)
557 continue;
558 *(default_list[i].flag) = 0;
560 return 0;
563 /***********************************************************/
564 /* QEMU state */
566 static RunState current_run_state = RUN_STATE_PRECONFIG;
568 /* We use RUN_STATE__MAX but any invalid value will do */
569 static RunState vmstop_requested = RUN_STATE__MAX;
570 static QemuMutex vmstop_lock;
572 typedef struct {
573 RunState from;
574 RunState to;
575 } RunStateTransition;
577 static const RunStateTransition runstate_transitions_def[] = {
578 /* from -> to */
579 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
580 /* Early switch to inmigrate state to allow -incoming CLI option work
581 * as it used to. TODO: delay actual switching to inmigrate state to
582 * the point after machine is built and remove this hack.
584 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
586 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
587 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
588 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
590 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
591 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
592 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
593 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
594 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
595 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
596 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
597 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
598 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
600 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
601 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
603 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
604 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
607 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
608 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
612 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
613 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
614 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
615 { RUN_STATE_PAUSED, RUN_STATE_COLO},
617 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
618 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
621 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
622 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
625 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
626 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
629 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
631 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
632 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_COLO, RUN_STATE_RUNNING },
636 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
637 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
638 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
639 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
640 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
642 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
643 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
644 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
645 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
646 { RUN_STATE_RUNNING, RUN_STATE_COLO},
648 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
650 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
651 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
652 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
654 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
655 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
656 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
657 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
658 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
659 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
661 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
662 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
663 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
664 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
666 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
667 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
668 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
670 { RUN_STATE__MAX, RUN_STATE__MAX },
673 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
675 bool runstate_check(RunState state)
677 return current_run_state == state;
680 bool runstate_store(char *str, size_t size)
682 const char *state = RunState_str(current_run_state);
683 size_t len = strlen(state) + 1;
685 if (len > size) {
686 return false;
688 memcpy(str, state, len);
689 return true;
692 static void runstate_init(void)
694 const RunStateTransition *p;
696 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
697 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
698 runstate_valid_transitions[p->from][p->to] = true;
701 qemu_mutex_init(&vmstop_lock);
704 /* This function will abort() on invalid state transitions */
705 void runstate_set(RunState new_state)
707 assert(new_state < RUN_STATE__MAX);
709 trace_runstate_set(current_run_state, RunState_str(current_run_state),
710 new_state, RunState_str(new_state));
712 if (current_run_state == new_state) {
713 return;
716 if (!runstate_valid_transitions[current_run_state][new_state]) {
717 error_report("invalid runstate transition: '%s' -> '%s'",
718 RunState_str(current_run_state),
719 RunState_str(new_state));
720 abort();
723 current_run_state = new_state;
726 int runstate_is_running(void)
728 return runstate_check(RUN_STATE_RUNNING);
731 bool runstate_needs_reset(void)
733 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
734 runstate_check(RUN_STATE_SHUTDOWN);
737 StatusInfo *qmp_query_status(Error **errp)
739 StatusInfo *info = g_malloc0(sizeof(*info));
741 info->running = runstate_is_running();
742 info->singlestep = singlestep;
743 info->status = current_run_state;
745 return info;
748 bool qemu_vmstop_requested(RunState *r)
750 qemu_mutex_lock(&vmstop_lock);
751 *r = vmstop_requested;
752 vmstop_requested = RUN_STATE__MAX;
753 qemu_mutex_unlock(&vmstop_lock);
754 return *r < RUN_STATE__MAX;
757 void qemu_system_vmstop_request_prepare(void)
759 qemu_mutex_lock(&vmstop_lock);
762 void qemu_system_vmstop_request(RunState state)
764 vmstop_requested = state;
765 qemu_mutex_unlock(&vmstop_lock);
766 qemu_notify_event();
769 /***********************************************************/
770 /* RTC reference time/date access */
771 static time_t qemu_ref_timedate(QEMUClockType clock)
773 time_t value = qemu_clock_get_ms(clock) / 1000;
774 switch (clock) {
775 case QEMU_CLOCK_REALTIME:
776 value -= rtc_realtime_clock_offset;
777 /* no break */
778 case QEMU_CLOCK_VIRTUAL:
779 value += rtc_ref_start_datetime;
780 break;
781 case QEMU_CLOCK_HOST:
782 if (rtc_base_type == RTC_BASE_DATETIME) {
783 value -= rtc_host_datetime_offset;
785 break;
786 default:
787 assert(0);
789 return value;
792 void qemu_get_timedate(struct tm *tm, int offset)
794 time_t ti = qemu_ref_timedate(rtc_clock);
796 ti += offset;
798 switch (rtc_base_type) {
799 case RTC_BASE_DATETIME:
800 case RTC_BASE_UTC:
801 gmtime_r(&ti, tm);
802 break;
803 case RTC_BASE_LOCALTIME:
804 localtime_r(&ti, tm);
805 break;
809 int qemu_timedate_diff(struct tm *tm)
811 time_t seconds;
813 switch (rtc_base_type) {
814 case RTC_BASE_DATETIME:
815 case RTC_BASE_UTC:
816 seconds = mktimegm(tm);
817 break;
818 case RTC_BASE_LOCALTIME:
820 struct tm tmp = *tm;
821 tmp.tm_isdst = -1; /* use timezone to figure it out */
822 seconds = mktime(&tmp);
823 break;
825 default:
826 abort();
829 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
832 static void configure_rtc_base_datetime(const char *startdate)
834 time_t rtc_start_datetime;
835 struct tm tm;
837 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
838 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
839 /* OK */
840 } else if (sscanf(startdate, "%d-%d-%d",
841 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
842 tm.tm_hour = 0;
843 tm.tm_min = 0;
844 tm.tm_sec = 0;
845 } else {
846 goto date_fail;
848 tm.tm_year -= 1900;
849 tm.tm_mon--;
850 rtc_start_datetime = mktimegm(&tm);
851 if (rtc_start_datetime == -1) {
852 date_fail:
853 error_report("invalid datetime format");
854 error_printf("valid formats: "
855 "'2006-06-17T16:01:21' or '2006-06-17'\n");
856 exit(1);
858 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
859 rtc_ref_start_datetime = rtc_start_datetime;
862 static void configure_rtc(QemuOpts *opts)
864 const char *value;
866 /* Set defaults */
867 rtc_clock = QEMU_CLOCK_HOST;
868 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
869 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
871 value = qemu_opt_get(opts, "base");
872 if (value) {
873 if (!strcmp(value, "utc")) {
874 rtc_base_type = RTC_BASE_UTC;
875 } else if (!strcmp(value, "localtime")) {
876 Error *blocker = NULL;
877 rtc_base_type = RTC_BASE_LOCALTIME;
878 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
879 "-rtc base=localtime");
880 replay_add_blocker(blocker);
881 } else {
882 rtc_base_type = RTC_BASE_DATETIME;
883 configure_rtc_base_datetime(value);
886 value = qemu_opt_get(opts, "clock");
887 if (value) {
888 if (!strcmp(value, "host")) {
889 rtc_clock = QEMU_CLOCK_HOST;
890 } else if (!strcmp(value, "rt")) {
891 rtc_clock = QEMU_CLOCK_REALTIME;
892 } else if (!strcmp(value, "vm")) {
893 rtc_clock = QEMU_CLOCK_VIRTUAL;
894 } else {
895 error_report("invalid option value '%s'", value);
896 exit(1);
899 value = qemu_opt_get(opts, "driftfix");
900 if (value) {
901 if (!strcmp(value, "slew")) {
902 static GlobalProperty slew_lost_ticks = {
903 .driver = "mc146818rtc",
904 .property = "lost_tick_policy",
905 .value = "slew",
908 qdev_prop_register_global(&slew_lost_ticks);
909 } else if (!strcmp(value, "none")) {
910 /* discard is default */
911 } else {
912 error_report("invalid option value '%s'", value);
913 exit(1);
918 /***********************************************************/
919 /* Bluetooth support */
920 static int nb_hcis;
921 static int cur_hci;
922 static struct HCIInfo *hci_table[MAX_NICS];
924 struct HCIInfo *qemu_next_hci(void)
926 if (cur_hci == nb_hcis)
927 return &null_hci;
929 return hci_table[cur_hci++];
932 static int bt_hci_parse(const char *str)
934 struct HCIInfo *hci;
935 bdaddr_t bdaddr;
937 if (nb_hcis >= MAX_NICS) {
938 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
939 return -1;
942 hci = hci_init(str);
943 if (!hci)
944 return -1;
946 bdaddr.b[0] = 0x52;
947 bdaddr.b[1] = 0x54;
948 bdaddr.b[2] = 0x00;
949 bdaddr.b[3] = 0x12;
950 bdaddr.b[4] = 0x34;
951 bdaddr.b[5] = 0x56 + nb_hcis;
952 hci->bdaddr_set(hci, bdaddr.b);
954 hci_table[nb_hcis++] = hci;
956 return 0;
959 static void bt_vhci_add(int vlan_id)
961 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
963 if (!vlan->slave)
964 warn_report("adding a VHCI to an empty scatternet %i",
965 vlan_id);
967 bt_vhci_init(bt_new_hci(vlan));
970 static struct bt_device_s *bt_device_add(const char *opt)
972 struct bt_scatternet_s *vlan;
973 int vlan_id = 0;
974 char *endp = strstr(opt, ",vlan=");
975 int len = (endp ? endp - opt : strlen(opt)) + 1;
976 char devname[10];
978 pstrcpy(devname, MIN(sizeof(devname), len), opt);
980 if (endp) {
981 vlan_id = strtol(endp + 6, &endp, 0);
982 if (*endp) {
983 error_report("unrecognised bluetooth vlan Id");
984 return 0;
988 vlan = qemu_find_bt_vlan(vlan_id);
990 if (!vlan->slave)
991 warn_report("adding a slave device to an empty scatternet %i",
992 vlan_id);
994 if (!strcmp(devname, "keyboard"))
995 return bt_keyboard_init(vlan);
997 error_report("unsupported bluetooth device '%s'", devname);
998 return 0;
1001 static int bt_parse(const char *opt)
1003 const char *endp, *p;
1004 int vlan;
1006 if (strstart(opt, "hci", &endp)) {
1007 if (!*endp || *endp == ',') {
1008 if (*endp)
1009 if (!strstart(endp, ",vlan=", 0))
1010 opt = endp + 1;
1012 return bt_hci_parse(opt);
1014 } else if (strstart(opt, "vhci", &endp)) {
1015 if (!*endp || *endp == ',') {
1016 if (*endp) {
1017 if (strstart(endp, ",vlan=", &p)) {
1018 vlan = strtol(p, (char **) &endp, 0);
1019 if (*endp) {
1020 error_report("bad scatternet '%s'", p);
1021 return 1;
1023 } else {
1024 error_report("bad parameter '%s'", endp + 1);
1025 return 1;
1027 } else
1028 vlan = 0;
1030 bt_vhci_add(vlan);
1031 return 0;
1033 } else if (strstart(opt, "device:", &endp))
1034 return !bt_device_add(endp);
1036 error_report("bad bluetooth parameter '%s'", opt);
1037 return 1;
1040 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1042 const char *proc_name;
1044 if (qemu_opt_get(opts, "debug-threads")) {
1045 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1047 qemu_name = qemu_opt_get(opts, "guest");
1049 proc_name = qemu_opt_get(opts, "process");
1050 if (proc_name) {
1051 os_set_proc_name(proc_name);
1054 return 0;
1057 bool defaults_enabled(void)
1059 return has_defaults;
1062 #ifndef _WIN32
1063 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1065 int fd, dupfd, flags;
1066 int64_t fdset_id;
1067 const char *fd_opaque = NULL;
1068 AddfdInfo *fdinfo;
1070 fd = qemu_opt_get_number(opts, "fd", -1);
1071 fdset_id = qemu_opt_get_number(opts, "set", -1);
1072 fd_opaque = qemu_opt_get(opts, "opaque");
1074 if (fd < 0) {
1075 error_setg(errp, "fd option is required and must be non-negative");
1076 return -1;
1079 if (fd <= STDERR_FILENO) {
1080 error_setg(errp, "fd cannot be a standard I/O stream");
1081 return -1;
1085 * All fds inherited across exec() necessarily have FD_CLOEXEC
1086 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1088 flags = fcntl(fd, F_GETFD);
1089 if (flags == -1 || (flags & FD_CLOEXEC)) {
1090 error_setg(errp, "fd is not valid or already in use");
1091 return -1;
1094 if (fdset_id < 0) {
1095 error_setg(errp, "set option is required and must be non-negative");
1096 return -1;
1099 #ifdef F_DUPFD_CLOEXEC
1100 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1101 #else
1102 dupfd = dup(fd);
1103 if (dupfd != -1) {
1104 qemu_set_cloexec(dupfd);
1106 #endif
1107 if (dupfd == -1) {
1108 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1109 return -1;
1112 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1113 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1114 &error_abort);
1115 g_free(fdinfo);
1117 return 0;
1120 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1122 int fd;
1124 fd = qemu_opt_get_number(opts, "fd", -1);
1125 close(fd);
1127 return 0;
1129 #endif
1131 /***********************************************************/
1132 /* QEMU Block devices */
1134 #define HD_OPTS "media=disk"
1135 #define CDROM_OPTS "media=cdrom"
1136 #define FD_OPTS ""
1137 #define PFLASH_OPTS ""
1138 #define MTD_OPTS ""
1139 #define SD_OPTS ""
1141 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1143 BlockInterfaceType *block_default_type = opaque;
1145 return drive_new(opts, *block_default_type, errp) == NULL;
1148 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1150 if (qemu_opt_get(opts, "snapshot") == NULL) {
1151 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1153 return 0;
1156 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1157 int index, const char *optstr)
1159 QemuOpts *opts;
1160 DriveInfo *dinfo;
1162 if (!enable || drive_get_by_index(type, index)) {
1163 return;
1166 opts = drive_add(type, index, NULL, optstr);
1167 if (snapshot) {
1168 drive_enable_snapshot(NULL, opts, NULL);
1171 dinfo = drive_new(opts, type, &error_abort);
1172 dinfo->is_default = true;
1176 typedef struct BlockdevOptionsQueueEntry {
1177 BlockdevOptions *bdo;
1178 Location loc;
1179 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1180 } BlockdevOptionsQueueEntry;
1182 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1184 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1185 MachineClass *machine_class, int snapshot)
1188 * If the currently selected machine wishes to override the
1189 * units-per-bus property of its default HBA interface type, do so
1190 * now.
1192 if (machine_class->units_per_default_bus) {
1193 override_max_devs(machine_class->block_default_type,
1194 machine_class->units_per_default_bus);
1197 /* open the virtual block devices */
1198 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1199 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1201 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1202 loc_push_restore(&bdo->loc);
1203 qmp_blockdev_add(bdo->bdo, &error_fatal);
1204 loc_pop(&bdo->loc);
1205 qapi_free_BlockdevOptions(bdo->bdo);
1206 g_free(bdo);
1208 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
1209 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1210 NULL, NULL);
1212 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1213 &machine_class->block_default_type, &error_fatal)) {
1214 /* We printed help */
1215 exit(0);
1218 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1219 CDROM_OPTS);
1220 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1221 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1225 static QemuOptsList qemu_smp_opts = {
1226 .name = "smp-opts",
1227 .implied_opt_name = "cpus",
1228 .merge_lists = true,
1229 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1230 .desc = {
1232 .name = "cpus",
1233 .type = QEMU_OPT_NUMBER,
1234 }, {
1235 .name = "sockets",
1236 .type = QEMU_OPT_NUMBER,
1237 }, {
1238 .name = "cores",
1239 .type = QEMU_OPT_NUMBER,
1240 }, {
1241 .name = "threads",
1242 .type = QEMU_OPT_NUMBER,
1243 }, {
1244 .name = "maxcpus",
1245 .type = QEMU_OPT_NUMBER,
1247 { /*End of list */ }
1251 static void smp_parse(QemuOpts *opts)
1253 if (opts) {
1254 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1255 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1256 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1257 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1259 /* compute missing values, prefer sockets over cores over threads */
1260 if (cpus == 0 || sockets == 0) {
1261 cores = cores > 0 ? cores : 1;
1262 threads = threads > 0 ? threads : 1;
1263 if (cpus == 0) {
1264 sockets = sockets > 0 ? sockets : 1;
1265 cpus = cores * threads * sockets;
1266 } else {
1267 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1268 sockets = max_cpus / (cores * threads);
1270 } else if (cores == 0) {
1271 threads = threads > 0 ? threads : 1;
1272 cores = cpus / (sockets * threads);
1273 cores = cores > 0 ? cores : 1;
1274 } else if (threads == 0) {
1275 threads = cpus / (cores * sockets);
1276 threads = threads > 0 ? threads : 1;
1277 } else if (sockets * cores * threads < cpus) {
1278 error_report("cpu topology: "
1279 "sockets (%u) * cores (%u) * threads (%u) < "
1280 "smp_cpus (%u)",
1281 sockets, cores, threads, cpus);
1282 exit(1);
1285 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1287 if (max_cpus < cpus) {
1288 error_report("maxcpus must be equal to or greater than smp");
1289 exit(1);
1292 if (sockets * cores * threads > max_cpus) {
1293 error_report("cpu topology: "
1294 "sockets (%u) * cores (%u) * threads (%u) > "
1295 "maxcpus (%u)",
1296 sockets, cores, threads, max_cpus);
1297 exit(1);
1300 if (sockets * cores * threads != max_cpus) {
1301 warn_report("Invalid CPU topology deprecated: "
1302 "sockets (%u) * cores (%u) * threads (%u) "
1303 "!= maxcpus (%u)",
1304 sockets, cores, threads, max_cpus);
1307 smp_cpus = cpus;
1308 smp_cores = cores;
1309 smp_threads = threads;
1312 if (smp_cpus > 1) {
1313 Error *blocker = NULL;
1314 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1315 replay_add_blocker(blocker);
1319 static void realtime_init(void)
1321 if (enable_mlock) {
1322 if (os_mlock() < 0) {
1323 error_report("locking memory failed");
1324 exit(1);
1330 static void configure_msg(QemuOpts *opts)
1332 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1336 /* Now we still need this for compatibility with XEN. */
1337 bool has_igd_gfx_passthru;
1338 static void igd_gfx_passthru(void)
1340 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1343 /***********************************************************/
1344 /* USB devices */
1346 static int usb_device_add(const char *devname)
1348 USBDevice *dev = NULL;
1350 if (!machine_usb(current_machine)) {
1351 return -1;
1354 dev = usbdevice_create(devname);
1355 if (!dev)
1356 return -1;
1358 return 0;
1361 static int usb_parse(const char *cmdline)
1363 int r;
1364 r = usb_device_add(cmdline);
1365 if (r < 0) {
1366 error_report("could not add USB device '%s'", cmdline);
1368 return r;
1371 /***********************************************************/
1372 /* machine registration */
1374 MachineState *current_machine;
1376 static MachineClass *find_machine(const char *name, GSList *machines)
1378 GSList *el;
1380 for (el = machines; el; el = el->next) {
1381 MachineClass *mc = el->data;
1383 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1384 return mc;
1388 return NULL;
1391 static MachineClass *find_default_machine(GSList *machines)
1393 GSList *el;
1395 for (el = machines; el; el = el->next) {
1396 MachineClass *mc = el->data;
1398 if (mc->is_default) {
1399 return mc;
1403 return NULL;
1406 MachineInfoList *qmp_query_machines(Error **errp)
1408 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1409 MachineInfoList *mach_list = NULL;
1411 for (el = machines; el; el = el->next) {
1412 MachineClass *mc = el->data;
1413 MachineInfoList *entry;
1414 MachineInfo *info;
1416 info = g_malloc0(sizeof(*info));
1417 if (mc->is_default) {
1418 info->has_is_default = true;
1419 info->is_default = true;
1422 if (mc->alias) {
1423 info->has_alias = true;
1424 info->alias = g_strdup(mc->alias);
1427 info->name = g_strdup(mc->name);
1428 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1429 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1431 entry = g_malloc0(sizeof(*entry));
1432 entry->value = info;
1433 entry->next = mach_list;
1434 mach_list = entry;
1437 g_slist_free(machines);
1438 return mach_list;
1441 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1443 ObjectProperty *prop;
1444 ObjectPropertyIterator iter;
1446 if (!qemu_opt_has_help_opt(opts)) {
1447 return 0;
1450 object_property_iter_init(&iter, OBJECT(machine));
1451 while ((prop = object_property_iter_next(&iter))) {
1452 if (!prop->set) {
1453 continue;
1456 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1457 prop->name, prop->type);
1458 if (prop->description) {
1459 printf(" (%s)\n", prop->description);
1460 } else {
1461 printf("\n");
1465 return 1;
1468 struct vm_change_state_entry {
1469 VMChangeStateHandler *cb;
1470 void *opaque;
1471 QLIST_ENTRY (vm_change_state_entry) entries;
1474 static QLIST_HEAD(, vm_change_state_entry) vm_change_state_head;
1476 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1477 void *opaque)
1479 VMChangeStateEntry *e;
1481 e = g_malloc0(sizeof (*e));
1483 e->cb = cb;
1484 e->opaque = opaque;
1485 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1486 return e;
1489 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1491 QLIST_REMOVE (e, entries);
1492 g_free (e);
1495 void vm_state_notify(int running, RunState state)
1497 VMChangeStateEntry *e, *next;
1499 trace_vm_state_notify(running, state, RunState_str(state));
1501 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1502 e->cb(e->opaque, running, state);
1506 static ShutdownCause reset_requested;
1507 static ShutdownCause shutdown_requested;
1508 static int shutdown_signal;
1509 static pid_t shutdown_pid;
1510 static int powerdown_requested;
1511 static int debug_requested;
1512 static int suspend_requested;
1513 static bool preconfig_exit_requested = true;
1514 static WakeupReason wakeup_reason;
1515 static NotifierList powerdown_notifiers =
1516 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1517 static NotifierList suspend_notifiers =
1518 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1519 static NotifierList wakeup_notifiers =
1520 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1521 static NotifierList shutdown_notifiers =
1522 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1523 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1525 ShutdownCause qemu_shutdown_requested_get(void)
1527 return shutdown_requested;
1530 ShutdownCause qemu_reset_requested_get(void)
1532 return reset_requested;
1535 static int qemu_shutdown_requested(void)
1537 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1540 static void qemu_kill_report(void)
1542 if (!qtest_driver() && shutdown_signal) {
1543 if (shutdown_pid == 0) {
1544 /* This happens for eg ^C at the terminal, so it's worth
1545 * avoiding printing an odd message in that case.
1547 error_report("terminating on signal %d", shutdown_signal);
1548 } else {
1549 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1551 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1552 shutdown_signal, shutdown_pid,
1553 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1554 g_free(shutdown_cmd);
1556 shutdown_signal = 0;
1560 static ShutdownCause qemu_reset_requested(void)
1562 ShutdownCause r = reset_requested;
1564 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1565 reset_requested = SHUTDOWN_CAUSE_NONE;
1566 return r;
1568 return SHUTDOWN_CAUSE_NONE;
1571 static int qemu_suspend_requested(void)
1573 int r = suspend_requested;
1574 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1575 suspend_requested = 0;
1576 return r;
1578 return false;
1581 static WakeupReason qemu_wakeup_requested(void)
1583 return wakeup_reason;
1586 static int qemu_powerdown_requested(void)
1588 int r = powerdown_requested;
1589 powerdown_requested = 0;
1590 return r;
1593 static int qemu_debug_requested(void)
1595 int r = debug_requested;
1596 debug_requested = 0;
1597 return r;
1600 void qemu_exit_preconfig_request(void)
1602 preconfig_exit_requested = true;
1606 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1608 void qemu_system_reset(ShutdownCause reason)
1610 MachineClass *mc;
1612 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1614 cpu_synchronize_all_states();
1616 if (mc && mc->reset) {
1617 mc->reset();
1618 } else {
1619 qemu_devices_reset();
1621 if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1622 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1624 cpu_synchronize_all_post_reset();
1627 void qemu_system_guest_panicked(GuestPanicInformation *info)
1629 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1631 if (current_cpu) {
1632 current_cpu->crash_occurred = true;
1634 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1635 !!info, info);
1636 vm_stop(RUN_STATE_GUEST_PANICKED);
1637 if (!no_shutdown) {
1638 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1639 !!info, info);
1640 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1643 if (info) {
1644 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1645 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1646 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1647 info->u.hyper_v.arg1,
1648 info->u.hyper_v.arg2,
1649 info->u.hyper_v.arg3,
1650 info->u.hyper_v.arg4,
1651 info->u.hyper_v.arg5);
1652 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1653 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1654 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1655 info->u.s390.core,
1656 S390CrashReason_str(info->u.s390.reason),
1657 info->u.s390.psw_mask,
1658 info->u.s390.psw_addr);
1660 qapi_free_GuestPanicInformation(info);
1664 void qemu_system_reset_request(ShutdownCause reason)
1666 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1667 shutdown_requested = reason;
1668 } else {
1669 reset_requested = reason;
1671 cpu_stop_current();
1672 qemu_notify_event();
1675 static void qemu_system_suspend(void)
1677 pause_all_vcpus();
1678 notifier_list_notify(&suspend_notifiers, NULL);
1679 runstate_set(RUN_STATE_SUSPENDED);
1680 qapi_event_send_suspend();
1683 void qemu_system_suspend_request(void)
1685 if (runstate_check(RUN_STATE_SUSPENDED)) {
1686 return;
1688 suspend_requested = 1;
1689 cpu_stop_current();
1690 qemu_notify_event();
1693 void qemu_register_suspend_notifier(Notifier *notifier)
1695 notifier_list_add(&suspend_notifiers, notifier);
1698 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1700 trace_system_wakeup_request(reason);
1702 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1703 error_setg(errp,
1704 "Unable to wake up: guest is not in suspended state");
1705 return;
1707 if (!(wakeup_reason_mask & (1 << reason))) {
1708 return;
1710 runstate_set(RUN_STATE_RUNNING);
1711 wakeup_reason = reason;
1712 qemu_notify_event();
1715 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1717 if (enabled) {
1718 wakeup_reason_mask |= (1 << reason);
1719 } else {
1720 wakeup_reason_mask &= ~(1 << reason);
1724 void qemu_register_wakeup_notifier(Notifier *notifier)
1726 notifier_list_add(&wakeup_notifiers, notifier);
1729 void qemu_register_wakeup_support(void)
1731 wakeup_suspend_enabled = true;
1734 bool qemu_wakeup_suspend_enabled(void)
1736 return wakeup_suspend_enabled;
1739 CurrentMachineParams *qmp_query_current_machine(Error **errp)
1741 CurrentMachineParams *params = g_malloc0(sizeof(*params));
1742 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
1744 return params;
1747 void qemu_system_killed(int signal, pid_t pid)
1749 shutdown_signal = signal;
1750 shutdown_pid = pid;
1751 no_shutdown = 0;
1753 /* Cannot call qemu_system_shutdown_request directly because
1754 * we are in a signal handler.
1756 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1757 qemu_notify_event();
1760 void qemu_system_shutdown_request(ShutdownCause reason)
1762 trace_qemu_system_shutdown_request(reason);
1763 replay_shutdown_request(reason);
1764 shutdown_requested = reason;
1765 qemu_notify_event();
1768 static void qemu_system_powerdown(void)
1770 qapi_event_send_powerdown();
1771 notifier_list_notify(&powerdown_notifiers, NULL);
1774 static void qemu_system_shutdown(ShutdownCause cause)
1776 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1777 notifier_list_notify(&shutdown_notifiers, &cause);
1780 void qemu_system_powerdown_request(void)
1782 trace_qemu_system_powerdown_request();
1783 powerdown_requested = 1;
1784 qemu_notify_event();
1787 void qemu_register_powerdown_notifier(Notifier *notifier)
1789 notifier_list_add(&powerdown_notifiers, notifier);
1792 void qemu_register_shutdown_notifier(Notifier *notifier)
1794 notifier_list_add(&shutdown_notifiers, notifier);
1797 void qemu_system_debug_request(void)
1799 debug_requested = 1;
1800 qemu_notify_event();
1803 static bool main_loop_should_exit(void)
1805 RunState r;
1806 ShutdownCause request;
1808 if (preconfig_exit_requested) {
1809 if (runstate_check(RUN_STATE_PRECONFIG)) {
1810 runstate_set(RUN_STATE_PRELAUNCH);
1812 preconfig_exit_requested = false;
1813 return true;
1815 if (qemu_debug_requested()) {
1816 vm_stop(RUN_STATE_DEBUG);
1818 if (qemu_suspend_requested()) {
1819 qemu_system_suspend();
1821 request = qemu_shutdown_requested();
1822 if (request) {
1823 qemu_kill_report();
1824 qemu_system_shutdown(request);
1825 if (no_shutdown) {
1826 vm_stop(RUN_STATE_SHUTDOWN);
1827 } else {
1828 return true;
1831 request = qemu_reset_requested();
1832 if (request) {
1833 pause_all_vcpus();
1834 qemu_system_reset(request);
1835 resume_all_vcpus();
1836 if (!runstate_check(RUN_STATE_RUNNING) &&
1837 !runstate_check(RUN_STATE_INMIGRATE)) {
1838 runstate_set(RUN_STATE_PRELAUNCH);
1841 if (qemu_wakeup_requested()) {
1842 pause_all_vcpus();
1843 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1844 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1845 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1846 resume_all_vcpus();
1847 qapi_event_send_wakeup();
1849 if (qemu_powerdown_requested()) {
1850 qemu_system_powerdown();
1852 if (qemu_vmstop_requested(&r)) {
1853 vm_stop(r);
1855 return false;
1858 static void main_loop(void)
1860 #ifdef CONFIG_PROFILER
1861 int64_t ti;
1862 #endif
1863 while (!main_loop_should_exit()) {
1864 #ifdef CONFIG_PROFILER
1865 ti = profile_getclock();
1866 #endif
1867 main_loop_wait(false);
1868 #ifdef CONFIG_PROFILER
1869 dev_time += profile_getclock() - ti;
1870 #endif
1874 static void version(void)
1876 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1877 QEMU_COPYRIGHT "\n");
1880 static void help(int exitcode)
1882 version();
1883 printf("usage: %s [options] [disk_image]\n\n"
1884 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1885 error_get_progname());
1887 #define QEMU_OPTIONS_GENERATE_HELP
1888 #include "qemu-options-wrapper.h"
1890 printf("\nDuring emulation, the following keys are useful:\n"
1891 "ctrl-alt-f toggle full screen\n"
1892 "ctrl-alt-n switch to virtual console 'n'\n"
1893 "ctrl-alt toggle mouse and keyboard grab\n"
1894 "\n"
1895 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1896 "\n"
1897 QEMU_HELP_BOTTOM "\n");
1899 exit(exitcode);
1902 #define HAS_ARG 0x0001
1904 typedef struct QEMUOption {
1905 const char *name;
1906 int flags;
1907 int index;
1908 uint32_t arch_mask;
1909 } QEMUOption;
1911 static const QEMUOption qemu_options[] = {
1912 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1913 #define QEMU_OPTIONS_GENERATE_OPTIONS
1914 #include "qemu-options-wrapper.h"
1915 { NULL },
1918 typedef struct VGAInterfaceInfo {
1919 const char *opt_name; /* option name */
1920 const char *name; /* human-readable name */
1921 /* Class names indicating that support is available.
1922 * If no class is specified, the interface is always available */
1923 const char *class_names[2];
1924 } VGAInterfaceInfo;
1926 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1927 [VGA_NONE] = {
1928 .opt_name = "none",
1929 .name = "no graphic card",
1931 [VGA_STD] = {
1932 .opt_name = "std",
1933 .name = "standard VGA",
1934 .class_names = { "VGA", "isa-vga" },
1936 [VGA_CIRRUS] = {
1937 .opt_name = "cirrus",
1938 .name = "Cirrus VGA",
1939 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1941 [VGA_VMWARE] = {
1942 .opt_name = "vmware",
1943 .name = "VMWare SVGA",
1944 .class_names = { "vmware-svga" },
1946 [VGA_VIRTIO] = {
1947 .opt_name = "virtio",
1948 .name = "Virtio VGA",
1949 .class_names = { "virtio-vga" },
1951 [VGA_QXL] = {
1952 .opt_name = "qxl",
1953 .name = "QXL VGA",
1954 .class_names = { "qxl-vga" },
1956 [VGA_TCX] = {
1957 .opt_name = "tcx",
1958 .name = "TCX framebuffer",
1959 .class_names = { "SUNW,tcx" },
1961 [VGA_CG3] = {
1962 .opt_name = "cg3",
1963 .name = "CG3 framebuffer",
1964 .class_names = { "cgthree" },
1966 [VGA_XENFB] = {
1967 .opt_name = "xenfb",
1968 .name = "Xen paravirtualized framebuffer",
1972 static bool vga_interface_available(VGAInterfaceType t)
1974 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1976 assert(t < VGA_TYPE_MAX);
1977 return !ti->class_names[0] ||
1978 object_class_by_name(ti->class_names[0]) ||
1979 object_class_by_name(ti->class_names[1]);
1982 static const char *
1983 get_default_vga_model(const MachineClass *machine_class)
1985 if (machine_class->default_display) {
1986 return machine_class->default_display;
1987 } else if (vga_interface_available(VGA_CIRRUS)) {
1988 return "cirrus";
1989 } else if (vga_interface_available(VGA_STD)) {
1990 return "std";
1993 return NULL;
1996 static void select_vgahw(const MachineClass *machine_class, const char *p)
1998 const char *opts;
1999 int t;
2001 if (g_str_equal(p, "help")) {
2002 const char *def = get_default_vga_model(machine_class);
2004 for (t = 0; t < VGA_TYPE_MAX; t++) {
2005 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2007 if (vga_interface_available(t) && ti->opt_name) {
2008 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
2009 g_str_equal(ti->opt_name, def) ? " (default)" : "");
2012 exit(0);
2015 assert(vga_interface_type == VGA_NONE);
2016 for (t = 0; t < VGA_TYPE_MAX; t++) {
2017 const VGAInterfaceInfo *ti = &vga_interfaces[t];
2018 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2019 if (!vga_interface_available(t)) {
2020 error_report("%s not available", ti->name);
2021 exit(1);
2023 vga_interface_type = t;
2024 break;
2027 if (t == VGA_TYPE_MAX) {
2028 invalid_vga:
2029 error_report("unknown vga type: %s", p);
2030 exit(1);
2032 while (*opts) {
2033 const char *nextopt;
2035 if (strstart(opts, ",retrace=", &nextopt)) {
2036 opts = nextopt;
2037 if (strstart(opts, "dumb", &nextopt))
2038 vga_retrace_method = VGA_RETRACE_DUMB;
2039 else if (strstart(opts, "precise", &nextopt))
2040 vga_retrace_method = VGA_RETRACE_PRECISE;
2041 else goto invalid_vga;
2042 } else goto invalid_vga;
2043 opts = nextopt;
2047 static void parse_display_qapi(const char *optarg)
2049 DisplayOptions *opts;
2050 Visitor *v;
2052 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2054 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2055 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2057 qapi_free_DisplayOptions(opts);
2058 visit_free(v);
2061 DisplayOptions *qmp_query_display_options(Error **errp)
2063 return QAPI_CLONE(DisplayOptions, &dpy);
2066 static void parse_display(const char *p)
2068 const char *opts;
2070 if (strstart(p, "sdl", &opts)) {
2072 * sdl DisplayType needs hand-crafted parser instead of
2073 * parse_display_qapi() due to some options not in
2074 * DisplayOptions, specifically:
2075 * - frame
2076 * Already deprecated.
2077 * - ctrl_grab + alt_grab
2078 * Not clear yet what happens to them long-term. Should
2079 * replaced by something better or deprecated and dropped.
2081 dpy.type = DISPLAY_TYPE_SDL;
2082 while (*opts) {
2083 const char *nextopt;
2085 if (strstart(opts, ",alt_grab=", &nextopt)) {
2086 opts = nextopt;
2087 if (strstart(opts, "on", &nextopt)) {
2088 alt_grab = 1;
2089 } else if (strstart(opts, "off", &nextopt)) {
2090 alt_grab = 0;
2091 } else {
2092 goto invalid_sdl_args;
2094 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2095 opts = nextopt;
2096 if (strstart(opts, "on", &nextopt)) {
2097 ctrl_grab = 1;
2098 } else if (strstart(opts, "off", &nextopt)) {
2099 ctrl_grab = 0;
2100 } else {
2101 goto invalid_sdl_args;
2103 } else if (strstart(opts, ",window_close=", &nextopt)) {
2104 opts = nextopt;
2105 dpy.has_window_close = true;
2106 if (strstart(opts, "on", &nextopt)) {
2107 dpy.window_close = true;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 dpy.window_close = false;
2110 } else {
2111 goto invalid_sdl_args;
2113 } else if (strstart(opts, ",gl=", &nextopt)) {
2114 opts = nextopt;
2115 dpy.has_gl = true;
2116 if (strstart(opts, "on", &nextopt)) {
2117 dpy.gl = DISPLAYGL_MODE_ON;
2118 } else if (strstart(opts, "core", &nextopt)) {
2119 dpy.gl = DISPLAYGL_MODE_CORE;
2120 } else if (strstart(opts, "es", &nextopt)) {
2121 dpy.gl = DISPLAYGL_MODE_ES;
2122 } else if (strstart(opts, "off", &nextopt)) {
2123 dpy.gl = DISPLAYGL_MODE_OFF;
2124 } else {
2125 goto invalid_sdl_args;
2127 } else {
2128 invalid_sdl_args:
2129 error_report("invalid SDL option string");
2130 exit(1);
2132 opts = nextopt;
2134 } else if (strstart(p, "vnc", &opts)) {
2136 * vnc isn't a (local) DisplayType but a protocol for remote
2137 * display access.
2139 if (*opts == '=') {
2140 vnc_parse(opts + 1, &error_fatal);
2141 } else {
2142 error_report("VNC requires a display argument vnc=<display>");
2143 exit(1);
2145 } else {
2146 parse_display_qapi(p);
2150 char *qemu_find_file(int type, const char *name)
2152 int i;
2153 const char *subdir;
2154 char *buf;
2156 /* Try the name as a straight path first */
2157 if (access(name, R_OK) == 0) {
2158 trace_load_file(name, name);
2159 return g_strdup(name);
2162 switch (type) {
2163 case QEMU_FILE_TYPE_BIOS:
2164 subdir = "";
2165 break;
2166 case QEMU_FILE_TYPE_KEYMAP:
2167 subdir = "keymaps/";
2168 break;
2169 default:
2170 abort();
2173 for (i = 0; i < data_dir_idx; i++) {
2174 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2175 if (access(buf, R_OK) == 0) {
2176 trace_load_file(name, buf);
2177 return buf;
2179 g_free(buf);
2181 return NULL;
2184 static void qemu_add_data_dir(const char *path)
2186 int i;
2188 if (path == NULL) {
2189 return;
2191 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2192 return;
2194 for (i = 0; i < data_dir_idx; i++) {
2195 if (strcmp(data_dir[i], path) == 0) {
2196 return; /* duplicate */
2199 data_dir[data_dir_idx++] = g_strdup(path);
2202 static inline bool nonempty_str(const char *str)
2204 return str && *str;
2207 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2209 gchar *buf;
2210 size_t size;
2211 const char *name, *file, *str;
2212 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2214 if (fw_cfg == NULL) {
2215 error_setg(errp, "fw_cfg device not available");
2216 return -1;
2218 name = qemu_opt_get(opts, "name");
2219 file = qemu_opt_get(opts, "file");
2220 str = qemu_opt_get(opts, "string");
2222 /* we need name and either a file or the content string */
2223 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2224 error_setg(errp, "invalid argument(s)");
2225 return -1;
2227 if (nonempty_str(file) && nonempty_str(str)) {
2228 error_setg(errp, "file and string are mutually exclusive");
2229 return -1;
2231 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2232 error_setg(errp, "name too long (max. %d char)",
2233 FW_CFG_MAX_FILE_PATH - 1);
2234 return -1;
2236 if (strncmp(name, "opt/", 4) != 0) {
2237 warn_report("externally provided fw_cfg item names "
2238 "should be prefixed with \"opt/\"");
2240 if (nonempty_str(str)) {
2241 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2242 buf = g_memdup(str, size);
2243 } else {
2244 GError *err = NULL;
2245 if (!g_file_get_contents(file, &buf, &size, &err)) {
2246 error_setg(errp, "can't load %s: %s", file, err->message);
2247 g_error_free(err);
2248 return -1;
2251 /* For legacy, keep user files in a specific global order. */
2252 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2253 fw_cfg_add_file(fw_cfg, name, buf, size);
2254 fw_cfg_reset_order_override(fw_cfg);
2255 return 0;
2258 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2260 return qdev_device_help(opts);
2263 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2265 DeviceState *dev;
2267 dev = qdev_device_add(opts, errp);
2268 if (!dev) {
2269 return -1;
2271 object_unref(OBJECT(dev));
2272 return 0;
2275 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2277 Error *local_err = NULL;
2279 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2280 if (local_err) {
2281 error_propagate(errp, local_err);
2282 return -1;
2284 exit(0);
2286 return 0;
2289 #ifdef CONFIG_VIRTFS
2290 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2292 return qemu_fsdev_add(opts, errp);
2294 #endif
2296 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2298 Chardev *chr;
2299 const char *chardev;
2300 const char *mode;
2301 int flags;
2303 mode = qemu_opt_get(opts, "mode");
2304 if (mode == NULL) {
2305 mode = "readline";
2307 if (strcmp(mode, "readline") == 0) {
2308 flags = MONITOR_USE_READLINE;
2309 } else if (strcmp(mode, "control") == 0) {
2310 flags = MONITOR_USE_CONTROL;
2311 } else {
2312 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2313 return -1;
2316 if (qemu_opt_get_bool(opts, "pretty", 0))
2317 flags |= MONITOR_USE_PRETTY;
2319 chardev = qemu_opt_get(opts, "chardev");
2320 if (!chardev) {
2321 error_report("chardev is required");
2322 exit(1);
2324 chr = qemu_chr_find(chardev);
2325 if (chr == NULL) {
2326 error_setg(errp, "chardev \"%s\" not found", chardev);
2327 return -1;
2330 monitor_init(chr, flags);
2331 return 0;
2334 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2336 static int monitor_device_index = 0;
2337 QemuOpts *opts;
2338 const char *p;
2339 char label[32];
2341 if (strstart(optarg, "chardev:", &p)) {
2342 snprintf(label, sizeof(label), "%s", p);
2343 } else {
2344 snprintf(label, sizeof(label), "compat_monitor%d",
2345 monitor_device_index);
2346 opts = qemu_chr_parse_compat(label, optarg, true);
2347 if (!opts) {
2348 error_report("parse error: %s", optarg);
2349 exit(1);
2353 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2354 qemu_opt_set(opts, "mode", mode, &error_abort);
2355 qemu_opt_set(opts, "chardev", label, &error_abort);
2356 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2357 monitor_device_index++;
2360 struct device_config {
2361 enum {
2362 DEV_USB, /* -usbdevice */
2363 DEV_BT, /* -bt */
2364 DEV_SERIAL, /* -serial */
2365 DEV_PARALLEL, /* -parallel */
2366 DEV_DEBUGCON, /* -debugcon */
2367 DEV_GDB, /* -gdb, -s */
2368 DEV_SCLP, /* s390 sclp */
2369 } type;
2370 const char *cmdline;
2371 Location loc;
2372 QTAILQ_ENTRY(device_config) next;
2375 static QTAILQ_HEAD(, device_config) device_configs =
2376 QTAILQ_HEAD_INITIALIZER(device_configs);
2378 static void add_device_config(int type, const char *cmdline)
2380 struct device_config *conf;
2382 conf = g_malloc0(sizeof(*conf));
2383 conf->type = type;
2384 conf->cmdline = cmdline;
2385 loc_save(&conf->loc);
2386 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2389 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2391 struct device_config *conf;
2392 int rc;
2394 QTAILQ_FOREACH(conf, &device_configs, next) {
2395 if (conf->type != type)
2396 continue;
2397 loc_push_restore(&conf->loc);
2398 rc = func(conf->cmdline);
2399 loc_pop(&conf->loc);
2400 if (rc) {
2401 return rc;
2404 return 0;
2407 static int serial_parse(const char *devname)
2409 int index = num_serial_hds;
2410 char label[32];
2412 if (strcmp(devname, "none") == 0)
2413 return 0;
2414 snprintf(label, sizeof(label), "serial%d", index);
2415 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2417 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2418 if (!serial_hds[index]) {
2419 error_report("could not connect serial device"
2420 " to character backend '%s'", devname);
2421 return -1;
2423 num_serial_hds++;
2424 return 0;
2427 Chardev *serial_hd(int i)
2429 assert(i >= 0);
2430 if (i < num_serial_hds) {
2431 return serial_hds[i];
2433 return NULL;
2436 int serial_max_hds(void)
2438 return num_serial_hds;
2441 static int parallel_parse(const char *devname)
2443 static int index = 0;
2444 char label[32];
2446 if (strcmp(devname, "none") == 0)
2447 return 0;
2448 if (index == MAX_PARALLEL_PORTS) {
2449 error_report("too many parallel ports");
2450 exit(1);
2452 snprintf(label, sizeof(label), "parallel%d", index);
2453 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2454 if (!parallel_hds[index]) {
2455 error_report("could not connect parallel device"
2456 " to character backend '%s'", devname);
2457 return -1;
2459 index++;
2460 return 0;
2463 static int debugcon_parse(const char *devname)
2465 QemuOpts *opts;
2467 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2468 error_report("invalid character backend '%s'", devname);
2469 exit(1);
2471 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2472 if (!opts) {
2473 error_report("already have a debugcon device");
2474 exit(1);
2476 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2477 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2478 return 0;
2481 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2483 const MachineClass *mc1 = a, *mc2 = b;
2484 int res;
2486 if (mc1->family == NULL) {
2487 if (mc2->family == NULL) {
2488 /* Compare standalone machine types against each other; they sort
2489 * in increasing order.
2491 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2492 object_class_get_name(OBJECT_CLASS(mc2)));
2495 /* Standalone machine types sort after families. */
2496 return 1;
2499 if (mc2->family == NULL) {
2500 /* Families sort before standalone machine types. */
2501 return -1;
2504 /* Families sort between each other alphabetically increasingly. */
2505 res = strcmp(mc1->family, mc2->family);
2506 if (res != 0) {
2507 return res;
2510 /* Within the same family, machine types sort in decreasing order. */
2511 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2512 object_class_get_name(OBJECT_CLASS(mc1)));
2515 static MachineClass *machine_parse(const char *name, GSList *machines)
2517 MachineClass *mc;
2518 GSList *el;
2520 if (is_help_option(name)) {
2521 printf("Supported machines are:\n");
2522 machines = g_slist_sort(machines, machine_class_cmp);
2523 for (el = machines; el; el = el->next) {
2524 MachineClass *mc = el->data;
2525 if (mc->alias) {
2526 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2528 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2529 mc->is_default ? " (default)" : "",
2530 mc->deprecation_reason ? " (deprecated)" : "");
2532 exit(0);
2535 mc = find_machine(name, machines);
2536 if (!mc) {
2537 error_report("unsupported machine type");
2538 error_printf("Use -machine help to list supported machines\n");
2539 exit(1);
2541 return mc;
2544 void qemu_add_exit_notifier(Notifier *notify)
2546 notifier_list_add(&exit_notifiers, notify);
2549 void qemu_remove_exit_notifier(Notifier *notify)
2551 notifier_remove(notify);
2554 static void qemu_run_exit_notifiers(void)
2556 notifier_list_notify(&exit_notifiers, NULL);
2559 static const char *pid_file;
2560 static Notifier qemu_unlink_pidfile_notifier;
2562 static void qemu_unlink_pidfile(Notifier *n, void *data)
2564 if (pid_file) {
2565 unlink(pid_file);
2569 bool machine_init_done;
2571 void qemu_add_machine_init_done_notifier(Notifier *notify)
2573 notifier_list_add(&machine_init_done_notifiers, notify);
2574 if (machine_init_done) {
2575 notify->notify(notify, NULL);
2579 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2581 notifier_remove(notify);
2584 static void qemu_run_machine_init_done_notifiers(void)
2586 machine_init_done = true;
2587 notifier_list_notify(&machine_init_done_notifiers, NULL);
2590 static const QEMUOption *lookup_opt(int argc, char **argv,
2591 const char **poptarg, int *poptind)
2593 const QEMUOption *popt;
2594 int optind = *poptind;
2595 char *r = argv[optind];
2596 const char *optarg;
2598 loc_set_cmdline(argv, optind, 1);
2599 optind++;
2600 /* Treat --foo the same as -foo. */
2601 if (r[1] == '-')
2602 r++;
2603 popt = qemu_options;
2604 for(;;) {
2605 if (!popt->name) {
2606 error_report("invalid option");
2607 exit(1);
2609 if (!strcmp(popt->name, r + 1))
2610 break;
2611 popt++;
2613 if (popt->flags & HAS_ARG) {
2614 if (optind >= argc) {
2615 error_report("requires an argument");
2616 exit(1);
2618 optarg = argv[optind++];
2619 loc_set_cmdline(argv, optind - 2, 2);
2620 } else {
2621 optarg = NULL;
2624 *poptarg = optarg;
2625 *poptind = optind;
2627 return popt;
2630 static MachineClass *select_machine(void)
2632 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2633 MachineClass *machine_class = find_default_machine(machines);
2634 const char *optarg;
2635 QemuOpts *opts;
2636 Location loc;
2638 loc_push_none(&loc);
2640 opts = qemu_get_machine_opts();
2641 qemu_opts_loc_restore(opts);
2643 optarg = qemu_opt_get(opts, "type");
2644 if (optarg) {
2645 machine_class = machine_parse(optarg, machines);
2648 if (!machine_class) {
2649 error_report("No machine specified, and there is no default");
2650 error_printf("Use -machine help to list supported machines\n");
2651 exit(1);
2654 loc_pop(&loc);
2655 g_slist_free(machines);
2656 return machine_class;
2659 static int machine_set_property(void *opaque,
2660 const char *name, const char *value,
2661 Error **errp)
2663 Object *obj = OBJECT(opaque);
2664 Error *local_err = NULL;
2665 char *p, *qom_name;
2667 if (strcmp(name, "type") == 0) {
2668 return 0;
2671 qom_name = g_strdup(name);
2672 for (p = qom_name; *p; p++) {
2673 if (*p == '_') {
2674 *p = '-';
2678 object_property_parse(obj, value, qom_name, &local_err);
2679 g_free(qom_name);
2681 if (local_err) {
2682 error_propagate(errp, local_err);
2683 return -1;
2686 return 0;
2691 * Initial object creation happens before all other
2692 * QEMU data types are created. The majority of objects
2693 * can be created at this point. The rng-egd object
2694 * cannot be created here, as it depends on the chardev
2695 * already existing.
2697 static bool object_create_initial(const char *type, QemuOpts *opts)
2699 ObjectClass *klass;
2701 if (is_help_option(type)) {
2702 GSList *l, *list;
2704 printf("List of user creatable objects:\n");
2705 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
2706 for (l = list; l != NULL; l = l->next) {
2707 ObjectClass *oc = OBJECT_CLASS(l->data);
2708 printf(" %s\n", object_class_get_name(oc));
2710 g_slist_free(list);
2711 exit(0);
2714 klass = object_class_by_name(type);
2715 if (klass && qemu_opt_has_help_opt(opts)) {
2716 ObjectPropertyIterator iter;
2717 ObjectProperty *prop;
2718 GPtrArray *array = g_ptr_array_new();
2719 int i;
2721 object_class_property_iter_init(&iter, klass);
2722 while ((prop = object_property_iter_next(&iter))) {
2723 GString *str;
2725 if (!prop->set) {
2726 continue;
2729 str = g_string_new(NULL);
2730 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
2731 if (prop->description) {
2732 if (str->len < 24) {
2733 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
2735 g_string_append_printf(str, " - %s", prop->description);
2737 g_ptr_array_add(array, g_string_free(str, false));
2739 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
2740 if (array->len > 0) {
2741 printf("%s options:\n", type);
2742 } else {
2743 printf("There are no options for %s.\n", type);
2745 for (i = 0; i < array->len; i++) {
2746 printf("%s\n", (char *)array->pdata[i]);
2748 g_ptr_array_set_free_func(array, g_free);
2749 g_ptr_array_free(array, true);
2750 exit(0);
2753 if (g_str_equal(type, "rng-egd") ||
2754 g_str_has_prefix(type, "pr-manager-")) {
2755 return false;
2758 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2759 if (g_str_equal(type, "cryptodev-vhost-user")) {
2760 return false;
2762 #endif
2765 * return false for concrete netfilters since
2766 * they depend on netdevs already existing
2768 if (g_str_equal(type, "filter-buffer") ||
2769 g_str_equal(type, "filter-dump") ||
2770 g_str_equal(type, "filter-mirror") ||
2771 g_str_equal(type, "filter-redirector") ||
2772 g_str_equal(type, "colo-compare") ||
2773 g_str_equal(type, "filter-rewriter") ||
2774 g_str_equal(type, "filter-replay")) {
2775 return false;
2778 /* Memory allocation by backends needs to be done
2779 * after configure_accelerator() (due to the tcg_enabled()
2780 * checks at memory_region_init_*()).
2782 * Also, allocation of large amounts of memory may delay
2783 * chardev initialization for too long, and trigger timeouts
2784 * on software that waits for a monitor socket to be created
2785 * (e.g. libvirt).
2787 if (g_str_has_prefix(type, "memory-backend-")) {
2788 return false;
2791 return true;
2796 * The remainder of object creation happens after the
2797 * creation of chardev, fsdev, net clients and device data types.
2799 static bool object_create_delayed(const char *type, QemuOpts *opts)
2801 return !object_create_initial(type, opts);
2805 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2806 MachineClass *mc)
2808 uint64_t sz;
2809 const char *mem_str;
2810 const ram_addr_t default_ram_size = mc->default_ram_size;
2811 QemuOpts *opts = qemu_find_opts_singleton("memory");
2812 Location loc;
2814 loc_push_none(&loc);
2815 qemu_opts_loc_restore(opts);
2817 sz = 0;
2818 mem_str = qemu_opt_get(opts, "size");
2819 if (mem_str) {
2820 if (!*mem_str) {
2821 error_report("missing 'size' option value");
2822 exit(EXIT_FAILURE);
2825 sz = qemu_opt_get_size(opts, "size", ram_size);
2827 /* Fix up legacy suffix-less format */
2828 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2829 uint64_t overflow_check = sz;
2831 sz *= MiB;
2832 if (sz / MiB != overflow_check) {
2833 error_report("too large 'size' option value");
2834 exit(EXIT_FAILURE);
2839 /* backward compatibility behaviour for case "-m 0" */
2840 if (sz == 0) {
2841 sz = default_ram_size;
2844 sz = QEMU_ALIGN_UP(sz, 8192);
2845 ram_size = sz;
2846 if (ram_size != sz) {
2847 error_report("ram size too large");
2848 exit(EXIT_FAILURE);
2851 /* store value for the future use */
2852 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2853 *maxram_size = ram_size;
2855 if (qemu_opt_get(opts, "maxmem")) {
2856 uint64_t slots;
2858 sz = qemu_opt_get_size(opts, "maxmem", 0);
2859 slots = qemu_opt_get_number(opts, "slots", 0);
2860 if (sz < ram_size) {
2861 error_report("invalid value of -m option maxmem: "
2862 "maximum memory size (0x%" PRIx64 ") must be at least "
2863 "the initial memory size (0x" RAM_ADDR_FMT ")",
2864 sz, ram_size);
2865 exit(EXIT_FAILURE);
2866 } else if (slots && sz == ram_size) {
2867 error_report("invalid value of -m option maxmem: "
2868 "memory slots were specified but maximum memory size "
2869 "(0x%" PRIx64 ") is equal to the initial memory size "
2870 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2871 exit(EXIT_FAILURE);
2874 *maxram_size = sz;
2875 *ram_slots = slots;
2876 } else if (qemu_opt_get(opts, "slots")) {
2877 error_report("invalid -m option value: missing 'maxmem' option");
2878 exit(EXIT_FAILURE);
2881 loc_pop(&loc);
2884 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2886 GlobalProperty *g;
2888 g = g_malloc0(sizeof(*g));
2889 g->driver = qemu_opt_get(opts, "driver");
2890 g->property = qemu_opt_get(opts, "property");
2891 g->value = qemu_opt_get(opts, "value");
2892 qdev_prop_register_global(g);
2893 return 0;
2896 static int qemu_read_default_config_file(void)
2898 int ret;
2900 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2901 if (ret < 0 && ret != -ENOENT) {
2902 return ret;
2905 return 0;
2908 static void user_register_global_props(void)
2910 qemu_opts_foreach(qemu_find_opts("global"),
2911 global_init_func, NULL, NULL);
2914 int main(int argc, char **argv, char **envp)
2916 int i;
2917 int snapshot, linux_boot;
2918 const char *initrd_filename;
2919 const char *kernel_filename, *kernel_cmdline;
2920 const char *boot_order = NULL;
2921 const char *boot_once = NULL;
2922 DisplayState *ds;
2923 QemuOpts *opts, *machine_opts;
2924 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2925 QemuOptsList *olist;
2926 int optind;
2927 const char *optarg;
2928 const char *loadvm = NULL;
2929 MachineClass *machine_class;
2930 const char *cpu_option;
2931 const char *vga_model = NULL;
2932 const char *qtest_chrdev = NULL;
2933 const char *qtest_log = NULL;
2934 const char *incoming = NULL;
2935 bool userconfig = true;
2936 bool nographic = false;
2937 int display_remote = 0;
2938 const char *log_mask = NULL;
2939 const char *log_file = NULL;
2940 char *trace_file = NULL;
2941 ram_addr_t maxram_size;
2942 uint64_t ram_slots = 0;
2943 FILE *vmstate_dump_file = NULL;
2944 Error *main_loop_err = NULL;
2945 Error *err = NULL;
2946 bool list_data_dirs = false;
2947 char *dir, **dirs;
2948 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2950 error_init(argv[0]);
2951 module_call_init(MODULE_INIT_TRACE);
2953 qemu_init_cpu_list();
2954 qemu_init_cpu_loop();
2956 qemu_mutex_lock_iothread();
2958 atexit(qemu_run_exit_notifiers);
2959 qemu_init_exec_dir(argv[0]);
2961 module_call_init(MODULE_INIT_QOM);
2963 qemu_add_opts(&qemu_drive_opts);
2964 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2965 qemu_add_drive_opts(&qemu_common_drive_opts);
2966 qemu_add_drive_opts(&qemu_drive_opts);
2967 qemu_add_drive_opts(&bdrv_runtime_opts);
2968 qemu_add_opts(&qemu_chardev_opts);
2969 qemu_add_opts(&qemu_device_opts);
2970 qemu_add_opts(&qemu_netdev_opts);
2971 qemu_add_opts(&qemu_nic_opts);
2972 qemu_add_opts(&qemu_net_opts);
2973 qemu_add_opts(&qemu_rtc_opts);
2974 qemu_add_opts(&qemu_global_opts);
2975 qemu_add_opts(&qemu_mon_opts);
2976 qemu_add_opts(&qemu_trace_opts);
2977 qemu_add_opts(&qemu_option_rom_opts);
2978 qemu_add_opts(&qemu_machine_opts);
2979 qemu_add_opts(&qemu_accel_opts);
2980 qemu_add_opts(&qemu_mem_opts);
2981 qemu_add_opts(&qemu_smp_opts);
2982 qemu_add_opts(&qemu_boot_opts);
2983 qemu_add_opts(&qemu_add_fd_opts);
2984 qemu_add_opts(&qemu_object_opts);
2985 qemu_add_opts(&qemu_tpmdev_opts);
2986 qemu_add_opts(&qemu_realtime_opts);
2987 qemu_add_opts(&qemu_overcommit_opts);
2988 qemu_add_opts(&qemu_msg_opts);
2989 qemu_add_opts(&qemu_name_opts);
2990 qemu_add_opts(&qemu_numa_opts);
2991 qemu_add_opts(&qemu_icount_opts);
2992 qemu_add_opts(&qemu_semihosting_config_opts);
2993 qemu_add_opts(&qemu_fw_cfg_opts);
2994 module_call_init(MODULE_INIT_OPTS);
2996 runstate_init();
2997 precopy_infrastructure_init();
2998 postcopy_infrastructure_init();
2999 monitor_init_globals();
3001 if (qcrypto_init(&err) < 0) {
3002 error_reportf_err(err, "cannot initialize crypto: ");
3003 exit(1);
3006 QLIST_INIT (&vm_change_state_head);
3007 os_setup_early_signal_handling();
3009 cpu_option = NULL;
3010 snapshot = 0;
3012 nb_nics = 0;
3014 bdrv_init_with_whitelist();
3016 autostart = 1;
3018 /* first pass of option parsing */
3019 optind = 1;
3020 while (optind < argc) {
3021 if (argv[optind][0] != '-') {
3022 /* disk image */
3023 optind++;
3024 } else {
3025 const QEMUOption *popt;
3027 popt = lookup_opt(argc, argv, &optarg, &optind);
3028 switch (popt->index) {
3029 case QEMU_OPTION_nouserconfig:
3030 userconfig = false;
3031 break;
3036 if (userconfig) {
3037 if (qemu_read_default_config_file() < 0) {
3038 exit(1);
3042 /* second pass of option parsing */
3043 optind = 1;
3044 for(;;) {
3045 if (optind >= argc)
3046 break;
3047 if (argv[optind][0] != '-') {
3048 loc_set_cmdline(argv, optind, 1);
3049 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3050 } else {
3051 const QEMUOption *popt;
3053 popt = lookup_opt(argc, argv, &optarg, &optind);
3054 if (!(popt->arch_mask & arch_type)) {
3055 error_report("Option not supported for this target");
3056 exit(1);
3058 switch(popt->index) {
3059 case QEMU_OPTION_cpu:
3060 /* hw initialization will check this */
3061 cpu_option = optarg;
3062 break;
3063 case QEMU_OPTION_hda:
3064 case QEMU_OPTION_hdb:
3065 case QEMU_OPTION_hdc:
3066 case QEMU_OPTION_hdd:
3067 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3068 HD_OPTS);
3069 break;
3070 case QEMU_OPTION_blockdev:
3072 Visitor *v;
3073 BlockdevOptionsQueueEntry *bdo;
3075 v = qobject_input_visitor_new_str(optarg, "driver",
3076 &error_fatal);
3078 bdo = g_new(BlockdevOptionsQueueEntry, 1);
3079 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3080 &error_fatal);
3081 visit_free(v);
3082 loc_save(&bdo->loc);
3083 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3084 break;
3086 case QEMU_OPTION_drive:
3087 if (drive_def(optarg) == NULL) {
3088 exit(1);
3090 break;
3091 case QEMU_OPTION_set:
3092 if (qemu_set_option(optarg) != 0)
3093 exit(1);
3094 break;
3095 case QEMU_OPTION_global:
3096 if (qemu_global_option(optarg) != 0)
3097 exit(1);
3098 break;
3099 case QEMU_OPTION_mtdblock:
3100 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3101 break;
3102 case QEMU_OPTION_sd:
3103 drive_add(IF_SD, -1, optarg, SD_OPTS);
3104 break;
3105 case QEMU_OPTION_pflash:
3106 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3107 break;
3108 case QEMU_OPTION_snapshot:
3109 snapshot = 1;
3110 break;
3111 case QEMU_OPTION_numa:
3112 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3113 optarg, true);
3114 if (!opts) {
3115 exit(1);
3117 break;
3118 case QEMU_OPTION_display:
3119 parse_display(optarg);
3120 break;
3121 case QEMU_OPTION_nographic:
3122 olist = qemu_find_opts("machine");
3123 qemu_opts_parse_noisily(olist, "graphics=off", false);
3124 nographic = true;
3125 dpy.type = DISPLAY_TYPE_NONE;
3126 break;
3127 case QEMU_OPTION_curses:
3128 #ifdef CONFIG_CURSES
3129 dpy.type = DISPLAY_TYPE_CURSES;
3130 #else
3131 error_report("curses or iconv support is disabled");
3132 exit(1);
3133 #endif
3134 break;
3135 case QEMU_OPTION_portrait:
3136 graphic_rotate = 90;
3137 break;
3138 case QEMU_OPTION_rotate:
3139 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3140 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3141 graphic_rotate != 180 && graphic_rotate != 270) {
3142 error_report("only 90, 180, 270 deg rotation is available");
3143 exit(1);
3145 break;
3146 case QEMU_OPTION_kernel:
3147 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3148 &error_abort);
3149 break;
3150 case QEMU_OPTION_initrd:
3151 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3152 &error_abort);
3153 break;
3154 case QEMU_OPTION_append:
3155 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3156 &error_abort);
3157 break;
3158 case QEMU_OPTION_dtb:
3159 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3160 &error_abort);
3161 break;
3162 case QEMU_OPTION_cdrom:
3163 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3164 break;
3165 case QEMU_OPTION_boot:
3166 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3167 optarg, true);
3168 if (!opts) {
3169 exit(1);
3171 break;
3172 case QEMU_OPTION_fda:
3173 case QEMU_OPTION_fdb:
3174 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3175 optarg, FD_OPTS);
3176 break;
3177 case QEMU_OPTION_no_fd_bootchk:
3178 fd_bootchk = 0;
3179 break;
3180 case QEMU_OPTION_netdev:
3181 default_net = 0;
3182 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3183 exit(1);
3185 break;
3186 case QEMU_OPTION_nic:
3187 default_net = 0;
3188 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3189 exit(1);
3191 break;
3192 case QEMU_OPTION_net:
3193 default_net = 0;
3194 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3195 exit(1);
3197 break;
3198 #ifdef CONFIG_LIBISCSI
3199 case QEMU_OPTION_iscsi:
3200 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3201 optarg, false);
3202 if (!opts) {
3203 exit(1);
3205 break;
3206 #endif
3207 case QEMU_OPTION_bt:
3208 warn_report("The bluetooth subsystem is deprecated and will "
3209 "be removed soon. If the bluetooth subsystem is "
3210 "still useful for you, please send a mail to "
3211 "qemu-devel@nongnu.org with your usecase.");
3212 add_device_config(DEV_BT, optarg);
3213 break;
3214 case QEMU_OPTION_audio_help:
3215 audio_legacy_help();
3216 exit (0);
3217 break;
3218 case QEMU_OPTION_audiodev:
3219 audio_parse_option(optarg);
3220 break;
3221 case QEMU_OPTION_soundhw:
3222 select_soundhw (optarg);
3223 break;
3224 case QEMU_OPTION_h:
3225 help(0);
3226 break;
3227 case QEMU_OPTION_version:
3228 version();
3229 exit(0);
3230 break;
3231 case QEMU_OPTION_m:
3232 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3233 optarg, true);
3234 if (!opts) {
3235 exit(EXIT_FAILURE);
3237 break;
3238 #ifdef CONFIG_TPM
3239 case QEMU_OPTION_tpmdev:
3240 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3241 exit(1);
3243 break;
3244 #endif
3245 case QEMU_OPTION_mempath:
3246 mem_path = optarg;
3247 break;
3248 case QEMU_OPTION_mem_prealloc:
3249 mem_prealloc = 1;
3250 break;
3251 case QEMU_OPTION_d:
3252 log_mask = optarg;
3253 break;
3254 case QEMU_OPTION_D:
3255 log_file = optarg;
3256 break;
3257 case QEMU_OPTION_DFILTER:
3258 qemu_set_dfilter_ranges(optarg, &error_fatal);
3259 break;
3260 case QEMU_OPTION_seed:
3261 qemu_guest_random_seed_main(optarg, &error_fatal);
3262 break;
3263 case QEMU_OPTION_s:
3264 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3265 break;
3266 case QEMU_OPTION_gdb:
3267 add_device_config(DEV_GDB, optarg);
3268 break;
3269 case QEMU_OPTION_L:
3270 if (is_help_option(optarg)) {
3271 list_data_dirs = true;
3272 } else {
3273 qemu_add_data_dir(optarg);
3275 break;
3276 case QEMU_OPTION_bios:
3277 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3278 &error_abort);
3279 break;
3280 case QEMU_OPTION_singlestep:
3281 singlestep = 1;
3282 break;
3283 case QEMU_OPTION_S:
3284 autostart = 0;
3285 break;
3286 case QEMU_OPTION_k:
3287 keyboard_layout = optarg;
3288 break;
3289 case QEMU_OPTION_vga:
3290 vga_model = optarg;
3291 default_vga = 0;
3292 break;
3293 case QEMU_OPTION_g:
3295 const char *p;
3296 int w, h, depth;
3297 p = optarg;
3298 w = strtol(p, (char **)&p, 10);
3299 if (w <= 0) {
3300 graphic_error:
3301 error_report("invalid resolution or depth");
3302 exit(1);
3304 if (*p != 'x')
3305 goto graphic_error;
3306 p++;
3307 h = strtol(p, (char **)&p, 10);
3308 if (h <= 0)
3309 goto graphic_error;
3310 if (*p == 'x') {
3311 p++;
3312 depth = strtol(p, (char **)&p, 10);
3313 if (depth != 8 && depth != 15 && depth != 16 &&
3314 depth != 24 && depth != 32)
3315 goto graphic_error;
3316 } else if (*p == '\0') {
3317 depth = graphic_depth;
3318 } else {
3319 goto graphic_error;
3322 graphic_width = w;
3323 graphic_height = h;
3324 graphic_depth = depth;
3326 break;
3327 case QEMU_OPTION_echr:
3329 char *r;
3330 term_escape_char = strtol(optarg, &r, 0);
3331 if (r == optarg)
3332 printf("Bad argument to echr\n");
3333 break;
3335 case QEMU_OPTION_monitor:
3336 default_monitor = 0;
3337 if (strncmp(optarg, "none", 4)) {
3338 monitor_parse(optarg, "readline", false);
3340 break;
3341 case QEMU_OPTION_qmp:
3342 monitor_parse(optarg, "control", false);
3343 default_monitor = 0;
3344 break;
3345 case QEMU_OPTION_qmp_pretty:
3346 monitor_parse(optarg, "control", true);
3347 default_monitor = 0;
3348 break;
3349 case QEMU_OPTION_mon:
3350 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3351 true);
3352 if (!opts) {
3353 exit(1);
3355 default_monitor = 0;
3356 break;
3357 case QEMU_OPTION_chardev:
3358 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3359 optarg, true);
3360 if (!opts) {
3361 exit(1);
3363 break;
3364 case QEMU_OPTION_fsdev:
3365 olist = qemu_find_opts("fsdev");
3366 if (!olist) {
3367 error_report("fsdev support is disabled");
3368 exit(1);
3370 opts = qemu_opts_parse_noisily(olist, optarg, true);
3371 if (!opts) {
3372 exit(1);
3374 break;
3375 case QEMU_OPTION_virtfs: {
3376 QemuOpts *fsdev;
3377 QemuOpts *device;
3378 const char *writeout, *sock_fd, *socket, *path, *security_model;
3380 olist = qemu_find_opts("virtfs");
3381 if (!olist) {
3382 error_report("virtfs support is disabled");
3383 exit(1);
3385 opts = qemu_opts_parse_noisily(olist, optarg, true);
3386 if (!opts) {
3387 exit(1);
3390 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3391 qemu_opt_get(opts, "mount_tag") == NULL) {
3392 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3393 exit(1);
3395 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3396 qemu_opts_id(opts) ?:
3397 qemu_opt_get(opts, "mount_tag"),
3398 1, NULL);
3399 if (!fsdev) {
3400 error_report("duplicate or invalid fsdev id: %s",
3401 qemu_opt_get(opts, "mount_tag"));
3402 exit(1);
3405 writeout = qemu_opt_get(opts, "writeout");
3406 if (writeout) {
3407 #ifdef CONFIG_SYNC_FILE_RANGE
3408 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3409 #else
3410 error_report("writeout=immediate not supported "
3411 "on this platform");
3412 exit(1);
3413 #endif
3415 qemu_opt_set(fsdev, "fsdriver",
3416 qemu_opt_get(opts, "fsdriver"), &error_abort);
3417 path = qemu_opt_get(opts, "path");
3418 if (path) {
3419 qemu_opt_set(fsdev, "path", path, &error_abort);
3421 security_model = qemu_opt_get(opts, "security_model");
3422 if (security_model) {
3423 qemu_opt_set(fsdev, "security_model", security_model,
3424 &error_abort);
3426 socket = qemu_opt_get(opts, "socket");
3427 if (socket) {
3428 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3430 sock_fd = qemu_opt_get(opts, "sock_fd");
3431 if (sock_fd) {
3432 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3435 qemu_opt_set_bool(fsdev, "readonly",
3436 qemu_opt_get_bool(opts, "readonly", 0),
3437 &error_abort);
3438 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3439 &error_abort);
3440 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3441 qemu_opt_set(device, "fsdev",
3442 qemu_opts_id(fsdev), &error_abort);
3443 qemu_opt_set(device, "mount_tag",
3444 qemu_opt_get(opts, "mount_tag"), &error_abort);
3445 break;
3447 case QEMU_OPTION_virtfs_synth: {
3448 QemuOpts *fsdev;
3449 QemuOpts *device;
3451 warn_report("'-virtfs_synth' is deprecated, please use "
3452 "'-fsdev synth' and '-device virtio-9p-...' "
3453 "instead");
3455 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3456 1, NULL);
3457 if (!fsdev) {
3458 error_report("duplicate option: %s", "virtfs_synth");
3459 exit(1);
3461 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3463 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3464 &error_abort);
3465 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3466 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3467 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3468 break;
3470 case QEMU_OPTION_serial:
3471 add_device_config(DEV_SERIAL, optarg);
3472 default_serial = 0;
3473 if (strncmp(optarg, "mon:", 4) == 0) {
3474 default_monitor = 0;
3476 break;
3477 case QEMU_OPTION_watchdog:
3478 if (watchdog) {
3479 error_report("only one watchdog option may be given");
3480 return 1;
3482 watchdog = optarg;
3483 break;
3484 case QEMU_OPTION_watchdog_action:
3485 if (select_watchdog_action(optarg) == -1) {
3486 error_report("unknown -watchdog-action parameter");
3487 exit(1);
3489 break;
3490 case QEMU_OPTION_parallel:
3491 add_device_config(DEV_PARALLEL, optarg);
3492 default_parallel = 0;
3493 if (strncmp(optarg, "mon:", 4) == 0) {
3494 default_monitor = 0;
3496 break;
3497 case QEMU_OPTION_debugcon:
3498 add_device_config(DEV_DEBUGCON, optarg);
3499 break;
3500 case QEMU_OPTION_loadvm:
3501 loadvm = optarg;
3502 break;
3503 case QEMU_OPTION_full_screen:
3504 dpy.has_full_screen = true;
3505 dpy.full_screen = true;
3506 break;
3507 case QEMU_OPTION_alt_grab:
3508 alt_grab = 1;
3509 break;
3510 case QEMU_OPTION_ctrl_grab:
3511 ctrl_grab = 1;
3512 break;
3513 case QEMU_OPTION_no_quit:
3514 dpy.has_window_close = true;
3515 dpy.window_close = false;
3516 break;
3517 case QEMU_OPTION_sdl:
3518 #ifdef CONFIG_SDL
3519 dpy.type = DISPLAY_TYPE_SDL;
3520 break;
3521 #else
3522 error_report("SDL support is disabled");
3523 exit(1);
3524 #endif
3525 case QEMU_OPTION_pidfile:
3526 pid_file = optarg;
3527 break;
3528 case QEMU_OPTION_win2k_hack:
3529 win2k_install_hack = 1;
3530 break;
3531 case QEMU_OPTION_acpitable:
3532 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3533 optarg, true);
3534 if (!opts) {
3535 exit(1);
3537 acpi_table_add(opts, &error_fatal);
3538 break;
3539 case QEMU_OPTION_smbios:
3540 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3541 optarg, false);
3542 if (!opts) {
3543 exit(1);
3545 smbios_entry_add(opts, &error_fatal);
3546 break;
3547 case QEMU_OPTION_fwcfg:
3548 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3549 optarg, true);
3550 if (opts == NULL) {
3551 exit(1);
3553 break;
3554 case QEMU_OPTION_preconfig:
3555 preconfig_exit_requested = false;
3556 break;
3557 case QEMU_OPTION_enable_kvm:
3558 olist = qemu_find_opts("machine");
3559 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3560 break;
3561 case QEMU_OPTION_M:
3562 case QEMU_OPTION_machine:
3563 olist = qemu_find_opts("machine");
3564 opts = qemu_opts_parse_noisily(olist, optarg, true);
3565 if (!opts) {
3566 exit(1);
3568 break;
3569 case QEMU_OPTION_no_kvm:
3570 olist = qemu_find_opts("machine");
3571 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3572 break;
3573 case QEMU_OPTION_accel:
3574 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3575 optarg, true);
3576 optarg = qemu_opt_get(accel_opts, "accel");
3577 if (!optarg || is_help_option(optarg)) {
3578 printf("Possible accelerators: kvm, xen, hax, tcg\n");
3579 exit(0);
3581 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3582 false, &error_abort);
3583 qemu_opt_set(opts, "accel", optarg, &error_abort);
3584 break;
3585 case QEMU_OPTION_usb:
3586 olist = qemu_find_opts("machine");
3587 qemu_opts_parse_noisily(olist, "usb=on", false);
3588 break;
3589 case QEMU_OPTION_usbdevice:
3590 error_report("'-usbdevice' is deprecated, please use "
3591 "'-device usb-...' instead");
3592 olist = qemu_find_opts("machine");
3593 qemu_opts_parse_noisily(olist, "usb=on", false);
3594 add_device_config(DEV_USB, optarg);
3595 break;
3596 case QEMU_OPTION_device:
3597 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3598 optarg, true)) {
3599 exit(1);
3601 break;
3602 case QEMU_OPTION_smp:
3603 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3604 optarg, true)) {
3605 exit(1);
3607 break;
3608 case QEMU_OPTION_vnc:
3609 vnc_parse(optarg, &error_fatal);
3610 break;
3611 case QEMU_OPTION_no_acpi:
3612 acpi_enabled = 0;
3613 break;
3614 case QEMU_OPTION_no_hpet:
3615 no_hpet = 1;
3616 break;
3617 case QEMU_OPTION_no_reboot:
3618 no_reboot = 1;
3619 break;
3620 case QEMU_OPTION_no_shutdown:
3621 no_shutdown = 1;
3622 break;
3623 case QEMU_OPTION_show_cursor:
3624 cursor_hide = 0;
3625 break;
3626 case QEMU_OPTION_uuid:
3627 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3628 error_report("failed to parse UUID string: wrong format");
3629 exit(1);
3631 qemu_uuid_set = true;
3632 break;
3633 case QEMU_OPTION_option_rom:
3634 if (nb_option_roms >= MAX_OPTION_ROMS) {
3635 error_report("too many option ROMs");
3636 exit(1);
3638 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3639 optarg, true);
3640 if (!opts) {
3641 exit(1);
3643 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3644 option_rom[nb_option_roms].bootindex =
3645 qemu_opt_get_number(opts, "bootindex", -1);
3646 if (!option_rom[nb_option_roms].name) {
3647 error_report("Option ROM file is not specified");
3648 exit(1);
3650 nb_option_roms++;
3651 break;
3652 case QEMU_OPTION_semihosting:
3653 qemu_semihosting_enable();
3654 break;
3655 case QEMU_OPTION_semihosting_config:
3656 if (qemu_semihosting_config_options(optarg) != 0) {
3657 exit(1);
3659 break;
3660 case QEMU_OPTION_name:
3661 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3662 optarg, true);
3663 if (!opts) {
3664 exit(1);
3666 break;
3667 case QEMU_OPTION_prom_env:
3668 if (nb_prom_envs >= MAX_PROM_ENVS) {
3669 error_report("too many prom variables");
3670 exit(1);
3672 prom_envs[nb_prom_envs] = optarg;
3673 nb_prom_envs++;
3674 break;
3675 case QEMU_OPTION_old_param:
3676 old_param = 1;
3677 break;
3678 case QEMU_OPTION_rtc:
3679 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3680 false);
3681 if (!opts) {
3682 exit(1);
3684 break;
3685 case QEMU_OPTION_tb_size:
3686 #ifndef CONFIG_TCG
3687 error_report("TCG is disabled");
3688 exit(1);
3689 #endif
3690 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3691 error_report("Invalid argument to -tb-size");
3692 exit(1);
3694 break;
3695 case QEMU_OPTION_icount:
3696 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3697 optarg, true);
3698 if (!icount_opts) {
3699 exit(1);
3701 break;
3702 case QEMU_OPTION_incoming:
3703 if (!incoming) {
3704 runstate_set(RUN_STATE_INMIGRATE);
3706 incoming = optarg;
3707 break;
3708 case QEMU_OPTION_only_migratable:
3709 only_migratable = 1;
3710 break;
3711 case QEMU_OPTION_nodefaults:
3712 has_defaults = 0;
3713 break;
3714 case QEMU_OPTION_xen_domid:
3715 if (!(xen_available())) {
3716 error_report("Option not supported for this target");
3717 exit(1);
3719 xen_domid = atoi(optarg);
3720 break;
3721 case QEMU_OPTION_xen_attach:
3722 if (!(xen_available())) {
3723 error_report("Option not supported for this target");
3724 exit(1);
3726 xen_mode = XEN_ATTACH;
3727 break;
3728 case QEMU_OPTION_xen_domid_restrict:
3729 if (!(xen_available())) {
3730 error_report("Option not supported for this target");
3731 exit(1);
3733 xen_domid_restrict = true;
3734 break;
3735 case QEMU_OPTION_trace:
3736 g_free(trace_file);
3737 trace_file = trace_opt_parse(optarg);
3738 break;
3739 case QEMU_OPTION_readconfig:
3741 int ret = qemu_read_config_file(optarg);
3742 if (ret < 0) {
3743 error_report("read config %s: %s", optarg,
3744 strerror(-ret));
3745 exit(1);
3747 break;
3749 case QEMU_OPTION_spice:
3750 olist = qemu_find_opts("spice");
3751 if (!olist) {
3752 error_report("spice support is disabled");
3753 exit(1);
3755 opts = qemu_opts_parse_noisily(olist, optarg, false);
3756 if (!opts) {
3757 exit(1);
3759 display_remote++;
3760 break;
3761 case QEMU_OPTION_writeconfig:
3763 FILE *fp;
3764 if (strcmp(optarg, "-") == 0) {
3765 fp = stdout;
3766 } else {
3767 fp = fopen(optarg, "w");
3768 if (fp == NULL) {
3769 error_report("open %s: %s", optarg,
3770 strerror(errno));
3771 exit(1);
3774 qemu_config_write(fp);
3775 if (fp != stdout) {
3776 fclose(fp);
3778 break;
3780 case QEMU_OPTION_qtest:
3781 qtest_chrdev = optarg;
3782 break;
3783 case QEMU_OPTION_qtest_log:
3784 qtest_log = optarg;
3785 break;
3786 case QEMU_OPTION_sandbox:
3787 olist = qemu_find_opts("sandbox");
3788 if (!olist) {
3789 #ifndef CONFIG_SECCOMP
3790 error_report("-sandbox support is not enabled "
3791 "in this QEMU binary");
3792 #endif
3793 exit(1);
3796 opts = qemu_opts_parse_noisily(olist, optarg, true);
3797 if (!opts) {
3798 exit(1);
3800 break;
3801 case QEMU_OPTION_add_fd:
3802 #ifndef _WIN32
3803 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3804 optarg, false);
3805 if (!opts) {
3806 exit(1);
3808 #else
3809 error_report("File descriptor passing is disabled on this "
3810 "platform");
3811 exit(1);
3812 #endif
3813 break;
3814 case QEMU_OPTION_object:
3815 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3816 optarg, true);
3817 if (!opts) {
3818 exit(1);
3820 break;
3821 case QEMU_OPTION_realtime:
3822 warn_report("'-realtime mlock=...' is deprecated, please use "
3823 "'-overcommit mem-lock=...' instead");
3824 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3825 optarg, false);
3826 if (!opts) {
3827 exit(1);
3829 /* Don't override the -overcommit option if set */
3830 enable_mlock = enable_mlock ||
3831 qemu_opt_get_bool(opts, "mlock", true);
3832 break;
3833 case QEMU_OPTION_overcommit:
3834 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3835 optarg, false);
3836 if (!opts) {
3837 exit(1);
3839 /* Don't override the -realtime option if set */
3840 enable_mlock = enable_mlock ||
3841 qemu_opt_get_bool(opts, "mem-lock", false);
3842 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3843 break;
3844 case QEMU_OPTION_msg:
3845 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3846 false);
3847 if (!opts) {
3848 exit(1);
3850 configure_msg(opts);
3851 break;
3852 case QEMU_OPTION_dump_vmstate:
3853 if (vmstate_dump_file) {
3854 error_report("only one '-dump-vmstate' "
3855 "option may be given");
3856 exit(1);
3858 vmstate_dump_file = fopen(optarg, "w");
3859 if (vmstate_dump_file == NULL) {
3860 error_report("open %s: %s", optarg, strerror(errno));
3861 exit(1);
3863 break;
3864 case QEMU_OPTION_enable_sync_profile:
3865 qsp_enable();
3866 break;
3867 case QEMU_OPTION_nouserconfig:
3868 /* Nothing to be parsed here. Especially, do not error out below. */
3869 break;
3870 default:
3871 if (os_parse_cmd_args(popt->index, optarg)) {
3872 error_report("Option not supported in this build");
3873 exit(1);
3879 * Clear error location left behind by the loop.
3880 * Best done right after the loop. Do not insert code here!
3882 loc_set_none();
3884 user_register_global_props();
3886 replay_configure(icount_opts);
3888 if (incoming && !preconfig_exit_requested) {
3889 error_report("'preconfig' and 'incoming' options are "
3890 "mutually exclusive");
3891 exit(EXIT_FAILURE);
3894 configure_rtc(qemu_find_opts_singleton("rtc"));
3896 machine_class = select_machine();
3897 object_set_machine_compat_props(machine_class->compat_props);
3899 set_memory_options(&ram_slots, &maxram_size, machine_class);
3901 os_daemonize();
3902 rcu_disable_atfork();
3904 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3905 error_reportf_err(err, "cannot create PID file: ");
3906 exit(1);
3909 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3910 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3912 if (qemu_init_main_loop(&main_loop_err)) {
3913 error_report_err(main_loop_err);
3914 exit(1);
3917 #ifdef CONFIG_SECCOMP
3918 olist = qemu_find_opts_err("sandbox", NULL);
3919 if (olist) {
3920 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
3922 #endif
3924 qemu_opts_foreach(qemu_find_opts("name"),
3925 parse_name, NULL, &error_fatal);
3927 #ifndef _WIN32
3928 qemu_opts_foreach(qemu_find_opts("add-fd"),
3929 parse_add_fd, NULL, &error_fatal);
3931 qemu_opts_foreach(qemu_find_opts("add-fd"),
3932 cleanup_add_fd, NULL, &error_fatal);
3933 #endif
3935 current_machine = MACHINE(object_new(object_class_get_name(
3936 OBJECT_CLASS(machine_class))));
3937 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3938 exit(0);
3940 object_property_add_child(object_get_root(), "machine",
3941 OBJECT(current_machine), &error_abort);
3942 object_property_add_child(container_get(OBJECT(current_machine),
3943 "/unattached"),
3944 "sysbus", OBJECT(sysbus_get_default()),
3945 NULL);
3947 if (machine_class->minimum_page_bits) {
3948 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3949 /* This would be a board error: specifying a minimum smaller than
3950 * a target's compile-time fixed setting.
3952 g_assert_not_reached();
3956 cpu_exec_init_all();
3958 if (machine_class->hw_version) {
3959 qemu_set_hw_version(machine_class->hw_version);
3962 if (cpu_option && is_help_option(cpu_option)) {
3963 list_cpus(cpu_option);
3964 exit(0);
3967 if (!trace_init_backends()) {
3968 exit(1);
3970 trace_init_file(trace_file);
3972 /* Open the logfile at this point and set the log mask if necessary.
3974 if (log_file) {
3975 qemu_set_log_filename(log_file, &error_fatal);
3978 if (log_mask) {
3979 int mask;
3980 mask = qemu_str_to_log_mask(log_mask);
3981 if (!mask) {
3982 qemu_print_log_usage(stdout);
3983 exit(1);
3985 qemu_set_log(mask);
3986 } else {
3987 qemu_set_log(0);
3990 /* add configured firmware directories */
3991 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
3992 for (i = 0; dirs[i] != NULL; i++) {
3993 qemu_add_data_dir(dirs[i]);
3995 g_strfreev(dirs);
3997 /* try to find datadir relative to the executable path */
3998 dir = os_find_datadir();
3999 qemu_add_data_dir(dir);
4000 g_free(dir);
4002 /* add the datadir specified when building */
4003 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4005 /* -L help lists the data directories and exits. */
4006 if (list_data_dirs) {
4007 for (i = 0; i < data_dir_idx; i++) {
4008 printf("%s\n", data_dir[i]);
4010 exit(0);
4013 /* machine_class: default to UP */
4014 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4015 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4016 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4018 /* default to machine_class->default_cpus */
4019 smp_cpus = machine_class->default_cpus;
4020 max_cpus = machine_class->default_cpus;
4022 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4024 /* sanity-check smp_cpus and max_cpus against machine_class */
4025 if (smp_cpus < machine_class->min_cpus) {
4026 error_report("Invalid SMP CPUs %d. The min CPUs "
4027 "supported by machine '%s' is %d", smp_cpus,
4028 machine_class->name, machine_class->min_cpus);
4029 exit(1);
4031 if (max_cpus > machine_class->max_cpus) {
4032 error_report("Invalid SMP CPUs %d. The max CPUs "
4033 "supported by machine '%s' is %d", max_cpus,
4034 machine_class->name, machine_class->max_cpus);
4035 exit(1);
4039 * Get the default machine options from the machine if it is not already
4040 * specified either by the configuration file or by the command line.
4042 if (machine_class->default_machine_opts) {
4043 qemu_opts_set_defaults(qemu_find_opts("machine"),
4044 machine_class->default_machine_opts, 0);
4047 qemu_opts_foreach(qemu_find_opts("device"),
4048 default_driver_check, NULL, NULL);
4049 qemu_opts_foreach(qemu_find_opts("global"),
4050 default_driver_check, NULL, NULL);
4052 if (!vga_model && !default_vga) {
4053 vga_interface_type = VGA_DEVICE;
4055 if (!has_defaults || machine_class->no_serial) {
4056 default_serial = 0;
4058 if (!has_defaults || machine_class->no_parallel) {
4059 default_parallel = 0;
4061 if (!has_defaults || machine_class->no_floppy) {
4062 default_floppy = 0;
4064 if (!has_defaults || machine_class->no_cdrom) {
4065 default_cdrom = 0;
4067 if (!has_defaults || machine_class->no_sdcard) {
4068 default_sdcard = 0;
4070 if (!has_defaults) {
4071 default_monitor = 0;
4072 default_net = 0;
4073 default_vga = 0;
4076 if (is_daemonized()) {
4077 if (!preconfig_exit_requested) {
4078 error_report("'preconfig' and 'daemonize' options are "
4079 "mutually exclusive");
4080 exit(EXIT_FAILURE);
4083 /* According to documentation and historically, -nographic redirects
4084 * serial port, parallel port and monitor to stdio, which does not work
4085 * with -daemonize. We can redirect these to null instead, but since
4086 * -nographic is legacy, let's just error out.
4087 * We disallow -nographic only if all other ports are not redirected
4088 * explicitly, to not break existing legacy setups which uses
4089 * -nographic _and_ redirects all ports explicitly - this is valid
4090 * usage, -nographic is just a no-op in this case.
4092 if (nographic
4093 && (default_parallel || default_serial || default_monitor)) {
4094 error_report("-nographic cannot be used with -daemonize");
4095 exit(1);
4097 #ifdef CONFIG_CURSES
4098 if (dpy.type == DISPLAY_TYPE_CURSES) {
4099 error_report("curses display cannot be used with -daemonize");
4100 exit(1);
4102 #endif
4105 if (nographic) {
4106 if (default_parallel)
4107 add_device_config(DEV_PARALLEL, "null");
4108 if (default_serial && default_monitor) {
4109 add_device_config(DEV_SERIAL, "mon:stdio");
4110 } else {
4111 if (default_serial)
4112 add_device_config(DEV_SERIAL, "stdio");
4113 if (default_monitor)
4114 monitor_parse("stdio", "readline", false);
4116 } else {
4117 if (default_serial)
4118 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4119 if (default_parallel)
4120 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4121 if (default_monitor)
4122 monitor_parse("vc:80Cx24C", "readline", false);
4125 #if defined(CONFIG_VNC)
4126 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4127 display_remote++;
4129 #endif
4130 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4131 if (!qemu_display_find_default(&dpy)) {
4132 dpy.type = DISPLAY_TYPE_NONE;
4133 #if defined(CONFIG_VNC)
4134 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4135 #endif
4138 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4139 dpy.type = DISPLAY_TYPE_NONE;
4142 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4143 error_report("-alt-grab and -ctrl-grab are only valid "
4144 "for SDL, ignoring option");
4146 if (dpy.has_window_close &&
4147 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4148 error_report("-no-quit is only valid for GTK and SDL, "
4149 "ignoring option");
4152 qemu_display_early_init(&dpy);
4153 qemu_console_early_init();
4155 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4156 #if defined(CONFIG_OPENGL)
4157 error_report("OpenGL is not supported by the display");
4158 #else
4159 error_report("OpenGL support is disabled");
4160 #endif
4161 exit(1);
4164 page_size_init();
4165 socket_init();
4167 qemu_opts_foreach(qemu_find_opts("object"),
4168 user_creatable_add_opts_foreach,
4169 object_create_initial, &error_fatal);
4171 qemu_opts_foreach(qemu_find_opts("chardev"),
4172 chardev_init_func, NULL, &error_fatal);
4173 /* now chardevs have been created we may have semihosting to connect */
4174 qemu_semihosting_connect_chardevs();
4176 #ifdef CONFIG_VIRTFS
4177 qemu_opts_foreach(qemu_find_opts("fsdev"),
4178 fsdev_init_func, NULL, &error_fatal);
4179 #endif
4181 if (qemu_opts_foreach(qemu_find_opts("device"),
4182 device_help_func, NULL, NULL)) {
4183 exit(0);
4187 * Note: we need to create block backends before
4188 * machine_set_property(), so machine properties can refer to
4189 * them.
4191 configure_blockdev(&bdo_queue, machine_class, snapshot);
4193 machine_opts = qemu_get_machine_opts();
4194 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4195 &error_fatal);
4196 current_machine->ram_size = ram_size;
4197 current_machine->maxram_size = maxram_size;
4198 current_machine->ram_slots = ram_slots;
4201 * Note: uses machine properties such as kernel-irqchip, must run
4202 * after machine_set_property().
4204 configure_accelerator(current_machine, argv[0]);
4207 * Beware, QOM objects created before this point miss global and
4208 * compat properties.
4210 * Global properties get set up by qdev_prop_register_global(),
4211 * called from user_register_global_props(), and certain option
4212 * desugaring. Also in CPU feature desugaring (buried in
4213 * parse_cpu_option()), which happens below this point, but may
4214 * only target the CPU type, which can only be created after
4215 * parse_cpu_option() returned the type.
4217 * Machine compat properties: object_set_machine_compat_props().
4218 * Accelerator compat props: object_set_accelerator_compat_props(),
4219 * called from configure_accelerator().
4222 if (!qtest_enabled() && machine_class->deprecation_reason) {
4223 error_report("Machine type '%s' is deprecated: %s",
4224 machine_class->name, machine_class->deprecation_reason);
4228 * Note: creates a QOM object, must run only after global and
4229 * compat properties have been set up.
4231 migration_object_init();
4233 if (qtest_chrdev) {
4234 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4237 machine_opts = qemu_get_machine_opts();
4238 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4239 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4240 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4241 bios_name = qemu_opt_get(machine_opts, "firmware");
4243 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4244 if (opts) {
4245 boot_order = qemu_opt_get(opts, "order");
4246 if (boot_order) {
4247 validate_bootdevices(boot_order, &error_fatal);
4250 boot_once = qemu_opt_get(opts, "once");
4251 if (boot_once) {
4252 validate_bootdevices(boot_once, &error_fatal);
4255 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4256 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4259 if (!boot_order) {
4260 boot_order = machine_class->default_boot_order;
4263 if (!kernel_cmdline) {
4264 kernel_cmdline = "";
4265 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4268 linux_boot = (kernel_filename != NULL);
4270 if (!linux_boot && *kernel_cmdline != '\0') {
4271 error_report("-append only allowed with -kernel option");
4272 exit(1);
4275 if (!linux_boot && initrd_filename != NULL) {
4276 error_report("-initrd only allowed with -kernel option");
4277 exit(1);
4280 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4281 /* fall back to the -kernel/-append */
4282 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4285 os_set_line_buffering();
4287 /* spice needs the timers to be initialized by this point */
4288 qemu_spice_init();
4290 cpu_ticks_init();
4291 if (icount_opts) {
4292 if (!tcg_enabled()) {
4293 error_report("-icount is not allowed with hardware virtualization");
4294 exit(1);
4296 configure_icount(icount_opts, &error_abort);
4297 qemu_opts_del(icount_opts);
4300 if (tcg_enabled()) {
4301 qemu_tcg_configure(accel_opts, &error_fatal);
4304 if (default_net) {
4305 QemuOptsList *net = qemu_find_opts("net");
4306 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4307 #ifdef CONFIG_SLIRP
4308 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4309 #endif
4312 if (net_init_clients(&err) < 0) {
4313 error_report_err(err);
4314 exit(1);
4317 qemu_opts_foreach(qemu_find_opts("object"),
4318 user_creatable_add_opts_foreach,
4319 object_create_delayed, &error_fatal);
4321 tpm_init();
4323 /* init the bluetooth world */
4324 if (foreach_device_config(DEV_BT, bt_parse))
4325 exit(1);
4327 if (!xen_enabled()) {
4328 /* On 32-bit hosts, QEMU is limited by virtual address space */
4329 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4330 error_report("at most 2047 MB RAM can be simulated");
4331 exit(1);
4335 blk_mig_init();
4336 ram_mig_init();
4337 dirty_bitmap_mig_init();
4339 qemu_opts_foreach(qemu_find_opts("mon"),
4340 mon_init_func, NULL, &error_fatal);
4342 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4343 exit(1);
4344 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4345 exit(1);
4346 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4347 exit(1);
4349 /* If no default VGA is requested, the default is "none". */
4350 if (default_vga) {
4351 vga_model = get_default_vga_model(machine_class);
4353 if (vga_model) {
4354 select_vgahw(machine_class, vga_model);
4357 if (watchdog) {
4358 i = select_watchdog(watchdog);
4359 if (i > 0)
4360 exit (i == 1 ? 1 : 0);
4363 /* This checkpoint is required by replay to separate prior clock
4364 reading from the other reads, because timer polling functions query
4365 clock values from the log. */
4366 replay_checkpoint(CHECKPOINT_INIT);
4367 qdev_machine_init();
4369 current_machine->boot_order = boot_order;
4371 /* parse features once if machine provides default cpu_type */
4372 current_machine->cpu_type = machine_class->default_cpu_type;
4373 if (cpu_option) {
4374 current_machine->cpu_type = parse_cpu_option(cpu_option);
4376 parse_numa_opts(current_machine);
4378 /* do monitor/qmp handling at preconfig state if requested */
4379 main_loop();
4381 audio_init_audiodevs();
4383 /* from here on runstate is RUN_STATE_PRELAUNCH */
4384 machine_run_board_init(current_machine);
4386 realtime_init();
4388 soundhw_init();
4390 if (hax_enabled()) {
4391 hax_sync_vcpus();
4394 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4395 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4397 /* init USB devices */
4398 if (machine_usb(current_machine)) {
4399 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4400 exit(1);
4403 /* Check if IGD GFX passthrough. */
4404 igd_gfx_passthru();
4406 /* init generic devices */
4407 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4408 qemu_opts_foreach(qemu_find_opts("device"),
4409 device_init_func, NULL, &error_fatal);
4411 cpu_synchronize_all_post_init();
4413 rom_reset_order_override();
4415 /* Did we create any drives that we failed to create a device for? */
4416 drive_check_orphaned();
4418 /* Don't warn about the default network setup that you get if
4419 * no command line -net or -netdev options are specified. There
4420 * are two cases that we would otherwise complain about:
4421 * (1) board doesn't support a NIC but the implicit "-net nic"
4422 * requested one
4423 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4424 * sets up a nic that isn't connected to anything.
4426 if (!default_net && (!qtest_enabled() || has_defaults)) {
4427 net_check_clients();
4430 if (boot_once) {
4431 qemu_boot_set(boot_once, &error_fatal);
4432 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4435 /* init local displays */
4436 ds = init_displaystate();
4437 qemu_display_init(ds, &dpy);
4439 /* must be after terminal init, SDL library changes signal handlers */
4440 os_setup_signal_handling();
4442 /* init remote displays */
4443 #ifdef CONFIG_VNC
4444 qemu_opts_foreach(qemu_find_opts("vnc"),
4445 vnc_init_func, NULL, &error_fatal);
4446 #endif
4448 if (using_spice) {
4449 qemu_spice_display_init();
4452 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4453 exit(1);
4456 qdev_machine_creation_done();
4458 /* TODO: once all bus devices are qdevified, this should be done
4459 * when bus is created by qdev.c */
4460 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4461 qemu_run_machine_init_done_notifiers();
4463 if (rom_check_and_register_reset() != 0) {
4464 error_report("rom check and register reset failed");
4465 exit(1);
4468 replay_start();
4470 /* This checkpoint is required by replay to separate prior clock
4471 reading from the other reads, because timer polling functions query
4472 clock values from the log. */
4473 replay_checkpoint(CHECKPOINT_RESET);
4474 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4475 register_global_state();
4476 if (loadvm) {
4477 Error *local_err = NULL;
4478 if (load_snapshot(loadvm, &local_err) < 0) {
4479 error_report_err(local_err);
4480 autostart = 0;
4481 exit(1);
4484 if (replay_mode != REPLAY_MODE_NONE) {
4485 replay_vmstate_init();
4488 qdev_prop_check_globals();
4489 if (vmstate_dump_file) {
4490 /* dump and exit */
4491 dump_vmstate_json_to_file(vmstate_dump_file);
4492 return 0;
4495 if (incoming) {
4496 Error *local_err = NULL;
4497 qemu_start_incoming_migration(incoming, &local_err);
4498 if (local_err) {
4499 error_reportf_err(local_err, "-incoming %s: ", incoming);
4500 exit(1);
4502 } else if (autostart) {
4503 vm_start();
4506 accel_setup_post(current_machine);
4507 os_setup_post();
4509 main_loop();
4511 gdbserver_cleanup();
4514 * cleaning up the migration object cancels any existing migration
4515 * try to do this early so that it also stops using devices.
4517 migration_shutdown();
4519 /* No more vcpu or device emulation activity beyond this point */
4520 vm_shutdown();
4522 job_cancel_sync_all();
4523 bdrv_close_all();
4525 res_free();
4527 /* vhost-user must be cleaned up before chardevs. */
4528 tpm_cleanup();
4529 net_cleanup();
4530 audio_cleanup();
4531 monitor_cleanup();
4532 qemu_chr_cleanup();
4533 user_creatable_cleanup();
4534 /* TODO: unref root container, check all devices are ok */
4536 return 0;