Merge tag 'v4.2.0-rc0'
[qemu/ar7.git] / vl.c
blob9b9b6b8a957447ec7cc2007cdce0aa6e42d44c44
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "qemu/units.h"
28 #include "hw/qdev-properties.h"
29 #include "qapi/error.h"
30 #include "qemu-version.h"
31 #include "qemu/cutils.h"
32 #include "qemu/help_option.h"
33 #include "qemu/uuid.h"
34 #include "sysemu/reset.h"
35 #include "sysemu/runstate.h"
36 #include "sysemu/seccomp.h"
37 #include "sysemu/tcg.h"
39 #ifdef CONFIG_SDL
40 #if defined(__APPLE__) || defined(main)
41 #define SDL_MAIN_HANDLED
42 #include <SDL.h>
43 #if SDL_MAJOR_VERSION == 1
44 int qemu_main(int argc, char **argv);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv);
49 #undef main
50 #define main qemu_main
51 #elif SDL_MAJOR_VERSION == 2
52 #undef main
53 #else
54 #error undefined SDL version
55 #endif
56 #endif
57 #endif /* CONFIG_SDL */
59 #ifdef CONFIG_COCOA
60 #undef main
61 #define main qemu_main
62 #endif /* CONFIG_COCOA */
65 #include "qemu/error-report.h"
66 #include "qemu/sockets.h"
67 #include "sysemu/accel.h"
68 #include "hw/usb.h"
69 #include "hw/isa/isa.h"
70 #include "hw/scsi/scsi.h"
71 #include "hw/display/vga.h"
72 #include "hw/bt.h"
73 #include "sysemu/watchdog.h"
74 #include "hw/firmware/smbios.h"
75 #include "hw/acpi/acpi.h"
76 #include "hw/xen/xen.h"
77 #include "hw/loader.h"
78 #include "monitor/qdev.h"
79 #include "sysemu/bt.h"
80 #include "net/net.h"
81 #include "net/slirp.h"
82 #include "monitor/monitor.h"
83 #include "ui/console.h"
84 #include "ui/input.h"
85 #include "sysemu/sysemu.h"
86 #include "sysemu/numa.h"
87 #include "exec/gdbstub.h"
88 #include "qemu/timer.h"
89 #include "chardev/char.h"
90 #include "qemu/bitmap.h"
91 #include "qemu/log.h"
92 #include "sysemu/blockdev.h"
93 #include "hw/block/block.h"
94 #include "migration/misc.h"
95 #include "migration/snapshot.h"
96 #include "migration/global_state.h"
97 #include "sysemu/tpm.h"
98 #include "sysemu/dma.h"
99 #include "hw/audio/soundhw.h"
100 #include "audio/audio.h"
101 #include "sysemu/cpus.h"
102 #include "migration/colo.h"
103 #include "migration/postcopy-ram.h"
104 #include "sysemu/kvm.h"
105 #include "sysemu/hax.h"
106 #include "qapi/qobject-input-visitor.h"
107 #include "qemu/option.h"
108 #include "qemu/config-file.h"
109 #include "qemu-options.h"
110 #include "qemu/main-loop.h"
111 #ifdef CONFIG_VIRTFS
112 #include "fsdev/qemu-fsdev.h"
113 #endif
114 #include "sysemu/qtest.h"
116 #include "disas/disas.h"
118 #include "trace-root.h"
119 #include "trace/control.h"
120 #include "qemu/plugin.h"
121 #include "qemu/queue.h"
122 #include "sysemu/arch_init.h"
124 #include "ui/qemu-spice.h"
125 #include "qapi/string-input-visitor.h"
126 #include "qapi/opts-visitor.h"
127 #include "qapi/clone-visitor.h"
128 #include "qom/object_interfaces.h"
129 #include "hw/semihosting/semihost.h"
130 #include "crypto/init.h"
131 #include "sysemu/replay.h"
132 #include "qapi/qapi-events-run-state.h"
133 #include "qapi/qapi-visit-block-core.h"
134 #include "qapi/qapi-visit-ui.h"
135 #include "qapi/qapi-commands-block-core.h"
136 #include "qapi/qapi-commands-run-state.h"
137 #include "qapi/qapi-commands-ui.h"
138 #include "qapi/qmp/qerror.h"
139 #include "sysemu/iothread.h"
140 #include "qemu/guest-random.h"
142 #define MAX_VIRTIO_CONSOLES 1
144 static const char *data_dir[16];
145 static int data_dir_idx;
146 const char *bios_name = NULL;
147 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
148 int display_opengl;
149 const char* keyboard_layout = NULL;
150 ram_addr_t ram_size;
151 const char *mem_path = NULL;
152 int mem_prealloc = 0; /* force preallocation of physical target memory */
153 bool enable_mlock = false;
154 bool enable_cpu_pm = false;
155 int nb_nics;
156 NICInfo nd_table[MAX_NICS];
157 int autostart;
158 static enum {
159 RTC_BASE_UTC,
160 RTC_BASE_LOCALTIME,
161 RTC_BASE_DATETIME,
162 } rtc_base_type = RTC_BASE_UTC;
163 static time_t rtc_ref_start_datetime;
164 static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
165 static int rtc_host_datetime_offset = -1; /* valid & used only with
166 RTC_BASE_DATETIME */
167 QEMUClockType rtc_clock;
168 int vga_interface_type = VGA_NONE;
169 static DisplayOptions dpy;
170 static int num_serial_hds;
171 static Chardev **serial_hds;
172 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
173 int win2k_install_hack = 0;
174 int singlestep = 0;
175 int acpi_enabled = 1;
176 int no_hpet = 0;
177 int fd_bootchk = 1;
178 static int no_reboot;
179 int no_shutdown = 0;
180 int cursor_hide = 1;
181 int graphic_rotate = 0;
182 const char *watchdog;
183 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
184 int nb_option_roms;
185 int old_param = 0;
186 const char *qemu_name;
187 int alt_grab = 0;
188 int ctrl_grab = 0;
189 unsigned int nb_prom_envs = 0;
190 const char *prom_envs[MAX_PROM_ENVS];
191 int boot_menu;
192 bool boot_strict;
193 uint8_t *boot_splash_filedata;
194 int only_migratable; /* turn it off unless user states otherwise */
195 bool wakeup_suspend_enabled;
197 int icount_align_option;
199 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
200 * little-endian "wire format" described in the SMBIOS 2.6 specification.
202 QemuUUID qemu_uuid;
203 bool qemu_uuid_set;
205 /* Trace unassigned memory or i/o accesses. */
206 bool trace_unassigned;
208 static NotifierList exit_notifiers =
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
211 static NotifierList machine_init_done_notifiers =
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
214 bool xen_allowed;
215 uint32_t xen_domid;
216 enum xen_mode xen_mode = XEN_EMULATE;
217 bool xen_domid_restrict;
219 static int has_defaults = 1;
220 static int default_serial = 1;
221 static int default_parallel = 1;
222 static int default_monitor = 1;
223 static int default_floppy = 1;
224 static int default_cdrom = 1;
225 static int default_sdcard = 1;
226 static int default_vga = 1;
227 static int default_net = 1;
229 static struct {
230 const char *driver;
231 int *flag;
232 } default_list[] = {
233 { .driver = "isa-serial", .flag = &default_serial },
234 { .driver = "isa-parallel", .flag = &default_parallel },
235 { .driver = "isa-fdc", .flag = &default_floppy },
236 { .driver = "floppy", .flag = &default_floppy },
237 { .driver = "ide-cd", .flag = &default_cdrom },
238 { .driver = "ide-hd", .flag = &default_cdrom },
239 { .driver = "ide-drive", .flag = &default_cdrom },
240 { .driver = "scsi-cd", .flag = &default_cdrom },
241 { .driver = "scsi-hd", .flag = &default_cdrom },
242 { .driver = "VGA", .flag = &default_vga },
243 { .driver = "isa-vga", .flag = &default_vga },
244 { .driver = "cirrus-vga", .flag = &default_vga },
245 { .driver = "isa-cirrus-vga", .flag = &default_vga },
246 { .driver = "vmware-svga", .flag = &default_vga },
247 { .driver = "qxl-vga", .flag = &default_vga },
248 { .driver = "virtio-vga", .flag = &default_vga },
249 { .driver = "ati-vga", .flag = &default_vga },
250 { .driver = "vhost-user-vga", .flag = &default_vga },
253 static QemuOptsList qemu_rtc_opts = {
254 .name = "rtc",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
256 .merge_lists = true,
257 .desc = {
259 .name = "base",
260 .type = QEMU_OPT_STRING,
262 .name = "clock",
263 .type = QEMU_OPT_STRING,
265 .name = "driftfix",
266 .type = QEMU_OPT_STRING,
268 { /* end of list */ }
272 static QemuOptsList qemu_option_rom_opts = {
273 .name = "option-rom",
274 .implied_opt_name = "romfile",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
276 .desc = {
278 .name = "bootindex",
279 .type = QEMU_OPT_NUMBER,
280 }, {
281 .name = "romfile",
282 .type = QEMU_OPT_STRING,
284 { /* end of list */ }
288 static QemuOptsList qemu_machine_opts = {
289 .name = "machine",
290 .implied_opt_name = "type",
291 .merge_lists = true,
292 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
293 .desc = {
295 * no elements => accept any
296 * sanity checking will happen later
297 * when setting machine properties
303 static QemuOptsList qemu_accel_opts = {
304 .name = "accel",
305 .implied_opt_name = "accel",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
307 .merge_lists = true,
308 .desc = {
310 .name = "accel",
311 .type = QEMU_OPT_STRING,
312 .help = "Select the type of accelerator",
315 .name = "thread",
316 .type = QEMU_OPT_STRING,
317 .help = "Enable/disable multi-threaded TCG",
319 { /* end of list */ }
323 static QemuOptsList qemu_boot_opts = {
324 .name = "boot-opts",
325 .implied_opt_name = "order",
326 .merge_lists = true,
327 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
328 .desc = {
330 .name = "order",
331 .type = QEMU_OPT_STRING,
332 }, {
333 .name = "once",
334 .type = QEMU_OPT_STRING,
335 }, {
336 .name = "menu",
337 .type = QEMU_OPT_BOOL,
338 }, {
339 .name = "splash",
340 .type = QEMU_OPT_STRING,
341 }, {
342 .name = "splash-time",
343 .type = QEMU_OPT_NUMBER,
344 }, {
345 .name = "reboot-timeout",
346 .type = QEMU_OPT_NUMBER,
347 }, {
348 .name = "strict",
349 .type = QEMU_OPT_BOOL,
351 { /*End of list */ }
355 static QemuOptsList qemu_add_fd_opts = {
356 .name = "add-fd",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
358 .desc = {
360 .name = "fd",
361 .type = QEMU_OPT_NUMBER,
362 .help = "file descriptor of which a duplicate is added to fd set",
364 .name = "set",
365 .type = QEMU_OPT_NUMBER,
366 .help = "ID of the fd set to add fd to",
368 .name = "opaque",
369 .type = QEMU_OPT_STRING,
370 .help = "free-form string used to describe fd",
372 { /* end of list */ }
376 static QemuOptsList qemu_object_opts = {
377 .name = "object",
378 .implied_opt_name = "qom-type",
379 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
380 .desc = {
385 static QemuOptsList qemu_tpmdev_opts = {
386 .name = "tpmdev",
387 .implied_opt_name = "type",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
389 .desc = {
390 /* options are defined in the TPM backends */
391 { /* end of list */ }
395 static QemuOptsList qemu_realtime_opts = {
396 .name = "realtime",
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
398 .desc = {
400 .name = "mlock",
401 .type = QEMU_OPT_BOOL,
403 { /* end of list */ }
407 static QemuOptsList qemu_overcommit_opts = {
408 .name = "overcommit",
409 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
410 .desc = {
412 .name = "mem-lock",
413 .type = QEMU_OPT_BOOL,
416 .name = "cpu-pm",
417 .type = QEMU_OPT_BOOL,
419 { /* end of list */ }
423 static QemuOptsList qemu_msg_opts = {
424 .name = "msg",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
426 .desc = {
428 .name = "timestamp",
429 .type = QEMU_OPT_BOOL,
431 { /* end of list */ }
435 static QemuOptsList qemu_name_opts = {
436 .name = "name",
437 .implied_opt_name = "guest",
438 .merge_lists = true,
439 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
440 .desc = {
442 .name = "guest",
443 .type = QEMU_OPT_STRING,
444 .help = "Sets the name of the guest.\n"
445 "This name will be displayed in the SDL window caption.\n"
446 "The name will also be used for the VNC server",
447 }, {
448 .name = "process",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the name of the QEMU process, as shown in top etc",
451 }, {
452 .name = "debug-threads",
453 .type = QEMU_OPT_BOOL,
454 .help = "When enabled, name the individual threads; defaults off.\n"
455 "NOTE: The thread names are for debugging and not a\n"
456 "stable API.",
458 { /* End of list */ }
462 static QemuOptsList qemu_mem_opts = {
463 .name = "memory",
464 .implied_opt_name = "size",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
466 .merge_lists = true,
467 .desc = {
469 .name = "size",
470 .type = QEMU_OPT_SIZE,
473 .name = "slots",
474 .type = QEMU_OPT_NUMBER,
477 .name = "maxmem",
478 .type = QEMU_OPT_SIZE,
480 { /* end of list */ }
484 static QemuOptsList qemu_icount_opts = {
485 .name = "icount",
486 .implied_opt_name = "shift",
487 .merge_lists = true,
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
489 .desc = {
491 .name = "shift",
492 .type = QEMU_OPT_STRING,
493 }, {
494 .name = "align",
495 .type = QEMU_OPT_BOOL,
496 }, {
497 .name = "sleep",
498 .type = QEMU_OPT_BOOL,
499 }, {
500 .name = "rr",
501 .type = QEMU_OPT_STRING,
502 }, {
503 .name = "rrfile",
504 .type = QEMU_OPT_STRING,
505 }, {
506 .name = "rrsnapshot",
507 .type = QEMU_OPT_STRING,
509 { /* end of list */ }
513 static QemuOptsList qemu_fw_cfg_opts = {
514 .name = "fw_cfg",
515 .implied_opt_name = "name",
516 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
517 .desc = {
519 .name = "name",
520 .type = QEMU_OPT_STRING,
521 .help = "Sets the fw_cfg name of the blob to be inserted",
522 }, {
523 .name = "file",
524 .type = QEMU_OPT_STRING,
525 .help = "Sets the name of the file from which "
526 "the fw_cfg blob will be loaded",
527 }, {
528 .name = "string",
529 .type = QEMU_OPT_STRING,
530 .help = "Sets content of the blob to be inserted from a string",
532 { /* end of list */ }
537 * Get machine options
539 * Returns: machine options (never null).
541 QemuOpts *qemu_get_machine_opts(void)
543 return qemu_find_opts_singleton("machine");
546 const char *qemu_get_vm_name(void)
548 return qemu_name;
551 static void res_free(void)
553 g_free(boot_splash_filedata);
554 boot_splash_filedata = NULL;
557 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
559 const char *driver = qemu_opt_get(opts, "driver");
560 int i;
562 if (!driver)
563 return 0;
564 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
565 if (strcmp(default_list[i].driver, driver) != 0)
566 continue;
567 *(default_list[i].flag) = 0;
569 return 0;
572 /***********************************************************/
573 /* QEMU state */
575 static RunState current_run_state = RUN_STATE_PRECONFIG;
577 /* We use RUN_STATE__MAX but any invalid value will do */
578 static RunState vmstop_requested = RUN_STATE__MAX;
579 static QemuMutex vmstop_lock;
581 typedef struct {
582 RunState from;
583 RunState to;
584 } RunStateTransition;
586 static const RunStateTransition runstate_transitions_def[] = {
587 /* from -> to */
588 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
589 /* Early switch to inmigrate state to allow -incoming CLI option work
590 * as it used to. TODO: delay actual switching to inmigrate state to
591 * the point after machine is built and remove this hack.
593 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
595 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
596 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
597 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
599 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
600 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
601 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
602 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
603 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
604 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
605 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
606 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
607 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
609 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
610 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
612 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
613 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
616 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
617 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
618 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
620 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
621 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
623 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_PAUSED, RUN_STATE_COLO},
626 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
634 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
635 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
640 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
643 { RUN_STATE_COLO, RUN_STATE_RUNNING },
645 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
646 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
648 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
649 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
652 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
653 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
654 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
655 { RUN_STATE_RUNNING, RUN_STATE_COLO},
657 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
659 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
660 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
661 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
664 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
665 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
666 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
670 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
671 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
672 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
679 { RUN_STATE__MAX, RUN_STATE__MAX },
682 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
684 bool runstate_check(RunState state)
686 return current_run_state == state;
689 bool runstate_store(char *str, size_t size)
691 const char *state = RunState_str(current_run_state);
692 size_t len = strlen(state) + 1;
694 if (len > size) {
695 return false;
697 memcpy(str, state, len);
698 return true;
701 static void runstate_init(void)
703 const RunStateTransition *p;
705 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
706 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
707 runstate_valid_transitions[p->from][p->to] = true;
710 qemu_mutex_init(&vmstop_lock);
713 /* This function will abort() on invalid state transitions */
714 void runstate_set(RunState new_state)
716 assert(new_state < RUN_STATE__MAX);
718 trace_runstate_set(current_run_state, RunState_str(current_run_state),
719 new_state, RunState_str(new_state));
721 if (current_run_state == new_state) {
722 return;
725 if (!runstate_valid_transitions[current_run_state][new_state]) {
726 error_report("invalid runstate transition: '%s' -> '%s'",
727 RunState_str(current_run_state),
728 RunState_str(new_state));
729 abort();
732 current_run_state = new_state;
735 int runstate_is_running(void)
737 return runstate_check(RUN_STATE_RUNNING);
740 bool runstate_needs_reset(void)
742 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
743 runstate_check(RUN_STATE_SHUTDOWN);
746 StatusInfo *qmp_query_status(Error **errp)
748 StatusInfo *info = g_malloc0(sizeof(*info));
750 info->running = runstate_is_running();
751 info->singlestep = singlestep;
752 info->status = current_run_state;
754 return info;
757 bool qemu_vmstop_requested(RunState *r)
759 qemu_mutex_lock(&vmstop_lock);
760 *r = vmstop_requested;
761 vmstop_requested = RUN_STATE__MAX;
762 qemu_mutex_unlock(&vmstop_lock);
763 return *r < RUN_STATE__MAX;
766 void qemu_system_vmstop_request_prepare(void)
768 qemu_mutex_lock(&vmstop_lock);
771 void qemu_system_vmstop_request(RunState state)
773 vmstop_requested = state;
774 qemu_mutex_unlock(&vmstop_lock);
775 qemu_notify_event();
778 /***********************************************************/
779 /* RTC reference time/date access */
780 static time_t qemu_ref_timedate(QEMUClockType clock)
782 time_t value = qemu_clock_get_ms(clock) / 1000;
783 switch (clock) {
784 case QEMU_CLOCK_REALTIME:
785 value -= rtc_realtime_clock_offset;
786 /* fall through */
787 case QEMU_CLOCK_VIRTUAL:
788 value += rtc_ref_start_datetime;
789 break;
790 case QEMU_CLOCK_HOST:
791 if (rtc_base_type == RTC_BASE_DATETIME) {
792 value -= rtc_host_datetime_offset;
794 break;
795 default:
796 assert(0);
798 return value;
801 void qemu_get_timedate(struct tm *tm, int offset)
803 time_t ti = qemu_ref_timedate(rtc_clock);
805 ti += offset;
807 switch (rtc_base_type) {
808 case RTC_BASE_DATETIME:
809 case RTC_BASE_UTC:
810 gmtime_r(&ti, tm);
811 break;
812 case RTC_BASE_LOCALTIME:
813 localtime_r(&ti, tm);
814 break;
818 int qemu_timedate_diff(struct tm *tm)
820 time_t seconds;
822 switch (rtc_base_type) {
823 case RTC_BASE_DATETIME:
824 case RTC_BASE_UTC:
825 seconds = mktimegm(tm);
826 break;
827 case RTC_BASE_LOCALTIME:
829 struct tm tmp = *tm;
830 tmp.tm_isdst = -1; /* use timezone to figure it out */
831 seconds = mktime(&tmp);
832 break;
834 default:
835 abort();
838 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
841 static void configure_rtc_base_datetime(const char *startdate)
843 time_t rtc_start_datetime;
844 struct tm tm;
846 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
847 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
848 /* OK */
849 } else if (sscanf(startdate, "%d-%d-%d",
850 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
851 tm.tm_hour = 0;
852 tm.tm_min = 0;
853 tm.tm_sec = 0;
854 } else {
855 goto date_fail;
857 tm.tm_year -= 1900;
858 tm.tm_mon--;
859 rtc_start_datetime = mktimegm(&tm);
860 if (rtc_start_datetime == -1) {
861 date_fail:
862 error_report("invalid datetime format");
863 error_printf("valid formats: "
864 "'2006-06-17T16:01:21' or '2006-06-17'\n");
865 exit(1);
867 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
868 rtc_ref_start_datetime = rtc_start_datetime;
871 static void configure_rtc(QemuOpts *opts)
873 const char *value;
875 /* Set defaults */
876 rtc_clock = QEMU_CLOCK_HOST;
877 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
878 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
880 value = qemu_opt_get(opts, "base");
881 if (value) {
882 if (!strcmp(value, "utc")) {
883 rtc_base_type = RTC_BASE_UTC;
884 } else if (!strcmp(value, "localtime")) {
885 Error *blocker = NULL;
886 rtc_base_type = RTC_BASE_LOCALTIME;
887 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
888 "-rtc base=localtime");
889 replay_add_blocker(blocker);
890 } else {
891 rtc_base_type = RTC_BASE_DATETIME;
892 configure_rtc_base_datetime(value);
895 value = qemu_opt_get(opts, "clock");
896 if (value) {
897 if (!strcmp(value, "host")) {
898 rtc_clock = QEMU_CLOCK_HOST;
899 } else if (!strcmp(value, "rt")) {
900 rtc_clock = QEMU_CLOCK_REALTIME;
901 } else if (!strcmp(value, "vm")) {
902 rtc_clock = QEMU_CLOCK_VIRTUAL;
903 } else {
904 error_report("invalid option value '%s'", value);
905 exit(1);
908 value = qemu_opt_get(opts, "driftfix");
909 if (value) {
910 if (!strcmp(value, "slew")) {
911 static GlobalProperty slew_lost_ticks = {
912 .driver = "mc146818rtc",
913 .property = "lost_tick_policy",
914 .value = "slew",
917 qdev_prop_register_global(&slew_lost_ticks);
918 } else if (!strcmp(value, "none")) {
919 /* discard is default */
920 } else {
921 error_report("invalid option value '%s'", value);
922 exit(1);
927 /***********************************************************/
928 /* Bluetooth support */
929 static int nb_hcis;
930 static int cur_hci;
931 static struct HCIInfo *hci_table[MAX_NICS];
933 struct HCIInfo *qemu_next_hci(void)
935 if (cur_hci == nb_hcis)
936 return &null_hci;
938 return hci_table[cur_hci++];
941 static int bt_hci_parse(const char *str)
943 struct HCIInfo *hci;
944 bdaddr_t bdaddr;
946 if (nb_hcis >= MAX_NICS) {
947 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
948 return -1;
951 hci = hci_init(str);
952 if (!hci)
953 return -1;
955 bdaddr.b[0] = 0x52;
956 bdaddr.b[1] = 0x54;
957 bdaddr.b[2] = 0x00;
958 bdaddr.b[3] = 0x12;
959 bdaddr.b[4] = 0x34;
960 bdaddr.b[5] = 0x56 + nb_hcis;
961 hci->bdaddr_set(hci, bdaddr.b);
963 hci_table[nb_hcis++] = hci;
965 return 0;
968 static void bt_vhci_add(int vlan_id)
970 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
972 if (!vlan->slave)
973 warn_report("adding a VHCI to an empty scatternet %i",
974 vlan_id);
976 bt_vhci_init(bt_new_hci(vlan));
979 static struct bt_device_s *bt_device_add(const char *opt)
981 struct bt_scatternet_s *vlan;
982 int vlan_id = 0;
983 char *endp = strstr(opt, ",vlan=");
984 int len = (endp ? endp - opt : strlen(opt)) + 1;
985 char devname[10];
987 pstrcpy(devname, MIN(sizeof(devname), len), opt);
989 if (endp) {
990 vlan_id = strtol(endp + 6, &endp, 0);
991 if (*endp) {
992 error_report("unrecognised bluetooth vlan Id");
993 return 0;
997 vlan = qemu_find_bt_vlan(vlan_id);
999 if (!vlan->slave)
1000 warn_report("adding a slave device to an empty scatternet %i",
1001 vlan_id);
1003 if (!strcmp(devname, "keyboard"))
1004 return bt_keyboard_init(vlan);
1006 error_report("unsupported bluetooth device '%s'", devname);
1007 return 0;
1010 static int bt_parse(const char *opt)
1012 const char *endp, *p;
1013 int vlan;
1015 if (strstart(opt, "hci", &endp)) {
1016 if (!*endp || *endp == ',') {
1017 if (*endp)
1018 if (!strstart(endp, ",vlan=", 0))
1019 opt = endp + 1;
1021 return bt_hci_parse(opt);
1023 } else if (strstart(opt, "vhci", &endp)) {
1024 if (!*endp || *endp == ',') {
1025 if (*endp) {
1026 if (strstart(endp, ",vlan=", &p)) {
1027 vlan = strtol(p, (char **) &endp, 0);
1028 if (*endp) {
1029 error_report("bad scatternet '%s'", p);
1030 return 1;
1032 } else {
1033 error_report("bad parameter '%s'", endp + 1);
1034 return 1;
1036 } else
1037 vlan = 0;
1039 bt_vhci_add(vlan);
1040 return 0;
1042 } else if (strstart(opt, "device:", &endp))
1043 return !bt_device_add(endp);
1045 error_report("bad bluetooth parameter '%s'", opt);
1046 return 1;
1049 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1051 const char *proc_name;
1053 if (qemu_opt_get(opts, "debug-threads")) {
1054 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1056 qemu_name = qemu_opt_get(opts, "guest");
1058 proc_name = qemu_opt_get(opts, "process");
1059 if (proc_name) {
1060 os_set_proc_name(proc_name);
1063 return 0;
1066 bool defaults_enabled(void)
1068 return has_defaults;
1071 #ifndef _WIN32
1072 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1074 int fd, dupfd, flags;
1075 int64_t fdset_id;
1076 const char *fd_opaque = NULL;
1077 AddfdInfo *fdinfo;
1079 fd = qemu_opt_get_number(opts, "fd", -1);
1080 fdset_id = qemu_opt_get_number(opts, "set", -1);
1081 fd_opaque = qemu_opt_get(opts, "opaque");
1083 if (fd < 0) {
1084 error_setg(errp, "fd option is required and must be non-negative");
1085 return -1;
1088 if (fd <= STDERR_FILENO) {
1089 error_setg(errp, "fd cannot be a standard I/O stream");
1090 return -1;
1094 * All fds inherited across exec() necessarily have FD_CLOEXEC
1095 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1097 flags = fcntl(fd, F_GETFD);
1098 if (flags == -1 || (flags & FD_CLOEXEC)) {
1099 error_setg(errp, "fd is not valid or already in use");
1100 return -1;
1103 if (fdset_id < 0) {
1104 error_setg(errp, "set option is required and must be non-negative");
1105 return -1;
1108 #ifdef F_DUPFD_CLOEXEC
1109 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1110 #else
1111 dupfd = dup(fd);
1112 if (dupfd != -1) {
1113 qemu_set_cloexec(dupfd);
1115 #endif
1116 if (dupfd == -1) {
1117 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1118 return -1;
1121 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1122 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1123 &error_abort);
1124 g_free(fdinfo);
1126 return 0;
1129 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1131 int fd;
1133 fd = qemu_opt_get_number(opts, "fd", -1);
1134 close(fd);
1136 return 0;
1138 #endif
1140 /***********************************************************/
1141 /* QEMU Block devices */
1143 #define HD_OPTS "media=disk"
1144 #define CDROM_OPTS "media=cdrom"
1145 #define FD_OPTS ""
1146 #define PFLASH_OPTS ""
1147 #define MTD_OPTS ""
1148 #define SD_OPTS ""
1150 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1152 BlockInterfaceType *block_default_type = opaque;
1154 return drive_new(opts, *block_default_type, errp) == NULL;
1157 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1159 if (qemu_opt_get(opts, "snapshot") == NULL) {
1160 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1162 return 0;
1165 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1166 int index, const char *optstr)
1168 QemuOpts *opts;
1169 DriveInfo *dinfo;
1171 if (!enable || drive_get_by_index(type, index)) {
1172 return;
1175 opts = drive_add(type, index, NULL, optstr);
1176 if (snapshot) {
1177 drive_enable_snapshot(NULL, opts, NULL);
1180 dinfo = drive_new(opts, type, &error_abort);
1181 dinfo->is_default = true;
1185 typedef struct BlockdevOptionsQueueEntry {
1186 BlockdevOptions *bdo;
1187 Location loc;
1188 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
1189 } BlockdevOptionsQueueEntry;
1191 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
1193 static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
1194 MachineClass *machine_class, int snapshot)
1197 * If the currently selected machine wishes to override the
1198 * units-per-bus property of its default HBA interface type, do so
1199 * now.
1201 if (machine_class->units_per_default_bus) {
1202 override_max_devs(machine_class->block_default_type,
1203 machine_class->units_per_default_bus);
1206 /* open the virtual block devices */
1207 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
1208 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
1210 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
1211 loc_push_restore(&bdo->loc);
1212 qmp_blockdev_add(bdo->bdo, &error_fatal);
1213 loc_pop(&bdo->loc);
1214 qapi_free_BlockdevOptions(bdo->bdo);
1215 g_free(bdo);
1217 if (snapshot) {
1218 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
1219 NULL, NULL);
1221 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
1222 &machine_class->block_default_type, &error_fatal)) {
1223 /* We printed help */
1224 exit(0);
1227 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
1228 CDROM_OPTS);
1229 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
1230 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
1234 static QemuOptsList qemu_smp_opts = {
1235 .name = "smp-opts",
1236 .implied_opt_name = "cpus",
1237 .merge_lists = true,
1238 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1239 .desc = {
1241 .name = "cpus",
1242 .type = QEMU_OPT_NUMBER,
1243 }, {
1244 .name = "sockets",
1245 .type = QEMU_OPT_NUMBER,
1246 }, {
1247 .name = "dies",
1248 .type = QEMU_OPT_NUMBER,
1249 }, {
1250 .name = "cores",
1251 .type = QEMU_OPT_NUMBER,
1252 }, {
1253 .name = "threads",
1254 .type = QEMU_OPT_NUMBER,
1255 }, {
1256 .name = "maxcpus",
1257 .type = QEMU_OPT_NUMBER,
1259 { /*End of list */ }
1263 static void realtime_init(void)
1265 if (enable_mlock) {
1266 if (os_mlock() < 0) {
1267 error_report("locking memory failed");
1268 exit(1);
1274 static void configure_msg(QemuOpts *opts)
1276 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1280 /* Now we still need this for compatibility with XEN. */
1281 bool has_igd_gfx_passthru;
1282 static void igd_gfx_passthru(void)
1284 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1287 /***********************************************************/
1288 /* USB devices */
1290 static int usb_device_add(const char *devname)
1292 USBDevice *dev = NULL;
1294 if (!machine_usb(current_machine)) {
1295 return -1;
1298 dev = usbdevice_create(devname);
1299 if (!dev)
1300 return -1;
1302 return 0;
1305 static int usb_parse(const char *cmdline)
1307 int r;
1308 r = usb_device_add(cmdline);
1309 if (r < 0) {
1310 error_report("could not add USB device '%s'", cmdline);
1312 return r;
1315 /***********************************************************/
1316 /* machine registration */
1318 MachineState *current_machine;
1320 static MachineClass *find_machine(const char *name, GSList *machines)
1322 GSList *el;
1324 for (el = machines; el; el = el->next) {
1325 MachineClass *mc = el->data;
1327 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
1328 return mc;
1332 return NULL;
1335 static MachineClass *find_default_machine(GSList *machines)
1337 GSList *el;
1339 for (el = machines; el; el = el->next) {
1340 MachineClass *mc = el->data;
1342 if (mc->is_default) {
1343 return mc;
1347 return NULL;
1350 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1352 ObjectProperty *prop;
1353 ObjectPropertyIterator iter;
1355 if (!qemu_opt_has_help_opt(opts)) {
1356 return 0;
1359 object_property_iter_init(&iter, OBJECT(machine));
1360 while ((prop = object_property_iter_next(&iter))) {
1361 if (!prop->set) {
1362 continue;
1365 printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1366 prop->name, prop->type);
1367 if (prop->description) {
1368 printf(" (%s)\n", prop->description);
1369 } else {
1370 printf("\n");
1374 return 1;
1377 struct VMChangeStateEntry {
1378 VMChangeStateHandler *cb;
1379 void *opaque;
1380 QTAILQ_ENTRY(VMChangeStateEntry) entries;
1381 int priority;
1384 static QTAILQ_HEAD(, VMChangeStateEntry) vm_change_state_head;
1387 * qemu_add_vm_change_state_handler_prio:
1388 * @cb: the callback to invoke
1389 * @opaque: user data passed to the callback
1390 * @priority: low priorities execute first when the vm runs and the reverse is
1391 * true when the vm stops
1393 * Register a callback function that is invoked when the vm starts or stops
1394 * running.
1396 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
1398 VMChangeStateEntry *qemu_add_vm_change_state_handler_prio(
1399 VMChangeStateHandler *cb, void *opaque, int priority)
1401 VMChangeStateEntry *e;
1402 VMChangeStateEntry *other;
1404 e = g_malloc0(sizeof(*e));
1405 e->cb = cb;
1406 e->opaque = opaque;
1407 e->priority = priority;
1409 /* Keep list sorted in ascending priority order */
1410 QTAILQ_FOREACH(other, &vm_change_state_head, entries) {
1411 if (priority < other->priority) {
1412 QTAILQ_INSERT_BEFORE(other, e, entries);
1413 return e;
1417 QTAILQ_INSERT_TAIL(&vm_change_state_head, e, entries);
1418 return e;
1421 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1422 void *opaque)
1424 return qemu_add_vm_change_state_handler_prio(cb, opaque, 0);
1427 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1429 QTAILQ_REMOVE(&vm_change_state_head, e, entries);
1430 g_free(e);
1433 void vm_state_notify(int running, RunState state)
1435 VMChangeStateEntry *e, *next;
1437 trace_vm_state_notify(running, state, RunState_str(state));
1439 if (running) {
1440 QTAILQ_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1441 e->cb(e->opaque, running, state);
1443 } else {
1444 QTAILQ_FOREACH_REVERSE_SAFE(e, &vm_change_state_head, entries, next) {
1445 e->cb(e->opaque, running, state);
1450 static ShutdownCause reset_requested;
1451 static ShutdownCause shutdown_requested;
1452 static int shutdown_signal;
1453 static pid_t shutdown_pid;
1454 static int powerdown_requested;
1455 static int debug_requested;
1456 static int suspend_requested;
1457 static bool preconfig_exit_requested = true;
1458 static WakeupReason wakeup_reason;
1459 static NotifierList powerdown_notifiers =
1460 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1461 static NotifierList suspend_notifiers =
1462 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1463 static NotifierList wakeup_notifiers =
1464 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1465 static NotifierList shutdown_notifiers =
1466 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1467 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1469 ShutdownCause qemu_shutdown_requested_get(void)
1471 return shutdown_requested;
1474 ShutdownCause qemu_reset_requested_get(void)
1476 return reset_requested;
1479 static int qemu_shutdown_requested(void)
1481 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1484 static void qemu_kill_report(void)
1486 if (!qtest_driver() && shutdown_signal) {
1487 if (shutdown_pid == 0) {
1488 /* This happens for eg ^C at the terminal, so it's worth
1489 * avoiding printing an odd message in that case.
1491 error_report("terminating on signal %d", shutdown_signal);
1492 } else {
1493 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1495 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1496 shutdown_signal, shutdown_pid,
1497 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1498 g_free(shutdown_cmd);
1500 shutdown_signal = 0;
1504 static ShutdownCause qemu_reset_requested(void)
1506 ShutdownCause r = reset_requested;
1508 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1509 reset_requested = SHUTDOWN_CAUSE_NONE;
1510 return r;
1512 return SHUTDOWN_CAUSE_NONE;
1515 static int qemu_suspend_requested(void)
1517 int r = suspend_requested;
1518 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1519 suspend_requested = 0;
1520 return r;
1522 return false;
1525 static WakeupReason qemu_wakeup_requested(void)
1527 return wakeup_reason;
1530 static int qemu_powerdown_requested(void)
1532 int r = powerdown_requested;
1533 powerdown_requested = 0;
1534 return r;
1537 static int qemu_debug_requested(void)
1539 int r = debug_requested;
1540 debug_requested = 0;
1541 return r;
1544 void qemu_exit_preconfig_request(void)
1546 preconfig_exit_requested = true;
1550 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1552 void qemu_system_reset(ShutdownCause reason)
1554 MachineClass *mc;
1556 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1558 cpu_synchronize_all_states();
1560 if (mc && mc->reset) {
1561 mc->reset(current_machine);
1562 } else {
1563 qemu_devices_reset();
1565 if (reason && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1566 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1568 cpu_synchronize_all_post_reset();
1572 * Wake the VM after suspend.
1574 static void qemu_system_wakeup(void)
1576 MachineClass *mc;
1578 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1580 if (mc && mc->wakeup) {
1581 mc->wakeup(current_machine);
1585 void qemu_system_guest_panicked(GuestPanicInformation *info)
1587 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1589 if (current_cpu) {
1590 current_cpu->crash_occurred = true;
1592 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1593 !!info, info);
1594 vm_stop(RUN_STATE_GUEST_PANICKED);
1595 if (!no_shutdown) {
1596 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1597 !!info, info);
1598 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1601 if (info) {
1602 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1603 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1604 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1605 info->u.hyper_v.arg1,
1606 info->u.hyper_v.arg2,
1607 info->u.hyper_v.arg3,
1608 info->u.hyper_v.arg4,
1609 info->u.hyper_v.arg5);
1610 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1611 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1612 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1613 info->u.s390.core,
1614 S390CrashReason_str(info->u.s390.reason),
1615 info->u.s390.psw_mask,
1616 info->u.s390.psw_addr);
1618 qapi_free_GuestPanicInformation(info);
1622 void qemu_system_reset_request(ShutdownCause reason)
1624 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1625 shutdown_requested = reason;
1626 } else {
1627 reset_requested = reason;
1629 cpu_stop_current();
1630 qemu_notify_event();
1633 static void qemu_system_suspend(void)
1635 pause_all_vcpus();
1636 notifier_list_notify(&suspend_notifiers, NULL);
1637 runstate_set(RUN_STATE_SUSPENDED);
1638 qapi_event_send_suspend();
1641 void qemu_system_suspend_request(void)
1643 if (runstate_check(RUN_STATE_SUSPENDED)) {
1644 return;
1646 suspend_requested = 1;
1647 cpu_stop_current();
1648 qemu_notify_event();
1651 void qemu_register_suspend_notifier(Notifier *notifier)
1653 notifier_list_add(&suspend_notifiers, notifier);
1656 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1658 trace_system_wakeup_request(reason);
1660 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1661 error_setg(errp,
1662 "Unable to wake up: guest is not in suspended state");
1663 return;
1665 if (!(wakeup_reason_mask & (1 << reason))) {
1666 return;
1668 runstate_set(RUN_STATE_RUNNING);
1669 wakeup_reason = reason;
1670 qemu_notify_event();
1673 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1675 if (enabled) {
1676 wakeup_reason_mask |= (1 << reason);
1677 } else {
1678 wakeup_reason_mask &= ~(1 << reason);
1682 void qemu_register_wakeup_notifier(Notifier *notifier)
1684 notifier_list_add(&wakeup_notifiers, notifier);
1687 void qemu_register_wakeup_support(void)
1689 wakeup_suspend_enabled = true;
1692 bool qemu_wakeup_suspend_enabled(void)
1694 return wakeup_suspend_enabled;
1697 void qemu_system_killed(int signal, pid_t pid)
1699 shutdown_signal = signal;
1700 shutdown_pid = pid;
1701 no_shutdown = 0;
1703 /* Cannot call qemu_system_shutdown_request directly because
1704 * we are in a signal handler.
1706 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1707 qemu_notify_event();
1710 void qemu_system_shutdown_request(ShutdownCause reason)
1712 trace_qemu_system_shutdown_request(reason);
1713 replay_shutdown_request(reason);
1714 shutdown_requested = reason;
1715 qemu_notify_event();
1718 static void qemu_system_powerdown(void)
1720 qapi_event_send_powerdown();
1721 notifier_list_notify(&powerdown_notifiers, NULL);
1724 static void qemu_system_shutdown(ShutdownCause cause)
1726 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1727 notifier_list_notify(&shutdown_notifiers, &cause);
1730 void qemu_system_powerdown_request(void)
1732 trace_qemu_system_powerdown_request();
1733 powerdown_requested = 1;
1734 qemu_notify_event();
1737 void qemu_register_powerdown_notifier(Notifier *notifier)
1739 notifier_list_add(&powerdown_notifiers, notifier);
1742 void qemu_register_shutdown_notifier(Notifier *notifier)
1744 notifier_list_add(&shutdown_notifiers, notifier);
1747 void qemu_system_debug_request(void)
1749 debug_requested = 1;
1750 qemu_notify_event();
1753 static bool main_loop_should_exit(void)
1755 RunState r;
1756 ShutdownCause request;
1758 if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
1759 return false;
1761 if (preconfig_exit_requested) {
1762 if (runstate_check(RUN_STATE_PRECONFIG)) {
1763 runstate_set(RUN_STATE_PRELAUNCH);
1765 preconfig_exit_requested = false;
1766 return true;
1768 if (qemu_debug_requested()) {
1769 vm_stop(RUN_STATE_DEBUG);
1771 if (qemu_suspend_requested()) {
1772 qemu_system_suspend();
1774 request = qemu_shutdown_requested();
1775 if (request) {
1776 qemu_kill_report();
1777 qemu_system_shutdown(request);
1778 if (no_shutdown) {
1779 vm_stop(RUN_STATE_SHUTDOWN);
1780 } else {
1781 return true;
1784 request = qemu_reset_requested();
1785 if (request) {
1786 pause_all_vcpus();
1787 qemu_system_reset(request);
1788 resume_all_vcpus();
1789 if (!runstate_check(RUN_STATE_RUNNING) &&
1790 !runstate_check(RUN_STATE_INMIGRATE)) {
1791 runstate_set(RUN_STATE_PRELAUNCH);
1794 if (qemu_wakeup_requested()) {
1795 pause_all_vcpus();
1796 qemu_system_wakeup();
1797 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1798 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1799 resume_all_vcpus();
1800 qapi_event_send_wakeup();
1802 if (qemu_powerdown_requested()) {
1803 qemu_system_powerdown();
1805 if (qemu_vmstop_requested(&r)) {
1806 vm_stop(r);
1808 return false;
1811 static void main_loop(void)
1813 #ifdef CONFIG_PROFILER
1814 int64_t ti;
1815 #endif
1816 while (!main_loop_should_exit()) {
1817 #ifdef CONFIG_PROFILER
1818 ti = profile_getclock();
1819 #endif
1820 main_loop_wait(false);
1821 #ifdef CONFIG_PROFILER
1822 dev_time += profile_getclock() - ti;
1823 #endif
1827 static void version(void)
1829 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1830 QEMU_COPYRIGHT "\n");
1833 static void QEMU_NORETURN help(int exitcode)
1835 version();
1836 printf("usage: %s [options] [disk_image]\n\n"
1837 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1838 error_get_progname());
1840 #define QEMU_OPTIONS_GENERATE_HELP
1841 #include "qemu-options-wrapper.h"
1843 printf("\nDuring emulation, the following keys are useful:\n"
1844 "ctrl-alt-f toggle full screen\n"
1845 "ctrl-alt-n switch to virtual console 'n'\n"
1846 "ctrl-alt toggle mouse and keyboard grab\n"
1847 "\n"
1848 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1849 "\n"
1850 QEMU_HELP_BOTTOM "\n");
1852 exit(exitcode);
1855 #define HAS_ARG 0x0001
1857 typedef struct QEMUOption {
1858 const char *name;
1859 int flags;
1860 int index;
1861 uint32_t arch_mask;
1862 } QEMUOption;
1864 static const QEMUOption qemu_options[] = {
1865 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1866 #define QEMU_OPTIONS_GENERATE_OPTIONS
1867 #include "qemu-options-wrapper.h"
1868 { NULL },
1871 typedef struct VGAInterfaceInfo {
1872 const char *opt_name; /* option name */
1873 const char *name; /* human-readable name */
1874 /* Class names indicating that support is available.
1875 * If no class is specified, the interface is always available */
1876 const char *class_names[2];
1877 } VGAInterfaceInfo;
1879 static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1880 [VGA_NONE] = {
1881 .opt_name = "none",
1882 .name = "no graphic card",
1884 [VGA_STD] = {
1885 .opt_name = "std",
1886 .name = "standard VGA",
1887 .class_names = { "VGA", "isa-vga" },
1889 [VGA_CIRRUS] = {
1890 .opt_name = "cirrus",
1891 .name = "Cirrus VGA",
1892 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1894 [VGA_VMWARE] = {
1895 .opt_name = "vmware",
1896 .name = "VMWare SVGA",
1897 .class_names = { "vmware-svga" },
1899 [VGA_VIRTIO] = {
1900 .opt_name = "virtio",
1901 .name = "Virtio VGA",
1902 .class_names = { "virtio-vga" },
1904 [VGA_QXL] = {
1905 .opt_name = "qxl",
1906 .name = "QXL VGA",
1907 .class_names = { "qxl-vga" },
1909 [VGA_TCX] = {
1910 .opt_name = "tcx",
1911 .name = "TCX framebuffer",
1912 .class_names = { "SUNW,tcx" },
1914 [VGA_CG3] = {
1915 .opt_name = "cg3",
1916 .name = "CG3 framebuffer",
1917 .class_names = { "cgthree" },
1919 [VGA_XENFB] = {
1920 .opt_name = "xenfb",
1921 .name = "Xen paravirtualized framebuffer",
1925 static bool vga_interface_available(VGAInterfaceType t)
1927 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1929 assert(t < VGA_TYPE_MAX);
1930 return !ti->class_names[0] ||
1931 object_class_by_name(ti->class_names[0]) ||
1932 object_class_by_name(ti->class_names[1]);
1935 static const char *
1936 get_default_vga_model(const MachineClass *machine_class)
1938 if (machine_class->default_display) {
1939 return machine_class->default_display;
1940 } else if (vga_interface_available(VGA_CIRRUS)) {
1941 return "cirrus";
1942 } else if (vga_interface_available(VGA_STD)) {
1943 return "std";
1946 return NULL;
1949 static void select_vgahw(const MachineClass *machine_class, const char *p)
1951 const char *opts;
1952 int t;
1954 if (g_str_equal(p, "help")) {
1955 const char *def = get_default_vga_model(machine_class);
1957 for (t = 0; t < VGA_TYPE_MAX; t++) {
1958 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1960 if (vga_interface_available(t) && ti->opt_name) {
1961 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
1962 g_str_equal(ti->opt_name, def) ? " (default)" : "");
1965 exit(0);
1968 assert(vga_interface_type == VGA_NONE);
1969 for (t = 0; t < VGA_TYPE_MAX; t++) {
1970 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1971 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
1972 if (!vga_interface_available(t)) {
1973 error_report("%s not available", ti->name);
1974 exit(1);
1976 vga_interface_type = t;
1977 break;
1980 if (t == VGA_TYPE_MAX) {
1981 invalid_vga:
1982 error_report("unknown vga type: %s", p);
1983 exit(1);
1985 while (*opts) {
1986 const char *nextopt;
1988 if (strstart(opts, ",retrace=", &nextopt)) {
1989 opts = nextopt;
1990 if (strstart(opts, "dumb", &nextopt))
1991 vga_retrace_method = VGA_RETRACE_DUMB;
1992 else if (strstart(opts, "precise", &nextopt))
1993 vga_retrace_method = VGA_RETRACE_PRECISE;
1994 else goto invalid_vga;
1995 } else goto invalid_vga;
1996 opts = nextopt;
2000 static void parse_display_qapi(const char *optarg)
2002 DisplayOptions *opts;
2003 Visitor *v;
2005 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2007 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2008 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2010 qapi_free_DisplayOptions(opts);
2011 visit_free(v);
2014 DisplayOptions *qmp_query_display_options(Error **errp)
2016 return QAPI_CLONE(DisplayOptions, &dpy);
2019 static void parse_display(const char *p)
2021 const char *opts;
2023 if (strstart(p, "sdl", &opts)) {
2025 * sdl DisplayType needs hand-crafted parser instead of
2026 * parse_display_qapi() due to some options not in
2027 * DisplayOptions, specifically:
2028 * - frame
2029 * Already deprecated.
2030 * - ctrl_grab + alt_grab
2031 * Not clear yet what happens to them long-term. Should
2032 * replaced by something better or deprecated and dropped.
2034 dpy.type = DISPLAY_TYPE_SDL;
2035 while (*opts) {
2036 const char *nextopt;
2038 if (strstart(opts, ",alt_grab=", &nextopt)) {
2039 opts = nextopt;
2040 if (strstart(opts, "on", &nextopt)) {
2041 alt_grab = 1;
2042 } else if (strstart(opts, "off", &nextopt)) {
2043 alt_grab = 0;
2044 } else {
2045 goto invalid_sdl_args;
2047 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2048 opts = nextopt;
2049 if (strstart(opts, "on", &nextopt)) {
2050 ctrl_grab = 1;
2051 } else if (strstart(opts, "off", &nextopt)) {
2052 ctrl_grab = 0;
2053 } else {
2054 goto invalid_sdl_args;
2056 } else if (strstart(opts, ",window_close=", &nextopt)) {
2057 opts = nextopt;
2058 dpy.has_window_close = true;
2059 if (strstart(opts, "on", &nextopt)) {
2060 dpy.window_close = true;
2061 } else if (strstart(opts, "off", &nextopt)) {
2062 dpy.window_close = false;
2063 } else {
2064 goto invalid_sdl_args;
2066 } else if (strstart(opts, ",gl=", &nextopt)) {
2067 opts = nextopt;
2068 dpy.has_gl = true;
2069 if (strstart(opts, "on", &nextopt)) {
2070 dpy.gl = DISPLAYGL_MODE_ON;
2071 } else if (strstart(opts, "core", &nextopt)) {
2072 dpy.gl = DISPLAYGL_MODE_CORE;
2073 } else if (strstart(opts, "es", &nextopt)) {
2074 dpy.gl = DISPLAYGL_MODE_ES;
2075 } else if (strstart(opts, "off", &nextopt)) {
2076 dpy.gl = DISPLAYGL_MODE_OFF;
2077 } else {
2078 goto invalid_sdl_args;
2080 } else {
2081 invalid_sdl_args:
2082 error_report("invalid SDL option string");
2083 exit(1);
2085 opts = nextopt;
2087 } else if (strstart(p, "vnc", &opts)) {
2089 * vnc isn't a (local) DisplayType but a protocol for remote
2090 * display access.
2092 if (*opts == '=') {
2093 vnc_parse(opts + 1, &error_fatal);
2094 } else {
2095 error_report("VNC requires a display argument vnc=<display>");
2096 exit(1);
2098 } else {
2099 parse_display_qapi(p);
2103 char *qemu_find_file(int type, const char *name)
2105 int i;
2106 const char *subdir;
2107 char *buf;
2109 /* Try the name as a straight path first */
2110 if (access(name, R_OK) == 0) {
2111 trace_load_file(name, name);
2112 return g_strdup(name);
2115 switch (type) {
2116 case QEMU_FILE_TYPE_BIOS:
2117 subdir = "";
2118 break;
2119 case QEMU_FILE_TYPE_KEYMAP:
2120 subdir = "keymaps/";
2121 break;
2122 default:
2123 abort();
2126 for (i = 0; i < data_dir_idx; i++) {
2127 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2128 if (access(buf, R_OK) == 0) {
2129 trace_load_file(name, buf);
2130 return buf;
2132 g_free(buf);
2134 return NULL;
2137 static void qemu_add_data_dir(const char *path)
2139 int i;
2141 if (path == NULL) {
2142 return;
2144 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2145 return;
2147 for (i = 0; i < data_dir_idx; i++) {
2148 if (strcmp(data_dir[i], path) == 0) {
2149 return; /* duplicate */
2152 data_dir[data_dir_idx++] = g_strdup(path);
2155 static inline bool nonempty_str(const char *str)
2157 return str && *str;
2160 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2162 gchar *buf;
2163 size_t size;
2164 const char *name, *file, *str;
2165 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2167 if (fw_cfg == NULL) {
2168 error_setg(errp, "fw_cfg device not available");
2169 return -1;
2171 name = qemu_opt_get(opts, "name");
2172 file = qemu_opt_get(opts, "file");
2173 str = qemu_opt_get(opts, "string");
2175 /* we need name and either a file or the content string */
2176 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2177 error_setg(errp, "invalid argument(s)");
2178 return -1;
2180 if (nonempty_str(file) && nonempty_str(str)) {
2181 error_setg(errp, "file and string are mutually exclusive");
2182 return -1;
2184 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2185 error_setg(errp, "name too long (max. %d char)",
2186 FW_CFG_MAX_FILE_PATH - 1);
2187 return -1;
2189 if (strncmp(name, "opt/", 4) != 0) {
2190 warn_report("externally provided fw_cfg item names "
2191 "should be prefixed with \"opt/\"");
2193 if (nonempty_str(str)) {
2194 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2195 buf = g_memdup(str, size);
2196 } else {
2197 GError *err = NULL;
2198 if (!g_file_get_contents(file, &buf, &size, &err)) {
2199 error_setg(errp, "can't load %s: %s", file, err->message);
2200 g_error_free(err);
2201 return -1;
2204 /* For legacy, keep user files in a specific global order. */
2205 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2206 fw_cfg_add_file(fw_cfg, name, buf, size);
2207 fw_cfg_reset_order_override(fw_cfg);
2208 return 0;
2211 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2213 return qdev_device_help(opts);
2216 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2218 DeviceState *dev;
2220 dev = qdev_device_add(opts, errp);
2221 if (!dev && *errp) {
2222 error_report_err(*errp);
2223 return -1;
2224 } else if (dev) {
2225 object_unref(OBJECT(dev));
2227 return 0;
2230 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2232 Error *local_err = NULL;
2234 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
2235 if (local_err) {
2236 error_propagate(errp, local_err);
2237 return -1;
2239 exit(0);
2241 return 0;
2244 #ifdef CONFIG_VIRTFS
2245 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2247 return qemu_fsdev_add(opts, errp);
2249 #endif
2251 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2253 Chardev *chr;
2254 bool qmp;
2255 bool pretty = false;
2256 const char *chardev;
2257 const char *mode;
2259 mode = qemu_opt_get(opts, "mode");
2260 if (mode == NULL) {
2261 mode = "readline";
2263 if (strcmp(mode, "readline") == 0) {
2264 qmp = false;
2265 } else if (strcmp(mode, "control") == 0) {
2266 qmp = true;
2267 } else {
2268 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2269 return -1;
2272 if (!qmp && qemu_opt_get(opts, "pretty")) {
2273 warn_report("'pretty' is deprecated for HMP monitors, it has no effect "
2274 "and will be removed in future versions");
2276 if (qemu_opt_get_bool(opts, "pretty", 0)) {
2277 pretty = true;
2280 chardev = qemu_opt_get(opts, "chardev");
2281 if (!chardev) {
2282 error_report("chardev is required");
2283 exit(1);
2285 chr = qemu_chr_find(chardev);
2286 if (chr == NULL) {
2287 error_setg(errp, "chardev \"%s\" not found", chardev);
2288 return -1;
2291 if (qmp) {
2292 monitor_init_qmp(chr, pretty);
2293 } else {
2294 monitor_init_hmp(chr, true);
2296 return 0;
2299 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2301 static int monitor_device_index = 0;
2302 QemuOpts *opts;
2303 const char *p;
2304 char label[32];
2306 if (strstart(optarg, "chardev:", &p)) {
2307 snprintf(label, sizeof(label), "%s", p);
2308 } else {
2309 snprintf(label, sizeof(label), "compat_monitor%d",
2310 monitor_device_index);
2311 opts = qemu_chr_parse_compat(label, optarg, true);
2312 if (!opts) {
2313 error_report("parse error: %s", optarg);
2314 exit(1);
2318 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2319 qemu_opt_set(opts, "mode", mode, &error_abort);
2320 qemu_opt_set(opts, "chardev", label, &error_abort);
2321 if (!strcmp(mode, "control")) {
2322 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2323 } else {
2324 assert(pretty == false);
2326 monitor_device_index++;
2329 struct device_config {
2330 enum {
2331 DEV_USB, /* -usbdevice */
2332 DEV_BT, /* -bt */
2333 DEV_SERIAL, /* -serial */
2334 DEV_PARALLEL, /* -parallel */
2335 DEV_DEBUGCON, /* -debugcon */
2336 DEV_GDB, /* -gdb, -s */
2337 DEV_SCLP, /* s390 sclp */
2338 } type;
2339 const char *cmdline;
2340 Location loc;
2341 QTAILQ_ENTRY(device_config) next;
2344 static QTAILQ_HEAD(, device_config) device_configs =
2345 QTAILQ_HEAD_INITIALIZER(device_configs);
2347 static void add_device_config(int type, const char *cmdline)
2349 struct device_config *conf;
2351 conf = g_malloc0(sizeof(*conf));
2352 conf->type = type;
2353 conf->cmdline = cmdline;
2354 loc_save(&conf->loc);
2355 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2358 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2360 struct device_config *conf;
2361 int rc;
2363 QTAILQ_FOREACH(conf, &device_configs, next) {
2364 if (conf->type != type)
2365 continue;
2366 loc_push_restore(&conf->loc);
2367 rc = func(conf->cmdline);
2368 loc_pop(&conf->loc);
2369 if (rc) {
2370 return rc;
2373 return 0;
2376 static int serial_parse(const char *devname)
2378 int index = num_serial_hds;
2379 char label[32];
2381 if (strcmp(devname, "none") == 0)
2382 return 0;
2383 snprintf(label, sizeof(label), "serial%d", index);
2384 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2386 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2387 if (!serial_hds[index]) {
2388 error_report("could not connect serial device"
2389 " to character backend '%s'", devname);
2390 return -1;
2392 num_serial_hds++;
2393 return 0;
2396 Chardev *serial_hd(int i)
2398 assert(i >= 0);
2399 if (i < num_serial_hds) {
2400 return serial_hds[i];
2402 return NULL;
2405 int serial_max_hds(void)
2407 return num_serial_hds;
2410 static int parallel_parse(const char *devname)
2412 static int index = 0;
2413 char label[32];
2415 if (strcmp(devname, "none") == 0)
2416 return 0;
2417 if (index == MAX_PARALLEL_PORTS) {
2418 error_report("too many parallel ports");
2419 exit(1);
2421 snprintf(label, sizeof(label), "parallel%d", index);
2422 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
2423 if (!parallel_hds[index]) {
2424 error_report("could not connect parallel device"
2425 " to character backend '%s'", devname);
2426 return -1;
2428 index++;
2429 return 0;
2432 static int debugcon_parse(const char *devname)
2434 QemuOpts *opts;
2436 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
2437 error_report("invalid character backend '%s'", devname);
2438 exit(1);
2440 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2441 if (!opts) {
2442 error_report("already have a debugcon device");
2443 exit(1);
2445 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2446 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2447 return 0;
2450 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2452 const MachineClass *mc1 = a, *mc2 = b;
2453 int res;
2455 if (mc1->family == NULL) {
2456 if (mc2->family == NULL) {
2457 /* Compare standalone machine types against each other; they sort
2458 * in increasing order.
2460 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2461 object_class_get_name(OBJECT_CLASS(mc2)));
2464 /* Standalone machine types sort after families. */
2465 return 1;
2468 if (mc2->family == NULL) {
2469 /* Families sort before standalone machine types. */
2470 return -1;
2473 /* Families sort between each other alphabetically increasingly. */
2474 res = strcmp(mc1->family, mc2->family);
2475 if (res != 0) {
2476 return res;
2479 /* Within the same family, machine types sort in decreasing order. */
2480 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2481 object_class_get_name(OBJECT_CLASS(mc1)));
2484 static MachineClass *machine_parse(const char *name, GSList *machines)
2486 MachineClass *mc;
2487 GSList *el;
2489 if (is_help_option(name)) {
2490 printf("Supported machines are:\n");
2491 machines = g_slist_sort(machines, machine_class_cmp);
2492 for (el = machines; el; el = el->next) {
2493 MachineClass *mc = el->data;
2494 if (mc->alias) {
2495 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2497 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2498 mc->is_default ? " (default)" : "",
2499 mc->deprecation_reason ? " (deprecated)" : "");
2501 exit(0);
2504 mc = find_machine(name, machines);
2505 if (!mc) {
2506 error_report("unsupported machine type");
2507 error_printf("Use -machine help to list supported machines\n");
2508 exit(1);
2510 return mc;
2513 void qemu_add_exit_notifier(Notifier *notify)
2515 notifier_list_add(&exit_notifiers, notify);
2518 void qemu_remove_exit_notifier(Notifier *notify)
2520 notifier_remove(notify);
2523 static void qemu_run_exit_notifiers(void)
2525 notifier_list_notify(&exit_notifiers, NULL);
2528 static const char *pid_file;
2529 static Notifier qemu_unlink_pidfile_notifier;
2531 static void qemu_unlink_pidfile(Notifier *n, void *data)
2533 if (pid_file) {
2534 unlink(pid_file);
2538 bool machine_init_done;
2540 void qemu_add_machine_init_done_notifier(Notifier *notify)
2542 notifier_list_add(&machine_init_done_notifiers, notify);
2543 if (machine_init_done) {
2544 notify->notify(notify, NULL);
2548 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2550 notifier_remove(notify);
2553 static void qemu_run_machine_init_done_notifiers(void)
2555 machine_init_done = true;
2556 notifier_list_notify(&machine_init_done_notifiers, NULL);
2559 static const QEMUOption *lookup_opt(int argc, char **argv,
2560 const char **poptarg, int *poptind)
2562 const QEMUOption *popt;
2563 int optind = *poptind;
2564 char *r = argv[optind];
2565 const char *optarg;
2567 loc_set_cmdline(argv, optind, 1);
2568 optind++;
2569 /* Treat --foo the same as -foo. */
2570 if (r[1] == '-')
2571 r++;
2572 popt = qemu_options;
2573 for(;;) {
2574 if (!popt->name) {
2575 error_report("invalid option");
2576 exit(1);
2578 if (!strcmp(popt->name, r + 1))
2579 break;
2580 popt++;
2582 if (popt->flags & HAS_ARG) {
2583 if (optind >= argc) {
2584 error_report("requires an argument");
2585 exit(1);
2587 optarg = argv[optind++];
2588 loc_set_cmdline(argv, optind - 2, 2);
2589 } else {
2590 optarg = NULL;
2593 *poptarg = optarg;
2594 *poptind = optind;
2596 return popt;
2599 static MachineClass *select_machine(void)
2601 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
2602 MachineClass *machine_class = find_default_machine(machines);
2603 const char *optarg;
2604 QemuOpts *opts;
2605 Location loc;
2607 loc_push_none(&loc);
2609 opts = qemu_get_machine_opts();
2610 qemu_opts_loc_restore(opts);
2612 optarg = qemu_opt_get(opts, "type");
2613 if (optarg) {
2614 machine_class = machine_parse(optarg, machines);
2617 if (!machine_class) {
2618 error_report("No machine specified, and there is no default");
2619 error_printf("Use -machine help to list supported machines\n");
2620 exit(1);
2623 loc_pop(&loc);
2624 g_slist_free(machines);
2625 return machine_class;
2628 static int machine_set_property(void *opaque,
2629 const char *name, const char *value,
2630 Error **errp)
2632 Object *obj = OBJECT(opaque);
2633 Error *local_err = NULL;
2634 char *p, *qom_name;
2636 if (strcmp(name, "type") == 0) {
2637 return 0;
2640 qom_name = g_strdup(name);
2641 for (p = qom_name; *p; p++) {
2642 if (*p == '_') {
2643 *p = '-';
2647 object_property_parse(obj, value, qom_name, &local_err);
2648 g_free(qom_name);
2650 if (local_err) {
2651 error_propagate(errp, local_err);
2652 return -1;
2655 return 0;
2660 * Initial object creation happens before all other
2661 * QEMU data types are created. The majority of objects
2662 * can be created at this point. The rng-egd object
2663 * cannot be created here, as it depends on the chardev
2664 * already existing.
2666 static bool object_create_initial(const char *type, QemuOpts *opts)
2668 if (user_creatable_print_help(type, opts)) {
2669 exit(0);
2673 * Objects should not be made "delayed" without a reason. If you
2674 * add one, state the reason in a comment!
2677 /* Reason: rng-egd property "chardev" */
2678 if (g_str_equal(type, "rng-egd")) {
2679 return false;
2682 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2683 /* Reason: cryptodev-vhost-user property "chardev" */
2684 if (g_str_equal(type, "cryptodev-vhost-user")) {
2685 return false;
2687 #endif
2690 * Reason: filter-* property "netdev" etc.
2692 if (g_str_equal(type, "filter-buffer") ||
2693 g_str_equal(type, "filter-dump") ||
2694 g_str_equal(type, "filter-mirror") ||
2695 g_str_equal(type, "filter-redirector") ||
2696 g_str_equal(type, "colo-compare") ||
2697 g_str_equal(type, "filter-rewriter") ||
2698 g_str_equal(type, "filter-replay")) {
2699 return false;
2702 /* Memory allocation by backends needs to be done
2703 * after configure_accelerator() (due to the tcg_enabled()
2704 * checks at memory_region_init_*()).
2706 * Also, allocation of large amounts of memory may delay
2707 * chardev initialization for too long, and trigger timeouts
2708 * on software that waits for a monitor socket to be created
2709 * (e.g. libvirt).
2711 if (g_str_has_prefix(type, "memory-backend-")) {
2712 return false;
2715 return true;
2720 * The remainder of object creation happens after the
2721 * creation of chardev, fsdev, net clients and device data types.
2723 static bool object_create_delayed(const char *type, QemuOpts *opts)
2725 return !object_create_initial(type, opts);
2729 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2730 MachineClass *mc)
2732 uint64_t sz;
2733 const char *mem_str;
2734 const ram_addr_t default_ram_size = mc->default_ram_size;
2735 QemuOpts *opts = qemu_find_opts_singleton("memory");
2736 Location loc;
2738 loc_push_none(&loc);
2739 qemu_opts_loc_restore(opts);
2741 sz = 0;
2742 mem_str = qemu_opt_get(opts, "size");
2743 if (mem_str) {
2744 if (!*mem_str) {
2745 error_report("missing 'size' option value");
2746 exit(EXIT_FAILURE);
2749 sz = qemu_opt_get_size(opts, "size", ram_size);
2751 /* Fix up legacy suffix-less format */
2752 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2753 uint64_t overflow_check = sz;
2755 sz *= MiB;
2756 if (sz / MiB != overflow_check) {
2757 error_report("too large 'size' option value");
2758 exit(EXIT_FAILURE);
2763 /* backward compatibility behaviour for case "-m 0" */
2764 if (sz == 0) {
2765 sz = default_ram_size;
2768 sz = QEMU_ALIGN_UP(sz, 8192);
2769 ram_size = sz;
2770 if (ram_size != sz) {
2771 error_report("ram size too large");
2772 exit(EXIT_FAILURE);
2775 /* store value for the future use */
2776 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2777 *maxram_size = ram_size;
2779 if (qemu_opt_get(opts, "maxmem")) {
2780 uint64_t slots;
2782 sz = qemu_opt_get_size(opts, "maxmem", 0);
2783 slots = qemu_opt_get_number(opts, "slots", 0);
2784 if (sz < ram_size) {
2785 error_report("invalid value of -m option maxmem: "
2786 "maximum memory size (0x%" PRIx64 ") must be at least "
2787 "the initial memory size (0x" RAM_ADDR_FMT ")",
2788 sz, ram_size);
2789 exit(EXIT_FAILURE);
2790 } else if (slots && sz == ram_size) {
2791 error_report("invalid value of -m option maxmem: "
2792 "memory slots were specified but maximum memory size "
2793 "(0x%" PRIx64 ") is equal to the initial memory size "
2794 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2795 exit(EXIT_FAILURE);
2798 *maxram_size = sz;
2799 *ram_slots = slots;
2800 } else if (qemu_opt_get(opts, "slots")) {
2801 error_report("invalid -m option value: missing 'maxmem' option");
2802 exit(EXIT_FAILURE);
2805 loc_pop(&loc);
2808 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2810 GlobalProperty *g;
2812 g = g_malloc0(sizeof(*g));
2813 g->driver = qemu_opt_get(opts, "driver");
2814 g->property = qemu_opt_get(opts, "property");
2815 g->value = qemu_opt_get(opts, "value");
2816 qdev_prop_register_global(g);
2817 return 0;
2820 static int qemu_read_default_config_file(void)
2822 int ret;
2824 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2825 if (ret < 0 && ret != -ENOENT) {
2826 return ret;
2829 return 0;
2832 static void user_register_global_props(void)
2834 qemu_opts_foreach(qemu_find_opts("global"),
2835 global_init_func, NULL, NULL);
2838 int main(int argc, char **argv)
2840 int i;
2841 int snapshot, linux_boot;
2842 const char *initrd_filename;
2843 const char *kernel_filename, *kernel_cmdline;
2844 const char *boot_order = NULL;
2845 const char *boot_once = NULL;
2846 DisplayState *ds;
2847 QemuOpts *opts, *machine_opts;
2848 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
2849 QemuOptsList *olist;
2850 int optind;
2851 const char *optarg;
2852 const char *loadvm = NULL;
2853 MachineClass *machine_class;
2854 const char *cpu_option;
2855 const char *vga_model = NULL;
2856 const char *qtest_chrdev = NULL;
2857 const char *qtest_log = NULL;
2858 const char *incoming = NULL;
2859 bool userconfig = true;
2860 bool nographic = false;
2861 int display_remote = 0;
2862 const char *log_mask = NULL;
2863 const char *log_file = NULL;
2864 char *trace_file = NULL;
2865 ram_addr_t maxram_size;
2866 uint64_t ram_slots = 0;
2867 FILE *vmstate_dump_file = NULL;
2868 Error *main_loop_err = NULL;
2869 Error *err = NULL;
2870 bool list_data_dirs = false;
2871 char *dir, **dirs;
2872 BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2873 QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
2875 os_set_line_buffering();
2877 error_init(argv[0]);
2878 module_call_init(MODULE_INIT_TRACE);
2880 qemu_init_cpu_list();
2881 qemu_init_cpu_loop();
2883 qemu_mutex_lock_iothread();
2885 atexit(qemu_run_exit_notifiers);
2886 qemu_init_exec_dir(argv[0]);
2888 module_call_init(MODULE_INIT_QOM);
2890 qemu_add_opts(&qemu_drive_opts);
2891 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2892 qemu_add_drive_opts(&qemu_common_drive_opts);
2893 qemu_add_drive_opts(&qemu_drive_opts);
2894 qemu_add_drive_opts(&bdrv_runtime_opts);
2895 qemu_add_opts(&qemu_chardev_opts);
2896 qemu_add_opts(&qemu_device_opts);
2897 qemu_add_opts(&qemu_netdev_opts);
2898 qemu_add_opts(&qemu_nic_opts);
2899 qemu_add_opts(&qemu_net_opts);
2900 qemu_add_opts(&qemu_rtc_opts);
2901 qemu_add_opts(&qemu_global_opts);
2902 qemu_add_opts(&qemu_mon_opts);
2903 qemu_add_opts(&qemu_trace_opts);
2904 qemu_plugin_add_opts();
2905 qemu_add_opts(&qemu_option_rom_opts);
2906 qemu_add_opts(&qemu_machine_opts);
2907 qemu_add_opts(&qemu_accel_opts);
2908 qemu_add_opts(&qemu_mem_opts);
2909 qemu_add_opts(&qemu_smp_opts);
2910 qemu_add_opts(&qemu_boot_opts);
2911 qemu_add_opts(&qemu_add_fd_opts);
2912 qemu_add_opts(&qemu_object_opts);
2913 qemu_add_opts(&qemu_tpmdev_opts);
2914 qemu_add_opts(&qemu_realtime_opts);
2915 qemu_add_opts(&qemu_overcommit_opts);
2916 qemu_add_opts(&qemu_msg_opts);
2917 qemu_add_opts(&qemu_name_opts);
2918 qemu_add_opts(&qemu_numa_opts);
2919 qemu_add_opts(&qemu_icount_opts);
2920 qemu_add_opts(&qemu_semihosting_config_opts);
2921 qemu_add_opts(&qemu_fw_cfg_opts);
2922 module_call_init(MODULE_INIT_OPTS);
2924 runstate_init();
2925 precopy_infrastructure_init();
2926 postcopy_infrastructure_init();
2927 monitor_init_globals();
2929 if (qcrypto_init(&err) < 0) {
2930 error_reportf_err(err, "cannot initialize crypto: ");
2931 exit(1);
2934 QTAILQ_INIT(&vm_change_state_head);
2935 os_setup_early_signal_handling();
2937 cpu_option = NULL;
2938 snapshot = 0;
2940 nb_nics = 0;
2942 bdrv_init_with_whitelist();
2944 autostart = 1;
2946 /* first pass of option parsing */
2947 optind = 1;
2948 while (optind < argc) {
2949 if (argv[optind][0] != '-') {
2950 /* disk image */
2951 optind++;
2952 } else {
2953 const QEMUOption *popt;
2955 popt = lookup_opt(argc, argv, &optarg, &optind);
2956 switch (popt->index) {
2957 case QEMU_OPTION_nouserconfig:
2958 userconfig = false;
2959 break;
2964 if (userconfig) {
2965 if (qemu_read_default_config_file() < 0) {
2966 exit(1);
2970 /* second pass of option parsing */
2971 optind = 1;
2972 for(;;) {
2973 if (optind >= argc)
2974 break;
2975 if (argv[optind][0] != '-') {
2976 loc_set_cmdline(argv, optind, 1);
2977 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2978 } else {
2979 const QEMUOption *popt;
2981 popt = lookup_opt(argc, argv, &optarg, &optind);
2982 if (!(popt->arch_mask & arch_type)) {
2983 error_report("Option not supported for this target");
2984 exit(1);
2986 switch(popt->index) {
2987 case QEMU_OPTION_cpu:
2988 /* hw initialization will check this */
2989 cpu_option = optarg;
2990 break;
2991 case QEMU_OPTION_hda:
2992 case QEMU_OPTION_hdb:
2993 case QEMU_OPTION_hdc:
2994 case QEMU_OPTION_hdd:
2995 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2996 HD_OPTS);
2997 break;
2998 case QEMU_OPTION_blockdev:
3000 Visitor *v;
3001 BlockdevOptionsQueueEntry *bdo;
3003 v = qobject_input_visitor_new_str(optarg, "driver",
3004 &error_fatal);
3006 bdo = g_new(BlockdevOptionsQueueEntry, 1);
3007 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3008 &error_fatal);
3009 visit_free(v);
3010 loc_save(&bdo->loc);
3011 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3012 break;
3014 case QEMU_OPTION_drive:
3015 if (drive_def(optarg) == NULL) {
3016 exit(1);
3018 break;
3019 case QEMU_OPTION_set:
3020 if (qemu_set_option(optarg) != 0)
3021 exit(1);
3022 break;
3023 case QEMU_OPTION_global:
3024 if (qemu_global_option(optarg) != 0)
3025 exit(1);
3026 break;
3027 case QEMU_OPTION_mtdblock:
3028 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3029 break;
3030 case QEMU_OPTION_sd:
3031 drive_add(IF_SD, -1, optarg, SD_OPTS);
3032 break;
3033 case QEMU_OPTION_pflash:
3034 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3035 break;
3036 case QEMU_OPTION_snapshot:
3038 Error *blocker = NULL;
3039 snapshot = 1;
3040 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
3041 "-snapshot");
3042 replay_add_blocker(blocker);
3044 break;
3045 case QEMU_OPTION_numa:
3046 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3047 optarg, true);
3048 if (!opts) {
3049 exit(1);
3051 break;
3052 case QEMU_OPTION_display:
3053 parse_display(optarg);
3054 break;
3055 case QEMU_OPTION_nographic:
3056 olist = qemu_find_opts("machine");
3057 qemu_opts_parse_noisily(olist, "graphics=off", false);
3058 nographic = true;
3059 dpy.type = DISPLAY_TYPE_NONE;
3060 break;
3061 case QEMU_OPTION_curses:
3062 #ifdef CONFIG_CURSES
3063 dpy.type = DISPLAY_TYPE_CURSES;
3064 #else
3065 error_report("curses or iconv support is disabled");
3066 exit(1);
3067 #endif
3068 break;
3069 case QEMU_OPTION_portrait:
3070 graphic_rotate = 90;
3071 break;
3072 case QEMU_OPTION_rotate:
3073 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3074 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3075 graphic_rotate != 180 && graphic_rotate != 270) {
3076 error_report("only 90, 180, 270 deg rotation is available");
3077 exit(1);
3079 break;
3080 case QEMU_OPTION_kernel:
3081 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3082 &error_abort);
3083 break;
3084 case QEMU_OPTION_initrd:
3085 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3086 &error_abort);
3087 break;
3088 case QEMU_OPTION_append:
3089 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3090 &error_abort);
3091 break;
3092 case QEMU_OPTION_dtb:
3093 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3094 &error_abort);
3095 break;
3096 case QEMU_OPTION_cdrom:
3097 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3098 break;
3099 case QEMU_OPTION_boot:
3100 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3101 optarg, true);
3102 if (!opts) {
3103 exit(1);
3105 break;
3106 case QEMU_OPTION_fda:
3107 case QEMU_OPTION_fdb:
3108 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3109 optarg, FD_OPTS);
3110 break;
3111 case QEMU_OPTION_no_fd_bootchk:
3112 fd_bootchk = 0;
3113 break;
3114 case QEMU_OPTION_netdev:
3115 default_net = 0;
3116 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3117 exit(1);
3119 break;
3120 case QEMU_OPTION_nic:
3121 default_net = 0;
3122 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3123 exit(1);
3125 break;
3126 case QEMU_OPTION_net:
3127 default_net = 0;
3128 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3129 exit(1);
3131 break;
3132 #ifdef CONFIG_LIBISCSI
3133 case QEMU_OPTION_iscsi:
3134 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3135 optarg, false);
3136 if (!opts) {
3137 exit(1);
3139 break;
3140 #endif
3141 case QEMU_OPTION_bt:
3142 warn_report("The bluetooth subsystem is deprecated and will "
3143 "be removed soon. If the bluetooth subsystem is "
3144 "still useful for you, please send a mail to "
3145 "qemu-devel@nongnu.org with your usecase.");
3146 add_device_config(DEV_BT, optarg);
3147 break;
3148 case QEMU_OPTION_audio_help:
3149 audio_legacy_help();
3150 exit (0);
3151 break;
3152 case QEMU_OPTION_audiodev:
3153 audio_parse_option(optarg);
3154 break;
3155 case QEMU_OPTION_soundhw:
3156 select_soundhw (optarg);
3157 break;
3158 case QEMU_OPTION_h:
3159 help(0);
3160 break;
3161 case QEMU_OPTION_version:
3162 version();
3163 exit(0);
3164 break;
3165 case QEMU_OPTION_m:
3166 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3167 optarg, true);
3168 if (!opts) {
3169 exit(EXIT_FAILURE);
3171 break;
3172 #ifdef CONFIG_TPM
3173 case QEMU_OPTION_tpmdev:
3174 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3175 exit(1);
3177 break;
3178 #endif
3179 case QEMU_OPTION_mempath:
3180 mem_path = optarg;
3181 break;
3182 case QEMU_OPTION_mem_prealloc:
3183 mem_prealloc = 1;
3184 break;
3185 case QEMU_OPTION_d:
3186 log_mask = optarg;
3187 break;
3188 case QEMU_OPTION_D:
3189 log_file = optarg;
3190 break;
3191 case QEMU_OPTION_DFILTER:
3192 qemu_set_dfilter_ranges(optarg, &error_fatal);
3193 break;
3194 case QEMU_OPTION_seed:
3195 qemu_guest_random_seed_main(optarg, &error_fatal);
3196 break;
3197 case QEMU_OPTION_s:
3198 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3199 break;
3200 case QEMU_OPTION_gdb:
3201 add_device_config(DEV_GDB, optarg);
3202 break;
3203 case QEMU_OPTION_L:
3204 if (is_help_option(optarg)) {
3205 list_data_dirs = true;
3206 } else {
3207 qemu_add_data_dir(optarg);
3209 break;
3210 case QEMU_OPTION_bios:
3211 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3212 &error_abort);
3213 break;
3214 case QEMU_OPTION_singlestep:
3215 singlestep = 1;
3216 break;
3217 case QEMU_OPTION_S:
3218 autostart = 0;
3219 break;
3220 case QEMU_OPTION_k:
3221 keyboard_layout = optarg;
3222 break;
3223 case QEMU_OPTION_vga:
3224 vga_model = optarg;
3225 default_vga = 0;
3226 break;
3227 case QEMU_OPTION_g:
3229 const char *p;
3230 int w, h, depth;
3231 p = optarg;
3232 w = strtol(p, (char **)&p, 10);
3233 if (w <= 0) {
3234 graphic_error:
3235 error_report("invalid resolution or depth");
3236 exit(1);
3238 if (*p != 'x')
3239 goto graphic_error;
3240 p++;
3241 h = strtol(p, (char **)&p, 10);
3242 if (h <= 0)
3243 goto graphic_error;
3244 if (*p == 'x') {
3245 p++;
3246 depth = strtol(p, (char **)&p, 10);
3247 if (depth != 1 && depth != 2 && depth != 4 &&
3248 depth != 8 && depth != 15 && depth != 16 &&
3249 depth != 24 && depth != 32)
3250 goto graphic_error;
3251 } else if (*p == '\0') {
3252 depth = graphic_depth;
3253 } else {
3254 goto graphic_error;
3257 graphic_width = w;
3258 graphic_height = h;
3259 graphic_depth = depth;
3261 break;
3262 case QEMU_OPTION_echr:
3264 char *r;
3265 term_escape_char = strtol(optarg, &r, 0);
3266 if (r == optarg)
3267 printf("Bad argument to echr\n");
3268 break;
3270 case QEMU_OPTION_monitor:
3271 default_monitor = 0;
3272 if (strncmp(optarg, "none", 4)) {
3273 monitor_parse(optarg, "readline", false);
3275 break;
3276 case QEMU_OPTION_qmp:
3277 monitor_parse(optarg, "control", false);
3278 default_monitor = 0;
3279 break;
3280 case QEMU_OPTION_qmp_pretty:
3281 monitor_parse(optarg, "control", true);
3282 default_monitor = 0;
3283 break;
3284 case QEMU_OPTION_mon:
3285 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3286 true);
3287 if (!opts) {
3288 exit(1);
3290 default_monitor = 0;
3291 break;
3292 case QEMU_OPTION_chardev:
3293 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3294 optarg, true);
3295 if (!opts) {
3296 exit(1);
3298 break;
3299 case QEMU_OPTION_fsdev:
3300 olist = qemu_find_opts("fsdev");
3301 if (!olist) {
3302 error_report("fsdev support is disabled");
3303 exit(1);
3305 opts = qemu_opts_parse_noisily(olist, optarg, true);
3306 if (!opts) {
3307 exit(1);
3309 break;
3310 case QEMU_OPTION_virtfs: {
3311 QemuOpts *fsdev;
3312 QemuOpts *device;
3313 const char *writeout, *sock_fd, *socket, *path, *security_model,
3314 *multidevs;
3316 olist = qemu_find_opts("virtfs");
3317 if (!olist) {
3318 error_report("virtfs support is disabled");
3319 exit(1);
3321 opts = qemu_opts_parse_noisily(olist, optarg, true);
3322 if (!opts) {
3323 exit(1);
3326 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3327 qemu_opt_get(opts, "mount_tag") == NULL) {
3328 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3329 exit(1);
3331 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3332 qemu_opts_id(opts) ?:
3333 qemu_opt_get(opts, "mount_tag"),
3334 1, NULL);
3335 if (!fsdev) {
3336 error_report("duplicate or invalid fsdev id: %s",
3337 qemu_opt_get(opts, "mount_tag"));
3338 exit(1);
3341 writeout = qemu_opt_get(opts, "writeout");
3342 if (writeout) {
3343 #ifdef CONFIG_SYNC_FILE_RANGE
3344 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3345 #else
3346 error_report("writeout=immediate not supported "
3347 "on this platform");
3348 exit(1);
3349 #endif
3351 qemu_opt_set(fsdev, "fsdriver",
3352 qemu_opt_get(opts, "fsdriver"), &error_abort);
3353 path = qemu_opt_get(opts, "path");
3354 if (path) {
3355 qemu_opt_set(fsdev, "path", path, &error_abort);
3357 security_model = qemu_opt_get(opts, "security_model");
3358 if (security_model) {
3359 qemu_opt_set(fsdev, "security_model", security_model,
3360 &error_abort);
3362 socket = qemu_opt_get(opts, "socket");
3363 if (socket) {
3364 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3366 sock_fd = qemu_opt_get(opts, "sock_fd");
3367 if (sock_fd) {
3368 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3371 qemu_opt_set_bool(fsdev, "readonly",
3372 qemu_opt_get_bool(opts, "readonly", 0),
3373 &error_abort);
3374 multidevs = qemu_opt_get(opts, "multidevs");
3375 if (multidevs) {
3376 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3378 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3379 &error_abort);
3380 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3381 qemu_opt_set(device, "fsdev",
3382 qemu_opts_id(fsdev), &error_abort);
3383 qemu_opt_set(device, "mount_tag",
3384 qemu_opt_get(opts, "mount_tag"), &error_abort);
3385 break;
3387 case QEMU_OPTION_virtfs_synth: {
3388 QemuOpts *fsdev;
3389 QemuOpts *device;
3391 warn_report("'-virtfs_synth' is deprecated, please use "
3392 "'-fsdev synth' and '-device virtio-9p-...' "
3393 "instead");
3395 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3396 1, NULL);
3397 if (!fsdev) {
3398 error_report("duplicate option: %s", "virtfs_synth");
3399 exit(1);
3401 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3403 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3404 &error_abort);
3405 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3406 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3407 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3408 break;
3410 case QEMU_OPTION_serial:
3411 add_device_config(DEV_SERIAL, optarg);
3412 default_serial = 0;
3413 if (strncmp(optarg, "mon:", 4) == 0) {
3414 default_monitor = 0;
3416 break;
3417 case QEMU_OPTION_watchdog:
3418 if (watchdog) {
3419 error_report("only one watchdog option may be given");
3420 return 1;
3422 watchdog = optarg;
3423 break;
3424 case QEMU_OPTION_watchdog_action:
3425 if (select_watchdog_action(optarg) == -1) {
3426 error_report("unknown -watchdog-action parameter");
3427 exit(1);
3429 break;
3430 case QEMU_OPTION_parallel:
3431 add_device_config(DEV_PARALLEL, optarg);
3432 default_parallel = 0;
3433 if (strncmp(optarg, "mon:", 4) == 0) {
3434 default_monitor = 0;
3436 break;
3437 case QEMU_OPTION_debugcon:
3438 add_device_config(DEV_DEBUGCON, optarg);
3439 break;
3440 case QEMU_OPTION_loadvm:
3441 loadvm = optarg;
3442 break;
3443 case QEMU_OPTION_full_screen:
3444 dpy.has_full_screen = true;
3445 dpy.full_screen = true;
3446 break;
3447 case QEMU_OPTION_alt_grab:
3448 alt_grab = 1;
3449 break;
3450 case QEMU_OPTION_ctrl_grab:
3451 ctrl_grab = 1;
3452 break;
3453 case QEMU_OPTION_no_quit:
3454 dpy.has_window_close = true;
3455 dpy.window_close = false;
3456 break;
3457 case QEMU_OPTION_sdl:
3458 #ifdef CONFIG_SDL
3459 dpy.type = DISPLAY_TYPE_SDL;
3460 break;
3461 #else
3462 error_report("SDL support is disabled");
3463 exit(1);
3464 #endif
3465 case QEMU_OPTION_pidfile:
3466 pid_file = optarg;
3467 break;
3468 case QEMU_OPTION_win2k_hack:
3469 win2k_install_hack = 1;
3470 break;
3471 case QEMU_OPTION_acpitable:
3472 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3473 optarg, true);
3474 if (!opts) {
3475 exit(1);
3477 acpi_table_add(opts, &error_fatal);
3478 break;
3479 case QEMU_OPTION_smbios:
3480 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3481 optarg, false);
3482 if (!opts) {
3483 exit(1);
3485 smbios_entry_add(opts, &error_fatal);
3486 break;
3487 case QEMU_OPTION_fwcfg:
3488 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3489 optarg, true);
3490 if (opts == NULL) {
3491 exit(1);
3493 break;
3494 case QEMU_OPTION_preconfig:
3495 preconfig_exit_requested = false;
3496 break;
3497 case QEMU_OPTION_enable_kvm:
3498 olist = qemu_find_opts("machine");
3499 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3500 break;
3501 case QEMU_OPTION_M:
3502 case QEMU_OPTION_machine:
3503 olist = qemu_find_opts("machine");
3504 opts = qemu_opts_parse_noisily(olist, optarg, true);
3505 if (!opts) {
3506 exit(1);
3508 break;
3509 case QEMU_OPTION_no_kvm:
3510 olist = qemu_find_opts("machine");
3511 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3512 break;
3513 case QEMU_OPTION_accel:
3514 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3515 optarg, true);
3516 optarg = qemu_opt_get(accel_opts, "accel");
3517 if (!optarg || is_help_option(optarg)) {
3518 printf("Accelerators supported in QEMU binary:\n");
3519 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3520 false);
3521 for (el = accel_list; el; el = el->next) {
3522 gchar *typename = g_strdup(object_class_get_name(
3523 OBJECT_CLASS(el->data)));
3524 /* omit qtest which is used for tests only */
3525 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3526 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3527 gchar **optname = g_strsplit(typename,
3528 ACCEL_CLASS_SUFFIX, 0);
3529 printf("%s\n", optname[0]);
3530 g_free(optname);
3532 g_free(typename);
3534 g_slist_free(accel_list);
3535 exit(0);
3537 if (optarg && strchr(optarg, ':')) {
3538 error_report("Don't use ':' with -accel, "
3539 "use -M accel=... for now instead");
3540 exit(1);
3542 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3543 false, &error_abort);
3544 qemu_opt_set(opts, "accel", optarg, &error_abort);
3545 break;
3546 case QEMU_OPTION_usb:
3547 olist = qemu_find_opts("machine");
3548 qemu_opts_parse_noisily(olist, "usb=on", false);
3549 break;
3550 case QEMU_OPTION_usbdevice:
3551 error_report("'-usbdevice' is deprecated, please use "
3552 "'-device usb-...' instead");
3553 olist = qemu_find_opts("machine");
3554 qemu_opts_parse_noisily(olist, "usb=on", false);
3555 add_device_config(DEV_USB, optarg);
3556 break;
3557 case QEMU_OPTION_device:
3558 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3559 optarg, true)) {
3560 exit(1);
3562 break;
3563 case QEMU_OPTION_smp:
3564 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3565 optarg, true)) {
3566 exit(1);
3568 break;
3569 case QEMU_OPTION_vnc:
3570 vnc_parse(optarg, &error_fatal);
3571 break;
3572 case QEMU_OPTION_no_acpi:
3573 acpi_enabled = 0;
3574 break;
3575 case QEMU_OPTION_no_hpet:
3576 no_hpet = 1;
3577 break;
3578 case QEMU_OPTION_no_reboot:
3579 no_reboot = 1;
3580 break;
3581 case QEMU_OPTION_no_shutdown:
3582 no_shutdown = 1;
3583 break;
3584 case QEMU_OPTION_show_cursor:
3585 cursor_hide = 0;
3586 break;
3587 case QEMU_OPTION_uuid:
3588 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3589 error_report("failed to parse UUID string: wrong format");
3590 exit(1);
3592 qemu_uuid_set = true;
3593 break;
3594 case QEMU_OPTION_option_rom:
3595 if (nb_option_roms >= MAX_OPTION_ROMS) {
3596 error_report("too many option ROMs");
3597 exit(1);
3599 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3600 optarg, true);
3601 if (!opts) {
3602 exit(1);
3604 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3605 option_rom[nb_option_roms].bootindex =
3606 qemu_opt_get_number(opts, "bootindex", -1);
3607 if (!option_rom[nb_option_roms].name) {
3608 error_report("Option ROM file is not specified");
3609 exit(1);
3611 nb_option_roms++;
3612 break;
3613 case QEMU_OPTION_semihosting:
3614 qemu_semihosting_enable();
3615 break;
3616 case QEMU_OPTION_semihosting_config:
3617 if (qemu_semihosting_config_options(optarg) != 0) {
3618 exit(1);
3620 break;
3621 case QEMU_OPTION_name:
3622 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3623 optarg, true);
3624 if (!opts) {
3625 exit(1);
3627 break;
3628 case QEMU_OPTION_prom_env:
3629 if (nb_prom_envs >= MAX_PROM_ENVS) {
3630 error_report("too many prom variables");
3631 exit(1);
3633 prom_envs[nb_prom_envs] = optarg;
3634 nb_prom_envs++;
3635 break;
3636 case QEMU_OPTION_old_param:
3637 old_param = 1;
3638 break;
3639 case QEMU_OPTION_rtc:
3640 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3641 false);
3642 if (!opts) {
3643 exit(1);
3645 break;
3646 case QEMU_OPTION_tb_size:
3647 #ifndef CONFIG_TCG
3648 error_report("TCG is disabled");
3649 exit(1);
3650 #endif
3651 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3652 error_report("Invalid argument to -tb-size");
3653 exit(1);
3655 break;
3656 case QEMU_OPTION_icount:
3657 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3658 optarg, true);
3659 if (!icount_opts) {
3660 exit(1);
3662 break;
3663 case QEMU_OPTION_incoming:
3664 if (!incoming) {
3665 runstate_set(RUN_STATE_INMIGRATE);
3667 incoming = optarg;
3668 break;
3669 case QEMU_OPTION_only_migratable:
3670 only_migratable = 1;
3671 break;
3672 case QEMU_OPTION_nodefaults:
3673 has_defaults = 0;
3674 break;
3675 case QEMU_OPTION_xen_domid:
3676 if (!(xen_available())) {
3677 error_report("Option not supported for this target");
3678 exit(1);
3680 xen_domid = atoi(optarg);
3681 break;
3682 case QEMU_OPTION_xen_attach:
3683 if (!(xen_available())) {
3684 error_report("Option not supported for this target");
3685 exit(1);
3687 xen_mode = XEN_ATTACH;
3688 break;
3689 case QEMU_OPTION_xen_domid_restrict:
3690 if (!(xen_available())) {
3691 error_report("Option not supported for this target");
3692 exit(1);
3694 xen_domid_restrict = true;
3695 break;
3696 case QEMU_OPTION_trace:
3697 g_free(trace_file);
3698 trace_file = trace_opt_parse(optarg);
3699 break;
3700 case QEMU_OPTION_trace_unassigned:
3701 trace_unassigned = true;
3702 break;
3703 case QEMU_OPTION_plugin:
3704 qemu_plugin_opt_parse(optarg, &plugin_list);
3705 break;
3706 case QEMU_OPTION_readconfig:
3708 int ret = qemu_read_config_file(optarg);
3709 if (ret < 0) {
3710 error_report("read config %s: %s", optarg,
3711 strerror(-ret));
3712 exit(1);
3714 break;
3716 case QEMU_OPTION_spice:
3717 olist = qemu_find_opts("spice");
3718 if (!olist) {
3719 error_report("spice support is disabled");
3720 exit(1);
3722 opts = qemu_opts_parse_noisily(olist, optarg, false);
3723 if (!opts) {
3724 exit(1);
3726 display_remote++;
3727 break;
3728 case QEMU_OPTION_writeconfig:
3730 FILE *fp;
3731 if (strcmp(optarg, "-") == 0) {
3732 fp = stdout;
3733 } else {
3734 fp = fopen(optarg, "w");
3735 if (fp == NULL) {
3736 error_report("open %s: %s", optarg,
3737 strerror(errno));
3738 exit(1);
3741 qemu_config_write(fp);
3742 if (fp != stdout) {
3743 fclose(fp);
3745 break;
3747 case QEMU_OPTION_qtest:
3748 qtest_chrdev = optarg;
3749 break;
3750 case QEMU_OPTION_qtest_log:
3751 qtest_log = optarg;
3752 break;
3753 case QEMU_OPTION_sandbox:
3754 olist = qemu_find_opts("sandbox");
3755 if (!olist) {
3756 #ifndef CONFIG_SECCOMP
3757 error_report("-sandbox support is not enabled "
3758 "in this QEMU binary");
3759 #endif
3760 exit(1);
3763 opts = qemu_opts_parse_noisily(olist, optarg, true);
3764 if (!opts) {
3765 exit(1);
3767 break;
3768 case QEMU_OPTION_add_fd:
3769 #ifndef _WIN32
3770 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3771 optarg, false);
3772 if (!opts) {
3773 exit(1);
3775 #else
3776 error_report("File descriptor passing is disabled on this "
3777 "platform");
3778 exit(1);
3779 #endif
3780 break;
3781 case QEMU_OPTION_object:
3782 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3783 optarg, true);
3784 if (!opts) {
3785 exit(1);
3787 break;
3788 case QEMU_OPTION_realtime:
3789 warn_report("'-realtime mlock=...' is deprecated, please use "
3790 "'-overcommit mem-lock=...' instead");
3791 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3792 optarg, false);
3793 if (!opts) {
3794 exit(1);
3796 /* Don't override the -overcommit option if set */
3797 enable_mlock = enable_mlock ||
3798 qemu_opt_get_bool(opts, "mlock", true);
3799 break;
3800 case QEMU_OPTION_overcommit:
3801 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3802 optarg, false);
3803 if (!opts) {
3804 exit(1);
3806 /* Don't override the -realtime option if set */
3807 enable_mlock = enable_mlock ||
3808 qemu_opt_get_bool(opts, "mem-lock", false);
3809 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
3810 break;
3811 case QEMU_OPTION_msg:
3812 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3813 false);
3814 if (!opts) {
3815 exit(1);
3817 configure_msg(opts);
3818 break;
3819 case QEMU_OPTION_dump_vmstate:
3820 if (vmstate_dump_file) {
3821 error_report("only one '-dump-vmstate' "
3822 "option may be given");
3823 exit(1);
3825 vmstate_dump_file = fopen(optarg, "w");
3826 if (vmstate_dump_file == NULL) {
3827 error_report("open %s: %s", optarg, strerror(errno));
3828 exit(1);
3830 break;
3831 case QEMU_OPTION_enable_sync_profile:
3832 qsp_enable();
3833 break;
3834 case QEMU_OPTION_nouserconfig:
3835 /* Nothing to be parsed here. Especially, do not error out below. */
3836 break;
3837 default:
3838 if (os_parse_cmd_args(popt->index, optarg)) {
3839 error_report("Option not supported in this build");
3840 exit(1);
3846 * Clear error location left behind by the loop.
3847 * Best done right after the loop. Do not insert code here!
3849 loc_set_none();
3851 user_register_global_props();
3853 replay_configure(icount_opts);
3855 if (incoming && !preconfig_exit_requested) {
3856 error_report("'preconfig' and 'incoming' options are "
3857 "mutually exclusive");
3858 exit(EXIT_FAILURE);
3861 configure_rtc(qemu_find_opts_singleton("rtc"));
3863 machine_class = select_machine();
3864 object_set_machine_compat_props(machine_class->compat_props);
3866 set_memory_options(&ram_slots, &maxram_size, machine_class);
3868 os_daemonize();
3869 rcu_disable_atfork();
3871 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
3872 error_reportf_err(err, "cannot create PID file: ");
3873 exit(1);
3876 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
3877 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
3879 if (qemu_init_main_loop(&main_loop_err)) {
3880 error_report_err(main_loop_err);
3881 exit(1);
3884 #ifdef CONFIG_SECCOMP
3885 olist = qemu_find_opts_err("sandbox", NULL);
3886 if (olist) {
3887 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
3889 #endif
3891 qemu_opts_foreach(qemu_find_opts("name"),
3892 parse_name, NULL, &error_fatal);
3894 #ifndef _WIN32
3895 qemu_opts_foreach(qemu_find_opts("add-fd"),
3896 parse_add_fd, NULL, &error_fatal);
3898 qemu_opts_foreach(qemu_find_opts("add-fd"),
3899 cleanup_add_fd, NULL, &error_fatal);
3900 #endif
3902 current_machine = MACHINE(object_new(object_class_get_name(
3903 OBJECT_CLASS(machine_class))));
3904 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3905 exit(0);
3907 object_property_add_child(object_get_root(), "machine",
3908 OBJECT(current_machine), &error_abort);
3909 object_property_add_child(container_get(OBJECT(current_machine),
3910 "/unattached"),
3911 "sysbus", OBJECT(sysbus_get_default()),
3912 NULL);
3914 if (machine_class->minimum_page_bits) {
3915 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
3916 /* This would be a board error: specifying a minimum smaller than
3917 * a target's compile-time fixed setting.
3919 g_assert_not_reached();
3923 cpu_exec_init_all();
3925 if (machine_class->hw_version) {
3926 qemu_set_hw_version(machine_class->hw_version);
3929 if (cpu_option && is_help_option(cpu_option)) {
3930 list_cpus(cpu_option);
3931 exit(0);
3934 if (!trace_init_backends()) {
3935 exit(1);
3937 trace_init_file(trace_file);
3939 /* Open the logfile at this point and set the log mask if necessary.
3941 if (log_file) {
3942 qemu_set_log_filename(log_file, &error_fatal);
3945 if (log_mask) {
3946 int mask;
3947 mask = qemu_str_to_log_mask(log_mask);
3948 if (!mask) {
3949 qemu_print_log_usage(stdout);
3950 exit(1);
3952 qemu_set_log(mask);
3953 } else {
3954 qemu_set_log(0);
3957 /* add configured firmware directories */
3958 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
3959 for (i = 0; dirs[i] != NULL; i++) {
3960 qemu_add_data_dir(dirs[i]);
3962 g_strfreev(dirs);
3964 /* try to find datadir relative to the executable path */
3965 dir = os_find_datadir();
3966 qemu_add_data_dir(dir);
3967 g_free(dir);
3969 /* add the datadir specified when building */
3970 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
3972 /* -L help lists the data directories and exits. */
3973 if (list_data_dirs) {
3974 for (i = 0; i < data_dir_idx; i++) {
3975 printf("%s\n", data_dir[i]);
3977 exit(0);
3980 /* machine_class: default to UP */
3981 machine_class->max_cpus = machine_class->max_cpus ?: 1;
3982 machine_class->min_cpus = machine_class->min_cpus ?: 1;
3983 machine_class->default_cpus = machine_class->default_cpus ?: 1;
3985 /* default to machine_class->default_cpus */
3986 current_machine->smp.cpus = machine_class->default_cpus;
3987 current_machine->smp.max_cpus = machine_class->default_cpus;
3988 current_machine->smp.cores = 1;
3989 current_machine->smp.threads = 1;
3991 machine_class->smp_parse(current_machine,
3992 qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3994 /* sanity-check smp_cpus and max_cpus against machine_class */
3995 if (current_machine->smp.cpus < machine_class->min_cpus) {
3996 error_report("Invalid SMP CPUs %d. The min CPUs "
3997 "supported by machine '%s' is %d",
3998 current_machine->smp.cpus,
3999 machine_class->name, machine_class->min_cpus);
4000 exit(1);
4002 if (current_machine->smp.max_cpus > machine_class->max_cpus) {
4003 error_report("Invalid SMP CPUs %d. The max CPUs "
4004 "supported by machine '%s' is %d",
4005 current_machine->smp.max_cpus,
4006 machine_class->name, machine_class->max_cpus);
4007 exit(1);
4011 * Get the default machine options from the machine if it is not already
4012 * specified either by the configuration file or by the command line.
4014 if (machine_class->default_machine_opts) {
4015 qemu_opts_set_defaults(qemu_find_opts("machine"),
4016 machine_class->default_machine_opts, 0);
4019 /* process plugin before CPUs are created, but once -smp has been parsed */
4020 if (qemu_plugin_load_list(&plugin_list)) {
4021 exit(1);
4024 qemu_opts_foreach(qemu_find_opts("device"),
4025 default_driver_check, NULL, NULL);
4026 qemu_opts_foreach(qemu_find_opts("global"),
4027 default_driver_check, NULL, NULL);
4029 if (!vga_model && !default_vga) {
4030 vga_interface_type = VGA_DEVICE;
4032 if (!has_defaults || machine_class->no_serial) {
4033 default_serial = 0;
4035 if (!has_defaults || machine_class->no_parallel) {
4036 default_parallel = 0;
4038 if (!has_defaults || machine_class->no_floppy) {
4039 default_floppy = 0;
4041 if (!has_defaults || machine_class->no_cdrom) {
4042 default_cdrom = 0;
4044 if (!has_defaults || machine_class->no_sdcard) {
4045 default_sdcard = 0;
4047 if (!has_defaults) {
4048 default_monitor = 0;
4049 default_net = 0;
4050 default_vga = 0;
4053 if (is_daemonized()) {
4054 if (!preconfig_exit_requested) {
4055 error_report("'preconfig' and 'daemonize' options are "
4056 "mutually exclusive");
4057 exit(EXIT_FAILURE);
4060 /* According to documentation and historically, -nographic redirects
4061 * serial port, parallel port and monitor to stdio, which does not work
4062 * with -daemonize. We can redirect these to null instead, but since
4063 * -nographic is legacy, let's just error out.
4064 * We disallow -nographic only if all other ports are not redirected
4065 * explicitly, to not break existing legacy setups which uses
4066 * -nographic _and_ redirects all ports explicitly - this is valid
4067 * usage, -nographic is just a no-op in this case.
4069 if (nographic
4070 && (default_parallel || default_serial || default_monitor)) {
4071 error_report("-nographic cannot be used with -daemonize");
4072 exit(1);
4074 #ifdef CONFIG_CURSES
4075 if (dpy.type == DISPLAY_TYPE_CURSES) {
4076 error_report("curses display cannot be used with -daemonize");
4077 exit(1);
4079 #endif
4082 if (nographic) {
4083 if (default_parallel)
4084 add_device_config(DEV_PARALLEL, "null");
4085 if (default_serial && default_monitor) {
4086 add_device_config(DEV_SERIAL, "mon:stdio");
4087 } else {
4088 if (default_serial)
4089 add_device_config(DEV_SERIAL, "stdio");
4090 if (default_monitor)
4091 monitor_parse("stdio", "readline", false);
4093 } else {
4094 if (default_serial)
4095 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4096 if (default_parallel)
4097 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4098 if (default_monitor)
4099 monitor_parse("vc:80Cx24C", "readline", false);
4102 #if defined(CONFIG_VNC)
4103 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4104 display_remote++;
4106 #endif
4107 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4108 if (!qemu_display_find_default(&dpy)) {
4109 dpy.type = DISPLAY_TYPE_NONE;
4110 #if defined(CONFIG_VNC)
4111 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4112 #endif
4115 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4116 dpy.type = DISPLAY_TYPE_NONE;
4119 if ((alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4120 error_report("-alt-grab and -ctrl-grab are only valid "
4121 "for SDL, ignoring option");
4123 if (dpy.has_window_close &&
4124 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4125 error_report("-no-quit is only valid for GTK and SDL, "
4126 "ignoring option");
4129 qemu_display_early_init(&dpy);
4130 qemu_console_early_init();
4132 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4133 #if defined(CONFIG_OPENGL)
4134 error_report("OpenGL is not supported by the display");
4135 #else
4136 error_report("OpenGL support is disabled");
4137 #endif
4138 exit(1);
4141 page_size_init();
4142 socket_init();
4144 qemu_opts_foreach(qemu_find_opts("object"),
4145 user_creatable_add_opts_foreach,
4146 object_create_initial, &error_fatal);
4148 qemu_opts_foreach(qemu_find_opts("chardev"),
4149 chardev_init_func, NULL, &error_fatal);
4150 /* now chardevs have been created we may have semihosting to connect */
4151 qemu_semihosting_connect_chardevs();
4153 #ifdef CONFIG_VIRTFS
4154 qemu_opts_foreach(qemu_find_opts("fsdev"),
4155 fsdev_init_func, NULL, &error_fatal);
4156 #endif
4158 if (qemu_opts_foreach(qemu_find_opts("device"),
4159 device_help_func, NULL, NULL)) {
4160 exit(0);
4164 * Note: we need to create block backends before
4165 * machine_set_property(), so machine properties can refer to
4166 * them.
4168 configure_blockdev(&bdo_queue, machine_class, snapshot);
4170 machine_opts = qemu_get_machine_opts();
4171 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4172 &error_fatal);
4173 current_machine->ram_size = ram_size;
4174 current_machine->maxram_size = maxram_size;
4175 current_machine->ram_slots = ram_slots;
4178 * Note: uses machine properties such as kernel-irqchip, must run
4179 * after machine_set_property().
4181 configure_accelerator(current_machine, argv[0]);
4184 * Beware, QOM objects created before this point miss global and
4185 * compat properties.
4187 * Global properties get set up by qdev_prop_register_global(),
4188 * called from user_register_global_props(), and certain option
4189 * desugaring. Also in CPU feature desugaring (buried in
4190 * parse_cpu_option()), which happens below this point, but may
4191 * only target the CPU type, which can only be created after
4192 * parse_cpu_option() returned the type.
4194 * Machine compat properties: object_set_machine_compat_props().
4195 * Accelerator compat props: object_set_accelerator_compat_props(),
4196 * called from configure_accelerator().
4199 if (!qtest_enabled() && machine_class->deprecation_reason) {
4200 error_report("Machine type '%s' is deprecated: %s",
4201 machine_class->name, machine_class->deprecation_reason);
4205 * Note: creates a QOM object, must run only after global and
4206 * compat properties have been set up.
4208 migration_object_init();
4210 if (qtest_chrdev) {
4211 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
4214 machine_opts = qemu_get_machine_opts();
4215 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4216 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4217 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4218 bios_name = qemu_opt_get(machine_opts, "firmware");
4220 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4221 if (opts) {
4222 boot_order = qemu_opt_get(opts, "order");
4223 if (boot_order) {
4224 validate_bootdevices(boot_order, &error_fatal);
4227 boot_once = qemu_opt_get(opts, "once");
4228 if (boot_once) {
4229 validate_bootdevices(boot_once, &error_fatal);
4232 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4233 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4236 if (!boot_order) {
4237 boot_order = machine_class->default_boot_order;
4240 if (!kernel_cmdline) {
4241 kernel_cmdline = "";
4242 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4245 linux_boot = (kernel_filename != NULL);
4247 if (!linux_boot && *kernel_cmdline != '\0') {
4248 error_report("-append only allowed with -kernel option");
4249 exit(1);
4252 if (!linux_boot && initrd_filename != NULL) {
4253 error_report("-initrd only allowed with -kernel option");
4254 exit(1);
4257 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4258 /* fall back to the -kernel/-append */
4259 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4262 /* spice needs the timers to be initialized by this point */
4263 qemu_spice_init();
4265 cpu_ticks_init();
4266 if (icount_opts) {
4267 if (!tcg_enabled()) {
4268 error_report("-icount is not allowed with hardware virtualization");
4269 exit(1);
4271 configure_icount(icount_opts, &error_abort);
4272 qemu_opts_del(icount_opts);
4275 if (tcg_enabled()) {
4276 qemu_tcg_configure(accel_opts, &error_fatal);
4279 if (default_net) {
4280 QemuOptsList *net = qemu_find_opts("net");
4281 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4282 #ifdef CONFIG_SLIRP
4283 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4284 #endif
4287 if (net_init_clients(&err) < 0) {
4288 error_report_err(err);
4289 exit(1);
4292 qemu_opts_foreach(qemu_find_opts("object"),
4293 user_creatable_add_opts_foreach,
4294 object_create_delayed, &error_fatal);
4296 tpm_init();
4298 /* init the bluetooth world */
4299 if (foreach_device_config(DEV_BT, bt_parse))
4300 exit(1);
4302 if (!xen_enabled()) {
4303 /* On 32-bit hosts, QEMU is limited by virtual address space */
4304 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4305 error_report("at most 2047 MB RAM can be simulated");
4306 exit(1);
4310 blk_mig_init();
4311 ram_mig_init();
4312 dirty_bitmap_mig_init();
4314 qemu_opts_foreach(qemu_find_opts("mon"),
4315 mon_init_func, NULL, &error_fatal);
4317 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4318 exit(1);
4319 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4320 exit(1);
4321 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4322 exit(1);
4324 /* If no default VGA is requested, the default is "none". */
4325 if (default_vga) {
4326 vga_model = get_default_vga_model(machine_class);
4328 if (vga_model) {
4329 select_vgahw(machine_class, vga_model);
4332 if (watchdog) {
4333 i = select_watchdog(watchdog);
4334 if (i > 0)
4335 exit (i == 1 ? 1 : 0);
4338 /* This checkpoint is required by replay to separate prior clock
4339 reading from the other reads, because timer polling functions query
4340 clock values from the log. */
4341 replay_checkpoint(CHECKPOINT_INIT);
4342 qdev_machine_init();
4344 current_machine->boot_order = boot_order;
4346 /* parse features once if machine provides default cpu_type */
4347 current_machine->cpu_type = machine_class->default_cpu_type;
4348 if (cpu_option) {
4349 current_machine->cpu_type = parse_cpu_option(cpu_option);
4351 parse_numa_opts(current_machine);
4353 /* do monitor/qmp handling at preconfig state if requested */
4354 main_loop();
4356 audio_init_audiodevs();
4358 /* from here on runstate is RUN_STATE_PRELAUNCH */
4359 machine_run_board_init(current_machine);
4361 realtime_init();
4363 soundhw_init();
4365 if (hax_enabled()) {
4366 hax_sync_vcpus();
4369 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4370 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4372 /* init USB devices */
4373 if (machine_usb(current_machine)) {
4374 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4375 exit(1);
4378 /* Check if IGD GFX passthrough. */
4379 igd_gfx_passthru();
4381 /* init generic devices */
4382 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4383 qemu_opts_foreach(qemu_find_opts("device"),
4384 device_init_func, NULL, &error_fatal);
4386 cpu_synchronize_all_post_init();
4388 rom_reset_order_override();
4390 /* Did we create any drives that we failed to create a device for? */
4391 drive_check_orphaned();
4393 /* Don't warn about the default network setup that you get if
4394 * no command line -net or -netdev options are specified. There
4395 * are two cases that we would otherwise complain about:
4396 * (1) board doesn't support a NIC but the implicit "-net nic"
4397 * requested one
4398 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4399 * sets up a nic that isn't connected to anything.
4401 if (!default_net && (!qtest_enabled() || has_defaults)) {
4402 net_check_clients();
4405 if (boot_once) {
4406 qemu_boot_set(boot_once, &error_fatal);
4407 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4410 /* init local displays */
4411 ds = init_displaystate();
4412 qemu_display_init(ds, &dpy);
4414 /* must be after terminal init, SDL library changes signal handlers */
4415 os_setup_signal_handling();
4417 /* init remote displays */
4418 #ifdef CONFIG_VNC
4419 qemu_opts_foreach(qemu_find_opts("vnc"),
4420 vnc_init_func, NULL, &error_fatal);
4421 #endif
4423 if (using_spice) {
4424 qemu_spice_display_init();
4427 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4428 exit(1);
4431 qdev_machine_creation_done();
4433 /* TODO: once all bus devices are qdevified, this should be done
4434 * when bus is created by qdev.c */
4435 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4436 qemu_run_machine_init_done_notifiers();
4438 if (rom_check_and_register_reset() != 0) {
4439 error_report("rom check and register reset failed");
4440 exit(1);
4443 replay_start();
4445 /* This checkpoint is required by replay to separate prior clock
4446 reading from the other reads, because timer polling functions query
4447 clock values from the log. */
4448 replay_checkpoint(CHECKPOINT_RESET);
4449 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4450 register_global_state();
4451 if (loadvm) {
4452 Error *local_err = NULL;
4453 if (load_snapshot(loadvm, &local_err) < 0) {
4454 error_report_err(local_err);
4455 autostart = 0;
4456 exit(1);
4459 if (replay_mode != REPLAY_MODE_NONE) {
4460 replay_vmstate_init();
4463 qdev_prop_check_globals();
4464 if (vmstate_dump_file) {
4465 /* dump and exit */
4466 dump_vmstate_json_to_file(vmstate_dump_file);
4467 return 0;
4470 if (incoming) {
4471 Error *local_err = NULL;
4472 qemu_start_incoming_migration(incoming, &local_err);
4473 if (local_err) {
4474 error_reportf_err(local_err, "-incoming %s: ", incoming);
4475 exit(1);
4477 } else if (autostart) {
4478 vm_start();
4481 accel_setup_post(current_machine);
4482 os_setup_post();
4484 main_loop();
4486 gdbserver_cleanup();
4489 * cleaning up the migration object cancels any existing migration
4490 * try to do this early so that it also stops using devices.
4492 migration_shutdown();
4495 * We must cancel all block jobs while the block layer is drained,
4496 * or cancelling will be affected by throttling and thus may block
4497 * for an extended period of time.
4498 * vm_shutdown() will bdrv_drain_all(), so we may as well include
4499 * it in the drained section.
4500 * We do not need to end this section, because we do not want any
4501 * requests happening from here on anyway.
4503 bdrv_drain_all_begin();
4505 /* No more vcpu or device emulation activity beyond this point */
4506 vm_shutdown();
4507 replay_finish();
4509 job_cancel_sync_all();
4510 bdrv_close_all();
4512 res_free();
4514 /* vhost-user must be cleaned up before chardevs. */
4515 tpm_cleanup();
4516 net_cleanup();
4517 audio_cleanup();
4518 monitor_cleanup();
4519 qemu_chr_cleanup();
4520 user_creatable_cleanup();
4521 /* TODO: unref root container, check all devices are ok */
4523 return 0;