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-drive", .flag
= &default_cdrom
},
286 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
287 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
288 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
289 { .driver
= "VGA", .flag
= &default_vga
},
290 { .driver
= "cirrus-vga", .flag
= &default_vga
},
291 { .driver
= "vmware-svga", .flag
= &default_vga
},
294 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
296 const char *driver
= qemu_opt_get(opts
, "driver");
301 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
302 if (strcmp(default_list
[i
].driver
, driver
) != 0)
304 *(default_list
[i
].flag
) = 0;
309 /***********************************************************/
310 /* real time host monotonic timer */
312 /***********************************************************/
313 /* host time/date access */
314 void qemu_get_timedate(struct tm
*tm
, int offset
)
321 if (rtc_date_offset
== -1) {
325 ret
= localtime(&ti
);
327 ti
-= rtc_date_offset
;
331 memcpy(tm
, ret
, sizeof(struct tm
));
334 int qemu_timedate_diff(struct tm
*tm
)
338 if (rtc_date_offset
== -1)
340 seconds
= mktimegm(tm
);
342 seconds
= mktime(tm
);
344 seconds
= mktimegm(tm
) + rtc_date_offset
;
346 return seconds
- time(NULL
);
349 void rtc_change_mon_event(struct tm
*tm
)
353 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
354 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
355 qobject_decref(data
);
358 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
360 time_t rtc_start_date
;
363 if (!strcmp(startdate
, "now") && legacy
) {
364 rtc_date_offset
= -1;
366 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
374 } else if (sscanf(startdate
, "%d-%d-%d",
386 rtc_start_date
= mktimegm(&tm
);
387 if (rtc_start_date
== -1) {
389 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
390 "'2006-06-17T16:01:21' or '2006-06-17'\n");
393 rtc_date_offset
= time(NULL
) - rtc_start_date
;
397 static void configure_rtc(QemuOpts
*opts
)
401 value
= qemu_opt_get(opts
, "base");
403 if (!strcmp(value
, "utc")) {
405 } else if (!strcmp(value
, "localtime")) {
408 configure_rtc_date_offset(value
, 0);
411 value
= qemu_opt_get(opts
, "clock");
413 if (!strcmp(value
, "host")) {
414 rtc_clock
= host_clock
;
415 } else if (!strcmp(value
, "vm")) {
416 rtc_clock
= vm_clock
;
418 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
422 value
= qemu_opt_get(opts
, "driftfix");
424 if (!strcmp(value
, "slew")) {
426 } else if (!strcmp(value
, "none")) {
429 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
435 /***********************************************************/
436 /* Bluetooth support */
439 static struct HCIInfo
*hci_table
[MAX_NICS
];
441 static struct bt_vlan_s
{
442 struct bt_scatternet_s net
;
444 struct bt_vlan_s
*next
;
447 /* find or alloc a new bluetooth "VLAN" */
448 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
450 struct bt_vlan_s
**pvlan
, *vlan
;
451 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
455 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
457 pvlan
= &first_bt_vlan
;
458 while (*pvlan
!= NULL
)
459 pvlan
= &(*pvlan
)->next
;
464 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
468 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
473 static struct HCIInfo null_hci
= {
474 .cmd_send
= null_hci_send
,
475 .sco_send
= null_hci_send
,
476 .acl_send
= null_hci_send
,
477 .bdaddr_set
= null_hci_addr_set
,
480 struct HCIInfo
*qemu_next_hci(void)
482 if (cur_hci
== nb_hcis
)
485 return hci_table
[cur_hci
++];
488 static struct HCIInfo
*hci_init(const char *str
)
491 struct bt_scatternet_s
*vlan
= 0;
493 if (!strcmp(str
, "null"))
496 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
498 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
499 else if (!strncmp(str
, "hci", 3)) {
502 if (!strncmp(str
+ 3, ",vlan=", 6)) {
503 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
508 vlan
= qemu_find_bt_vlan(0);
510 return bt_new_hci(vlan
);
513 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
518 static int bt_hci_parse(const char *str
)
523 if (nb_hcis
>= MAX_NICS
) {
524 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
537 bdaddr
.b
[5] = 0x56 + nb_hcis
;
538 hci
->bdaddr_set(hci
, bdaddr
.b
);
540 hci_table
[nb_hcis
++] = hci
;
545 static void bt_vhci_add(int vlan_id
)
547 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
550 fprintf(stderr
, "qemu: warning: adding a VHCI to "
551 "an empty scatternet %i\n", vlan_id
);
553 bt_vhci_init(bt_new_hci(vlan
));
556 static struct bt_device_s
*bt_device_add(const char *opt
)
558 struct bt_scatternet_s
*vlan
;
560 char *endp
= strstr(opt
, ",vlan=");
561 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
564 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
567 vlan_id
= strtol(endp
+ 6, &endp
, 0);
569 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
574 vlan
= qemu_find_bt_vlan(vlan_id
);
577 fprintf(stderr
, "qemu: warning: adding a slave device to "
578 "an empty scatternet %i\n", vlan_id
);
580 if (!strcmp(devname
, "keyboard"))
581 return bt_keyboard_init(vlan
);
583 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
587 static int bt_parse(const char *opt
)
589 const char *endp
, *p
;
592 if (strstart(opt
, "hci", &endp
)) {
593 if (!*endp
|| *endp
== ',') {
595 if (!strstart(endp
, ",vlan=", 0))
598 return bt_hci_parse(opt
);
600 } else if (strstart(opt
, "vhci", &endp
)) {
601 if (!*endp
|| *endp
== ',') {
603 if (strstart(endp
, ",vlan=", &p
)) {
604 vlan
= strtol(p
, (char **) &endp
, 0);
606 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
610 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
619 } else if (strstart(opt
, "device:", &endp
))
620 return !bt_device_add(endp
);
622 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
626 /***********************************************************/
627 /* QEMU Block devices */
629 #define HD_OPTS "media=disk"
630 #define CDROM_OPTS "media=cdrom"
632 #define PFLASH_OPTS ""
636 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
638 int *use_scsi
= opaque
;
640 return drive_init(opts
, *use_scsi
) == NULL
;
643 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
645 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
646 qemu_opt_set(opts
, "snapshot", "on");
651 static void default_drive(int enable
, int snapshot
, int use_scsi
,
652 BlockInterfaceType type
, int index
,
657 if (type
== IF_DEFAULT
) {
658 type
= use_scsi
? IF_SCSI
: IF_IDE
;
661 if (!enable
|| drive_get_by_index(type
, index
)) {
665 opts
= drive_add(type
, index
, NULL
, optstr
);
667 drive_enable_snapshot(opts
, NULL
);
669 if (!drive_init(opts
, use_scsi
)) {
674 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
676 boot_set_handler
= func
;
677 boot_set_opaque
= opaque
;
680 int qemu_boot_set(const char *boot_devices
)
682 if (!boot_set_handler
) {
685 return boot_set_handler(boot_set_opaque
, boot_devices
);
688 static void validate_bootdevices(char *devices
)
690 /* We just do some generic consistency checks */
694 for (p
= devices
; *p
!= '\0'; p
++) {
695 /* Allowed boot devices are:
696 * a-b: floppy disk drives
697 * c-f: IDE disk drives
698 * g-m: machine implementation dependant drives
699 * n-p: network devices
700 * It's up to each machine implementation to check if the given boot
701 * devices match the actual hardware implementation and firmware
704 if (*p
< 'a' || *p
> 'p') {
705 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
708 if (bitmap
& (1 << (*p
- 'a'))) {
709 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
712 bitmap
|= 1 << (*p
- 'a');
716 static void restore_boot_devices(void *opaque
)
718 char *standard_boot_devices
= opaque
;
719 static int first
= 1;
721 /* Restore boot order and remove ourselves after the first boot */
727 qemu_boot_set(standard_boot_devices
);
729 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
730 qemu_free(standard_boot_devices
);
733 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
736 FWBootEntry
*node
, *i
;
742 assert(dev
!= NULL
|| suffix
!= NULL
);
744 node
= qemu_mallocz(sizeof(FWBootEntry
));
745 node
->bootindex
= bootindex
;
746 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
749 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
750 if (i
->bootindex
== bootindex
) {
751 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
753 } else if (i
->bootindex
< bootindex
) {
756 QTAILQ_INSERT_BEFORE(i
, node
, link
);
759 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
763 * This function returns null terminated string that consist of new line
764 * separated device paths.
766 * memory pointed by "size" is assigned total length of the array in bytes
769 char *get_boot_devices_list(uint32_t *size
)
775 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
776 char *devpath
= NULL
, *bootpath
;
780 devpath
= qdev_get_fw_dev_path(i
->dev
);
784 if (i
->suffix
&& devpath
) {
785 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
787 bootpath
= qemu_malloc(bootpathlen
);
788 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
790 } else if (devpath
) {
793 bootpath
= qemu_strdup(i
->suffix
);
798 list
[total
-1] = '\n';
800 len
= strlen(bootpath
) + 1;
801 list
= qemu_realloc(list
, total
+ len
);
802 memcpy(&list
[total
], bootpath
, len
);
812 static void numa_add(const char *optarg
)
816 unsigned long long value
, endvalue
;
819 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
820 if (!strcmp(option
, "node")) {
821 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
822 nodenr
= nb_numa_nodes
;
824 nodenr
= strtoull(option
, NULL
, 10);
827 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
828 node_mem
[nodenr
] = 0;
831 sval
= strtosz(option
, NULL
);
833 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
836 node_mem
[nodenr
] = sval
;
838 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
839 node_cpumask
[nodenr
] = 0;
841 value
= strtoull(option
, &endptr
, 10);
844 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
846 if (*endptr
== '-') {
847 endvalue
= strtoull(endptr
+1, &endptr
, 10);
848 if (endvalue
>= 63) {
851 "only 63 CPUs in NUMA mode supported.\n");
853 value
= (2ULL << endvalue
) - (1ULL << value
);
855 value
= 1ULL << value
;
858 node_cpumask
[nodenr
] = value
;
865 static void smp_parse(const char *optarg
)
867 int smp
, sockets
= 0, threads
= 0, cores
= 0;
871 smp
= strtoul(optarg
, &endptr
, 10);
872 if (endptr
!= optarg
) {
873 if (*endptr
== ',') {
877 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
878 sockets
= strtoull(option
, NULL
, 10);
879 if (get_param_value(option
, 128, "cores", endptr
) != 0)
880 cores
= strtoull(option
, NULL
, 10);
881 if (get_param_value(option
, 128, "threads", endptr
) != 0)
882 threads
= strtoull(option
, NULL
, 10);
883 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
884 max_cpus
= strtoull(option
, NULL
, 10);
886 /* compute missing values, prefer sockets over cores over threads */
887 if (smp
== 0 || sockets
== 0) {
888 sockets
= sockets
> 0 ? sockets
: 1;
889 cores
= cores
> 0 ? cores
: 1;
890 threads
= threads
> 0 ? threads
: 1;
892 smp
= cores
* threads
* sockets
;
896 threads
= threads
> 0 ? threads
: 1;
897 cores
= smp
/ (sockets
* threads
);
899 threads
= smp
/ (cores
* sockets
);
903 smp_cores
= cores
> 0 ? cores
: 1;
904 smp_threads
= threads
> 0 ? threads
: 1;
909 /***********************************************************/
912 static int usb_device_add(const char *devname
)
915 USBDevice
*dev
= NULL
;
920 /* drivers with .usbdevice_name entry in USBDeviceInfo */
921 dev
= usbdevice_create(devname
);
926 if (strstart(devname
, "host:", &p
)) {
927 dev
= usb_host_device_open(p
);
928 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
929 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
930 bt_new_hci(qemu_find_bt_vlan(0)));
941 static int usb_device_del(const char *devname
)
946 if (strstart(devname
, "host:", &p
))
947 return usb_host_device_close(p
);
952 p
= strchr(devname
, '.');
955 bus_num
= strtoul(devname
, NULL
, 0);
956 addr
= strtoul(p
+ 1, NULL
, 0);
958 return usb_device_delete_addr(bus_num
, addr
);
961 static int usb_parse(const char *cmdline
)
964 r
= usb_device_add(cmdline
);
966 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
971 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
973 const char *devname
= qdict_get_str(qdict
, "devname");
974 if (usb_device_add(devname
) < 0) {
975 error_report("could not add USB device '%s'", devname
);
979 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
981 const char *devname
= qdict_get_str(qdict
, "devname");
982 if (usb_device_del(devname
) < 0) {
983 error_report("could not delete USB device '%s'", devname
);
987 /***********************************************************/
990 static struct pcmcia_socket_entry_s
{
991 PCMCIASocket
*socket
;
992 struct pcmcia_socket_entry_s
*next
;
993 } *pcmcia_sockets
= 0;
995 void pcmcia_socket_register(PCMCIASocket
*socket
)
997 struct pcmcia_socket_entry_s
*entry
;
999 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1000 entry
->socket
= socket
;
1001 entry
->next
= pcmcia_sockets
;
1002 pcmcia_sockets
= entry
;
1005 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1007 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1009 ptr
= &pcmcia_sockets
;
1010 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1011 if (entry
->socket
== socket
) {
1017 void pcmcia_info(Monitor
*mon
)
1019 struct pcmcia_socket_entry_s
*iter
;
1021 if (!pcmcia_sockets
)
1022 monitor_printf(mon
, "No PCMCIA sockets\n");
1024 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1025 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1026 iter
->socket
->attached
? iter
->socket
->card_string
:
1030 /***********************************************************/
1031 /* machine registration */
1033 static QEMUMachine
*first_machine
= NULL
;
1034 QEMUMachine
*current_machine
= NULL
;
1036 int qemu_register_machine(QEMUMachine
*m
)
1039 pm
= &first_machine
;
1047 static QEMUMachine
*find_machine(const char *name
)
1051 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1052 if (!strcmp(m
->name
, name
))
1054 if (m
->alias
&& !strcmp(m
->alias
, name
))
1060 static QEMUMachine
*find_default_machine(void)
1064 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1065 if (m
->is_default
) {
1072 /***********************************************************/
1073 /* main execution loop */
1075 static void gui_update(void *opaque
)
1077 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1078 DisplayState
*ds
= opaque
;
1079 DisplayChangeListener
*dcl
= ds
->listeners
;
1081 qemu_flush_coalesced_mmio_buffer();
1084 while (dcl
!= NULL
) {
1085 if (dcl
->gui_timer_interval
&&
1086 dcl
->gui_timer_interval
< interval
)
1087 interval
= dcl
->gui_timer_interval
;
1090 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1093 static void nographic_update(void *opaque
)
1095 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1097 qemu_flush_coalesced_mmio_buffer();
1098 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1101 struct vm_change_state_entry
{
1102 VMChangeStateHandler
*cb
;
1104 QLIST_ENTRY (vm_change_state_entry
) entries
;
1107 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1109 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1112 VMChangeStateEntry
*e
;
1114 e
= qemu_mallocz(sizeof (*e
));
1118 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1122 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1124 QLIST_REMOVE (e
, entries
);
1128 void vm_state_notify(int running
, int reason
)
1130 VMChangeStateEntry
*e
;
1132 trace_vm_state_notify(running
, reason
);
1134 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1135 e
->cb(e
->opaque
, running
, reason
);
1144 vm_state_notify(1, 0);
1146 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1150 /* reset/shutdown handler */
1152 typedef struct QEMUResetEntry
{
1153 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1154 QEMUResetHandler
*func
;
1158 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1159 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1160 static int reset_requested
;
1161 static int shutdown_requested
, shutdown_signal
= -1;
1162 static pid_t shutdown_pid
;
1163 static int powerdown_requested
;
1164 static int debug_requested
;
1165 static int vmstop_requested
;
1167 int qemu_no_shutdown(void)
1169 int r
= no_shutdown
;
1174 int qemu_shutdown_requested_get(void)
1176 return shutdown_requested
;
1179 int qemu_reset_requested_get(void)
1181 return reset_requested
;
1184 int qemu_shutdown_requested(void)
1186 int r
= shutdown_requested
;
1187 shutdown_requested
= 0;
1191 void qemu_kill_report(void)
1193 if (shutdown_signal
!= -1) {
1194 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1195 if (shutdown_pid
== 0) {
1196 /* This happens for eg ^C at the terminal, so it's worth
1197 * avoiding printing an odd message in that case.
1199 fputc('\n', stderr
);
1201 fprintf(stderr
, " from pid %d\n", shutdown_pid
);
1203 shutdown_signal
= -1;
1207 int qemu_reset_requested(void)
1209 int r
= reset_requested
;
1210 reset_requested
= 0;
1214 int qemu_powerdown_requested(void)
1216 int r
= powerdown_requested
;
1217 powerdown_requested
= 0;
1221 static int qemu_debug_requested(void)
1223 int r
= debug_requested
;
1224 debug_requested
= 0;
1228 static int qemu_vmstop_requested(void)
1230 int r
= vmstop_requested
;
1231 vmstop_requested
= 0;
1235 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1237 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1240 re
->opaque
= opaque
;
1241 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1244 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1248 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1249 if (re
->func
== func
&& re
->opaque
== opaque
) {
1250 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1257 void qemu_system_reset(void)
1259 QEMUResetEntry
*re
, *nre
;
1261 /* reset all devices */
1262 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1263 re
->func(re
->opaque
);
1265 monitor_protocol_event(QEVENT_RESET
, NULL
);
1266 cpu_synchronize_all_post_reset();
1269 void qemu_system_reset_request(void)
1272 shutdown_requested
= 1;
1274 reset_requested
= 1;
1276 if (cpu_single_env
) {
1277 cpu_single_env
->stopped
= 1;
1278 cpu_exit(cpu_single_env
);
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 if (kvm_enabled()) {
1381 cpu_disable_ticks();
1385 qemu_main_loop_start();
1388 #ifndef CONFIG_IOTHREAD
1389 nonblocking
= cpu_exec_all();
1390 if (vm_request_pending()) {
1394 #ifdef CONFIG_PROFILER
1395 ti
= profile_getclock();
1397 main_loop_wait(nonblocking
);
1398 #ifdef CONFIG_PROFILER
1399 dev_time
+= profile_getclock() - ti
;
1402 if (qemu_debug_requested()) {
1403 vm_stop(VMSTOP_DEBUG
);
1405 if (qemu_shutdown_requested()) {
1407 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1409 vm_stop(VMSTOP_SHUTDOWN
);
1414 if (qemu_reset_requested()) {
1416 cpu_synchronize_all_states();
1417 qemu_system_reset();
1420 if (qemu_powerdown_requested()) {
1421 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1422 qemu_irq_raise(qemu_system_powerdown
);
1424 if ((r
= qemu_vmstop_requested())) {
1432 static void version(void)
1434 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1437 static void help(int exitcode
)
1439 const char *options_help
=
1440 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1442 #define DEFHEADING(text) stringify(text) "\n"
1443 #include "qemu-options.def"
1449 printf("usage: %s [options] [disk_image]\n"
1451 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1454 "During emulation, the following keys are useful:\n"
1455 "ctrl-alt-f toggle full screen\n"
1456 "ctrl-alt-n switch to virtual console 'n'\n"
1457 "ctrl-alt toggle mouse and keyboard grab\n"
1459 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1465 #define HAS_ARG 0x0001
1467 typedef struct QEMUOption
{
1474 static const QEMUOption qemu_options
[] = {
1475 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1476 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1477 { option, opt_arg, opt_enum, arch_mask },
1478 #define DEFHEADING(text)
1479 #include "qemu-options.def"
1485 static void select_vgahw (const char *p
)
1490 vga_interface_type
= VGA_NONE
;
1491 if (strstart(p
, "std", &opts
)) {
1492 vga_interface_type
= VGA_STD
;
1493 } else if (strstart(p
, "cirrus", &opts
)) {
1494 vga_interface_type
= VGA_CIRRUS
;
1495 } else if (strstart(p
, "vmware", &opts
)) {
1496 vga_interface_type
= VGA_VMWARE
;
1497 } else if (strstart(p
, "xenfb", &opts
)) {
1498 vga_interface_type
= VGA_XENFB
;
1499 } else if (strstart(p
, "qxl", &opts
)) {
1500 vga_interface_type
= VGA_QXL
;
1501 } else if (!strstart(p
, "none", &opts
)) {
1503 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1507 const char *nextopt
;
1509 if (strstart(opts
, ",retrace=", &nextopt
)) {
1511 if (strstart(opts
, "dumb", &nextopt
))
1512 vga_retrace_method
= VGA_RETRACE_DUMB
;
1513 else if (strstart(opts
, "precise", &nextopt
))
1514 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1515 else goto invalid_vga
;
1516 } else goto invalid_vga
;
1521 static DisplayType
select_display(const char *p
)
1524 DisplayType display
= DT_DEFAULT
;
1526 if (strstart(p
, "sdl", &opts
)) {
1530 const char *nextopt
;
1532 if (strstart(opts
, ",frame=", &nextopt
)) {
1534 if (strstart(opts
, "on", &nextopt
)) {
1536 } else if (strstart(opts
, "off", &nextopt
)) {
1539 goto invalid_sdl_args
;
1541 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1543 if (strstart(opts
, "on", &nextopt
)) {
1545 } else if (strstart(opts
, "off", &nextopt
)) {
1548 goto invalid_sdl_args
;
1550 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1552 if (strstart(opts
, "on", &nextopt
)) {
1554 } else if (strstart(opts
, "off", &nextopt
)) {
1557 goto invalid_sdl_args
;
1559 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1561 if (strstart(opts
, "on", &nextopt
)) {
1563 } else if (strstart(opts
, "off", &nextopt
)) {
1566 goto invalid_sdl_args
;
1570 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1576 fprintf(stderr
, "SDL support is disabled\n");
1579 } else if (strstart(p
, "vnc", &opts
)) {
1584 const char *nextopt
;
1586 if (strstart(opts
, "=", &nextopt
)) {
1587 vnc_display
= nextopt
;
1591 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1595 fprintf(stderr
, "VNC support is disabled\n");
1598 } else if (strstart(p
, "curses", &opts
)) {
1599 #ifdef CONFIG_CURSES
1600 display
= DT_CURSES
;
1602 fprintf(stderr
, "Curses support is disabled\n");
1605 } else if (strstart(p
, "none", &opts
)) {
1608 fprintf(stderr
, "Unknown display type: %s\n", p
);
1615 static int balloon_parse(const char *arg
)
1619 if (strcmp(arg
, "none") == 0) {
1623 if (!strncmp(arg
, "virtio", 6)) {
1624 if (arg
[6] == ',') {
1625 /* have params -> parse them */
1626 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1630 /* create empty opts */
1631 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1633 qemu_opt_set(opts
, "driver", "virtio-balloon");
1640 char *qemu_find_file(int type
, const char *name
)
1646 /* If name contains path separators then try it as a straight path. */
1647 if ((strchr(name
, '/') || strchr(name
, '\\'))
1648 && access(name
, R_OK
) == 0) {
1649 return qemu_strdup(name
);
1652 case QEMU_FILE_TYPE_BIOS
:
1655 case QEMU_FILE_TYPE_KEYMAP
:
1656 subdir
= "keymaps/";
1661 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1662 buf
= qemu_mallocz(len
);
1663 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1664 if (access(buf
, R_OK
)) {
1671 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1673 return qdev_device_help(opts
);
1676 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1680 dev
= qdev_device_add(opts
);
1686 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1688 CharDriverState
*chr
;
1690 chr
= qemu_chr_open_opts(opts
, NULL
);
1696 #ifdef CONFIG_VIRTFS
1697 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1700 ret
= qemu_fsdev_add(opts
);
1706 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1708 CharDriverState
*chr
;
1709 const char *chardev
;
1713 mode
= qemu_opt_get(opts
, "mode");
1717 if (strcmp(mode
, "readline") == 0) {
1718 flags
= MONITOR_USE_READLINE
;
1719 } else if (strcmp(mode
, "control") == 0) {
1720 flags
= MONITOR_USE_CONTROL
;
1722 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1726 if (qemu_opt_get_bool(opts
, "pretty", 0))
1727 flags
|= MONITOR_USE_PRETTY
;
1729 if (qemu_opt_get_bool(opts
, "default", 0))
1730 flags
|= MONITOR_IS_DEFAULT
;
1732 chardev
= qemu_opt_get(opts
, "chardev");
1733 chr
= qemu_chr_find(chardev
);
1735 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1739 monitor_init(chr
, flags
);
1743 static void monitor_parse(const char *optarg
, const char *mode
)
1745 static int monitor_device_index
= 0;
1751 if (strstart(optarg
, "chardev:", &p
)) {
1752 snprintf(label
, sizeof(label
), "%s", p
);
1754 snprintf(label
, sizeof(label
), "compat_monitor%d",
1755 monitor_device_index
);
1756 if (monitor_device_index
== 0) {
1759 opts
= qemu_chr_parse_compat(label
, optarg
);
1761 fprintf(stderr
, "parse error: %s\n", optarg
);
1766 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1768 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1771 qemu_opt_set(opts
, "mode", mode
);
1772 qemu_opt_set(opts
, "chardev", label
);
1774 qemu_opt_set(opts
, "default", "on");
1775 monitor_device_index
++;
1778 struct device_config
{
1780 DEV_USB
, /* -usbdevice */
1782 DEV_SERIAL
, /* -serial */
1783 DEV_PARALLEL
, /* -parallel */
1784 DEV_VIRTCON
, /* -virtioconsole */
1785 DEV_DEBUGCON
, /* -debugcon */
1787 const char *cmdline
;
1788 QTAILQ_ENTRY(device_config
) next
;
1790 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1792 static void add_device_config(int type
, const char *cmdline
)
1794 struct device_config
*conf
;
1796 conf
= qemu_mallocz(sizeof(*conf
));
1798 conf
->cmdline
= cmdline
;
1799 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1802 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1804 struct device_config
*conf
;
1807 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1808 if (conf
->type
!= type
)
1810 rc
= func(conf
->cmdline
);
1817 static int serial_parse(const char *devname
)
1819 static int index
= 0;
1822 if (strcmp(devname
, "none") == 0)
1824 if (index
== MAX_SERIAL_PORTS
) {
1825 fprintf(stderr
, "qemu: too many serial ports\n");
1828 snprintf(label
, sizeof(label
), "serial%d", index
);
1829 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1830 if (!serial_hds
[index
]) {
1831 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1832 devname
, strerror(errno
));
1839 static int parallel_parse(const char *devname
)
1841 static int index
= 0;
1844 if (strcmp(devname
, "none") == 0)
1846 if (index
== MAX_PARALLEL_PORTS
) {
1847 fprintf(stderr
, "qemu: too many parallel ports\n");
1850 snprintf(label
, sizeof(label
), "parallel%d", index
);
1851 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1852 if (!parallel_hds
[index
]) {
1853 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1854 devname
, strerror(errno
));
1861 static int virtcon_parse(const char *devname
)
1863 QemuOptsList
*device
= qemu_find_opts("device");
1864 static int index
= 0;
1866 QemuOpts
*bus_opts
, *dev_opts
;
1868 if (strcmp(devname
, "none") == 0)
1870 if (index
== MAX_VIRTIO_CONSOLES
) {
1871 fprintf(stderr
, "qemu: too many virtio consoles\n");
1875 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1876 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1878 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1879 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1881 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1882 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1883 if (!virtcon_hds
[index
]) {
1884 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1885 devname
, strerror(errno
));
1888 qemu_opt_set(dev_opts
, "chardev", label
);
1894 static int debugcon_parse(const char *devname
)
1898 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1901 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1903 fprintf(stderr
, "qemu: already have a debugcon device\n");
1906 qemu_opt_set(opts
, "driver", "isa-debugcon");
1907 qemu_opt_set(opts
, "chardev", "debugcon");
1911 static int tcg_init(void)
1913 fprintf(stderr
, "Emulation temporarily broken\n");
1918 const char *opt_name
;
1920 int (*available
)(void);
1924 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
1925 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
1926 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
1929 static int configure_accelerator(void)
1931 const char *p
= NULL
;
1934 bool accel_initalised
= 0;
1935 bool init_failed
= 0;
1937 QemuOptsList
*list
= qemu_find_opts("machine");
1938 if (!QTAILQ_EMPTY(&list
->head
)) {
1939 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
1943 /* Use the default "accelerator", kvm */
1947 while (!accel_initalised
&& *p
!= '\0') {
1951 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
1952 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
1953 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
1954 ret
= accel_list
[i
].init();
1957 if (!accel_list
[i
].available()) {
1958 printf("%s not supported for this target\n",
1959 accel_list
[i
].name
);
1961 fprintf(stderr
, "failed to initialize %s: %s\n",
1966 accel_initalised
= 1;
1967 *(accel_list
[i
].allowed
) = 1;
1972 if (i
== ARRAY_SIZE(accel_list
)) {
1973 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
1977 if (!accel_initalised
) {
1978 fprintf(stderr
, "No accelerator found!\n");
1983 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
1986 return !accel_initalised
;
1989 void qemu_add_exit_notifier(Notifier
*notify
)
1991 notifier_list_add(&exit_notifiers
, notify
);
1994 void qemu_remove_exit_notifier(Notifier
*notify
)
1996 notifier_list_remove(&exit_notifiers
, notify
);
1999 static void qemu_run_exit_notifiers(void)
2001 notifier_list_notify(&exit_notifiers
);
2004 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2006 notifier_list_add(&machine_init_done_notifiers
, notify
);
2009 static void qemu_run_machine_init_done_notifiers(void)
2011 notifier_list_notify(&machine_init_done_notifiers
);
2014 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2015 const char **poptarg
, int *poptind
)
2017 const QEMUOption
*popt
;
2018 int optind
= *poptind
;
2019 char *r
= argv
[optind
];
2022 loc_set_cmdline(argv
, optind
, 1);
2024 /* Treat --foo the same as -foo. */
2027 popt
= qemu_options
;
2030 error_report("invalid option");
2033 if (!strcmp(popt
->name
, r
+ 1))
2037 if (popt
->flags
& HAS_ARG
) {
2038 if (optind
>= argc
) {
2039 error_report("requires an argument");
2042 optarg
= argv
[optind
++];
2043 loc_set_cmdline(argv
, optind
- 2, 2);
2054 int main(int argc
, char **argv
, char **envp
)
2056 const char *gdbstub_dev
= NULL
;
2058 int snapshot
, linux_boot
;
2059 const char *icount_option
= NULL
;
2060 const char *initrd_filename
;
2061 const char *kernel_filename
, *kernel_cmdline
;
2062 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2064 DisplayChangeListener
*dcl
;
2065 int cyls
, heads
, secs
, translation
;
2066 QemuOpts
*hda_opts
= NULL
, *opts
;
2067 QemuOptsList
*olist
;
2070 const char *loadvm
= NULL
;
2071 QEMUMachine
*machine
;
2072 const char *cpu_model
;
2074 const char *pid_file
= NULL
;
2075 const char *incoming
= NULL
;
2077 int show_vnc_port
= 0;
2080 const char *trace_file
= NULL
;
2082 atexit(qemu_run_exit_notifiers
);
2083 error_set_progname(argv
[0]);
2087 qemu_cache_utils_init(envp
);
2089 QLIST_INIT (&vm_change_state_head
);
2090 os_setup_early_signal_handling();
2092 module_call_init(MODULE_INIT_MACHINE
);
2093 machine
= find_default_machine();
2095 initrd_filename
= NULL
;
2098 kernel_filename
= NULL
;
2099 kernel_cmdline
= "";
2100 cyls
= heads
= secs
= 0;
2101 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2103 for (i
= 0; i
< MAX_NODES
; i
++) {
2105 node_cpumask
[i
] = 0;
2114 /* first pass of option parsing */
2116 while (optind
< argc
) {
2117 if (argv
[optind
][0] != '-') {
2122 const QEMUOption
*popt
;
2124 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2125 switch (popt
->index
) {
2126 case QEMU_OPTION_nodefconfig
:
2136 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2137 if (ret
< 0 && ret
!= -ENOENT
) {
2141 ret
= qemu_read_config_file(arch_config_name
);
2142 if (ret
< 0 && ret
!= -ENOENT
) {
2148 /* second pass of option parsing */
2153 if (argv
[optind
][0] != '-') {
2154 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2156 const QEMUOption
*popt
;
2158 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2159 if (!(popt
->arch_mask
& arch_type
)) {
2160 printf("Option %s not supported for this target\n", popt
->name
);
2163 switch(popt
->index
) {
2165 machine
= find_machine(optarg
);
2168 printf("Supported machines are:\n");
2169 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2171 printf("%-10s %s (alias of %s)\n",
2172 m
->alias
, m
->desc
, m
->name
);
2173 printf("%-10s %s%s\n",
2175 m
->is_default
? " (default)" : "");
2177 exit(*optarg
!= '?');
2180 case QEMU_OPTION_cpu
:
2181 /* hw initialization will check this */
2182 if (*optarg
== '?') {
2183 list_cpus(stdout
, &fprintf
, optarg
);
2189 case QEMU_OPTION_initrd
:
2190 initrd_filename
= optarg
;
2192 case QEMU_OPTION_hda
:
2196 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2198 snprintf(buf
, sizeof(buf
),
2199 "%s,cyls=%d,heads=%d,secs=%d%s",
2200 HD_OPTS
, cyls
, heads
, secs
,
2201 translation
== BIOS_ATA_TRANSLATION_LBA
?
2203 translation
== BIOS_ATA_TRANSLATION_NONE
?
2204 ",trans=none" : "");
2205 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2208 case QEMU_OPTION_hdb
:
2209 case QEMU_OPTION_hdc
:
2210 case QEMU_OPTION_hdd
:
2211 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2214 case QEMU_OPTION_drive
:
2215 if (drive_def(optarg
) == NULL
) {
2219 case QEMU_OPTION_set
:
2220 if (qemu_set_option(optarg
) != 0)
2223 case QEMU_OPTION_global
:
2224 if (qemu_global_option(optarg
) != 0)
2227 case QEMU_OPTION_mtdblock
:
2228 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2230 case QEMU_OPTION_sd
:
2231 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2233 case QEMU_OPTION_pflash
:
2234 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2236 case QEMU_OPTION_snapshot
:
2239 case QEMU_OPTION_hdachs
:
2243 cyls
= strtol(p
, (char **)&p
, 0);
2244 if (cyls
< 1 || cyls
> 16383)
2249 heads
= strtol(p
, (char **)&p
, 0);
2250 if (heads
< 1 || heads
> 16)
2255 secs
= strtol(p
, (char **)&p
, 0);
2256 if (secs
< 1 || secs
> 63)
2260 if (!strcmp(p
, "none"))
2261 translation
= BIOS_ATA_TRANSLATION_NONE
;
2262 else if (!strcmp(p
, "lba"))
2263 translation
= BIOS_ATA_TRANSLATION_LBA
;
2264 else if (!strcmp(p
, "auto"))
2265 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2268 } else if (*p
!= '\0') {
2270 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2273 if (hda_opts
!= NULL
) {
2275 snprintf(num
, sizeof(num
), "%d", cyls
);
2276 qemu_opt_set(hda_opts
, "cyls", num
);
2277 snprintf(num
, sizeof(num
), "%d", heads
);
2278 qemu_opt_set(hda_opts
, "heads", num
);
2279 snprintf(num
, sizeof(num
), "%d", secs
);
2280 qemu_opt_set(hda_opts
, "secs", num
);
2281 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2282 qemu_opt_set(hda_opts
, "trans", "lba");
2283 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2284 qemu_opt_set(hda_opts
, "trans", "none");
2288 case QEMU_OPTION_numa
:
2289 if (nb_numa_nodes
>= MAX_NODES
) {
2290 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2295 case QEMU_OPTION_display
:
2296 display_type
= select_display(optarg
);
2298 case QEMU_OPTION_nographic
:
2299 display_type
= DT_NOGRAPHIC
;
2301 case QEMU_OPTION_curses
:
2302 #ifdef CONFIG_CURSES
2303 display_type
= DT_CURSES
;
2305 fprintf(stderr
, "Curses support is disabled\n");
2309 case QEMU_OPTION_portrait
:
2312 case QEMU_OPTION_kernel
:
2313 kernel_filename
= optarg
;
2315 case QEMU_OPTION_append
:
2316 kernel_cmdline
= optarg
;
2318 case QEMU_OPTION_cdrom
:
2319 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2321 case QEMU_OPTION_boot
:
2323 static const char * const params
[] = {
2324 "order", "once", "menu", NULL
2326 char buf
[sizeof(boot_devices
)];
2327 char *standard_boot_devices
;
2330 if (!strchr(optarg
, '=')) {
2332 pstrcpy(buf
, sizeof(buf
), optarg
);
2333 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2335 "qemu: unknown boot parameter '%s' in '%s'\n",
2341 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2342 validate_bootdevices(buf
);
2343 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2346 if (get_param_value(buf
, sizeof(buf
),
2348 validate_bootdevices(buf
);
2349 standard_boot_devices
= qemu_strdup(boot_devices
);
2350 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2351 qemu_register_reset(restore_boot_devices
,
2352 standard_boot_devices
);
2354 if (get_param_value(buf
, sizeof(buf
),
2356 if (!strcmp(buf
, "on")) {
2358 } else if (!strcmp(buf
, "off")) {
2362 "qemu: invalid option value '%s'\n",
2370 case QEMU_OPTION_fda
:
2371 case QEMU_OPTION_fdb
:
2372 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2375 case QEMU_OPTION_no_fd_bootchk
:
2378 case QEMU_OPTION_netdev
:
2379 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2383 case QEMU_OPTION_net
:
2384 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2389 case QEMU_OPTION_tftp
:
2390 legacy_tftp_prefix
= optarg
;
2392 case QEMU_OPTION_bootp
:
2393 legacy_bootp_filename
= optarg
;
2395 case QEMU_OPTION_redir
:
2396 if (net_slirp_redir(optarg
) < 0)
2400 case QEMU_OPTION_bt
:
2401 add_device_config(DEV_BT
, optarg
);
2403 case QEMU_OPTION_audio_help
:
2404 if (!(audio_available())) {
2405 printf("Option %s not supported for this target\n", popt
->name
);
2411 case QEMU_OPTION_soundhw
:
2412 if (!(audio_available())) {
2413 printf("Option %s not supported for this target\n", popt
->name
);
2416 select_soundhw (optarg
);
2421 case QEMU_OPTION_version
:
2425 case QEMU_OPTION_m
: {
2428 value
= strtosz(optarg
, NULL
);
2430 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2434 /* On 32-bit hosts, QEMU is limited by virtual address space */
2435 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2436 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2439 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2440 fprintf(stderr
, "qemu: ram size too large\n");
2446 case QEMU_OPTION_mempath
:
2450 case QEMU_OPTION_mem_prealloc
:
2455 set_cpu_log(optarg
);
2458 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2460 case QEMU_OPTION_gdb
:
2461 gdbstub_dev
= optarg
;
2466 case QEMU_OPTION_bios
:
2469 case QEMU_OPTION_singlestep
:
2476 keyboard_layout
= optarg
;
2478 case QEMU_OPTION_localtime
:
2481 case QEMU_OPTION_vga
:
2482 select_vgahw (optarg
);
2489 w
= strtol(p
, (char **)&p
, 10);
2492 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2498 h
= strtol(p
, (char **)&p
, 10);
2503 depth
= strtol(p
, (char **)&p
, 10);
2504 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2505 depth
!= 24 && depth
!= 32)
2507 } else if (*p
== '\0') {
2508 depth
= graphic_depth
;
2515 graphic_depth
= depth
;
2518 case QEMU_OPTION_echr
:
2521 term_escape_char
= strtol(optarg
, &r
, 0);
2523 printf("Bad argument to echr\n");
2526 case QEMU_OPTION_monitor
:
2527 monitor_parse(optarg
, "readline");
2528 default_monitor
= 0;
2530 case QEMU_OPTION_qmp
:
2531 monitor_parse(optarg
, "control");
2532 default_monitor
= 0;
2534 case QEMU_OPTION_mon
:
2535 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2539 default_monitor
= 0;
2541 case QEMU_OPTION_chardev
:
2542 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2547 case QEMU_OPTION_fsdev
:
2548 olist
= qemu_find_opts("fsdev");
2550 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2553 opts
= qemu_opts_parse(olist
, optarg
, 1);
2555 fprintf(stderr
, "parse error: %s\n", optarg
);
2559 case QEMU_OPTION_virtfs
: {
2563 olist
= qemu_find_opts("virtfs");
2565 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2568 opts
= qemu_opts_parse(olist
, optarg
, 1);
2570 fprintf(stderr
, "parse error: %s\n", optarg
);
2574 if (qemu_opt_get(opts
, "fstype") == NULL
||
2575 qemu_opt_get(opts
, "mount_tag") == NULL
||
2576 qemu_opt_get(opts
, "path") == NULL
||
2577 qemu_opt_get(opts
, "security_model") == NULL
) {
2578 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2579 "security_model=[mapped|passthrough|none],"
2580 "mount_tag=tag.\n");
2584 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2585 qemu_opt_get(opts
, "mount_tag"), 1);
2587 fprintf(stderr
, "duplicate fsdev id: %s\n",
2588 qemu_opt_get(opts
, "mount_tag"));
2591 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2592 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2593 qemu_opt_set(fsdev
, "security_model",
2594 qemu_opt_get(opts
, "security_model"));
2596 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2597 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2598 qemu_opt_set(device
, "fsdev",
2599 qemu_opt_get(opts
, "mount_tag"));
2600 qemu_opt_set(device
, "mount_tag",
2601 qemu_opt_get(opts
, "mount_tag"));
2604 case QEMU_OPTION_serial
:
2605 add_device_config(DEV_SERIAL
, optarg
);
2607 if (strncmp(optarg
, "mon:", 4) == 0) {
2608 default_monitor
= 0;
2611 case QEMU_OPTION_watchdog
:
2614 "qemu: only one watchdog option may be given\n");
2619 case QEMU_OPTION_watchdog_action
:
2620 if (select_watchdog_action(optarg
) == -1) {
2621 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2625 case QEMU_OPTION_virtiocon
:
2626 add_device_config(DEV_VIRTCON
, optarg
);
2627 default_virtcon
= 0;
2628 if (strncmp(optarg
, "mon:", 4) == 0) {
2629 default_monitor
= 0;
2632 case QEMU_OPTION_parallel
:
2633 add_device_config(DEV_PARALLEL
, optarg
);
2634 default_parallel
= 0;
2635 if (strncmp(optarg
, "mon:", 4) == 0) {
2636 default_monitor
= 0;
2639 case QEMU_OPTION_debugcon
:
2640 add_device_config(DEV_DEBUGCON
, optarg
);
2642 case QEMU_OPTION_loadvm
:
2645 case QEMU_OPTION_full_screen
:
2649 case QEMU_OPTION_no_frame
:
2652 case QEMU_OPTION_alt_grab
:
2655 case QEMU_OPTION_ctrl_grab
:
2658 case QEMU_OPTION_no_quit
:
2661 case QEMU_OPTION_sdl
:
2662 display_type
= DT_SDL
;
2665 case QEMU_OPTION_no_frame
:
2666 case QEMU_OPTION_alt_grab
:
2667 case QEMU_OPTION_ctrl_grab
:
2668 case QEMU_OPTION_no_quit
:
2669 case QEMU_OPTION_sdl
:
2670 fprintf(stderr
, "SDL support is disabled\n");
2673 case QEMU_OPTION_pidfile
:
2676 case QEMU_OPTION_win2k_hack
:
2677 win2k_install_hack
= 1;
2679 case QEMU_OPTION_rtc_td_hack
:
2682 case QEMU_OPTION_acpitable
:
2683 do_acpitable_option(optarg
);
2685 case QEMU_OPTION_smbios
:
2686 do_smbios_option(optarg
);
2688 case QEMU_OPTION_enable_kvm
:
2689 olist
= qemu_find_opts("machine");
2690 qemu_opts_reset(olist
);
2691 qemu_opts_parse(olist
, "accel=kvm", 0);
2693 case QEMU_OPTION_machine
:
2694 olist
= qemu_find_opts("machine");
2695 qemu_opts_reset(olist
);
2696 opts
= qemu_opts_parse(olist
, optarg
, 0);
2698 fprintf(stderr
, "parse error: %s\n", optarg
);
2702 case QEMU_OPTION_no_kvm
:
2703 fprintf(stderr
, "Emulation temporarily broken\n");
2705 olist
= qemu_find_opts("machine");
2706 qemu_opts_reset(olist
);
2707 qemu_opts_parse(olist
, "accel=tcg", 0);
2710 case QEMU_OPTION_no_kvm_irqchip
: {
2715 case QEMU_OPTION_no_kvm_pit
: {
2719 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2720 kvm_pit_reinject
= 0;
2723 case QEMU_OPTION_enable_nesting
: {
2728 case QEMU_OPTION_usb
:
2731 case QEMU_OPTION_usbdevice
:
2733 add_device_config(DEV_USB
, optarg
);
2735 case QEMU_OPTION_device
:
2736 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2740 case QEMU_OPTION_smp
:
2743 fprintf(stderr
, "Invalid number of CPUs\n");
2746 if (max_cpus
< smp_cpus
) {
2747 fprintf(stderr
, "maxcpus must be equal to or greater than "
2751 if (max_cpus
> 255) {
2752 fprintf(stderr
, "Unsupported number of maxcpus\n");
2756 case QEMU_OPTION_vnc
:
2759 vnc_display
= optarg
;
2761 fprintf(stderr
, "VNC support is disabled\n");
2765 case QEMU_OPTION_no_acpi
:
2768 case QEMU_OPTION_no_hpet
:
2771 case QEMU_OPTION_balloon
:
2772 if (balloon_parse(optarg
) < 0) {
2773 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2777 case QEMU_OPTION_no_reboot
:
2780 case QEMU_OPTION_no_shutdown
:
2783 case QEMU_OPTION_show_cursor
:
2786 case QEMU_OPTION_uuid
:
2787 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2788 fprintf(stderr
, "Fail to parse UUID string."
2789 " Wrong format.\n");
2793 case QEMU_OPTION_option_rom
:
2794 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2795 fprintf(stderr
, "Too many option ROMs\n");
2798 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2799 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2800 option_rom
[nb_option_roms
].bootindex
=
2801 qemu_opt_get_number(opts
, "bootindex", -1);
2802 if (!option_rom
[nb_option_roms
].name
) {
2803 fprintf(stderr
, "Option ROM file is not specified\n");
2808 case QEMU_OPTION_semihosting
:
2809 semihosting_enabled
= 1;
2811 case QEMU_OPTION_tdf
:
2814 case QEMU_OPTION_kvm_shadow_memory
:
2815 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
2817 case QEMU_OPTION_name
:
2818 qemu_name
= qemu_strdup(optarg
);
2820 char *p
= strchr(qemu_name
, ',');
2823 if (strncmp(p
, "process=", 8)) {
2824 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2828 os_set_proc_name(p
);
2832 case QEMU_OPTION_prom_env
:
2833 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2834 fprintf(stderr
, "Too many prom variables\n");
2837 prom_envs
[nb_prom_envs
] = optarg
;
2840 case QEMU_OPTION_old_param
:
2843 case QEMU_OPTION_clock
:
2844 configure_alarms(optarg
);
2846 case QEMU_OPTION_startdate
:
2847 configure_rtc_date_offset(optarg
, 1);
2849 case QEMU_OPTION_rtc
:
2850 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2854 configure_rtc(opts
);
2856 case QEMU_OPTION_tb_size
:
2857 tb_size
= strtol(optarg
, NULL
, 0);
2861 case QEMU_OPTION_icount
:
2862 icount_option
= optarg
;
2864 case QEMU_OPTION_incoming
:
2866 incoming_expected
= true;
2868 case QEMU_OPTION_nodefaults
:
2870 default_parallel
= 0;
2871 default_virtcon
= 0;
2872 default_monitor
= 0;
2880 case QEMU_OPTION_nvram
:
2884 case QEMU_OPTION_xen_domid
:
2885 if (!(xen_available())) {
2886 printf("Option %s not supported for this target\n", popt
->name
);
2889 xen_domid
= atoi(optarg
);
2891 case QEMU_OPTION_xen_create
:
2892 if (!(xen_available())) {
2893 printf("Option %s not supported for this target\n", popt
->name
);
2896 xen_mode
= XEN_CREATE
;
2898 case QEMU_OPTION_xen_attach
:
2899 if (!(xen_available())) {
2900 printf("Option %s not supported for this target\n", popt
->name
);
2903 xen_mode
= XEN_ATTACH
;
2905 #ifdef CONFIG_SIMPLE_TRACE
2906 case QEMU_OPTION_trace
:
2907 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2909 trace_file
= qemu_opt_get(opts
, "file");
2913 case QEMU_OPTION_readconfig
:
2915 int ret
= qemu_read_config_file(optarg
);
2917 fprintf(stderr
, "read config %s: %s\n", optarg
,
2923 case QEMU_OPTION_spice
:
2924 olist
= qemu_find_opts("spice");
2926 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2929 opts
= qemu_opts_parse(olist
, optarg
, 0);
2931 fprintf(stderr
, "parse error: %s\n", optarg
);
2935 case QEMU_OPTION_writeconfig
:
2938 if (strcmp(optarg
, "-") == 0) {
2941 fp
= fopen(optarg
, "w");
2943 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2947 qemu_config_write(fp
);
2952 os_parse_cmd_args(popt
->index
, optarg
);
2958 if (!st_init(trace_file
)) {
2959 fprintf(stderr
, "warning: unable to initialize simple trace backend\n");
2962 /* If no data_dir is specified then try to find it relative to the
2965 data_dir
= os_find_datadir(argv
[0]);
2967 /* If all else fails use the install patch specified when building. */
2969 data_dir
= CONFIG_QEMU_DATADIR
;
2973 * Default to max_cpus = smp_cpus, in case the user doesn't
2974 * specify a max_cpus value.
2977 max_cpus
= smp_cpus
;
2979 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2980 if (smp_cpus
> machine
->max_cpus
) {
2981 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2982 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2988 * Get the default machine options from the machine if it is not already
2989 * specified either by the configuration file or by the command line.
2991 if (machine
->default_machine_opts
) {
2992 QemuOptsList
*list
= qemu_find_opts("machine");
2993 const char *p
= NULL
;
2995 if (!QTAILQ_EMPTY(&list
->head
)) {
2996 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2999 opts
= qemu_opts_parse(qemu_find_opts("machine"),
3000 machine
->default_machine_opts
, 0);
3002 fprintf(stderr
, "parse error for machine %s: %s\n",
3003 machine
->name
, machine
->default_machine_opts
);
3009 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3010 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3012 if (machine
->no_serial
) {
3015 if (machine
->no_parallel
) {
3016 default_parallel
= 0;
3018 if (!machine
->use_virtcon
) {
3019 default_virtcon
= 0;
3021 if (machine
->no_vga
) {
3024 if (machine
->no_floppy
) {
3027 if (machine
->no_cdrom
) {
3030 if (machine
->no_sdcard
) {
3034 if (display_type
== DT_NOGRAPHIC
) {
3035 if (default_parallel
)
3036 add_device_config(DEV_PARALLEL
, "null");
3037 if (default_serial
&& default_monitor
) {
3038 add_device_config(DEV_SERIAL
, "mon:stdio");
3039 } else if (default_virtcon
&& default_monitor
) {
3040 add_device_config(DEV_VIRTCON
, "mon:stdio");
3043 add_device_config(DEV_SERIAL
, "stdio");
3044 if (default_virtcon
)
3045 add_device_config(DEV_VIRTCON
, "stdio");
3046 if (default_monitor
)
3047 monitor_parse("stdio", "readline");
3051 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3052 if (default_parallel
)
3053 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3054 if (default_monitor
)
3055 monitor_parse("vc:80Cx24C", "readline");
3056 if (default_virtcon
)
3057 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3060 vga_interface_type
= VGA_CIRRUS
;
3064 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3066 #ifdef CONFIG_VIRTFS
3067 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3074 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3079 configure_accelerator();
3081 if (qemu_init_main_loop()) {
3082 fprintf(stderr
, "qemu_init_main_loop failed\n");
3085 linux_boot
= (kernel_filename
!= NULL
);
3087 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3088 fprintf(stderr
, "-append only allowed with -kernel option\n");
3092 if (!linux_boot
&& initrd_filename
!= NULL
) {
3093 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3097 os_set_line_buffering();
3099 if (init_timer_alarm() < 0) {
3100 fprintf(stderr
, "could not initialize alarm timer\n");
3103 configure_icount(icount_option
);
3105 if (net_init_clients() < 0) {
3109 /* init the bluetooth world */
3110 if (foreach_device_config(DEV_BT
, bt_parse
))
3113 /* init the memory */
3115 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3117 /* init the dynamic translator */
3118 cpu_exec_init_all(tb_size
* 1024 * 1024);
3120 bdrv_init_with_whitelist();
3124 /* open the virtual block devices */
3126 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3127 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3130 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3131 IF_DEFAULT
, 2, CDROM_OPTS
);
3132 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3133 IF_FLOPPY
, 0, FD_OPTS
);
3134 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3137 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3140 if (nb_numa_nodes
> 0) {
3143 if (nb_numa_nodes
> smp_cpus
) {
3144 nb_numa_nodes
= smp_cpus
;
3147 /* If no memory size if given for any node, assume the default case
3148 * and distribute the available memory equally across all nodes
3150 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3151 if (node_mem
[i
] != 0)
3154 if (i
== nb_numa_nodes
) {
3155 uint64_t usedmem
= 0;
3157 /* On Linux, the each node's border has to be 8MB aligned,
3158 * the final node gets the rest.
3160 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3161 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3162 usedmem
+= node_mem
[i
];
3164 node_mem
[i
] = ram_size
- usedmem
;
3167 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3168 if (node_cpumask
[i
] != 0)
3171 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3172 * must cope with this anyway, because there are BIOSes out there in
3173 * real machines which also use this scheme.
3175 if (i
== nb_numa_nodes
) {
3176 for (i
= 0; i
< smp_cpus
; i
++) {
3177 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3182 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3186 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3188 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3190 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3192 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3195 module_call_init(MODULE_INIT_DEVICE
);
3197 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3201 i
= select_watchdog(watchdog
);
3203 exit (i
== 1 ? 1 : 0);
3206 if (machine
->compat_props
) {
3207 qdev_prop_register_global_list(machine
->compat_props
);
3211 machine
->init(ram_size
, boot_devices
,
3212 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3214 cpu_synchronize_all_post_init();
3218 current_machine
= machine
;
3220 /* init USB devices */
3222 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3226 /* init generic devices */
3227 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3230 net_check_clients();
3232 /* just use the first displaystate for the moment */
3233 ds
= get_displaystate();
3237 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3238 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3239 display_type
= DT_SDL
;
3240 #elif defined(CONFIG_VNC)
3241 vnc_display
= "localhost:0,to=99";
3244 display_type
= DT_NONE
;
3249 /* init local displays */
3250 switch (display_type
) {
3253 #if defined(CONFIG_CURSES)
3255 curses_display_init(ds
, full_screen
);
3258 #if defined(CONFIG_SDL)
3260 sdl_display_init(ds
, full_screen
, no_frame
);
3262 #elif defined(CONFIG_COCOA)
3264 cocoa_display_init(ds
, full_screen
);
3271 /* must be after terminal init, SDL library changes signal handlers */
3272 os_setup_signal_handling();
3275 /* init remote displays */
3277 vnc_display_init(ds
);
3278 if (vnc_display_open(ds
, vnc_display
) < 0)
3281 if (show_vnc_port
) {
3282 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3287 if (using_spice
&& !qxl_enabled
) {
3288 qemu_spice_display_init(ds
);
3294 dcl
= ds
->listeners
;
3295 while (dcl
!= NULL
) {
3296 if (dcl
->dpy_refresh
!= NULL
) {
3297 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3298 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3303 if (ds
->gui_timer
== NULL
) {
3304 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3305 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3307 text_consoles_set_display(ds
);
3309 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3310 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3315 qdev_machine_creation_done();
3317 if (rom_load_all() != 0) {
3318 fprintf(stderr
, "rom loading failed\n");
3322 /* TODO: once all bus devices are qdevified, this should be done
3323 * when bus is created by qdev.c */
3324 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3325 qemu_run_machine_init_done_notifiers();
3327 qemu_system_reset();
3329 if (load_vmstate(loadvm
) < 0) {
3335 int ret
= qemu_start_incoming_migration(incoming
);
3337 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3341 } else if (autostart
) {