Merge tag 'v2.10.0-rc0'
[qemu/ar7.git] / vl.c
blob983572b9f1aca4976bb725272f942347c16bfe44
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 #define SDL_MAIN_HANDLED
41 #include <SDL.h>
42 #if SDL_MAJOR_VERSION == 1
43 int qemu_main(int argc, char **argv);
44 int main(int argc, char **argv)
46 return qemu_main(argc, argv);
48 #undef main
49 #define main qemu_main
50 #elif SDL_MAJOR_VERSION == 2
51 #undef main
52 #else
53 #error undefined SDL version
54 #endif
55 #endif
56 #endif /* CONFIG_SDL */
58 #ifdef CONFIG_COCOA
59 #undef main
60 #define main qemu_main
61 #endif /* CONFIG_COCOA */
64 #include "qemu/error-report.h"
65 #include "qemu/sockets.h"
66 #include "hw/hw.h"
67 #include "hw/boards.h"
68 #include "sysemu/accel.h"
69 #include "hw/usb.h"
70 #include "hw/i386/pc.h"
71 #include "hw/isa/isa.h"
72 #include "hw/scsi/scsi.h"
73 #include "hw/bt.h"
74 #include "sysemu/watchdog.h"
75 #include "hw/smbios/smbios.h"
76 #include "hw/acpi/acpi.h"
77 #include "hw/xen/xen.h"
78 #include "hw/qdev.h"
79 #include "hw/loader.h"
80 #include "monitor/qdev.h"
81 #include "sysemu/bt.h"
82 #include "net/net.h"
83 #include "net/slirp.h"
84 #include "monitor/monitor.h"
85 #include "ui/console.h"
86 #include "ui/input.h"
87 #include "sysemu/sysemu.h"
88 #include "sysemu/numa.h"
89 #include "exec/gdbstub.h"
90 #include "qemu/timer.h"
91 #include "chardev/char.h"
92 #include "qemu/bitmap.h"
93 #include "qemu/log.h"
94 #include "sysemu/blockdev.h"
95 #include "hw/block/block.h"
96 #include "migration/misc.h"
97 #include "migration/snapshot.h"
98 #include "migration/global_state.h"
99 #include "sysemu/tpm.h"
100 #include "sysemu/dma.h"
101 #include "hw/audio/soundhw.h"
102 #include "audio/audio.h"
103 #include "sysemu/cpus.h"
104 #include "migration/colo.h"
105 #include "sysemu/kvm.h"
106 #include "sysemu/hax.h"
107 #include "qapi/qobject-input-visitor.h"
108 #include "qapi/qobject-input-visitor.h"
109 #include "qapi-visit.h"
110 #include "qapi/qmp/qjson.h"
111 #include "qemu/option.h"
112 #include "qemu/config-file.h"
113 #include "qemu-options.h"
114 #include "qmp-commands.h"
115 #include "qemu/main-loop.h"
116 #ifdef CONFIG_VIRTFS
117 #include "fsdev/qemu-fsdev.h"
118 #endif
119 #include "sysemu/qtest.h"
121 #include "disas/disas.h"
124 #include "slirp/libslirp.h"
126 #include "trace-root.h"
127 #include "trace/control.h"
128 #include "qemu/queue.h"
129 #include "sysemu/arch_init.h"
131 #include "ui/qemu-spice.h"
132 #include "qapi/string-input-visitor.h"
133 #include "qapi/opts-visitor.h"
134 #include "qom/object_interfaces.h"
135 #include "qapi-event.h"
136 #include "exec/semihost.h"
137 #include "crypto/init.h"
138 #include "sysemu/replay.h"
139 #include "qapi/qmp/qerror.h"
140 #include "sysemu/iothread.h"
142 #define MAX_VIRTIO_CONSOLES 1
143 #define MAX_SCLP_CONSOLES 1
145 static const char *data_dir[16];
146 static int data_dir_idx;
147 const char *bios_name = NULL;
148 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
149 int request_opengl = -1;
150 int display_opengl;
151 const char* keyboard_layout = NULL;
152 ram_addr_t ram_size;
153 const char *mem_path = NULL;
154 int mem_prealloc = 0; /* force preallocation of physical target memory */
155 bool enable_mlock = false;
156 int nb_nics;
157 NICInfo nd_table[MAX_NICS];
158 int autostart;
159 static int rtc_utc = 1;
160 static int rtc_date_offset = -1; /* -1 means no change */
161 QEMUClockType rtc_clock;
162 int vga_interface_type = VGA_NONE;
163 static int full_screen = 0;
164 static int no_frame = 0;
165 int no_quit = 0;
166 static bool grab_on_hover;
167 Chardev *serial_hds[MAX_SERIAL_PORTS];
168 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
169 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
170 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
171 int win2k_install_hack = 0;
172 int singlestep = 0;
173 int smp_cpus = 1;
174 int max_cpus = 1;
175 int smp_cores = 1;
176 int smp_threads = 1;
177 int acpi_enabled = 1;
178 int no_hpet = 0;
179 int fd_bootchk = 1;
180 static int no_reboot;
181 int no_shutdown = 0;
182 int cursor_hide = 1;
183 int graphic_rotate = 0;
184 const char *watchdog;
185 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
186 int nb_option_roms;
187 int old_param = 0;
188 const char *qemu_name;
189 int alt_grab = 0;
190 int ctrl_grab = 0;
191 unsigned int nb_prom_envs = 0;
192 const char *prom_envs[MAX_PROM_ENVS];
193 int boot_menu;
194 bool boot_strict;
195 uint8_t *boot_splash_filedata;
196 size_t boot_splash_filedata_size;
197 uint8_t qemu_extra_params_fw[2];
199 int icount_align_option;
201 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
202 * little-endian "wire format" described in the SMBIOS 2.6 specification.
204 QemuUUID qemu_uuid;
205 bool qemu_uuid_set;
207 /* Trace unassigned memory or i/o accesses. */
208 bool trace_unassigned;
210 static NotifierList exit_notifiers =
211 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
213 static NotifierList machine_init_done_notifiers =
214 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
216 bool xen_allowed;
217 uint32_t xen_domid;
218 enum xen_mode xen_mode = XEN_EMULATE;
219 bool xen_domid_restrict;
221 static int has_defaults = 1;
222 static int default_serial = 1;
223 static int default_parallel = 1;
224 static int default_virtcon = 1;
225 static int default_sclp = 1;
226 static int default_monitor = 1;
227 static int default_floppy = 1;
228 static int default_cdrom = 1;
229 static int default_sdcard = 1;
230 static int default_vga = 1;
231 static int default_net = 1;
233 static struct {
234 const char *driver;
235 int *flag;
236 } default_list[] = {
237 { .driver = "isa-serial", .flag = &default_serial },
238 { .driver = "isa-parallel", .flag = &default_parallel },
239 { .driver = "isa-fdc", .flag = &default_floppy },
240 { .driver = "floppy", .flag = &default_floppy },
241 { .driver = "ide-cd", .flag = &default_cdrom },
242 { .driver = "ide-hd", .flag = &default_cdrom },
243 { .driver = "ide-drive", .flag = &default_cdrom },
244 { .driver = "scsi-cd", .flag = &default_cdrom },
245 { .driver = "scsi-hd", .flag = &default_cdrom },
246 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
247 { .driver = "virtio-serial", .flag = &default_virtcon },
248 { .driver = "VGA", .flag = &default_vga },
249 { .driver = "isa-vga", .flag = &default_vga },
250 { .driver = "cirrus-vga", .flag = &default_vga },
251 { .driver = "isa-cirrus-vga", .flag = &default_vga },
252 { .driver = "vmware-svga", .flag = &default_vga },
253 { .driver = "qxl-vga", .flag = &default_vga },
254 { .driver = "virtio-vga", .flag = &default_vga },
257 static QemuOptsList qemu_rtc_opts = {
258 .name = "rtc",
259 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
260 .desc = {
262 .name = "base",
263 .type = QEMU_OPT_STRING,
265 .name = "clock",
266 .type = QEMU_OPT_STRING,
268 .name = "driftfix",
269 .type = QEMU_OPT_STRING,
271 { /* end of list */ }
275 static QemuOptsList qemu_sandbox_opts = {
276 .name = "sandbox",
277 .implied_opt_name = "enable",
278 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
279 .desc = {
281 .name = "enable",
282 .type = QEMU_OPT_BOOL,
284 { /* end of list */ }
288 static QemuOptsList qemu_option_rom_opts = {
289 .name = "option-rom",
290 .implied_opt_name = "romfile",
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
292 .desc = {
294 .name = "bootindex",
295 .type = QEMU_OPT_NUMBER,
296 }, {
297 .name = "romfile",
298 .type = QEMU_OPT_STRING,
300 { /* end of list */ }
304 static QemuOptsList qemu_machine_opts = {
305 .name = "machine",
306 .implied_opt_name = "type",
307 .merge_lists = true,
308 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
309 .desc = {
311 * no elements => accept any
312 * sanity checking will happen later
313 * when setting machine properties
319 static QemuOptsList qemu_accel_opts = {
320 .name = "accel",
321 .implied_opt_name = "accel",
322 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
323 .merge_lists = true,
324 .desc = {
326 .name = "accel",
327 .type = QEMU_OPT_STRING,
328 .help = "Select the type of accelerator",
331 .name = "thread",
332 .type = QEMU_OPT_STRING,
333 .help = "Enable/disable multi-threaded TCG",
335 { /* end of list */ }
339 static QemuOptsList qemu_boot_opts = {
340 .name = "boot-opts",
341 .implied_opt_name = "order",
342 .merge_lists = true,
343 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
344 .desc = {
346 .name = "order",
347 .type = QEMU_OPT_STRING,
348 }, {
349 .name = "once",
350 .type = QEMU_OPT_STRING,
351 }, {
352 .name = "menu",
353 .type = QEMU_OPT_BOOL,
354 }, {
355 .name = "splash",
356 .type = QEMU_OPT_STRING,
357 }, {
358 .name = "splash-time",
359 .type = QEMU_OPT_STRING,
360 }, {
361 .name = "reboot-timeout",
362 .type = QEMU_OPT_STRING,
363 }, {
364 .name = "strict",
365 .type = QEMU_OPT_BOOL,
367 { /*End of list */ }
371 static QemuOptsList qemu_add_fd_opts = {
372 .name = "add-fd",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
374 .desc = {
376 .name = "fd",
377 .type = QEMU_OPT_NUMBER,
378 .help = "file descriptor of which a duplicate is added to fd set",
380 .name = "set",
381 .type = QEMU_OPT_NUMBER,
382 .help = "ID of the fd set to add fd to",
384 .name = "opaque",
385 .type = QEMU_OPT_STRING,
386 .help = "free-form string used to describe fd",
388 { /* end of list */ }
392 static QemuOptsList qemu_object_opts = {
393 .name = "object",
394 .implied_opt_name = "qom-type",
395 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
396 .desc = {
401 static QemuOptsList qemu_tpmdev_opts = {
402 .name = "tpmdev",
403 .implied_opt_name = "type",
404 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
405 .desc = {
406 /* options are defined in the TPM backends */
407 { /* end of list */ }
411 static QemuOptsList qemu_realtime_opts = {
412 .name = "realtime",
413 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
414 .desc = {
416 .name = "mlock",
417 .type = QEMU_OPT_BOOL,
419 { /* end of list */ }
423 static QemuOptsList qemu_msg_opts = {
424 .name = "msg",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
426 .desc = {
428 .name = "timestamp",
429 .type = QEMU_OPT_BOOL,
431 { /* end of list */ }
435 static QemuOptsList qemu_name_opts = {
436 .name = "name",
437 .implied_opt_name = "guest",
438 .merge_lists = true,
439 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
440 .desc = {
442 .name = "guest",
443 .type = QEMU_OPT_STRING,
444 .help = "Sets the name of the guest.\n"
445 "This name will be displayed in the SDL window caption.\n"
446 "The name will also be used for the VNC server",
447 }, {
448 .name = "process",
449 .type = QEMU_OPT_STRING,
450 .help = "Sets the name of the QEMU process, as shown in top etc",
451 }, {
452 .name = "debug-threads",
453 .type = QEMU_OPT_BOOL,
454 .help = "When enabled, name the individual threads; defaults off.\n"
455 "NOTE: The thread names are for debugging and not a\n"
456 "stable API.",
458 { /* End of list */ }
462 static QemuOptsList qemu_mem_opts = {
463 .name = "memory",
464 .implied_opt_name = "size",
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
466 .merge_lists = true,
467 .desc = {
469 .name = "size",
470 .type = QEMU_OPT_SIZE,
473 .name = "slots",
474 .type = QEMU_OPT_NUMBER,
477 .name = "maxmem",
478 .type = QEMU_OPT_SIZE,
480 { /* end of list */ }
484 static QemuOptsList qemu_icount_opts = {
485 .name = "icount",
486 .implied_opt_name = "shift",
487 .merge_lists = true,
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
489 .desc = {
491 .name = "shift",
492 .type = QEMU_OPT_STRING,
493 }, {
494 .name = "align",
495 .type = QEMU_OPT_BOOL,
496 }, {
497 .name = "sleep",
498 .type = QEMU_OPT_BOOL,
499 }, {
500 .name = "rr",
501 .type = QEMU_OPT_STRING,
502 }, {
503 .name = "rrfile",
504 .type = QEMU_OPT_STRING,
505 }, {
506 .name = "rrsnapshot",
507 .type = QEMU_OPT_STRING,
509 { /* end of list */ }
513 static QemuOptsList qemu_semihosting_config_opts = {
514 .name = "semihosting-config",
515 .implied_opt_name = "enable",
516 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
517 .desc = {
519 .name = "enable",
520 .type = QEMU_OPT_BOOL,
521 }, {
522 .name = "target",
523 .type = QEMU_OPT_STRING,
524 }, {
525 .name = "arg",
526 .type = QEMU_OPT_STRING,
528 { /* end of list */ }
532 static QemuOptsList qemu_fw_cfg_opts = {
533 .name = "fw_cfg",
534 .implied_opt_name = "name",
535 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
536 .desc = {
538 .name = "name",
539 .type = QEMU_OPT_STRING,
540 .help = "Sets the fw_cfg name of the blob to be inserted",
541 }, {
542 .name = "file",
543 .type = QEMU_OPT_STRING,
544 .help = "Sets the name of the file from which\n"
545 "the fw_cfg blob will be loaded",
546 }, {
547 .name = "string",
548 .type = QEMU_OPT_STRING,
549 .help = "Sets content of the blob to be inserted from a string",
551 { /* end of list */ }
556 * Get machine options
558 * Returns: machine options (never null).
560 QemuOpts *qemu_get_machine_opts(void)
562 return qemu_find_opts_singleton("machine");
565 const char *qemu_get_vm_name(void)
567 return qemu_name;
570 static void res_free(void)
572 g_free(boot_splash_filedata);
573 boot_splash_filedata = NULL;
576 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
578 const char *driver = qemu_opt_get(opts, "driver");
579 int i;
581 if (!driver)
582 return 0;
583 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
584 if (strcmp(default_list[i].driver, driver) != 0)
585 continue;
586 *(default_list[i].flag) = 0;
588 return 0;
591 /***********************************************************/
592 /* QEMU state */
594 static RunState current_run_state = RUN_STATE_PRELAUNCH;
596 /* We use RUN_STATE__MAX but any invalid value will do */
597 static RunState vmstop_requested = RUN_STATE__MAX;
598 static QemuMutex vmstop_lock;
600 typedef struct {
601 RunState from;
602 RunState to;
603 } RunStateTransition;
605 static const RunStateTransition runstate_transitions_def[] = {
606 /* from -> to */
607 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
608 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
611 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
612 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
613 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
614 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
615 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
616 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
617 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
618 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
621 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
622 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
624 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
625 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
628 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
629 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
632 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
633 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
634 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
635 { RUN_STATE_PAUSED, RUN_STATE_COLO},
637 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
638 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
642 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
645 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
646 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
647 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
650 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
651 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
653 { RUN_STATE_COLO, RUN_STATE_RUNNING },
655 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
656 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
657 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
658 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
659 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
660 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
661 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
662 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
663 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
664 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
665 { RUN_STATE_RUNNING, RUN_STATE_COLO},
667 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
669 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
670 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
671 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
673 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
674 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
675 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
676 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
677 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
678 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
680 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
681 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
682 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
683 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
685 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
686 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
687 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
689 { RUN_STATE__MAX, RUN_STATE__MAX },
692 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
694 bool runstate_check(RunState state)
696 return current_run_state == state;
699 bool runstate_store(char *str, size_t size)
701 const char *state = RunState_lookup[current_run_state];
702 size_t len = strlen(state) + 1;
704 if (len > size) {
705 return false;
707 memcpy(str, state, len);
708 return true;
711 static void runstate_init(void)
713 const RunStateTransition *p;
715 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
716 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
717 runstate_valid_transitions[p->from][p->to] = true;
720 qemu_mutex_init(&vmstop_lock);
723 /* This function will abort() on invalid state transitions */
724 void runstate_set(RunState new_state)
726 assert(new_state < RUN_STATE__MAX);
728 if (current_run_state == new_state) {
729 return;
732 if (!runstate_valid_transitions[current_run_state][new_state]) {
733 error_report("invalid runstate transition: '%s' -> '%s'",
734 RunState_lookup[current_run_state],
735 RunState_lookup[new_state]);
736 abort();
738 trace_runstate_set(new_state);
739 current_run_state = new_state;
742 int runstate_is_running(void)
744 return runstate_check(RUN_STATE_RUNNING);
747 bool runstate_needs_reset(void)
749 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
750 runstate_check(RUN_STATE_SHUTDOWN);
753 StatusInfo *qmp_query_status(Error **errp)
755 StatusInfo *info = g_malloc0(sizeof(*info));
757 info->running = runstate_is_running();
758 info->singlestep = singlestep;
759 info->status = current_run_state;
761 return info;
764 bool qemu_vmstop_requested(RunState *r)
766 qemu_mutex_lock(&vmstop_lock);
767 *r = vmstop_requested;
768 vmstop_requested = RUN_STATE__MAX;
769 qemu_mutex_unlock(&vmstop_lock);
770 return *r < RUN_STATE__MAX;
773 void qemu_system_vmstop_request_prepare(void)
775 qemu_mutex_lock(&vmstop_lock);
778 void qemu_system_vmstop_request(RunState state)
780 vmstop_requested = state;
781 qemu_mutex_unlock(&vmstop_lock);
782 qemu_notify_event();
785 /***********************************************************/
786 /* real time host monotonic timer */
788 static time_t qemu_time(void)
790 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
793 /***********************************************************/
794 /* host time/date access */
795 void qemu_get_timedate(struct tm *tm, int offset)
797 time_t ti = qemu_time();
799 ti += offset;
800 if (rtc_date_offset == -1) {
801 if (rtc_utc)
802 gmtime_r(&ti, tm);
803 else
804 localtime_r(&ti, tm);
805 } else {
806 ti -= rtc_date_offset;
807 gmtime_r(&ti, tm);
811 int qemu_timedate_diff(struct tm *tm)
813 time_t seconds;
815 if (rtc_date_offset == -1)
816 if (rtc_utc)
817 seconds = mktimegm(tm);
818 else {
819 struct tm tmp = *tm;
820 tmp.tm_isdst = -1; /* use timezone to figure it out */
821 seconds = mktime(&tmp);
823 else
824 seconds = mktimegm(tm) + rtc_date_offset;
826 return seconds - qemu_time();
829 static void configure_rtc_date_offset(const char *startdate, int legacy)
831 time_t rtc_start_date;
832 struct tm tm;
834 if (!strcmp(startdate, "now") && legacy) {
835 rtc_date_offset = -1;
836 } else {
837 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
838 &tm.tm_year,
839 &tm.tm_mon,
840 &tm.tm_mday,
841 &tm.tm_hour,
842 &tm.tm_min,
843 &tm.tm_sec) == 6) {
844 /* OK */
845 } else if (sscanf(startdate, "%d-%d-%d",
846 &tm.tm_year,
847 &tm.tm_mon,
848 &tm.tm_mday) == 3) {
849 tm.tm_hour = 0;
850 tm.tm_min = 0;
851 tm.tm_sec = 0;
852 } else {
853 goto date_fail;
855 tm.tm_year -= 1900;
856 tm.tm_mon--;
857 rtc_start_date = mktimegm(&tm);
858 if (rtc_start_date == -1) {
859 date_fail:
860 error_report("invalid date format");
861 error_printf("valid formats: "
862 "'2006-06-17T16:01:21' or '2006-06-17'\n");
863 exit(1);
865 rtc_date_offset = qemu_time() - rtc_start_date;
869 static void configure_rtc(QemuOpts *opts)
871 const char *value;
873 value = qemu_opt_get(opts, "base");
874 if (value) {
875 if (!strcmp(value, "utc")) {
876 rtc_utc = 1;
877 } else if (!strcmp(value, "localtime")) {
878 Error *blocker = NULL;
879 rtc_utc = 0;
880 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
881 "-rtc base=localtime");
882 replay_add_blocker(blocker);
883 } else {
884 configure_rtc_date_offset(value, 0);
887 value = qemu_opt_get(opts, "clock");
888 if (value) {
889 if (!strcmp(value, "host")) {
890 rtc_clock = QEMU_CLOCK_HOST;
891 } else if (!strcmp(value, "rt")) {
892 rtc_clock = QEMU_CLOCK_REALTIME;
893 } else if (!strcmp(value, "vm")) {
894 rtc_clock = QEMU_CLOCK_VIRTUAL;
895 } else {
896 error_report("invalid option value '%s'", value);
897 exit(1);
900 value = qemu_opt_get(opts, "driftfix");
901 if (value) {
902 if (!strcmp(value, "slew")) {
903 static GlobalProperty slew_lost_ticks = {
904 .driver = "mc146818rtc",
905 .property = "lost_tick_policy",
906 .value = "slew",
909 qdev_prop_register_global(&slew_lost_ticks);
910 } else if (!strcmp(value, "none")) {
911 /* discard is default */
912 } else {
913 error_report("invalid option value '%s'", value);
914 exit(1);
919 /***********************************************************/
920 /* Bluetooth support */
921 static int nb_hcis;
922 static int cur_hci;
923 static struct HCIInfo *hci_table[MAX_NICS];
925 struct HCIInfo *qemu_next_hci(void)
927 if (cur_hci == nb_hcis)
928 return &null_hci;
930 return hci_table[cur_hci++];
933 static int bt_hci_parse(const char *str)
935 struct HCIInfo *hci;
936 bdaddr_t bdaddr;
938 if (nb_hcis >= MAX_NICS) {
939 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
940 return -1;
943 hci = hci_init(str);
944 if (!hci)
945 return -1;
947 bdaddr.b[0] = 0x52;
948 bdaddr.b[1] = 0x54;
949 bdaddr.b[2] = 0x00;
950 bdaddr.b[3] = 0x12;
951 bdaddr.b[4] = 0x34;
952 bdaddr.b[5] = 0x56 + nb_hcis;
953 hci->bdaddr_set(hci, bdaddr.b);
955 hci_table[nb_hcis++] = hci;
957 return 0;
960 static void bt_vhci_add(int vlan_id)
962 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
964 if (!vlan->slave)
965 warn_report("adding a VHCI to an empty scatternet %i",
966 vlan_id);
968 bt_vhci_init(bt_new_hci(vlan));
971 static struct bt_device_s *bt_device_add(const char *opt)
973 struct bt_scatternet_s *vlan;
974 int vlan_id = 0;
975 char *endp = strstr(opt, ",vlan=");
976 int len = (endp ? endp - opt : strlen(opt)) + 1;
977 char devname[10];
979 pstrcpy(devname, MIN(sizeof(devname), len), opt);
981 if (endp) {
982 vlan_id = strtol(endp + 6, &endp, 0);
983 if (*endp) {
984 error_report("unrecognised bluetooth vlan Id");
985 return 0;
989 vlan = qemu_find_bt_vlan(vlan_id);
991 if (!vlan->slave)
992 warn_report("adding a slave device to an empty scatternet %i",
993 vlan_id);
995 if (!strcmp(devname, "keyboard"))
996 return bt_keyboard_init(vlan);
998 error_report("unsupported bluetooth device '%s'", devname);
999 return 0;
1002 static int bt_parse(const char *opt)
1004 const char *endp, *p;
1005 int vlan;
1007 if (strstart(opt, "hci", &endp)) {
1008 if (!*endp || *endp == ',') {
1009 if (*endp)
1010 if (!strstart(endp, ",vlan=", 0))
1011 opt = endp + 1;
1013 return bt_hci_parse(opt);
1015 } else if (strstart(opt, "vhci", &endp)) {
1016 if (!*endp || *endp == ',') {
1017 if (*endp) {
1018 if (strstart(endp, ",vlan=", &p)) {
1019 vlan = strtol(p, (char **) &endp, 0);
1020 if (*endp) {
1021 error_report("bad scatternet '%s'", p);
1022 return 1;
1024 } else {
1025 error_report("bad parameter '%s'", endp + 1);
1026 return 1;
1028 } else
1029 vlan = 0;
1031 bt_vhci_add(vlan);
1032 return 0;
1034 } else if (strstart(opt, "device:", &endp))
1035 return !bt_device_add(endp);
1037 error_report("bad bluetooth parameter '%s'", opt);
1038 return 1;
1041 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1043 /* FIXME: change this to true for 1.3 */
1044 if (qemu_opt_get_bool(opts, "enable", false)) {
1045 #ifdef CONFIG_SECCOMP
1046 if (seccomp_start() < 0) {
1047 error_report("failed to install seccomp syscall filter "
1048 "in the kernel");
1049 return -1;
1051 #else
1052 error_report("seccomp support is disabled");
1053 return -1;
1054 #endif
1057 return 0;
1060 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1062 const char *proc_name;
1064 if (qemu_opt_get(opts, "debug-threads")) {
1065 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1067 qemu_name = qemu_opt_get(opts, "guest");
1069 proc_name = qemu_opt_get(opts, "process");
1070 if (proc_name) {
1071 os_set_proc_name(proc_name);
1074 return 0;
1077 bool defaults_enabled(void)
1079 return has_defaults;
1082 #ifndef _WIN32
1083 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1085 int fd, dupfd, flags;
1086 int64_t fdset_id;
1087 const char *fd_opaque = NULL;
1088 AddfdInfo *fdinfo;
1090 fd = qemu_opt_get_number(opts, "fd", -1);
1091 fdset_id = qemu_opt_get_number(opts, "set", -1);
1092 fd_opaque = qemu_opt_get(opts, "opaque");
1094 if (fd < 0) {
1095 error_report("fd option is required and must be non-negative");
1096 return -1;
1099 if (fd <= STDERR_FILENO) {
1100 error_report("fd cannot be a standard I/O stream");
1101 return -1;
1105 * All fds inherited across exec() necessarily have FD_CLOEXEC
1106 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1108 flags = fcntl(fd, F_GETFD);
1109 if (flags == -1 || (flags & FD_CLOEXEC)) {
1110 error_report("fd is not valid or already in use");
1111 return -1;
1114 if (fdset_id < 0) {
1115 error_report("set option is required and must be non-negative");
1116 return -1;
1119 #ifdef F_DUPFD_CLOEXEC
1120 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1121 #else
1122 dupfd = dup(fd);
1123 if (dupfd != -1) {
1124 qemu_set_cloexec(dupfd);
1126 #endif
1127 if (dupfd == -1) {
1128 error_report("error duplicating fd: %s", strerror(errno));
1129 return -1;
1132 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1133 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1134 &error_abort);
1135 g_free(fdinfo);
1137 return 0;
1140 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1142 int fd;
1144 fd = qemu_opt_get_number(opts, "fd", -1);
1145 close(fd);
1147 return 0;
1149 #endif
1151 /***********************************************************/
1152 /* QEMU Block devices */
1154 #define HD_OPTS "media=disk"
1155 #define CDROM_OPTS "media=cdrom"
1156 #define FD_OPTS ""
1157 #define PFLASH_OPTS ""
1158 #define MTD_OPTS ""
1159 #define SD_OPTS ""
1161 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1163 BlockInterfaceType *block_default_type = opaque;
1165 return drive_new(opts, *block_default_type) == NULL;
1168 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1170 if (qemu_opt_get(opts, "snapshot") == NULL) {
1171 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1173 return 0;
1176 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1177 int index, const char *optstr)
1179 QemuOpts *opts;
1180 DriveInfo *dinfo;
1182 if (!enable || drive_get_by_index(type, index)) {
1183 return;
1186 opts = drive_add(type, index, NULL, optstr);
1187 if (snapshot) {
1188 drive_enable_snapshot(NULL, opts, NULL);
1191 dinfo = drive_new(opts, type);
1192 if (!dinfo) {
1193 exit(1);
1195 dinfo->is_default = true;
1199 static QemuOptsList qemu_smp_opts = {
1200 .name = "smp-opts",
1201 .implied_opt_name = "cpus",
1202 .merge_lists = true,
1203 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1204 .desc = {
1206 .name = "cpus",
1207 .type = QEMU_OPT_NUMBER,
1208 }, {
1209 .name = "sockets",
1210 .type = QEMU_OPT_NUMBER,
1211 }, {
1212 .name = "cores",
1213 .type = QEMU_OPT_NUMBER,
1214 }, {
1215 .name = "threads",
1216 .type = QEMU_OPT_NUMBER,
1217 }, {
1218 .name = "maxcpus",
1219 .type = QEMU_OPT_NUMBER,
1221 { /*End of list */ }
1225 static void smp_parse(QemuOpts *opts)
1227 if (opts) {
1228 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1229 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1230 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1231 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1233 /* compute missing values, prefer sockets over cores over threads */
1234 if (cpus == 0 || sockets == 0) {
1235 sockets = sockets > 0 ? sockets : 1;
1236 cores = cores > 0 ? cores : 1;
1237 threads = threads > 0 ? threads : 1;
1238 if (cpus == 0) {
1239 cpus = cores * threads * sockets;
1241 } else if (cores == 0) {
1242 threads = threads > 0 ? threads : 1;
1243 cores = cpus / (sockets * threads);
1244 cores = cores > 0 ? cores : 1;
1245 } else if (threads == 0) {
1246 threads = cpus / (cores * sockets);
1247 threads = threads > 0 ? threads : 1;
1248 } else if (sockets * cores * threads < cpus) {
1249 error_report("cpu topology: "
1250 "sockets (%u) * cores (%u) * threads (%u) < "
1251 "smp_cpus (%u)",
1252 sockets, cores, threads, cpus);
1253 exit(1);
1256 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1258 if (max_cpus < cpus) {
1259 error_report("maxcpus must be equal to or greater than smp");
1260 exit(1);
1263 if (sockets * cores * threads > max_cpus) {
1264 error_report("cpu topology: "
1265 "sockets (%u) * cores (%u) * threads (%u) > "
1266 "maxcpus (%u)",
1267 sockets, cores, threads, max_cpus);
1268 exit(1);
1271 smp_cpus = cpus;
1272 smp_cores = cores;
1273 smp_threads = threads;
1276 if (smp_cpus > 1) {
1277 Error *blocker = NULL;
1278 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1279 replay_add_blocker(blocker);
1283 static void realtime_init(void)
1285 if (enable_mlock) {
1286 if (os_mlock() < 0) {
1287 error_report("locking memory failed");
1288 exit(1);
1294 static void configure_msg(QemuOpts *opts)
1296 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1299 /***********************************************************/
1300 /* Semihosting */
1302 typedef struct SemihostingConfig {
1303 bool enabled;
1304 SemihostingTarget target;
1305 const char **argv;
1306 int argc;
1307 const char *cmdline; /* concatenated argv */
1308 } SemihostingConfig;
1310 static SemihostingConfig semihosting;
1312 bool semihosting_enabled(void)
1314 return semihosting.enabled;
1317 SemihostingTarget semihosting_get_target(void)
1319 return semihosting.target;
1322 const char *semihosting_get_arg(int i)
1324 if (i >= semihosting.argc) {
1325 return NULL;
1327 return semihosting.argv[i];
1330 int semihosting_get_argc(void)
1332 return semihosting.argc;
1335 const char *semihosting_get_cmdline(void)
1337 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1338 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1340 return semihosting.cmdline;
1343 static int add_semihosting_arg(void *opaque,
1344 const char *name, const char *val,
1345 Error **errp)
1347 SemihostingConfig *s = opaque;
1348 if (strcmp(name, "arg") == 0) {
1349 s->argc++;
1350 /* one extra element as g_strjoinv() expects NULL-terminated array */
1351 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1352 s->argv[s->argc - 1] = val;
1353 s->argv[s->argc] = NULL;
1355 return 0;
1358 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1359 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1361 char *cmd_token;
1363 /* argv[0] */
1364 add_semihosting_arg(&semihosting, "arg", file, NULL);
1366 /* split -append and initialize argv[1..n] */
1367 cmd_token = strtok(g_strdup(cmd), " ");
1368 while (cmd_token) {
1369 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1370 cmd_token = strtok(NULL, " ");
1374 /* Now we still need this for compatibility with XEN. */
1375 bool has_igd_gfx_passthru;
1376 static void igd_gfx_passthru(void)
1378 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1381 /***********************************************************/
1382 /* USB devices */
1384 static int usb_device_add(const char *devname)
1386 USBDevice *dev = NULL;
1387 #ifndef CONFIG_LINUX
1388 const char *p;
1389 #endif
1391 if (!machine_usb(current_machine)) {
1392 return -1;
1395 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1396 dev = usbdevice_create(devname);
1397 if (dev)
1398 goto done;
1400 /* the other ones */
1401 #ifndef CONFIG_LINUX
1402 /* only the linux version is qdev-ified, usb-bsd still needs this */
1403 if (strstart(devname, "host:", &p)) {
1404 dev = usb_host_device_open(usb_bus_find(-1), p);
1406 #endif
1407 if (!dev)
1408 return -1;
1410 done:
1411 return 0;
1414 static int usb_device_del(const char *devname)
1416 int bus_num, addr;
1417 const char *p;
1419 if (strstart(devname, "host:", &p)) {
1420 return -1;
1423 if (!machine_usb(current_machine)) {
1424 return -1;
1427 p = strchr(devname, '.');
1428 if (!p)
1429 return -1;
1430 bus_num = strtoul(devname, NULL, 0);
1431 addr = strtoul(p + 1, NULL, 0);
1433 return usb_device_delete_addr(bus_num, addr);
1436 static int usb_parse(const char *cmdline)
1438 int r;
1439 r = usb_device_add(cmdline);
1440 if (r < 0) {
1441 error_report("could not add USB device '%s'", cmdline);
1443 return r;
1446 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1448 const char *devname = qdict_get_str(qdict, "devname");
1450 error_report("usb_add is deprecated, please use device_add instead");
1452 if (usb_device_add(devname) < 0) {
1453 error_report("could not add USB device '%s'", devname);
1457 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1459 const char *devname = qdict_get_str(qdict, "devname");
1461 error_report("usb_del is deprecated, please use device_del instead");
1463 if (usb_device_del(devname) < 0) {
1464 error_report("could not delete USB device '%s'", devname);
1468 /***********************************************************/
1469 /* machine registration */
1471 MachineState *current_machine;
1473 static MachineClass *find_machine(const char *name)
1475 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1476 MachineClass *mc = NULL;
1478 for (el = machines; el; el = el->next) {
1479 MachineClass *temp = el->data;
1481 if (!strcmp(temp->name, name)) {
1482 mc = temp;
1483 break;
1485 if (temp->alias &&
1486 !strcmp(temp->alias, name)) {
1487 mc = temp;
1488 break;
1492 g_slist_free(machines);
1493 return mc;
1496 MachineClass *find_default_machine(void)
1498 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1499 MachineClass *mc = NULL;
1501 for (el = machines; el; el = el->next) {
1502 MachineClass *temp = el->data;
1504 if (temp->is_default) {
1505 mc = temp;
1506 break;
1510 g_slist_free(machines);
1511 return mc;
1514 MachineInfoList *qmp_query_machines(Error **errp)
1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1517 MachineInfoList *mach_list = NULL;
1519 for (el = machines; el; el = el->next) {
1520 MachineClass *mc = el->data;
1521 MachineInfoList *entry;
1522 MachineInfo *info;
1524 info = g_malloc0(sizeof(*info));
1525 if (mc->is_default) {
1526 info->has_is_default = true;
1527 info->is_default = true;
1530 if (mc->alias) {
1531 info->has_alias = true;
1532 info->alias = g_strdup(mc->alias);
1535 info->name = g_strdup(mc->name);
1536 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1537 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1539 entry = g_malloc0(sizeof(*entry));
1540 entry->value = info;
1541 entry->next = mach_list;
1542 mach_list = entry;
1545 g_slist_free(machines);
1546 return mach_list;
1549 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1551 ObjectProperty *prop;
1552 ObjectPropertyIterator iter;
1554 if (!qemu_opt_has_help_opt(opts)) {
1555 return 0;
1558 object_property_iter_init(&iter, OBJECT(machine));
1559 while ((prop = object_property_iter_next(&iter))) {
1560 if (!prop->set) {
1561 continue;
1564 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1565 prop->name, prop->type);
1566 if (prop->description) {
1567 error_printf(" (%s)\n", prop->description);
1568 } else {
1569 error_printf("\n");
1573 return 1;
1576 /***********************************************************/
1577 /* main execution loop */
1579 struct vm_change_state_entry {
1580 VMChangeStateHandler *cb;
1581 void *opaque;
1582 QLIST_ENTRY (vm_change_state_entry) entries;
1585 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1587 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1588 void *opaque)
1590 VMChangeStateEntry *e;
1592 e = g_malloc0(sizeof (*e));
1594 e->cb = cb;
1595 e->opaque = opaque;
1596 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1597 return e;
1600 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1602 QLIST_REMOVE (e, entries);
1603 g_free (e);
1606 void vm_state_notify(int running, RunState state)
1608 VMChangeStateEntry *e, *next;
1610 trace_vm_state_notify(running, state);
1612 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1613 e->cb(e->opaque, running, state);
1617 static ShutdownCause reset_requested;
1618 static ShutdownCause shutdown_requested;
1619 static int shutdown_signal;
1620 static pid_t shutdown_pid;
1621 static int powerdown_requested;
1622 static int debug_requested;
1623 static int suspend_requested;
1624 static WakeupReason wakeup_reason;
1625 static NotifierList powerdown_notifiers =
1626 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1627 static NotifierList suspend_notifiers =
1628 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1629 static NotifierList wakeup_notifiers =
1630 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1631 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1633 ShutdownCause qemu_shutdown_requested_get(void)
1635 return shutdown_requested;
1638 ShutdownCause qemu_reset_requested_get(void)
1640 return reset_requested;
1643 static int qemu_shutdown_requested(void)
1645 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1648 static void qemu_kill_report(void)
1650 if (!qtest_driver() && shutdown_signal) {
1651 if (shutdown_pid == 0) {
1652 /* This happens for eg ^C at the terminal, so it's worth
1653 * avoiding printing an odd message in that case.
1655 error_report("terminating on signal %d", shutdown_signal);
1656 } else {
1657 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1659 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1660 shutdown_signal, shutdown_pid,
1661 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1662 g_free(shutdown_cmd);
1664 shutdown_signal = 0;
1668 static ShutdownCause qemu_reset_requested(void)
1670 ShutdownCause r = reset_requested;
1672 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1673 reset_requested = SHUTDOWN_CAUSE_NONE;
1674 return r;
1676 return SHUTDOWN_CAUSE_NONE;
1679 static int qemu_suspend_requested(void)
1681 int r = suspend_requested;
1682 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1683 suspend_requested = 0;
1684 return r;
1686 return false;
1689 static WakeupReason qemu_wakeup_requested(void)
1691 return wakeup_reason;
1694 static int qemu_powerdown_requested(void)
1696 int r = powerdown_requested;
1697 powerdown_requested = 0;
1698 return r;
1701 static int qemu_debug_requested(void)
1703 int r = debug_requested;
1704 debug_requested = 0;
1705 return r;
1709 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1711 void qemu_system_reset(ShutdownCause reason)
1713 MachineClass *mc;
1715 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1717 cpu_synchronize_all_states();
1719 if (mc && mc->reset) {
1720 mc->reset();
1721 } else {
1722 qemu_devices_reset();
1724 if (reason) {
1725 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1726 &error_abort);
1728 cpu_synchronize_all_post_reset();
1731 void qemu_system_guest_panicked(GuestPanicInformation *info)
1733 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1735 if (current_cpu) {
1736 current_cpu->crash_occurred = true;
1738 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1739 !!info, info, &error_abort);
1740 vm_stop(RUN_STATE_GUEST_PANICKED);
1741 if (!no_shutdown) {
1742 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1743 !!info, info, &error_abort);
1744 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1747 if (info) {
1748 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1749 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1750 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1751 info->u.hyper_v.arg1,
1752 info->u.hyper_v.arg2,
1753 info->u.hyper_v.arg3,
1754 info->u.hyper_v.arg4,
1755 info->u.hyper_v.arg5);
1757 qapi_free_GuestPanicInformation(info);
1761 void qemu_system_reset_request(ShutdownCause reason)
1763 if (no_reboot) {
1764 shutdown_requested = reason;
1765 } else {
1766 reset_requested = reason;
1768 cpu_stop_current();
1769 qemu_notify_event();
1772 static void qemu_system_suspend(void)
1774 pause_all_vcpus();
1775 notifier_list_notify(&suspend_notifiers, NULL);
1776 runstate_set(RUN_STATE_SUSPENDED);
1777 qapi_event_send_suspend(&error_abort);
1780 void qemu_system_suspend_request(void)
1782 if (runstate_check(RUN_STATE_SUSPENDED)) {
1783 return;
1785 suspend_requested = 1;
1786 cpu_stop_current();
1787 qemu_notify_event();
1790 void qemu_register_suspend_notifier(Notifier *notifier)
1792 notifier_list_add(&suspend_notifiers, notifier);
1795 void qemu_system_wakeup_request(WakeupReason reason)
1797 trace_system_wakeup_request(reason);
1799 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1800 return;
1802 if (!(wakeup_reason_mask & (1 << reason))) {
1803 return;
1805 runstate_set(RUN_STATE_RUNNING);
1806 wakeup_reason = reason;
1807 qemu_notify_event();
1810 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1812 if (enabled) {
1813 wakeup_reason_mask |= (1 << reason);
1814 } else {
1815 wakeup_reason_mask &= ~(1 << reason);
1819 void qemu_register_wakeup_notifier(Notifier *notifier)
1821 notifier_list_add(&wakeup_notifiers, notifier);
1824 void qemu_system_killed(int signal, pid_t pid)
1826 shutdown_signal = signal;
1827 shutdown_pid = pid;
1828 no_shutdown = 0;
1830 /* Cannot call qemu_system_shutdown_request directly because
1831 * we are in a signal handler.
1833 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1834 qemu_notify_event();
1837 void qemu_system_shutdown_request(ShutdownCause reason)
1839 trace_qemu_system_shutdown_request(reason);
1840 replay_shutdown_request(reason);
1841 shutdown_requested = reason;
1842 qemu_notify_event();
1845 static void qemu_system_powerdown(void)
1847 qapi_event_send_powerdown(&error_abort);
1848 notifier_list_notify(&powerdown_notifiers, NULL);
1851 void qemu_system_powerdown_request(void)
1853 trace_qemu_system_powerdown_request();
1854 powerdown_requested = 1;
1855 qemu_notify_event();
1858 void qemu_register_powerdown_notifier(Notifier *notifier)
1860 notifier_list_add(&powerdown_notifiers, notifier);
1863 void qemu_system_debug_request(void)
1865 debug_requested = 1;
1866 qemu_notify_event();
1869 static bool main_loop_should_exit(void)
1871 RunState r;
1872 ShutdownCause request;
1874 if (qemu_debug_requested()) {
1875 vm_stop(RUN_STATE_DEBUG);
1877 if (qemu_suspend_requested()) {
1878 qemu_system_suspend();
1880 request = qemu_shutdown_requested();
1881 if (request) {
1882 qemu_kill_report();
1883 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1884 &error_abort);
1885 if (no_shutdown) {
1886 vm_stop(RUN_STATE_SHUTDOWN);
1887 } else {
1888 return true;
1891 request = qemu_reset_requested();
1892 if (request) {
1893 pause_all_vcpus();
1894 qemu_system_reset(request);
1895 resume_all_vcpus();
1896 if (!runstate_check(RUN_STATE_RUNNING) &&
1897 !runstate_check(RUN_STATE_INMIGRATE)) {
1898 runstate_set(RUN_STATE_PRELAUNCH);
1901 if (qemu_wakeup_requested()) {
1902 pause_all_vcpus();
1903 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1904 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1905 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1906 resume_all_vcpus();
1907 qapi_event_send_wakeup(&error_abort);
1909 if (qemu_powerdown_requested()) {
1910 qemu_system_powerdown();
1912 if (qemu_vmstop_requested(&r)) {
1913 vm_stop(r);
1915 return false;
1918 static void main_loop(void)
1920 #ifdef CONFIG_PROFILER
1921 int64_t ti;
1922 #endif
1923 do {
1924 #ifdef CONFIG_PROFILER
1925 ti = profile_getclock();
1926 #endif
1927 main_loop_wait(false);
1928 #ifdef CONFIG_PROFILER
1929 dev_time += profile_getclock() - ti;
1930 #endif
1931 } while (!main_loop_should_exit());
1934 static void version(void)
1936 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1937 QEMU_COPYRIGHT "\n");
1940 static void QEMU_NORETURN help(int exitcode)
1942 version();
1943 printf("usage: %s [options] [disk_image]\n\n"
1944 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1945 error_get_progname());
1947 #define QEMU_OPTIONS_GENERATE_HELP
1948 #include "qemu-options-wrapper.h"
1950 printf("\nDuring emulation, the following keys are useful:\n"
1951 "ctrl-alt-f toggle full screen\n"
1952 "ctrl-alt-n switch to virtual console 'n'\n"
1953 "ctrl-alt toggle mouse and keyboard grab\n"
1954 "\n"
1955 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1957 exit(exitcode);
1960 #define HAS_ARG 0x0001
1962 typedef struct QEMUOption {
1963 const char *name;
1964 int flags;
1965 int index;
1966 uint32_t arch_mask;
1967 } QEMUOption;
1969 static const QEMUOption qemu_options[] = {
1970 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1971 #define QEMU_OPTIONS_GENERATE_OPTIONS
1972 #include "qemu-options-wrapper.h"
1973 { NULL },
1976 typedef struct VGAInterfaceInfo {
1977 const char *opt_name; /* option name */
1978 const char *name; /* human-readable name */
1979 /* Class names indicating that support is available.
1980 * If no class is specified, the interface is always available */
1981 const char *class_names[2];
1982 } VGAInterfaceInfo;
1984 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1985 [VGA_NONE] = {
1986 .opt_name = "none",
1988 [VGA_STD] = {
1989 .opt_name = "std",
1990 .name = "standard VGA",
1991 .class_names = { "VGA", "isa-vga" },
1993 [VGA_CIRRUS] = {
1994 .opt_name = "cirrus",
1995 .name = "Cirrus VGA",
1996 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1998 [VGA_VMWARE] = {
1999 .opt_name = "vmware",
2000 .name = "VMWare SVGA",
2001 .class_names = { "vmware-svga" },
2003 [VGA_VIRTIO] = {
2004 .opt_name = "virtio",
2005 .name = "Virtio VGA",
2006 .class_names = { "virtio-vga" },
2008 [VGA_QXL] = {
2009 .opt_name = "qxl",
2010 .name = "QXL VGA",
2011 .class_names = { "qxl-vga" },
2013 [VGA_TCX] = {
2014 .opt_name = "tcx",
2015 .name = "TCX framebuffer",
2016 .class_names = { "SUNW,tcx" },
2018 [VGA_CG3] = {
2019 .opt_name = "cg3",
2020 .name = "CG3 framebuffer",
2021 .class_names = { "cgthree" },
2023 [VGA_XENFB] = {
2024 .opt_name = "xenfb",
2028 static bool vga_interface_available(VGAInterfaceType t)
2030 VGAInterfaceInfo *ti = &vga_interfaces[t];
2032 assert(t < VGA_TYPE_MAX);
2033 return !ti->class_names[0] ||
2034 object_class_by_name(ti->class_names[0]) ||
2035 object_class_by_name(ti->class_names[1]);
2038 static void select_vgahw(const char *p)
2040 const char *opts;
2041 int t;
2043 assert(vga_interface_type == VGA_NONE);
2044 for (t = 0; t < VGA_TYPE_MAX; t++) {
2045 VGAInterfaceInfo *ti = &vga_interfaces[t];
2046 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2047 if (!vga_interface_available(t)) {
2048 error_report("%s not available", ti->name);
2049 exit(1);
2051 vga_interface_type = t;
2052 break;
2055 if (t == VGA_TYPE_MAX) {
2056 invalid_vga:
2057 error_report("unknown vga type: %s", p);
2058 exit(1);
2060 while (*opts) {
2061 const char *nextopt;
2063 if (strstart(opts, ",retrace=", &nextopt)) {
2064 opts = nextopt;
2065 if (strstart(opts, "dumb", &nextopt))
2066 vga_retrace_method = VGA_RETRACE_DUMB;
2067 else if (strstart(opts, "precise", &nextopt))
2068 vga_retrace_method = VGA_RETRACE_PRECISE;
2069 else goto invalid_vga;
2070 } else goto invalid_vga;
2071 opts = nextopt;
2075 typedef enum DisplayType {
2076 DT_DEFAULT,
2077 DT_CURSES,
2078 DT_SDL,
2079 DT_COCOA,
2080 DT_GTK,
2081 DT_EGL,
2082 DT_NONE,
2083 } DisplayType;
2085 static DisplayType select_display(const char *p)
2087 const char *opts;
2088 DisplayType display = DT_DEFAULT;
2090 if (strstart(p, "sdl", &opts)) {
2091 #ifdef CONFIG_SDL
2092 display = DT_SDL;
2093 while (*opts) {
2094 const char *nextopt;
2096 if (strstart(opts, ",frame=", &nextopt)) {
2097 opts = nextopt;
2098 if (strstart(opts, "on", &nextopt)) {
2099 no_frame = 0;
2100 } else if (strstart(opts, "off", &nextopt)) {
2101 no_frame = 1;
2102 } else {
2103 goto invalid_sdl_args;
2105 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2106 opts = nextopt;
2107 if (strstart(opts, "on", &nextopt)) {
2108 alt_grab = 1;
2109 } else if (strstart(opts, "off", &nextopt)) {
2110 alt_grab = 0;
2111 } else {
2112 goto invalid_sdl_args;
2114 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2115 opts = nextopt;
2116 if (strstart(opts, "on", &nextopt)) {
2117 ctrl_grab = 1;
2118 } else if (strstart(opts, "off", &nextopt)) {
2119 ctrl_grab = 0;
2120 } else {
2121 goto invalid_sdl_args;
2123 } else if (strstart(opts, ",window_close=", &nextopt)) {
2124 opts = nextopt;
2125 if (strstart(opts, "on", &nextopt)) {
2126 no_quit = 0;
2127 } else if (strstart(opts, "off", &nextopt)) {
2128 no_quit = 1;
2129 } else {
2130 goto invalid_sdl_args;
2132 } else if (strstart(opts, ",gl=", &nextopt)) {
2133 opts = nextopt;
2134 if (strstart(opts, "on", &nextopt)) {
2135 request_opengl = 1;
2136 } else if (strstart(opts, "off", &nextopt)) {
2137 request_opengl = 0;
2138 } else {
2139 goto invalid_sdl_args;
2141 } else {
2142 invalid_sdl_args:
2143 error_report("invalid SDL option string");
2144 exit(1);
2146 opts = nextopt;
2148 #else
2149 error_report("SDL support is disabled");
2150 exit(1);
2151 #endif
2152 } else if (strstart(p, "vnc", &opts)) {
2153 if (*opts == '=') {
2154 vnc_parse(opts + 1, &error_fatal);
2155 } else {
2156 error_report("VNC requires a display argument vnc=<display>");
2157 exit(1);
2159 } else if (strstart(p, "egl-headless", &opts)) {
2160 #ifdef CONFIG_OPENGL_DMABUF
2161 request_opengl = 1;
2162 display_opengl = 1;
2163 display = DT_EGL;
2164 #else
2165 fprintf(stderr, "egl support is disabled\n");
2166 exit(1);
2167 #endif
2168 } else if (strstart(p, "curses", &opts)) {
2169 #ifdef CONFIG_CURSES
2170 display = DT_CURSES;
2171 #else
2172 error_report("curses support is disabled");
2173 exit(1);
2174 #endif
2175 } else if (strstart(p, "gtk", &opts)) {
2176 #ifdef CONFIG_GTK
2177 display = DT_GTK;
2178 while (*opts) {
2179 const char *nextopt;
2181 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 grab_on_hover = true;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 grab_on_hover = false;
2187 } else {
2188 goto invalid_gtk_args;
2190 } else if (strstart(opts, ",gl=", &nextopt)) {
2191 opts = nextopt;
2192 if (strstart(opts, "on", &nextopt)) {
2193 request_opengl = 1;
2194 } else if (strstart(opts, "off", &nextopt)) {
2195 request_opengl = 0;
2196 } else {
2197 goto invalid_gtk_args;
2199 } else {
2200 invalid_gtk_args:
2201 error_report("invalid GTK option string");
2202 exit(1);
2204 opts = nextopt;
2206 #else
2207 error_report("GTK support is disabled");
2208 exit(1);
2209 #endif
2210 } else if (strstart(p, "none", &opts)) {
2211 display = DT_NONE;
2212 } else {
2213 error_report("unknown display type");
2214 exit(1);
2217 return display;
2220 static int balloon_parse(const char *arg)
2222 QemuOpts *opts;
2224 if (strcmp(arg, "none") == 0) {
2225 return 0;
2228 if (!strncmp(arg, "virtio", 6)) {
2229 if (arg[6] == ',') {
2230 /* have params -> parse them */
2231 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2232 false);
2233 if (!opts)
2234 return -1;
2235 } else {
2236 /* create empty opts */
2237 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2238 &error_abort);
2240 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2241 return 0;
2244 return -1;
2247 #if defined(CONFIG_RUBY)
2248 /* Ruby interface for QEMU. See README.EXT for programming example. */
2249 #warning mit ruby
2250 #include <ruby.h>
2252 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2254 int i;
2255 for (i = 0; i < argc; i++) {
2256 argv[i] = StringValue(argv[i]);
2257 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2259 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2260 //~ mode = 0666; /* default value */
2261 //~ }
2262 return INT2FIX(argc);
2265 void Init_qemu(void)
2267 printf("%s\n", __func__);
2268 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2269 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2270 #if 0
2271 rb_define_method();
2272 #endif
2274 #endif /* CONFIG_RUBY */
2276 #if defined(CONFIG_DLL)
2277 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2279 return FALSE;
2281 #endif /* CONFIG_DLL */
2283 char *qemu_find_file(int type, const char *name)
2285 int i;
2286 const char *subdir;
2287 char *buf;
2289 /* Try the name as a straight path first */
2290 if (access(name, R_OK) == 0) {
2291 trace_load_file(name, name);
2292 return g_strdup(name);
2295 switch (type) {
2296 case QEMU_FILE_TYPE_BIOS:
2297 subdir = "";
2298 break;
2299 case QEMU_FILE_TYPE_KEYMAP:
2300 subdir = "keymaps/";
2301 break;
2302 default:
2303 abort();
2306 for (i = 0; i < data_dir_idx; i++) {
2307 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2308 if (access(buf, R_OK) == 0) {
2309 trace_load_file(name, buf);
2310 return buf;
2312 g_free(buf);
2314 return NULL;
2317 static inline bool nonempty_str(const char *str)
2319 return str && *str;
2322 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2324 gchar *buf;
2325 size_t size;
2326 const char *name, *file, *str;
2327 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2329 if (fw_cfg == NULL) {
2330 error_report("fw_cfg device not available");
2331 return -1;
2333 name = qemu_opt_get(opts, "name");
2334 file = qemu_opt_get(opts, "file");
2335 str = qemu_opt_get(opts, "string");
2337 /* we need name and either a file or the content string */
2338 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2339 error_report("invalid argument(s)");
2340 return -1;
2342 if (nonempty_str(file) && nonempty_str(str)) {
2343 error_report("file and string are mutually exclusive");
2344 return -1;
2346 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2347 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2348 return -1;
2350 if (strncmp(name, "opt/", 4) != 0) {
2351 warn_report("externally provided fw_cfg item names "
2352 "should be prefixed with \"opt/\"");
2354 if (nonempty_str(str)) {
2355 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2356 buf = g_memdup(str, size);
2357 } else {
2358 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2359 error_report("can't load %s", file);
2360 return -1;
2363 /* For legacy, keep user files in a specific global order. */
2364 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2365 fw_cfg_add_file(fw_cfg, name, buf, size);
2366 fw_cfg_reset_order_override(fw_cfg);
2367 return 0;
2370 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2372 return qdev_device_help(opts);
2375 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2377 Error *err = NULL;
2378 DeviceState *dev;
2380 dev = qdev_device_add(opts, &err);
2381 if (!dev) {
2382 error_report_err(err);
2383 return -1;
2385 object_unref(OBJECT(dev));
2386 return 0;
2389 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2391 Error *local_err = NULL;
2393 qemu_chr_new_from_opts(opts, &local_err);
2394 if (local_err) {
2395 error_report_err(local_err);
2396 return -1;
2398 return 0;
2401 #ifdef CONFIG_VIRTFS
2402 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2404 return qemu_fsdev_add(opts);
2406 #endif
2408 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2410 Chardev *chr;
2411 const char *chardev;
2412 const char *mode;
2413 int flags;
2415 mode = qemu_opt_get(opts, "mode");
2416 if (mode == NULL) {
2417 mode = "readline";
2419 if (strcmp(mode, "readline") == 0) {
2420 flags = MONITOR_USE_READLINE;
2421 } else if (strcmp(mode, "control") == 0) {
2422 flags = MONITOR_USE_CONTROL;
2423 } else {
2424 error_report("unknown monitor mode \"%s\"", mode);
2425 exit(1);
2428 if (qemu_opt_get_bool(opts, "pretty", 0))
2429 flags |= MONITOR_USE_PRETTY;
2431 if (qemu_opt_get_bool(opts, "default", 0)) {
2432 error_report("option 'default' does nothing and is deprecated");
2435 chardev = qemu_opt_get(opts, "chardev");
2436 chr = qemu_chr_find(chardev);
2437 if (chr == NULL) {
2438 error_report("chardev \"%s\" not found", chardev);
2439 exit(1);
2442 monitor_init(chr, flags);
2443 return 0;
2446 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2448 static int monitor_device_index = 0;
2449 QemuOpts *opts;
2450 const char *p;
2451 char label[32];
2453 if (strstart(optarg, "chardev:", &p)) {
2454 snprintf(label, sizeof(label), "%s", p);
2455 } else {
2456 snprintf(label, sizeof(label), "compat_monitor%d",
2457 monitor_device_index);
2458 opts = qemu_chr_parse_compat(label, optarg);
2459 if (!opts) {
2460 error_report("parse error: %s", optarg);
2461 exit(1);
2465 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2466 qemu_opt_set(opts, "mode", mode, &error_abort);
2467 qemu_opt_set(opts, "chardev", label, &error_abort);
2468 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2469 monitor_device_index++;
2472 struct device_config {
2473 enum {
2474 DEV_USB, /* -usbdevice */
2475 DEV_BT, /* -bt */
2476 DEV_SERIAL, /* -serial */
2477 DEV_PARALLEL, /* -parallel */
2478 DEV_VIRTCON, /* -virtioconsole */
2479 DEV_DEBUGCON, /* -debugcon */
2480 DEV_GDB, /* -gdb, -s */
2481 DEV_SCLP, /* s390 sclp */
2482 } type;
2483 const char *cmdline;
2484 Location loc;
2485 QTAILQ_ENTRY(device_config) next;
2488 static QTAILQ_HEAD(, device_config) device_configs =
2489 QTAILQ_HEAD_INITIALIZER(device_configs);
2491 static void add_device_config(int type, const char *cmdline)
2493 struct device_config *conf;
2495 conf = g_malloc0(sizeof(*conf));
2496 conf->type = type;
2497 conf->cmdline = cmdline;
2498 loc_save(&conf->loc);
2499 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2502 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2504 struct device_config *conf;
2505 int rc;
2507 QTAILQ_FOREACH(conf, &device_configs, next) {
2508 if (conf->type != type)
2509 continue;
2510 loc_push_restore(&conf->loc);
2511 rc = func(conf->cmdline);
2512 loc_pop(&conf->loc);
2513 if (rc) {
2514 return rc;
2517 return 0;
2520 static int serial_parse(const char *devname)
2522 static int index = 0;
2523 char label[32];
2525 if (strcmp(devname, "none") == 0)
2526 return 0;
2527 if (index == MAX_SERIAL_PORTS) {
2528 error_report("too many serial ports");
2529 exit(1);
2531 snprintf(label, sizeof(label), "serial%d", index);
2532 serial_hds[index] = qemu_chr_new(label, devname);
2533 if (!serial_hds[index]) {
2534 error_report("could not connect serial device"
2535 " to character backend '%s'", devname);
2536 return -1;
2538 index++;
2539 return 0;
2542 static int parallel_parse(const char *devname)
2544 static int index = 0;
2545 char label[32];
2547 if (strcmp(devname, "none") == 0)
2548 return 0;
2549 if (index == MAX_PARALLEL_PORTS) {
2550 error_report("too many parallel ports");
2551 exit(1);
2553 snprintf(label, sizeof(label), "parallel%d", index);
2554 parallel_hds[index] = qemu_chr_new(label, devname);
2555 if (!parallel_hds[index]) {
2556 error_report("could not connect parallel device"
2557 " to character backend '%s'", devname);
2558 return -1;
2560 index++;
2561 return 0;
2564 static int virtcon_parse(const char *devname)
2566 QemuOptsList *device = qemu_find_opts("device");
2567 static int index = 0;
2568 char label[32];
2569 QemuOpts *bus_opts, *dev_opts;
2571 if (strcmp(devname, "none") == 0)
2572 return 0;
2573 if (index == MAX_VIRTIO_CONSOLES) {
2574 error_report("too many virtio consoles");
2575 exit(1);
2578 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2579 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2581 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2582 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2584 snprintf(label, sizeof(label), "virtcon%d", index);
2585 virtcon_hds[index] = qemu_chr_new(label, devname);
2586 if (!virtcon_hds[index]) {
2587 error_report("could not connect virtio console"
2588 " to character backend '%s'", devname);
2589 return -1;
2591 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2593 index++;
2594 return 0;
2597 static int sclp_parse(const char *devname)
2599 QemuOptsList *device = qemu_find_opts("device");
2600 static int index = 0;
2601 char label[32];
2602 QemuOpts *dev_opts;
2604 if (strcmp(devname, "none") == 0) {
2605 return 0;
2607 if (index == MAX_SCLP_CONSOLES) {
2608 error_report("too many sclp consoles");
2609 exit(1);
2612 assert(arch_type == QEMU_ARCH_S390X);
2614 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2615 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2617 snprintf(label, sizeof(label), "sclpcon%d", index);
2618 sclp_hds[index] = qemu_chr_new(label, devname);
2619 if (!sclp_hds[index]) {
2620 error_report("could not connect sclp console"
2621 " to character backend '%s'", devname);
2622 return -1;
2624 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2626 index++;
2627 return 0;
2630 static int debugcon_parse(const char *devname)
2632 QemuOpts *opts;
2634 if (!qemu_chr_new("debugcon", devname)) {
2635 exit(1);
2637 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2638 if (!opts) {
2639 error_report("already have a debugcon device");
2640 exit(1);
2642 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2643 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2644 return 0;
2647 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2649 const MachineClass *mc1 = a, *mc2 = b;
2650 int res;
2652 if (mc1->family == NULL) {
2653 if (mc2->family == NULL) {
2654 /* Compare standalone machine types against each other; they sort
2655 * in increasing order.
2657 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2658 object_class_get_name(OBJECT_CLASS(mc2)));
2661 /* Standalone machine types sort after families. */
2662 return 1;
2665 if (mc2->family == NULL) {
2666 /* Families sort before standalone machine types. */
2667 return -1;
2670 /* Families sort between each other alphabetically increasingly. */
2671 res = strcmp(mc1->family, mc2->family);
2672 if (res != 0) {
2673 return res;
2676 /* Within the same family, machine types sort in decreasing order. */
2677 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2678 object_class_get_name(OBJECT_CLASS(mc1)));
2681 static MachineClass *machine_parse(const char *name)
2683 MachineClass *mc = NULL;
2684 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2686 if (name) {
2687 mc = find_machine(name);
2689 if (mc) {
2690 g_slist_free(machines);
2691 return mc;
2693 if (name && !is_help_option(name)) {
2694 error_report("unsupported machine type");
2695 error_printf("Use -machine help to list supported machines\n");
2696 } else {
2697 printf("Supported machines are:\n");
2698 machines = g_slist_sort(machines, machine_class_cmp);
2699 for (el = machines; el; el = el->next) {
2700 MachineClass *mc = el->data;
2701 if (mc->alias) {
2702 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2704 printf("%-20s %s%s\n", mc->name, mc->desc,
2705 mc->is_default ? " (default)" : "");
2709 g_slist_free(machines);
2710 exit(!name || !is_help_option(name));
2713 void qemu_add_exit_notifier(Notifier *notify)
2715 notifier_list_add(&exit_notifiers, notify);
2718 void qemu_remove_exit_notifier(Notifier *notify)
2720 notifier_remove(notify);
2723 static void qemu_run_exit_notifiers(void)
2725 notifier_list_notify(&exit_notifiers, NULL);
2728 static bool machine_init_done;
2730 void qemu_add_machine_init_done_notifier(Notifier *notify)
2732 notifier_list_add(&machine_init_done_notifiers, notify);
2733 if (machine_init_done) {
2734 notify->notify(notify, NULL);
2738 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2740 notifier_remove(notify);
2743 static void qemu_run_machine_init_done_notifiers(void)
2745 notifier_list_notify(&machine_init_done_notifiers, NULL);
2746 machine_init_done = true;
2749 static const QEMUOption *lookup_opt(int argc, char **argv,
2750 const char **poptarg, int *poptind)
2752 const QEMUOption *popt;
2753 int optind = *poptind;
2754 char *r = argv[optind];
2755 const char *optarg;
2757 loc_set_cmdline(argv, optind, 1);
2758 optind++;
2759 /* Treat --foo the same as -foo. */
2760 if (r[1] == '-')
2761 r++;
2762 popt = qemu_options;
2763 for(;;) {
2764 if (!popt->name) {
2765 error_report("invalid option");
2766 exit(1);
2768 if (!strcmp(popt->name, r + 1))
2769 break;
2770 popt++;
2772 if (popt->flags & HAS_ARG) {
2773 if (optind >= argc) {
2774 error_report("requires an argument");
2775 exit(1);
2777 optarg = argv[optind++];
2778 loc_set_cmdline(argv, optind - 2, 2);
2779 } else {
2780 optarg = NULL;
2783 *poptarg = optarg;
2784 *poptind = optind;
2786 return popt;
2789 static MachineClass *select_machine(void)
2791 MachineClass *machine_class = find_default_machine();
2792 const char *optarg;
2793 QemuOpts *opts;
2794 Location loc;
2796 loc_push_none(&loc);
2798 opts = qemu_get_machine_opts();
2799 qemu_opts_loc_restore(opts);
2801 optarg = qemu_opt_get(opts, "type");
2802 if (optarg) {
2803 machine_class = machine_parse(optarg);
2806 if (!machine_class) {
2807 error_report("No machine specified, and there is no default");
2808 error_printf("Use -machine help to list supported machines\n");
2809 exit(1);
2812 loc_pop(&loc);
2813 return machine_class;
2816 static int machine_set_property(void *opaque,
2817 const char *name, const char *value,
2818 Error **errp)
2820 Object *obj = OBJECT(opaque);
2821 Error *local_err = NULL;
2822 char *p, *qom_name;
2824 if (strcmp(name, "type") == 0) {
2825 return 0;
2828 qom_name = g_strdup(name);
2829 for (p = qom_name; *p; p++) {
2830 if (*p == '_') {
2831 *p = '-';
2835 object_property_parse(obj, value, qom_name, &local_err);
2836 g_free(qom_name);
2838 if (local_err) {
2839 error_report_err(local_err);
2840 return -1;
2843 return 0;
2848 * Initial object creation happens before all other
2849 * QEMU data types are created. The majority of objects
2850 * can be created at this point. The rng-egd object
2851 * cannot be created here, as it depends on the chardev
2852 * already existing.
2854 static bool object_create_initial(const char *type)
2856 if (g_str_equal(type, "rng-egd")) {
2857 return false;
2861 * return false for concrete netfilters since
2862 * they depend on netdevs already existing
2864 if (g_str_equal(type, "filter-buffer") ||
2865 g_str_equal(type, "filter-dump") ||
2866 g_str_equal(type, "filter-mirror") ||
2867 g_str_equal(type, "filter-redirector") ||
2868 g_str_equal(type, "colo-compare") ||
2869 g_str_equal(type, "filter-rewriter") ||
2870 g_str_equal(type, "filter-replay")) {
2871 return false;
2874 /* Memory allocation by backends needs to be done
2875 * after configure_accelerator() (due to the tcg_enabled()
2876 * checks at memory_region_init_*()).
2878 * Also, allocation of large amounts of memory may delay
2879 * chardev initialization for too long, and trigger timeouts
2880 * on software that waits for a monitor socket to be created
2881 * (e.g. libvirt).
2883 if (g_str_has_prefix(type, "memory-backend-")) {
2884 return false;
2887 return true;
2892 * The remainder of object creation happens after the
2893 * creation of chardev, fsdev, net clients and device data types.
2895 static bool object_create_delayed(const char *type)
2897 return !object_create_initial(type);
2901 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2902 MachineClass *mc)
2904 uint64_t sz;
2905 const char *mem_str;
2906 const char *maxmem_str, *slots_str;
2907 const ram_addr_t default_ram_size = mc->default_ram_size;
2908 QemuOpts *opts = qemu_find_opts_singleton("memory");
2909 Location loc;
2911 loc_push_none(&loc);
2912 qemu_opts_loc_restore(opts);
2914 sz = 0;
2915 mem_str = qemu_opt_get(opts, "size");
2916 if (mem_str) {
2917 if (!*mem_str) {
2918 error_report("missing 'size' option value");
2919 exit(EXIT_FAILURE);
2922 sz = qemu_opt_get_size(opts, "size", ram_size);
2924 /* Fix up legacy suffix-less format */
2925 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2926 uint64_t overflow_check = sz;
2928 sz <<= 20;
2929 if ((sz >> 20) != overflow_check) {
2930 error_report("too large 'size' option value");
2931 exit(EXIT_FAILURE);
2936 /* backward compatibility behaviour for case "-m 0" */
2937 if (sz == 0) {
2938 sz = default_ram_size;
2941 sz = QEMU_ALIGN_UP(sz, 8192);
2942 ram_size = sz;
2943 if (ram_size != sz) {
2944 error_report("ram size too large");
2945 exit(EXIT_FAILURE);
2948 /* store value for the future use */
2949 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2950 *maxram_size = ram_size;
2952 maxmem_str = qemu_opt_get(opts, "maxmem");
2953 slots_str = qemu_opt_get(opts, "slots");
2954 if (maxmem_str && slots_str) {
2955 uint64_t slots;
2957 sz = qemu_opt_get_size(opts, "maxmem", 0);
2958 slots = qemu_opt_get_number(opts, "slots", 0);
2959 if (sz < ram_size) {
2960 error_report("invalid value of -m option maxmem: "
2961 "maximum memory size (0x%" PRIx64 ") must be at least "
2962 "the initial memory size (0x" RAM_ADDR_FMT ")",
2963 sz, ram_size);
2964 exit(EXIT_FAILURE);
2965 } else if (sz > ram_size) {
2966 if (!slots) {
2967 error_report("invalid value of -m option: maxmem was "
2968 "specified, but no hotplug slots were specified");
2969 exit(EXIT_FAILURE);
2971 } else if (slots) {
2972 error_report("invalid value of -m option maxmem: "
2973 "memory slots were specified but maximum memory size "
2974 "(0x%" PRIx64 ") is equal to the initial memory size "
2975 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2976 exit(EXIT_FAILURE);
2979 *maxram_size = sz;
2980 *ram_slots = slots;
2981 } else if ((!maxmem_str && slots_str) ||
2982 (maxmem_str && !slots_str)) {
2983 error_report("invalid -m option value: missing "
2984 "'%s' option", slots_str ? "maxmem" : "slots");
2985 exit(EXIT_FAILURE);
2988 loc_pop(&loc);
2991 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2993 GlobalProperty *g;
2995 g = g_malloc0(sizeof(*g));
2996 g->driver = qemu_opt_get(opts, "driver");
2997 g->property = qemu_opt_get(opts, "property");
2998 g->value = qemu_opt_get(opts, "value");
2999 g->user_provided = true;
3000 g->errp = &error_fatal;
3001 qdev_prop_register_global(g);
3002 return 0;
3005 static int qemu_read_default_config_file(void)
3007 int ret;
3009 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3010 if (ret < 0 && ret != -ENOENT) {
3011 return ret;
3014 return 0;
3017 static void user_register_global_props(void)
3019 qemu_opts_foreach(qemu_find_opts("global"),
3020 global_init_func, NULL, NULL);
3024 * Note: we should see that these properties are actually having a
3025 * priority: accel < machine < user. This means e.g. when user
3026 * specifies something in "-global", it'll always be used with highest
3027 * priority than either machine/accelerator compat properties.
3029 static void register_global_properties(MachineState *ms)
3031 accel_register_compat_props(ms->accelerator);
3032 machine_register_compat_props(ms);
3033 user_register_global_props();
3036 int main(int argc, char **argv)
3038 int i;
3039 int snapshot, linux_boot;
3040 const char *initrd_filename;
3041 const char *kernel_filename, *kernel_cmdline;
3042 const char *boot_order = NULL;
3043 const char *boot_once = NULL;
3044 DisplayState *ds;
3045 int cyls, heads, secs, translation;
3046 QemuOpts *opts, *machine_opts;
3047 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3048 QemuOptsList *olist;
3049 int optind;
3050 const char *optarg;
3051 const char *loadvm = NULL;
3052 MachineClass *machine_class;
3053 const char *cpu_model;
3054 const char *vga_model = NULL;
3055 const char *qtest_chrdev = NULL;
3056 const char *qtest_log = NULL;
3057 const char *pid_file = NULL;
3058 const char *incoming = NULL;
3059 bool defconfig = true;
3060 bool userconfig = true;
3061 bool nographic = false;
3062 DisplayType display_type = DT_DEFAULT;
3063 int display_remote = 0;
3064 const char *log_mask = NULL;
3065 const char *log_file = NULL;
3066 char *trace_file = NULL;
3067 ram_addr_t maxram_size;
3068 uint64_t ram_slots = 0;
3069 FILE *vmstate_dump_file = NULL;
3070 Error *main_loop_err = NULL;
3071 Error *err = NULL;
3072 bool list_data_dirs = false;
3073 typedef struct BlockdevOptions_queue {
3074 BlockdevOptions *bdo;
3075 Location loc;
3076 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3077 } BlockdevOptions_queue;
3078 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3079 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3081 module_call_init(MODULE_INIT_TRACE);
3083 qemu_init_cpu_list();
3084 qemu_init_cpu_loop();
3085 qemu_mutex_lock_iothread();
3087 atexit(qemu_run_exit_notifiers);
3088 error_set_progname(argv[0]);
3089 qemu_init_exec_dir(argv[0]);
3091 module_call_init(MODULE_INIT_QOM);
3092 monitor_init_qmp_commands();
3094 qemu_add_opts(&qemu_drive_opts);
3095 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3096 qemu_add_drive_opts(&qemu_common_drive_opts);
3097 qemu_add_drive_opts(&qemu_drive_opts);
3098 qemu_add_drive_opts(&bdrv_runtime_opts);
3099 qemu_add_opts(&qemu_chardev_opts);
3100 qemu_add_opts(&qemu_device_opts);
3101 qemu_add_opts(&qemu_netdev_opts);
3102 qemu_add_opts(&qemu_net_opts);
3103 qemu_add_opts(&qemu_rtc_opts);
3104 qemu_add_opts(&qemu_global_opts);
3105 qemu_add_opts(&qemu_mon_opts);
3106 qemu_add_opts(&qemu_trace_opts);
3107 qemu_add_opts(&qemu_option_rom_opts);
3108 qemu_add_opts(&qemu_machine_opts);
3109 qemu_add_opts(&qemu_accel_opts);
3110 qemu_add_opts(&qemu_mem_opts);
3111 qemu_add_opts(&qemu_smp_opts);
3112 qemu_add_opts(&qemu_boot_opts);
3113 qemu_add_opts(&qemu_sandbox_opts);
3114 qemu_add_opts(&qemu_add_fd_opts);
3115 qemu_add_opts(&qemu_object_opts);
3116 qemu_add_opts(&qemu_tpmdev_opts);
3117 qemu_add_opts(&qemu_realtime_opts);
3118 qemu_add_opts(&qemu_msg_opts);
3119 qemu_add_opts(&qemu_name_opts);
3120 qemu_add_opts(&qemu_numa_opts);
3121 qemu_add_opts(&qemu_icount_opts);
3122 qemu_add_opts(&qemu_semihosting_config_opts);
3123 qemu_add_opts(&qemu_fw_cfg_opts);
3124 module_call_init(MODULE_INIT_OPTS);
3126 runstate_init();
3128 if (qcrypto_init(&err) < 0) {
3129 error_reportf_err(err, "cannot initialize crypto: ");
3130 exit(1);
3132 rtc_clock = QEMU_CLOCK_HOST;
3134 QLIST_INIT (&vm_change_state_head);
3135 os_setup_early_signal_handling();
3137 cpu_model = NULL;
3138 snapshot = 0;
3139 cyls = heads = secs = 0;
3140 translation = BIOS_ATA_TRANSLATION_AUTO;
3142 nb_nics = 0;
3144 bdrv_init_with_whitelist();
3146 autostart = 1;
3148 /* first pass of option parsing */
3149 optind = 1;
3150 while (optind < argc) {
3151 if (argv[optind][0] != '-') {
3152 /* disk image */
3153 optind++;
3154 } else {
3155 const QEMUOption *popt;
3157 popt = lookup_opt(argc, argv, &optarg, &optind);
3158 switch (popt->index) {
3159 case QEMU_OPTION_nodefconfig:
3160 defconfig = false;
3161 break;
3162 case QEMU_OPTION_nouserconfig:
3163 userconfig = false;
3164 break;
3169 if (defconfig && userconfig) {
3170 if (qemu_read_default_config_file() < 0) {
3171 exit(1);
3175 /* second pass of option parsing */
3176 optind = 1;
3177 for(;;) {
3178 if (optind >= argc)
3179 break;
3180 if (argv[optind][0] != '-') {
3181 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3182 } else {
3183 const QEMUOption *popt;
3185 popt = lookup_opt(argc, argv, &optarg, &optind);
3186 if (!(popt->arch_mask & arch_type)) {
3187 error_report("Option not supported for this target");
3188 exit(1);
3190 switch(popt->index) {
3191 case QEMU_OPTION_no_kvm_irqchip: {
3192 olist = qemu_find_opts("machine");
3193 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3194 break;
3196 case QEMU_OPTION_cpu:
3197 /* hw initialization will check this */
3198 cpu_model = optarg;
3199 break;
3200 case QEMU_OPTION_hda:
3202 char buf[256];
3203 if (cyls == 0)
3204 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3205 else
3206 snprintf(buf, sizeof(buf),
3207 "%s,cyls=%d,heads=%d,secs=%d%s",
3208 HD_OPTS , cyls, heads, secs,
3209 translation == BIOS_ATA_TRANSLATION_LBA ?
3210 ",trans=lba" :
3211 translation == BIOS_ATA_TRANSLATION_NONE ?
3212 ",trans=none" : "");
3213 drive_add(IF_DEFAULT, 0, optarg, buf);
3214 break;
3216 case QEMU_OPTION_hdb:
3217 case QEMU_OPTION_hdc:
3218 case QEMU_OPTION_hdd:
3219 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3220 HD_OPTS);
3221 break;
3222 case QEMU_OPTION_blockdev:
3224 Visitor *v;
3225 BlockdevOptions_queue *bdo;
3227 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3228 if (!v) {
3229 error_report_err(err);
3230 exit(1);
3233 bdo = g_new(BlockdevOptions_queue, 1);
3234 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3235 &error_fatal);
3236 visit_free(v);
3237 loc_save(&bdo->loc);
3238 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3239 break;
3241 case QEMU_OPTION_drive:
3242 if (drive_def(optarg) == NULL) {
3243 exit(1);
3245 break;
3246 case QEMU_OPTION_set:
3247 if (qemu_set_option(optarg) != 0)
3248 exit(1);
3249 break;
3250 case QEMU_OPTION_global:
3251 if (qemu_global_option(optarg) != 0)
3252 exit(1);
3253 break;
3254 case QEMU_OPTION_mtdblock:
3255 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3256 break;
3257 case QEMU_OPTION_sd:
3258 drive_add(IF_SD, -1, optarg, SD_OPTS);
3259 break;
3260 case QEMU_OPTION_pflash:
3261 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3262 break;
3263 case QEMU_OPTION_snapshot:
3264 snapshot = 1;
3265 break;
3266 case QEMU_OPTION_hdachs:
3268 const char *p;
3269 p = optarg;
3270 cyls = strtol(p, (char **)&p, 0);
3271 if (cyls < 1 || cyls > 16383)
3272 goto chs_fail;
3273 if (*p != ',')
3274 goto chs_fail;
3275 p++;
3276 heads = strtol(p, (char **)&p, 0);
3277 if (heads < 1 || heads > 16)
3278 goto chs_fail;
3279 if (*p != ',')
3280 goto chs_fail;
3281 p++;
3282 secs = strtol(p, (char **)&p, 0);
3283 if (secs < 1 || secs > 63)
3284 goto chs_fail;
3285 if (*p == ',') {
3286 p++;
3287 if (!strcmp(p, "large")) {
3288 translation = BIOS_ATA_TRANSLATION_LARGE;
3289 } else if (!strcmp(p, "rechs")) {
3290 translation = BIOS_ATA_TRANSLATION_RECHS;
3291 } else if (!strcmp(p, "none")) {
3292 translation = BIOS_ATA_TRANSLATION_NONE;
3293 } else if (!strcmp(p, "lba")) {
3294 translation = BIOS_ATA_TRANSLATION_LBA;
3295 } else if (!strcmp(p, "auto")) {
3296 translation = BIOS_ATA_TRANSLATION_AUTO;
3297 } else {
3298 goto chs_fail;
3300 } else if (*p != '\0') {
3301 chs_fail:
3302 error_report("invalid physical CHS format");
3303 exit(1);
3305 if (hda_opts != NULL) {
3306 qemu_opt_set_number(hda_opts, "cyls", cyls,
3307 &error_abort);
3308 qemu_opt_set_number(hda_opts, "heads", heads,
3309 &error_abort);
3310 qemu_opt_set_number(hda_opts, "secs", secs,
3311 &error_abort);
3312 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3313 qemu_opt_set(hda_opts, "trans", "large",
3314 &error_abort);
3315 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3316 qemu_opt_set(hda_opts, "trans", "rechs",
3317 &error_abort);
3318 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3319 qemu_opt_set(hda_opts, "trans", "lba",
3320 &error_abort);
3321 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3322 qemu_opt_set(hda_opts, "trans", "none",
3323 &error_abort);
3327 error_report("'-hdachs' is deprecated, please use '-device"
3328 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3329 break;
3330 case QEMU_OPTION_numa:
3331 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3332 optarg, true);
3333 if (!opts) {
3334 exit(1);
3336 break;
3337 case QEMU_OPTION_display:
3338 display_type = select_display(optarg);
3339 break;
3340 case QEMU_OPTION_nographic:
3341 olist = qemu_find_opts("machine");
3342 qemu_opts_parse_noisily(olist, "graphics=off", false);
3343 nographic = true;
3344 display_type = DT_NONE;
3345 break;
3346 case QEMU_OPTION_curses:
3347 #ifdef CONFIG_CURSES
3348 display_type = DT_CURSES;
3349 #else
3350 error_report("curses support is disabled");
3351 exit(1);
3352 #endif
3353 break;
3354 case QEMU_OPTION_portrait:
3355 graphic_rotate = 90;
3356 break;
3357 case QEMU_OPTION_rotate:
3358 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3359 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3360 graphic_rotate != 180 && graphic_rotate != 270) {
3361 error_report("only 90, 180, 270 deg rotation is available");
3362 exit(1);
3364 break;
3365 case QEMU_OPTION_kernel:
3366 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3367 &error_abort);
3368 break;
3369 case QEMU_OPTION_initrd:
3370 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3371 &error_abort);
3372 break;
3373 case QEMU_OPTION_append:
3374 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3375 &error_abort);
3376 break;
3377 case QEMU_OPTION_dtb:
3378 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3379 &error_abort);
3380 break;
3381 case QEMU_OPTION_cdrom:
3382 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3383 break;
3384 case QEMU_OPTION_boot:
3385 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3386 optarg, true);
3387 if (!opts) {
3388 exit(1);
3390 break;
3391 case QEMU_OPTION_fda:
3392 case QEMU_OPTION_fdb:
3393 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3394 optarg, FD_OPTS);
3395 break;
3396 case QEMU_OPTION_no_fd_bootchk:
3397 fd_bootchk = 0;
3398 break;
3399 case QEMU_OPTION_netdev:
3400 default_net = 0;
3401 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3402 exit(1);
3404 break;
3405 case QEMU_OPTION_net:
3406 default_net = 0;
3407 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3408 exit(1);
3410 break;
3411 #ifdef CONFIG_LIBISCSI
3412 case QEMU_OPTION_iscsi:
3413 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3414 optarg, false);
3415 if (!opts) {
3416 exit(1);
3418 break;
3419 #endif
3420 #ifdef CONFIG_SLIRP
3421 case QEMU_OPTION_tftp:
3422 error_report("The -tftp option is deprecated. "
3423 "Please use '-netdev user,tftp=...' instead.");
3424 legacy_tftp_prefix = optarg;
3425 break;
3426 case QEMU_OPTION_bootp:
3427 error_report("The -bootp option is deprecated. "
3428 "Please use '-netdev user,bootfile=...' instead.");
3429 legacy_bootp_filename = optarg;
3430 break;
3431 case QEMU_OPTION_redir:
3432 error_report("The -redir option is deprecated. "
3433 "Please use '-netdev user,hostfwd=...' instead.");
3434 if (net_slirp_redir(optarg) < 0)
3435 exit(1);
3436 break;
3437 #endif
3438 case QEMU_OPTION_bt:
3439 add_device_config(DEV_BT, optarg);
3440 break;
3441 case QEMU_OPTION_audio_help:
3442 AUD_help ();
3443 exit (0);
3444 break;
3445 case QEMU_OPTION_soundhw:
3446 select_soundhw (optarg);
3447 break;
3448 case QEMU_OPTION_h:
3449 help(0);
3450 break;
3451 case QEMU_OPTION_version:
3452 version();
3453 exit(0);
3454 break;
3455 case QEMU_OPTION_m:
3456 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3457 optarg, true);
3458 if (!opts) {
3459 exit(EXIT_FAILURE);
3461 break;
3462 #ifdef CONFIG_TPM
3463 case QEMU_OPTION_tpmdev:
3464 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3465 exit(1);
3467 break;
3468 #endif
3469 case QEMU_OPTION_mempath:
3470 mem_path = optarg;
3471 break;
3472 case QEMU_OPTION_mem_prealloc:
3473 mem_prealloc = 1;
3474 break;
3475 case QEMU_OPTION_d:
3476 log_mask = optarg;
3477 break;
3478 case QEMU_OPTION_D:
3479 log_file = optarg;
3480 break;
3481 case QEMU_OPTION_DFILTER:
3482 qemu_set_dfilter_ranges(optarg, &error_fatal);
3483 break;
3484 case QEMU_OPTION_s:
3485 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3486 break;
3487 case QEMU_OPTION_gdb:
3488 add_device_config(DEV_GDB, optarg);
3489 break;
3490 case QEMU_OPTION_L:
3491 if (is_help_option(optarg)) {
3492 list_data_dirs = true;
3493 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3494 data_dir[data_dir_idx++] = optarg;
3496 break;
3497 case QEMU_OPTION_bios:
3498 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3499 &error_abort);
3500 break;
3501 case QEMU_OPTION_singlestep:
3502 singlestep = 1;
3503 break;
3504 case QEMU_OPTION_S:
3505 autostart = 0;
3506 break;
3507 case QEMU_OPTION_k:
3508 keyboard_layout = optarg;
3509 break;
3510 case QEMU_OPTION_localtime:
3511 rtc_utc = 0;
3512 break;
3513 case QEMU_OPTION_vga:
3514 vga_model = optarg;
3515 default_vga = 0;
3516 break;
3517 case QEMU_OPTION_g:
3519 const char *p;
3520 int w, h, depth;
3521 p = optarg;
3522 w = strtol(p, (char **)&p, 10);
3523 if (w <= 0) {
3524 graphic_error:
3525 error_report("invalid resolution or depth");
3526 exit(1);
3528 if (*p != 'x')
3529 goto graphic_error;
3530 p++;
3531 h = strtol(p, (char **)&p, 10);
3532 if (h <= 0)
3533 goto graphic_error;
3534 if (*p == 'x') {
3535 p++;
3536 depth = strtol(p, (char **)&p, 10);
3537 if (depth != 8 && depth != 15 && depth != 16 &&
3538 depth != 24 && depth != 32)
3539 goto graphic_error;
3540 } else if (*p == '\0') {
3541 depth = graphic_depth;
3542 } else {
3543 goto graphic_error;
3546 graphic_width = w;
3547 graphic_height = h;
3548 graphic_depth = depth;
3550 break;
3551 case QEMU_OPTION_echr:
3553 char *r;
3554 term_escape_char = strtol(optarg, &r, 0);
3555 if (r == optarg)
3556 printf("Bad argument to echr\n");
3557 break;
3559 case QEMU_OPTION_monitor:
3560 default_monitor = 0;
3561 if (strncmp(optarg, "none", 4)) {
3562 monitor_parse(optarg, "readline", false);
3564 break;
3565 case QEMU_OPTION_qmp:
3566 monitor_parse(optarg, "control", false);
3567 default_monitor = 0;
3568 break;
3569 case QEMU_OPTION_qmp_pretty:
3570 monitor_parse(optarg, "control", true);
3571 default_monitor = 0;
3572 break;
3573 case QEMU_OPTION_mon:
3574 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3575 true);
3576 if (!opts) {
3577 exit(1);
3579 default_monitor = 0;
3580 break;
3581 case QEMU_OPTION_chardev:
3582 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3583 optarg, true);
3584 if (!opts) {
3585 exit(1);
3587 break;
3588 case QEMU_OPTION_fsdev:
3589 olist = qemu_find_opts("fsdev");
3590 if (!olist) {
3591 error_report("fsdev support is disabled");
3592 exit(1);
3594 opts = qemu_opts_parse_noisily(olist, optarg, true);
3595 if (!opts) {
3596 exit(1);
3598 break;
3599 case QEMU_OPTION_virtfs: {
3600 QemuOpts *fsdev;
3601 QemuOpts *device;
3602 const char *writeout, *sock_fd, *socket;
3604 olist = qemu_find_opts("virtfs");
3605 if (!olist) {
3606 error_report("virtfs support is disabled");
3607 exit(1);
3609 opts = qemu_opts_parse_noisily(olist, optarg, true);
3610 if (!opts) {
3611 exit(1);
3614 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3615 qemu_opt_get(opts, "mount_tag") == NULL) {
3616 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3617 exit(1);
3619 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3620 qemu_opts_id(opts) ?:
3621 qemu_opt_get(opts, "mount_tag"),
3622 1, NULL);
3623 if (!fsdev) {
3624 error_report("duplicate or invalid fsdev id: %s",
3625 qemu_opt_get(opts, "mount_tag"));
3626 exit(1);
3629 writeout = qemu_opt_get(opts, "writeout");
3630 if (writeout) {
3631 #ifdef CONFIG_SYNC_FILE_RANGE
3632 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3633 #else
3634 error_report("writeout=immediate not supported "
3635 "on this platform");
3636 exit(1);
3637 #endif
3639 qemu_opt_set(fsdev, "fsdriver",
3640 qemu_opt_get(opts, "fsdriver"), &error_abort);
3641 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3642 &error_abort);
3643 qemu_opt_set(fsdev, "security_model",
3644 qemu_opt_get(opts, "security_model"),
3645 &error_abort);
3646 socket = qemu_opt_get(opts, "socket");
3647 if (socket) {
3648 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3650 sock_fd = qemu_opt_get(opts, "sock_fd");
3651 if (sock_fd) {
3652 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3655 qemu_opt_set_bool(fsdev, "readonly",
3656 qemu_opt_get_bool(opts, "readonly", 0),
3657 &error_abort);
3658 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3659 &error_abort);
3660 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3661 qemu_opt_set(device, "fsdev",
3662 qemu_opts_id(fsdev), &error_abort);
3663 qemu_opt_set(device, "mount_tag",
3664 qemu_opt_get(opts, "mount_tag"), &error_abort);
3665 break;
3667 case QEMU_OPTION_virtfs_synth: {
3668 QemuOpts *fsdev;
3669 QemuOpts *device;
3671 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3672 1, NULL);
3673 if (!fsdev) {
3674 error_report("duplicate option: %s", "virtfs_synth");
3675 exit(1);
3677 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3679 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3680 &error_abort);
3681 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3682 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3683 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3684 break;
3686 case QEMU_OPTION_serial:
3687 add_device_config(DEV_SERIAL, optarg);
3688 default_serial = 0;
3689 if (strncmp(optarg, "mon:", 4) == 0) {
3690 default_monitor = 0;
3692 break;
3693 case QEMU_OPTION_watchdog:
3694 if (watchdog) {
3695 error_report("only one watchdog option may be given");
3696 return 1;
3698 watchdog = optarg;
3699 break;
3700 case QEMU_OPTION_watchdog_action:
3701 if (select_watchdog_action(optarg) == -1) {
3702 error_report("unknown -watchdog-action parameter");
3703 exit(1);
3705 break;
3706 case QEMU_OPTION_virtiocon:
3707 add_device_config(DEV_VIRTCON, optarg);
3708 default_virtcon = 0;
3709 if (strncmp(optarg, "mon:", 4) == 0) {
3710 default_monitor = 0;
3712 break;
3713 case QEMU_OPTION_parallel:
3714 add_device_config(DEV_PARALLEL, optarg);
3715 default_parallel = 0;
3716 if (strncmp(optarg, "mon:", 4) == 0) {
3717 default_monitor = 0;
3719 break;
3720 case QEMU_OPTION_debugcon:
3721 add_device_config(DEV_DEBUGCON, optarg);
3722 break;
3723 case QEMU_OPTION_loadvm:
3724 loadvm = optarg;
3725 break;
3726 case QEMU_OPTION_full_screen:
3727 full_screen = 1;
3728 break;
3729 case QEMU_OPTION_no_frame:
3730 no_frame = 1;
3731 break;
3732 case QEMU_OPTION_alt_grab:
3733 alt_grab = 1;
3734 break;
3735 case QEMU_OPTION_ctrl_grab:
3736 ctrl_grab = 1;
3737 break;
3738 case QEMU_OPTION_no_quit:
3739 no_quit = 1;
3740 break;
3741 case QEMU_OPTION_sdl:
3742 #ifdef CONFIG_SDL
3743 display_type = DT_SDL;
3744 break;
3745 #else
3746 error_report("SDL support is disabled");
3747 exit(1);
3748 #endif
3749 case QEMU_OPTION_pidfile:
3750 pid_file = optarg;
3751 break;
3752 case QEMU_OPTION_win2k_hack:
3753 win2k_install_hack = 1;
3754 break;
3755 case QEMU_OPTION_rtc_td_hack: {
3756 static GlobalProperty slew_lost_ticks = {
3757 .driver = "mc146818rtc",
3758 .property = "lost_tick_policy",
3759 .value = "slew",
3762 qdev_prop_register_global(&slew_lost_ticks);
3763 break;
3765 case QEMU_OPTION_acpitable:
3766 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3767 optarg, true);
3768 if (!opts) {
3769 exit(1);
3771 acpi_table_add(opts, &error_fatal);
3772 break;
3773 case QEMU_OPTION_smbios:
3774 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3775 optarg, false);
3776 if (!opts) {
3777 exit(1);
3779 smbios_entry_add(opts, &error_fatal);
3780 break;
3781 case QEMU_OPTION_fwcfg:
3782 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3783 optarg, true);
3784 if (opts == NULL) {
3785 exit(1);
3787 break;
3788 case QEMU_OPTION_enable_kvm:
3789 olist = qemu_find_opts("machine");
3790 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3791 break;
3792 case QEMU_OPTION_enable_hax:
3793 olist = qemu_find_opts("machine");
3794 qemu_opts_parse_noisily(olist, "accel=hax", false);
3795 break;
3796 case QEMU_OPTION_M:
3797 case QEMU_OPTION_machine:
3798 olist = qemu_find_opts("machine");
3799 opts = qemu_opts_parse_noisily(olist, optarg, true);
3800 if (!opts) {
3801 exit(1);
3803 break;
3804 case QEMU_OPTION_no_kvm:
3805 olist = qemu_find_opts("machine");
3806 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3807 break;
3808 case QEMU_OPTION_no_kvm_pit: {
3809 warn_report("ignoring deprecated option");
3810 break;
3812 case QEMU_OPTION_no_kvm_pit_reinjection: {
3813 static GlobalProperty kvm_pit_lost_tick_policy = {
3814 .driver = "kvm-pit",
3815 .property = "lost_tick_policy",
3816 .value = "discard",
3819 warn_report("deprecated, replaced by "
3820 "-global kvm-pit.lost_tick_policy=discard");
3821 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3822 break;
3824 case QEMU_OPTION_accel:
3825 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3826 optarg, true);
3827 optarg = qemu_opt_get(accel_opts, "accel");
3828 if (!optarg || is_help_option(optarg)) {
3829 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3830 exit(0);
3832 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3833 false, &error_abort);
3834 qemu_opt_set(opts, "accel", optarg, &error_abort);
3835 break;
3836 case QEMU_OPTION_usb:
3837 olist = qemu_find_opts("machine");
3838 qemu_opts_parse_noisily(olist, "usb=on", false);
3839 break;
3840 case QEMU_OPTION_usbdevice:
3841 error_report("'-usbdevice' is deprecated, please use "
3842 "'-device usb-...' instead");
3843 olist = qemu_find_opts("machine");
3844 qemu_opts_parse_noisily(olist, "usb=on", false);
3845 add_device_config(DEV_USB, optarg);
3846 break;
3847 case QEMU_OPTION_device:
3848 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3849 optarg, true)) {
3850 exit(1);
3852 break;
3853 case QEMU_OPTION_smp:
3854 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3855 optarg, true)) {
3856 exit(1);
3858 break;
3859 case QEMU_OPTION_vnc:
3860 vnc_parse(optarg, &error_fatal);
3861 break;
3862 case QEMU_OPTION_no_acpi:
3863 acpi_enabled = 0;
3864 break;
3865 case QEMU_OPTION_no_hpet:
3866 no_hpet = 1;
3867 break;
3868 case QEMU_OPTION_balloon:
3869 if (balloon_parse(optarg) < 0) {
3870 error_report("unknown -balloon argument %s", optarg);
3871 exit(1);
3873 break;
3874 case QEMU_OPTION_no_reboot:
3875 no_reboot = 1;
3876 break;
3877 case QEMU_OPTION_no_shutdown:
3878 no_shutdown = 1;
3879 break;
3880 case QEMU_OPTION_show_cursor:
3881 cursor_hide = 0;
3882 break;
3883 case QEMU_OPTION_uuid:
3884 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3885 error_report("failed to parse UUID string: wrong format");
3886 exit(1);
3888 qemu_uuid_set = true;
3889 break;
3890 case QEMU_OPTION_option_rom:
3891 if (nb_option_roms >= MAX_OPTION_ROMS) {
3892 error_report("too many option ROMs");
3893 exit(1);
3895 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3896 optarg, true);
3897 if (!opts) {
3898 exit(1);
3900 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3901 option_rom[nb_option_roms].bootindex =
3902 qemu_opt_get_number(opts, "bootindex", -1);
3903 if (!option_rom[nb_option_roms].name) {
3904 error_report("Option ROM file is not specified");
3905 exit(1);
3907 nb_option_roms++;
3908 break;
3909 case QEMU_OPTION_semihosting:
3910 semihosting.enabled = true;
3911 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3912 break;
3913 case QEMU_OPTION_semihosting_config:
3914 semihosting.enabled = true;
3915 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3916 optarg, false);
3917 if (opts != NULL) {
3918 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3919 true);
3920 const char *target = qemu_opt_get(opts, "target");
3921 if (target != NULL) {
3922 if (strcmp("native", target) == 0) {
3923 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3924 } else if (strcmp("gdb", target) == 0) {
3925 semihosting.target = SEMIHOSTING_TARGET_GDB;
3926 } else if (strcmp("auto", target) == 0) {
3927 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3928 } else {
3929 error_report("unsupported semihosting-config %s",
3930 optarg);
3931 exit(1);
3933 } else {
3934 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3936 /* Set semihosting argument count and vector */
3937 qemu_opt_foreach(opts, add_semihosting_arg,
3938 &semihosting, NULL);
3939 } else {
3940 error_report("unsupported semihosting-config %s", optarg);
3941 exit(1);
3943 break;
3944 case QEMU_OPTION_tdf:
3945 warn_report("ignoring deprecated option");
3946 break;
3947 case QEMU_OPTION_name:
3948 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3949 optarg, true);
3950 if (!opts) {
3951 exit(1);
3953 break;
3954 case QEMU_OPTION_prom_env:
3955 if (nb_prom_envs >= MAX_PROM_ENVS) {
3956 error_report("too many prom variables");
3957 exit(1);
3959 prom_envs[nb_prom_envs] = optarg;
3960 nb_prom_envs++;
3961 break;
3962 case QEMU_OPTION_old_param:
3963 old_param = 1;
3964 break;
3965 case QEMU_OPTION_clock:
3966 /* Clock options no longer exist. Keep this option for
3967 * backward compatibility.
3969 break;
3970 case QEMU_OPTION_startdate:
3971 configure_rtc_date_offset(optarg, 1);
3972 break;
3973 case QEMU_OPTION_rtc:
3974 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3975 false);
3976 if (!opts) {
3977 exit(1);
3979 configure_rtc(opts);
3980 break;
3981 case QEMU_OPTION_tb_size:
3982 #ifndef CONFIG_TCG
3983 error_report("TCG is disabled");
3984 exit(1);
3985 #endif
3986 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3987 error_report("Invalid argument to -tb-size");
3988 exit(1);
3990 break;
3991 case QEMU_OPTION_icount:
3992 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3993 optarg, true);
3994 if (!icount_opts) {
3995 exit(1);
3997 break;
3998 case QEMU_OPTION_incoming:
3999 if (!incoming) {
4000 runstate_set(RUN_STATE_INMIGRATE);
4002 incoming = optarg;
4003 break;
4004 case QEMU_OPTION_only_migratable:
4006 * TODO: we can remove this option one day, and we
4007 * should all use:
4009 * "-global migration.only-migratable=true"
4011 qemu_global_option("migration.only-migratable=true");
4012 break;
4013 case QEMU_OPTION_nodefaults:
4014 has_defaults = 0;
4015 break;
4016 case QEMU_OPTION_xen_domid:
4017 if (!(xen_available())) {
4018 error_report("Option not supported for this target");
4019 exit(1);
4021 xen_domid = atoi(optarg);
4022 break;
4023 case QEMU_OPTION_xen_create:
4024 if (!(xen_available())) {
4025 error_report("Option not supported for this target");
4026 exit(1);
4028 xen_mode = XEN_CREATE;
4029 break;
4030 case QEMU_OPTION_xen_attach:
4031 if (!(xen_available())) {
4032 error_report("Option not supported for this target");
4033 exit(1);
4035 xen_mode = XEN_ATTACH;
4036 break;
4037 case QEMU_OPTION_xen_domid_restrict:
4038 if (!(xen_available())) {
4039 error_report("Option not supported for this target");
4040 exit(1);
4042 xen_domid_restrict = true;
4043 break;
4044 case QEMU_OPTION_trace:
4045 g_free(trace_file);
4046 trace_file = trace_opt_parse(optarg);
4047 break;
4048 case QEMU_OPTION_trace_unassigned:
4049 trace_unassigned = true;
4050 break;
4051 case QEMU_OPTION_readconfig:
4053 int ret = qemu_read_config_file(optarg);
4054 if (ret < 0) {
4055 error_report("read config %s: %s", optarg,
4056 strerror(-ret));
4057 exit(1);
4059 break;
4061 case QEMU_OPTION_spice:
4062 olist = qemu_find_opts("spice");
4063 if (!olist) {
4064 error_report("spice support is disabled");
4065 exit(1);
4067 opts = qemu_opts_parse_noisily(olist, optarg, false);
4068 if (!opts) {
4069 exit(1);
4071 display_remote++;
4072 break;
4073 case QEMU_OPTION_writeconfig:
4075 FILE *fp;
4076 if (strcmp(optarg, "-") == 0) {
4077 fp = stdout;
4078 } else {
4079 fp = fopen(optarg, "w");
4080 if (fp == NULL) {
4081 error_report("open %s: %s", optarg,
4082 strerror(errno));
4083 exit(1);
4086 qemu_config_write(fp);
4087 if (fp != stdout) {
4088 fclose(fp);
4090 break;
4092 case QEMU_OPTION_qtest:
4093 qtest_chrdev = optarg;
4094 break;
4095 case QEMU_OPTION_qtest_log:
4096 qtest_log = optarg;
4097 break;
4098 case QEMU_OPTION_sandbox:
4099 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4100 optarg, true);
4101 if (!opts) {
4102 exit(1);
4104 break;
4105 case QEMU_OPTION_add_fd:
4106 #ifndef _WIN32
4107 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4108 optarg, false);
4109 if (!opts) {
4110 exit(1);
4112 #else
4113 error_report("File descriptor passing is disabled on this "
4114 "platform");
4115 exit(1);
4116 #endif
4117 break;
4118 case QEMU_OPTION_object:
4119 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4120 optarg, true);
4121 if (!opts) {
4122 exit(1);
4124 break;
4125 case QEMU_OPTION_realtime:
4126 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4127 optarg, false);
4128 if (!opts) {
4129 exit(1);
4131 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4132 break;
4133 case QEMU_OPTION_msg:
4134 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4135 false);
4136 if (!opts) {
4137 exit(1);
4139 configure_msg(opts);
4140 break;
4141 case QEMU_OPTION_dump_vmstate:
4142 if (vmstate_dump_file) {
4143 error_report("only one '-dump-vmstate' "
4144 "option may be given");
4145 exit(1);
4147 vmstate_dump_file = fopen(optarg, "w");
4148 if (vmstate_dump_file == NULL) {
4149 error_report("open %s: %s", optarg, strerror(errno));
4150 exit(1);
4152 break;
4153 default:
4154 os_parse_cmd_args(popt->index, optarg);
4159 * Clear error location left behind by the loop.
4160 * Best done right after the loop. Do not insert code here!
4162 loc_set_none();
4164 replay_configure(icount_opts);
4166 machine_class = select_machine();
4168 set_memory_options(&ram_slots, &maxram_size, machine_class);
4170 os_daemonize();
4172 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4173 error_report("could not acquire pid file: %s", strerror(errno));
4174 exit(1);
4177 if (qemu_init_main_loop(&main_loop_err)) {
4178 error_report_err(main_loop_err);
4179 exit(1);
4182 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4183 parse_sandbox, NULL, NULL)) {
4184 exit(1);
4187 if (qemu_opts_foreach(qemu_find_opts("name"),
4188 parse_name, NULL, NULL)) {
4189 exit(1);
4192 #ifndef _WIN32
4193 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4194 parse_add_fd, NULL, NULL)) {
4195 exit(1);
4198 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4199 cleanup_add_fd, NULL, NULL)) {
4200 exit(1);
4202 #endif
4204 current_machine = MACHINE(object_new(object_class_get_name(
4205 OBJECT_CLASS(machine_class))));
4206 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4207 exit(0);
4209 object_property_add_child(object_get_root(), "machine",
4210 OBJECT(current_machine), &error_abort);
4212 if (machine_class->minimum_page_bits) {
4213 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4214 /* This would be a board error: specifying a minimum smaller than
4215 * a target's compile-time fixed setting.
4217 g_assert_not_reached();
4221 cpu_exec_init_all();
4223 if (machine_class->hw_version) {
4224 qemu_set_hw_version(machine_class->hw_version);
4227 if (cpu_model && is_help_option(cpu_model)) {
4228 list_cpus(stdout, &fprintf, cpu_model);
4229 exit(0);
4232 if (!trace_init_backends()) {
4233 exit(1);
4235 trace_init_file(trace_file);
4237 /* Open the logfile at this point and set the log mask if necessary.
4239 if (log_file) {
4240 qemu_set_log_filename(log_file, &error_fatal);
4243 if (log_mask) {
4244 int mask;
4245 mask = qemu_str_to_log_mask(log_mask);
4246 if (!mask) {
4247 qemu_print_log_usage(stdout);
4248 exit(1);
4250 qemu_set_log(mask);
4251 } else {
4252 qemu_set_log(0);
4255 /* If no data_dir is specified then try to find it relative to the
4256 executable path. */
4257 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4258 data_dir[data_dir_idx] = os_find_datadir();
4259 if (data_dir[data_dir_idx] != NULL) {
4260 data_dir_idx++;
4263 /* If all else fails use the install path specified when building. */
4264 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4265 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4268 /* -L help lists the data directories and exits. */
4269 if (list_data_dirs) {
4270 for (i = 0; i < data_dir_idx; i++) {
4271 printf("%s\n", data_dir[i]);
4273 exit(0);
4276 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4278 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4279 if (max_cpus > machine_class->max_cpus) {
4280 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4281 "supported by machine '%s' (%d)", max_cpus,
4282 machine_class->name, machine_class->max_cpus);
4283 exit(1);
4287 * Get the default machine options from the machine if it is not already
4288 * specified either by the configuration file or by the command line.
4290 if (machine_class->default_machine_opts) {
4291 qemu_opts_set_defaults(qemu_find_opts("machine"),
4292 machine_class->default_machine_opts, 0);
4295 qemu_opts_foreach(qemu_find_opts("device"),
4296 default_driver_check, NULL, NULL);
4297 qemu_opts_foreach(qemu_find_opts("global"),
4298 default_driver_check, NULL, NULL);
4300 if (!vga_model && !default_vga) {
4301 vga_interface_type = VGA_DEVICE;
4303 if (!has_defaults || machine_class->no_serial) {
4304 default_serial = 0;
4306 if (!has_defaults || machine_class->no_parallel) {
4307 default_parallel = 0;
4309 if (!has_defaults || !machine_class->use_virtcon) {
4310 default_virtcon = 0;
4312 if (!has_defaults || !machine_class->use_sclp) {
4313 default_sclp = 0;
4315 if (!has_defaults || machine_class->no_floppy) {
4316 default_floppy = 0;
4318 if (!has_defaults || machine_class->no_cdrom) {
4319 default_cdrom = 0;
4321 if (!has_defaults || machine_class->no_sdcard) {
4322 default_sdcard = 0;
4324 if (!has_defaults) {
4325 default_monitor = 0;
4326 default_net = 0;
4327 default_vga = 0;
4330 if (is_daemonized()) {
4331 /* According to documentation and historically, -nographic redirects
4332 * serial port, parallel port and monitor to stdio, which does not work
4333 * with -daemonize. We can redirect these to null instead, but since
4334 * -nographic is legacy, let's just error out.
4335 * We disallow -nographic only if all other ports are not redirected
4336 * explicitly, to not break existing legacy setups which uses
4337 * -nographic _and_ redirects all ports explicitly - this is valid
4338 * usage, -nographic is just a no-op in this case.
4340 if (nographic
4341 && (default_parallel || default_serial
4342 || default_monitor || default_virtcon)) {
4343 error_report("-nographic cannot be used with -daemonize");
4344 exit(1);
4346 #ifdef CONFIG_CURSES
4347 if (display_type == DT_CURSES) {
4348 error_report("curses display cannot be used with -daemonize");
4349 exit(1);
4351 #endif
4354 if (nographic) {
4355 if (default_parallel)
4356 add_device_config(DEV_PARALLEL, "null");
4357 if (default_serial && default_monitor) {
4358 add_device_config(DEV_SERIAL, "mon:stdio");
4359 } else if (default_virtcon && default_monitor) {
4360 add_device_config(DEV_VIRTCON, "mon:stdio");
4361 } else if (default_sclp && default_monitor) {
4362 add_device_config(DEV_SCLP, "mon:stdio");
4363 } else {
4364 if (default_serial)
4365 add_device_config(DEV_SERIAL, "stdio");
4366 if (default_virtcon)
4367 add_device_config(DEV_VIRTCON, "stdio");
4368 if (default_sclp) {
4369 add_device_config(DEV_SCLP, "stdio");
4371 if (default_monitor)
4372 monitor_parse("stdio", "readline", false);
4374 } else {
4375 if (default_serial)
4376 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4377 if (default_parallel)
4378 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4379 if (default_monitor)
4380 monitor_parse("vc:80Cx24C", "readline", false);
4381 if (default_virtcon)
4382 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4383 if (default_sclp) {
4384 add_device_config(DEV_SCLP, "vc:80Cx24C");
4388 #if defined(CONFIG_VNC)
4389 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4390 display_remote++;
4392 #endif
4393 if (display_type == DT_DEFAULT && !display_remote) {
4394 #if defined(CONFIG_GTK)
4395 display_type = DT_GTK;
4396 #elif defined(CONFIG_SDL)
4397 display_type = DT_SDL;
4398 #elif defined(CONFIG_COCOA)
4399 display_type = DT_COCOA;
4400 #elif defined(CONFIG_VNC)
4401 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4402 #else
4403 display_type = DT_NONE;
4404 #endif
4407 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4408 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4409 "for SDL, ignoring option");
4411 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4412 error_report("-no-quit is only valid for GTK and SDL, "
4413 "ignoring option");
4416 if (display_type == DT_GTK) {
4417 early_gtk_display_init(request_opengl);
4420 if (display_type == DT_SDL) {
4421 sdl_display_early_init(request_opengl);
4424 qemu_console_early_init();
4426 if (request_opengl == 1 && display_opengl == 0) {
4427 #if defined(CONFIG_OPENGL)
4428 error_report("OpenGL is not supported by the display");
4429 #else
4430 error_report("OpenGL support is disabled");
4431 #endif
4432 exit(1);
4435 page_size_init();
4436 socket_init();
4438 if (qemu_opts_foreach(qemu_find_opts("object"),
4439 user_creatable_add_opts_foreach,
4440 object_create_initial, NULL)) {
4441 exit(1);
4444 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4445 chardev_init_func, NULL, NULL)) {
4446 exit(1);
4449 #ifdef CONFIG_VIRTFS
4450 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4451 fsdev_init_func, NULL, NULL)) {
4452 exit(1);
4454 #endif
4456 if (qemu_opts_foreach(qemu_find_opts("device"),
4457 device_help_func, NULL, NULL)) {
4458 exit(0);
4461 machine_opts = qemu_get_machine_opts();
4462 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4463 NULL)) {
4464 object_unref(OBJECT(current_machine));
4465 exit(1);
4468 configure_accelerator(current_machine);
4471 * Register all the global properties, including accel properties,
4472 * machine properties, and user-specified ones.
4474 register_global_properties(current_machine);
4477 * Migration object can only be created after global properties
4478 * are applied correctly.
4480 migration_object_init();
4482 if (qtest_chrdev) {
4483 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4486 machine_opts = qemu_get_machine_opts();
4487 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4488 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4489 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4490 bios_name = qemu_opt_get(machine_opts, "firmware");
4492 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4493 if (opts) {
4494 boot_order = qemu_opt_get(opts, "order");
4495 if (boot_order) {
4496 validate_bootdevices(boot_order, &error_fatal);
4499 boot_once = qemu_opt_get(opts, "once");
4500 if (boot_once) {
4501 validate_bootdevices(boot_once, &error_fatal);
4504 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4505 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4508 if (!boot_order) {
4509 boot_order = machine_class->default_boot_order;
4512 if (!kernel_cmdline) {
4513 kernel_cmdline = "";
4514 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4517 linux_boot = (kernel_filename != NULL);
4519 if (!linux_boot && *kernel_cmdline != '\0') {
4520 error_report("-append only allowed with -kernel option");
4521 exit(1);
4524 if (!linux_boot && initrd_filename != NULL) {
4525 error_report("-initrd only allowed with -kernel option");
4526 exit(1);
4529 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4530 /* fall back to the -kernel/-append */
4531 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4534 os_set_line_buffering();
4536 /* spice needs the timers to be initialized by this point */
4537 qemu_spice_init();
4539 cpu_ticks_init();
4540 if (icount_opts) {
4541 if (!tcg_enabled()) {
4542 error_report("-icount is not allowed with hardware virtualization");
4543 exit(1);
4545 configure_icount(icount_opts, &error_abort);
4546 qemu_opts_del(icount_opts);
4549 if (tcg_enabled()) {
4550 qemu_tcg_configure(accel_opts, &error_fatal);
4553 if (default_net) {
4554 QemuOptsList *net = qemu_find_opts("net");
4555 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4556 #ifdef CONFIG_SLIRP
4557 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4558 #endif
4561 colo_info_init();
4563 if (net_init_clients() < 0) {
4564 exit(1);
4567 if (qemu_opts_foreach(qemu_find_opts("object"),
4568 user_creatable_add_opts_foreach,
4569 object_create_delayed, NULL)) {
4570 exit(1);
4573 #ifdef CONFIG_TPM
4574 if (tpm_init() < 0) {
4575 exit(1);
4577 #endif
4579 /* init the bluetooth world */
4580 if (foreach_device_config(DEV_BT, bt_parse))
4581 exit(1);
4583 if (!xen_enabled()) {
4584 /* On 32-bit hosts, QEMU is limited by virtual address space */
4585 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4586 error_report("at most 2047 MB RAM can be simulated");
4587 exit(1);
4591 blk_mig_init();
4592 ram_mig_init();
4594 /* If the currently selected machine wishes to override the units-per-bus
4595 * property of its default HBA interface type, do so now. */
4596 if (machine_class->units_per_default_bus) {
4597 override_max_devs(machine_class->block_default_type,
4598 machine_class->units_per_default_bus);
4601 /* open the virtual block devices */
4602 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4603 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4605 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4606 loc_push_restore(&bdo->loc);
4607 qmp_blockdev_add(bdo->bdo, &error_fatal);
4608 loc_pop(&bdo->loc);
4609 qapi_free_BlockdevOptions(bdo->bdo);
4610 g_free(bdo);
4612 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4613 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4614 NULL, NULL);
4616 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4617 &machine_class->block_default_type, NULL)) {
4618 exit(1);
4621 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4622 CDROM_OPTS);
4623 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4624 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4626 parse_numa_opts(current_machine);
4628 if (qemu_opts_foreach(qemu_find_opts("mon"),
4629 mon_init_func, NULL, NULL)) {
4630 exit(1);
4633 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4634 exit(1);
4635 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4636 exit(1);
4637 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4638 exit(1);
4639 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4640 exit(1);
4642 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4643 exit(1);
4645 /* If no default VGA is requested, the default is "none". */
4646 if (default_vga) {
4647 if (machine_class->default_display) {
4648 vga_model = machine_class->default_display;
4649 } else if (vga_interface_available(VGA_CIRRUS)) {
4650 vga_model = "cirrus";
4651 } else if (vga_interface_available(VGA_STD)) {
4652 vga_model = "std";
4655 if (vga_model) {
4656 select_vgahw(vga_model);
4659 if (watchdog) {
4660 i = select_watchdog(watchdog);
4661 if (i > 0)
4662 exit (i == 1 ? 1 : 0);
4665 /* This checkpoint is required by replay to separate prior clock
4666 reading from the other reads, because timer polling functions query
4667 clock values from the log. */
4668 replay_checkpoint(CHECKPOINT_INIT);
4669 qdev_machine_init();
4671 current_machine->ram_size = ram_size;
4672 current_machine->maxram_size = maxram_size;
4673 current_machine->ram_slots = ram_slots;
4674 current_machine->boot_order = boot_order;
4675 current_machine->cpu_model = cpu_model;
4677 machine_run_board_init(current_machine);
4679 realtime_init();
4681 soundhw_init();
4683 if (hax_enabled()) {
4684 hax_sync_vcpus();
4687 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4688 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4689 exit(1);
4692 /* init USB devices */
4693 if (machine_usb(current_machine)) {
4694 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4695 exit(1);
4698 /* Check if IGD GFX passthrough. */
4699 igd_gfx_passthru();
4701 /* init generic devices */
4702 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4703 if (qemu_opts_foreach(qemu_find_opts("device"),
4704 device_init_func, NULL, NULL)) {
4705 exit(1);
4708 cpu_synchronize_all_post_init();
4710 rom_reset_order_override();
4713 * Create frontends for -drive if=scsi leftovers.
4714 * Normally, frontends for -drive get created by machine
4715 * initialization for onboard SCSI HBAs. However, we create a few
4716 * more ever since SCSI qdevification, but this is pretty much an
4717 * implementation accident, and deprecated.
4719 scsi_legacy_handle_cmdline();
4721 /* Did we create any drives that we failed to create a device for? */
4722 drive_check_orphaned();
4724 /* Don't warn about the default network setup that you get if
4725 * no command line -net or -netdev options are specified. There
4726 * are two cases that we would otherwise complain about:
4727 * (1) board doesn't support a NIC but the implicit "-net nic"
4728 * requested one
4729 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4730 * sets up a nic that isn't connected to anything.
4732 if (!default_net) {
4733 net_check_clients();
4737 if (boot_once) {
4738 qemu_boot_set(boot_once, &error_fatal);
4739 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4742 ds = init_displaystate();
4744 /* init local displays */
4745 switch (display_type) {
4746 case DT_CURSES:
4747 curses_display_init(ds, full_screen);
4748 break;
4749 case DT_SDL:
4750 sdl_display_init(ds, full_screen, no_frame);
4751 break;
4752 case DT_COCOA:
4753 cocoa_display_init(ds, full_screen);
4754 break;
4755 case DT_GTK:
4756 gtk_display_init(ds, full_screen, grab_on_hover);
4757 break;
4758 default:
4759 break;
4762 /* must be after terminal init, SDL library changes signal handlers */
4763 os_setup_signal_handling();
4765 /* init remote displays */
4766 #ifdef CONFIG_VNC
4767 qemu_opts_foreach(qemu_find_opts("vnc"),
4768 vnc_init_func, NULL, NULL);
4769 #endif
4771 if (using_spice) {
4772 qemu_spice_display_init();
4775 #ifdef CONFIG_OPENGL_DMABUF
4776 if (display_type == DT_EGL) {
4777 egl_headless_init();
4779 #endif
4781 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4782 exit(1);
4785 qdev_machine_creation_done();
4787 /* TODO: once all bus devices are qdevified, this should be done
4788 * when bus is created by qdev.c */
4789 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4790 qemu_run_machine_init_done_notifiers();
4792 if (rom_check_and_register_reset() != 0) {
4793 error_report("rom check and register reset failed");
4794 exit(1);
4797 replay_start();
4799 /* This checkpoint is required by replay to separate prior clock
4800 reading from the other reads, because timer polling functions query
4801 clock values from the log. */
4802 replay_checkpoint(CHECKPOINT_RESET);
4803 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4804 register_global_state();
4805 if (replay_mode != REPLAY_MODE_NONE) {
4806 replay_vmstate_init();
4807 } else if (loadvm) {
4808 Error *local_err = NULL;
4809 if (load_snapshot(loadvm, &local_err) < 0) {
4810 error_report_err(local_err);
4811 autostart = 0;
4815 qdev_prop_check_globals();
4816 if (vmstate_dump_file) {
4817 /* dump and exit */
4818 dump_vmstate_json_to_file(vmstate_dump_file);
4819 return 0;
4822 if (incoming) {
4823 Error *local_err = NULL;
4824 qemu_start_incoming_migration(incoming, &local_err);
4825 if (local_err) {
4826 error_reportf_err(local_err, "-incoming %s: ", incoming);
4827 exit(1);
4829 } else if (autostart) {
4830 vm_start();
4833 os_setup_post();
4835 main_loop();
4836 replay_disable_events();
4837 iothread_stop_all();
4839 bdrv_close_all();
4840 pause_all_vcpus();
4841 res_free();
4843 /* vhost-user must be cleaned up before chardevs. */
4844 net_cleanup();
4845 audio_cleanup();
4846 monitor_cleanup();
4847 qemu_chr_cleanup();
4848 /* TODO: unref root container, check all devices are ok */
4850 return 0;