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-common.h"
27 #include "qemu/units.h"
28 #include "hw/boards.h"
29 #include "hw/qdev-properties.h"
30 #include "qapi/error.h"
31 #include "qemu-version.h"
32 #include "qemu/cutils.h"
33 #include "qemu/help_option.h"
34 #include "qemu/uuid.h"
35 #include "sysemu/reset.h"
36 #include "sysemu/runstate.h"
37 #include "sysemu/seccomp.h"
38 #include "sysemu/tcg.h"
40 #include "qemu/error-report.h"
41 #include "qemu/sockets.h"
42 #include "sysemu/accel.h"
44 #include "hw/isa/isa.h"
45 #include "hw/scsi/scsi.h"
46 #include "hw/display/vga.h"
47 #include "sysemu/watchdog.h"
48 #include "hw/firmware/smbios.h"
49 #include "hw/acpi/acpi.h"
50 #include "hw/xen/xen.h"
51 #include "hw/loader.h"
52 #include "monitor/qdev.h"
54 #include "net/slirp.h"
55 #include "monitor/monitor.h"
56 #include "ui/console.h"
58 #include "sysemu/sysemu.h"
59 #include "sysemu/numa.h"
60 #include "sysemu/hostmem.h"
61 #include "exec/gdbstub.h"
62 #include "qemu/timer.h"
63 #include "chardev/char.h"
64 #include "qemu/bitmap.h"
66 #include "sysemu/blockdev.h"
67 #include "hw/block/block.h"
68 #include "migration/misc.h"
69 #include "migration/snapshot.h"
70 #include "migration/global_state.h"
71 #include "sysemu/tpm.h"
72 #include "sysemu/dma.h"
73 #include "hw/audio/soundhw.h"
74 #include "audio/audio.h"
75 #include "sysemu/cpus.h"
76 #include "migration/colo.h"
77 #include "migration/postcopy-ram.h"
78 #include "sysemu/kvm.h"
79 #include "sysemu/hax.h"
80 #include "qapi/qobject-input-visitor.h"
81 #include "qemu/option.h"
82 #include "qemu/config-file.h"
83 #include "qemu-options.h"
84 #include "qemu/main-loop.h"
86 #include "fsdev/qemu-fsdev.h"
88 #include "sysemu/qtest.h"
90 #include "disas/disas.h"
92 #include "trace-root.h"
93 #include "trace/control.h"
94 #include "qemu/plugin.h"
95 #include "qemu/queue.h"
96 #include "sysemu/arch_init.h"
98 #include "ui/qemu-spice.h"
99 #include "qapi/string-input-visitor.h"
100 #include "qapi/opts-visitor.h"
101 #include "qapi/clone-visitor.h"
102 #include "qom/object_interfaces.h"
103 #include "hw/semihosting/semihost.h"
104 #include "crypto/init.h"
105 #include "sysemu/replay.h"
106 #include "qapi/qapi-events-run-state.h"
107 #include "qapi/qapi-visit-block-core.h"
108 #include "qapi/qapi-visit-ui.h"
109 #include "qapi/qapi-commands-block-core.h"
110 #include "qapi/qapi-commands-run-state.h"
111 #include "qapi/qapi-commands-ui.h"
112 #include "qapi/qmp/qerror.h"
113 #include "sysemu/iothread.h"
114 #include "qemu/guest-random.h"
116 #define MAX_VIRTIO_CONSOLES 1
118 static const char *data_dir
[16];
119 static int data_dir_idx
;
120 const char *bios_name
= NULL
;
121 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
123 const char* keyboard_layout
= NULL
;
125 bool enable_mlock
= false;
126 bool enable_cpu_pm
= false;
128 NICInfo nd_table
[MAX_NICS
];
134 } rtc_base_type
= RTC_BASE_UTC
;
135 static time_t rtc_ref_start_datetime
;
136 static int rtc_realtime_clock_offset
; /* used only with QEMU_CLOCK_REALTIME */
137 static int rtc_host_datetime_offset
= -1; /* valid & used only with
139 QEMUClockType rtc_clock
;
140 int vga_interface_type
= VGA_NONE
;
141 static DisplayOptions dpy
;
142 static int num_serial_hds
;
143 static Chardev
**serial_hds
;
144 Chardev
*parallel_hds
[MAX_PARALLEL_PORTS
];
145 int win2k_install_hack
= 0;
147 int acpi_enabled
= 1;
150 static int no_reboot
;
152 int graphic_rotate
= 0;
153 const char *watchdog
;
154 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
157 const char *qemu_name
;
160 unsigned int nb_prom_envs
= 0;
161 const char *prom_envs
[MAX_PROM_ENVS
];
164 uint8_t *boot_splash_filedata
;
165 int only_migratable
; /* turn it off unless user states otherwise */
166 bool wakeup_suspend_enabled
;
168 int icount_align_option
;
170 /* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
171 * little-endian "wire format" described in the SMBIOS 2.6 specification.
176 static NotifierList exit_notifiers
=
177 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
179 static NotifierList machine_init_done_notifiers
=
180 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
184 enum xen_mode xen_mode
= XEN_EMULATE
;
185 bool xen_domid_restrict
;
187 static int has_defaults
= 1;
188 static int default_serial
= 1;
189 static int default_parallel
= 1;
190 static int default_monitor
= 1;
191 static int default_floppy
= 1;
192 static int default_cdrom
= 1;
193 static int default_sdcard
= 1;
194 static int default_vga
= 1;
195 static int default_net
= 1;
201 { .driver
= "isa-serial", .flag
= &default_serial
},
202 { .driver
= "isa-parallel", .flag
= &default_parallel
},
203 { .driver
= "isa-fdc", .flag
= &default_floppy
},
204 { .driver
= "floppy", .flag
= &default_floppy
},
205 { .driver
= "ide-cd", .flag
= &default_cdrom
},
206 { .driver
= "ide-hd", .flag
= &default_cdrom
},
207 { .driver
= "ide-drive", .flag
= &default_cdrom
},
208 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
209 { .driver
= "scsi-hd", .flag
= &default_cdrom
},
210 { .driver
= "VGA", .flag
= &default_vga
},
211 { .driver
= "isa-vga", .flag
= &default_vga
},
212 { .driver
= "cirrus-vga", .flag
= &default_vga
},
213 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
214 { .driver
= "vmware-svga", .flag
= &default_vga
},
215 { .driver
= "qxl-vga", .flag
= &default_vga
},
216 { .driver
= "virtio-vga", .flag
= &default_vga
},
217 { .driver
= "ati-vga", .flag
= &default_vga
},
218 { .driver
= "vhost-user-vga", .flag
= &default_vga
},
221 static QemuOptsList qemu_rtc_opts
= {
223 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
228 .type
= QEMU_OPT_STRING
,
231 .type
= QEMU_OPT_STRING
,
234 .type
= QEMU_OPT_STRING
,
236 { /* end of list */ }
240 static QemuOptsList qemu_option_rom_opts
= {
241 .name
= "option-rom",
242 .implied_opt_name
= "romfile",
243 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
247 .type
= QEMU_OPT_NUMBER
,
250 .type
= QEMU_OPT_STRING
,
252 { /* end of list */ }
256 static QemuOptsList qemu_machine_opts
= {
258 .implied_opt_name
= "type",
260 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
263 * no elements => accept any
264 * sanity checking will happen later
265 * when setting machine properties
271 static QemuOptsList qemu_accel_opts
= {
273 .implied_opt_name
= "accel",
274 .head
= QTAILQ_HEAD_INITIALIZER(qemu_accel_opts
.head
),
277 * no elements => accept any
278 * sanity checking will happen later
279 * when setting accelerator properties
285 static QemuOptsList qemu_boot_opts
= {
287 .implied_opt_name
= "order",
289 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
293 .type
= QEMU_OPT_STRING
,
296 .type
= QEMU_OPT_STRING
,
299 .type
= QEMU_OPT_BOOL
,
302 .type
= QEMU_OPT_STRING
,
304 .name
= "splash-time",
305 .type
= QEMU_OPT_NUMBER
,
307 .name
= "reboot-timeout",
308 .type
= QEMU_OPT_NUMBER
,
311 .type
= QEMU_OPT_BOOL
,
317 static QemuOptsList qemu_add_fd_opts
= {
319 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
323 .type
= QEMU_OPT_NUMBER
,
324 .help
= "file descriptor of which a duplicate is added to fd set",
327 .type
= QEMU_OPT_NUMBER
,
328 .help
= "ID of the fd set to add fd to",
331 .type
= QEMU_OPT_STRING
,
332 .help
= "free-form string used to describe fd",
334 { /* end of list */ }
338 static QemuOptsList qemu_object_opts
= {
340 .implied_opt_name
= "qom-type",
341 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
347 static QemuOptsList qemu_tpmdev_opts
= {
349 .implied_opt_name
= "type",
350 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
352 /* options are defined in the TPM backends */
353 { /* end of list */ }
357 static QemuOptsList qemu_realtime_opts
= {
359 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
363 .type
= QEMU_OPT_BOOL
,
365 { /* end of list */ }
369 static QemuOptsList qemu_overcommit_opts
= {
370 .name
= "overcommit",
371 .head
= QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts
.head
),
375 .type
= QEMU_OPT_BOOL
,
379 .type
= QEMU_OPT_BOOL
,
381 { /* end of list */ }
385 static QemuOptsList qemu_msg_opts
= {
387 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
391 .type
= QEMU_OPT_BOOL
,
393 { /* end of list */ }
397 static QemuOptsList qemu_name_opts
= {
399 .implied_opt_name
= "guest",
401 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
405 .type
= QEMU_OPT_STRING
,
406 .help
= "Sets the name of the guest.\n"
407 "This name will be displayed in the SDL window caption.\n"
408 "The name will also be used for the VNC server",
411 .type
= QEMU_OPT_STRING
,
412 .help
= "Sets the name of the QEMU process, as shown in top etc",
414 .name
= "debug-threads",
415 .type
= QEMU_OPT_BOOL
,
416 .help
= "When enabled, name the individual threads; defaults off.\n"
417 "NOTE: The thread names are for debugging and not a\n"
420 { /* End of list */ }
424 static QemuOptsList qemu_mem_opts
= {
426 .implied_opt_name
= "size",
427 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
432 .type
= QEMU_OPT_SIZE
,
436 .type
= QEMU_OPT_NUMBER
,
440 .type
= QEMU_OPT_SIZE
,
442 { /* end of list */ }
446 static QemuOptsList qemu_icount_opts
= {
448 .implied_opt_name
= "shift",
450 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
454 .type
= QEMU_OPT_STRING
,
457 .type
= QEMU_OPT_BOOL
,
460 .type
= QEMU_OPT_BOOL
,
463 .type
= QEMU_OPT_STRING
,
466 .type
= QEMU_OPT_STRING
,
468 .name
= "rrsnapshot",
469 .type
= QEMU_OPT_STRING
,
471 { /* end of list */ }
475 static QemuOptsList qemu_fw_cfg_opts
= {
477 .implied_opt_name
= "name",
478 .head
= QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts
.head
),
482 .type
= QEMU_OPT_STRING
,
483 .help
= "Sets the fw_cfg name of the blob to be inserted",
486 .type
= QEMU_OPT_STRING
,
487 .help
= "Sets the name of the file from which "
488 "the fw_cfg blob will be loaded",
491 .type
= QEMU_OPT_STRING
,
492 .help
= "Sets content of the blob to be inserted from a string",
494 { /* end of list */ }
499 * Get machine options
501 * Returns: machine options (never null).
503 QemuOpts
*qemu_get_machine_opts(void)
505 return qemu_find_opts_singleton("machine");
508 const char *qemu_get_vm_name(void)
513 static void res_free(void)
515 g_free(boot_splash_filedata
);
516 boot_splash_filedata
= NULL
;
519 static int default_driver_check(void *opaque
, QemuOpts
*opts
, Error
**errp
)
521 const char *driver
= qemu_opt_get(opts
, "driver");
526 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
527 if (strcmp(default_list
[i
].driver
, driver
) != 0)
529 *(default_list
[i
].flag
) = 0;
534 /***********************************************************/
537 static RunState current_run_state
= RUN_STATE_PRECONFIG
;
539 /* We use RUN_STATE__MAX but any invalid value will do */
540 static RunState vmstop_requested
= RUN_STATE__MAX
;
541 static QemuMutex vmstop_lock
;
546 } RunStateTransition
;
548 static const RunStateTransition runstate_transitions_def
[] = {
550 { RUN_STATE_PRECONFIG
, RUN_STATE_PRELAUNCH
},
551 /* Early switch to inmigrate state to allow -incoming CLI option work
552 * as it used to. TODO: delay actual switching to inmigrate state to
553 * the point after machine is built and remove this hack.
555 { RUN_STATE_PRECONFIG
, RUN_STATE_INMIGRATE
},
557 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
558 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
559 { RUN_STATE_DEBUG
, RUN_STATE_PRELAUNCH
},
561 { RUN_STATE_INMIGRATE
, RUN_STATE_INTERNAL_ERROR
},
562 { RUN_STATE_INMIGRATE
, RUN_STATE_IO_ERROR
},
563 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
564 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
565 { RUN_STATE_INMIGRATE
, RUN_STATE_SHUTDOWN
},
566 { RUN_STATE_INMIGRATE
, RUN_STATE_SUSPENDED
},
567 { RUN_STATE_INMIGRATE
, RUN_STATE_WATCHDOG
},
568 { RUN_STATE_INMIGRATE
, RUN_STATE_GUEST_PANICKED
},
569 { RUN_STATE_INMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
570 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
571 { RUN_STATE_INMIGRATE
, RUN_STATE_POSTMIGRATE
},
572 { RUN_STATE_INMIGRATE
, RUN_STATE_COLO
},
574 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
575 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
576 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PRELAUNCH
},
578 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
579 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
580 { RUN_STATE_IO_ERROR
, RUN_STATE_PRELAUNCH
},
582 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
583 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
584 { RUN_STATE_PAUSED
, RUN_STATE_POSTMIGRATE
},
585 { RUN_STATE_PAUSED
, RUN_STATE_PRELAUNCH
},
586 { RUN_STATE_PAUSED
, RUN_STATE_COLO
},
588 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
589 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
590 { RUN_STATE_POSTMIGRATE
, RUN_STATE_PRELAUNCH
},
592 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
593 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
594 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
596 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
597 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_PAUSED
},
598 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
599 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_PRELAUNCH
},
600 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_COLO
},
602 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
603 { RUN_STATE_RESTORE_VM
, RUN_STATE_PRELAUNCH
},
605 { RUN_STATE_COLO
, RUN_STATE_RUNNING
},
607 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
608 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
609 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
610 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
611 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
612 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
613 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
614 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
615 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
616 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
617 { RUN_STATE_RUNNING
, RUN_STATE_COLO
},
619 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
621 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
622 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
623 { RUN_STATE_SHUTDOWN
, RUN_STATE_PRELAUNCH
},
625 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
626 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
627 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
628 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
629 { RUN_STATE_SUSPENDED
, RUN_STATE_PRELAUNCH
},
630 { RUN_STATE_SUSPENDED
, RUN_STATE_COLO
},
632 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
633 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
634 { RUN_STATE_WATCHDOG
, RUN_STATE_PRELAUNCH
},
635 { RUN_STATE_WATCHDOG
, RUN_STATE_COLO
},
637 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
638 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
639 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_PRELAUNCH
},
641 { RUN_STATE__MAX
, RUN_STATE__MAX
},
644 static bool runstate_valid_transitions
[RUN_STATE__MAX
][RUN_STATE__MAX
];
646 bool runstate_check(RunState state
)
648 return current_run_state
== state
;
651 bool runstate_store(char *str
, size_t size
)
653 const char *state
= RunState_str(current_run_state
);
654 size_t len
= strlen(state
) + 1;
659 memcpy(str
, state
, len
);
663 static void runstate_init(void)
665 const RunStateTransition
*p
;
667 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
668 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE__MAX
; p
++) {
669 runstate_valid_transitions
[p
->from
][p
->to
] = true;
672 qemu_mutex_init(&vmstop_lock
);
675 /* This function will abort() on invalid state transitions */
676 void runstate_set(RunState new_state
)
678 assert(new_state
< RUN_STATE__MAX
);
680 trace_runstate_set(current_run_state
, RunState_str(current_run_state
),
681 new_state
, RunState_str(new_state
));
683 if (current_run_state
== new_state
) {
687 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
688 error_report("invalid runstate transition: '%s' -> '%s'",
689 RunState_str(current_run_state
),
690 RunState_str(new_state
));
694 current_run_state
= new_state
;
697 int runstate_is_running(void)
699 return runstate_check(RUN_STATE_RUNNING
);
702 bool runstate_needs_reset(void)
704 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
705 runstate_check(RUN_STATE_SHUTDOWN
);
708 StatusInfo
*qmp_query_status(Error
**errp
)
710 StatusInfo
*info
= g_malloc0(sizeof(*info
));
712 info
->running
= runstate_is_running();
713 info
->singlestep
= singlestep
;
714 info
->status
= current_run_state
;
719 bool qemu_vmstop_requested(RunState
*r
)
721 qemu_mutex_lock(&vmstop_lock
);
722 *r
= vmstop_requested
;
723 vmstop_requested
= RUN_STATE__MAX
;
724 qemu_mutex_unlock(&vmstop_lock
);
725 return *r
< RUN_STATE__MAX
;
728 void qemu_system_vmstop_request_prepare(void)
730 qemu_mutex_lock(&vmstop_lock
);
733 void qemu_system_vmstop_request(RunState state
)
735 vmstop_requested
= state
;
736 qemu_mutex_unlock(&vmstop_lock
);
740 /***********************************************************/
741 /* RTC reference time/date access */
742 static time_t qemu_ref_timedate(QEMUClockType clock
)
744 time_t value
= qemu_clock_get_ms(clock
) / 1000;
746 case QEMU_CLOCK_REALTIME
:
747 value
-= rtc_realtime_clock_offset
;
749 case QEMU_CLOCK_VIRTUAL
:
750 value
+= rtc_ref_start_datetime
;
752 case QEMU_CLOCK_HOST
:
753 if (rtc_base_type
== RTC_BASE_DATETIME
) {
754 value
-= rtc_host_datetime_offset
;
763 void qemu_get_timedate(struct tm
*tm
, int offset
)
765 time_t ti
= qemu_ref_timedate(rtc_clock
);
769 switch (rtc_base_type
) {
770 case RTC_BASE_DATETIME
:
774 case RTC_BASE_LOCALTIME
:
775 localtime_r(&ti
, tm
);
780 int qemu_timedate_diff(struct tm
*tm
)
784 switch (rtc_base_type
) {
785 case RTC_BASE_DATETIME
:
787 seconds
= mktimegm(tm
);
789 case RTC_BASE_LOCALTIME
:
792 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
793 seconds
= mktime(&tmp
);
800 return seconds
- qemu_ref_timedate(QEMU_CLOCK_HOST
);
803 static void configure_rtc_base_datetime(const char *startdate
)
805 time_t rtc_start_datetime
;
808 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d", &tm
.tm_year
, &tm
.tm_mon
,
809 &tm
.tm_mday
, &tm
.tm_hour
, &tm
.tm_min
, &tm
.tm_sec
) == 6) {
811 } else if (sscanf(startdate
, "%d-%d-%d",
812 &tm
.tm_year
, &tm
.tm_mon
, &tm
.tm_mday
) == 3) {
821 rtc_start_datetime
= mktimegm(&tm
);
822 if (rtc_start_datetime
== -1) {
824 error_report("invalid datetime format");
825 error_printf("valid formats: "
826 "'2006-06-17T16:01:21' or '2006-06-17'\n");
829 rtc_host_datetime_offset
= rtc_ref_start_datetime
- rtc_start_datetime
;
830 rtc_ref_start_datetime
= rtc_start_datetime
;
833 static void configure_rtc(QemuOpts
*opts
)
838 rtc_clock
= QEMU_CLOCK_HOST
;
839 rtc_ref_start_datetime
= qemu_clock_get_ms(QEMU_CLOCK_HOST
) / 1000;
840 rtc_realtime_clock_offset
= qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) / 1000;
842 value
= qemu_opt_get(opts
, "base");
844 if (!strcmp(value
, "utc")) {
845 rtc_base_type
= RTC_BASE_UTC
;
846 } else if (!strcmp(value
, "localtime")) {
847 Error
*blocker
= NULL
;
848 rtc_base_type
= RTC_BASE_LOCALTIME
;
849 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
,
850 "-rtc base=localtime");
851 replay_add_blocker(blocker
);
853 rtc_base_type
= RTC_BASE_DATETIME
;
854 configure_rtc_base_datetime(value
);
857 value
= qemu_opt_get(opts
, "clock");
859 if (!strcmp(value
, "host")) {
860 rtc_clock
= QEMU_CLOCK_HOST
;
861 } else if (!strcmp(value
, "rt")) {
862 rtc_clock
= QEMU_CLOCK_REALTIME
;
863 } else if (!strcmp(value
, "vm")) {
864 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
866 error_report("invalid option value '%s'", value
);
870 value
= qemu_opt_get(opts
, "driftfix");
872 if (!strcmp(value
, "slew")) {
873 object_register_sugar_prop("mc146818rtc",
876 } else if (!strcmp(value
, "none")) {
877 /* discard is default */
879 error_report("invalid option value '%s'", value
);
885 static int parse_name(void *opaque
, QemuOpts
*opts
, Error
**errp
)
887 const char *proc_name
;
889 if (qemu_opt_get(opts
, "debug-threads")) {
890 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
892 qemu_name
= qemu_opt_get(opts
, "guest");
894 proc_name
= qemu_opt_get(opts
, "process");
896 os_set_proc_name(proc_name
);
902 bool defaults_enabled(void)
908 static int parse_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
910 int fd
, dupfd
, flags
;
912 const char *fd_opaque
= NULL
;
915 fd
= qemu_opt_get_number(opts
, "fd", -1);
916 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
917 fd_opaque
= qemu_opt_get(opts
, "opaque");
920 error_setg(errp
, "fd option is required and must be non-negative");
924 if (fd
<= STDERR_FILENO
) {
925 error_setg(errp
, "fd cannot be a standard I/O stream");
930 * All fds inherited across exec() necessarily have FD_CLOEXEC
931 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
933 flags
= fcntl(fd
, F_GETFD
);
934 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
935 error_setg(errp
, "fd is not valid or already in use");
940 error_setg(errp
, "set option is required and must be non-negative");
944 #ifdef F_DUPFD_CLOEXEC
945 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
949 qemu_set_cloexec(dupfd
);
953 error_setg(errp
, "error duplicating fd: %s", strerror(errno
));
957 /* add the duplicate fd, and optionally the opaque string, to the fd set */
958 fdinfo
= monitor_fdset_add_fd(dupfd
, true, fdset_id
, !!fd_opaque
, fd_opaque
,
965 static int cleanup_add_fd(void *opaque
, QemuOpts
*opts
, Error
**errp
)
969 fd
= qemu_opt_get_number(opts
, "fd", -1);
976 /***********************************************************/
977 /* QEMU Block devices */
979 #define HD_OPTS "media=disk"
980 #define CDROM_OPTS "media=cdrom"
982 #define PFLASH_OPTS ""
986 static int drive_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
988 BlockInterfaceType
*block_default_type
= opaque
;
990 return drive_new(opts
, *block_default_type
, errp
) == NULL
;
993 static int drive_enable_snapshot(void *opaque
, QemuOpts
*opts
, Error
**errp
)
995 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
996 qemu_opt_set(opts
, "snapshot", "on", &error_abort
);
1001 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1002 int index
, const char *optstr
)
1007 if (!enable
|| drive_get_by_index(type
, index
)) {
1011 opts
= drive_add(type
, index
, NULL
, optstr
);
1013 drive_enable_snapshot(NULL
, opts
, NULL
);
1016 dinfo
= drive_new(opts
, type
, &error_abort
);
1017 dinfo
->is_default
= true;
1021 typedef struct BlockdevOptionsQueueEntry
{
1022 BlockdevOptions
*bdo
;
1024 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry
) entry
;
1025 } BlockdevOptionsQueueEntry
;
1027 typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry
) BlockdevOptionsQueue
;
1029 static void configure_blockdev(BlockdevOptionsQueue
*bdo_queue
,
1030 MachineClass
*machine_class
, int snapshot
)
1033 * If the currently selected machine wishes to override the
1034 * units-per-bus property of its default HBA interface type, do so
1037 if (machine_class
->units_per_default_bus
) {
1038 override_max_devs(machine_class
->block_default_type
,
1039 machine_class
->units_per_default_bus
);
1042 /* open the virtual block devices */
1043 while (!QSIMPLEQ_EMPTY(bdo_queue
)) {
1044 BlockdevOptionsQueueEntry
*bdo
= QSIMPLEQ_FIRST(bdo_queue
);
1046 QSIMPLEQ_REMOVE_HEAD(bdo_queue
, entry
);
1047 loc_push_restore(&bdo
->loc
);
1048 qmp_blockdev_add(bdo
->bdo
, &error_fatal
);
1050 qapi_free_BlockdevOptions(bdo
->bdo
);
1054 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
,
1057 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
1058 &machine_class
->block_default_type
, &error_fatal
)) {
1059 /* We printed help */
1063 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
1065 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
1066 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
1070 static QemuOptsList qemu_smp_opts
= {
1072 .implied_opt_name
= "cpus",
1073 .merge_lists
= true,
1074 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1078 .type
= QEMU_OPT_NUMBER
,
1081 .type
= QEMU_OPT_NUMBER
,
1084 .type
= QEMU_OPT_NUMBER
,
1087 .type
= QEMU_OPT_NUMBER
,
1090 .type
= QEMU_OPT_NUMBER
,
1093 .type
= QEMU_OPT_NUMBER
,
1095 { /*End of list */ }
1099 static void realtime_init(void)
1102 if (os_mlock() < 0) {
1103 error_report("locking memory failed");
1110 static void configure_msg(QemuOpts
*opts
)
1112 error_with_timestamp
= qemu_opt_get_bool(opts
, "timestamp", false);
1116 /***********************************************************/
1119 static int usb_device_add(const char *devname
)
1121 USBDevice
*dev
= NULL
;
1123 if (!machine_usb(current_machine
)) {
1127 dev
= usbdevice_create(devname
);
1134 static int usb_parse(const char *cmdline
)
1137 r
= usb_device_add(cmdline
);
1139 error_report("could not add USB device '%s'", cmdline
);
1144 /***********************************************************/
1145 /* machine registration */
1147 MachineState
*current_machine
;
1149 static MachineClass
*find_machine(const char *name
, GSList
*machines
)
1153 for (el
= machines
; el
; el
= el
->next
) {
1154 MachineClass
*mc
= el
->data
;
1156 if (!strcmp(mc
->name
, name
) || !g_strcmp0(mc
->alias
, name
)) {
1164 static MachineClass
*find_default_machine(GSList
*machines
)
1167 MachineClass
*default_machineclass
= NULL
;
1169 for (el
= machines
; el
; el
= el
->next
) {
1170 MachineClass
*mc
= el
->data
;
1172 if (mc
->is_default
) {
1173 assert(default_machineclass
== NULL
&& "Multiple default machines");
1174 default_machineclass
= mc
;
1178 return default_machineclass
;
1181 static int machine_help_func(QemuOpts
*opts
, MachineState
*machine
)
1183 ObjectProperty
*prop
;
1184 ObjectPropertyIterator iter
;
1186 if (!qemu_opt_has_help_opt(opts
)) {
1190 object_property_iter_init(&iter
, OBJECT(machine
));
1191 while ((prop
= object_property_iter_next(&iter
))) {
1196 printf("%s.%s=%s", MACHINE_GET_CLASS(machine
)->name
,
1197 prop
->name
, prop
->type
);
1198 if (prop
->description
) {
1199 printf(" (%s)\n", prop
->description
);
1208 struct VMChangeStateEntry
{
1209 VMChangeStateHandler
*cb
;
1211 QTAILQ_ENTRY(VMChangeStateEntry
) entries
;
1215 static QTAILQ_HEAD(, VMChangeStateEntry
) vm_change_state_head
;
1218 * qemu_add_vm_change_state_handler_prio:
1219 * @cb: the callback to invoke
1220 * @opaque: user data passed to the callback
1221 * @priority: low priorities execute first when the vm runs and the reverse is
1222 * true when the vm stops
1224 * Register a callback function that is invoked when the vm starts or stops
1227 * Returns: an entry to be freed using qemu_del_vm_change_state_handler()
1229 VMChangeStateEntry
*qemu_add_vm_change_state_handler_prio(
1230 VMChangeStateHandler
*cb
, void *opaque
, int priority
)
1232 VMChangeStateEntry
*e
;
1233 VMChangeStateEntry
*other
;
1235 e
= g_malloc0(sizeof(*e
));
1238 e
->priority
= priority
;
1240 /* Keep list sorted in ascending priority order */
1241 QTAILQ_FOREACH(other
, &vm_change_state_head
, entries
) {
1242 if (priority
< other
->priority
) {
1243 QTAILQ_INSERT_BEFORE(other
, e
, entries
);
1248 QTAILQ_INSERT_TAIL(&vm_change_state_head
, e
, entries
);
1252 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1255 return qemu_add_vm_change_state_handler_prio(cb
, opaque
, 0);
1258 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1260 QTAILQ_REMOVE(&vm_change_state_head
, e
, entries
);
1264 void vm_state_notify(int running
, RunState state
)
1266 VMChangeStateEntry
*e
, *next
;
1268 trace_vm_state_notify(running
, state
, RunState_str(state
));
1271 QTAILQ_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1272 e
->cb(e
->opaque
, running
, state
);
1275 QTAILQ_FOREACH_REVERSE_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1276 e
->cb(e
->opaque
, running
, state
);
1281 static ShutdownCause reset_requested
;
1282 static ShutdownCause shutdown_requested
;
1283 static int shutdown_signal
;
1284 static pid_t shutdown_pid
;
1285 static int powerdown_requested
;
1286 static int debug_requested
;
1287 static int suspend_requested
;
1288 static bool preconfig_exit_requested
= true;
1289 static WakeupReason wakeup_reason
;
1290 static NotifierList powerdown_notifiers
=
1291 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1292 static NotifierList suspend_notifiers
=
1293 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1294 static NotifierList wakeup_notifiers
=
1295 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1296 static NotifierList shutdown_notifiers
=
1297 NOTIFIER_LIST_INITIALIZER(shutdown_notifiers
);
1298 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1300 ShutdownCause
qemu_shutdown_requested_get(void)
1302 return shutdown_requested
;
1305 ShutdownCause
qemu_reset_requested_get(void)
1307 return reset_requested
;
1310 static int qemu_shutdown_requested(void)
1312 return atomic_xchg(&shutdown_requested
, SHUTDOWN_CAUSE_NONE
);
1315 static void qemu_kill_report(void)
1317 if (!qtest_driver() && shutdown_signal
) {
1318 if (shutdown_pid
== 0) {
1319 /* This happens for eg ^C at the terminal, so it's worth
1320 * avoiding printing an odd message in that case.
1322 error_report("terminating on signal %d", shutdown_signal
);
1324 char *shutdown_cmd
= qemu_get_pid_name(shutdown_pid
);
1326 error_report("terminating on signal %d from pid " FMT_pid
" (%s)",
1327 shutdown_signal
, shutdown_pid
,
1328 shutdown_cmd
? shutdown_cmd
: "<unknown process>");
1329 g_free(shutdown_cmd
);
1331 shutdown_signal
= 0;
1335 static ShutdownCause
qemu_reset_requested(void)
1337 ShutdownCause r
= reset_requested
;
1339 if (r
&& replay_checkpoint(CHECKPOINT_RESET_REQUESTED
)) {
1340 reset_requested
= SHUTDOWN_CAUSE_NONE
;
1343 return SHUTDOWN_CAUSE_NONE
;
1346 static int qemu_suspend_requested(void)
1348 int r
= suspend_requested
;
1349 if (r
&& replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED
)) {
1350 suspend_requested
= 0;
1356 static WakeupReason
qemu_wakeup_requested(void)
1358 return wakeup_reason
;
1361 static int qemu_powerdown_requested(void)
1363 int r
= powerdown_requested
;
1364 powerdown_requested
= 0;
1368 static int qemu_debug_requested(void)
1370 int r
= debug_requested
;
1371 debug_requested
= 0;
1375 void qemu_exit_preconfig_request(void)
1377 preconfig_exit_requested
= true;
1381 * Reset the VM. Issue an event unless @reason is SHUTDOWN_CAUSE_NONE.
1383 void qemu_system_reset(ShutdownCause reason
)
1387 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1389 cpu_synchronize_all_states();
1391 if (mc
&& mc
->reset
) {
1392 mc
->reset(current_machine
);
1394 qemu_devices_reset();
1396 if (reason
&& reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
1397 qapi_event_send_reset(shutdown_caused_by_guest(reason
), reason
);
1399 cpu_synchronize_all_post_reset();
1403 * Wake the VM after suspend.
1405 static void qemu_system_wakeup(void)
1409 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1411 if (mc
&& mc
->wakeup
) {
1412 mc
->wakeup(current_machine
);
1416 void qemu_system_guest_panicked(GuestPanicInformation
*info
)
1418 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crashed");
1421 current_cpu
->crash_occurred
= true;
1423 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE
,
1425 vm_stop(RUN_STATE_GUEST_PANICKED
);
1427 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF
,
1429 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC
);
1433 if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_HYPER_V
) {
1434 qemu_log_mask(LOG_GUEST_ERROR
, "\nHV crash parameters: (%#"PRIx64
1435 " %#"PRIx64
" %#"PRIx64
" %#"PRIx64
" %#"PRIx64
")\n",
1436 info
->u
.hyper_v
.arg1
,
1437 info
->u
.hyper_v
.arg2
,
1438 info
->u
.hyper_v
.arg3
,
1439 info
->u
.hyper_v
.arg4
,
1440 info
->u
.hyper_v
.arg5
);
1441 } else if (info
->type
== GUEST_PANIC_INFORMATION_TYPE_S390
) {
1442 qemu_log_mask(LOG_GUEST_ERROR
, " on cpu %d: %s\n"
1443 "PSW: 0x%016" PRIx64
" 0x%016" PRIx64
"\n",
1445 S390CrashReason_str(info
->u
.s390
.reason
),
1446 info
->u
.s390
.psw_mask
,
1447 info
->u
.s390
.psw_addr
);
1449 qapi_free_GuestPanicInformation(info
);
1453 void qemu_system_guest_crashloaded(GuestPanicInformation
*info
)
1455 qemu_log_mask(LOG_GUEST_ERROR
, "Guest crash loaded");
1457 qapi_event_send_guest_crashloaded(GUEST_PANIC_ACTION_RUN
,
1461 qapi_free_GuestPanicInformation(info
);
1465 void qemu_system_reset_request(ShutdownCause reason
)
1467 if (no_reboot
&& reason
!= SHUTDOWN_CAUSE_SUBSYSTEM_RESET
) {
1468 shutdown_requested
= reason
;
1470 reset_requested
= reason
;
1473 qemu_notify_event();
1476 static void qemu_system_suspend(void)
1479 notifier_list_notify(&suspend_notifiers
, NULL
);
1480 runstate_set(RUN_STATE_SUSPENDED
);
1481 qapi_event_send_suspend();
1484 void qemu_system_suspend_request(void)
1486 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1489 suspend_requested
= 1;
1491 qemu_notify_event();
1494 void qemu_register_suspend_notifier(Notifier
*notifier
)
1496 notifier_list_add(&suspend_notifiers
, notifier
);
1499 void qemu_system_wakeup_request(WakeupReason reason
, Error
**errp
)
1501 trace_system_wakeup_request(reason
);
1503 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1505 "Unable to wake up: guest is not in suspended state");
1508 if (!(wakeup_reason_mask
& (1 << reason
))) {
1511 runstate_set(RUN_STATE_RUNNING
);
1512 wakeup_reason
= reason
;
1513 qemu_notify_event();
1516 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1519 wakeup_reason_mask
|= (1 << reason
);
1521 wakeup_reason_mask
&= ~(1 << reason
);
1525 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1527 notifier_list_add(&wakeup_notifiers
, notifier
);
1530 void qemu_register_wakeup_support(void)
1532 wakeup_suspend_enabled
= true;
1535 bool qemu_wakeup_suspend_enabled(void)
1537 return wakeup_suspend_enabled
;
1540 void qemu_system_killed(int signal
, pid_t pid
)
1542 shutdown_signal
= signal
;
1546 /* Cannot call qemu_system_shutdown_request directly because
1547 * we are in a signal handler.
1549 shutdown_requested
= SHUTDOWN_CAUSE_HOST_SIGNAL
;
1550 qemu_notify_event();
1553 void qemu_system_shutdown_request(ShutdownCause reason
)
1555 trace_qemu_system_shutdown_request(reason
);
1556 replay_shutdown_request(reason
);
1557 shutdown_requested
= reason
;
1558 qemu_notify_event();
1561 static void qemu_system_powerdown(void)
1563 qapi_event_send_powerdown();
1564 notifier_list_notify(&powerdown_notifiers
, NULL
);
1567 static void qemu_system_shutdown(ShutdownCause cause
)
1569 qapi_event_send_shutdown(shutdown_caused_by_guest(cause
), cause
);
1570 notifier_list_notify(&shutdown_notifiers
, &cause
);
1573 void qemu_system_powerdown_request(void)
1575 trace_qemu_system_powerdown_request();
1576 powerdown_requested
= 1;
1577 qemu_notify_event();
1580 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1582 notifier_list_add(&powerdown_notifiers
, notifier
);
1585 void qemu_register_shutdown_notifier(Notifier
*notifier
)
1587 notifier_list_add(&shutdown_notifiers
, notifier
);
1590 void qemu_system_debug_request(void)
1592 debug_requested
= 1;
1593 qemu_notify_event();
1596 static bool main_loop_should_exit(void)
1599 ShutdownCause request
;
1601 if (preconfig_exit_requested
) {
1602 if (runstate_check(RUN_STATE_PRECONFIG
)) {
1603 runstate_set(RUN_STATE_PRELAUNCH
);
1605 preconfig_exit_requested
= false;
1608 if (qemu_debug_requested()) {
1609 vm_stop(RUN_STATE_DEBUG
);
1611 if (qemu_suspend_requested()) {
1612 qemu_system_suspend();
1614 request
= qemu_shutdown_requested();
1617 qemu_system_shutdown(request
);
1619 vm_stop(RUN_STATE_SHUTDOWN
);
1624 request
= qemu_reset_requested();
1627 qemu_system_reset(request
);
1630 * runstate can change in pause_all_vcpus()
1631 * as iothread mutex is unlocked
1633 if (!runstate_check(RUN_STATE_RUNNING
) &&
1634 !runstate_check(RUN_STATE_INMIGRATE
) &&
1635 !runstate_check(RUN_STATE_FINISH_MIGRATE
)) {
1636 runstate_set(RUN_STATE_PRELAUNCH
);
1639 if (qemu_wakeup_requested()) {
1641 qemu_system_wakeup();
1642 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1643 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1645 qapi_event_send_wakeup();
1647 if (qemu_powerdown_requested()) {
1648 qemu_system_powerdown();
1650 if (qemu_vmstop_requested(&r
)) {
1656 void qemu_main_loop(void)
1658 #ifdef CONFIG_PROFILER
1661 while (!main_loop_should_exit()) {
1662 #ifdef CONFIG_PROFILER
1663 ti
= profile_getclock();
1665 main_loop_wait(false);
1666 #ifdef CONFIG_PROFILER
1667 dev_time
+= profile_getclock() - ti
;
1672 static void version(void)
1674 printf("QEMU emulator version " QEMU_FULL_VERSION
"\n"
1675 QEMU_COPYRIGHT
"\n");
1678 static void help(int exitcode
)
1681 printf("usage: %s [options] [disk_image]\n\n"
1682 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1683 error_get_progname());
1685 #define QEMU_OPTIONS_GENERATE_HELP
1686 #include "qemu-options-wrapper.h"
1688 printf("\nDuring emulation, the following keys are useful:\n"
1689 "ctrl-alt-f toggle full screen\n"
1690 "ctrl-alt-n switch to virtual console 'n'\n"
1691 "ctrl-alt toggle mouse and keyboard grab\n"
1693 "When using -nographic, press 'ctrl-a h' to get some help.\n"
1695 QEMU_HELP_BOTTOM
"\n");
1700 #define HAS_ARG 0x0001
1702 typedef struct QEMUOption
{
1709 static const QEMUOption qemu_options
[] = {
1710 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1711 #define QEMU_OPTIONS_GENERATE_OPTIONS
1712 #include "qemu-options-wrapper.h"
1716 typedef struct VGAInterfaceInfo
{
1717 const char *opt_name
; /* option name */
1718 const char *name
; /* human-readable name */
1719 /* Class names indicating that support is available.
1720 * If no class is specified, the interface is always available */
1721 const char *class_names
[2];
1724 static const VGAInterfaceInfo vga_interfaces
[VGA_TYPE_MAX
] = {
1727 .name
= "no graphic card",
1731 .name
= "standard VGA",
1732 .class_names
= { "VGA", "isa-vga" },
1735 .opt_name
= "cirrus",
1736 .name
= "Cirrus VGA",
1737 .class_names
= { "cirrus-vga", "isa-cirrus-vga" },
1740 .opt_name
= "vmware",
1741 .name
= "VMWare SVGA",
1742 .class_names
= { "vmware-svga" },
1745 .opt_name
= "virtio",
1746 .name
= "Virtio VGA",
1747 .class_names
= { "virtio-vga" },
1752 .class_names
= { "qxl-vga" },
1756 .name
= "TCX framebuffer",
1757 .class_names
= { "SUNW,tcx" },
1761 .name
= "CG3 framebuffer",
1762 .class_names
= { "cgthree" },
1765 .opt_name
= "xenfb",
1766 .name
= "Xen paravirtualized framebuffer",
1770 static bool vga_interface_available(VGAInterfaceType t
)
1772 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
1774 assert(t
< VGA_TYPE_MAX
);
1775 return !ti
->class_names
[0] ||
1776 object_class_by_name(ti
->class_names
[0]) ||
1777 object_class_by_name(ti
->class_names
[1]);
1781 get_default_vga_model(const MachineClass
*machine_class
)
1783 if (machine_class
->default_display
) {
1784 return machine_class
->default_display
;
1785 } else if (vga_interface_available(VGA_CIRRUS
)) {
1787 } else if (vga_interface_available(VGA_STD
)) {
1794 static void select_vgahw(const MachineClass
*machine_class
, const char *p
)
1799 if (g_str_equal(p
, "help")) {
1800 const char *def
= get_default_vga_model(machine_class
);
1802 for (t
= 0; t
< VGA_TYPE_MAX
; t
++) {
1803 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
1805 if (vga_interface_available(t
) && ti
->opt_name
) {
1806 printf("%-20s %s%s\n", ti
->opt_name
, ti
->name
?: "",
1807 g_str_equal(ti
->opt_name
, def
) ? " (default)" : "");
1813 assert(vga_interface_type
== VGA_NONE
);
1814 for (t
= 0; t
< VGA_TYPE_MAX
; t
++) {
1815 const VGAInterfaceInfo
*ti
= &vga_interfaces
[t
];
1816 if (ti
->opt_name
&& strstart(p
, ti
->opt_name
, &opts
)) {
1817 if (!vga_interface_available(t
)) {
1818 error_report("%s not available", ti
->name
);
1821 vga_interface_type
= t
;
1825 if (t
== VGA_TYPE_MAX
) {
1827 error_report("unknown vga type: %s", p
);
1831 const char *nextopt
;
1833 if (strstart(opts
, ",retrace=", &nextopt
)) {
1835 if (strstart(opts
, "dumb", &nextopt
))
1836 vga_retrace_method
= VGA_RETRACE_DUMB
;
1837 else if (strstart(opts
, "precise", &nextopt
))
1838 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1839 else goto invalid_vga
;
1840 } else goto invalid_vga
;
1845 static void parse_display_qapi(const char *optarg
)
1847 DisplayOptions
*opts
;
1850 v
= qobject_input_visitor_new_str(optarg
, "type", &error_fatal
);
1852 visit_type_DisplayOptions(v
, NULL
, &opts
, &error_fatal
);
1853 QAPI_CLONE_MEMBERS(DisplayOptions
, &dpy
, opts
);
1855 qapi_free_DisplayOptions(opts
);
1859 DisplayOptions
*qmp_query_display_options(Error
**errp
)
1861 return QAPI_CLONE(DisplayOptions
, &dpy
);
1864 static void parse_display(const char *p
)
1868 if (is_help_option(p
)) {
1869 qemu_display_help();
1873 if (strstart(p
, "sdl", &opts
)) {
1875 * sdl DisplayType needs hand-crafted parser instead of
1876 * parse_display_qapi() due to some options not in
1877 * DisplayOptions, specifically:
1879 * Already deprecated.
1880 * - ctrl_grab + alt_grab
1881 * Not clear yet what happens to them long-term. Should
1882 * replaced by something better or deprecated and dropped.
1884 dpy
.type
= DISPLAY_TYPE_SDL
;
1886 const char *nextopt
;
1888 if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1890 if (strstart(opts
, "on", &nextopt
)) {
1892 } else if (strstart(opts
, "off", &nextopt
)) {
1895 goto invalid_sdl_args
;
1897 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1899 if (strstart(opts
, "on", &nextopt
)) {
1901 } else if (strstart(opts
, "off", &nextopt
)) {
1904 goto invalid_sdl_args
;
1906 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1908 dpy
.has_window_close
= true;
1909 if (strstart(opts
, "on", &nextopt
)) {
1910 dpy
.window_close
= true;
1911 } else if (strstart(opts
, "off", &nextopt
)) {
1912 dpy
.window_close
= false;
1914 goto invalid_sdl_args
;
1916 } else if (strstart(opts
, ",show-cursor=", &nextopt
)) {
1918 dpy
.has_show_cursor
= true;
1919 if (strstart(opts
, "on", &nextopt
)) {
1920 dpy
.show_cursor
= true;
1921 } else if (strstart(opts
, "off", &nextopt
)) {
1922 dpy
.show_cursor
= false;
1924 goto invalid_sdl_args
;
1926 } else if (strstart(opts
, ",gl=", &nextopt
)) {
1929 if (strstart(opts
, "on", &nextopt
)) {
1930 dpy
.gl
= DISPLAYGL_MODE_ON
;
1931 } else if (strstart(opts
, "core", &nextopt
)) {
1932 dpy
.gl
= DISPLAYGL_MODE_CORE
;
1933 } else if (strstart(opts
, "es", &nextopt
)) {
1934 dpy
.gl
= DISPLAYGL_MODE_ES
;
1935 } else if (strstart(opts
, "off", &nextopt
)) {
1936 dpy
.gl
= DISPLAYGL_MODE_OFF
;
1938 goto invalid_sdl_args
;
1942 error_report("invalid SDL option string");
1947 } else if (strstart(p
, "vnc", &opts
)) {
1949 * vnc isn't a (local) DisplayType but a protocol for remote
1953 vnc_parse(opts
+ 1, &error_fatal
);
1955 error_report("VNC requires a display argument vnc=<display>");
1959 parse_display_qapi(p
);
1963 char *qemu_find_file(int type
, const char *name
)
1969 /* Try the name as a straight path first */
1970 if (access(name
, R_OK
) == 0) {
1971 trace_load_file(name
, name
);
1972 return g_strdup(name
);
1976 case QEMU_FILE_TYPE_BIOS
:
1979 case QEMU_FILE_TYPE_KEYMAP
:
1980 subdir
= "keymaps/";
1986 for (i
= 0; i
< data_dir_idx
; i
++) {
1987 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
1988 if (access(buf
, R_OK
) == 0) {
1989 trace_load_file(name
, buf
);
1997 static void qemu_add_data_dir(const char *path
)
2004 if (data_dir_idx
== ARRAY_SIZE(data_dir
)) {
2007 for (i
= 0; i
< data_dir_idx
; i
++) {
2008 if (strcmp(data_dir
[i
], path
) == 0) {
2009 return; /* duplicate */
2012 data_dir
[data_dir_idx
++] = g_strdup(path
);
2015 static inline bool nonempty_str(const char *str
)
2020 static int parse_fw_cfg(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2024 const char *name
, *file
, *str
;
2025 FWCfgState
*fw_cfg
= (FWCfgState
*) opaque
;
2027 if (fw_cfg
== NULL
) {
2028 error_setg(errp
, "fw_cfg device not available");
2031 name
= qemu_opt_get(opts
, "name");
2032 file
= qemu_opt_get(opts
, "file");
2033 str
= qemu_opt_get(opts
, "string");
2035 /* we need name and either a file or the content string */
2036 if (!(nonempty_str(name
) && (nonempty_str(file
) || nonempty_str(str
)))) {
2037 error_setg(errp
, "invalid argument(s)");
2040 if (nonempty_str(file
) && nonempty_str(str
)) {
2041 error_setg(errp
, "file and string are mutually exclusive");
2044 if (strlen(name
) > FW_CFG_MAX_FILE_PATH
- 1) {
2045 error_setg(errp
, "name too long (max. %d char)",
2046 FW_CFG_MAX_FILE_PATH
- 1);
2049 if (strncmp(name
, "opt/", 4) != 0) {
2050 warn_report("externally provided fw_cfg item names "
2051 "should be prefixed with \"opt/\"");
2053 if (nonempty_str(str
)) {
2054 size
= strlen(str
); /* NUL terminator NOT included in fw_cfg blob */
2055 buf
= g_memdup(str
, size
);
2058 if (!g_file_get_contents(file
, &buf
, &size
, &err
)) {
2059 error_setg(errp
, "can't load %s: %s", file
, err
->message
);
2064 /* For legacy, keep user files in a specific global order. */
2065 fw_cfg_set_order_override(fw_cfg
, FW_CFG_ORDER_OVERRIDE_USER
);
2066 fw_cfg_add_file(fw_cfg
, name
, buf
, size
);
2067 fw_cfg_reset_order_override(fw_cfg
);
2071 static int device_help_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2073 return qdev_device_help(opts
);
2076 static int device_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2080 dev
= qdev_device_add(opts
, errp
);
2081 if (!dev
&& *errp
) {
2082 error_report_err(*errp
);
2085 object_unref(OBJECT(dev
));
2090 static int chardev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2092 Error
*local_err
= NULL
;
2094 if (!qemu_chr_new_from_opts(opts
, NULL
, &local_err
)) {
2096 error_propagate(errp
, local_err
);
2104 #ifdef CONFIG_VIRTFS
2105 static int fsdev_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2107 return qemu_fsdev_add(opts
, errp
);
2111 static int mon_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2113 return monitor_init_opts(opts
, errp
);
2116 static void monitor_parse(const char *optarg
, const char *mode
, bool pretty
)
2118 static int monitor_device_index
= 0;
2123 if (strstart(optarg
, "chardev:", &p
)) {
2124 snprintf(label
, sizeof(label
), "%s", p
);
2126 snprintf(label
, sizeof(label
), "compat_monitor%d",
2127 monitor_device_index
);
2128 opts
= qemu_chr_parse_compat(label
, optarg
, true);
2130 error_report("parse error: %s", optarg
);
2135 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, &error_fatal
);
2136 qemu_opt_set(opts
, "mode", mode
, &error_abort
);
2137 qemu_opt_set(opts
, "chardev", label
, &error_abort
);
2138 if (!strcmp(mode
, "control")) {
2139 qemu_opt_set_bool(opts
, "pretty", pretty
, &error_abort
);
2141 assert(pretty
== false);
2143 monitor_device_index
++;
2146 struct device_config
{
2148 DEV_USB
, /* -usbdevice */
2149 DEV_SERIAL
, /* -serial */
2150 DEV_PARALLEL
, /* -parallel */
2151 DEV_DEBUGCON
, /* -debugcon */
2152 DEV_GDB
, /* -gdb, -s */
2153 DEV_SCLP
, /* s390 sclp */
2155 const char *cmdline
;
2157 QTAILQ_ENTRY(device_config
) next
;
2160 static QTAILQ_HEAD(, device_config
) device_configs
=
2161 QTAILQ_HEAD_INITIALIZER(device_configs
);
2163 static void add_device_config(int type
, const char *cmdline
)
2165 struct device_config
*conf
;
2167 conf
= g_malloc0(sizeof(*conf
));
2169 conf
->cmdline
= cmdline
;
2170 loc_save(&conf
->loc
);
2171 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2174 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2176 struct device_config
*conf
;
2179 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2180 if (conf
->type
!= type
)
2182 loc_push_restore(&conf
->loc
);
2183 rc
= func(conf
->cmdline
);
2184 loc_pop(&conf
->loc
);
2192 static int serial_parse(const char *devname
)
2194 int index
= num_serial_hds
;
2197 if (strcmp(devname
, "none") == 0)
2199 snprintf(label
, sizeof(label
), "serial%d", index
);
2200 serial_hds
= g_renew(Chardev
*, serial_hds
, index
+ 1);
2202 serial_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
, NULL
);
2203 if (!serial_hds
[index
]) {
2204 error_report("could not connect serial device"
2205 " to character backend '%s'", devname
);
2212 Chardev
*serial_hd(int i
)
2215 if (i
< num_serial_hds
) {
2216 return serial_hds
[i
];
2221 int serial_max_hds(void)
2223 return num_serial_hds
;
2226 static int parallel_parse(const char *devname
)
2228 static int index
= 0;
2231 if (strcmp(devname
, "none") == 0)
2233 if (index
== MAX_PARALLEL_PORTS
) {
2234 error_report("too many parallel ports");
2237 snprintf(label
, sizeof(label
), "parallel%d", index
);
2238 parallel_hds
[index
] = qemu_chr_new_mux_mon(label
, devname
, NULL
);
2239 if (!parallel_hds
[index
]) {
2240 error_report("could not connect parallel device"
2241 " to character backend '%s'", devname
);
2248 static int debugcon_parse(const char *devname
)
2252 if (!qemu_chr_new_mux_mon("debugcon", devname
, NULL
)) {
2253 error_report("invalid character backend '%s'", devname
);
2256 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2258 error_report("already have a debugcon device");
2261 qemu_opt_set(opts
, "driver", "isa-debugcon", &error_abort
);
2262 qemu_opt_set(opts
, "chardev", "debugcon", &error_abort
);
2266 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2268 const MachineClass
*mc1
= a
, *mc2
= b
;
2271 if (mc1
->family
== NULL
) {
2272 if (mc2
->family
== NULL
) {
2273 /* Compare standalone machine types against each other; they sort
2274 * in increasing order.
2276 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2277 object_class_get_name(OBJECT_CLASS(mc2
)));
2280 /* Standalone machine types sort after families. */
2284 if (mc2
->family
== NULL
) {
2285 /* Families sort before standalone machine types. */
2289 /* Families sort between each other alphabetically increasingly. */
2290 res
= strcmp(mc1
->family
, mc2
->family
);
2295 /* Within the same family, machine types sort in decreasing order. */
2296 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2297 object_class_get_name(OBJECT_CLASS(mc1
)));
2300 static MachineClass
*machine_parse(const char *name
, GSList
*machines
)
2305 if (is_help_option(name
)) {
2306 printf("Supported machines are:\n");
2307 machines
= g_slist_sort(machines
, machine_class_cmp
);
2308 for (el
= machines
; el
; el
= el
->next
) {
2309 MachineClass
*mc
= el
->data
;
2311 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2313 printf("%-20s %s%s%s\n", mc
->name
, mc
->desc
,
2314 mc
->is_default
? " (default)" : "",
2315 mc
->deprecation_reason
? " (deprecated)" : "");
2320 mc
= find_machine(name
, machines
);
2322 error_report("unsupported machine type");
2323 error_printf("Use -machine help to list supported machines\n");
2329 void qemu_add_exit_notifier(Notifier
*notify
)
2331 notifier_list_add(&exit_notifiers
, notify
);
2334 void qemu_remove_exit_notifier(Notifier
*notify
)
2336 notifier_remove(notify
);
2339 static void qemu_run_exit_notifiers(void)
2341 notifier_list_notify(&exit_notifiers
, NULL
);
2344 static const char *pid_file
;
2345 static Notifier qemu_unlink_pidfile_notifier
;
2347 static void qemu_unlink_pidfile(Notifier
*n
, void *data
)
2354 bool machine_init_done
;
2356 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2358 notifier_list_add(&machine_init_done_notifiers
, notify
);
2359 if (machine_init_done
) {
2360 notify
->notify(notify
, NULL
);
2364 void qemu_remove_machine_init_done_notifier(Notifier
*notify
)
2366 notifier_remove(notify
);
2369 static void qemu_run_machine_init_done_notifiers(void)
2371 machine_init_done
= true;
2372 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2375 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2376 const char **poptarg
, int *poptind
)
2378 const QEMUOption
*popt
;
2379 int optind
= *poptind
;
2380 char *r
= argv
[optind
];
2383 loc_set_cmdline(argv
, optind
, 1);
2385 /* Treat --foo the same as -foo. */
2388 popt
= qemu_options
;
2391 error_report("invalid option");
2394 if (!strcmp(popt
->name
, r
+ 1))
2398 if (popt
->flags
& HAS_ARG
) {
2399 if (optind
>= argc
) {
2400 error_report("requires an argument");
2403 optarg
= argv
[optind
++];
2404 loc_set_cmdline(argv
, optind
- 2, 2);
2415 static MachineClass
*select_machine(void)
2417 GSList
*machines
= object_class_get_list(TYPE_MACHINE
, false);
2418 MachineClass
*machine_class
= find_default_machine(machines
);
2423 loc_push_none(&loc
);
2425 opts
= qemu_get_machine_opts();
2426 qemu_opts_loc_restore(opts
);
2428 optarg
= qemu_opt_get(opts
, "type");
2430 machine_class
= machine_parse(optarg
, machines
);
2433 if (!machine_class
) {
2434 error_report("No machine specified, and there is no default");
2435 error_printf("Use -machine help to list supported machines\n");
2440 g_slist_free(machines
);
2441 return machine_class
;
2444 static int object_parse_property_opt(Object
*obj
,
2445 const char *name
, const char *value
,
2446 const char *skip
, Error
**errp
)
2448 Error
*local_err
= NULL
;
2450 if (g_str_equal(name
, skip
)) {
2454 object_property_parse(obj
, value
, name
, &local_err
);
2457 error_propagate(errp
, local_err
);
2464 static int machine_set_property(void *opaque
,
2465 const char *name
, const char *value
,
2468 g_autofree
char *qom_name
= g_strdup(name
);
2471 for (p
= qom_name
; *p
; p
++) {
2477 /* Legacy options do not correspond to MachineState properties. */
2478 if (g_str_equal(qom_name
, "accel")) {
2481 if (g_str_equal(qom_name
, "igd-passthru")) {
2482 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), qom_name
, value
);
2485 if (g_str_equal(qom_name
, "kvm-shadow-mem") ||
2486 g_str_equal(qom_name
, "kernel-irqchip")) {
2487 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), qom_name
, value
);
2491 return object_parse_property_opt(opaque
, name
, value
, "type", errp
);
2495 * Initial object creation happens before all other
2496 * QEMU data types are created. The majority of objects
2497 * can be created at this point. The rng-egd object
2498 * cannot be created here, as it depends on the chardev
2501 static bool object_create_initial(const char *type
, QemuOpts
*opts
)
2503 if (user_creatable_print_help(type
, opts
)) {
2508 * Objects should not be made "delayed" without a reason. If you
2509 * add one, state the reason in a comment!
2512 /* Reason: rng-egd property "chardev" */
2513 if (g_str_equal(type
, "rng-egd")) {
2517 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
2518 /* Reason: cryptodev-vhost-user property "chardev" */
2519 if (g_str_equal(type
, "cryptodev-vhost-user")) {
2525 * Reason: filter-* property "netdev" etc.
2527 if (g_str_equal(type
, "filter-buffer") ||
2528 g_str_equal(type
, "filter-dump") ||
2529 g_str_equal(type
, "filter-mirror") ||
2530 g_str_equal(type
, "filter-redirector") ||
2531 g_str_equal(type
, "colo-compare") ||
2532 g_str_equal(type
, "filter-rewriter") ||
2533 g_str_equal(type
, "filter-replay")) {
2537 /* Memory allocation by backends needs to be done
2538 * after configure_accelerator() (due to the tcg_enabled()
2539 * checks at memory_region_init_*()).
2541 * Also, allocation of large amounts of memory may delay
2542 * chardev initialization for too long, and trigger timeouts
2543 * on software that waits for a monitor socket to be created
2546 if (g_str_has_prefix(type
, "memory-backend-")) {
2555 * The remainder of object creation happens after the
2556 * creation of chardev, fsdev, net clients and device data types.
2558 static bool object_create_delayed(const char *type
, QemuOpts
*opts
)
2560 return !object_create_initial(type
, opts
);
2564 static bool set_memory_options(uint64_t *ram_slots
, ram_addr_t
*maxram_size
,
2568 const char *mem_str
;
2569 const ram_addr_t default_ram_size
= mc
->default_ram_size
;
2570 QemuOpts
*opts
= qemu_find_opts_singleton("memory");
2573 loc_push_none(&loc
);
2574 qemu_opts_loc_restore(opts
);
2577 mem_str
= qemu_opt_get(opts
, "size");
2580 error_report("missing 'size' option value");
2584 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
2586 /* Fix up legacy suffix-less format */
2587 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
2588 uint64_t overflow_check
= sz
;
2591 if (sz
/ MiB
!= overflow_check
) {
2592 error_report("too large 'size' option value");
2598 /* backward compatibility behaviour for case "-m 0" */
2600 sz
= default_ram_size
;
2603 sz
= QEMU_ALIGN_UP(sz
, 8192);
2605 if (ram_size
!= sz
) {
2606 error_report("ram size too large");
2610 /* store value for the future use */
2611 qemu_opt_set_number(opts
, "size", ram_size
, &error_abort
);
2612 *maxram_size
= ram_size
;
2614 if (qemu_opt_get(opts
, "maxmem")) {
2617 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
2618 slots
= qemu_opt_get_number(opts
, "slots", 0);
2619 if (sz
< ram_size
) {
2620 error_report("invalid value of -m option maxmem: "
2621 "maximum memory size (0x%" PRIx64
") must be at least "
2622 "the initial memory size (0x" RAM_ADDR_FMT
")",
2625 } else if (slots
&& sz
== ram_size
) {
2626 error_report("invalid value of -m option maxmem: "
2627 "memory slots were specified but maximum memory size "
2628 "(0x%" PRIx64
") is equal to the initial memory size "
2629 "(0x" RAM_ADDR_FMT
")", sz
, ram_size
);
2635 } else if (qemu_opt_get(opts
, "slots")) {
2636 error_report("invalid -m option value: missing 'maxmem' option");
2644 static int global_init_func(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2648 g
= g_malloc0(sizeof(*g
));
2649 g
->driver
= qemu_opt_get(opts
, "driver");
2650 g
->property
= qemu_opt_get(opts
, "property");
2651 g
->value
= qemu_opt_get(opts
, "value");
2652 qdev_prop_register_global(g
);
2656 static int qemu_read_default_config_file(void)
2660 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2661 if (ret
< 0 && ret
!= -ENOENT
) {
2668 static void user_register_global_props(void)
2670 qemu_opts_foreach(qemu_find_opts("global"),
2671 global_init_func
, NULL
, NULL
);
2674 static int do_configure_icount(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2676 configure_icount(opts
, errp
);
2680 static int accelerator_set_property(void *opaque
,
2681 const char *name
, const char *value
,
2684 return object_parse_property_opt(opaque
, name
, value
, "accel", errp
);
2687 static int do_configure_accelerator(void *opaque
, QemuOpts
*opts
, Error
**errp
)
2689 bool *p_init_failed
= opaque
;
2690 const char *acc
= qemu_opt_get(opts
, "accel");
2691 AccelClass
*ac
= accel_find(acc
);
2696 *p_init_failed
= true;
2697 error_report("invalid accelerator %s", acc
);
2700 accel
= ACCEL(object_new_with_class(OBJECT_CLASS(ac
)));
2701 object_apply_compat_props(OBJECT(accel
));
2702 qemu_opt_foreach(opts
, accelerator_set_property
,
2706 ret
= accel_init_machine(accel
, current_machine
);
2708 *p_init_failed
= true;
2709 error_report("failed to initialize %s: %s",
2710 acc
, strerror(-ret
));
2717 static void configure_accelerators(const char *progname
)
2720 bool init_failed
= false;
2722 qemu_opts_foreach(qemu_find_opts("icount"),
2723 do_configure_icount
, NULL
, &error_fatal
);
2725 accel
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2726 if (QTAILQ_EMPTY(&qemu_accel_opts
.head
)) {
2727 char **accel_list
, **tmp
;
2729 if (accel
== NULL
) {
2730 /* Select the default accelerator */
2731 bool have_tcg
= accel_find("tcg");
2732 bool have_kvm
= accel_find("kvm");
2734 if (have_tcg
&& have_kvm
) {
2735 if (g_str_has_suffix(progname
, "kvm")) {
2736 /* If the program name ends with "kvm", we prefer KVM */
2741 } else if (have_kvm
) {
2743 } else if (have_tcg
) {
2746 error_report("No accelerator selected and"
2747 " no default accelerator available");
2751 accel_list
= g_strsplit(accel
, ":", 0);
2753 for (tmp
= accel_list
; *tmp
; tmp
++) {
2755 * Filter invalid accelerators here, to prevent obscenities
2756 * such as "-machine accel=tcg,,thread=single".
2758 if (accel_find(*tmp
)) {
2759 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp
, true);
2762 error_report("invalid accelerator %s", *tmp
);
2765 g_strfreev(accel_list
);
2767 if (accel
!= NULL
) {
2768 error_report("The -accel and \"-machine accel=\" options are incompatible");
2773 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2774 do_configure_accelerator
, &init_failed
, &error_fatal
)) {
2776 error_report("no accelerator found");
2782 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
2783 error_report("falling back to %s", ac
->name
);
2786 if (use_icount
&& !(tcg_enabled() || qtest_enabled())) {
2787 error_report("-icount is not allowed with hardware virtualization");
2792 static void create_default_memdev(MachineState
*ms
, const char *path
)
2795 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
2797 obj
= object_new(path
? TYPE_MEMORY_BACKEND_FILE
: TYPE_MEMORY_BACKEND_RAM
);
2799 object_property_set_str(obj
, path
, "mem-path", &error_fatal
);
2801 object_property_set_int(obj
, ms
->ram_size
, "size", &error_fatal
);
2802 object_property_add_child(object_get_objects_root(), mc
->default_ram_id
,
2804 /* Ensure backend's memory region name is equal to mc->default_ram_id */
2805 object_property_set_bool(obj
, false, "x-use-canonical-path-for-ramblock-id",
2807 user_creatable_complete(USER_CREATABLE(obj
), &error_fatal
);
2809 object_property_set_str(OBJECT(ms
), mc
->default_ram_id
, "memory-backend",
2813 void qemu_init(int argc
, char **argv
, char **envp
)
2816 int snapshot
, linux_boot
;
2817 const char *initrd_filename
;
2818 const char *kernel_filename
, *kernel_cmdline
;
2819 const char *boot_order
= NULL
;
2820 const char *boot_once
= NULL
;
2822 QemuOpts
*opts
, *machine_opts
;
2823 QemuOpts
*icount_opts
= NULL
, *accel_opts
= NULL
;
2824 QemuOptsList
*olist
;
2827 const char *loadvm
= NULL
;
2828 MachineClass
*machine_class
;
2829 const char *cpu_option
;
2830 const char *vga_model
= NULL
;
2831 const char *qtest_chrdev
= NULL
;
2832 const char *qtest_log
= NULL
;
2833 const char *incoming
= NULL
;
2834 bool userconfig
= true;
2835 bool nographic
= false;
2836 int display_remote
= 0;
2837 const char *log_mask
= NULL
;
2838 const char *log_file
= NULL
;
2839 char *trace_file
= NULL
;
2840 ram_addr_t maxram_size
;
2841 uint64_t ram_slots
= 0;
2842 FILE *vmstate_dump_file
= NULL
;
2843 Error
*main_loop_err
= NULL
;
2845 bool list_data_dirs
= false;
2847 const char *mem_path
= NULL
;
2848 bool have_custom_ram_size
;
2849 BlockdevOptionsQueue bdo_queue
= QSIMPLEQ_HEAD_INITIALIZER(bdo_queue
);
2850 QemuPluginList plugin_list
= QTAILQ_HEAD_INITIALIZER(plugin_list
);
2851 int mem_prealloc
= 0; /* force preallocation of physical target memory */
2853 os_set_line_buffering();
2855 error_init(argv
[0]);
2856 module_call_init(MODULE_INIT_TRACE
);
2858 qemu_init_cpu_list();
2859 qemu_init_cpu_loop();
2861 qemu_mutex_lock_iothread();
2863 atexit(qemu_run_exit_notifiers
);
2864 qemu_init_exec_dir(argv
[0]);
2866 module_call_init(MODULE_INIT_QOM
);
2867 module_call_init(MODULE_INIT_MIGRATION
);
2869 qemu_add_opts(&qemu_drive_opts
);
2870 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2871 qemu_add_drive_opts(&qemu_common_drive_opts
);
2872 qemu_add_drive_opts(&qemu_drive_opts
);
2873 qemu_add_drive_opts(&bdrv_runtime_opts
);
2874 qemu_add_opts(&qemu_chardev_opts
);
2875 qemu_add_opts(&qemu_device_opts
);
2876 qemu_add_opts(&qemu_netdev_opts
);
2877 qemu_add_opts(&qemu_nic_opts
);
2878 qemu_add_opts(&qemu_net_opts
);
2879 qemu_add_opts(&qemu_rtc_opts
);
2880 qemu_add_opts(&qemu_global_opts
);
2881 qemu_add_opts(&qemu_mon_opts
);
2882 qemu_add_opts(&qemu_trace_opts
);
2883 qemu_plugin_add_opts();
2884 qemu_add_opts(&qemu_option_rom_opts
);
2885 qemu_add_opts(&qemu_machine_opts
);
2886 qemu_add_opts(&qemu_accel_opts
);
2887 qemu_add_opts(&qemu_mem_opts
);
2888 qemu_add_opts(&qemu_smp_opts
);
2889 qemu_add_opts(&qemu_boot_opts
);
2890 qemu_add_opts(&qemu_add_fd_opts
);
2891 qemu_add_opts(&qemu_object_opts
);
2892 qemu_add_opts(&qemu_tpmdev_opts
);
2893 qemu_add_opts(&qemu_realtime_opts
);
2894 qemu_add_opts(&qemu_overcommit_opts
);
2895 qemu_add_opts(&qemu_msg_opts
);
2896 qemu_add_opts(&qemu_name_opts
);
2897 qemu_add_opts(&qemu_numa_opts
);
2898 qemu_add_opts(&qemu_icount_opts
);
2899 qemu_add_opts(&qemu_semihosting_config_opts
);
2900 qemu_add_opts(&qemu_fw_cfg_opts
);
2901 module_call_init(MODULE_INIT_OPTS
);
2904 precopy_infrastructure_init();
2905 postcopy_infrastructure_init();
2906 monitor_init_globals();
2908 if (qcrypto_init(&err
) < 0) {
2909 error_reportf_err(err
, "cannot initialize crypto: ");
2913 QTAILQ_INIT(&vm_change_state_head
);
2914 os_setup_early_signal_handling();
2921 bdrv_init_with_whitelist();
2925 /* first pass of option parsing */
2927 while (optind
< argc
) {
2928 if (argv
[optind
][0] != '-') {
2932 const QEMUOption
*popt
;
2934 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2935 switch (popt
->index
) {
2936 case QEMU_OPTION_nouserconfig
:
2944 if (qemu_read_default_config_file() < 0) {
2949 /* second pass of option parsing */
2954 if (argv
[optind
][0] != '-') {
2955 loc_set_cmdline(argv
, optind
, 1);
2956 drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2958 const QEMUOption
*popt
;
2960 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2961 if (!(popt
->arch_mask
& arch_type
)) {
2962 error_report("Option not supported for this target");
2965 switch(popt
->index
) {
2966 case QEMU_OPTION_cpu
:
2967 /* hw initialization will check this */
2968 cpu_option
= optarg
;
2970 case QEMU_OPTION_hda
:
2971 case QEMU_OPTION_hdb
:
2972 case QEMU_OPTION_hdc
:
2973 case QEMU_OPTION_hdd
:
2974 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2977 case QEMU_OPTION_blockdev
:
2980 BlockdevOptionsQueueEntry
*bdo
;
2982 v
= qobject_input_visitor_new_str(optarg
, "driver",
2985 bdo
= g_new(BlockdevOptionsQueueEntry
, 1);
2986 visit_type_BlockdevOptions(v
, NULL
, &bdo
->bdo
,
2989 loc_save(&bdo
->loc
);
2990 QSIMPLEQ_INSERT_TAIL(&bdo_queue
, bdo
, entry
);
2993 case QEMU_OPTION_drive
:
2994 if (drive_def(optarg
) == NULL
) {
2998 case QEMU_OPTION_set
:
2999 if (qemu_set_option(optarg
) != 0)
3002 case QEMU_OPTION_global
:
3003 if (qemu_global_option(optarg
) != 0)
3006 case QEMU_OPTION_mtdblock
:
3007 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3009 case QEMU_OPTION_sd
:
3010 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3012 case QEMU_OPTION_pflash
:
3013 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3015 case QEMU_OPTION_snapshot
:
3017 Error
*blocker
= NULL
;
3019 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
,
3021 replay_add_blocker(blocker
);
3024 case QEMU_OPTION_numa
:
3025 opts
= qemu_opts_parse_noisily(qemu_find_opts("numa"),
3031 case QEMU_OPTION_display
:
3032 parse_display(optarg
);
3034 case QEMU_OPTION_nographic
:
3035 olist
= qemu_find_opts("machine");
3036 qemu_opts_parse_noisily(olist
, "graphics=off", false);
3038 dpy
.type
= DISPLAY_TYPE_NONE
;
3040 case QEMU_OPTION_curses
:
3041 #ifdef CONFIG_CURSES
3042 dpy
.type
= DISPLAY_TYPE_CURSES
;
3044 error_report("curses or iconv support is disabled");
3048 case QEMU_OPTION_portrait
:
3049 graphic_rotate
= 90;
3051 case QEMU_OPTION_rotate
:
3052 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3053 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3054 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3055 error_report("only 90, 180, 270 deg rotation is available");
3059 case QEMU_OPTION_kernel
:
3060 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
,
3063 case QEMU_OPTION_initrd
:
3064 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
,
3067 case QEMU_OPTION_append
:
3068 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
,
3071 case QEMU_OPTION_dtb
:
3072 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
,
3075 case QEMU_OPTION_cdrom
:
3076 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3078 case QEMU_OPTION_boot
:
3079 opts
= qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3085 case QEMU_OPTION_fda
:
3086 case QEMU_OPTION_fdb
:
3087 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3090 case QEMU_OPTION_no_fd_bootchk
:
3093 case QEMU_OPTION_netdev
:
3095 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3099 case QEMU_OPTION_nic
:
3101 if (net_client_parse(qemu_find_opts("nic"), optarg
) == -1) {
3105 case QEMU_OPTION_net
:
3107 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3111 #ifdef CONFIG_LIBISCSI
3112 case QEMU_OPTION_iscsi
:
3113 opts
= qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3120 case QEMU_OPTION_audio_help
:
3121 audio_legacy_help();
3124 case QEMU_OPTION_audiodev
:
3125 audio_parse_option(optarg
);
3127 case QEMU_OPTION_soundhw
:
3128 select_soundhw (optarg
);
3133 case QEMU_OPTION_version
:
3138 opts
= qemu_opts_parse_noisily(qemu_find_opts("memory"),
3145 case QEMU_OPTION_tpmdev
:
3146 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3151 case QEMU_OPTION_mempath
:
3154 case QEMU_OPTION_mem_prealloc
:
3163 case QEMU_OPTION_DFILTER
:
3164 qemu_set_dfilter_ranges(optarg
, &error_fatal
);
3166 case QEMU_OPTION_seed
:
3167 qemu_guest_random_seed_main(optarg
, &error_fatal
);
3170 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3172 case QEMU_OPTION_gdb
:
3173 add_device_config(DEV_GDB
, optarg
);
3176 if (is_help_option(optarg
)) {
3177 list_data_dirs
= true;
3179 qemu_add_data_dir(optarg
);
3182 case QEMU_OPTION_bios
:
3183 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
,
3186 case QEMU_OPTION_singlestep
:
3193 keyboard_layout
= optarg
;
3195 case QEMU_OPTION_vga
:
3204 w
= strtol(p
, (char **)&p
, 10);
3207 error_report("invalid resolution or depth");
3213 h
= strtol(p
, (char **)&p
, 10);
3218 depth
= strtol(p
, (char **)&p
, 10);
3219 if (depth
!= 1 && depth
!= 2 && depth
!= 4 &&
3220 depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3221 depth
!= 24 && depth
!= 32)
3223 } else if (*p
== '\0') {
3224 depth
= graphic_depth
;
3231 graphic_depth
= depth
;
3234 case QEMU_OPTION_echr
:
3237 term_escape_char
= strtol(optarg
, &r
, 0);
3239 printf("Bad argument to echr\n");
3242 case QEMU_OPTION_monitor
:
3243 default_monitor
= 0;
3244 if (strncmp(optarg
, "none", 4)) {
3245 monitor_parse(optarg
, "readline", false);
3248 case QEMU_OPTION_qmp
:
3249 monitor_parse(optarg
, "control", false);
3250 default_monitor
= 0;
3252 case QEMU_OPTION_qmp_pretty
:
3253 monitor_parse(optarg
, "control", true);
3254 default_monitor
= 0;
3256 case QEMU_OPTION_mon
:
3257 opts
= qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg
,
3262 default_monitor
= 0;
3264 case QEMU_OPTION_chardev
:
3265 opts
= qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3271 case QEMU_OPTION_fsdev
:
3272 olist
= qemu_find_opts("fsdev");
3274 error_report("fsdev support is disabled");
3277 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3282 case QEMU_OPTION_virtfs
: {
3285 const char *writeout
, *sock_fd
, *socket
, *path
, *security_model
,
3288 olist
= qemu_find_opts("virtfs");
3290 error_report("virtfs support is disabled");
3293 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3298 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3299 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3300 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3303 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3304 qemu_opts_id(opts
) ?:
3305 qemu_opt_get(opts
, "mount_tag"),
3308 error_report("duplicate or invalid fsdev id: %s",
3309 qemu_opt_get(opts
, "mount_tag"));
3313 writeout
= qemu_opt_get(opts
, "writeout");
3315 #ifdef CONFIG_SYNC_FILE_RANGE
3316 qemu_opt_set(fsdev
, "writeout", writeout
, &error_abort
);
3318 error_report("writeout=immediate not supported "
3319 "on this platform");
3323 qemu_opt_set(fsdev
, "fsdriver",
3324 qemu_opt_get(opts
, "fsdriver"), &error_abort
);
3325 path
= qemu_opt_get(opts
, "path");
3327 qemu_opt_set(fsdev
, "path", path
, &error_abort
);
3329 security_model
= qemu_opt_get(opts
, "security_model");
3330 if (security_model
) {
3331 qemu_opt_set(fsdev
, "security_model", security_model
,
3334 socket
= qemu_opt_get(opts
, "socket");
3336 qemu_opt_set(fsdev
, "socket", socket
, &error_abort
);
3338 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3340 qemu_opt_set(fsdev
, "sock_fd", sock_fd
, &error_abort
);
3343 qemu_opt_set_bool(fsdev
, "readonly",
3344 qemu_opt_get_bool(opts
, "readonly", 0),
3346 multidevs
= qemu_opt_get(opts
, "multidevs");
3348 qemu_opt_set(fsdev
, "multidevs", multidevs
, &error_abort
);
3350 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3352 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3353 qemu_opt_set(device
, "fsdev",
3354 qemu_opts_id(fsdev
), &error_abort
);
3355 qemu_opt_set(device
, "mount_tag",
3356 qemu_opt_get(opts
, "mount_tag"), &error_abort
);
3359 case QEMU_OPTION_serial
:
3360 add_device_config(DEV_SERIAL
, optarg
);
3362 if (strncmp(optarg
, "mon:", 4) == 0) {
3363 default_monitor
= 0;
3366 case QEMU_OPTION_watchdog
:
3368 error_report("only one watchdog option may be given");
3373 case QEMU_OPTION_watchdog_action
:
3374 if (select_watchdog_action(optarg
) == -1) {
3375 error_report("unknown -watchdog-action parameter");
3379 case QEMU_OPTION_parallel
:
3380 add_device_config(DEV_PARALLEL
, optarg
);
3381 default_parallel
= 0;
3382 if (strncmp(optarg
, "mon:", 4) == 0) {
3383 default_monitor
= 0;
3386 case QEMU_OPTION_debugcon
:
3387 add_device_config(DEV_DEBUGCON
, optarg
);
3389 case QEMU_OPTION_loadvm
:
3392 case QEMU_OPTION_full_screen
:
3393 dpy
.has_full_screen
= true;
3394 dpy
.full_screen
= true;
3396 case QEMU_OPTION_alt_grab
:
3399 case QEMU_OPTION_ctrl_grab
:
3402 case QEMU_OPTION_no_quit
:
3403 dpy
.has_window_close
= true;
3404 dpy
.window_close
= false;
3406 case QEMU_OPTION_sdl
:
3408 dpy
.type
= DISPLAY_TYPE_SDL
;
3411 error_report("SDL support is disabled");
3414 case QEMU_OPTION_pidfile
:
3417 case QEMU_OPTION_win2k_hack
:
3418 win2k_install_hack
= 1;
3420 case QEMU_OPTION_acpitable
:
3421 opts
= qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3426 acpi_table_add(opts
, &error_fatal
);
3428 case QEMU_OPTION_smbios
:
3429 opts
= qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3434 smbios_entry_add(opts
, &error_fatal
);
3436 case QEMU_OPTION_fwcfg
:
3437 opts
= qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3443 case QEMU_OPTION_preconfig
:
3444 preconfig_exit_requested
= false;
3446 case QEMU_OPTION_enable_kvm
:
3447 olist
= qemu_find_opts("machine");
3448 qemu_opts_parse_noisily(olist
, "accel=kvm", false);
3451 case QEMU_OPTION_machine
:
3452 olist
= qemu_find_opts("machine");
3453 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3458 case QEMU_OPTION_no_kvm
:
3459 olist
= qemu_find_opts("machine");
3460 qemu_opts_parse_noisily(olist
, "accel=tcg", false);
3462 case QEMU_OPTION_accel
:
3463 accel_opts
= qemu_opts_parse_noisily(qemu_find_opts("accel"),
3465 optarg
= qemu_opt_get(accel_opts
, "accel");
3466 if (!optarg
|| is_help_option(optarg
)) {
3467 printf("Accelerators supported in QEMU binary:\n");
3468 GSList
*el
, *accel_list
= object_class_get_list(TYPE_ACCEL
,
3470 for (el
= accel_list
; el
; el
= el
->next
) {
3471 gchar
*typename
= g_strdup(object_class_get_name(
3472 OBJECT_CLASS(el
->data
)));
3473 /* omit qtest which is used for tests only */
3474 if (g_strcmp0(typename
, ACCEL_CLASS_NAME("qtest")) &&
3475 g_str_has_suffix(typename
, ACCEL_CLASS_SUFFIX
)) {
3476 gchar
**optname
= g_strsplit(typename
,
3477 ACCEL_CLASS_SUFFIX
, 0);
3478 printf("%s\n", optname
[0]);
3479 g_strfreev(optname
);
3483 g_slist_free(accel_list
);
3486 if (optarg
&& strchr(optarg
, ':')) {
3487 error_report("Don't use ':' with -accel, "
3488 "use -M accel=... for now instead");
3492 case QEMU_OPTION_usb
:
3493 olist
= qemu_find_opts("machine");
3494 qemu_opts_parse_noisily(olist
, "usb=on", false);
3496 case QEMU_OPTION_usbdevice
:
3497 error_report("'-usbdevice' is deprecated, please use "
3498 "'-device usb-...' instead");
3499 olist
= qemu_find_opts("machine");
3500 qemu_opts_parse_noisily(olist
, "usb=on", false);
3501 add_device_config(DEV_USB
, optarg
);
3503 case QEMU_OPTION_device
:
3504 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3509 case QEMU_OPTION_smp
:
3510 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3515 case QEMU_OPTION_vnc
:
3516 vnc_parse(optarg
, &error_fatal
);
3518 case QEMU_OPTION_no_acpi
:
3521 case QEMU_OPTION_no_hpet
:
3524 case QEMU_OPTION_no_reboot
:
3527 case QEMU_OPTION_no_shutdown
:
3530 case QEMU_OPTION_show_cursor
:
3531 warn_report("The -show-cursor option is deprecated, "
3532 "use -display {sdl,gtk},show-cursor=on instead");
3533 dpy
.has_show_cursor
= true;
3534 dpy
.show_cursor
= true;
3536 case QEMU_OPTION_uuid
:
3537 if (qemu_uuid_parse(optarg
, &qemu_uuid
) < 0) {
3538 error_report("failed to parse UUID string: wrong format");
3541 qemu_uuid_set
= true;
3543 case QEMU_OPTION_option_rom
:
3544 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3545 error_report("too many option ROMs");
3548 opts
= qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3553 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3554 option_rom
[nb_option_roms
].bootindex
=
3555 qemu_opt_get_number(opts
, "bootindex", -1);
3556 if (!option_rom
[nb_option_roms
].name
) {
3557 error_report("Option ROM file is not specified");
3562 case QEMU_OPTION_semihosting
:
3563 qemu_semihosting_enable();
3565 case QEMU_OPTION_semihosting_config
:
3566 if (qemu_semihosting_config_options(optarg
) != 0) {
3570 case QEMU_OPTION_name
:
3571 opts
= qemu_opts_parse_noisily(qemu_find_opts("name"),
3577 case QEMU_OPTION_prom_env
:
3578 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3579 error_report("too many prom variables");
3582 prom_envs
[nb_prom_envs
] = optarg
;
3585 case QEMU_OPTION_old_param
:
3588 case QEMU_OPTION_rtc
:
3589 opts
= qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg
,
3595 case QEMU_OPTION_tb_size
:
3597 error_report("TCG is disabled");
3600 warn_report("The -tb-size option is deprecated, use -accel tcg,tb-size instead");
3601 object_register_sugar_prop(ACCEL_CLASS_NAME("tcg"), "tb-size", optarg
);
3603 case QEMU_OPTION_icount
:
3604 icount_opts
= qemu_opts_parse_noisily(qemu_find_opts("icount"),
3610 case QEMU_OPTION_incoming
:
3612 runstate_set(RUN_STATE_INMIGRATE
);
3616 case QEMU_OPTION_only_migratable
:
3617 only_migratable
= 1;
3619 case QEMU_OPTION_nodefaults
:
3622 case QEMU_OPTION_xen_domid
:
3623 if (!(xen_available())) {
3624 error_report("Option not supported for this target");
3627 xen_domid
= atoi(optarg
);
3629 case QEMU_OPTION_xen_attach
:
3630 if (!(xen_available())) {
3631 error_report("Option not supported for this target");
3634 xen_mode
= XEN_ATTACH
;
3636 case QEMU_OPTION_xen_domid_restrict
:
3637 if (!(xen_available())) {
3638 error_report("Option not supported for this target");
3641 xen_domid_restrict
= true;
3643 case QEMU_OPTION_trace
:
3645 trace_file
= trace_opt_parse(optarg
);
3647 case QEMU_OPTION_plugin
:
3648 qemu_plugin_opt_parse(optarg
, &plugin_list
);
3650 case QEMU_OPTION_readconfig
:
3652 int ret
= qemu_read_config_file(optarg
);
3654 error_report("read config %s: %s", optarg
,
3660 case QEMU_OPTION_spice
:
3661 olist
= qemu_find_opts("spice");
3663 error_report("spice support is disabled");
3666 opts
= qemu_opts_parse_noisily(olist
, optarg
, false);
3672 case QEMU_OPTION_writeconfig
:
3675 if (strcmp(optarg
, "-") == 0) {
3678 fp
= fopen(optarg
, "w");
3680 error_report("open %s: %s", optarg
,
3685 qemu_config_write(fp
);
3691 case QEMU_OPTION_qtest
:
3692 qtest_chrdev
= optarg
;
3694 case QEMU_OPTION_qtest_log
:
3697 case QEMU_OPTION_sandbox
:
3698 olist
= qemu_find_opts("sandbox");
3700 #ifndef CONFIG_SECCOMP
3701 error_report("-sandbox support is not enabled "
3702 "in this QEMU binary");
3707 opts
= qemu_opts_parse_noisily(olist
, optarg
, true);
3712 case QEMU_OPTION_add_fd
:
3714 opts
= qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3720 error_report("File descriptor passing is disabled on this "
3725 case QEMU_OPTION_object
:
3726 opts
= qemu_opts_parse_noisily(qemu_find_opts("object"),
3732 case QEMU_OPTION_realtime
:
3733 warn_report("'-realtime mlock=...' is deprecated, please use "
3734 "'-overcommit mem-lock=...' instead");
3735 opts
= qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3740 /* Don't override the -overcommit option if set */
3741 enable_mlock
= enable_mlock
||
3742 qemu_opt_get_bool(opts
, "mlock", true);
3744 case QEMU_OPTION_overcommit
:
3745 opts
= qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3750 /* Don't override the -realtime option if set */
3751 enable_mlock
= enable_mlock
||
3752 qemu_opt_get_bool(opts
, "mem-lock", false);
3753 enable_cpu_pm
= qemu_opt_get_bool(opts
, "cpu-pm", false);
3755 case QEMU_OPTION_msg
:
3756 opts
= qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg
,
3761 configure_msg(opts
);
3763 case QEMU_OPTION_dump_vmstate
:
3764 if (vmstate_dump_file
) {
3765 error_report("only one '-dump-vmstate' "
3766 "option may be given");
3769 vmstate_dump_file
= fopen(optarg
, "w");
3770 if (vmstate_dump_file
== NULL
) {
3771 error_report("open %s: %s", optarg
, strerror(errno
));
3775 case QEMU_OPTION_enable_sync_profile
:
3778 case QEMU_OPTION_nouserconfig
:
3779 /* Nothing to be parsed here. Especially, do not error out below. */
3782 if (os_parse_cmd_args(popt
->index
, optarg
)) {
3783 error_report("Option not supported in this build");
3790 * Clear error location left behind by the loop.
3791 * Best done right after the loop. Do not insert code here!
3796 * Check for -cpu help and -device help before we call select_machine(),
3797 * which will return an error if the architecture has no default machine
3798 * type and the user did not specify one, so that the user doesn't need
3799 * to say '-cpu help -machine something'.
3801 if (cpu_option
&& is_help_option(cpu_option
)) {
3802 list_cpus(cpu_option
);
3806 if (qemu_opts_foreach(qemu_find_opts("device"),
3807 device_help_func
, NULL
, NULL
)) {
3811 user_register_global_props();
3813 replay_configure(icount_opts
);
3815 if (incoming
&& !preconfig_exit_requested
) {
3816 error_report("'preconfig' and 'incoming' options are "
3817 "mutually exclusive");
3821 configure_rtc(qemu_find_opts_singleton("rtc"));
3823 machine_class
= select_machine();
3824 object_set_machine_compat_props(machine_class
->compat_props
);
3826 have_custom_ram_size
= set_memory_options(&ram_slots
, &maxram_size
,
3832 * If QTest is enabled, keep the rcu_atfork enabled, since system processes
3833 * may be forked testing purposes (e.g. fork-server based fuzzing) The fork
3834 * should happen before a signle cpu instruction is executed, to prevent
3835 * deadlocks. See commit 73c6e40, rcu: "completely disable pthread_atfork
3836 * callbacks as soon as possible"
3838 if (!qtest_enabled()) {
3839 rcu_disable_atfork();
3842 if (pid_file
&& !qemu_write_pidfile(pid_file
, &err
)) {
3843 error_reportf_err(err
, "cannot create PID file: ");
3847 qemu_unlink_pidfile_notifier
.notify
= qemu_unlink_pidfile
;
3848 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier
);
3850 if (qemu_init_main_loop(&main_loop_err
)) {
3851 error_report_err(main_loop_err
);
3855 #ifdef CONFIG_SECCOMP
3856 olist
= qemu_find_opts_err("sandbox", NULL
);
3858 qemu_opts_foreach(olist
, parse_sandbox
, NULL
, &error_fatal
);
3862 qemu_opts_foreach(qemu_find_opts("name"),
3863 parse_name
, NULL
, &error_fatal
);
3866 qemu_opts_foreach(qemu_find_opts("add-fd"),
3867 parse_add_fd
, NULL
, &error_fatal
);
3869 qemu_opts_foreach(qemu_find_opts("add-fd"),
3870 cleanup_add_fd
, NULL
, &error_fatal
);
3873 current_machine
= MACHINE(object_new_with_class(OBJECT_CLASS(machine_class
)));
3874 if (machine_help_func(qemu_get_machine_opts(), current_machine
)) {
3877 object_property_add_child(object_get_root(), "machine",
3878 OBJECT(current_machine
), &error_abort
);
3879 object_property_add_child(container_get(OBJECT(current_machine
),
3881 "sysbus", OBJECT(sysbus_get_default()),
3884 if (machine_class
->minimum_page_bits
) {
3885 if (!set_preferred_target_page_bits(machine_class
->minimum_page_bits
)) {
3886 /* This would be a board error: specifying a minimum smaller than
3887 * a target's compile-time fixed setting.
3889 g_assert_not_reached();
3893 cpu_exec_init_all();
3895 if (machine_class
->hw_version
) {
3896 qemu_set_hw_version(machine_class
->hw_version
);
3899 if (!trace_init_backends()) {
3902 trace_init_file(trace_file
);
3904 /* Open the logfile at this point and set the log mask if necessary.
3906 qemu_set_log_filename(log_file
, &error_fatal
);
3909 mask
= qemu_str_to_log_mask(log_mask
);
3911 qemu_print_log_usage(stdout
);
3919 /* add configured firmware directories */
3920 dirs
= g_strsplit(CONFIG_QEMU_FIRMWAREPATH
, G_SEARCHPATH_SEPARATOR_S
, 0);
3921 for (i
= 0; dirs
[i
] != NULL
; i
++) {
3922 qemu_add_data_dir(dirs
[i
]);
3926 /* try to find datadir relative to the executable path */
3927 dir
= os_find_datadir();
3928 qemu_add_data_dir(dir
);
3931 /* add the datadir specified when building */
3932 qemu_add_data_dir(CONFIG_QEMU_DATADIR
);
3934 /* -L help lists the data directories and exits. */
3935 if (list_data_dirs
) {
3936 for (i
= 0; i
< data_dir_idx
; i
++) {
3937 printf("%s\n", data_dir
[i
]);
3942 /* machine_class: default to UP */
3943 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1;
3944 machine_class
->min_cpus
= machine_class
->min_cpus
?: 1;
3945 machine_class
->default_cpus
= machine_class
->default_cpus
?: 1;
3947 /* default to machine_class->default_cpus */
3948 current_machine
->smp
.cpus
= machine_class
->default_cpus
;
3949 current_machine
->smp
.max_cpus
= machine_class
->default_cpus
;
3950 current_machine
->smp
.cores
= 1;
3951 current_machine
->smp
.threads
= 1;
3952 current_machine
->smp
.sockets
= 1;
3954 machine_class
->smp_parse(current_machine
,
3955 qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3957 /* sanity-check smp_cpus and max_cpus against machine_class */
3958 if (current_machine
->smp
.cpus
< machine_class
->min_cpus
) {
3959 error_report("Invalid SMP CPUs %d. The min CPUs "
3960 "supported by machine '%s' is %d",
3961 current_machine
->smp
.cpus
,
3962 machine_class
->name
, machine_class
->min_cpus
);
3965 if (current_machine
->smp
.max_cpus
> machine_class
->max_cpus
) {
3966 error_report("Invalid SMP CPUs %d. The max CPUs "
3967 "supported by machine '%s' is %d",
3968 current_machine
->smp
.max_cpus
,
3969 machine_class
->name
, machine_class
->max_cpus
);
3976 val
= g_strdup_printf("%d", current_machine
->smp
.cpus
);
3977 object_register_sugar_prop("memory-backend", "prealloc-threads", val
);
3979 object_register_sugar_prop("memory-backend", "prealloc", "on");
3983 * Get the default machine options from the machine if it is not already
3984 * specified either by the configuration file or by the command line.
3986 if (machine_class
->default_machine_opts
) {
3987 qemu_opts_set_defaults(qemu_find_opts("machine"),
3988 machine_class
->default_machine_opts
, 0);
3991 /* process plugin before CPUs are created, but once -smp has been parsed */
3992 if (qemu_plugin_load_list(&plugin_list
)) {
3996 qemu_opts_foreach(qemu_find_opts("device"),
3997 default_driver_check
, NULL
, NULL
);
3998 qemu_opts_foreach(qemu_find_opts("global"),
3999 default_driver_check
, NULL
, NULL
);
4001 if (!vga_model
&& !default_vga
) {
4002 vga_interface_type
= VGA_DEVICE
;
4004 if (!has_defaults
|| machine_class
->no_serial
) {
4007 if (!has_defaults
|| machine_class
->no_parallel
) {
4008 default_parallel
= 0;
4010 if (!has_defaults
|| machine_class
->no_floppy
) {
4013 if (!has_defaults
|| machine_class
->no_cdrom
) {
4016 if (!has_defaults
|| machine_class
->no_sdcard
) {
4019 if (!has_defaults
) {
4020 default_monitor
= 0;
4025 if (is_daemonized()) {
4026 if (!preconfig_exit_requested
) {
4027 error_report("'preconfig' and 'daemonize' options are "
4028 "mutually exclusive");
4032 /* According to documentation and historically, -nographic redirects
4033 * serial port, parallel port and monitor to stdio, which does not work
4034 * with -daemonize. We can redirect these to null instead, but since
4035 * -nographic is legacy, let's just error out.
4036 * We disallow -nographic only if all other ports are not redirected
4037 * explicitly, to not break existing legacy setups which uses
4038 * -nographic _and_ redirects all ports explicitly - this is valid
4039 * usage, -nographic is just a no-op in this case.
4042 && (default_parallel
|| default_serial
|| default_monitor
)) {
4043 error_report("-nographic cannot be used with -daemonize");
4046 #ifdef CONFIG_CURSES
4047 if (dpy
.type
== DISPLAY_TYPE_CURSES
) {
4048 error_report("curses display cannot be used with -daemonize");
4055 if (default_parallel
)
4056 add_device_config(DEV_PARALLEL
, "null");
4057 if (default_serial
&& default_monitor
) {
4058 add_device_config(DEV_SERIAL
, "mon:stdio");
4061 add_device_config(DEV_SERIAL
, "stdio");
4062 if (default_monitor
)
4063 monitor_parse("stdio", "readline", false);
4067 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4068 if (default_parallel
)
4069 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4070 if (default_monitor
)
4071 monitor_parse("vc:80Cx24C", "readline", false);
4074 #if defined(CONFIG_VNC)
4075 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head
))) {
4079 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
&& !display_remote
) {
4080 if (!qemu_display_find_default(&dpy
)) {
4081 dpy
.type
= DISPLAY_TYPE_NONE
;
4082 #if defined(CONFIG_VNC)
4083 vnc_parse("localhost:0,to=99,id=default", &error_abort
);
4087 if (dpy
.type
== DISPLAY_TYPE_DEFAULT
) {
4088 dpy
.type
= DISPLAY_TYPE_NONE
;
4091 if ((alt_grab
|| ctrl_grab
) && dpy
.type
!= DISPLAY_TYPE_SDL
) {
4092 error_report("-alt-grab and -ctrl-grab are only valid "
4093 "for SDL, ignoring option");
4095 if (dpy
.has_window_close
&&
4096 (dpy
.type
!= DISPLAY_TYPE_GTK
&& dpy
.type
!= DISPLAY_TYPE_SDL
)) {
4097 error_report("-no-quit is only valid for GTK and SDL, "
4101 qemu_display_early_init(&dpy
);
4102 qemu_console_early_init();
4104 if (dpy
.has_gl
&& dpy
.gl
!= DISPLAYGL_MODE_OFF
&& display_opengl
== 0) {
4105 #if defined(CONFIG_OPENGL)
4106 error_report("OpenGL is not supported by the display");
4108 error_report("OpenGL support is disabled");
4116 qemu_opts_foreach(qemu_find_opts("object"),
4117 user_creatable_add_opts_foreach
,
4118 object_create_initial
, &error_fatal
);
4120 qemu_opts_foreach(qemu_find_opts("chardev"),
4121 chardev_init_func
, NULL
, &error_fatal
);
4122 /* now chardevs have been created we may have semihosting to connect */
4123 qemu_semihosting_connect_chardevs();
4125 #ifdef CONFIG_VIRTFS
4126 qemu_opts_foreach(qemu_find_opts("fsdev"),
4127 fsdev_init_func
, NULL
, &error_fatal
);
4131 * Note: we need to create block backends before
4132 * machine_set_property(), so machine properties can refer to
4135 configure_blockdev(&bdo_queue
, machine_class
, snapshot
);
4137 machine_opts
= qemu_get_machine_opts();
4138 qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4142 * Note: uses machine properties such as kernel-irqchip, must run
4143 * after machine_set_property().
4145 configure_accelerators(argv
[0]);
4148 * Beware, QOM objects created before this point miss global and
4149 * compat properties.
4151 * Global properties get set up by qdev_prop_register_global(),
4152 * called from user_register_global_props(), and certain option
4153 * desugaring. Also in CPU feature desugaring (buried in
4154 * parse_cpu_option()), which happens below this point, but may
4155 * only target the CPU type, which can only be created after
4156 * parse_cpu_option() returned the type.
4158 * Machine compat properties: object_set_machine_compat_props().
4159 * Accelerator compat props: object_set_accelerator_compat_props(),
4160 * called from configure_accelerator().
4163 if (!qtest_enabled() && machine_class
->deprecation_reason
) {
4164 error_report("Machine type '%s' is deprecated: %s",
4165 machine_class
->name
, machine_class
->deprecation_reason
);
4169 * Note: creates a QOM object, must run only after global and
4170 * compat properties have been set up.
4172 migration_object_init();
4175 qtest_server_init(qtest_chrdev
, qtest_log
, &error_fatal
);
4178 machine_opts
= qemu_get_machine_opts();
4179 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4180 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4181 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4182 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4184 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4186 boot_order
= qemu_opt_get(opts
, "order");
4188 validate_bootdevices(boot_order
, &error_fatal
);
4191 boot_once
= qemu_opt_get(opts
, "once");
4193 validate_bootdevices(boot_once
, &error_fatal
);
4196 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4197 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4201 boot_order
= machine_class
->default_boot_order
;
4204 if (!kernel_cmdline
) {
4205 kernel_cmdline
= "";
4206 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4209 linux_boot
= (kernel_filename
!= NULL
);
4211 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4212 error_report("-append only allowed with -kernel option");
4216 if (!linux_boot
&& initrd_filename
!= NULL
) {
4217 error_report("-initrd only allowed with -kernel option");
4221 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename
) {
4222 /* fall back to the -kernel/-append */
4223 semihosting_arg_fallback(kernel_filename
, kernel_cmdline
);
4226 /* spice needs the timers to be initialized by this point */
4232 QemuOptsList
*net
= qemu_find_opts("net");
4233 qemu_opts_set(net
, NULL
, "type", "nic", &error_abort
);
4235 qemu_opts_set(net
, NULL
, "type", "user", &error_abort
);
4239 if (net_init_clients(&err
) < 0) {
4240 error_report_err(err
);
4244 qemu_opts_foreach(qemu_find_opts("object"),
4245 user_creatable_add_opts_foreach
,
4246 object_create_delayed
, &error_fatal
);
4252 dirty_bitmap_mig_init();
4254 qemu_opts_foreach(qemu_find_opts("mon"),
4255 mon_init_func
, NULL
, &error_fatal
);
4257 /* connect semihosting console input if requested */
4258 qemu_semihosting_console_init();
4260 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4262 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4264 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4267 /* If no default VGA is requested, the default is "none". */
4269 vga_model
= get_default_vga_model(machine_class
);
4272 select_vgahw(machine_class
, vga_model
);
4276 i
= select_watchdog(watchdog
);
4278 exit (i
== 1 ? 1 : 0);
4281 /* This checkpoint is required by replay to separate prior clock
4282 reading from the other reads, because timer polling functions query
4283 clock values from the log. */
4284 replay_checkpoint(CHECKPOINT_INIT
);
4285 qdev_machine_init();
4287 current_machine
->boot_order
= boot_order
;
4289 /* parse features once if machine provides default cpu_type */
4290 current_machine
->cpu_type
= machine_class
->default_cpu_type
;
4292 current_machine
->cpu_type
= parse_cpu_option(cpu_option
);
4295 if (current_machine
->ram_memdev_id
) {
4297 ram_addr_t backend_size
;
4299 backend
= object_resolve_path_type(current_machine
->ram_memdev_id
,
4300 TYPE_MEMORY_BACKEND
, NULL
);
4301 backend_size
= object_property_get_uint(backend
, "size", &error_abort
);
4302 if (have_custom_ram_size
&& backend_size
!= ram_size
) {
4303 error_report("Size specified by -m option must match size of "
4304 "explicitly specified 'memory-backend' property");
4307 ram_size
= backend_size
;
4310 if (!xen_enabled()) {
4311 /* On 32-bit hosts, QEMU is limited by virtual address space */
4312 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4313 error_report("at most 2047 MB RAM can be simulated");
4318 current_machine
->ram_size
= ram_size
;
4319 current_machine
->maxram_size
= maxram_size
;
4320 current_machine
->ram_slots
= ram_slots
;
4322 parse_numa_opts(current_machine
);
4324 if (machine_class
->default_ram_id
&& current_machine
->ram_size
&&
4325 numa_uses_legacy_mem() && !current_machine
->ram_memdev_id
) {
4326 create_default_memdev(current_machine
, mem_path
);
4328 /* do monitor/qmp handling at preconfig state if requested */
4331 audio_init_audiodevs();
4333 /* from here on runstate is RUN_STATE_PRELAUNCH */
4334 machine_run_board_init(current_machine
);
4340 if (hax_enabled()) {
4344 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4345 parse_fw_cfg
, fw_cfg_find(), &error_fatal
);
4347 /* init USB devices */
4348 if (machine_usb(current_machine
)) {
4349 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4353 /* init generic devices */
4354 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE
);
4355 qemu_opts_foreach(qemu_find_opts("device"),
4356 device_init_func
, NULL
, &error_fatal
);
4358 cpu_synchronize_all_post_init();
4360 rom_reset_order_override();
4362 /* Did we create any drives that we failed to create a device for? */
4363 drive_check_orphaned();
4365 /* Don't warn about the default network setup that you get if
4366 * no command line -net or -netdev options are specified. There
4367 * are two cases that we would otherwise complain about:
4368 * (1) board doesn't support a NIC but the implicit "-net nic"
4370 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4371 * sets up a nic that isn't connected to anything.
4373 if (!default_net
&& (!qtest_enabled() || has_defaults
)) {
4374 net_check_clients();
4378 qemu_boot_set(boot_once
, &error_fatal
);
4379 qemu_register_reset(restore_boot_order
, g_strdup(boot_order
));
4382 /* init local displays */
4383 ds
= init_displaystate();
4384 qemu_display_init(ds
, &dpy
);
4386 /* must be after terminal init, SDL library changes signal handlers */
4387 os_setup_signal_handling();
4389 /* init remote displays */
4391 qemu_opts_foreach(qemu_find_opts("vnc"),
4392 vnc_init_func
, NULL
, &error_fatal
);
4396 qemu_spice_display_init();
4399 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4403 qdev_machine_creation_done();
4405 /* TODO: once all bus devices are qdevified, this should be done
4406 * when bus is created by qdev.c */
4408 * TODO: If we had a main 'reset container' that the whole system
4409 * lived in, we could reset that using the multi-phase reset
4410 * APIs. For the moment, we just reset the sysbus, which will cause
4411 * all devices hanging off it (and all their child buses, recursively)
4412 * to be reset. Note that this will *not* reset any Device objects
4413 * which are not attached to some part of the qbus tree!
4415 qemu_register_reset(resettable_cold_reset_fn
, sysbus_get_default());
4416 qemu_run_machine_init_done_notifiers();
4418 if (rom_check_and_register_reset() != 0) {
4419 error_report("rom check and register reset failed");
4425 /* This checkpoint is required by replay to separate prior clock
4426 reading from the other reads, because timer polling functions query
4427 clock values from the log. */
4428 replay_checkpoint(CHECKPOINT_RESET
);
4429 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
4430 register_global_state();
4432 Error
*local_err
= NULL
;
4433 if (load_snapshot(loadvm
, &local_err
) < 0) {
4434 error_report_err(local_err
);
4439 if (replay_mode
!= REPLAY_MODE_NONE
) {
4440 replay_vmstate_init();
4443 qdev_prop_check_globals();
4444 if (vmstate_dump_file
) {
4446 dump_vmstate_json_to_file(vmstate_dump_file
);
4451 Error
*local_err
= NULL
;
4452 qemu_start_incoming_migration(incoming
, &local_err
);
4454 error_reportf_err(local_err
, "-incoming %s: ", incoming
);
4457 } else if (autostart
) {
4461 accel_setup_post(current_machine
);
4467 void qemu_cleanup(void)
4469 gdbserver_cleanup();
4472 * cleaning up the migration object cancels any existing migration
4473 * try to do this early so that it also stops using devices.
4475 migration_shutdown();
4478 * We must cancel all block jobs while the block layer is drained,
4479 * or cancelling will be affected by throttling and thus may block
4480 * for an extended period of time.
4481 * vm_shutdown() will bdrv_drain_all(), so we may as well include
4482 * it in the drained section.
4483 * We do not need to end this section, because we do not want any
4484 * requests happening from here on anyway.
4486 bdrv_drain_all_begin();
4488 /* No more vcpu or device emulation activity beyond this point */
4492 job_cancel_sync_all();
4497 /* vhost-user must be cleaned up before chardevs. */
4503 user_creatable_cleanup();
4504 /* TODO: unref root container, check all devices are ok */