oslib-posix: Fix compiler warning
[qemu/ar7.git] / vl.c
blob7e76a3c1ab095e799159eb25c0dfc30bf87d1dfd
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
28 #include "qemu/uuid.h"
30 #ifdef CONFIG_SECCOMP
31 #include "sysemu/seccomp.h"
32 #include "sys/prctl.h"
33 #endif
35 #ifdef CONFIG_SDL
36 #if defined(__APPLE__) || defined(main)
37 #define SDL_MAIN_HANDLED
38 #include <SDL.h>
39 #if SDL_MAJOR_VERSION == 1
40 int qemu_main(int argc, char **argv);
41 int main(int argc, char **argv)
43 return qemu_main(argc, argv);
45 #undef main
46 #define main qemu_main
47 #elif SDL_MAJOR_VERSION == 2
48 #undef main
49 #else
50 #error undefined SDL version
51 #endif
52 #endif
53 #endif /* CONFIG_SDL */
55 #ifdef CONFIG_COCOA
56 #undef main
57 #define main qemu_main
58 #endif /* CONFIG_COCOA */
61 #include "qemu/error-report.h"
62 #include "qemu/sockets.h"
63 #include "hw/hw.h"
64 #include "hw/boards.h"
65 #include "sysemu/accel.h"
66 #include "hw/usb.h"
67 #include "hw/i386/pc.h"
68 #include "hw/isa/isa.h"
69 #include "hw/scsi/scsi.h"
70 #include "hw/bt.h"
71 #include "sysemu/watchdog.h"
72 #include "hw/smbios/smbios.h"
73 #include "hw/acpi/acpi.h"
74 #include "hw/xen/xen.h"
75 #include "hw/qdev.h"
76 #include "hw/loader.h"
77 #include "monitor/qdev.h"
78 #include "sysemu/bt.h"
79 #include "net/net.h"
80 #include "net/slirp.h"
81 #include "monitor/monitor.h"
82 #include "ui/console.h"
83 #include "ui/input.h"
84 #include "sysemu/sysemu.h"
85 #include "sysemu/numa.h"
86 #include "exec/gdbstub.h"
87 #include "qemu/timer.h"
88 #include "chardev/char.h"
89 #include "qemu/bitmap.h"
90 #include "qemu/log.h"
91 #include "sysemu/blockdev.h"
92 #include "hw/block/block.h"
93 #include "migration/misc.h"
94 #include "migration/snapshot.h"
95 #include "migration/global_state.h"
96 #include "sysemu/tpm.h"
97 #include "sysemu/dma.h"
98 #include "hw/audio/soundhw.h"
99 #include "audio/audio.h"
100 #include "sysemu/cpus.h"
101 #include "migration/colo.h"
102 #include "sysemu/kvm.h"
103 #include "sysemu/hax.h"
104 #include "qapi/qobject-input-visitor.h"
105 #include "qapi/qobject-input-visitor.h"
106 #include "qapi-visit.h"
107 #include "qapi/qmp/qjson.h"
108 #include "qemu/option.h"
109 #include "qemu/config-file.h"
110 #include "qemu-options.h"
111 #include "qmp-commands.h"
112 #include "qemu/main-loop.h"
113 #ifdef CONFIG_VIRTFS
114 #include "fsdev/qemu-fsdev.h"
115 #endif
116 #include "sysemu/qtest.h"
118 #include "disas/disas.h"
121 #include "slirp/libslirp.h"
123 #include "trace-root.h"
124 #include "trace/control.h"
125 #include "qemu/queue.h"
126 #include "sysemu/arch_init.h"
128 #include "ui/qemu-spice.h"
129 #include "qapi/string-input-visitor.h"
130 #include "qapi/opts-visitor.h"
131 #include "qom/object_interfaces.h"
132 #include "qapi-event.h"
133 #include "exec/semihost.h"
134 #include "crypto/init.h"
135 #include "sysemu/replay.h"
136 #include "qapi/qmp/qerror.h"
137 #include "sysemu/iothread.h"
139 #define MAX_VIRTIO_CONSOLES 1
140 #define MAX_SCLP_CONSOLES 1
142 static const char *data_dir[16];
143 static int data_dir_idx;
144 const char *bios_name = NULL;
145 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
146 int request_opengl = -1;
147 int display_opengl;
148 const char* keyboard_layout = NULL;
149 ram_addr_t ram_size;
150 const char *mem_path = NULL;
151 int mem_prealloc = 0; /* force preallocation of physical target memory */
152 bool enable_mlock = false;
153 int nb_nics;
154 NICInfo nd_table[MAX_NICS];
155 int autostart;
156 static int rtc_utc = 1;
157 static int rtc_date_offset = -1; /* -1 means no change */
158 QEMUClockType rtc_clock;
159 int vga_interface_type = VGA_NONE;
160 static int full_screen = 0;
161 static int no_frame = 0;
162 int no_quit = 0;
163 static bool grab_on_hover;
164 Chardev *serial_hds[MAX_SERIAL_PORTS];
165 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
166 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
167 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
168 int win2k_install_hack = 0;
169 int singlestep = 0;
170 int smp_cpus = 1;
171 int max_cpus = 1;
172 int smp_cores = 1;
173 int smp_threads = 1;
174 int acpi_enabled = 1;
175 int no_hpet = 0;
176 int fd_bootchk = 1;
177 static int no_reboot;
178 int no_shutdown = 0;
179 int cursor_hide = 1;
180 int graphic_rotate = 0;
181 const char *watchdog;
182 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
183 int nb_option_roms;
184 int old_param = 0;
185 const char *qemu_name;
186 int alt_grab = 0;
187 int ctrl_grab = 0;
188 unsigned int nb_prom_envs = 0;
189 const char *prom_envs[MAX_PROM_ENVS];
190 int boot_menu;
191 bool boot_strict;
192 uint8_t *boot_splash_filedata;
193 size_t boot_splash_filedata_size;
194 uint8_t qemu_extra_params_fw[2];
196 int icount_align_option;
198 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
199 * little-endian "wire format" described in the SMBIOS 2.6 specification.
201 QemuUUID qemu_uuid;
202 bool qemu_uuid_set;
204 /* Trace unassigned memory or i/o accesses. */
205 bool trace_unassigned;
207 static NotifierList exit_notifiers =
208 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
210 static NotifierList machine_init_done_notifiers =
211 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
213 bool xen_allowed;
214 uint32_t xen_domid;
215 enum xen_mode xen_mode = XEN_EMULATE;
216 bool xen_domid_restrict;
218 static int has_defaults = 1;
219 static int default_serial = 1;
220 static int default_parallel = 1;
221 static int default_virtcon = 1;
222 static int default_sclp = 1;
223 static int default_monitor = 1;
224 static int default_floppy = 1;
225 static int default_cdrom = 1;
226 static int default_sdcard = 1;
227 static int default_vga = 1;
228 static int default_net = 1;
230 static struct {
231 const char *driver;
232 int *flag;
233 } default_list[] = {
234 { .driver = "isa-serial", .flag = &default_serial },
235 { .driver = "isa-parallel", .flag = &default_parallel },
236 { .driver = "isa-fdc", .flag = &default_floppy },
237 { .driver = "floppy", .flag = &default_floppy },
238 { .driver = "ide-cd", .flag = &default_cdrom },
239 { .driver = "ide-hd", .flag = &default_cdrom },
240 { .driver = "ide-drive", .flag = &default_cdrom },
241 { .driver = "scsi-cd", .flag = &default_cdrom },
242 { .driver = "scsi-hd", .flag = &default_cdrom },
243 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
244 { .driver = "virtio-serial", .flag = &default_virtcon },
245 { .driver = "VGA", .flag = &default_vga },
246 { .driver = "isa-vga", .flag = &default_vga },
247 { .driver = "cirrus-vga", .flag = &default_vga },
248 { .driver = "isa-cirrus-vga", .flag = &default_vga },
249 { .driver = "vmware-svga", .flag = &default_vga },
250 { .driver = "qxl-vga", .flag = &default_vga },
251 { .driver = "virtio-vga", .flag = &default_vga },
254 static QemuOptsList qemu_rtc_opts = {
255 .name = "rtc",
256 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
257 .desc = {
259 .name = "base",
260 .type = QEMU_OPT_STRING,
262 .name = "clock",
263 .type = QEMU_OPT_STRING,
265 .name = "driftfix",
266 .type = QEMU_OPT_STRING,
268 { /* end of list */ }
272 static QemuOptsList qemu_sandbox_opts = {
273 .name = "sandbox",
274 .implied_opt_name = "enable",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
276 .desc = {
278 .name = "enable",
279 .type = QEMU_OPT_BOOL,
282 .name = "obsolete",
283 .type = QEMU_OPT_STRING,
286 .name = "elevateprivileges",
287 .type = QEMU_OPT_STRING,
290 .name = "spawn",
291 .type = QEMU_OPT_STRING,
294 .name = "resourcecontrol",
295 .type = QEMU_OPT_STRING,
297 { /* end of list */ }
301 static QemuOptsList qemu_option_rom_opts = {
302 .name = "option-rom",
303 .implied_opt_name = "romfile",
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
305 .desc = {
307 .name = "bootindex",
308 .type = QEMU_OPT_NUMBER,
309 }, {
310 .name = "romfile",
311 .type = QEMU_OPT_STRING,
313 { /* end of list */ }
317 static QemuOptsList qemu_machine_opts = {
318 .name = "machine",
319 .implied_opt_name = "type",
320 .merge_lists = true,
321 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
322 .desc = {
324 * no elements => accept any
325 * sanity checking will happen later
326 * when setting machine properties
332 static QemuOptsList qemu_accel_opts = {
333 .name = "accel",
334 .implied_opt_name = "accel",
335 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
336 .merge_lists = true,
337 .desc = {
339 .name = "accel",
340 .type = QEMU_OPT_STRING,
341 .help = "Select the type of accelerator",
344 .name = "thread",
345 .type = QEMU_OPT_STRING,
346 .help = "Enable/disable multi-threaded TCG",
348 { /* end of list */ }
352 static QemuOptsList qemu_boot_opts = {
353 .name = "boot-opts",
354 .implied_opt_name = "order",
355 .merge_lists = true,
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
357 .desc = {
359 .name = "order",
360 .type = QEMU_OPT_STRING,
361 }, {
362 .name = "once",
363 .type = QEMU_OPT_STRING,
364 }, {
365 .name = "menu",
366 .type = QEMU_OPT_BOOL,
367 }, {
368 .name = "splash",
369 .type = QEMU_OPT_STRING,
370 }, {
371 .name = "splash-time",
372 .type = QEMU_OPT_STRING,
373 }, {
374 .name = "reboot-timeout",
375 .type = QEMU_OPT_STRING,
376 }, {
377 .name = "strict",
378 .type = QEMU_OPT_BOOL,
380 { /*End of list */ }
384 static QemuOptsList qemu_add_fd_opts = {
385 .name = "add-fd",
386 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
387 .desc = {
389 .name = "fd",
390 .type = QEMU_OPT_NUMBER,
391 .help = "file descriptor of which a duplicate is added to fd set",
393 .name = "set",
394 .type = QEMU_OPT_NUMBER,
395 .help = "ID of the fd set to add fd to",
397 .name = "opaque",
398 .type = QEMU_OPT_STRING,
399 .help = "free-form string used to describe fd",
401 { /* end of list */ }
405 static QemuOptsList qemu_object_opts = {
406 .name = "object",
407 .implied_opt_name = "qom-type",
408 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
409 .desc = {
414 static QemuOptsList qemu_tpmdev_opts = {
415 .name = "tpmdev",
416 .implied_opt_name = "type",
417 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
418 .desc = {
419 /* options are defined in the TPM backends */
420 { /* end of list */ }
424 static QemuOptsList qemu_realtime_opts = {
425 .name = "realtime",
426 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
427 .desc = {
429 .name = "mlock",
430 .type = QEMU_OPT_BOOL,
432 { /* end of list */ }
436 static QemuOptsList qemu_msg_opts = {
437 .name = "msg",
438 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
439 .desc = {
441 .name = "timestamp",
442 .type = QEMU_OPT_BOOL,
444 { /* end of list */ }
448 static QemuOptsList qemu_name_opts = {
449 .name = "name",
450 .implied_opt_name = "guest",
451 .merge_lists = true,
452 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
453 .desc = {
455 .name = "guest",
456 .type = QEMU_OPT_STRING,
457 .help = "Sets the name of the guest.\n"
458 "This name will be displayed in the SDL window caption.\n"
459 "The name will also be used for the VNC server",
460 }, {
461 .name = "process",
462 .type = QEMU_OPT_STRING,
463 .help = "Sets the name of the QEMU process, as shown in top etc",
464 }, {
465 .name = "debug-threads",
466 .type = QEMU_OPT_BOOL,
467 .help = "When enabled, name the individual threads; defaults off.\n"
468 "NOTE: The thread names are for debugging and not a\n"
469 "stable API.",
471 { /* End of list */ }
475 static QemuOptsList qemu_mem_opts = {
476 .name = "memory",
477 .implied_opt_name = "size",
478 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
479 .merge_lists = true,
480 .desc = {
482 .name = "size",
483 .type = QEMU_OPT_SIZE,
486 .name = "slots",
487 .type = QEMU_OPT_NUMBER,
490 .name = "maxmem",
491 .type = QEMU_OPT_SIZE,
493 { /* end of list */ }
497 static QemuOptsList qemu_icount_opts = {
498 .name = "icount",
499 .implied_opt_name = "shift",
500 .merge_lists = true,
501 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
502 .desc = {
504 .name = "shift",
505 .type = QEMU_OPT_STRING,
506 }, {
507 .name = "align",
508 .type = QEMU_OPT_BOOL,
509 }, {
510 .name = "sleep",
511 .type = QEMU_OPT_BOOL,
512 }, {
513 .name = "rr",
514 .type = QEMU_OPT_STRING,
515 }, {
516 .name = "rrfile",
517 .type = QEMU_OPT_STRING,
518 }, {
519 .name = "rrsnapshot",
520 .type = QEMU_OPT_STRING,
522 { /* end of list */ }
526 static QemuOptsList qemu_semihosting_config_opts = {
527 .name = "semihosting-config",
528 .implied_opt_name = "enable",
529 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
530 .desc = {
532 .name = "enable",
533 .type = QEMU_OPT_BOOL,
534 }, {
535 .name = "target",
536 .type = QEMU_OPT_STRING,
537 }, {
538 .name = "arg",
539 .type = QEMU_OPT_STRING,
541 { /* end of list */ }
545 static QemuOptsList qemu_fw_cfg_opts = {
546 .name = "fw_cfg",
547 .implied_opt_name = "name",
548 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
549 .desc = {
551 .name = "name",
552 .type = QEMU_OPT_STRING,
553 .help = "Sets the fw_cfg name of the blob to be inserted",
554 }, {
555 .name = "file",
556 .type = QEMU_OPT_STRING,
557 .help = "Sets the name of the file from which\n"
558 "the fw_cfg blob will be loaded",
559 }, {
560 .name = "string",
561 .type = QEMU_OPT_STRING,
562 .help = "Sets content of the blob to be inserted from a string",
564 { /* end of list */ }
569 * Get machine options
571 * Returns: machine options (never null).
573 QemuOpts *qemu_get_machine_opts(void)
575 return qemu_find_opts_singleton("machine");
578 const char *qemu_get_vm_name(void)
580 return qemu_name;
583 static void res_free(void)
585 g_free(boot_splash_filedata);
586 boot_splash_filedata = NULL;
589 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
591 const char *driver = qemu_opt_get(opts, "driver");
592 int i;
594 if (!driver)
595 return 0;
596 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
597 if (strcmp(default_list[i].driver, driver) != 0)
598 continue;
599 *(default_list[i].flag) = 0;
601 return 0;
604 /***********************************************************/
605 /* QEMU state */
607 static RunState current_run_state = RUN_STATE_PRELAUNCH;
609 /* We use RUN_STATE__MAX but any invalid value will do */
610 static RunState vmstop_requested = RUN_STATE__MAX;
611 static QemuMutex vmstop_lock;
613 typedef struct {
614 RunState from;
615 RunState to;
616 } RunStateTransition;
618 static const RunStateTransition runstate_transitions_def[] = {
619 /* from -> to */
620 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
621 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
625 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
626 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
627 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
629 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
630 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
631 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
632 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
633 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
635 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
637 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
638 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
641 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
642 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
645 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
646 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
648 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
649 { RUN_STATE_PAUSED, RUN_STATE_COLO},
651 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
652 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
655 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
656 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
659 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
660 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
663 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
665 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
666 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
668 { RUN_STATE_COLO, RUN_STATE_RUNNING },
670 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
671 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
672 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
673 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
674 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
676 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
677 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
678 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
679 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
680 { RUN_STATE_RUNNING, RUN_STATE_COLO},
682 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
684 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
685 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
686 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
688 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
689 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
690 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
691 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
692 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
693 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
695 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
696 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
697 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
698 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
700 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
701 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
702 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
704 { RUN_STATE__MAX, RUN_STATE__MAX },
707 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
709 bool runstate_check(RunState state)
711 return current_run_state == state;
714 bool runstate_store(char *str, size_t size)
716 const char *state = RunState_str(current_run_state);
717 size_t len = strlen(state) + 1;
719 if (len > size) {
720 return false;
722 memcpy(str, state, len);
723 return true;
726 static void runstate_init(void)
728 const RunStateTransition *p;
730 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
731 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
732 runstate_valid_transitions[p->from][p->to] = true;
735 qemu_mutex_init(&vmstop_lock);
738 /* This function will abort() on invalid state transitions */
739 void runstate_set(RunState new_state)
741 assert(new_state < RUN_STATE__MAX);
743 if (current_run_state == new_state) {
744 return;
747 if (!runstate_valid_transitions[current_run_state][new_state]) {
748 error_report("invalid runstate transition: '%s' -> '%s'",
749 RunState_str(current_run_state),
750 RunState_str(new_state));
751 abort();
753 trace_runstate_set(new_state);
754 current_run_state = new_state;
757 int runstate_is_running(void)
759 return runstate_check(RUN_STATE_RUNNING);
762 bool runstate_needs_reset(void)
764 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
765 runstate_check(RUN_STATE_SHUTDOWN);
768 StatusInfo *qmp_query_status(Error **errp)
770 StatusInfo *info = g_malloc0(sizeof(*info));
772 info->running = runstate_is_running();
773 info->singlestep = singlestep;
774 info->status = current_run_state;
776 return info;
779 bool qemu_vmstop_requested(RunState *r)
781 qemu_mutex_lock(&vmstop_lock);
782 *r = vmstop_requested;
783 vmstop_requested = RUN_STATE__MAX;
784 qemu_mutex_unlock(&vmstop_lock);
785 return *r < RUN_STATE__MAX;
788 void qemu_system_vmstop_request_prepare(void)
790 qemu_mutex_lock(&vmstop_lock);
793 void qemu_system_vmstop_request(RunState state)
795 vmstop_requested = state;
796 qemu_mutex_unlock(&vmstop_lock);
797 qemu_notify_event();
800 /***********************************************************/
801 /* real time host monotonic timer */
803 static time_t qemu_time(void)
805 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
808 /***********************************************************/
809 /* host time/date access */
810 void qemu_get_timedate(struct tm *tm, int offset)
812 time_t ti = qemu_time();
814 ti += offset;
815 if (rtc_date_offset == -1) {
816 if (rtc_utc)
817 gmtime_r(&ti, tm);
818 else
819 localtime_r(&ti, tm);
820 } else {
821 ti -= rtc_date_offset;
822 gmtime_r(&ti, tm);
826 int qemu_timedate_diff(struct tm *tm)
828 time_t seconds;
830 if (rtc_date_offset == -1)
831 if (rtc_utc)
832 seconds = mktimegm(tm);
833 else {
834 struct tm tmp = *tm;
835 tmp.tm_isdst = -1; /* use timezone to figure it out */
836 seconds = mktime(&tmp);
838 else
839 seconds = mktimegm(tm) + rtc_date_offset;
841 return seconds - qemu_time();
844 static void configure_rtc_date_offset(const char *startdate, int legacy)
846 time_t rtc_start_date;
847 struct tm tm;
849 if (!strcmp(startdate, "now") && legacy) {
850 rtc_date_offset = -1;
851 } else {
852 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
853 &tm.tm_year,
854 &tm.tm_mon,
855 &tm.tm_mday,
856 &tm.tm_hour,
857 &tm.tm_min,
858 &tm.tm_sec) == 6) {
859 /* OK */
860 } else if (sscanf(startdate, "%d-%d-%d",
861 &tm.tm_year,
862 &tm.tm_mon,
863 &tm.tm_mday) == 3) {
864 tm.tm_hour = 0;
865 tm.tm_min = 0;
866 tm.tm_sec = 0;
867 } else {
868 goto date_fail;
870 tm.tm_year -= 1900;
871 tm.tm_mon--;
872 rtc_start_date = mktimegm(&tm);
873 if (rtc_start_date == -1) {
874 date_fail:
875 error_report("invalid date format");
876 error_printf("valid formats: "
877 "'2006-06-17T16:01:21' or '2006-06-17'\n");
878 exit(1);
880 rtc_date_offset = qemu_time() - rtc_start_date;
884 static void configure_rtc(QemuOpts *opts)
886 const char *value;
888 value = qemu_opt_get(opts, "base");
889 if (value) {
890 if (!strcmp(value, "utc")) {
891 rtc_utc = 1;
892 } else if (!strcmp(value, "localtime")) {
893 Error *blocker = NULL;
894 rtc_utc = 0;
895 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
896 "-rtc base=localtime");
897 replay_add_blocker(blocker);
898 } else {
899 configure_rtc_date_offset(value, 0);
902 value = qemu_opt_get(opts, "clock");
903 if (value) {
904 if (!strcmp(value, "host")) {
905 rtc_clock = QEMU_CLOCK_HOST;
906 } else if (!strcmp(value, "rt")) {
907 rtc_clock = QEMU_CLOCK_REALTIME;
908 } else if (!strcmp(value, "vm")) {
909 rtc_clock = QEMU_CLOCK_VIRTUAL;
910 } else {
911 error_report("invalid option value '%s'", value);
912 exit(1);
915 value = qemu_opt_get(opts, "driftfix");
916 if (value) {
917 if (!strcmp(value, "slew")) {
918 static GlobalProperty slew_lost_ticks = {
919 .driver = "mc146818rtc",
920 .property = "lost_tick_policy",
921 .value = "slew",
924 qdev_prop_register_global(&slew_lost_ticks);
925 } else if (!strcmp(value, "none")) {
926 /* discard is default */
927 } else {
928 error_report("invalid option value '%s'", value);
929 exit(1);
934 /***********************************************************/
935 /* Bluetooth support */
936 static int nb_hcis;
937 static int cur_hci;
938 static struct HCIInfo *hci_table[MAX_NICS];
940 struct HCIInfo *qemu_next_hci(void)
942 if (cur_hci == nb_hcis)
943 return &null_hci;
945 return hci_table[cur_hci++];
948 static int bt_hci_parse(const char *str)
950 struct HCIInfo *hci;
951 bdaddr_t bdaddr;
953 if (nb_hcis >= MAX_NICS) {
954 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
955 return -1;
958 hci = hci_init(str);
959 if (!hci)
960 return -1;
962 bdaddr.b[0] = 0x52;
963 bdaddr.b[1] = 0x54;
964 bdaddr.b[2] = 0x00;
965 bdaddr.b[3] = 0x12;
966 bdaddr.b[4] = 0x34;
967 bdaddr.b[5] = 0x56 + nb_hcis;
968 hci->bdaddr_set(hci, bdaddr.b);
970 hci_table[nb_hcis++] = hci;
972 return 0;
975 static void bt_vhci_add(int vlan_id)
977 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
979 if (!vlan->slave)
980 warn_report("adding a VHCI to an empty scatternet %i",
981 vlan_id);
983 bt_vhci_init(bt_new_hci(vlan));
986 static struct bt_device_s *bt_device_add(const char *opt)
988 struct bt_scatternet_s *vlan;
989 int vlan_id = 0;
990 char *endp = strstr(opt, ",vlan=");
991 int len = (endp ? endp - opt : strlen(opt)) + 1;
992 char devname[10];
994 pstrcpy(devname, MIN(sizeof(devname), len), opt);
996 if (endp) {
997 vlan_id = strtol(endp + 6, &endp, 0);
998 if (*endp) {
999 error_report("unrecognised bluetooth vlan Id");
1000 return 0;
1004 vlan = qemu_find_bt_vlan(vlan_id);
1006 if (!vlan->slave)
1007 warn_report("adding a slave device to an empty scatternet %i",
1008 vlan_id);
1010 if (!strcmp(devname, "keyboard"))
1011 return bt_keyboard_init(vlan);
1013 error_report("unsupported bluetooth device '%s'", devname);
1014 return 0;
1017 static int bt_parse(const char *opt)
1019 const char *endp, *p;
1020 int vlan;
1022 if (strstart(opt, "hci", &endp)) {
1023 if (!*endp || *endp == ',') {
1024 if (*endp)
1025 if (!strstart(endp, ",vlan=", 0))
1026 opt = endp + 1;
1028 return bt_hci_parse(opt);
1030 } else if (strstart(opt, "vhci", &endp)) {
1031 if (!*endp || *endp == ',') {
1032 if (*endp) {
1033 if (strstart(endp, ",vlan=", &p)) {
1034 vlan = strtol(p, (char **) &endp, 0);
1035 if (*endp) {
1036 error_report("bad scatternet '%s'", p);
1037 return 1;
1039 } else {
1040 error_report("bad parameter '%s'", endp + 1);
1041 return 1;
1043 } else
1044 vlan = 0;
1046 bt_vhci_add(vlan);
1047 return 0;
1049 } else if (strstart(opt, "device:", &endp))
1050 return !bt_device_add(endp);
1052 error_report("bad bluetooth parameter '%s'", opt);
1053 return 1;
1056 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1058 if (qemu_opt_get_bool(opts, "enable", false)) {
1059 #ifdef CONFIG_SECCOMP
1060 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1061 | QEMU_SECCOMP_SET_OBSOLETE;
1062 const char *value = NULL;
1064 value = qemu_opt_get(opts, "obsolete");
1065 if (value) {
1066 if (g_str_equal(value, "allow")) {
1067 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1068 } else if (g_str_equal(value, "deny")) {
1069 /* this is the default option, this if is here
1070 * to provide a little bit of consistency for
1071 * the command line */
1072 } else {
1073 error_report("invalid argument for obsolete");
1074 return -1;
1078 value = qemu_opt_get(opts, "elevateprivileges");
1079 if (value) {
1080 if (g_str_equal(value, "deny")) {
1081 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1082 } else if (g_str_equal(value, "children")) {
1083 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1085 /* calling prctl directly because we're
1086 * not sure if host has CAP_SYS_ADMIN set*/
1087 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1088 error_report("failed to set no_new_privs "
1089 "aborting");
1090 return -1;
1092 } else if (g_str_equal(value, "allow")) {
1093 /* default value */
1094 } else {
1095 error_report("invalid argument for elevateprivileges");
1096 return -1;
1100 value = qemu_opt_get(opts, "spawn");
1101 if (value) {
1102 if (g_str_equal(value, "deny")) {
1103 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1104 } else if (g_str_equal(value, "allow")) {
1105 /* default value */
1106 } else {
1107 error_report("invalid argument for spawn");
1108 return -1;
1112 value = qemu_opt_get(opts, "resourcecontrol");
1113 if (value) {
1114 if (g_str_equal(value, "deny")) {
1115 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1116 } else if (g_str_equal(value, "allow")) {
1117 /* default value */
1118 } else {
1119 error_report("invalid argument for resourcecontrol");
1120 return -1;
1124 if (seccomp_start(seccomp_opts) < 0) {
1125 error_report("failed to install seccomp syscall filter "
1126 "in the kernel");
1127 return -1;
1129 #else
1130 error_report("seccomp support is disabled");
1131 return -1;
1132 #endif
1135 return 0;
1138 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1140 const char *proc_name;
1142 if (qemu_opt_get(opts, "debug-threads")) {
1143 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1145 qemu_name = qemu_opt_get(opts, "guest");
1147 proc_name = qemu_opt_get(opts, "process");
1148 if (proc_name) {
1149 os_set_proc_name(proc_name);
1152 return 0;
1155 bool defaults_enabled(void)
1157 return has_defaults;
1160 #ifndef _WIN32
1161 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1163 int fd, dupfd, flags;
1164 int64_t fdset_id;
1165 const char *fd_opaque = NULL;
1166 AddfdInfo *fdinfo;
1168 fd = qemu_opt_get_number(opts, "fd", -1);
1169 fdset_id = qemu_opt_get_number(opts, "set", -1);
1170 fd_opaque = qemu_opt_get(opts, "opaque");
1172 if (fd < 0) {
1173 error_report("fd option is required and must be non-negative");
1174 return -1;
1177 if (fd <= STDERR_FILENO) {
1178 error_report("fd cannot be a standard I/O stream");
1179 return -1;
1183 * All fds inherited across exec() necessarily have FD_CLOEXEC
1184 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1186 flags = fcntl(fd, F_GETFD);
1187 if (flags == -1 || (flags & FD_CLOEXEC)) {
1188 error_report("fd is not valid or already in use");
1189 return -1;
1192 if (fdset_id < 0) {
1193 error_report("set option is required and must be non-negative");
1194 return -1;
1197 #ifdef F_DUPFD_CLOEXEC
1198 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1199 #else
1200 dupfd = dup(fd);
1201 if (dupfd != -1) {
1202 qemu_set_cloexec(dupfd);
1204 #endif
1205 if (dupfd == -1) {
1206 error_report("error duplicating fd: %s", strerror(errno));
1207 return -1;
1210 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1211 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1212 &error_abort);
1213 g_free(fdinfo);
1215 return 0;
1218 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1220 int fd;
1222 fd = qemu_opt_get_number(opts, "fd", -1);
1223 close(fd);
1225 return 0;
1227 #endif
1229 /***********************************************************/
1230 /* QEMU Block devices */
1232 #define HD_OPTS "media=disk"
1233 #define CDROM_OPTS "media=cdrom"
1234 #define FD_OPTS ""
1235 #define PFLASH_OPTS ""
1236 #define MTD_OPTS ""
1237 #define SD_OPTS ""
1239 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1241 BlockInterfaceType *block_default_type = opaque;
1243 return drive_new(opts, *block_default_type) == NULL;
1246 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1248 if (qemu_opt_get(opts, "snapshot") == NULL) {
1249 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1251 return 0;
1254 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1255 int index, const char *optstr)
1257 QemuOpts *opts;
1258 DriveInfo *dinfo;
1260 if (!enable || drive_get_by_index(type, index)) {
1261 return;
1264 opts = drive_add(type, index, NULL, optstr);
1265 if (snapshot) {
1266 drive_enable_snapshot(NULL, opts, NULL);
1269 dinfo = drive_new(opts, type);
1270 if (!dinfo) {
1271 exit(1);
1273 dinfo->is_default = true;
1277 static QemuOptsList qemu_smp_opts = {
1278 .name = "smp-opts",
1279 .implied_opt_name = "cpus",
1280 .merge_lists = true,
1281 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1282 .desc = {
1284 .name = "cpus",
1285 .type = QEMU_OPT_NUMBER,
1286 }, {
1287 .name = "sockets",
1288 .type = QEMU_OPT_NUMBER,
1289 }, {
1290 .name = "cores",
1291 .type = QEMU_OPT_NUMBER,
1292 }, {
1293 .name = "threads",
1294 .type = QEMU_OPT_NUMBER,
1295 }, {
1296 .name = "maxcpus",
1297 .type = QEMU_OPT_NUMBER,
1299 { /*End of list */ }
1303 static void smp_parse(QemuOpts *opts)
1305 if (opts) {
1306 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1307 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1308 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1309 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1311 /* compute missing values, prefer sockets over cores over threads */
1312 if (cpus == 0 || sockets == 0) {
1313 sockets = sockets > 0 ? sockets : 1;
1314 cores = cores > 0 ? cores : 1;
1315 threads = threads > 0 ? threads : 1;
1316 if (cpus == 0) {
1317 cpus = cores * threads * sockets;
1319 } else if (cores == 0) {
1320 threads = threads > 0 ? threads : 1;
1321 cores = cpus / (sockets * threads);
1322 cores = cores > 0 ? cores : 1;
1323 } else if (threads == 0) {
1324 threads = cpus / (cores * sockets);
1325 threads = threads > 0 ? threads : 1;
1326 } else if (sockets * cores * threads < cpus) {
1327 error_report("cpu topology: "
1328 "sockets (%u) * cores (%u) * threads (%u) < "
1329 "smp_cpus (%u)",
1330 sockets, cores, threads, cpus);
1331 exit(1);
1334 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1336 if (max_cpus < cpus) {
1337 error_report("maxcpus must be equal to or greater than smp");
1338 exit(1);
1341 if (sockets * cores * threads > max_cpus) {
1342 error_report("cpu topology: "
1343 "sockets (%u) * cores (%u) * threads (%u) > "
1344 "maxcpus (%u)",
1345 sockets, cores, threads, max_cpus);
1346 exit(1);
1349 smp_cpus = cpus;
1350 smp_cores = cores;
1351 smp_threads = threads;
1354 if (smp_cpus > 1) {
1355 Error *blocker = NULL;
1356 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1357 replay_add_blocker(blocker);
1361 static void realtime_init(void)
1363 if (enable_mlock) {
1364 if (os_mlock() < 0) {
1365 error_report("locking memory failed");
1366 exit(1);
1372 static void configure_msg(QemuOpts *opts)
1374 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1377 /***********************************************************/
1378 /* Semihosting */
1380 typedef struct SemihostingConfig {
1381 bool enabled;
1382 SemihostingTarget target;
1383 const char **argv;
1384 int argc;
1385 const char *cmdline; /* concatenated argv */
1386 } SemihostingConfig;
1388 static SemihostingConfig semihosting;
1390 bool semihosting_enabled(void)
1392 return semihosting.enabled;
1395 SemihostingTarget semihosting_get_target(void)
1397 return semihosting.target;
1400 const char *semihosting_get_arg(int i)
1402 if (i >= semihosting.argc) {
1403 return NULL;
1405 return semihosting.argv[i];
1408 int semihosting_get_argc(void)
1410 return semihosting.argc;
1413 const char *semihosting_get_cmdline(void)
1415 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1416 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1418 return semihosting.cmdline;
1421 static int add_semihosting_arg(void *opaque,
1422 const char *name, const char *val,
1423 Error **errp)
1425 SemihostingConfig *s = opaque;
1426 if (strcmp(name, "arg") == 0) {
1427 s->argc++;
1428 /* one extra element as g_strjoinv() expects NULL-terminated array */
1429 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1430 s->argv[s->argc - 1] = val;
1431 s->argv[s->argc] = NULL;
1433 return 0;
1436 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1437 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1439 char *cmd_token;
1441 /* argv[0] */
1442 add_semihosting_arg(&semihosting, "arg", file, NULL);
1444 /* split -append and initialize argv[1..n] */
1445 cmd_token = strtok(g_strdup(cmd), " ");
1446 while (cmd_token) {
1447 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1448 cmd_token = strtok(NULL, " ");
1452 /* Now we still need this for compatibility with XEN. */
1453 bool has_igd_gfx_passthru;
1454 static void igd_gfx_passthru(void)
1456 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1459 /***********************************************************/
1460 /* USB devices */
1462 static int usb_device_add(const char *devname)
1464 USBDevice *dev = NULL;
1465 #ifndef CONFIG_LINUX
1466 const char *p;
1467 #endif
1469 if (!machine_usb(current_machine)) {
1470 return -1;
1473 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1474 dev = usbdevice_create(devname);
1475 if (dev)
1476 goto done;
1478 /* the other ones */
1479 #ifndef CONFIG_LINUX
1480 /* only the linux version is qdev-ified, usb-bsd still needs this */
1481 if (strstart(devname, "host:", &p)) {
1482 dev = usb_host_device_open(usb_bus_find(-1), p);
1484 #endif
1485 if (!dev)
1486 return -1;
1488 done:
1489 return 0;
1492 static int usb_device_del(const char *devname)
1494 int bus_num, addr;
1495 const char *p;
1497 if (strstart(devname, "host:", &p)) {
1498 return -1;
1501 if (!machine_usb(current_machine)) {
1502 return -1;
1505 p = strchr(devname, '.');
1506 if (!p)
1507 return -1;
1508 bus_num = strtoul(devname, NULL, 0);
1509 addr = strtoul(p + 1, NULL, 0);
1511 return usb_device_delete_addr(bus_num, addr);
1514 static int usb_parse(const char *cmdline)
1516 int r;
1517 r = usb_device_add(cmdline);
1518 if (r < 0) {
1519 error_report("could not add USB device '%s'", cmdline);
1521 return r;
1524 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1526 const char *devname = qdict_get_str(qdict, "devname");
1528 error_report("usb_add is deprecated, please use device_add instead");
1530 if (usb_device_add(devname) < 0) {
1531 error_report("could not add USB device '%s'", devname);
1535 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1537 const char *devname = qdict_get_str(qdict, "devname");
1539 error_report("usb_del is deprecated, please use device_del instead");
1541 if (usb_device_del(devname) < 0) {
1542 error_report("could not delete USB device '%s'", devname);
1546 /***********************************************************/
1547 /* machine registration */
1549 MachineState *current_machine;
1551 static MachineClass *find_machine(const char *name)
1553 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1554 MachineClass *mc = NULL;
1556 for (el = machines; el; el = el->next) {
1557 MachineClass *temp = el->data;
1559 if (!strcmp(temp->name, name)) {
1560 mc = temp;
1561 break;
1563 if (temp->alias &&
1564 !strcmp(temp->alias, name)) {
1565 mc = temp;
1566 break;
1570 g_slist_free(machines);
1571 return mc;
1574 MachineClass *find_default_machine(void)
1576 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1577 MachineClass *mc = NULL;
1579 for (el = machines; el; el = el->next) {
1580 MachineClass *temp = el->data;
1582 if (temp->is_default) {
1583 mc = temp;
1584 break;
1588 g_slist_free(machines);
1589 return mc;
1592 MachineInfoList *qmp_query_machines(Error **errp)
1594 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1595 MachineInfoList *mach_list = NULL;
1597 for (el = machines; el; el = el->next) {
1598 MachineClass *mc = el->data;
1599 MachineInfoList *entry;
1600 MachineInfo *info;
1602 info = g_malloc0(sizeof(*info));
1603 if (mc->is_default) {
1604 info->has_is_default = true;
1605 info->is_default = true;
1608 if (mc->alias) {
1609 info->has_alias = true;
1610 info->alias = g_strdup(mc->alias);
1613 info->name = g_strdup(mc->name);
1614 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1615 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1617 entry = g_malloc0(sizeof(*entry));
1618 entry->value = info;
1619 entry->next = mach_list;
1620 mach_list = entry;
1623 g_slist_free(machines);
1624 return mach_list;
1627 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1629 ObjectProperty *prop;
1630 ObjectPropertyIterator iter;
1632 if (!qemu_opt_has_help_opt(opts)) {
1633 return 0;
1636 object_property_iter_init(&iter, OBJECT(machine));
1637 while ((prop = object_property_iter_next(&iter))) {
1638 if (!prop->set) {
1639 continue;
1642 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1643 prop->name, prop->type);
1644 if (prop->description) {
1645 error_printf(" (%s)\n", prop->description);
1646 } else {
1647 error_printf("\n");
1651 return 1;
1654 /***********************************************************/
1655 /* main execution loop */
1657 struct vm_change_state_entry {
1658 VMChangeStateHandler *cb;
1659 void *opaque;
1660 QLIST_ENTRY (vm_change_state_entry) entries;
1663 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1665 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1666 void *opaque)
1668 VMChangeStateEntry *e;
1670 e = g_malloc0(sizeof (*e));
1672 e->cb = cb;
1673 e->opaque = opaque;
1674 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1675 return e;
1678 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1680 QLIST_REMOVE (e, entries);
1681 g_free (e);
1684 void vm_state_notify(int running, RunState state)
1686 VMChangeStateEntry *e, *next;
1688 trace_vm_state_notify(running, state);
1690 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1691 e->cb(e->opaque, running, state);
1695 static ShutdownCause reset_requested;
1696 static ShutdownCause shutdown_requested;
1697 static int shutdown_signal;
1698 static pid_t shutdown_pid;
1699 static int powerdown_requested;
1700 static int debug_requested;
1701 static int suspend_requested;
1702 static WakeupReason wakeup_reason;
1703 static NotifierList powerdown_notifiers =
1704 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1705 static NotifierList suspend_notifiers =
1706 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1707 static NotifierList wakeup_notifiers =
1708 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1709 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1711 ShutdownCause qemu_shutdown_requested_get(void)
1713 return shutdown_requested;
1716 ShutdownCause qemu_reset_requested_get(void)
1718 return reset_requested;
1721 static int qemu_shutdown_requested(void)
1723 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1726 static void qemu_kill_report(void)
1728 if (!qtest_driver() && shutdown_signal) {
1729 if (shutdown_pid == 0) {
1730 /* This happens for eg ^C at the terminal, so it's worth
1731 * avoiding printing an odd message in that case.
1733 error_report("terminating on signal %d", shutdown_signal);
1734 } else {
1735 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1737 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1738 shutdown_signal, shutdown_pid,
1739 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1740 g_free(shutdown_cmd);
1742 shutdown_signal = 0;
1746 static ShutdownCause qemu_reset_requested(void)
1748 ShutdownCause r = reset_requested;
1750 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1751 reset_requested = SHUTDOWN_CAUSE_NONE;
1752 return r;
1754 return SHUTDOWN_CAUSE_NONE;
1757 static int qemu_suspend_requested(void)
1759 int r = suspend_requested;
1760 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1761 suspend_requested = 0;
1762 return r;
1764 return false;
1767 static WakeupReason qemu_wakeup_requested(void)
1769 return wakeup_reason;
1772 static int qemu_powerdown_requested(void)
1774 int r = powerdown_requested;
1775 powerdown_requested = 0;
1776 return r;
1779 static int qemu_debug_requested(void)
1781 int r = debug_requested;
1782 debug_requested = 0;
1783 return r;
1787 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1789 void qemu_system_reset(ShutdownCause reason)
1791 MachineClass *mc;
1793 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1795 cpu_synchronize_all_states();
1797 if (mc && mc->reset) {
1798 mc->reset();
1799 } else {
1800 qemu_devices_reset();
1802 if (reason) {
1803 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1804 &error_abort);
1806 cpu_synchronize_all_post_reset();
1809 void qemu_system_guest_panicked(GuestPanicInformation *info)
1811 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1813 if (current_cpu) {
1814 current_cpu->crash_occurred = true;
1816 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1817 !!info, info, &error_abort);
1818 vm_stop(RUN_STATE_GUEST_PANICKED);
1819 if (!no_shutdown) {
1820 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1821 !!info, info, &error_abort);
1822 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1825 if (info) {
1826 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1827 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1828 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1829 info->u.hyper_v.arg1,
1830 info->u.hyper_v.arg2,
1831 info->u.hyper_v.arg3,
1832 info->u.hyper_v.arg4,
1833 info->u.hyper_v.arg5);
1835 qapi_free_GuestPanicInformation(info);
1839 void qemu_system_reset_request(ShutdownCause reason)
1841 if (no_reboot) {
1842 shutdown_requested = reason;
1843 } else {
1844 reset_requested = reason;
1846 cpu_stop_current();
1847 qemu_notify_event();
1850 static void qemu_system_suspend(void)
1852 pause_all_vcpus();
1853 notifier_list_notify(&suspend_notifiers, NULL);
1854 runstate_set(RUN_STATE_SUSPENDED);
1855 qapi_event_send_suspend(&error_abort);
1858 void qemu_system_suspend_request(void)
1860 if (runstate_check(RUN_STATE_SUSPENDED)) {
1861 return;
1863 suspend_requested = 1;
1864 cpu_stop_current();
1865 qemu_notify_event();
1868 void qemu_register_suspend_notifier(Notifier *notifier)
1870 notifier_list_add(&suspend_notifiers, notifier);
1873 void qemu_system_wakeup_request(WakeupReason reason)
1875 trace_system_wakeup_request(reason);
1877 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1878 return;
1880 if (!(wakeup_reason_mask & (1 << reason))) {
1881 return;
1883 runstate_set(RUN_STATE_RUNNING);
1884 wakeup_reason = reason;
1885 qemu_notify_event();
1888 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1890 if (enabled) {
1891 wakeup_reason_mask |= (1 << reason);
1892 } else {
1893 wakeup_reason_mask &= ~(1 << reason);
1897 void qemu_register_wakeup_notifier(Notifier *notifier)
1899 notifier_list_add(&wakeup_notifiers, notifier);
1902 void qemu_system_killed(int signal, pid_t pid)
1904 shutdown_signal = signal;
1905 shutdown_pid = pid;
1906 no_shutdown = 0;
1908 /* Cannot call qemu_system_shutdown_request directly because
1909 * we are in a signal handler.
1911 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1912 qemu_notify_event();
1915 void qemu_system_shutdown_request(ShutdownCause reason)
1917 trace_qemu_system_shutdown_request(reason);
1918 replay_shutdown_request(reason);
1919 shutdown_requested = reason;
1920 qemu_notify_event();
1923 static void qemu_system_powerdown(void)
1925 qapi_event_send_powerdown(&error_abort);
1926 notifier_list_notify(&powerdown_notifiers, NULL);
1929 void qemu_system_powerdown_request(void)
1931 trace_qemu_system_powerdown_request();
1932 powerdown_requested = 1;
1933 qemu_notify_event();
1936 void qemu_register_powerdown_notifier(Notifier *notifier)
1938 notifier_list_add(&powerdown_notifiers, notifier);
1941 void qemu_system_debug_request(void)
1943 debug_requested = 1;
1944 qemu_notify_event();
1947 static bool main_loop_should_exit(void)
1949 RunState r;
1950 ShutdownCause request;
1952 if (qemu_debug_requested()) {
1953 vm_stop(RUN_STATE_DEBUG);
1955 if (qemu_suspend_requested()) {
1956 qemu_system_suspend();
1958 request = qemu_shutdown_requested();
1959 if (request) {
1960 qemu_kill_report();
1961 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1962 &error_abort);
1963 if (no_shutdown) {
1964 vm_stop(RUN_STATE_SHUTDOWN);
1965 } else {
1966 return true;
1969 request = qemu_reset_requested();
1970 if (request) {
1971 pause_all_vcpus();
1972 qemu_system_reset(request);
1973 resume_all_vcpus();
1974 if (!runstate_check(RUN_STATE_RUNNING) &&
1975 !runstate_check(RUN_STATE_INMIGRATE)) {
1976 runstate_set(RUN_STATE_PRELAUNCH);
1979 if (qemu_wakeup_requested()) {
1980 pause_all_vcpus();
1981 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1982 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1983 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1984 resume_all_vcpus();
1985 qapi_event_send_wakeup(&error_abort);
1987 if (qemu_powerdown_requested()) {
1988 qemu_system_powerdown();
1990 if (qemu_vmstop_requested(&r)) {
1991 vm_stop(r);
1993 return false;
1996 static void main_loop(void)
1998 #ifdef CONFIG_PROFILER
1999 int64_t ti;
2000 #endif
2001 do {
2002 #ifdef CONFIG_PROFILER
2003 ti = profile_getclock();
2004 #endif
2005 main_loop_wait(false);
2006 #ifdef CONFIG_PROFILER
2007 dev_time += profile_getclock() - ti;
2008 #endif
2009 } while (!main_loop_should_exit());
2012 static void version(void)
2014 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
2015 QEMU_COPYRIGHT "\n");
2018 static void QEMU_NORETURN help(int exitcode)
2020 version();
2021 printf("usage: %s [options] [disk_image]\n\n"
2022 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2023 error_get_progname());
2025 #define QEMU_OPTIONS_GENERATE_HELP
2026 #include "qemu-options-wrapper.h"
2028 printf("\nDuring emulation, the following keys are useful:\n"
2029 "ctrl-alt-f toggle full screen\n"
2030 "ctrl-alt-n switch to virtual console 'n'\n"
2031 "ctrl-alt toggle mouse and keyboard grab\n"
2032 "\n"
2033 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2034 "\n"
2035 QEMU_HELP_BOTTOM "\n");
2037 exit(exitcode);
2040 #define HAS_ARG 0x0001
2042 typedef struct QEMUOption {
2043 const char *name;
2044 int flags;
2045 int index;
2046 uint32_t arch_mask;
2047 } QEMUOption;
2049 static const QEMUOption qemu_options[] = {
2050 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2051 #define QEMU_OPTIONS_GENERATE_OPTIONS
2052 #include "qemu-options-wrapper.h"
2053 { NULL },
2056 typedef struct VGAInterfaceInfo {
2057 const char *opt_name; /* option name */
2058 const char *name; /* human-readable name */
2059 /* Class names indicating that support is available.
2060 * If no class is specified, the interface is always available */
2061 const char *class_names[2];
2062 } VGAInterfaceInfo;
2064 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2065 [VGA_NONE] = {
2066 .opt_name = "none",
2068 [VGA_STD] = {
2069 .opt_name = "std",
2070 .name = "standard VGA",
2071 .class_names = { "VGA", "isa-vga" },
2073 [VGA_CIRRUS] = {
2074 .opt_name = "cirrus",
2075 .name = "Cirrus VGA",
2076 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2078 [VGA_VMWARE] = {
2079 .opt_name = "vmware",
2080 .name = "VMWare SVGA",
2081 .class_names = { "vmware-svga" },
2083 [VGA_VIRTIO] = {
2084 .opt_name = "virtio",
2085 .name = "Virtio VGA",
2086 .class_names = { "virtio-vga" },
2088 [VGA_QXL] = {
2089 .opt_name = "qxl",
2090 .name = "QXL VGA",
2091 .class_names = { "qxl-vga" },
2093 [VGA_TCX] = {
2094 .opt_name = "tcx",
2095 .name = "TCX framebuffer",
2096 .class_names = { "SUNW,tcx" },
2098 [VGA_CG3] = {
2099 .opt_name = "cg3",
2100 .name = "CG3 framebuffer",
2101 .class_names = { "cgthree" },
2103 [VGA_XENFB] = {
2104 .opt_name = "xenfb",
2108 static bool vga_interface_available(VGAInterfaceType t)
2110 VGAInterfaceInfo *ti = &vga_interfaces[t];
2112 assert(t < VGA_TYPE_MAX);
2113 return !ti->class_names[0] ||
2114 object_class_by_name(ti->class_names[0]) ||
2115 object_class_by_name(ti->class_names[1]);
2118 static void select_vgahw(const char *p)
2120 const char *opts;
2121 int t;
2123 assert(vga_interface_type == VGA_NONE);
2124 for (t = 0; t < VGA_TYPE_MAX; t++) {
2125 VGAInterfaceInfo *ti = &vga_interfaces[t];
2126 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2127 if (!vga_interface_available(t)) {
2128 error_report("%s not available", ti->name);
2129 exit(1);
2131 vga_interface_type = t;
2132 break;
2135 if (t == VGA_TYPE_MAX) {
2136 invalid_vga:
2137 error_report("unknown vga type: %s", p);
2138 exit(1);
2140 while (*opts) {
2141 const char *nextopt;
2143 if (strstart(opts, ",retrace=", &nextopt)) {
2144 opts = nextopt;
2145 if (strstart(opts, "dumb", &nextopt))
2146 vga_retrace_method = VGA_RETRACE_DUMB;
2147 else if (strstart(opts, "precise", &nextopt))
2148 vga_retrace_method = VGA_RETRACE_PRECISE;
2149 else goto invalid_vga;
2150 } else goto invalid_vga;
2151 opts = nextopt;
2155 typedef enum DisplayType {
2156 DT_DEFAULT,
2157 DT_CURSES,
2158 DT_SDL,
2159 DT_COCOA,
2160 DT_GTK,
2161 DT_EGL,
2162 DT_NONE,
2163 } DisplayType;
2165 static DisplayType select_display(const char *p)
2167 const char *opts;
2168 DisplayType display = DT_DEFAULT;
2170 if (strstart(p, "sdl", &opts)) {
2171 #ifdef CONFIG_SDL
2172 display = DT_SDL;
2173 while (*opts) {
2174 const char *nextopt;
2176 if (strstart(opts, ",frame=", &nextopt)) {
2177 opts = nextopt;
2178 if (strstart(opts, "on", &nextopt)) {
2179 no_frame = 0;
2180 } else if (strstart(opts, "off", &nextopt)) {
2181 no_frame = 1;
2182 } else {
2183 goto invalid_sdl_args;
2185 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2186 opts = nextopt;
2187 if (strstart(opts, "on", &nextopt)) {
2188 alt_grab = 1;
2189 } else if (strstart(opts, "off", &nextopt)) {
2190 alt_grab = 0;
2191 } else {
2192 goto invalid_sdl_args;
2194 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2195 opts = nextopt;
2196 if (strstart(opts, "on", &nextopt)) {
2197 ctrl_grab = 1;
2198 } else if (strstart(opts, "off", &nextopt)) {
2199 ctrl_grab = 0;
2200 } else {
2201 goto invalid_sdl_args;
2203 } else if (strstart(opts, ",window_close=", &nextopt)) {
2204 opts = nextopt;
2205 if (strstart(opts, "on", &nextopt)) {
2206 no_quit = 0;
2207 } else if (strstart(opts, "off", &nextopt)) {
2208 no_quit = 1;
2209 } else {
2210 goto invalid_sdl_args;
2212 } else if (strstart(opts, ",gl=", &nextopt)) {
2213 opts = nextopt;
2214 if (strstart(opts, "on", &nextopt)) {
2215 request_opengl = 1;
2216 } else if (strstart(opts, "off", &nextopt)) {
2217 request_opengl = 0;
2218 } else {
2219 goto invalid_sdl_args;
2221 } else {
2222 invalid_sdl_args:
2223 error_report("invalid SDL option string");
2224 exit(1);
2226 opts = nextopt;
2228 #else
2229 error_report("SDL support is disabled");
2230 exit(1);
2231 #endif
2232 } else if (strstart(p, "vnc", &opts)) {
2233 if (*opts == '=') {
2234 vnc_parse(opts + 1, &error_fatal);
2235 } else {
2236 error_report("VNC requires a display argument vnc=<display>");
2237 exit(1);
2239 } else if (strstart(p, "egl-headless", &opts)) {
2240 #ifdef CONFIG_OPENGL_DMABUF
2241 request_opengl = 1;
2242 display_opengl = 1;
2243 display = DT_EGL;
2244 #else
2245 fprintf(stderr, "egl support is disabled\n");
2246 exit(1);
2247 #endif
2248 } else if (strstart(p, "curses", &opts)) {
2249 #ifdef CONFIG_CURSES
2250 display = DT_CURSES;
2251 #else
2252 error_report("curses support is disabled");
2253 exit(1);
2254 #endif
2255 } else if (strstart(p, "gtk", &opts)) {
2256 #ifdef CONFIG_GTK
2257 display = DT_GTK;
2258 while (*opts) {
2259 const char *nextopt;
2261 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2262 opts = nextopt;
2263 if (strstart(opts, "on", &nextopt)) {
2264 grab_on_hover = true;
2265 } else if (strstart(opts, "off", &nextopt)) {
2266 grab_on_hover = false;
2267 } else {
2268 goto invalid_gtk_args;
2270 } else if (strstart(opts, ",gl=", &nextopt)) {
2271 opts = nextopt;
2272 if (strstart(opts, "on", &nextopt)) {
2273 request_opengl = 1;
2274 } else if (strstart(opts, "off", &nextopt)) {
2275 request_opengl = 0;
2276 } else {
2277 goto invalid_gtk_args;
2279 } else {
2280 invalid_gtk_args:
2281 error_report("invalid GTK option string");
2282 exit(1);
2284 opts = nextopt;
2286 #else
2287 error_report("GTK support is disabled");
2288 exit(1);
2289 #endif
2290 } else if (strstart(p, "none", &opts)) {
2291 display = DT_NONE;
2292 } else {
2293 error_report("unknown display type");
2294 exit(1);
2297 return display;
2300 static int balloon_parse(const char *arg)
2302 QemuOpts *opts;
2304 if (strcmp(arg, "none") == 0) {
2305 return 0;
2308 if (!strncmp(arg, "virtio", 6)) {
2309 if (arg[6] == ',') {
2310 /* have params -> parse them */
2311 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2312 false);
2313 if (!opts)
2314 return -1;
2315 } else {
2316 /* create empty opts */
2317 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2318 &error_abort);
2320 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2321 return 0;
2324 return -1;
2327 #if defined(CONFIG_RUBY)
2328 /* Ruby interface for QEMU. See README.EXT for programming example. */
2329 #warning mit ruby
2330 #include <ruby.h>
2332 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2334 int i;
2335 for (i = 0; i < argc; i++) {
2336 argv[i] = StringValue(argv[i]);
2337 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2339 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2340 //~ mode = 0666; /* default value */
2341 //~ }
2342 return INT2FIX(argc);
2345 void Init_qemu(void)
2347 printf("%s\n", __func__);
2348 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2349 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2350 #if 0
2351 rb_define_method();
2352 #endif
2354 #endif /* CONFIG_RUBY */
2356 #if defined(CONFIG_DLL)
2357 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2359 return FALSE;
2361 #endif /* CONFIG_DLL */
2363 char *qemu_find_file(int type, const char *name)
2365 int i;
2366 const char *subdir;
2367 char *buf;
2369 /* Try the name as a straight path first */
2370 if (access(name, R_OK) == 0) {
2371 trace_load_file(name, name);
2372 return g_strdup(name);
2375 switch (type) {
2376 case QEMU_FILE_TYPE_BIOS:
2377 subdir = "";
2378 break;
2379 case QEMU_FILE_TYPE_KEYMAP:
2380 subdir = "keymaps/";
2381 break;
2382 default:
2383 abort();
2386 for (i = 0; i < data_dir_idx; i++) {
2387 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2388 if (access(buf, R_OK) == 0) {
2389 trace_load_file(name, buf);
2390 return buf;
2392 g_free(buf);
2394 return NULL;
2397 static void qemu_add_data_dir(const char *path)
2399 int i;
2401 if (path == NULL) {
2402 return;
2404 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2405 return;
2407 for (i = 0; i < data_dir_idx; i++) {
2408 if (strcmp(data_dir[i], path) == 0) {
2409 return; /* duplicate */
2412 data_dir[data_dir_idx++] = path;
2415 static inline bool nonempty_str(const char *str)
2417 return str && *str;
2420 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2422 gchar *buf;
2423 size_t size;
2424 const char *name, *file, *str;
2425 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2427 if (fw_cfg == NULL) {
2428 error_report("fw_cfg device not available");
2429 return -1;
2431 name = qemu_opt_get(opts, "name");
2432 file = qemu_opt_get(opts, "file");
2433 str = qemu_opt_get(opts, "string");
2435 /* we need name and either a file or the content string */
2436 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2437 error_report("invalid argument(s)");
2438 return -1;
2440 if (nonempty_str(file) && nonempty_str(str)) {
2441 error_report("file and string are mutually exclusive");
2442 return -1;
2444 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2445 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2446 return -1;
2448 if (strncmp(name, "opt/", 4) != 0) {
2449 warn_report("externally provided fw_cfg item names "
2450 "should be prefixed with \"opt/\"");
2452 if (nonempty_str(str)) {
2453 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2454 buf = g_memdup(str, size);
2455 } else {
2456 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2457 error_report("can't load %s", file);
2458 return -1;
2461 /* For legacy, keep user files in a specific global order. */
2462 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2463 fw_cfg_add_file(fw_cfg, name, buf, size);
2464 fw_cfg_reset_order_override(fw_cfg);
2465 return 0;
2468 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2470 return qdev_device_help(opts);
2473 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2475 Error *err = NULL;
2476 DeviceState *dev;
2478 dev = qdev_device_add(opts, &err);
2479 if (!dev) {
2480 error_report_err(err);
2481 return -1;
2483 object_unref(OBJECT(dev));
2484 return 0;
2487 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2489 Error *local_err = NULL;
2491 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2492 if (local_err) {
2493 error_report_err(local_err);
2494 return -1;
2496 exit(0);
2498 return 0;
2501 #ifdef CONFIG_VIRTFS
2502 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2504 return qemu_fsdev_add(opts);
2506 #endif
2508 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2510 Chardev *chr;
2511 const char *chardev;
2512 const char *mode;
2513 int flags;
2515 mode = qemu_opt_get(opts, "mode");
2516 if (mode == NULL) {
2517 mode = "readline";
2519 if (strcmp(mode, "readline") == 0) {
2520 flags = MONITOR_USE_READLINE;
2521 } else if (strcmp(mode, "control") == 0) {
2522 flags = MONITOR_USE_CONTROL;
2523 } else {
2524 error_report("unknown monitor mode \"%s\"", mode);
2525 exit(1);
2528 if (qemu_opt_get_bool(opts, "pretty", 0))
2529 flags |= MONITOR_USE_PRETTY;
2531 if (qemu_opt_get_bool(opts, "default", 0)) {
2532 error_report("option 'default' does nothing and is deprecated");
2535 chardev = qemu_opt_get(opts, "chardev");
2536 chr = qemu_chr_find(chardev);
2537 if (chr == NULL) {
2538 error_report("chardev \"%s\" not found", chardev);
2539 exit(1);
2542 monitor_init(chr, flags);
2543 return 0;
2546 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2548 static int monitor_device_index = 0;
2549 QemuOpts *opts;
2550 const char *p;
2551 char label[32];
2553 if (strstart(optarg, "chardev:", &p)) {
2554 snprintf(label, sizeof(label), "%s", p);
2555 } else {
2556 snprintf(label, sizeof(label), "compat_monitor%d",
2557 monitor_device_index);
2558 opts = qemu_chr_parse_compat(label, optarg);
2559 if (!opts) {
2560 error_report("parse error: %s", optarg);
2561 exit(1);
2565 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2566 qemu_opt_set(opts, "mode", mode, &error_abort);
2567 qemu_opt_set(opts, "chardev", label, &error_abort);
2568 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2569 monitor_device_index++;
2572 struct device_config {
2573 enum {
2574 DEV_USB, /* -usbdevice */
2575 DEV_BT, /* -bt */
2576 DEV_SERIAL, /* -serial */
2577 DEV_PARALLEL, /* -parallel */
2578 DEV_VIRTCON, /* -virtioconsole */
2579 DEV_DEBUGCON, /* -debugcon */
2580 DEV_GDB, /* -gdb, -s */
2581 DEV_SCLP, /* s390 sclp */
2582 } type;
2583 const char *cmdline;
2584 Location loc;
2585 QTAILQ_ENTRY(device_config) next;
2588 static QTAILQ_HEAD(, device_config) device_configs =
2589 QTAILQ_HEAD_INITIALIZER(device_configs);
2591 static void add_device_config(int type, const char *cmdline)
2593 struct device_config *conf;
2595 conf = g_malloc0(sizeof(*conf));
2596 conf->type = type;
2597 conf->cmdline = cmdline;
2598 loc_save(&conf->loc);
2599 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2602 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2604 struct device_config *conf;
2605 int rc;
2607 QTAILQ_FOREACH(conf, &device_configs, next) {
2608 if (conf->type != type)
2609 continue;
2610 loc_push_restore(&conf->loc);
2611 rc = func(conf->cmdline);
2612 loc_pop(&conf->loc);
2613 if (rc) {
2614 return rc;
2617 return 0;
2620 static int serial_parse(const char *devname)
2622 static int index = 0;
2623 char label[32];
2625 if (strcmp(devname, "none") == 0)
2626 return 0;
2627 if (index == MAX_SERIAL_PORTS) {
2628 error_report("too many serial ports");
2629 exit(1);
2631 snprintf(label, sizeof(label), "serial%d", index);
2632 serial_hds[index] = qemu_chr_new(label, devname);
2633 if (!serial_hds[index]) {
2634 error_report("could not connect serial device"
2635 " to character backend '%s'", devname);
2636 return -1;
2638 index++;
2639 return 0;
2642 static int parallel_parse(const char *devname)
2644 static int index = 0;
2645 char label[32];
2647 if (strcmp(devname, "none") == 0)
2648 return 0;
2649 if (index == MAX_PARALLEL_PORTS) {
2650 error_report("too many parallel ports");
2651 exit(1);
2653 snprintf(label, sizeof(label), "parallel%d", index);
2654 parallel_hds[index] = qemu_chr_new(label, devname);
2655 if (!parallel_hds[index]) {
2656 error_report("could not connect parallel device"
2657 " to character backend '%s'", devname);
2658 return -1;
2660 index++;
2661 return 0;
2664 static int virtcon_parse(const char *devname)
2666 QemuOptsList *device = qemu_find_opts("device");
2667 static int index = 0;
2668 char label[32];
2669 QemuOpts *bus_opts, *dev_opts;
2671 if (strcmp(devname, "none") == 0)
2672 return 0;
2673 if (index == MAX_VIRTIO_CONSOLES) {
2674 error_report("too many virtio consoles");
2675 exit(1);
2678 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2679 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2681 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2682 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2684 snprintf(label, sizeof(label), "virtcon%d", index);
2685 virtcon_hds[index] = qemu_chr_new(label, devname);
2686 if (!virtcon_hds[index]) {
2687 error_report("could not connect virtio console"
2688 " to character backend '%s'", devname);
2689 return -1;
2691 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2693 index++;
2694 return 0;
2697 static int sclp_parse(const char *devname)
2699 QemuOptsList *device = qemu_find_opts("device");
2700 static int index = 0;
2701 char label[32];
2702 QemuOpts *dev_opts;
2704 if (strcmp(devname, "none") == 0) {
2705 return 0;
2707 if (index == MAX_SCLP_CONSOLES) {
2708 error_report("too many sclp consoles");
2709 exit(1);
2712 assert(arch_type == QEMU_ARCH_S390X);
2714 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2715 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2717 snprintf(label, sizeof(label), "sclpcon%d", index);
2718 sclp_hds[index] = qemu_chr_new(label, devname);
2719 if (!sclp_hds[index]) {
2720 error_report("could not connect sclp console"
2721 " to character backend '%s'", devname);
2722 return -1;
2724 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2726 index++;
2727 return 0;
2730 static int debugcon_parse(const char *devname)
2732 QemuOpts *opts;
2734 if (!qemu_chr_new("debugcon", devname)) {
2735 exit(1);
2737 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2738 if (!opts) {
2739 error_report("already have a debugcon device");
2740 exit(1);
2742 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2743 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2744 return 0;
2747 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2749 const MachineClass *mc1 = a, *mc2 = b;
2750 int res;
2752 if (mc1->family == NULL) {
2753 if (mc2->family == NULL) {
2754 /* Compare standalone machine types against each other; they sort
2755 * in increasing order.
2757 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2758 object_class_get_name(OBJECT_CLASS(mc2)));
2761 /* Standalone machine types sort after families. */
2762 return 1;
2765 if (mc2->family == NULL) {
2766 /* Families sort before standalone machine types. */
2767 return -1;
2770 /* Families sort between each other alphabetically increasingly. */
2771 res = strcmp(mc1->family, mc2->family);
2772 if (res != 0) {
2773 return res;
2776 /* Within the same family, machine types sort in decreasing order. */
2777 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2778 object_class_get_name(OBJECT_CLASS(mc1)));
2781 static MachineClass *machine_parse(const char *name)
2783 MachineClass *mc = NULL;
2784 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2786 if (name) {
2787 mc = find_machine(name);
2789 if (mc) {
2790 g_slist_free(machines);
2791 return mc;
2793 if (name && !is_help_option(name)) {
2794 error_report("unsupported machine type");
2795 error_printf("Use -machine help to list supported machines\n");
2796 } else {
2797 printf("Supported machines are:\n");
2798 machines = g_slist_sort(machines, machine_class_cmp);
2799 for (el = machines; el; el = el->next) {
2800 MachineClass *mc = el->data;
2801 if (mc->alias) {
2802 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2804 printf("%-20s %s%s\n", mc->name, mc->desc,
2805 mc->is_default ? " (default)" : "");
2809 g_slist_free(machines);
2810 exit(!name || !is_help_option(name));
2813 void qemu_add_exit_notifier(Notifier *notify)
2815 notifier_list_add(&exit_notifiers, notify);
2818 void qemu_remove_exit_notifier(Notifier *notify)
2820 notifier_remove(notify);
2823 static void qemu_run_exit_notifiers(void)
2825 notifier_list_notify(&exit_notifiers, NULL);
2828 static bool machine_init_done;
2830 void qemu_add_machine_init_done_notifier(Notifier *notify)
2832 notifier_list_add(&machine_init_done_notifiers, notify);
2833 if (machine_init_done) {
2834 notify->notify(notify, NULL);
2838 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2840 notifier_remove(notify);
2843 static void qemu_run_machine_init_done_notifiers(void)
2845 notifier_list_notify(&machine_init_done_notifiers, NULL);
2846 machine_init_done = true;
2849 static const QEMUOption *lookup_opt(int argc, char **argv,
2850 const char **poptarg, int *poptind)
2852 const QEMUOption *popt;
2853 int optind = *poptind;
2854 char *r = argv[optind];
2855 const char *optarg;
2857 loc_set_cmdline(argv, optind, 1);
2858 optind++;
2859 /* Treat --foo the same as -foo. */
2860 if (r[1] == '-')
2861 r++;
2862 popt = qemu_options;
2863 for(;;) {
2864 if (!popt->name) {
2865 error_report("invalid option");
2866 exit(1);
2868 if (!strcmp(popt->name, r + 1))
2869 break;
2870 popt++;
2872 if (popt->flags & HAS_ARG) {
2873 if (optind >= argc) {
2874 error_report("requires an argument");
2875 exit(1);
2877 optarg = argv[optind++];
2878 loc_set_cmdline(argv, optind - 2, 2);
2879 } else {
2880 optarg = NULL;
2883 *poptarg = optarg;
2884 *poptind = optind;
2886 return popt;
2889 static MachineClass *select_machine(void)
2891 MachineClass *machine_class = find_default_machine();
2892 const char *optarg;
2893 QemuOpts *opts;
2894 Location loc;
2896 loc_push_none(&loc);
2898 opts = qemu_get_machine_opts();
2899 qemu_opts_loc_restore(opts);
2901 optarg = qemu_opt_get(opts, "type");
2902 if (optarg) {
2903 machine_class = machine_parse(optarg);
2906 if (!machine_class) {
2907 error_report("No machine specified, and there is no default");
2908 error_printf("Use -machine help to list supported machines\n");
2909 exit(1);
2912 loc_pop(&loc);
2913 return machine_class;
2916 static int machine_set_property(void *opaque,
2917 const char *name, const char *value,
2918 Error **errp)
2920 Object *obj = OBJECT(opaque);
2921 Error *local_err = NULL;
2922 char *p, *qom_name;
2924 if (strcmp(name, "type") == 0) {
2925 return 0;
2928 qom_name = g_strdup(name);
2929 for (p = qom_name; *p; p++) {
2930 if (*p == '_') {
2931 *p = '-';
2935 object_property_parse(obj, value, qom_name, &local_err);
2936 g_free(qom_name);
2938 if (local_err) {
2939 error_report_err(local_err);
2940 return -1;
2943 return 0;
2948 * Initial object creation happens before all other
2949 * QEMU data types are created. The majority of objects
2950 * can be created at this point. The rng-egd object
2951 * cannot be created here, as it depends on the chardev
2952 * already existing.
2954 static bool object_create_initial(const char *type)
2956 if (g_str_equal(type, "rng-egd") ||
2957 g_str_has_prefix(type, "pr-manager-")) {
2958 return false;
2962 * return false for concrete netfilters since
2963 * they depend on netdevs already existing
2965 if (g_str_equal(type, "filter-buffer") ||
2966 g_str_equal(type, "filter-dump") ||
2967 g_str_equal(type, "filter-mirror") ||
2968 g_str_equal(type, "filter-redirector") ||
2969 g_str_equal(type, "colo-compare") ||
2970 g_str_equal(type, "filter-rewriter") ||
2971 g_str_equal(type, "filter-replay")) {
2972 return false;
2975 /* Memory allocation by backends needs to be done
2976 * after configure_accelerator() (due to the tcg_enabled()
2977 * checks at memory_region_init_*()).
2979 * Also, allocation of large amounts of memory may delay
2980 * chardev initialization for too long, and trigger timeouts
2981 * on software that waits for a monitor socket to be created
2982 * (e.g. libvirt).
2984 if (g_str_has_prefix(type, "memory-backend-")) {
2985 return false;
2988 return true;
2993 * The remainder of object creation happens after the
2994 * creation of chardev, fsdev, net clients and device data types.
2996 static bool object_create_delayed(const char *type)
2998 return !object_create_initial(type);
3002 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
3003 MachineClass *mc)
3005 uint64_t sz;
3006 const char *mem_str;
3007 const char *maxmem_str, *slots_str;
3008 const ram_addr_t default_ram_size = mc->default_ram_size;
3009 QemuOpts *opts = qemu_find_opts_singleton("memory");
3010 Location loc;
3012 loc_push_none(&loc);
3013 qemu_opts_loc_restore(opts);
3015 sz = 0;
3016 mem_str = qemu_opt_get(opts, "size");
3017 if (mem_str) {
3018 if (!*mem_str) {
3019 error_report("missing 'size' option value");
3020 exit(EXIT_FAILURE);
3023 sz = qemu_opt_get_size(opts, "size", ram_size);
3025 /* Fix up legacy suffix-less format */
3026 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3027 uint64_t overflow_check = sz;
3029 sz <<= 20;
3030 if ((sz >> 20) != overflow_check) {
3031 error_report("too large 'size' option value");
3032 exit(EXIT_FAILURE);
3037 /* backward compatibility behaviour for case "-m 0" */
3038 if (sz == 0) {
3039 sz = default_ram_size;
3042 sz = QEMU_ALIGN_UP(sz, 8192);
3043 ram_size = sz;
3044 if (ram_size != sz) {
3045 error_report("ram size too large");
3046 exit(EXIT_FAILURE);
3049 /* store value for the future use */
3050 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
3051 *maxram_size = ram_size;
3053 maxmem_str = qemu_opt_get(opts, "maxmem");
3054 slots_str = qemu_opt_get(opts, "slots");
3055 if (maxmem_str && slots_str) {
3056 uint64_t slots;
3058 sz = qemu_opt_get_size(opts, "maxmem", 0);
3059 slots = qemu_opt_get_number(opts, "slots", 0);
3060 if (sz < ram_size) {
3061 error_report("invalid value of -m option maxmem: "
3062 "maximum memory size (0x%" PRIx64 ") must be at least "
3063 "the initial memory size (0x" RAM_ADDR_FMT ")",
3064 sz, ram_size);
3065 exit(EXIT_FAILURE);
3066 } else if (sz > ram_size) {
3067 if (!slots) {
3068 error_report("invalid value of -m option: maxmem was "
3069 "specified, but no hotplug slots were specified");
3070 exit(EXIT_FAILURE);
3072 } else if (slots) {
3073 error_report("invalid value of -m option maxmem: "
3074 "memory slots were specified but maximum memory size "
3075 "(0x%" PRIx64 ") is equal to the initial memory size "
3076 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3077 exit(EXIT_FAILURE);
3080 *maxram_size = sz;
3081 *ram_slots = slots;
3082 } else if ((!maxmem_str && slots_str) ||
3083 (maxmem_str && !slots_str)) {
3084 error_report("invalid -m option value: missing "
3085 "'%s' option", slots_str ? "maxmem" : "slots");
3086 exit(EXIT_FAILURE);
3089 loc_pop(&loc);
3092 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3094 GlobalProperty *g;
3096 g = g_malloc0(sizeof(*g));
3097 g->driver = qemu_opt_get(opts, "driver");
3098 g->property = qemu_opt_get(opts, "property");
3099 g->value = qemu_opt_get(opts, "value");
3100 g->user_provided = true;
3101 g->errp = &error_fatal;
3102 qdev_prop_register_global(g);
3103 return 0;
3106 static int qemu_read_default_config_file(void)
3108 int ret;
3110 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3111 if (ret < 0 && ret != -ENOENT) {
3112 return ret;
3115 return 0;
3118 static void user_register_global_props(void)
3120 qemu_opts_foreach(qemu_find_opts("global"),
3121 global_init_func, NULL, NULL);
3125 * Note: we should see that these properties are actually having a
3126 * priority: accel < machine < user. This means e.g. when user
3127 * specifies something in "-global", it'll always be used with highest
3128 * priority than either machine/accelerator compat properties.
3130 static void register_global_properties(MachineState *ms)
3132 accel_register_compat_props(ms->accelerator);
3133 machine_register_compat_props(ms);
3134 user_register_global_props();
3137 int main(int argc, char **argv)
3139 int i;
3140 int snapshot, linux_boot;
3141 const char *initrd_filename;
3142 const char *kernel_filename, *kernel_cmdline;
3143 const char *boot_order = NULL;
3144 const char *boot_once = NULL;
3145 DisplayState *ds;
3146 int cyls, heads, secs, translation;
3147 QemuOpts *opts, *machine_opts;
3148 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3149 QemuOptsList *olist;
3150 int optind;
3151 const char *optarg;
3152 const char *loadvm = NULL;
3153 MachineClass *machine_class;
3154 const char *cpu_model;
3155 const char *vga_model = NULL;
3156 const char *qtest_chrdev = NULL;
3157 const char *qtest_log = NULL;
3158 const char *pid_file = NULL;
3159 const char *incoming = NULL;
3160 bool defconfig = true;
3161 bool userconfig = true;
3162 bool nographic = false;
3163 DisplayType display_type = DT_DEFAULT;
3164 int display_remote = 0;
3165 const char *log_mask = NULL;
3166 const char *log_file = NULL;
3167 char *trace_file = NULL;
3168 ram_addr_t maxram_size;
3169 uint64_t ram_slots = 0;
3170 FILE *vmstate_dump_file = NULL;
3171 Error *main_loop_err = NULL;
3172 Error *err = NULL;
3173 bool list_data_dirs = false;
3174 char **dirs;
3175 typedef struct BlockdevOptions_queue {
3176 BlockdevOptions *bdo;
3177 Location loc;
3178 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3179 } BlockdevOptions_queue;
3180 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3181 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3183 module_call_init(MODULE_INIT_TRACE);
3185 qemu_init_cpu_list();
3186 qemu_init_cpu_loop();
3187 qemu_mutex_lock_iothread();
3189 atexit(qemu_run_exit_notifiers);
3190 error_set_progname(argv[0]);
3191 qemu_init_exec_dir(argv[0]);
3193 module_call_init(MODULE_INIT_QOM);
3194 monitor_init_qmp_commands();
3196 qemu_add_opts(&qemu_drive_opts);
3197 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3198 qemu_add_drive_opts(&qemu_common_drive_opts);
3199 qemu_add_drive_opts(&qemu_drive_opts);
3200 qemu_add_drive_opts(&bdrv_runtime_opts);
3201 qemu_add_opts(&qemu_chardev_opts);
3202 qemu_add_opts(&qemu_device_opts);
3203 qemu_add_opts(&qemu_netdev_opts);
3204 qemu_add_opts(&qemu_net_opts);
3205 qemu_add_opts(&qemu_rtc_opts);
3206 qemu_add_opts(&qemu_global_opts);
3207 qemu_add_opts(&qemu_mon_opts);
3208 qemu_add_opts(&qemu_trace_opts);
3209 qemu_add_opts(&qemu_option_rom_opts);
3210 qemu_add_opts(&qemu_machine_opts);
3211 qemu_add_opts(&qemu_accel_opts);
3212 qemu_add_opts(&qemu_mem_opts);
3213 qemu_add_opts(&qemu_smp_opts);
3214 qemu_add_opts(&qemu_boot_opts);
3215 qemu_add_opts(&qemu_sandbox_opts);
3216 qemu_add_opts(&qemu_add_fd_opts);
3217 qemu_add_opts(&qemu_object_opts);
3218 qemu_add_opts(&qemu_tpmdev_opts);
3219 qemu_add_opts(&qemu_realtime_opts);
3220 qemu_add_opts(&qemu_msg_opts);
3221 qemu_add_opts(&qemu_name_opts);
3222 qemu_add_opts(&qemu_numa_opts);
3223 qemu_add_opts(&qemu_icount_opts);
3224 qemu_add_opts(&qemu_semihosting_config_opts);
3225 qemu_add_opts(&qemu_fw_cfg_opts);
3226 module_call_init(MODULE_INIT_OPTS);
3228 runstate_init();
3230 if (qcrypto_init(&err) < 0) {
3231 error_reportf_err(err, "cannot initialize crypto: ");
3232 exit(1);
3234 rtc_clock = QEMU_CLOCK_HOST;
3236 QLIST_INIT (&vm_change_state_head);
3237 os_setup_early_signal_handling();
3239 cpu_model = NULL;
3240 snapshot = 0;
3241 cyls = heads = secs = 0;
3242 translation = BIOS_ATA_TRANSLATION_AUTO;
3244 nb_nics = 0;
3246 bdrv_init_with_whitelist();
3248 autostart = 1;
3250 /* first pass of option parsing */
3251 optind = 1;
3252 while (optind < argc) {
3253 if (argv[optind][0] != '-') {
3254 /* disk image */
3255 optind++;
3256 } else {
3257 const QEMUOption *popt;
3259 popt = lookup_opt(argc, argv, &optarg, &optind);
3260 switch (popt->index) {
3261 case QEMU_OPTION_nodefconfig:
3262 defconfig = false;
3263 break;
3264 case QEMU_OPTION_nouserconfig:
3265 userconfig = false;
3266 break;
3271 if (defconfig && userconfig) {
3272 if (qemu_read_default_config_file() < 0) {
3273 exit(1);
3277 /* second pass of option parsing */
3278 optind = 1;
3279 for(;;) {
3280 if (optind >= argc)
3281 break;
3282 if (argv[optind][0] != '-') {
3283 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3284 } else {
3285 const QEMUOption *popt;
3287 popt = lookup_opt(argc, argv, &optarg, &optind);
3288 if (!(popt->arch_mask & arch_type)) {
3289 error_report("Option not supported for this target");
3290 exit(1);
3292 switch(popt->index) {
3293 case QEMU_OPTION_no_kvm_irqchip: {
3294 olist = qemu_find_opts("machine");
3295 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3296 break;
3298 case QEMU_OPTION_cpu:
3299 /* hw initialization will check this */
3300 cpu_model = optarg;
3301 break;
3302 case QEMU_OPTION_hda:
3304 char buf[256];
3305 if (cyls == 0)
3306 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3307 else
3308 snprintf(buf, sizeof(buf),
3309 "%s,cyls=%d,heads=%d,secs=%d%s",
3310 HD_OPTS , cyls, heads, secs,
3311 translation == BIOS_ATA_TRANSLATION_LBA ?
3312 ",trans=lba" :
3313 translation == BIOS_ATA_TRANSLATION_NONE ?
3314 ",trans=none" : "");
3315 drive_add(IF_DEFAULT, 0, optarg, buf);
3316 break;
3318 case QEMU_OPTION_hdb:
3319 case QEMU_OPTION_hdc:
3320 case QEMU_OPTION_hdd:
3321 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3322 HD_OPTS);
3323 break;
3324 case QEMU_OPTION_blockdev:
3326 Visitor *v;
3327 BlockdevOptions_queue *bdo;
3329 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3330 if (!v) {
3331 error_report_err(err);
3332 exit(1);
3335 bdo = g_new(BlockdevOptions_queue, 1);
3336 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3337 &error_fatal);
3338 visit_free(v);
3339 loc_save(&bdo->loc);
3340 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3341 break;
3343 case QEMU_OPTION_drive:
3344 if (drive_def(optarg) == NULL) {
3345 exit(1);
3347 break;
3348 case QEMU_OPTION_set:
3349 if (qemu_set_option(optarg) != 0)
3350 exit(1);
3351 break;
3352 case QEMU_OPTION_global:
3353 if (qemu_global_option(optarg) != 0)
3354 exit(1);
3355 break;
3356 case QEMU_OPTION_mtdblock:
3357 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3358 break;
3359 case QEMU_OPTION_sd:
3360 drive_add(IF_SD, -1, optarg, SD_OPTS);
3361 break;
3362 case QEMU_OPTION_pflash:
3363 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3364 break;
3365 case QEMU_OPTION_snapshot:
3366 snapshot = 1;
3367 break;
3368 case QEMU_OPTION_hdachs:
3370 const char *p;
3371 p = optarg;
3372 cyls = strtol(p, (char **)&p, 0);
3373 if (cyls < 1 || cyls > 16383)
3374 goto chs_fail;
3375 if (*p != ',')
3376 goto chs_fail;
3377 p++;
3378 heads = strtol(p, (char **)&p, 0);
3379 if (heads < 1 || heads > 16)
3380 goto chs_fail;
3381 if (*p != ',')
3382 goto chs_fail;
3383 p++;
3384 secs = strtol(p, (char **)&p, 0);
3385 if (secs < 1 || secs > 63)
3386 goto chs_fail;
3387 if (*p == ',') {
3388 p++;
3389 if (!strcmp(p, "large")) {
3390 translation = BIOS_ATA_TRANSLATION_LARGE;
3391 } else if (!strcmp(p, "rechs")) {
3392 translation = BIOS_ATA_TRANSLATION_RECHS;
3393 } else if (!strcmp(p, "none")) {
3394 translation = BIOS_ATA_TRANSLATION_NONE;
3395 } else if (!strcmp(p, "lba")) {
3396 translation = BIOS_ATA_TRANSLATION_LBA;
3397 } else if (!strcmp(p, "auto")) {
3398 translation = BIOS_ATA_TRANSLATION_AUTO;
3399 } else {
3400 goto chs_fail;
3402 } else if (*p != '\0') {
3403 chs_fail:
3404 error_report("invalid physical CHS format");
3405 exit(1);
3407 if (hda_opts != NULL) {
3408 qemu_opt_set_number(hda_opts, "cyls", cyls,
3409 &error_abort);
3410 qemu_opt_set_number(hda_opts, "heads", heads,
3411 &error_abort);
3412 qemu_opt_set_number(hda_opts, "secs", secs,
3413 &error_abort);
3414 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3415 qemu_opt_set(hda_opts, "trans", "large",
3416 &error_abort);
3417 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3418 qemu_opt_set(hda_opts, "trans", "rechs",
3419 &error_abort);
3420 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3421 qemu_opt_set(hda_opts, "trans", "lba",
3422 &error_abort);
3423 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3424 qemu_opt_set(hda_opts, "trans", "none",
3425 &error_abort);
3429 error_report("'-hdachs' is deprecated, please use '-device"
3430 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3431 break;
3432 case QEMU_OPTION_numa:
3433 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3434 optarg, true);
3435 if (!opts) {
3436 exit(1);
3438 break;
3439 case QEMU_OPTION_display:
3440 display_type = select_display(optarg);
3441 break;
3442 case QEMU_OPTION_nographic:
3443 olist = qemu_find_opts("machine");
3444 qemu_opts_parse_noisily(olist, "graphics=off", false);
3445 nographic = true;
3446 display_type = DT_NONE;
3447 break;
3448 case QEMU_OPTION_curses:
3449 #ifdef CONFIG_CURSES
3450 display_type = DT_CURSES;
3451 #else
3452 error_report("curses support is disabled");
3453 exit(1);
3454 #endif
3455 break;
3456 case QEMU_OPTION_portrait:
3457 graphic_rotate = 90;
3458 break;
3459 case QEMU_OPTION_rotate:
3460 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3461 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3462 graphic_rotate != 180 && graphic_rotate != 270) {
3463 error_report("only 90, 180, 270 deg rotation is available");
3464 exit(1);
3466 break;
3467 case QEMU_OPTION_kernel:
3468 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3469 &error_abort);
3470 break;
3471 case QEMU_OPTION_initrd:
3472 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3473 &error_abort);
3474 break;
3475 case QEMU_OPTION_append:
3476 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3477 &error_abort);
3478 break;
3479 case QEMU_OPTION_dtb:
3480 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3481 &error_abort);
3482 break;
3483 case QEMU_OPTION_cdrom:
3484 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3485 break;
3486 case QEMU_OPTION_boot:
3487 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3488 optarg, true);
3489 if (!opts) {
3490 exit(1);
3492 break;
3493 case QEMU_OPTION_fda:
3494 case QEMU_OPTION_fdb:
3495 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3496 optarg, FD_OPTS);
3497 break;
3498 case QEMU_OPTION_no_fd_bootchk:
3499 fd_bootchk = 0;
3500 break;
3501 case QEMU_OPTION_netdev:
3502 default_net = 0;
3503 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3504 exit(1);
3506 break;
3507 case QEMU_OPTION_net:
3508 default_net = 0;
3509 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3510 exit(1);
3512 break;
3513 #ifdef CONFIG_LIBISCSI
3514 case QEMU_OPTION_iscsi:
3515 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3516 optarg, false);
3517 if (!opts) {
3518 exit(1);
3520 break;
3521 #endif
3522 #ifdef CONFIG_SLIRP
3523 case QEMU_OPTION_tftp:
3524 error_report("The -tftp option is deprecated. "
3525 "Please use '-netdev user,tftp=...' instead.");
3526 legacy_tftp_prefix = optarg;
3527 break;
3528 case QEMU_OPTION_bootp:
3529 error_report("The -bootp option is deprecated. "
3530 "Please use '-netdev user,bootfile=...' instead.");
3531 legacy_bootp_filename = optarg;
3532 break;
3533 case QEMU_OPTION_redir:
3534 error_report("The -redir option is deprecated. "
3535 "Please use '-netdev user,hostfwd=...' instead.");
3536 if (net_slirp_redir(optarg) < 0)
3537 exit(1);
3538 break;
3539 #endif
3540 case QEMU_OPTION_bt:
3541 add_device_config(DEV_BT, optarg);
3542 break;
3543 case QEMU_OPTION_audio_help:
3544 AUD_help ();
3545 exit (0);
3546 break;
3547 case QEMU_OPTION_soundhw:
3548 select_soundhw (optarg);
3549 break;
3550 case QEMU_OPTION_h:
3551 help(0);
3552 break;
3553 case QEMU_OPTION_version:
3554 version();
3555 exit(0);
3556 break;
3557 case QEMU_OPTION_m:
3558 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3559 optarg, true);
3560 if (!opts) {
3561 exit(EXIT_FAILURE);
3563 break;
3564 #ifdef CONFIG_TPM
3565 case QEMU_OPTION_tpmdev:
3566 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3567 exit(1);
3569 break;
3570 #endif
3571 case QEMU_OPTION_mempath:
3572 mem_path = optarg;
3573 break;
3574 case QEMU_OPTION_mem_prealloc:
3575 mem_prealloc = 1;
3576 break;
3577 case QEMU_OPTION_d:
3578 log_mask = optarg;
3579 break;
3580 case QEMU_OPTION_D:
3581 log_file = optarg;
3582 break;
3583 case QEMU_OPTION_DFILTER:
3584 qemu_set_dfilter_ranges(optarg, &error_fatal);
3585 break;
3586 case QEMU_OPTION_s:
3587 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3588 break;
3589 case QEMU_OPTION_gdb:
3590 add_device_config(DEV_GDB, optarg);
3591 break;
3592 case QEMU_OPTION_L:
3593 if (is_help_option(optarg)) {
3594 list_data_dirs = true;
3595 } else {
3596 qemu_add_data_dir(optarg);
3598 break;
3599 case QEMU_OPTION_bios:
3600 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3601 &error_abort);
3602 break;
3603 case QEMU_OPTION_singlestep:
3604 singlestep = 1;
3605 break;
3606 case QEMU_OPTION_S:
3607 autostart = 0;
3608 break;
3609 case QEMU_OPTION_k:
3610 keyboard_layout = optarg;
3611 break;
3612 case QEMU_OPTION_localtime:
3613 rtc_utc = 0;
3614 break;
3615 case QEMU_OPTION_vga:
3616 vga_model = optarg;
3617 default_vga = 0;
3618 break;
3619 case QEMU_OPTION_g:
3621 const char *p;
3622 int w, h, depth;
3623 p = optarg;
3624 w = strtol(p, (char **)&p, 10);
3625 if (w <= 0) {
3626 graphic_error:
3627 error_report("invalid resolution or depth");
3628 exit(1);
3630 if (*p != 'x')
3631 goto graphic_error;
3632 p++;
3633 h = strtol(p, (char **)&p, 10);
3634 if (h <= 0)
3635 goto graphic_error;
3636 if (*p == 'x') {
3637 p++;
3638 depth = strtol(p, (char **)&p, 10);
3639 if (depth != 8 && depth != 15 && depth != 16 &&
3640 depth != 24 && depth != 32)
3641 goto graphic_error;
3642 } else if (*p == '\0') {
3643 depth = graphic_depth;
3644 } else {
3645 goto graphic_error;
3648 graphic_width = w;
3649 graphic_height = h;
3650 graphic_depth = depth;
3652 break;
3653 case QEMU_OPTION_echr:
3655 char *r;
3656 term_escape_char = strtol(optarg, &r, 0);
3657 if (r == optarg)
3658 printf("Bad argument to echr\n");
3659 break;
3661 case QEMU_OPTION_monitor:
3662 default_monitor = 0;
3663 if (strncmp(optarg, "none", 4)) {
3664 monitor_parse(optarg, "readline", false);
3666 break;
3667 case QEMU_OPTION_qmp:
3668 monitor_parse(optarg, "control", false);
3669 default_monitor = 0;
3670 break;
3671 case QEMU_OPTION_qmp_pretty:
3672 monitor_parse(optarg, "control", true);
3673 default_monitor = 0;
3674 break;
3675 case QEMU_OPTION_mon:
3676 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3677 true);
3678 if (!opts) {
3679 exit(1);
3681 default_monitor = 0;
3682 break;
3683 case QEMU_OPTION_chardev:
3684 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3685 optarg, true);
3686 if (!opts) {
3687 exit(1);
3689 break;
3690 case QEMU_OPTION_fsdev:
3691 olist = qemu_find_opts("fsdev");
3692 if (!olist) {
3693 error_report("fsdev support is disabled");
3694 exit(1);
3696 opts = qemu_opts_parse_noisily(olist, optarg, true);
3697 if (!opts) {
3698 exit(1);
3700 break;
3701 case QEMU_OPTION_virtfs: {
3702 QemuOpts *fsdev;
3703 QemuOpts *device;
3704 const char *writeout, *sock_fd, *socket, *path, *security_model;
3706 olist = qemu_find_opts("virtfs");
3707 if (!olist) {
3708 error_report("virtfs support is disabled");
3709 exit(1);
3711 opts = qemu_opts_parse_noisily(olist, optarg, true);
3712 if (!opts) {
3713 exit(1);
3716 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3717 qemu_opt_get(opts, "mount_tag") == NULL) {
3718 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3719 exit(1);
3721 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3722 qemu_opts_id(opts) ?:
3723 qemu_opt_get(opts, "mount_tag"),
3724 1, NULL);
3725 if (!fsdev) {
3726 error_report("duplicate or invalid fsdev id: %s",
3727 qemu_opt_get(opts, "mount_tag"));
3728 exit(1);
3731 writeout = qemu_opt_get(opts, "writeout");
3732 if (writeout) {
3733 #ifdef CONFIG_SYNC_FILE_RANGE
3734 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3735 #else
3736 error_report("writeout=immediate not supported "
3737 "on this platform");
3738 exit(1);
3739 #endif
3741 qemu_opt_set(fsdev, "fsdriver",
3742 qemu_opt_get(opts, "fsdriver"), &error_abort);
3743 path = qemu_opt_get(opts, "path");
3744 if (path) {
3745 qemu_opt_set(fsdev, "path", path, &error_abort);
3747 security_model = qemu_opt_get(opts, "security_model");
3748 if (security_model) {
3749 qemu_opt_set(fsdev, "security_model", security_model,
3750 &error_abort);
3752 socket = qemu_opt_get(opts, "socket");
3753 if (socket) {
3754 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3756 sock_fd = qemu_opt_get(opts, "sock_fd");
3757 if (sock_fd) {
3758 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3761 qemu_opt_set_bool(fsdev, "readonly",
3762 qemu_opt_get_bool(opts, "readonly", 0),
3763 &error_abort);
3764 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3765 &error_abort);
3766 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3767 qemu_opt_set(device, "fsdev",
3768 qemu_opts_id(fsdev), &error_abort);
3769 qemu_opt_set(device, "mount_tag",
3770 qemu_opt_get(opts, "mount_tag"), &error_abort);
3771 break;
3773 case QEMU_OPTION_virtfs_synth: {
3774 QemuOpts *fsdev;
3775 QemuOpts *device;
3777 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3778 1, NULL);
3779 if (!fsdev) {
3780 error_report("duplicate option: %s", "virtfs_synth");
3781 exit(1);
3783 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3785 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3786 &error_abort);
3787 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3788 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3789 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3790 break;
3792 case QEMU_OPTION_serial:
3793 add_device_config(DEV_SERIAL, optarg);
3794 default_serial = 0;
3795 if (strncmp(optarg, "mon:", 4) == 0) {
3796 default_monitor = 0;
3798 break;
3799 case QEMU_OPTION_watchdog:
3800 if (watchdog) {
3801 error_report("only one watchdog option may be given");
3802 return 1;
3804 watchdog = optarg;
3805 break;
3806 case QEMU_OPTION_watchdog_action:
3807 if (select_watchdog_action(optarg) == -1) {
3808 error_report("unknown -watchdog-action parameter");
3809 exit(1);
3811 break;
3812 case QEMU_OPTION_virtiocon:
3813 add_device_config(DEV_VIRTCON, optarg);
3814 default_virtcon = 0;
3815 if (strncmp(optarg, "mon:", 4) == 0) {
3816 default_monitor = 0;
3818 break;
3819 case QEMU_OPTION_parallel:
3820 add_device_config(DEV_PARALLEL, optarg);
3821 default_parallel = 0;
3822 if (strncmp(optarg, "mon:", 4) == 0) {
3823 default_monitor = 0;
3825 break;
3826 case QEMU_OPTION_debugcon:
3827 add_device_config(DEV_DEBUGCON, optarg);
3828 break;
3829 case QEMU_OPTION_loadvm:
3830 loadvm = optarg;
3831 break;
3832 case QEMU_OPTION_full_screen:
3833 full_screen = 1;
3834 break;
3835 case QEMU_OPTION_no_frame:
3836 no_frame = 1;
3837 break;
3838 case QEMU_OPTION_alt_grab:
3839 alt_grab = 1;
3840 break;
3841 case QEMU_OPTION_ctrl_grab:
3842 ctrl_grab = 1;
3843 break;
3844 case QEMU_OPTION_no_quit:
3845 no_quit = 1;
3846 break;
3847 case QEMU_OPTION_sdl:
3848 #ifdef CONFIG_SDL
3849 display_type = DT_SDL;
3850 break;
3851 #else
3852 error_report("SDL support is disabled");
3853 exit(1);
3854 #endif
3855 case QEMU_OPTION_pidfile:
3856 pid_file = optarg;
3857 break;
3858 case QEMU_OPTION_win2k_hack:
3859 win2k_install_hack = 1;
3860 break;
3861 case QEMU_OPTION_rtc_td_hack: {
3862 static GlobalProperty slew_lost_ticks = {
3863 .driver = "mc146818rtc",
3864 .property = "lost_tick_policy",
3865 .value = "slew",
3868 qdev_prop_register_global(&slew_lost_ticks);
3869 break;
3871 case QEMU_OPTION_acpitable:
3872 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3873 optarg, true);
3874 if (!opts) {
3875 exit(1);
3877 acpi_table_add(opts, &error_fatal);
3878 break;
3879 case QEMU_OPTION_smbios:
3880 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3881 optarg, false);
3882 if (!opts) {
3883 exit(1);
3885 smbios_entry_add(opts, &error_fatal);
3886 break;
3887 case QEMU_OPTION_fwcfg:
3888 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3889 optarg, true);
3890 if (opts == NULL) {
3891 exit(1);
3893 break;
3894 case QEMU_OPTION_enable_kvm:
3895 olist = qemu_find_opts("machine");
3896 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3897 break;
3898 case QEMU_OPTION_enable_hax:
3899 olist = qemu_find_opts("machine");
3900 qemu_opts_parse_noisily(olist, "accel=hax", false);
3901 break;
3902 case QEMU_OPTION_M:
3903 case QEMU_OPTION_machine:
3904 olist = qemu_find_opts("machine");
3905 opts = qemu_opts_parse_noisily(olist, optarg, true);
3906 if (!opts) {
3907 exit(1);
3909 break;
3910 case QEMU_OPTION_no_kvm:
3911 olist = qemu_find_opts("machine");
3912 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3913 break;
3914 case QEMU_OPTION_no_kvm_pit: {
3915 warn_report("ignoring deprecated option");
3916 break;
3918 case QEMU_OPTION_no_kvm_pit_reinjection: {
3919 static GlobalProperty kvm_pit_lost_tick_policy = {
3920 .driver = "kvm-pit",
3921 .property = "lost_tick_policy",
3922 .value = "discard",
3925 warn_report("deprecated, replaced by "
3926 "-global kvm-pit.lost_tick_policy=discard");
3927 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3928 break;
3930 case QEMU_OPTION_accel:
3931 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3932 optarg, true);
3933 optarg = qemu_opt_get(accel_opts, "accel");
3934 if (!optarg || is_help_option(optarg)) {
3935 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3936 exit(0);
3938 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3939 false, &error_abort);
3940 qemu_opt_set(opts, "accel", optarg, &error_abort);
3941 break;
3942 case QEMU_OPTION_usb:
3943 olist = qemu_find_opts("machine");
3944 qemu_opts_parse_noisily(olist, "usb=on", false);
3945 break;
3946 case QEMU_OPTION_usbdevice:
3947 error_report("'-usbdevice' is deprecated, please use "
3948 "'-device usb-...' instead");
3949 olist = qemu_find_opts("machine");
3950 qemu_opts_parse_noisily(olist, "usb=on", false);
3951 add_device_config(DEV_USB, optarg);
3952 break;
3953 case QEMU_OPTION_device:
3954 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3955 optarg, true)) {
3956 exit(1);
3958 break;
3959 case QEMU_OPTION_smp:
3960 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3961 optarg, true)) {
3962 exit(1);
3964 break;
3965 case QEMU_OPTION_vnc:
3966 vnc_parse(optarg, &error_fatal);
3967 break;
3968 case QEMU_OPTION_no_acpi:
3969 acpi_enabled = 0;
3970 break;
3971 case QEMU_OPTION_no_hpet:
3972 no_hpet = 1;
3973 break;
3974 case QEMU_OPTION_balloon:
3975 if (balloon_parse(optarg) < 0) {
3976 error_report("unknown -balloon argument %s", optarg);
3977 exit(1);
3979 break;
3980 case QEMU_OPTION_no_reboot:
3981 no_reboot = 1;
3982 break;
3983 case QEMU_OPTION_no_shutdown:
3984 no_shutdown = 1;
3985 break;
3986 case QEMU_OPTION_show_cursor:
3987 cursor_hide = 0;
3988 break;
3989 case QEMU_OPTION_uuid:
3990 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3991 error_report("failed to parse UUID string: wrong format");
3992 exit(1);
3994 qemu_uuid_set = true;
3995 break;
3996 case QEMU_OPTION_option_rom:
3997 if (nb_option_roms >= MAX_OPTION_ROMS) {
3998 error_report("too many option ROMs");
3999 exit(1);
4001 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
4002 optarg, true);
4003 if (!opts) {
4004 exit(1);
4006 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
4007 option_rom[nb_option_roms].bootindex =
4008 qemu_opt_get_number(opts, "bootindex", -1);
4009 if (!option_rom[nb_option_roms].name) {
4010 error_report("Option ROM file is not specified");
4011 exit(1);
4013 nb_option_roms++;
4014 break;
4015 case QEMU_OPTION_semihosting:
4016 semihosting.enabled = true;
4017 semihosting.target = SEMIHOSTING_TARGET_AUTO;
4018 break;
4019 case QEMU_OPTION_semihosting_config:
4020 semihosting.enabled = true;
4021 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
4022 optarg, false);
4023 if (opts != NULL) {
4024 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
4025 true);
4026 const char *target = qemu_opt_get(opts, "target");
4027 if (target != NULL) {
4028 if (strcmp("native", target) == 0) {
4029 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
4030 } else if (strcmp("gdb", target) == 0) {
4031 semihosting.target = SEMIHOSTING_TARGET_GDB;
4032 } else if (strcmp("auto", target) == 0) {
4033 semihosting.target = SEMIHOSTING_TARGET_AUTO;
4034 } else {
4035 error_report("unsupported semihosting-config %s",
4036 optarg);
4037 exit(1);
4039 } else {
4040 semihosting.target = SEMIHOSTING_TARGET_AUTO;
4042 /* Set semihosting argument count and vector */
4043 qemu_opt_foreach(opts, add_semihosting_arg,
4044 &semihosting, NULL);
4045 } else {
4046 error_report("unsupported semihosting-config %s", optarg);
4047 exit(1);
4049 break;
4050 case QEMU_OPTION_tdf:
4051 warn_report("ignoring deprecated option");
4052 break;
4053 case QEMU_OPTION_name:
4054 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
4055 optarg, true);
4056 if (!opts) {
4057 exit(1);
4059 break;
4060 case QEMU_OPTION_prom_env:
4061 if (nb_prom_envs >= MAX_PROM_ENVS) {
4062 error_report("too many prom variables");
4063 exit(1);
4065 prom_envs[nb_prom_envs] = optarg;
4066 nb_prom_envs++;
4067 break;
4068 case QEMU_OPTION_old_param:
4069 old_param = 1;
4070 break;
4071 case QEMU_OPTION_clock:
4072 /* Clock options no longer exist. Keep this option for
4073 * backward compatibility.
4075 break;
4076 case QEMU_OPTION_startdate:
4077 configure_rtc_date_offset(optarg, 1);
4078 break;
4079 case QEMU_OPTION_rtc:
4080 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
4081 false);
4082 if (!opts) {
4083 exit(1);
4085 configure_rtc(opts);
4086 break;
4087 case QEMU_OPTION_tb_size:
4088 #ifndef CONFIG_TCG
4089 error_report("TCG is disabled");
4090 exit(1);
4091 #endif
4092 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
4093 error_report("Invalid argument to -tb-size");
4094 exit(1);
4096 break;
4097 case QEMU_OPTION_icount:
4098 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
4099 optarg, true);
4100 if (!icount_opts) {
4101 exit(1);
4103 break;
4104 case QEMU_OPTION_incoming:
4105 if (!incoming) {
4106 runstate_set(RUN_STATE_INMIGRATE);
4108 incoming = optarg;
4109 break;
4110 case QEMU_OPTION_only_migratable:
4112 * TODO: we can remove this option one day, and we
4113 * should all use:
4115 * "-global migration.only-migratable=true"
4117 qemu_global_option("migration.only-migratable=true");
4118 break;
4119 case QEMU_OPTION_nodefaults:
4120 has_defaults = 0;
4121 break;
4122 case QEMU_OPTION_xen_domid:
4123 if (!(xen_available())) {
4124 error_report("Option not supported for this target");
4125 exit(1);
4127 xen_domid = atoi(optarg);
4128 break;
4129 case QEMU_OPTION_xen_create:
4130 if (!(xen_available())) {
4131 error_report("Option not supported for this target");
4132 exit(1);
4134 xen_mode = XEN_CREATE;
4135 break;
4136 case QEMU_OPTION_xen_attach:
4137 if (!(xen_available())) {
4138 error_report("Option not supported for this target");
4139 exit(1);
4141 xen_mode = XEN_ATTACH;
4142 break;
4143 case QEMU_OPTION_xen_domid_restrict:
4144 if (!(xen_available())) {
4145 error_report("Option not supported for this target");
4146 exit(1);
4148 xen_domid_restrict = true;
4149 break;
4150 case QEMU_OPTION_trace:
4151 g_free(trace_file);
4152 trace_file = trace_opt_parse(optarg);
4153 break;
4154 case QEMU_OPTION_trace_unassigned:
4155 trace_unassigned = true;
4156 break;
4157 case QEMU_OPTION_readconfig:
4159 int ret = qemu_read_config_file(optarg);
4160 if (ret < 0) {
4161 error_report("read config %s: %s", optarg,
4162 strerror(-ret));
4163 exit(1);
4165 break;
4167 case QEMU_OPTION_spice:
4168 olist = qemu_find_opts("spice");
4169 if (!olist) {
4170 error_report("spice support is disabled");
4171 exit(1);
4173 opts = qemu_opts_parse_noisily(olist, optarg, false);
4174 if (!opts) {
4175 exit(1);
4177 display_remote++;
4178 break;
4179 case QEMU_OPTION_writeconfig:
4181 FILE *fp;
4182 if (strcmp(optarg, "-") == 0) {
4183 fp = stdout;
4184 } else {
4185 fp = fopen(optarg, "w");
4186 if (fp == NULL) {
4187 error_report("open %s: %s", optarg,
4188 strerror(errno));
4189 exit(1);
4192 qemu_config_write(fp);
4193 if (fp != stdout) {
4194 fclose(fp);
4196 break;
4198 case QEMU_OPTION_qtest:
4199 qtest_chrdev = optarg;
4200 break;
4201 case QEMU_OPTION_qtest_log:
4202 qtest_log = optarg;
4203 break;
4204 case QEMU_OPTION_sandbox:
4205 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4206 optarg, true);
4207 if (!opts) {
4208 exit(1);
4210 break;
4211 case QEMU_OPTION_add_fd:
4212 #ifndef _WIN32
4213 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4214 optarg, false);
4215 if (!opts) {
4216 exit(1);
4218 #else
4219 error_report("File descriptor passing is disabled on this "
4220 "platform");
4221 exit(1);
4222 #endif
4223 break;
4224 case QEMU_OPTION_object:
4225 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4226 optarg, true);
4227 if (!opts) {
4228 exit(1);
4230 break;
4231 case QEMU_OPTION_realtime:
4232 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4233 optarg, false);
4234 if (!opts) {
4235 exit(1);
4237 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4238 break;
4239 case QEMU_OPTION_msg:
4240 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4241 false);
4242 if (!opts) {
4243 exit(1);
4245 configure_msg(opts);
4246 break;
4247 case QEMU_OPTION_dump_vmstate:
4248 if (vmstate_dump_file) {
4249 error_report("only one '-dump-vmstate' "
4250 "option may be given");
4251 exit(1);
4253 vmstate_dump_file = fopen(optarg, "w");
4254 if (vmstate_dump_file == NULL) {
4255 error_report("open %s: %s", optarg, strerror(errno));
4256 exit(1);
4258 break;
4259 default:
4260 os_parse_cmd_args(popt->index, optarg);
4265 * Clear error location left behind by the loop.
4266 * Best done right after the loop. Do not insert code here!
4268 loc_set_none();
4270 replay_configure(icount_opts);
4272 machine_class = select_machine();
4274 set_memory_options(&ram_slots, &maxram_size, machine_class);
4276 os_daemonize();
4277 rcu_disable_atfork();
4279 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4280 error_report("could not acquire pid file: %s", strerror(errno));
4281 exit(1);
4284 if (qemu_init_main_loop(&main_loop_err)) {
4285 error_report_err(main_loop_err);
4286 exit(1);
4289 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4290 parse_sandbox, NULL, NULL)) {
4291 exit(1);
4294 if (qemu_opts_foreach(qemu_find_opts("name"),
4295 parse_name, NULL, NULL)) {
4296 exit(1);
4299 #ifndef _WIN32
4300 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4301 parse_add_fd, NULL, NULL)) {
4302 exit(1);
4305 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4306 cleanup_add_fd, NULL, NULL)) {
4307 exit(1);
4309 #endif
4311 current_machine = MACHINE(object_new(object_class_get_name(
4312 OBJECT_CLASS(machine_class))));
4313 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4314 exit(0);
4316 object_property_add_child(object_get_root(), "machine",
4317 OBJECT(current_machine), &error_abort);
4319 if (machine_class->minimum_page_bits) {
4320 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4321 /* This would be a board error: specifying a minimum smaller than
4322 * a target's compile-time fixed setting.
4324 g_assert_not_reached();
4328 cpu_exec_init_all();
4330 if (machine_class->hw_version) {
4331 qemu_set_hw_version(machine_class->hw_version);
4334 if (cpu_model && is_help_option(cpu_model)) {
4335 list_cpus(stdout, &fprintf, cpu_model);
4336 exit(0);
4339 if (!trace_init_backends()) {
4340 exit(1);
4342 trace_init_file(trace_file);
4344 /* Open the logfile at this point and set the log mask if necessary.
4346 if (log_file) {
4347 qemu_set_log_filename(log_file, &error_fatal);
4350 if (log_mask) {
4351 int mask;
4352 mask = qemu_str_to_log_mask(log_mask);
4353 if (!mask) {
4354 qemu_print_log_usage(stdout);
4355 exit(1);
4357 qemu_set_log(mask);
4358 } else {
4359 qemu_set_log(0);
4362 /* add configured firmware directories */
4363 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4364 for (i = 0; dirs[i] != NULL; i++) {
4365 qemu_add_data_dir(dirs[i]);
4368 /* try to find datadir relative to the executable path */
4369 qemu_add_data_dir(os_find_datadir());
4371 /* add the datadir specified when building */
4372 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4374 /* -L help lists the data directories and exits. */
4375 if (list_data_dirs) {
4376 for (i = 0; i < data_dir_idx; i++) {
4377 printf("%s\n", data_dir[i]);
4379 exit(0);
4382 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4384 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4385 if (max_cpus > machine_class->max_cpus) {
4386 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4387 "supported by machine '%s' (%d)", max_cpus,
4388 machine_class->name, machine_class->max_cpus);
4389 exit(1);
4393 * Get the default machine options from the machine if it is not already
4394 * specified either by the configuration file or by the command line.
4396 if (machine_class->default_machine_opts) {
4397 qemu_opts_set_defaults(qemu_find_opts("machine"),
4398 machine_class->default_machine_opts, 0);
4401 qemu_opts_foreach(qemu_find_opts("device"),
4402 default_driver_check, NULL, NULL);
4403 qemu_opts_foreach(qemu_find_opts("global"),
4404 default_driver_check, NULL, NULL);
4406 if (!vga_model && !default_vga) {
4407 vga_interface_type = VGA_DEVICE;
4409 if (!has_defaults || machine_class->no_serial) {
4410 default_serial = 0;
4412 if (!has_defaults || machine_class->no_parallel) {
4413 default_parallel = 0;
4415 if (!has_defaults || !machine_class->use_virtcon) {
4416 default_virtcon = 0;
4418 if (!has_defaults || !machine_class->use_sclp) {
4419 default_sclp = 0;
4421 if (!has_defaults || machine_class->no_floppy) {
4422 default_floppy = 0;
4424 if (!has_defaults || machine_class->no_cdrom) {
4425 default_cdrom = 0;
4427 if (!has_defaults || machine_class->no_sdcard) {
4428 default_sdcard = 0;
4430 if (!has_defaults) {
4431 default_monitor = 0;
4432 default_net = 0;
4433 default_vga = 0;
4436 if (is_daemonized()) {
4437 /* According to documentation and historically, -nographic redirects
4438 * serial port, parallel port and monitor to stdio, which does not work
4439 * with -daemonize. We can redirect these to null instead, but since
4440 * -nographic is legacy, let's just error out.
4441 * We disallow -nographic only if all other ports are not redirected
4442 * explicitly, to not break existing legacy setups which uses
4443 * -nographic _and_ redirects all ports explicitly - this is valid
4444 * usage, -nographic is just a no-op in this case.
4446 if (nographic
4447 && (default_parallel || default_serial
4448 || default_monitor || default_virtcon)) {
4449 error_report("-nographic cannot be used with -daemonize");
4450 exit(1);
4452 #ifdef CONFIG_CURSES
4453 if (display_type == DT_CURSES) {
4454 error_report("curses display cannot be used with -daemonize");
4455 exit(1);
4457 #endif
4460 if (nographic) {
4461 if (default_parallel)
4462 add_device_config(DEV_PARALLEL, "null");
4463 if (default_serial && default_monitor) {
4464 add_device_config(DEV_SERIAL, "mon:stdio");
4465 } else if (default_virtcon && default_monitor) {
4466 add_device_config(DEV_VIRTCON, "mon:stdio");
4467 } else if (default_sclp && default_monitor) {
4468 add_device_config(DEV_SCLP, "mon:stdio");
4469 } else {
4470 if (default_serial)
4471 add_device_config(DEV_SERIAL, "stdio");
4472 if (default_virtcon)
4473 add_device_config(DEV_VIRTCON, "stdio");
4474 if (default_sclp) {
4475 add_device_config(DEV_SCLP, "stdio");
4477 if (default_monitor)
4478 monitor_parse("stdio", "readline", false);
4480 } else {
4481 if (default_serial)
4482 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4483 if (default_parallel)
4484 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4485 if (default_monitor)
4486 monitor_parse("vc:80Cx24C", "readline", false);
4487 if (default_virtcon)
4488 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4489 if (default_sclp) {
4490 add_device_config(DEV_SCLP, "vc:80Cx24C");
4494 #if defined(CONFIG_VNC)
4495 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4496 display_remote++;
4498 #endif
4499 if (display_type == DT_DEFAULT && !display_remote) {
4500 #if defined(CONFIG_GTK)
4501 display_type = DT_GTK;
4502 #elif defined(CONFIG_SDL)
4503 display_type = DT_SDL;
4504 #elif defined(CONFIG_COCOA)
4505 display_type = DT_COCOA;
4506 #elif defined(CONFIG_VNC)
4507 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4508 #else
4509 display_type = DT_NONE;
4510 #endif
4513 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4514 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4515 "for SDL, ignoring option");
4517 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4518 error_report("-no-quit is only valid for GTK and SDL, "
4519 "ignoring option");
4522 if (display_type == DT_GTK) {
4523 early_gtk_display_init(request_opengl);
4526 if (display_type == DT_SDL) {
4527 sdl_display_early_init(request_opengl);
4530 qemu_console_early_init();
4532 if (request_opengl == 1 && display_opengl == 0) {
4533 #if defined(CONFIG_OPENGL)
4534 error_report("OpenGL is not supported by the display");
4535 #else
4536 error_report("OpenGL support is disabled");
4537 #endif
4538 exit(1);
4541 page_size_init();
4542 socket_init();
4544 if (qemu_opts_foreach(qemu_find_opts("object"),
4545 user_creatable_add_opts_foreach,
4546 object_create_initial, NULL)) {
4547 exit(1);
4550 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4551 chardev_init_func, NULL, NULL)) {
4552 exit(1);
4555 #ifdef CONFIG_VIRTFS
4556 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4557 fsdev_init_func, NULL, NULL)) {
4558 exit(1);
4560 #endif
4562 if (qemu_opts_foreach(qemu_find_opts("device"),
4563 device_help_func, NULL, NULL)) {
4564 exit(0);
4567 machine_opts = qemu_get_machine_opts();
4568 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4569 NULL)) {
4570 object_unref(OBJECT(current_machine));
4571 exit(1);
4574 configure_accelerator(current_machine);
4577 * Register all the global properties, including accel properties,
4578 * machine properties, and user-specified ones.
4580 register_global_properties(current_machine);
4583 * Migration object can only be created after global properties
4584 * are applied correctly.
4586 migration_object_init();
4588 if (qtest_chrdev) {
4589 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4592 machine_opts = qemu_get_machine_opts();
4593 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4594 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4595 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4596 bios_name = qemu_opt_get(machine_opts, "firmware");
4598 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4599 if (opts) {
4600 boot_order = qemu_opt_get(opts, "order");
4601 if (boot_order) {
4602 validate_bootdevices(boot_order, &error_fatal);
4605 boot_once = qemu_opt_get(opts, "once");
4606 if (boot_once) {
4607 validate_bootdevices(boot_once, &error_fatal);
4610 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4611 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4614 if (!boot_order) {
4615 boot_order = machine_class->default_boot_order;
4618 if (!kernel_cmdline) {
4619 kernel_cmdline = "";
4620 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4623 linux_boot = (kernel_filename != NULL);
4625 if (!linux_boot && *kernel_cmdline != '\0') {
4626 error_report("-append only allowed with -kernel option");
4627 exit(1);
4630 if (!linux_boot && initrd_filename != NULL) {
4631 error_report("-initrd only allowed with -kernel option");
4632 exit(1);
4635 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4636 /* fall back to the -kernel/-append */
4637 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4640 os_set_line_buffering();
4642 /* spice needs the timers to be initialized by this point */
4643 qemu_spice_init();
4645 cpu_ticks_init();
4646 if (icount_opts) {
4647 if (!tcg_enabled()) {
4648 error_report("-icount is not allowed with hardware virtualization");
4649 exit(1);
4651 configure_icount(icount_opts, &error_abort);
4652 qemu_opts_del(icount_opts);
4655 if (tcg_enabled()) {
4656 qemu_tcg_configure(accel_opts, &error_fatal);
4659 if (default_net) {
4660 QemuOptsList *net = qemu_find_opts("net");
4661 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4662 #ifdef CONFIG_SLIRP
4663 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4664 #endif
4667 colo_info_init();
4669 if (net_init_clients() < 0) {
4670 exit(1);
4673 if (qemu_opts_foreach(qemu_find_opts("object"),
4674 user_creatable_add_opts_foreach,
4675 object_create_delayed, NULL)) {
4676 exit(1);
4679 #ifdef CONFIG_TPM
4680 if (tpm_init() < 0) {
4681 exit(1);
4683 #endif
4685 /* init the bluetooth world */
4686 if (foreach_device_config(DEV_BT, bt_parse))
4687 exit(1);
4689 if (!xen_enabled()) {
4690 /* On 32-bit hosts, QEMU is limited by virtual address space */
4691 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4692 error_report("at most 2047 MB RAM can be simulated");
4693 exit(1);
4697 blk_mig_init();
4698 ram_mig_init();
4700 /* If the currently selected machine wishes to override the units-per-bus
4701 * property of its default HBA interface type, do so now. */
4702 if (machine_class->units_per_default_bus) {
4703 override_max_devs(machine_class->block_default_type,
4704 machine_class->units_per_default_bus);
4707 /* open the virtual block devices */
4708 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4709 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4711 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4712 loc_push_restore(&bdo->loc);
4713 qmp_blockdev_add(bdo->bdo, &error_fatal);
4714 loc_pop(&bdo->loc);
4715 qapi_free_BlockdevOptions(bdo->bdo);
4716 g_free(bdo);
4718 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4719 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4720 NULL, NULL);
4722 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4723 &machine_class->block_default_type, NULL)) {
4724 exit(1);
4727 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4728 CDROM_OPTS);
4729 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4730 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4732 parse_numa_opts(current_machine);
4734 if (qemu_opts_foreach(qemu_find_opts("mon"),
4735 mon_init_func, NULL, NULL)) {
4736 exit(1);
4739 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4740 exit(1);
4741 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4742 exit(1);
4743 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4744 exit(1);
4745 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4746 exit(1);
4748 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4749 exit(1);
4751 /* If no default VGA is requested, the default is "none". */
4752 if (default_vga) {
4753 if (machine_class->default_display) {
4754 vga_model = machine_class->default_display;
4755 } else if (vga_interface_available(VGA_CIRRUS)) {
4756 vga_model = "cirrus";
4757 } else if (vga_interface_available(VGA_STD)) {
4758 vga_model = "std";
4761 if (vga_model) {
4762 select_vgahw(vga_model);
4765 if (watchdog) {
4766 i = select_watchdog(watchdog);
4767 if (i > 0)
4768 exit (i == 1 ? 1 : 0);
4771 /* This checkpoint is required by replay to separate prior clock
4772 reading from the other reads, because timer polling functions query
4773 clock values from the log. */
4774 replay_checkpoint(CHECKPOINT_INIT);
4775 qdev_machine_init();
4777 current_machine->ram_size = ram_size;
4778 current_machine->maxram_size = maxram_size;
4779 current_machine->ram_slots = ram_slots;
4780 current_machine->boot_order = boot_order;
4781 current_machine->cpu_model = cpu_model;
4784 /* parse features once if machine provides default cpu_type */
4785 if (machine_class->default_cpu_type) {
4786 current_machine->cpu_type = machine_class->default_cpu_type;
4787 if (cpu_model) {
4788 current_machine->cpu_type =
4789 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
4793 machine_run_board_init(current_machine);
4795 realtime_init();
4797 soundhw_init();
4799 if (hax_enabled()) {
4800 hax_sync_vcpus();
4803 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4804 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4805 exit(1);
4808 /* init USB devices */
4809 if (machine_usb(current_machine)) {
4810 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4811 exit(1);
4814 /* Check if IGD GFX passthrough. */
4815 igd_gfx_passthru();
4817 /* init generic devices */
4818 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4819 if (qemu_opts_foreach(qemu_find_opts("device"),
4820 device_init_func, NULL, NULL)) {
4821 exit(1);
4824 cpu_synchronize_all_post_init();
4826 rom_reset_order_override();
4829 * Create frontends for -drive if=scsi leftovers.
4830 * Normally, frontends for -drive get created by machine
4831 * initialization for onboard SCSI HBAs. However, we create a few
4832 * more ever since SCSI qdevification, but this is pretty much an
4833 * implementation accident, and deprecated.
4835 scsi_legacy_handle_cmdline();
4837 /* Did we create any drives that we failed to create a device for? */
4838 drive_check_orphaned();
4840 /* Don't warn about the default network setup that you get if
4841 * no command line -net or -netdev options are specified. There
4842 * are two cases that we would otherwise complain about:
4843 * (1) board doesn't support a NIC but the implicit "-net nic"
4844 * requested one
4845 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4846 * sets up a nic that isn't connected to anything.
4848 if (!default_net) {
4849 net_check_clients();
4853 if (boot_once) {
4854 qemu_boot_set(boot_once, &error_fatal);
4855 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4858 ds = init_displaystate();
4860 /* init local displays */
4861 switch (display_type) {
4862 case DT_CURSES:
4863 curses_display_init(ds, full_screen);
4864 break;
4865 case DT_SDL:
4866 sdl_display_init(ds, full_screen, no_frame);
4867 break;
4868 case DT_COCOA:
4869 cocoa_display_init(ds, full_screen);
4870 break;
4871 case DT_GTK:
4872 gtk_display_init(ds, full_screen, grab_on_hover);
4873 break;
4874 default:
4875 break;
4878 /* must be after terminal init, SDL library changes signal handlers */
4879 os_setup_signal_handling();
4881 /* init remote displays */
4882 #ifdef CONFIG_VNC
4883 qemu_opts_foreach(qemu_find_opts("vnc"),
4884 vnc_init_func, NULL, NULL);
4885 #endif
4887 if (using_spice) {
4888 qemu_spice_display_init();
4891 #ifdef CONFIG_OPENGL_DMABUF
4892 if (display_type == DT_EGL) {
4893 egl_headless_init();
4895 #endif
4897 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4898 exit(1);
4901 qdev_machine_creation_done();
4903 /* TODO: once all bus devices are qdevified, this should be done
4904 * when bus is created by qdev.c */
4905 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4906 qemu_run_machine_init_done_notifiers();
4908 if (rom_check_and_register_reset() != 0) {
4909 error_report("rom check and register reset failed");
4910 exit(1);
4913 replay_start();
4915 /* This checkpoint is required by replay to separate prior clock
4916 reading from the other reads, because timer polling functions query
4917 clock values from the log. */
4918 replay_checkpoint(CHECKPOINT_RESET);
4919 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4920 register_global_state();
4921 if (replay_mode != REPLAY_MODE_NONE) {
4922 replay_vmstate_init();
4923 } else if (loadvm) {
4924 Error *local_err = NULL;
4925 if (load_snapshot(loadvm, &local_err) < 0) {
4926 error_report_err(local_err);
4927 autostart = 0;
4931 qdev_prop_check_globals();
4932 if (vmstate_dump_file) {
4933 /* dump and exit */
4934 dump_vmstate_json_to_file(vmstate_dump_file);
4935 return 0;
4938 if (incoming) {
4939 Error *local_err = NULL;
4940 qemu_start_incoming_migration(incoming, &local_err);
4941 if (local_err) {
4942 error_reportf_err(local_err, "-incoming %s: ", incoming);
4943 exit(1);
4945 } else if (autostart) {
4946 vm_start();
4949 os_setup_post();
4951 main_loop();
4952 replay_disable_events();
4953 iothread_stop_all();
4955 pause_all_vcpus();
4956 bdrv_close_all();
4957 res_free();
4959 /* vhost-user must be cleaned up before chardevs. */
4960 net_cleanup();
4961 audio_cleanup();
4962 monitor_cleanup();
4963 qemu_chr_cleanup();
4964 user_creatable_cleanup();
4965 /* TODO: unref root container, check all devices are ok */
4967 return 0;