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
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
27 #include "qapi/error.h"
28 #include "qemu-version.h"
29 #include "qemu/cutils.h"
30 #include "qemu/help_option.h"
31 #include "qemu/uuid.h"
32 #include "sysemu/seccomp.h"
35 #if defined(__APPLE__) || defined(main)
36 #define SDL_MAIN_HANDLED
38 #if SDL_MAJOR_VERSION == 1
39 int qemu_main(int argc
, char **argv
);
40 int main(int argc
, char **argv
)
42 return qemu_main(argc
, argv
);
45 #define main qemu_main
46 #elif SDL_MAJOR_VERSION == 2
49 #error undefined SDL version
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
60 #include "qemu/error-report.h"
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/isa/isa.h"
67 #include "hw/scsi/scsi.h"
68 #include "hw/display/vga.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/smbios/smbios.h"
72 #include "hw/acpi/acpi.h"
73 #include "hw/xen/xen.h"
75 #include "hw/loader.h"
76 #include "monitor/qdev.h"
77 #include "sysemu/bt.h"
79 #include "net/slirp.h"
80 #include "monitor/monitor.h"
81 #include "ui/console.h"
83 #include "sysemu/sysemu.h"
84 #include "sysemu/numa.h"
85 #include "exec/gdbstub.h"
86 #include "qemu/timer.h"
87 #include "chardev/char.h"
88 #include "qemu/bitmap.h"
90 #include "sysemu/blockdev.h"
91 #include "hw/block/block.h"
92 #include "migration/misc.h"
93 #include "migration/snapshot.h"
94 #include "migration/global_state.h"
95 #include "sysemu/tpm.h"
96 #include "sysemu/dma.h"
97 #include "hw/audio/soundhw.h"
98 #include "audio/audio.h"
99 #include "sysemu/cpus.h"
100 #include "migration/colo.h"
101 #include "migration/postcopy-ram.h"
102 #include "sysemu/kvm.h"
103 #include "sysemu/hax.h"
104 #include "qapi/qobject-input-visitor.h"
105 #include "qemu/option.h"
106 #include "qemu/config-file.h"
107 #include "qemu-options.h"
108 #include "qemu/main-loop.h"
110 #include "fsdev/qemu-fsdev.h"
112 #include "sysemu/qtest.h"
114 #include "disas/disas.h"
117 #include "slirp/libslirp.h"
119 #include "trace-root.h"
120 #include "trace/control.h"
121 #include "qemu/queue.h"
122 #include "sysemu/arch_init.h"
124 #include "ui/qemu-spice.h"
125 #include "qapi/string-input-visitor.h"
126 #include "qapi/opts-visitor.h"
127 #include "qapi/clone-visitor.h"
128 #include "qom/object_interfaces.h"
129 #include "exec/semihost.h"
130 #include "crypto/init.h"
131 #include "sysemu/replay.h"
132 #include "qapi/qapi-events-run-state.h"
133 #include "qapi/qapi-visit-block-core.h"
134 #include "qapi/qapi-visit-ui.h"
135 #include "qapi/qapi-commands-block-core.h"
136 #include "qapi/qapi-commands-misc.h"
137 #include "qapi/qapi-commands-run-state.h"
138 #include "qapi/qmp/qerror.h"
139 #include "sysemu/iothread.h"
141 #define MAX_VIRTIO_CONSOLES 1
143 static const char *data_dir
[16];
144 static int data_dir_idx
;
145 const char *bios_name
= NULL
;
146 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
148 const char* keyboard_layout
= NULL
;
150 const char *mem_path
= NULL
;
151 int mem_prealloc
= 0; /* force preallocation of physical target memory */
152 bool enable_mlock
= false;
153 bool enable_cpu_pm
= false;
155 NICInfo nd_table
[MAX_NICS
];
161 } rtc_base_type
= RTC_BASE_UTC
;
162 static time_t rtc_ref_start_datetime
;
163 static int rtc_realtime_clock_offset
; /* used only with QEMU_CLOCK_REALTIME */
164 static int rtc_host_datetime_offset
= -1; /* valid & used only with
166 QEMUClockType rtc_clock
;
167 int vga_interface_type
= VGA_NONE
;
168 static DisplayOptions dpy
;
170 static int num_serial_hds
;
171 static Chardev
**serial_hds
;
172 Chardev
*parallel_hds
[MAX_PARALLEL_PORTS
];
173 Chardev
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
174 int win2k_install_hack
= 0;
177 unsigned int max_cpus
;
180 int acpi_enabled
= 1;
183 static int no_reboot
;
186 int graphic_rotate
= 0;
187 const char *watchdog
;
188 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
191 const char *qemu_name
;
194 unsigned int nb_prom_envs
= 0;
195 const char *prom_envs
[MAX_PROM_ENVS
];
198 uint8_t *boot_splash_filedata
;
199 size_t boot_splash_filedata_size
;
200 uint8_t qemu_extra_params_fw
[2];
202 int icount_align_option
;
204 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
205 * little-endian "wire format" described in the SMBIOS 2.6 specification.
210 /* Trace unassigned memory or i/o accesses. */
211 bool trace_unassigned
;
213 static NotifierList exit_notifiers
=
214 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
216 static NotifierList machine_init_done_notifiers
=
217 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
221 enum xen_mode xen_mode
= XEN_EMULATE
;
222 bool xen_domid_restrict
;
224 static int has_defaults
= 1;
225 static int default_serial
= 1;
226 static int default_parallel
= 1;
227 static int default_virtcon
= 1;
228 static int default_monitor
= 1;
229 static int default_floppy
= 1;
230 static int default_cdrom
= 1;
231 static int default_sdcard
= 1;
232 static int default_vga
= 1;
233 static int default_net
= 1;
239 { .driver
= "isa-serial", .flag
= &default_serial
},
240 { .driver
= "isa-parallel", .flag
= &default_parallel
},
241 { .driver
= "isa-fdc", .flag
= &default_floppy
},
242 { .driver
= "floppy", .flag
= &default_floppy
},
243 { .driver
= "ide-cd", .flag
= &default_cdrom
},
244 { .driver
= "ide-hd", .flag
= &default_cdrom
},
245 { .driver
= "ide-drive", .flag
= &default_cdrom
},
246 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
247 { .driver
= "scsi-hd", .flag
= &default_cdrom
},
248 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
249 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
250 { .driver
= "VGA", .flag
= &default_vga
},
251 { .driver
= "isa-vga", .flag
= &default_vga
},
252 { .driver
= "cirrus-vga", .flag
= &default_vga
},
253 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
254 { .driver
= "vmware-svga", .flag
= &default_vga
},
255 { .driver
= "qxl-vga", .flag
= &default_vga
},
256 { .driver
= "virtio-vga", .flag
= &default_vga
},
259 static QemuOptsList qemu_rtc_opts
= {
261 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
266 .type
= QEMU_OPT_STRING
,
269 .type
= QEMU_OPT_STRING
,
272 .type
= QEMU_OPT_STRING
,
274 { /* end of list */ }
278 static QemuOptsList qemu_option_rom_opts
= {
279 .name
= "option-rom",
280 .implied_opt_name
= "romfile",
281 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
285 .type
= QEMU_OPT_NUMBER
,
288 .type
= QEMU_OPT_STRING
,
290 { /* end of list */ }
294 static QemuOptsList qemu_machine_opts
= {
296 .implied_opt_name
= "type",
298 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
301 * no elements => accept any
302 * sanity checking will happen later
303 * when setting machine properties
309 static QemuOptsList qemu_accel_opts
= {
311 .implied_opt_name
= "accel",
312 .head
= QTAILQ_HEAD_INITIALIZER(qemu_accel_opts
.head
),
317 .type
= QEMU_OPT_STRING
,
318 .help
= "Select the type of accelerator",
322 .type
= QEMU_OPT_STRING
,
323 .help
= "Enable/disable multi-threaded TCG",
325 { /* end of list */ }
329 static QemuOptsList qemu_boot_opts
= {
331 .implied_opt_name
= "order",
333 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
337 .type
= QEMU_OPT_STRING
,
340 .type
= QEMU_OPT_STRING
,
343 .type
= QEMU_OPT_BOOL
,
346 .type
= QEMU_OPT_STRING
,
348 .name
= "splash-time",
349 .type
= QEMU_OPT_STRING
,
351 .name
= "reboot-timeout",
352 .type
= QEMU_OPT_STRING
,
355 .type
= QEMU_OPT_BOOL
,
361 static QemuOptsList qemu_add_fd_opts
= {
363 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
367 .type
= QEMU_OPT_NUMBER
,
368 .help
= "file descriptor of which a duplicate is added to fd set",
371 .type
= QEMU_OPT_NUMBER
,
372 .help
= "ID of the fd set to add fd to",
375 .type
= QEMU_OPT_STRING
,
376 .help
= "free-form string used to describe fd",
378 { /* end of list */ }
382 static QemuOptsList qemu_object_opts
= {
384 .implied_opt_name
= "qom-type",
385 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
391 static QemuOptsList qemu_tpmdev_opts
= {
393 .implied_opt_name
= "type",
394 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
396 /* options are defined in the TPM backends */
397 { /* end of list */ }
401 static QemuOptsList qemu_realtime_opts
= {
403 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
407 .type
= QEMU_OPT_BOOL
,
409 { /* end of list */ }
413 static QemuOptsList qemu_overcommit_opts
= {
414 .name
= "overcommit",
415 .head
= QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts
.head
),
419 .type
= QEMU_OPT_BOOL
,
423 .type
= QEMU_OPT_BOOL
,
425 { /* end of list */ }
429 static QemuOptsList qemu_msg_opts
= {
431 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
435 .type
= QEMU_OPT_BOOL
,
437 { /* end of list */ }
441 static QemuOptsList qemu_name_opts
= {
443 .implied_opt_name
= "guest",
445 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
449 .type
= QEMU_OPT_STRING
,
450 .help
= "Sets the name of the guest.\n"
451 "This name will be displayed in the SDL window caption.\n"
452 "The name will also be used for the VNC server",
455 .type
= QEMU_OPT_STRING
,
456 .help
= "Sets the name of the QEMU process, as shown in top etc",
458 .name
= "debug-threads",
459 .type
= QEMU_OPT_BOOL
,
460 .help
= "When enabled, name the individual threads; defaults off.\n"
461 "NOTE: The thread names are for debugging and not a\n"
464 { /* End of list */ }
468 static QemuOptsList qemu_mem_opts
= {
470 .implied_opt_name
= "size",
471 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
476 .type
= QEMU_OPT_SIZE
,
480 .type
= QEMU_OPT_NUMBER
,
484 .type
= QEMU_OPT_SIZE
,
486 { /* end of list */ }
490 static QemuOptsList qemu_icount_opts
= {
492 .implied_opt_name
= "shift",
494 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
498 .type
= QEMU_OPT_STRING
,
501 .type
= QEMU_OPT_BOOL
,
504 .type
= QEMU_OPT_BOOL
,
507 .type
= QEMU_OPT_STRING
,
510 .type
= QEMU_OPT_STRING
,
512 .name
= "rrsnapshot",
513 .type
= QEMU_OPT_STRING
,
515 { /* end of list */ }
519 static QemuOptsList qemu_semihosting_config_opts
= {
520 .name
= "semihosting-config",
521 .implied_opt_name
= "enable",
522 .head
= QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts
.head
),
526 .type
= QEMU_OPT_BOOL
,
529 .type
= QEMU_OPT_STRING
,
532 .type
= QEMU_OPT_STRING
,
534 { /* end of list */ }
538 static QemuOptsList qemu_fw_cfg_opts
= {
540 .implied_opt_name
= "name",
541 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts
.head
),
545 .type
= QEMU_OPT_STRING
,
546 .help
= "Sets the fw_cfg name of the blob to be inserted",
549 .type
= QEMU_OPT_STRING
,
550 .help
= "Sets the name of the file from which "
551 "the fw_cfg blob will be loaded",
554 .type
= QEMU_OPT_STRING
,
555 .help
= "Sets content of the blob to be inserted from a string",
557 { /* end of list */ }
562 * Get machine options
564 * Returns: machine options (never null).
566 QemuOpts
*qemu_get_machine_opts(void)
568 return qemu_find_opts_singleton("machine");
571 const char *qemu_get_vm_name(void)
576 static void res_free(void)
578 g_free(boot_splash_filedata
);
579 boot_splash_filedata
= NULL
;
582 static int default_driver_check(void *opaque
, QemuOpts
*opts
, Error
**errp
)
584 const char *driver
= qemu_opt_get(opts
, "driver");
589 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
590 if (strcmp(default_list
[i
].driver
, driver
) != 0)
592 *(default_list
[i
].flag
) = 0;
597 /***********************************************************/
600 static RunState current_run_state
= RUN_STATE_PRECONFIG
;
602 /* We use RUN_STATE__MAX but any invalid value will do */
603 static RunState vmstop_requested
= RUN_STATE__MAX
;
604 static QemuMutex vmstop_lock
;
609 } RunStateTransition
;
611 static const RunStateTransition runstate_transitions_def
[] = {
613 { RUN_STATE_PRECONFIG
, RUN_STATE_PRELAUNCH
},
614 /* Early switch to inmigrate state to allow -incoming CLI option work
615 * as it used to. TODO: delay actual switching to inmigrate state to
616 * the point after machine is built and remove this hack.
618 { RUN_STATE_PRECONFIG
, RUN_STATE_INMIGRATE
},
620 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
621 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
622 { RUN_STATE_DEBUG
, RUN_STATE_PRELAUNCH
},
624 { RUN_STATE_INMIGRATE
, RUN_STATE_INTERNAL_ERROR
},
625 { RUN_STATE_INMIGRATE
, RUN_STATE_IO_ERROR
},
626 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
627 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
628 { RUN_STATE_INMIGRATE
, RUN_STATE_SHUTDOWN
},
629 { RUN_STATE_INMIGRATE
, RUN_STATE_SUSPENDED
},
630 { RUN_STATE_INMIGRATE
, RUN_STATE_WATCHDOG
},
631 { RUN_STATE_INMIGRATE
, RUN_STATE_GUEST_PANICKED
},
632 { RUN_STATE_INMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
633 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
634 { RUN_STATE_INMIGRATE
, RUN_STATE_POSTMIGRATE
},
635 { RUN_STATE_INMIGRATE
, RUN_STATE_COLO
},
637 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
638 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
639 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PRELAUNCH
},
641 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
642 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
643 { RUN_STATE_IO_ERROR
, RUN_STATE_PRELAUNCH
},
645 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
646 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
647 { RUN_STATE_PAUSED
, RUN_STATE_POSTMIGRATE
},
648 { RUN_STATE_PAUSED
, RUN_STATE_PRELAUNCH
},
649 { RUN_STATE_PAUSED
, RUN_STATE_COLO
},
651 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
652 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
653 { RUN_STATE_POSTMIGRATE
, RUN_STATE_PRELAUNCH
},
655 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
656 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
657 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
659 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
660 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_PAUSED
},
661 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
662 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_PRELAUNCH
},
663 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_COLO
},
665 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
666 { RUN_STATE_RESTORE_VM
, RUN_STATE_PRELAUNCH
},
668 { RUN_STATE_COLO
, RUN_STATE_RUNNING
},
670 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
671 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
672 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
673 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
674 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
675 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
676 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
677 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
678 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
679 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
680 { RUN_STATE_RUNNING
, RUN_STATE_COLO
},
682 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
684 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
685 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
686 { RUN_STATE_SHUTDOWN
, RUN_STATE_PRELAUNCH
},
688 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
689 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
690 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
691 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
692 { RUN_STATE_SUSPENDED
, RUN_STATE_PRELAUNCH
},
693 { RUN_STATE_SUSPENDED
, RUN_STATE_COLO
},
695 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
696 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
697 { RUN_STATE_WATCHDOG
, RUN_STATE_PRELAUNCH
},
698 { RUN_STATE_WATCHDOG
, RUN_STATE_COLO
},
700 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
701 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
702 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_PRELAUNCH
},
704 { RUN_STATE__MAX
, RUN_STATE__MAX
},
707 static bool runstate_valid_transitions
[RUN_STATE__MAX
][RUN_STATE__MAX
];
709 bool runstate_check(RunState state
)
711 return current_run_state
== state
;
714 bool runstate_store(char *str
, size_t size
)
716 const char *state
= RunState_str(current_run_state
);
717 size_t len
= strlen(state
) + 1;
722 memcpy(str
, state
, len
);
726 static void runstate_init(void)
728 const RunStateTransition
*p
;
730 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
731 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE__MAX
; p
++) {
732 runstate_valid_transitions
[p
->from
][p
->to
] = true;
735 qemu_mutex_init(&vmstop_lock
);
738 /* This function will abort() on invalid state transitions */
739 void runstate_set(RunState new_state
)
741 assert(new_state
< RUN_STATE__MAX
);
743 if (current_run_state
== new_state
) {
747 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
748 error_report("invalid runstate transition: '%s' -> '%s'",
749 RunState_str(current_run_state
),
750 RunState_str(new_state
));
753 trace_runstate_set(new_state
);
754 current_run_state
= new_state
;
757 int runstate_is_running(void)
759 return runstate_check(RUN_STATE_RUNNING
);
762 bool runstate_needs_reset(void)
764 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
765 runstate_check(RUN_STATE_SHUTDOWN
);
768 StatusInfo
*qmp_query_status(Error
**errp
)
770 StatusInfo
*info
= g_malloc0(sizeof(*info
));
772 info
->running
= runstate_is_running();
773 info
->singlestep
= singlestep
;
774 info
->status
= current_run_state
;
779 bool qemu_vmstop_requested(RunState
*r
)
781 qemu_mutex_lock(&vmstop_lock
);
782 *r
= vmstop_requested
;
783 vmstop_requested
= RUN_STATE__MAX
;
784 qemu_mutex_unlock(&vmstop_lock
);
785 return *r
< RUN_STATE__MAX
;
788 void qemu_system_vmstop_request_prepare(void)
790 qemu_mutex_lock(&vmstop_lock
);
793 void qemu_system_vmstop_request(RunState state
)
795 vmstop_requested
= state
;
796 qemu_mutex_unlock(&vmstop_lock
);
800 /***********************************************************/
801 /* RTC reference time/date access */
802 static time_t qemu_ref_timedate(QEMUClockType clock
)
804 time_t value
= qemu_clock_get_ms(clock
) / 1000;
806 case QEMU_CLOCK_REALTIME
:
807 value
-= rtc_realtime_clock_offset
;
809 case QEMU_CLOCK_VIRTUAL
:
810 value
+= rtc_ref_start_datetime
;
812 case QEMU_CLOCK_HOST
:
813 if (rtc_base_type
== RTC_BASE_DATETIME
) {
814 value
-= rtc_host_datetime_offset
;
823 void qemu_get_timedate(struct tm
*tm
, int offset
)
825 time_t ti
= qemu_ref_timedate(rtc_clock
);
829 switch (rtc_base_type
) {
830 case RTC_BASE_DATETIME
:
834 case RTC_BASE_LOCALTIME
:
835 localtime_r(&ti
, tm
);
840 int qemu_timedate_diff(struct tm
*tm
)
844 switch (rtc_base_type
) {
845 case RTC_BASE_DATETIME
:
847 seconds
= mktimegm(tm
);
849 case RTC_BASE_LOCALTIME
:
852 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
853 seconds
= mktime(&tmp
);
860 return seconds
- qemu_ref_timedate(QEMU_CLOCK_HOST
);
863 static void configure_rtc_base_datetime(const char *startdate
)
865 time_t rtc_start_datetime
;
868 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d", &tm
.tm_year
, &tm
.tm_mon
,
869 &tm
.tm_mday
, &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) == 6) {
871 } else if (sscanf(startdate
, "%d-%d-%d",
872 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
) == 3) {
881 rtc_start_datetime
= mktimegm(&tm
);
882 if (rtc_start_datetime
== -1) {
884 error_report("invalid datetime format");
885 error_printf("valid formats: "
886 "'2006-06-17T16:01:21' or '2006-06-17'\n");
889 rtc_host_datetime_offset
= rtc_ref_start_datetime
- rtc_start_datetime
;
890 rtc_ref_start_datetime
= rtc_start_datetime
;
893 static void configure_rtc(QemuOpts
*opts
)
898 rtc_clock
= QEMU_CLOCK_HOST
;
899 rtc_ref_start_datetime
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) / 1000;
900 rtc_realtime_clock_offset
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
902 value
= qemu_opt_get(opts
, "base");
904 if (!strcmp(value
, "utc")) {
905 rtc_base_type
= RTC_BASE_UTC
;
906 } else if (!strcmp(value
, "localtime")) {
907 Error
*blocker
= NULL
;
908 rtc_base_type
= RTC_BASE_LOCALTIME
;
909 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
,
910 "-rtc base=localtime");
911 replay_add_blocker(blocker
);
913 rtc_base_type
= RTC_BASE_DATETIME
;
914 configure_rtc_base_datetime(value
);
917 value
= qemu_opt_get(opts
, "clock");
919 if (!strcmp(value
, "host")) {
920 rtc_clock
= QEMU_CLOCK_HOST
;
921 } else if (!strcmp(value
, "rt")) {
922 rtc_clock
= QEMU_CLOCK_REALTIME
;
923 } else if (!strcmp(value
, "vm")) {
924 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
926 error_report("invalid option value '%s'", value
);
930 value
= qemu_opt_get(opts
, "driftfix");
932 if (!strcmp(value
, "slew")) {
933 static GlobalProperty slew_lost_ticks
= {
934 .driver
= "mc146818rtc",
935 .property
= "lost_tick_policy",
939 qdev_prop_register_global(&slew_lost_ticks
);
940 } else if (!strcmp(value
, "none")) {
941 /* discard is default */
943 error_report("invalid option value '%s'", value
);
949 /***********************************************************/
950 /* Bluetooth support */
953 static struct HCIInfo
*hci_table
[MAX_NICS
];
955 struct HCIInfo
*qemu_next_hci(void)
957 if (cur_hci
== nb_hcis
)
960 return hci_table
[cur_hci
++];
963 static int bt_hci_parse(const char *str
)
968 if (nb_hcis
>= MAX_NICS
) {
969 error_report("too many bluetooth HCIs (max %i)", MAX_NICS
);
982 bdaddr
.b
[5] = 0x56 + nb_hcis
;
983 hci
->bdaddr_set(hci
, bdaddr
.b
);
985 hci_table
[nb_hcis
++] = hci
;
990 static void bt_vhci_add(int vlan_id
)
992 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
995 warn_report("adding a VHCI to an empty scatternet %i",
998 bt_vhci_init(bt_new_hci(vlan
));
1001 static struct bt_device_s
*bt_device_add(const char *opt
)
1003 struct bt_scatternet_s
*vlan
;
1005 char *endp
= strstr(opt
, ",vlan=");
1006 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1009 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1012 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1014 error_report("unrecognised bluetooth vlan Id");
1019 vlan
= qemu_find_bt_vlan(vlan_id
);
1022 warn_report("adding a slave device to an empty scatternet %i",
1025 if (!strcmp(devname
, "keyboard"))
1026 return bt_keyboard_init(vlan
);
1028 error_report("unsupported bluetooth device '%s'", devname
);
1032 static int bt_parse(const char *opt
)
1034 const char *endp
, *p
;
1037 if (strstart(opt
, "hci", &endp
)) {
1038 if (!*endp
|| *endp
== ',') {
1040 if (!strstart(endp
, ",vlan=", 0))
1043 return bt_hci_parse(opt
);
1045 } else if (strstart(opt
, "vhci", &endp
)) {
1046 if (!*endp
|| *endp
== ',') {
1048 if (strstart(endp
, ",vlan=", &p
)) {
1049 vlan
= strtol(p
, (char **) &endp
, 0);
1051 error_report("bad scatternet '%s'", p
);
1055 error_report("bad parameter '%s'", endp
+ 1);
1064 } else if (strstart(opt
, "device:", &endp
))
1065 return !bt_device_add(endp
);
1067 error_report("bad bluetooth parameter '%s'", opt
);
1071 static int parse_name(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1073 const char *proc_name
;
1075 if (qemu_opt_get(opts
, "debug-threads")) {
1076 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1078 qemu_name
= qemu_opt_get(opts
, "guest");
1080 proc_name
= qemu_opt_get(opts
, "process");
1082 os_set_proc_name(proc_name
);
1088 bool defaults_enabled(void)
1090 return has_defaults
;
1094 static int parse_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1096 int fd
, dupfd
, flags
;
1098 const char *fd_opaque
= NULL
;
1101 fd
= qemu_opt_get_number(opts
, "fd", -1);
1102 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1103 fd_opaque
= qemu_opt_get(opts
, "opaque");
1106 error_setg(errp
, "fd option is required and must be non-negative");
1110 if (fd
<= STDERR_FILENO
) {
1111 error_setg(errp
, "fd cannot be a standard I/O stream");
1116 * All fds inherited across exec() necessarily have FD_CLOEXEC
1117 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1119 flags
= fcntl(fd
, F_GETFD
);
1120 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1121 error_setg(errp
, "fd is not valid or already in use");
1126 error_setg(errp
, "set option is required and must be non-negative");
1130 #ifdef F_DUPFD_CLOEXEC
1131 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1135 qemu_set_cloexec(dupfd
);
1139 error_setg(errp
, "error duplicating fd: %s", strerror(errno
));
1143 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1144 fdinfo
= monitor_fdset_add_fd(dupfd
, true, fdset_id
, !!fd_opaque
, fd_opaque
,
1151 static int cleanup_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1155 fd
= qemu_opt_get_number(opts
, "fd", -1);
1162 /***********************************************************/
1163 /* QEMU Block devices */
1165 #define HD_OPTS "media=disk"
1166 #define CDROM_OPTS "media=cdrom"
1168 #define PFLASH_OPTS ""
1172 static int drive_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1174 BlockInterfaceType
*block_default_type
= opaque
;
1176 return drive_new(opts
, *block_default_type
, errp
) == NULL
;
1179 static int drive_enable_snapshot(void *opaque
, QemuOpts
*opts
, Error
**errp
)
1181 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1182 qemu_opt_set(opts
, "snapshot", "on", &error_abort
);
1187 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1188 int index
, const char *optstr
)
1193 if (!enable
|| drive_get_by_index(type
, index
)) {
1197 opts
= drive_add(type
, index
, NULL
, optstr
);
1199 drive_enable_snapshot(NULL
, opts
, NULL
);
1202 dinfo
= drive_new(opts
, type
, &error_abort
);
1203 dinfo
->is_default
= true;
1207 static QemuOptsList qemu_smp_opts
= {
1209 .implied_opt_name
= "cpus",
1210 .merge_lists
= true,
1211 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1215 .type
= QEMU_OPT_NUMBER
,
1218 .type
= QEMU_OPT_NUMBER
,
1221 .type
= QEMU_OPT_NUMBER
,
1224 .type
= QEMU_OPT_NUMBER
,
1227 .type
= QEMU_OPT_NUMBER
,
1229 { /*End of list */ }
1233 static void smp_parse(QemuOpts
*opts
)
1236 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1237 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1238 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1239 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1241 /* compute missing values, prefer sockets over cores over threads */
1242 if (cpus
== 0 || sockets
== 0) {
1243 cores
= cores
> 0 ? cores
: 1;
1244 threads
= threads
> 0 ? threads
: 1;
1246 sockets
= sockets
> 0 ? sockets
: 1;
1247 cpus
= cores
* threads
* sockets
;
1249 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", cpus
);
1250 sockets
= max_cpus
/ (cores
* threads
);
1252 } else if (cores
== 0) {
1253 threads
= threads
> 0 ? threads
: 1;
1254 cores
= cpus
/ (sockets
* threads
);
1255 cores
= cores
> 0 ? cores
: 1;
1256 } else if (threads
== 0) {
1257 threads
= cpus
/ (cores
* sockets
);
1258 threads
= threads
> 0 ? threads
: 1;
1259 } else if (sockets
* cores
* threads
< cpus
) {
1260 error_report("cpu topology: "
1261 "sockets (%u) * cores (%u) * threads (%u) < "
1263 sockets
, cores
, threads
, cpus
);
1267 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", cpus
);
1269 if (max_cpus
< cpus
) {
1270 error_report("maxcpus must be equal to or greater than smp");
1274 if (sockets
* cores
* threads
> max_cpus
) {
1275 error_report("cpu topology: "
1276 "sockets (%u) * cores (%u) * threads (%u) > "
1278 sockets
, cores
, threads
, max_cpus
);
1282 if (sockets
* cores
* threads
!= max_cpus
) {
1283 warn_report("Invalid CPU topology deprecated: "
1284 "sockets (%u) * cores (%u) * threads (%u) "
1286 sockets
, cores
, threads
, max_cpus
);
1291 smp_threads
= threads
;
1295 Error
*blocker
= NULL
;
1296 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
1297 replay_add_blocker(blocker
);
1301 static void realtime_init(void)
1304 if (os_mlock() < 0) {
1305 error_report("locking memory failed");
1312 static void configure_msg(QemuOpts
*opts
)
1314 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1317 /***********************************************************/
1320 typedef struct SemihostingConfig
{
1322 SemihostingTarget target
;
1325 const char *cmdline
; /* concatenated argv */
1326 } SemihostingConfig
;
1328 static SemihostingConfig semihosting
;
1330 bool semihosting_enabled(void)
1332 return semihosting
.enabled
;
1335 SemihostingTarget
semihosting_get_target(void)
1337 return semihosting
.target
;
1340 const char *semihosting_get_arg(int i
)
1342 if (i
>= semihosting
.argc
) {
1345 return semihosting
.argv
[i
];
1348 int semihosting_get_argc(void)
1350 return semihosting
.argc
;
1353 const char *semihosting_get_cmdline(void)
1355 if (semihosting
.cmdline
== NULL
&& semihosting
.argc
> 0) {
1356 semihosting
.cmdline
= g_strjoinv(" ", (gchar
**)semihosting
.argv
);
1358 return semihosting
.cmdline
;
1361 static int add_semihosting_arg(void *opaque
,
1362 const char *name
, const char *val
,
1365 SemihostingConfig
*s
= opaque
;
1366 if (strcmp(name
, "arg") == 0) {
1368 /* one extra element as g_strjoinv() expects NULL-terminated array */
1369 s
->argv
= g_realloc(s
->argv
, (s
->argc
+ 1) * sizeof(void *));
1370 s
->argv
[s
->argc
- 1] = val
;
1371 s
->argv
[s
->argc
] = NULL
;
1376 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1377 static inline void semihosting_arg_fallback(const char *file
, const char *cmd
)
1382 add_semihosting_arg(&semihosting
, "arg", file
, NULL
);
1384 /* split -append and initialize argv[1..n] */
1385 cmd_token
= strtok(g_strdup(cmd
), " ");
1387 add_semihosting_arg(&semihosting
, "arg", cmd_token
, NULL
);
1388 cmd_token
= strtok(NULL
, " ");
1392 /* Now we still need this for compatibility with XEN. */
1393 bool has_igd_gfx_passthru
;
1394 static void igd_gfx_passthru(void)
1396 has_igd_gfx_passthru
= current_machine
->igd_gfx_passthru
;
1399 /***********************************************************/
1402 static int usb_device_add(const char *devname
)
1404 USBDevice
*dev
= NULL
;
1406 if (!machine_usb(current_machine
)) {
1410 dev
= usbdevice_create(devname
);
1417 static int usb_parse(const char *cmdline
)
1420 r
= usb_device_add(cmdline
);
1422 error_report("could not add USB device '%s'", cmdline
);
1427 /***********************************************************/
1428 /* machine registration */
1430 MachineState
*current_machine
;
1432 static MachineClass
*find_machine(const char *name
)
1434 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1435 MachineClass
*mc
= NULL
;
1437 for (el
= machines
; el
; el
= el
->next
) {
1438 MachineClass
*temp
= el
->data
;
1440 if (!strcmp(temp
->name
, name
)) {
1445 !strcmp(temp
->alias
, name
)) {
1451 g_slist_free(machines
);
1455 MachineClass
*find_default_machine(void)
1457 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1458 MachineClass
*mc
= NULL
;
1460 for (el
= machines
; el
; el
= el
->next
) {
1461 MachineClass
*temp
= el
->data
;
1463 if (temp
->is_default
) {
1469 g_slist_free(machines
);
1473 MachineInfoList
*qmp_query_machines(Error
**errp
)
1475 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1476 MachineInfoList
*mach_list
= NULL
;
1478 for (el
= machines
; el
; el
= el
->next
) {
1479 MachineClass
*mc
= el
->data
;
1480 MachineInfoList
*entry
;
1483 info
= g_malloc0(sizeof(*info
));
1484 if (mc
->is_default
) {
1485 info
->has_is_default
= true;
1486 info
->is_default
= true;
1490 info
->has_alias
= true;
1491 info
->alias
= g_strdup(mc
->alias
);
1494 info
->name
= g_strdup(mc
->name
);
1495 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1496 info
->hotpluggable_cpus
= mc
->has_hotpluggable_cpus
;
1498 entry
= g_malloc0(sizeof(*entry
));
1499 entry
->value
= info
;
1500 entry
->next
= mach_list
;
1504 g_slist_free(machines
);
1508 static int machine_help_func(QemuOpts
*opts
, MachineState
*machine
)
1510 ObjectProperty
*prop
;
1511 ObjectPropertyIterator iter
;
1513 if (!qemu_opt_has_help_opt(opts
)) {
1517 object_property_iter_init(&iter
, OBJECT(machine
));
1518 while ((prop
= object_property_iter_next(&iter
))) {
1523 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine
)->name
,
1524 prop
->name
, prop
->type
);
1525 if (prop
->description
) {
1526 error_printf(" (%s)\n", prop
->description
);
1535 /***********************************************************/
1536 /* main execution loop */
1538 struct vm_change_state_entry
{
1539 VMChangeStateHandler
*cb
;
1541 QLIST_ENTRY (vm_change_state_entry
) entries
;
1544 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1546 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1549 VMChangeStateEntry
*e
;
1551 e
= g_malloc0(sizeof (*e
));
1555 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1559 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1561 QLIST_REMOVE (e
, entries
);
1565 void vm_state_notify(int running
, RunState state
)
1567 VMChangeStateEntry
*e
, *next
;
1569 trace_vm_state_notify(running
, state
);
1571 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1572 e
->cb(e
->opaque
, running
, state
);
1576 static ShutdownCause reset_requested
;
1577 static ShutdownCause shutdown_requested
;
1578 static int shutdown_signal
;
1579 static pid_t shutdown_pid
;
1580 static int powerdown_requested
;
1581 static int debug_requested
;
1582 static int suspend_requested
;
1583 static bool preconfig_exit_requested
= true;
1584 static WakeupReason wakeup_reason
;
1585 static NotifierList powerdown_notifiers
=
1586 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1587 static NotifierList suspend_notifiers
=
1588 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1589 static NotifierList wakeup_notifiers
=
1590 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1591 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1593 ShutdownCause
qemu_shutdown_requested_get(void)
1595 return shutdown_requested
;
1598 ShutdownCause
qemu_reset_requested_get(void)
1600 return reset_requested
;
1603 static int qemu_shutdown_requested(void)
1605 return atomic_xchg(&shutdown_requested
, SHUTDOWN_CAUSE_NONE
);
1608 static void qemu_kill_report(void)
1610 if (!qtest_driver() && shutdown_signal
) {
1611 if (shutdown_pid
== 0) {
1612 /* This happens for eg ^C at the terminal, so it's worth
1613 * avoiding printing an odd message in that case.
1615 error_report("terminating on signal %d", shutdown_signal
);
1617 char *shutdown_cmd
= qemu_get_pid_name(shutdown_pid
);
1619 error_report("terminating on signal %d from pid " FMT_pid
" (%s)",
1620 shutdown_signal
, shutdown_pid
,
1621 shutdown_cmd
? shutdown_cmd
: "<unknown process>");
1622 g_free(shutdown_cmd
);
1624 shutdown_signal
= 0;
1628 static ShutdownCause
qemu_reset_requested(void)
1630 ShutdownCause r
= reset_requested
;
1632 if (r
&& replay_checkpoint(CHECKPOINT_RESET_REQUESTED
)) {
1633 reset_requested
= SHUTDOWN_CAUSE_NONE
;
1636 return SHUTDOWN_CAUSE_NONE
;
1639 static int qemu_suspend_requested(void)
1641 int r
= suspend_requested
;
1642 if (r
&& replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED
)) {
1643 suspend_requested
= 0;
1649 static WakeupReason
qemu_wakeup_requested(void)
1651 return wakeup_reason
;
1654 static int qemu_powerdown_requested(void)
1656 int r
= powerdown_requested
;
1657 powerdown_requested
= 0;
1661 static int qemu_debug_requested(void)
1663 int r
= debug_requested
;
1664 debug_requested
= 0;
1668 void qemu_exit_preconfig_request(void)
1670 preconfig_exit_requested
= true;
1674 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1676 void qemu_system_reset(ShutdownCause reason
)
1680 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1682 cpu_synchronize_all_states();
1684 if (mc
&& mc
->reset
) {
1687 qemu_devices_reset();
1689 if (reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
1690 qapi_event_send_reset(shutdown_caused_by_guest(reason
));
1692 cpu_synchronize_all_post_reset();
1695 void qemu_system_guest_panicked(GuestPanicInformation
*info
)
1697 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crashed");
1700 current_cpu
->crash_occurred
= true;
1702 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
,
1704 vm_stop(RUN_STATE_GUEST_PANICKED
);
1706 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF
,
1708 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC
);
1712 if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_HYPER_V
) {
1713 qemu_log_mask(LOG_GUEST_ERROR
, "\nHV crash parameters: (%#"PRIx64
1714 " %#"PRIx64
" %#"PRIx64
" %#"PRIx64
" %#"PRIx64
")\n",
1715 info
->u
.hyper_v
.arg1
,
1716 info
->u
.hyper_v
.arg2
,
1717 info
->u
.hyper_v
.arg3
,
1718 info
->u
.hyper_v
.arg4
,
1719 info
->u
.hyper_v
.arg5
);
1720 } else if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_S390
) {
1721 qemu_log_mask(LOG_GUEST_ERROR
, " on cpu %d: %s\n"
1722 "PSW: 0x%016" PRIx64
" 0x%016" PRIx64
"\n",
1724 S390CrashReason_str(info
->u
.s390
.reason
),
1725 info
->u
.s390
.psw_mask
,
1726 info
->u
.s390
.psw_addr
);
1728 qapi_free_GuestPanicInformation(info
);
1732 void qemu_system_reset_request(ShutdownCause reason
)
1734 if (no_reboot
&& reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
1735 shutdown_requested
= reason
;
1737 reset_requested
= reason
;
1740 qemu_notify_event();
1743 static void qemu_system_suspend(void)
1746 notifier_list_notify(&suspend_notifiers
, NULL
);
1747 runstate_set(RUN_STATE_SUSPENDED
);
1748 qapi_event_send_suspend();
1751 void qemu_system_suspend_request(void)
1753 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1756 suspend_requested
= 1;
1758 qemu_notify_event();
1761 void qemu_register_suspend_notifier(Notifier
*notifier
)
1763 notifier_list_add(&suspend_notifiers
, notifier
);
1766 void qemu_system_wakeup_request(WakeupReason reason
)
1768 trace_system_wakeup_request(reason
);
1770 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1773 if (!(wakeup_reason_mask
& (1 << reason
))) {
1776 runstate_set(RUN_STATE_RUNNING
);
1777 wakeup_reason
= reason
;
1778 qemu_notify_event();
1781 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1784 wakeup_reason_mask
|= (1 << reason
);
1786 wakeup_reason_mask
&= ~(1 << reason
);
1790 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1792 notifier_list_add(&wakeup_notifiers
, notifier
);
1795 void qemu_system_killed(int signal
, pid_t pid
)
1797 shutdown_signal
= signal
;
1801 /* Cannot call qemu_system_shutdown_request directly because
1802 * we are in a signal handler.
1804 shutdown_requested
= SHUTDOWN_CAUSE_HOST_SIGNAL
;
1805 qemu_notify_event();
1808 void qemu_system_shutdown_request(ShutdownCause reason
)
1810 trace_qemu_system_shutdown_request(reason
);
1811 replay_shutdown_request(reason
);
1812 shutdown_requested
= reason
;
1813 qemu_notify_event();
1816 static void qemu_system_powerdown(void)
1818 qapi_event_send_powerdown();
1819 notifier_list_notify(&powerdown_notifiers
, NULL
);
1822 void qemu_system_powerdown_request(void)
1824 trace_qemu_system_powerdown_request();
1825 powerdown_requested
= 1;
1826 qemu_notify_event();
1829 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1831 notifier_list_add(&powerdown_notifiers
, notifier
);
1834 void qemu_system_debug_request(void)
1836 debug_requested
= 1;
1837 qemu_notify_event();
1840 static bool main_loop_should_exit(void)
1843 ShutdownCause request
;
1845 if (preconfig_exit_requested
) {
1846 if (runstate_check(RUN_STATE_PRECONFIG
)) {
1847 runstate_set(RUN_STATE_PRELAUNCH
);
1849 preconfig_exit_requested
= false;
1852 if (qemu_debug_requested()) {
1853 vm_stop(RUN_STATE_DEBUG
);
1855 if (qemu_suspend_requested()) {
1856 qemu_system_suspend();
1858 request
= qemu_shutdown_requested();
1861 qapi_event_send_shutdown(shutdown_caused_by_guest(request
));
1863 vm_stop(RUN_STATE_SHUTDOWN
);
1868 request
= qemu_reset_requested();
1871 qemu_system_reset(request
);
1873 if (!runstate_check(RUN_STATE_RUNNING
) &&
1874 !runstate_check(RUN_STATE_INMIGRATE
)) {
1875 runstate_set(RUN_STATE_PRELAUNCH
);
1878 if (qemu_wakeup_requested()) {
1880 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
1881 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1882 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1884 qapi_event_send_wakeup();
1886 if (qemu_powerdown_requested()) {
1887 qemu_system_powerdown();
1889 if (qemu_vmstop_requested(&r
)) {
1895 static void main_loop(void)
1897 #ifdef CONFIG_PROFILER
1900 while (!main_loop_should_exit()) {
1901 #ifdef CONFIG_PROFILER
1902 ti
= profile_getclock();
1904 main_loop_wait(false);
1905 #ifdef CONFIG_PROFILER
1906 dev_time
+= profile_getclock() - ti
;
1911 static void version(void)
1913 printf("QEMU emulator version " QEMU_FULL_VERSION
"\n"
1914 QEMU_COPYRIGHT
"\n");
1917 static void QEMU_NORETURN
help(int exitcode
)
1920 printf("usage: %s [options] [disk_image]\n\n"
1921 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1922 error_get_progname());
1924 #define QEMU_OPTIONS_GENERATE_HELP
1925 #include "qemu-options-wrapper.h"
1927 printf("\nDuring emulation, the following keys are useful:\n"
1928 "ctrl-alt-f toggle full screen\n"
1929 "ctrl-alt-n switch to virtual console 'n'\n"
1930 "ctrl-alt toggle mouse and keyboard grab\n"
1932 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1934 QEMU_HELP_BOTTOM
"\n");
1939 #define HAS_ARG 0x0001
1941 typedef struct QEMUOption
{
1948 static const QEMUOption qemu_options
[] = {
1949 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1950 #define QEMU_OPTIONS_GENERATE_OPTIONS
1951 #include "qemu-options-wrapper.h"
1955 typedef struct VGAInterfaceInfo
{
1956 const char *opt_name
; /* option name */
1957 const char *name
; /* human-readable name */
1958 /* Class names indicating that support is available.
1959 * If no class is specified, the interface is always available */
1960 const char *class_names
[2];
1963 static VGAInterfaceInfo vga_interfaces
[VGA_TYPE_MAX
] = {
1969 .name
= "standard VGA",
1970 .class_names
= { "VGA", "isa-vga" },
1973 .opt_name
= "cirrus",
1974 .name
= "Cirrus VGA",
1975 .class_names
= { "cirrus-vga", "isa-cirrus-vga" },
1978 .opt_name
= "vmware",
1979 .name
= "VMWare SVGA",
1980 .class_names
= { "vmware-svga" },
1983 .opt_name
= "virtio",
1984 .name
= "Virtio VGA",
1985 .class_names
= { "virtio-vga" },
1990 .class_names
= { "qxl-vga" },
1994 .name
= "TCX framebuffer",
1995 .class_names
= { "SUNW,tcx" },
1999 .name
= "CG3 framebuffer",
2000 .class_names
= { "cgthree" },
2003 .opt_name
= "xenfb",
2007 static bool vga_interface_available(VGAInterfaceType t
)
2009 VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
2011 assert(t
< VGA_TYPE_MAX
);
2012 return !ti
->class_names
[0] ||
2013 object_class_by_name(ti
->class_names
[0]) ||
2014 object_class_by_name(ti
->class_names
[1]);
2017 static void select_vgahw(const char *p
)
2022 assert(vga_interface_type
== VGA_NONE
);
2023 for (t
= 0; t
< VGA_TYPE_MAX
; t
++) {
2024 VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
2025 if (ti
->opt_name
&& strstart(p
, ti
->opt_name
, &opts
)) {
2026 if (!vga_interface_available(t
)) {
2027 error_report("%s not available", ti
->name
);
2030 vga_interface_type
= t
;
2034 if (t
== VGA_TYPE_MAX
) {
2036 error_report("unknown vga type: %s", p
);
2040 const char *nextopt
;
2042 if (strstart(opts
, ",retrace=", &nextopt
)) {
2044 if (strstart(opts
, "dumb", &nextopt
))
2045 vga_retrace_method
= VGA_RETRACE_DUMB
;
2046 else if (strstart(opts
, "precise", &nextopt
))
2047 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2048 else goto invalid_vga
;
2049 } else goto invalid_vga
;
2054 static void parse_display_qapi(const char *optarg
)
2056 DisplayOptions
*opts
;
2059 v
= qobject_input_visitor_new_str(optarg
, "type", &error_fatal
);
2061 visit_type_DisplayOptions(v
, NULL
, &opts
, &error_fatal
);
2062 QAPI_CLONE_MEMBERS(DisplayOptions
, &dpy
, opts
);
2064 qapi_free_DisplayOptions(opts
);
2068 static void parse_display(const char *p
)
2072 if (strstart(p
, "sdl", &opts
)) {
2074 * sdl DisplayType needs hand-crafted parser instead of
2075 * parse_display_qapi() due to some options not in
2076 * DisplayOptions, specifically:
2078 * Already deprecated.
2079 * - ctrl_grab + alt_grab
2080 * Not clear yet what happens to them long-term. Should
2081 * replaced by something better or deprecated and dropped.
2083 dpy
.type
= DISPLAY_TYPE_SDL
;
2085 const char *nextopt
;
2087 if (strstart(opts
, ",frame=", &nextopt
)) {
2088 g_printerr("The frame= sdl option is deprecated, and will be\n"
2089 "removed in a future release.\n");
2091 if (strstart(opts
, "on", &nextopt
)) {
2093 } else if (strstart(opts
, "off", &nextopt
)) {
2096 goto invalid_sdl_args
;
2098 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2100 if (strstart(opts
, "on", &nextopt
)) {
2102 } else if (strstart(opts
, "off", &nextopt
)) {
2105 goto invalid_sdl_args
;
2107 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2109 if (strstart(opts
, "on", &nextopt
)) {
2111 } else if (strstart(opts
, "off", &nextopt
)) {
2114 goto invalid_sdl_args
;
2116 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2118 dpy
.has_window_close
= true;
2119 if (strstart(opts
, "on", &nextopt
)) {
2120 dpy
.window_close
= true;
2121 } else if (strstart(opts
, "off", &nextopt
)) {
2122 dpy
.window_close
= false;
2124 goto invalid_sdl_args
;
2126 } else if (strstart(opts
, ",gl=", &nextopt
)) {
2129 if (strstart(opts
, "on", &nextopt
)) {
2130 dpy
.gl
= DISPLAYGL_MODE_ON
;
2131 } else if (strstart(opts
, "core", &nextopt
)) {
2132 dpy
.gl
= DISPLAYGL_MODE_CORE
;
2133 } else if (strstart(opts
, "es", &nextopt
)) {
2134 dpy
.gl
= DISPLAYGL_MODE_ES
;
2135 } else if (strstart(opts
, "off", &nextopt
)) {
2136 dpy
.gl
= DISPLAYGL_MODE_OFF
;
2138 goto invalid_sdl_args
;
2142 error_report("invalid SDL option string");
2147 } else if (strstart(p
, "vnc", &opts
)) {
2149 * vnc isn't a (local) DisplayType but a protocol for remote
2153 vnc_parse(opts
+ 1, &error_fatal
);
2155 error_report("VNC requires a display argument vnc=<display>");
2159 parse_display_qapi(p
);
2163 char *qemu_find_file(int type
, const char *name
)
2169 /* Try the name as a straight path first */
2170 if (access(name
, R_OK
) == 0) {
2171 trace_load_file(name
, name
);
2172 return g_strdup(name
);
2176 case QEMU_FILE_TYPE_BIOS
:
2179 case QEMU_FILE_TYPE_KEYMAP
:
2180 subdir
= "keymaps/";
2186 for (i
= 0; i
< data_dir_idx
; i
++) {
2187 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2188 if (access(buf
, R_OK
) == 0) {
2189 trace_load_file(name
, buf
);
2197 static void qemu_add_data_dir(const char *path
)
2204 if (data_dir_idx
== ARRAY_SIZE(data_dir
)) {
2207 for (i
= 0; i
< data_dir_idx
; i
++) {
2208 if (strcmp(data_dir
[i
], path
) == 0) {
2209 return; /* duplicate */
2212 data_dir
[data_dir_idx
++] = g_strdup(path
);
2215 static inline bool nonempty_str(const char *str
)
2220 static int parse_fw_cfg(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2224 const char *name
, *file
, *str
;
2225 FWCfgState
*fw_cfg
= (FWCfgState
*) opaque
;
2227 if (fw_cfg
== NULL
) {
2228 error_setg(errp
, "fw_cfg device not available");
2231 name
= qemu_opt_get(opts
, "name");
2232 file
= qemu_opt_get(opts
, "file");
2233 str
= qemu_opt_get(opts
, "string");
2235 /* we need name and either a file or the content string */
2236 if (!(nonempty_str(name
) && (nonempty_str(file
) || nonempty_str(str
)))) {
2237 error_setg(errp
, "invalid argument(s)");
2240 if (nonempty_str(file
) && nonempty_str(str
)) {
2241 error_setg(errp
, "file and string are mutually exclusive");
2244 if (strlen(name
) > FW_CFG_MAX_FILE_PATH
- 1) {
2245 error_setg(errp
, "name too long (max. %d char)",
2246 FW_CFG_MAX_FILE_PATH
- 1);
2249 if (strncmp(name
, "opt/", 4) != 0) {
2250 warn_report("externally provided fw_cfg item names "
2251 "should be prefixed with \"opt/\"");
2253 if (nonempty_str(str
)) {
2254 size
= strlen(str
); /* NUL terminator NOT included in fw_cfg blob */
2255 buf
= g_memdup(str
, size
);
2257 if (!g_file_get_contents(file
, &buf
, &size
, NULL
)) {
2258 error_setg(errp
, "can't load %s", file
);
2262 /* For legacy, keep user files in a specific global order. */
2263 fw_cfg_set_order_override(fw_cfg
, FW_CFG_ORDER_OVERRIDE_USER
);
2264 fw_cfg_add_file(fw_cfg
, name
, buf
, size
);
2265 fw_cfg_reset_order_override(fw_cfg
);
2269 static int device_help_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2271 return qdev_device_help(opts
);
2274 static int device_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2278 dev
= qdev_device_add(opts
, errp
);
2282 object_unref(OBJECT(dev
));
2286 static int chardev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2288 Error
*local_err
= NULL
;
2290 if (!qemu_chr_new_from_opts(opts
, &local_err
)) {
2292 error_propagate(errp
, local_err
);
2300 #ifdef CONFIG_VIRTFS
2301 static int fsdev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2303 return qemu_fsdev_add(opts
, errp
);
2307 static int mon_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2310 const char *chardev
;
2314 mode
= qemu_opt_get(opts
, "mode");
2318 if (strcmp(mode
, "readline") == 0) {
2319 flags
= MONITOR_USE_READLINE
;
2320 } else if (strcmp(mode
, "control") == 0) {
2321 flags
= MONITOR_USE_CONTROL
;
2323 error_setg(errp
, "unknown monitor mode \"%s\"", mode
);
2327 if (qemu_opt_get_bool(opts
, "pretty", 0))
2328 flags
|= MONITOR_USE_PRETTY
;
2330 /* OOB is off by default */
2331 if (qemu_opt_get_bool(opts
, "x-oob", 0)) {
2332 flags
|= MONITOR_USE_OOB
;
2335 chardev
= qemu_opt_get(opts
, "chardev");
2337 error_report("chardev is required");
2340 chr
= qemu_chr_find(chardev
);
2342 error_setg(errp
, "chardev \"%s\" not found", chardev
);
2346 monitor_init(chr
, flags
);
2350 static void monitor_parse(const char *optarg
, const char *mode
, bool pretty
)
2352 static int monitor_device_index
= 0;
2357 if (strstart(optarg
, "chardev:", &p
)) {
2358 snprintf(label
, sizeof(label
), "%s", p
);
2360 snprintf(label
, sizeof(label
), "compat_monitor%d",
2361 monitor_device_index
);
2362 opts
= qemu_chr_parse_compat(label
, optarg
, true);
2364 error_report("parse error: %s", optarg
);
2369 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, &error_fatal
);
2370 qemu_opt_set(opts
, "mode", mode
, &error_abort
);
2371 qemu_opt_set(opts
, "chardev", label
, &error_abort
);
2372 qemu_opt_set_bool(opts
, "pretty", pretty
, &error_abort
);
2373 monitor_device_index
++;
2376 struct device_config
{
2378 DEV_USB
, /* -usbdevice */
2380 DEV_SERIAL
, /* -serial */
2381 DEV_PARALLEL
, /* -parallel */
2382 DEV_VIRTCON
, /* -virtioconsole */
2383 DEV_DEBUGCON
, /* -debugcon */
2384 DEV_GDB
, /* -gdb, -s */
2385 DEV_SCLP
, /* s390 sclp */
2387 const char *cmdline
;
2389 QTAILQ_ENTRY(device_config
) next
;
2392 static QTAILQ_HEAD(, device_config
) device_configs
=
2393 QTAILQ_HEAD_INITIALIZER(device_configs
);
2395 static void add_device_config(int type
, const char *cmdline
)
2397 struct device_config
*conf
;
2399 conf
= g_malloc0(sizeof(*conf
));
2401 conf
->cmdline
= cmdline
;
2402 loc_save(&conf
->loc
);
2403 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2406 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2408 struct device_config
*conf
;
2411 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2412 if (conf
->type
!= type
)
2414 loc_push_restore(&conf
->loc
);
2415 rc
= func(conf
->cmdline
);
2416 loc_pop(&conf
->loc
);
2424 static int serial_parse(const char *devname
)
2426 int index
= num_serial_hds
;
2429 if (strcmp(devname
, "none") == 0)
2431 snprintf(label
, sizeof(label
), "serial%d", index
);
2432 serial_hds
= g_renew(Chardev
*, serial_hds
, index
+ 1);
2434 serial_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
);
2435 if (!serial_hds
[index
]) {
2436 error_report("could not connect serial device"
2437 " to character backend '%s'", devname
);
2444 Chardev
*serial_hd(int i
)
2447 if (i
< num_serial_hds
) {
2448 return serial_hds
[i
];
2453 int serial_max_hds(void)
2455 return num_serial_hds
;
2458 static int parallel_parse(const char *devname
)
2460 static int index
= 0;
2463 if (strcmp(devname
, "none") == 0)
2465 if (index
== MAX_PARALLEL_PORTS
) {
2466 error_report("too many parallel ports");
2469 snprintf(label
, sizeof(label
), "parallel%d", index
);
2470 parallel_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
);
2471 if (!parallel_hds
[index
]) {
2472 error_report("could not connect parallel device"
2473 " to character backend '%s'", devname
);
2480 static int virtcon_parse(const char *devname
)
2482 QemuOptsList
*device
= qemu_find_opts("device");
2483 static int index
= 0;
2485 QemuOpts
*bus_opts
, *dev_opts
;
2487 if (strcmp(devname
, "none") == 0)
2489 if (index
== MAX_VIRTIO_CONSOLES
) {
2490 error_report("too many virtio consoles");
2494 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2495 qemu_opt_set(bus_opts
, "driver", "virtio-serial", &error_abort
);
2497 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2498 qemu_opt_set(dev_opts
, "driver", "virtconsole", &error_abort
);
2500 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2501 virtcon_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
);
2502 if (!virtcon_hds
[index
]) {
2503 error_report("could not connect virtio console"
2504 " to character backend '%s'", devname
);
2507 qemu_opt_set(dev_opts
, "chardev", label
, &error_abort
);
2513 static int debugcon_parse(const char *devname
)
2517 if (!qemu_chr_new_mux_mon("debugcon", devname
)) {
2518 error_report("invalid character backend '%s'", devname
);
2521 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2523 error_report("already have a debugcon device");
2526 qemu_opt_set(opts
, "driver", "isa-debugcon", &error_abort
);
2527 qemu_opt_set(opts
, "chardev", "debugcon", &error_abort
);
2531 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2533 const MachineClass
*mc1
= a
, *mc2
= b
;
2536 if (mc1
->family
== NULL
) {
2537 if (mc2
->family
== NULL
) {
2538 /* Compare standalone machine types against each other; they sort
2539 * in increasing order.
2541 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2542 object_class_get_name(OBJECT_CLASS(mc2
)));
2545 /* Standalone machine types sort after families. */
2549 if (mc2
->family
== NULL
) {
2550 /* Families sort before standalone machine types. */
2554 /* Families sort between each other alphabetically increasingly. */
2555 res
= strcmp(mc1
->family
, mc2
->family
);
2560 /* Within the same family, machine types sort in decreasing order. */
2561 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2562 object_class_get_name(OBJECT_CLASS(mc1
)));
2565 static MachineClass
*machine_parse(const char *name
)
2567 MachineClass
*mc
= NULL
;
2568 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2571 mc
= find_machine(name
);
2574 g_slist_free(machines
);
2577 if (name
&& !is_help_option(name
)) {
2578 error_report("unsupported machine type");
2579 error_printf("Use -machine help to list supported machines\n");
2581 printf("Supported machines are:\n");
2582 machines
= g_slist_sort(machines
, machine_class_cmp
);
2583 for (el
= machines
; el
; el
= el
->next
) {
2584 MachineClass
*mc
= el
->data
;
2586 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2588 printf("%-20s %s%s%s\n", mc
->name
, mc
->desc
,
2589 mc
->is_default
? " (default)" : "",
2590 mc
->deprecation_reason
? " (deprecated)" : "");
2594 g_slist_free(machines
);
2595 exit(!name
|| !is_help_option(name
));
2598 void qemu_add_exit_notifier(Notifier
*notify
)
2600 notifier_list_add(&exit_notifiers
, notify
);
2603 void qemu_remove_exit_notifier(Notifier
*notify
)
2605 notifier_remove(notify
);
2608 static void qemu_run_exit_notifiers(void)
2610 notifier_list_notify(&exit_notifiers
, NULL
);
2613 static const char *pid_file
;
2614 static Notifier qemu_unlink_pidfile_notifier
;
2616 static void qemu_unlink_pidfile(Notifier
*n
, void *data
)
2623 bool machine_init_done
;
2625 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2627 notifier_list_add(&machine_init_done_notifiers
, notify
);
2628 if (machine_init_done
) {
2629 notify
->notify(notify
, NULL
);
2633 void qemu_remove_machine_init_done_notifier(Notifier
*notify
)
2635 notifier_remove(notify
);
2638 static void qemu_run_machine_init_done_notifiers(void)
2640 machine_init_done
= true;
2641 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2644 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2645 const char **poptarg
, int *poptind
)
2647 const QEMUOption
*popt
;
2648 int optind
= *poptind
;
2649 char *r
= argv
[optind
];
2652 loc_set_cmdline(argv
, optind
, 1);
2654 /* Treat --foo the same as -foo. */
2657 popt
= qemu_options
;
2660 error_report("invalid option");
2663 if (!strcmp(popt
->name
, r
+ 1))
2667 if (popt
->flags
& HAS_ARG
) {
2668 if (optind
>= argc
) {
2669 error_report("requires an argument");
2672 optarg
= argv
[optind
++];
2673 loc_set_cmdline(argv
, optind
- 2, 2);
2684 static MachineClass
*select_machine(void)
2686 MachineClass
*machine_class
= find_default_machine();
2691 loc_push_none(&loc
);
2693 opts
= qemu_get_machine_opts();
2694 qemu_opts_loc_restore(opts
);
2696 optarg
= qemu_opt_get(opts
, "type");
2698 machine_class
= machine_parse(optarg
);
2701 if (!machine_class
) {
2702 error_report("No machine specified, and there is no default");
2703 error_printf("Use -machine help to list supported machines\n");
2708 return machine_class
;
2711 static int machine_set_property(void *opaque
,
2712 const char *name
, const char *value
,
2715 Object
*obj
= OBJECT(opaque
);
2716 Error
*local_err
= NULL
;
2719 if (strcmp(name
, "type") == 0) {
2723 qom_name
= g_strdup(name
);
2724 for (p
= qom_name
; *p
; p
++) {
2730 object_property_parse(obj
, value
, qom_name
, &local_err
);
2734 error_propagate(errp
, local_err
);
2743 * Initial object creation happens before all other
2744 * QEMU data types are created. The majority of objects
2745 * can be created at this point. The rng-egd object
2746 * cannot be created here, as it depends on the chardev
2749 static bool object_create_initial(const char *type
, QemuOpts
*opts
)
2753 if (is_help_option(type
)) {
2756 printf("List of user creatable objects:\n");
2757 list
= object_class_get_list_sorted(TYPE_USER_CREATABLE
, false);
2758 for (l
= list
; l
!= NULL
; l
= l
->next
) {
2759 ObjectClass
*oc
= OBJECT_CLASS(l
->data
);
2760 printf(" %s\n", object_class_get_name(oc
));
2766 klass
= object_class_by_name(type
);
2767 if (klass
&& qemu_opt_has_help_opt(opts
)) {
2768 ObjectPropertyIterator iter
;
2769 ObjectProperty
*prop
;
2770 GPtrArray
*array
= g_ptr_array_new();
2773 object_class_property_iter_init(&iter
, klass
);
2774 while ((prop
= object_property_iter_next(&iter
))) {
2781 str
= g_string_new(NULL
);
2782 g_string_append_printf(str
, " %s=<%s>", prop
->name
, prop
->type
);
2783 if (prop
->description
) {
2784 if (str
->len
< 24) {
2785 g_string_append_printf(str
, "%*s", 24 - (int)str
->len
, "");
2787 g_string_append_printf(str
, " - %s", prop
->description
);
2789 g_ptr_array_add(array
, g_string_free(str
, false));
2791 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
2792 if (array
->len
> 0) {
2793 printf("%s options:\n", type
);
2795 printf("There are no options for %s.\n", type
);
2797 for (i
= 0; i
< array
->len
; i
++) {
2798 printf("%s\n", (char *)array
->pdata
[i
]);
2800 g_ptr_array_set_free_func(array
, g_free
);
2801 g_ptr_array_free(array
, true);
2805 if (g_str_equal(type
, "rng-egd") ||
2806 g_str_has_prefix(type
, "pr-manager-")) {
2810 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2811 if (g_str_equal(type
, "cryptodev-vhost-user")) {
2817 * return false for concrete netfilters since
2818 * they depend on netdevs already existing
2820 if (g_str_equal(type
, "filter-buffer") ||
2821 g_str_equal(type
, "filter-dump") ||
2822 g_str_equal(type
, "filter-mirror") ||
2823 g_str_equal(type
, "filter-redirector") ||
2824 g_str_equal(type
, "colo-compare") ||
2825 g_str_equal(type
, "filter-rewriter") ||
2826 g_str_equal(type
, "filter-replay")) {
2830 /* Memory allocation by backends needs to be done
2831 * after configure_accelerator() (due to the tcg_enabled()
2832 * checks at memory_region_init_*()).
2834 * Also, allocation of large amounts of memory may delay
2835 * chardev initialization for too long, and trigger timeouts
2836 * on software that waits for a monitor socket to be created
2839 if (g_str_has_prefix(type
, "memory-backend-")) {
2848 * The remainder of object creation happens after the
2849 * creation of chardev, fsdev, net clients and device data types.
2851 static bool object_create_delayed(const char *type
, QemuOpts
*opts
)
2853 return !object_create_initial(type
, opts
);
2857 static void set_memory_options(uint64_t *ram_slots
, ram_addr_t
*maxram_size
,
2861 const char *mem_str
;
2862 const ram_addr_t default_ram_size
= mc
->default_ram_size
;
2863 QemuOpts
*opts
= qemu_find_opts_singleton("memory");
2866 loc_push_none(&loc
);
2867 qemu_opts_loc_restore(opts
);
2870 mem_str
= qemu_opt_get(opts
, "size");
2873 error_report("missing 'size' option value");
2877 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
2879 /* Fix up legacy suffix-less format */
2880 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
2881 uint64_t overflow_check
= sz
;
2884 if (sz
/ MiB
!= overflow_check
) {
2885 error_report("too large 'size' option value");
2891 /* backward compatibility behaviour for case "-m 0" */
2893 sz
= default_ram_size
;
2896 sz
= QEMU_ALIGN_UP(sz
, 8192);
2898 if (ram_size
!= sz
) {
2899 error_report("ram size too large");
2903 /* store value for the future use */
2904 qemu_opt_set_number(opts
, "size", ram_size
, &error_abort
);
2905 *maxram_size
= ram_size
;
2907 if (qemu_opt_get(opts
, "maxmem")) {
2910 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
2911 slots
= qemu_opt_get_number(opts
, "slots", 0);
2912 if (sz
< ram_size
) {
2913 error_report("invalid value of -m option maxmem: "
2914 "maximum memory size (0x%" PRIx64
") must be at least "
2915 "the initial memory size (0x" RAM_ADDR_FMT
")",
2918 } else if (slots
&& sz
== ram_size
) {
2919 error_report("invalid value of -m option maxmem: "
2920 "memory slots were specified but maximum memory size "
2921 "(0x%" PRIx64
") is equal to the initial memory size "
2922 "(0x" RAM_ADDR_FMT
")", sz
, ram_size
);
2928 } else if (qemu_opt_get(opts
, "slots")) {
2929 error_report("invalid -m option value: missing 'maxmem' option");
2936 static int global_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2940 g
= g_malloc0(sizeof(*g
));
2941 g
->driver
= qemu_opt_get(opts
, "driver");
2942 g
->property
= qemu_opt_get(opts
, "property");
2943 g
->value
= qemu_opt_get(opts
, "value");
2944 g
->user_provided
= true;
2945 g
->errp
= &error_fatal
;
2946 qdev_prop_register_global(g
);
2950 static int qemu_read_default_config_file(void)
2954 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2955 if (ret
< 0 && ret
!= -ENOENT
) {
2962 static void user_register_global_props(void)
2964 qemu_opts_foreach(qemu_find_opts("global"),
2965 global_init_func
, NULL
, NULL
);
2969 * Note: we should see that these properties are actually having a
2970 * priority: accel < machine < user. This means e.g. when user
2971 * specifies something in "-global", it'll always be used with highest
2972 * priority than either machine/accelerator compat properties.
2974 static void register_global_properties(MachineState
*ms
)
2976 accel_register_compat_props(ms
->accelerator
);
2977 machine_register_compat_props(ms
);
2978 user_register_global_props();
2981 int main(int argc
, char **argv
)
2984 int snapshot
, linux_boot
;
2985 const char *initrd_filename
;
2986 const char *kernel_filename
, *kernel_cmdline
;
2987 const char *boot_order
= NULL
;
2988 const char *boot_once
= NULL
;
2990 QemuOpts
*opts
, *machine_opts
;
2991 QemuOpts
*icount_opts
= NULL
, *accel_opts
= NULL
;
2992 QemuOptsList
*olist
;
2995 const char *loadvm
= NULL
;
2996 MachineClass
*machine_class
;
2997 const char *cpu_model
;
2998 const char *vga_model
= NULL
;
2999 const char *qtest_chrdev
= NULL
;
3000 const char *qtest_log
= NULL
;
3001 const char *incoming
= NULL
;
3002 bool userconfig
= true;
3003 bool nographic
= false;
3004 int display_remote
= 0;
3005 const char *log_mask
= NULL
;
3006 const char *log_file
= NULL
;
3007 char *trace_file
= NULL
;
3008 ram_addr_t maxram_size
;
3009 uint64_t ram_slots
= 0;
3010 FILE *vmstate_dump_file
= NULL
;
3011 Error
*main_loop_err
= NULL
;
3013 bool list_data_dirs
= false;
3015 typedef struct BlockdevOptions_queue
{
3016 BlockdevOptions
*bdo
;
3018 QSIMPLEQ_ENTRY(BlockdevOptions_queue
) entry
;
3019 } BlockdevOptions_queue
;
3020 QSIMPLEQ_HEAD(, BlockdevOptions_queue
) bdo_queue
3021 = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue
);
3023 module_call_init(MODULE_INIT_TRACE
);
3025 qemu_init_cpu_list();
3026 qemu_init_cpu_loop();
3028 qemu_mutex_lock_iothread();
3030 atexit(qemu_run_exit_notifiers
);
3031 error_set_progname(argv
[0]);
3032 qemu_init_exec_dir(argv
[0]);
3034 module_call_init(MODULE_INIT_QOM
);
3036 qemu_add_opts(&qemu_drive_opts
);
3037 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
3038 qemu_add_drive_opts(&qemu_common_drive_opts
);
3039 qemu_add_drive_opts(&qemu_drive_opts
);
3040 qemu_add_drive_opts(&bdrv_runtime_opts
);
3041 qemu_add_opts(&qemu_chardev_opts
);
3042 qemu_add_opts(&qemu_device_opts
);
3043 qemu_add_opts(&qemu_netdev_opts
);
3044 qemu_add_opts(&qemu_nic_opts
);
3045 qemu_add_opts(&qemu_net_opts
);
3046 qemu_add_opts(&qemu_rtc_opts
);
3047 qemu_add_opts(&qemu_global_opts
);
3048 qemu_add_opts(&qemu_mon_opts
);
3049 qemu_add_opts(&qemu_trace_opts
);
3050 qemu_add_opts(&qemu_option_rom_opts
);
3051 qemu_add_opts(&qemu_machine_opts
);
3052 qemu_add_opts(&qemu_accel_opts
);
3053 qemu_add_opts(&qemu_mem_opts
);
3054 qemu_add_opts(&qemu_smp_opts
);
3055 qemu_add_opts(&qemu_boot_opts
);
3056 qemu_add_opts(&qemu_add_fd_opts
);
3057 qemu_add_opts(&qemu_object_opts
);
3058 qemu_add_opts(&qemu_tpmdev_opts
);
3059 qemu_add_opts(&qemu_realtime_opts
);
3060 qemu_add_opts(&qemu_overcommit_opts
);
3061 qemu_add_opts(&qemu_msg_opts
);
3062 qemu_add_opts(&qemu_name_opts
);
3063 qemu_add_opts(&qemu_numa_opts
);
3064 qemu_add_opts(&qemu_icount_opts
);
3065 qemu_add_opts(&qemu_semihosting_config_opts
);
3066 qemu_add_opts(&qemu_fw_cfg_opts
);
3067 module_call_init(MODULE_INIT_OPTS
);
3070 postcopy_infrastructure_init();
3071 monitor_init_globals();
3073 if (qcrypto_init(&err
) < 0) {
3074 error_reportf_err(err
, "cannot initialize crypto: ");
3078 QLIST_INIT (&vm_change_state_head
);
3079 os_setup_early_signal_handling();
3086 bdrv_init_with_whitelist();
3090 /* first pass of option parsing */
3092 while (optind
< argc
) {
3093 if (argv
[optind
][0] != '-') {
3097 const QEMUOption
*popt
;
3099 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3100 switch (popt
->index
) {
3101 case QEMU_OPTION_nouserconfig
:
3109 if (qemu_read_default_config_file() < 0) {
3114 /* second pass of option parsing */
3119 if (argv
[optind
][0] != '-') {
3120 drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3122 const QEMUOption
*popt
;
3124 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3125 if (!(popt
->arch_mask
& arch_type
)) {
3126 error_report("Option not supported for this target");
3129 switch(popt
->index
) {
3130 case QEMU_OPTION_cpu
:
3131 /* hw initialization will check this */
3134 case QEMU_OPTION_hda
:
3135 case QEMU_OPTION_hdb
:
3136 case QEMU_OPTION_hdc
:
3137 case QEMU_OPTION_hdd
:
3138 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3141 case QEMU_OPTION_blockdev
:
3144 BlockdevOptions_queue
*bdo
;
3146 v
= qobject_input_visitor_new_str(optarg
, "driver", &err
);
3148 error_report_err(err
);
3152 bdo
= g_new(BlockdevOptions_queue
, 1);
3153 visit_type_BlockdevOptions(v
, NULL
, &bdo
->bdo
,
3156 loc_save(&bdo
->loc
);
3157 QSIMPLEQ_INSERT_TAIL(&bdo_queue
, bdo
, entry
);
3160 case QEMU_OPTION_drive
:
3161 if (drive_def(optarg
) == NULL
) {
3165 case QEMU_OPTION_set
:
3166 if (qemu_set_option(optarg
) != 0)
3169 case QEMU_OPTION_global
:
3170 if (qemu_global_option(optarg
) != 0)
3173 case QEMU_OPTION_mtdblock
:
3174 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3176 case QEMU_OPTION_sd
:
3177 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3179 case QEMU_OPTION_pflash
:
3180 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3182 case QEMU_OPTION_snapshot
:
3185 case QEMU_OPTION_numa
:
3186 opts
= qemu_opts_parse_noisily(qemu_find_opts("numa"),
3192 case QEMU_OPTION_display
:
3193 parse_display(optarg
);
3195 case QEMU_OPTION_nographic
:
3196 olist
= qemu_find_opts("machine");
3197 qemu_opts_parse_noisily(olist
, "graphics=off", false);
3199 dpy
.type
= DISPLAY_TYPE_NONE
;
3201 case QEMU_OPTION_curses
:
3202 #ifdef CONFIG_CURSES
3203 dpy
.type
= DISPLAY_TYPE_CURSES
;
3205 error_report("curses support is disabled");
3209 case QEMU_OPTION_portrait
:
3210 graphic_rotate
= 90;
3212 case QEMU_OPTION_rotate
:
3213 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3214 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3215 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3216 error_report("only 90, 180, 270 deg rotation is available");
3220 case QEMU_OPTION_kernel
:
3221 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
,
3224 case QEMU_OPTION_initrd
:
3225 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
,
3228 case QEMU_OPTION_append
:
3229 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
,
3232 case QEMU_OPTION_dtb
:
3233 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
,
3236 case QEMU_OPTION_cdrom
:
3237 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3239 case QEMU_OPTION_boot
:
3240 opts
= qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3246 case QEMU_OPTION_fda
:
3247 case QEMU_OPTION_fdb
:
3248 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3251 case QEMU_OPTION_no_fd_bootchk
:
3254 case QEMU_OPTION_netdev
:
3256 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3260 case QEMU_OPTION_nic
:
3262 if (net_client_parse(qemu_find_opts("nic"), optarg
) == -1) {
3266 case QEMU_OPTION_net
:
3268 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3272 #ifdef CONFIG_LIBISCSI
3273 case QEMU_OPTION_iscsi
:
3274 opts
= qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3281 case QEMU_OPTION_bt
:
3282 add_device_config(DEV_BT
, optarg
);
3284 case QEMU_OPTION_audio_help
:
3288 case QEMU_OPTION_soundhw
:
3289 select_soundhw (optarg
);
3294 case QEMU_OPTION_version
:
3299 opts
= qemu_opts_parse_noisily(qemu_find_opts("memory"),
3306 case QEMU_OPTION_tpmdev
:
3307 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3312 case QEMU_OPTION_mempath
:
3315 case QEMU_OPTION_mem_prealloc
:
3324 case QEMU_OPTION_DFILTER
:
3325 qemu_set_dfilter_ranges(optarg
, &error_fatal
);
3328 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3330 case QEMU_OPTION_gdb
:
3331 add_device_config(DEV_GDB
, optarg
);
3334 if (is_help_option(optarg
)) {
3335 list_data_dirs
= true;
3337 qemu_add_data_dir(optarg
);
3340 case QEMU_OPTION_bios
:
3341 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
,
3344 case QEMU_OPTION_singlestep
:
3351 keyboard_layout
= optarg
;
3353 case QEMU_OPTION_vga
:
3362 w
= strtol(p
, (char **)&p
, 10);
3365 error_report("invalid resolution or depth");
3371 h
= strtol(p
, (char **)&p
, 10);
3376 depth
= strtol(p
, (char **)&p
, 10);
3377 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3378 depth
!= 24 && depth
!= 32)
3380 } else if (*p
== '\0') {
3381 depth
= graphic_depth
;
3388 graphic_depth
= depth
;
3391 case QEMU_OPTION_echr
:
3394 term_escape_char
= strtol(optarg
, &r
, 0);
3396 printf("Bad argument to echr\n");
3399 case QEMU_OPTION_monitor
:
3400 default_monitor
= 0;
3401 if (strncmp(optarg
, "none", 4)) {
3402 monitor_parse(optarg
, "readline", false);
3405 case QEMU_OPTION_qmp
:
3406 monitor_parse(optarg
, "control", false);
3407 default_monitor
= 0;
3409 case QEMU_OPTION_qmp_pretty
:
3410 monitor_parse(optarg
, "control", true);
3411 default_monitor
= 0;
3413 case QEMU_OPTION_mon
:
3414 opts
= qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg
,
3419 default_monitor
= 0;
3421 case QEMU_OPTION_chardev
:
3422 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3428 case QEMU_OPTION_fsdev
:
3429 olist
= qemu_find_opts("fsdev");
3431 error_report("fsdev support is disabled");
3434 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3439 case QEMU_OPTION_virtfs
: {
3442 const char *writeout
, *sock_fd
, *socket
, *path
, *security_model
;
3444 olist
= qemu_find_opts("virtfs");
3446 error_report("virtfs support is disabled");
3449 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3454 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3455 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3456 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3459 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3460 qemu_opts_id(opts
) ?:
3461 qemu_opt_get(opts
, "mount_tag"),
3464 error_report("duplicate or invalid fsdev id: %s",
3465 qemu_opt_get(opts
, "mount_tag"));
3469 writeout
= qemu_opt_get(opts
, "writeout");
3471 #ifdef CONFIG_SYNC_FILE_RANGE
3472 qemu_opt_set(fsdev
, "writeout", writeout
, &error_abort
);
3474 error_report("writeout=immediate not supported "
3475 "on this platform");
3479 qemu_opt_set(fsdev
, "fsdriver",
3480 qemu_opt_get(opts
, "fsdriver"), &error_abort
);
3481 path
= qemu_opt_get(opts
, "path");
3483 qemu_opt_set(fsdev
, "path", path
, &error_abort
);
3485 security_model
= qemu_opt_get(opts
, "security_model");
3486 if (security_model
) {
3487 qemu_opt_set(fsdev
, "security_model", security_model
,
3490 socket
= qemu_opt_get(opts
, "socket");
3492 qemu_opt_set(fsdev
, "socket", socket
, &error_abort
);
3494 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3496 qemu_opt_set(fsdev
, "sock_fd", sock_fd
, &error_abort
);
3499 qemu_opt_set_bool(fsdev
, "readonly",
3500 qemu_opt_get_bool(opts
, "readonly", 0),
3502 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3504 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3505 qemu_opt_set(device
, "fsdev",
3506 qemu_opts_id(fsdev
), &error_abort
);
3507 qemu_opt_set(device
, "mount_tag",
3508 qemu_opt_get(opts
, "mount_tag"), &error_abort
);
3511 case QEMU_OPTION_virtfs_synth
: {
3515 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3518 error_report("duplicate option: %s", "virtfs_synth");
3521 qemu_opt_set(fsdev
, "fsdriver", "synth", &error_abort
);
3523 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3525 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3526 qemu_opt_set(device
, "fsdev", "v_synth", &error_abort
);
3527 qemu_opt_set(device
, "mount_tag", "v_synth", &error_abort
);
3530 case QEMU_OPTION_serial
:
3531 add_device_config(DEV_SERIAL
, optarg
);
3533 if (strncmp(optarg
, "mon:", 4) == 0) {
3534 default_monitor
= 0;
3537 case QEMU_OPTION_watchdog
:
3539 error_report("only one watchdog option may be given");
3544 case QEMU_OPTION_watchdog_action
:
3545 if (select_watchdog_action(optarg
) == -1) {
3546 error_report("unknown -watchdog-action parameter");
3550 case QEMU_OPTION_virtiocon
:
3551 warn_report("This option is deprecated, "
3552 "use '-device virtconsole' instead");
3553 add_device_config(DEV_VIRTCON
, optarg
);
3554 default_virtcon
= 0;
3555 if (strncmp(optarg
, "mon:", 4) == 0) {
3556 default_monitor
= 0;
3559 case QEMU_OPTION_parallel
:
3560 add_device_config(DEV_PARALLEL
, optarg
);
3561 default_parallel
= 0;
3562 if (strncmp(optarg
, "mon:", 4) == 0) {
3563 default_monitor
= 0;
3566 case QEMU_OPTION_debugcon
:
3567 add_device_config(DEV_DEBUGCON
, optarg
);
3569 case QEMU_OPTION_loadvm
:
3572 case QEMU_OPTION_full_screen
:
3573 dpy
.has_full_screen
= true;
3574 dpy
.full_screen
= true;
3576 case QEMU_OPTION_no_frame
:
3577 g_printerr("The -no-frame switch is deprecated, and will be\n"
3578 "removed in a future release.\n");
3581 case QEMU_OPTION_alt_grab
:
3584 case QEMU_OPTION_ctrl_grab
:
3587 case QEMU_OPTION_no_quit
:
3588 dpy
.has_window_close
= true;
3589 dpy
.window_close
= false;
3591 case QEMU_OPTION_sdl
:
3593 dpy
.type
= DISPLAY_TYPE_SDL
;
3596 error_report("SDL support is disabled");
3599 case QEMU_OPTION_pidfile
:
3602 case QEMU_OPTION_win2k_hack
:
3603 win2k_install_hack
= 1;
3605 case QEMU_OPTION_acpitable
:
3606 opts
= qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3611 acpi_table_add(opts
, &error_fatal
);
3613 case QEMU_OPTION_smbios
:
3614 opts
= qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3619 smbios_entry_add(opts
, &error_fatal
);
3621 case QEMU_OPTION_fwcfg
:
3622 opts
= qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3628 case QEMU_OPTION_preconfig
:
3629 preconfig_exit_requested
= false;
3631 case QEMU_OPTION_enable_kvm
:
3632 olist
= qemu_find_opts("machine");
3633 qemu_opts_parse_noisily(olist
, "accel=kvm", false);
3635 case QEMU_OPTION_enable_hax
:
3636 warn_report("Option is deprecated, use '-accel hax' instead");
3637 olist
= qemu_find_opts("machine");
3638 qemu_opts_parse_noisily(olist
, "accel=hax", false);
3641 case QEMU_OPTION_machine
:
3642 olist
= qemu_find_opts("machine");
3643 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3648 case QEMU_OPTION_no_kvm
:
3649 olist
= qemu_find_opts("machine");
3650 qemu_opts_parse_noisily(olist
, "accel=tcg", false);
3652 case QEMU_OPTION_accel
:
3653 accel_opts
= qemu_opts_parse_noisily(qemu_find_opts("accel"),
3655 optarg
= qemu_opt_get(accel_opts
, "accel");
3656 if (!optarg
|| is_help_option(optarg
)) {
3657 error_printf("Possible accelerators: kvm, xen, hax, tcg\n");
3660 opts
= qemu_opts_create(qemu_find_opts("machine"), NULL
,
3661 false, &error_abort
);
3662 qemu_opt_set(opts
, "accel", optarg
, &error_abort
);
3664 case QEMU_OPTION_usb
:
3665 olist
= qemu_find_opts("machine");
3666 qemu_opts_parse_noisily(olist
, "usb=on", false);
3668 case QEMU_OPTION_usbdevice
:
3669 error_report("'-usbdevice' is deprecated, please use "
3670 "'-device usb-...' instead");
3671 olist
= qemu_find_opts("machine");
3672 qemu_opts_parse_noisily(olist
, "usb=on", false);
3673 add_device_config(DEV_USB
, optarg
);
3675 case QEMU_OPTION_device
:
3676 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3681 case QEMU_OPTION_smp
:
3682 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3687 case QEMU_OPTION_vnc
:
3688 vnc_parse(optarg
, &error_fatal
);
3690 case QEMU_OPTION_no_acpi
:
3693 case QEMU_OPTION_no_hpet
:
3696 case QEMU_OPTION_no_reboot
:
3699 case QEMU_OPTION_no_shutdown
:
3702 case QEMU_OPTION_show_cursor
:
3705 case QEMU_OPTION_uuid
:
3706 if (qemu_uuid_parse(optarg
, &qemu_uuid
) < 0) {
3707 error_report("failed to parse UUID string: wrong format");
3710 qemu_uuid_set
= true;
3712 case QEMU_OPTION_option_rom
:
3713 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3714 error_report("too many option ROMs");
3717 opts
= qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3722 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3723 option_rom
[nb_option_roms
].bootindex
=
3724 qemu_opt_get_number(opts
, "bootindex", -1);
3725 if (!option_rom
[nb_option_roms
].name
) {
3726 error_report("Option ROM file is not specified");
3731 case QEMU_OPTION_semihosting
:
3732 semihosting
.enabled
= true;
3733 semihosting
.target
= SEMIHOSTING_TARGET_AUTO
;
3735 case QEMU_OPTION_semihosting_config
:
3736 semihosting
.enabled
= true;
3737 opts
= qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3740 semihosting
.enabled
= qemu_opt_get_bool(opts
, "enable",
3742 const char *target
= qemu_opt_get(opts
, "target");
3743 if (target
!= NULL
) {
3744 if (strcmp("native", target
) == 0) {
3745 semihosting
.target
= SEMIHOSTING_TARGET_NATIVE
;
3746 } else if (strcmp("gdb", target
) == 0) {
3747 semihosting
.target
= SEMIHOSTING_TARGET_GDB
;
3748 } else if (strcmp("auto", target
) == 0) {
3749 semihosting
.target
= SEMIHOSTING_TARGET_AUTO
;
3751 error_report("unsupported semihosting-config %s",
3756 semihosting
.target
= SEMIHOSTING_TARGET_AUTO
;
3758 /* Set semihosting argument count and vector */
3759 qemu_opt_foreach(opts
, add_semihosting_arg
,
3760 &semihosting
, NULL
);
3762 error_report("unsupported semihosting-config %s", optarg
);
3766 case QEMU_OPTION_name
:
3767 opts
= qemu_opts_parse_noisily(qemu_find_opts("name"),
3773 case QEMU_OPTION_prom_env
:
3774 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3775 error_report("too many prom variables");
3778 prom_envs
[nb_prom_envs
] = optarg
;
3781 case QEMU_OPTION_old_param
:
3784 case QEMU_OPTION_clock
:
3785 /* Clock options no longer exist. Keep this option for
3786 * backward compatibility.
3788 warn_report("This option is ignored and will be removed soon");
3790 case QEMU_OPTION_rtc
:
3791 opts
= qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg
,
3797 case QEMU_OPTION_tb_size
:
3799 error_report("TCG is disabled");
3802 if (qemu_strtoul(optarg
, NULL
, 0, &tcg_tb_size
) < 0) {
3803 error_report("Invalid argument to -tb-size");
3807 case QEMU_OPTION_icount
:
3808 icount_opts
= qemu_opts_parse_noisily(qemu_find_opts("icount"),
3814 case QEMU_OPTION_incoming
:
3816 runstate_set(RUN_STATE_INMIGRATE
);
3820 case QEMU_OPTION_only_migratable
:
3822 * TODO: we can remove this option one day, and we
3825 * "-global migration.only-migratable=true"
3827 qemu_global_option("migration.only-migratable=true");
3829 case QEMU_OPTION_nodefaults
:
3832 case QEMU_OPTION_xen_domid
:
3833 if (!(xen_available())) {
3834 error_report("Option not supported for this target");
3837 xen_domid
= atoi(optarg
);
3839 case QEMU_OPTION_xen_create
:
3840 if (!(xen_available())) {
3841 error_report("Option not supported for this target");
3844 xen_mode
= XEN_CREATE
;
3846 case QEMU_OPTION_xen_attach
:
3847 if (!(xen_available())) {
3848 error_report("Option not supported for this target");
3851 xen_mode
= XEN_ATTACH
;
3853 case QEMU_OPTION_xen_domid_restrict
:
3854 if (!(xen_available())) {
3855 error_report("Option not supported for this target");
3858 xen_domid_restrict
= true;
3860 case QEMU_OPTION_trace
:
3862 trace_file
= trace_opt_parse(optarg
);
3864 case QEMU_OPTION_trace_unassigned
:
3865 trace_unassigned
= true;
3867 case QEMU_OPTION_readconfig
:
3869 int ret
= qemu_read_config_file(optarg
);
3871 error_report("read config %s: %s", optarg
,
3877 case QEMU_OPTION_spice
:
3878 olist
= qemu_find_opts("spice");
3880 error_report("spice support is disabled");
3883 opts
= qemu_opts_parse_noisily(olist
, optarg
, false);
3889 case QEMU_OPTION_writeconfig
:
3892 if (strcmp(optarg
, "-") == 0) {
3895 fp
= fopen(optarg
, "w");
3897 error_report("open %s: %s", optarg
,
3902 qemu_config_write(fp
);
3908 case QEMU_OPTION_qtest
:
3909 qtest_chrdev
= optarg
;
3911 case QEMU_OPTION_qtest_log
:
3914 case QEMU_OPTION_sandbox
:
3915 #ifdef CONFIG_SECCOMP
3916 opts
= qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3922 error_report("-sandbox support is not enabled "
3923 "in this QEMU binary");
3927 case QEMU_OPTION_add_fd
:
3929 opts
= qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3935 error_report("File descriptor passing is disabled on this "
3940 case QEMU_OPTION_object
:
3941 opts
= qemu_opts_parse_noisily(qemu_find_opts("object"),
3947 case QEMU_OPTION_realtime
:
3948 opts
= qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3953 /* Don't override the -overcommit option if set */
3954 enable_mlock
= enable_mlock
||
3955 qemu_opt_get_bool(opts
, "mlock", true);
3957 case QEMU_OPTION_overcommit
:
3958 opts
= qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3963 /* Don't override the -realtime option if set */
3964 enable_mlock
= enable_mlock
||
3965 qemu_opt_get_bool(opts
, "mem-lock", false);
3966 enable_cpu_pm
= qemu_opt_get_bool(opts
, "cpu-pm", false);
3968 case QEMU_OPTION_msg
:
3969 opts
= qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg
,
3974 configure_msg(opts
);
3976 case QEMU_OPTION_dump_vmstate
:
3977 if (vmstate_dump_file
) {
3978 error_report("only one '-dump-vmstate' "
3979 "option may be given");
3982 vmstate_dump_file
= fopen(optarg
, "w");
3983 if (vmstate_dump_file
== NULL
) {
3984 error_report("open %s: %s", optarg
, strerror(errno
));
3988 case QEMU_OPTION_enable_sync_profile
:
3991 case QEMU_OPTION_nouserconfig
:
3992 /* Nothing to be parsed here. Especially, do not error out below. */
3995 if (os_parse_cmd_args(popt
->index
, optarg
)) {
3996 error_report("Option not supported in this build");
4003 * Clear error location left behind by the loop.
4004 * Best done right after the loop. Do not insert code here!
4008 replay_configure(icount_opts
);
4010 if (incoming
&& !preconfig_exit_requested
) {
4011 error_report("'preconfig' and 'incoming' options are "
4012 "mutually exclusive");
4016 configure_rtc(qemu_find_opts_singleton("rtc"));
4018 machine_class
= select_machine();
4020 set_memory_options(&ram_slots
, &maxram_size
, machine_class
);
4023 rcu_disable_atfork();
4025 if (pid_file
&& !qemu_write_pidfile(pid_file
, &err
)) {
4026 error_reportf_err(err
, "cannot create PID file: ");
4030 qemu_unlink_pidfile_notifier
.notify
= qemu_unlink_pidfile
;
4031 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier
);
4033 if (qemu_init_main_loop(&main_loop_err
)) {
4034 error_report_err(main_loop_err
);
4038 #ifdef CONFIG_SECCOMP
4039 olist
= qemu_find_opts_err("sandbox", NULL
);
4041 qemu_opts_foreach(olist
, parse_sandbox
, NULL
, &error_fatal
);
4045 qemu_opts_foreach(qemu_find_opts("name"),
4046 parse_name
, NULL
, &error_fatal
);
4049 qemu_opts_foreach(qemu_find_opts("add-fd"),
4050 parse_add_fd
, NULL
, &error_fatal
);
4052 qemu_opts_foreach(qemu_find_opts("add-fd"),
4053 cleanup_add_fd
, NULL
, &error_fatal
);
4056 current_machine
= MACHINE(object_new(object_class_get_name(
4057 OBJECT_CLASS(machine_class
))));
4058 if (machine_help_func(qemu_get_machine_opts(), current_machine
)) {
4061 object_property_add_child(object_get_root(), "machine",
4062 OBJECT(current_machine
), &error_abort
);
4064 if (machine_class
->minimum_page_bits
) {
4065 if (!set_preferred_target_page_bits(machine_class
->minimum_page_bits
)) {
4066 /* This would be a board error: specifying a minimum smaller than
4067 * a target's compile-time fixed setting.
4069 g_assert_not_reached();
4073 cpu_exec_init_all();
4075 if (machine_class
->hw_version
) {
4076 qemu_set_hw_version(machine_class
->hw_version
);
4079 if (cpu_model
&& is_help_option(cpu_model
)) {
4080 list_cpus(stdout
, &fprintf
, cpu_model
);
4084 if (!trace_init_backends()) {
4087 trace_init_file(trace_file
);
4089 /* Open the logfile at this point and set the log mask if necessary.
4092 qemu_set_log_filename(log_file
, &error_fatal
);
4097 mask
= qemu_str_to_log_mask(log_mask
);
4099 qemu_print_log_usage(stdout
);
4107 /* add configured firmware directories */
4108 dirs
= g_strsplit(CONFIG_QEMU_FIRMWAREPATH
, G_SEARCHPATH_SEPARATOR_S
, 0);
4109 for (i
= 0; dirs
[i
] != NULL
; i
++) {
4110 qemu_add_data_dir(dirs
[i
]);
4114 /* try to find datadir relative to the executable path */
4115 dir
= os_find_datadir();
4116 qemu_add_data_dir(dir
);
4119 /* add the datadir specified when building */
4120 qemu_add_data_dir(CONFIG_QEMU_DATADIR
);
4122 /* -L help lists the data directories and exits. */
4123 if (list_data_dirs
) {
4124 for (i
= 0; i
< data_dir_idx
; i
++) {
4125 printf("%s\n", data_dir
[i
]);
4130 /* machine_class: default to UP */
4131 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1;
4132 machine_class
->min_cpus
= machine_class
->min_cpus
?: 1;
4133 machine_class
->default_cpus
= machine_class
->default_cpus
?: 1;
4135 /* default to machine_class->default_cpus */
4136 smp_cpus
= machine_class
->default_cpus
;
4137 max_cpus
= machine_class
->default_cpus
;
4139 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4141 /* sanity-check smp_cpus and max_cpus against machine_class */
4142 if (smp_cpus
< machine_class
->min_cpus
) {
4143 error_report("Invalid SMP CPUs %d. The min CPUs "
4144 "supported by machine '%s' is %d", smp_cpus
,
4145 machine_class
->name
, machine_class
->min_cpus
);
4148 if (max_cpus
> machine_class
->max_cpus
) {
4149 error_report("Invalid SMP CPUs %d. The max CPUs "
4150 "supported by machine '%s' is %d", max_cpus
,
4151 machine_class
->name
, machine_class
->max_cpus
);
4156 * Get the default machine options from the machine if it is not already
4157 * specified either by the configuration file or by the command line.
4159 if (machine_class
->default_machine_opts
) {
4160 qemu_opts_set_defaults(qemu_find_opts("machine"),
4161 machine_class
->default_machine_opts
, 0);
4164 qemu_opts_foreach(qemu_find_opts("device"),
4165 default_driver_check
, NULL
, NULL
);
4166 qemu_opts_foreach(qemu_find_opts("global"),
4167 default_driver_check
, NULL
, NULL
);
4169 if (!vga_model
&& !default_vga
) {
4170 vga_interface_type
= VGA_DEVICE
;
4172 if (!has_defaults
|| machine_class
->no_serial
) {
4175 if (!has_defaults
|| machine_class
->no_parallel
) {
4176 default_parallel
= 0;
4178 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4179 default_virtcon
= 0;
4181 if (!has_defaults
|| machine_class
->no_floppy
) {
4184 if (!has_defaults
|| machine_class
->no_cdrom
) {
4187 if (!has_defaults
|| machine_class
->no_sdcard
) {
4190 if (!has_defaults
) {
4191 default_monitor
= 0;
4196 if (is_daemonized()) {
4197 if (!preconfig_exit_requested
) {
4198 error_report("'preconfig' and 'daemonize' options are "
4199 "mutually exclusive");
4203 /* According to documentation and historically, -nographic redirects
4204 * serial port, parallel port and monitor to stdio, which does not work
4205 * with -daemonize. We can redirect these to null instead, but since
4206 * -nographic is legacy, let's just error out.
4207 * We disallow -nographic only if all other ports are not redirected
4208 * explicitly, to not break existing legacy setups which uses
4209 * -nographic _and_ redirects all ports explicitly - this is valid
4210 * usage, -nographic is just a no-op in this case.
4213 && (default_parallel
|| default_serial
4214 || default_monitor
|| default_virtcon
)) {
4215 error_report("-nographic cannot be used with -daemonize");
4218 #ifdef CONFIG_CURSES
4219 if (dpy
.type
== DISPLAY_TYPE_CURSES
) {
4220 error_report("curses display cannot be used with -daemonize");
4227 if (default_parallel
)
4228 add_device_config(DEV_PARALLEL
, "null");
4229 if (default_serial
&& default_monitor
) {
4230 add_device_config(DEV_SERIAL
, "mon:stdio");
4231 } else if (default_virtcon
&& default_monitor
) {
4232 add_device_config(DEV_VIRTCON
, "mon:stdio");
4235 add_device_config(DEV_SERIAL
, "stdio");
4236 if (default_virtcon
)
4237 add_device_config(DEV_VIRTCON
, "stdio");
4238 if (default_monitor
)
4239 monitor_parse("stdio", "readline", false);
4243 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4244 if (default_parallel
)
4245 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4246 if (default_monitor
)
4247 monitor_parse("vc:80Cx24C", "readline", false);
4248 if (default_virtcon
)
4249 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4252 #if defined(CONFIG_VNC)
4253 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head
))) {
4257 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
&& !display_remote
) {
4258 if (!qemu_display_find_default(&dpy
)) {
4259 dpy
.type
= DISPLAY_TYPE_NONE
;
4260 #if defined(CONFIG_VNC)
4261 vnc_parse("localhost:0,to=99,id=default", &error_abort
);
4265 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
) {
4266 dpy
.type
= DISPLAY_TYPE_NONE
;
4269 if ((no_frame
|| alt_grab
|| ctrl_grab
) && dpy
.type
!= DISPLAY_TYPE_SDL
) {
4270 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4271 "for SDL, ignoring option");
4273 if (dpy
.has_window_close
&&
4274 (dpy
.type
!= DISPLAY_TYPE_GTK
&& dpy
.type
!= DISPLAY_TYPE_SDL
)) {
4275 error_report("-no-quit is only valid for GTK and SDL, "
4279 qemu_display_early_init(&dpy
);
4280 qemu_console_early_init();
4282 if (dpy
.has_gl
&& dpy
.gl
!= DISPLAYGL_MODE_OFF
&& display_opengl
== 0) {
4283 #if defined(CONFIG_OPENGL)
4284 error_report("OpenGL is not supported by the display");
4286 error_report("OpenGL support is disabled");
4294 qemu_opts_foreach(qemu_find_opts("object"),
4295 user_creatable_add_opts_foreach
,
4296 object_create_initial
, &error_fatal
);
4298 qemu_opts_foreach(qemu_find_opts("chardev"),
4299 chardev_init_func
, NULL
, &error_fatal
);
4301 #ifdef CONFIG_VIRTFS
4302 qemu_opts_foreach(qemu_find_opts("fsdev"),
4303 fsdev_init_func
, NULL
, &error_fatal
);
4306 if (qemu_opts_foreach(qemu_find_opts("device"),
4307 device_help_func
, NULL
, NULL
)) {
4311 machine_opts
= qemu_get_machine_opts();
4312 qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4315 configure_accelerator(current_machine
);
4317 if (!qtest_enabled() && machine_class
->deprecation_reason
) {
4318 error_report("Machine type '%s' is deprecated: %s",
4319 machine_class
->name
, machine_class
->deprecation_reason
);
4323 * Register all the global properties, including accel properties,
4324 * machine properties, and user-specified ones.
4326 register_global_properties(current_machine
);
4329 * Migration object can only be created after global properties
4330 * are applied correctly.
4332 migration_object_init();
4335 qtest_init(qtest_chrdev
, qtest_log
, &error_fatal
);
4338 machine_opts
= qemu_get_machine_opts();
4339 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4340 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4341 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4342 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4344 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4346 boot_order
= qemu_opt_get(opts
, "order");
4348 validate_bootdevices(boot_order
, &error_fatal
);
4351 boot_once
= qemu_opt_get(opts
, "once");
4353 validate_bootdevices(boot_once
, &error_fatal
);
4356 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4357 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4361 boot_order
= machine_class
->default_boot_order
;
4364 if (!kernel_cmdline
) {
4365 kernel_cmdline
= "";
4366 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4369 linux_boot
= (kernel_filename
!= NULL
);
4371 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4372 error_report("-append only allowed with -kernel option");
4376 if (!linux_boot
&& initrd_filename
!= NULL
) {
4377 error_report("-initrd only allowed with -kernel option");
4381 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename
) {
4382 /* fall back to the -kernel/-append */
4383 semihosting_arg_fallback(kernel_filename
, kernel_cmdline
);
4386 os_set_line_buffering();
4388 /* spice needs the timers to be initialized by this point */
4393 if (!tcg_enabled()) {
4394 error_report("-icount is not allowed with hardware virtualization");
4397 configure_icount(icount_opts
, &error_abort
);
4398 qemu_opts_del(icount_opts
);
4401 if (tcg_enabled()) {
4402 qemu_tcg_configure(accel_opts
, &error_fatal
);
4406 QemuOptsList
*net
= qemu_find_opts("net");
4407 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
4409 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
4413 if (net_init_clients(&err
) < 0) {
4414 error_report_err(err
);
4418 qemu_opts_foreach(qemu_find_opts("object"),
4419 user_creatable_add_opts_foreach
,
4420 object_create_delayed
, &error_fatal
);
4424 /* init the bluetooth world */
4425 if (foreach_device_config(DEV_BT
, bt_parse
))
4428 if (!xen_enabled()) {
4429 /* On 32-bit hosts, QEMU is limited by virtual address space */
4430 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4431 error_report("at most 2047 MB RAM can be simulated");
4438 dirty_bitmap_mig_init();
4440 /* If the currently selected machine wishes to override the units-per-bus
4441 * property of its default HBA interface type, do so now. */
4442 if (machine_class
->units_per_default_bus
) {
4443 override_max_devs(machine_class
->block_default_type
,
4444 machine_class
->units_per_default_bus
);
4447 /* open the virtual block devices */
4448 while (!QSIMPLEQ_EMPTY(&bdo_queue
)) {
4449 BlockdevOptions_queue
*bdo
= QSIMPLEQ_FIRST(&bdo_queue
);
4451 QSIMPLEQ_REMOVE_HEAD(&bdo_queue
, entry
);
4452 loc_push_restore(&bdo
->loc
);
4453 qmp_blockdev_add(bdo
->bdo
, &error_fatal
);
4455 qapi_free_BlockdevOptions(bdo
->bdo
);
4458 if (snapshot
|| replay_mode
!= REPLAY_MODE_NONE
) {
4459 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
,
4462 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4463 &machine_class
->block_default_type
, &error_fatal
)) {
4464 /* We printed help */
4468 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4470 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4471 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4473 qemu_opts_foreach(qemu_find_opts("mon"),
4474 mon_init_func
, NULL
, &error_fatal
);
4476 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4478 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4480 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4482 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4485 /* If no default VGA is requested, the default is "none". */
4487 if (machine_class
->default_display
) {
4488 vga_model
= machine_class
->default_display
;
4489 } else if (vga_interface_available(VGA_CIRRUS
)) {
4490 vga_model
= "cirrus";
4491 } else if (vga_interface_available(VGA_STD
)) {
4496 select_vgahw(vga_model
);
4500 i
= select_watchdog(watchdog
);
4502 exit (i
== 1 ? 1 : 0);
4505 /* This checkpoint is required by replay to separate prior clock
4506 reading from the other reads, because timer polling functions query
4507 clock values from the log. */
4508 replay_checkpoint(CHECKPOINT_INIT
);
4509 qdev_machine_init();
4511 current_machine
->ram_size
= ram_size
;
4512 current_machine
->maxram_size
= maxram_size
;
4513 current_machine
->ram_slots
= ram_slots
;
4514 current_machine
->boot_order
= boot_order
;
4516 /* parse features once if machine provides default cpu_type */
4517 current_machine
->cpu_type
= machine_class
->default_cpu_type
;
4519 current_machine
->cpu_type
= parse_cpu_model(cpu_model
);
4521 parse_numa_opts(current_machine
);
4523 /* do monitor/qmp handling at preconfig state if requested */
4526 /* from here on runstate is RUN_STATE_PRELAUNCH */
4527 machine_run_board_init(current_machine
);
4533 if (hax_enabled()) {
4537 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4538 parse_fw_cfg
, fw_cfg_find(), &error_fatal
);
4540 /* init USB devices */
4541 if (machine_usb(current_machine
)) {
4542 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4546 /* Check if IGD GFX passthrough. */
4549 /* init generic devices */
4550 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE
);
4551 qemu_opts_foreach(qemu_find_opts("device"),
4552 device_init_func
, NULL
, &error_fatal
);
4554 cpu_synchronize_all_post_init();
4556 rom_reset_order_override();
4558 /* Did we create any drives that we failed to create a device for? */
4559 drive_check_orphaned();
4561 /* Don't warn about the default network setup that you get if
4562 * no command line -net or -netdev options are specified. There
4563 * are two cases that we would otherwise complain about:
4564 * (1) board doesn't support a NIC but the implicit "-net nic"
4566 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4567 * sets up a nic that isn't connected to anything.
4569 if (!default_net
&& (!qtest_enabled() || has_defaults
)) {
4570 net_check_clients();
4574 qemu_boot_set(boot_once
, &error_fatal
);
4575 qemu_register_reset(restore_boot_order
, g_strdup(boot_order
));
4578 /* init local displays */
4579 ds
= init_displaystate();
4580 qemu_display_init(ds
, &dpy
);
4582 /* must be after terminal init, SDL library changes signal handlers */
4583 os_setup_signal_handling();
4585 /* init remote displays */
4587 qemu_opts_foreach(qemu_find_opts("vnc"),
4588 vnc_init_func
, NULL
, &error_fatal
);
4592 qemu_spice_display_init();
4595 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4599 qdev_machine_creation_done();
4601 /* TODO: once all bus devices are qdevified, this should be done
4602 * when bus is created by qdev.c */
4603 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4604 qemu_run_machine_init_done_notifiers();
4606 if (rom_check_and_register_reset() != 0) {
4607 error_report("rom check and register reset failed");
4613 /* This checkpoint is required by replay to separate prior clock
4614 reading from the other reads, because timer polling functions query
4615 clock values from the log. */
4616 replay_checkpoint(CHECKPOINT_RESET
);
4617 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
4618 register_global_state();
4620 Error
*local_err
= NULL
;
4621 if (load_snapshot(loadvm
, &local_err
) < 0) {
4622 error_report_err(local_err
);
4627 if (replay_mode
!= REPLAY_MODE_NONE
) {
4628 replay_vmstate_init();
4631 qdev_prop_check_globals();
4632 if (vmstate_dump_file
) {
4634 dump_vmstate_json_to_file(vmstate_dump_file
);
4639 Error
*local_err
= NULL
;
4640 qemu_start_incoming_migration(incoming
, &local_err
);
4642 error_reportf_err(local_err
, "-incoming %s: ", incoming
);
4645 } else if (autostart
) {
4649 accel_setup_post(current_machine
);
4654 gdbserver_cleanup();
4656 /* No more vcpu or device emulation activity beyond this point */
4659 job_cancel_sync_all();
4664 /* vhost-user must be cleaned up before chardevs. */
4670 user_creatable_cleanup();
4671 migration_object_finalize();
4672 /* TODO: unref root container, check all devices are ok */