Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob5c2721593cd0312600d371fe13df389c20d82664
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu-version.h"
28 #include "qemu/cutils.h"
29 #include "qemu/help_option.h"
30 #include "qemu/uuid.h"
32 #ifdef CONFIG_SECCOMP
33 #include <sys/prctl.h>
34 #include "sysemu/seccomp.h"
35 #endif
37 #ifdef CONFIG_SDL
38 #if defined(__APPLE__) || defined(main)
39 #define SDL_MAIN_HANDLED
40 #include <SDL.h>
41 #if SDL_MAJOR_VERSION == 1
42 int qemu_main(int argc, char **argv);
43 int main(int argc, char **argv)
45 return qemu_main(argc, argv);
47 #undef main
48 #define main qemu_main
49 #elif SDL_MAJOR_VERSION == 2
50 #undef main
51 #else
52 #error undefined SDL version
53 #endif
54 #endif
55 #endif /* CONFIG_SDL */
57 #ifdef CONFIG_COCOA
58 #undef main
59 #define main qemu_main
60 #endif /* CONFIG_COCOA */
63 #include "qemu/error-report.h"
64 #include "qemu/sockets.h"
65 #include "hw/hw.h"
66 #include "hw/boards.h"
67 #include "sysemu/accel.h"
68 #include "hw/usb.h"
69 #include "hw/isa/isa.h"
70 #include "hw/scsi/scsi.h"
71 #include "hw/display/vga.h"
72 #include "hw/bt.h"
73 #include "sysemu/watchdog.h"
74 #include "hw/smbios/smbios.h"
75 #include "hw/acpi/acpi.h"
76 #include "hw/xen/xen.h"
77 #include "hw/qdev.h"
78 #include "hw/loader.h"
79 #include "monitor/qdev.h"
80 #include "sysemu/bt.h"
81 #include "net/net.h"
82 #include "net/slirp.h"
83 #include "monitor/monitor.h"
84 #include "ui/console.h"
85 #include "ui/input.h"
86 #include "sysemu/sysemu.h"
87 #include "sysemu/numa.h"
88 #include "exec/gdbstub.h"
89 #include "qemu/timer.h"
90 #include "chardev/char.h"
91 #include "qemu/bitmap.h"
92 #include "qemu/log.h"
93 #include "sysemu/blockdev.h"
94 #include "hw/block/block.h"
95 #include "migration/misc.h"
96 #include "migration/snapshot.h"
97 #include "migration/global_state.h"
98 #include "sysemu/tpm.h"
99 #include "sysemu/dma.h"
100 #include "hw/audio/soundhw.h"
101 #include "audio/audio.h"
102 #include "sysemu/cpus.h"
103 #include "migration/colo.h"
104 #include "sysemu/kvm.h"
105 #include "sysemu/hax.h"
106 #include "qapi/qobject-input-visitor.h"
107 #include "qemu/option.h"
108 #include "qemu/config-file.h"
109 #include "qemu-options.h"
110 #include "qemu/main-loop.h"
111 #ifdef CONFIG_VIRTFS
112 #include "fsdev/qemu-fsdev.h"
113 #endif
114 #include "sysemu/qtest.h"
116 #include "disas/disas.h"
119 #include "slirp/libslirp.h"
121 #include "trace-root.h"
122 #include "trace/control.h"
123 #include "qemu/queue.h"
124 #include "sysemu/arch_init.h"
126 #include "ui/qemu-spice.h"
127 #include "qapi/string-input-visitor.h"
128 #include "qapi/opts-visitor.h"
129 #include "qom/object_interfaces.h"
130 #include "exec/semihost.h"
131 #include "crypto/init.h"
132 #include "sysemu/replay.h"
133 #include "qapi/qapi-events-run-state.h"
134 #include "qapi/qapi-visit-block-core.h"
135 #include "qapi/qapi-commands-block-core.h"
136 #include "qapi/qapi-commands-misc.h"
137 #include "qapi/qapi-commands-run-state.h"
138 #include "qapi/qmp/qerror.h"
139 #include "sysemu/iothread.h"
141 #define MAX_VIRTIO_CONSOLES 1
142 #define MAX_SCLP_CONSOLES 1
144 static const char *data_dir[16];
145 static int data_dir_idx;
146 const char *bios_name = NULL;
147 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
148 int display_opengl;
149 const char* keyboard_layout = NULL;
150 ram_addr_t ram_size;
151 const char *mem_path = NULL;
152 int mem_prealloc = 0; /* force preallocation of physical target memory */
153 bool enable_mlock = false;
154 int nb_nics;
155 NICInfo nd_table[MAX_NICS];
156 int autostart;
157 static int rtc_utc = 1;
158 static int rtc_date_offset = -1; /* -1 means no change */
159 QEMUClockType rtc_clock;
160 int vga_interface_type = VGA_NONE;
161 static DisplayOptions dpy;
162 int no_frame;
163 Chardev *serial_hds[MAX_SERIAL_PORTS];
164 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
165 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
166 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
167 int win2k_install_hack = 0;
168 int singlestep = 0;
169 int smp_cpus;
170 unsigned int max_cpus;
171 int smp_cores = 1;
172 int smp_threads = 1;
173 int acpi_enabled = 1;
174 int no_hpet = 0;
175 int fd_bootchk = 1;
176 static int no_reboot;
177 int no_shutdown = 0;
178 int cursor_hide = 1;
179 int graphic_rotate = 0;
180 const char *watchdog;
181 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
182 int nb_option_roms;
183 int old_param = 0;
184 const char *qemu_name;
185 int alt_grab = 0;
186 int ctrl_grab = 0;
187 unsigned int nb_prom_envs = 0;
188 const char *prom_envs[MAX_PROM_ENVS];
189 int boot_menu;
190 bool boot_strict;
191 uint8_t *boot_splash_filedata;
192 size_t boot_splash_filedata_size;
193 uint8_t qemu_extra_params_fw[2];
195 int icount_align_option;
197 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
198 * little-endian "wire format" described in the SMBIOS 2.6 specification.
200 QemuUUID qemu_uuid;
201 bool qemu_uuid_set;
203 /* Trace unassigned memory or i/o accesses. */
204 bool trace_unassigned;
206 static NotifierList exit_notifiers =
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
209 static NotifierList machine_init_done_notifiers =
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
212 bool xen_allowed;
213 uint32_t xen_domid;
214 enum xen_mode xen_mode = XEN_EMULATE;
215 bool xen_domid_restrict;
217 static int has_defaults = 1;
218 static int default_serial = 1;
219 static int default_parallel = 1;
220 static int default_virtcon = 1;
221 static int default_sclp = 1;
222 static int default_monitor = 1;
223 static int default_floppy = 1;
224 static int default_cdrom = 1;
225 static int default_sdcard = 1;
226 static int default_vga = 1;
227 static int default_net = 1;
229 static struct {
230 const char *driver;
231 int *flag;
232 } default_list[] = {
233 { .driver = "isa-serial", .flag = &default_serial },
234 { .driver = "isa-parallel", .flag = &default_parallel },
235 { .driver = "isa-fdc", .flag = &default_floppy },
236 { .driver = "floppy", .flag = &default_floppy },
237 { .driver = "ide-cd", .flag = &default_cdrom },
238 { .driver = "ide-hd", .flag = &default_cdrom },
239 { .driver = "ide-drive", .flag = &default_cdrom },
240 { .driver = "scsi-cd", .flag = &default_cdrom },
241 { .driver = "scsi-hd", .flag = &default_cdrom },
242 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
243 { .driver = "virtio-serial", .flag = &default_virtcon },
244 { .driver = "VGA", .flag = &default_vga },
245 { .driver = "isa-vga", .flag = &default_vga },
246 { .driver = "cirrus-vga", .flag = &default_vga },
247 { .driver = "isa-cirrus-vga", .flag = &default_vga },
248 { .driver = "vmware-svga", .flag = &default_vga },
249 { .driver = "qxl-vga", .flag = &default_vga },
250 { .driver = "virtio-vga", .flag = &default_vga },
253 static QemuOptsList qemu_rtc_opts = {
254 .name = "rtc",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
256 .desc = {
258 .name = "base",
259 .type = QEMU_OPT_STRING,
261 .name = "clock",
262 .type = QEMU_OPT_STRING,
264 .name = "driftfix",
265 .type = QEMU_OPT_STRING,
267 { /* end of list */ }
271 static QemuOptsList qemu_sandbox_opts = {
272 .name = "sandbox",
273 .implied_opt_name = "enable",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
275 .desc = {
277 .name = "enable",
278 .type = QEMU_OPT_BOOL,
281 .name = "obsolete",
282 .type = QEMU_OPT_STRING,
285 .name = "elevateprivileges",
286 .type = QEMU_OPT_STRING,
289 .name = "spawn",
290 .type = QEMU_OPT_STRING,
293 .name = "resourcecontrol",
294 .type = QEMU_OPT_STRING,
296 { /* end of list */ }
300 static QemuOptsList qemu_option_rom_opts = {
301 .name = "option-rom",
302 .implied_opt_name = "romfile",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
304 .desc = {
306 .name = "bootindex",
307 .type = QEMU_OPT_NUMBER,
308 }, {
309 .name = "romfile",
310 .type = QEMU_OPT_STRING,
312 { /* end of list */ }
316 static QemuOptsList qemu_machine_opts = {
317 .name = "machine",
318 .implied_opt_name = "type",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
321 .desc = {
323 * no elements => accept any
324 * sanity checking will happen later
325 * when setting machine properties
331 static QemuOptsList qemu_accel_opts = {
332 .name = "accel",
333 .implied_opt_name = "accel",
334 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
335 .merge_lists = true,
336 .desc = {
338 .name = "accel",
339 .type = QEMU_OPT_STRING,
340 .help = "Select the type of accelerator",
343 .name = "thread",
344 .type = QEMU_OPT_STRING,
345 .help = "Enable/disable multi-threaded TCG",
347 { /* end of list */ }
351 static QemuOptsList qemu_boot_opts = {
352 .name = "boot-opts",
353 .implied_opt_name = "order",
354 .merge_lists = true,
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
356 .desc = {
358 .name = "order",
359 .type = QEMU_OPT_STRING,
360 }, {
361 .name = "once",
362 .type = QEMU_OPT_STRING,
363 }, {
364 .name = "menu",
365 .type = QEMU_OPT_BOOL,
366 }, {
367 .name = "splash",
368 .type = QEMU_OPT_STRING,
369 }, {
370 .name = "splash-time",
371 .type = QEMU_OPT_STRING,
372 }, {
373 .name = "reboot-timeout",
374 .type = QEMU_OPT_STRING,
375 }, {
376 .name = "strict",
377 .type = QEMU_OPT_BOOL,
379 { /*End of list */ }
383 static QemuOptsList qemu_add_fd_opts = {
384 .name = "add-fd",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
386 .desc = {
388 .name = "fd",
389 .type = QEMU_OPT_NUMBER,
390 .help = "file descriptor of which a duplicate is added to fd set",
392 .name = "set",
393 .type = QEMU_OPT_NUMBER,
394 .help = "ID of the fd set to add fd to",
396 .name = "opaque",
397 .type = QEMU_OPT_STRING,
398 .help = "free-form string used to describe fd",
400 { /* end of list */ }
404 static QemuOptsList qemu_object_opts = {
405 .name = "object",
406 .implied_opt_name = "qom-type",
407 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
408 .desc = {
413 static QemuOptsList qemu_tpmdev_opts = {
414 .name = "tpmdev",
415 .implied_opt_name = "type",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
417 .desc = {
418 /* options are defined in the TPM backends */
419 { /* end of list */ }
423 static QemuOptsList qemu_realtime_opts = {
424 .name = "realtime",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
426 .desc = {
428 .name = "mlock",
429 .type = QEMU_OPT_BOOL,
431 { /* end of list */ }
435 static QemuOptsList qemu_msg_opts = {
436 .name = "msg",
437 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
438 .desc = {
440 .name = "timestamp",
441 .type = QEMU_OPT_BOOL,
443 { /* end of list */ }
447 static QemuOptsList qemu_name_opts = {
448 .name = "name",
449 .implied_opt_name = "guest",
450 .merge_lists = true,
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
452 .desc = {
454 .name = "guest",
455 .type = QEMU_OPT_STRING,
456 .help = "Sets the name of the guest.\n"
457 "This name will be displayed in the SDL window caption.\n"
458 "The name will also be used for the VNC server",
459 }, {
460 .name = "process",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets the name of the QEMU process, as shown in top etc",
463 }, {
464 .name = "debug-threads",
465 .type = QEMU_OPT_BOOL,
466 .help = "When enabled, name the individual threads; defaults off.\n"
467 "NOTE: The thread names are for debugging and not a\n"
468 "stable API.",
470 { /* End of list */ }
474 static QemuOptsList qemu_mem_opts = {
475 .name = "memory",
476 .implied_opt_name = "size",
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
478 .merge_lists = true,
479 .desc = {
481 .name = "size",
482 .type = QEMU_OPT_SIZE,
485 .name = "slots",
486 .type = QEMU_OPT_NUMBER,
489 .name = "maxmem",
490 .type = QEMU_OPT_SIZE,
492 { /* end of list */ }
496 static QemuOptsList qemu_icount_opts = {
497 .name = "icount",
498 .implied_opt_name = "shift",
499 .merge_lists = true,
500 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
501 .desc = {
503 .name = "shift",
504 .type = QEMU_OPT_STRING,
505 }, {
506 .name = "align",
507 .type = QEMU_OPT_BOOL,
508 }, {
509 .name = "sleep",
510 .type = QEMU_OPT_BOOL,
511 }, {
512 .name = "rr",
513 .type = QEMU_OPT_STRING,
514 }, {
515 .name = "rrfile",
516 .type = QEMU_OPT_STRING,
517 }, {
518 .name = "rrsnapshot",
519 .type = QEMU_OPT_STRING,
521 { /* end of list */ }
525 static QemuOptsList qemu_semihosting_config_opts = {
526 .name = "semihosting-config",
527 .implied_opt_name = "enable",
528 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
529 .desc = {
531 .name = "enable",
532 .type = QEMU_OPT_BOOL,
533 }, {
534 .name = "target",
535 .type = QEMU_OPT_STRING,
536 }, {
537 .name = "arg",
538 .type = QEMU_OPT_STRING,
540 { /* end of list */ }
544 static QemuOptsList qemu_fw_cfg_opts = {
545 .name = "fw_cfg",
546 .implied_opt_name = "name",
547 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
548 .desc = {
550 .name = "name",
551 .type = QEMU_OPT_STRING,
552 .help = "Sets the fw_cfg name of the blob to be inserted",
553 }, {
554 .name = "file",
555 .type = QEMU_OPT_STRING,
556 .help = "Sets the name of the file from which\n"
557 "the fw_cfg blob will be loaded",
558 }, {
559 .name = "string",
560 .type = QEMU_OPT_STRING,
561 .help = "Sets content of the blob to be inserted from a string",
563 { /* end of list */ }
568 * Get machine options
570 * Returns: machine options (never null).
572 QemuOpts *qemu_get_machine_opts(void)
574 return qemu_find_opts_singleton("machine");
577 const char *qemu_get_vm_name(void)
579 return qemu_name;
582 static void res_free(void)
584 g_free(boot_splash_filedata);
585 boot_splash_filedata = NULL;
588 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
590 const char *driver = qemu_opt_get(opts, "driver");
591 int i;
593 if (!driver)
594 return 0;
595 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
596 if (strcmp(default_list[i].driver, driver) != 0)
597 continue;
598 *(default_list[i].flag) = 0;
600 return 0;
603 /***********************************************************/
604 /* QEMU state */
606 static RunState current_run_state = RUN_STATE_PRELAUNCH;
608 /* We use RUN_STATE__MAX but any invalid value will do */
609 static RunState vmstop_requested = RUN_STATE__MAX;
610 static QemuMutex vmstop_lock;
612 typedef struct {
613 RunState from;
614 RunState to;
615 } RunStateTransition;
617 static const RunStateTransition runstate_transitions_def[] = {
618 /* from -> to */
619 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
620 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
621 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
624 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
625 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
626 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
628 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
629 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
630 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
631 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
633 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
634 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
636 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
637 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
638 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
640 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
641 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
645 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
647 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_PAUSED, RUN_STATE_COLO},
650 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
651 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
652 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
654 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
655 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
658 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
659 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
660 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
664 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
665 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_COLO, RUN_STATE_RUNNING },
669 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
670 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
671 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
672 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
673 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
674 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
675 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
676 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
677 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
678 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
679 { RUN_STATE_RUNNING, RUN_STATE_COLO},
681 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
683 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
684 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
685 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
687 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
688 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
689 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
690 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
691 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
692 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
694 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
695 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
696 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
697 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
699 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
700 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
701 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
703 { RUN_STATE__MAX, RUN_STATE__MAX },
706 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
708 bool runstate_check(RunState state)
710 return current_run_state == state;
713 bool runstate_store(char *str, size_t size)
715 const char *state = RunState_str(current_run_state);
716 size_t len = strlen(state) + 1;
718 if (len > size) {
719 return false;
721 memcpy(str, state, len);
722 return true;
725 static void runstate_init(void)
727 const RunStateTransition *p;
729 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
730 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
731 runstate_valid_transitions[p->from][p->to] = true;
734 qemu_mutex_init(&vmstop_lock);
737 /* This function will abort() on invalid state transitions */
738 void runstate_set(RunState new_state)
740 assert(new_state < RUN_STATE__MAX);
742 if (current_run_state == new_state) {
743 return;
746 if (!runstate_valid_transitions[current_run_state][new_state]) {
747 error_report("invalid runstate transition: '%s' -> '%s'",
748 RunState_str(current_run_state),
749 RunState_str(new_state));
750 abort();
752 trace_runstate_set(new_state);
753 current_run_state = new_state;
756 int runstate_is_running(void)
758 return runstate_check(RUN_STATE_RUNNING);
761 bool runstate_needs_reset(void)
763 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
764 runstate_check(RUN_STATE_SHUTDOWN);
767 StatusInfo *qmp_query_status(Error **errp)
769 StatusInfo *info = g_malloc0(sizeof(*info));
771 info->running = runstate_is_running();
772 info->singlestep = singlestep;
773 info->status = current_run_state;
775 return info;
778 bool qemu_vmstop_requested(RunState *r)
780 qemu_mutex_lock(&vmstop_lock);
781 *r = vmstop_requested;
782 vmstop_requested = RUN_STATE__MAX;
783 qemu_mutex_unlock(&vmstop_lock);
784 return *r < RUN_STATE__MAX;
787 void qemu_system_vmstop_request_prepare(void)
789 qemu_mutex_lock(&vmstop_lock);
792 void qemu_system_vmstop_request(RunState state)
794 vmstop_requested = state;
795 qemu_mutex_unlock(&vmstop_lock);
796 qemu_notify_event();
799 /***********************************************************/
800 /* real time host monotonic timer */
802 static time_t qemu_time(void)
804 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
807 /***********************************************************/
808 /* host time/date access */
809 void qemu_get_timedate(struct tm *tm, int offset)
811 time_t ti = qemu_time();
813 ti += offset;
814 if (rtc_date_offset == -1) {
815 if (rtc_utc)
816 gmtime_r(&ti, tm);
817 else
818 localtime_r(&ti, tm);
819 } else {
820 ti -= rtc_date_offset;
821 gmtime_r(&ti, tm);
825 int qemu_timedate_diff(struct tm *tm)
827 time_t seconds;
829 if (rtc_date_offset == -1)
830 if (rtc_utc)
831 seconds = mktimegm(tm);
832 else {
833 struct tm tmp = *tm;
834 tmp.tm_isdst = -1; /* use timezone to figure it out */
835 seconds = mktime(&tmp);
837 else
838 seconds = mktimegm(tm) + rtc_date_offset;
840 return seconds - qemu_time();
843 static void configure_rtc_date_offset(const char *startdate, int legacy)
845 time_t rtc_start_date;
846 struct tm tm;
848 if (!strcmp(startdate, "now") && legacy) {
849 rtc_date_offset = -1;
850 } else {
851 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
852 &tm.tm_year,
853 &tm.tm_mon,
854 &tm.tm_mday,
855 &tm.tm_hour,
856 &tm.tm_min,
857 &tm.tm_sec) == 6) {
858 /* OK */
859 } else if (sscanf(startdate, "%d-%d-%d",
860 &tm.tm_year,
861 &tm.tm_mon,
862 &tm.tm_mday) == 3) {
863 tm.tm_hour = 0;
864 tm.tm_min = 0;
865 tm.tm_sec = 0;
866 } else {
867 goto date_fail;
869 tm.tm_year -= 1900;
870 tm.tm_mon--;
871 rtc_start_date = mktimegm(&tm);
872 if (rtc_start_date == -1) {
873 date_fail:
874 error_report("invalid date format");
875 error_printf("valid formats: "
876 "'2006-06-17T16:01:21' or '2006-06-17'\n");
877 exit(1);
879 rtc_date_offset = qemu_time() - rtc_start_date;
883 static void configure_rtc(QemuOpts *opts)
885 const char *value;
887 value = qemu_opt_get(opts, "base");
888 if (value) {
889 if (!strcmp(value, "utc")) {
890 rtc_utc = 1;
891 } else if (!strcmp(value, "localtime")) {
892 Error *blocker = NULL;
893 rtc_utc = 0;
894 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
895 "-rtc base=localtime");
896 replay_add_blocker(blocker);
897 } else {
898 configure_rtc_date_offset(value, 0);
901 value = qemu_opt_get(opts, "clock");
902 if (value) {
903 if (!strcmp(value, "host")) {
904 rtc_clock = QEMU_CLOCK_HOST;
905 } else if (!strcmp(value, "rt")) {
906 rtc_clock = QEMU_CLOCK_REALTIME;
907 } else if (!strcmp(value, "vm")) {
908 rtc_clock = QEMU_CLOCK_VIRTUAL;
909 } else {
910 error_report("invalid option value '%s'", value);
911 exit(1);
914 value = qemu_opt_get(opts, "driftfix");
915 if (value) {
916 if (!strcmp(value, "slew")) {
917 static GlobalProperty slew_lost_ticks = {
918 .driver = "mc146818rtc",
919 .property = "lost_tick_policy",
920 .value = "slew",
923 qdev_prop_register_global(&slew_lost_ticks);
924 } else if (!strcmp(value, "none")) {
925 /* discard is default */
926 } else {
927 error_report("invalid option value '%s'", value);
928 exit(1);
933 /***********************************************************/
934 /* Bluetooth support */
935 static int nb_hcis;
936 static int cur_hci;
937 static struct HCIInfo *hci_table[MAX_NICS];
939 struct HCIInfo *qemu_next_hci(void)
941 if (cur_hci == nb_hcis)
942 return &null_hci;
944 return hci_table[cur_hci++];
947 static int bt_hci_parse(const char *str)
949 struct HCIInfo *hci;
950 bdaddr_t bdaddr;
952 if (nb_hcis >= MAX_NICS) {
953 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
954 return -1;
957 hci = hci_init(str);
958 if (!hci)
959 return -1;
961 bdaddr.b[0] = 0x52;
962 bdaddr.b[1] = 0x54;
963 bdaddr.b[2] = 0x00;
964 bdaddr.b[3] = 0x12;
965 bdaddr.b[4] = 0x34;
966 bdaddr.b[5] = 0x56 + nb_hcis;
967 hci->bdaddr_set(hci, bdaddr.b);
969 hci_table[nb_hcis++] = hci;
971 return 0;
974 static void bt_vhci_add(int vlan_id)
976 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
978 if (!vlan->slave)
979 warn_report("adding a VHCI to an empty scatternet %i",
980 vlan_id);
982 bt_vhci_init(bt_new_hci(vlan));
985 static struct bt_device_s *bt_device_add(const char *opt)
987 struct bt_scatternet_s *vlan;
988 int vlan_id = 0;
989 char *endp = strstr(opt, ",vlan=");
990 int len = (endp ? endp - opt : strlen(opt)) + 1;
991 char devname[10];
993 pstrcpy(devname, MIN(sizeof(devname), len), opt);
995 if (endp) {
996 vlan_id = strtol(endp + 6, &endp, 0);
997 if (*endp) {
998 error_report("unrecognised bluetooth vlan Id");
999 return 0;
1003 vlan = qemu_find_bt_vlan(vlan_id);
1005 if (!vlan->slave)
1006 warn_report("adding a slave device to an empty scatternet %i",
1007 vlan_id);
1009 if (!strcmp(devname, "keyboard"))
1010 return bt_keyboard_init(vlan);
1012 error_report("unsupported bluetooth device '%s'", devname);
1013 return 0;
1016 static int bt_parse(const char *opt)
1018 const char *endp, *p;
1019 int vlan;
1021 if (strstart(opt, "hci", &endp)) {
1022 if (!*endp || *endp == ',') {
1023 if (*endp)
1024 if (!strstart(endp, ",vlan=", 0))
1025 opt = endp + 1;
1027 return bt_hci_parse(opt);
1029 } else if (strstart(opt, "vhci", &endp)) {
1030 if (!*endp || *endp == ',') {
1031 if (*endp) {
1032 if (strstart(endp, ",vlan=", &p)) {
1033 vlan = strtol(p, (char **) &endp, 0);
1034 if (*endp) {
1035 error_report("bad scatternet '%s'", p);
1036 return 1;
1038 } else {
1039 error_report("bad parameter '%s'", endp + 1);
1040 return 1;
1042 } else
1043 vlan = 0;
1045 bt_vhci_add(vlan);
1046 return 0;
1048 } else if (strstart(opt, "device:", &endp))
1049 return !bt_device_add(endp);
1051 error_report("bad bluetooth parameter '%s'", opt);
1052 return 1;
1055 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1057 if (qemu_opt_get_bool(opts, "enable", false)) {
1058 #ifdef CONFIG_SECCOMP
1059 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1060 | QEMU_SECCOMP_SET_OBSOLETE;
1061 const char *value = NULL;
1063 value = qemu_opt_get(opts, "obsolete");
1064 if (value) {
1065 if (g_str_equal(value, "allow")) {
1066 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1067 } else if (g_str_equal(value, "deny")) {
1068 /* this is the default option, this if is here
1069 * to provide a little bit of consistency for
1070 * the command line */
1071 } else {
1072 error_report("invalid argument for obsolete");
1073 return -1;
1077 value = qemu_opt_get(opts, "elevateprivileges");
1078 if (value) {
1079 if (g_str_equal(value, "deny")) {
1080 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1081 } else if (g_str_equal(value, "children")) {
1082 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1084 /* calling prctl directly because we're
1085 * not sure if host has CAP_SYS_ADMIN set*/
1086 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1087 error_report("failed to set no_new_privs "
1088 "aborting");
1089 return -1;
1091 } else if (g_str_equal(value, "allow")) {
1092 /* default value */
1093 } else {
1094 error_report("invalid argument for elevateprivileges");
1095 return -1;
1099 value = qemu_opt_get(opts, "spawn");
1100 if (value) {
1101 if (g_str_equal(value, "deny")) {
1102 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1103 } else if (g_str_equal(value, "allow")) {
1104 /* default value */
1105 } else {
1106 error_report("invalid argument for spawn");
1107 return -1;
1111 value = qemu_opt_get(opts, "resourcecontrol");
1112 if (value) {
1113 if (g_str_equal(value, "deny")) {
1114 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1115 } else if (g_str_equal(value, "allow")) {
1116 /* default value */
1117 } else {
1118 error_report("invalid argument for resourcecontrol");
1119 return -1;
1123 if (seccomp_start(seccomp_opts) < 0) {
1124 error_report("failed to install seccomp syscall filter "
1125 "in the kernel");
1126 return -1;
1128 #else
1129 error_report("seccomp support is disabled");
1130 return -1;
1131 #endif
1134 return 0;
1137 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1139 const char *proc_name;
1141 if (qemu_opt_get(opts, "debug-threads")) {
1142 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1144 qemu_name = qemu_opt_get(opts, "guest");
1146 proc_name = qemu_opt_get(opts, "process");
1147 if (proc_name) {
1148 os_set_proc_name(proc_name);
1151 return 0;
1154 bool defaults_enabled(void)
1156 return has_defaults;
1159 #ifndef _WIN32
1160 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1162 int fd, dupfd, flags;
1163 int64_t fdset_id;
1164 const char *fd_opaque = NULL;
1165 AddfdInfo *fdinfo;
1167 fd = qemu_opt_get_number(opts, "fd", -1);
1168 fdset_id = qemu_opt_get_number(opts, "set", -1);
1169 fd_opaque = qemu_opt_get(opts, "opaque");
1171 if (fd < 0) {
1172 error_report("fd option is required and must be non-negative");
1173 return -1;
1176 if (fd <= STDERR_FILENO) {
1177 error_report("fd cannot be a standard I/O stream");
1178 return -1;
1182 * All fds inherited across exec() necessarily have FD_CLOEXEC
1183 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1185 flags = fcntl(fd, F_GETFD);
1186 if (flags == -1 || (flags & FD_CLOEXEC)) {
1187 error_report("fd is not valid or already in use");
1188 return -1;
1191 if (fdset_id < 0) {
1192 error_report("set option is required and must be non-negative");
1193 return -1;
1196 #ifdef F_DUPFD_CLOEXEC
1197 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1198 #else
1199 dupfd = dup(fd);
1200 if (dupfd != -1) {
1201 qemu_set_cloexec(dupfd);
1203 #endif
1204 if (dupfd == -1) {
1205 error_report("error duplicating fd: %s", strerror(errno));
1206 return -1;
1209 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1210 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1211 &error_abort);
1212 g_free(fdinfo);
1214 return 0;
1217 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1219 int fd;
1221 fd = qemu_opt_get_number(opts, "fd", -1);
1222 close(fd);
1224 return 0;
1226 #endif
1228 /***********************************************************/
1229 /* QEMU Block devices */
1231 #define HD_OPTS "media=disk"
1232 #define CDROM_OPTS "media=cdrom"
1233 #define FD_OPTS ""
1234 #define PFLASH_OPTS ""
1235 #define MTD_OPTS ""
1236 #define SD_OPTS ""
1238 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1240 BlockInterfaceType *block_default_type = opaque;
1242 return drive_new(opts, *block_default_type) == NULL;
1245 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1247 if (qemu_opt_get(opts, "snapshot") == NULL) {
1248 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1250 return 0;
1253 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1254 int index, const char *optstr)
1256 QemuOpts *opts;
1257 DriveInfo *dinfo;
1259 if (!enable || drive_get_by_index(type, index)) {
1260 return;
1263 opts = drive_add(type, index, NULL, optstr);
1264 if (snapshot) {
1265 drive_enable_snapshot(NULL, opts, NULL);
1268 dinfo = drive_new(opts, type);
1269 if (!dinfo) {
1270 exit(1);
1272 dinfo->is_default = true;
1276 static QemuOptsList qemu_smp_opts = {
1277 .name = "smp-opts",
1278 .implied_opt_name = "cpus",
1279 .merge_lists = true,
1280 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1281 .desc = {
1283 .name = "cpus",
1284 .type = QEMU_OPT_NUMBER,
1285 }, {
1286 .name = "sockets",
1287 .type = QEMU_OPT_NUMBER,
1288 }, {
1289 .name = "cores",
1290 .type = QEMU_OPT_NUMBER,
1291 }, {
1292 .name = "threads",
1293 .type = QEMU_OPT_NUMBER,
1294 }, {
1295 .name = "maxcpus",
1296 .type = QEMU_OPT_NUMBER,
1298 { /*End of list */ }
1302 static void smp_parse(QemuOpts *opts)
1304 if (opts) {
1305 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1306 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1307 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1308 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1310 /* compute missing values, prefer sockets over cores over threads */
1311 if (cpus == 0 || sockets == 0) {
1312 sockets = sockets > 0 ? sockets : 1;
1313 cores = cores > 0 ? cores : 1;
1314 threads = threads > 0 ? threads : 1;
1315 if (cpus == 0) {
1316 cpus = cores * threads * sockets;
1318 } else if (cores == 0) {
1319 threads = threads > 0 ? threads : 1;
1320 cores = cpus / (sockets * threads);
1321 cores = cores > 0 ? cores : 1;
1322 } else if (threads == 0) {
1323 threads = cpus / (cores * sockets);
1324 threads = threads > 0 ? threads : 1;
1325 } else if (sockets * cores * threads < cpus) {
1326 error_report("cpu topology: "
1327 "sockets (%u) * cores (%u) * threads (%u) < "
1328 "smp_cpus (%u)",
1329 sockets, cores, threads, cpus);
1330 exit(1);
1333 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1335 if (max_cpus < cpus) {
1336 error_report("maxcpus must be equal to or greater than smp");
1337 exit(1);
1340 if (sockets * cores * threads > max_cpus) {
1341 error_report("cpu topology: "
1342 "sockets (%u) * cores (%u) * threads (%u) > "
1343 "maxcpus (%u)",
1344 sockets, cores, threads, max_cpus);
1345 exit(1);
1348 smp_cpus = cpus;
1349 smp_cores = cores;
1350 smp_threads = threads;
1353 if (smp_cpus > 1) {
1354 Error *blocker = NULL;
1355 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1356 replay_add_blocker(blocker);
1360 static void realtime_init(void)
1362 if (enable_mlock) {
1363 if (os_mlock() < 0) {
1364 error_report("locking memory failed");
1365 exit(1);
1371 static void configure_msg(QemuOpts *opts)
1373 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1376 /***********************************************************/
1377 /* Semihosting */
1379 typedef struct SemihostingConfig {
1380 bool enabled;
1381 SemihostingTarget target;
1382 const char **argv;
1383 int argc;
1384 const char *cmdline; /* concatenated argv */
1385 } SemihostingConfig;
1387 static SemihostingConfig semihosting;
1389 bool semihosting_enabled(void)
1391 return semihosting.enabled;
1394 SemihostingTarget semihosting_get_target(void)
1396 return semihosting.target;
1399 const char *semihosting_get_arg(int i)
1401 if (i >= semihosting.argc) {
1402 return NULL;
1404 return semihosting.argv[i];
1407 int semihosting_get_argc(void)
1409 return semihosting.argc;
1412 const char *semihosting_get_cmdline(void)
1414 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1415 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1417 return semihosting.cmdline;
1420 static int add_semihosting_arg(void *opaque,
1421 const char *name, const char *val,
1422 Error **errp)
1424 SemihostingConfig *s = opaque;
1425 if (strcmp(name, "arg") == 0) {
1426 s->argc++;
1427 /* one extra element as g_strjoinv() expects NULL-terminated array */
1428 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1429 s->argv[s->argc - 1] = val;
1430 s->argv[s->argc] = NULL;
1432 return 0;
1435 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1436 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1438 char *cmd_token;
1440 /* argv[0] */
1441 add_semihosting_arg(&semihosting, "arg", file, NULL);
1443 /* split -append and initialize argv[1..n] */
1444 cmd_token = strtok(g_strdup(cmd), " ");
1445 while (cmd_token) {
1446 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1447 cmd_token = strtok(NULL, " ");
1451 /* Now we still need this for compatibility with XEN. */
1452 bool has_igd_gfx_passthru;
1453 static void igd_gfx_passthru(void)
1455 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1458 /***********************************************************/
1459 /* USB devices */
1461 static int usb_device_add(const char *devname)
1463 USBDevice *dev = NULL;
1465 if (!machine_usb(current_machine)) {
1466 return -1;
1469 dev = usbdevice_create(devname);
1470 if (!dev)
1471 return -1;
1473 return 0;
1476 static int usb_parse(const char *cmdline)
1478 int r;
1479 r = usb_device_add(cmdline);
1480 if (r < 0) {
1481 error_report("could not add USB device '%s'", cmdline);
1483 return r;
1486 /***********************************************************/
1487 /* machine registration */
1489 MachineState *current_machine;
1491 static MachineClass *find_machine(const char *name)
1493 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1494 MachineClass *mc = NULL;
1496 for (el = machines; el; el = el->next) {
1497 MachineClass *temp = el->data;
1499 if (!strcmp(temp->name, name)) {
1500 mc = temp;
1501 break;
1503 if (temp->alias &&
1504 !strcmp(temp->alias, name)) {
1505 mc = temp;
1506 break;
1510 g_slist_free(machines);
1511 return mc;
1514 MachineClass *find_default_machine(void)
1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1517 MachineClass *mc = NULL;
1519 for (el = machines; el; el = el->next) {
1520 MachineClass *temp = el->data;
1522 if (temp->is_default) {
1523 mc = temp;
1524 break;
1528 g_slist_free(machines);
1529 return mc;
1532 MachineInfoList *qmp_query_machines(Error **errp)
1534 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1535 MachineInfoList *mach_list = NULL;
1537 for (el = machines; el; el = el->next) {
1538 MachineClass *mc = el->data;
1539 MachineInfoList *entry;
1540 MachineInfo *info;
1542 info = g_malloc0(sizeof(*info));
1543 if (mc->is_default) {
1544 info->has_is_default = true;
1545 info->is_default = true;
1548 if (mc->alias) {
1549 info->has_alias = true;
1550 info->alias = g_strdup(mc->alias);
1553 info->name = g_strdup(mc->name);
1554 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1555 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1557 entry = g_malloc0(sizeof(*entry));
1558 entry->value = info;
1559 entry->next = mach_list;
1560 mach_list = entry;
1563 g_slist_free(machines);
1564 return mach_list;
1567 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1569 ObjectProperty *prop;
1570 ObjectPropertyIterator iter;
1572 if (!qemu_opt_has_help_opt(opts)) {
1573 return 0;
1576 object_property_iter_init(&iter, OBJECT(machine));
1577 while ((prop = object_property_iter_next(&iter))) {
1578 if (!prop->set) {
1579 continue;
1582 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1583 prop->name, prop->type);
1584 if (prop->description) {
1585 error_printf(" (%s)\n", prop->description);
1586 } else {
1587 error_printf("\n");
1591 return 1;
1594 /***********************************************************/
1595 /* main execution loop */
1597 struct vm_change_state_entry {
1598 VMChangeStateHandler *cb;
1599 void *opaque;
1600 QLIST_ENTRY (vm_change_state_entry) entries;
1603 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1605 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1606 void *opaque)
1608 VMChangeStateEntry *e;
1610 e = g_malloc0(sizeof (*e));
1612 e->cb = cb;
1613 e->opaque = opaque;
1614 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1615 return e;
1618 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1620 QLIST_REMOVE (e, entries);
1621 g_free (e);
1624 void vm_state_notify(int running, RunState state)
1626 VMChangeStateEntry *e, *next;
1628 trace_vm_state_notify(running, state);
1630 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1631 e->cb(e->opaque, running, state);
1635 static ShutdownCause reset_requested;
1636 static ShutdownCause shutdown_requested;
1637 static int shutdown_signal;
1638 static pid_t shutdown_pid;
1639 static int powerdown_requested;
1640 static int debug_requested;
1641 static int suspend_requested;
1642 static WakeupReason wakeup_reason;
1643 static NotifierList powerdown_notifiers =
1644 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1645 static NotifierList suspend_notifiers =
1646 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1647 static NotifierList wakeup_notifiers =
1648 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1649 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1651 ShutdownCause qemu_shutdown_requested_get(void)
1653 return shutdown_requested;
1656 ShutdownCause qemu_reset_requested_get(void)
1658 return reset_requested;
1661 static int qemu_shutdown_requested(void)
1663 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1666 static void qemu_kill_report(void)
1668 if (!qtest_driver() && shutdown_signal) {
1669 if (shutdown_pid == 0) {
1670 /* This happens for eg ^C at the terminal, so it's worth
1671 * avoiding printing an odd message in that case.
1673 error_report("terminating on signal %d", shutdown_signal);
1674 } else {
1675 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1677 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1678 shutdown_signal, shutdown_pid,
1679 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1680 g_free(shutdown_cmd);
1682 shutdown_signal = 0;
1686 static ShutdownCause qemu_reset_requested(void)
1688 ShutdownCause r = reset_requested;
1690 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1691 reset_requested = SHUTDOWN_CAUSE_NONE;
1692 return r;
1694 return SHUTDOWN_CAUSE_NONE;
1697 static int qemu_suspend_requested(void)
1699 int r = suspend_requested;
1700 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1701 suspend_requested = 0;
1702 return r;
1704 return false;
1707 static WakeupReason qemu_wakeup_requested(void)
1709 return wakeup_reason;
1712 static int qemu_powerdown_requested(void)
1714 int r = powerdown_requested;
1715 powerdown_requested = 0;
1716 return r;
1719 static int qemu_debug_requested(void)
1721 int r = debug_requested;
1722 debug_requested = 0;
1723 return r;
1727 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1729 void qemu_system_reset(ShutdownCause reason)
1731 MachineClass *mc;
1733 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1735 cpu_synchronize_all_states();
1737 if (mc && mc->reset) {
1738 mc->reset();
1739 } else {
1740 qemu_devices_reset();
1742 if (reason) {
1743 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1744 &error_abort);
1746 cpu_synchronize_all_post_reset();
1749 void qemu_system_guest_panicked(GuestPanicInformation *info)
1751 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed");
1753 if (current_cpu) {
1754 current_cpu->crash_occurred = true;
1756 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1757 !!info, info, &error_abort);
1758 vm_stop(RUN_STATE_GUEST_PANICKED);
1759 if (!no_shutdown) {
1760 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1761 !!info, info, &error_abort);
1762 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1765 if (info) {
1766 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1767 qemu_log_mask(LOG_GUEST_ERROR, "\nHV crash parameters: (%#"PRIx64
1768 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1769 info->u.hyper_v.arg1,
1770 info->u.hyper_v.arg2,
1771 info->u.hyper_v.arg3,
1772 info->u.hyper_v.arg4,
1773 info->u.hyper_v.arg5);
1774 } else if (info->type == GUEST_PANIC_INFORMATION_TYPE_S390) {
1775 qemu_log_mask(LOG_GUEST_ERROR, " on cpu %d: %s\n"
1776 "PSW: 0x%016" PRIx64 " 0x%016" PRIx64"\n",
1777 info->u.s390.core,
1778 S390CrashReason_str(info->u.s390.reason),
1779 info->u.s390.psw_mask,
1780 info->u.s390.psw_addr);
1782 qapi_free_GuestPanicInformation(info);
1786 void qemu_system_reset_request(ShutdownCause reason)
1788 if (no_reboot) {
1789 shutdown_requested = reason;
1790 } else {
1791 reset_requested = reason;
1793 cpu_stop_current();
1794 qemu_notify_event();
1797 static void qemu_system_suspend(void)
1799 pause_all_vcpus();
1800 notifier_list_notify(&suspend_notifiers, NULL);
1801 runstate_set(RUN_STATE_SUSPENDED);
1802 qapi_event_send_suspend(&error_abort);
1805 void qemu_system_suspend_request(void)
1807 if (runstate_check(RUN_STATE_SUSPENDED)) {
1808 return;
1810 suspend_requested = 1;
1811 cpu_stop_current();
1812 qemu_notify_event();
1815 void qemu_register_suspend_notifier(Notifier *notifier)
1817 notifier_list_add(&suspend_notifiers, notifier);
1820 void qemu_system_wakeup_request(WakeupReason reason)
1822 trace_system_wakeup_request(reason);
1824 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1825 return;
1827 if (!(wakeup_reason_mask & (1 << reason))) {
1828 return;
1830 runstate_set(RUN_STATE_RUNNING);
1831 wakeup_reason = reason;
1832 qemu_notify_event();
1835 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1837 if (enabled) {
1838 wakeup_reason_mask |= (1 << reason);
1839 } else {
1840 wakeup_reason_mask &= ~(1 << reason);
1844 void qemu_register_wakeup_notifier(Notifier *notifier)
1846 notifier_list_add(&wakeup_notifiers, notifier);
1849 void qemu_system_killed(int signal, pid_t pid)
1851 shutdown_signal = signal;
1852 shutdown_pid = pid;
1853 no_shutdown = 0;
1855 /* Cannot call qemu_system_shutdown_request directly because
1856 * we are in a signal handler.
1858 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1859 qemu_notify_event();
1862 void qemu_system_shutdown_request(ShutdownCause reason)
1864 trace_qemu_system_shutdown_request(reason);
1865 replay_shutdown_request(reason);
1866 shutdown_requested = reason;
1867 qemu_notify_event();
1870 static void qemu_system_powerdown(void)
1872 qapi_event_send_powerdown(&error_abort);
1873 notifier_list_notify(&powerdown_notifiers, NULL);
1876 void qemu_system_powerdown_request(void)
1878 trace_qemu_system_powerdown_request();
1879 powerdown_requested = 1;
1880 qemu_notify_event();
1883 void qemu_register_powerdown_notifier(Notifier *notifier)
1885 notifier_list_add(&powerdown_notifiers, notifier);
1888 void qemu_system_debug_request(void)
1890 debug_requested = 1;
1891 qemu_notify_event();
1894 static bool main_loop_should_exit(void)
1896 RunState r;
1897 ShutdownCause request;
1899 if (qemu_debug_requested()) {
1900 vm_stop(RUN_STATE_DEBUG);
1902 if (qemu_suspend_requested()) {
1903 qemu_system_suspend();
1905 request = qemu_shutdown_requested();
1906 if (request) {
1907 qemu_kill_report();
1908 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1909 &error_abort);
1910 if (no_shutdown) {
1911 vm_stop(RUN_STATE_SHUTDOWN);
1912 } else {
1913 return true;
1916 request = qemu_reset_requested();
1917 if (request) {
1918 pause_all_vcpus();
1919 qemu_system_reset(request);
1920 resume_all_vcpus();
1921 if (!runstate_check(RUN_STATE_RUNNING) &&
1922 !runstate_check(RUN_STATE_INMIGRATE)) {
1923 runstate_set(RUN_STATE_PRELAUNCH);
1926 if (qemu_wakeup_requested()) {
1927 pause_all_vcpus();
1928 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1929 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1930 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1931 resume_all_vcpus();
1932 qapi_event_send_wakeup(&error_abort);
1934 if (qemu_powerdown_requested()) {
1935 qemu_system_powerdown();
1937 if (qemu_vmstop_requested(&r)) {
1938 vm_stop(r);
1940 return false;
1943 static void main_loop(void)
1945 #ifdef CONFIG_PROFILER
1946 int64_t ti;
1947 #endif
1948 do {
1949 #ifdef CONFIG_PROFILER
1950 ti = profile_getclock();
1951 #endif
1952 main_loop_wait(false);
1953 #ifdef CONFIG_PROFILER
1954 dev_time += profile_getclock() - ti;
1955 #endif
1956 } while (!main_loop_should_exit());
1959 static void version(void)
1961 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
1962 QEMU_COPYRIGHT "\n");
1965 static void QEMU_NORETURN help(int exitcode)
1967 version();
1968 printf("usage: %s [options] [disk_image]\n\n"
1969 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1970 error_get_progname());
1972 #define QEMU_OPTIONS_GENERATE_HELP
1973 #include "qemu-options-wrapper.h"
1975 printf("\nDuring emulation, the following keys are useful:\n"
1976 "ctrl-alt-f toggle full screen\n"
1977 "ctrl-alt-n switch to virtual console 'n'\n"
1978 "ctrl-alt toggle mouse and keyboard grab\n"
1979 "\n"
1980 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1981 "\n"
1982 QEMU_HELP_BOTTOM "\n");
1984 exit(exitcode);
1987 #define HAS_ARG 0x0001
1989 typedef struct QEMUOption {
1990 const char *name;
1991 int flags;
1992 int index;
1993 uint32_t arch_mask;
1994 } QEMUOption;
1996 static const QEMUOption qemu_options[] = {
1997 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1998 #define QEMU_OPTIONS_GENERATE_OPTIONS
1999 #include "qemu-options-wrapper.h"
2000 { NULL },
2003 typedef struct VGAInterfaceInfo {
2004 const char *opt_name; /* option name */
2005 const char *name; /* human-readable name */
2006 /* Class names indicating that support is available.
2007 * If no class is specified, the interface is always available */
2008 const char *class_names[2];
2009 } VGAInterfaceInfo;
2011 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2012 [VGA_NONE] = {
2013 .opt_name = "none",
2015 [VGA_STD] = {
2016 .opt_name = "std",
2017 .name = "standard VGA",
2018 .class_names = { "VGA", "isa-vga" },
2020 [VGA_CIRRUS] = {
2021 .opt_name = "cirrus",
2022 .name = "Cirrus VGA",
2023 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2025 [VGA_VMWARE] = {
2026 .opt_name = "vmware",
2027 .name = "VMWare SVGA",
2028 .class_names = { "vmware-svga" },
2030 [VGA_VIRTIO] = {
2031 .opt_name = "virtio",
2032 .name = "Virtio VGA",
2033 .class_names = { "virtio-vga" },
2035 [VGA_QXL] = {
2036 .opt_name = "qxl",
2037 .name = "QXL VGA",
2038 .class_names = { "qxl-vga" },
2040 [VGA_TCX] = {
2041 .opt_name = "tcx",
2042 .name = "TCX framebuffer",
2043 .class_names = { "SUNW,tcx" },
2045 [VGA_CG3] = {
2046 .opt_name = "cg3",
2047 .name = "CG3 framebuffer",
2048 .class_names = { "cgthree" },
2050 [VGA_XENFB] = {
2051 .opt_name = "xenfb",
2055 static bool vga_interface_available(VGAInterfaceType t)
2057 VGAInterfaceInfo *ti = &vga_interfaces[t];
2059 assert(t < VGA_TYPE_MAX);
2060 return !ti->class_names[0] ||
2061 object_class_by_name(ti->class_names[0]) ||
2062 object_class_by_name(ti->class_names[1]);
2065 static void select_vgahw(const char *p)
2067 const char *opts;
2068 int t;
2070 assert(vga_interface_type == VGA_NONE);
2071 for (t = 0; t < VGA_TYPE_MAX; t++) {
2072 VGAInterfaceInfo *ti = &vga_interfaces[t];
2073 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2074 if (!vga_interface_available(t)) {
2075 error_report("%s not available", ti->name);
2076 exit(1);
2078 vga_interface_type = t;
2079 break;
2082 if (t == VGA_TYPE_MAX) {
2083 invalid_vga:
2084 error_report("unknown vga type: %s", p);
2085 exit(1);
2087 while (*opts) {
2088 const char *nextopt;
2090 if (strstart(opts, ",retrace=", &nextopt)) {
2091 opts = nextopt;
2092 if (strstart(opts, "dumb", &nextopt))
2093 vga_retrace_method = VGA_RETRACE_DUMB;
2094 else if (strstart(opts, "precise", &nextopt))
2095 vga_retrace_method = VGA_RETRACE_PRECISE;
2096 else goto invalid_vga;
2097 } else goto invalid_vga;
2098 opts = nextopt;
2102 static void parse_display(const char *p)
2104 const char *opts;
2106 if (strstart(p, "sdl", &opts)) {
2107 dpy.type = DISPLAY_TYPE_SDL;
2108 while (*opts) {
2109 const char *nextopt;
2111 if (strstart(opts, ",frame=", &nextopt)) {
2112 g_printerr("The frame= sdl option is deprecated, and will be\n"
2113 "removed in a future release.\n");
2114 opts = nextopt;
2115 if (strstart(opts, "on", &nextopt)) {
2116 no_frame = 0;
2117 } else if (strstart(opts, "off", &nextopt)) {
2118 no_frame = 1;
2119 } else {
2120 goto invalid_sdl_args;
2122 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2123 opts = nextopt;
2124 if (strstart(opts, "on", &nextopt)) {
2125 alt_grab = 1;
2126 } else if (strstart(opts, "off", &nextopt)) {
2127 alt_grab = 0;
2128 } else {
2129 goto invalid_sdl_args;
2131 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2132 opts = nextopt;
2133 if (strstart(opts, "on", &nextopt)) {
2134 ctrl_grab = 1;
2135 } else if (strstart(opts, "off", &nextopt)) {
2136 ctrl_grab = 0;
2137 } else {
2138 goto invalid_sdl_args;
2140 } else if (strstart(opts, ",window_close=", &nextopt)) {
2141 opts = nextopt;
2142 dpy.has_window_close = true;
2143 if (strstart(opts, "on", &nextopt)) {
2144 dpy.window_close = true;
2145 } else if (strstart(opts, "off", &nextopt)) {
2146 dpy.window_close = false;
2147 } else {
2148 goto invalid_sdl_args;
2150 } else if (strstart(opts, ",gl=", &nextopt)) {
2151 opts = nextopt;
2152 dpy.has_gl = true;
2153 if (strstart(opts, "on", &nextopt)) {
2154 dpy.gl = true;
2155 } else if (strstart(opts, "off", &nextopt)) {
2156 dpy.gl = false;
2157 } else {
2158 goto invalid_sdl_args;
2160 } else {
2161 invalid_sdl_args:
2162 error_report("invalid SDL option string");
2163 exit(1);
2165 opts = nextopt;
2167 } else if (strstart(p, "vnc", &opts)) {
2168 if (*opts == '=') {
2169 vnc_parse(opts + 1, &error_fatal);
2170 } else {
2171 error_report("VNC requires a display argument vnc=<display>");
2172 exit(1);
2174 } else if (strstart(p, "egl-headless", &opts)) {
2175 dpy.type = DISPLAY_TYPE_EGL_HEADLESS;
2176 } else if (strstart(p, "curses", &opts)) {
2177 dpy.type = DISPLAY_TYPE_CURSES;
2178 } else if (strstart(p, "gtk", &opts)) {
2179 dpy.type = DISPLAY_TYPE_GTK;
2180 while (*opts) {
2181 const char *nextopt;
2183 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2184 opts = nextopt;
2185 dpy.u.gtk.has_grab_on_hover = true;
2186 if (strstart(opts, "on", &nextopt)) {
2187 dpy.u.gtk.grab_on_hover = true;
2188 } else if (strstart(opts, "off", &nextopt)) {
2189 dpy.u.gtk.grab_on_hover = false;
2190 } else {
2191 goto invalid_gtk_args;
2193 } else if (strstart(opts, ",gl=", &nextopt)) {
2194 opts = nextopt;
2195 dpy.has_gl = true;
2196 if (strstart(opts, "on", &nextopt)) {
2197 dpy.gl = true;
2198 } else if (strstart(opts, "off", &nextopt)) {
2199 dpy.gl = false;
2200 } else {
2201 goto invalid_gtk_args;
2203 } else {
2204 invalid_gtk_args:
2205 error_report("invalid GTK option string");
2206 exit(1);
2208 opts = nextopt;
2210 } else if (strstart(p, "none", &opts)) {
2211 dpy.type = DISPLAY_TYPE_NONE;
2212 } else {
2213 error_report("unknown display type");
2214 exit(1);
2218 static int balloon_parse(const char *arg)
2220 QemuOpts *opts;
2222 warn_report("This option is deprecated. "
2223 "Use '--device virtio-balloon' to enable the balloon device.");
2225 if (strcmp(arg, "none") == 0) {
2226 return 0;
2229 if (!strncmp(arg, "virtio", 6)) {
2230 if (arg[6] == ',') {
2231 /* have params -> parse them */
2232 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2233 false);
2234 if (!opts)
2235 return -1;
2236 } else {
2237 /* create empty opts */
2238 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2239 &error_abort);
2241 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2242 return 0;
2245 return -1;
2248 #if defined(CONFIG_RUBY)
2249 /* Ruby interface for QEMU. See README.EXT for programming example. */
2250 #warning mit ruby
2251 #include <ruby.h>
2253 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2255 int i;
2256 for (i = 0; i < argc; i++) {
2257 argv[i] = StringValue(argv[i]);
2258 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2260 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2261 //~ mode = 0666; /* default value */
2262 //~ }
2263 return INT2FIX(argc);
2266 void Init_qemu(void)
2268 printf("%s\n", __func__);
2269 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2270 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2271 #if 0
2272 rb_define_method();
2273 #endif
2275 #endif /* CONFIG_RUBY */
2277 #if defined(CONFIG_DLL)
2278 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2280 return FALSE;
2282 #endif /* CONFIG_DLL */
2284 char *qemu_find_file(int type, const char *name)
2286 int i;
2287 const char *subdir;
2288 char *buf;
2290 /* Try the name as a straight path first */
2291 if (access(name, R_OK) == 0) {
2292 trace_load_file(name, name);
2293 return g_strdup(name);
2296 switch (type) {
2297 case QEMU_FILE_TYPE_BIOS:
2298 subdir = "";
2299 break;
2300 case QEMU_FILE_TYPE_KEYMAP:
2301 subdir = "keymaps/";
2302 break;
2303 default:
2304 abort();
2307 for (i = 0; i < data_dir_idx; i++) {
2308 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2309 if (access(buf, R_OK) == 0) {
2310 trace_load_file(name, buf);
2311 return buf;
2313 g_free(buf);
2315 return NULL;
2318 static void qemu_add_data_dir(const char *path)
2320 int i;
2322 if (path == NULL) {
2323 return;
2325 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2326 return;
2328 for (i = 0; i < data_dir_idx; i++) {
2329 if (strcmp(data_dir[i], path) == 0) {
2330 return; /* duplicate */
2333 data_dir[data_dir_idx++] = g_strdup(path);
2336 static inline bool nonempty_str(const char *str)
2338 return str && *str;
2341 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2343 gchar *buf;
2344 size_t size;
2345 const char *name, *file, *str;
2346 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2348 if (fw_cfg == NULL) {
2349 error_report("fw_cfg device not available");
2350 return -1;
2352 name = qemu_opt_get(opts, "name");
2353 file = qemu_opt_get(opts, "file");
2354 str = qemu_opt_get(opts, "string");
2356 /* we need name and either a file or the content string */
2357 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2358 error_report("invalid argument(s)");
2359 return -1;
2361 if (nonempty_str(file) && nonempty_str(str)) {
2362 error_report("file and string are mutually exclusive");
2363 return -1;
2365 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2366 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2367 return -1;
2369 if (strncmp(name, "opt/", 4) != 0) {
2370 warn_report("externally provided fw_cfg item names "
2371 "should be prefixed with \"opt/\"");
2373 if (nonempty_str(str)) {
2374 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2375 buf = g_memdup(str, size);
2376 } else {
2377 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2378 error_report("can't load %s", file);
2379 return -1;
2382 /* For legacy, keep user files in a specific global order. */
2383 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2384 fw_cfg_add_file(fw_cfg, name, buf, size);
2385 fw_cfg_reset_order_override(fw_cfg);
2386 return 0;
2389 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2391 return qdev_device_help(opts);
2394 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2396 Error *err = NULL;
2397 DeviceState *dev;
2399 dev = qdev_device_add(opts, &err);
2400 if (!dev) {
2401 error_report_err(err);
2402 return -1;
2404 object_unref(OBJECT(dev));
2405 return 0;
2408 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2410 Error *local_err = NULL;
2412 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2413 if (local_err) {
2414 error_report_err(local_err);
2415 return -1;
2417 exit(0);
2419 return 0;
2422 #ifdef CONFIG_VIRTFS
2423 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2425 return qemu_fsdev_add(opts);
2427 #endif
2429 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2431 Chardev *chr;
2432 const char *chardev;
2433 const char *mode;
2434 int flags;
2436 mode = qemu_opt_get(opts, "mode");
2437 if (mode == NULL) {
2438 mode = "readline";
2440 if (strcmp(mode, "readline") == 0) {
2441 flags = MONITOR_USE_READLINE;
2442 } else if (strcmp(mode, "control") == 0) {
2443 flags = MONITOR_USE_CONTROL;
2444 } else {
2445 error_report("unknown monitor mode \"%s\"", mode);
2446 exit(1);
2449 if (qemu_opt_get_bool(opts, "pretty", 0))
2450 flags |= MONITOR_USE_PRETTY;
2452 chardev = qemu_opt_get(opts, "chardev");
2453 chr = qemu_chr_find(chardev);
2454 if (chr == NULL) {
2455 error_report("chardev \"%s\" not found", chardev);
2456 exit(1);
2459 monitor_init(chr, flags);
2460 return 0;
2463 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2465 static int monitor_device_index = 0;
2466 QemuOpts *opts;
2467 const char *p;
2468 char label[32];
2470 if (strstart(optarg, "chardev:", &p)) {
2471 snprintf(label, sizeof(label), "%s", p);
2472 } else {
2473 snprintf(label, sizeof(label), "compat_monitor%d",
2474 monitor_device_index);
2475 opts = qemu_chr_parse_compat(label, optarg);
2476 if (!opts) {
2477 error_report("parse error: %s", optarg);
2478 exit(1);
2482 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2483 qemu_opt_set(opts, "mode", mode, &error_abort);
2484 qemu_opt_set(opts, "chardev", label, &error_abort);
2485 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2486 monitor_device_index++;
2489 struct device_config {
2490 enum {
2491 DEV_USB, /* -usbdevice */
2492 DEV_BT, /* -bt */
2493 DEV_SERIAL, /* -serial */
2494 DEV_PARALLEL, /* -parallel */
2495 DEV_VIRTCON, /* -virtioconsole */
2496 DEV_DEBUGCON, /* -debugcon */
2497 DEV_GDB, /* -gdb, -s */
2498 DEV_SCLP, /* s390 sclp */
2499 } type;
2500 const char *cmdline;
2501 Location loc;
2502 QTAILQ_ENTRY(device_config) next;
2505 static QTAILQ_HEAD(, device_config) device_configs =
2506 QTAILQ_HEAD_INITIALIZER(device_configs);
2508 static void add_device_config(int type, const char *cmdline)
2510 struct device_config *conf;
2512 conf = g_malloc0(sizeof(*conf));
2513 conf->type = type;
2514 conf->cmdline = cmdline;
2515 loc_save(&conf->loc);
2516 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2519 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2521 struct device_config *conf;
2522 int rc;
2524 QTAILQ_FOREACH(conf, &device_configs, next) {
2525 if (conf->type != type)
2526 continue;
2527 loc_push_restore(&conf->loc);
2528 rc = func(conf->cmdline);
2529 loc_pop(&conf->loc);
2530 if (rc) {
2531 return rc;
2534 return 0;
2537 static int serial_parse(const char *devname)
2539 static int index = 0;
2540 char label[32];
2542 if (strcmp(devname, "none") == 0)
2543 return 0;
2544 if (index == MAX_SERIAL_PORTS) {
2545 error_report("too many serial ports");
2546 exit(1);
2548 snprintf(label, sizeof(label), "serial%d", index);
2549 serial_hds[index] = qemu_chr_new(label, devname);
2550 if (!serial_hds[index]) {
2551 error_report("could not connect serial device"
2552 " to character backend '%s'", devname);
2553 return -1;
2555 index++;
2556 return 0;
2559 static int parallel_parse(const char *devname)
2561 static int index = 0;
2562 char label[32];
2564 if (strcmp(devname, "none") == 0)
2565 return 0;
2566 if (index == MAX_PARALLEL_PORTS) {
2567 error_report("too many parallel ports");
2568 exit(1);
2570 snprintf(label, sizeof(label), "parallel%d", index);
2571 parallel_hds[index] = qemu_chr_new(label, devname);
2572 if (!parallel_hds[index]) {
2573 error_report("could not connect parallel device"
2574 " to character backend '%s'", devname);
2575 return -1;
2577 index++;
2578 return 0;
2581 static int virtcon_parse(const char *devname)
2583 QemuOptsList *device = qemu_find_opts("device");
2584 static int index = 0;
2585 char label[32];
2586 QemuOpts *bus_opts, *dev_opts;
2588 if (strcmp(devname, "none") == 0)
2589 return 0;
2590 if (index == MAX_VIRTIO_CONSOLES) {
2591 error_report("too many virtio consoles");
2592 exit(1);
2595 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2596 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2598 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2599 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2601 snprintf(label, sizeof(label), "virtcon%d", index);
2602 virtcon_hds[index] = qemu_chr_new(label, devname);
2603 if (!virtcon_hds[index]) {
2604 error_report("could not connect virtio console"
2605 " to character backend '%s'", devname);
2606 return -1;
2608 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2610 index++;
2611 return 0;
2614 static int sclp_parse(const char *devname)
2616 QemuOptsList *device = qemu_find_opts("device");
2617 static int index = 0;
2618 char label[32];
2619 QemuOpts *dev_opts;
2621 if (strcmp(devname, "none") == 0) {
2622 return 0;
2624 if (index == MAX_SCLP_CONSOLES) {
2625 error_report("too many sclp consoles");
2626 exit(1);
2629 assert(arch_type == QEMU_ARCH_S390X);
2631 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2632 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2634 snprintf(label, sizeof(label), "sclpcon%d", index);
2635 sclp_hds[index] = qemu_chr_new(label, devname);
2636 if (!sclp_hds[index]) {
2637 error_report("could not connect sclp console"
2638 " to character backend '%s'", devname);
2639 return -1;
2641 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2643 index++;
2644 return 0;
2647 static int debugcon_parse(const char *devname)
2649 QemuOpts *opts;
2651 if (!qemu_chr_new("debugcon", devname)) {
2652 exit(1);
2654 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2655 if (!opts) {
2656 error_report("already have a debugcon device");
2657 exit(1);
2659 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2660 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2661 return 0;
2664 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2666 const MachineClass *mc1 = a, *mc2 = b;
2667 int res;
2669 if (mc1->family == NULL) {
2670 if (mc2->family == NULL) {
2671 /* Compare standalone machine types against each other; they sort
2672 * in increasing order.
2674 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2675 object_class_get_name(OBJECT_CLASS(mc2)));
2678 /* Standalone machine types sort after families. */
2679 return 1;
2682 if (mc2->family == NULL) {
2683 /* Families sort before standalone machine types. */
2684 return -1;
2687 /* Families sort between each other alphabetically increasingly. */
2688 res = strcmp(mc1->family, mc2->family);
2689 if (res != 0) {
2690 return res;
2693 /* Within the same family, machine types sort in decreasing order. */
2694 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2695 object_class_get_name(OBJECT_CLASS(mc1)));
2698 static MachineClass *machine_parse(const char *name)
2700 MachineClass *mc = NULL;
2701 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2703 if (name) {
2704 mc = find_machine(name);
2706 if (mc) {
2707 g_slist_free(machines);
2708 return mc;
2710 if (name && !is_help_option(name)) {
2711 error_report("unsupported machine type");
2712 error_printf("Use -machine help to list supported machines\n");
2713 } else {
2714 printf("Supported machines are:\n");
2715 machines = g_slist_sort(machines, machine_class_cmp);
2716 for (el = machines; el; el = el->next) {
2717 MachineClass *mc = el->data;
2718 if (mc->alias) {
2719 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2721 printf("%-20s %s%s\n", mc->name, mc->desc,
2722 mc->is_default ? " (default)" : "");
2726 g_slist_free(machines);
2727 exit(!name || !is_help_option(name));
2730 void qemu_add_exit_notifier(Notifier *notify)
2732 notifier_list_add(&exit_notifiers, notify);
2735 void qemu_remove_exit_notifier(Notifier *notify)
2737 notifier_remove(notify);
2740 static void qemu_run_exit_notifiers(void)
2742 notifier_list_notify(&exit_notifiers, NULL);
2745 bool machine_init_done;
2747 void qemu_add_machine_init_done_notifier(Notifier *notify)
2749 notifier_list_add(&machine_init_done_notifiers, notify);
2750 if (machine_init_done) {
2751 notify->notify(notify, NULL);
2755 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2757 notifier_remove(notify);
2760 static void qemu_run_machine_init_done_notifiers(void)
2762 machine_init_done = true;
2763 notifier_list_notify(&machine_init_done_notifiers, NULL);
2766 static const QEMUOption *lookup_opt(int argc, char **argv,
2767 const char **poptarg, int *poptind)
2769 const QEMUOption *popt;
2770 int optind = *poptind;
2771 char *r = argv[optind];
2772 const char *optarg;
2774 loc_set_cmdline(argv, optind, 1);
2775 optind++;
2776 /* Treat --foo the same as -foo. */
2777 if (r[1] == '-')
2778 r++;
2779 popt = qemu_options;
2780 for(;;) {
2781 if (!popt->name) {
2782 error_report("invalid option");
2783 exit(1);
2785 if (!strcmp(popt->name, r + 1))
2786 break;
2787 popt++;
2789 if (popt->flags & HAS_ARG) {
2790 if (optind >= argc) {
2791 error_report("requires an argument");
2792 exit(1);
2794 optarg = argv[optind++];
2795 loc_set_cmdline(argv, optind - 2, 2);
2796 } else {
2797 optarg = NULL;
2800 *poptarg = optarg;
2801 *poptind = optind;
2803 return popt;
2806 static MachineClass *select_machine(void)
2808 MachineClass *machine_class = find_default_machine();
2809 const char *optarg;
2810 QemuOpts *opts;
2811 Location loc;
2813 loc_push_none(&loc);
2815 opts = qemu_get_machine_opts();
2816 qemu_opts_loc_restore(opts);
2818 optarg = qemu_opt_get(opts, "type");
2819 if (optarg) {
2820 machine_class = machine_parse(optarg);
2823 if (!machine_class) {
2824 error_report("No machine specified, and there is no default");
2825 error_printf("Use -machine help to list supported machines\n");
2826 exit(1);
2829 loc_pop(&loc);
2830 return machine_class;
2833 static int machine_set_property(void *opaque,
2834 const char *name, const char *value,
2835 Error **errp)
2837 Object *obj = OBJECT(opaque);
2838 Error *local_err = NULL;
2839 char *p, *qom_name;
2841 if (strcmp(name, "type") == 0) {
2842 return 0;
2845 qom_name = g_strdup(name);
2846 for (p = qom_name; *p; p++) {
2847 if (*p == '_') {
2848 *p = '-';
2852 object_property_parse(obj, value, qom_name, &local_err);
2853 g_free(qom_name);
2855 if (local_err) {
2856 error_report_err(local_err);
2857 return -1;
2860 return 0;
2865 * Initial object creation happens before all other
2866 * QEMU data types are created. The majority of objects
2867 * can be created at this point. The rng-egd object
2868 * cannot be created here, as it depends on the chardev
2869 * already existing.
2871 static bool object_create_initial(const char *type)
2873 if (g_str_equal(type, "rng-egd") ||
2874 g_str_has_prefix(type, "pr-manager-")) {
2875 return false;
2878 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2879 if (g_str_equal(type, "cryptodev-vhost-user")) {
2880 return false;
2882 #endif
2885 * return false for concrete netfilters since
2886 * they depend on netdevs already existing
2888 if (g_str_equal(type, "filter-buffer") ||
2889 g_str_equal(type, "filter-dump") ||
2890 g_str_equal(type, "filter-mirror") ||
2891 g_str_equal(type, "filter-redirector") ||
2892 g_str_equal(type, "colo-compare") ||
2893 g_str_equal(type, "filter-rewriter") ||
2894 g_str_equal(type, "filter-replay")) {
2895 return false;
2898 /* Memory allocation by backends needs to be done
2899 * after configure_accelerator() (due to the tcg_enabled()
2900 * checks at memory_region_init_*()).
2902 * Also, allocation of large amounts of memory may delay
2903 * chardev initialization for too long, and trigger timeouts
2904 * on software that waits for a monitor socket to be created
2905 * (e.g. libvirt).
2907 if (g_str_has_prefix(type, "memory-backend-")) {
2908 return false;
2911 return true;
2916 * The remainder of object creation happens after the
2917 * creation of chardev, fsdev, net clients and device data types.
2919 static bool object_create_delayed(const char *type)
2921 return !object_create_initial(type);
2925 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2926 MachineClass *mc)
2928 uint64_t sz;
2929 const char *mem_str;
2930 const char *maxmem_str, *slots_str;
2931 const ram_addr_t default_ram_size = mc->default_ram_size;
2932 QemuOpts *opts = qemu_find_opts_singleton("memory");
2933 Location loc;
2935 loc_push_none(&loc);
2936 qemu_opts_loc_restore(opts);
2938 sz = 0;
2939 mem_str = qemu_opt_get(opts, "size");
2940 if (mem_str) {
2941 if (!*mem_str) {
2942 error_report("missing 'size' option value");
2943 exit(EXIT_FAILURE);
2946 sz = qemu_opt_get_size(opts, "size", ram_size);
2948 /* Fix up legacy suffix-less format */
2949 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2950 uint64_t overflow_check = sz;
2952 sz <<= 20;
2953 if ((sz >> 20) != overflow_check) {
2954 error_report("too large 'size' option value");
2955 exit(EXIT_FAILURE);
2960 /* backward compatibility behaviour for case "-m 0" */
2961 if (sz == 0) {
2962 sz = default_ram_size;
2965 sz = QEMU_ALIGN_UP(sz, 8192);
2966 ram_size = sz;
2967 if (ram_size != sz) {
2968 error_report("ram size too large");
2969 exit(EXIT_FAILURE);
2972 /* store value for the future use */
2973 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2974 *maxram_size = ram_size;
2976 maxmem_str = qemu_opt_get(opts, "maxmem");
2977 slots_str = qemu_opt_get(opts, "slots");
2978 if (maxmem_str && slots_str) {
2979 uint64_t slots;
2981 sz = qemu_opt_get_size(opts, "maxmem", 0);
2982 slots = qemu_opt_get_number(opts, "slots", 0);
2983 if (sz < ram_size) {
2984 error_report("invalid value of -m option maxmem: "
2985 "maximum memory size (0x%" PRIx64 ") must be at least "
2986 "the initial memory size (0x" RAM_ADDR_FMT ")",
2987 sz, ram_size);
2988 exit(EXIT_FAILURE);
2989 } else if (sz > ram_size) {
2990 if (!slots) {
2991 error_report("invalid value of -m option: maxmem was "
2992 "specified, but no hotplug slots were specified");
2993 exit(EXIT_FAILURE);
2995 } else if (slots) {
2996 error_report("invalid value of -m option maxmem: "
2997 "memory slots were specified but maximum memory size "
2998 "(0x%" PRIx64 ") is equal to the initial memory size "
2999 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3000 exit(EXIT_FAILURE);
3003 *maxram_size = sz;
3004 *ram_slots = slots;
3005 } else if ((!maxmem_str && slots_str) ||
3006 (maxmem_str && !slots_str)) {
3007 error_report("invalid -m option value: missing "
3008 "'%s' option", slots_str ? "maxmem" : "slots");
3009 exit(EXIT_FAILURE);
3012 loc_pop(&loc);
3015 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3017 GlobalProperty *g;
3019 g = g_malloc0(sizeof(*g));
3020 g->driver = qemu_opt_get(opts, "driver");
3021 g->property = qemu_opt_get(opts, "property");
3022 g->value = qemu_opt_get(opts, "value");
3023 g->user_provided = true;
3024 g->errp = &error_fatal;
3025 qdev_prop_register_global(g);
3026 return 0;
3029 static int qemu_read_default_config_file(void)
3031 int ret;
3033 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3034 if (ret < 0 && ret != -ENOENT) {
3035 return ret;
3038 return 0;
3041 static void user_register_global_props(void)
3043 qemu_opts_foreach(qemu_find_opts("global"),
3044 global_init_func, NULL, NULL);
3048 * Note: we should see that these properties are actually having a
3049 * priority: accel < machine < user. This means e.g. when user
3050 * specifies something in "-global", it'll always be used with highest
3051 * priority than either machine/accelerator compat properties.
3053 static void register_global_properties(MachineState *ms)
3055 accel_register_compat_props(ms->accelerator);
3056 machine_register_compat_props(ms);
3057 user_register_global_props();
3060 int main(int argc, char **argv)
3062 int i;
3063 int snapshot, linux_boot;
3064 const char *initrd_filename;
3065 const char *kernel_filename, *kernel_cmdline;
3066 const char *boot_order = NULL;
3067 const char *boot_once = NULL;
3068 DisplayState *ds;
3069 QemuOpts *opts, *machine_opts;
3070 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
3071 QemuOptsList *olist;
3072 int optind;
3073 const char *optarg;
3074 const char *loadvm = NULL;
3075 MachineClass *machine_class;
3076 const char *cpu_model;
3077 const char *vga_model = NULL;
3078 const char *qtest_chrdev = NULL;
3079 const char *qtest_log = NULL;
3080 const char *pid_file = NULL;
3081 const char *incoming = NULL;
3082 bool userconfig = true;
3083 bool nographic = false;
3084 int display_remote = 0;
3085 const char *log_mask = NULL;
3086 const char *log_file = NULL;
3087 char *trace_file = NULL;
3088 ram_addr_t maxram_size;
3089 uint64_t ram_slots = 0;
3090 FILE *vmstate_dump_file = NULL;
3091 Error *main_loop_err = NULL;
3092 Error *err = NULL;
3093 bool list_data_dirs = false;
3094 char *dir, **dirs;
3095 typedef struct BlockdevOptions_queue {
3096 BlockdevOptions *bdo;
3097 Location loc;
3098 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3099 } BlockdevOptions_queue;
3100 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3101 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3103 module_call_init(MODULE_INIT_TRACE);
3105 qemu_init_cpu_list();
3106 qemu_init_cpu_loop();
3108 qemu_mutex_lock_iothread();
3110 atexit(qemu_run_exit_notifiers);
3111 error_set_progname(argv[0]);
3112 qemu_init_exec_dir(argv[0]);
3114 module_call_init(MODULE_INIT_QOM);
3115 monitor_init_qmp_commands();
3117 qemu_add_opts(&qemu_drive_opts);
3118 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3119 qemu_add_drive_opts(&qemu_common_drive_opts);
3120 qemu_add_drive_opts(&qemu_drive_opts);
3121 qemu_add_drive_opts(&bdrv_runtime_opts);
3122 qemu_add_opts(&qemu_chardev_opts);
3123 qemu_add_opts(&qemu_device_opts);
3124 qemu_add_opts(&qemu_netdev_opts);
3125 qemu_add_opts(&qemu_nic_opts);
3126 qemu_add_opts(&qemu_net_opts);
3127 qemu_add_opts(&qemu_rtc_opts);
3128 qemu_add_opts(&qemu_global_opts);
3129 qemu_add_opts(&qemu_mon_opts);
3130 qemu_add_opts(&qemu_trace_opts);
3131 qemu_add_opts(&qemu_option_rom_opts);
3132 qemu_add_opts(&qemu_machine_opts);
3133 qemu_add_opts(&qemu_accel_opts);
3134 qemu_add_opts(&qemu_mem_opts);
3135 qemu_add_opts(&qemu_smp_opts);
3136 qemu_add_opts(&qemu_boot_opts);
3137 qemu_add_opts(&qemu_sandbox_opts);
3138 qemu_add_opts(&qemu_add_fd_opts);
3139 qemu_add_opts(&qemu_object_opts);
3140 qemu_add_opts(&qemu_tpmdev_opts);
3141 qemu_add_opts(&qemu_realtime_opts);
3142 qemu_add_opts(&qemu_msg_opts);
3143 qemu_add_opts(&qemu_name_opts);
3144 qemu_add_opts(&qemu_numa_opts);
3145 qemu_add_opts(&qemu_icount_opts);
3146 qemu_add_opts(&qemu_semihosting_config_opts);
3147 qemu_add_opts(&qemu_fw_cfg_opts);
3148 module_call_init(MODULE_INIT_OPTS);
3150 runstate_init();
3152 if (qcrypto_init(&err) < 0) {
3153 error_reportf_err(err, "cannot initialize crypto: ");
3154 exit(1);
3156 rtc_clock = QEMU_CLOCK_HOST;
3158 QLIST_INIT (&vm_change_state_head);
3159 os_setup_early_signal_handling();
3161 cpu_model = NULL;
3162 snapshot = 0;
3164 nb_nics = 0;
3166 bdrv_init_with_whitelist();
3168 autostart = 1;
3170 /* first pass of option parsing */
3171 optind = 1;
3172 while (optind < argc) {
3173 if (argv[optind][0] != '-') {
3174 /* disk image */
3175 optind++;
3176 } else {
3177 const QEMUOption *popt;
3179 popt = lookup_opt(argc, argv, &optarg, &optind);
3180 switch (popt->index) {
3181 case QEMU_OPTION_nodefconfig:
3182 case QEMU_OPTION_nouserconfig:
3183 userconfig = false;
3184 break;
3189 if (userconfig) {
3190 if (qemu_read_default_config_file() < 0) {
3191 exit(1);
3195 /* second pass of option parsing */
3196 optind = 1;
3197 for(;;) {
3198 if (optind >= argc)
3199 break;
3200 if (argv[optind][0] != '-') {
3201 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3202 } else {
3203 const QEMUOption *popt;
3205 popt = lookup_opt(argc, argv, &optarg, &optind);
3206 if (!(popt->arch_mask & arch_type)) {
3207 error_report("Option not supported for this target");
3208 exit(1);
3210 switch(popt->index) {
3211 case QEMU_OPTION_no_kvm_irqchip: {
3212 olist = qemu_find_opts("machine");
3213 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3214 break;
3216 case QEMU_OPTION_cpu:
3217 /* hw initialization will check this */
3218 cpu_model = optarg;
3219 break;
3220 case QEMU_OPTION_hda:
3221 case QEMU_OPTION_hdb:
3222 case QEMU_OPTION_hdc:
3223 case QEMU_OPTION_hdd:
3224 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3225 HD_OPTS);
3226 break;
3227 case QEMU_OPTION_blockdev:
3229 Visitor *v;
3230 BlockdevOptions_queue *bdo;
3232 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3233 if (!v) {
3234 error_report_err(err);
3235 exit(1);
3238 bdo = g_new(BlockdevOptions_queue, 1);
3239 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3240 &error_fatal);
3241 visit_free(v);
3242 loc_save(&bdo->loc);
3243 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3244 break;
3246 case QEMU_OPTION_drive:
3247 if (drive_def(optarg) == NULL) {
3248 exit(1);
3250 break;
3251 case QEMU_OPTION_set:
3252 if (qemu_set_option(optarg) != 0)
3253 exit(1);
3254 break;
3255 case QEMU_OPTION_global:
3256 if (qemu_global_option(optarg) != 0)
3257 exit(1);
3258 break;
3259 case QEMU_OPTION_mtdblock:
3260 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3261 break;
3262 case QEMU_OPTION_sd:
3263 drive_add(IF_SD, -1, optarg, SD_OPTS);
3264 break;
3265 case QEMU_OPTION_pflash:
3266 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3267 break;
3268 case QEMU_OPTION_snapshot:
3269 snapshot = 1;
3270 break;
3271 case QEMU_OPTION_numa:
3272 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3273 optarg, true);
3274 if (!opts) {
3275 exit(1);
3277 break;
3278 case QEMU_OPTION_display:
3279 parse_display(optarg);
3280 break;
3281 case QEMU_OPTION_nographic:
3282 olist = qemu_find_opts("machine");
3283 qemu_opts_parse_noisily(olist, "graphics=off", false);
3284 nographic = true;
3285 dpy.type = DISPLAY_TYPE_NONE;
3286 break;
3287 case QEMU_OPTION_curses:
3288 #ifdef CONFIG_CURSES
3289 dpy.type = DISPLAY_TYPE_CURSES;
3290 #else
3291 error_report("curses support is disabled");
3292 exit(1);
3293 #endif
3294 break;
3295 case QEMU_OPTION_portrait:
3296 graphic_rotate = 90;
3297 break;
3298 case QEMU_OPTION_rotate:
3299 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3300 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3301 graphic_rotate != 180 && graphic_rotate != 270) {
3302 error_report("only 90, 180, 270 deg rotation is available");
3303 exit(1);
3305 break;
3306 case QEMU_OPTION_kernel:
3307 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3308 &error_abort);
3309 break;
3310 case QEMU_OPTION_initrd:
3311 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3312 &error_abort);
3313 break;
3314 case QEMU_OPTION_append:
3315 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3316 &error_abort);
3317 break;
3318 case QEMU_OPTION_dtb:
3319 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3320 &error_abort);
3321 break;
3322 case QEMU_OPTION_cdrom:
3323 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3324 break;
3325 case QEMU_OPTION_boot:
3326 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3327 optarg, true);
3328 if (!opts) {
3329 exit(1);
3331 break;
3332 case QEMU_OPTION_fda:
3333 case QEMU_OPTION_fdb:
3334 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3335 optarg, FD_OPTS);
3336 break;
3337 case QEMU_OPTION_no_fd_bootchk:
3338 fd_bootchk = 0;
3339 break;
3340 case QEMU_OPTION_netdev:
3341 default_net = 0;
3342 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3343 exit(1);
3345 break;
3346 case QEMU_OPTION_nic:
3347 default_net = 0;
3348 if (net_client_parse(qemu_find_opts("nic"), optarg) == -1) {
3349 exit(1);
3351 break;
3352 case QEMU_OPTION_net:
3353 default_net = 0;
3354 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3355 exit(1);
3357 break;
3358 #ifdef CONFIG_LIBISCSI
3359 case QEMU_OPTION_iscsi:
3360 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3361 optarg, false);
3362 if (!opts) {
3363 exit(1);
3365 break;
3366 #endif
3367 #ifdef CONFIG_SLIRP
3368 case QEMU_OPTION_tftp:
3369 error_report("The -tftp option is deprecated. "
3370 "Please use '-netdev user,tftp=...' instead.");
3371 legacy_tftp_prefix = optarg;
3372 break;
3373 case QEMU_OPTION_bootp:
3374 error_report("The -bootp option is deprecated. "
3375 "Please use '-netdev user,bootfile=...' instead.");
3376 legacy_bootp_filename = optarg;
3377 break;
3378 case QEMU_OPTION_redir:
3379 error_report("The -redir option is deprecated. "
3380 "Please use '-netdev user,hostfwd=...' instead.");
3381 if (net_slirp_redir(optarg) < 0)
3382 exit(1);
3383 break;
3384 #endif
3385 case QEMU_OPTION_bt:
3386 add_device_config(DEV_BT, optarg);
3387 break;
3388 case QEMU_OPTION_audio_help:
3389 AUD_help ();
3390 exit (0);
3391 break;
3392 case QEMU_OPTION_soundhw:
3393 select_soundhw (optarg);
3394 break;
3395 case QEMU_OPTION_h:
3396 help(0);
3397 break;
3398 case QEMU_OPTION_version:
3399 version();
3400 exit(0);
3401 break;
3402 case QEMU_OPTION_m:
3403 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3404 optarg, true);
3405 if (!opts) {
3406 exit(EXIT_FAILURE);
3408 break;
3409 #ifdef CONFIG_TPM
3410 case QEMU_OPTION_tpmdev:
3411 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3412 exit(1);
3414 break;
3415 #endif
3416 case QEMU_OPTION_mempath:
3417 mem_path = optarg;
3418 break;
3419 case QEMU_OPTION_mem_prealloc:
3420 mem_prealloc = 1;
3421 break;
3422 case QEMU_OPTION_d:
3423 log_mask = optarg;
3424 break;
3425 case QEMU_OPTION_D:
3426 log_file = optarg;
3427 break;
3428 case QEMU_OPTION_DFILTER:
3429 qemu_set_dfilter_ranges(optarg, &error_fatal);
3430 break;
3431 case QEMU_OPTION_s:
3432 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3433 break;
3434 case QEMU_OPTION_gdb:
3435 add_device_config(DEV_GDB, optarg);
3436 break;
3437 case QEMU_OPTION_L:
3438 if (is_help_option(optarg)) {
3439 list_data_dirs = true;
3440 } else {
3441 qemu_add_data_dir(optarg);
3443 break;
3444 case QEMU_OPTION_bios:
3445 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3446 &error_abort);
3447 break;
3448 case QEMU_OPTION_singlestep:
3449 singlestep = 1;
3450 break;
3451 case QEMU_OPTION_S:
3452 autostart = 0;
3453 break;
3454 case QEMU_OPTION_k:
3455 keyboard_layout = optarg;
3456 break;
3457 case QEMU_OPTION_localtime:
3458 rtc_utc = 0;
3459 warn_report("This option is deprecated, "
3460 "use '-rtc base=localtime' instead.");
3461 break;
3462 case QEMU_OPTION_vga:
3463 vga_model = optarg;
3464 default_vga = 0;
3465 break;
3466 case QEMU_OPTION_g:
3468 const char *p;
3469 int w, h, depth;
3470 p = optarg;
3471 w = strtol(p, (char **)&p, 10);
3472 if (w <= 0) {
3473 graphic_error:
3474 error_report("invalid resolution or depth");
3475 exit(1);
3477 if (*p != 'x')
3478 goto graphic_error;
3479 p++;
3480 h = strtol(p, (char **)&p, 10);
3481 if (h <= 0)
3482 goto graphic_error;
3483 if (*p == 'x') {
3484 p++;
3485 depth = strtol(p, (char **)&p, 10);
3486 if (depth != 8 && depth != 15 && depth != 16 &&
3487 depth != 24 && depth != 32)
3488 goto graphic_error;
3489 } else if (*p == '\0') {
3490 depth = graphic_depth;
3491 } else {
3492 goto graphic_error;
3495 graphic_width = w;
3496 graphic_height = h;
3497 graphic_depth = depth;
3499 break;
3500 case QEMU_OPTION_echr:
3502 char *r;
3503 term_escape_char = strtol(optarg, &r, 0);
3504 if (r == optarg)
3505 printf("Bad argument to echr\n");
3506 break;
3508 case QEMU_OPTION_monitor:
3509 default_monitor = 0;
3510 if (strncmp(optarg, "none", 4)) {
3511 monitor_parse(optarg, "readline", false);
3513 break;
3514 case QEMU_OPTION_qmp:
3515 monitor_parse(optarg, "control", false);
3516 default_monitor = 0;
3517 break;
3518 case QEMU_OPTION_qmp_pretty:
3519 monitor_parse(optarg, "control", true);
3520 default_monitor = 0;
3521 break;
3522 case QEMU_OPTION_mon:
3523 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3524 true);
3525 if (!opts) {
3526 exit(1);
3528 default_monitor = 0;
3529 break;
3530 case QEMU_OPTION_chardev:
3531 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3532 optarg, true);
3533 if (!opts) {
3534 exit(1);
3536 break;
3537 case QEMU_OPTION_fsdev:
3538 olist = qemu_find_opts("fsdev");
3539 if (!olist) {
3540 error_report("fsdev support is disabled");
3541 exit(1);
3543 opts = qemu_opts_parse_noisily(olist, optarg, true);
3544 if (!opts) {
3545 exit(1);
3547 break;
3548 case QEMU_OPTION_virtfs: {
3549 QemuOpts *fsdev;
3550 QemuOpts *device;
3551 const char *writeout, *sock_fd, *socket, *path, *security_model;
3553 olist = qemu_find_opts("virtfs");
3554 if (!olist) {
3555 error_report("virtfs support is disabled");
3556 exit(1);
3558 opts = qemu_opts_parse_noisily(olist, optarg, true);
3559 if (!opts) {
3560 exit(1);
3563 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3564 qemu_opt_get(opts, "mount_tag") == NULL) {
3565 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3566 exit(1);
3568 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3569 qemu_opts_id(opts) ?:
3570 qemu_opt_get(opts, "mount_tag"),
3571 1, NULL);
3572 if (!fsdev) {
3573 error_report("duplicate or invalid fsdev id: %s",
3574 qemu_opt_get(opts, "mount_tag"));
3575 exit(1);
3578 writeout = qemu_opt_get(opts, "writeout");
3579 if (writeout) {
3580 #ifdef CONFIG_SYNC_FILE_RANGE
3581 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3582 #else
3583 error_report("writeout=immediate not supported "
3584 "on this platform");
3585 exit(1);
3586 #endif
3588 qemu_opt_set(fsdev, "fsdriver",
3589 qemu_opt_get(opts, "fsdriver"), &error_abort);
3590 path = qemu_opt_get(opts, "path");
3591 if (path) {
3592 qemu_opt_set(fsdev, "path", path, &error_abort);
3594 security_model = qemu_opt_get(opts, "security_model");
3595 if (security_model) {
3596 qemu_opt_set(fsdev, "security_model", security_model,
3597 &error_abort);
3599 socket = qemu_opt_get(opts, "socket");
3600 if (socket) {
3601 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3603 sock_fd = qemu_opt_get(opts, "sock_fd");
3604 if (sock_fd) {
3605 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3608 qemu_opt_set_bool(fsdev, "readonly",
3609 qemu_opt_get_bool(opts, "readonly", 0),
3610 &error_abort);
3611 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3612 &error_abort);
3613 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3614 qemu_opt_set(device, "fsdev",
3615 qemu_opts_id(fsdev), &error_abort);
3616 qemu_opt_set(device, "mount_tag",
3617 qemu_opt_get(opts, "mount_tag"), &error_abort);
3618 break;
3620 case QEMU_OPTION_virtfs_synth: {
3621 QemuOpts *fsdev;
3622 QemuOpts *device;
3624 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3625 1, NULL);
3626 if (!fsdev) {
3627 error_report("duplicate option: %s", "virtfs_synth");
3628 exit(1);
3630 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3632 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3633 &error_abort);
3634 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3635 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3636 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3637 break;
3639 case QEMU_OPTION_serial:
3640 add_device_config(DEV_SERIAL, optarg);
3641 default_serial = 0;
3642 if (strncmp(optarg, "mon:", 4) == 0) {
3643 default_monitor = 0;
3645 break;
3646 case QEMU_OPTION_watchdog:
3647 if (watchdog) {
3648 error_report("only one watchdog option may be given");
3649 return 1;
3651 watchdog = optarg;
3652 break;
3653 case QEMU_OPTION_watchdog_action:
3654 if (select_watchdog_action(optarg) == -1) {
3655 error_report("unknown -watchdog-action parameter");
3656 exit(1);
3658 break;
3659 case QEMU_OPTION_virtiocon:
3660 add_device_config(DEV_VIRTCON, optarg);
3661 default_virtcon = 0;
3662 if (strncmp(optarg, "mon:", 4) == 0) {
3663 default_monitor = 0;
3665 break;
3666 case QEMU_OPTION_parallel:
3667 add_device_config(DEV_PARALLEL, optarg);
3668 default_parallel = 0;
3669 if (strncmp(optarg, "mon:", 4) == 0) {
3670 default_monitor = 0;
3672 break;
3673 case QEMU_OPTION_debugcon:
3674 add_device_config(DEV_DEBUGCON, optarg);
3675 break;
3676 case QEMU_OPTION_loadvm:
3677 loadvm = optarg;
3678 break;
3679 case QEMU_OPTION_full_screen:
3680 dpy.has_full_screen = true;
3681 dpy.full_screen = true;
3682 break;
3683 case QEMU_OPTION_no_frame:
3684 g_printerr("The -no-frame switch is deprecated, and will be\n"
3685 "removed in a future release.\n");
3686 no_frame = 1;
3687 break;
3688 case QEMU_OPTION_alt_grab:
3689 alt_grab = 1;
3690 break;
3691 case QEMU_OPTION_ctrl_grab:
3692 ctrl_grab = 1;
3693 break;
3694 case QEMU_OPTION_no_quit:
3695 dpy.has_window_close = true;
3696 dpy.window_close = false;
3697 break;
3698 case QEMU_OPTION_sdl:
3699 #ifdef CONFIG_SDL
3700 dpy.type = DISPLAY_TYPE_SDL;
3701 break;
3702 #else
3703 error_report("SDL support is disabled");
3704 exit(1);
3705 #endif
3706 case QEMU_OPTION_pidfile:
3707 pid_file = optarg;
3708 break;
3709 case QEMU_OPTION_win2k_hack:
3710 win2k_install_hack = 1;
3711 break;
3712 case QEMU_OPTION_rtc_td_hack: {
3713 static GlobalProperty slew_lost_ticks = {
3714 .driver = "mc146818rtc",
3715 .property = "lost_tick_policy",
3716 .value = "slew",
3719 qdev_prop_register_global(&slew_lost_ticks);
3720 warn_report("This option is deprecated, "
3721 "use '-rtc driftfix=slew' instead.");
3722 break;
3724 case QEMU_OPTION_acpitable:
3725 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3726 optarg, true);
3727 if (!opts) {
3728 exit(1);
3730 acpi_table_add(opts, &error_fatal);
3731 break;
3732 case QEMU_OPTION_smbios:
3733 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3734 optarg, false);
3735 if (!opts) {
3736 exit(1);
3738 smbios_entry_add(opts, &error_fatal);
3739 break;
3740 case QEMU_OPTION_fwcfg:
3741 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3742 optarg, true);
3743 if (opts == NULL) {
3744 exit(1);
3746 break;
3747 case QEMU_OPTION_enable_kvm:
3748 olist = qemu_find_opts("machine");
3749 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3750 break;
3751 case QEMU_OPTION_enable_hax:
3752 olist = qemu_find_opts("machine");
3753 qemu_opts_parse_noisily(olist, "accel=hax", false);
3754 break;
3755 case QEMU_OPTION_M:
3756 case QEMU_OPTION_machine:
3757 olist = qemu_find_opts("machine");
3758 opts = qemu_opts_parse_noisily(olist, optarg, true);
3759 if (!opts) {
3760 exit(1);
3762 break;
3763 case QEMU_OPTION_no_kvm:
3764 olist = qemu_find_opts("machine");
3765 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3766 break;
3767 case QEMU_OPTION_no_kvm_pit_reinjection: {
3768 static GlobalProperty kvm_pit_lost_tick_policy = {
3769 .driver = "kvm-pit",
3770 .property = "lost_tick_policy",
3771 .value = "discard",
3774 warn_report("deprecated, replaced by "
3775 "-global kvm-pit.lost_tick_policy=discard");
3776 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3777 break;
3779 case QEMU_OPTION_accel:
3780 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3781 optarg, true);
3782 optarg = qemu_opt_get(accel_opts, "accel");
3783 if (!optarg || is_help_option(optarg)) {
3784 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3785 exit(0);
3787 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3788 false, &error_abort);
3789 qemu_opt_set(opts, "accel", optarg, &error_abort);
3790 break;
3791 case QEMU_OPTION_usb:
3792 olist = qemu_find_opts("machine");
3793 qemu_opts_parse_noisily(olist, "usb=on", false);
3794 break;
3795 case QEMU_OPTION_usbdevice:
3796 error_report("'-usbdevice' is deprecated, please use "
3797 "'-device usb-...' instead");
3798 olist = qemu_find_opts("machine");
3799 qemu_opts_parse_noisily(olist, "usb=on", false);
3800 add_device_config(DEV_USB, optarg);
3801 break;
3802 case QEMU_OPTION_device:
3803 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3804 optarg, true)) {
3805 exit(1);
3807 break;
3808 case QEMU_OPTION_smp:
3809 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3810 optarg, true)) {
3811 exit(1);
3813 break;
3814 case QEMU_OPTION_vnc:
3815 vnc_parse(optarg, &error_fatal);
3816 break;
3817 case QEMU_OPTION_no_acpi:
3818 acpi_enabled = 0;
3819 break;
3820 case QEMU_OPTION_no_hpet:
3821 no_hpet = 1;
3822 break;
3823 case QEMU_OPTION_balloon:
3824 if (balloon_parse(optarg) < 0) {
3825 error_report("unknown -balloon argument %s", optarg);
3826 exit(1);
3828 break;
3829 case QEMU_OPTION_no_reboot:
3830 no_reboot = 1;
3831 break;
3832 case QEMU_OPTION_no_shutdown:
3833 no_shutdown = 1;
3834 break;
3835 case QEMU_OPTION_show_cursor:
3836 cursor_hide = 0;
3837 break;
3838 case QEMU_OPTION_uuid:
3839 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3840 error_report("failed to parse UUID string: wrong format");
3841 exit(1);
3843 qemu_uuid_set = true;
3844 break;
3845 case QEMU_OPTION_option_rom:
3846 if (nb_option_roms >= MAX_OPTION_ROMS) {
3847 error_report("too many option ROMs");
3848 exit(1);
3850 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3851 optarg, true);
3852 if (!opts) {
3853 exit(1);
3855 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3856 option_rom[nb_option_roms].bootindex =
3857 qemu_opt_get_number(opts, "bootindex", -1);
3858 if (!option_rom[nb_option_roms].name) {
3859 error_report("Option ROM file is not specified");
3860 exit(1);
3862 nb_option_roms++;
3863 break;
3864 case QEMU_OPTION_semihosting:
3865 semihosting.enabled = true;
3866 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3867 break;
3868 case QEMU_OPTION_semihosting_config:
3869 semihosting.enabled = true;
3870 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3871 optarg, false);
3872 if (opts != NULL) {
3873 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3874 true);
3875 const char *target = qemu_opt_get(opts, "target");
3876 if (target != NULL) {
3877 if (strcmp("native", target) == 0) {
3878 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3879 } else if (strcmp("gdb", target) == 0) {
3880 semihosting.target = SEMIHOSTING_TARGET_GDB;
3881 } else if (strcmp("auto", target) == 0) {
3882 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3883 } else {
3884 error_report("unsupported semihosting-config %s",
3885 optarg);
3886 exit(1);
3888 } else {
3889 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3891 /* Set semihosting argument count and vector */
3892 qemu_opt_foreach(opts, add_semihosting_arg,
3893 &semihosting, NULL);
3894 } else {
3895 error_report("unsupported semihosting-config %s", optarg);
3896 exit(1);
3898 break;
3899 case QEMU_OPTION_name:
3900 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3901 optarg, true);
3902 if (!opts) {
3903 exit(1);
3905 break;
3906 case QEMU_OPTION_prom_env:
3907 if (nb_prom_envs >= MAX_PROM_ENVS) {
3908 error_report("too many prom variables");
3909 exit(1);
3911 prom_envs[nb_prom_envs] = optarg;
3912 nb_prom_envs++;
3913 break;
3914 case QEMU_OPTION_old_param:
3915 old_param = 1;
3916 break;
3917 case QEMU_OPTION_clock:
3918 /* Clock options no longer exist. Keep this option for
3919 * backward compatibility.
3921 break;
3922 case QEMU_OPTION_startdate:
3923 warn_report("This option is deprecated, use '-rtc base=' instead.");
3924 configure_rtc_date_offset(optarg, 1);
3925 break;
3926 case QEMU_OPTION_rtc:
3927 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3928 false);
3929 if (!opts) {
3930 exit(1);
3932 configure_rtc(opts);
3933 break;
3934 case QEMU_OPTION_tb_size:
3935 #ifndef CONFIG_TCG
3936 error_report("TCG is disabled");
3937 exit(1);
3938 #endif
3939 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3940 error_report("Invalid argument to -tb-size");
3941 exit(1);
3943 break;
3944 case QEMU_OPTION_icount:
3945 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3946 optarg, true);
3947 if (!icount_opts) {
3948 exit(1);
3950 break;
3951 case QEMU_OPTION_incoming:
3952 if (!incoming) {
3953 runstate_set(RUN_STATE_INMIGRATE);
3955 incoming = optarg;
3956 break;
3957 case QEMU_OPTION_only_migratable:
3959 * TODO: we can remove this option one day, and we
3960 * should all use:
3962 * "-global migration.only-migratable=true"
3964 qemu_global_option("migration.only-migratable=true");
3965 break;
3966 case QEMU_OPTION_nodefaults:
3967 has_defaults = 0;
3968 break;
3969 case QEMU_OPTION_xen_domid:
3970 if (!(xen_available())) {
3971 error_report("Option not supported for this target");
3972 exit(1);
3974 xen_domid = atoi(optarg);
3975 break;
3976 case QEMU_OPTION_xen_create:
3977 if (!(xen_available())) {
3978 error_report("Option not supported for this target");
3979 exit(1);
3981 xen_mode = XEN_CREATE;
3982 break;
3983 case QEMU_OPTION_xen_attach:
3984 if (!(xen_available())) {
3985 error_report("Option not supported for this target");
3986 exit(1);
3988 xen_mode = XEN_ATTACH;
3989 break;
3990 case QEMU_OPTION_xen_domid_restrict:
3991 if (!(xen_available())) {
3992 error_report("Option not supported for this target");
3993 exit(1);
3995 xen_domid_restrict = true;
3996 break;
3997 case QEMU_OPTION_trace:
3998 g_free(trace_file);
3999 trace_file = trace_opt_parse(optarg);
4000 break;
4001 case QEMU_OPTION_trace_unassigned:
4002 trace_unassigned = true;
4003 break;
4004 case QEMU_OPTION_readconfig:
4006 int ret = qemu_read_config_file(optarg);
4007 if (ret < 0) {
4008 error_report("read config %s: %s", optarg,
4009 strerror(-ret));
4010 exit(1);
4012 break;
4014 case QEMU_OPTION_spice:
4015 olist = qemu_find_opts("spice");
4016 if (!olist) {
4017 error_report("spice support is disabled");
4018 exit(1);
4020 opts = qemu_opts_parse_noisily(olist, optarg, false);
4021 if (!opts) {
4022 exit(1);
4024 display_remote++;
4025 break;
4026 case QEMU_OPTION_writeconfig:
4028 FILE *fp;
4029 if (strcmp(optarg, "-") == 0) {
4030 fp = stdout;
4031 } else {
4032 fp = fopen(optarg, "w");
4033 if (fp == NULL) {
4034 error_report("open %s: %s", optarg,
4035 strerror(errno));
4036 exit(1);
4039 qemu_config_write(fp);
4040 if (fp != stdout) {
4041 fclose(fp);
4043 break;
4045 case QEMU_OPTION_qtest:
4046 qtest_chrdev = optarg;
4047 break;
4048 case QEMU_OPTION_qtest_log:
4049 qtest_log = optarg;
4050 break;
4051 case QEMU_OPTION_sandbox:
4052 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4053 optarg, true);
4054 if (!opts) {
4055 exit(1);
4057 break;
4058 case QEMU_OPTION_add_fd:
4059 #ifndef _WIN32
4060 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4061 optarg, false);
4062 if (!opts) {
4063 exit(1);
4065 #else
4066 error_report("File descriptor passing is disabled on this "
4067 "platform");
4068 exit(1);
4069 #endif
4070 break;
4071 case QEMU_OPTION_object:
4072 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4073 optarg, true);
4074 if (!opts) {
4075 exit(1);
4077 break;
4078 case QEMU_OPTION_realtime:
4079 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4080 optarg, false);
4081 if (!opts) {
4082 exit(1);
4084 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4085 break;
4086 case QEMU_OPTION_msg:
4087 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4088 false);
4089 if (!opts) {
4090 exit(1);
4092 configure_msg(opts);
4093 break;
4094 case QEMU_OPTION_dump_vmstate:
4095 if (vmstate_dump_file) {
4096 error_report("only one '-dump-vmstate' "
4097 "option may be given");
4098 exit(1);
4100 vmstate_dump_file = fopen(optarg, "w");
4101 if (vmstate_dump_file == NULL) {
4102 error_report("open %s: %s", optarg, strerror(errno));
4103 exit(1);
4105 break;
4106 default:
4107 os_parse_cmd_args(popt->index, optarg);
4112 * Clear error location left behind by the loop.
4113 * Best done right after the loop. Do not insert code here!
4115 loc_set_none();
4117 replay_configure(icount_opts);
4119 machine_class = select_machine();
4121 set_memory_options(&ram_slots, &maxram_size, machine_class);
4123 os_daemonize();
4124 rcu_disable_atfork();
4126 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4127 error_report("could not acquire pid file: %s", strerror(errno));
4128 exit(1);
4131 if (qemu_init_main_loop(&main_loop_err)) {
4132 error_report_err(main_loop_err);
4133 exit(1);
4136 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4137 parse_sandbox, NULL, NULL)) {
4138 exit(1);
4141 if (qemu_opts_foreach(qemu_find_opts("name"),
4142 parse_name, NULL, NULL)) {
4143 exit(1);
4146 #ifndef _WIN32
4147 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4148 parse_add_fd, NULL, NULL)) {
4149 exit(1);
4152 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4153 cleanup_add_fd, NULL, NULL)) {
4154 exit(1);
4156 #endif
4158 current_machine = MACHINE(object_new(object_class_get_name(
4159 OBJECT_CLASS(machine_class))));
4160 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4161 exit(0);
4163 object_property_add_child(object_get_root(), "machine",
4164 OBJECT(current_machine), &error_abort);
4166 if (machine_class->minimum_page_bits) {
4167 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4168 /* This would be a board error: specifying a minimum smaller than
4169 * a target's compile-time fixed setting.
4171 g_assert_not_reached();
4175 cpu_exec_init_all();
4177 if (machine_class->hw_version) {
4178 qemu_set_hw_version(machine_class->hw_version);
4181 if (cpu_model && is_help_option(cpu_model)) {
4182 list_cpus(stdout, &fprintf, cpu_model);
4183 exit(0);
4186 if (!trace_init_backends()) {
4187 exit(1);
4189 trace_init_file(trace_file);
4191 /* Open the logfile at this point and set the log mask if necessary.
4193 if (log_file) {
4194 qemu_set_log_filename(log_file, &error_fatal);
4197 if (log_mask) {
4198 int mask;
4199 mask = qemu_str_to_log_mask(log_mask);
4200 if (!mask) {
4201 qemu_print_log_usage(stdout);
4202 exit(1);
4204 qemu_set_log(mask);
4205 } else {
4206 qemu_set_log(0);
4209 /* add configured firmware directories */
4210 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4211 for (i = 0; dirs[i] != NULL; i++) {
4212 qemu_add_data_dir(dirs[i]);
4214 g_strfreev(dirs);
4216 /* try to find datadir relative to the executable path */
4217 dir = os_find_datadir();
4218 qemu_add_data_dir(dir);
4219 g_free(dir);
4221 /* add the datadir specified when building */
4222 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4224 /* -L help lists the data directories and exits. */
4225 if (list_data_dirs) {
4226 for (i = 0; i < data_dir_idx; i++) {
4227 printf("%s\n", data_dir[i]);
4229 exit(0);
4232 /* machine_class: default to UP */
4233 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4234 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4235 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4237 /* default to machine_class->default_cpus */
4238 smp_cpus = machine_class->default_cpus;
4239 max_cpus = machine_class->default_cpus;
4241 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4243 /* sanity-check smp_cpus and max_cpus against machine_class */
4244 if (smp_cpus < machine_class->min_cpus) {
4245 error_report("Invalid SMP CPUs %d. The min CPUs "
4246 "supported by machine '%s' is %d", smp_cpus,
4247 machine_class->name, machine_class->min_cpus);
4248 exit(1);
4250 if (max_cpus > machine_class->max_cpus) {
4251 error_report("Invalid SMP CPUs %d. The max CPUs "
4252 "supported by machine '%s' is %d", max_cpus,
4253 machine_class->name, machine_class->max_cpus);
4254 exit(1);
4258 * Get the default machine options from the machine if it is not already
4259 * specified either by the configuration file or by the command line.
4261 if (machine_class->default_machine_opts) {
4262 qemu_opts_set_defaults(qemu_find_opts("machine"),
4263 machine_class->default_machine_opts, 0);
4266 qemu_opts_foreach(qemu_find_opts("device"),
4267 default_driver_check, NULL, NULL);
4268 qemu_opts_foreach(qemu_find_opts("global"),
4269 default_driver_check, NULL, NULL);
4271 if (!vga_model && !default_vga) {
4272 vga_interface_type = VGA_DEVICE;
4274 if (!has_defaults || machine_class->no_serial) {
4275 default_serial = 0;
4277 if (!has_defaults || machine_class->no_parallel) {
4278 default_parallel = 0;
4280 if (!has_defaults || !machine_class->use_virtcon) {
4281 default_virtcon = 0;
4283 if (!has_defaults || !machine_class->use_sclp) {
4284 default_sclp = 0;
4286 if (!has_defaults || machine_class->no_floppy) {
4287 default_floppy = 0;
4289 if (!has_defaults || machine_class->no_cdrom) {
4290 default_cdrom = 0;
4292 if (!has_defaults || machine_class->no_sdcard) {
4293 default_sdcard = 0;
4295 if (!has_defaults) {
4296 default_monitor = 0;
4297 default_net = 0;
4298 default_vga = 0;
4301 if (is_daemonized()) {
4302 /* According to documentation and historically, -nographic redirects
4303 * serial port, parallel port and monitor to stdio, which does not work
4304 * with -daemonize. We can redirect these to null instead, but since
4305 * -nographic is legacy, let's just error out.
4306 * We disallow -nographic only if all other ports are not redirected
4307 * explicitly, to not break existing legacy setups which uses
4308 * -nographic _and_ redirects all ports explicitly - this is valid
4309 * usage, -nographic is just a no-op in this case.
4311 if (nographic
4312 && (default_parallel || default_serial
4313 || default_monitor || default_virtcon)) {
4314 error_report("-nographic cannot be used with -daemonize");
4315 exit(1);
4317 #ifdef CONFIG_CURSES
4318 if (dpy.type == DISPLAY_TYPE_CURSES) {
4319 error_report("curses display cannot be used with -daemonize");
4320 exit(1);
4322 #endif
4325 if (nographic) {
4326 if (default_parallel)
4327 add_device_config(DEV_PARALLEL, "null");
4328 if (default_serial && default_monitor) {
4329 add_device_config(DEV_SERIAL, "mon:stdio");
4330 } else if (default_virtcon && default_monitor) {
4331 add_device_config(DEV_VIRTCON, "mon:stdio");
4332 } else if (default_sclp && default_monitor) {
4333 add_device_config(DEV_SCLP, "mon:stdio");
4334 } else {
4335 if (default_serial)
4336 add_device_config(DEV_SERIAL, "stdio");
4337 if (default_virtcon)
4338 add_device_config(DEV_VIRTCON, "stdio");
4339 if (default_sclp) {
4340 add_device_config(DEV_SCLP, "stdio");
4342 if (default_monitor)
4343 monitor_parse("stdio", "readline", false);
4345 } else {
4346 if (default_serial)
4347 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4348 if (default_parallel)
4349 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4350 if (default_monitor)
4351 monitor_parse("vc:80Cx24C", "readline", false);
4352 if (default_virtcon)
4353 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4354 if (default_sclp) {
4355 add_device_config(DEV_SCLP, "vc:80Cx24C");
4359 #if defined(CONFIG_VNC)
4360 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4361 display_remote++;
4363 #endif
4364 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
4365 if (!qemu_display_find_default(&dpy)) {
4366 dpy.type = DISPLAY_TYPE_NONE;
4367 #if defined(CONFIG_VNC)
4368 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4369 #endif
4372 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
4373 dpy.type = DISPLAY_TYPE_NONE;
4376 if ((no_frame || alt_grab || ctrl_grab) && dpy.type != DISPLAY_TYPE_SDL) {
4377 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4378 "for SDL, ignoring option");
4380 if (dpy.has_window_close &&
4381 (dpy.type != DISPLAY_TYPE_GTK && dpy.type != DISPLAY_TYPE_SDL)) {
4382 error_report("-no-quit is only valid for GTK and SDL, "
4383 "ignoring option");
4386 qemu_display_early_init(&dpy);
4387 qemu_console_early_init();
4389 if (dpy.has_gl && dpy.gl && display_opengl == 0) {
4390 #if defined(CONFIG_OPENGL)
4391 error_report("OpenGL is not supported by the display");
4392 #else
4393 error_report("OpenGL support is disabled");
4394 #endif
4395 exit(1);
4398 page_size_init();
4399 socket_init();
4401 if (qemu_opts_foreach(qemu_find_opts("object"),
4402 user_creatable_add_opts_foreach,
4403 object_create_initial, NULL)) {
4404 exit(1);
4407 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4408 chardev_init_func, NULL, NULL)) {
4409 exit(1);
4412 #ifdef CONFIG_VIRTFS
4413 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4414 fsdev_init_func, NULL, NULL)) {
4415 exit(1);
4417 #endif
4419 if (qemu_opts_foreach(qemu_find_opts("device"),
4420 device_help_func, NULL, NULL)) {
4421 exit(0);
4424 machine_opts = qemu_get_machine_opts();
4425 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4426 NULL)) {
4427 object_unref(OBJECT(current_machine));
4428 exit(1);
4431 configure_accelerator(current_machine);
4434 * Register all the global properties, including accel properties,
4435 * machine properties, and user-specified ones.
4437 register_global_properties(current_machine);
4440 * Migration object can only be created after global properties
4441 * are applied correctly.
4443 migration_object_init();
4445 if (qtest_chrdev) {
4446 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4449 machine_opts = qemu_get_machine_opts();
4450 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4451 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4452 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4453 bios_name = qemu_opt_get(machine_opts, "firmware");
4455 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4456 if (opts) {
4457 boot_order = qemu_opt_get(opts, "order");
4458 if (boot_order) {
4459 validate_bootdevices(boot_order, &error_fatal);
4462 boot_once = qemu_opt_get(opts, "once");
4463 if (boot_once) {
4464 validate_bootdevices(boot_once, &error_fatal);
4467 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4468 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4471 if (!boot_order) {
4472 boot_order = machine_class->default_boot_order;
4475 if (!kernel_cmdline) {
4476 kernel_cmdline = "";
4477 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4480 linux_boot = (kernel_filename != NULL);
4482 if (!linux_boot && *kernel_cmdline != '\0') {
4483 error_report("-append only allowed with -kernel option");
4484 exit(1);
4487 if (!linux_boot && initrd_filename != NULL) {
4488 error_report("-initrd only allowed with -kernel option");
4489 exit(1);
4492 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4493 /* fall back to the -kernel/-append */
4494 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4497 os_set_line_buffering();
4499 /* spice needs the timers to be initialized by this point */
4500 qemu_spice_init();
4502 cpu_ticks_init();
4503 if (icount_opts) {
4504 if (!tcg_enabled()) {
4505 error_report("-icount is not allowed with hardware virtualization");
4506 exit(1);
4508 configure_icount(icount_opts, &error_abort);
4509 qemu_opts_del(icount_opts);
4512 if (tcg_enabled()) {
4513 qemu_tcg_configure(accel_opts, &error_fatal);
4516 if (default_net) {
4517 QemuOptsList *net = qemu_find_opts("net");
4518 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4519 #ifdef CONFIG_SLIRP
4520 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4521 #endif
4524 colo_info_init();
4526 if (net_init_clients(&err) < 0) {
4527 error_report_err(err);
4528 exit(1);
4531 if (qemu_opts_foreach(qemu_find_opts("object"),
4532 user_creatable_add_opts_foreach,
4533 object_create_delayed, NULL)) {
4534 exit(1);
4537 if (tpm_init() < 0) {
4538 exit(1);
4541 /* init the bluetooth world */
4542 if (foreach_device_config(DEV_BT, bt_parse))
4543 exit(1);
4545 if (!xen_enabled()) {
4546 /* On 32-bit hosts, QEMU is limited by virtual address space */
4547 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4548 error_report("at most 2047 MB RAM can be simulated");
4549 exit(1);
4553 blk_mig_init();
4554 ram_mig_init();
4555 dirty_bitmap_mig_init();
4557 /* If the currently selected machine wishes to override the units-per-bus
4558 * property of its default HBA interface type, do so now. */
4559 if (machine_class->units_per_default_bus) {
4560 override_max_devs(machine_class->block_default_type,
4561 machine_class->units_per_default_bus);
4564 /* open the virtual block devices */
4565 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4566 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4568 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4569 loc_push_restore(&bdo->loc);
4570 qmp_blockdev_add(bdo->bdo, &error_fatal);
4571 loc_pop(&bdo->loc);
4572 qapi_free_BlockdevOptions(bdo->bdo);
4573 g_free(bdo);
4575 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4576 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4577 NULL, NULL);
4579 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4580 &machine_class->block_default_type, NULL)) {
4581 exit(1);
4584 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4585 CDROM_OPTS);
4586 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4587 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4589 if (qemu_opts_foreach(qemu_find_opts("mon"),
4590 mon_init_func, NULL, NULL)) {
4591 exit(1);
4594 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4595 exit(1);
4596 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4597 exit(1);
4598 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4599 exit(1);
4600 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4601 exit(1);
4603 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4604 exit(1);
4606 /* If no default VGA is requested, the default is "none". */
4607 if (default_vga) {
4608 if (machine_class->default_display) {
4609 vga_model = machine_class->default_display;
4610 } else if (vga_interface_available(VGA_CIRRUS)) {
4611 vga_model = "cirrus";
4612 } else if (vga_interface_available(VGA_STD)) {
4613 vga_model = "std";
4616 if (vga_model) {
4617 select_vgahw(vga_model);
4620 if (watchdog) {
4621 i = select_watchdog(watchdog);
4622 if (i > 0)
4623 exit (i == 1 ? 1 : 0);
4626 /* This checkpoint is required by replay to separate prior clock
4627 reading from the other reads, because timer polling functions query
4628 clock values from the log. */
4629 replay_checkpoint(CHECKPOINT_INIT);
4630 qdev_machine_init();
4632 current_machine->ram_size = ram_size;
4633 current_machine->maxram_size = maxram_size;
4634 current_machine->ram_slots = ram_slots;
4635 current_machine->boot_order = boot_order;
4636 current_machine->cpu_model = cpu_model;
4638 /* parse features once if machine provides default cpu_type */
4639 if (machine_class->default_cpu_type) {
4640 current_machine->cpu_type = machine_class->default_cpu_type;
4641 if (cpu_model) {
4642 current_machine->cpu_type =
4643 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
4646 parse_numa_opts(current_machine);
4648 machine_run_board_init(current_machine);
4650 realtime_init();
4652 soundhw_init();
4654 if (hax_enabled()) {
4655 hax_sync_vcpus();
4658 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4659 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4660 exit(1);
4663 /* init USB devices */
4664 if (machine_usb(current_machine)) {
4665 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4666 exit(1);
4669 /* Check if IGD GFX passthrough. */
4670 igd_gfx_passthru();
4672 /* init generic devices */
4673 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4674 if (qemu_opts_foreach(qemu_find_opts("device"),
4675 device_init_func, NULL, NULL)) {
4676 exit(1);
4679 cpu_synchronize_all_post_init();
4681 rom_reset_order_override();
4683 /* Did we create any drives that we failed to create a device for? */
4684 drive_check_orphaned();
4686 /* Don't warn about the default network setup that you get if
4687 * no command line -net or -netdev options are specified. There
4688 * are two cases that we would otherwise complain about:
4689 * (1) board doesn't support a NIC but the implicit "-net nic"
4690 * requested one
4691 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4692 * sets up a nic that isn't connected to anything.
4694 if (!default_net) {
4695 net_check_clients();
4699 if (boot_once) {
4700 qemu_boot_set(boot_once, &error_fatal);
4701 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4704 /* init local displays */
4705 ds = init_displaystate();
4706 qemu_display_init(ds, &dpy);
4708 /* must be after terminal init, SDL library changes signal handlers */
4709 os_setup_signal_handling();
4711 /* init remote displays */
4712 #ifdef CONFIG_VNC
4713 qemu_opts_foreach(qemu_find_opts("vnc"),
4714 vnc_init_func, NULL, NULL);
4715 #endif
4717 if (using_spice) {
4718 qemu_spice_display_init();
4721 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4722 exit(1);
4725 qdev_machine_creation_done();
4727 /* TODO: once all bus devices are qdevified, this should be done
4728 * when bus is created by qdev.c */
4729 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4730 qemu_run_machine_init_done_notifiers();
4732 if (rom_check_and_register_reset() != 0) {
4733 error_report("rom check and register reset failed");
4734 exit(1);
4737 replay_start();
4739 /* This checkpoint is required by replay to separate prior clock
4740 reading from the other reads, because timer polling functions query
4741 clock values from the log. */
4742 replay_checkpoint(CHECKPOINT_RESET);
4743 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4744 register_global_state();
4745 if (replay_mode != REPLAY_MODE_NONE) {
4746 replay_vmstate_init();
4747 } else if (loadvm) {
4748 Error *local_err = NULL;
4749 if (load_snapshot(loadvm, &local_err) < 0) {
4750 error_report_err(local_err);
4751 autostart = 0;
4755 qdev_prop_check_globals();
4756 if (vmstate_dump_file) {
4757 /* dump and exit */
4758 dump_vmstate_json_to_file(vmstate_dump_file);
4759 return 0;
4762 if (incoming) {
4763 Error *local_err = NULL;
4764 qemu_start_incoming_migration(incoming, &local_err);
4765 if (local_err) {
4766 error_reportf_err(local_err, "-incoming %s: ", incoming);
4767 exit(1);
4769 } else if (autostart) {
4770 vm_start();
4773 os_setup_post();
4775 main_loop();
4777 /* No more vcpu or device emulation activity beyond this point */
4778 vm_shutdown();
4780 bdrv_close_all();
4782 res_free();
4784 /* vhost-user must be cleaned up before chardevs. */
4785 tpm_cleanup();
4786 net_cleanup();
4787 audio_cleanup();
4788 monitor_cleanup();
4789 qemu_chr_cleanup();
4790 user_creatable_cleanup();
4791 migration_object_finalize();
4792 /* TODO: unref root container, check all devices are ok */
4794 return 0;