ppc440_uc: Fix unintialized variable warning with older gcc
[qemu/ar7.git] / vl.c
blob6120b889ed418eaccab5ca4942e1d2cb0d921357
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 "qapi/error.h"
27 #include "qemu-version.h"
28 #include "qemu/cutils.h"
29 #include "qemu/help_option.h"
30 #include "qemu/uuid.h"
32 #ifdef CONFIG_SECCOMP
33 #include <sys/prctl.h>
34 #include "sysemu/seccomp.h"
35 #endif
37 #ifdef CONFIG_SDL
38 #if defined(__APPLE__) || defined(main)
39 #include <SDL.h>
40 int qemu_main(int argc, char **argv, char **envp);
41 int main(int argc, char **argv)
43 return qemu_main(argc, argv, NULL);
45 #undef main
46 #define main qemu_main
47 #endif
48 #endif /* CONFIG_SDL */
50 #ifdef CONFIG_COCOA
51 #undef main
52 #define main qemu_main
53 #endif /* CONFIG_COCOA */
56 #include "qemu/error-report.h"
57 #include "qemu/sockets.h"
58 #include "hw/hw.h"
59 #include "hw/boards.h"
60 #include "sysemu/accel.h"
61 #include "hw/usb.h"
62 #include "hw/isa/isa.h"
63 #include "hw/scsi/scsi.h"
64 #include "hw/display/vga.h"
65 #include "hw/bt.h"
66 #include "sysemu/watchdog.h"
67 #include "hw/smbios/smbios.h"
68 #include "hw/acpi/acpi.h"
69 #include "hw/xen/xen.h"
70 #include "hw/qdev.h"
71 #include "hw/loader.h"
72 #include "monitor/qdev.h"
73 #include "sysemu/bt.h"
74 #include "net/net.h"
75 #include "net/slirp.h"
76 #include "monitor/monitor.h"
77 #include "ui/console.h"
78 #include "ui/input.h"
79 #include "sysemu/sysemu.h"
80 #include "sysemu/numa.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "chardev/char.h"
84 #include "qemu/bitmap.h"
85 #include "qemu/log.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/misc.h"
89 #include "migration/snapshot.h"
90 #include "migration/global_state.h"
91 #include "sysemu/tpm.h"
92 #include "sysemu/dma.h"
93 #include "hw/audio/soundhw.h"
94 #include "audio/audio.h"
95 #include "sysemu/cpus.h"
96 #include "migration/colo.h"
97 #include "sysemu/kvm.h"
98 #include "sysemu/hax.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qemu/option.h"
101 #include "qemu/config-file.h"
102 #include "qemu-options.h"
103 #include "qemu/main-loop.h"
104 #ifdef CONFIG_VIRTFS
105 #include "fsdev/qemu-fsdev.h"
106 #endif
107 #include "sysemu/qtest.h"
109 #include "disas/disas.h"
112 #include "slirp/libslirp.h"
114 #include "trace-root.h"
115 #include "trace/control.h"
116 #include "qemu/queue.h"
117 #include "sysemu/arch_init.h"
119 #include "ui/qemu-spice.h"
120 #include "qapi/string-input-visitor.h"
121 #include "qapi/opts-visitor.h"
122 #include "qom/object_interfaces.h"
123 #include "exec/semihost.h"
124 #include "crypto/init.h"
125 #include "sysemu/replay.h"
126 #include "qapi/qapi-events-run-state.h"
127 #include "qapi/qapi-visit-block-core.h"
128 #include "qapi/qapi-commands-block-core.h"
129 #include "qapi/qapi-commands-misc.h"
130 #include "qapi/qapi-commands-run-state.h"
131 #include "qapi/qmp/qerror.h"
132 #include "sysemu/iothread.h"
134 #define MAX_VIRTIO_CONSOLES 1
135 #define MAX_SCLP_CONSOLES 1
137 static const char *data_dir[16];
138 static int data_dir_idx;
139 const char *bios_name = NULL;
140 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
141 int display_opengl;
142 const char* keyboard_layout = NULL;
143 ram_addr_t ram_size;
144 const char *mem_path = NULL;
145 int mem_prealloc = 0; /* force preallocation of physical target memory */
146 bool enable_mlock = false;
147 int nb_nics;
148 NICInfo nd_table[MAX_NICS];
149 int autostart;
150 static int rtc_utc = 1;
151 static int rtc_date_offset = -1; /* -1 means no change */
152 QEMUClockType rtc_clock;
153 int vga_interface_type = VGA_NONE;
154 static DisplayOptions dpy;
155 int no_frame;
156 Chardev *serial_hds[MAX_SERIAL_PORTS];
157 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
158 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
159 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
160 int win2k_install_hack = 0;
161 int singlestep = 0;
162 int smp_cpus;
163 unsigned int max_cpus;
164 int smp_cores = 1;
165 int smp_threads = 1;
166 int acpi_enabled = 1;
167 int no_hpet = 0;
168 int fd_bootchk = 1;
169 static int no_reboot;
170 int no_shutdown = 0;
171 int cursor_hide = 1;
172 int graphic_rotate = 0;
173 const char *watchdog;
174 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
175 int nb_option_roms;
176 int old_param = 0;
177 const char *qemu_name;
178 int alt_grab = 0;
179 int ctrl_grab = 0;
180 unsigned int nb_prom_envs = 0;
181 const char *prom_envs[MAX_PROM_ENVS];
182 int boot_menu;
183 bool boot_strict;
184 uint8_t *boot_splash_filedata;
185 size_t boot_splash_filedata_size;
186 uint8_t qemu_extra_params_fw[2];
188 int icount_align_option;
190 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
191 * little-endian "wire format" described in the SMBIOS 2.6 specification.
193 QemuUUID qemu_uuid;
194 bool qemu_uuid_set;
196 static NotifierList exit_notifiers =
197 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
199 static NotifierList machine_init_done_notifiers =
200 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
202 bool xen_allowed;
203 uint32_t xen_domid;
204 enum xen_mode xen_mode = XEN_EMULATE;
205 bool xen_domid_restrict;
207 static int has_defaults = 1;
208 static int default_serial = 1;
209 static int default_parallel = 1;
210 static int default_virtcon = 1;
211 static int default_sclp = 1;
212 static int default_monitor = 1;
213 static int default_floppy = 1;
214 static int default_cdrom = 1;
215 static int default_sdcard = 1;
216 static int default_vga = 1;
217 static int default_net = 1;
219 static struct {
220 const char *driver;
221 int *flag;
222 } default_list[] = {
223 { .driver = "isa-serial", .flag = &default_serial },
224 { .driver = "isa-parallel", .flag = &default_parallel },
225 { .driver = "isa-fdc", .flag = &default_floppy },
226 { .driver = "floppy", .flag = &default_floppy },
227 { .driver = "ide-cd", .flag = &default_cdrom },
228 { .driver = "ide-hd", .flag = &default_cdrom },
229 { .driver = "ide-drive", .flag = &default_cdrom },
230 { .driver = "scsi-cd", .flag = &default_cdrom },
231 { .driver = "scsi-hd", .flag = &default_cdrom },
232 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
233 { .driver = "virtio-serial", .flag = &default_virtcon },
234 { .driver = "VGA", .flag = &default_vga },
235 { .driver = "isa-vga", .flag = &default_vga },
236 { .driver = "cirrus-vga", .flag = &default_vga },
237 { .driver = "isa-cirrus-vga", .flag = &default_vga },
238 { .driver = "vmware-svga", .flag = &default_vga },
239 { .driver = "qxl-vga", .flag = &default_vga },
240 { .driver = "virtio-vga", .flag = &default_vga },
243 static QemuOptsList qemu_rtc_opts = {
244 .name = "rtc",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
246 .desc = {
248 .name = "base",
249 .type = QEMU_OPT_STRING,
251 .name = "clock",
252 .type = QEMU_OPT_STRING,
254 .name = "driftfix",
255 .type = QEMU_OPT_STRING,
257 { /* end of list */ }
261 static QemuOptsList qemu_sandbox_opts = {
262 .name = "sandbox",
263 .implied_opt_name = "enable",
264 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
265 .desc = {
267 .name = "enable",
268 .type = QEMU_OPT_BOOL,
271 .name = "obsolete",
272 .type = QEMU_OPT_STRING,
275 .name = "elevateprivileges",
276 .type = QEMU_OPT_STRING,
279 .name = "spawn",
280 .type = QEMU_OPT_STRING,
283 .name = "resourcecontrol",
284 .type = QEMU_OPT_STRING,
286 { /* end of list */ }
290 static QemuOptsList qemu_option_rom_opts = {
291 .name = "option-rom",
292 .implied_opt_name = "romfile",
293 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
294 .desc = {
296 .name = "bootindex",
297 .type = QEMU_OPT_NUMBER,
298 }, {
299 .name = "romfile",
300 .type = QEMU_OPT_STRING,
302 { /* end of list */ }
306 static QemuOptsList qemu_machine_opts = {
307 .name = "machine",
308 .implied_opt_name = "type",
309 .merge_lists = true,
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
311 .desc = {
313 * no elements => accept any
314 * sanity checking will happen later
315 * when setting machine properties
321 static QemuOptsList qemu_accel_opts = {
322 .name = "accel",
323 .implied_opt_name = "accel",
324 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
325 .merge_lists = true,
326 .desc = {
328 .name = "accel",
329 .type = QEMU_OPT_STRING,
330 .help = "Select the type of accelerator",
333 .name = "thread",
334 .type = QEMU_OPT_STRING,
335 .help = "Enable/disable multi-threaded TCG",
337 { /* end of list */ }
341 static QemuOptsList qemu_boot_opts = {
342 .name = "boot-opts",
343 .implied_opt_name = "order",
344 .merge_lists = true,
345 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
346 .desc = {
348 .name = "order",
349 .type = QEMU_OPT_STRING,
350 }, {
351 .name = "once",
352 .type = QEMU_OPT_STRING,
353 }, {
354 .name = "menu",
355 .type = QEMU_OPT_BOOL,
356 }, {
357 .name = "splash",
358 .type = QEMU_OPT_STRING,
359 }, {
360 .name = "splash-time",
361 .type = QEMU_OPT_STRING,
362 }, {
363 .name = "reboot-timeout",
364 .type = QEMU_OPT_STRING,
365 }, {
366 .name = "strict",
367 .type = QEMU_OPT_BOOL,
369 { /*End of list */ }
373 static QemuOptsList qemu_add_fd_opts = {
374 .name = "add-fd",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
376 .desc = {
378 .name = "fd",
379 .type = QEMU_OPT_NUMBER,
380 .help = "file descriptor of which a duplicate is added to fd set",
382 .name = "set",
383 .type = QEMU_OPT_NUMBER,
384 .help = "ID of the fd set to add fd to",
386 .name = "opaque",
387 .type = QEMU_OPT_STRING,
388 .help = "free-form string used to describe fd",
390 { /* end of list */ }
394 static QemuOptsList qemu_object_opts = {
395 .name = "object",
396 .implied_opt_name = "qom-type",
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
398 .desc = {
403 static QemuOptsList qemu_tpmdev_opts = {
404 .name = "tpmdev",
405 .implied_opt_name = "type",
406 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
407 .desc = {
408 /* options are defined in the TPM backends */
409 { /* end of list */ }
413 static QemuOptsList qemu_realtime_opts = {
414 .name = "realtime",
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
416 .desc = {
418 .name = "mlock",
419 .type = QEMU_OPT_BOOL,
421 { /* end of list */ }
425 static QemuOptsList qemu_msg_opts = {
426 .name = "msg",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
428 .desc = {
430 .name = "timestamp",
431 .type = QEMU_OPT_BOOL,
433 { /* end of list */ }
437 static QemuOptsList qemu_name_opts = {
438 .name = "name",
439 .implied_opt_name = "guest",
440 .merge_lists = true,
441 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
442 .desc = {
444 .name = "guest",
445 .type = QEMU_OPT_STRING,
446 .help = "Sets the name of the guest.\n"
447 "This name will be displayed in the SDL window caption.\n"
448 "The name will also be used for the VNC server",
449 }, {
450 .name = "process",
451 .type = QEMU_OPT_STRING,
452 .help = "Sets the name of the QEMU process, as shown in top etc",
453 }, {
454 .name = "debug-threads",
455 .type = QEMU_OPT_BOOL,
456 .help = "When enabled, name the individual threads; defaults off.\n"
457 "NOTE: The thread names are for debugging and not a\n"
458 "stable API.",
460 { /* End of list */ }
464 static QemuOptsList qemu_mem_opts = {
465 .name = "memory",
466 .implied_opt_name = "size",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
468 .merge_lists = true,
469 .desc = {
471 .name = "size",
472 .type = QEMU_OPT_SIZE,
475 .name = "slots",
476 .type = QEMU_OPT_NUMBER,
479 .name = "maxmem",
480 .type = QEMU_OPT_SIZE,
482 { /* end of list */ }
486 static QemuOptsList qemu_icount_opts = {
487 .name = "icount",
488 .implied_opt_name = "shift",
489 .merge_lists = true,
490 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
491 .desc = {
493 .name = "shift",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "align",
497 .type = QEMU_OPT_BOOL,
498 }, {
499 .name = "sleep",
500 .type = QEMU_OPT_BOOL,
501 }, {
502 .name = "rr",
503 .type = QEMU_OPT_STRING,
504 }, {
505 .name = "rrfile",
506 .type = QEMU_OPT_STRING,
507 }, {
508 .name = "rrsnapshot",
509 .type = QEMU_OPT_STRING,
511 { /* end of list */ }
515 static QemuOptsList qemu_semihosting_config_opts = {
516 .name = "semihosting-config",
517 .implied_opt_name = "enable",
518 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
519 .desc = {
521 .name = "enable",
522 .type = QEMU_OPT_BOOL,
523 }, {
524 .name = "target",
525 .type = QEMU_OPT_STRING,
526 }, {
527 .name = "arg",
528 .type = QEMU_OPT_STRING,
530 { /* end of list */ }
534 static QemuOptsList qemu_fw_cfg_opts = {
535 .name = "fw_cfg",
536 .implied_opt_name = "name",
537 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
538 .desc = {
540 .name = "name",
541 .type = QEMU_OPT_STRING,
542 .help = "Sets the fw_cfg name of the blob to be inserted",
543 }, {
544 .name = "file",
545 .type = QEMU_OPT_STRING,
546 .help = "Sets the name of the file from which\n"
547 "the fw_cfg blob will be loaded",
548 }, {
549 .name = "string",
550 .type = QEMU_OPT_STRING,
551 .help = "Sets content of the blob to be inserted from a string",
553 { /* end of list */ }
558 * Get machine options
560 * Returns: machine options (never null).
562 QemuOpts *qemu_get_machine_opts(void)
564 return qemu_find_opts_singleton("machine");
567 const char *qemu_get_vm_name(void)
569 return qemu_name;
572 static void res_free(void)
574 g_free(boot_splash_filedata);
575 boot_splash_filedata = NULL;
578 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
580 const char *driver = qemu_opt_get(opts, "driver");
581 int i;
583 if (!driver)
584 return 0;
585 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
586 if (strcmp(default_list[i].driver, driver) != 0)
587 continue;
588 *(default_list[i].flag) = 0;
590 return 0;
593 /***********************************************************/
594 /* QEMU state */
596 static RunState current_run_state = RUN_STATE_PRELAUNCH;
598 /* We use RUN_STATE__MAX but any invalid value will do */
599 static RunState vmstop_requested = RUN_STATE__MAX;
600 static QemuMutex vmstop_lock;
602 typedef struct {
603 RunState from;
604 RunState to;
605 } RunStateTransition;
607 static const RunStateTransition runstate_transitions_def[] = {
608 /* from -> to */
609 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
610 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
611 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
613 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
614 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
615 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
616 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
618 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
620 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
621 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
624 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
626 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
627 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
631 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
635 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_PAUSED, RUN_STATE_COLO},
640 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
641 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
645 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
648 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
649 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
650 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
651 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
652 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
654 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
655 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
657 { RUN_STATE_COLO, RUN_STATE_RUNNING },
659 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
660 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
661 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
662 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
663 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
664 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
665 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
666 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
667 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
668 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
669 { RUN_STATE_RUNNING, RUN_STATE_COLO},
671 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
673 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
674 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
677 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
678 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
679 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
680 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
681 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
682 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
684 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
685 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
686 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
687 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
689 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
690 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
691 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
693 { RUN_STATE__MAX, RUN_STATE__MAX },
696 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
698 bool runstate_check(RunState state)
700 return current_run_state == state;
703 bool runstate_store(char *str, size_t size)
705 const char *state = RunState_str(current_run_state);
706 size_t len = strlen(state) + 1;
708 if (len > size) {
709 return false;
711 memcpy(str, state, len);
712 return true;
715 static void runstate_init(void)
717 const RunStateTransition *p;
719 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
720 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
721 runstate_valid_transitions[p->from][p->to] = true;
724 qemu_mutex_init(&vmstop_lock);
727 /* This function will abort() on invalid state transitions */
728 void runstate_set(RunState new_state)
730 assert(new_state < RUN_STATE__MAX);
732 if (current_run_state == new_state) {
733 return;
736 if (!runstate_valid_transitions[current_run_state][new_state]) {
737 error_report("invalid runstate transition: '%s' -> '%s'",
738 RunState_str(current_run_state),
739 RunState_str(new_state));
740 abort();
742 trace_runstate_set(new_state);
743 current_run_state = new_state;
746 int runstate_is_running(void)
748 return runstate_check(RUN_STATE_RUNNING);
751 bool runstate_needs_reset(void)
753 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
754 runstate_check(RUN_STATE_SHUTDOWN);
757 StatusInfo *qmp_query_status(Error **errp)
759 StatusInfo *info = g_malloc0(sizeof(*info));
761 info->running = runstate_is_running();
762 info->singlestep = singlestep;
763 info->status = current_run_state;
765 return info;
768 bool qemu_vmstop_requested(RunState *r)
770 qemu_mutex_lock(&vmstop_lock);
771 *r = vmstop_requested;
772 vmstop_requested = RUN_STATE__MAX;
773 qemu_mutex_unlock(&vmstop_lock);
774 return *r < RUN_STATE__MAX;
777 void qemu_system_vmstop_request_prepare(void)
779 qemu_mutex_lock(&vmstop_lock);
782 void qemu_system_vmstop_request(RunState state)
784 vmstop_requested = state;
785 qemu_mutex_unlock(&vmstop_lock);
786 qemu_notify_event();
789 /***********************************************************/
790 /* real time host monotonic timer */
792 static time_t qemu_time(void)
794 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
797 /***********************************************************/
798 /* host time/date access */
799 void qemu_get_timedate(struct tm *tm, int offset)
801 time_t ti = qemu_time();
803 ti += offset;
804 if (rtc_date_offset == -1) {
805 if (rtc_utc)
806 gmtime_r(&ti, tm);
807 else
808 localtime_r(&ti, tm);
809 } else {
810 ti -= rtc_date_offset;
811 gmtime_r(&ti, tm);
815 int qemu_timedate_diff(struct tm *tm)
817 time_t seconds;
819 if (rtc_date_offset == -1)
820 if (rtc_utc)
821 seconds = mktimegm(tm);
822 else {
823 struct tm tmp = *tm;
824 tmp.tm_isdst = -1; /* use timezone to figure it out */
825 seconds = mktime(&tmp);
827 else
828 seconds = mktimegm(tm) + rtc_date_offset;
830 return seconds - qemu_time();
833 static void configure_rtc_date_offset(const char *startdate, int legacy)
835 time_t rtc_start_date;
836 struct tm tm;
838 if (!strcmp(startdate, "now") && legacy) {
839 rtc_date_offset = -1;
840 } else {
841 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
842 &tm.tm_year,
843 &tm.tm_mon,
844 &tm.tm_mday,
845 &tm.tm_hour,
846 &tm.tm_min,
847 &tm.tm_sec) == 6) {
848 /* OK */
849 } else if (sscanf(startdate, "%d-%d-%d",
850 &tm.tm_year,
851 &tm.tm_mon,
852 &tm.tm_mday) == 3) {
853 tm.tm_hour = 0;
854 tm.tm_min = 0;
855 tm.tm_sec = 0;
856 } else {
857 goto date_fail;
859 tm.tm_year -= 1900;
860 tm.tm_mon--;
861 rtc_start_date = mktimegm(&tm);
862 if (rtc_start_date == -1) {
863 date_fail:
864 error_report("invalid date format");
865 error_printf("valid formats: "
866 "'2006-06-17T16:01:21' or '2006-06-17'\n");
867 exit(1);
869 rtc_date_offset = qemu_time() - rtc_start_date;
873 static void configure_rtc(QemuOpts *opts)
875 const char *value;
877 value = qemu_opt_get(opts, "base");
878 if (value) {
879 if (!strcmp(value, "utc")) {
880 rtc_utc = 1;
881 } else if (!strcmp(value, "localtime")) {
882 Error *blocker = NULL;
883 rtc_utc = 0;
884 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
885 "-rtc base=localtime");
886 replay_add_blocker(blocker);
887 } else {
888 configure_rtc_date_offset(value, 0);
891 value = qemu_opt_get(opts, "clock");
892 if (value) {
893 if (!strcmp(value, "host")) {
894 rtc_clock = QEMU_CLOCK_HOST;
895 } else if (!strcmp(value, "rt")) {
896 rtc_clock = QEMU_CLOCK_REALTIME;
897 } else if (!strcmp(value, "vm")) {
898 rtc_clock = QEMU_CLOCK_VIRTUAL;
899 } else {
900 error_report("invalid option value '%s'", value);
901 exit(1);
904 value = qemu_opt_get(opts, "driftfix");
905 if (value) {
906 if (!strcmp(value, "slew")) {
907 static GlobalProperty slew_lost_ticks = {
908 .driver = "mc146818rtc",
909 .property = "lost_tick_policy",
910 .value = "slew",
913 qdev_prop_register_global(&slew_lost_ticks);
914 } else if (!strcmp(value, "none")) {
915 /* discard is default */
916 } else {
917 error_report("invalid option value '%s'", value);
918 exit(1);
923 /***********************************************************/
924 /* Bluetooth support */
925 static int nb_hcis;
926 static int cur_hci;
927 static struct HCIInfo *hci_table[MAX_NICS];
929 struct HCIInfo *qemu_next_hci(void)
931 if (cur_hci == nb_hcis)
932 return &null_hci;
934 return hci_table[cur_hci++];
937 static int bt_hci_parse(const char *str)
939 struct HCIInfo *hci;
940 bdaddr_t bdaddr;
942 if (nb_hcis >= MAX_NICS) {
943 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
944 return -1;
947 hci = hci_init(str);
948 if (!hci)
949 return -1;
951 bdaddr.b[0] = 0x52;
952 bdaddr.b[1] = 0x54;
953 bdaddr.b[2] = 0x00;
954 bdaddr.b[3] = 0x12;
955 bdaddr.b[4] = 0x34;
956 bdaddr.b[5] = 0x56 + nb_hcis;
957 hci->bdaddr_set(hci, bdaddr.b);
959 hci_table[nb_hcis++] = hci;
961 return 0;
964 static void bt_vhci_add(int vlan_id)
966 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
968 if (!vlan->slave)
969 warn_report("adding a VHCI to an empty scatternet %i",
970 vlan_id);
972 bt_vhci_init(bt_new_hci(vlan));
975 static struct bt_device_s *bt_device_add(const char *opt)
977 struct bt_scatternet_s *vlan;
978 int vlan_id = 0;
979 char *endp = strstr(opt, ",vlan=");
980 int len = (endp ? endp - opt : strlen(opt)) + 1;
981 char devname[10];
983 pstrcpy(devname, MIN(sizeof(devname), len), opt);
985 if (endp) {
986 vlan_id = strtol(endp + 6, &endp, 0);
987 if (*endp) {
988 error_report("unrecognised bluetooth vlan Id");
989 return 0;
993 vlan = qemu_find_bt_vlan(vlan_id);
995 if (!vlan->slave)
996 warn_report("adding a slave device to an empty scatternet %i",
997 vlan_id);
999 if (!strcmp(devname, "keyboard"))
1000 return bt_keyboard_init(vlan);
1002 error_report("unsupported bluetooth device '%s'", devname);
1003 return 0;
1006 static int bt_parse(const char *opt)
1008 const char *endp, *p;
1009 int vlan;
1011 if (strstart(opt, "hci", &endp)) {
1012 if (!*endp || *endp == ',') {
1013 if (*endp)
1014 if (!strstart(endp, ",vlan=", 0))
1015 opt = endp + 1;
1017 return bt_hci_parse(opt);
1019 } else if (strstart(opt, "vhci", &endp)) {
1020 if (!*endp || *endp == ',') {
1021 if (*endp) {
1022 if (strstart(endp, ",vlan=", &p)) {
1023 vlan = strtol(p, (char **) &endp, 0);
1024 if (*endp) {
1025 error_report("bad scatternet '%s'", p);
1026 return 1;
1028 } else {
1029 error_report("bad parameter '%s'", endp + 1);
1030 return 1;
1032 } else
1033 vlan = 0;
1035 bt_vhci_add(vlan);
1036 return 0;
1038 } else if (strstart(opt, "device:", &endp))
1039 return !bt_device_add(endp);
1041 error_report("bad bluetooth parameter '%s'", opt);
1042 return 1;
1045 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1047 if (qemu_opt_get_bool(opts, "enable", false)) {
1048 #ifdef CONFIG_SECCOMP
1049 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1050 | QEMU_SECCOMP_SET_OBSOLETE;
1051 const char *value = NULL;
1053 value = qemu_opt_get(opts, "obsolete");
1054 if (value) {
1055 if (g_str_equal(value, "allow")) {
1056 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1057 } else if (g_str_equal(value, "deny")) {
1058 /* this is the default option, this if is here
1059 * to provide a little bit of consistency for
1060 * the command line */
1061 } else {
1062 error_report("invalid argument for obsolete");
1063 return -1;
1067 value = qemu_opt_get(opts, "elevateprivileges");
1068 if (value) {
1069 if (g_str_equal(value, "deny")) {
1070 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1071 } else if (g_str_equal(value, "children")) {
1072 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1074 /* calling prctl directly because we're
1075 * not sure if host has CAP_SYS_ADMIN set*/
1076 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1077 error_report("failed to set no_new_privs "
1078 "aborting");
1079 return -1;
1081 } else if (g_str_equal(value, "allow")) {
1082 /* default value */
1083 } else {
1084 error_report("invalid argument for elevateprivileges");
1085 return -1;
1089 value = qemu_opt_get(opts, "spawn");
1090 if (value) {
1091 if (g_str_equal(value, "deny")) {
1092 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1093 } else if (g_str_equal(value, "allow")) {
1094 /* default value */
1095 } else {
1096 error_report("invalid argument for spawn");
1097 return -1;
1101 value = qemu_opt_get(opts, "resourcecontrol");
1102 if (value) {
1103 if (g_str_equal(value, "deny")) {
1104 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1105 } else if (g_str_equal(value, "allow")) {
1106 /* default value */
1107 } else {
1108 error_report("invalid argument for resourcecontrol");
1109 return -1;
1113 if (seccomp_start(seccomp_opts) < 0) {
1114 error_report("failed to install seccomp syscall filter "
1115 "in the kernel");
1116 return -1;
1118 #else
1119 error_report("seccomp support is disabled");
1120 return -1;
1121 #endif
1124 return 0;
1127 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1129 const char *proc_name;
1131 if (qemu_opt_get(opts, "debug-threads")) {
1132 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1134 qemu_name = qemu_opt_get(opts, "guest");
1136 proc_name = qemu_opt_get(opts, "process");
1137 if (proc_name) {
1138 os_set_proc_name(proc_name);
1141 return 0;
1144 bool defaults_enabled(void)
1146 return has_defaults;
1149 #ifndef _WIN32
1150 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1152 int fd, dupfd, flags;
1153 int64_t fdset_id;
1154 const char *fd_opaque = NULL;
1155 AddfdInfo *fdinfo;
1157 fd = qemu_opt_get_number(opts, "fd", -1);
1158 fdset_id = qemu_opt_get_number(opts, "set", -1);
1159 fd_opaque = qemu_opt_get(opts, "opaque");
1161 if (fd < 0) {
1162 error_report("fd option is required and must be non-negative");
1163 return -1;
1166 if (fd <= STDERR_FILENO) {
1167 error_report("fd cannot be a standard I/O stream");
1168 return -1;
1172 * All fds inherited across exec() necessarily have FD_CLOEXEC
1173 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1175 flags = fcntl(fd, F_GETFD);
1176 if (flags == -1 || (flags & FD_CLOEXEC)) {
1177 error_report("fd is not valid or already in use");
1178 return -1;
1181 if (fdset_id < 0) {
1182 error_report("set option is required and must be non-negative");
1183 return -1;
1186 #ifdef F_DUPFD_CLOEXEC
1187 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1188 #else
1189 dupfd = dup(fd);
1190 if (dupfd != -1) {
1191 qemu_set_cloexec(dupfd);
1193 #endif
1194 if (dupfd == -1) {
1195 error_report("error duplicating fd: %s", strerror(errno));
1196 return -1;
1199 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1200 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1201 &error_abort);
1202 g_free(fdinfo);
1204 return 0;
1207 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1209 int fd;
1211 fd = qemu_opt_get_number(opts, "fd", -1);
1212 close(fd);
1214 return 0;
1216 #endif
1218 /***********************************************************/
1219 /* QEMU Block devices */
1221 #define HD_OPTS "media=disk"
1222 #define CDROM_OPTS "media=cdrom"
1223 #define FD_OPTS ""
1224 #define PFLASH_OPTS ""
1225 #define MTD_OPTS ""
1226 #define SD_OPTS ""
1228 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1230 BlockInterfaceType *block_default_type = opaque;
1232 return drive_new(opts, *block_default_type) == NULL;
1235 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1237 if (qemu_opt_get(opts, "snapshot") == NULL) {
1238 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1240 return 0;
1243 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1244 int index, const char *optstr)
1246 QemuOpts *opts;
1247 DriveInfo *dinfo;
1249 if (!enable || drive_get_by_index(type, index)) {
1250 return;
1253 opts = drive_add(type, index, NULL, optstr);
1254 if (snapshot) {
1255 drive_enable_snapshot(NULL, opts, NULL);
1258 dinfo = drive_new(opts, type);
1259 if (!dinfo) {
1260 exit(1);
1262 dinfo->is_default = true;
1266 static QemuOptsList qemu_smp_opts = {
1267 .name = "smp-opts",
1268 .implied_opt_name = "cpus",
1269 .merge_lists = true,
1270 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1271 .desc = {
1273 .name = "cpus",
1274 .type = QEMU_OPT_NUMBER,
1275 }, {
1276 .name = "sockets",
1277 .type = QEMU_OPT_NUMBER,
1278 }, {
1279 .name = "cores",
1280 .type = QEMU_OPT_NUMBER,
1281 }, {
1282 .name = "threads",
1283 .type = QEMU_OPT_NUMBER,
1284 }, {
1285 .name = "maxcpus",
1286 .type = QEMU_OPT_NUMBER,
1288 { /*End of list */ }
1292 static void smp_parse(QemuOpts *opts)
1294 if (opts) {
1295 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1296 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1297 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1298 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1300 /* compute missing values, prefer sockets over cores over threads */
1301 if (cpus == 0 || sockets == 0) {
1302 sockets = sockets > 0 ? sockets : 1;
1303 cores = cores > 0 ? cores : 1;
1304 threads = threads > 0 ? threads : 1;
1305 if (cpus == 0) {
1306 cpus = cores * threads * sockets;
1308 } else if (cores == 0) {
1309 threads = threads > 0 ? threads : 1;
1310 cores = cpus / (sockets * threads);
1311 cores = cores > 0 ? cores : 1;
1312 } else if (threads == 0) {
1313 threads = cpus / (cores * sockets);
1314 threads = threads > 0 ? threads : 1;
1315 } else if (sockets * cores * threads < cpus) {
1316 error_report("cpu topology: "
1317 "sockets (%u) * cores (%u) * threads (%u) < "
1318 "smp_cpus (%u)",
1319 sockets, cores, threads, cpus);
1320 exit(1);
1323 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1325 if (max_cpus < cpus) {
1326 error_report("maxcpus must be equal to or greater than smp");
1327 exit(1);
1330 if (sockets * cores * threads > max_cpus) {
1331 error_report("cpu topology: "
1332 "sockets (%u) * cores (%u) * threads (%u) > "
1333 "maxcpus (%u)",
1334 sockets, cores, threads, max_cpus);
1335 exit(1);
1338 smp_cpus = cpus;
1339 smp_cores = cores;
1340 smp_threads = threads;
1343 if (smp_cpus > 1) {
1344 Error *blocker = NULL;
1345 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1346 replay_add_blocker(blocker);
1350 static void realtime_init(void)
1352 if (enable_mlock) {
1353 if (os_mlock() < 0) {
1354 error_report("locking memory failed");
1355 exit(1);
1361 static void configure_msg(QemuOpts *opts)
1363 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1366 /***********************************************************/
1367 /* Semihosting */
1369 typedef struct SemihostingConfig {
1370 bool enabled;
1371 SemihostingTarget target;
1372 const char **argv;
1373 int argc;
1374 const char *cmdline; /* concatenated argv */
1375 } SemihostingConfig;
1377 static SemihostingConfig semihosting;
1379 bool semihosting_enabled(void)
1381 return semihosting.enabled;
1384 SemihostingTarget semihosting_get_target(void)
1386 return semihosting.target;
1389 const char *semihosting_get_arg(int i)
1391 if (i >= semihosting.argc) {
1392 return NULL;
1394 return semihosting.argv[i];
1397 int semihosting_get_argc(void)
1399 return semihosting.argc;
1402 const char *semihosting_get_cmdline(void)
1404 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1405 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1407 return semihosting.cmdline;
1410 static int add_semihosting_arg(void *opaque,
1411 const char *name, const char *val,
1412 Error **errp)
1414 SemihostingConfig *s = opaque;
1415 if (strcmp(name, "arg") == 0) {
1416 s->argc++;
1417 /* one extra element as g_strjoinv() expects NULL-terminated array */
1418 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1419 s->argv[s->argc - 1] = val;
1420 s->argv[s->argc] = NULL;
1422 return 0;
1425 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1426 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1428 char *cmd_token;
1430 /* argv[0] */
1431 add_semihosting_arg(&semihosting, "arg", file, NULL);
1433 /* split -append and initialize argv[1..n] */
1434 cmd_token = strtok(g_strdup(cmd), " ");
1435 while (cmd_token) {
1436 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1437 cmd_token = strtok(NULL, " ");
1441 /* Now we still need this for compatibility with XEN. */
1442 bool has_igd_gfx_passthru;
1443 static void igd_gfx_passthru(void)
1445 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1448 /***********************************************************/
1449 /* USB devices */
1451 static int usb_device_add(const char *devname)
1453 USBDevice *dev = NULL;
1455 if (!machine_usb(current_machine)) {
1456 return -1;
1459 dev = usbdevice_create(devname);
1460 if (!dev)
1461 return -1;
1463 return 0;
1466 static int usb_parse(const char *cmdline)
1468 int r;
1469 r = usb_device_add(cmdline);
1470 if (r < 0) {
1471 error_report("could not add USB device '%s'", cmdline);
1473 return r;
1476 /***********************************************************/
1477 /* machine registration */
1479 MachineState *current_machine;
1481 static MachineClass *find_machine(const char *name)
1483 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1484 MachineClass *mc = NULL;
1486 for (el = machines; el; el = el->next) {
1487 MachineClass *temp = el->data;
1489 if (!strcmp(temp->name, name)) {
1490 mc = temp;
1491 break;
1493 if (temp->alias &&
1494 !strcmp(temp->alias, name)) {
1495 mc = temp;
1496 break;
1500 g_slist_free(machines);
1501 return mc;
1504 MachineClass *find_default_machine(void)
1506 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1507 MachineClass *mc = NULL;
1509 for (el = machines; el; el = el->next) {
1510 MachineClass *temp = el->data;
1512 if (temp->is_default) {
1513 mc = temp;
1514 break;
1518 g_slist_free(machines);
1519 return mc;
1522 MachineInfoList *qmp_query_machines(Error **errp)
1524 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1525 MachineInfoList *mach_list = NULL;
1527 for (el = machines; el; el = el->next) {
1528 MachineClass *mc = el->data;
1529 MachineInfoList *entry;
1530 MachineInfo *info;
1532 info = g_malloc0(sizeof(*info));
1533 if (mc->is_default) {
1534 info->has_is_default = true;
1535 info->is_default = true;
1538 if (mc->alias) {
1539 info->has_alias = true;
1540 info->alias = g_strdup(mc->alias);
1543 info->name = g_strdup(mc->name);
1544 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1545 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1547 entry = g_malloc0(sizeof(*entry));
1548 entry->value = info;
1549 entry->next = mach_list;
1550 mach_list = entry;
1553 g_slist_free(machines);
1554 return mach_list;
1557 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1559 ObjectProperty *prop;
1560 ObjectPropertyIterator iter;
1562 if (!qemu_opt_has_help_opt(opts)) {
1563 return 0;
1566 object_property_iter_init(&iter, OBJECT(machine));
1567 while ((prop = object_property_iter_next(&iter))) {
1568 if (!prop->set) {
1569 continue;
1572 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1573 prop->name, prop->type);
1574 if (prop->description) {
1575 error_printf(" (%s)\n", prop->description);
1576 } else {
1577 error_printf("\n");
1581 return 1;
1584 /***********************************************************/
1585 /* main execution loop */
1587 struct vm_change_state_entry {
1588 VMChangeStateHandler *cb;
1589 void *opaque;
1590 QLIST_ENTRY (vm_change_state_entry) entries;
1593 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1595 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1596 void *opaque)
1598 VMChangeStateEntry *e;
1600 e = g_malloc0(sizeof (*e));
1602 e->cb = cb;
1603 e->opaque = opaque;
1604 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1605 return e;
1608 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1610 QLIST_REMOVE (e, entries);
1611 g_free (e);
1614 void vm_state_notify(int running, RunState state)
1616 VMChangeStateEntry *e, *next;
1618 trace_vm_state_notify(running, state);
1620 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1621 e->cb(e->opaque, running, state);
1625 static ShutdownCause reset_requested;
1626 static ShutdownCause shutdown_requested;
1627 static int shutdown_signal;
1628 static pid_t shutdown_pid;
1629 static int powerdown_requested;
1630 static int debug_requested;
1631 static int suspend_requested;
1632 static WakeupReason wakeup_reason;
1633 static NotifierList powerdown_notifiers =
1634 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1635 static NotifierList suspend_notifiers =
1636 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1637 static NotifierList wakeup_notifiers =
1638 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1639 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1641 ShutdownCause qemu_shutdown_requested_get(void)
1643 return shutdown_requested;
1646 ShutdownCause qemu_reset_requested_get(void)
1648 return reset_requested;
1651 static int qemu_shutdown_requested(void)
1653 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1656 static void qemu_kill_report(void)
1658 if (!qtest_driver() && shutdown_signal) {
1659 if (shutdown_pid == 0) {
1660 /* This happens for eg ^C at the terminal, so it's worth
1661 * avoiding printing an odd message in that case.
1663 error_report("terminating on signal %d", shutdown_signal);
1664 } else {
1665 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1667 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1668 shutdown_signal, shutdown_pid,
1669 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1670 g_free(shutdown_cmd);
1672 shutdown_signal = 0;
1676 static ShutdownCause qemu_reset_requested(void)
1678 ShutdownCause r = reset_requested;
1680 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1681 reset_requested = SHUTDOWN_CAUSE_NONE;
1682 return r;
1684 return SHUTDOWN_CAUSE_NONE;
1687 static int qemu_suspend_requested(void)
1689 int r = suspend_requested;
1690 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1691 suspend_requested = 0;
1692 return r;
1694 return false;
1697 static WakeupReason qemu_wakeup_requested(void)
1699 return wakeup_reason;
1702 static int qemu_powerdown_requested(void)
1704 int r = powerdown_requested;
1705 powerdown_requested = 0;
1706 return r;
1709 static int qemu_debug_requested(void)
1711 int r = debug_requested;
1712 debug_requested = 0;
1713 return r;
1717 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1719 void qemu_system_reset(ShutdownCause reason)
1721 MachineClass *mc;
1723 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1725 cpu_synchronize_all_states();
1727 if (mc && mc->reset) {
1728 mc->reset();
1729 } else {
1730 qemu_devices_reset();
1732 if (reason) {
1733 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1734 &error_abort);
1736 cpu_synchronize_all_post_reset();
1739 void qemu_system_guest_panicked(GuestPanicInformation *info)
1741 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1743 if (current_cpu) {
1744 current_cpu->crash_occurred = true;
1746 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1747 !!info, info, &error_abort);
1748 vm_stop(RUN_STATE_GUEST_PANICKED);
1749 if (!no_shutdown) {
1750 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1751 !!info, info, &error_abort);
1752 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1755 if (info) {
1756 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1757 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1758 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1759 info->u.hyper_v.arg1,
1760 info->u.hyper_v.arg2,
1761 info->u.hyper_v.arg3,
1762 info->u.hyper_v.arg4,
1763 info->u.hyper_v.arg5);
1764 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1765 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1766 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1767 info->u.s390.core,
1768 S390CrashReason_str(info->u.s390.reason),
1769 info->u.s390.psw_mask,
1770 info->u.s390.psw_addr);
1772 qapi_free_GuestPanicInformation(info);
1776 void qemu_system_reset_request(ShutdownCause reason)
1778 if (no_reboot) {
1779 shutdown_requested = reason;
1780 } else {
1781 reset_requested = reason;
1783 cpu_stop_current();
1784 qemu_notify_event();
1787 static void qemu_system_suspend(void)
1789 pause_all_vcpus();
1790 notifier_list_notify(&suspend_notifiers, NULL);
1791 runstate_set(RUN_STATE_SUSPENDED);
1792 qapi_event_send_suspend(&error_abort);
1795 void qemu_system_suspend_request(void)
1797 if (runstate_check(RUN_STATE_SUSPENDED)) {
1798 return;
1800 suspend_requested = 1;
1801 cpu_stop_current();
1802 qemu_notify_event();
1805 void qemu_register_suspend_notifier(Notifier *notifier)
1807 notifier_list_add(&suspend_notifiers, notifier);
1810 void qemu_system_wakeup_request(WakeupReason reason)
1812 trace_system_wakeup_request(reason);
1814 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1815 return;
1817 if (!(wakeup_reason_mask & (1 << reason))) {
1818 return;
1820 runstate_set(RUN_STATE_RUNNING);
1821 wakeup_reason = reason;
1822 qemu_notify_event();
1825 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1827 if (enabled) {
1828 wakeup_reason_mask |= (1 << reason);
1829 } else {
1830 wakeup_reason_mask &= ~(1 << reason);
1834 void qemu_register_wakeup_notifier(Notifier *notifier)
1836 notifier_list_add(&wakeup_notifiers, notifier);
1839 void qemu_system_killed(int signal, pid_t pid)
1841 shutdown_signal = signal;
1842 shutdown_pid = pid;
1843 no_shutdown = 0;
1845 /* Cannot call qemu_system_shutdown_request directly because
1846 * we are in a signal handler.
1848 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1849 qemu_notify_event();
1852 void qemu_system_shutdown_request(ShutdownCause reason)
1854 trace_qemu_system_shutdown_request(reason);
1855 replay_shutdown_request(reason);
1856 shutdown_requested = reason;
1857 qemu_notify_event();
1860 static void qemu_system_powerdown(void)
1862 qapi_event_send_powerdown(&error_abort);
1863 notifier_list_notify(&powerdown_notifiers, NULL);
1866 void qemu_system_powerdown_request(void)
1868 trace_qemu_system_powerdown_request();
1869 powerdown_requested = 1;
1870 qemu_notify_event();
1873 void qemu_register_powerdown_notifier(Notifier *notifier)
1875 notifier_list_add(&powerdown_notifiers, notifier);
1878 void qemu_system_debug_request(void)
1880 debug_requested = 1;
1881 qemu_notify_event();
1884 static bool main_loop_should_exit(void)
1886 RunState r;
1887 ShutdownCause request;
1889 if (qemu_debug_requested()) {
1890 vm_stop(RUN_STATE_DEBUG);
1892 if (qemu_suspend_requested()) {
1893 qemu_system_suspend();
1895 request = qemu_shutdown_requested();
1896 if (request) {
1897 qemu_kill_report();
1898 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1899 &error_abort);
1900 if (no_shutdown) {
1901 vm_stop(RUN_STATE_SHUTDOWN);
1902 } else {
1903 return true;
1906 request = qemu_reset_requested();
1907 if (request) {
1908 pause_all_vcpus();
1909 qemu_system_reset(request);
1910 resume_all_vcpus();
1911 if (!runstate_check(RUN_STATE_RUNNING) &&
1912 !runstate_check(RUN_STATE_INMIGRATE)) {
1913 runstate_set(RUN_STATE_PRELAUNCH);
1916 if (qemu_wakeup_requested()) {
1917 pause_all_vcpus();
1918 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1919 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1920 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1921 resume_all_vcpus();
1922 qapi_event_send_wakeup(&error_abort);
1924 if (qemu_powerdown_requested()) {
1925 qemu_system_powerdown();
1927 if (qemu_vmstop_requested(&r)) {
1928 vm_stop(r);
1930 return false;
1933 static void main_loop(void)
1935 #ifdef CONFIG_PROFILER
1936 int64_t ti;
1937 #endif
1938 do {
1939 #ifdef CONFIG_PROFILER
1940 ti = profile_getclock();
1941 #endif
1942 main_loop_wait(false);
1943 #ifdef CONFIG_PROFILER
1944 dev_time += profile_getclock() - ti;
1945 #endif
1946 } while (!main_loop_should_exit());
1949 static void version(void)
1951 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1952 QEMU_COPYRIGHT "\n");
1955 static void help(int exitcode)
1957 version();
1958 printf("usage: %s [options] [disk_image]\n\n"
1959 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1960 error_get_progname());
1962 #define QEMU_OPTIONS_GENERATE_HELP
1963 #include "qemu-options-wrapper.h"
1965 printf("\nDuring emulation, the following keys are useful:\n"
1966 "ctrl-alt-f toggle full screen\n"
1967 "ctrl-alt-n switch to virtual console 'n'\n"
1968 "ctrl-alt toggle mouse and keyboard grab\n"
1969 "\n"
1970 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1971 "\n"
1972 QEMU_HELP_BOTTOM "\n");
1974 exit(exitcode);
1977 #define HAS_ARG 0x0001
1979 typedef struct QEMUOption {
1980 const char *name;
1981 int flags;
1982 int index;
1983 uint32_t arch_mask;
1984 } QEMUOption;
1986 static const QEMUOption qemu_options[] = {
1987 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1988 #define QEMU_OPTIONS_GENERATE_OPTIONS
1989 #include "qemu-options-wrapper.h"
1990 { NULL },
1993 typedef struct VGAInterfaceInfo {
1994 const char *opt_name; /* option name */
1995 const char *name; /* human-readable name */
1996 /* Class names indicating that support is available.
1997 * If no class is specified, the interface is always available */
1998 const char *class_names[2];
1999 } VGAInterfaceInfo;
2001 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2002 [VGA_NONE] = {
2003 .opt_name = "none",
2005 [VGA_STD] = {
2006 .opt_name = "std",
2007 .name = "standard VGA",
2008 .class_names = { "VGA", "isa-vga" },
2010 [VGA_CIRRUS] = {
2011 .opt_name = "cirrus",
2012 .name = "Cirrus VGA",
2013 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2015 [VGA_VMWARE] = {
2016 .opt_name = "vmware",
2017 .name = "VMWare SVGA",
2018 .class_names = { "vmware-svga" },
2020 [VGA_VIRTIO] = {
2021 .opt_name = "virtio",
2022 .name = "Virtio VGA",
2023 .class_names = { "virtio-vga" },
2025 [VGA_QXL] = {
2026 .opt_name = "qxl",
2027 .name = "QXL VGA",
2028 .class_names = { "qxl-vga" },
2030 [VGA_TCX] = {
2031 .opt_name = "tcx",
2032 .name = "TCX framebuffer",
2033 .class_names = { "SUNW,tcx" },
2035 [VGA_CG3] = {
2036 .opt_name = "cg3",
2037 .name = "CG3 framebuffer",
2038 .class_names = { "cgthree" },
2040 [VGA_XENFB] = {
2041 .opt_name = "xenfb",
2045 static bool vga_interface_available(VGAInterfaceType t)
2047 VGAInterfaceInfo *ti = &vga_interfaces[t];
2049 assert(t < VGA_TYPE_MAX);
2050 return !ti->class_names[0] ||
2051 object_class_by_name(ti->class_names[0]) ||
2052 object_class_by_name(ti->class_names[1]);
2055 static void select_vgahw(const char *p)
2057 const char *opts;
2058 int t;
2060 assert(vga_interface_type == VGA_NONE);
2061 for (t = 0; t < VGA_TYPE_MAX; t++) {
2062 VGAInterfaceInfo *ti = &vga_interfaces[t];
2063 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2064 if (!vga_interface_available(t)) {
2065 error_report("%s not available", ti->name);
2066 exit(1);
2068 vga_interface_type = t;
2069 break;
2072 if (t == VGA_TYPE_MAX) {
2073 invalid_vga:
2074 error_report("unknown vga type: %s", p);
2075 exit(1);
2077 while (*opts) {
2078 const char *nextopt;
2080 if (strstart(opts, ",retrace=", &nextopt)) {
2081 opts = nextopt;
2082 if (strstart(opts, "dumb", &nextopt))
2083 vga_retrace_method = VGA_RETRACE_DUMB;
2084 else if (strstart(opts, "precise", &nextopt))
2085 vga_retrace_method = VGA_RETRACE_PRECISE;
2086 else goto invalid_vga;
2087 } else goto invalid_vga;
2088 opts = nextopt;
2092 static void parse_display(const char *p)
2094 const char *opts;
2096 if (strstart(p, "sdl", &opts)) {
2097 dpy.type = DISPLAY_TYPE_SDL;
2098 while (*opts) {
2099 const char *nextopt;
2101 if (strstart(opts, ",frame=", &nextopt)) {
2102 g_printerr("The frame= sdl option is deprecated, and will be\n"
2103 "removed in a future release.\n");
2104 opts = nextopt;
2105 if (strstart(opts, "on", &nextopt)) {
2106 no_frame = 0;
2107 } else if (strstart(opts, "off", &nextopt)) {
2108 no_frame = 1;
2109 } else {
2110 goto invalid_sdl_args;
2112 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2113 opts = nextopt;
2114 if (strstart(opts, "on", &nextopt)) {
2115 alt_grab = 1;
2116 } else if (strstart(opts, "off", &nextopt)) {
2117 alt_grab = 0;
2118 } else {
2119 goto invalid_sdl_args;
2121 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2122 opts = nextopt;
2123 if (strstart(opts, "on", &nextopt)) {
2124 ctrl_grab = 1;
2125 } else if (strstart(opts, "off", &nextopt)) {
2126 ctrl_grab = 0;
2127 } else {
2128 goto invalid_sdl_args;
2130 } else if (strstart(opts, ",window_close=", &nextopt)) {
2131 opts = nextopt;
2132 dpy.has_window_close = true;
2133 if (strstart(opts, "on", &nextopt)) {
2134 dpy.window_close = true;
2135 } else if (strstart(opts, "off", &nextopt)) {
2136 dpy.window_close = false;
2137 } else {
2138 goto invalid_sdl_args;
2140 } else if (strstart(opts, ",gl=", &nextopt)) {
2141 opts = nextopt;
2142 dpy.has_gl = true;
2143 if (strstart(opts, "on", &nextopt)) {
2144 dpy.gl = true;
2145 } else if (strstart(opts, "off", &nextopt)) {
2146 dpy.gl = false;
2147 } else {
2148 goto invalid_sdl_args;
2150 } else {
2151 invalid_sdl_args:
2152 error_report("invalid SDL option string");
2153 exit(1);
2155 opts = nextopt;
2157 } else if (strstart(p, "vnc", &opts)) {
2158 if (*opts == '=') {
2159 vnc_parse(opts + 1, &error_fatal);
2160 } else {
2161 error_report("VNC requires a display argument vnc=<display>");
2162 exit(1);
2164 } else if (strstart(p, "egl-headless", &opts)) {
2165 dpy.type = DISPLAY_TYPE_EGL_HEADLESS;
2166 } else if (strstart(p, "curses", &opts)) {
2167 dpy.type = DISPLAY_TYPE_CURSES;
2168 } else if (strstart(p, "gtk", &opts)) {
2169 dpy.type = DISPLAY_TYPE_GTK;
2170 while (*opts) {
2171 const char *nextopt;
2173 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2174 opts = nextopt;
2175 dpy.u.gtk.has_grab_on_hover = true;
2176 if (strstart(opts, "on", &nextopt)) {
2177 dpy.u.gtk.grab_on_hover = true;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 dpy.u.gtk.grab_on_hover = false;
2180 } else {
2181 goto invalid_gtk_args;
2183 } else if (strstart(opts, ",gl=", &nextopt)) {
2184 opts = nextopt;
2185 dpy.has_gl = true;
2186 if (strstart(opts, "on", &nextopt)) {
2187 dpy.gl = true;
2188 } else if (strstart(opts, "off", &nextopt)) {
2189 dpy.gl = false;
2190 } else {
2191 goto invalid_gtk_args;
2193 } else {
2194 invalid_gtk_args:
2195 error_report("invalid GTK option string");
2196 exit(1);
2198 opts = nextopt;
2200 } else if (strstart(p, "none", &opts)) {
2201 dpy.type = DISPLAY_TYPE_NONE;
2202 } else {
2203 error_report("unknown display type");
2204 exit(1);
2208 static int balloon_parse(const char *arg)
2210 QemuOpts *opts;
2212 if (strcmp(arg, "none") == 0) {
2213 return 0;
2216 if (!strncmp(arg, "virtio", 6)) {
2217 if (arg[6] == ',') {
2218 /* have params -> parse them */
2219 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2220 false);
2221 if (!opts)
2222 return -1;
2223 } else {
2224 /* create empty opts */
2225 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2226 &error_abort);
2228 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2229 return 0;
2232 return -1;
2235 char *qemu_find_file(int type, const char *name)
2237 int i;
2238 const char *subdir;
2239 char *buf;
2241 /* Try the name as a straight path first */
2242 if (access(name, R_OK) == 0) {
2243 trace_load_file(name, name);
2244 return g_strdup(name);
2247 switch (type) {
2248 case QEMU_FILE_TYPE_BIOS:
2249 subdir = "";
2250 break;
2251 case QEMU_FILE_TYPE_KEYMAP:
2252 subdir = "keymaps/";
2253 break;
2254 default:
2255 abort();
2258 for (i = 0; i < data_dir_idx; i++) {
2259 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2260 if (access(buf, R_OK) == 0) {
2261 trace_load_file(name, buf);
2262 return buf;
2264 g_free(buf);
2266 return NULL;
2269 static void qemu_add_data_dir(const char *path)
2271 int i;
2273 if (path == NULL) {
2274 return;
2276 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2277 return;
2279 for (i = 0; i < data_dir_idx; i++) {
2280 if (strcmp(data_dir[i], path) == 0) {
2281 return; /* duplicate */
2284 data_dir[data_dir_idx++] = g_strdup(path);
2287 static inline bool nonempty_str(const char *str)
2289 return str && *str;
2292 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2294 gchar *buf;
2295 size_t size;
2296 const char *name, *file, *str;
2297 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2299 if (fw_cfg == NULL) {
2300 error_report("fw_cfg device not available");
2301 return -1;
2303 name = qemu_opt_get(opts, "name");
2304 file = qemu_opt_get(opts, "file");
2305 str = qemu_opt_get(opts, "string");
2307 /* we need name and either a file or the content string */
2308 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2309 error_report("invalid argument(s)");
2310 return -1;
2312 if (nonempty_str(file) && nonempty_str(str)) {
2313 error_report("file and string are mutually exclusive");
2314 return -1;
2316 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2317 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2318 return -1;
2320 if (strncmp(name, "opt/", 4) != 0) {
2321 warn_report("externally provided fw_cfg item names "
2322 "should be prefixed with \"opt/\"");
2324 if (nonempty_str(str)) {
2325 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2326 buf = g_memdup(str, size);
2327 } else {
2328 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2329 error_report("can't load %s", file);
2330 return -1;
2333 /* For legacy, keep user files in a specific global order. */
2334 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2335 fw_cfg_add_file(fw_cfg, name, buf, size);
2336 fw_cfg_reset_order_override(fw_cfg);
2337 return 0;
2340 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2342 return qdev_device_help(opts);
2345 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2347 Error *err = NULL;
2348 DeviceState *dev;
2350 dev = qdev_device_add(opts, &err);
2351 if (!dev) {
2352 error_report_err(err);
2353 return -1;
2355 object_unref(OBJECT(dev));
2356 return 0;
2359 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2361 Error *local_err = NULL;
2363 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2364 if (local_err) {
2365 error_report_err(local_err);
2366 return -1;
2368 exit(0);
2370 return 0;
2373 #ifdef CONFIG_VIRTFS
2374 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2376 return qemu_fsdev_add(opts);
2378 #endif
2380 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2382 Chardev *chr;
2383 const char *chardev;
2384 const char *mode;
2385 int flags;
2387 mode = qemu_opt_get(opts, "mode");
2388 if (mode == NULL) {
2389 mode = "readline";
2391 if (strcmp(mode, "readline") == 0) {
2392 flags = MONITOR_USE_READLINE;
2393 } else if (strcmp(mode, "control") == 0) {
2394 flags = MONITOR_USE_CONTROL;
2395 } else {
2396 error_report("unknown monitor mode \"%s\"", mode);
2397 exit(1);
2400 if (qemu_opt_get_bool(opts, "pretty", 0))
2401 flags |= MONITOR_USE_PRETTY;
2403 chardev = qemu_opt_get(opts, "chardev");
2404 chr = qemu_chr_find(chardev);
2405 if (chr == NULL) {
2406 error_report("chardev \"%s\" not found", chardev);
2407 exit(1);
2410 monitor_init(chr, flags);
2411 return 0;
2414 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2416 static int monitor_device_index = 0;
2417 QemuOpts *opts;
2418 const char *p;
2419 char label[32];
2421 if (strstart(optarg, "chardev:", &p)) {
2422 snprintf(label, sizeof(label), "%s", p);
2423 } else {
2424 snprintf(label, sizeof(label), "compat_monitor%d",
2425 monitor_device_index);
2426 opts = qemu_chr_parse_compat(label, optarg);
2427 if (!opts) {
2428 error_report("parse error: %s", optarg);
2429 exit(1);
2433 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2434 qemu_opt_set(opts, "mode", mode, &error_abort);
2435 qemu_opt_set(opts, "chardev", label, &error_abort);
2436 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2437 monitor_device_index++;
2440 struct device_config {
2441 enum {
2442 DEV_USB, /* -usbdevice */
2443 DEV_BT, /* -bt */
2444 DEV_SERIAL, /* -serial */
2445 DEV_PARALLEL, /* -parallel */
2446 DEV_VIRTCON, /* -virtioconsole */
2447 DEV_DEBUGCON, /* -debugcon */
2448 DEV_GDB, /* -gdb, -s */
2449 DEV_SCLP, /* s390 sclp */
2450 } type;
2451 const char *cmdline;
2452 Location loc;
2453 QTAILQ_ENTRY(device_config) next;
2456 static QTAILQ_HEAD(, device_config) device_configs =
2457 QTAILQ_HEAD_INITIALIZER(device_configs);
2459 static void add_device_config(int type, const char *cmdline)
2461 struct device_config *conf;
2463 conf = g_malloc0(sizeof(*conf));
2464 conf->type = type;
2465 conf->cmdline = cmdline;
2466 loc_save(&conf->loc);
2467 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2470 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2472 struct device_config *conf;
2473 int rc;
2475 QTAILQ_FOREACH(conf, &device_configs, next) {
2476 if (conf->type != type)
2477 continue;
2478 loc_push_restore(&conf->loc);
2479 rc = func(conf->cmdline);
2480 loc_pop(&conf->loc);
2481 if (rc) {
2482 return rc;
2485 return 0;
2488 static int serial_parse(const char *devname)
2490 static int index = 0;
2491 char label[32];
2493 if (strcmp(devname, "none") == 0)
2494 return 0;
2495 if (index == MAX_SERIAL_PORTS) {
2496 error_report("too many serial ports");
2497 exit(1);
2499 snprintf(label, sizeof(label), "serial%d", index);
2500 serial_hds[index] = qemu_chr_new(label, devname);
2501 if (!serial_hds[index]) {
2502 error_report("could not connect serial device"
2503 " to character backend '%s'", devname);
2504 return -1;
2506 index++;
2507 return 0;
2510 static int parallel_parse(const char *devname)
2512 static int index = 0;
2513 char label[32];
2515 if (strcmp(devname, "none") == 0)
2516 return 0;
2517 if (index == MAX_PARALLEL_PORTS) {
2518 error_report("too many parallel ports");
2519 exit(1);
2521 snprintf(label, sizeof(label), "parallel%d", index);
2522 parallel_hds[index] = qemu_chr_new(label, devname);
2523 if (!parallel_hds[index]) {
2524 error_report("could not connect parallel device"
2525 " to character backend '%s'", devname);
2526 return -1;
2528 index++;
2529 return 0;
2532 static int virtcon_parse(const char *devname)
2534 QemuOptsList *device = qemu_find_opts("device");
2535 static int index = 0;
2536 char label[32];
2537 QemuOpts *bus_opts, *dev_opts;
2539 if (strcmp(devname, "none") == 0)
2540 return 0;
2541 if (index == MAX_VIRTIO_CONSOLES) {
2542 error_report("too many virtio consoles");
2543 exit(1);
2546 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2547 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2549 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2550 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2552 snprintf(label, sizeof(label), "virtcon%d", index);
2553 virtcon_hds[index] = qemu_chr_new(label, devname);
2554 if (!virtcon_hds[index]) {
2555 error_report("could not connect virtio console"
2556 " to character backend '%s'", devname);
2557 return -1;
2559 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2561 index++;
2562 return 0;
2565 static int sclp_parse(const char *devname)
2567 QemuOptsList *device = qemu_find_opts("device");
2568 static int index = 0;
2569 char label[32];
2570 QemuOpts *dev_opts;
2572 if (strcmp(devname, "none") == 0) {
2573 return 0;
2575 if (index == MAX_SCLP_CONSOLES) {
2576 error_report("too many sclp consoles");
2577 exit(1);
2580 assert(arch_type == QEMU_ARCH_S390X);
2582 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2583 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2585 snprintf(label, sizeof(label), "sclpcon%d", index);
2586 sclp_hds[index] = qemu_chr_new(label, devname);
2587 if (!sclp_hds[index]) {
2588 error_report("could not connect sclp console"
2589 " to character backend '%s'", devname);
2590 return -1;
2592 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2594 index++;
2595 return 0;
2598 static int debugcon_parse(const char *devname)
2600 QemuOpts *opts;
2602 if (!qemu_chr_new("debugcon", devname)) {
2603 exit(1);
2605 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2606 if (!opts) {
2607 error_report("already have a debugcon device");
2608 exit(1);
2610 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2611 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2612 return 0;
2615 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2617 const MachineClass *mc1 = a, *mc2 = b;
2618 int res;
2620 if (mc1->family == NULL) {
2621 if (mc2->family == NULL) {
2622 /* Compare standalone machine types against each other; they sort
2623 * in increasing order.
2625 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2626 object_class_get_name(OBJECT_CLASS(mc2)));
2629 /* Standalone machine types sort after families. */
2630 return 1;
2633 if (mc2->family == NULL) {
2634 /* Families sort before standalone machine types. */
2635 return -1;
2638 /* Families sort between each other alphabetically increasingly. */
2639 res = strcmp(mc1->family, mc2->family);
2640 if (res != 0) {
2641 return res;
2644 /* Within the same family, machine types sort in decreasing order. */
2645 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2646 object_class_get_name(OBJECT_CLASS(mc1)));
2649 static MachineClass *machine_parse(const char *name)
2651 MachineClass *mc = NULL;
2652 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2654 if (name) {
2655 mc = find_machine(name);
2657 if (mc) {
2658 g_slist_free(machines);
2659 return mc;
2661 if (name && !is_help_option(name)) {
2662 error_report("unsupported machine type");
2663 error_printf("Use -machine help to list supported machines\n");
2664 } else {
2665 printf("Supported machines are:\n");
2666 machines = g_slist_sort(machines, machine_class_cmp);
2667 for (el = machines; el; el = el->next) {
2668 MachineClass *mc = el->data;
2669 if (mc->alias) {
2670 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2672 printf("%-20s %s%s\n", mc->name, mc->desc,
2673 mc->is_default ? " (default)" : "");
2677 g_slist_free(machines);
2678 exit(!name || !is_help_option(name));
2681 void qemu_add_exit_notifier(Notifier *notify)
2683 notifier_list_add(&exit_notifiers, notify);
2686 void qemu_remove_exit_notifier(Notifier *notify)
2688 notifier_remove(notify);
2691 static void qemu_run_exit_notifiers(void)
2693 notifier_list_notify(&exit_notifiers, NULL);
2696 static bool machine_init_done;
2698 void qemu_add_machine_init_done_notifier(Notifier *notify)
2700 notifier_list_add(&machine_init_done_notifiers, notify);
2701 if (machine_init_done) {
2702 notify->notify(notify, NULL);
2706 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2708 notifier_remove(notify);
2711 static void qemu_run_machine_init_done_notifiers(void)
2713 notifier_list_notify(&machine_init_done_notifiers, NULL);
2714 machine_init_done = true;
2717 static const QEMUOption *lookup_opt(int argc, char **argv,
2718 const char **poptarg, int *poptind)
2720 const QEMUOption *popt;
2721 int optind = *poptind;
2722 char *r = argv[optind];
2723 const char *optarg;
2725 loc_set_cmdline(argv, optind, 1);
2726 optind++;
2727 /* Treat --foo the same as -foo. */
2728 if (r[1] == '-')
2729 r++;
2730 popt = qemu_options;
2731 for(;;) {
2732 if (!popt->name) {
2733 error_report("invalid option");
2734 exit(1);
2736 if (!strcmp(popt->name, r + 1))
2737 break;
2738 popt++;
2740 if (popt->flags & HAS_ARG) {
2741 if (optind >= argc) {
2742 error_report("requires an argument");
2743 exit(1);
2745 optarg = argv[optind++];
2746 loc_set_cmdline(argv, optind - 2, 2);
2747 } else {
2748 optarg = NULL;
2751 *poptarg = optarg;
2752 *poptind = optind;
2754 return popt;
2757 static MachineClass *select_machine(void)
2759 MachineClass *machine_class = find_default_machine();
2760 const char *optarg;
2761 QemuOpts *opts;
2762 Location loc;
2764 loc_push_none(&loc);
2766 opts = qemu_get_machine_opts();
2767 qemu_opts_loc_restore(opts);
2769 optarg = qemu_opt_get(opts, "type");
2770 if (optarg) {
2771 machine_class = machine_parse(optarg);
2774 if (!machine_class) {
2775 error_report("No machine specified, and there is no default");
2776 error_printf("Use -machine help to list supported machines\n");
2777 exit(1);
2780 loc_pop(&loc);
2781 return machine_class;
2784 static int machine_set_property(void *opaque,
2785 const char *name, const char *value,
2786 Error **errp)
2788 Object *obj = OBJECT(opaque);
2789 Error *local_err = NULL;
2790 char *p, *qom_name;
2792 if (strcmp(name, "type") == 0) {
2793 return 0;
2796 qom_name = g_strdup(name);
2797 for (p = qom_name; *p; p++) {
2798 if (*p == '_') {
2799 *p = '-';
2803 object_property_parse(obj, value, qom_name, &local_err);
2804 g_free(qom_name);
2806 if (local_err) {
2807 error_report_err(local_err);
2808 return -1;
2811 return 0;
2816 * Initial object creation happens before all other
2817 * QEMU data types are created. The majority of objects
2818 * can be created at this point. The rng-egd object
2819 * cannot be created here, as it depends on the chardev
2820 * already existing.
2822 static bool object_create_initial(const char *type)
2824 if (g_str_equal(type, "rng-egd") ||
2825 g_str_has_prefix(type, "pr-manager-")) {
2826 return false;
2829 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2830 if (g_str_equal(type, "cryptodev-vhost-user")) {
2831 return false;
2833 #endif
2836 * return false for concrete netfilters since
2837 * they depend on netdevs already existing
2839 if (g_str_equal(type, "filter-buffer") ||
2840 g_str_equal(type, "filter-dump") ||
2841 g_str_equal(type, "filter-mirror") ||
2842 g_str_equal(type, "filter-redirector") ||
2843 g_str_equal(type, "colo-compare") ||
2844 g_str_equal(type, "filter-rewriter") ||
2845 g_str_equal(type, "filter-replay")) {
2846 return false;
2849 /* Memory allocation by backends needs to be done
2850 * after configure_accelerator() (due to the tcg_enabled()
2851 * checks at memory_region_init_*()).
2853 * Also, allocation of large amounts of memory may delay
2854 * chardev initialization for too long, and trigger timeouts
2855 * on software that waits for a monitor socket to be created
2856 * (e.g. libvirt).
2858 if (g_str_has_prefix(type, "memory-backend-")) {
2859 return false;
2862 return true;
2867 * The remainder of object creation happens after the
2868 * creation of chardev, fsdev, net clients and device data types.
2870 static bool object_create_delayed(const char *type)
2872 return !object_create_initial(type);
2876 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2877 MachineClass *mc)
2879 uint64_t sz;
2880 const char *mem_str;
2881 const char *maxmem_str, *slots_str;
2882 const ram_addr_t default_ram_size = mc->default_ram_size;
2883 QemuOpts *opts = qemu_find_opts_singleton("memory");
2884 Location loc;
2886 loc_push_none(&loc);
2887 qemu_opts_loc_restore(opts);
2889 sz = 0;
2890 mem_str = qemu_opt_get(opts, "size");
2891 if (mem_str) {
2892 if (!*mem_str) {
2893 error_report("missing 'size' option value");
2894 exit(EXIT_FAILURE);
2897 sz = qemu_opt_get_size(opts, "size", ram_size);
2899 /* Fix up legacy suffix-less format */
2900 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2901 uint64_t overflow_check = sz;
2903 sz <<= 20;
2904 if ((sz >> 20) != overflow_check) {
2905 error_report("too large 'size' option value");
2906 exit(EXIT_FAILURE);
2911 /* backward compatibility behaviour for case "-m 0" */
2912 if (sz == 0) {
2913 sz = default_ram_size;
2916 sz = QEMU_ALIGN_UP(sz, 8192);
2917 ram_size = sz;
2918 if (ram_size != sz) {
2919 error_report("ram size too large");
2920 exit(EXIT_FAILURE);
2923 /* store value for the future use */
2924 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2925 *maxram_size = ram_size;
2927 maxmem_str = qemu_opt_get(opts, "maxmem");
2928 slots_str = qemu_opt_get(opts, "slots");
2929 if (maxmem_str && slots_str) {
2930 uint64_t slots;
2932 sz = qemu_opt_get_size(opts, "maxmem", 0);
2933 slots = qemu_opt_get_number(opts, "slots", 0);
2934 if (sz < ram_size) {
2935 error_report("invalid value of -m option maxmem: "
2936 "maximum memory size (0x%" PRIx64 ") must be at least "
2937 "the initial memory size (0x" RAM_ADDR_FMT ")",
2938 sz, ram_size);
2939 exit(EXIT_FAILURE);
2940 } else if (sz > ram_size) {
2941 if (!slots) {
2942 error_report("invalid value of -m option: maxmem was "
2943 "specified, but no hotplug slots were specified");
2944 exit(EXIT_FAILURE);
2946 } else if (slots) {
2947 error_report("invalid value of -m option maxmem: "
2948 "memory slots were specified but maximum memory size "
2949 "(0x%" PRIx64 ") is equal to the initial memory size "
2950 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2951 exit(EXIT_FAILURE);
2954 *maxram_size = sz;
2955 *ram_slots = slots;
2956 } else if ((!maxmem_str && slots_str) ||
2957 (maxmem_str && !slots_str)) {
2958 error_report("invalid -m option value: missing "
2959 "'%s' option", slots_str ? "maxmem" : "slots");
2960 exit(EXIT_FAILURE);
2963 loc_pop(&loc);
2966 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2968 GlobalProperty *g;
2970 g = g_malloc0(sizeof(*g));
2971 g->driver = qemu_opt_get(opts, "driver");
2972 g->property = qemu_opt_get(opts, "property");
2973 g->value = qemu_opt_get(opts, "value");
2974 g->user_provided = true;
2975 g->errp = &error_fatal;
2976 qdev_prop_register_global(g);
2977 return 0;
2980 static int qemu_read_default_config_file(void)
2982 int ret;
2984 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2985 if (ret < 0 && ret != -ENOENT) {
2986 return ret;
2989 return 0;
2992 static void user_register_global_props(void)
2994 qemu_opts_foreach(qemu_find_opts("global"),
2995 global_init_func, NULL, NULL);
2999 * Note: we should see that these properties are actually having a
3000 * priority: accel < machine < user. This means e.g. when user
3001 * specifies something in "-global", it'll always be used with highest
3002 * priority than either machine/accelerator compat properties.
3004 static void register_global_properties(MachineState *ms)
3006 accel_register_compat_props(ms->accelerator);
3007 machine_register_compat_props(ms);
3008 user_register_global_props();
3011 int main(int argc, char **argv, char **envp)
3013 int i;
3014 int snapshot, linux_boot;
3015 const char *initrd_filename;
3016 const char *kernel_filename, *kernel_cmdline;
3017 const char *boot_order = NULL;
3018 const char *boot_once = NULL;
3019 DisplayState *ds;
3020 QemuOpts *opts, *machine_opts;
3021 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
3022 QemuOptsList *olist;
3023 int optind;
3024 const char *optarg;
3025 const char *loadvm = NULL;
3026 MachineClass *machine_class;
3027 const char *cpu_model;
3028 const char *vga_model = NULL;
3029 const char *qtest_chrdev = NULL;
3030 const char *qtest_log = NULL;
3031 const char *pid_file = NULL;
3032 const char *incoming = NULL;
3033 bool userconfig = true;
3034 bool nographic = false;
3035 int display_remote = 0;
3036 const char *log_mask = NULL;
3037 const char *log_file = NULL;
3038 char *trace_file = NULL;
3039 ram_addr_t maxram_size;
3040 uint64_t ram_slots = 0;
3041 FILE *vmstate_dump_file = NULL;
3042 Error *main_loop_err = NULL;
3043 Error *err = NULL;
3044 bool list_data_dirs = false;
3045 char *dir, **dirs;
3046 typedef struct BlockdevOptions_queue {
3047 BlockdevOptions *bdo;
3048 Location loc;
3049 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3050 } BlockdevOptions_queue;
3051 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3052 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3054 module_call_init(MODULE_INIT_TRACE);
3056 qemu_init_cpu_list();
3057 qemu_init_cpu_loop();
3058 qemu_mutex_lock_iothread();
3060 atexit(qemu_run_exit_notifiers);
3061 error_set_progname(argv[0]);
3062 qemu_init_exec_dir(argv[0]);
3064 module_call_init(MODULE_INIT_QOM);
3065 monitor_init_qmp_commands();
3067 qemu_add_opts(&qemu_drive_opts);
3068 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3069 qemu_add_drive_opts(&qemu_common_drive_opts);
3070 qemu_add_drive_opts(&qemu_drive_opts);
3071 qemu_add_drive_opts(&bdrv_runtime_opts);
3072 qemu_add_opts(&qemu_chardev_opts);
3073 qemu_add_opts(&qemu_device_opts);
3074 qemu_add_opts(&qemu_netdev_opts);
3075 qemu_add_opts(&qemu_nic_opts);
3076 qemu_add_opts(&qemu_net_opts);
3077 qemu_add_opts(&qemu_rtc_opts);
3078 qemu_add_opts(&qemu_global_opts);
3079 qemu_add_opts(&qemu_mon_opts);
3080 qemu_add_opts(&qemu_trace_opts);
3081 qemu_add_opts(&qemu_option_rom_opts);
3082 qemu_add_opts(&qemu_machine_opts);
3083 qemu_add_opts(&qemu_accel_opts);
3084 qemu_add_opts(&qemu_mem_opts);
3085 qemu_add_opts(&qemu_smp_opts);
3086 qemu_add_opts(&qemu_boot_opts);
3087 qemu_add_opts(&qemu_sandbox_opts);
3088 qemu_add_opts(&qemu_add_fd_opts);
3089 qemu_add_opts(&qemu_object_opts);
3090 qemu_add_opts(&qemu_tpmdev_opts);
3091 qemu_add_opts(&qemu_realtime_opts);
3092 qemu_add_opts(&qemu_msg_opts);
3093 qemu_add_opts(&qemu_name_opts);
3094 qemu_add_opts(&qemu_numa_opts);
3095 qemu_add_opts(&qemu_icount_opts);
3096 qemu_add_opts(&qemu_semihosting_config_opts);
3097 qemu_add_opts(&qemu_fw_cfg_opts);
3098 module_call_init(MODULE_INIT_OPTS);
3100 runstate_init();
3102 if (qcrypto_init(&err) < 0) {
3103 error_reportf_err(err, "cannot initialize crypto: ");
3104 exit(1);
3106 rtc_clock = QEMU_CLOCK_HOST;
3108 QLIST_INIT (&vm_change_state_head);
3109 os_setup_early_signal_handling();
3111 cpu_model = NULL;
3112 snapshot = 0;
3114 nb_nics = 0;
3116 bdrv_init_with_whitelist();
3118 autostart = 1;
3120 /* first pass of option parsing */
3121 optind = 1;
3122 while (optind < argc) {
3123 if (argv[optind][0] != '-') {
3124 /* disk image */
3125 optind++;
3126 } else {
3127 const QEMUOption *popt;
3129 popt = lookup_opt(argc, argv, &optarg, &optind);
3130 switch (popt->index) {
3131 case QEMU_OPTION_nodefconfig:
3132 case QEMU_OPTION_nouserconfig:
3133 userconfig = false;
3134 break;
3139 if (userconfig) {
3140 if (qemu_read_default_config_file() < 0) {
3141 exit(1);
3145 /* second pass of option parsing */
3146 optind = 1;
3147 for(;;) {
3148 if (optind >= argc)
3149 break;
3150 if (argv[optind][0] != '-') {
3151 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3152 } else {
3153 const QEMUOption *popt;
3155 popt = lookup_opt(argc, argv, &optarg, &optind);
3156 if (!(popt->arch_mask & arch_type)) {
3157 error_report("Option not supported for this target");
3158 exit(1);
3160 switch(popt->index) {
3161 case QEMU_OPTION_no_kvm_irqchip: {
3162 olist = qemu_find_opts("machine");
3163 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3164 break;
3166 case QEMU_OPTION_cpu:
3167 /* hw initialization will check this */
3168 cpu_model = optarg;
3169 break;
3170 case QEMU_OPTION_hda:
3171 case QEMU_OPTION_hdb:
3172 case QEMU_OPTION_hdc:
3173 case QEMU_OPTION_hdd:
3174 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3175 HD_OPTS);
3176 break;
3177 case QEMU_OPTION_blockdev:
3179 Visitor *v;
3180 BlockdevOptions_queue *bdo;
3182 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3183 if (!v) {
3184 error_report_err(err);
3185 exit(1);
3188 bdo = g_new(BlockdevOptions_queue, 1);
3189 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3190 &error_fatal);
3191 visit_free(v);
3192 loc_save(&bdo->loc);
3193 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3194 break;
3196 case QEMU_OPTION_drive:
3197 if (drive_def(optarg) == NULL) {
3198 exit(1);
3200 break;
3201 case QEMU_OPTION_set:
3202 if (qemu_set_option(optarg) != 0)
3203 exit(1);
3204 break;
3205 case QEMU_OPTION_global:
3206 if (qemu_global_option(optarg) != 0)
3207 exit(1);
3208 break;
3209 case QEMU_OPTION_mtdblock:
3210 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3211 break;
3212 case QEMU_OPTION_sd:
3213 drive_add(IF_SD, -1, optarg, SD_OPTS);
3214 break;
3215 case QEMU_OPTION_pflash:
3216 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3217 break;
3218 case QEMU_OPTION_snapshot:
3219 snapshot = 1;
3220 break;
3221 case QEMU_OPTION_numa:
3222 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3223 optarg, true);
3224 if (!opts) {
3225 exit(1);
3227 break;
3228 case QEMU_OPTION_display:
3229 parse_display(optarg);
3230 break;
3231 case QEMU_OPTION_nographic:
3232 olist = qemu_find_opts("machine");
3233 qemu_opts_parse_noisily(olist, "graphics=off", false);
3234 nographic = true;
3235 dpy.type = DISPLAY_TYPE_NONE;
3236 break;
3237 case QEMU_OPTION_curses:
3238 #ifdef CONFIG_CURSES
3239 dpy.type = DISPLAY_TYPE_CURSES;
3240 #else
3241 error_report("curses support is disabled");
3242 exit(1);
3243 #endif
3244 break;
3245 case QEMU_OPTION_portrait:
3246 graphic_rotate = 90;
3247 break;
3248 case QEMU_OPTION_rotate:
3249 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3250 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3251 graphic_rotate != 180 && graphic_rotate != 270) {
3252 error_report("only 90, 180, 270 deg rotation is available");
3253 exit(1);
3255 break;
3256 case QEMU_OPTION_kernel:
3257 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3258 &error_abort);
3259 break;
3260 case QEMU_OPTION_initrd:
3261 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3262 &error_abort);
3263 break;
3264 case QEMU_OPTION_append:
3265 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3266 &error_abort);
3267 break;
3268 case QEMU_OPTION_dtb:
3269 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3270 &error_abort);
3271 break;
3272 case QEMU_OPTION_cdrom:
3273 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3274 break;
3275 case QEMU_OPTION_boot:
3276 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3277 optarg, true);
3278 if (!opts) {
3279 exit(1);
3281 break;
3282 case QEMU_OPTION_fda:
3283 case QEMU_OPTION_fdb:
3284 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3285 optarg, FD_OPTS);
3286 break;
3287 case QEMU_OPTION_no_fd_bootchk:
3288 fd_bootchk = 0;
3289 break;
3290 case QEMU_OPTION_netdev:
3291 default_net = 0;
3292 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3293 exit(1);
3295 break;
3296 case QEMU_OPTION_nic:
3297 default_net = 0;
3298 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3299 exit(1);
3301 break;
3302 case QEMU_OPTION_net:
3303 default_net = 0;
3304 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3305 exit(1);
3307 break;
3308 #ifdef CONFIG_LIBISCSI
3309 case QEMU_OPTION_iscsi:
3310 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3311 optarg, false);
3312 if (!opts) {
3313 exit(1);
3315 break;
3316 #endif
3317 #ifdef CONFIG_SLIRP
3318 case QEMU_OPTION_tftp:
3319 error_report("The -tftp option is deprecated. "
3320 "Please use '-netdev user,tftp=...' instead.");
3321 legacy_tftp_prefix = optarg;
3322 break;
3323 case QEMU_OPTION_bootp:
3324 error_report("The -bootp option is deprecated. "
3325 "Please use '-netdev user,bootfile=...' instead.");
3326 legacy_bootp_filename = optarg;
3327 break;
3328 case QEMU_OPTION_redir:
3329 error_report("The -redir option is deprecated. "
3330 "Please use '-netdev user,hostfwd=...' instead.");
3331 if (net_slirp_redir(optarg) < 0)
3332 exit(1);
3333 break;
3334 #endif
3335 case QEMU_OPTION_bt:
3336 add_device_config(DEV_BT, optarg);
3337 break;
3338 case QEMU_OPTION_audio_help:
3339 AUD_help ();
3340 exit (0);
3341 break;
3342 case QEMU_OPTION_soundhw:
3343 select_soundhw (optarg);
3344 break;
3345 case QEMU_OPTION_h:
3346 help(0);
3347 break;
3348 case QEMU_OPTION_version:
3349 version();
3350 exit(0);
3351 break;
3352 case QEMU_OPTION_m:
3353 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3354 optarg, true);
3355 if (!opts) {
3356 exit(EXIT_FAILURE);
3358 break;
3359 #ifdef CONFIG_TPM
3360 case QEMU_OPTION_tpmdev:
3361 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3362 exit(1);
3364 break;
3365 #endif
3366 case QEMU_OPTION_mempath:
3367 mem_path = optarg;
3368 break;
3369 case QEMU_OPTION_mem_prealloc:
3370 mem_prealloc = 1;
3371 break;
3372 case QEMU_OPTION_d:
3373 log_mask = optarg;
3374 break;
3375 case QEMU_OPTION_D:
3376 log_file = optarg;
3377 break;
3378 case QEMU_OPTION_DFILTER:
3379 qemu_set_dfilter_ranges(optarg, &error_fatal);
3380 break;
3381 case QEMU_OPTION_s:
3382 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3383 break;
3384 case QEMU_OPTION_gdb:
3385 add_device_config(DEV_GDB, optarg);
3386 break;
3387 case QEMU_OPTION_L:
3388 if (is_help_option(optarg)) {
3389 list_data_dirs = true;
3390 } else {
3391 qemu_add_data_dir(optarg);
3393 break;
3394 case QEMU_OPTION_bios:
3395 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3396 &error_abort);
3397 break;
3398 case QEMU_OPTION_singlestep:
3399 singlestep = 1;
3400 break;
3401 case QEMU_OPTION_S:
3402 autostart = 0;
3403 break;
3404 case QEMU_OPTION_k:
3405 keyboard_layout = optarg;
3406 break;
3407 case QEMU_OPTION_localtime:
3408 rtc_utc = 0;
3409 break;
3410 case QEMU_OPTION_vga:
3411 vga_model = optarg;
3412 default_vga = 0;
3413 break;
3414 case QEMU_OPTION_g:
3416 const char *p;
3417 int w, h, depth;
3418 p = optarg;
3419 w = strtol(p, (char **)&p, 10);
3420 if (w <= 0) {
3421 graphic_error:
3422 error_report("invalid resolution or depth");
3423 exit(1);
3425 if (*p != 'x')
3426 goto graphic_error;
3427 p++;
3428 h = strtol(p, (char **)&p, 10);
3429 if (h <= 0)
3430 goto graphic_error;
3431 if (*p == 'x') {
3432 p++;
3433 depth = strtol(p, (char **)&p, 10);
3434 if (depth != 8 && depth != 15 && depth != 16 &&
3435 depth != 24 && depth != 32)
3436 goto graphic_error;
3437 } else if (*p == '\0') {
3438 depth = graphic_depth;
3439 } else {
3440 goto graphic_error;
3443 graphic_width = w;
3444 graphic_height = h;
3445 graphic_depth = depth;
3447 break;
3448 case QEMU_OPTION_echr:
3450 char *r;
3451 term_escape_char = strtol(optarg, &r, 0);
3452 if (r == optarg)
3453 printf("Bad argument to echr\n");
3454 break;
3456 case QEMU_OPTION_monitor:
3457 default_monitor = 0;
3458 if (strncmp(optarg, "none", 4)) {
3459 monitor_parse(optarg, "readline", false);
3461 break;
3462 case QEMU_OPTION_qmp:
3463 monitor_parse(optarg, "control", false);
3464 default_monitor = 0;
3465 break;
3466 case QEMU_OPTION_qmp_pretty:
3467 monitor_parse(optarg, "control", true);
3468 default_monitor = 0;
3469 break;
3470 case QEMU_OPTION_mon:
3471 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3472 true);
3473 if (!opts) {
3474 exit(1);
3476 default_monitor = 0;
3477 break;
3478 case QEMU_OPTION_chardev:
3479 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3480 optarg, true);
3481 if (!opts) {
3482 exit(1);
3484 break;
3485 case QEMU_OPTION_fsdev:
3486 olist = qemu_find_opts("fsdev");
3487 if (!olist) {
3488 error_report("fsdev support is disabled");
3489 exit(1);
3491 opts = qemu_opts_parse_noisily(olist, optarg, true);
3492 if (!opts) {
3493 exit(1);
3495 break;
3496 case QEMU_OPTION_virtfs: {
3497 QemuOpts *fsdev;
3498 QemuOpts *device;
3499 const char *writeout, *sock_fd, *socket, *path, *security_model;
3501 olist = qemu_find_opts("virtfs");
3502 if (!olist) {
3503 error_report("virtfs support is disabled");
3504 exit(1);
3506 opts = qemu_opts_parse_noisily(olist, optarg, true);
3507 if (!opts) {
3508 exit(1);
3511 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3512 qemu_opt_get(opts, "mount_tag") == NULL) {
3513 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3514 exit(1);
3516 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3517 qemu_opts_id(opts) ?:
3518 qemu_opt_get(opts, "mount_tag"),
3519 1, NULL);
3520 if (!fsdev) {
3521 error_report("duplicate or invalid fsdev id: %s",
3522 qemu_opt_get(opts, "mount_tag"));
3523 exit(1);
3526 writeout = qemu_opt_get(opts, "writeout");
3527 if (writeout) {
3528 #ifdef CONFIG_SYNC_FILE_RANGE
3529 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3530 #else
3531 error_report("writeout=immediate not supported "
3532 "on this platform");
3533 exit(1);
3534 #endif
3536 qemu_opt_set(fsdev, "fsdriver",
3537 qemu_opt_get(opts, "fsdriver"), &error_abort);
3538 path = qemu_opt_get(opts, "path");
3539 if (path) {
3540 qemu_opt_set(fsdev, "path", path, &error_abort);
3542 security_model = qemu_opt_get(opts, "security_model");
3543 if (security_model) {
3544 qemu_opt_set(fsdev, "security_model", security_model,
3545 &error_abort);
3547 socket = qemu_opt_get(opts, "socket");
3548 if (socket) {
3549 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3551 sock_fd = qemu_opt_get(opts, "sock_fd");
3552 if (sock_fd) {
3553 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3556 qemu_opt_set_bool(fsdev, "readonly",
3557 qemu_opt_get_bool(opts, "readonly", 0),
3558 &error_abort);
3559 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3560 &error_abort);
3561 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3562 qemu_opt_set(device, "fsdev",
3563 qemu_opts_id(fsdev), &error_abort);
3564 qemu_opt_set(device, "mount_tag",
3565 qemu_opt_get(opts, "mount_tag"), &error_abort);
3566 break;
3568 case QEMU_OPTION_virtfs_synth: {
3569 QemuOpts *fsdev;
3570 QemuOpts *device;
3572 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3573 1, NULL);
3574 if (!fsdev) {
3575 error_report("duplicate option: %s", "virtfs_synth");
3576 exit(1);
3578 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3580 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3581 &error_abort);
3582 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3583 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3584 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3585 break;
3587 case QEMU_OPTION_serial:
3588 add_device_config(DEV_SERIAL, optarg);
3589 default_serial = 0;
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3593 break;
3594 case QEMU_OPTION_watchdog:
3595 if (watchdog) {
3596 error_report("only one watchdog option may be given");
3597 return 1;
3599 watchdog = optarg;
3600 break;
3601 case QEMU_OPTION_watchdog_action:
3602 if (select_watchdog_action(optarg) == -1) {
3603 error_report("unknown -watchdog-action parameter");
3604 exit(1);
3606 break;
3607 case QEMU_OPTION_virtiocon:
3608 add_device_config(DEV_VIRTCON, optarg);
3609 default_virtcon = 0;
3610 if (strncmp(optarg, "mon:", 4) == 0) {
3611 default_monitor = 0;
3613 break;
3614 case QEMU_OPTION_parallel:
3615 add_device_config(DEV_PARALLEL, optarg);
3616 default_parallel = 0;
3617 if (strncmp(optarg, "mon:", 4) == 0) {
3618 default_monitor = 0;
3620 break;
3621 case QEMU_OPTION_debugcon:
3622 add_device_config(DEV_DEBUGCON, optarg);
3623 break;
3624 case QEMU_OPTION_loadvm:
3625 loadvm = optarg;
3626 break;
3627 case QEMU_OPTION_full_screen:
3628 dpy.has_full_screen = true;
3629 dpy.full_screen = true;
3630 break;
3631 case QEMU_OPTION_no_frame:
3632 g_printerr("The -no-frame switch is deprecated, and will be\n"
3633 "removed in a future release.\n");
3634 no_frame = 1;
3635 break;
3636 case QEMU_OPTION_alt_grab:
3637 alt_grab = 1;
3638 break;
3639 case QEMU_OPTION_ctrl_grab:
3640 ctrl_grab = 1;
3641 break;
3642 case QEMU_OPTION_no_quit:
3643 dpy.has_window_close = true;
3644 dpy.window_close = false;
3645 break;
3646 case QEMU_OPTION_sdl:
3647 #ifdef CONFIG_SDL
3648 dpy.type = DISPLAY_TYPE_SDL;
3649 break;
3650 #else
3651 error_report("SDL support is disabled");
3652 exit(1);
3653 #endif
3654 case QEMU_OPTION_pidfile:
3655 pid_file = optarg;
3656 break;
3657 case QEMU_OPTION_win2k_hack:
3658 win2k_install_hack = 1;
3659 break;
3660 case QEMU_OPTION_rtc_td_hack: {
3661 static GlobalProperty slew_lost_ticks = {
3662 .driver = "mc146818rtc",
3663 .property = "lost_tick_policy",
3664 .value = "slew",
3667 qdev_prop_register_global(&slew_lost_ticks);
3668 break;
3670 case QEMU_OPTION_acpitable:
3671 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3672 optarg, true);
3673 if (!opts) {
3674 exit(1);
3676 acpi_table_add(opts, &error_fatal);
3677 break;
3678 case QEMU_OPTION_smbios:
3679 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3680 optarg, false);
3681 if (!opts) {
3682 exit(1);
3684 smbios_entry_add(opts, &error_fatal);
3685 break;
3686 case QEMU_OPTION_fwcfg:
3687 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3688 optarg, true);
3689 if (opts == NULL) {
3690 exit(1);
3692 break;
3693 case QEMU_OPTION_enable_kvm:
3694 olist = qemu_find_opts("machine");
3695 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3696 break;
3697 case QEMU_OPTION_enable_hax:
3698 olist = qemu_find_opts("machine");
3699 qemu_opts_parse_noisily(olist, "accel=hax", false);
3700 break;
3701 case QEMU_OPTION_M:
3702 case QEMU_OPTION_machine:
3703 olist = qemu_find_opts("machine");
3704 opts = qemu_opts_parse_noisily(olist, optarg, true);
3705 if (!opts) {
3706 exit(1);
3708 break;
3709 case QEMU_OPTION_no_kvm:
3710 olist = qemu_find_opts("machine");
3711 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3712 break;
3713 case QEMU_OPTION_no_kvm_pit_reinjection: {
3714 static GlobalProperty kvm_pit_lost_tick_policy = {
3715 .driver = "kvm-pit",
3716 .property = "lost_tick_policy",
3717 .value = "discard",
3720 warn_report("deprecated, replaced by "
3721 "-global kvm-pit.lost_tick_policy=discard");
3722 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3723 break;
3725 case QEMU_OPTION_accel:
3726 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3727 optarg, true);
3728 optarg = qemu_opt_get(accel_opts, "accel");
3729 if (!optarg || is_help_option(optarg)) {
3730 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3731 exit(0);
3733 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3734 false, &error_abort);
3735 qemu_opt_set(opts, "accel", optarg, &error_abort);
3736 break;
3737 case QEMU_OPTION_usb:
3738 olist = qemu_find_opts("machine");
3739 qemu_opts_parse_noisily(olist, "usb=on", false);
3740 break;
3741 case QEMU_OPTION_usbdevice:
3742 error_report("'-usbdevice' is deprecated, please use "
3743 "'-device usb-...' instead");
3744 olist = qemu_find_opts("machine");
3745 qemu_opts_parse_noisily(olist, "usb=on", false);
3746 add_device_config(DEV_USB, optarg);
3747 break;
3748 case QEMU_OPTION_device:
3749 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3750 optarg, true)) {
3751 exit(1);
3753 break;
3754 case QEMU_OPTION_smp:
3755 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3756 optarg, true)) {
3757 exit(1);
3759 break;
3760 case QEMU_OPTION_vnc:
3761 vnc_parse(optarg, &error_fatal);
3762 break;
3763 case QEMU_OPTION_no_acpi:
3764 acpi_enabled = 0;
3765 break;
3766 case QEMU_OPTION_no_hpet:
3767 no_hpet = 1;
3768 break;
3769 case QEMU_OPTION_balloon:
3770 if (balloon_parse(optarg) < 0) {
3771 error_report("unknown -balloon argument %s", optarg);
3772 exit(1);
3774 break;
3775 case QEMU_OPTION_no_reboot:
3776 no_reboot = 1;
3777 break;
3778 case QEMU_OPTION_no_shutdown:
3779 no_shutdown = 1;
3780 break;
3781 case QEMU_OPTION_show_cursor:
3782 cursor_hide = 0;
3783 break;
3784 case QEMU_OPTION_uuid:
3785 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3786 error_report("failed to parse UUID string: wrong format");
3787 exit(1);
3789 qemu_uuid_set = true;
3790 break;
3791 case QEMU_OPTION_option_rom:
3792 if (nb_option_roms >= MAX_OPTION_ROMS) {
3793 error_report("too many option ROMs");
3794 exit(1);
3796 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3797 optarg, true);
3798 if (!opts) {
3799 exit(1);
3801 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3802 option_rom[nb_option_roms].bootindex =
3803 qemu_opt_get_number(opts, "bootindex", -1);
3804 if (!option_rom[nb_option_roms].name) {
3805 error_report("Option ROM file is not specified");
3806 exit(1);
3808 nb_option_roms++;
3809 break;
3810 case QEMU_OPTION_semihosting:
3811 semihosting.enabled = true;
3812 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3813 break;
3814 case QEMU_OPTION_semihosting_config:
3815 semihosting.enabled = true;
3816 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3817 optarg, false);
3818 if (opts != NULL) {
3819 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3820 true);
3821 const char *target = qemu_opt_get(opts, "target");
3822 if (target != NULL) {
3823 if (strcmp("native", target) == 0) {
3824 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3825 } else if (strcmp("gdb", target) == 0) {
3826 semihosting.target = SEMIHOSTING_TARGET_GDB;
3827 } else if (strcmp("auto", target) == 0) {
3828 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3829 } else {
3830 error_report("unsupported semihosting-config %s",
3831 optarg);
3832 exit(1);
3834 } else {
3835 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3837 /* Set semihosting argument count and vector */
3838 qemu_opt_foreach(opts, add_semihosting_arg,
3839 &semihosting, NULL);
3840 } else {
3841 error_report("unsupported semihosting-config %s", optarg);
3842 exit(1);
3844 break;
3845 case QEMU_OPTION_tdf:
3846 warn_report("ignoring deprecated option");
3847 break;
3848 case QEMU_OPTION_name:
3849 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3850 optarg, true);
3851 if (!opts) {
3852 exit(1);
3854 break;
3855 case QEMU_OPTION_prom_env:
3856 if (nb_prom_envs >= MAX_PROM_ENVS) {
3857 error_report("too many prom variables");
3858 exit(1);
3860 prom_envs[nb_prom_envs] = optarg;
3861 nb_prom_envs++;
3862 break;
3863 case QEMU_OPTION_old_param:
3864 old_param = 1;
3865 break;
3866 case QEMU_OPTION_clock:
3867 /* Clock options no longer exist. Keep this option for
3868 * backward compatibility.
3870 break;
3871 case QEMU_OPTION_startdate:
3872 configure_rtc_date_offset(optarg, 1);
3873 break;
3874 case QEMU_OPTION_rtc:
3875 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3876 false);
3877 if (!opts) {
3878 exit(1);
3880 configure_rtc(opts);
3881 break;
3882 case QEMU_OPTION_tb_size:
3883 #ifndef CONFIG_TCG
3884 error_report("TCG is disabled");
3885 exit(1);
3886 #endif
3887 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3888 error_report("Invalid argument to -tb-size");
3889 exit(1);
3891 break;
3892 case QEMU_OPTION_icount:
3893 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3894 optarg, true);
3895 if (!icount_opts) {
3896 exit(1);
3898 break;
3899 case QEMU_OPTION_incoming:
3900 if (!incoming) {
3901 runstate_set(RUN_STATE_INMIGRATE);
3903 incoming = optarg;
3904 break;
3905 case QEMU_OPTION_only_migratable:
3907 * TODO: we can remove this option one day, and we
3908 * should all use:
3910 * "-global migration.only-migratable=true"
3912 qemu_global_option("migration.only-migratable=true");
3913 break;
3914 case QEMU_OPTION_nodefaults:
3915 has_defaults = 0;
3916 break;
3917 case QEMU_OPTION_xen_domid:
3918 if (!(xen_available())) {
3919 error_report("Option not supported for this target");
3920 exit(1);
3922 xen_domid = atoi(optarg);
3923 break;
3924 case QEMU_OPTION_xen_create:
3925 if (!(xen_available())) {
3926 error_report("Option not supported for this target");
3927 exit(1);
3929 xen_mode = XEN_CREATE;
3930 break;
3931 case QEMU_OPTION_xen_attach:
3932 if (!(xen_available())) {
3933 error_report("Option not supported for this target");
3934 exit(1);
3936 xen_mode = XEN_ATTACH;
3937 break;
3938 case QEMU_OPTION_xen_domid_restrict:
3939 if (!(xen_available())) {
3940 error_report("Option not supported for this target");
3941 exit(1);
3943 xen_domid_restrict = true;
3944 break;
3945 case QEMU_OPTION_trace:
3946 g_free(trace_file);
3947 trace_file = trace_opt_parse(optarg);
3948 break;
3949 case QEMU_OPTION_readconfig:
3951 int ret = qemu_read_config_file(optarg);
3952 if (ret < 0) {
3953 error_report("read config %s: %s", optarg,
3954 strerror(-ret));
3955 exit(1);
3957 break;
3959 case QEMU_OPTION_spice:
3960 olist = qemu_find_opts("spice");
3961 if (!olist) {
3962 error_report("spice support is disabled");
3963 exit(1);
3965 opts = qemu_opts_parse_noisily(olist, optarg, false);
3966 if (!opts) {
3967 exit(1);
3969 display_remote++;
3970 break;
3971 case QEMU_OPTION_writeconfig:
3973 FILE *fp;
3974 if (strcmp(optarg, "-") == 0) {
3975 fp = stdout;
3976 } else {
3977 fp = fopen(optarg, "w");
3978 if (fp == NULL) {
3979 error_report("open %s: %s", optarg,
3980 strerror(errno));
3981 exit(1);
3984 qemu_config_write(fp);
3985 if (fp != stdout) {
3986 fclose(fp);
3988 break;
3990 case QEMU_OPTION_qtest:
3991 qtest_chrdev = optarg;
3992 break;
3993 case QEMU_OPTION_qtest_log:
3994 qtest_log = optarg;
3995 break;
3996 case QEMU_OPTION_sandbox:
3997 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3998 optarg, true);
3999 if (!opts) {
4000 exit(1);
4002 break;
4003 case QEMU_OPTION_add_fd:
4004 #ifndef _WIN32
4005 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4006 optarg, false);
4007 if (!opts) {
4008 exit(1);
4010 #else
4011 error_report("File descriptor passing is disabled on this "
4012 "platform");
4013 exit(1);
4014 #endif
4015 break;
4016 case QEMU_OPTION_object:
4017 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4018 optarg, true);
4019 if (!opts) {
4020 exit(1);
4022 break;
4023 case QEMU_OPTION_realtime:
4024 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4025 optarg, false);
4026 if (!opts) {
4027 exit(1);
4029 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4030 break;
4031 case QEMU_OPTION_msg:
4032 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4033 false);
4034 if (!opts) {
4035 exit(1);
4037 configure_msg(opts);
4038 break;
4039 case QEMU_OPTION_dump_vmstate:
4040 if (vmstate_dump_file) {
4041 error_report("only one '-dump-vmstate' "
4042 "option may be given");
4043 exit(1);
4045 vmstate_dump_file = fopen(optarg, "w");
4046 if (vmstate_dump_file == NULL) {
4047 error_report("open %s: %s", optarg, strerror(errno));
4048 exit(1);
4050 break;
4051 default:
4052 os_parse_cmd_args(popt->index, optarg);
4057 * Clear error location left behind by the loop.
4058 * Best done right after the loop. Do not insert code here!
4060 loc_set_none();
4062 replay_configure(icount_opts);
4064 machine_class = select_machine();
4066 set_memory_options(&ram_slots, &maxram_size, machine_class);
4068 os_daemonize();
4069 rcu_disable_atfork();
4071 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4072 error_report("could not acquire pid file: %s", strerror(errno));
4073 exit(1);
4076 if (qemu_init_main_loop(&main_loop_err)) {
4077 error_report_err(main_loop_err);
4078 exit(1);
4081 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4082 parse_sandbox, NULL, NULL)) {
4083 exit(1);
4086 if (qemu_opts_foreach(qemu_find_opts("name"),
4087 parse_name, NULL, NULL)) {
4088 exit(1);
4091 #ifndef _WIN32
4092 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4093 parse_add_fd, NULL, NULL)) {
4094 exit(1);
4097 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4098 cleanup_add_fd, NULL, NULL)) {
4099 exit(1);
4101 #endif
4103 current_machine = MACHINE(object_new(object_class_get_name(
4104 OBJECT_CLASS(machine_class))));
4105 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4106 exit(0);
4108 object_property_add_child(object_get_root(), "machine",
4109 OBJECT(current_machine), &error_abort);
4111 if (machine_class->minimum_page_bits) {
4112 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4113 /* This would be a board error: specifying a minimum smaller than
4114 * a target's compile-time fixed setting.
4116 g_assert_not_reached();
4120 cpu_exec_init_all();
4122 if (machine_class->hw_version) {
4123 qemu_set_hw_version(machine_class->hw_version);
4126 if (cpu_model && is_help_option(cpu_model)) {
4127 list_cpus(stdout, &fprintf, cpu_model);
4128 exit(0);
4131 if (!trace_init_backends()) {
4132 exit(1);
4134 trace_init_file(trace_file);
4136 /* Open the logfile at this point and set the log mask if necessary.
4138 if (log_file) {
4139 qemu_set_log_filename(log_file, &error_fatal);
4142 if (log_mask) {
4143 int mask;
4144 mask = qemu_str_to_log_mask(log_mask);
4145 if (!mask) {
4146 qemu_print_log_usage(stdout);
4147 exit(1);
4149 qemu_set_log(mask);
4150 } else {
4151 qemu_set_log(0);
4154 /* add configured firmware directories */
4155 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4156 for (i = 0; dirs[i] != NULL; i++) {
4157 qemu_add_data_dir(dirs[i]);
4159 g_strfreev(dirs);
4161 /* try to find datadir relative to the executable path */
4162 dir = os_find_datadir();
4163 qemu_add_data_dir(dir);
4164 g_free(dir);
4166 /* add the datadir specified when building */
4167 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4169 /* -L help lists the data directories and exits. */
4170 if (list_data_dirs) {
4171 for (i = 0; i < data_dir_idx; i++) {
4172 printf("%s\n", data_dir[i]);
4174 exit(0);
4177 /* machine_class: default to UP */
4178 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4179 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4180 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4182 /* default to machine_class->default_cpus */
4183 smp_cpus = machine_class->default_cpus;
4184 max_cpus = machine_class->default_cpus;
4186 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4188 /* sanity-check smp_cpus and max_cpus against machine_class */
4189 if (smp_cpus < machine_class->min_cpus) {
4190 error_report("Invalid SMP CPUs %d. The min CPUs "
4191 "supported by machine '%s' is %d", smp_cpus,
4192 machine_class->name, machine_class->min_cpus);
4193 exit(1);
4195 if (max_cpus > machine_class->max_cpus) {
4196 error_report("Invalid SMP CPUs %d. The max CPUs "
4197 "supported by machine '%s' is %d", max_cpus,
4198 machine_class->name, machine_class->max_cpus);
4199 exit(1);
4203 * Get the default machine options from the machine if it is not already
4204 * specified either by the configuration file or by the command line.
4206 if (machine_class->default_machine_opts) {
4207 qemu_opts_set_defaults(qemu_find_opts("machine"),
4208 machine_class->default_machine_opts, 0);
4211 qemu_opts_foreach(qemu_find_opts("device"),
4212 default_driver_check, NULL, NULL);
4213 qemu_opts_foreach(qemu_find_opts("global"),
4214 default_driver_check, NULL, NULL);
4216 if (!vga_model && !default_vga) {
4217 vga_interface_type = VGA_DEVICE;
4219 if (!has_defaults || machine_class->no_serial) {
4220 default_serial = 0;
4222 if (!has_defaults || machine_class->no_parallel) {
4223 default_parallel = 0;
4225 if (!has_defaults || !machine_class->use_virtcon) {
4226 default_virtcon = 0;
4228 if (!has_defaults || !machine_class->use_sclp) {
4229 default_sclp = 0;
4231 if (!has_defaults || machine_class->no_floppy) {
4232 default_floppy = 0;
4234 if (!has_defaults || machine_class->no_cdrom) {
4235 default_cdrom = 0;
4237 if (!has_defaults || machine_class->no_sdcard) {
4238 default_sdcard = 0;
4240 if (!has_defaults) {
4241 default_monitor = 0;
4242 default_net = 0;
4243 default_vga = 0;
4246 if (is_daemonized()) {
4247 /* According to documentation and historically, -nographic redirects
4248 * serial port, parallel port and monitor to stdio, which does not work
4249 * with -daemonize. We can redirect these to null instead, but since
4250 * -nographic is legacy, let's just error out.
4251 * We disallow -nographic only if all other ports are not redirected
4252 * explicitly, to not break existing legacy setups which uses
4253 * -nographic _and_ redirects all ports explicitly - this is valid
4254 * usage, -nographic is just a no-op in this case.
4256 if (nographic
4257 && (default_parallel || default_serial
4258 || default_monitor || default_virtcon)) {
4259 error_report("-nographic cannot be used with -daemonize");
4260 exit(1);
4262 #ifdef CONFIG_CURSES
4263 if (dpy.type == DISPLAY_TYPE_CURSES) {
4264 error_report("curses display cannot be used with -daemonize");
4265 exit(1);
4267 #endif
4270 if (nographic) {
4271 if (default_parallel)
4272 add_device_config(DEV_PARALLEL, "null");
4273 if (default_serial && default_monitor) {
4274 add_device_config(DEV_SERIAL, "mon:stdio");
4275 } else if (default_virtcon && default_monitor) {
4276 add_device_config(DEV_VIRTCON, "mon:stdio");
4277 } else if (default_sclp && default_monitor) {
4278 add_device_config(DEV_SCLP, "mon:stdio");
4279 } else {
4280 if (default_serial)
4281 add_device_config(DEV_SERIAL, "stdio");
4282 if (default_virtcon)
4283 add_device_config(DEV_VIRTCON, "stdio");
4284 if (default_sclp) {
4285 add_device_config(DEV_SCLP, "stdio");
4287 if (default_monitor)
4288 monitor_parse("stdio", "readline", false);
4290 } else {
4291 if (default_serial)
4292 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4293 if (default_parallel)
4294 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4295 if (default_monitor)
4296 monitor_parse("vc:80Cx24C", "readline", false);
4297 if (default_virtcon)
4298 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4299 if (default_sclp) {
4300 add_device_config(DEV_SCLP, "vc:80Cx24C");
4304 #if defined(CONFIG_VNC)
4305 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4306 display_remote++;
4308 #endif
4309 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4310 if (!qemu_display_find_default(&dpy)) {
4311 dpy.type = DISPLAY_TYPE_NONE;
4312 #if defined(CONFIG_VNC)
4313 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4314 #endif
4317 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4318 dpy.type = DISPLAY_TYPE_NONE;
4321 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4322 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4323 "for SDL, ignoring option");
4325 if (dpy.has_window_close &&
4326 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4327 error_report("-no-quit is only valid for GTK and SDL, "
4328 "ignoring option");
4331 qemu_display_early_init(&dpy);
4332 qemu_console_early_init();
4334 if (dpy.has_gl && dpy.gl && display_opengl == 0) {
4335 #if defined(CONFIG_OPENGL)
4336 error_report("OpenGL is not supported by the display");
4337 #else
4338 error_report("OpenGL support is disabled");
4339 #endif
4340 exit(1);
4343 page_size_init();
4344 socket_init();
4346 if (qemu_opts_foreach(qemu_find_opts("object"),
4347 user_creatable_add_opts_foreach,
4348 object_create_initial, NULL)) {
4349 exit(1);
4352 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4353 chardev_init_func, NULL, NULL)) {
4354 exit(1);
4357 #ifdef CONFIG_VIRTFS
4358 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4359 fsdev_init_func, NULL, NULL)) {
4360 exit(1);
4362 #endif
4364 if (qemu_opts_foreach(qemu_find_opts("device"),
4365 device_help_func, NULL, NULL)) {
4366 exit(0);
4369 machine_opts = qemu_get_machine_opts();
4370 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4371 NULL)) {
4372 object_unref(OBJECT(current_machine));
4373 exit(1);
4376 configure_accelerator(current_machine);
4379 * Register all the global properties, including accel properties,
4380 * machine properties, and user-specified ones.
4382 register_global_properties(current_machine);
4385 * Migration object can only be created after global properties
4386 * are applied correctly.
4388 migration_object_init();
4390 if (qtest_chrdev) {
4391 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4394 machine_opts = qemu_get_machine_opts();
4395 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4396 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4397 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4398 bios_name = qemu_opt_get(machine_opts, "firmware");
4400 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4401 if (opts) {
4402 boot_order = qemu_opt_get(opts, "order");
4403 if (boot_order) {
4404 validate_bootdevices(boot_order, &error_fatal);
4407 boot_once = qemu_opt_get(opts, "once");
4408 if (boot_once) {
4409 validate_bootdevices(boot_once, &error_fatal);
4412 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4413 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4416 if (!boot_order) {
4417 boot_order = machine_class->default_boot_order;
4420 if (!kernel_cmdline) {
4421 kernel_cmdline = "";
4422 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4425 linux_boot = (kernel_filename != NULL);
4427 if (!linux_boot && *kernel_cmdline != '\0') {
4428 error_report("-append only allowed with -kernel option");
4429 exit(1);
4432 if (!linux_boot && initrd_filename != NULL) {
4433 error_report("-initrd only allowed with -kernel option");
4434 exit(1);
4437 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4438 /* fall back to the -kernel/-append */
4439 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4442 os_set_line_buffering();
4444 /* spice needs the timers to be initialized by this point */
4445 qemu_spice_init();
4447 cpu_ticks_init();
4448 if (icount_opts) {
4449 if (!tcg_enabled()) {
4450 error_report("-icount is not allowed with hardware virtualization");
4451 exit(1);
4453 configure_icount(icount_opts, &error_abort);
4454 qemu_opts_del(icount_opts);
4457 if (tcg_enabled()) {
4458 qemu_tcg_configure(accel_opts, &error_fatal);
4461 if (default_net) {
4462 QemuOptsList *net = qemu_find_opts("net");
4463 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4464 #ifdef CONFIG_SLIRP
4465 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4466 #endif
4469 colo_info_init();
4471 if (net_init_clients(&err) < 0) {
4472 error_report_err(err);
4473 exit(1);
4476 if (qemu_opts_foreach(qemu_find_opts("object"),
4477 user_creatable_add_opts_foreach,
4478 object_create_delayed, NULL)) {
4479 exit(1);
4482 if (tpm_init() < 0) {
4483 exit(1);
4486 /* init the bluetooth world */
4487 if (foreach_device_config(DEV_BT, bt_parse))
4488 exit(1);
4490 if (!xen_enabled()) {
4491 /* On 32-bit hosts, QEMU is limited by virtual address space */
4492 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4493 error_report("at most 2047 MB RAM can be simulated");
4494 exit(1);
4498 blk_mig_init();
4499 ram_mig_init();
4501 /* If the currently selected machine wishes to override the units-per-bus
4502 * property of its default HBA interface type, do so now. */
4503 if (machine_class->units_per_default_bus) {
4504 override_max_devs(machine_class->block_default_type,
4505 machine_class->units_per_default_bus);
4508 /* open the virtual block devices */
4509 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4510 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4512 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4513 loc_push_restore(&bdo->loc);
4514 qmp_blockdev_add(bdo->bdo, &error_fatal);
4515 loc_pop(&bdo->loc);
4516 qapi_free_BlockdevOptions(bdo->bdo);
4517 g_free(bdo);
4519 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4520 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4521 NULL, NULL);
4523 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4524 &machine_class->block_default_type, NULL)) {
4525 exit(1);
4528 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4529 CDROM_OPTS);
4530 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4531 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4533 if (qemu_opts_foreach(qemu_find_opts("mon"),
4534 mon_init_func, NULL, NULL)) {
4535 exit(1);
4538 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4539 exit(1);
4540 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4541 exit(1);
4542 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4543 exit(1);
4544 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4545 exit(1);
4547 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4548 exit(1);
4550 /* If no default VGA is requested, the default is "none". */
4551 if (default_vga) {
4552 if (machine_class->default_display) {
4553 vga_model = machine_class->default_display;
4554 } else if (vga_interface_available(VGA_CIRRUS)) {
4555 vga_model = "cirrus";
4556 } else if (vga_interface_available(VGA_STD)) {
4557 vga_model = "std";
4560 if (vga_model) {
4561 select_vgahw(vga_model);
4564 if (watchdog) {
4565 i = select_watchdog(watchdog);
4566 if (i > 0)
4567 exit (i == 1 ? 1 : 0);
4570 /* This checkpoint is required by replay to separate prior clock
4571 reading from the other reads, because timer polling functions query
4572 clock values from the log. */
4573 replay_checkpoint(CHECKPOINT_INIT);
4574 qdev_machine_init();
4576 current_machine->ram_size = ram_size;
4577 current_machine->maxram_size = maxram_size;
4578 current_machine->ram_slots = ram_slots;
4579 current_machine->boot_order = boot_order;
4580 current_machine->cpu_model = cpu_model;
4582 /* parse features once if machine provides default cpu_type */
4583 if (machine_class->default_cpu_type) {
4584 current_machine->cpu_type = machine_class->default_cpu_type;
4585 if (cpu_model) {
4586 current_machine->cpu_type =
4587 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
4590 parse_numa_opts(current_machine);
4592 machine_run_board_init(current_machine);
4594 realtime_init();
4596 soundhw_init();
4598 if (hax_enabled()) {
4599 hax_sync_vcpus();
4602 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4603 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4604 exit(1);
4607 /* init USB devices */
4608 if (machine_usb(current_machine)) {
4609 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4610 exit(1);
4613 /* Check if IGD GFX passthrough. */
4614 igd_gfx_passthru();
4616 /* init generic devices */
4617 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4618 if (qemu_opts_foreach(qemu_find_opts("device"),
4619 device_init_func, NULL, NULL)) {
4620 exit(1);
4623 cpu_synchronize_all_post_init();
4625 rom_reset_order_override();
4628 * Create frontends for -drive if=scsi leftovers.
4629 * Normally, frontends for -drive get created by machine
4630 * initialization for onboard SCSI HBAs. However, we create a few
4631 * more ever since SCSI qdevification, but this is pretty much an
4632 * implementation accident, and deprecated.
4634 scsi_legacy_handle_cmdline();
4636 /* Did we create any drives that we failed to create a device for? */
4637 drive_check_orphaned();
4639 /* Don't warn about the default network setup that you get if
4640 * no command line -net or -netdev options are specified. There
4641 * are two cases that we would otherwise complain about:
4642 * (1) board doesn't support a NIC but the implicit "-net nic"
4643 * requested one
4644 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4645 * sets up a nic that isn't connected to anything.
4647 if (!default_net) {
4648 net_check_clients();
4652 if (boot_once) {
4653 qemu_boot_set(boot_once, &error_fatal);
4654 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4657 /* init local displays */
4658 ds = init_displaystate();
4659 qemu_display_init(ds, &dpy);
4661 /* must be after terminal init, SDL library changes signal handlers */
4662 os_setup_signal_handling();
4664 /* init remote displays */
4665 #ifdef CONFIG_VNC
4666 qemu_opts_foreach(qemu_find_opts("vnc"),
4667 vnc_init_func, NULL, NULL);
4668 #endif
4670 if (using_spice) {
4671 qemu_spice_display_init();
4674 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4675 exit(1);
4678 qdev_machine_creation_done();
4680 /* TODO: once all bus devices are qdevified, this should be done
4681 * when bus is created by qdev.c */
4682 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4683 qemu_run_machine_init_done_notifiers();
4685 if (rom_check_and_register_reset() != 0) {
4686 error_report("rom check and register reset failed");
4687 exit(1);
4690 replay_start();
4692 /* This checkpoint is required by replay to separate prior clock
4693 reading from the other reads, because timer polling functions query
4694 clock values from the log. */
4695 replay_checkpoint(CHECKPOINT_RESET);
4696 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4697 register_global_state();
4698 if (replay_mode != REPLAY_MODE_NONE) {
4699 replay_vmstate_init();
4700 } else if (loadvm) {
4701 Error *local_err = NULL;
4702 if (load_snapshot(loadvm, &local_err) < 0) {
4703 error_report_err(local_err);
4704 autostart = 0;
4708 qdev_prop_check_globals();
4709 if (vmstate_dump_file) {
4710 /* dump and exit */
4711 dump_vmstate_json_to_file(vmstate_dump_file);
4712 return 0;
4715 if (incoming) {
4716 Error *local_err = NULL;
4717 qemu_start_incoming_migration(incoming, &local_err);
4718 if (local_err) {
4719 error_reportf_err(local_err, "-incoming %s: ", incoming);
4720 exit(1);
4722 } else if (autostart) {
4723 vm_start();
4726 os_setup_post();
4728 main_loop();
4729 replay_disable_events();
4731 /* The ordering of the following is delicate. Stop vcpus to prevent new
4732 * I/O requests being queued by the guest. Then stop IOThreads (this
4733 * includes a drain operation and completes all request processing). At
4734 * this point emulated devices are still associated with their IOThreads
4735 * (if any) but no longer have any work to do. Only then can we close
4736 * block devices safely because we know there is no more I/O coming.
4738 pause_all_vcpus();
4739 iothread_stop_all();
4740 bdrv_close_all();
4742 res_free();
4744 /* vhost-user must be cleaned up before chardevs. */
4745 tpm_cleanup();
4746 net_cleanup();
4747 audio_cleanup();
4748 monitor_cleanup();
4749 qemu_chr_cleanup();
4750 user_creatable_cleanup();
4751 migration_object_finalize();
4752 /* TODO: unref root container, check all devices are ok */
4754 return 0;