virtio-net: fix wild pointer when remove virtio-net queues
[qemu.git] / vl.c
blob1512df6e9ef340a8e5b615b9f6015105ce8eb593
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 "sysemu/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/block.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "audio/audio.h"
93 #include "migration/migration.h"
94 #include "sysemu/cpus.h"
95 #include "migration/colo.h"
96 #include "sysemu/kvm.h"
97 #include "sysemu/hax.h"
98 #include "qapi/qobject-input-visitor.h"
99 #include "qapi/qobject-input-visitor.h"
100 #include "qapi-visit.h"
101 #include "qapi/qmp/qjson.h"
102 #include "qemu/option.h"
103 #include "qemu/config-file.h"
104 #include "qemu-options.h"
105 #include "qmp-commands.h"
106 #include "qemu/main-loop.h"
107 #ifdef CONFIG_VIRTFS
108 #include "fsdev/qemu-fsdev.h"
109 #endif
110 #include "sysemu/qtest.h"
112 #include "disas/disas.h"
115 #include "slirp/libslirp.h"
117 #include "trace-root.h"
118 #include "trace/control.h"
119 #include "qemu/queue.h"
120 #include "sysemu/arch_init.h"
122 #include "ui/qemu-spice.h"
123 #include "qapi/string-input-visitor.h"
124 #include "qapi/opts-visitor.h"
125 #include "qom/object_interfaces.h"
126 #include "qapi-event.h"
127 #include "exec/semihost.h"
128 #include "crypto/init.h"
129 #include "sysemu/replay.h"
130 #include "qapi/qmp/qerror.h"
131 #include "sysemu/iothread.h"
133 #define MAX_VIRTIO_CONSOLES 1
134 #define MAX_SCLP_CONSOLES 1
136 static const char *data_dir[16];
137 static int data_dir_idx;
138 const char *bios_name = NULL;
139 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
140 int request_opengl = -1;
141 int display_opengl;
142 const char* keyboard_layout = NULL;
143 ram_addr_t ram_size;
144 const char *mem_path = NULL;
145 int mem_prealloc = 0; /* force preallocation of physical target memory */
146 bool enable_mlock = false;
147 int nb_nics;
148 NICInfo nd_table[MAX_NICS];
149 int autostart;
150 static int rtc_utc = 1;
151 static int rtc_date_offset = -1; /* -1 means no change */
152 QEMUClockType rtc_clock;
153 int vga_interface_type = VGA_NONE;
154 static int full_screen = 0;
155 static int no_frame = 0;
156 int no_quit = 0;
157 static bool grab_on_hover;
158 Chardev *serial_hds[MAX_SERIAL_PORTS];
159 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
160 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
161 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
162 int win2k_install_hack = 0;
163 int singlestep = 0;
164 int smp_cpus = 1;
165 int max_cpus = 1;
166 int smp_cores = 1;
167 int smp_threads = 1;
168 int acpi_enabled = 1;
169 int no_hpet = 0;
170 int fd_bootchk = 1;
171 static int no_reboot;
172 int no_shutdown = 0;
173 int cursor_hide = 1;
174 int graphic_rotate = 0;
175 const char *watchdog;
176 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
177 int nb_option_roms;
178 int old_param = 0;
179 const char *qemu_name;
180 int alt_grab = 0;
181 int ctrl_grab = 0;
182 unsigned int nb_prom_envs = 0;
183 const char *prom_envs[MAX_PROM_ENVS];
184 int boot_menu;
185 bool boot_strict;
186 uint8_t *boot_splash_filedata;
187 size_t boot_splash_filedata_size;
188 uint8_t qemu_extra_params_fw[2];
189 int only_migratable; /* turn it off unless user states otherwise */
191 int icount_align_option;
193 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 QemuUUID qemu_uuid;
197 bool qemu_uuid_set;
199 static NotifierList exit_notifiers =
200 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
202 static NotifierList machine_init_done_notifiers =
203 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
205 bool xen_allowed;
206 uint32_t xen_domid;
207 enum xen_mode xen_mode = XEN_EMULATE;
208 bool xen_domid_restrict;
210 static int has_defaults = 1;
211 static int default_serial = 1;
212 static int default_parallel = 1;
213 static int default_virtcon = 1;
214 static int default_sclp = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
220 static int default_net = 1;
222 static struct {
223 const char *driver;
224 int *flag;
225 } default_list[] = {
226 { .driver = "isa-serial", .flag = &default_serial },
227 { .driver = "isa-parallel", .flag = &default_parallel },
228 { .driver = "isa-fdc", .flag = &default_floppy },
229 { .driver = "floppy", .flag = &default_floppy },
230 { .driver = "ide-cd", .flag = &default_cdrom },
231 { .driver = "ide-hd", .flag = &default_cdrom },
232 { .driver = "ide-drive", .flag = &default_cdrom },
233 { .driver = "scsi-cd", .flag = &default_cdrom },
234 { .driver = "scsi-hd", .flag = &default_cdrom },
235 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
236 { .driver = "virtio-serial", .flag = &default_virtcon },
237 { .driver = "VGA", .flag = &default_vga },
238 { .driver = "isa-vga", .flag = &default_vga },
239 { .driver = "cirrus-vga", .flag = &default_vga },
240 { .driver = "isa-cirrus-vga", .flag = &default_vga },
241 { .driver = "vmware-svga", .flag = &default_vga },
242 { .driver = "qxl-vga", .flag = &default_vga },
243 { .driver = "virtio-vga", .flag = &default_vga },
246 static QemuOptsList qemu_rtc_opts = {
247 .name = "rtc",
248 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
249 .desc = {
251 .name = "base",
252 .type = QEMU_OPT_STRING,
254 .name = "clock",
255 .type = QEMU_OPT_STRING,
257 .name = "driftfix",
258 .type = QEMU_OPT_STRING,
260 { /* end of list */ }
264 static QemuOptsList qemu_sandbox_opts = {
265 .name = "sandbox",
266 .implied_opt_name = "enable",
267 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
268 .desc = {
270 .name = "enable",
271 .type = QEMU_OPT_BOOL,
273 { /* end of list */ }
277 static QemuOptsList qemu_option_rom_opts = {
278 .name = "option-rom",
279 .implied_opt_name = "romfile",
280 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
281 .desc = {
283 .name = "bootindex",
284 .type = QEMU_OPT_NUMBER,
285 }, {
286 .name = "romfile",
287 .type = QEMU_OPT_STRING,
289 { /* end of list */ }
293 static QemuOptsList qemu_machine_opts = {
294 .name = "machine",
295 .implied_opt_name = "type",
296 .merge_lists = true,
297 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
298 .desc = {
300 * no elements => accept any
301 * sanity checking will happen later
302 * when setting machine properties
308 static QemuOptsList qemu_accel_opts = {
309 .name = "accel",
310 .implied_opt_name = "accel",
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
312 .merge_lists = true,
313 .desc = {
315 .name = "accel",
316 .type = QEMU_OPT_STRING,
317 .help = "Select the type of accelerator",
320 .name = "thread",
321 .type = QEMU_OPT_STRING,
322 .help = "Enable/disable multi-threaded TCG",
324 { /* end of list */ }
328 static QemuOptsList qemu_boot_opts = {
329 .name = "boot-opts",
330 .implied_opt_name = "order",
331 .merge_lists = true,
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
333 .desc = {
335 .name = "order",
336 .type = QEMU_OPT_STRING,
337 }, {
338 .name = "once",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "menu",
342 .type = QEMU_OPT_BOOL,
343 }, {
344 .name = "splash",
345 .type = QEMU_OPT_STRING,
346 }, {
347 .name = "splash-time",
348 .type = QEMU_OPT_STRING,
349 }, {
350 .name = "reboot-timeout",
351 .type = QEMU_OPT_STRING,
352 }, {
353 .name = "strict",
354 .type = QEMU_OPT_BOOL,
356 { /*End of list */ }
360 static QemuOptsList qemu_add_fd_opts = {
361 .name = "add-fd",
362 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
363 .desc = {
365 .name = "fd",
366 .type = QEMU_OPT_NUMBER,
367 .help = "file descriptor of which a duplicate is added to fd set",
369 .name = "set",
370 .type = QEMU_OPT_NUMBER,
371 .help = "ID of the fd set to add fd to",
373 .name = "opaque",
374 .type = QEMU_OPT_STRING,
375 .help = "free-form string used to describe fd",
377 { /* end of list */ }
381 static QemuOptsList qemu_object_opts = {
382 .name = "object",
383 .implied_opt_name = "qom-type",
384 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
385 .desc = {
390 static QemuOptsList qemu_tpmdev_opts = {
391 .name = "tpmdev",
392 .implied_opt_name = "type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
394 .desc = {
395 /* options are defined in the TPM backends */
396 { /* end of list */ }
400 static QemuOptsList qemu_realtime_opts = {
401 .name = "realtime",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
403 .desc = {
405 .name = "mlock",
406 .type = QEMU_OPT_BOOL,
408 { /* end of list */ }
412 static QemuOptsList qemu_msg_opts = {
413 .name = "msg",
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
415 .desc = {
417 .name = "timestamp",
418 .type = QEMU_OPT_BOOL,
420 { /* end of list */ }
424 static QemuOptsList qemu_name_opts = {
425 .name = "name",
426 .implied_opt_name = "guest",
427 .merge_lists = true,
428 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
429 .desc = {
431 .name = "guest",
432 .type = QEMU_OPT_STRING,
433 .help = "Sets the name of the guest.\n"
434 "This name will be displayed in the SDL window caption.\n"
435 "The name will also be used for the VNC server",
436 }, {
437 .name = "process",
438 .type = QEMU_OPT_STRING,
439 .help = "Sets the name of the QEMU process, as shown in top etc",
440 }, {
441 .name = "debug-threads",
442 .type = QEMU_OPT_BOOL,
443 .help = "When enabled, name the individual threads; defaults off.\n"
444 "NOTE: The thread names are for debugging and not a\n"
445 "stable API.",
447 { /* End of list */ }
451 static QemuOptsList qemu_mem_opts = {
452 .name = "memory",
453 .implied_opt_name = "size",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
455 .merge_lists = true,
456 .desc = {
458 .name = "size",
459 .type = QEMU_OPT_SIZE,
462 .name = "slots",
463 .type = QEMU_OPT_NUMBER,
466 .name = "maxmem",
467 .type = QEMU_OPT_SIZE,
469 { /* end of list */ }
473 static QemuOptsList qemu_icount_opts = {
474 .name = "icount",
475 .implied_opt_name = "shift",
476 .merge_lists = true,
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
478 .desc = {
480 .name = "shift",
481 .type = QEMU_OPT_STRING,
482 }, {
483 .name = "align",
484 .type = QEMU_OPT_BOOL,
485 }, {
486 .name = "sleep",
487 .type = QEMU_OPT_BOOL,
488 }, {
489 .name = "rr",
490 .type = QEMU_OPT_STRING,
491 }, {
492 .name = "rrfile",
493 .type = QEMU_OPT_STRING,
494 }, {
495 .name = "rrsnapshot",
496 .type = QEMU_OPT_STRING,
498 { /* end of list */ }
502 static QemuOptsList qemu_semihosting_config_opts = {
503 .name = "semihosting-config",
504 .implied_opt_name = "enable",
505 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
506 .desc = {
508 .name = "enable",
509 .type = QEMU_OPT_BOOL,
510 }, {
511 .name = "target",
512 .type = QEMU_OPT_STRING,
513 }, {
514 .name = "arg",
515 .type = QEMU_OPT_STRING,
517 { /* end of list */ }
521 static QemuOptsList qemu_fw_cfg_opts = {
522 .name = "fw_cfg",
523 .implied_opt_name = "name",
524 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
525 .desc = {
527 .name = "name",
528 .type = QEMU_OPT_STRING,
529 .help = "Sets the fw_cfg name of the blob to be inserted",
530 }, {
531 .name = "file",
532 .type = QEMU_OPT_STRING,
533 .help = "Sets the name of the file from which\n"
534 "the fw_cfg blob will be loaded",
535 }, {
536 .name = "string",
537 .type = QEMU_OPT_STRING,
538 .help = "Sets content of the blob to be inserted from a string",
540 { /* end of list */ }
545 * Get machine options
547 * Returns: machine options (never null).
549 QemuOpts *qemu_get_machine_opts(void)
551 return qemu_find_opts_singleton("machine");
554 const char *qemu_get_vm_name(void)
556 return qemu_name;
559 static void res_free(void)
561 g_free(boot_splash_filedata);
562 boot_splash_filedata = NULL;
565 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
567 const char *driver = qemu_opt_get(opts, "driver");
568 int i;
570 if (!driver)
571 return 0;
572 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
573 if (strcmp(default_list[i].driver, driver) != 0)
574 continue;
575 *(default_list[i].flag) = 0;
577 return 0;
580 /***********************************************************/
581 /* QEMU state */
583 static RunState current_run_state = RUN_STATE_PRELAUNCH;
585 /* We use RUN_STATE__MAX but any invalid value will do */
586 static RunState vmstop_requested = RUN_STATE__MAX;
587 static QemuMutex vmstop_lock;
589 typedef struct {
590 RunState from;
591 RunState to;
592 } RunStateTransition;
594 static const RunStateTransition runstate_transitions_def[] = {
595 /* from -> to */
596 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
597 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
600 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
601 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
602 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
603 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
604 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
605 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
606 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
607 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
608 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
610 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
611 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
613 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
614 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
618 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
621 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
622 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_PAUSED, RUN_STATE_COLO},
626 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
634 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
635 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
639 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
640 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
642 { RUN_STATE_COLO, RUN_STATE_RUNNING },
644 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
645 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
646 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
647 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
648 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
649 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
650 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
651 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
652 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
653 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
654 { RUN_STATE_RUNNING, RUN_STATE_COLO},
656 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
658 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
659 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
662 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
663 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
664 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
665 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
666 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
669 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
670 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
672 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
674 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
675 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
676 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
678 { RUN_STATE__MAX, RUN_STATE__MAX },
681 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
683 bool runstate_check(RunState state)
685 return current_run_state == state;
688 bool runstate_store(char *str, size_t size)
690 const char *state = RunState_lookup[current_run_state];
691 size_t len = strlen(state) + 1;
693 if (len > size) {
694 return false;
696 memcpy(str, state, len);
697 return true;
700 static void runstate_init(void)
702 const RunStateTransition *p;
704 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
705 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
706 runstate_valid_transitions[p->from][p->to] = true;
709 qemu_mutex_init(&vmstop_lock);
712 /* This function will abort() on invalid state transitions */
713 void runstate_set(RunState new_state)
715 assert(new_state < RUN_STATE__MAX);
717 if (current_run_state == new_state) {
718 return;
721 if (!runstate_valid_transitions[current_run_state][new_state]) {
722 error_report("invalid runstate transition: '%s' -> '%s'",
723 RunState_lookup[current_run_state],
724 RunState_lookup[new_state]);
725 abort();
727 trace_runstate_set(new_state);
728 current_run_state = new_state;
731 int runstate_is_running(void)
733 return runstate_check(RUN_STATE_RUNNING);
736 bool runstate_needs_reset(void)
738 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
739 runstate_check(RUN_STATE_SHUTDOWN);
742 StatusInfo *qmp_query_status(Error **errp)
744 StatusInfo *info = g_malloc0(sizeof(*info));
746 info->running = runstate_is_running();
747 info->singlestep = singlestep;
748 info->status = current_run_state;
750 return info;
753 bool qemu_vmstop_requested(RunState *r)
755 qemu_mutex_lock(&vmstop_lock);
756 *r = vmstop_requested;
757 vmstop_requested = RUN_STATE__MAX;
758 qemu_mutex_unlock(&vmstop_lock);
759 return *r < RUN_STATE__MAX;
762 void qemu_system_vmstop_request_prepare(void)
764 qemu_mutex_lock(&vmstop_lock);
767 void qemu_system_vmstop_request(RunState state)
769 vmstop_requested = state;
770 qemu_mutex_unlock(&vmstop_lock);
771 qemu_notify_event();
774 /***********************************************************/
775 /* real time host monotonic timer */
777 static time_t qemu_time(void)
779 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
782 /***********************************************************/
783 /* host time/date access */
784 void qemu_get_timedate(struct tm *tm, int offset)
786 time_t ti = qemu_time();
788 ti += offset;
789 if (rtc_date_offset == -1) {
790 if (rtc_utc)
791 gmtime_r(&ti, tm);
792 else
793 localtime_r(&ti, tm);
794 } else {
795 ti -= rtc_date_offset;
796 gmtime_r(&ti, tm);
800 int qemu_timedate_diff(struct tm *tm)
802 time_t seconds;
804 if (rtc_date_offset == -1)
805 if (rtc_utc)
806 seconds = mktimegm(tm);
807 else {
808 struct tm tmp = *tm;
809 tmp.tm_isdst = -1; /* use timezone to figure it out */
810 seconds = mktime(&tmp);
812 else
813 seconds = mktimegm(tm) + rtc_date_offset;
815 return seconds - qemu_time();
818 static void configure_rtc_date_offset(const char *startdate, int legacy)
820 time_t rtc_start_date;
821 struct tm tm;
823 if (!strcmp(startdate, "now") && legacy) {
824 rtc_date_offset = -1;
825 } else {
826 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
827 &tm.tm_year,
828 &tm.tm_mon,
829 &tm.tm_mday,
830 &tm.tm_hour,
831 &tm.tm_min,
832 &tm.tm_sec) == 6) {
833 /* OK */
834 } else if (sscanf(startdate, "%d-%d-%d",
835 &tm.tm_year,
836 &tm.tm_mon,
837 &tm.tm_mday) == 3) {
838 tm.tm_hour = 0;
839 tm.tm_min = 0;
840 tm.tm_sec = 0;
841 } else {
842 goto date_fail;
844 tm.tm_year -= 1900;
845 tm.tm_mon--;
846 rtc_start_date = mktimegm(&tm);
847 if (rtc_start_date == -1) {
848 date_fail:
849 error_report("invalid date format");
850 error_printf("valid formats: "
851 "'2006-06-17T16:01:21' or '2006-06-17'\n");
852 exit(1);
854 rtc_date_offset = qemu_time() - rtc_start_date;
858 static void configure_rtc(QemuOpts *opts)
860 const char *value;
862 value = qemu_opt_get(opts, "base");
863 if (value) {
864 if (!strcmp(value, "utc")) {
865 rtc_utc = 1;
866 } else if (!strcmp(value, "localtime")) {
867 Error *blocker = NULL;
868 rtc_utc = 0;
869 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
870 "-rtc base=localtime");
871 replay_add_blocker(blocker);
872 } else {
873 configure_rtc_date_offset(value, 0);
876 value = qemu_opt_get(opts, "clock");
877 if (value) {
878 if (!strcmp(value, "host")) {
879 rtc_clock = QEMU_CLOCK_HOST;
880 } else if (!strcmp(value, "rt")) {
881 rtc_clock = QEMU_CLOCK_REALTIME;
882 } else if (!strcmp(value, "vm")) {
883 rtc_clock = QEMU_CLOCK_VIRTUAL;
884 } else {
885 error_report("invalid option value '%s'", value);
886 exit(1);
889 value = qemu_opt_get(opts, "driftfix");
890 if (value) {
891 if (!strcmp(value, "slew")) {
892 static GlobalProperty slew_lost_ticks = {
893 .driver = "mc146818rtc",
894 .property = "lost_tick_policy",
895 .value = "slew",
898 qdev_prop_register_global(&slew_lost_ticks);
899 } else if (!strcmp(value, "none")) {
900 /* discard is default */
901 } else {
902 error_report("invalid option value '%s'", value);
903 exit(1);
908 /***********************************************************/
909 /* Bluetooth support */
910 static int nb_hcis;
911 static int cur_hci;
912 static struct HCIInfo *hci_table[MAX_NICS];
914 struct HCIInfo *qemu_next_hci(void)
916 if (cur_hci == nb_hcis)
917 return &null_hci;
919 return hci_table[cur_hci++];
922 static int bt_hci_parse(const char *str)
924 struct HCIInfo *hci;
925 bdaddr_t bdaddr;
927 if (nb_hcis >= MAX_NICS) {
928 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
929 return -1;
932 hci = hci_init(str);
933 if (!hci)
934 return -1;
936 bdaddr.b[0] = 0x52;
937 bdaddr.b[1] = 0x54;
938 bdaddr.b[2] = 0x00;
939 bdaddr.b[3] = 0x12;
940 bdaddr.b[4] = 0x34;
941 bdaddr.b[5] = 0x56 + nb_hcis;
942 hci->bdaddr_set(hci, bdaddr.b);
944 hci_table[nb_hcis++] = hci;
946 return 0;
949 static void bt_vhci_add(int vlan_id)
951 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
953 if (!vlan->slave)
954 error_report("warning: adding a VHCI to an empty scatternet %i",
955 vlan_id);
957 bt_vhci_init(bt_new_hci(vlan));
960 static struct bt_device_s *bt_device_add(const char *opt)
962 struct bt_scatternet_s *vlan;
963 int vlan_id = 0;
964 char *endp = strstr(opt, ",vlan=");
965 int len = (endp ? endp - opt : strlen(opt)) + 1;
966 char devname[10];
968 pstrcpy(devname, MIN(sizeof(devname), len), opt);
970 if (endp) {
971 vlan_id = strtol(endp + 6, &endp, 0);
972 if (*endp) {
973 error_report("unrecognised bluetooth vlan Id");
974 return 0;
978 vlan = qemu_find_bt_vlan(vlan_id);
980 if (!vlan->slave)
981 error_report("warning: adding a slave device to an empty scatternet %i",
982 vlan_id);
984 if (!strcmp(devname, "keyboard"))
985 return bt_keyboard_init(vlan);
987 error_report("unsupported bluetooth device '%s'", devname);
988 return 0;
991 static int bt_parse(const char *opt)
993 const char *endp, *p;
994 int vlan;
996 if (strstart(opt, "hci", &endp)) {
997 if (!*endp || *endp == ',') {
998 if (*endp)
999 if (!strstart(endp, ",vlan=", 0))
1000 opt = endp + 1;
1002 return bt_hci_parse(opt);
1004 } else if (strstart(opt, "vhci", &endp)) {
1005 if (!*endp || *endp == ',') {
1006 if (*endp) {
1007 if (strstart(endp, ",vlan=", &p)) {
1008 vlan = strtol(p, (char **) &endp, 0);
1009 if (*endp) {
1010 error_report("bad scatternet '%s'", p);
1011 return 1;
1013 } else {
1014 error_report("bad parameter '%s'", endp + 1);
1015 return 1;
1017 } else
1018 vlan = 0;
1020 bt_vhci_add(vlan);
1021 return 0;
1023 } else if (strstart(opt, "device:", &endp))
1024 return !bt_device_add(endp);
1026 error_report("bad bluetooth parameter '%s'", opt);
1027 return 1;
1030 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1032 /* FIXME: change this to true for 1.3 */
1033 if (qemu_opt_get_bool(opts, "enable", false)) {
1034 #ifdef CONFIG_SECCOMP
1035 if (seccomp_start() < 0) {
1036 error_report("failed to install seccomp syscall filter "
1037 "in the kernel");
1038 return -1;
1040 #else
1041 error_report("seccomp support is disabled");
1042 return -1;
1043 #endif
1046 return 0;
1049 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1051 const char *proc_name;
1053 if (qemu_opt_get(opts, "debug-threads")) {
1054 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1056 qemu_name = qemu_opt_get(opts, "guest");
1058 proc_name = qemu_opt_get(opts, "process");
1059 if (proc_name) {
1060 os_set_proc_name(proc_name);
1063 return 0;
1066 bool defaults_enabled(void)
1068 return has_defaults;
1071 #ifndef _WIN32
1072 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1074 int fd, dupfd, flags;
1075 int64_t fdset_id;
1076 const char *fd_opaque = NULL;
1077 AddfdInfo *fdinfo;
1079 fd = qemu_opt_get_number(opts, "fd", -1);
1080 fdset_id = qemu_opt_get_number(opts, "set", -1);
1081 fd_opaque = qemu_opt_get(opts, "opaque");
1083 if (fd < 0) {
1084 error_report("fd option is required and must be non-negative");
1085 return -1;
1088 if (fd <= STDERR_FILENO) {
1089 error_report("fd cannot be a standard I/O stream");
1090 return -1;
1094 * All fds inherited across exec() necessarily have FD_CLOEXEC
1095 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1097 flags = fcntl(fd, F_GETFD);
1098 if (flags == -1 || (flags & FD_CLOEXEC)) {
1099 error_report("fd is not valid or already in use");
1100 return -1;
1103 if (fdset_id < 0) {
1104 error_report("set option is required and must be non-negative");
1105 return -1;
1108 #ifdef F_DUPFD_CLOEXEC
1109 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1110 #else
1111 dupfd = dup(fd);
1112 if (dupfd != -1) {
1113 qemu_set_cloexec(dupfd);
1115 #endif
1116 if (dupfd == -1) {
1117 error_report("error duplicating fd: %s", strerror(errno));
1118 return -1;
1121 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1122 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1123 &error_abort);
1124 g_free(fdinfo);
1126 return 0;
1129 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1131 int fd;
1133 fd = qemu_opt_get_number(opts, "fd", -1);
1134 close(fd);
1136 return 0;
1138 #endif
1140 /***********************************************************/
1141 /* QEMU Block devices */
1143 #define HD_OPTS "media=disk"
1144 #define CDROM_OPTS "media=cdrom"
1145 #define FD_OPTS ""
1146 #define PFLASH_OPTS ""
1147 #define MTD_OPTS ""
1148 #define SD_OPTS ""
1150 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1152 BlockInterfaceType *block_default_type = opaque;
1154 return drive_new(opts, *block_default_type) == NULL;
1157 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1159 if (qemu_opt_get(opts, "snapshot") == NULL) {
1160 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1162 return 0;
1165 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1166 int index, const char *optstr)
1168 QemuOpts *opts;
1169 DriveInfo *dinfo;
1171 if (!enable || drive_get_by_index(type, index)) {
1172 return;
1175 opts = drive_add(type, index, NULL, optstr);
1176 if (snapshot) {
1177 drive_enable_snapshot(NULL, opts, NULL);
1180 dinfo = drive_new(opts, type);
1181 if (!dinfo) {
1182 exit(1);
1184 dinfo->is_default = true;
1188 static QemuOptsList qemu_smp_opts = {
1189 .name = "smp-opts",
1190 .implied_opt_name = "cpus",
1191 .merge_lists = true,
1192 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1193 .desc = {
1195 .name = "cpus",
1196 .type = QEMU_OPT_NUMBER,
1197 }, {
1198 .name = "sockets",
1199 .type = QEMU_OPT_NUMBER,
1200 }, {
1201 .name = "cores",
1202 .type = QEMU_OPT_NUMBER,
1203 }, {
1204 .name = "threads",
1205 .type = QEMU_OPT_NUMBER,
1206 }, {
1207 .name = "maxcpus",
1208 .type = QEMU_OPT_NUMBER,
1210 { /*End of list */ }
1214 static void smp_parse(QemuOpts *opts)
1216 if (opts) {
1217 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1218 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1219 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1220 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1222 /* compute missing values, prefer sockets over cores over threads */
1223 if (cpus == 0 || sockets == 0) {
1224 sockets = sockets > 0 ? sockets : 1;
1225 cores = cores > 0 ? cores : 1;
1226 threads = threads > 0 ? threads : 1;
1227 if (cpus == 0) {
1228 cpus = cores * threads * sockets;
1230 } else if (cores == 0) {
1231 threads = threads > 0 ? threads : 1;
1232 cores = cpus / (sockets * threads);
1233 cores = cores > 0 ? cores : 1;
1234 } else if (threads == 0) {
1235 threads = cpus / (cores * sockets);
1236 threads = threads > 0 ? threads : 1;
1237 } else if (sockets * cores * threads < cpus) {
1238 error_report("cpu topology: "
1239 "sockets (%u) * cores (%u) * threads (%u) < "
1240 "smp_cpus (%u)",
1241 sockets, cores, threads, cpus);
1242 exit(1);
1245 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1247 if (max_cpus < cpus) {
1248 error_report("maxcpus must be equal to or greater than smp");
1249 exit(1);
1252 if (sockets * cores * threads > max_cpus) {
1253 error_report("cpu topology: "
1254 "sockets (%u) * cores (%u) * threads (%u) > "
1255 "maxcpus (%u)",
1256 sockets, cores, threads, max_cpus);
1257 exit(1);
1260 smp_cpus = cpus;
1261 smp_cores = cores;
1262 smp_threads = threads;
1265 if (smp_cpus > 1) {
1266 Error *blocker = NULL;
1267 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1268 replay_add_blocker(blocker);
1272 static void realtime_init(void)
1274 if (enable_mlock) {
1275 if (os_mlock() < 0) {
1276 error_report("locking memory failed");
1277 exit(1);
1283 static void configure_msg(QemuOpts *opts)
1285 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1288 /***********************************************************/
1289 /* Semihosting */
1291 typedef struct SemihostingConfig {
1292 bool enabled;
1293 SemihostingTarget target;
1294 const char **argv;
1295 int argc;
1296 const char *cmdline; /* concatenated argv */
1297 } SemihostingConfig;
1299 static SemihostingConfig semihosting;
1301 bool semihosting_enabled(void)
1303 return semihosting.enabled;
1306 SemihostingTarget semihosting_get_target(void)
1308 return semihosting.target;
1311 const char *semihosting_get_arg(int i)
1313 if (i >= semihosting.argc) {
1314 return NULL;
1316 return semihosting.argv[i];
1319 int semihosting_get_argc(void)
1321 return semihosting.argc;
1324 const char *semihosting_get_cmdline(void)
1326 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1327 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1329 return semihosting.cmdline;
1332 static int add_semihosting_arg(void *opaque,
1333 const char *name, const char *val,
1334 Error **errp)
1336 SemihostingConfig *s = opaque;
1337 if (strcmp(name, "arg") == 0) {
1338 s->argc++;
1339 /* one extra element as g_strjoinv() expects NULL-terminated array */
1340 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1341 s->argv[s->argc - 1] = val;
1342 s->argv[s->argc] = NULL;
1344 return 0;
1347 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1348 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1350 char *cmd_token;
1352 /* argv[0] */
1353 add_semihosting_arg(&semihosting, "arg", file, NULL);
1355 /* split -append and initialize argv[1..n] */
1356 cmd_token = strtok(g_strdup(cmd), " ");
1357 while (cmd_token) {
1358 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1359 cmd_token = strtok(NULL, " ");
1363 /* Now we still need this for compatibility with XEN. */
1364 bool has_igd_gfx_passthru;
1365 static void igd_gfx_passthru(void)
1367 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1370 /***********************************************************/
1371 /* USB devices */
1373 static int usb_device_add(const char *devname)
1375 USBDevice *dev = NULL;
1376 #ifndef CONFIG_LINUX
1377 const char *p;
1378 #endif
1380 if (!machine_usb(current_machine)) {
1381 return -1;
1384 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1385 dev = usbdevice_create(devname);
1386 if (dev)
1387 goto done;
1389 /* the other ones */
1390 #ifndef CONFIG_LINUX
1391 /* only the linux version is qdev-ified, usb-bsd still needs this */
1392 if (strstart(devname, "host:", &p)) {
1393 dev = usb_host_device_open(usb_bus_find(-1), p);
1395 #endif
1396 if (!dev)
1397 return -1;
1399 done:
1400 return 0;
1403 static int usb_device_del(const char *devname)
1405 int bus_num, addr;
1406 const char *p;
1408 if (strstart(devname, "host:", &p)) {
1409 return -1;
1412 if (!machine_usb(current_machine)) {
1413 return -1;
1416 p = strchr(devname, '.');
1417 if (!p)
1418 return -1;
1419 bus_num = strtoul(devname, NULL, 0);
1420 addr = strtoul(p + 1, NULL, 0);
1422 return usb_device_delete_addr(bus_num, addr);
1425 static int usb_parse(const char *cmdline)
1427 int r;
1428 r = usb_device_add(cmdline);
1429 if (r < 0) {
1430 error_report("could not add USB device '%s'", cmdline);
1432 return r;
1435 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1437 const char *devname = qdict_get_str(qdict, "devname");
1438 if (usb_device_add(devname) < 0) {
1439 error_report("could not add USB device '%s'", devname);
1443 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1445 const char *devname = qdict_get_str(qdict, "devname");
1446 if (usb_device_del(devname) < 0) {
1447 error_report("could not delete USB device '%s'", devname);
1451 /***********************************************************/
1452 /* machine registration */
1454 MachineState *current_machine;
1456 static MachineClass *find_machine(const char *name)
1458 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1459 MachineClass *mc = NULL;
1461 for (el = machines; el; el = el->next) {
1462 MachineClass *temp = el->data;
1464 if (!strcmp(temp->name, name)) {
1465 mc = temp;
1466 break;
1468 if (temp->alias &&
1469 !strcmp(temp->alias, name)) {
1470 mc = temp;
1471 break;
1475 g_slist_free(machines);
1476 return mc;
1479 MachineClass *find_default_machine(void)
1481 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1482 MachineClass *mc = NULL;
1484 for (el = machines; el; el = el->next) {
1485 MachineClass *temp = el->data;
1487 if (temp->is_default) {
1488 mc = temp;
1489 break;
1493 g_slist_free(machines);
1494 return mc;
1497 MachineInfoList *qmp_query_machines(Error **errp)
1499 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1500 MachineInfoList *mach_list = NULL;
1502 for (el = machines; el; el = el->next) {
1503 MachineClass *mc = el->data;
1504 MachineInfoList *entry;
1505 MachineInfo *info;
1507 info = g_malloc0(sizeof(*info));
1508 if (mc->is_default) {
1509 info->has_is_default = true;
1510 info->is_default = true;
1513 if (mc->alias) {
1514 info->has_alias = true;
1515 info->alias = g_strdup(mc->alias);
1518 info->name = g_strdup(mc->name);
1519 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1520 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1522 entry = g_malloc0(sizeof(*entry));
1523 entry->value = info;
1524 entry->next = mach_list;
1525 mach_list = entry;
1528 g_slist_free(machines);
1529 return mach_list;
1532 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1534 ObjectProperty *prop;
1535 ObjectPropertyIterator iter;
1537 if (!qemu_opt_has_help_opt(opts)) {
1538 return 0;
1541 object_property_iter_init(&iter, OBJECT(machine));
1542 while ((prop = object_property_iter_next(&iter))) {
1543 if (!prop->set) {
1544 continue;
1547 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1548 prop->name, prop->type);
1549 if (prop->description) {
1550 error_printf(" (%s)\n", prop->description);
1551 } else {
1552 error_printf("\n");
1556 return 1;
1559 /***********************************************************/
1560 /* main execution loop */
1562 struct vm_change_state_entry {
1563 VMChangeStateHandler *cb;
1564 void *opaque;
1565 QLIST_ENTRY (vm_change_state_entry) entries;
1568 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1570 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1571 void *opaque)
1573 VMChangeStateEntry *e;
1575 e = g_malloc0(sizeof (*e));
1577 e->cb = cb;
1578 e->opaque = opaque;
1579 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1580 return e;
1583 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1585 QLIST_REMOVE (e, entries);
1586 g_free (e);
1589 void vm_state_notify(int running, RunState state)
1591 VMChangeStateEntry *e, *next;
1593 trace_vm_state_notify(running, state);
1595 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1596 e->cb(e->opaque, running, state);
1600 static int reset_requested;
1601 static int shutdown_requested, shutdown_signal = -1;
1602 static pid_t shutdown_pid;
1603 static int powerdown_requested;
1604 static int debug_requested;
1605 static int suspend_requested;
1606 static WakeupReason wakeup_reason;
1607 static NotifierList powerdown_notifiers =
1608 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1609 static NotifierList suspend_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1611 static NotifierList wakeup_notifiers =
1612 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1613 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1615 int qemu_shutdown_requested_get(void)
1617 return shutdown_requested;
1620 int qemu_reset_requested_get(void)
1622 return reset_requested;
1625 static int qemu_shutdown_requested(void)
1627 return atomic_xchg(&shutdown_requested, 0);
1630 static void qemu_kill_report(void)
1632 if (!qtest_driver() && shutdown_signal != -1) {
1633 if (shutdown_pid == 0) {
1634 /* This happens for eg ^C at the terminal, so it's worth
1635 * avoiding printing an odd message in that case.
1637 error_report("terminating on signal %d", shutdown_signal);
1638 } else {
1639 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1641 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1642 shutdown_signal, shutdown_pid,
1643 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1644 g_free(shutdown_cmd);
1646 shutdown_signal = -1;
1650 static int qemu_reset_requested(void)
1652 int r = reset_requested;
1653 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1654 reset_requested = 0;
1655 return r;
1657 return false;
1660 static int qemu_suspend_requested(void)
1662 int r = suspend_requested;
1663 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1664 suspend_requested = 0;
1665 return r;
1667 return false;
1670 static WakeupReason qemu_wakeup_requested(void)
1672 return wakeup_reason;
1675 static int qemu_powerdown_requested(void)
1677 int r = powerdown_requested;
1678 powerdown_requested = 0;
1679 return r;
1682 static int qemu_debug_requested(void)
1684 int r = debug_requested;
1685 debug_requested = 0;
1686 return r;
1689 void qemu_system_reset(bool report)
1691 MachineClass *mc;
1693 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1695 cpu_synchronize_all_states();
1697 if (mc && mc->reset) {
1698 mc->reset();
1699 } else {
1700 qemu_devices_reset();
1702 if (report) {
1703 qapi_event_send_reset(&error_abort);
1705 cpu_synchronize_all_post_reset();
1708 void qemu_system_guest_panicked(GuestPanicInformation *info)
1710 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1712 if (current_cpu) {
1713 current_cpu->crash_occurred = true;
1715 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1716 !!info, info, &error_abort);
1717 vm_stop(RUN_STATE_GUEST_PANICKED);
1718 if (!no_shutdown) {
1719 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1720 !!info, info, &error_abort);
1721 qemu_system_shutdown_request();
1724 if (info) {
1725 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1726 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1727 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1728 info->u.hyper_v.arg1,
1729 info->u.hyper_v.arg2,
1730 info->u.hyper_v.arg3,
1731 info->u.hyper_v.arg4,
1732 info->u.hyper_v.arg5);
1734 qapi_free_GuestPanicInformation(info);
1738 void qemu_system_reset_request(void)
1740 if (no_reboot) {
1741 shutdown_requested = 1;
1742 } else {
1743 reset_requested = 1;
1745 cpu_stop_current();
1746 qemu_notify_event();
1749 static void qemu_system_suspend(void)
1751 pause_all_vcpus();
1752 notifier_list_notify(&suspend_notifiers, NULL);
1753 runstate_set(RUN_STATE_SUSPENDED);
1754 qapi_event_send_suspend(&error_abort);
1757 void qemu_system_suspend_request(void)
1759 if (runstate_check(RUN_STATE_SUSPENDED)) {
1760 return;
1762 suspend_requested = 1;
1763 cpu_stop_current();
1764 qemu_notify_event();
1767 void qemu_register_suspend_notifier(Notifier *notifier)
1769 notifier_list_add(&suspend_notifiers, notifier);
1772 void qemu_system_wakeup_request(WakeupReason reason)
1774 trace_system_wakeup_request(reason);
1776 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1777 return;
1779 if (!(wakeup_reason_mask & (1 << reason))) {
1780 return;
1782 runstate_set(RUN_STATE_RUNNING);
1783 wakeup_reason = reason;
1784 qemu_notify_event();
1787 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1789 if (enabled) {
1790 wakeup_reason_mask |= (1 << reason);
1791 } else {
1792 wakeup_reason_mask &= ~(1 << reason);
1796 void qemu_register_wakeup_notifier(Notifier *notifier)
1798 notifier_list_add(&wakeup_notifiers, notifier);
1801 void qemu_system_killed(int signal, pid_t pid)
1803 shutdown_signal = signal;
1804 shutdown_pid = pid;
1805 no_shutdown = 0;
1807 /* Cannot call qemu_system_shutdown_request directly because
1808 * we are in a signal handler.
1810 shutdown_requested = 1;
1811 qemu_notify_event();
1814 void qemu_system_shutdown_request(void)
1816 trace_qemu_system_shutdown_request();
1817 replay_shutdown_request();
1818 shutdown_requested = 1;
1819 qemu_notify_event();
1822 static void qemu_system_powerdown(void)
1824 qapi_event_send_powerdown(&error_abort);
1825 notifier_list_notify(&powerdown_notifiers, NULL);
1828 void qemu_system_powerdown_request(void)
1830 trace_qemu_system_powerdown_request();
1831 powerdown_requested = 1;
1832 qemu_notify_event();
1835 void qemu_register_powerdown_notifier(Notifier *notifier)
1837 notifier_list_add(&powerdown_notifiers, notifier);
1840 void qemu_system_debug_request(void)
1842 debug_requested = 1;
1843 qemu_notify_event();
1846 static bool main_loop_should_exit(void)
1848 RunState r;
1849 if (qemu_debug_requested()) {
1850 vm_stop(RUN_STATE_DEBUG);
1852 if (qemu_suspend_requested()) {
1853 qemu_system_suspend();
1855 if (qemu_shutdown_requested()) {
1856 qemu_kill_report();
1857 qapi_event_send_shutdown(&error_abort);
1858 if (no_shutdown) {
1859 vm_stop(RUN_STATE_SHUTDOWN);
1860 } else {
1861 return true;
1864 if (qemu_reset_requested()) {
1865 pause_all_vcpus();
1866 qemu_system_reset(VMRESET_REPORT);
1867 resume_all_vcpus();
1868 if (!runstate_check(RUN_STATE_RUNNING) &&
1869 !runstate_check(RUN_STATE_INMIGRATE)) {
1870 runstate_set(RUN_STATE_PRELAUNCH);
1873 if (qemu_wakeup_requested()) {
1874 pause_all_vcpus();
1875 qemu_system_reset(VMRESET_SILENT);
1876 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1877 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1878 resume_all_vcpus();
1879 qapi_event_send_wakeup(&error_abort);
1881 if (qemu_powerdown_requested()) {
1882 qemu_system_powerdown();
1884 if (qemu_vmstop_requested(&r)) {
1885 vm_stop(r);
1887 return false;
1890 static void main_loop(void)
1892 #ifdef CONFIG_PROFILER
1893 int64_t ti;
1894 #endif
1895 do {
1896 #ifdef CONFIG_PROFILER
1897 ti = profile_getclock();
1898 #endif
1899 main_loop_wait(false);
1900 #ifdef CONFIG_PROFILER
1901 dev_time += profile_getclock() - ti;
1902 #endif
1903 } while (!main_loop_should_exit());
1906 static void version(void)
1908 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1909 QEMU_COPYRIGHT "\n");
1912 static void help(int exitcode)
1914 version();
1915 printf("usage: %s [options] [disk_image]\n\n"
1916 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1917 error_get_progname());
1919 #define QEMU_OPTIONS_GENERATE_HELP
1920 #include "qemu-options-wrapper.h"
1922 printf("\nDuring emulation, the following keys are useful:\n"
1923 "ctrl-alt-f toggle full screen\n"
1924 "ctrl-alt-n switch to virtual console 'n'\n"
1925 "ctrl-alt toggle mouse and keyboard grab\n"
1926 "\n"
1927 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1929 exit(exitcode);
1932 #define HAS_ARG 0x0001
1934 typedef struct QEMUOption {
1935 const char *name;
1936 int flags;
1937 int index;
1938 uint32_t arch_mask;
1939 } QEMUOption;
1941 static const QEMUOption qemu_options[] = {
1942 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1943 #define QEMU_OPTIONS_GENERATE_OPTIONS
1944 #include "qemu-options-wrapper.h"
1945 { NULL },
1948 typedef struct VGAInterfaceInfo {
1949 const char *opt_name; /* option name */
1950 const char *name; /* human-readable name */
1951 /* Class names indicating that support is available.
1952 * If no class is specified, the interface is always available */
1953 const char *class_names[2];
1954 } VGAInterfaceInfo;
1956 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1957 [VGA_NONE] = {
1958 .opt_name = "none",
1960 [VGA_STD] = {
1961 .opt_name = "std",
1962 .name = "standard VGA",
1963 .class_names = { "VGA", "isa-vga" },
1965 [VGA_CIRRUS] = {
1966 .opt_name = "cirrus",
1967 .name = "Cirrus VGA",
1968 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1970 [VGA_VMWARE] = {
1971 .opt_name = "vmware",
1972 .name = "VMWare SVGA",
1973 .class_names = { "vmware-svga" },
1975 [VGA_VIRTIO] = {
1976 .opt_name = "virtio",
1977 .name = "Virtio VGA",
1978 .class_names = { "virtio-vga" },
1980 [VGA_QXL] = {
1981 .opt_name = "qxl",
1982 .name = "QXL VGA",
1983 .class_names = { "qxl-vga" },
1985 [VGA_TCX] = {
1986 .opt_name = "tcx",
1987 .name = "TCX framebuffer",
1988 .class_names = { "SUNW,tcx" },
1990 [VGA_CG3] = {
1991 .opt_name = "cg3",
1992 .name = "CG3 framebuffer",
1993 .class_names = { "cgthree" },
1995 [VGA_XENFB] = {
1996 .opt_name = "xenfb",
2000 static bool vga_interface_available(VGAInterfaceType t)
2002 VGAInterfaceInfo *ti = &vga_interfaces[t];
2004 assert(t < VGA_TYPE_MAX);
2005 return !ti->class_names[0] ||
2006 object_class_by_name(ti->class_names[0]) ||
2007 object_class_by_name(ti->class_names[1]);
2010 static void select_vgahw(const char *p)
2012 const char *opts;
2013 int t;
2015 assert(vga_interface_type == VGA_NONE);
2016 for (t = 0; t < VGA_TYPE_MAX; t++) {
2017 VGAInterfaceInfo *ti = &vga_interfaces[t];
2018 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2019 if (!vga_interface_available(t)) {
2020 error_report("%s not available", ti->name);
2021 exit(1);
2023 vga_interface_type = t;
2024 break;
2027 if (t == VGA_TYPE_MAX) {
2028 invalid_vga:
2029 error_report("unknown vga type: %s", p);
2030 exit(1);
2032 while (*opts) {
2033 const char *nextopt;
2035 if (strstart(opts, ",retrace=", &nextopt)) {
2036 opts = nextopt;
2037 if (strstart(opts, "dumb", &nextopt))
2038 vga_retrace_method = VGA_RETRACE_DUMB;
2039 else if (strstart(opts, "precise", &nextopt))
2040 vga_retrace_method = VGA_RETRACE_PRECISE;
2041 else goto invalid_vga;
2042 } else goto invalid_vga;
2043 opts = nextopt;
2047 typedef enum DisplayType {
2048 DT_DEFAULT,
2049 DT_CURSES,
2050 DT_SDL,
2051 DT_COCOA,
2052 DT_GTK,
2053 DT_EGL,
2054 DT_NONE,
2055 } DisplayType;
2057 static DisplayType select_display(const char *p)
2059 const char *opts;
2060 DisplayType display = DT_DEFAULT;
2062 if (strstart(p, "sdl", &opts)) {
2063 #ifdef CONFIG_SDL
2064 display = DT_SDL;
2065 while (*opts) {
2066 const char *nextopt;
2068 if (strstart(opts, ",frame=", &nextopt)) {
2069 opts = nextopt;
2070 if (strstart(opts, "on", &nextopt)) {
2071 no_frame = 0;
2072 } else if (strstart(opts, "off", &nextopt)) {
2073 no_frame = 1;
2074 } else {
2075 goto invalid_sdl_args;
2077 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2078 opts = nextopt;
2079 if (strstart(opts, "on", &nextopt)) {
2080 alt_grab = 1;
2081 } else if (strstart(opts, "off", &nextopt)) {
2082 alt_grab = 0;
2083 } else {
2084 goto invalid_sdl_args;
2086 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2087 opts = nextopt;
2088 if (strstart(opts, "on", &nextopt)) {
2089 ctrl_grab = 1;
2090 } else if (strstart(opts, "off", &nextopt)) {
2091 ctrl_grab = 0;
2092 } else {
2093 goto invalid_sdl_args;
2095 } else if (strstart(opts, ",window_close=", &nextopt)) {
2096 opts = nextopt;
2097 if (strstart(opts, "on", &nextopt)) {
2098 no_quit = 0;
2099 } else if (strstart(opts, "off", &nextopt)) {
2100 no_quit = 1;
2101 } else {
2102 goto invalid_sdl_args;
2104 } else if (strstart(opts, ",gl=", &nextopt)) {
2105 opts = nextopt;
2106 if (strstart(opts, "on", &nextopt)) {
2107 request_opengl = 1;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 request_opengl = 0;
2110 } else {
2111 goto invalid_sdl_args;
2113 } else {
2114 invalid_sdl_args:
2115 error_report("invalid SDL option string");
2116 exit(1);
2118 opts = nextopt;
2120 #else
2121 error_report("SDL support is disabled");
2122 exit(1);
2123 #endif
2124 } else if (strstart(p, "vnc", &opts)) {
2125 if (*opts == '=') {
2126 vnc_parse(opts + 1, &error_fatal);
2127 } else {
2128 error_report("VNC requires a display argument vnc=<display>");
2129 exit(1);
2131 } else if (strstart(p, "egl-headless", &opts)) {
2132 #ifdef CONFIG_OPENGL
2133 request_opengl = 1;
2134 display_opengl = 1;
2135 display = DT_EGL;
2136 #else
2137 fprintf(stderr, "egl support is disabled\n");
2138 exit(1);
2139 #endif
2140 } else if (strstart(p, "curses", &opts)) {
2141 #ifdef CONFIG_CURSES
2142 display = DT_CURSES;
2143 #else
2144 error_report("curses support is disabled");
2145 exit(1);
2146 #endif
2147 } else if (strstart(p, "gtk", &opts)) {
2148 #ifdef CONFIG_GTK
2149 display = DT_GTK;
2150 while (*opts) {
2151 const char *nextopt;
2153 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2154 opts = nextopt;
2155 if (strstart(opts, "on", &nextopt)) {
2156 grab_on_hover = true;
2157 } else if (strstart(opts, "off", &nextopt)) {
2158 grab_on_hover = false;
2159 } else {
2160 goto invalid_gtk_args;
2162 } else if (strstart(opts, ",gl=", &nextopt)) {
2163 opts = nextopt;
2164 if (strstart(opts, "on", &nextopt)) {
2165 request_opengl = 1;
2166 } else if (strstart(opts, "off", &nextopt)) {
2167 request_opengl = 0;
2168 } else {
2169 goto invalid_gtk_args;
2171 } else {
2172 invalid_gtk_args:
2173 error_report("invalid GTK option string");
2174 exit(1);
2176 opts = nextopt;
2178 #else
2179 error_report("GTK support is disabled");
2180 exit(1);
2181 #endif
2182 } else if (strstart(p, "none", &opts)) {
2183 display = DT_NONE;
2184 } else {
2185 error_report("unknown display type");
2186 exit(1);
2189 return display;
2192 static int balloon_parse(const char *arg)
2194 QemuOpts *opts;
2196 if (strcmp(arg, "none") == 0) {
2197 return 0;
2200 if (!strncmp(arg, "virtio", 6)) {
2201 if (arg[6] == ',') {
2202 /* have params -> parse them */
2203 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2204 false);
2205 if (!opts)
2206 return -1;
2207 } else {
2208 /* create empty opts */
2209 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2210 &error_abort);
2212 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2213 return 0;
2216 return -1;
2219 char *qemu_find_file(int type, const char *name)
2221 int i;
2222 const char *subdir;
2223 char *buf;
2225 /* Try the name as a straight path first */
2226 if (access(name, R_OK) == 0) {
2227 trace_load_file(name, name);
2228 return g_strdup(name);
2231 switch (type) {
2232 case QEMU_FILE_TYPE_BIOS:
2233 subdir = "";
2234 break;
2235 case QEMU_FILE_TYPE_KEYMAP:
2236 subdir = "keymaps/";
2237 break;
2238 default:
2239 abort();
2242 for (i = 0; i < data_dir_idx; i++) {
2243 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2244 if (access(buf, R_OK) == 0) {
2245 trace_load_file(name, buf);
2246 return buf;
2248 g_free(buf);
2250 return NULL;
2253 static inline bool nonempty_str(const char *str)
2255 return str && *str;
2258 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2260 gchar *buf;
2261 size_t size;
2262 const char *name, *file, *str;
2263 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2265 if (fw_cfg == NULL) {
2266 error_report("fw_cfg device not available");
2267 return -1;
2269 name = qemu_opt_get(opts, "name");
2270 file = qemu_opt_get(opts, "file");
2271 str = qemu_opt_get(opts, "string");
2273 /* we need name and either a file or the content string */
2274 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2275 error_report("invalid argument(s)");
2276 return -1;
2278 if (nonempty_str(file) && nonempty_str(str)) {
2279 error_report("file and string are mutually exclusive");
2280 return -1;
2282 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2283 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2284 return -1;
2286 if (strncmp(name, "opt/", 4) != 0) {
2287 error_report("warning: externally provided fw_cfg item names "
2288 "should be prefixed with \"opt/\"");
2290 if (nonempty_str(str)) {
2291 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2292 buf = g_memdup(str, size);
2293 } else {
2294 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2295 error_report("can't load %s", file);
2296 return -1;
2299 /* For legacy, keep user files in a specific global order. */
2300 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2301 fw_cfg_add_file(fw_cfg, name, buf, size);
2302 fw_cfg_reset_order_override(fw_cfg);
2303 return 0;
2306 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2308 return qdev_device_help(opts);
2311 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2313 Error *err = NULL;
2314 DeviceState *dev;
2316 dev = qdev_device_add(opts, &err);
2317 if (!dev) {
2318 error_report_err(err);
2319 return -1;
2321 object_unref(OBJECT(dev));
2322 return 0;
2325 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2327 Error *local_err = NULL;
2329 qemu_chr_new_from_opts(opts, &local_err);
2330 if (local_err) {
2331 error_report_err(local_err);
2332 return -1;
2334 return 0;
2337 #ifdef CONFIG_VIRTFS
2338 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2340 return qemu_fsdev_add(opts);
2342 #endif
2344 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2346 Chardev *chr;
2347 const char *chardev;
2348 const char *mode;
2349 int flags;
2351 mode = qemu_opt_get(opts, "mode");
2352 if (mode == NULL) {
2353 mode = "readline";
2355 if (strcmp(mode, "readline") == 0) {
2356 flags = MONITOR_USE_READLINE;
2357 } else if (strcmp(mode, "control") == 0) {
2358 flags = MONITOR_USE_CONTROL;
2359 } else {
2360 error_report("unknown monitor mode \"%s\"", mode);
2361 exit(1);
2364 if (qemu_opt_get_bool(opts, "pretty", 0))
2365 flags |= MONITOR_USE_PRETTY;
2367 if (qemu_opt_get_bool(opts, "default", 0)) {
2368 error_report("option 'default' does nothing and is deprecated");
2371 chardev = qemu_opt_get(opts, "chardev");
2372 chr = qemu_chr_find(chardev);
2373 if (chr == NULL) {
2374 error_report("chardev \"%s\" not found", chardev);
2375 exit(1);
2378 monitor_init(chr, flags);
2379 return 0;
2382 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2384 static int monitor_device_index = 0;
2385 QemuOpts *opts;
2386 const char *p;
2387 char label[32];
2389 if (strstart(optarg, "chardev:", &p)) {
2390 snprintf(label, sizeof(label), "%s", p);
2391 } else {
2392 snprintf(label, sizeof(label), "compat_monitor%d",
2393 monitor_device_index);
2394 opts = qemu_chr_parse_compat(label, optarg);
2395 if (!opts) {
2396 error_report("parse error: %s", optarg);
2397 exit(1);
2401 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2402 qemu_opt_set(opts, "mode", mode, &error_abort);
2403 qemu_opt_set(opts, "chardev", label, &error_abort);
2404 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2405 monitor_device_index++;
2408 struct device_config {
2409 enum {
2410 DEV_USB, /* -usbdevice */
2411 DEV_BT, /* -bt */
2412 DEV_SERIAL, /* -serial */
2413 DEV_PARALLEL, /* -parallel */
2414 DEV_VIRTCON, /* -virtioconsole */
2415 DEV_DEBUGCON, /* -debugcon */
2416 DEV_GDB, /* -gdb, -s */
2417 DEV_SCLP, /* s390 sclp */
2418 } type;
2419 const char *cmdline;
2420 Location loc;
2421 QTAILQ_ENTRY(device_config) next;
2424 static QTAILQ_HEAD(, device_config) device_configs =
2425 QTAILQ_HEAD_INITIALIZER(device_configs);
2427 static void add_device_config(int type, const char *cmdline)
2429 struct device_config *conf;
2431 conf = g_malloc0(sizeof(*conf));
2432 conf->type = type;
2433 conf->cmdline = cmdline;
2434 loc_save(&conf->loc);
2435 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2438 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2440 struct device_config *conf;
2441 int rc;
2443 QTAILQ_FOREACH(conf, &device_configs, next) {
2444 if (conf->type != type)
2445 continue;
2446 loc_push_restore(&conf->loc);
2447 rc = func(conf->cmdline);
2448 loc_pop(&conf->loc);
2449 if (rc) {
2450 return rc;
2453 return 0;
2456 static int serial_parse(const char *devname)
2458 static int index = 0;
2459 char label[32];
2461 if (strcmp(devname, "none") == 0)
2462 return 0;
2463 if (index == MAX_SERIAL_PORTS) {
2464 error_report("too many serial ports");
2465 exit(1);
2467 snprintf(label, sizeof(label), "serial%d", index);
2468 serial_hds[index] = qemu_chr_new(label, devname);
2469 if (!serial_hds[index]) {
2470 error_report("could not connect serial device"
2471 " to character backend '%s'", devname);
2472 return -1;
2474 index++;
2475 return 0;
2478 static int parallel_parse(const char *devname)
2480 static int index = 0;
2481 char label[32];
2483 if (strcmp(devname, "none") == 0)
2484 return 0;
2485 if (index == MAX_PARALLEL_PORTS) {
2486 error_report("too many parallel ports");
2487 exit(1);
2489 snprintf(label, sizeof(label), "parallel%d", index);
2490 parallel_hds[index] = qemu_chr_new(label, devname);
2491 if (!parallel_hds[index]) {
2492 error_report("could not connect parallel device"
2493 " to character backend '%s'", devname);
2494 return -1;
2496 index++;
2497 return 0;
2500 static int virtcon_parse(const char *devname)
2502 QemuOptsList *device = qemu_find_opts("device");
2503 static int index = 0;
2504 char label[32];
2505 QemuOpts *bus_opts, *dev_opts;
2507 if (strcmp(devname, "none") == 0)
2508 return 0;
2509 if (index == MAX_VIRTIO_CONSOLES) {
2510 error_report("too many virtio consoles");
2511 exit(1);
2514 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2515 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2517 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2518 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2520 snprintf(label, sizeof(label), "virtcon%d", index);
2521 virtcon_hds[index] = qemu_chr_new(label, devname);
2522 if (!virtcon_hds[index]) {
2523 error_report("could not connect virtio console"
2524 " to character backend '%s'", devname);
2525 return -1;
2527 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2529 index++;
2530 return 0;
2533 static int sclp_parse(const char *devname)
2535 QemuOptsList *device = qemu_find_opts("device");
2536 static int index = 0;
2537 char label[32];
2538 QemuOpts *dev_opts;
2540 if (strcmp(devname, "none") == 0) {
2541 return 0;
2543 if (index == MAX_SCLP_CONSOLES) {
2544 error_report("too many sclp consoles");
2545 exit(1);
2548 assert(arch_type == QEMU_ARCH_S390X);
2550 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2551 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2553 snprintf(label, sizeof(label), "sclpcon%d", index);
2554 sclp_hds[index] = qemu_chr_new(label, devname);
2555 if (!sclp_hds[index]) {
2556 error_report("could not connect sclp console"
2557 " to character backend '%s'", devname);
2558 return -1;
2560 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2562 index++;
2563 return 0;
2566 static int debugcon_parse(const char *devname)
2568 QemuOpts *opts;
2570 if (!qemu_chr_new("debugcon", devname)) {
2571 exit(1);
2573 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2574 if (!opts) {
2575 error_report("already have a debugcon device");
2576 exit(1);
2578 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2579 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2580 return 0;
2583 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2585 const MachineClass *mc1 = a, *mc2 = b;
2586 int res;
2588 if (mc1->family == NULL) {
2589 if (mc2->family == NULL) {
2590 /* Compare standalone machine types against each other; they sort
2591 * in increasing order.
2593 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2594 object_class_get_name(OBJECT_CLASS(mc2)));
2597 /* Standalone machine types sort after families. */
2598 return 1;
2601 if (mc2->family == NULL) {
2602 /* Families sort before standalone machine types. */
2603 return -1;
2606 /* Families sort between each other alphabetically increasingly. */
2607 res = strcmp(mc1->family, mc2->family);
2608 if (res != 0) {
2609 return res;
2612 /* Within the same family, machine types sort in decreasing order. */
2613 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2614 object_class_get_name(OBJECT_CLASS(mc1)));
2617 static MachineClass *machine_parse(const char *name)
2619 MachineClass *mc = NULL;
2620 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2622 if (name) {
2623 mc = find_machine(name);
2625 if (mc) {
2626 g_slist_free(machines);
2627 return mc;
2629 if (name && !is_help_option(name)) {
2630 error_report("unsupported machine type");
2631 error_printf("Use -machine help to list supported machines\n");
2632 } else {
2633 printf("Supported machines are:\n");
2634 machines = g_slist_sort(machines, machine_class_cmp);
2635 for (el = machines; el; el = el->next) {
2636 MachineClass *mc = el->data;
2637 if (mc->alias) {
2638 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2640 printf("%-20s %s%s\n", mc->name, mc->desc,
2641 mc->is_default ? " (default)" : "");
2645 g_slist_free(machines);
2646 exit(!name || !is_help_option(name));
2649 void qemu_add_exit_notifier(Notifier *notify)
2651 notifier_list_add(&exit_notifiers, notify);
2654 void qemu_remove_exit_notifier(Notifier *notify)
2656 notifier_remove(notify);
2659 static void qemu_run_exit_notifiers(void)
2661 notifier_list_notify(&exit_notifiers, NULL);
2664 static bool machine_init_done;
2666 void qemu_add_machine_init_done_notifier(Notifier *notify)
2668 notifier_list_add(&machine_init_done_notifiers, notify);
2669 if (machine_init_done) {
2670 notify->notify(notify, NULL);
2674 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2676 notifier_remove(notify);
2679 static void qemu_run_machine_init_done_notifiers(void)
2681 notifier_list_notify(&machine_init_done_notifiers, NULL);
2682 machine_init_done = true;
2685 static const QEMUOption *lookup_opt(int argc, char **argv,
2686 const char **poptarg, int *poptind)
2688 const QEMUOption *popt;
2689 int optind = *poptind;
2690 char *r = argv[optind];
2691 const char *optarg;
2693 loc_set_cmdline(argv, optind, 1);
2694 optind++;
2695 /* Treat --foo the same as -foo. */
2696 if (r[1] == '-')
2697 r++;
2698 popt = qemu_options;
2699 for(;;) {
2700 if (!popt->name) {
2701 error_report("invalid option");
2702 exit(1);
2704 if (!strcmp(popt->name, r + 1))
2705 break;
2706 popt++;
2708 if (popt->flags & HAS_ARG) {
2709 if (optind >= argc) {
2710 error_report("requires an argument");
2711 exit(1);
2713 optarg = argv[optind++];
2714 loc_set_cmdline(argv, optind - 2, 2);
2715 } else {
2716 optarg = NULL;
2719 *poptarg = optarg;
2720 *poptind = optind;
2722 return popt;
2725 static MachineClass *select_machine(void)
2727 MachineClass *machine_class = find_default_machine();
2728 const char *optarg;
2729 QemuOpts *opts;
2730 Location loc;
2732 loc_push_none(&loc);
2734 opts = qemu_get_machine_opts();
2735 qemu_opts_loc_restore(opts);
2737 optarg = qemu_opt_get(opts, "type");
2738 if (optarg) {
2739 machine_class = machine_parse(optarg);
2742 if (!machine_class) {
2743 error_report("No machine specified, and there is no default");
2744 error_printf("Use -machine help to list supported machines\n");
2745 exit(1);
2748 loc_pop(&loc);
2749 return machine_class;
2752 static int machine_set_property(void *opaque,
2753 const char *name, const char *value,
2754 Error **errp)
2756 Object *obj = OBJECT(opaque);
2757 Error *local_err = NULL;
2758 char *p, *qom_name;
2760 if (strcmp(name, "type") == 0) {
2761 return 0;
2764 qom_name = g_strdup(name);
2765 for (p = qom_name; *p; p++) {
2766 if (*p == '_') {
2767 *p = '-';
2771 object_property_parse(obj, value, qom_name, &local_err);
2772 g_free(qom_name);
2774 if (local_err) {
2775 error_report_err(local_err);
2776 return -1;
2779 return 0;
2784 * Initial object creation happens before all other
2785 * QEMU data types are created. The majority of objects
2786 * can be created at this point. The rng-egd object
2787 * cannot be created here, as it depends on the chardev
2788 * already existing.
2790 static bool object_create_initial(const char *type)
2792 if (g_str_equal(type, "rng-egd")) {
2793 return false;
2797 * return false for concrete netfilters since
2798 * they depend on netdevs already existing
2800 if (g_str_equal(type, "filter-buffer") ||
2801 g_str_equal(type, "filter-dump") ||
2802 g_str_equal(type, "filter-mirror") ||
2803 g_str_equal(type, "filter-redirector") ||
2804 g_str_equal(type, "colo-compare") ||
2805 g_str_equal(type, "filter-rewriter") ||
2806 g_str_equal(type, "filter-replay")) {
2807 return false;
2810 /* Memory allocation by backends needs to be done
2811 * after configure_accelerator() (due to the tcg_enabled()
2812 * checks at memory_region_init_*()).
2814 * Also, allocation of large amounts of memory may delay
2815 * chardev initialization for too long, and trigger timeouts
2816 * on software that waits for a monitor socket to be created
2817 * (e.g. libvirt).
2819 if (g_str_has_prefix(type, "memory-backend-")) {
2820 return false;
2823 return true;
2828 * The remainder of object creation happens after the
2829 * creation of chardev, fsdev, net clients and device data types.
2831 static bool object_create_delayed(const char *type)
2833 return !object_create_initial(type);
2837 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2838 MachineClass *mc)
2840 uint64_t sz;
2841 const char *mem_str;
2842 const char *maxmem_str, *slots_str;
2843 const ram_addr_t default_ram_size = mc->default_ram_size;
2844 QemuOpts *opts = qemu_find_opts_singleton("memory");
2845 Location loc;
2847 loc_push_none(&loc);
2848 qemu_opts_loc_restore(opts);
2850 sz = 0;
2851 mem_str = qemu_opt_get(opts, "size");
2852 if (mem_str) {
2853 if (!*mem_str) {
2854 error_report("missing 'size' option value");
2855 exit(EXIT_FAILURE);
2858 sz = qemu_opt_get_size(opts, "size", ram_size);
2860 /* Fix up legacy suffix-less format */
2861 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2862 uint64_t overflow_check = sz;
2864 sz <<= 20;
2865 if ((sz >> 20) != overflow_check) {
2866 error_report("too large 'size' option value");
2867 exit(EXIT_FAILURE);
2872 /* backward compatibility behaviour for case "-m 0" */
2873 if (sz == 0) {
2874 sz = default_ram_size;
2877 sz = QEMU_ALIGN_UP(sz, 8192);
2878 ram_size = sz;
2879 if (ram_size != sz) {
2880 error_report("ram size too large");
2881 exit(EXIT_FAILURE);
2884 /* store value for the future use */
2885 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2886 *maxram_size = ram_size;
2888 maxmem_str = qemu_opt_get(opts, "maxmem");
2889 slots_str = qemu_opt_get(opts, "slots");
2890 if (maxmem_str && slots_str) {
2891 uint64_t slots;
2893 sz = qemu_opt_get_size(opts, "maxmem", 0);
2894 slots = qemu_opt_get_number(opts, "slots", 0);
2895 if (sz < ram_size) {
2896 error_report("invalid value of -m option maxmem: "
2897 "maximum memory size (0x%" PRIx64 ") must be at least "
2898 "the initial memory size (0x" RAM_ADDR_FMT ")",
2899 sz, ram_size);
2900 exit(EXIT_FAILURE);
2901 } else if (sz > ram_size) {
2902 if (!slots) {
2903 error_report("invalid value of -m option: maxmem was "
2904 "specified, but no hotplug slots were specified");
2905 exit(EXIT_FAILURE);
2907 } else if (slots) {
2908 error_report("invalid value of -m option maxmem: "
2909 "memory slots were specified but maximum memory size "
2910 "(0x%" PRIx64 ") is equal to the initial memory size "
2911 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2912 exit(EXIT_FAILURE);
2915 *maxram_size = sz;
2916 *ram_slots = slots;
2917 } else if ((!maxmem_str && slots_str) ||
2918 (maxmem_str && !slots_str)) {
2919 error_report("invalid -m option value: missing "
2920 "'%s' option", slots_str ? "maxmem" : "slots");
2921 exit(EXIT_FAILURE);
2924 loc_pop(&loc);
2927 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2929 GlobalProperty *g;
2931 g = g_malloc0(sizeof(*g));
2932 g->driver = qemu_opt_get(opts, "driver");
2933 g->property = qemu_opt_get(opts, "property");
2934 g->value = qemu_opt_get(opts, "value");
2935 g->user_provided = true;
2936 g->errp = &error_fatal;
2937 qdev_prop_register_global(g);
2938 return 0;
2941 static int qemu_read_default_config_file(void)
2943 int ret;
2945 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2946 if (ret < 0 && ret != -ENOENT) {
2947 return ret;
2950 return 0;
2953 int main(int argc, char **argv, char **envp)
2955 int i;
2956 int snapshot, linux_boot;
2957 const char *initrd_filename;
2958 const char *kernel_filename, *kernel_cmdline;
2959 const char *boot_order = NULL;
2960 const char *boot_once = NULL;
2961 DisplayState *ds;
2962 int cyls, heads, secs, translation;
2963 QemuOpts *opts, *machine_opts;
2964 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
2965 QemuOptsList *olist;
2966 int optind;
2967 const char *optarg;
2968 const char *loadvm = NULL;
2969 MachineClass *machine_class;
2970 const char *cpu_model;
2971 const char *vga_model = NULL;
2972 const char *qtest_chrdev = NULL;
2973 const char *qtest_log = NULL;
2974 const char *pid_file = NULL;
2975 const char *incoming = NULL;
2976 bool defconfig = true;
2977 bool userconfig = true;
2978 bool nographic = false;
2979 DisplayType display_type = DT_DEFAULT;
2980 int display_remote = 0;
2981 const char *log_mask = NULL;
2982 const char *log_file = NULL;
2983 char *trace_file = NULL;
2984 ram_addr_t maxram_size;
2985 uint64_t ram_slots = 0;
2986 FILE *vmstate_dump_file = NULL;
2987 Error *main_loop_err = NULL;
2988 Error *err = NULL;
2989 bool list_data_dirs = false;
2990 typedef struct BlockdevOptions_queue {
2991 BlockdevOptions *bdo;
2992 Location loc;
2993 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
2994 } BlockdevOptions_queue;
2995 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
2996 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
2998 module_call_init(MODULE_INIT_TRACE);
3000 qemu_init_cpu_list();
3001 qemu_init_cpu_loop();
3002 qemu_mutex_lock_iothread();
3004 atexit(qemu_run_exit_notifiers);
3005 error_set_progname(argv[0]);
3006 qemu_init_exec_dir(argv[0]);
3008 module_call_init(MODULE_INIT_QOM);
3009 monitor_init_qmp_commands();
3011 qemu_add_opts(&qemu_drive_opts);
3012 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3013 qemu_add_drive_opts(&qemu_common_drive_opts);
3014 qemu_add_drive_opts(&qemu_drive_opts);
3015 qemu_add_drive_opts(&bdrv_runtime_opts);
3016 qemu_add_opts(&qemu_chardev_opts);
3017 qemu_add_opts(&qemu_device_opts);
3018 qemu_add_opts(&qemu_netdev_opts);
3019 qemu_add_opts(&qemu_net_opts);
3020 qemu_add_opts(&qemu_rtc_opts);
3021 qemu_add_opts(&qemu_global_opts);
3022 qemu_add_opts(&qemu_mon_opts);
3023 qemu_add_opts(&qemu_trace_opts);
3024 qemu_add_opts(&qemu_option_rom_opts);
3025 qemu_add_opts(&qemu_machine_opts);
3026 qemu_add_opts(&qemu_accel_opts);
3027 qemu_add_opts(&qemu_mem_opts);
3028 qemu_add_opts(&qemu_smp_opts);
3029 qemu_add_opts(&qemu_boot_opts);
3030 qemu_add_opts(&qemu_sandbox_opts);
3031 qemu_add_opts(&qemu_add_fd_opts);
3032 qemu_add_opts(&qemu_object_opts);
3033 qemu_add_opts(&qemu_tpmdev_opts);
3034 qemu_add_opts(&qemu_realtime_opts);
3035 qemu_add_opts(&qemu_msg_opts);
3036 qemu_add_opts(&qemu_name_opts);
3037 qemu_add_opts(&qemu_numa_opts);
3038 qemu_add_opts(&qemu_icount_opts);
3039 qemu_add_opts(&qemu_semihosting_config_opts);
3040 qemu_add_opts(&qemu_fw_cfg_opts);
3041 module_call_init(MODULE_INIT_OPTS);
3043 runstate_init();
3045 if (qcrypto_init(&err) < 0) {
3046 error_reportf_err(err, "cannot initialize crypto: ");
3047 exit(1);
3049 rtc_clock = QEMU_CLOCK_HOST;
3051 QLIST_INIT (&vm_change_state_head);
3052 os_setup_early_signal_handling();
3054 cpu_model = NULL;
3055 snapshot = 0;
3056 cyls = heads = secs = 0;
3057 translation = BIOS_ATA_TRANSLATION_AUTO;
3059 nb_nics = 0;
3061 bdrv_init_with_whitelist();
3063 autostart = 1;
3065 /* first pass of option parsing */
3066 optind = 1;
3067 while (optind < argc) {
3068 if (argv[optind][0] != '-') {
3069 /* disk image */
3070 optind++;
3071 } else {
3072 const QEMUOption *popt;
3074 popt = lookup_opt(argc, argv, &optarg, &optind);
3075 switch (popt->index) {
3076 case QEMU_OPTION_nodefconfig:
3077 defconfig = false;
3078 break;
3079 case QEMU_OPTION_nouserconfig:
3080 userconfig = false;
3081 break;
3086 if (defconfig && userconfig) {
3087 if (qemu_read_default_config_file() < 0) {
3088 exit(1);
3092 /* second pass of option parsing */
3093 optind = 1;
3094 for(;;) {
3095 if (optind >= argc)
3096 break;
3097 if (argv[optind][0] != '-') {
3098 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3099 } else {
3100 const QEMUOption *popt;
3102 popt = lookup_opt(argc, argv, &optarg, &optind);
3103 if (!(popt->arch_mask & arch_type)) {
3104 error_report("Option not supported for this target");
3105 exit(1);
3107 switch(popt->index) {
3108 case QEMU_OPTION_no_kvm_irqchip: {
3109 olist = qemu_find_opts("machine");
3110 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3111 break;
3113 case QEMU_OPTION_cpu:
3114 /* hw initialization will check this */
3115 cpu_model = optarg;
3116 break;
3117 case QEMU_OPTION_hda:
3119 char buf[256];
3120 if (cyls == 0)
3121 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3122 else
3123 snprintf(buf, sizeof(buf),
3124 "%s,cyls=%d,heads=%d,secs=%d%s",
3125 HD_OPTS , cyls, heads, secs,
3126 translation == BIOS_ATA_TRANSLATION_LBA ?
3127 ",trans=lba" :
3128 translation == BIOS_ATA_TRANSLATION_NONE ?
3129 ",trans=none" : "");
3130 drive_add(IF_DEFAULT, 0, optarg, buf);
3131 break;
3133 case QEMU_OPTION_hdb:
3134 case QEMU_OPTION_hdc:
3135 case QEMU_OPTION_hdd:
3136 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3137 HD_OPTS);
3138 break;
3139 case QEMU_OPTION_blockdev:
3141 Visitor *v;
3142 BlockdevOptions_queue *bdo;
3144 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3145 if (!v) {
3146 error_report_err(err);
3147 exit(1);
3150 bdo = g_new(BlockdevOptions_queue, 1);
3151 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3152 &error_fatal);
3153 visit_free(v);
3154 loc_save(&bdo->loc);
3155 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3156 break;
3158 case QEMU_OPTION_drive:
3159 if (drive_def(optarg) == NULL) {
3160 exit(1);
3162 break;
3163 case QEMU_OPTION_set:
3164 if (qemu_set_option(optarg) != 0)
3165 exit(1);
3166 break;
3167 case QEMU_OPTION_global:
3168 if (qemu_global_option(optarg) != 0)
3169 exit(1);
3170 break;
3171 case QEMU_OPTION_mtdblock:
3172 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3173 break;
3174 case QEMU_OPTION_sd:
3175 drive_add(IF_SD, -1, optarg, SD_OPTS);
3176 break;
3177 case QEMU_OPTION_pflash:
3178 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3179 break;
3180 case QEMU_OPTION_snapshot:
3181 snapshot = 1;
3182 break;
3183 case QEMU_OPTION_hdachs:
3185 const char *p;
3186 p = optarg;
3187 cyls = strtol(p, (char **)&p, 0);
3188 if (cyls < 1 || cyls > 16383)
3189 goto chs_fail;
3190 if (*p != ',')
3191 goto chs_fail;
3192 p++;
3193 heads = strtol(p, (char **)&p, 0);
3194 if (heads < 1 || heads > 16)
3195 goto chs_fail;
3196 if (*p != ',')
3197 goto chs_fail;
3198 p++;
3199 secs = strtol(p, (char **)&p, 0);
3200 if (secs < 1 || secs > 63)
3201 goto chs_fail;
3202 if (*p == ',') {
3203 p++;
3204 if (!strcmp(p, "large")) {
3205 translation = BIOS_ATA_TRANSLATION_LARGE;
3206 } else if (!strcmp(p, "rechs")) {
3207 translation = BIOS_ATA_TRANSLATION_RECHS;
3208 } else if (!strcmp(p, "none")) {
3209 translation = BIOS_ATA_TRANSLATION_NONE;
3210 } else if (!strcmp(p, "lba")) {
3211 translation = BIOS_ATA_TRANSLATION_LBA;
3212 } else if (!strcmp(p, "auto")) {
3213 translation = BIOS_ATA_TRANSLATION_AUTO;
3214 } else {
3215 goto chs_fail;
3217 } else if (*p != '\0') {
3218 chs_fail:
3219 error_report("invalid physical CHS format");
3220 exit(1);
3222 if (hda_opts != NULL) {
3223 qemu_opt_set_number(hda_opts, "cyls", cyls,
3224 &error_abort);
3225 qemu_opt_set_number(hda_opts, "heads", heads,
3226 &error_abort);
3227 qemu_opt_set_number(hda_opts, "secs", secs,
3228 &error_abort);
3229 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3230 qemu_opt_set(hda_opts, "trans", "large",
3231 &error_abort);
3232 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3233 qemu_opt_set(hda_opts, "trans", "rechs",
3234 &error_abort);
3235 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3236 qemu_opt_set(hda_opts, "trans", "lba",
3237 &error_abort);
3238 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3239 qemu_opt_set(hda_opts, "trans", "none",
3240 &error_abort);
3244 error_report("'-hdachs' is deprecated, please use '-device"
3245 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3246 break;
3247 case QEMU_OPTION_numa:
3248 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3249 optarg, true);
3250 if (!opts) {
3251 exit(1);
3253 break;
3254 case QEMU_OPTION_display:
3255 display_type = select_display(optarg);
3256 break;
3257 case QEMU_OPTION_nographic:
3258 olist = qemu_find_opts("machine");
3259 qemu_opts_parse_noisily(olist, "graphics=off", false);
3260 nographic = true;
3261 display_type = DT_NONE;
3262 break;
3263 case QEMU_OPTION_curses:
3264 #ifdef CONFIG_CURSES
3265 display_type = DT_CURSES;
3266 #else
3267 error_report("curses support is disabled");
3268 exit(1);
3269 #endif
3270 break;
3271 case QEMU_OPTION_portrait:
3272 graphic_rotate = 90;
3273 break;
3274 case QEMU_OPTION_rotate:
3275 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3276 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3277 graphic_rotate != 180 && graphic_rotate != 270) {
3278 error_report("only 90, 180, 270 deg rotation is available");
3279 exit(1);
3281 break;
3282 case QEMU_OPTION_kernel:
3283 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3284 &error_abort);
3285 break;
3286 case QEMU_OPTION_initrd:
3287 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3288 &error_abort);
3289 break;
3290 case QEMU_OPTION_append:
3291 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3292 &error_abort);
3293 break;
3294 case QEMU_OPTION_dtb:
3295 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3296 &error_abort);
3297 break;
3298 case QEMU_OPTION_cdrom:
3299 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3300 break;
3301 case QEMU_OPTION_boot:
3302 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3303 optarg, true);
3304 if (!opts) {
3305 exit(1);
3307 break;
3308 case QEMU_OPTION_fda:
3309 case QEMU_OPTION_fdb:
3310 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3311 optarg, FD_OPTS);
3312 break;
3313 case QEMU_OPTION_no_fd_bootchk:
3314 fd_bootchk = 0;
3315 break;
3316 case QEMU_OPTION_netdev:
3317 default_net = 0;
3318 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3319 exit(1);
3321 break;
3322 case QEMU_OPTION_net:
3323 default_net = 0;
3324 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3325 exit(1);
3327 break;
3328 #ifdef CONFIG_LIBISCSI
3329 case QEMU_OPTION_iscsi:
3330 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3331 optarg, false);
3332 if (!opts) {
3333 exit(1);
3335 break;
3336 #endif
3337 #ifdef CONFIG_SLIRP
3338 case QEMU_OPTION_tftp:
3339 error_report("The -tftp option is deprecated. "
3340 "Please use '-netdev user,tftp=...' instead.");
3341 legacy_tftp_prefix = optarg;
3342 break;
3343 case QEMU_OPTION_bootp:
3344 error_report("The -bootp option is deprecated. "
3345 "Please use '-netdev user,bootfile=...' instead.");
3346 legacy_bootp_filename = optarg;
3347 break;
3348 case QEMU_OPTION_redir:
3349 error_report("The -redir option is deprecated. "
3350 "Please use '-netdev user,hostfwd=...' instead.");
3351 if (net_slirp_redir(optarg) < 0)
3352 exit(1);
3353 break;
3354 #endif
3355 case QEMU_OPTION_bt:
3356 add_device_config(DEV_BT, optarg);
3357 break;
3358 case QEMU_OPTION_audio_help:
3359 AUD_help ();
3360 exit (0);
3361 break;
3362 case QEMU_OPTION_soundhw:
3363 select_soundhw (optarg);
3364 break;
3365 case QEMU_OPTION_h:
3366 help(0);
3367 break;
3368 case QEMU_OPTION_version:
3369 version();
3370 exit(0);
3371 break;
3372 case QEMU_OPTION_m:
3373 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3374 optarg, true);
3375 if (!opts) {
3376 exit(EXIT_FAILURE);
3378 break;
3379 #ifdef CONFIG_TPM
3380 case QEMU_OPTION_tpmdev:
3381 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3382 exit(1);
3384 break;
3385 #endif
3386 case QEMU_OPTION_mempath:
3387 mem_path = optarg;
3388 break;
3389 case QEMU_OPTION_mem_prealloc:
3390 mem_prealloc = 1;
3391 break;
3392 case QEMU_OPTION_d:
3393 log_mask = optarg;
3394 break;
3395 case QEMU_OPTION_D:
3396 log_file = optarg;
3397 break;
3398 case QEMU_OPTION_DFILTER:
3399 qemu_set_dfilter_ranges(optarg, &error_fatal);
3400 break;
3401 case QEMU_OPTION_s:
3402 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3403 break;
3404 case QEMU_OPTION_gdb:
3405 add_device_config(DEV_GDB, optarg);
3406 break;
3407 case QEMU_OPTION_L:
3408 if (is_help_option(optarg)) {
3409 list_data_dirs = true;
3410 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3411 data_dir[data_dir_idx++] = optarg;
3413 break;
3414 case QEMU_OPTION_bios:
3415 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3416 &error_abort);
3417 break;
3418 case QEMU_OPTION_singlestep:
3419 singlestep = 1;
3420 break;
3421 case QEMU_OPTION_S:
3422 autostart = 0;
3423 break;
3424 case QEMU_OPTION_k:
3425 keyboard_layout = optarg;
3426 break;
3427 case QEMU_OPTION_localtime:
3428 rtc_utc = 0;
3429 break;
3430 case QEMU_OPTION_vga:
3431 vga_model = optarg;
3432 default_vga = 0;
3433 break;
3434 case QEMU_OPTION_g:
3436 const char *p;
3437 int w, h, depth;
3438 p = optarg;
3439 w = strtol(p, (char **)&p, 10);
3440 if (w <= 0) {
3441 graphic_error:
3442 error_report("invalid resolution or depth");
3443 exit(1);
3445 if (*p != 'x')
3446 goto graphic_error;
3447 p++;
3448 h = strtol(p, (char **)&p, 10);
3449 if (h <= 0)
3450 goto graphic_error;
3451 if (*p == 'x') {
3452 p++;
3453 depth = strtol(p, (char **)&p, 10);
3454 if (depth != 8 && depth != 15 && depth != 16 &&
3455 depth != 24 && depth != 32)
3456 goto graphic_error;
3457 } else if (*p == '\0') {
3458 depth = graphic_depth;
3459 } else {
3460 goto graphic_error;
3463 graphic_width = w;
3464 graphic_height = h;
3465 graphic_depth = depth;
3467 break;
3468 case QEMU_OPTION_echr:
3470 char *r;
3471 term_escape_char = strtol(optarg, &r, 0);
3472 if (r == optarg)
3473 printf("Bad argument to echr\n");
3474 break;
3476 case QEMU_OPTION_monitor:
3477 default_monitor = 0;
3478 if (strncmp(optarg, "none", 4)) {
3479 monitor_parse(optarg, "readline", false);
3481 break;
3482 case QEMU_OPTION_qmp:
3483 monitor_parse(optarg, "control", false);
3484 default_monitor = 0;
3485 break;
3486 case QEMU_OPTION_qmp_pretty:
3487 monitor_parse(optarg, "control", true);
3488 default_monitor = 0;
3489 break;
3490 case QEMU_OPTION_mon:
3491 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3492 true);
3493 if (!opts) {
3494 exit(1);
3496 default_monitor = 0;
3497 break;
3498 case QEMU_OPTION_chardev:
3499 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3500 optarg, true);
3501 if (!opts) {
3502 exit(1);
3504 break;
3505 case QEMU_OPTION_fsdev:
3506 olist = qemu_find_opts("fsdev");
3507 if (!olist) {
3508 error_report("fsdev support is disabled");
3509 exit(1);
3511 opts = qemu_opts_parse_noisily(olist, optarg, true);
3512 if (!opts) {
3513 exit(1);
3515 break;
3516 case QEMU_OPTION_virtfs: {
3517 QemuOpts *fsdev;
3518 QemuOpts *device;
3519 const char *writeout, *sock_fd, *socket;
3521 olist = qemu_find_opts("virtfs");
3522 if (!olist) {
3523 error_report("virtfs support is disabled");
3524 exit(1);
3526 opts = qemu_opts_parse_noisily(olist, optarg, true);
3527 if (!opts) {
3528 exit(1);
3531 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3532 qemu_opt_get(opts, "mount_tag") == NULL) {
3533 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3534 exit(1);
3536 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3537 qemu_opts_id(opts) ?:
3538 qemu_opt_get(opts, "mount_tag"),
3539 1, NULL);
3540 if (!fsdev) {
3541 error_report("duplicate or invalid fsdev id: %s",
3542 qemu_opt_get(opts, "mount_tag"));
3543 exit(1);
3546 writeout = qemu_opt_get(opts, "writeout");
3547 if (writeout) {
3548 #ifdef CONFIG_SYNC_FILE_RANGE
3549 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3550 #else
3551 error_report("writeout=immediate not supported "
3552 "on this platform");
3553 exit(1);
3554 #endif
3556 qemu_opt_set(fsdev, "fsdriver",
3557 qemu_opt_get(opts, "fsdriver"), &error_abort);
3558 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3559 &error_abort);
3560 qemu_opt_set(fsdev, "security_model",
3561 qemu_opt_get(opts, "security_model"),
3562 &error_abort);
3563 socket = qemu_opt_get(opts, "socket");
3564 if (socket) {
3565 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3567 sock_fd = qemu_opt_get(opts, "sock_fd");
3568 if (sock_fd) {
3569 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3572 qemu_opt_set_bool(fsdev, "readonly",
3573 qemu_opt_get_bool(opts, "readonly", 0),
3574 &error_abort);
3575 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3576 &error_abort);
3577 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3578 qemu_opt_set(device, "fsdev",
3579 qemu_opts_id(fsdev), &error_abort);
3580 qemu_opt_set(device, "mount_tag",
3581 qemu_opt_get(opts, "mount_tag"), &error_abort);
3582 break;
3584 case QEMU_OPTION_virtfs_synth: {
3585 QemuOpts *fsdev;
3586 QemuOpts *device;
3588 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3589 1, NULL);
3590 if (!fsdev) {
3591 error_report("duplicate option: %s", "virtfs_synth");
3592 exit(1);
3594 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3596 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3597 &error_abort);
3598 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3599 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3600 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3601 break;
3603 case QEMU_OPTION_serial:
3604 add_device_config(DEV_SERIAL, optarg);
3605 default_serial = 0;
3606 if (strncmp(optarg, "mon:", 4) == 0) {
3607 default_monitor = 0;
3609 break;
3610 case QEMU_OPTION_watchdog:
3611 if (watchdog) {
3612 error_report("only one watchdog option may be given");
3613 return 1;
3615 watchdog = optarg;
3616 break;
3617 case QEMU_OPTION_watchdog_action:
3618 if (select_watchdog_action(optarg) == -1) {
3619 error_report("unknown -watchdog-action parameter");
3620 exit(1);
3622 break;
3623 case QEMU_OPTION_virtiocon:
3624 add_device_config(DEV_VIRTCON, optarg);
3625 default_virtcon = 0;
3626 if (strncmp(optarg, "mon:", 4) == 0) {
3627 default_monitor = 0;
3629 break;
3630 case QEMU_OPTION_parallel:
3631 add_device_config(DEV_PARALLEL, optarg);
3632 default_parallel = 0;
3633 if (strncmp(optarg, "mon:", 4) == 0) {
3634 default_monitor = 0;
3636 break;
3637 case QEMU_OPTION_debugcon:
3638 add_device_config(DEV_DEBUGCON, optarg);
3639 break;
3640 case QEMU_OPTION_loadvm:
3641 loadvm = optarg;
3642 break;
3643 case QEMU_OPTION_full_screen:
3644 full_screen = 1;
3645 break;
3646 case QEMU_OPTION_no_frame:
3647 no_frame = 1;
3648 break;
3649 case QEMU_OPTION_alt_grab:
3650 alt_grab = 1;
3651 break;
3652 case QEMU_OPTION_ctrl_grab:
3653 ctrl_grab = 1;
3654 break;
3655 case QEMU_OPTION_no_quit:
3656 no_quit = 1;
3657 break;
3658 case QEMU_OPTION_sdl:
3659 #ifdef CONFIG_SDL
3660 display_type = DT_SDL;
3661 break;
3662 #else
3663 error_report("SDL support is disabled");
3664 exit(1);
3665 #endif
3666 case QEMU_OPTION_pidfile:
3667 pid_file = optarg;
3668 break;
3669 case QEMU_OPTION_win2k_hack:
3670 win2k_install_hack = 1;
3671 break;
3672 case QEMU_OPTION_rtc_td_hack: {
3673 static GlobalProperty slew_lost_ticks = {
3674 .driver = "mc146818rtc",
3675 .property = "lost_tick_policy",
3676 .value = "slew",
3679 qdev_prop_register_global(&slew_lost_ticks);
3680 break;
3682 case QEMU_OPTION_acpitable:
3683 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3684 optarg, true);
3685 if (!opts) {
3686 exit(1);
3688 acpi_table_add(opts, &error_fatal);
3689 break;
3690 case QEMU_OPTION_smbios:
3691 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3692 optarg, false);
3693 if (!opts) {
3694 exit(1);
3696 smbios_entry_add(opts, &error_fatal);
3697 break;
3698 case QEMU_OPTION_fwcfg:
3699 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3700 optarg, true);
3701 if (opts == NULL) {
3702 exit(1);
3704 break;
3705 case QEMU_OPTION_enable_kvm:
3706 olist = qemu_find_opts("machine");
3707 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3708 break;
3709 case QEMU_OPTION_enable_hax:
3710 olist = qemu_find_opts("machine");
3711 qemu_opts_parse_noisily(olist, "accel=hax", false);
3712 break;
3713 case QEMU_OPTION_M:
3714 case QEMU_OPTION_machine:
3715 olist = qemu_find_opts("machine");
3716 opts = qemu_opts_parse_noisily(olist, optarg, true);
3717 if (!opts) {
3718 exit(1);
3720 break;
3721 case QEMU_OPTION_no_kvm:
3722 olist = qemu_find_opts("machine");
3723 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3724 break;
3725 case QEMU_OPTION_no_kvm_pit: {
3726 error_report("warning: ignoring deprecated option");
3727 break;
3729 case QEMU_OPTION_no_kvm_pit_reinjection: {
3730 static GlobalProperty kvm_pit_lost_tick_policy = {
3731 .driver = "kvm-pit",
3732 .property = "lost_tick_policy",
3733 .value = "discard",
3736 error_report("warning: deprecated, replaced by "
3737 "-global kvm-pit.lost_tick_policy=discard");
3738 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3739 break;
3741 case QEMU_OPTION_accel: {
3742 QemuOpts *accel_opts;
3744 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3745 optarg, true);
3746 optarg = qemu_opt_get(accel_opts, "accel");
3747 if (!optarg || is_help_option(optarg)) {
3748 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3749 exit(1);
3751 accel_opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3752 false, &error_abort);
3753 qemu_opt_set(accel_opts, "accel", optarg, &error_abort);
3754 break;
3756 case QEMU_OPTION_usb:
3757 olist = qemu_find_opts("machine");
3758 qemu_opts_parse_noisily(olist, "usb=on", false);
3759 break;
3760 case QEMU_OPTION_usbdevice:
3761 olist = qemu_find_opts("machine");
3762 qemu_opts_parse_noisily(olist, "usb=on", false);
3763 add_device_config(DEV_USB, optarg);
3764 break;
3765 case QEMU_OPTION_device:
3766 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3767 optarg, true)) {
3768 exit(1);
3770 break;
3771 case QEMU_OPTION_smp:
3772 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3773 optarg, true)) {
3774 exit(1);
3776 break;
3777 case QEMU_OPTION_vnc:
3778 vnc_parse(optarg, &error_fatal);
3779 break;
3780 case QEMU_OPTION_no_acpi:
3781 acpi_enabled = 0;
3782 break;
3783 case QEMU_OPTION_no_hpet:
3784 no_hpet = 1;
3785 break;
3786 case QEMU_OPTION_balloon:
3787 if (balloon_parse(optarg) < 0) {
3788 error_report("unknown -balloon argument %s", optarg);
3789 exit(1);
3791 break;
3792 case QEMU_OPTION_no_reboot:
3793 no_reboot = 1;
3794 break;
3795 case QEMU_OPTION_no_shutdown:
3796 no_shutdown = 1;
3797 break;
3798 case QEMU_OPTION_show_cursor:
3799 cursor_hide = 0;
3800 break;
3801 case QEMU_OPTION_uuid:
3802 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3803 error_report("failed to parse UUID string: wrong format");
3804 exit(1);
3806 qemu_uuid_set = true;
3807 break;
3808 case QEMU_OPTION_option_rom:
3809 if (nb_option_roms >= MAX_OPTION_ROMS) {
3810 error_report("too many option ROMs");
3811 exit(1);
3813 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3814 optarg, true);
3815 if (!opts) {
3816 exit(1);
3818 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3819 option_rom[nb_option_roms].bootindex =
3820 qemu_opt_get_number(opts, "bootindex", -1);
3821 if (!option_rom[nb_option_roms].name) {
3822 error_report("Option ROM file is not specified");
3823 exit(1);
3825 nb_option_roms++;
3826 break;
3827 case QEMU_OPTION_semihosting:
3828 semihosting.enabled = true;
3829 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3830 break;
3831 case QEMU_OPTION_semihosting_config:
3832 semihosting.enabled = true;
3833 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3834 optarg, false);
3835 if (opts != NULL) {
3836 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3837 true);
3838 const char *target = qemu_opt_get(opts, "target");
3839 if (target != NULL) {
3840 if (strcmp("native", target) == 0) {
3841 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3842 } else if (strcmp("gdb", target) == 0) {
3843 semihosting.target = SEMIHOSTING_TARGET_GDB;
3844 } else if (strcmp("auto", target) == 0) {
3845 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3846 } else {
3847 error_report("unsupported semihosting-config %s",
3848 optarg);
3849 exit(1);
3851 } else {
3852 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3854 /* Set semihosting argument count and vector */
3855 qemu_opt_foreach(opts, add_semihosting_arg,
3856 &semihosting, NULL);
3857 } else {
3858 error_report("unsupported semihosting-config %s", optarg);
3859 exit(1);
3861 break;
3862 case QEMU_OPTION_tdf:
3863 error_report("warning: ignoring deprecated option");
3864 break;
3865 case QEMU_OPTION_name:
3866 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3867 optarg, true);
3868 if (!opts) {
3869 exit(1);
3871 break;
3872 case QEMU_OPTION_prom_env:
3873 if (nb_prom_envs >= MAX_PROM_ENVS) {
3874 error_report("too many prom variables");
3875 exit(1);
3877 prom_envs[nb_prom_envs] = optarg;
3878 nb_prom_envs++;
3879 break;
3880 case QEMU_OPTION_old_param:
3881 old_param = 1;
3882 break;
3883 case QEMU_OPTION_clock:
3884 /* Clock options no longer exist. Keep this option for
3885 * backward compatibility.
3887 break;
3888 case QEMU_OPTION_startdate:
3889 configure_rtc_date_offset(optarg, 1);
3890 break;
3891 case QEMU_OPTION_rtc:
3892 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3893 false);
3894 if (!opts) {
3895 exit(1);
3897 configure_rtc(opts);
3898 break;
3899 case QEMU_OPTION_tb_size:
3900 tcg_tb_size = strtol(optarg, NULL, 0);
3901 if (tcg_tb_size < 0) {
3902 tcg_tb_size = 0;
3904 break;
3905 case QEMU_OPTION_icount:
3906 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3907 optarg, true);
3908 if (!icount_opts) {
3909 exit(1);
3911 break;
3912 case QEMU_OPTION_incoming:
3913 if (!incoming) {
3914 runstate_set(RUN_STATE_INMIGRATE);
3916 incoming = optarg;
3917 break;
3918 case QEMU_OPTION_only_migratable:
3919 only_migratable = 1;
3920 break;
3921 case QEMU_OPTION_nodefaults:
3922 has_defaults = 0;
3923 break;
3924 case QEMU_OPTION_xen_domid:
3925 if (!(xen_available())) {
3926 error_report("Option not supported for this target");
3927 exit(1);
3929 xen_domid = atoi(optarg);
3930 break;
3931 case QEMU_OPTION_xen_create:
3932 if (!(xen_available())) {
3933 error_report("Option not supported for this target");
3934 exit(1);
3936 xen_mode = XEN_CREATE;
3937 break;
3938 case QEMU_OPTION_xen_attach:
3939 if (!(xen_available())) {
3940 error_report("Option not supported for this target");
3941 exit(1);
3943 xen_mode = XEN_ATTACH;
3944 break;
3945 case QEMU_OPTION_xen_domid_restrict:
3946 if (!(xen_available())) {
3947 error_report("Option not supported for this target");
3948 exit(1);
3950 xen_domid_restrict = true;
3951 break;
3952 case QEMU_OPTION_trace:
3953 g_free(trace_file);
3954 trace_file = trace_opt_parse(optarg);
3955 break;
3956 case QEMU_OPTION_readconfig:
3958 int ret = qemu_read_config_file(optarg);
3959 if (ret < 0) {
3960 error_report("read config %s: %s", optarg,
3961 strerror(-ret));
3962 exit(1);
3964 break;
3966 case QEMU_OPTION_spice:
3967 olist = qemu_find_opts("spice");
3968 if (!olist) {
3969 error_report("spice support is disabled");
3970 exit(1);
3972 opts = qemu_opts_parse_noisily(olist, optarg, false);
3973 if (!opts) {
3974 exit(1);
3976 display_remote++;
3977 break;
3978 case QEMU_OPTION_writeconfig:
3980 FILE *fp;
3981 if (strcmp(optarg, "-") == 0) {
3982 fp = stdout;
3983 } else {
3984 fp = fopen(optarg, "w");
3985 if (fp == NULL) {
3986 error_report("open %s: %s", optarg,
3987 strerror(errno));
3988 exit(1);
3991 qemu_config_write(fp);
3992 if (fp != stdout) {
3993 fclose(fp);
3995 break;
3997 case QEMU_OPTION_qtest:
3998 qtest_chrdev = optarg;
3999 break;
4000 case QEMU_OPTION_qtest_log:
4001 qtest_log = optarg;
4002 break;
4003 case QEMU_OPTION_sandbox:
4004 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4005 optarg, true);
4006 if (!opts) {
4007 exit(1);
4009 break;
4010 case QEMU_OPTION_add_fd:
4011 #ifndef _WIN32
4012 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4013 optarg, false);
4014 if (!opts) {
4015 exit(1);
4017 #else
4018 error_report("File descriptor passing is disabled on this "
4019 "platform");
4020 exit(1);
4021 #endif
4022 break;
4023 case QEMU_OPTION_object:
4024 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4025 optarg, true);
4026 if (!opts) {
4027 exit(1);
4029 break;
4030 case QEMU_OPTION_realtime:
4031 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4032 optarg, false);
4033 if (!opts) {
4034 exit(1);
4036 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4037 break;
4038 case QEMU_OPTION_msg:
4039 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4040 false);
4041 if (!opts) {
4042 exit(1);
4044 configure_msg(opts);
4045 break;
4046 case QEMU_OPTION_dump_vmstate:
4047 if (vmstate_dump_file) {
4048 error_report("only one '-dump-vmstate' "
4049 "option may be given");
4050 exit(1);
4052 vmstate_dump_file = fopen(optarg, "w");
4053 if (vmstate_dump_file == NULL) {
4054 error_report("open %s: %s", optarg, strerror(errno));
4055 exit(1);
4057 break;
4058 default:
4059 os_parse_cmd_args(popt->index, optarg);
4064 * Clear error location left behind by the loop.
4065 * Best done right after the loop. Do not insert code here!
4067 loc_set_none();
4069 replay_configure(icount_opts);
4071 machine_class = select_machine();
4073 set_memory_options(&ram_slots, &maxram_size, machine_class);
4075 os_daemonize();
4077 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4078 error_report("could not acquire pid file: %s", strerror(errno));
4079 exit(1);
4082 if (qemu_init_main_loop(&main_loop_err)) {
4083 error_report_err(main_loop_err);
4084 exit(1);
4087 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4088 parse_sandbox, NULL, NULL)) {
4089 exit(1);
4092 if (qemu_opts_foreach(qemu_find_opts("name"),
4093 parse_name, NULL, NULL)) {
4094 exit(1);
4097 #ifndef _WIN32
4098 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4099 parse_add_fd, NULL, NULL)) {
4100 exit(1);
4103 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4104 cleanup_add_fd, NULL, NULL)) {
4105 exit(1);
4107 #endif
4109 current_machine = MACHINE(object_new(object_class_get_name(
4110 OBJECT_CLASS(machine_class))));
4111 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4112 exit(0);
4114 object_property_add_child(object_get_root(), "machine",
4115 OBJECT(current_machine), &error_abort);
4117 if (machine_class->minimum_page_bits) {
4118 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4119 /* This would be a board error: specifying a minimum smaller than
4120 * a target's compile-time fixed setting.
4122 g_assert_not_reached();
4126 cpu_exec_init_all();
4128 if (machine_class->hw_version) {
4129 qemu_set_hw_version(machine_class->hw_version);
4132 if (cpu_model && is_help_option(cpu_model)) {
4133 list_cpus(stdout, &fprintf, cpu_model);
4134 exit(0);
4137 if (!trace_init_backends()) {
4138 exit(1);
4140 trace_init_file(trace_file);
4142 /* Open the logfile at this point and set the log mask if necessary.
4144 if (log_file) {
4145 qemu_set_log_filename(log_file, &error_fatal);
4148 if (log_mask) {
4149 int mask;
4150 mask = qemu_str_to_log_mask(log_mask);
4151 if (!mask) {
4152 qemu_print_log_usage(stdout);
4153 exit(1);
4155 qemu_set_log(mask);
4156 } else {
4157 qemu_set_log(0);
4160 /* If no data_dir is specified then try to find it relative to the
4161 executable path. */
4162 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4163 data_dir[data_dir_idx] = os_find_datadir();
4164 if (data_dir[data_dir_idx] != NULL) {
4165 data_dir_idx++;
4168 /* If all else fails use the install path specified when building. */
4169 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4170 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4173 /* -L help lists the data directories and exits. */
4174 if (list_data_dirs) {
4175 for (i = 0; i < data_dir_idx; i++) {
4176 printf("%s\n", data_dir[i]);
4178 exit(0);
4181 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4183 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4184 if (max_cpus > machine_class->max_cpus) {
4185 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4186 "supported by machine '%s' (%d)", max_cpus,
4187 machine_class->name, machine_class->max_cpus);
4188 exit(1);
4192 * Get the default machine options from the machine if it is not already
4193 * specified either by the configuration file or by the command line.
4195 if (machine_class->default_machine_opts) {
4196 qemu_opts_set_defaults(qemu_find_opts("machine"),
4197 machine_class->default_machine_opts, 0);
4200 qemu_opts_foreach(qemu_find_opts("device"),
4201 default_driver_check, NULL, NULL);
4202 qemu_opts_foreach(qemu_find_opts("global"),
4203 default_driver_check, NULL, NULL);
4205 if (!vga_model && !default_vga) {
4206 vga_interface_type = VGA_DEVICE;
4208 if (!has_defaults || machine_class->no_serial) {
4209 default_serial = 0;
4211 if (!has_defaults || machine_class->no_parallel) {
4212 default_parallel = 0;
4214 if (!has_defaults || !machine_class->use_virtcon) {
4215 default_virtcon = 0;
4217 if (!has_defaults || !machine_class->use_sclp) {
4218 default_sclp = 0;
4220 if (!has_defaults || machine_class->no_floppy) {
4221 default_floppy = 0;
4223 if (!has_defaults || machine_class->no_cdrom) {
4224 default_cdrom = 0;
4226 if (!has_defaults || machine_class->no_sdcard) {
4227 default_sdcard = 0;
4229 if (!has_defaults) {
4230 default_monitor = 0;
4231 default_net = 0;
4232 default_vga = 0;
4235 if (is_daemonized()) {
4236 /* According to documentation and historically, -nographic redirects
4237 * serial port, parallel port and monitor to stdio, which does not work
4238 * with -daemonize. We can redirect these to null instead, but since
4239 * -nographic is legacy, let's just error out.
4240 * We disallow -nographic only if all other ports are not redirected
4241 * explicitly, to not break existing legacy setups which uses
4242 * -nographic _and_ redirects all ports explicitly - this is valid
4243 * usage, -nographic is just a no-op in this case.
4245 if (nographic
4246 && (default_parallel || default_serial
4247 || default_monitor || default_virtcon)) {
4248 error_report("-nographic cannot be used with -daemonize");
4249 exit(1);
4251 #ifdef CONFIG_CURSES
4252 if (display_type == DT_CURSES) {
4253 error_report("curses display cannot be used with -daemonize");
4254 exit(1);
4256 #endif
4259 if (nographic) {
4260 if (default_parallel)
4261 add_device_config(DEV_PARALLEL, "null");
4262 if (default_serial && default_monitor) {
4263 add_device_config(DEV_SERIAL, "mon:stdio");
4264 } else if (default_virtcon && default_monitor) {
4265 add_device_config(DEV_VIRTCON, "mon:stdio");
4266 } else if (default_sclp && default_monitor) {
4267 add_device_config(DEV_SCLP, "mon:stdio");
4268 } else {
4269 if (default_serial)
4270 add_device_config(DEV_SERIAL, "stdio");
4271 if (default_virtcon)
4272 add_device_config(DEV_VIRTCON, "stdio");
4273 if (default_sclp) {
4274 add_device_config(DEV_SCLP, "stdio");
4276 if (default_monitor)
4277 monitor_parse("stdio", "readline", false);
4279 } else {
4280 if (default_serial)
4281 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4282 if (default_parallel)
4283 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4284 if (default_monitor)
4285 monitor_parse("vc:80Cx24C", "readline", false);
4286 if (default_virtcon)
4287 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4288 if (default_sclp) {
4289 add_device_config(DEV_SCLP, "vc:80Cx24C");
4293 #if defined(CONFIG_VNC)
4294 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4295 display_remote++;
4297 #endif
4298 if (display_type == DT_DEFAULT && !display_remote) {
4299 #if defined(CONFIG_GTK)
4300 display_type = DT_GTK;
4301 #elif defined(CONFIG_SDL)
4302 display_type = DT_SDL;
4303 #elif defined(CONFIG_COCOA)
4304 display_type = DT_COCOA;
4305 #elif defined(CONFIG_VNC)
4306 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4307 #else
4308 display_type = DT_NONE;
4309 #endif
4312 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4313 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4314 "for SDL, ignoring option");
4316 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4317 error_report("-no-quit is only valid for GTK and SDL, "
4318 "ignoring option");
4321 if (display_type == DT_GTK) {
4322 early_gtk_display_init(request_opengl);
4325 if (display_type == DT_SDL) {
4326 sdl_display_early_init(request_opengl);
4329 qemu_console_early_init();
4331 if (request_opengl == 1 && display_opengl == 0) {
4332 #if defined(CONFIG_OPENGL)
4333 error_report("OpenGL is not supported by the display");
4334 #else
4335 error_report("OpenGL support is disabled");
4336 #endif
4337 exit(1);
4340 page_size_init();
4341 socket_init();
4343 if (qemu_opts_foreach(qemu_find_opts("object"),
4344 user_creatable_add_opts_foreach,
4345 object_create_initial, NULL)) {
4346 exit(1);
4349 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4350 chardev_init_func, NULL, NULL)) {
4351 exit(1);
4354 #ifdef CONFIG_VIRTFS
4355 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4356 fsdev_init_func, NULL, NULL)) {
4357 exit(1);
4359 #endif
4361 if (qemu_opts_foreach(qemu_find_opts("device"),
4362 device_help_func, NULL, NULL)) {
4363 exit(0);
4366 machine_opts = qemu_get_machine_opts();
4367 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4368 NULL)) {
4369 object_unref(OBJECT(current_machine));
4370 exit(1);
4373 configure_accelerator(current_machine);
4375 if (qtest_chrdev) {
4376 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4379 machine_opts = qemu_get_machine_opts();
4380 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4381 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4382 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4383 bios_name = qemu_opt_get(machine_opts, "firmware");
4385 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4386 if (opts) {
4387 boot_order = qemu_opt_get(opts, "order");
4388 if (boot_order) {
4389 validate_bootdevices(boot_order, &error_fatal);
4392 boot_once = qemu_opt_get(opts, "once");
4393 if (boot_once) {
4394 validate_bootdevices(boot_once, &error_fatal);
4397 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4398 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4401 if (!boot_order) {
4402 boot_order = machine_class->default_boot_order;
4405 if (!kernel_cmdline) {
4406 kernel_cmdline = "";
4407 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4410 linux_boot = (kernel_filename != NULL);
4412 if (!linux_boot && *kernel_cmdline != '\0') {
4413 error_report("-append only allowed with -kernel option");
4414 exit(1);
4417 if (!linux_boot && initrd_filename != NULL) {
4418 error_report("-initrd only allowed with -kernel option");
4419 exit(1);
4422 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4423 /* fall back to the -kernel/-append */
4424 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4427 os_set_line_buffering();
4429 /* spice needs the timers to be initialized by this point */
4430 qemu_spice_init();
4432 cpu_ticks_init();
4433 if (icount_opts) {
4434 if (!tcg_enabled()) {
4435 error_report("-icount is not allowed with hardware virtualization");
4436 exit(1);
4438 configure_icount(icount_opts, &error_abort);
4439 qemu_opts_del(icount_opts);
4442 qemu_tcg_configure(accel_opts, &error_fatal);
4444 if (default_net) {
4445 QemuOptsList *net = qemu_find_opts("net");
4446 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4447 #ifdef CONFIG_SLIRP
4448 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4449 #endif
4452 colo_info_init();
4454 if (net_init_clients() < 0) {
4455 exit(1);
4458 if (qemu_opts_foreach(qemu_find_opts("object"),
4459 user_creatable_add_opts_foreach,
4460 object_create_delayed, NULL)) {
4461 exit(1);
4464 #ifdef CONFIG_TPM
4465 if (tpm_init() < 0) {
4466 exit(1);
4468 #endif
4470 /* init the bluetooth world */
4471 if (foreach_device_config(DEV_BT, bt_parse))
4472 exit(1);
4474 if (!xen_enabled()) {
4475 /* On 32-bit hosts, QEMU is limited by virtual address space */
4476 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4477 error_report("at most 2047 MB RAM can be simulated");
4478 exit(1);
4482 blk_mig_init();
4483 ram_mig_init();
4485 /* If the currently selected machine wishes to override the units-per-bus
4486 * property of its default HBA interface type, do so now. */
4487 if (machine_class->units_per_default_bus) {
4488 override_max_devs(machine_class->block_default_type,
4489 machine_class->units_per_default_bus);
4492 /* open the virtual block devices */
4493 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4494 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4496 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4497 loc_push_restore(&bdo->loc);
4498 qmp_blockdev_add(bdo->bdo, &error_fatal);
4499 loc_pop(&bdo->loc);
4500 qapi_free_BlockdevOptions(bdo->bdo);
4501 g_free(bdo);
4503 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4504 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4505 NULL, NULL);
4507 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4508 &machine_class->block_default_type, NULL)) {
4509 exit(1);
4512 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4513 CDROM_OPTS);
4514 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4515 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4517 parse_numa_opts(current_machine);
4519 if (qemu_opts_foreach(qemu_find_opts("mon"),
4520 mon_init_func, NULL, NULL)) {
4521 exit(1);
4524 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4525 exit(1);
4526 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4527 exit(1);
4528 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4529 exit(1);
4530 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4531 exit(1);
4533 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4534 exit(1);
4536 /* If no default VGA is requested, the default is "none". */
4537 if (default_vga) {
4538 if (machine_class->default_display) {
4539 vga_model = machine_class->default_display;
4540 } else if (vga_interface_available(VGA_CIRRUS)) {
4541 vga_model = "cirrus";
4542 } else if (vga_interface_available(VGA_STD)) {
4543 vga_model = "std";
4546 if (vga_model) {
4547 select_vgahw(vga_model);
4550 if (watchdog) {
4551 i = select_watchdog(watchdog);
4552 if (i > 0)
4553 exit (i == 1 ? 1 : 0);
4556 machine_register_compat_props(current_machine);
4558 qemu_opts_foreach(qemu_find_opts("global"),
4559 global_init_func, NULL, NULL);
4561 /* This checkpoint is required by replay to separate prior clock
4562 reading from the other reads, because timer polling functions query
4563 clock values from the log. */
4564 replay_checkpoint(CHECKPOINT_INIT);
4565 qdev_machine_init();
4567 current_machine->ram_size = ram_size;
4568 current_machine->maxram_size = maxram_size;
4569 current_machine->ram_slots = ram_slots;
4570 current_machine->boot_order = boot_order;
4571 current_machine->cpu_model = cpu_model;
4573 machine_run_board_init(current_machine);
4575 realtime_init();
4577 audio_init();
4579 if (hax_enabled()) {
4580 hax_sync_vcpus();
4583 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4584 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4585 exit(1);
4588 /* init USB devices */
4589 if (machine_usb(current_machine)) {
4590 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4591 exit(1);
4594 /* Check if IGD GFX passthrough. */
4595 igd_gfx_passthru();
4597 /* init generic devices */
4598 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4599 if (qemu_opts_foreach(qemu_find_opts("device"),
4600 device_init_func, NULL, NULL)) {
4601 exit(1);
4604 cpu_synchronize_all_post_init();
4606 rom_reset_order_override();
4609 * Create frontends for -drive if=scsi leftovers.
4610 * Normally, frontends for -drive get created by machine
4611 * initialization for onboard SCSI HBAs. However, we create a few
4612 * more ever since SCSI qdevification, but this is pretty much an
4613 * implementation accident, and deprecated.
4615 scsi_legacy_handle_cmdline();
4617 /* Did we create any drives that we failed to create a device for? */
4618 drive_check_orphaned();
4620 /* Don't warn about the default network setup that you get if
4621 * no command line -net or -netdev options are specified. There
4622 * are two cases that we would otherwise complain about:
4623 * (1) board doesn't support a NIC but the implicit "-net nic"
4624 * requested one
4625 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4626 * sets up a nic that isn't connected to anything.
4628 if (!default_net) {
4629 net_check_clients();
4633 if (boot_once) {
4634 qemu_boot_set(boot_once, &error_fatal);
4635 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4638 ds = init_displaystate();
4640 /* init local displays */
4641 switch (display_type) {
4642 case DT_CURSES:
4643 curses_display_init(ds, full_screen);
4644 break;
4645 case DT_SDL:
4646 sdl_display_init(ds, full_screen, no_frame);
4647 break;
4648 case DT_COCOA:
4649 cocoa_display_init(ds, full_screen);
4650 break;
4651 case DT_GTK:
4652 gtk_display_init(ds, full_screen, grab_on_hover);
4653 break;
4654 default:
4655 break;
4658 /* must be after terminal init, SDL library changes signal handlers */
4659 os_setup_signal_handling();
4661 /* init remote displays */
4662 #ifdef CONFIG_VNC
4663 qemu_opts_foreach(qemu_find_opts("vnc"),
4664 vnc_init_func, NULL, NULL);
4665 #endif
4667 if (using_spice) {
4668 qemu_spice_display_init();
4671 #ifdef CONFIG_OPENGL
4672 if (display_type == DT_EGL) {
4673 egl_headless_init();
4675 #endif
4677 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4678 exit(1);
4681 qdev_machine_creation_done();
4683 /* TODO: once all bus devices are qdevified, this should be done
4684 * when bus is created by qdev.c */
4685 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4686 qemu_run_machine_init_done_notifiers();
4688 if (rom_check_and_register_reset() != 0) {
4689 error_report("rom check and register reset failed");
4690 exit(1);
4693 replay_start();
4695 /* This checkpoint is required by replay to separate prior clock
4696 reading from the other reads, because timer polling functions query
4697 clock values from the log. */
4698 replay_checkpoint(CHECKPOINT_RESET);
4699 qemu_system_reset(VMRESET_SILENT);
4700 register_global_state();
4701 if (replay_mode != REPLAY_MODE_NONE) {
4702 replay_vmstate_init();
4703 } else if (loadvm) {
4704 Error *local_err = NULL;
4705 if (load_vmstate(loadvm, &local_err) < 0) {
4706 error_report_err(local_err);
4707 autostart = 0;
4711 qdev_prop_check_globals();
4712 if (vmstate_dump_file) {
4713 /* dump and exit */
4714 dump_vmstate_json_to_file(vmstate_dump_file);
4715 return 0;
4718 if (incoming) {
4719 Error *local_err = NULL;
4720 qemu_start_incoming_migration(incoming, &local_err);
4721 if (local_err) {
4722 error_reportf_err(local_err, "-incoming %s: ", incoming);
4723 exit(1);
4725 } else if (autostart) {
4726 vm_start();
4729 os_setup_post();
4731 main_loop();
4732 replay_disable_events();
4733 iothread_stop_all();
4735 bdrv_close_all();
4736 pause_all_vcpus();
4737 res_free();
4739 /* vhost-user must be cleaned up before chardevs. */
4740 net_cleanup();
4741 audio_cleanup();
4742 monitor_cleanup();
4743 qemu_chr_cleanup();
4744 /* TODO: unref root container, check all devices are ok */
4746 return 0;