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>
50 #ifdef CONFIG_SIMPLE_TRACE
56 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
58 #include <sys/sysctl.h>
67 #include <linux/ppdev.h>
68 #include <linux/parport.h>
72 #include <sys/ethernet.h>
73 #include <sys/sockio.h>
74 #include <netinet/arp.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
77 #include <netinet/ip_icmp.h> // must come after ip.h
78 #include <netinet/udp.h>
79 #include <netinet/tcp.h>
87 #if defined(__OpenBSD__)
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
100 #if defined(__APPLE__) || defined(main)
102 int qemu_main(int argc
, char **argv
, char **envp
);
103 int main(int argc
, char **argv
)
105 return qemu_main(argc
, argv
, NULL
);
108 #define main qemu_main
110 #endif /* CONFIG_SDL */
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
118 #include "hw/boards.h"
120 #include "hw/pcmcia.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
129 #include "hw/loader.h"
132 #include "net/slirp.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
141 #include "blockdev.h"
142 #include "block-migration.h"
144 #include "audio/audio.h"
145 #include "migration.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
153 #include "fsdev/qemu-fsdev.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 int display_remote
= 0;
181 const char* keyboard_layout
= NULL
;
183 const char *mem_path
= NULL
;
185 int mem_prealloc
= 0; /* force preallocation of physical target memory */
188 NICInfo nd_table
[MAX_NICS
];
191 int incoming_expected
; /* Started with -incoming and waiting for incoming */
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_NONE
;
196 static int full_screen
= 0;
198 static int no_frame
= 0;
201 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
202 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
203 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
204 int win2k_install_hack
= 0;
212 const char *vnc_display
;
213 int acpi_enabled
= 1;
219 int graphic_rotate
= 0;
220 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
225 int time_drift_fix
= 0;
226 unsigned int kvm_shadow_memory
= 0;
228 const char *qemu_name
;
231 unsigned int nb_prom_envs
= 0;
232 const char *prom_envs
[MAX_PROM_ENVS
];
233 const char *nvram
= NULL
;
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 static QEMUTimer
*nographic_timer
;
253 uint8_t qemu_uuid
[16];
255 static QEMUBootSetHandler
*boot_set_handler
;
256 static void *boot_set_opaque
;
258 static NotifierList exit_notifiers
=
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
261 static NotifierList machine_init_done_notifiers
=
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
264 int kvm_allowed
= -1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
268 static int default_serial
= 1;
269 static int default_parallel
= 1;
270 static int default_virtcon
= 1;
271 static int default_monitor
= 1;
272 static int default_vga
= 1;
273 static int default_floppy
= 1;
274 static int default_cdrom
= 1;
275 static int default_sdcard
= 1;
281 { .driver
= "isa-serial", .flag
= &default_serial
},
282 { .driver
= "isa-parallel", .flag
= &default_parallel
},
283 { .driver
= "isa-fdc", .flag
= &default_floppy
},
284 { .driver
= "ide-drive", .flag
= &default_cdrom
},
285 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
286 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
287 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
288 { .driver
= "VGA", .flag
= &default_vga
},
289 { .driver
= "cirrus-vga", .flag
= &default_vga
},
290 { .driver
= "vmware-svga", .flag
= &default_vga
},
293 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
295 const char *driver
= qemu_opt_get(opts
, "driver");
300 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
301 if (strcmp(default_list
[i
].driver
, driver
) != 0)
303 *(default_list
[i
].flag
) = 0;
308 /***********************************************************/
309 /* real time host monotonic timer */
311 /***********************************************************/
312 /* host time/date access */
313 void qemu_get_timedate(struct tm
*tm
, int offset
)
320 if (rtc_date_offset
== -1) {
324 ret
= localtime(&ti
);
326 ti
-= rtc_date_offset
;
330 memcpy(tm
, ret
, sizeof(struct tm
));
333 int qemu_timedate_diff(struct tm
*tm
)
337 if (rtc_date_offset
== -1)
339 seconds
= mktimegm(tm
);
341 seconds
= mktime(tm
);
343 seconds
= mktimegm(tm
) + rtc_date_offset
;
345 return seconds
- time(NULL
);
348 void rtc_change_mon_event(struct tm
*tm
)
352 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
353 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
354 qobject_decref(data
);
357 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
359 time_t rtc_start_date
;
362 if (!strcmp(startdate
, "now") && legacy
) {
363 rtc_date_offset
= -1;
365 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
373 } else if (sscanf(startdate
, "%d-%d-%d",
385 rtc_start_date
= mktimegm(&tm
);
386 if (rtc_start_date
== -1) {
388 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
389 "'2006-06-17T16:01:21' or '2006-06-17'\n");
392 rtc_date_offset
= time(NULL
) - rtc_start_date
;
396 static void configure_rtc(QemuOpts
*opts
)
400 value
= qemu_opt_get(opts
, "base");
402 if (!strcmp(value
, "utc")) {
404 } else if (!strcmp(value
, "localtime")) {
407 configure_rtc_date_offset(value
, 0);
410 value
= qemu_opt_get(opts
, "clock");
412 if (!strcmp(value
, "host")) {
413 rtc_clock
= host_clock
;
414 } else if (!strcmp(value
, "vm")) {
415 rtc_clock
= vm_clock
;
417 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
421 value
= qemu_opt_get(opts
, "driftfix");
423 if (!strcmp(value
, "slew")) {
425 } else if (!strcmp(value
, "none")) {
428 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
434 /***********************************************************/
435 /* Bluetooth support */
438 static struct HCIInfo
*hci_table
[MAX_NICS
];
440 static struct bt_vlan_s
{
441 struct bt_scatternet_s net
;
443 struct bt_vlan_s
*next
;
446 /* find or alloc a new bluetooth "VLAN" */
447 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
449 struct bt_vlan_s
**pvlan
, *vlan
;
450 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
454 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
456 pvlan
= &first_bt_vlan
;
457 while (*pvlan
!= NULL
)
458 pvlan
= &(*pvlan
)->next
;
463 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
467 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
472 static struct HCIInfo null_hci
= {
473 .cmd_send
= null_hci_send
,
474 .sco_send
= null_hci_send
,
475 .acl_send
= null_hci_send
,
476 .bdaddr_set
= null_hci_addr_set
,
479 struct HCIInfo
*qemu_next_hci(void)
481 if (cur_hci
== nb_hcis
)
484 return hci_table
[cur_hci
++];
487 static struct HCIInfo
*hci_init(const char *str
)
490 struct bt_scatternet_s
*vlan
= 0;
492 if (!strcmp(str
, "null"))
495 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
497 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
498 else if (!strncmp(str
, "hci", 3)) {
501 if (!strncmp(str
+ 3, ",vlan=", 6)) {
502 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
507 vlan
= qemu_find_bt_vlan(0);
509 return bt_new_hci(vlan
);
512 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
517 static int bt_hci_parse(const char *str
)
522 if (nb_hcis
>= MAX_NICS
) {
523 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
536 bdaddr
.b
[5] = 0x56 + nb_hcis
;
537 hci
->bdaddr_set(hci
, bdaddr
.b
);
539 hci_table
[nb_hcis
++] = hci
;
544 static void bt_vhci_add(int vlan_id
)
546 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
549 fprintf(stderr
, "qemu: warning: adding a VHCI to "
550 "an empty scatternet %i\n", vlan_id
);
552 bt_vhci_init(bt_new_hci(vlan
));
555 static struct bt_device_s
*bt_device_add(const char *opt
)
557 struct bt_scatternet_s
*vlan
;
559 char *endp
= strstr(opt
, ",vlan=");
560 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
563 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
566 vlan_id
= strtol(endp
+ 6, &endp
, 0);
568 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
573 vlan
= qemu_find_bt_vlan(vlan_id
);
576 fprintf(stderr
, "qemu: warning: adding a slave device to "
577 "an empty scatternet %i\n", vlan_id
);
579 if (!strcmp(devname
, "keyboard"))
580 return bt_keyboard_init(vlan
);
582 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
586 static int bt_parse(const char *opt
)
588 const char *endp
, *p
;
591 if (strstart(opt
, "hci", &endp
)) {
592 if (!*endp
|| *endp
== ',') {
594 if (!strstart(endp
, ",vlan=", 0))
597 return bt_hci_parse(opt
);
599 } else if (strstart(opt
, "vhci", &endp
)) {
600 if (!*endp
|| *endp
== ',') {
602 if (strstart(endp
, ",vlan=", &p
)) {
603 vlan
= strtol(p
, (char **) &endp
, 0);
605 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
609 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
618 } else if (strstart(opt
, "device:", &endp
))
619 return !bt_device_add(endp
);
621 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
625 /***********************************************************/
626 /* QEMU Block devices */
628 #define HD_OPTS "media=disk"
629 #define CDROM_OPTS "media=cdrom"
631 #define PFLASH_OPTS ""
635 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
637 int *use_scsi
= opaque
;
639 return drive_init(opts
, *use_scsi
) == NULL
;
642 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
644 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
645 qemu_opt_set(opts
, "snapshot", "on");
650 static void default_drive(int enable
, int snapshot
, int use_scsi
,
651 BlockInterfaceType type
, int index
,
656 if (type
== IF_DEFAULT
) {
657 type
= use_scsi
? IF_SCSI
: IF_IDE
;
660 if (!enable
|| drive_get_by_index(type
, index
)) {
664 opts
= drive_add(type
, index
, NULL
, optstr
);
666 drive_enable_snapshot(opts
, NULL
);
668 if (!drive_init(opts
, use_scsi
)) {
673 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
675 boot_set_handler
= func
;
676 boot_set_opaque
= opaque
;
679 int qemu_boot_set(const char *boot_devices
)
681 if (!boot_set_handler
) {
684 return boot_set_handler(boot_set_opaque
, boot_devices
);
687 static void validate_bootdevices(char *devices
)
689 /* We just do some generic consistency checks */
693 for (p
= devices
; *p
!= '\0'; p
++) {
694 /* Allowed boot devices are:
695 * a-b: floppy disk drives
696 * c-f: IDE disk drives
697 * g-m: machine implementation dependant drives
698 * n-p: network devices
699 * It's up to each machine implementation to check if the given boot
700 * devices match the actual hardware implementation and firmware
703 if (*p
< 'a' || *p
> 'p') {
704 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
707 if (bitmap
& (1 << (*p
- 'a'))) {
708 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
711 bitmap
|= 1 << (*p
- 'a');
715 static void restore_boot_devices(void *opaque
)
717 char *standard_boot_devices
= opaque
;
718 static int first
= 1;
720 /* Restore boot order and remove ourselves after the first boot */
726 qemu_boot_set(standard_boot_devices
);
728 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
729 qemu_free(standard_boot_devices
);
732 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
735 FWBootEntry
*node
, *i
;
741 assert(dev
!= NULL
|| suffix
!= NULL
);
743 node
= qemu_mallocz(sizeof(FWBootEntry
));
744 node
->bootindex
= bootindex
;
745 node
->suffix
= suffix
? qemu_strdup(suffix
) : NULL
;
748 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
749 if (i
->bootindex
== bootindex
) {
750 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
752 } else if (i
->bootindex
< bootindex
) {
755 QTAILQ_INSERT_BEFORE(i
, node
, link
);
758 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
762 * This function returns null terminated string that consist of new line
763 * separated device pathes.
765 * memory pointed by "size" is assigned total length of the array in bytes
768 char *get_boot_devices_list(uint32_t *size
)
774 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
775 char *devpath
= NULL
, *bootpath
;
779 devpath
= qdev_get_fw_dev_path(i
->dev
);
783 if (i
->suffix
&& devpath
) {
784 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
786 bootpath
= qemu_malloc(bootpathlen
);
787 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
789 } else if (devpath
) {
792 bootpath
= qemu_strdup(i
->suffix
);
797 list
[total
-1] = '\n';
799 len
= strlen(bootpath
) + 1;
800 list
= qemu_realloc(list
, total
+ len
);
801 memcpy(&list
[total
], bootpath
, len
);
811 static void numa_add(const char *optarg
)
815 unsigned long long value
, endvalue
;
818 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
819 if (!strcmp(option
, "node")) {
820 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
821 nodenr
= nb_numa_nodes
;
823 nodenr
= strtoull(option
, NULL
, 10);
826 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
827 node_mem
[nodenr
] = 0;
830 sval
= strtosz(option
, NULL
);
832 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
835 node_mem
[nodenr
] = sval
;
837 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
838 node_cpumask
[nodenr
] = 0;
840 value
= strtoull(option
, &endptr
, 10);
843 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
845 if (*endptr
== '-') {
846 endvalue
= strtoull(endptr
+1, &endptr
, 10);
847 if (endvalue
>= 63) {
850 "only 63 CPUs in NUMA mode supported.\n");
852 value
= (2ULL << endvalue
) - (1ULL << value
);
854 value
= 1ULL << value
;
857 node_cpumask
[nodenr
] = value
;
864 static void smp_parse(const char *optarg
)
866 int smp
, sockets
= 0, threads
= 0, cores
= 0;
870 smp
= strtoul(optarg
, &endptr
, 10);
871 if (endptr
!= optarg
) {
872 if (*endptr
== ',') {
876 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
877 sockets
= strtoull(option
, NULL
, 10);
878 if (get_param_value(option
, 128, "cores", endptr
) != 0)
879 cores
= strtoull(option
, NULL
, 10);
880 if (get_param_value(option
, 128, "threads", endptr
) != 0)
881 threads
= strtoull(option
, NULL
, 10);
882 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
883 max_cpus
= strtoull(option
, NULL
, 10);
885 /* compute missing values, prefer sockets over cores over threads */
886 if (smp
== 0 || sockets
== 0) {
887 sockets
= sockets
> 0 ? sockets
: 1;
888 cores
= cores
> 0 ? cores
: 1;
889 threads
= threads
> 0 ? threads
: 1;
891 smp
= cores
* threads
* sockets
;
895 threads
= threads
> 0 ? threads
: 1;
896 cores
= smp
/ (sockets
* threads
);
898 threads
= smp
/ (cores
* sockets
);
902 smp_cores
= cores
> 0 ? cores
: 1;
903 smp_threads
= threads
> 0 ? threads
: 1;
908 /***********************************************************/
911 static int usb_device_add(const char *devname
)
914 USBDevice
*dev
= NULL
;
919 /* drivers with .usbdevice_name entry in USBDeviceInfo */
920 dev
= usbdevice_create(devname
);
925 if (strstart(devname
, "host:", &p
)) {
926 dev
= usb_host_device_open(p
);
927 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
928 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
929 bt_new_hci(qemu_find_bt_vlan(0)));
940 static int usb_device_del(const char *devname
)
945 if (strstart(devname
, "host:", &p
))
946 return usb_host_device_close(p
);
951 p
= strchr(devname
, '.');
954 bus_num
= strtoul(devname
, NULL
, 0);
955 addr
= strtoul(p
+ 1, NULL
, 0);
957 return usb_device_delete_addr(bus_num
, addr
);
960 static int usb_parse(const char *cmdline
)
963 r
= usb_device_add(cmdline
);
965 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
970 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
972 const char *devname
= qdict_get_str(qdict
, "devname");
973 if (usb_device_add(devname
) < 0) {
974 error_report("could not add USB device '%s'", devname
);
978 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
980 const char *devname
= qdict_get_str(qdict
, "devname");
981 if (usb_device_del(devname
) < 0) {
982 error_report("could not delete USB device '%s'", devname
);
986 /***********************************************************/
989 static struct pcmcia_socket_entry_s
{
990 PCMCIASocket
*socket
;
991 struct pcmcia_socket_entry_s
*next
;
992 } *pcmcia_sockets
= 0;
994 void pcmcia_socket_register(PCMCIASocket
*socket
)
996 struct pcmcia_socket_entry_s
*entry
;
998 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
999 entry
->socket
= socket
;
1000 entry
->next
= pcmcia_sockets
;
1001 pcmcia_sockets
= entry
;
1004 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1006 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1008 ptr
= &pcmcia_sockets
;
1009 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1010 if (entry
->socket
== socket
) {
1016 void pcmcia_info(Monitor
*mon
)
1018 struct pcmcia_socket_entry_s
*iter
;
1020 if (!pcmcia_sockets
)
1021 monitor_printf(mon
, "No PCMCIA sockets\n");
1023 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1024 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1025 iter
->socket
->attached
? iter
->socket
->card_string
:
1029 /***********************************************************/
1032 typedef struct IOHandlerRecord
{
1034 IOCanReadHandler
*fd_read_poll
;
1036 IOHandler
*fd_write
;
1039 /* temporary data */
1041 QLIST_ENTRY(IOHandlerRecord
) next
;
1044 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1045 QLIST_HEAD_INITIALIZER(io_handlers
);
1048 /* XXX: fd_read_poll should be suppressed, but an API change is
1049 necessary in the character devices to suppress fd_can_read(). */
1050 int qemu_set_fd_handler2(int fd
,
1051 IOCanReadHandler
*fd_read_poll
,
1053 IOHandler
*fd_write
,
1056 IOHandlerRecord
*ioh
;
1058 if (!fd_read
&& !fd_write
) {
1059 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1060 if (ioh
->fd
== fd
) {
1066 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1070 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1071 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1074 ioh
->fd_read_poll
= fd_read_poll
;
1075 ioh
->fd_read
= fd_read
;
1076 ioh
->fd_write
= fd_write
;
1077 ioh
->opaque
= opaque
;
1080 qemu_notify_event();
1084 int qemu_set_fd_handler(int fd
,
1086 IOHandler
*fd_write
,
1089 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1092 /***********************************************************/
1093 /* machine registration */
1095 static QEMUMachine
*first_machine
= NULL
;
1096 QEMUMachine
*current_machine
= NULL
;
1098 int qemu_register_machine(QEMUMachine
*m
)
1101 pm
= &first_machine
;
1109 static QEMUMachine
*find_machine(const char *name
)
1113 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1114 if (!strcmp(m
->name
, name
))
1116 if (m
->alias
&& !strcmp(m
->alias
, name
))
1122 static QEMUMachine
*find_default_machine(void)
1126 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1127 if (m
->is_default
) {
1134 /***********************************************************/
1135 /* main execution loop */
1137 static void gui_update(void *opaque
)
1139 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1140 DisplayState
*ds
= opaque
;
1141 DisplayChangeListener
*dcl
= ds
->listeners
;
1143 qemu_flush_coalesced_mmio_buffer();
1146 while (dcl
!= NULL
) {
1147 if (dcl
->gui_timer_interval
&&
1148 dcl
->gui_timer_interval
< interval
)
1149 interval
= dcl
->gui_timer_interval
;
1152 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1155 static void nographic_update(void *opaque
)
1157 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1159 qemu_flush_coalesced_mmio_buffer();
1160 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1163 struct vm_change_state_entry
{
1164 VMChangeStateHandler
*cb
;
1166 QLIST_ENTRY (vm_change_state_entry
) entries
;
1169 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1171 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1174 VMChangeStateEntry
*e
;
1176 e
= qemu_mallocz(sizeof (*e
));
1180 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1184 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1186 QLIST_REMOVE (e
, entries
);
1190 void vm_state_notify(int running
, int reason
)
1192 VMChangeStateEntry
*e
;
1194 trace_vm_state_notify(running
, reason
);
1196 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1197 e
->cb(e
->opaque
, running
, reason
);
1206 vm_state_notify(1, 0);
1208 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1212 /* reset/shutdown handler */
1214 typedef struct QEMUResetEntry
{
1215 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1216 QEMUResetHandler
*func
;
1220 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1221 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1222 static int reset_requested
;
1223 static int shutdown_requested
;
1224 static int powerdown_requested
;
1225 int debug_requested
;
1226 int vmstop_requested
;
1228 int qemu_no_shutdown(void)
1230 int r
= no_shutdown
;
1235 int qemu_shutdown_requested(void)
1237 int r
= shutdown_requested
;
1238 shutdown_requested
= 0;
1242 int qemu_reset_requested(void)
1244 int r
= reset_requested
;
1245 reset_requested
= 0;
1249 int qemu_powerdown_requested(void)
1251 int r
= powerdown_requested
;
1252 powerdown_requested
= 0;
1256 static int qemu_debug_requested(void)
1258 int r
= debug_requested
;
1259 debug_requested
= 0;
1263 static int qemu_vmstop_requested(void)
1265 int r
= vmstop_requested
;
1266 vmstop_requested
= 0;
1270 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1272 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1275 re
->opaque
= opaque
;
1276 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1279 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1283 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1284 if (re
->func
== func
&& re
->opaque
== opaque
) {
1285 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1292 void qemu_system_reset(void)
1294 QEMUResetEntry
*re
, *nre
;
1296 /* reset all devices */
1297 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1298 re
->func(re
->opaque
);
1300 monitor_protocol_event(QEVENT_RESET
, NULL
);
1301 cpu_synchronize_all_post_reset();
1304 void qemu_system_reset_request(void)
1307 shutdown_requested
= 1;
1309 reset_requested
= 1;
1311 if (cpu_single_env
) {
1312 cpu_single_env
->stopped
= 1;
1313 cpu_exit(cpu_single_env
);
1316 qemu_notify_event();
1319 void qemu_system_shutdown_request(void)
1321 shutdown_requested
= 1;
1322 qemu_notify_event();
1325 void qemu_system_powerdown_request(void)
1327 powerdown_requested
= 1;
1328 qemu_notify_event();
1331 void main_loop_wait(int nonblocking
)
1333 IOHandlerRecord
*ioh
;
1334 fd_set rfds
, wfds
, xfds
;
1342 timeout
= qemu_calculate_timeout();
1343 qemu_bh_update_timeout(&timeout
);
1346 os_host_main_loop_wait(&timeout
);
1348 /* poll any events */
1349 /* XXX: separate device handlers from system ones */
1354 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1358 (!ioh
->fd_read_poll
||
1359 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1360 FD_SET(ioh
->fd
, &rfds
);
1364 if (ioh
->fd_write
) {
1365 FD_SET(ioh
->fd
, &wfds
);
1371 tv
.tv_sec
= timeout
/ 1000;
1372 tv
.tv_usec
= (timeout
% 1000) * 1000;
1374 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1376 qemu_mutex_unlock_iothread();
1377 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1378 qemu_mutex_lock_iothread();
1380 IOHandlerRecord
*pioh
;
1382 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1383 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1384 ioh
->fd_read(ioh
->opaque
);
1385 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
1386 FD_CLR(ioh
->fd
, &rfds
);
1388 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1389 ioh
->fd_write(ioh
->opaque
);
1392 /* Do this last in case read/write handlers marked it for deletion */
1394 QLIST_REMOVE(ioh
, next
);
1400 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1402 qemu_run_all_timers();
1404 /* Check bottom-halves last in case any of the earlier events triggered
1410 static int vm_can_run(void)
1412 if (powerdown_requested
)
1414 if (reset_requested
)
1416 if (shutdown_requested
)
1418 if (debug_requested
)
1423 qemu_irq qemu_system_powerdown
;
1425 static void main_loop(void)
1429 if (kvm_enabled()) {
1431 cpu_disable_ticks();
1435 qemu_main_loop_start();
1439 bool nonblocking
= false;
1440 #ifdef CONFIG_PROFILER
1443 #ifndef CONFIG_IOTHREAD
1444 nonblocking
= cpu_exec_all();
1446 #ifdef CONFIG_PROFILER
1447 ti
= profile_getclock();
1449 main_loop_wait(nonblocking
);
1450 #ifdef CONFIG_PROFILER
1451 dev_time
+= profile_getclock() - ti
;
1453 } while (vm_can_run());
1455 if ((r
= qemu_debug_requested())) {
1458 if (qemu_shutdown_requested()) {
1459 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1466 if (qemu_reset_requested()) {
1468 qemu_system_reset();
1471 if (qemu_powerdown_requested()) {
1472 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1473 qemu_irq_raise(qemu_system_powerdown
);
1475 if ((r
= qemu_vmstop_requested())) {
1483 static void version(void)
1485 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1488 static void help(int exitcode
)
1490 const char *options_help
=
1491 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1493 #define DEFHEADING(text) stringify(text) "\n"
1494 #include "qemu-options.def"
1500 printf("usage: %s [options] [disk_image]\n"
1502 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1505 "During emulation, the following keys are useful:\n"
1506 "ctrl-alt-f toggle full screen\n"
1507 "ctrl-alt-n switch to virtual console 'n'\n"
1508 "ctrl-alt toggle mouse and keyboard grab\n"
1510 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1516 #define HAS_ARG 0x0001
1518 typedef struct QEMUOption
{
1525 static const QEMUOption qemu_options
[] = {
1526 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1527 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1528 { option, opt_arg, opt_enum, arch_mask },
1529 #define DEFHEADING(text)
1530 #include "qemu-options.def"
1536 static void select_vgahw (const char *p
)
1541 vga_interface_type
= VGA_NONE
;
1542 if (strstart(p
, "std", &opts
)) {
1543 vga_interface_type
= VGA_STD
;
1544 } else if (strstart(p
, "cirrus", &opts
)) {
1545 vga_interface_type
= VGA_CIRRUS
;
1546 } else if (strstart(p
, "vmware", &opts
)) {
1547 vga_interface_type
= VGA_VMWARE
;
1548 } else if (strstart(p
, "xenfb", &opts
)) {
1549 vga_interface_type
= VGA_XENFB
;
1550 } else if (strstart(p
, "qxl", &opts
)) {
1551 vga_interface_type
= VGA_QXL
;
1552 } else if (!strstart(p
, "none", &opts
)) {
1554 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1558 const char *nextopt
;
1560 if (strstart(opts
, ",retrace=", &nextopt
)) {
1562 if (strstart(opts
, "dumb", &nextopt
))
1563 vga_retrace_method
= VGA_RETRACE_DUMB
;
1564 else if (strstart(opts
, "precise", &nextopt
))
1565 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1566 else goto invalid_vga
;
1567 } else goto invalid_vga
;
1572 static int balloon_parse(const char *arg
)
1576 if (strcmp(arg
, "none") == 0) {
1580 if (!strncmp(arg
, "virtio", 6)) {
1581 if (arg
[6] == ',') {
1582 /* have params -> parse them */
1583 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1587 /* create empty opts */
1588 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1590 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1597 char *qemu_find_file(int type
, const char *name
)
1603 /* If name contains path separators then try it as a straight path. */
1604 if ((strchr(name
, '/') || strchr(name
, '\\'))
1605 && access(name
, R_OK
) == 0) {
1606 return qemu_strdup(name
);
1609 case QEMU_FILE_TYPE_BIOS
:
1612 case QEMU_FILE_TYPE_KEYMAP
:
1613 subdir
= "keymaps/";
1618 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1619 buf
= qemu_mallocz(len
);
1620 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1621 if (access(buf
, R_OK
)) {
1628 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1630 return qdev_device_help(opts
);
1633 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1637 dev
= qdev_device_add(opts
);
1643 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1645 CharDriverState
*chr
;
1647 chr
= qemu_chr_open_opts(opts
, NULL
);
1653 #ifdef CONFIG_VIRTFS
1654 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1657 ret
= qemu_fsdev_add(opts
);
1663 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1665 CharDriverState
*chr
;
1666 const char *chardev
;
1670 mode
= qemu_opt_get(opts
, "mode");
1674 if (strcmp(mode
, "readline") == 0) {
1675 flags
= MONITOR_USE_READLINE
;
1676 } else if (strcmp(mode
, "control") == 0) {
1677 flags
= MONITOR_USE_CONTROL
;
1679 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1683 if (qemu_opt_get_bool(opts
, "pretty", 0))
1684 flags
|= MONITOR_USE_PRETTY
;
1686 if (qemu_opt_get_bool(opts
, "default", 0))
1687 flags
|= MONITOR_IS_DEFAULT
;
1689 chardev
= qemu_opt_get(opts
, "chardev");
1690 chr
= qemu_chr_find(chardev
);
1692 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1696 monitor_init(chr
, flags
);
1700 static void monitor_parse(const char *optarg
, const char *mode
)
1702 static int monitor_device_index
= 0;
1708 if (strstart(optarg
, "chardev:", &p
)) {
1709 snprintf(label
, sizeof(label
), "%s", p
);
1711 snprintf(label
, sizeof(label
), "compat_monitor%d",
1712 monitor_device_index
);
1713 if (monitor_device_index
== 0) {
1716 opts
= qemu_chr_parse_compat(label
, optarg
);
1718 fprintf(stderr
, "parse error: %s\n", optarg
);
1723 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1725 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1728 qemu_opt_set(opts
, "mode", mode
);
1729 qemu_opt_set(opts
, "chardev", label
);
1731 qemu_opt_set(opts
, "default", "on");
1732 monitor_device_index
++;
1735 struct device_config
{
1737 DEV_USB
, /* -usbdevice */
1739 DEV_SERIAL
, /* -serial */
1740 DEV_PARALLEL
, /* -parallel */
1741 DEV_VIRTCON
, /* -virtioconsole */
1742 DEV_DEBUGCON
, /* -debugcon */
1744 const char *cmdline
;
1745 QTAILQ_ENTRY(device_config
) next
;
1747 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1749 static void add_device_config(int type
, const char *cmdline
)
1751 struct device_config
*conf
;
1753 conf
= qemu_mallocz(sizeof(*conf
));
1755 conf
->cmdline
= cmdline
;
1756 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1759 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1761 struct device_config
*conf
;
1764 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1765 if (conf
->type
!= type
)
1767 rc
= func(conf
->cmdline
);
1774 static int serial_parse(const char *devname
)
1776 static int index
= 0;
1779 if (strcmp(devname
, "none") == 0)
1781 if (index
== MAX_SERIAL_PORTS
) {
1782 fprintf(stderr
, "qemu: too many serial ports\n");
1785 snprintf(label
, sizeof(label
), "serial%d", index
);
1786 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1787 if (!serial_hds
[index
]) {
1788 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1789 devname
, strerror(errno
));
1796 static int parallel_parse(const char *devname
)
1798 static int index
= 0;
1801 if (strcmp(devname
, "none") == 0)
1803 if (index
== MAX_PARALLEL_PORTS
) {
1804 fprintf(stderr
, "qemu: too many parallel ports\n");
1807 snprintf(label
, sizeof(label
), "parallel%d", index
);
1808 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1809 if (!parallel_hds
[index
]) {
1810 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1811 devname
, strerror(errno
));
1818 static int virtcon_parse(const char *devname
)
1820 QemuOptsList
*device
= qemu_find_opts("device");
1821 static int index
= 0;
1823 QemuOpts
*bus_opts
, *dev_opts
;
1825 if (strcmp(devname
, "none") == 0)
1827 if (index
== MAX_VIRTIO_CONSOLES
) {
1828 fprintf(stderr
, "qemu: too many virtio consoles\n");
1832 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1833 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1835 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1836 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1838 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1839 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1840 if (!virtcon_hds
[index
]) {
1841 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1842 devname
, strerror(errno
));
1845 qemu_opt_set(dev_opts
, "chardev", label
);
1851 static int debugcon_parse(const char *devname
)
1855 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1858 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1860 fprintf(stderr
, "qemu: already have a debugcon device\n");
1863 qemu_opt_set(opts
, "driver", "isa-debugcon");
1864 qemu_opt_set(opts
, "chardev", "debugcon");
1868 void qemu_add_exit_notifier(Notifier
*notify
)
1870 notifier_list_add(&exit_notifiers
, notify
);
1873 void qemu_remove_exit_notifier(Notifier
*notify
)
1875 notifier_list_remove(&exit_notifiers
, notify
);
1878 static void qemu_run_exit_notifiers(void)
1880 notifier_list_notify(&exit_notifiers
);
1883 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1885 notifier_list_add(&machine_init_done_notifiers
, notify
);
1888 static void qemu_run_machine_init_done_notifiers(void)
1890 notifier_list_notify(&machine_init_done_notifiers
);
1893 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1894 const char **poptarg
, int *poptind
)
1896 const QEMUOption
*popt
;
1897 int optind
= *poptind
;
1898 char *r
= argv
[optind
];
1901 loc_set_cmdline(argv
, optind
, 1);
1903 /* Treat --foo the same as -foo. */
1906 popt
= qemu_options
;
1909 error_report("invalid option");
1912 if (!strcmp(popt
->name
, r
+ 1))
1916 if (popt
->flags
& HAS_ARG
) {
1917 if (optind
>= argc
) {
1918 error_report("requires an argument");
1921 optarg
= argv
[optind
++];
1922 loc_set_cmdline(argv
, optind
- 2, 2);
1933 int main(int argc
, char **argv
, char **envp
)
1935 const char *gdbstub_dev
= NULL
;
1937 int snapshot
, linux_boot
;
1938 const char *icount_option
= NULL
;
1939 const char *initrd_filename
;
1940 const char *kernel_filename
, *kernel_cmdline
;
1941 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
1943 DisplayChangeListener
*dcl
;
1944 int cyls
, heads
, secs
, translation
;
1945 QemuOpts
*hda_opts
= NULL
, *opts
;
1946 QemuOptsList
*olist
;
1949 const char *loadvm
= NULL
;
1950 QEMUMachine
*machine
;
1951 const char *cpu_model
;
1953 const char *pid_file
= NULL
;
1954 const char *incoming
= NULL
;
1955 int show_vnc_port
= 0;
1958 #ifdef CONFIG_SIMPLE_TRACE
1959 const char *trace_file
= NULL
;
1961 atexit(qemu_run_exit_notifiers
);
1962 error_set_progname(argv
[0]);
1966 qemu_cache_utils_init(envp
);
1968 QLIST_INIT (&vm_change_state_head
);
1969 os_setup_early_signal_handling();
1971 module_call_init(MODULE_INIT_MACHINE
);
1972 machine
= find_default_machine();
1974 initrd_filename
= NULL
;
1977 kernel_filename
= NULL
;
1978 kernel_cmdline
= "";
1979 cyls
= heads
= secs
= 0;
1980 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1982 for (i
= 0; i
< MAX_NODES
; i
++) {
1984 node_cpumask
[i
] = 0;
1993 /* first pass of option parsing */
1995 while (optind
< argc
) {
1996 if (argv
[optind
][0] != '-') {
2001 const QEMUOption
*popt
;
2003 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2004 switch (popt
->index
) {
2005 case QEMU_OPTION_nodefconfig
:
2015 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2016 if (ret
< 0 && ret
!= -ENOENT
) {
2020 ret
= qemu_read_config_file(arch_config_name
);
2021 if (ret
< 0 && ret
!= -ENOENT
) {
2027 /* second pass of option parsing */
2032 if (argv
[optind
][0] != '-') {
2033 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2035 const QEMUOption
*popt
;
2037 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2038 if (!(popt
->arch_mask
& arch_type
)) {
2039 printf("Option %s not supported for this target\n", popt
->name
);
2042 switch(popt
->index
) {
2044 machine
= find_machine(optarg
);
2047 printf("Supported machines are:\n");
2048 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2050 printf("%-10s %s (alias of %s)\n",
2051 m
->alias
, m
->desc
, m
->name
);
2052 printf("%-10s %s%s\n",
2054 m
->is_default
? " (default)" : "");
2056 exit(*optarg
!= '?');
2059 case QEMU_OPTION_cpu
:
2060 /* hw initialization will check this */
2061 if (*optarg
== '?') {
2062 list_cpus(stdout
, &fprintf
, optarg
);
2068 case QEMU_OPTION_initrd
:
2069 initrd_filename
= optarg
;
2071 case QEMU_OPTION_hda
:
2075 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2077 snprintf(buf
, sizeof(buf
),
2078 "%s,cyls=%d,heads=%d,secs=%d%s",
2079 HD_OPTS
, cyls
, heads
, secs
,
2080 translation
== BIOS_ATA_TRANSLATION_LBA
?
2082 translation
== BIOS_ATA_TRANSLATION_NONE
?
2083 ",trans=none" : "");
2084 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2087 case QEMU_OPTION_hdb
:
2088 case QEMU_OPTION_hdc
:
2089 case QEMU_OPTION_hdd
:
2090 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2093 case QEMU_OPTION_drive
:
2096 case QEMU_OPTION_set
:
2097 if (qemu_set_option(optarg
) != 0)
2100 case QEMU_OPTION_global
:
2101 if (qemu_global_option(optarg
) != 0)
2104 case QEMU_OPTION_mtdblock
:
2105 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2107 case QEMU_OPTION_sd
:
2108 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2110 case QEMU_OPTION_pflash
:
2111 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2113 case QEMU_OPTION_snapshot
:
2116 case QEMU_OPTION_hdachs
:
2120 cyls
= strtol(p
, (char **)&p
, 0);
2121 if (cyls
< 1 || cyls
> 16383)
2126 heads
= strtol(p
, (char **)&p
, 0);
2127 if (heads
< 1 || heads
> 16)
2132 secs
= strtol(p
, (char **)&p
, 0);
2133 if (secs
< 1 || secs
> 63)
2137 if (!strcmp(p
, "none"))
2138 translation
= BIOS_ATA_TRANSLATION_NONE
;
2139 else if (!strcmp(p
, "lba"))
2140 translation
= BIOS_ATA_TRANSLATION_LBA
;
2141 else if (!strcmp(p
, "auto"))
2142 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2145 } else if (*p
!= '\0') {
2147 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2150 if (hda_opts
!= NULL
) {
2152 snprintf(num
, sizeof(num
), "%d", cyls
);
2153 qemu_opt_set(hda_opts
, "cyls", num
);
2154 snprintf(num
, sizeof(num
), "%d", heads
);
2155 qemu_opt_set(hda_opts
, "heads", num
);
2156 snprintf(num
, sizeof(num
), "%d", secs
);
2157 qemu_opt_set(hda_opts
, "secs", num
);
2158 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2159 qemu_opt_set(hda_opts
, "trans", "lba");
2160 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2161 qemu_opt_set(hda_opts
, "trans", "none");
2165 case QEMU_OPTION_numa
:
2166 if (nb_numa_nodes
>= MAX_NODES
) {
2167 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2172 case QEMU_OPTION_nographic
:
2173 display_type
= DT_NOGRAPHIC
;
2175 #ifdef CONFIG_CURSES
2176 case QEMU_OPTION_curses
:
2177 display_type
= DT_CURSES
;
2180 case QEMU_OPTION_portrait
:
2183 case QEMU_OPTION_kernel
:
2184 kernel_filename
= optarg
;
2186 case QEMU_OPTION_append
:
2187 kernel_cmdline
= optarg
;
2189 case QEMU_OPTION_cdrom
:
2190 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2192 case QEMU_OPTION_boot
:
2194 static const char * const params
[] = {
2195 "order", "once", "menu", NULL
2197 char buf
[sizeof(boot_devices
)];
2198 char *standard_boot_devices
;
2201 if (!strchr(optarg
, '=')) {
2203 pstrcpy(buf
, sizeof(buf
), optarg
);
2204 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2206 "qemu: unknown boot parameter '%s' in '%s'\n",
2212 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2213 validate_bootdevices(buf
);
2214 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2217 if (get_param_value(buf
, sizeof(buf
),
2219 validate_bootdevices(buf
);
2220 standard_boot_devices
= qemu_strdup(boot_devices
);
2221 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2222 qemu_register_reset(restore_boot_devices
,
2223 standard_boot_devices
);
2225 if (get_param_value(buf
, sizeof(buf
),
2227 if (!strcmp(buf
, "on")) {
2229 } else if (!strcmp(buf
, "off")) {
2233 "qemu: invalid option value '%s'\n",
2241 case QEMU_OPTION_fda
:
2242 case QEMU_OPTION_fdb
:
2243 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2246 case QEMU_OPTION_no_fd_bootchk
:
2249 case QEMU_OPTION_netdev
:
2250 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2254 case QEMU_OPTION_net
:
2255 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2260 case QEMU_OPTION_tftp
:
2261 legacy_tftp_prefix
= optarg
;
2263 case QEMU_OPTION_bootp
:
2264 legacy_bootp_filename
= optarg
;
2266 case QEMU_OPTION_redir
:
2267 if (net_slirp_redir(optarg
) < 0)
2271 case QEMU_OPTION_bt
:
2272 add_device_config(DEV_BT
, optarg
);
2274 case QEMU_OPTION_audio_help
:
2275 if (!(audio_available())) {
2276 printf("Option %s not supported for this target\n", popt
->name
);
2282 case QEMU_OPTION_soundhw
:
2283 if (!(audio_available())) {
2284 printf("Option %s not supported for this target\n", popt
->name
);
2287 select_soundhw (optarg
);
2292 case QEMU_OPTION_version
:
2296 case QEMU_OPTION_m
: {
2299 value
= strtosz(optarg
, NULL
);
2301 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2305 /* On 32-bit hosts, QEMU is limited by virtual address space */
2306 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2307 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2310 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2311 fprintf(stderr
, "qemu: ram size too large\n");
2317 case QEMU_OPTION_mempath
:
2321 case QEMU_OPTION_mem_prealloc
:
2326 set_cpu_log(optarg
);
2329 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2331 case QEMU_OPTION_gdb
:
2332 gdbstub_dev
= optarg
;
2337 case QEMU_OPTION_bios
:
2340 case QEMU_OPTION_singlestep
:
2347 keyboard_layout
= optarg
;
2349 case QEMU_OPTION_localtime
:
2352 case QEMU_OPTION_vga
:
2353 select_vgahw (optarg
);
2360 w
= strtol(p
, (char **)&p
, 10);
2363 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2369 h
= strtol(p
, (char **)&p
, 10);
2374 depth
= strtol(p
, (char **)&p
, 10);
2375 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2376 depth
!= 24 && depth
!= 32)
2378 } else if (*p
== '\0') {
2379 depth
= graphic_depth
;
2386 graphic_depth
= depth
;
2389 case QEMU_OPTION_echr
:
2392 term_escape_char
= strtol(optarg
, &r
, 0);
2394 printf("Bad argument to echr\n");
2397 case QEMU_OPTION_monitor
:
2398 monitor_parse(optarg
, "readline");
2399 default_monitor
= 0;
2401 case QEMU_OPTION_qmp
:
2402 monitor_parse(optarg
, "control");
2403 default_monitor
= 0;
2405 case QEMU_OPTION_mon
:
2406 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2410 default_monitor
= 0;
2412 case QEMU_OPTION_chardev
:
2413 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2418 case QEMU_OPTION_fsdev
:
2419 olist
= qemu_find_opts("fsdev");
2421 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2424 opts
= qemu_opts_parse(olist
, optarg
, 1);
2426 fprintf(stderr
, "parse error: %s\n", optarg
);
2430 case QEMU_OPTION_virtfs
: {
2431 char *arg_fsdev
= NULL
;
2432 char *arg_9p
= NULL
;
2435 olist
= qemu_find_opts("virtfs");
2437 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2440 opts
= qemu_opts_parse(olist
, optarg
, 1);
2442 fprintf(stderr
, "parse error: %s\n", optarg
);
2446 if (qemu_opt_get(opts
, "fstype") == NULL
||
2447 qemu_opt_get(opts
, "mount_tag") == NULL
||
2448 qemu_opt_get(opts
, "path") == NULL
||
2449 qemu_opt_get(opts
, "security_model") == NULL
) {
2450 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2451 "security_model=[mapped|passthrough|none],"
2456 len
= strlen(",id=,path=,security_model=");
2457 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2458 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2459 len
+= strlen(qemu_opt_get(opts
, "path"));
2460 len
+= strlen(qemu_opt_get(opts
, "security_model"));
2461 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2463 snprintf(arg_fsdev
, (len
+ 1) * sizeof(*arg_fsdev
),
2464 "%s,id=%s,path=%s,security_model=%s",
2465 qemu_opt_get(opts
, "fstype"),
2466 qemu_opt_get(opts
, "mount_tag"),
2467 qemu_opt_get(opts
, "path"),
2468 qemu_opt_get(opts
, "security_model"));
2470 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2471 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2472 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2474 snprintf(arg_9p
, (len
+ 1) * sizeof(*arg_9p
),
2475 "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2476 qemu_opt_get(opts
, "mount_tag"),
2477 qemu_opt_get(opts
, "mount_tag"));
2479 if (!qemu_opts_parse(qemu_find_opts("fsdev"), arg_fsdev
, 1)) {
2480 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2484 if (!qemu_opts_parse(qemu_find_opts("device"), arg_9p
, 1)) {
2485 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2489 qemu_free(arg_fsdev
);
2493 case QEMU_OPTION_serial
:
2494 add_device_config(DEV_SERIAL
, optarg
);
2496 if (strncmp(optarg
, "mon:", 4) == 0) {
2497 default_monitor
= 0;
2500 case QEMU_OPTION_watchdog
:
2503 "qemu: only one watchdog option may be given\n");
2508 case QEMU_OPTION_watchdog_action
:
2509 if (select_watchdog_action(optarg
) == -1) {
2510 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2514 case QEMU_OPTION_virtiocon
:
2515 add_device_config(DEV_VIRTCON
, optarg
);
2516 default_virtcon
= 0;
2517 if (strncmp(optarg
, "mon:", 4) == 0) {
2518 default_monitor
= 0;
2521 case QEMU_OPTION_parallel
:
2522 add_device_config(DEV_PARALLEL
, optarg
);
2523 default_parallel
= 0;
2524 if (strncmp(optarg
, "mon:", 4) == 0) {
2525 default_monitor
= 0;
2528 case QEMU_OPTION_debugcon
:
2529 add_device_config(DEV_DEBUGCON
, optarg
);
2531 case QEMU_OPTION_loadvm
:
2534 case QEMU_OPTION_full_screen
:
2538 case QEMU_OPTION_no_frame
:
2541 case QEMU_OPTION_alt_grab
:
2544 case QEMU_OPTION_ctrl_grab
:
2547 case QEMU_OPTION_no_quit
:
2550 case QEMU_OPTION_sdl
:
2551 display_type
= DT_SDL
;
2554 case QEMU_OPTION_pidfile
:
2557 case QEMU_OPTION_win2k_hack
:
2558 win2k_install_hack
= 1;
2560 case QEMU_OPTION_rtc_td_hack
:
2563 case QEMU_OPTION_acpitable
:
2564 do_acpitable_option(optarg
);
2566 case QEMU_OPTION_smbios
:
2567 do_smbios_option(optarg
);
2569 case QEMU_OPTION_enable_kvm
:
2572 case QEMU_OPTION_no_kvm
:
2574 #ifdef CONFIG_NO_CPU_EMULATION
2575 fprintf(stderr
, "cpu emulation not configured\n");
2580 case QEMU_OPTION_no_kvm_irqchip
: {
2585 case QEMU_OPTION_no_kvm_pit
: {
2589 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2590 kvm_pit_reinject
= 0;
2593 case QEMU_OPTION_enable_nesting
: {
2598 case QEMU_OPTION_usb
:
2601 case QEMU_OPTION_usbdevice
:
2603 add_device_config(DEV_USB
, optarg
);
2605 case QEMU_OPTION_device
:
2606 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2610 case QEMU_OPTION_smp
:
2613 fprintf(stderr
, "Invalid number of CPUs\n");
2616 if (max_cpus
< smp_cpus
) {
2617 fprintf(stderr
, "maxcpus must be equal to or greater than "
2621 if (max_cpus
> 255) {
2622 fprintf(stderr
, "Unsupported number of maxcpus\n");
2626 case QEMU_OPTION_vnc
:
2628 vnc_display
= optarg
;
2630 case QEMU_OPTION_no_acpi
:
2633 case QEMU_OPTION_no_hpet
:
2636 case QEMU_OPTION_balloon
:
2637 if (balloon_parse(optarg
) < 0) {
2638 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2642 case QEMU_OPTION_no_reboot
:
2645 case QEMU_OPTION_no_shutdown
:
2648 case QEMU_OPTION_show_cursor
:
2651 case QEMU_OPTION_uuid
:
2652 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2653 fprintf(stderr
, "Fail to parse UUID string."
2654 " Wrong format.\n");
2658 case QEMU_OPTION_option_rom
:
2659 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2660 fprintf(stderr
, "Too many option ROMs\n");
2663 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2664 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2665 option_rom
[nb_option_roms
].bootindex
=
2666 qemu_opt_get_number(opts
, "bootindex", -1);
2667 if (!option_rom
[nb_option_roms
].name
) {
2668 fprintf(stderr
, "Option ROM file is not specified\n");
2673 case QEMU_OPTION_semihosting
:
2674 semihosting_enabled
= 1;
2676 case QEMU_OPTION_tdf
:
2679 case QEMU_OPTION_kvm_shadow_memory
:
2680 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
2682 case QEMU_OPTION_name
:
2683 qemu_name
= qemu_strdup(optarg
);
2685 char *p
= strchr(qemu_name
, ',');
2688 if (strncmp(p
, "process=", 8)) {
2689 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2693 os_set_proc_name(p
);
2697 case QEMU_OPTION_prom_env
:
2698 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2699 fprintf(stderr
, "Too many prom variables\n");
2702 prom_envs
[nb_prom_envs
] = optarg
;
2705 case QEMU_OPTION_old_param
:
2708 case QEMU_OPTION_clock
:
2709 configure_alarms(optarg
);
2711 case QEMU_OPTION_startdate
:
2712 configure_rtc_date_offset(optarg
, 1);
2714 case QEMU_OPTION_rtc
:
2715 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2719 configure_rtc(opts
);
2721 case QEMU_OPTION_tb_size
:
2722 tb_size
= strtol(optarg
, NULL
, 0);
2726 case QEMU_OPTION_icount
:
2727 icount_option
= optarg
;
2729 case QEMU_OPTION_incoming
:
2731 incoming_expected
= true;
2733 case QEMU_OPTION_nodefaults
:
2735 default_parallel
= 0;
2736 default_virtcon
= 0;
2737 default_monitor
= 0;
2745 case QEMU_OPTION_nvram
:
2749 case QEMU_OPTION_xen_domid
:
2750 if (!(xen_available())) {
2751 printf("Option %s not supported for this target\n", popt
->name
);
2754 xen_domid
= atoi(optarg
);
2756 case QEMU_OPTION_xen_create
:
2757 if (!(xen_available())) {
2758 printf("Option %s not supported for this target\n", popt
->name
);
2761 xen_mode
= XEN_CREATE
;
2763 case QEMU_OPTION_xen_attach
:
2764 if (!(xen_available())) {
2765 printf("Option %s not supported for this target\n", popt
->name
);
2768 xen_mode
= XEN_ATTACH
;
2770 #ifdef CONFIG_SIMPLE_TRACE
2771 case QEMU_OPTION_trace
:
2772 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
2774 trace_file
= qemu_opt_get(opts
, "file");
2778 case QEMU_OPTION_readconfig
:
2780 int ret
= qemu_read_config_file(optarg
);
2782 fprintf(stderr
, "read config %s: %s\n", optarg
,
2788 case QEMU_OPTION_spice
:
2789 olist
= qemu_find_opts("spice");
2791 fprintf(stderr
, "spice is not supported by this qemu build.\n");
2794 opts
= qemu_opts_parse(olist
, optarg
, 0);
2796 fprintf(stderr
, "parse error: %s\n", optarg
);
2800 case QEMU_OPTION_writeconfig
:
2803 if (strcmp(optarg
, "-") == 0) {
2806 fp
= fopen(optarg
, "w");
2808 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2812 qemu_config_write(fp
);
2817 os_parse_cmd_args(popt
->index
, optarg
);
2823 /* If no data_dir is specified then try to find it relative to the
2826 data_dir
= os_find_datadir(argv
[0]);
2828 /* If all else fails use the install patch specified when building. */
2830 data_dir
= CONFIG_QEMU_DATADIR
;
2833 #ifdef CONFIG_SIMPLE_TRACE
2835 * Set the trace file name, if specified.
2837 st_set_trace_file(trace_file
);
2840 * Default to max_cpus = smp_cpus, in case the user doesn't
2841 * specify a max_cpus value.
2844 max_cpus
= smp_cpus
;
2846 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2847 if (smp_cpus
> machine
->max_cpus
) {
2848 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2849 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2854 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
2855 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
2857 if (machine
->no_serial
) {
2860 if (machine
->no_parallel
) {
2861 default_parallel
= 0;
2863 if (!machine
->use_virtcon
) {
2864 default_virtcon
= 0;
2866 if (machine
->no_vga
) {
2869 if (machine
->no_floppy
) {
2872 if (machine
->no_cdrom
) {
2875 if (machine
->no_sdcard
) {
2879 if (display_type
== DT_NOGRAPHIC
) {
2880 if (default_parallel
)
2881 add_device_config(DEV_PARALLEL
, "null");
2882 if (default_serial
&& default_monitor
) {
2883 add_device_config(DEV_SERIAL
, "mon:stdio");
2884 } else if (default_virtcon
&& default_monitor
) {
2885 add_device_config(DEV_VIRTCON
, "mon:stdio");
2888 add_device_config(DEV_SERIAL
, "stdio");
2889 if (default_virtcon
)
2890 add_device_config(DEV_VIRTCON
, "stdio");
2891 if (default_monitor
)
2892 monitor_parse("stdio", "readline");
2896 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2897 if (default_parallel
)
2898 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2899 if (default_monitor
)
2900 monitor_parse("vc:80Cx24C", "readline");
2901 if (default_virtcon
)
2902 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2905 vga_interface_type
= VGA_CIRRUS
;
2909 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
2911 #ifdef CONFIG_VIRTFS
2912 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
2919 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
2925 int ret
= kvm_init();
2927 if (kvm_allowed
> 0) {
2928 if (!kvm_available()) {
2929 printf("KVM not supported for this target\n");
2931 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
2935 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
2937 kvm_allowed
= ret
>= 0;
2940 if (qemu_init_main_loop()) {
2941 fprintf(stderr
, "qemu_init_main_loop failed\n");
2944 linux_boot
= (kernel_filename
!= NULL
);
2946 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
2947 fprintf(stderr
, "-append only allowed with -kernel option\n");
2951 if (!linux_boot
&& initrd_filename
!= NULL
) {
2952 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
2956 os_set_line_buffering();
2958 if (init_timer_alarm() < 0) {
2959 fprintf(stderr
, "could not initialize alarm timer\n");
2962 configure_icount(icount_option
);
2964 if (net_init_clients() < 0) {
2968 /* init the bluetooth world */
2969 if (foreach_device_config(DEV_BT
, bt_parse
))
2972 /* init the memory */
2974 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2976 /* init the dynamic translator */
2977 cpu_exec_init_all(tb_size
* 1024 * 1024);
2979 bdrv_init_with_whitelist();
2983 /* open the virtual block devices */
2985 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
2986 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
2989 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
2990 IF_DEFAULT
, 2, CDROM_OPTS
);
2991 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
2992 IF_FLOPPY
, 0, FD_OPTS
);
2993 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
2996 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
2999 if (nb_numa_nodes
> 0) {
3002 if (nb_numa_nodes
> smp_cpus
) {
3003 nb_numa_nodes
= smp_cpus
;
3006 /* If no memory size if given for any node, assume the default case
3007 * and distribute the available memory equally across all nodes
3009 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3010 if (node_mem
[i
] != 0)
3013 if (i
== nb_numa_nodes
) {
3014 uint64_t usedmem
= 0;
3016 /* On Linux, the each node's border has to be 8MB aligned,
3017 * the final node gets the rest.
3019 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3020 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3021 usedmem
+= node_mem
[i
];
3023 node_mem
[i
] = ram_size
- usedmem
;
3026 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3027 if (node_cpumask
[i
] != 0)
3030 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3031 * must cope with this anyway, because there are BIOSes out there in
3032 * real machines which also use this scheme.
3034 if (i
== nb_numa_nodes
) {
3035 for (i
= 0; i
< smp_cpus
; i
++) {
3036 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3041 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3045 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3047 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3049 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3051 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3054 module_call_init(MODULE_INIT_DEVICE
);
3056 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3060 i
= select_watchdog(watchdog
);
3062 exit (i
== 1 ? 1 : 0);
3065 if (machine
->compat_props
) {
3066 qdev_prop_register_global_list(machine
->compat_props
);
3070 machine
->init(ram_size
, boot_devices
,
3071 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3073 cpu_synchronize_all_post_init();
3075 /* must be after terminal init, SDL library changes signal handlers */
3076 os_setup_signal_handling();
3080 current_machine
= machine
;
3082 /* init USB devices */
3084 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3088 /* init generic devices */
3089 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3092 net_check_clients();
3094 /* just use the first displaystate for the moment */
3095 ds
= get_displaystate();
3099 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3100 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3101 display_type
= DT_SDL
;
3103 vnc_display
= "localhost:0,to=99";
3109 /* init local displays */
3110 switch (display_type
) {
3113 #if defined(CONFIG_CURSES)
3115 curses_display_init(ds
, full_screen
);
3118 #if defined(CONFIG_SDL)
3120 sdl_display_init(ds
, full_screen
, no_frame
);
3122 #elif defined(CONFIG_COCOA)
3124 cocoa_display_init(ds
, full_screen
);
3131 /* init remote displays */
3133 vnc_display_init(ds
);
3134 if (vnc_display_open(ds
, vnc_display
) < 0)
3137 if (show_vnc_port
) {
3138 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3142 if (using_spice
&& !qxl_enabled
) {
3143 qemu_spice_display_init(ds
);
3149 dcl
= ds
->listeners
;
3150 while (dcl
!= NULL
) {
3151 if (dcl
->dpy_refresh
!= NULL
) {
3152 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3153 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3158 if (ds
->gui_timer
== NULL
) {
3159 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3160 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3162 text_consoles_set_display(ds
);
3164 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3165 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3170 qdev_machine_creation_done();
3172 if (rom_load_all() != 0) {
3173 fprintf(stderr
, "rom loading failed\n");
3177 /* TODO: once all bus devices are qdevified, this should be done
3178 * when bus is created by qdev.c */
3179 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3180 qemu_run_machine_init_done_notifiers();
3182 qemu_system_reset();
3184 if (load_vmstate(loadvm
) < 0) {
3190 int ret
= qemu_start_incoming_migration(incoming
);
3192 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3196 } else if (autostart
) {