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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 int display_remote
= 0;
181 const char* keyboard_layout
= NULL
;
183 const char *mem_path
= NULL
;
185 int mem_prealloc
= 0; /* force preallocation of physical target memory */
188 NICInfo nd_table
[MAX_NICS
];
191 int incoming_expected
; /* Started with -incoming and waiting for incoming */
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_NONE
;
196 static int full_screen
= 0;
198 static int no_frame
= 0;
201 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
202 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
203 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
204 int win2k_install_hack
= 0;
213 const char *vnc_display
;
215 int acpi_enabled
= 1;
221 int graphic_rotate
= 0;
222 const char *watchdog
;
223 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
225 int semihosting_enabled
= 0;
226 int time_drift_fix
= 0;
227 unsigned int kvm_shadow_memory
= 0;
229 const char *qemu_name
;
232 unsigned int nb_prom_envs
= 0;
233 const char *prom_envs
[MAX_PROM_ENVS
];
234 const char *nvram
= NULL
;
236 uint8_t *boot_splash_filedata
;
237 int boot_splash_filedata_size
;
238 uint8_t qemu_extra_params_fw
[2];
240 typedef struct FWBootEntry FWBootEntry
;
243 QTAILQ_ENTRY(FWBootEntry
) link
;
249 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static QEMUTimer
*nographic_timer
;
257 uint8_t qemu_uuid
[16];
259 static QEMUBootSetHandler
*boot_set_handler
;
260 static void *boot_set_opaque
;
262 static NotifierList exit_notifiers
=
263 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
265 static NotifierList machine_init_done_notifiers
=
266 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
268 static int tcg_allowed
= 1;
272 enum xen_mode xen_mode
= XEN_EMULATE
;
273 static int tcg_tb_size
;
275 static int default_serial
= 1;
276 static int default_parallel
= 1;
277 static int default_virtcon
= 1;
278 static int default_monitor
= 1;
279 static int default_vga
= 1;
280 static int default_floppy
= 1;
281 static int default_cdrom
= 1;
282 static int default_sdcard
= 1;
288 { .driver
= "isa-serial", .flag
= &default_serial
},
289 { .driver
= "isa-parallel", .flag
= &default_parallel
},
290 { .driver
= "isa-fdc", .flag
= &default_floppy
},
291 { .driver
= "ide-cd", .flag
= &default_cdrom
},
292 { .driver
= "ide-hd", .flag
= &default_cdrom
},
293 { .driver
= "ide-drive", .flag
= &default_cdrom
},
294 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
295 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
296 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
297 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
298 { .driver
= "VGA", .flag
= &default_vga
},
299 { .driver
= "cirrus-vga", .flag
= &default_vga
},
300 { .driver
= "vmware-svga", .flag
= &default_vga
},
301 { .driver
= "isa-vga", .flag
= &default_vga
},
302 { .driver
= "qxl-vga", .flag
= &default_vga
},
305 static void res_free(void)
307 if (boot_splash_filedata
!= NULL
) {
308 g_free(boot_splash_filedata
);
309 boot_splash_filedata
= NULL
;
313 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
315 const char *driver
= qemu_opt_get(opts
, "driver");
320 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
321 if (strcmp(default_list
[i
].driver
, driver
) != 0)
323 *(default_list
[i
].flag
) = 0;
328 /***********************************************************/
329 /* real time host monotonic timer */
331 /***********************************************************/
332 /* host time/date access */
333 void qemu_get_timedate(struct tm
*tm
, int offset
)
340 if (rtc_date_offset
== -1) {
344 ret
= localtime(&ti
);
346 ti
-= rtc_date_offset
;
350 memcpy(tm
, ret
, sizeof(struct tm
));
353 int qemu_timedate_diff(struct tm
*tm
)
357 if (rtc_date_offset
== -1)
359 seconds
= mktimegm(tm
);
361 seconds
= mktime(tm
);
363 seconds
= mktimegm(tm
) + rtc_date_offset
;
365 return seconds
- time(NULL
);
368 void rtc_change_mon_event(struct tm
*tm
)
372 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
373 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
374 qobject_decref(data
);
377 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
379 time_t rtc_start_date
;
382 if (!strcmp(startdate
, "now") && legacy
) {
383 rtc_date_offset
= -1;
385 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
393 } else if (sscanf(startdate
, "%d-%d-%d",
405 rtc_start_date
= mktimegm(&tm
);
406 if (rtc_start_date
== -1) {
408 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
409 "'2006-06-17T16:01:21' or '2006-06-17'\n");
412 rtc_date_offset
= time(NULL
) - rtc_start_date
;
416 static void configure_rtc(QemuOpts
*opts
)
420 value
= qemu_opt_get(opts
, "base");
422 if (!strcmp(value
, "utc")) {
424 } else if (!strcmp(value
, "localtime")) {
427 configure_rtc_date_offset(value
, 0);
430 value
= qemu_opt_get(opts
, "clock");
432 if (!strcmp(value
, "host")) {
433 rtc_clock
= host_clock
;
434 } else if (!strcmp(value
, "vm")) {
435 rtc_clock
= vm_clock
;
437 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
441 value
= qemu_opt_get(opts
, "driftfix");
443 if (!strcmp(value
, "slew")) {
445 } else if (!strcmp(value
, "none")) {
448 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
454 /***********************************************************/
455 /* Bluetooth support */
458 static struct HCIInfo
*hci_table
[MAX_NICS
];
460 static struct bt_vlan_s
{
461 struct bt_scatternet_s net
;
463 struct bt_vlan_s
*next
;
466 /* find or alloc a new bluetooth "VLAN" */
467 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
469 struct bt_vlan_s
**pvlan
, *vlan
;
470 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
474 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
476 pvlan
= &first_bt_vlan
;
477 while (*pvlan
!= NULL
)
478 pvlan
= &(*pvlan
)->next
;
483 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
487 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
492 static struct HCIInfo null_hci
= {
493 .cmd_send
= null_hci_send
,
494 .sco_send
= null_hci_send
,
495 .acl_send
= null_hci_send
,
496 .bdaddr_set
= null_hci_addr_set
,
499 struct HCIInfo
*qemu_next_hci(void)
501 if (cur_hci
== nb_hcis
)
504 return hci_table
[cur_hci
++];
507 static struct HCIInfo
*hci_init(const char *str
)
510 struct bt_scatternet_s
*vlan
= 0;
512 if (!strcmp(str
, "null"))
515 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
517 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
518 else if (!strncmp(str
, "hci", 3)) {
521 if (!strncmp(str
+ 3, ",vlan=", 6)) {
522 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
527 vlan
= qemu_find_bt_vlan(0);
529 return bt_new_hci(vlan
);
532 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
537 static int bt_hci_parse(const char *str
)
542 if (nb_hcis
>= MAX_NICS
) {
543 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
556 bdaddr
.b
[5] = 0x56 + nb_hcis
;
557 hci
->bdaddr_set(hci
, bdaddr
.b
);
559 hci_table
[nb_hcis
++] = hci
;
564 static void bt_vhci_add(int vlan_id
)
566 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
569 fprintf(stderr
, "qemu: warning: adding a VHCI to "
570 "an empty scatternet %i\n", vlan_id
);
572 bt_vhci_init(bt_new_hci(vlan
));
575 static struct bt_device_s
*bt_device_add(const char *opt
)
577 struct bt_scatternet_s
*vlan
;
579 char *endp
= strstr(opt
, ",vlan=");
580 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
583 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
586 vlan_id
= strtol(endp
+ 6, &endp
, 0);
588 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
593 vlan
= qemu_find_bt_vlan(vlan_id
);
596 fprintf(stderr
, "qemu: warning: adding a slave device to "
597 "an empty scatternet %i\n", vlan_id
);
599 if (!strcmp(devname
, "keyboard"))
600 return bt_keyboard_init(vlan
);
602 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
606 static int bt_parse(const char *opt
)
608 const char *endp
, *p
;
611 if (strstart(opt
, "hci", &endp
)) {
612 if (!*endp
|| *endp
== ',') {
614 if (!strstart(endp
, ",vlan=", 0))
617 return bt_hci_parse(opt
);
619 } else if (strstart(opt
, "vhci", &endp
)) {
620 if (!*endp
|| *endp
== ',') {
622 if (strstart(endp
, ",vlan=", &p
)) {
623 vlan
= strtol(p
, (char **) &endp
, 0);
625 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
629 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
638 } else if (strstart(opt
, "device:", &endp
))
639 return !bt_device_add(endp
);
641 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
645 /***********************************************************/
646 /* QEMU Block devices */
648 #define HD_OPTS "media=disk"
649 #define CDROM_OPTS "media=cdrom"
651 #define PFLASH_OPTS ""
655 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
657 int *use_scsi
= opaque
;
659 return drive_init(opts
, *use_scsi
) == NULL
;
662 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
664 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
665 qemu_opt_set(opts
, "snapshot", "on");
670 static void default_drive(int enable
, int snapshot
, int use_scsi
,
671 BlockInterfaceType type
, int index
,
676 if (type
== IF_DEFAULT
) {
677 type
= use_scsi
? IF_SCSI
: IF_IDE
;
680 if (!enable
|| drive_get_by_index(type
, index
)) {
684 opts
= drive_add(type
, index
, NULL
, optstr
);
686 drive_enable_snapshot(opts
, NULL
);
688 if (!drive_init(opts
, use_scsi
)) {
693 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
695 boot_set_handler
= func
;
696 boot_set_opaque
= opaque
;
699 int qemu_boot_set(const char *boot_devices
)
701 if (!boot_set_handler
) {
704 return boot_set_handler(boot_set_opaque
, boot_devices
);
707 static void validate_bootdevices(char *devices
)
709 /* We just do some generic consistency checks */
713 for (p
= devices
; *p
!= '\0'; p
++) {
714 /* Allowed boot devices are:
715 * a-b: floppy disk drives
716 * c-f: IDE disk drives
717 * g-m: machine implementation dependant drives
718 * n-p: network devices
719 * It's up to each machine implementation to check if the given boot
720 * devices match the actual hardware implementation and firmware
723 if (*p
< 'a' || *p
> 'p') {
724 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
727 if (bitmap
& (1 << (*p
- 'a'))) {
728 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
731 bitmap
|= 1 << (*p
- 'a');
735 static void restore_boot_devices(void *opaque
)
737 char *standard_boot_devices
= opaque
;
738 static int first
= 1;
740 /* Restore boot order and remove ourselves after the first boot */
746 qemu_boot_set(standard_boot_devices
);
748 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
749 g_free(standard_boot_devices
);
752 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
755 FWBootEntry
*node
, *i
;
761 assert(dev
!= NULL
|| suffix
!= NULL
);
763 node
= g_malloc0(sizeof(FWBootEntry
));
764 node
->bootindex
= bootindex
;
765 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
768 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
769 if (i
->bootindex
== bootindex
) {
770 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
772 } else if (i
->bootindex
< bootindex
) {
775 QTAILQ_INSERT_BEFORE(i
, node
, link
);
778 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
782 * This function returns null terminated string that consist of new line
783 * separated device paths.
785 * memory pointed by "size" is assigned total length of the array in bytes
788 char *get_boot_devices_list(uint32_t *size
)
794 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
795 char *devpath
= NULL
, *bootpath
;
799 devpath
= qdev_get_fw_dev_path(i
->dev
);
803 if (i
->suffix
&& devpath
) {
804 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
806 bootpath
= g_malloc(bootpathlen
);
807 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
809 } else if (devpath
) {
812 bootpath
= g_strdup(i
->suffix
);
817 list
[total
-1] = '\n';
819 len
= strlen(bootpath
) + 1;
820 list
= g_realloc(list
, total
+ len
);
821 memcpy(&list
[total
], bootpath
, len
);
831 static void numa_add(const char *optarg
)
835 unsigned long long value
, endvalue
;
838 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
839 if (!strcmp(option
, "node")) {
840 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
841 nodenr
= nb_numa_nodes
;
843 nodenr
= strtoull(option
, NULL
, 10);
846 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
847 node_mem
[nodenr
] = 0;
850 sval
= strtosz(option
, NULL
);
852 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
855 node_mem
[nodenr
] = sval
;
857 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
858 node_cpumask
[nodenr
] = 0;
860 value
= strtoull(option
, &endptr
, 10);
863 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
865 if (*endptr
== '-') {
866 endvalue
= strtoull(endptr
+1, &endptr
, 10);
867 if (endvalue
>= 63) {
870 "only 63 CPUs in NUMA mode supported.\n");
872 value
= (2ULL << endvalue
) - (1ULL << value
);
874 value
= 1ULL << value
;
877 node_cpumask
[nodenr
] = value
;
884 static void smp_parse(const char *optarg
)
886 int smp
, sockets
= 0, threads
= 0, cores
= 0;
890 smp
= strtoul(optarg
, &endptr
, 10);
891 if (endptr
!= optarg
) {
892 if (*endptr
== ',') {
896 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
897 sockets
= strtoull(option
, NULL
, 10);
898 if (get_param_value(option
, 128, "cores", endptr
) != 0)
899 cores
= strtoull(option
, NULL
, 10);
900 if (get_param_value(option
, 128, "threads", endptr
) != 0)
901 threads
= strtoull(option
, NULL
, 10);
902 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
903 max_cpus
= strtoull(option
, NULL
, 10);
905 /* compute missing values, prefer sockets over cores over threads */
906 if (smp
== 0 || sockets
== 0) {
907 sockets
= sockets
> 0 ? sockets
: 1;
908 cores
= cores
> 0 ? cores
: 1;
909 threads
= threads
> 0 ? threads
: 1;
911 smp
= cores
* threads
* sockets
;
915 threads
= threads
> 0 ? threads
: 1;
916 cores
= smp
/ (sockets
* threads
);
918 threads
= smp
/ (cores
* sockets
);
922 smp_cores
= cores
> 0 ? cores
: 1;
923 smp_threads
= threads
> 0 ? threads
: 1;
928 /***********************************************************/
931 static int usb_device_add(const char *devname
)
934 USBDevice
*dev
= NULL
;
939 /* drivers with .usbdevice_name entry in USBDeviceInfo */
940 dev
= usbdevice_create(devname
);
946 /* only the linux version is qdev-ified, usb-bsd still needs this */
947 if (strstart(devname
, "host:", &p
)) {
948 dev
= usb_host_device_open(p
);
951 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
952 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
953 bt_new_hci(qemu_find_bt_vlan(0)));
964 static int usb_device_del(const char *devname
)
969 if (strstart(devname
, "host:", &p
))
970 return usb_host_device_close(p
);
975 p
= strchr(devname
, '.');
978 bus_num
= strtoul(devname
, NULL
, 0);
979 addr
= strtoul(p
+ 1, NULL
, 0);
981 return usb_device_delete_addr(bus_num
, addr
);
984 static int usb_parse(const char *cmdline
)
987 r
= usb_device_add(cmdline
);
989 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
994 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
996 const char *devname
= qdict_get_str(qdict
, "devname");
997 if (usb_device_add(devname
) < 0) {
998 error_report("could not add USB device '%s'", devname
);
1002 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1004 const char *devname
= qdict_get_str(qdict
, "devname");
1005 if (usb_device_del(devname
) < 0) {
1006 error_report("could not delete USB device '%s'", devname
);
1010 /***********************************************************/
1011 /* PCMCIA/Cardbus */
1013 static struct pcmcia_socket_entry_s
{
1014 PCMCIASocket
*socket
;
1015 struct pcmcia_socket_entry_s
*next
;
1016 } *pcmcia_sockets
= 0;
1018 void pcmcia_socket_register(PCMCIASocket
*socket
)
1020 struct pcmcia_socket_entry_s
*entry
;
1022 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1023 entry
->socket
= socket
;
1024 entry
->next
= pcmcia_sockets
;
1025 pcmcia_sockets
= entry
;
1028 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1030 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1032 ptr
= &pcmcia_sockets
;
1033 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1034 if (entry
->socket
== socket
) {
1040 void pcmcia_info(Monitor
*mon
)
1042 struct pcmcia_socket_entry_s
*iter
;
1044 if (!pcmcia_sockets
)
1045 monitor_printf(mon
, "No PCMCIA sockets\n");
1047 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1048 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1049 iter
->socket
->attached
? iter
->socket
->card_string
:
1053 /***********************************************************/
1054 /* machine registration */
1056 static QEMUMachine
*first_machine
= NULL
;
1057 QEMUMachine
*current_machine
= NULL
;
1059 int qemu_register_machine(QEMUMachine
*m
)
1062 pm
= &first_machine
;
1070 static QEMUMachine
*find_machine(const char *name
)
1074 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1075 if (!strcmp(m
->name
, name
))
1077 if (m
->alias
&& !strcmp(m
->alias
, name
))
1083 static QEMUMachine
*find_default_machine(void)
1087 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1088 if (m
->is_default
) {
1095 /***********************************************************/
1096 /* main execution loop */
1098 static void gui_update(void *opaque
)
1100 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1101 DisplayState
*ds
= opaque
;
1102 DisplayChangeListener
*dcl
= ds
->listeners
;
1104 qemu_flush_coalesced_mmio_buffer();
1107 while (dcl
!= NULL
) {
1108 if (dcl
->gui_timer_interval
&&
1109 dcl
->gui_timer_interval
< interval
)
1110 interval
= dcl
->gui_timer_interval
;
1113 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1116 static void nographic_update(void *opaque
)
1118 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1120 qemu_flush_coalesced_mmio_buffer();
1121 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1124 struct vm_change_state_entry
{
1125 VMChangeStateHandler
*cb
;
1127 QLIST_ENTRY (vm_change_state_entry
) entries
;
1130 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1132 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1135 VMChangeStateEntry
*e
;
1137 e
= g_malloc0(sizeof (*e
));
1141 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1145 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1147 QLIST_REMOVE (e
, entries
);
1151 void vm_state_notify(int running
, int reason
)
1153 VMChangeStateEntry
*e
;
1155 trace_vm_state_notify(running
, reason
);
1157 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1158 e
->cb(e
->opaque
, running
, reason
);
1167 vm_state_notify(1, 0);
1169 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1173 /* reset/shutdown handler */
1175 typedef struct QEMUResetEntry
{
1176 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1177 QEMUResetHandler
*func
;
1181 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1182 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1183 static int reset_requested
;
1184 static int shutdown_requested
, shutdown_signal
= -1;
1185 static pid_t shutdown_pid
;
1186 static int powerdown_requested
;
1187 static int debug_requested
;
1188 static int vmstop_requested
;
1190 int qemu_shutdown_requested_get(void)
1192 return shutdown_requested
;
1195 int qemu_reset_requested_get(void)
1197 return reset_requested
;
1200 int qemu_shutdown_requested(void)
1202 int r
= shutdown_requested
;
1203 shutdown_requested
= 0;
1207 void qemu_kill_report(void)
1209 if (shutdown_signal
!= -1) {
1210 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1211 if (shutdown_pid
== 0) {
1212 /* This happens for eg ^C at the terminal, so it's worth
1213 * avoiding printing an odd message in that case.
1215 fputc('\n', stderr
);
1217 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1219 shutdown_signal
= -1;
1223 int qemu_reset_requested(void)
1225 int r
= reset_requested
;
1226 reset_requested
= 0;
1230 int qemu_powerdown_requested(void)
1232 int r
= powerdown_requested
;
1233 powerdown_requested
= 0;
1237 static int qemu_debug_requested(void)
1239 int r
= debug_requested
;
1240 debug_requested
= 0;
1244 static int qemu_vmstop_requested(void)
1246 int r
= vmstop_requested
;
1247 vmstop_requested
= 0;
1251 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1253 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1256 re
->opaque
= opaque
;
1257 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1260 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1264 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1265 if (re
->func
== func
&& re
->opaque
== opaque
) {
1266 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1273 void qemu_system_reset(bool report
)
1275 QEMUResetEntry
*re
, *nre
;
1277 /* reset all devices */
1278 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1279 re
->func(re
->opaque
);
1282 monitor_protocol_event(QEVENT_RESET
, NULL
);
1284 cpu_synchronize_all_post_reset();
1287 void qemu_system_reset_request(void)
1290 shutdown_requested
= 1;
1292 reset_requested
= 1;
1295 qemu_notify_event();
1298 void qemu_system_killed(int signal
, pid_t pid
)
1300 shutdown_signal
= signal
;
1302 qemu_system_shutdown_request();
1305 void qemu_system_shutdown_request(void)
1307 shutdown_requested
= 1;
1308 qemu_notify_event();
1311 void qemu_system_powerdown_request(void)
1313 powerdown_requested
= 1;
1314 qemu_notify_event();
1317 void qemu_system_debug_request(void)
1319 debug_requested
= 1;
1320 qemu_notify_event();
1323 void qemu_system_vmstop_request(int reason
)
1325 vmstop_requested
= reason
;
1326 qemu_notify_event();
1329 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1330 static int n_poll_fds
;
1331 static int max_priority
;
1333 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1334 fd_set
*xfds
, struct timeval
*tv
)
1336 GMainContext
*context
= g_main_context_default();
1338 int timeout
= 0, cur_timeout
;
1340 g_main_context_prepare(context
, &max_priority
);
1342 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1343 poll_fds
, ARRAY_SIZE(poll_fds
));
1344 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1346 for (i
= 0; i
< n_poll_fds
; i
++) {
1347 GPollFD
*p
= &poll_fds
[i
];
1349 if ((p
->events
& G_IO_IN
)) {
1350 FD_SET(p
->fd
, rfds
);
1351 *max_fd
= MAX(*max_fd
, p
->fd
);
1353 if ((p
->events
& G_IO_OUT
)) {
1354 FD_SET(p
->fd
, wfds
);
1355 *max_fd
= MAX(*max_fd
, p
->fd
);
1357 if ((p
->events
& G_IO_ERR
)) {
1358 FD_SET(p
->fd
, xfds
);
1359 *max_fd
= MAX(*max_fd
, p
->fd
);
1363 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1364 if (timeout
>= 0 && timeout
< cur_timeout
) {
1365 tv
->tv_sec
= timeout
/ 1000;
1366 tv
->tv_usec
= (timeout
% 1000) * 1000;
1370 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1373 GMainContext
*context
= g_main_context_default();
1378 for (i
= 0; i
< n_poll_fds
; i
++) {
1379 GPollFD
*p
= &poll_fds
[i
];
1381 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1382 p
->revents
|= G_IO_IN
;
1384 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1385 p
->revents
|= G_IO_OUT
;
1387 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1388 p
->revents
|= G_IO_ERR
;
1393 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1394 g_main_context_dispatch(context
);
1398 int main_loop_wait(int nonblocking
)
1400 fd_set rfds
, wfds
, xfds
;
1408 timeout
= qemu_calculate_timeout();
1409 qemu_bh_update_timeout(&timeout
);
1412 os_host_main_loop_wait(&timeout
);
1414 tv
.tv_sec
= timeout
/ 1000;
1415 tv
.tv_usec
= (timeout
% 1000) * 1000;
1417 /* poll any events */
1418 /* XXX: separate device handlers from system ones */
1424 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1425 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1426 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1429 qemu_mutex_unlock_iothread();
1432 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1435 qemu_mutex_lock_iothread();
1438 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1439 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1440 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1442 qemu_run_all_timers();
1444 /* Check bottom-halves last in case any of the earlier events triggered
1451 qemu_irq qemu_system_powerdown
;
1453 static void main_loop(void)
1456 int last_io
__attribute__ ((unused
)) = 0;
1457 #ifdef CONFIG_PROFILER
1462 qemu_main_loop_start();
1465 nonblocking
= !kvm_enabled() && last_io
> 0;
1466 #ifdef CONFIG_PROFILER
1467 ti
= profile_getclock();
1469 last_io
= main_loop_wait(nonblocking
);
1470 #ifdef CONFIG_PROFILER
1471 dev_time
+= profile_getclock() - ti
;
1474 if (qemu_debug_requested()) {
1475 vm_stop(VMSTOP_DEBUG
);
1477 if (qemu_shutdown_requested()) {
1479 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1481 vm_stop(VMSTOP_SHUTDOWN
);
1485 if (qemu_reset_requested()) {
1487 cpu_synchronize_all_states();
1488 qemu_system_reset(VMRESET_REPORT
);
1491 if (qemu_powerdown_requested()) {
1492 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1493 qemu_irq_raise(qemu_system_powerdown
);
1495 if ((r
= qemu_vmstop_requested())) {
1503 static void version(void)
1505 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1508 static void help(int exitcode
)
1510 const char *options_help
=
1511 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1513 #define DEFHEADING(text) stringify(text) "\n"
1514 #include "qemu-options.def"
1520 printf("usage: %s [options] [disk_image]\n"
1522 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1525 "During emulation, the following keys are useful:\n"
1526 "ctrl-alt-f toggle full screen\n"
1527 "ctrl-alt-n switch to virtual console 'n'\n"
1528 "ctrl-alt toggle mouse and keyboard grab\n"
1530 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1536 #define HAS_ARG 0x0001
1538 typedef struct QEMUOption
{
1545 static const QEMUOption qemu_options
[] = {
1546 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1547 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1548 { option, opt_arg, opt_enum, arch_mask },
1549 #define DEFHEADING(text)
1550 #include "qemu-options.def"
1556 static void select_vgahw (const char *p
)
1561 vga_interface_type
= VGA_NONE
;
1562 if (strstart(p
, "std", &opts
)) {
1563 vga_interface_type
= VGA_STD
;
1564 } else if (strstart(p
, "cirrus", &opts
)) {
1565 vga_interface_type
= VGA_CIRRUS
;
1566 } else if (strstart(p
, "vmware", &opts
)) {
1567 vga_interface_type
= VGA_VMWARE
;
1568 } else if (strstart(p
, "xenfb", &opts
)) {
1569 vga_interface_type
= VGA_XENFB
;
1570 } else if (strstart(p
, "qxl", &opts
)) {
1571 vga_interface_type
= VGA_QXL
;
1572 } else if (!strstart(p
, "none", &opts
)) {
1574 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1578 const char *nextopt
;
1580 if (strstart(opts
, ",retrace=", &nextopt
)) {
1582 if (strstart(opts
, "dumb", &nextopt
))
1583 vga_retrace_method
= VGA_RETRACE_DUMB
;
1584 else if (strstart(opts
, "precise", &nextopt
))
1585 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1586 else goto invalid_vga
;
1587 } else goto invalid_vga
;
1592 static DisplayType
select_display(const char *p
)
1595 DisplayType display
= DT_DEFAULT
;
1597 if (strstart(p
, "sdl", &opts
)) {
1601 const char *nextopt
;
1603 if (strstart(opts
, ",frame=", &nextopt
)) {
1605 if (strstart(opts
, "on", &nextopt
)) {
1607 } else if (strstart(opts
, "off", &nextopt
)) {
1610 goto invalid_sdl_args
;
1612 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1614 if (strstart(opts
, "on", &nextopt
)) {
1616 } else if (strstart(opts
, "off", &nextopt
)) {
1619 goto invalid_sdl_args
;
1621 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1623 if (strstart(opts
, "on", &nextopt
)) {
1625 } else if (strstart(opts
, "off", &nextopt
)) {
1628 goto invalid_sdl_args
;
1630 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1632 if (strstart(opts
, "on", &nextopt
)) {
1634 } else if (strstart(opts
, "off", &nextopt
)) {
1637 goto invalid_sdl_args
;
1641 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1647 fprintf(stderr
, "SDL support is disabled\n");
1650 } else if (strstart(p
, "vnc", &opts
)) {
1655 const char *nextopt
;
1657 if (strstart(opts
, "=", &nextopt
)) {
1658 vnc_display
= nextopt
;
1662 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1666 fprintf(stderr
, "VNC support is disabled\n");
1669 } else if (strstart(p
, "curses", &opts
)) {
1670 #ifdef CONFIG_CURSES
1671 display
= DT_CURSES
;
1673 fprintf(stderr
, "Curses support is disabled\n");
1676 } else if (strstart(p
, "none", &opts
)) {
1679 fprintf(stderr
, "Unknown display type: %s\n", p
);
1686 static int balloon_parse(const char *arg
)
1690 if (strcmp(arg
, "none") == 0) {
1694 if (!strncmp(arg
, "virtio", 6)) {
1695 if (arg
[6] == ',') {
1696 /* have params -> parse them */
1697 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1701 /* create empty opts */
1702 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1704 qemu_opt_set(opts
, "driver", "virtio-balloon");
1711 char *qemu_find_file(int type
, const char *name
)
1717 /* If name contains path separators then try it as a straight path. */
1718 if ((strchr(name
, '/') || strchr(name
, '\\'))
1719 && access(name
, R_OK
) == 0) {
1720 return g_strdup(name
);
1723 case QEMU_FILE_TYPE_BIOS
:
1726 case QEMU_FILE_TYPE_KEYMAP
:
1727 subdir
= "keymaps/";
1732 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1733 buf
= g_malloc0(len
);
1734 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1735 if (access(buf
, R_OK
)) {
1742 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1744 return qdev_device_help(opts
);
1747 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1751 dev
= qdev_device_add(opts
);
1757 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1759 CharDriverState
*chr
;
1761 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1767 #ifdef CONFIG_VIRTFS
1768 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1771 ret
= qemu_fsdev_add(opts
);
1777 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1779 CharDriverState
*chr
;
1780 const char *chardev
;
1784 mode
= qemu_opt_get(opts
, "mode");
1788 if (strcmp(mode
, "readline") == 0) {
1789 flags
= MONITOR_USE_READLINE
;
1790 } else if (strcmp(mode
, "control") == 0) {
1791 flags
= MONITOR_USE_CONTROL
;
1793 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1797 if (qemu_opt_get_bool(opts
, "pretty", 0))
1798 flags
|= MONITOR_USE_PRETTY
;
1800 if (qemu_opt_get_bool(opts
, "default", 0))
1801 flags
|= MONITOR_IS_DEFAULT
;
1803 chardev
= qemu_opt_get(opts
, "chardev");
1804 chr
= qemu_chr_find(chardev
);
1806 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1810 monitor_init(chr
, flags
);
1814 static void monitor_parse(const char *optarg
, const char *mode
)
1816 static int monitor_device_index
= 0;
1822 if (strstart(optarg
, "chardev:", &p
)) {
1823 snprintf(label
, sizeof(label
), "%s", p
);
1825 snprintf(label
, sizeof(label
), "compat_monitor%d",
1826 monitor_device_index
);
1827 if (monitor_device_index
== 0) {
1830 opts
= qemu_chr_parse_compat(label
, optarg
);
1832 fprintf(stderr
, "parse error: %s\n", optarg
);
1837 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1839 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1842 qemu_opt_set(opts
, "mode", mode
);
1843 qemu_opt_set(opts
, "chardev", label
);
1845 qemu_opt_set(opts
, "default", "on");
1846 monitor_device_index
++;
1849 struct device_config
{
1851 DEV_USB
, /* -usbdevice */
1853 DEV_SERIAL
, /* -serial */
1854 DEV_PARALLEL
, /* -parallel */
1855 DEV_VIRTCON
, /* -virtioconsole */
1856 DEV_DEBUGCON
, /* -debugcon */
1858 const char *cmdline
;
1859 QTAILQ_ENTRY(device_config
) next
;
1861 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1863 static void add_device_config(int type
, const char *cmdline
)
1865 struct device_config
*conf
;
1867 conf
= g_malloc0(sizeof(*conf
));
1869 conf
->cmdline
= cmdline
;
1870 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1873 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1875 struct device_config
*conf
;
1878 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1879 if (conf
->type
!= type
)
1881 rc
= func(conf
->cmdline
);
1888 static int serial_parse(const char *devname
)
1890 static int index
= 0;
1893 if (strcmp(devname
, "none") == 0)
1895 if (index
== MAX_SERIAL_PORTS
) {
1896 fprintf(stderr
, "qemu: too many serial ports\n");
1899 snprintf(label
, sizeof(label
), "serial%d", index
);
1900 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1901 if (!serial_hds
[index
]) {
1902 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1903 devname
, strerror(errno
));
1910 static int parallel_parse(const char *devname
)
1912 static int index
= 0;
1915 if (strcmp(devname
, "none") == 0)
1917 if (index
== MAX_PARALLEL_PORTS
) {
1918 fprintf(stderr
, "qemu: too many parallel ports\n");
1921 snprintf(label
, sizeof(label
), "parallel%d", index
);
1922 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1923 if (!parallel_hds
[index
]) {
1924 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1925 devname
, strerror(errno
));
1932 static int virtcon_parse(const char *devname
)
1934 QemuOptsList
*device
= qemu_find_opts("device");
1935 static int index
= 0;
1937 QemuOpts
*bus_opts
, *dev_opts
;
1939 if (strcmp(devname
, "none") == 0)
1941 if (index
== MAX_VIRTIO_CONSOLES
) {
1942 fprintf(stderr
, "qemu: too many virtio consoles\n");
1946 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1947 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1949 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1950 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1952 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1953 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1954 if (!virtcon_hds
[index
]) {
1955 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1956 devname
, strerror(errno
));
1959 qemu_opt_set(dev_opts
, "chardev", label
);
1965 static int debugcon_parse(const char *devname
)
1969 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
1972 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1974 fprintf(stderr
, "qemu: already have a debugcon device\n");
1977 qemu_opt_set(opts
, "driver", "isa-debugcon");
1978 qemu_opt_set(opts
, "chardev", "debugcon");
1982 static QEMUMachine
*machine_parse(const char *name
)
1984 QEMUMachine
*m
, *machine
= NULL
;
1987 machine
= find_machine(name
);
1992 printf("Supported machines are:\n");
1993 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1995 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
1997 printf("%-10s %s%s\n", m
->name
, m
->desc
,
1998 m
->is_default
? " (default)" : "");
2000 exit(!name
|| *name
!= '?');
2003 static int tcg_init(void)
2005 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2010 const char *opt_name
;
2012 int (*available
)(void);
2016 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2017 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2018 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2021 static int configure_accelerator(void)
2023 const char *p
= NULL
;
2026 bool accel_initalised
= 0;
2027 bool init_failed
= 0;
2029 QemuOptsList
*list
= qemu_find_opts("machine");
2030 if (!QTAILQ_EMPTY(&list
->head
)) {
2031 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2036 /* Use the default "accelerator", kvm */
2039 /* Use the default "accelerator", tcg */
2044 while (!accel_initalised
&& *p
!= '\0') {
2048 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2049 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2050 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2051 *(accel_list
[i
].allowed
) = 1;
2052 ret
= accel_list
[i
].init();
2055 if (!accel_list
[i
].available()) {
2056 printf("%s not supported for this target\n",
2057 accel_list
[i
].name
);
2059 fprintf(stderr
, "failed to initialize %s: %s\n",
2063 *(accel_list
[i
].allowed
) = 0;
2065 accel_initalised
= 1;
2070 if (i
== ARRAY_SIZE(accel_list
)) {
2071 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2075 if (!accel_initalised
) {
2076 fprintf(stderr
, "No accelerator found!\n");
2081 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2084 return !accel_initalised
;
2087 void qemu_add_exit_notifier(Notifier
*notify
)
2089 notifier_list_add(&exit_notifiers
, notify
);
2092 void qemu_remove_exit_notifier(Notifier
*notify
)
2094 notifier_list_remove(&exit_notifiers
, notify
);
2097 static void qemu_run_exit_notifiers(void)
2099 notifier_list_notify(&exit_notifiers
, NULL
);
2102 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2104 notifier_list_add(&machine_init_done_notifiers
, notify
);
2107 static void qemu_run_machine_init_done_notifiers(void)
2109 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2112 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2113 const char **poptarg
, int *poptind
)
2115 const QEMUOption
*popt
;
2116 int optind
= *poptind
;
2117 char *r
= argv
[optind
];
2120 loc_set_cmdline(argv
, optind
, 1);
2122 /* Treat --foo the same as -foo. */
2125 popt
= qemu_options
;
2128 error_report("invalid option");
2131 if (!strcmp(popt
->name
, r
+ 1))
2135 if (popt
->flags
& HAS_ARG
) {
2136 if (optind
>= argc
) {
2137 error_report("requires an argument");
2140 optarg
= argv
[optind
++];
2141 loc_set_cmdline(argv
, optind
- 2, 2);
2152 static gpointer
malloc_and_trace(gsize n_bytes
)
2154 void *ptr
= malloc(n_bytes
);
2155 trace_g_malloc(n_bytes
, ptr
);
2159 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2161 void *ptr
= realloc(mem
, n_bytes
);
2162 trace_g_realloc(mem
, n_bytes
, ptr
);
2166 static void free_and_trace(gpointer mem
)
2172 int main(int argc
, char **argv
, char **envp
)
2174 const char *gdbstub_dev
= NULL
;
2176 int snapshot
, linux_boot
;
2177 const char *icount_option
= NULL
;
2178 const char *initrd_filename
;
2179 const char *kernel_filename
, *kernel_cmdline
;
2180 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2182 DisplayChangeListener
*dcl
;
2183 int cyls
, heads
, secs
, translation
;
2184 QemuOpts
*hda_opts
= NULL
, *opts
;
2185 QemuOptsList
*olist
;
2188 const char *loadvm
= NULL
;
2189 QEMUMachine
*machine
;
2190 const char *cpu_model
;
2191 const char *pid_file
= NULL
;
2192 const char *incoming
= NULL
;
2194 int show_vnc_port
= 0;
2197 const char *log_mask
= NULL
;
2198 const char *log_file
= NULL
;
2199 GMemVTable mem_trace
= {
2200 .malloc
= malloc_and_trace
,
2201 .realloc
= realloc_and_trace
,
2202 .free
= free_and_trace
,
2204 const char *trace_events
= NULL
;
2205 const char *trace_file
= NULL
;
2207 atexit(qemu_run_exit_notifiers
);
2208 error_set_progname(argv
[0]);
2210 g_mem_set_vtable(&mem_trace
);
2214 qemu_cache_utils_init(envp
);
2216 QLIST_INIT (&vm_change_state_head
);
2217 os_setup_early_signal_handling();
2219 module_call_init(MODULE_INIT_MACHINE
);
2220 machine
= find_default_machine();
2222 initrd_filename
= NULL
;
2225 kernel_filename
= NULL
;
2226 kernel_cmdline
= "";
2227 cyls
= heads
= secs
= 0;
2228 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2230 for (i
= 0; i
< MAX_NODES
; i
++) {
2232 node_cpumask
[i
] = 0;
2240 /* first pass of option parsing */
2242 while (optind
< argc
) {
2243 if (argv
[optind
][0] != '-') {
2248 const QEMUOption
*popt
;
2250 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2251 switch (popt
->index
) {
2252 case QEMU_OPTION_nodefconfig
:
2262 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2263 if (ret
< 0 && ret
!= -ENOENT
) {
2267 ret
= qemu_read_config_file(arch_config_name
);
2268 if (ret
< 0 && ret
!= -ENOENT
) {
2274 /* second pass of option parsing */
2279 if (argv
[optind
][0] != '-') {
2280 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2282 const QEMUOption
*popt
;
2284 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2285 if (!(popt
->arch_mask
& arch_type
)) {
2286 printf("Option %s not supported for this target\n", popt
->name
);
2289 switch(popt
->index
) {
2291 machine
= machine_parse(optarg
);
2293 case QEMU_OPTION_cpu
:
2294 /* hw initialization will check this */
2295 if (*optarg
== '?') {
2296 list_cpus(stdout
, &fprintf
, optarg
);
2302 case QEMU_OPTION_initrd
:
2303 initrd_filename
= optarg
;
2305 case QEMU_OPTION_hda
:
2309 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2311 snprintf(buf
, sizeof(buf
),
2312 "%s,cyls=%d,heads=%d,secs=%d%s",
2313 HD_OPTS
, cyls
, heads
, secs
,
2314 translation
== BIOS_ATA_TRANSLATION_LBA
?
2316 translation
== BIOS_ATA_TRANSLATION_NONE
?
2317 ",trans=none" : "");
2318 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2321 case QEMU_OPTION_hdb
:
2322 case QEMU_OPTION_hdc
:
2323 case QEMU_OPTION_hdd
:
2324 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2327 case QEMU_OPTION_drive
:
2328 if (drive_def(optarg
) == NULL
) {
2332 case QEMU_OPTION_set
:
2333 if (qemu_set_option(optarg
) != 0)
2336 case QEMU_OPTION_global
:
2337 if (qemu_global_option(optarg
) != 0)
2340 case QEMU_OPTION_mtdblock
:
2341 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2343 case QEMU_OPTION_sd
:
2344 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2346 case QEMU_OPTION_pflash
:
2347 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2349 case QEMU_OPTION_snapshot
:
2352 case QEMU_OPTION_hdachs
:
2356 cyls
= strtol(p
, (char **)&p
, 0);
2357 if (cyls
< 1 || cyls
> 16383)
2362 heads
= strtol(p
, (char **)&p
, 0);
2363 if (heads
< 1 || heads
> 16)
2368 secs
= strtol(p
, (char **)&p
, 0);
2369 if (secs
< 1 || secs
> 63)
2373 if (!strcmp(p
, "none"))
2374 translation
= BIOS_ATA_TRANSLATION_NONE
;
2375 else if (!strcmp(p
, "lba"))
2376 translation
= BIOS_ATA_TRANSLATION_LBA
;
2377 else if (!strcmp(p
, "auto"))
2378 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2381 } else if (*p
!= '\0') {
2383 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2386 if (hda_opts
!= NULL
) {
2388 snprintf(num
, sizeof(num
), "%d", cyls
);
2389 qemu_opt_set(hda_opts
, "cyls", num
);
2390 snprintf(num
, sizeof(num
), "%d", heads
);
2391 qemu_opt_set(hda_opts
, "heads", num
);
2392 snprintf(num
, sizeof(num
), "%d", secs
);
2393 qemu_opt_set(hda_opts
, "secs", num
);
2394 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2395 qemu_opt_set(hda_opts
, "trans", "lba");
2396 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2397 qemu_opt_set(hda_opts
, "trans", "none");
2401 case QEMU_OPTION_numa
:
2402 if (nb_numa_nodes
>= MAX_NODES
) {
2403 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2408 case QEMU_OPTION_display
:
2409 display_type
= select_display(optarg
);
2411 case QEMU_OPTION_nographic
:
2412 display_type
= DT_NOGRAPHIC
;
2414 case QEMU_OPTION_curses
:
2415 #ifdef CONFIG_CURSES
2416 display_type
= DT_CURSES
;
2418 fprintf(stderr
, "Curses support is disabled\n");
2422 case QEMU_OPTION_portrait
:
2423 graphic_rotate
= 90;
2425 case QEMU_OPTION_rotate
:
2426 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2427 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2428 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2430 "qemu: only 90, 180, 270 deg rotation is available\n");
2434 case QEMU_OPTION_kernel
:
2435 kernel_filename
= optarg
;
2437 case QEMU_OPTION_append
:
2438 kernel_cmdline
= optarg
;
2440 case QEMU_OPTION_cdrom
:
2441 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2443 case QEMU_OPTION_boot
:
2445 static const char * const params
[] = {
2446 "order", "once", "menu",
2447 "splash", "splash-time", NULL
2449 char buf
[sizeof(boot_devices
)];
2450 char *standard_boot_devices
;
2453 if (!strchr(optarg
, '=')) {
2455 pstrcpy(buf
, sizeof(buf
), optarg
);
2456 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2458 "qemu: unknown boot parameter '%s' in '%s'\n",
2464 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2465 validate_bootdevices(buf
);
2466 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2469 if (get_param_value(buf
, sizeof(buf
),
2471 validate_bootdevices(buf
);
2472 standard_boot_devices
= g_strdup(boot_devices
);
2473 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2474 qemu_register_reset(restore_boot_devices
,
2475 standard_boot_devices
);
2477 if (get_param_value(buf
, sizeof(buf
),
2479 if (!strcmp(buf
, "on")) {
2481 } else if (!strcmp(buf
, "off")) {
2485 "qemu: invalid option value '%s'\n",
2490 qemu_opts_parse(qemu_find_opts("boot-opts"),
2495 case QEMU_OPTION_fda
:
2496 case QEMU_OPTION_fdb
:
2497 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2500 case QEMU_OPTION_no_fd_bootchk
:
2503 case QEMU_OPTION_netdev
:
2504 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2508 case QEMU_OPTION_net
:
2509 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2514 case QEMU_OPTION_tftp
:
2515 legacy_tftp_prefix
= optarg
;
2517 case QEMU_OPTION_bootp
:
2518 legacy_bootp_filename
= optarg
;
2520 case QEMU_OPTION_redir
:
2521 if (net_slirp_redir(optarg
) < 0)
2525 case QEMU_OPTION_bt
:
2526 add_device_config(DEV_BT
, optarg
);
2528 case QEMU_OPTION_audio_help
:
2529 if (!(audio_available())) {
2530 printf("Option %s not supported for this target\n", popt
->name
);
2536 case QEMU_OPTION_soundhw
:
2537 if (!(audio_available())) {
2538 printf("Option %s not supported for this target\n", popt
->name
);
2541 select_soundhw (optarg
);
2546 case QEMU_OPTION_version
:
2550 case QEMU_OPTION_m
: {
2553 value
= strtosz(optarg
, NULL
);
2555 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2559 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2560 fprintf(stderr
, "qemu: ram size too large\n");
2566 case QEMU_OPTION_mempath
:
2570 case QEMU_OPTION_mem_prealloc
:
2581 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2583 case QEMU_OPTION_gdb
:
2584 gdbstub_dev
= optarg
;
2589 case QEMU_OPTION_bios
:
2592 case QEMU_OPTION_singlestep
:
2599 keyboard_layout
= optarg
;
2601 case QEMU_OPTION_localtime
:
2604 case QEMU_OPTION_vga
:
2605 select_vgahw (optarg
);
2612 w
= strtol(p
, (char **)&p
, 10);
2615 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2621 h
= strtol(p
, (char **)&p
, 10);
2626 depth
= strtol(p
, (char **)&p
, 10);
2627 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2628 depth
!= 24 && depth
!= 32)
2630 } else if (*p
== '\0') {
2631 depth
= graphic_depth
;
2638 graphic_depth
= depth
;
2641 case QEMU_OPTION_echr
:
2644 term_escape_char
= strtol(optarg
, &r
, 0);
2646 printf("Bad argument to echr\n");
2649 case QEMU_OPTION_monitor
:
2650 monitor_parse(optarg
, "readline");
2651 default_monitor
= 0;
2653 case QEMU_OPTION_qmp
:
2654 monitor_parse(optarg
, "control");
2655 default_monitor
= 0;
2657 case QEMU_OPTION_mon
:
2658 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2662 default_monitor
= 0;
2664 case QEMU_OPTION_chardev
:
2665 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2670 case QEMU_OPTION_fsdev
:
2671 olist
= qemu_find_opts("fsdev");
2673 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2676 opts
= qemu_opts_parse(olist
, optarg
, 1);
2678 fprintf(stderr
, "parse error: %s\n", optarg
);
2682 case QEMU_OPTION_virtfs
: {
2686 olist
= qemu_find_opts("virtfs");
2688 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2691 opts
= qemu_opts_parse(olist
, optarg
, 1);
2693 fprintf(stderr
, "parse error: %s\n", optarg
);
2697 if (qemu_opt_get(opts
, "fstype") == NULL
||
2698 qemu_opt_get(opts
, "mount_tag") == NULL
||
2699 qemu_opt_get(opts
, "path") == NULL
||
2700 qemu_opt_get(opts
, "security_model") == NULL
) {
2701 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2702 "security_model=[mapped|passthrough|none],"
2703 "mount_tag=tag.\n");
2707 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2708 qemu_opt_get(opts
, "mount_tag"), 1);
2710 fprintf(stderr
, "duplicate fsdev id: %s\n",
2711 qemu_opt_get(opts
, "mount_tag"));
2714 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2715 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2716 qemu_opt_set(fsdev
, "security_model",
2717 qemu_opt_get(opts
, "security_model"));
2719 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2720 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2721 qemu_opt_set(device
, "fsdev",
2722 qemu_opt_get(opts
, "mount_tag"));
2723 qemu_opt_set(device
, "mount_tag",
2724 qemu_opt_get(opts
, "mount_tag"));
2727 case QEMU_OPTION_serial
:
2728 add_device_config(DEV_SERIAL
, optarg
);
2730 if (strncmp(optarg
, "mon:", 4) == 0) {
2731 default_monitor
= 0;
2734 case QEMU_OPTION_watchdog
:
2737 "qemu: only one watchdog option may be given\n");
2742 case QEMU_OPTION_watchdog_action
:
2743 if (select_watchdog_action(optarg
) == -1) {
2744 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2748 case QEMU_OPTION_virtiocon
:
2749 add_device_config(DEV_VIRTCON
, optarg
);
2750 default_virtcon
= 0;
2751 if (strncmp(optarg
, "mon:", 4) == 0) {
2752 default_monitor
= 0;
2755 case QEMU_OPTION_parallel
:
2756 add_device_config(DEV_PARALLEL
, optarg
);
2757 default_parallel
= 0;
2758 if (strncmp(optarg
, "mon:", 4) == 0) {
2759 default_monitor
= 0;
2762 case QEMU_OPTION_debugcon
:
2763 add_device_config(DEV_DEBUGCON
, optarg
);
2765 case QEMU_OPTION_loadvm
:
2768 case QEMU_OPTION_full_screen
:
2772 case QEMU_OPTION_no_frame
:
2775 case QEMU_OPTION_alt_grab
:
2778 case QEMU_OPTION_ctrl_grab
:
2781 case QEMU_OPTION_no_quit
:
2784 case QEMU_OPTION_sdl
:
2785 display_type
= DT_SDL
;
2788 case QEMU_OPTION_no_frame
:
2789 case QEMU_OPTION_alt_grab
:
2790 case QEMU_OPTION_ctrl_grab
:
2791 case QEMU_OPTION_no_quit
:
2792 case QEMU_OPTION_sdl
:
2793 fprintf(stderr
, "SDL support is disabled\n");
2796 case QEMU_OPTION_pidfile
:
2799 case QEMU_OPTION_win2k_hack
:
2800 win2k_install_hack
= 1;
2802 case QEMU_OPTION_rtc_td_hack
:
2805 case QEMU_OPTION_acpitable
:
2806 do_acpitable_option(optarg
);
2808 case QEMU_OPTION_smbios
:
2809 do_smbios_option(optarg
);
2811 case QEMU_OPTION_enable_kvm
:
2812 olist
= qemu_find_opts("machine");
2813 qemu_opts_reset(olist
);
2814 qemu_opts_parse(olist
, "accel=kvm", 0);
2816 case QEMU_OPTION_machine
:
2817 olist
= qemu_find_opts("machine");
2818 qemu_opts_reset(olist
);
2819 opts
= qemu_opts_parse(olist
, optarg
, 1);
2821 fprintf(stderr
, "parse error: %s\n", optarg
);
2824 optarg
= qemu_opt_get(opts
, "type");
2826 machine
= machine_parse(optarg
);
2829 case QEMU_OPTION_no_kvm
:
2830 olist
= qemu_find_opts("machine");
2831 qemu_opts_reset(olist
);
2832 qemu_opts_parse(olist
, "accel=tcg", 0);
2835 case QEMU_OPTION_no_kvm_irqchip
: {
2840 case QEMU_OPTION_no_kvm_pit
: {
2844 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2845 kvm_pit_reinject
= 0;
2849 case QEMU_OPTION_usb
:
2852 case QEMU_OPTION_usbdevice
:
2854 add_device_config(DEV_USB
, optarg
);
2856 case QEMU_OPTION_device
:
2857 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2861 case QEMU_OPTION_smp
:
2864 fprintf(stderr
, "Invalid number of CPUs\n");
2867 if (max_cpus
< smp_cpus
) {
2868 fprintf(stderr
, "maxcpus must be equal to or greater than "
2872 if (max_cpus
> 255) {
2873 fprintf(stderr
, "Unsupported number of maxcpus\n");
2877 case QEMU_OPTION_vnc
:
2880 vnc_display
= optarg
;
2882 fprintf(stderr
, "VNC support is disabled\n");
2886 case QEMU_OPTION_no_acpi
:
2889 case QEMU_OPTION_no_hpet
:
2892 case QEMU_OPTION_balloon
:
2893 if (balloon_parse(optarg
) < 0) {
2894 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2898 case QEMU_OPTION_no_reboot
:
2901 case QEMU_OPTION_no_shutdown
:
2904 case QEMU_OPTION_show_cursor
:
2907 case QEMU_OPTION_uuid
:
2908 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2909 fprintf(stderr
, "Fail to parse UUID string."
2910 " Wrong format.\n");
2914 case QEMU_OPTION_option_rom
:
2915 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2916 fprintf(stderr
, "Too many option ROMs\n");
2919 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2920 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2921 option_rom
[nb_option_roms
].bootindex
=
2922 qemu_opt_get_number(opts
, "bootindex", -1);
2923 if (!option_rom
[nb_option_roms
].name
) {
2924 fprintf(stderr
, "Option ROM file is not specified\n");
2929 case QEMU_OPTION_semihosting
:
2930 semihosting_enabled
= 1;
2932 case QEMU_OPTION_tdf
:
2935 case QEMU_OPTION_kvm_shadow_memory
:
2936 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
2938 case QEMU_OPTION_name
:
2939 qemu_name
= g_strdup(optarg
);
2941 char *p
= strchr(qemu_name
, ',');
2944 if (strncmp(p
, "process=", 8)) {
2945 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2949 os_set_proc_name(p
);
2953 case QEMU_OPTION_prom_env
:
2954 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2955 fprintf(stderr
, "Too many prom variables\n");
2958 prom_envs
[nb_prom_envs
] = optarg
;
2961 case QEMU_OPTION_old_param
:
2964 case QEMU_OPTION_clock
:
2965 configure_alarms(optarg
);
2967 case QEMU_OPTION_startdate
:
2968 configure_rtc_date_offset(optarg
, 1);
2970 case QEMU_OPTION_rtc
:
2971 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2975 configure_rtc(opts
);
2977 case QEMU_OPTION_tb_size
:
2978 tcg_tb_size
= strtol(optarg
, NULL
, 0);
2979 if (tcg_tb_size
< 0) {
2983 case QEMU_OPTION_icount
:
2984 icount_option
= optarg
;
2986 case QEMU_OPTION_incoming
:
2988 incoming_expected
= true;
2990 case QEMU_OPTION_nodefaults
:
2992 default_parallel
= 0;
2993 default_virtcon
= 0;
2994 default_monitor
= 0;
3002 case QEMU_OPTION_nvram
:
3006 case QEMU_OPTION_xen_domid
:
3007 if (!(xen_available())) {
3008 printf("Option %s not supported for this target\n", popt
->name
);
3011 xen_domid
= atoi(optarg
);
3013 case QEMU_OPTION_xen_create
:
3014 if (!(xen_available())) {
3015 printf("Option %s not supported for this target\n", popt
->name
);
3018 xen_mode
= XEN_CREATE
;
3020 case QEMU_OPTION_xen_attach
:
3021 if (!(xen_available())) {
3022 printf("Option %s not supported for this target\n", popt
->name
);
3025 xen_mode
= XEN_ATTACH
;
3027 case QEMU_OPTION_trace
:
3029 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3033 trace_events
= qemu_opt_get(opts
, "events");
3034 trace_file
= qemu_opt_get(opts
, "file");
3037 case QEMU_OPTION_readconfig
:
3039 int ret
= qemu_read_config_file(optarg
);
3041 fprintf(stderr
, "read config %s: %s\n", optarg
,
3047 case QEMU_OPTION_spice
:
3048 olist
= qemu_find_opts("spice");
3050 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3053 opts
= qemu_opts_parse(olist
, optarg
, 0);
3055 fprintf(stderr
, "parse error: %s\n", optarg
);
3059 case QEMU_OPTION_writeconfig
:
3062 if (strcmp(optarg
, "-") == 0) {
3065 fp
= fopen(optarg
, "w");
3067 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3071 qemu_config_write(fp
);
3076 os_parse_cmd_args(popt
->index
, optarg
);
3082 /* Open the logfile at this point, if necessary. We can't open the logfile
3083 * when encountering either of the logging options (-d or -D) because the
3084 * other one may be encountered later on the command line, changing the
3085 * location or level of logging.
3089 set_cpu_log_filename(log_file
);
3091 set_cpu_log(log_mask
);
3094 if (!trace_backend_init(trace_events
, trace_file
)) {
3098 /* If no data_dir is specified then try to find it relative to the
3101 data_dir
= os_find_datadir(argv
[0]);
3103 /* If all else fails use the install patch specified when building. */
3105 data_dir
= CONFIG_QEMU_DATADIR
;
3109 * Default to max_cpus = smp_cpus, in case the user doesn't
3110 * specify a max_cpus value.
3113 max_cpus
= smp_cpus
;
3115 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3116 if (smp_cpus
> machine
->max_cpus
) {
3117 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3118 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3124 * Get the default machine options from the machine if it is not already
3125 * specified either by the configuration file or by the command line.
3127 if (machine
->default_machine_opts
) {
3128 QemuOptsList
*list
= qemu_find_opts("machine");
3129 const char *p
= NULL
;
3131 if (!QTAILQ_EMPTY(&list
->head
)) {
3132 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3135 qemu_opts_reset(list
);
3136 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3138 fprintf(stderr
, "parse error for machine %s: %s\n",
3139 machine
->name
, machine
->default_machine_opts
);
3145 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3146 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3148 if (machine
->no_serial
) {
3151 if (machine
->no_parallel
) {
3152 default_parallel
= 0;
3154 if (!machine
->use_virtcon
) {
3155 default_virtcon
= 0;
3157 if (machine
->no_vga
) {
3160 if (machine
->no_floppy
) {
3163 if (machine
->no_cdrom
) {
3166 if (machine
->no_sdcard
) {
3170 if (display_type
== DT_NOGRAPHIC
) {
3171 if (default_parallel
)
3172 add_device_config(DEV_PARALLEL
, "null");
3173 if (default_serial
&& default_monitor
) {
3174 add_device_config(DEV_SERIAL
, "mon:stdio");
3175 } else if (default_virtcon
&& default_monitor
) {
3176 add_device_config(DEV_VIRTCON
, "mon:stdio");
3179 add_device_config(DEV_SERIAL
, "stdio");
3180 if (default_virtcon
)
3181 add_device_config(DEV_VIRTCON
, "stdio");
3182 if (default_monitor
)
3183 monitor_parse("stdio", "readline");
3187 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3188 if (default_parallel
)
3189 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3190 if (default_monitor
)
3191 monitor_parse("vc:80Cx24C", "readline");
3192 if (default_virtcon
)
3193 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3196 vga_interface_type
= VGA_CIRRUS
;
3200 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3202 #ifdef CONFIG_VIRTFS
3203 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3210 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3215 /* init the memory */
3216 if (ram_size
== 0) {
3217 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3220 configure_accelerator();
3222 if (qemu_init_main_loop()) {
3223 fprintf(stderr
, "qemu_init_main_loop failed\n");
3226 linux_boot
= (kernel_filename
!= NULL
);
3228 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3229 fprintf(stderr
, "-append only allowed with -kernel option\n");
3233 if (!linux_boot
&& initrd_filename
!= NULL
) {
3234 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3238 os_set_line_buffering();
3240 if (init_timer_alarm() < 0) {
3241 fprintf(stderr
, "could not initialize alarm timer\n");
3244 configure_icount(icount_option
);
3246 if (net_init_clients() < 0) {
3250 /* init the bluetooth world */
3251 if (foreach_device_config(DEV_BT
, bt_parse
))
3254 if (!xen_enabled()) {
3255 /* On 32-bit hosts, QEMU is limited by virtual address space */
3256 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3257 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3262 cpu_exec_init_all();
3264 bdrv_init_with_whitelist();
3268 /* open the virtual block devices */
3270 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3271 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3274 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3275 IF_DEFAULT
, 2, CDROM_OPTS
);
3276 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3277 IF_FLOPPY
, 0, FD_OPTS
);
3278 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3281 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3284 if (nb_numa_nodes
> 0) {
3287 if (nb_numa_nodes
> MAX_NODES
) {
3288 nb_numa_nodes
= MAX_NODES
;
3291 /* If no memory size if given for any node, assume the default case
3292 * and distribute the available memory equally across all nodes
3294 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3295 if (node_mem
[i
] != 0)
3298 if (i
== nb_numa_nodes
) {
3299 uint64_t usedmem
= 0;
3301 /* On Linux, the each node's border has to be 8MB aligned,
3302 * the final node gets the rest.
3304 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3305 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3306 usedmem
+= node_mem
[i
];
3308 node_mem
[i
] = ram_size
- usedmem
;
3311 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3312 if (node_cpumask
[i
] != 0)
3315 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3316 * must cope with this anyway, because there are BIOSes out there in
3317 * real machines which also use this scheme.
3319 if (i
== nb_numa_nodes
) {
3320 for (i
= 0; i
< smp_cpus
; i
++) {
3321 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3326 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3330 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3332 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3334 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3336 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3339 module_call_init(MODULE_INIT_DEVICE
);
3341 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3345 i
= select_watchdog(watchdog
);
3347 exit (i
== 1 ? 1 : 0);
3350 if (machine
->compat_props
) {
3351 qdev_prop_register_global_list(machine
->compat_props
);
3355 machine
->init(ram_size
, boot_devices
,
3356 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3358 cpu_synchronize_all_post_init();
3362 current_machine
= machine
;
3364 /* init USB devices */
3366 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3370 /* init generic devices */
3371 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3374 net_check_clients();
3376 /* just use the first displaystate for the moment */
3377 ds
= get_displaystate();
3381 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3382 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3383 display_type
= DT_SDL
;
3384 #elif defined(CONFIG_VNC)
3385 vnc_display
= "localhost:0,to=99";
3388 display_type
= DT_NONE
;
3393 /* init local displays */
3394 switch (display_type
) {
3397 #if defined(CONFIG_CURSES)
3399 curses_display_init(ds
, full_screen
);
3402 #if defined(CONFIG_SDL)
3404 sdl_display_init(ds
, full_screen
, no_frame
);
3406 #elif defined(CONFIG_COCOA)
3408 cocoa_display_init(ds
, full_screen
);
3415 /* must be after terminal init, SDL library changes signal handlers */
3416 os_setup_signal_handling();
3419 /* init remote displays */
3421 vnc_display_init(ds
);
3422 if (vnc_display_open(ds
, vnc_display
) < 0)
3425 if (show_vnc_port
) {
3426 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3431 if (using_spice
&& !qxl_enabled
) {
3432 qemu_spice_display_init(ds
);
3438 dcl
= ds
->listeners
;
3439 while (dcl
!= NULL
) {
3440 if (dcl
->dpy_refresh
!= NULL
) {
3441 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3442 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3447 if (ds
->gui_timer
== NULL
) {
3448 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3449 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3451 text_consoles_set_display(ds
);
3453 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3454 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3459 qdev_machine_creation_done();
3461 if (rom_load_all() != 0) {
3462 fprintf(stderr
, "rom loading failed\n");
3466 /* TODO: once all bus devices are qdevified, this should be done
3467 * when bus is created by qdev.c */
3468 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3469 qemu_run_machine_init_done_notifiers();
3471 qemu_system_reset(VMRESET_SILENT
);
3473 if (load_vmstate(loadvm
) < 0) {
3479 int ret
= qemu_start_incoming_migration(incoming
);
3481 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3485 } else if (autostart
) {