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 NotifierList exit_notifiers
=
200 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
202 static NotifierList machine_init_done_notifiers
=
203 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
207 enum xen_mode xen_mode
= XEN_EMULATE
;
209 static int has_defaults
= 1;
210 static int default_serial
= 1;
211 static int default_parallel
= 1;
212 static int default_virtcon
= 1;
213 static int default_sclp
= 1;
214 static int default_monitor
= 1;
215 static int default_floppy
= 1;
216 static int default_cdrom
= 1;
217 static int default_sdcard
= 1;
218 static int default_vga
= 1;
224 { .driver
= "isa-serial", .flag
= &default_serial
},
225 { .driver
= "isa-parallel", .flag
= &default_parallel
},
226 { .driver
= "isa-fdc", .flag
= &default_floppy
},
227 { .driver
= "ide-cd", .flag
= &default_cdrom
},
228 { .driver
= "ide-hd", .flag
= &default_cdrom
},
229 { .driver
= "ide-drive", .flag
= &default_cdrom
},
230 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
231 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
232 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
233 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
234 { .driver
= "VGA", .flag
= &default_vga
},
235 { .driver
= "isa-vga", .flag
= &default_vga
},
236 { .driver
= "cirrus-vga", .flag
= &default_vga
},
237 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
238 { .driver
= "vmware-svga", .flag
= &default_vga
},
239 { .driver
= "qxl-vga", .flag
= &default_vga
},
242 static QemuOptsList qemu_rtc_opts
= {
244 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
248 .type
= QEMU_OPT_STRING
,
251 .type
= QEMU_OPT_STRING
,
254 .type
= QEMU_OPT_STRING
,
256 { /* end of list */ }
260 static QemuOptsList qemu_sandbox_opts
= {
262 .implied_opt_name
= "enable",
263 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
267 .type
= QEMU_OPT_BOOL
,
269 { /* end of list */ }
273 static QemuOptsList qemu_trace_opts
= {
275 .implied_opt_name
= "trace",
276 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
280 .type
= QEMU_OPT_STRING
,
283 .type
= QEMU_OPT_STRING
,
285 { /* end of list */ }
289 static QemuOptsList qemu_option_rom_opts
= {
290 .name
= "option-rom",
291 .implied_opt_name
= "romfile",
292 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
296 .type
= QEMU_OPT_NUMBER
,
299 .type
= QEMU_OPT_STRING
,
301 { /* end of list */ }
305 static QemuOptsList qemu_machine_opts
= {
307 .implied_opt_name
= "type",
309 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
313 .type
= QEMU_OPT_STRING
,
314 .help
= "emulated machine"
317 .type
= QEMU_OPT_STRING
,
318 .help
= "accelerator list",
320 .name
= "kernel_irqchip",
321 .type
= QEMU_OPT_BOOL
,
322 .help
= "use KVM in-kernel irqchip",
324 .name
= "kvm_shadow_mem",
325 .type
= QEMU_OPT_SIZE
,
326 .help
= "KVM shadow MMU size",
329 .type
= QEMU_OPT_STRING
,
330 .help
= "Linux kernel image file",
333 .type
= QEMU_OPT_STRING
,
334 .help
= "Linux initial ramdisk file",
337 .type
= QEMU_OPT_STRING
,
338 .help
= "Linux kernel command line",
341 .type
= QEMU_OPT_STRING
,
342 .help
= "Linux kernel device tree file",
345 .type
= QEMU_OPT_STRING
,
346 .help
= "Dump current dtb to a file and quit",
348 .name
= "phandle_start",
349 .type
= QEMU_OPT_NUMBER
,
350 .help
= "The first phandle ID we may generate dynamically",
352 .name
= "dt_compatible",
353 .type
= QEMU_OPT_STRING
,
354 .help
= "Overrides the \"compatible\" property of the dt root node",
356 .name
= "dump-guest-core",
357 .type
= QEMU_OPT_BOOL
,
358 .help
= "Include guest memory in a core dump",
361 .type
= QEMU_OPT_BOOL
,
362 .help
= "enable/disable memory merge support",
365 .type
= QEMU_OPT_BOOL
,
366 .help
= "Set on/off to enable/disable usb",
369 .type
= QEMU_OPT_STRING
,
370 .help
= "firmware image",
373 .type
= QEMU_OPT_STRING
,
374 .help
= "Specifies the KVM virtualization mode (HV, PR)",
376 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
377 .type
= QEMU_OPT_SIZE
,
378 .help
= "maximum ram below the 4G boundary (32bit boundary)",
380 .name
= PC_MACHINE_VMPORT
,
381 .type
= QEMU_OPT_STRING
,
382 .help
= "Enable vmport (pc & q35)",
385 .type
= QEMU_OPT_BOOL
,
386 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
388 { /* End of list */ }
392 static QemuOptsList qemu_boot_opts
= {
394 .implied_opt_name
= "order",
396 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
400 .type
= QEMU_OPT_STRING
,
403 .type
= QEMU_OPT_STRING
,
406 .type
= QEMU_OPT_BOOL
,
409 .type
= QEMU_OPT_STRING
,
411 .name
= "splash-time",
412 .type
= QEMU_OPT_STRING
,
414 .name
= "reboot-timeout",
415 .type
= QEMU_OPT_STRING
,
418 .type
= QEMU_OPT_BOOL
,
424 static QemuOptsList qemu_add_fd_opts
= {
426 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
430 .type
= QEMU_OPT_NUMBER
,
431 .help
= "file descriptor of which a duplicate is added to fd set",
434 .type
= QEMU_OPT_NUMBER
,
435 .help
= "ID of the fd set to add fd to",
438 .type
= QEMU_OPT_STRING
,
439 .help
= "free-form string used to describe fd",
441 { /* end of list */ }
445 static QemuOptsList qemu_object_opts
= {
447 .implied_opt_name
= "qom-type",
448 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
454 static QemuOptsList qemu_tpmdev_opts
= {
456 .implied_opt_name
= "type",
457 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
459 /* options are defined in the TPM backends */
460 { /* end of list */ }
464 static QemuOptsList qemu_realtime_opts
= {
466 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
470 .type
= QEMU_OPT_BOOL
,
472 { /* end of list */ }
476 static QemuOptsList qemu_msg_opts
= {
478 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
482 .type
= QEMU_OPT_BOOL
,
484 { /* end of list */ }
488 static QemuOptsList qemu_name_opts
= {
490 .implied_opt_name
= "guest",
492 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
496 .type
= QEMU_OPT_STRING
,
497 .help
= "Sets the name of the guest.\n"
498 "This name will be displayed in the SDL window caption.\n"
499 "The name will also be used for the VNC server",
502 .type
= QEMU_OPT_STRING
,
503 .help
= "Sets the name of the QEMU process, as shown in top etc",
505 .name
= "debug-threads",
506 .type
= QEMU_OPT_BOOL
,
507 .help
= "When enabled, name the individual threads; defaults off.\n"
508 "NOTE: The thread names are for debugging and not a\n"
511 { /* End of list */ }
515 static QemuOptsList qemu_mem_opts
= {
517 .implied_opt_name
= "size",
518 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
523 .type
= QEMU_OPT_SIZE
,
527 .type
= QEMU_OPT_NUMBER
,
531 .type
= QEMU_OPT_SIZE
,
533 { /* end of list */ }
537 static QemuOptsList qemu_icount_opts
= {
539 .implied_opt_name
= "shift",
541 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
545 .type
= QEMU_OPT_STRING
,
548 .type
= QEMU_OPT_BOOL
,
550 { /* end of list */ }
554 static QemuOptsList qemu_semihosting_config_opts
= {
555 .name
= "semihosting-config",
556 .implied_opt_name
= "enable",
557 .head
= QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts
.head
),
561 .type
= QEMU_OPT_BOOL
,
564 .type
= QEMU_OPT_STRING
,
566 { /* end of list */ }
571 * Get machine options
573 * Returns: machine options (never null).
575 QemuOpts
*qemu_get_machine_opts(void)
577 return qemu_find_opts_singleton("machine");
580 const char *qemu_get_vm_name(void)
585 static void res_free(void)
587 if (boot_splash_filedata
!= NULL
) {
588 g_free(boot_splash_filedata
);
589 boot_splash_filedata
= NULL
;
593 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
595 const char *driver
= qemu_opt_get(opts
, "driver");
600 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
601 if (strcmp(default_list
[i
].driver
, driver
) != 0)
603 *(default_list
[i
].flag
) = 0;
608 /***********************************************************/
611 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
613 /* We use RUN_STATE_MAX but any invalid value will do */
614 static RunState vmstop_requested
= RUN_STATE_MAX
;
615 static QemuMutex vmstop_lock
;
620 } RunStateTransition
;
622 static const RunStateTransition runstate_transitions_def
[] = {
624 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
625 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
627 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
628 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
630 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
631 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
633 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
634 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
636 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
637 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
639 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
640 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
642 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
643 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
644 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
646 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
647 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
649 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
651 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
652 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
653 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
654 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
655 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
656 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
657 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
658 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
659 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
660 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
662 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
664 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
665 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
667 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
668 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
669 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
670 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
672 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
673 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
675 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
676 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
678 { RUN_STATE_MAX
, RUN_STATE_MAX
},
681 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
683 bool runstate_check(RunState state
)
685 return current_run_state
== state
;
688 static void runstate_init(void)
690 const RunStateTransition
*p
;
692 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
693 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
694 runstate_valid_transitions
[p
->from
][p
->to
] = true;
697 qemu_mutex_init(&vmstop_lock
);
700 /* This function will abort() on invalid state transitions */
701 void runstate_set(RunState new_state
)
703 assert(new_state
< RUN_STATE_MAX
);
705 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
706 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
707 RunState_lookup
[current_run_state
],
708 RunState_lookup
[new_state
]);
711 trace_runstate_set(new_state
);
712 current_run_state
= new_state
;
715 int runstate_is_running(void)
717 return runstate_check(RUN_STATE_RUNNING
);
720 bool runstate_needs_reset(void)
722 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
723 runstate_check(RUN_STATE_SHUTDOWN
);
726 StatusInfo
*qmp_query_status(Error
**errp
)
728 StatusInfo
*info
= g_malloc0(sizeof(*info
));
730 info
->running
= runstate_is_running();
731 info
->singlestep
= singlestep
;
732 info
->status
= current_run_state
;
737 static bool qemu_vmstop_requested(RunState
*r
)
739 qemu_mutex_lock(&vmstop_lock
);
740 *r
= vmstop_requested
;
741 vmstop_requested
= RUN_STATE_MAX
;
742 qemu_mutex_unlock(&vmstop_lock
);
743 return *r
< RUN_STATE_MAX
;
746 void qemu_system_vmstop_request_prepare(void)
748 qemu_mutex_lock(&vmstop_lock
);
751 void qemu_system_vmstop_request(RunState state
)
753 vmstop_requested
= state
;
754 qemu_mutex_unlock(&vmstop_lock
);
762 qemu_vmstop_requested(&requested
);
763 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
767 /* Ensure that a STOP/RESUME pair of events is emitted if a
768 * vmstop request was pending. The BLOCK_IO_ERROR event, for
769 * example, according to documentation is always followed by
772 if (runstate_is_running()) {
773 qapi_event_send_stop(&error_abort
);
776 runstate_set(RUN_STATE_RUNNING
);
777 vm_state_notify(1, RUN_STATE_RUNNING
);
781 qapi_event_send_resume(&error_abort
);
785 /***********************************************************/
786 /* real time host monotonic timer */
788 /***********************************************************/
789 /* host time/date access */
790 void qemu_get_timedate(struct tm
*tm
, int offset
)
796 if (rtc_date_offset
== -1) {
800 localtime_r(&ti
, tm
);
802 ti
-= rtc_date_offset
;
807 int qemu_timedate_diff(struct tm
*tm
)
811 if (rtc_date_offset
== -1)
813 seconds
= mktimegm(tm
);
816 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
817 seconds
= mktime(&tmp
);
820 seconds
= mktimegm(tm
) + rtc_date_offset
;
822 return seconds
- time(NULL
);
825 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
827 time_t rtc_start_date
;
830 if (!strcmp(startdate
, "now") && legacy
) {
831 rtc_date_offset
= -1;
833 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
841 } else if (sscanf(startdate
, "%d-%d-%d",
853 rtc_start_date
= mktimegm(&tm
);
854 if (rtc_start_date
== -1) {
856 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
857 "'2006-06-17T16:01:21' or '2006-06-17'\n");
860 rtc_date_offset
= time(NULL
) - rtc_start_date
;
864 static void configure_rtc(QemuOpts
*opts
)
868 value
= qemu_opt_get(opts
, "base");
870 if (!strcmp(value
, "utc")) {
872 } else if (!strcmp(value
, "localtime")) {
875 configure_rtc_date_offset(value
, 0);
878 value
= qemu_opt_get(opts
, "clock");
880 if (!strcmp(value
, "host")) {
881 rtc_clock
= QEMU_CLOCK_HOST
;
882 } else if (!strcmp(value
, "rt")) {
883 rtc_clock
= QEMU_CLOCK_REALTIME
;
884 } else if (!strcmp(value
, "vm")) {
885 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
887 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
891 value
= qemu_opt_get(opts
, "driftfix");
893 if (!strcmp(value
, "slew")) {
894 static GlobalProperty slew_lost_ticks
[] = {
896 .driver
= "mc146818rtc",
897 .property
= "lost_tick_policy",
900 { /* end of list */ }
903 qdev_prop_register_global_list(slew_lost_ticks
);
904 } else if (!strcmp(value
, "none")) {
905 /* discard is default */
907 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
913 /***********************************************************/
914 /* Bluetooth support */
917 static struct HCIInfo
*hci_table
[MAX_NICS
];
919 struct HCIInfo
*qemu_next_hci(void)
921 if (cur_hci
== nb_hcis
)
924 return hci_table
[cur_hci
++];
927 static int bt_hci_parse(const char *str
)
932 if (nb_hcis
>= MAX_NICS
) {
933 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
946 bdaddr
.b
[5] = 0x56 + nb_hcis
;
947 hci
->bdaddr_set(hci
, bdaddr
.b
);
949 hci_table
[nb_hcis
++] = hci
;
954 static void bt_vhci_add(int vlan_id
)
956 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
959 fprintf(stderr
, "qemu: warning: adding a VHCI to "
960 "an empty scatternet %i\n", vlan_id
);
962 bt_vhci_init(bt_new_hci(vlan
));
965 static struct bt_device_s
*bt_device_add(const char *opt
)
967 struct bt_scatternet_s
*vlan
;
969 char *endp
= strstr(opt
, ",vlan=");
970 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
973 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
976 vlan_id
= strtol(endp
+ 6, &endp
, 0);
978 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
983 vlan
= qemu_find_bt_vlan(vlan_id
);
986 fprintf(stderr
, "qemu: warning: adding a slave device to "
987 "an empty scatternet %i\n", vlan_id
);
989 if (!strcmp(devname
, "keyboard"))
990 return bt_keyboard_init(vlan
);
992 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
996 static int bt_parse(const char *opt
)
998 const char *endp
, *p
;
1001 if (strstart(opt
, "hci", &endp
)) {
1002 if (!*endp
|| *endp
== ',') {
1004 if (!strstart(endp
, ",vlan=", 0))
1007 return bt_hci_parse(opt
);
1009 } else if (strstart(opt
, "vhci", &endp
)) {
1010 if (!*endp
|| *endp
== ',') {
1012 if (strstart(endp
, ",vlan=", &p
)) {
1013 vlan
= strtol(p
, (char **) &endp
, 0);
1015 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1019 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1028 } else if (strstart(opt
, "device:", &endp
))
1029 return !bt_device_add(endp
);
1031 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1035 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1037 /* FIXME: change this to true for 1.3 */
1038 if (qemu_opt_get_bool(opts
, "enable", false)) {
1039 #ifdef CONFIG_SECCOMP
1040 if (seccomp_start() < 0) {
1041 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1042 "failed to install seccomp syscall filter in the kernel");
1046 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1047 "sandboxing request but seccomp is not compiled into this build");
1055 static int parse_name(QemuOpts
*opts
, void *opaque
)
1057 const char *proc_name
;
1059 if (qemu_opt_get(opts
, "debug-threads")) {
1060 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1062 qemu_name
= qemu_opt_get(opts
, "guest");
1064 proc_name
= qemu_opt_get(opts
, "process");
1066 os_set_proc_name(proc_name
);
1072 bool usb_enabled(bool default_usb
)
1074 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1075 has_defaults
&& default_usb
);
1079 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1081 int fd
, dupfd
, flags
;
1083 const char *fd_opaque
= NULL
;
1085 fd
= qemu_opt_get_number(opts
, "fd", -1);
1086 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1087 fd_opaque
= qemu_opt_get(opts
, "opaque");
1090 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1091 "fd option is required and must be non-negative");
1095 if (fd
<= STDERR_FILENO
) {
1096 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1097 "fd cannot be a standard I/O stream");
1102 * All fds inherited across exec() necessarily have FD_CLOEXEC
1103 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1105 flags
= fcntl(fd
, F_GETFD
);
1106 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1107 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1108 "fd is not valid or already in use");
1113 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1114 "set option is required and must be non-negative");
1118 #ifdef F_DUPFD_CLOEXEC
1119 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1123 qemu_set_cloexec(dupfd
);
1127 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1128 "Error duplicating fd: %s", strerror(errno
));
1132 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1133 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1139 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1143 fd
= qemu_opt_get_number(opts
, "fd", -1);
1150 /***********************************************************/
1151 /* QEMU Block devices */
1153 #define HD_OPTS "media=disk"
1154 #define CDROM_OPTS "media=cdrom"
1156 #define PFLASH_OPTS ""
1160 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1162 BlockInterfaceType
*block_default_type
= opaque
;
1164 return drive_new(opts
, *block_default_type
) == NULL
;
1167 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1169 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1170 qemu_opt_set(opts
, "snapshot", "on");
1175 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1176 int index
, const char *optstr
)
1181 if (!enable
|| drive_get_by_index(type
, index
)) {
1185 opts
= drive_add(type
, index
, NULL
, optstr
);
1187 drive_enable_snapshot(opts
, NULL
);
1190 dinfo
= drive_new(opts
, type
);
1194 dinfo
->is_default
= true;
1198 static QemuOptsList qemu_smp_opts
= {
1200 .implied_opt_name
= "cpus",
1201 .merge_lists
= true,
1202 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1206 .type
= QEMU_OPT_NUMBER
,
1209 .type
= QEMU_OPT_NUMBER
,
1212 .type
= QEMU_OPT_NUMBER
,
1215 .type
= QEMU_OPT_NUMBER
,
1218 .type
= QEMU_OPT_NUMBER
,
1220 { /*End of list */ }
1224 static void smp_parse(QemuOpts
*opts
)
1228 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1229 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1230 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1231 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1233 /* compute missing values, prefer sockets over cores over threads */
1234 if (cpus
== 0 || sockets
== 0) {
1235 sockets
= sockets
> 0 ? sockets
: 1;
1236 cores
= cores
> 0 ? cores
: 1;
1237 threads
= threads
> 0 ? threads
: 1;
1239 cpus
= cores
* threads
* sockets
;
1243 threads
= threads
> 0 ? threads
: 1;
1244 cores
= cpus
/ (sockets
* threads
);
1246 threads
= cpus
/ (cores
* sockets
);
1250 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1253 smp_cores
= cores
> 0 ? cores
: 1;
1254 smp_threads
= threads
> 0 ? threads
: 1;
1258 if (max_cpus
== 0) {
1259 max_cpus
= smp_cpus
;
1262 if (max_cpus
> MAX_CPUMASK_BITS
) {
1263 fprintf(stderr
, "Unsupported number of maxcpus\n");
1266 if (max_cpus
< smp_cpus
) {
1267 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1273 static void realtime_init(void)
1276 if (os_mlock() < 0) {
1277 fprintf(stderr
, "qemu: locking memory failed\n");
1284 static void configure_msg(QemuOpts
*opts
)
1286 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1289 /***********************************************************/
1292 static int usb_device_add(const char *devname
)
1294 USBDevice
*dev
= NULL
;
1295 #ifndef CONFIG_LINUX
1299 if (!usb_enabled(false)) {
1303 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1304 dev
= usbdevice_create(devname
);
1308 /* the other ones */
1309 #ifndef CONFIG_LINUX
1310 /* only the linux version is qdev-ified, usb-bsd still needs this */
1311 if (strstart(devname
, "host:", &p
)) {
1312 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1322 static int usb_device_del(const char *devname
)
1327 if (strstart(devname
, "host:", &p
)) {
1331 if (!usb_enabled(false)) {
1335 p
= strchr(devname
, '.');
1338 bus_num
= strtoul(devname
, NULL
, 0);
1339 addr
= strtoul(p
+ 1, NULL
, 0);
1341 return usb_device_delete_addr(bus_num
, addr
);
1344 static int usb_parse(const char *cmdline
)
1347 r
= usb_device_add(cmdline
);
1349 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1354 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1356 const char *devname
= qdict_get_str(qdict
, "devname");
1357 if (usb_device_add(devname
) < 0) {
1358 error_report("could not add USB device '%s'", devname
);
1362 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1364 const char *devname
= qdict_get_str(qdict
, "devname");
1365 if (usb_device_del(devname
) < 0) {
1366 error_report("could not delete USB device '%s'", devname
);
1370 /***********************************************************/
1371 /* machine registration */
1373 MachineState
*current_machine
;
1375 static void machine_class_init(ObjectClass
*oc
, void *data
)
1377 MachineClass
*mc
= MACHINE_CLASS(oc
);
1378 QEMUMachine
*qm
= data
;
1380 mc
->family
= qm
->family
;
1381 mc
->name
= qm
->name
;
1382 mc
->alias
= qm
->alias
;
1383 mc
->desc
= qm
->desc
;
1384 mc
->init
= qm
->init
;
1385 mc
->reset
= qm
->reset
;
1386 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1387 mc
->kvm_type
= qm
->kvm_type
;
1388 mc
->block_default_type
= qm
->block_default_type
;
1389 mc
->units_per_default_bus
= qm
->units_per_default_bus
;
1390 mc
->max_cpus
= qm
->max_cpus
;
1391 mc
->no_serial
= qm
->no_serial
;
1392 mc
->no_parallel
= qm
->no_parallel
;
1393 mc
->use_virtcon
= qm
->use_virtcon
;
1394 mc
->use_sclp
= qm
->use_sclp
;
1395 mc
->no_floppy
= qm
->no_floppy
;
1396 mc
->no_cdrom
= qm
->no_cdrom
;
1397 mc
->no_sdcard
= qm
->no_sdcard
;
1398 mc
->has_dynamic_sysbus
= qm
->has_dynamic_sysbus
;
1399 mc
->is_default
= qm
->is_default
;
1400 mc
->default_machine_opts
= qm
->default_machine_opts
;
1401 mc
->default_boot_order
= qm
->default_boot_order
;
1402 mc
->default_display
= qm
->default_display
;
1403 mc
->compat_props
= qm
->compat_props
;
1404 mc
->hw_version
= qm
->hw_version
;
1407 int qemu_register_machine(QEMUMachine
*m
)
1409 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1412 .parent
= TYPE_MACHINE
,
1413 .class_init
= machine_class_init
,
1414 .class_data
= (void *)m
,
1423 static MachineClass
*find_machine(const char *name
)
1425 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1426 MachineClass
*mc
= NULL
;
1428 for (el
= machines
; el
; el
= el
->next
) {
1429 MachineClass
*temp
= el
->data
;
1431 if (!strcmp(temp
->name
, name
)) {
1436 !strcmp(temp
->alias
, name
)) {
1442 g_slist_free(machines
);
1446 MachineClass
*find_default_machine(void)
1448 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1449 MachineClass
*mc
= NULL
;
1451 for (el
= machines
; el
; el
= el
->next
) {
1452 MachineClass
*temp
= el
->data
;
1454 if (temp
->is_default
) {
1460 g_slist_free(machines
);
1464 MachineInfoList
*qmp_query_machines(Error
**errp
)
1466 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1467 MachineInfoList
*mach_list
= NULL
;
1469 for (el
= machines
; el
; el
= el
->next
) {
1470 MachineClass
*mc
= el
->data
;
1471 MachineInfoList
*entry
;
1474 info
= g_malloc0(sizeof(*info
));
1475 if (mc
->is_default
) {
1476 info
->has_is_default
= true;
1477 info
->is_default
= true;
1481 info
->has_alias
= true;
1482 info
->alias
= g_strdup(mc
->alias
);
1485 info
->name
= g_strdup(mc
->name
);
1486 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1488 entry
= g_malloc0(sizeof(*entry
));
1489 entry
->value
= info
;
1490 entry
->next
= mach_list
;
1494 g_slist_free(machines
);
1498 /***********************************************************/
1499 /* main execution loop */
1501 struct vm_change_state_entry
{
1502 VMChangeStateHandler
*cb
;
1504 QLIST_ENTRY (vm_change_state_entry
) entries
;
1507 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1509 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1512 VMChangeStateEntry
*e
;
1514 e
= g_malloc0(sizeof (*e
));
1518 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1522 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1524 QLIST_REMOVE (e
, entries
);
1528 void vm_state_notify(int running
, RunState state
)
1530 VMChangeStateEntry
*e
, *next
;
1532 trace_vm_state_notify(running
, state
);
1534 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1535 e
->cb(e
->opaque
, running
, state
);
1539 /* reset/shutdown handler */
1541 typedef struct QEMUResetEntry
{
1542 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1543 QEMUResetHandler
*func
;
1547 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1548 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1549 static int reset_requested
;
1550 static int shutdown_requested
, shutdown_signal
= -1;
1551 static pid_t shutdown_pid
;
1552 static int powerdown_requested
;
1553 static int debug_requested
;
1554 static int suspend_requested
;
1555 static WakeupReason wakeup_reason
;
1556 static NotifierList powerdown_notifiers
=
1557 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1558 static NotifierList suspend_notifiers
=
1559 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1560 static NotifierList wakeup_notifiers
=
1561 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1562 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1564 int qemu_shutdown_requested_get(void)
1566 return shutdown_requested
;
1569 int qemu_reset_requested_get(void)
1571 return reset_requested
;
1574 static int qemu_shutdown_requested(void)
1576 return atomic_xchg(&shutdown_requested
, 0);
1579 static void qemu_kill_report(void)
1581 if (!qtest_driver() && shutdown_signal
!= -1) {
1582 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1583 if (shutdown_pid
== 0) {
1584 /* This happens for eg ^C at the terminal, so it's worth
1585 * avoiding printing an odd message in that case.
1587 fputc('\n', stderr
);
1589 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1591 shutdown_signal
= -1;
1595 static int qemu_reset_requested(void)
1597 int r
= reset_requested
;
1598 reset_requested
= 0;
1602 static int qemu_suspend_requested(void)
1604 int r
= suspend_requested
;
1605 suspend_requested
= 0;
1609 static WakeupReason
qemu_wakeup_requested(void)
1611 return wakeup_reason
;
1614 static int qemu_powerdown_requested(void)
1616 int r
= powerdown_requested
;
1617 powerdown_requested
= 0;
1621 static int qemu_debug_requested(void)
1623 int r
= debug_requested
;
1624 debug_requested
= 0;
1628 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1630 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1633 re
->opaque
= opaque
;
1634 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1637 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1641 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1642 if (re
->func
== func
&& re
->opaque
== opaque
) {
1643 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1650 void qemu_devices_reset(void)
1652 QEMUResetEntry
*re
, *nre
;
1654 /* reset all devices */
1655 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1656 re
->func(re
->opaque
);
1660 void qemu_system_reset(bool report
)
1664 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1666 if (mc
&& mc
->reset
) {
1669 qemu_devices_reset();
1672 qapi_event_send_reset(&error_abort
);
1674 cpu_synchronize_all_post_reset();
1677 void qemu_system_reset_request(void)
1680 shutdown_requested
= 1;
1682 reset_requested
= 1;
1685 qemu_notify_event();
1688 static void qemu_system_suspend(void)
1691 notifier_list_notify(&suspend_notifiers
, NULL
);
1692 runstate_set(RUN_STATE_SUSPENDED
);
1693 qapi_event_send_suspend(&error_abort
);
1696 void qemu_system_suspend_request(void)
1698 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1701 suspend_requested
= 1;
1703 qemu_notify_event();
1706 void qemu_register_suspend_notifier(Notifier
*notifier
)
1708 notifier_list_add(&suspend_notifiers
, notifier
);
1711 void qemu_system_wakeup_request(WakeupReason reason
)
1713 trace_system_wakeup_request(reason
);
1715 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1718 if (!(wakeup_reason_mask
& (1 << reason
))) {
1721 runstate_set(RUN_STATE_RUNNING
);
1722 wakeup_reason
= reason
;
1723 qemu_notify_event();
1726 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1729 wakeup_reason_mask
|= (1 << reason
);
1731 wakeup_reason_mask
&= ~(1 << reason
);
1735 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1737 notifier_list_add(&wakeup_notifiers
, notifier
);
1740 void qemu_system_killed(int signal
, pid_t pid
)
1742 shutdown_signal
= signal
;
1745 qemu_system_shutdown_request();
1748 void qemu_system_shutdown_request(void)
1750 trace_qemu_system_shutdown_request();
1751 shutdown_requested
= 1;
1752 qemu_notify_event();
1755 static void qemu_system_powerdown(void)
1757 qapi_event_send_powerdown(&error_abort
);
1758 notifier_list_notify(&powerdown_notifiers
, NULL
);
1761 void qemu_system_powerdown_request(void)
1763 trace_qemu_system_powerdown_request();
1764 powerdown_requested
= 1;
1765 qemu_notify_event();
1768 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1770 notifier_list_add(&powerdown_notifiers
, notifier
);
1773 void qemu_system_debug_request(void)
1775 debug_requested
= 1;
1776 qemu_notify_event();
1779 static bool main_loop_should_exit(void)
1782 if (qemu_debug_requested()) {
1783 vm_stop(RUN_STATE_DEBUG
);
1785 if (qemu_suspend_requested()) {
1786 qemu_system_suspend();
1788 if (qemu_shutdown_requested()) {
1790 qapi_event_send_shutdown(&error_abort
);
1792 vm_stop(RUN_STATE_SHUTDOWN
);
1797 if (qemu_reset_requested()) {
1799 cpu_synchronize_all_states();
1800 qemu_system_reset(VMRESET_REPORT
);
1802 if (runstate_needs_reset()) {
1803 runstate_set(RUN_STATE_PAUSED
);
1806 if (qemu_wakeup_requested()) {
1808 cpu_synchronize_all_states();
1809 qemu_system_reset(VMRESET_SILENT
);
1810 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1811 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1813 qapi_event_send_wakeup(&error_abort
);
1815 if (qemu_powerdown_requested()) {
1816 qemu_system_powerdown();
1818 if (qemu_vmstop_requested(&r
)) {
1824 static void main_loop(void)
1828 #ifdef CONFIG_PROFILER
1832 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1833 #ifdef CONFIG_PROFILER
1834 ti
= profile_getclock();
1836 last_io
= main_loop_wait(nonblocking
);
1837 #ifdef CONFIG_PROFILER
1838 dev_time
+= profile_getclock() - ti
;
1840 } while (!main_loop_should_exit());
1843 static void version(void)
1845 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1848 static void help(int exitcode
)
1851 printf("usage: %s [options] [disk_image]\n\n"
1852 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1853 error_get_progname());
1855 #define QEMU_OPTIONS_GENERATE_HELP
1856 #include "qemu-options-wrapper.h"
1858 printf("\nDuring emulation, the following keys are useful:\n"
1859 "ctrl-alt-f toggle full screen\n"
1860 "ctrl-alt-n switch to virtual console 'n'\n"
1861 "ctrl-alt toggle mouse and keyboard grab\n"
1863 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1868 #define HAS_ARG 0x0001
1870 typedef struct QEMUOption
{
1877 static const QEMUOption qemu_options
[] = {
1878 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1879 #define QEMU_OPTIONS_GENERATE_OPTIONS
1880 #include "qemu-options-wrapper.h"
1884 static bool vga_available(void)
1886 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1889 static bool cirrus_vga_available(void)
1891 return object_class_by_name("cirrus-vga")
1892 || object_class_by_name("isa-cirrus-vga");
1895 static bool vmware_vga_available(void)
1897 return object_class_by_name("vmware-svga");
1900 static bool qxl_vga_available(void)
1902 return object_class_by_name("qxl-vga");
1905 static bool tcx_vga_available(void)
1907 return object_class_by_name("SUNW,tcx");
1910 static bool cg3_vga_available(void)
1912 return object_class_by_name("cgthree");
1915 static void select_vgahw (const char *p
)
1919 assert(vga_interface_type
== VGA_NONE
);
1920 if (strstart(p
, "std", &opts
)) {
1921 if (vga_available()) {
1922 vga_interface_type
= VGA_STD
;
1924 fprintf(stderr
, "Error: standard VGA not available\n");
1927 } else if (strstart(p
, "cirrus", &opts
)) {
1928 if (cirrus_vga_available()) {
1929 vga_interface_type
= VGA_CIRRUS
;
1931 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1934 } else if (strstart(p
, "vmware", &opts
)) {
1935 if (vmware_vga_available()) {
1936 vga_interface_type
= VGA_VMWARE
;
1938 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1941 } else if (strstart(p
, "xenfb", &opts
)) {
1942 vga_interface_type
= VGA_XENFB
;
1943 } else if (strstart(p
, "qxl", &opts
)) {
1944 if (qxl_vga_available()) {
1945 vga_interface_type
= VGA_QXL
;
1947 fprintf(stderr
, "Error: QXL VGA not available\n");
1950 } else if (strstart(p
, "tcx", &opts
)) {
1951 if (tcx_vga_available()) {
1952 vga_interface_type
= VGA_TCX
;
1954 fprintf(stderr
, "Error: TCX framebuffer not available\n");
1957 } else if (strstart(p
, "cg3", &opts
)) {
1958 if (cg3_vga_available()) {
1959 vga_interface_type
= VGA_CG3
;
1961 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
1964 } else if (!strstart(p
, "none", &opts
)) {
1966 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1970 const char *nextopt
;
1972 if (strstart(opts
, ",retrace=", &nextopt
)) {
1974 if (strstart(opts
, "dumb", &nextopt
))
1975 vga_retrace_method
= VGA_RETRACE_DUMB
;
1976 else if (strstart(opts
, "precise", &nextopt
))
1977 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1978 else goto invalid_vga
;
1979 } else goto invalid_vga
;
1984 static DisplayType
select_display(const char *p
)
1987 DisplayType display
= DT_DEFAULT
;
1989 if (strstart(p
, "sdl", &opts
)) {
1993 const char *nextopt
;
1995 if (strstart(opts
, ",frame=", &nextopt
)) {
1997 if (strstart(opts
, "on", &nextopt
)) {
1999 } else if (strstart(opts
, "off", &nextopt
)) {
2002 goto invalid_sdl_args
;
2004 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2006 if (strstart(opts
, "on", &nextopt
)) {
2008 } else if (strstart(opts
, "off", &nextopt
)) {
2011 goto invalid_sdl_args
;
2013 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2015 if (strstart(opts
, "on", &nextopt
)) {
2017 } else if (strstart(opts
, "off", &nextopt
)) {
2020 goto invalid_sdl_args
;
2022 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2024 if (strstart(opts
, "on", &nextopt
)) {
2026 } else if (strstart(opts
, "off", &nextopt
)) {
2029 goto invalid_sdl_args
;
2033 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2039 fprintf(stderr
, "SDL support is disabled\n");
2042 } else if (strstart(p
, "vnc", &opts
)) {
2047 const char *nextopt
;
2049 if (strstart(opts
, "=", &nextopt
)) {
2050 vnc_display
= nextopt
;
2054 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2058 fprintf(stderr
, "VNC support is disabled\n");
2061 } else if (strstart(p
, "curses", &opts
)) {
2062 #ifdef CONFIG_CURSES
2063 display
= DT_CURSES
;
2065 fprintf(stderr
, "Curses support is disabled\n");
2068 } else if (strstart(p
, "gtk", &opts
)) {
2072 const char *nextopt
;
2074 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2076 if (strstart(opts
, "on", &nextopt
)) {
2077 grab_on_hover
= true;
2078 } else if (strstart(opts
, "off", &nextopt
)) {
2079 grab_on_hover
= false;
2081 goto invalid_gtk_args
;
2085 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2091 fprintf(stderr
, "GTK support is disabled\n");
2094 } else if (strstart(p
, "none", &opts
)) {
2097 fprintf(stderr
, "Unknown display type: %s\n", p
);
2104 static int balloon_parse(const char *arg
)
2108 if (strcmp(arg
, "none") == 0) {
2112 if (!strncmp(arg
, "virtio", 6)) {
2113 if (arg
[6] == ',') {
2114 /* have params -> parse them */
2115 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2119 /* create empty opts */
2120 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2123 qemu_opt_set(opts
, "driver", "virtio-balloon");
2130 char *qemu_find_file(int type
, const char *name
)
2136 /* Try the name as a straight path first */
2137 if (access(name
, R_OK
) == 0) {
2138 trace_load_file(name
, name
);
2139 return g_strdup(name
);
2143 case QEMU_FILE_TYPE_BIOS
:
2146 case QEMU_FILE_TYPE_KEYMAP
:
2147 subdir
= "keymaps/";
2153 for (i
= 0; i
< data_dir_idx
; i
++) {
2154 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2155 if (access(buf
, R_OK
) == 0) {
2156 trace_load_file(name
, buf
);
2164 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2166 return qdev_device_help(opts
);
2169 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2173 dev
= qdev_device_add(opts
);
2176 object_unref(OBJECT(dev
));
2180 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2182 Error
*local_err
= NULL
;
2184 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2186 error_report("%s", error_get_pretty(local_err
));
2187 error_free(local_err
);
2193 #ifdef CONFIG_VIRTFS
2194 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2197 ret
= qemu_fsdev_add(opts
);
2203 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2205 CharDriverState
*chr
;
2206 const char *chardev
;
2210 mode
= qemu_opt_get(opts
, "mode");
2214 if (strcmp(mode
, "readline") == 0) {
2215 flags
= MONITOR_USE_READLINE
;
2216 } else if (strcmp(mode
, "control") == 0) {
2217 flags
= MONITOR_USE_CONTROL
;
2219 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2223 if (qemu_opt_get_bool(opts
, "pretty", 0))
2224 flags
|= MONITOR_USE_PRETTY
;
2226 if (qemu_opt_get_bool(opts
, "default", 0))
2227 flags
|= MONITOR_IS_DEFAULT
;
2229 chardev
= qemu_opt_get(opts
, "chardev");
2230 chr
= qemu_chr_find(chardev
);
2232 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2236 qemu_chr_fe_claim_no_fail(chr
);
2237 monitor_init(chr
, flags
);
2241 static void monitor_parse(const char *optarg
, const char *mode
, bool pretty
)
2243 static int monitor_device_index
= 0;
2249 if (strstart(optarg
, "chardev:", &p
)) {
2250 snprintf(label
, sizeof(label
), "%s", p
);
2252 snprintf(label
, sizeof(label
), "compat_monitor%d",
2253 monitor_device_index
);
2254 if (monitor_device_index
== 0) {
2257 opts
= qemu_chr_parse_compat(label
, optarg
);
2259 fprintf(stderr
, "parse error: %s\n", optarg
);
2264 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2266 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2269 qemu_opt_set(opts
, "mode", mode
);
2270 qemu_opt_set(opts
, "chardev", label
);
2271 qemu_opt_set_bool(opts
, "pretty", pretty
);
2273 qemu_opt_set(opts
, "default", "on");
2274 monitor_device_index
++;
2277 struct device_config
{
2279 DEV_USB
, /* -usbdevice */
2281 DEV_SERIAL
, /* -serial */
2282 DEV_PARALLEL
, /* -parallel */
2283 DEV_VIRTCON
, /* -virtioconsole */
2284 DEV_DEBUGCON
, /* -debugcon */
2285 DEV_GDB
, /* -gdb, -s */
2286 DEV_SCLP
, /* s390 sclp */
2288 const char *cmdline
;
2290 QTAILQ_ENTRY(device_config
) next
;
2293 static QTAILQ_HEAD(, device_config
) device_configs
=
2294 QTAILQ_HEAD_INITIALIZER(device_configs
);
2296 static void add_device_config(int type
, const char *cmdline
)
2298 struct device_config
*conf
;
2300 conf
= g_malloc0(sizeof(*conf
));
2302 conf
->cmdline
= cmdline
;
2303 loc_save(&conf
->loc
);
2304 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2307 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2309 struct device_config
*conf
;
2312 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2313 if (conf
->type
!= type
)
2315 loc_push_restore(&conf
->loc
);
2316 rc
= func(conf
->cmdline
);
2317 loc_pop(&conf
->loc
);
2325 static int serial_parse(const char *devname
)
2327 static int index
= 0;
2330 if (strcmp(devname
, "none") == 0)
2332 if (index
== MAX_SERIAL_PORTS
) {
2333 fprintf(stderr
, "qemu: too many serial ports\n");
2336 snprintf(label
, sizeof(label
), "serial%d", index
);
2337 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2338 if (!serial_hds
[index
]) {
2339 fprintf(stderr
, "qemu: could not connect serial device"
2340 " to character backend '%s'\n", devname
);
2347 static int parallel_parse(const char *devname
)
2349 static int index
= 0;
2352 if (strcmp(devname
, "none") == 0)
2354 if (index
== MAX_PARALLEL_PORTS
) {
2355 fprintf(stderr
, "qemu: too many parallel ports\n");
2358 snprintf(label
, sizeof(label
), "parallel%d", index
);
2359 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2360 if (!parallel_hds
[index
]) {
2361 fprintf(stderr
, "qemu: could not connect parallel device"
2362 " to character backend '%s'\n", devname
);
2369 static int virtcon_parse(const char *devname
)
2371 QemuOptsList
*device
= qemu_find_opts("device");
2372 static int index
= 0;
2374 QemuOpts
*bus_opts
, *dev_opts
;
2376 if (strcmp(devname
, "none") == 0)
2378 if (index
== MAX_VIRTIO_CONSOLES
) {
2379 fprintf(stderr
, "qemu: too many virtio consoles\n");
2383 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2384 if (arch_type
== QEMU_ARCH_S390X
) {
2385 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2387 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2390 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2391 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2393 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2394 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2395 if (!virtcon_hds
[index
]) {
2396 fprintf(stderr
, "qemu: could not connect virtio console"
2397 " to character backend '%s'\n", devname
);
2400 qemu_opt_set(dev_opts
, "chardev", label
);
2406 static int sclp_parse(const char *devname
)
2408 QemuOptsList
*device
= qemu_find_opts("device");
2409 static int index
= 0;
2413 if (strcmp(devname
, "none") == 0) {
2416 if (index
== MAX_SCLP_CONSOLES
) {
2417 fprintf(stderr
, "qemu: too many sclp consoles\n");
2421 assert(arch_type
== QEMU_ARCH_S390X
);
2423 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2424 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2426 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2427 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2428 if (!sclp_hds
[index
]) {
2429 fprintf(stderr
, "qemu: could not connect sclp console"
2430 " to character backend '%s'\n", devname
);
2433 qemu_opt_set(dev_opts
, "chardev", label
);
2439 static int debugcon_parse(const char *devname
)
2443 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2446 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2448 fprintf(stderr
, "qemu: already have a debugcon device\n");
2451 qemu_opt_set(opts
, "driver", "isa-debugcon");
2452 qemu_opt_set(opts
, "chardev", "debugcon");
2456 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2458 const MachineClass
*mc1
= a
, *mc2
= b
;
2461 if (mc1
->family
== NULL
) {
2462 if (mc2
->family
== NULL
) {
2463 /* Compare standalone machine types against each other; they sort
2464 * in increasing order.
2466 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2467 object_class_get_name(OBJECT_CLASS(mc2
)));
2470 /* Standalone machine types sort after families. */
2474 if (mc2
->family
== NULL
) {
2475 /* Families sort before standalone machine types. */
2479 /* Families sort between each other alphabetically increasingly. */
2480 res
= strcmp(mc1
->family
, mc2
->family
);
2485 /* Within the same family, machine types sort in decreasing order. */
2486 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2487 object_class_get_name(OBJECT_CLASS(mc1
)));
2490 static MachineClass
*machine_parse(const char *name
)
2492 MachineClass
*mc
= NULL
;
2493 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2496 mc
= find_machine(name
);
2501 if (name
&& !is_help_option(name
)) {
2502 error_report("Unsupported machine type");
2503 error_printf("Use -machine help to list supported machines!\n");
2505 printf("Supported machines are:\n");
2506 machines
= g_slist_sort(machines
, machine_class_cmp
);
2507 for (el
= machines
; el
; el
= el
->next
) {
2508 MachineClass
*mc
= el
->data
;
2510 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2512 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2513 mc
->is_default
? " (default)" : "");
2517 g_slist_free(machines
);
2518 exit(!name
|| !is_help_option(name
));
2521 void qemu_add_exit_notifier(Notifier
*notify
)
2523 notifier_list_add(&exit_notifiers
, notify
);
2526 void qemu_remove_exit_notifier(Notifier
*notify
)
2528 notifier_remove(notify
);
2531 static void qemu_run_exit_notifiers(void)
2533 notifier_list_notify(&exit_notifiers
, NULL
);
2536 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2538 notifier_list_add(&machine_init_done_notifiers
, notify
);
2541 static void qemu_run_machine_init_done_notifiers(void)
2543 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2546 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2547 const char **poptarg
, int *poptind
)
2549 const QEMUOption
*popt
;
2550 int optind
= *poptind
;
2551 char *r
= argv
[optind
];
2554 loc_set_cmdline(argv
, optind
, 1);
2556 /* Treat --foo the same as -foo. */
2559 popt
= qemu_options
;
2562 error_report("invalid option");
2565 if (!strcmp(popt
->name
, r
+ 1))
2569 if (popt
->flags
& HAS_ARG
) {
2570 if (optind
>= argc
) {
2571 error_report("requires an argument");
2574 optarg
= argv
[optind
++];
2575 loc_set_cmdline(argv
, optind
- 2, 2);
2586 static gpointer
malloc_and_trace(gsize n_bytes
)
2588 void *ptr
= malloc(n_bytes
);
2589 trace_g_malloc(n_bytes
, ptr
);
2593 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2595 void *ptr
= realloc(mem
, n_bytes
);
2596 trace_g_realloc(mem
, n_bytes
, ptr
);
2600 static void free_and_trace(gpointer mem
)
2606 static int machine_set_property(const char *name
, const char *value
,
2609 Object
*obj
= OBJECT(opaque
);
2610 StringInputVisitor
*siv
;
2611 Error
*local_err
= NULL
;
2614 if (strcmp(name
, "type") == 0) {
2618 qom_name
= g_strdup(name
);
2626 siv
= string_input_visitor_new(value
);
2627 object_property_set(obj
, string_input_get_visitor(siv
), qom_name
, &local_err
);
2628 string_input_visitor_cleanup(siv
);
2632 qerror_report_err(local_err
);
2633 error_free(local_err
);
2640 static int object_create(QemuOpts
*opts
, void *opaque
)
2649 ov
= opts_visitor_new(opts
);
2650 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2652 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2657 qdict_del(pdict
, "qom-type");
2658 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2663 qdict_del(pdict
, "id");
2664 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2669 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2673 visit_end_struct(opts_get_visitor(ov
), &err
);
2675 qmp_object_del(id
, NULL
);
2679 opts_visitor_cleanup(ov
);
2686 qerror_report_err(err
);
2693 int main(int argc
, char **argv
, char **envp
)
2696 int snapshot
, linux_boot
;
2697 const char *initrd_filename
;
2698 const char *kernel_filename
, *kernel_cmdline
;
2699 const char *boot_order
;
2701 int cyls
, heads
, secs
, translation
;
2702 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
, *icount_opts
= NULL
;
2703 QemuOptsList
*olist
;
2706 const char *loadvm
= NULL
;
2707 MachineClass
*machine_class
;
2708 const char *cpu_model
;
2709 const char *vga_model
= NULL
;
2710 const char *qtest_chrdev
= NULL
;
2711 const char *qtest_log
= NULL
;
2712 const char *pid_file
= NULL
;
2713 const char *incoming
= NULL
;
2715 int show_vnc_port
= 0;
2717 bool defconfig
= true;
2718 bool userconfig
= true;
2719 const char *log_mask
= NULL
;
2720 const char *log_file
= NULL
;
2721 GMemVTable mem_trace
= {
2722 .malloc
= malloc_and_trace
,
2723 .realloc
= realloc_and_trace
,
2724 .free
= free_and_trace
,
2726 const char *trace_events
= NULL
;
2727 const char *trace_file
= NULL
;
2728 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2730 ram_addr_t maxram_size
= default_ram_size
;
2731 uint64_t ram_slots
= 0;
2732 FILE *vmstate_dump_file
= NULL
;
2733 Error
*main_loop_err
= NULL
;
2735 atexit(qemu_run_exit_notifiers
);
2736 error_set_progname(argv
[0]);
2737 qemu_init_exec_dir(argv
[0]);
2739 g_mem_set_vtable(&mem_trace
);
2741 module_call_init(MODULE_INIT_QOM
);
2743 qemu_add_opts(&qemu_drive_opts
);
2744 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2745 qemu_add_drive_opts(&qemu_common_drive_opts
);
2746 qemu_add_drive_opts(&qemu_drive_opts
);
2747 qemu_add_opts(&qemu_chardev_opts
);
2748 qemu_add_opts(&qemu_device_opts
);
2749 qemu_add_opts(&qemu_netdev_opts
);
2750 qemu_add_opts(&qemu_net_opts
);
2751 qemu_add_opts(&qemu_rtc_opts
);
2752 qemu_add_opts(&qemu_global_opts
);
2753 qemu_add_opts(&qemu_mon_opts
);
2754 qemu_add_opts(&qemu_trace_opts
);
2755 qemu_add_opts(&qemu_option_rom_opts
);
2756 qemu_add_opts(&qemu_machine_opts
);
2757 qemu_add_opts(&qemu_mem_opts
);
2758 qemu_add_opts(&qemu_smp_opts
);
2759 qemu_add_opts(&qemu_boot_opts
);
2760 qemu_add_opts(&qemu_sandbox_opts
);
2761 qemu_add_opts(&qemu_add_fd_opts
);
2762 qemu_add_opts(&qemu_object_opts
);
2763 qemu_add_opts(&qemu_tpmdev_opts
);
2764 qemu_add_opts(&qemu_realtime_opts
);
2765 qemu_add_opts(&qemu_msg_opts
);
2766 qemu_add_opts(&qemu_name_opts
);
2767 qemu_add_opts(&qemu_numa_opts
);
2768 qemu_add_opts(&qemu_icount_opts
);
2769 qemu_add_opts(&qemu_semihosting_config_opts
);
2773 rtc_clock
= QEMU_CLOCK_HOST
;
2775 QLIST_INIT (&vm_change_state_head
);
2776 os_setup_early_signal_handling();
2778 module_call_init(MODULE_INIT_MACHINE
);
2779 machine_class
= find_default_machine();
2781 ram_size
= default_ram_size
;
2783 cyls
= heads
= secs
= 0;
2784 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2786 for (i
= 0; i
< MAX_NODES
; i
++) {
2787 numa_info
[i
].node_mem
= 0;
2788 numa_info
[i
].present
= false;
2789 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2793 max_numa_nodeid
= 0;
2796 bdrv_init_with_whitelist();
2800 /* first pass of option parsing */
2802 while (optind
< argc
) {
2803 if (argv
[optind
][0] != '-') {
2807 const QEMUOption
*popt
;
2809 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2810 switch (popt
->index
) {
2811 case QEMU_OPTION_nodefconfig
:
2814 case QEMU_OPTION_nouserconfig
:
2823 ret
= qemu_read_default_config_files(userconfig
);
2829 /* second pass of option parsing */
2834 if (argv
[optind
][0] != '-') {
2835 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2837 const QEMUOption
*popt
;
2839 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2840 if (!(popt
->arch_mask
& arch_type
)) {
2841 printf("Option %s not supported for this target\n", popt
->name
);
2844 switch(popt
->index
) {
2846 machine_class
= machine_parse(optarg
);
2848 case QEMU_OPTION_no_kvm_irqchip
: {
2849 olist
= qemu_find_opts("machine");
2850 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2853 case QEMU_OPTION_cpu
:
2854 /* hw initialization will check this */
2857 case QEMU_OPTION_hda
:
2861 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2863 snprintf(buf
, sizeof(buf
),
2864 "%s,cyls=%d,heads=%d,secs=%d%s",
2865 HD_OPTS
, cyls
, heads
, secs
,
2866 translation
== BIOS_ATA_TRANSLATION_LBA
?
2868 translation
== BIOS_ATA_TRANSLATION_NONE
?
2869 ",trans=none" : "");
2870 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2873 case QEMU_OPTION_hdb
:
2874 case QEMU_OPTION_hdc
:
2875 case QEMU_OPTION_hdd
:
2876 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2879 case QEMU_OPTION_drive
:
2880 if (drive_def(optarg
) == NULL
) {
2884 case QEMU_OPTION_set
:
2885 if (qemu_set_option(optarg
) != 0)
2888 case QEMU_OPTION_global
:
2889 if (qemu_global_option(optarg
) != 0)
2892 case QEMU_OPTION_mtdblock
:
2893 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2895 case QEMU_OPTION_sd
:
2896 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2898 case QEMU_OPTION_pflash
:
2899 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2901 case QEMU_OPTION_snapshot
:
2904 case QEMU_OPTION_hdachs
:
2908 cyls
= strtol(p
, (char **)&p
, 0);
2909 if (cyls
< 1 || cyls
> 16383)
2914 heads
= strtol(p
, (char **)&p
, 0);
2915 if (heads
< 1 || heads
> 16)
2920 secs
= strtol(p
, (char **)&p
, 0);
2921 if (secs
< 1 || secs
> 63)
2925 if (!strcmp(p
, "large")) {
2926 translation
= BIOS_ATA_TRANSLATION_LARGE
;
2927 } else if (!strcmp(p
, "rechs")) {
2928 translation
= BIOS_ATA_TRANSLATION_RECHS
;
2929 } else if (!strcmp(p
, "none")) {
2930 translation
= BIOS_ATA_TRANSLATION_NONE
;
2931 } else if (!strcmp(p
, "lba")) {
2932 translation
= BIOS_ATA_TRANSLATION_LBA
;
2933 } else if (!strcmp(p
, "auto")) {
2934 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2938 } else if (*p
!= '\0') {
2940 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2943 if (hda_opts
!= NULL
) {
2945 snprintf(num
, sizeof(num
), "%d", cyls
);
2946 qemu_opt_set(hda_opts
, "cyls", num
);
2947 snprintf(num
, sizeof(num
), "%d", heads
);
2948 qemu_opt_set(hda_opts
, "heads", num
);
2949 snprintf(num
, sizeof(num
), "%d", secs
);
2950 qemu_opt_set(hda_opts
, "secs", num
);
2951 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
2952 qemu_opt_set(hda_opts
, "trans", "large");
2953 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
2954 qemu_opt_set(hda_opts
, "trans", "rechs");
2955 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
2956 qemu_opt_set(hda_opts
, "trans", "lba");
2957 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
2958 qemu_opt_set(hda_opts
, "trans", "none");
2963 case QEMU_OPTION_numa
:
2964 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
2969 case QEMU_OPTION_display
:
2970 display_type
= select_display(optarg
);
2972 case QEMU_OPTION_nographic
:
2973 display_type
= DT_NOGRAPHIC
;
2975 case QEMU_OPTION_curses
:
2976 #ifdef CONFIG_CURSES
2977 display_type
= DT_CURSES
;
2979 fprintf(stderr
, "Curses support is disabled\n");
2983 case QEMU_OPTION_portrait
:
2984 graphic_rotate
= 90;
2986 case QEMU_OPTION_rotate
:
2987 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2988 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2989 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2991 "qemu: only 90, 180, 270 deg rotation is available\n");
2995 case QEMU_OPTION_kernel
:
2996 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2998 case QEMU_OPTION_initrd
:
2999 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3001 case QEMU_OPTION_append
:
3002 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3004 case QEMU_OPTION_dtb
:
3005 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3007 case QEMU_OPTION_cdrom
:
3008 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3010 case QEMU_OPTION_boot
:
3011 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3016 case QEMU_OPTION_fda
:
3017 case QEMU_OPTION_fdb
:
3018 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3021 case QEMU_OPTION_no_fd_bootchk
:
3024 case QEMU_OPTION_netdev
:
3025 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3029 case QEMU_OPTION_net
:
3030 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3034 #ifdef CONFIG_LIBISCSI
3035 case QEMU_OPTION_iscsi
:
3036 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3043 case QEMU_OPTION_tftp
:
3044 legacy_tftp_prefix
= optarg
;
3046 case QEMU_OPTION_bootp
:
3047 legacy_bootp_filename
= optarg
;
3049 case QEMU_OPTION_redir
:
3050 if (net_slirp_redir(optarg
) < 0)
3054 case QEMU_OPTION_bt
:
3055 add_device_config(DEV_BT
, optarg
);
3057 case QEMU_OPTION_audio_help
:
3061 case QEMU_OPTION_soundhw
:
3062 select_soundhw (optarg
);
3067 case QEMU_OPTION_version
:
3071 case QEMU_OPTION_m
: {
3073 const char *mem_str
;
3074 const char *maxmem_str
, *slots_str
;
3076 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3082 mem_str
= qemu_opt_get(opts
, "size");
3084 error_report("invalid -m option, missing 'size' option");
3088 error_report("missing 'size' option value");
3092 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3094 /* Fix up legacy suffix-less format */
3095 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3096 uint64_t overflow_check
= sz
;
3099 if ((sz
>> 20) != overflow_check
) {
3100 error_report("too large 'size' option value");
3105 /* backward compatibility behaviour for case "-m 0" */
3107 sz
= default_ram_size
;
3110 sz
= QEMU_ALIGN_UP(sz
, 8192);
3112 if (ram_size
!= sz
) {
3113 error_report("ram size too large");
3116 maxram_size
= ram_size
;
3118 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3119 slots_str
= qemu_opt_get(opts
, "slots");
3120 if (maxmem_str
&& slots_str
) {
3123 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3124 if (sz
< ram_size
) {
3125 error_report("invalid -m option value: maxmem "
3126 "(0x%" PRIx64
") <= initial memory (0x"
3127 RAM_ADDR_FMT
")", sz
, ram_size
);
3131 slots
= qemu_opt_get_number(opts
, "slots", 0);
3132 if ((sz
> ram_size
) && !slots
) {
3133 error_report("invalid -m option value: maxmem "
3134 "(0x%" PRIx64
") more than initial memory (0x"
3135 RAM_ADDR_FMT
") but no hotplug slots where "
3136 "specified", sz
, ram_size
);
3140 if ((sz
<= ram_size
) && slots
) {
3141 error_report("invalid -m option value: %"
3142 PRIu64
" hotplug slots where specified but "
3143 "maxmem (0x%" PRIx64
") <= initial memory (0x"
3144 RAM_ADDR_FMT
")", slots
, sz
, ram_size
);
3149 } else if ((!maxmem_str
&& slots_str
) ||
3150 (maxmem_str
&& !slots_str
)) {
3151 error_report("invalid -m option value: missing "
3152 "'%s' option", slots_str
? "maxmem" : "slots");
3158 case QEMU_OPTION_tpmdev
:
3159 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3164 case QEMU_OPTION_mempath
:
3167 case QEMU_OPTION_mem_prealloc
:
3177 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3179 case QEMU_OPTION_gdb
:
3180 add_device_config(DEV_GDB
, optarg
);
3183 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3184 data_dir
[data_dir_idx
++] = optarg
;
3187 case QEMU_OPTION_bios
:
3188 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3190 case QEMU_OPTION_singlestep
:
3197 keyboard_layout
= optarg
;
3199 case QEMU_OPTION_localtime
:
3202 case QEMU_OPTION_vga
:
3211 w
= strtol(p
, (char **)&p
, 10);
3214 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3220 h
= strtol(p
, (char **)&p
, 10);
3225 depth
= strtol(p
, (char **)&p
, 10);
3226 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3227 depth
!= 24 && depth
!= 32)
3229 } else if (*p
== '\0') {
3230 depth
= graphic_depth
;
3237 graphic_depth
= depth
;
3240 case QEMU_OPTION_echr
:
3243 term_escape_char
= strtol(optarg
, &r
, 0);
3245 printf("Bad argument to echr\n");
3248 case QEMU_OPTION_monitor
:
3249 default_monitor
= 0;
3250 if (strncmp(optarg
, "none", 4)) {
3251 monitor_parse(optarg
, "readline", false);
3254 case QEMU_OPTION_qmp
:
3255 monitor_parse(optarg
, "control", false);
3256 default_monitor
= 0;
3258 case QEMU_OPTION_qmp_pretty
:
3259 monitor_parse(optarg
, "control", true);
3260 default_monitor
= 0;
3262 case QEMU_OPTION_mon
:
3263 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3267 default_monitor
= 0;
3269 case QEMU_OPTION_chardev
:
3270 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3275 case QEMU_OPTION_fsdev
:
3276 olist
= qemu_find_opts("fsdev");
3278 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3281 opts
= qemu_opts_parse(olist
, optarg
, 1);
3286 case QEMU_OPTION_virtfs
: {
3289 const char *writeout
, *sock_fd
, *socket
;
3291 olist
= qemu_find_opts("virtfs");
3293 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3296 opts
= qemu_opts_parse(olist
, optarg
, 1);
3301 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3302 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3303 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3306 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3307 qemu_opt_get(opts
, "mount_tag"),
3310 fprintf(stderr
, "duplicate fsdev id: %s\n",
3311 qemu_opt_get(opts
, "mount_tag"));
3315 writeout
= qemu_opt_get(opts
, "writeout");
3317 #ifdef CONFIG_SYNC_FILE_RANGE
3318 qemu_opt_set(fsdev
, "writeout", writeout
);
3320 fprintf(stderr
, "writeout=immediate not supported on "
3325 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3326 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3327 qemu_opt_set(fsdev
, "security_model",
3328 qemu_opt_get(opts
, "security_model"));
3329 socket
= qemu_opt_get(opts
, "socket");
3331 qemu_opt_set(fsdev
, "socket", socket
);
3333 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3335 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3338 qemu_opt_set_bool(fsdev
, "readonly",
3339 qemu_opt_get_bool(opts
, "readonly", 0));
3340 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3342 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3343 qemu_opt_set(device
, "fsdev",
3344 qemu_opt_get(opts
, "mount_tag"));
3345 qemu_opt_set(device
, "mount_tag",
3346 qemu_opt_get(opts
, "mount_tag"));
3349 case QEMU_OPTION_virtfs_synth
: {
3353 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3356 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3359 qemu_opt_set(fsdev
, "fsdriver", "synth");
3361 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3363 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3364 qemu_opt_set(device
, "fsdev", "v_synth");
3365 qemu_opt_set(device
, "mount_tag", "v_synth");
3368 case QEMU_OPTION_serial
:
3369 add_device_config(DEV_SERIAL
, optarg
);
3371 if (strncmp(optarg
, "mon:", 4) == 0) {
3372 default_monitor
= 0;
3375 case QEMU_OPTION_watchdog
:
3378 "qemu: only one watchdog option may be given\n");
3383 case QEMU_OPTION_watchdog_action
:
3384 if (select_watchdog_action(optarg
) == -1) {
3385 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3389 case QEMU_OPTION_virtiocon
:
3390 add_device_config(DEV_VIRTCON
, optarg
);
3391 default_virtcon
= 0;
3392 if (strncmp(optarg
, "mon:", 4) == 0) {
3393 default_monitor
= 0;
3396 case QEMU_OPTION_parallel
:
3397 add_device_config(DEV_PARALLEL
, optarg
);
3398 default_parallel
= 0;
3399 if (strncmp(optarg
, "mon:", 4) == 0) {
3400 default_monitor
= 0;
3403 case QEMU_OPTION_debugcon
:
3404 add_device_config(DEV_DEBUGCON
, optarg
);
3406 case QEMU_OPTION_loadvm
:
3409 case QEMU_OPTION_full_screen
:
3412 case QEMU_OPTION_no_frame
:
3415 case QEMU_OPTION_alt_grab
:
3418 case QEMU_OPTION_ctrl_grab
:
3421 case QEMU_OPTION_no_quit
:
3424 case QEMU_OPTION_sdl
:
3426 display_type
= DT_SDL
;
3429 fprintf(stderr
, "SDL support is disabled\n");
3432 case QEMU_OPTION_pidfile
:
3435 case QEMU_OPTION_win2k_hack
:
3436 win2k_install_hack
= 1;
3438 case QEMU_OPTION_rtc_td_hack
: {
3439 static GlobalProperty slew_lost_ticks
[] = {
3441 .driver
= "mc146818rtc",
3442 .property
= "lost_tick_policy",
3445 { /* end of list */ }
3448 qdev_prop_register_global_list(slew_lost_ticks
);
3451 case QEMU_OPTION_acpitable
:
3452 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3456 do_acpitable_option(opts
);
3458 case QEMU_OPTION_smbios
:
3459 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3463 do_smbios_option(opts
);
3465 case QEMU_OPTION_enable_kvm
:
3466 olist
= qemu_find_opts("machine");
3467 qemu_opts_parse(olist
, "accel=kvm", 0);
3469 case QEMU_OPTION_machine
:
3470 olist
= qemu_find_opts("machine");
3471 opts
= qemu_opts_parse(olist
, optarg
, 1);
3475 optarg
= qemu_opt_get(opts
, "type");
3477 machine_class
= machine_parse(optarg
);
3480 case QEMU_OPTION_no_kvm
:
3481 olist
= qemu_find_opts("machine");
3482 qemu_opts_parse(olist
, "accel=tcg", 0);
3484 case QEMU_OPTION_no_kvm_pit
: {
3485 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3489 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3490 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3492 .driver
= "kvm-pit",
3493 .property
= "lost_tick_policy",
3496 { /* end of list */ }
3499 fprintf(stderr
, "Warning: option deprecated, use "
3500 "lost_tick_policy property of kvm-pit instead.\n");
3501 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3504 case QEMU_OPTION_usb
:
3505 olist
= qemu_find_opts("machine");
3506 qemu_opts_parse(olist
, "usb=on", 0);
3508 case QEMU_OPTION_usbdevice
:
3509 olist
= qemu_find_opts("machine");
3510 qemu_opts_parse(olist
, "usb=on", 0);
3511 add_device_config(DEV_USB
, optarg
);
3513 case QEMU_OPTION_device
:
3514 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3518 case QEMU_OPTION_smp
:
3519 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3523 case QEMU_OPTION_vnc
:
3526 vnc_display
= optarg
;
3528 fprintf(stderr
, "VNC support is disabled\n");
3532 case QEMU_OPTION_no_acpi
:
3535 case QEMU_OPTION_no_hpet
:
3538 case QEMU_OPTION_balloon
:
3539 if (balloon_parse(optarg
) < 0) {
3540 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3544 case QEMU_OPTION_no_reboot
:
3547 case QEMU_OPTION_no_shutdown
:
3550 case QEMU_OPTION_show_cursor
:
3553 case QEMU_OPTION_uuid
:
3554 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3555 fprintf(stderr
, "Fail to parse UUID string."
3556 " Wrong format.\n");
3559 qemu_uuid_set
= true;
3561 case QEMU_OPTION_option_rom
:
3562 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3563 fprintf(stderr
, "Too many option ROMs\n");
3566 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3570 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3571 option_rom
[nb_option_roms
].bootindex
=
3572 qemu_opt_get_number(opts
, "bootindex", -1);
3573 if (!option_rom
[nb_option_roms
].name
) {
3574 fprintf(stderr
, "Option ROM file is not specified\n");
3579 case QEMU_OPTION_semihosting
:
3580 semihosting_enabled
= 1;
3581 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3583 case QEMU_OPTION_semihosting_config
:
3584 semihosting_enabled
= 1;
3585 opts
= qemu_opts_parse(qemu_find_opts("semihosting-config"),
3588 semihosting_enabled
= qemu_opt_get_bool(opts
, "enable",
3590 const char *target
= qemu_opt_get(opts
, "target");
3591 if (target
!= NULL
) {
3592 if (strcmp("native", target
) == 0) {
3593 semihosting_target
= SEMIHOSTING_TARGET_NATIVE
;
3594 } else if (strcmp("gdb", target
) == 0) {
3595 semihosting_target
= SEMIHOSTING_TARGET_GDB
;
3596 } else if (strcmp("auto", target
) == 0) {
3597 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3599 fprintf(stderr
, "Unsupported semihosting-config"
3605 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3608 fprintf(stderr
, "Unsupported semihosting-config %s\n",
3613 case QEMU_OPTION_tdf
:
3614 fprintf(stderr
, "Warning: user space PIT time drift fix "
3615 "is no longer supported.\n");
3617 case QEMU_OPTION_name
:
3618 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3623 case QEMU_OPTION_prom_env
:
3624 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3625 fprintf(stderr
, "Too many prom variables\n");
3628 prom_envs
[nb_prom_envs
] = optarg
;
3631 case QEMU_OPTION_old_param
:
3634 case QEMU_OPTION_clock
:
3635 /* Clock options no longer exist. Keep this option for
3636 * backward compatibility.
3639 case QEMU_OPTION_startdate
:
3640 configure_rtc_date_offset(optarg
, 1);
3642 case QEMU_OPTION_rtc
:
3643 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3647 configure_rtc(opts
);
3649 case QEMU_OPTION_tb_size
:
3650 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3651 if (tcg_tb_size
< 0) {
3655 case QEMU_OPTION_icount
:
3656 icount_opts
= qemu_opts_parse(qemu_find_opts("icount"),
3662 case QEMU_OPTION_incoming
:
3664 runstate_set(RUN_STATE_INMIGRATE
);
3666 case QEMU_OPTION_nodefaults
:
3669 case QEMU_OPTION_xen_domid
:
3670 if (!(xen_available())) {
3671 printf("Option %s not supported for this target\n", popt
->name
);
3674 xen_domid
= atoi(optarg
);
3676 case QEMU_OPTION_xen_create
:
3677 if (!(xen_available())) {
3678 printf("Option %s not supported for this target\n", popt
->name
);
3681 xen_mode
= XEN_CREATE
;
3683 case QEMU_OPTION_xen_attach
:
3684 if (!(xen_available())) {
3685 printf("Option %s not supported for this target\n", popt
->name
);
3688 xen_mode
= XEN_ATTACH
;
3690 case QEMU_OPTION_trace
:
3692 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3696 trace_events
= qemu_opt_get(opts
, "events");
3697 trace_file
= qemu_opt_get(opts
, "file");
3700 case QEMU_OPTION_readconfig
:
3702 int ret
= qemu_read_config_file(optarg
);
3704 fprintf(stderr
, "read config %s: %s\n", optarg
,
3710 case QEMU_OPTION_spice
:
3711 olist
= qemu_find_opts("spice");
3713 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3716 opts
= qemu_opts_parse(olist
, optarg
, 0);
3722 case QEMU_OPTION_writeconfig
:
3725 if (strcmp(optarg
, "-") == 0) {
3728 fp
= fopen(optarg
, "w");
3730 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3734 qemu_config_write(fp
);
3740 case QEMU_OPTION_qtest
:
3741 qtest_chrdev
= optarg
;
3743 case QEMU_OPTION_qtest_log
:
3746 case QEMU_OPTION_sandbox
:
3747 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3752 case QEMU_OPTION_add_fd
:
3754 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3759 error_report("File descriptor passing is disabled on this "
3764 case QEMU_OPTION_object
:
3765 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3770 case QEMU_OPTION_realtime
:
3771 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3775 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
3777 case QEMU_OPTION_msg
:
3778 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3782 configure_msg(opts
);
3784 case QEMU_OPTION_dump_vmstate
:
3785 if (vmstate_dump_file
) {
3786 fprintf(stderr
, "qemu: only one '-dump-vmstate' "
3787 "option may be given\n");
3790 vmstate_dump_file
= fopen(optarg
, "w");
3791 if (vmstate_dump_file
== NULL
) {
3792 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3797 os_parse_cmd_args(popt
->index
, optarg
);
3805 if (qemu_init_main_loop(&main_loop_err
)) {
3806 error_report("%s", error_get_pretty(main_loop_err
));
3810 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3814 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3819 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3823 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3828 if (machine_class
== NULL
) {
3829 fprintf(stderr
, "No machine specified, and there is no default.\n"
3830 "Use -machine help to list supported machines!\n");
3834 current_machine
= MACHINE(object_new(object_class_get_name(
3835 OBJECT_CLASS(machine_class
))));
3836 object_property_add_child(object_get_root(), "machine",
3837 OBJECT(current_machine
), &error_abort
);
3838 cpu_exec_init_all();
3840 if (machine_class
->hw_version
) {
3841 qemu_set_version(machine_class
->hw_version
);
3844 /* Init CPU def lists, based on config
3845 * - Must be called after all the qemu_read_config_file() calls
3846 * - Must be called before list_cpus()
3847 * - Must be called before machine->init()
3851 if (cpu_model
&& is_help_option(cpu_model
)) {
3852 list_cpus(stdout
, &fprintf
, cpu_model
);
3856 /* Open the logfile at this point, if necessary. We can't open the logfile
3857 * when encountering either of the logging options (-d or -D) because the
3858 * other one may be encountered later on the command line, changing the
3859 * location or level of logging.
3864 qemu_set_log_filename(log_file
);
3867 mask
= qemu_str_to_log_mask(log_mask
);
3869 qemu_print_log_usage(stdout
);
3875 if (!is_daemonized()) {
3876 if (!trace_init_backends(trace_events
, trace_file
)) {
3881 /* If no data_dir is specified then try to find it relative to the
3883 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3884 data_dir
[data_dir_idx
] = os_find_datadir();
3885 if (data_dir
[data_dir_idx
] != NULL
) {
3889 /* If all else fails use the install path specified when building. */
3890 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3891 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3894 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3896 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
3897 if (smp_cpus
> machine_class
->max_cpus
) {
3898 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3899 "supported by machine `%s' (%d)\n", smp_cpus
,
3900 machine_class
->name
, machine_class
->max_cpus
);
3905 * Get the default machine options from the machine if it is not already
3906 * specified either by the configuration file or by the command line.
3908 if (machine_class
->default_machine_opts
) {
3909 qemu_opts_set_defaults(qemu_find_opts("machine"),
3910 machine_class
->default_machine_opts
, 0);
3913 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3914 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3916 if (!vga_model
&& !default_vga
) {
3917 vga_interface_type
= VGA_DEVICE
;
3919 if (!has_defaults
|| machine_class
->no_serial
) {
3922 if (!has_defaults
|| machine_class
->no_parallel
) {
3923 default_parallel
= 0;
3925 if (!has_defaults
|| !machine_class
->use_virtcon
) {
3926 default_virtcon
= 0;
3928 if (!has_defaults
|| !machine_class
->use_sclp
) {
3931 if (!has_defaults
|| machine_class
->no_floppy
) {
3934 if (!has_defaults
|| machine_class
->no_cdrom
) {
3937 if (!has_defaults
|| machine_class
->no_sdcard
) {
3940 if (!has_defaults
) {
3941 default_monitor
= 0;
3946 if (is_daemonized()) {
3947 /* According to documentation and historically, -nographic redirects
3948 * serial port, parallel port and monitor to stdio, which does not work
3949 * with -daemonize. We can redirect these to null instead, but since
3950 * -nographic is legacy, let's just error out.
3951 * We disallow -nographic only if all other ports are not redirected
3952 * explicitly, to not break existing legacy setups which uses
3953 * -nographic _and_ redirects all ports explicitly - this is valid
3954 * usage, -nographic is just a no-op in this case.
3956 if (display_type
== DT_NOGRAPHIC
3957 && (default_parallel
|| default_serial
3958 || default_monitor
|| default_virtcon
)) {
3959 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3962 #ifdef CONFIG_CURSES
3963 if (display_type
== DT_CURSES
) {
3964 fprintf(stderr
, "curses display can not be used with -daemonize\n");
3970 if (display_type
== DT_NOGRAPHIC
) {
3971 if (default_parallel
)
3972 add_device_config(DEV_PARALLEL
, "null");
3973 if (default_serial
&& default_monitor
) {
3974 add_device_config(DEV_SERIAL
, "mon:stdio");
3975 } else if (default_virtcon
&& default_monitor
) {
3976 add_device_config(DEV_VIRTCON
, "mon:stdio");
3977 } else if (default_sclp
&& default_monitor
) {
3978 add_device_config(DEV_SCLP
, "mon:stdio");
3981 add_device_config(DEV_SERIAL
, "stdio");
3982 if (default_virtcon
)
3983 add_device_config(DEV_VIRTCON
, "stdio");
3985 add_device_config(DEV_SCLP
, "stdio");
3987 if (default_monitor
)
3988 monitor_parse("stdio", "readline", false);
3992 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3993 if (default_parallel
)
3994 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3995 if (default_monitor
)
3996 monitor_parse("vc:80Cx24C", "readline", false);
3997 if (default_virtcon
)
3998 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4000 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4004 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4005 #if defined(CONFIG_GTK)
4006 display_type
= DT_GTK
;
4007 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4008 display_type
= DT_SDL
;
4009 #elif defined(CONFIG_VNC)
4010 vnc_display
= "localhost:0,to=99";
4013 display_type
= DT_NONE
;
4017 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4018 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4019 "for SDL, ignoring option\n");
4021 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4022 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4023 "ignoring option\n");
4026 #if defined(CONFIG_GTK)
4027 if (display_type
== DT_GTK
) {
4028 early_gtk_display_init();
4034 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4036 #ifdef CONFIG_VIRTFS
4037 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4042 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4043 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4047 /* store value for the future use */
4048 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4050 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4055 if (qemu_opts_foreach(qemu_find_opts("object"),
4056 object_create
, NULL
, 0) != 0) {
4060 machine_opts
= qemu_get_machine_opts();
4061 if (qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4063 object_unref(OBJECT(current_machine
));
4067 configure_accelerator(current_machine
);
4070 Error
*local_err
= NULL
;
4071 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4073 error_report("%s", error_get_pretty(local_err
));
4074 error_free(local_err
);
4079 machine_opts
= qemu_get_machine_opts();
4080 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4081 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4082 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4083 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4085 boot_order
= machine_class
->default_boot_order
;
4086 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4088 char *normal_boot_order
;
4089 const char *order
, *once
;
4090 Error
*local_err
= NULL
;
4092 order
= qemu_opt_get(opts
, "order");
4094 validate_bootdevices(order
, &local_err
);
4096 error_report("%s", error_get_pretty(local_err
));
4102 once
= qemu_opt_get(opts
, "once");
4104 validate_bootdevices(once
, &local_err
);
4106 error_report("%s", error_get_pretty(local_err
));
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
)) {