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;
209 const char *vnc_display
;
210 int acpi_enabled
= 1;
216 int graphic_rotate
= 0;
217 uint8_t irq0override
= 1;
218 const char *watchdog
;
219 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
221 int semihosting_enabled
= 0;
223 const char *qemu_name
;
226 unsigned int nb_prom_envs
= 0;
227 const char *prom_envs
[MAX_PROM_ENVS
];
230 typedef struct FWBootEntry FWBootEntry
;
233 QTAILQ_ENTRY(FWBootEntry
) link
;
239 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
242 uint64_t node_mem
[MAX_NODES
];
243 uint64_t node_cpumask
[MAX_NODES
];
245 static QEMUTimer
*nographic_timer
;
247 uint8_t qemu_uuid
[16];
249 static QEMUBootSetHandler
*boot_set_handler
;
250 static void *boot_set_opaque
;
252 static NotifierList exit_notifiers
=
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
255 static NotifierList machine_init_done_notifiers
=
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
260 enum xen_mode xen_mode
= XEN_EMULATE
;
262 static int default_serial
= 1;
263 static int default_parallel
= 1;
264 static int default_virtcon
= 1;
265 static int default_monitor
= 1;
266 static int default_vga
= 1;
267 static int default_floppy
= 1;
268 static int default_cdrom
= 1;
269 static int default_sdcard
= 1;
275 { .driver
= "isa-serial", .flag
= &default_serial
},
276 { .driver
= "isa-parallel", .flag
= &default_parallel
},
277 { .driver
= "isa-fdc", .flag
= &default_floppy
},
278 { .driver
= "ide-drive", .flag
= &default_cdrom
},
279 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
280 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
281 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
282 { .driver
= "VGA", .flag
= &default_vga
},
283 { .driver
= "cirrus-vga", .flag
= &default_vga
},
284 { .driver
= "vmware-svga", .flag
= &default_vga
},
287 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
289 const char *driver
= qemu_opt_get(opts
, "driver");
294 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
295 if (strcmp(default_list
[i
].driver
, driver
) != 0)
297 *(default_list
[i
].flag
) = 0;
302 /***********************************************************/
303 /* real time host monotonic timer */
305 /***********************************************************/
306 /* host time/date access */
307 void qemu_get_timedate(struct tm
*tm
, int offset
)
314 if (rtc_date_offset
== -1) {
318 ret
= localtime(&ti
);
320 ti
-= rtc_date_offset
;
324 memcpy(tm
, ret
, sizeof(struct tm
));
327 int qemu_timedate_diff(struct tm
*tm
)
331 if (rtc_date_offset
== -1)
333 seconds
= mktimegm(tm
);
335 seconds
= mktime(tm
);
337 seconds
= mktimegm(tm
) + rtc_date_offset
;
339 return seconds
- time(NULL
);
342 void rtc_change_mon_event(struct tm
*tm
)
346 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
347 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
348 qobject_decref(data
);
351 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
353 time_t rtc_start_date
;
356 if (!strcmp(startdate
, "now") && legacy
) {
357 rtc_date_offset
= -1;
359 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
367 } else if (sscanf(startdate
, "%d-%d-%d",
379 rtc_start_date
= mktimegm(&tm
);
380 if (rtc_start_date
== -1) {
382 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
383 "'2006-06-17T16:01:21' or '2006-06-17'\n");
386 rtc_date_offset
= time(NULL
) - rtc_start_date
;
390 static void configure_rtc(QemuOpts
*opts
)
394 value
= qemu_opt_get(opts
, "base");
396 if (!strcmp(value
, "utc")) {
398 } else if (!strcmp(value
, "localtime")) {
401 configure_rtc_date_offset(value
, 0);
404 value
= qemu_opt_get(opts
, "clock");
406 if (!strcmp(value
, "host")) {
407 rtc_clock
= host_clock
;
408 } else if (!strcmp(value
, "vm")) {
409 rtc_clock
= vm_clock
;
411 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
415 value
= qemu_opt_get(opts
, "driftfix");
417 if (!strcmp(value
, "slew")) {
419 } else if (!strcmp(value
, "none")) {
422 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
428 /***********************************************************/
429 /* Bluetooth support */
432 static struct HCIInfo
*hci_table
[MAX_NICS
];
434 static struct bt_vlan_s
{
435 struct bt_scatternet_s net
;
437 struct bt_vlan_s
*next
;
440 /* find or alloc a new bluetooth "VLAN" */
441 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
443 struct bt_vlan_s
**pvlan
, *vlan
;
444 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
448 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
450 pvlan
= &first_bt_vlan
;
451 while (*pvlan
!= NULL
)
452 pvlan
= &(*pvlan
)->next
;
457 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
461 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
466 static struct HCIInfo null_hci
= {
467 .cmd_send
= null_hci_send
,
468 .sco_send
= null_hci_send
,
469 .acl_send
= null_hci_send
,
470 .bdaddr_set
= null_hci_addr_set
,
473 struct HCIInfo
*qemu_next_hci(void)
475 if (cur_hci
== nb_hcis
)
478 return hci_table
[cur_hci
++];
481 static struct HCIInfo
*hci_init(const char *str
)
484 struct bt_scatternet_s
*vlan
= 0;
486 if (!strcmp(str
, "null"))
489 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
491 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
492 else if (!strncmp(str
, "hci", 3)) {
495 if (!strncmp(str
+ 3, ",vlan=", 6)) {
496 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
501 vlan
= qemu_find_bt_vlan(0);
503 return bt_new_hci(vlan
);
506 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
511 static int bt_hci_parse(const char *str
)
516 if (nb_hcis
>= MAX_NICS
) {
517 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
530 bdaddr
.b
[5] = 0x56 + nb_hcis
;
531 hci
->bdaddr_set(hci
, bdaddr
.b
);
533 hci_table
[nb_hcis
++] = hci
;
538 static void bt_vhci_add(int vlan_id
)
540 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
543 fprintf(stderr
, "qemu: warning: adding a VHCI to "
544 "an empty scatternet %i\n", vlan_id
);
546 bt_vhci_init(bt_new_hci(vlan
));
549 static struct bt_device_s
*bt_device_add(const char *opt
)
551 struct bt_scatternet_s
*vlan
;
553 char *endp
= strstr(opt
, ",vlan=");
554 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
557 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
560 vlan_id
= strtol(endp
+ 6, &endp
, 0);
562 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
567 vlan
= qemu_find_bt_vlan(vlan_id
);
570 fprintf(stderr
, "qemu: warning: adding a slave device to "
571 "an empty scatternet %i\n", vlan_id
);
573 if (!strcmp(devname
, "keyboard"))
574 return bt_keyboard_init(vlan
);
576 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
580 static int bt_parse(const char *opt
)
582 const char *endp
, *p
;
585 if (strstart(opt
, "hci", &endp
)) {
586 if (!*endp
|| *endp
== ',') {
588 if (!strstart(endp
, ",vlan=", 0))
591 return bt_hci_parse(opt
);
593 } else if (strstart(opt
, "vhci", &endp
)) {
594 if (!*endp
|| *endp
== ',') {
596 if (strstart(endp
, ",vlan=", &p
)) {
597 vlan
= strtol(p
, (char **) &endp
, 0);
599 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
603 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
612 } else if (strstart(opt
, "device:", &endp
))
613 return !bt_device_add(endp
);
615 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
619 /***********************************************************/
620 /* QEMU Block devices */
622 #define HD_OPTS "media=disk"
623 #define CDROM_OPTS "media=cdrom"
625 #define PFLASH_OPTS ""
629 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
631 int *use_scsi
= opaque
;
633 return drive_init(opts
, *use_scsi
) == NULL
;
636 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
638 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
639 qemu_opt_set(opts
, "snapshot", "on");
644 static void default_drive(int enable
, int snapshot
, int use_scsi
,
645 BlockInterfaceType type
, int index
,
650 if (type
== IF_DEFAULT
) {
651 type
= use_scsi
? IF_SCSI
: IF_IDE
;
654 if (!enable
|| drive_get_by_index(type
, index
)) {
658 opts
= drive_add(type
, index
, NULL
, optstr
);
660 drive_enable_snapshot(opts
, NULL
);
662 if (!drive_init(opts
, use_scsi
)) {
667 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
669 boot_set_handler
= func
;
670 boot_set_opaque
= opaque
;
673 int qemu_boot_set(const char *boot_devices
)
675 if (!boot_set_handler
) {
678 return boot_set_handler(boot_set_opaque
, boot_devices
);
681 static void validate_bootdevices(char *devices
)
683 /* We just do some generic consistency checks */
687 for (p
= devices
; *p
!= '\0'; p
++) {
688 /* Allowed boot devices are:
689 * a-b: floppy disk drives
690 * c-f: IDE disk drives
691 * g-m: machine implementation dependant drives
692 * n-p: network devices
693 * It's up to each machine implementation to check if the given boot
694 * devices match the actual hardware implementation and firmware
697 if (*p
< 'a' || *p
> 'p') {
698 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
701 if (bitmap
& (1 << (*p
- 'a'))) {
702 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
705 bitmap
|= 1 << (*p
- 'a');
709 static void restore_boot_devices(void *opaque
)
711 char *standard_boot_devices
= opaque
;
712 static int first
= 1;
714 /* Restore boot order and remove ourselves after the first boot */
720 qemu_boot_set(standard_boot_devices
);
722 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
723 qemu_free(standard_boot_devices
);
726 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
729 FWBootEntry
*node
, *i
;
735 assert(dev
!= NULL
|| suffix
!= NULL
);
737 node
= qemu_mallocz(sizeof(FWBootEntry
));
738 node
->bootindex
= bootindex
;
739 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
742 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
743 if (i
->bootindex
== bootindex
) {
744 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
746 } else if (i
->bootindex
< bootindex
) {
749 QTAILQ_INSERT_BEFORE(i
, node
, link
);
752 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
756 * This function returns null terminated string that consist of new line
757 * separated device pathes.
759 * memory pointed by "size" is assigned total length of the array in bytes
762 char *get_boot_devices_list(uint32_t *size
)
768 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
769 char *devpath
= NULL
, *bootpath
;
773 devpath
= qdev_get_fw_dev_path(i
->dev
);
777 if (i
->suffix
&& devpath
) {
778 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
780 bootpath
= qemu_malloc(bootpathlen
);
781 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
783 } else if (devpath
) {
786 bootpath
= qemu_strdup(i
->suffix
);
791 list
[total
-1] = '\n';
793 len
= strlen(bootpath
) + 1;
794 list
= qemu_realloc(list
, total
+ len
);
795 memcpy(&list
[total
], bootpath
, len
);
805 static void numa_add(const char *optarg
)
809 unsigned long long value
, endvalue
;
812 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
813 if (!strcmp(option
, "node")) {
814 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
815 nodenr
= nb_numa_nodes
;
817 nodenr
= strtoull(option
, NULL
, 10);
820 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
821 node_mem
[nodenr
] = 0;
824 sval
= strtosz(option
, NULL
);
826 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
829 node_mem
[nodenr
] = sval
;
831 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
832 node_cpumask
[nodenr
] = 0;
834 value
= strtoull(option
, &endptr
, 10);
837 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
839 if (*endptr
== '-') {
840 endvalue
= strtoull(endptr
+1, &endptr
, 10);
841 if (endvalue
>= 63) {
844 "only 63 CPUs in NUMA mode supported.\n");
846 value
= (2ULL << endvalue
) - (1ULL << value
);
848 value
= 1ULL << value
;
851 node_cpumask
[nodenr
] = value
;
858 static void smp_parse(const char *optarg
)
860 int smp
, sockets
= 0, threads
= 0, cores
= 0;
864 smp
= strtoul(optarg
, &endptr
, 10);
865 if (endptr
!= optarg
) {
866 if (*endptr
== ',') {
870 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
871 sockets
= strtoull(option
, NULL
, 10);
872 if (get_param_value(option
, 128, "cores", endptr
) != 0)
873 cores
= strtoull(option
, NULL
, 10);
874 if (get_param_value(option
, 128, "threads", endptr
) != 0)
875 threads
= strtoull(option
, NULL
, 10);
876 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
877 max_cpus
= strtoull(option
, NULL
, 10);
879 /* compute missing values, prefer sockets over cores over threads */
880 if (smp
== 0 || sockets
== 0) {
881 sockets
= sockets
> 0 ? sockets
: 1;
882 cores
= cores
> 0 ? cores
: 1;
883 threads
= threads
> 0 ? threads
: 1;
885 smp
= cores
* threads
* sockets
;
889 threads
= threads
> 0 ? threads
: 1;
890 cores
= smp
/ (sockets
* threads
);
892 threads
= smp
/ (cores
* sockets
);
896 smp_cores
= cores
> 0 ? cores
: 1;
897 smp_threads
= threads
> 0 ? threads
: 1;
902 /***********************************************************/
905 static int usb_device_add(const char *devname
)
908 USBDevice
*dev
= NULL
;
913 /* drivers with .usbdevice_name entry in USBDeviceInfo */
914 dev
= usbdevice_create(devname
);
919 if (strstart(devname
, "host:", &p
)) {
920 dev
= usb_host_device_open(p
);
921 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
922 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
923 bt_new_hci(qemu_find_bt_vlan(0)));
934 static int usb_device_del(const char *devname
)
939 if (strstart(devname
, "host:", &p
))
940 return usb_host_device_close(p
);
945 p
= strchr(devname
, '.');
948 bus_num
= strtoul(devname
, NULL
, 0);
949 addr
= strtoul(p
+ 1, NULL
, 0);
951 return usb_device_delete_addr(bus_num
, addr
);
954 static int usb_parse(const char *cmdline
)
957 r
= usb_device_add(cmdline
);
959 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
964 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
966 const char *devname
= qdict_get_str(qdict
, "devname");
967 if (usb_device_add(devname
) < 0) {
968 error_report("could not add USB device '%s'", devname
);
972 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
974 const char *devname
= qdict_get_str(qdict
, "devname");
975 if (usb_device_del(devname
) < 0) {
976 error_report("could not delete USB device '%s'", devname
);
980 /***********************************************************/
983 static struct pcmcia_socket_entry_s
{
984 PCMCIASocket
*socket
;
985 struct pcmcia_socket_entry_s
*next
;
986 } *pcmcia_sockets
= 0;
988 void pcmcia_socket_register(PCMCIASocket
*socket
)
990 struct pcmcia_socket_entry_s
*entry
;
992 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
993 entry
->socket
= socket
;
994 entry
->next
= pcmcia_sockets
;
995 pcmcia_sockets
= entry
;
998 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1000 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1002 ptr
= &pcmcia_sockets
;
1003 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1004 if (entry
->socket
== socket
) {
1010 void pcmcia_info(Monitor
*mon
)
1012 struct pcmcia_socket_entry_s
*iter
;
1014 if (!pcmcia_sockets
)
1015 monitor_printf(mon
, "No PCMCIA sockets\n");
1017 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1018 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1019 iter
->socket
->attached
? iter
->socket
->card_string
:
1023 /***********************************************************/
1026 typedef struct IOHandlerRecord
{
1028 IOCanReadHandler
*fd_read_poll
;
1030 IOHandler
*fd_write
;
1033 /* temporary data */
1035 QLIST_ENTRY(IOHandlerRecord
) next
;
1038 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1039 QLIST_HEAD_INITIALIZER(io_handlers
);
1042 /* XXX: fd_read_poll should be suppressed, but an API change is
1043 necessary in the character devices to suppress fd_can_read(). */
1044 int qemu_set_fd_handler2(int fd
,
1045 IOCanReadHandler
*fd_read_poll
,
1047 IOHandler
*fd_write
,
1050 IOHandlerRecord
*ioh
;
1052 if (!fd_read
&& !fd_write
) {
1053 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1054 if (ioh
->fd
== fd
) {
1060 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1064 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1065 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1068 ioh
->fd_read_poll
= fd_read_poll
;
1069 ioh
->fd_read
= fd_read
;
1070 ioh
->fd_write
= fd_write
;
1071 ioh
->opaque
= opaque
;
1077 int qemu_set_fd_handler(int fd
,
1079 IOHandler
*fd_write
,
1082 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1085 /***********************************************************/
1086 /* machine registration */
1088 static QEMUMachine
*first_machine
= NULL
;
1089 QEMUMachine
*current_machine
= NULL
;
1091 int qemu_register_machine(QEMUMachine
*m
)
1094 pm
= &first_machine
;
1102 static QEMUMachine
*find_machine(const char *name
)
1106 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1107 if (!strcmp(m
->name
, name
))
1109 if (m
->alias
&& !strcmp(m
->alias
, name
))
1115 static QEMUMachine
*find_default_machine(void)
1119 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1120 if (m
->is_default
) {
1127 /***********************************************************/
1128 /* main execution loop */
1130 static void gui_update(void *opaque
)
1132 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1133 DisplayState
*ds
= opaque
;
1134 DisplayChangeListener
*dcl
= ds
->listeners
;
1136 qemu_flush_coalesced_mmio_buffer();
1139 while (dcl
!= NULL
) {
1140 if (dcl
->gui_timer_interval
&&
1141 dcl
->gui_timer_interval
< interval
)
1142 interval
= dcl
->gui_timer_interval
;
1145 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1148 static void nographic_update(void *opaque
)
1150 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1152 qemu_flush_coalesced_mmio_buffer();
1153 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1156 struct vm_change_state_entry
{
1157 VMChangeStateHandler
*cb
;
1159 QLIST_ENTRY (vm_change_state_entry
) entries
;
1162 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1164 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1167 VMChangeStateEntry
*e
;
1169 e
= qemu_mallocz(sizeof (*e
));
1173 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1177 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1179 QLIST_REMOVE (e
, entries
);
1183 void vm_state_notify(int running
, int reason
)
1185 VMChangeStateEntry
*e
;
1187 trace_vm_state_notify(running
, reason
);
1189 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1190 e
->cb(e
->opaque
, running
, reason
);
1199 vm_state_notify(1, 0);
1201 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1205 /* reset/shutdown handler */
1207 typedef struct QEMUResetEntry
{
1208 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1209 QEMUResetHandler
*func
;
1213 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1214 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1215 static int reset_requested
;
1216 static int shutdown_requested
;
1217 static int powerdown_requested
;
1218 static int debug_requested
;
1219 static int vmstop_requested
;
1221 int qemu_shutdown_requested(void)
1223 int r
= shutdown_requested
;
1224 shutdown_requested
= 0;
1228 int qemu_reset_requested(void)
1230 int r
= reset_requested
;
1231 reset_requested
= 0;
1235 int qemu_powerdown_requested(void)
1237 int r
= powerdown_requested
;
1238 powerdown_requested
= 0;
1242 static int qemu_debug_requested(void)
1244 int r
= debug_requested
;
1245 debug_requested
= 0;
1249 static int qemu_vmstop_requested(void)
1251 int r
= vmstop_requested
;
1252 vmstop_requested
= 0;
1256 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1258 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1261 re
->opaque
= opaque
;
1262 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1265 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1269 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1270 if (re
->func
== func
&& re
->opaque
== opaque
) {
1271 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1278 void qemu_system_reset(void)
1280 QEMUResetEntry
*re
, *nre
;
1282 /* reset all devices */
1283 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1284 re
->func(re
->opaque
);
1286 monitor_protocol_event(QEVENT_RESET
, NULL
);
1287 cpu_synchronize_all_post_reset();
1290 void qemu_system_reset_request(void)
1293 shutdown_requested
= 1;
1295 reset_requested
= 1;
1298 qemu_notify_event();
1301 void qemu_system_shutdown_request(void)
1303 shutdown_requested
= 1;
1304 qemu_notify_event();
1307 void qemu_system_powerdown_request(void)
1309 powerdown_requested
= 1;
1310 qemu_notify_event();
1313 void qemu_system_debug_request(void)
1315 debug_requested
= 1;
1316 qemu_notify_event();
1319 void qemu_system_vmstop_request(int reason
)
1321 vmstop_requested
= reason
;
1322 qemu_notify_event();
1325 void main_loop_wait(int nonblocking
)
1327 IOHandlerRecord
*ioh
;
1328 fd_set rfds
, wfds
, xfds
;
1336 timeout
= qemu_calculate_timeout();
1337 qemu_bh_update_timeout(&timeout
);
1340 os_host_main_loop_wait(&timeout
);
1342 /* poll any events */
1343 /* XXX: separate device handlers from system ones */
1348 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1352 (!ioh
->fd_read_poll
||
1353 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1354 FD_SET(ioh
->fd
, &rfds
);
1358 if (ioh
->fd_write
) {
1359 FD_SET(ioh
->fd
, &wfds
);
1365 tv
.tv_sec
= timeout
/ 1000;
1366 tv
.tv_usec
= (timeout
% 1000) * 1000;
1368 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1370 qemu_mutex_unlock_iothread();
1371 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1372 qemu_mutex_lock_iothread();
1374 IOHandlerRecord
*pioh
;
1376 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1377 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1378 ioh
->fd_read(ioh
->opaque
);
1380 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1381 ioh
->fd_write(ioh
->opaque
);
1384 /* Do this last in case read/write handlers marked it for deletion */
1386 QLIST_REMOVE(ioh
, next
);
1392 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1394 qemu_run_all_timers();
1396 /* Check bottom-halves last in case any of the earlier events triggered
1402 #ifndef CONFIG_IOTHREAD
1403 static int vm_request_pending(void)
1405 return powerdown_requested
||
1407 shutdown_requested
||
1413 qemu_irq qemu_system_powerdown
;
1415 static void main_loop(void)
1417 bool nonblocking
= false;
1418 #ifdef CONFIG_PROFILER
1423 qemu_main_loop_start();
1426 #ifndef CONFIG_IOTHREAD
1427 nonblocking
= cpu_exec_all();
1428 if (vm_request_pending()) {
1432 #ifdef CONFIG_PROFILER
1433 ti
= profile_getclock();
1435 main_loop_wait(nonblocking
);
1436 #ifdef CONFIG_PROFILER
1437 dev_time
+= profile_getclock() - ti
;
1440 if (qemu_debug_requested()) {
1441 vm_stop(VMSTOP_DEBUG
);
1443 if (qemu_shutdown_requested()) {
1444 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1446 vm_stop(VMSTOP_SHUTDOWN
);
1451 if (qemu_reset_requested()) {
1453 cpu_synchronize_all_states();
1454 qemu_system_reset();
1457 if (qemu_powerdown_requested()) {
1458 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1459 qemu_irq_raise(qemu_system_powerdown
);
1461 if ((r
= qemu_vmstop_requested())) {
1469 static void version(void)
1471 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1474 static void help(int exitcode
)
1476 const char *options_help
=
1477 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1479 #define DEFHEADING(text) stringify(text) "\n"
1480 #include "qemu-options.def"
1486 printf("usage: %s [options] [disk_image]\n"
1488 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1491 "During emulation, the following keys are useful:\n"
1492 "ctrl-alt-f toggle full screen\n"
1493 "ctrl-alt-n switch to virtual console 'n'\n"
1494 "ctrl-alt toggle mouse and keyboard grab\n"
1496 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1502 #define HAS_ARG 0x0001
1504 typedef struct QEMUOption
{
1511 static const QEMUOption qemu_options
[] = {
1512 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1513 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1514 { option, opt_arg, opt_enum, arch_mask },
1515 #define DEFHEADING(text)
1516 #include "qemu-options.def"
1522 static void select_vgahw (const char *p
)
1527 vga_interface_type
= VGA_NONE
;
1528 if (strstart(p
, "std", &opts
)) {
1529 vga_interface_type
= VGA_STD
;
1530 } else if (strstart(p
, "cirrus", &opts
)) {
1531 vga_interface_type
= VGA_CIRRUS
;
1532 } else if (strstart(p
, "vmware", &opts
)) {
1533 vga_interface_type
= VGA_VMWARE
;
1534 } else if (strstart(p
, "xenfb", &opts
)) {
1535 vga_interface_type
= VGA_XENFB
;
1536 } else if (strstart(p
, "qxl", &opts
)) {
1537 vga_interface_type
= VGA_QXL
;
1538 } else if (!strstart(p
, "none", &opts
)) {
1540 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1544 const char *nextopt
;
1546 if (strstart(opts
, ",retrace=", &nextopt
)) {
1548 if (strstart(opts
, "dumb", &nextopt
))
1549 vga_retrace_method
= VGA_RETRACE_DUMB
;
1550 else if (strstart(opts
, "precise", &nextopt
))
1551 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1552 else goto invalid_vga
;
1553 } else goto invalid_vga
;
1558 static DisplayType
select_display(const char *p
)
1561 DisplayType display
= DT_DEFAULT
;
1563 if (strstart(p
, "sdl", &opts
)) {
1567 const char *nextopt
;
1569 if (strstart(opts
, ",frame=", &nextopt
)) {
1571 if (strstart(opts
, "on", &nextopt
)) {
1573 } else if (strstart(opts
, "off", &nextopt
)) {
1576 goto invalid_display
;
1578 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1580 if (strstart(opts
, "on", &nextopt
)) {
1582 } else if (strstart(opts
, "off", &nextopt
)) {
1585 goto invalid_display
;
1587 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1589 if (strstart(opts
, "on", &nextopt
)) {
1591 } else if (strstart(opts
, "off", &nextopt
)) {
1594 goto invalid_display
;
1596 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1598 if (strstart(opts
, "on", &nextopt
)) {
1600 } else if (strstart(opts
, "off", &nextopt
)) {
1603 goto invalid_display
;
1606 goto invalid_display
;
1611 fprintf(stderr
, "SDL support is disabled\n");
1614 } else if (strstart(p
, "vnc", &opts
)) {
1618 const char *nextopt
;
1620 if (strstart(opts
, "=", &nextopt
)) {
1621 vnc_display
= nextopt
;
1625 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1628 } else if (strstart(p
, "curses", &opts
)) {
1629 #ifdef CONFIG_CURSES
1630 display
= DT_CURSES
;
1632 fprintf(stderr
, "Curses support is disabled\n");
1635 } else if (strstart(p
, "none", &opts
)) {
1639 fprintf(stderr
, "Unknown display type: %s\n", p
);
1646 static int balloon_parse(const char *arg
)
1650 if (strcmp(arg
, "none") == 0) {
1654 if (!strncmp(arg
, "virtio", 6)) {
1655 if (arg
[6] == ',') {
1656 /* have params -> parse them */
1657 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1661 /* create empty opts */
1662 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1664 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1671 char *qemu_find_file(int type
, const char *name
)
1677 /* If name contains path separators then try it as a straight path. */
1678 if ((strchr(name
, '/') || strchr(name
, '\\'))
1679 && access(name
, R_OK
) == 0) {
1680 return qemu_strdup(name
);
1683 case QEMU_FILE_TYPE_BIOS
:
1686 case QEMU_FILE_TYPE_KEYMAP
:
1687 subdir
= "keymaps/";
1692 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1693 buf
= qemu_mallocz(len
);
1694 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1695 if (access(buf
, R_OK
)) {
1702 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1704 return qdev_device_help(opts
);
1707 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1711 dev
= qdev_device_add(opts
);
1717 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1719 CharDriverState
*chr
;
1721 chr
= qemu_chr_open_opts(opts
, NULL
);
1727 #ifdef CONFIG_VIRTFS
1728 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1731 ret
= qemu_fsdev_add(opts
);
1737 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1739 CharDriverState
*chr
;
1740 const char *chardev
;
1744 mode
= qemu_opt_get(opts
, "mode");
1748 if (strcmp(mode
, "readline") == 0) {
1749 flags
= MONITOR_USE_READLINE
;
1750 } else if (strcmp(mode
, "control") == 0) {
1751 flags
= MONITOR_USE_CONTROL
;
1753 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1757 if (qemu_opt_get_bool(opts
, "pretty", 0))
1758 flags
|= MONITOR_USE_PRETTY
;
1760 if (qemu_opt_get_bool(opts
, "default", 0))
1761 flags
|= MONITOR_IS_DEFAULT
;
1763 chardev
= qemu_opt_get(opts
, "chardev");
1764 chr
= qemu_chr_find(chardev
);
1766 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1770 monitor_init(chr
, flags
);
1774 static void monitor_parse(const char *optarg
, const char *mode
)
1776 static int monitor_device_index
= 0;
1782 if (strstart(optarg
, "chardev:", &p
)) {
1783 snprintf(label
, sizeof(label
), "%s", p
);
1785 snprintf(label
, sizeof(label
), "compat_monitor%d",
1786 monitor_device_index
);
1787 if (monitor_device_index
== 0) {
1790 opts
= qemu_chr_parse_compat(label
, optarg
);
1792 fprintf(stderr
, "parse error: %s\n", optarg
);
1797 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1799 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1802 qemu_opt_set(opts
, "mode", mode
);
1803 qemu_opt_set(opts
, "chardev", label
);
1805 qemu_opt_set(opts
, "default", "on");
1806 monitor_device_index
++;
1809 struct device_config
{
1811 DEV_USB
, /* -usbdevice */
1813 DEV_SERIAL
, /* -serial */
1814 DEV_PARALLEL
, /* -parallel */
1815 DEV_VIRTCON
, /* -virtioconsole */
1816 DEV_DEBUGCON
, /* -debugcon */
1818 const char *cmdline
;
1819 QTAILQ_ENTRY(device_config
) next
;
1821 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1823 static void add_device_config(int type
, const char *cmdline
)
1825 struct device_config
*conf
;
1827 conf
= qemu_mallocz(sizeof(*conf
));
1829 conf
->cmdline
= cmdline
;
1830 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1833 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1835 struct device_config
*conf
;
1838 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1839 if (conf
->type
!= type
)
1841 rc
= func(conf
->cmdline
);
1848 static int serial_parse(const char *devname
)
1850 static int index
= 0;
1853 if (strcmp(devname
, "none") == 0)
1855 if (index
== MAX_SERIAL_PORTS
) {
1856 fprintf(stderr
, "qemu: too many serial ports\n");
1859 snprintf(label
, sizeof(label
), "serial%d", index
);
1860 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1861 if (!serial_hds
[index
]) {
1862 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1863 devname
, strerror(errno
));
1870 static int parallel_parse(const char *devname
)
1872 static int index
= 0;
1875 if (strcmp(devname
, "none") == 0)
1877 if (index
== MAX_PARALLEL_PORTS
) {
1878 fprintf(stderr
, "qemu: too many parallel ports\n");
1881 snprintf(label
, sizeof(label
), "parallel%d", index
);
1882 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1883 if (!parallel_hds
[index
]) {
1884 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1885 devname
, strerror(errno
));
1892 static int virtcon_parse(const char *devname
)
1894 QemuOptsList
*device
= qemu_find_opts("device");
1895 static int index
= 0;
1897 QemuOpts
*bus_opts
, *dev_opts
;
1899 if (strcmp(devname
, "none") == 0)
1901 if (index
== MAX_VIRTIO_CONSOLES
) {
1902 fprintf(stderr
, "qemu: too many virtio consoles\n");
1906 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1907 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1909 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1910 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1912 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1913 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1914 if (!virtcon_hds
[index
]) {
1915 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1916 devname
, strerror(errno
));
1919 qemu_opt_set(dev_opts
, "chardev", label
);
1925 static int debugcon_parse(const char *devname
)
1929 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1932 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1934 fprintf(stderr
, "qemu: already have a debugcon device\n");
1937 qemu_opt_set(opts
, "driver", "isa-debugcon");
1938 qemu_opt_set(opts
, "chardev", "debugcon");
1942 void qemu_add_exit_notifier(Notifier
*notify
)
1944 notifier_list_add(&exit_notifiers
, notify
);
1947 void qemu_remove_exit_notifier(Notifier
*notify
)
1949 notifier_list_remove(&exit_notifiers
, notify
);
1952 static void qemu_run_exit_notifiers(void)
1954 notifier_list_notify(&exit_notifiers
);
1957 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1959 notifier_list_add(&machine_init_done_notifiers
, notify
);
1962 static void qemu_run_machine_init_done_notifiers(void)
1964 notifier_list_notify(&machine_init_done_notifiers
);
1967 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1968 const char **poptarg
, int *poptind
)
1970 const QEMUOption
*popt
;
1971 int optind
= *poptind
;
1972 char *r
= argv
[optind
];
1975 loc_set_cmdline(argv
, optind
, 1);
1977 /* Treat --foo the same as -foo. */
1980 popt
= qemu_options
;
1983 error_report("invalid option");
1986 if (!strcmp(popt
->name
, r
+ 1))
1990 if (popt
->flags
& HAS_ARG
) {
1991 if (optind
>= argc
) {
1992 error_report("requires an argument");
1995 optarg
= argv
[optind
++];
1996 loc_set_cmdline(argv
, optind
- 2, 2);
2007 int main(int argc
, char **argv
, char **envp
)
2009 const char *gdbstub_dev
= NULL
;
2011 int snapshot
, linux_boot
;
2012 const char *icount_option
= NULL
;
2013 const char *initrd_filename
;
2014 const char *kernel_filename
, *kernel_cmdline
;
2015 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2017 DisplayChangeListener
*dcl
;
2018 int cyls
, heads
, secs
, translation
;
2019 QemuOpts
*hda_opts
= NULL
, *opts
;
2020 QemuOptsList
*olist
;
2023 const char *loadvm
= NULL
;
2024 QEMUMachine
*machine
;
2025 const char *cpu_model
;
2027 const char *pid_file
= NULL
;
2028 const char *incoming
= NULL
;
2029 int show_vnc_port
= 0;
2031 const char *trace_file
= NULL
;
2033 atexit(qemu_run_exit_notifiers
);
2034 error_set_progname(argv
[0]);
2038 qemu_cache_utils_init(envp
);
2040 QLIST_INIT (&vm_change_state_head
);
2041 os_setup_early_signal_handling();
2043 module_call_init(MODULE_INIT_MACHINE
);
2044 machine
= find_default_machine();
2046 initrd_filename
= NULL
;
2049 kernel_filename
= NULL
;
2050 kernel_cmdline
= "";
2051 cyls
= heads
= secs
= 0;
2052 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2054 for (i
= 0; i
< MAX_NODES
; i
++) {
2056 node_cpumask
[i
] = 0;
2065 /* first pass of option parsing */
2067 while (optind
< argc
) {
2068 if (argv
[optind
][0] != '-') {
2073 const QEMUOption
*popt
;
2075 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2076 switch (popt
->index
) {
2077 case QEMU_OPTION_nodefconfig
:
2087 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2088 if (ret
< 0 && ret
!= -ENOENT
) {
2092 ret
= qemu_read_config_file(arch_config_name
);
2093 if (ret
< 0 && ret
!= -ENOENT
) {
2099 /* second pass of option parsing */
2104 if (argv
[optind
][0] != '-') {
2105 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2107 const QEMUOption
*popt
;
2109 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2110 if (!(popt
->arch_mask
& arch_type
)) {
2111 printf("Option %s not supported for this target\n", popt
->name
);
2114 switch(popt
->index
) {
2116 machine
= find_machine(optarg
);
2119 printf("Supported machines are:\n");
2120 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2122 printf("%-10s %s (alias of %s)\n",
2123 m
->alias
, m
->desc
, m
->name
);
2124 printf("%-10s %s%s\n",
2126 m
->is_default
? " (default)" : "");
2128 exit(*optarg
!= '?');
2131 case QEMU_OPTION_cpu
:
2132 /* hw initialization will check this */
2133 if (*optarg
== '?') {
2134 list_cpus(stdout
, &fprintf
, optarg
);
2140 case QEMU_OPTION_initrd
:
2141 initrd_filename
= optarg
;
2143 case QEMU_OPTION_hda
:
2147 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2149 snprintf(buf
, sizeof(buf
),
2150 "%s,cyls=%d,heads=%d,secs=%d%s",
2151 HD_OPTS
, cyls
, heads
, secs
,
2152 translation
== BIOS_ATA_TRANSLATION_LBA
?
2154 translation
== BIOS_ATA_TRANSLATION_NONE
?
2155 ",trans=none" : "");
2156 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2159 case QEMU_OPTION_hdb
:
2160 case QEMU_OPTION_hdc
:
2161 case QEMU_OPTION_hdd
:
2162 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2165 case QEMU_OPTION_drive
:
2168 case QEMU_OPTION_set
:
2169 if (qemu_set_option(optarg
) != 0)
2172 case QEMU_OPTION_global
:
2173 if (qemu_global_option(optarg
) != 0)
2176 case QEMU_OPTION_mtdblock
:
2177 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2179 case QEMU_OPTION_sd
:
2180 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2182 case QEMU_OPTION_pflash
:
2183 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2185 case QEMU_OPTION_snapshot
:
2188 case QEMU_OPTION_hdachs
:
2192 cyls
= strtol(p
, (char **)&p
, 0);
2193 if (cyls
< 1 || cyls
> 16383)
2198 heads
= strtol(p
, (char **)&p
, 0);
2199 if (heads
< 1 || heads
> 16)
2204 secs
= strtol(p
, (char **)&p
, 0);
2205 if (secs
< 1 || secs
> 63)
2209 if (!strcmp(p
, "none"))
2210 translation
= BIOS_ATA_TRANSLATION_NONE
;
2211 else if (!strcmp(p
, "lba"))
2212 translation
= BIOS_ATA_TRANSLATION_LBA
;
2213 else if (!strcmp(p
, "auto"))
2214 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2217 } else if (*p
!= '\0') {
2219 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2222 if (hda_opts
!= NULL
) {
2224 snprintf(num
, sizeof(num
), "%d", cyls
);
2225 qemu_opt_set(hda_opts
, "cyls", num
);
2226 snprintf(num
, sizeof(num
), "%d", heads
);
2227 qemu_opt_set(hda_opts
, "heads", num
);
2228 snprintf(num
, sizeof(num
), "%d", secs
);
2229 qemu_opt_set(hda_opts
, "secs", num
);
2230 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2231 qemu_opt_set(hda_opts
, "trans", "lba");
2232 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2233 qemu_opt_set(hda_opts
, "trans", "none");
2237 case QEMU_OPTION_numa
:
2238 if (nb_numa_nodes
>= MAX_NODES
) {
2239 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2244 case QEMU_OPTION_display
:
2245 display_type
= select_display(optarg
);
2247 case QEMU_OPTION_nographic
:
2248 display_type
= DT_NOGRAPHIC
;
2250 #ifdef CONFIG_CURSES
2251 case QEMU_OPTION_curses
:
2252 display_type
= DT_CURSES
;
2255 case QEMU_OPTION_portrait
:
2258 case QEMU_OPTION_kernel
:
2259 kernel_filename
= optarg
;
2261 case QEMU_OPTION_append
:
2262 kernel_cmdline
= optarg
;
2264 case QEMU_OPTION_cdrom
:
2265 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2267 case QEMU_OPTION_boot
:
2269 static const char * const params
[] = {
2270 "order", "once", "menu", NULL
2272 char buf
[sizeof(boot_devices
)];
2273 char *standard_boot_devices
;
2276 if (!strchr(optarg
, '=')) {
2278 pstrcpy(buf
, sizeof(buf
), optarg
);
2279 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2281 "qemu: unknown boot parameter '%s' in '%s'\n",
2287 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2288 validate_bootdevices(buf
);
2289 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2292 if (get_param_value(buf
, sizeof(buf
),
2294 validate_bootdevices(buf
);
2295 standard_boot_devices
= qemu_strdup(boot_devices
);
2296 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2297 qemu_register_reset(restore_boot_devices
,
2298 standard_boot_devices
);
2300 if (get_param_value(buf
, sizeof(buf
),
2302 if (!strcmp(buf
, "on")) {
2304 } else if (!strcmp(buf
, "off")) {
2308 "qemu: invalid option value '%s'\n",
2316 case QEMU_OPTION_fda
:
2317 case QEMU_OPTION_fdb
:
2318 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2321 case QEMU_OPTION_no_fd_bootchk
:
2324 case QEMU_OPTION_netdev
:
2325 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2329 case QEMU_OPTION_net
:
2330 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2335 case QEMU_OPTION_tftp
:
2336 legacy_tftp_prefix
= optarg
;
2338 case QEMU_OPTION_bootp
:
2339 legacy_bootp_filename
= optarg
;
2341 case QEMU_OPTION_redir
:
2342 if (net_slirp_redir(optarg
) < 0)
2346 case QEMU_OPTION_bt
:
2347 add_device_config(DEV_BT
, optarg
);
2349 case QEMU_OPTION_audio_help
:
2350 if (!(audio_available())) {
2351 printf("Option %s not supported for this target\n", popt
->name
);
2357 case QEMU_OPTION_soundhw
:
2358 if (!(audio_available())) {
2359 printf("Option %s not supported for this target\n", popt
->name
);
2362 select_soundhw (optarg
);
2367 case QEMU_OPTION_version
:
2371 case QEMU_OPTION_m
: {
2374 value
= strtosz(optarg
, NULL
);
2376 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2380 /* On 32-bit hosts, QEMU is limited by virtual address space */
2381 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2382 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2385 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2386 fprintf(stderr
, "qemu: ram size too large\n");
2392 case QEMU_OPTION_mempath
:
2396 case QEMU_OPTION_mem_prealloc
:
2401 set_cpu_log(optarg
);
2404 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2406 case QEMU_OPTION_gdb
:
2407 gdbstub_dev
= optarg
;
2412 case QEMU_OPTION_bios
:
2415 case QEMU_OPTION_singlestep
:
2422 keyboard_layout
= optarg
;
2424 case QEMU_OPTION_localtime
:
2427 case QEMU_OPTION_vga
:
2428 select_vgahw (optarg
);
2435 w
= strtol(p
, (char **)&p
, 10);
2438 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2444 h
= strtol(p
, (char **)&p
, 10);
2449 depth
= strtol(p
, (char **)&p
, 10);
2450 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2451 depth
!= 24 && depth
!= 32)
2453 } else if (*p
== '\0') {
2454 depth
= graphic_depth
;
2461 graphic_depth
= depth
;
2464 case QEMU_OPTION_echr
:
2467 term_escape_char
= strtol(optarg
, &r
, 0);
2469 printf("Bad argument to echr\n");
2472 case QEMU_OPTION_monitor
:
2473 monitor_parse(optarg
, "readline");
2474 default_monitor
= 0;
2476 case QEMU_OPTION_qmp
:
2477 monitor_parse(optarg
, "control");
2478 default_monitor
= 0;
2480 case QEMU_OPTION_mon
:
2481 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2485 default_monitor
= 0;
2487 case QEMU_OPTION_chardev
:
2488 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2493 case QEMU_OPTION_fsdev
:
2494 olist
= qemu_find_opts("fsdev");
2496 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2499 opts
= qemu_opts_parse(olist
, optarg
, 1);
2501 fprintf(stderr
, "parse error: %s\n", optarg
);
2505 case QEMU_OPTION_virtfs
: {
2506 char *arg_fsdev
= NULL
;
2507 char *arg_9p
= NULL
;
2510 olist
= qemu_find_opts("virtfs");
2512 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2515 opts
= qemu_opts_parse(olist
, optarg
, 1);
2517 fprintf(stderr
, "parse error: %s\n", optarg
);
2521 if (qemu_opt_get(opts
, "fstype") == NULL
||
2522 qemu_opt_get(opts
, "mount_tag") == NULL
||
2523 qemu_opt_get(opts
, "path") == NULL
||
2524 qemu_opt_get(opts
, "security_model") == NULL
) {
2525 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2526 "security_model=[mapped|passthrough|none],"
2531 len
= strlen(",id=,path=,security_model=");
2532 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2533 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2534 len
+= strlen(qemu_opt_get(opts
, "path"));
2535 len
+= strlen(qemu_opt_get(opts
, "security_model"));
2536 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2538 snprintf(arg_fsdev
, (len
+ 1) * sizeof(*arg_fsdev
),
2539 "%s,id=%s,path=%s,security_model=%s",
2540 qemu_opt_get(opts
, "fstype"),
2541 qemu_opt_get(opts
, "mount_tag"),
2542 qemu_opt_get(opts
, "path"),
2543 qemu_opt_get(opts
, "security_model"));
2545 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2546 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2547 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2549 snprintf(arg_9p
, (len
+ 1) * sizeof(*arg_9p
),
2550 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2551 qemu_opt_get(opts
, "mount_tag"),
2552 qemu_opt_get(opts
, "mount_tag"));
2554 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev
, 1)) {
2555 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2559 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p
, 1)) {
2560 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2564 qemu_free(arg_fsdev
);
2568 case QEMU_OPTION_serial
:
2569 add_device_config(DEV_SERIAL
, optarg
);
2571 if (strncmp(optarg
, "mon:", 4) == 0) {
2572 default_monitor
= 0;
2575 case QEMU_OPTION_watchdog
:
2578 "qemu: only one watchdog option may be given\n");
2583 case QEMU_OPTION_watchdog_action
:
2584 if (select_watchdog_action(optarg
) == -1) {
2585 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2589 case QEMU_OPTION_virtiocon
:
2590 add_device_config(DEV_VIRTCON
, optarg
);
2591 default_virtcon
= 0;
2592 if (strncmp(optarg
, "mon:", 4) == 0) {
2593 default_monitor
= 0;
2596 case QEMU_OPTION_parallel
:
2597 add_device_config(DEV_PARALLEL
, optarg
);
2598 default_parallel
= 0;
2599 if (strncmp(optarg
, "mon:", 4) == 0) {
2600 default_monitor
= 0;
2603 case QEMU_OPTION_debugcon
:
2604 add_device_config(DEV_DEBUGCON
, optarg
);
2606 case QEMU_OPTION_loadvm
:
2609 case QEMU_OPTION_full_screen
:
2613 case QEMU_OPTION_no_frame
:
2616 case QEMU_OPTION_alt_grab
:
2619 case QEMU_OPTION_ctrl_grab
:
2622 case QEMU_OPTION_no_quit
:
2625 case QEMU_OPTION_sdl
:
2626 display_type
= DT_SDL
;
2629 case QEMU_OPTION_no_frame
:
2630 case QEMU_OPTION_alt_grab
:
2631 case QEMU_OPTION_ctrl_grab
:
2632 case QEMU_OPTION_no_quit
:
2633 case QEMU_OPTION_sdl
:
2634 fprintf(stderr
, "SDL support is disabled\n");
2637 case QEMU_OPTION_pidfile
:
2640 case QEMU_OPTION_win2k_hack
:
2641 win2k_install_hack
= 1;
2643 case QEMU_OPTION_rtc_td_hack
:
2646 case QEMU_OPTION_acpitable
:
2647 do_acpitable_option(optarg
);
2649 case QEMU_OPTION_smbios
:
2650 do_smbios_option(optarg
);
2652 case QEMU_OPTION_enable_kvm
:
2655 case QEMU_OPTION_usb
:
2658 case QEMU_OPTION_usbdevice
:
2660 add_device_config(DEV_USB
, optarg
);
2662 case QEMU_OPTION_device
:
2663 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2667 case QEMU_OPTION_smp
:
2670 fprintf(stderr
, "Invalid number of CPUs\n");
2673 if (max_cpus
< smp_cpus
) {
2674 fprintf(stderr
, "maxcpus must be equal to or greater than "
2678 if (max_cpus
> 255) {
2679 fprintf(stderr
, "Unsupported number of maxcpus\n");
2683 case QEMU_OPTION_vnc
:
2685 vnc_display
= optarg
;
2687 case QEMU_OPTION_no_acpi
:
2690 case QEMU_OPTION_no_hpet
:
2693 case QEMU_OPTION_balloon
:
2694 if (balloon_parse(optarg
) < 0) {
2695 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2699 case QEMU_OPTION_no_reboot
:
2702 case QEMU_OPTION_no_shutdown
:
2705 case QEMU_OPTION_show_cursor
:
2708 case QEMU_OPTION_uuid
:
2709 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2710 fprintf(stderr
, "Fail to parse UUID string."
2711 " Wrong format.\n");
2715 case QEMU_OPTION_option_rom
:
2716 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2717 fprintf(stderr
, "Too many option ROMs\n");
2720 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2721 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2722 option_rom
[nb_option_roms
].bootindex
=
2723 qemu_opt_get_number(opts
, "bootindex", -1);
2724 if (!option_rom
[nb_option_roms
].name
) {
2725 fprintf(stderr
, "Option ROM file is not specified\n");
2730 case QEMU_OPTION_semihosting
:
2731 semihosting_enabled
= 1;
2733 case QEMU_OPTION_name
:
2734 qemu_name
= qemu_strdup(optarg
);
2736 char *p
= strchr(qemu_name
, ',');
2739 if (strncmp(p
, "process=", 8)) {
2740 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2744 os_set_proc_name(p
);
2748 case QEMU_OPTION_prom_env
:
2749 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2750 fprintf(stderr
, "Too many prom variables\n");
2753 prom_envs
[nb_prom_envs
] = optarg
;
2756 case QEMU_OPTION_old_param
:
2759 case QEMU_OPTION_clock
:
2760 configure_alarms(optarg
);
2762 case QEMU_OPTION_startdate
:
2763 configure_rtc_date_offset(optarg
, 1);
2765 case QEMU_OPTION_rtc
:
2766 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2770 configure_rtc(opts
);
2772 case QEMU_OPTION_tb_size
:
2773 tb_size
= strtol(optarg
, NULL
, 0);
2777 case QEMU_OPTION_icount
:
2778 icount_option
= optarg
;
2780 case QEMU_OPTION_incoming
:
2782 incoming_expected
= true;
2784 case QEMU_OPTION_nodefaults
:
2786 default_parallel
= 0;
2787 default_virtcon
= 0;
2788 default_monitor
= 0;
2795 case QEMU_OPTION_xen_domid
:
2796 if (!(xen_available())) {
2797 printf("Option %s not supported for this target\n", popt
->name
);
2800 xen_domid
= atoi(optarg
);
2802 case QEMU_OPTION_xen_create
:
2803 if (!(xen_available())) {
2804 printf("Option %s not supported for this target\n", popt
->name
);
2807 xen_mode
= XEN_CREATE
;
2809 case QEMU_OPTION_xen_attach
:
2810 if (!(xen_available())) {
2811 printf("Option %s not supported for this target\n", popt
->name
);
2814 xen_mode
= XEN_ATTACH
;
2816 #ifdef CONFIG_SIMPLE_TRACE
2817 case QEMU_OPTION_trace
:
2818 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2820 trace_file
= qemu_opt_get(opts
, "file");
2824 case QEMU_OPTION_readconfig
:
2826 int ret
= qemu_read_config_file(optarg
);
2828 fprintf(stderr
, "read config %s: %s\n", optarg
,
2834 case QEMU_OPTION_spice
:
2835 olist
= qemu_find_opts("spice");
2837 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2840 opts
= qemu_opts_parse(olist
, optarg
, 0);
2842 fprintf(stderr
, "parse error: %s\n", optarg
);
2846 case QEMU_OPTION_writeconfig
:
2849 if (strcmp(optarg
, "-") == 0) {
2852 fp
= fopen(optarg
, "w");
2854 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2858 qemu_config_write(fp
);
2863 os_parse_cmd_args(popt
->index
, optarg
);
2869 if (!st_init(trace_file
)) {
2870 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2873 /* If no data_dir is specified then try to find it relative to the
2876 data_dir
= os_find_datadir(argv
[0]);
2878 /* If all else fails use the install patch specified when building. */
2880 data_dir
= CONFIG_QEMU_DATADIR
;
2884 * Default to max_cpus = smp_cpus, in case the user doesn't
2885 * specify a max_cpus value.
2888 max_cpus
= smp_cpus
;
2890 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2891 if (smp_cpus
> machine
->max_cpus
) {
2892 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2893 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2898 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
2899 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
2901 if (machine
->no_serial
) {
2904 if (machine
->no_parallel
) {
2905 default_parallel
= 0;
2907 if (!machine
->use_virtcon
) {
2908 default_virtcon
= 0;
2910 if (machine
->no_vga
) {
2913 if (machine
->no_floppy
) {
2916 if (machine
->no_cdrom
) {
2919 if (machine
->no_sdcard
) {
2923 if (display_type
== DT_NOGRAPHIC
) {
2924 if (default_parallel
)
2925 add_device_config(DEV_PARALLEL
, "null");
2926 if (default_serial
&& default_monitor
) {
2927 add_device_config(DEV_SERIAL
, "mon:stdio");
2928 } else if (default_virtcon
&& default_monitor
) {
2929 add_device_config(DEV_VIRTCON
, "mon:stdio");
2932 add_device_config(DEV_SERIAL
, "stdio");
2933 if (default_virtcon
)
2934 add_device_config(DEV_VIRTCON
, "stdio");
2935 if (default_monitor
)
2936 monitor_parse("stdio", "readline");
2940 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2941 if (default_parallel
)
2942 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2943 if (default_monitor
)
2944 monitor_parse("vc:80Cx24C", "readline");
2945 if (default_virtcon
)
2946 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2949 vga_interface_type
= VGA_CIRRUS
;
2953 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
2955 #ifdef CONFIG_VIRTFS
2956 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
2963 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
2969 int ret
= kvm_init();
2971 if (!kvm_available()) {
2972 printf("KVM not supported for this target\n");
2974 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
2980 if (qemu_init_main_loop()) {
2981 fprintf(stderr
, "qemu_init_main_loop failed\n");
2984 linux_boot
= (kernel_filename
!= NULL
);
2986 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
2987 fprintf(stderr
, "-append only allowed with -kernel option\n");
2991 if (!linux_boot
&& initrd_filename
!= NULL
) {
2992 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
2996 os_set_line_buffering();
2998 if (init_timer_alarm() < 0) {
2999 fprintf(stderr
, "could not initialize alarm timer\n");
3002 configure_icount(icount_option
);
3004 if (net_init_clients() < 0) {
3008 /* init the bluetooth world */
3009 if (foreach_device_config(DEV_BT
, bt_parse
))
3012 /* init the memory */
3014 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3016 /* init the dynamic translator */
3017 cpu_exec_init_all(tb_size
* 1024 * 1024);
3019 bdrv_init_with_whitelist();
3023 /* open the virtual block devices */
3025 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3026 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3029 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3030 IF_DEFAULT
, 2, CDROM_OPTS
);
3031 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3032 IF_FLOPPY
, 0, FD_OPTS
);
3033 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3036 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3039 if (nb_numa_nodes
> 0) {
3042 if (nb_numa_nodes
> smp_cpus
) {
3043 nb_numa_nodes
= smp_cpus
;
3046 /* If no memory size if given for any node, assume the default case
3047 * and distribute the available memory equally across all nodes
3049 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3050 if (node_mem
[i
] != 0)
3053 if (i
== nb_numa_nodes
) {
3054 uint64_t usedmem
= 0;
3056 /* On Linux, the each node's border has to be 8MB aligned,
3057 * the final node gets the rest.
3059 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3060 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3061 usedmem
+= node_mem
[i
];
3063 node_mem
[i
] = ram_size
- usedmem
;
3066 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3067 if (node_cpumask
[i
] != 0)
3070 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3071 * must cope with this anyway, because there are BIOSes out there in
3072 * real machines which also use this scheme.
3074 if (i
== nb_numa_nodes
) {
3075 for (i
= 0; i
< smp_cpus
; i
++) {
3076 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3081 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3085 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3087 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3089 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3091 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3094 module_call_init(MODULE_INIT_DEVICE
);
3096 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3100 i
= select_watchdog(watchdog
);
3102 exit (i
== 1 ? 1 : 0);
3105 if (machine
->compat_props
) {
3106 qdev_prop_register_global_list(machine
->compat_props
);
3110 machine
->init(ram_size
, boot_devices
,
3111 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3113 cpu_synchronize_all_post_init();
3115 /* must be after terminal init, SDL library changes signal handlers */
3116 os_setup_signal_handling();
3120 current_machine
= machine
;
3122 /* init USB devices */
3124 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3128 /* init generic devices */
3129 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3132 net_check_clients();
3134 /* just use the first displaystate for the moment */
3135 ds
= get_displaystate();
3139 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3140 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3141 display_type
= DT_SDL
;
3143 vnc_display
= "localhost:0,to=99";
3149 /* init local displays */
3150 switch (display_type
) {
3153 #if defined(CONFIG_CURSES)
3155 curses_display_init(ds
, full_screen
);
3158 #if defined(CONFIG_SDL)
3160 sdl_display_init(ds
, full_screen
, no_frame
);
3162 #elif defined(CONFIG_COCOA)
3164 cocoa_display_init(ds
, full_screen
);
3171 /* init remote displays */
3173 vnc_display_init(ds
);
3174 if (vnc_display_open(ds
, vnc_display
) < 0)
3177 if (show_vnc_port
) {
3178 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3182 if (using_spice
&& !qxl_enabled
) {
3183 qemu_spice_display_init(ds
);
3189 dcl
= ds
->listeners
;
3190 while (dcl
!= NULL
) {
3191 if (dcl
->dpy_refresh
!= NULL
) {
3192 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3193 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3198 if (ds
->gui_timer
== NULL
) {
3199 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3200 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3202 text_consoles_set_display(ds
);
3204 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3205 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3210 qdev_machine_creation_done();
3212 if (rom_load_all() != 0) {
3213 fprintf(stderr
, "rom loading failed\n");
3217 /* TODO: once all bus devices are qdevified, this should be done
3218 * when bus is created by qdev.c */
3219 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3220 qemu_run_machine_init_done_notifiers();
3222 qemu_system_reset();
3224 if (load_vmstate(loadvm
) < 0) {
3230 int ret
= qemu_start_incoming_migration(incoming
);
3232 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3236 } else if (autostart
) {