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 */
115 #include "hw/boards.h"
117 #include "hw/pcmcia.h"
122 #include "hw/watchdog.h"
123 #include "hw/smbios.h"
126 #include "hw/loader.h"
129 #include "net/slirp.h"
134 #include "qemu-timer.h"
135 #include "qemu-char.h"
136 #include "cache-utils.h"
138 #include "blockdev.h"
139 #include "block-migration.h"
141 #include "audio/audio.h"
142 #include "migration.h"
144 #include "qemu-option.h"
145 #include "qemu-config.h"
146 #include "qemu-objects.h"
147 #include "qemu-options.h"
149 #include "fsdev/qemu-fsdev.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
159 #include "simpletrace.h"
160 #include "qemu-queue.h"
162 #include "arch_init.h"
164 #include "ui/qemu-spice.h"
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir
;
174 const char *bios_name
= NULL
;
175 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
176 DisplayType display_type
= DT_DEFAULT
;
177 int display_remote
= 0;
178 const char* keyboard_layout
= NULL
;
180 const char *mem_path
= NULL
;
182 int mem_prealloc
= 0; /* force preallocation of physical target memory */
185 NICInfo nd_table
[MAX_NICS
];
188 int incoming_expected
; /* Started with -incoming and waiting for incoming */
189 static int rtc_utc
= 1;
190 static int rtc_date_offset
= -1; /* -1 means no change */
191 QEMUClock
*rtc_clock
;
192 int vga_interface_type
= VGA_NONE
;
193 static int full_screen
= 0;
195 static int no_frame
= 0;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
201 int win2k_install_hack
= 0;
210 const char *vnc_display
;
212 int acpi_enabled
= 1;
218 int graphic_rotate
= 0;
219 uint8_t irq0override
= 1;
220 const char *watchdog
;
221 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
225 const char *qemu_name
;
228 unsigned int nb_prom_envs
= 0;
229 const char *prom_envs
[MAX_PROM_ENVS
];
231 uint8_t *boot_splash_filedata
;
232 int boot_splash_filedata_size
;
233 uint8_t qemu_extra_params_fw
[2];
235 typedef struct FWBootEntry FWBootEntry
;
238 QTAILQ_ENTRY(FWBootEntry
) link
;
244 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
247 uint64_t node_mem
[MAX_NODES
];
248 uint64_t node_cpumask
[MAX_NODES
];
250 static QEMUTimer
*nographic_timer
;
252 uint8_t qemu_uuid
[16];
254 static QEMUBootSetHandler
*boot_set_handler
;
255 static void *boot_set_opaque
;
257 static NotifierList exit_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
260 static NotifierList machine_init_done_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
263 static int tcg_allowed
= 1;
267 enum xen_mode xen_mode
= XEN_EMULATE
;
268 static int tcg_tb_size
;
270 static int default_serial
= 1;
271 static int default_parallel
= 1;
272 static int default_virtcon
= 1;
273 static int default_monitor
= 1;
274 static int default_vga
= 1;
275 static int default_floppy
= 1;
276 static int default_cdrom
= 1;
277 static int default_sdcard
= 1;
283 { .driver
= "isa-serial", .flag
= &default_serial
},
284 { .driver
= "isa-parallel", .flag
= &default_parallel
},
285 { .driver
= "isa-fdc", .flag
= &default_floppy
},
286 { .driver
= "ide-cd", .flag
= &default_cdrom
},
287 { .driver
= "ide-hd", .flag
= &default_cdrom
},
288 { .driver
= "ide-drive", .flag
= &default_cdrom
},
289 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
290 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "cirrus-vga", .flag
= &default_vga
},
295 { .driver
= "vmware-svga", .flag
= &default_vga
},
296 { .driver
= "isa-vga", .flag
= &default_vga
},
297 { .driver
= "qxl-vga", .flag
= &default_vga
},
300 static void res_free(void)
302 if (boot_splash_filedata
!= NULL
) {
303 g_free(boot_splash_filedata
);
304 boot_splash_filedata
= NULL
;
308 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
310 const char *driver
= qemu_opt_get(opts
, "driver");
315 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
316 if (strcmp(default_list
[i
].driver
, driver
) != 0)
318 *(default_list
[i
].flag
) = 0;
323 /***********************************************************/
324 /* real time host monotonic timer */
326 /***********************************************************/
327 /* host time/date access */
328 void qemu_get_timedate(struct tm
*tm
, int offset
)
335 if (rtc_date_offset
== -1) {
339 ret
= localtime(&ti
);
341 ti
-= rtc_date_offset
;
345 memcpy(tm
, ret
, sizeof(struct tm
));
348 int qemu_timedate_diff(struct tm
*tm
)
352 if (rtc_date_offset
== -1)
354 seconds
= mktimegm(tm
);
356 seconds
= mktime(tm
);
358 seconds
= mktimegm(tm
) + rtc_date_offset
;
360 return seconds
- time(NULL
);
363 void rtc_change_mon_event(struct tm
*tm
)
367 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
368 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
369 qobject_decref(data
);
372 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
374 time_t rtc_start_date
;
377 if (!strcmp(startdate
, "now") && legacy
) {
378 rtc_date_offset
= -1;
380 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
388 } else if (sscanf(startdate
, "%d-%d-%d",
400 rtc_start_date
= mktimegm(&tm
);
401 if (rtc_start_date
== -1) {
403 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
404 "'2006-06-17T16:01:21' or '2006-06-17'\n");
407 rtc_date_offset
= time(NULL
) - rtc_start_date
;
411 static void configure_rtc(QemuOpts
*opts
)
415 value
= qemu_opt_get(opts
, "base");
417 if (!strcmp(value
, "utc")) {
419 } else if (!strcmp(value
, "localtime")) {
422 configure_rtc_date_offset(value
, 0);
425 value
= qemu_opt_get(opts
, "clock");
427 if (!strcmp(value
, "host")) {
428 rtc_clock
= host_clock
;
429 } else if (!strcmp(value
, "vm")) {
430 rtc_clock
= vm_clock
;
432 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
436 value
= qemu_opt_get(opts
, "driftfix");
438 if (!strcmp(value
, "slew")) {
440 } else if (!strcmp(value
, "none")) {
443 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
449 /***********************************************************/
450 /* Bluetooth support */
453 static struct HCIInfo
*hci_table
[MAX_NICS
];
455 static struct bt_vlan_s
{
456 struct bt_scatternet_s net
;
458 struct bt_vlan_s
*next
;
461 /* find or alloc a new bluetooth "VLAN" */
462 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
464 struct bt_vlan_s
**pvlan
, *vlan
;
465 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
469 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
471 pvlan
= &first_bt_vlan
;
472 while (*pvlan
!= NULL
)
473 pvlan
= &(*pvlan
)->next
;
478 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
482 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
487 static struct HCIInfo null_hci
= {
488 .cmd_send
= null_hci_send
,
489 .sco_send
= null_hci_send
,
490 .acl_send
= null_hci_send
,
491 .bdaddr_set
= null_hci_addr_set
,
494 struct HCIInfo
*qemu_next_hci(void)
496 if (cur_hci
== nb_hcis
)
499 return hci_table
[cur_hci
++];
502 static struct HCIInfo
*hci_init(const char *str
)
505 struct bt_scatternet_s
*vlan
= 0;
507 if (!strcmp(str
, "null"))
510 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
512 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
513 else if (!strncmp(str
, "hci", 3)) {
516 if (!strncmp(str
+ 3, ",vlan=", 6)) {
517 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
522 vlan
= qemu_find_bt_vlan(0);
524 return bt_new_hci(vlan
);
527 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
532 static int bt_hci_parse(const char *str
)
537 if (nb_hcis
>= MAX_NICS
) {
538 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
551 bdaddr
.b
[5] = 0x56 + nb_hcis
;
552 hci
->bdaddr_set(hci
, bdaddr
.b
);
554 hci_table
[nb_hcis
++] = hci
;
559 static void bt_vhci_add(int vlan_id
)
561 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
564 fprintf(stderr
, "qemu: warning: adding a VHCI to "
565 "an empty scatternet %i\n", vlan_id
);
567 bt_vhci_init(bt_new_hci(vlan
));
570 static struct bt_device_s
*bt_device_add(const char *opt
)
572 struct bt_scatternet_s
*vlan
;
574 char *endp
= strstr(opt
, ",vlan=");
575 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
578 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
581 vlan_id
= strtol(endp
+ 6, &endp
, 0);
583 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
588 vlan
= qemu_find_bt_vlan(vlan_id
);
591 fprintf(stderr
, "qemu: warning: adding a slave device to "
592 "an empty scatternet %i\n", vlan_id
);
594 if (!strcmp(devname
, "keyboard"))
595 return bt_keyboard_init(vlan
);
597 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
601 static int bt_parse(const char *opt
)
603 const char *endp
, *p
;
606 if (strstart(opt
, "hci", &endp
)) {
607 if (!*endp
|| *endp
== ',') {
609 if (!strstart(endp
, ",vlan=", 0))
612 return bt_hci_parse(opt
);
614 } else if (strstart(opt
, "vhci", &endp
)) {
615 if (!*endp
|| *endp
== ',') {
617 if (strstart(endp
, ",vlan=", &p
)) {
618 vlan
= strtol(p
, (char **) &endp
, 0);
620 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
624 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
633 } else if (strstart(opt
, "device:", &endp
))
634 return !bt_device_add(endp
);
636 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
640 /***********************************************************/
641 /* QEMU Block devices */
643 #define HD_OPTS "media=disk"
644 #define CDROM_OPTS "media=cdrom"
646 #define PFLASH_OPTS ""
650 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
652 int *use_scsi
= opaque
;
654 return drive_init(opts
, *use_scsi
) == NULL
;
657 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
659 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
660 qemu_opt_set(opts
, "snapshot", "on");
665 static void default_drive(int enable
, int snapshot
, int use_scsi
,
666 BlockInterfaceType type
, int index
,
671 if (type
== IF_DEFAULT
) {
672 type
= use_scsi
? IF_SCSI
: IF_IDE
;
675 if (!enable
|| drive_get_by_index(type
, index
)) {
679 opts
= drive_add(type
, index
, NULL
, optstr
);
681 drive_enable_snapshot(opts
, NULL
);
683 if (!drive_init(opts
, use_scsi
)) {
688 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
690 boot_set_handler
= func
;
691 boot_set_opaque
= opaque
;
694 int qemu_boot_set(const char *boot_devices
)
696 if (!boot_set_handler
) {
699 return boot_set_handler(boot_set_opaque
, boot_devices
);
702 static void validate_bootdevices(char *devices
)
704 /* We just do some generic consistency checks */
708 for (p
= devices
; *p
!= '\0'; p
++) {
709 /* Allowed boot devices are:
710 * a-b: floppy disk drives
711 * c-f: IDE disk drives
712 * g-m: machine implementation dependant drives
713 * n-p: network devices
714 * It's up to each machine implementation to check if the given boot
715 * devices match the actual hardware implementation and firmware
718 if (*p
< 'a' || *p
> 'p') {
719 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
722 if (bitmap
& (1 << (*p
- 'a'))) {
723 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
726 bitmap
|= 1 << (*p
- 'a');
730 static void restore_boot_devices(void *opaque
)
732 char *standard_boot_devices
= opaque
;
733 static int first
= 1;
735 /* Restore boot order and remove ourselves after the first boot */
741 qemu_boot_set(standard_boot_devices
);
743 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
744 g_free(standard_boot_devices
);
747 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
750 FWBootEntry
*node
, *i
;
756 assert(dev
!= NULL
|| suffix
!= NULL
);
758 node
= g_malloc0(sizeof(FWBootEntry
));
759 node
->bootindex
= bootindex
;
760 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
763 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
764 if (i
->bootindex
== bootindex
) {
765 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
767 } else if (i
->bootindex
< bootindex
) {
770 QTAILQ_INSERT_BEFORE(i
, node
, link
);
773 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
777 * This function returns null terminated string that consist of new line
778 * separated device paths.
780 * memory pointed by "size" is assigned total length of the array in bytes
783 char *get_boot_devices_list(uint32_t *size
)
789 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
790 char *devpath
= NULL
, *bootpath
;
794 devpath
= qdev_get_fw_dev_path(i
->dev
);
798 if (i
->suffix
&& devpath
) {
799 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
801 bootpath
= g_malloc(bootpathlen
);
802 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
804 } else if (devpath
) {
807 bootpath
= g_strdup(i
->suffix
);
812 list
[total
-1] = '\n';
814 len
= strlen(bootpath
) + 1;
815 list
= g_realloc(list
, total
+ len
);
816 memcpy(&list
[total
], bootpath
, len
);
826 static void numa_add(const char *optarg
)
830 unsigned long long value
, endvalue
;
833 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
834 if (!strcmp(option
, "node")) {
835 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
836 nodenr
= nb_numa_nodes
;
838 nodenr
= strtoull(option
, NULL
, 10);
841 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
842 node_mem
[nodenr
] = 0;
845 sval
= strtosz(option
, NULL
);
847 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
850 node_mem
[nodenr
] = sval
;
852 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
853 node_cpumask
[nodenr
] = 0;
855 value
= strtoull(option
, &endptr
, 10);
858 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
860 if (*endptr
== '-') {
861 endvalue
= strtoull(endptr
+1, &endptr
, 10);
862 if (endvalue
>= 63) {
865 "only 63 CPUs in NUMA mode supported.\n");
867 value
= (2ULL << endvalue
) - (1ULL << value
);
869 value
= 1ULL << value
;
872 node_cpumask
[nodenr
] = value
;
879 static void smp_parse(const char *optarg
)
881 int smp
, sockets
= 0, threads
= 0, cores
= 0;
885 smp
= strtoul(optarg
, &endptr
, 10);
886 if (endptr
!= optarg
) {
887 if (*endptr
== ',') {
891 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
892 sockets
= strtoull(option
, NULL
, 10);
893 if (get_param_value(option
, 128, "cores", endptr
) != 0)
894 cores
= strtoull(option
, NULL
, 10);
895 if (get_param_value(option
, 128, "threads", endptr
) != 0)
896 threads
= strtoull(option
, NULL
, 10);
897 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
898 max_cpus
= strtoull(option
, NULL
, 10);
900 /* compute missing values, prefer sockets over cores over threads */
901 if (smp
== 0 || sockets
== 0) {
902 sockets
= sockets
> 0 ? sockets
: 1;
903 cores
= cores
> 0 ? cores
: 1;
904 threads
= threads
> 0 ? threads
: 1;
906 smp
= cores
* threads
* sockets
;
910 threads
= threads
> 0 ? threads
: 1;
911 cores
= smp
/ (sockets
* threads
);
913 threads
= smp
/ (cores
* sockets
);
917 smp_cores
= cores
> 0 ? cores
: 1;
918 smp_threads
= threads
> 0 ? threads
: 1;
923 /***********************************************************/
926 static int usb_device_add(const char *devname
)
929 USBDevice
*dev
= NULL
;
934 /* drivers with .usbdevice_name entry in USBDeviceInfo */
935 dev
= usbdevice_create(devname
);
941 /* only the linux version is qdev-ified, usb-bsd still needs this */
942 if (strstart(devname
, "host:", &p
)) {
943 dev
= usb_host_device_open(p
);
946 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
947 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
948 bt_new_hci(qemu_find_bt_vlan(0)));
959 static int usb_device_del(const char *devname
)
964 if (strstart(devname
, "host:", &p
))
965 return usb_host_device_close(p
);
970 p
= strchr(devname
, '.');
973 bus_num
= strtoul(devname
, NULL
, 0);
974 addr
= strtoul(p
+ 1, NULL
, 0);
976 return usb_device_delete_addr(bus_num
, addr
);
979 static int usb_parse(const char *cmdline
)
982 r
= usb_device_add(cmdline
);
984 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
989 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
991 const char *devname
= qdict_get_str(qdict
, "devname");
992 if (usb_device_add(devname
) < 0) {
993 error_report("could not add USB device '%s'", devname
);
997 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
999 const char *devname
= qdict_get_str(qdict
, "devname");
1000 if (usb_device_del(devname
) < 0) {
1001 error_report("could not delete USB device '%s'", devname
);
1005 /***********************************************************/
1006 /* PCMCIA/Cardbus */
1008 static struct pcmcia_socket_entry_s
{
1009 PCMCIASocket
*socket
;
1010 struct pcmcia_socket_entry_s
*next
;
1011 } *pcmcia_sockets
= 0;
1013 void pcmcia_socket_register(PCMCIASocket
*socket
)
1015 struct pcmcia_socket_entry_s
*entry
;
1017 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1018 entry
->socket
= socket
;
1019 entry
->next
= pcmcia_sockets
;
1020 pcmcia_sockets
= entry
;
1023 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1025 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1027 ptr
= &pcmcia_sockets
;
1028 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1029 if (entry
->socket
== socket
) {
1035 void pcmcia_info(Monitor
*mon
)
1037 struct pcmcia_socket_entry_s
*iter
;
1039 if (!pcmcia_sockets
)
1040 monitor_printf(mon
, "No PCMCIA sockets\n");
1042 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1043 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1044 iter
->socket
->attached
? iter
->socket
->card_string
:
1048 /***********************************************************/
1049 /* machine registration */
1051 static QEMUMachine
*first_machine
= NULL
;
1052 QEMUMachine
*current_machine
= NULL
;
1054 int qemu_register_machine(QEMUMachine
*m
)
1057 pm
= &first_machine
;
1065 static QEMUMachine
*find_machine(const char *name
)
1069 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1070 if (!strcmp(m
->name
, name
))
1072 if (m
->alias
&& !strcmp(m
->alias
, name
))
1078 static QEMUMachine
*find_default_machine(void)
1082 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1083 if (m
->is_default
) {
1090 /***********************************************************/
1091 /* main execution loop */
1093 static void gui_update(void *opaque
)
1095 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1096 DisplayState
*ds
= opaque
;
1097 DisplayChangeListener
*dcl
= ds
->listeners
;
1099 qemu_flush_coalesced_mmio_buffer();
1102 while (dcl
!= NULL
) {
1103 if (dcl
->gui_timer_interval
&&
1104 dcl
->gui_timer_interval
< interval
)
1105 interval
= dcl
->gui_timer_interval
;
1108 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1111 static void nographic_update(void *opaque
)
1113 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1115 qemu_flush_coalesced_mmio_buffer();
1116 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1119 struct vm_change_state_entry
{
1120 VMChangeStateHandler
*cb
;
1122 QLIST_ENTRY (vm_change_state_entry
) entries
;
1125 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1127 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1130 VMChangeStateEntry
*e
;
1132 e
= g_malloc0(sizeof (*e
));
1136 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1140 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1142 QLIST_REMOVE (e
, entries
);
1146 void vm_state_notify(int running
, int reason
)
1148 VMChangeStateEntry
*e
;
1150 trace_vm_state_notify(running
, reason
);
1152 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1153 e
->cb(e
->opaque
, running
, reason
);
1162 vm_state_notify(1, 0);
1164 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1168 /* reset/shutdown handler */
1170 typedef struct QEMUResetEntry
{
1171 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1172 QEMUResetHandler
*func
;
1176 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1177 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1178 static int reset_requested
;
1179 static int shutdown_requested
, shutdown_signal
= -1;
1180 static pid_t shutdown_pid
;
1181 static int powerdown_requested
;
1182 static int debug_requested
;
1183 static int vmstop_requested
;
1185 int qemu_shutdown_requested_get(void)
1187 return shutdown_requested
;
1190 int qemu_reset_requested_get(void)
1192 return reset_requested
;
1195 int qemu_shutdown_requested(void)
1197 int r
= shutdown_requested
;
1198 shutdown_requested
= 0;
1202 void qemu_kill_report(void)
1204 if (shutdown_signal
!= -1) {
1205 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1206 if (shutdown_pid
== 0) {
1207 /* This happens for eg ^C at the terminal, so it's worth
1208 * avoiding printing an odd message in that case.
1210 fputc('\n', stderr
);
1212 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1214 shutdown_signal
= -1;
1218 int qemu_reset_requested(void)
1220 int r
= reset_requested
;
1221 reset_requested
= 0;
1225 int qemu_powerdown_requested(void)
1227 int r
= powerdown_requested
;
1228 powerdown_requested
= 0;
1232 static int qemu_debug_requested(void)
1234 int r
= debug_requested
;
1235 debug_requested
= 0;
1239 static int qemu_vmstop_requested(void)
1241 int r
= vmstop_requested
;
1242 vmstop_requested
= 0;
1246 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1248 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1251 re
->opaque
= opaque
;
1252 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1255 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1259 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1260 if (re
->func
== func
&& re
->opaque
== opaque
) {
1261 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1268 void qemu_system_reset(bool report
)
1270 QEMUResetEntry
*re
, *nre
;
1272 /* reset all devices */
1273 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1274 re
->func(re
->opaque
);
1277 monitor_protocol_event(QEVENT_RESET
, NULL
);
1279 cpu_synchronize_all_post_reset();
1282 void qemu_system_reset_request(void)
1285 shutdown_requested
= 1;
1287 reset_requested
= 1;
1290 qemu_notify_event();
1293 void qemu_system_killed(int signal
, pid_t pid
)
1295 shutdown_signal
= signal
;
1297 qemu_system_shutdown_request();
1300 void qemu_system_shutdown_request(void)
1302 shutdown_requested
= 1;
1303 qemu_notify_event();
1306 void qemu_system_powerdown_request(void)
1308 powerdown_requested
= 1;
1309 qemu_notify_event();
1312 void qemu_system_debug_request(void)
1314 debug_requested
= 1;
1315 qemu_notify_event();
1318 void qemu_system_vmstop_request(int reason
)
1320 vmstop_requested
= reason
;
1321 qemu_notify_event();
1324 void main_loop_wait(int nonblocking
)
1326 fd_set rfds
, wfds
, xfds
;
1334 timeout
= qemu_calculate_timeout();
1335 qemu_bh_update_timeout(&timeout
);
1338 os_host_main_loop_wait(&timeout
);
1340 tv
.tv_sec
= timeout
/ 1000;
1341 tv
.tv_usec
= (timeout
% 1000) * 1000;
1343 /* poll any events */
1344 /* XXX: separate device handlers from system ones */
1349 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1350 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1353 qemu_mutex_unlock_iothread();
1356 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1359 qemu_mutex_lock_iothread();
1362 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1363 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1365 qemu_run_all_timers();
1367 /* Check bottom-halves last in case any of the earlier events triggered
1373 #ifndef CONFIG_IOTHREAD
1374 static int vm_request_pending(void)
1376 return powerdown_requested
||
1378 shutdown_requested
||
1384 qemu_irq qemu_system_powerdown
;
1386 static void main_loop(void)
1388 bool nonblocking
= false;
1389 #ifdef CONFIG_PROFILER
1394 qemu_main_loop_start();
1397 #ifndef CONFIG_IOTHREAD
1398 nonblocking
= cpu_exec_all();
1399 if (vm_request_pending()) {
1403 #ifdef CONFIG_PROFILER
1404 ti
= profile_getclock();
1406 main_loop_wait(nonblocking
);
1407 #ifdef CONFIG_PROFILER
1408 dev_time
+= profile_getclock() - ti
;
1411 if (qemu_debug_requested()) {
1412 vm_stop(VMSTOP_DEBUG
);
1414 if (qemu_shutdown_requested()) {
1416 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1418 vm_stop(VMSTOP_SHUTDOWN
);
1422 if (qemu_reset_requested()) {
1424 cpu_synchronize_all_states();
1425 qemu_system_reset(VMRESET_REPORT
);
1428 if (qemu_powerdown_requested()) {
1429 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1430 qemu_irq_raise(qemu_system_powerdown
);
1432 if ((r
= qemu_vmstop_requested())) {
1440 static void version(void)
1442 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1445 static void help(int exitcode
)
1447 const char *options_help
=
1448 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1450 #define DEFHEADING(text) stringify(text) "\n"
1451 #include "qemu-options.def"
1457 printf("usage: %s [options] [disk_image]\n"
1459 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1462 "During emulation, the following keys are useful:\n"
1463 "ctrl-alt-f toggle full screen\n"
1464 "ctrl-alt-n switch to virtual console 'n'\n"
1465 "ctrl-alt toggle mouse and keyboard grab\n"
1467 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1473 #define HAS_ARG 0x0001
1475 typedef struct QEMUOption
{
1482 static const QEMUOption qemu_options
[] = {
1483 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1484 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1485 { option, opt_arg, opt_enum, arch_mask },
1486 #define DEFHEADING(text)
1487 #include "qemu-options.def"
1493 static void select_vgahw (const char *p
)
1498 vga_interface_type
= VGA_NONE
;
1499 if (strstart(p
, "std", &opts
)) {
1500 vga_interface_type
= VGA_STD
;
1501 } else if (strstart(p
, "cirrus", &opts
)) {
1502 vga_interface_type
= VGA_CIRRUS
;
1503 } else if (strstart(p
, "vmware", &opts
)) {
1504 vga_interface_type
= VGA_VMWARE
;
1505 } else if (strstart(p
, "xenfb", &opts
)) {
1506 vga_interface_type
= VGA_XENFB
;
1507 } else if (strstart(p
, "qxl", &opts
)) {
1508 vga_interface_type
= VGA_QXL
;
1509 } else if (!strstart(p
, "none", &opts
)) {
1511 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1515 const char *nextopt
;
1517 if (strstart(opts
, ",retrace=", &nextopt
)) {
1519 if (strstart(opts
, "dumb", &nextopt
))
1520 vga_retrace_method
= VGA_RETRACE_DUMB
;
1521 else if (strstart(opts
, "precise", &nextopt
))
1522 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1523 else goto invalid_vga
;
1524 } else goto invalid_vga
;
1529 static DisplayType
select_display(const char *p
)
1532 DisplayType display
= DT_DEFAULT
;
1534 if (strstart(p
, "sdl", &opts
)) {
1538 const char *nextopt
;
1540 if (strstart(opts
, ",frame=", &nextopt
)) {
1542 if (strstart(opts
, "on", &nextopt
)) {
1544 } else if (strstart(opts
, "off", &nextopt
)) {
1547 goto invalid_sdl_args
;
1549 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1551 if (strstart(opts
, "on", &nextopt
)) {
1553 } else if (strstart(opts
, "off", &nextopt
)) {
1556 goto invalid_sdl_args
;
1558 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1560 if (strstart(opts
, "on", &nextopt
)) {
1562 } else if (strstart(opts
, "off", &nextopt
)) {
1565 goto invalid_sdl_args
;
1567 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1569 if (strstart(opts
, "on", &nextopt
)) {
1571 } else if (strstart(opts
, "off", &nextopt
)) {
1574 goto invalid_sdl_args
;
1578 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1584 fprintf(stderr
, "SDL support is disabled\n");
1587 } else if (strstart(p
, "vnc", &opts
)) {
1592 const char *nextopt
;
1594 if (strstart(opts
, "=", &nextopt
)) {
1595 vnc_display
= nextopt
;
1599 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1603 fprintf(stderr
, "VNC support is disabled\n");
1606 } else if (strstart(p
, "curses", &opts
)) {
1607 #ifdef CONFIG_CURSES
1608 display
= DT_CURSES
;
1610 fprintf(stderr
, "Curses support is disabled\n");
1613 } else if (strstart(p
, "none", &opts
)) {
1616 fprintf(stderr
, "Unknown display type: %s\n", p
);
1623 static int balloon_parse(const char *arg
)
1627 if (strcmp(arg
, "none") == 0) {
1631 if (!strncmp(arg
, "virtio", 6)) {
1632 if (arg
[6] == ',') {
1633 /* have params -> parse them */
1634 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1638 /* create empty opts */
1639 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1641 qemu_opt_set(opts
, "driver", "virtio-balloon");
1648 char *qemu_find_file(int type
, const char *name
)
1654 /* If name contains path separators then try it as a straight path. */
1655 if ((strchr(name
, '/') || strchr(name
, '\\'))
1656 && access(name
, R_OK
) == 0) {
1657 return g_strdup(name
);
1660 case QEMU_FILE_TYPE_BIOS
:
1663 case QEMU_FILE_TYPE_KEYMAP
:
1664 subdir
= "keymaps/";
1669 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1670 buf
= g_malloc0(len
);
1671 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1672 if (access(buf
, R_OK
)) {
1679 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1681 return qdev_device_help(opts
);
1684 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1688 dev
= qdev_device_add(opts
);
1694 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1696 CharDriverState
*chr
;
1698 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1704 #ifdef CONFIG_VIRTFS
1705 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1708 ret
= qemu_fsdev_add(opts
);
1714 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1716 CharDriverState
*chr
;
1717 const char *chardev
;
1721 mode
= qemu_opt_get(opts
, "mode");
1725 if (strcmp(mode
, "readline") == 0) {
1726 flags
= MONITOR_USE_READLINE
;
1727 } else if (strcmp(mode
, "control") == 0) {
1728 flags
= MONITOR_USE_CONTROL
;
1730 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1734 if (qemu_opt_get_bool(opts
, "pretty", 0))
1735 flags
|= MONITOR_USE_PRETTY
;
1737 if (qemu_opt_get_bool(opts
, "default", 0))
1738 flags
|= MONITOR_IS_DEFAULT
;
1740 chardev
= qemu_opt_get(opts
, "chardev");
1741 chr
= qemu_chr_find(chardev
);
1743 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1747 monitor_init(chr
, flags
);
1751 static void monitor_parse(const char *optarg
, const char *mode
)
1753 static int monitor_device_index
= 0;
1759 if (strstart(optarg
, "chardev:", &p
)) {
1760 snprintf(label
, sizeof(label
), "%s", p
);
1762 snprintf(label
, sizeof(label
), "compat_monitor%d",
1763 monitor_device_index
);
1764 if (monitor_device_index
== 0) {
1767 opts
= qemu_chr_parse_compat(label
, optarg
);
1769 fprintf(stderr
, "parse error: %s\n", optarg
);
1774 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1776 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1779 qemu_opt_set(opts
, "mode", mode
);
1780 qemu_opt_set(opts
, "chardev", label
);
1782 qemu_opt_set(opts
, "default", "on");
1783 monitor_device_index
++;
1786 struct device_config
{
1788 DEV_USB
, /* -usbdevice */
1790 DEV_SERIAL
, /* -serial */
1791 DEV_PARALLEL
, /* -parallel */
1792 DEV_VIRTCON
, /* -virtioconsole */
1793 DEV_DEBUGCON
, /* -debugcon */
1795 const char *cmdline
;
1796 QTAILQ_ENTRY(device_config
) next
;
1798 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1800 static void add_device_config(int type
, const char *cmdline
)
1802 struct device_config
*conf
;
1804 conf
= g_malloc0(sizeof(*conf
));
1806 conf
->cmdline
= cmdline
;
1807 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1810 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1812 struct device_config
*conf
;
1815 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1816 if (conf
->type
!= type
)
1818 rc
= func(conf
->cmdline
);
1825 static int serial_parse(const char *devname
)
1827 static int index
= 0;
1830 if (strcmp(devname
, "none") == 0)
1832 if (index
== MAX_SERIAL_PORTS
) {
1833 fprintf(stderr
, "qemu: too many serial ports\n");
1836 snprintf(label
, sizeof(label
), "serial%d", index
);
1837 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1838 if (!serial_hds
[index
]) {
1839 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1840 devname
, strerror(errno
));
1847 static int parallel_parse(const char *devname
)
1849 static int index
= 0;
1852 if (strcmp(devname
, "none") == 0)
1854 if (index
== MAX_PARALLEL_PORTS
) {
1855 fprintf(stderr
, "qemu: too many parallel ports\n");
1858 snprintf(label
, sizeof(label
), "parallel%d", index
);
1859 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1860 if (!parallel_hds
[index
]) {
1861 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1862 devname
, strerror(errno
));
1869 static int virtcon_parse(const char *devname
)
1871 QemuOptsList
*device
= qemu_find_opts("device");
1872 static int index
= 0;
1874 QemuOpts
*bus_opts
, *dev_opts
;
1876 if (strcmp(devname
, "none") == 0)
1878 if (index
== MAX_VIRTIO_CONSOLES
) {
1879 fprintf(stderr
, "qemu: too many virtio consoles\n");
1883 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1884 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1886 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1887 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1889 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1890 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1891 if (!virtcon_hds
[index
]) {
1892 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1893 devname
, strerror(errno
));
1896 qemu_opt_set(dev_opts
, "chardev", label
);
1902 static int debugcon_parse(const char *devname
)
1906 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
1909 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1911 fprintf(stderr
, "qemu: already have a debugcon device\n");
1914 qemu_opt_set(opts
, "driver", "isa-debugcon");
1915 qemu_opt_set(opts
, "chardev", "debugcon");
1919 static QEMUMachine
*machine_parse(const char *name
)
1921 QEMUMachine
*m
, *machine
= NULL
;
1924 machine
= find_machine(name
);
1929 printf("Supported machines are:\n");
1930 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1932 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
1934 printf("%-10s %s%s\n", m
->name
, m
->desc
,
1935 m
->is_default
? " (default)" : "");
1937 exit(!name
|| *name
!= '?');
1940 static int tcg_init(void)
1942 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
1947 const char *opt_name
;
1949 int (*available
)(void);
1953 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
1954 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
1955 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
1958 static int configure_accelerator(void)
1960 const char *p
= NULL
;
1963 bool accel_initalised
= 0;
1964 bool init_failed
= 0;
1966 QemuOptsList
*list
= qemu_find_opts("machine");
1967 if (!QTAILQ_EMPTY(&list
->head
)) {
1968 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
1972 /* Use the default "accelerator", tcg */
1976 while (!accel_initalised
&& *p
!= '\0') {
1980 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
1981 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
1982 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
1983 *(accel_list
[i
].allowed
) = 1;
1984 ret
= accel_list
[i
].init();
1987 if (!accel_list
[i
].available()) {
1988 printf("%s not supported for this target\n",
1989 accel_list
[i
].name
);
1991 fprintf(stderr
, "failed to initialize %s: %s\n",
1995 *(accel_list
[i
].allowed
) = 0;
1997 accel_initalised
= 1;
2002 if (i
== ARRAY_SIZE(accel_list
)) {
2003 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2007 if (!accel_initalised
) {
2008 fprintf(stderr
, "No accelerator found!\n");
2013 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2016 return !accel_initalised
;
2019 void qemu_add_exit_notifier(Notifier
*notify
)
2021 notifier_list_add(&exit_notifiers
, notify
);
2024 void qemu_remove_exit_notifier(Notifier
*notify
)
2026 notifier_list_remove(&exit_notifiers
, notify
);
2029 static void qemu_run_exit_notifiers(void)
2031 notifier_list_notify(&exit_notifiers
, NULL
);
2034 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2036 notifier_list_add(&machine_init_done_notifiers
, notify
);
2039 static void qemu_run_machine_init_done_notifiers(void)
2041 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2044 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2045 const char **poptarg
, int *poptind
)
2047 const QEMUOption
*popt
;
2048 int optind
= *poptind
;
2049 char *r
= argv
[optind
];
2052 loc_set_cmdline(argv
, optind
, 1);
2054 /* Treat --foo the same as -foo. */
2057 popt
= qemu_options
;
2060 error_report("invalid option");
2063 if (!strcmp(popt
->name
, r
+ 1))
2067 if (popt
->flags
& HAS_ARG
) {
2068 if (optind
>= argc
) {
2069 error_report("requires an argument");
2072 optarg
= argv
[optind
++];
2073 loc_set_cmdline(argv
, optind
- 2, 2);
2084 static gpointer
malloc_and_trace(gsize n_bytes
)
2086 void *ptr
= malloc(n_bytes
);
2087 trace_qemu_malloc(n_bytes
, ptr
);
2091 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2093 void *ptr
= realloc(mem
, n_bytes
);
2094 trace_qemu_realloc(mem
, n_bytes
, ptr
);
2098 static void free_and_trace(gpointer mem
)
2100 trace_qemu_free(mem
);
2104 int main(int argc
, char **argv
, char **envp
)
2106 const char *gdbstub_dev
= NULL
;
2108 int snapshot
, linux_boot
;
2109 const char *icount_option
= NULL
;
2110 const char *initrd_filename
;
2111 const char *kernel_filename
, *kernel_cmdline
;
2112 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2114 DisplayChangeListener
*dcl
;
2115 int cyls
, heads
, secs
, translation
;
2116 QemuOpts
*hda_opts
= NULL
, *opts
;
2117 QemuOptsList
*olist
;
2120 const char *loadvm
= NULL
;
2121 QEMUMachine
*machine
;
2122 const char *cpu_model
;
2123 const char *pid_file
= NULL
;
2124 const char *incoming
= NULL
;
2126 int show_vnc_port
= 0;
2129 const char *trace_file
= NULL
;
2130 const char *log_mask
= NULL
;
2131 const char *log_file
= NULL
;
2132 GMemVTable mem_trace
= {
2133 .malloc
= malloc_and_trace
,
2134 .realloc
= realloc_and_trace
,
2135 .free
= free_and_trace
,
2138 atexit(qemu_run_exit_notifiers
);
2139 error_set_progname(argv
[0]);
2141 g_mem_set_vtable(&mem_trace
);
2145 qemu_cache_utils_init(envp
);
2147 QLIST_INIT (&vm_change_state_head
);
2148 os_setup_early_signal_handling();
2150 module_call_init(MODULE_INIT_MACHINE
);
2151 machine
= find_default_machine();
2153 initrd_filename
= NULL
;
2156 kernel_filename
= NULL
;
2157 kernel_cmdline
= "";
2158 cyls
= heads
= secs
= 0;
2159 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2161 for (i
= 0; i
< MAX_NODES
; i
++) {
2163 node_cpumask
[i
] = 0;
2171 /* first pass of option parsing */
2173 while (optind
< argc
) {
2174 if (argv
[optind
][0] != '-') {
2179 const QEMUOption
*popt
;
2181 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2182 switch (popt
->index
) {
2183 case QEMU_OPTION_nodefconfig
:
2193 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2194 if (ret
< 0 && ret
!= -ENOENT
) {
2198 ret
= qemu_read_config_file(arch_config_name
);
2199 if (ret
< 0 && ret
!= -ENOENT
) {
2205 /* second pass of option parsing */
2210 if (argv
[optind
][0] != '-') {
2211 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2213 const QEMUOption
*popt
;
2215 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2216 if (!(popt
->arch_mask
& arch_type
)) {
2217 printf("Option %s not supported for this target\n", popt
->name
);
2220 switch(popt
->index
) {
2222 machine
= machine_parse(optarg
);
2224 case QEMU_OPTION_cpu
:
2225 /* hw initialization will check this */
2226 if (*optarg
== '?') {
2227 list_cpus(stdout
, &fprintf
, optarg
);
2233 case QEMU_OPTION_initrd
:
2234 initrd_filename
= optarg
;
2236 case QEMU_OPTION_hda
:
2240 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2242 snprintf(buf
, sizeof(buf
),
2243 "%s,cyls=%d,heads=%d,secs=%d%s",
2244 HD_OPTS
, cyls
, heads
, secs
,
2245 translation
== BIOS_ATA_TRANSLATION_LBA
?
2247 translation
== BIOS_ATA_TRANSLATION_NONE
?
2248 ",trans=none" : "");
2249 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2252 case QEMU_OPTION_hdb
:
2253 case QEMU_OPTION_hdc
:
2254 case QEMU_OPTION_hdd
:
2255 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2258 case QEMU_OPTION_drive
:
2259 if (drive_def(optarg
) == NULL
) {
2263 case QEMU_OPTION_set
:
2264 if (qemu_set_option(optarg
) != 0)
2267 case QEMU_OPTION_global
:
2268 if (qemu_global_option(optarg
) != 0)
2271 case QEMU_OPTION_mtdblock
:
2272 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2274 case QEMU_OPTION_sd
:
2275 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2277 case QEMU_OPTION_pflash
:
2278 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2280 case QEMU_OPTION_snapshot
:
2283 case QEMU_OPTION_hdachs
:
2287 cyls
= strtol(p
, (char **)&p
, 0);
2288 if (cyls
< 1 || cyls
> 16383)
2293 heads
= strtol(p
, (char **)&p
, 0);
2294 if (heads
< 1 || heads
> 16)
2299 secs
= strtol(p
, (char **)&p
, 0);
2300 if (secs
< 1 || secs
> 63)
2304 if (!strcmp(p
, "none"))
2305 translation
= BIOS_ATA_TRANSLATION_NONE
;
2306 else if (!strcmp(p
, "lba"))
2307 translation
= BIOS_ATA_TRANSLATION_LBA
;
2308 else if (!strcmp(p
, "auto"))
2309 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2312 } else if (*p
!= '\0') {
2314 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2317 if (hda_opts
!= NULL
) {
2319 snprintf(num
, sizeof(num
), "%d", cyls
);
2320 qemu_opt_set(hda_opts
, "cyls", num
);
2321 snprintf(num
, sizeof(num
), "%d", heads
);
2322 qemu_opt_set(hda_opts
, "heads", num
);
2323 snprintf(num
, sizeof(num
), "%d", secs
);
2324 qemu_opt_set(hda_opts
, "secs", num
);
2325 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2326 qemu_opt_set(hda_opts
, "trans", "lba");
2327 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2328 qemu_opt_set(hda_opts
, "trans", "none");
2332 case QEMU_OPTION_numa
:
2333 if (nb_numa_nodes
>= MAX_NODES
) {
2334 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2339 case QEMU_OPTION_display
:
2340 display_type
= select_display(optarg
);
2342 case QEMU_OPTION_nographic
:
2343 display_type
= DT_NOGRAPHIC
;
2345 case QEMU_OPTION_curses
:
2346 #ifdef CONFIG_CURSES
2347 display_type
= DT_CURSES
;
2349 fprintf(stderr
, "Curses support is disabled\n");
2353 case QEMU_OPTION_portrait
:
2354 graphic_rotate
= 90;
2356 case QEMU_OPTION_rotate
:
2357 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2358 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2359 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2361 "qemu: only 90, 180, 270 deg rotation is available\n");
2365 case QEMU_OPTION_kernel
:
2366 kernel_filename
= optarg
;
2368 case QEMU_OPTION_append
:
2369 kernel_cmdline
= optarg
;
2371 case QEMU_OPTION_cdrom
:
2372 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2374 case QEMU_OPTION_boot
:
2376 static const char * const params
[] = {
2377 "order", "once", "menu",
2378 "splash", "splash-time", NULL
2380 char buf
[sizeof(boot_devices
)];
2381 char *standard_boot_devices
;
2384 if (!strchr(optarg
, '=')) {
2386 pstrcpy(buf
, sizeof(buf
), optarg
);
2387 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2389 "qemu: unknown boot parameter '%s' in '%s'\n",
2395 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2396 validate_bootdevices(buf
);
2397 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2400 if (get_param_value(buf
, sizeof(buf
),
2402 validate_bootdevices(buf
);
2403 standard_boot_devices
= g_strdup(boot_devices
);
2404 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2405 qemu_register_reset(restore_boot_devices
,
2406 standard_boot_devices
);
2408 if (get_param_value(buf
, sizeof(buf
),
2410 if (!strcmp(buf
, "on")) {
2412 } else if (!strcmp(buf
, "off")) {
2416 "qemu: invalid option value '%s'\n",
2421 qemu_opts_parse(qemu_find_opts("boot-opts"),
2426 case QEMU_OPTION_fda
:
2427 case QEMU_OPTION_fdb
:
2428 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2431 case QEMU_OPTION_no_fd_bootchk
:
2434 case QEMU_OPTION_netdev
:
2435 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2439 case QEMU_OPTION_net
:
2440 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2445 case QEMU_OPTION_tftp
:
2446 legacy_tftp_prefix
= optarg
;
2448 case QEMU_OPTION_bootp
:
2449 legacy_bootp_filename
= optarg
;
2451 case QEMU_OPTION_redir
:
2452 if (net_slirp_redir(optarg
) < 0)
2456 case QEMU_OPTION_bt
:
2457 add_device_config(DEV_BT
, optarg
);
2459 case QEMU_OPTION_audio_help
:
2460 if (!(audio_available())) {
2461 printf("Option %s not supported for this target\n", popt
->name
);
2467 case QEMU_OPTION_soundhw
:
2468 if (!(audio_available())) {
2469 printf("Option %s not supported for this target\n", popt
->name
);
2472 select_soundhw (optarg
);
2477 case QEMU_OPTION_version
:
2481 case QEMU_OPTION_m
: {
2484 value
= strtosz(optarg
, NULL
);
2486 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2490 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2491 fprintf(stderr
, "qemu: ram size too large\n");
2497 case QEMU_OPTION_mempath
:
2501 case QEMU_OPTION_mem_prealloc
:
2512 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2514 case QEMU_OPTION_gdb
:
2515 gdbstub_dev
= optarg
;
2520 case QEMU_OPTION_bios
:
2523 case QEMU_OPTION_singlestep
:
2530 keyboard_layout
= optarg
;
2532 case QEMU_OPTION_localtime
:
2535 case QEMU_OPTION_vga
:
2536 select_vgahw (optarg
);
2543 w
= strtol(p
, (char **)&p
, 10);
2546 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2552 h
= strtol(p
, (char **)&p
, 10);
2557 depth
= strtol(p
, (char **)&p
, 10);
2558 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2559 depth
!= 24 && depth
!= 32)
2561 } else if (*p
== '\0') {
2562 depth
= graphic_depth
;
2569 graphic_depth
= depth
;
2572 case QEMU_OPTION_echr
:
2575 term_escape_char
= strtol(optarg
, &r
, 0);
2577 printf("Bad argument to echr\n");
2580 case QEMU_OPTION_monitor
:
2581 monitor_parse(optarg
, "readline");
2582 default_monitor
= 0;
2584 case QEMU_OPTION_qmp
:
2585 monitor_parse(optarg
, "control");
2586 default_monitor
= 0;
2588 case QEMU_OPTION_mon
:
2589 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2593 default_monitor
= 0;
2595 case QEMU_OPTION_chardev
:
2596 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2601 case QEMU_OPTION_fsdev
:
2602 olist
= qemu_find_opts("fsdev");
2604 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2607 opts
= qemu_opts_parse(olist
, optarg
, 1);
2609 fprintf(stderr
, "parse error: %s\n", optarg
);
2613 case QEMU_OPTION_virtfs
: {
2617 olist
= qemu_find_opts("virtfs");
2619 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2622 opts
= qemu_opts_parse(olist
, optarg
, 1);
2624 fprintf(stderr
, "parse error: %s\n", optarg
);
2628 if (qemu_opt_get(opts
, "fstype") == NULL
||
2629 qemu_opt_get(opts
, "mount_tag") == NULL
||
2630 qemu_opt_get(opts
, "path") == NULL
||
2631 qemu_opt_get(opts
, "security_model") == NULL
) {
2632 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2633 "security_model=[mapped|passthrough|none],"
2634 "mount_tag=tag.\n");
2638 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2639 qemu_opt_get(opts
, "mount_tag"), 1);
2641 fprintf(stderr
, "duplicate fsdev id: %s\n",
2642 qemu_opt_get(opts
, "mount_tag"));
2645 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2646 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2647 qemu_opt_set(fsdev
, "security_model",
2648 qemu_opt_get(opts
, "security_model"));
2650 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2651 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2652 qemu_opt_set(device
, "fsdev",
2653 qemu_opt_get(opts
, "mount_tag"));
2654 qemu_opt_set(device
, "mount_tag",
2655 qemu_opt_get(opts
, "mount_tag"));
2658 case QEMU_OPTION_serial
:
2659 add_device_config(DEV_SERIAL
, optarg
);
2661 if (strncmp(optarg
, "mon:", 4) == 0) {
2662 default_monitor
= 0;
2665 case QEMU_OPTION_watchdog
:
2668 "qemu: only one watchdog option may be given\n");
2673 case QEMU_OPTION_watchdog_action
:
2674 if (select_watchdog_action(optarg
) == -1) {
2675 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2679 case QEMU_OPTION_virtiocon
:
2680 add_device_config(DEV_VIRTCON
, optarg
);
2681 default_virtcon
= 0;
2682 if (strncmp(optarg
, "mon:", 4) == 0) {
2683 default_monitor
= 0;
2686 case QEMU_OPTION_parallel
:
2687 add_device_config(DEV_PARALLEL
, optarg
);
2688 default_parallel
= 0;
2689 if (strncmp(optarg
, "mon:", 4) == 0) {
2690 default_monitor
= 0;
2693 case QEMU_OPTION_debugcon
:
2694 add_device_config(DEV_DEBUGCON
, optarg
);
2696 case QEMU_OPTION_loadvm
:
2699 case QEMU_OPTION_full_screen
:
2703 case QEMU_OPTION_no_frame
:
2706 case QEMU_OPTION_alt_grab
:
2709 case QEMU_OPTION_ctrl_grab
:
2712 case QEMU_OPTION_no_quit
:
2715 case QEMU_OPTION_sdl
:
2716 display_type
= DT_SDL
;
2719 case QEMU_OPTION_no_frame
:
2720 case QEMU_OPTION_alt_grab
:
2721 case QEMU_OPTION_ctrl_grab
:
2722 case QEMU_OPTION_no_quit
:
2723 case QEMU_OPTION_sdl
:
2724 fprintf(stderr
, "SDL support is disabled\n");
2727 case QEMU_OPTION_pidfile
:
2730 case QEMU_OPTION_win2k_hack
:
2731 win2k_install_hack
= 1;
2733 case QEMU_OPTION_rtc_td_hack
:
2736 case QEMU_OPTION_acpitable
:
2737 do_acpitable_option(optarg
);
2739 case QEMU_OPTION_smbios
:
2740 do_smbios_option(optarg
);
2742 case QEMU_OPTION_enable_kvm
:
2743 olist
= qemu_find_opts("machine");
2744 qemu_opts_reset(olist
);
2745 qemu_opts_parse(olist
, "accel=kvm", 0);
2747 case QEMU_OPTION_machine
:
2748 olist
= qemu_find_opts("machine");
2749 qemu_opts_reset(olist
);
2750 opts
= qemu_opts_parse(olist
, optarg
, 1);
2752 fprintf(stderr
, "parse error: %s\n", optarg
);
2755 optarg
= qemu_opt_get(opts
, "type");
2757 machine
= machine_parse(optarg
);
2760 case QEMU_OPTION_usb
:
2763 case QEMU_OPTION_usbdevice
:
2765 add_device_config(DEV_USB
, optarg
);
2767 case QEMU_OPTION_device
:
2768 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2772 case QEMU_OPTION_smp
:
2775 fprintf(stderr
, "Invalid number of CPUs\n");
2778 if (max_cpus
< smp_cpus
) {
2779 fprintf(stderr
, "maxcpus must be equal to or greater than "
2783 if (max_cpus
> 255) {
2784 fprintf(stderr
, "Unsupported number of maxcpus\n");
2788 case QEMU_OPTION_vnc
:
2791 vnc_display
= optarg
;
2793 fprintf(stderr
, "VNC support is disabled\n");
2797 case QEMU_OPTION_no_acpi
:
2800 case QEMU_OPTION_no_hpet
:
2803 case QEMU_OPTION_balloon
:
2804 if (balloon_parse(optarg
) < 0) {
2805 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2809 case QEMU_OPTION_no_reboot
:
2812 case QEMU_OPTION_no_shutdown
:
2815 case QEMU_OPTION_show_cursor
:
2818 case QEMU_OPTION_uuid
:
2819 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2820 fprintf(stderr
, "Fail to parse UUID string."
2821 " Wrong format.\n");
2825 case QEMU_OPTION_option_rom
:
2826 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2827 fprintf(stderr
, "Too many option ROMs\n");
2830 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2831 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2832 option_rom
[nb_option_roms
].bootindex
=
2833 qemu_opt_get_number(opts
, "bootindex", -1);
2834 if (!option_rom
[nb_option_roms
].name
) {
2835 fprintf(stderr
, "Option ROM file is not specified\n");
2840 case QEMU_OPTION_semihosting
:
2841 semihosting_enabled
= 1;
2843 case QEMU_OPTION_name
:
2844 qemu_name
= g_strdup(optarg
);
2846 char *p
= strchr(qemu_name
, ',');
2849 if (strncmp(p
, "process=", 8)) {
2850 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2854 os_set_proc_name(p
);
2858 case QEMU_OPTION_prom_env
:
2859 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2860 fprintf(stderr
, "Too many prom variables\n");
2863 prom_envs
[nb_prom_envs
] = optarg
;
2866 case QEMU_OPTION_old_param
:
2869 case QEMU_OPTION_clock
:
2870 configure_alarms(optarg
);
2872 case QEMU_OPTION_startdate
:
2873 configure_rtc_date_offset(optarg
, 1);
2875 case QEMU_OPTION_rtc
:
2876 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2880 configure_rtc(opts
);
2882 case QEMU_OPTION_tb_size
:
2883 tcg_tb_size
= strtol(optarg
, NULL
, 0);
2884 if (tcg_tb_size
< 0) {
2888 case QEMU_OPTION_icount
:
2889 icount_option
= optarg
;
2891 case QEMU_OPTION_incoming
:
2893 incoming_expected
= true;
2895 case QEMU_OPTION_nodefaults
:
2897 default_parallel
= 0;
2898 default_virtcon
= 0;
2899 default_monitor
= 0;
2906 case QEMU_OPTION_xen_domid
:
2907 if (!(xen_available())) {
2908 printf("Option %s not supported for this target\n", popt
->name
);
2911 xen_domid
= atoi(optarg
);
2913 case QEMU_OPTION_xen_create
:
2914 if (!(xen_available())) {
2915 printf("Option %s not supported for this target\n", popt
->name
);
2918 xen_mode
= XEN_CREATE
;
2920 case QEMU_OPTION_xen_attach
:
2921 if (!(xen_available())) {
2922 printf("Option %s not supported for this target\n", popt
->name
);
2925 xen_mode
= XEN_ATTACH
;
2927 #ifdef CONFIG_SIMPLE_TRACE
2928 case QEMU_OPTION_trace
:
2929 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2931 trace_file
= qemu_opt_get(opts
, "file");
2935 case QEMU_OPTION_readconfig
:
2937 int ret
= qemu_read_config_file(optarg
);
2939 fprintf(stderr
, "read config %s: %s\n", optarg
,
2945 case QEMU_OPTION_spice
:
2946 olist
= qemu_find_opts("spice");
2948 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2951 opts
= qemu_opts_parse(olist
, optarg
, 0);
2953 fprintf(stderr
, "parse error: %s\n", optarg
);
2957 case QEMU_OPTION_writeconfig
:
2960 if (strcmp(optarg
, "-") == 0) {
2963 fp
= fopen(optarg
, "w");
2965 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2969 qemu_config_write(fp
);
2974 os_parse_cmd_args(popt
->index
, optarg
);
2980 /* Open the logfile at this point, if necessary. We can't open the logfile
2981 * when encountering either of the logging options (-d or -D) because the
2982 * other one may be encountered later on the command line, changing the
2983 * location or level of logging.
2987 set_cpu_log_filename(log_file
);
2989 set_cpu_log(log_mask
);
2992 if (!st_init(trace_file
)) {
2993 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2996 /* If no data_dir is specified then try to find it relative to the
2999 data_dir
= os_find_datadir(argv
[0]);
3001 /* If all else fails use the install patch specified when building. */
3003 data_dir
= CONFIG_QEMU_DATADIR
;
3007 * Default to max_cpus = smp_cpus, in case the user doesn't
3008 * specify a max_cpus value.
3011 max_cpus
= smp_cpus
;
3013 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3014 if (smp_cpus
> machine
->max_cpus
) {
3015 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3016 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3022 * Get the default machine options from the machine if it is not already
3023 * specified either by the configuration file or by the command line.
3025 if (machine
->default_machine_opts
) {
3026 QemuOptsList
*list
= qemu_find_opts("machine");
3027 const char *p
= NULL
;
3029 if (!QTAILQ_EMPTY(&list
->head
)) {
3030 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3033 qemu_opts_reset(list
);
3034 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3036 fprintf(stderr
, "parse error for machine %s: %s\n",
3037 machine
->name
, machine
->default_machine_opts
);
3043 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3044 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3046 if (machine
->no_serial
) {
3049 if (machine
->no_parallel
) {
3050 default_parallel
= 0;
3052 if (!machine
->use_virtcon
) {
3053 default_virtcon
= 0;
3055 if (machine
->no_vga
) {
3058 if (machine
->no_floppy
) {
3061 if (machine
->no_cdrom
) {
3064 if (machine
->no_sdcard
) {
3068 if (display_type
== DT_NOGRAPHIC
) {
3069 if (default_parallel
)
3070 add_device_config(DEV_PARALLEL
, "null");
3071 if (default_serial
&& default_monitor
) {
3072 add_device_config(DEV_SERIAL
, "mon:stdio");
3073 } else if (default_virtcon
&& default_monitor
) {
3074 add_device_config(DEV_VIRTCON
, "mon:stdio");
3077 add_device_config(DEV_SERIAL
, "stdio");
3078 if (default_virtcon
)
3079 add_device_config(DEV_VIRTCON
, "stdio");
3080 if (default_monitor
)
3081 monitor_parse("stdio", "readline");
3085 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3086 if (default_parallel
)
3087 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3088 if (default_monitor
)
3089 monitor_parse("vc:80Cx24C", "readline");
3090 if (default_virtcon
)
3091 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3094 vga_interface_type
= VGA_CIRRUS
;
3098 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3100 #ifdef CONFIG_VIRTFS
3101 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3108 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3113 /* init the memory */
3114 if (ram_size
== 0) {
3115 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3118 configure_accelerator();
3120 if (qemu_init_main_loop()) {
3121 fprintf(stderr
, "qemu_init_main_loop failed\n");
3124 linux_boot
= (kernel_filename
!= NULL
);
3126 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3127 fprintf(stderr
, "-append only allowed with -kernel option\n");
3131 if (!linux_boot
&& initrd_filename
!= NULL
) {
3132 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3136 os_set_line_buffering();
3138 if (init_timer_alarm() < 0) {
3139 fprintf(stderr
, "could not initialize alarm timer\n");
3142 configure_icount(icount_option
);
3144 if (net_init_clients() < 0) {
3148 /* init the bluetooth world */
3149 if (foreach_device_config(DEV_BT
, bt_parse
))
3152 if (!xen_enabled()) {
3153 /* On 32-bit hosts, QEMU is limited by virtual address space */
3154 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3155 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3160 cpu_exec_init_all();
3162 bdrv_init_with_whitelist();
3166 /* open the virtual block devices */
3168 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3169 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3172 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3173 IF_DEFAULT
, 2, CDROM_OPTS
);
3174 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3175 IF_FLOPPY
, 0, FD_OPTS
);
3176 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3179 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3182 if (nb_numa_nodes
> 0) {
3185 if (nb_numa_nodes
> MAX_NODES
) {
3186 nb_numa_nodes
= MAX_NODES
;
3189 /* If no memory size if given for any node, assume the default case
3190 * and distribute the available memory equally across all nodes
3192 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3193 if (node_mem
[i
] != 0)
3196 if (i
== nb_numa_nodes
) {
3197 uint64_t usedmem
= 0;
3199 /* On Linux, the each node's border has to be 8MB aligned,
3200 * the final node gets the rest.
3202 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3203 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3204 usedmem
+= node_mem
[i
];
3206 node_mem
[i
] = ram_size
- usedmem
;
3209 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3210 if (node_cpumask
[i
] != 0)
3213 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3214 * must cope with this anyway, because there are BIOSes out there in
3215 * real machines which also use this scheme.
3217 if (i
== nb_numa_nodes
) {
3218 for (i
= 0; i
< smp_cpus
; i
++) {
3219 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3224 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3228 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3230 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3232 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3234 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3237 module_call_init(MODULE_INIT_DEVICE
);
3239 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3243 i
= select_watchdog(watchdog
);
3245 exit (i
== 1 ? 1 : 0);
3248 if (machine
->compat_props
) {
3249 qdev_prop_register_global_list(machine
->compat_props
);
3253 machine
->init(ram_size
, boot_devices
,
3254 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3256 cpu_synchronize_all_post_init();
3260 current_machine
= machine
;
3262 /* init USB devices */
3264 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3268 /* init generic devices */
3269 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3272 net_check_clients();
3274 /* just use the first displaystate for the moment */
3275 ds
= get_displaystate();
3279 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3280 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3281 display_type
= DT_SDL
;
3282 #elif defined(CONFIG_VNC)
3283 vnc_display
= "localhost:0,to=99";
3286 display_type
= DT_NONE
;
3291 /* init local displays */
3292 switch (display_type
) {
3295 #if defined(CONFIG_CURSES)
3297 curses_display_init(ds
, full_screen
);
3300 #if defined(CONFIG_SDL)
3302 sdl_display_init(ds
, full_screen
, no_frame
);
3304 #elif defined(CONFIG_COCOA)
3306 cocoa_display_init(ds
, full_screen
);
3313 /* must be after terminal init, SDL library changes signal handlers */
3314 os_setup_signal_handling();
3317 /* init remote displays */
3319 vnc_display_init(ds
);
3320 if (vnc_display_open(ds
, vnc_display
) < 0)
3323 if (show_vnc_port
) {
3324 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3329 if (using_spice
&& !qxl_enabled
) {
3330 qemu_spice_display_init(ds
);
3336 dcl
= ds
->listeners
;
3337 while (dcl
!= NULL
) {
3338 if (dcl
->dpy_refresh
!= NULL
) {
3339 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3340 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3345 if (ds
->gui_timer
== NULL
) {
3346 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3347 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3349 text_consoles_set_display(ds
);
3351 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3352 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3357 qdev_machine_creation_done();
3359 if (rom_load_all() != 0) {
3360 fprintf(stderr
, "rom loading failed\n");
3364 /* TODO: once all bus devices are qdevified, this should be done
3365 * when bus is created by qdev.c */
3366 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3367 qemu_run_machine_init_done_notifiers();
3369 qemu_system_reset(VMRESET_SILENT
);
3371 if (load_vmstate(loadvm
) < 0) {
3377 int ret
= qemu_start_incoming_migration(incoming
);
3379 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3383 } else if (autostart
) {