Merge remote-tracking branch 'remotes/dgibson/tags/ppc-for-2.10-20170731' into staging
[qemu.git] / vl.c
blobfb6b2efafa83b87a7b05166e5433325e57b16e10
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.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #endif
34 #if defined(CONFIG_VDE)
35 #include <libvdeplug.h>
36 #endif
38 #ifdef CONFIG_SDL
39 #if defined(__APPLE__) || defined(main)
40 #include <SDL.h>
41 int qemu_main(int argc, char **argv, char **envp);
42 int main(int argc, char **argv)
44 return qemu_main(argc, argv, NULL);
46 #undef main
47 #define main qemu_main
48 #endif
49 #endif /* CONFIG_SDL */
51 #ifdef CONFIG_COCOA
52 #undef main
53 #define main qemu_main
54 #endif /* CONFIG_COCOA */
57 #include "qemu/error-report.h"
58 #include "qemu/sockets.h"
59 #include "hw/hw.h"
60 #include "hw/boards.h"
61 #include "sysemu/accel.h"
62 #include "hw/usb.h"
63 #include "hw/i386/pc.h"
64 #include "hw/isa/isa.h"
65 #include "hw/scsi/scsi.h"
66 #include "hw/bt.h"
67 #include "sysemu/watchdog.h"
68 #include "hw/smbios/smbios.h"
69 #include "hw/acpi/acpi.h"
70 #include "hw/xen/xen.h"
71 #include "hw/qdev.h"
72 #include "hw/loader.h"
73 #include "monitor/qdev.h"
74 #include "sysemu/bt.h"
75 #include "net/net.h"
76 #include "net/slirp.h"
77 #include "monitor/monitor.h"
78 #include "ui/console.h"
79 #include "ui/input.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "chardev/char.h"
85 #include "qemu/bitmap.h"
86 #include "qemu/log.h"
87 #include "sysemu/blockdev.h"
88 #include "hw/block/block.h"
89 #include "migration/misc.h"
90 #include "migration/snapshot.h"
91 #include "migration/global_state.h"
92 #include "sysemu/tpm.h"
93 #include "sysemu/dma.h"
94 #include "hw/audio/soundhw.h"
95 #include "audio/audio.h"
96 #include "sysemu/cpus.h"
97 #include "migration/colo.h"
98 #include "sysemu/kvm.h"
99 #include "sysemu/hax.h"
100 #include "qapi/qobject-input-visitor.h"
101 #include "qapi/qobject-input-visitor.h"
102 #include "qapi-visit.h"
103 #include "qapi/qmp/qjson.h"
104 #include "qemu/option.h"
105 #include "qemu/config-file.h"
106 #include "qemu-options.h"
107 #include "qmp-commands.h"
108 #include "qemu/main-loop.h"
109 #ifdef CONFIG_VIRTFS
110 #include "fsdev/qemu-fsdev.h"
111 #endif
112 #include "sysemu/qtest.h"
114 #include "disas/disas.h"
117 #include "slirp/libslirp.h"
119 #include "trace-root.h"
120 #include "trace/control.h"
121 #include "qemu/queue.h"
122 #include "sysemu/arch_init.h"
124 #include "ui/qemu-spice.h"
125 #include "qapi/string-input-visitor.h"
126 #include "qapi/opts-visitor.h"
127 #include "qom/object_interfaces.h"
128 #include "qapi-event.h"
129 #include "exec/semihost.h"
130 #include "crypto/init.h"
131 #include "sysemu/replay.h"
132 #include "qapi/qmp/qerror.h"
133 #include "sysemu/iothread.h"
135 #define MAX_VIRTIO_CONSOLES 1
136 #define MAX_SCLP_CONSOLES 1
138 static const char *data_dir[16];
139 static int data_dir_idx;
140 const char *bios_name = NULL;
141 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
142 int request_opengl = -1;
143 int display_opengl;
144 const char* keyboard_layout = NULL;
145 ram_addr_t ram_size;
146 const char *mem_path = NULL;
147 int mem_prealloc = 0; /* force preallocation of physical target memory */
148 bool enable_mlock = false;
149 int nb_nics;
150 NICInfo nd_table[MAX_NICS];
151 int autostart;
152 static int rtc_utc = 1;
153 static int rtc_date_offset = -1; /* -1 means no change */
154 QEMUClockType rtc_clock;
155 int vga_interface_type = VGA_NONE;
156 static int full_screen = 0;
157 static int no_frame = 0;
158 int no_quit = 0;
159 static bool grab_on_hover;
160 Chardev *serial_hds[MAX_SERIAL_PORTS];
161 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
162 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
163 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
164 int win2k_install_hack = 0;
165 int singlestep = 0;
166 int smp_cpus = 1;
167 int max_cpus = 1;
168 int smp_cores = 1;
169 int smp_threads = 1;
170 int acpi_enabled = 1;
171 int no_hpet = 0;
172 int fd_bootchk = 1;
173 static int no_reboot;
174 int no_shutdown = 0;
175 int cursor_hide = 1;
176 int graphic_rotate = 0;
177 const char *watchdog;
178 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
179 int nb_option_roms;
180 int old_param = 0;
181 const char *qemu_name;
182 int alt_grab = 0;
183 int ctrl_grab = 0;
184 unsigned int nb_prom_envs = 0;
185 const char *prom_envs[MAX_PROM_ENVS];
186 int boot_menu;
187 bool boot_strict;
188 uint8_t *boot_splash_filedata;
189 size_t boot_splash_filedata_size;
190 uint8_t qemu_extra_params_fw[2];
192 int icount_align_option;
194 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
195 * little-endian "wire format" described in the SMBIOS 2.6 specification.
197 QemuUUID qemu_uuid;
198 bool qemu_uuid_set;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 bool xen_allowed;
207 uint32_t xen_domid;
208 enum xen_mode xen_mode = XEN_EMULATE;
209 bool xen_domid_restrict;
211 static int has_defaults = 1;
212 static int default_serial = 1;
213 static int default_parallel = 1;
214 static int default_virtcon = 1;
215 static int default_sclp = 1;
216 static int default_monitor = 1;
217 static int default_floppy = 1;
218 static int default_cdrom = 1;
219 static int default_sdcard = 1;
220 static int default_vga = 1;
221 static int default_net = 1;
223 static struct {
224 const char *driver;
225 int *flag;
226 } default_list[] = {
227 { .driver = "isa-serial", .flag = &default_serial },
228 { .driver = "isa-parallel", .flag = &default_parallel },
229 { .driver = "isa-fdc", .flag = &default_floppy },
230 { .driver = "floppy", .flag = &default_floppy },
231 { .driver = "ide-cd", .flag = &default_cdrom },
232 { .driver = "ide-hd", .flag = &default_cdrom },
233 { .driver = "ide-drive", .flag = &default_cdrom },
234 { .driver = "scsi-cd", .flag = &default_cdrom },
235 { .driver = "scsi-hd", .flag = &default_cdrom },
236 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
237 { .driver = "virtio-serial", .flag = &default_virtcon },
238 { .driver = "VGA", .flag = &default_vga },
239 { .driver = "isa-vga", .flag = &default_vga },
240 { .driver = "cirrus-vga", .flag = &default_vga },
241 { .driver = "isa-cirrus-vga", .flag = &default_vga },
242 { .driver = "vmware-svga", .flag = &default_vga },
243 { .driver = "qxl-vga", .flag = &default_vga },
244 { .driver = "virtio-vga", .flag = &default_vga },
247 static QemuOptsList qemu_rtc_opts = {
248 .name = "rtc",
249 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
250 .desc = {
252 .name = "base",
253 .type = QEMU_OPT_STRING,
255 .name = "clock",
256 .type = QEMU_OPT_STRING,
258 .name = "driftfix",
259 .type = QEMU_OPT_STRING,
261 { /* end of list */ }
265 static QemuOptsList qemu_sandbox_opts = {
266 .name = "sandbox",
267 .implied_opt_name = "enable",
268 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
269 .desc = {
271 .name = "enable",
272 .type = QEMU_OPT_BOOL,
274 { /* end of list */ }
278 static QemuOptsList qemu_option_rom_opts = {
279 .name = "option-rom",
280 .implied_opt_name = "romfile",
281 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
282 .desc = {
284 .name = "bootindex",
285 .type = QEMU_OPT_NUMBER,
286 }, {
287 .name = "romfile",
288 .type = QEMU_OPT_STRING,
290 { /* end of list */ }
294 static QemuOptsList qemu_machine_opts = {
295 .name = "machine",
296 .implied_opt_name = "type",
297 .merge_lists = true,
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
299 .desc = {
301 * no elements => accept any
302 * sanity checking will happen later
303 * when setting machine properties
309 static QemuOptsList qemu_accel_opts = {
310 .name = "accel",
311 .implied_opt_name = "accel",
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
313 .merge_lists = true,
314 .desc = {
316 .name = "accel",
317 .type = QEMU_OPT_STRING,
318 .help = "Select the type of accelerator",
321 .name = "thread",
322 .type = QEMU_OPT_STRING,
323 .help = "Enable/disable multi-threaded TCG",
325 { /* end of list */ }
329 static QemuOptsList qemu_boot_opts = {
330 .name = "boot-opts",
331 .implied_opt_name = "order",
332 .merge_lists = true,
333 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
334 .desc = {
336 .name = "order",
337 .type = QEMU_OPT_STRING,
338 }, {
339 .name = "once",
340 .type = QEMU_OPT_STRING,
341 }, {
342 .name = "menu",
343 .type = QEMU_OPT_BOOL,
344 }, {
345 .name = "splash",
346 .type = QEMU_OPT_STRING,
347 }, {
348 .name = "splash-time",
349 .type = QEMU_OPT_STRING,
350 }, {
351 .name = "reboot-timeout",
352 .type = QEMU_OPT_STRING,
353 }, {
354 .name = "strict",
355 .type = QEMU_OPT_BOOL,
357 { /*End of list */ }
361 static QemuOptsList qemu_add_fd_opts = {
362 .name = "add-fd",
363 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
364 .desc = {
366 .name = "fd",
367 .type = QEMU_OPT_NUMBER,
368 .help = "file descriptor of which a duplicate is added to fd set",
370 .name = "set",
371 .type = QEMU_OPT_NUMBER,
372 .help = "ID of the fd set to add fd to",
374 .name = "opaque",
375 .type = QEMU_OPT_STRING,
376 .help = "free-form string used to describe fd",
378 { /* end of list */ }
382 static QemuOptsList qemu_object_opts = {
383 .name = "object",
384 .implied_opt_name = "qom-type",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
386 .desc = {
391 static QemuOptsList qemu_tpmdev_opts = {
392 .name = "tpmdev",
393 .implied_opt_name = "type",
394 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
395 .desc = {
396 /* options are defined in the TPM backends */
397 { /* end of list */ }
401 static QemuOptsList qemu_realtime_opts = {
402 .name = "realtime",
403 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
404 .desc = {
406 .name = "mlock",
407 .type = QEMU_OPT_BOOL,
409 { /* end of list */ }
413 static QemuOptsList qemu_msg_opts = {
414 .name = "msg",
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
416 .desc = {
418 .name = "timestamp",
419 .type = QEMU_OPT_BOOL,
421 { /* end of list */ }
425 static QemuOptsList qemu_name_opts = {
426 .name = "name",
427 .implied_opt_name = "guest",
428 .merge_lists = true,
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
430 .desc = {
432 .name = "guest",
433 .type = QEMU_OPT_STRING,
434 .help = "Sets the name of the guest.\n"
435 "This name will be displayed in the SDL window caption.\n"
436 "The name will also be used for the VNC server",
437 }, {
438 .name = "process",
439 .type = QEMU_OPT_STRING,
440 .help = "Sets the name of the QEMU process, as shown in top etc",
441 }, {
442 .name = "debug-threads",
443 .type = QEMU_OPT_BOOL,
444 .help = "When enabled, name the individual threads; defaults off.\n"
445 "NOTE: The thread names are for debugging and not a\n"
446 "stable API.",
448 { /* End of list */ }
452 static QemuOptsList qemu_mem_opts = {
453 .name = "memory",
454 .implied_opt_name = "size",
455 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
456 .merge_lists = true,
457 .desc = {
459 .name = "size",
460 .type = QEMU_OPT_SIZE,
463 .name = "slots",
464 .type = QEMU_OPT_NUMBER,
467 .name = "maxmem",
468 .type = QEMU_OPT_SIZE,
470 { /* end of list */ }
474 static QemuOptsList qemu_icount_opts = {
475 .name = "icount",
476 .implied_opt_name = "shift",
477 .merge_lists = true,
478 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
479 .desc = {
481 .name = "shift",
482 .type = QEMU_OPT_STRING,
483 }, {
484 .name = "align",
485 .type = QEMU_OPT_BOOL,
486 }, {
487 .name = "sleep",
488 .type = QEMU_OPT_BOOL,
489 }, {
490 .name = "rr",
491 .type = QEMU_OPT_STRING,
492 }, {
493 .name = "rrfile",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "rrsnapshot",
497 .type = QEMU_OPT_STRING,
499 { /* end of list */ }
503 static QemuOptsList qemu_semihosting_config_opts = {
504 .name = "semihosting-config",
505 .implied_opt_name = "enable",
506 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
507 .desc = {
509 .name = "enable",
510 .type = QEMU_OPT_BOOL,
511 }, {
512 .name = "target",
513 .type = QEMU_OPT_STRING,
514 }, {
515 .name = "arg",
516 .type = QEMU_OPT_STRING,
518 { /* end of list */ }
522 static QemuOptsList qemu_fw_cfg_opts = {
523 .name = "fw_cfg",
524 .implied_opt_name = "name",
525 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
526 .desc = {
528 .name = "name",
529 .type = QEMU_OPT_STRING,
530 .help = "Sets the fw_cfg name of the blob to be inserted",
531 }, {
532 .name = "file",
533 .type = QEMU_OPT_STRING,
534 .help = "Sets the name of the file from which\n"
535 "the fw_cfg blob will be loaded",
536 }, {
537 .name = "string",
538 .type = QEMU_OPT_STRING,
539 .help = "Sets content of the blob to be inserted from a string",
541 { /* end of list */ }
546 * Get machine options
548 * Returns: machine options (never null).
550 QemuOpts *qemu_get_machine_opts(void)
552 return qemu_find_opts_singleton("machine");
555 const char *qemu_get_vm_name(void)
557 return qemu_name;
560 static void res_free(void)
562 g_free(boot_splash_filedata);
563 boot_splash_filedata = NULL;
566 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
568 const char *driver = qemu_opt_get(opts, "driver");
569 int i;
571 if (!driver)
572 return 0;
573 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
574 if (strcmp(default_list[i].driver, driver) != 0)
575 continue;
576 *(default_list[i].flag) = 0;
578 return 0;
581 /***********************************************************/
582 /* QEMU state */
584 static RunState current_run_state = RUN_STATE_PRELAUNCH;
586 /* We use RUN_STATE__MAX but any invalid value will do */
587 static RunState vmstop_requested = RUN_STATE__MAX;
588 static QemuMutex vmstop_lock;
590 typedef struct {
591 RunState from;
592 RunState to;
593 } RunStateTransition;
595 static const RunStateTransition runstate_transitions_def[] = {
596 /* from -> to */
597 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
598 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
601 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
602 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
603 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
604 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
605 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
606 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
607 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
608 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
609 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
610 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
612 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
614 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
615 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
616 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
618 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
619 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
622 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
623 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
624 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
625 { RUN_STATE_PAUSED, RUN_STATE_COLO},
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
629 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
635 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
640 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
643 { RUN_STATE_COLO, RUN_STATE_RUNNING },
645 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
646 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
648 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
649 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
650 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
652 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
653 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
654 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
655 { RUN_STATE_RUNNING, RUN_STATE_COLO},
657 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
659 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
660 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
661 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
664 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
665 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
666 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
667 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
670 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
671 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
672 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
679 { RUN_STATE__MAX, RUN_STATE__MAX },
682 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
684 bool runstate_check(RunState state)
686 return current_run_state == state;
689 bool runstate_store(char *str, size_t size)
691 const char *state = RunState_lookup[current_run_state];
692 size_t len = strlen(state) + 1;
694 if (len > size) {
695 return false;
697 memcpy(str, state, len);
698 return true;
701 static void runstate_init(void)
703 const RunStateTransition *p;
705 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
706 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
707 runstate_valid_transitions[p->from][p->to] = true;
710 qemu_mutex_init(&vmstop_lock);
713 /* This function will abort() on invalid state transitions */
714 void runstate_set(RunState new_state)
716 assert(new_state < RUN_STATE__MAX);
718 if (current_run_state == new_state) {
719 return;
722 if (!runstate_valid_transitions[current_run_state][new_state]) {
723 error_report("invalid runstate transition: '%s' -> '%s'",
724 RunState_lookup[current_run_state],
725 RunState_lookup[new_state]);
726 abort();
728 trace_runstate_set(new_state);
729 current_run_state = new_state;
732 int runstate_is_running(void)
734 return runstate_check(RUN_STATE_RUNNING);
737 bool runstate_needs_reset(void)
739 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
740 runstate_check(RUN_STATE_SHUTDOWN);
743 StatusInfo *qmp_query_status(Error **errp)
745 StatusInfo *info = g_malloc0(sizeof(*info));
747 info->running = runstate_is_running();
748 info->singlestep = singlestep;
749 info->status = current_run_state;
751 return info;
754 bool qemu_vmstop_requested(RunState *r)
756 qemu_mutex_lock(&vmstop_lock);
757 *r = vmstop_requested;
758 vmstop_requested = RUN_STATE__MAX;
759 qemu_mutex_unlock(&vmstop_lock);
760 return *r < RUN_STATE__MAX;
763 void qemu_system_vmstop_request_prepare(void)
765 qemu_mutex_lock(&vmstop_lock);
768 void qemu_system_vmstop_request(RunState state)
770 vmstop_requested = state;
771 qemu_mutex_unlock(&vmstop_lock);
772 qemu_notify_event();
775 /***********************************************************/
776 /* real time host monotonic timer */
778 static time_t qemu_time(void)
780 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
783 /***********************************************************/
784 /* host time/date access */
785 void qemu_get_timedate(struct tm *tm, int offset)
787 time_t ti = qemu_time();
789 ti += offset;
790 if (rtc_date_offset == -1) {
791 if (rtc_utc)
792 gmtime_r(&ti, tm);
793 else
794 localtime_r(&ti, tm);
795 } else {
796 ti -= rtc_date_offset;
797 gmtime_r(&ti, tm);
801 int qemu_timedate_diff(struct tm *tm)
803 time_t seconds;
805 if (rtc_date_offset == -1)
806 if (rtc_utc)
807 seconds = mktimegm(tm);
808 else {
809 struct tm tmp = *tm;
810 tmp.tm_isdst = -1; /* use timezone to figure it out */
811 seconds = mktime(&tmp);
813 else
814 seconds = mktimegm(tm) + rtc_date_offset;
816 return seconds - qemu_time();
819 static void configure_rtc_date_offset(const char *startdate, int legacy)
821 time_t rtc_start_date;
822 struct tm tm;
824 if (!strcmp(startdate, "now") && legacy) {
825 rtc_date_offset = -1;
826 } else {
827 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
828 &tm.tm_year,
829 &tm.tm_mon,
830 &tm.tm_mday,
831 &tm.tm_hour,
832 &tm.tm_min,
833 &tm.tm_sec) == 6) {
834 /* OK */
835 } else if (sscanf(startdate, "%d-%d-%d",
836 &tm.tm_year,
837 &tm.tm_mon,
838 &tm.tm_mday) == 3) {
839 tm.tm_hour = 0;
840 tm.tm_min = 0;
841 tm.tm_sec = 0;
842 } else {
843 goto date_fail;
845 tm.tm_year -= 1900;
846 tm.tm_mon--;
847 rtc_start_date = mktimegm(&tm);
848 if (rtc_start_date == -1) {
849 date_fail:
850 error_report("invalid date format");
851 error_printf("valid formats: "
852 "'2006-06-17T16:01:21' or '2006-06-17'\n");
853 exit(1);
855 rtc_date_offset = qemu_time() - rtc_start_date;
859 static void configure_rtc(QemuOpts *opts)
861 const char *value;
863 value = qemu_opt_get(opts, "base");
864 if (value) {
865 if (!strcmp(value, "utc")) {
866 rtc_utc = 1;
867 } else if (!strcmp(value, "localtime")) {
868 Error *blocker = NULL;
869 rtc_utc = 0;
870 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
871 "-rtc base=localtime");
872 replay_add_blocker(blocker);
873 } else {
874 configure_rtc_date_offset(value, 0);
877 value = qemu_opt_get(opts, "clock");
878 if (value) {
879 if (!strcmp(value, "host")) {
880 rtc_clock = QEMU_CLOCK_HOST;
881 } else if (!strcmp(value, "rt")) {
882 rtc_clock = QEMU_CLOCK_REALTIME;
883 } else if (!strcmp(value, "vm")) {
884 rtc_clock = QEMU_CLOCK_VIRTUAL;
885 } else {
886 error_report("invalid option value '%s'", value);
887 exit(1);
890 value = qemu_opt_get(opts, "driftfix");
891 if (value) {
892 if (!strcmp(value, "slew")) {
893 static GlobalProperty slew_lost_ticks = {
894 .driver = "mc146818rtc",
895 .property = "lost_tick_policy",
896 .value = "slew",
899 qdev_prop_register_global(&slew_lost_ticks);
900 } else if (!strcmp(value, "none")) {
901 /* discard is default */
902 } else {
903 error_report("invalid option value '%s'", value);
904 exit(1);
909 /***********************************************************/
910 /* Bluetooth support */
911 static int nb_hcis;
912 static int cur_hci;
913 static struct HCIInfo *hci_table[MAX_NICS];
915 struct HCIInfo *qemu_next_hci(void)
917 if (cur_hci == nb_hcis)
918 return &null_hci;
920 return hci_table[cur_hci++];
923 static int bt_hci_parse(const char *str)
925 struct HCIInfo *hci;
926 bdaddr_t bdaddr;
928 if (nb_hcis >= MAX_NICS) {
929 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
930 return -1;
933 hci = hci_init(str);
934 if (!hci)
935 return -1;
937 bdaddr.b[0] = 0x52;
938 bdaddr.b[1] = 0x54;
939 bdaddr.b[2] = 0x00;
940 bdaddr.b[3] = 0x12;
941 bdaddr.b[4] = 0x34;
942 bdaddr.b[5] = 0x56 + nb_hcis;
943 hci->bdaddr_set(hci, bdaddr.b);
945 hci_table[nb_hcis++] = hci;
947 return 0;
950 static void bt_vhci_add(int vlan_id)
952 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
954 if (!vlan->slave)
955 warn_report("adding a VHCI to an empty scatternet %i",
956 vlan_id);
958 bt_vhci_init(bt_new_hci(vlan));
961 static struct bt_device_s *bt_device_add(const char *opt)
963 struct bt_scatternet_s *vlan;
964 int vlan_id = 0;
965 char *endp = strstr(opt, ",vlan=");
966 int len = (endp ? endp - opt : strlen(opt)) + 1;
967 char devname[10];
969 pstrcpy(devname, MIN(sizeof(devname), len), opt);
971 if (endp) {
972 vlan_id = strtol(endp + 6, &endp, 0);
973 if (*endp) {
974 error_report("unrecognised bluetooth vlan Id");
975 return 0;
979 vlan = qemu_find_bt_vlan(vlan_id);
981 if (!vlan->slave)
982 warn_report("adding a slave device to an empty scatternet %i",
983 vlan_id);
985 if (!strcmp(devname, "keyboard"))
986 return bt_keyboard_init(vlan);
988 error_report("unsupported bluetooth device '%s'", devname);
989 return 0;
992 static int bt_parse(const char *opt)
994 const char *endp, *p;
995 int vlan;
997 if (strstart(opt, "hci", &endp)) {
998 if (!*endp || *endp == ',') {
999 if (*endp)
1000 if (!strstart(endp, ",vlan=", 0))
1001 opt = endp + 1;
1003 return bt_hci_parse(opt);
1005 } else if (strstart(opt, "vhci", &endp)) {
1006 if (!*endp || *endp == ',') {
1007 if (*endp) {
1008 if (strstart(endp, ",vlan=", &p)) {
1009 vlan = strtol(p, (char **) &endp, 0);
1010 if (*endp) {
1011 error_report("bad scatternet '%s'", p);
1012 return 1;
1014 } else {
1015 error_report("bad parameter '%s'", endp + 1);
1016 return 1;
1018 } else
1019 vlan = 0;
1021 bt_vhci_add(vlan);
1022 return 0;
1024 } else if (strstart(opt, "device:", &endp))
1025 return !bt_device_add(endp);
1027 error_report("bad bluetooth parameter '%s'", opt);
1028 return 1;
1031 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1033 /* FIXME: change this to true for 1.3 */
1034 if (qemu_opt_get_bool(opts, "enable", false)) {
1035 #ifdef CONFIG_SECCOMP
1036 if (seccomp_start() < 0) {
1037 error_report("failed to install seccomp syscall filter "
1038 "in the kernel");
1039 return -1;
1041 #else
1042 error_report("seccomp support is disabled");
1043 return -1;
1044 #endif
1047 return 0;
1050 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1052 const char *proc_name;
1054 if (qemu_opt_get(opts, "debug-threads")) {
1055 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1057 qemu_name = qemu_opt_get(opts, "guest");
1059 proc_name = qemu_opt_get(opts, "process");
1060 if (proc_name) {
1061 os_set_proc_name(proc_name);
1064 return 0;
1067 bool defaults_enabled(void)
1069 return has_defaults;
1072 #ifndef _WIN32
1073 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1075 int fd, dupfd, flags;
1076 int64_t fdset_id;
1077 const char *fd_opaque = NULL;
1078 AddfdInfo *fdinfo;
1080 fd = qemu_opt_get_number(opts, "fd", -1);
1081 fdset_id = qemu_opt_get_number(opts, "set", -1);
1082 fd_opaque = qemu_opt_get(opts, "opaque");
1084 if (fd < 0) {
1085 error_report("fd option is required and must be non-negative");
1086 return -1;
1089 if (fd <= STDERR_FILENO) {
1090 error_report("fd cannot be a standard I/O stream");
1091 return -1;
1095 * All fds inherited across exec() necessarily have FD_CLOEXEC
1096 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1098 flags = fcntl(fd, F_GETFD);
1099 if (flags == -1 || (flags & FD_CLOEXEC)) {
1100 error_report("fd is not valid or already in use");
1101 return -1;
1104 if (fdset_id < 0) {
1105 error_report("set option is required and must be non-negative");
1106 return -1;
1109 #ifdef F_DUPFD_CLOEXEC
1110 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1111 #else
1112 dupfd = dup(fd);
1113 if (dupfd != -1) {
1114 qemu_set_cloexec(dupfd);
1116 #endif
1117 if (dupfd == -1) {
1118 error_report("error duplicating fd: %s", strerror(errno));
1119 return -1;
1122 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1123 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1124 &error_abort);
1125 g_free(fdinfo);
1127 return 0;
1130 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1132 int fd;
1134 fd = qemu_opt_get_number(opts, "fd", -1);
1135 close(fd);
1137 return 0;
1139 #endif
1141 /***********************************************************/
1142 /* QEMU Block devices */
1144 #define HD_OPTS "media=disk"
1145 #define CDROM_OPTS "media=cdrom"
1146 #define FD_OPTS ""
1147 #define PFLASH_OPTS ""
1148 #define MTD_OPTS ""
1149 #define SD_OPTS ""
1151 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1153 BlockInterfaceType *block_default_type = opaque;
1155 return drive_new(opts, *block_default_type) == NULL;
1158 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1160 if (qemu_opt_get(opts, "snapshot") == NULL) {
1161 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1163 return 0;
1166 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1167 int index, const char *optstr)
1169 QemuOpts *opts;
1170 DriveInfo *dinfo;
1172 if (!enable || drive_get_by_index(type, index)) {
1173 return;
1176 opts = drive_add(type, index, NULL, optstr);
1177 if (snapshot) {
1178 drive_enable_snapshot(NULL, opts, NULL);
1181 dinfo = drive_new(opts, type);
1182 if (!dinfo) {
1183 exit(1);
1185 dinfo->is_default = true;
1189 static QemuOptsList qemu_smp_opts = {
1190 .name = "smp-opts",
1191 .implied_opt_name = "cpus",
1192 .merge_lists = true,
1193 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1194 .desc = {
1196 .name = "cpus",
1197 .type = QEMU_OPT_NUMBER,
1198 }, {
1199 .name = "sockets",
1200 .type = QEMU_OPT_NUMBER,
1201 }, {
1202 .name = "cores",
1203 .type = QEMU_OPT_NUMBER,
1204 }, {
1205 .name = "threads",
1206 .type = QEMU_OPT_NUMBER,
1207 }, {
1208 .name = "maxcpus",
1209 .type = QEMU_OPT_NUMBER,
1211 { /*End of list */ }
1215 static void smp_parse(QemuOpts *opts)
1217 if (opts) {
1218 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1219 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1220 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1221 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1223 /* compute missing values, prefer sockets over cores over threads */
1224 if (cpus == 0 || sockets == 0) {
1225 sockets = sockets > 0 ? sockets : 1;
1226 cores = cores > 0 ? cores : 1;
1227 threads = threads > 0 ? threads : 1;
1228 if (cpus == 0) {
1229 cpus = cores * threads * sockets;
1231 } else if (cores == 0) {
1232 threads = threads > 0 ? threads : 1;
1233 cores = cpus / (sockets * threads);
1234 cores = cores > 0 ? cores : 1;
1235 } else if (threads == 0) {
1236 threads = cpus / (cores * sockets);
1237 threads = threads > 0 ? threads : 1;
1238 } else if (sockets * cores * threads < cpus) {
1239 error_report("cpu topology: "
1240 "sockets (%u) * cores (%u) * threads (%u) < "
1241 "smp_cpus (%u)",
1242 sockets, cores, threads, cpus);
1243 exit(1);
1246 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1248 if (max_cpus < cpus) {
1249 error_report("maxcpus must be equal to or greater than smp");
1250 exit(1);
1253 if (sockets * cores * threads > max_cpus) {
1254 error_report("cpu topology: "
1255 "sockets (%u) * cores (%u) * threads (%u) > "
1256 "maxcpus (%u)",
1257 sockets, cores, threads, max_cpus);
1258 exit(1);
1261 smp_cpus = cpus;
1262 smp_cores = cores;
1263 smp_threads = threads;
1266 if (smp_cpus > 1) {
1267 Error *blocker = NULL;
1268 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1269 replay_add_blocker(blocker);
1273 static void realtime_init(void)
1275 if (enable_mlock) {
1276 if (os_mlock() < 0) {
1277 error_report("locking memory failed");
1278 exit(1);
1284 static void configure_msg(QemuOpts *opts)
1286 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1289 /***********************************************************/
1290 /* Semihosting */
1292 typedef struct SemihostingConfig {
1293 bool enabled;
1294 SemihostingTarget target;
1295 const char **argv;
1296 int argc;
1297 const char *cmdline; /* concatenated argv */
1298 } SemihostingConfig;
1300 static SemihostingConfig semihosting;
1302 bool semihosting_enabled(void)
1304 return semihosting.enabled;
1307 SemihostingTarget semihosting_get_target(void)
1309 return semihosting.target;
1312 const char *semihosting_get_arg(int i)
1314 if (i >= semihosting.argc) {
1315 return NULL;
1317 return semihosting.argv[i];
1320 int semihosting_get_argc(void)
1322 return semihosting.argc;
1325 const char *semihosting_get_cmdline(void)
1327 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1328 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1330 return semihosting.cmdline;
1333 static int add_semihosting_arg(void *opaque,
1334 const char *name, const char *val,
1335 Error **errp)
1337 SemihostingConfig *s = opaque;
1338 if (strcmp(name, "arg") == 0) {
1339 s->argc++;
1340 /* one extra element as g_strjoinv() expects NULL-terminated array */
1341 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1342 s->argv[s->argc - 1] = val;
1343 s->argv[s->argc] = NULL;
1345 return 0;
1348 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1349 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1351 char *cmd_token;
1353 /* argv[0] */
1354 add_semihosting_arg(&semihosting, "arg", file, NULL);
1356 /* split -append and initialize argv[1..n] */
1357 cmd_token = strtok(g_strdup(cmd), " ");
1358 while (cmd_token) {
1359 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1360 cmd_token = strtok(NULL, " ");
1364 /* Now we still need this for compatibility with XEN. */
1365 bool has_igd_gfx_passthru;
1366 static void igd_gfx_passthru(void)
1368 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1371 /***********************************************************/
1372 /* USB devices */
1374 static int usb_device_add(const char *devname)
1376 USBDevice *dev = NULL;
1377 #ifndef CONFIG_LINUX
1378 const char *p;
1379 #endif
1381 if (!machine_usb(current_machine)) {
1382 return -1;
1385 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1386 dev = usbdevice_create(devname);
1387 if (dev)
1388 goto done;
1390 /* the other ones */
1391 #ifndef CONFIG_LINUX
1392 /* only the linux version is qdev-ified, usb-bsd still needs this */
1393 if (strstart(devname, "host:", &p)) {
1394 dev = usb_host_device_open(usb_bus_find(-1), p);
1396 #endif
1397 if (!dev)
1398 return -1;
1400 done:
1401 return 0;
1404 static int usb_device_del(const char *devname)
1406 int bus_num, addr;
1407 const char *p;
1409 if (strstart(devname, "host:", &p)) {
1410 return -1;
1413 if (!machine_usb(current_machine)) {
1414 return -1;
1417 p = strchr(devname, '.');
1418 if (!p)
1419 return -1;
1420 bus_num = strtoul(devname, NULL, 0);
1421 addr = strtoul(p + 1, NULL, 0);
1423 return usb_device_delete_addr(bus_num, addr);
1426 static int usb_parse(const char *cmdline)
1428 int r;
1429 r = usb_device_add(cmdline);
1430 if (r < 0) {
1431 error_report("could not add USB device '%s'", cmdline);
1433 return r;
1436 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1438 const char *devname = qdict_get_str(qdict, "devname");
1440 error_report("usb_add is deprecated, please use device_add instead");
1442 if (usb_device_add(devname) < 0) {
1443 error_report("could not add USB device '%s'", devname);
1447 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1449 const char *devname = qdict_get_str(qdict, "devname");
1451 error_report("usb_del is deprecated, please use device_del instead");
1453 if (usb_device_del(devname) < 0) {
1454 error_report("could not delete USB device '%s'", devname);
1458 /***********************************************************/
1459 /* machine registration */
1461 MachineState *current_machine;
1463 static MachineClass *find_machine(const char *name)
1465 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1466 MachineClass *mc = NULL;
1468 for (el = machines; el; el = el->next) {
1469 MachineClass *temp = el->data;
1471 if (!strcmp(temp->name, name)) {
1472 mc = temp;
1473 break;
1475 if (temp->alias &&
1476 !strcmp(temp->alias, name)) {
1477 mc = temp;
1478 break;
1482 g_slist_free(machines);
1483 return mc;
1486 MachineClass *find_default_machine(void)
1488 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1489 MachineClass *mc = NULL;
1491 for (el = machines; el; el = el->next) {
1492 MachineClass *temp = el->data;
1494 if (temp->is_default) {
1495 mc = temp;
1496 break;
1500 g_slist_free(machines);
1501 return mc;
1504 MachineInfoList *qmp_query_machines(Error **errp)
1506 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1507 MachineInfoList *mach_list = NULL;
1509 for (el = machines; el; el = el->next) {
1510 MachineClass *mc = el->data;
1511 MachineInfoList *entry;
1512 MachineInfo *info;
1514 info = g_malloc0(sizeof(*info));
1515 if (mc->is_default) {
1516 info->has_is_default = true;
1517 info->is_default = true;
1520 if (mc->alias) {
1521 info->has_alias = true;
1522 info->alias = g_strdup(mc->alias);
1525 info->name = g_strdup(mc->name);
1526 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1527 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1529 entry = g_malloc0(sizeof(*entry));
1530 entry->value = info;
1531 entry->next = mach_list;
1532 mach_list = entry;
1535 g_slist_free(machines);
1536 return mach_list;
1539 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1541 ObjectProperty *prop;
1542 ObjectPropertyIterator iter;
1544 if (!qemu_opt_has_help_opt(opts)) {
1545 return 0;
1548 object_property_iter_init(&iter, OBJECT(machine));
1549 while ((prop = object_property_iter_next(&iter))) {
1550 if (!prop->set) {
1551 continue;
1554 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1555 prop->name, prop->type);
1556 if (prop->description) {
1557 error_printf(" (%s)\n", prop->description);
1558 } else {
1559 error_printf("\n");
1563 return 1;
1566 /***********************************************************/
1567 /* main execution loop */
1569 struct vm_change_state_entry {
1570 VMChangeStateHandler *cb;
1571 void *opaque;
1572 QLIST_ENTRY (vm_change_state_entry) entries;
1575 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1577 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1578 void *opaque)
1580 VMChangeStateEntry *e;
1582 e = g_malloc0(sizeof (*e));
1584 e->cb = cb;
1585 e->opaque = opaque;
1586 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1587 return e;
1590 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1592 QLIST_REMOVE (e, entries);
1593 g_free (e);
1596 void vm_state_notify(int running, RunState state)
1598 VMChangeStateEntry *e, *next;
1600 trace_vm_state_notify(running, state);
1602 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1603 e->cb(e->opaque, running, state);
1607 static ShutdownCause reset_requested;
1608 static ShutdownCause shutdown_requested;
1609 static int shutdown_signal;
1610 static pid_t shutdown_pid;
1611 static int powerdown_requested;
1612 static int debug_requested;
1613 static int suspend_requested;
1614 static WakeupReason wakeup_reason;
1615 static NotifierList powerdown_notifiers =
1616 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1617 static NotifierList suspend_notifiers =
1618 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1619 static NotifierList wakeup_notifiers =
1620 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1621 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1623 ShutdownCause qemu_shutdown_requested_get(void)
1625 return shutdown_requested;
1628 ShutdownCause qemu_reset_requested_get(void)
1630 return reset_requested;
1633 static int qemu_shutdown_requested(void)
1635 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1638 static void qemu_kill_report(void)
1640 if (!qtest_driver() && shutdown_signal) {
1641 if (shutdown_pid == 0) {
1642 /* This happens for eg ^C at the terminal, so it's worth
1643 * avoiding printing an odd message in that case.
1645 error_report("terminating on signal %d", shutdown_signal);
1646 } else {
1647 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1649 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1650 shutdown_signal, shutdown_pid,
1651 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1652 g_free(shutdown_cmd);
1654 shutdown_signal = 0;
1658 static ShutdownCause qemu_reset_requested(void)
1660 ShutdownCause r = reset_requested;
1662 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1663 reset_requested = SHUTDOWN_CAUSE_NONE;
1664 return r;
1666 return SHUTDOWN_CAUSE_NONE;
1669 static int qemu_suspend_requested(void)
1671 int r = suspend_requested;
1672 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1673 suspend_requested = 0;
1674 return r;
1676 return false;
1679 static WakeupReason qemu_wakeup_requested(void)
1681 return wakeup_reason;
1684 static int qemu_powerdown_requested(void)
1686 int r = powerdown_requested;
1687 powerdown_requested = 0;
1688 return r;
1691 static int qemu_debug_requested(void)
1693 int r = debug_requested;
1694 debug_requested = 0;
1695 return r;
1699 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1701 void qemu_system_reset(ShutdownCause reason)
1703 MachineClass *mc;
1705 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1707 cpu_synchronize_all_states();
1709 if (mc && mc->reset) {
1710 mc->reset();
1711 } else {
1712 qemu_devices_reset();
1714 if (reason) {
1715 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1716 &error_abort);
1718 cpu_synchronize_all_post_reset();
1721 void qemu_system_guest_panicked(GuestPanicInformation *info)
1723 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1725 if (current_cpu) {
1726 current_cpu->crash_occurred = true;
1728 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1729 !!info, info, &error_abort);
1730 vm_stop(RUN_STATE_GUEST_PANICKED);
1731 if (!no_shutdown) {
1732 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1733 !!info, info, &error_abort);
1734 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1737 if (info) {
1738 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1739 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1740 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1741 info->u.hyper_v.arg1,
1742 info->u.hyper_v.arg2,
1743 info->u.hyper_v.arg3,
1744 info->u.hyper_v.arg4,
1745 info->u.hyper_v.arg5);
1747 qapi_free_GuestPanicInformation(info);
1751 void qemu_system_reset_request(ShutdownCause reason)
1753 if (no_reboot) {
1754 shutdown_requested = reason;
1755 } else {
1756 reset_requested = reason;
1758 cpu_stop_current();
1759 qemu_notify_event();
1762 static void qemu_system_suspend(void)
1764 pause_all_vcpus();
1765 notifier_list_notify(&suspend_notifiers, NULL);
1766 runstate_set(RUN_STATE_SUSPENDED);
1767 qapi_event_send_suspend(&error_abort);
1770 void qemu_system_suspend_request(void)
1772 if (runstate_check(RUN_STATE_SUSPENDED)) {
1773 return;
1775 suspend_requested = 1;
1776 cpu_stop_current();
1777 qemu_notify_event();
1780 void qemu_register_suspend_notifier(Notifier *notifier)
1782 notifier_list_add(&suspend_notifiers, notifier);
1785 void qemu_system_wakeup_request(WakeupReason reason)
1787 trace_system_wakeup_request(reason);
1789 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1790 return;
1792 if (!(wakeup_reason_mask & (1 << reason))) {
1793 return;
1795 runstate_set(RUN_STATE_RUNNING);
1796 wakeup_reason = reason;
1797 qemu_notify_event();
1800 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1802 if (enabled) {
1803 wakeup_reason_mask |= (1 << reason);
1804 } else {
1805 wakeup_reason_mask &= ~(1 << reason);
1809 void qemu_register_wakeup_notifier(Notifier *notifier)
1811 notifier_list_add(&wakeup_notifiers, notifier);
1814 void qemu_system_killed(int signal, pid_t pid)
1816 shutdown_signal = signal;
1817 shutdown_pid = pid;
1818 no_shutdown = 0;
1820 /* Cannot call qemu_system_shutdown_request directly because
1821 * we are in a signal handler.
1823 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1824 qemu_notify_event();
1827 void qemu_system_shutdown_request(ShutdownCause reason)
1829 trace_qemu_system_shutdown_request(reason);
1830 replay_shutdown_request(reason);
1831 shutdown_requested = reason;
1832 qemu_notify_event();
1835 static void qemu_system_powerdown(void)
1837 qapi_event_send_powerdown(&error_abort);
1838 notifier_list_notify(&powerdown_notifiers, NULL);
1841 void qemu_system_powerdown_request(void)
1843 trace_qemu_system_powerdown_request();
1844 powerdown_requested = 1;
1845 qemu_notify_event();
1848 void qemu_register_powerdown_notifier(Notifier *notifier)
1850 notifier_list_add(&powerdown_notifiers, notifier);
1853 void qemu_system_debug_request(void)
1855 debug_requested = 1;
1856 qemu_notify_event();
1859 static bool main_loop_should_exit(void)
1861 RunState r;
1862 ShutdownCause request;
1864 if (qemu_debug_requested()) {
1865 vm_stop(RUN_STATE_DEBUG);
1867 if (qemu_suspend_requested()) {
1868 qemu_system_suspend();
1870 request = qemu_shutdown_requested();
1871 if (request) {
1872 qemu_kill_report();
1873 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1874 &error_abort);
1875 if (no_shutdown) {
1876 vm_stop(RUN_STATE_SHUTDOWN);
1877 } else {
1878 return true;
1881 request = qemu_reset_requested();
1882 if (request) {
1883 pause_all_vcpus();
1884 qemu_system_reset(request);
1885 resume_all_vcpus();
1886 if (!runstate_check(RUN_STATE_RUNNING) &&
1887 !runstate_check(RUN_STATE_INMIGRATE)) {
1888 runstate_set(RUN_STATE_PRELAUNCH);
1891 if (qemu_wakeup_requested()) {
1892 pause_all_vcpus();
1893 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1894 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1895 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1896 resume_all_vcpus();
1897 qapi_event_send_wakeup(&error_abort);
1899 if (qemu_powerdown_requested()) {
1900 qemu_system_powerdown();
1902 if (qemu_vmstop_requested(&r)) {
1903 vm_stop(r);
1905 return false;
1908 static void main_loop(void)
1910 #ifdef CONFIG_PROFILER
1911 int64_t ti;
1912 #endif
1913 do {
1914 #ifdef CONFIG_PROFILER
1915 ti = profile_getclock();
1916 #endif
1917 main_loop_wait(false);
1918 #ifdef CONFIG_PROFILER
1919 dev_time += profile_getclock() - ti;
1920 #endif
1921 } while (!main_loop_should_exit());
1924 static void version(void)
1926 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1927 QEMU_COPYRIGHT "\n");
1930 static void help(int exitcode)
1932 version();
1933 printf("usage: %s [options] [disk_image]\n\n"
1934 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1935 error_get_progname());
1937 #define QEMU_OPTIONS_GENERATE_HELP
1938 #include "qemu-options-wrapper.h"
1940 printf("\nDuring emulation, the following keys are useful:\n"
1941 "ctrl-alt-f toggle full screen\n"
1942 "ctrl-alt-n switch to virtual console 'n'\n"
1943 "ctrl-alt toggle mouse and keyboard grab\n"
1944 "\n"
1945 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1947 exit(exitcode);
1950 #define HAS_ARG 0x0001
1952 typedef struct QEMUOption {
1953 const char *name;
1954 int flags;
1955 int index;
1956 uint32_t arch_mask;
1957 } QEMUOption;
1959 static const QEMUOption qemu_options[] = {
1960 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1961 #define QEMU_OPTIONS_GENERATE_OPTIONS
1962 #include "qemu-options-wrapper.h"
1963 { NULL },
1966 typedef struct VGAInterfaceInfo {
1967 const char *opt_name; /* option name */
1968 const char *name; /* human-readable name */
1969 /* Class names indicating that support is available.
1970 * If no class is specified, the interface is always available */
1971 const char *class_names[2];
1972 } VGAInterfaceInfo;
1974 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1975 [VGA_NONE] = {
1976 .opt_name = "none",
1978 [VGA_STD] = {
1979 .opt_name = "std",
1980 .name = "standard VGA",
1981 .class_names = { "VGA", "isa-vga" },
1983 [VGA_CIRRUS] = {
1984 .opt_name = "cirrus",
1985 .name = "Cirrus VGA",
1986 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1988 [VGA_VMWARE] = {
1989 .opt_name = "vmware",
1990 .name = "VMWare SVGA",
1991 .class_names = { "vmware-svga" },
1993 [VGA_VIRTIO] = {
1994 .opt_name = "virtio",
1995 .name = "Virtio VGA",
1996 .class_names = { "virtio-vga" },
1998 [VGA_QXL] = {
1999 .opt_name = "qxl",
2000 .name = "QXL VGA",
2001 .class_names = { "qxl-vga" },
2003 [VGA_TCX] = {
2004 .opt_name = "tcx",
2005 .name = "TCX framebuffer",
2006 .class_names = { "SUNW,tcx" },
2008 [VGA_CG3] = {
2009 .opt_name = "cg3",
2010 .name = "CG3 framebuffer",
2011 .class_names = { "cgthree" },
2013 [VGA_XENFB] = {
2014 .opt_name = "xenfb",
2018 static bool vga_interface_available(VGAInterfaceType t)
2020 VGAInterfaceInfo *ti = &vga_interfaces[t];
2022 assert(t < VGA_TYPE_MAX);
2023 return !ti->class_names[0] ||
2024 object_class_by_name(ti->class_names[0]) ||
2025 object_class_by_name(ti->class_names[1]);
2028 static void select_vgahw(const char *p)
2030 const char *opts;
2031 int t;
2033 assert(vga_interface_type == VGA_NONE);
2034 for (t = 0; t < VGA_TYPE_MAX; t++) {
2035 VGAInterfaceInfo *ti = &vga_interfaces[t];
2036 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2037 if (!vga_interface_available(t)) {
2038 error_report("%s not available", ti->name);
2039 exit(1);
2041 vga_interface_type = t;
2042 break;
2045 if (t == VGA_TYPE_MAX) {
2046 invalid_vga:
2047 error_report("unknown vga type: %s", p);
2048 exit(1);
2050 while (*opts) {
2051 const char *nextopt;
2053 if (strstart(opts, ",retrace=", &nextopt)) {
2054 opts = nextopt;
2055 if (strstart(opts, "dumb", &nextopt))
2056 vga_retrace_method = VGA_RETRACE_DUMB;
2057 else if (strstart(opts, "precise", &nextopt))
2058 vga_retrace_method = VGA_RETRACE_PRECISE;
2059 else goto invalid_vga;
2060 } else goto invalid_vga;
2061 opts = nextopt;
2065 typedef enum DisplayType {
2066 DT_DEFAULT,
2067 DT_CURSES,
2068 DT_SDL,
2069 DT_COCOA,
2070 DT_GTK,
2071 DT_EGL,
2072 DT_NONE,
2073 } DisplayType;
2075 static DisplayType select_display(const char *p)
2077 const char *opts;
2078 DisplayType display = DT_DEFAULT;
2080 if (strstart(p, "sdl", &opts)) {
2081 #ifdef CONFIG_SDL
2082 display = DT_SDL;
2083 while (*opts) {
2084 const char *nextopt;
2086 if (strstart(opts, ",frame=", &nextopt)) {
2087 opts = nextopt;
2088 if (strstart(opts, "on", &nextopt)) {
2089 no_frame = 0;
2090 } else if (strstart(opts, "off", &nextopt)) {
2091 no_frame = 1;
2092 } else {
2093 goto invalid_sdl_args;
2095 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2096 opts = nextopt;
2097 if (strstart(opts, "on", &nextopt)) {
2098 alt_grab = 1;
2099 } else if (strstart(opts, "off", &nextopt)) {
2100 alt_grab = 0;
2101 } else {
2102 goto invalid_sdl_args;
2104 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2105 opts = nextopt;
2106 if (strstart(opts, "on", &nextopt)) {
2107 ctrl_grab = 1;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 ctrl_grab = 0;
2110 } else {
2111 goto invalid_sdl_args;
2113 } else if (strstart(opts, ",window_close=", &nextopt)) {
2114 opts = nextopt;
2115 if (strstart(opts, "on", &nextopt)) {
2116 no_quit = 0;
2117 } else if (strstart(opts, "off", &nextopt)) {
2118 no_quit = 1;
2119 } else {
2120 goto invalid_sdl_args;
2122 } else if (strstart(opts, ",gl=", &nextopt)) {
2123 opts = nextopt;
2124 if (strstart(opts, "on", &nextopt)) {
2125 request_opengl = 1;
2126 } else if (strstart(opts, "off", &nextopt)) {
2127 request_opengl = 0;
2128 } else {
2129 goto invalid_sdl_args;
2131 } else {
2132 invalid_sdl_args:
2133 error_report("invalid SDL option string");
2134 exit(1);
2136 opts = nextopt;
2138 #else
2139 error_report("SDL support is disabled");
2140 exit(1);
2141 #endif
2142 } else if (strstart(p, "vnc", &opts)) {
2143 if (*opts == '=') {
2144 vnc_parse(opts + 1, &error_fatal);
2145 } else {
2146 error_report("VNC requires a display argument vnc=<display>");
2147 exit(1);
2149 } else if (strstart(p, "egl-headless", &opts)) {
2150 #ifdef CONFIG_OPENGL_DMABUF
2151 request_opengl = 1;
2152 display_opengl = 1;
2153 display = DT_EGL;
2154 #else
2155 fprintf(stderr, "egl support is disabled\n");
2156 exit(1);
2157 #endif
2158 } else if (strstart(p, "curses", &opts)) {
2159 #ifdef CONFIG_CURSES
2160 display = DT_CURSES;
2161 #else
2162 error_report("curses support is disabled");
2163 exit(1);
2164 #endif
2165 } else if (strstart(p, "gtk", &opts)) {
2166 #ifdef CONFIG_GTK
2167 display = DT_GTK;
2168 while (*opts) {
2169 const char *nextopt;
2171 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2172 opts = nextopt;
2173 if (strstart(opts, "on", &nextopt)) {
2174 grab_on_hover = true;
2175 } else if (strstart(opts, "off", &nextopt)) {
2176 grab_on_hover = false;
2177 } else {
2178 goto invalid_gtk_args;
2180 } else if (strstart(opts, ",gl=", &nextopt)) {
2181 opts = nextopt;
2182 if (strstart(opts, "on", &nextopt)) {
2183 request_opengl = 1;
2184 } else if (strstart(opts, "off", &nextopt)) {
2185 request_opengl = 0;
2186 } else {
2187 goto invalid_gtk_args;
2189 } else {
2190 invalid_gtk_args:
2191 error_report("invalid GTK option string");
2192 exit(1);
2194 opts = nextopt;
2196 #else
2197 error_report("GTK support is disabled");
2198 exit(1);
2199 #endif
2200 } else if (strstart(p, "none", &opts)) {
2201 display = DT_NONE;
2202 } else {
2203 error_report("unknown display type");
2204 exit(1);
2207 return display;
2210 static int balloon_parse(const char *arg)
2212 QemuOpts *opts;
2214 if (strcmp(arg, "none") == 0) {
2215 return 0;
2218 if (!strncmp(arg, "virtio", 6)) {
2219 if (arg[6] == ',') {
2220 /* have params -> parse them */
2221 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2222 false);
2223 if (!opts)
2224 return -1;
2225 } else {
2226 /* create empty opts */
2227 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2228 &error_abort);
2230 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2231 return 0;
2234 return -1;
2237 char *qemu_find_file(int type, const char *name)
2239 int i;
2240 const char *subdir;
2241 char *buf;
2243 /* Try the name as a straight path first */
2244 if (access(name, R_OK) == 0) {
2245 trace_load_file(name, name);
2246 return g_strdup(name);
2249 switch (type) {
2250 case QEMU_FILE_TYPE_BIOS:
2251 subdir = "";
2252 break;
2253 case QEMU_FILE_TYPE_KEYMAP:
2254 subdir = "keymaps/";
2255 break;
2256 default:
2257 abort();
2260 for (i = 0; i < data_dir_idx; i++) {
2261 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2262 if (access(buf, R_OK) == 0) {
2263 trace_load_file(name, buf);
2264 return buf;
2266 g_free(buf);
2268 return NULL;
2271 static inline bool nonempty_str(const char *str)
2273 return str && *str;
2276 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2278 gchar *buf;
2279 size_t size;
2280 const char *name, *file, *str;
2281 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2283 if (fw_cfg == NULL) {
2284 error_report("fw_cfg device not available");
2285 return -1;
2287 name = qemu_opt_get(opts, "name");
2288 file = qemu_opt_get(opts, "file");
2289 str = qemu_opt_get(opts, "string");
2291 /* we need name and either a file or the content string */
2292 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2293 error_report("invalid argument(s)");
2294 return -1;
2296 if (nonempty_str(file) && nonempty_str(str)) {
2297 error_report("file and string are mutually exclusive");
2298 return -1;
2300 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2301 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2302 return -1;
2304 if (strncmp(name, "opt/", 4) != 0) {
2305 warn_report("externally provided fw_cfg item names "
2306 "should be prefixed with \"opt/\"");
2308 if (nonempty_str(str)) {
2309 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2310 buf = g_memdup(str, size);
2311 } else {
2312 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2313 error_report("can't load %s", file);
2314 return -1;
2317 /* For legacy, keep user files in a specific global order. */
2318 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2319 fw_cfg_add_file(fw_cfg, name, buf, size);
2320 fw_cfg_reset_order_override(fw_cfg);
2321 return 0;
2324 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2326 return qdev_device_help(opts);
2329 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2331 Error *err = NULL;
2332 DeviceState *dev;
2334 dev = qdev_device_add(opts, &err);
2335 if (!dev) {
2336 error_report_err(err);
2337 return -1;
2339 object_unref(OBJECT(dev));
2340 return 0;
2343 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2345 Error *local_err = NULL;
2347 qemu_chr_new_from_opts(opts, &local_err);
2348 if (local_err) {
2349 error_report_err(local_err);
2350 return -1;
2352 return 0;
2355 #ifdef CONFIG_VIRTFS
2356 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2358 return qemu_fsdev_add(opts);
2360 #endif
2362 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2364 Chardev *chr;
2365 const char *chardev;
2366 const char *mode;
2367 int flags;
2369 mode = qemu_opt_get(opts, "mode");
2370 if (mode == NULL) {
2371 mode = "readline";
2373 if (strcmp(mode, "readline") == 0) {
2374 flags = MONITOR_USE_READLINE;
2375 } else if (strcmp(mode, "control") == 0) {
2376 flags = MONITOR_USE_CONTROL;
2377 } else {
2378 error_report("unknown monitor mode \"%s\"", mode);
2379 exit(1);
2382 if (qemu_opt_get_bool(opts, "pretty", 0))
2383 flags |= MONITOR_USE_PRETTY;
2385 if (qemu_opt_get_bool(opts, "default", 0)) {
2386 error_report("option 'default' does nothing and is deprecated");
2389 chardev = qemu_opt_get(opts, "chardev");
2390 chr = qemu_chr_find(chardev);
2391 if (chr == NULL) {
2392 error_report("chardev \"%s\" not found", chardev);
2393 exit(1);
2396 monitor_init(chr, flags);
2397 return 0;
2400 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2402 static int monitor_device_index = 0;
2403 QemuOpts *opts;
2404 const char *p;
2405 char label[32];
2407 if (strstart(optarg, "chardev:", &p)) {
2408 snprintf(label, sizeof(label), "%s", p);
2409 } else {
2410 snprintf(label, sizeof(label), "compat_monitor%d",
2411 monitor_device_index);
2412 opts = qemu_chr_parse_compat(label, optarg);
2413 if (!opts) {
2414 error_report("parse error: %s", optarg);
2415 exit(1);
2419 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2420 qemu_opt_set(opts, "mode", mode, &error_abort);
2421 qemu_opt_set(opts, "chardev", label, &error_abort);
2422 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2423 monitor_device_index++;
2426 struct device_config {
2427 enum {
2428 DEV_USB, /* -usbdevice */
2429 DEV_BT, /* -bt */
2430 DEV_SERIAL, /* -serial */
2431 DEV_PARALLEL, /* -parallel */
2432 DEV_VIRTCON, /* -virtioconsole */
2433 DEV_DEBUGCON, /* -debugcon */
2434 DEV_GDB, /* -gdb, -s */
2435 DEV_SCLP, /* s390 sclp */
2436 } type;
2437 const char *cmdline;
2438 Location loc;
2439 QTAILQ_ENTRY(device_config) next;
2442 static QTAILQ_HEAD(, device_config) device_configs =
2443 QTAILQ_HEAD_INITIALIZER(device_configs);
2445 static void add_device_config(int type, const char *cmdline)
2447 struct device_config *conf;
2449 conf = g_malloc0(sizeof(*conf));
2450 conf->type = type;
2451 conf->cmdline = cmdline;
2452 loc_save(&conf->loc);
2453 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2456 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2458 struct device_config *conf;
2459 int rc;
2461 QTAILQ_FOREACH(conf, &device_configs, next) {
2462 if (conf->type != type)
2463 continue;
2464 loc_push_restore(&conf->loc);
2465 rc = func(conf->cmdline);
2466 loc_pop(&conf->loc);
2467 if (rc) {
2468 return rc;
2471 return 0;
2474 static int serial_parse(const char *devname)
2476 static int index = 0;
2477 char label[32];
2479 if (strcmp(devname, "none") == 0)
2480 return 0;
2481 if (index == MAX_SERIAL_PORTS) {
2482 error_report("too many serial ports");
2483 exit(1);
2485 snprintf(label, sizeof(label), "serial%d", index);
2486 serial_hds[index] = qemu_chr_new(label, devname);
2487 if (!serial_hds[index]) {
2488 error_report("could not connect serial device"
2489 " to character backend '%s'", devname);
2490 return -1;
2492 index++;
2493 return 0;
2496 static int parallel_parse(const char *devname)
2498 static int index = 0;
2499 char label[32];
2501 if (strcmp(devname, "none") == 0)
2502 return 0;
2503 if (index == MAX_PARALLEL_PORTS) {
2504 error_report("too many parallel ports");
2505 exit(1);
2507 snprintf(label, sizeof(label), "parallel%d", index);
2508 parallel_hds[index] = qemu_chr_new(label, devname);
2509 if (!parallel_hds[index]) {
2510 error_report("could not connect parallel device"
2511 " to character backend '%s'", devname);
2512 return -1;
2514 index++;
2515 return 0;
2518 static int virtcon_parse(const char *devname)
2520 QemuOptsList *device = qemu_find_opts("device");
2521 static int index = 0;
2522 char label[32];
2523 QemuOpts *bus_opts, *dev_opts;
2525 if (strcmp(devname, "none") == 0)
2526 return 0;
2527 if (index == MAX_VIRTIO_CONSOLES) {
2528 error_report("too many virtio consoles");
2529 exit(1);
2532 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2533 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2535 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2536 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2538 snprintf(label, sizeof(label), "virtcon%d", index);
2539 virtcon_hds[index] = qemu_chr_new(label, devname);
2540 if (!virtcon_hds[index]) {
2541 error_report("could not connect virtio console"
2542 " to character backend '%s'", devname);
2543 return -1;
2545 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2547 index++;
2548 return 0;
2551 static int sclp_parse(const char *devname)
2553 QemuOptsList *device = qemu_find_opts("device");
2554 static int index = 0;
2555 char label[32];
2556 QemuOpts *dev_opts;
2558 if (strcmp(devname, "none") == 0) {
2559 return 0;
2561 if (index == MAX_SCLP_CONSOLES) {
2562 error_report("too many sclp consoles");
2563 exit(1);
2566 assert(arch_type == QEMU_ARCH_S390X);
2568 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2569 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2571 snprintf(label, sizeof(label), "sclpcon%d", index);
2572 sclp_hds[index] = qemu_chr_new(label, devname);
2573 if (!sclp_hds[index]) {
2574 error_report("could not connect sclp console"
2575 " to character backend '%s'", devname);
2576 return -1;
2578 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2580 index++;
2581 return 0;
2584 static int debugcon_parse(const char *devname)
2586 QemuOpts *opts;
2588 if (!qemu_chr_new("debugcon", devname)) {
2589 exit(1);
2591 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2592 if (!opts) {
2593 error_report("already have a debugcon device");
2594 exit(1);
2596 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2597 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2598 return 0;
2601 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2603 const MachineClass *mc1 = a, *mc2 = b;
2604 int res;
2606 if (mc1->family == NULL) {
2607 if (mc2->family == NULL) {
2608 /* Compare standalone machine types against each other; they sort
2609 * in increasing order.
2611 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2612 object_class_get_name(OBJECT_CLASS(mc2)));
2615 /* Standalone machine types sort after families. */
2616 return 1;
2619 if (mc2->family == NULL) {
2620 /* Families sort before standalone machine types. */
2621 return -1;
2624 /* Families sort between each other alphabetically increasingly. */
2625 res = strcmp(mc1->family, mc2->family);
2626 if (res != 0) {
2627 return res;
2630 /* Within the same family, machine types sort in decreasing order. */
2631 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2632 object_class_get_name(OBJECT_CLASS(mc1)));
2635 static MachineClass *machine_parse(const char *name)
2637 MachineClass *mc = NULL;
2638 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2640 if (name) {
2641 mc = find_machine(name);
2643 if (mc) {
2644 g_slist_free(machines);
2645 return mc;
2647 if (name && !is_help_option(name)) {
2648 error_report("unsupported machine type");
2649 error_printf("Use -machine help to list supported machines\n");
2650 } else {
2651 printf("Supported machines are:\n");
2652 machines = g_slist_sort(machines, machine_class_cmp);
2653 for (el = machines; el; el = el->next) {
2654 MachineClass *mc = el->data;
2655 if (mc->alias) {
2656 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2658 printf("%-20s %s%s\n", mc->name, mc->desc,
2659 mc->is_default ? " (default)" : "");
2663 g_slist_free(machines);
2664 exit(!name || !is_help_option(name));
2667 void qemu_add_exit_notifier(Notifier *notify)
2669 notifier_list_add(&exit_notifiers, notify);
2672 void qemu_remove_exit_notifier(Notifier *notify)
2674 notifier_remove(notify);
2677 static void qemu_run_exit_notifiers(void)
2679 notifier_list_notify(&exit_notifiers, NULL);
2682 static bool machine_init_done;
2684 void qemu_add_machine_init_done_notifier(Notifier *notify)
2686 notifier_list_add(&machine_init_done_notifiers, notify);
2687 if (machine_init_done) {
2688 notify->notify(notify, NULL);
2692 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2694 notifier_remove(notify);
2697 static void qemu_run_machine_init_done_notifiers(void)
2699 notifier_list_notify(&machine_init_done_notifiers, NULL);
2700 machine_init_done = true;
2703 static const QEMUOption *lookup_opt(int argc, char **argv,
2704 const char **poptarg, int *poptind)
2706 const QEMUOption *popt;
2707 int optind = *poptind;
2708 char *r = argv[optind];
2709 const char *optarg;
2711 loc_set_cmdline(argv, optind, 1);
2712 optind++;
2713 /* Treat --foo the same as -foo. */
2714 if (r[1] == '-')
2715 r++;
2716 popt = qemu_options;
2717 for(;;) {
2718 if (!popt->name) {
2719 error_report("invalid option");
2720 exit(1);
2722 if (!strcmp(popt->name, r + 1))
2723 break;
2724 popt++;
2726 if (popt->flags & HAS_ARG) {
2727 if (optind >= argc) {
2728 error_report("requires an argument");
2729 exit(1);
2731 optarg = argv[optind++];
2732 loc_set_cmdline(argv, optind - 2, 2);
2733 } else {
2734 optarg = NULL;
2737 *poptarg = optarg;
2738 *poptind = optind;
2740 return popt;
2743 static MachineClass *select_machine(void)
2745 MachineClass *machine_class = find_default_machine();
2746 const char *optarg;
2747 QemuOpts *opts;
2748 Location loc;
2750 loc_push_none(&loc);
2752 opts = qemu_get_machine_opts();
2753 qemu_opts_loc_restore(opts);
2755 optarg = qemu_opt_get(opts, "type");
2756 if (optarg) {
2757 machine_class = machine_parse(optarg);
2760 if (!machine_class) {
2761 error_report("No machine specified, and there is no default");
2762 error_printf("Use -machine help to list supported machines\n");
2763 exit(1);
2766 loc_pop(&loc);
2767 return machine_class;
2770 static int machine_set_property(void *opaque,
2771 const char *name, const char *value,
2772 Error **errp)
2774 Object *obj = OBJECT(opaque);
2775 Error *local_err = NULL;
2776 char *p, *qom_name;
2778 if (strcmp(name, "type") == 0) {
2779 return 0;
2782 qom_name = g_strdup(name);
2783 for (p = qom_name; *p; p++) {
2784 if (*p == '_') {
2785 *p = '-';
2789 object_property_parse(obj, value, qom_name, &local_err);
2790 g_free(qom_name);
2792 if (local_err) {
2793 error_report_err(local_err);
2794 return -1;
2797 return 0;
2802 * Initial object creation happens before all other
2803 * QEMU data types are created. The majority of objects
2804 * can be created at this point. The rng-egd object
2805 * cannot be created here, as it depends on the chardev
2806 * already existing.
2808 static bool object_create_initial(const char *type)
2810 if (g_str_equal(type, "rng-egd")) {
2811 return false;
2815 * return false for concrete netfilters since
2816 * they depend on netdevs already existing
2818 if (g_str_equal(type, "filter-buffer") ||
2819 g_str_equal(type, "filter-dump") ||
2820 g_str_equal(type, "filter-mirror") ||
2821 g_str_equal(type, "filter-redirector") ||
2822 g_str_equal(type, "colo-compare") ||
2823 g_str_equal(type, "filter-rewriter") ||
2824 g_str_equal(type, "filter-replay")) {
2825 return false;
2828 /* Memory allocation by backends needs to be done
2829 * after configure_accelerator() (due to the tcg_enabled()
2830 * checks at memory_region_init_*()).
2832 * Also, allocation of large amounts of memory may delay
2833 * chardev initialization for too long, and trigger timeouts
2834 * on software that waits for a monitor socket to be created
2835 * (e.g. libvirt).
2837 if (g_str_has_prefix(type, "memory-backend-")) {
2838 return false;
2841 return true;
2846 * The remainder of object creation happens after the
2847 * creation of chardev, fsdev, net clients and device data types.
2849 static bool object_create_delayed(const char *type)
2851 return !object_create_initial(type);
2855 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2856 MachineClass *mc)
2858 uint64_t sz;
2859 const char *mem_str;
2860 const char *maxmem_str, *slots_str;
2861 const ram_addr_t default_ram_size = mc->default_ram_size;
2862 QemuOpts *opts = qemu_find_opts_singleton("memory");
2863 Location loc;
2865 loc_push_none(&loc);
2866 qemu_opts_loc_restore(opts);
2868 sz = 0;
2869 mem_str = qemu_opt_get(opts, "size");
2870 if (mem_str) {
2871 if (!*mem_str) {
2872 error_report("missing 'size' option value");
2873 exit(EXIT_FAILURE);
2876 sz = qemu_opt_get_size(opts, "size", ram_size);
2878 /* Fix up legacy suffix-less format */
2879 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2880 uint64_t overflow_check = sz;
2882 sz <<= 20;
2883 if ((sz >> 20) != overflow_check) {
2884 error_report("too large 'size' option value");
2885 exit(EXIT_FAILURE);
2890 /* backward compatibility behaviour for case "-m 0" */
2891 if (sz == 0) {
2892 sz = default_ram_size;
2895 sz = QEMU_ALIGN_UP(sz, 8192);
2896 ram_size = sz;
2897 if (ram_size != sz) {
2898 error_report("ram size too large");
2899 exit(EXIT_FAILURE);
2902 /* store value for the future use */
2903 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2904 *maxram_size = ram_size;
2906 maxmem_str = qemu_opt_get(opts, "maxmem");
2907 slots_str = qemu_opt_get(opts, "slots");
2908 if (maxmem_str && slots_str) {
2909 uint64_t slots;
2911 sz = qemu_opt_get_size(opts, "maxmem", 0);
2912 slots = qemu_opt_get_number(opts, "slots", 0);
2913 if (sz < ram_size) {
2914 error_report("invalid value of -m option maxmem: "
2915 "maximum memory size (0x%" PRIx64 ") must be at least "
2916 "the initial memory size (0x" RAM_ADDR_FMT ")",
2917 sz, ram_size);
2918 exit(EXIT_FAILURE);
2919 } else if (sz > ram_size) {
2920 if (!slots) {
2921 error_report("invalid value of -m option: maxmem was "
2922 "specified, but no hotplug slots were specified");
2923 exit(EXIT_FAILURE);
2925 } else if (slots) {
2926 error_report("invalid value of -m option maxmem: "
2927 "memory slots were specified but maximum memory size "
2928 "(0x%" PRIx64 ") is equal to the initial memory size "
2929 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2930 exit(EXIT_FAILURE);
2933 *maxram_size = sz;
2934 *ram_slots = slots;
2935 } else if ((!maxmem_str && slots_str) ||
2936 (maxmem_str && !slots_str)) {
2937 error_report("invalid -m option value: missing "
2938 "'%s' option", slots_str ? "maxmem" : "slots");
2939 exit(EXIT_FAILURE);
2942 loc_pop(&loc);
2945 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2947 GlobalProperty *g;
2949 g = g_malloc0(sizeof(*g));
2950 g->driver = qemu_opt_get(opts, "driver");
2951 g->property = qemu_opt_get(opts, "property");
2952 g->value = qemu_opt_get(opts, "value");
2953 g->user_provided = true;
2954 g->errp = &error_fatal;
2955 qdev_prop_register_global(g);
2956 return 0;
2959 static int qemu_read_default_config_file(void)
2961 int ret;
2963 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2964 if (ret < 0 && ret != -ENOENT) {
2965 return ret;
2968 return 0;
2971 static void user_register_global_props(void)
2973 qemu_opts_foreach(qemu_find_opts("global"),
2974 global_init_func, NULL, NULL);
2978 * Note: we should see that these properties are actually having a
2979 * priority: accel < machine < user. This means e.g. when user
2980 * specifies something in "-global", it'll always be used with highest
2981 * priority than either machine/accelerator compat properties.
2983 static void register_global_properties(MachineState *ms)
2985 accel_register_compat_props(ms->accelerator);
2986 machine_register_compat_props(ms);
2987 user_register_global_props();
2990 int main(int argc, char **argv, char **envp)
2992 int i;
2993 int snapshot, linux_boot;
2994 const char *initrd_filename;
2995 const char *kernel_filename, *kernel_cmdline;
2996 const char *boot_order = NULL;
2997 const char *boot_once = NULL;
2998 DisplayState *ds;
2999 int cyls, heads, secs, translation;
3000 QemuOpts *opts, *machine_opts;
3001 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3002 QemuOptsList *olist;
3003 int optind;
3004 const char *optarg;
3005 const char *loadvm = NULL;
3006 MachineClass *machine_class;
3007 const char *cpu_model;
3008 const char *vga_model = NULL;
3009 const char *qtest_chrdev = NULL;
3010 const char *qtest_log = NULL;
3011 const char *pid_file = NULL;
3012 const char *incoming = NULL;
3013 bool defconfig = true;
3014 bool userconfig = true;
3015 bool nographic = false;
3016 DisplayType display_type = DT_DEFAULT;
3017 int display_remote = 0;
3018 const char *log_mask = NULL;
3019 const char *log_file = NULL;
3020 char *trace_file = NULL;
3021 ram_addr_t maxram_size;
3022 uint64_t ram_slots = 0;
3023 FILE *vmstate_dump_file = NULL;
3024 Error *main_loop_err = NULL;
3025 Error *err = NULL;
3026 bool list_data_dirs = false;
3027 typedef struct BlockdevOptions_queue {
3028 BlockdevOptions *bdo;
3029 Location loc;
3030 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3031 } BlockdevOptions_queue;
3032 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3033 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3035 module_call_init(MODULE_INIT_TRACE);
3037 qemu_init_cpu_list();
3038 qemu_init_cpu_loop();
3039 qemu_mutex_lock_iothread();
3041 atexit(qemu_run_exit_notifiers);
3042 error_set_progname(argv[0]);
3043 qemu_init_exec_dir(argv[0]);
3045 module_call_init(MODULE_INIT_QOM);
3046 monitor_init_qmp_commands();
3048 qemu_add_opts(&qemu_drive_opts);
3049 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3050 qemu_add_drive_opts(&qemu_common_drive_opts);
3051 qemu_add_drive_opts(&qemu_drive_opts);
3052 qemu_add_drive_opts(&bdrv_runtime_opts);
3053 qemu_add_opts(&qemu_chardev_opts);
3054 qemu_add_opts(&qemu_device_opts);
3055 qemu_add_opts(&qemu_netdev_opts);
3056 qemu_add_opts(&qemu_net_opts);
3057 qemu_add_opts(&qemu_rtc_opts);
3058 qemu_add_opts(&qemu_global_opts);
3059 qemu_add_opts(&qemu_mon_opts);
3060 qemu_add_opts(&qemu_trace_opts);
3061 qemu_add_opts(&qemu_option_rom_opts);
3062 qemu_add_opts(&qemu_machine_opts);
3063 qemu_add_opts(&qemu_accel_opts);
3064 qemu_add_opts(&qemu_mem_opts);
3065 qemu_add_opts(&qemu_smp_opts);
3066 qemu_add_opts(&qemu_boot_opts);
3067 qemu_add_opts(&qemu_sandbox_opts);
3068 qemu_add_opts(&qemu_add_fd_opts);
3069 qemu_add_opts(&qemu_object_opts);
3070 qemu_add_opts(&qemu_tpmdev_opts);
3071 qemu_add_opts(&qemu_realtime_opts);
3072 qemu_add_opts(&qemu_msg_opts);
3073 qemu_add_opts(&qemu_name_opts);
3074 qemu_add_opts(&qemu_numa_opts);
3075 qemu_add_opts(&qemu_icount_opts);
3076 qemu_add_opts(&qemu_semihosting_config_opts);
3077 qemu_add_opts(&qemu_fw_cfg_opts);
3078 module_call_init(MODULE_INIT_OPTS);
3080 runstate_init();
3082 if (qcrypto_init(&err) < 0) {
3083 error_reportf_err(err, "cannot initialize crypto: ");
3084 exit(1);
3086 rtc_clock = QEMU_CLOCK_HOST;
3088 QLIST_INIT (&vm_change_state_head);
3089 os_setup_early_signal_handling();
3091 cpu_model = NULL;
3092 snapshot = 0;
3093 cyls = heads = secs = 0;
3094 translation = BIOS_ATA_TRANSLATION_AUTO;
3096 nb_nics = 0;
3098 bdrv_init_with_whitelist();
3100 autostart = 1;
3102 /* first pass of option parsing */
3103 optind = 1;
3104 while (optind < argc) {
3105 if (argv[optind][0] != '-') {
3106 /* disk image */
3107 optind++;
3108 } else {
3109 const QEMUOption *popt;
3111 popt = lookup_opt(argc, argv, &optarg, &optind);
3112 switch (popt->index) {
3113 case QEMU_OPTION_nodefconfig:
3114 defconfig = false;
3115 break;
3116 case QEMU_OPTION_nouserconfig:
3117 userconfig = false;
3118 break;
3123 if (defconfig && userconfig) {
3124 if (qemu_read_default_config_file() < 0) {
3125 exit(1);
3129 /* second pass of option parsing */
3130 optind = 1;
3131 for(;;) {
3132 if (optind >= argc)
3133 break;
3134 if (argv[optind][0] != '-') {
3135 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3136 } else {
3137 const QEMUOption *popt;
3139 popt = lookup_opt(argc, argv, &optarg, &optind);
3140 if (!(popt->arch_mask & arch_type)) {
3141 error_report("Option not supported for this target");
3142 exit(1);
3144 switch(popt->index) {
3145 case QEMU_OPTION_no_kvm_irqchip: {
3146 olist = qemu_find_opts("machine");
3147 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3148 break;
3150 case QEMU_OPTION_cpu:
3151 /* hw initialization will check this */
3152 cpu_model = optarg;
3153 break;
3154 case QEMU_OPTION_hda:
3156 char buf[256];
3157 if (cyls == 0)
3158 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3159 else
3160 snprintf(buf, sizeof(buf),
3161 "%s,cyls=%d,heads=%d,secs=%d%s",
3162 HD_OPTS , cyls, heads, secs,
3163 translation == BIOS_ATA_TRANSLATION_LBA ?
3164 ",trans=lba" :
3165 translation == BIOS_ATA_TRANSLATION_NONE ?
3166 ",trans=none" : "");
3167 drive_add(IF_DEFAULT, 0, optarg, buf);
3168 break;
3170 case QEMU_OPTION_hdb:
3171 case QEMU_OPTION_hdc:
3172 case QEMU_OPTION_hdd:
3173 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3174 HD_OPTS);
3175 break;
3176 case QEMU_OPTION_blockdev:
3178 Visitor *v;
3179 BlockdevOptions_queue *bdo;
3181 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3182 if (!v) {
3183 error_report_err(err);
3184 exit(1);
3187 bdo = g_new(BlockdevOptions_queue, 1);
3188 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3189 &error_fatal);
3190 visit_free(v);
3191 loc_save(&bdo->loc);
3192 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3193 break;
3195 case QEMU_OPTION_drive:
3196 if (drive_def(optarg) == NULL) {
3197 exit(1);
3199 break;
3200 case QEMU_OPTION_set:
3201 if (qemu_set_option(optarg) != 0)
3202 exit(1);
3203 break;
3204 case QEMU_OPTION_global:
3205 if (qemu_global_option(optarg) != 0)
3206 exit(1);
3207 break;
3208 case QEMU_OPTION_mtdblock:
3209 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3210 break;
3211 case QEMU_OPTION_sd:
3212 drive_add(IF_SD, -1, optarg, SD_OPTS);
3213 break;
3214 case QEMU_OPTION_pflash:
3215 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3216 break;
3217 case QEMU_OPTION_snapshot:
3218 snapshot = 1;
3219 break;
3220 case QEMU_OPTION_hdachs:
3222 const char *p;
3223 p = optarg;
3224 cyls = strtol(p, (char **)&p, 0);
3225 if (cyls < 1 || cyls > 16383)
3226 goto chs_fail;
3227 if (*p != ',')
3228 goto chs_fail;
3229 p++;
3230 heads = strtol(p, (char **)&p, 0);
3231 if (heads < 1 || heads > 16)
3232 goto chs_fail;
3233 if (*p != ',')
3234 goto chs_fail;
3235 p++;
3236 secs = strtol(p, (char **)&p, 0);
3237 if (secs < 1 || secs > 63)
3238 goto chs_fail;
3239 if (*p == ',') {
3240 p++;
3241 if (!strcmp(p, "large")) {
3242 translation = BIOS_ATA_TRANSLATION_LARGE;
3243 } else if (!strcmp(p, "rechs")) {
3244 translation = BIOS_ATA_TRANSLATION_RECHS;
3245 } else if (!strcmp(p, "none")) {
3246 translation = BIOS_ATA_TRANSLATION_NONE;
3247 } else if (!strcmp(p, "lba")) {
3248 translation = BIOS_ATA_TRANSLATION_LBA;
3249 } else if (!strcmp(p, "auto")) {
3250 translation = BIOS_ATA_TRANSLATION_AUTO;
3251 } else {
3252 goto chs_fail;
3254 } else if (*p != '\0') {
3255 chs_fail:
3256 error_report("invalid physical CHS format");
3257 exit(1);
3259 if (hda_opts != NULL) {
3260 qemu_opt_set_number(hda_opts, "cyls", cyls,
3261 &error_abort);
3262 qemu_opt_set_number(hda_opts, "heads", heads,
3263 &error_abort);
3264 qemu_opt_set_number(hda_opts, "secs", secs,
3265 &error_abort);
3266 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3267 qemu_opt_set(hda_opts, "trans", "large",
3268 &error_abort);
3269 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3270 qemu_opt_set(hda_opts, "trans", "rechs",
3271 &error_abort);
3272 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3273 qemu_opt_set(hda_opts, "trans", "lba",
3274 &error_abort);
3275 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3276 qemu_opt_set(hda_opts, "trans", "none",
3277 &error_abort);
3281 error_report("'-hdachs' is deprecated, please use '-device"
3282 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3283 break;
3284 case QEMU_OPTION_numa:
3285 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3286 optarg, true);
3287 if (!opts) {
3288 exit(1);
3290 break;
3291 case QEMU_OPTION_display:
3292 display_type = select_display(optarg);
3293 break;
3294 case QEMU_OPTION_nographic:
3295 olist = qemu_find_opts("machine");
3296 qemu_opts_parse_noisily(olist, "graphics=off", false);
3297 nographic = true;
3298 display_type = DT_NONE;
3299 break;
3300 case QEMU_OPTION_curses:
3301 #ifdef CONFIG_CURSES
3302 display_type = DT_CURSES;
3303 #else
3304 error_report("curses support is disabled");
3305 exit(1);
3306 #endif
3307 break;
3308 case QEMU_OPTION_portrait:
3309 graphic_rotate = 90;
3310 break;
3311 case QEMU_OPTION_rotate:
3312 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3313 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3314 graphic_rotate != 180 && graphic_rotate != 270) {
3315 error_report("only 90, 180, 270 deg rotation is available");
3316 exit(1);
3318 break;
3319 case QEMU_OPTION_kernel:
3320 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3321 &error_abort);
3322 break;
3323 case QEMU_OPTION_initrd:
3324 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3325 &error_abort);
3326 break;
3327 case QEMU_OPTION_append:
3328 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3329 &error_abort);
3330 break;
3331 case QEMU_OPTION_dtb:
3332 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3333 &error_abort);
3334 break;
3335 case QEMU_OPTION_cdrom:
3336 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3337 break;
3338 case QEMU_OPTION_boot:
3339 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3340 optarg, true);
3341 if (!opts) {
3342 exit(1);
3344 break;
3345 case QEMU_OPTION_fda:
3346 case QEMU_OPTION_fdb:
3347 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3348 optarg, FD_OPTS);
3349 break;
3350 case QEMU_OPTION_no_fd_bootchk:
3351 fd_bootchk = 0;
3352 break;
3353 case QEMU_OPTION_netdev:
3354 default_net = 0;
3355 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3356 exit(1);
3358 break;
3359 case QEMU_OPTION_net:
3360 default_net = 0;
3361 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3362 exit(1);
3364 break;
3365 #ifdef CONFIG_LIBISCSI
3366 case QEMU_OPTION_iscsi:
3367 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3368 optarg, false);
3369 if (!opts) {
3370 exit(1);
3372 break;
3373 #endif
3374 #ifdef CONFIG_SLIRP
3375 case QEMU_OPTION_tftp:
3376 error_report("The -tftp option is deprecated. "
3377 "Please use '-netdev user,tftp=...' instead.");
3378 legacy_tftp_prefix = optarg;
3379 break;
3380 case QEMU_OPTION_bootp:
3381 error_report("The -bootp option is deprecated. "
3382 "Please use '-netdev user,bootfile=...' instead.");
3383 legacy_bootp_filename = optarg;
3384 break;
3385 case QEMU_OPTION_redir:
3386 error_report("The -redir option is deprecated. "
3387 "Please use '-netdev user,hostfwd=...' instead.");
3388 if (net_slirp_redir(optarg) < 0)
3389 exit(1);
3390 break;
3391 #endif
3392 case QEMU_OPTION_bt:
3393 add_device_config(DEV_BT, optarg);
3394 break;
3395 case QEMU_OPTION_audio_help:
3396 AUD_help ();
3397 exit (0);
3398 break;
3399 case QEMU_OPTION_soundhw:
3400 select_soundhw (optarg);
3401 break;
3402 case QEMU_OPTION_h:
3403 help(0);
3404 break;
3405 case QEMU_OPTION_version:
3406 version();
3407 exit(0);
3408 break;
3409 case QEMU_OPTION_m:
3410 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3411 optarg, true);
3412 if (!opts) {
3413 exit(EXIT_FAILURE);
3415 break;
3416 #ifdef CONFIG_TPM
3417 case QEMU_OPTION_tpmdev:
3418 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3419 exit(1);
3421 break;
3422 #endif
3423 case QEMU_OPTION_mempath:
3424 mem_path = optarg;
3425 break;
3426 case QEMU_OPTION_mem_prealloc:
3427 mem_prealloc = 1;
3428 break;
3429 case QEMU_OPTION_d:
3430 log_mask = optarg;
3431 break;
3432 case QEMU_OPTION_D:
3433 log_file = optarg;
3434 break;
3435 case QEMU_OPTION_DFILTER:
3436 qemu_set_dfilter_ranges(optarg, &error_fatal);
3437 break;
3438 case QEMU_OPTION_s:
3439 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3440 break;
3441 case QEMU_OPTION_gdb:
3442 add_device_config(DEV_GDB, optarg);
3443 break;
3444 case QEMU_OPTION_L:
3445 if (is_help_option(optarg)) {
3446 list_data_dirs = true;
3447 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3448 data_dir[data_dir_idx++] = optarg;
3450 break;
3451 case QEMU_OPTION_bios:
3452 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3453 &error_abort);
3454 break;
3455 case QEMU_OPTION_singlestep:
3456 singlestep = 1;
3457 break;
3458 case QEMU_OPTION_S:
3459 autostart = 0;
3460 break;
3461 case QEMU_OPTION_k:
3462 keyboard_layout = optarg;
3463 break;
3464 case QEMU_OPTION_localtime:
3465 rtc_utc = 0;
3466 break;
3467 case QEMU_OPTION_vga:
3468 vga_model = optarg;
3469 default_vga = 0;
3470 break;
3471 case QEMU_OPTION_g:
3473 const char *p;
3474 int w, h, depth;
3475 p = optarg;
3476 w = strtol(p, (char **)&p, 10);
3477 if (w <= 0) {
3478 graphic_error:
3479 error_report("invalid resolution or depth");
3480 exit(1);
3482 if (*p != 'x')
3483 goto graphic_error;
3484 p++;
3485 h = strtol(p, (char **)&p, 10);
3486 if (h <= 0)
3487 goto graphic_error;
3488 if (*p == 'x') {
3489 p++;
3490 depth = strtol(p, (char **)&p, 10);
3491 if (depth != 8 && depth != 15 && depth != 16 &&
3492 depth != 24 && depth != 32)
3493 goto graphic_error;
3494 } else if (*p == '\0') {
3495 depth = graphic_depth;
3496 } else {
3497 goto graphic_error;
3500 graphic_width = w;
3501 graphic_height = h;
3502 graphic_depth = depth;
3504 break;
3505 case QEMU_OPTION_echr:
3507 char *r;
3508 term_escape_char = strtol(optarg, &r, 0);
3509 if (r == optarg)
3510 printf("Bad argument to echr\n");
3511 break;
3513 case QEMU_OPTION_monitor:
3514 default_monitor = 0;
3515 if (strncmp(optarg, "none", 4)) {
3516 monitor_parse(optarg, "readline", false);
3518 break;
3519 case QEMU_OPTION_qmp:
3520 monitor_parse(optarg, "control", false);
3521 default_monitor = 0;
3522 break;
3523 case QEMU_OPTION_qmp_pretty:
3524 monitor_parse(optarg, "control", true);
3525 default_monitor = 0;
3526 break;
3527 case QEMU_OPTION_mon:
3528 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3529 true);
3530 if (!opts) {
3531 exit(1);
3533 default_monitor = 0;
3534 break;
3535 case QEMU_OPTION_chardev:
3536 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3537 optarg, true);
3538 if (!opts) {
3539 exit(1);
3541 break;
3542 case QEMU_OPTION_fsdev:
3543 olist = qemu_find_opts("fsdev");
3544 if (!olist) {
3545 error_report("fsdev support is disabled");
3546 exit(1);
3548 opts = qemu_opts_parse_noisily(olist, optarg, true);
3549 if (!opts) {
3550 exit(1);
3552 break;
3553 case QEMU_OPTION_virtfs: {
3554 QemuOpts *fsdev;
3555 QemuOpts *device;
3556 const char *writeout, *sock_fd, *socket;
3558 olist = qemu_find_opts("virtfs");
3559 if (!olist) {
3560 error_report("virtfs support is disabled");
3561 exit(1);
3563 opts = qemu_opts_parse_noisily(olist, optarg, true);
3564 if (!opts) {
3565 exit(1);
3568 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3569 qemu_opt_get(opts, "mount_tag") == NULL) {
3570 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3571 exit(1);
3573 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3574 qemu_opts_id(opts) ?:
3575 qemu_opt_get(opts, "mount_tag"),
3576 1, NULL);
3577 if (!fsdev) {
3578 error_report("duplicate or invalid fsdev id: %s",
3579 qemu_opt_get(opts, "mount_tag"));
3580 exit(1);
3583 writeout = qemu_opt_get(opts, "writeout");
3584 if (writeout) {
3585 #ifdef CONFIG_SYNC_FILE_RANGE
3586 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3587 #else
3588 error_report("writeout=immediate not supported "
3589 "on this platform");
3590 exit(1);
3591 #endif
3593 qemu_opt_set(fsdev, "fsdriver",
3594 qemu_opt_get(opts, "fsdriver"), &error_abort);
3595 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3596 &error_abort);
3597 qemu_opt_set(fsdev, "security_model",
3598 qemu_opt_get(opts, "security_model"),
3599 &error_abort);
3600 socket = qemu_opt_get(opts, "socket");
3601 if (socket) {
3602 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3604 sock_fd = qemu_opt_get(opts, "sock_fd");
3605 if (sock_fd) {
3606 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3609 qemu_opt_set_bool(fsdev, "readonly",
3610 qemu_opt_get_bool(opts, "readonly", 0),
3611 &error_abort);
3612 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3613 &error_abort);
3614 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3615 qemu_opt_set(device, "fsdev",
3616 qemu_opts_id(fsdev), &error_abort);
3617 qemu_opt_set(device, "mount_tag",
3618 qemu_opt_get(opts, "mount_tag"), &error_abort);
3619 break;
3621 case QEMU_OPTION_virtfs_synth: {
3622 QemuOpts *fsdev;
3623 QemuOpts *device;
3625 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3626 1, NULL);
3627 if (!fsdev) {
3628 error_report("duplicate option: %s", "virtfs_synth");
3629 exit(1);
3631 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3633 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3634 &error_abort);
3635 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3636 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3637 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3638 break;
3640 case QEMU_OPTION_serial:
3641 add_device_config(DEV_SERIAL, optarg);
3642 default_serial = 0;
3643 if (strncmp(optarg, "mon:", 4) == 0) {
3644 default_monitor = 0;
3646 break;
3647 case QEMU_OPTION_watchdog:
3648 if (watchdog) {
3649 error_report("only one watchdog option may be given");
3650 return 1;
3652 watchdog = optarg;
3653 break;
3654 case QEMU_OPTION_watchdog_action:
3655 if (select_watchdog_action(optarg) == -1) {
3656 error_report("unknown -watchdog-action parameter");
3657 exit(1);
3659 break;
3660 case QEMU_OPTION_virtiocon:
3661 add_device_config(DEV_VIRTCON, optarg);
3662 default_virtcon = 0;
3663 if (strncmp(optarg, "mon:", 4) == 0) {
3664 default_monitor = 0;
3666 break;
3667 case QEMU_OPTION_parallel:
3668 add_device_config(DEV_PARALLEL, optarg);
3669 default_parallel = 0;
3670 if (strncmp(optarg, "mon:", 4) == 0) {
3671 default_monitor = 0;
3673 break;
3674 case QEMU_OPTION_debugcon:
3675 add_device_config(DEV_DEBUGCON, optarg);
3676 break;
3677 case QEMU_OPTION_loadvm:
3678 loadvm = optarg;
3679 break;
3680 case QEMU_OPTION_full_screen:
3681 full_screen = 1;
3682 break;
3683 case QEMU_OPTION_no_frame:
3684 no_frame = 1;
3685 break;
3686 case QEMU_OPTION_alt_grab:
3687 alt_grab = 1;
3688 break;
3689 case QEMU_OPTION_ctrl_grab:
3690 ctrl_grab = 1;
3691 break;
3692 case QEMU_OPTION_no_quit:
3693 no_quit = 1;
3694 break;
3695 case QEMU_OPTION_sdl:
3696 #ifdef CONFIG_SDL
3697 display_type = DT_SDL;
3698 break;
3699 #else
3700 error_report("SDL support is disabled");
3701 exit(1);
3702 #endif
3703 case QEMU_OPTION_pidfile:
3704 pid_file = optarg;
3705 break;
3706 case QEMU_OPTION_win2k_hack:
3707 win2k_install_hack = 1;
3708 break;
3709 case QEMU_OPTION_rtc_td_hack: {
3710 static GlobalProperty slew_lost_ticks = {
3711 .driver = "mc146818rtc",
3712 .property = "lost_tick_policy",
3713 .value = "slew",
3716 qdev_prop_register_global(&slew_lost_ticks);
3717 break;
3719 case QEMU_OPTION_acpitable:
3720 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3721 optarg, true);
3722 if (!opts) {
3723 exit(1);
3725 acpi_table_add(opts, &error_fatal);
3726 break;
3727 case QEMU_OPTION_smbios:
3728 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3729 optarg, false);
3730 if (!opts) {
3731 exit(1);
3733 smbios_entry_add(opts, &error_fatal);
3734 break;
3735 case QEMU_OPTION_fwcfg:
3736 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3737 optarg, true);
3738 if (opts == NULL) {
3739 exit(1);
3741 break;
3742 case QEMU_OPTION_enable_kvm:
3743 olist = qemu_find_opts("machine");
3744 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3745 break;
3746 case QEMU_OPTION_enable_hax:
3747 olist = qemu_find_opts("machine");
3748 qemu_opts_parse_noisily(olist, "accel=hax", false);
3749 break;
3750 case QEMU_OPTION_M:
3751 case QEMU_OPTION_machine:
3752 olist = qemu_find_opts("machine");
3753 opts = qemu_opts_parse_noisily(olist, optarg, true);
3754 if (!opts) {
3755 exit(1);
3757 break;
3758 case QEMU_OPTION_no_kvm:
3759 olist = qemu_find_opts("machine");
3760 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3761 break;
3762 case QEMU_OPTION_no_kvm_pit: {
3763 warn_report("ignoring deprecated option");
3764 break;
3766 case QEMU_OPTION_no_kvm_pit_reinjection: {
3767 static GlobalProperty kvm_pit_lost_tick_policy = {
3768 .driver = "kvm-pit",
3769 .property = "lost_tick_policy",
3770 .value = "discard",
3773 warn_report("deprecated, replaced by "
3774 "-global kvm-pit.lost_tick_policy=discard");
3775 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3776 break;
3778 case QEMU_OPTION_accel:
3779 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3780 optarg, true);
3781 optarg = qemu_opt_get(accel_opts, "accel");
3782 if (!optarg || is_help_option(optarg)) {
3783 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3784 exit(0);
3786 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3787 false, &error_abort);
3788 qemu_opt_set(opts, "accel", optarg, &error_abort);
3789 break;
3790 case QEMU_OPTION_usb:
3791 olist = qemu_find_opts("machine");
3792 qemu_opts_parse_noisily(olist, "usb=on", false);
3793 break;
3794 case QEMU_OPTION_usbdevice:
3795 error_report("'-usbdevice' is deprecated, please use "
3796 "'-device usb-...' instead");
3797 olist = qemu_find_opts("machine");
3798 qemu_opts_parse_noisily(olist, "usb=on", false);
3799 add_device_config(DEV_USB, optarg);
3800 break;
3801 case QEMU_OPTION_device:
3802 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3803 optarg, true)) {
3804 exit(1);
3806 break;
3807 case QEMU_OPTION_smp:
3808 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3809 optarg, true)) {
3810 exit(1);
3812 break;
3813 case QEMU_OPTION_vnc:
3814 vnc_parse(optarg, &error_fatal);
3815 break;
3816 case QEMU_OPTION_no_acpi:
3817 acpi_enabled = 0;
3818 break;
3819 case QEMU_OPTION_no_hpet:
3820 no_hpet = 1;
3821 break;
3822 case QEMU_OPTION_balloon:
3823 if (balloon_parse(optarg) < 0) {
3824 error_report("unknown -balloon argument %s", optarg);
3825 exit(1);
3827 break;
3828 case QEMU_OPTION_no_reboot:
3829 no_reboot = 1;
3830 break;
3831 case QEMU_OPTION_no_shutdown:
3832 no_shutdown = 1;
3833 break;
3834 case QEMU_OPTION_show_cursor:
3835 cursor_hide = 0;
3836 break;
3837 case QEMU_OPTION_uuid:
3838 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3839 error_report("failed to parse UUID string: wrong format");
3840 exit(1);
3842 qemu_uuid_set = true;
3843 break;
3844 case QEMU_OPTION_option_rom:
3845 if (nb_option_roms >= MAX_OPTION_ROMS) {
3846 error_report("too many option ROMs");
3847 exit(1);
3849 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3850 optarg, true);
3851 if (!opts) {
3852 exit(1);
3854 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3855 option_rom[nb_option_roms].bootindex =
3856 qemu_opt_get_number(opts, "bootindex", -1);
3857 if (!option_rom[nb_option_roms].name) {
3858 error_report("Option ROM file is not specified");
3859 exit(1);
3861 nb_option_roms++;
3862 break;
3863 case QEMU_OPTION_semihosting:
3864 semihosting.enabled = true;
3865 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3866 break;
3867 case QEMU_OPTION_semihosting_config:
3868 semihosting.enabled = true;
3869 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3870 optarg, false);
3871 if (opts != NULL) {
3872 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3873 true);
3874 const char *target = qemu_opt_get(opts, "target");
3875 if (target != NULL) {
3876 if (strcmp("native", target) == 0) {
3877 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3878 } else if (strcmp("gdb", target) == 0) {
3879 semihosting.target = SEMIHOSTING_TARGET_GDB;
3880 } else if (strcmp("auto", target) == 0) {
3881 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3882 } else {
3883 error_report("unsupported semihosting-config %s",
3884 optarg);
3885 exit(1);
3887 } else {
3888 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3890 /* Set semihosting argument count and vector */
3891 qemu_opt_foreach(opts, add_semihosting_arg,
3892 &semihosting, NULL);
3893 } else {
3894 error_report("unsupported semihosting-config %s", optarg);
3895 exit(1);
3897 break;
3898 case QEMU_OPTION_tdf:
3899 warn_report("ignoring deprecated option");
3900 break;
3901 case QEMU_OPTION_name:
3902 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3903 optarg, true);
3904 if (!opts) {
3905 exit(1);
3907 break;
3908 case QEMU_OPTION_prom_env:
3909 if (nb_prom_envs >= MAX_PROM_ENVS) {
3910 error_report("too many prom variables");
3911 exit(1);
3913 prom_envs[nb_prom_envs] = optarg;
3914 nb_prom_envs++;
3915 break;
3916 case QEMU_OPTION_old_param:
3917 old_param = 1;
3918 break;
3919 case QEMU_OPTION_clock:
3920 /* Clock options no longer exist. Keep this option for
3921 * backward compatibility.
3923 break;
3924 case QEMU_OPTION_startdate:
3925 configure_rtc_date_offset(optarg, 1);
3926 break;
3927 case QEMU_OPTION_rtc:
3928 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3929 false);
3930 if (!opts) {
3931 exit(1);
3933 configure_rtc(opts);
3934 break;
3935 case QEMU_OPTION_tb_size:
3936 #ifndef CONFIG_TCG
3937 error_report("TCG is disabled");
3938 exit(1);
3939 #endif
3940 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3941 error_report("Invalid argument to -tb-size");
3942 exit(1);
3944 break;
3945 case QEMU_OPTION_icount:
3946 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3947 optarg, true);
3948 if (!icount_opts) {
3949 exit(1);
3951 break;
3952 case QEMU_OPTION_incoming:
3953 if (!incoming) {
3954 runstate_set(RUN_STATE_INMIGRATE);
3956 incoming = optarg;
3957 break;
3958 case QEMU_OPTION_only_migratable:
3960 * TODO: we can remove this option one day, and we
3961 * should all use:
3963 * "-global migration.only-migratable=true"
3965 qemu_global_option("migration.only-migratable=true");
3966 break;
3967 case QEMU_OPTION_nodefaults:
3968 has_defaults = 0;
3969 break;
3970 case QEMU_OPTION_xen_domid:
3971 if (!(xen_available())) {
3972 error_report("Option not supported for this target");
3973 exit(1);
3975 xen_domid = atoi(optarg);
3976 break;
3977 case QEMU_OPTION_xen_create:
3978 if (!(xen_available())) {
3979 error_report("Option not supported for this target");
3980 exit(1);
3982 xen_mode = XEN_CREATE;
3983 break;
3984 case QEMU_OPTION_xen_attach:
3985 if (!(xen_available())) {
3986 error_report("Option not supported for this target");
3987 exit(1);
3989 xen_mode = XEN_ATTACH;
3990 break;
3991 case QEMU_OPTION_xen_domid_restrict:
3992 if (!(xen_available())) {
3993 error_report("Option not supported for this target");
3994 exit(1);
3996 xen_domid_restrict = true;
3997 break;
3998 case QEMU_OPTION_trace:
3999 g_free(trace_file);
4000 trace_file = trace_opt_parse(optarg);
4001 break;
4002 case QEMU_OPTION_readconfig:
4004 int ret = qemu_read_config_file(optarg);
4005 if (ret < 0) {
4006 error_report("read config %s: %s", optarg,
4007 strerror(-ret));
4008 exit(1);
4010 break;
4012 case QEMU_OPTION_spice:
4013 olist = qemu_find_opts("spice");
4014 if (!olist) {
4015 error_report("spice support is disabled");
4016 exit(1);
4018 opts = qemu_opts_parse_noisily(olist, optarg, false);
4019 if (!opts) {
4020 exit(1);
4022 display_remote++;
4023 break;
4024 case QEMU_OPTION_writeconfig:
4026 FILE *fp;
4027 if (strcmp(optarg, "-") == 0) {
4028 fp = stdout;
4029 } else {
4030 fp = fopen(optarg, "w");
4031 if (fp == NULL) {
4032 error_report("open %s: %s", optarg,
4033 strerror(errno));
4034 exit(1);
4037 qemu_config_write(fp);
4038 if (fp != stdout) {
4039 fclose(fp);
4041 break;
4043 case QEMU_OPTION_qtest:
4044 qtest_chrdev = optarg;
4045 break;
4046 case QEMU_OPTION_qtest_log:
4047 qtest_log = optarg;
4048 break;
4049 case QEMU_OPTION_sandbox:
4050 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4051 optarg, true);
4052 if (!opts) {
4053 exit(1);
4055 break;
4056 case QEMU_OPTION_add_fd:
4057 #ifndef _WIN32
4058 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4059 optarg, false);
4060 if (!opts) {
4061 exit(1);
4063 #else
4064 error_report("File descriptor passing is disabled on this "
4065 "platform");
4066 exit(1);
4067 #endif
4068 break;
4069 case QEMU_OPTION_object:
4070 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4071 optarg, true);
4072 if (!opts) {
4073 exit(1);
4075 break;
4076 case QEMU_OPTION_realtime:
4077 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4078 optarg, false);
4079 if (!opts) {
4080 exit(1);
4082 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4083 break;
4084 case QEMU_OPTION_msg:
4085 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4086 false);
4087 if (!opts) {
4088 exit(1);
4090 configure_msg(opts);
4091 break;
4092 case QEMU_OPTION_dump_vmstate:
4093 if (vmstate_dump_file) {
4094 error_report("only one '-dump-vmstate' "
4095 "option may be given");
4096 exit(1);
4098 vmstate_dump_file = fopen(optarg, "w");
4099 if (vmstate_dump_file == NULL) {
4100 error_report("open %s: %s", optarg, strerror(errno));
4101 exit(1);
4103 break;
4104 default:
4105 os_parse_cmd_args(popt->index, optarg);
4110 * Clear error location left behind by the loop.
4111 * Best done right after the loop. Do not insert code here!
4113 loc_set_none();
4115 replay_configure(icount_opts);
4117 machine_class = select_machine();
4119 set_memory_options(&ram_slots, &maxram_size, machine_class);
4121 os_daemonize();
4123 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4124 error_report("could not acquire pid file: %s", strerror(errno));
4125 exit(1);
4128 if (qemu_init_main_loop(&main_loop_err)) {
4129 error_report_err(main_loop_err);
4130 exit(1);
4133 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4134 parse_sandbox, NULL, NULL)) {
4135 exit(1);
4138 if (qemu_opts_foreach(qemu_find_opts("name"),
4139 parse_name, NULL, NULL)) {
4140 exit(1);
4143 #ifndef _WIN32
4144 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4145 parse_add_fd, NULL, NULL)) {
4146 exit(1);
4149 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4150 cleanup_add_fd, NULL, NULL)) {
4151 exit(1);
4153 #endif
4155 current_machine = MACHINE(object_new(object_class_get_name(
4156 OBJECT_CLASS(machine_class))));
4157 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4158 exit(0);
4160 object_property_add_child(object_get_root(), "machine",
4161 OBJECT(current_machine), &error_abort);
4163 if (machine_class->minimum_page_bits) {
4164 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4165 /* This would be a board error: specifying a minimum smaller than
4166 * a target's compile-time fixed setting.
4168 g_assert_not_reached();
4172 cpu_exec_init_all();
4174 if (machine_class->hw_version) {
4175 qemu_set_hw_version(machine_class->hw_version);
4178 if (cpu_model && is_help_option(cpu_model)) {
4179 list_cpus(stdout, &fprintf, cpu_model);
4180 exit(0);
4183 if (!trace_init_backends()) {
4184 exit(1);
4186 trace_init_file(trace_file);
4188 /* Open the logfile at this point and set the log mask if necessary.
4190 if (log_file) {
4191 qemu_set_log_filename(log_file, &error_fatal);
4194 if (log_mask) {
4195 int mask;
4196 mask = qemu_str_to_log_mask(log_mask);
4197 if (!mask) {
4198 qemu_print_log_usage(stdout);
4199 exit(1);
4201 qemu_set_log(mask);
4202 } else {
4203 qemu_set_log(0);
4206 /* If no data_dir is specified then try to find it relative to the
4207 executable path. */
4208 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4209 data_dir[data_dir_idx] = os_find_datadir();
4210 if (data_dir[data_dir_idx] != NULL) {
4211 data_dir_idx++;
4214 /* If all else fails use the install path specified when building. */
4215 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4216 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4219 /* -L help lists the data directories and exits. */
4220 if (list_data_dirs) {
4221 for (i = 0; i < data_dir_idx; i++) {
4222 printf("%s\n", data_dir[i]);
4224 exit(0);
4227 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4229 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4230 if (max_cpus > machine_class->max_cpus) {
4231 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4232 "supported by machine '%s' (%d)", max_cpus,
4233 machine_class->name, machine_class->max_cpus);
4234 exit(1);
4238 * Get the default machine options from the machine if it is not already
4239 * specified either by the configuration file or by the command line.
4241 if (machine_class->default_machine_opts) {
4242 qemu_opts_set_defaults(qemu_find_opts("machine"),
4243 machine_class->default_machine_opts, 0);
4246 qemu_opts_foreach(qemu_find_opts("device"),
4247 default_driver_check, NULL, NULL);
4248 qemu_opts_foreach(qemu_find_opts("global"),
4249 default_driver_check, NULL, NULL);
4251 if (!vga_model && !default_vga) {
4252 vga_interface_type = VGA_DEVICE;
4254 if (!has_defaults || machine_class->no_serial) {
4255 default_serial = 0;
4257 if (!has_defaults || machine_class->no_parallel) {
4258 default_parallel = 0;
4260 if (!has_defaults || !machine_class->use_virtcon) {
4261 default_virtcon = 0;
4263 if (!has_defaults || !machine_class->use_sclp) {
4264 default_sclp = 0;
4266 if (!has_defaults || machine_class->no_floppy) {
4267 default_floppy = 0;
4269 if (!has_defaults || machine_class->no_cdrom) {
4270 default_cdrom = 0;
4272 if (!has_defaults || machine_class->no_sdcard) {
4273 default_sdcard = 0;
4275 if (!has_defaults) {
4276 default_monitor = 0;
4277 default_net = 0;
4278 default_vga = 0;
4281 if (is_daemonized()) {
4282 /* According to documentation and historically, -nographic redirects
4283 * serial port, parallel port and monitor to stdio, which does not work
4284 * with -daemonize. We can redirect these to null instead, but since
4285 * -nographic is legacy, let's just error out.
4286 * We disallow -nographic only if all other ports are not redirected
4287 * explicitly, to not break existing legacy setups which uses
4288 * -nographic _and_ redirects all ports explicitly - this is valid
4289 * usage, -nographic is just a no-op in this case.
4291 if (nographic
4292 && (default_parallel || default_serial
4293 || default_monitor || default_virtcon)) {
4294 error_report("-nographic cannot be used with -daemonize");
4295 exit(1);
4297 #ifdef CONFIG_CURSES
4298 if (display_type == DT_CURSES) {
4299 error_report("curses display cannot be used with -daemonize");
4300 exit(1);
4302 #endif
4305 if (nographic) {
4306 if (default_parallel)
4307 add_device_config(DEV_PARALLEL, "null");
4308 if (default_serial && default_monitor) {
4309 add_device_config(DEV_SERIAL, "mon:stdio");
4310 } else if (default_virtcon && default_monitor) {
4311 add_device_config(DEV_VIRTCON, "mon:stdio");
4312 } else if (default_sclp && default_monitor) {
4313 add_device_config(DEV_SCLP, "mon:stdio");
4314 } else {
4315 if (default_serial)
4316 add_device_config(DEV_SERIAL, "stdio");
4317 if (default_virtcon)
4318 add_device_config(DEV_VIRTCON, "stdio");
4319 if (default_sclp) {
4320 add_device_config(DEV_SCLP, "stdio");
4322 if (default_monitor)
4323 monitor_parse("stdio", "readline", false);
4325 } else {
4326 if (default_serial)
4327 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4328 if (default_parallel)
4329 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4330 if (default_monitor)
4331 monitor_parse("vc:80Cx24C", "readline", false);
4332 if (default_virtcon)
4333 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4334 if (default_sclp) {
4335 add_device_config(DEV_SCLP, "vc:80Cx24C");
4339 #if defined(CONFIG_VNC)
4340 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4341 display_remote++;
4343 #endif
4344 if (display_type == DT_DEFAULT && !display_remote) {
4345 #if defined(CONFIG_GTK)
4346 display_type = DT_GTK;
4347 #elif defined(CONFIG_SDL)
4348 display_type = DT_SDL;
4349 #elif defined(CONFIG_COCOA)
4350 display_type = DT_COCOA;
4351 #elif defined(CONFIG_VNC)
4352 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4353 #else
4354 display_type = DT_NONE;
4355 #endif
4358 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4359 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4360 "for SDL, ignoring option");
4362 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4363 error_report("-no-quit is only valid for GTK and SDL, "
4364 "ignoring option");
4367 if (display_type == DT_GTK) {
4368 early_gtk_display_init(request_opengl);
4371 if (display_type == DT_SDL) {
4372 sdl_display_early_init(request_opengl);
4375 qemu_console_early_init();
4377 if (request_opengl == 1 && display_opengl == 0) {
4378 #if defined(CONFIG_OPENGL)
4379 error_report("OpenGL is not supported by the display");
4380 #else
4381 error_report("OpenGL support is disabled");
4382 #endif
4383 exit(1);
4386 page_size_init();
4387 socket_init();
4389 if (qemu_opts_foreach(qemu_find_opts("object"),
4390 user_creatable_add_opts_foreach,
4391 object_create_initial, NULL)) {
4392 exit(1);
4395 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4396 chardev_init_func, NULL, NULL)) {
4397 exit(1);
4400 #ifdef CONFIG_VIRTFS
4401 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4402 fsdev_init_func, NULL, NULL)) {
4403 exit(1);
4405 #endif
4407 if (qemu_opts_foreach(qemu_find_opts("device"),
4408 device_help_func, NULL, NULL)) {
4409 exit(0);
4412 machine_opts = qemu_get_machine_opts();
4413 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4414 NULL)) {
4415 object_unref(OBJECT(current_machine));
4416 exit(1);
4419 configure_accelerator(current_machine);
4422 * Register all the global properties, including accel properties,
4423 * machine properties, and user-specified ones.
4425 register_global_properties(current_machine);
4428 * Migration object can only be created after global properties
4429 * are applied correctly.
4431 migration_object_init();
4433 if (qtest_chrdev) {
4434 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4437 machine_opts = qemu_get_machine_opts();
4438 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4439 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4440 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4441 bios_name = qemu_opt_get(machine_opts, "firmware");
4443 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4444 if (opts) {
4445 boot_order = qemu_opt_get(opts, "order");
4446 if (boot_order) {
4447 validate_bootdevices(boot_order, &error_fatal);
4450 boot_once = qemu_opt_get(opts, "once");
4451 if (boot_once) {
4452 validate_bootdevices(boot_once, &error_fatal);
4455 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4456 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4459 if (!boot_order) {
4460 boot_order = machine_class->default_boot_order;
4463 if (!kernel_cmdline) {
4464 kernel_cmdline = "";
4465 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4468 linux_boot = (kernel_filename != NULL);
4470 if (!linux_boot && *kernel_cmdline != '\0') {
4471 error_report("-append only allowed with -kernel option");
4472 exit(1);
4475 if (!linux_boot && initrd_filename != NULL) {
4476 error_report("-initrd only allowed with -kernel option");
4477 exit(1);
4480 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4481 /* fall back to the -kernel/-append */
4482 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4485 os_set_line_buffering();
4487 /* spice needs the timers to be initialized by this point */
4488 qemu_spice_init();
4490 cpu_ticks_init();
4491 if (icount_opts) {
4492 if (!tcg_enabled()) {
4493 error_report("-icount is not allowed with hardware virtualization");
4494 exit(1);
4496 configure_icount(icount_opts, &error_abort);
4497 qemu_opts_del(icount_opts);
4500 if (tcg_enabled()) {
4501 qemu_tcg_configure(accel_opts, &error_fatal);
4504 if (default_net) {
4505 QemuOptsList *net = qemu_find_opts("net");
4506 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4507 #ifdef CONFIG_SLIRP
4508 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4509 #endif
4512 colo_info_init();
4514 if (net_init_clients() < 0) {
4515 exit(1);
4518 if (qemu_opts_foreach(qemu_find_opts("object"),
4519 user_creatable_add_opts_foreach,
4520 object_create_delayed, NULL)) {
4521 exit(1);
4524 #ifdef CONFIG_TPM
4525 if (tpm_init() < 0) {
4526 exit(1);
4528 #endif
4530 /* init the bluetooth world */
4531 if (foreach_device_config(DEV_BT, bt_parse))
4532 exit(1);
4534 if (!xen_enabled()) {
4535 /* On 32-bit hosts, QEMU is limited by virtual address space */
4536 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4537 error_report("at most 2047 MB RAM can be simulated");
4538 exit(1);
4542 blk_mig_init();
4543 ram_mig_init();
4545 /* If the currently selected machine wishes to override the units-per-bus
4546 * property of its default HBA interface type, do so now. */
4547 if (machine_class->units_per_default_bus) {
4548 override_max_devs(machine_class->block_default_type,
4549 machine_class->units_per_default_bus);
4552 /* open the virtual block devices */
4553 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4554 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4556 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4557 loc_push_restore(&bdo->loc);
4558 qmp_blockdev_add(bdo->bdo, &error_fatal);
4559 loc_pop(&bdo->loc);
4560 qapi_free_BlockdevOptions(bdo->bdo);
4561 g_free(bdo);
4563 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4564 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4565 NULL, NULL);
4567 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4568 &machine_class->block_default_type, NULL)) {
4569 exit(1);
4572 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4573 CDROM_OPTS);
4574 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4575 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4577 parse_numa_opts(current_machine);
4579 if (qemu_opts_foreach(qemu_find_opts("mon"),
4580 mon_init_func, NULL, NULL)) {
4581 exit(1);
4584 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4585 exit(1);
4586 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4587 exit(1);
4588 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4589 exit(1);
4590 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4591 exit(1);
4593 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4594 exit(1);
4596 /* If no default VGA is requested, the default is "none". */
4597 if (default_vga) {
4598 if (machine_class->default_display) {
4599 vga_model = machine_class->default_display;
4600 } else if (vga_interface_available(VGA_CIRRUS)) {
4601 vga_model = "cirrus";
4602 } else if (vga_interface_available(VGA_STD)) {
4603 vga_model = "std";
4606 if (vga_model) {
4607 select_vgahw(vga_model);
4610 if (watchdog) {
4611 i = select_watchdog(watchdog);
4612 if (i > 0)
4613 exit (i == 1 ? 1 : 0);
4616 /* This checkpoint is required by replay to separate prior clock
4617 reading from the other reads, because timer polling functions query
4618 clock values from the log. */
4619 replay_checkpoint(CHECKPOINT_INIT);
4620 qdev_machine_init();
4622 current_machine->ram_size = ram_size;
4623 current_machine->maxram_size = maxram_size;
4624 current_machine->ram_slots = ram_slots;
4625 current_machine->boot_order = boot_order;
4626 current_machine->cpu_model = cpu_model;
4628 machine_run_board_init(current_machine);
4630 realtime_init();
4632 soundhw_init();
4634 if (hax_enabled()) {
4635 hax_sync_vcpus();
4638 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4639 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4640 exit(1);
4643 /* init USB devices */
4644 if (machine_usb(current_machine)) {
4645 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4646 exit(1);
4649 /* Check if IGD GFX passthrough. */
4650 igd_gfx_passthru();
4652 /* init generic devices */
4653 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4654 if (qemu_opts_foreach(qemu_find_opts("device"),
4655 device_init_func, NULL, NULL)) {
4656 exit(1);
4659 cpu_synchronize_all_post_init();
4661 rom_reset_order_override();
4664 * Create frontends for -drive if=scsi leftovers.
4665 * Normally, frontends for -drive get created by machine
4666 * initialization for onboard SCSI HBAs. However, we create a few
4667 * more ever since SCSI qdevification, but this is pretty much an
4668 * implementation accident, and deprecated.
4670 scsi_legacy_handle_cmdline();
4672 /* Did we create any drives that we failed to create a device for? */
4673 drive_check_orphaned();
4675 /* Don't warn about the default network setup that you get if
4676 * no command line -net or -netdev options are specified. There
4677 * are two cases that we would otherwise complain about:
4678 * (1) board doesn't support a NIC but the implicit "-net nic"
4679 * requested one
4680 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4681 * sets up a nic that isn't connected to anything.
4683 if (!default_net) {
4684 net_check_clients();
4688 if (boot_once) {
4689 qemu_boot_set(boot_once, &error_fatal);
4690 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4693 ds = init_displaystate();
4695 /* init local displays */
4696 switch (display_type) {
4697 case DT_CURSES:
4698 curses_display_init(ds, full_screen);
4699 break;
4700 case DT_SDL:
4701 sdl_display_init(ds, full_screen, no_frame);
4702 break;
4703 case DT_COCOA:
4704 cocoa_display_init(ds, full_screen);
4705 break;
4706 case DT_GTK:
4707 gtk_display_init(ds, full_screen, grab_on_hover);
4708 break;
4709 default:
4710 break;
4713 /* must be after terminal init, SDL library changes signal handlers */
4714 os_setup_signal_handling();
4716 /* init remote displays */
4717 #ifdef CONFIG_VNC
4718 qemu_opts_foreach(qemu_find_opts("vnc"),
4719 vnc_init_func, NULL, NULL);
4720 #endif
4722 if (using_spice) {
4723 qemu_spice_display_init();
4726 #ifdef CONFIG_OPENGL_DMABUF
4727 if (display_type == DT_EGL) {
4728 egl_headless_init();
4730 #endif
4732 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4733 exit(1);
4736 qdev_machine_creation_done();
4738 /* TODO: once all bus devices are qdevified, this should be done
4739 * when bus is created by qdev.c */
4740 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4741 qemu_run_machine_init_done_notifiers();
4743 if (rom_check_and_register_reset() != 0) {
4744 error_report("rom check and register reset failed");
4745 exit(1);
4748 replay_start();
4750 /* This checkpoint is required by replay to separate prior clock
4751 reading from the other reads, because timer polling functions query
4752 clock values from the log. */
4753 replay_checkpoint(CHECKPOINT_RESET);
4754 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4755 register_global_state();
4756 if (replay_mode != REPLAY_MODE_NONE) {
4757 replay_vmstate_init();
4758 } else if (loadvm) {
4759 Error *local_err = NULL;
4760 if (load_snapshot(loadvm, &local_err) < 0) {
4761 error_report_err(local_err);
4762 autostart = 0;
4766 qdev_prop_check_globals();
4767 if (vmstate_dump_file) {
4768 /* dump and exit */
4769 dump_vmstate_json_to_file(vmstate_dump_file);
4770 return 0;
4773 if (incoming) {
4774 Error *local_err = NULL;
4775 qemu_start_incoming_migration(incoming, &local_err);
4776 if (local_err) {
4777 error_reportf_err(local_err, "-incoming %s: ", incoming);
4778 exit(1);
4780 } else if (autostart) {
4781 vm_start();
4784 os_setup_post();
4786 main_loop();
4787 replay_disable_events();
4788 iothread_stop_all();
4790 bdrv_close_all();
4791 pause_all_vcpus();
4792 res_free();
4794 /* vhost-user must be cleaned up before chardevs. */
4795 net_cleanup();
4796 audio_cleanup();
4797 monitor_cleanup();
4798 qemu_chr_cleanup();
4799 /* TODO: unref root container, check all devices are ok */
4801 return 0;