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
);
45 int main(int argc
, char **argv
)
47 return qemu_main(argc
, argv
);
50 #define main qemu_main
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
101 #include "fsdev/qemu-fsdev.h"
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir
[16];
129 static int data_dir_idx
;
130 const char *bios_name
= NULL
;
131 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
132 DisplayType display_type
= DT_DEFAULT
;
133 int request_opengl
= -1;
135 static int display_remote
;
136 const char* keyboard_layout
= NULL
;
138 const char *mem_path
= NULL
;
139 int mem_prealloc
= 0; /* force preallocation of physical target memory */
140 bool enable_mlock
= false;
142 NICInfo nd_table
[MAX_NICS
];
144 static int rtc_utc
= 1;
145 static int rtc_date_offset
= -1; /* -1 means no change */
146 QEMUClockType rtc_clock
;
147 int vga_interface_type
= VGA_NONE
;
148 static int full_screen
= 0;
149 static int no_frame
= 0;
152 static bool grab_on_hover
;
154 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
155 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
156 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
157 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
158 int win2k_install_hack
= 0;
164 int acpi_enabled
= 1;
167 static int no_reboot
;
170 int graphic_rotate
= 0;
171 const char *watchdog
;
172 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
174 int semihosting_enabled
= 0;
176 const char *qemu_name
;
179 unsigned int nb_prom_envs
= 0;
180 const char *prom_envs
[MAX_PROM_ENVS
];
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 int icount_align_option
;
189 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
190 * little-endian "wire format" described in the SMBIOS 2.6 specification.
192 uint8_t qemu_uuid
[16];
195 /* Trace unassigned memory or i/o accesses. */
196 bool trace_unassigned
;
198 static NotifierList exit_notifiers
=
199 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
201 static NotifierList machine_init_done_notifiers
=
202 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
206 enum xen_mode xen_mode
= XEN_EMULATE
;
208 static int has_defaults
= 1;
209 static int default_serial
= 1;
210 static int default_parallel
= 1;
211 static int default_virtcon
= 1;
212 static int default_sclp
= 1;
213 static int default_monitor
= 1;
214 static int default_floppy
= 1;
215 static int default_cdrom
= 1;
216 static int default_sdcard
= 1;
217 static int default_vga
= 1;
223 { .driver
= "isa-serial", .flag
= &default_serial
},
224 { .driver
= "isa-parallel", .flag
= &default_parallel
},
225 { .driver
= "isa-fdc", .flag
= &default_floppy
},
226 { .driver
= "ide-cd", .flag
= &default_cdrom
},
227 { .driver
= "ide-hd", .flag
= &default_cdrom
},
228 { .driver
= "ide-drive", .flag
= &default_cdrom
},
229 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
230 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
231 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
232 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
233 { .driver
= "VGA", .flag
= &default_vga
},
234 { .driver
= "isa-vga", .flag
= &default_vga
},
235 { .driver
= "cirrus-vga", .flag
= &default_vga
},
236 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
237 { .driver
= "vmware-svga", .flag
= &default_vga
},
238 { .driver
= "qxl-vga", .flag
= &default_vga
},
241 static QemuOptsList qemu_rtc_opts
= {
243 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
247 .type
= QEMU_OPT_STRING
,
250 .type
= QEMU_OPT_STRING
,
253 .type
= QEMU_OPT_STRING
,
255 { /* end of list */ }
259 static QemuOptsList qemu_sandbox_opts
= {
261 .implied_opt_name
= "enable",
262 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
266 .type
= QEMU_OPT_BOOL
,
268 { /* end of list */ }
272 static QemuOptsList qemu_trace_opts
= {
274 .implied_opt_name
= "trace",
275 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
279 .type
= QEMU_OPT_STRING
,
282 .type
= QEMU_OPT_STRING
,
284 { /* end of list */ }
288 static QemuOptsList qemu_option_rom_opts
= {
289 .name
= "option-rom",
290 .implied_opt_name
= "romfile",
291 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
295 .type
= QEMU_OPT_NUMBER
,
298 .type
= QEMU_OPT_STRING
,
300 { /* end of list */ }
304 static QemuOptsList qemu_machine_opts
= {
306 .implied_opt_name
= "type",
308 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
311 * no elements => accept any
312 * sanity checking will happen later
313 * when setting machine properties
319 static QemuOptsList qemu_boot_opts
= {
321 .implied_opt_name
= "order",
323 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
327 .type
= QEMU_OPT_STRING
,
330 .type
= QEMU_OPT_STRING
,
333 .type
= QEMU_OPT_BOOL
,
336 .type
= QEMU_OPT_STRING
,
338 .name
= "splash-time",
339 .type
= QEMU_OPT_STRING
,
341 .name
= "reboot-timeout",
342 .type
= QEMU_OPT_STRING
,
345 .type
= QEMU_OPT_BOOL
,
351 static QemuOptsList qemu_add_fd_opts
= {
353 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
357 .type
= QEMU_OPT_NUMBER
,
358 .help
= "file descriptor of which a duplicate is added to fd set",
361 .type
= QEMU_OPT_NUMBER
,
362 .help
= "ID of the fd set to add fd to",
365 .type
= QEMU_OPT_STRING
,
366 .help
= "free-form string used to describe fd",
368 { /* end of list */ }
372 static QemuOptsList qemu_object_opts
= {
374 .implied_opt_name
= "qom-type",
375 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
381 static QemuOptsList qemu_tpmdev_opts
= {
383 .implied_opt_name
= "type",
384 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
386 /* options are defined in the TPM backends */
387 { /* end of list */ }
391 static QemuOptsList qemu_realtime_opts
= {
393 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
397 .type
= QEMU_OPT_BOOL
,
399 { /* end of list */ }
403 static QemuOptsList qemu_msg_opts
= {
405 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
409 .type
= QEMU_OPT_BOOL
,
411 { /* end of list */ }
415 static QemuOptsList qemu_name_opts
= {
417 .implied_opt_name
= "guest",
419 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
423 .type
= QEMU_OPT_STRING
,
424 .help
= "Sets the name of the guest.\n"
425 "This name will be displayed in the SDL window caption.\n"
426 "The name will also be used for the VNC server",
429 .type
= QEMU_OPT_STRING
,
430 .help
= "Sets the name of the QEMU process, as shown in top etc",
432 .name
= "debug-threads",
433 .type
= QEMU_OPT_BOOL
,
434 .help
= "When enabled, name the individual threads; defaults off.\n"
435 "NOTE: The thread names are for debugging and not a\n"
438 { /* End of list */ }
442 static QemuOptsList qemu_mem_opts
= {
444 .implied_opt_name
= "size",
445 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
450 .type
= QEMU_OPT_SIZE
,
454 .type
= QEMU_OPT_NUMBER
,
458 .type
= QEMU_OPT_SIZE
,
460 { /* end of list */ }
464 static QemuOptsList qemu_icount_opts
= {
466 .implied_opt_name
= "shift",
468 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
472 .type
= QEMU_OPT_STRING
,
475 .type
= QEMU_OPT_BOOL
,
477 { /* end of list */ }
481 static QemuOptsList qemu_semihosting_config_opts
= {
482 .name
= "semihosting-config",
483 .implied_opt_name
= "enable",
484 .head
= QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts
.head
),
488 .type
= QEMU_OPT_BOOL
,
491 .type
= QEMU_OPT_STRING
,
493 { /* end of list */ }
498 * Get machine options
500 * Returns: machine options (never null).
502 QemuOpts
*qemu_get_machine_opts(void)
504 return qemu_find_opts_singleton("machine");
507 const char *qemu_get_vm_name(void)
512 static void res_free(void)
514 if (boot_splash_filedata
!= NULL
) {
515 g_free(boot_splash_filedata
);
516 boot_splash_filedata
= NULL
;
520 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
522 const char *driver
= qemu_opt_get(opts
, "driver");
527 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
528 if (strcmp(default_list
[i
].driver
, driver
) != 0)
530 *(default_list
[i
].flag
) = 0;
535 /***********************************************************/
538 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
540 /* We use RUN_STATE_MAX but any invalid value will do */
541 static RunState vmstop_requested
= RUN_STATE_MAX
;
542 static QemuMutex vmstop_lock
;
547 } RunStateTransition
;
549 static const RunStateTransition runstate_transitions_def
[] = {
551 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
552 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
554 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
555 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
557 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
558 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
560 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
561 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
563 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
564 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
566 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
567 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
569 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
570 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
571 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
573 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
574 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
576 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
578 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
579 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
580 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
581 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
582 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
583 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
584 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
585 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
586 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
587 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
589 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
591 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
592 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
594 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
595 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
596 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
597 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
599 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
600 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
603 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
605 { RUN_STATE_MAX
, RUN_STATE_MAX
},
608 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
610 bool runstate_check(RunState state
)
612 return current_run_state
== state
;
615 static void runstate_init(void)
617 const RunStateTransition
*p
;
619 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
620 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
621 runstate_valid_transitions
[p
->from
][p
->to
] = true;
624 qemu_mutex_init(&vmstop_lock
);
627 /* This function will abort() on invalid state transitions */
628 void runstate_set(RunState new_state
)
630 assert(new_state
< RUN_STATE_MAX
);
632 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
633 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
634 RunState_lookup
[current_run_state
],
635 RunState_lookup
[new_state
]);
638 trace_runstate_set(new_state
);
639 current_run_state
= new_state
;
642 int runstate_is_running(void)
644 return runstate_check(RUN_STATE_RUNNING
);
647 bool runstate_needs_reset(void)
649 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
650 runstate_check(RUN_STATE_SHUTDOWN
);
653 StatusInfo
*qmp_query_status(Error
**errp
)
655 StatusInfo
*info
= g_malloc0(sizeof(*info
));
657 info
->running
= runstate_is_running();
658 info
->singlestep
= singlestep
;
659 info
->status
= current_run_state
;
664 static bool qemu_vmstop_requested(RunState
*r
)
666 qemu_mutex_lock(&vmstop_lock
);
667 *r
= vmstop_requested
;
668 vmstop_requested
= RUN_STATE_MAX
;
669 qemu_mutex_unlock(&vmstop_lock
);
670 return *r
< RUN_STATE_MAX
;
673 void qemu_system_vmstop_request_prepare(void)
675 qemu_mutex_lock(&vmstop_lock
);
678 void qemu_system_vmstop_request(RunState state
)
680 vmstop_requested
= state
;
681 qemu_mutex_unlock(&vmstop_lock
);
689 qemu_vmstop_requested(&requested
);
690 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
694 /* Ensure that a STOP/RESUME pair of events is emitted if a
695 * vmstop request was pending. The BLOCK_IO_ERROR event, for
696 * example, according to documentation is always followed by
699 if (runstate_is_running()) {
700 qapi_event_send_stop(&error_abort
);
703 runstate_set(RUN_STATE_RUNNING
);
704 vm_state_notify(1, RUN_STATE_RUNNING
);
708 qapi_event_send_resume(&error_abort
);
712 /***********************************************************/
713 /* real time host monotonic timer */
715 static time_t qemu_time(void)
717 return qemu_clock_get_ms(QEMU_CLOCK_HOST
) / 1000;
720 /***********************************************************/
721 /* host time/date access */
722 void qemu_get_timedate(struct tm
*tm
, int offset
)
724 time_t ti
= qemu_time();
727 if (rtc_date_offset
== -1) {
731 localtime_r(&ti
, tm
);
733 ti
-= rtc_date_offset
;
738 int qemu_timedate_diff(struct tm
*tm
)
742 if (rtc_date_offset
== -1)
744 seconds
= mktimegm(tm
);
747 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
748 seconds
= mktime(&tmp
);
751 seconds
= mktimegm(tm
) + rtc_date_offset
;
753 return seconds
- qemu_time();
756 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
758 time_t rtc_start_date
;
761 if (!strcmp(startdate
, "now") && legacy
) {
762 rtc_date_offset
= -1;
764 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
772 } else if (sscanf(startdate
, "%d-%d-%d",
784 rtc_start_date
= mktimegm(&tm
);
785 if (rtc_start_date
== -1) {
787 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
788 "'2006-06-17T16:01:21' or '2006-06-17'\n");
791 rtc_date_offset
= qemu_time() - rtc_start_date
;
795 static void configure_rtc(QemuOpts
*opts
)
799 value
= qemu_opt_get(opts
, "base");
801 if (!strcmp(value
, "utc")) {
803 } else if (!strcmp(value
, "localtime")) {
806 configure_rtc_date_offset(value
, 0);
809 value
= qemu_opt_get(opts
, "clock");
811 if (!strcmp(value
, "host")) {
812 rtc_clock
= QEMU_CLOCK_HOST
;
813 } else if (!strcmp(value
, "rt")) {
814 rtc_clock
= QEMU_CLOCK_REALTIME
;
815 } else if (!strcmp(value
, "vm")) {
816 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
818 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
822 value
= qemu_opt_get(opts
, "driftfix");
824 if (!strcmp(value
, "slew")) {
825 static GlobalProperty slew_lost_ticks
[] = {
827 .driver
= "mc146818rtc",
828 .property
= "lost_tick_policy",
831 { /* end of list */ }
834 qdev_prop_register_global_list(slew_lost_ticks
);
835 } else if (!strcmp(value
, "none")) {
836 /* discard is default */
838 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
844 /***********************************************************/
845 /* Bluetooth support */
848 static struct HCIInfo
*hci_table
[MAX_NICS
];
850 struct HCIInfo
*qemu_next_hci(void)
852 if (cur_hci
== nb_hcis
)
855 return hci_table
[cur_hci
++];
858 static int bt_hci_parse(const char *str
)
863 if (nb_hcis
>= MAX_NICS
) {
864 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
877 bdaddr
.b
[5] = 0x56 + nb_hcis
;
878 hci
->bdaddr_set(hci
, bdaddr
.b
);
880 hci_table
[nb_hcis
++] = hci
;
885 static void bt_vhci_add(int vlan_id
)
887 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
890 fprintf(stderr
, "qemu: warning: adding a VHCI to "
891 "an empty scatternet %i\n", vlan_id
);
893 bt_vhci_init(bt_new_hci(vlan
));
896 static struct bt_device_s
*bt_device_add(const char *opt
)
898 struct bt_scatternet_s
*vlan
;
900 char *endp
= strstr(opt
, ",vlan=");
901 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
904 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
907 vlan_id
= strtol(endp
+ 6, &endp
, 0);
909 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
914 vlan
= qemu_find_bt_vlan(vlan_id
);
917 fprintf(stderr
, "qemu: warning: adding a slave device to "
918 "an empty scatternet %i\n", vlan_id
);
920 if (!strcmp(devname
, "keyboard"))
921 return bt_keyboard_init(vlan
);
923 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
927 static int bt_parse(const char *opt
)
929 const char *endp
, *p
;
932 if (strstart(opt
, "hci", &endp
)) {
933 if (!*endp
|| *endp
== ',') {
935 if (!strstart(endp
, ",vlan=", 0))
938 return bt_hci_parse(opt
);
940 } else if (strstart(opt
, "vhci", &endp
)) {
941 if (!*endp
|| *endp
== ',') {
943 if (strstart(endp
, ",vlan=", &p
)) {
944 vlan
= strtol(p
, (char **) &endp
, 0);
946 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
950 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
959 } else if (strstart(opt
, "device:", &endp
))
960 return !bt_device_add(endp
);
962 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
966 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
968 /* FIXME: change this to true for 1.3 */
969 if (qemu_opt_get_bool(opts
, "enable", false)) {
970 #ifdef CONFIG_SECCOMP
971 if (seccomp_start() < 0) {
972 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
973 "failed to install seccomp syscall filter in the kernel");
977 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
978 "sandboxing request but seccomp is not compiled into this build");
986 static int parse_name(QemuOpts
*opts
, void *opaque
)
988 const char *proc_name
;
990 if (qemu_opt_get(opts
, "debug-threads")) {
991 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
993 qemu_name
= qemu_opt_get(opts
, "guest");
995 proc_name
= qemu_opt_get(opts
, "process");
997 os_set_proc_name(proc_name
);
1003 bool defaults_enabled(void)
1005 return has_defaults
;
1008 bool usb_enabled(void)
1010 return machine_usb(current_machine
);
1014 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1016 int fd
, dupfd
, flags
;
1018 const char *fd_opaque
= NULL
;
1021 fd
= qemu_opt_get_number(opts
, "fd", -1);
1022 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1023 fd_opaque
= qemu_opt_get(opts
, "opaque");
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1027 "fd option is required and must be non-negative");
1031 if (fd
<= STDERR_FILENO
) {
1032 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1033 "fd cannot be a standard I/O stream");
1038 * All fds inherited across exec() necessarily have FD_CLOEXEC
1039 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1041 flags
= fcntl(fd
, F_GETFD
);
1042 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1043 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1044 "fd is not valid or already in use");
1049 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1050 "set option is required and must be non-negative");
1054 #ifdef F_DUPFD_CLOEXEC
1055 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1059 qemu_set_cloexec(dupfd
);
1063 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1064 "Error duplicating fd: %s", strerror(errno
));
1068 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1069 fdinfo
= monitor_fdset_add_fd(dupfd
, true, fdset_id
, !!fd_opaque
, fd_opaque
,
1076 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1080 fd
= qemu_opt_get_number(opts
, "fd", -1);
1087 /***********************************************************/
1088 /* QEMU Block devices */
1090 #define HD_OPTS "media=disk"
1091 #define CDROM_OPTS "media=cdrom"
1093 #define PFLASH_OPTS ""
1097 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1099 BlockInterfaceType
*block_default_type
= opaque
;
1101 return drive_new(opts
, *block_default_type
) == NULL
;
1104 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1106 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1107 qemu_opt_set(opts
, "snapshot", "on", &error_abort
);
1112 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1113 int index
, const char *optstr
)
1118 if (!enable
|| drive_get_by_index(type
, index
)) {
1122 opts
= drive_add(type
, index
, NULL
, optstr
);
1124 drive_enable_snapshot(opts
, NULL
);
1127 dinfo
= drive_new(opts
, type
);
1131 dinfo
->is_default
= true;
1135 static QemuOptsList qemu_smp_opts
= {
1137 .implied_opt_name
= "cpus",
1138 .merge_lists
= true,
1139 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1143 .type
= QEMU_OPT_NUMBER
,
1146 .type
= QEMU_OPT_NUMBER
,
1149 .type
= QEMU_OPT_NUMBER
,
1152 .type
= QEMU_OPT_NUMBER
,
1155 .type
= QEMU_OPT_NUMBER
,
1157 { /*End of list */ }
1161 static void smp_parse(QemuOpts
*opts
)
1165 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1166 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1167 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1168 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1170 /* compute missing values, prefer sockets over cores over threads */
1171 if (cpus
== 0 || sockets
== 0) {
1172 sockets
= sockets
> 0 ? sockets
: 1;
1173 cores
= cores
> 0 ? cores
: 1;
1174 threads
= threads
> 0 ? threads
: 1;
1176 cpus
= cores
* threads
* sockets
;
1178 } else if (cores
== 0) {
1179 threads
= threads
> 0 ? threads
: 1;
1180 cores
= cpus
/ (sockets
* threads
);
1181 } else if (threads
== 0) {
1182 threads
= cpus
/ (cores
* sockets
);
1183 } else if (sockets
* cores
* threads
< cpus
) {
1184 fprintf(stderr
, "cpu topology: error: "
1185 "sockets (%u) * cores (%u) * threads (%u) < "
1187 sockets
, cores
, threads
, cpus
);
1191 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1194 smp_cores
= cores
> 0 ? cores
: 1;
1195 smp_threads
= threads
> 0 ? threads
: 1;
1199 if (max_cpus
== 0) {
1200 max_cpus
= smp_cpus
;
1203 if (max_cpus
> MAX_CPUMASK_BITS
) {
1204 fprintf(stderr
, "Unsupported number of maxcpus\n");
1207 if (max_cpus
< smp_cpus
) {
1208 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1214 static void realtime_init(void)
1217 if (os_mlock() < 0) {
1218 fprintf(stderr
, "qemu: locking memory failed\n");
1225 static void configure_msg(QemuOpts
*opts
)
1227 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1230 /***********************************************************/
1233 static int usb_device_add(const char *devname
)
1235 USBDevice
*dev
= NULL
;
1236 #ifndef CONFIG_LINUX
1240 if (!usb_enabled()) {
1244 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1245 dev
= usbdevice_create(devname
);
1249 /* the other ones */
1250 #ifndef CONFIG_LINUX
1251 /* only the linux version is qdev-ified, usb-bsd still needs this */
1252 if (strstart(devname
, "host:", &p
)) {
1253 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1263 static int usb_device_del(const char *devname
)
1268 if (strstart(devname
, "host:", &p
)) {
1272 if (!usb_enabled()) {
1276 p
= strchr(devname
, '.');
1279 bus_num
= strtoul(devname
, NULL
, 0);
1280 addr
= strtoul(p
+ 1, NULL
, 0);
1282 return usb_device_delete_addr(bus_num
, addr
);
1285 static int usb_parse(const char *cmdline
)
1288 r
= usb_device_add(cmdline
);
1290 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1295 void hmp_usb_add(Monitor
*mon
, const QDict
*qdict
)
1297 const char *devname
= qdict_get_str(qdict
, "devname");
1298 if (usb_device_add(devname
) < 0) {
1299 error_report("could not add USB device '%s'", devname
);
1303 void hmp_usb_del(Monitor
*mon
, const QDict
*qdict
)
1305 const char *devname
= qdict_get_str(qdict
, "devname");
1306 if (usb_device_del(devname
) < 0) {
1307 error_report("could not delete USB device '%s'", devname
);
1311 /***********************************************************/
1312 /* machine registration */
1314 MachineState
*current_machine
;
1316 static void machine_class_init(ObjectClass
*oc
, void *data
)
1318 MachineClass
*mc
= MACHINE_CLASS(oc
);
1319 QEMUMachine
*qm
= data
;
1321 mc
->family
= qm
->family
;
1322 mc
->name
= qm
->name
;
1323 mc
->alias
= qm
->alias
;
1324 mc
->desc
= qm
->desc
;
1325 mc
->init
= qm
->init
;
1326 mc
->reset
= qm
->reset
;
1327 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1328 mc
->kvm_type
= qm
->kvm_type
;
1329 mc
->block_default_type
= qm
->block_default_type
;
1330 mc
->units_per_default_bus
= qm
->units_per_default_bus
;
1331 mc
->max_cpus
= qm
->max_cpus
;
1332 mc
->no_serial
= qm
->no_serial
;
1333 mc
->no_parallel
= qm
->no_parallel
;
1334 mc
->use_virtcon
= qm
->use_virtcon
;
1335 mc
->use_sclp
= qm
->use_sclp
;
1336 mc
->no_floppy
= qm
->no_floppy
;
1337 mc
->no_cdrom
= qm
->no_cdrom
;
1338 mc
->no_sdcard
= qm
->no_sdcard
;
1339 mc
->has_dynamic_sysbus
= qm
->has_dynamic_sysbus
;
1340 mc
->is_default
= qm
->is_default
;
1341 mc
->default_machine_opts
= qm
->default_machine_opts
;
1342 mc
->default_boot_order
= qm
->default_boot_order
;
1343 mc
->default_display
= qm
->default_display
;
1344 mc
->compat_props
= qm
->compat_props
;
1345 mc
->hw_version
= qm
->hw_version
;
1348 int qemu_register_machine(QEMUMachine
*m
)
1350 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1353 .parent
= TYPE_MACHINE
,
1354 .class_init
= machine_class_init
,
1355 .class_data
= (void *)m
,
1364 static MachineClass
*find_machine(const char *name
)
1366 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1367 MachineClass
*mc
= NULL
;
1369 for (el
= machines
; el
; el
= el
->next
) {
1370 MachineClass
*temp
= el
->data
;
1372 if (!strcmp(temp
->name
, name
)) {
1377 !strcmp(temp
->alias
, name
)) {
1383 g_slist_free(machines
);
1387 MachineClass
*find_default_machine(void)
1389 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1390 MachineClass
*mc
= NULL
;
1392 for (el
= machines
; el
; el
= el
->next
) {
1393 MachineClass
*temp
= el
->data
;
1395 if (temp
->is_default
) {
1401 g_slist_free(machines
);
1405 MachineInfoList
*qmp_query_machines(Error
**errp
)
1407 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1408 MachineInfoList
*mach_list
= NULL
;
1410 for (el
= machines
; el
; el
= el
->next
) {
1411 MachineClass
*mc
= el
->data
;
1412 MachineInfoList
*entry
;
1415 info
= g_malloc0(sizeof(*info
));
1416 if (mc
->is_default
) {
1417 info
->has_is_default
= true;
1418 info
->is_default
= true;
1422 info
->has_alias
= true;
1423 info
->alias
= g_strdup(mc
->alias
);
1426 info
->name
= g_strdup(mc
->name
);
1427 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1429 entry
= g_malloc0(sizeof(*entry
));
1430 entry
->value
= info
;
1431 entry
->next
= mach_list
;
1435 g_slist_free(machines
);
1439 static int machine_help_func(QemuOpts
*opts
, MachineState
*machine
)
1441 ObjectProperty
*prop
;
1443 if (!qemu_opt_has_help_opt(opts
)) {
1447 QTAILQ_FOREACH(prop
, &OBJECT(machine
)->properties
, node
) {
1452 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine
)->name
,
1453 prop
->name
, prop
->type
);
1454 if (prop
->description
) {
1455 error_printf(" (%s)\n", prop
->description
);
1464 /***********************************************************/
1465 /* main execution loop */
1467 struct vm_change_state_entry
{
1468 VMChangeStateHandler
*cb
;
1470 QLIST_ENTRY (vm_change_state_entry
) entries
;
1473 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1475 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1478 VMChangeStateEntry
*e
;
1480 e
= g_malloc0(sizeof (*e
));
1484 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1488 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1490 QLIST_REMOVE (e
, entries
);
1494 void vm_state_notify(int running
, RunState state
)
1496 VMChangeStateEntry
*e
, *next
;
1498 trace_vm_state_notify(running
, state
);
1500 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1501 e
->cb(e
->opaque
, running
, state
);
1505 /* reset/shutdown handler */
1507 typedef struct QEMUResetEntry
{
1508 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1509 QEMUResetHandler
*func
;
1513 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1514 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1515 static int reset_requested
;
1516 static int shutdown_requested
, shutdown_signal
= -1;
1517 static pid_t shutdown_pid
;
1518 static int powerdown_requested
;
1519 static int debug_requested
;
1520 static int suspend_requested
;
1521 static WakeupReason wakeup_reason
;
1522 static NotifierList powerdown_notifiers
=
1523 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1524 static NotifierList suspend_notifiers
=
1525 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1526 static NotifierList wakeup_notifiers
=
1527 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1528 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1530 int qemu_shutdown_requested_get(void)
1532 return shutdown_requested
;
1535 int qemu_reset_requested_get(void)
1537 return reset_requested
;
1540 static int qemu_shutdown_requested(void)
1542 return atomic_xchg(&shutdown_requested
, 0);
1545 static void qemu_kill_report(void)
1547 if (!qtest_driver() && shutdown_signal
!= -1) {
1548 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1549 if (shutdown_pid
== 0) {
1550 /* This happens for eg ^C at the terminal, so it's worth
1551 * avoiding printing an odd message in that case.
1553 fputc('\n', stderr
);
1555 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1557 shutdown_signal
= -1;
1561 static int qemu_reset_requested(void)
1563 int r
= reset_requested
;
1564 reset_requested
= 0;
1568 static int qemu_suspend_requested(void)
1570 int r
= suspend_requested
;
1571 suspend_requested
= 0;
1575 static WakeupReason
qemu_wakeup_requested(void)
1577 return wakeup_reason
;
1580 static int qemu_powerdown_requested(void)
1582 int r
= powerdown_requested
;
1583 powerdown_requested
= 0;
1587 static int qemu_debug_requested(void)
1589 int r
= debug_requested
;
1590 debug_requested
= 0;
1594 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1596 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1599 re
->opaque
= opaque
;
1600 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1603 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1607 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1608 if (re
->func
== func
&& re
->opaque
== opaque
) {
1609 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1616 void qemu_devices_reset(void)
1618 QEMUResetEntry
*re
, *nre
;
1620 /* reset all devices */
1621 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1622 re
->func(re
->opaque
);
1626 void qemu_system_reset(bool report
)
1630 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1632 if (mc
&& mc
->reset
) {
1635 qemu_devices_reset();
1638 qapi_event_send_reset(&error_abort
);
1640 cpu_synchronize_all_post_reset();
1643 void qemu_system_reset_request(void)
1646 shutdown_requested
= 1;
1648 reset_requested
= 1;
1651 qemu_notify_event();
1654 static void qemu_system_suspend(void)
1657 notifier_list_notify(&suspend_notifiers
, NULL
);
1658 runstate_set(RUN_STATE_SUSPENDED
);
1659 qapi_event_send_suspend(&error_abort
);
1662 void qemu_system_suspend_request(void)
1664 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1667 suspend_requested
= 1;
1669 qemu_notify_event();
1672 void qemu_register_suspend_notifier(Notifier
*notifier
)
1674 notifier_list_add(&suspend_notifiers
, notifier
);
1677 void qemu_system_wakeup_request(WakeupReason reason
)
1679 trace_system_wakeup_request(reason
);
1681 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1684 if (!(wakeup_reason_mask
& (1 << reason
))) {
1687 runstate_set(RUN_STATE_RUNNING
);
1688 wakeup_reason
= reason
;
1689 qemu_notify_event();
1692 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1695 wakeup_reason_mask
|= (1 << reason
);
1697 wakeup_reason_mask
&= ~(1 << reason
);
1701 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1703 notifier_list_add(&wakeup_notifiers
, notifier
);
1706 void qemu_system_killed(int signal
, pid_t pid
)
1708 shutdown_signal
= signal
;
1711 qemu_system_shutdown_request();
1714 void qemu_system_shutdown_request(void)
1716 trace_qemu_system_shutdown_request();
1717 shutdown_requested
= 1;
1718 qemu_notify_event();
1721 static void qemu_system_powerdown(void)
1723 qapi_event_send_powerdown(&error_abort
);
1724 notifier_list_notify(&powerdown_notifiers
, NULL
);
1727 void qemu_system_powerdown_request(void)
1729 trace_qemu_system_powerdown_request();
1730 powerdown_requested
= 1;
1731 qemu_notify_event();
1734 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1736 notifier_list_add(&powerdown_notifiers
, notifier
);
1739 void qemu_system_debug_request(void)
1741 debug_requested
= 1;
1742 qemu_notify_event();
1745 static bool main_loop_should_exit(void)
1748 if (qemu_debug_requested()) {
1749 vm_stop(RUN_STATE_DEBUG
);
1751 if (qemu_suspend_requested()) {
1752 qemu_system_suspend();
1754 if (qemu_shutdown_requested()) {
1756 qapi_event_send_shutdown(&error_abort
);
1758 vm_stop(RUN_STATE_SHUTDOWN
);
1763 if (qemu_reset_requested()) {
1765 cpu_synchronize_all_states();
1766 qemu_system_reset(VMRESET_REPORT
);
1768 if (runstate_needs_reset()) {
1769 runstate_set(RUN_STATE_PAUSED
);
1772 if (qemu_wakeup_requested()) {
1774 cpu_synchronize_all_states();
1775 qemu_system_reset(VMRESET_SILENT
);
1776 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1777 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1779 qapi_event_send_wakeup(&error_abort
);
1781 if (qemu_powerdown_requested()) {
1782 qemu_system_powerdown();
1784 if (qemu_vmstop_requested(&r
)) {
1790 static void main_loop(void)
1794 #ifdef CONFIG_PROFILER
1798 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1799 #ifdef CONFIG_PROFILER
1800 ti
= profile_getclock();
1802 last_io
= main_loop_wait(nonblocking
);
1803 #ifdef CONFIG_PROFILER
1804 dev_time
+= profile_getclock() - ti
;
1806 } while (!main_loop_should_exit());
1809 static void version(void)
1811 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1814 static void QEMU_NORETURN
help(int exitcode
)
1817 printf("usage: %s [options] [disk_image]\n\n"
1818 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1819 error_get_progname());
1821 #define QEMU_OPTIONS_GENERATE_HELP
1822 #include "qemu-options-wrapper.h"
1824 printf("\nDuring emulation, the following keys are useful:\n"
1825 "ctrl-alt-f toggle full screen\n"
1826 "ctrl-alt-n switch to virtual console 'n'\n"
1827 "ctrl-alt toggle mouse and keyboard grab\n"
1829 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1834 #define HAS_ARG 0x0001
1836 typedef struct QEMUOption
{
1843 static const QEMUOption qemu_options
[] = {
1844 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1845 #define QEMU_OPTIONS_GENERATE_OPTIONS
1846 #include "qemu-options-wrapper.h"
1850 static bool vga_available(void)
1852 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1855 static bool cirrus_vga_available(void)
1857 return object_class_by_name("cirrus-vga")
1858 || object_class_by_name("isa-cirrus-vga");
1861 static bool vmware_vga_available(void)
1863 return object_class_by_name("vmware-svga");
1866 static bool qxl_vga_available(void)
1868 return object_class_by_name("qxl-vga");
1871 static bool tcx_vga_available(void)
1873 return object_class_by_name("SUNW,tcx");
1876 static bool cg3_vga_available(void)
1878 return object_class_by_name("cgthree");
1881 static void select_vgahw (const char *p
)
1885 assert(vga_interface_type
== VGA_NONE
);
1886 if (strstart(p
, "std", &opts
)) {
1887 if (vga_available()) {
1888 vga_interface_type
= VGA_STD
;
1890 fprintf(stderr
, "Error: standard VGA not available\n");
1893 } else if (strstart(p
, "cirrus", &opts
)) {
1894 if (cirrus_vga_available()) {
1895 vga_interface_type
= VGA_CIRRUS
;
1897 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1900 } else if (strstart(p
, "vmware", &opts
)) {
1901 if (vmware_vga_available()) {
1902 vga_interface_type
= VGA_VMWARE
;
1904 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1907 } else if (strstart(p
, "xenfb", &opts
)) {
1908 vga_interface_type
= VGA_XENFB
;
1909 } else if (strstart(p
, "qxl", &opts
)) {
1910 if (qxl_vga_available()) {
1911 vga_interface_type
= VGA_QXL
;
1913 fprintf(stderr
, "Error: QXL VGA not available\n");
1916 } else if (strstart(p
, "tcx", &opts
)) {
1917 if (tcx_vga_available()) {
1918 vga_interface_type
= VGA_TCX
;
1920 fprintf(stderr
, "Error: TCX framebuffer not available\n");
1923 } else if (strstart(p
, "cg3", &opts
)) {
1924 if (cg3_vga_available()) {
1925 vga_interface_type
= VGA_CG3
;
1927 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
1930 } else if (!strstart(p
, "none", &opts
)) {
1932 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1936 const char *nextopt
;
1938 if (strstart(opts
, ",retrace=", &nextopt
)) {
1940 if (strstart(opts
, "dumb", &nextopt
))
1941 vga_retrace_method
= VGA_RETRACE_DUMB
;
1942 else if (strstart(opts
, "precise", &nextopt
))
1943 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1944 else goto invalid_vga
;
1945 } else goto invalid_vga
;
1950 static DisplayType
select_display(const char *p
)
1953 DisplayType display
= DT_DEFAULT
;
1955 if (strstart(p
, "sdl", &opts
)) {
1959 const char *nextopt
;
1961 if (strstart(opts
, ",frame=", &nextopt
)) {
1963 if (strstart(opts
, "on", &nextopt
)) {
1965 } else if (strstart(opts
, "off", &nextopt
)) {
1968 goto invalid_sdl_args
;
1970 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1972 if (strstart(opts
, "on", &nextopt
)) {
1974 } else if (strstart(opts
, "off", &nextopt
)) {
1977 goto invalid_sdl_args
;
1979 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1981 if (strstart(opts
, "on", &nextopt
)) {
1983 } else if (strstart(opts
, "off", &nextopt
)) {
1986 goto invalid_sdl_args
;
1988 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1990 if (strstart(opts
, "on", &nextopt
)) {
1992 } else if (strstart(opts
, "off", &nextopt
)) {
1995 goto invalid_sdl_args
;
1997 } else if (strstart(opts
, ",gl=", &nextopt
)) {
1999 if (strstart(opts
, "on", &nextopt
)) {
2001 } else if (strstart(opts
, "off", &nextopt
)) {
2004 goto invalid_sdl_args
;
2008 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2014 fprintf(stderr
, "SDL support is disabled\n");
2017 } else if (strstart(p
, "vnc", &opts
)) {
2020 if (vnc_parse_func(opts
+1) == NULL
) {
2024 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2028 fprintf(stderr
, "VNC support is disabled\n");
2031 } else if (strstart(p
, "curses", &opts
)) {
2032 #ifdef CONFIG_CURSES
2033 display
= DT_CURSES
;
2035 fprintf(stderr
, "Curses support is disabled\n");
2038 } else if (strstart(p
, "gtk", &opts
)) {
2042 const char *nextopt
;
2044 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2046 if (strstart(opts
, "on", &nextopt
)) {
2047 grab_on_hover
= true;
2048 } else if (strstart(opts
, "off", &nextopt
)) {
2049 grab_on_hover
= false;
2051 goto invalid_gtk_args
;
2055 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2061 fprintf(stderr
, "GTK support is disabled\n");
2064 } else if (strstart(p
, "none", &opts
)) {
2067 fprintf(stderr
, "Unknown display type: %s\n", p
);
2074 static int balloon_parse(const char *arg
)
2078 if (strcmp(arg
, "none") == 0) {
2082 if (!strncmp(arg
, "virtio", 6)) {
2083 if (arg
[6] == ',') {
2084 /* have params -> parse them */
2085 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2089 /* create empty opts */
2090 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2093 qemu_opt_set(opts
, "driver", "virtio-balloon", &error_abort
);
2100 #if defined(CONFIG_RUBY)
2101 /* Ruby interface for QEMU. See README.EXT for programming example. */
2105 static VALUE
qemu_open(int argc
, VALUE
*argv
, VALUE klass
)
2108 for (i
= 0; i
< argc
; i
++) {
2109 argv
[i
] = StringValue(argv
[i
]);
2110 fprintf(stderr
, "argv[%d] = %s\n", i
, StringValuePtr(argv
[i
]));
2112 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2113 //~ mode = 0666; /* default value */
2115 return INT2FIX(argc
);
2118 void Init_qemu(void)
2120 printf("%s\n", __func__
);
2121 VALUE cQEMU
= rb_define_class("QEMU", rb_cObject
);
2122 rb_define_singleton_method(cQEMU
, "run", qemu_open
, -1);
2127 #endif /* CONFIG_RUBY */
2129 #if defined(CONFIG_DLL)
2130 BOOL APIENTRY
DllMain(HINSTANCE hModule
, DWORD ul_reason_for_call
, LPVOID data
)
2134 #endif /* CONFIG_DLL */
2136 char *qemu_find_file(int type
, const char *name
)
2142 /* Try the name as a straight path first */
2143 if (access(name
, R_OK
) == 0) {
2144 trace_load_file(name
, name
);
2145 return g_strdup(name
);
2149 case QEMU_FILE_TYPE_BIOS
:
2152 case QEMU_FILE_TYPE_KEYMAP
:
2153 subdir
= "keymaps/";
2159 for (i
= 0; i
< data_dir_idx
; i
++) {
2160 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2161 if (access(buf
, R_OK
) == 0) {
2162 trace_load_file(name
, buf
);
2170 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2172 return qdev_device_help(opts
);
2175 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2179 dev
= qdev_device_add(opts
);
2182 object_unref(OBJECT(dev
));
2186 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2188 Error
*local_err
= NULL
;
2190 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2192 error_report_err(local_err
);
2198 #ifdef CONFIG_VIRTFS
2199 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2202 ret
= qemu_fsdev_add(opts
);
2208 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2210 CharDriverState
*chr
;
2211 const char *chardev
;
2215 mode
= qemu_opt_get(opts
, "mode");
2219 if (strcmp(mode
, "readline") == 0) {
2220 flags
= MONITOR_USE_READLINE
;
2221 } else if (strcmp(mode
, "control") == 0) {
2222 flags
= MONITOR_USE_CONTROL
;
2224 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2228 if (qemu_opt_get_bool(opts
, "pretty", 0))
2229 flags
|= MONITOR_USE_PRETTY
;
2231 if (qemu_opt_get_bool(opts
, "default", 0))
2232 flags
|= MONITOR_IS_DEFAULT
;
2234 chardev
= qemu_opt_get(opts
, "chardev");
2235 chr
= qemu_chr_find(chardev
);
2237 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2241 qemu_chr_fe_claim_no_fail(chr
);
2242 monitor_init(chr
, flags
);
2246 static void monitor_parse(const char *optarg
, const char *mode
, bool pretty
)
2248 static int monitor_device_index
= 0;
2249 Error
*local_err
= NULL
;
2255 if (strstart(optarg
, "chardev:", &p
)) {
2256 snprintf(label
, sizeof(label
), "%s", p
);
2258 snprintf(label
, sizeof(label
), "compat_monitor%d",
2259 monitor_device_index
);
2260 if (monitor_device_index
== 0) {
2263 opts
= qemu_chr_parse_compat(label
, optarg
);
2265 fprintf(stderr
, "parse error: %s\n", optarg
);
2270 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, &local_err
);
2272 error_report_err(local_err
);
2275 qemu_opt_set(opts
, "mode", mode
, &error_abort
);
2276 qemu_opt_set(opts
, "chardev", label
, &error_abort
);
2277 qemu_opt_set_bool(opts
, "pretty", pretty
, &error_abort
);
2279 qemu_opt_set(opts
, "default", "on", &error_abort
);
2280 monitor_device_index
++;
2283 struct device_config
{
2285 DEV_USB
, /* -usbdevice */
2287 DEV_SERIAL
, /* -serial */
2288 DEV_PARALLEL
, /* -parallel */
2289 DEV_VIRTCON
, /* -virtioconsole */
2290 DEV_DEBUGCON
, /* -debugcon */
2291 DEV_GDB
, /* -gdb, -s */
2292 DEV_SCLP
, /* s390 sclp */
2294 const char *cmdline
;
2296 QTAILQ_ENTRY(device_config
) next
;
2299 static QTAILQ_HEAD(, device_config
) device_configs
=
2300 QTAILQ_HEAD_INITIALIZER(device_configs
);
2302 static void add_device_config(int type
, const char *cmdline
)
2304 struct device_config
*conf
;
2306 conf
= g_malloc0(sizeof(*conf
));
2308 conf
->cmdline
= cmdline
;
2309 loc_save(&conf
->loc
);
2310 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2313 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2315 struct device_config
*conf
;
2318 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2319 if (conf
->type
!= type
)
2321 loc_push_restore(&conf
->loc
);
2322 rc
= func(conf
->cmdline
);
2323 loc_pop(&conf
->loc
);
2331 static int serial_parse(const char *devname
)
2333 static int index
= 0;
2336 if (strcmp(devname
, "none") == 0)
2338 if (index
== MAX_SERIAL_PORTS
) {
2339 fprintf(stderr
, "qemu: too many serial ports\n");
2342 snprintf(label
, sizeof(label
), "serial%d", index
);
2343 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2344 if (!serial_hds
[index
]) {
2345 fprintf(stderr
, "qemu: could not connect serial device"
2346 " to character backend '%s'\n", devname
);
2353 static int parallel_parse(const char *devname
)
2355 static int index
= 0;
2358 if (strcmp(devname
, "none") == 0)
2360 if (index
== MAX_PARALLEL_PORTS
) {
2361 fprintf(stderr
, "qemu: too many parallel ports\n");
2364 snprintf(label
, sizeof(label
), "parallel%d", index
);
2365 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2366 if (!parallel_hds
[index
]) {
2367 fprintf(stderr
, "qemu: could not connect parallel device"
2368 " to character backend '%s'\n", devname
);
2375 static int virtcon_parse(const char *devname
)
2377 QemuOptsList
*device
= qemu_find_opts("device");
2378 static int index
= 0;
2380 QemuOpts
*bus_opts
, *dev_opts
;
2382 if (strcmp(devname
, "none") == 0)
2384 if (index
== MAX_VIRTIO_CONSOLES
) {
2385 fprintf(stderr
, "qemu: too many virtio consoles\n");
2389 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2390 if (arch_type
== QEMU_ARCH_S390X
) {
2391 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390", &error_abort
);
2393 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci", &error_abort
);
2396 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2397 qemu_opt_set(dev_opts
, "driver", "virtconsole", &error_abort
);
2399 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2400 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2401 if (!virtcon_hds
[index
]) {
2402 fprintf(stderr
, "qemu: could not connect virtio console"
2403 " to character backend '%s'\n", devname
);
2406 qemu_opt_set(dev_opts
, "chardev", label
, &error_abort
);
2412 static int sclp_parse(const char *devname
)
2414 QemuOptsList
*device
= qemu_find_opts("device");
2415 static int index
= 0;
2419 if (strcmp(devname
, "none") == 0) {
2422 if (index
== MAX_SCLP_CONSOLES
) {
2423 fprintf(stderr
, "qemu: too many sclp consoles\n");
2427 assert(arch_type
== QEMU_ARCH_S390X
);
2429 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2430 qemu_opt_set(dev_opts
, "driver", "sclpconsole", &error_abort
);
2432 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2433 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2434 if (!sclp_hds
[index
]) {
2435 fprintf(stderr
, "qemu: could not connect sclp console"
2436 " to character backend '%s'\n", devname
);
2439 qemu_opt_set(dev_opts
, "chardev", label
, &error_abort
);
2445 static int debugcon_parse(const char *devname
)
2449 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2452 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2454 fprintf(stderr
, "qemu: already have a debugcon device\n");
2457 qemu_opt_set(opts
, "driver", "isa-debugcon", &error_abort
);
2458 qemu_opt_set(opts
, "chardev", "debugcon", &error_abort
);
2462 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2464 const MachineClass
*mc1
= a
, *mc2
= b
;
2467 if (mc1
->family
== NULL
) {
2468 if (mc2
->family
== NULL
) {
2469 /* Compare standalone machine types against each other; they sort
2470 * in increasing order.
2472 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2473 object_class_get_name(OBJECT_CLASS(mc2
)));
2476 /* Standalone machine types sort after families. */
2480 if (mc2
->family
== NULL
) {
2481 /* Families sort before standalone machine types. */
2485 /* Families sort between each other alphabetically increasingly. */
2486 res
= strcmp(mc1
->family
, mc2
->family
);
2491 /* Within the same family, machine types sort in decreasing order. */
2492 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2493 object_class_get_name(OBJECT_CLASS(mc1
)));
2496 static MachineClass
*machine_parse(const char *name
)
2498 MachineClass
*mc
= NULL
;
2499 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2502 mc
= find_machine(name
);
2505 g_slist_free(machines
);
2508 if (name
&& !is_help_option(name
)) {
2509 error_report("Unsupported machine type");
2510 error_printf("Use -machine help to list supported machines!\n");
2512 printf("Supported machines are:\n");
2513 machines
= g_slist_sort(machines
, machine_class_cmp
);
2514 for (el
= machines
; el
; el
= el
->next
) {
2515 MachineClass
*mc
= el
->data
;
2517 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2519 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2520 mc
->is_default
? " (default)" : "");
2524 g_slist_free(machines
);
2525 exit(!name
|| !is_help_option(name
));
2528 void qemu_add_exit_notifier(Notifier
*notify
)
2530 notifier_list_add(&exit_notifiers
, notify
);
2533 void qemu_remove_exit_notifier(Notifier
*notify
)
2535 notifier_remove(notify
);
2538 static void qemu_run_exit_notifiers(void)
2540 notifier_list_notify(&exit_notifiers
, NULL
);
2543 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2545 notifier_list_add(&machine_init_done_notifiers
, notify
);
2548 static void qemu_run_machine_init_done_notifiers(void)
2550 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2553 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2554 const char **poptarg
, int *poptind
)
2556 const QEMUOption
*popt
;
2557 int optind
= *poptind
;
2558 char *r
= argv
[optind
];
2561 loc_set_cmdline(argv
, optind
, 1);
2563 /* Treat --foo the same as -foo. */
2566 popt
= qemu_options
;
2569 error_report("invalid option");
2572 if (!strcmp(popt
->name
, r
+ 1))
2576 if (popt
->flags
& HAS_ARG
) {
2577 if (optind
>= argc
) {
2578 error_report("requires an argument");
2581 optarg
= argv
[optind
++];
2582 loc_set_cmdline(argv
, optind
- 2, 2);
2593 static gpointer
malloc_and_trace(gsize n_bytes
)
2595 void *ptr
= malloc(n_bytes
);
2596 trace_g_malloc(n_bytes
, ptr
);
2600 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2602 void *ptr
= realloc(mem
, n_bytes
);
2603 trace_g_realloc(mem
, n_bytes
, ptr
);
2607 static void free_and_trace(gpointer mem
)
2613 static int machine_set_property(const char *name
, const char *value
,
2616 Object
*obj
= OBJECT(opaque
);
2617 Error
*local_err
= NULL
;
2620 if (strcmp(name
, "type") == 0) {
2624 qom_name
= g_strdup(name
);
2632 object_property_parse(obj
, value
, qom_name
, &local_err
);
2636 error_report_err(local_err
);
2643 static int object_create(QemuOpts
*opts
, void *opaque
)
2652 ov
= opts_visitor_new(opts
);
2653 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2655 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2660 qdict_del(pdict
, "qom-type");
2661 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2666 qdict_del(pdict
, "id");
2667 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2672 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2676 visit_end_struct(opts_get_visitor(ov
), &err
);
2678 qmp_object_del(id
, NULL
);
2682 opts_visitor_cleanup(ov
);
2689 error_report_err(err
);
2695 static void set_memory_options(uint64_t *ram_slots
, ram_addr_t
*maxram_size
)
2698 const char *mem_str
;
2699 const char *maxmem_str
, *slots_str
;
2700 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2702 QemuOpts
*opts
= qemu_find_opts_singleton("memory");
2705 mem_str
= qemu_opt_get(opts
, "size");
2708 error_report("missing 'size' option value");
2712 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
2714 /* Fix up legacy suffix-less format */
2715 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
2716 uint64_t overflow_check
= sz
;
2719 if ((sz
>> 20) != overflow_check
) {
2720 error_report("too large 'size' option value");
2726 /* backward compatibility behaviour for case "-m 0" */
2728 sz
= default_ram_size
;
2731 sz
= QEMU_ALIGN_UP(sz
, 8192);
2733 if (ram_size
!= sz
) {
2734 error_report("ram size too large");
2738 /* store value for the future use */
2739 qemu_opt_set_number(opts
, "size", ram_size
, &error_abort
);
2740 *maxram_size
= ram_size
;
2742 maxmem_str
= qemu_opt_get(opts
, "maxmem");
2743 slots_str
= qemu_opt_get(opts
, "slots");
2744 if (maxmem_str
&& slots_str
) {
2747 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
2748 slots
= qemu_opt_get_number(opts
, "slots", 0);
2749 if (sz
< ram_size
) {
2750 error_report("invalid value of -m option maxmem: "
2751 "maximum memory size (0x%" PRIx64
") must be at least "
2752 "the initial memory size (0x" RAM_ADDR_FMT
")",
2755 } else if (sz
> ram_size
) {
2757 error_report("invalid value of -m option: maxmem was "
2758 "specified, but no hotplug slots were specified");
2762 error_report("invalid value of -m option maxmem: "
2763 "memory slots were specified but maximum memory size "
2764 "(0x%" PRIx64
") is equal to the initial memory size "
2765 "(0x" RAM_ADDR_FMT
")", sz
, ram_size
);
2771 } else if ((!maxmem_str
&& slots_str
) ||
2772 (maxmem_str
&& !slots_str
)) {
2773 error_report("invalid -m option value: missing "
2774 "'%s' option", slots_str
? "maxmem" : "slots");
2779 int main(int argc
, char **argv
)
2782 int snapshot
, linux_boot
;
2783 const char *initrd_filename
;
2784 const char *kernel_filename
, *kernel_cmdline
;
2785 const char *boot_order
= NULL
;
2786 const char *boot_once
= NULL
;
2788 int cyls
, heads
, secs
, translation
;
2789 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
, *icount_opts
= NULL
;
2790 QemuOptsList
*olist
;
2793 const char *loadvm
= NULL
;
2794 MachineClass
*machine_class
;
2795 const char *cpu_model
;
2796 const char *vga_model
= NULL
;
2797 const char *qtest_chrdev
= NULL
;
2798 const char *qtest_log
= NULL
;
2799 const char *pid_file
= NULL
;
2800 const char *incoming
= NULL
;
2802 int show_vnc_port
= 0;
2804 bool defconfig
= true;
2805 bool userconfig
= true;
2806 const char *log_mask
= NULL
;
2807 const char *log_file
= NULL
;
2808 GMemVTable mem_trace
= {
2809 .malloc
= malloc_and_trace
,
2810 .realloc
= realloc_and_trace
,
2811 .free
= free_and_trace
,
2813 const char *trace_events
= NULL
;
2814 const char *trace_file
= NULL
;
2815 ram_addr_t maxram_size
;
2816 uint64_t ram_slots
= 0;
2817 FILE *vmstate_dump_file
= NULL
;
2818 Error
*main_loop_err
= NULL
;
2820 qemu_init_cpu_loop();
2821 qemu_mutex_lock_iothread();
2823 atexit(qemu_run_exit_notifiers
);
2824 error_set_progname(argv
[0]);
2825 qemu_init_exec_dir(argv
[0]);
2827 g_mem_set_vtable(&mem_trace
);
2829 module_call_init(MODULE_INIT_QOM
);
2831 qemu_add_opts(&qemu_drive_opts
);
2832 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2833 qemu_add_drive_opts(&qemu_common_drive_opts
);
2834 qemu_add_drive_opts(&qemu_drive_opts
);
2835 qemu_add_opts(&qemu_chardev_opts
);
2836 qemu_add_opts(&qemu_device_opts
);
2837 qemu_add_opts(&qemu_netdev_opts
);
2838 qemu_add_opts(&qemu_net_opts
);
2839 qemu_add_opts(&qemu_rtc_opts
);
2840 qemu_add_opts(&qemu_global_opts
);
2841 qemu_add_opts(&qemu_mon_opts
);
2842 qemu_add_opts(&qemu_trace_opts
);
2843 qemu_add_opts(&qemu_option_rom_opts
);
2844 qemu_add_opts(&qemu_machine_opts
);
2845 qemu_add_opts(&qemu_mem_opts
);
2846 qemu_add_opts(&qemu_smp_opts
);
2847 qemu_add_opts(&qemu_boot_opts
);
2848 qemu_add_opts(&qemu_sandbox_opts
);
2849 qemu_add_opts(&qemu_add_fd_opts
);
2850 qemu_add_opts(&qemu_object_opts
);
2851 qemu_add_opts(&qemu_tpmdev_opts
);
2852 qemu_add_opts(&qemu_realtime_opts
);
2853 qemu_add_opts(&qemu_msg_opts
);
2854 qemu_add_opts(&qemu_name_opts
);
2855 qemu_add_opts(&qemu_numa_opts
);
2856 qemu_add_opts(&qemu_icount_opts
);
2857 qemu_add_opts(&qemu_semihosting_config_opts
);
2861 rtc_clock
= QEMU_CLOCK_HOST
;
2863 QLIST_INIT (&vm_change_state_head
);
2864 os_setup_early_signal_handling();
2866 module_call_init(MODULE_INIT_MACHINE
);
2868 optarg
= strrchr(argv
[0], '/');
2870 machine_class
= find_machine(optarg
+ 1);
2872 if (!machine_class
) {
2873 machine_class
= find_default_machine();
2877 cyls
= heads
= secs
= 0;
2878 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2882 bdrv_init_with_whitelist();
2886 /* first pass of option parsing */
2888 while (optind
< argc
) {
2889 if (argv
[optind
][0] != '-') {
2893 const QEMUOption
*popt
;
2895 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2896 switch (popt
->index
) {
2897 case QEMU_OPTION_nodefconfig
:
2900 case QEMU_OPTION_nouserconfig
:
2909 ret
= qemu_read_default_config_files(userconfig
);
2915 /* second pass of option parsing */
2920 if (argv
[optind
][0] != '-') {
2921 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2923 const QEMUOption
*popt
;
2925 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2926 if (!(popt
->arch_mask
& arch_type
)) {
2927 printf("Option %s not supported for this target\n", popt
->name
);
2930 switch(popt
->index
) {
2931 case QEMU_OPTION_no_kvm_irqchip
: {
2932 olist
= qemu_find_opts("machine");
2933 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2936 case QEMU_OPTION_cpu
:
2937 /* hw initialization will check this */
2940 case QEMU_OPTION_hda
:
2944 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2946 snprintf(buf
, sizeof(buf
),
2947 "%s,cyls=%d,heads=%d,secs=%d%s",
2948 HD_OPTS
, cyls
, heads
, secs
,
2949 translation
== BIOS_ATA_TRANSLATION_LBA
?
2951 translation
== BIOS_ATA_TRANSLATION_NONE
?
2952 ",trans=none" : "");
2953 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2956 case QEMU_OPTION_hdb
:
2957 case QEMU_OPTION_hdc
:
2958 case QEMU_OPTION_hdd
:
2959 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2962 case QEMU_OPTION_drive
:
2963 if (drive_def(optarg
) == NULL
) {
2967 case QEMU_OPTION_set
:
2968 if (qemu_set_option(optarg
) != 0)
2971 case QEMU_OPTION_global
:
2972 if (qemu_global_option(optarg
) != 0)
2975 case QEMU_OPTION_mtdblock
:
2976 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2978 case QEMU_OPTION_sd
:
2979 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2981 case QEMU_OPTION_pflash
:
2982 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2984 case QEMU_OPTION_snapshot
:
2987 case QEMU_OPTION_hdachs
:
2991 cyls
= strtol(p
, (char **)&p
, 0);
2992 if (cyls
< 1 || cyls
> 16383)
2997 heads
= strtol(p
, (char **)&p
, 0);
2998 if (heads
< 1 || heads
> 16)
3003 secs
= strtol(p
, (char **)&p
, 0);
3004 if (secs
< 1 || secs
> 63)
3008 if (!strcmp(p
, "large")) {
3009 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3010 } else if (!strcmp(p
, "rechs")) {
3011 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3012 } else if (!strcmp(p
, "none")) {
3013 translation
= BIOS_ATA_TRANSLATION_NONE
;
3014 } else if (!strcmp(p
, "lba")) {
3015 translation
= BIOS_ATA_TRANSLATION_LBA
;
3016 } else if (!strcmp(p
, "auto")) {
3017 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3021 } else if (*p
!= '\0') {
3023 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3026 if (hda_opts
!= NULL
) {
3027 qemu_opt_set_number(hda_opts
, "cyls", cyls
,
3029 qemu_opt_set_number(hda_opts
, "heads", heads
,
3031 qemu_opt_set_number(hda_opts
, "secs", secs
,
3033 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3034 qemu_opt_set(hda_opts
, "trans", "large",
3036 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3037 qemu_opt_set(hda_opts
, "trans", "rechs",
3039 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3040 qemu_opt_set(hda_opts
, "trans", "lba",
3042 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3043 qemu_opt_set(hda_opts
, "trans", "none",
3049 case QEMU_OPTION_numa
:
3050 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3055 case QEMU_OPTION_display
:
3056 display_type
= select_display(optarg
);
3058 case QEMU_OPTION_nographic
:
3059 display_type
= DT_NOGRAPHIC
;
3061 case QEMU_OPTION_curses
:
3062 #ifdef CONFIG_CURSES
3063 display_type
= DT_CURSES
;
3065 fprintf(stderr
, "Curses support is disabled\n");
3069 case QEMU_OPTION_portrait
:
3070 graphic_rotate
= 90;
3072 case QEMU_OPTION_rotate
:
3073 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3074 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3075 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3077 "qemu: only 90, 180, 270 deg rotation is available\n");
3081 case QEMU_OPTION_kernel
:
3082 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
,
3085 case QEMU_OPTION_initrd
:
3086 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
,
3089 case QEMU_OPTION_append
:
3090 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
,
3093 case QEMU_OPTION_dtb
:
3094 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
,
3097 case QEMU_OPTION_cdrom
:
3098 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3100 case QEMU_OPTION_boot
:
3101 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3106 case QEMU_OPTION_fda
:
3107 case QEMU_OPTION_fdb
:
3108 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3111 case QEMU_OPTION_no_fd_bootchk
:
3114 case QEMU_OPTION_netdev
:
3115 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3119 case QEMU_OPTION_net
:
3120 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3124 #ifdef CONFIG_LIBISCSI
3125 case QEMU_OPTION_iscsi
:
3126 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3133 case QEMU_OPTION_tftp
:
3134 legacy_tftp_prefix
= optarg
;
3136 case QEMU_OPTION_bootp
:
3137 legacy_bootp_filename
= optarg
;
3139 case QEMU_OPTION_redir
:
3140 if (net_slirp_redir(optarg
) < 0)
3144 case QEMU_OPTION_bt
:
3145 add_device_config(DEV_BT
, optarg
);
3147 case QEMU_OPTION_audio_help
:
3151 case QEMU_OPTION_soundhw
:
3152 select_soundhw (optarg
);
3157 case QEMU_OPTION_version
:
3162 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3169 case QEMU_OPTION_tpmdev
:
3170 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3175 case QEMU_OPTION_mempath
:
3178 case QEMU_OPTION_mem_prealloc
:
3188 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3190 case QEMU_OPTION_gdb
:
3191 add_device_config(DEV_GDB
, optarg
);
3194 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3195 data_dir
[data_dir_idx
++] = optarg
;
3198 case QEMU_OPTION_bios
:
3199 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
,
3202 case QEMU_OPTION_singlestep
:
3209 keyboard_layout
= optarg
;
3211 case QEMU_OPTION_localtime
:
3214 case QEMU_OPTION_vga
:
3223 w
= strtol(p
, (char **)&p
, 10);
3226 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3232 h
= strtol(p
, (char **)&p
, 10);
3237 depth
= strtol(p
, (char **)&p
, 10);
3238 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3239 depth
!= 24 && depth
!= 32)
3241 } else if (*p
== '\0') {
3242 depth
= graphic_depth
;
3249 graphic_depth
= depth
;
3252 case QEMU_OPTION_echr
:
3255 term_escape_char
= strtol(optarg
, &r
, 0);
3257 printf("Bad argument to echr\n");
3260 case QEMU_OPTION_monitor
:
3261 default_monitor
= 0;
3262 if (strncmp(optarg
, "none", 4)) {
3263 monitor_parse(optarg
, "readline", false);
3266 case QEMU_OPTION_qmp
:
3267 monitor_parse(optarg
, "control", false);
3268 default_monitor
= 0;
3270 case QEMU_OPTION_qmp_pretty
:
3271 monitor_parse(optarg
, "control", true);
3272 default_monitor
= 0;
3274 case QEMU_OPTION_mon
:
3275 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3279 default_monitor
= 0;
3281 case QEMU_OPTION_chardev
:
3282 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3287 case QEMU_OPTION_fsdev
:
3288 olist
= qemu_find_opts("fsdev");
3290 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3293 opts
= qemu_opts_parse(olist
, optarg
, 1);
3298 case QEMU_OPTION_virtfs
: {
3301 const char *writeout
, *sock_fd
, *socket
;
3303 olist
= qemu_find_opts("virtfs");
3305 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3308 opts
= qemu_opts_parse(olist
, optarg
, 1);
3313 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3314 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3315 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3318 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3319 qemu_opt_get(opts
, "mount_tag"),
3322 fprintf(stderr
, "duplicate fsdev id: %s\n",
3323 qemu_opt_get(opts
, "mount_tag"));
3327 writeout
= qemu_opt_get(opts
, "writeout");
3329 #ifdef CONFIG_SYNC_FILE_RANGE
3330 qemu_opt_set(fsdev
, "writeout", writeout
, &error_abort
);
3332 fprintf(stderr
, "writeout=immediate not supported on "
3337 qemu_opt_set(fsdev
, "fsdriver",
3338 qemu_opt_get(opts
, "fsdriver"), &error_abort
);
3339 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"),
3341 qemu_opt_set(fsdev
, "security_model",
3342 qemu_opt_get(opts
, "security_model"),
3344 socket
= qemu_opt_get(opts
, "socket");
3346 qemu_opt_set(fsdev
, "socket", socket
, &error_abort
);
3348 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3350 qemu_opt_set(fsdev
, "sock_fd", sock_fd
, &error_abort
);
3353 qemu_opt_set_bool(fsdev
, "readonly",
3354 qemu_opt_get_bool(opts
, "readonly", 0),
3356 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3358 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3359 qemu_opt_set(device
, "fsdev",
3360 qemu_opt_get(opts
, "mount_tag"), &error_abort
);
3361 qemu_opt_set(device
, "mount_tag",
3362 qemu_opt_get(opts
, "mount_tag"), &error_abort
);
3365 case QEMU_OPTION_virtfs_synth
: {
3369 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3372 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3375 qemu_opt_set(fsdev
, "fsdriver", "synth", &error_abort
);
3377 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3379 qemu_opt_set(device
, "driver", "virtio-9p-pci", &error_abort
);
3380 qemu_opt_set(device
, "fsdev", "v_synth", &error_abort
);
3381 qemu_opt_set(device
, "mount_tag", "v_synth", &error_abort
);
3384 case QEMU_OPTION_serial
:
3385 add_device_config(DEV_SERIAL
, optarg
);
3387 if (strncmp(optarg
, "mon:", 4) == 0) {
3388 default_monitor
= 0;
3391 case QEMU_OPTION_watchdog
:
3394 "qemu: only one watchdog option may be given\n");
3399 case QEMU_OPTION_watchdog_action
:
3400 if (select_watchdog_action(optarg
) == -1) {
3401 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3405 case QEMU_OPTION_virtiocon
:
3406 add_device_config(DEV_VIRTCON
, optarg
);
3407 default_virtcon
= 0;
3408 if (strncmp(optarg
, "mon:", 4) == 0) {
3409 default_monitor
= 0;
3412 case QEMU_OPTION_parallel
:
3413 add_device_config(DEV_PARALLEL
, optarg
);
3414 default_parallel
= 0;
3415 if (strncmp(optarg
, "mon:", 4) == 0) {
3416 default_monitor
= 0;
3419 case QEMU_OPTION_debugcon
:
3420 add_device_config(DEV_DEBUGCON
, optarg
);
3422 case QEMU_OPTION_loadvm
:
3425 case QEMU_OPTION_full_screen
:
3428 case QEMU_OPTION_no_frame
:
3431 case QEMU_OPTION_alt_grab
:
3434 case QEMU_OPTION_ctrl_grab
:
3437 case QEMU_OPTION_no_quit
:
3440 case QEMU_OPTION_sdl
:
3442 display_type
= DT_SDL
;
3445 fprintf(stderr
, "SDL support is disabled\n");
3448 case QEMU_OPTION_pidfile
:
3451 case QEMU_OPTION_win2k_hack
:
3452 win2k_install_hack
= 1;
3454 case QEMU_OPTION_rtc_td_hack
: {
3455 static GlobalProperty slew_lost_ticks
[] = {
3457 .driver
= "mc146818rtc",
3458 .property
= "lost_tick_policy",
3461 { /* end of list */ }
3464 qdev_prop_register_global_list(slew_lost_ticks
);
3467 case QEMU_OPTION_acpitable
:
3468 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3472 do_acpitable_option(opts
);
3474 case QEMU_OPTION_smbios
:
3475 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3479 do_smbios_option(opts
);
3481 case QEMU_OPTION_enable_kvm
:
3482 olist
= qemu_find_opts("machine");
3483 qemu_opts_parse(olist
, "accel=kvm", 0);
3486 case QEMU_OPTION_machine
:
3487 olist
= qemu_find_opts("machine");
3488 opts
= qemu_opts_parse(olist
, optarg
, 1);
3493 case QEMU_OPTION_no_kvm
:
3494 olist
= qemu_find_opts("machine");
3495 qemu_opts_parse(olist
, "accel=tcg", 0);
3497 case QEMU_OPTION_no_kvm_pit
: {
3498 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3502 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3503 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3505 .driver
= "kvm-pit",
3506 .property
= "lost_tick_policy",
3509 { /* end of list */ }
3512 fprintf(stderr
, "Warning: option deprecated, use "
3513 "lost_tick_policy property of kvm-pit instead.\n");
3514 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3517 case QEMU_OPTION_usb
:
3518 olist
= qemu_find_opts("machine");
3519 qemu_opts_parse(olist
, "usb=on", 0);
3521 case QEMU_OPTION_usbdevice
:
3522 olist
= qemu_find_opts("machine");
3523 qemu_opts_parse(olist
, "usb=on", 0);
3524 add_device_config(DEV_USB
, optarg
);
3526 case QEMU_OPTION_device
:
3527 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3531 case QEMU_OPTION_smp
:
3532 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3536 case QEMU_OPTION_vnc
:
3538 if (vnc_parse_func(optarg
) == NULL
) {
3542 fprintf(stderr
, "VNC support is disabled\n");
3546 case QEMU_OPTION_no_acpi
:
3549 case QEMU_OPTION_no_hpet
:
3552 case QEMU_OPTION_balloon
:
3553 if (balloon_parse(optarg
) < 0) {
3554 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3558 case QEMU_OPTION_no_reboot
:
3561 case QEMU_OPTION_no_shutdown
:
3564 case QEMU_OPTION_show_cursor
:
3567 case QEMU_OPTION_uuid
:
3568 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3569 fprintf(stderr
, "Fail to parse UUID string."
3570 " Wrong format.\n");
3573 qemu_uuid_set
= true;
3575 case QEMU_OPTION_option_rom
:
3576 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3577 fprintf(stderr
, "Too many option ROMs\n");
3580 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3584 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3585 option_rom
[nb_option_roms
].bootindex
=
3586 qemu_opt_get_number(opts
, "bootindex", -1);
3587 if (!option_rom
[nb_option_roms
].name
) {
3588 fprintf(stderr
, "Option ROM file is not specified\n");
3593 case QEMU_OPTION_semihosting
:
3594 semihosting_enabled
= 1;
3595 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3597 case QEMU_OPTION_semihosting_config
:
3598 semihosting_enabled
= 1;
3599 opts
= qemu_opts_parse(qemu_find_opts("semihosting-config"),
3602 semihosting_enabled
= qemu_opt_get_bool(opts
, "enable",
3604 const char *target
= qemu_opt_get(opts
, "target");
3605 if (target
!= NULL
) {
3606 if (strcmp("native", target
) == 0) {
3607 semihosting_target
= SEMIHOSTING_TARGET_NATIVE
;
3608 } else if (strcmp("gdb", target
) == 0) {
3609 semihosting_target
= SEMIHOSTING_TARGET_GDB
;
3610 } else if (strcmp("auto", target
) == 0) {
3611 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3613 fprintf(stderr
, "Unsupported semihosting-config"
3619 semihosting_target
= SEMIHOSTING_TARGET_AUTO
;
3622 fprintf(stderr
, "Unsupported semihosting-config %s\n",
3627 case QEMU_OPTION_tdf
:
3628 fprintf(stderr
, "Warning: user space PIT time drift fix "
3629 "is no longer supported.\n");
3631 case QEMU_OPTION_name
:
3632 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3637 case QEMU_OPTION_prom_env
:
3638 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3639 fprintf(stderr
, "Too many prom variables\n");
3642 prom_envs
[nb_prom_envs
] = optarg
;
3645 case QEMU_OPTION_old_param
:
3648 case QEMU_OPTION_clock
:
3649 /* Clock options no longer exist. Keep this option for
3650 * backward compatibility.
3653 case QEMU_OPTION_startdate
:
3654 configure_rtc_date_offset(optarg
, 1);
3656 case QEMU_OPTION_rtc
:
3657 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3661 configure_rtc(opts
);
3663 case QEMU_OPTION_tb_size
:
3664 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3665 if (tcg_tb_size
< 0) {
3669 case QEMU_OPTION_icount
:
3670 icount_opts
= qemu_opts_parse(qemu_find_opts("icount"),
3676 case QEMU_OPTION_incoming
:
3678 runstate_set(RUN_STATE_INMIGRATE
);
3682 case QEMU_OPTION_nodefaults
:
3685 case QEMU_OPTION_xen_domid
:
3686 if (!(xen_available())) {
3687 printf("Option %s not supported for this target\n", popt
->name
);
3690 xen_domid
= atoi(optarg
);
3692 case QEMU_OPTION_xen_create
:
3693 if (!(xen_available())) {
3694 printf("Option %s not supported for this target\n", popt
->name
);
3697 xen_mode
= XEN_CREATE
;
3699 case QEMU_OPTION_xen_attach
:
3700 if (!(xen_available())) {
3701 printf("Option %s not supported for this target\n", popt
->name
);
3704 xen_mode
= XEN_ATTACH
;
3706 case QEMU_OPTION_trace
:
3708 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3712 trace_events
= qemu_opt_get(opts
, "events");
3713 trace_file
= qemu_opt_get(opts
, "file");
3716 case QEMU_OPTION_trace_unassigned
:
3717 trace_unassigned
= true;
3719 case QEMU_OPTION_readconfig
:
3721 int ret
= qemu_read_config_file(optarg
);
3723 fprintf(stderr
, "read config %s: %s\n", optarg
,
3729 case QEMU_OPTION_spice
:
3730 olist
= qemu_find_opts("spice");
3732 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3735 opts
= qemu_opts_parse(olist
, optarg
, 0);
3741 case QEMU_OPTION_writeconfig
:
3744 if (strcmp(optarg
, "-") == 0) {
3747 fp
= fopen(optarg
, "w");
3749 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3753 qemu_config_write(fp
);
3759 case QEMU_OPTION_qtest
:
3760 qtest_chrdev
= optarg
;
3762 case QEMU_OPTION_qtest_log
:
3765 case QEMU_OPTION_sandbox
:
3766 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3771 case QEMU_OPTION_add_fd
:
3773 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3778 error_report("File descriptor passing is disabled on this "
3783 case QEMU_OPTION_object
:
3784 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3789 case QEMU_OPTION_realtime
:
3790 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3794 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
3796 case QEMU_OPTION_msg
:
3797 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3801 configure_msg(opts
);
3803 case QEMU_OPTION_dump_vmstate
:
3804 if (vmstate_dump_file
) {
3805 fprintf(stderr
, "qemu: only one '-dump-vmstate' "
3806 "option may be given\n");
3809 vmstate_dump_file
= fopen(optarg
, "w");
3810 if (vmstate_dump_file
== NULL
) {
3811 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3816 os_parse_cmd_args(popt
->index
, optarg
);
3821 opts
= qemu_get_machine_opts();
3822 optarg
= qemu_opt_get(opts
, "type");
3824 machine_class
= machine_parse(optarg
);
3827 set_memory_options(&ram_slots
, &maxram_size
);
3833 if (qemu_init_main_loop(&main_loop_err
)) {
3834 error_report_err(main_loop_err
);
3838 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3842 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3847 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3851 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3856 if (machine_class
== NULL
) {
3857 fprintf(stderr
, "No machine specified, and there is no default.\n"
3858 "Use -machine help to list supported machines!\n");
3862 current_machine
= MACHINE(object_new(object_class_get_name(
3863 OBJECT_CLASS(machine_class
))));
3864 if (machine_help_func(qemu_get_machine_opts(), current_machine
)) {
3867 object_property_add_child(object_get_root(), "machine",
3868 OBJECT(current_machine
), &error_abort
);
3869 cpu_exec_init_all();
3871 if (machine_class
->hw_version
) {
3872 qemu_set_version(machine_class
->hw_version
);
3875 /* Init CPU def lists, based on config
3876 * - Must be called after all the qemu_read_config_file() calls
3877 * - Must be called before list_cpus()
3878 * - Must be called before machine->init()
3882 if (cpu_model
&& is_help_option(cpu_model
)) {
3883 list_cpus(stdout
, &fprintf
, cpu_model
);
3887 /* Open the logfile at this point, if necessary. We can't open the logfile
3888 * when encountering either of the logging options (-d or -D) because the
3889 * other one may be encountered later on the command line, changing the
3890 * location or level of logging.
3895 qemu_set_log_filename(log_file
);
3898 mask
= qemu_str_to_log_mask(log_mask
);
3900 qemu_print_log_usage(stdout
);
3906 if (!is_daemonized()) {
3907 if (!trace_init_backends(trace_events
, trace_file
)) {
3912 /* If no data_dir is specified then try to find it relative to the
3914 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3915 data_dir
[data_dir_idx
] = os_find_datadir();
3916 if (data_dir
[data_dir_idx
] != NULL
) {
3920 /* If all else fails use the install path specified when building. */
3921 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3922 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3925 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3927 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
3928 if (max_cpus
> machine_class
->max_cpus
) {
3929 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3930 "supported by machine `%s' (%d)\n", max_cpus
,
3931 machine_class
->name
, machine_class
->max_cpus
);
3936 * Get the default machine options from the machine if it is not already
3937 * specified either by the configuration file or by the command line.
3939 if (machine_class
->default_machine_opts
) {
3940 qemu_opts_set_defaults(qemu_find_opts("machine"),
3941 machine_class
->default_machine_opts
, 0);
3944 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3945 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3947 if (!vga_model
&& !default_vga
) {
3948 vga_interface_type
= VGA_DEVICE
;
3950 if (!has_defaults
|| machine_class
->no_serial
) {
3953 if (!has_defaults
|| machine_class
->no_parallel
) {
3954 default_parallel
= 0;
3956 if (!has_defaults
|| !machine_class
->use_virtcon
) {
3957 default_virtcon
= 0;
3959 if (!has_defaults
|| !machine_class
->use_sclp
) {
3962 if (!has_defaults
|| machine_class
->no_floppy
) {
3965 if (!has_defaults
|| machine_class
->no_cdrom
) {
3968 if (!has_defaults
|| machine_class
->no_sdcard
) {
3971 if (!has_defaults
) {
3972 default_monitor
= 0;
3977 if (is_daemonized()) {
3978 /* According to documentation and historically, -nographic redirects
3979 * serial port, parallel port and monitor to stdio, which does not work
3980 * with -daemonize. We can redirect these to null instead, but since
3981 * -nographic is legacy, let's just error out.
3982 * We disallow -nographic only if all other ports are not redirected
3983 * explicitly, to not break existing legacy setups which uses
3984 * -nographic _and_ redirects all ports explicitly - this is valid
3985 * usage, -nographic is just a no-op in this case.
3987 if (display_type
== DT_NOGRAPHIC
3988 && (default_parallel
|| default_serial
3989 || default_monitor
|| default_virtcon
)) {
3990 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3993 #ifdef CONFIG_CURSES
3994 if (display_type
== DT_CURSES
) {
3995 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4001 if (display_type
== DT_NOGRAPHIC
) {
4002 if (default_parallel
)
4003 add_device_config(DEV_PARALLEL
, "null");
4004 if (default_serial
&& default_monitor
) {
4005 add_device_config(DEV_SERIAL
, "mon:stdio");
4006 } else if (default_virtcon
&& default_monitor
) {
4007 add_device_config(DEV_VIRTCON
, "mon:stdio");
4008 } else if (default_sclp
&& default_monitor
) {
4009 add_device_config(DEV_SCLP
, "mon:stdio");
4012 add_device_config(DEV_SERIAL
, "stdio");
4013 if (default_virtcon
)
4014 add_device_config(DEV_VIRTCON
, "stdio");
4016 add_device_config(DEV_SCLP
, "stdio");
4018 if (default_monitor
)
4019 monitor_parse("stdio", "readline", false);
4023 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4024 if (default_parallel
)
4025 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4026 if (default_monitor
)
4027 monitor_parse("vc:80Cx24C", "readline", false);
4028 if (default_virtcon
)
4029 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4031 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4035 #if defined(CONFIG_VNC)
4036 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head
))) {
4040 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4041 #if defined(CONFIG_GTK)
4042 display_type
= DT_GTK
;
4043 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4044 display_type
= DT_SDL
;
4045 #elif defined(CONFIG_VNC)
4046 vnc_parse_func("localhost:0,to=99,id=default");
4049 display_type
= DT_NONE
;
4053 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4054 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4055 "for SDL, ignoring option\n");
4057 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4058 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4059 "ignoring option\n");
4062 #if defined(CONFIG_GTK)
4063 if (display_type
== DT_GTK
) {
4064 early_gtk_display_init();
4067 #if defined(CONFIG_SDL)
4068 if (display_type
== DT_SDL
) {
4069 sdl_display_early_init(request_opengl
);
4072 if (request_opengl
== 1 && display_opengl
== 0) {
4073 #if defined(CONFIG_OPENGL)
4074 fprintf(stderr
, "OpenGL is not supported by the display.\n");
4076 fprintf(stderr
, "QEMU was built without opengl support.\n");
4083 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4085 #ifdef CONFIG_VIRTFS
4086 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4091 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4092 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4096 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4101 if (qemu_opts_foreach(qemu_find_opts("object"),
4102 object_create
, NULL
, 0) != 0) {
4106 machine_opts
= qemu_get_machine_opts();
4107 if (qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4109 object_unref(OBJECT(current_machine
));
4113 configure_accelerator(current_machine
);
4116 Error
*local_err
= NULL
;
4117 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4119 error_report_err(local_err
);
4124 machine_opts
= qemu_get_machine_opts();
4125 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4126 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4127 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4128 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4130 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4132 Error
*local_err
= NULL
;
4134 boot_order
= qemu_opt_get(opts
, "order");
4136 validate_bootdevices(boot_order
, &local_err
);
4138 error_report_err(local_err
);
4143 boot_once
= qemu_opt_get(opts
, "once");
4145 validate_bootdevices(boot_once
, &local_err
);
4147 error_report_err(local_err
);
4152 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4153 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4157 boot_order
= machine_class
->default_boot_order
;
4160 if (!kernel_cmdline
) {
4161 kernel_cmdline
= "";
4162 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4165 linux_boot
= (kernel_filename
!= NULL
);
4167 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4168 fprintf(stderr
, "-append only allowed with -kernel option\n");
4172 if (!linux_boot
&& initrd_filename
!= NULL
) {
4173 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4177 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4178 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4182 os_set_line_buffering();
4185 /* spice needs the timers to be initialized by this point */
4191 if (kvm_enabled() || xen_enabled()) {
4192 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4195 configure_icount(icount_opts
, &error_abort
);
4196 qemu_opts_del(icount_opts
);
4199 /* clean up network at qemu process termination */
4200 atexit(&net_cleanup
);
4202 if (net_init_clients() < 0) {
4207 if (tpm_init() < 0) {
4212 /* init the bluetooth world */
4213 if (foreach_device_config(DEV_BT
, bt_parse
))
4216 if (!xen_enabled()) {
4217 /* On 32-bit hosts, QEMU is limited by virtual address space */
4218 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4219 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4227 /* If the currently selected machine wishes to override the units-per-bus
4228 * property of its default HBA interface type, do so now. */
4229 if (machine_class
->units_per_default_bus
) {
4230 override_max_devs(machine_class
->block_default_type
,
4231 machine_class
->units_per_default_bus
);
4234 /* open the virtual block devices */
4236 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4237 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4238 &machine_class
->block_default_type
, 1) != 0) {
4242 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4244 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4245 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4247 parse_numa_opts(machine_class
);
4249 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4253 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4255 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4257 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4259 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4262 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4265 /* If no default VGA is requested, the default is "none". */
4267 if (machine_class
->default_display
) {
4268 vga_model
= machine_class
->default_display
;
4269 } else if (cirrus_vga_available()) {
4270 vga_model
= "cirrus";
4271 } else if (vga_available()) {
4276 select_vgahw(vga_model
);
4280 i
= select_watchdog(watchdog
);
4282 exit (i
== 1 ? 1 : 0);
4285 if (machine_class
->compat_props
) {
4286 qdev_prop_register_global_list(machine_class
->compat_props
);
4290 qdev_machine_init();
4292 current_machine
->ram_size
= ram_size
;
4293 current_machine
->maxram_size
= maxram_size
;
4294 current_machine
->ram_slots
= ram_slots
;
4295 current_machine
->boot_order
= boot_order
;
4296 current_machine
->cpu_model
= cpu_model
;
4298 machine_class
->init(current_machine
);
4304 cpu_synchronize_all_post_init();
4306 numa_post_machine_init();
4308 /* init USB devices */
4309 if (usb_enabled()) {
4310 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4314 /* init generic devices */
4315 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4318 /* Did we create any drives that we failed to create a device for? */
4319 drive_check_orphaned();
4321 net_check_clients();
4324 Error
*local_err
= NULL
;
4325 qemu_boot_set(boot_once
, &local_err
);
4327 error_report("%s", error_get_pretty(local_err
));
4330 qemu_register_reset(restore_boot_order
, g_strdup(boot_order
));
4333 ds
= init_displaystate();
4335 /* init local displays */
4336 switch (display_type
) {
4338 (void)ds
; /* avoid warning if no display is configured */
4340 #if defined(CONFIG_CURSES)
4342 curses_display_init(ds
, full_screen
);
4345 #if defined(CONFIG_SDL)
4347 sdl_display_init(ds
, full_screen
, no_frame
);
4349 #elif defined(CONFIG_COCOA)
4351 cocoa_display_init(ds
, full_screen
);
4354 #if defined(CONFIG_GTK)
4356 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4363 /* must be after terminal init, SDL library changes signal handlers */
4364 os_setup_signal_handling();
4367 /* init remote displays */
4368 qemu_opts_foreach(qemu_find_opts("vnc"), vnc_init_func
, NULL
, 0);
4369 if (show_vnc_port
) {
4370 printf("VNC server running on `%s'\n",
4371 vnc_display_local_addr("default"));
4376 qemu_spice_display_init();
4380 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4384 qdev_machine_creation_done();
4386 if (rom_load_all() != 0) {
4387 fprintf(stderr
, "rom loading failed\n");
4391 /* TODO: once all bus devices are qdevified, this should be done
4392 * when bus is created by qdev.c */
4393 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4394 qemu_run_machine_init_done_notifiers();
4396 /* Done notifiers can load ROMs */
4399 qemu_system_reset(VMRESET_SILENT
);
4401 if (load_vmstate(loadvm
) < 0) {
4406 qdev_prop_check_globals();
4407 if (vmstate_dump_file
) {
4409 dump_vmstate_json_to_file(vmstate_dump_file
);
4414 Error
*local_err
= NULL
;
4415 qemu_start_incoming_migration(incoming
, &local_err
);
4417 error_report("-incoming %s: %s", incoming
,
4418 error_get_pretty(local_err
));
4419 error_free(local_err
);
4422 } else if (autostart
) {
4428 if (is_daemonized()) {
4429 if (!trace_init_backends(trace_events
, trace_file
)) {