Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blobf7eab0aa1b2c00876ea233918f993bc7741856ea
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "qemu-version.h"
29 #include "qemu/cutils.h"
30 #include "qemu/help_option.h"
31 #include "qemu/uuid.h"
32 #include "sysemu/seccomp.h"
34 #ifdef CONFIG_SDL
35 #if defined(__APPLE__) || defined(main)
36 #define SDL_MAIN_HANDLED
37 #include <SDL.h>
38 #if SDL_MAJOR_VERSION == 1
39 int qemu_main(int argc, char **argv);
40 int main(int argc, char **argv)
42 return qemu_main(argc, argv);
44 #undef main
45 #define main qemu_main
46 #elif SDL_MAJOR_VERSION == 2
47 #undef main
48 #else
49 #error undefined SDL version
50 #endif
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
60 #include "qemu/error-report.h"
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/isa/isa.h"
67 #include "hw/scsi/scsi.h"
68 #include "hw/display/vga.h"
69 #include "hw/bt.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/firmware/smbios.h"
72 #include "hw/acpi/acpi.h"
73 #include "hw/xen/xen.h"
74 #include "hw/qdev.h"
75 #include "hw/loader.h"
76 #include "monitor/qdev.h"
77 #include "sysemu/bt.h"
78 #include "net/net.h"
79 #include "net/slirp.h"
80 #include "monitor/monitor.h"
81 #include "ui/console.h"
82 #include "ui/input.h"
83 #include "sysemu/sysemu.h"
84 #include "sysemu/numa.h"
85 #include "exec/gdbstub.h"
86 #include "qemu/timer.h"
87 #include "chardev/char.h"
88 #include "qemu/bitmap.h"
89 #include "qemu/log.h"
90 #include "sysemu/blockdev.h"
91 #include "hw/block/block.h"
92 #include "migration/misc.h"
93 #include "migration/snapshot.h"
94 #include "migration/global_state.h"
95 #include "sysemu/tpm.h"
96 #include "sysemu/dma.h"
97 #include "hw/audio/soundhw.h"
98 #include "audio/audio.h"
99 #include "sysemu/cpus.h"
100 #include "migration/colo.h"
101 #include "migration/postcopy-ram.h"
102 #include "sysemu/kvm.h"
103 #include "sysemu/hax.h"
104 #include "qapi/qobject-input-visitor.h"
105 #include "qemu/option.h"
106 #include "qemu/config-file.h"
107 #include "qemu-options.h"
108 #include "qemu/main-loop.h"
109 #ifdef CONFIG_VIRTFS
110 #include "fsdev/qemu-fsdev.h"
111 #endif
112 #include "sysemu/qtest.h"
114 #include "disas/disas.h"
117 #include "slirp/libslirp.h"
119 #include "trace-root.h"
120 #include "trace/control.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 "exec/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-misc.h"
137 #include "qapi/qapi-commands-run-state.h"
138 #include "qapi/qapi-commands-ui.h"
139 #include "qapi/qmp/qerror.h"
140 #include "sysemu/iothread.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 int no_frame;
171 static int num_serial_hds;
172 static Chardev **serial_hds;
173 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
174 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
175 int win2k_install_hack = 0;
176 int singlestep = 0;
177 int smp_cpus;
178 unsigned int max_cpus;
179 int smp_cores = 1;
180 int smp_threads = 1;
181 int acpi_enabled = 1;
182 int no_hpet = 0;
183 int fd_bootchk = 1;
184 static int no_reboot;
185 int no_shutdown = 0;
186 int cursor_hide = 1;
187 int graphic_rotate = 0;
188 const char *watchdog;
189 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
190 int nb_option_roms;
191 int old_param = 0;
192 const char *qemu_name;
193 int alt_grab = 0;
194 int ctrl_grab = 0;
195 unsigned int nb_prom_envs = 0;
196 const char *prom_envs[MAX_PROM_ENVS];
197 int boot_menu;
198 bool boot_strict;
199 uint8_t *boot_splash_filedata;
200 size_t boot_splash_filedata_size;
201 uint8_t qemu_extra_params_fw[2];
202 bool wakeup_suspend_enabled;
204 int icount_align_option;
206 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
207 * little-endian "wire format" described in the SMBIOS 2.6 specification.
209 QemuUUID qemu_uuid;
210 bool qemu_uuid_set;
212 /* Trace unassigned memory or i/o accesses. */
213 bool trace_unassigned;
215 static NotifierList exit_notifiers =
216 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
218 static NotifierList machine_init_done_notifiers =
219 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
221 bool xen_allowed;
222 uint32_t xen_domid;
223 enum xen_mode xen_mode = XEN_EMULATE;
224 bool xen_domid_restrict;
226 static int has_defaults = 1;
227 static int default_serial = 1;
228 static int default_parallel = 1;
229 static int default_virtcon = 1;
230 static int default_monitor = 1;
231 static int default_floppy = 1;
232 static int default_cdrom = 1;
233 static int default_sdcard = 1;
234 static int default_vga = 1;
235 static int default_net = 1;
237 static struct {
238 const char *driver;
239 int *flag;
240 } default_list[] = {
241 { .driver = "isa-serial", .flag = &default_serial },
242 { .driver = "isa-parallel", .flag = &default_parallel },
243 { .driver = "isa-fdc", .flag = &default_floppy },
244 { .driver = "floppy", .flag = &default_floppy },
245 { .driver = "ide-cd", .flag = &default_cdrom },
246 { .driver = "ide-hd", .flag = &default_cdrom },
247 { .driver = "ide-drive", .flag = &default_cdrom },
248 { .driver = "scsi-cd", .flag = &default_cdrom },
249 { .driver = "scsi-hd", .flag = &default_cdrom },
250 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
251 { .driver = "virtio-serial", .flag = &default_virtcon },
252 { .driver = "VGA", .flag = &default_vga },
253 { .driver = "isa-vga", .flag = &default_vga },
254 { .driver = "cirrus-vga", .flag = &default_vga },
255 { .driver = "isa-cirrus-vga", .flag = &default_vga },
256 { .driver = "vmware-svga", .flag = &default_vga },
257 { .driver = "qxl-vga", .flag = &default_vga },
258 { .driver = "virtio-vga", .flag = &default_vga },
261 static QemuOptsList qemu_rtc_opts = {
262 .name = "rtc",
263 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
264 .merge_lists = true,
265 .desc = {
267 .name = "base",
268 .type = QEMU_OPT_STRING,
270 .name = "clock",
271 .type = QEMU_OPT_STRING,
273 .name = "driftfix",
274 .type = QEMU_OPT_STRING,
276 { /* end of list */ }
280 static QemuOptsList qemu_option_rom_opts = {
281 .name = "option-rom",
282 .implied_opt_name = "romfile",
283 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
284 .desc = {
286 .name = "bootindex",
287 .type = QEMU_OPT_NUMBER,
288 }, {
289 .name = "romfile",
290 .type = QEMU_OPT_STRING,
292 { /* end of list */ }
296 static QemuOptsList qemu_machine_opts = {
297 .name = "machine",
298 .implied_opt_name = "type",
299 .merge_lists = true,
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
301 .desc = {
303 * no elements => accept any
304 * sanity checking will happen later
305 * when setting machine properties
311 static QemuOptsList qemu_accel_opts = {
312 .name = "accel",
313 .implied_opt_name = "accel",
314 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
315 .merge_lists = true,
316 .desc = {
318 .name = "accel",
319 .type = QEMU_OPT_STRING,
320 .help = "Select the type of accelerator",
323 .name = "thread",
324 .type = QEMU_OPT_STRING,
325 .help = "Enable/disable multi-threaded TCG",
327 { /* end of list */ }
331 static QemuOptsList qemu_boot_opts = {
332 .name = "boot-opts",
333 .implied_opt_name = "order",
334 .merge_lists = true,
335 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
336 .desc = {
338 .name = "order",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "once",
342 .type = QEMU_OPT_STRING,
343 }, {
344 .name = "menu",
345 .type = QEMU_OPT_BOOL,
346 }, {
347 .name = "splash",
348 .type = QEMU_OPT_STRING,
349 }, {
350 .name = "splash-time",
351 .type = QEMU_OPT_STRING,
352 }, {
353 .name = "reboot-timeout",
354 .type = QEMU_OPT_STRING,
355 }, {
356 .name = "strict",
357 .type = QEMU_OPT_BOOL,
359 { /*End of list */ }
363 static QemuOptsList qemu_add_fd_opts = {
364 .name = "add-fd",
365 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
366 .desc = {
368 .name = "fd",
369 .type = QEMU_OPT_NUMBER,
370 .help = "file descriptor of which a duplicate is added to fd set",
372 .name = "set",
373 .type = QEMU_OPT_NUMBER,
374 .help = "ID of the fd set to add fd to",
376 .name = "opaque",
377 .type = QEMU_OPT_STRING,
378 .help = "free-form string used to describe fd",
380 { /* end of list */ }
384 static QemuOptsList qemu_object_opts = {
385 .name = "object",
386 .implied_opt_name = "qom-type",
387 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
388 .desc = {
393 static QemuOptsList qemu_tpmdev_opts = {
394 .name = "tpmdev",
395 .implied_opt_name = "type",
396 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
397 .desc = {
398 /* options are defined in the TPM backends */
399 { /* end of list */ }
403 static QemuOptsList qemu_realtime_opts = {
404 .name = "realtime",
405 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
406 .desc = {
408 .name = "mlock",
409 .type = QEMU_OPT_BOOL,
411 { /* end of list */ }
415 static QemuOptsList qemu_overcommit_opts = {
416 .name = "overcommit",
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
418 .desc = {
420 .name = "mem-lock",
421 .type = QEMU_OPT_BOOL,
424 .name = "cpu-pm",
425 .type = QEMU_OPT_BOOL,
427 { /* end of list */ }
431 static QemuOptsList qemu_msg_opts = {
432 .name = "msg",
433 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
434 .desc = {
436 .name = "timestamp",
437 .type = QEMU_OPT_BOOL,
439 { /* end of list */ }
443 static QemuOptsList qemu_name_opts = {
444 .name = "name",
445 .implied_opt_name = "guest",
446 .merge_lists = true,
447 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
448 .desc = {
450 .name = "guest",
451 .type = QEMU_OPT_STRING,
452 .help = "Sets the name of the guest.\n"
453 "This name will be displayed in the SDL window caption.\n"
454 "The name will also be used for the VNC server",
455 }, {
456 .name = "process",
457 .type = QEMU_OPT_STRING,
458 .help = "Sets the name of the QEMU process, as shown in top etc",
459 }, {
460 .name = "debug-threads",
461 .type = QEMU_OPT_BOOL,
462 .help = "When enabled, name the individual threads; defaults off.\n"
463 "NOTE: The thread names are for debugging and not a\n"
464 "stable API.",
466 { /* End of list */ }
470 static QemuOptsList qemu_mem_opts = {
471 .name = "memory",
472 .implied_opt_name = "size",
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
474 .merge_lists = true,
475 .desc = {
477 .name = "size",
478 .type = QEMU_OPT_SIZE,
481 .name = "slots",
482 .type = QEMU_OPT_NUMBER,
485 .name = "maxmem",
486 .type = QEMU_OPT_SIZE,
488 { /* end of list */ }
492 static QemuOptsList qemu_icount_opts = {
493 .name = "icount",
494 .implied_opt_name = "shift",
495 .merge_lists = true,
496 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
497 .desc = {
499 .name = "shift",
500 .type = QEMU_OPT_STRING,
501 }, {
502 .name = "align",
503 .type = QEMU_OPT_BOOL,
504 }, {
505 .name = "sleep",
506 .type = QEMU_OPT_BOOL,
507 }, {
508 .name = "rr",
509 .type = QEMU_OPT_STRING,
510 }, {
511 .name = "rrfile",
512 .type = QEMU_OPT_STRING,
513 }, {
514 .name = "rrsnapshot",
515 .type = QEMU_OPT_STRING,
517 { /* end of list */ }
521 static QemuOptsList qemu_semihosting_config_opts = {
522 .name = "semihosting-config",
523 .implied_opt_name = "enable",
524 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
525 .desc = {
527 .name = "enable",
528 .type = QEMU_OPT_BOOL,
529 }, {
530 .name = "target",
531 .type = QEMU_OPT_STRING,
532 }, {
533 .name = "arg",
534 .type = QEMU_OPT_STRING,
536 { /* end of list */ }
540 static QemuOptsList qemu_fw_cfg_opts = {
541 .name = "fw_cfg",
542 .implied_opt_name = "name",
543 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
544 .desc = {
546 .name = "name",
547 .type = QEMU_OPT_STRING,
548 .help = "Sets the fw_cfg name of the blob to be inserted",
549 }, {
550 .name = "file",
551 .type = QEMU_OPT_STRING,
552 .help = "Sets the name of the file from which "
553 "the fw_cfg blob will be loaded",
554 }, {
555 .name = "string",
556 .type = QEMU_OPT_STRING,
557 .help = "Sets content of the blob to be inserted from a string",
559 { /* end of list */ }
564 * Get machine options
566 * Returns: machine options (never null).
568 QemuOpts *qemu_get_machine_opts(void)
570 return qemu_find_opts_singleton("machine");
573 const char *qemu_get_vm_name(void)
575 return qemu_name;
578 static void res_free(void)
580 g_free(boot_splash_filedata);
581 boot_splash_filedata = NULL;
584 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
586 const char *driver = qemu_opt_get(opts, "driver");
587 int i;
589 if (!driver)
590 return 0;
591 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
592 if (strcmp(default_list[i].driver, driver) != 0)
593 continue;
594 *(default_list[i].flag) = 0;
596 return 0;
599 /***********************************************************/
600 /* QEMU state */
602 static RunState current_run_state = RUN_STATE_PRECONFIG;
604 /* We use RUN_STATE__MAX but any invalid value will do */
605 static RunState vmstop_requested = RUN_STATE__MAX;
606 static QemuMutex vmstop_lock;
608 typedef struct {
609 RunState from;
610 RunState to;
611 } RunStateTransition;
613 static const RunStateTransition runstate_transitions_def[] = {
614 /* from -> to */
615 { RUN_STATE_PRECONFIG, RUN_STATE_PRELAUNCH },
616 /* Early switch to inmigrate state to allow -incoming CLI option work
617 * as it used to. TODO: delay actual switching to inmigrate state to
618 * the point after machine is built and remove this hack.
620 { RUN_STATE_PRECONFIG, RUN_STATE_INMIGRATE },
622 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
623 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
626 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
627 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
628 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
629 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
630 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
631 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
632 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
633 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
634 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
636 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
639 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
640 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
643 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
644 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
647 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
648 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
650 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
651 { RUN_STATE_PAUSED, RUN_STATE_COLO},
653 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
654 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
655 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
657 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
658 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
663 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
664 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
665 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
667 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
668 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
670 { RUN_STATE_COLO, RUN_STATE_RUNNING },
672 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
673 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
674 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
675 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
676 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
678 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
679 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
680 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
681 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
682 { RUN_STATE_RUNNING, RUN_STATE_COLO},
684 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
686 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
687 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
688 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
690 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
691 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
692 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
693 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
694 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
695 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
697 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
698 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
699 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
700 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
702 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
703 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
704 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
706 { RUN_STATE__MAX, RUN_STATE__MAX },
709 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
711 bool runstate_check(RunState state)
713 return current_run_state == state;
716 bool runstate_store(char *str, size_t size)
718 const char *state = RunState_str(current_run_state);
719 size_t len = strlen(state) + 1;
721 if (len > size) {
722 return false;
724 memcpy(str, state, len);
725 return true;
728 static void runstate_init(void)
730 const RunStateTransition *p;
732 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
733 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
734 runstate_valid_transitions[p->from][p->to] = true;
737 qemu_mutex_init(&vmstop_lock);
740 /* This function will abort() on invalid state transitions */
741 void runstate_set(RunState new_state)
743 assert(new_state < RUN_STATE__MAX);
745 if (current_run_state == new_state) {
746 return;
749 if (!runstate_valid_transitions[current_run_state][new_state]) {
750 error_report("invalid runstate transition: '%s' -> '%s'",
751 RunState_str(current_run_state),
752 RunState_str(new_state));
753 abort();
755 trace_runstate_set(new_state);
756 current_run_state = new_state;
759 int runstate_is_running(void)
761 return runstate_check(RUN_STATE_RUNNING);
764 bool runstate_needs_reset(void)
766 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
767 runstate_check(RUN_STATE_SHUTDOWN);
770 StatusInfo *qmp_query_status(Error **errp)
772 StatusInfo *info = g_malloc0(sizeof(*info));
774 info->running = runstate_is_running();
775 info->singlestep = singlestep;
776 info->status = current_run_state;
778 return info;
781 bool qemu_vmstop_requested(RunState *r)
783 qemu_mutex_lock(&vmstop_lock);
784 *r = vmstop_requested;
785 vmstop_requested = RUN_STATE__MAX;
786 qemu_mutex_unlock(&vmstop_lock);
787 return *r < RUN_STATE__MAX;
790 void qemu_system_vmstop_request_prepare(void)
792 qemu_mutex_lock(&vmstop_lock);
795 void qemu_system_vmstop_request(RunState state)
797 vmstop_requested = state;
798 qemu_mutex_unlock(&vmstop_lock);
799 qemu_notify_event();
802 /***********************************************************/
803 /* RTC reference time/date access */
804 static time_t qemu_ref_timedate(QEMUClockType clock)
806 time_t value = qemu_clock_get_ms(clock) / 1000;
807 switch (clock) {
808 case QEMU_CLOCK_REALTIME:
809 value -= rtc_realtime_clock_offset;
810 /* no break */
811 case QEMU_CLOCK_VIRTUAL:
812 value += rtc_ref_start_datetime;
813 break;
814 case QEMU_CLOCK_HOST:
815 if (rtc_base_type == RTC_BASE_DATETIME) {
816 value -= rtc_host_datetime_offset;
818 break;
819 default:
820 assert(0);
822 return value;
825 void qemu_get_timedate(struct tm *tm, int offset)
827 time_t ti = qemu_ref_timedate(rtc_clock);
829 ti += offset;
831 switch (rtc_base_type) {
832 case RTC_BASE_DATETIME:
833 case RTC_BASE_UTC:
834 gmtime_r(&ti, tm);
835 break;
836 case RTC_BASE_LOCALTIME:
837 localtime_r(&ti, tm);
838 break;
842 int qemu_timedate_diff(struct tm *tm)
844 time_t seconds;
846 switch (rtc_base_type) {
847 case RTC_BASE_DATETIME:
848 case RTC_BASE_UTC:
849 seconds = mktimegm(tm);
850 break;
851 case RTC_BASE_LOCALTIME:
853 struct tm tmp = *tm;
854 tmp.tm_isdst = -1; /* use timezone to figure it out */
855 seconds = mktime(&tmp);
856 break;
858 default:
859 abort();
862 return seconds - qemu_ref_timedate(QEMU_CLOCK_HOST);
865 static void configure_rtc_base_datetime(const char *startdate)
867 time_t rtc_start_datetime;
868 struct tm tm;
870 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d", &tm.tm_year, &tm.tm_mon,
871 &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) == 6) {
872 /* OK */
873 } else if (sscanf(startdate, "%d-%d-%d",
874 &tm.tm_year, &tm.tm_mon, &tm.tm_mday) == 3) {
875 tm.tm_hour = 0;
876 tm.tm_min = 0;
877 tm.tm_sec = 0;
878 } else {
879 goto date_fail;
881 tm.tm_year -= 1900;
882 tm.tm_mon--;
883 rtc_start_datetime = mktimegm(&tm);
884 if (rtc_start_datetime == -1) {
885 date_fail:
886 error_report("invalid datetime format");
887 error_printf("valid formats: "
888 "'2006-06-17T16:01:21' or '2006-06-17'\n");
889 exit(1);
891 rtc_host_datetime_offset = rtc_ref_start_datetime - rtc_start_datetime;
892 rtc_ref_start_datetime = rtc_start_datetime;
895 static void configure_rtc(QemuOpts *opts)
897 const char *value;
899 /* Set defaults */
900 rtc_clock = QEMU_CLOCK_HOST;
901 rtc_ref_start_datetime = qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
902 rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;
904 value = qemu_opt_get(opts, "base");
905 if (value) {
906 if (!strcmp(value, "utc")) {
907 rtc_base_type = RTC_BASE_UTC;
908 } else if (!strcmp(value, "localtime")) {
909 Error *blocker = NULL;
910 rtc_base_type = RTC_BASE_LOCALTIME;
911 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
912 "-rtc base=localtime");
913 replay_add_blocker(blocker);
914 } else {
915 rtc_base_type = RTC_BASE_DATETIME;
916 configure_rtc_base_datetime(value);
919 value = qemu_opt_get(opts, "clock");
920 if (value) {
921 if (!strcmp(value, "host")) {
922 rtc_clock = QEMU_CLOCK_HOST;
923 } else if (!strcmp(value, "rt")) {
924 rtc_clock = QEMU_CLOCK_REALTIME;
925 } else if (!strcmp(value, "vm")) {
926 rtc_clock = QEMU_CLOCK_VIRTUAL;
927 } else {
928 error_report("invalid option value '%s'", value);
929 exit(1);
932 value = qemu_opt_get(opts, "driftfix");
933 if (value) {
934 if (!strcmp(value, "slew")) {
935 static GlobalProperty slew_lost_ticks = {
936 .driver = "mc146818rtc",
937 .property = "lost_tick_policy",
938 .value = "slew",
941 qdev_prop_register_global(&slew_lost_ticks);
942 } else if (!strcmp(value, "none")) {
943 /* discard is default */
944 } else {
945 error_report("invalid option value '%s'", value);
946 exit(1);
951 /***********************************************************/
952 /* Bluetooth support */
953 static int nb_hcis;
954 static int cur_hci;
955 static struct HCIInfo *hci_table[MAX_NICS];
957 struct HCIInfo *qemu_next_hci(void)
959 if (cur_hci == nb_hcis)
960 return &null_hci;
962 return hci_table[cur_hci++];
965 static int bt_hci_parse(const char *str)
967 struct HCIInfo *hci;
968 bdaddr_t bdaddr;
970 if (nb_hcis >= MAX_NICS) {
971 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
972 return -1;
975 hci = hci_init(str);
976 if (!hci)
977 return -1;
979 bdaddr.b[0] = 0x52;
980 bdaddr.b[1] = 0x54;
981 bdaddr.b[2] = 0x00;
982 bdaddr.b[3] = 0x12;
983 bdaddr.b[4] = 0x34;
984 bdaddr.b[5] = 0x56 + nb_hcis;
985 hci->bdaddr_set(hci, bdaddr.b);
987 hci_table[nb_hcis++] = hci;
989 return 0;
992 static void bt_vhci_add(int vlan_id)
994 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
996 if (!vlan->slave)
997 warn_report("adding a VHCI to an empty scatternet %i",
998 vlan_id);
1000 bt_vhci_init(bt_new_hci(vlan));
1003 static struct bt_device_s *bt_device_add(const char *opt)
1005 struct bt_scatternet_s *vlan;
1006 int vlan_id = 0;
1007 char *endp = strstr(opt, ",vlan=");
1008 int len = (endp ? endp - opt : strlen(opt)) + 1;
1009 char devname[10];
1011 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1013 if (endp) {
1014 vlan_id = strtol(endp + 6, &endp, 0);
1015 if (*endp) {
1016 error_report("unrecognised bluetooth vlan Id");
1017 return 0;
1021 vlan = qemu_find_bt_vlan(vlan_id);
1023 if (!vlan->slave)
1024 warn_report("adding a slave device to an empty scatternet %i",
1025 vlan_id);
1027 if (!strcmp(devname, "keyboard"))
1028 return bt_keyboard_init(vlan);
1030 error_report("unsupported bluetooth device '%s'", devname);
1031 return 0;
1034 static int bt_parse(const char *opt)
1036 const char *endp, *p;
1037 int vlan;
1039 if (strstart(opt, "hci", &endp)) {
1040 if (!*endp || *endp == ',') {
1041 if (*endp)
1042 if (!strstart(endp, ",vlan=", 0))
1043 opt = endp + 1;
1045 return bt_hci_parse(opt);
1047 } else if (strstart(opt, "vhci", &endp)) {
1048 if (!*endp || *endp == ',') {
1049 if (*endp) {
1050 if (strstart(endp, ",vlan=", &p)) {
1051 vlan = strtol(p, (char **) &endp, 0);
1052 if (*endp) {
1053 error_report("bad scatternet '%s'", p);
1054 return 1;
1056 } else {
1057 error_report("bad parameter '%s'", endp + 1);
1058 return 1;
1060 } else
1061 vlan = 0;
1063 bt_vhci_add(vlan);
1064 return 0;
1066 } else if (strstart(opt, "device:", &endp))
1067 return !bt_device_add(endp);
1069 error_report("bad bluetooth parameter '%s'", opt);
1070 return 1;
1073 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1075 const char *proc_name;
1077 if (qemu_opt_get(opts, "debug-threads")) {
1078 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1080 qemu_name = qemu_opt_get(opts, "guest");
1082 proc_name = qemu_opt_get(opts, "process");
1083 if (proc_name) {
1084 os_set_proc_name(proc_name);
1087 return 0;
1090 bool defaults_enabled(void)
1092 return has_defaults;
1095 #ifndef _WIN32
1096 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1098 int fd, dupfd, flags;
1099 int64_t fdset_id;
1100 const char *fd_opaque = NULL;
1101 AddfdInfo *fdinfo;
1103 fd = qemu_opt_get_number(opts, "fd", -1);
1104 fdset_id = qemu_opt_get_number(opts, "set", -1);
1105 fd_opaque = qemu_opt_get(opts, "opaque");
1107 if (fd < 0) {
1108 error_setg(errp, "fd option is required and must be non-negative");
1109 return -1;
1112 if (fd <= STDERR_FILENO) {
1113 error_setg(errp, "fd cannot be a standard I/O stream");
1114 return -1;
1118 * All fds inherited across exec() necessarily have FD_CLOEXEC
1119 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1121 flags = fcntl(fd, F_GETFD);
1122 if (flags == -1 || (flags & FD_CLOEXEC)) {
1123 error_setg(errp, "fd is not valid or already in use");
1124 return -1;
1127 if (fdset_id < 0) {
1128 error_setg(errp, "set option is required and must be non-negative");
1129 return -1;
1132 #ifdef F_DUPFD_CLOEXEC
1133 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1134 #else
1135 dupfd = dup(fd);
1136 if (dupfd != -1) {
1137 qemu_set_cloexec(dupfd);
1139 #endif
1140 if (dupfd == -1) {
1141 error_setg(errp, "error duplicating fd: %s", strerror(errno));
1142 return -1;
1145 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1146 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1147 &error_abort);
1148 g_free(fdinfo);
1150 return 0;
1153 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1155 int fd;
1157 fd = qemu_opt_get_number(opts, "fd", -1);
1158 close(fd);
1160 return 0;
1162 #endif
1164 /***********************************************************/
1165 /* QEMU Block devices */
1167 #define HD_OPTS "media=disk"
1168 #define CDROM_OPTS "media=cdrom"
1169 #define FD_OPTS ""
1170 #define PFLASH_OPTS ""
1171 #define MTD_OPTS ""
1172 #define SD_OPTS ""
1174 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1176 BlockInterfaceType *block_default_type = opaque;
1178 return drive_new(opts, *block_default_type, errp) == NULL;
1181 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1183 if (qemu_opt_get(opts, "snapshot") == NULL) {
1184 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1186 return 0;
1189 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1190 int index, const char *optstr)
1192 QemuOpts *opts;
1193 DriveInfo *dinfo;
1195 if (!enable || drive_get_by_index(type, index)) {
1196 return;
1199 opts = drive_add(type, index, NULL, optstr);
1200 if (snapshot) {
1201 drive_enable_snapshot(NULL, opts, NULL);
1204 dinfo = drive_new(opts, type, &error_abort);
1205 dinfo->is_default = true;
1209 static QemuOptsList qemu_smp_opts = {
1210 .name = "smp-opts",
1211 .implied_opt_name = "cpus",
1212 .merge_lists = true,
1213 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1214 .desc = {
1216 .name = "cpus",
1217 .type = QEMU_OPT_NUMBER,
1218 }, {
1219 .name = "sockets",
1220 .type = QEMU_OPT_NUMBER,
1221 }, {
1222 .name = "cores",
1223 .type = QEMU_OPT_NUMBER,
1224 }, {
1225 .name = "threads",
1226 .type = QEMU_OPT_NUMBER,
1227 }, {
1228 .name = "maxcpus",
1229 .type = QEMU_OPT_NUMBER,
1231 { /*End of list */ }
1235 static void smp_parse(QemuOpts *opts)
1237 if (opts) {
1238 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1239 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1240 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1241 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1243 /* compute missing values, prefer sockets over cores over threads */
1244 if (cpus == 0 || sockets == 0) {
1245 cores = cores > 0 ? cores : 1;
1246 threads = threads > 0 ? threads : 1;
1247 if (cpus == 0) {
1248 sockets = sockets > 0 ? sockets : 1;
1249 cpus = cores * threads * sockets;
1250 } else {
1251 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1252 sockets = max_cpus / (cores * threads);
1254 } else if (cores == 0) {
1255 threads = threads > 0 ? threads : 1;
1256 cores = cpus / (sockets * threads);
1257 cores = cores > 0 ? cores : 1;
1258 } else if (threads == 0) {
1259 threads = cpus / (cores * sockets);
1260 threads = threads > 0 ? threads : 1;
1261 } else if (sockets * cores * threads < cpus) {
1262 error_report("cpu topology: "
1263 "sockets (%u) * cores (%u) * threads (%u) < "
1264 "smp_cpus (%u)",
1265 sockets, cores, threads, cpus);
1266 exit(1);
1269 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1271 if (max_cpus < cpus) {
1272 error_report("maxcpus must be equal to or greater than smp");
1273 exit(1);
1276 if (sockets * cores * threads > max_cpus) {
1277 error_report("cpu topology: "
1278 "sockets (%u) * cores (%u) * threads (%u) > "
1279 "maxcpus (%u)",
1280 sockets, cores, threads, max_cpus);
1281 exit(1);
1284 if (sockets * cores * threads != max_cpus) {
1285 warn_report("Invalid CPU topology deprecated: "
1286 "sockets (%u) * cores (%u) * threads (%u) "
1287 "!= maxcpus (%u)",
1288 sockets, cores, threads, max_cpus);
1291 smp_cpus = cpus;
1292 smp_cores = cores;
1293 smp_threads = threads;
1296 if (smp_cpus > 1) {
1297 Error *blocker = NULL;
1298 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1299 replay_add_blocker(blocker);
1303 static void realtime_init(void)
1305 if (enable_mlock) {
1306 if (os_mlock() < 0) {
1307 error_report("locking memory failed");
1308 exit(1);
1314 static void configure_msg(QemuOpts *opts)
1316 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1319 /***********************************************************/
1320 /* Semihosting */
1322 typedef struct SemihostingConfig {
1323 bool enabled;
1324 SemihostingTarget target;
1325 const char **argv;
1326 int argc;
1327 const char *cmdline; /* concatenated argv */
1328 } SemihostingConfig;
1330 static SemihostingConfig semihosting;
1332 bool semihosting_enabled(void)
1334 return semihosting.enabled;
1337 SemihostingTarget semihosting_get_target(void)
1339 return semihosting.target;
1342 const char *semihosting_get_arg(int i)
1344 if (i >= semihosting.argc) {
1345 return NULL;
1347 return semihosting.argv[i];
1350 int semihosting_get_argc(void)
1352 return semihosting.argc;
1355 const char *semihosting_get_cmdline(void)
1357 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1358 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1360 return semihosting.cmdline;
1363 static int add_semihosting_arg(void *opaque,
1364 const char *name, const char *val,
1365 Error **errp)
1367 SemihostingConfig *s = opaque;
1368 if (strcmp(name, "arg") == 0) {
1369 s->argc++;
1370 /* one extra element as g_strjoinv() expects NULL-terminated array */
1371 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1372 s->argv[s->argc - 1] = val;
1373 s->argv[s->argc] = NULL;
1375 return 0;
1378 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1379 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1381 char *cmd_token;
1383 /* argv[0] */
1384 add_semihosting_arg(&semihosting, "arg", file, NULL);
1386 /* split -append and initialize argv[1..n] */
1387 cmd_token = strtok(g_strdup(cmd), " ");
1388 while (cmd_token) {
1389 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1390 cmd_token = strtok(NULL, " ");
1394 /* Now we still need this for compatibility with XEN. */
1395 bool has_igd_gfx_passthru;
1396 static void igd_gfx_passthru(void)
1398 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1401 /***********************************************************/
1402 /* USB devices */
1404 static int usb_device_add(const char *devname)
1406 USBDevice *dev = NULL;
1408 if (!machine_usb(current_machine)) {
1409 return -1;
1412 dev = usbdevice_create(devname);
1413 if (!dev)
1414 return -1;
1416 return 0;
1419 static int usb_parse(const char *cmdline)
1421 int r;
1422 r = usb_device_add(cmdline);
1423 if (r < 0) {
1424 error_report("could not add USB device '%s'", cmdline);
1426 return r;
1429 /***********************************************************/
1430 /* machine registration */
1432 MachineState *current_machine;
1434 static MachineClass *find_machine(const char *name)
1436 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1437 MachineClass *mc = NULL;
1439 for (el = machines; el; el = el->next) {
1440 MachineClass *temp = el->data;
1442 if (!strcmp(temp->name, name)) {
1443 mc = temp;
1444 break;
1446 if (temp->alias &&
1447 !strcmp(temp->alias, name)) {
1448 mc = temp;
1449 break;
1453 g_slist_free(machines);
1454 return mc;
1457 MachineClass *find_default_machine(void)
1459 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1460 MachineClass *mc = NULL;
1462 for (el = machines; el; el = el->next) {
1463 MachineClass *temp = el->data;
1465 if (temp->is_default) {
1466 mc = temp;
1467 break;
1471 g_slist_free(machines);
1472 return mc;
1475 MachineInfoList *qmp_query_machines(Error **errp)
1477 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1478 MachineInfoList *mach_list = NULL;
1480 for (el = machines; el; el = el->next) {
1481 MachineClass *mc = el->data;
1482 MachineInfoList *entry;
1483 MachineInfo *info;
1485 info = g_malloc0(sizeof(*info));
1486 if (mc->is_default) {
1487 info->has_is_default = true;
1488 info->is_default = true;
1491 if (mc->alias) {
1492 info->has_alias = true;
1493 info->alias = g_strdup(mc->alias);
1496 info->name = g_strdup(mc->name);
1497 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1498 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1500 entry = g_malloc0(sizeof(*entry));
1501 entry->value = info;
1502 entry->next = mach_list;
1503 mach_list = entry;
1506 g_slist_free(machines);
1507 return mach_list;
1510 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1512 ObjectProperty *prop;
1513 ObjectPropertyIterator iter;
1515 if (!qemu_opt_has_help_opt(opts)) {
1516 return 0;
1519 object_property_iter_init(&iter, OBJECT(machine));
1520 while ((prop = object_property_iter_next(&iter))) {
1521 if (!prop->set) {
1522 continue;
1525 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1526 prop->name, prop->type);
1527 if (prop->description) {
1528 error_printf(" (%s)\n", prop->description);
1529 } else {
1530 error_printf("\n");
1534 return 1;
1537 struct vm_change_state_entry {
1538 VMChangeStateHandler *cb;
1539 void *opaque;
1540 QLIST_ENTRY (vm_change_state_entry) entries;
1543 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1545 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1546 void *opaque)
1548 VMChangeStateEntry *e;
1550 e = g_malloc0(sizeof (*e));
1552 e->cb = cb;
1553 e->opaque = opaque;
1554 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1555 return e;
1558 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1560 QLIST_REMOVE (e, entries);
1561 g_free (e);
1564 void vm_state_notify(int running, RunState state)
1566 VMChangeStateEntry *e, *next;
1568 trace_vm_state_notify(running, state);
1570 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1571 e->cb(e->opaque, running, state);
1575 static ShutdownCause reset_requested;
1576 static ShutdownCause shutdown_requested;
1577 static int shutdown_signal;
1578 static pid_t shutdown_pid;
1579 static int powerdown_requested;
1580 static int debug_requested;
1581 static int suspend_requested;
1582 static bool preconfig_exit_requested = true;
1583 static WakeupReason wakeup_reason;
1584 static NotifierList powerdown_notifiers =
1585 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1586 static NotifierList suspend_notifiers =
1587 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1588 static NotifierList wakeup_notifiers =
1589 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1590 static NotifierList shutdown_notifiers =
1591 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers);
1592 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1594 ShutdownCause qemu_shutdown_requested_get(void)
1596 return shutdown_requested;
1599 ShutdownCause qemu_reset_requested_get(void)
1601 return reset_requested;
1604 static int qemu_shutdown_requested(void)
1606 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1609 static void qemu_kill_report(void)
1611 if (!qtest_driver() && shutdown_signal) {
1612 if (shutdown_pid == 0) {
1613 /* This happens for eg ^C at the terminal, so it's worth
1614 * avoiding printing an odd message in that case.
1616 error_report("terminating on signal %d", shutdown_signal);
1617 } else {
1618 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1620 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1621 shutdown_signal, shutdown_pid,
1622 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1623 g_free(shutdown_cmd);
1625 shutdown_signal = 0;
1629 static ShutdownCause qemu_reset_requested(void)
1631 ShutdownCause r = reset_requested;
1633 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1634 reset_requested = SHUTDOWN_CAUSE_NONE;
1635 return r;
1637 return SHUTDOWN_CAUSE_NONE;
1640 static int qemu_suspend_requested(void)
1642 int r = suspend_requested;
1643 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1644 suspend_requested = 0;
1645 return r;
1647 return false;
1650 static WakeupReason qemu_wakeup_requested(void)
1652 return wakeup_reason;
1655 static int qemu_powerdown_requested(void)
1657 int r = powerdown_requested;
1658 powerdown_requested = 0;
1659 return r;
1662 static int qemu_debug_requested(void)
1664 int r = debug_requested;
1665 debug_requested = 0;
1666 return r;
1669 void qemu_exit_preconfig_request(void)
1671 preconfig_exit_requested = true;
1675 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1677 void qemu_system_reset(ShutdownCause reason)
1679 MachineClass *mc;
1681 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1683 cpu_synchronize_all_states();
1685 if (mc && mc->reset) {
1686 mc->reset();
1687 } else {
1688 qemu_devices_reset();
1690 if (reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1691 qapi_event_send_reset(shutdown_caused_by_guest(reason), reason);
1693 cpu_synchronize_all_post_reset();
1696 void qemu_system_guest_panicked(GuestPanicInformation *info)
1698 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1700 if (current_cpu) {
1701 current_cpu->crash_occurred = true;
1703 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1704 !!info, info);
1705 vm_stop(RUN_STATE_GUEST_PANICKED);
1706 if (!no_shutdown) {
1707 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1708 !!info, info);
1709 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1712 if (info) {
1713 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1714 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1715 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1716 info->u.hyper_v.arg1,
1717 info->u.hyper_v.arg2,
1718 info->u.hyper_v.arg3,
1719 info->u.hyper_v.arg4,
1720 info->u.hyper_v.arg5);
1721 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1722 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1723 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1724 info->u.s390.core,
1725 S390CrashReason_str(info->u.s390.reason),
1726 info->u.s390.psw_mask,
1727 info->u.s390.psw_addr);
1729 qapi_free_GuestPanicInformation(info);
1733 void qemu_system_reset_request(ShutdownCause reason)
1735 if (no_reboot && reason != SHUTDOWN_CAUSE_SUBSYSTEM_RESET) {
1736 shutdown_requested = reason;
1737 } else {
1738 reset_requested = reason;
1740 cpu_stop_current();
1741 qemu_notify_event();
1744 static void qemu_system_suspend(void)
1746 pause_all_vcpus();
1747 notifier_list_notify(&suspend_notifiers, NULL);
1748 runstate_set(RUN_STATE_SUSPENDED);
1749 qapi_event_send_suspend();
1752 void qemu_system_suspend_request(void)
1754 if (runstate_check(RUN_STATE_SUSPENDED)) {
1755 return;
1757 suspend_requested = 1;
1758 cpu_stop_current();
1759 qemu_notify_event();
1762 void qemu_register_suspend_notifier(Notifier *notifier)
1764 notifier_list_add(&suspend_notifiers, notifier);
1767 void qemu_system_wakeup_request(WakeupReason reason, Error **errp)
1769 trace_system_wakeup_request(reason);
1771 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1772 error_setg(errp,
1773 "Unable to wake up: guest is not in suspended state");
1774 return;
1776 if (!(wakeup_reason_mask & (1 << reason))) {
1777 return;
1779 runstate_set(RUN_STATE_RUNNING);
1780 wakeup_reason = reason;
1781 qemu_notify_event();
1784 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1786 if (enabled) {
1787 wakeup_reason_mask |= (1 << reason);
1788 } else {
1789 wakeup_reason_mask &= ~(1 << reason);
1793 void qemu_register_wakeup_notifier(Notifier *notifier)
1795 notifier_list_add(&wakeup_notifiers, notifier);
1798 void qemu_register_wakeup_support(void)
1800 wakeup_suspend_enabled = true;
1803 bool qemu_wakeup_suspend_enabled(void)
1805 return wakeup_suspend_enabled;
1808 CurrentMachineParams *qmp_query_current_machine(Error **errp)
1810 CurrentMachineParams *params = g_malloc0(sizeof(*params));
1811 params->wakeup_suspend_support = qemu_wakeup_suspend_enabled();
1813 return params;
1816 void qemu_system_killed(int signal, pid_t pid)
1818 shutdown_signal = signal;
1819 shutdown_pid = pid;
1820 no_shutdown = 0;
1822 /* Cannot call qemu_system_shutdown_request directly because
1823 * we are in a signal handler.
1825 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1826 qemu_notify_event();
1829 void qemu_system_shutdown_request(ShutdownCause reason)
1831 trace_qemu_system_shutdown_request(reason);
1832 replay_shutdown_request(reason);
1833 shutdown_requested = reason;
1834 qemu_notify_event();
1837 static void qemu_system_powerdown(void)
1839 qapi_event_send_powerdown();
1840 notifier_list_notify(&powerdown_notifiers, NULL);
1843 static void qemu_system_shutdown(ShutdownCause cause)
1845 qapi_event_send_shutdown(shutdown_caused_by_guest(cause), cause);
1846 notifier_list_notify(&shutdown_notifiers, &cause);
1849 void qemu_system_powerdown_request(void)
1851 trace_qemu_system_powerdown_request();
1852 powerdown_requested = 1;
1853 qemu_notify_event();
1856 void qemu_register_powerdown_notifier(Notifier *notifier)
1858 notifier_list_add(&powerdown_notifiers, notifier);
1861 void qemu_register_shutdown_notifier(Notifier *notifier)
1863 notifier_list_add(&shutdown_notifiers, notifier);
1866 void qemu_system_debug_request(void)
1868 debug_requested = 1;
1869 qemu_notify_event();
1872 static bool main_loop_should_exit(void)
1874 RunState r;
1875 ShutdownCause request;
1877 if (preconfig_exit_requested) {
1878 if (runstate_check(RUN_STATE_PRECONFIG)) {
1879 runstate_set(RUN_STATE_PRELAUNCH);
1881 preconfig_exit_requested = false;
1882 return true;
1884 if (qemu_debug_requested()) {
1885 vm_stop(RUN_STATE_DEBUG);
1887 if (qemu_suspend_requested()) {
1888 qemu_system_suspend();
1890 request = qemu_shutdown_requested();
1891 if (request) {
1892 qemu_kill_report();
1893 qemu_system_shutdown(request);
1894 if (no_shutdown) {
1895 vm_stop(RUN_STATE_SHUTDOWN);
1896 } else {
1897 return true;
1900 request = qemu_reset_requested();
1901 if (request) {
1902 pause_all_vcpus();
1903 qemu_system_reset(request);
1904 resume_all_vcpus();
1905 if (!runstate_check(RUN_STATE_RUNNING) &&
1906 !runstate_check(RUN_STATE_INMIGRATE)) {
1907 runstate_set(RUN_STATE_PRELAUNCH);
1910 if (qemu_wakeup_requested()) {
1911 pause_all_vcpus();
1912 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1913 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1914 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1915 resume_all_vcpus();
1916 qapi_event_send_wakeup();
1918 if (qemu_powerdown_requested()) {
1919 qemu_system_powerdown();
1921 if (qemu_vmstop_requested(&r)) {
1922 vm_stop(r);
1924 return false;
1927 static void main_loop(void)
1929 #ifdef CONFIG_PROFILER
1930 int64_t ti;
1931 #endif
1932 while (!main_loop_should_exit()) {
1933 #ifdef CONFIG_PROFILER
1934 ti = profile_getclock();
1935 #endif
1936 main_loop_wait(false);
1937 #ifdef CONFIG_PROFILER
1938 dev_time += profile_getclock() - ti;
1939 #endif
1943 static void version(void)
1945 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1946 QEMU_COPYRIGHT "\n");
1949 static void QEMU_NORETURN help(int exitcode)
1951 version();
1952 printf("usage: %s [options] [disk_image]\n\n"
1953 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1954 error_get_progname());
1956 #define QEMU_OPTIONS_GENERATE_HELP
1957 #include "qemu-options-wrapper.h"
1959 printf("\nDuring emulation, the following keys are useful:\n"
1960 "ctrl-alt-f toggle full screen\n"
1961 "ctrl-alt-n switch to virtual console 'n'\n"
1962 "ctrl-alt toggle mouse and keyboard grab\n"
1963 "\n"
1964 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1965 "\n"
1966 QEMU_HELP_BOTTOM "\n");
1968 exit(exitcode);
1971 #define HAS_ARG 0x0001
1973 typedef struct QEMUOption {
1974 const char *name;
1975 int flags;
1976 int index;
1977 uint32_t arch_mask;
1978 } QEMUOption;
1980 static const QEMUOption qemu_options[] = {
1981 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1982 #define QEMU_OPTIONS_GENERATE_OPTIONS
1983 #include "qemu-options-wrapper.h"
1984 { NULL },
1987 typedef struct VGAInterfaceInfo {
1988 const char *opt_name; /* option name */
1989 const char *name; /* human-readable name */
1990 /* Class names indicating that support is available.
1991 * If no class is specified, the interface is always available */
1992 const char *class_names[2];
1993 } VGAInterfaceInfo;
1995 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1996 [VGA_NONE] = {
1997 .opt_name = "none",
1999 [VGA_STD] = {
2000 .opt_name = "std",
2001 .name = "standard VGA",
2002 .class_names = { "VGA", "isa-vga" },
2004 [VGA_CIRRUS] = {
2005 .opt_name = "cirrus",
2006 .name = "Cirrus VGA",
2007 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2009 [VGA_VMWARE] = {
2010 .opt_name = "vmware",
2011 .name = "VMWare SVGA",
2012 .class_names = { "vmware-svga" },
2014 [VGA_VIRTIO] = {
2015 .opt_name = "virtio",
2016 .name = "Virtio VGA",
2017 .class_names = { "virtio-vga" },
2019 [VGA_QXL] = {
2020 .opt_name = "qxl",
2021 .name = "QXL VGA",
2022 .class_names = { "qxl-vga" },
2024 [VGA_TCX] = {
2025 .opt_name = "tcx",
2026 .name = "TCX framebuffer",
2027 .class_names = { "SUNW,tcx" },
2029 [VGA_CG3] = {
2030 .opt_name = "cg3",
2031 .name = "CG3 framebuffer",
2032 .class_names = { "cgthree" },
2034 [VGA_XENFB] = {
2035 .opt_name = "xenfb",
2039 static bool vga_interface_available(VGAInterfaceType t)
2041 VGAInterfaceInfo *ti = &vga_interfaces[t];
2043 assert(t < VGA_TYPE_MAX);
2044 return !ti->class_names[0] ||
2045 object_class_by_name(ti->class_names[0]) ||
2046 object_class_by_name(ti->class_names[1]);
2049 static void select_vgahw(const char *p)
2051 const char *opts;
2052 int t;
2054 assert(vga_interface_type == VGA_NONE);
2055 for (t = 0; t < VGA_TYPE_MAX; t++) {
2056 VGAInterfaceInfo *ti = &vga_interfaces[t];
2057 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2058 if (!vga_interface_available(t)) {
2059 error_report("%s not available", ti->name);
2060 exit(1);
2062 vga_interface_type = t;
2063 break;
2066 if (t == VGA_TYPE_MAX) {
2067 invalid_vga:
2068 error_report("unknown vga type: %s", p);
2069 exit(1);
2071 while (*opts) {
2072 const char *nextopt;
2074 if (strstart(opts, ",retrace=", &nextopt)) {
2075 opts = nextopt;
2076 if (strstart(opts, "dumb", &nextopt))
2077 vga_retrace_method = VGA_RETRACE_DUMB;
2078 else if (strstart(opts, "precise", &nextopt))
2079 vga_retrace_method = VGA_RETRACE_PRECISE;
2080 else goto invalid_vga;
2081 } else goto invalid_vga;
2082 opts = nextopt;
2086 static void parse_display_qapi(const char *optarg)
2088 DisplayOptions *opts;
2089 Visitor *v;
2091 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
2093 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2094 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2096 qapi_free_DisplayOptions(opts);
2097 visit_free(v);
2100 DisplayOptions *qmp_query_display_options(Error **errp)
2102 return QAPI_CLONE(DisplayOptions, &dpy);
2105 static void parse_display(const char *p)
2107 const char *opts;
2109 if (strstart(p, "sdl", &opts)) {
2111 * sdl DisplayType needs hand-crafted parser instead of
2112 * parse_display_qapi() due to some options not in
2113 * DisplayOptions, specifically:
2114 * - frame
2115 * Already deprecated.
2116 * - ctrl_grab + alt_grab
2117 * Not clear yet what happens to them long-term. Should
2118 * replaced by something better or deprecated and dropped.
2120 dpy.type = DISPLAY_TYPE_SDL;
2121 while (*opts) {
2122 const char *nextopt;
2124 if (strstart(opts, ",frame=", &nextopt)) {
2125 g_printerr("The frame= sdl option is deprecated, and will be\n"
2126 "removed in a future release.\n");
2127 opts = nextopt;
2128 if (strstart(opts, "on", &nextopt)) {
2129 no_frame = 0;
2130 } else if (strstart(opts, "off", &nextopt)) {
2131 no_frame = 1;
2132 } else {
2133 goto invalid_sdl_args;
2135 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2136 opts = nextopt;
2137 if (strstart(opts, "on", &nextopt)) {
2138 alt_grab = 1;
2139 } else if (strstart(opts, "off", &nextopt)) {
2140 alt_grab = 0;
2141 } else {
2142 goto invalid_sdl_args;
2144 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2145 opts = nextopt;
2146 if (strstart(opts, "on", &nextopt)) {
2147 ctrl_grab = 1;
2148 } else if (strstart(opts, "off", &nextopt)) {
2149 ctrl_grab = 0;
2150 } else {
2151 goto invalid_sdl_args;
2153 } else if (strstart(opts, ",window_close=", &nextopt)) {
2154 opts = nextopt;
2155 dpy.has_window_close = true;
2156 if (strstart(opts, "on", &nextopt)) {
2157 dpy.window_close = true;
2158 } else if (strstart(opts, "off", &nextopt)) {
2159 dpy.window_close = false;
2160 } else {
2161 goto invalid_sdl_args;
2163 } else if (strstart(opts, ",gl=", &nextopt)) {
2164 opts = nextopt;
2165 dpy.has_gl = true;
2166 if (strstart(opts, "on", &nextopt)) {
2167 dpy.gl = DISPLAYGL_MODE_ON;
2168 } else if (strstart(opts, "core", &nextopt)) {
2169 dpy.gl = DISPLAYGL_MODE_CORE;
2170 } else if (strstart(opts, "es", &nextopt)) {
2171 dpy.gl = DISPLAYGL_MODE_ES;
2172 } else if (strstart(opts, "off", &nextopt)) {
2173 dpy.gl = DISPLAYGL_MODE_OFF;
2174 } else {
2175 goto invalid_sdl_args;
2177 } else {
2178 invalid_sdl_args:
2179 error_report("invalid SDL option string");
2180 exit(1);
2182 opts = nextopt;
2184 } else if (strstart(p, "vnc", &opts)) {
2186 * vnc isn't a (local) DisplayType but a protocol for remote
2187 * display access.
2189 if (*opts == '=') {
2190 vnc_parse(opts + 1, &error_fatal);
2191 } else {
2192 error_report("VNC requires a display argument vnc=<display>");
2193 exit(1);
2195 } else {
2196 parse_display_qapi(p);
2200 char *qemu_find_file(int type, const char *name)
2202 int i;
2203 const char *subdir;
2204 char *buf;
2206 /* Try the name as a straight path first */
2207 if (access(name, R_OK) == 0) {
2208 trace_load_file(name, name);
2209 return g_strdup(name);
2212 switch (type) {
2213 case QEMU_FILE_TYPE_BIOS:
2214 subdir = "";
2215 break;
2216 case QEMU_FILE_TYPE_KEYMAP:
2217 subdir = "keymaps/";
2218 break;
2219 default:
2220 abort();
2223 for (i = 0; i < data_dir_idx; i++) {
2224 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2225 if (access(buf, R_OK) == 0) {
2226 trace_load_file(name, buf);
2227 return buf;
2229 g_free(buf);
2231 return NULL;
2234 static void qemu_add_data_dir(const char *path)
2236 int i;
2238 if (path == NULL) {
2239 return;
2241 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2242 return;
2244 for (i = 0; i < data_dir_idx; i++) {
2245 if (strcmp(data_dir[i], path) == 0) {
2246 return; /* duplicate */
2249 data_dir[data_dir_idx++] = g_strdup(path);
2252 static inline bool nonempty_str(const char *str)
2254 return str && *str;
2257 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2259 gchar *buf;
2260 size_t size;
2261 const char *name, *file, *str;
2262 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2264 if (fw_cfg == NULL) {
2265 error_setg(errp, "fw_cfg device not available");
2266 return -1;
2268 name = qemu_opt_get(opts, "name");
2269 file = qemu_opt_get(opts, "file");
2270 str = qemu_opt_get(opts, "string");
2272 /* we need name and either a file or the content string */
2273 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2274 error_setg(errp, "invalid argument(s)");
2275 return -1;
2277 if (nonempty_str(file) && nonempty_str(str)) {
2278 error_setg(errp, "file and string are mutually exclusive");
2279 return -1;
2281 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2282 error_setg(errp, "name too long (max. %d char)",
2283 FW_CFG_MAX_FILE_PATH - 1);
2284 return -1;
2286 if (strncmp(name, "opt/", 4) != 0) {
2287 warn_report("externally provided fw_cfg item names "
2288 "should be prefixed with \"opt/\"");
2290 if (nonempty_str(str)) {
2291 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2292 buf = g_memdup(str, size);
2293 } else {
2294 GError *err = NULL;
2295 if (!g_file_get_contents(file, &buf, &size, &err)) {
2296 error_setg(errp, "can't load %s: %s", file, err->message);
2297 g_error_free(err);
2298 return -1;
2301 /* For legacy, keep user files in a specific global order. */
2302 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2303 fw_cfg_add_file(fw_cfg, name, buf, size);
2304 fw_cfg_reset_order_override(fw_cfg);
2305 return 0;
2308 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2310 return qdev_device_help(opts);
2313 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2315 DeviceState *dev;
2317 dev = qdev_device_add(opts, errp);
2318 if (!dev) {
2319 return -1;
2321 object_unref(OBJECT(dev));
2322 return 0;
2325 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2327 Error *local_err = NULL;
2329 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2330 if (local_err) {
2331 error_propagate(errp, local_err);
2332 return -1;
2334 exit(0);
2336 return 0;
2339 #ifdef CONFIG_VIRTFS
2340 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2342 return qemu_fsdev_add(opts, errp);
2344 #endif
2346 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2348 Chardev *chr;
2349 const char *chardev;
2350 const char *mode;
2351 int flags;
2353 mode = qemu_opt_get(opts, "mode");
2354 if (mode == NULL) {
2355 mode = "readline";
2357 if (strcmp(mode, "readline") == 0) {
2358 flags = MONITOR_USE_READLINE;
2359 } else if (strcmp(mode, "control") == 0) {
2360 flags = MONITOR_USE_CONTROL;
2361 } else {
2362 error_setg(errp, "unknown monitor mode \"%s\"", mode);
2363 return -1;
2366 if (qemu_opt_get_bool(opts, "pretty", 0))
2367 flags |= MONITOR_USE_PRETTY;
2369 chardev = qemu_opt_get(opts, "chardev");
2370 if (!chardev) {
2371 error_report("chardev is required");
2372 exit(1);
2374 chr = qemu_chr_find(chardev);
2375 if (chr == NULL) {
2376 error_setg(errp, "chardev \"%s\" not found", chardev);
2377 return -1;
2380 monitor_init(chr, flags);
2381 return 0;
2384 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2386 static int monitor_device_index = 0;
2387 QemuOpts *opts;
2388 const char *p;
2389 char label[32];
2391 if (strstart(optarg, "chardev:", &p)) {
2392 snprintf(label, sizeof(label), "%s", p);
2393 } else {
2394 snprintf(label, sizeof(label), "compat_monitor%d",
2395 monitor_device_index);
2396 opts = qemu_chr_parse_compat(label, optarg, true);
2397 if (!opts) {
2398 error_report("parse error: %s", optarg);
2399 exit(1);
2403 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2404 qemu_opt_set(opts, "mode", mode, &error_abort);
2405 qemu_opt_set(opts, "chardev", label, &error_abort);
2406 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2407 monitor_device_index++;
2410 struct device_config {
2411 enum {
2412 DEV_USB, /* -usbdevice */
2413 DEV_BT, /* -bt */
2414 DEV_SERIAL, /* -serial */
2415 DEV_PARALLEL, /* -parallel */
2416 DEV_VIRTCON, /* -virtioconsole */
2417 DEV_DEBUGCON, /* -debugcon */
2418 DEV_GDB, /* -gdb, -s */
2419 DEV_SCLP, /* s390 sclp */
2420 } type;
2421 const char *cmdline;
2422 Location loc;
2423 QTAILQ_ENTRY(device_config) next;
2426 static QTAILQ_HEAD(, device_config) device_configs =
2427 QTAILQ_HEAD_INITIALIZER(device_configs);
2429 static void add_device_config(int type, const char *cmdline)
2431 struct device_config *conf;
2433 conf = g_malloc0(sizeof(*conf));
2434 conf->type = type;
2435 conf->cmdline = cmdline;
2436 loc_save(&conf->loc);
2437 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2440 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2442 struct device_config *conf;
2443 int rc;
2445 QTAILQ_FOREACH(conf, &device_configs, next) {
2446 if (conf->type != type)
2447 continue;
2448 loc_push_restore(&conf->loc);
2449 rc = func(conf->cmdline);
2450 loc_pop(&conf->loc);
2451 if (rc) {
2452 return rc;
2455 return 0;
2458 static int serial_parse(const char *devname)
2460 int index = num_serial_hds;
2461 char label[32];
2463 if (strcmp(devname, "none") == 0)
2464 return 0;
2465 snprintf(label, sizeof(label), "serial%d", index);
2466 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2468 serial_hds[index] = qemu_chr_new_mux_mon(label, devname);
2469 if (!serial_hds[index]) {
2470 error_report("could not connect serial device"
2471 " to character backend '%s'", devname);
2472 return -1;
2474 num_serial_hds++;
2475 return 0;
2478 Chardev *serial_hd(int i)
2480 assert(i >= 0);
2481 if (i < num_serial_hds) {
2482 return serial_hds[i];
2484 return NULL;
2487 int serial_max_hds(void)
2489 return num_serial_hds;
2492 static int parallel_parse(const char *devname)
2494 static int index = 0;
2495 char label[32];
2497 if (strcmp(devname, "none") == 0)
2498 return 0;
2499 if (index == MAX_PARALLEL_PORTS) {
2500 error_report("too many parallel ports");
2501 exit(1);
2503 snprintf(label, sizeof(label), "parallel%d", index);
2504 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname);
2505 if (!parallel_hds[index]) {
2506 error_report("could not connect parallel device"
2507 " to character backend '%s'", devname);
2508 return -1;
2510 index++;
2511 return 0;
2514 static int virtcon_parse(const char *devname)
2516 QemuOptsList *device = qemu_find_opts("device");
2517 static int index = 0;
2518 char label[32];
2519 QemuOpts *bus_opts, *dev_opts;
2521 if (strcmp(devname, "none") == 0)
2522 return 0;
2523 if (index == MAX_VIRTIO_CONSOLES) {
2524 error_report("too many virtio consoles");
2525 exit(1);
2528 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2529 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2531 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2532 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2534 snprintf(label, sizeof(label), "virtcon%d", index);
2535 virtcon_hds[index] = qemu_chr_new_mux_mon(label, devname);
2536 if (!virtcon_hds[index]) {
2537 error_report("could not connect virtio console"
2538 " to character backend '%s'", devname);
2539 return -1;
2541 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2543 index++;
2544 return 0;
2547 static int debugcon_parse(const char *devname)
2549 QemuOpts *opts;
2551 if (!qemu_chr_new_mux_mon("debugcon", devname)) {
2552 error_report("invalid character backend '%s'", devname);
2553 exit(1);
2555 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2556 if (!opts) {
2557 error_report("already have a debugcon device");
2558 exit(1);
2560 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2561 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2562 return 0;
2565 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2567 const MachineClass *mc1 = a, *mc2 = b;
2568 int res;
2570 if (mc1->family == NULL) {
2571 if (mc2->family == NULL) {
2572 /* Compare standalone machine types against each other; they sort
2573 * in increasing order.
2575 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2576 object_class_get_name(OBJECT_CLASS(mc2)));
2579 /* Standalone machine types sort after families. */
2580 return 1;
2583 if (mc2->family == NULL) {
2584 /* Families sort before standalone machine types. */
2585 return -1;
2588 /* Families sort between each other alphabetically increasingly. */
2589 res = strcmp(mc1->family, mc2->family);
2590 if (res != 0) {
2591 return res;
2594 /* Within the same family, machine types sort in decreasing order. */
2595 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2596 object_class_get_name(OBJECT_CLASS(mc1)));
2599 static MachineClass *machine_parse(const char *name)
2601 MachineClass *mc = NULL;
2602 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2604 if (name) {
2605 mc = find_machine(name);
2607 if (mc) {
2608 g_slist_free(machines);
2609 return mc;
2611 if (name && !is_help_option(name)) {
2612 error_report("unsupported machine type");
2613 error_printf("Use -machine help to list supported machines\n");
2614 } else {
2615 printf("Supported machines are:\n");
2616 machines = g_slist_sort(machines, machine_class_cmp);
2617 for (el = machines; el; el = el->next) {
2618 MachineClass *mc = el->data;
2619 if (mc->alias) {
2620 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2622 printf("%-20s %s%s%s\n", mc->name, mc->desc,
2623 mc->is_default ? " (default)" : "",
2624 mc->deprecation_reason ? " (deprecated)" : "");
2628 g_slist_free(machines);
2629 exit(!name || !is_help_option(name));
2632 void qemu_add_exit_notifier(Notifier *notify)
2634 notifier_list_add(&exit_notifiers, notify);
2637 void qemu_remove_exit_notifier(Notifier *notify)
2639 notifier_remove(notify);
2642 static void qemu_run_exit_notifiers(void)
2644 notifier_list_notify(&exit_notifiers, NULL);
2647 static const char *pid_file;
2648 static Notifier qemu_unlink_pidfile_notifier;
2650 static void qemu_unlink_pidfile(Notifier *n, void *data)
2652 if (pid_file) {
2653 unlink(pid_file);
2657 bool machine_init_done;
2659 void qemu_add_machine_init_done_notifier(Notifier *notify)
2661 notifier_list_add(&machine_init_done_notifiers, notify);
2662 if (machine_init_done) {
2663 notify->notify(notify, NULL);
2667 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2669 notifier_remove(notify);
2672 static void qemu_run_machine_init_done_notifiers(void)
2674 machine_init_done = true;
2675 notifier_list_notify(&machine_init_done_notifiers, NULL);
2678 static const QEMUOption *lookup_opt(int argc, char **argv,
2679 const char **poptarg, int *poptind)
2681 const QEMUOption *popt;
2682 int optind = *poptind;
2683 char *r = argv[optind];
2684 const char *optarg;
2686 loc_set_cmdline(argv, optind, 1);
2687 optind++;
2688 /* Treat --foo the same as -foo. */
2689 if (r[1] == '-')
2690 r++;
2691 popt = qemu_options;
2692 for(;;) {
2693 if (!popt->name) {
2694 error_report("invalid option");
2695 exit(1);
2697 if (!strcmp(popt->name, r + 1))
2698 break;
2699 popt++;
2701 if (popt->flags & HAS_ARG) {
2702 if (optind >= argc) {
2703 error_report("requires an argument");
2704 exit(1);
2706 optarg = argv[optind++];
2707 loc_set_cmdline(argv, optind - 2, 2);
2708 } else {
2709 optarg = NULL;
2712 *poptarg = optarg;
2713 *poptind = optind;
2715 return popt;
2718 static MachineClass *select_machine(void)
2720 MachineClass *machine_class = find_default_machine();
2721 const char *optarg;
2722 QemuOpts *opts;
2723 Location loc;
2725 loc_push_none(&loc);
2727 opts = qemu_get_machine_opts();
2728 qemu_opts_loc_restore(opts);
2730 optarg = qemu_opt_get(opts, "type");
2731 if (optarg) {
2732 machine_class = machine_parse(optarg);
2735 if (!machine_class) {
2736 error_report("No machine specified, and there is no default");
2737 error_printf("Use -machine help to list supported machines\n");
2738 exit(1);
2741 loc_pop(&loc);
2742 return machine_class;
2745 static int machine_set_property(void *opaque,
2746 const char *name, const char *value,
2747 Error **errp)
2749 Object *obj = OBJECT(opaque);
2750 Error *local_err = NULL;
2751 char *p, *qom_name;
2753 if (strcmp(name, "type") == 0) {
2754 return 0;
2757 qom_name = g_strdup(name);
2758 for (p = qom_name; *p; p++) {
2759 if (*p == '_') {
2760 *p = '-';
2764 object_property_parse(obj, value, qom_name, &local_err);
2765 g_free(qom_name);
2767 if (local_err) {
2768 error_propagate(errp, local_err);
2769 return -1;
2772 return 0;
2777 * Initial object creation happens before all other
2778 * QEMU data types are created. The majority of objects
2779 * can be created at this point. The rng-egd object
2780 * cannot be created here, as it depends on the chardev
2781 * already existing.
2783 static bool object_create_initial(const char *type, QemuOpts *opts)
2785 ObjectClass *klass;
2787 if (is_help_option(type)) {
2788 GSList *l, *list;
2790 printf("List of user creatable objects:\n");
2791 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
2792 for (l = list; l != NULL; l = l->next) {
2793 ObjectClass *oc = OBJECT_CLASS(l->data);
2794 printf(" %s\n", object_class_get_name(oc));
2796 g_slist_free(list);
2797 exit(0);
2800 klass = object_class_by_name(type);
2801 if (klass && qemu_opt_has_help_opt(opts)) {
2802 ObjectPropertyIterator iter;
2803 ObjectProperty *prop;
2804 GPtrArray *array = g_ptr_array_new();
2805 int i;
2807 object_class_property_iter_init(&iter, klass);
2808 while ((prop = object_property_iter_next(&iter))) {
2809 GString *str;
2811 if (!prop->set) {
2812 continue;
2815 str = g_string_new(NULL);
2816 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
2817 if (prop->description) {
2818 if (str->len < 24) {
2819 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
2821 g_string_append_printf(str, " - %s", prop->description);
2823 g_ptr_array_add(array, g_string_free(str, false));
2825 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
2826 if (array->len > 0) {
2827 printf("%s options:\n", type);
2828 } else {
2829 printf("There are no options for %s.\n", type);
2831 for (i = 0; i < array->len; i++) {
2832 printf("%s\n", (char *)array->pdata[i]);
2834 g_ptr_array_set_free_func(array, g_free);
2835 g_ptr_array_free(array, true);
2836 exit(0);
2839 if (g_str_equal(type, "rng-egd") ||
2840 g_str_has_prefix(type, "pr-manager-")) {
2841 return false;
2844 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2845 if (g_str_equal(type, "cryptodev-vhost-user")) {
2846 return false;
2848 #endif
2851 * return false for concrete netfilters since
2852 * they depend on netdevs already existing
2854 if (g_str_equal(type, "filter-buffer") ||
2855 g_str_equal(type, "filter-dump") ||
2856 g_str_equal(type, "filter-mirror") ||
2857 g_str_equal(type, "filter-redirector") ||
2858 g_str_equal(type, "colo-compare") ||
2859 g_str_equal(type, "filter-rewriter") ||
2860 g_str_equal(type, "filter-replay")) {
2861 return false;
2864 /* Memory allocation by backends needs to be done
2865 * after configure_accelerator() (due to the tcg_enabled()
2866 * checks at memory_region_init_*()).
2868 * Also, allocation of large amounts of memory may delay
2869 * chardev initialization for too long, and trigger timeouts
2870 * on software that waits for a monitor socket to be created
2871 * (e.g. libvirt).
2873 if (g_str_has_prefix(type, "memory-backend-")) {
2874 return false;
2877 return true;
2882 * The remainder of object creation happens after the
2883 * creation of chardev, fsdev, net clients and device data types.
2885 static bool object_create_delayed(const char *type, QemuOpts *opts)
2887 return !object_create_initial(type, opts);
2891 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2892 MachineClass *mc)
2894 uint64_t sz;
2895 const char *mem_str;
2896 const ram_addr_t default_ram_size = mc->default_ram_size;
2897 QemuOpts *opts = qemu_find_opts_singleton("memory");
2898 Location loc;
2900 loc_push_none(&loc);
2901 qemu_opts_loc_restore(opts);
2903 sz = 0;
2904 mem_str = qemu_opt_get(opts, "size");
2905 if (mem_str) {
2906 if (!*mem_str) {
2907 error_report("missing 'size' option value");
2908 exit(EXIT_FAILURE);
2911 sz = qemu_opt_get_size(opts, "size", ram_size);
2913 /* Fix up legacy suffix-less format */
2914 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2915 uint64_t overflow_check = sz;
2917 sz *= MiB;
2918 if (sz / MiB != overflow_check) {
2919 error_report("too large 'size' option value");
2920 exit(EXIT_FAILURE);
2925 /* backward compatibility behaviour for case "-m 0" */
2926 if (sz == 0) {
2927 sz = default_ram_size;
2930 sz = QEMU_ALIGN_UP(sz, 8192);
2931 ram_size = sz;
2932 if (ram_size != sz) {
2933 error_report("ram size too large");
2934 exit(EXIT_FAILURE);
2937 /* store value for the future use */
2938 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2939 *maxram_size = ram_size;
2941 if (qemu_opt_get(opts, "maxmem")) {
2942 uint64_t slots;
2944 sz = qemu_opt_get_size(opts, "maxmem", 0);
2945 slots = qemu_opt_get_number(opts, "slots", 0);
2946 if (sz < ram_size) {
2947 error_report("invalid value of -m option maxmem: "
2948 "maximum memory size (0x%" PRIx64 ") must be at least "
2949 "the initial memory size (0x" RAM_ADDR_FMT ")",
2950 sz, ram_size);
2951 exit(EXIT_FAILURE);
2952 } else if (slots && sz == ram_size) {
2953 error_report("invalid value of -m option maxmem: "
2954 "memory slots were specified but maximum memory size "
2955 "(0x%" PRIx64 ") is equal to the initial memory size "
2956 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2957 exit(EXIT_FAILURE);
2960 *maxram_size = sz;
2961 *ram_slots = slots;
2962 } else if (qemu_opt_get(opts, "slots")) {
2963 error_report("invalid -m option value: missing 'maxmem' option");
2964 exit(EXIT_FAILURE);
2967 loc_pop(&loc);
2970 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2972 GlobalProperty *g;
2974 g = g_malloc0(sizeof(*g));
2975 g->driver = qemu_opt_get(opts, "driver");
2976 g->property = qemu_opt_get(opts, "property");
2977 g->value = qemu_opt_get(opts, "value");
2978 g->user_provided = true;
2979 g->errp = &error_fatal;
2980 qdev_prop_register_global(g);
2981 return 0;
2984 static int qemu_read_default_config_file(void)
2986 int ret;
2988 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2989 if (ret < 0 && ret != -ENOENT) {
2990 return ret;
2993 return 0;
2996 static void user_register_global_props(void)
2998 qemu_opts_foreach(qemu_find_opts("global"),
2999 global_init_func, NULL, NULL);
3003 * Note: we should see that these properties are actually having a
3004 * priority: accel < machine < user. This means e.g. when user
3005 * specifies something in "-global", it'll always be used with highest
3006 * priority than either machine/accelerator compat properties.
3008 static void register_global_properties(MachineState *ms)
3010 accel_register_compat_props(ms->accelerator);
3011 machine_register_compat_props(ms);
3012 user_register_global_props();
3015 int main(int argc, char **argv)
3017 int i;
3018 int snapshot, linux_boot;
3019 const char *initrd_filename;
3020 const char *kernel_filename, *kernel_cmdline;
3021 const char *boot_order = NULL;
3022 const char *boot_once = NULL;
3023 DisplayState *ds;
3024 QemuOpts *opts, *machine_opts;
3025 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
3026 QemuOptsList *olist;
3027 int optind;
3028 const char *optarg;
3029 const char *loadvm = NULL;
3030 MachineClass *machine_class;
3031 const char *cpu_model;
3032 const char *vga_model = NULL;
3033 const char *qtest_chrdev = NULL;
3034 const char *qtest_log = NULL;
3035 const char *incoming = NULL;
3036 bool userconfig = true;
3037 bool nographic = false;
3038 int display_remote = 0;
3039 const char *log_mask = NULL;
3040 const char *log_file = NULL;
3041 char *trace_file = NULL;
3042 ram_addr_t maxram_size;
3043 uint64_t ram_slots = 0;
3044 FILE *vmstate_dump_file = NULL;
3045 Error *main_loop_err = NULL;
3046 Error *err = NULL;
3047 bool list_data_dirs = false;
3048 char *dir, **dirs;
3049 typedef struct BlockdevOptions_queue {
3050 BlockdevOptions *bdo;
3051 Location loc;
3052 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3053 } BlockdevOptions_queue;
3054 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3055 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3057 module_call_init(MODULE_INIT_TRACE);
3059 qemu_init_cpu_list();
3060 qemu_init_cpu_loop();
3062 qemu_mutex_lock_iothread();
3064 atexit(qemu_run_exit_notifiers);
3065 error_set_progname(argv[0]);
3066 qemu_init_exec_dir(argv[0]);
3068 module_call_init(MODULE_INIT_QOM);
3070 qemu_add_opts(&qemu_drive_opts);
3071 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3072 qemu_add_drive_opts(&qemu_common_drive_opts);
3073 qemu_add_drive_opts(&qemu_drive_opts);
3074 qemu_add_drive_opts(&bdrv_runtime_opts);
3075 qemu_add_opts(&qemu_chardev_opts);
3076 qemu_add_opts(&qemu_device_opts);
3077 qemu_add_opts(&qemu_netdev_opts);
3078 qemu_add_opts(&qemu_nic_opts);
3079 qemu_add_opts(&qemu_net_opts);
3080 qemu_add_opts(&qemu_rtc_opts);
3081 qemu_add_opts(&qemu_global_opts);
3082 qemu_add_opts(&qemu_mon_opts);
3083 qemu_add_opts(&qemu_trace_opts);
3084 qemu_add_opts(&qemu_option_rom_opts);
3085 qemu_add_opts(&qemu_machine_opts);
3086 qemu_add_opts(&qemu_accel_opts);
3087 qemu_add_opts(&qemu_mem_opts);
3088 qemu_add_opts(&qemu_smp_opts);
3089 qemu_add_opts(&qemu_boot_opts);
3090 qemu_add_opts(&qemu_add_fd_opts);
3091 qemu_add_opts(&qemu_object_opts);
3092 qemu_add_opts(&qemu_tpmdev_opts);
3093 qemu_add_opts(&qemu_realtime_opts);
3094 qemu_add_opts(&qemu_overcommit_opts);
3095 qemu_add_opts(&qemu_msg_opts);
3096 qemu_add_opts(&qemu_name_opts);
3097 qemu_add_opts(&qemu_numa_opts);
3098 qemu_add_opts(&qemu_icount_opts);
3099 qemu_add_opts(&qemu_semihosting_config_opts);
3100 qemu_add_opts(&qemu_fw_cfg_opts);
3101 module_call_init(MODULE_INIT_OPTS);
3103 runstate_init();
3104 postcopy_infrastructure_init();
3105 monitor_init_globals();
3107 if (qcrypto_init(&err) < 0) {
3108 error_reportf_err(err, "cannot initialize crypto: ");
3109 exit(1);
3112 QLIST_INIT (&vm_change_state_head);
3113 os_setup_early_signal_handling();
3115 cpu_model = NULL;
3116 snapshot = 0;
3118 nb_nics = 0;
3120 bdrv_init_with_whitelist();
3122 autostart = 1;
3124 /* first pass of option parsing */
3125 optind = 1;
3126 while (optind < argc) {
3127 if (argv[optind][0] != '-') {
3128 /* disk image */
3129 optind++;
3130 } else {
3131 const QEMUOption *popt;
3133 popt = lookup_opt(argc, argv, &optarg, &optind);
3134 switch (popt->index) {
3135 case QEMU_OPTION_nouserconfig:
3136 userconfig = false;
3137 break;
3142 if (userconfig) {
3143 if (qemu_read_default_config_file() < 0) {
3144 exit(1);
3148 /* second pass of option parsing */
3149 optind = 1;
3150 for(;;) {
3151 if (optind >= argc)
3152 break;
3153 if (argv[optind][0] != '-') {
3154 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3155 } else {
3156 const QEMUOption *popt;
3158 popt = lookup_opt(argc, argv, &optarg, &optind);
3159 if (!(popt->arch_mask & arch_type)) {
3160 error_report("Option not supported for this target");
3161 exit(1);
3163 switch(popt->index) {
3164 case QEMU_OPTION_cpu:
3165 /* hw initialization will check this */
3166 cpu_model = optarg;
3167 break;
3168 case QEMU_OPTION_hda:
3169 case QEMU_OPTION_hdb:
3170 case QEMU_OPTION_hdc:
3171 case QEMU_OPTION_hdd:
3172 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3173 HD_OPTS);
3174 break;
3175 case QEMU_OPTION_blockdev:
3177 Visitor *v;
3178 BlockdevOptions_queue *bdo;
3180 v = qobject_input_visitor_new_str(optarg, "driver",
3181 &error_fatal);
3183 bdo = g_new(BlockdevOptions_queue, 1);
3184 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3185 &error_fatal);
3186 visit_free(v);
3187 loc_save(&bdo->loc);
3188 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3189 break;
3191 case QEMU_OPTION_drive:
3192 if (drive_def(optarg) == NULL) {
3193 exit(1);
3195 break;
3196 case QEMU_OPTION_set:
3197 if (qemu_set_option(optarg) != 0)
3198 exit(1);
3199 break;
3200 case QEMU_OPTION_global:
3201 if (qemu_global_option(optarg) != 0)
3202 exit(1);
3203 break;
3204 case QEMU_OPTION_mtdblock:
3205 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3206 break;
3207 case QEMU_OPTION_sd:
3208 drive_add(IF_SD, -1, optarg, SD_OPTS);
3209 break;
3210 case QEMU_OPTION_pflash:
3211 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3212 break;
3213 case QEMU_OPTION_snapshot:
3214 snapshot = 1;
3215 break;
3216 case QEMU_OPTION_numa:
3217 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3218 optarg, true);
3219 if (!opts) {
3220 exit(1);
3222 break;
3223 case QEMU_OPTION_display:
3224 parse_display(optarg);
3225 break;
3226 case QEMU_OPTION_nographic:
3227 olist = qemu_find_opts("machine");
3228 qemu_opts_parse_noisily(olist, "graphics=off", false);
3229 nographic = true;
3230 dpy.type = DISPLAY_TYPE_NONE;
3231 break;
3232 case QEMU_OPTION_curses:
3233 #ifdef CONFIG_CURSES
3234 dpy.type = DISPLAY_TYPE_CURSES;
3235 #else
3236 error_report("curses support is disabled");
3237 exit(1);
3238 #endif
3239 break;
3240 case QEMU_OPTION_portrait:
3241 graphic_rotate = 90;
3242 break;
3243 case QEMU_OPTION_rotate:
3244 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3245 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3246 graphic_rotate != 180 && graphic_rotate != 270) {
3247 error_report("only 90, 180, 270 deg rotation is available");
3248 exit(1);
3250 break;
3251 case QEMU_OPTION_kernel:
3252 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3253 &error_abort);
3254 break;
3255 case QEMU_OPTION_initrd:
3256 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3257 &error_abort);
3258 break;
3259 case QEMU_OPTION_append:
3260 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3261 &error_abort);
3262 break;
3263 case QEMU_OPTION_dtb:
3264 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3265 &error_abort);
3266 break;
3267 case QEMU_OPTION_cdrom:
3268 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3269 break;
3270 case QEMU_OPTION_boot:
3271 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3272 optarg, true);
3273 if (!opts) {
3274 exit(1);
3276 break;
3277 case QEMU_OPTION_fda:
3278 case QEMU_OPTION_fdb:
3279 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3280 optarg, FD_OPTS);
3281 break;
3282 case QEMU_OPTION_no_fd_bootchk:
3283 fd_bootchk = 0;
3284 break;
3285 case QEMU_OPTION_netdev:
3286 default_net = 0;
3287 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3288 exit(1);
3290 break;
3291 case QEMU_OPTION_nic:
3292 default_net = 0;
3293 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3294 exit(1);
3296 break;
3297 case QEMU_OPTION_net:
3298 default_net = 0;
3299 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3300 exit(1);
3302 break;
3303 #ifdef CONFIG_LIBISCSI
3304 case QEMU_OPTION_iscsi:
3305 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3306 optarg, false);
3307 if (!opts) {
3308 exit(1);
3310 break;
3311 #endif
3312 case QEMU_OPTION_bt:
3313 warn_report("The bluetooth subsystem is deprecated and will "
3314 "be removed soon. If the bluetooth subsystem is "
3315 "still useful for you, please send a mail to "
3316 "qemu-devel@nongnu.org with your usecase.");
3317 add_device_config(DEV_BT, optarg);
3318 break;
3319 case QEMU_OPTION_audio_help:
3320 AUD_help ();
3321 exit (0);
3322 break;
3323 case QEMU_OPTION_soundhw:
3324 select_soundhw (optarg);
3325 break;
3326 case QEMU_OPTION_h:
3327 help(0);
3328 break;
3329 case QEMU_OPTION_version:
3330 version();
3331 exit(0);
3332 break;
3333 case QEMU_OPTION_m:
3334 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3335 optarg, true);
3336 if (!opts) {
3337 exit(EXIT_FAILURE);
3339 break;
3340 #ifdef CONFIG_TPM
3341 case QEMU_OPTION_tpmdev:
3342 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3343 exit(1);
3345 break;
3346 #endif
3347 case QEMU_OPTION_mempath:
3348 mem_path = optarg;
3349 break;
3350 case QEMU_OPTION_mem_prealloc:
3351 mem_prealloc = 1;
3352 break;
3353 case QEMU_OPTION_d:
3354 log_mask = optarg;
3355 break;
3356 case QEMU_OPTION_D:
3357 log_file = optarg;
3358 break;
3359 case QEMU_OPTION_DFILTER:
3360 qemu_set_dfilter_ranges(optarg, &error_fatal);
3361 break;
3362 case QEMU_OPTION_s:
3363 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3364 break;
3365 case QEMU_OPTION_gdb:
3366 add_device_config(DEV_GDB, optarg);
3367 break;
3368 case QEMU_OPTION_L:
3369 if (is_help_option(optarg)) {
3370 list_data_dirs = true;
3371 } else {
3372 qemu_add_data_dir(optarg);
3374 break;
3375 case QEMU_OPTION_bios:
3376 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3377 &error_abort);
3378 break;
3379 case QEMU_OPTION_singlestep:
3380 singlestep = 1;
3381 break;
3382 case QEMU_OPTION_S:
3383 autostart = 0;
3384 break;
3385 case QEMU_OPTION_k:
3386 keyboard_layout = optarg;
3387 break;
3388 case QEMU_OPTION_vga:
3389 vga_model = optarg;
3390 default_vga = 0;
3391 break;
3392 case QEMU_OPTION_g:
3394 const char *p;
3395 int w, h, depth;
3396 p = optarg;
3397 w = strtol(p, (char **)&p, 10);
3398 if (w <= 0) {
3399 graphic_error:
3400 error_report("invalid resolution or depth");
3401 exit(1);
3403 if (*p != 'x')
3404 goto graphic_error;
3405 p++;
3406 h = strtol(p, (char **)&p, 10);
3407 if (h <= 0)
3408 goto graphic_error;
3409 if (*p == 'x') {
3410 p++;
3411 depth = strtol(p, (char **)&p, 10);
3412 if (depth != 8 && depth != 15 && depth != 16 &&
3413 depth != 24 && depth != 32)
3414 goto graphic_error;
3415 } else if (*p == '\0') {
3416 depth = graphic_depth;
3417 } else {
3418 goto graphic_error;
3421 graphic_width = w;
3422 graphic_height = h;
3423 graphic_depth = depth;
3425 break;
3426 case QEMU_OPTION_echr:
3428 char *r;
3429 term_escape_char = strtol(optarg, &r, 0);
3430 if (r == optarg)
3431 printf("Bad argument to echr\n");
3432 break;
3434 case QEMU_OPTION_monitor:
3435 default_monitor = 0;
3436 if (strncmp(optarg, "none", 4)) {
3437 monitor_parse(optarg, "readline", false);
3439 break;
3440 case QEMU_OPTION_qmp:
3441 monitor_parse(optarg, "control", false);
3442 default_monitor = 0;
3443 break;
3444 case QEMU_OPTION_qmp_pretty:
3445 monitor_parse(optarg, "control", true);
3446 default_monitor = 0;
3447 break;
3448 case QEMU_OPTION_mon:
3449 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3450 true);
3451 if (!opts) {
3452 exit(1);
3454 default_monitor = 0;
3455 break;
3456 case QEMU_OPTION_chardev:
3457 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3458 optarg, true);
3459 if (!opts) {
3460 exit(1);
3462 break;
3463 case QEMU_OPTION_fsdev:
3464 olist = qemu_find_opts("fsdev");
3465 if (!olist) {
3466 error_report("fsdev support is disabled");
3467 exit(1);
3469 opts = qemu_opts_parse_noisily(olist, optarg, true);
3470 if (!opts) {
3471 exit(1);
3473 break;
3474 case QEMU_OPTION_virtfs: {
3475 QemuOpts *fsdev;
3476 QemuOpts *device;
3477 const char *writeout, *sock_fd, *socket, *path, *security_model;
3479 olist = qemu_find_opts("virtfs");
3480 if (!olist) {
3481 error_report("virtfs support is disabled");
3482 exit(1);
3484 opts = qemu_opts_parse_noisily(olist, optarg, true);
3485 if (!opts) {
3486 exit(1);
3489 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3490 qemu_opt_get(opts, "mount_tag") == NULL) {
3491 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3492 exit(1);
3494 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3495 qemu_opts_id(opts) ?:
3496 qemu_opt_get(opts, "mount_tag"),
3497 1, NULL);
3498 if (!fsdev) {
3499 error_report("duplicate or invalid fsdev id: %s",
3500 qemu_opt_get(opts, "mount_tag"));
3501 exit(1);
3504 writeout = qemu_opt_get(opts, "writeout");
3505 if (writeout) {
3506 #ifdef CONFIG_SYNC_FILE_RANGE
3507 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3508 #else
3509 error_report("writeout=immediate not supported "
3510 "on this platform");
3511 exit(1);
3512 #endif
3514 qemu_opt_set(fsdev, "fsdriver",
3515 qemu_opt_get(opts, "fsdriver"), &error_abort);
3516 path = qemu_opt_get(opts, "path");
3517 if (path) {
3518 qemu_opt_set(fsdev, "path", path, &error_abort);
3520 security_model = qemu_opt_get(opts, "security_model");
3521 if (security_model) {
3522 qemu_opt_set(fsdev, "security_model", security_model,
3523 &error_abort);
3525 socket = qemu_opt_get(opts, "socket");
3526 if (socket) {
3527 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3529 sock_fd = qemu_opt_get(opts, "sock_fd");
3530 if (sock_fd) {
3531 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3534 qemu_opt_set_bool(fsdev, "readonly",
3535 qemu_opt_get_bool(opts, "readonly", 0),
3536 &error_abort);
3537 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3538 &error_abort);
3539 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3540 qemu_opt_set(device, "fsdev",
3541 qemu_opts_id(fsdev), &error_abort);
3542 qemu_opt_set(device, "mount_tag",
3543 qemu_opt_get(opts, "mount_tag"), &error_abort);
3544 break;
3546 case QEMU_OPTION_virtfs_synth: {
3547 QemuOpts *fsdev;
3548 QemuOpts *device;
3550 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3551 1, NULL);
3552 if (!fsdev) {
3553 error_report("duplicate option: %s", "virtfs_synth");
3554 exit(1);
3556 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3558 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3559 &error_abort);
3560 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3561 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3562 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3563 break;
3565 case QEMU_OPTION_serial:
3566 add_device_config(DEV_SERIAL, optarg);
3567 default_serial = 0;
3568 if (strncmp(optarg, "mon:", 4) == 0) {
3569 default_monitor = 0;
3571 break;
3572 case QEMU_OPTION_watchdog:
3573 if (watchdog) {
3574 error_report("only one watchdog option may be given");
3575 return 1;
3577 watchdog = optarg;
3578 break;
3579 case QEMU_OPTION_watchdog_action:
3580 if (select_watchdog_action(optarg) == -1) {
3581 error_report("unknown -watchdog-action parameter");
3582 exit(1);
3584 break;
3585 case QEMU_OPTION_virtiocon:
3586 warn_report("This option is deprecated, "
3587 "use '-device virtconsole' instead");
3588 add_device_config(DEV_VIRTCON, optarg);
3589 default_virtcon = 0;
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3593 break;
3594 case QEMU_OPTION_parallel:
3595 add_device_config(DEV_PARALLEL, optarg);
3596 default_parallel = 0;
3597 if (strncmp(optarg, "mon:", 4) == 0) {
3598 default_monitor = 0;
3600 break;
3601 case QEMU_OPTION_debugcon:
3602 add_device_config(DEV_DEBUGCON, optarg);
3603 break;
3604 case QEMU_OPTION_loadvm:
3605 loadvm = optarg;
3606 break;
3607 case QEMU_OPTION_full_screen:
3608 dpy.has_full_screen = true;
3609 dpy.full_screen = true;
3610 break;
3611 case QEMU_OPTION_no_frame:
3612 g_printerr("The -no-frame switch is deprecated, and will be\n"
3613 "removed in a future release.\n");
3614 no_frame = 1;
3615 break;
3616 case QEMU_OPTION_alt_grab:
3617 alt_grab = 1;
3618 break;
3619 case QEMU_OPTION_ctrl_grab:
3620 ctrl_grab = 1;
3621 break;
3622 case QEMU_OPTION_no_quit:
3623 dpy.has_window_close = true;
3624 dpy.window_close = false;
3625 break;
3626 case QEMU_OPTION_sdl:
3627 #ifdef CONFIG_SDL
3628 dpy.type = DISPLAY_TYPE_SDL;
3629 break;
3630 #else
3631 error_report("SDL support is disabled");
3632 exit(1);
3633 #endif
3634 case QEMU_OPTION_pidfile:
3635 pid_file = optarg;
3636 break;
3637 case QEMU_OPTION_win2k_hack:
3638 win2k_install_hack = 1;
3639 break;
3640 case QEMU_OPTION_acpitable:
3641 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3642 optarg, true);
3643 if (!opts) {
3644 exit(1);
3646 acpi_table_add(opts, &error_fatal);
3647 break;
3648 case QEMU_OPTION_smbios:
3649 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3650 optarg, false);
3651 if (!opts) {
3652 exit(1);
3654 smbios_entry_add(opts, &error_fatal);
3655 break;
3656 case QEMU_OPTION_fwcfg:
3657 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3658 optarg, true);
3659 if (opts == NULL) {
3660 exit(1);
3662 break;
3663 case QEMU_OPTION_preconfig:
3664 preconfig_exit_requested = false;
3665 break;
3666 case QEMU_OPTION_enable_kvm:
3667 olist = qemu_find_opts("machine");
3668 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3669 break;
3670 case QEMU_OPTION_enable_hax:
3671 warn_report("Option is deprecated, use '-accel hax' instead");
3672 olist = qemu_find_opts("machine");
3673 qemu_opts_parse_noisily(olist, "accel=hax", false);
3674 break;
3675 case QEMU_OPTION_M:
3676 case QEMU_OPTION_machine:
3677 olist = qemu_find_opts("machine");
3678 opts = qemu_opts_parse_noisily(olist, optarg, true);
3679 if (!opts) {
3680 exit(1);
3682 break;
3683 case QEMU_OPTION_no_kvm:
3684 olist = qemu_find_opts("machine");
3685 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3686 break;
3687 case QEMU_OPTION_accel:
3688 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3689 optarg, true);
3690 optarg = qemu_opt_get(accel_opts, "accel");
3691 if (!optarg || is_help_option(optarg)) {
3692 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3693 exit(0);
3695 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3696 false, &error_abort);
3697 qemu_opt_set(opts, "accel", optarg, &error_abort);
3698 break;
3699 case QEMU_OPTION_usb:
3700 olist = qemu_find_opts("machine");
3701 qemu_opts_parse_noisily(olist, "usb=on", false);
3702 break;
3703 case QEMU_OPTION_usbdevice:
3704 error_report("'-usbdevice' is deprecated, please use "
3705 "'-device usb-...' instead");
3706 olist = qemu_find_opts("machine");
3707 qemu_opts_parse_noisily(olist, "usb=on", false);
3708 add_device_config(DEV_USB, optarg);
3709 break;
3710 case QEMU_OPTION_device:
3711 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3712 optarg, true)) {
3713 exit(1);
3715 break;
3716 case QEMU_OPTION_smp:
3717 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3718 optarg, true)) {
3719 exit(1);
3721 break;
3722 case QEMU_OPTION_vnc:
3723 vnc_parse(optarg, &error_fatal);
3724 break;
3725 case QEMU_OPTION_no_acpi:
3726 acpi_enabled = 0;
3727 break;
3728 case QEMU_OPTION_no_hpet:
3729 no_hpet = 1;
3730 break;
3731 case QEMU_OPTION_no_reboot:
3732 no_reboot = 1;
3733 break;
3734 case QEMU_OPTION_no_shutdown:
3735 no_shutdown = 1;
3736 break;
3737 case QEMU_OPTION_show_cursor:
3738 cursor_hide = 0;
3739 break;
3740 case QEMU_OPTION_uuid:
3741 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3742 error_report("failed to parse UUID string: wrong format");
3743 exit(1);
3745 qemu_uuid_set = true;
3746 break;
3747 case QEMU_OPTION_option_rom:
3748 if (nb_option_roms >= MAX_OPTION_ROMS) {
3749 error_report("too many option ROMs");
3750 exit(1);
3752 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3753 optarg, true);
3754 if (!opts) {
3755 exit(1);
3757 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3758 option_rom[nb_option_roms].bootindex =
3759 qemu_opt_get_number(opts, "bootindex", -1);
3760 if (!option_rom[nb_option_roms].name) {
3761 error_report("Option ROM file is not specified");
3762 exit(1);
3764 nb_option_roms++;
3765 break;
3766 case QEMU_OPTION_semihosting:
3767 semihosting.enabled = true;
3768 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3769 break;
3770 case QEMU_OPTION_semihosting_config:
3771 semihosting.enabled = true;
3772 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3773 optarg, false);
3774 if (opts != NULL) {
3775 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3776 true);
3777 const char *target = qemu_opt_get(opts, "target");
3778 if (target != NULL) {
3779 if (strcmp("native", target) == 0) {
3780 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3781 } else if (strcmp("gdb", target) == 0) {
3782 semihosting.target = SEMIHOSTING_TARGET_GDB;
3783 } else if (strcmp("auto", target) == 0) {
3784 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3785 } else {
3786 error_report("unsupported semihosting-config %s",
3787 optarg);
3788 exit(1);
3790 } else {
3791 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3793 /* Set semihosting argument count and vector */
3794 qemu_opt_foreach(opts, add_semihosting_arg,
3795 &semihosting, NULL);
3796 } else {
3797 error_report("unsupported semihosting-config %s", optarg);
3798 exit(1);
3800 break;
3801 case QEMU_OPTION_name:
3802 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3803 optarg, true);
3804 if (!opts) {
3805 exit(1);
3807 break;
3808 case QEMU_OPTION_prom_env:
3809 if (nb_prom_envs >= MAX_PROM_ENVS) {
3810 error_report("too many prom variables");
3811 exit(1);
3813 prom_envs[nb_prom_envs] = optarg;
3814 nb_prom_envs++;
3815 break;
3816 case QEMU_OPTION_old_param:
3817 old_param = 1;
3818 break;
3819 case QEMU_OPTION_clock:
3820 /* Clock options no longer exist. Keep this option for
3821 * backward compatibility.
3823 warn_report("This option is ignored and will be removed soon");
3824 break;
3825 case QEMU_OPTION_rtc:
3826 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3827 false);
3828 if (!opts) {
3829 exit(1);
3831 break;
3832 case QEMU_OPTION_tb_size:
3833 #ifndef CONFIG_TCG
3834 error_report("TCG is disabled");
3835 exit(1);
3836 #endif
3837 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3838 error_report("Invalid argument to -tb-size");
3839 exit(1);
3841 break;
3842 case QEMU_OPTION_icount:
3843 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3844 optarg, true);
3845 if (!icount_opts) {
3846 exit(1);
3848 break;
3849 case QEMU_OPTION_incoming:
3850 if (!incoming) {
3851 runstate_set(RUN_STATE_INMIGRATE);
3853 incoming = optarg;
3854 break;
3855 case QEMU_OPTION_only_migratable:
3857 * TODO: we can remove this option one day, and we
3858 * should all use:
3860 * "-global migration.only-migratable=true"
3862 qemu_global_option("migration.only-migratable=true");
3863 break;
3864 case QEMU_OPTION_nodefaults:
3865 has_defaults = 0;
3866 break;
3867 case QEMU_OPTION_xen_domid:
3868 if (!(xen_available())) {
3869 error_report("Option not supported for this target");
3870 exit(1);
3872 xen_domid = atoi(optarg);
3873 break;
3874 case QEMU_OPTION_xen_create:
3875 if (!(xen_available())) {
3876 error_report("Option not supported for this target");
3877 exit(1);
3879 xen_mode = XEN_CREATE;
3880 break;
3881 case QEMU_OPTION_xen_attach:
3882 if (!(xen_available())) {
3883 error_report("Option not supported for this target");
3884 exit(1);
3886 xen_mode = XEN_ATTACH;
3887 break;
3888 case QEMU_OPTION_xen_domid_restrict:
3889 if (!(xen_available())) {
3890 error_report("Option not supported for this target");
3891 exit(1);
3893 xen_domid_restrict = true;
3894 break;
3895 case QEMU_OPTION_trace:
3896 g_free(trace_file);
3897 trace_file = trace_opt_parse(optarg);
3898 break;
3899 case QEMU_OPTION_trace_unassigned:
3900 trace_unassigned = true;
3901 break;
3902 case QEMU_OPTION_readconfig:
3904 int ret = qemu_read_config_file(optarg);
3905 if (ret < 0) {
3906 error_report("read config %s: %s", optarg,
3907 strerror(-ret));
3908 exit(1);
3910 break;
3912 case QEMU_OPTION_spice:
3913 olist = qemu_find_opts("spice");
3914 if (!olist) {
3915 error_report("spice support is disabled");
3916 exit(1);
3918 opts = qemu_opts_parse_noisily(olist, optarg, false);
3919 if (!opts) {
3920 exit(1);
3922 display_remote++;
3923 break;
3924 case QEMU_OPTION_writeconfig:
3926 FILE *fp;
3927 if (strcmp(optarg, "-") == 0) {
3928 fp = stdout;
3929 } else {
3930 fp = fopen(optarg, "w");
3931 if (fp == NULL) {
3932 error_report("open %s: %s", optarg,
3933 strerror(errno));
3934 exit(1);
3937 qemu_config_write(fp);
3938 if (fp != stdout) {
3939 fclose(fp);
3941 break;
3943 case QEMU_OPTION_qtest:
3944 qtest_chrdev = optarg;
3945 break;
3946 case QEMU_OPTION_qtest_log:
3947 qtest_log = optarg;
3948 break;
3949 case QEMU_OPTION_sandbox:
3950 #ifdef CONFIG_SECCOMP
3951 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3952 optarg, true);
3953 if (!opts) {
3954 exit(1);
3956 #else
3957 error_report("-sandbox support is not enabled "
3958 "in this QEMU binary");
3959 exit(1);
3960 #endif
3961 break;
3962 case QEMU_OPTION_add_fd:
3963 #ifndef _WIN32
3964 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3965 optarg, false);
3966 if (!opts) {
3967 exit(1);
3969 #else
3970 error_report("File descriptor passing is disabled on this "
3971 "platform");
3972 exit(1);
3973 #endif
3974 break;
3975 case QEMU_OPTION_object:
3976 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3977 optarg, true);
3978 if (!opts) {
3979 exit(1);
3981 break;
3982 case QEMU_OPTION_realtime:
3983 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3984 optarg, false);
3985 if (!opts) {
3986 exit(1);
3988 /* Don't override the -overcommit option if set */
3989 enable_mlock = enable_mlock ||
3990 qemu_opt_get_bool(opts, "mlock", true);
3991 break;
3992 case QEMU_OPTION_overcommit:
3993 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3994 optarg, false);
3995 if (!opts) {
3996 exit(1);
3998 /* Don't override the -realtime option if set */
3999 enable_mlock = enable_mlock ||
4000 qemu_opt_get_bool(opts, "mem-lock", false);
4001 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
4002 break;
4003 case QEMU_OPTION_msg:
4004 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4005 false);
4006 if (!opts) {
4007 exit(1);
4009 configure_msg(opts);
4010 break;
4011 case QEMU_OPTION_dump_vmstate:
4012 if (vmstate_dump_file) {
4013 error_report("only one '-dump-vmstate' "
4014 "option may be given");
4015 exit(1);
4017 vmstate_dump_file = fopen(optarg, "w");
4018 if (vmstate_dump_file == NULL) {
4019 error_report("open %s: %s", optarg, strerror(errno));
4020 exit(1);
4022 break;
4023 case QEMU_OPTION_enable_sync_profile:
4024 qsp_enable();
4025 break;
4026 case QEMU_OPTION_nouserconfig:
4027 /* Nothing to be parsed here. Especially, do not error out below. */
4028 break;
4029 default:
4030 if (os_parse_cmd_args(popt->index, optarg)) {
4031 error_report("Option not supported in this build");
4032 exit(1);
4038 * Clear error location left behind by the loop.
4039 * Best done right after the loop. Do not insert code here!
4041 loc_set_none();
4043 replay_configure(icount_opts);
4045 if (incoming && !preconfig_exit_requested) {
4046 error_report("'preconfig' and 'incoming' options are "
4047 "mutually exclusive");
4048 exit(EXIT_FAILURE);
4051 configure_rtc(qemu_find_opts_singleton("rtc"));
4053 machine_class = select_machine();
4055 set_memory_options(&ram_slots, &maxram_size, machine_class);
4057 os_daemonize();
4058 rcu_disable_atfork();
4060 if (pid_file && !qemu_write_pidfile(pid_file, &err)) {
4061 error_reportf_err(err, "cannot create PID file: ");
4062 exit(1);
4065 qemu_unlink_pidfile_notifier.notify = qemu_unlink_pidfile;
4066 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier);
4068 if (qemu_init_main_loop(&main_loop_err)) {
4069 error_report_err(main_loop_err);
4070 exit(1);
4073 #ifdef CONFIG_SECCOMP
4074 olist = qemu_find_opts_err("sandbox", NULL);
4075 if (olist) {
4076 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
4078 #endif
4080 qemu_opts_foreach(qemu_find_opts("name"),
4081 parse_name, NULL, &error_fatal);
4083 #ifndef _WIN32
4084 qemu_opts_foreach(qemu_find_opts("add-fd"),
4085 parse_add_fd, NULL, &error_fatal);
4087 qemu_opts_foreach(qemu_find_opts("add-fd"),
4088 cleanup_add_fd, NULL, &error_fatal);
4089 #endif
4091 current_machine = MACHINE(object_new(object_class_get_name(
4092 OBJECT_CLASS(machine_class))));
4093 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4094 exit(0);
4096 object_property_add_child(object_get_root(), "machine",
4097 OBJECT(current_machine), &error_abort);
4099 if (machine_class->minimum_page_bits) {
4100 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4101 /* This would be a board error: specifying a minimum smaller than
4102 * a target's compile-time fixed setting.
4104 g_assert_not_reached();
4108 cpu_exec_init_all();
4110 if (machine_class->hw_version) {
4111 qemu_set_hw_version(machine_class->hw_version);
4114 if (cpu_model && is_help_option(cpu_model)) {
4115 list_cpus(stdout, &fprintf, cpu_model);
4116 exit(0);
4119 if (!trace_init_backends()) {
4120 exit(1);
4122 trace_init_file(trace_file);
4124 /* Open the logfile at this point and set the log mask if necessary.
4126 if (log_file) {
4127 qemu_set_log_filename(log_file, &error_fatal);
4130 if (log_mask) {
4131 int mask;
4132 mask = qemu_str_to_log_mask(log_mask);
4133 if (!mask) {
4134 qemu_print_log_usage(stdout);
4135 exit(1);
4137 qemu_set_log(mask);
4138 } else {
4139 qemu_set_log(0);
4142 /* add configured firmware directories */
4143 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4144 for (i = 0; dirs[i] != NULL; i++) {
4145 qemu_add_data_dir(dirs[i]);
4147 g_strfreev(dirs);
4149 /* try to find datadir relative to the executable path */
4150 dir = os_find_datadir();
4151 qemu_add_data_dir(dir);
4152 g_free(dir);
4154 /* add the datadir specified when building */
4155 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4157 /* -L help lists the data directories and exits. */
4158 if (list_data_dirs) {
4159 for (i = 0; i < data_dir_idx; i++) {
4160 printf("%s\n", data_dir[i]);
4162 exit(0);
4165 /* machine_class: default to UP */
4166 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4167 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4168 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4170 /* default to machine_class->default_cpus */
4171 smp_cpus = machine_class->default_cpus;
4172 max_cpus = machine_class->default_cpus;
4174 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4176 /* sanity-check smp_cpus and max_cpus against machine_class */
4177 if (smp_cpus < machine_class->min_cpus) {
4178 error_report("Invalid SMP CPUs %d. The min CPUs "
4179 "supported by machine '%s' is %d", smp_cpus,
4180 machine_class->name, machine_class->min_cpus);
4181 exit(1);
4183 if (max_cpus > machine_class->max_cpus) {
4184 error_report("Invalid SMP CPUs %d. The max CPUs "
4185 "supported by machine '%s' is %d", max_cpus,
4186 machine_class->name, machine_class->max_cpus);
4187 exit(1);
4191 * Get the default machine options from the machine if it is not already
4192 * specified either by the configuration file or by the command line.
4194 if (machine_class->default_machine_opts) {
4195 qemu_opts_set_defaults(qemu_find_opts("machine"),
4196 machine_class->default_machine_opts, 0);
4199 qemu_opts_foreach(qemu_find_opts("device"),
4200 default_driver_check, NULL, NULL);
4201 qemu_opts_foreach(qemu_find_opts("global"),
4202 default_driver_check, NULL, NULL);
4204 if (!vga_model && !default_vga) {
4205 vga_interface_type = VGA_DEVICE;
4207 if (!has_defaults || machine_class->no_serial) {
4208 default_serial = 0;
4210 if (!has_defaults || machine_class->no_parallel) {
4211 default_parallel = 0;
4213 if (!has_defaults || !machine_class->use_virtcon) {
4214 default_virtcon = 0;
4216 if (!has_defaults || machine_class->no_floppy) {
4217 default_floppy = 0;
4219 if (!has_defaults || machine_class->no_cdrom) {
4220 default_cdrom = 0;
4222 if (!has_defaults || machine_class->no_sdcard) {
4223 default_sdcard = 0;
4225 if (!has_defaults) {
4226 default_monitor = 0;
4227 default_net = 0;
4228 default_vga = 0;
4231 if (is_daemonized()) {
4232 if (!preconfig_exit_requested) {
4233 error_report("'preconfig' and 'daemonize' options are "
4234 "mutually exclusive");
4235 exit(EXIT_FAILURE);
4238 /* According to documentation and historically, -nographic redirects
4239 * serial port, parallel port and monitor to stdio, which does not work
4240 * with -daemonize. We can redirect these to null instead, but since
4241 * -nographic is legacy, let's just error out.
4242 * We disallow -nographic only if all other ports are not redirected
4243 * explicitly, to not break existing legacy setups which uses
4244 * -nographic _and_ redirects all ports explicitly - this is valid
4245 * usage, -nographic is just a no-op in this case.
4247 if (nographic
4248 && (default_parallel || default_serial
4249 || default_monitor || default_virtcon)) {
4250 error_report("-nographic cannot be used with -daemonize");
4251 exit(1);
4253 #ifdef CONFIG_CURSES
4254 if (dpy.type == DISPLAY_TYPE_CURSES) {
4255 error_report("curses display cannot be used with -daemonize");
4256 exit(1);
4258 #endif
4261 if (nographic) {
4262 if (default_parallel)
4263 add_device_config(DEV_PARALLEL, "null");
4264 if (default_serial && default_monitor) {
4265 add_device_config(DEV_SERIAL, "mon:stdio");
4266 } else if (default_virtcon && default_monitor) {
4267 add_device_config(DEV_VIRTCON, "mon:stdio");
4268 } else {
4269 if (default_serial)
4270 add_device_config(DEV_SERIAL, "stdio");
4271 if (default_virtcon)
4272 add_device_config(DEV_VIRTCON, "stdio");
4273 if (default_monitor)
4274 monitor_parse("stdio", "readline", false);
4276 } else {
4277 if (default_serial)
4278 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4279 if (default_parallel)
4280 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4281 if (default_monitor)
4282 monitor_parse("vc:80Cx24C", "readline", false);
4283 if (default_virtcon)
4284 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4287 #if defined(CONFIG_VNC)
4288 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4289 display_remote++;
4291 #endif
4292 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4293 if (!qemu_display_find_default(&dpy)) {
4294 dpy.type = DISPLAY_TYPE_NONE;
4295 #if defined(CONFIG_VNC)
4296 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4297 #endif
4300 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4301 dpy.type = DISPLAY_TYPE_NONE;
4304 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4305 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4306 "for SDL, ignoring option");
4308 if (dpy.has_window_close &&
4309 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4310 error_report("-no-quit is only valid for GTK and SDL, "
4311 "ignoring option");
4314 qemu_display_early_init(&dpy);
4315 qemu_console_early_init();
4317 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4318 #if defined(CONFIG_OPENGL)
4319 error_report("OpenGL is not supported by the display");
4320 #else
4321 error_report("OpenGL support is disabled");
4322 #endif
4323 exit(1);
4326 page_size_init();
4327 socket_init();
4329 qemu_opts_foreach(qemu_find_opts("object"),
4330 user_creatable_add_opts_foreach,
4331 object_create_initial, &error_fatal);
4333 qemu_opts_foreach(qemu_find_opts("chardev"),
4334 chardev_init_func, NULL, &error_fatal);
4336 #ifdef CONFIG_VIRTFS
4337 qemu_opts_foreach(qemu_find_opts("fsdev"),
4338 fsdev_init_func, NULL, &error_fatal);
4339 #endif
4341 if (qemu_opts_foreach(qemu_find_opts("device"),
4342 device_help_func, NULL, NULL)) {
4343 exit(0);
4346 machine_opts = qemu_get_machine_opts();
4347 qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4348 &error_fatal);
4350 configure_accelerator(current_machine);
4352 if (!qtest_enabled() && machine_class->deprecation_reason) {
4353 error_report("Machine type '%s' is deprecated: %s",
4354 machine_class->name, machine_class->deprecation_reason);
4358 * Register all the global properties, including accel properties,
4359 * machine properties, and user-specified ones.
4361 register_global_properties(current_machine);
4364 * Migration object can only be created after global properties
4365 * are applied correctly.
4367 migration_object_init();
4369 if (qtest_chrdev) {
4370 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4373 machine_opts = qemu_get_machine_opts();
4374 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4375 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4376 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4377 bios_name = qemu_opt_get(machine_opts, "firmware");
4379 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4380 if (opts) {
4381 boot_order = qemu_opt_get(opts, "order");
4382 if (boot_order) {
4383 validate_bootdevices(boot_order, &error_fatal);
4386 boot_once = qemu_opt_get(opts, "once");
4387 if (boot_once) {
4388 validate_bootdevices(boot_once, &error_fatal);
4391 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4392 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4395 if (!boot_order) {
4396 boot_order = machine_class->default_boot_order;
4399 if (!kernel_cmdline) {
4400 kernel_cmdline = "";
4401 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4404 linux_boot = (kernel_filename != NULL);
4406 if (!linux_boot && *kernel_cmdline != '\0') {
4407 error_report("-append only allowed with -kernel option");
4408 exit(1);
4411 if (!linux_boot && initrd_filename != NULL) {
4412 error_report("-initrd only allowed with -kernel option");
4413 exit(1);
4416 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4417 /* fall back to the -kernel/-append */
4418 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4421 os_set_line_buffering();
4423 /* spice needs the timers to be initialized by this point */
4424 qemu_spice_init();
4426 cpu_ticks_init();
4427 if (icount_opts) {
4428 if (!tcg_enabled()) {
4429 error_report("-icount is not allowed with hardware virtualization");
4430 exit(1);
4432 configure_icount(icount_opts, &error_abort);
4433 qemu_opts_del(icount_opts);
4436 if (tcg_enabled()) {
4437 qemu_tcg_configure(accel_opts, &error_fatal);
4440 if (default_net) {
4441 QemuOptsList *net = qemu_find_opts("net");
4442 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4443 #ifdef CONFIG_SLIRP
4444 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4445 #endif
4448 if (net_init_clients(&err) < 0) {
4449 error_report_err(err);
4450 exit(1);
4453 qemu_opts_foreach(qemu_find_opts("object"),
4454 user_creatable_add_opts_foreach,
4455 object_create_delayed, &error_fatal);
4457 tpm_init();
4459 /* init the bluetooth world */
4460 if (foreach_device_config(DEV_BT, bt_parse))
4461 exit(1);
4463 if (!xen_enabled()) {
4464 /* On 32-bit hosts, QEMU is limited by virtual address space */
4465 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4466 error_report("at most 2047 MB RAM can be simulated");
4467 exit(1);
4471 blk_mig_init();
4472 ram_mig_init();
4473 dirty_bitmap_mig_init();
4475 /* If the currently selected machine wishes to override the units-per-bus
4476 * property of its default HBA interface type, do so now. */
4477 if (machine_class->units_per_default_bus) {
4478 override_max_devs(machine_class->block_default_type,
4479 machine_class->units_per_default_bus);
4482 /* open the virtual block devices */
4483 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4484 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4486 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4487 loc_push_restore(&bdo->loc);
4488 qmp_blockdev_add(bdo->bdo, &error_fatal);
4489 loc_pop(&bdo->loc);
4490 qapi_free_BlockdevOptions(bdo->bdo);
4491 g_free(bdo);
4493 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4494 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4495 NULL, NULL);
4497 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4498 &machine_class->block_default_type, &error_fatal)) {
4499 /* We printed help */
4500 exit(0);
4503 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4504 CDROM_OPTS);
4505 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4506 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4508 qemu_opts_foreach(qemu_find_opts("mon"),
4509 mon_init_func, NULL, &error_fatal);
4511 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4512 exit(1);
4513 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4514 exit(1);
4515 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4516 exit(1);
4517 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4518 exit(1);
4520 /* If no default VGA is requested, the default is "none". */
4521 if (default_vga) {
4522 if (machine_class->default_display) {
4523 vga_model = machine_class->default_display;
4524 } else if (vga_interface_available(VGA_CIRRUS)) {
4525 vga_model = "cirrus";
4526 } else if (vga_interface_available(VGA_STD)) {
4527 vga_model = "std";
4530 if (vga_model) {
4531 select_vgahw(vga_model);
4534 if (watchdog) {
4535 i = select_watchdog(watchdog);
4536 if (i > 0)
4537 exit (i == 1 ? 1 : 0);
4540 /* This checkpoint is required by replay to separate prior clock
4541 reading from the other reads, because timer polling functions query
4542 clock values from the log. */
4543 replay_checkpoint(CHECKPOINT_INIT);
4544 qdev_machine_init();
4546 current_machine->ram_size = ram_size;
4547 current_machine->maxram_size = maxram_size;
4548 current_machine->ram_slots = ram_slots;
4549 current_machine->boot_order = boot_order;
4551 /* parse features once if machine provides default cpu_type */
4552 current_machine->cpu_type = machine_class->default_cpu_type;
4553 if (cpu_model) {
4554 current_machine->cpu_type = parse_cpu_model(cpu_model);
4556 parse_numa_opts(current_machine);
4558 /* do monitor/qmp handling at preconfig state if requested */
4559 main_loop();
4561 /* from here on runstate is RUN_STATE_PRELAUNCH */
4562 machine_run_board_init(current_machine);
4564 realtime_init();
4566 soundhw_init();
4568 if (hax_enabled()) {
4569 hax_sync_vcpus();
4572 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4573 parse_fw_cfg, fw_cfg_find(), &error_fatal);
4575 /* init USB devices */
4576 if (machine_usb(current_machine)) {
4577 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4578 exit(1);
4581 /* Check if IGD GFX passthrough. */
4582 igd_gfx_passthru();
4584 /* init generic devices */
4585 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4586 qemu_opts_foreach(qemu_find_opts("device"),
4587 device_init_func, NULL, &error_fatal);
4589 cpu_synchronize_all_post_init();
4591 rom_reset_order_override();
4593 /* Did we create any drives that we failed to create a device for? */
4594 drive_check_orphaned();
4596 /* Don't warn about the default network setup that you get if
4597 * no command line -net or -netdev options are specified. There
4598 * are two cases that we would otherwise complain about:
4599 * (1) board doesn't support a NIC but the implicit "-net nic"
4600 * requested one
4601 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4602 * sets up a nic that isn't connected to anything.
4604 if (!default_net && (!qtest_enabled() || has_defaults)) {
4605 net_check_clients();
4608 if (boot_once) {
4609 qemu_boot_set(boot_once, &error_fatal);
4610 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4613 /* init local displays */
4614 ds = init_displaystate();
4615 qemu_display_init(ds, &dpy);
4617 /* must be after terminal init, SDL library changes signal handlers */
4618 os_setup_signal_handling();
4620 /* init remote displays */
4621 #ifdef CONFIG_VNC
4622 qemu_opts_foreach(qemu_find_opts("vnc"),
4623 vnc_init_func, NULL, &error_fatal);
4624 #endif
4626 if (using_spice) {
4627 qemu_spice_display_init();
4630 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4631 exit(1);
4634 qdev_machine_creation_done();
4636 /* TODO: once all bus devices are qdevified, this should be done
4637 * when bus is created by qdev.c */
4638 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4639 qemu_run_machine_init_done_notifiers();
4641 if (rom_check_and_register_reset() != 0) {
4642 error_report("rom check and register reset failed");
4643 exit(1);
4646 replay_start();
4648 /* This checkpoint is required by replay to separate prior clock
4649 reading from the other reads, because timer polling functions query
4650 clock values from the log. */
4651 replay_checkpoint(CHECKPOINT_RESET);
4652 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4653 register_global_state();
4654 if (loadvm) {
4655 Error *local_err = NULL;
4656 if (load_snapshot(loadvm, &local_err) < 0) {
4657 error_report_err(local_err);
4658 autostart = 0;
4659 exit(1);
4662 if (replay_mode != REPLAY_MODE_NONE) {
4663 replay_vmstate_init();
4666 qdev_prop_check_globals();
4667 if (vmstate_dump_file) {
4668 /* dump and exit */
4669 dump_vmstate_json_to_file(vmstate_dump_file);
4670 return 0;
4673 if (incoming) {
4674 Error *local_err = NULL;
4675 qemu_start_incoming_migration(incoming, &local_err);
4676 if (local_err) {
4677 error_reportf_err(local_err, "-incoming %s: ", incoming);
4678 exit(1);
4680 } else if (autostart) {
4681 vm_start();
4684 accel_setup_post(current_machine);
4685 os_setup_post();
4687 main_loop();
4689 gdbserver_cleanup();
4691 /* No more vcpu or device emulation activity beyond this point */
4692 vm_shutdown();
4694 job_cancel_sync_all();
4695 bdrv_close_all();
4697 res_free();
4699 /* vhost-user must be cleaned up before chardevs. */
4700 tpm_cleanup();
4701 net_cleanup();
4702 audio_cleanup();
4703 monitor_cleanup();
4704 qemu_chr_cleanup();
4705 user_creatable_cleanup();
4706 migration_object_finalize();
4707 /* TODO: unref root container, check all devices are ok */
4709 return 0;