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"
148 #include "hw/device-assignment.h"
150 #include "fsdev/qemu-fsdev.h"
155 #include "qemu_socket.h"
157 #include "slirp/libslirp.h"
160 #include "simpletrace.h"
161 #include "qemu-queue.h"
163 #include "arch_init.h"
165 #include "ui/qemu-spice.h"
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir
;
175 const char *bios_name
= NULL
;
176 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
177 DisplayType display_type
= DT_DEFAULT
;
178 int display_remote
= 0;
179 const char* keyboard_layout
= NULL
;
181 const char *mem_path
= NULL
;
183 int mem_prealloc
= 0; /* force preallocation of physical target memory */
186 NICInfo nd_table
[MAX_NICS
];
189 int incoming_expected
; /* Started with -incoming and waiting for incoming */
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
194 static int full_screen
= 0;
196 static int no_frame
= 0;
199 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
200 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
201 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
202 int win2k_install_hack
= 0;
211 const char *vnc_display
;
213 int acpi_enabled
= 1;
219 int graphic_rotate
= 0;
220 const char *watchdog
;
221 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
224 int time_drift_fix
= 0;
225 unsigned int kvm_shadow_memory
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
232 const char *nvram
= NULL
;
235 typedef struct FWBootEntry FWBootEntry
;
238 QTAILQ_ENTRY(FWBootEntry
) link
;
244 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
247 uint64_t node_mem
[MAX_NODES
];
248 uint64_t node_cpumask
[MAX_NODES
];
250 static QEMUTimer
*nographic_timer
;
252 uint8_t qemu_uuid
[16];
254 static QEMUBootSetHandler
*boot_set_handler
;
255 static void *boot_set_opaque
;
257 static NotifierList exit_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
260 static NotifierList machine_init_done_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
263 static int tcg_allowed
= 1;
267 enum xen_mode xen_mode
= XEN_EMULATE
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_vga
= 1;
274 static int default_floppy
= 1;
275 static int default_cdrom
= 1;
276 static int default_sdcard
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "isa-fdc", .flag
= &default_floppy
},
285 { .driver
= "ide-cd", .flag
= &default_cdrom
},
286 { .driver
= "ide-hd", .flag
= &default_cdrom
},
287 { .driver
= "ide-drive", .flag
= &default_cdrom
},
288 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
289 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
292 { .driver
= "VGA", .flag
= &default_vga
},
293 { .driver
= "cirrus-vga", .flag
= &default_vga
},
294 { .driver
= "vmware-svga", .flag
= &default_vga
},
295 { .driver
= "isa-vga", .flag
= &default_vga
},
296 { .driver
= "qxl-vga", .flag
= &default_vga
},
299 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
301 const char *driver
= qemu_opt_get(opts
, "driver");
306 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
307 if (strcmp(default_list
[i
].driver
, driver
) != 0)
309 *(default_list
[i
].flag
) = 0;
314 /***********************************************************/
315 /* real time host monotonic timer */
317 /***********************************************************/
318 /* host time/date access */
319 void qemu_get_timedate(struct tm
*tm
, int offset
)
326 if (rtc_date_offset
== -1) {
330 ret
= localtime(&ti
);
332 ti
-= rtc_date_offset
;
336 memcpy(tm
, ret
, sizeof(struct tm
));
339 int qemu_timedate_diff(struct tm
*tm
)
343 if (rtc_date_offset
== -1)
345 seconds
= mktimegm(tm
);
347 seconds
= mktime(tm
);
349 seconds
= mktimegm(tm
) + rtc_date_offset
;
351 return seconds
- time(NULL
);
354 void rtc_change_mon_event(struct tm
*tm
)
358 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
359 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
360 qobject_decref(data
);
363 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
365 time_t rtc_start_date
;
368 if (!strcmp(startdate
, "now") && legacy
) {
369 rtc_date_offset
= -1;
371 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
379 } else if (sscanf(startdate
, "%d-%d-%d",
391 rtc_start_date
= mktimegm(&tm
);
392 if (rtc_start_date
== -1) {
394 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
395 "'2006-06-17T16:01:21' or '2006-06-17'\n");
398 rtc_date_offset
= time(NULL
) - rtc_start_date
;
402 static void configure_rtc(QemuOpts
*opts
)
406 value
= qemu_opt_get(opts
, "base");
408 if (!strcmp(value
, "utc")) {
410 } else if (!strcmp(value
, "localtime")) {
413 configure_rtc_date_offset(value
, 0);
416 value
= qemu_opt_get(opts
, "clock");
418 if (!strcmp(value
, "host")) {
419 rtc_clock
= host_clock
;
420 } else if (!strcmp(value
, "vm")) {
421 rtc_clock
= vm_clock
;
423 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
427 value
= qemu_opt_get(opts
, "driftfix");
429 if (!strcmp(value
, "slew")) {
431 } else if (!strcmp(value
, "none")) {
434 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
440 /***********************************************************/
441 /* Bluetooth support */
444 static struct HCIInfo
*hci_table
[MAX_NICS
];
446 static struct bt_vlan_s
{
447 struct bt_scatternet_s net
;
449 struct bt_vlan_s
*next
;
452 /* find or alloc a new bluetooth "VLAN" */
453 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
455 struct bt_vlan_s
**pvlan
, *vlan
;
456 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
460 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
462 pvlan
= &first_bt_vlan
;
463 while (*pvlan
!= NULL
)
464 pvlan
= &(*pvlan
)->next
;
469 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
473 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
478 static struct HCIInfo null_hci
= {
479 .cmd_send
= null_hci_send
,
480 .sco_send
= null_hci_send
,
481 .acl_send
= null_hci_send
,
482 .bdaddr_set
= null_hci_addr_set
,
485 struct HCIInfo
*qemu_next_hci(void)
487 if (cur_hci
== nb_hcis
)
490 return hci_table
[cur_hci
++];
493 static struct HCIInfo
*hci_init(const char *str
)
496 struct bt_scatternet_s
*vlan
= 0;
498 if (!strcmp(str
, "null"))
501 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
503 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
504 else if (!strncmp(str
, "hci", 3)) {
507 if (!strncmp(str
+ 3, ",vlan=", 6)) {
508 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
513 vlan
= qemu_find_bt_vlan(0);
515 return bt_new_hci(vlan
);
518 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
523 static int bt_hci_parse(const char *str
)
528 if (nb_hcis
>= MAX_NICS
) {
529 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
542 bdaddr
.b
[5] = 0x56 + nb_hcis
;
543 hci
->bdaddr_set(hci
, bdaddr
.b
);
545 hci_table
[nb_hcis
++] = hci
;
550 static void bt_vhci_add(int vlan_id
)
552 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
555 fprintf(stderr
, "qemu: warning: adding a VHCI to "
556 "an empty scatternet %i\n", vlan_id
);
558 bt_vhci_init(bt_new_hci(vlan
));
561 static struct bt_device_s
*bt_device_add(const char *opt
)
563 struct bt_scatternet_s
*vlan
;
565 char *endp
= strstr(opt
, ",vlan=");
566 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
569 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
572 vlan_id
= strtol(endp
+ 6, &endp
, 0);
574 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
579 vlan
= qemu_find_bt_vlan(vlan_id
);
582 fprintf(stderr
, "qemu: warning: adding a slave device to "
583 "an empty scatternet %i\n", vlan_id
);
585 if (!strcmp(devname
, "keyboard"))
586 return bt_keyboard_init(vlan
);
588 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
592 static int bt_parse(const char *opt
)
594 const char *endp
, *p
;
597 if (strstart(opt
, "hci", &endp
)) {
598 if (!*endp
|| *endp
== ',') {
600 if (!strstart(endp
, ",vlan=", 0))
603 return bt_hci_parse(opt
);
605 } else if (strstart(opt
, "vhci", &endp
)) {
606 if (!*endp
|| *endp
== ',') {
608 if (strstart(endp
, ",vlan=", &p
)) {
609 vlan
= strtol(p
, (char **) &endp
, 0);
611 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
615 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
624 } else if (strstart(opt
, "device:", &endp
))
625 return !bt_device_add(endp
);
627 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
631 /***********************************************************/
632 /* QEMU Block devices */
634 #define HD_OPTS "media=disk"
635 #define CDROM_OPTS "media=cdrom"
637 #define PFLASH_OPTS ""
641 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
643 int *use_scsi
= opaque
;
645 return drive_init(opts
, *use_scsi
) == NULL
;
648 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
650 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
651 qemu_opt_set(opts
, "snapshot", "on");
656 static void default_drive(int enable
, int snapshot
, int use_scsi
,
657 BlockInterfaceType type
, int index
,
662 if (type
== IF_DEFAULT
) {
663 type
= use_scsi
? IF_SCSI
: IF_IDE
;
666 if (!enable
|| drive_get_by_index(type
, index
)) {
670 opts
= drive_add(type
, index
, NULL
, optstr
);
672 drive_enable_snapshot(opts
, NULL
);
674 if (!drive_init(opts
, use_scsi
)) {
679 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
681 boot_set_handler
= func
;
682 boot_set_opaque
= opaque
;
685 int qemu_boot_set(const char *boot_devices
)
687 if (!boot_set_handler
) {
690 return boot_set_handler(boot_set_opaque
, boot_devices
);
693 static void validate_bootdevices(char *devices
)
695 /* We just do some generic consistency checks */
699 for (p
= devices
; *p
!= '\0'; p
++) {
700 /* Allowed boot devices are:
701 * a-b: floppy disk drives
702 * c-f: IDE disk drives
703 * g-m: machine implementation dependant drives
704 * n-p: network devices
705 * It's up to each machine implementation to check if the given boot
706 * devices match the actual hardware implementation and firmware
709 if (*p
< 'a' || *p
> 'p') {
710 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
713 if (bitmap
& (1 << (*p
- 'a'))) {
714 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
717 bitmap
|= 1 << (*p
- 'a');
721 static void restore_boot_devices(void *opaque
)
723 char *standard_boot_devices
= opaque
;
724 static int first
= 1;
726 /* Restore boot order and remove ourselves after the first boot */
732 qemu_boot_set(standard_boot_devices
);
734 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
735 qemu_free(standard_boot_devices
);
738 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
741 FWBootEntry
*node
, *i
;
747 assert(dev
!= NULL
|| suffix
!= NULL
);
749 node
= qemu_mallocz(sizeof(FWBootEntry
));
750 node
->bootindex
= bootindex
;
751 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
754 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
755 if (i
->bootindex
== bootindex
) {
756 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
758 } else if (i
->bootindex
< bootindex
) {
761 QTAILQ_INSERT_BEFORE(i
, node
, link
);
764 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
768 * This function returns null terminated string that consist of new line
769 * separated device paths.
771 * memory pointed by "size" is assigned total length of the array in bytes
774 char *get_boot_devices_list(uint32_t *size
)
780 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
781 char *devpath
= NULL
, *bootpath
;
785 devpath
= qdev_get_fw_dev_path(i
->dev
);
789 if (i
->suffix
&& devpath
) {
790 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
792 bootpath
= qemu_malloc(bootpathlen
);
793 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
795 } else if (devpath
) {
798 bootpath
= qemu_strdup(i
->suffix
);
803 list
[total
-1] = '\n';
805 len
= strlen(bootpath
) + 1;
806 list
= qemu_realloc(list
, total
+ len
);
807 memcpy(&list
[total
], bootpath
, len
);
817 static void numa_add(const char *optarg
)
821 unsigned long long value
, endvalue
;
824 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
825 if (!strcmp(option
, "node")) {
826 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
827 nodenr
= nb_numa_nodes
;
829 nodenr
= strtoull(option
, NULL
, 10);
832 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
833 node_mem
[nodenr
] = 0;
836 sval
= strtosz(option
, NULL
);
838 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
841 node_mem
[nodenr
] = sval
;
843 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
844 node_cpumask
[nodenr
] = 0;
846 value
= strtoull(option
, &endptr
, 10);
849 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
851 if (*endptr
== '-') {
852 endvalue
= strtoull(endptr
+1, &endptr
, 10);
853 if (endvalue
>= 63) {
856 "only 63 CPUs in NUMA mode supported.\n");
858 value
= (2ULL << endvalue
) - (1ULL << value
);
860 value
= 1ULL << value
;
863 node_cpumask
[nodenr
] = value
;
870 static void smp_parse(const char *optarg
)
872 int smp
, sockets
= 0, threads
= 0, cores
= 0;
876 smp
= strtoul(optarg
, &endptr
, 10);
877 if (endptr
!= optarg
) {
878 if (*endptr
== ',') {
882 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
883 sockets
= strtoull(option
, NULL
, 10);
884 if (get_param_value(option
, 128, "cores", endptr
) != 0)
885 cores
= strtoull(option
, NULL
, 10);
886 if (get_param_value(option
, 128, "threads", endptr
) != 0)
887 threads
= strtoull(option
, NULL
, 10);
888 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
889 max_cpus
= strtoull(option
, NULL
, 10);
891 /* compute missing values, prefer sockets over cores over threads */
892 if (smp
== 0 || sockets
== 0) {
893 sockets
= sockets
> 0 ? sockets
: 1;
894 cores
= cores
> 0 ? cores
: 1;
895 threads
= threads
> 0 ? threads
: 1;
897 smp
= cores
* threads
* sockets
;
901 threads
= threads
> 0 ? threads
: 1;
902 cores
= smp
/ (sockets
* threads
);
904 threads
= smp
/ (cores
* sockets
);
908 smp_cores
= cores
> 0 ? cores
: 1;
909 smp_threads
= threads
> 0 ? threads
: 1;
914 /***********************************************************/
917 static int usb_device_add(const char *devname
)
920 USBDevice
*dev
= NULL
;
925 /* drivers with .usbdevice_name entry in USBDeviceInfo */
926 dev
= usbdevice_create(devname
);
932 /* only the linux version is qdev-ified, usb-bsd still needs this */
933 if (strstart(devname
, "host:", &p
)) {
934 dev
= usb_host_device_open(p
);
937 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
938 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
939 bt_new_hci(qemu_find_bt_vlan(0)));
950 static int usb_device_del(const char *devname
)
955 if (strstart(devname
, "host:", &p
))
956 return usb_host_device_close(p
);
961 p
= strchr(devname
, '.');
964 bus_num
= strtoul(devname
, NULL
, 0);
965 addr
= strtoul(p
+ 1, NULL
, 0);
967 return usb_device_delete_addr(bus_num
, addr
);
970 static int usb_parse(const char *cmdline
)
973 r
= usb_device_add(cmdline
);
975 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
980 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
982 const char *devname
= qdict_get_str(qdict
, "devname");
983 if (usb_device_add(devname
) < 0) {
984 error_report("could not add USB device '%s'", devname
);
988 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
990 const char *devname
= qdict_get_str(qdict
, "devname");
991 if (usb_device_del(devname
) < 0) {
992 error_report("could not delete USB device '%s'", devname
);
996 /***********************************************************/
999 static struct pcmcia_socket_entry_s
{
1000 PCMCIASocket
*socket
;
1001 struct pcmcia_socket_entry_s
*next
;
1002 } *pcmcia_sockets
= 0;
1004 void pcmcia_socket_register(PCMCIASocket
*socket
)
1006 struct pcmcia_socket_entry_s
*entry
;
1008 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1009 entry
->socket
= socket
;
1010 entry
->next
= pcmcia_sockets
;
1011 pcmcia_sockets
= entry
;
1014 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1016 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1018 ptr
= &pcmcia_sockets
;
1019 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1020 if (entry
->socket
== socket
) {
1026 void pcmcia_info(Monitor
*mon
)
1028 struct pcmcia_socket_entry_s
*iter
;
1030 if (!pcmcia_sockets
)
1031 monitor_printf(mon
, "No PCMCIA sockets\n");
1033 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1034 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1035 iter
->socket
->attached
? iter
->socket
->card_string
:
1039 /***********************************************************/
1040 /* machine registration */
1042 static QEMUMachine
*first_machine
= NULL
;
1043 QEMUMachine
*current_machine
= NULL
;
1045 int qemu_register_machine(QEMUMachine
*m
)
1048 pm
= &first_machine
;
1056 static QEMUMachine
*find_machine(const char *name
)
1060 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1061 if (!strcmp(m
->name
, name
))
1063 if (m
->alias
&& !strcmp(m
->alias
, name
))
1069 static QEMUMachine
*find_default_machine(void)
1073 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1074 if (m
->is_default
) {
1081 /***********************************************************/
1082 /* main execution loop */
1084 static void gui_update(void *opaque
)
1086 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1087 DisplayState
*ds
= opaque
;
1088 DisplayChangeListener
*dcl
= ds
->listeners
;
1090 qemu_flush_coalesced_mmio_buffer();
1093 while (dcl
!= NULL
) {
1094 if (dcl
->gui_timer_interval
&&
1095 dcl
->gui_timer_interval
< interval
)
1096 interval
= dcl
->gui_timer_interval
;
1099 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1102 static void nographic_update(void *opaque
)
1104 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1106 qemu_flush_coalesced_mmio_buffer();
1107 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1110 struct vm_change_state_entry
{
1111 VMChangeStateHandler
*cb
;
1113 QLIST_ENTRY (vm_change_state_entry
) entries
;
1116 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1118 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1121 VMChangeStateEntry
*e
;
1123 e
= qemu_mallocz(sizeof (*e
));
1127 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1131 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1133 QLIST_REMOVE (e
, entries
);
1137 void vm_state_notify(int running
, int reason
)
1139 VMChangeStateEntry
*e
;
1141 trace_vm_state_notify(running
, reason
);
1143 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1144 e
->cb(e
->opaque
, running
, reason
);
1153 vm_state_notify(1, 0);
1155 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1159 /* reset/shutdown handler */
1161 typedef struct QEMUResetEntry
{
1162 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1163 QEMUResetHandler
*func
;
1167 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1168 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1169 static int reset_requested
;
1170 static int shutdown_requested
, shutdown_signal
= -1;
1171 static pid_t shutdown_pid
;
1172 static int powerdown_requested
;
1173 static int debug_requested
;
1174 static int vmstop_requested
;
1176 int qemu_shutdown_requested_get(void)
1178 return shutdown_requested
;
1181 int qemu_reset_requested_get(void)
1183 return reset_requested
;
1186 int qemu_shutdown_requested(void)
1188 int r
= shutdown_requested
;
1189 shutdown_requested
= 0;
1193 void qemu_kill_report(void)
1195 if (shutdown_signal
!= -1) {
1196 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1197 if (shutdown_pid
== 0) {
1198 /* This happens for eg ^C at the terminal, so it's worth
1199 * avoiding printing an odd message in that case.
1201 fputc('\n', stderr
);
1203 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1205 shutdown_signal
= -1;
1209 int qemu_reset_requested(void)
1211 int r
= reset_requested
;
1212 reset_requested
= 0;
1216 int qemu_powerdown_requested(void)
1218 int r
= powerdown_requested
;
1219 powerdown_requested
= 0;
1223 static int qemu_debug_requested(void)
1225 int r
= debug_requested
;
1226 debug_requested
= 0;
1230 static int qemu_vmstop_requested(void)
1232 int r
= vmstop_requested
;
1233 vmstop_requested
= 0;
1237 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1239 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1242 re
->opaque
= opaque
;
1243 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1246 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1250 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1251 if (re
->func
== func
&& re
->opaque
== opaque
) {
1252 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1259 void qemu_system_reset(bool report
)
1261 QEMUResetEntry
*re
, *nre
;
1263 /* reset all devices */
1264 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1265 re
->func(re
->opaque
);
1268 monitor_protocol_event(QEVENT_RESET
, NULL
);
1270 cpu_synchronize_all_post_reset();
1273 void qemu_system_reset_request(void)
1276 shutdown_requested
= 1;
1278 reset_requested
= 1;
1281 qemu_notify_event();
1284 void qemu_system_killed(int signal
, pid_t pid
)
1286 shutdown_signal
= signal
;
1288 qemu_system_shutdown_request();
1291 void qemu_system_shutdown_request(void)
1293 shutdown_requested
= 1;
1294 qemu_notify_event();
1297 void qemu_system_powerdown_request(void)
1299 powerdown_requested
= 1;
1300 qemu_notify_event();
1303 void qemu_system_debug_request(void)
1305 debug_requested
= 1;
1306 qemu_notify_event();
1309 void qemu_system_vmstop_request(int reason
)
1311 vmstop_requested
= reason
;
1312 qemu_notify_event();
1315 void main_loop_wait(int nonblocking
)
1317 fd_set rfds
, wfds
, xfds
;
1325 timeout
= qemu_calculate_timeout();
1326 qemu_bh_update_timeout(&timeout
);
1329 os_host_main_loop_wait(&timeout
);
1331 tv
.tv_sec
= timeout
/ 1000;
1332 tv
.tv_usec
= (timeout
% 1000) * 1000;
1334 /* poll any events */
1335 /* XXX: separate device handlers from system ones */
1340 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1341 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1343 qemu_mutex_unlock_iothread();
1344 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1345 qemu_mutex_lock_iothread();
1347 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1348 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1350 qemu_run_all_timers();
1352 /* Check bottom-halves last in case any of the earlier events triggered
1358 #ifndef CONFIG_IOTHREAD
1359 static int vm_request_pending(void)
1361 return powerdown_requested
||
1363 shutdown_requested
||
1369 qemu_irq qemu_system_powerdown
;
1371 static void main_loop(void)
1373 bool nonblocking
= false;
1374 #ifdef CONFIG_PROFILER
1379 qemu_main_loop_start();
1382 #ifndef CONFIG_IOTHREAD
1383 nonblocking
= cpu_exec_all();
1384 if (vm_request_pending()) {
1388 #ifdef CONFIG_PROFILER
1389 ti
= profile_getclock();
1391 main_loop_wait(nonblocking
);
1392 #ifdef CONFIG_PROFILER
1393 dev_time
+= profile_getclock() - ti
;
1396 if (qemu_debug_requested()) {
1397 vm_stop(VMSTOP_DEBUG
);
1399 if (qemu_shutdown_requested()) {
1401 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1403 vm_stop(VMSTOP_SHUTDOWN
);
1408 if (qemu_reset_requested()) {
1410 cpu_synchronize_all_states();
1411 qemu_system_reset(VMRESET_REPORT
);
1414 if (qemu_powerdown_requested()) {
1415 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1416 qemu_irq_raise(qemu_system_powerdown
);
1418 if ((r
= qemu_vmstop_requested())) {
1426 static void version(void)
1428 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1431 static void help(int exitcode
)
1433 const char *options_help
=
1434 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1436 #define DEFHEADING(text) stringify(text) "\n"
1437 #include "qemu-options.def"
1443 printf("usage: %s [options] [disk_image]\n"
1445 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1448 "During emulation, the following keys are useful:\n"
1449 "ctrl-alt-f toggle full screen\n"
1450 "ctrl-alt-n switch to virtual console 'n'\n"
1451 "ctrl-alt toggle mouse and keyboard grab\n"
1453 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1459 #define HAS_ARG 0x0001
1461 typedef struct QEMUOption
{
1468 static const QEMUOption qemu_options
[] = {
1469 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1470 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1471 { option, opt_arg, opt_enum, arch_mask },
1472 #define DEFHEADING(text)
1473 #include "qemu-options.def"
1479 static void select_vgahw (const char *p
)
1484 vga_interface_type
= VGA_NONE
;
1485 if (strstart(p
, "std", &opts
)) {
1486 vga_interface_type
= VGA_STD
;
1487 } else if (strstart(p
, "cirrus", &opts
)) {
1488 vga_interface_type
= VGA_CIRRUS
;
1489 } else if (strstart(p
, "vmware", &opts
)) {
1490 vga_interface_type
= VGA_VMWARE
;
1491 } else if (strstart(p
, "xenfb", &opts
)) {
1492 vga_interface_type
= VGA_XENFB
;
1493 } else if (strstart(p
, "qxl", &opts
)) {
1494 vga_interface_type
= VGA_QXL
;
1495 } else if (!strstart(p
, "none", &opts
)) {
1497 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1501 const char *nextopt
;
1503 if (strstart(opts
, ",retrace=", &nextopt
)) {
1505 if (strstart(opts
, "dumb", &nextopt
))
1506 vga_retrace_method
= VGA_RETRACE_DUMB
;
1507 else if (strstart(opts
, "precise", &nextopt
))
1508 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1509 else goto invalid_vga
;
1510 } else goto invalid_vga
;
1515 static DisplayType
select_display(const char *p
)
1518 DisplayType display
= DT_DEFAULT
;
1520 if (strstart(p
, "sdl", &opts
)) {
1524 const char *nextopt
;
1526 if (strstart(opts
, ",frame=", &nextopt
)) {
1528 if (strstart(opts
, "on", &nextopt
)) {
1530 } else if (strstart(opts
, "off", &nextopt
)) {
1533 goto invalid_sdl_args
;
1535 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1537 if (strstart(opts
, "on", &nextopt
)) {
1539 } else if (strstart(opts
, "off", &nextopt
)) {
1542 goto invalid_sdl_args
;
1544 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1546 if (strstart(opts
, "on", &nextopt
)) {
1548 } else if (strstart(opts
, "off", &nextopt
)) {
1551 goto invalid_sdl_args
;
1553 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1555 if (strstart(opts
, "on", &nextopt
)) {
1557 } else if (strstart(opts
, "off", &nextopt
)) {
1560 goto invalid_sdl_args
;
1564 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1570 fprintf(stderr
, "SDL support is disabled\n");
1573 } else if (strstart(p
, "vnc", &opts
)) {
1578 const char *nextopt
;
1580 if (strstart(opts
, "=", &nextopt
)) {
1581 vnc_display
= nextopt
;
1585 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1589 fprintf(stderr
, "VNC support is disabled\n");
1592 } else if (strstart(p
, "curses", &opts
)) {
1593 #ifdef CONFIG_CURSES
1594 display
= DT_CURSES
;
1596 fprintf(stderr
, "Curses support is disabled\n");
1599 } else if (strstart(p
, "none", &opts
)) {
1602 fprintf(stderr
, "Unknown display type: %s\n", p
);
1609 static int balloon_parse(const char *arg
)
1613 if (strcmp(arg
, "none") == 0) {
1617 if (!strncmp(arg
, "virtio", 6)) {
1618 if (arg
[6] == ',') {
1619 /* have params -> parse them */
1620 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1624 /* create empty opts */
1625 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1627 qemu_opt_set(opts
, "driver", "virtio-balloon");
1634 char *qemu_find_file(int type
, const char *name
)
1640 /* If name contains path separators then try it as a straight path. */
1641 if ((strchr(name
, '/') || strchr(name
, '\\'))
1642 && access(name
, R_OK
) == 0) {
1643 return qemu_strdup(name
);
1646 case QEMU_FILE_TYPE_BIOS
:
1649 case QEMU_FILE_TYPE_KEYMAP
:
1650 subdir
= "keymaps/";
1655 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1656 buf
= qemu_mallocz(len
);
1657 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1658 if (access(buf
, R_OK
)) {
1665 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1667 return qdev_device_help(opts
);
1670 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1674 dev
= qdev_device_add(opts
);
1680 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1682 CharDriverState
*chr
;
1684 chr
= qemu_chr_open_opts(opts
, NULL
);
1690 #ifdef CONFIG_VIRTFS
1691 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1694 ret
= qemu_fsdev_add(opts
);
1700 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1702 CharDriverState
*chr
;
1703 const char *chardev
;
1707 mode
= qemu_opt_get(opts
, "mode");
1711 if (strcmp(mode
, "readline") == 0) {
1712 flags
= MONITOR_USE_READLINE
;
1713 } else if (strcmp(mode
, "control") == 0) {
1714 flags
= MONITOR_USE_CONTROL
;
1716 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1720 if (qemu_opt_get_bool(opts
, "pretty", 0))
1721 flags
|= MONITOR_USE_PRETTY
;
1723 if (qemu_opt_get_bool(opts
, "default", 0))
1724 flags
|= MONITOR_IS_DEFAULT
;
1726 chardev
= qemu_opt_get(opts
, "chardev");
1727 chr
= qemu_chr_find(chardev
);
1729 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1733 monitor_init(chr
, flags
);
1737 static void monitor_parse(const char *optarg
, const char *mode
)
1739 static int monitor_device_index
= 0;
1745 if (strstart(optarg
, "chardev:", &p
)) {
1746 snprintf(label
, sizeof(label
), "%s", p
);
1748 snprintf(label
, sizeof(label
), "compat_monitor%d",
1749 monitor_device_index
);
1750 if (monitor_device_index
== 0) {
1753 opts
= qemu_chr_parse_compat(label
, optarg
);
1755 fprintf(stderr
, "parse error: %s\n", optarg
);
1760 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1762 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1765 qemu_opt_set(opts
, "mode", mode
);
1766 qemu_opt_set(opts
, "chardev", label
);
1768 qemu_opt_set(opts
, "default", "on");
1769 monitor_device_index
++;
1772 struct device_config
{
1774 DEV_USB
, /* -usbdevice */
1776 DEV_SERIAL
, /* -serial */
1777 DEV_PARALLEL
, /* -parallel */
1778 DEV_VIRTCON
, /* -virtioconsole */
1779 DEV_DEBUGCON
, /* -debugcon */
1781 const char *cmdline
;
1782 QTAILQ_ENTRY(device_config
) next
;
1784 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1786 static void add_device_config(int type
, const char *cmdline
)
1788 struct device_config
*conf
;
1790 conf
= qemu_mallocz(sizeof(*conf
));
1792 conf
->cmdline
= cmdline
;
1793 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1796 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1798 struct device_config
*conf
;
1801 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1802 if (conf
->type
!= type
)
1804 rc
= func(conf
->cmdline
);
1811 static int serial_parse(const char *devname
)
1813 static int index
= 0;
1816 if (strcmp(devname
, "none") == 0)
1818 if (index
== MAX_SERIAL_PORTS
) {
1819 fprintf(stderr
, "qemu: too many serial ports\n");
1822 snprintf(label
, sizeof(label
), "serial%d", index
);
1823 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1824 if (!serial_hds
[index
]) {
1825 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1826 devname
, strerror(errno
));
1833 static int parallel_parse(const char *devname
)
1835 static int index
= 0;
1838 if (strcmp(devname
, "none") == 0)
1840 if (index
== MAX_PARALLEL_PORTS
) {
1841 fprintf(stderr
, "qemu: too many parallel ports\n");
1844 snprintf(label
, sizeof(label
), "parallel%d", index
);
1845 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1846 if (!parallel_hds
[index
]) {
1847 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1848 devname
, strerror(errno
));
1855 static int virtcon_parse(const char *devname
)
1857 QemuOptsList
*device
= qemu_find_opts("device");
1858 static int index
= 0;
1860 QemuOpts
*bus_opts
, *dev_opts
;
1862 if (strcmp(devname
, "none") == 0)
1864 if (index
== MAX_VIRTIO_CONSOLES
) {
1865 fprintf(stderr
, "qemu: too many virtio consoles\n");
1869 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1870 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1872 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1873 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1875 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1876 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1877 if (!virtcon_hds
[index
]) {
1878 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1879 devname
, strerror(errno
));
1882 qemu_opt_set(dev_opts
, "chardev", label
);
1888 static int debugcon_parse(const char *devname
)
1892 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1895 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1897 fprintf(stderr
, "qemu: already have a debugcon device\n");
1900 qemu_opt_set(opts
, "driver", "isa-debugcon");
1901 qemu_opt_set(opts
, "chardev", "debugcon");
1905 static int tcg_init(void)
1911 const char *opt_name
;
1913 int (*available
)(void);
1917 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
1918 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
1919 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
1922 static int configure_accelerator(void)
1924 const char *p
= NULL
;
1927 bool accel_initalised
= 0;
1928 bool init_failed
= 0;
1930 QemuOptsList
*list
= qemu_find_opts("machine");
1931 if (!QTAILQ_EMPTY(&list
->head
)) {
1932 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
1936 /* Use the default "accelerator", kvm */
1940 while (!accel_initalised
&& *p
!= '\0') {
1944 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
1945 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
1946 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
1947 *(accel_list
[i
].allowed
) = 1;
1948 ret
= accel_list
[i
].init();
1951 if (!accel_list
[i
].available()) {
1952 printf("%s not supported for this target\n",
1953 accel_list
[i
].name
);
1955 fprintf(stderr
, "failed to initialize %s: %s\n",
1959 *(accel_list
[i
].allowed
) = 0;
1961 accel_initalised
= 1;
1966 if (i
== ARRAY_SIZE(accel_list
)) {
1967 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
1971 if (!accel_initalised
) {
1972 fprintf(stderr
, "No accelerator found!\n");
1977 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
1980 return !accel_initalised
;
1983 void qemu_add_exit_notifier(Notifier
*notify
)
1985 notifier_list_add(&exit_notifiers
, notify
);
1988 void qemu_remove_exit_notifier(Notifier
*notify
)
1990 notifier_list_remove(&exit_notifiers
, notify
);
1993 static void qemu_run_exit_notifiers(void)
1995 notifier_list_notify(&exit_notifiers
);
1998 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2000 notifier_list_add(&machine_init_done_notifiers
, notify
);
2003 static void qemu_run_machine_init_done_notifiers(void)
2005 notifier_list_notify(&machine_init_done_notifiers
);
2008 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2009 const char **poptarg
, int *poptind
)
2011 const QEMUOption
*popt
;
2012 int optind
= *poptind
;
2013 char *r
= argv
[optind
];
2016 loc_set_cmdline(argv
, optind
, 1);
2018 /* Treat --foo the same as -foo. */
2021 popt
= qemu_options
;
2024 error_report("invalid option");
2027 if (!strcmp(popt
->name
, r
+ 1))
2031 if (popt
->flags
& HAS_ARG
) {
2032 if (optind
>= argc
) {
2033 error_report("requires an argument");
2036 optarg
= argv
[optind
++];
2037 loc_set_cmdline(argv
, optind
- 2, 2);
2048 int main(int argc
, char **argv
, char **envp
)
2050 const char *gdbstub_dev
= NULL
;
2052 int snapshot
, linux_boot
;
2053 const char *icount_option
= NULL
;
2054 const char *initrd_filename
;
2055 const char *kernel_filename
, *kernel_cmdline
;
2056 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2058 DisplayChangeListener
*dcl
;
2059 int cyls
, heads
, secs
, translation
;
2060 QemuOpts
*hda_opts
= NULL
, *opts
;
2061 QemuOptsList
*olist
;
2064 const char *loadvm
= NULL
;
2065 QEMUMachine
*machine
;
2066 const char *cpu_model
;
2068 const char *pid_file
= NULL
;
2069 const char *incoming
= NULL
;
2071 int show_vnc_port
= 0;
2074 const char *trace_file
= NULL
;
2075 const char *log_mask
= NULL
;
2076 const char *log_file
= NULL
;
2078 atexit(qemu_run_exit_notifiers
);
2079 error_set_progname(argv
[0]);
2083 qemu_cache_utils_init(envp
);
2085 QLIST_INIT (&vm_change_state_head
);
2086 os_setup_early_signal_handling();
2088 module_call_init(MODULE_INIT_MACHINE
);
2089 machine
= find_default_machine();
2091 initrd_filename
= NULL
;
2094 kernel_filename
= NULL
;
2095 kernel_cmdline
= "";
2096 cyls
= heads
= secs
= 0;
2097 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2099 for (i
= 0; i
< MAX_NODES
; i
++) {
2101 node_cpumask
[i
] = 0;
2110 /* first pass of option parsing */
2112 while (optind
< argc
) {
2113 if (argv
[optind
][0] != '-') {
2118 const QEMUOption
*popt
;
2120 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2121 switch (popt
->index
) {
2122 case QEMU_OPTION_nodefconfig
:
2132 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2133 if (ret
< 0 && ret
!= -ENOENT
) {
2137 ret
= qemu_read_config_file(arch_config_name
);
2138 if (ret
< 0 && ret
!= -ENOENT
) {
2144 /* second pass of option parsing */
2149 if (argv
[optind
][0] != '-') {
2150 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2152 const QEMUOption
*popt
;
2154 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2155 if (!(popt
->arch_mask
& arch_type
)) {
2156 printf("Option %s not supported for this target\n", popt
->name
);
2159 switch(popt
->index
) {
2161 machine
= find_machine(optarg
);
2164 printf("Supported machines are:\n");
2165 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2167 printf("%-10s %s (alias of %s)\n",
2168 m
->alias
, m
->desc
, m
->name
);
2169 printf("%-10s %s%s\n",
2171 m
->is_default
? " (default)" : "");
2173 exit(*optarg
!= '?');
2176 case QEMU_OPTION_cpu
:
2177 /* hw initialization will check this */
2178 if (*optarg
== '?') {
2179 list_cpus(stdout
, &fprintf
, optarg
);
2185 case QEMU_OPTION_initrd
:
2186 initrd_filename
= optarg
;
2188 case QEMU_OPTION_hda
:
2192 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2194 snprintf(buf
, sizeof(buf
),
2195 "%s,cyls=%d,heads=%d,secs=%d%s",
2196 HD_OPTS
, cyls
, heads
, secs
,
2197 translation
== BIOS_ATA_TRANSLATION_LBA
?
2199 translation
== BIOS_ATA_TRANSLATION_NONE
?
2200 ",trans=none" : "");
2201 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2204 case QEMU_OPTION_hdb
:
2205 case QEMU_OPTION_hdc
:
2206 case QEMU_OPTION_hdd
:
2207 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2210 case QEMU_OPTION_drive
:
2211 if (drive_def(optarg
) == NULL
) {
2215 case QEMU_OPTION_set
:
2216 if (qemu_set_option(optarg
) != 0)
2219 case QEMU_OPTION_global
:
2220 if (qemu_global_option(optarg
) != 0)
2223 case QEMU_OPTION_mtdblock
:
2224 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2226 case QEMU_OPTION_sd
:
2227 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2229 case QEMU_OPTION_pflash
:
2230 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2232 case QEMU_OPTION_snapshot
:
2235 case QEMU_OPTION_hdachs
:
2239 cyls
= strtol(p
, (char **)&p
, 0);
2240 if (cyls
< 1 || cyls
> 16383)
2245 heads
= strtol(p
, (char **)&p
, 0);
2246 if (heads
< 1 || heads
> 16)
2251 secs
= strtol(p
, (char **)&p
, 0);
2252 if (secs
< 1 || secs
> 63)
2256 if (!strcmp(p
, "none"))
2257 translation
= BIOS_ATA_TRANSLATION_NONE
;
2258 else if (!strcmp(p
, "lba"))
2259 translation
= BIOS_ATA_TRANSLATION_LBA
;
2260 else if (!strcmp(p
, "auto"))
2261 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2264 } else if (*p
!= '\0') {
2266 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2269 if (hda_opts
!= NULL
) {
2271 snprintf(num
, sizeof(num
), "%d", cyls
);
2272 qemu_opt_set(hda_opts
, "cyls", num
);
2273 snprintf(num
, sizeof(num
), "%d", heads
);
2274 qemu_opt_set(hda_opts
, "heads", num
);
2275 snprintf(num
, sizeof(num
), "%d", secs
);
2276 qemu_opt_set(hda_opts
, "secs", num
);
2277 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2278 qemu_opt_set(hda_opts
, "trans", "lba");
2279 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2280 qemu_opt_set(hda_opts
, "trans", "none");
2284 case QEMU_OPTION_numa
:
2285 if (nb_numa_nodes
>= MAX_NODES
) {
2286 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2291 case QEMU_OPTION_display
:
2292 display_type
= select_display(optarg
);
2294 case QEMU_OPTION_nographic
:
2295 display_type
= DT_NOGRAPHIC
;
2297 case QEMU_OPTION_curses
:
2298 #ifdef CONFIG_CURSES
2299 display_type
= DT_CURSES
;
2301 fprintf(stderr
, "Curses support is disabled\n");
2305 case QEMU_OPTION_portrait
:
2308 case QEMU_OPTION_kernel
:
2309 kernel_filename
= optarg
;
2311 case QEMU_OPTION_append
:
2312 kernel_cmdline
= optarg
;
2314 case QEMU_OPTION_cdrom
:
2315 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2317 case QEMU_OPTION_boot
:
2319 static const char * const params
[] = {
2320 "order", "once", "menu", NULL
2322 char buf
[sizeof(boot_devices
)];
2323 char *standard_boot_devices
;
2326 if (!strchr(optarg
, '=')) {
2328 pstrcpy(buf
, sizeof(buf
), optarg
);
2329 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2331 "qemu: unknown boot parameter '%s' in '%s'\n",
2337 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2338 validate_bootdevices(buf
);
2339 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2342 if (get_param_value(buf
, sizeof(buf
),
2344 validate_bootdevices(buf
);
2345 standard_boot_devices
= qemu_strdup(boot_devices
);
2346 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2347 qemu_register_reset(restore_boot_devices
,
2348 standard_boot_devices
);
2350 if (get_param_value(buf
, sizeof(buf
),
2352 if (!strcmp(buf
, "on")) {
2354 } else if (!strcmp(buf
, "off")) {
2358 "qemu: invalid option value '%s'\n",
2366 case QEMU_OPTION_fda
:
2367 case QEMU_OPTION_fdb
:
2368 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2371 case QEMU_OPTION_no_fd_bootchk
:
2374 case QEMU_OPTION_netdev
:
2375 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2379 case QEMU_OPTION_net
:
2380 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2385 case QEMU_OPTION_tftp
:
2386 legacy_tftp_prefix
= optarg
;
2388 case QEMU_OPTION_bootp
:
2389 legacy_bootp_filename
= optarg
;
2391 case QEMU_OPTION_redir
:
2392 if (net_slirp_redir(optarg
) < 0)
2396 case QEMU_OPTION_bt
:
2397 add_device_config(DEV_BT
, optarg
);
2399 case QEMU_OPTION_audio_help
:
2400 if (!(audio_available())) {
2401 printf("Option %s not supported for this target\n", popt
->name
);
2407 case QEMU_OPTION_soundhw
:
2408 if (!(audio_available())) {
2409 printf("Option %s not supported for this target\n", popt
->name
);
2412 select_soundhw (optarg
);
2417 case QEMU_OPTION_version
:
2421 case QEMU_OPTION_m
: {
2424 value
= strtosz(optarg
, NULL
);
2426 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2430 /* On 32-bit hosts, QEMU is limited by virtual address space */
2431 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2432 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2435 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2436 fprintf(stderr
, "qemu: ram size too large\n");
2442 case QEMU_OPTION_mempath
:
2446 case QEMU_OPTION_mem_prealloc
:
2457 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2459 case QEMU_OPTION_gdb
:
2460 gdbstub_dev
= optarg
;
2465 case QEMU_OPTION_bios
:
2468 case QEMU_OPTION_singlestep
:
2475 keyboard_layout
= optarg
;
2477 case QEMU_OPTION_localtime
:
2480 case QEMU_OPTION_vga
:
2481 select_vgahw (optarg
);
2488 w
= strtol(p
, (char **)&p
, 10);
2491 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2497 h
= strtol(p
, (char **)&p
, 10);
2502 depth
= strtol(p
, (char **)&p
, 10);
2503 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2504 depth
!= 24 && depth
!= 32)
2506 } else if (*p
== '\0') {
2507 depth
= graphic_depth
;
2514 graphic_depth
= depth
;
2517 case QEMU_OPTION_echr
:
2520 term_escape_char
= strtol(optarg
, &r
, 0);
2522 printf("Bad argument to echr\n");
2525 case QEMU_OPTION_monitor
:
2526 monitor_parse(optarg
, "readline");
2527 default_monitor
= 0;
2529 case QEMU_OPTION_qmp
:
2530 monitor_parse(optarg
, "control");
2531 default_monitor
= 0;
2533 case QEMU_OPTION_mon
:
2534 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2538 default_monitor
= 0;
2540 case QEMU_OPTION_chardev
:
2541 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2546 case QEMU_OPTION_fsdev
:
2547 olist
= qemu_find_opts("fsdev");
2549 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2552 opts
= qemu_opts_parse(olist
, optarg
, 1);
2554 fprintf(stderr
, "parse error: %s\n", optarg
);
2558 case QEMU_OPTION_virtfs
: {
2562 olist
= qemu_find_opts("virtfs");
2564 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2567 opts
= qemu_opts_parse(olist
, optarg
, 1);
2569 fprintf(stderr
, "parse error: %s\n", optarg
);
2573 if (qemu_opt_get(opts
, "fstype") == NULL
||
2574 qemu_opt_get(opts
, "mount_tag") == NULL
||
2575 qemu_opt_get(opts
, "path") == NULL
||
2576 qemu_opt_get(opts
, "security_model") == NULL
) {
2577 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2578 "security_model=[mapped|passthrough|none],"
2579 "mount_tag=tag.\n");
2583 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2584 qemu_opt_get(opts
, "mount_tag"), 1);
2586 fprintf(stderr
, "duplicate fsdev id: %s\n",
2587 qemu_opt_get(opts
, "mount_tag"));
2590 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2591 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2592 qemu_opt_set(fsdev
, "security_model",
2593 qemu_opt_get(opts
, "security_model"));
2595 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2596 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2597 qemu_opt_set(device
, "fsdev",
2598 qemu_opt_get(opts
, "mount_tag"));
2599 qemu_opt_set(device
, "mount_tag",
2600 qemu_opt_get(opts
, "mount_tag"));
2603 case QEMU_OPTION_serial
:
2604 add_device_config(DEV_SERIAL
, optarg
);
2606 if (strncmp(optarg
, "mon:", 4) == 0) {
2607 default_monitor
= 0;
2610 case QEMU_OPTION_watchdog
:
2613 "qemu: only one watchdog option may be given\n");
2618 case QEMU_OPTION_watchdog_action
:
2619 if (select_watchdog_action(optarg
) == -1) {
2620 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2624 case QEMU_OPTION_virtiocon
:
2625 add_device_config(DEV_VIRTCON
, optarg
);
2626 default_virtcon
= 0;
2627 if (strncmp(optarg
, "mon:", 4) == 0) {
2628 default_monitor
= 0;
2631 case QEMU_OPTION_parallel
:
2632 add_device_config(DEV_PARALLEL
, optarg
);
2633 default_parallel
= 0;
2634 if (strncmp(optarg
, "mon:", 4) == 0) {
2635 default_monitor
= 0;
2638 case QEMU_OPTION_debugcon
:
2639 add_device_config(DEV_DEBUGCON
, optarg
);
2641 case QEMU_OPTION_loadvm
:
2644 case QEMU_OPTION_full_screen
:
2648 case QEMU_OPTION_no_frame
:
2651 case QEMU_OPTION_alt_grab
:
2654 case QEMU_OPTION_ctrl_grab
:
2657 case QEMU_OPTION_no_quit
:
2660 case QEMU_OPTION_sdl
:
2661 display_type
= DT_SDL
;
2664 case QEMU_OPTION_no_frame
:
2665 case QEMU_OPTION_alt_grab
:
2666 case QEMU_OPTION_ctrl_grab
:
2667 case QEMU_OPTION_no_quit
:
2668 case QEMU_OPTION_sdl
:
2669 fprintf(stderr
, "SDL support is disabled\n");
2672 case QEMU_OPTION_pidfile
:
2675 case QEMU_OPTION_win2k_hack
:
2676 win2k_install_hack
= 1;
2678 case QEMU_OPTION_rtc_td_hack
:
2681 case QEMU_OPTION_acpitable
:
2682 do_acpitable_option(optarg
);
2684 case QEMU_OPTION_smbios
:
2685 do_smbios_option(optarg
);
2687 case QEMU_OPTION_enable_kvm
:
2688 olist
= qemu_find_opts("machine");
2689 qemu_opts_reset(olist
);
2690 qemu_opts_parse(olist
, "accel=kvm", 0);
2692 case QEMU_OPTION_machine
:
2693 olist
= qemu_find_opts("machine");
2694 qemu_opts_reset(olist
);
2695 opts
= qemu_opts_parse(olist
, optarg
, 0);
2697 fprintf(stderr
, "parse error: %s\n", optarg
);
2701 case QEMU_OPTION_no_kvm
:
2702 olist
= qemu_find_opts("machine");
2703 qemu_opts_reset(olist
);
2704 qemu_opts_parse(olist
, "accel=tcg", 0);
2707 case QEMU_OPTION_no_kvm_irqchip
: {
2712 case QEMU_OPTION_no_kvm_pit
: {
2716 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2717 kvm_pit_reinject
= 0;
2721 case QEMU_OPTION_usb
:
2724 case QEMU_OPTION_usbdevice
:
2726 add_device_config(DEV_USB
, optarg
);
2728 case QEMU_OPTION_device
:
2729 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2733 case QEMU_OPTION_smp
:
2736 fprintf(stderr
, "Invalid number of CPUs\n");
2739 if (max_cpus
< smp_cpus
) {
2740 fprintf(stderr
, "maxcpus must be equal to or greater than "
2744 if (max_cpus
> 255) {
2745 fprintf(stderr
, "Unsupported number of maxcpus\n");
2749 case QEMU_OPTION_vnc
:
2752 vnc_display
= optarg
;
2754 fprintf(stderr
, "VNC support is disabled\n");
2758 case QEMU_OPTION_no_acpi
:
2761 case QEMU_OPTION_no_hpet
:
2764 case QEMU_OPTION_balloon
:
2765 if (balloon_parse(optarg
) < 0) {
2766 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2770 case QEMU_OPTION_no_reboot
:
2773 case QEMU_OPTION_no_shutdown
:
2776 case QEMU_OPTION_show_cursor
:
2779 case QEMU_OPTION_uuid
:
2780 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2781 fprintf(stderr
, "Fail to parse UUID string."
2782 " Wrong format.\n");
2786 case QEMU_OPTION_option_rom
:
2787 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2788 fprintf(stderr
, "Too many option ROMs\n");
2791 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2792 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2793 option_rom
[nb_option_roms
].bootindex
=
2794 qemu_opt_get_number(opts
, "bootindex", -1);
2795 if (!option_rom
[nb_option_roms
].name
) {
2796 fprintf(stderr
, "Option ROM file is not specified\n");
2801 case QEMU_OPTION_semihosting
:
2802 semihosting_enabled
= 1;
2804 case QEMU_OPTION_tdf
:
2807 case QEMU_OPTION_kvm_shadow_memory
:
2808 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
2810 case QEMU_OPTION_name
:
2811 qemu_name
= qemu_strdup(optarg
);
2813 char *p
= strchr(qemu_name
, ',');
2816 if (strncmp(p
, "process=", 8)) {
2817 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2821 os_set_proc_name(p
);
2825 case QEMU_OPTION_prom_env
:
2826 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2827 fprintf(stderr
, "Too many prom variables\n");
2830 prom_envs
[nb_prom_envs
] = optarg
;
2833 case QEMU_OPTION_old_param
:
2836 case QEMU_OPTION_clock
:
2837 configure_alarms(optarg
);
2839 case QEMU_OPTION_startdate
:
2840 configure_rtc_date_offset(optarg
, 1);
2842 case QEMU_OPTION_rtc
:
2843 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2847 configure_rtc(opts
);
2849 case QEMU_OPTION_tb_size
:
2850 tb_size
= strtol(optarg
, NULL
, 0);
2854 case QEMU_OPTION_icount
:
2855 icount_option
= optarg
;
2857 case QEMU_OPTION_incoming
:
2859 incoming_expected
= true;
2861 case QEMU_OPTION_nodefaults
:
2863 default_parallel
= 0;
2864 default_virtcon
= 0;
2865 default_monitor
= 0;
2873 case QEMU_OPTION_nvram
:
2877 case QEMU_OPTION_xen_domid
:
2878 if (!(xen_available())) {
2879 printf("Option %s not supported for this target\n", popt
->name
);
2882 xen_domid
= atoi(optarg
);
2884 case QEMU_OPTION_xen_create
:
2885 if (!(xen_available())) {
2886 printf("Option %s not supported for this target\n", popt
->name
);
2889 xen_mode
= XEN_CREATE
;
2891 case QEMU_OPTION_xen_attach
:
2892 if (!(xen_available())) {
2893 printf("Option %s not supported for this target\n", popt
->name
);
2896 xen_mode
= XEN_ATTACH
;
2898 #ifdef CONFIG_SIMPLE_TRACE
2899 case QEMU_OPTION_trace
:
2900 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2902 trace_file
= qemu_opt_get(opts
, "file");
2906 case QEMU_OPTION_readconfig
:
2908 int ret
= qemu_read_config_file(optarg
);
2910 fprintf(stderr
, "read config %s: %s\n", optarg
,
2916 case QEMU_OPTION_spice
:
2917 olist
= qemu_find_opts("spice");
2919 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2922 opts
= qemu_opts_parse(olist
, optarg
, 0);
2924 fprintf(stderr
, "parse error: %s\n", optarg
);
2928 case QEMU_OPTION_writeconfig
:
2931 if (strcmp(optarg
, "-") == 0) {
2934 fp
= fopen(optarg
, "w");
2936 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2940 qemu_config_write(fp
);
2945 os_parse_cmd_args(popt
->index
, optarg
);
2951 /* Open the logfile at this point, if necessary. We can't open the logfile
2952 * when encountering either of the logging options (-d or -D) because the
2953 * other one may be encountered later on the command line, changing the
2954 * location or level of logging.
2958 set_cpu_log_filename(log_file
);
2960 set_cpu_log(log_mask
);
2963 if (!st_init(trace_file
)) {
2964 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2967 /* If no data_dir is specified then try to find it relative to the
2970 data_dir
= os_find_datadir(argv
[0]);
2972 /* If all else fails use the install patch specified when building. */
2974 data_dir
= CONFIG_QEMU_DATADIR
;
2978 * Default to max_cpus = smp_cpus, in case the user doesn't
2979 * specify a max_cpus value.
2982 max_cpus
= smp_cpus
;
2984 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2985 if (smp_cpus
> machine
->max_cpus
) {
2986 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2987 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2993 * Get the default machine options from the machine if it is not already
2994 * specified either by the configuration file or by the command line.
2996 if (machine
->default_machine_opts
) {
2997 QemuOptsList
*list
= qemu_find_opts("machine");
2998 const char *p
= NULL
;
3000 if (!QTAILQ_EMPTY(&list
->head
)) {
3001 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3004 opts
= qemu_opts_parse(qemu_find_opts("machine"),
3005 machine
->default_machine_opts
, 0);
3007 fprintf(stderr
, "parse error for machine %s: %s\n",
3008 machine
->name
, machine
->default_machine_opts
);
3014 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3015 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3017 if (machine
->no_serial
) {
3020 if (machine
->no_parallel
) {
3021 default_parallel
= 0;
3023 if (!machine
->use_virtcon
) {
3024 default_virtcon
= 0;
3026 if (machine
->no_vga
) {
3029 if (machine
->no_floppy
) {
3032 if (machine
->no_cdrom
) {
3035 if (machine
->no_sdcard
) {
3039 if (display_type
== DT_NOGRAPHIC
) {
3040 if (default_parallel
)
3041 add_device_config(DEV_PARALLEL
, "null");
3042 if (default_serial
&& default_monitor
) {
3043 add_device_config(DEV_SERIAL
, "mon:stdio");
3044 } else if (default_virtcon
&& default_monitor
) {
3045 add_device_config(DEV_VIRTCON
, "mon:stdio");
3048 add_device_config(DEV_SERIAL
, "stdio");
3049 if (default_virtcon
)
3050 add_device_config(DEV_VIRTCON
, "stdio");
3051 if (default_monitor
)
3052 monitor_parse("stdio", "readline");
3056 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3057 if (default_parallel
)
3058 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3059 if (default_monitor
)
3060 monitor_parse("vc:80Cx24C", "readline");
3061 if (default_virtcon
)
3062 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3065 vga_interface_type
= VGA_CIRRUS
;
3069 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3071 #ifdef CONFIG_VIRTFS
3072 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3079 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3084 configure_accelerator();
3086 if (qemu_init_main_loop()) {
3087 fprintf(stderr
, "qemu_init_main_loop failed\n");
3090 linux_boot
= (kernel_filename
!= NULL
);
3092 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3093 fprintf(stderr
, "-append only allowed with -kernel option\n");
3097 if (!linux_boot
&& initrd_filename
!= NULL
) {
3098 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3102 os_set_line_buffering();
3104 if (init_timer_alarm() < 0) {
3105 fprintf(stderr
, "could not initialize alarm timer\n");
3108 configure_icount(icount_option
);
3110 if (net_init_clients() < 0) {
3114 /* init the bluetooth world */
3115 if (foreach_device_config(DEV_BT
, bt_parse
))
3118 /* init the memory */
3120 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3122 /* init the dynamic translator */
3123 cpu_exec_init_all(tb_size
* 1024 * 1024);
3125 bdrv_init_with_whitelist();
3129 /* open the virtual block devices */
3131 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3132 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3135 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3136 IF_DEFAULT
, 2, CDROM_OPTS
);
3137 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3138 IF_FLOPPY
, 0, FD_OPTS
);
3139 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3142 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3145 if (nb_numa_nodes
> 0) {
3148 if (nb_numa_nodes
> smp_cpus
) {
3149 nb_numa_nodes
= smp_cpus
;
3152 /* If no memory size if given for any node, assume the default case
3153 * and distribute the available memory equally across all nodes
3155 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3156 if (node_mem
[i
] != 0)
3159 if (i
== nb_numa_nodes
) {
3160 uint64_t usedmem
= 0;
3162 /* On Linux, the each node's border has to be 8MB aligned,
3163 * the final node gets the rest.
3165 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3166 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3167 usedmem
+= node_mem
[i
];
3169 node_mem
[i
] = ram_size
- usedmem
;
3172 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3173 if (node_cpumask
[i
] != 0)
3176 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3177 * must cope with this anyway, because there are BIOSes out there in
3178 * real machines which also use this scheme.
3180 if (i
== nb_numa_nodes
) {
3181 for (i
= 0; i
< smp_cpus
; i
++) {
3182 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3187 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3191 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3193 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3195 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3197 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3200 module_call_init(MODULE_INIT_DEVICE
);
3202 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3206 i
= select_watchdog(watchdog
);
3208 exit (i
== 1 ? 1 : 0);
3211 if (machine
->compat_props
) {
3212 qdev_prop_register_global_list(machine
->compat_props
);
3216 machine
->init(ram_size
, boot_devices
,
3217 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3219 cpu_synchronize_all_post_init();
3223 current_machine
= machine
;
3225 /* init USB devices */
3227 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3231 /* init generic devices */
3232 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3235 net_check_clients();
3237 /* just use the first displaystate for the moment */
3238 ds
= get_displaystate();
3242 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3243 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3244 display_type
= DT_SDL
;
3245 #elif defined(CONFIG_VNC)
3246 vnc_display
= "localhost:0,to=99";
3249 display_type
= DT_NONE
;
3254 /* init local displays */
3255 switch (display_type
) {
3258 #if defined(CONFIG_CURSES)
3260 curses_display_init(ds
, full_screen
);
3263 #if defined(CONFIG_SDL)
3265 sdl_display_init(ds
, full_screen
, no_frame
);
3267 #elif defined(CONFIG_COCOA)
3269 cocoa_display_init(ds
, full_screen
);
3276 /* must be after terminal init, SDL library changes signal handlers */
3277 os_setup_signal_handling();
3280 /* init remote displays */
3282 vnc_display_init(ds
);
3283 if (vnc_display_open(ds
, vnc_display
) < 0)
3286 if (show_vnc_port
) {
3287 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3292 if (using_spice
&& !qxl_enabled
) {
3293 qemu_spice_display_init(ds
);
3299 dcl
= ds
->listeners
;
3300 while (dcl
!= NULL
) {
3301 if (dcl
->dpy_refresh
!= NULL
) {
3302 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3303 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3308 if (ds
->gui_timer
== NULL
) {
3309 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3310 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3312 text_consoles_set_display(ds
);
3314 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3315 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3320 qdev_machine_creation_done();
3322 if (rom_load_all() != 0) {
3323 fprintf(stderr
, "rom loading failed\n");
3327 /* TODO: once all bus devices are qdevified, this should be done
3328 * when bus is created by qdev.c */
3329 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3330 qemu_run_machine_init_done_notifiers();
3332 qemu_system_reset(VMRESET_SILENT
);
3334 if (load_vmstate(loadvm
) < 0) {
3340 int ret
= qemu_start_incoming_migration(incoming
);
3342 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3346 } else if (autostart
) {