Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob79642a78ee0a0d27ce779133d03314fabb8f09b6
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/isa/isa.h"
68 #include "hw/scsi/scsi.h"
69 #include "hw/display/vga.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-visit.h"
106 #include "qapi/qmp/qjson.h"
107 #include "qemu/option.h"
108 #include "qemu/config-file.h"
109 #include "qemu-options.h"
110 #include "qmp-commands.h"
111 #include "qemu/main-loop.h"
112 #ifdef CONFIG_VIRTFS
113 #include "fsdev/qemu-fsdev.h"
114 #endif
115 #include "sysemu/qtest.h"
117 #include "disas/disas.h"
120 #include "slirp/libslirp.h"
122 #include "trace-root.h"
123 #include "trace/control.h"
124 #include "qemu/queue.h"
125 #include "sysemu/arch_init.h"
127 #include "ui/qemu-spice.h"
128 #include "qapi/string-input-visitor.h"
129 #include "qapi/opts-visitor.h"
130 #include "qom/object_interfaces.h"
131 #include "qapi-event.h"
132 #include "exec/semihost.h"
133 #include "crypto/init.h"
134 #include "sysemu/replay.h"
135 #include "qapi/qmp/qerror.h"
136 #include "sysemu/iothread.h"
138 #define MAX_VIRTIO_CONSOLES 1
139 #define MAX_SCLP_CONSOLES 1
141 static const char *data_dir[16];
142 static int data_dir_idx;
143 const char *bios_name = NULL;
144 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
145 int request_opengl = -1;
146 int display_opengl;
147 const char* keyboard_layout = NULL;
148 ram_addr_t ram_size;
149 const char *mem_path = NULL;
150 int mem_prealloc = 0; /* force preallocation of physical target memory */
151 bool enable_mlock = false;
152 int nb_nics;
153 NICInfo nd_table[MAX_NICS];
154 int autostart;
155 static int rtc_utc = 1;
156 static int rtc_date_offset = -1; /* -1 means no change */
157 QEMUClockType rtc_clock;
158 int vga_interface_type = VGA_NONE;
159 static int full_screen = 0;
160 static int no_frame = 0;
161 int no_quit = 0;
162 static bool grab_on_hover;
163 Chardev *serial_hds[MAX_SERIAL_PORTS];
164 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
165 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
166 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
167 int win2k_install_hack = 0;
168 int singlestep = 0;
169 int smp_cpus;
170 unsigned int max_cpus;
171 int smp_cores = 1;
172 int smp_threads = 1;
173 int acpi_enabled = 1;
174 int no_hpet = 0;
175 int fd_bootchk = 1;
176 static int no_reboot;
177 int no_shutdown = 0;
178 int cursor_hide = 1;
179 int graphic_rotate = 0;
180 const char *watchdog;
181 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
182 int nb_option_roms;
183 int old_param = 0;
184 const char *qemu_name;
185 int alt_grab = 0;
186 int ctrl_grab = 0;
187 unsigned int nb_prom_envs = 0;
188 const char *prom_envs[MAX_PROM_ENVS];
189 int boot_menu;
190 bool boot_strict;
191 uint8_t *boot_splash_filedata;
192 size_t boot_splash_filedata_size;
193 uint8_t qemu_extra_params_fw[2];
195 int icount_align_option;
197 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
198 * little-endian "wire format" described in the SMBIOS 2.6 specification.
200 QemuUUID qemu_uuid;
201 bool qemu_uuid_set;
203 /* Trace unassigned memory or i/o accesses. */
204 bool trace_unassigned;
206 static NotifierList exit_notifiers =
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
209 static NotifierList machine_init_done_notifiers =
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
212 bool xen_allowed;
213 uint32_t xen_domid;
214 enum xen_mode xen_mode = XEN_EMULATE;
215 bool xen_domid_restrict;
217 static int has_defaults = 1;
218 static int default_serial = 1;
219 static int default_parallel = 1;
220 static int default_virtcon = 1;
221 static int default_sclp = 1;
222 static int default_monitor = 1;
223 static int default_floppy = 1;
224 static int default_cdrom = 1;
225 static int default_sdcard = 1;
226 static int default_vga = 1;
227 static int default_net = 1;
229 static struct {
230 const char *driver;
231 int *flag;
232 } default_list[] = {
233 { .driver = "isa-serial", .flag = &default_serial },
234 { .driver = "isa-parallel", .flag = &default_parallel },
235 { .driver = "isa-fdc", .flag = &default_floppy },
236 { .driver = "floppy", .flag = &default_floppy },
237 { .driver = "ide-cd", .flag = &default_cdrom },
238 { .driver = "ide-hd", .flag = &default_cdrom },
239 { .driver = "ide-drive", .flag = &default_cdrom },
240 { .driver = "scsi-cd", .flag = &default_cdrom },
241 { .driver = "scsi-hd", .flag = &default_cdrom },
242 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
243 { .driver = "virtio-serial", .flag = &default_virtcon },
244 { .driver = "VGA", .flag = &default_vga },
245 { .driver = "isa-vga", .flag = &default_vga },
246 { .driver = "cirrus-vga", .flag = &default_vga },
247 { .driver = "isa-cirrus-vga", .flag = &default_vga },
248 { .driver = "vmware-svga", .flag = &default_vga },
249 { .driver = "qxl-vga", .flag = &default_vga },
250 { .driver = "virtio-vga", .flag = &default_vga },
253 static QemuOptsList qemu_rtc_opts = {
254 .name = "rtc",
255 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
256 .desc = {
258 .name = "base",
259 .type = QEMU_OPT_STRING,
261 .name = "clock",
262 .type = QEMU_OPT_STRING,
264 .name = "driftfix",
265 .type = QEMU_OPT_STRING,
267 { /* end of list */ }
271 static QemuOptsList qemu_sandbox_opts = {
272 .name = "sandbox",
273 .implied_opt_name = "enable",
274 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
275 .desc = {
277 .name = "enable",
278 .type = QEMU_OPT_BOOL,
281 .name = "obsolete",
282 .type = QEMU_OPT_STRING,
285 .name = "elevateprivileges",
286 .type = QEMU_OPT_STRING,
289 .name = "spawn",
290 .type = QEMU_OPT_STRING,
293 .name = "resourcecontrol",
294 .type = QEMU_OPT_STRING,
296 { /* end of list */ }
300 static QemuOptsList qemu_option_rom_opts = {
301 .name = "option-rom",
302 .implied_opt_name = "romfile",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
304 .desc = {
306 .name = "bootindex",
307 .type = QEMU_OPT_NUMBER,
308 }, {
309 .name = "romfile",
310 .type = QEMU_OPT_STRING,
312 { /* end of list */ }
316 static QemuOptsList qemu_machine_opts = {
317 .name = "machine",
318 .implied_opt_name = "type",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
321 .desc = {
323 * no elements => accept any
324 * sanity checking will happen later
325 * when setting machine properties
331 static QemuOptsList qemu_accel_opts = {
332 .name = "accel",
333 .implied_opt_name = "accel",
334 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
335 .merge_lists = true,
336 .desc = {
338 .name = "accel",
339 .type = QEMU_OPT_STRING,
340 .help = "Select the type of accelerator",
343 .name = "thread",
344 .type = QEMU_OPT_STRING,
345 .help = "Enable/disable multi-threaded TCG",
347 { /* end of list */ }
351 static QemuOptsList qemu_boot_opts = {
352 .name = "boot-opts",
353 .implied_opt_name = "order",
354 .merge_lists = true,
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
356 .desc = {
358 .name = "order",
359 .type = QEMU_OPT_STRING,
360 }, {
361 .name = "once",
362 .type = QEMU_OPT_STRING,
363 }, {
364 .name = "menu",
365 .type = QEMU_OPT_BOOL,
366 }, {
367 .name = "splash",
368 .type = QEMU_OPT_STRING,
369 }, {
370 .name = "splash-time",
371 .type = QEMU_OPT_STRING,
372 }, {
373 .name = "reboot-timeout",
374 .type = QEMU_OPT_STRING,
375 }, {
376 .name = "strict",
377 .type = QEMU_OPT_BOOL,
379 { /*End of list */ }
383 static QemuOptsList qemu_add_fd_opts = {
384 .name = "add-fd",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
386 .desc = {
388 .name = "fd",
389 .type = QEMU_OPT_NUMBER,
390 .help = "file descriptor of which a duplicate is added to fd set",
392 .name = "set",
393 .type = QEMU_OPT_NUMBER,
394 .help = "ID of the fd set to add fd to",
396 .name = "opaque",
397 .type = QEMU_OPT_STRING,
398 .help = "free-form string used to describe fd",
400 { /* end of list */ }
404 static QemuOptsList qemu_object_opts = {
405 .name = "object",
406 .implied_opt_name = "qom-type",
407 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
408 .desc = {
413 static QemuOptsList qemu_tpmdev_opts = {
414 .name = "tpmdev",
415 .implied_opt_name = "type",
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
417 .desc = {
418 /* options are defined in the TPM backends */
419 { /* end of list */ }
423 static QemuOptsList qemu_realtime_opts = {
424 .name = "realtime",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
426 .desc = {
428 .name = "mlock",
429 .type = QEMU_OPT_BOOL,
431 { /* end of list */ }
435 static QemuOptsList qemu_msg_opts = {
436 .name = "msg",
437 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
438 .desc = {
440 .name = "timestamp",
441 .type = QEMU_OPT_BOOL,
443 { /* end of list */ }
447 static QemuOptsList qemu_name_opts = {
448 .name = "name",
449 .implied_opt_name = "guest",
450 .merge_lists = true,
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
452 .desc = {
454 .name = "guest",
455 .type = QEMU_OPT_STRING,
456 .help = "Sets the name of the guest.\n"
457 "This name will be displayed in the SDL window caption.\n"
458 "The name will also be used for the VNC server",
459 }, {
460 .name = "process",
461 .type = QEMU_OPT_STRING,
462 .help = "Sets the name of the QEMU process, as shown in top etc",
463 }, {
464 .name = "debug-threads",
465 .type = QEMU_OPT_BOOL,
466 .help = "When enabled, name the individual threads; defaults off.\n"
467 "NOTE: The thread names are for debugging and not a\n"
468 "stable API.",
470 { /* End of list */ }
474 static QemuOptsList qemu_mem_opts = {
475 .name = "memory",
476 .implied_opt_name = "size",
477 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
478 .merge_lists = true,
479 .desc = {
481 .name = "size",
482 .type = QEMU_OPT_SIZE,
485 .name = "slots",
486 .type = QEMU_OPT_NUMBER,
489 .name = "maxmem",
490 .type = QEMU_OPT_SIZE,
492 { /* end of list */ }
496 static QemuOptsList qemu_icount_opts = {
497 .name = "icount",
498 .implied_opt_name = "shift",
499 .merge_lists = true,
500 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
501 .desc = {
503 .name = "shift",
504 .type = QEMU_OPT_STRING,
505 }, {
506 .name = "align",
507 .type = QEMU_OPT_BOOL,
508 }, {
509 .name = "sleep",
510 .type = QEMU_OPT_BOOL,
511 }, {
512 .name = "rr",
513 .type = QEMU_OPT_STRING,
514 }, {
515 .name = "rrfile",
516 .type = QEMU_OPT_STRING,
517 }, {
518 .name = "rrsnapshot",
519 .type = QEMU_OPT_STRING,
521 { /* end of list */ }
525 static QemuOptsList qemu_semihosting_config_opts = {
526 .name = "semihosting-config",
527 .implied_opt_name = "enable",
528 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
529 .desc = {
531 .name = "enable",
532 .type = QEMU_OPT_BOOL,
533 }, {
534 .name = "target",
535 .type = QEMU_OPT_STRING,
536 }, {
537 .name = "arg",
538 .type = QEMU_OPT_STRING,
540 { /* end of list */ }
544 static QemuOptsList qemu_fw_cfg_opts = {
545 .name = "fw_cfg",
546 .implied_opt_name = "name",
547 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
548 .desc = {
550 .name = "name",
551 .type = QEMU_OPT_STRING,
552 .help = "Sets the fw_cfg name of the blob to be inserted",
553 }, {
554 .name = "file",
555 .type = QEMU_OPT_STRING,
556 .help = "Sets the name of the file from which\n"
557 "the fw_cfg blob will be loaded",
558 }, {
559 .name = "string",
560 .type = QEMU_OPT_STRING,
561 .help = "Sets content of the blob to be inserted from a string",
563 { /* end of list */ }
568 * Get machine options
570 * Returns: machine options (never null).
572 QemuOpts *qemu_get_machine_opts(void)
574 return qemu_find_opts_singleton("machine");
577 const char *qemu_get_vm_name(void)
579 return qemu_name;
582 static void res_free(void)
584 g_free(boot_splash_filedata);
585 boot_splash_filedata = NULL;
588 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
590 const char *driver = qemu_opt_get(opts, "driver");
591 int i;
593 if (!driver)
594 return 0;
595 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
596 if (strcmp(default_list[i].driver, driver) != 0)
597 continue;
598 *(default_list[i].flag) = 0;
600 return 0;
603 /***********************************************************/
604 /* QEMU state */
606 static RunState current_run_state = RUN_STATE_PRELAUNCH;
608 /* We use RUN_STATE__MAX but any invalid value will do */
609 static RunState vmstop_requested = RUN_STATE__MAX;
610 static QemuMutex vmstop_lock;
612 typedef struct {
613 RunState from;
614 RunState to;
615 } RunStateTransition;
617 static const RunStateTransition runstate_transitions_def[] = {
618 /* from -> to */
619 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
620 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
621 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
624 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
625 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
626 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
628 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
629 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
630 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
631 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
633 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
634 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
636 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
637 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
638 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
640 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
641 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
645 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
647 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
648 { RUN_STATE_PAUSED, RUN_STATE_COLO},
650 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
651 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
652 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
654 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
655 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
658 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
659 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
660 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
661 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
662 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
664 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
665 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
667 { RUN_STATE_COLO, RUN_STATE_RUNNING },
669 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
670 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
671 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
672 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
673 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
674 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
675 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
676 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
677 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
678 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
679 { RUN_STATE_RUNNING, RUN_STATE_COLO},
681 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
683 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
684 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
685 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
687 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
688 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
689 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
690 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
691 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
692 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
694 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
695 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
696 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
697 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
699 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
700 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
701 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
703 { RUN_STATE__MAX, RUN_STATE__MAX },
706 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
708 bool runstate_check(RunState state)
710 return current_run_state == state;
713 bool runstate_store(char *str, size_t size)
715 const char *state = RunState_str(current_run_state);
716 size_t len = strlen(state) + 1;
718 if (len > size) {
719 return false;
721 memcpy(str, state, len);
722 return true;
725 static void runstate_init(void)
727 const RunStateTransition *p;
729 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
730 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
731 runstate_valid_transitions[p->from][p->to] = true;
734 qemu_mutex_init(&vmstop_lock);
737 /* This function will abort() on invalid state transitions */
738 void runstate_set(RunState new_state)
740 assert(new_state < RUN_STATE__MAX);
742 if (current_run_state == new_state) {
743 return;
746 if (!runstate_valid_transitions[current_run_state][new_state]) {
747 error_report("invalid runstate transition: '%s' -> '%s'",
748 RunState_str(current_run_state),
749 RunState_str(new_state));
750 abort();
752 trace_runstate_set(new_state);
753 current_run_state = new_state;
756 int runstate_is_running(void)
758 return runstate_check(RUN_STATE_RUNNING);
761 bool runstate_needs_reset(void)
763 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
764 runstate_check(RUN_STATE_SHUTDOWN);
767 StatusInfo *qmp_query_status(Error **errp)
769 StatusInfo *info = g_malloc0(sizeof(*info));
771 info->running = runstate_is_running();
772 info->singlestep = singlestep;
773 info->status = current_run_state;
775 return info;
778 bool qemu_vmstop_requested(RunState *r)
780 qemu_mutex_lock(&vmstop_lock);
781 *r = vmstop_requested;
782 vmstop_requested = RUN_STATE__MAX;
783 qemu_mutex_unlock(&vmstop_lock);
784 return *r < RUN_STATE__MAX;
787 void qemu_system_vmstop_request_prepare(void)
789 qemu_mutex_lock(&vmstop_lock);
792 void qemu_system_vmstop_request(RunState state)
794 vmstop_requested = state;
795 qemu_mutex_unlock(&vmstop_lock);
796 qemu_notify_event();
799 /***********************************************************/
800 /* real time host monotonic timer */
802 static time_t qemu_time(void)
804 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
807 /***********************************************************/
808 /* host time/date access */
809 void qemu_get_timedate(struct tm *tm, int offset)
811 time_t ti = qemu_time();
813 ti += offset;
814 if (rtc_date_offset == -1) {
815 if (rtc_utc)
816 gmtime_r(&ti, tm);
817 else
818 localtime_r(&ti, tm);
819 } else {
820 ti -= rtc_date_offset;
821 gmtime_r(&ti, tm);
825 int qemu_timedate_diff(struct tm *tm)
827 time_t seconds;
829 if (rtc_date_offset == -1)
830 if (rtc_utc)
831 seconds = mktimegm(tm);
832 else {
833 struct tm tmp = *tm;
834 tmp.tm_isdst = -1; /* use timezone to figure it out */
835 seconds = mktime(&tmp);
837 else
838 seconds = mktimegm(tm) + rtc_date_offset;
840 return seconds - qemu_time();
843 static void configure_rtc_date_offset(const char *startdate, int legacy)
845 time_t rtc_start_date;
846 struct tm tm;
848 if (!strcmp(startdate, "now") && legacy) {
849 rtc_date_offset = -1;
850 } else {
851 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
852 &tm.tm_year,
853 &tm.tm_mon,
854 &tm.tm_mday,
855 &tm.tm_hour,
856 &tm.tm_min,
857 &tm.tm_sec) == 6) {
858 /* OK */
859 } else if (sscanf(startdate, "%d-%d-%d",
860 &tm.tm_year,
861 &tm.tm_mon,
862 &tm.tm_mday) == 3) {
863 tm.tm_hour = 0;
864 tm.tm_min = 0;
865 tm.tm_sec = 0;
866 } else {
867 goto date_fail;
869 tm.tm_year -= 1900;
870 tm.tm_mon--;
871 rtc_start_date = mktimegm(&tm);
872 if (rtc_start_date == -1) {
873 date_fail:
874 error_report("invalid date format");
875 error_printf("valid formats: "
876 "'2006-06-17T16:01:21' or '2006-06-17'\n");
877 exit(1);
879 rtc_date_offset = qemu_time() - rtc_start_date;
883 static void configure_rtc(QemuOpts *opts)
885 const char *value;
887 value = qemu_opt_get(opts, "base");
888 if (value) {
889 if (!strcmp(value, "utc")) {
890 rtc_utc = 1;
891 } else if (!strcmp(value, "localtime")) {
892 Error *blocker = NULL;
893 rtc_utc = 0;
894 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
895 "-rtc base=localtime");
896 replay_add_blocker(blocker);
897 } else {
898 configure_rtc_date_offset(value, 0);
901 value = qemu_opt_get(opts, "clock");
902 if (value) {
903 if (!strcmp(value, "host")) {
904 rtc_clock = QEMU_CLOCK_HOST;
905 } else if (!strcmp(value, "rt")) {
906 rtc_clock = QEMU_CLOCK_REALTIME;
907 } else if (!strcmp(value, "vm")) {
908 rtc_clock = QEMU_CLOCK_VIRTUAL;
909 } else {
910 error_report("invalid option value '%s'", value);
911 exit(1);
914 value = qemu_opt_get(opts, "driftfix");
915 if (value) {
916 if (!strcmp(value, "slew")) {
917 static GlobalProperty slew_lost_ticks = {
918 .driver = "mc146818rtc",
919 .property = "lost_tick_policy",
920 .value = "slew",
923 qdev_prop_register_global(&slew_lost_ticks);
924 } else if (!strcmp(value, "none")) {
925 /* discard is default */
926 } else {
927 error_report("invalid option value '%s'", value);
928 exit(1);
933 /***********************************************************/
934 /* Bluetooth support */
935 static int nb_hcis;
936 static int cur_hci;
937 static struct HCIInfo *hci_table[MAX_NICS];
939 struct HCIInfo *qemu_next_hci(void)
941 if (cur_hci == nb_hcis)
942 return &null_hci;
944 return hci_table[cur_hci++];
947 static int bt_hci_parse(const char *str)
949 struct HCIInfo *hci;
950 bdaddr_t bdaddr;
952 if (nb_hcis >= MAX_NICS) {
953 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
954 return -1;
957 hci = hci_init(str);
958 if (!hci)
959 return -1;
961 bdaddr.b[0] = 0x52;
962 bdaddr.b[1] = 0x54;
963 bdaddr.b[2] = 0x00;
964 bdaddr.b[3] = 0x12;
965 bdaddr.b[4] = 0x34;
966 bdaddr.b[5] = 0x56 + nb_hcis;
967 hci->bdaddr_set(hci, bdaddr.b);
969 hci_table[nb_hcis++] = hci;
971 return 0;
974 static void bt_vhci_add(int vlan_id)
976 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
978 if (!vlan->slave)
979 warn_report("adding a VHCI to an empty scatternet %i",
980 vlan_id);
982 bt_vhci_init(bt_new_hci(vlan));
985 static struct bt_device_s *bt_device_add(const char *opt)
987 struct bt_scatternet_s *vlan;
988 int vlan_id = 0;
989 char *endp = strstr(opt, ",vlan=");
990 int len = (endp ? endp - opt : strlen(opt)) + 1;
991 char devname[10];
993 pstrcpy(devname, MIN(sizeof(devname), len), opt);
995 if (endp) {
996 vlan_id = strtol(endp + 6, &endp, 0);
997 if (*endp) {
998 error_report("unrecognised bluetooth vlan Id");
999 return 0;
1003 vlan = qemu_find_bt_vlan(vlan_id);
1005 if (!vlan->slave)
1006 warn_report("adding a slave device to an empty scatternet %i",
1007 vlan_id);
1009 if (!strcmp(devname, "keyboard"))
1010 return bt_keyboard_init(vlan);
1012 error_report("unsupported bluetooth device '%s'", devname);
1013 return 0;
1016 static int bt_parse(const char *opt)
1018 const char *endp, *p;
1019 int vlan;
1021 if (strstart(opt, "hci", &endp)) {
1022 if (!*endp || *endp == ',') {
1023 if (*endp)
1024 if (!strstart(endp, ",vlan=", 0))
1025 opt = endp + 1;
1027 return bt_hci_parse(opt);
1029 } else if (strstart(opt, "vhci", &endp)) {
1030 if (!*endp || *endp == ',') {
1031 if (*endp) {
1032 if (strstart(endp, ",vlan=", &p)) {
1033 vlan = strtol(p, (char **) &endp, 0);
1034 if (*endp) {
1035 error_report("bad scatternet '%s'", p);
1036 return 1;
1038 } else {
1039 error_report("bad parameter '%s'", endp + 1);
1040 return 1;
1042 } else
1043 vlan = 0;
1045 bt_vhci_add(vlan);
1046 return 0;
1048 } else if (strstart(opt, "device:", &endp))
1049 return !bt_device_add(endp);
1051 error_report("bad bluetooth parameter '%s'", opt);
1052 return 1;
1055 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1057 if (qemu_opt_get_bool(opts, "enable", false)) {
1058 #ifdef CONFIG_SECCOMP
1059 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1060 | QEMU_SECCOMP_SET_OBSOLETE;
1061 const char *value = NULL;
1063 value = qemu_opt_get(opts, "obsolete");
1064 if (value) {
1065 if (g_str_equal(value, "allow")) {
1066 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1067 } else if (g_str_equal(value, "deny")) {
1068 /* this is the default option, this if is here
1069 * to provide a little bit of consistency for
1070 * the command line */
1071 } else {
1072 error_report("invalid argument for obsolete");
1073 return -1;
1077 value = qemu_opt_get(opts, "elevateprivileges");
1078 if (value) {
1079 if (g_str_equal(value, "deny")) {
1080 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1081 } else if (g_str_equal(value, "children")) {
1082 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1084 /* calling prctl directly because we're
1085 * not sure if host has CAP_SYS_ADMIN set*/
1086 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1087 error_report("failed to set no_new_privs "
1088 "aborting");
1089 return -1;
1091 } else if (g_str_equal(value, "allow")) {
1092 /* default value */
1093 } else {
1094 error_report("invalid argument for elevateprivileges");
1095 return -1;
1099 value = qemu_opt_get(opts, "spawn");
1100 if (value) {
1101 if (g_str_equal(value, "deny")) {
1102 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1103 } else if (g_str_equal(value, "allow")) {
1104 /* default value */
1105 } else {
1106 error_report("invalid argument for spawn");
1107 return -1;
1111 value = qemu_opt_get(opts, "resourcecontrol");
1112 if (value) {
1113 if (g_str_equal(value, "deny")) {
1114 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1115 } else if (g_str_equal(value, "allow")) {
1116 /* default value */
1117 } else {
1118 error_report("invalid argument for resourcecontrol");
1119 return -1;
1123 if (seccomp_start(seccomp_opts) < 0) {
1124 error_report("failed to install seccomp syscall filter "
1125 "in the kernel");
1126 return -1;
1128 #else
1129 error_report("seccomp support is disabled");
1130 return -1;
1131 #endif
1134 return 0;
1137 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1139 const char *proc_name;
1141 if (qemu_opt_get(opts, "debug-threads")) {
1142 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1144 qemu_name = qemu_opt_get(opts, "guest");
1146 proc_name = qemu_opt_get(opts, "process");
1147 if (proc_name) {
1148 os_set_proc_name(proc_name);
1151 return 0;
1154 bool defaults_enabled(void)
1156 return has_defaults;
1159 #ifndef _WIN32
1160 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1162 int fd, dupfd, flags;
1163 int64_t fdset_id;
1164 const char *fd_opaque = NULL;
1165 AddfdInfo *fdinfo;
1167 fd = qemu_opt_get_number(opts, "fd", -1);
1168 fdset_id = qemu_opt_get_number(opts, "set", -1);
1169 fd_opaque = qemu_opt_get(opts, "opaque");
1171 if (fd < 0) {
1172 error_report("fd option is required and must be non-negative");
1173 return -1;
1176 if (fd <= STDERR_FILENO) {
1177 error_report("fd cannot be a standard I/O stream");
1178 return -1;
1182 * All fds inherited across exec() necessarily have FD_CLOEXEC
1183 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1185 flags = fcntl(fd, F_GETFD);
1186 if (flags == -1 || (flags & FD_CLOEXEC)) {
1187 error_report("fd is not valid or already in use");
1188 return -1;
1191 if (fdset_id < 0) {
1192 error_report("set option is required and must be non-negative");
1193 return -1;
1196 #ifdef F_DUPFD_CLOEXEC
1197 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1198 #else
1199 dupfd = dup(fd);
1200 if (dupfd != -1) {
1201 qemu_set_cloexec(dupfd);
1203 #endif
1204 if (dupfd == -1) {
1205 error_report("error duplicating fd: %s", strerror(errno));
1206 return -1;
1209 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1210 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1211 &error_abort);
1212 g_free(fdinfo);
1214 return 0;
1217 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1219 int fd;
1221 fd = qemu_opt_get_number(opts, "fd", -1);
1222 close(fd);
1224 return 0;
1226 #endif
1228 /***********************************************************/
1229 /* QEMU Block devices */
1231 #define HD_OPTS "media=disk"
1232 #define CDROM_OPTS "media=cdrom"
1233 #define FD_OPTS ""
1234 #define PFLASH_OPTS ""
1235 #define MTD_OPTS ""
1236 #define SD_OPTS ""
1238 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1240 BlockInterfaceType *block_default_type = opaque;
1242 return drive_new(opts, *block_default_type) == NULL;
1245 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1247 if (qemu_opt_get(opts, "snapshot") == NULL) {
1248 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1250 return 0;
1253 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1254 int index, const char *optstr)
1256 QemuOpts *opts;
1257 DriveInfo *dinfo;
1259 if (!enable || drive_get_by_index(type, index)) {
1260 return;
1263 opts = drive_add(type, index, NULL, optstr);
1264 if (snapshot) {
1265 drive_enable_snapshot(NULL, opts, NULL);
1268 dinfo = drive_new(opts, type);
1269 if (!dinfo) {
1270 exit(1);
1272 dinfo->is_default = true;
1276 static QemuOptsList qemu_smp_opts = {
1277 .name = "smp-opts",
1278 .implied_opt_name = "cpus",
1279 .merge_lists = true,
1280 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1281 .desc = {
1283 .name = "cpus",
1284 .type = QEMU_OPT_NUMBER,
1285 }, {
1286 .name = "sockets",
1287 .type = QEMU_OPT_NUMBER,
1288 }, {
1289 .name = "cores",
1290 .type = QEMU_OPT_NUMBER,
1291 }, {
1292 .name = "threads",
1293 .type = QEMU_OPT_NUMBER,
1294 }, {
1295 .name = "maxcpus",
1296 .type = QEMU_OPT_NUMBER,
1298 { /*End of list */ }
1302 static void smp_parse(QemuOpts *opts)
1304 if (opts) {
1305 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1306 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1307 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1308 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1310 /* compute missing values, prefer sockets over cores over threads */
1311 if (cpus == 0 || sockets == 0) {
1312 sockets = sockets > 0 ? sockets : 1;
1313 cores = cores > 0 ? cores : 1;
1314 threads = threads > 0 ? threads : 1;
1315 if (cpus == 0) {
1316 cpus = cores * threads * sockets;
1318 } else if (cores == 0) {
1319 threads = threads > 0 ? threads : 1;
1320 cores = cpus / (sockets * threads);
1321 cores = cores > 0 ? cores : 1;
1322 } else if (threads == 0) {
1323 threads = cpus / (cores * sockets);
1324 threads = threads > 0 ? threads : 1;
1325 } else if (sockets * cores * threads < cpus) {
1326 error_report("cpu topology: "
1327 "sockets (%u) * cores (%u) * threads (%u) < "
1328 "smp_cpus (%u)",
1329 sockets, cores, threads, cpus);
1330 exit(1);
1333 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1335 if (max_cpus < cpus) {
1336 error_report("maxcpus must be equal to or greater than smp");
1337 exit(1);
1340 if (sockets * cores * threads > max_cpus) {
1341 error_report("cpu topology: "
1342 "sockets (%u) * cores (%u) * threads (%u) > "
1343 "maxcpus (%u)",
1344 sockets, cores, threads, max_cpus);
1345 exit(1);
1348 smp_cpus = cpus;
1349 smp_cores = cores;
1350 smp_threads = threads;
1353 if (smp_cpus > 1) {
1354 Error *blocker = NULL;
1355 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1356 replay_add_blocker(blocker);
1360 static void realtime_init(void)
1362 if (enable_mlock) {
1363 if (os_mlock() < 0) {
1364 error_report("locking memory failed");
1365 exit(1);
1371 static void configure_msg(QemuOpts *opts)
1373 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1376 /***********************************************************/
1377 /* Semihosting */
1379 typedef struct SemihostingConfig {
1380 bool enabled;
1381 SemihostingTarget target;
1382 const char **argv;
1383 int argc;
1384 const char *cmdline; /* concatenated argv */
1385 } SemihostingConfig;
1387 static SemihostingConfig semihosting;
1389 bool semihosting_enabled(void)
1391 return semihosting.enabled;
1394 SemihostingTarget semihosting_get_target(void)
1396 return semihosting.target;
1399 const char *semihosting_get_arg(int i)
1401 if (i >= semihosting.argc) {
1402 return NULL;
1404 return semihosting.argv[i];
1407 int semihosting_get_argc(void)
1409 return semihosting.argc;
1412 const char *semihosting_get_cmdline(void)
1414 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1415 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1417 return semihosting.cmdline;
1420 static int add_semihosting_arg(void *opaque,
1421 const char *name, const char *val,
1422 Error **errp)
1424 SemihostingConfig *s = opaque;
1425 if (strcmp(name, "arg") == 0) {
1426 s->argc++;
1427 /* one extra element as g_strjoinv() expects NULL-terminated array */
1428 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1429 s->argv[s->argc - 1] = val;
1430 s->argv[s->argc] = NULL;
1432 return 0;
1435 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1436 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1438 char *cmd_token;
1440 /* argv[0] */
1441 add_semihosting_arg(&semihosting, "arg", file, NULL);
1443 /* split -append and initialize argv[1..n] */
1444 cmd_token = strtok(g_strdup(cmd), " ");
1445 while (cmd_token) {
1446 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1447 cmd_token = strtok(NULL, " ");
1451 /* Now we still need this for compatibility with XEN. */
1452 bool has_igd_gfx_passthru;
1453 static void igd_gfx_passthru(void)
1455 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1458 /***********************************************************/
1459 /* USB devices */
1461 static int usb_device_add(const char *devname)
1463 USBDevice *dev = NULL;
1464 #ifndef CONFIG_LINUX
1465 const char *p;
1466 #endif
1468 if (!machine_usb(current_machine)) {
1469 return -1;
1472 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1473 dev = usbdevice_create(devname);
1474 if (dev)
1475 goto done;
1477 /* the other ones */
1478 #ifndef CONFIG_LINUX
1479 /* only the linux version is qdev-ified, usb-bsd still needs this */
1480 if (strstart(devname, "host:", &p)) {
1481 dev = usb_host_device_open(usb_bus_find(-1), p);
1483 #endif
1484 if (!dev)
1485 return -1;
1487 done:
1488 return 0;
1491 static int usb_parse(const char *cmdline)
1493 int r;
1494 r = usb_device_add(cmdline);
1495 if (r < 0) {
1496 error_report("could not add USB device '%s'", cmdline);
1498 return r;
1501 /***********************************************************/
1502 /* machine registration */
1504 MachineState *current_machine;
1506 static MachineClass *find_machine(const char *name)
1508 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1509 MachineClass *mc = NULL;
1511 for (el = machines; el; el = el->next) {
1512 MachineClass *temp = el->data;
1514 if (!strcmp(temp->name, name)) {
1515 mc = temp;
1516 break;
1518 if (temp->alias &&
1519 !strcmp(temp->alias, name)) {
1520 mc = temp;
1521 break;
1525 g_slist_free(machines);
1526 return mc;
1529 MachineClass *find_default_machine(void)
1531 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1532 MachineClass *mc = NULL;
1534 for (el = machines; el; el = el->next) {
1535 MachineClass *temp = el->data;
1537 if (temp->is_default) {
1538 mc = temp;
1539 break;
1543 g_slist_free(machines);
1544 return mc;
1547 MachineInfoList *qmp_query_machines(Error **errp)
1549 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1550 MachineInfoList *mach_list = NULL;
1552 for (el = machines; el; el = el->next) {
1553 MachineClass *mc = el->data;
1554 MachineInfoList *entry;
1555 MachineInfo *info;
1557 info = g_malloc0(sizeof(*info));
1558 if (mc->is_default) {
1559 info->has_is_default = true;
1560 info->is_default = true;
1563 if (mc->alias) {
1564 info->has_alias = true;
1565 info->alias = g_strdup(mc->alias);
1568 info->name = g_strdup(mc->name);
1569 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1570 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1572 entry = g_malloc0(sizeof(*entry));
1573 entry->value = info;
1574 entry->next = mach_list;
1575 mach_list = entry;
1578 g_slist_free(machines);
1579 return mach_list;
1582 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1584 ObjectProperty *prop;
1585 ObjectPropertyIterator iter;
1587 if (!qemu_opt_has_help_opt(opts)) {
1588 return 0;
1591 object_property_iter_init(&iter, OBJECT(machine));
1592 while ((prop = object_property_iter_next(&iter))) {
1593 if (!prop->set) {
1594 continue;
1597 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1598 prop->name, prop->type);
1599 if (prop->description) {
1600 error_printf(" (%s)\n", prop->description);
1601 } else {
1602 error_printf("\n");
1606 return 1;
1609 /***********************************************************/
1610 /* main execution loop */
1612 struct vm_change_state_entry {
1613 VMChangeStateHandler *cb;
1614 void *opaque;
1615 QLIST_ENTRY (vm_change_state_entry) entries;
1618 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1620 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1621 void *opaque)
1623 VMChangeStateEntry *e;
1625 e = g_malloc0(sizeof (*e));
1627 e->cb = cb;
1628 e->opaque = opaque;
1629 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1630 return e;
1633 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1635 QLIST_REMOVE (e, entries);
1636 g_free (e);
1639 void vm_state_notify(int running, RunState state)
1641 VMChangeStateEntry *e, *next;
1643 trace_vm_state_notify(running, state);
1645 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1646 e->cb(e->opaque, running, state);
1650 static ShutdownCause reset_requested;
1651 static ShutdownCause shutdown_requested;
1652 static int shutdown_signal;
1653 static pid_t shutdown_pid;
1654 static int powerdown_requested;
1655 static int debug_requested;
1656 static int suspend_requested;
1657 static WakeupReason wakeup_reason;
1658 static NotifierList powerdown_notifiers =
1659 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1660 static NotifierList suspend_notifiers =
1661 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1662 static NotifierList wakeup_notifiers =
1663 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1664 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1666 ShutdownCause qemu_shutdown_requested_get(void)
1668 return shutdown_requested;
1671 ShutdownCause qemu_reset_requested_get(void)
1673 return reset_requested;
1676 static int qemu_shutdown_requested(void)
1678 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1681 static void qemu_kill_report(void)
1683 if (!qtest_driver() && shutdown_signal) {
1684 if (shutdown_pid == 0) {
1685 /* This happens for eg ^C at the terminal, so it's worth
1686 * avoiding printing an odd message in that case.
1688 error_report("terminating on signal %d", shutdown_signal);
1689 } else {
1690 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1692 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1693 shutdown_signal, shutdown_pid,
1694 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1695 g_free(shutdown_cmd);
1697 shutdown_signal = 0;
1701 static ShutdownCause qemu_reset_requested(void)
1703 ShutdownCause r = reset_requested;
1705 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1706 reset_requested = SHUTDOWN_CAUSE_NONE;
1707 return r;
1709 return SHUTDOWN_CAUSE_NONE;
1712 static int qemu_suspend_requested(void)
1714 int r = suspend_requested;
1715 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1716 suspend_requested = 0;
1717 return r;
1719 return false;
1722 static WakeupReason qemu_wakeup_requested(void)
1724 return wakeup_reason;
1727 static int qemu_powerdown_requested(void)
1729 int r = powerdown_requested;
1730 powerdown_requested = 0;
1731 return r;
1734 static int qemu_debug_requested(void)
1736 int r = debug_requested;
1737 debug_requested = 0;
1738 return r;
1742 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1744 void qemu_system_reset(ShutdownCause reason)
1746 MachineClass *mc;
1748 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1750 cpu_synchronize_all_states();
1752 if (mc && mc->reset) {
1753 mc->reset();
1754 } else {
1755 qemu_devices_reset();
1757 if (reason) {
1758 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1759 &error_abort);
1761 cpu_synchronize_all_post_reset();
1764 void qemu_system_guest_panicked(GuestPanicInformation *info)
1766 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1768 if (current_cpu) {
1769 current_cpu->crash_occurred = true;
1771 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1772 !!info, info, &error_abort);
1773 vm_stop(RUN_STATE_GUEST_PANICKED);
1774 if (!no_shutdown) {
1775 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1776 !!info, info, &error_abort);
1777 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1780 if (info) {
1781 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1782 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1783 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1784 info->u.hyper_v.arg1,
1785 info->u.hyper_v.arg2,
1786 info->u.hyper_v.arg3,
1787 info->u.hyper_v.arg4,
1788 info->u.hyper_v.arg5);
1790 qapi_free_GuestPanicInformation(info);
1794 void qemu_system_reset_request(ShutdownCause reason)
1796 if (no_reboot) {
1797 shutdown_requested = reason;
1798 } else {
1799 reset_requested = reason;
1801 cpu_stop_current();
1802 qemu_notify_event();
1805 static void qemu_system_suspend(void)
1807 pause_all_vcpus();
1808 notifier_list_notify(&suspend_notifiers, NULL);
1809 runstate_set(RUN_STATE_SUSPENDED);
1810 qapi_event_send_suspend(&error_abort);
1813 void qemu_system_suspend_request(void)
1815 if (runstate_check(RUN_STATE_SUSPENDED)) {
1816 return;
1818 suspend_requested = 1;
1819 cpu_stop_current();
1820 qemu_notify_event();
1823 void qemu_register_suspend_notifier(Notifier *notifier)
1825 notifier_list_add(&suspend_notifiers, notifier);
1828 void qemu_system_wakeup_request(WakeupReason reason)
1830 trace_system_wakeup_request(reason);
1832 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1833 return;
1835 if (!(wakeup_reason_mask & (1 << reason))) {
1836 return;
1838 runstate_set(RUN_STATE_RUNNING);
1839 wakeup_reason = reason;
1840 qemu_notify_event();
1843 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1845 if (enabled) {
1846 wakeup_reason_mask |= (1 << reason);
1847 } else {
1848 wakeup_reason_mask &= ~(1 << reason);
1852 void qemu_register_wakeup_notifier(Notifier *notifier)
1854 notifier_list_add(&wakeup_notifiers, notifier);
1857 void qemu_system_killed(int signal, pid_t pid)
1859 shutdown_signal = signal;
1860 shutdown_pid = pid;
1861 no_shutdown = 0;
1863 /* Cannot call qemu_system_shutdown_request directly because
1864 * we are in a signal handler.
1866 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1867 qemu_notify_event();
1870 void qemu_system_shutdown_request(ShutdownCause reason)
1872 trace_qemu_system_shutdown_request(reason);
1873 replay_shutdown_request(reason);
1874 shutdown_requested = reason;
1875 qemu_notify_event();
1878 static void qemu_system_powerdown(void)
1880 qapi_event_send_powerdown(&error_abort);
1881 notifier_list_notify(&powerdown_notifiers, NULL);
1884 void qemu_system_powerdown_request(void)
1886 trace_qemu_system_powerdown_request();
1887 powerdown_requested = 1;
1888 qemu_notify_event();
1891 void qemu_register_powerdown_notifier(Notifier *notifier)
1893 notifier_list_add(&powerdown_notifiers, notifier);
1896 void qemu_system_debug_request(void)
1898 debug_requested = 1;
1899 qemu_notify_event();
1902 static bool main_loop_should_exit(void)
1904 RunState r;
1905 ShutdownCause request;
1907 if (qemu_debug_requested()) {
1908 vm_stop(RUN_STATE_DEBUG);
1910 if (qemu_suspend_requested()) {
1911 qemu_system_suspend();
1913 request = qemu_shutdown_requested();
1914 if (request) {
1915 qemu_kill_report();
1916 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1917 &error_abort);
1918 if (no_shutdown) {
1919 vm_stop(RUN_STATE_SHUTDOWN);
1920 } else {
1921 return true;
1924 request = qemu_reset_requested();
1925 if (request) {
1926 pause_all_vcpus();
1927 qemu_system_reset(request);
1928 resume_all_vcpus();
1929 if (!runstate_check(RUN_STATE_RUNNING) &&
1930 !runstate_check(RUN_STATE_INMIGRATE)) {
1931 runstate_set(RUN_STATE_PRELAUNCH);
1934 if (qemu_wakeup_requested()) {
1935 pause_all_vcpus();
1936 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1937 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1938 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1939 resume_all_vcpus();
1940 qapi_event_send_wakeup(&error_abort);
1942 if (qemu_powerdown_requested()) {
1943 qemu_system_powerdown();
1945 if (qemu_vmstop_requested(&r)) {
1946 vm_stop(r);
1948 return false;
1951 static void main_loop(void)
1953 #ifdef CONFIG_PROFILER
1954 int64_t ti;
1955 #endif
1956 do {
1957 #ifdef CONFIG_PROFILER
1958 ti = profile_getclock();
1959 #endif
1960 main_loop_wait(false);
1961 #ifdef CONFIG_PROFILER
1962 dev_time += profile_getclock() - ti;
1963 #endif
1964 } while (!main_loop_should_exit());
1967 static void version(void)
1969 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1970 QEMU_COPYRIGHT "\n");
1973 static void QEMU_NORETURN help(int exitcode)
1975 version();
1976 printf("usage: %s [options] [disk_image]\n\n"
1977 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1978 error_get_progname());
1980 #define QEMU_OPTIONS_GENERATE_HELP
1981 #include "qemu-options-wrapper.h"
1983 printf("\nDuring emulation, the following keys are useful:\n"
1984 "ctrl-alt-f toggle full screen\n"
1985 "ctrl-alt-n switch to virtual console 'n'\n"
1986 "ctrl-alt toggle mouse and keyboard grab\n"
1987 "\n"
1988 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1989 "\n"
1990 QEMU_HELP_BOTTOM "\n");
1992 exit(exitcode);
1995 #define HAS_ARG 0x0001
1997 typedef struct QEMUOption {
1998 const char *name;
1999 int flags;
2000 int index;
2001 uint32_t arch_mask;
2002 } QEMUOption;
2004 static const QEMUOption qemu_options[] = {
2005 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2006 #define QEMU_OPTIONS_GENERATE_OPTIONS
2007 #include "qemu-options-wrapper.h"
2008 { NULL },
2011 typedef struct VGAInterfaceInfo {
2012 const char *opt_name; /* option name */
2013 const char *name; /* human-readable name */
2014 /* Class names indicating that support is available.
2015 * If no class is specified, the interface is always available */
2016 const char *class_names[2];
2017 } VGAInterfaceInfo;
2019 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2020 [VGA_NONE] = {
2021 .opt_name = "none",
2023 [VGA_STD] = {
2024 .opt_name = "std",
2025 .name = "standard VGA",
2026 .class_names = { "VGA", "isa-vga" },
2028 [VGA_CIRRUS] = {
2029 .opt_name = "cirrus",
2030 .name = "Cirrus VGA",
2031 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2033 [VGA_VMWARE] = {
2034 .opt_name = "vmware",
2035 .name = "VMWare SVGA",
2036 .class_names = { "vmware-svga" },
2038 [VGA_VIRTIO] = {
2039 .opt_name = "virtio",
2040 .name = "Virtio VGA",
2041 .class_names = { "virtio-vga" },
2043 [VGA_QXL] = {
2044 .opt_name = "qxl",
2045 .name = "QXL VGA",
2046 .class_names = { "qxl-vga" },
2048 [VGA_TCX] = {
2049 .opt_name = "tcx",
2050 .name = "TCX framebuffer",
2051 .class_names = { "SUNW,tcx" },
2053 [VGA_CG3] = {
2054 .opt_name = "cg3",
2055 .name = "CG3 framebuffer",
2056 .class_names = { "cgthree" },
2058 [VGA_XENFB] = {
2059 .opt_name = "xenfb",
2063 static bool vga_interface_available(VGAInterfaceType t)
2065 VGAInterfaceInfo *ti = &vga_interfaces[t];
2067 assert(t < VGA_TYPE_MAX);
2068 return !ti->class_names[0] ||
2069 object_class_by_name(ti->class_names[0]) ||
2070 object_class_by_name(ti->class_names[1]);
2073 static void select_vgahw(const char *p)
2075 const char *opts;
2076 int t;
2078 assert(vga_interface_type == VGA_NONE);
2079 for (t = 0; t < VGA_TYPE_MAX; t++) {
2080 VGAInterfaceInfo *ti = &vga_interfaces[t];
2081 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2082 if (!vga_interface_available(t)) {
2083 error_report("%s not available", ti->name);
2084 exit(1);
2086 vga_interface_type = t;
2087 break;
2090 if (t == VGA_TYPE_MAX) {
2091 invalid_vga:
2092 error_report("unknown vga type: %s", p);
2093 exit(1);
2095 while (*opts) {
2096 const char *nextopt;
2098 if (strstart(opts, ",retrace=", &nextopt)) {
2099 opts = nextopt;
2100 if (strstart(opts, "dumb", &nextopt))
2101 vga_retrace_method = VGA_RETRACE_DUMB;
2102 else if (strstart(opts, "precise", &nextopt))
2103 vga_retrace_method = VGA_RETRACE_PRECISE;
2104 else goto invalid_vga;
2105 } else goto invalid_vga;
2106 opts = nextopt;
2110 typedef enum DisplayType {
2111 DT_DEFAULT,
2112 DT_CURSES,
2113 DT_SDL,
2114 DT_COCOA,
2115 DT_GTK,
2116 DT_EGL,
2117 DT_NONE,
2118 } DisplayType;
2120 static DisplayType select_display(const char *p)
2122 const char *opts;
2123 DisplayType display = DT_DEFAULT;
2125 if (strstart(p, "sdl", &opts)) {
2126 #ifdef CONFIG_SDL
2127 display = DT_SDL;
2128 while (*opts) {
2129 const char *nextopt;
2131 if (strstart(opts, ",frame=", &nextopt)) {
2132 opts = nextopt;
2133 if (strstart(opts, "on", &nextopt)) {
2134 no_frame = 0;
2135 } else if (strstart(opts, "off", &nextopt)) {
2136 no_frame = 1;
2137 } else {
2138 goto invalid_sdl_args;
2140 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2141 opts = nextopt;
2142 if (strstart(opts, "on", &nextopt)) {
2143 alt_grab = 1;
2144 } else if (strstart(opts, "off", &nextopt)) {
2145 alt_grab = 0;
2146 } else {
2147 goto invalid_sdl_args;
2149 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2150 opts = nextopt;
2151 if (strstart(opts, "on", &nextopt)) {
2152 ctrl_grab = 1;
2153 } else if (strstart(opts, "off", &nextopt)) {
2154 ctrl_grab = 0;
2155 } else {
2156 goto invalid_sdl_args;
2158 } else if (strstart(opts, ",window_close=", &nextopt)) {
2159 opts = nextopt;
2160 if (strstart(opts, "on", &nextopt)) {
2161 no_quit = 0;
2162 } else if (strstart(opts, "off", &nextopt)) {
2163 no_quit = 1;
2164 } else {
2165 goto invalid_sdl_args;
2167 } else if (strstart(opts, ",gl=", &nextopt)) {
2168 opts = nextopt;
2169 if (strstart(opts, "on", &nextopt)) {
2170 request_opengl = 1;
2171 } else if (strstart(opts, "off", &nextopt)) {
2172 request_opengl = 0;
2173 } else {
2174 goto invalid_sdl_args;
2176 } else {
2177 invalid_sdl_args:
2178 error_report("invalid SDL option string");
2179 exit(1);
2181 opts = nextopt;
2183 #else
2184 error_report("SDL support is disabled");
2185 exit(1);
2186 #endif
2187 } else if (strstart(p, "vnc", &opts)) {
2188 if (*opts == '=') {
2189 vnc_parse(opts + 1, &error_fatal);
2190 } else {
2191 error_report("VNC requires a display argument vnc=<display>");
2192 exit(1);
2194 } else if (strstart(p, "egl-headless", &opts)) {
2195 #ifdef CONFIG_OPENGL_DMABUF
2196 request_opengl = 1;
2197 display_opengl = 1;
2198 display = DT_EGL;
2199 #else
2200 fprintf(stderr, "egl support is disabled\n");
2201 exit(1);
2202 #endif
2203 } else if (strstart(p, "curses", &opts)) {
2204 #ifdef CONFIG_CURSES
2205 display = DT_CURSES;
2206 #else
2207 error_report("curses support is disabled");
2208 exit(1);
2209 #endif
2210 } else if (strstart(p, "gtk", &opts)) {
2211 #ifdef CONFIG_GTK
2212 display = DT_GTK;
2213 while (*opts) {
2214 const char *nextopt;
2216 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2217 opts = nextopt;
2218 if (strstart(opts, "on", &nextopt)) {
2219 grab_on_hover = true;
2220 } else if (strstart(opts, "off", &nextopt)) {
2221 grab_on_hover = false;
2222 } else {
2223 goto invalid_gtk_args;
2225 } else if (strstart(opts, ",gl=", &nextopt)) {
2226 opts = nextopt;
2227 if (strstart(opts, "on", &nextopt)) {
2228 request_opengl = 1;
2229 } else if (strstart(opts, "off", &nextopt)) {
2230 request_opengl = 0;
2231 } else {
2232 goto invalid_gtk_args;
2234 } else {
2235 invalid_gtk_args:
2236 error_report("invalid GTK option string");
2237 exit(1);
2239 opts = nextopt;
2241 #else
2242 error_report("GTK support is disabled");
2243 exit(1);
2244 #endif
2245 } else if (strstart(p, "none", &opts)) {
2246 display = DT_NONE;
2247 } else {
2248 error_report("unknown display type");
2249 exit(1);
2252 return display;
2255 static int balloon_parse(const char *arg)
2257 QemuOpts *opts;
2259 if (strcmp(arg, "none") == 0) {
2260 return 0;
2263 if (!strncmp(arg, "virtio", 6)) {
2264 if (arg[6] == ',') {
2265 /* have params -> parse them */
2266 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2267 false);
2268 if (!opts)
2269 return -1;
2270 } else {
2271 /* create empty opts */
2272 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2273 &error_abort);
2275 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2276 return 0;
2279 return -1;
2282 #if defined(CONFIG_RUBY)
2283 /* Ruby interface for QEMU. See README.EXT for programming example. */
2284 #warning mit ruby
2285 #include <ruby.h>
2287 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2289 int i;
2290 for (i = 0; i < argc; i++) {
2291 argv[i] = StringValue(argv[i]);
2292 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2294 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2295 //~ mode = 0666; /* default value */
2296 //~ }
2297 return INT2FIX(argc);
2300 void Init_qemu(void)
2302 printf("%s\n", __func__);
2303 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2304 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2305 #if 0
2306 rb_define_method();
2307 #endif
2309 #endif /* CONFIG_RUBY */
2311 #if defined(CONFIG_DLL)
2312 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2314 return FALSE;
2316 #endif /* CONFIG_DLL */
2318 char *qemu_find_file(int type, const char *name)
2320 int i;
2321 const char *subdir;
2322 char *buf;
2324 /* Try the name as a straight path first */
2325 if (access(name, R_OK) == 0) {
2326 trace_load_file(name, name);
2327 return g_strdup(name);
2330 switch (type) {
2331 case QEMU_FILE_TYPE_BIOS:
2332 subdir = "";
2333 break;
2334 case QEMU_FILE_TYPE_KEYMAP:
2335 subdir = "keymaps/";
2336 break;
2337 default:
2338 abort();
2341 for (i = 0; i < data_dir_idx; i++) {
2342 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2343 if (access(buf, R_OK) == 0) {
2344 trace_load_file(name, buf);
2345 return buf;
2347 g_free(buf);
2349 return NULL;
2352 static void qemu_add_data_dir(const char *path)
2354 int i;
2356 if (path == NULL) {
2357 return;
2359 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2360 return;
2362 for (i = 0; i < data_dir_idx; i++) {
2363 if (strcmp(data_dir[i], path) == 0) {
2364 return; /* duplicate */
2367 data_dir[data_dir_idx++] = path;
2370 static inline bool nonempty_str(const char *str)
2372 return str && *str;
2375 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2377 gchar *buf;
2378 size_t size;
2379 const char *name, *file, *str;
2380 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2382 if (fw_cfg == NULL) {
2383 error_report("fw_cfg device not available");
2384 return -1;
2386 name = qemu_opt_get(opts, "name");
2387 file = qemu_opt_get(opts, "file");
2388 str = qemu_opt_get(opts, "string");
2390 /* we need name and either a file or the content string */
2391 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2392 error_report("invalid argument(s)");
2393 return -1;
2395 if (nonempty_str(file) && nonempty_str(str)) {
2396 error_report("file and string are mutually exclusive");
2397 return -1;
2399 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2400 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2401 return -1;
2403 if (strncmp(name, "opt/", 4) != 0) {
2404 warn_report("externally provided fw_cfg item names "
2405 "should be prefixed with \"opt/\"");
2407 if (nonempty_str(str)) {
2408 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2409 buf = g_memdup(str, size);
2410 } else {
2411 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2412 error_report("can't load %s", file);
2413 return -1;
2416 /* For legacy, keep user files in a specific global order. */
2417 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2418 fw_cfg_add_file(fw_cfg, name, buf, size);
2419 fw_cfg_reset_order_override(fw_cfg);
2420 return 0;
2423 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2425 return qdev_device_help(opts);
2428 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2430 Error *err = NULL;
2431 DeviceState *dev;
2433 dev = qdev_device_add(opts, &err);
2434 if (!dev) {
2435 error_report_err(err);
2436 return -1;
2438 object_unref(OBJECT(dev));
2439 return 0;
2442 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2444 Error *local_err = NULL;
2446 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2447 if (local_err) {
2448 error_report_err(local_err);
2449 return -1;
2451 exit(0);
2453 return 0;
2456 #ifdef CONFIG_VIRTFS
2457 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2459 return qemu_fsdev_add(opts);
2461 #endif
2463 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2465 Chardev *chr;
2466 const char *chardev;
2467 const char *mode;
2468 int flags;
2470 mode = qemu_opt_get(opts, "mode");
2471 if (mode == NULL) {
2472 mode = "readline";
2474 if (strcmp(mode, "readline") == 0) {
2475 flags = MONITOR_USE_READLINE;
2476 } else if (strcmp(mode, "control") == 0) {
2477 flags = MONITOR_USE_CONTROL;
2478 } else {
2479 error_report("unknown monitor mode \"%s\"", mode);
2480 exit(1);
2483 if (qemu_opt_get_bool(opts, "pretty", 0))
2484 flags |= MONITOR_USE_PRETTY;
2486 if (qemu_opt_get_bool(opts, "default", 0)) {
2487 error_report("option 'default' does nothing and is deprecated");
2490 chardev = qemu_opt_get(opts, "chardev");
2491 chr = qemu_chr_find(chardev);
2492 if (chr == NULL) {
2493 error_report("chardev \"%s\" not found", chardev);
2494 exit(1);
2497 monitor_init(chr, flags);
2498 return 0;
2501 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2503 static int monitor_device_index = 0;
2504 QemuOpts *opts;
2505 const char *p;
2506 char label[32];
2508 if (strstart(optarg, "chardev:", &p)) {
2509 snprintf(label, sizeof(label), "%s", p);
2510 } else {
2511 snprintf(label, sizeof(label), "compat_monitor%d",
2512 monitor_device_index);
2513 opts = qemu_chr_parse_compat(label, optarg);
2514 if (!opts) {
2515 error_report("parse error: %s", optarg);
2516 exit(1);
2520 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2521 qemu_opt_set(opts, "mode", mode, &error_abort);
2522 qemu_opt_set(opts, "chardev", label, &error_abort);
2523 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2524 monitor_device_index++;
2527 struct device_config {
2528 enum {
2529 DEV_USB, /* -usbdevice */
2530 DEV_BT, /* -bt */
2531 DEV_SERIAL, /* -serial */
2532 DEV_PARALLEL, /* -parallel */
2533 DEV_VIRTCON, /* -virtioconsole */
2534 DEV_DEBUGCON, /* -debugcon */
2535 DEV_GDB, /* -gdb, -s */
2536 DEV_SCLP, /* s390 sclp */
2537 } type;
2538 const char *cmdline;
2539 Location loc;
2540 QTAILQ_ENTRY(device_config) next;
2543 static QTAILQ_HEAD(, device_config) device_configs =
2544 QTAILQ_HEAD_INITIALIZER(device_configs);
2546 static void add_device_config(int type, const char *cmdline)
2548 struct device_config *conf;
2550 conf = g_malloc0(sizeof(*conf));
2551 conf->type = type;
2552 conf->cmdline = cmdline;
2553 loc_save(&conf->loc);
2554 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2557 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2559 struct device_config *conf;
2560 int rc;
2562 QTAILQ_FOREACH(conf, &device_configs, next) {
2563 if (conf->type != type)
2564 continue;
2565 loc_push_restore(&conf->loc);
2566 rc = func(conf->cmdline);
2567 loc_pop(&conf->loc);
2568 if (rc) {
2569 return rc;
2572 return 0;
2575 static int serial_parse(const char *devname)
2577 static int index = 0;
2578 char label[32];
2580 if (strcmp(devname, "none") == 0)
2581 return 0;
2582 if (index == MAX_SERIAL_PORTS) {
2583 error_report("too many serial ports");
2584 exit(1);
2586 snprintf(label, sizeof(label), "serial%d", index);
2587 serial_hds[index] = qemu_chr_new(label, devname);
2588 if (!serial_hds[index]) {
2589 error_report("could not connect serial device"
2590 " to character backend '%s'", devname);
2591 return -1;
2593 index++;
2594 return 0;
2597 static int parallel_parse(const char *devname)
2599 static int index = 0;
2600 char label[32];
2602 if (strcmp(devname, "none") == 0)
2603 return 0;
2604 if (index == MAX_PARALLEL_PORTS) {
2605 error_report("too many parallel ports");
2606 exit(1);
2608 snprintf(label, sizeof(label), "parallel%d", index);
2609 parallel_hds[index] = qemu_chr_new(label, devname);
2610 if (!parallel_hds[index]) {
2611 error_report("could not connect parallel device"
2612 " to character backend '%s'", devname);
2613 return -1;
2615 index++;
2616 return 0;
2619 static int virtcon_parse(const char *devname)
2621 QemuOptsList *device = qemu_find_opts("device");
2622 static int index = 0;
2623 char label[32];
2624 QemuOpts *bus_opts, *dev_opts;
2626 if (strcmp(devname, "none") == 0)
2627 return 0;
2628 if (index == MAX_VIRTIO_CONSOLES) {
2629 error_report("too many virtio consoles");
2630 exit(1);
2633 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2634 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2636 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2637 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2639 snprintf(label, sizeof(label), "virtcon%d", index);
2640 virtcon_hds[index] = qemu_chr_new(label, devname);
2641 if (!virtcon_hds[index]) {
2642 error_report("could not connect virtio console"
2643 " to character backend '%s'", devname);
2644 return -1;
2646 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2648 index++;
2649 return 0;
2652 static int sclp_parse(const char *devname)
2654 QemuOptsList *device = qemu_find_opts("device");
2655 static int index = 0;
2656 char label[32];
2657 QemuOpts *dev_opts;
2659 if (strcmp(devname, "none") == 0) {
2660 return 0;
2662 if (index == MAX_SCLP_CONSOLES) {
2663 error_report("too many sclp consoles");
2664 exit(1);
2667 assert(arch_type == QEMU_ARCH_S390X);
2669 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2670 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2672 snprintf(label, sizeof(label), "sclpcon%d", index);
2673 sclp_hds[index] = qemu_chr_new(label, devname);
2674 if (!sclp_hds[index]) {
2675 error_report("could not connect sclp console"
2676 " to character backend '%s'", devname);
2677 return -1;
2679 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2681 index++;
2682 return 0;
2685 static int debugcon_parse(const char *devname)
2687 QemuOpts *opts;
2689 if (!qemu_chr_new("debugcon", devname)) {
2690 exit(1);
2692 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2693 if (!opts) {
2694 error_report("already have a debugcon device");
2695 exit(1);
2697 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2698 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2699 return 0;
2702 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2704 const MachineClass *mc1 = a, *mc2 = b;
2705 int res;
2707 if (mc1->family == NULL) {
2708 if (mc2->family == NULL) {
2709 /* Compare standalone machine types against each other; they sort
2710 * in increasing order.
2712 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2713 object_class_get_name(OBJECT_CLASS(mc2)));
2716 /* Standalone machine types sort after families. */
2717 return 1;
2720 if (mc2->family == NULL) {
2721 /* Families sort before standalone machine types. */
2722 return -1;
2725 /* Families sort between each other alphabetically increasingly. */
2726 res = strcmp(mc1->family, mc2->family);
2727 if (res != 0) {
2728 return res;
2731 /* Within the same family, machine types sort in decreasing order. */
2732 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2733 object_class_get_name(OBJECT_CLASS(mc1)));
2736 static MachineClass *machine_parse(const char *name)
2738 MachineClass *mc = NULL;
2739 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2741 if (name) {
2742 mc = find_machine(name);
2744 if (mc) {
2745 g_slist_free(machines);
2746 return mc;
2748 if (name && !is_help_option(name)) {
2749 error_report("unsupported machine type");
2750 error_printf("Use -machine help to list supported machines\n");
2751 } else {
2752 printf("Supported machines are:\n");
2753 machines = g_slist_sort(machines, machine_class_cmp);
2754 for (el = machines; el; el = el->next) {
2755 MachineClass *mc = el->data;
2756 if (mc->alias) {
2757 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2759 printf("%-20s %s%s\n", mc->name, mc->desc,
2760 mc->is_default ? " (default)" : "");
2764 g_slist_free(machines);
2765 exit(!name || !is_help_option(name));
2768 void qemu_add_exit_notifier(Notifier *notify)
2770 notifier_list_add(&exit_notifiers, notify);
2773 void qemu_remove_exit_notifier(Notifier *notify)
2775 notifier_remove(notify);
2778 static void qemu_run_exit_notifiers(void)
2780 notifier_list_notify(&exit_notifiers, NULL);
2783 static bool machine_init_done;
2785 void qemu_add_machine_init_done_notifier(Notifier *notify)
2787 notifier_list_add(&machine_init_done_notifiers, notify);
2788 if (machine_init_done) {
2789 notify->notify(notify, NULL);
2793 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2795 notifier_remove(notify);
2798 static void qemu_run_machine_init_done_notifiers(void)
2800 notifier_list_notify(&machine_init_done_notifiers, NULL);
2801 machine_init_done = true;
2804 static const QEMUOption *lookup_opt(int argc, char **argv,
2805 const char **poptarg, int *poptind)
2807 const QEMUOption *popt;
2808 int optind = *poptind;
2809 char *r = argv[optind];
2810 const char *optarg;
2812 loc_set_cmdline(argv, optind, 1);
2813 optind++;
2814 /* Treat --foo the same as -foo. */
2815 if (r[1] == '-')
2816 r++;
2817 popt = qemu_options;
2818 for(;;) {
2819 if (!popt->name) {
2820 error_report("invalid option");
2821 exit(1);
2823 if (!strcmp(popt->name, r + 1))
2824 break;
2825 popt++;
2827 if (popt->flags & HAS_ARG) {
2828 if (optind >= argc) {
2829 error_report("requires an argument");
2830 exit(1);
2832 optarg = argv[optind++];
2833 loc_set_cmdline(argv, optind - 2, 2);
2834 } else {
2835 optarg = NULL;
2838 *poptarg = optarg;
2839 *poptind = optind;
2841 return popt;
2844 static MachineClass *select_machine(void)
2846 MachineClass *machine_class = find_default_machine();
2847 const char *optarg;
2848 QemuOpts *opts;
2849 Location loc;
2851 loc_push_none(&loc);
2853 opts = qemu_get_machine_opts();
2854 qemu_opts_loc_restore(opts);
2856 optarg = qemu_opt_get(opts, "type");
2857 if (optarg) {
2858 machine_class = machine_parse(optarg);
2861 if (!machine_class) {
2862 error_report("No machine specified, and there is no default");
2863 error_printf("Use -machine help to list supported machines\n");
2864 exit(1);
2867 loc_pop(&loc);
2868 return machine_class;
2871 static int machine_set_property(void *opaque,
2872 const char *name, const char *value,
2873 Error **errp)
2875 Object *obj = OBJECT(opaque);
2876 Error *local_err = NULL;
2877 char *p, *qom_name;
2879 if (strcmp(name, "type") == 0) {
2880 return 0;
2883 qom_name = g_strdup(name);
2884 for (p = qom_name; *p; p++) {
2885 if (*p == '_') {
2886 *p = '-';
2890 object_property_parse(obj, value, qom_name, &local_err);
2891 g_free(qom_name);
2893 if (local_err) {
2894 error_report_err(local_err);
2895 return -1;
2898 return 0;
2903 * Initial object creation happens before all other
2904 * QEMU data types are created. The majority of objects
2905 * can be created at this point. The rng-egd object
2906 * cannot be created here, as it depends on the chardev
2907 * already existing.
2909 static bool object_create_initial(const char *type)
2911 if (g_str_equal(type, "rng-egd") ||
2912 g_str_has_prefix(type, "pr-manager-")) {
2913 return false;
2917 * return false for concrete netfilters since
2918 * they depend on netdevs already existing
2920 if (g_str_equal(type, "filter-buffer") ||
2921 g_str_equal(type, "filter-dump") ||
2922 g_str_equal(type, "filter-mirror") ||
2923 g_str_equal(type, "filter-redirector") ||
2924 g_str_equal(type, "colo-compare") ||
2925 g_str_equal(type, "filter-rewriter") ||
2926 g_str_equal(type, "filter-replay")) {
2927 return false;
2930 /* Memory allocation by backends needs to be done
2931 * after configure_accelerator() (due to the tcg_enabled()
2932 * checks at memory_region_init_*()).
2934 * Also, allocation of large amounts of memory may delay
2935 * chardev initialization for too long, and trigger timeouts
2936 * on software that waits for a monitor socket to be created
2937 * (e.g. libvirt).
2939 if (g_str_has_prefix(type, "memory-backend-")) {
2940 return false;
2943 return true;
2948 * The remainder of object creation happens after the
2949 * creation of chardev, fsdev, net clients and device data types.
2951 static bool object_create_delayed(const char *type)
2953 return !object_create_initial(type);
2957 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2958 MachineClass *mc)
2960 uint64_t sz;
2961 const char *mem_str;
2962 const char *maxmem_str, *slots_str;
2963 const ram_addr_t default_ram_size = mc->default_ram_size;
2964 QemuOpts *opts = qemu_find_opts_singleton("memory");
2965 Location loc;
2967 loc_push_none(&loc);
2968 qemu_opts_loc_restore(opts);
2970 sz = 0;
2971 mem_str = qemu_opt_get(opts, "size");
2972 if (mem_str) {
2973 if (!*mem_str) {
2974 error_report("missing 'size' option value");
2975 exit(EXIT_FAILURE);
2978 sz = qemu_opt_get_size(opts, "size", ram_size);
2980 /* Fix up legacy suffix-less format */
2981 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2982 uint64_t overflow_check = sz;
2984 sz <<= 20;
2985 if ((sz >> 20) != overflow_check) {
2986 error_report("too large 'size' option value");
2987 exit(EXIT_FAILURE);
2992 /* backward compatibility behaviour for case "-m 0" */
2993 if (sz == 0) {
2994 sz = default_ram_size;
2997 sz = QEMU_ALIGN_UP(sz, 8192);
2998 ram_size = sz;
2999 if (ram_size != sz) {
3000 error_report("ram size too large");
3001 exit(EXIT_FAILURE);
3004 /* store value for the future use */
3005 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
3006 *maxram_size = ram_size;
3008 maxmem_str = qemu_opt_get(opts, "maxmem");
3009 slots_str = qemu_opt_get(opts, "slots");
3010 if (maxmem_str && slots_str) {
3011 uint64_t slots;
3013 sz = qemu_opt_get_size(opts, "maxmem", 0);
3014 slots = qemu_opt_get_number(opts, "slots", 0);
3015 if (sz < ram_size) {
3016 error_report("invalid value of -m option maxmem: "
3017 "maximum memory size (0x%" PRIx64 ") must be at least "
3018 "the initial memory size (0x" RAM_ADDR_FMT ")",
3019 sz, ram_size);
3020 exit(EXIT_FAILURE);
3021 } else if (sz > ram_size) {
3022 if (!slots) {
3023 error_report("invalid value of -m option: maxmem was "
3024 "specified, but no hotplug slots were specified");
3025 exit(EXIT_FAILURE);
3027 } else if (slots) {
3028 error_report("invalid value of -m option maxmem: "
3029 "memory slots were specified but maximum memory size "
3030 "(0x%" PRIx64 ") is equal to the initial memory size "
3031 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3032 exit(EXIT_FAILURE);
3035 *maxram_size = sz;
3036 *ram_slots = slots;
3037 } else if ((!maxmem_str && slots_str) ||
3038 (maxmem_str && !slots_str)) {
3039 error_report("invalid -m option value: missing "
3040 "'%s' option", slots_str ? "maxmem" : "slots");
3041 exit(EXIT_FAILURE);
3044 loc_pop(&loc);
3047 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3049 GlobalProperty *g;
3051 g = g_malloc0(sizeof(*g));
3052 g->driver = qemu_opt_get(opts, "driver");
3053 g->property = qemu_opt_get(opts, "property");
3054 g->value = qemu_opt_get(opts, "value");
3055 g->user_provided = true;
3056 g->errp = &error_fatal;
3057 qdev_prop_register_global(g);
3058 return 0;
3061 static int qemu_read_default_config_file(void)
3063 int ret;
3065 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3066 if (ret < 0 && ret != -ENOENT) {
3067 return ret;
3070 return 0;
3073 static void user_register_global_props(void)
3075 qemu_opts_foreach(qemu_find_opts("global"),
3076 global_init_func, NULL, NULL);
3080 * Note: we should see that these properties are actually having a
3081 * priority: accel < machine < user. This means e.g. when user
3082 * specifies something in "-global", it'll always be used with highest
3083 * priority than either machine/accelerator compat properties.
3085 static void register_global_properties(MachineState *ms)
3087 accel_register_compat_props(ms->accelerator);
3088 machine_register_compat_props(ms);
3089 user_register_global_props();
3092 int main(int argc, char **argv)
3094 int i;
3095 int snapshot, linux_boot;
3096 const char *initrd_filename;
3097 const char *kernel_filename, *kernel_cmdline;
3098 const char *boot_order = NULL;
3099 const char *boot_once = NULL;
3100 DisplayState *ds;
3101 int cyls, heads, secs, translation;
3102 QemuOpts *opts, *machine_opts;
3103 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3104 QemuOptsList *olist;
3105 int optind;
3106 const char *optarg;
3107 const char *loadvm = NULL;
3108 MachineClass *machine_class;
3109 const char *cpu_model;
3110 const char *vga_model = NULL;
3111 const char *qtest_chrdev = NULL;
3112 const char *qtest_log = NULL;
3113 const char *pid_file = NULL;
3114 const char *incoming = NULL;
3115 bool userconfig = true;
3116 bool nographic = false;
3117 DisplayType display_type = DT_DEFAULT;
3118 int display_remote = 0;
3119 const char *log_mask = NULL;
3120 const char *log_file = NULL;
3121 char *trace_file = NULL;
3122 ram_addr_t maxram_size;
3123 uint64_t ram_slots = 0;
3124 FILE *vmstate_dump_file = NULL;
3125 Error *main_loop_err = NULL;
3126 Error *err = NULL;
3127 bool list_data_dirs = false;
3128 char **dirs;
3129 typedef struct BlockdevOptions_queue {
3130 BlockdevOptions *bdo;
3131 Location loc;
3132 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3133 } BlockdevOptions_queue;
3134 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3135 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3137 module_call_init(MODULE_INIT_TRACE);
3139 qemu_init_cpu_list();
3140 qemu_init_cpu_loop();
3141 qemu_mutex_lock_iothread();
3143 atexit(qemu_run_exit_notifiers);
3144 error_set_progname(argv[0]);
3145 qemu_init_exec_dir(argv[0]);
3147 module_call_init(MODULE_INIT_QOM);
3148 monitor_init_qmp_commands();
3150 qemu_add_opts(&qemu_drive_opts);
3151 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3152 qemu_add_drive_opts(&qemu_common_drive_opts);
3153 qemu_add_drive_opts(&qemu_drive_opts);
3154 qemu_add_drive_opts(&bdrv_runtime_opts);
3155 qemu_add_opts(&qemu_chardev_opts);
3156 qemu_add_opts(&qemu_device_opts);
3157 qemu_add_opts(&qemu_netdev_opts);
3158 qemu_add_opts(&qemu_net_opts);
3159 qemu_add_opts(&qemu_rtc_opts);
3160 qemu_add_opts(&qemu_global_opts);
3161 qemu_add_opts(&qemu_mon_opts);
3162 qemu_add_opts(&qemu_trace_opts);
3163 qemu_add_opts(&qemu_option_rom_opts);
3164 qemu_add_opts(&qemu_machine_opts);
3165 qemu_add_opts(&qemu_accel_opts);
3166 qemu_add_opts(&qemu_mem_opts);
3167 qemu_add_opts(&qemu_smp_opts);
3168 qemu_add_opts(&qemu_boot_opts);
3169 qemu_add_opts(&qemu_sandbox_opts);
3170 qemu_add_opts(&qemu_add_fd_opts);
3171 qemu_add_opts(&qemu_object_opts);
3172 qemu_add_opts(&qemu_tpmdev_opts);
3173 qemu_add_opts(&qemu_realtime_opts);
3174 qemu_add_opts(&qemu_msg_opts);
3175 qemu_add_opts(&qemu_name_opts);
3176 qemu_add_opts(&qemu_numa_opts);
3177 qemu_add_opts(&qemu_icount_opts);
3178 qemu_add_opts(&qemu_semihosting_config_opts);
3179 qemu_add_opts(&qemu_fw_cfg_opts);
3180 module_call_init(MODULE_INIT_OPTS);
3182 runstate_init();
3184 if (qcrypto_init(&err) < 0) {
3185 error_reportf_err(err, "cannot initialize crypto: ");
3186 exit(1);
3188 rtc_clock = QEMU_CLOCK_HOST;
3190 QLIST_INIT (&vm_change_state_head);
3191 os_setup_early_signal_handling();
3193 cpu_model = NULL;
3194 snapshot = 0;
3195 cyls = heads = secs = 0;
3196 translation = BIOS_ATA_TRANSLATION_AUTO;
3198 nb_nics = 0;
3200 bdrv_init_with_whitelist();
3202 autostart = 1;
3204 /* first pass of option parsing */
3205 optind = 1;
3206 while (optind < argc) {
3207 if (argv[optind][0] != '-') {
3208 /* disk image */
3209 optind++;
3210 } else {
3211 const QEMUOption *popt;
3213 popt = lookup_opt(argc, argv, &optarg, &optind);
3214 switch (popt->index) {
3215 case QEMU_OPTION_nodefconfig:
3216 case QEMU_OPTION_nouserconfig:
3217 userconfig = false;
3218 break;
3223 if (userconfig) {
3224 if (qemu_read_default_config_file() < 0) {
3225 exit(1);
3229 /* second pass of option parsing */
3230 optind = 1;
3231 for(;;) {
3232 if (optind >= argc)
3233 break;
3234 if (argv[optind][0] != '-') {
3235 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3236 } else {
3237 const QEMUOption *popt;
3239 popt = lookup_opt(argc, argv, &optarg, &optind);
3240 if (!(popt->arch_mask & arch_type)) {
3241 error_report("Option not supported for this target");
3242 exit(1);
3244 switch(popt->index) {
3245 case QEMU_OPTION_no_kvm_irqchip: {
3246 olist = qemu_find_opts("machine");
3247 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3248 break;
3250 case QEMU_OPTION_cpu:
3251 /* hw initialization will check this */
3252 cpu_model = optarg;
3253 break;
3254 case QEMU_OPTION_hda:
3256 char buf[256];
3257 if (cyls == 0)
3258 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3259 else
3260 snprintf(buf, sizeof(buf),
3261 "%s,cyls=%d,heads=%d,secs=%d%s",
3262 HD_OPTS , cyls, heads, secs,
3263 translation == BIOS_ATA_TRANSLATION_LBA ?
3264 ",trans=lba" :
3265 translation == BIOS_ATA_TRANSLATION_NONE ?
3266 ",trans=none" : "");
3267 drive_add(IF_DEFAULT, 0, optarg, buf);
3268 break;
3270 case QEMU_OPTION_hdb:
3271 case QEMU_OPTION_hdc:
3272 case QEMU_OPTION_hdd:
3273 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3274 HD_OPTS);
3275 break;
3276 case QEMU_OPTION_blockdev:
3278 Visitor *v;
3279 BlockdevOptions_queue *bdo;
3281 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3282 if (!v) {
3283 error_report_err(err);
3284 exit(1);
3287 bdo = g_new(BlockdevOptions_queue, 1);
3288 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3289 &error_fatal);
3290 visit_free(v);
3291 loc_save(&bdo->loc);
3292 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3293 break;
3295 case QEMU_OPTION_drive:
3296 if (drive_def(optarg) == NULL) {
3297 exit(1);
3299 break;
3300 case QEMU_OPTION_set:
3301 if (qemu_set_option(optarg) != 0)
3302 exit(1);
3303 break;
3304 case QEMU_OPTION_global:
3305 if (qemu_global_option(optarg) != 0)
3306 exit(1);
3307 break;
3308 case QEMU_OPTION_mtdblock:
3309 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3310 break;
3311 case QEMU_OPTION_sd:
3312 drive_add(IF_SD, -1, optarg, SD_OPTS);
3313 break;
3314 case QEMU_OPTION_pflash:
3315 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3316 break;
3317 case QEMU_OPTION_snapshot:
3318 snapshot = 1;
3319 break;
3320 case QEMU_OPTION_hdachs:
3322 const char *p;
3323 p = optarg;
3324 cyls = strtol(p, (char **)&p, 0);
3325 if (cyls < 1 || cyls > 16383)
3326 goto chs_fail;
3327 if (*p != ',')
3328 goto chs_fail;
3329 p++;
3330 heads = strtol(p, (char **)&p, 0);
3331 if (heads < 1 || heads > 16)
3332 goto chs_fail;
3333 if (*p != ',')
3334 goto chs_fail;
3335 p++;
3336 secs = strtol(p, (char **)&p, 0);
3337 if (secs < 1 || secs > 63)
3338 goto chs_fail;
3339 if (*p == ',') {
3340 p++;
3341 if (!strcmp(p, "large")) {
3342 translation = BIOS_ATA_TRANSLATION_LARGE;
3343 } else if (!strcmp(p, "rechs")) {
3344 translation = BIOS_ATA_TRANSLATION_RECHS;
3345 } else if (!strcmp(p, "none")) {
3346 translation = BIOS_ATA_TRANSLATION_NONE;
3347 } else if (!strcmp(p, "lba")) {
3348 translation = BIOS_ATA_TRANSLATION_LBA;
3349 } else if (!strcmp(p, "auto")) {
3350 translation = BIOS_ATA_TRANSLATION_AUTO;
3351 } else {
3352 goto chs_fail;
3354 } else if (*p != '\0') {
3355 chs_fail:
3356 error_report("invalid physical CHS format");
3357 exit(1);
3359 if (hda_opts != NULL) {
3360 qemu_opt_set_number(hda_opts, "cyls", cyls,
3361 &error_abort);
3362 qemu_opt_set_number(hda_opts, "heads", heads,
3363 &error_abort);
3364 qemu_opt_set_number(hda_opts, "secs", secs,
3365 &error_abort);
3366 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3367 qemu_opt_set(hda_opts, "trans", "large",
3368 &error_abort);
3369 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3370 qemu_opt_set(hda_opts, "trans", "rechs",
3371 &error_abort);
3372 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3373 qemu_opt_set(hda_opts, "trans", "lba",
3374 &error_abort);
3375 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3376 qemu_opt_set(hda_opts, "trans", "none",
3377 &error_abort);
3381 error_report("'-hdachs' is deprecated, please use '-device"
3382 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3383 break;
3384 case QEMU_OPTION_numa:
3385 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3386 optarg, true);
3387 if (!opts) {
3388 exit(1);
3390 break;
3391 case QEMU_OPTION_display:
3392 display_type = select_display(optarg);
3393 break;
3394 case QEMU_OPTION_nographic:
3395 olist = qemu_find_opts("machine");
3396 qemu_opts_parse_noisily(olist, "graphics=off", false);
3397 nographic = true;
3398 display_type = DT_NONE;
3399 break;
3400 case QEMU_OPTION_curses:
3401 #ifdef CONFIG_CURSES
3402 display_type = DT_CURSES;
3403 #else
3404 error_report("curses support is disabled");
3405 exit(1);
3406 #endif
3407 break;
3408 case QEMU_OPTION_portrait:
3409 graphic_rotate = 90;
3410 break;
3411 case QEMU_OPTION_rotate:
3412 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3413 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3414 graphic_rotate != 180 && graphic_rotate != 270) {
3415 error_report("only 90, 180, 270 deg rotation is available");
3416 exit(1);
3418 break;
3419 case QEMU_OPTION_kernel:
3420 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3421 &error_abort);
3422 break;
3423 case QEMU_OPTION_initrd:
3424 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3425 &error_abort);
3426 break;
3427 case QEMU_OPTION_append:
3428 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3429 &error_abort);
3430 break;
3431 case QEMU_OPTION_dtb:
3432 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3433 &error_abort);
3434 break;
3435 case QEMU_OPTION_cdrom:
3436 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3437 break;
3438 case QEMU_OPTION_boot:
3439 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3440 optarg, true);
3441 if (!opts) {
3442 exit(1);
3444 break;
3445 case QEMU_OPTION_fda:
3446 case QEMU_OPTION_fdb:
3447 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3448 optarg, FD_OPTS);
3449 break;
3450 case QEMU_OPTION_no_fd_bootchk:
3451 fd_bootchk = 0;
3452 break;
3453 case QEMU_OPTION_netdev:
3454 default_net = 0;
3455 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3456 exit(1);
3458 break;
3459 case QEMU_OPTION_net:
3460 default_net = 0;
3461 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3462 exit(1);
3464 break;
3465 #ifdef CONFIG_LIBISCSI
3466 case QEMU_OPTION_iscsi:
3467 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3468 optarg, false);
3469 if (!opts) {
3470 exit(1);
3472 break;
3473 #endif
3474 #ifdef CONFIG_SLIRP
3475 case QEMU_OPTION_tftp:
3476 error_report("The -tftp option is deprecated. "
3477 "Please use '-netdev user,tftp=...' instead.");
3478 legacy_tftp_prefix = optarg;
3479 break;
3480 case QEMU_OPTION_bootp:
3481 error_report("The -bootp option is deprecated. "
3482 "Please use '-netdev user,bootfile=...' instead.");
3483 legacy_bootp_filename = optarg;
3484 break;
3485 case QEMU_OPTION_redir:
3486 error_report("The -redir option is deprecated. "
3487 "Please use '-netdev user,hostfwd=...' instead.");
3488 if (net_slirp_redir(optarg) < 0)
3489 exit(1);
3490 break;
3491 #endif
3492 case QEMU_OPTION_bt:
3493 add_device_config(DEV_BT, optarg);
3494 break;
3495 case QEMU_OPTION_audio_help:
3496 AUD_help ();
3497 exit (0);
3498 break;
3499 case QEMU_OPTION_soundhw:
3500 select_soundhw (optarg);
3501 break;
3502 case QEMU_OPTION_h:
3503 help(0);
3504 break;
3505 case QEMU_OPTION_version:
3506 version();
3507 exit(0);
3508 break;
3509 case QEMU_OPTION_m:
3510 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3511 optarg, true);
3512 if (!opts) {
3513 exit(EXIT_FAILURE);
3515 break;
3516 #ifdef CONFIG_TPM
3517 case QEMU_OPTION_tpmdev:
3518 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3519 exit(1);
3521 break;
3522 #endif
3523 case QEMU_OPTION_mempath:
3524 mem_path = optarg;
3525 break;
3526 case QEMU_OPTION_mem_prealloc:
3527 mem_prealloc = 1;
3528 break;
3529 case QEMU_OPTION_d:
3530 log_mask = optarg;
3531 break;
3532 case QEMU_OPTION_D:
3533 log_file = optarg;
3534 break;
3535 case QEMU_OPTION_DFILTER:
3536 qemu_set_dfilter_ranges(optarg, &error_fatal);
3537 break;
3538 case QEMU_OPTION_s:
3539 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3540 break;
3541 case QEMU_OPTION_gdb:
3542 add_device_config(DEV_GDB, optarg);
3543 break;
3544 case QEMU_OPTION_L:
3545 if (is_help_option(optarg)) {
3546 list_data_dirs = true;
3547 } else {
3548 qemu_add_data_dir(optarg);
3550 break;
3551 case QEMU_OPTION_bios:
3552 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3553 &error_abort);
3554 break;
3555 case QEMU_OPTION_singlestep:
3556 singlestep = 1;
3557 break;
3558 case QEMU_OPTION_S:
3559 autostart = 0;
3560 break;
3561 case QEMU_OPTION_k:
3562 keyboard_layout = optarg;
3563 break;
3564 case QEMU_OPTION_localtime:
3565 rtc_utc = 0;
3566 break;
3567 case QEMU_OPTION_vga:
3568 vga_model = optarg;
3569 default_vga = 0;
3570 break;
3571 case QEMU_OPTION_g:
3573 const char *p;
3574 int w, h, depth;
3575 p = optarg;
3576 w = strtol(p, (char **)&p, 10);
3577 if (w <= 0) {
3578 graphic_error:
3579 error_report("invalid resolution or depth");
3580 exit(1);
3582 if (*p != 'x')
3583 goto graphic_error;
3584 p++;
3585 h = strtol(p, (char **)&p, 10);
3586 if (h <= 0)
3587 goto graphic_error;
3588 if (*p == 'x') {
3589 p++;
3590 depth = strtol(p, (char **)&p, 10);
3591 if (depth != 8 && depth != 15 && depth != 16 &&
3592 depth != 24 && depth != 32)
3593 goto graphic_error;
3594 } else if (*p == '\0') {
3595 depth = graphic_depth;
3596 } else {
3597 goto graphic_error;
3600 graphic_width = w;
3601 graphic_height = h;
3602 graphic_depth = depth;
3604 break;
3605 case QEMU_OPTION_echr:
3607 char *r;
3608 term_escape_char = strtol(optarg, &r, 0);
3609 if (r == optarg)
3610 printf("Bad argument to echr\n");
3611 break;
3613 case QEMU_OPTION_monitor:
3614 default_monitor = 0;
3615 if (strncmp(optarg, "none", 4)) {
3616 monitor_parse(optarg, "readline", false);
3618 break;
3619 case QEMU_OPTION_qmp:
3620 monitor_parse(optarg, "control", false);
3621 default_monitor = 0;
3622 break;
3623 case QEMU_OPTION_qmp_pretty:
3624 monitor_parse(optarg, "control", true);
3625 default_monitor = 0;
3626 break;
3627 case QEMU_OPTION_mon:
3628 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3629 true);
3630 if (!opts) {
3631 exit(1);
3633 default_monitor = 0;
3634 break;
3635 case QEMU_OPTION_chardev:
3636 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3637 optarg, true);
3638 if (!opts) {
3639 exit(1);
3641 break;
3642 case QEMU_OPTION_fsdev:
3643 olist = qemu_find_opts("fsdev");
3644 if (!olist) {
3645 error_report("fsdev support is disabled");
3646 exit(1);
3648 opts = qemu_opts_parse_noisily(olist, optarg, true);
3649 if (!opts) {
3650 exit(1);
3652 break;
3653 case QEMU_OPTION_virtfs: {
3654 QemuOpts *fsdev;
3655 QemuOpts *device;
3656 const char *writeout, *sock_fd, *socket, *path, *security_model;
3658 olist = qemu_find_opts("virtfs");
3659 if (!olist) {
3660 error_report("virtfs support is disabled");
3661 exit(1);
3663 opts = qemu_opts_parse_noisily(olist, optarg, true);
3664 if (!opts) {
3665 exit(1);
3668 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3669 qemu_opt_get(opts, "mount_tag") == NULL) {
3670 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3671 exit(1);
3673 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3674 qemu_opts_id(opts) ?:
3675 qemu_opt_get(opts, "mount_tag"),
3676 1, NULL);
3677 if (!fsdev) {
3678 error_report("duplicate or invalid fsdev id: %s",
3679 qemu_opt_get(opts, "mount_tag"));
3680 exit(1);
3683 writeout = qemu_opt_get(opts, "writeout");
3684 if (writeout) {
3685 #ifdef CONFIG_SYNC_FILE_RANGE
3686 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3687 #else
3688 error_report("writeout=immediate not supported "
3689 "on this platform");
3690 exit(1);
3691 #endif
3693 qemu_opt_set(fsdev, "fsdriver",
3694 qemu_opt_get(opts, "fsdriver"), &error_abort);
3695 path = qemu_opt_get(opts, "path");
3696 if (path) {
3697 qemu_opt_set(fsdev, "path", path, &error_abort);
3699 security_model = qemu_opt_get(opts, "security_model");
3700 if (security_model) {
3701 qemu_opt_set(fsdev, "security_model", security_model,
3702 &error_abort);
3704 socket = qemu_opt_get(opts, "socket");
3705 if (socket) {
3706 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3708 sock_fd = qemu_opt_get(opts, "sock_fd");
3709 if (sock_fd) {
3710 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3713 qemu_opt_set_bool(fsdev, "readonly",
3714 qemu_opt_get_bool(opts, "readonly", 0),
3715 &error_abort);
3716 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3717 &error_abort);
3718 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3719 qemu_opt_set(device, "fsdev",
3720 qemu_opts_id(fsdev), &error_abort);
3721 qemu_opt_set(device, "mount_tag",
3722 qemu_opt_get(opts, "mount_tag"), &error_abort);
3723 break;
3725 case QEMU_OPTION_virtfs_synth: {
3726 QemuOpts *fsdev;
3727 QemuOpts *device;
3729 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3730 1, NULL);
3731 if (!fsdev) {
3732 error_report("duplicate option: %s", "virtfs_synth");
3733 exit(1);
3735 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3737 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3738 &error_abort);
3739 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3740 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3741 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3742 break;
3744 case QEMU_OPTION_serial:
3745 add_device_config(DEV_SERIAL, optarg);
3746 default_serial = 0;
3747 if (strncmp(optarg, "mon:", 4) == 0) {
3748 default_monitor = 0;
3750 break;
3751 case QEMU_OPTION_watchdog:
3752 if (watchdog) {
3753 error_report("only one watchdog option may be given");
3754 return 1;
3756 watchdog = optarg;
3757 break;
3758 case QEMU_OPTION_watchdog_action:
3759 if (select_watchdog_action(optarg) == -1) {
3760 error_report("unknown -watchdog-action parameter");
3761 exit(1);
3763 break;
3764 case QEMU_OPTION_virtiocon:
3765 add_device_config(DEV_VIRTCON, optarg);
3766 default_virtcon = 0;
3767 if (strncmp(optarg, "mon:", 4) == 0) {
3768 default_monitor = 0;
3770 break;
3771 case QEMU_OPTION_parallel:
3772 add_device_config(DEV_PARALLEL, optarg);
3773 default_parallel = 0;
3774 if (strncmp(optarg, "mon:", 4) == 0) {
3775 default_monitor = 0;
3777 break;
3778 case QEMU_OPTION_debugcon:
3779 add_device_config(DEV_DEBUGCON, optarg);
3780 break;
3781 case QEMU_OPTION_loadvm:
3782 loadvm = optarg;
3783 break;
3784 case QEMU_OPTION_full_screen:
3785 full_screen = 1;
3786 break;
3787 case QEMU_OPTION_no_frame:
3788 no_frame = 1;
3789 break;
3790 case QEMU_OPTION_alt_grab:
3791 alt_grab = 1;
3792 break;
3793 case QEMU_OPTION_ctrl_grab:
3794 ctrl_grab = 1;
3795 break;
3796 case QEMU_OPTION_no_quit:
3797 no_quit = 1;
3798 break;
3799 case QEMU_OPTION_sdl:
3800 #ifdef CONFIG_SDL
3801 display_type = DT_SDL;
3802 break;
3803 #else
3804 error_report("SDL support is disabled");
3805 exit(1);
3806 #endif
3807 case QEMU_OPTION_pidfile:
3808 pid_file = optarg;
3809 break;
3810 case QEMU_OPTION_win2k_hack:
3811 win2k_install_hack = 1;
3812 break;
3813 case QEMU_OPTION_rtc_td_hack: {
3814 static GlobalProperty slew_lost_ticks = {
3815 .driver = "mc146818rtc",
3816 .property = "lost_tick_policy",
3817 .value = "slew",
3820 qdev_prop_register_global(&slew_lost_ticks);
3821 break;
3823 case QEMU_OPTION_acpitable:
3824 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3825 optarg, true);
3826 if (!opts) {
3827 exit(1);
3829 acpi_table_add(opts, &error_fatal);
3830 break;
3831 case QEMU_OPTION_smbios:
3832 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3833 optarg, false);
3834 if (!opts) {
3835 exit(1);
3837 smbios_entry_add(opts, &error_fatal);
3838 break;
3839 case QEMU_OPTION_fwcfg:
3840 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3841 optarg, true);
3842 if (opts == NULL) {
3843 exit(1);
3845 break;
3846 case QEMU_OPTION_enable_kvm:
3847 olist = qemu_find_opts("machine");
3848 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3849 break;
3850 case QEMU_OPTION_enable_hax:
3851 olist = qemu_find_opts("machine");
3852 qemu_opts_parse_noisily(olist, "accel=hax", false);
3853 break;
3854 case QEMU_OPTION_M:
3855 case QEMU_OPTION_machine:
3856 olist = qemu_find_opts("machine");
3857 opts = qemu_opts_parse_noisily(olist, optarg, true);
3858 if (!opts) {
3859 exit(1);
3861 break;
3862 case QEMU_OPTION_no_kvm:
3863 olist = qemu_find_opts("machine");
3864 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3865 break;
3866 case QEMU_OPTION_no_kvm_pit: {
3867 warn_report("ignoring deprecated option");
3868 break;
3870 case QEMU_OPTION_no_kvm_pit_reinjection: {
3871 static GlobalProperty kvm_pit_lost_tick_policy = {
3872 .driver = "kvm-pit",
3873 .property = "lost_tick_policy",
3874 .value = "discard",
3877 warn_report("deprecated, replaced by "
3878 "-global kvm-pit.lost_tick_policy=discard");
3879 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3880 break;
3882 case QEMU_OPTION_accel:
3883 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3884 optarg, true);
3885 optarg = qemu_opt_get(accel_opts, "accel");
3886 if (!optarg || is_help_option(optarg)) {
3887 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3888 exit(0);
3890 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3891 false, &error_abort);
3892 qemu_opt_set(opts, "accel", optarg, &error_abort);
3893 break;
3894 case QEMU_OPTION_usb:
3895 olist = qemu_find_opts("machine");
3896 qemu_opts_parse_noisily(olist, "usb=on", false);
3897 break;
3898 case QEMU_OPTION_usbdevice:
3899 error_report("'-usbdevice' is deprecated, please use "
3900 "'-device usb-...' instead");
3901 olist = qemu_find_opts("machine");
3902 qemu_opts_parse_noisily(olist, "usb=on", false);
3903 add_device_config(DEV_USB, optarg);
3904 break;
3905 case QEMU_OPTION_device:
3906 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3907 optarg, true)) {
3908 exit(1);
3910 break;
3911 case QEMU_OPTION_smp:
3912 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3913 optarg, true)) {
3914 exit(1);
3916 break;
3917 case QEMU_OPTION_vnc:
3918 vnc_parse(optarg, &error_fatal);
3919 break;
3920 case QEMU_OPTION_no_acpi:
3921 acpi_enabled = 0;
3922 break;
3923 case QEMU_OPTION_no_hpet:
3924 no_hpet = 1;
3925 break;
3926 case QEMU_OPTION_balloon:
3927 if (balloon_parse(optarg) < 0) {
3928 error_report("unknown -balloon argument %s", optarg);
3929 exit(1);
3931 break;
3932 case QEMU_OPTION_no_reboot:
3933 no_reboot = 1;
3934 break;
3935 case QEMU_OPTION_no_shutdown:
3936 no_shutdown = 1;
3937 break;
3938 case QEMU_OPTION_show_cursor:
3939 cursor_hide = 0;
3940 break;
3941 case QEMU_OPTION_uuid:
3942 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3943 error_report("failed to parse UUID string: wrong format");
3944 exit(1);
3946 qemu_uuid_set = true;
3947 break;
3948 case QEMU_OPTION_option_rom:
3949 if (nb_option_roms >= MAX_OPTION_ROMS) {
3950 error_report("too many option ROMs");
3951 exit(1);
3953 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3954 optarg, true);
3955 if (!opts) {
3956 exit(1);
3958 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3959 option_rom[nb_option_roms].bootindex =
3960 qemu_opt_get_number(opts, "bootindex", -1);
3961 if (!option_rom[nb_option_roms].name) {
3962 error_report("Option ROM file is not specified");
3963 exit(1);
3965 nb_option_roms++;
3966 break;
3967 case QEMU_OPTION_semihosting:
3968 semihosting.enabled = true;
3969 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3970 break;
3971 case QEMU_OPTION_semihosting_config:
3972 semihosting.enabled = true;
3973 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3974 optarg, false);
3975 if (opts != NULL) {
3976 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3977 true);
3978 const char *target = qemu_opt_get(opts, "target");
3979 if (target != NULL) {
3980 if (strcmp("native", target) == 0) {
3981 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3982 } else if (strcmp("gdb", target) == 0) {
3983 semihosting.target = SEMIHOSTING_TARGET_GDB;
3984 } else if (strcmp("auto", target) == 0) {
3985 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3986 } else {
3987 error_report("unsupported semihosting-config %s",
3988 optarg);
3989 exit(1);
3991 } else {
3992 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3994 /* Set semihosting argument count and vector */
3995 qemu_opt_foreach(opts, add_semihosting_arg,
3996 &semihosting, NULL);
3997 } else {
3998 error_report("unsupported semihosting-config %s", optarg);
3999 exit(1);
4001 break;
4002 case QEMU_OPTION_tdf:
4003 warn_report("ignoring deprecated option");
4004 break;
4005 case QEMU_OPTION_name:
4006 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
4007 optarg, true);
4008 if (!opts) {
4009 exit(1);
4011 break;
4012 case QEMU_OPTION_prom_env:
4013 if (nb_prom_envs >= MAX_PROM_ENVS) {
4014 error_report("too many prom variables");
4015 exit(1);
4017 prom_envs[nb_prom_envs] = optarg;
4018 nb_prom_envs++;
4019 break;
4020 case QEMU_OPTION_old_param:
4021 old_param = 1;
4022 break;
4023 case QEMU_OPTION_clock:
4024 /* Clock options no longer exist. Keep this option for
4025 * backward compatibility.
4027 break;
4028 case QEMU_OPTION_startdate:
4029 configure_rtc_date_offset(optarg, 1);
4030 break;
4031 case QEMU_OPTION_rtc:
4032 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
4033 false);
4034 if (!opts) {
4035 exit(1);
4037 configure_rtc(opts);
4038 break;
4039 case QEMU_OPTION_tb_size:
4040 #ifndef CONFIG_TCG
4041 error_report("TCG is disabled");
4042 exit(1);
4043 #endif
4044 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
4045 error_report("Invalid argument to -tb-size");
4046 exit(1);
4048 break;
4049 case QEMU_OPTION_icount:
4050 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
4051 optarg, true);
4052 if (!icount_opts) {
4053 exit(1);
4055 break;
4056 case QEMU_OPTION_incoming:
4057 if (!incoming) {
4058 runstate_set(RUN_STATE_INMIGRATE);
4060 incoming = optarg;
4061 break;
4062 case QEMU_OPTION_only_migratable:
4064 * TODO: we can remove this option one day, and we
4065 * should all use:
4067 * "-global migration.only-migratable=true"
4069 qemu_global_option("migration.only-migratable=true");
4070 break;
4071 case QEMU_OPTION_nodefaults:
4072 has_defaults = 0;
4073 break;
4074 case QEMU_OPTION_xen_domid:
4075 if (!(xen_available())) {
4076 error_report("Option not supported for this target");
4077 exit(1);
4079 xen_domid = atoi(optarg);
4080 break;
4081 case QEMU_OPTION_xen_create:
4082 if (!(xen_available())) {
4083 error_report("Option not supported for this target");
4084 exit(1);
4086 xen_mode = XEN_CREATE;
4087 break;
4088 case QEMU_OPTION_xen_attach:
4089 if (!(xen_available())) {
4090 error_report("Option not supported for this target");
4091 exit(1);
4093 xen_mode = XEN_ATTACH;
4094 break;
4095 case QEMU_OPTION_xen_domid_restrict:
4096 if (!(xen_available())) {
4097 error_report("Option not supported for this target");
4098 exit(1);
4100 xen_domid_restrict = true;
4101 break;
4102 case QEMU_OPTION_trace:
4103 g_free(trace_file);
4104 trace_file = trace_opt_parse(optarg);
4105 break;
4106 case QEMU_OPTION_trace_unassigned:
4107 trace_unassigned = true;
4108 break;
4109 case QEMU_OPTION_readconfig:
4111 int ret = qemu_read_config_file(optarg);
4112 if (ret < 0) {
4113 error_report("read config %s: %s", optarg,
4114 strerror(-ret));
4115 exit(1);
4117 break;
4119 case QEMU_OPTION_spice:
4120 olist = qemu_find_opts("spice");
4121 if (!olist) {
4122 error_report("spice support is disabled");
4123 exit(1);
4125 opts = qemu_opts_parse_noisily(olist, optarg, false);
4126 if (!opts) {
4127 exit(1);
4129 display_remote++;
4130 break;
4131 case QEMU_OPTION_writeconfig:
4133 FILE *fp;
4134 if (strcmp(optarg, "-") == 0) {
4135 fp = stdout;
4136 } else {
4137 fp = fopen(optarg, "w");
4138 if (fp == NULL) {
4139 error_report("open %s: %s", optarg,
4140 strerror(errno));
4141 exit(1);
4144 qemu_config_write(fp);
4145 if (fp != stdout) {
4146 fclose(fp);
4148 break;
4150 case QEMU_OPTION_qtest:
4151 qtest_chrdev = optarg;
4152 break;
4153 case QEMU_OPTION_qtest_log:
4154 qtest_log = optarg;
4155 break;
4156 case QEMU_OPTION_sandbox:
4157 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4158 optarg, true);
4159 if (!opts) {
4160 exit(1);
4162 break;
4163 case QEMU_OPTION_add_fd:
4164 #ifndef _WIN32
4165 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4166 optarg, false);
4167 if (!opts) {
4168 exit(1);
4170 #else
4171 error_report("File descriptor passing is disabled on this "
4172 "platform");
4173 exit(1);
4174 #endif
4175 break;
4176 case QEMU_OPTION_object:
4177 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4178 optarg, true);
4179 if (!opts) {
4180 exit(1);
4182 break;
4183 case QEMU_OPTION_realtime:
4184 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4185 optarg, false);
4186 if (!opts) {
4187 exit(1);
4189 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4190 break;
4191 case QEMU_OPTION_msg:
4192 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4193 false);
4194 if (!opts) {
4195 exit(1);
4197 configure_msg(opts);
4198 break;
4199 case QEMU_OPTION_dump_vmstate:
4200 if (vmstate_dump_file) {
4201 error_report("only one '-dump-vmstate' "
4202 "option may be given");
4203 exit(1);
4205 vmstate_dump_file = fopen(optarg, "w");
4206 if (vmstate_dump_file == NULL) {
4207 error_report("open %s: %s", optarg, strerror(errno));
4208 exit(1);
4210 break;
4211 default:
4212 os_parse_cmd_args(popt->index, optarg);
4217 * Clear error location left behind by the loop.
4218 * Best done right after the loop. Do not insert code here!
4220 loc_set_none();
4222 replay_configure(icount_opts);
4224 machine_class = select_machine();
4226 set_memory_options(&ram_slots, &maxram_size, machine_class);
4228 os_daemonize();
4229 rcu_disable_atfork();
4231 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4232 error_report("could not acquire pid file: %s", strerror(errno));
4233 exit(1);
4236 if (qemu_init_main_loop(&main_loop_err)) {
4237 error_report_err(main_loop_err);
4238 exit(1);
4241 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4242 parse_sandbox, NULL, NULL)) {
4243 exit(1);
4246 if (qemu_opts_foreach(qemu_find_opts("name"),
4247 parse_name, NULL, NULL)) {
4248 exit(1);
4251 #ifndef _WIN32
4252 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4253 parse_add_fd, NULL, NULL)) {
4254 exit(1);
4257 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4258 cleanup_add_fd, NULL, NULL)) {
4259 exit(1);
4261 #endif
4263 current_machine = MACHINE(object_new(object_class_get_name(
4264 OBJECT_CLASS(machine_class))));
4265 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4266 exit(0);
4268 object_property_add_child(object_get_root(), "machine",
4269 OBJECT(current_machine), &error_abort);
4271 if (machine_class->minimum_page_bits) {
4272 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4273 /* This would be a board error: specifying a minimum smaller than
4274 * a target's compile-time fixed setting.
4276 g_assert_not_reached();
4280 cpu_exec_init_all();
4282 if (machine_class->hw_version) {
4283 qemu_set_hw_version(machine_class->hw_version);
4286 if (cpu_model && is_help_option(cpu_model)) {
4287 list_cpus(stdout, &fprintf, cpu_model);
4288 exit(0);
4291 if (!trace_init_backends()) {
4292 exit(1);
4294 trace_init_file(trace_file);
4296 /* Open the logfile at this point and set the log mask if necessary.
4298 if (log_file) {
4299 qemu_set_log_filename(log_file, &error_fatal);
4302 if (log_mask) {
4303 int mask;
4304 mask = qemu_str_to_log_mask(log_mask);
4305 if (!mask) {
4306 qemu_print_log_usage(stdout);
4307 exit(1);
4309 qemu_set_log(mask);
4310 } else {
4311 qemu_set_log(0);
4314 /* add configured firmware directories */
4315 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4316 for (i = 0; dirs[i] != NULL; i++) {
4317 qemu_add_data_dir(dirs[i]);
4320 /* try to find datadir relative to the executable path */
4321 qemu_add_data_dir(os_find_datadir());
4323 /* add the datadir specified when building */
4324 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4326 /* -L help lists the data directories and exits. */
4327 if (list_data_dirs) {
4328 for (i = 0; i < data_dir_idx; i++) {
4329 printf("%s\n", data_dir[i]);
4331 exit(0);
4334 /* machine_class: default to UP */
4335 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4336 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4337 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4339 /* default to machine_class->default_cpus */
4340 smp_cpus = machine_class->default_cpus;
4341 max_cpus = machine_class->default_cpus;
4343 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4345 /* sanity-check smp_cpus and max_cpus against machine_class */
4346 if (smp_cpus < machine_class->min_cpus) {
4347 error_report("Invalid SMP CPUs %d. The min CPUs "
4348 "supported by machine '%s' is %d", smp_cpus,
4349 machine_class->name, machine_class->min_cpus);
4350 exit(1);
4352 if (max_cpus > machine_class->max_cpus) {
4353 error_report("Invalid SMP CPUs %d. The max CPUs "
4354 "supported by machine '%s' is %d", max_cpus,
4355 machine_class->name, machine_class->max_cpus);
4356 exit(1);
4360 * Get the default machine options from the machine if it is not already
4361 * specified either by the configuration file or by the command line.
4363 if (machine_class->default_machine_opts) {
4364 qemu_opts_set_defaults(qemu_find_opts("machine"),
4365 machine_class->default_machine_opts, 0);
4368 qemu_opts_foreach(qemu_find_opts("device"),
4369 default_driver_check, NULL, NULL);
4370 qemu_opts_foreach(qemu_find_opts("global"),
4371 default_driver_check, NULL, NULL);
4373 if (!vga_model && !default_vga) {
4374 vga_interface_type = VGA_DEVICE;
4376 if (!has_defaults || machine_class->no_serial) {
4377 default_serial = 0;
4379 if (!has_defaults || machine_class->no_parallel) {
4380 default_parallel = 0;
4382 if (!has_defaults || !machine_class->use_virtcon) {
4383 default_virtcon = 0;
4385 if (!has_defaults || !machine_class->use_sclp) {
4386 default_sclp = 0;
4388 if (!has_defaults || machine_class->no_floppy) {
4389 default_floppy = 0;
4391 if (!has_defaults || machine_class->no_cdrom) {
4392 default_cdrom = 0;
4394 if (!has_defaults || machine_class->no_sdcard) {
4395 default_sdcard = 0;
4397 if (!has_defaults) {
4398 default_monitor = 0;
4399 default_net = 0;
4400 default_vga = 0;
4403 if (is_daemonized()) {
4404 /* According to documentation and historically, -nographic redirects
4405 * serial port, parallel port and monitor to stdio, which does not work
4406 * with -daemonize. We can redirect these to null instead, but since
4407 * -nographic is legacy, let's just error out.
4408 * We disallow -nographic only if all other ports are not redirected
4409 * explicitly, to not break existing legacy setups which uses
4410 * -nographic _and_ redirects all ports explicitly - this is valid
4411 * usage, -nographic is just a no-op in this case.
4413 if (nographic
4414 && (default_parallel || default_serial
4415 || default_monitor || default_virtcon)) {
4416 error_report("-nographic cannot be used with -daemonize");
4417 exit(1);
4419 #ifdef CONFIG_CURSES
4420 if (display_type == DT_CURSES) {
4421 error_report("curses display cannot be used with -daemonize");
4422 exit(1);
4424 #endif
4427 if (nographic) {
4428 if (default_parallel)
4429 add_device_config(DEV_PARALLEL, "null");
4430 if (default_serial && default_monitor) {
4431 add_device_config(DEV_SERIAL, "mon:stdio");
4432 } else if (default_virtcon && default_monitor) {
4433 add_device_config(DEV_VIRTCON, "mon:stdio");
4434 } else if (default_sclp && default_monitor) {
4435 add_device_config(DEV_SCLP, "mon:stdio");
4436 } else {
4437 if (default_serial)
4438 add_device_config(DEV_SERIAL, "stdio");
4439 if (default_virtcon)
4440 add_device_config(DEV_VIRTCON, "stdio");
4441 if (default_sclp) {
4442 add_device_config(DEV_SCLP, "stdio");
4444 if (default_monitor)
4445 monitor_parse("stdio", "readline", false);
4447 } else {
4448 if (default_serial)
4449 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4450 if (default_parallel)
4451 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4452 if (default_monitor)
4453 monitor_parse("vc:80Cx24C", "readline", false);
4454 if (default_virtcon)
4455 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4456 if (default_sclp) {
4457 add_device_config(DEV_SCLP, "vc:80Cx24C");
4461 #if defined(CONFIG_VNC)
4462 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4463 display_remote++;
4465 #endif
4466 if (display_type == DT_DEFAULT && !display_remote) {
4467 #if defined(CONFIG_GTK)
4468 display_type = DT_GTK;
4469 #elif defined(CONFIG_SDL)
4470 display_type = DT_SDL;
4471 #elif defined(CONFIG_COCOA)
4472 display_type = DT_COCOA;
4473 #elif defined(CONFIG_VNC)
4474 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4475 #else
4476 display_type = DT_NONE;
4477 #endif
4480 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4481 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4482 "for SDL, ignoring option");
4484 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4485 error_report("-no-quit is only valid for GTK and SDL, "
4486 "ignoring option");
4489 if (display_type == DT_GTK) {
4490 early_gtk_display_init(request_opengl);
4493 if (display_type == DT_SDL) {
4494 sdl_display_early_init(request_opengl);
4497 qemu_console_early_init();
4499 if (request_opengl == 1 && display_opengl == 0) {
4500 #if defined(CONFIG_OPENGL)
4501 error_report("OpenGL is not supported by the display");
4502 #else
4503 error_report("OpenGL support is disabled");
4504 #endif
4505 exit(1);
4508 page_size_init();
4509 socket_init();
4511 if (qemu_opts_foreach(qemu_find_opts("object"),
4512 user_creatable_add_opts_foreach,
4513 object_create_initial, NULL)) {
4514 exit(1);
4517 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4518 chardev_init_func, NULL, NULL)) {
4519 exit(1);
4522 #ifdef CONFIG_VIRTFS
4523 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4524 fsdev_init_func, NULL, NULL)) {
4525 exit(1);
4527 #endif
4529 if (qemu_opts_foreach(qemu_find_opts("device"),
4530 device_help_func, NULL, NULL)) {
4531 exit(0);
4534 machine_opts = qemu_get_machine_opts();
4535 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4536 NULL)) {
4537 object_unref(OBJECT(current_machine));
4538 exit(1);
4541 configure_accelerator(current_machine);
4544 * Register all the global properties, including accel properties,
4545 * machine properties, and user-specified ones.
4547 register_global_properties(current_machine);
4550 * Migration object can only be created after global properties
4551 * are applied correctly.
4553 migration_object_init();
4555 if (qtest_chrdev) {
4556 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4559 machine_opts = qemu_get_machine_opts();
4560 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4561 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4562 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4563 bios_name = qemu_opt_get(machine_opts, "firmware");
4565 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4566 if (opts) {
4567 boot_order = qemu_opt_get(opts, "order");
4568 if (boot_order) {
4569 validate_bootdevices(boot_order, &error_fatal);
4572 boot_once = qemu_opt_get(opts, "once");
4573 if (boot_once) {
4574 validate_bootdevices(boot_once, &error_fatal);
4577 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4578 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4581 if (!boot_order) {
4582 boot_order = machine_class->default_boot_order;
4585 if (!kernel_cmdline) {
4586 kernel_cmdline = "";
4587 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4590 linux_boot = (kernel_filename != NULL);
4592 if (!linux_boot && *kernel_cmdline != '\0') {
4593 error_report("-append only allowed with -kernel option");
4594 exit(1);
4597 if (!linux_boot && initrd_filename != NULL) {
4598 error_report("-initrd only allowed with -kernel option");
4599 exit(1);
4602 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4603 /* fall back to the -kernel/-append */
4604 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4607 os_set_line_buffering();
4609 /* spice needs the timers to be initialized by this point */
4610 qemu_spice_init();
4612 cpu_ticks_init();
4613 if (icount_opts) {
4614 if (!tcg_enabled()) {
4615 error_report("-icount is not allowed with hardware virtualization");
4616 exit(1);
4618 configure_icount(icount_opts, &error_abort);
4619 qemu_opts_del(icount_opts);
4622 if (tcg_enabled()) {
4623 qemu_tcg_configure(accel_opts, &error_fatal);
4626 if (default_net) {
4627 QemuOptsList *net = qemu_find_opts("net");
4628 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4629 #ifdef CONFIG_SLIRP
4630 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4631 #endif
4634 colo_info_init();
4636 if (net_init_clients() < 0) {
4637 exit(1);
4640 if (qemu_opts_foreach(qemu_find_opts("object"),
4641 user_creatable_add_opts_foreach,
4642 object_create_delayed, NULL)) {
4643 exit(1);
4646 if (tpm_init() < 0) {
4647 exit(1);
4650 /* init the bluetooth world */
4651 if (foreach_device_config(DEV_BT, bt_parse))
4652 exit(1);
4654 if (!xen_enabled()) {
4655 /* On 32-bit hosts, QEMU is limited by virtual address space */
4656 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4657 error_report("at most 2047 MB RAM can be simulated");
4658 exit(1);
4662 blk_mig_init();
4663 ram_mig_init();
4665 /* If the currently selected machine wishes to override the units-per-bus
4666 * property of its default HBA interface type, do so now. */
4667 if (machine_class->units_per_default_bus) {
4668 override_max_devs(machine_class->block_default_type,
4669 machine_class->units_per_default_bus);
4672 /* open the virtual block devices */
4673 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4674 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4676 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4677 loc_push_restore(&bdo->loc);
4678 qmp_blockdev_add(bdo->bdo, &error_fatal);
4679 loc_pop(&bdo->loc);
4680 qapi_free_BlockdevOptions(bdo->bdo);
4681 g_free(bdo);
4683 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4684 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4685 NULL, NULL);
4687 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4688 &machine_class->block_default_type, NULL)) {
4689 exit(1);
4692 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4693 CDROM_OPTS);
4694 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4695 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4697 if (qemu_opts_foreach(qemu_find_opts("mon"),
4698 mon_init_func, NULL, NULL)) {
4699 exit(1);
4702 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4703 exit(1);
4704 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4705 exit(1);
4706 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4707 exit(1);
4708 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4709 exit(1);
4711 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4712 exit(1);
4714 /* If no default VGA is requested, the default is "none". */
4715 if (default_vga) {
4716 if (machine_class->default_display) {
4717 vga_model = machine_class->default_display;
4718 } else if (vga_interface_available(VGA_CIRRUS)) {
4719 vga_model = "cirrus";
4720 } else if (vga_interface_available(VGA_STD)) {
4721 vga_model = "std";
4724 if (vga_model) {
4725 select_vgahw(vga_model);
4728 if (watchdog) {
4729 i = select_watchdog(watchdog);
4730 if (i > 0)
4731 exit (i == 1 ? 1 : 0);
4734 /* This checkpoint is required by replay to separate prior clock
4735 reading from the other reads, because timer polling functions query
4736 clock values from the log. */
4737 replay_checkpoint(CHECKPOINT_INIT);
4738 qdev_machine_init();
4740 current_machine->ram_size = ram_size;
4741 current_machine->maxram_size = maxram_size;
4742 current_machine->ram_slots = ram_slots;
4743 current_machine->boot_order = boot_order;
4744 current_machine->cpu_model = cpu_model;
4746 parse_numa_opts(current_machine);
4748 /* parse features once if machine provides default cpu_type */
4749 if (machine_class->default_cpu_type) {
4750 current_machine->cpu_type = machine_class->default_cpu_type;
4751 if (cpu_model) {
4752 current_machine->cpu_type =
4753 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
4757 machine_run_board_init(current_machine);
4759 realtime_init();
4761 soundhw_init();
4763 if (hax_enabled()) {
4764 hax_sync_vcpus();
4767 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4768 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4769 exit(1);
4772 /* init USB devices */
4773 if (machine_usb(current_machine)) {
4774 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4775 exit(1);
4778 /* Check if IGD GFX passthrough. */
4779 igd_gfx_passthru();
4781 /* init generic devices */
4782 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4783 if (qemu_opts_foreach(qemu_find_opts("device"),
4784 device_init_func, NULL, NULL)) {
4785 exit(1);
4788 cpu_synchronize_all_post_init();
4790 rom_reset_order_override();
4793 * Create frontends for -drive if=scsi leftovers.
4794 * Normally, frontends for -drive get created by machine
4795 * initialization for onboard SCSI HBAs. However, we create a few
4796 * more ever since SCSI qdevification, but this is pretty much an
4797 * implementation accident, and deprecated.
4799 scsi_legacy_handle_cmdline();
4801 /* Did we create any drives that we failed to create a device for? */
4802 drive_check_orphaned();
4804 /* Don't warn about the default network setup that you get if
4805 * no command line -net or -netdev options are specified. There
4806 * are two cases that we would otherwise complain about:
4807 * (1) board doesn't support a NIC but the implicit "-net nic"
4808 * requested one
4809 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4810 * sets up a nic that isn't connected to anything.
4812 if (!default_net) {
4813 net_check_clients();
4817 if (boot_once) {
4818 qemu_boot_set(boot_once, &error_fatal);
4819 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4822 ds = init_displaystate();
4824 /* init local displays */
4825 switch (display_type) {
4826 case DT_CURSES:
4827 curses_display_init(ds, full_screen);
4828 break;
4829 case DT_SDL:
4830 sdl_display_init(ds, full_screen, no_frame);
4831 break;
4832 case DT_COCOA:
4833 cocoa_display_init(ds, full_screen);
4834 break;
4835 case DT_GTK:
4836 gtk_display_init(ds, full_screen, grab_on_hover);
4837 break;
4838 default:
4839 break;
4842 /* must be after terminal init, SDL library changes signal handlers */
4843 os_setup_signal_handling();
4845 /* init remote displays */
4846 #ifdef CONFIG_VNC
4847 qemu_opts_foreach(qemu_find_opts("vnc"),
4848 vnc_init_func, NULL, NULL);
4849 #endif
4851 if (using_spice) {
4852 qemu_spice_display_init();
4855 #ifdef CONFIG_OPENGL_DMABUF
4856 if (display_type == DT_EGL) {
4857 egl_headless_init();
4859 #endif
4861 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4862 exit(1);
4865 qdev_machine_creation_done();
4867 /* TODO: once all bus devices are qdevified, this should be done
4868 * when bus is created by qdev.c */
4869 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4870 qemu_run_machine_init_done_notifiers();
4872 if (rom_check_and_register_reset() != 0) {
4873 error_report("rom check and register reset failed");
4874 exit(1);
4877 replay_start();
4879 /* This checkpoint is required by replay to separate prior clock
4880 reading from the other reads, because timer polling functions query
4881 clock values from the log. */
4882 replay_checkpoint(CHECKPOINT_RESET);
4883 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4884 register_global_state();
4885 if (replay_mode != REPLAY_MODE_NONE) {
4886 replay_vmstate_init();
4887 } else if (loadvm) {
4888 Error *local_err = NULL;
4889 if (load_snapshot(loadvm, &local_err) < 0) {
4890 error_report_err(local_err);
4891 autostart = 0;
4895 qdev_prop_check_globals();
4896 if (vmstate_dump_file) {
4897 /* dump and exit */
4898 dump_vmstate_json_to_file(vmstate_dump_file);
4899 return 0;
4902 if (incoming) {
4903 Error *local_err = NULL;
4904 qemu_start_incoming_migration(incoming, &local_err);
4905 if (local_err) {
4906 error_reportf_err(local_err, "-incoming %s: ", incoming);
4907 exit(1);
4909 } else if (autostart) {
4910 vm_start();
4913 os_setup_post();
4915 main_loop();
4916 replay_disable_events();
4917 iothread_stop_all();
4919 pause_all_vcpus();
4920 bdrv_close_all();
4921 res_free();
4923 /* vhost-user must be cleaned up before chardevs. */
4924 tpm_cleanup();
4925 net_cleanup();
4926 audio_cleanup();
4927 monitor_cleanup();
4928 qemu_chr_cleanup();
4929 user_creatable_cleanup();
4930 /* TODO: unref root container, check all devices are ok */
4932 return 0;