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
);
260 static int tcg_allowed
= 1;
264 enum xen_mode xen_mode
= XEN_EMULATE
;
266 static int default_serial
= 1;
267 static int default_parallel
= 1;
268 static int default_virtcon
= 1;
269 static int default_monitor
= 1;
270 static int default_vga
= 1;
271 static int default_floppy
= 1;
272 static int default_cdrom
= 1;
273 static int default_sdcard
= 1;
279 { .driver
= "isa-serial", .flag
= &default_serial
},
280 { .driver
= "isa-parallel", .flag
= &default_parallel
},
281 { .driver
= "isa-fdc", .flag
= &default_floppy
},
282 { .driver
= "ide-drive", .flag
= &default_cdrom
},
283 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
284 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
285 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
286 { .driver
= "VGA", .flag
= &default_vga
},
287 { .driver
= "cirrus-vga", .flag
= &default_vga
},
288 { .driver
= "vmware-svga", .flag
= &default_vga
},
291 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
293 const char *driver
= qemu_opt_get(opts
, "driver");
298 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
299 if (strcmp(default_list
[i
].driver
, driver
) != 0)
301 *(default_list
[i
].flag
) = 0;
306 /***********************************************************/
307 /* real time host monotonic timer */
309 /***********************************************************/
310 /* host time/date access */
311 void qemu_get_timedate(struct tm
*tm
, int offset
)
318 if (rtc_date_offset
== -1) {
322 ret
= localtime(&ti
);
324 ti
-= rtc_date_offset
;
328 memcpy(tm
, ret
, sizeof(struct tm
));
331 int qemu_timedate_diff(struct tm
*tm
)
335 if (rtc_date_offset
== -1)
337 seconds
= mktimegm(tm
);
339 seconds
= mktime(tm
);
341 seconds
= mktimegm(tm
) + rtc_date_offset
;
343 return seconds
- time(NULL
);
346 void rtc_change_mon_event(struct tm
*tm
)
350 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
351 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
352 qobject_decref(data
);
355 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
357 time_t rtc_start_date
;
360 if (!strcmp(startdate
, "now") && legacy
) {
361 rtc_date_offset
= -1;
363 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
371 } else if (sscanf(startdate
, "%d-%d-%d",
383 rtc_start_date
= mktimegm(&tm
);
384 if (rtc_start_date
== -1) {
386 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
387 "'2006-06-17T16:01:21' or '2006-06-17'\n");
390 rtc_date_offset
= time(NULL
) - rtc_start_date
;
394 static void configure_rtc(QemuOpts
*opts
)
398 value
= qemu_opt_get(opts
, "base");
400 if (!strcmp(value
, "utc")) {
402 } else if (!strcmp(value
, "localtime")) {
405 configure_rtc_date_offset(value
, 0);
408 value
= qemu_opt_get(opts
, "clock");
410 if (!strcmp(value
, "host")) {
411 rtc_clock
= host_clock
;
412 } else if (!strcmp(value
, "vm")) {
413 rtc_clock
= vm_clock
;
415 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
419 value
= qemu_opt_get(opts
, "driftfix");
421 if (!strcmp(value
, "slew")) {
423 } else if (!strcmp(value
, "none")) {
426 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
432 /***********************************************************/
433 /* Bluetooth support */
436 static struct HCIInfo
*hci_table
[MAX_NICS
];
438 static struct bt_vlan_s
{
439 struct bt_scatternet_s net
;
441 struct bt_vlan_s
*next
;
444 /* find or alloc a new bluetooth "VLAN" */
445 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
447 struct bt_vlan_s
**pvlan
, *vlan
;
448 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
452 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
454 pvlan
= &first_bt_vlan
;
455 while (*pvlan
!= NULL
)
456 pvlan
= &(*pvlan
)->next
;
461 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
465 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
470 static struct HCIInfo null_hci
= {
471 .cmd_send
= null_hci_send
,
472 .sco_send
= null_hci_send
,
473 .acl_send
= null_hci_send
,
474 .bdaddr_set
= null_hci_addr_set
,
477 struct HCIInfo
*qemu_next_hci(void)
479 if (cur_hci
== nb_hcis
)
482 return hci_table
[cur_hci
++];
485 static struct HCIInfo
*hci_init(const char *str
)
488 struct bt_scatternet_s
*vlan
= 0;
490 if (!strcmp(str
, "null"))
493 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
495 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
496 else if (!strncmp(str
, "hci", 3)) {
499 if (!strncmp(str
+ 3, ",vlan=", 6)) {
500 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
505 vlan
= qemu_find_bt_vlan(0);
507 return bt_new_hci(vlan
);
510 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
515 static int bt_hci_parse(const char *str
)
520 if (nb_hcis
>= MAX_NICS
) {
521 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
534 bdaddr
.b
[5] = 0x56 + nb_hcis
;
535 hci
->bdaddr_set(hci
, bdaddr
.b
);
537 hci_table
[nb_hcis
++] = hci
;
542 static void bt_vhci_add(int vlan_id
)
544 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
547 fprintf(stderr
, "qemu: warning: adding a VHCI to "
548 "an empty scatternet %i\n", vlan_id
);
550 bt_vhci_init(bt_new_hci(vlan
));
553 static struct bt_device_s
*bt_device_add(const char *opt
)
555 struct bt_scatternet_s
*vlan
;
557 char *endp
= strstr(opt
, ",vlan=");
558 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
561 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
564 vlan_id
= strtol(endp
+ 6, &endp
, 0);
566 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
571 vlan
= qemu_find_bt_vlan(vlan_id
);
574 fprintf(stderr
, "qemu: warning: adding a slave device to "
575 "an empty scatternet %i\n", vlan_id
);
577 if (!strcmp(devname
, "keyboard"))
578 return bt_keyboard_init(vlan
);
580 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
584 static int bt_parse(const char *opt
)
586 const char *endp
, *p
;
589 if (strstart(opt
, "hci", &endp
)) {
590 if (!*endp
|| *endp
== ',') {
592 if (!strstart(endp
, ",vlan=", 0))
595 return bt_hci_parse(opt
);
597 } else if (strstart(opt
, "vhci", &endp
)) {
598 if (!*endp
|| *endp
== ',') {
600 if (strstart(endp
, ",vlan=", &p
)) {
601 vlan
= strtol(p
, (char **) &endp
, 0);
603 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
607 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
616 } else if (strstart(opt
, "device:", &endp
))
617 return !bt_device_add(endp
);
619 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
623 /***********************************************************/
624 /* QEMU Block devices */
626 #define HD_OPTS "media=disk"
627 #define CDROM_OPTS "media=cdrom"
629 #define PFLASH_OPTS ""
633 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
635 int *use_scsi
= opaque
;
637 return drive_init(opts
, *use_scsi
) == NULL
;
640 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
642 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
643 qemu_opt_set(opts
, "snapshot", "on");
648 static void default_drive(int enable
, int snapshot
, int use_scsi
,
649 BlockInterfaceType type
, int index
,
654 if (type
== IF_DEFAULT
) {
655 type
= use_scsi
? IF_SCSI
: IF_IDE
;
658 if (!enable
|| drive_get_by_index(type
, index
)) {
662 opts
= drive_add(type
, index
, NULL
, optstr
);
664 drive_enable_snapshot(opts
, NULL
);
666 if (!drive_init(opts
, use_scsi
)) {
671 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
673 boot_set_handler
= func
;
674 boot_set_opaque
= opaque
;
677 int qemu_boot_set(const char *boot_devices
)
679 if (!boot_set_handler
) {
682 return boot_set_handler(boot_set_opaque
, boot_devices
);
685 static void validate_bootdevices(char *devices
)
687 /* We just do some generic consistency checks */
691 for (p
= devices
; *p
!= '\0'; p
++) {
692 /* Allowed boot devices are:
693 * a-b: floppy disk drives
694 * c-f: IDE disk drives
695 * g-m: machine implementation dependant drives
696 * n-p: network devices
697 * It's up to each machine implementation to check if the given boot
698 * devices match the actual hardware implementation and firmware
701 if (*p
< 'a' || *p
> 'p') {
702 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
705 if (bitmap
& (1 << (*p
- 'a'))) {
706 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
709 bitmap
|= 1 << (*p
- 'a');
713 static void restore_boot_devices(void *opaque
)
715 char *standard_boot_devices
= opaque
;
716 static int first
= 1;
718 /* Restore boot order and remove ourselves after the first boot */
724 qemu_boot_set(standard_boot_devices
);
726 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
727 qemu_free(standard_boot_devices
);
730 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
733 FWBootEntry
*node
, *i
;
739 assert(dev
!= NULL
|| suffix
!= NULL
);
741 node
= qemu_mallocz(sizeof(FWBootEntry
));
742 node
->bootindex
= bootindex
;
743 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
746 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
747 if (i
->bootindex
== bootindex
) {
748 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
750 } else if (i
->bootindex
< bootindex
) {
753 QTAILQ_INSERT_BEFORE(i
, node
, link
);
756 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
760 * This function returns null terminated string that consist of new line
761 * separated device paths.
763 * memory pointed by "size" is assigned total length of the array in bytes
766 char *get_boot_devices_list(uint32_t *size
)
772 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
773 char *devpath
= NULL
, *bootpath
;
777 devpath
= qdev_get_fw_dev_path(i
->dev
);
781 if (i
->suffix
&& devpath
) {
782 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
784 bootpath
= qemu_malloc(bootpathlen
);
785 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
787 } else if (devpath
) {
790 bootpath
= qemu_strdup(i
->suffix
);
795 list
[total
-1] = '\n';
797 len
= strlen(bootpath
) + 1;
798 list
= qemu_realloc(list
, total
+ len
);
799 memcpy(&list
[total
], bootpath
, len
);
809 static void numa_add(const char *optarg
)
813 unsigned long long value
, endvalue
;
816 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
817 if (!strcmp(option
, "node")) {
818 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
819 nodenr
= nb_numa_nodes
;
821 nodenr
= strtoull(option
, NULL
, 10);
824 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
825 node_mem
[nodenr
] = 0;
828 sval
= strtosz(option
, NULL
);
830 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
833 node_mem
[nodenr
] = sval
;
835 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
836 node_cpumask
[nodenr
] = 0;
838 value
= strtoull(option
, &endptr
, 10);
841 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
843 if (*endptr
== '-') {
844 endvalue
= strtoull(endptr
+1, &endptr
, 10);
845 if (endvalue
>= 63) {
848 "only 63 CPUs in NUMA mode supported.\n");
850 value
= (2ULL << endvalue
) - (1ULL << value
);
852 value
= 1ULL << value
;
855 node_cpumask
[nodenr
] = value
;
862 static void smp_parse(const char *optarg
)
864 int smp
, sockets
= 0, threads
= 0, cores
= 0;
868 smp
= strtoul(optarg
, &endptr
, 10);
869 if (endptr
!= optarg
) {
870 if (*endptr
== ',') {
874 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
875 sockets
= strtoull(option
, NULL
, 10);
876 if (get_param_value(option
, 128, "cores", endptr
) != 0)
877 cores
= strtoull(option
, NULL
, 10);
878 if (get_param_value(option
, 128, "threads", endptr
) != 0)
879 threads
= strtoull(option
, NULL
, 10);
880 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
881 max_cpus
= strtoull(option
, NULL
, 10);
883 /* compute missing values, prefer sockets over cores over threads */
884 if (smp
== 0 || sockets
== 0) {
885 sockets
= sockets
> 0 ? sockets
: 1;
886 cores
= cores
> 0 ? cores
: 1;
887 threads
= threads
> 0 ? threads
: 1;
889 smp
= cores
* threads
* sockets
;
893 threads
= threads
> 0 ? threads
: 1;
894 cores
= smp
/ (sockets
* threads
);
896 threads
= smp
/ (cores
* sockets
);
900 smp_cores
= cores
> 0 ? cores
: 1;
901 smp_threads
= threads
> 0 ? threads
: 1;
906 /***********************************************************/
909 static int usb_device_add(const char *devname
)
912 USBDevice
*dev
= NULL
;
917 /* drivers with .usbdevice_name entry in USBDeviceInfo */
918 dev
= usbdevice_create(devname
);
923 if (strstart(devname
, "host:", &p
)) {
924 dev
= usb_host_device_open(p
);
925 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
926 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
927 bt_new_hci(qemu_find_bt_vlan(0)));
938 static int usb_device_del(const char *devname
)
943 if (strstart(devname
, "host:", &p
))
944 return usb_host_device_close(p
);
949 p
= strchr(devname
, '.');
952 bus_num
= strtoul(devname
, NULL
, 0);
953 addr
= strtoul(p
+ 1, NULL
, 0);
955 return usb_device_delete_addr(bus_num
, addr
);
958 static int usb_parse(const char *cmdline
)
961 r
= usb_device_add(cmdline
);
963 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
968 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
970 const char *devname
= qdict_get_str(qdict
, "devname");
971 if (usb_device_add(devname
) < 0) {
972 error_report("could not add USB device '%s'", devname
);
976 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
978 const char *devname
= qdict_get_str(qdict
, "devname");
979 if (usb_device_del(devname
) < 0) {
980 error_report("could not delete USB device '%s'", devname
);
984 /***********************************************************/
987 static struct pcmcia_socket_entry_s
{
988 PCMCIASocket
*socket
;
989 struct pcmcia_socket_entry_s
*next
;
990 } *pcmcia_sockets
= 0;
992 void pcmcia_socket_register(PCMCIASocket
*socket
)
994 struct pcmcia_socket_entry_s
*entry
;
996 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
997 entry
->socket
= socket
;
998 entry
->next
= pcmcia_sockets
;
999 pcmcia_sockets
= entry
;
1002 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1004 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1006 ptr
= &pcmcia_sockets
;
1007 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1008 if (entry
->socket
== socket
) {
1014 void pcmcia_info(Monitor
*mon
)
1016 struct pcmcia_socket_entry_s
*iter
;
1018 if (!pcmcia_sockets
)
1019 monitor_printf(mon
, "No PCMCIA sockets\n");
1021 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1022 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1023 iter
->socket
->attached
? iter
->socket
->card_string
:
1027 /***********************************************************/
1028 /* machine registration */
1030 static QEMUMachine
*first_machine
= NULL
;
1031 QEMUMachine
*current_machine
= NULL
;
1033 int qemu_register_machine(QEMUMachine
*m
)
1036 pm
= &first_machine
;
1044 static QEMUMachine
*find_machine(const char *name
)
1048 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1049 if (!strcmp(m
->name
, name
))
1051 if (m
->alias
&& !strcmp(m
->alias
, name
))
1057 static QEMUMachine
*find_default_machine(void)
1061 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1062 if (m
->is_default
) {
1069 /***********************************************************/
1070 /* main execution loop */
1072 static void gui_update(void *opaque
)
1074 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1075 DisplayState
*ds
= opaque
;
1076 DisplayChangeListener
*dcl
= ds
->listeners
;
1078 qemu_flush_coalesced_mmio_buffer();
1081 while (dcl
!= NULL
) {
1082 if (dcl
->gui_timer_interval
&&
1083 dcl
->gui_timer_interval
< interval
)
1084 interval
= dcl
->gui_timer_interval
;
1087 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1090 static void nographic_update(void *opaque
)
1092 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1094 qemu_flush_coalesced_mmio_buffer();
1095 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1098 struct vm_change_state_entry
{
1099 VMChangeStateHandler
*cb
;
1101 QLIST_ENTRY (vm_change_state_entry
) entries
;
1104 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1106 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1109 VMChangeStateEntry
*e
;
1111 e
= qemu_mallocz(sizeof (*e
));
1115 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1119 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1121 QLIST_REMOVE (e
, entries
);
1125 void vm_state_notify(int running
, int reason
)
1127 VMChangeStateEntry
*e
;
1129 trace_vm_state_notify(running
, reason
);
1131 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1132 e
->cb(e
->opaque
, running
, reason
);
1141 vm_state_notify(1, 0);
1143 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1147 /* reset/shutdown handler */
1149 typedef struct QEMUResetEntry
{
1150 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1151 QEMUResetHandler
*func
;
1155 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1156 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1157 static int reset_requested
;
1158 static int shutdown_requested
, shutdown_signal
= -1;
1159 static pid_t shutdown_pid
;
1160 static int powerdown_requested
;
1161 static int debug_requested
;
1162 static int vmstop_requested
;
1164 int qemu_shutdown_requested(void)
1166 int r
= shutdown_requested
;
1167 shutdown_requested
= 0;
1171 void qemu_kill_report(void)
1173 if (shutdown_signal
!= -1) {
1174 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1175 if (shutdown_pid
== 0) {
1176 /* This happens for eg ^C at the terminal, so it's worth
1177 * avoiding printing an odd message in that case.
1179 fputc('\n', stderr
);
1181 fprintf(stderr
, " from pid %d\n", shutdown_pid
);
1183 shutdown_signal
= -1;
1187 int qemu_reset_requested(void)
1189 int r
= reset_requested
;
1190 reset_requested
= 0;
1194 int qemu_powerdown_requested(void)
1196 int r
= powerdown_requested
;
1197 powerdown_requested
= 0;
1201 static int qemu_debug_requested(void)
1203 int r
= debug_requested
;
1204 debug_requested
= 0;
1208 static int qemu_vmstop_requested(void)
1210 int r
= vmstop_requested
;
1211 vmstop_requested
= 0;
1215 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1217 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1220 re
->opaque
= opaque
;
1221 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1224 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1228 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1229 if (re
->func
== func
&& re
->opaque
== opaque
) {
1230 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1237 void qemu_system_reset(void)
1239 QEMUResetEntry
*re
, *nre
;
1241 /* reset all devices */
1242 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1243 re
->func(re
->opaque
);
1245 monitor_protocol_event(QEVENT_RESET
, NULL
);
1246 cpu_synchronize_all_post_reset();
1249 void qemu_system_reset_request(void)
1252 shutdown_requested
= 1;
1254 reset_requested
= 1;
1257 qemu_notify_event();
1260 void qemu_system_killed(int signal
, pid_t pid
)
1262 shutdown_signal
= signal
;
1264 qemu_system_shutdown_request();
1267 void qemu_system_shutdown_request(void)
1269 shutdown_requested
= 1;
1270 qemu_notify_event();
1273 void qemu_system_powerdown_request(void)
1275 powerdown_requested
= 1;
1276 qemu_notify_event();
1279 void qemu_system_debug_request(void)
1281 debug_requested
= 1;
1282 qemu_notify_event();
1285 void qemu_system_vmstop_request(int reason
)
1287 vmstop_requested
= reason
;
1288 qemu_notify_event();
1291 void main_loop_wait(int nonblocking
)
1293 fd_set rfds
, wfds
, xfds
;
1301 timeout
= qemu_calculate_timeout();
1302 qemu_bh_update_timeout(&timeout
);
1305 os_host_main_loop_wait(&timeout
);
1307 tv
.tv_sec
= timeout
/ 1000;
1308 tv
.tv_usec
= (timeout
% 1000) * 1000;
1310 /* poll any events */
1311 /* XXX: separate device handlers from system ones */
1316 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1317 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1319 qemu_mutex_unlock_iothread();
1320 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1321 qemu_mutex_lock_iothread();
1323 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1324 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1326 qemu_run_all_timers();
1328 /* Check bottom-halves last in case any of the earlier events triggered
1334 #ifndef CONFIG_IOTHREAD
1335 static int vm_request_pending(void)
1337 return powerdown_requested
||
1339 shutdown_requested
||
1345 qemu_irq qemu_system_powerdown
;
1347 static void main_loop(void)
1349 bool nonblocking
= false;
1350 #ifdef CONFIG_PROFILER
1355 qemu_main_loop_start();
1358 #ifndef CONFIG_IOTHREAD
1359 nonblocking
= cpu_exec_all();
1360 if (vm_request_pending()) {
1364 #ifdef CONFIG_PROFILER
1365 ti
= profile_getclock();
1367 main_loop_wait(nonblocking
);
1368 #ifdef CONFIG_PROFILER
1369 dev_time
+= profile_getclock() - ti
;
1372 if (qemu_debug_requested()) {
1373 vm_stop(VMSTOP_DEBUG
);
1375 if (qemu_shutdown_requested()) {
1377 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1379 vm_stop(VMSTOP_SHUTDOWN
);
1384 if (qemu_reset_requested()) {
1386 cpu_synchronize_all_states();
1387 qemu_system_reset();
1390 if (qemu_powerdown_requested()) {
1391 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1392 qemu_irq_raise(qemu_system_powerdown
);
1394 if ((r
= qemu_vmstop_requested())) {
1402 static void version(void)
1404 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1407 static void help(int exitcode
)
1409 const char *options_help
=
1410 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1412 #define DEFHEADING(text) stringify(text) "\n"
1413 #include "qemu-options.def"
1419 printf("usage: %s [options] [disk_image]\n"
1421 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1424 "During emulation, the following keys are useful:\n"
1425 "ctrl-alt-f toggle full screen\n"
1426 "ctrl-alt-n switch to virtual console 'n'\n"
1427 "ctrl-alt toggle mouse and keyboard grab\n"
1429 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1435 #define HAS_ARG 0x0001
1437 typedef struct QEMUOption
{
1444 static const QEMUOption qemu_options
[] = {
1445 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1446 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1447 { option, opt_arg, opt_enum, arch_mask },
1448 #define DEFHEADING(text)
1449 #include "qemu-options.def"
1455 static void select_vgahw (const char *p
)
1460 vga_interface_type
= VGA_NONE
;
1461 if (strstart(p
, "std", &opts
)) {
1462 vga_interface_type
= VGA_STD
;
1463 } else if (strstart(p
, "cirrus", &opts
)) {
1464 vga_interface_type
= VGA_CIRRUS
;
1465 } else if (strstart(p
, "vmware", &opts
)) {
1466 vga_interface_type
= VGA_VMWARE
;
1467 } else if (strstart(p
, "xenfb", &opts
)) {
1468 vga_interface_type
= VGA_XENFB
;
1469 } else if (strstart(p
, "qxl", &opts
)) {
1470 vga_interface_type
= VGA_QXL
;
1471 } else if (!strstart(p
, "none", &opts
)) {
1473 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1477 const char *nextopt
;
1479 if (strstart(opts
, ",retrace=", &nextopt
)) {
1481 if (strstart(opts
, "dumb", &nextopt
))
1482 vga_retrace_method
= VGA_RETRACE_DUMB
;
1483 else if (strstart(opts
, "precise", &nextopt
))
1484 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1485 else goto invalid_vga
;
1486 } else goto invalid_vga
;
1491 static DisplayType
select_display(const char *p
)
1494 DisplayType display
= DT_DEFAULT
;
1496 if (strstart(p
, "sdl", &opts
)) {
1500 const char *nextopt
;
1502 if (strstart(opts
, ",frame=", &nextopt
)) {
1504 if (strstart(opts
, "on", &nextopt
)) {
1506 } else if (strstart(opts
, "off", &nextopt
)) {
1509 goto invalid_sdl_args
;
1511 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1513 if (strstart(opts
, "on", &nextopt
)) {
1515 } else if (strstart(opts
, "off", &nextopt
)) {
1518 goto invalid_sdl_args
;
1520 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1522 if (strstart(opts
, "on", &nextopt
)) {
1524 } else if (strstart(opts
, "off", &nextopt
)) {
1527 goto invalid_sdl_args
;
1529 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1531 if (strstart(opts
, "on", &nextopt
)) {
1533 } else if (strstart(opts
, "off", &nextopt
)) {
1536 goto invalid_sdl_args
;
1540 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1546 fprintf(stderr
, "SDL support is disabled\n");
1549 } else if (strstart(p
, "vnc", &opts
)) {
1554 const char *nextopt
;
1556 if (strstart(opts
, "=", &nextopt
)) {
1557 vnc_display
= nextopt
;
1561 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1565 fprintf(stderr
, "VNC support is disabled\n");
1568 } else if (strstart(p
, "curses", &opts
)) {
1569 #ifdef CONFIG_CURSES
1570 display
= DT_CURSES
;
1572 fprintf(stderr
, "Curses support is disabled\n");
1575 } else if (strstart(p
, "none", &opts
)) {
1578 fprintf(stderr
, "Unknown display type: %s\n", p
);
1585 static int balloon_parse(const char *arg
)
1589 if (strcmp(arg
, "none") == 0) {
1593 if (!strncmp(arg
, "virtio", 6)) {
1594 if (arg
[6] == ',') {
1595 /* have params -> parse them */
1596 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1600 /* create empty opts */
1601 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1603 qemu_opt_set(opts
, "driver", "virtio-balloon");
1610 char *qemu_find_file(int type
, const char *name
)
1616 /* If name contains path separators then try it as a straight path. */
1617 if ((strchr(name
, '/') || strchr(name
, '\\'))
1618 && access(name
, R_OK
) == 0) {
1619 return qemu_strdup(name
);
1622 case QEMU_FILE_TYPE_BIOS
:
1625 case QEMU_FILE_TYPE_KEYMAP
:
1626 subdir
= "keymaps/";
1631 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1632 buf
= qemu_mallocz(len
);
1633 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1634 if (access(buf
, R_OK
)) {
1641 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1643 return qdev_device_help(opts
);
1646 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1650 dev
= qdev_device_add(opts
);
1656 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1658 CharDriverState
*chr
;
1660 chr
= qemu_chr_open_opts(opts
, NULL
);
1666 #ifdef CONFIG_VIRTFS
1667 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1670 ret
= qemu_fsdev_add(opts
);
1676 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1678 CharDriverState
*chr
;
1679 const char *chardev
;
1683 mode
= qemu_opt_get(opts
, "mode");
1687 if (strcmp(mode
, "readline") == 0) {
1688 flags
= MONITOR_USE_READLINE
;
1689 } else if (strcmp(mode
, "control") == 0) {
1690 flags
= MONITOR_USE_CONTROL
;
1692 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1696 if (qemu_opt_get_bool(opts
, "pretty", 0))
1697 flags
|= MONITOR_USE_PRETTY
;
1699 if (qemu_opt_get_bool(opts
, "default", 0))
1700 flags
|= MONITOR_IS_DEFAULT
;
1702 chardev
= qemu_opt_get(opts
, "chardev");
1703 chr
= qemu_chr_find(chardev
);
1705 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1709 monitor_init(chr
, flags
);
1713 static void monitor_parse(const char *optarg
, const char *mode
)
1715 static int monitor_device_index
= 0;
1721 if (strstart(optarg
, "chardev:", &p
)) {
1722 snprintf(label
, sizeof(label
), "%s", p
);
1724 snprintf(label
, sizeof(label
), "compat_monitor%d",
1725 monitor_device_index
);
1726 if (monitor_device_index
== 0) {
1729 opts
= qemu_chr_parse_compat(label
, optarg
);
1731 fprintf(stderr
, "parse error: %s\n", optarg
);
1736 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1738 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1741 qemu_opt_set(opts
, "mode", mode
);
1742 qemu_opt_set(opts
, "chardev", label
);
1744 qemu_opt_set(opts
, "default", "on");
1745 monitor_device_index
++;
1748 struct device_config
{
1750 DEV_USB
, /* -usbdevice */
1752 DEV_SERIAL
, /* -serial */
1753 DEV_PARALLEL
, /* -parallel */
1754 DEV_VIRTCON
, /* -virtioconsole */
1755 DEV_DEBUGCON
, /* -debugcon */
1757 const char *cmdline
;
1758 QTAILQ_ENTRY(device_config
) next
;
1760 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1762 static void add_device_config(int type
, const char *cmdline
)
1764 struct device_config
*conf
;
1766 conf
= qemu_mallocz(sizeof(*conf
));
1768 conf
->cmdline
= cmdline
;
1769 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1772 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1774 struct device_config
*conf
;
1777 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1778 if (conf
->type
!= type
)
1780 rc
= func(conf
->cmdline
);
1787 static int serial_parse(const char *devname
)
1789 static int index
= 0;
1792 if (strcmp(devname
, "none") == 0)
1794 if (index
== MAX_SERIAL_PORTS
) {
1795 fprintf(stderr
, "qemu: too many serial ports\n");
1798 snprintf(label
, sizeof(label
), "serial%d", index
);
1799 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1800 if (!serial_hds
[index
]) {
1801 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1802 devname
, strerror(errno
));
1809 static int parallel_parse(const char *devname
)
1811 static int index
= 0;
1814 if (strcmp(devname
, "none") == 0)
1816 if (index
== MAX_PARALLEL_PORTS
) {
1817 fprintf(stderr
, "qemu: too many parallel ports\n");
1820 snprintf(label
, sizeof(label
), "parallel%d", index
);
1821 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1822 if (!parallel_hds
[index
]) {
1823 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1824 devname
, strerror(errno
));
1831 static int virtcon_parse(const char *devname
)
1833 QemuOptsList
*device
= qemu_find_opts("device");
1834 static int index
= 0;
1836 QemuOpts
*bus_opts
, *dev_opts
;
1838 if (strcmp(devname
, "none") == 0)
1840 if (index
== MAX_VIRTIO_CONSOLES
) {
1841 fprintf(stderr
, "qemu: too many virtio consoles\n");
1845 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1846 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1848 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1849 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1851 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1852 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1853 if (!virtcon_hds
[index
]) {
1854 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1855 devname
, strerror(errno
));
1858 qemu_opt_set(dev_opts
, "chardev", label
);
1864 static int debugcon_parse(const char *devname
)
1868 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1871 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1873 fprintf(stderr
, "qemu: already have a debugcon device\n");
1876 qemu_opt_set(opts
, "driver", "isa-debugcon");
1877 qemu_opt_set(opts
, "chardev", "debugcon");
1881 static int tcg_init(void)
1887 const char *opt_name
;
1889 int (*available
)(void);
1893 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
1894 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
1895 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
1898 static int configure_accelerator(void)
1900 const char *p
= NULL
;
1903 bool accel_initalised
= 0;
1904 bool init_failed
= 0;
1906 QemuOptsList
*list
= qemu_find_opts("machine");
1907 if (!QTAILQ_EMPTY(&list
->head
)) {
1908 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
1912 /* Use the default "accelerator", tcg */
1916 while (!accel_initalised
&& *p
!= '\0') {
1920 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
1921 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
1922 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
1923 ret
= accel_list
[i
].init();
1926 if (!accel_list
[i
].available()) {
1927 printf("%s not supported for this target\n",
1928 accel_list
[i
].name
);
1930 fprintf(stderr
, "failed to initialize %s: %s\n",
1935 accel_initalised
= 1;
1936 *(accel_list
[i
].allowed
) = 1;
1941 if (i
== ARRAY_SIZE(accel_list
)) {
1942 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
1946 if (!accel_initalised
) {
1947 fprintf(stderr
, "No accelerator found!\n");
1952 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
1955 return !accel_initalised
;
1958 void qemu_add_exit_notifier(Notifier
*notify
)
1960 notifier_list_add(&exit_notifiers
, notify
);
1963 void qemu_remove_exit_notifier(Notifier
*notify
)
1965 notifier_list_remove(&exit_notifiers
, notify
);
1968 static void qemu_run_exit_notifiers(void)
1970 notifier_list_notify(&exit_notifiers
);
1973 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1975 notifier_list_add(&machine_init_done_notifiers
, notify
);
1978 static void qemu_run_machine_init_done_notifiers(void)
1980 notifier_list_notify(&machine_init_done_notifiers
);
1983 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1984 const char **poptarg
, int *poptind
)
1986 const QEMUOption
*popt
;
1987 int optind
= *poptind
;
1988 char *r
= argv
[optind
];
1991 loc_set_cmdline(argv
, optind
, 1);
1993 /* Treat --foo the same as -foo. */
1996 popt
= qemu_options
;
1999 error_report("invalid option");
2002 if (!strcmp(popt
->name
, r
+ 1))
2006 if (popt
->flags
& HAS_ARG
) {
2007 if (optind
>= argc
) {
2008 error_report("requires an argument");
2011 optarg
= argv
[optind
++];
2012 loc_set_cmdline(argv
, optind
- 2, 2);
2023 int main(int argc
, char **argv
, char **envp
)
2025 const char *gdbstub_dev
= NULL
;
2027 int snapshot
, linux_boot
;
2028 const char *icount_option
= NULL
;
2029 const char *initrd_filename
;
2030 const char *kernel_filename
, *kernel_cmdline
;
2031 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2033 DisplayChangeListener
*dcl
;
2034 int cyls
, heads
, secs
, translation
;
2035 QemuOpts
*hda_opts
= NULL
, *opts
;
2036 QemuOptsList
*olist
;
2039 const char *loadvm
= NULL
;
2040 QEMUMachine
*machine
;
2041 const char *cpu_model
;
2043 const char *pid_file
= NULL
;
2044 const char *incoming
= NULL
;
2046 int show_vnc_port
= 0;
2049 const char *trace_file
= NULL
;
2051 atexit(qemu_run_exit_notifiers
);
2052 error_set_progname(argv
[0]);
2056 qemu_cache_utils_init(envp
);
2058 QLIST_INIT (&vm_change_state_head
);
2059 os_setup_early_signal_handling();
2061 module_call_init(MODULE_INIT_MACHINE
);
2062 machine
= find_default_machine();
2064 initrd_filename
= NULL
;
2067 kernel_filename
= NULL
;
2068 kernel_cmdline
= "";
2069 cyls
= heads
= secs
= 0;
2070 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2072 for (i
= 0; i
< MAX_NODES
; i
++) {
2074 node_cpumask
[i
] = 0;
2083 /* first pass of option parsing */
2085 while (optind
< argc
) {
2086 if (argv
[optind
][0] != '-') {
2091 const QEMUOption
*popt
;
2093 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2094 switch (popt
->index
) {
2095 case QEMU_OPTION_nodefconfig
:
2105 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2106 if (ret
< 0 && ret
!= -ENOENT
) {
2110 ret
= qemu_read_config_file(arch_config_name
);
2111 if (ret
< 0 && ret
!= -ENOENT
) {
2117 /* second pass of option parsing */
2122 if (argv
[optind
][0] != '-') {
2123 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2125 const QEMUOption
*popt
;
2127 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2128 if (!(popt
->arch_mask
& arch_type
)) {
2129 printf("Option %s not supported for this target\n", popt
->name
);
2132 switch(popt
->index
) {
2134 machine
= find_machine(optarg
);
2137 printf("Supported machines are:\n");
2138 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2140 printf("%-10s %s (alias of %s)\n",
2141 m
->alias
, m
->desc
, m
->name
);
2142 printf("%-10s %s%s\n",
2144 m
->is_default
? " (default)" : "");
2146 exit(*optarg
!= '?');
2149 case QEMU_OPTION_cpu
:
2150 /* hw initialization will check this */
2151 if (*optarg
== '?') {
2152 list_cpus(stdout
, &fprintf
, optarg
);
2158 case QEMU_OPTION_initrd
:
2159 initrd_filename
= optarg
;
2161 case QEMU_OPTION_hda
:
2165 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2167 snprintf(buf
, sizeof(buf
),
2168 "%s,cyls=%d,heads=%d,secs=%d%s",
2169 HD_OPTS
, cyls
, heads
, secs
,
2170 translation
== BIOS_ATA_TRANSLATION_LBA
?
2172 translation
== BIOS_ATA_TRANSLATION_NONE
?
2173 ",trans=none" : "");
2174 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2177 case QEMU_OPTION_hdb
:
2178 case QEMU_OPTION_hdc
:
2179 case QEMU_OPTION_hdd
:
2180 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2183 case QEMU_OPTION_drive
:
2184 if (drive_def(optarg
) == NULL
) {
2188 case QEMU_OPTION_set
:
2189 if (qemu_set_option(optarg
) != 0)
2192 case QEMU_OPTION_global
:
2193 if (qemu_global_option(optarg
) != 0)
2196 case QEMU_OPTION_mtdblock
:
2197 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2199 case QEMU_OPTION_sd
:
2200 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2202 case QEMU_OPTION_pflash
:
2203 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2205 case QEMU_OPTION_snapshot
:
2208 case QEMU_OPTION_hdachs
:
2212 cyls
= strtol(p
, (char **)&p
, 0);
2213 if (cyls
< 1 || cyls
> 16383)
2218 heads
= strtol(p
, (char **)&p
, 0);
2219 if (heads
< 1 || heads
> 16)
2224 secs
= strtol(p
, (char **)&p
, 0);
2225 if (secs
< 1 || secs
> 63)
2229 if (!strcmp(p
, "none"))
2230 translation
= BIOS_ATA_TRANSLATION_NONE
;
2231 else if (!strcmp(p
, "lba"))
2232 translation
= BIOS_ATA_TRANSLATION_LBA
;
2233 else if (!strcmp(p
, "auto"))
2234 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2237 } else if (*p
!= '\0') {
2239 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2242 if (hda_opts
!= NULL
) {
2244 snprintf(num
, sizeof(num
), "%d", cyls
);
2245 qemu_opt_set(hda_opts
, "cyls", num
);
2246 snprintf(num
, sizeof(num
), "%d", heads
);
2247 qemu_opt_set(hda_opts
, "heads", num
);
2248 snprintf(num
, sizeof(num
), "%d", secs
);
2249 qemu_opt_set(hda_opts
, "secs", num
);
2250 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2251 qemu_opt_set(hda_opts
, "trans", "lba");
2252 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2253 qemu_opt_set(hda_opts
, "trans", "none");
2257 case QEMU_OPTION_numa
:
2258 if (nb_numa_nodes
>= MAX_NODES
) {
2259 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2264 case QEMU_OPTION_display
:
2265 display_type
= select_display(optarg
);
2267 case QEMU_OPTION_nographic
:
2268 display_type
= DT_NOGRAPHIC
;
2270 case QEMU_OPTION_curses
:
2271 #ifdef CONFIG_CURSES
2272 display_type
= DT_CURSES
;
2274 fprintf(stderr
, "Curses support is disabled\n");
2278 case QEMU_OPTION_portrait
:
2281 case QEMU_OPTION_kernel
:
2282 kernel_filename
= optarg
;
2284 case QEMU_OPTION_append
:
2285 kernel_cmdline
= optarg
;
2287 case QEMU_OPTION_cdrom
:
2288 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2290 case QEMU_OPTION_boot
:
2292 static const char * const params
[] = {
2293 "order", "once", "menu", NULL
2295 char buf
[sizeof(boot_devices
)];
2296 char *standard_boot_devices
;
2299 if (!strchr(optarg
, '=')) {
2301 pstrcpy(buf
, sizeof(buf
), optarg
);
2302 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2304 "qemu: unknown boot parameter '%s' in '%s'\n",
2310 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2311 validate_bootdevices(buf
);
2312 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2315 if (get_param_value(buf
, sizeof(buf
),
2317 validate_bootdevices(buf
);
2318 standard_boot_devices
= qemu_strdup(boot_devices
);
2319 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2320 qemu_register_reset(restore_boot_devices
,
2321 standard_boot_devices
);
2323 if (get_param_value(buf
, sizeof(buf
),
2325 if (!strcmp(buf
, "on")) {
2327 } else if (!strcmp(buf
, "off")) {
2331 "qemu: invalid option value '%s'\n",
2339 case QEMU_OPTION_fda
:
2340 case QEMU_OPTION_fdb
:
2341 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2344 case QEMU_OPTION_no_fd_bootchk
:
2347 case QEMU_OPTION_netdev
:
2348 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2352 case QEMU_OPTION_net
:
2353 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2358 case QEMU_OPTION_tftp
:
2359 legacy_tftp_prefix
= optarg
;
2361 case QEMU_OPTION_bootp
:
2362 legacy_bootp_filename
= optarg
;
2364 case QEMU_OPTION_redir
:
2365 if (net_slirp_redir(optarg
) < 0)
2369 case QEMU_OPTION_bt
:
2370 add_device_config(DEV_BT
, optarg
);
2372 case QEMU_OPTION_audio_help
:
2373 if (!(audio_available())) {
2374 printf("Option %s not supported for this target\n", popt
->name
);
2380 case QEMU_OPTION_soundhw
:
2381 if (!(audio_available())) {
2382 printf("Option %s not supported for this target\n", popt
->name
);
2385 select_soundhw (optarg
);
2390 case QEMU_OPTION_version
:
2394 case QEMU_OPTION_m
: {
2397 value
= strtosz(optarg
, NULL
);
2399 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2403 /* On 32-bit hosts, QEMU is limited by virtual address space */
2404 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2405 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2408 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2409 fprintf(stderr
, "qemu: ram size too large\n");
2415 case QEMU_OPTION_mempath
:
2419 case QEMU_OPTION_mem_prealloc
:
2424 set_cpu_log(optarg
);
2427 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2429 case QEMU_OPTION_gdb
:
2430 gdbstub_dev
= optarg
;
2435 case QEMU_OPTION_bios
:
2438 case QEMU_OPTION_singlestep
:
2445 keyboard_layout
= optarg
;
2447 case QEMU_OPTION_localtime
:
2450 case QEMU_OPTION_vga
:
2451 select_vgahw (optarg
);
2458 w
= strtol(p
, (char **)&p
, 10);
2461 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2467 h
= strtol(p
, (char **)&p
, 10);
2472 depth
= strtol(p
, (char **)&p
, 10);
2473 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2474 depth
!= 24 && depth
!= 32)
2476 } else if (*p
== '\0') {
2477 depth
= graphic_depth
;
2484 graphic_depth
= depth
;
2487 case QEMU_OPTION_echr
:
2490 term_escape_char
= strtol(optarg
, &r
, 0);
2492 printf("Bad argument to echr\n");
2495 case QEMU_OPTION_monitor
:
2496 monitor_parse(optarg
, "readline");
2497 default_monitor
= 0;
2499 case QEMU_OPTION_qmp
:
2500 monitor_parse(optarg
, "control");
2501 default_monitor
= 0;
2503 case QEMU_OPTION_mon
:
2504 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2508 default_monitor
= 0;
2510 case QEMU_OPTION_chardev
:
2511 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2516 case QEMU_OPTION_fsdev
:
2517 olist
= qemu_find_opts("fsdev");
2519 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2522 opts
= qemu_opts_parse(olist
, optarg
, 1);
2524 fprintf(stderr
, "parse error: %s\n", optarg
);
2528 case QEMU_OPTION_virtfs
: {
2532 olist
= qemu_find_opts("virtfs");
2534 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2537 opts
= qemu_opts_parse(olist
, optarg
, 1);
2539 fprintf(stderr
, "parse error: %s\n", optarg
);
2543 if (qemu_opt_get(opts
, "fstype") == NULL
||
2544 qemu_opt_get(opts
, "mount_tag") == NULL
||
2545 qemu_opt_get(opts
, "path") == NULL
||
2546 qemu_opt_get(opts
, "security_model") == NULL
) {
2547 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2548 "security_model=[mapped|passthrough|none],"
2549 "mount_tag=tag.\n");
2553 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2554 qemu_opt_get(opts
, "mount_tag"), 1);
2556 fprintf(stderr
, "duplicate fsdev id: %s\n",
2557 qemu_opt_get(opts
, "mount_tag"));
2560 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2561 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2562 qemu_opt_set(fsdev
, "security_model",
2563 qemu_opt_get(opts
, "security_model"));
2565 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2566 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2567 qemu_opt_set(device
, "fsdev",
2568 qemu_opt_get(opts
, "mount_tag"));
2569 qemu_opt_set(device
, "mount_tag",
2570 qemu_opt_get(opts
, "mount_tag"));
2573 case QEMU_OPTION_serial
:
2574 add_device_config(DEV_SERIAL
, optarg
);
2576 if (strncmp(optarg
, "mon:", 4) == 0) {
2577 default_monitor
= 0;
2580 case QEMU_OPTION_watchdog
:
2583 "qemu: only one watchdog option may be given\n");
2588 case QEMU_OPTION_watchdog_action
:
2589 if (select_watchdog_action(optarg
) == -1) {
2590 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2594 case QEMU_OPTION_virtiocon
:
2595 add_device_config(DEV_VIRTCON
, optarg
);
2596 default_virtcon
= 0;
2597 if (strncmp(optarg
, "mon:", 4) == 0) {
2598 default_monitor
= 0;
2601 case QEMU_OPTION_parallel
:
2602 add_device_config(DEV_PARALLEL
, optarg
);
2603 default_parallel
= 0;
2604 if (strncmp(optarg
, "mon:", 4) == 0) {
2605 default_monitor
= 0;
2608 case QEMU_OPTION_debugcon
:
2609 add_device_config(DEV_DEBUGCON
, optarg
);
2611 case QEMU_OPTION_loadvm
:
2614 case QEMU_OPTION_full_screen
:
2618 case QEMU_OPTION_no_frame
:
2621 case QEMU_OPTION_alt_grab
:
2624 case QEMU_OPTION_ctrl_grab
:
2627 case QEMU_OPTION_no_quit
:
2630 case QEMU_OPTION_sdl
:
2631 display_type
= DT_SDL
;
2634 case QEMU_OPTION_no_frame
:
2635 case QEMU_OPTION_alt_grab
:
2636 case QEMU_OPTION_ctrl_grab
:
2637 case QEMU_OPTION_no_quit
:
2638 case QEMU_OPTION_sdl
:
2639 fprintf(stderr
, "SDL support is disabled\n");
2642 case QEMU_OPTION_pidfile
:
2645 case QEMU_OPTION_win2k_hack
:
2646 win2k_install_hack
= 1;
2648 case QEMU_OPTION_rtc_td_hack
:
2651 case QEMU_OPTION_acpitable
:
2652 do_acpitable_option(optarg
);
2654 case QEMU_OPTION_smbios
:
2655 do_smbios_option(optarg
);
2657 case QEMU_OPTION_enable_kvm
:
2658 olist
= qemu_find_opts("machine");
2659 qemu_opts_reset(olist
);
2660 qemu_opts_parse(olist
, "accel=kvm", 0);
2662 case QEMU_OPTION_machine
:
2663 olist
= qemu_find_opts("machine");
2664 qemu_opts_reset(olist
);
2665 opts
= qemu_opts_parse(olist
, optarg
, 0);
2667 fprintf(stderr
, "parse error: %s\n", optarg
);
2671 case QEMU_OPTION_usb
:
2674 case QEMU_OPTION_usbdevice
:
2676 add_device_config(DEV_USB
, optarg
);
2678 case QEMU_OPTION_device
:
2679 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2683 case QEMU_OPTION_smp
:
2686 fprintf(stderr
, "Invalid number of CPUs\n");
2689 if (max_cpus
< smp_cpus
) {
2690 fprintf(stderr
, "maxcpus must be equal to or greater than "
2694 if (max_cpus
> 255) {
2695 fprintf(stderr
, "Unsupported number of maxcpus\n");
2699 case QEMU_OPTION_vnc
:
2702 vnc_display
= optarg
;
2704 fprintf(stderr
, "VNC support is disabled\n");
2708 case QEMU_OPTION_no_acpi
:
2711 case QEMU_OPTION_no_hpet
:
2714 case QEMU_OPTION_balloon
:
2715 if (balloon_parse(optarg
) < 0) {
2716 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2720 case QEMU_OPTION_no_reboot
:
2723 case QEMU_OPTION_no_shutdown
:
2726 case QEMU_OPTION_show_cursor
:
2729 case QEMU_OPTION_uuid
:
2730 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2731 fprintf(stderr
, "Fail to parse UUID string."
2732 " Wrong format.\n");
2736 case QEMU_OPTION_option_rom
:
2737 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2738 fprintf(stderr
, "Too many option ROMs\n");
2741 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2742 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2743 option_rom
[nb_option_roms
].bootindex
=
2744 qemu_opt_get_number(opts
, "bootindex", -1);
2745 if (!option_rom
[nb_option_roms
].name
) {
2746 fprintf(stderr
, "Option ROM file is not specified\n");
2751 case QEMU_OPTION_semihosting
:
2752 semihosting_enabled
= 1;
2754 case QEMU_OPTION_name
:
2755 qemu_name
= qemu_strdup(optarg
);
2757 char *p
= strchr(qemu_name
, ',');
2760 if (strncmp(p
, "process=", 8)) {
2761 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2765 os_set_proc_name(p
);
2769 case QEMU_OPTION_prom_env
:
2770 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2771 fprintf(stderr
, "Too many prom variables\n");
2774 prom_envs
[nb_prom_envs
] = optarg
;
2777 case QEMU_OPTION_old_param
:
2780 case QEMU_OPTION_clock
:
2781 configure_alarms(optarg
);
2783 case QEMU_OPTION_startdate
:
2784 configure_rtc_date_offset(optarg
, 1);
2786 case QEMU_OPTION_rtc
:
2787 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2791 configure_rtc(opts
);
2793 case QEMU_OPTION_tb_size
:
2794 tb_size
= strtol(optarg
, NULL
, 0);
2798 case QEMU_OPTION_icount
:
2799 icount_option
= optarg
;
2801 case QEMU_OPTION_incoming
:
2803 incoming_expected
= true;
2805 case QEMU_OPTION_nodefaults
:
2807 default_parallel
= 0;
2808 default_virtcon
= 0;
2809 default_monitor
= 0;
2816 case QEMU_OPTION_xen_domid
:
2817 if (!(xen_available())) {
2818 printf("Option %s not supported for this target\n", popt
->name
);
2821 xen_domid
= atoi(optarg
);
2823 case QEMU_OPTION_xen_create
:
2824 if (!(xen_available())) {
2825 printf("Option %s not supported for this target\n", popt
->name
);
2828 xen_mode
= XEN_CREATE
;
2830 case QEMU_OPTION_xen_attach
:
2831 if (!(xen_available())) {
2832 printf("Option %s not supported for this target\n", popt
->name
);
2835 xen_mode
= XEN_ATTACH
;
2837 #ifdef CONFIG_SIMPLE_TRACE
2838 case QEMU_OPTION_trace
:
2839 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2841 trace_file
= qemu_opt_get(opts
, "file");
2845 case QEMU_OPTION_readconfig
:
2847 int ret
= qemu_read_config_file(optarg
);
2849 fprintf(stderr
, "read config %s: %s\n", optarg
,
2855 case QEMU_OPTION_spice
:
2856 olist
= qemu_find_opts("spice");
2858 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2861 opts
= qemu_opts_parse(olist
, optarg
, 0);
2863 fprintf(stderr
, "parse error: %s\n", optarg
);
2867 case QEMU_OPTION_writeconfig
:
2870 if (strcmp(optarg
, "-") == 0) {
2873 fp
= fopen(optarg
, "w");
2875 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2879 qemu_config_write(fp
);
2884 os_parse_cmd_args(popt
->index
, optarg
);
2890 if (!st_init(trace_file
)) {
2891 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2894 /* If no data_dir is specified then try to find it relative to the
2897 data_dir
= os_find_datadir(argv
[0]);
2899 /* If all else fails use the install patch specified when building. */
2901 data_dir
= CONFIG_QEMU_DATADIR
;
2905 * Default to max_cpus = smp_cpus, in case the user doesn't
2906 * specify a max_cpus value.
2909 max_cpus
= smp_cpus
;
2911 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2912 if (smp_cpus
> machine
->max_cpus
) {
2913 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2914 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2920 * Get the default machine options from the machine if it is not already
2921 * specified either by the configuration file or by the command line.
2923 if (machine
->default_machine_opts
) {
2924 QemuOptsList
*list
= qemu_find_opts("machine");
2925 const char *p
= NULL
;
2927 if (!QTAILQ_EMPTY(&list
->head
)) {
2928 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2931 opts
= qemu_opts_parse(qemu_find_opts("machine"),
2932 machine
->default_machine_opts
, 0);
2934 fprintf(stderr
, "parse error for machine %s: %s\n",
2935 machine
->name
, machine
->default_machine_opts
);
2941 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
2942 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
2944 if (machine
->no_serial
) {
2947 if (machine
->no_parallel
) {
2948 default_parallel
= 0;
2950 if (!machine
->use_virtcon
) {
2951 default_virtcon
= 0;
2953 if (machine
->no_vga
) {
2956 if (machine
->no_floppy
) {
2959 if (machine
->no_cdrom
) {
2962 if (machine
->no_sdcard
) {
2966 if (display_type
== DT_NOGRAPHIC
) {
2967 if (default_parallel
)
2968 add_device_config(DEV_PARALLEL
, "null");
2969 if (default_serial
&& default_monitor
) {
2970 add_device_config(DEV_SERIAL
, "mon:stdio");
2971 } else if (default_virtcon
&& default_monitor
) {
2972 add_device_config(DEV_VIRTCON
, "mon:stdio");
2975 add_device_config(DEV_SERIAL
, "stdio");
2976 if (default_virtcon
)
2977 add_device_config(DEV_VIRTCON
, "stdio");
2978 if (default_monitor
)
2979 monitor_parse("stdio", "readline");
2983 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2984 if (default_parallel
)
2985 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2986 if (default_monitor
)
2987 monitor_parse("vc:80Cx24C", "readline");
2988 if (default_virtcon
)
2989 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2992 vga_interface_type
= VGA_CIRRUS
;
2996 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
2998 #ifdef CONFIG_VIRTFS
2999 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3006 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3011 configure_accelerator();
3013 if (qemu_init_main_loop()) {
3014 fprintf(stderr
, "qemu_init_main_loop failed\n");
3017 linux_boot
= (kernel_filename
!= NULL
);
3019 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3020 fprintf(stderr
, "-append only allowed with -kernel option\n");
3024 if (!linux_boot
&& initrd_filename
!= NULL
) {
3025 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3029 os_set_line_buffering();
3031 if (init_timer_alarm() < 0) {
3032 fprintf(stderr
, "could not initialize alarm timer\n");
3035 configure_icount(icount_option
);
3037 if (net_init_clients() < 0) {
3041 /* init the bluetooth world */
3042 if (foreach_device_config(DEV_BT
, bt_parse
))
3045 /* init the memory */
3047 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3049 /* init the dynamic translator */
3050 cpu_exec_init_all(tb_size
* 1024 * 1024);
3052 bdrv_init_with_whitelist();
3056 /* open the virtual block devices */
3058 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3059 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3062 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3063 IF_DEFAULT
, 2, CDROM_OPTS
);
3064 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3065 IF_FLOPPY
, 0, FD_OPTS
);
3066 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3069 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3072 if (nb_numa_nodes
> 0) {
3075 if (nb_numa_nodes
> smp_cpus
) {
3076 nb_numa_nodes
= smp_cpus
;
3079 /* If no memory size if given for any node, assume the default case
3080 * and distribute the available memory equally across all nodes
3082 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3083 if (node_mem
[i
] != 0)
3086 if (i
== nb_numa_nodes
) {
3087 uint64_t usedmem
= 0;
3089 /* On Linux, the each node's border has to be 8MB aligned,
3090 * the final node gets the rest.
3092 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3093 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3094 usedmem
+= node_mem
[i
];
3096 node_mem
[i
] = ram_size
- usedmem
;
3099 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3100 if (node_cpumask
[i
] != 0)
3103 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3104 * must cope with this anyway, because there are BIOSes out there in
3105 * real machines which also use this scheme.
3107 if (i
== nb_numa_nodes
) {
3108 for (i
= 0; i
< smp_cpus
; i
++) {
3109 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3114 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3118 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3120 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3122 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3124 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3127 module_call_init(MODULE_INIT_DEVICE
);
3129 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3133 i
= select_watchdog(watchdog
);
3135 exit (i
== 1 ? 1 : 0);
3138 if (machine
->compat_props
) {
3139 qdev_prop_register_global_list(machine
->compat_props
);
3143 machine
->init(ram_size
, boot_devices
,
3144 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3146 cpu_synchronize_all_post_init();
3150 current_machine
= machine
;
3152 /* init USB devices */
3154 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3158 /* init generic devices */
3159 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3162 net_check_clients();
3164 /* just use the first displaystate for the moment */
3165 ds
= get_displaystate();
3169 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3170 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3171 display_type
= DT_SDL
;
3172 #elif defined(CONFIG_VNC)
3173 vnc_display
= "localhost:0,to=99";
3176 display_type
= DT_NONE
;
3181 /* init local displays */
3182 switch (display_type
) {
3185 #if defined(CONFIG_CURSES)
3187 curses_display_init(ds
, full_screen
);
3190 #if defined(CONFIG_SDL)
3192 sdl_display_init(ds
, full_screen
, no_frame
);
3194 #elif defined(CONFIG_COCOA)
3196 cocoa_display_init(ds
, full_screen
);
3203 /* must be after terminal init, SDL library changes signal handlers */
3204 os_setup_signal_handling();
3207 /* init remote displays */
3209 vnc_display_init(ds
);
3210 if (vnc_display_open(ds
, vnc_display
) < 0)
3213 if (show_vnc_port
) {
3214 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3219 if (using_spice
&& !qxl_enabled
) {
3220 qemu_spice_display_init(ds
);
3226 dcl
= ds
->listeners
;
3227 while (dcl
!= NULL
) {
3228 if (dcl
->dpy_refresh
!= NULL
) {
3229 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3230 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3235 if (ds
->gui_timer
== NULL
) {
3236 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3237 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3239 text_consoles_set_display(ds
);
3241 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3242 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3247 qdev_machine_creation_done();
3249 if (rom_load_all() != 0) {
3250 fprintf(stderr
, "rom loading failed\n");
3254 /* TODO: once all bus devices are qdevified, this should be done
3255 * when bus is created by qdev.c */
3256 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3257 qemu_run_machine_init_done_notifiers();
3259 qemu_system_reset();
3261 if (load_vmstate(loadvm
) < 0) {
3267 int ret
= qemu_start_incoming_migration(incoming
);
3269 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3273 } else if (autostart
) {