hw/arm: Restore local modifications
[qemu/ar7.git] / vl.c
blob3fef9f030b4494ee311ee23688b8b3c3671f8366
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 #define SDL_MAIN_HANDLED
40 #include <SDL.h>
41 #if SDL_MAJOR_VERSION == 1
42 int qemu_main(int argc, char **argv);
43 int main(int argc, char **argv)
45 return qemu_main(argc, argv);
47 #undef main
48 #define main qemu_main
49 #elif SDL_MAJOR_VERSION == 2
50 #undef main
51 #else
52 #error undefined SDL version
53 #endif
54 #endif
55 #endif /* CONFIG_SDL */
57 #ifdef CONFIG_COCOA
58 #undef main
59 #define main qemu_main
60 #endif /* CONFIG_COCOA */
63 #include "qemu/error-report.h"
64 #include "qemu/sockets.h"
65 #include "hw/hw.h"
66 #include "hw/boards.h"
67 #include "sysemu/accel.h"
68 #include "hw/usb.h"
69 #include "hw/isa/isa.h"
70 #include "hw/scsi/scsi.h"
71 #include "hw/display/vga.h"
72 #include "hw/bt.h"
73 #include "sysemu/watchdog.h"
74 #include "hw/smbios/smbios.h"
75 #include "hw/acpi/acpi.h"
76 #include "hw/xen/xen.h"
77 #include "hw/qdev.h"
78 #include "hw/loader.h"
79 #include "monitor/qdev.h"
80 #include "sysemu/bt.h"
81 #include "net/net.h"
82 #include "net/slirp.h"
83 #include "monitor/monitor.h"
84 #include "ui/console.h"
85 #include "ui/input.h"
86 #include "sysemu/sysemu.h"
87 #include "sysemu/numa.h"
88 #include "exec/gdbstub.h"
89 #include "qemu/timer.h"
90 #include "chardev/char.h"
91 #include "qemu/bitmap.h"
92 #include "qemu/log.h"
93 #include "sysemu/blockdev.h"
94 #include "hw/block/block.h"
95 #include "migration/misc.h"
96 #include "migration/snapshot.h"
97 #include "migration/global_state.h"
98 #include "sysemu/tpm.h"
99 #include "sysemu/dma.h"
100 #include "hw/audio/soundhw.h"
101 #include "audio/audio.h"
102 #include "sysemu/cpus.h"
103 #include "migration/colo.h"
104 #include "migration/postcopy-ram.h"
105 #include "sysemu/kvm.h"
106 #include "sysemu/hax.h"
107 #include "qapi/qobject-input-visitor.h"
108 #include "qemu/option.h"
109 #include "qemu/config-file.h"
110 #include "qemu-options.h"
111 #include "qemu/main-loop.h"
112 #ifdef CONFIG_VIRTFS
113 #include "fsdev/qemu-fsdev.h"
114 #endif
115 #include "sysemu/qtest.h"
117 #include "disas/disas.h"
120 #include "slirp/libslirp.h"
122 #include "trace-root.h"
123 #include "trace/control.h"
124 #include "qemu/queue.h"
125 #include "sysemu/arch_init.h"
127 #include "ui/qemu-spice.h"
128 #include "qapi/string-input-visitor.h"
129 #include "qapi/opts-visitor.h"
130 #include "qapi/clone-visitor.h"
131 #include "qom/object_interfaces.h"
132 #include "exec/semihost.h"
133 #include "crypto/init.h"
134 #include "sysemu/replay.h"
135 #include "qapi/qapi-events-run-state.h"
136 #include "qapi/qapi-visit-block-core.h"
137 #include "qapi/qapi-visit-ui.h"
138 #include "qapi/qapi-commands-block-core.h"
139 #include "qapi/qapi-commands-misc.h"
140 #include "qapi/qapi-commands-run-state.h"
141 #include "qapi/qmp/qerror.h"
142 #include "sysemu/iothread.h"
144 #define MAX_VIRTIO_CONSOLES 1
146 static const char *data_dir[16];
147 static int data_dir_idx;
148 const char *bios_name = NULL;
149 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
150 int display_opengl;
151 const char* keyboard_layout = NULL;
152 ram_addr_t ram_size;
153 const char *mem_path = NULL;
154 int mem_prealloc = 0; /* force preallocation of physical target memory */
155 bool enable_mlock = false;
156 int nb_nics;
157 NICInfo nd_table[MAX_NICS];
158 int autostart;
159 static int rtc_utc = 1;
160 static int rtc_date_offset = -1; /* -1 means no change */
161 QEMUClockType rtc_clock;
162 int vga_interface_type = VGA_NONE;
163 static DisplayOptions dpy;
164 int no_frame;
165 static int num_serial_hds = 0;
166 static Chardev **serial_hds = NULL;
167 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
168 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
169 int win2k_install_hack = 0;
170 int singlestep = 0;
171 int smp_cpus;
172 unsigned int max_cpus;
173 int smp_cores = 1;
174 int smp_threads = 1;
175 int acpi_enabled = 1;
176 int no_hpet = 0;
177 int fd_bootchk = 1;
178 static int no_reboot;
179 int no_shutdown = 0;
180 int cursor_hide = 1;
181 int graphic_rotate = 0;
182 const char *watchdog;
183 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
184 int nb_option_roms;
185 int old_param = 0;
186 const char *qemu_name;
187 int alt_grab = 0;
188 int ctrl_grab = 0;
189 unsigned int nb_prom_envs = 0;
190 const char *prom_envs[MAX_PROM_ENVS];
191 int boot_menu;
192 bool boot_strict;
193 uint8_t *boot_splash_filedata;
194 size_t boot_splash_filedata_size;
195 uint8_t qemu_extra_params_fw[2];
197 int icount_align_option;
199 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
200 * little-endian "wire format" described in the SMBIOS 2.6 specification.
202 QemuUUID qemu_uuid;
203 bool qemu_uuid_set;
205 /* Trace unassigned memory or i/o accesses. */
206 bool trace_unassigned;
208 static NotifierList exit_notifiers =
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
211 static NotifierList machine_init_done_notifiers =
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
214 bool xen_allowed;
215 uint32_t xen_domid;
216 enum xen_mode xen_mode = XEN_EMULATE;
217 bool xen_domid_restrict;
219 static int has_defaults = 1;
220 static int default_serial = 1;
221 static int default_parallel = 1;
222 static int default_virtcon = 1;
223 static int default_monitor = 1;
224 static int default_floppy = 1;
225 static int default_cdrom = 1;
226 static int default_sdcard = 1;
227 static int default_vga = 1;
228 static int default_net = 1;
230 static struct {
231 const char *driver;
232 int *flag;
233 } default_list[] = {
234 { .driver = "isa-serial", .flag = &default_serial },
235 { .driver = "isa-parallel", .flag = &default_parallel },
236 { .driver = "isa-fdc", .flag = &default_floppy },
237 { .driver = "floppy", .flag = &default_floppy },
238 { .driver = "ide-cd", .flag = &default_cdrom },
239 { .driver = "ide-hd", .flag = &default_cdrom },
240 { .driver = "ide-drive", .flag = &default_cdrom },
241 { .driver = "scsi-cd", .flag = &default_cdrom },
242 { .driver = "scsi-hd", .flag = &default_cdrom },
243 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
244 { .driver = "virtio-serial", .flag = &default_virtcon },
245 { .driver = "VGA", .flag = &default_vga },
246 { .driver = "isa-vga", .flag = &default_vga },
247 { .driver = "cirrus-vga", .flag = &default_vga },
248 { .driver = "isa-cirrus-vga", .flag = &default_vga },
249 { .driver = "vmware-svga", .flag = &default_vga },
250 { .driver = "qxl-vga", .flag = &default_vga },
251 { .driver = "virtio-vga", .flag = &default_vga },
254 static QemuOptsList qemu_rtc_opts = {
255 .name = "rtc",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
257 .desc = {
259 .name = "base",
260 .type = QEMU_OPT_STRING,
262 .name = "clock",
263 .type = QEMU_OPT_STRING,
265 .name = "driftfix",
266 .type = QEMU_OPT_STRING,
268 { /* end of list */ }
272 static QemuOptsList qemu_sandbox_opts = {
273 .name = "sandbox",
274 .implied_opt_name = "enable",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
276 .desc = {
278 .name = "enable",
279 .type = QEMU_OPT_BOOL,
282 .name = "obsolete",
283 .type = QEMU_OPT_STRING,
286 .name = "elevateprivileges",
287 .type = QEMU_OPT_STRING,
290 .name = "spawn",
291 .type = QEMU_OPT_STRING,
294 .name = "resourcecontrol",
295 .type = QEMU_OPT_STRING,
297 { /* end of list */ }
301 static QemuOptsList qemu_option_rom_opts = {
302 .name = "option-rom",
303 .implied_opt_name = "romfile",
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
305 .desc = {
307 .name = "bootindex",
308 .type = QEMU_OPT_NUMBER,
309 }, {
310 .name = "romfile",
311 .type = QEMU_OPT_STRING,
313 { /* end of list */ }
317 static QemuOptsList qemu_machine_opts = {
318 .name = "machine",
319 .implied_opt_name = "type",
320 .merge_lists = true,
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
322 .desc = {
324 * no elements => accept any
325 * sanity checking will happen later
326 * when setting machine properties
332 static QemuOptsList qemu_accel_opts = {
333 .name = "accel",
334 .implied_opt_name = "accel",
335 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
336 .merge_lists = true,
337 .desc = {
339 .name = "accel",
340 .type = QEMU_OPT_STRING,
341 .help = "Select the type of accelerator",
344 .name = "thread",
345 .type = QEMU_OPT_STRING,
346 .help = "Enable/disable multi-threaded TCG",
348 { /* end of list */ }
352 static QemuOptsList qemu_boot_opts = {
353 .name = "boot-opts",
354 .implied_opt_name = "order",
355 .merge_lists = true,
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
357 .desc = {
359 .name = "order",
360 .type = QEMU_OPT_STRING,
361 }, {
362 .name = "once",
363 .type = QEMU_OPT_STRING,
364 }, {
365 .name = "menu",
366 .type = QEMU_OPT_BOOL,
367 }, {
368 .name = "splash",
369 .type = QEMU_OPT_STRING,
370 }, {
371 .name = "splash-time",
372 .type = QEMU_OPT_STRING,
373 }, {
374 .name = "reboot-timeout",
375 .type = QEMU_OPT_STRING,
376 }, {
377 .name = "strict",
378 .type = QEMU_OPT_BOOL,
380 { /*End of list */ }
384 static QemuOptsList qemu_add_fd_opts = {
385 .name = "add-fd",
386 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
387 .desc = {
389 .name = "fd",
390 .type = QEMU_OPT_NUMBER,
391 .help = "file descriptor of which a duplicate is added to fd set",
393 .name = "set",
394 .type = QEMU_OPT_NUMBER,
395 .help = "ID of the fd set to add fd to",
397 .name = "opaque",
398 .type = QEMU_OPT_STRING,
399 .help = "free-form string used to describe fd",
401 { /* end of list */ }
405 static QemuOptsList qemu_object_opts = {
406 .name = "object",
407 .implied_opt_name = "qom-type",
408 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
409 .desc = {
414 static QemuOptsList qemu_tpmdev_opts = {
415 .name = "tpmdev",
416 .implied_opt_name = "type",
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
418 .desc = {
419 /* options are defined in the TPM backends */
420 { /* end of list */ }
424 static QemuOptsList qemu_realtime_opts = {
425 .name = "realtime",
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
427 .desc = {
429 .name = "mlock",
430 .type = QEMU_OPT_BOOL,
432 { /* end of list */ }
436 static QemuOptsList qemu_msg_opts = {
437 .name = "msg",
438 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
439 .desc = {
441 .name = "timestamp",
442 .type = QEMU_OPT_BOOL,
444 { /* end of list */ }
448 static QemuOptsList qemu_name_opts = {
449 .name = "name",
450 .implied_opt_name = "guest",
451 .merge_lists = true,
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
453 .desc = {
455 .name = "guest",
456 .type = QEMU_OPT_STRING,
457 .help = "Sets the name of the guest.\n"
458 "This name will be displayed in the SDL window caption.\n"
459 "The name will also be used for the VNC server",
460 }, {
461 .name = "process",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the QEMU process, as shown in top etc",
464 }, {
465 .name = "debug-threads",
466 .type = QEMU_OPT_BOOL,
467 .help = "When enabled, name the individual threads; defaults off.\n"
468 "NOTE: The thread names are for debugging and not a\n"
469 "stable API.",
471 { /* End of list */ }
475 static QemuOptsList qemu_mem_opts = {
476 .name = "memory",
477 .implied_opt_name = "size",
478 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
479 .merge_lists = true,
480 .desc = {
482 .name = "size",
483 .type = QEMU_OPT_SIZE,
486 .name = "slots",
487 .type = QEMU_OPT_NUMBER,
490 .name = "maxmem",
491 .type = QEMU_OPT_SIZE,
493 { /* end of list */ }
497 static QemuOptsList qemu_icount_opts = {
498 .name = "icount",
499 .implied_opt_name = "shift",
500 .merge_lists = true,
501 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
502 .desc = {
504 .name = "shift",
505 .type = QEMU_OPT_STRING,
506 }, {
507 .name = "align",
508 .type = QEMU_OPT_BOOL,
509 }, {
510 .name = "sleep",
511 .type = QEMU_OPT_BOOL,
512 }, {
513 .name = "rr",
514 .type = QEMU_OPT_STRING,
515 }, {
516 .name = "rrfile",
517 .type = QEMU_OPT_STRING,
518 }, {
519 .name = "rrsnapshot",
520 .type = QEMU_OPT_STRING,
522 { /* end of list */ }
526 static QemuOptsList qemu_semihosting_config_opts = {
527 .name = "semihosting-config",
528 .implied_opt_name = "enable",
529 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
530 .desc = {
532 .name = "enable",
533 .type = QEMU_OPT_BOOL,
534 }, {
535 .name = "target",
536 .type = QEMU_OPT_STRING,
537 }, {
538 .name = "arg",
539 .type = QEMU_OPT_STRING,
541 { /* end of list */ }
545 static QemuOptsList qemu_fw_cfg_opts = {
546 .name = "fw_cfg",
547 .implied_opt_name = "name",
548 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
549 .desc = {
551 .name = "name",
552 .type = QEMU_OPT_STRING,
553 .help = "Sets the fw_cfg name of the blob to be inserted",
554 }, {
555 .name = "file",
556 .type = QEMU_OPT_STRING,
557 .help = "Sets the name of the file from which\n"
558 "the fw_cfg blob will be loaded",
559 }, {
560 .name = "string",
561 .type = QEMU_OPT_STRING,
562 .help = "Sets content of the blob to be inserted from a string",
564 { /* end of list */ }
569 * Get machine options
571 * Returns: machine options (never null).
573 QemuOpts *qemu_get_machine_opts(void)
575 return qemu_find_opts_singleton("machine");
578 const char *qemu_get_vm_name(void)
580 return qemu_name;
583 static void res_free(void)
585 g_free(boot_splash_filedata);
586 boot_splash_filedata = NULL;
589 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
591 const char *driver = qemu_opt_get(opts, "driver");
592 int i;
594 if (!driver)
595 return 0;
596 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
597 if (strcmp(default_list[i].driver, driver) != 0)
598 continue;
599 *(default_list[i].flag) = 0;
601 return 0;
604 /***********************************************************/
605 /* QEMU state */
607 static RunState current_run_state = RUN_STATE_PRELAUNCH;
609 /* We use RUN_STATE__MAX but any invalid value will do */
610 static RunState vmstop_requested = RUN_STATE__MAX;
611 static QemuMutex vmstop_lock;
613 typedef struct {
614 RunState from;
615 RunState to;
616 } RunStateTransition;
618 static const RunStateTransition runstate_transitions_def[] = {
619 /* from -> to */
620 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
621 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
625 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
626 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
627 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
629 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
630 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
631 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
632 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
635 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
637 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
638 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
642 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
646 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
648 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
649 { RUN_STATE_PAUSED, RUN_STATE_COLO},
651 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
652 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
655 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
656 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
659 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
660 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
665 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
666 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_COLO, RUN_STATE_RUNNING },
670 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
671 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
672 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
673 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
674 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
676 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
677 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
678 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
679 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
680 { RUN_STATE_RUNNING, RUN_STATE_COLO},
682 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
684 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
685 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
686 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
688 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
689 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
690 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
691 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
692 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
693 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
695 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
696 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
697 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
698 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
700 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
701 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
702 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
704 { RUN_STATE__MAX, RUN_STATE__MAX },
707 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
709 bool runstate_check(RunState state)
711 return current_run_state == state;
714 bool runstate_store(char *str, size_t size)
716 const char *state = RunState_str(current_run_state);
717 size_t len = strlen(state) + 1;
719 if (len > size) {
720 return false;
722 memcpy(str, state, len);
723 return true;
726 static void runstate_init(void)
728 const RunStateTransition *p;
730 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
731 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
732 runstate_valid_transitions[p->from][p->to] = true;
735 qemu_mutex_init(&vmstop_lock);
738 /* This function will abort() on invalid state transitions */
739 void runstate_set(RunState new_state)
741 assert(new_state < RUN_STATE__MAX);
743 if (current_run_state == new_state) {
744 return;
747 if (!runstate_valid_transitions[current_run_state][new_state]) {
748 error_report("invalid runstate transition: '%s' -> '%s'",
749 RunState_str(current_run_state),
750 RunState_str(new_state));
751 abort();
753 trace_runstate_set(new_state);
754 current_run_state = new_state;
757 int runstate_is_running(void)
759 return runstate_check(RUN_STATE_RUNNING);
762 bool runstate_needs_reset(void)
764 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
765 runstate_check(RUN_STATE_SHUTDOWN);
768 StatusInfo *qmp_query_status(Error **errp)
770 StatusInfo *info = g_malloc0(sizeof(*info));
772 info->running = runstate_is_running();
773 info->singlestep = singlestep;
774 info->status = current_run_state;
776 return info;
779 bool qemu_vmstop_requested(RunState *r)
781 qemu_mutex_lock(&vmstop_lock);
782 *r = vmstop_requested;
783 vmstop_requested = RUN_STATE__MAX;
784 qemu_mutex_unlock(&vmstop_lock);
785 return *r < RUN_STATE__MAX;
788 void qemu_system_vmstop_request_prepare(void)
790 qemu_mutex_lock(&vmstop_lock);
793 void qemu_system_vmstop_request(RunState state)
795 vmstop_requested = state;
796 qemu_mutex_unlock(&vmstop_lock);
797 qemu_notify_event();
800 /***********************************************************/
801 /* real time host monotonic timer */
803 static time_t qemu_time(void)
805 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
808 /***********************************************************/
809 /* host time/date access */
810 void qemu_get_timedate(struct tm *tm, int offset)
812 time_t ti = qemu_time();
814 ti += offset;
815 if (rtc_date_offset == -1) {
816 if (rtc_utc)
817 gmtime_r(&ti, tm);
818 else
819 localtime_r(&ti, tm);
820 } else {
821 ti -= rtc_date_offset;
822 gmtime_r(&ti, tm);
826 int qemu_timedate_diff(struct tm *tm)
828 time_t seconds;
830 if (rtc_date_offset == -1)
831 if (rtc_utc)
832 seconds = mktimegm(tm);
833 else {
834 struct tm tmp = *tm;
835 tmp.tm_isdst = -1; /* use timezone to figure it out */
836 seconds = mktime(&tmp);
838 else
839 seconds = mktimegm(tm) + rtc_date_offset;
841 return seconds - qemu_time();
844 static void configure_rtc_date_offset(const char *startdate, int legacy)
846 time_t rtc_start_date;
847 struct tm tm;
849 if (!strcmp(startdate, "now") && legacy) {
850 rtc_date_offset = -1;
851 } else {
852 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
853 &tm.tm_year,
854 &tm.tm_mon,
855 &tm.tm_mday,
856 &tm.tm_hour,
857 &tm.tm_min,
858 &tm.tm_sec) == 6) {
859 /* OK */
860 } else if (sscanf(startdate, "%d-%d-%d",
861 &tm.tm_year,
862 &tm.tm_mon,
863 &tm.tm_mday) == 3) {
864 tm.tm_hour = 0;
865 tm.tm_min = 0;
866 tm.tm_sec = 0;
867 } else {
868 goto date_fail;
870 tm.tm_year -= 1900;
871 tm.tm_mon--;
872 rtc_start_date = mktimegm(&tm);
873 if (rtc_start_date == -1) {
874 date_fail:
875 error_report("invalid date format");
876 error_printf("valid formats: "
877 "'2006-06-17T16:01:21' or '2006-06-17'\n");
878 exit(1);
880 rtc_date_offset = qemu_time() - rtc_start_date;
884 static void configure_rtc(QemuOpts *opts)
886 const char *value;
888 value = qemu_opt_get(opts, "base");
889 if (value) {
890 if (!strcmp(value, "utc")) {
891 rtc_utc = 1;
892 } else if (!strcmp(value, "localtime")) {
893 Error *blocker = NULL;
894 rtc_utc = 0;
895 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
896 "-rtc base=localtime");
897 replay_add_blocker(blocker);
898 } else {
899 configure_rtc_date_offset(value, 0);
902 value = qemu_opt_get(opts, "clock");
903 if (value) {
904 if (!strcmp(value, "host")) {
905 rtc_clock = QEMU_CLOCK_HOST;
906 } else if (!strcmp(value, "rt")) {
907 rtc_clock = QEMU_CLOCK_REALTIME;
908 } else if (!strcmp(value, "vm")) {
909 rtc_clock = QEMU_CLOCK_VIRTUAL;
910 } else {
911 error_report("invalid option value '%s'", value);
912 exit(1);
915 value = qemu_opt_get(opts, "driftfix");
916 if (value) {
917 if (!strcmp(value, "slew")) {
918 static GlobalProperty slew_lost_ticks = {
919 .driver = "mc146818rtc",
920 .property = "lost_tick_policy",
921 .value = "slew",
924 qdev_prop_register_global(&slew_lost_ticks);
925 } else if (!strcmp(value, "none")) {
926 /* discard is default */
927 } else {
928 error_report("invalid option value '%s'", value);
929 exit(1);
934 /***********************************************************/
935 /* Bluetooth support */
936 static int nb_hcis;
937 static int cur_hci;
938 static struct HCIInfo *hci_table[MAX_NICS];
940 struct HCIInfo *qemu_next_hci(void)
942 if (cur_hci == nb_hcis)
943 return &null_hci;
945 return hci_table[cur_hci++];
948 static int bt_hci_parse(const char *str)
950 struct HCIInfo *hci;
951 bdaddr_t bdaddr;
953 if (nb_hcis >= MAX_NICS) {
954 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
955 return -1;
958 hci = hci_init(str);
959 if (!hci)
960 return -1;
962 bdaddr.b[0] = 0x52;
963 bdaddr.b[1] = 0x54;
964 bdaddr.b[2] = 0x00;
965 bdaddr.b[3] = 0x12;
966 bdaddr.b[4] = 0x34;
967 bdaddr.b[5] = 0x56 + nb_hcis;
968 hci->bdaddr_set(hci, bdaddr.b);
970 hci_table[nb_hcis++] = hci;
972 return 0;
975 static void bt_vhci_add(int vlan_id)
977 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
979 if (!vlan->slave)
980 warn_report("adding a VHCI to an empty scatternet %i",
981 vlan_id);
983 bt_vhci_init(bt_new_hci(vlan));
986 static struct bt_device_s *bt_device_add(const char *opt)
988 struct bt_scatternet_s *vlan;
989 int vlan_id = 0;
990 char *endp = strstr(opt, ",vlan=");
991 int len = (endp ? endp - opt : strlen(opt)) + 1;
992 char devname[10];
994 pstrcpy(devname, MIN(sizeof(devname), len), opt);
996 if (endp) {
997 vlan_id = strtol(endp + 6, &endp, 0);
998 if (*endp) {
999 error_report("unrecognised bluetooth vlan Id");
1000 return 0;
1004 vlan = qemu_find_bt_vlan(vlan_id);
1006 if (!vlan->slave)
1007 warn_report("adding a slave device to an empty scatternet %i",
1008 vlan_id);
1010 if (!strcmp(devname, "keyboard"))
1011 return bt_keyboard_init(vlan);
1013 error_report("unsupported bluetooth device '%s'", devname);
1014 return 0;
1017 static int bt_parse(const char *opt)
1019 const char *endp, *p;
1020 int vlan;
1022 if (strstart(opt, "hci", &endp)) {
1023 if (!*endp || *endp == ',') {
1024 if (*endp)
1025 if (!strstart(endp, ",vlan=", 0))
1026 opt = endp + 1;
1028 return bt_hci_parse(opt);
1030 } else if (strstart(opt, "vhci", &endp)) {
1031 if (!*endp || *endp == ',') {
1032 if (*endp) {
1033 if (strstart(endp, ",vlan=", &p)) {
1034 vlan = strtol(p, (char **) &endp, 0);
1035 if (*endp) {
1036 error_report("bad scatternet '%s'", p);
1037 return 1;
1039 } else {
1040 error_report("bad parameter '%s'", endp + 1);
1041 return 1;
1043 } else
1044 vlan = 0;
1046 bt_vhci_add(vlan);
1047 return 0;
1049 } else if (strstart(opt, "device:", &endp))
1050 return !bt_device_add(endp);
1052 error_report("bad bluetooth parameter '%s'", opt);
1053 return 1;
1056 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1058 if (qemu_opt_get_bool(opts, "enable", false)) {
1059 #ifdef CONFIG_SECCOMP
1060 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1061 | QEMU_SECCOMP_SET_OBSOLETE;
1062 const char *value = NULL;
1064 value = qemu_opt_get(opts, "obsolete");
1065 if (value) {
1066 if (g_str_equal(value, "allow")) {
1067 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1068 } else if (g_str_equal(value, "deny")) {
1069 /* this is the default option, this if is here
1070 * to provide a little bit of consistency for
1071 * the command line */
1072 } else {
1073 error_report("invalid argument for obsolete");
1074 return -1;
1078 value = qemu_opt_get(opts, "elevateprivileges");
1079 if (value) {
1080 if (g_str_equal(value, "deny")) {
1081 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1082 } else if (g_str_equal(value, "children")) {
1083 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1085 /* calling prctl directly because we're
1086 * not sure if host has CAP_SYS_ADMIN set*/
1087 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1088 error_report("failed to set no_new_privs "
1089 "aborting");
1090 return -1;
1092 } else if (g_str_equal(value, "allow")) {
1093 /* default value */
1094 } else {
1095 error_report("invalid argument for elevateprivileges");
1096 return -1;
1100 value = qemu_opt_get(opts, "spawn");
1101 if (value) {
1102 if (g_str_equal(value, "deny")) {
1103 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1104 } else if (g_str_equal(value, "allow")) {
1105 /* default value */
1106 } else {
1107 error_report("invalid argument for spawn");
1108 return -1;
1112 value = qemu_opt_get(opts, "resourcecontrol");
1113 if (value) {
1114 if (g_str_equal(value, "deny")) {
1115 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1116 } else if (g_str_equal(value, "allow")) {
1117 /* default value */
1118 } else {
1119 error_report("invalid argument for resourcecontrol");
1120 return -1;
1124 if (seccomp_start(seccomp_opts) < 0) {
1125 error_report("failed to install seccomp syscall filter "
1126 "in the kernel");
1127 return -1;
1129 #else
1130 error_report("seccomp support is disabled");
1131 return -1;
1132 #endif
1135 return 0;
1138 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1140 const char *proc_name;
1142 if (qemu_opt_get(opts, "debug-threads")) {
1143 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1145 qemu_name = qemu_opt_get(opts, "guest");
1147 proc_name = qemu_opt_get(opts, "process");
1148 if (proc_name) {
1149 os_set_proc_name(proc_name);
1152 return 0;
1155 bool defaults_enabled(void)
1157 return has_defaults;
1160 #ifndef _WIN32
1161 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1163 int fd, dupfd, flags;
1164 int64_t fdset_id;
1165 const char *fd_opaque = NULL;
1166 AddfdInfo *fdinfo;
1168 fd = qemu_opt_get_number(opts, "fd", -1);
1169 fdset_id = qemu_opt_get_number(opts, "set", -1);
1170 fd_opaque = qemu_opt_get(opts, "opaque");
1172 if (fd < 0) {
1173 error_report("fd option is required and must be non-negative");
1174 return -1;
1177 if (fd <= STDERR_FILENO) {
1178 error_report("fd cannot be a standard I/O stream");
1179 return -1;
1183 * All fds inherited across exec() necessarily have FD_CLOEXEC
1184 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1186 flags = fcntl(fd, F_GETFD);
1187 if (flags == -1 || (flags & FD_CLOEXEC)) {
1188 error_report("fd is not valid or already in use");
1189 return -1;
1192 if (fdset_id < 0) {
1193 error_report("set option is required and must be non-negative");
1194 return -1;
1197 #ifdef F_DUPFD_CLOEXEC
1198 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1199 #else
1200 dupfd = dup(fd);
1201 if (dupfd != -1) {
1202 qemu_set_cloexec(dupfd);
1204 #endif
1205 if (dupfd == -1) {
1206 error_report("error duplicating fd: %s", strerror(errno));
1207 return -1;
1210 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1211 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1212 &error_abort);
1213 g_free(fdinfo);
1215 return 0;
1218 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1220 int fd;
1222 fd = qemu_opt_get_number(opts, "fd", -1);
1223 close(fd);
1225 return 0;
1227 #endif
1229 /***********************************************************/
1230 /* QEMU Block devices */
1232 #define HD_OPTS "media=disk"
1233 #define CDROM_OPTS "media=cdrom"
1234 #define FD_OPTS ""
1235 #define PFLASH_OPTS ""
1236 #define MTD_OPTS ""
1237 #define SD_OPTS ""
1239 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1241 BlockInterfaceType *block_default_type = opaque;
1243 return drive_new(opts, *block_default_type) == NULL;
1246 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1248 if (qemu_opt_get(opts, "snapshot") == NULL) {
1249 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1251 return 0;
1254 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1255 int index, const char *optstr)
1257 QemuOpts *opts;
1258 DriveInfo *dinfo;
1260 if (!enable || drive_get_by_index(type, index)) {
1261 return;
1264 opts = drive_add(type, index, NULL, optstr);
1265 if (snapshot) {
1266 drive_enable_snapshot(NULL, opts, NULL);
1269 dinfo = drive_new(opts, type);
1270 if (!dinfo) {
1271 exit(1);
1273 dinfo->is_default = true;
1277 static QemuOptsList qemu_smp_opts = {
1278 .name = "smp-opts",
1279 .implied_opt_name = "cpus",
1280 .merge_lists = true,
1281 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1282 .desc = {
1284 .name = "cpus",
1285 .type = QEMU_OPT_NUMBER,
1286 }, {
1287 .name = "sockets",
1288 .type = QEMU_OPT_NUMBER,
1289 }, {
1290 .name = "cores",
1291 .type = QEMU_OPT_NUMBER,
1292 }, {
1293 .name = "threads",
1294 .type = QEMU_OPT_NUMBER,
1295 }, {
1296 .name = "maxcpus",
1297 .type = QEMU_OPT_NUMBER,
1299 { /*End of list */ }
1303 static void smp_parse(QemuOpts *opts)
1305 if (opts) {
1306 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1307 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1308 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1309 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1311 /* compute missing values, prefer sockets over cores over threads */
1312 if (cpus == 0 || sockets == 0) {
1313 sockets = sockets > 0 ? sockets : 1;
1314 cores = cores > 0 ? cores : 1;
1315 threads = threads > 0 ? threads : 1;
1316 if (cpus == 0) {
1317 cpus = cores * threads * sockets;
1319 } else if (cores == 0) {
1320 threads = threads > 0 ? threads : 1;
1321 cores = cpus / (sockets * threads);
1322 cores = cores > 0 ? cores : 1;
1323 } else if (threads == 0) {
1324 threads = cpus / (cores * sockets);
1325 threads = threads > 0 ? threads : 1;
1326 } else if (sockets * cores * threads < cpus) {
1327 error_report("cpu topology: "
1328 "sockets (%u) * cores (%u) * threads (%u) < "
1329 "smp_cpus (%u)",
1330 sockets, cores, threads, cpus);
1331 exit(1);
1334 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1336 if (max_cpus < cpus) {
1337 error_report("maxcpus must be equal to or greater than smp");
1338 exit(1);
1341 if (sockets * cores * threads > max_cpus) {
1342 error_report("cpu topology: "
1343 "sockets (%u) * cores (%u) * threads (%u) > "
1344 "maxcpus (%u)",
1345 sockets, cores, threads, max_cpus);
1346 exit(1);
1349 smp_cpus = cpus;
1350 smp_cores = cores;
1351 smp_threads = threads;
1354 if (smp_cpus > 1) {
1355 Error *blocker = NULL;
1356 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1357 replay_add_blocker(blocker);
1361 static void realtime_init(void)
1363 if (enable_mlock) {
1364 if (os_mlock() < 0) {
1365 error_report("locking memory failed");
1366 exit(1);
1372 static void configure_msg(QemuOpts *opts)
1374 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1377 /***********************************************************/
1378 /* Semihosting */
1380 typedef struct SemihostingConfig {
1381 bool enabled;
1382 SemihostingTarget target;
1383 const char **argv;
1384 int argc;
1385 const char *cmdline; /* concatenated argv */
1386 } SemihostingConfig;
1388 static SemihostingConfig semihosting;
1390 bool semihosting_enabled(void)
1392 return semihosting.enabled;
1395 SemihostingTarget semihosting_get_target(void)
1397 return semihosting.target;
1400 const char *semihosting_get_arg(int i)
1402 if (i >= semihosting.argc) {
1403 return NULL;
1405 return semihosting.argv[i];
1408 int semihosting_get_argc(void)
1410 return semihosting.argc;
1413 const char *semihosting_get_cmdline(void)
1415 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1416 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1418 return semihosting.cmdline;
1421 static int add_semihosting_arg(void *opaque,
1422 const char *name, const char *val,
1423 Error **errp)
1425 SemihostingConfig *s = opaque;
1426 if (strcmp(name, "arg") == 0) {
1427 s->argc++;
1428 /* one extra element as g_strjoinv() expects NULL-terminated array */
1429 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1430 s->argv[s->argc - 1] = val;
1431 s->argv[s->argc] = NULL;
1433 return 0;
1436 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1437 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1439 char *cmd_token;
1441 /* argv[0] */
1442 add_semihosting_arg(&semihosting, "arg", file, NULL);
1444 /* split -append and initialize argv[1..n] */
1445 cmd_token = strtok(g_strdup(cmd), " ");
1446 while (cmd_token) {
1447 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1448 cmd_token = strtok(NULL, " ");
1452 /* Now we still need this for compatibility with XEN. */
1453 bool has_igd_gfx_passthru;
1454 static void igd_gfx_passthru(void)
1456 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1459 /***********************************************************/
1460 /* USB devices */
1462 static int usb_device_add(const char *devname)
1464 USBDevice *dev = NULL;
1466 if (!machine_usb(current_machine)) {
1467 return -1;
1470 dev = usbdevice_create(devname);
1471 if (!dev)
1472 return -1;
1474 return 0;
1477 static int usb_parse(const char *cmdline)
1479 int r;
1480 r = usb_device_add(cmdline);
1481 if (r < 0) {
1482 error_report("could not add USB device '%s'", cmdline);
1484 return r;
1487 /***********************************************************/
1488 /* machine registration */
1490 MachineState *current_machine;
1492 static MachineClass *find_machine(const char *name)
1494 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1495 MachineClass *mc = NULL;
1497 for (el = machines; el; el = el->next) {
1498 MachineClass *temp = el->data;
1500 if (!strcmp(temp->name, name)) {
1501 mc = temp;
1502 break;
1504 if (temp->alias &&
1505 !strcmp(temp->alias, name)) {
1506 mc = temp;
1507 break;
1511 g_slist_free(machines);
1512 return mc;
1515 MachineClass *find_default_machine(void)
1517 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1518 MachineClass *mc = NULL;
1520 for (el = machines; el; el = el->next) {
1521 MachineClass *temp = el->data;
1523 if (temp->is_default) {
1524 mc = temp;
1525 break;
1529 g_slist_free(machines);
1530 return mc;
1533 MachineInfoList *qmp_query_machines(Error **errp)
1535 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1536 MachineInfoList *mach_list = NULL;
1538 for (el = machines; el; el = el->next) {
1539 MachineClass *mc = el->data;
1540 MachineInfoList *entry;
1541 MachineInfo *info;
1543 info = g_malloc0(sizeof(*info));
1544 if (mc->is_default) {
1545 info->has_is_default = true;
1546 info->is_default = true;
1549 if (mc->alias) {
1550 info->has_alias = true;
1551 info->alias = g_strdup(mc->alias);
1554 info->name = g_strdup(mc->name);
1555 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1556 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1558 entry = g_malloc0(sizeof(*entry));
1559 entry->value = info;
1560 entry->next = mach_list;
1561 mach_list = entry;
1564 g_slist_free(machines);
1565 return mach_list;
1568 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1570 ObjectProperty *prop;
1571 ObjectPropertyIterator iter;
1573 if (!qemu_opt_has_help_opt(opts)) {
1574 return 0;
1577 object_property_iter_init(&iter, OBJECT(machine));
1578 while ((prop = object_property_iter_next(&iter))) {
1579 if (!prop->set) {
1580 continue;
1583 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1584 prop->name, prop->type);
1585 if (prop->description) {
1586 error_printf(" (%s)\n", prop->description);
1587 } else {
1588 error_printf("\n");
1592 return 1;
1595 /***********************************************************/
1596 /* main execution loop */
1598 struct vm_change_state_entry {
1599 VMChangeStateHandler *cb;
1600 void *opaque;
1601 QLIST_ENTRY (vm_change_state_entry) entries;
1604 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1606 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1607 void *opaque)
1609 VMChangeStateEntry *e;
1611 e = g_malloc0(sizeof (*e));
1613 e->cb = cb;
1614 e->opaque = opaque;
1615 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1616 return e;
1619 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1621 QLIST_REMOVE (e, entries);
1622 g_free (e);
1625 void vm_state_notify(int running, RunState state)
1627 VMChangeStateEntry *e, *next;
1629 trace_vm_state_notify(running, state);
1631 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1632 e->cb(e->opaque, running, state);
1636 static ShutdownCause reset_requested;
1637 static ShutdownCause shutdown_requested;
1638 static int shutdown_signal;
1639 static pid_t shutdown_pid;
1640 static int powerdown_requested;
1641 static int debug_requested;
1642 static int suspend_requested;
1643 static WakeupReason wakeup_reason;
1644 static NotifierList powerdown_notifiers =
1645 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1646 static NotifierList suspend_notifiers =
1647 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1648 static NotifierList wakeup_notifiers =
1649 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1650 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1652 ShutdownCause qemu_shutdown_requested_get(void)
1654 return shutdown_requested;
1657 ShutdownCause qemu_reset_requested_get(void)
1659 return reset_requested;
1662 static int qemu_shutdown_requested(void)
1664 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1667 static void qemu_kill_report(void)
1669 if (!qtest_driver() && shutdown_signal) {
1670 if (shutdown_pid == 0) {
1671 /* This happens for eg ^C at the terminal, so it's worth
1672 * avoiding printing an odd message in that case.
1674 error_report("terminating on signal %d", shutdown_signal);
1675 } else {
1676 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1678 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1679 shutdown_signal, shutdown_pid,
1680 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1681 g_free(shutdown_cmd);
1683 shutdown_signal = 0;
1687 static ShutdownCause qemu_reset_requested(void)
1689 ShutdownCause r = reset_requested;
1691 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1692 reset_requested = SHUTDOWN_CAUSE_NONE;
1693 return r;
1695 return SHUTDOWN_CAUSE_NONE;
1698 static int qemu_suspend_requested(void)
1700 int r = suspend_requested;
1701 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1702 suspend_requested = 0;
1703 return r;
1705 return false;
1708 static WakeupReason qemu_wakeup_requested(void)
1710 return wakeup_reason;
1713 static int qemu_powerdown_requested(void)
1715 int r = powerdown_requested;
1716 powerdown_requested = 0;
1717 return r;
1720 static int qemu_debug_requested(void)
1722 int r = debug_requested;
1723 debug_requested = 0;
1724 return r;
1728 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1730 void qemu_system_reset(ShutdownCause reason)
1732 MachineClass *mc;
1734 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1736 cpu_synchronize_all_states();
1738 if (mc && mc->reset) {
1739 mc->reset();
1740 } else {
1741 qemu_devices_reset();
1743 if (reason) {
1744 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1745 &error_abort);
1747 cpu_synchronize_all_post_reset();
1750 void qemu_system_guest_panicked(GuestPanicInformation *info)
1752 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1754 if (current_cpu) {
1755 current_cpu->crash_occurred = true;
1757 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1758 !!info, info, &error_abort);
1759 vm_stop(RUN_STATE_GUEST_PANICKED);
1760 if (!no_shutdown) {
1761 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1762 !!info, info, &error_abort);
1763 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1766 if (info) {
1767 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1768 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1769 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1770 info->u.hyper_v.arg1,
1771 info->u.hyper_v.arg2,
1772 info->u.hyper_v.arg3,
1773 info->u.hyper_v.arg4,
1774 info->u.hyper_v.arg5);
1775 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1776 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1777 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1778 info->u.s390.core,
1779 S390CrashReason_str(info->u.s390.reason),
1780 info->u.s390.psw_mask,
1781 info->u.s390.psw_addr);
1783 qapi_free_GuestPanicInformation(info);
1787 void qemu_system_reset_request(ShutdownCause reason)
1789 if (no_reboot) {
1790 shutdown_requested = reason;
1791 } else {
1792 reset_requested = reason;
1794 cpu_stop_current();
1795 qemu_notify_event();
1798 static void qemu_system_suspend(void)
1800 pause_all_vcpus();
1801 notifier_list_notify(&suspend_notifiers, NULL);
1802 runstate_set(RUN_STATE_SUSPENDED);
1803 qapi_event_send_suspend(&error_abort);
1806 void qemu_system_suspend_request(void)
1808 if (runstate_check(RUN_STATE_SUSPENDED)) {
1809 return;
1811 suspend_requested = 1;
1812 cpu_stop_current();
1813 qemu_notify_event();
1816 void qemu_register_suspend_notifier(Notifier *notifier)
1818 notifier_list_add(&suspend_notifiers, notifier);
1821 void qemu_system_wakeup_request(WakeupReason reason)
1823 trace_system_wakeup_request(reason);
1825 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1826 return;
1828 if (!(wakeup_reason_mask & (1 << reason))) {
1829 return;
1831 runstate_set(RUN_STATE_RUNNING);
1832 wakeup_reason = reason;
1833 qemu_notify_event();
1836 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1838 if (enabled) {
1839 wakeup_reason_mask |= (1 << reason);
1840 } else {
1841 wakeup_reason_mask &= ~(1 << reason);
1845 void qemu_register_wakeup_notifier(Notifier *notifier)
1847 notifier_list_add(&wakeup_notifiers, notifier);
1850 void qemu_system_killed(int signal, pid_t pid)
1852 shutdown_signal = signal;
1853 shutdown_pid = pid;
1854 no_shutdown = 0;
1856 /* Cannot call qemu_system_shutdown_request directly because
1857 * we are in a signal handler.
1859 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1860 qemu_notify_event();
1863 void qemu_system_shutdown_request(ShutdownCause reason)
1865 trace_qemu_system_shutdown_request(reason);
1866 replay_shutdown_request(reason);
1867 shutdown_requested = reason;
1868 qemu_notify_event();
1871 static void qemu_system_powerdown(void)
1873 qapi_event_send_powerdown(&error_abort);
1874 notifier_list_notify(&powerdown_notifiers, NULL);
1877 void qemu_system_powerdown_request(void)
1879 trace_qemu_system_powerdown_request();
1880 powerdown_requested = 1;
1881 qemu_notify_event();
1884 void qemu_register_powerdown_notifier(Notifier *notifier)
1886 notifier_list_add(&powerdown_notifiers, notifier);
1889 void qemu_system_debug_request(void)
1891 debug_requested = 1;
1892 qemu_notify_event();
1895 static bool main_loop_should_exit(void)
1897 RunState r;
1898 ShutdownCause request;
1900 if (qemu_debug_requested()) {
1901 vm_stop(RUN_STATE_DEBUG);
1903 if (qemu_suspend_requested()) {
1904 qemu_system_suspend();
1906 request = qemu_shutdown_requested();
1907 if (request) {
1908 qemu_kill_report();
1909 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1910 &error_abort);
1911 if (no_shutdown) {
1912 vm_stop(RUN_STATE_SHUTDOWN);
1913 } else {
1914 return true;
1917 request = qemu_reset_requested();
1918 if (request) {
1919 pause_all_vcpus();
1920 qemu_system_reset(request);
1921 resume_all_vcpus();
1922 if (!runstate_check(RUN_STATE_RUNNING) &&
1923 !runstate_check(RUN_STATE_INMIGRATE)) {
1924 runstate_set(RUN_STATE_PRELAUNCH);
1927 if (qemu_wakeup_requested()) {
1928 pause_all_vcpus();
1929 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1930 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1931 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1932 resume_all_vcpus();
1933 qapi_event_send_wakeup(&error_abort);
1935 if (qemu_powerdown_requested()) {
1936 qemu_system_powerdown();
1938 if (qemu_vmstop_requested(&r)) {
1939 vm_stop(r);
1941 return false;
1944 static void main_loop(void)
1946 #ifdef CONFIG_PROFILER
1947 int64_t ti;
1948 #endif
1949 do {
1950 #ifdef CONFIG_PROFILER
1951 ti = profile_getclock();
1952 #endif
1953 main_loop_wait(false);
1954 #ifdef CONFIG_PROFILER
1955 dev_time += profile_getclock() - ti;
1956 #endif
1957 } while (!main_loop_should_exit());
1960 static void version(void)
1962 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1963 QEMU_COPYRIGHT "\n");
1966 static void QEMU_NORETURN help(int exitcode)
1968 version();
1969 printf("usage: %s [options] [disk_image]\n\n"
1970 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1971 error_get_progname());
1973 #define QEMU_OPTIONS_GENERATE_HELP
1974 #include "qemu-options-wrapper.h"
1976 printf("\nDuring emulation, the following keys are useful:\n"
1977 "ctrl-alt-f toggle full screen\n"
1978 "ctrl-alt-n switch to virtual console 'n'\n"
1979 "ctrl-alt toggle mouse and keyboard grab\n"
1980 "\n"
1981 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1982 "\n"
1983 QEMU_HELP_BOTTOM "\n");
1985 exit(exitcode);
1988 #define HAS_ARG 0x0001
1990 typedef struct QEMUOption {
1991 const char *name;
1992 int flags;
1993 int index;
1994 uint32_t arch_mask;
1995 } QEMUOption;
1997 static const QEMUOption qemu_options[] = {
1998 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1999 #define QEMU_OPTIONS_GENERATE_OPTIONS
2000 #include "qemu-options-wrapper.h"
2001 { NULL },
2004 typedef struct VGAInterfaceInfo {
2005 const char *opt_name; /* option name */
2006 const char *name; /* human-readable name */
2007 /* Class names indicating that support is available.
2008 * If no class is specified, the interface is always available */
2009 const char *class_names[2];
2010 } VGAInterfaceInfo;
2012 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2013 [VGA_NONE] = {
2014 .opt_name = "none",
2016 [VGA_STD] = {
2017 .opt_name = "std",
2018 .name = "standard VGA",
2019 .class_names = { "VGA", "isa-vga" },
2021 [VGA_CIRRUS] = {
2022 .opt_name = "cirrus",
2023 .name = "Cirrus VGA",
2024 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2026 [VGA_VMWARE] = {
2027 .opt_name = "vmware",
2028 .name = "VMWare SVGA",
2029 .class_names = { "vmware-svga" },
2031 [VGA_VIRTIO] = {
2032 .opt_name = "virtio",
2033 .name = "Virtio VGA",
2034 .class_names = { "virtio-vga" },
2036 [VGA_QXL] = {
2037 .opt_name = "qxl",
2038 .name = "QXL VGA",
2039 .class_names = { "qxl-vga" },
2041 [VGA_TCX] = {
2042 .opt_name = "tcx",
2043 .name = "TCX framebuffer",
2044 .class_names = { "SUNW,tcx" },
2046 [VGA_CG3] = {
2047 .opt_name = "cg3",
2048 .name = "CG3 framebuffer",
2049 .class_names = { "cgthree" },
2051 [VGA_XENFB] = {
2052 .opt_name = "xenfb",
2056 static bool vga_interface_available(VGAInterfaceType t)
2058 VGAInterfaceInfo *ti = &vga_interfaces[t];
2060 assert(t < VGA_TYPE_MAX);
2061 return !ti->class_names[0] ||
2062 object_class_by_name(ti->class_names[0]) ||
2063 object_class_by_name(ti->class_names[1]);
2066 static void select_vgahw(const char *p)
2068 const char *opts;
2069 int t;
2071 assert(vga_interface_type == VGA_NONE);
2072 for (t = 0; t < VGA_TYPE_MAX; t++) {
2073 VGAInterfaceInfo *ti = &vga_interfaces[t];
2074 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2075 if (!vga_interface_available(t)) {
2076 error_report("%s not available", ti->name);
2077 exit(1);
2079 vga_interface_type = t;
2080 break;
2083 if (t == VGA_TYPE_MAX) {
2084 invalid_vga:
2085 error_report("unknown vga type: %s", p);
2086 exit(1);
2088 while (*opts) {
2089 const char *nextopt;
2091 if (strstart(opts, ",retrace=", &nextopt)) {
2092 opts = nextopt;
2093 if (strstart(opts, "dumb", &nextopt))
2094 vga_retrace_method = VGA_RETRACE_DUMB;
2095 else if (strstart(opts, "precise", &nextopt))
2096 vga_retrace_method = VGA_RETRACE_PRECISE;
2097 else goto invalid_vga;
2098 } else goto invalid_vga;
2099 opts = nextopt;
2103 static void parse_display_qapi(const char *optarg)
2105 Error *err = NULL;
2106 DisplayOptions *opts;
2107 Visitor *v;
2109 v = qobject_input_visitor_new_str(optarg, "type", &err);
2110 if (!v) {
2111 error_report_err(err);
2112 exit(1);
2115 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
2116 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
2118 qapi_free_DisplayOptions(opts);
2119 visit_free(v);
2122 static void parse_display(const char *p)
2124 const char *opts;
2126 if (strstart(p, "sdl", &opts)) {
2128 * sdl DisplayType needs hand-crafted parser instead of
2129 * parse_display_qapi() due to some options not in
2130 * DisplayOptions, specifically:
2131 * - frame
2132 * Already deprecated.
2133 * - ctrl_grab + alt_grab
2134 * Not clear yet what happens to them long-term. Should
2135 * replaced by something better or deprecated and dropped.
2137 dpy.type = DISPLAY_TYPE_SDL;
2138 while (*opts) {
2139 const char *nextopt;
2141 if (strstart(opts, ",frame=", &nextopt)) {
2142 g_printerr("The frame= sdl option is deprecated, and will be\n"
2143 "removed in a future release.\n");
2144 opts = nextopt;
2145 if (strstart(opts, "on", &nextopt)) {
2146 no_frame = 0;
2147 } else if (strstart(opts, "off", &nextopt)) {
2148 no_frame = 1;
2149 } else {
2150 goto invalid_sdl_args;
2152 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2153 opts = nextopt;
2154 if (strstart(opts, "on", &nextopt)) {
2155 alt_grab = 1;
2156 } else if (strstart(opts, "off", &nextopt)) {
2157 alt_grab = 0;
2158 } else {
2159 goto invalid_sdl_args;
2161 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2162 opts = nextopt;
2163 if (strstart(opts, "on", &nextopt)) {
2164 ctrl_grab = 1;
2165 } else if (strstart(opts, "off", &nextopt)) {
2166 ctrl_grab = 0;
2167 } else {
2168 goto invalid_sdl_args;
2170 } else if (strstart(opts, ",window_close=", &nextopt)) {
2171 opts = nextopt;
2172 dpy.has_window_close = true;
2173 if (strstart(opts, "on", &nextopt)) {
2174 dpy.window_close = true;
2175 } else if (strstart(opts, "off", &nextopt)) {
2176 dpy.window_close = false;
2177 } else {
2178 goto invalid_sdl_args;
2180 } else if (strstart(opts, ",gl=", &nextopt)) {
2181 opts = nextopt;
2182 dpy.has_gl = true;
2183 if (strstart(opts, "on", &nextopt)) {
2184 dpy.gl = DISPLAYGL_MODE_ON;
2185 } else if (strstart(opts, "core", &nextopt)) {
2186 dpy.gl = DISPLAYGL_MODE_CORE;
2187 } else if (strstart(opts, "es", &nextopt)) {
2188 dpy.gl = DISPLAYGL_MODE_ES;
2189 } else if (strstart(opts, "off", &nextopt)) {
2190 dpy.gl = DISPLAYGL_MODE_OFF;
2191 } else {
2192 goto invalid_sdl_args;
2194 } else {
2195 invalid_sdl_args:
2196 error_report("invalid SDL option string");
2197 exit(1);
2199 opts = nextopt;
2201 } else if (strstart(p, "vnc", &opts)) {
2203 * vnc isn't a (local) DisplayType but a protocol for remote
2204 * display access.
2206 if (*opts == '=') {
2207 vnc_parse(opts + 1, &error_fatal);
2208 } else {
2209 error_report("VNC requires a display argument vnc=<display>");
2210 exit(1);
2212 } else {
2213 parse_display_qapi(p);
2217 static int balloon_parse(const char *arg)
2219 QemuOpts *opts;
2221 warn_report("This option is deprecated. "
2222 "Use '--device virtio-balloon' to enable the balloon device.");
2224 if (strcmp(arg, "none") == 0) {
2225 return 0;
2228 if (!strncmp(arg, "virtio", 6)) {
2229 if (arg[6] == ',') {
2230 /* have params -> parse them */
2231 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2232 false);
2233 if (!opts)
2234 return -1;
2235 } else {
2236 /* create empty opts */
2237 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2238 &error_abort);
2240 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2241 return 0;
2244 return -1;
2247 #if defined(CONFIG_RUBY)
2248 /* Ruby interface for QEMU. See README.EXT for programming example. */
2249 #warning mit ruby
2250 #include <ruby.h>
2252 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2254 int i;
2255 for (i = 0; i < argc; i++) {
2256 argv[i] = StringValue(argv[i]);
2257 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2259 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2260 //~ mode = 0666; /* default value */
2261 //~ }
2262 return INT2FIX(argc);
2265 void Init_qemu(void)
2267 printf("%s\n", __func__);
2268 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2269 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2270 #if 0
2271 rb_define_method();
2272 #endif
2274 #endif /* CONFIG_RUBY */
2276 #if defined(CONFIG_DLL)
2277 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2279 return FALSE;
2281 #endif /* CONFIG_DLL */
2283 char *qemu_find_file(int type, const char *name)
2285 int i;
2286 const char *subdir;
2287 char *buf;
2289 /* Try the name as a straight path first */
2290 if (access(name, R_OK) == 0) {
2291 trace_load_file(name, name);
2292 return g_strdup(name);
2295 switch (type) {
2296 case QEMU_FILE_TYPE_BIOS:
2297 subdir = "";
2298 break;
2299 case QEMU_FILE_TYPE_KEYMAP:
2300 subdir = "keymaps/";
2301 break;
2302 default:
2303 abort();
2306 for (i = 0; i < data_dir_idx; i++) {
2307 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2308 if (access(buf, R_OK) == 0) {
2309 trace_load_file(name, buf);
2310 return buf;
2312 g_free(buf);
2314 return NULL;
2317 static void qemu_add_data_dir(const char *path)
2319 int i;
2321 if (path == NULL) {
2322 return;
2324 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2325 return;
2327 for (i = 0; i < data_dir_idx; i++) {
2328 if (strcmp(data_dir[i], path) == 0) {
2329 return; /* duplicate */
2332 data_dir[data_dir_idx++] = g_strdup(path);
2335 static inline bool nonempty_str(const char *str)
2337 return str && *str;
2340 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2342 gchar *buf;
2343 size_t size;
2344 const char *name, *file, *str;
2345 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2347 if (fw_cfg == NULL) {
2348 error_report("fw_cfg device not available");
2349 return -1;
2351 name = qemu_opt_get(opts, "name");
2352 file = qemu_opt_get(opts, "file");
2353 str = qemu_opt_get(opts, "string");
2355 /* we need name and either a file or the content string */
2356 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2357 error_report("invalid argument(s)");
2358 return -1;
2360 if (nonempty_str(file) && nonempty_str(str)) {
2361 error_report("file and string are mutually exclusive");
2362 return -1;
2364 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2365 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2366 return -1;
2368 if (strncmp(name, "opt/", 4) != 0) {
2369 warn_report("externally provided fw_cfg item names "
2370 "should be prefixed with \"opt/\"");
2372 if (nonempty_str(str)) {
2373 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2374 buf = g_memdup(str, size);
2375 } else {
2376 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2377 error_report("can't load %s", file);
2378 return -1;
2381 /* For legacy, keep user files in a specific global order. */
2382 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2383 fw_cfg_add_file(fw_cfg, name, buf, size);
2384 fw_cfg_reset_order_override(fw_cfg);
2385 return 0;
2388 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2390 return qdev_device_help(opts);
2393 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2395 Error *err = NULL;
2396 DeviceState *dev;
2398 dev = qdev_device_add(opts, &err);
2399 if (!dev) {
2400 error_report_err(err);
2401 return -1;
2403 object_unref(OBJECT(dev));
2404 return 0;
2407 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2409 Error *local_err = NULL;
2411 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2412 if (local_err) {
2413 error_report_err(local_err);
2414 return -1;
2416 exit(0);
2418 return 0;
2421 #ifdef CONFIG_VIRTFS
2422 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2424 return qemu_fsdev_add(opts);
2426 #endif
2428 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2430 Chardev *chr;
2431 const char *chardev;
2432 const char *mode;
2433 int flags;
2435 mode = qemu_opt_get(opts, "mode");
2436 if (mode == NULL) {
2437 mode = "readline";
2439 if (strcmp(mode, "readline") == 0) {
2440 flags = MONITOR_USE_READLINE;
2441 } else if (strcmp(mode, "control") == 0) {
2442 flags = MONITOR_USE_CONTROL;
2443 } else {
2444 error_report("unknown monitor mode \"%s\"", mode);
2445 exit(1);
2448 if (qemu_opt_get_bool(opts, "pretty", 0))
2449 flags |= MONITOR_USE_PRETTY;
2451 /* OOB is off by default */
2452 if (qemu_opt_get_bool(opts, "x-oob", 0)) {
2453 flags |= MONITOR_USE_OOB;
2456 chardev = qemu_opt_get(opts, "chardev");
2457 chr = qemu_chr_find(chardev);
2458 if (chr == NULL) {
2459 error_report("chardev \"%s\" not found", chardev);
2460 exit(1);
2463 monitor_init(chr, flags);
2464 return 0;
2467 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2469 static int monitor_device_index = 0;
2470 QemuOpts *opts;
2471 const char *p;
2472 char label[32];
2474 if (strstart(optarg, "chardev:", &p)) {
2475 snprintf(label, sizeof(label), "%s", p);
2476 } else {
2477 snprintf(label, sizeof(label), "compat_monitor%d",
2478 monitor_device_index);
2479 opts = qemu_chr_parse_compat(label, optarg);
2480 if (!opts) {
2481 error_report("parse error: %s", optarg);
2482 exit(1);
2486 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2487 qemu_opt_set(opts, "mode", mode, &error_abort);
2488 qemu_opt_set(opts, "chardev", label, &error_abort);
2489 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2490 monitor_device_index++;
2493 struct device_config {
2494 enum {
2495 DEV_USB, /* -usbdevice */
2496 DEV_BT, /* -bt */
2497 DEV_SERIAL, /* -serial */
2498 DEV_PARALLEL, /* -parallel */
2499 DEV_VIRTCON, /* -virtioconsole */
2500 DEV_DEBUGCON, /* -debugcon */
2501 DEV_GDB, /* -gdb, -s */
2502 DEV_SCLP, /* s390 sclp */
2503 } type;
2504 const char *cmdline;
2505 Location loc;
2506 QTAILQ_ENTRY(device_config) next;
2509 static QTAILQ_HEAD(, device_config) device_configs =
2510 QTAILQ_HEAD_INITIALIZER(device_configs);
2512 static void add_device_config(int type, const char *cmdline)
2514 struct device_config *conf;
2516 conf = g_malloc0(sizeof(*conf));
2517 conf->type = type;
2518 conf->cmdline = cmdline;
2519 loc_save(&conf->loc);
2520 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2523 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2525 struct device_config *conf;
2526 int rc;
2528 QTAILQ_FOREACH(conf, &device_configs, next) {
2529 if (conf->type != type)
2530 continue;
2531 loc_push_restore(&conf->loc);
2532 rc = func(conf->cmdline);
2533 loc_pop(&conf->loc);
2534 if (rc) {
2535 return rc;
2538 return 0;
2541 static int serial_parse(const char *devname)
2543 int index = num_serial_hds;
2544 char label[32];
2546 if (strcmp(devname, "none") == 0)
2547 return 0;
2548 snprintf(label, sizeof(label), "serial%d", index);
2549 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
2551 serial_hds[index] = qemu_chr_new(label, devname);
2552 if (!serial_hds[index]) {
2553 error_report("could not connect serial device"
2554 " to character backend '%s'", devname);
2555 return -1;
2557 num_serial_hds++;
2558 return 0;
2561 Chardev *serial_hd(int i)
2563 assert(i >= 0);
2564 if (i < num_serial_hds) {
2565 return serial_hds[i];
2567 return NULL;
2570 int serial_max_hds(void)
2572 return num_serial_hds;
2575 static int parallel_parse(const char *devname)
2577 static int index = 0;
2578 char label[32];
2580 if (strcmp(devname, "none") == 0)
2581 return 0;
2582 if (index == MAX_PARALLEL_PORTS) {
2583 error_report("too many parallel ports");
2584 exit(1);
2586 snprintf(label, sizeof(label), "parallel%d", index);
2587 parallel_hds[index] = qemu_chr_new(label, devname);
2588 if (!parallel_hds[index]) {
2589 error_report("could not connect parallel device"
2590 " to character backend '%s'", devname);
2591 return -1;
2593 index++;
2594 return 0;
2597 static int virtcon_parse(const char *devname)
2599 QemuOptsList *device = qemu_find_opts("device");
2600 static int index = 0;
2601 char label[32];
2602 QemuOpts *bus_opts, *dev_opts;
2604 if (strcmp(devname, "none") == 0)
2605 return 0;
2606 if (index == MAX_VIRTIO_CONSOLES) {
2607 error_report("too many virtio consoles");
2608 exit(1);
2611 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2612 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2614 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2615 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2617 snprintf(label, sizeof(label), "virtcon%d", index);
2618 virtcon_hds[index] = qemu_chr_new(label, devname);
2619 if (!virtcon_hds[index]) {
2620 error_report("could not connect virtio console"
2621 " to character backend '%s'", devname);
2622 return -1;
2624 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2626 index++;
2627 return 0;
2630 static int debugcon_parse(const char *devname)
2632 QemuOpts *opts;
2634 if (!qemu_chr_new("debugcon", devname)) {
2635 exit(1);
2637 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2638 if (!opts) {
2639 error_report("already have a debugcon device");
2640 exit(1);
2642 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2643 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2644 return 0;
2647 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2649 const MachineClass *mc1 = a, *mc2 = b;
2650 int res;
2652 if (mc1->family == NULL) {
2653 if (mc2->family == NULL) {
2654 /* Compare standalone machine types against each other; they sort
2655 * in increasing order.
2657 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2658 object_class_get_name(OBJECT_CLASS(mc2)));
2661 /* Standalone machine types sort after families. */
2662 return 1;
2665 if (mc2->family == NULL) {
2666 /* Families sort before standalone machine types. */
2667 return -1;
2670 /* Families sort between each other alphabetically increasingly. */
2671 res = strcmp(mc1->family, mc2->family);
2672 if (res != 0) {
2673 return res;
2676 /* Within the same family, machine types sort in decreasing order. */
2677 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2678 object_class_get_name(OBJECT_CLASS(mc1)));
2681 static MachineClass *machine_parse(const char *name)
2683 MachineClass *mc = NULL;
2684 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2686 if (name) {
2687 mc = find_machine(name);
2689 if (mc) {
2690 g_slist_free(machines);
2691 return mc;
2693 if (name && !is_help_option(name)) {
2694 error_report("unsupported machine type");
2695 error_printf("Use -machine help to list supported machines\n");
2696 } else {
2697 printf("Supported machines are:\n");
2698 machines = g_slist_sort(machines, machine_class_cmp);
2699 for (el = machines; el; el = el->next) {
2700 MachineClass *mc = el->data;
2701 if (mc->alias) {
2702 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2704 printf("%-20s %s%s\n", mc->name, mc->desc,
2705 mc->is_default ? " (default)" : "");
2709 g_slist_free(machines);
2710 exit(!name || !is_help_option(name));
2713 void qemu_add_exit_notifier(Notifier *notify)
2715 notifier_list_add(&exit_notifiers, notify);
2718 void qemu_remove_exit_notifier(Notifier *notify)
2720 notifier_remove(notify);
2723 static void qemu_run_exit_notifiers(void)
2725 notifier_list_notify(&exit_notifiers, NULL);
2728 bool machine_init_done;
2730 void qemu_add_machine_init_done_notifier(Notifier *notify)
2732 notifier_list_add(&machine_init_done_notifiers, notify);
2733 if (machine_init_done) {
2734 notify->notify(notify, NULL);
2738 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2740 notifier_remove(notify);
2743 static void qemu_run_machine_init_done_notifiers(void)
2745 machine_init_done = true;
2746 notifier_list_notify(&machine_init_done_notifiers, NULL);
2749 static const QEMUOption *lookup_opt(int argc, char **argv,
2750 const char **poptarg, int *poptind)
2752 const QEMUOption *popt;
2753 int optind = *poptind;
2754 char *r = argv[optind];
2755 const char *optarg;
2757 loc_set_cmdline(argv, optind, 1);
2758 optind++;
2759 /* Treat --foo the same as -foo. */
2760 if (r[1] == '-')
2761 r++;
2762 popt = qemu_options;
2763 for(;;) {
2764 if (!popt->name) {
2765 error_report("invalid option");
2766 exit(1);
2768 if (!strcmp(popt->name, r + 1))
2769 break;
2770 popt++;
2772 if (popt->flags & HAS_ARG) {
2773 if (optind >= argc) {
2774 error_report("requires an argument");
2775 exit(1);
2777 optarg = argv[optind++];
2778 loc_set_cmdline(argv, optind - 2, 2);
2779 } else {
2780 optarg = NULL;
2783 *poptarg = optarg;
2784 *poptind = optind;
2786 return popt;
2789 static MachineClass *select_machine(void)
2791 MachineClass *machine_class = find_default_machine();
2792 const char *optarg;
2793 QemuOpts *opts;
2794 Location loc;
2796 loc_push_none(&loc);
2798 opts = qemu_get_machine_opts();
2799 qemu_opts_loc_restore(opts);
2801 optarg = qemu_opt_get(opts, "type");
2802 if (optarg) {
2803 machine_class = machine_parse(optarg);
2806 if (!machine_class) {
2807 error_report("No machine specified, and there is no default");
2808 error_printf("Use -machine help to list supported machines\n");
2809 exit(1);
2812 loc_pop(&loc);
2813 return machine_class;
2816 static int machine_set_property(void *opaque,
2817 const char *name, const char *value,
2818 Error **errp)
2820 Object *obj = OBJECT(opaque);
2821 Error *local_err = NULL;
2822 char *p, *qom_name;
2824 if (strcmp(name, "type") == 0) {
2825 return 0;
2828 qom_name = g_strdup(name);
2829 for (p = qom_name; *p; p++) {
2830 if (*p == '_') {
2831 *p = '-';
2835 object_property_parse(obj, value, qom_name, &local_err);
2836 g_free(qom_name);
2838 if (local_err) {
2839 error_report_err(local_err);
2840 return -1;
2843 return 0;
2848 * Initial object creation happens before all other
2849 * QEMU data types are created. The majority of objects
2850 * can be created at this point. The rng-egd object
2851 * cannot be created here, as it depends on the chardev
2852 * already existing.
2854 static bool object_create_initial(const char *type)
2856 if (g_str_equal(type, "rng-egd") ||
2857 g_str_has_prefix(type, "pr-manager-")) {
2858 return false;
2861 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2862 if (g_str_equal(type, "cryptodev-vhost-user")) {
2863 return false;
2865 #endif
2868 * return false for concrete netfilters since
2869 * they depend on netdevs already existing
2871 if (g_str_equal(type, "filter-buffer") ||
2872 g_str_equal(type, "filter-dump") ||
2873 g_str_equal(type, "filter-mirror") ||
2874 g_str_equal(type, "filter-redirector") ||
2875 g_str_equal(type, "colo-compare") ||
2876 g_str_equal(type, "filter-rewriter") ||
2877 g_str_equal(type, "filter-replay")) {
2878 return false;
2881 /* Memory allocation by backends needs to be done
2882 * after configure_accelerator() (due to the tcg_enabled()
2883 * checks at memory_region_init_*()).
2885 * Also, allocation of large amounts of memory may delay
2886 * chardev initialization for too long, and trigger timeouts
2887 * on software that waits for a monitor socket to be created
2888 * (e.g. libvirt).
2890 if (g_str_has_prefix(type, "memory-backend-")) {
2891 return false;
2894 return true;
2899 * The remainder of object creation happens after the
2900 * creation of chardev, fsdev, net clients and device data types.
2902 static bool object_create_delayed(const char *type)
2904 return !object_create_initial(type);
2908 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2909 MachineClass *mc)
2911 uint64_t sz;
2912 const char *mem_str;
2913 const ram_addr_t default_ram_size = mc->default_ram_size;
2914 QemuOpts *opts = qemu_find_opts_singleton("memory");
2915 Location loc;
2917 loc_push_none(&loc);
2918 qemu_opts_loc_restore(opts);
2920 sz = 0;
2921 mem_str = qemu_opt_get(opts, "size");
2922 if (mem_str) {
2923 if (!*mem_str) {
2924 error_report("missing 'size' option value");
2925 exit(EXIT_FAILURE);
2928 sz = qemu_opt_get_size(opts, "size", ram_size);
2930 /* Fix up legacy suffix-less format */
2931 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2932 uint64_t overflow_check = sz;
2934 sz <<= 20;
2935 if ((sz >> 20) != overflow_check) {
2936 error_report("too large 'size' option value");
2937 exit(EXIT_FAILURE);
2942 /* backward compatibility behaviour for case "-m 0" */
2943 if (sz == 0) {
2944 sz = default_ram_size;
2947 sz = QEMU_ALIGN_UP(sz, 8192);
2948 ram_size = sz;
2949 if (ram_size != sz) {
2950 error_report("ram size too large");
2951 exit(EXIT_FAILURE);
2954 /* store value for the future use */
2955 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2956 *maxram_size = ram_size;
2958 if (qemu_opt_get(opts, "maxmem")) {
2959 uint64_t slots;
2961 sz = qemu_opt_get_size(opts, "maxmem", 0);
2962 slots = qemu_opt_get_number(opts, "slots", 0);
2963 if (sz < ram_size) {
2964 error_report("invalid value of -m option maxmem: "
2965 "maximum memory size (0x%" PRIx64 ") must be at least "
2966 "the initial memory size (0x" RAM_ADDR_FMT ")",
2967 sz, ram_size);
2968 exit(EXIT_FAILURE);
2969 } else if (slots && sz == ram_size) {
2970 error_report("invalid value of -m option maxmem: "
2971 "memory slots were specified but maximum memory size "
2972 "(0x%" PRIx64 ") is equal to the initial memory size "
2973 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2974 exit(EXIT_FAILURE);
2977 *maxram_size = sz;
2978 *ram_slots = slots;
2979 } else if (qemu_opt_get(opts, "slots")) {
2980 error_report("invalid -m option value: missing 'maxmem' option");
2981 exit(EXIT_FAILURE);
2984 loc_pop(&loc);
2987 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2989 GlobalProperty *g;
2991 g = g_malloc0(sizeof(*g));
2992 g->driver = qemu_opt_get(opts, "driver");
2993 g->property = qemu_opt_get(opts, "property");
2994 g->value = qemu_opt_get(opts, "value");
2995 g->user_provided = true;
2996 g->errp = &error_fatal;
2997 qdev_prop_register_global(g);
2998 return 0;
3001 static int qemu_read_default_config_file(void)
3003 int ret;
3005 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3006 if (ret < 0 && ret != -ENOENT) {
3007 return ret;
3010 return 0;
3013 static void user_register_global_props(void)
3015 qemu_opts_foreach(qemu_find_opts("global"),
3016 global_init_func, NULL, NULL);
3020 * Note: we should see that these properties are actually having a
3021 * priority: accel < machine < user. This means e.g. when user
3022 * specifies something in "-global", it'll always be used with highest
3023 * priority than either machine/accelerator compat properties.
3025 static void register_global_properties(MachineState *ms)
3027 accel_register_compat_props(ms->accelerator);
3028 machine_register_compat_props(ms);
3029 user_register_global_props();
3032 int main(int argc, char **argv)
3034 int i;
3035 int snapshot, linux_boot;
3036 const char *initrd_filename;
3037 const char *kernel_filename, *kernel_cmdline;
3038 const char *boot_order = NULL;
3039 const char *boot_once = NULL;
3040 DisplayState *ds;
3041 QemuOpts *opts, *machine_opts;
3042 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
3043 QemuOptsList *olist;
3044 int optind;
3045 const char *optarg;
3046 const char *loadvm = NULL;
3047 MachineClass *machine_class;
3048 const char *cpu_model;
3049 const char *vga_model = NULL;
3050 const char *qtest_chrdev = NULL;
3051 const char *qtest_log = NULL;
3052 const char *pid_file = NULL;
3053 const char *incoming = NULL;
3054 bool userconfig = true;
3055 bool nographic = false;
3056 int display_remote = 0;
3057 const char *log_mask = NULL;
3058 const char *log_file = NULL;
3059 char *trace_file = NULL;
3060 ram_addr_t maxram_size;
3061 uint64_t ram_slots = 0;
3062 FILE *vmstate_dump_file = NULL;
3063 Error *main_loop_err = NULL;
3064 Error *err = NULL;
3065 bool list_data_dirs = false;
3066 char *dir, **dirs;
3067 typedef struct BlockdevOptions_queue {
3068 BlockdevOptions *bdo;
3069 Location loc;
3070 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3071 } BlockdevOptions_queue;
3072 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3073 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3075 module_call_init(MODULE_INIT_TRACE);
3077 qemu_init_cpu_list();
3078 qemu_init_cpu_loop();
3080 qemu_mutex_lock_iothread();
3082 atexit(qemu_run_exit_notifiers);
3083 error_set_progname(argv[0]);
3084 qemu_init_exec_dir(argv[0]);
3086 module_call_init(MODULE_INIT_QOM);
3088 qemu_add_opts(&qemu_drive_opts);
3089 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3090 qemu_add_drive_opts(&qemu_common_drive_opts);
3091 qemu_add_drive_opts(&qemu_drive_opts);
3092 qemu_add_drive_opts(&bdrv_runtime_opts);
3093 qemu_add_opts(&qemu_chardev_opts);
3094 qemu_add_opts(&qemu_device_opts);
3095 qemu_add_opts(&qemu_netdev_opts);
3096 qemu_add_opts(&qemu_nic_opts);
3097 qemu_add_opts(&qemu_net_opts);
3098 qemu_add_opts(&qemu_rtc_opts);
3099 qemu_add_opts(&qemu_global_opts);
3100 qemu_add_opts(&qemu_mon_opts);
3101 qemu_add_opts(&qemu_trace_opts);
3102 qemu_add_opts(&qemu_option_rom_opts);
3103 qemu_add_opts(&qemu_machine_opts);
3104 qemu_add_opts(&qemu_accel_opts);
3105 qemu_add_opts(&qemu_mem_opts);
3106 qemu_add_opts(&qemu_smp_opts);
3107 qemu_add_opts(&qemu_boot_opts);
3108 qemu_add_opts(&qemu_sandbox_opts);
3109 qemu_add_opts(&qemu_add_fd_opts);
3110 qemu_add_opts(&qemu_object_opts);
3111 qemu_add_opts(&qemu_tpmdev_opts);
3112 qemu_add_opts(&qemu_realtime_opts);
3113 qemu_add_opts(&qemu_msg_opts);
3114 qemu_add_opts(&qemu_name_opts);
3115 qemu_add_opts(&qemu_numa_opts);
3116 qemu_add_opts(&qemu_icount_opts);
3117 qemu_add_opts(&qemu_semihosting_config_opts);
3118 qemu_add_opts(&qemu_fw_cfg_opts);
3119 module_call_init(MODULE_INIT_OPTS);
3121 runstate_init();
3122 postcopy_infrastructure_init();
3124 if (qcrypto_init(&err) < 0) {
3125 error_reportf_err(err, "cannot initialize crypto: ");
3126 exit(1);
3128 rtc_clock = QEMU_CLOCK_HOST;
3130 QLIST_INIT (&vm_change_state_head);
3131 os_setup_early_signal_handling();
3133 cpu_model = NULL;
3134 snapshot = 0;
3136 nb_nics = 0;
3138 bdrv_init_with_whitelist();
3140 autostart = 1;
3142 /* first pass of option parsing */
3143 optind = 1;
3144 while (optind < argc) {
3145 if (argv[optind][0] != '-') {
3146 /* disk image */
3147 optind++;
3148 } else {
3149 const QEMUOption *popt;
3151 popt = lookup_opt(argc, argv, &optarg, &optind);
3152 switch (popt->index) {
3153 case QEMU_OPTION_nodefconfig:
3154 case QEMU_OPTION_nouserconfig:
3155 userconfig = false;
3156 break;
3161 if (userconfig) {
3162 if (qemu_read_default_config_file() < 0) {
3163 exit(1);
3167 /* second pass of option parsing */
3168 optind = 1;
3169 for(;;) {
3170 if (optind >= argc)
3171 break;
3172 if (argv[optind][0] != '-') {
3173 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3174 } else {
3175 const QEMUOption *popt;
3177 popt = lookup_opt(argc, argv, &optarg, &optind);
3178 if (!(popt->arch_mask & arch_type)) {
3179 error_report("Option not supported for this target");
3180 exit(1);
3182 switch(popt->index) {
3183 case QEMU_OPTION_cpu:
3184 /* hw initialization will check this */
3185 cpu_model = optarg;
3186 break;
3187 case QEMU_OPTION_hda:
3188 case QEMU_OPTION_hdb:
3189 case QEMU_OPTION_hdc:
3190 case QEMU_OPTION_hdd:
3191 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3192 HD_OPTS);
3193 break;
3194 case QEMU_OPTION_blockdev:
3196 Visitor *v;
3197 BlockdevOptions_queue *bdo;
3199 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3200 if (!v) {
3201 error_report_err(err);
3202 exit(1);
3205 bdo = g_new(BlockdevOptions_queue, 1);
3206 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3207 &error_fatal);
3208 visit_free(v);
3209 loc_save(&bdo->loc);
3210 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3211 break;
3213 case QEMU_OPTION_drive:
3214 if (drive_def(optarg) == NULL) {
3215 exit(1);
3217 break;
3218 case QEMU_OPTION_set:
3219 if (qemu_set_option(optarg) != 0)
3220 exit(1);
3221 break;
3222 case QEMU_OPTION_global:
3223 if (qemu_global_option(optarg) != 0)
3224 exit(1);
3225 break;
3226 case QEMU_OPTION_mtdblock:
3227 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3228 break;
3229 case QEMU_OPTION_sd:
3230 drive_add(IF_SD, -1, optarg, SD_OPTS);
3231 break;
3232 case QEMU_OPTION_pflash:
3233 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3234 break;
3235 case QEMU_OPTION_snapshot:
3236 snapshot = 1;
3237 break;
3238 case QEMU_OPTION_numa:
3239 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3240 optarg, true);
3241 if (!opts) {
3242 exit(1);
3244 break;
3245 case QEMU_OPTION_display:
3246 parse_display(optarg);
3247 break;
3248 case QEMU_OPTION_nographic:
3249 olist = qemu_find_opts("machine");
3250 qemu_opts_parse_noisily(olist, "graphics=off", false);
3251 nographic = true;
3252 dpy.type = DISPLAY_TYPE_NONE;
3253 break;
3254 case QEMU_OPTION_curses:
3255 #ifdef CONFIG_CURSES
3256 dpy.type = DISPLAY_TYPE_CURSES;
3257 #else
3258 error_report("curses support is disabled");
3259 exit(1);
3260 #endif
3261 break;
3262 case QEMU_OPTION_portrait:
3263 graphic_rotate = 90;
3264 break;
3265 case QEMU_OPTION_rotate:
3266 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3267 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3268 graphic_rotate != 180 && graphic_rotate != 270) {
3269 error_report("only 90, 180, 270 deg rotation is available");
3270 exit(1);
3272 break;
3273 case QEMU_OPTION_kernel:
3274 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3275 &error_abort);
3276 break;
3277 case QEMU_OPTION_initrd:
3278 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3279 &error_abort);
3280 break;
3281 case QEMU_OPTION_append:
3282 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3283 &error_abort);
3284 break;
3285 case QEMU_OPTION_dtb:
3286 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3287 &error_abort);
3288 break;
3289 case QEMU_OPTION_cdrom:
3290 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3291 break;
3292 case QEMU_OPTION_boot:
3293 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3294 optarg, true);
3295 if (!opts) {
3296 exit(1);
3298 break;
3299 case QEMU_OPTION_fda:
3300 case QEMU_OPTION_fdb:
3301 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3302 optarg, FD_OPTS);
3303 break;
3304 case QEMU_OPTION_no_fd_bootchk:
3305 fd_bootchk = 0;
3306 break;
3307 case QEMU_OPTION_netdev:
3308 default_net = 0;
3309 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3310 exit(1);
3312 break;
3313 case QEMU_OPTION_nic:
3314 default_net = 0;
3315 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3316 exit(1);
3318 break;
3319 case QEMU_OPTION_net:
3320 default_net = 0;
3321 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3322 exit(1);
3324 break;
3325 #ifdef CONFIG_LIBISCSI
3326 case QEMU_OPTION_iscsi:
3327 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3328 optarg, false);
3329 if (!opts) {
3330 exit(1);
3332 break;
3333 #endif
3334 #ifdef CONFIG_SLIRP
3335 case QEMU_OPTION_tftp:
3336 error_report("The -tftp option is deprecated. "
3337 "Please use '-netdev user,tftp=...' instead.");
3338 legacy_tftp_prefix = optarg;
3339 break;
3340 case QEMU_OPTION_bootp:
3341 error_report("The -bootp option is deprecated. "
3342 "Please use '-netdev user,bootfile=...' instead.");
3343 legacy_bootp_filename = optarg;
3344 break;
3345 case QEMU_OPTION_redir:
3346 error_report("The -redir option is deprecated. "
3347 "Please use '-netdev user,hostfwd=...' instead.");
3348 if (net_slirp_redir(optarg) < 0)
3349 exit(1);
3350 break;
3351 #endif
3352 case QEMU_OPTION_bt:
3353 add_device_config(DEV_BT, optarg);
3354 break;
3355 case QEMU_OPTION_audio_help:
3356 AUD_help ();
3357 exit (0);
3358 break;
3359 case QEMU_OPTION_soundhw:
3360 select_soundhw (optarg);
3361 break;
3362 case QEMU_OPTION_h:
3363 help(0);
3364 break;
3365 case QEMU_OPTION_version:
3366 version();
3367 exit(0);
3368 break;
3369 case QEMU_OPTION_m:
3370 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3371 optarg, true);
3372 if (!opts) {
3373 exit(EXIT_FAILURE);
3375 break;
3376 #ifdef CONFIG_TPM
3377 case QEMU_OPTION_tpmdev:
3378 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3379 exit(1);
3381 break;
3382 #endif
3383 case QEMU_OPTION_mempath:
3384 mem_path = optarg;
3385 break;
3386 case QEMU_OPTION_mem_prealloc:
3387 mem_prealloc = 1;
3388 break;
3389 case QEMU_OPTION_d:
3390 log_mask = optarg;
3391 break;
3392 case QEMU_OPTION_D:
3393 log_file = optarg;
3394 break;
3395 case QEMU_OPTION_DFILTER:
3396 qemu_set_dfilter_ranges(optarg, &error_fatal);
3397 break;
3398 case QEMU_OPTION_s:
3399 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3400 break;
3401 case QEMU_OPTION_gdb:
3402 add_device_config(DEV_GDB, optarg);
3403 break;
3404 case QEMU_OPTION_L:
3405 if (is_help_option(optarg)) {
3406 list_data_dirs = true;
3407 } else {
3408 qemu_add_data_dir(optarg);
3410 break;
3411 case QEMU_OPTION_bios:
3412 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3413 &error_abort);
3414 break;
3415 case QEMU_OPTION_singlestep:
3416 singlestep = 1;
3417 break;
3418 case QEMU_OPTION_S:
3419 autostart = 0;
3420 break;
3421 case QEMU_OPTION_k:
3422 keyboard_layout = optarg;
3423 break;
3424 case QEMU_OPTION_localtime:
3425 rtc_utc = 0;
3426 warn_report("This option is deprecated, "
3427 "use '-rtc base=localtime' instead.");
3428 break;
3429 case QEMU_OPTION_vga:
3430 vga_model = optarg;
3431 default_vga = 0;
3432 break;
3433 case QEMU_OPTION_g:
3435 const char *p;
3436 int w, h, depth;
3437 p = optarg;
3438 w = strtol(p, (char **)&p, 10);
3439 if (w <= 0) {
3440 graphic_error:
3441 error_report("invalid resolution or depth");
3442 exit(1);
3444 if (*p != 'x')
3445 goto graphic_error;
3446 p++;
3447 h = strtol(p, (char **)&p, 10);
3448 if (h <= 0)
3449 goto graphic_error;
3450 if (*p == 'x') {
3451 p++;
3452 depth = strtol(p, (char **)&p, 10);
3453 if (depth != 8 && depth != 15 && depth != 16 &&
3454 depth != 24 && depth != 32)
3455 goto graphic_error;
3456 } else if (*p == '\0') {
3457 depth = graphic_depth;
3458 } else {
3459 goto graphic_error;
3462 graphic_width = w;
3463 graphic_height = h;
3464 graphic_depth = depth;
3466 break;
3467 case QEMU_OPTION_echr:
3469 char *r;
3470 term_escape_char = strtol(optarg, &r, 0);
3471 if (r == optarg)
3472 printf("Bad argument to echr\n");
3473 break;
3475 case QEMU_OPTION_monitor:
3476 default_monitor = 0;
3477 if (strncmp(optarg, "none", 4)) {
3478 monitor_parse(optarg, "readline", false);
3480 break;
3481 case QEMU_OPTION_qmp:
3482 monitor_parse(optarg, "control", false);
3483 default_monitor = 0;
3484 break;
3485 case QEMU_OPTION_qmp_pretty:
3486 monitor_parse(optarg, "control", true);
3487 default_monitor = 0;
3488 break;
3489 case QEMU_OPTION_mon:
3490 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3491 true);
3492 if (!opts) {
3493 exit(1);
3495 default_monitor = 0;
3496 break;
3497 case QEMU_OPTION_chardev:
3498 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3499 optarg, true);
3500 if (!opts) {
3501 exit(1);
3503 break;
3504 case QEMU_OPTION_fsdev:
3505 olist = qemu_find_opts("fsdev");
3506 if (!olist) {
3507 error_report("fsdev support is disabled");
3508 exit(1);
3510 opts = qemu_opts_parse_noisily(olist, optarg, true);
3511 if (!opts) {
3512 exit(1);
3514 break;
3515 case QEMU_OPTION_virtfs: {
3516 QemuOpts *fsdev;
3517 QemuOpts *device;
3518 const char *writeout, *sock_fd, *socket, *path, *security_model;
3520 olist = qemu_find_opts("virtfs");
3521 if (!olist) {
3522 error_report("virtfs support is disabled");
3523 exit(1);
3525 opts = qemu_opts_parse_noisily(olist, optarg, true);
3526 if (!opts) {
3527 exit(1);
3530 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3531 qemu_opt_get(opts, "mount_tag") == NULL) {
3532 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3533 exit(1);
3535 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3536 qemu_opts_id(opts) ?:
3537 qemu_opt_get(opts, "mount_tag"),
3538 1, NULL);
3539 if (!fsdev) {
3540 error_report("duplicate or invalid fsdev id: %s",
3541 qemu_opt_get(opts, "mount_tag"));
3542 exit(1);
3545 writeout = qemu_opt_get(opts, "writeout");
3546 if (writeout) {
3547 #ifdef CONFIG_SYNC_FILE_RANGE
3548 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3549 #else
3550 error_report("writeout=immediate not supported "
3551 "on this platform");
3552 exit(1);
3553 #endif
3555 qemu_opt_set(fsdev, "fsdriver",
3556 qemu_opt_get(opts, "fsdriver"), &error_abort);
3557 path = qemu_opt_get(opts, "path");
3558 if (path) {
3559 qemu_opt_set(fsdev, "path", path, &error_abort);
3561 security_model = qemu_opt_get(opts, "security_model");
3562 if (security_model) {
3563 qemu_opt_set(fsdev, "security_model", security_model,
3564 &error_abort);
3566 socket = qemu_opt_get(opts, "socket");
3567 if (socket) {
3568 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3570 sock_fd = qemu_opt_get(opts, "sock_fd");
3571 if (sock_fd) {
3572 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3575 qemu_opt_set_bool(fsdev, "readonly",
3576 qemu_opt_get_bool(opts, "readonly", 0),
3577 &error_abort);
3578 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3579 &error_abort);
3580 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3581 qemu_opt_set(device, "fsdev",
3582 qemu_opts_id(fsdev), &error_abort);
3583 qemu_opt_set(device, "mount_tag",
3584 qemu_opt_get(opts, "mount_tag"), &error_abort);
3585 break;
3587 case QEMU_OPTION_virtfs_synth: {
3588 QemuOpts *fsdev;
3589 QemuOpts *device;
3591 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3592 1, NULL);
3593 if (!fsdev) {
3594 error_report("duplicate option: %s", "virtfs_synth");
3595 exit(1);
3597 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3599 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3600 &error_abort);
3601 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3602 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3603 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3604 break;
3606 case QEMU_OPTION_serial:
3607 add_device_config(DEV_SERIAL, optarg);
3608 default_serial = 0;
3609 if (strncmp(optarg, "mon:", 4) == 0) {
3610 default_monitor = 0;
3612 break;
3613 case QEMU_OPTION_watchdog:
3614 if (watchdog) {
3615 error_report("only one watchdog option may be given");
3616 return 1;
3618 watchdog = optarg;
3619 break;
3620 case QEMU_OPTION_watchdog_action:
3621 if (select_watchdog_action(optarg) == -1) {
3622 error_report("unknown -watchdog-action parameter");
3623 exit(1);
3625 break;
3626 case QEMU_OPTION_virtiocon:
3627 warn_report("This option is deprecated, "
3628 "use '-device virtconsole' instead");
3629 add_device_config(DEV_VIRTCON, optarg);
3630 default_virtcon = 0;
3631 if (strncmp(optarg, "mon:", 4) == 0) {
3632 default_monitor = 0;
3634 break;
3635 case QEMU_OPTION_parallel:
3636 add_device_config(DEV_PARALLEL, optarg);
3637 default_parallel = 0;
3638 if (strncmp(optarg, "mon:", 4) == 0) {
3639 default_monitor = 0;
3641 break;
3642 case QEMU_OPTION_debugcon:
3643 add_device_config(DEV_DEBUGCON, optarg);
3644 break;
3645 case QEMU_OPTION_loadvm:
3646 loadvm = optarg;
3647 break;
3648 case QEMU_OPTION_full_screen:
3649 dpy.has_full_screen = true;
3650 dpy.full_screen = true;
3651 break;
3652 case QEMU_OPTION_no_frame:
3653 g_printerr("The -no-frame switch is deprecated, and will be\n"
3654 "removed in a future release.\n");
3655 no_frame = 1;
3656 break;
3657 case QEMU_OPTION_alt_grab:
3658 alt_grab = 1;
3659 break;
3660 case QEMU_OPTION_ctrl_grab:
3661 ctrl_grab = 1;
3662 break;
3663 case QEMU_OPTION_no_quit:
3664 dpy.has_window_close = true;
3665 dpy.window_close = false;
3666 break;
3667 case QEMU_OPTION_sdl:
3668 #ifdef CONFIG_SDL
3669 dpy.type = DISPLAY_TYPE_SDL;
3670 break;
3671 #else
3672 error_report("SDL support is disabled");
3673 exit(1);
3674 #endif
3675 case QEMU_OPTION_pidfile:
3676 pid_file = optarg;
3677 break;
3678 case QEMU_OPTION_win2k_hack:
3679 win2k_install_hack = 1;
3680 break;
3681 case QEMU_OPTION_rtc_td_hack: {
3682 static GlobalProperty slew_lost_ticks = {
3683 .driver = "mc146818rtc",
3684 .property = "lost_tick_policy",
3685 .value = "slew",
3688 qdev_prop_register_global(&slew_lost_ticks);
3689 warn_report("This option is deprecated, "
3690 "use '-rtc driftfix=slew' instead.");
3691 break;
3693 case QEMU_OPTION_acpitable:
3694 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3695 optarg, true);
3696 if (!opts) {
3697 exit(1);
3699 acpi_table_add(opts, &error_fatal);
3700 break;
3701 case QEMU_OPTION_smbios:
3702 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3703 optarg, false);
3704 if (!opts) {
3705 exit(1);
3707 smbios_entry_add(opts, &error_fatal);
3708 break;
3709 case QEMU_OPTION_fwcfg:
3710 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3711 optarg, true);
3712 if (opts == NULL) {
3713 exit(1);
3715 break;
3716 case QEMU_OPTION_enable_kvm:
3717 olist = qemu_find_opts("machine");
3718 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3719 break;
3720 case QEMU_OPTION_enable_hax:
3721 olist = qemu_find_opts("machine");
3722 qemu_opts_parse_noisily(olist, "accel=hax", false);
3723 break;
3724 case QEMU_OPTION_M:
3725 case QEMU_OPTION_machine:
3726 olist = qemu_find_opts("machine");
3727 opts = qemu_opts_parse_noisily(olist, optarg, true);
3728 if (!opts) {
3729 exit(1);
3731 break;
3732 case QEMU_OPTION_no_kvm:
3733 olist = qemu_find_opts("machine");
3734 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3735 break;
3736 case QEMU_OPTION_accel:
3737 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3738 optarg, true);
3739 optarg = qemu_opt_get(accel_opts, "accel");
3740 if (!optarg || is_help_option(optarg)) {
3741 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3742 exit(0);
3744 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3745 false, &error_abort);
3746 qemu_opt_set(opts, "accel", optarg, &error_abort);
3747 break;
3748 case QEMU_OPTION_usb:
3749 olist = qemu_find_opts("machine");
3750 qemu_opts_parse_noisily(olist, "usb=on", false);
3751 break;
3752 case QEMU_OPTION_usbdevice:
3753 error_report("'-usbdevice' is deprecated, please use "
3754 "'-device usb-...' instead");
3755 olist = qemu_find_opts("machine");
3756 qemu_opts_parse_noisily(olist, "usb=on", false);
3757 add_device_config(DEV_USB, optarg);
3758 break;
3759 case QEMU_OPTION_device:
3760 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3761 optarg, true)) {
3762 exit(1);
3764 break;
3765 case QEMU_OPTION_smp:
3766 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3767 optarg, true)) {
3768 exit(1);
3770 break;
3771 case QEMU_OPTION_vnc:
3772 vnc_parse(optarg, &error_fatal);
3773 break;
3774 case QEMU_OPTION_no_acpi:
3775 acpi_enabled = 0;
3776 break;
3777 case QEMU_OPTION_no_hpet:
3778 no_hpet = 1;
3779 break;
3780 case QEMU_OPTION_balloon:
3781 if (balloon_parse(optarg) < 0) {
3782 error_report("unknown -balloon argument %s", optarg);
3783 exit(1);
3785 break;
3786 case QEMU_OPTION_no_reboot:
3787 no_reboot = 1;
3788 break;
3789 case QEMU_OPTION_no_shutdown:
3790 no_shutdown = 1;
3791 break;
3792 case QEMU_OPTION_show_cursor:
3793 cursor_hide = 0;
3794 break;
3795 case QEMU_OPTION_uuid:
3796 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3797 error_report("failed to parse UUID string: wrong format");
3798 exit(1);
3800 qemu_uuid_set = true;
3801 break;
3802 case QEMU_OPTION_option_rom:
3803 if (nb_option_roms >= MAX_OPTION_ROMS) {
3804 error_report("too many option ROMs");
3805 exit(1);
3807 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3808 optarg, true);
3809 if (!opts) {
3810 exit(1);
3812 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3813 option_rom[nb_option_roms].bootindex =
3814 qemu_opt_get_number(opts, "bootindex", -1);
3815 if (!option_rom[nb_option_roms].name) {
3816 error_report("Option ROM file is not specified");
3817 exit(1);
3819 nb_option_roms++;
3820 break;
3821 case QEMU_OPTION_semihosting:
3822 semihosting.enabled = true;
3823 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3824 break;
3825 case QEMU_OPTION_semihosting_config:
3826 semihosting.enabled = true;
3827 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3828 optarg, false);
3829 if (opts != NULL) {
3830 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3831 true);
3832 const char *target = qemu_opt_get(opts, "target");
3833 if (target != NULL) {
3834 if (strcmp("native", target) == 0) {
3835 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3836 } else if (strcmp("gdb", target) == 0) {
3837 semihosting.target = SEMIHOSTING_TARGET_GDB;
3838 } else if (strcmp("auto", target) == 0) {
3839 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3840 } else {
3841 error_report("unsupported semihosting-config %s",
3842 optarg);
3843 exit(1);
3845 } else {
3846 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3848 /* Set semihosting argument count and vector */
3849 qemu_opt_foreach(opts, add_semihosting_arg,
3850 &semihosting, NULL);
3851 } else {
3852 error_report("unsupported semihosting-config %s", optarg);
3853 exit(1);
3855 break;
3856 case QEMU_OPTION_name:
3857 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3858 optarg, true);
3859 if (!opts) {
3860 exit(1);
3862 break;
3863 case QEMU_OPTION_prom_env:
3864 if (nb_prom_envs >= MAX_PROM_ENVS) {
3865 error_report("too many prom variables");
3866 exit(1);
3868 prom_envs[nb_prom_envs] = optarg;
3869 nb_prom_envs++;
3870 break;
3871 case QEMU_OPTION_old_param:
3872 old_param = 1;
3873 break;
3874 case QEMU_OPTION_clock:
3875 /* Clock options no longer exist. Keep this option for
3876 * backward compatibility.
3878 break;
3879 case QEMU_OPTION_startdate:
3880 warn_report("This option is deprecated, use '-rtc base=' instead.");
3881 configure_rtc_date_offset(optarg, 1);
3882 break;
3883 case QEMU_OPTION_rtc:
3884 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3885 false);
3886 if (!opts) {
3887 exit(1);
3889 configure_rtc(opts);
3890 break;
3891 case QEMU_OPTION_tb_size:
3892 #ifndef CONFIG_TCG
3893 error_report("TCG is disabled");
3894 exit(1);
3895 #endif
3896 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3897 error_report("Invalid argument to -tb-size");
3898 exit(1);
3900 break;
3901 case QEMU_OPTION_icount:
3902 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3903 optarg, true);
3904 if (!icount_opts) {
3905 exit(1);
3907 break;
3908 case QEMU_OPTION_incoming:
3909 if (!incoming) {
3910 runstate_set(RUN_STATE_INMIGRATE);
3912 incoming = optarg;
3913 break;
3914 case QEMU_OPTION_only_migratable:
3916 * TODO: we can remove this option one day, and we
3917 * should all use:
3919 * "-global migration.only-migratable=true"
3921 qemu_global_option("migration.only-migratable=true");
3922 break;
3923 case QEMU_OPTION_nodefaults:
3924 has_defaults = 0;
3925 break;
3926 case QEMU_OPTION_xen_domid:
3927 if (!(xen_available())) {
3928 error_report("Option not supported for this target");
3929 exit(1);
3931 xen_domid = atoi(optarg);
3932 break;
3933 case QEMU_OPTION_xen_create:
3934 if (!(xen_available())) {
3935 error_report("Option not supported for this target");
3936 exit(1);
3938 xen_mode = XEN_CREATE;
3939 break;
3940 case QEMU_OPTION_xen_attach:
3941 if (!(xen_available())) {
3942 error_report("Option not supported for this target");
3943 exit(1);
3945 xen_mode = XEN_ATTACH;
3946 break;
3947 case QEMU_OPTION_xen_domid_restrict:
3948 if (!(xen_available())) {
3949 error_report("Option not supported for this target");
3950 exit(1);
3952 xen_domid_restrict = true;
3953 break;
3954 case QEMU_OPTION_trace:
3955 g_free(trace_file);
3956 trace_file = trace_opt_parse(optarg);
3957 break;
3958 case QEMU_OPTION_trace_unassigned:
3959 trace_unassigned = true;
3960 break;
3961 case QEMU_OPTION_readconfig:
3963 int ret = qemu_read_config_file(optarg);
3964 if (ret < 0) {
3965 error_report("read config %s: %s", optarg,
3966 strerror(-ret));
3967 exit(1);
3969 break;
3971 case QEMU_OPTION_spice:
3972 olist = qemu_find_opts("spice");
3973 if (!olist) {
3974 error_report("spice support is disabled");
3975 exit(1);
3977 opts = qemu_opts_parse_noisily(olist, optarg, false);
3978 if (!opts) {
3979 exit(1);
3981 display_remote++;
3982 break;
3983 case QEMU_OPTION_writeconfig:
3985 FILE *fp;
3986 if (strcmp(optarg, "-") == 0) {
3987 fp = stdout;
3988 } else {
3989 fp = fopen(optarg, "w");
3990 if (fp == NULL) {
3991 error_report("open %s: %s", optarg,
3992 strerror(errno));
3993 exit(1);
3996 qemu_config_write(fp);
3997 if (fp != stdout) {
3998 fclose(fp);
4000 break;
4002 case QEMU_OPTION_qtest:
4003 qtest_chrdev = optarg;
4004 break;
4005 case QEMU_OPTION_qtest_log:
4006 qtest_log = optarg;
4007 break;
4008 case QEMU_OPTION_sandbox:
4009 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4010 optarg, true);
4011 if (!opts) {
4012 exit(1);
4014 break;
4015 case QEMU_OPTION_add_fd:
4016 #ifndef _WIN32
4017 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4018 optarg, false);
4019 if (!opts) {
4020 exit(1);
4022 #else
4023 error_report("File descriptor passing is disabled on this "
4024 "platform");
4025 exit(1);
4026 #endif
4027 break;
4028 case QEMU_OPTION_object:
4029 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4030 optarg, true);
4031 if (!opts) {
4032 exit(1);
4034 break;
4035 case QEMU_OPTION_realtime:
4036 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4037 optarg, false);
4038 if (!opts) {
4039 exit(1);
4041 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4042 break;
4043 case QEMU_OPTION_msg:
4044 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4045 false);
4046 if (!opts) {
4047 exit(1);
4049 configure_msg(opts);
4050 break;
4051 case QEMU_OPTION_dump_vmstate:
4052 if (vmstate_dump_file) {
4053 error_report("only one '-dump-vmstate' "
4054 "option may be given");
4055 exit(1);
4057 vmstate_dump_file = fopen(optarg, "w");
4058 if (vmstate_dump_file == NULL) {
4059 error_report("open %s: %s", optarg, strerror(errno));
4060 exit(1);
4062 break;
4063 default:
4064 if (os_parse_cmd_args(popt->index, optarg)) {
4065 error_report("Option not supported in this build");
4066 exit(1);
4072 * Clear error location left behind by the loop.
4073 * Best done right after the loop. Do not insert code here!
4075 loc_set_none();
4077 replay_configure(icount_opts);
4079 machine_class = select_machine();
4081 set_memory_options(&ram_slots, &maxram_size, machine_class);
4083 os_daemonize();
4084 rcu_disable_atfork();
4086 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4087 error_report("could not acquire pid file: %s", strerror(errno));
4088 exit(1);
4091 if (qemu_init_main_loop(&main_loop_err)) {
4092 error_report_err(main_loop_err);
4093 exit(1);
4096 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4097 parse_sandbox, NULL, NULL)) {
4098 exit(1);
4101 if (qemu_opts_foreach(qemu_find_opts("name"),
4102 parse_name, NULL, NULL)) {
4103 exit(1);
4106 #ifndef _WIN32
4107 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4108 parse_add_fd, NULL, NULL)) {
4109 exit(1);
4112 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4113 cleanup_add_fd, NULL, NULL)) {
4114 exit(1);
4116 #endif
4118 current_machine = MACHINE(object_new(object_class_get_name(
4119 OBJECT_CLASS(machine_class))));
4120 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4121 exit(0);
4123 object_property_add_child(object_get_root(), "machine",
4124 OBJECT(current_machine), &error_abort);
4126 if (machine_class->minimum_page_bits) {
4127 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4128 /* This would be a board error: specifying a minimum smaller than
4129 * a target's compile-time fixed setting.
4131 g_assert_not_reached();
4135 cpu_exec_init_all();
4137 if (machine_class->hw_version) {
4138 qemu_set_hw_version(machine_class->hw_version);
4141 if (cpu_model && is_help_option(cpu_model)) {
4142 list_cpus(stdout, &fprintf, cpu_model);
4143 exit(0);
4146 if (!trace_init_backends()) {
4147 exit(1);
4149 trace_init_file(trace_file);
4151 /* Open the logfile at this point and set the log mask if necessary.
4153 if (log_file) {
4154 qemu_set_log_filename(log_file, &error_fatal);
4157 if (log_mask) {
4158 int mask;
4159 mask = qemu_str_to_log_mask(log_mask);
4160 if (!mask) {
4161 qemu_print_log_usage(stdout);
4162 exit(1);
4164 qemu_set_log(mask);
4165 } else {
4166 qemu_set_log(0);
4169 /* add configured firmware directories */
4170 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4171 for (i = 0; dirs[i] != NULL; i++) {
4172 qemu_add_data_dir(dirs[i]);
4174 g_strfreev(dirs);
4176 /* try to find datadir relative to the executable path */
4177 dir = os_find_datadir();
4178 qemu_add_data_dir(dir);
4179 g_free(dir);
4181 /* add the datadir specified when building */
4182 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4184 /* -L help lists the data directories and exits. */
4185 if (list_data_dirs) {
4186 for (i = 0; i < data_dir_idx; i++) {
4187 printf("%s\n", data_dir[i]);
4189 exit(0);
4192 /* machine_class: default to UP */
4193 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4194 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4195 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4197 /* default to machine_class->default_cpus */
4198 smp_cpus = machine_class->default_cpus;
4199 max_cpus = machine_class->default_cpus;
4201 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4203 /* sanity-check smp_cpus and max_cpus against machine_class */
4204 if (smp_cpus < machine_class->min_cpus) {
4205 error_report("Invalid SMP CPUs %d. The min CPUs "
4206 "supported by machine '%s' is %d", smp_cpus,
4207 machine_class->name, machine_class->min_cpus);
4208 exit(1);
4210 if (max_cpus > machine_class->max_cpus) {
4211 error_report("Invalid SMP CPUs %d. The max CPUs "
4212 "supported by machine '%s' is %d", max_cpus,
4213 machine_class->name, machine_class->max_cpus);
4214 exit(1);
4218 * Get the default machine options from the machine if it is not already
4219 * specified either by the configuration file or by the command line.
4221 if (machine_class->default_machine_opts) {
4222 qemu_opts_set_defaults(qemu_find_opts("machine"),
4223 machine_class->default_machine_opts, 0);
4226 qemu_opts_foreach(qemu_find_opts("device"),
4227 default_driver_check, NULL, NULL);
4228 qemu_opts_foreach(qemu_find_opts("global"),
4229 default_driver_check, NULL, NULL);
4231 if (!vga_model && !default_vga) {
4232 vga_interface_type = VGA_DEVICE;
4234 if (!has_defaults || machine_class->no_serial) {
4235 default_serial = 0;
4237 if (!has_defaults || machine_class->no_parallel) {
4238 default_parallel = 0;
4240 if (!has_defaults || !machine_class->use_virtcon) {
4241 default_virtcon = 0;
4243 if (!has_defaults || machine_class->no_floppy) {
4244 default_floppy = 0;
4246 if (!has_defaults || machine_class->no_cdrom) {
4247 default_cdrom = 0;
4249 if (!has_defaults || machine_class->no_sdcard) {
4250 default_sdcard = 0;
4252 if (!has_defaults) {
4253 default_monitor = 0;
4254 default_net = 0;
4255 default_vga = 0;
4258 if (is_daemonized()) {
4259 /* According to documentation and historically, -nographic redirects
4260 * serial port, parallel port and monitor to stdio, which does not work
4261 * with -daemonize. We can redirect these to null instead, but since
4262 * -nographic is legacy, let's just error out.
4263 * We disallow -nographic only if all other ports are not redirected
4264 * explicitly, to not break existing legacy setups which uses
4265 * -nographic _and_ redirects all ports explicitly - this is valid
4266 * usage, -nographic is just a no-op in this case.
4268 if (nographic
4269 && (default_parallel || default_serial
4270 || default_monitor || default_virtcon)) {
4271 error_report("-nographic cannot be used with -daemonize");
4272 exit(1);
4274 #ifdef CONFIG_CURSES
4275 if (dpy.type == DISPLAY_TYPE_CURSES) {
4276 error_report("curses display cannot be used with -daemonize");
4277 exit(1);
4279 #endif
4282 if (nographic) {
4283 if (default_parallel)
4284 add_device_config(DEV_PARALLEL, "null");
4285 if (default_serial && default_monitor) {
4286 add_device_config(DEV_SERIAL, "mon:stdio");
4287 } else if (default_virtcon && default_monitor) {
4288 add_device_config(DEV_VIRTCON, "mon:stdio");
4289 } else {
4290 if (default_serial)
4291 add_device_config(DEV_SERIAL, "stdio");
4292 if (default_virtcon)
4293 add_device_config(DEV_VIRTCON, "stdio");
4294 if (default_monitor)
4295 monitor_parse("stdio", "readline", false);
4297 } else {
4298 if (default_serial)
4299 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4300 if (default_parallel)
4301 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4302 if (default_monitor)
4303 monitor_parse("vc:80Cx24C", "readline", false);
4304 if (default_virtcon)
4305 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4308 #if defined(CONFIG_VNC)
4309 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4310 display_remote++;
4312 #endif
4313 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4314 if (!qemu_display_find_default(&dpy)) {
4315 dpy.type = DISPLAY_TYPE_NONE;
4316 #if defined(CONFIG_VNC)
4317 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4318 #endif
4321 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4322 dpy.type = DISPLAY_TYPE_NONE;
4325 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4326 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4327 "for SDL, ignoring option");
4329 if (dpy.has_window_close &&
4330 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4331 error_report("-no-quit is only valid for GTK and SDL, "
4332 "ignoring option");
4335 qemu_display_early_init(&dpy);
4336 qemu_console_early_init();
4338 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
4339 #if defined(CONFIG_OPENGL)
4340 error_report("OpenGL is not supported by the display");
4341 #else
4342 error_report("OpenGL support is disabled");
4343 #endif
4344 exit(1);
4347 page_size_init();
4348 socket_init();
4350 if (qemu_opts_foreach(qemu_find_opts("object"),
4351 user_creatable_add_opts_foreach,
4352 object_create_initial, NULL)) {
4353 exit(1);
4356 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4357 chardev_init_func, NULL, NULL)) {
4358 exit(1);
4361 #ifdef CONFIG_VIRTFS
4362 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4363 fsdev_init_func, NULL, NULL)) {
4364 exit(1);
4366 #endif
4368 if (qemu_opts_foreach(qemu_find_opts("device"),
4369 device_help_func, NULL, NULL)) {
4370 exit(0);
4373 machine_opts = qemu_get_machine_opts();
4374 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4375 NULL)) {
4376 object_unref(OBJECT(current_machine));
4377 exit(1);
4380 configure_accelerator(current_machine);
4383 * Register all the global properties, including accel properties,
4384 * machine properties, and user-specified ones.
4386 register_global_properties(current_machine);
4389 * Migration object can only be created after global properties
4390 * are applied correctly.
4392 migration_object_init();
4394 if (qtest_chrdev) {
4395 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4398 machine_opts = qemu_get_machine_opts();
4399 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4400 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4401 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4402 bios_name = qemu_opt_get(machine_opts, "firmware");
4404 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4405 if (opts) {
4406 boot_order = qemu_opt_get(opts, "order");
4407 if (boot_order) {
4408 validate_bootdevices(boot_order, &error_fatal);
4411 boot_once = qemu_opt_get(opts, "once");
4412 if (boot_once) {
4413 validate_bootdevices(boot_once, &error_fatal);
4416 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4417 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4420 if (!boot_order) {
4421 boot_order = machine_class->default_boot_order;
4424 if (!kernel_cmdline) {
4425 kernel_cmdline = "";
4426 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4429 linux_boot = (kernel_filename != NULL);
4431 if (!linux_boot && *kernel_cmdline != '\0') {
4432 error_report("-append only allowed with -kernel option");
4433 exit(1);
4436 if (!linux_boot && initrd_filename != NULL) {
4437 error_report("-initrd only allowed with -kernel option");
4438 exit(1);
4441 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4442 /* fall back to the -kernel/-append */
4443 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4446 os_set_line_buffering();
4448 /* spice needs the timers to be initialized by this point */
4449 qemu_spice_init();
4451 cpu_ticks_init();
4452 if (icount_opts) {
4453 if (!tcg_enabled()) {
4454 error_report("-icount is not allowed with hardware virtualization");
4455 exit(1);
4457 configure_icount(icount_opts, &error_abort);
4458 qemu_opts_del(icount_opts);
4461 if (tcg_enabled()) {
4462 qemu_tcg_configure(accel_opts, &error_fatal);
4465 if (default_net) {
4466 QemuOptsList *net = qemu_find_opts("net");
4467 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4468 #ifdef CONFIG_SLIRP
4469 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4470 #endif
4473 colo_info_init();
4475 if (net_init_clients(&err) < 0) {
4476 error_report_err(err);
4477 exit(1);
4480 if (qemu_opts_foreach(qemu_find_opts("object"),
4481 user_creatable_add_opts_foreach,
4482 object_create_delayed, NULL)) {
4483 exit(1);
4486 if (tpm_init() < 0) {
4487 exit(1);
4490 /* init the bluetooth world */
4491 if (foreach_device_config(DEV_BT, bt_parse))
4492 exit(1);
4494 if (!xen_enabled()) {
4495 /* On 32-bit hosts, QEMU is limited by virtual address space */
4496 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4497 error_report("at most 2047 MB RAM can be simulated");
4498 exit(1);
4502 blk_mig_init();
4503 ram_mig_init();
4504 dirty_bitmap_mig_init();
4506 /* If the currently selected machine wishes to override the units-per-bus
4507 * property of its default HBA interface type, do so now. */
4508 if (machine_class->units_per_default_bus) {
4509 override_max_devs(machine_class->block_default_type,
4510 machine_class->units_per_default_bus);
4513 /* open the virtual block devices */
4514 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4515 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4517 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4518 loc_push_restore(&bdo->loc);
4519 qmp_blockdev_add(bdo->bdo, &error_fatal);
4520 loc_pop(&bdo->loc);
4521 qapi_free_BlockdevOptions(bdo->bdo);
4522 g_free(bdo);
4524 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4525 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4526 NULL, NULL);
4528 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4529 &machine_class->block_default_type, NULL)) {
4530 exit(1);
4533 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4534 CDROM_OPTS);
4535 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4536 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4539 * Note: qtest_enabled() (which is used in monitor_qapi_event_init())
4540 * depends on configure_accelerator() above.
4542 monitor_init_globals();
4544 if (qemu_opts_foreach(qemu_find_opts("mon"),
4545 mon_init_func, NULL, NULL)) {
4546 exit(1);
4549 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4550 exit(1);
4551 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4552 exit(1);
4553 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4554 exit(1);
4555 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4556 exit(1);
4558 /* If no default VGA is requested, the default is "none". */
4559 if (default_vga) {
4560 if (machine_class->default_display) {
4561 vga_model = machine_class->default_display;
4562 } else if (vga_interface_available(VGA_CIRRUS)) {
4563 vga_model = "cirrus";
4564 } else if (vga_interface_available(VGA_STD)) {
4565 vga_model = "std";
4568 if (vga_model) {
4569 select_vgahw(vga_model);
4572 if (watchdog) {
4573 i = select_watchdog(watchdog);
4574 if (i > 0)
4575 exit (i == 1 ? 1 : 0);
4578 /* This checkpoint is required by replay to separate prior clock
4579 reading from the other reads, because timer polling functions query
4580 clock values from the log. */
4581 replay_checkpoint(CHECKPOINT_INIT);
4582 qdev_machine_init();
4584 current_machine->ram_size = ram_size;
4585 current_machine->maxram_size = maxram_size;
4586 current_machine->ram_slots = ram_slots;
4587 current_machine->boot_order = boot_order;
4589 /* parse features once if machine provides default cpu_type */
4590 current_machine->cpu_type = machine_class->default_cpu_type;
4591 if (cpu_model) {
4592 current_machine->cpu_type = parse_cpu_model(cpu_model);
4594 parse_numa_opts(current_machine);
4596 machine_run_board_init(current_machine);
4598 realtime_init();
4600 soundhw_init();
4602 if (hax_enabled()) {
4603 hax_sync_vcpus();
4606 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4607 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4608 exit(1);
4611 /* init USB devices */
4612 if (machine_usb(current_machine)) {
4613 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4614 exit(1);
4617 /* Check if IGD GFX passthrough. */
4618 igd_gfx_passthru();
4620 /* init generic devices */
4621 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4622 if (qemu_opts_foreach(qemu_find_opts("device"),
4623 device_init_func, NULL, NULL)) {
4624 exit(1);
4627 cpu_synchronize_all_post_init();
4629 rom_reset_order_override();
4631 /* Did we create any drives that we failed to create a device for? */
4632 drive_check_orphaned();
4634 /* Don't warn about the default network setup that you get if
4635 * no command line -net or -netdev options are specified. There
4636 * are two cases that we would otherwise complain about:
4637 * (1) board doesn't support a NIC but the implicit "-net nic"
4638 * requested one
4639 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4640 * sets up a nic that isn't connected to anything.
4642 if (!default_net) {
4643 net_check_clients();
4647 if (boot_once) {
4648 qemu_boot_set(boot_once, &error_fatal);
4649 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4652 /* init local displays */
4653 ds = init_displaystate();
4654 qemu_display_init(ds, &dpy);
4656 /* must be after terminal init, SDL library changes signal handlers */
4657 os_setup_signal_handling();
4659 /* init remote displays */
4660 #ifdef CONFIG_VNC
4661 qemu_opts_foreach(qemu_find_opts("vnc"),
4662 vnc_init_func, NULL, NULL);
4663 #endif
4665 if (using_spice) {
4666 qemu_spice_display_init();
4669 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4670 exit(1);
4673 qdev_machine_creation_done();
4675 /* TODO: once all bus devices are qdevified, this should be done
4676 * when bus is created by qdev.c */
4677 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4678 qemu_run_machine_init_done_notifiers();
4680 if (rom_check_and_register_reset() != 0) {
4681 error_report("rom check and register reset failed");
4682 exit(1);
4685 replay_start();
4687 /* This checkpoint is required by replay to separate prior clock
4688 reading from the other reads, because timer polling functions query
4689 clock values from the log. */
4690 replay_checkpoint(CHECKPOINT_RESET);
4691 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4692 register_global_state();
4693 if (replay_mode != REPLAY_MODE_NONE) {
4694 replay_vmstate_init();
4695 } else if (loadvm) {
4696 Error *local_err = NULL;
4697 if (load_snapshot(loadvm, &local_err) < 0) {
4698 error_report_err(local_err);
4699 autostart = 0;
4703 qdev_prop_check_globals();
4704 if (vmstate_dump_file) {
4705 /* dump and exit */
4706 dump_vmstate_json_to_file(vmstate_dump_file);
4707 return 0;
4710 if (incoming) {
4711 Error *local_err = NULL;
4712 qemu_start_incoming_migration(incoming, &local_err);
4713 if (local_err) {
4714 error_reportf_err(local_err, "-incoming %s: ", incoming);
4715 exit(1);
4717 } else if (autostart) {
4718 vm_start();
4721 accel_setup_post(current_machine);
4722 os_setup_post();
4724 main_loop();
4726 gdbserver_cleanup();
4728 /* No more vcpu or device emulation activity beyond this point */
4729 vm_shutdown();
4731 bdrv_close_all();
4733 res_free();
4735 /* vhost-user must be cleaned up before chardevs. */
4736 tpm_cleanup();
4737 net_cleanup();
4738 audio_cleanup();
4739 monitor_cleanup();
4740 qemu_chr_cleanup();
4741 user_creatable_cleanup();
4742 migration_object_finalize();
4743 /* TODO: unref root container, check all devices are ok */
4745 return 0;