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"
65 #include "hw/pcmcia.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 "qemu/cache-utils.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
101 #include "fsdev/qemu-fsdev.h"
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
121 #define DEFAULT_RAM_SIZE 128
123 #define MAX_VIRTIO_CONSOLES 1
124 #define MAX_SCLP_CONSOLES 1
126 static const char *data_dir
[16];
127 static int data_dir_idx
;
128 const char *bios_name
= NULL
;
129 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
130 DisplayType display_type
= DT_DEFAULT
;
131 static int display_remote
;
132 const char* keyboard_layout
= NULL
;
134 const char *mem_path
= NULL
;
135 int mem_prealloc
= 0; /* force preallocation of physical target memory */
137 NICInfo nd_table
[MAX_NICS
];
139 static int rtc_utc
= 1;
140 static int rtc_date_offset
= -1; /* -1 means no change */
141 QEMUClockType rtc_clock
;
142 int vga_interface_type
= VGA_NONE
;
143 static int full_screen
= 0;
144 static int no_frame
= 0;
147 static bool grab_on_hover
;
149 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
150 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
151 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
152 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
153 int win2k_install_hack
= 0;
160 const char *vnc_display
;
162 int acpi_enabled
= 1;
165 static int no_reboot
;
168 int graphic_rotate
= 0;
169 const char *watchdog
;
170 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
172 int semihosting_enabled
= 0;
174 const char *qemu_name
;
177 unsigned int nb_prom_envs
= 0;
178 const char *prom_envs
[MAX_PROM_ENVS
];
180 static bool boot_strict
;
181 uint8_t *boot_splash_filedata
;
182 size_t boot_splash_filedata_size
;
183 uint8_t qemu_extra_params_fw
[2];
185 typedef struct FWBootEntry FWBootEntry
;
188 QTAILQ_ENTRY(FWBootEntry
) link
;
194 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
195 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
198 NodeInfo numa_info
[MAX_NODES
];
200 uint8_t qemu_uuid
[16];
203 static QEMUBootSetHandler
*boot_set_handler
;
204 static void *boot_set_opaque
;
206 static NotifierList exit_notifiers
=
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
209 static NotifierList machine_init_done_notifiers
=
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
212 static bool tcg_allowed
= true;
215 enum xen_mode xen_mode
= XEN_EMULATE
;
216 static int tcg_tb_size
;
218 static int has_defaults
= 1;
219 static int default_serial
= 1;
220 static int default_parallel
= 1;
221 static int default_virtcon
= 1;
222 static int default_sclp
= 1;
223 static int default_monitor
= 1;
224 static int default_floppy
= 1;
225 static int default_cdrom
= 1;
226 static int default_sdcard
= 1;
227 static int default_vga
= 1;
233 { .driver
= "isa-serial", .flag
= &default_serial
},
234 { .driver
= "isa-parallel", .flag
= &default_parallel
},
235 { .driver
= "isa-fdc", .flag
= &default_floppy
},
236 { .driver
= "ide-cd", .flag
= &default_cdrom
},
237 { .driver
= "ide-hd", .flag
= &default_cdrom
},
238 { .driver
= "ide-drive", .flag
= &default_cdrom
},
239 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
240 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
241 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
242 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
243 { .driver
= "VGA", .flag
= &default_vga
},
244 { .driver
= "isa-vga", .flag
= &default_vga
},
245 { .driver
= "cirrus-vga", .flag
= &default_vga
},
246 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
247 { .driver
= "vmware-svga", .flag
= &default_vga
},
248 { .driver
= "qxl-vga", .flag
= &default_vga
},
251 static QemuOptsList qemu_rtc_opts
= {
253 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_STRING
,
263 .type
= QEMU_OPT_STRING
,
265 { /* end of list */ }
269 static QemuOptsList qemu_sandbox_opts
= {
271 .implied_opt_name
= "enable",
272 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
276 .type
= QEMU_OPT_BOOL
,
278 { /* end of list */ }
282 static QemuOptsList qemu_trace_opts
= {
284 .implied_opt_name
= "trace",
285 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
289 .type
= QEMU_OPT_STRING
,
292 .type
= QEMU_OPT_STRING
,
294 { /* end of list */ }
298 static QemuOptsList qemu_option_rom_opts
= {
299 .name
= "option-rom",
300 .implied_opt_name
= "romfile",
301 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
305 .type
= QEMU_OPT_NUMBER
,
308 .type
= QEMU_OPT_STRING
,
310 { /* end of list */ }
314 static QemuOptsList qemu_machine_opts
= {
316 .implied_opt_name
= "type",
318 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
322 .type
= QEMU_OPT_STRING
,
323 .help
= "emulated machine"
326 .type
= QEMU_OPT_STRING
,
327 .help
= "accelerator list",
329 .name
= "kernel_irqchip",
330 .type
= QEMU_OPT_BOOL
,
331 .help
= "use KVM in-kernel irqchip",
333 .name
= "kvm_shadow_mem",
334 .type
= QEMU_OPT_SIZE
,
335 .help
= "KVM shadow MMU size",
338 .type
= QEMU_OPT_STRING
,
339 .help
= "Linux kernel image file",
342 .type
= QEMU_OPT_STRING
,
343 .help
= "Linux initial ramdisk file",
346 .type
= QEMU_OPT_STRING
,
347 .help
= "Linux kernel command line",
350 .type
= QEMU_OPT_STRING
,
351 .help
= "Linux kernel device tree file",
354 .type
= QEMU_OPT_STRING
,
355 .help
= "Dump current dtb to a file and quit",
357 .name
= "phandle_start",
358 .type
= QEMU_OPT_NUMBER
,
359 .help
= "The first phandle ID we may generate dynamically",
361 .name
= "dt_compatible",
362 .type
= QEMU_OPT_STRING
,
363 .help
= "Overrides the \"compatible\" property of the dt root node",
365 .name
= "dump-guest-core",
366 .type
= QEMU_OPT_BOOL
,
367 .help
= "Include guest memory in a core dump",
370 .type
= QEMU_OPT_BOOL
,
371 .help
= "enable/disable memory merge support",
374 .type
= QEMU_OPT_BOOL
,
375 .help
= "Set on/off to enable/disable usb",
378 .type
= QEMU_OPT_STRING
,
379 .help
= "firmware image",
382 .type
= QEMU_OPT_STRING
,
383 .help
= "Specifies the KVM virtualization mode (HV, PR)",
385 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
386 .type
= QEMU_OPT_SIZE
,
387 .help
= "maximum ram below the 4G boundary (32bit boundary)",
389 { /* End of list */ }
393 static QemuOptsList qemu_boot_opts
= {
395 .implied_opt_name
= "order",
397 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
401 .type
= QEMU_OPT_STRING
,
404 .type
= QEMU_OPT_STRING
,
407 .type
= QEMU_OPT_BOOL
,
410 .type
= QEMU_OPT_STRING
,
412 .name
= "splash-time",
413 .type
= QEMU_OPT_STRING
,
415 .name
= "reboot-timeout",
416 .type
= QEMU_OPT_STRING
,
419 .type
= QEMU_OPT_BOOL
,
425 static QemuOptsList qemu_add_fd_opts
= {
427 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
431 .type
= QEMU_OPT_NUMBER
,
432 .help
= "file descriptor of which a duplicate is added to fd set",
435 .type
= QEMU_OPT_NUMBER
,
436 .help
= "ID of the fd set to add fd to",
439 .type
= QEMU_OPT_STRING
,
440 .help
= "free-form string used to describe fd",
442 { /* end of list */ }
446 static QemuOptsList qemu_object_opts
= {
448 .implied_opt_name
= "qom-type",
449 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
455 static QemuOptsList qemu_tpmdev_opts
= {
457 .implied_opt_name
= "type",
458 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
460 /* options are defined in the TPM backends */
461 { /* end of list */ }
465 static QemuOptsList qemu_realtime_opts
= {
467 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
471 .type
= QEMU_OPT_BOOL
,
473 { /* end of list */ }
477 static QemuOptsList qemu_msg_opts
= {
479 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
483 .type
= QEMU_OPT_BOOL
,
485 { /* end of list */ }
489 static QemuOptsList qemu_name_opts
= {
491 .implied_opt_name
= "guest",
493 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
497 .type
= QEMU_OPT_STRING
,
498 .help
= "Sets the name of the guest.\n"
499 "This name will be displayed in the SDL window caption.\n"
500 "The name will also be used for the VNC server",
503 .type
= QEMU_OPT_STRING
,
504 .help
= "Sets the name of the QEMU process, as shown in top etc",
506 .name
= "debug-threads",
507 .type
= QEMU_OPT_BOOL
,
508 .help
= "When enabled, name the individual threads; defaults off.\n"
509 "NOTE: The thread names are for debugging and not a\n"
512 { /* End of list */ }
516 static QemuOptsList qemu_mem_opts
= {
518 .implied_opt_name
= "size",
519 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
524 .type
= QEMU_OPT_SIZE
,
528 .type
= QEMU_OPT_NUMBER
,
532 .type
= QEMU_OPT_SIZE
,
534 { /* end of list */ }
539 * Get machine options
541 * Returns: machine options (never null).
543 QemuOpts
*qemu_get_machine_opts(void)
545 return qemu_find_opts_singleton("machine");
548 const char *qemu_get_vm_name(void)
553 static void res_free(void)
555 if (boot_splash_filedata
!= NULL
) {
556 g_free(boot_splash_filedata
);
557 boot_splash_filedata
= NULL
;
561 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
563 const char *driver
= qemu_opt_get(opts
, "driver");
568 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
569 if (strcmp(default_list
[i
].driver
, driver
) != 0)
571 *(default_list
[i
].flag
) = 0;
576 /***********************************************************/
579 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
581 /* We use RUN_STATE_MAX but any invalid value will do */
582 static RunState vmstop_requested
= RUN_STATE_MAX
;
583 static QemuMutex vmstop_lock
;
588 } RunStateTransition
;
590 static const RunStateTransition runstate_transitions_def
[] = {
592 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
593 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
595 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
596 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
598 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
599 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
601 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
602 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
604 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
605 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
607 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
608 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
610 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
611 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
612 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
614 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
615 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
617 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
619 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
620 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
621 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
622 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
623 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
624 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
625 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
626 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
627 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
628 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
630 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
632 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
633 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
635 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
636 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
637 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
638 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
640 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
641 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
643 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
644 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
646 { RUN_STATE_MAX
, RUN_STATE_MAX
},
649 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
651 bool runstate_check(RunState state
)
653 return current_run_state
== state
;
656 static void runstate_init(void)
658 const RunStateTransition
*p
;
660 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
661 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
662 runstate_valid_transitions
[p
->from
][p
->to
] = true;
665 qemu_mutex_init(&vmstop_lock
);
668 /* This function will abort() on invalid state transitions */
669 void runstate_set(RunState new_state
)
671 assert(new_state
< RUN_STATE_MAX
);
673 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
674 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
675 RunState_lookup
[current_run_state
],
676 RunState_lookup
[new_state
]);
679 trace_runstate_set(new_state
);
680 current_run_state
= new_state
;
683 int runstate_is_running(void)
685 return runstate_check(RUN_STATE_RUNNING
);
688 bool runstate_needs_reset(void)
690 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
691 runstate_check(RUN_STATE_SHUTDOWN
);
694 StatusInfo
*qmp_query_status(Error
**errp
)
696 StatusInfo
*info
= g_malloc0(sizeof(*info
));
698 info
->running
= runstate_is_running();
699 info
->singlestep
= singlestep
;
700 info
->status
= current_run_state
;
705 static bool qemu_vmstop_requested(RunState
*r
)
707 qemu_mutex_lock(&vmstop_lock
);
708 *r
= vmstop_requested
;
709 vmstop_requested
= RUN_STATE_MAX
;
710 qemu_mutex_unlock(&vmstop_lock
);
711 return *r
< RUN_STATE_MAX
;
714 void qemu_system_vmstop_request_prepare(void)
716 qemu_mutex_lock(&vmstop_lock
);
719 void qemu_system_vmstop_request(RunState state
)
721 vmstop_requested
= state
;
722 qemu_mutex_unlock(&vmstop_lock
);
730 qemu_vmstop_requested(&requested
);
731 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
735 /* Ensure that a STOP/RESUME pair of events is emitted if a
736 * vmstop request was pending. The BLOCK_IO_ERROR event, for
737 * example, according to documentation is always followed by
740 if (runstate_is_running()) {
741 monitor_protocol_event(QEVENT_STOP
, NULL
);
744 runstate_set(RUN_STATE_RUNNING
);
745 vm_state_notify(1, RUN_STATE_RUNNING
);
749 monitor_protocol_event(QEVENT_RESUME
, NULL
);
753 /***********************************************************/
754 /* real time host monotonic timer */
756 /***********************************************************/
757 /* host time/date access */
758 void qemu_get_timedate(struct tm
*tm
, int offset
)
764 if (rtc_date_offset
== -1) {
768 localtime_r(&ti
, tm
);
770 ti
-= rtc_date_offset
;
775 int qemu_timedate_diff(struct tm
*tm
)
779 if (rtc_date_offset
== -1)
781 seconds
= mktimegm(tm
);
784 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
785 seconds
= mktime(&tmp
);
788 seconds
= mktimegm(tm
) + rtc_date_offset
;
790 return seconds
- time(NULL
);
793 void rtc_change_mon_event(struct tm
*tm
)
797 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
798 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
799 qobject_decref(data
);
802 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
804 time_t rtc_start_date
;
807 if (!strcmp(startdate
, "now") && legacy
) {
808 rtc_date_offset
= -1;
810 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
818 } else if (sscanf(startdate
, "%d-%d-%d",
830 rtc_start_date
= mktimegm(&tm
);
831 if (rtc_start_date
== -1) {
833 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
834 "'2006-06-17T16:01:21' or '2006-06-17'\n");
837 rtc_date_offset
= time(NULL
) - rtc_start_date
;
841 static void configure_rtc(QemuOpts
*opts
)
845 value
= qemu_opt_get(opts
, "base");
847 if (!strcmp(value
, "utc")) {
849 } else if (!strcmp(value
, "localtime")) {
852 configure_rtc_date_offset(value
, 0);
855 value
= qemu_opt_get(opts
, "clock");
857 if (!strcmp(value
, "host")) {
858 rtc_clock
= QEMU_CLOCK_HOST
;
859 } else if (!strcmp(value
, "rt")) {
860 rtc_clock
= QEMU_CLOCK_REALTIME
;
861 } else if (!strcmp(value
, "vm")) {
862 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
864 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
868 value
= qemu_opt_get(opts
, "driftfix");
870 if (!strcmp(value
, "slew")) {
871 static GlobalProperty slew_lost_ticks
[] = {
873 .driver
= "mc146818rtc",
874 .property
= "lost_tick_policy",
877 { /* end of list */ }
880 qdev_prop_register_global_list(slew_lost_ticks
);
881 } else if (!strcmp(value
, "none")) {
882 /* discard is default */
884 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
890 /***********************************************************/
891 /* Bluetooth support */
894 static struct HCIInfo
*hci_table
[MAX_NICS
];
896 struct HCIInfo
*qemu_next_hci(void)
898 if (cur_hci
== nb_hcis
)
901 return hci_table
[cur_hci
++];
904 static int bt_hci_parse(const char *str
)
909 if (nb_hcis
>= MAX_NICS
) {
910 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
923 bdaddr
.b
[5] = 0x56 + nb_hcis
;
924 hci
->bdaddr_set(hci
, bdaddr
.b
);
926 hci_table
[nb_hcis
++] = hci
;
931 static void bt_vhci_add(int vlan_id
)
933 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
936 fprintf(stderr
, "qemu: warning: adding a VHCI to "
937 "an empty scatternet %i\n", vlan_id
);
939 bt_vhci_init(bt_new_hci(vlan
));
942 static struct bt_device_s
*bt_device_add(const char *opt
)
944 struct bt_scatternet_s
*vlan
;
946 char *endp
= strstr(opt
, ",vlan=");
947 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
950 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
953 vlan_id
= strtol(endp
+ 6, &endp
, 0);
955 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
960 vlan
= qemu_find_bt_vlan(vlan_id
);
963 fprintf(stderr
, "qemu: warning: adding a slave device to "
964 "an empty scatternet %i\n", vlan_id
);
966 if (!strcmp(devname
, "keyboard"))
967 return bt_keyboard_init(vlan
);
969 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
973 static int bt_parse(const char *opt
)
975 const char *endp
, *p
;
978 if (strstart(opt
, "hci", &endp
)) {
979 if (!*endp
|| *endp
== ',') {
981 if (!strstart(endp
, ",vlan=", 0))
984 return bt_hci_parse(opt
);
986 } else if (strstart(opt
, "vhci", &endp
)) {
987 if (!*endp
|| *endp
== ',') {
989 if (strstart(endp
, ",vlan=", &p
)) {
990 vlan
= strtol(p
, (char **) &endp
, 0);
992 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
996 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1005 } else if (strstart(opt
, "device:", &endp
))
1006 return !bt_device_add(endp
);
1008 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1012 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1014 /* FIXME: change this to true for 1.3 */
1015 if (qemu_opt_get_bool(opts
, "enable", false)) {
1016 #ifdef CONFIG_SECCOMP
1017 if (seccomp_start() < 0) {
1018 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1019 "failed to install seccomp syscall filter in the kernel");
1023 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1024 "sandboxing request but seccomp is not compiled into this build");
1032 static int parse_name(QemuOpts
*opts
, void *opaque
)
1034 const char *proc_name
;
1036 if (qemu_opt_get(opts
, "debug-threads")) {
1037 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1039 qemu_name
= qemu_opt_get(opts
, "guest");
1041 proc_name
= qemu_opt_get(opts
, "process");
1043 os_set_proc_name(proc_name
);
1049 bool usb_enabled(bool default_usb
)
1051 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1052 has_defaults
&& default_usb
);
1056 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1058 int fd
, dupfd
, flags
;
1060 const char *fd_opaque
= NULL
;
1062 fd
= qemu_opt_get_number(opts
, "fd", -1);
1063 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1064 fd_opaque
= qemu_opt_get(opts
, "opaque");
1067 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1068 "fd option is required and must be non-negative");
1072 if (fd
<= STDERR_FILENO
) {
1073 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1074 "fd cannot be a standard I/O stream");
1079 * All fds inherited across exec() necessarily have FD_CLOEXEC
1080 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1082 flags
= fcntl(fd
, F_GETFD
);
1083 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1084 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1085 "fd is not valid or already in use");
1090 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1091 "set option is required and must be non-negative");
1095 #ifdef F_DUPFD_CLOEXEC
1096 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1100 qemu_set_cloexec(dupfd
);
1104 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1105 "Error duplicating fd: %s", strerror(errno
));
1109 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1110 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1116 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1120 fd
= qemu_opt_get_number(opts
, "fd", -1);
1127 /***********************************************************/
1128 /* QEMU Block devices */
1130 #define HD_OPTS "media=disk"
1131 #define CDROM_OPTS "media=cdrom"
1133 #define PFLASH_OPTS ""
1137 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1139 BlockInterfaceType
*block_default_type
= opaque
;
1141 return drive_new(opts
, *block_default_type
) == NULL
;
1144 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1146 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1147 qemu_opt_set(opts
, "snapshot", "on");
1152 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1153 int index
, const char *optstr
)
1157 if (!enable
|| drive_get_by_index(type
, index
)) {
1161 opts
= drive_add(type
, index
, NULL
, optstr
);
1163 drive_enable_snapshot(opts
, NULL
);
1165 if (!drive_new(opts
, type
)) {
1170 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1172 boot_set_handler
= func
;
1173 boot_set_opaque
= opaque
;
1176 int qemu_boot_set(const char *boot_order
)
1178 if (!boot_set_handler
) {
1181 return boot_set_handler(boot_set_opaque
, boot_order
);
1184 static void validate_bootdevices(const char *devices
)
1186 /* We just do some generic consistency checks */
1190 for (p
= devices
; *p
!= '\0'; p
++) {
1191 /* Allowed boot devices are:
1192 * a-b: floppy disk drives
1193 * c-f: IDE disk drives
1194 * g-m: machine implementation dependent drives
1195 * n-p: network devices
1196 * It's up to each machine implementation to check if the given boot
1197 * devices match the actual hardware implementation and firmware
1200 if (*p
< 'a' || *p
> 'p') {
1201 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1204 if (bitmap
& (1 << (*p
- 'a'))) {
1205 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1208 bitmap
|= 1 << (*p
- 'a');
1212 static void restore_boot_order(void *opaque
)
1214 char *normal_boot_order
= opaque
;
1215 static int first
= 1;
1217 /* Restore boot order and remove ourselves after the first boot */
1223 qemu_boot_set(normal_boot_order
);
1225 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1226 g_free(normal_boot_order
);
1229 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1232 FWBootEntry
*node
, *i
;
1234 if (bootindex
< 0) {
1238 assert(dev
!= NULL
|| suffix
!= NULL
);
1240 node
= g_malloc0(sizeof(FWBootEntry
));
1241 node
->bootindex
= bootindex
;
1242 node
->suffix
= g_strdup(suffix
);
1245 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1246 if (i
->bootindex
== bootindex
) {
1247 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1249 } else if (i
->bootindex
< bootindex
) {
1252 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1255 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1258 DeviceState
*get_boot_device(uint32_t position
)
1260 uint32_t counter
= 0;
1261 FWBootEntry
*i
= NULL
;
1262 DeviceState
*res
= NULL
;
1264 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1265 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1266 if (counter
== position
) {
1277 * This function returns null terminated string that consist of new line
1278 * separated device paths.
1280 * memory pointed by "size" is assigned total length of the array in bytes
1283 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1289 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1290 char *devpath
= NULL
, *bootpath
;
1294 devpath
= qdev_get_fw_dev_path(i
->dev
);
1298 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1299 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1301 bootpath
= g_malloc(bootpathlen
);
1302 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1304 } else if (devpath
) {
1306 } else if (!ignore_suffixes
) {
1308 bootpath
= g_strdup(i
->suffix
);
1310 bootpath
= g_strdup("");
1314 list
[total
-1] = '\n';
1316 len
= strlen(bootpath
) + 1;
1317 list
= g_realloc(list
, total
+ len
);
1318 memcpy(&list
[total
], bootpath
, len
);
1325 if (boot_strict
&& *size
> 0) {
1326 list
[total
-1] = '\n';
1327 list
= g_realloc(list
, total
+ 5);
1328 memcpy(&list
[total
], "HALT", 5);
1334 static QemuOptsList qemu_smp_opts
= {
1336 .implied_opt_name
= "cpus",
1337 .merge_lists
= true,
1338 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1342 .type
= QEMU_OPT_NUMBER
,
1345 .type
= QEMU_OPT_NUMBER
,
1348 .type
= QEMU_OPT_NUMBER
,
1351 .type
= QEMU_OPT_NUMBER
,
1354 .type
= QEMU_OPT_NUMBER
,
1356 { /*End of list */ }
1360 static void smp_parse(QemuOpts
*opts
)
1364 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1365 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1366 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1367 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1369 /* compute missing values, prefer sockets over cores over threads */
1370 if (cpus
== 0 || sockets
== 0) {
1371 sockets
= sockets
> 0 ? sockets
: 1;
1372 cores
= cores
> 0 ? cores
: 1;
1373 threads
= threads
> 0 ? threads
: 1;
1375 cpus
= cores
* threads
* sockets
;
1379 threads
= threads
> 0 ? threads
: 1;
1380 cores
= cpus
/ (sockets
* threads
);
1382 threads
= cpus
/ (cores
* sockets
);
1386 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1389 smp_cores
= cores
> 0 ? cores
: 1;
1390 smp_threads
= threads
> 0 ? threads
: 1;
1394 if (max_cpus
== 0) {
1395 max_cpus
= smp_cpus
;
1398 if (max_cpus
> MAX_CPUMASK_BITS
) {
1399 fprintf(stderr
, "Unsupported number of maxcpus\n");
1402 if (max_cpus
< smp_cpus
) {
1403 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1409 static void configure_realtime(QemuOpts
*opts
)
1413 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1416 if (os_mlock() < 0) {
1417 fprintf(stderr
, "qemu: locking memory failed\n");
1424 static void configure_msg(QemuOpts
*opts
)
1426 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1429 /***********************************************************/
1432 static int usb_device_add(const char *devname
)
1434 USBDevice
*dev
= NULL
;
1435 #ifndef CONFIG_LINUX
1439 if (!usb_enabled(false)) {
1443 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1444 dev
= usbdevice_create(devname
);
1448 /* the other ones */
1449 #ifndef CONFIG_LINUX
1450 /* only the linux version is qdev-ified, usb-bsd still needs this */
1451 if (strstart(devname
, "host:", &p
)) {
1452 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1462 static int usb_device_del(const char *devname
)
1467 if (strstart(devname
, "host:", &p
)) {
1471 if (!usb_enabled(false)) {
1475 p
= strchr(devname
, '.');
1478 bus_num
= strtoul(devname
, NULL
, 0);
1479 addr
= strtoul(p
+ 1, NULL
, 0);
1481 return usb_device_delete_addr(bus_num
, addr
);
1484 static int usb_parse(const char *cmdline
)
1487 r
= usb_device_add(cmdline
);
1489 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1494 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1496 const char *devname
= qdict_get_str(qdict
, "devname");
1497 if (usb_device_add(devname
) < 0) {
1498 error_report("could not add USB device '%s'", devname
);
1502 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1504 const char *devname
= qdict_get_str(qdict
, "devname");
1505 if (usb_device_del(devname
) < 0) {
1506 error_report("could not delete USB device '%s'", devname
);
1510 /***********************************************************/
1511 /* PCMCIA/Cardbus */
1513 static struct pcmcia_socket_entry_s
{
1514 PCMCIASocket
*socket
;
1515 struct pcmcia_socket_entry_s
*next
;
1516 } *pcmcia_sockets
= 0;
1518 void pcmcia_socket_register(PCMCIASocket
*socket
)
1520 struct pcmcia_socket_entry_s
*entry
;
1522 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1523 entry
->socket
= socket
;
1524 entry
->next
= pcmcia_sockets
;
1525 pcmcia_sockets
= entry
;
1528 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1530 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1532 ptr
= &pcmcia_sockets
;
1533 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1534 if (entry
->socket
== socket
) {
1540 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1542 struct pcmcia_socket_entry_s
*iter
;
1544 if (!pcmcia_sockets
)
1545 monitor_printf(mon
, "No PCMCIA sockets\n");
1547 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1548 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1549 iter
->socket
->attached
? iter
->socket
->card_string
:
1553 /***********************************************************/
1554 /* machine registration */
1556 MachineState
*current_machine
;
1558 static void machine_class_init(ObjectClass
*oc
, void *data
)
1560 MachineClass
*mc
= MACHINE_CLASS(oc
);
1561 QEMUMachine
*qm
= data
;
1563 mc
->name
= qm
->name
;
1564 mc
->alias
= qm
->alias
;
1565 mc
->desc
= qm
->desc
;
1566 mc
->init
= qm
->init
;
1567 mc
->reset
= qm
->reset
;
1568 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1569 mc
->kvm_type
= qm
->kvm_type
;
1570 mc
->block_default_type
= qm
->block_default_type
;
1571 mc
->max_cpus
= qm
->max_cpus
;
1572 mc
->no_serial
= qm
->no_serial
;
1573 mc
->no_parallel
= qm
->no_parallel
;
1574 mc
->use_virtcon
= qm
->use_virtcon
;
1575 mc
->use_sclp
= qm
->use_sclp
;
1576 mc
->no_floppy
= qm
->no_floppy
;
1577 mc
->no_cdrom
= qm
->no_cdrom
;
1578 mc
->no_sdcard
= qm
->no_sdcard
;
1579 mc
->is_default
= qm
->is_default
;
1580 mc
->default_machine_opts
= qm
->default_machine_opts
;
1581 mc
->default_boot_order
= qm
->default_boot_order
;
1582 mc
->compat_props
= qm
->compat_props
;
1583 mc
->hw_version
= qm
->hw_version
;
1586 int qemu_register_machine(QEMUMachine
*m
)
1588 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1591 .parent
= TYPE_MACHINE
,
1592 .class_init
= machine_class_init
,
1593 .class_data
= (void *)m
,
1602 static MachineClass
*find_machine(const char *name
)
1604 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1605 MachineClass
*mc
= NULL
;
1607 for (el
= machines
; el
; el
= el
->next
) {
1608 MachineClass
*temp
= el
->data
;
1610 if (!strcmp(temp
->name
, name
)) {
1615 !strcmp(temp
->alias
, name
)) {
1621 g_slist_free(machines
);
1625 MachineClass
*find_default_machine(void)
1627 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1628 MachineClass
*mc
= NULL
;
1630 for (el
= machines
; el
; el
= el
->next
) {
1631 MachineClass
*temp
= el
->data
;
1633 if (temp
->is_default
) {
1639 g_slist_free(machines
);
1643 MachineInfoList
*qmp_query_machines(Error
**errp
)
1645 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1646 MachineInfoList
*mach_list
= NULL
;
1648 for (el
= machines
; el
; el
= el
->next
) {
1649 MachineClass
*mc
= el
->data
;
1650 MachineInfoList
*entry
;
1653 info
= g_malloc0(sizeof(*info
));
1654 if (mc
->is_default
) {
1655 info
->has_is_default
= true;
1656 info
->is_default
= true;
1660 info
->has_alias
= true;
1661 info
->alias
= g_strdup(mc
->alias
);
1664 info
->name
= g_strdup(mc
->name
);
1665 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1667 entry
= g_malloc0(sizeof(*entry
));
1668 entry
->value
= info
;
1669 entry
->next
= mach_list
;
1673 g_slist_free(machines
);
1677 /***********************************************************/
1678 /* main execution loop */
1680 struct vm_change_state_entry
{
1681 VMChangeStateHandler
*cb
;
1683 QLIST_ENTRY (vm_change_state_entry
) entries
;
1686 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1688 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1691 VMChangeStateEntry
*e
;
1693 e
= g_malloc0(sizeof (*e
));
1697 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1701 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1703 QLIST_REMOVE (e
, entries
);
1707 void vm_state_notify(int running
, RunState state
)
1709 VMChangeStateEntry
*e
;
1711 trace_vm_state_notify(running
, state
);
1713 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1714 e
->cb(e
->opaque
, running
, state
);
1718 /* reset/shutdown handler */
1720 typedef struct QEMUResetEntry
{
1721 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1722 QEMUResetHandler
*func
;
1726 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1727 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1728 static int reset_requested
;
1729 static int shutdown_requested
, shutdown_signal
= -1;
1730 static pid_t shutdown_pid
;
1731 static int powerdown_requested
;
1732 static int debug_requested
;
1733 static int suspend_requested
;
1734 static WakeupReason wakeup_reason
;
1735 static NotifierList powerdown_notifiers
=
1736 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1737 static NotifierList suspend_notifiers
=
1738 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1739 static NotifierList wakeup_notifiers
=
1740 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1741 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1743 int qemu_shutdown_requested_get(void)
1745 return shutdown_requested
;
1748 int qemu_reset_requested_get(void)
1750 return reset_requested
;
1753 static int qemu_shutdown_requested(void)
1755 int r
= shutdown_requested
;
1756 shutdown_requested
= 0;
1760 static void qemu_kill_report(void)
1762 if (!qtest_driver() && shutdown_signal
!= -1) {
1763 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1764 if (shutdown_pid
== 0) {
1765 /* This happens for eg ^C at the terminal, so it's worth
1766 * avoiding printing an odd message in that case.
1768 fputc('\n', stderr
);
1770 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1772 shutdown_signal
= -1;
1776 static int qemu_reset_requested(void)
1778 int r
= reset_requested
;
1779 reset_requested
= 0;
1783 static int qemu_suspend_requested(void)
1785 int r
= suspend_requested
;
1786 suspend_requested
= 0;
1790 static WakeupReason
qemu_wakeup_requested(void)
1792 return wakeup_reason
;
1795 static int qemu_powerdown_requested(void)
1797 int r
= powerdown_requested
;
1798 powerdown_requested
= 0;
1802 static int qemu_debug_requested(void)
1804 int r
= debug_requested
;
1805 debug_requested
= 0;
1809 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1811 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1814 re
->opaque
= opaque
;
1815 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1818 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1822 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1823 if (re
->func
== func
&& re
->opaque
== opaque
) {
1824 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1831 void qemu_devices_reset(void)
1833 QEMUResetEntry
*re
, *nre
;
1835 /* reset all devices */
1836 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1837 re
->func(re
->opaque
);
1841 void qemu_system_reset(bool report
)
1845 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1847 if (mc
&& mc
->reset
) {
1850 qemu_devices_reset();
1853 monitor_protocol_event(QEVENT_RESET
, NULL
);
1855 cpu_synchronize_all_post_reset();
1858 void qemu_system_reset_request(void)
1861 shutdown_requested
= 1;
1863 reset_requested
= 1;
1866 qemu_notify_event();
1869 static void qemu_system_suspend(void)
1872 notifier_list_notify(&suspend_notifiers
, NULL
);
1873 runstate_set(RUN_STATE_SUSPENDED
);
1874 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1877 void qemu_system_suspend_request(void)
1879 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1882 suspend_requested
= 1;
1884 qemu_notify_event();
1887 void qemu_register_suspend_notifier(Notifier
*notifier
)
1889 notifier_list_add(&suspend_notifiers
, notifier
);
1892 void qemu_system_wakeup_request(WakeupReason reason
)
1894 trace_system_wakeup_request(reason
);
1896 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1899 if (!(wakeup_reason_mask
& (1 << reason
))) {
1902 runstate_set(RUN_STATE_RUNNING
);
1903 wakeup_reason
= reason
;
1904 qemu_notify_event();
1907 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1910 wakeup_reason_mask
|= (1 << reason
);
1912 wakeup_reason_mask
&= ~(1 << reason
);
1916 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1918 notifier_list_add(&wakeup_notifiers
, notifier
);
1921 void qemu_system_killed(int signal
, pid_t pid
)
1923 shutdown_signal
= signal
;
1926 qemu_system_shutdown_request();
1929 void qemu_system_shutdown_request(void)
1931 shutdown_requested
= 1;
1932 qemu_notify_event();
1935 static void qemu_system_powerdown(void)
1937 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1938 notifier_list_notify(&powerdown_notifiers
, NULL
);
1941 void qemu_system_powerdown_request(void)
1943 powerdown_requested
= 1;
1944 qemu_notify_event();
1947 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1949 notifier_list_add(&powerdown_notifiers
, notifier
);
1952 void qemu_system_debug_request(void)
1954 debug_requested
= 1;
1955 qemu_notify_event();
1958 static bool main_loop_should_exit(void)
1961 if (qemu_debug_requested()) {
1962 vm_stop(RUN_STATE_DEBUG
);
1964 if (qemu_suspend_requested()) {
1965 qemu_system_suspend();
1967 if (qemu_shutdown_requested()) {
1969 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1971 vm_stop(RUN_STATE_SHUTDOWN
);
1976 if (qemu_reset_requested()) {
1978 cpu_synchronize_all_states();
1979 qemu_system_reset(VMRESET_REPORT
);
1981 if (runstate_needs_reset()) {
1982 runstate_set(RUN_STATE_PAUSED
);
1985 if (qemu_wakeup_requested()) {
1987 cpu_synchronize_all_states();
1988 qemu_system_reset(VMRESET_SILENT
);
1989 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1990 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1992 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1994 if (qemu_powerdown_requested()) {
1995 qemu_system_powerdown();
1997 if (qemu_vmstop_requested(&r
)) {
2003 static void main_loop(void)
2007 #ifdef CONFIG_PROFILER
2011 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2012 #ifdef CONFIG_PROFILER
2013 ti
= profile_getclock();
2015 last_io
= main_loop_wait(nonblocking
);
2016 #ifdef CONFIG_PROFILER
2017 dev_time
+= profile_getclock() - ti
;
2019 } while (!main_loop_should_exit());
2022 static void version(void)
2024 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2027 static void help(int exitcode
)
2030 printf("usage: %s [options] [disk_image]\n\n"
2031 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2032 error_get_progname());
2034 #define QEMU_OPTIONS_GENERATE_HELP
2035 #include "qemu-options-wrapper.h"
2037 printf("\nDuring emulation, the following keys are useful:\n"
2038 "ctrl-alt-f toggle full screen\n"
2039 "ctrl-alt-n switch to virtual console 'n'\n"
2040 "ctrl-alt toggle mouse and keyboard grab\n"
2042 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2047 #define HAS_ARG 0x0001
2049 typedef struct QEMUOption
{
2056 static const QEMUOption qemu_options
[] = {
2057 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2058 #define QEMU_OPTIONS_GENERATE_OPTIONS
2059 #include "qemu-options-wrapper.h"
2063 static bool vga_available(void)
2065 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2068 static bool cirrus_vga_available(void)
2070 return object_class_by_name("cirrus-vga")
2071 || object_class_by_name("isa-cirrus-vga");
2074 static bool vmware_vga_available(void)
2076 return object_class_by_name("vmware-svga");
2079 static bool qxl_vga_available(void)
2081 return object_class_by_name("qxl-vga");
2084 static bool tcx_vga_available(void)
2086 return object_class_by_name("SUNW,tcx");
2089 static bool cg3_vga_available(void)
2091 return object_class_by_name("cgthree");
2094 static void select_vgahw (const char *p
)
2098 assert(vga_interface_type
== VGA_NONE
);
2099 if (strstart(p
, "std", &opts
)) {
2100 if (vga_available()) {
2101 vga_interface_type
= VGA_STD
;
2103 fprintf(stderr
, "Error: standard VGA not available\n");
2106 } else if (strstart(p
, "cirrus", &opts
)) {
2107 if (cirrus_vga_available()) {
2108 vga_interface_type
= VGA_CIRRUS
;
2110 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2113 } else if (strstart(p
, "vmware", &opts
)) {
2114 if (vmware_vga_available()) {
2115 vga_interface_type
= VGA_VMWARE
;
2117 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2120 } else if (strstart(p
, "xenfb", &opts
)) {
2121 vga_interface_type
= VGA_XENFB
;
2122 } else if (strstart(p
, "qxl", &opts
)) {
2123 if (qxl_vga_available()) {
2124 vga_interface_type
= VGA_QXL
;
2126 fprintf(stderr
, "Error: QXL VGA not available\n");
2129 } else if (strstart(p
, "tcx", &opts
)) {
2130 if (tcx_vga_available()) {
2131 vga_interface_type
= VGA_TCX
;
2133 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2136 } else if (strstart(p
, "cg3", &opts
)) {
2137 if (cg3_vga_available()) {
2138 vga_interface_type
= VGA_CG3
;
2140 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2143 } else if (!strstart(p
, "none", &opts
)) {
2145 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2149 const char *nextopt
;
2151 if (strstart(opts
, ",retrace=", &nextopt
)) {
2153 if (strstart(opts
, "dumb", &nextopt
))
2154 vga_retrace_method
= VGA_RETRACE_DUMB
;
2155 else if (strstart(opts
, "precise", &nextopt
))
2156 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2157 else goto invalid_vga
;
2158 } else goto invalid_vga
;
2163 static DisplayType
select_display(const char *p
)
2166 DisplayType display
= DT_DEFAULT
;
2168 if (strstart(p
, "sdl", &opts
)) {
2172 const char *nextopt
;
2174 if (strstart(opts
, ",frame=", &nextopt
)) {
2176 if (strstart(opts
, "on", &nextopt
)) {
2178 } else if (strstart(opts
, "off", &nextopt
)) {
2181 goto invalid_sdl_args
;
2183 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2185 if (strstart(opts
, "on", &nextopt
)) {
2187 } else if (strstart(opts
, "off", &nextopt
)) {
2190 goto invalid_sdl_args
;
2192 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2194 if (strstart(opts
, "on", &nextopt
)) {
2196 } else if (strstart(opts
, "off", &nextopt
)) {
2199 goto invalid_sdl_args
;
2201 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2203 if (strstart(opts
, "on", &nextopt
)) {
2205 } else if (strstart(opts
, "off", &nextopt
)) {
2208 goto invalid_sdl_args
;
2212 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2218 fprintf(stderr
, "SDL support is disabled\n");
2221 } else if (strstart(p
, "vnc", &opts
)) {
2226 const char *nextopt
;
2228 if (strstart(opts
, "=", &nextopt
)) {
2229 vnc_display
= nextopt
;
2233 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2237 fprintf(stderr
, "VNC support is disabled\n");
2240 } else if (strstart(p
, "curses", &opts
)) {
2241 #ifdef CONFIG_CURSES
2242 display
= DT_CURSES
;
2244 fprintf(stderr
, "Curses support is disabled\n");
2247 } else if (strstart(p
, "gtk", &opts
)) {
2251 const char *nextopt
;
2253 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2255 if (strstart(opts
, "on", &nextopt
)) {
2256 grab_on_hover
= true;
2257 } else if (strstart(opts
, "off", &nextopt
)) {
2258 grab_on_hover
= false;
2260 goto invalid_gtk_args
;
2264 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2270 fprintf(stderr
, "GTK support is disabled\n");
2273 } else if (strstart(p
, "none", &opts
)) {
2276 fprintf(stderr
, "Unknown display type: %s\n", p
);
2283 static int balloon_parse(const char *arg
)
2287 if (strcmp(arg
, "none") == 0) {
2291 if (!strncmp(arg
, "virtio", 6)) {
2292 if (arg
[6] == ',') {
2293 /* have params -> parse them */
2294 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2298 /* create empty opts */
2299 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2302 qemu_opt_set(opts
, "driver", "virtio-balloon");
2309 char *qemu_find_file(int type
, const char *name
)
2315 /* Try the name as a straight path first */
2316 if (access(name
, R_OK
) == 0) {
2317 trace_load_file(name
, name
);
2318 return g_strdup(name
);
2322 case QEMU_FILE_TYPE_BIOS
:
2325 case QEMU_FILE_TYPE_KEYMAP
:
2326 subdir
= "keymaps/";
2332 for (i
= 0; i
< data_dir_idx
; i
++) {
2333 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2334 if (access(buf
, R_OK
) == 0) {
2335 trace_load_file(name
, buf
);
2343 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2345 return qdev_device_help(opts
);
2348 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2352 dev
= qdev_device_add(opts
);
2355 object_unref(OBJECT(dev
));
2359 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2361 Error
*local_err
= NULL
;
2363 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2365 error_report("%s", error_get_pretty(local_err
));
2366 error_free(local_err
);
2372 #ifdef CONFIG_VIRTFS
2373 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2376 ret
= qemu_fsdev_add(opts
);
2382 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2384 CharDriverState
*chr
;
2385 const char *chardev
;
2389 mode
= qemu_opt_get(opts
, "mode");
2393 if (strcmp(mode
, "readline") == 0) {
2394 flags
= MONITOR_USE_READLINE
;
2395 } else if (strcmp(mode
, "control") == 0) {
2396 flags
= MONITOR_USE_CONTROL
;
2398 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2402 if (qemu_opt_get_bool(opts
, "pretty", 0))
2403 flags
|= MONITOR_USE_PRETTY
;
2405 if (qemu_opt_get_bool(opts
, "default", 0))
2406 flags
|= MONITOR_IS_DEFAULT
;
2408 chardev
= qemu_opt_get(opts
, "chardev");
2409 chr
= qemu_chr_find(chardev
);
2411 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2415 qemu_chr_fe_claim_no_fail(chr
);
2416 monitor_init(chr
, flags
);
2420 static void monitor_parse(const char *optarg
, const char *mode
)
2422 static int monitor_device_index
= 0;
2428 if (strstart(optarg
, "chardev:", &p
)) {
2429 snprintf(label
, sizeof(label
), "%s", p
);
2431 snprintf(label
, sizeof(label
), "compat_monitor%d",
2432 monitor_device_index
);
2433 if (monitor_device_index
== 0) {
2436 opts
= qemu_chr_parse_compat(label
, optarg
);
2438 fprintf(stderr
, "parse error: %s\n", optarg
);
2443 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2445 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2448 qemu_opt_set(opts
, "mode", mode
);
2449 qemu_opt_set(opts
, "chardev", label
);
2451 qemu_opt_set(opts
, "default", "on");
2452 monitor_device_index
++;
2455 struct device_config
{
2457 DEV_USB
, /* -usbdevice */
2459 DEV_SERIAL
, /* -serial */
2460 DEV_PARALLEL
, /* -parallel */
2461 DEV_VIRTCON
, /* -virtioconsole */
2462 DEV_DEBUGCON
, /* -debugcon */
2463 DEV_GDB
, /* -gdb, -s */
2464 DEV_SCLP
, /* s390 sclp */
2466 const char *cmdline
;
2468 QTAILQ_ENTRY(device_config
) next
;
2471 static QTAILQ_HEAD(, device_config
) device_configs
=
2472 QTAILQ_HEAD_INITIALIZER(device_configs
);
2474 static void add_device_config(int type
, const char *cmdline
)
2476 struct device_config
*conf
;
2478 conf
= g_malloc0(sizeof(*conf
));
2480 conf
->cmdline
= cmdline
;
2481 loc_save(&conf
->loc
);
2482 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2485 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2487 struct device_config
*conf
;
2490 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2491 if (conf
->type
!= type
)
2493 loc_push_restore(&conf
->loc
);
2494 rc
= func(conf
->cmdline
);
2495 loc_pop(&conf
->loc
);
2502 static int serial_parse(const char *devname
)
2504 static int index
= 0;
2507 if (strcmp(devname
, "none") == 0)
2509 if (index
== MAX_SERIAL_PORTS
) {
2510 fprintf(stderr
, "qemu: too many serial ports\n");
2513 snprintf(label
, sizeof(label
), "serial%d", index
);
2514 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2515 if (!serial_hds
[index
]) {
2516 fprintf(stderr
, "qemu: could not connect serial device"
2517 " to character backend '%s'\n", devname
);
2524 static int parallel_parse(const char *devname
)
2526 static int index
= 0;
2529 if (strcmp(devname
, "none") == 0)
2531 if (index
== MAX_PARALLEL_PORTS
) {
2532 fprintf(stderr
, "qemu: too many parallel ports\n");
2535 snprintf(label
, sizeof(label
), "parallel%d", index
);
2536 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2537 if (!parallel_hds
[index
]) {
2538 fprintf(stderr
, "qemu: could not connect parallel device"
2539 " to character backend '%s'\n", devname
);
2546 static int virtcon_parse(const char *devname
)
2548 QemuOptsList
*device
= qemu_find_opts("device");
2549 static int index
= 0;
2551 QemuOpts
*bus_opts
, *dev_opts
;
2553 if (strcmp(devname
, "none") == 0)
2555 if (index
== MAX_VIRTIO_CONSOLES
) {
2556 fprintf(stderr
, "qemu: too many virtio consoles\n");
2560 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2561 if (arch_type
== QEMU_ARCH_S390X
) {
2562 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2564 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2567 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2568 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2570 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2571 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2572 if (!virtcon_hds
[index
]) {
2573 fprintf(stderr
, "qemu: could not connect virtio console"
2574 " to character backend '%s'\n", devname
);
2577 qemu_opt_set(dev_opts
, "chardev", label
);
2583 static int sclp_parse(const char *devname
)
2585 QemuOptsList
*device
= qemu_find_opts("device");
2586 static int index
= 0;
2590 if (strcmp(devname
, "none") == 0) {
2593 if (index
== MAX_SCLP_CONSOLES
) {
2594 fprintf(stderr
, "qemu: too many sclp consoles\n");
2598 assert(arch_type
== QEMU_ARCH_S390X
);
2600 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2601 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2603 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2604 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2605 if (!sclp_hds
[index
]) {
2606 fprintf(stderr
, "qemu: could not connect sclp console"
2607 " to character backend '%s'\n", devname
);
2610 qemu_opt_set(dev_opts
, "chardev", label
);
2616 static int debugcon_parse(const char *devname
)
2620 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2623 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2625 fprintf(stderr
, "qemu: already have a debugcon device\n");
2628 qemu_opt_set(opts
, "driver", "isa-debugcon");
2629 qemu_opt_set(opts
, "chardev", "debugcon");
2633 static MachineClass
*machine_parse(const char *name
)
2635 MachineClass
*mc
= NULL
;
2636 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2639 mc
= find_machine(name
);
2644 if (name
&& !is_help_option(name
)) {
2645 error_report("Unsupported machine type");
2646 error_printf("Use -machine help to list supported machines!\n");
2648 printf("Supported machines are:\n");
2649 for (el
= machines
; el
; el
= el
->next
) {
2650 MachineClass
*mc
= el
->data
;
2652 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2654 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2655 mc
->is_default
? " (default)" : "");
2659 g_slist_free(machines
);
2660 exit(!name
|| !is_help_option(name
));
2663 static int tcg_init(MachineClass
*mc
)
2665 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2670 const char *opt_name
;
2672 int (*available
)(void);
2673 int (*init
)(MachineClass
*mc
);
2676 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2677 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2678 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2679 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2682 static int configure_accelerator(MachineClass
*mc
)
2687 bool accel_initialised
= false;
2688 bool init_failed
= false;
2690 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2692 /* Use the default "accelerator", tcg */
2696 while (!accel_initialised
&& *p
!= '\0') {
2700 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2701 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2702 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2703 if (!accel_list
[i
].available()) {
2704 printf("%s not supported for this target\n",
2705 accel_list
[i
].name
);
2708 *(accel_list
[i
].allowed
) = true;
2709 ret
= accel_list
[i
].init(mc
);
2712 fprintf(stderr
, "failed to initialize %s: %s\n",
2715 *(accel_list
[i
].allowed
) = false;
2717 accel_initialised
= true;
2722 if (i
== ARRAY_SIZE(accel_list
)) {
2723 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2727 if (!accel_initialised
) {
2729 fprintf(stderr
, "No accelerator found!\n");
2735 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2738 return !accel_initialised
;
2741 void qemu_add_exit_notifier(Notifier
*notify
)
2743 notifier_list_add(&exit_notifiers
, notify
);
2746 void qemu_remove_exit_notifier(Notifier
*notify
)
2748 notifier_remove(notify
);
2751 static void qemu_run_exit_notifiers(void)
2753 notifier_list_notify(&exit_notifiers
, NULL
);
2756 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2758 notifier_list_add(&machine_init_done_notifiers
, notify
);
2761 static void qemu_run_machine_init_done_notifiers(void)
2763 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2766 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2767 const char **poptarg
, int *poptind
)
2769 const QEMUOption
*popt
;
2770 int optind
= *poptind
;
2771 char *r
= argv
[optind
];
2774 loc_set_cmdline(argv
, optind
, 1);
2776 /* Treat --foo the same as -foo. */
2779 popt
= qemu_options
;
2782 error_report("invalid option");
2785 if (!strcmp(popt
->name
, r
+ 1))
2789 if (popt
->flags
& HAS_ARG
) {
2790 if (optind
>= argc
) {
2791 error_report("requires an argument");
2794 optarg
= argv
[optind
++];
2795 loc_set_cmdline(argv
, optind
- 2, 2);
2806 static gpointer
malloc_and_trace(gsize n_bytes
)
2808 void *ptr
= malloc(n_bytes
);
2809 trace_g_malloc(n_bytes
, ptr
);
2813 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2815 void *ptr
= realloc(mem
, n_bytes
);
2816 trace_g_realloc(mem
, n_bytes
, ptr
);
2820 static void free_and_trace(gpointer mem
)
2826 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2828 Object
*obj
= OBJECT(opaque
);
2829 StringInputVisitor
*siv
;
2830 Error
*local_err
= NULL
;
2832 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2833 strcmp(name
, "type") == 0) {
2837 siv
= string_input_visitor_new(value
);
2838 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2839 string_input_visitor_cleanup(siv
);
2842 qerror_report_err(local_err
);
2843 error_free(local_err
);
2850 static int object_create(QemuOpts
*opts
, void *opaque
)
2859 ov
= opts_visitor_new(opts
);
2860 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2862 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2867 qdict_del(pdict
, "qom-type");
2868 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2873 qdict_del(pdict
, "id");
2874 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2879 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2883 visit_end_struct(opts_get_visitor(ov
), &err
);
2885 qmp_object_del(id
, NULL
);
2889 opts_visitor_cleanup(ov
);
2896 qerror_report_err(err
);
2902 int main(int argc
, char **argv
, char **envp
)
2905 int snapshot
, linux_boot
;
2906 const char *icount_option
= NULL
;
2907 const char *initrd_filename
;
2908 const char *kernel_filename
, *kernel_cmdline
;
2909 const char *boot_order
;
2911 int cyls
, heads
, secs
, translation
;
2912 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2913 QemuOptsList
*olist
;
2916 const char *loadvm
= NULL
;
2917 MachineClass
*machine_class
;
2918 const char *cpu_model
;
2919 const char *vga_model
= NULL
;
2920 const char *qtest_chrdev
= NULL
;
2921 const char *qtest_log
= NULL
;
2922 const char *pid_file
= NULL
;
2923 const char *incoming
= NULL
;
2925 int show_vnc_port
= 0;
2927 bool defconfig
= true;
2928 bool userconfig
= true;
2929 const char *log_mask
= NULL
;
2930 const char *log_file
= NULL
;
2931 GMemVTable mem_trace
= {
2932 .malloc
= malloc_and_trace
,
2933 .realloc
= realloc_and_trace
,
2934 .free
= free_and_trace
,
2936 const char *trace_events
= NULL
;
2937 const char *trace_file
= NULL
;
2938 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2940 ram_addr_t maxram_size
= default_ram_size
;
2941 uint64_t ram_slots
= 0;
2943 atexit(qemu_run_exit_notifiers
);
2944 error_set_progname(argv
[0]);
2945 qemu_init_exec_dir(argv
[0]);
2947 g_mem_set_vtable(&mem_trace
);
2949 module_call_init(MODULE_INIT_QOM
);
2951 qemu_add_opts(&qemu_drive_opts
);
2952 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2953 qemu_add_drive_opts(&qemu_common_drive_opts
);
2954 qemu_add_drive_opts(&qemu_drive_opts
);
2955 qemu_add_opts(&qemu_chardev_opts
);
2956 qemu_add_opts(&qemu_device_opts
);
2957 qemu_add_opts(&qemu_netdev_opts
);
2958 qemu_add_opts(&qemu_net_opts
);
2959 qemu_add_opts(&qemu_rtc_opts
);
2960 qemu_add_opts(&qemu_global_opts
);
2961 qemu_add_opts(&qemu_mon_opts
);
2962 qemu_add_opts(&qemu_trace_opts
);
2963 qemu_add_opts(&qemu_option_rom_opts
);
2964 qemu_add_opts(&qemu_machine_opts
);
2965 qemu_add_opts(&qemu_mem_opts
);
2966 qemu_add_opts(&qemu_smp_opts
);
2967 qemu_add_opts(&qemu_boot_opts
);
2968 qemu_add_opts(&qemu_sandbox_opts
);
2969 qemu_add_opts(&qemu_add_fd_opts
);
2970 qemu_add_opts(&qemu_object_opts
);
2971 qemu_add_opts(&qemu_tpmdev_opts
);
2972 qemu_add_opts(&qemu_realtime_opts
);
2973 qemu_add_opts(&qemu_msg_opts
);
2974 qemu_add_opts(&qemu_name_opts
);
2975 qemu_add_opts(&qemu_numa_opts
);
2979 rtc_clock
= QEMU_CLOCK_HOST
;
2981 qemu_init_auxval(envp
);
2982 qemu_cache_utils_init();
2984 QLIST_INIT (&vm_change_state_head
);
2985 os_setup_early_signal_handling();
2987 module_call_init(MODULE_INIT_MACHINE
);
2988 machine_class
= find_default_machine();
2990 ram_size
= default_ram_size
;
2992 cyls
= heads
= secs
= 0;
2993 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2995 for (i
= 0; i
< MAX_NODES
; i
++) {
2996 numa_info
[i
].node_mem
= 0;
2997 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
3003 bdrv_init_with_whitelist();
3007 /* first pass of option parsing */
3009 while (optind
< argc
) {
3010 if (argv
[optind
][0] != '-') {
3014 const QEMUOption
*popt
;
3016 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3017 switch (popt
->index
) {
3018 case QEMU_OPTION_nodefconfig
:
3021 case QEMU_OPTION_nouserconfig
:
3030 ret
= qemu_read_default_config_files(userconfig
);
3036 /* second pass of option parsing */
3041 if (argv
[optind
][0] != '-') {
3042 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3044 const QEMUOption
*popt
;
3046 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3047 if (!(popt
->arch_mask
& arch_type
)) {
3048 printf("Option %s not supported for this target\n", popt
->name
);
3051 switch(popt
->index
) {
3053 machine_class
= machine_parse(optarg
);
3055 case QEMU_OPTION_no_kvm_irqchip
: {
3056 olist
= qemu_find_opts("machine");
3057 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3060 case QEMU_OPTION_cpu
:
3061 /* hw initialization will check this */
3064 case QEMU_OPTION_hda
:
3068 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3070 snprintf(buf
, sizeof(buf
),
3071 "%s,cyls=%d,heads=%d,secs=%d%s",
3072 HD_OPTS
, cyls
, heads
, secs
,
3073 translation
== BIOS_ATA_TRANSLATION_LBA
?
3075 translation
== BIOS_ATA_TRANSLATION_NONE
?
3076 ",trans=none" : "");
3077 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3080 case QEMU_OPTION_hdb
:
3081 case QEMU_OPTION_hdc
:
3082 case QEMU_OPTION_hdd
:
3083 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3086 case QEMU_OPTION_drive
:
3087 if (drive_def(optarg
) == NULL
) {
3091 case QEMU_OPTION_set
:
3092 if (qemu_set_option(optarg
) != 0)
3095 case QEMU_OPTION_global
:
3096 if (qemu_global_option(optarg
) != 0)
3099 case QEMU_OPTION_mtdblock
:
3100 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3102 case QEMU_OPTION_sd
:
3103 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3105 case QEMU_OPTION_pflash
:
3106 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3108 case QEMU_OPTION_snapshot
:
3111 case QEMU_OPTION_hdachs
:
3115 cyls
= strtol(p
, (char **)&p
, 0);
3116 if (cyls
< 1 || cyls
> 16383)
3121 heads
= strtol(p
, (char **)&p
, 0);
3122 if (heads
< 1 || heads
> 16)
3127 secs
= strtol(p
, (char **)&p
, 0);
3128 if (secs
< 1 || secs
> 63)
3132 if (!strcmp(p
, "large")) {
3133 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3134 } else if (!strcmp(p
, "rechs")) {
3135 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3136 } else if (!strcmp(p
, "none")) {
3137 translation
= BIOS_ATA_TRANSLATION_NONE
;
3138 } else if (!strcmp(p
, "lba")) {
3139 translation
= BIOS_ATA_TRANSLATION_LBA
;
3140 } else if (!strcmp(p
, "auto")) {
3141 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3145 } else if (*p
!= '\0') {
3147 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3150 if (hda_opts
!= NULL
) {
3152 snprintf(num
, sizeof(num
), "%d", cyls
);
3153 qemu_opt_set(hda_opts
, "cyls", num
);
3154 snprintf(num
, sizeof(num
), "%d", heads
);
3155 qemu_opt_set(hda_opts
, "heads", num
);
3156 snprintf(num
, sizeof(num
), "%d", secs
);
3157 qemu_opt_set(hda_opts
, "secs", num
);
3158 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3159 qemu_opt_set(hda_opts
, "trans", "large");
3160 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3161 qemu_opt_set(hda_opts
, "trans", "rechs");
3162 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3163 qemu_opt_set(hda_opts
, "trans", "lba");
3164 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3165 qemu_opt_set(hda_opts
, "trans", "none");
3170 case QEMU_OPTION_numa
:
3171 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3176 case QEMU_OPTION_display
:
3177 display_type
= select_display(optarg
);
3179 case QEMU_OPTION_nographic
:
3180 display_type
= DT_NOGRAPHIC
;
3182 case QEMU_OPTION_curses
:
3183 #ifdef CONFIG_CURSES
3184 display_type
= DT_CURSES
;
3186 fprintf(stderr
, "Curses support is disabled\n");
3190 case QEMU_OPTION_portrait
:
3191 graphic_rotate
= 90;
3193 case QEMU_OPTION_rotate
:
3194 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3195 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3196 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3198 "qemu: only 90, 180, 270 deg rotation is available\n");
3202 case QEMU_OPTION_kernel
:
3203 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3205 case QEMU_OPTION_initrd
:
3206 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3208 case QEMU_OPTION_append
:
3209 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3211 case QEMU_OPTION_dtb
:
3212 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3214 case QEMU_OPTION_cdrom
:
3215 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3217 case QEMU_OPTION_boot
:
3218 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3223 case QEMU_OPTION_fda
:
3224 case QEMU_OPTION_fdb
:
3225 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3228 case QEMU_OPTION_no_fd_bootchk
:
3231 case QEMU_OPTION_netdev
:
3232 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3236 case QEMU_OPTION_net
:
3237 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3241 #ifdef CONFIG_LIBISCSI
3242 case QEMU_OPTION_iscsi
:
3243 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3250 case QEMU_OPTION_tftp
:
3251 legacy_tftp_prefix
= optarg
;
3253 case QEMU_OPTION_bootp
:
3254 legacy_bootp_filename
= optarg
;
3256 case QEMU_OPTION_redir
:
3257 if (net_slirp_redir(optarg
) < 0)
3261 case QEMU_OPTION_bt
:
3262 add_device_config(DEV_BT
, optarg
);
3264 case QEMU_OPTION_audio_help
:
3268 case QEMU_OPTION_soundhw
:
3269 select_soundhw (optarg
);
3274 case QEMU_OPTION_version
:
3278 case QEMU_OPTION_m
: {
3280 const char *mem_str
;
3281 const char *maxmem_str
, *slots_str
;
3283 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3289 mem_str
= qemu_opt_get(opts
, "size");
3291 error_report("invalid -m option, missing 'size' option");
3295 error_report("missing 'size' option value");
3299 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3301 /* Fix up legacy suffix-less format */
3302 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3303 uint64_t overflow_check
= sz
;
3306 if ((sz
>> 20) != overflow_check
) {
3307 error_report("too large 'size' option value");
3312 /* backward compatibility behaviour for case "-m 0" */
3314 sz
= default_ram_size
;
3317 sz
= QEMU_ALIGN_UP(sz
, 8192);
3319 if (ram_size
!= sz
) {
3320 error_report("ram size too large");
3324 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3325 slots_str
= qemu_opt_get(opts
, "slots");
3326 if (maxmem_str
&& slots_str
) {
3329 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3330 if (sz
< ram_size
) {
3331 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3332 "(%" PRIu64
") <= initial memory ("
3333 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3337 slots
= qemu_opt_get_number(opts
, "slots", 0);
3338 if ((sz
> ram_size
) && !slots
) {
3339 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3340 "(%" PRIu64
") more than initial memory ("
3341 RAM_ADDR_FMT
") but no hotplug slots where "
3342 "specified\n", sz
, ram_size
);
3346 if ((sz
<= ram_size
) && slots
) {
3347 fprintf(stderr
, "qemu: invalid -m option value: %"
3348 PRIu64
" hotplug slots where specified but "
3349 "maxmem (%" PRIu64
") <= initial memory ("
3350 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3355 } else if ((!maxmem_str
&& slots_str
) ||
3356 (maxmem_str
&& !slots_str
)) {
3357 fprintf(stderr
, "qemu: invalid -m option value: missing "
3358 "'%s' option\n", slots_str
? "maxmem" : "slots");
3364 case QEMU_OPTION_tpmdev
:
3365 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3370 case QEMU_OPTION_mempath
:
3373 case QEMU_OPTION_mem_prealloc
:
3383 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3385 case QEMU_OPTION_gdb
:
3386 add_device_config(DEV_GDB
, optarg
);
3389 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3390 data_dir
[data_dir_idx
++] = optarg
;
3393 case QEMU_OPTION_bios
:
3394 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3396 case QEMU_OPTION_singlestep
:
3403 keyboard_layout
= optarg
;
3405 case QEMU_OPTION_localtime
:
3408 case QEMU_OPTION_vga
:
3417 w
= strtol(p
, (char **)&p
, 10);
3420 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3426 h
= strtol(p
, (char **)&p
, 10);
3431 depth
= strtol(p
, (char **)&p
, 10);
3432 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3433 depth
!= 24 && depth
!= 32)
3435 } else if (*p
== '\0') {
3436 depth
= graphic_depth
;
3443 graphic_depth
= depth
;
3446 case QEMU_OPTION_echr
:
3449 term_escape_char
= strtol(optarg
, &r
, 0);
3451 printf("Bad argument to echr\n");
3454 case QEMU_OPTION_monitor
:
3455 default_monitor
= 0;
3456 if (strncmp(optarg
, "none", 4)) {
3457 monitor_parse(optarg
, "readline");
3460 case QEMU_OPTION_qmp
:
3461 monitor_parse(optarg
, "control");
3462 default_monitor
= 0;
3464 case QEMU_OPTION_mon
:
3465 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3469 default_monitor
= 0;
3471 case QEMU_OPTION_chardev
:
3472 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3477 case QEMU_OPTION_fsdev
:
3478 olist
= qemu_find_opts("fsdev");
3480 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3483 opts
= qemu_opts_parse(olist
, optarg
, 1);
3488 case QEMU_OPTION_virtfs
: {
3491 const char *writeout
, *sock_fd
, *socket
;
3493 olist
= qemu_find_opts("virtfs");
3495 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3498 opts
= qemu_opts_parse(olist
, optarg
, 1);
3503 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3504 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3505 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3508 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3509 qemu_opt_get(opts
, "mount_tag"),
3512 fprintf(stderr
, "duplicate fsdev id: %s\n",
3513 qemu_opt_get(opts
, "mount_tag"));
3517 writeout
= qemu_opt_get(opts
, "writeout");
3519 #ifdef CONFIG_SYNC_FILE_RANGE
3520 qemu_opt_set(fsdev
, "writeout", writeout
);
3522 fprintf(stderr
, "writeout=immediate not supported on "
3527 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3528 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3529 qemu_opt_set(fsdev
, "security_model",
3530 qemu_opt_get(opts
, "security_model"));
3531 socket
= qemu_opt_get(opts
, "socket");
3533 qemu_opt_set(fsdev
, "socket", socket
);
3535 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3537 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3540 qemu_opt_set_bool(fsdev
, "readonly",
3541 qemu_opt_get_bool(opts
, "readonly", 0));
3542 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3544 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3545 qemu_opt_set(device
, "fsdev",
3546 qemu_opt_get(opts
, "mount_tag"));
3547 qemu_opt_set(device
, "mount_tag",
3548 qemu_opt_get(opts
, "mount_tag"));
3551 case QEMU_OPTION_virtfs_synth
: {
3555 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3558 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3561 qemu_opt_set(fsdev
, "fsdriver", "synth");
3563 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3565 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3566 qemu_opt_set(device
, "fsdev", "v_synth");
3567 qemu_opt_set(device
, "mount_tag", "v_synth");
3570 case QEMU_OPTION_serial
:
3571 add_device_config(DEV_SERIAL
, optarg
);
3573 if (strncmp(optarg
, "mon:", 4) == 0) {
3574 default_monitor
= 0;
3577 case QEMU_OPTION_watchdog
:
3580 "qemu: only one watchdog option may be given\n");
3585 case QEMU_OPTION_watchdog_action
:
3586 if (select_watchdog_action(optarg
) == -1) {
3587 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3591 case QEMU_OPTION_virtiocon
:
3592 add_device_config(DEV_VIRTCON
, optarg
);
3593 default_virtcon
= 0;
3594 if (strncmp(optarg
, "mon:", 4) == 0) {
3595 default_monitor
= 0;
3598 case QEMU_OPTION_parallel
:
3599 add_device_config(DEV_PARALLEL
, optarg
);
3600 default_parallel
= 0;
3601 if (strncmp(optarg
, "mon:", 4) == 0) {
3602 default_monitor
= 0;
3605 case QEMU_OPTION_debugcon
:
3606 add_device_config(DEV_DEBUGCON
, optarg
);
3608 case QEMU_OPTION_loadvm
:
3611 case QEMU_OPTION_full_screen
:
3614 case QEMU_OPTION_no_frame
:
3617 case QEMU_OPTION_alt_grab
:
3620 case QEMU_OPTION_ctrl_grab
:
3623 case QEMU_OPTION_no_quit
:
3626 case QEMU_OPTION_sdl
:
3628 display_type
= DT_SDL
;
3631 fprintf(stderr
, "SDL support is disabled\n");
3634 case QEMU_OPTION_pidfile
:
3637 case QEMU_OPTION_win2k_hack
:
3638 win2k_install_hack
= 1;
3640 case QEMU_OPTION_rtc_td_hack
: {
3641 static GlobalProperty slew_lost_ticks
[] = {
3643 .driver
= "mc146818rtc",
3644 .property
= "lost_tick_policy",
3647 { /* end of list */ }
3650 qdev_prop_register_global_list(slew_lost_ticks
);
3653 case QEMU_OPTION_acpitable
:
3654 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3658 do_acpitable_option(opts
);
3660 case QEMU_OPTION_smbios
:
3661 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3665 do_smbios_option(opts
);
3667 case QEMU_OPTION_enable_kvm
:
3668 olist
= qemu_find_opts("machine");
3669 qemu_opts_parse(olist
, "accel=kvm", 0);
3671 case QEMU_OPTION_machine
:
3672 olist
= qemu_find_opts("machine");
3673 opts
= qemu_opts_parse(olist
, optarg
, 1);
3677 optarg
= qemu_opt_get(opts
, "type");
3679 machine_class
= machine_parse(optarg
);
3682 case QEMU_OPTION_no_kvm
:
3683 olist
= qemu_find_opts("machine");
3684 qemu_opts_parse(olist
, "accel=tcg", 0);
3686 case QEMU_OPTION_no_kvm_pit
: {
3687 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3691 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3692 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3694 .driver
= "kvm-pit",
3695 .property
= "lost_tick_policy",
3698 { /* end of list */ }
3701 fprintf(stderr
, "Warning: option deprecated, use "
3702 "lost_tick_policy property of kvm-pit instead.\n");
3703 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3706 case QEMU_OPTION_usb
:
3707 olist
= qemu_find_opts("machine");
3708 qemu_opts_parse(olist
, "usb=on", 0);
3710 case QEMU_OPTION_usbdevice
:
3711 olist
= qemu_find_opts("machine");
3712 qemu_opts_parse(olist
, "usb=on", 0);
3713 add_device_config(DEV_USB
, optarg
);
3715 case QEMU_OPTION_device
:
3716 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3720 case QEMU_OPTION_smp
:
3721 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3725 case QEMU_OPTION_vnc
:
3728 vnc_display
= optarg
;
3730 fprintf(stderr
, "VNC support is disabled\n");
3734 case QEMU_OPTION_no_acpi
:
3737 case QEMU_OPTION_no_hpet
:
3740 case QEMU_OPTION_balloon
:
3741 if (balloon_parse(optarg
) < 0) {
3742 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3746 case QEMU_OPTION_no_reboot
:
3749 case QEMU_OPTION_no_shutdown
:
3752 case QEMU_OPTION_show_cursor
:
3755 case QEMU_OPTION_uuid
:
3756 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3757 fprintf(stderr
, "Fail to parse UUID string."
3758 " Wrong format.\n");
3761 qemu_uuid_set
= true;
3763 case QEMU_OPTION_option_rom
:
3764 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3765 fprintf(stderr
, "Too many option ROMs\n");
3768 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3772 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3773 option_rom
[nb_option_roms
].bootindex
=
3774 qemu_opt_get_number(opts
, "bootindex", -1);
3775 if (!option_rom
[nb_option_roms
].name
) {
3776 fprintf(stderr
, "Option ROM file is not specified\n");
3781 case QEMU_OPTION_semihosting
:
3782 semihosting_enabled
= 1;
3784 case QEMU_OPTION_tdf
:
3785 fprintf(stderr
, "Warning: user space PIT time drift fix "
3786 "is no longer supported.\n");
3788 case QEMU_OPTION_name
:
3789 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3794 case QEMU_OPTION_prom_env
:
3795 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3796 fprintf(stderr
, "Too many prom variables\n");
3799 prom_envs
[nb_prom_envs
] = optarg
;
3802 case QEMU_OPTION_old_param
:
3805 case QEMU_OPTION_clock
:
3806 /* Clock options no longer exist. Keep this option for
3807 * backward compatibility.
3810 case QEMU_OPTION_startdate
:
3811 configure_rtc_date_offset(optarg
, 1);
3813 case QEMU_OPTION_rtc
:
3814 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3818 configure_rtc(opts
);
3820 case QEMU_OPTION_tb_size
:
3821 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3822 if (tcg_tb_size
< 0) {
3826 case QEMU_OPTION_icount
:
3827 icount_option
= optarg
;
3829 case QEMU_OPTION_incoming
:
3831 runstate_set(RUN_STATE_INMIGRATE
);
3833 case QEMU_OPTION_nodefaults
:
3836 case QEMU_OPTION_xen_domid
:
3837 if (!(xen_available())) {
3838 printf("Option %s not supported for this target\n", popt
->name
);
3841 xen_domid
= atoi(optarg
);
3843 case QEMU_OPTION_xen_create
:
3844 if (!(xen_available())) {
3845 printf("Option %s not supported for this target\n", popt
->name
);
3848 xen_mode
= XEN_CREATE
;
3850 case QEMU_OPTION_xen_attach
:
3851 if (!(xen_available())) {
3852 printf("Option %s not supported for this target\n", popt
->name
);
3855 xen_mode
= XEN_ATTACH
;
3857 case QEMU_OPTION_trace
:
3859 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3863 trace_events
= qemu_opt_get(opts
, "events");
3864 trace_file
= qemu_opt_get(opts
, "file");
3867 case QEMU_OPTION_readconfig
:
3869 int ret
= qemu_read_config_file(optarg
);
3871 fprintf(stderr
, "read config %s: %s\n", optarg
,
3877 case QEMU_OPTION_spice
:
3878 olist
= qemu_find_opts("spice");
3880 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3883 opts
= qemu_opts_parse(olist
, optarg
, 0);
3889 case QEMU_OPTION_writeconfig
:
3892 if (strcmp(optarg
, "-") == 0) {
3895 fp
= fopen(optarg
, "w");
3897 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3901 qemu_config_write(fp
);
3907 case QEMU_OPTION_qtest
:
3908 qtest_chrdev
= optarg
;
3910 case QEMU_OPTION_qtest_log
:
3913 case QEMU_OPTION_sandbox
:
3914 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3919 case QEMU_OPTION_add_fd
:
3921 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3926 error_report("File descriptor passing is disabled on this "
3931 case QEMU_OPTION_object
:
3932 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3937 case QEMU_OPTION_realtime
:
3938 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3942 configure_realtime(opts
);
3944 case QEMU_OPTION_msg
:
3945 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3949 configure_msg(opts
);
3952 os_parse_cmd_args(popt
->index
, optarg
);
3960 if (qemu_init_main_loop()) {
3961 fprintf(stderr
, "qemu_init_main_loop failed\n");
3965 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3969 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3974 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3978 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3983 if (machine_class
== NULL
) {
3984 fprintf(stderr
, "No machine specified, and there is no default.\n"
3985 "Use -machine help to list supported machines!\n");
3989 cpu_exec_init_all();
3991 current_machine
= MACHINE(object_new(object_class_get_name(
3992 OBJECT_CLASS(machine_class
))));
3993 object_property_add_child(object_get_root(), "machine",
3994 OBJECT(current_machine
), &error_abort
);
3996 if (machine_class
->hw_version
) {
3997 qemu_set_version(machine_class
->hw_version
);
4000 if (qemu_opts_foreach(qemu_find_opts("object"),
4001 object_create
, NULL
, 0) != 0) {
4005 /* Init CPU def lists, based on config
4006 * - Must be called after all the qemu_read_config_file() calls
4007 * - Must be called before list_cpus()
4008 * - Must be called before machine->init()
4012 if (cpu_model
&& is_help_option(cpu_model
)) {
4013 list_cpus(stdout
, &fprintf
, cpu_model
);
4017 /* Open the logfile at this point, if necessary. We can't open the logfile
4018 * when encountering either of the logging options (-d or -D) because the
4019 * other one may be encountered later on the command line, changing the
4020 * location or level of logging.
4025 qemu_set_log_filename(log_file
);
4028 mask
= qemu_str_to_log_mask(log_mask
);
4030 qemu_print_log_usage(stdout
);
4036 if (!is_daemonized()) {
4037 if (!trace_init_backends(trace_events
, trace_file
)) {
4042 /* If no data_dir is specified then try to find it relative to the
4044 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4045 data_dir
[data_dir_idx
] = os_find_datadir();
4046 if (data_dir
[data_dir_idx
] != NULL
) {
4050 /* If all else fails use the install path specified when building. */
4051 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4052 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4055 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4057 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4058 if (smp_cpus
> machine_class
->max_cpus
) {
4059 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4060 "supported by machine `%s' (%d)\n", smp_cpus
,
4061 machine_class
->name
, machine_class
->max_cpus
);
4066 * Get the default machine options from the machine if it is not already
4067 * specified either by the configuration file or by the command line.
4069 if (machine_class
->default_machine_opts
) {
4070 qemu_opts_set_defaults(qemu_find_opts("machine"),
4071 machine_class
->default_machine_opts
, 0);
4074 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4075 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4077 if (!vga_model
&& !default_vga
) {
4078 vga_interface_type
= VGA_DEVICE
;
4080 if (!has_defaults
|| machine_class
->no_serial
) {
4083 if (!has_defaults
|| machine_class
->no_parallel
) {
4084 default_parallel
= 0;
4086 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4087 default_virtcon
= 0;
4089 if (!has_defaults
|| !machine_class
->use_sclp
) {
4092 if (!has_defaults
|| machine_class
->no_floppy
) {
4095 if (!has_defaults
|| machine_class
->no_cdrom
) {
4098 if (!has_defaults
|| machine_class
->no_sdcard
) {
4101 if (!has_defaults
) {
4102 default_monitor
= 0;
4107 if (is_daemonized()) {
4108 /* According to documentation and historically, -nographic redirects
4109 * serial port, parallel port and monitor to stdio, which does not work
4110 * with -daemonize. We can redirect these to null instead, but since
4111 * -nographic is legacy, let's just error out.
4112 * We disallow -nographic only if all other ports are not redirected
4113 * explicitly, to not break existing legacy setups which uses
4114 * -nographic _and_ redirects all ports explicitly - this is valid
4115 * usage, -nographic is just a no-op in this case.
4117 if (display_type
== DT_NOGRAPHIC
4118 && (default_parallel
|| default_serial
4119 || default_monitor
|| default_virtcon
)) {
4120 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4123 #ifdef CONFIG_CURSES
4124 if (display_type
== DT_CURSES
) {
4125 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4131 if (display_type
== DT_NOGRAPHIC
) {
4132 if (default_parallel
)
4133 add_device_config(DEV_PARALLEL
, "null");
4134 if (default_serial
&& default_monitor
) {
4135 add_device_config(DEV_SERIAL
, "mon:stdio");
4136 } else if (default_virtcon
&& default_monitor
) {
4137 add_device_config(DEV_VIRTCON
, "mon:stdio");
4138 } else if (default_sclp
&& default_monitor
) {
4139 add_device_config(DEV_SCLP
, "mon:stdio");
4142 add_device_config(DEV_SERIAL
, "stdio");
4143 if (default_virtcon
)
4144 add_device_config(DEV_VIRTCON
, "stdio");
4146 add_device_config(DEV_SCLP
, "stdio");
4148 if (default_monitor
)
4149 monitor_parse("stdio", "readline");
4153 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4154 if (default_parallel
)
4155 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4156 if (default_monitor
)
4157 monitor_parse("vc:80Cx24C", "readline");
4158 if (default_virtcon
)
4159 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4161 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4165 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4166 #if defined(CONFIG_GTK)
4167 display_type
= DT_GTK
;
4168 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4169 display_type
= DT_SDL
;
4170 #elif defined(CONFIG_VNC)
4171 vnc_display
= "localhost:0,to=99";
4174 display_type
= DT_NONE
;
4178 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4179 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4180 "for SDL, ignoring option\n");
4182 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4183 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4184 "ignoring option\n");
4187 #if defined(CONFIG_GTK)
4188 if (display_type
== DT_GTK
) {
4189 early_gtk_display_init();
4195 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4197 #ifdef CONFIG_VIRTFS
4198 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4203 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4208 /* store value for the future use */
4209 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4211 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4216 machine_opts
= qemu_get_machine_opts();
4217 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4219 object_unref(OBJECT(current_machine
));
4223 configure_accelerator(machine_class
);
4226 Error
*local_err
= NULL
;
4227 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4229 error_report("%s", error_get_pretty(local_err
));
4230 error_free(local_err
);
4235 machine_opts
= qemu_get_machine_opts();
4236 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4237 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4238 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4239 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4241 boot_order
= machine_class
->default_boot_order
;
4242 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4244 char *normal_boot_order
;
4245 const char *order
, *once
;
4247 order
= qemu_opt_get(opts
, "order");
4249 validate_bootdevices(order
);
4253 once
= qemu_opt_get(opts
, "once");
4255 validate_bootdevices(once
);
4256 normal_boot_order
= g_strdup(boot_order
);
4258 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4261 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4262 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4265 if (!kernel_cmdline
) {
4266 kernel_cmdline
= "";
4267 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4270 linux_boot
= (kernel_filename
!= NULL
);
4272 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4273 fprintf(stderr
, "-append only allowed with -kernel option\n");
4277 if (!linux_boot
&& initrd_filename
!= NULL
) {
4278 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4282 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4283 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4287 os_set_line_buffering();
4289 qemu_init_cpu_loop();
4290 qemu_mutex_lock_iothread();
4293 /* spice needs the timers to be initialized by this point */
4297 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4298 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4301 configure_icount(icount_option
);
4303 /* clean up network at qemu process termination */
4304 atexit(&net_cleanup
);
4306 if (net_init_clients() < 0) {
4311 if (tpm_init() < 0) {
4316 /* init the bluetooth world */
4317 if (foreach_device_config(DEV_BT
, bt_parse
))
4320 if (!xen_enabled()) {
4321 /* On 32-bit hosts, QEMU is limited by virtual address space */
4322 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4323 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4331 /* open the virtual block devices */
4333 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4334 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4335 &machine_class
->block_default_type
, 1) != 0) {
4339 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4341 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4342 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4344 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4351 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4355 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4357 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4359 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4361 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4364 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4367 /* If no default VGA is requested, the default is "none". */
4369 if (cirrus_vga_available()) {
4370 vga_model
= "cirrus";
4371 } else if (vga_available()) {
4376 select_vgahw(vga_model
);
4380 i
= select_watchdog(watchdog
);
4382 exit (i
== 1 ? 1 : 0);
4385 if (machine_class
->compat_props
) {
4386 qdev_prop_register_global_list(machine_class
->compat_props
);
4390 qdev_machine_init();
4392 current_machine
->ram_size
= ram_size
;
4393 current_machine
->maxram_size
= maxram_size
;
4394 current_machine
->ram_slots
= ram_slots
;
4395 current_machine
->boot_order
= boot_order
;
4396 current_machine
->cpu_model
= cpu_model
;
4398 machine_class
->init(current_machine
);
4402 cpu_synchronize_all_post_init();
4406 /* init USB devices */
4407 if (usb_enabled(false)) {
4408 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4412 /* init generic devices */
4413 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4416 net_check_clients();
4418 ds
= init_displaystate();
4420 /* init local displays */
4421 switch (display_type
) {
4423 (void)ds
; /* avoid warning if no display is configured */
4425 #if defined(CONFIG_CURSES)
4427 curses_display_init(ds
, full_screen
);
4430 #if defined(CONFIG_SDL)
4432 sdl_display_init(ds
, full_screen
, no_frame
);
4434 #elif defined(CONFIG_COCOA)
4436 cocoa_display_init(ds
, full_screen
);
4439 #if defined(CONFIG_GTK)
4441 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4448 /* must be after terminal init, SDL library changes signal handlers */
4449 os_setup_signal_handling();
4452 /* init remote displays */
4454 Error
*local_err
= NULL
;
4455 vnc_display_init(ds
);
4456 vnc_display_open(ds
, vnc_display
, &local_err
);
4457 if (local_err
!= NULL
) {
4458 error_report("Failed to start VNC server on `%s': %s",
4459 vnc_display
, error_get_pretty(local_err
));
4460 error_free(local_err
);
4464 if (show_vnc_port
) {
4465 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4471 qemu_spice_display_init();
4475 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4479 qdev_machine_creation_done();
4481 if (rom_load_all() != 0) {
4482 fprintf(stderr
, "rom loading failed\n");
4486 /* TODO: once all bus devices are qdevified, this should be done
4487 * when bus is created by qdev.c */
4488 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4489 qemu_run_machine_init_done_notifiers();
4491 /* Done notifiers can load ROMs */
4494 qemu_system_reset(VMRESET_SILENT
);
4496 if (load_vmstate(loadvm
) < 0) {
4501 qdev_prop_check_global();
4504 Error
*local_err
= NULL
;
4505 qemu_start_incoming_migration(incoming
, &local_err
);
4507 error_report("-incoming %s: %s", incoming
,
4508 error_get_pretty(local_err
));
4509 error_free(local_err
);
4512 } else if (autostart
) {
4518 if (is_daemonized()) {
4519 if (!trace_init_backends(trace_events
, trace_file
)) {