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
];
232 typedef struct FWBootEntry FWBootEntry
;
235 QTAILQ_ENTRY(FWBootEntry
) link
;
241 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
244 uint64_t node_mem
[MAX_NODES
];
245 uint64_t node_cpumask
[MAX_NODES
];
247 static QEMUTimer
*nographic_timer
;
249 uint8_t qemu_uuid
[16];
251 static QEMUBootSetHandler
*boot_set_handler
;
252 static void *boot_set_opaque
;
254 static NotifierList exit_notifiers
=
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
257 static NotifierList machine_init_done_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 enum xen_mode xen_mode
= XEN_EMULATE
;
264 static int default_serial
= 1;
265 static int default_parallel
= 1;
266 static int default_virtcon
= 1;
267 static int default_monitor
= 1;
268 static int default_vga
= 1;
269 static int default_floppy
= 1;
270 static int default_cdrom
= 1;
271 static int default_sdcard
= 1;
277 { .driver
= "isa-serial", .flag
= &default_serial
},
278 { .driver
= "isa-parallel", .flag
= &default_parallel
},
279 { .driver
= "isa-fdc", .flag
= &default_floppy
},
280 { .driver
= "ide-drive", .flag
= &default_cdrom
},
281 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
282 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
283 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
284 { .driver
= "VGA", .flag
= &default_vga
},
285 { .driver
= "cirrus-vga", .flag
= &default_vga
},
286 { .driver
= "vmware-svga", .flag
= &default_vga
},
289 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
291 const char *driver
= qemu_opt_get(opts
, "driver");
296 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
297 if (strcmp(default_list
[i
].driver
, driver
) != 0)
299 *(default_list
[i
].flag
) = 0;
304 /***********************************************************/
305 /* real time host monotonic timer */
307 /***********************************************************/
308 /* host time/date access */
309 void qemu_get_timedate(struct tm
*tm
, int offset
)
316 if (rtc_date_offset
== -1) {
320 ret
= localtime(&ti
);
322 ti
-= rtc_date_offset
;
326 memcpy(tm
, ret
, sizeof(struct tm
));
329 int qemu_timedate_diff(struct tm
*tm
)
333 if (rtc_date_offset
== -1)
335 seconds
= mktimegm(tm
);
337 seconds
= mktime(tm
);
339 seconds
= mktimegm(tm
) + rtc_date_offset
;
341 return seconds
- time(NULL
);
344 void rtc_change_mon_event(struct tm
*tm
)
348 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
349 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
350 qobject_decref(data
);
353 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
355 time_t rtc_start_date
;
358 if (!strcmp(startdate
, "now") && legacy
) {
359 rtc_date_offset
= -1;
361 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
369 } else if (sscanf(startdate
, "%d-%d-%d",
381 rtc_start_date
= mktimegm(&tm
);
382 if (rtc_start_date
== -1) {
384 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
385 "'2006-06-17T16:01:21' or '2006-06-17'\n");
388 rtc_date_offset
= time(NULL
) - rtc_start_date
;
392 static void configure_rtc(QemuOpts
*opts
)
396 value
= qemu_opt_get(opts
, "base");
398 if (!strcmp(value
, "utc")) {
400 } else if (!strcmp(value
, "localtime")) {
403 configure_rtc_date_offset(value
, 0);
406 value
= qemu_opt_get(opts
, "clock");
408 if (!strcmp(value
, "host")) {
409 rtc_clock
= host_clock
;
410 } else if (!strcmp(value
, "vm")) {
411 rtc_clock
= vm_clock
;
413 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
417 value
= qemu_opt_get(opts
, "driftfix");
419 if (!strcmp(value
, "slew")) {
421 } else if (!strcmp(value
, "none")) {
424 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
430 /***********************************************************/
431 /* Bluetooth support */
434 static struct HCIInfo
*hci_table
[MAX_NICS
];
436 static struct bt_vlan_s
{
437 struct bt_scatternet_s net
;
439 struct bt_vlan_s
*next
;
442 /* find or alloc a new bluetooth "VLAN" */
443 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
445 struct bt_vlan_s
**pvlan
, *vlan
;
446 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
450 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
452 pvlan
= &first_bt_vlan
;
453 while (*pvlan
!= NULL
)
454 pvlan
= &(*pvlan
)->next
;
459 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
463 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
468 static struct HCIInfo null_hci
= {
469 .cmd_send
= null_hci_send
,
470 .sco_send
= null_hci_send
,
471 .acl_send
= null_hci_send
,
472 .bdaddr_set
= null_hci_addr_set
,
475 struct HCIInfo
*qemu_next_hci(void)
477 if (cur_hci
== nb_hcis
)
480 return hci_table
[cur_hci
++];
483 static struct HCIInfo
*hci_init(const char *str
)
486 struct bt_scatternet_s
*vlan
= 0;
488 if (!strcmp(str
, "null"))
491 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
493 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
494 else if (!strncmp(str
, "hci", 3)) {
497 if (!strncmp(str
+ 3, ",vlan=", 6)) {
498 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
503 vlan
= qemu_find_bt_vlan(0);
505 return bt_new_hci(vlan
);
508 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
513 static int bt_hci_parse(const char *str
)
518 if (nb_hcis
>= MAX_NICS
) {
519 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
532 bdaddr
.b
[5] = 0x56 + nb_hcis
;
533 hci
->bdaddr_set(hci
, bdaddr
.b
);
535 hci_table
[nb_hcis
++] = hci
;
540 static void bt_vhci_add(int vlan_id
)
542 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
545 fprintf(stderr
, "qemu: warning: adding a VHCI to "
546 "an empty scatternet %i\n", vlan_id
);
548 bt_vhci_init(bt_new_hci(vlan
));
551 static struct bt_device_s
*bt_device_add(const char *opt
)
553 struct bt_scatternet_s
*vlan
;
555 char *endp
= strstr(opt
, ",vlan=");
556 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
559 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
562 vlan_id
= strtol(endp
+ 6, &endp
, 0);
564 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
569 vlan
= qemu_find_bt_vlan(vlan_id
);
572 fprintf(stderr
, "qemu: warning: adding a slave device to "
573 "an empty scatternet %i\n", vlan_id
);
575 if (!strcmp(devname
, "keyboard"))
576 return bt_keyboard_init(vlan
);
578 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
582 static int bt_parse(const char *opt
)
584 const char *endp
, *p
;
587 if (strstart(opt
, "hci", &endp
)) {
588 if (!*endp
|| *endp
== ',') {
590 if (!strstart(endp
, ",vlan=", 0))
593 return bt_hci_parse(opt
);
595 } else if (strstart(opt
, "vhci", &endp
)) {
596 if (!*endp
|| *endp
== ',') {
598 if (strstart(endp
, ",vlan=", &p
)) {
599 vlan
= strtol(p
, (char **) &endp
, 0);
601 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
605 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
614 } else if (strstart(opt
, "device:", &endp
))
615 return !bt_device_add(endp
);
617 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
621 /***********************************************************/
622 /* QEMU Block devices */
624 #define HD_OPTS "media=disk"
625 #define CDROM_OPTS "media=cdrom"
627 #define PFLASH_OPTS ""
631 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
633 int *use_scsi
= opaque
;
635 return drive_init(opts
, *use_scsi
) == NULL
;
638 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
640 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
641 qemu_opt_set(opts
, "snapshot", "on");
646 static void default_drive(int enable
, int snapshot
, int use_scsi
,
647 BlockInterfaceType type
, int index
,
652 if (type
== IF_DEFAULT
) {
653 type
= use_scsi
? IF_SCSI
: IF_IDE
;
656 if (!enable
|| drive_get_by_index(type
, index
)) {
660 opts
= drive_add(type
, index
, NULL
, optstr
);
662 drive_enable_snapshot(opts
, NULL
);
664 if (!drive_init(opts
, use_scsi
)) {
669 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
671 boot_set_handler
= func
;
672 boot_set_opaque
= opaque
;
675 int qemu_boot_set(const char *boot_devices
)
677 if (!boot_set_handler
) {
680 return boot_set_handler(boot_set_opaque
, boot_devices
);
683 static void validate_bootdevices(char *devices
)
685 /* We just do some generic consistency checks */
689 for (p
= devices
; *p
!= '\0'; p
++) {
690 /* Allowed boot devices are:
691 * a-b: floppy disk drives
692 * c-f: IDE disk drives
693 * g-m: machine implementation dependant drives
694 * n-p: network devices
695 * It's up to each machine implementation to check if the given boot
696 * devices match the actual hardware implementation and firmware
699 if (*p
< 'a' || *p
> 'p') {
700 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
703 if (bitmap
& (1 << (*p
- 'a'))) {
704 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
707 bitmap
|= 1 << (*p
- 'a');
711 static void restore_boot_devices(void *opaque
)
713 char *standard_boot_devices
= opaque
;
714 static int first
= 1;
716 /* Restore boot order and remove ourselves after the first boot */
722 qemu_boot_set(standard_boot_devices
);
724 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
725 qemu_free(standard_boot_devices
);
728 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
731 FWBootEntry
*node
, *i
;
737 assert(dev
!= NULL
|| suffix
!= NULL
);
739 node
= qemu_mallocz(sizeof(FWBootEntry
));
740 node
->bootindex
= bootindex
;
741 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
744 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
745 if (i
->bootindex
== bootindex
) {
746 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
748 } else if (i
->bootindex
< bootindex
) {
751 QTAILQ_INSERT_BEFORE(i
, node
, link
);
754 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
758 * This function returns null terminated string that consist of new line
759 * separated device pathes.
761 * memory pointed by "size" is assigned total length of the array in bytes
764 char *get_boot_devices_list(uint32_t *size
)
770 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
771 char *devpath
= NULL
, *bootpath
;
775 devpath
= qdev_get_fw_dev_path(i
->dev
);
779 if (i
->suffix
&& devpath
) {
780 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
782 bootpath
= qemu_malloc(bootpathlen
);
783 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
785 } else if (devpath
) {
788 bootpath
= qemu_strdup(i
->suffix
);
793 list
[total
-1] = '\n';
795 len
= strlen(bootpath
) + 1;
796 list
= qemu_realloc(list
, total
+ len
);
797 memcpy(&list
[total
], bootpath
, len
);
807 static void numa_add(const char *optarg
)
811 unsigned long long value
, endvalue
;
814 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
815 if (!strcmp(option
, "node")) {
816 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
817 nodenr
= nb_numa_nodes
;
819 nodenr
= strtoull(option
, NULL
, 10);
822 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
823 node_mem
[nodenr
] = 0;
826 sval
= strtosz(option
, NULL
);
828 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
831 node_mem
[nodenr
] = sval
;
833 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
834 node_cpumask
[nodenr
] = 0;
836 value
= strtoull(option
, &endptr
, 10);
839 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
841 if (*endptr
== '-') {
842 endvalue
= strtoull(endptr
+1, &endptr
, 10);
843 if (endvalue
>= 63) {
846 "only 63 CPUs in NUMA mode supported.\n");
848 value
= (2ULL << endvalue
) - (1ULL << value
);
850 value
= 1ULL << value
;
853 node_cpumask
[nodenr
] = value
;
860 static void smp_parse(const char *optarg
)
862 int smp
, sockets
= 0, threads
= 0, cores
= 0;
866 smp
= strtoul(optarg
, &endptr
, 10);
867 if (endptr
!= optarg
) {
868 if (*endptr
== ',') {
872 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
873 sockets
= strtoull(option
, NULL
, 10);
874 if (get_param_value(option
, 128, "cores", endptr
) != 0)
875 cores
= strtoull(option
, NULL
, 10);
876 if (get_param_value(option
, 128, "threads", endptr
) != 0)
877 threads
= strtoull(option
, NULL
, 10);
878 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
879 max_cpus
= strtoull(option
, NULL
, 10);
881 /* compute missing values, prefer sockets over cores over threads */
882 if (smp
== 0 || sockets
== 0) {
883 sockets
= sockets
> 0 ? sockets
: 1;
884 cores
= cores
> 0 ? cores
: 1;
885 threads
= threads
> 0 ? threads
: 1;
887 smp
= cores
* threads
* sockets
;
891 threads
= threads
> 0 ? threads
: 1;
892 cores
= smp
/ (sockets
* threads
);
894 threads
= smp
/ (cores
* sockets
);
898 smp_cores
= cores
> 0 ? cores
: 1;
899 smp_threads
= threads
> 0 ? threads
: 1;
904 /***********************************************************/
907 static int usb_device_add(const char *devname
)
910 USBDevice
*dev
= NULL
;
915 /* drivers with .usbdevice_name entry in USBDeviceInfo */
916 dev
= usbdevice_create(devname
);
921 if (strstart(devname
, "host:", &p
)) {
922 dev
= usb_host_device_open(p
);
923 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
924 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
925 bt_new_hci(qemu_find_bt_vlan(0)));
936 static int usb_device_del(const char *devname
)
941 if (strstart(devname
, "host:", &p
))
942 return usb_host_device_close(p
);
947 p
= strchr(devname
, '.');
950 bus_num
= strtoul(devname
, NULL
, 0);
951 addr
= strtoul(p
+ 1, NULL
, 0);
953 return usb_device_delete_addr(bus_num
, addr
);
956 static int usb_parse(const char *cmdline
)
959 r
= usb_device_add(cmdline
);
961 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
966 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
968 const char *devname
= qdict_get_str(qdict
, "devname");
969 if (usb_device_add(devname
) < 0) {
970 error_report("could not add USB device '%s'", devname
);
974 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
976 const char *devname
= qdict_get_str(qdict
, "devname");
977 if (usb_device_del(devname
) < 0) {
978 error_report("could not delete USB device '%s'", devname
);
982 /***********************************************************/
985 static struct pcmcia_socket_entry_s
{
986 PCMCIASocket
*socket
;
987 struct pcmcia_socket_entry_s
*next
;
988 } *pcmcia_sockets
= 0;
990 void pcmcia_socket_register(PCMCIASocket
*socket
)
992 struct pcmcia_socket_entry_s
*entry
;
994 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
995 entry
->socket
= socket
;
996 entry
->next
= pcmcia_sockets
;
997 pcmcia_sockets
= entry
;
1000 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1002 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1004 ptr
= &pcmcia_sockets
;
1005 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1006 if (entry
->socket
== socket
) {
1012 void pcmcia_info(Monitor
*mon
)
1014 struct pcmcia_socket_entry_s
*iter
;
1016 if (!pcmcia_sockets
)
1017 monitor_printf(mon
, "No PCMCIA sockets\n");
1019 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1020 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1021 iter
->socket
->attached
? iter
->socket
->card_string
:
1025 /***********************************************************/
1028 typedef struct IOHandlerRecord
{
1030 IOCanReadHandler
*fd_read_poll
;
1032 IOHandler
*fd_write
;
1035 /* temporary data */
1037 QLIST_ENTRY(IOHandlerRecord
) next
;
1040 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1041 QLIST_HEAD_INITIALIZER(io_handlers
);
1044 /* XXX: fd_read_poll should be suppressed, but an API change is
1045 necessary in the character devices to suppress fd_can_read(). */
1046 int qemu_set_fd_handler2(int fd
,
1047 IOCanReadHandler
*fd_read_poll
,
1049 IOHandler
*fd_write
,
1052 IOHandlerRecord
*ioh
;
1054 if (!fd_read
&& !fd_write
) {
1055 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1056 if (ioh
->fd
== fd
) {
1062 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1066 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1067 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1070 ioh
->fd_read_poll
= fd_read_poll
;
1071 ioh
->fd_read
= fd_read
;
1072 ioh
->fd_write
= fd_write
;
1073 ioh
->opaque
= opaque
;
1079 int qemu_set_fd_handler(int fd
,
1081 IOHandler
*fd_write
,
1084 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1087 /***********************************************************/
1088 /* machine registration */
1090 static QEMUMachine
*first_machine
= NULL
;
1091 QEMUMachine
*current_machine
= NULL
;
1093 int qemu_register_machine(QEMUMachine
*m
)
1096 pm
= &first_machine
;
1104 static QEMUMachine
*find_machine(const char *name
)
1108 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1109 if (!strcmp(m
->name
, name
))
1111 if (m
->alias
&& !strcmp(m
->alias
, name
))
1117 static QEMUMachine
*find_default_machine(void)
1121 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1122 if (m
->is_default
) {
1129 /***********************************************************/
1130 /* main execution loop */
1132 static void gui_update(void *opaque
)
1134 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1135 DisplayState
*ds
= opaque
;
1136 DisplayChangeListener
*dcl
= ds
->listeners
;
1138 qemu_flush_coalesced_mmio_buffer();
1141 while (dcl
!= NULL
) {
1142 if (dcl
->gui_timer_interval
&&
1143 dcl
->gui_timer_interval
< interval
)
1144 interval
= dcl
->gui_timer_interval
;
1147 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1150 static void nographic_update(void *opaque
)
1152 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1154 qemu_flush_coalesced_mmio_buffer();
1155 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1158 struct vm_change_state_entry
{
1159 VMChangeStateHandler
*cb
;
1161 QLIST_ENTRY (vm_change_state_entry
) entries
;
1164 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1166 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1169 VMChangeStateEntry
*e
;
1171 e
= qemu_mallocz(sizeof (*e
));
1175 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1179 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1181 QLIST_REMOVE (e
, entries
);
1185 void vm_state_notify(int running
, int reason
)
1187 VMChangeStateEntry
*e
;
1189 trace_vm_state_notify(running
, reason
);
1191 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1192 e
->cb(e
->opaque
, running
, reason
);
1201 vm_state_notify(1, 0);
1203 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1207 /* reset/shutdown handler */
1209 typedef struct QEMUResetEntry
{
1210 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1211 QEMUResetHandler
*func
;
1215 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1216 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1217 static int reset_requested
;
1218 static int shutdown_requested
;
1219 static int powerdown_requested
;
1220 static int debug_requested
;
1221 static int vmstop_requested
;
1223 int qemu_shutdown_requested(void)
1225 int r
= shutdown_requested
;
1226 shutdown_requested
= 0;
1230 int qemu_reset_requested(void)
1232 int r
= reset_requested
;
1233 reset_requested
= 0;
1237 int qemu_powerdown_requested(void)
1239 int r
= powerdown_requested
;
1240 powerdown_requested
= 0;
1244 static int qemu_debug_requested(void)
1246 int r
= debug_requested
;
1247 debug_requested
= 0;
1251 static int qemu_vmstop_requested(void)
1253 int r
= vmstop_requested
;
1254 vmstop_requested
= 0;
1258 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1260 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1263 re
->opaque
= opaque
;
1264 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1267 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1271 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1272 if (re
->func
== func
&& re
->opaque
== opaque
) {
1273 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1280 void qemu_system_reset(void)
1282 QEMUResetEntry
*re
, *nre
;
1284 /* reset all devices */
1285 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1286 re
->func(re
->opaque
);
1288 monitor_protocol_event(QEVENT_RESET
, NULL
);
1289 cpu_synchronize_all_post_reset();
1292 void qemu_system_reset_request(void)
1295 shutdown_requested
= 1;
1297 reset_requested
= 1;
1300 qemu_notify_event();
1303 void qemu_system_shutdown_request(void)
1305 shutdown_requested
= 1;
1306 qemu_notify_event();
1309 void qemu_system_powerdown_request(void)
1311 powerdown_requested
= 1;
1312 qemu_notify_event();
1315 void qemu_system_debug_request(void)
1317 debug_requested
= 1;
1318 qemu_notify_event();
1321 void qemu_system_vmstop_request(int reason
)
1323 vmstop_requested
= reason
;
1324 qemu_notify_event();
1327 void main_loop_wait(int nonblocking
)
1329 IOHandlerRecord
*ioh
;
1330 fd_set rfds
, wfds
, xfds
;
1338 timeout
= qemu_calculate_timeout();
1339 qemu_bh_update_timeout(&timeout
);
1342 os_host_main_loop_wait(&timeout
);
1344 /* poll any events */
1345 /* XXX: separate device handlers from system ones */
1350 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1354 (!ioh
->fd_read_poll
||
1355 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1356 FD_SET(ioh
->fd
, &rfds
);
1360 if (ioh
->fd_write
) {
1361 FD_SET(ioh
->fd
, &wfds
);
1367 tv
.tv_sec
= timeout
/ 1000;
1368 tv
.tv_usec
= (timeout
% 1000) * 1000;
1370 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1372 qemu_mutex_unlock_iothread();
1373 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1374 qemu_mutex_lock_iothread();
1376 IOHandlerRecord
*pioh
;
1378 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1379 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1380 ioh
->fd_read(ioh
->opaque
);
1382 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1383 ioh
->fd_write(ioh
->opaque
);
1386 /* Do this last in case read/write handlers marked it for deletion */
1388 QLIST_REMOVE(ioh
, next
);
1394 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1396 qemu_run_all_timers();
1398 /* Check bottom-halves last in case any of the earlier events triggered
1404 #ifndef CONFIG_IOTHREAD
1405 static int vm_request_pending(void)
1407 return powerdown_requested
||
1409 shutdown_requested
||
1415 qemu_irq qemu_system_powerdown
;
1417 static void main_loop(void)
1419 bool nonblocking
= false;
1420 #ifdef CONFIG_PROFILER
1425 qemu_main_loop_start();
1428 #ifndef CONFIG_IOTHREAD
1429 nonblocking
= cpu_exec_all();
1430 if (vm_request_pending()) {
1434 #ifdef CONFIG_PROFILER
1435 ti
= profile_getclock();
1437 main_loop_wait(nonblocking
);
1438 #ifdef CONFIG_PROFILER
1439 dev_time
+= profile_getclock() - ti
;
1442 if (qemu_debug_requested()) {
1443 vm_stop(VMSTOP_DEBUG
);
1445 if (qemu_shutdown_requested()) {
1446 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1448 vm_stop(VMSTOP_SHUTDOWN
);
1453 if (qemu_reset_requested()) {
1455 cpu_synchronize_all_states();
1456 qemu_system_reset();
1459 if (qemu_powerdown_requested()) {
1460 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1461 qemu_irq_raise(qemu_system_powerdown
);
1463 if ((r
= qemu_vmstop_requested())) {
1471 static void version(void)
1473 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1476 static void help(int exitcode
)
1478 const char *options_help
=
1479 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1481 #define DEFHEADING(text) stringify(text) "\n"
1482 #include "qemu-options.def"
1488 printf("usage: %s [options] [disk_image]\n"
1490 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1493 "During emulation, the following keys are useful:\n"
1494 "ctrl-alt-f toggle full screen\n"
1495 "ctrl-alt-n switch to virtual console 'n'\n"
1496 "ctrl-alt toggle mouse and keyboard grab\n"
1498 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1504 #define HAS_ARG 0x0001
1506 typedef struct QEMUOption
{
1513 static const QEMUOption qemu_options
[] = {
1514 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1515 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1516 { option, opt_arg, opt_enum, arch_mask },
1517 #define DEFHEADING(text)
1518 #include "qemu-options.def"
1524 static void select_vgahw (const char *p
)
1529 vga_interface_type
= VGA_NONE
;
1530 if (strstart(p
, "std", &opts
)) {
1531 vga_interface_type
= VGA_STD
;
1532 } else if (strstart(p
, "cirrus", &opts
)) {
1533 vga_interface_type
= VGA_CIRRUS
;
1534 } else if (strstart(p
, "vmware", &opts
)) {
1535 vga_interface_type
= VGA_VMWARE
;
1536 } else if (strstart(p
, "xenfb", &opts
)) {
1537 vga_interface_type
= VGA_XENFB
;
1538 } else if (strstart(p
, "qxl", &opts
)) {
1539 vga_interface_type
= VGA_QXL
;
1540 } else if (!strstart(p
, "none", &opts
)) {
1542 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1546 const char *nextopt
;
1548 if (strstart(opts
, ",retrace=", &nextopt
)) {
1550 if (strstart(opts
, "dumb", &nextopt
))
1551 vga_retrace_method
= VGA_RETRACE_DUMB
;
1552 else if (strstart(opts
, "precise", &nextopt
))
1553 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1554 else goto invalid_vga
;
1555 } else goto invalid_vga
;
1560 static DisplayType
select_display(const char *p
)
1563 DisplayType display
= DT_DEFAULT
;
1565 if (strstart(p
, "sdl", &opts
)) {
1569 const char *nextopt
;
1571 if (strstart(opts
, ",frame=", &nextopt
)) {
1573 if (strstart(opts
, "on", &nextopt
)) {
1575 } else if (strstart(opts
, "off", &nextopt
)) {
1578 goto invalid_display
;
1580 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1582 if (strstart(opts
, "on", &nextopt
)) {
1584 } else if (strstart(opts
, "off", &nextopt
)) {
1587 goto invalid_display
;
1589 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1591 if (strstart(opts
, "on", &nextopt
)) {
1593 } else if (strstart(opts
, "off", &nextopt
)) {
1596 goto invalid_display
;
1598 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1600 if (strstart(opts
, "on", &nextopt
)) {
1602 } else if (strstart(opts
, "off", &nextopt
)) {
1605 goto invalid_display
;
1608 goto invalid_display
;
1613 fprintf(stderr
, "SDL support is disabled\n");
1616 } else if (strstart(p
, "vnc", &opts
)) {
1621 const char *nextopt
;
1623 if (strstart(opts
, "=", &nextopt
)) {
1624 vnc_display
= nextopt
;
1628 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1632 fprintf(stderr
, "VNC support is disabled\n");
1635 } else if (strstart(p
, "curses", &opts
)) {
1636 #ifdef CONFIG_CURSES
1637 display
= DT_CURSES
;
1639 fprintf(stderr
, "Curses support is disabled\n");
1642 } else if (strstart(p
, "none", &opts
)) {
1646 fprintf(stderr
, "Unknown display type: %s\n", p
);
1653 static int balloon_parse(const char *arg
)
1657 if (strcmp(arg
, "none") == 0) {
1661 if (!strncmp(arg
, "virtio", 6)) {
1662 if (arg
[6] == ',') {
1663 /* have params -> parse them */
1664 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1668 /* create empty opts */
1669 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1671 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1678 char *qemu_find_file(int type
, const char *name
)
1684 /* If name contains path separators then try it as a straight path. */
1685 if ((strchr(name
, '/') || strchr(name
, '\\'))
1686 && access(name
, R_OK
) == 0) {
1687 return qemu_strdup(name
);
1690 case QEMU_FILE_TYPE_BIOS
:
1693 case QEMU_FILE_TYPE_KEYMAP
:
1694 subdir
= "keymaps/";
1699 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1700 buf
= qemu_mallocz(len
);
1701 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1702 if (access(buf
, R_OK
)) {
1709 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1711 return qdev_device_help(opts
);
1714 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1718 dev
= qdev_device_add(opts
);
1724 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1726 CharDriverState
*chr
;
1728 chr
= qemu_chr_open_opts(opts
, NULL
);
1734 #ifdef CONFIG_VIRTFS
1735 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1738 ret
= qemu_fsdev_add(opts
);
1744 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1746 CharDriverState
*chr
;
1747 const char *chardev
;
1751 mode
= qemu_opt_get(opts
, "mode");
1755 if (strcmp(mode
, "readline") == 0) {
1756 flags
= MONITOR_USE_READLINE
;
1757 } else if (strcmp(mode
, "control") == 0) {
1758 flags
= MONITOR_USE_CONTROL
;
1760 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1764 if (qemu_opt_get_bool(opts
, "pretty", 0))
1765 flags
|= MONITOR_USE_PRETTY
;
1767 if (qemu_opt_get_bool(opts
, "default", 0))
1768 flags
|= MONITOR_IS_DEFAULT
;
1770 chardev
= qemu_opt_get(opts
, "chardev");
1771 chr
= qemu_chr_find(chardev
);
1773 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1777 monitor_init(chr
, flags
);
1781 static void monitor_parse(const char *optarg
, const char *mode
)
1783 static int monitor_device_index
= 0;
1789 if (strstart(optarg
, "chardev:", &p
)) {
1790 snprintf(label
, sizeof(label
), "%s", p
);
1792 snprintf(label
, sizeof(label
), "compat_monitor%d",
1793 monitor_device_index
);
1794 if (monitor_device_index
== 0) {
1797 opts
= qemu_chr_parse_compat(label
, optarg
);
1799 fprintf(stderr
, "parse error: %s\n", optarg
);
1804 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1806 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1809 qemu_opt_set(opts
, "mode", mode
);
1810 qemu_opt_set(opts
, "chardev", label
);
1812 qemu_opt_set(opts
, "default", "on");
1813 monitor_device_index
++;
1816 struct device_config
{
1818 DEV_USB
, /* -usbdevice */
1820 DEV_SERIAL
, /* -serial */
1821 DEV_PARALLEL
, /* -parallel */
1822 DEV_VIRTCON
, /* -virtioconsole */
1823 DEV_DEBUGCON
, /* -debugcon */
1825 const char *cmdline
;
1826 QTAILQ_ENTRY(device_config
) next
;
1828 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1830 static void add_device_config(int type
, const char *cmdline
)
1832 struct device_config
*conf
;
1834 conf
= qemu_mallocz(sizeof(*conf
));
1836 conf
->cmdline
= cmdline
;
1837 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1840 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1842 struct device_config
*conf
;
1845 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1846 if (conf
->type
!= type
)
1848 rc
= func(conf
->cmdline
);
1855 static int serial_parse(const char *devname
)
1857 static int index
= 0;
1860 if (strcmp(devname
, "none") == 0)
1862 if (index
== MAX_SERIAL_PORTS
) {
1863 fprintf(stderr
, "qemu: too many serial ports\n");
1866 snprintf(label
, sizeof(label
), "serial%d", index
);
1867 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1868 if (!serial_hds
[index
]) {
1869 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1870 devname
, strerror(errno
));
1877 static int parallel_parse(const char *devname
)
1879 static int index
= 0;
1882 if (strcmp(devname
, "none") == 0)
1884 if (index
== MAX_PARALLEL_PORTS
) {
1885 fprintf(stderr
, "qemu: too many parallel ports\n");
1888 snprintf(label
, sizeof(label
), "parallel%d", index
);
1889 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1890 if (!parallel_hds
[index
]) {
1891 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1892 devname
, strerror(errno
));
1899 static int virtcon_parse(const char *devname
)
1901 QemuOptsList
*device
= qemu_find_opts("device");
1902 static int index
= 0;
1904 QemuOpts
*bus_opts
, *dev_opts
;
1906 if (strcmp(devname
, "none") == 0)
1908 if (index
== MAX_VIRTIO_CONSOLES
) {
1909 fprintf(stderr
, "qemu: too many virtio consoles\n");
1913 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1914 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1916 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1917 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1919 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1920 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1921 if (!virtcon_hds
[index
]) {
1922 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1923 devname
, strerror(errno
));
1926 qemu_opt_set(dev_opts
, "chardev", label
);
1932 static int debugcon_parse(const char *devname
)
1936 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1939 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1941 fprintf(stderr
, "qemu: already have a debugcon device\n");
1944 qemu_opt_set(opts
, "driver", "isa-debugcon");
1945 qemu_opt_set(opts
, "chardev", "debugcon");
1949 void qemu_add_exit_notifier(Notifier
*notify
)
1951 notifier_list_add(&exit_notifiers
, notify
);
1954 void qemu_remove_exit_notifier(Notifier
*notify
)
1956 notifier_list_remove(&exit_notifiers
, notify
);
1959 static void qemu_run_exit_notifiers(void)
1961 notifier_list_notify(&exit_notifiers
);
1964 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1966 notifier_list_add(&machine_init_done_notifiers
, notify
);
1969 static void qemu_run_machine_init_done_notifiers(void)
1971 notifier_list_notify(&machine_init_done_notifiers
);
1974 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1975 const char **poptarg
, int *poptind
)
1977 const QEMUOption
*popt
;
1978 int optind
= *poptind
;
1979 char *r
= argv
[optind
];
1982 loc_set_cmdline(argv
, optind
, 1);
1984 /* Treat --foo the same as -foo. */
1987 popt
= qemu_options
;
1990 error_report("invalid option");
1993 if (!strcmp(popt
->name
, r
+ 1))
1997 if (popt
->flags
& HAS_ARG
) {
1998 if (optind
>= argc
) {
1999 error_report("requires an argument");
2002 optarg
= argv
[optind
++];
2003 loc_set_cmdline(argv
, optind
- 2, 2);
2014 int main(int argc
, char **argv
, char **envp
)
2016 const char *gdbstub_dev
= NULL
;
2018 int snapshot
, linux_boot
;
2019 const char *icount_option
= NULL
;
2020 const char *initrd_filename
;
2021 const char *kernel_filename
, *kernel_cmdline
;
2022 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2024 DisplayChangeListener
*dcl
;
2025 int cyls
, heads
, secs
, translation
;
2026 QemuOpts
*hda_opts
= NULL
, *opts
;
2027 QemuOptsList
*olist
;
2030 const char *loadvm
= NULL
;
2031 QEMUMachine
*machine
;
2032 const char *cpu_model
;
2034 const char *pid_file
= NULL
;
2035 const char *incoming
= NULL
;
2037 int show_vnc_port
= 0;
2040 const char *trace_file
= NULL
;
2042 atexit(qemu_run_exit_notifiers
);
2043 error_set_progname(argv
[0]);
2047 qemu_cache_utils_init(envp
);
2049 QLIST_INIT (&vm_change_state_head
);
2050 os_setup_early_signal_handling();
2052 module_call_init(MODULE_INIT_MACHINE
);
2053 machine
= find_default_machine();
2055 initrd_filename
= NULL
;
2058 kernel_filename
= NULL
;
2059 kernel_cmdline
= "";
2060 cyls
= heads
= secs
= 0;
2061 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2063 for (i
= 0; i
< MAX_NODES
; i
++) {
2065 node_cpumask
[i
] = 0;
2074 /* first pass of option parsing */
2076 while (optind
< argc
) {
2077 if (argv
[optind
][0] != '-') {
2082 const QEMUOption
*popt
;
2084 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2085 switch (popt
->index
) {
2086 case QEMU_OPTION_nodefconfig
:
2096 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2097 if (ret
< 0 && ret
!= -ENOENT
) {
2101 ret
= qemu_read_config_file(arch_config_name
);
2102 if (ret
< 0 && ret
!= -ENOENT
) {
2108 /* second pass of option parsing */
2113 if (argv
[optind
][0] != '-') {
2114 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2116 const QEMUOption
*popt
;
2118 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2119 if (!(popt
->arch_mask
& arch_type
)) {
2120 printf("Option %s not supported for this target\n", popt
->name
);
2123 switch(popt
->index
) {
2125 machine
= find_machine(optarg
);
2128 printf("Supported machines are:\n");
2129 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2131 printf("%-10s %s (alias of %s)\n",
2132 m
->alias
, m
->desc
, m
->name
);
2133 printf("%-10s %s%s\n",
2135 m
->is_default
? " (default)" : "");
2137 exit(*optarg
!= '?');
2140 case QEMU_OPTION_cpu
:
2141 /* hw initialization will check this */
2142 if (*optarg
== '?') {
2143 list_cpus(stdout
, &fprintf
, optarg
);
2149 case QEMU_OPTION_initrd
:
2150 initrd_filename
= optarg
;
2152 case QEMU_OPTION_hda
:
2156 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2158 snprintf(buf
, sizeof(buf
),
2159 "%s,cyls=%d,heads=%d,secs=%d%s",
2160 HD_OPTS
, cyls
, heads
, secs
,
2161 translation
== BIOS_ATA_TRANSLATION_LBA
?
2163 translation
== BIOS_ATA_TRANSLATION_NONE
?
2164 ",trans=none" : "");
2165 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2168 case QEMU_OPTION_hdb
:
2169 case QEMU_OPTION_hdc
:
2170 case QEMU_OPTION_hdd
:
2171 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2174 case QEMU_OPTION_drive
:
2177 case QEMU_OPTION_set
:
2178 if (qemu_set_option(optarg
) != 0)
2181 case QEMU_OPTION_global
:
2182 if (qemu_global_option(optarg
) != 0)
2185 case QEMU_OPTION_mtdblock
:
2186 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2188 case QEMU_OPTION_sd
:
2189 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2191 case QEMU_OPTION_pflash
:
2192 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2194 case QEMU_OPTION_snapshot
:
2197 case QEMU_OPTION_hdachs
:
2201 cyls
= strtol(p
, (char **)&p
, 0);
2202 if (cyls
< 1 || cyls
> 16383)
2207 heads
= strtol(p
, (char **)&p
, 0);
2208 if (heads
< 1 || heads
> 16)
2213 secs
= strtol(p
, (char **)&p
, 0);
2214 if (secs
< 1 || secs
> 63)
2218 if (!strcmp(p
, "none"))
2219 translation
= BIOS_ATA_TRANSLATION_NONE
;
2220 else if (!strcmp(p
, "lba"))
2221 translation
= BIOS_ATA_TRANSLATION_LBA
;
2222 else if (!strcmp(p
, "auto"))
2223 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2226 } else if (*p
!= '\0') {
2228 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2231 if (hda_opts
!= NULL
) {
2233 snprintf(num
, sizeof(num
), "%d", cyls
);
2234 qemu_opt_set(hda_opts
, "cyls", num
);
2235 snprintf(num
, sizeof(num
), "%d", heads
);
2236 qemu_opt_set(hda_opts
, "heads", num
);
2237 snprintf(num
, sizeof(num
), "%d", secs
);
2238 qemu_opt_set(hda_opts
, "secs", num
);
2239 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2240 qemu_opt_set(hda_opts
, "trans", "lba");
2241 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2242 qemu_opt_set(hda_opts
, "trans", "none");
2246 case QEMU_OPTION_numa
:
2247 if (nb_numa_nodes
>= MAX_NODES
) {
2248 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2253 case QEMU_OPTION_display
:
2254 display_type
= select_display(optarg
);
2256 case QEMU_OPTION_nographic
:
2257 display_type
= DT_NOGRAPHIC
;
2259 case QEMU_OPTION_curses
:
2260 #ifdef CONFIG_CURSES
2261 display_type
= DT_CURSES
;
2263 fprintf(stderr
, "Curses support is disabled\n");
2267 case QEMU_OPTION_portrait
:
2270 case QEMU_OPTION_kernel
:
2271 kernel_filename
= optarg
;
2273 case QEMU_OPTION_append
:
2274 kernel_cmdline
= optarg
;
2276 case QEMU_OPTION_cdrom
:
2277 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2279 case QEMU_OPTION_boot
:
2281 static const char * const params
[] = {
2282 "order", "once", "menu", NULL
2284 char buf
[sizeof(boot_devices
)];
2285 char *standard_boot_devices
;
2288 if (!strchr(optarg
, '=')) {
2290 pstrcpy(buf
, sizeof(buf
), optarg
);
2291 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2293 "qemu: unknown boot parameter '%s' in '%s'\n",
2299 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2300 validate_bootdevices(buf
);
2301 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2304 if (get_param_value(buf
, sizeof(buf
),
2306 validate_bootdevices(buf
);
2307 standard_boot_devices
= qemu_strdup(boot_devices
);
2308 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2309 qemu_register_reset(restore_boot_devices
,
2310 standard_boot_devices
);
2312 if (get_param_value(buf
, sizeof(buf
),
2314 if (!strcmp(buf
, "on")) {
2316 } else if (!strcmp(buf
, "off")) {
2320 "qemu: invalid option value '%s'\n",
2328 case QEMU_OPTION_fda
:
2329 case QEMU_OPTION_fdb
:
2330 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2333 case QEMU_OPTION_no_fd_bootchk
:
2336 case QEMU_OPTION_netdev
:
2337 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2341 case QEMU_OPTION_net
:
2342 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2347 case QEMU_OPTION_tftp
:
2348 legacy_tftp_prefix
= optarg
;
2350 case QEMU_OPTION_bootp
:
2351 legacy_bootp_filename
= optarg
;
2353 case QEMU_OPTION_redir
:
2354 if (net_slirp_redir(optarg
) < 0)
2358 case QEMU_OPTION_bt
:
2359 add_device_config(DEV_BT
, optarg
);
2361 case QEMU_OPTION_audio_help
:
2362 if (!(audio_available())) {
2363 printf("Option %s not supported for this target\n", popt
->name
);
2369 case QEMU_OPTION_soundhw
:
2370 if (!(audio_available())) {
2371 printf("Option %s not supported for this target\n", popt
->name
);
2374 select_soundhw (optarg
);
2379 case QEMU_OPTION_version
:
2383 case QEMU_OPTION_m
: {
2386 value
= strtosz(optarg
, NULL
);
2388 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2392 /* On 32-bit hosts, QEMU is limited by virtual address space */
2393 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2394 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2397 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2398 fprintf(stderr
, "qemu: ram size too large\n");
2404 case QEMU_OPTION_mempath
:
2408 case QEMU_OPTION_mem_prealloc
:
2413 set_cpu_log(optarg
);
2416 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2418 case QEMU_OPTION_gdb
:
2419 gdbstub_dev
= optarg
;
2424 case QEMU_OPTION_bios
:
2427 case QEMU_OPTION_singlestep
:
2434 keyboard_layout
= optarg
;
2436 case QEMU_OPTION_localtime
:
2439 case QEMU_OPTION_vga
:
2440 select_vgahw (optarg
);
2447 w
= strtol(p
, (char **)&p
, 10);
2450 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2456 h
= strtol(p
, (char **)&p
, 10);
2461 depth
= strtol(p
, (char **)&p
, 10);
2462 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2463 depth
!= 24 && depth
!= 32)
2465 } else if (*p
== '\0') {
2466 depth
= graphic_depth
;
2473 graphic_depth
= depth
;
2476 case QEMU_OPTION_echr
:
2479 term_escape_char
= strtol(optarg
, &r
, 0);
2481 printf("Bad argument to echr\n");
2484 case QEMU_OPTION_monitor
:
2485 monitor_parse(optarg
, "readline");
2486 default_monitor
= 0;
2488 case QEMU_OPTION_qmp
:
2489 monitor_parse(optarg
, "control");
2490 default_monitor
= 0;
2492 case QEMU_OPTION_mon
:
2493 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2497 default_monitor
= 0;
2499 case QEMU_OPTION_chardev
:
2500 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2505 case QEMU_OPTION_fsdev
:
2506 olist
= qemu_find_opts("fsdev");
2508 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2511 opts
= qemu_opts_parse(olist
, optarg
, 1);
2513 fprintf(stderr
, "parse error: %s\n", optarg
);
2517 case QEMU_OPTION_virtfs
: {
2518 char *arg_fsdev
= NULL
;
2519 char *arg_9p
= NULL
;
2522 olist
= qemu_find_opts("virtfs");
2524 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2527 opts
= qemu_opts_parse(olist
, optarg
, 1);
2529 fprintf(stderr
, "parse error: %s\n", optarg
);
2533 if (qemu_opt_get(opts
, "fstype") == NULL
||
2534 qemu_opt_get(opts
, "mount_tag") == NULL
||
2535 qemu_opt_get(opts
, "path") == NULL
||
2536 qemu_opt_get(opts
, "security_model") == NULL
) {
2537 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2538 "security_model=[mapped|passthrough|none],"
2543 len
= strlen(",id=,path=,security_model=");
2544 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2545 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2546 len
+= strlen(qemu_opt_get(opts
, "path"));
2547 len
+= strlen(qemu_opt_get(opts
, "security_model"));
2548 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2550 snprintf(arg_fsdev
, (len
+ 1) * sizeof(*arg_fsdev
),
2551 "%s,id=%s,path=%s,security_model=%s",
2552 qemu_opt_get(opts
, "fstype"),
2553 qemu_opt_get(opts
, "mount_tag"),
2554 qemu_opt_get(opts
, "path"),
2555 qemu_opt_get(opts
, "security_model"));
2557 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2558 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2559 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2561 snprintf(arg_9p
, (len
+ 1) * sizeof(*arg_9p
),
2562 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2563 qemu_opt_get(opts
, "mount_tag"),
2564 qemu_opt_get(opts
, "mount_tag"));
2566 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev
, 1)) {
2567 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2571 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p
, 1)) {
2572 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2576 qemu_free(arg_fsdev
);
2580 case QEMU_OPTION_serial
:
2581 add_device_config(DEV_SERIAL
, optarg
);
2583 if (strncmp(optarg
, "mon:", 4) == 0) {
2584 default_monitor
= 0;
2587 case QEMU_OPTION_watchdog
:
2590 "qemu: only one watchdog option may be given\n");
2595 case QEMU_OPTION_watchdog_action
:
2596 if (select_watchdog_action(optarg
) == -1) {
2597 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2601 case QEMU_OPTION_virtiocon
:
2602 add_device_config(DEV_VIRTCON
, optarg
);
2603 default_virtcon
= 0;
2604 if (strncmp(optarg
, "mon:", 4) == 0) {
2605 default_monitor
= 0;
2608 case QEMU_OPTION_parallel
:
2609 add_device_config(DEV_PARALLEL
, optarg
);
2610 default_parallel
= 0;
2611 if (strncmp(optarg
, "mon:", 4) == 0) {
2612 default_monitor
= 0;
2615 case QEMU_OPTION_debugcon
:
2616 add_device_config(DEV_DEBUGCON
, optarg
);
2618 case QEMU_OPTION_loadvm
:
2621 case QEMU_OPTION_full_screen
:
2625 case QEMU_OPTION_no_frame
:
2628 case QEMU_OPTION_alt_grab
:
2631 case QEMU_OPTION_ctrl_grab
:
2634 case QEMU_OPTION_no_quit
:
2637 case QEMU_OPTION_sdl
:
2638 display_type
= DT_SDL
;
2641 case QEMU_OPTION_no_frame
:
2642 case QEMU_OPTION_alt_grab
:
2643 case QEMU_OPTION_ctrl_grab
:
2644 case QEMU_OPTION_no_quit
:
2645 case QEMU_OPTION_sdl
:
2646 fprintf(stderr
, "SDL support is disabled\n");
2649 case QEMU_OPTION_pidfile
:
2652 case QEMU_OPTION_win2k_hack
:
2653 win2k_install_hack
= 1;
2655 case QEMU_OPTION_rtc_td_hack
:
2658 case QEMU_OPTION_acpitable
:
2659 do_acpitable_option(optarg
);
2661 case QEMU_OPTION_smbios
:
2662 do_smbios_option(optarg
);
2664 case QEMU_OPTION_enable_kvm
:
2667 case QEMU_OPTION_usb
:
2670 case QEMU_OPTION_usbdevice
:
2672 add_device_config(DEV_USB
, optarg
);
2674 case QEMU_OPTION_device
:
2675 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2679 case QEMU_OPTION_smp
:
2682 fprintf(stderr
, "Invalid number of CPUs\n");
2685 if (max_cpus
< smp_cpus
) {
2686 fprintf(stderr
, "maxcpus must be equal to or greater than "
2690 if (max_cpus
> 255) {
2691 fprintf(stderr
, "Unsupported number of maxcpus\n");
2695 case QEMU_OPTION_vnc
:
2698 vnc_display
= optarg
;
2700 fprintf(stderr
, "VNC support is disabled\n");
2704 case QEMU_OPTION_no_acpi
:
2707 case QEMU_OPTION_no_hpet
:
2710 case QEMU_OPTION_balloon
:
2711 if (balloon_parse(optarg
) < 0) {
2712 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2716 case QEMU_OPTION_no_reboot
:
2719 case QEMU_OPTION_no_shutdown
:
2722 case QEMU_OPTION_show_cursor
:
2725 case QEMU_OPTION_uuid
:
2726 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2727 fprintf(stderr
, "Fail to parse UUID string."
2728 " Wrong format.\n");
2732 case QEMU_OPTION_option_rom
:
2733 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2734 fprintf(stderr
, "Too many option ROMs\n");
2737 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2738 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2739 option_rom
[nb_option_roms
].bootindex
=
2740 qemu_opt_get_number(opts
, "bootindex", -1);
2741 if (!option_rom
[nb_option_roms
].name
) {
2742 fprintf(stderr
, "Option ROM file is not specified\n");
2747 case QEMU_OPTION_semihosting
:
2748 semihosting_enabled
= 1;
2750 case QEMU_OPTION_name
:
2751 qemu_name
= qemu_strdup(optarg
);
2753 char *p
= strchr(qemu_name
, ',');
2756 if (strncmp(p
, "process=", 8)) {
2757 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2761 os_set_proc_name(p
);
2765 case QEMU_OPTION_prom_env
:
2766 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2767 fprintf(stderr
, "Too many prom variables\n");
2770 prom_envs
[nb_prom_envs
] = optarg
;
2773 case QEMU_OPTION_old_param
:
2776 case QEMU_OPTION_clock
:
2777 configure_alarms(optarg
);
2779 case QEMU_OPTION_startdate
:
2780 configure_rtc_date_offset(optarg
, 1);
2782 case QEMU_OPTION_rtc
:
2783 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2787 configure_rtc(opts
);
2789 case QEMU_OPTION_tb_size
:
2790 tb_size
= strtol(optarg
, NULL
, 0);
2794 case QEMU_OPTION_icount
:
2795 icount_option
= optarg
;
2797 case QEMU_OPTION_incoming
:
2799 incoming_expected
= true;
2801 case QEMU_OPTION_nodefaults
:
2803 default_parallel
= 0;
2804 default_virtcon
= 0;
2805 default_monitor
= 0;
2812 case QEMU_OPTION_xen_domid
:
2813 if (!(xen_available())) {
2814 printf("Option %s not supported for this target\n", popt
->name
);
2817 xen_domid
= atoi(optarg
);
2819 case QEMU_OPTION_xen_create
:
2820 if (!(xen_available())) {
2821 printf("Option %s not supported for this target\n", popt
->name
);
2824 xen_mode
= XEN_CREATE
;
2826 case QEMU_OPTION_xen_attach
:
2827 if (!(xen_available())) {
2828 printf("Option %s not supported for this target\n", popt
->name
);
2831 xen_mode
= XEN_ATTACH
;
2833 #ifdef CONFIG_SIMPLE_TRACE
2834 case QEMU_OPTION_trace
:
2835 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2837 trace_file
= qemu_opt_get(opts
, "file");
2841 case QEMU_OPTION_readconfig
:
2843 int ret
= qemu_read_config_file(optarg
);
2845 fprintf(stderr
, "read config %s: %s\n", optarg
,
2851 case QEMU_OPTION_spice
:
2852 olist
= qemu_find_opts("spice");
2854 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2857 opts
= qemu_opts_parse(olist
, optarg
, 0);
2859 fprintf(stderr
, "parse error: %s\n", optarg
);
2863 case QEMU_OPTION_writeconfig
:
2866 if (strcmp(optarg
, "-") == 0) {
2869 fp
= fopen(optarg
, "w");
2871 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2875 qemu_config_write(fp
);
2880 os_parse_cmd_args(popt
->index
, optarg
);
2886 if (!st_init(trace_file
)) {
2887 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2890 /* If no data_dir is specified then try to find it relative to the
2893 data_dir
= os_find_datadir(argv
[0]);
2895 /* If all else fails use the install patch specified when building. */
2897 data_dir
= CONFIG_QEMU_DATADIR
;
2901 * Default to max_cpus = smp_cpus, in case the user doesn't
2902 * specify a max_cpus value.
2905 max_cpus
= smp_cpus
;
2907 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2908 if (smp_cpus
> machine
->max_cpus
) {
2909 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2910 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2915 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
2916 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
2918 if (machine
->no_serial
) {
2921 if (machine
->no_parallel
) {
2922 default_parallel
= 0;
2924 if (!machine
->use_virtcon
) {
2925 default_virtcon
= 0;
2927 if (machine
->no_vga
) {
2930 if (machine
->no_floppy
) {
2933 if (machine
->no_cdrom
) {
2936 if (machine
->no_sdcard
) {
2940 if (display_type
== DT_NOGRAPHIC
) {
2941 if (default_parallel
)
2942 add_device_config(DEV_PARALLEL
, "null");
2943 if (default_serial
&& default_monitor
) {
2944 add_device_config(DEV_SERIAL
, "mon:stdio");
2945 } else if (default_virtcon
&& default_monitor
) {
2946 add_device_config(DEV_VIRTCON
, "mon:stdio");
2949 add_device_config(DEV_SERIAL
, "stdio");
2950 if (default_virtcon
)
2951 add_device_config(DEV_VIRTCON
, "stdio");
2952 if (default_monitor
)
2953 monitor_parse("stdio", "readline");
2957 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2958 if (default_parallel
)
2959 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2960 if (default_monitor
)
2961 monitor_parse("vc:80Cx24C", "readline");
2962 if (default_virtcon
)
2963 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2966 vga_interface_type
= VGA_CIRRUS
;
2970 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
2972 #ifdef CONFIG_VIRTFS
2973 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
2980 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
2986 int ret
= kvm_init();
2988 if (!kvm_available()) {
2989 printf("KVM not supported for this target\n");
2991 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
2997 if (qemu_init_main_loop()) {
2998 fprintf(stderr
, "qemu_init_main_loop failed\n");
3001 linux_boot
= (kernel_filename
!= NULL
);
3003 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3004 fprintf(stderr
, "-append only allowed with -kernel option\n");
3008 if (!linux_boot
&& initrd_filename
!= NULL
) {
3009 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3013 os_set_line_buffering();
3015 if (init_timer_alarm() < 0) {
3016 fprintf(stderr
, "could not initialize alarm timer\n");
3019 configure_icount(icount_option
);
3021 if (net_init_clients() < 0) {
3025 /* init the bluetooth world */
3026 if (foreach_device_config(DEV_BT
, bt_parse
))
3029 /* init the memory */
3031 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3033 /* init the dynamic translator */
3034 cpu_exec_init_all(tb_size
* 1024 * 1024);
3036 bdrv_init_with_whitelist();
3040 /* open the virtual block devices */
3042 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3043 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3046 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3047 IF_DEFAULT
, 2, CDROM_OPTS
);
3048 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3049 IF_FLOPPY
, 0, FD_OPTS
);
3050 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3053 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3056 if (nb_numa_nodes
> 0) {
3059 if (nb_numa_nodes
> smp_cpus
) {
3060 nb_numa_nodes
= smp_cpus
;
3063 /* If no memory size if given for any node, assume the default case
3064 * and distribute the available memory equally across all nodes
3066 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3067 if (node_mem
[i
] != 0)
3070 if (i
== nb_numa_nodes
) {
3071 uint64_t usedmem
= 0;
3073 /* On Linux, the each node's border has to be 8MB aligned,
3074 * the final node gets the rest.
3076 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3077 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3078 usedmem
+= node_mem
[i
];
3080 node_mem
[i
] = ram_size
- usedmem
;
3083 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3084 if (node_cpumask
[i
] != 0)
3087 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3088 * must cope with this anyway, because there are BIOSes out there in
3089 * real machines which also use this scheme.
3091 if (i
== nb_numa_nodes
) {
3092 for (i
= 0; i
< smp_cpus
; i
++) {
3093 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3098 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3102 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3104 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3106 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3108 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3111 module_call_init(MODULE_INIT_DEVICE
);
3113 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3117 i
= select_watchdog(watchdog
);
3119 exit (i
== 1 ? 1 : 0);
3122 if (machine
->compat_props
) {
3123 qdev_prop_register_global_list(machine
->compat_props
);
3127 machine
->init(ram_size
, boot_devices
,
3128 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3130 cpu_synchronize_all_post_init();
3132 /* must be after terminal init, SDL library changes signal handlers */
3133 os_setup_signal_handling();
3137 current_machine
= machine
;
3139 /* init USB devices */
3141 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3145 /* init generic devices */
3146 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3149 net_check_clients();
3151 /* just use the first displaystate for the moment */
3152 ds
= get_displaystate();
3156 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3157 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3158 display_type
= DT_SDL
;
3159 #elif defined(CONFIG_VNC)
3160 vnc_display
= "localhost:0,to=99";
3163 display_type
= DT_NONE
;
3168 /* init local displays */
3169 switch (display_type
) {
3172 #if defined(CONFIG_CURSES)
3174 curses_display_init(ds
, full_screen
);
3177 #if defined(CONFIG_SDL)
3179 sdl_display_init(ds
, full_screen
, no_frame
);
3181 #elif defined(CONFIG_COCOA)
3183 cocoa_display_init(ds
, full_screen
);
3191 /* init remote displays */
3193 vnc_display_init(ds
);
3194 if (vnc_display_open(ds
, vnc_display
) < 0)
3197 if (show_vnc_port
) {
3198 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3203 if (using_spice
&& !qxl_enabled
) {
3204 qemu_spice_display_init(ds
);
3210 dcl
= ds
->listeners
;
3211 while (dcl
!= NULL
) {
3212 if (dcl
->dpy_refresh
!= NULL
) {
3213 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3214 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3219 if (ds
->gui_timer
== NULL
) {
3220 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3221 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3223 text_consoles_set_display(ds
);
3225 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3226 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3231 qdev_machine_creation_done();
3233 if (rom_load_all() != 0) {
3234 fprintf(stderr
, "rom loading failed\n");
3238 /* TODO: once all bus devices are qdevified, this should be done
3239 * when bus is created by qdev.c */
3240 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3241 qemu_run_machine_init_done_notifiers();
3243 qemu_system_reset();
3245 if (load_vmstate(loadvm
) < 0) {
3251 int ret
= qemu_start_incoming_migration(incoming
);
3253 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3257 } else if (autostart
) {