hw/block: Use errp directly rather than local_err
[qemu/ar7.git] / vl.c
blobe9012bb0097e8472e6edfd3c88c39bb2f3abf522
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 #include <SDL.h>
38 int qemu_main(int argc, char **argv, char **envp);
39 int main(int argc, char **argv)
41 return qemu_main(argc, argv, NULL);
43 #undef main
44 #define main qemu_main
45 #endif
46 #endif /* CONFIG_SDL */
48 #ifdef CONFIG_COCOA
49 #undef main
50 #define main qemu_main
51 #endif /* CONFIG_COCOA */
54 #include "qemu/error-report.h"
55 #include "qemu/sockets.h"
56 #include "hw/hw.h"
57 #include "hw/boards.h"
58 #include "sysemu/accel.h"
59 #include "hw/usb.h"
60 #include "hw/isa/isa.h"
61 #include "hw/scsi/scsi.h"
62 #include "hw/display/vga.h"
63 #include "hw/bt.h"
64 #include "sysemu/watchdog.h"
65 #include "hw/smbios/smbios.h"
66 #include "hw/acpi/acpi.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "chardev/char.h"
82 #include "qemu/bitmap.h"
83 #include "qemu/log.h"
84 #include "sysemu/blockdev.h"
85 #include "hw/block/block.h"
86 #include "migration/misc.h"
87 #include "migration/snapshot.h"
88 #include "migration/global_state.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "hw/audio/soundhw.h"
92 #include "audio/audio.h"
93 #include "sysemu/cpus.h"
94 #include "migration/colo.h"
95 #include "sysemu/kvm.h"
96 #include "sysemu/hax.h"
97 #include "qapi/qobject-input-visitor.h"
98 #include "qapi-visit.h"
99 #include "qapi/qmp/qjson.h"
100 #include "qemu/option.h"
101 #include "qemu/config-file.h"
102 #include "qemu-options.h"
103 #include "qmp-commands.h"
104 #include "qemu/main-loop.h"
105 #ifdef CONFIG_VIRTFS
106 #include "fsdev/qemu-fsdev.h"
107 #endif
108 #include "sysemu/qtest.h"
110 #include "disas/disas.h"
113 #include "slirp/libslirp.h"
115 #include "trace-root.h"
116 #include "trace/control.h"
117 #include "qemu/queue.h"
118 #include "sysemu/arch_init.h"
120 #include "ui/qemu-spice.h"
121 #include "qapi/string-input-visitor.h"
122 #include "qapi/opts-visitor.h"
123 #include "qom/object_interfaces.h"
124 #include "qapi-event.h"
125 #include "exec/semihost.h"
126 #include "crypto/init.h"
127 #include "sysemu/replay.h"
128 #include "qapi/qmp/qerror.h"
129 #include "sysemu/iothread.h"
131 #define MAX_VIRTIO_CONSOLES 1
132 #define MAX_SCLP_CONSOLES 1
134 static const char *data_dir[16];
135 static int data_dir_idx;
136 const char *bios_name = NULL;
137 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
138 int request_opengl = -1;
139 int display_opengl;
140 const char* keyboard_layout = NULL;
141 ram_addr_t ram_size;
142 const char *mem_path = NULL;
143 int mem_prealloc = 0; /* force preallocation of physical target memory */
144 bool enable_mlock = false;
145 int nb_nics;
146 NICInfo nd_table[MAX_NICS];
147 int autostart;
148 static int rtc_utc = 1;
149 static int rtc_date_offset = -1; /* -1 means no change */
150 QEMUClockType rtc_clock;
151 int vga_interface_type = VGA_NONE;
152 static int full_screen = 0;
153 static int no_frame = 0;
154 int no_quit = 0;
155 static bool grab_on_hover;
156 Chardev *serial_hds[MAX_SERIAL_PORTS];
157 Chardev *parallel_hds[MAX_PARALLEL_PORTS];
158 Chardev *virtcon_hds[MAX_VIRTIO_CONSOLES];
159 Chardev *sclp_hds[MAX_SCLP_CONSOLES];
160 int win2k_install_hack = 0;
161 int singlestep = 0;
162 int smp_cpus;
163 unsigned int max_cpus;
164 int smp_cores = 1;
165 int smp_threads = 1;
166 int acpi_enabled = 1;
167 int no_hpet = 0;
168 int fd_bootchk = 1;
169 static int no_reboot;
170 int no_shutdown = 0;
171 int cursor_hide = 1;
172 int graphic_rotate = 0;
173 const char *watchdog;
174 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
175 int nb_option_roms;
176 int old_param = 0;
177 const char *qemu_name;
178 int alt_grab = 0;
179 int ctrl_grab = 0;
180 unsigned int nb_prom_envs = 0;
181 const char *prom_envs[MAX_PROM_ENVS];
182 int boot_menu;
183 bool boot_strict;
184 uint8_t *boot_splash_filedata;
185 size_t boot_splash_filedata_size;
186 uint8_t qemu_extra_params_fw[2];
188 int icount_align_option;
190 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
191 * little-endian "wire format" described in the SMBIOS 2.6 specification.
193 QemuUUID qemu_uuid;
194 bool qemu_uuid_set;
196 static NotifierList exit_notifiers =
197 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
199 static NotifierList machine_init_done_notifiers =
200 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
202 bool xen_allowed;
203 uint32_t xen_domid;
204 enum xen_mode xen_mode = XEN_EMULATE;
205 bool xen_domid_restrict;
207 static int has_defaults = 1;
208 static int default_serial = 1;
209 static int default_parallel = 1;
210 static int default_virtcon = 1;
211 static int default_sclp = 1;
212 static int default_monitor = 1;
213 static int default_floppy = 1;
214 static int default_cdrom = 1;
215 static int default_sdcard = 1;
216 static int default_vga = 1;
217 static int default_net = 1;
219 static struct {
220 const char *driver;
221 int *flag;
222 } default_list[] = {
223 { .driver = "isa-serial", .flag = &default_serial },
224 { .driver = "isa-parallel", .flag = &default_parallel },
225 { .driver = "isa-fdc", .flag = &default_floppy },
226 { .driver = "floppy", .flag = &default_floppy },
227 { .driver = "ide-cd", .flag = &default_cdrom },
228 { .driver = "ide-hd", .flag = &default_cdrom },
229 { .driver = "ide-drive", .flag = &default_cdrom },
230 { .driver = "scsi-cd", .flag = &default_cdrom },
231 { .driver = "scsi-hd", .flag = &default_cdrom },
232 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
233 { .driver = "virtio-serial", .flag = &default_virtcon },
234 { .driver = "VGA", .flag = &default_vga },
235 { .driver = "isa-vga", .flag = &default_vga },
236 { .driver = "cirrus-vga", .flag = &default_vga },
237 { .driver = "isa-cirrus-vga", .flag = &default_vga },
238 { .driver = "vmware-svga", .flag = &default_vga },
239 { .driver = "qxl-vga", .flag = &default_vga },
240 { .driver = "virtio-vga", .flag = &default_vga },
243 static QemuOptsList qemu_rtc_opts = {
244 .name = "rtc",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
246 .desc = {
248 .name = "base",
249 .type = QEMU_OPT_STRING,
251 .name = "clock",
252 .type = QEMU_OPT_STRING,
254 .name = "driftfix",
255 .type = QEMU_OPT_STRING,
257 { /* end of list */ }
261 static QemuOptsList qemu_sandbox_opts = {
262 .name = "sandbox",
263 .implied_opt_name = "enable",
264 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
265 .desc = {
267 .name = "enable",
268 .type = QEMU_OPT_BOOL,
271 .name = "obsolete",
272 .type = QEMU_OPT_STRING,
275 .name = "elevateprivileges",
276 .type = QEMU_OPT_STRING,
279 .name = "spawn",
280 .type = QEMU_OPT_STRING,
283 .name = "resourcecontrol",
284 .type = QEMU_OPT_STRING,
286 { /* end of list */ }
290 static QemuOptsList qemu_option_rom_opts = {
291 .name = "option-rom",
292 .implied_opt_name = "romfile",
293 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
294 .desc = {
296 .name = "bootindex",
297 .type = QEMU_OPT_NUMBER,
298 }, {
299 .name = "romfile",
300 .type = QEMU_OPT_STRING,
302 { /* end of list */ }
306 static QemuOptsList qemu_machine_opts = {
307 .name = "machine",
308 .implied_opt_name = "type",
309 .merge_lists = true,
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
311 .desc = {
313 * no elements => accept any
314 * sanity checking will happen later
315 * when setting machine properties
321 static QemuOptsList qemu_accel_opts = {
322 .name = "accel",
323 .implied_opt_name = "accel",
324 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
325 .merge_lists = true,
326 .desc = {
328 .name = "accel",
329 .type = QEMU_OPT_STRING,
330 .help = "Select the type of accelerator",
333 .name = "thread",
334 .type = QEMU_OPT_STRING,
335 .help = "Enable/disable multi-threaded TCG",
337 { /* end of list */ }
341 static QemuOptsList qemu_boot_opts = {
342 .name = "boot-opts",
343 .implied_opt_name = "order",
344 .merge_lists = true,
345 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
346 .desc = {
348 .name = "order",
349 .type = QEMU_OPT_STRING,
350 }, {
351 .name = "once",
352 .type = QEMU_OPT_STRING,
353 }, {
354 .name = "menu",
355 .type = QEMU_OPT_BOOL,
356 }, {
357 .name = "splash",
358 .type = QEMU_OPT_STRING,
359 }, {
360 .name = "splash-time",
361 .type = QEMU_OPT_STRING,
362 }, {
363 .name = "reboot-timeout",
364 .type = QEMU_OPT_STRING,
365 }, {
366 .name = "strict",
367 .type = QEMU_OPT_BOOL,
369 { /*End of list */ }
373 static QemuOptsList qemu_add_fd_opts = {
374 .name = "add-fd",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
376 .desc = {
378 .name = "fd",
379 .type = QEMU_OPT_NUMBER,
380 .help = "file descriptor of which a duplicate is added to fd set",
382 .name = "set",
383 .type = QEMU_OPT_NUMBER,
384 .help = "ID of the fd set to add fd to",
386 .name = "opaque",
387 .type = QEMU_OPT_STRING,
388 .help = "free-form string used to describe fd",
390 { /* end of list */ }
394 static QemuOptsList qemu_object_opts = {
395 .name = "object",
396 .implied_opt_name = "qom-type",
397 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
398 .desc = {
403 static QemuOptsList qemu_tpmdev_opts = {
404 .name = "tpmdev",
405 .implied_opt_name = "type",
406 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
407 .desc = {
408 /* options are defined in the TPM backends */
409 { /* end of list */ }
413 static QemuOptsList qemu_realtime_opts = {
414 .name = "realtime",
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
416 .desc = {
418 .name = "mlock",
419 .type = QEMU_OPT_BOOL,
421 { /* end of list */ }
425 static QemuOptsList qemu_msg_opts = {
426 .name = "msg",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
428 .desc = {
430 .name = "timestamp",
431 .type = QEMU_OPT_BOOL,
433 { /* end of list */ }
437 static QemuOptsList qemu_name_opts = {
438 .name = "name",
439 .implied_opt_name = "guest",
440 .merge_lists = true,
441 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
442 .desc = {
444 .name = "guest",
445 .type = QEMU_OPT_STRING,
446 .help = "Sets the name of the guest.\n"
447 "This name will be displayed in the SDL window caption.\n"
448 "The name will also be used for the VNC server",
449 }, {
450 .name = "process",
451 .type = QEMU_OPT_STRING,
452 .help = "Sets the name of the QEMU process, as shown in top etc",
453 }, {
454 .name = "debug-threads",
455 .type = QEMU_OPT_BOOL,
456 .help = "When enabled, name the individual threads; defaults off.\n"
457 "NOTE: The thread names are for debugging and not a\n"
458 "stable API.",
460 { /* End of list */ }
464 static QemuOptsList qemu_mem_opts = {
465 .name = "memory",
466 .implied_opt_name = "size",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
468 .merge_lists = true,
469 .desc = {
471 .name = "size",
472 .type = QEMU_OPT_SIZE,
475 .name = "slots",
476 .type = QEMU_OPT_NUMBER,
479 .name = "maxmem",
480 .type = QEMU_OPT_SIZE,
482 { /* end of list */ }
486 static QemuOptsList qemu_icount_opts = {
487 .name = "icount",
488 .implied_opt_name = "shift",
489 .merge_lists = true,
490 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
491 .desc = {
493 .name = "shift",
494 .type = QEMU_OPT_STRING,
495 }, {
496 .name = "align",
497 .type = QEMU_OPT_BOOL,
498 }, {
499 .name = "sleep",
500 .type = QEMU_OPT_BOOL,
501 }, {
502 .name = "rr",
503 .type = QEMU_OPT_STRING,
504 }, {
505 .name = "rrfile",
506 .type = QEMU_OPT_STRING,
507 }, {
508 .name = "rrsnapshot",
509 .type = QEMU_OPT_STRING,
511 { /* end of list */ }
515 static QemuOptsList qemu_semihosting_config_opts = {
516 .name = "semihosting-config",
517 .implied_opt_name = "enable",
518 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
519 .desc = {
521 .name = "enable",
522 .type = QEMU_OPT_BOOL,
523 }, {
524 .name = "target",
525 .type = QEMU_OPT_STRING,
526 }, {
527 .name = "arg",
528 .type = QEMU_OPT_STRING,
530 { /* end of list */ }
534 static QemuOptsList qemu_fw_cfg_opts = {
535 .name = "fw_cfg",
536 .implied_opt_name = "name",
537 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
538 .desc = {
540 .name = "name",
541 .type = QEMU_OPT_STRING,
542 .help = "Sets the fw_cfg name of the blob to be inserted",
543 }, {
544 .name = "file",
545 .type = QEMU_OPT_STRING,
546 .help = "Sets the name of the file from which\n"
547 "the fw_cfg blob will be loaded",
548 }, {
549 .name = "string",
550 .type = QEMU_OPT_STRING,
551 .help = "Sets content of the blob to be inserted from a string",
553 { /* end of list */ }
558 * Get machine options
560 * Returns: machine options (never null).
562 QemuOpts *qemu_get_machine_opts(void)
564 return qemu_find_opts_singleton("machine");
567 const char *qemu_get_vm_name(void)
569 return qemu_name;
572 static void res_free(void)
574 g_free(boot_splash_filedata);
575 boot_splash_filedata = NULL;
578 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
580 const char *driver = qemu_opt_get(opts, "driver");
581 int i;
583 if (!driver)
584 return 0;
585 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
586 if (strcmp(default_list[i].driver, driver) != 0)
587 continue;
588 *(default_list[i].flag) = 0;
590 return 0;
593 /***********************************************************/
594 /* QEMU state */
596 static RunState current_run_state = RUN_STATE_PRELAUNCH;
598 /* We use RUN_STATE__MAX but any invalid value will do */
599 static RunState vmstop_requested = RUN_STATE__MAX;
600 static QemuMutex vmstop_lock;
602 typedef struct {
603 RunState from;
604 RunState to;
605 } RunStateTransition;
607 static const RunStateTransition runstate_transitions_def[] = {
608 /* from -> to */
609 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
610 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
611 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
613 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
614 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
615 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
616 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
618 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
619 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
620 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
621 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
623 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
624 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
626 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
627 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
631 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
635 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_PAUSED, RUN_STATE_POSTMIGRATE },
637 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
638 { RUN_STATE_PAUSED, RUN_STATE_COLO},
640 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
641 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
644 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
645 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
648 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
649 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PAUSED },
650 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
651 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
652 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
654 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
655 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
657 { RUN_STATE_COLO, RUN_STATE_RUNNING },
659 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
660 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
661 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
662 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
663 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
664 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
665 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
666 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
667 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
668 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
669 { RUN_STATE_RUNNING, RUN_STATE_COLO},
671 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
673 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
674 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
677 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
678 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
679 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
680 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
681 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
682 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
684 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
685 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
686 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
687 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
689 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
690 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
691 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
693 { RUN_STATE__MAX, RUN_STATE__MAX },
696 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
698 bool runstate_check(RunState state)
700 return current_run_state == state;
703 bool runstate_store(char *str, size_t size)
705 const char *state = RunState_str(current_run_state);
706 size_t len = strlen(state) + 1;
708 if (len > size) {
709 return false;
711 memcpy(str, state, len);
712 return true;
715 static void runstate_init(void)
717 const RunStateTransition *p;
719 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
720 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
721 runstate_valid_transitions[p->from][p->to] = true;
724 qemu_mutex_init(&vmstop_lock);
727 /* This function will abort() on invalid state transitions */
728 void runstate_set(RunState new_state)
730 assert(new_state < RUN_STATE__MAX);
732 if (current_run_state == new_state) {
733 return;
736 if (!runstate_valid_transitions[current_run_state][new_state]) {
737 error_report("invalid runstate transition: '%s' -> '%s'",
738 RunState_str(current_run_state),
739 RunState_str(new_state));
740 abort();
742 trace_runstate_set(new_state);
743 current_run_state = new_state;
746 int runstate_is_running(void)
748 return runstate_check(RUN_STATE_RUNNING);
751 bool runstate_needs_reset(void)
753 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
754 runstate_check(RUN_STATE_SHUTDOWN);
757 StatusInfo *qmp_query_status(Error **errp)
759 StatusInfo *info = g_malloc0(sizeof(*info));
761 info->running = runstate_is_running();
762 info->singlestep = singlestep;
763 info->status = current_run_state;
765 return info;
768 bool qemu_vmstop_requested(RunState *r)
770 qemu_mutex_lock(&vmstop_lock);
771 *r = vmstop_requested;
772 vmstop_requested = RUN_STATE__MAX;
773 qemu_mutex_unlock(&vmstop_lock);
774 return *r < RUN_STATE__MAX;
777 void qemu_system_vmstop_request_prepare(void)
779 qemu_mutex_lock(&vmstop_lock);
782 void qemu_system_vmstop_request(RunState state)
784 vmstop_requested = state;
785 qemu_mutex_unlock(&vmstop_lock);
786 qemu_notify_event();
789 /***********************************************************/
790 /* real time host monotonic timer */
792 static time_t qemu_time(void)
794 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
797 /***********************************************************/
798 /* host time/date access */
799 void qemu_get_timedate(struct tm *tm, int offset)
801 time_t ti = qemu_time();
803 ti += offset;
804 if (rtc_date_offset == -1) {
805 if (rtc_utc)
806 gmtime_r(&ti, tm);
807 else
808 localtime_r(&ti, tm);
809 } else {
810 ti -= rtc_date_offset;
811 gmtime_r(&ti, tm);
815 int qemu_timedate_diff(struct tm *tm)
817 time_t seconds;
819 if (rtc_date_offset == -1)
820 if (rtc_utc)
821 seconds = mktimegm(tm);
822 else {
823 struct tm tmp = *tm;
824 tmp.tm_isdst = -1; /* use timezone to figure it out */
825 seconds = mktime(&tmp);
827 else
828 seconds = mktimegm(tm) + rtc_date_offset;
830 return seconds - qemu_time();
833 static void configure_rtc_date_offset(const char *startdate, int legacy)
835 time_t rtc_start_date;
836 struct tm tm;
838 if (!strcmp(startdate, "now") && legacy) {
839 rtc_date_offset = -1;
840 } else {
841 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
842 &tm.tm_year,
843 &tm.tm_mon,
844 &tm.tm_mday,
845 &tm.tm_hour,
846 &tm.tm_min,
847 &tm.tm_sec) == 6) {
848 /* OK */
849 } else if (sscanf(startdate, "%d-%d-%d",
850 &tm.tm_year,
851 &tm.tm_mon,
852 &tm.tm_mday) == 3) {
853 tm.tm_hour = 0;
854 tm.tm_min = 0;
855 tm.tm_sec = 0;
856 } else {
857 goto date_fail;
859 tm.tm_year -= 1900;
860 tm.tm_mon--;
861 rtc_start_date = mktimegm(&tm);
862 if (rtc_start_date == -1) {
863 date_fail:
864 error_report("invalid date format");
865 error_printf("valid formats: "
866 "'2006-06-17T16:01:21' or '2006-06-17'\n");
867 exit(1);
869 rtc_date_offset = qemu_time() - rtc_start_date;
873 static void configure_rtc(QemuOpts *opts)
875 const char *value;
877 value = qemu_opt_get(opts, "base");
878 if (value) {
879 if (!strcmp(value, "utc")) {
880 rtc_utc = 1;
881 } else if (!strcmp(value, "localtime")) {
882 Error *blocker = NULL;
883 rtc_utc = 0;
884 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
885 "-rtc base=localtime");
886 replay_add_blocker(blocker);
887 } else {
888 configure_rtc_date_offset(value, 0);
891 value = qemu_opt_get(opts, "clock");
892 if (value) {
893 if (!strcmp(value, "host")) {
894 rtc_clock = QEMU_CLOCK_HOST;
895 } else if (!strcmp(value, "rt")) {
896 rtc_clock = QEMU_CLOCK_REALTIME;
897 } else if (!strcmp(value, "vm")) {
898 rtc_clock = QEMU_CLOCK_VIRTUAL;
899 } else {
900 error_report("invalid option value '%s'", value);
901 exit(1);
904 value = qemu_opt_get(opts, "driftfix");
905 if (value) {
906 if (!strcmp(value, "slew")) {
907 static GlobalProperty slew_lost_ticks = {
908 .driver = "mc146818rtc",
909 .property = "lost_tick_policy",
910 .value = "slew",
913 qdev_prop_register_global(&slew_lost_ticks);
914 } else if (!strcmp(value, "none")) {
915 /* discard is default */
916 } else {
917 error_report("invalid option value '%s'", value);
918 exit(1);
923 /***********************************************************/
924 /* Bluetooth support */
925 static int nb_hcis;
926 static int cur_hci;
927 static struct HCIInfo *hci_table[MAX_NICS];
929 struct HCIInfo *qemu_next_hci(void)
931 if (cur_hci == nb_hcis)
932 return &null_hci;
934 return hci_table[cur_hci++];
937 static int bt_hci_parse(const char *str)
939 struct HCIInfo *hci;
940 bdaddr_t bdaddr;
942 if (nb_hcis >= MAX_NICS) {
943 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
944 return -1;
947 hci = hci_init(str);
948 if (!hci)
949 return -1;
951 bdaddr.b[0] = 0x52;
952 bdaddr.b[1] = 0x54;
953 bdaddr.b[2] = 0x00;
954 bdaddr.b[3] = 0x12;
955 bdaddr.b[4] = 0x34;
956 bdaddr.b[5] = 0x56 + nb_hcis;
957 hci->bdaddr_set(hci, bdaddr.b);
959 hci_table[nb_hcis++] = hci;
961 return 0;
964 static void bt_vhci_add(int vlan_id)
966 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
968 if (!vlan->slave)
969 warn_report("adding a VHCI to an empty scatternet %i",
970 vlan_id);
972 bt_vhci_init(bt_new_hci(vlan));
975 static struct bt_device_s *bt_device_add(const char *opt)
977 struct bt_scatternet_s *vlan;
978 int vlan_id = 0;
979 char *endp = strstr(opt, ",vlan=");
980 int len = (endp ? endp - opt : strlen(opt)) + 1;
981 char devname[10];
983 pstrcpy(devname, MIN(sizeof(devname), len), opt);
985 if (endp) {
986 vlan_id = strtol(endp + 6, &endp, 0);
987 if (*endp) {
988 error_report("unrecognised bluetooth vlan Id");
989 return 0;
993 vlan = qemu_find_bt_vlan(vlan_id);
995 if (!vlan->slave)
996 warn_report("adding a slave device to an empty scatternet %i",
997 vlan_id);
999 if (!strcmp(devname, "keyboard"))
1000 return bt_keyboard_init(vlan);
1002 error_report("unsupported bluetooth device '%s'", devname);
1003 return 0;
1006 static int bt_parse(const char *opt)
1008 const char *endp, *p;
1009 int vlan;
1011 if (strstart(opt, "hci", &endp)) {
1012 if (!*endp || *endp == ',') {
1013 if (*endp)
1014 if (!strstart(endp, ",vlan=", 0))
1015 opt = endp + 1;
1017 return bt_hci_parse(opt);
1019 } else if (strstart(opt, "vhci", &endp)) {
1020 if (!*endp || *endp == ',') {
1021 if (*endp) {
1022 if (strstart(endp, ",vlan=", &p)) {
1023 vlan = strtol(p, (char **) &endp, 0);
1024 if (*endp) {
1025 error_report("bad scatternet '%s'", p);
1026 return 1;
1028 } else {
1029 error_report("bad parameter '%s'", endp + 1);
1030 return 1;
1032 } else
1033 vlan = 0;
1035 bt_vhci_add(vlan);
1036 return 0;
1038 } else if (strstart(opt, "device:", &endp))
1039 return !bt_device_add(endp);
1041 error_report("bad bluetooth parameter '%s'", opt);
1042 return 1;
1045 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1047 if (qemu_opt_get_bool(opts, "enable", false)) {
1048 #ifdef CONFIG_SECCOMP
1049 uint32_t seccomp_opts = QEMU_SECCOMP_SET_DEFAULT
1050 | QEMU_SECCOMP_SET_OBSOLETE;
1051 const char *value = NULL;
1053 value = qemu_opt_get(opts, "obsolete");
1054 if (value) {
1055 if (g_str_equal(value, "allow")) {
1056 seccomp_opts &= ~QEMU_SECCOMP_SET_OBSOLETE;
1057 } else if (g_str_equal(value, "deny")) {
1058 /* this is the default option, this if is here
1059 * to provide a little bit of consistency for
1060 * the command line */
1061 } else {
1062 error_report("invalid argument for obsolete");
1063 return -1;
1067 value = qemu_opt_get(opts, "elevateprivileges");
1068 if (value) {
1069 if (g_str_equal(value, "deny")) {
1070 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1071 } else if (g_str_equal(value, "children")) {
1072 seccomp_opts |= QEMU_SECCOMP_SET_PRIVILEGED;
1074 /* calling prctl directly because we're
1075 * not sure if host has CAP_SYS_ADMIN set*/
1076 if (prctl(PR_SET_NO_NEW_PRIVS, 1)) {
1077 error_report("failed to set no_new_privs "
1078 "aborting");
1079 return -1;
1081 } else if (g_str_equal(value, "allow")) {
1082 /* default value */
1083 } else {
1084 error_report("invalid argument for elevateprivileges");
1085 return -1;
1089 value = qemu_opt_get(opts, "spawn");
1090 if (value) {
1091 if (g_str_equal(value, "deny")) {
1092 seccomp_opts |= QEMU_SECCOMP_SET_SPAWN;
1093 } else if (g_str_equal(value, "allow")) {
1094 /* default value */
1095 } else {
1096 error_report("invalid argument for spawn");
1097 return -1;
1101 value = qemu_opt_get(opts, "resourcecontrol");
1102 if (value) {
1103 if (g_str_equal(value, "deny")) {
1104 seccomp_opts |= QEMU_SECCOMP_SET_RESOURCECTL;
1105 } else if (g_str_equal(value, "allow")) {
1106 /* default value */
1107 } else {
1108 error_report("invalid argument for resourcecontrol");
1109 return -1;
1113 if (seccomp_start(seccomp_opts) < 0) {
1114 error_report("failed to install seccomp syscall filter "
1115 "in the kernel");
1116 return -1;
1118 #else
1119 error_report("seccomp support is disabled");
1120 return -1;
1121 #endif
1124 return 0;
1127 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1129 const char *proc_name;
1131 if (qemu_opt_get(opts, "debug-threads")) {
1132 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1134 qemu_name = qemu_opt_get(opts, "guest");
1136 proc_name = qemu_opt_get(opts, "process");
1137 if (proc_name) {
1138 os_set_proc_name(proc_name);
1141 return 0;
1144 bool defaults_enabled(void)
1146 return has_defaults;
1149 #ifndef _WIN32
1150 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1152 int fd, dupfd, flags;
1153 int64_t fdset_id;
1154 const char *fd_opaque = NULL;
1155 AddfdInfo *fdinfo;
1157 fd = qemu_opt_get_number(opts, "fd", -1);
1158 fdset_id = qemu_opt_get_number(opts, "set", -1);
1159 fd_opaque = qemu_opt_get(opts, "opaque");
1161 if (fd < 0) {
1162 error_report("fd option is required and must be non-negative");
1163 return -1;
1166 if (fd <= STDERR_FILENO) {
1167 error_report("fd cannot be a standard I/O stream");
1168 return -1;
1172 * All fds inherited across exec() necessarily have FD_CLOEXEC
1173 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1175 flags = fcntl(fd, F_GETFD);
1176 if (flags == -1 || (flags & FD_CLOEXEC)) {
1177 error_report("fd is not valid or already in use");
1178 return -1;
1181 if (fdset_id < 0) {
1182 error_report("set option is required and must be non-negative");
1183 return -1;
1186 #ifdef F_DUPFD_CLOEXEC
1187 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1188 #else
1189 dupfd = dup(fd);
1190 if (dupfd != -1) {
1191 qemu_set_cloexec(dupfd);
1193 #endif
1194 if (dupfd == -1) {
1195 error_report("error duplicating fd: %s", strerror(errno));
1196 return -1;
1199 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1200 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1201 &error_abort);
1202 g_free(fdinfo);
1204 return 0;
1207 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1209 int fd;
1211 fd = qemu_opt_get_number(opts, "fd", -1);
1212 close(fd);
1214 return 0;
1216 #endif
1218 /***********************************************************/
1219 /* QEMU Block devices */
1221 #define HD_OPTS "media=disk"
1222 #define CDROM_OPTS "media=cdrom"
1223 #define FD_OPTS ""
1224 #define PFLASH_OPTS ""
1225 #define MTD_OPTS ""
1226 #define SD_OPTS ""
1228 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1230 BlockInterfaceType *block_default_type = opaque;
1232 return drive_new(opts, *block_default_type) == NULL;
1235 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1237 if (qemu_opt_get(opts, "snapshot") == NULL) {
1238 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1240 return 0;
1243 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1244 int index, const char *optstr)
1246 QemuOpts *opts;
1247 DriveInfo *dinfo;
1249 if (!enable || drive_get_by_index(type, index)) {
1250 return;
1253 opts = drive_add(type, index, NULL, optstr);
1254 if (snapshot) {
1255 drive_enable_snapshot(NULL, opts, NULL);
1258 dinfo = drive_new(opts, type);
1259 if (!dinfo) {
1260 exit(1);
1262 dinfo->is_default = true;
1266 static QemuOptsList qemu_smp_opts = {
1267 .name = "smp-opts",
1268 .implied_opt_name = "cpus",
1269 .merge_lists = true,
1270 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1271 .desc = {
1273 .name = "cpus",
1274 .type = QEMU_OPT_NUMBER,
1275 }, {
1276 .name = "sockets",
1277 .type = QEMU_OPT_NUMBER,
1278 }, {
1279 .name = "cores",
1280 .type = QEMU_OPT_NUMBER,
1281 }, {
1282 .name = "threads",
1283 .type = QEMU_OPT_NUMBER,
1284 }, {
1285 .name = "maxcpus",
1286 .type = QEMU_OPT_NUMBER,
1288 { /*End of list */ }
1292 static void smp_parse(QemuOpts *opts)
1294 if (opts) {
1295 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1296 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1297 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1298 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1300 /* compute missing values, prefer sockets over cores over threads */
1301 if (cpus == 0 || sockets == 0) {
1302 sockets = sockets > 0 ? sockets : 1;
1303 cores = cores > 0 ? cores : 1;
1304 threads = threads > 0 ? threads : 1;
1305 if (cpus == 0) {
1306 cpus = cores * threads * sockets;
1308 } else if (cores == 0) {
1309 threads = threads > 0 ? threads : 1;
1310 cores = cpus / (sockets * threads);
1311 cores = cores > 0 ? cores : 1;
1312 } else if (threads == 0) {
1313 threads = cpus / (cores * sockets);
1314 threads = threads > 0 ? threads : 1;
1315 } else if (sockets * cores * threads < cpus) {
1316 error_report("cpu topology: "
1317 "sockets (%u) * cores (%u) * threads (%u) < "
1318 "smp_cpus (%u)",
1319 sockets, cores, threads, cpus);
1320 exit(1);
1323 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1325 if (max_cpus < cpus) {
1326 error_report("maxcpus must be equal to or greater than smp");
1327 exit(1);
1330 if (sockets * cores * threads > max_cpus) {
1331 error_report("cpu topology: "
1332 "sockets (%u) * cores (%u) * threads (%u) > "
1333 "maxcpus (%u)",
1334 sockets, cores, threads, max_cpus);
1335 exit(1);
1338 smp_cpus = cpus;
1339 smp_cores = cores;
1340 smp_threads = threads;
1343 if (smp_cpus > 1) {
1344 Error *blocker = NULL;
1345 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1346 replay_add_blocker(blocker);
1350 static void realtime_init(void)
1352 if (enable_mlock) {
1353 if (os_mlock() < 0) {
1354 error_report("locking memory failed");
1355 exit(1);
1361 static void configure_msg(QemuOpts *opts)
1363 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1366 /***********************************************************/
1367 /* Semihosting */
1369 typedef struct SemihostingConfig {
1370 bool enabled;
1371 SemihostingTarget target;
1372 const char **argv;
1373 int argc;
1374 const char *cmdline; /* concatenated argv */
1375 } SemihostingConfig;
1377 static SemihostingConfig semihosting;
1379 bool semihosting_enabled(void)
1381 return semihosting.enabled;
1384 SemihostingTarget semihosting_get_target(void)
1386 return semihosting.target;
1389 const char *semihosting_get_arg(int i)
1391 if (i >= semihosting.argc) {
1392 return NULL;
1394 return semihosting.argv[i];
1397 int semihosting_get_argc(void)
1399 return semihosting.argc;
1402 const char *semihosting_get_cmdline(void)
1404 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1405 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1407 return semihosting.cmdline;
1410 static int add_semihosting_arg(void *opaque,
1411 const char *name, const char *val,
1412 Error **errp)
1414 SemihostingConfig *s = opaque;
1415 if (strcmp(name, "arg") == 0) {
1416 s->argc++;
1417 /* one extra element as g_strjoinv() expects NULL-terminated array */
1418 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1419 s->argv[s->argc - 1] = val;
1420 s->argv[s->argc] = NULL;
1422 return 0;
1425 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1426 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1428 char *cmd_token;
1430 /* argv[0] */
1431 add_semihosting_arg(&semihosting, "arg", file, NULL);
1433 /* split -append and initialize argv[1..n] */
1434 cmd_token = strtok(g_strdup(cmd), " ");
1435 while (cmd_token) {
1436 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1437 cmd_token = strtok(NULL, " ");
1441 /* Now we still need this for compatibility with XEN. */
1442 bool has_igd_gfx_passthru;
1443 static void igd_gfx_passthru(void)
1445 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1448 /***********************************************************/
1449 /* USB devices */
1451 static int usb_device_add(const char *devname)
1453 USBDevice *dev = NULL;
1454 #ifndef CONFIG_LINUX
1455 const char *p;
1456 #endif
1458 if (!machine_usb(current_machine)) {
1459 return -1;
1462 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1463 dev = usbdevice_create(devname);
1464 if (dev)
1465 goto done;
1467 /* the other ones */
1468 #ifndef CONFIG_LINUX
1469 /* only the linux version is qdev-ified, usb-bsd still needs this */
1470 if (strstart(devname, "host:", &p)) {
1471 dev = usb_host_device_open(usb_bus_find(-1), p);
1473 #endif
1474 if (!dev)
1475 return -1;
1477 done:
1478 return 0;
1481 static int usb_parse(const char *cmdline)
1483 int r;
1484 r = usb_device_add(cmdline);
1485 if (r < 0) {
1486 error_report("could not add USB device '%s'", cmdline);
1488 return r;
1491 /***********************************************************/
1492 /* machine registration */
1494 MachineState *current_machine;
1496 static MachineClass *find_machine(const char *name)
1498 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1499 MachineClass *mc = NULL;
1501 for (el = machines; el; el = el->next) {
1502 MachineClass *temp = el->data;
1504 if (!strcmp(temp->name, name)) {
1505 mc = temp;
1506 break;
1508 if (temp->alias &&
1509 !strcmp(temp->alias, name)) {
1510 mc = temp;
1511 break;
1515 g_slist_free(machines);
1516 return mc;
1519 MachineClass *find_default_machine(void)
1521 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1522 MachineClass *mc = NULL;
1524 for (el = machines; el; el = el->next) {
1525 MachineClass *temp = el->data;
1527 if (temp->is_default) {
1528 mc = temp;
1529 break;
1533 g_slist_free(machines);
1534 return mc;
1537 MachineInfoList *qmp_query_machines(Error **errp)
1539 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1540 MachineInfoList *mach_list = NULL;
1542 for (el = machines; el; el = el->next) {
1543 MachineClass *mc = el->data;
1544 MachineInfoList *entry;
1545 MachineInfo *info;
1547 info = g_malloc0(sizeof(*info));
1548 if (mc->is_default) {
1549 info->has_is_default = true;
1550 info->is_default = true;
1553 if (mc->alias) {
1554 info->has_alias = true;
1555 info->alias = g_strdup(mc->alias);
1558 info->name = g_strdup(mc->name);
1559 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1560 info->hotpluggable_cpus = mc->has_hotpluggable_cpus;
1562 entry = g_malloc0(sizeof(*entry));
1563 entry->value = info;
1564 entry->next = mach_list;
1565 mach_list = entry;
1568 g_slist_free(machines);
1569 return mach_list;
1572 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1574 ObjectProperty *prop;
1575 ObjectPropertyIterator iter;
1577 if (!qemu_opt_has_help_opt(opts)) {
1578 return 0;
1581 object_property_iter_init(&iter, OBJECT(machine));
1582 while ((prop = object_property_iter_next(&iter))) {
1583 if (!prop->set) {
1584 continue;
1587 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1588 prop->name, prop->type);
1589 if (prop->description) {
1590 error_printf(" (%s)\n", prop->description);
1591 } else {
1592 error_printf("\n");
1596 return 1;
1599 /***********************************************************/
1600 /* main execution loop */
1602 struct vm_change_state_entry {
1603 VMChangeStateHandler *cb;
1604 void *opaque;
1605 QLIST_ENTRY (vm_change_state_entry) entries;
1608 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1610 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1611 void *opaque)
1613 VMChangeStateEntry *e;
1615 e = g_malloc0(sizeof (*e));
1617 e->cb = cb;
1618 e->opaque = opaque;
1619 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1620 return e;
1623 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1625 QLIST_REMOVE (e, entries);
1626 g_free (e);
1629 void vm_state_notify(int running, RunState state)
1631 VMChangeStateEntry *e, *next;
1633 trace_vm_state_notify(running, state);
1635 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1636 e->cb(e->opaque, running, state);
1640 static ShutdownCause reset_requested;
1641 static ShutdownCause shutdown_requested;
1642 static int shutdown_signal;
1643 static pid_t shutdown_pid;
1644 static int powerdown_requested;
1645 static int debug_requested;
1646 static int suspend_requested;
1647 static WakeupReason wakeup_reason;
1648 static NotifierList powerdown_notifiers =
1649 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1650 static NotifierList suspend_notifiers =
1651 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1652 static NotifierList wakeup_notifiers =
1653 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1654 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1656 ShutdownCause qemu_shutdown_requested_get(void)
1658 return shutdown_requested;
1661 ShutdownCause qemu_reset_requested_get(void)
1663 return reset_requested;
1666 static int qemu_shutdown_requested(void)
1668 return atomic_xchg(&shutdown_requested, SHUTDOWN_CAUSE_NONE);
1671 static void qemu_kill_report(void)
1673 if (!qtest_driver() && shutdown_signal) {
1674 if (shutdown_pid == 0) {
1675 /* This happens for eg ^C at the terminal, so it's worth
1676 * avoiding printing an odd message in that case.
1678 error_report("terminating on signal %d", shutdown_signal);
1679 } else {
1680 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1682 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1683 shutdown_signal, shutdown_pid,
1684 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1685 g_free(shutdown_cmd);
1687 shutdown_signal = 0;
1691 static ShutdownCause qemu_reset_requested(void)
1693 ShutdownCause r = reset_requested;
1695 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1696 reset_requested = SHUTDOWN_CAUSE_NONE;
1697 return r;
1699 return SHUTDOWN_CAUSE_NONE;
1702 static int qemu_suspend_requested(void)
1704 int r = suspend_requested;
1705 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1706 suspend_requested = 0;
1707 return r;
1709 return false;
1712 static WakeupReason qemu_wakeup_requested(void)
1714 return wakeup_reason;
1717 static int qemu_powerdown_requested(void)
1719 int r = powerdown_requested;
1720 powerdown_requested = 0;
1721 return r;
1724 static int qemu_debug_requested(void)
1726 int r = debug_requested;
1727 debug_requested = 0;
1728 return r;
1732 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1734 void qemu_system_reset(ShutdownCause reason)
1736 MachineClass *mc;
1738 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1740 cpu_synchronize_all_states();
1742 if (mc && mc->reset) {
1743 mc->reset();
1744 } else {
1745 qemu_devices_reset();
1747 if (reason) {
1748 qapi_event_send_reset(shutdown_caused_by_guest(reason),
1749 &error_abort);
1751 cpu_synchronize_all_post_reset();
1754 void qemu_system_guest_panicked(GuestPanicInformation *info)
1756 qemu_log_mask(LOG_GUEST_ERROR, "Guest crashed\n");
1758 if (current_cpu) {
1759 current_cpu->crash_occurred = true;
1761 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE,
1762 !!info, info, &error_abort);
1763 vm_stop(RUN_STATE_GUEST_PANICKED);
1764 if (!no_shutdown) {
1765 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1766 !!info, info, &error_abort);
1767 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
1770 if (info) {
1771 if (info->type == GUEST_PANIC_INFORMATION_TYPE_HYPER_V) {
1772 qemu_log_mask(LOG_GUEST_ERROR, "HV crash parameters: (%#"PRIx64
1773 " %#"PRIx64" %#"PRIx64" %#"PRIx64" %#"PRIx64")\n",
1774 info->u.hyper_v.arg1,
1775 info->u.hyper_v.arg2,
1776 info->u.hyper_v.arg3,
1777 info->u.hyper_v.arg4,
1778 info->u.hyper_v.arg5);
1780 qapi_free_GuestPanicInformation(info);
1784 void qemu_system_reset_request(ShutdownCause reason)
1786 if (no_reboot) {
1787 shutdown_requested = reason;
1788 } else {
1789 reset_requested = reason;
1791 cpu_stop_current();
1792 qemu_notify_event();
1795 static void qemu_system_suspend(void)
1797 pause_all_vcpus();
1798 notifier_list_notify(&suspend_notifiers, NULL);
1799 runstate_set(RUN_STATE_SUSPENDED);
1800 qapi_event_send_suspend(&error_abort);
1803 void qemu_system_suspend_request(void)
1805 if (runstate_check(RUN_STATE_SUSPENDED)) {
1806 return;
1808 suspend_requested = 1;
1809 cpu_stop_current();
1810 qemu_notify_event();
1813 void qemu_register_suspend_notifier(Notifier *notifier)
1815 notifier_list_add(&suspend_notifiers, notifier);
1818 void qemu_system_wakeup_request(WakeupReason reason)
1820 trace_system_wakeup_request(reason);
1822 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1823 return;
1825 if (!(wakeup_reason_mask & (1 << reason))) {
1826 return;
1828 runstate_set(RUN_STATE_RUNNING);
1829 wakeup_reason = reason;
1830 qemu_notify_event();
1833 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1835 if (enabled) {
1836 wakeup_reason_mask |= (1 << reason);
1837 } else {
1838 wakeup_reason_mask &= ~(1 << reason);
1842 void qemu_register_wakeup_notifier(Notifier *notifier)
1844 notifier_list_add(&wakeup_notifiers, notifier);
1847 void qemu_system_killed(int signal, pid_t pid)
1849 shutdown_signal = signal;
1850 shutdown_pid = pid;
1851 no_shutdown = 0;
1853 /* Cannot call qemu_system_shutdown_request directly because
1854 * we are in a signal handler.
1856 shutdown_requested = SHUTDOWN_CAUSE_HOST_SIGNAL;
1857 qemu_notify_event();
1860 void qemu_system_shutdown_request(ShutdownCause reason)
1862 trace_qemu_system_shutdown_request(reason);
1863 replay_shutdown_request(reason);
1864 shutdown_requested = reason;
1865 qemu_notify_event();
1868 static void qemu_system_powerdown(void)
1870 qapi_event_send_powerdown(&error_abort);
1871 notifier_list_notify(&powerdown_notifiers, NULL);
1874 void qemu_system_powerdown_request(void)
1876 trace_qemu_system_powerdown_request();
1877 powerdown_requested = 1;
1878 qemu_notify_event();
1881 void qemu_register_powerdown_notifier(Notifier *notifier)
1883 notifier_list_add(&powerdown_notifiers, notifier);
1886 void qemu_system_debug_request(void)
1888 debug_requested = 1;
1889 qemu_notify_event();
1892 static bool main_loop_should_exit(void)
1894 RunState r;
1895 ShutdownCause request;
1897 if (qemu_debug_requested()) {
1898 vm_stop(RUN_STATE_DEBUG);
1900 if (qemu_suspend_requested()) {
1901 qemu_system_suspend();
1903 request = qemu_shutdown_requested();
1904 if (request) {
1905 qemu_kill_report();
1906 qapi_event_send_shutdown(shutdown_caused_by_guest(request),
1907 &error_abort);
1908 if (no_shutdown) {
1909 vm_stop(RUN_STATE_SHUTDOWN);
1910 } else {
1911 return true;
1914 request = qemu_reset_requested();
1915 if (request) {
1916 pause_all_vcpus();
1917 qemu_system_reset(request);
1918 resume_all_vcpus();
1919 if (!runstate_check(RUN_STATE_RUNNING) &&
1920 !runstate_check(RUN_STATE_INMIGRATE)) {
1921 runstate_set(RUN_STATE_PRELAUNCH);
1924 if (qemu_wakeup_requested()) {
1925 pause_all_vcpus();
1926 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1927 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1928 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1929 resume_all_vcpus();
1930 qapi_event_send_wakeup(&error_abort);
1932 if (qemu_powerdown_requested()) {
1933 qemu_system_powerdown();
1935 if (qemu_vmstop_requested(&r)) {
1936 vm_stop(r);
1938 return false;
1941 static void main_loop(void)
1943 #ifdef CONFIG_PROFILER
1944 int64_t ti;
1945 #endif
1946 do {
1947 #ifdef CONFIG_PROFILER
1948 ti = profile_getclock();
1949 #endif
1950 main_loop_wait(false);
1951 #ifdef CONFIG_PROFILER
1952 dev_time += profile_getclock() - ti;
1953 #endif
1954 } while (!main_loop_should_exit());
1957 static void version(void)
1959 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
1960 QEMU_COPYRIGHT "\n");
1963 static void help(int exitcode)
1965 version();
1966 printf("usage: %s [options] [disk_image]\n\n"
1967 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1968 error_get_progname());
1970 #define QEMU_OPTIONS_GENERATE_HELP
1971 #include "qemu-options-wrapper.h"
1973 printf("\nDuring emulation, the following keys are useful:\n"
1974 "ctrl-alt-f toggle full screen\n"
1975 "ctrl-alt-n switch to virtual console 'n'\n"
1976 "ctrl-alt toggle mouse and keyboard grab\n"
1977 "\n"
1978 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1979 "\n"
1980 QEMU_HELP_BOTTOM "\n");
1982 exit(exitcode);
1985 #define HAS_ARG 0x0001
1987 typedef struct QEMUOption {
1988 const char *name;
1989 int flags;
1990 int index;
1991 uint32_t arch_mask;
1992 } QEMUOption;
1994 static const QEMUOption qemu_options[] = {
1995 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1996 #define QEMU_OPTIONS_GENERATE_OPTIONS
1997 #include "qemu-options-wrapper.h"
1998 { NULL },
2001 typedef struct VGAInterfaceInfo {
2002 const char *opt_name; /* option name */
2003 const char *name; /* human-readable name */
2004 /* Class names indicating that support is available.
2005 * If no class is specified, the interface is always available */
2006 const char *class_names[2];
2007 } VGAInterfaceInfo;
2009 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
2010 [VGA_NONE] = {
2011 .opt_name = "none",
2013 [VGA_STD] = {
2014 .opt_name = "std",
2015 .name = "standard VGA",
2016 .class_names = { "VGA", "isa-vga" },
2018 [VGA_CIRRUS] = {
2019 .opt_name = "cirrus",
2020 .name = "Cirrus VGA",
2021 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
2023 [VGA_VMWARE] = {
2024 .opt_name = "vmware",
2025 .name = "VMWare SVGA",
2026 .class_names = { "vmware-svga" },
2028 [VGA_VIRTIO] = {
2029 .opt_name = "virtio",
2030 .name = "Virtio VGA",
2031 .class_names = { "virtio-vga" },
2033 [VGA_QXL] = {
2034 .opt_name = "qxl",
2035 .name = "QXL VGA",
2036 .class_names = { "qxl-vga" },
2038 [VGA_TCX] = {
2039 .opt_name = "tcx",
2040 .name = "TCX framebuffer",
2041 .class_names = { "SUNW,tcx" },
2043 [VGA_CG3] = {
2044 .opt_name = "cg3",
2045 .name = "CG3 framebuffer",
2046 .class_names = { "cgthree" },
2048 [VGA_XENFB] = {
2049 .opt_name = "xenfb",
2053 static bool vga_interface_available(VGAInterfaceType t)
2055 VGAInterfaceInfo *ti = &vga_interfaces[t];
2057 assert(t < VGA_TYPE_MAX);
2058 return !ti->class_names[0] ||
2059 object_class_by_name(ti->class_names[0]) ||
2060 object_class_by_name(ti->class_names[1]);
2063 static void select_vgahw(const char *p)
2065 const char *opts;
2066 int t;
2068 assert(vga_interface_type == VGA_NONE);
2069 for (t = 0; t < VGA_TYPE_MAX; t++) {
2070 VGAInterfaceInfo *ti = &vga_interfaces[t];
2071 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2072 if (!vga_interface_available(t)) {
2073 error_report("%s not available", ti->name);
2074 exit(1);
2076 vga_interface_type = t;
2077 break;
2080 if (t == VGA_TYPE_MAX) {
2081 invalid_vga:
2082 error_report("unknown vga type: %s", p);
2083 exit(1);
2085 while (*opts) {
2086 const char *nextopt;
2088 if (strstart(opts, ",retrace=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "dumb", &nextopt))
2091 vga_retrace_method = VGA_RETRACE_DUMB;
2092 else if (strstart(opts, "precise", &nextopt))
2093 vga_retrace_method = VGA_RETRACE_PRECISE;
2094 else goto invalid_vga;
2095 } else goto invalid_vga;
2096 opts = nextopt;
2100 typedef enum DisplayType {
2101 DT_DEFAULT,
2102 DT_CURSES,
2103 DT_SDL,
2104 DT_COCOA,
2105 DT_GTK,
2106 DT_EGL,
2107 DT_NONE,
2108 } DisplayType;
2110 static DisplayType select_display(const char *p)
2112 const char *opts;
2113 DisplayType display = DT_DEFAULT;
2115 if (strstart(p, "sdl", &opts)) {
2116 #ifdef CONFIG_SDL
2117 display = DT_SDL;
2118 while (*opts) {
2119 const char *nextopt;
2121 if (strstart(opts, ",frame=", &nextopt)) {
2122 opts = nextopt;
2123 if (strstart(opts, "on", &nextopt)) {
2124 no_frame = 0;
2125 } else if (strstart(opts, "off", &nextopt)) {
2126 no_frame = 1;
2127 } else {
2128 goto invalid_sdl_args;
2130 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2131 opts = nextopt;
2132 if (strstart(opts, "on", &nextopt)) {
2133 alt_grab = 1;
2134 } else if (strstart(opts, "off", &nextopt)) {
2135 alt_grab = 0;
2136 } else {
2137 goto invalid_sdl_args;
2139 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2140 opts = nextopt;
2141 if (strstart(opts, "on", &nextopt)) {
2142 ctrl_grab = 1;
2143 } else if (strstart(opts, "off", &nextopt)) {
2144 ctrl_grab = 0;
2145 } else {
2146 goto invalid_sdl_args;
2148 } else if (strstart(opts, ",window_close=", &nextopt)) {
2149 opts = nextopt;
2150 if (strstart(opts, "on", &nextopt)) {
2151 no_quit = 0;
2152 } else if (strstart(opts, "off", &nextopt)) {
2153 no_quit = 1;
2154 } else {
2155 goto invalid_sdl_args;
2157 } else if (strstart(opts, ",gl=", &nextopt)) {
2158 opts = nextopt;
2159 if (strstart(opts, "on", &nextopt)) {
2160 request_opengl = 1;
2161 } else if (strstart(opts, "off", &nextopt)) {
2162 request_opengl = 0;
2163 } else {
2164 goto invalid_sdl_args;
2166 } else {
2167 invalid_sdl_args:
2168 error_report("invalid SDL option string");
2169 exit(1);
2171 opts = nextopt;
2173 #else
2174 error_report("SDL support is disabled");
2175 exit(1);
2176 #endif
2177 } else if (strstart(p, "vnc", &opts)) {
2178 if (*opts == '=') {
2179 vnc_parse(opts + 1, &error_fatal);
2180 } else {
2181 error_report("VNC requires a display argument vnc=<display>");
2182 exit(1);
2184 } else if (strstart(p, "egl-headless", &opts)) {
2185 #ifdef CONFIG_OPENGL_DMABUF
2186 request_opengl = 1;
2187 display_opengl = 1;
2188 display = DT_EGL;
2189 #else
2190 fprintf(stderr, "egl support is disabled\n");
2191 exit(1);
2192 #endif
2193 } else if (strstart(p, "curses", &opts)) {
2194 #ifdef CONFIG_CURSES
2195 display = DT_CURSES;
2196 #else
2197 error_report("curses support is disabled");
2198 exit(1);
2199 #endif
2200 } else if (strstart(p, "gtk", &opts)) {
2201 #ifdef CONFIG_GTK
2202 display = DT_GTK;
2203 while (*opts) {
2204 const char *nextopt;
2206 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2207 opts = nextopt;
2208 if (strstart(opts, "on", &nextopt)) {
2209 grab_on_hover = true;
2210 } else if (strstart(opts, "off", &nextopt)) {
2211 grab_on_hover = false;
2212 } else {
2213 goto invalid_gtk_args;
2215 } else if (strstart(opts, ",gl=", &nextopt)) {
2216 opts = nextopt;
2217 if (strstart(opts, "on", &nextopt)) {
2218 request_opengl = 1;
2219 } else if (strstart(opts, "off", &nextopt)) {
2220 request_opengl = 0;
2221 } else {
2222 goto invalid_gtk_args;
2224 } else {
2225 invalid_gtk_args:
2226 error_report("invalid GTK option string");
2227 exit(1);
2229 opts = nextopt;
2231 #else
2232 error_report("GTK support is disabled");
2233 exit(1);
2234 #endif
2235 } else if (strstart(p, "none", &opts)) {
2236 display = DT_NONE;
2237 } else {
2238 error_report("unknown display type");
2239 exit(1);
2242 return display;
2245 static int balloon_parse(const char *arg)
2247 QemuOpts *opts;
2249 if (strcmp(arg, "none") == 0) {
2250 return 0;
2253 if (!strncmp(arg, "virtio", 6)) {
2254 if (arg[6] == ',') {
2255 /* have params -> parse them */
2256 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2257 false);
2258 if (!opts)
2259 return -1;
2260 } else {
2261 /* create empty opts */
2262 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2263 &error_abort);
2265 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2266 return 0;
2269 return -1;
2272 char *qemu_find_file(int type, const char *name)
2274 int i;
2275 const char *subdir;
2276 char *buf;
2278 /* Try the name as a straight path first */
2279 if (access(name, R_OK) == 0) {
2280 trace_load_file(name, name);
2281 return g_strdup(name);
2284 switch (type) {
2285 case QEMU_FILE_TYPE_BIOS:
2286 subdir = "";
2287 break;
2288 case QEMU_FILE_TYPE_KEYMAP:
2289 subdir = "keymaps/";
2290 break;
2291 default:
2292 abort();
2295 for (i = 0; i < data_dir_idx; i++) {
2296 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2297 if (access(buf, R_OK) == 0) {
2298 trace_load_file(name, buf);
2299 return buf;
2301 g_free(buf);
2303 return NULL;
2306 static void qemu_add_data_dir(const char *path)
2308 int i;
2310 if (path == NULL) {
2311 return;
2313 if (data_dir_idx == ARRAY_SIZE(data_dir)) {
2314 return;
2316 for (i = 0; i < data_dir_idx; i++) {
2317 if (strcmp(data_dir[i], path) == 0) {
2318 return; /* duplicate */
2321 data_dir[data_dir_idx++] = path;
2324 static inline bool nonempty_str(const char *str)
2326 return str && *str;
2329 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2331 gchar *buf;
2332 size_t size;
2333 const char *name, *file, *str;
2334 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2336 if (fw_cfg == NULL) {
2337 error_report("fw_cfg device not available");
2338 return -1;
2340 name = qemu_opt_get(opts, "name");
2341 file = qemu_opt_get(opts, "file");
2342 str = qemu_opt_get(opts, "string");
2344 /* we need name and either a file or the content string */
2345 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2346 error_report("invalid argument(s)");
2347 return -1;
2349 if (nonempty_str(file) && nonempty_str(str)) {
2350 error_report("file and string are mutually exclusive");
2351 return -1;
2353 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2354 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2355 return -1;
2357 if (strncmp(name, "opt/", 4) != 0) {
2358 warn_report("externally provided fw_cfg item names "
2359 "should be prefixed with \"opt/\"");
2361 if (nonempty_str(str)) {
2362 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2363 buf = g_memdup(str, size);
2364 } else {
2365 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2366 error_report("can't load %s", file);
2367 return -1;
2370 /* For legacy, keep user files in a specific global order. */
2371 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2372 fw_cfg_add_file(fw_cfg, name, buf, size);
2373 fw_cfg_reset_order_override(fw_cfg);
2374 return 0;
2377 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2379 return qdev_device_help(opts);
2382 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2384 Error *err = NULL;
2385 DeviceState *dev;
2387 dev = qdev_device_add(opts, &err);
2388 if (!dev) {
2389 error_report_err(err);
2390 return -1;
2392 object_unref(OBJECT(dev));
2393 return 0;
2396 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2398 Error *local_err = NULL;
2400 if (!qemu_chr_new_from_opts(opts, &local_err)) {
2401 if (local_err) {
2402 error_report_err(local_err);
2403 return -1;
2405 exit(0);
2407 return 0;
2410 #ifdef CONFIG_VIRTFS
2411 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2413 return qemu_fsdev_add(opts);
2415 #endif
2417 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2419 Chardev *chr;
2420 const char *chardev;
2421 const char *mode;
2422 int flags;
2424 mode = qemu_opt_get(opts, "mode");
2425 if (mode == NULL) {
2426 mode = "readline";
2428 if (strcmp(mode, "readline") == 0) {
2429 flags = MONITOR_USE_READLINE;
2430 } else if (strcmp(mode, "control") == 0) {
2431 flags = MONITOR_USE_CONTROL;
2432 } else {
2433 error_report("unknown monitor mode \"%s\"", mode);
2434 exit(1);
2437 if (qemu_opt_get_bool(opts, "pretty", 0))
2438 flags |= MONITOR_USE_PRETTY;
2440 if (qemu_opt_get_bool(opts, "default", 0)) {
2441 error_report("option 'default' does nothing and is deprecated");
2444 chardev = qemu_opt_get(opts, "chardev");
2445 chr = qemu_chr_find(chardev);
2446 if (chr == NULL) {
2447 error_report("chardev \"%s\" not found", chardev);
2448 exit(1);
2451 monitor_init(chr, flags);
2452 return 0;
2455 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2457 static int monitor_device_index = 0;
2458 QemuOpts *opts;
2459 const char *p;
2460 char label[32];
2462 if (strstart(optarg, "chardev:", &p)) {
2463 snprintf(label, sizeof(label), "%s", p);
2464 } else {
2465 snprintf(label, sizeof(label), "compat_monitor%d",
2466 monitor_device_index);
2467 opts = qemu_chr_parse_compat(label, optarg);
2468 if (!opts) {
2469 error_report("parse error: %s", optarg);
2470 exit(1);
2474 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2475 qemu_opt_set(opts, "mode", mode, &error_abort);
2476 qemu_opt_set(opts, "chardev", label, &error_abort);
2477 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2478 monitor_device_index++;
2481 struct device_config {
2482 enum {
2483 DEV_USB, /* -usbdevice */
2484 DEV_BT, /* -bt */
2485 DEV_SERIAL, /* -serial */
2486 DEV_PARALLEL, /* -parallel */
2487 DEV_VIRTCON, /* -virtioconsole */
2488 DEV_DEBUGCON, /* -debugcon */
2489 DEV_GDB, /* -gdb, -s */
2490 DEV_SCLP, /* s390 sclp */
2491 } type;
2492 const char *cmdline;
2493 Location loc;
2494 QTAILQ_ENTRY(device_config) next;
2497 static QTAILQ_HEAD(, device_config) device_configs =
2498 QTAILQ_HEAD_INITIALIZER(device_configs);
2500 static void add_device_config(int type, const char *cmdline)
2502 struct device_config *conf;
2504 conf = g_malloc0(sizeof(*conf));
2505 conf->type = type;
2506 conf->cmdline = cmdline;
2507 loc_save(&conf->loc);
2508 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2511 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2513 struct device_config *conf;
2514 int rc;
2516 QTAILQ_FOREACH(conf, &device_configs, next) {
2517 if (conf->type != type)
2518 continue;
2519 loc_push_restore(&conf->loc);
2520 rc = func(conf->cmdline);
2521 loc_pop(&conf->loc);
2522 if (rc) {
2523 return rc;
2526 return 0;
2529 static int serial_parse(const char *devname)
2531 static int index = 0;
2532 char label[32];
2534 if (strcmp(devname, "none") == 0)
2535 return 0;
2536 if (index == MAX_SERIAL_PORTS) {
2537 error_report("too many serial ports");
2538 exit(1);
2540 snprintf(label, sizeof(label), "serial%d", index);
2541 serial_hds[index] = qemu_chr_new(label, devname);
2542 if (!serial_hds[index]) {
2543 error_report("could not connect serial device"
2544 " to character backend '%s'", devname);
2545 return -1;
2547 index++;
2548 return 0;
2551 static int parallel_parse(const char *devname)
2553 static int index = 0;
2554 char label[32];
2556 if (strcmp(devname, "none") == 0)
2557 return 0;
2558 if (index == MAX_PARALLEL_PORTS) {
2559 error_report("too many parallel ports");
2560 exit(1);
2562 snprintf(label, sizeof(label), "parallel%d", index);
2563 parallel_hds[index] = qemu_chr_new(label, devname);
2564 if (!parallel_hds[index]) {
2565 error_report("could not connect parallel device"
2566 " to character backend '%s'", devname);
2567 return -1;
2569 index++;
2570 return 0;
2573 static int virtcon_parse(const char *devname)
2575 QemuOptsList *device = qemu_find_opts("device");
2576 static int index = 0;
2577 char label[32];
2578 QemuOpts *bus_opts, *dev_opts;
2580 if (strcmp(devname, "none") == 0)
2581 return 0;
2582 if (index == MAX_VIRTIO_CONSOLES) {
2583 error_report("too many virtio consoles");
2584 exit(1);
2587 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2588 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2590 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2591 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2593 snprintf(label, sizeof(label), "virtcon%d", index);
2594 virtcon_hds[index] = qemu_chr_new(label, devname);
2595 if (!virtcon_hds[index]) {
2596 error_report("could not connect virtio console"
2597 " to character backend '%s'", devname);
2598 return -1;
2600 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2602 index++;
2603 return 0;
2606 static int sclp_parse(const char *devname)
2608 QemuOptsList *device = qemu_find_opts("device");
2609 static int index = 0;
2610 char label[32];
2611 QemuOpts *dev_opts;
2613 if (strcmp(devname, "none") == 0) {
2614 return 0;
2616 if (index == MAX_SCLP_CONSOLES) {
2617 error_report("too many sclp consoles");
2618 exit(1);
2621 assert(arch_type == QEMU_ARCH_S390X);
2623 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2624 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2626 snprintf(label, sizeof(label), "sclpcon%d", index);
2627 sclp_hds[index] = qemu_chr_new(label, devname);
2628 if (!sclp_hds[index]) {
2629 error_report("could not connect sclp console"
2630 " to character backend '%s'", devname);
2631 return -1;
2633 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2635 index++;
2636 return 0;
2639 static int debugcon_parse(const char *devname)
2641 QemuOpts *opts;
2643 if (!qemu_chr_new("debugcon", devname)) {
2644 exit(1);
2646 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2647 if (!opts) {
2648 error_report("already have a debugcon device");
2649 exit(1);
2651 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2652 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2653 return 0;
2656 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2658 const MachineClass *mc1 = a, *mc2 = b;
2659 int res;
2661 if (mc1->family == NULL) {
2662 if (mc2->family == NULL) {
2663 /* Compare standalone machine types against each other; they sort
2664 * in increasing order.
2666 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2667 object_class_get_name(OBJECT_CLASS(mc2)));
2670 /* Standalone machine types sort after families. */
2671 return 1;
2674 if (mc2->family == NULL) {
2675 /* Families sort before standalone machine types. */
2676 return -1;
2679 /* Families sort between each other alphabetically increasingly. */
2680 res = strcmp(mc1->family, mc2->family);
2681 if (res != 0) {
2682 return res;
2685 /* Within the same family, machine types sort in decreasing order. */
2686 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2687 object_class_get_name(OBJECT_CLASS(mc1)));
2690 static MachineClass *machine_parse(const char *name)
2692 MachineClass *mc = NULL;
2693 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2695 if (name) {
2696 mc = find_machine(name);
2698 if (mc) {
2699 g_slist_free(machines);
2700 return mc;
2702 if (name && !is_help_option(name)) {
2703 error_report("unsupported machine type");
2704 error_printf("Use -machine help to list supported machines\n");
2705 } else {
2706 printf("Supported machines are:\n");
2707 machines = g_slist_sort(machines, machine_class_cmp);
2708 for (el = machines; el; el = el->next) {
2709 MachineClass *mc = el->data;
2710 if (mc->alias) {
2711 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2713 printf("%-20s %s%s\n", mc->name, mc->desc,
2714 mc->is_default ? " (default)" : "");
2718 g_slist_free(machines);
2719 exit(!name || !is_help_option(name));
2722 void qemu_add_exit_notifier(Notifier *notify)
2724 notifier_list_add(&exit_notifiers, notify);
2727 void qemu_remove_exit_notifier(Notifier *notify)
2729 notifier_remove(notify);
2732 static void qemu_run_exit_notifiers(void)
2734 notifier_list_notify(&exit_notifiers, NULL);
2737 static bool machine_init_done;
2739 void qemu_add_machine_init_done_notifier(Notifier *notify)
2741 notifier_list_add(&machine_init_done_notifiers, notify);
2742 if (machine_init_done) {
2743 notify->notify(notify, NULL);
2747 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2749 notifier_remove(notify);
2752 static void qemu_run_machine_init_done_notifiers(void)
2754 notifier_list_notify(&machine_init_done_notifiers, NULL);
2755 machine_init_done = true;
2758 static const QEMUOption *lookup_opt(int argc, char **argv,
2759 const char **poptarg, int *poptind)
2761 const QEMUOption *popt;
2762 int optind = *poptind;
2763 char *r = argv[optind];
2764 const char *optarg;
2766 loc_set_cmdline(argv, optind, 1);
2767 optind++;
2768 /* Treat --foo the same as -foo. */
2769 if (r[1] == '-')
2770 r++;
2771 popt = qemu_options;
2772 for(;;) {
2773 if (!popt->name) {
2774 error_report("invalid option");
2775 exit(1);
2777 if (!strcmp(popt->name, r + 1))
2778 break;
2779 popt++;
2781 if (popt->flags & HAS_ARG) {
2782 if (optind >= argc) {
2783 error_report("requires an argument");
2784 exit(1);
2786 optarg = argv[optind++];
2787 loc_set_cmdline(argv, optind - 2, 2);
2788 } else {
2789 optarg = NULL;
2792 *poptarg = optarg;
2793 *poptind = optind;
2795 return popt;
2798 static MachineClass *select_machine(void)
2800 MachineClass *machine_class = find_default_machine();
2801 const char *optarg;
2802 QemuOpts *opts;
2803 Location loc;
2805 loc_push_none(&loc);
2807 opts = qemu_get_machine_opts();
2808 qemu_opts_loc_restore(opts);
2810 optarg = qemu_opt_get(opts, "type");
2811 if (optarg) {
2812 machine_class = machine_parse(optarg);
2815 if (!machine_class) {
2816 error_report("No machine specified, and there is no default");
2817 error_printf("Use -machine help to list supported machines\n");
2818 exit(1);
2821 loc_pop(&loc);
2822 return machine_class;
2825 static int machine_set_property(void *opaque,
2826 const char *name, const char *value,
2827 Error **errp)
2829 Object *obj = OBJECT(opaque);
2830 Error *local_err = NULL;
2831 char *p, *qom_name;
2833 if (strcmp(name, "type") == 0) {
2834 return 0;
2837 qom_name = g_strdup(name);
2838 for (p = qom_name; *p; p++) {
2839 if (*p == '_') {
2840 *p = '-';
2844 object_property_parse(obj, value, qom_name, &local_err);
2845 g_free(qom_name);
2847 if (local_err) {
2848 error_report_err(local_err);
2849 return -1;
2852 return 0;
2857 * Initial object creation happens before all other
2858 * QEMU data types are created. The majority of objects
2859 * can be created at this point. The rng-egd object
2860 * cannot be created here, as it depends on the chardev
2861 * already existing.
2863 static bool object_create_initial(const char *type)
2865 if (g_str_equal(type, "rng-egd") ||
2866 g_str_has_prefix(type, "pr-manager-")) {
2867 return false;
2871 * return false for concrete netfilters since
2872 * they depend on netdevs already existing
2874 if (g_str_equal(type, "filter-buffer") ||
2875 g_str_equal(type, "filter-dump") ||
2876 g_str_equal(type, "filter-mirror") ||
2877 g_str_equal(type, "filter-redirector") ||
2878 g_str_equal(type, "colo-compare") ||
2879 g_str_equal(type, "filter-rewriter") ||
2880 g_str_equal(type, "filter-replay")) {
2881 return false;
2884 /* Memory allocation by backends needs to be done
2885 * after configure_accelerator() (due to the tcg_enabled()
2886 * checks at memory_region_init_*()).
2888 * Also, allocation of large amounts of memory may delay
2889 * chardev initialization for too long, and trigger timeouts
2890 * on software that waits for a monitor socket to be created
2891 * (e.g. libvirt).
2893 if (g_str_has_prefix(type, "memory-backend-")) {
2894 return false;
2897 return true;
2902 * The remainder of object creation happens after the
2903 * creation of chardev, fsdev, net clients and device data types.
2905 static bool object_create_delayed(const char *type)
2907 return !object_create_initial(type);
2911 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2912 MachineClass *mc)
2914 uint64_t sz;
2915 const char *mem_str;
2916 const char *maxmem_str, *slots_str;
2917 const ram_addr_t default_ram_size = mc->default_ram_size;
2918 QemuOpts *opts = qemu_find_opts_singleton("memory");
2919 Location loc;
2921 loc_push_none(&loc);
2922 qemu_opts_loc_restore(opts);
2924 sz = 0;
2925 mem_str = qemu_opt_get(opts, "size");
2926 if (mem_str) {
2927 if (!*mem_str) {
2928 error_report("missing 'size' option value");
2929 exit(EXIT_FAILURE);
2932 sz = qemu_opt_get_size(opts, "size", ram_size);
2934 /* Fix up legacy suffix-less format */
2935 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2936 uint64_t overflow_check = sz;
2938 sz <<= 20;
2939 if ((sz >> 20) != overflow_check) {
2940 error_report("too large 'size' option value");
2941 exit(EXIT_FAILURE);
2946 /* backward compatibility behaviour for case "-m 0" */
2947 if (sz == 0) {
2948 sz = default_ram_size;
2951 sz = QEMU_ALIGN_UP(sz, 8192);
2952 ram_size = sz;
2953 if (ram_size != sz) {
2954 error_report("ram size too large");
2955 exit(EXIT_FAILURE);
2958 /* store value for the future use */
2959 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2960 *maxram_size = ram_size;
2962 maxmem_str = qemu_opt_get(opts, "maxmem");
2963 slots_str = qemu_opt_get(opts, "slots");
2964 if (maxmem_str && slots_str) {
2965 uint64_t slots;
2967 sz = qemu_opt_get_size(opts, "maxmem", 0);
2968 slots = qemu_opt_get_number(opts, "slots", 0);
2969 if (sz < ram_size) {
2970 error_report("invalid value of -m option maxmem: "
2971 "maximum memory size (0x%" PRIx64 ") must be at least "
2972 "the initial memory size (0x" RAM_ADDR_FMT ")",
2973 sz, ram_size);
2974 exit(EXIT_FAILURE);
2975 } else if (sz > ram_size) {
2976 if (!slots) {
2977 error_report("invalid value of -m option: maxmem was "
2978 "specified, but no hotplug slots were specified");
2979 exit(EXIT_FAILURE);
2981 } else if (slots) {
2982 error_report("invalid value of -m option maxmem: "
2983 "memory slots were specified but maximum memory size "
2984 "(0x%" PRIx64 ") is equal to the initial memory size "
2985 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2986 exit(EXIT_FAILURE);
2989 *maxram_size = sz;
2990 *ram_slots = slots;
2991 } else if ((!maxmem_str && slots_str) ||
2992 (maxmem_str && !slots_str)) {
2993 error_report("invalid -m option value: missing "
2994 "'%s' option", slots_str ? "maxmem" : "slots");
2995 exit(EXIT_FAILURE);
2998 loc_pop(&loc);
3001 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
3003 GlobalProperty *g;
3005 g = g_malloc0(sizeof(*g));
3006 g->driver = qemu_opt_get(opts, "driver");
3007 g->property = qemu_opt_get(opts, "property");
3008 g->value = qemu_opt_get(opts, "value");
3009 g->user_provided = true;
3010 g->errp = &error_fatal;
3011 qdev_prop_register_global(g);
3012 return 0;
3015 static int qemu_read_default_config_file(void)
3017 int ret;
3019 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
3020 if (ret < 0 && ret != -ENOENT) {
3021 return ret;
3024 return 0;
3027 static void user_register_global_props(void)
3029 qemu_opts_foreach(qemu_find_opts("global"),
3030 global_init_func, NULL, NULL);
3034 * Note: we should see that these properties are actually having a
3035 * priority: accel < machine < user. This means e.g. when user
3036 * specifies something in "-global", it'll always be used with highest
3037 * priority than either machine/accelerator compat properties.
3039 static void register_global_properties(MachineState *ms)
3041 accel_register_compat_props(ms->accelerator);
3042 machine_register_compat_props(ms);
3043 user_register_global_props();
3046 int main(int argc, char **argv, char **envp)
3048 int i;
3049 int snapshot, linux_boot;
3050 const char *initrd_filename;
3051 const char *kernel_filename, *kernel_cmdline;
3052 const char *boot_order = NULL;
3053 const char *boot_once = NULL;
3054 DisplayState *ds;
3055 int cyls, heads, secs, translation;
3056 QemuOpts *opts, *machine_opts;
3057 QemuOpts *hda_opts = NULL, *icount_opts = NULL, *accel_opts = NULL;
3058 QemuOptsList *olist;
3059 int optind;
3060 const char *optarg;
3061 const char *loadvm = NULL;
3062 MachineClass *machine_class;
3063 const char *cpu_model;
3064 const char *vga_model = NULL;
3065 const char *qtest_chrdev = NULL;
3066 const char *qtest_log = NULL;
3067 const char *pid_file = NULL;
3068 const char *incoming = NULL;
3069 bool userconfig = true;
3070 bool nographic = false;
3071 DisplayType display_type = DT_DEFAULT;
3072 int display_remote = 0;
3073 const char *log_mask = NULL;
3074 const char *log_file = NULL;
3075 char *trace_file = NULL;
3076 ram_addr_t maxram_size;
3077 uint64_t ram_slots = 0;
3078 FILE *vmstate_dump_file = NULL;
3079 Error *main_loop_err = NULL;
3080 Error *err = NULL;
3081 bool list_data_dirs = false;
3082 char **dirs;
3083 typedef struct BlockdevOptions_queue {
3084 BlockdevOptions *bdo;
3085 Location loc;
3086 QSIMPLEQ_ENTRY(BlockdevOptions_queue) entry;
3087 } BlockdevOptions_queue;
3088 QSIMPLEQ_HEAD(, BlockdevOptions_queue) bdo_queue
3089 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
3091 module_call_init(MODULE_INIT_TRACE);
3093 qemu_init_cpu_list();
3094 qemu_init_cpu_loop();
3095 qemu_mutex_lock_iothread();
3097 atexit(qemu_run_exit_notifiers);
3098 error_set_progname(argv[0]);
3099 qemu_init_exec_dir(argv[0]);
3101 module_call_init(MODULE_INIT_QOM);
3102 monitor_init_qmp_commands();
3104 qemu_add_opts(&qemu_drive_opts);
3105 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3106 qemu_add_drive_opts(&qemu_common_drive_opts);
3107 qemu_add_drive_opts(&qemu_drive_opts);
3108 qemu_add_drive_opts(&bdrv_runtime_opts);
3109 qemu_add_opts(&qemu_chardev_opts);
3110 qemu_add_opts(&qemu_device_opts);
3111 qemu_add_opts(&qemu_netdev_opts);
3112 qemu_add_opts(&qemu_net_opts);
3113 qemu_add_opts(&qemu_rtc_opts);
3114 qemu_add_opts(&qemu_global_opts);
3115 qemu_add_opts(&qemu_mon_opts);
3116 qemu_add_opts(&qemu_trace_opts);
3117 qemu_add_opts(&qemu_option_rom_opts);
3118 qemu_add_opts(&qemu_machine_opts);
3119 qemu_add_opts(&qemu_accel_opts);
3120 qemu_add_opts(&qemu_mem_opts);
3121 qemu_add_opts(&qemu_smp_opts);
3122 qemu_add_opts(&qemu_boot_opts);
3123 qemu_add_opts(&qemu_sandbox_opts);
3124 qemu_add_opts(&qemu_add_fd_opts);
3125 qemu_add_opts(&qemu_object_opts);
3126 qemu_add_opts(&qemu_tpmdev_opts);
3127 qemu_add_opts(&qemu_realtime_opts);
3128 qemu_add_opts(&qemu_msg_opts);
3129 qemu_add_opts(&qemu_name_opts);
3130 qemu_add_opts(&qemu_numa_opts);
3131 qemu_add_opts(&qemu_icount_opts);
3132 qemu_add_opts(&qemu_semihosting_config_opts);
3133 qemu_add_opts(&qemu_fw_cfg_opts);
3134 module_call_init(MODULE_INIT_OPTS);
3136 runstate_init();
3138 if (qcrypto_init(&err) < 0) {
3139 error_reportf_err(err, "cannot initialize crypto: ");
3140 exit(1);
3142 rtc_clock = QEMU_CLOCK_HOST;
3144 QLIST_INIT (&vm_change_state_head);
3145 os_setup_early_signal_handling();
3147 cpu_model = NULL;
3148 snapshot = 0;
3149 cyls = heads = secs = 0;
3150 translation = BIOS_ATA_TRANSLATION_AUTO;
3152 nb_nics = 0;
3154 bdrv_init_with_whitelist();
3156 autostart = 1;
3158 /* first pass of option parsing */
3159 optind = 1;
3160 while (optind < argc) {
3161 if (argv[optind][0] != '-') {
3162 /* disk image */
3163 optind++;
3164 } else {
3165 const QEMUOption *popt;
3167 popt = lookup_opt(argc, argv, &optarg, &optind);
3168 switch (popt->index) {
3169 case QEMU_OPTION_nodefconfig:
3170 case QEMU_OPTION_nouserconfig:
3171 userconfig = false;
3172 break;
3177 if (userconfig) {
3178 if (qemu_read_default_config_file() < 0) {
3179 exit(1);
3183 /* second pass of option parsing */
3184 optind = 1;
3185 for(;;) {
3186 if (optind >= argc)
3187 break;
3188 if (argv[optind][0] != '-') {
3189 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3190 } else {
3191 const QEMUOption *popt;
3193 popt = lookup_opt(argc, argv, &optarg, &optind);
3194 if (!(popt->arch_mask & arch_type)) {
3195 error_report("Option not supported for this target");
3196 exit(1);
3198 switch(popt->index) {
3199 case QEMU_OPTION_no_kvm_irqchip: {
3200 olist = qemu_find_opts("machine");
3201 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3202 break;
3204 case QEMU_OPTION_cpu:
3205 /* hw initialization will check this */
3206 cpu_model = optarg;
3207 break;
3208 case QEMU_OPTION_hda:
3210 char buf[256];
3211 if (cyls == 0)
3212 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3213 else
3214 snprintf(buf, sizeof(buf),
3215 "%s,cyls=%d,heads=%d,secs=%d%s",
3216 HD_OPTS , cyls, heads, secs,
3217 translation == BIOS_ATA_TRANSLATION_LBA ?
3218 ",trans=lba" :
3219 translation == BIOS_ATA_TRANSLATION_NONE ?
3220 ",trans=none" : "");
3221 drive_add(IF_DEFAULT, 0, optarg, buf);
3222 break;
3224 case QEMU_OPTION_hdb:
3225 case QEMU_OPTION_hdc:
3226 case QEMU_OPTION_hdd:
3227 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3228 HD_OPTS);
3229 break;
3230 case QEMU_OPTION_blockdev:
3232 Visitor *v;
3233 BlockdevOptions_queue *bdo;
3235 v = qobject_input_visitor_new_str(optarg, "driver", &err);
3236 if (!v) {
3237 error_report_err(err);
3238 exit(1);
3241 bdo = g_new(BlockdevOptions_queue, 1);
3242 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
3243 &error_fatal);
3244 visit_free(v);
3245 loc_save(&bdo->loc);
3246 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
3247 break;
3249 case QEMU_OPTION_drive:
3250 if (drive_def(optarg) == NULL) {
3251 exit(1);
3253 break;
3254 case QEMU_OPTION_set:
3255 if (qemu_set_option(optarg) != 0)
3256 exit(1);
3257 break;
3258 case QEMU_OPTION_global:
3259 if (qemu_global_option(optarg) != 0)
3260 exit(1);
3261 break;
3262 case QEMU_OPTION_mtdblock:
3263 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3264 break;
3265 case QEMU_OPTION_sd:
3266 drive_add(IF_SD, -1, optarg, SD_OPTS);
3267 break;
3268 case QEMU_OPTION_pflash:
3269 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3270 break;
3271 case QEMU_OPTION_snapshot:
3272 snapshot = 1;
3273 break;
3274 case QEMU_OPTION_hdachs:
3276 const char *p;
3277 p = optarg;
3278 cyls = strtol(p, (char **)&p, 0);
3279 if (cyls < 1 || cyls > 16383)
3280 goto chs_fail;
3281 if (*p != ',')
3282 goto chs_fail;
3283 p++;
3284 heads = strtol(p, (char **)&p, 0);
3285 if (heads < 1 || heads > 16)
3286 goto chs_fail;
3287 if (*p != ',')
3288 goto chs_fail;
3289 p++;
3290 secs = strtol(p, (char **)&p, 0);
3291 if (secs < 1 || secs > 63)
3292 goto chs_fail;
3293 if (*p == ',') {
3294 p++;
3295 if (!strcmp(p, "large")) {
3296 translation = BIOS_ATA_TRANSLATION_LARGE;
3297 } else if (!strcmp(p, "rechs")) {
3298 translation = BIOS_ATA_TRANSLATION_RECHS;
3299 } else if (!strcmp(p, "none")) {
3300 translation = BIOS_ATA_TRANSLATION_NONE;
3301 } else if (!strcmp(p, "lba")) {
3302 translation = BIOS_ATA_TRANSLATION_LBA;
3303 } else if (!strcmp(p, "auto")) {
3304 translation = BIOS_ATA_TRANSLATION_AUTO;
3305 } else {
3306 goto chs_fail;
3308 } else if (*p != '\0') {
3309 chs_fail:
3310 error_report("invalid physical CHS format");
3311 exit(1);
3313 if (hda_opts != NULL) {
3314 qemu_opt_set_number(hda_opts, "cyls", cyls,
3315 &error_abort);
3316 qemu_opt_set_number(hda_opts, "heads", heads,
3317 &error_abort);
3318 qemu_opt_set_number(hda_opts, "secs", secs,
3319 &error_abort);
3320 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3321 qemu_opt_set(hda_opts, "trans", "large",
3322 &error_abort);
3323 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3324 qemu_opt_set(hda_opts, "trans", "rechs",
3325 &error_abort);
3326 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3327 qemu_opt_set(hda_opts, "trans", "lba",
3328 &error_abort);
3329 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3330 qemu_opt_set(hda_opts, "trans", "none",
3331 &error_abort);
3335 error_report("'-hdachs' is deprecated, please use '-device"
3336 " ide-hd,cyls=c,heads=h,secs=s,...' instead");
3337 break;
3338 case QEMU_OPTION_numa:
3339 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3340 optarg, true);
3341 if (!opts) {
3342 exit(1);
3344 break;
3345 case QEMU_OPTION_display:
3346 display_type = select_display(optarg);
3347 break;
3348 case QEMU_OPTION_nographic:
3349 olist = qemu_find_opts("machine");
3350 qemu_opts_parse_noisily(olist, "graphics=off", false);
3351 nographic = true;
3352 display_type = DT_NONE;
3353 break;
3354 case QEMU_OPTION_curses:
3355 #ifdef CONFIG_CURSES
3356 display_type = DT_CURSES;
3357 #else
3358 error_report("curses support is disabled");
3359 exit(1);
3360 #endif
3361 break;
3362 case QEMU_OPTION_portrait:
3363 graphic_rotate = 90;
3364 break;
3365 case QEMU_OPTION_rotate:
3366 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3367 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3368 graphic_rotate != 180 && graphic_rotate != 270) {
3369 error_report("only 90, 180, 270 deg rotation is available");
3370 exit(1);
3372 break;
3373 case QEMU_OPTION_kernel:
3374 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3375 &error_abort);
3376 break;
3377 case QEMU_OPTION_initrd:
3378 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3379 &error_abort);
3380 break;
3381 case QEMU_OPTION_append:
3382 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3383 &error_abort);
3384 break;
3385 case QEMU_OPTION_dtb:
3386 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3387 &error_abort);
3388 break;
3389 case QEMU_OPTION_cdrom:
3390 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3391 break;
3392 case QEMU_OPTION_boot:
3393 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3394 optarg, true);
3395 if (!opts) {
3396 exit(1);
3398 break;
3399 case QEMU_OPTION_fda:
3400 case QEMU_OPTION_fdb:
3401 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3402 optarg, FD_OPTS);
3403 break;
3404 case QEMU_OPTION_no_fd_bootchk:
3405 fd_bootchk = 0;
3406 break;
3407 case QEMU_OPTION_netdev:
3408 default_net = 0;
3409 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3410 exit(1);
3412 break;
3413 case QEMU_OPTION_net:
3414 default_net = 0;
3415 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3416 exit(1);
3418 break;
3419 #ifdef CONFIG_LIBISCSI
3420 case QEMU_OPTION_iscsi:
3421 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3422 optarg, false);
3423 if (!opts) {
3424 exit(1);
3426 break;
3427 #endif
3428 #ifdef CONFIG_SLIRP
3429 case QEMU_OPTION_tftp:
3430 error_report("The -tftp option is deprecated. "
3431 "Please use '-netdev user,tftp=...' instead.");
3432 legacy_tftp_prefix = optarg;
3433 break;
3434 case QEMU_OPTION_bootp:
3435 error_report("The -bootp option is deprecated. "
3436 "Please use '-netdev user,bootfile=...' instead.");
3437 legacy_bootp_filename = optarg;
3438 break;
3439 case QEMU_OPTION_redir:
3440 error_report("The -redir option is deprecated. "
3441 "Please use '-netdev user,hostfwd=...' instead.");
3442 if (net_slirp_redir(optarg) < 0)
3443 exit(1);
3444 break;
3445 #endif
3446 case QEMU_OPTION_bt:
3447 add_device_config(DEV_BT, optarg);
3448 break;
3449 case QEMU_OPTION_audio_help:
3450 AUD_help ();
3451 exit (0);
3452 break;
3453 case QEMU_OPTION_soundhw:
3454 select_soundhw (optarg);
3455 break;
3456 case QEMU_OPTION_h:
3457 help(0);
3458 break;
3459 case QEMU_OPTION_version:
3460 version();
3461 exit(0);
3462 break;
3463 case QEMU_OPTION_m:
3464 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3465 optarg, true);
3466 if (!opts) {
3467 exit(EXIT_FAILURE);
3469 break;
3470 #ifdef CONFIG_TPM
3471 case QEMU_OPTION_tpmdev:
3472 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3473 exit(1);
3475 break;
3476 #endif
3477 case QEMU_OPTION_mempath:
3478 mem_path = optarg;
3479 break;
3480 case QEMU_OPTION_mem_prealloc:
3481 mem_prealloc = 1;
3482 break;
3483 case QEMU_OPTION_d:
3484 log_mask = optarg;
3485 break;
3486 case QEMU_OPTION_D:
3487 log_file = optarg;
3488 break;
3489 case QEMU_OPTION_DFILTER:
3490 qemu_set_dfilter_ranges(optarg, &error_fatal);
3491 break;
3492 case QEMU_OPTION_s:
3493 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3494 break;
3495 case QEMU_OPTION_gdb:
3496 add_device_config(DEV_GDB, optarg);
3497 break;
3498 case QEMU_OPTION_L:
3499 if (is_help_option(optarg)) {
3500 list_data_dirs = true;
3501 } else {
3502 qemu_add_data_dir(optarg);
3504 break;
3505 case QEMU_OPTION_bios:
3506 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3507 &error_abort);
3508 break;
3509 case QEMU_OPTION_singlestep:
3510 singlestep = 1;
3511 break;
3512 case QEMU_OPTION_S:
3513 autostart = 0;
3514 break;
3515 case QEMU_OPTION_k:
3516 keyboard_layout = optarg;
3517 break;
3518 case QEMU_OPTION_localtime:
3519 rtc_utc = 0;
3520 break;
3521 case QEMU_OPTION_vga:
3522 vga_model = optarg;
3523 default_vga = 0;
3524 break;
3525 case QEMU_OPTION_g:
3527 const char *p;
3528 int w, h, depth;
3529 p = optarg;
3530 w = strtol(p, (char **)&p, 10);
3531 if (w <= 0) {
3532 graphic_error:
3533 error_report("invalid resolution or depth");
3534 exit(1);
3536 if (*p != 'x')
3537 goto graphic_error;
3538 p++;
3539 h = strtol(p, (char **)&p, 10);
3540 if (h <= 0)
3541 goto graphic_error;
3542 if (*p == 'x') {
3543 p++;
3544 depth = strtol(p, (char **)&p, 10);
3545 if (depth != 8 && depth != 15 && depth != 16 &&
3546 depth != 24 && depth != 32)
3547 goto graphic_error;
3548 } else if (*p == '\0') {
3549 depth = graphic_depth;
3550 } else {
3551 goto graphic_error;
3554 graphic_width = w;
3555 graphic_height = h;
3556 graphic_depth = depth;
3558 break;
3559 case QEMU_OPTION_echr:
3561 char *r;
3562 term_escape_char = strtol(optarg, &r, 0);
3563 if (r == optarg)
3564 printf("Bad argument to echr\n");
3565 break;
3567 case QEMU_OPTION_monitor:
3568 default_monitor = 0;
3569 if (strncmp(optarg, "none", 4)) {
3570 monitor_parse(optarg, "readline", false);
3572 break;
3573 case QEMU_OPTION_qmp:
3574 monitor_parse(optarg, "control", false);
3575 default_monitor = 0;
3576 break;
3577 case QEMU_OPTION_qmp_pretty:
3578 monitor_parse(optarg, "control", true);
3579 default_monitor = 0;
3580 break;
3581 case QEMU_OPTION_mon:
3582 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3583 true);
3584 if (!opts) {
3585 exit(1);
3587 default_monitor = 0;
3588 break;
3589 case QEMU_OPTION_chardev:
3590 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3591 optarg, true);
3592 if (!opts) {
3593 exit(1);
3595 break;
3596 case QEMU_OPTION_fsdev:
3597 olist = qemu_find_opts("fsdev");
3598 if (!olist) {
3599 error_report("fsdev support is disabled");
3600 exit(1);
3602 opts = qemu_opts_parse_noisily(olist, optarg, true);
3603 if (!opts) {
3604 exit(1);
3606 break;
3607 case QEMU_OPTION_virtfs: {
3608 QemuOpts *fsdev;
3609 QemuOpts *device;
3610 const char *writeout, *sock_fd, *socket, *path, *security_model;
3612 olist = qemu_find_opts("virtfs");
3613 if (!olist) {
3614 error_report("virtfs support is disabled");
3615 exit(1);
3617 opts = qemu_opts_parse_noisily(olist, optarg, true);
3618 if (!opts) {
3619 exit(1);
3622 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3623 qemu_opt_get(opts, "mount_tag") == NULL) {
3624 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3625 exit(1);
3627 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3628 qemu_opts_id(opts) ?:
3629 qemu_opt_get(opts, "mount_tag"),
3630 1, NULL);
3631 if (!fsdev) {
3632 error_report("duplicate or invalid fsdev id: %s",
3633 qemu_opt_get(opts, "mount_tag"));
3634 exit(1);
3637 writeout = qemu_opt_get(opts, "writeout");
3638 if (writeout) {
3639 #ifdef CONFIG_SYNC_FILE_RANGE
3640 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3641 #else
3642 error_report("writeout=immediate not supported "
3643 "on this platform");
3644 exit(1);
3645 #endif
3647 qemu_opt_set(fsdev, "fsdriver",
3648 qemu_opt_get(opts, "fsdriver"), &error_abort);
3649 path = qemu_opt_get(opts, "path");
3650 if (path) {
3651 qemu_opt_set(fsdev, "path", path, &error_abort);
3653 security_model = qemu_opt_get(opts, "security_model");
3654 if (security_model) {
3655 qemu_opt_set(fsdev, "security_model", security_model,
3656 &error_abort);
3658 socket = qemu_opt_get(opts, "socket");
3659 if (socket) {
3660 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3662 sock_fd = qemu_opt_get(opts, "sock_fd");
3663 if (sock_fd) {
3664 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3667 qemu_opt_set_bool(fsdev, "readonly",
3668 qemu_opt_get_bool(opts, "readonly", 0),
3669 &error_abort);
3670 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3671 &error_abort);
3672 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3673 qemu_opt_set(device, "fsdev",
3674 qemu_opts_id(fsdev), &error_abort);
3675 qemu_opt_set(device, "mount_tag",
3676 qemu_opt_get(opts, "mount_tag"), &error_abort);
3677 break;
3679 case QEMU_OPTION_virtfs_synth: {
3680 QemuOpts *fsdev;
3681 QemuOpts *device;
3683 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3684 1, NULL);
3685 if (!fsdev) {
3686 error_report("duplicate option: %s", "virtfs_synth");
3687 exit(1);
3689 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3691 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3692 &error_abort);
3693 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3694 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3695 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3696 break;
3698 case QEMU_OPTION_serial:
3699 add_device_config(DEV_SERIAL, optarg);
3700 default_serial = 0;
3701 if (strncmp(optarg, "mon:", 4) == 0) {
3702 default_monitor = 0;
3704 break;
3705 case QEMU_OPTION_watchdog:
3706 if (watchdog) {
3707 error_report("only one watchdog option may be given");
3708 return 1;
3710 watchdog = optarg;
3711 break;
3712 case QEMU_OPTION_watchdog_action:
3713 if (select_watchdog_action(optarg) == -1) {
3714 error_report("unknown -watchdog-action parameter");
3715 exit(1);
3717 break;
3718 case QEMU_OPTION_virtiocon:
3719 add_device_config(DEV_VIRTCON, optarg);
3720 default_virtcon = 0;
3721 if (strncmp(optarg, "mon:", 4) == 0) {
3722 default_monitor = 0;
3724 break;
3725 case QEMU_OPTION_parallel:
3726 add_device_config(DEV_PARALLEL, optarg);
3727 default_parallel = 0;
3728 if (strncmp(optarg, "mon:", 4) == 0) {
3729 default_monitor = 0;
3731 break;
3732 case QEMU_OPTION_debugcon:
3733 add_device_config(DEV_DEBUGCON, optarg);
3734 break;
3735 case QEMU_OPTION_loadvm:
3736 loadvm = optarg;
3737 break;
3738 case QEMU_OPTION_full_screen:
3739 full_screen = 1;
3740 break;
3741 case QEMU_OPTION_no_frame:
3742 no_frame = 1;
3743 break;
3744 case QEMU_OPTION_alt_grab:
3745 alt_grab = 1;
3746 break;
3747 case QEMU_OPTION_ctrl_grab:
3748 ctrl_grab = 1;
3749 break;
3750 case QEMU_OPTION_no_quit:
3751 no_quit = 1;
3752 break;
3753 case QEMU_OPTION_sdl:
3754 #ifdef CONFIG_SDL
3755 display_type = DT_SDL;
3756 break;
3757 #else
3758 error_report("SDL support is disabled");
3759 exit(1);
3760 #endif
3761 case QEMU_OPTION_pidfile:
3762 pid_file = optarg;
3763 break;
3764 case QEMU_OPTION_win2k_hack:
3765 win2k_install_hack = 1;
3766 break;
3767 case QEMU_OPTION_rtc_td_hack: {
3768 static GlobalProperty slew_lost_ticks = {
3769 .driver = "mc146818rtc",
3770 .property = "lost_tick_policy",
3771 .value = "slew",
3774 qdev_prop_register_global(&slew_lost_ticks);
3775 break;
3777 case QEMU_OPTION_acpitable:
3778 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3779 optarg, true);
3780 if (!opts) {
3781 exit(1);
3783 acpi_table_add(opts, &error_fatal);
3784 break;
3785 case QEMU_OPTION_smbios:
3786 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3787 optarg, false);
3788 if (!opts) {
3789 exit(1);
3791 smbios_entry_add(opts, &error_fatal);
3792 break;
3793 case QEMU_OPTION_fwcfg:
3794 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3795 optarg, true);
3796 if (opts == NULL) {
3797 exit(1);
3799 break;
3800 case QEMU_OPTION_enable_kvm:
3801 olist = qemu_find_opts("machine");
3802 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3803 break;
3804 case QEMU_OPTION_enable_hax:
3805 olist = qemu_find_opts("machine");
3806 qemu_opts_parse_noisily(olist, "accel=hax", false);
3807 break;
3808 case QEMU_OPTION_M:
3809 case QEMU_OPTION_machine:
3810 olist = qemu_find_opts("machine");
3811 opts = qemu_opts_parse_noisily(olist, optarg, true);
3812 if (!opts) {
3813 exit(1);
3815 break;
3816 case QEMU_OPTION_no_kvm:
3817 olist = qemu_find_opts("machine");
3818 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3819 break;
3820 case QEMU_OPTION_no_kvm_pit: {
3821 warn_report("ignoring deprecated option");
3822 break;
3824 case QEMU_OPTION_no_kvm_pit_reinjection: {
3825 static GlobalProperty kvm_pit_lost_tick_policy = {
3826 .driver = "kvm-pit",
3827 .property = "lost_tick_policy",
3828 .value = "discard",
3831 warn_report("deprecated, replaced by "
3832 "-global kvm-pit.lost_tick_policy=discard");
3833 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3834 break;
3836 case QEMU_OPTION_accel:
3837 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3838 optarg, true);
3839 optarg = qemu_opt_get(accel_opts, "accel");
3840 if (!optarg || is_help_option(optarg)) {
3841 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3842 exit(0);
3844 opts = qemu_opts_create(qemu_find_opts("machine"), NULL,
3845 false, &error_abort);
3846 qemu_opt_set(opts, "accel", optarg, &error_abort);
3847 break;
3848 case QEMU_OPTION_usb:
3849 olist = qemu_find_opts("machine");
3850 qemu_opts_parse_noisily(olist, "usb=on", false);
3851 break;
3852 case QEMU_OPTION_usbdevice:
3853 error_report("'-usbdevice' is deprecated, please use "
3854 "'-device usb-...' instead");
3855 olist = qemu_find_opts("machine");
3856 qemu_opts_parse_noisily(olist, "usb=on", false);
3857 add_device_config(DEV_USB, optarg);
3858 break;
3859 case QEMU_OPTION_device:
3860 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3861 optarg, true)) {
3862 exit(1);
3864 break;
3865 case QEMU_OPTION_smp:
3866 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3867 optarg, true)) {
3868 exit(1);
3870 break;
3871 case QEMU_OPTION_vnc:
3872 vnc_parse(optarg, &error_fatal);
3873 break;
3874 case QEMU_OPTION_no_acpi:
3875 acpi_enabled = 0;
3876 break;
3877 case QEMU_OPTION_no_hpet:
3878 no_hpet = 1;
3879 break;
3880 case QEMU_OPTION_balloon:
3881 if (balloon_parse(optarg) < 0) {
3882 error_report("unknown -balloon argument %s", optarg);
3883 exit(1);
3885 break;
3886 case QEMU_OPTION_no_reboot:
3887 no_reboot = 1;
3888 break;
3889 case QEMU_OPTION_no_shutdown:
3890 no_shutdown = 1;
3891 break;
3892 case QEMU_OPTION_show_cursor:
3893 cursor_hide = 0;
3894 break;
3895 case QEMU_OPTION_uuid:
3896 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
3897 error_report("failed to parse UUID string: wrong format");
3898 exit(1);
3900 qemu_uuid_set = true;
3901 break;
3902 case QEMU_OPTION_option_rom:
3903 if (nb_option_roms >= MAX_OPTION_ROMS) {
3904 error_report("too many option ROMs");
3905 exit(1);
3907 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3908 optarg, true);
3909 if (!opts) {
3910 exit(1);
3912 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3913 option_rom[nb_option_roms].bootindex =
3914 qemu_opt_get_number(opts, "bootindex", -1);
3915 if (!option_rom[nb_option_roms].name) {
3916 error_report("Option ROM file is not specified");
3917 exit(1);
3919 nb_option_roms++;
3920 break;
3921 case QEMU_OPTION_semihosting:
3922 semihosting.enabled = true;
3923 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3924 break;
3925 case QEMU_OPTION_semihosting_config:
3926 semihosting.enabled = true;
3927 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3928 optarg, false);
3929 if (opts != NULL) {
3930 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3931 true);
3932 const char *target = qemu_opt_get(opts, "target");
3933 if (target != NULL) {
3934 if (strcmp("native", target) == 0) {
3935 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3936 } else if (strcmp("gdb", target) == 0) {
3937 semihosting.target = SEMIHOSTING_TARGET_GDB;
3938 } else if (strcmp("auto", target) == 0) {
3939 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3940 } else {
3941 error_report("unsupported semihosting-config %s",
3942 optarg);
3943 exit(1);
3945 } else {
3946 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3948 /* Set semihosting argument count and vector */
3949 qemu_opt_foreach(opts, add_semihosting_arg,
3950 &semihosting, NULL);
3951 } else {
3952 error_report("unsupported semihosting-config %s", optarg);
3953 exit(1);
3955 break;
3956 case QEMU_OPTION_tdf:
3957 warn_report("ignoring deprecated option");
3958 break;
3959 case QEMU_OPTION_name:
3960 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3961 optarg, true);
3962 if (!opts) {
3963 exit(1);
3965 break;
3966 case QEMU_OPTION_prom_env:
3967 if (nb_prom_envs >= MAX_PROM_ENVS) {
3968 error_report("too many prom variables");
3969 exit(1);
3971 prom_envs[nb_prom_envs] = optarg;
3972 nb_prom_envs++;
3973 break;
3974 case QEMU_OPTION_old_param:
3975 old_param = 1;
3976 break;
3977 case QEMU_OPTION_clock:
3978 /* Clock options no longer exist. Keep this option for
3979 * backward compatibility.
3981 break;
3982 case QEMU_OPTION_startdate:
3983 configure_rtc_date_offset(optarg, 1);
3984 break;
3985 case QEMU_OPTION_rtc:
3986 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3987 false);
3988 if (!opts) {
3989 exit(1);
3991 configure_rtc(opts);
3992 break;
3993 case QEMU_OPTION_tb_size:
3994 #ifndef CONFIG_TCG
3995 error_report("TCG is disabled");
3996 exit(1);
3997 #endif
3998 if (qemu_strtoul(optarg, NULL, 0, &tcg_tb_size) < 0) {
3999 error_report("Invalid argument to -tb-size");
4000 exit(1);
4002 break;
4003 case QEMU_OPTION_icount:
4004 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
4005 optarg, true);
4006 if (!icount_opts) {
4007 exit(1);
4009 break;
4010 case QEMU_OPTION_incoming:
4011 if (!incoming) {
4012 runstate_set(RUN_STATE_INMIGRATE);
4014 incoming = optarg;
4015 break;
4016 case QEMU_OPTION_only_migratable:
4018 * TODO: we can remove this option one day, and we
4019 * should all use:
4021 * "-global migration.only-migratable=true"
4023 qemu_global_option("migration.only-migratable=true");
4024 break;
4025 case QEMU_OPTION_nodefaults:
4026 has_defaults = 0;
4027 break;
4028 case QEMU_OPTION_xen_domid:
4029 if (!(xen_available())) {
4030 error_report("Option not supported for this target");
4031 exit(1);
4033 xen_domid = atoi(optarg);
4034 break;
4035 case QEMU_OPTION_xen_create:
4036 if (!(xen_available())) {
4037 error_report("Option not supported for this target");
4038 exit(1);
4040 xen_mode = XEN_CREATE;
4041 break;
4042 case QEMU_OPTION_xen_attach:
4043 if (!(xen_available())) {
4044 error_report("Option not supported for this target");
4045 exit(1);
4047 xen_mode = XEN_ATTACH;
4048 break;
4049 case QEMU_OPTION_xen_domid_restrict:
4050 if (!(xen_available())) {
4051 error_report("Option not supported for this target");
4052 exit(1);
4054 xen_domid_restrict = true;
4055 break;
4056 case QEMU_OPTION_trace:
4057 g_free(trace_file);
4058 trace_file = trace_opt_parse(optarg);
4059 break;
4060 case QEMU_OPTION_readconfig:
4062 int ret = qemu_read_config_file(optarg);
4063 if (ret < 0) {
4064 error_report("read config %s: %s", optarg,
4065 strerror(-ret));
4066 exit(1);
4068 break;
4070 case QEMU_OPTION_spice:
4071 olist = qemu_find_opts("spice");
4072 if (!olist) {
4073 error_report("spice support is disabled");
4074 exit(1);
4076 opts = qemu_opts_parse_noisily(olist, optarg, false);
4077 if (!opts) {
4078 exit(1);
4080 display_remote++;
4081 break;
4082 case QEMU_OPTION_writeconfig:
4084 FILE *fp;
4085 if (strcmp(optarg, "-") == 0) {
4086 fp = stdout;
4087 } else {
4088 fp = fopen(optarg, "w");
4089 if (fp == NULL) {
4090 error_report("open %s: %s", optarg,
4091 strerror(errno));
4092 exit(1);
4095 qemu_config_write(fp);
4096 if (fp != stdout) {
4097 fclose(fp);
4099 break;
4101 case QEMU_OPTION_qtest:
4102 qtest_chrdev = optarg;
4103 break;
4104 case QEMU_OPTION_qtest_log:
4105 qtest_log = optarg;
4106 break;
4107 case QEMU_OPTION_sandbox:
4108 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4109 optarg, true);
4110 if (!opts) {
4111 exit(1);
4113 break;
4114 case QEMU_OPTION_add_fd:
4115 #ifndef _WIN32
4116 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4117 optarg, false);
4118 if (!opts) {
4119 exit(1);
4121 #else
4122 error_report("File descriptor passing is disabled on this "
4123 "platform");
4124 exit(1);
4125 #endif
4126 break;
4127 case QEMU_OPTION_object:
4128 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4129 optarg, true);
4130 if (!opts) {
4131 exit(1);
4133 break;
4134 case QEMU_OPTION_realtime:
4135 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4136 optarg, false);
4137 if (!opts) {
4138 exit(1);
4140 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4141 break;
4142 case QEMU_OPTION_msg:
4143 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4144 false);
4145 if (!opts) {
4146 exit(1);
4148 configure_msg(opts);
4149 break;
4150 case QEMU_OPTION_dump_vmstate:
4151 if (vmstate_dump_file) {
4152 error_report("only one '-dump-vmstate' "
4153 "option may be given");
4154 exit(1);
4156 vmstate_dump_file = fopen(optarg, "w");
4157 if (vmstate_dump_file == NULL) {
4158 error_report("open %s: %s", optarg, strerror(errno));
4159 exit(1);
4161 break;
4162 default:
4163 os_parse_cmd_args(popt->index, optarg);
4168 * Clear error location left behind by the loop.
4169 * Best done right after the loop. Do not insert code here!
4171 loc_set_none();
4173 replay_configure(icount_opts);
4175 machine_class = select_machine();
4177 set_memory_options(&ram_slots, &maxram_size, machine_class);
4179 os_daemonize();
4180 rcu_disable_atfork();
4182 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4183 error_report("could not acquire pid file: %s", strerror(errno));
4184 exit(1);
4187 if (qemu_init_main_loop(&main_loop_err)) {
4188 error_report_err(main_loop_err);
4189 exit(1);
4192 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4193 parse_sandbox, NULL, NULL)) {
4194 exit(1);
4197 if (qemu_opts_foreach(qemu_find_opts("name"),
4198 parse_name, NULL, NULL)) {
4199 exit(1);
4202 #ifndef _WIN32
4203 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4204 parse_add_fd, NULL, NULL)) {
4205 exit(1);
4208 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4209 cleanup_add_fd, NULL, NULL)) {
4210 exit(1);
4212 #endif
4214 current_machine = MACHINE(object_new(object_class_get_name(
4215 OBJECT_CLASS(machine_class))));
4216 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4217 exit(0);
4219 object_property_add_child(object_get_root(), "machine",
4220 OBJECT(current_machine), &error_abort);
4222 if (machine_class->minimum_page_bits) {
4223 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4224 /* This would be a board error: specifying a minimum smaller than
4225 * a target's compile-time fixed setting.
4227 g_assert_not_reached();
4231 cpu_exec_init_all();
4233 if (machine_class->hw_version) {
4234 qemu_set_hw_version(machine_class->hw_version);
4237 if (cpu_model && is_help_option(cpu_model)) {
4238 list_cpus(stdout, &fprintf, cpu_model);
4239 exit(0);
4242 if (!trace_init_backends()) {
4243 exit(1);
4245 trace_init_file(trace_file);
4247 /* Open the logfile at this point and set the log mask if necessary.
4249 if (log_file) {
4250 qemu_set_log_filename(log_file, &error_fatal);
4253 if (log_mask) {
4254 int mask;
4255 mask = qemu_str_to_log_mask(log_mask);
4256 if (!mask) {
4257 qemu_print_log_usage(stdout);
4258 exit(1);
4260 qemu_set_log(mask);
4261 } else {
4262 qemu_set_log(0);
4265 /* add configured firmware directories */
4266 dirs = g_strsplit(CONFIG_QEMU_FIRMWAREPATH, G_SEARCHPATH_SEPARATOR_S, 0);
4267 for (i = 0; dirs[i] != NULL; i++) {
4268 qemu_add_data_dir(dirs[i]);
4271 /* try to find datadir relative to the executable path */
4272 qemu_add_data_dir(os_find_datadir());
4274 /* add the datadir specified when building */
4275 qemu_add_data_dir(CONFIG_QEMU_DATADIR);
4277 /* -L help lists the data directories and exits. */
4278 if (list_data_dirs) {
4279 for (i = 0; i < data_dir_idx; i++) {
4280 printf("%s\n", data_dir[i]);
4282 exit(0);
4285 /* machine_class: default to UP */
4286 machine_class->max_cpus = machine_class->max_cpus ?: 1;
4287 machine_class->min_cpus = machine_class->min_cpus ?: 1;
4288 machine_class->default_cpus = machine_class->default_cpus ?: 1;
4290 /* default to machine_class->default_cpus */
4291 smp_cpus = machine_class->default_cpus;
4292 max_cpus = machine_class->default_cpus;
4294 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4296 /* sanity-check smp_cpus and max_cpus against machine_class */
4297 if (smp_cpus < machine_class->min_cpus) {
4298 error_report("Invalid SMP CPUs %d. The min CPUs "
4299 "supported by machine '%s' is %d", smp_cpus,
4300 machine_class->name, machine_class->min_cpus);
4301 exit(1);
4303 if (max_cpus > machine_class->max_cpus) {
4304 error_report("Invalid SMP CPUs %d. The max CPUs "
4305 "supported by machine '%s' is %d", max_cpus,
4306 machine_class->name, machine_class->max_cpus);
4307 exit(1);
4311 * Get the default machine options from the machine if it is not already
4312 * specified either by the configuration file or by the command line.
4314 if (machine_class->default_machine_opts) {
4315 qemu_opts_set_defaults(qemu_find_opts("machine"),
4316 machine_class->default_machine_opts, 0);
4319 qemu_opts_foreach(qemu_find_opts("device"),
4320 default_driver_check, NULL, NULL);
4321 qemu_opts_foreach(qemu_find_opts("global"),
4322 default_driver_check, NULL, NULL);
4324 if (!vga_model && !default_vga) {
4325 vga_interface_type = VGA_DEVICE;
4327 if (!has_defaults || machine_class->no_serial) {
4328 default_serial = 0;
4330 if (!has_defaults || machine_class->no_parallel) {
4331 default_parallel = 0;
4333 if (!has_defaults || !machine_class->use_virtcon) {
4334 default_virtcon = 0;
4336 if (!has_defaults || !machine_class->use_sclp) {
4337 default_sclp = 0;
4339 if (!has_defaults || machine_class->no_floppy) {
4340 default_floppy = 0;
4342 if (!has_defaults || machine_class->no_cdrom) {
4343 default_cdrom = 0;
4345 if (!has_defaults || machine_class->no_sdcard) {
4346 default_sdcard = 0;
4348 if (!has_defaults) {
4349 default_monitor = 0;
4350 default_net = 0;
4351 default_vga = 0;
4354 if (is_daemonized()) {
4355 /* According to documentation and historically, -nographic redirects
4356 * serial port, parallel port and monitor to stdio, which does not work
4357 * with -daemonize. We can redirect these to null instead, but since
4358 * -nographic is legacy, let's just error out.
4359 * We disallow -nographic only if all other ports are not redirected
4360 * explicitly, to not break existing legacy setups which uses
4361 * -nographic _and_ redirects all ports explicitly - this is valid
4362 * usage, -nographic is just a no-op in this case.
4364 if (nographic
4365 && (default_parallel || default_serial
4366 || default_monitor || default_virtcon)) {
4367 error_report("-nographic cannot be used with -daemonize");
4368 exit(1);
4370 #ifdef CONFIG_CURSES
4371 if (display_type == DT_CURSES) {
4372 error_report("curses display cannot be used with -daemonize");
4373 exit(1);
4375 #endif
4378 if (nographic) {
4379 if (default_parallel)
4380 add_device_config(DEV_PARALLEL, "null");
4381 if (default_serial && default_monitor) {
4382 add_device_config(DEV_SERIAL, "mon:stdio");
4383 } else if (default_virtcon && default_monitor) {
4384 add_device_config(DEV_VIRTCON, "mon:stdio");
4385 } else if (default_sclp && default_monitor) {
4386 add_device_config(DEV_SCLP, "mon:stdio");
4387 } else {
4388 if (default_serial)
4389 add_device_config(DEV_SERIAL, "stdio");
4390 if (default_virtcon)
4391 add_device_config(DEV_VIRTCON, "stdio");
4392 if (default_sclp) {
4393 add_device_config(DEV_SCLP, "stdio");
4395 if (default_monitor)
4396 monitor_parse("stdio", "readline", false);
4398 } else {
4399 if (default_serial)
4400 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4401 if (default_parallel)
4402 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4403 if (default_monitor)
4404 monitor_parse("vc:80Cx24C", "readline", false);
4405 if (default_virtcon)
4406 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4407 if (default_sclp) {
4408 add_device_config(DEV_SCLP, "vc:80Cx24C");
4412 #if defined(CONFIG_VNC)
4413 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4414 display_remote++;
4416 #endif
4417 if (display_type == DT_DEFAULT && !display_remote) {
4418 #if defined(CONFIG_GTK)
4419 display_type = DT_GTK;
4420 #elif defined(CONFIG_SDL)
4421 display_type = DT_SDL;
4422 #elif defined(CONFIG_COCOA)
4423 display_type = DT_COCOA;
4424 #elif defined(CONFIG_VNC)
4425 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4426 #else
4427 display_type = DT_NONE;
4428 #endif
4431 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4432 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4433 "for SDL, ignoring option");
4435 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4436 error_report("-no-quit is only valid for GTK and SDL, "
4437 "ignoring option");
4440 if (display_type == DT_GTK) {
4441 early_gtk_display_init(request_opengl);
4444 if (display_type == DT_SDL) {
4445 sdl_display_early_init(request_opengl);
4448 qemu_console_early_init();
4450 if (request_opengl == 1 && display_opengl == 0) {
4451 #if defined(CONFIG_OPENGL)
4452 error_report("OpenGL is not supported by the display");
4453 #else
4454 error_report("OpenGL support is disabled");
4455 #endif
4456 exit(1);
4459 page_size_init();
4460 socket_init();
4462 if (qemu_opts_foreach(qemu_find_opts("object"),
4463 user_creatable_add_opts_foreach,
4464 object_create_initial, NULL)) {
4465 exit(1);
4468 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4469 chardev_init_func, NULL, NULL)) {
4470 exit(1);
4473 #ifdef CONFIG_VIRTFS
4474 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4475 fsdev_init_func, NULL, NULL)) {
4476 exit(1);
4478 #endif
4480 if (qemu_opts_foreach(qemu_find_opts("device"),
4481 device_help_func, NULL, NULL)) {
4482 exit(0);
4485 machine_opts = qemu_get_machine_opts();
4486 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4487 NULL)) {
4488 object_unref(OBJECT(current_machine));
4489 exit(1);
4492 configure_accelerator(current_machine);
4495 * Register all the global properties, including accel properties,
4496 * machine properties, and user-specified ones.
4498 register_global_properties(current_machine);
4501 * Migration object can only be created after global properties
4502 * are applied correctly.
4504 migration_object_init();
4506 if (qtest_chrdev) {
4507 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4510 machine_opts = qemu_get_machine_opts();
4511 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4512 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4513 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4514 bios_name = qemu_opt_get(machine_opts, "firmware");
4516 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4517 if (opts) {
4518 boot_order = qemu_opt_get(opts, "order");
4519 if (boot_order) {
4520 validate_bootdevices(boot_order, &error_fatal);
4523 boot_once = qemu_opt_get(opts, "once");
4524 if (boot_once) {
4525 validate_bootdevices(boot_once, &error_fatal);
4528 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4529 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4532 if (!boot_order) {
4533 boot_order = machine_class->default_boot_order;
4536 if (!kernel_cmdline) {
4537 kernel_cmdline = "";
4538 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4541 linux_boot = (kernel_filename != NULL);
4543 if (!linux_boot && *kernel_cmdline != '\0') {
4544 error_report("-append only allowed with -kernel option");
4545 exit(1);
4548 if (!linux_boot && initrd_filename != NULL) {
4549 error_report("-initrd only allowed with -kernel option");
4550 exit(1);
4553 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4554 /* fall back to the -kernel/-append */
4555 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4558 os_set_line_buffering();
4560 /* spice needs the timers to be initialized by this point */
4561 qemu_spice_init();
4563 cpu_ticks_init();
4564 if (icount_opts) {
4565 if (!tcg_enabled()) {
4566 error_report("-icount is not allowed with hardware virtualization");
4567 exit(1);
4569 configure_icount(icount_opts, &error_abort);
4570 qemu_opts_del(icount_opts);
4573 if (tcg_enabled()) {
4574 qemu_tcg_configure(accel_opts, &error_fatal);
4577 if (default_net) {
4578 QemuOptsList *net = qemu_find_opts("net");
4579 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4580 #ifdef CONFIG_SLIRP
4581 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4582 #endif
4585 colo_info_init();
4587 if (net_init_clients() < 0) {
4588 exit(1);
4591 if (qemu_opts_foreach(qemu_find_opts("object"),
4592 user_creatable_add_opts_foreach,
4593 object_create_delayed, NULL)) {
4594 exit(1);
4597 if (tpm_init() < 0) {
4598 exit(1);
4601 /* init the bluetooth world */
4602 if (foreach_device_config(DEV_BT, bt_parse))
4603 exit(1);
4605 if (!xen_enabled()) {
4606 /* On 32-bit hosts, QEMU is limited by virtual address space */
4607 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4608 error_report("at most 2047 MB RAM can be simulated");
4609 exit(1);
4613 blk_mig_init();
4614 ram_mig_init();
4616 /* If the currently selected machine wishes to override the units-per-bus
4617 * property of its default HBA interface type, do so now. */
4618 if (machine_class->units_per_default_bus) {
4619 override_max_devs(machine_class->block_default_type,
4620 machine_class->units_per_default_bus);
4623 /* open the virtual block devices */
4624 while (!QSIMPLEQ_EMPTY(&bdo_queue)) {
4625 BlockdevOptions_queue *bdo = QSIMPLEQ_FIRST(&bdo_queue);
4627 QSIMPLEQ_REMOVE_HEAD(&bdo_queue, entry);
4628 loc_push_restore(&bdo->loc);
4629 qmp_blockdev_add(bdo->bdo, &error_fatal);
4630 loc_pop(&bdo->loc);
4631 qapi_free_BlockdevOptions(bdo->bdo);
4632 g_free(bdo);
4634 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4635 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4636 NULL, NULL);
4638 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4639 &machine_class->block_default_type, NULL)) {
4640 exit(1);
4643 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4644 CDROM_OPTS);
4645 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4646 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4648 if (qemu_opts_foreach(qemu_find_opts("mon"),
4649 mon_init_func, NULL, NULL)) {
4650 exit(1);
4653 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4654 exit(1);
4655 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4656 exit(1);
4657 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4658 exit(1);
4659 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4660 exit(1);
4662 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4663 exit(1);
4665 /* If no default VGA is requested, the default is "none". */
4666 if (default_vga) {
4667 if (machine_class->default_display) {
4668 vga_model = machine_class->default_display;
4669 } else if (vga_interface_available(VGA_CIRRUS)) {
4670 vga_model = "cirrus";
4671 } else if (vga_interface_available(VGA_STD)) {
4672 vga_model = "std";
4675 if (vga_model) {
4676 select_vgahw(vga_model);
4679 if (watchdog) {
4680 i = select_watchdog(watchdog);
4681 if (i > 0)
4682 exit (i == 1 ? 1 : 0);
4685 /* This checkpoint is required by replay to separate prior clock
4686 reading from the other reads, because timer polling functions query
4687 clock values from the log. */
4688 replay_checkpoint(CHECKPOINT_INIT);
4689 qdev_machine_init();
4691 current_machine->ram_size = ram_size;
4692 current_machine->maxram_size = maxram_size;
4693 current_machine->ram_slots = ram_slots;
4694 current_machine->boot_order = boot_order;
4695 current_machine->cpu_model = cpu_model;
4697 parse_numa_opts(current_machine);
4699 /* parse features once if machine provides default cpu_type */
4700 if (machine_class->default_cpu_type) {
4701 current_machine->cpu_type = machine_class->default_cpu_type;
4702 if (cpu_model) {
4703 current_machine->cpu_type =
4704 cpu_parse_cpu_model(machine_class->default_cpu_type, cpu_model);
4708 machine_run_board_init(current_machine);
4710 realtime_init();
4712 soundhw_init();
4714 if (hax_enabled()) {
4715 hax_sync_vcpus();
4718 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4719 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4720 exit(1);
4723 /* init USB devices */
4724 if (machine_usb(current_machine)) {
4725 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4726 exit(1);
4729 /* Check if IGD GFX passthrough. */
4730 igd_gfx_passthru();
4732 /* init generic devices */
4733 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4734 if (qemu_opts_foreach(qemu_find_opts("device"),
4735 device_init_func, NULL, NULL)) {
4736 exit(1);
4739 cpu_synchronize_all_post_init();
4741 rom_reset_order_override();
4744 * Create frontends for -drive if=scsi leftovers.
4745 * Normally, frontends for -drive get created by machine
4746 * initialization for onboard SCSI HBAs. However, we create a few
4747 * more ever since SCSI qdevification, but this is pretty much an
4748 * implementation accident, and deprecated.
4750 scsi_legacy_handle_cmdline();
4752 /* Did we create any drives that we failed to create a device for? */
4753 drive_check_orphaned();
4755 /* Don't warn about the default network setup that you get if
4756 * no command line -net or -netdev options are specified. There
4757 * are two cases that we would otherwise complain about:
4758 * (1) board doesn't support a NIC but the implicit "-net nic"
4759 * requested one
4760 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4761 * sets up a nic that isn't connected to anything.
4763 if (!default_net) {
4764 net_check_clients();
4768 if (boot_once) {
4769 qemu_boot_set(boot_once, &error_fatal);
4770 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4773 ds = init_displaystate();
4775 /* init local displays */
4776 switch (display_type) {
4777 case DT_CURSES:
4778 curses_display_init(ds, full_screen);
4779 break;
4780 case DT_SDL:
4781 sdl_display_init(ds, full_screen, no_frame);
4782 break;
4783 case DT_COCOA:
4784 cocoa_display_init(ds, full_screen);
4785 break;
4786 case DT_GTK:
4787 gtk_display_init(ds, full_screen, grab_on_hover);
4788 break;
4789 default:
4790 break;
4793 /* must be after terminal init, SDL library changes signal handlers */
4794 os_setup_signal_handling();
4796 /* init remote displays */
4797 #ifdef CONFIG_VNC
4798 qemu_opts_foreach(qemu_find_opts("vnc"),
4799 vnc_init_func, NULL, NULL);
4800 #endif
4802 if (using_spice) {
4803 qemu_spice_display_init();
4806 #ifdef CONFIG_OPENGL_DMABUF
4807 if (display_type == DT_EGL) {
4808 egl_headless_init();
4810 #endif
4812 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4813 exit(1);
4816 qdev_machine_creation_done();
4818 /* TODO: once all bus devices are qdevified, this should be done
4819 * when bus is created by qdev.c */
4820 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4821 qemu_run_machine_init_done_notifiers();
4823 if (rom_check_and_register_reset() != 0) {
4824 error_report("rom check and register reset failed");
4825 exit(1);
4828 replay_start();
4830 /* This checkpoint is required by replay to separate prior clock
4831 reading from the other reads, because timer polling functions query
4832 clock values from the log. */
4833 replay_checkpoint(CHECKPOINT_RESET);
4834 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
4835 register_global_state();
4836 if (replay_mode != REPLAY_MODE_NONE) {
4837 replay_vmstate_init();
4838 } else if (loadvm) {
4839 Error *local_err = NULL;
4840 if (load_snapshot(loadvm, &local_err) < 0) {
4841 error_report_err(local_err);
4842 autostart = 0;
4846 qdev_prop_check_globals();
4847 if (vmstate_dump_file) {
4848 /* dump and exit */
4849 dump_vmstate_json_to_file(vmstate_dump_file);
4850 return 0;
4853 if (incoming) {
4854 Error *local_err = NULL;
4855 qemu_start_incoming_migration(incoming, &local_err);
4856 if (local_err) {
4857 error_reportf_err(local_err, "-incoming %s: ", incoming);
4858 exit(1);
4860 } else if (autostart) {
4861 vm_start();
4864 os_setup_post();
4866 main_loop();
4867 replay_disable_events();
4868 iothread_stop_all();
4870 pause_all_vcpus();
4871 bdrv_close_all();
4872 res_free();
4874 /* vhost-user must be cleaned up before chardevs. */
4875 tpm_cleanup();
4876 net_cleanup();
4877 audio_cleanup();
4878 monitor_cleanup();
4879 qemu_chr_cleanup();
4880 user_creatable_cleanup();
4881 /* TODO: unref root container, check all devices are ok */
4883 return 0;