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
, shutdown_signal
= -1;
1219 static pid_t shutdown_pid
;
1220 static int powerdown_requested
;
1221 static int debug_requested
;
1222 static int vmstop_requested
;
1224 int qemu_shutdown_requested(void)
1226 int r
= shutdown_requested
;
1227 shutdown_requested
= 0;
1231 void qemu_kill_report(void)
1233 if (shutdown_signal
!= -1) {
1234 fprintf(stderr
, "Got signal %d from pid %d\n",
1235 shutdown_signal
, shutdown_pid
);
1236 shutdown_signal
= -1;
1240 int qemu_reset_requested(void)
1242 int r
= reset_requested
;
1243 reset_requested
= 0;
1247 int qemu_powerdown_requested(void)
1249 int r
= powerdown_requested
;
1250 powerdown_requested
= 0;
1254 static int qemu_debug_requested(void)
1256 int r
= debug_requested
;
1257 debug_requested
= 0;
1261 static int qemu_vmstop_requested(void)
1263 int r
= vmstop_requested
;
1264 vmstop_requested
= 0;
1268 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1270 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1273 re
->opaque
= opaque
;
1274 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1277 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1281 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1282 if (re
->func
== func
&& re
->opaque
== opaque
) {
1283 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1290 void qemu_system_reset(void)
1292 QEMUResetEntry
*re
, *nre
;
1294 /* reset all devices */
1295 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1296 re
->func(re
->opaque
);
1298 monitor_protocol_event(QEVENT_RESET
, NULL
);
1299 cpu_synchronize_all_post_reset();
1302 void qemu_system_reset_request(void)
1305 shutdown_requested
= 1;
1307 reset_requested
= 1;
1310 qemu_notify_event();
1313 void qemu_system_killed(int signal
, pid_t pid
)
1315 shutdown_signal
= signal
;
1317 qemu_system_shutdown_request();
1320 void qemu_system_shutdown_request(void)
1322 shutdown_requested
= 1;
1323 qemu_notify_event();
1326 void qemu_system_powerdown_request(void)
1328 powerdown_requested
= 1;
1329 qemu_notify_event();
1332 void qemu_system_debug_request(void)
1334 debug_requested
= 1;
1335 qemu_notify_event();
1338 void qemu_system_vmstop_request(int reason
)
1340 vmstop_requested
= reason
;
1341 qemu_notify_event();
1344 void main_loop_wait(int nonblocking
)
1346 IOHandlerRecord
*ioh
;
1347 fd_set rfds
, wfds
, xfds
;
1355 timeout
= qemu_calculate_timeout();
1356 qemu_bh_update_timeout(&timeout
);
1359 os_host_main_loop_wait(&timeout
);
1361 /* poll any events */
1362 /* XXX: separate device handlers from system ones */
1367 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1371 (!ioh
->fd_read_poll
||
1372 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1373 FD_SET(ioh
->fd
, &rfds
);
1377 if (ioh
->fd_write
) {
1378 FD_SET(ioh
->fd
, &wfds
);
1384 tv
.tv_sec
= timeout
/ 1000;
1385 tv
.tv_usec
= (timeout
% 1000) * 1000;
1387 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1389 qemu_mutex_unlock_iothread();
1390 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1391 qemu_mutex_lock_iothread();
1393 IOHandlerRecord
*pioh
;
1395 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1396 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1397 ioh
->fd_read(ioh
->opaque
);
1399 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1400 ioh
->fd_write(ioh
->opaque
);
1403 /* Do this last in case read/write handlers marked it for deletion */
1405 QLIST_REMOVE(ioh
, next
);
1411 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1413 qemu_run_all_timers();
1415 /* Check bottom-halves last in case any of the earlier events triggered
1421 #ifndef CONFIG_IOTHREAD
1422 static int vm_request_pending(void)
1424 return powerdown_requested
||
1426 shutdown_requested
||
1432 qemu_irq qemu_system_powerdown
;
1434 static void main_loop(void)
1436 bool nonblocking
= false;
1437 #ifdef CONFIG_PROFILER
1442 qemu_main_loop_start();
1445 #ifndef CONFIG_IOTHREAD
1446 nonblocking
= cpu_exec_all();
1447 if (vm_request_pending()) {
1451 #ifdef CONFIG_PROFILER
1452 ti
= profile_getclock();
1454 main_loop_wait(nonblocking
);
1455 #ifdef CONFIG_PROFILER
1456 dev_time
+= profile_getclock() - ti
;
1459 if (qemu_debug_requested()) {
1460 vm_stop(VMSTOP_DEBUG
);
1462 if (qemu_shutdown_requested()) {
1464 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1466 vm_stop(VMSTOP_SHUTDOWN
);
1471 if (qemu_reset_requested()) {
1473 cpu_synchronize_all_states();
1474 qemu_system_reset();
1477 if (qemu_powerdown_requested()) {
1478 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1479 qemu_irq_raise(qemu_system_powerdown
);
1481 if ((r
= qemu_vmstop_requested())) {
1489 static void version(void)
1491 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1494 static void help(int exitcode
)
1496 const char *options_help
=
1497 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1499 #define DEFHEADING(text) stringify(text) "\n"
1500 #include "qemu-options.def"
1506 printf("usage: %s [options] [disk_image]\n"
1508 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1511 "During emulation, the following keys are useful:\n"
1512 "ctrl-alt-f toggle full screen\n"
1513 "ctrl-alt-n switch to virtual console 'n'\n"
1514 "ctrl-alt toggle mouse and keyboard grab\n"
1516 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1522 #define HAS_ARG 0x0001
1524 typedef struct QEMUOption
{
1531 static const QEMUOption qemu_options
[] = {
1532 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1533 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1534 { option, opt_arg, opt_enum, arch_mask },
1535 #define DEFHEADING(text)
1536 #include "qemu-options.def"
1542 static void select_vgahw (const char *p
)
1547 vga_interface_type
= VGA_NONE
;
1548 if (strstart(p
, "std", &opts
)) {
1549 vga_interface_type
= VGA_STD
;
1550 } else if (strstart(p
, "cirrus", &opts
)) {
1551 vga_interface_type
= VGA_CIRRUS
;
1552 } else if (strstart(p
, "vmware", &opts
)) {
1553 vga_interface_type
= VGA_VMWARE
;
1554 } else if (strstart(p
, "xenfb", &opts
)) {
1555 vga_interface_type
= VGA_XENFB
;
1556 } else if (strstart(p
, "qxl", &opts
)) {
1557 vga_interface_type
= VGA_QXL
;
1558 } else if (!strstart(p
, "none", &opts
)) {
1560 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1564 const char *nextopt
;
1566 if (strstart(opts
, ",retrace=", &nextopt
)) {
1568 if (strstart(opts
, "dumb", &nextopt
))
1569 vga_retrace_method
= VGA_RETRACE_DUMB
;
1570 else if (strstart(opts
, "precise", &nextopt
))
1571 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1572 else goto invalid_vga
;
1573 } else goto invalid_vga
;
1578 static DisplayType
select_display(const char *p
)
1581 DisplayType display
= DT_DEFAULT
;
1583 if (strstart(p
, "sdl", &opts
)) {
1587 const char *nextopt
;
1589 if (strstart(opts
, ",frame=", &nextopt
)) {
1591 if (strstart(opts
, "on", &nextopt
)) {
1593 } else if (strstart(opts
, "off", &nextopt
)) {
1596 goto invalid_sdl_args
;
1598 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1600 if (strstart(opts
, "on", &nextopt
)) {
1602 } else if (strstart(opts
, "off", &nextopt
)) {
1605 goto invalid_sdl_args
;
1607 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1609 if (strstart(opts
, "on", &nextopt
)) {
1611 } else if (strstart(opts
, "off", &nextopt
)) {
1614 goto invalid_sdl_args
;
1616 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1618 if (strstart(opts
, "on", &nextopt
)) {
1620 } else if (strstart(opts
, "off", &nextopt
)) {
1623 goto invalid_sdl_args
;
1627 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1633 fprintf(stderr
, "SDL support is disabled\n");
1636 } else if (strstart(p
, "vnc", &opts
)) {
1641 const char *nextopt
;
1643 if (strstart(opts
, "=", &nextopt
)) {
1644 vnc_display
= nextopt
;
1648 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1652 fprintf(stderr
, "VNC support is disabled\n");
1655 } else if (strstart(p
, "curses", &opts
)) {
1656 #ifdef CONFIG_CURSES
1657 display
= DT_CURSES
;
1659 fprintf(stderr
, "Curses support is disabled\n");
1662 } else if (strstart(p
, "none", &opts
)) {
1665 fprintf(stderr
, "Unknown display type: %s\n", p
);
1672 static int balloon_parse(const char *arg
)
1676 if (strcmp(arg
, "none") == 0) {
1680 if (!strncmp(arg
, "virtio", 6)) {
1681 if (arg
[6] == ',') {
1682 /* have params -> parse them */
1683 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1687 /* create empty opts */
1688 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1690 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1697 char *qemu_find_file(int type
, const char *name
)
1703 /* If name contains path separators then try it as a straight path. */
1704 if ((strchr(name
, '/') || strchr(name
, '\\'))
1705 && access(name
, R_OK
) == 0) {
1706 return qemu_strdup(name
);
1709 case QEMU_FILE_TYPE_BIOS
:
1712 case QEMU_FILE_TYPE_KEYMAP
:
1713 subdir
= "keymaps/";
1718 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1719 buf
= qemu_mallocz(len
);
1720 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1721 if (access(buf
, R_OK
)) {
1728 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1730 return qdev_device_help(opts
);
1733 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1737 dev
= qdev_device_add(opts
);
1743 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1745 CharDriverState
*chr
;
1747 chr
= qemu_chr_open_opts(opts
, NULL
);
1753 #ifdef CONFIG_VIRTFS
1754 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1757 ret
= qemu_fsdev_add(opts
);
1763 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1765 CharDriverState
*chr
;
1766 const char *chardev
;
1770 mode
= qemu_opt_get(opts
, "mode");
1774 if (strcmp(mode
, "readline") == 0) {
1775 flags
= MONITOR_USE_READLINE
;
1776 } else if (strcmp(mode
, "control") == 0) {
1777 flags
= MONITOR_USE_CONTROL
;
1779 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1783 if (qemu_opt_get_bool(opts
, "pretty", 0))
1784 flags
|= MONITOR_USE_PRETTY
;
1786 if (qemu_opt_get_bool(opts
, "default", 0))
1787 flags
|= MONITOR_IS_DEFAULT
;
1789 chardev
= qemu_opt_get(opts
, "chardev");
1790 chr
= qemu_chr_find(chardev
);
1792 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1796 monitor_init(chr
, flags
);
1800 static void monitor_parse(const char *optarg
, const char *mode
)
1802 static int monitor_device_index
= 0;
1808 if (strstart(optarg
, "chardev:", &p
)) {
1809 snprintf(label
, sizeof(label
), "%s", p
);
1811 snprintf(label
, sizeof(label
), "compat_monitor%d",
1812 monitor_device_index
);
1813 if (monitor_device_index
== 0) {
1816 opts
= qemu_chr_parse_compat(label
, optarg
);
1818 fprintf(stderr
, "parse error: %s\n", optarg
);
1823 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1825 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1828 qemu_opt_set(opts
, "mode", mode
);
1829 qemu_opt_set(opts
, "chardev", label
);
1831 qemu_opt_set(opts
, "default", "on");
1832 monitor_device_index
++;
1835 struct device_config
{
1837 DEV_USB
, /* -usbdevice */
1839 DEV_SERIAL
, /* -serial */
1840 DEV_PARALLEL
, /* -parallel */
1841 DEV_VIRTCON
, /* -virtioconsole */
1842 DEV_DEBUGCON
, /* -debugcon */
1844 const char *cmdline
;
1845 QTAILQ_ENTRY(device_config
) next
;
1847 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1849 static void add_device_config(int type
, const char *cmdline
)
1851 struct device_config
*conf
;
1853 conf
= qemu_mallocz(sizeof(*conf
));
1855 conf
->cmdline
= cmdline
;
1856 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1859 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1861 struct device_config
*conf
;
1864 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1865 if (conf
->type
!= type
)
1867 rc
= func(conf
->cmdline
);
1874 static int serial_parse(const char *devname
)
1876 static int index
= 0;
1879 if (strcmp(devname
, "none") == 0)
1881 if (index
== MAX_SERIAL_PORTS
) {
1882 fprintf(stderr
, "qemu: too many serial ports\n");
1885 snprintf(label
, sizeof(label
), "serial%d", index
);
1886 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1887 if (!serial_hds
[index
]) {
1888 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1889 devname
, strerror(errno
));
1896 static int parallel_parse(const char *devname
)
1898 static int index
= 0;
1901 if (strcmp(devname
, "none") == 0)
1903 if (index
== MAX_PARALLEL_PORTS
) {
1904 fprintf(stderr
, "qemu: too many parallel ports\n");
1907 snprintf(label
, sizeof(label
), "parallel%d", index
);
1908 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1909 if (!parallel_hds
[index
]) {
1910 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1911 devname
, strerror(errno
));
1918 static int virtcon_parse(const char *devname
)
1920 QemuOptsList
*device
= qemu_find_opts("device");
1921 static int index
= 0;
1923 QemuOpts
*bus_opts
, *dev_opts
;
1925 if (strcmp(devname
, "none") == 0)
1927 if (index
== MAX_VIRTIO_CONSOLES
) {
1928 fprintf(stderr
, "qemu: too many virtio consoles\n");
1932 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1933 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1935 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1936 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1938 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1939 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1940 if (!virtcon_hds
[index
]) {
1941 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1942 devname
, strerror(errno
));
1945 qemu_opt_set(dev_opts
, "chardev", label
);
1951 static int debugcon_parse(const char *devname
)
1955 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1958 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1960 fprintf(stderr
, "qemu: already have a debugcon device\n");
1963 qemu_opt_set(opts
, "driver", "isa-debugcon");
1964 qemu_opt_set(opts
, "chardev", "debugcon");
1968 void qemu_add_exit_notifier(Notifier
*notify
)
1970 notifier_list_add(&exit_notifiers
, notify
);
1973 void qemu_remove_exit_notifier(Notifier
*notify
)
1975 notifier_list_remove(&exit_notifiers
, notify
);
1978 static void qemu_run_exit_notifiers(void)
1980 notifier_list_notify(&exit_notifiers
);
1983 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1985 notifier_list_add(&machine_init_done_notifiers
, notify
);
1988 static void qemu_run_machine_init_done_notifiers(void)
1990 notifier_list_notify(&machine_init_done_notifiers
);
1993 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1994 const char **poptarg
, int *poptind
)
1996 const QEMUOption
*popt
;
1997 int optind
= *poptind
;
1998 char *r
= argv
[optind
];
2001 loc_set_cmdline(argv
, optind
, 1);
2003 /* Treat --foo the same as -foo. */
2006 popt
= qemu_options
;
2009 error_report("invalid option");
2012 if (!strcmp(popt
->name
, r
+ 1))
2016 if (popt
->flags
& HAS_ARG
) {
2017 if (optind
>= argc
) {
2018 error_report("requires an argument");
2021 optarg
= argv
[optind
++];
2022 loc_set_cmdline(argv
, optind
- 2, 2);
2033 int main(int argc
, char **argv
, char **envp
)
2035 const char *gdbstub_dev
= NULL
;
2037 int snapshot
, linux_boot
;
2038 const char *icount_option
= NULL
;
2039 const char *initrd_filename
;
2040 const char *kernel_filename
, *kernel_cmdline
;
2041 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2043 DisplayChangeListener
*dcl
;
2044 int cyls
, heads
, secs
, translation
;
2045 QemuOpts
*hda_opts
= NULL
, *opts
;
2046 QemuOptsList
*olist
;
2049 const char *loadvm
= NULL
;
2050 QEMUMachine
*machine
;
2051 const char *cpu_model
;
2053 const char *pid_file
= NULL
;
2054 const char *incoming
= NULL
;
2056 int show_vnc_port
= 0;
2059 const char *trace_file
= NULL
;
2061 atexit(qemu_run_exit_notifiers
);
2062 error_set_progname(argv
[0]);
2066 qemu_cache_utils_init(envp
);
2068 QLIST_INIT (&vm_change_state_head
);
2069 os_setup_early_signal_handling();
2071 module_call_init(MODULE_INIT_MACHINE
);
2072 machine
= find_default_machine();
2074 initrd_filename
= NULL
;
2077 kernel_filename
= NULL
;
2078 kernel_cmdline
= "";
2079 cyls
= heads
= secs
= 0;
2080 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2082 for (i
= 0; i
< MAX_NODES
; i
++) {
2084 node_cpumask
[i
] = 0;
2093 /* first pass of option parsing */
2095 while (optind
< argc
) {
2096 if (argv
[optind
][0] != '-') {
2101 const QEMUOption
*popt
;
2103 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2104 switch (popt
->index
) {
2105 case QEMU_OPTION_nodefconfig
:
2115 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2116 if (ret
< 0 && ret
!= -ENOENT
) {
2120 ret
= qemu_read_config_file(arch_config_name
);
2121 if (ret
< 0 && ret
!= -ENOENT
) {
2127 /* second pass of option parsing */
2132 if (argv
[optind
][0] != '-') {
2133 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2135 const QEMUOption
*popt
;
2137 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2138 if (!(popt
->arch_mask
& arch_type
)) {
2139 printf("Option %s not supported for this target\n", popt
->name
);
2142 switch(popt
->index
) {
2144 machine
= find_machine(optarg
);
2147 printf("Supported machines are:\n");
2148 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2150 printf("%-10s %s (alias of %s)\n",
2151 m
->alias
, m
->desc
, m
->name
);
2152 printf("%-10s %s%s\n",
2154 m
->is_default
? " (default)" : "");
2156 exit(*optarg
!= '?');
2159 case QEMU_OPTION_cpu
:
2160 /* hw initialization will check this */
2161 if (*optarg
== '?') {
2162 list_cpus(stdout
, &fprintf
, optarg
);
2168 case QEMU_OPTION_initrd
:
2169 initrd_filename
= optarg
;
2171 case QEMU_OPTION_hda
:
2175 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2177 snprintf(buf
, sizeof(buf
),
2178 "%s,cyls=%d,heads=%d,secs=%d%s",
2179 HD_OPTS
, cyls
, heads
, secs
,
2180 translation
== BIOS_ATA_TRANSLATION_LBA
?
2182 translation
== BIOS_ATA_TRANSLATION_NONE
?
2183 ",trans=none" : "");
2184 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2187 case QEMU_OPTION_hdb
:
2188 case QEMU_OPTION_hdc
:
2189 case QEMU_OPTION_hdd
:
2190 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2193 case QEMU_OPTION_drive
:
2196 case QEMU_OPTION_set
:
2197 if (qemu_set_option(optarg
) != 0)
2200 case QEMU_OPTION_global
:
2201 if (qemu_global_option(optarg
) != 0)
2204 case QEMU_OPTION_mtdblock
:
2205 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2207 case QEMU_OPTION_sd
:
2208 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2210 case QEMU_OPTION_pflash
:
2211 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2213 case QEMU_OPTION_snapshot
:
2216 case QEMU_OPTION_hdachs
:
2220 cyls
= strtol(p
, (char **)&p
, 0);
2221 if (cyls
< 1 || cyls
> 16383)
2226 heads
= strtol(p
, (char **)&p
, 0);
2227 if (heads
< 1 || heads
> 16)
2232 secs
= strtol(p
, (char **)&p
, 0);
2233 if (secs
< 1 || secs
> 63)
2237 if (!strcmp(p
, "none"))
2238 translation
= BIOS_ATA_TRANSLATION_NONE
;
2239 else if (!strcmp(p
, "lba"))
2240 translation
= BIOS_ATA_TRANSLATION_LBA
;
2241 else if (!strcmp(p
, "auto"))
2242 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2245 } else if (*p
!= '\0') {
2247 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2250 if (hda_opts
!= NULL
) {
2252 snprintf(num
, sizeof(num
), "%d", cyls
);
2253 qemu_opt_set(hda_opts
, "cyls", num
);
2254 snprintf(num
, sizeof(num
), "%d", heads
);
2255 qemu_opt_set(hda_opts
, "heads", num
);
2256 snprintf(num
, sizeof(num
), "%d", secs
);
2257 qemu_opt_set(hda_opts
, "secs", num
);
2258 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2259 qemu_opt_set(hda_opts
, "trans", "lba");
2260 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2261 qemu_opt_set(hda_opts
, "trans", "none");
2265 case QEMU_OPTION_numa
:
2266 if (nb_numa_nodes
>= MAX_NODES
) {
2267 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2272 case QEMU_OPTION_display
:
2273 display_type
= select_display(optarg
);
2275 case QEMU_OPTION_nographic
:
2276 display_type
= DT_NOGRAPHIC
;
2278 case QEMU_OPTION_curses
:
2279 #ifdef CONFIG_CURSES
2280 display_type
= DT_CURSES
;
2282 fprintf(stderr
, "Curses support is disabled\n");
2286 case QEMU_OPTION_portrait
:
2289 case QEMU_OPTION_kernel
:
2290 kernel_filename
= optarg
;
2292 case QEMU_OPTION_append
:
2293 kernel_cmdline
= optarg
;
2295 case QEMU_OPTION_cdrom
:
2296 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2298 case QEMU_OPTION_boot
:
2300 static const char * const params
[] = {
2301 "order", "once", "menu", NULL
2303 char buf
[sizeof(boot_devices
)];
2304 char *standard_boot_devices
;
2307 if (!strchr(optarg
, '=')) {
2309 pstrcpy(buf
, sizeof(buf
), optarg
);
2310 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2312 "qemu: unknown boot parameter '%s' in '%s'\n",
2318 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2319 validate_bootdevices(buf
);
2320 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2323 if (get_param_value(buf
, sizeof(buf
),
2325 validate_bootdevices(buf
);
2326 standard_boot_devices
= qemu_strdup(boot_devices
);
2327 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2328 qemu_register_reset(restore_boot_devices
,
2329 standard_boot_devices
);
2331 if (get_param_value(buf
, sizeof(buf
),
2333 if (!strcmp(buf
, "on")) {
2335 } else if (!strcmp(buf
, "off")) {
2339 "qemu: invalid option value '%s'\n",
2347 case QEMU_OPTION_fda
:
2348 case QEMU_OPTION_fdb
:
2349 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2352 case QEMU_OPTION_no_fd_bootchk
:
2355 case QEMU_OPTION_netdev
:
2356 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2360 case QEMU_OPTION_net
:
2361 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2366 case QEMU_OPTION_tftp
:
2367 legacy_tftp_prefix
= optarg
;
2369 case QEMU_OPTION_bootp
:
2370 legacy_bootp_filename
= optarg
;
2372 case QEMU_OPTION_redir
:
2373 if (net_slirp_redir(optarg
) < 0)
2377 case QEMU_OPTION_bt
:
2378 add_device_config(DEV_BT
, optarg
);
2380 case QEMU_OPTION_audio_help
:
2381 if (!(audio_available())) {
2382 printf("Option %s not supported for this target\n", popt
->name
);
2388 case QEMU_OPTION_soundhw
:
2389 if (!(audio_available())) {
2390 printf("Option %s not supported for this target\n", popt
->name
);
2393 select_soundhw (optarg
);
2398 case QEMU_OPTION_version
:
2402 case QEMU_OPTION_m
: {
2405 value
= strtosz(optarg
, NULL
);
2407 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2411 /* On 32-bit hosts, QEMU is limited by virtual address space */
2412 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2413 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2416 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2417 fprintf(stderr
, "qemu: ram size too large\n");
2423 case QEMU_OPTION_mempath
:
2427 case QEMU_OPTION_mem_prealloc
:
2432 set_cpu_log(optarg
);
2435 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2437 case QEMU_OPTION_gdb
:
2438 gdbstub_dev
= optarg
;
2443 case QEMU_OPTION_bios
:
2446 case QEMU_OPTION_singlestep
:
2453 keyboard_layout
= optarg
;
2455 case QEMU_OPTION_localtime
:
2458 case QEMU_OPTION_vga
:
2459 select_vgahw (optarg
);
2466 w
= strtol(p
, (char **)&p
, 10);
2469 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2475 h
= strtol(p
, (char **)&p
, 10);
2480 depth
= strtol(p
, (char **)&p
, 10);
2481 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2482 depth
!= 24 && depth
!= 32)
2484 } else if (*p
== '\0') {
2485 depth
= graphic_depth
;
2492 graphic_depth
= depth
;
2495 case QEMU_OPTION_echr
:
2498 term_escape_char
= strtol(optarg
, &r
, 0);
2500 printf("Bad argument to echr\n");
2503 case QEMU_OPTION_monitor
:
2504 monitor_parse(optarg
, "readline");
2505 default_monitor
= 0;
2507 case QEMU_OPTION_qmp
:
2508 monitor_parse(optarg
, "control");
2509 default_monitor
= 0;
2511 case QEMU_OPTION_mon
:
2512 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2516 default_monitor
= 0;
2518 case QEMU_OPTION_chardev
:
2519 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2524 case QEMU_OPTION_fsdev
:
2525 olist
= qemu_find_opts("fsdev");
2527 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2530 opts
= qemu_opts_parse(olist
, optarg
, 1);
2532 fprintf(stderr
, "parse error: %s\n", optarg
);
2536 case QEMU_OPTION_virtfs
: {
2537 char *arg_fsdev
= NULL
;
2538 char *arg_9p
= NULL
;
2541 olist
= qemu_find_opts("virtfs");
2543 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2546 opts
= qemu_opts_parse(olist
, optarg
, 1);
2548 fprintf(stderr
, "parse error: %s\n", optarg
);
2552 if (qemu_opt_get(opts
, "fstype") == NULL
||
2553 qemu_opt_get(opts
, "mount_tag") == NULL
||
2554 qemu_opt_get(opts
, "path") == NULL
||
2555 qemu_opt_get(opts
, "security_model") == NULL
) {
2556 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2557 "security_model=[mapped|passthrough|none],"
2562 len
= strlen(",id=,path=,security_model=");
2563 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2564 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2565 len
+= strlen(qemu_opt_get(opts
, "path"));
2566 len
+= strlen(qemu_opt_get(opts
, "security_model"));
2567 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2569 snprintf(arg_fsdev
, (len
+ 1) * sizeof(*arg_fsdev
),
2570 "%s,id=%s,path=%s,security_model=%s",
2571 qemu_opt_get(opts
, "fstype"),
2572 qemu_opt_get(opts
, "mount_tag"),
2573 qemu_opt_get(opts
, "path"),
2574 qemu_opt_get(opts
, "security_model"));
2576 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2577 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2578 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2580 snprintf(arg_9p
, (len
+ 1) * sizeof(*arg_9p
),
2581 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2582 qemu_opt_get(opts
, "mount_tag"),
2583 qemu_opt_get(opts
, "mount_tag"));
2585 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev
, 1)) {
2586 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2590 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p
, 1)) {
2591 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2595 qemu_free(arg_fsdev
);
2599 case QEMU_OPTION_serial
:
2600 add_device_config(DEV_SERIAL
, optarg
);
2602 if (strncmp(optarg
, "mon:", 4) == 0) {
2603 default_monitor
= 0;
2606 case QEMU_OPTION_watchdog
:
2609 "qemu: only one watchdog option may be given\n");
2614 case QEMU_OPTION_watchdog_action
:
2615 if (select_watchdog_action(optarg
) == -1) {
2616 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2620 case QEMU_OPTION_virtiocon
:
2621 add_device_config(DEV_VIRTCON
, optarg
);
2622 default_virtcon
= 0;
2623 if (strncmp(optarg
, "mon:", 4) == 0) {
2624 default_monitor
= 0;
2627 case QEMU_OPTION_parallel
:
2628 add_device_config(DEV_PARALLEL
, optarg
);
2629 default_parallel
= 0;
2630 if (strncmp(optarg
, "mon:", 4) == 0) {
2631 default_monitor
= 0;
2634 case QEMU_OPTION_debugcon
:
2635 add_device_config(DEV_DEBUGCON
, optarg
);
2637 case QEMU_OPTION_loadvm
:
2640 case QEMU_OPTION_full_screen
:
2644 case QEMU_OPTION_no_frame
:
2647 case QEMU_OPTION_alt_grab
:
2650 case QEMU_OPTION_ctrl_grab
:
2653 case QEMU_OPTION_no_quit
:
2656 case QEMU_OPTION_sdl
:
2657 display_type
= DT_SDL
;
2660 case QEMU_OPTION_no_frame
:
2661 case QEMU_OPTION_alt_grab
:
2662 case QEMU_OPTION_ctrl_grab
:
2663 case QEMU_OPTION_no_quit
:
2664 case QEMU_OPTION_sdl
:
2665 fprintf(stderr
, "SDL support is disabled\n");
2668 case QEMU_OPTION_pidfile
:
2671 case QEMU_OPTION_win2k_hack
:
2672 win2k_install_hack
= 1;
2674 case QEMU_OPTION_rtc_td_hack
:
2677 case QEMU_OPTION_acpitable
:
2678 do_acpitable_option(optarg
);
2680 case QEMU_OPTION_smbios
:
2681 do_smbios_option(optarg
);
2683 case QEMU_OPTION_enable_kvm
:
2686 case QEMU_OPTION_usb
:
2689 case QEMU_OPTION_usbdevice
:
2691 add_device_config(DEV_USB
, optarg
);
2693 case QEMU_OPTION_device
:
2694 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2698 case QEMU_OPTION_smp
:
2701 fprintf(stderr
, "Invalid number of CPUs\n");
2704 if (max_cpus
< smp_cpus
) {
2705 fprintf(stderr
, "maxcpus must be equal to or greater than "
2709 if (max_cpus
> 255) {
2710 fprintf(stderr
, "Unsupported number of maxcpus\n");
2714 case QEMU_OPTION_vnc
:
2717 vnc_display
= optarg
;
2719 fprintf(stderr
, "VNC support is disabled\n");
2723 case QEMU_OPTION_no_acpi
:
2726 case QEMU_OPTION_no_hpet
:
2729 case QEMU_OPTION_balloon
:
2730 if (balloon_parse(optarg
) < 0) {
2731 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2735 case QEMU_OPTION_no_reboot
:
2738 case QEMU_OPTION_no_shutdown
:
2741 case QEMU_OPTION_show_cursor
:
2744 case QEMU_OPTION_uuid
:
2745 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2746 fprintf(stderr
, "Fail to parse UUID string."
2747 " Wrong format.\n");
2751 case QEMU_OPTION_option_rom
:
2752 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2753 fprintf(stderr
, "Too many option ROMs\n");
2756 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2757 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2758 option_rom
[nb_option_roms
].bootindex
=
2759 qemu_opt_get_number(opts
, "bootindex", -1);
2760 if (!option_rom
[nb_option_roms
].name
) {
2761 fprintf(stderr
, "Option ROM file is not specified\n");
2766 case QEMU_OPTION_semihosting
:
2767 semihosting_enabled
= 1;
2769 case QEMU_OPTION_name
:
2770 qemu_name
= qemu_strdup(optarg
);
2772 char *p
= strchr(qemu_name
, ',');
2775 if (strncmp(p
, "process=", 8)) {
2776 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2780 os_set_proc_name(p
);
2784 case QEMU_OPTION_prom_env
:
2785 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2786 fprintf(stderr
, "Too many prom variables\n");
2789 prom_envs
[nb_prom_envs
] = optarg
;
2792 case QEMU_OPTION_old_param
:
2795 case QEMU_OPTION_clock
:
2796 configure_alarms(optarg
);
2798 case QEMU_OPTION_startdate
:
2799 configure_rtc_date_offset(optarg
, 1);
2801 case QEMU_OPTION_rtc
:
2802 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2806 configure_rtc(opts
);
2808 case QEMU_OPTION_tb_size
:
2809 tb_size
= strtol(optarg
, NULL
, 0);
2813 case QEMU_OPTION_icount
:
2814 icount_option
= optarg
;
2816 case QEMU_OPTION_incoming
:
2818 incoming_expected
= true;
2820 case QEMU_OPTION_nodefaults
:
2822 default_parallel
= 0;
2823 default_virtcon
= 0;
2824 default_monitor
= 0;
2831 case QEMU_OPTION_xen_domid
:
2832 if (!(xen_available())) {
2833 printf("Option %s not supported for this target\n", popt
->name
);
2836 xen_domid
= atoi(optarg
);
2838 case QEMU_OPTION_xen_create
:
2839 if (!(xen_available())) {
2840 printf("Option %s not supported for this target\n", popt
->name
);
2843 xen_mode
= XEN_CREATE
;
2845 case QEMU_OPTION_xen_attach
:
2846 if (!(xen_available())) {
2847 printf("Option %s not supported for this target\n", popt
->name
);
2850 xen_mode
= XEN_ATTACH
;
2852 #ifdef CONFIG_SIMPLE_TRACE
2853 case QEMU_OPTION_trace
:
2854 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2856 trace_file
= qemu_opt_get(opts
, "file");
2860 case QEMU_OPTION_readconfig
:
2862 int ret
= qemu_read_config_file(optarg
);
2864 fprintf(stderr
, "read config %s: %s\n", optarg
,
2870 case QEMU_OPTION_spice
:
2871 olist
= qemu_find_opts("spice");
2873 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2876 opts
= qemu_opts_parse(olist
, optarg
, 0);
2878 fprintf(stderr
, "parse error: %s\n", optarg
);
2882 case QEMU_OPTION_writeconfig
:
2885 if (strcmp(optarg
, "-") == 0) {
2888 fp
= fopen(optarg
, "w");
2890 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2894 qemu_config_write(fp
);
2899 os_parse_cmd_args(popt
->index
, optarg
);
2905 if (!st_init(trace_file
)) {
2906 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2909 /* If no data_dir is specified then try to find it relative to the
2912 data_dir
= os_find_datadir(argv
[0]);
2914 /* If all else fails use the install patch specified when building. */
2916 data_dir
= CONFIG_QEMU_DATADIR
;
2920 * Default to max_cpus = smp_cpus, in case the user doesn't
2921 * specify a max_cpus value.
2924 max_cpus
= smp_cpus
;
2926 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2927 if (smp_cpus
> machine
->max_cpus
) {
2928 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2929 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2934 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
2935 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
2937 if (machine
->no_serial
) {
2940 if (machine
->no_parallel
) {
2941 default_parallel
= 0;
2943 if (!machine
->use_virtcon
) {
2944 default_virtcon
= 0;
2946 if (machine
->no_vga
) {
2949 if (machine
->no_floppy
) {
2952 if (machine
->no_cdrom
) {
2955 if (machine
->no_sdcard
) {
2959 if (display_type
== DT_NOGRAPHIC
) {
2960 if (default_parallel
)
2961 add_device_config(DEV_PARALLEL
, "null");
2962 if (default_serial
&& default_monitor
) {
2963 add_device_config(DEV_SERIAL
, "mon:stdio");
2964 } else if (default_virtcon
&& default_monitor
) {
2965 add_device_config(DEV_VIRTCON
, "mon:stdio");
2968 add_device_config(DEV_SERIAL
, "stdio");
2969 if (default_virtcon
)
2970 add_device_config(DEV_VIRTCON
, "stdio");
2971 if (default_monitor
)
2972 monitor_parse("stdio", "readline");
2976 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2977 if (default_parallel
)
2978 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2979 if (default_monitor
)
2980 monitor_parse("vc:80Cx24C", "readline");
2981 if (default_virtcon
)
2982 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2985 vga_interface_type
= VGA_CIRRUS
;
2989 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
2991 #ifdef CONFIG_VIRTFS
2992 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
2999 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3005 int ret
= kvm_init();
3007 if (!kvm_available()) {
3008 printf("KVM not supported for this target\n");
3010 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3016 if (qemu_init_main_loop()) {
3017 fprintf(stderr
, "qemu_init_main_loop failed\n");
3020 linux_boot
= (kernel_filename
!= NULL
);
3022 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3023 fprintf(stderr
, "-append only allowed with -kernel option\n");
3027 if (!linux_boot
&& initrd_filename
!= NULL
) {
3028 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3032 os_set_line_buffering();
3034 if (init_timer_alarm() < 0) {
3035 fprintf(stderr
, "could not initialize alarm timer\n");
3038 configure_icount(icount_option
);
3040 if (net_init_clients() < 0) {
3044 /* init the bluetooth world */
3045 if (foreach_device_config(DEV_BT
, bt_parse
))
3048 /* init the memory */
3050 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3052 /* init the dynamic translator */
3053 cpu_exec_init_all(tb_size
* 1024 * 1024);
3055 bdrv_init_with_whitelist();
3059 /* open the virtual block devices */
3061 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3062 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3065 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3066 IF_DEFAULT
, 2, CDROM_OPTS
);
3067 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3068 IF_FLOPPY
, 0, FD_OPTS
);
3069 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3072 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3075 if (nb_numa_nodes
> 0) {
3078 if (nb_numa_nodes
> smp_cpus
) {
3079 nb_numa_nodes
= smp_cpus
;
3082 /* If no memory size if given for any node, assume the default case
3083 * and distribute the available memory equally across all nodes
3085 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3086 if (node_mem
[i
] != 0)
3089 if (i
== nb_numa_nodes
) {
3090 uint64_t usedmem
= 0;
3092 /* On Linux, the each node's border has to be 8MB aligned,
3093 * the final node gets the rest.
3095 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3096 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3097 usedmem
+= node_mem
[i
];
3099 node_mem
[i
] = ram_size
- usedmem
;
3102 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3103 if (node_cpumask
[i
] != 0)
3106 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3107 * must cope with this anyway, because there are BIOSes out there in
3108 * real machines which also use this scheme.
3110 if (i
== nb_numa_nodes
) {
3111 for (i
= 0; i
< smp_cpus
; i
++) {
3112 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3117 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3121 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3123 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3125 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3127 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3130 module_call_init(MODULE_INIT_DEVICE
);
3132 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3136 i
= select_watchdog(watchdog
);
3138 exit (i
== 1 ? 1 : 0);
3141 if (machine
->compat_props
) {
3142 qdev_prop_register_global_list(machine
->compat_props
);
3146 machine
->init(ram_size
, boot_devices
,
3147 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3149 cpu_synchronize_all_post_init();
3151 /* must be after terminal init, SDL library changes signal handlers */
3152 os_setup_signal_handling();
3156 current_machine
= machine
;
3158 /* init USB devices */
3160 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3164 /* init generic devices */
3165 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3168 net_check_clients();
3170 /* just use the first displaystate for the moment */
3171 ds
= get_displaystate();
3175 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3176 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3177 display_type
= DT_SDL
;
3178 #elif defined(CONFIG_VNC)
3179 vnc_display
= "localhost:0,to=99";
3182 display_type
= DT_NONE
;
3187 /* init local displays */
3188 switch (display_type
) {
3191 #if defined(CONFIG_CURSES)
3193 curses_display_init(ds
, full_screen
);
3196 #if defined(CONFIG_SDL)
3198 sdl_display_init(ds
, full_screen
, no_frame
);
3200 #elif defined(CONFIG_COCOA)
3202 cocoa_display_init(ds
, full_screen
);
3210 /* init remote displays */
3212 vnc_display_init(ds
);
3213 if (vnc_display_open(ds
, vnc_display
) < 0)
3216 if (show_vnc_port
) {
3217 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3222 if (using_spice
&& !qxl_enabled
) {
3223 qemu_spice_display_init(ds
);
3229 dcl
= ds
->listeners
;
3230 while (dcl
!= NULL
) {
3231 if (dcl
->dpy_refresh
!= NULL
) {
3232 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3233 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3238 if (ds
->gui_timer
== NULL
) {
3239 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3240 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3242 text_consoles_set_display(ds
);
3244 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3245 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3250 qdev_machine_creation_done();
3252 if (rom_load_all() != 0) {
3253 fprintf(stderr
, "rom loading failed\n");
3257 /* TODO: once all bus devices are qdevified, this should be done
3258 * when bus is created by qdev.c */
3259 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3260 qemu_run_machine_init_done_notifiers();
3262 qemu_system_reset();
3264 if (load_vmstate(loadvm
) < 0) {
3270 int ret
= qemu_start_incoming_migration(incoming
);
3272 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3276 } else if (autostart
) {