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
31 #include "config-host.h"
34 #include "sysemu/seccomp.h"
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
42 #if defined(__APPLE__) || defined(main)
44 int qemu_main(int argc
, char **argv
, char **envp
);
45 int main(int argc
, char **argv
)
47 return qemu_main(argc
, argv
, NULL
);
50 #define main qemu_main
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
100 #include "fsdev/qemu-fsdev.h"
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir
[16];
128 static int data_dir_idx
;
129 const char *bios_name
= NULL
;
130 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
131 DisplayType display_type
= DT_DEFAULT
;
132 static int display_remote
;
133 const char* keyboard_layout
= NULL
;
135 const char *mem_path
= NULL
;
136 int mem_prealloc
= 0; /* force preallocation of physical target memory */
137 bool enable_mlock
= false;
139 NICInfo nd_table
[MAX_NICS
];
141 static int rtc_utc
= 1;
142 static int rtc_date_offset
= -1; /* -1 means no change */
143 QEMUClockType rtc_clock
;
144 int vga_interface_type
= VGA_NONE
;
145 static int full_screen
= 0;
146 static int no_frame
= 0;
149 static bool grab_on_hover
;
151 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
152 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
153 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
154 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
155 int win2k_install_hack
= 0;
162 const char *vnc_display
;
164 int acpi_enabled
= 1;
167 static int no_reboot
;
170 int graphic_rotate
= 0;
171 const char *watchdog
;
172 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
174 int semihosting_enabled
= 0;
176 const char *qemu_name
;
179 unsigned int nb_prom_envs
= 0;
180 const char *prom_envs
[MAX_PROM_ENVS
];
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 int icount_align_option
;
191 NodeInfo numa_info
[MAX_NODES
];
193 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 uint8_t qemu_uuid
[16];
199 static QEMUBootSetHandler
*boot_set_handler
;
200 static void *boot_set_opaque
;
202 static NotifierList exit_notifiers
=
203 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
205 static NotifierList machine_init_done_notifiers
=
206 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
210 enum xen_mode xen_mode
= XEN_EMULATE
;
212 static int has_defaults
= 1;
213 static int default_serial
= 1;
214 static int default_parallel
= 1;
215 static int default_virtcon
= 1;
216 static int default_sclp
= 1;
217 static int default_monitor
= 1;
218 static int default_floppy
= 1;
219 static int default_cdrom
= 1;
220 static int default_sdcard
= 1;
221 static int default_vga
= 1;
227 { .driver
= "isa-serial", .flag
= &default_serial
},
228 { .driver
= "isa-parallel", .flag
= &default_parallel
},
229 { .driver
= "isa-fdc", .flag
= &default_floppy
},
230 { .driver
= "ide-cd", .flag
= &default_cdrom
},
231 { .driver
= "ide-hd", .flag
= &default_cdrom
},
232 { .driver
= "ide-drive", .flag
= &default_cdrom
},
233 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
234 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
235 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
236 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
237 { .driver
= "VGA", .flag
= &default_vga
},
238 { .driver
= "isa-vga", .flag
= &default_vga
},
239 { .driver
= "cirrus-vga", .flag
= &default_vga
},
240 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
241 { .driver
= "vmware-svga", .flag
= &default_vga
},
242 { .driver
= "qxl-vga", .flag
= &default_vga
},
245 static QemuOptsList qemu_rtc_opts
= {
247 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_STRING
,
257 .type
= QEMU_OPT_STRING
,
259 { /* end of list */ }
263 static QemuOptsList qemu_sandbox_opts
= {
265 .implied_opt_name
= "enable",
266 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
270 .type
= QEMU_OPT_BOOL
,
272 { /* end of list */ }
276 static QemuOptsList qemu_trace_opts
= {
278 .implied_opt_name
= "trace",
279 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
283 .type
= QEMU_OPT_STRING
,
286 .type
= QEMU_OPT_STRING
,
288 { /* end of list */ }
292 static QemuOptsList qemu_option_rom_opts
= {
293 .name
= "option-rom",
294 .implied_opt_name
= "romfile",
295 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
299 .type
= QEMU_OPT_NUMBER
,
302 .type
= QEMU_OPT_STRING
,
304 { /* end of list */ }
308 static QemuOptsList qemu_machine_opts
= {
310 .implied_opt_name
= "type",
312 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
316 .type
= QEMU_OPT_STRING
,
317 .help
= "emulated machine"
320 .type
= QEMU_OPT_STRING
,
321 .help
= "accelerator list",
323 .name
= "kernel_irqchip",
324 .type
= QEMU_OPT_BOOL
,
325 .help
= "use KVM in-kernel irqchip",
327 .name
= "kvm_shadow_mem",
328 .type
= QEMU_OPT_SIZE
,
329 .help
= "KVM shadow MMU size",
332 .type
= QEMU_OPT_STRING
,
333 .help
= "Linux kernel image file",
336 .type
= QEMU_OPT_STRING
,
337 .help
= "Linux initial ramdisk file",
340 .type
= QEMU_OPT_STRING
,
341 .help
= "Linux kernel command line",
344 .type
= QEMU_OPT_STRING
,
345 .help
= "Linux kernel device tree file",
348 .type
= QEMU_OPT_STRING
,
349 .help
= "Dump current dtb to a file and quit",
351 .name
= "phandle_start",
352 .type
= QEMU_OPT_NUMBER
,
353 .help
= "The first phandle ID we may generate dynamically",
355 .name
= "dt_compatible",
356 .type
= QEMU_OPT_STRING
,
357 .help
= "Overrides the \"compatible\" property of the dt root node",
359 .name
= "dump-guest-core",
360 .type
= QEMU_OPT_BOOL
,
361 .help
= "Include guest memory in a core dump",
364 .type
= QEMU_OPT_BOOL
,
365 .help
= "enable/disable memory merge support",
368 .type
= QEMU_OPT_BOOL
,
369 .help
= "Set on/off to enable/disable usb",
372 .type
= QEMU_OPT_STRING
,
373 .help
= "firmware image",
376 .type
= QEMU_OPT_STRING
,
377 .help
= "Specifies the KVM virtualization mode (HV, PR)",
379 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
380 .type
= QEMU_OPT_SIZE
,
381 .help
= "maximum ram below the 4G boundary (32bit boundary)",
383 .name
= PC_MACHINE_VMPORT
,
384 .type
= QEMU_OPT_STRING
,
385 .help
= "Enable vmport (pc & q35)",
388 .type
= QEMU_OPT_BOOL
,
389 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
391 { /* End of list */ }
395 static QemuOptsList qemu_boot_opts
= {
397 .implied_opt_name
= "order",
399 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
403 .type
= QEMU_OPT_STRING
,
406 .type
= QEMU_OPT_STRING
,
409 .type
= QEMU_OPT_BOOL
,
412 .type
= QEMU_OPT_STRING
,
414 .name
= "splash-time",
415 .type
= QEMU_OPT_STRING
,
417 .name
= "reboot-timeout",
418 .type
= QEMU_OPT_STRING
,
421 .type
= QEMU_OPT_BOOL
,
427 static QemuOptsList qemu_add_fd_opts
= {
429 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
433 .type
= QEMU_OPT_NUMBER
,
434 .help
= "file descriptor of which a duplicate is added to fd set",
437 .type
= QEMU_OPT_NUMBER
,
438 .help
= "ID of the fd set to add fd to",
441 .type
= QEMU_OPT_STRING
,
442 .help
= "free-form string used to describe fd",
444 { /* end of list */ }
448 static QemuOptsList qemu_object_opts
= {
450 .implied_opt_name
= "qom-type",
451 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
457 static QemuOptsList qemu_tpmdev_opts
= {
459 .implied_opt_name
= "type",
460 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
462 /* options are defined in the TPM backends */
463 { /* end of list */ }
467 static QemuOptsList qemu_realtime_opts
= {
469 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
473 .type
= QEMU_OPT_BOOL
,
475 { /* end of list */ }
479 static QemuOptsList qemu_msg_opts
= {
481 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
485 .type
= QEMU_OPT_BOOL
,
487 { /* end of list */ }
491 static QemuOptsList qemu_name_opts
= {
493 .implied_opt_name
= "guest",
495 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
499 .type
= QEMU_OPT_STRING
,
500 .help
= "Sets the name of the guest.\n"
501 "This name will be displayed in the SDL window caption.\n"
502 "The name will also be used for the VNC server",
505 .type
= QEMU_OPT_STRING
,
506 .help
= "Sets the name of the QEMU process, as shown in top etc",
508 .name
= "debug-threads",
509 .type
= QEMU_OPT_BOOL
,
510 .help
= "When enabled, name the individual threads; defaults off.\n"
511 "NOTE: The thread names are for debugging and not a\n"
514 { /* End of list */ }
518 static QemuOptsList qemu_mem_opts
= {
520 .implied_opt_name
= "size",
521 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
526 .type
= QEMU_OPT_SIZE
,
530 .type
= QEMU_OPT_NUMBER
,
534 .type
= QEMU_OPT_SIZE
,
536 { /* end of list */ }
540 static QemuOptsList qemu_icount_opts
= {
542 .implied_opt_name
= "shift",
544 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
548 .type
= QEMU_OPT_STRING
,
551 .type
= QEMU_OPT_BOOL
,
553 { /* end of list */ }
558 * Get machine options
560 * Returns: machine options (never null).
562 QemuOpts
*qemu_get_machine_opts(void)
564 return qemu_find_opts_singleton("machine");
567 const char *qemu_get_vm_name(void)
572 static void res_free(void)
574 if (boot_splash_filedata
!= NULL
) {
575 g_free(boot_splash_filedata
);
576 boot_splash_filedata
= NULL
;
580 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
582 const char *driver
= qemu_opt_get(opts
, "driver");
587 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
588 if (strcmp(default_list
[i
].driver
, driver
) != 0)
590 *(default_list
[i
].flag
) = 0;
595 /***********************************************************/
598 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
600 /* We use RUN_STATE_MAX but any invalid value will do */
601 static RunState vmstop_requested
= RUN_STATE_MAX
;
602 static QemuMutex vmstop_lock
;
607 } RunStateTransition
;
609 static const RunStateTransition runstate_transitions_def
[] = {
611 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
612 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
614 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
615 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
617 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
618 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
620 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
621 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
623 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
624 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
626 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
627 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
629 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
630 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
631 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
633 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
634 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
636 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
638 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
639 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
640 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
641 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
642 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
643 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
644 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
645 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
646 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
647 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
649 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
651 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
652 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
654 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
655 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
656 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
657 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
659 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
660 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
662 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
663 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
665 { RUN_STATE_MAX
, RUN_STATE_MAX
},
668 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
670 bool runstate_check(RunState state
)
672 return current_run_state
== state
;
675 static void runstate_init(void)
677 const RunStateTransition
*p
;
679 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
680 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
681 runstate_valid_transitions
[p
->from
][p
->to
] = true;
684 qemu_mutex_init(&vmstop_lock
);
687 /* This function will abort() on invalid state transitions */
688 void runstate_set(RunState new_state
)
690 assert(new_state
< RUN_STATE_MAX
);
692 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
693 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
694 RunState_lookup
[current_run_state
],
695 RunState_lookup
[new_state
]);
698 trace_runstate_set(new_state
);
699 current_run_state
= new_state
;
702 int runstate_is_running(void)
704 return runstate_check(RUN_STATE_RUNNING
);
707 bool runstate_needs_reset(void)
709 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
710 runstate_check(RUN_STATE_SHUTDOWN
);
713 StatusInfo
*qmp_query_status(Error
**errp
)
715 StatusInfo
*info
= g_malloc0(sizeof(*info
));
717 info
->running
= runstate_is_running();
718 info
->singlestep
= singlestep
;
719 info
->status
= current_run_state
;
724 static bool qemu_vmstop_requested(RunState
*r
)
726 qemu_mutex_lock(&vmstop_lock
);
727 *r
= vmstop_requested
;
728 vmstop_requested
= RUN_STATE_MAX
;
729 qemu_mutex_unlock(&vmstop_lock
);
730 return *r
< RUN_STATE_MAX
;
733 void qemu_system_vmstop_request_prepare(void)
735 qemu_mutex_lock(&vmstop_lock
);
738 void qemu_system_vmstop_request(RunState state
)
740 vmstop_requested
= state
;
741 qemu_mutex_unlock(&vmstop_lock
);
749 qemu_vmstop_requested(&requested
);
750 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
754 /* Ensure that a STOP/RESUME pair of events is emitted if a
755 * vmstop request was pending. The BLOCK_IO_ERROR event, for
756 * example, according to documentation is always followed by
759 if (runstate_is_running()) {
760 qapi_event_send_stop(&error_abort
);
763 runstate_set(RUN_STATE_RUNNING
);
764 vm_state_notify(1, RUN_STATE_RUNNING
);
768 qapi_event_send_resume(&error_abort
);
772 /***********************************************************/
773 /* real time host monotonic timer */
775 /***********************************************************/
776 /* host time/date access */
777 void qemu_get_timedate(struct tm
*tm
, int offset
)
783 if (rtc_date_offset
== -1) {
787 localtime_r(&ti
, tm
);
789 ti
-= rtc_date_offset
;
794 int qemu_timedate_diff(struct tm
*tm
)
798 if (rtc_date_offset
== -1)
800 seconds
= mktimegm(tm
);
803 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
804 seconds
= mktime(&tmp
);
807 seconds
= mktimegm(tm
) + rtc_date_offset
;
809 return seconds
- time(NULL
);
812 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
814 time_t rtc_start_date
;
817 if (!strcmp(startdate
, "now") && legacy
) {
818 rtc_date_offset
= -1;
820 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
828 } else if (sscanf(startdate
, "%d-%d-%d",
840 rtc_start_date
= mktimegm(&tm
);
841 if (rtc_start_date
== -1) {
843 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
844 "'2006-06-17T16:01:21' or '2006-06-17'\n");
847 rtc_date_offset
= time(NULL
) - rtc_start_date
;
851 static void configure_rtc(QemuOpts
*opts
)
855 value
= qemu_opt_get(opts
, "base");
857 if (!strcmp(value
, "utc")) {
859 } else if (!strcmp(value
, "localtime")) {
862 configure_rtc_date_offset(value
, 0);
865 value
= qemu_opt_get(opts
, "clock");
867 if (!strcmp(value
, "host")) {
868 rtc_clock
= QEMU_CLOCK_HOST
;
869 } else if (!strcmp(value
, "rt")) {
870 rtc_clock
= QEMU_CLOCK_REALTIME
;
871 } else if (!strcmp(value
, "vm")) {
872 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
874 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
878 value
= qemu_opt_get(opts
, "driftfix");
880 if (!strcmp(value
, "slew")) {
881 static GlobalProperty slew_lost_ticks
[] = {
883 .driver
= "mc146818rtc",
884 .property
= "lost_tick_policy",
887 { /* end of list */ }
890 qdev_prop_register_global_list(slew_lost_ticks
);
891 } else if (!strcmp(value
, "none")) {
892 /* discard is default */
894 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
900 /***********************************************************/
901 /* Bluetooth support */
904 static struct HCIInfo
*hci_table
[MAX_NICS
];
906 struct HCIInfo
*qemu_next_hci(void)
908 if (cur_hci
== nb_hcis
)
911 return hci_table
[cur_hci
++];
914 static int bt_hci_parse(const char *str
)
919 if (nb_hcis
>= MAX_NICS
) {
920 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
933 bdaddr
.b
[5] = 0x56 + nb_hcis
;
934 hci
->bdaddr_set(hci
, bdaddr
.b
);
936 hci_table
[nb_hcis
++] = hci
;
941 static void bt_vhci_add(int vlan_id
)
943 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
946 fprintf(stderr
, "qemu: warning: adding a VHCI to "
947 "an empty scatternet %i\n", vlan_id
);
949 bt_vhci_init(bt_new_hci(vlan
));
952 static struct bt_device_s
*bt_device_add(const char *opt
)
954 struct bt_scatternet_s
*vlan
;
956 char *endp
= strstr(opt
, ",vlan=");
957 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
960 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
963 vlan_id
= strtol(endp
+ 6, &endp
, 0);
965 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
970 vlan
= qemu_find_bt_vlan(vlan_id
);
973 fprintf(stderr
, "qemu: warning: adding a slave device to "
974 "an empty scatternet %i\n", vlan_id
);
976 if (!strcmp(devname
, "keyboard"))
977 return bt_keyboard_init(vlan
);
979 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
983 static int bt_parse(const char *opt
)
985 const char *endp
, *p
;
988 if (strstart(opt
, "hci", &endp
)) {
989 if (!*endp
|| *endp
== ',') {
991 if (!strstart(endp
, ",vlan=", 0))
994 return bt_hci_parse(opt
);
996 } else if (strstart(opt
, "vhci", &endp
)) {
997 if (!*endp
|| *endp
== ',') {
999 if (strstart(endp
, ",vlan=", &p
)) {
1000 vlan
= strtol(p
, (char **) &endp
, 0);
1002 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1006 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1015 } else if (strstart(opt
, "device:", &endp
))
1016 return !bt_device_add(endp
);
1018 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1022 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1024 /* FIXME: change this to true for 1.3 */
1025 if (qemu_opt_get_bool(opts
, "enable", false)) {
1026 #ifdef CONFIG_SECCOMP
1027 if (seccomp_start() < 0) {
1028 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1029 "failed to install seccomp syscall filter in the kernel");
1033 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1034 "sandboxing request but seccomp is not compiled into this build");
1042 static int parse_name(QemuOpts
*opts
, void *opaque
)
1044 const char *proc_name
;
1046 if (qemu_opt_get(opts
, "debug-threads")) {
1047 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1049 qemu_name
= qemu_opt_get(opts
, "guest");
1051 proc_name
= qemu_opt_get(opts
, "process");
1053 os_set_proc_name(proc_name
);
1059 bool usb_enabled(bool default_usb
)
1061 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1062 has_defaults
&& default_usb
);
1066 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1068 int fd
, dupfd
, flags
;
1070 const char *fd_opaque
= NULL
;
1072 fd
= qemu_opt_get_number(opts
, "fd", -1);
1073 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1074 fd_opaque
= qemu_opt_get(opts
, "opaque");
1077 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1078 "fd option is required and must be non-negative");
1082 if (fd
<= STDERR_FILENO
) {
1083 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1084 "fd cannot be a standard I/O stream");
1089 * All fds inherited across exec() necessarily have FD_CLOEXEC
1090 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1092 flags
= fcntl(fd
, F_GETFD
);
1093 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1094 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1095 "fd is not valid or already in use");
1100 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1101 "set option is required and must be non-negative");
1105 #ifdef F_DUPFD_CLOEXEC
1106 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1110 qemu_set_cloexec(dupfd
);
1114 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1115 "Error duplicating fd: %s", strerror(errno
));
1119 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1120 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1126 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1130 fd
= qemu_opt_get_number(opts
, "fd", -1);
1137 /***********************************************************/
1138 /* QEMU Block devices */
1140 #define HD_OPTS "media=disk"
1141 #define CDROM_OPTS "media=cdrom"
1143 #define PFLASH_OPTS ""
1147 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1149 BlockInterfaceType
*block_default_type
= opaque
;
1151 return drive_new(opts
, *block_default_type
) == NULL
;
1154 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1156 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1157 qemu_opt_set(opts
, "snapshot", "on");
1162 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1163 int index
, const char *optstr
)
1168 if (!enable
|| drive_get_by_index(type
, index
)) {
1172 opts
= drive_add(type
, index
, NULL
, optstr
);
1174 drive_enable_snapshot(opts
, NULL
);
1177 dinfo
= drive_new(opts
, type
);
1181 dinfo
->is_default
= true;
1185 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1187 boot_set_handler
= func
;
1188 boot_set_opaque
= opaque
;
1191 int qemu_boot_set(const char *boot_order
)
1193 if (!boot_set_handler
) {
1196 return boot_set_handler(boot_set_opaque
, boot_order
);
1199 static void validate_bootdevices(const char *devices
)
1201 /* We just do some generic consistency checks */
1205 for (p
= devices
; *p
!= '\0'; p
++) {
1206 /* Allowed boot devices are:
1207 * a-b: floppy disk drives
1208 * c-f: IDE disk drives
1209 * g-m: machine implementation dependent drives
1210 * n-p: network devices
1211 * It's up to each machine implementation to check if the given boot
1212 * devices match the actual hardware implementation and firmware
1215 if (*p
< 'a' || *p
> 'p') {
1216 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1219 if (bitmap
& (1 << (*p
- 'a'))) {
1220 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1223 bitmap
|= 1 << (*p
- 'a');
1227 static void restore_boot_order(void *opaque
)
1229 char *normal_boot_order
= opaque
;
1230 static int first
= 1;
1232 /* Restore boot order and remove ourselves after the first boot */
1238 qemu_boot_set(normal_boot_order
);
1240 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1241 g_free(normal_boot_order
);
1244 static QemuOptsList qemu_smp_opts
= {
1246 .implied_opt_name
= "cpus",
1247 .merge_lists
= true,
1248 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1252 .type
= QEMU_OPT_NUMBER
,
1255 .type
= QEMU_OPT_NUMBER
,
1258 .type
= QEMU_OPT_NUMBER
,
1261 .type
= QEMU_OPT_NUMBER
,
1264 .type
= QEMU_OPT_NUMBER
,
1266 { /*End of list */ }
1270 static void smp_parse(QemuOpts
*opts
)
1274 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1275 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1276 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1277 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1279 /* compute missing values, prefer sockets over cores over threads */
1280 if (cpus
== 0 || sockets
== 0) {
1281 sockets
= sockets
> 0 ? sockets
: 1;
1282 cores
= cores
> 0 ? cores
: 1;
1283 threads
= threads
> 0 ? threads
: 1;
1285 cpus
= cores
* threads
* sockets
;
1289 threads
= threads
> 0 ? threads
: 1;
1290 cores
= cpus
/ (sockets
* threads
);
1292 threads
= cpus
/ (cores
* sockets
);
1296 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1299 smp_cores
= cores
> 0 ? cores
: 1;
1300 smp_threads
= threads
> 0 ? threads
: 1;
1304 if (max_cpus
== 0) {
1305 max_cpus
= smp_cpus
;
1308 if (max_cpus
> MAX_CPUMASK_BITS
) {
1309 fprintf(stderr
, "Unsupported number of maxcpus\n");
1312 if (max_cpus
< smp_cpus
) {
1313 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1319 static void realtime_init(void)
1322 if (os_mlock() < 0) {
1323 fprintf(stderr
, "qemu: locking memory failed\n");
1330 static void configure_msg(QemuOpts
*opts
)
1332 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1335 /***********************************************************/
1338 static int usb_device_add(const char *devname
)
1340 USBDevice
*dev
= NULL
;
1341 #ifndef CONFIG_LINUX
1345 if (!usb_enabled(false)) {
1349 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1350 dev
= usbdevice_create(devname
);
1354 /* the other ones */
1355 #ifndef CONFIG_LINUX
1356 /* only the linux version is qdev-ified, usb-bsd still needs this */
1357 if (strstart(devname
, "host:", &p
)) {
1358 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1368 static int usb_device_del(const char *devname
)
1373 if (strstart(devname
, "host:", &p
)) {
1377 if (!usb_enabled(false)) {
1381 p
= strchr(devname
, '.');
1384 bus_num
= strtoul(devname
, NULL
, 0);
1385 addr
= strtoul(p
+ 1, NULL
, 0);
1387 return usb_device_delete_addr(bus_num
, addr
);
1390 static int usb_parse(const char *cmdline
)
1393 r
= usb_device_add(cmdline
);
1395 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1400 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1402 const char *devname
= qdict_get_str(qdict
, "devname");
1403 if (usb_device_add(devname
) < 0) {
1404 error_report("could not add USB device '%s'", devname
);
1408 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1410 const char *devname
= qdict_get_str(qdict
, "devname");
1411 if (usb_device_del(devname
) < 0) {
1412 error_report("could not delete USB device '%s'", devname
);
1416 /***********************************************************/
1417 /* machine registration */
1419 MachineState
*current_machine
;
1421 static void machine_class_init(ObjectClass
*oc
, void *data
)
1423 MachineClass
*mc
= MACHINE_CLASS(oc
);
1424 QEMUMachine
*qm
= data
;
1426 mc
->family
= qm
->family
;
1427 mc
->name
= qm
->name
;
1428 mc
->alias
= qm
->alias
;
1429 mc
->desc
= qm
->desc
;
1430 mc
->init
= qm
->init
;
1431 mc
->reset
= qm
->reset
;
1432 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1433 mc
->kvm_type
= qm
->kvm_type
;
1434 mc
->block_default_type
= qm
->block_default_type
;
1435 mc
->units_per_default_bus
= qm
->units_per_default_bus
;
1436 mc
->max_cpus
= qm
->max_cpus
;
1437 mc
->no_serial
= qm
->no_serial
;
1438 mc
->no_parallel
= qm
->no_parallel
;
1439 mc
->use_virtcon
= qm
->use_virtcon
;
1440 mc
->use_sclp
= qm
->use_sclp
;
1441 mc
->no_floppy
= qm
->no_floppy
;
1442 mc
->no_cdrom
= qm
->no_cdrom
;
1443 mc
->no_sdcard
= qm
->no_sdcard
;
1444 mc
->has_dynamic_sysbus
= qm
->has_dynamic_sysbus
;
1445 mc
->is_default
= qm
->is_default
;
1446 mc
->default_machine_opts
= qm
->default_machine_opts
;
1447 mc
->default_boot_order
= qm
->default_boot_order
;
1448 mc
->default_display
= qm
->default_display
;
1449 mc
->compat_props
= qm
->compat_props
;
1450 mc
->hw_version
= qm
->hw_version
;
1453 int qemu_register_machine(QEMUMachine
*m
)
1455 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1458 .parent
= TYPE_MACHINE
,
1459 .class_init
= machine_class_init
,
1460 .class_data
= (void *)m
,
1469 static MachineClass
*find_machine(const char *name
)
1471 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1472 MachineClass
*mc
= NULL
;
1474 for (el
= machines
; el
; el
= el
->next
) {
1475 MachineClass
*temp
= el
->data
;
1477 if (!strcmp(temp
->name
, name
)) {
1482 !strcmp(temp
->alias
, name
)) {
1488 g_slist_free(machines
);
1492 MachineClass
*find_default_machine(void)
1494 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1495 MachineClass
*mc
= NULL
;
1497 for (el
= machines
; el
; el
= el
->next
) {
1498 MachineClass
*temp
= el
->data
;
1500 if (temp
->is_default
) {
1506 g_slist_free(machines
);
1510 MachineInfoList
*qmp_query_machines(Error
**errp
)
1512 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1513 MachineInfoList
*mach_list
= NULL
;
1515 for (el
= machines
; el
; el
= el
->next
) {
1516 MachineClass
*mc
= el
->data
;
1517 MachineInfoList
*entry
;
1520 info
= g_malloc0(sizeof(*info
));
1521 if (mc
->is_default
) {
1522 info
->has_is_default
= true;
1523 info
->is_default
= true;
1527 info
->has_alias
= true;
1528 info
->alias
= g_strdup(mc
->alias
);
1531 info
->name
= g_strdup(mc
->name
);
1532 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1534 entry
= g_malloc0(sizeof(*entry
));
1535 entry
->value
= info
;
1536 entry
->next
= mach_list
;
1540 g_slist_free(machines
);
1544 /***********************************************************/
1545 /* main execution loop */
1547 struct vm_change_state_entry
{
1548 VMChangeStateHandler
*cb
;
1550 QLIST_ENTRY (vm_change_state_entry
) entries
;
1553 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1555 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1558 VMChangeStateEntry
*e
;
1560 e
= g_malloc0(sizeof (*e
));
1564 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1568 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1570 QLIST_REMOVE (e
, entries
);
1574 void vm_state_notify(int running
, RunState state
)
1576 VMChangeStateEntry
*e
, *next
;
1578 trace_vm_state_notify(running
, state
);
1580 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1581 e
->cb(e
->opaque
, running
, state
);
1585 /* reset/shutdown handler */
1587 typedef struct QEMUResetEntry
{
1588 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1589 QEMUResetHandler
*func
;
1593 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1594 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1595 static int reset_requested
;
1596 static int shutdown_requested
, shutdown_signal
= -1;
1597 static pid_t shutdown_pid
;
1598 static int powerdown_requested
;
1599 static int debug_requested
;
1600 static int suspend_requested
;
1601 static WakeupReason wakeup_reason
;
1602 static NotifierList powerdown_notifiers
=
1603 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1604 static NotifierList suspend_notifiers
=
1605 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1606 static NotifierList wakeup_notifiers
=
1607 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1608 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1610 int qemu_shutdown_requested_get(void)
1612 return shutdown_requested
;
1615 int qemu_reset_requested_get(void)
1617 return reset_requested
;
1620 static int qemu_shutdown_requested(void)
1622 return atomic_xchg(&shutdown_requested
, 0);
1625 static void qemu_kill_report(void)
1627 if (!qtest_driver() && shutdown_signal
!= -1) {
1628 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1629 if (shutdown_pid
== 0) {
1630 /* This happens for eg ^C at the terminal, so it's worth
1631 * avoiding printing an odd message in that case.
1633 fputc('\n', stderr
);
1635 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1637 shutdown_signal
= -1;
1641 static int qemu_reset_requested(void)
1643 int r
= reset_requested
;
1644 reset_requested
= 0;
1648 static int qemu_suspend_requested(void)
1650 int r
= suspend_requested
;
1651 suspend_requested
= 0;
1655 static WakeupReason
qemu_wakeup_requested(void)
1657 return wakeup_reason
;
1660 static int qemu_powerdown_requested(void)
1662 int r
= powerdown_requested
;
1663 powerdown_requested
= 0;
1667 static int qemu_debug_requested(void)
1669 int r
= debug_requested
;
1670 debug_requested
= 0;
1674 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1676 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1679 re
->opaque
= opaque
;
1680 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1683 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1687 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1688 if (re
->func
== func
&& re
->opaque
== opaque
) {
1689 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1696 void qemu_devices_reset(void)
1698 QEMUResetEntry
*re
, *nre
;
1700 /* reset all devices */
1701 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1702 re
->func(re
->opaque
);
1706 void qemu_system_reset(bool report
)
1710 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1712 if (mc
&& mc
->reset
) {
1715 qemu_devices_reset();
1718 qapi_event_send_reset(&error_abort
);
1720 cpu_synchronize_all_post_reset();
1723 void qemu_system_reset_request(void)
1726 shutdown_requested
= 1;
1728 reset_requested
= 1;
1731 qemu_notify_event();
1734 static void qemu_system_suspend(void)
1737 notifier_list_notify(&suspend_notifiers
, NULL
);
1738 runstate_set(RUN_STATE_SUSPENDED
);
1739 qapi_event_send_suspend(&error_abort
);
1742 void qemu_system_suspend_request(void)
1744 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1747 suspend_requested
= 1;
1749 qemu_notify_event();
1752 void qemu_register_suspend_notifier(Notifier
*notifier
)
1754 notifier_list_add(&suspend_notifiers
, notifier
);
1757 void qemu_system_wakeup_request(WakeupReason reason
)
1759 trace_system_wakeup_request(reason
);
1761 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1764 if (!(wakeup_reason_mask
& (1 << reason
))) {
1767 runstate_set(RUN_STATE_RUNNING
);
1768 wakeup_reason
= reason
;
1769 qemu_notify_event();
1772 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1775 wakeup_reason_mask
|= (1 << reason
);
1777 wakeup_reason_mask
&= ~(1 << reason
);
1781 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1783 notifier_list_add(&wakeup_notifiers
, notifier
);
1786 void qemu_system_killed(int signal
, pid_t pid
)
1788 shutdown_signal
= signal
;
1791 qemu_system_shutdown_request();
1794 void qemu_system_shutdown_request(void)
1796 trace_qemu_system_shutdown_request();
1797 shutdown_requested
= 1;
1798 qemu_notify_event();
1801 static void qemu_system_powerdown(void)
1803 qapi_event_send_powerdown(&error_abort
);
1804 notifier_list_notify(&powerdown_notifiers
, NULL
);
1807 void qemu_system_powerdown_request(void)
1809 trace_qemu_system_powerdown_request();
1810 powerdown_requested
= 1;
1811 qemu_notify_event();
1814 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1816 notifier_list_add(&powerdown_notifiers
, notifier
);
1819 void qemu_system_debug_request(void)
1821 debug_requested
= 1;
1822 qemu_notify_event();
1825 static bool main_loop_should_exit(void)
1828 if (qemu_debug_requested()) {
1829 vm_stop(RUN_STATE_DEBUG
);
1831 if (qemu_suspend_requested()) {
1832 qemu_system_suspend();
1834 if (qemu_shutdown_requested()) {
1836 qapi_event_send_shutdown(&error_abort
);
1838 vm_stop(RUN_STATE_SHUTDOWN
);
1843 if (qemu_reset_requested()) {
1845 cpu_synchronize_all_states();
1846 qemu_system_reset(VMRESET_REPORT
);
1848 if (runstate_needs_reset()) {
1849 runstate_set(RUN_STATE_PAUSED
);
1852 if (qemu_wakeup_requested()) {
1854 cpu_synchronize_all_states();
1855 qemu_system_reset(VMRESET_SILENT
);
1856 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1857 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1859 qapi_event_send_wakeup(&error_abort
);
1861 if (qemu_powerdown_requested()) {
1862 qemu_system_powerdown();
1864 if (qemu_vmstop_requested(&r
)) {
1870 static void main_loop(void)
1874 #ifdef CONFIG_PROFILER
1878 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1879 #ifdef CONFIG_PROFILER
1880 ti
= profile_getclock();
1882 last_io
= main_loop_wait(nonblocking
);
1883 #ifdef CONFIG_PROFILER
1884 dev_time
+= profile_getclock() - ti
;
1886 } while (!main_loop_should_exit());
1889 static void version(void)
1891 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1894 static void help(int exitcode
)
1897 printf("usage: %s [options] [disk_image]\n\n"
1898 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1899 error_get_progname());
1901 #define QEMU_OPTIONS_GENERATE_HELP
1902 #include "qemu-options-wrapper.h"
1904 printf("\nDuring emulation, the following keys are useful:\n"
1905 "ctrl-alt-f toggle full screen\n"
1906 "ctrl-alt-n switch to virtual console 'n'\n"
1907 "ctrl-alt toggle mouse and keyboard grab\n"
1909 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1914 #define HAS_ARG 0x0001
1916 typedef struct QEMUOption
{
1923 static const QEMUOption qemu_options
[] = {
1924 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1925 #define QEMU_OPTIONS_GENERATE_OPTIONS
1926 #include "qemu-options-wrapper.h"
1930 static bool vga_available(void)
1932 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1935 static bool cirrus_vga_available(void)
1937 return object_class_by_name("cirrus-vga")
1938 || object_class_by_name("isa-cirrus-vga");
1941 static bool vmware_vga_available(void)
1943 return object_class_by_name("vmware-svga");
1946 static bool qxl_vga_available(void)
1948 return object_class_by_name("qxl-vga");
1951 static bool tcx_vga_available(void)
1953 return object_class_by_name("SUNW,tcx");
1956 static bool cg3_vga_available(void)
1958 return object_class_by_name("cgthree");
1961 static void select_vgahw (const char *p
)
1965 assert(vga_interface_type
== VGA_NONE
);
1966 if (strstart(p
, "std", &opts
)) {
1967 if (vga_available()) {
1968 vga_interface_type
= VGA_STD
;
1970 fprintf(stderr
, "Error: standard VGA not available\n");
1973 } else if (strstart(p
, "cirrus", &opts
)) {
1974 if (cirrus_vga_available()) {
1975 vga_interface_type
= VGA_CIRRUS
;
1977 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1980 } else if (strstart(p
, "vmware", &opts
)) {
1981 if (vmware_vga_available()) {
1982 vga_interface_type
= VGA_VMWARE
;
1984 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1987 } else if (strstart(p
, "xenfb", &opts
)) {
1988 vga_interface_type
= VGA_XENFB
;
1989 } else if (strstart(p
, "qxl", &opts
)) {
1990 if (qxl_vga_available()) {
1991 vga_interface_type
= VGA_QXL
;
1993 fprintf(stderr
, "Error: QXL VGA not available\n");
1996 } else if (strstart(p
, "tcx", &opts
)) {
1997 if (tcx_vga_available()) {
1998 vga_interface_type
= VGA_TCX
;
2000 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2003 } else if (strstart(p
, "cg3", &opts
)) {
2004 if (cg3_vga_available()) {
2005 vga_interface_type
= VGA_CG3
;
2007 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2010 } else if (!strstart(p
, "none", &opts
)) {
2012 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2016 const char *nextopt
;
2018 if (strstart(opts
, ",retrace=", &nextopt
)) {
2020 if (strstart(opts
, "dumb", &nextopt
))
2021 vga_retrace_method
= VGA_RETRACE_DUMB
;
2022 else if (strstart(opts
, "precise", &nextopt
))
2023 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2024 else goto invalid_vga
;
2025 } else goto invalid_vga
;
2030 static DisplayType
select_display(const char *p
)
2033 DisplayType display
= DT_DEFAULT
;
2035 if (strstart(p
, "sdl", &opts
)) {
2039 const char *nextopt
;
2041 if (strstart(opts
, ",frame=", &nextopt
)) {
2043 if (strstart(opts
, "on", &nextopt
)) {
2045 } else if (strstart(opts
, "off", &nextopt
)) {
2048 goto invalid_sdl_args
;
2050 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2052 if (strstart(opts
, "on", &nextopt
)) {
2054 } else if (strstart(opts
, "off", &nextopt
)) {
2057 goto invalid_sdl_args
;
2059 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2061 if (strstart(opts
, "on", &nextopt
)) {
2063 } else if (strstart(opts
, "off", &nextopt
)) {
2066 goto invalid_sdl_args
;
2068 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2070 if (strstart(opts
, "on", &nextopt
)) {
2072 } else if (strstart(opts
, "off", &nextopt
)) {
2075 goto invalid_sdl_args
;
2079 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2085 fprintf(stderr
, "SDL support is disabled\n");
2088 } else if (strstart(p
, "vnc", &opts
)) {
2093 const char *nextopt
;
2095 if (strstart(opts
, "=", &nextopt
)) {
2096 vnc_display
= nextopt
;
2100 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2104 fprintf(stderr
, "VNC support is disabled\n");
2107 } else if (strstart(p
, "curses", &opts
)) {
2108 #ifdef CONFIG_CURSES
2109 display
= DT_CURSES
;
2111 fprintf(stderr
, "Curses support is disabled\n");
2114 } else if (strstart(p
, "gtk", &opts
)) {
2118 const char *nextopt
;
2120 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2122 if (strstart(opts
, "on", &nextopt
)) {
2123 grab_on_hover
= true;
2124 } else if (strstart(opts
, "off", &nextopt
)) {
2125 grab_on_hover
= false;
2127 goto invalid_gtk_args
;
2131 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2137 fprintf(stderr
, "GTK support is disabled\n");
2140 } else if (strstart(p
, "none", &opts
)) {
2143 fprintf(stderr
, "Unknown display type: %s\n", p
);
2150 static int balloon_parse(const char *arg
)
2154 if (strcmp(arg
, "none") == 0) {
2158 if (!strncmp(arg
, "virtio", 6)) {
2159 if (arg
[6] == ',') {
2160 /* have params -> parse them */
2161 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2165 /* create empty opts */
2166 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2169 qemu_opt_set(opts
, "driver", "virtio-balloon");
2176 char *qemu_find_file(int type
, const char *name
)
2182 /* Try the name as a straight path first */
2183 if (access(name
, R_OK
) == 0) {
2184 trace_load_file(name
, name
);
2185 return g_strdup(name
);
2189 case QEMU_FILE_TYPE_BIOS
:
2192 case QEMU_FILE_TYPE_KEYMAP
:
2193 subdir
= "keymaps/";
2199 for (i
= 0; i
< data_dir_idx
; i
++) {
2200 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2201 if (access(buf
, R_OK
) == 0) {
2202 trace_load_file(name
, buf
);
2210 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2212 return qdev_device_help(opts
);
2215 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2219 dev
= qdev_device_add(opts
);
2222 object_unref(OBJECT(dev
));
2226 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2228 Error
*local_err
= NULL
;
2230 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2232 error_report("%s", error_get_pretty(local_err
));
2233 error_free(local_err
);
2239 #ifdef CONFIG_VIRTFS
2240 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2243 ret
= qemu_fsdev_add(opts
);
2249 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2251 CharDriverState
*chr
;
2252 const char *chardev
;
2256 mode
= qemu_opt_get(opts
, "mode");
2260 if (strcmp(mode
, "readline") == 0) {
2261 flags
= MONITOR_USE_READLINE
;
2262 } else if (strcmp(mode
, "control") == 0) {
2263 flags
= MONITOR_USE_CONTROL
;
2265 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2269 if (qemu_opt_get_bool(opts
, "pretty", 0))
2270 flags
|= MONITOR_USE_PRETTY
;
2272 if (qemu_opt_get_bool(opts
, "default", 0))
2273 flags
|= MONITOR_IS_DEFAULT
;
2275 chardev
= qemu_opt_get(opts
, "chardev");
2276 chr
= qemu_chr_find(chardev
);
2278 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2282 qemu_chr_fe_claim_no_fail(chr
);
2283 monitor_init(chr
, flags
);
2287 static void monitor_parse(const char *optarg
, const char *mode
)
2289 static int monitor_device_index
= 0;
2295 if (strstart(optarg
, "chardev:", &p
)) {
2296 snprintf(label
, sizeof(label
), "%s", p
);
2298 snprintf(label
, sizeof(label
), "compat_monitor%d",
2299 monitor_device_index
);
2300 if (monitor_device_index
== 0) {
2303 opts
= qemu_chr_parse_compat(label
, optarg
);
2305 fprintf(stderr
, "parse error: %s\n", optarg
);
2310 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2312 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2315 qemu_opt_set(opts
, "mode", mode
);
2316 qemu_opt_set(opts
, "chardev", label
);
2318 qemu_opt_set(opts
, "default", "on");
2319 monitor_device_index
++;
2322 struct device_config
{
2324 DEV_USB
, /* -usbdevice */
2326 DEV_SERIAL
, /* -serial */
2327 DEV_PARALLEL
, /* -parallel */
2328 DEV_VIRTCON
, /* -virtioconsole */
2329 DEV_DEBUGCON
, /* -debugcon */
2330 DEV_GDB
, /* -gdb, -s */
2331 DEV_SCLP
, /* s390 sclp */
2333 const char *cmdline
;
2335 QTAILQ_ENTRY(device_config
) next
;
2338 static QTAILQ_HEAD(, device_config
) device_configs
=
2339 QTAILQ_HEAD_INITIALIZER(device_configs
);
2341 static void add_device_config(int type
, const char *cmdline
)
2343 struct device_config
*conf
;
2345 conf
= g_malloc0(sizeof(*conf
));
2347 conf
->cmdline
= cmdline
;
2348 loc_save(&conf
->loc
);
2349 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2352 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2354 struct device_config
*conf
;
2357 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2358 if (conf
->type
!= type
)
2360 loc_push_restore(&conf
->loc
);
2361 rc
= func(conf
->cmdline
);
2362 loc_pop(&conf
->loc
);
2370 static int serial_parse(const char *devname
)
2372 static int index
= 0;
2375 if (strcmp(devname
, "none") == 0)
2377 if (index
== MAX_SERIAL_PORTS
) {
2378 fprintf(stderr
, "qemu: too many serial ports\n");
2381 snprintf(label
, sizeof(label
), "serial%d", index
);
2382 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2383 if (!serial_hds
[index
]) {
2384 fprintf(stderr
, "qemu: could not connect serial device"
2385 " to character backend '%s'\n", devname
);
2392 static int parallel_parse(const char *devname
)
2394 static int index
= 0;
2397 if (strcmp(devname
, "none") == 0)
2399 if (index
== MAX_PARALLEL_PORTS
) {
2400 fprintf(stderr
, "qemu: too many parallel ports\n");
2403 snprintf(label
, sizeof(label
), "parallel%d", index
);
2404 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2405 if (!parallel_hds
[index
]) {
2406 fprintf(stderr
, "qemu: could not connect parallel device"
2407 " to character backend '%s'\n", devname
);
2414 static int virtcon_parse(const char *devname
)
2416 QemuOptsList
*device
= qemu_find_opts("device");
2417 static int index
= 0;
2419 QemuOpts
*bus_opts
, *dev_opts
;
2421 if (strcmp(devname
, "none") == 0)
2423 if (index
== MAX_VIRTIO_CONSOLES
) {
2424 fprintf(stderr
, "qemu: too many virtio consoles\n");
2428 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2429 if (arch_type
== QEMU_ARCH_S390X
) {
2430 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2432 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2435 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2436 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2438 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2439 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2440 if (!virtcon_hds
[index
]) {
2441 fprintf(stderr
, "qemu: could not connect virtio console"
2442 " to character backend '%s'\n", devname
);
2445 qemu_opt_set(dev_opts
, "chardev", label
);
2451 static int sclp_parse(const char *devname
)
2453 QemuOptsList
*device
= qemu_find_opts("device");
2454 static int index
= 0;
2458 if (strcmp(devname
, "none") == 0) {
2461 if (index
== MAX_SCLP_CONSOLES
) {
2462 fprintf(stderr
, "qemu: too many sclp consoles\n");
2466 assert(arch_type
== QEMU_ARCH_S390X
);
2468 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2469 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2471 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2472 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2473 if (!sclp_hds
[index
]) {
2474 fprintf(stderr
, "qemu: could not connect sclp console"
2475 " to character backend '%s'\n", devname
);
2478 qemu_opt_set(dev_opts
, "chardev", label
);
2484 static int debugcon_parse(const char *devname
)
2488 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2491 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2493 fprintf(stderr
, "qemu: already have a debugcon device\n");
2496 qemu_opt_set(opts
, "driver", "isa-debugcon");
2497 qemu_opt_set(opts
, "chardev", "debugcon");
2501 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2503 const MachineClass
*mc1
= a
, *mc2
= b
;
2506 if (mc1
->family
== NULL
) {
2507 if (mc2
->family
== NULL
) {
2508 /* Compare standalone machine types against each other; they sort
2509 * in increasing order.
2511 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2512 object_class_get_name(OBJECT_CLASS(mc2
)));
2515 /* Standalone machine types sort after families. */
2519 if (mc2
->family
== NULL
) {
2520 /* Families sort before standalone machine types. */
2524 /* Families sort between each other alphabetically increasingly. */
2525 res
= strcmp(mc1
->family
, mc2
->family
);
2530 /* Within the same family, machine types sort in decreasing order. */
2531 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2532 object_class_get_name(OBJECT_CLASS(mc1
)));
2535 static MachineClass
*machine_parse(const char *name
)
2537 MachineClass
*mc
= NULL
;
2538 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2541 mc
= find_machine(name
);
2546 if (name
&& !is_help_option(name
)) {
2547 error_report("Unsupported machine type");
2548 error_printf("Use -machine help to list supported machines!\n");
2550 printf("Supported machines are:\n");
2551 machines
= g_slist_sort(machines
, machine_class_cmp
);
2552 for (el
= machines
; el
; el
= el
->next
) {
2553 MachineClass
*mc
= el
->data
;
2555 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2557 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2558 mc
->is_default
? " (default)" : "");
2562 g_slist_free(machines
);
2563 exit(!name
|| !is_help_option(name
));
2566 void qemu_add_exit_notifier(Notifier
*notify
)
2568 notifier_list_add(&exit_notifiers
, notify
);
2571 void qemu_remove_exit_notifier(Notifier
*notify
)
2573 notifier_remove(notify
);
2576 static void qemu_run_exit_notifiers(void)
2578 notifier_list_notify(&exit_notifiers
, NULL
);
2581 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2583 notifier_list_add(&machine_init_done_notifiers
, notify
);
2586 static void qemu_run_machine_init_done_notifiers(void)
2588 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2591 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2592 const char **poptarg
, int *poptind
)
2594 const QEMUOption
*popt
;
2595 int optind
= *poptind
;
2596 char *r
= argv
[optind
];
2599 loc_set_cmdline(argv
, optind
, 1);
2601 /* Treat --foo the same as -foo. */
2604 popt
= qemu_options
;
2607 error_report("invalid option");
2610 if (!strcmp(popt
->name
, r
+ 1))
2614 if (popt
->flags
& HAS_ARG
) {
2615 if (optind
>= argc
) {
2616 error_report("requires an argument");
2619 optarg
= argv
[optind
++];
2620 loc_set_cmdline(argv
, optind
- 2, 2);
2631 static gpointer
malloc_and_trace(gsize n_bytes
)
2633 void *ptr
= malloc(n_bytes
);
2634 trace_g_malloc(n_bytes
, ptr
);
2638 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2640 void *ptr
= realloc(mem
, n_bytes
);
2641 trace_g_realloc(mem
, n_bytes
, ptr
);
2645 static void free_and_trace(gpointer mem
)
2651 static int machine_set_property(const char *name
, const char *value
,
2654 Object
*obj
= OBJECT(opaque
);
2655 StringInputVisitor
*siv
;
2656 Error
*local_err
= NULL
;
2659 if (strcmp(name
, "type") == 0) {
2663 qom_name
= g_strdup(name
);
2671 siv
= string_input_visitor_new(value
);
2672 object_property_set(obj
, string_input_get_visitor(siv
), qom_name
, &local_err
);
2673 string_input_visitor_cleanup(siv
);
2677 qerror_report_err(local_err
);
2678 error_free(local_err
);
2685 static int object_create(QemuOpts
*opts
, void *opaque
)
2694 ov
= opts_visitor_new(opts
);
2695 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2697 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2702 qdict_del(pdict
, "qom-type");
2703 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2708 qdict_del(pdict
, "id");
2709 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2714 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2718 visit_end_struct(opts_get_visitor(ov
), &err
);
2720 qmp_object_del(id
, NULL
);
2724 opts_visitor_cleanup(ov
);
2731 qerror_report_err(err
);
2738 int main(int argc
, char **argv
, char **envp
)
2741 int snapshot
, linux_boot
;
2742 const char *initrd_filename
;
2743 const char *kernel_filename
, *kernel_cmdline
;
2744 const char *boot_order
;
2746 int cyls
, heads
, secs
, translation
;
2747 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
, *icount_opts
= NULL
;
2748 QemuOptsList
*olist
;
2751 const char *loadvm
= NULL
;
2752 MachineClass
*machine_class
;
2753 const char *cpu_model
;
2754 const char *vga_model
= NULL
;
2755 const char *qtest_chrdev
= NULL
;
2756 const char *qtest_log
= NULL
;
2757 const char *pid_file
= NULL
;
2758 const char *incoming
= NULL
;
2760 int show_vnc_port
= 0;
2762 bool defconfig
= true;
2763 bool userconfig
= true;
2764 const char *log_mask
= NULL
;
2765 const char *log_file
= NULL
;
2766 GMemVTable mem_trace
= {
2767 .malloc
= malloc_and_trace
,
2768 .realloc
= realloc_and_trace
,
2769 .free
= free_and_trace
,
2771 const char *trace_events
= NULL
;
2772 const char *trace_file
= NULL
;
2773 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2775 ram_addr_t maxram_size
= default_ram_size
;
2776 uint64_t ram_slots
= 0;
2777 FILE *vmstate_dump_file
= NULL
;
2778 Error
*main_loop_err
= NULL
;
2780 atexit(qemu_run_exit_notifiers
);
2781 error_set_progname(argv
[0]);
2782 qemu_init_exec_dir(argv
[0]);
2784 g_mem_set_vtable(&mem_trace
);
2786 module_call_init(MODULE_INIT_QOM
);
2788 qemu_add_opts(&qemu_drive_opts
);
2789 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2790 qemu_add_drive_opts(&qemu_common_drive_opts
);
2791 qemu_add_drive_opts(&qemu_drive_opts
);
2792 qemu_add_opts(&qemu_chardev_opts
);
2793 qemu_add_opts(&qemu_device_opts
);
2794 qemu_add_opts(&qemu_netdev_opts
);
2795 qemu_add_opts(&qemu_net_opts
);
2796 qemu_add_opts(&qemu_rtc_opts
);
2797 qemu_add_opts(&qemu_global_opts
);
2798 qemu_add_opts(&qemu_mon_opts
);
2799 qemu_add_opts(&qemu_trace_opts
);
2800 qemu_add_opts(&qemu_option_rom_opts
);
2801 qemu_add_opts(&qemu_machine_opts
);
2802 qemu_add_opts(&qemu_mem_opts
);
2803 qemu_add_opts(&qemu_smp_opts
);
2804 qemu_add_opts(&qemu_boot_opts
);
2805 qemu_add_opts(&qemu_sandbox_opts
);
2806 qemu_add_opts(&qemu_add_fd_opts
);
2807 qemu_add_opts(&qemu_object_opts
);
2808 qemu_add_opts(&qemu_tpmdev_opts
);
2809 qemu_add_opts(&qemu_realtime_opts
);
2810 qemu_add_opts(&qemu_msg_opts
);
2811 qemu_add_opts(&qemu_name_opts
);
2812 qemu_add_opts(&qemu_numa_opts
);
2813 qemu_add_opts(&qemu_icount_opts
);
2817 rtc_clock
= QEMU_CLOCK_HOST
;
2819 QLIST_INIT (&vm_change_state_head
);
2820 os_setup_early_signal_handling();
2822 module_call_init(MODULE_INIT_MACHINE
);
2823 machine_class
= find_default_machine();
2825 ram_size
= default_ram_size
;
2827 cyls
= heads
= secs
= 0;
2828 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2830 for (i
= 0; i
< MAX_NODES
; i
++) {
2831 numa_info
[i
].node_mem
= 0;
2832 numa_info
[i
].present
= false;
2833 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2837 max_numa_nodeid
= 0;
2840 bdrv_init_with_whitelist();
2844 /* first pass of option parsing */
2846 while (optind
< argc
) {
2847 if (argv
[optind
][0] != '-') {
2851 const QEMUOption
*popt
;
2853 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2854 switch (popt
->index
) {
2855 case QEMU_OPTION_nodefconfig
:
2858 case QEMU_OPTION_nouserconfig
:
2867 ret
= qemu_read_default_config_files(userconfig
);
2873 /* second pass of option parsing */
2878 if (argv
[optind
][0] != '-') {
2879 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2881 const QEMUOption
*popt
;
2883 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2884 if (!(popt
->arch_mask
& arch_type
)) {
2885 printf("Option %s not supported for this target\n", popt
->name
);
2888 switch(popt
->index
) {
2890 machine_class
= machine_parse(optarg
);
2892 case QEMU_OPTION_no_kvm_irqchip
: {
2893 olist
= qemu_find_opts("machine");
2894 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2897 case QEMU_OPTION_cpu
:
2898 /* hw initialization will check this */
2901 case QEMU_OPTION_hda
:
2905 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2907 snprintf(buf
, sizeof(buf
),
2908 "%s,cyls=%d,heads=%d,secs=%d%s",
2909 HD_OPTS
, cyls
, heads
, secs
,
2910 translation
== BIOS_ATA_TRANSLATION_LBA
?
2912 translation
== BIOS_ATA_TRANSLATION_NONE
?
2913 ",trans=none" : "");
2914 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2917 case QEMU_OPTION_hdb
:
2918 case QEMU_OPTION_hdc
:
2919 case QEMU_OPTION_hdd
:
2920 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2923 case QEMU_OPTION_drive
:
2924 if (drive_def(optarg
) == NULL
) {
2928 case QEMU_OPTION_set
:
2929 if (qemu_set_option(optarg
) != 0)
2932 case QEMU_OPTION_global
:
2933 if (qemu_global_option(optarg
) != 0)
2936 case QEMU_OPTION_mtdblock
:
2937 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2939 case QEMU_OPTION_sd
:
2940 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2942 case QEMU_OPTION_pflash
:
2943 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2945 case QEMU_OPTION_snapshot
:
2948 case QEMU_OPTION_hdachs
:
2952 cyls
= strtol(p
, (char **)&p
, 0);
2953 if (cyls
< 1 || cyls
> 16383)
2958 heads
= strtol(p
, (char **)&p
, 0);
2959 if (heads
< 1 || heads
> 16)
2964 secs
= strtol(p
, (char **)&p
, 0);
2965 if (secs
< 1 || secs
> 63)
2969 if (!strcmp(p
, "large")) {
2970 translation
= BIOS_ATA_TRANSLATION_LARGE
;
2971 } else if (!strcmp(p
, "rechs")) {
2972 translation
= BIOS_ATA_TRANSLATION_RECHS
;
2973 } else if (!strcmp(p
, "none")) {
2974 translation
= BIOS_ATA_TRANSLATION_NONE
;
2975 } else if (!strcmp(p
, "lba")) {
2976 translation
= BIOS_ATA_TRANSLATION_LBA
;
2977 } else if (!strcmp(p
, "auto")) {
2978 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2982 } else if (*p
!= '\0') {
2984 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2987 if (hda_opts
!= NULL
) {
2989 snprintf(num
, sizeof(num
), "%d", cyls
);
2990 qemu_opt_set(hda_opts
, "cyls", num
);
2991 snprintf(num
, sizeof(num
), "%d", heads
);
2992 qemu_opt_set(hda_opts
, "heads", num
);
2993 snprintf(num
, sizeof(num
), "%d", secs
);
2994 qemu_opt_set(hda_opts
, "secs", num
);
2995 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
2996 qemu_opt_set(hda_opts
, "trans", "large");
2997 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
2998 qemu_opt_set(hda_opts
, "trans", "rechs");
2999 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3000 qemu_opt_set(hda_opts
, "trans", "lba");
3001 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3002 qemu_opt_set(hda_opts
, "trans", "none");
3007 case QEMU_OPTION_numa
:
3008 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3013 case QEMU_OPTION_display
:
3014 display_type
= select_display(optarg
);
3016 case QEMU_OPTION_nographic
:
3017 display_type
= DT_NOGRAPHIC
;
3019 case QEMU_OPTION_curses
:
3020 #ifdef CONFIG_CURSES
3021 display_type
= DT_CURSES
;
3023 fprintf(stderr
, "Curses support is disabled\n");
3027 case QEMU_OPTION_portrait
:
3028 graphic_rotate
= 90;
3030 case QEMU_OPTION_rotate
:
3031 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3032 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3033 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3035 "qemu: only 90, 180, 270 deg rotation is available\n");
3039 case QEMU_OPTION_kernel
:
3040 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3042 case QEMU_OPTION_initrd
:
3043 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3045 case QEMU_OPTION_append
:
3046 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3048 case QEMU_OPTION_dtb
:
3049 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3051 case QEMU_OPTION_cdrom
:
3052 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3054 case QEMU_OPTION_boot
:
3055 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3060 case QEMU_OPTION_fda
:
3061 case QEMU_OPTION_fdb
:
3062 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3065 case QEMU_OPTION_no_fd_bootchk
:
3068 case QEMU_OPTION_netdev
:
3069 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3073 case QEMU_OPTION_net
:
3074 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3078 #ifdef CONFIG_LIBISCSI
3079 case QEMU_OPTION_iscsi
:
3080 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3087 case QEMU_OPTION_tftp
:
3088 legacy_tftp_prefix
= optarg
;
3090 case QEMU_OPTION_bootp
:
3091 legacy_bootp_filename
= optarg
;
3093 case QEMU_OPTION_redir
:
3094 if (net_slirp_redir(optarg
) < 0)
3098 case QEMU_OPTION_bt
:
3099 add_device_config(DEV_BT
, optarg
);
3101 case QEMU_OPTION_audio_help
:
3105 case QEMU_OPTION_soundhw
:
3106 select_soundhw (optarg
);
3111 case QEMU_OPTION_version
:
3115 case QEMU_OPTION_m
: {
3117 const char *mem_str
;
3118 const char *maxmem_str
, *slots_str
;
3120 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3126 mem_str
= qemu_opt_get(opts
, "size");
3128 error_report("invalid -m option, missing 'size' option");
3132 error_report("missing 'size' option value");
3136 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3138 /* Fix up legacy suffix-less format */
3139 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3140 uint64_t overflow_check
= sz
;
3143 if ((sz
>> 20) != overflow_check
) {
3144 error_report("too large 'size' option value");
3149 /* backward compatibility behaviour for case "-m 0" */
3151 sz
= default_ram_size
;
3154 sz
= QEMU_ALIGN_UP(sz
, 8192);
3156 if (ram_size
!= sz
) {
3157 error_report("ram size too large");
3160 maxram_size
= ram_size
;
3162 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3163 slots_str
= qemu_opt_get(opts
, "slots");
3164 if (maxmem_str
&& slots_str
) {
3167 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3168 if (sz
< ram_size
) {
3169 error_report("invalid -m option value: maxmem "
3170 "(0x%" PRIx64
") <= initial memory (0x"
3171 RAM_ADDR_FMT
")", sz
, ram_size
);
3175 slots
= qemu_opt_get_number(opts
, "slots", 0);
3176 if ((sz
> ram_size
) && !slots
) {
3177 error_report("invalid -m option value: maxmem "
3178 "(0x%" PRIx64
") more than initial memory (0x"
3179 RAM_ADDR_FMT
") but no hotplug slots where "
3180 "specified", sz
, ram_size
);
3184 if ((sz
<= ram_size
) && slots
) {
3185 error_report("invalid -m option value: %"
3186 PRIu64
" hotplug slots where specified but "
3187 "maxmem (0x%" PRIx64
") <= initial memory (0x"
3188 RAM_ADDR_FMT
")", slots
, sz
, ram_size
);
3193 } else if ((!maxmem_str
&& slots_str
) ||
3194 (maxmem_str
&& !slots_str
)) {
3195 error_report("invalid -m option value: missing "
3196 "'%s' option", slots_str
? "maxmem" : "slots");
3202 case QEMU_OPTION_tpmdev
:
3203 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3208 case QEMU_OPTION_mempath
:
3211 case QEMU_OPTION_mem_prealloc
:
3221 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3223 case QEMU_OPTION_gdb
:
3224 add_device_config(DEV_GDB
, optarg
);
3227 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3228 data_dir
[data_dir_idx
++] = optarg
;
3231 case QEMU_OPTION_bios
:
3232 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3234 case QEMU_OPTION_singlestep
:
3241 keyboard_layout
= optarg
;
3243 case QEMU_OPTION_localtime
:
3246 case QEMU_OPTION_vga
:
3255 w
= strtol(p
, (char **)&p
, 10);
3258 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3264 h
= strtol(p
, (char **)&p
, 10);
3269 depth
= strtol(p
, (char **)&p
, 10);
3270 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3271 depth
!= 24 && depth
!= 32)
3273 } else if (*p
== '\0') {
3274 depth
= graphic_depth
;
3281 graphic_depth
= depth
;
3284 case QEMU_OPTION_echr
:
3287 term_escape_char
= strtol(optarg
, &r
, 0);
3289 printf("Bad argument to echr\n");
3292 case QEMU_OPTION_monitor
:
3293 default_monitor
= 0;
3294 if (strncmp(optarg
, "none", 4)) {
3295 monitor_parse(optarg
, "readline");
3298 case QEMU_OPTION_qmp
:
3299 monitor_parse(optarg
, "control");
3300 default_monitor
= 0;
3302 case QEMU_OPTION_mon
:
3303 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3307 default_monitor
= 0;
3309 case QEMU_OPTION_chardev
:
3310 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3315 case QEMU_OPTION_fsdev
:
3316 olist
= qemu_find_opts("fsdev");
3318 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3321 opts
= qemu_opts_parse(olist
, optarg
, 1);
3326 case QEMU_OPTION_virtfs
: {
3329 const char *writeout
, *sock_fd
, *socket
;
3331 olist
= qemu_find_opts("virtfs");
3333 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3336 opts
= qemu_opts_parse(olist
, optarg
, 1);
3341 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3342 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3343 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3346 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3347 qemu_opt_get(opts
, "mount_tag"),
3350 fprintf(stderr
, "duplicate fsdev id: %s\n",
3351 qemu_opt_get(opts
, "mount_tag"));
3355 writeout
= qemu_opt_get(opts
, "writeout");
3357 #ifdef CONFIG_SYNC_FILE_RANGE
3358 qemu_opt_set(fsdev
, "writeout", writeout
);
3360 fprintf(stderr
, "writeout=immediate not supported on "
3365 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3366 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3367 qemu_opt_set(fsdev
, "security_model",
3368 qemu_opt_get(opts
, "security_model"));
3369 socket
= qemu_opt_get(opts
, "socket");
3371 qemu_opt_set(fsdev
, "socket", socket
);
3373 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3375 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3378 qemu_opt_set_bool(fsdev
, "readonly",
3379 qemu_opt_get_bool(opts
, "readonly", 0));
3380 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3382 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3383 qemu_opt_set(device
, "fsdev",
3384 qemu_opt_get(opts
, "mount_tag"));
3385 qemu_opt_set(device
, "mount_tag",
3386 qemu_opt_get(opts
, "mount_tag"));
3389 case QEMU_OPTION_virtfs_synth
: {
3393 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3396 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3399 qemu_opt_set(fsdev
, "fsdriver", "synth");
3401 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3403 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3404 qemu_opt_set(device
, "fsdev", "v_synth");
3405 qemu_opt_set(device
, "mount_tag", "v_synth");
3408 case QEMU_OPTION_serial
:
3409 add_device_config(DEV_SERIAL
, optarg
);
3411 if (strncmp(optarg
, "mon:", 4) == 0) {
3412 default_monitor
= 0;
3415 case QEMU_OPTION_watchdog
:
3418 "qemu: only one watchdog option may be given\n");
3423 case QEMU_OPTION_watchdog_action
:
3424 if (select_watchdog_action(optarg
) == -1) {
3425 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3429 case QEMU_OPTION_virtiocon
:
3430 add_device_config(DEV_VIRTCON
, optarg
);
3431 default_virtcon
= 0;
3432 if (strncmp(optarg
, "mon:", 4) == 0) {
3433 default_monitor
= 0;
3436 case QEMU_OPTION_parallel
:
3437 add_device_config(DEV_PARALLEL
, optarg
);
3438 default_parallel
= 0;
3439 if (strncmp(optarg
, "mon:", 4) == 0) {
3440 default_monitor
= 0;
3443 case QEMU_OPTION_debugcon
:
3444 add_device_config(DEV_DEBUGCON
, optarg
);
3446 case QEMU_OPTION_loadvm
:
3449 case QEMU_OPTION_full_screen
:
3452 case QEMU_OPTION_no_frame
:
3455 case QEMU_OPTION_alt_grab
:
3458 case QEMU_OPTION_ctrl_grab
:
3461 case QEMU_OPTION_no_quit
:
3464 case QEMU_OPTION_sdl
:
3466 display_type
= DT_SDL
;
3469 fprintf(stderr
, "SDL support is disabled\n");
3472 case QEMU_OPTION_pidfile
:
3475 case QEMU_OPTION_win2k_hack
:
3476 win2k_install_hack
= 1;
3478 case QEMU_OPTION_rtc_td_hack
: {
3479 static GlobalProperty slew_lost_ticks
[] = {
3481 .driver
= "mc146818rtc",
3482 .property
= "lost_tick_policy",
3485 { /* end of list */ }
3488 qdev_prop_register_global_list(slew_lost_ticks
);
3491 case QEMU_OPTION_acpitable
:
3492 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3496 do_acpitable_option(opts
);
3498 case QEMU_OPTION_smbios
:
3499 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3503 do_smbios_option(opts
);
3505 case QEMU_OPTION_enable_kvm
:
3506 olist
= qemu_find_opts("machine");
3507 qemu_opts_parse(olist
, "accel=kvm", 0);
3509 case QEMU_OPTION_machine
:
3510 olist
= qemu_find_opts("machine");
3511 opts
= qemu_opts_parse(olist
, optarg
, 1);
3515 optarg
= qemu_opt_get(opts
, "type");
3517 machine_class
= machine_parse(optarg
);
3520 case QEMU_OPTION_no_kvm
:
3521 olist
= qemu_find_opts("machine");
3522 qemu_opts_parse(olist
, "accel=tcg", 0);
3524 case QEMU_OPTION_no_kvm_pit
: {
3525 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3529 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3530 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3532 .driver
= "kvm-pit",
3533 .property
= "lost_tick_policy",
3536 { /* end of list */ }
3539 fprintf(stderr
, "Warning: option deprecated, use "
3540 "lost_tick_policy property of kvm-pit instead.\n");
3541 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3544 case QEMU_OPTION_usb
:
3545 olist
= qemu_find_opts("machine");
3546 qemu_opts_parse(olist
, "usb=on", 0);
3548 case QEMU_OPTION_usbdevice
:
3549 olist
= qemu_find_opts("machine");
3550 qemu_opts_parse(olist
, "usb=on", 0);
3551 add_device_config(DEV_USB
, optarg
);
3553 case QEMU_OPTION_device
:
3554 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3558 case QEMU_OPTION_smp
:
3559 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3563 case QEMU_OPTION_vnc
:
3566 vnc_display
= optarg
;
3568 fprintf(stderr
, "VNC support is disabled\n");
3572 case QEMU_OPTION_no_acpi
:
3575 case QEMU_OPTION_no_hpet
:
3578 case QEMU_OPTION_balloon
:
3579 if (balloon_parse(optarg
) < 0) {
3580 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3584 case QEMU_OPTION_no_reboot
:
3587 case QEMU_OPTION_no_shutdown
:
3590 case QEMU_OPTION_show_cursor
:
3593 case QEMU_OPTION_uuid
:
3594 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3595 fprintf(stderr
, "Fail to parse UUID string."
3596 " Wrong format.\n");
3599 qemu_uuid_set
= true;
3601 case QEMU_OPTION_option_rom
:
3602 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3603 fprintf(stderr
, "Too many option ROMs\n");
3606 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3610 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3611 option_rom
[nb_option_roms
].bootindex
=
3612 qemu_opt_get_number(opts
, "bootindex", -1);
3613 if (!option_rom
[nb_option_roms
].name
) {
3614 fprintf(stderr
, "Option ROM file is not specified\n");
3619 case QEMU_OPTION_semihosting
:
3620 semihosting_enabled
= 1;
3622 case QEMU_OPTION_tdf
:
3623 fprintf(stderr
, "Warning: user space PIT time drift fix "
3624 "is no longer supported.\n");
3626 case QEMU_OPTION_name
:
3627 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3632 case QEMU_OPTION_prom_env
:
3633 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3634 fprintf(stderr
, "Too many prom variables\n");
3637 prom_envs
[nb_prom_envs
] = optarg
;
3640 case QEMU_OPTION_old_param
:
3643 case QEMU_OPTION_clock
:
3644 /* Clock options no longer exist. Keep this option for
3645 * backward compatibility.
3648 case QEMU_OPTION_startdate
:
3649 configure_rtc_date_offset(optarg
, 1);
3651 case QEMU_OPTION_rtc
:
3652 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3656 configure_rtc(opts
);
3658 case QEMU_OPTION_tb_size
:
3659 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3660 if (tcg_tb_size
< 0) {
3664 case QEMU_OPTION_icount
:
3665 icount_opts
= qemu_opts_parse(qemu_find_opts("icount"),
3671 case QEMU_OPTION_incoming
:
3673 runstate_set(RUN_STATE_INMIGRATE
);
3675 case QEMU_OPTION_nodefaults
:
3678 case QEMU_OPTION_xen_domid
:
3679 if (!(xen_available())) {
3680 printf("Option %s not supported for this target\n", popt
->name
);
3683 xen_domid
= atoi(optarg
);
3685 case QEMU_OPTION_xen_create
:
3686 if (!(xen_available())) {
3687 printf("Option %s not supported for this target\n", popt
->name
);
3690 xen_mode
= XEN_CREATE
;
3692 case QEMU_OPTION_xen_attach
:
3693 if (!(xen_available())) {
3694 printf("Option %s not supported for this target\n", popt
->name
);
3697 xen_mode
= XEN_ATTACH
;
3699 case QEMU_OPTION_trace
:
3701 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3705 trace_events
= qemu_opt_get(opts
, "events");
3706 trace_file
= qemu_opt_get(opts
, "file");
3709 case QEMU_OPTION_readconfig
:
3711 int ret
= qemu_read_config_file(optarg
);
3713 fprintf(stderr
, "read config %s: %s\n", optarg
,
3719 case QEMU_OPTION_spice
:
3720 olist
= qemu_find_opts("spice");
3722 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3725 opts
= qemu_opts_parse(olist
, optarg
, 0);
3731 case QEMU_OPTION_writeconfig
:
3734 if (strcmp(optarg
, "-") == 0) {
3737 fp
= fopen(optarg
, "w");
3739 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3743 qemu_config_write(fp
);
3749 case QEMU_OPTION_qtest
:
3750 qtest_chrdev
= optarg
;
3752 case QEMU_OPTION_qtest_log
:
3755 case QEMU_OPTION_sandbox
:
3756 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3761 case QEMU_OPTION_add_fd
:
3763 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3768 error_report("File descriptor passing is disabled on this "
3773 case QEMU_OPTION_object
:
3774 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3779 case QEMU_OPTION_realtime
:
3780 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3784 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
3786 case QEMU_OPTION_msg
:
3787 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3791 configure_msg(opts
);
3793 case QEMU_OPTION_dump_vmstate
:
3794 if (vmstate_dump_file
) {
3795 fprintf(stderr
, "qemu: only one '-dump-vmstate' "
3796 "option may be given\n");
3799 vmstate_dump_file
= fopen(optarg
, "w");
3800 if (vmstate_dump_file
== NULL
) {
3801 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3806 os_parse_cmd_args(popt
->index
, optarg
);
3814 if (qemu_init_main_loop(&main_loop_err
)) {
3815 error_report("%s", error_get_pretty(main_loop_err
));
3819 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3823 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3828 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3832 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3837 if (machine_class
== NULL
) {
3838 fprintf(stderr
, "No machine specified, and there is no default.\n"
3839 "Use -machine help to list supported machines!\n");
3843 current_machine
= MACHINE(object_new(object_class_get_name(
3844 OBJECT_CLASS(machine_class
))));
3845 object_property_add_child(object_get_root(), "machine",
3846 OBJECT(current_machine
), &error_abort
);
3847 cpu_exec_init_all();
3849 if (machine_class
->hw_version
) {
3850 qemu_set_version(machine_class
->hw_version
);
3853 /* Init CPU def lists, based on config
3854 * - Must be called after all the qemu_read_config_file() calls
3855 * - Must be called before list_cpus()
3856 * - Must be called before machine->init()
3860 if (cpu_model
&& is_help_option(cpu_model
)) {
3861 list_cpus(stdout
, &fprintf
, cpu_model
);
3865 /* Open the logfile at this point, if necessary. We can't open the logfile
3866 * when encountering either of the logging options (-d or -D) because the
3867 * other one may be encountered later on the command line, changing the
3868 * location or level of logging.
3873 qemu_set_log_filename(log_file
);
3876 mask
= qemu_str_to_log_mask(log_mask
);
3878 qemu_print_log_usage(stdout
);
3884 if (!is_daemonized()) {
3885 if (!trace_init_backends(trace_events
, trace_file
)) {
3890 /* If no data_dir is specified then try to find it relative to the
3892 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3893 data_dir
[data_dir_idx
] = os_find_datadir();
3894 if (data_dir
[data_dir_idx
] != NULL
) {
3898 /* If all else fails use the install path specified when building. */
3899 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3900 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3903 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3905 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
3906 if (smp_cpus
> machine_class
->max_cpus
) {
3907 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3908 "supported by machine `%s' (%d)\n", smp_cpus
,
3909 machine_class
->name
, machine_class
->max_cpus
);
3914 * Get the default machine options from the machine if it is not already
3915 * specified either by the configuration file or by the command line.
3917 if (machine_class
->default_machine_opts
) {
3918 qemu_opts_set_defaults(qemu_find_opts("machine"),
3919 machine_class
->default_machine_opts
, 0);
3922 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3923 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3925 if (!vga_model
&& !default_vga
) {
3926 vga_interface_type
= VGA_DEVICE
;
3928 if (!has_defaults
|| machine_class
->no_serial
) {
3931 if (!has_defaults
|| machine_class
->no_parallel
) {
3932 default_parallel
= 0;
3934 if (!has_defaults
|| !machine_class
->use_virtcon
) {
3935 default_virtcon
= 0;
3937 if (!has_defaults
|| !machine_class
->use_sclp
) {
3940 if (!has_defaults
|| machine_class
->no_floppy
) {
3943 if (!has_defaults
|| machine_class
->no_cdrom
) {
3946 if (!has_defaults
|| machine_class
->no_sdcard
) {
3949 if (!has_defaults
) {
3950 default_monitor
= 0;
3955 if (is_daemonized()) {
3956 /* According to documentation and historically, -nographic redirects
3957 * serial port, parallel port and monitor to stdio, which does not work
3958 * with -daemonize. We can redirect these to null instead, but since
3959 * -nographic is legacy, let's just error out.
3960 * We disallow -nographic only if all other ports are not redirected
3961 * explicitly, to not break existing legacy setups which uses
3962 * -nographic _and_ redirects all ports explicitly - this is valid
3963 * usage, -nographic is just a no-op in this case.
3965 if (display_type
== DT_NOGRAPHIC
3966 && (default_parallel
|| default_serial
3967 || default_monitor
|| default_virtcon
)) {
3968 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3971 #ifdef CONFIG_CURSES
3972 if (display_type
== DT_CURSES
) {
3973 fprintf(stderr
, "curses display can not be used with -daemonize\n");
3979 if (display_type
== DT_NOGRAPHIC
) {
3980 if (default_parallel
)
3981 add_device_config(DEV_PARALLEL
, "null");
3982 if (default_serial
&& default_monitor
) {
3983 add_device_config(DEV_SERIAL
, "mon:stdio");
3984 } else if (default_virtcon
&& default_monitor
) {
3985 add_device_config(DEV_VIRTCON
, "mon:stdio");
3986 } else if (default_sclp
&& default_monitor
) {
3987 add_device_config(DEV_SCLP
, "mon:stdio");
3990 add_device_config(DEV_SERIAL
, "stdio");
3991 if (default_virtcon
)
3992 add_device_config(DEV_VIRTCON
, "stdio");
3994 add_device_config(DEV_SCLP
, "stdio");
3996 if (default_monitor
)
3997 monitor_parse("stdio", "readline");
4001 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4002 if (default_parallel
)
4003 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4004 if (default_monitor
)
4005 monitor_parse("vc:80Cx24C", "readline");
4006 if (default_virtcon
)
4007 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4009 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4013 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4014 #if defined(CONFIG_GTK)
4015 display_type
= DT_GTK
;
4016 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4017 display_type
= DT_SDL
;
4018 #elif defined(CONFIG_VNC)
4019 vnc_display
= "localhost:0,to=99";
4022 display_type
= DT_NONE
;
4026 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4027 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4028 "for SDL, ignoring option\n");
4030 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4031 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4032 "ignoring option\n");
4035 #if defined(CONFIG_GTK)
4036 if (display_type
== DT_GTK
) {
4037 early_gtk_display_init();
4043 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4045 #ifdef CONFIG_VIRTFS
4046 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4051 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4052 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4056 /* store value for the future use */
4057 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4059 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4064 if (qemu_opts_foreach(qemu_find_opts("object"),
4065 object_create
, NULL
, 0) != 0) {
4069 machine_opts
= qemu_get_machine_opts();
4070 if (qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4072 object_unref(OBJECT(current_machine
));
4076 configure_accelerator(current_machine
);
4079 Error
*local_err
= NULL
;
4080 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4082 error_report("%s", error_get_pretty(local_err
));
4083 error_free(local_err
);
4088 machine_opts
= qemu_get_machine_opts();
4089 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4090 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4091 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4092 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4094 boot_order
= machine_class
->default_boot_order
;
4095 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4097 char *normal_boot_order
;
4098 const char *order
, *once
;
4100 order
= qemu_opt_get(opts
, "order");
4102 validate_bootdevices(order
);
4106 once
= qemu_opt_get(opts
, "once");
4108 validate_bootdevices(once
);
4109 normal_boot_order
= g_strdup(boot_order
);
4111 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4114 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4115 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4118 if (!kernel_cmdline
) {
4119 kernel_cmdline
= "";
4120 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4123 linux_boot
= (kernel_filename
!= NULL
);
4125 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4126 fprintf(stderr
, "-append only allowed with -kernel option\n");
4130 if (!linux_boot
&& initrd_filename
!= NULL
) {
4131 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4135 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4136 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4140 os_set_line_buffering();
4142 qemu_init_cpu_loop();
4143 qemu_mutex_lock_iothread();
4146 /* spice needs the timers to be initialized by this point */
4152 if (kvm_enabled() || xen_enabled()) {
4153 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4156 configure_icount(icount_opts
, &error_abort
);
4157 qemu_opts_del(icount_opts
);
4160 /* clean up network at qemu process termination */
4161 atexit(&net_cleanup
);
4163 if (net_init_clients() < 0) {
4168 if (tpm_init() < 0) {
4173 /* init the bluetooth world */
4174 if (foreach_device_config(DEV_BT
, bt_parse
))
4177 if (!xen_enabled()) {
4178 /* On 32-bit hosts, QEMU is limited by virtual address space */
4179 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4180 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4188 /* If the currently selected machine wishes to override the units-per-bus
4189 * property of its default HBA interface type, do so now. */
4190 if (machine_class
->units_per_default_bus
) {
4191 override_max_devs(machine_class
->block_default_type
,
4192 machine_class
->units_per_default_bus
);
4195 /* open the virtual block devices */
4197 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4198 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4199 &machine_class
->block_default_type
, 1) != 0) {
4203 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4205 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4206 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4208 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4215 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4219 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4221 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4223 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4225 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4228 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4231 /* If no default VGA is requested, the default is "none". */
4233 if (machine_class
->default_display
) {
4234 vga_model
= machine_class
->default_display
;
4235 } else if (cirrus_vga_available()) {
4236 vga_model
= "cirrus";
4237 } else if (vga_available()) {
4242 select_vgahw(vga_model
);
4246 i
= select_watchdog(watchdog
);
4248 exit (i
== 1 ? 1 : 0);
4251 if (machine_class
->compat_props
) {
4252 qdev_prop_register_global_list(machine_class
->compat_props
);
4256 qdev_machine_init();
4258 current_machine
->ram_size
= ram_size
;
4259 current_machine
->maxram_size
= maxram_size
;
4260 current_machine
->ram_slots
= ram_slots
;
4261 current_machine
->boot_order
= boot_order
;
4262 current_machine
->cpu_model
= cpu_model
;
4264 machine_class
->init(current_machine
);
4270 cpu_synchronize_all_post_init();
4274 /* init USB devices */
4275 if (usb_enabled(false)) {
4276 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4280 /* init generic devices */
4281 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4284 /* Did we create any drives that we failed to create a device for? */
4285 drive_check_orphaned();
4287 net_check_clients();
4289 ds
= init_displaystate();
4291 /* init local displays */
4292 switch (display_type
) {
4294 (void)ds
; /* avoid warning if no display is configured */
4296 #if defined(CONFIG_CURSES)
4298 curses_display_init(ds
, full_screen
);
4301 #if defined(CONFIG_SDL)
4303 sdl_display_init(ds
, full_screen
, no_frame
);
4305 #elif defined(CONFIG_COCOA)
4307 cocoa_display_init(ds
, full_screen
);
4310 #if defined(CONFIG_GTK)
4312 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4319 /* must be after terminal init, SDL library changes signal handlers */
4320 os_setup_signal_handling();
4323 /* init remote displays */
4325 Error
*local_err
= NULL
;
4326 vnc_display_init(ds
);
4327 vnc_display_open(ds
, vnc_display
, &local_err
);
4328 if (local_err
!= NULL
) {
4329 error_report("Failed to start VNC server on `%s': %s",
4330 vnc_display
, error_get_pretty(local_err
));
4331 error_free(local_err
);
4335 if (show_vnc_port
) {
4336 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4342 qemu_spice_display_init();
4346 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4350 qdev_machine_creation_done();
4352 if (rom_load_all() != 0) {
4353 fprintf(stderr
, "rom loading failed\n");
4357 /* TODO: once all bus devices are qdevified, this should be done
4358 * when bus is created by qdev.c */
4359 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4360 qemu_run_machine_init_done_notifiers();
4362 /* Done notifiers can load ROMs */
4365 qemu_system_reset(VMRESET_SILENT
);
4367 if (load_vmstate(loadvm
) < 0) {
4372 qdev_prop_check_globals();
4373 if (vmstate_dump_file
) {
4375 dump_vmstate_json_to_file(vmstate_dump_file
);
4380 Error
*local_err
= NULL
;
4381 qemu_start_incoming_migration(incoming
, &local_err
);
4383 error_report("-incoming %s: %s", incoming
,
4384 error_get_pretty(local_err
));
4385 error_free(local_err
);
4388 } else if (autostart
) {
4394 if (is_daemonized()) {
4395 if (!trace_init_backends(trace_events
, trace_file
)) {