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 "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
100 #include "fsdev/qemu-fsdev.h"
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
120 #define DEFAULT_RAM_SIZE 128
122 #define MAX_VIRTIO_CONSOLES 1
123 #define MAX_SCLP_CONSOLES 1
125 static const char *data_dir
[16];
126 static int data_dir_idx
;
127 const char *bios_name
= NULL
;
128 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
129 DisplayType display_type
= DT_DEFAULT
;
130 static int display_remote
;
131 const char* keyboard_layout
= NULL
;
133 const char *mem_path
= NULL
;
134 int mem_prealloc
= 0; /* force preallocation of physical target memory */
136 NICInfo nd_table
[MAX_NICS
];
138 static int rtc_utc
= 1;
139 static int rtc_date_offset
= -1; /* -1 means no change */
140 QEMUClockType rtc_clock
;
141 int vga_interface_type
= VGA_NONE
;
142 static int full_screen
= 0;
143 static int no_frame
= 0;
146 static bool grab_on_hover
;
148 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
149 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
150 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
151 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
152 int win2k_install_hack
= 0;
159 const char *vnc_display
;
161 int acpi_enabled
= 1;
164 static int no_reboot
;
167 int graphic_rotate
= 0;
168 const char *watchdog
;
169 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
171 int semihosting_enabled
= 0;
173 const char *qemu_name
;
176 unsigned int nb_prom_envs
= 0;
177 const char *prom_envs
[MAX_PROM_ENVS
];
179 static bool boot_strict
;
180 uint8_t *boot_splash_filedata
;
181 size_t boot_splash_filedata_size
;
182 uint8_t qemu_extra_params_fw
[2];
184 typedef struct FWBootEntry FWBootEntry
;
187 QTAILQ_ENTRY(FWBootEntry
) link
;
193 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
194 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
197 NodeInfo numa_info
[MAX_NODES
];
199 uint8_t qemu_uuid
[16];
202 static QEMUBootSetHandler
*boot_set_handler
;
203 static void *boot_set_opaque
;
205 static NotifierList exit_notifiers
=
206 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
208 static NotifierList machine_init_done_notifiers
=
209 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
211 static bool tcg_allowed
= true;
214 enum xen_mode xen_mode
= XEN_EMULATE
;
215 static int tcg_tb_size
;
217 static int has_defaults
= 1;
218 static int default_serial
= 1;
219 static int default_parallel
= 1;
220 static int default_virtcon
= 1;
221 static int default_sclp
= 1;
222 static int default_monitor
= 1;
223 static int default_floppy
= 1;
224 static int default_cdrom
= 1;
225 static int default_sdcard
= 1;
226 static int default_vga
= 1;
232 { .driver
= "isa-serial", .flag
= &default_serial
},
233 { .driver
= "isa-parallel", .flag
= &default_parallel
},
234 { .driver
= "isa-fdc", .flag
= &default_floppy
},
235 { .driver
= "ide-cd", .flag
= &default_cdrom
},
236 { .driver
= "ide-hd", .flag
= &default_cdrom
},
237 { .driver
= "ide-drive", .flag
= &default_cdrom
},
238 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
239 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
240 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
241 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
242 { .driver
= "VGA", .flag
= &default_vga
},
243 { .driver
= "isa-vga", .flag
= &default_vga
},
244 { .driver
= "cirrus-vga", .flag
= &default_vga
},
245 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
246 { .driver
= "vmware-svga", .flag
= &default_vga
},
247 { .driver
= "qxl-vga", .flag
= &default_vga
},
250 static QemuOptsList qemu_rtc_opts
= {
252 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
256 .type
= QEMU_OPT_STRING
,
259 .type
= QEMU_OPT_STRING
,
262 .type
= QEMU_OPT_STRING
,
264 { /* end of list */ }
268 static QemuOptsList qemu_sandbox_opts
= {
270 .implied_opt_name
= "enable",
271 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
275 .type
= QEMU_OPT_BOOL
,
277 { /* end of list */ }
281 static QemuOptsList qemu_trace_opts
= {
283 .implied_opt_name
= "trace",
284 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
288 .type
= QEMU_OPT_STRING
,
291 .type
= QEMU_OPT_STRING
,
293 { /* end of list */ }
297 static QemuOptsList qemu_option_rom_opts
= {
298 .name
= "option-rom",
299 .implied_opt_name
= "romfile",
300 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
304 .type
= QEMU_OPT_NUMBER
,
307 .type
= QEMU_OPT_STRING
,
309 { /* end of list */ }
313 static QemuOptsList qemu_machine_opts
= {
315 .implied_opt_name
= "type",
317 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
321 .type
= QEMU_OPT_STRING
,
322 .help
= "emulated machine"
325 .type
= QEMU_OPT_STRING
,
326 .help
= "accelerator list",
328 .name
= "kernel_irqchip",
329 .type
= QEMU_OPT_BOOL
,
330 .help
= "use KVM in-kernel irqchip",
332 .name
= "kvm_shadow_mem",
333 .type
= QEMU_OPT_SIZE
,
334 .help
= "KVM shadow MMU size",
337 .type
= QEMU_OPT_STRING
,
338 .help
= "Linux kernel image file",
341 .type
= QEMU_OPT_STRING
,
342 .help
= "Linux initial ramdisk file",
345 .type
= QEMU_OPT_STRING
,
346 .help
= "Linux kernel command line",
349 .type
= QEMU_OPT_STRING
,
350 .help
= "Linux kernel device tree file",
353 .type
= QEMU_OPT_STRING
,
354 .help
= "Dump current dtb to a file and quit",
356 .name
= "phandle_start",
357 .type
= QEMU_OPT_NUMBER
,
358 .help
= "The first phandle ID we may generate dynamically",
360 .name
= "dt_compatible",
361 .type
= QEMU_OPT_STRING
,
362 .help
= "Overrides the \"compatible\" property of the dt root node",
364 .name
= "dump-guest-core",
365 .type
= QEMU_OPT_BOOL
,
366 .help
= "Include guest memory in a core dump",
369 .type
= QEMU_OPT_BOOL
,
370 .help
= "enable/disable memory merge support",
373 .type
= QEMU_OPT_BOOL
,
374 .help
= "Set on/off to enable/disable usb",
377 .type
= QEMU_OPT_STRING
,
378 .help
= "firmware image",
381 .type
= QEMU_OPT_STRING
,
382 .help
= "Specifies the KVM virtualization mode (HV, PR)",
384 { /* End of list */ }
388 static QemuOptsList qemu_boot_opts
= {
390 .implied_opt_name
= "order",
392 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
396 .type
= QEMU_OPT_STRING
,
399 .type
= QEMU_OPT_STRING
,
402 .type
= QEMU_OPT_BOOL
,
405 .type
= QEMU_OPT_STRING
,
407 .name
= "splash-time",
408 .type
= QEMU_OPT_STRING
,
410 .name
= "reboot-timeout",
411 .type
= QEMU_OPT_STRING
,
414 .type
= QEMU_OPT_BOOL
,
420 static QemuOptsList qemu_add_fd_opts
= {
422 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
426 .type
= QEMU_OPT_NUMBER
,
427 .help
= "file descriptor of which a duplicate is added to fd set",
430 .type
= QEMU_OPT_NUMBER
,
431 .help
= "ID of the fd set to add fd to",
434 .type
= QEMU_OPT_STRING
,
435 .help
= "free-form string used to describe fd",
437 { /* end of list */ }
441 static QemuOptsList qemu_object_opts
= {
443 .implied_opt_name
= "qom-type",
444 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
450 static QemuOptsList qemu_tpmdev_opts
= {
452 .implied_opt_name
= "type",
453 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
455 /* options are defined in the TPM backends */
456 { /* end of list */ }
460 static QemuOptsList qemu_realtime_opts
= {
462 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
466 .type
= QEMU_OPT_BOOL
,
468 { /* end of list */ }
472 static QemuOptsList qemu_msg_opts
= {
474 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
478 .type
= QEMU_OPT_BOOL
,
480 { /* end of list */ }
484 static QemuOptsList qemu_name_opts
= {
486 .implied_opt_name
= "guest",
488 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
492 .type
= QEMU_OPT_STRING
,
493 .help
= "Sets the name of the guest.\n"
494 "This name will be displayed in the SDL window caption.\n"
495 "The name will also be used for the VNC server",
498 .type
= QEMU_OPT_STRING
,
499 .help
= "Sets the name of the QEMU process, as shown in top etc",
501 .name
= "debug-threads",
502 .type
= QEMU_OPT_BOOL
,
503 .help
= "When enabled, name the individual threads; defaults off.\n"
504 "NOTE: The thread names are for debugging and not a\n"
507 { /* End of list */ }
511 static QemuOptsList qemu_mem_opts
= {
513 .implied_opt_name
= "size",
514 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
519 .type
= QEMU_OPT_SIZE
,
523 .type
= QEMU_OPT_NUMBER
,
527 .type
= QEMU_OPT_SIZE
,
529 { /* end of list */ }
534 * Get machine options
536 * Returns: machine options (never null).
538 QemuOpts
*qemu_get_machine_opts(void)
540 return qemu_find_opts_singleton("machine");
543 const char *qemu_get_vm_name(void)
548 static void res_free(void)
550 if (boot_splash_filedata
!= NULL
) {
551 g_free(boot_splash_filedata
);
552 boot_splash_filedata
= NULL
;
556 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
558 const char *driver
= qemu_opt_get(opts
, "driver");
563 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
564 if (strcmp(default_list
[i
].driver
, driver
) != 0)
566 *(default_list
[i
].flag
) = 0;
571 /***********************************************************/
574 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
576 /* We use RUN_STATE_MAX but any invalid value will do */
577 static RunState vmstop_requested
= RUN_STATE_MAX
;
578 static QemuMutex vmstop_lock
;
583 } RunStateTransition
;
585 static const RunStateTransition runstate_transitions_def
[] = {
587 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
588 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
590 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
591 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
593 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
594 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
596 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
597 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
599 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
600 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
603 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
605 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
606 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
607 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
609 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
610 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
612 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
614 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
615 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
616 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
617 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
618 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
619 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
620 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
621 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
622 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
623 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
625 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
627 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
628 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
630 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
631 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
632 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
633 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
635 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
636 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
638 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
639 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
641 { RUN_STATE_MAX
, RUN_STATE_MAX
},
644 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
646 bool runstate_check(RunState state
)
648 return current_run_state
== state
;
651 static void runstate_init(void)
653 const RunStateTransition
*p
;
655 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
656 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
657 runstate_valid_transitions
[p
->from
][p
->to
] = true;
660 qemu_mutex_init(&vmstop_lock
);
663 /* This function will abort() on invalid state transitions */
664 void runstate_set(RunState new_state
)
666 assert(new_state
< RUN_STATE_MAX
);
668 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
669 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
670 RunState_lookup
[current_run_state
],
671 RunState_lookup
[new_state
]);
674 trace_runstate_set(new_state
);
675 current_run_state
= new_state
;
678 int runstate_is_running(void)
680 return runstate_check(RUN_STATE_RUNNING
);
683 bool runstate_needs_reset(void)
685 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
686 runstate_check(RUN_STATE_SHUTDOWN
);
689 StatusInfo
*qmp_query_status(Error
**errp
)
691 StatusInfo
*info
= g_malloc0(sizeof(*info
));
693 info
->running
= runstate_is_running();
694 info
->singlestep
= singlestep
;
695 info
->status
= current_run_state
;
700 static bool qemu_vmstop_requested(RunState
*r
)
702 qemu_mutex_lock(&vmstop_lock
);
703 *r
= vmstop_requested
;
704 vmstop_requested
= RUN_STATE_MAX
;
705 qemu_mutex_unlock(&vmstop_lock
);
706 return *r
< RUN_STATE_MAX
;
709 void qemu_system_vmstop_request_prepare(void)
711 qemu_mutex_lock(&vmstop_lock
);
714 void qemu_system_vmstop_request(RunState state
)
716 vmstop_requested
= state
;
717 qemu_mutex_unlock(&vmstop_lock
);
725 qemu_vmstop_requested(&requested
);
726 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
730 /* Ensure that a STOP/RESUME pair of events is emitted if a
731 * vmstop request was pending. The BLOCK_IO_ERROR event, for
732 * example, according to documentation is always followed by
735 if (runstate_is_running()) {
736 monitor_protocol_event(QEVENT_STOP
, NULL
);
739 runstate_set(RUN_STATE_RUNNING
);
740 vm_state_notify(1, RUN_STATE_RUNNING
);
744 monitor_protocol_event(QEVENT_RESUME
, NULL
);
748 /***********************************************************/
749 /* real time host monotonic timer */
751 /***********************************************************/
752 /* host time/date access */
753 void qemu_get_timedate(struct tm
*tm
, int offset
)
759 if (rtc_date_offset
== -1) {
763 localtime_r(&ti
, tm
);
765 ti
-= rtc_date_offset
;
770 int qemu_timedate_diff(struct tm
*tm
)
774 if (rtc_date_offset
== -1)
776 seconds
= mktimegm(tm
);
779 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
780 seconds
= mktime(&tmp
);
783 seconds
= mktimegm(tm
) + rtc_date_offset
;
785 return seconds
- time(NULL
);
788 void rtc_change_mon_event(struct tm
*tm
)
792 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
793 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
794 qobject_decref(data
);
797 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
799 time_t rtc_start_date
;
802 if (!strcmp(startdate
, "now") && legacy
) {
803 rtc_date_offset
= -1;
805 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
813 } else if (sscanf(startdate
, "%d-%d-%d",
825 rtc_start_date
= mktimegm(&tm
);
826 if (rtc_start_date
== -1) {
828 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
829 "'2006-06-17T16:01:21' or '2006-06-17'\n");
832 rtc_date_offset
= time(NULL
) - rtc_start_date
;
836 static void configure_rtc(QemuOpts
*opts
)
840 value
= qemu_opt_get(opts
, "base");
842 if (!strcmp(value
, "utc")) {
844 } else if (!strcmp(value
, "localtime")) {
847 configure_rtc_date_offset(value
, 0);
850 value
= qemu_opt_get(opts
, "clock");
852 if (!strcmp(value
, "host")) {
853 rtc_clock
= QEMU_CLOCK_HOST
;
854 } else if (!strcmp(value
, "rt")) {
855 rtc_clock
= QEMU_CLOCK_REALTIME
;
856 } else if (!strcmp(value
, "vm")) {
857 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
859 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
863 value
= qemu_opt_get(opts
, "driftfix");
865 if (!strcmp(value
, "slew")) {
866 static GlobalProperty slew_lost_ticks
[] = {
868 .driver
= "mc146818rtc",
869 .property
= "lost_tick_policy",
872 { /* end of list */ }
875 qdev_prop_register_global_list(slew_lost_ticks
);
876 } else if (!strcmp(value
, "none")) {
877 /* discard is default */
879 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
885 /***********************************************************/
886 /* Bluetooth support */
889 static struct HCIInfo
*hci_table
[MAX_NICS
];
891 struct HCIInfo
*qemu_next_hci(void)
893 if (cur_hci
== nb_hcis
)
896 return hci_table
[cur_hci
++];
899 static int bt_hci_parse(const char *str
)
904 if (nb_hcis
>= MAX_NICS
) {
905 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
918 bdaddr
.b
[5] = 0x56 + nb_hcis
;
919 hci
->bdaddr_set(hci
, bdaddr
.b
);
921 hci_table
[nb_hcis
++] = hci
;
926 static void bt_vhci_add(int vlan_id
)
928 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
931 fprintf(stderr
, "qemu: warning: adding a VHCI to "
932 "an empty scatternet %i\n", vlan_id
);
934 bt_vhci_init(bt_new_hci(vlan
));
937 static struct bt_device_s
*bt_device_add(const char *opt
)
939 struct bt_scatternet_s
*vlan
;
941 char *endp
= strstr(opt
, ",vlan=");
942 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
945 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
948 vlan_id
= strtol(endp
+ 6, &endp
, 0);
950 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
955 vlan
= qemu_find_bt_vlan(vlan_id
);
958 fprintf(stderr
, "qemu: warning: adding a slave device to "
959 "an empty scatternet %i\n", vlan_id
);
961 if (!strcmp(devname
, "keyboard"))
962 return bt_keyboard_init(vlan
);
964 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
968 static int bt_parse(const char *opt
)
970 const char *endp
, *p
;
973 if (strstart(opt
, "hci", &endp
)) {
974 if (!*endp
|| *endp
== ',') {
976 if (!strstart(endp
, ",vlan=", 0))
979 return bt_hci_parse(opt
);
981 } else if (strstart(opt
, "vhci", &endp
)) {
982 if (!*endp
|| *endp
== ',') {
984 if (strstart(endp
, ",vlan=", &p
)) {
985 vlan
= strtol(p
, (char **) &endp
, 0);
987 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
991 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1000 } else if (strstart(opt
, "device:", &endp
))
1001 return !bt_device_add(endp
);
1003 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1007 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1009 /* FIXME: change this to true for 1.3 */
1010 if (qemu_opt_get_bool(opts
, "enable", false)) {
1011 #ifdef CONFIG_SECCOMP
1012 if (seccomp_start() < 0) {
1013 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1014 "failed to install seccomp syscall filter in the kernel");
1018 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1019 "sandboxing request but seccomp is not compiled into this build");
1027 static int parse_name(QemuOpts
*opts
, void *opaque
)
1029 const char *proc_name
;
1031 if (qemu_opt_get(opts
, "debug-threads")) {
1032 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1034 qemu_name
= qemu_opt_get(opts
, "guest");
1036 proc_name
= qemu_opt_get(opts
, "process");
1038 os_set_proc_name(proc_name
);
1044 bool usb_enabled(bool default_usb
)
1046 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1047 has_defaults
&& default_usb
);
1051 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1053 int fd
, dupfd
, flags
;
1055 const char *fd_opaque
= NULL
;
1057 fd
= qemu_opt_get_number(opts
, "fd", -1);
1058 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1059 fd_opaque
= qemu_opt_get(opts
, "opaque");
1062 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1063 "fd option is required and must be non-negative");
1067 if (fd
<= STDERR_FILENO
) {
1068 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1069 "fd cannot be a standard I/O stream");
1074 * All fds inherited across exec() necessarily have FD_CLOEXEC
1075 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1077 flags
= fcntl(fd
, F_GETFD
);
1078 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1079 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1080 "fd is not valid or already in use");
1085 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1086 "set option is required and must be non-negative");
1090 #ifdef F_DUPFD_CLOEXEC
1091 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1095 qemu_set_cloexec(dupfd
);
1099 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1100 "Error duplicating fd: %s", strerror(errno
));
1104 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1105 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1111 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1115 fd
= qemu_opt_get_number(opts
, "fd", -1);
1122 /***********************************************************/
1123 /* QEMU Block devices */
1125 #define HD_OPTS "media=disk"
1126 #define CDROM_OPTS "media=cdrom"
1128 #define PFLASH_OPTS ""
1132 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1134 BlockInterfaceType
*block_default_type
= opaque
;
1136 return drive_new(opts
, *block_default_type
) == NULL
;
1139 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1141 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1142 qemu_opt_set(opts
, "snapshot", "on");
1147 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1148 int index
, const char *optstr
)
1152 if (!enable
|| drive_get_by_index(type
, index
)) {
1156 opts
= drive_add(type
, index
, NULL
, optstr
);
1158 drive_enable_snapshot(opts
, NULL
);
1160 if (!drive_new(opts
, type
)) {
1165 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1167 boot_set_handler
= func
;
1168 boot_set_opaque
= opaque
;
1171 int qemu_boot_set(const char *boot_order
)
1173 if (!boot_set_handler
) {
1176 return boot_set_handler(boot_set_opaque
, boot_order
);
1179 static void validate_bootdevices(const char *devices
)
1181 /* We just do some generic consistency checks */
1185 for (p
= devices
; *p
!= '\0'; p
++) {
1186 /* Allowed boot devices are:
1187 * a-b: floppy disk drives
1188 * c-f: IDE disk drives
1189 * g-m: machine implementation dependent drives
1190 * n-p: network devices
1191 * It's up to each machine implementation to check if the given boot
1192 * devices match the actual hardware implementation and firmware
1195 if (*p
< 'a' || *p
> 'p') {
1196 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1199 if (bitmap
& (1 << (*p
- 'a'))) {
1200 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1203 bitmap
|= 1 << (*p
- 'a');
1207 static void restore_boot_order(void *opaque
)
1209 char *normal_boot_order
= opaque
;
1210 static int first
= 1;
1212 /* Restore boot order and remove ourselves after the first boot */
1218 qemu_boot_set(normal_boot_order
);
1220 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1221 g_free(normal_boot_order
);
1224 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1227 FWBootEntry
*node
, *i
;
1229 if (bootindex
< 0) {
1233 assert(dev
!= NULL
|| suffix
!= NULL
);
1235 node
= g_malloc0(sizeof(FWBootEntry
));
1236 node
->bootindex
= bootindex
;
1237 node
->suffix
= g_strdup(suffix
);
1240 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1241 if (i
->bootindex
== bootindex
) {
1242 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1244 } else if (i
->bootindex
< bootindex
) {
1247 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1250 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1253 DeviceState
*get_boot_device(uint32_t position
)
1255 uint32_t counter
= 0;
1256 FWBootEntry
*i
= NULL
;
1257 DeviceState
*res
= NULL
;
1259 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1260 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1261 if (counter
== position
) {
1272 * This function returns null terminated string that consist of new line
1273 * separated device paths.
1275 * memory pointed by "size" is assigned total length of the array in bytes
1278 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1284 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1285 char *devpath
= NULL
, *bootpath
;
1289 devpath
= qdev_get_fw_dev_path(i
->dev
);
1293 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1294 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1296 bootpath
= g_malloc(bootpathlen
);
1297 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1299 } else if (devpath
) {
1301 } else if (!ignore_suffixes
) {
1303 bootpath
= g_strdup(i
->suffix
);
1305 bootpath
= g_strdup("");
1309 list
[total
-1] = '\n';
1311 len
= strlen(bootpath
) + 1;
1312 list
= g_realloc(list
, total
+ len
);
1313 memcpy(&list
[total
], bootpath
, len
);
1320 if (boot_strict
&& *size
> 0) {
1321 list
[total
-1] = '\n';
1322 list
= g_realloc(list
, total
+ 5);
1323 memcpy(&list
[total
], "HALT", 5);
1329 static QemuOptsList qemu_smp_opts
= {
1331 .implied_opt_name
= "cpus",
1332 .merge_lists
= true,
1333 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1337 .type
= QEMU_OPT_NUMBER
,
1340 .type
= QEMU_OPT_NUMBER
,
1343 .type
= QEMU_OPT_NUMBER
,
1346 .type
= QEMU_OPT_NUMBER
,
1349 .type
= QEMU_OPT_NUMBER
,
1351 { /*End of list */ }
1355 static void smp_parse(QemuOpts
*opts
)
1359 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1360 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1361 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1362 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1364 /* compute missing values, prefer sockets over cores over threads */
1365 if (cpus
== 0 || sockets
== 0) {
1366 sockets
= sockets
> 0 ? sockets
: 1;
1367 cores
= cores
> 0 ? cores
: 1;
1368 threads
= threads
> 0 ? threads
: 1;
1370 cpus
= cores
* threads
* sockets
;
1374 threads
= threads
> 0 ? threads
: 1;
1375 cores
= cpus
/ (sockets
* threads
);
1377 threads
= cpus
/ (cores
* sockets
);
1381 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1384 smp_cores
= cores
> 0 ? cores
: 1;
1385 smp_threads
= threads
> 0 ? threads
: 1;
1389 if (max_cpus
== 0) {
1390 max_cpus
= smp_cpus
;
1393 if (max_cpus
> MAX_CPUMASK_BITS
) {
1394 fprintf(stderr
, "Unsupported number of maxcpus\n");
1397 if (max_cpus
< smp_cpus
) {
1398 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1404 static void configure_realtime(QemuOpts
*opts
)
1408 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1411 if (os_mlock() < 0) {
1412 fprintf(stderr
, "qemu: locking memory failed\n");
1419 static void configure_msg(QemuOpts
*opts
)
1421 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1424 /***********************************************************/
1427 static int usb_device_add(const char *devname
)
1429 USBDevice
*dev
= NULL
;
1430 #ifndef CONFIG_LINUX
1434 if (!usb_enabled(false)) {
1438 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1439 dev
= usbdevice_create(devname
);
1443 /* the other ones */
1444 #ifndef CONFIG_LINUX
1445 /* only the linux version is qdev-ified, usb-bsd still needs this */
1446 if (strstart(devname
, "host:", &p
)) {
1447 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1457 static int usb_device_del(const char *devname
)
1462 if (strstart(devname
, "host:", &p
)) {
1466 if (!usb_enabled(false)) {
1470 p
= strchr(devname
, '.');
1473 bus_num
= strtoul(devname
, NULL
, 0);
1474 addr
= strtoul(p
+ 1, NULL
, 0);
1476 return usb_device_delete_addr(bus_num
, addr
);
1479 static int usb_parse(const char *cmdline
)
1482 r
= usb_device_add(cmdline
);
1484 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1489 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1491 const char *devname
= qdict_get_str(qdict
, "devname");
1492 if (usb_device_add(devname
) < 0) {
1493 error_report("could not add USB device '%s'", devname
);
1497 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1499 const char *devname
= qdict_get_str(qdict
, "devname");
1500 if (usb_device_del(devname
) < 0) {
1501 error_report("could not delete USB device '%s'", devname
);
1505 /***********************************************************/
1506 /* PCMCIA/Cardbus */
1508 static struct pcmcia_socket_entry_s
{
1509 PCMCIASocket
*socket
;
1510 struct pcmcia_socket_entry_s
*next
;
1511 } *pcmcia_sockets
= 0;
1513 void pcmcia_socket_register(PCMCIASocket
*socket
)
1515 struct pcmcia_socket_entry_s
*entry
;
1517 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1518 entry
->socket
= socket
;
1519 entry
->next
= pcmcia_sockets
;
1520 pcmcia_sockets
= entry
;
1523 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1525 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1527 ptr
= &pcmcia_sockets
;
1528 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1529 if (entry
->socket
== socket
) {
1535 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1537 struct pcmcia_socket_entry_s
*iter
;
1539 if (!pcmcia_sockets
)
1540 monitor_printf(mon
, "No PCMCIA sockets\n");
1542 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1543 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1544 iter
->socket
->attached
? iter
->socket
->card_string
:
1548 /***********************************************************/
1549 /* machine registration */
1551 MachineState
*current_machine
;
1553 static void machine_class_init(ObjectClass
*oc
, void *data
)
1555 MachineClass
*mc
= MACHINE_CLASS(oc
);
1556 QEMUMachine
*qm
= data
;
1558 mc
->name
= qm
->name
;
1559 mc
->alias
= qm
->alias
;
1560 mc
->desc
= qm
->desc
;
1561 mc
->init
= qm
->init
;
1562 mc
->reset
= qm
->reset
;
1563 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1564 mc
->kvm_type
= qm
->kvm_type
;
1565 mc
->block_default_type
= qm
->block_default_type
;
1566 mc
->max_cpus
= qm
->max_cpus
;
1567 mc
->no_serial
= qm
->no_serial
;
1568 mc
->no_parallel
= qm
->no_parallel
;
1569 mc
->use_virtcon
= qm
->use_virtcon
;
1570 mc
->use_sclp
= qm
->use_sclp
;
1571 mc
->no_floppy
= qm
->no_floppy
;
1572 mc
->no_cdrom
= qm
->no_cdrom
;
1573 mc
->no_sdcard
= qm
->no_sdcard
;
1574 mc
->is_default
= qm
->is_default
;
1575 mc
->default_machine_opts
= qm
->default_machine_opts
;
1576 mc
->default_boot_order
= qm
->default_boot_order
;
1577 mc
->compat_props
= qm
->compat_props
;
1578 mc
->hw_version
= qm
->hw_version
;
1581 int qemu_register_machine(QEMUMachine
*m
)
1583 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1586 .parent
= TYPE_MACHINE
,
1587 .class_init
= machine_class_init
,
1588 .class_data
= (void *)m
,
1597 static MachineClass
*find_machine(const char *name
)
1599 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1600 MachineClass
*mc
= NULL
;
1602 for (el
= machines
; el
; el
= el
->next
) {
1603 MachineClass
*temp
= el
->data
;
1605 if (!strcmp(temp
->name
, name
)) {
1610 !strcmp(temp
->alias
, name
)) {
1616 g_slist_free(machines
);
1620 MachineClass
*find_default_machine(void)
1622 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1623 MachineClass
*mc
= NULL
;
1625 for (el
= machines
; el
; el
= el
->next
) {
1626 MachineClass
*temp
= el
->data
;
1628 if (temp
->is_default
) {
1634 g_slist_free(machines
);
1638 MachineInfoList
*qmp_query_machines(Error
**errp
)
1640 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1641 MachineInfoList
*mach_list
= NULL
;
1643 for (el
= machines
; el
; el
= el
->next
) {
1644 MachineClass
*mc
= el
->data
;
1645 MachineInfoList
*entry
;
1648 info
= g_malloc0(sizeof(*info
));
1649 if (mc
->is_default
) {
1650 info
->has_is_default
= true;
1651 info
->is_default
= true;
1655 info
->has_alias
= true;
1656 info
->alias
= g_strdup(mc
->alias
);
1659 info
->name
= g_strdup(mc
->name
);
1660 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1662 entry
= g_malloc0(sizeof(*entry
));
1663 entry
->value
= info
;
1664 entry
->next
= mach_list
;
1668 g_slist_free(machines
);
1672 /***********************************************************/
1673 /* main execution loop */
1675 struct vm_change_state_entry
{
1676 VMChangeStateHandler
*cb
;
1678 QLIST_ENTRY (vm_change_state_entry
) entries
;
1681 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1683 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1686 VMChangeStateEntry
*e
;
1688 e
= g_malloc0(sizeof (*e
));
1692 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1696 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1698 QLIST_REMOVE (e
, entries
);
1702 void vm_state_notify(int running
, RunState state
)
1704 VMChangeStateEntry
*e
;
1706 trace_vm_state_notify(running
, state
);
1708 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1709 e
->cb(e
->opaque
, running
, state
);
1713 /* reset/shutdown handler */
1715 typedef struct QEMUResetEntry
{
1716 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1717 QEMUResetHandler
*func
;
1721 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1722 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1723 static int reset_requested
;
1724 static int shutdown_requested
, shutdown_signal
= -1;
1725 static pid_t shutdown_pid
;
1726 static int powerdown_requested
;
1727 static int debug_requested
;
1728 static int suspend_requested
;
1729 static WakeupReason wakeup_reason
;
1730 static NotifierList powerdown_notifiers
=
1731 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1732 static NotifierList suspend_notifiers
=
1733 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1734 static NotifierList wakeup_notifiers
=
1735 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1736 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1738 int qemu_shutdown_requested_get(void)
1740 return shutdown_requested
;
1743 int qemu_reset_requested_get(void)
1745 return reset_requested
;
1748 static int qemu_shutdown_requested(void)
1750 int r
= shutdown_requested
;
1751 shutdown_requested
= 0;
1755 static void qemu_kill_report(void)
1757 if (!qtest_driver() && shutdown_signal
!= -1) {
1758 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1759 if (shutdown_pid
== 0) {
1760 /* This happens for eg ^C at the terminal, so it's worth
1761 * avoiding printing an odd message in that case.
1763 fputc('\n', stderr
);
1765 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1767 shutdown_signal
= -1;
1771 static int qemu_reset_requested(void)
1773 int r
= reset_requested
;
1774 reset_requested
= 0;
1778 static int qemu_suspend_requested(void)
1780 int r
= suspend_requested
;
1781 suspend_requested
= 0;
1785 static WakeupReason
qemu_wakeup_requested(void)
1787 return wakeup_reason
;
1790 static int qemu_powerdown_requested(void)
1792 int r
= powerdown_requested
;
1793 powerdown_requested
= 0;
1797 static int qemu_debug_requested(void)
1799 int r
= debug_requested
;
1800 debug_requested
= 0;
1804 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1806 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1809 re
->opaque
= opaque
;
1810 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1813 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1817 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1818 if (re
->func
== func
&& re
->opaque
== opaque
) {
1819 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1826 void qemu_devices_reset(void)
1828 QEMUResetEntry
*re
, *nre
;
1830 /* reset all devices */
1831 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1832 re
->func(re
->opaque
);
1836 void qemu_system_reset(bool report
)
1840 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1842 if (mc
&& mc
->reset
) {
1845 qemu_devices_reset();
1848 monitor_protocol_event(QEVENT_RESET
, NULL
);
1850 cpu_synchronize_all_post_reset();
1853 void qemu_system_reset_request(void)
1856 shutdown_requested
= 1;
1858 reset_requested
= 1;
1861 qemu_notify_event();
1864 static void qemu_system_suspend(void)
1867 notifier_list_notify(&suspend_notifiers
, NULL
);
1868 runstate_set(RUN_STATE_SUSPENDED
);
1869 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1872 void qemu_system_suspend_request(void)
1874 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1877 suspend_requested
= 1;
1879 qemu_notify_event();
1882 void qemu_register_suspend_notifier(Notifier
*notifier
)
1884 notifier_list_add(&suspend_notifiers
, notifier
);
1887 void qemu_system_wakeup_request(WakeupReason reason
)
1889 trace_system_wakeup_request(reason
);
1891 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1894 if (!(wakeup_reason_mask
& (1 << reason
))) {
1897 runstate_set(RUN_STATE_RUNNING
);
1898 wakeup_reason
= reason
;
1899 qemu_notify_event();
1902 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1905 wakeup_reason_mask
|= (1 << reason
);
1907 wakeup_reason_mask
&= ~(1 << reason
);
1911 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1913 notifier_list_add(&wakeup_notifiers
, notifier
);
1916 void qemu_system_killed(int signal
, pid_t pid
)
1918 shutdown_signal
= signal
;
1921 qemu_system_shutdown_request();
1924 void qemu_system_shutdown_request(void)
1926 shutdown_requested
= 1;
1927 qemu_notify_event();
1930 static void qemu_system_powerdown(void)
1932 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1933 notifier_list_notify(&powerdown_notifiers
, NULL
);
1936 void qemu_system_powerdown_request(void)
1938 powerdown_requested
= 1;
1939 qemu_notify_event();
1942 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1944 notifier_list_add(&powerdown_notifiers
, notifier
);
1947 void qemu_system_debug_request(void)
1949 debug_requested
= 1;
1950 qemu_notify_event();
1953 static bool main_loop_should_exit(void)
1956 if (qemu_debug_requested()) {
1957 vm_stop(RUN_STATE_DEBUG
);
1959 if (qemu_suspend_requested()) {
1960 qemu_system_suspend();
1962 if (qemu_shutdown_requested()) {
1964 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1966 vm_stop(RUN_STATE_SHUTDOWN
);
1971 if (qemu_reset_requested()) {
1973 cpu_synchronize_all_states();
1974 qemu_system_reset(VMRESET_REPORT
);
1976 if (runstate_needs_reset()) {
1977 runstate_set(RUN_STATE_PAUSED
);
1980 if (qemu_wakeup_requested()) {
1982 cpu_synchronize_all_states();
1983 qemu_system_reset(VMRESET_SILENT
);
1984 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1985 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1987 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1989 if (qemu_powerdown_requested()) {
1990 qemu_system_powerdown();
1992 if (qemu_vmstop_requested(&r
)) {
1998 static void main_loop(void)
2002 #ifdef CONFIG_PROFILER
2006 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2007 #ifdef CONFIG_PROFILER
2008 ti
= profile_getclock();
2010 last_io
= main_loop_wait(nonblocking
);
2011 #ifdef CONFIG_PROFILER
2012 dev_time
+= profile_getclock() - ti
;
2014 } while (!main_loop_should_exit());
2017 static void version(void)
2019 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2022 static void help(int exitcode
)
2025 printf("usage: %s [options] [disk_image]\n\n"
2026 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2027 error_get_progname());
2029 #define QEMU_OPTIONS_GENERATE_HELP
2030 #include "qemu-options-wrapper.h"
2032 printf("\nDuring emulation, the following keys are useful:\n"
2033 "ctrl-alt-f toggle full screen\n"
2034 "ctrl-alt-n switch to virtual console 'n'\n"
2035 "ctrl-alt toggle mouse and keyboard grab\n"
2037 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2042 #define HAS_ARG 0x0001
2044 typedef struct QEMUOption
{
2051 static const QEMUOption qemu_options
[] = {
2052 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2053 #define QEMU_OPTIONS_GENERATE_OPTIONS
2054 #include "qemu-options-wrapper.h"
2058 static bool vga_available(void)
2060 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2063 static bool cirrus_vga_available(void)
2065 return object_class_by_name("cirrus-vga")
2066 || object_class_by_name("isa-cirrus-vga");
2069 static bool vmware_vga_available(void)
2071 return object_class_by_name("vmware-svga");
2074 static bool qxl_vga_available(void)
2076 return object_class_by_name("qxl-vga");
2079 static bool tcx_vga_available(void)
2081 return object_class_by_name("SUNW,tcx");
2084 static bool cg3_vga_available(void)
2086 return object_class_by_name("cgthree");
2089 static void select_vgahw (const char *p
)
2093 assert(vga_interface_type
== VGA_NONE
);
2094 if (strstart(p
, "std", &opts
)) {
2095 if (vga_available()) {
2096 vga_interface_type
= VGA_STD
;
2098 fprintf(stderr
, "Error: standard VGA not available\n");
2101 } else if (strstart(p
, "cirrus", &opts
)) {
2102 if (cirrus_vga_available()) {
2103 vga_interface_type
= VGA_CIRRUS
;
2105 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2108 } else if (strstart(p
, "vmware", &opts
)) {
2109 if (vmware_vga_available()) {
2110 vga_interface_type
= VGA_VMWARE
;
2112 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2115 } else if (strstart(p
, "xenfb", &opts
)) {
2116 vga_interface_type
= VGA_XENFB
;
2117 } else if (strstart(p
, "qxl", &opts
)) {
2118 if (qxl_vga_available()) {
2119 vga_interface_type
= VGA_QXL
;
2121 fprintf(stderr
, "Error: QXL VGA not available\n");
2124 } else if (strstart(p
, "tcx", &opts
)) {
2125 if (tcx_vga_available()) {
2126 vga_interface_type
= VGA_TCX
;
2128 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2131 } else if (strstart(p
, "cg3", &opts
)) {
2132 if (cg3_vga_available()) {
2133 vga_interface_type
= VGA_CG3
;
2135 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2138 } else if (!strstart(p
, "none", &opts
)) {
2140 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2144 const char *nextopt
;
2146 if (strstart(opts
, ",retrace=", &nextopt
)) {
2148 if (strstart(opts
, "dumb", &nextopt
))
2149 vga_retrace_method
= VGA_RETRACE_DUMB
;
2150 else if (strstart(opts
, "precise", &nextopt
))
2151 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2152 else goto invalid_vga
;
2153 } else goto invalid_vga
;
2158 static DisplayType
select_display(const char *p
)
2161 DisplayType display
= DT_DEFAULT
;
2163 if (strstart(p
, "sdl", &opts
)) {
2167 const char *nextopt
;
2169 if (strstart(opts
, ",frame=", &nextopt
)) {
2171 if (strstart(opts
, "on", &nextopt
)) {
2173 } else if (strstart(opts
, "off", &nextopt
)) {
2176 goto invalid_sdl_args
;
2178 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2180 if (strstart(opts
, "on", &nextopt
)) {
2182 } else if (strstart(opts
, "off", &nextopt
)) {
2185 goto invalid_sdl_args
;
2187 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2189 if (strstart(opts
, "on", &nextopt
)) {
2191 } else if (strstart(opts
, "off", &nextopt
)) {
2194 goto invalid_sdl_args
;
2196 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2198 if (strstart(opts
, "on", &nextopt
)) {
2200 } else if (strstart(opts
, "off", &nextopt
)) {
2203 goto invalid_sdl_args
;
2207 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2213 fprintf(stderr
, "SDL support is disabled\n");
2216 } else if (strstart(p
, "vnc", &opts
)) {
2221 const char *nextopt
;
2223 if (strstart(opts
, "=", &nextopt
)) {
2224 vnc_display
= nextopt
;
2228 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2232 fprintf(stderr
, "VNC support is disabled\n");
2235 } else if (strstart(p
, "curses", &opts
)) {
2236 #ifdef CONFIG_CURSES
2237 display
= DT_CURSES
;
2239 fprintf(stderr
, "Curses support is disabled\n");
2242 } else if (strstart(p
, "gtk", &opts
)) {
2246 const char *nextopt
;
2248 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2250 if (strstart(opts
, "on", &nextopt
)) {
2251 grab_on_hover
= true;
2252 } else if (strstart(opts
, "off", &nextopt
)) {
2253 grab_on_hover
= false;
2255 goto invalid_gtk_args
;
2259 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2265 fprintf(stderr
, "GTK support is disabled\n");
2268 } else if (strstart(p
, "none", &opts
)) {
2271 fprintf(stderr
, "Unknown display type: %s\n", p
);
2278 static int balloon_parse(const char *arg
)
2282 if (strcmp(arg
, "none") == 0) {
2286 if (!strncmp(arg
, "virtio", 6)) {
2287 if (arg
[6] == ',') {
2288 /* have params -> parse them */
2289 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2293 /* create empty opts */
2294 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2297 qemu_opt_set(opts
, "driver", "virtio-balloon");
2304 char *qemu_find_file(int type
, const char *name
)
2310 /* Try the name as a straight path first */
2311 if (access(name
, R_OK
) == 0) {
2312 trace_load_file(name
, name
);
2313 return g_strdup(name
);
2317 case QEMU_FILE_TYPE_BIOS
:
2320 case QEMU_FILE_TYPE_KEYMAP
:
2321 subdir
= "keymaps/";
2327 for (i
= 0; i
< data_dir_idx
; i
++) {
2328 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2329 if (access(buf
, R_OK
) == 0) {
2330 trace_load_file(name
, buf
);
2338 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2340 return qdev_device_help(opts
);
2343 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2347 dev
= qdev_device_add(opts
);
2350 object_unref(OBJECT(dev
));
2354 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2356 Error
*local_err
= NULL
;
2358 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2360 error_report("%s", error_get_pretty(local_err
));
2361 error_free(local_err
);
2367 #ifdef CONFIG_VIRTFS
2368 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2371 ret
= qemu_fsdev_add(opts
);
2377 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2379 CharDriverState
*chr
;
2380 const char *chardev
;
2384 mode
= qemu_opt_get(opts
, "mode");
2388 if (strcmp(mode
, "readline") == 0) {
2389 flags
= MONITOR_USE_READLINE
;
2390 } else if (strcmp(mode
, "control") == 0) {
2391 flags
= MONITOR_USE_CONTROL
;
2393 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2397 if (qemu_opt_get_bool(opts
, "pretty", 0))
2398 flags
|= MONITOR_USE_PRETTY
;
2400 if (qemu_opt_get_bool(opts
, "default", 0))
2401 flags
|= MONITOR_IS_DEFAULT
;
2403 chardev
= qemu_opt_get(opts
, "chardev");
2404 chr
= qemu_chr_find(chardev
);
2406 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2410 qemu_chr_fe_claim_no_fail(chr
);
2411 monitor_init(chr
, flags
);
2415 static void monitor_parse(const char *optarg
, const char *mode
)
2417 static int monitor_device_index
= 0;
2423 if (strstart(optarg
, "chardev:", &p
)) {
2424 snprintf(label
, sizeof(label
), "%s", p
);
2426 snprintf(label
, sizeof(label
), "compat_monitor%d",
2427 monitor_device_index
);
2428 if (monitor_device_index
== 0) {
2431 opts
= qemu_chr_parse_compat(label
, optarg
);
2433 fprintf(stderr
, "parse error: %s\n", optarg
);
2438 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2440 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2443 qemu_opt_set(opts
, "mode", mode
);
2444 qemu_opt_set(opts
, "chardev", label
);
2446 qemu_opt_set(opts
, "default", "on");
2447 monitor_device_index
++;
2450 struct device_config
{
2452 DEV_USB
, /* -usbdevice */
2454 DEV_SERIAL
, /* -serial */
2455 DEV_PARALLEL
, /* -parallel */
2456 DEV_VIRTCON
, /* -virtioconsole */
2457 DEV_DEBUGCON
, /* -debugcon */
2458 DEV_GDB
, /* -gdb, -s */
2459 DEV_SCLP
, /* s390 sclp */
2461 const char *cmdline
;
2463 QTAILQ_ENTRY(device_config
) next
;
2466 static QTAILQ_HEAD(, device_config
) device_configs
=
2467 QTAILQ_HEAD_INITIALIZER(device_configs
);
2469 static void add_device_config(int type
, const char *cmdline
)
2471 struct device_config
*conf
;
2473 conf
= g_malloc0(sizeof(*conf
));
2475 conf
->cmdline
= cmdline
;
2476 loc_save(&conf
->loc
);
2477 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2480 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2482 struct device_config
*conf
;
2485 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2486 if (conf
->type
!= type
)
2488 loc_push_restore(&conf
->loc
);
2489 rc
= func(conf
->cmdline
);
2490 loc_pop(&conf
->loc
);
2497 static int serial_parse(const char *devname
)
2499 static int index
= 0;
2502 if (strcmp(devname
, "none") == 0)
2504 if (index
== MAX_SERIAL_PORTS
) {
2505 fprintf(stderr
, "qemu: too many serial ports\n");
2508 snprintf(label
, sizeof(label
), "serial%d", index
);
2509 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2510 if (!serial_hds
[index
]) {
2511 fprintf(stderr
, "qemu: could not connect serial device"
2512 " to character backend '%s'\n", devname
);
2519 static int parallel_parse(const char *devname
)
2521 static int index
= 0;
2524 if (strcmp(devname
, "none") == 0)
2526 if (index
== MAX_PARALLEL_PORTS
) {
2527 fprintf(stderr
, "qemu: too many parallel ports\n");
2530 snprintf(label
, sizeof(label
), "parallel%d", index
);
2531 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2532 if (!parallel_hds
[index
]) {
2533 fprintf(stderr
, "qemu: could not connect parallel device"
2534 " to character backend '%s'\n", devname
);
2541 static int virtcon_parse(const char *devname
)
2543 QemuOptsList
*device
= qemu_find_opts("device");
2544 static int index
= 0;
2546 QemuOpts
*bus_opts
, *dev_opts
;
2548 if (strcmp(devname
, "none") == 0)
2550 if (index
== MAX_VIRTIO_CONSOLES
) {
2551 fprintf(stderr
, "qemu: too many virtio consoles\n");
2555 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2556 if (arch_type
== QEMU_ARCH_S390X
) {
2557 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2559 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2562 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2563 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2565 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2566 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2567 if (!virtcon_hds
[index
]) {
2568 fprintf(stderr
, "qemu: could not connect virtio console"
2569 " to character backend '%s'\n", devname
);
2572 qemu_opt_set(dev_opts
, "chardev", label
);
2578 static int sclp_parse(const char *devname
)
2580 QemuOptsList
*device
= qemu_find_opts("device");
2581 static int index
= 0;
2585 if (strcmp(devname
, "none") == 0) {
2588 if (index
== MAX_SCLP_CONSOLES
) {
2589 fprintf(stderr
, "qemu: too many sclp consoles\n");
2593 assert(arch_type
== QEMU_ARCH_S390X
);
2595 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2596 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2598 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2599 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2600 if (!sclp_hds
[index
]) {
2601 fprintf(stderr
, "qemu: could not connect sclp console"
2602 " to character backend '%s'\n", devname
);
2605 qemu_opt_set(dev_opts
, "chardev", label
);
2611 static int debugcon_parse(const char *devname
)
2615 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2618 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2620 fprintf(stderr
, "qemu: already have a debugcon device\n");
2623 qemu_opt_set(opts
, "driver", "isa-debugcon");
2624 qemu_opt_set(opts
, "chardev", "debugcon");
2628 static MachineClass
*machine_parse(const char *name
)
2630 MachineClass
*mc
= NULL
;
2631 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2634 mc
= find_machine(name
);
2639 if (name
&& !is_help_option(name
)) {
2640 error_report("Unsupported machine type");
2641 error_printf("Use -machine help to list supported machines!\n");
2643 printf("Supported machines are:\n");
2644 for (el
= machines
; el
; el
= el
->next
) {
2645 MachineClass
*mc
= el
->data
;
2647 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2649 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2650 mc
->is_default
? " (default)" : "");
2654 g_slist_free(machines
);
2655 exit(!name
|| !is_help_option(name
));
2658 static int tcg_init(MachineClass
*mc
)
2660 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2665 const char *opt_name
;
2667 int (*available
)(void);
2668 int (*init
)(MachineClass
*mc
);
2671 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2672 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2673 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2674 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2677 static int configure_accelerator(MachineClass
*mc
)
2682 bool accel_initialised
= false;
2683 bool init_failed
= false;
2685 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2687 /* Use the default "accelerator", tcg */
2691 while (!accel_initialised
&& *p
!= '\0') {
2695 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2696 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2697 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2698 if (!accel_list
[i
].available()) {
2699 printf("%s not supported for this target\n",
2700 accel_list
[i
].name
);
2703 *(accel_list
[i
].allowed
) = true;
2704 ret
= accel_list
[i
].init(mc
);
2707 fprintf(stderr
, "failed to initialize %s: %s\n",
2710 *(accel_list
[i
].allowed
) = false;
2712 accel_initialised
= true;
2717 if (i
== ARRAY_SIZE(accel_list
)) {
2718 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2722 if (!accel_initialised
) {
2724 fprintf(stderr
, "No accelerator found!\n");
2730 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2733 return !accel_initialised
;
2736 void qemu_add_exit_notifier(Notifier
*notify
)
2738 notifier_list_add(&exit_notifiers
, notify
);
2741 void qemu_remove_exit_notifier(Notifier
*notify
)
2743 notifier_remove(notify
);
2746 static void qemu_run_exit_notifiers(void)
2748 notifier_list_notify(&exit_notifiers
, NULL
);
2751 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2753 notifier_list_add(&machine_init_done_notifiers
, notify
);
2756 static void qemu_run_machine_init_done_notifiers(void)
2758 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2761 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2762 const char **poptarg
, int *poptind
)
2764 const QEMUOption
*popt
;
2765 int optind
= *poptind
;
2766 char *r
= argv
[optind
];
2769 loc_set_cmdline(argv
, optind
, 1);
2771 /* Treat --foo the same as -foo. */
2774 popt
= qemu_options
;
2777 error_report("invalid option");
2780 if (!strcmp(popt
->name
, r
+ 1))
2784 if (popt
->flags
& HAS_ARG
) {
2785 if (optind
>= argc
) {
2786 error_report("requires an argument");
2789 optarg
= argv
[optind
++];
2790 loc_set_cmdline(argv
, optind
- 2, 2);
2801 static gpointer
malloc_and_trace(gsize n_bytes
)
2803 void *ptr
= malloc(n_bytes
);
2804 trace_g_malloc(n_bytes
, ptr
);
2808 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2810 void *ptr
= realloc(mem
, n_bytes
);
2811 trace_g_realloc(mem
, n_bytes
, ptr
);
2815 static void free_and_trace(gpointer mem
)
2821 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2823 Object
*obj
= OBJECT(opaque
);
2824 StringInputVisitor
*siv
;
2825 Error
*local_err
= NULL
;
2827 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2828 strcmp(name
, "type") == 0) {
2832 siv
= string_input_visitor_new(value
);
2833 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2834 string_input_visitor_cleanup(siv
);
2837 qerror_report_err(local_err
);
2838 error_free(local_err
);
2845 static int object_create(QemuOpts
*opts
, void *opaque
)
2854 ov
= opts_visitor_new(opts
);
2855 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2857 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2862 qdict_del(pdict
, "qom-type");
2863 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2868 qdict_del(pdict
, "id");
2869 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2874 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2878 visit_end_struct(opts_get_visitor(ov
), &err
);
2880 qmp_object_del(id
, NULL
);
2884 opts_visitor_cleanup(ov
);
2891 qerror_report_err(err
);
2897 int main(int argc
, char **argv
, char **envp
)
2900 int snapshot
, linux_boot
;
2901 const char *icount_option
= NULL
;
2902 const char *initrd_filename
;
2903 const char *kernel_filename
, *kernel_cmdline
;
2904 const char *boot_order
;
2906 int cyls
, heads
, secs
, translation
;
2907 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2908 QemuOptsList
*olist
;
2911 const char *loadvm
= NULL
;
2912 MachineClass
*machine_class
;
2913 const char *cpu_model
;
2914 const char *vga_model
= NULL
;
2915 const char *qtest_chrdev
= NULL
;
2916 const char *qtest_log
= NULL
;
2917 const char *pid_file
= NULL
;
2918 const char *incoming
= NULL
;
2920 int show_vnc_port
= 0;
2922 bool defconfig
= true;
2923 bool userconfig
= true;
2924 const char *log_mask
= NULL
;
2925 const char *log_file
= NULL
;
2926 GMemVTable mem_trace
= {
2927 .malloc
= malloc_and_trace
,
2928 .realloc
= realloc_and_trace
,
2929 .free
= free_and_trace
,
2931 const char *trace_events
= NULL
;
2932 const char *trace_file
= NULL
;
2933 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2935 ram_addr_t maxram_size
= default_ram_size
;
2936 uint64_t ram_slots
= 0;
2938 atexit(qemu_run_exit_notifiers
);
2939 error_set_progname(argv
[0]);
2940 qemu_init_exec_dir(argv
[0]);
2942 g_mem_set_vtable(&mem_trace
);
2944 module_call_init(MODULE_INIT_QOM
);
2946 qemu_add_opts(&qemu_drive_opts
);
2947 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2948 qemu_add_drive_opts(&qemu_common_drive_opts
);
2949 qemu_add_drive_opts(&qemu_drive_opts
);
2950 qemu_add_opts(&qemu_chardev_opts
);
2951 qemu_add_opts(&qemu_device_opts
);
2952 qemu_add_opts(&qemu_netdev_opts
);
2953 qemu_add_opts(&qemu_net_opts
);
2954 qemu_add_opts(&qemu_rtc_opts
);
2955 qemu_add_opts(&qemu_global_opts
);
2956 qemu_add_opts(&qemu_mon_opts
);
2957 qemu_add_opts(&qemu_trace_opts
);
2958 qemu_add_opts(&qemu_option_rom_opts
);
2959 qemu_add_opts(&qemu_machine_opts
);
2960 qemu_add_opts(&qemu_mem_opts
);
2961 qemu_add_opts(&qemu_smp_opts
);
2962 qemu_add_opts(&qemu_boot_opts
);
2963 qemu_add_opts(&qemu_sandbox_opts
);
2964 qemu_add_opts(&qemu_add_fd_opts
);
2965 qemu_add_opts(&qemu_object_opts
);
2966 qemu_add_opts(&qemu_tpmdev_opts
);
2967 qemu_add_opts(&qemu_realtime_opts
);
2968 qemu_add_opts(&qemu_msg_opts
);
2969 qemu_add_opts(&qemu_name_opts
);
2970 qemu_add_opts(&qemu_numa_opts
);
2974 rtc_clock
= QEMU_CLOCK_HOST
;
2976 QLIST_INIT (&vm_change_state_head
);
2977 os_setup_early_signal_handling();
2979 module_call_init(MODULE_INIT_MACHINE
);
2980 machine_class
= find_default_machine();
2982 ram_size
= default_ram_size
;
2984 cyls
= heads
= secs
= 0;
2985 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2987 for (i
= 0; i
< MAX_NODES
; i
++) {
2988 numa_info
[i
].node_mem
= 0;
2989 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2995 bdrv_init_with_whitelist();
2999 /* first pass of option parsing */
3001 while (optind
< argc
) {
3002 if (argv
[optind
][0] != '-') {
3006 const QEMUOption
*popt
;
3008 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3009 switch (popt
->index
) {
3010 case QEMU_OPTION_nodefconfig
:
3013 case QEMU_OPTION_nouserconfig
:
3022 ret
= qemu_read_default_config_files(userconfig
);
3028 /* second pass of option parsing */
3033 if (argv
[optind
][0] != '-') {
3034 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3036 const QEMUOption
*popt
;
3038 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3039 if (!(popt
->arch_mask
& arch_type
)) {
3040 printf("Option %s not supported for this target\n", popt
->name
);
3043 switch(popt
->index
) {
3045 machine_class
= machine_parse(optarg
);
3047 case QEMU_OPTION_no_kvm_irqchip
: {
3048 olist
= qemu_find_opts("machine");
3049 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3052 case QEMU_OPTION_cpu
:
3053 /* hw initialization will check this */
3056 case QEMU_OPTION_hda
:
3060 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3062 snprintf(buf
, sizeof(buf
),
3063 "%s,cyls=%d,heads=%d,secs=%d%s",
3064 HD_OPTS
, cyls
, heads
, secs
,
3065 translation
== BIOS_ATA_TRANSLATION_LBA
?
3067 translation
== BIOS_ATA_TRANSLATION_NONE
?
3068 ",trans=none" : "");
3069 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3072 case QEMU_OPTION_hdb
:
3073 case QEMU_OPTION_hdc
:
3074 case QEMU_OPTION_hdd
:
3075 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3078 case QEMU_OPTION_drive
:
3079 if (drive_def(optarg
) == NULL
) {
3083 case QEMU_OPTION_set
:
3084 if (qemu_set_option(optarg
) != 0)
3087 case QEMU_OPTION_global
:
3088 if (qemu_global_option(optarg
) != 0)
3091 case QEMU_OPTION_mtdblock
:
3092 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3094 case QEMU_OPTION_sd
:
3095 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3097 case QEMU_OPTION_pflash
:
3098 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3100 case QEMU_OPTION_snapshot
:
3103 case QEMU_OPTION_hdachs
:
3107 cyls
= strtol(p
, (char **)&p
, 0);
3108 if (cyls
< 1 || cyls
> 16383)
3113 heads
= strtol(p
, (char **)&p
, 0);
3114 if (heads
< 1 || heads
> 16)
3119 secs
= strtol(p
, (char **)&p
, 0);
3120 if (secs
< 1 || secs
> 63)
3124 if (!strcmp(p
, "large")) {
3125 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3126 } else if (!strcmp(p
, "rechs")) {
3127 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3128 } else if (!strcmp(p
, "none")) {
3129 translation
= BIOS_ATA_TRANSLATION_NONE
;
3130 } else if (!strcmp(p
, "lba")) {
3131 translation
= BIOS_ATA_TRANSLATION_LBA
;
3132 } else if (!strcmp(p
, "auto")) {
3133 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3137 } else if (*p
!= '\0') {
3139 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3142 if (hda_opts
!= NULL
) {
3144 snprintf(num
, sizeof(num
), "%d", cyls
);
3145 qemu_opt_set(hda_opts
, "cyls", num
);
3146 snprintf(num
, sizeof(num
), "%d", heads
);
3147 qemu_opt_set(hda_opts
, "heads", num
);
3148 snprintf(num
, sizeof(num
), "%d", secs
);
3149 qemu_opt_set(hda_opts
, "secs", num
);
3150 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3151 qemu_opt_set(hda_opts
, "trans", "large");
3152 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3153 qemu_opt_set(hda_opts
, "trans", "rechs");
3154 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3155 qemu_opt_set(hda_opts
, "trans", "lba");
3156 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3157 qemu_opt_set(hda_opts
, "trans", "none");
3162 case QEMU_OPTION_numa
:
3163 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3168 case QEMU_OPTION_display
:
3169 display_type
= select_display(optarg
);
3171 case QEMU_OPTION_nographic
:
3172 display_type
= DT_NOGRAPHIC
;
3174 case QEMU_OPTION_curses
:
3175 #ifdef CONFIG_CURSES
3176 display_type
= DT_CURSES
;
3178 fprintf(stderr
, "Curses support is disabled\n");
3182 case QEMU_OPTION_portrait
:
3183 graphic_rotate
= 90;
3185 case QEMU_OPTION_rotate
:
3186 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3187 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3188 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3190 "qemu: only 90, 180, 270 deg rotation is available\n");
3194 case QEMU_OPTION_kernel
:
3195 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3197 case QEMU_OPTION_initrd
:
3198 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3200 case QEMU_OPTION_append
:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3203 case QEMU_OPTION_dtb
:
3204 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3206 case QEMU_OPTION_cdrom
:
3207 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3209 case QEMU_OPTION_boot
:
3210 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3215 case QEMU_OPTION_fda
:
3216 case QEMU_OPTION_fdb
:
3217 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3220 case QEMU_OPTION_no_fd_bootchk
:
3223 case QEMU_OPTION_netdev
:
3224 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3228 case QEMU_OPTION_net
:
3229 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3233 #ifdef CONFIG_LIBISCSI
3234 case QEMU_OPTION_iscsi
:
3235 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3242 case QEMU_OPTION_tftp
:
3243 legacy_tftp_prefix
= optarg
;
3245 case QEMU_OPTION_bootp
:
3246 legacy_bootp_filename
= optarg
;
3248 case QEMU_OPTION_redir
:
3249 if (net_slirp_redir(optarg
) < 0)
3253 case QEMU_OPTION_bt
:
3254 add_device_config(DEV_BT
, optarg
);
3256 case QEMU_OPTION_audio_help
:
3260 case QEMU_OPTION_soundhw
:
3261 select_soundhw (optarg
);
3266 case QEMU_OPTION_version
:
3270 case QEMU_OPTION_m
: {
3272 const char *mem_str
;
3273 const char *maxmem_str
, *slots_str
;
3275 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3281 mem_str
= qemu_opt_get(opts
, "size");
3283 error_report("invalid -m option, missing 'size' option");
3287 error_report("missing 'size' option value");
3291 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3293 /* Fix up legacy suffix-less format */
3294 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3295 uint64_t overflow_check
= sz
;
3298 if ((sz
>> 20) != overflow_check
) {
3299 error_report("too large 'size' option value");
3304 /* backward compatibility behaviour for case "-m 0" */
3306 sz
= default_ram_size
;
3309 sz
= QEMU_ALIGN_UP(sz
, 8192);
3311 if (ram_size
!= sz
) {
3312 error_report("ram size too large");
3316 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3317 slots_str
= qemu_opt_get(opts
, "slots");
3318 if (maxmem_str
&& slots_str
) {
3321 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3322 if (sz
< ram_size
) {
3323 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3324 "(%" PRIu64
") <= initial memory ("
3325 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3329 slots
= qemu_opt_get_number(opts
, "slots", 0);
3330 if ((sz
> ram_size
) && !slots
) {
3331 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3332 "(%" PRIu64
") more than initial memory ("
3333 RAM_ADDR_FMT
") but no hotplug slots where "
3334 "specified\n", sz
, ram_size
);
3338 if ((sz
<= ram_size
) && slots
) {
3339 fprintf(stderr
, "qemu: invalid -m option value: %"
3340 PRIu64
" hotplug slots where specified but "
3341 "maxmem (%" PRIu64
") <= initial memory ("
3342 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3347 } else if ((!maxmem_str
&& slots_str
) ||
3348 (maxmem_str
&& !slots_str
)) {
3349 fprintf(stderr
, "qemu: invalid -m option value: missing "
3350 "'%s' option\n", slots_str
? "maxmem" : "slots");
3356 case QEMU_OPTION_tpmdev
:
3357 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3362 case QEMU_OPTION_mempath
:
3365 case QEMU_OPTION_mem_prealloc
:
3375 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3377 case QEMU_OPTION_gdb
:
3378 add_device_config(DEV_GDB
, optarg
);
3381 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3382 data_dir
[data_dir_idx
++] = optarg
;
3385 case QEMU_OPTION_bios
:
3386 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3388 case QEMU_OPTION_singlestep
:
3395 keyboard_layout
= optarg
;
3397 case QEMU_OPTION_localtime
:
3400 case QEMU_OPTION_vga
:
3409 w
= strtol(p
, (char **)&p
, 10);
3412 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3418 h
= strtol(p
, (char **)&p
, 10);
3423 depth
= strtol(p
, (char **)&p
, 10);
3424 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3425 depth
!= 24 && depth
!= 32)
3427 } else if (*p
== '\0') {
3428 depth
= graphic_depth
;
3435 graphic_depth
= depth
;
3438 case QEMU_OPTION_echr
:
3441 term_escape_char
= strtol(optarg
, &r
, 0);
3443 printf("Bad argument to echr\n");
3446 case QEMU_OPTION_monitor
:
3447 default_monitor
= 0;
3448 if (strncmp(optarg
, "none", 4)) {
3449 monitor_parse(optarg
, "readline");
3452 case QEMU_OPTION_qmp
:
3453 monitor_parse(optarg
, "control");
3454 default_monitor
= 0;
3456 case QEMU_OPTION_mon
:
3457 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3461 default_monitor
= 0;
3463 case QEMU_OPTION_chardev
:
3464 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3469 case QEMU_OPTION_fsdev
:
3470 olist
= qemu_find_opts("fsdev");
3472 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3475 opts
= qemu_opts_parse(olist
, optarg
, 1);
3480 case QEMU_OPTION_virtfs
: {
3483 const char *writeout
, *sock_fd
, *socket
;
3485 olist
= qemu_find_opts("virtfs");
3487 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3490 opts
= qemu_opts_parse(olist
, optarg
, 1);
3495 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3496 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3497 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3500 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3501 qemu_opt_get(opts
, "mount_tag"),
3504 fprintf(stderr
, "duplicate fsdev id: %s\n",
3505 qemu_opt_get(opts
, "mount_tag"));
3509 writeout
= qemu_opt_get(opts
, "writeout");
3511 #ifdef CONFIG_SYNC_FILE_RANGE
3512 qemu_opt_set(fsdev
, "writeout", writeout
);
3514 fprintf(stderr
, "writeout=immediate not supported on "
3519 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3520 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3521 qemu_opt_set(fsdev
, "security_model",
3522 qemu_opt_get(opts
, "security_model"));
3523 socket
= qemu_opt_get(opts
, "socket");
3525 qemu_opt_set(fsdev
, "socket", socket
);
3527 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3529 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3532 qemu_opt_set_bool(fsdev
, "readonly",
3533 qemu_opt_get_bool(opts
, "readonly", 0));
3534 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3536 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3537 qemu_opt_set(device
, "fsdev",
3538 qemu_opt_get(opts
, "mount_tag"));
3539 qemu_opt_set(device
, "mount_tag",
3540 qemu_opt_get(opts
, "mount_tag"));
3543 case QEMU_OPTION_virtfs_synth
: {
3547 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3550 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3553 qemu_opt_set(fsdev
, "fsdriver", "synth");
3555 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3557 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3558 qemu_opt_set(device
, "fsdev", "v_synth");
3559 qemu_opt_set(device
, "mount_tag", "v_synth");
3562 case QEMU_OPTION_serial
:
3563 add_device_config(DEV_SERIAL
, optarg
);
3565 if (strncmp(optarg
, "mon:", 4) == 0) {
3566 default_monitor
= 0;
3569 case QEMU_OPTION_watchdog
:
3572 "qemu: only one watchdog option may be given\n");
3577 case QEMU_OPTION_watchdog_action
:
3578 if (select_watchdog_action(optarg
) == -1) {
3579 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3583 case QEMU_OPTION_virtiocon
:
3584 add_device_config(DEV_VIRTCON
, optarg
);
3585 default_virtcon
= 0;
3586 if (strncmp(optarg
, "mon:", 4) == 0) {
3587 default_monitor
= 0;
3590 case QEMU_OPTION_parallel
:
3591 add_device_config(DEV_PARALLEL
, optarg
);
3592 default_parallel
= 0;
3593 if (strncmp(optarg
, "mon:", 4) == 0) {
3594 default_monitor
= 0;
3597 case QEMU_OPTION_debugcon
:
3598 add_device_config(DEV_DEBUGCON
, optarg
);
3600 case QEMU_OPTION_loadvm
:
3603 case QEMU_OPTION_full_screen
:
3606 case QEMU_OPTION_no_frame
:
3609 case QEMU_OPTION_alt_grab
:
3612 case QEMU_OPTION_ctrl_grab
:
3615 case QEMU_OPTION_no_quit
:
3618 case QEMU_OPTION_sdl
:
3620 display_type
= DT_SDL
;
3623 fprintf(stderr
, "SDL support is disabled\n");
3626 case QEMU_OPTION_pidfile
:
3629 case QEMU_OPTION_win2k_hack
:
3630 win2k_install_hack
= 1;
3632 case QEMU_OPTION_rtc_td_hack
: {
3633 static GlobalProperty slew_lost_ticks
[] = {
3635 .driver
= "mc146818rtc",
3636 .property
= "lost_tick_policy",
3639 { /* end of list */ }
3642 qdev_prop_register_global_list(slew_lost_ticks
);
3645 case QEMU_OPTION_acpitable
:
3646 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3650 do_acpitable_option(opts
);
3652 case QEMU_OPTION_smbios
:
3653 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3657 do_smbios_option(opts
);
3659 case QEMU_OPTION_enable_kvm
:
3660 olist
= qemu_find_opts("machine");
3661 qemu_opts_parse(olist
, "accel=kvm", 0);
3663 case QEMU_OPTION_machine
:
3664 olist
= qemu_find_opts("machine");
3665 opts
= qemu_opts_parse(olist
, optarg
, 1);
3669 optarg
= qemu_opt_get(opts
, "type");
3671 machine_class
= machine_parse(optarg
);
3674 case QEMU_OPTION_no_kvm
:
3675 olist
= qemu_find_opts("machine");
3676 qemu_opts_parse(olist
, "accel=tcg", 0);
3678 case QEMU_OPTION_no_kvm_pit
: {
3679 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3683 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3684 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3686 .driver
= "kvm-pit",
3687 .property
= "lost_tick_policy",
3690 { /* end of list */ }
3693 fprintf(stderr
, "Warning: option deprecated, use "
3694 "lost_tick_policy property of kvm-pit instead.\n");
3695 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3698 case QEMU_OPTION_usb
:
3699 olist
= qemu_find_opts("machine");
3700 qemu_opts_parse(olist
, "usb=on", 0);
3702 case QEMU_OPTION_usbdevice
:
3703 olist
= qemu_find_opts("machine");
3704 qemu_opts_parse(olist
, "usb=on", 0);
3705 add_device_config(DEV_USB
, optarg
);
3707 case QEMU_OPTION_device
:
3708 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3712 case QEMU_OPTION_smp
:
3713 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3717 case QEMU_OPTION_vnc
:
3720 vnc_display
= optarg
;
3722 fprintf(stderr
, "VNC support is disabled\n");
3726 case QEMU_OPTION_no_acpi
:
3729 case QEMU_OPTION_no_hpet
:
3732 case QEMU_OPTION_balloon
:
3733 if (balloon_parse(optarg
) < 0) {
3734 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3738 case QEMU_OPTION_no_reboot
:
3741 case QEMU_OPTION_no_shutdown
:
3744 case QEMU_OPTION_show_cursor
:
3747 case QEMU_OPTION_uuid
:
3748 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3749 fprintf(stderr
, "Fail to parse UUID string."
3750 " Wrong format.\n");
3753 qemu_uuid_set
= true;
3755 case QEMU_OPTION_option_rom
:
3756 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3757 fprintf(stderr
, "Too many option ROMs\n");
3760 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3764 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3765 option_rom
[nb_option_roms
].bootindex
=
3766 qemu_opt_get_number(opts
, "bootindex", -1);
3767 if (!option_rom
[nb_option_roms
].name
) {
3768 fprintf(stderr
, "Option ROM file is not specified\n");
3773 case QEMU_OPTION_semihosting
:
3774 semihosting_enabled
= 1;
3776 case QEMU_OPTION_tdf
:
3777 fprintf(stderr
, "Warning: user space PIT time drift fix "
3778 "is no longer supported.\n");
3780 case QEMU_OPTION_name
:
3781 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3786 case QEMU_OPTION_prom_env
:
3787 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3788 fprintf(stderr
, "Too many prom variables\n");
3791 prom_envs
[nb_prom_envs
] = optarg
;
3794 case QEMU_OPTION_old_param
:
3797 case QEMU_OPTION_clock
:
3798 /* Clock options no longer exist. Keep this option for
3799 * backward compatibility.
3802 case QEMU_OPTION_startdate
:
3803 configure_rtc_date_offset(optarg
, 1);
3805 case QEMU_OPTION_rtc
:
3806 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3810 configure_rtc(opts
);
3812 case QEMU_OPTION_tb_size
:
3813 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3814 if (tcg_tb_size
< 0) {
3818 case QEMU_OPTION_icount
:
3819 icount_option
= optarg
;
3821 case QEMU_OPTION_incoming
:
3823 runstate_set(RUN_STATE_INMIGRATE
);
3825 case QEMU_OPTION_nodefaults
:
3828 case QEMU_OPTION_xen_domid
:
3829 if (!(xen_available())) {
3830 printf("Option %s not supported for this target\n", popt
->name
);
3833 xen_domid
= atoi(optarg
);
3835 case QEMU_OPTION_xen_create
:
3836 if (!(xen_available())) {
3837 printf("Option %s not supported for this target\n", popt
->name
);
3840 xen_mode
= XEN_CREATE
;
3842 case QEMU_OPTION_xen_attach
:
3843 if (!(xen_available())) {
3844 printf("Option %s not supported for this target\n", popt
->name
);
3847 xen_mode
= XEN_ATTACH
;
3849 case QEMU_OPTION_trace
:
3851 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3855 trace_events
= qemu_opt_get(opts
, "events");
3856 trace_file
= qemu_opt_get(opts
, "file");
3859 case QEMU_OPTION_readconfig
:
3861 int ret
= qemu_read_config_file(optarg
);
3863 fprintf(stderr
, "read config %s: %s\n", optarg
,
3869 case QEMU_OPTION_spice
:
3870 olist
= qemu_find_opts("spice");
3872 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3875 opts
= qemu_opts_parse(olist
, optarg
, 0);
3881 case QEMU_OPTION_writeconfig
:
3884 if (strcmp(optarg
, "-") == 0) {
3887 fp
= fopen(optarg
, "w");
3889 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3893 qemu_config_write(fp
);
3899 case QEMU_OPTION_qtest
:
3900 qtest_chrdev
= optarg
;
3902 case QEMU_OPTION_qtest_log
:
3905 case QEMU_OPTION_sandbox
:
3906 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3911 case QEMU_OPTION_add_fd
:
3913 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3918 error_report("File descriptor passing is disabled on this "
3923 case QEMU_OPTION_object
:
3924 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3929 case QEMU_OPTION_realtime
:
3930 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3934 configure_realtime(opts
);
3936 case QEMU_OPTION_msg
:
3937 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3941 configure_msg(opts
);
3944 os_parse_cmd_args(popt
->index
, optarg
);
3952 if (qemu_init_main_loop()) {
3953 fprintf(stderr
, "qemu_init_main_loop failed\n");
3957 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3961 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3966 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3970 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3975 if (machine_class
== NULL
) {
3976 fprintf(stderr
, "No machine specified, and there is no default.\n"
3977 "Use -machine help to list supported machines!\n");
3981 cpu_exec_init_all();
3983 current_machine
= MACHINE(object_new(object_class_get_name(
3984 OBJECT_CLASS(machine_class
))));
3985 object_property_add_child(object_get_root(), "machine",
3986 OBJECT(current_machine
), &error_abort
);
3988 if (machine_class
->hw_version
) {
3989 qemu_set_version(machine_class
->hw_version
);
3992 if (qemu_opts_foreach(qemu_find_opts("object"),
3993 object_create
, NULL
, 0) != 0) {
3997 /* Init CPU def lists, based on config
3998 * - Must be called after all the qemu_read_config_file() calls
3999 * - Must be called before list_cpus()
4000 * - Must be called before machine->init()
4004 if (cpu_model
&& is_help_option(cpu_model
)) {
4005 list_cpus(stdout
, &fprintf
, cpu_model
);
4009 /* Open the logfile at this point, if necessary. We can't open the logfile
4010 * when encountering either of the logging options (-d or -D) because the
4011 * other one may be encountered later on the command line, changing the
4012 * location or level of logging.
4017 qemu_set_log_filename(log_file
);
4020 mask
= qemu_str_to_log_mask(log_mask
);
4022 qemu_print_log_usage(stdout
);
4028 if (!is_daemonized()) {
4029 if (!trace_init_backends(trace_events
, trace_file
)) {
4034 /* If no data_dir is specified then try to find it relative to the
4036 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4037 data_dir
[data_dir_idx
] = os_find_datadir();
4038 if (data_dir
[data_dir_idx
] != NULL
) {
4042 /* If all else fails use the install path specified when building. */
4043 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4044 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4047 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4049 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4050 if (smp_cpus
> machine_class
->max_cpus
) {
4051 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4052 "supported by machine `%s' (%d)\n", smp_cpus
,
4053 machine_class
->name
, machine_class
->max_cpus
);
4058 * Get the default machine options from the machine if it is not already
4059 * specified either by the configuration file or by the command line.
4061 if (machine_class
->default_machine_opts
) {
4062 qemu_opts_set_defaults(qemu_find_opts("machine"),
4063 machine_class
->default_machine_opts
, 0);
4066 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4067 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4069 if (!vga_model
&& !default_vga
) {
4070 vga_interface_type
= VGA_DEVICE
;
4072 if (!has_defaults
|| machine_class
->no_serial
) {
4075 if (!has_defaults
|| machine_class
->no_parallel
) {
4076 default_parallel
= 0;
4078 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4079 default_virtcon
= 0;
4081 if (!has_defaults
|| !machine_class
->use_sclp
) {
4084 if (!has_defaults
|| machine_class
->no_floppy
) {
4087 if (!has_defaults
|| machine_class
->no_cdrom
) {
4090 if (!has_defaults
|| machine_class
->no_sdcard
) {
4093 if (!has_defaults
) {
4094 default_monitor
= 0;
4099 if (is_daemonized()) {
4100 /* According to documentation and historically, -nographic redirects
4101 * serial port, parallel port and monitor to stdio, which does not work
4102 * with -daemonize. We can redirect these to null instead, but since
4103 * -nographic is legacy, let's just error out.
4104 * We disallow -nographic only if all other ports are not redirected
4105 * explicitly, to not break existing legacy setups which uses
4106 * -nographic _and_ redirects all ports explicitly - this is valid
4107 * usage, -nographic is just a no-op in this case.
4109 if (display_type
== DT_NOGRAPHIC
4110 && (default_parallel
|| default_serial
4111 || default_monitor
|| default_virtcon
)) {
4112 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4115 #ifdef CONFIG_CURSES
4116 if (display_type
== DT_CURSES
) {
4117 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4123 if (display_type
== DT_NOGRAPHIC
) {
4124 if (default_parallel
)
4125 add_device_config(DEV_PARALLEL
, "null");
4126 if (default_serial
&& default_monitor
) {
4127 add_device_config(DEV_SERIAL
, "mon:stdio");
4128 } else if (default_virtcon
&& default_monitor
) {
4129 add_device_config(DEV_VIRTCON
, "mon:stdio");
4130 } else if (default_sclp
&& default_monitor
) {
4131 add_device_config(DEV_SCLP
, "mon:stdio");
4134 add_device_config(DEV_SERIAL
, "stdio");
4135 if (default_virtcon
)
4136 add_device_config(DEV_VIRTCON
, "stdio");
4138 add_device_config(DEV_SCLP
, "stdio");
4140 if (default_monitor
)
4141 monitor_parse("stdio", "readline");
4145 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4146 if (default_parallel
)
4147 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4148 if (default_monitor
)
4149 monitor_parse("vc:80Cx24C", "readline");
4150 if (default_virtcon
)
4151 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4153 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4157 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4158 #if defined(CONFIG_GTK)
4159 display_type
= DT_GTK
;
4160 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4161 display_type
= DT_SDL
;
4162 #elif defined(CONFIG_VNC)
4163 vnc_display
= "localhost:0,to=99";
4166 display_type
= DT_NONE
;
4170 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4171 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4172 "for SDL, ignoring option\n");
4174 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4175 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4176 "ignoring option\n");
4179 #if defined(CONFIG_GTK)
4180 if (display_type
== DT_GTK
) {
4181 early_gtk_display_init();
4187 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4189 #ifdef CONFIG_VIRTFS
4190 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4195 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4200 /* store value for the future use */
4201 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4203 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4208 machine_opts
= qemu_get_machine_opts();
4209 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4211 object_unref(OBJECT(current_machine
));
4215 configure_accelerator(machine_class
);
4218 Error
*local_err
= NULL
;
4219 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4221 error_report("%s", error_get_pretty(local_err
));
4222 error_free(local_err
);
4227 machine_opts
= qemu_get_machine_opts();
4228 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4229 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4230 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4231 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4233 boot_order
= machine_class
->default_boot_order
;
4234 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4236 char *normal_boot_order
;
4237 const char *order
, *once
;
4239 order
= qemu_opt_get(opts
, "order");
4241 validate_bootdevices(order
);
4245 once
= qemu_opt_get(opts
, "once");
4247 validate_bootdevices(once
);
4248 normal_boot_order
= g_strdup(boot_order
);
4250 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4253 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4254 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4257 if (!kernel_cmdline
) {
4258 kernel_cmdline
= "";
4259 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4262 linux_boot
= (kernel_filename
!= NULL
);
4264 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4265 fprintf(stderr
, "-append only allowed with -kernel option\n");
4269 if (!linux_boot
&& initrd_filename
!= NULL
) {
4270 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4274 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4275 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4279 os_set_line_buffering();
4281 qemu_init_cpu_loop();
4282 qemu_mutex_lock_iothread();
4285 /* spice needs the timers to be initialized by this point */
4289 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4290 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4293 configure_icount(icount_option
);
4295 /* clean up network at qemu process termination */
4296 atexit(&net_cleanup
);
4298 if (net_init_clients() < 0) {
4303 if (tpm_init() < 0) {
4308 /* init the bluetooth world */
4309 if (foreach_device_config(DEV_BT
, bt_parse
))
4312 if (!xen_enabled()) {
4313 /* On 32-bit hosts, QEMU is limited by virtual address space */
4314 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4315 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4323 /* open the virtual block devices */
4325 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4326 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4327 &machine_class
->block_default_type
, 1) != 0) {
4331 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4333 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4334 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4336 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4343 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4347 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4349 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4351 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4353 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4356 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4359 /* If no default VGA is requested, the default is "none". */
4361 if (cirrus_vga_available()) {
4362 vga_model
= "cirrus";
4363 } else if (vga_available()) {
4368 select_vgahw(vga_model
);
4372 i
= select_watchdog(watchdog
);
4374 exit (i
== 1 ? 1 : 0);
4377 if (machine_class
->compat_props
) {
4378 qdev_prop_register_global_list(machine_class
->compat_props
);
4382 qdev_machine_init();
4384 current_machine
->ram_size
= ram_size
;
4385 current_machine
->maxram_size
= maxram_size
;
4386 current_machine
->ram_slots
= ram_slots
;
4387 current_machine
->boot_order
= boot_order
;
4388 current_machine
->cpu_model
= cpu_model
;
4390 machine_class
->init(current_machine
);
4394 cpu_synchronize_all_post_init();
4398 /* init USB devices */
4399 if (usb_enabled(false)) {
4400 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4404 /* init generic devices */
4405 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4408 net_check_clients();
4410 ds
= init_displaystate();
4412 /* init local displays */
4413 switch (display_type
) {
4415 (void)ds
; /* avoid warning if no display is configured */
4417 #if defined(CONFIG_CURSES)
4419 curses_display_init(ds
, full_screen
);
4422 #if defined(CONFIG_SDL)
4424 sdl_display_init(ds
, full_screen
, no_frame
);
4426 #elif defined(CONFIG_COCOA)
4428 cocoa_display_init(ds
, full_screen
);
4431 #if defined(CONFIG_GTK)
4433 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4440 /* must be after terminal init, SDL library changes signal handlers */
4441 os_setup_signal_handling();
4444 /* init remote displays */
4446 Error
*local_err
= NULL
;
4447 vnc_display_init(ds
);
4448 vnc_display_open(ds
, vnc_display
, &local_err
);
4449 if (local_err
!= NULL
) {
4450 error_report("Failed to start VNC server on `%s': %s",
4451 vnc_display
, error_get_pretty(local_err
));
4452 error_free(local_err
);
4456 if (show_vnc_port
) {
4457 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4463 qemu_spice_display_init();
4467 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4471 qdev_machine_creation_done();
4473 if (rom_load_all() != 0) {
4474 fprintf(stderr
, "rom loading failed\n");
4478 /* TODO: once all bus devices are qdevified, this should be done
4479 * when bus is created by qdev.c */
4480 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4481 qemu_run_machine_init_done_notifiers();
4483 /* Done notifiers can load ROMs */
4486 qemu_system_reset(VMRESET_SILENT
);
4488 if (load_vmstate(loadvm
) < 0) {
4493 qdev_prop_check_global();
4496 Error
*local_err
= NULL
;
4497 qemu_start_incoming_migration(incoming
, &local_err
);
4499 error_report("-incoming %s: %s", incoming
,
4500 error_get_pretty(local_err
));
4501 error_free(local_err
);
4504 } else if (autostart
) {
4510 if (is_daemonized()) {
4511 if (!trace_init_backends(trace_events
, trace_file
)) {