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>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <sys/sysctl.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81 extern int madvise(caddr_t
, size_t, int);
86 #if defined(__OpenBSD__)
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
99 #if defined(__APPLE__) || defined(main)
101 int qemu_main(int argc
, char **argv
, char **envp
);
102 int main(int argc
, char **argv
)
104 return qemu_main(argc
, argv
, NULL
);
107 #define main qemu_main
109 #endif /* CONFIG_SDL */
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "block_int.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "hw/device-assignment.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
161 #include "qemu-queue.h"
163 #include "arch_init.h"
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir
;
173 const char *bios_name
= NULL
;
174 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 to store the VM snapshots */
176 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
177 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
178 DriveInfo
*extboot_drive
= NULL
;
179 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
180 DisplayType display_type
= DT_DEFAULT
;
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 static int rtc_utc
= 1;
192 static int rtc_date_offset
= -1; /* -1 means no change */
193 QEMUClock
*rtc_clock
;
194 int vga_interface_type
= VGA_NONE
;
195 static int full_screen
= 0;
197 static int no_frame
= 0;
200 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
201 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
202 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
203 int win2k_install_hack
= 0;
207 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
208 int assigned_devices_index
;
213 const char *vnc_display
;
214 int acpi_enabled
= 1;
222 int graphic_rotate
= 0;
223 uint8_t irq0override
= 1;
224 const char *watchdog
;
225 const char *option_rom
[MAX_OPTION_ROMS
];
227 int semihosting_enabled
= 0;
228 int time_drift_fix
= 0;
229 unsigned int kvm_shadow_memory
= 0;
231 const char *qemu_name
;
234 unsigned int nb_prom_envs
= 0;
235 const char *prom_envs
[MAX_PROM_ENVS
];
236 const char *nvram
= NULL
;
240 uint64_t node_mem
[MAX_NODES
];
241 uint64_t node_cpumask
[MAX_NODES
];
243 static QEMUTimer
*nographic_timer
;
245 uint8_t qemu_uuid
[16];
247 static QEMUBootSetHandler
*boot_set_handler
;
248 static void *boot_set_opaque
;
252 enum xen_mode xen_mode
= XEN_EMULATE
;
254 static int default_serial
= 1;
255 static int default_parallel
= 1;
256 static int default_virtcon
= 1;
257 static int default_monitor
= 1;
258 static int default_vga
= 1;
259 static int default_floppy
= 1;
260 static int default_cdrom
= 1;
261 static int default_sdcard
= 1;
267 { .driver
= "isa-serial", .flag
= &default_serial
},
268 { .driver
= "isa-parallel", .flag
= &default_parallel
},
269 { .driver
= "isa-fdc", .flag
= &default_floppy
},
270 { .driver
= "ide-drive", .flag
= &default_cdrom
},
271 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
272 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
273 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
274 { .driver
= "VGA", .flag
= &default_vga
},
275 { .driver
= "cirrus-vga", .flag
= &default_vga
},
276 { .driver
= "vmware-svga", .flag
= &default_vga
},
279 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
281 const char *driver
= qemu_opt_get(opts
, "driver");
286 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
287 if (strcmp(default_list
[i
].driver
, driver
) != 0)
289 *(default_list
[i
].flag
) = 0;
294 /***********************************************************/
295 /* real time host monotonic timer */
297 /* compute with 96 bit intermediate result: (a*b)/c */
298 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
303 #ifdef HOST_WORDS_BIGENDIAN
313 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
314 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
317 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
321 /***********************************************************/
322 /* host time/date access */
323 void qemu_get_timedate(struct tm
*tm
, int offset
)
330 if (rtc_date_offset
== -1) {
334 ret
= localtime(&ti
);
336 ti
-= rtc_date_offset
;
340 memcpy(tm
, ret
, sizeof(struct tm
));
343 int qemu_timedate_diff(struct tm
*tm
)
347 if (rtc_date_offset
== -1)
349 seconds
= mktimegm(tm
);
351 seconds
= mktime(tm
);
353 seconds
= mktimegm(tm
) + rtc_date_offset
;
355 return seconds
- time(NULL
);
358 void rtc_change_mon_event(struct tm
*tm
)
362 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
363 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
364 qobject_decref(data
);
367 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
369 time_t rtc_start_date
;
372 if (!strcmp(startdate
, "now") && legacy
) {
373 rtc_date_offset
= -1;
375 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
383 } else if (sscanf(startdate
, "%d-%d-%d",
395 rtc_start_date
= mktimegm(&tm
);
396 if (rtc_start_date
== -1) {
398 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
399 "'2006-06-17T16:01:21' or '2006-06-17'\n");
402 rtc_date_offset
= time(NULL
) - rtc_start_date
;
406 static void configure_rtc(QemuOpts
*opts
)
410 value
= qemu_opt_get(opts
, "base");
412 if (!strcmp(value
, "utc")) {
414 } else if (!strcmp(value
, "localtime")) {
417 configure_rtc_date_offset(value
, 0);
420 value
= qemu_opt_get(opts
, "clock");
422 if (!strcmp(value
, "host")) {
423 rtc_clock
= host_clock
;
424 } else if (!strcmp(value
, "vm")) {
425 rtc_clock
= vm_clock
;
427 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
431 value
= qemu_opt_get(opts
, "driftfix");
433 if (!strcmp(value
, "slew")) {
435 } else if (!strcmp(value
, "none")) {
438 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
444 /***********************************************************/
445 /* Bluetooth support */
448 static struct HCIInfo
*hci_table
[MAX_NICS
];
450 static struct bt_vlan_s
{
451 struct bt_scatternet_s net
;
453 struct bt_vlan_s
*next
;
456 /* find or alloc a new bluetooth "VLAN" */
457 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
459 struct bt_vlan_s
**pvlan
, *vlan
;
460 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
464 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
466 pvlan
= &first_bt_vlan
;
467 while (*pvlan
!= NULL
)
468 pvlan
= &(*pvlan
)->next
;
473 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
477 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
482 static struct HCIInfo null_hci
= {
483 .cmd_send
= null_hci_send
,
484 .sco_send
= null_hci_send
,
485 .acl_send
= null_hci_send
,
486 .bdaddr_set
= null_hci_addr_set
,
489 struct HCIInfo
*qemu_next_hci(void)
491 if (cur_hci
== nb_hcis
)
494 return hci_table
[cur_hci
++];
497 static struct HCIInfo
*hci_init(const char *str
)
500 struct bt_scatternet_s
*vlan
= 0;
502 if (!strcmp(str
, "null"))
505 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
507 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
508 else if (!strncmp(str
, "hci", 3)) {
511 if (!strncmp(str
+ 3, ",vlan=", 6)) {
512 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
517 vlan
= qemu_find_bt_vlan(0);
519 return bt_new_hci(vlan
);
522 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
527 static int bt_hci_parse(const char *str
)
532 if (nb_hcis
>= MAX_NICS
) {
533 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
546 bdaddr
.b
[5] = 0x56 + nb_hcis
;
547 hci
->bdaddr_set(hci
, bdaddr
.b
);
549 hci_table
[nb_hcis
++] = hci
;
554 static void bt_vhci_add(int vlan_id
)
556 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
559 fprintf(stderr
, "qemu: warning: adding a VHCI to "
560 "an empty scatternet %i\n", vlan_id
);
562 bt_vhci_init(bt_new_hci(vlan
));
565 static struct bt_device_s
*bt_device_add(const char *opt
)
567 struct bt_scatternet_s
*vlan
;
569 char *endp
= strstr(opt
, ",vlan=");
570 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
573 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
576 vlan_id
= strtol(endp
+ 6, &endp
, 0);
578 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
583 vlan
= qemu_find_bt_vlan(vlan_id
);
586 fprintf(stderr
, "qemu: warning: adding a slave device to "
587 "an empty scatternet %i\n", vlan_id
);
589 if (!strcmp(devname
, "keyboard"))
590 return bt_keyboard_init(vlan
);
592 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
596 static int bt_parse(const char *opt
)
598 const char *endp
, *p
;
601 if (strstart(opt
, "hci", &endp
)) {
602 if (!*endp
|| *endp
== ',') {
604 if (!strstart(endp
, ",vlan=", 0))
607 return bt_hci_parse(opt
);
609 } else if (strstart(opt
, "vhci", &endp
)) {
610 if (!*endp
|| *endp
== ',') {
612 if (strstart(endp
, ",vlan=", &p
)) {
613 vlan
= strtol(p
, (char **) &endp
, 0);
615 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
619 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
628 } else if (strstart(opt
, "device:", &endp
))
629 return !bt_device_add(endp
);
631 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
635 /***********************************************************/
636 /* QEMU Block devices */
638 #define HD_ALIAS "index=%d,media=disk"
639 #define CDROM_ALIAS "index=2,media=cdrom"
640 #define FD_ALIAS "index=%d,if=floppy"
641 #define PFLASH_ALIAS "if=pflash"
642 #define MTD_ALIAS "if=mtd"
643 #define SD_ALIAS "index=0,if=sd"
645 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
652 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
655 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
660 qemu_opt_set(opts
, "file", file
);
664 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
668 /* seek interface, bus and unit */
670 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
671 if (dinfo
->type
== type
&&
680 DriveInfo
*drive_get_by_id(const char *id
)
684 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
685 if (strcmp(id
, dinfo
->id
))
692 int drive_get_max_bus(BlockInterfaceType type
)
698 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
699 if(dinfo
->type
== type
&&
700 dinfo
->bus
> max_bus
)
701 max_bus
= dinfo
->bus
;
706 const char *drive_get_serial(BlockDriverState
*bdrv
)
710 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
711 if (dinfo
->bdrv
== bdrv
)
712 return dinfo
->serial
;
718 BlockInterfaceErrorAction
drive_get_on_error(
719 BlockDriverState
*bdrv
, int is_read
)
723 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
724 if (dinfo
->bdrv
== bdrv
)
725 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
728 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
731 static void bdrv_format_print(void *opaque
, const char *name
)
733 fprintf(stderr
, " %s", name
);
736 void drive_uninit(DriveInfo
*dinfo
)
738 qemu_opts_del(dinfo
->opts
);
739 bdrv_delete(dinfo
->bdrv
);
740 QTAILQ_REMOVE(&drives
, dinfo
, next
);
744 static int parse_block_error_action(const char *buf
, int is_read
)
746 if (!strcmp(buf
, "ignore")) {
747 return BLOCK_ERR_IGNORE
;
748 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
749 return BLOCK_ERR_STOP_ENOSPC
;
750 } else if (!strcmp(buf
, "stop")) {
751 return BLOCK_ERR_STOP_ANY
;
752 } else if (!strcmp(buf
, "report")) {
753 return BLOCK_ERR_REPORT
;
755 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
756 buf
, is_read
? "read" : "write");
761 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
765 const char *file
= NULL
;
768 const char *mediastr
= "";
769 BlockInterfaceType type
;
770 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
772 int cyls
, heads
, secs
, translation
;
773 BlockDriver
*drv
= NULL
;
774 QEMUMachine
*machine
= opaque
;
779 int on_read_error
, on_write_error
;
787 translation
= BIOS_ATA_TRANSLATION_AUTO
;
789 if (machine
&& machine
->use_scsi
) {
791 max_devs
= MAX_SCSI_DEVS
;
792 pstrcpy(devname
, sizeof(devname
), "scsi");
795 max_devs
= MAX_IDE_DEVS
;
796 pstrcpy(devname
, sizeof(devname
), "ide");
800 /* extract parameters */
801 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
802 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
803 index
= qemu_opt_get_number(opts
, "index", -1);
805 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
806 heads
= qemu_opt_get_number(opts
, "heads", 0);
807 secs
= qemu_opt_get_number(opts
, "secs", 0);
809 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
810 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
812 file
= qemu_opt_get(opts
, "file");
813 serial
= qemu_opt_get(opts
, "serial");
815 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
816 pstrcpy(devname
, sizeof(devname
), buf
);
817 if (!strcmp(buf
, "ide")) {
819 max_devs
= MAX_IDE_DEVS
;
820 } else if (!strcmp(buf
, "scsi")) {
822 max_devs
= MAX_SCSI_DEVS
;
823 } else if (!strcmp(buf
, "floppy")) {
826 } else if (!strcmp(buf
, "pflash")) {
829 } else if (!strcmp(buf
, "mtd")) {
832 } else if (!strcmp(buf
, "sd")) {
835 } else if (!strcmp(buf
, "virtio")) {
838 } else if (!strcmp(buf
, "xen")) {
841 } else if (!strcmp(buf
, "none")) {
845 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
850 if (cyls
|| heads
|| secs
) {
851 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
852 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
855 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
856 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
859 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
860 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
865 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
868 "qemu: '%s' trans must be used with cyls,heads and secs\n",
872 if (!strcmp(buf
, "none"))
873 translation
= BIOS_ATA_TRANSLATION_NONE
;
874 else if (!strcmp(buf
, "lba"))
875 translation
= BIOS_ATA_TRANSLATION_LBA
;
876 else if (!strcmp(buf
, "auto"))
877 translation
= BIOS_ATA_TRANSLATION_AUTO
;
879 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
884 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
885 if (!strcmp(buf
, "disk")) {
887 } else if (!strcmp(buf
, "cdrom")) {
888 if (cyls
|| secs
|| heads
) {
890 "qemu: '%s' invalid physical CHS format\n", buf
);
895 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
900 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
901 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
902 bdrv_flags
|= BDRV_O_NOCACHE
;
903 } else if (!strcmp(buf
, "writeback")) {
904 bdrv_flags
|= BDRV_O_CACHE_WB
;
905 } else if (!strcmp(buf
, "unsafe")) {
906 bdrv_flags
|= BDRV_O_CACHE_WB
;
907 bdrv_flags
|= BDRV_O_NO_FLUSH
;
908 } else if (!strcmp(buf
, "writethrough")) {
909 /* this is the default */
911 fprintf(stderr
, "qemu: invalid cache option\n");
916 #ifdef CONFIG_LINUX_AIO
917 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
918 if (!strcmp(buf
, "native")) {
919 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
920 } else if (!strcmp(buf
, "threads")) {
921 /* this is the default */
923 fprintf(stderr
, "qemu: invalid aio option\n");
929 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
930 if (strcmp(buf
, "?") == 0) {
931 fprintf(stderr
, "qemu: Supported formats:");
932 bdrv_iterate_format(bdrv_format_print
, NULL
);
933 fprintf(stderr
, "\n");
936 drv
= bdrv_find_whitelisted_format(buf
);
938 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
943 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
944 if (is_extboot
&& extboot_drive
) {
945 fprintf(stderr
, "qemu: two bootable drives specified\n");
949 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
950 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
951 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
952 fprintf(stderr
, "werror is no supported by this format\n");
956 on_write_error
= parse_block_error_action(buf
, 0);
957 if (on_write_error
< 0) {
962 on_read_error
= BLOCK_ERR_REPORT
;
963 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
964 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
965 fprintf(stderr
, "rerror is no supported by this format\n");
969 on_read_error
= parse_block_error_action(buf
, 1);
970 if (on_read_error
< 0) {
975 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
976 if (type
!= IF_VIRTIO
) {
977 fprintf(stderr
, "addr is not supported\n");
982 /* compute bus and unit according index */
985 if (bus_id
!= 0 || unit_id
!= -1) {
987 "qemu: index cannot be used with bus and unit\n");
995 unit_id
= index
% max_devs
;
996 bus_id
= index
/ max_devs
;
1000 /* if user doesn't specify a unit_id,
1001 * try to find the first free
1004 if (unit_id
== -1) {
1006 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1008 if (max_devs
&& unit_id
>= max_devs
) {
1009 unit_id
-= max_devs
;
1017 if (max_devs
&& unit_id
>= max_devs
) {
1018 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1019 unit_id
, max_devs
- 1);
1024 * ignore multiple definitions
1027 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1034 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1035 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1036 dinfo
->id
= qemu_strdup(buf
);
1038 /* no id supplied -> create one */
1039 dinfo
->id
= qemu_mallocz(32);
1040 if (type
== IF_IDE
|| type
== IF_SCSI
)
1041 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1043 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1044 devname
, bus_id
, mediastr
, unit_id
);
1046 snprintf(dinfo
->id
, 32, "%s%s%i",
1047 devname
, mediastr
, unit_id
);
1049 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1050 dinfo
->devaddr
= devaddr
;
1052 dinfo
->bus
= bus_id
;
1053 dinfo
->unit
= unit_id
;
1054 dinfo
->on_read_error
= on_read_error
;
1055 dinfo
->on_write_error
= on_write_error
;
1058 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1059 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1061 extboot_drive
= dinfo
;
1072 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1073 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1077 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1082 /* FIXME: This isn't really a floppy, but it's a reasonable
1085 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1091 /* add virtio block device */
1092 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1093 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1094 qemu_opt_set(opts
, "drive", dinfo
->id
);
1096 qemu_opt_set(opts
, "addr", devaddr
);
1106 /* always use cache=unsafe with snapshot */
1107 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
1108 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
1111 if (media
== MEDIA_CDROM
) {
1112 /* CDROM is fine for any interface, don't check. */
1114 } else if (ro
== 1) {
1115 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
1116 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1121 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1123 if (bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1124 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1125 file
, strerror(errno
));
1129 if (bdrv_key_required(dinfo
->bdrv
))
1135 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1137 QEMUMachine
*machine
= opaque
;
1138 int fatal_error
= 0;
1140 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1147 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1149 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1150 qemu_opt_set(opts
, "snapshot", "on");
1155 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1157 boot_set_handler
= func
;
1158 boot_set_opaque
= opaque
;
1161 int qemu_boot_set(const char *boot_devices
)
1163 if (!boot_set_handler
) {
1166 return boot_set_handler(boot_set_opaque
, boot_devices
);
1169 static void validate_bootdevices(char *devices
)
1171 /* We just do some generic consistency checks */
1175 for (p
= devices
; *p
!= '\0'; p
++) {
1176 /* Allowed boot devices are:
1177 * a-b: floppy disk drives
1178 * c-f: IDE disk drives
1179 * g-m: machine implementation dependant drives
1180 * n-p: network devices
1181 * It's up to each machine implementation to check if the given boot
1182 * devices match the actual hardware implementation and firmware
1185 if (*p
< 'a' || *p
> 'p') {
1186 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1189 if (bitmap
& (1 << (*p
- 'a'))) {
1190 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1193 bitmap
|= 1 << (*p
- 'a');
1197 static void restore_boot_devices(void *opaque
)
1199 char *standard_boot_devices
= opaque
;
1200 static int first
= 1;
1202 /* Restore boot order and remove ourselves after the first boot */
1208 qemu_boot_set(standard_boot_devices
);
1210 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1211 qemu_free(standard_boot_devices
);
1214 static void numa_add(const char *optarg
)
1218 unsigned long long value
, endvalue
;
1221 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1222 if (!strcmp(option
, "node")) {
1223 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1224 nodenr
= nb_numa_nodes
;
1226 nodenr
= strtoull(option
, NULL
, 10);
1229 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1230 node_mem
[nodenr
] = 0;
1232 value
= strtoull(option
, &endptr
, 0);
1234 case 0: case 'M': case 'm':
1241 node_mem
[nodenr
] = value
;
1243 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1244 node_cpumask
[nodenr
] = 0;
1246 value
= strtoull(option
, &endptr
, 10);
1249 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1251 if (*endptr
== '-') {
1252 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1253 if (endvalue
>= 63) {
1256 "only 63 CPUs in NUMA mode supported.\n");
1258 value
= (2ULL << endvalue
) - (1ULL << value
);
1260 value
= 1ULL << value
;
1263 node_cpumask
[nodenr
] = value
;
1270 static void smp_parse(const char *optarg
)
1272 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1276 smp
= strtoul(optarg
, &endptr
, 10);
1277 if (endptr
!= optarg
) {
1278 if (*endptr
== ',') {
1282 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1283 sockets
= strtoull(option
, NULL
, 10);
1284 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1285 cores
= strtoull(option
, NULL
, 10);
1286 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1287 threads
= strtoull(option
, NULL
, 10);
1288 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1289 max_cpus
= strtoull(option
, NULL
, 10);
1291 /* compute missing values, prefer sockets over cores over threads */
1292 if (smp
== 0 || sockets
== 0) {
1293 sockets
= sockets
> 0 ? sockets
: 1;
1294 cores
= cores
> 0 ? cores
: 1;
1295 threads
= threads
> 0 ? threads
: 1;
1297 smp
= cores
* threads
* sockets
;
1301 threads
= threads
> 0 ? threads
: 1;
1302 cores
= smp
/ (sockets
* threads
);
1305 threads
= smp
/ (cores
* sockets
);
1310 smp_cores
= cores
> 0 ? cores
: 1;
1311 smp_threads
= threads
> 0 ? threads
: 1;
1313 max_cpus
= smp_cpus
;
1316 /***********************************************************/
1319 static int usb_device_add(const char *devname
, int is_hotplug
)
1322 USBDevice
*dev
= NULL
;
1327 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1328 dev
= usbdevice_create(devname
);
1332 /* the other ones */
1333 if (strstart(devname
, "host:", &p
)) {
1334 dev
= usb_host_device_open(p
);
1335 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1336 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1337 bt_new_hci(qemu_find_bt_vlan(0)));
1348 static int usb_device_del(const char *devname
)
1353 if (strstart(devname
, "host:", &p
))
1354 return usb_host_device_close(p
);
1359 p
= strchr(devname
, '.');
1362 bus_num
= strtoul(devname
, NULL
, 0);
1363 addr
= strtoul(p
+ 1, NULL
, 0);
1365 return usb_device_delete_addr(bus_num
, addr
);
1368 static int usb_parse(const char *cmdline
)
1371 r
= usb_device_add(cmdline
, 0);
1373 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1378 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1380 const char *devname
= qdict_get_str(qdict
, "devname");
1381 if (usb_device_add(devname
, 1) < 0) {
1382 error_report("could not add USB device '%s'", devname
);
1386 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1388 const char *devname
= qdict_get_str(qdict
, "devname");
1389 if (usb_device_del(devname
) < 0) {
1390 error_report("could not delete USB device '%s'", devname
);
1394 /***********************************************************/
1395 /* PCMCIA/Cardbus */
1397 static struct pcmcia_socket_entry_s
{
1398 PCMCIASocket
*socket
;
1399 struct pcmcia_socket_entry_s
*next
;
1400 } *pcmcia_sockets
= 0;
1402 void pcmcia_socket_register(PCMCIASocket
*socket
)
1404 struct pcmcia_socket_entry_s
*entry
;
1406 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1407 entry
->socket
= socket
;
1408 entry
->next
= pcmcia_sockets
;
1409 pcmcia_sockets
= entry
;
1412 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1414 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1416 ptr
= &pcmcia_sockets
;
1417 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1418 if (entry
->socket
== socket
) {
1424 void pcmcia_info(Monitor
*mon
)
1426 struct pcmcia_socket_entry_s
*iter
;
1428 if (!pcmcia_sockets
)
1429 monitor_printf(mon
, "No PCMCIA sockets\n");
1431 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1432 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1433 iter
->socket
->attached
? iter
->socket
->card_string
:
1437 /***********************************************************/
1440 typedef struct IOHandlerRecord
{
1442 IOCanReadHandler
*fd_read_poll
;
1444 IOHandler
*fd_write
;
1447 /* temporary data */
1449 QLIST_ENTRY(IOHandlerRecord
) next
;
1452 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1453 QLIST_HEAD_INITIALIZER(io_handlers
);
1456 /* XXX: fd_read_poll should be suppressed, but an API change is
1457 necessary in the character devices to suppress fd_can_read(). */
1458 int qemu_set_fd_handler2(int fd
,
1459 IOCanReadHandler
*fd_read_poll
,
1461 IOHandler
*fd_write
,
1464 IOHandlerRecord
*ioh
;
1466 if (!fd_read
&& !fd_write
) {
1467 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1468 if (ioh
->fd
== fd
) {
1474 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1478 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1479 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1482 ioh
->fd_read_poll
= fd_read_poll
;
1483 ioh
->fd_read
= fd_read
;
1484 ioh
->fd_write
= fd_write
;
1485 ioh
->opaque
= opaque
;
1488 qemu_notify_event();
1492 int qemu_set_fd_handler(int fd
,
1494 IOHandler
*fd_write
,
1497 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1500 /***********************************************************/
1501 /* machine registration */
1503 static QEMUMachine
*first_machine
= NULL
;
1504 QEMUMachine
*current_machine
= NULL
;
1506 int qemu_register_machine(QEMUMachine
*m
)
1509 pm
= &first_machine
;
1517 static QEMUMachine
*find_machine(const char *name
)
1521 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1522 if (!strcmp(m
->name
, name
))
1524 if (m
->alias
&& !strcmp(m
->alias
, name
))
1530 static QEMUMachine
*find_default_machine(void)
1534 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1535 if (m
->is_default
) {
1542 /***********************************************************/
1543 /* main execution loop */
1545 static void gui_update(void *opaque
)
1547 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1548 DisplayState
*ds
= opaque
;
1549 DisplayChangeListener
*dcl
= ds
->listeners
;
1551 qemu_flush_coalesced_mmio_buffer();
1554 while (dcl
!= NULL
) {
1555 if (dcl
->gui_timer_interval
&&
1556 dcl
->gui_timer_interval
< interval
)
1557 interval
= dcl
->gui_timer_interval
;
1560 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1563 static void nographic_update(void *opaque
)
1565 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1567 qemu_flush_coalesced_mmio_buffer();
1568 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1571 struct vm_change_state_entry
{
1572 VMChangeStateHandler
*cb
;
1574 QLIST_ENTRY (vm_change_state_entry
) entries
;
1577 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1579 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1582 VMChangeStateEntry
*e
;
1584 e
= qemu_mallocz(sizeof (*e
));
1588 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1592 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1594 QLIST_REMOVE (e
, entries
);
1598 void vm_state_notify(int running
, int reason
)
1600 VMChangeStateEntry
*e
;
1602 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1603 e
->cb(e
->opaque
, running
, reason
);
1612 vm_state_notify(1, 0);
1614 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1618 /* reset/shutdown handler */
1620 typedef struct QEMUResetEntry
{
1621 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1622 QEMUResetHandler
*func
;
1626 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1627 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1628 static int reset_requested
;
1629 static int shutdown_requested
;
1630 static int powerdown_requested
;
1631 int debug_requested
;
1632 int vmstop_requested
;
1634 int qemu_no_shutdown(void)
1636 int r
= no_shutdown
;
1641 int qemu_shutdown_requested(void)
1643 int r
= shutdown_requested
;
1644 shutdown_requested
= 0;
1648 int qemu_reset_requested(void)
1650 int r
= reset_requested
;
1651 reset_requested
= 0;
1655 int qemu_powerdown_requested(void)
1657 int r
= powerdown_requested
;
1658 powerdown_requested
= 0;
1662 static int qemu_debug_requested(void)
1664 int r
= debug_requested
;
1665 debug_requested
= 0;
1669 static int qemu_vmstop_requested(void)
1671 int r
= vmstop_requested
;
1672 vmstop_requested
= 0;
1676 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1678 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1681 re
->opaque
= opaque
;
1682 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1685 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1689 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1690 if (re
->func
== func
&& re
->opaque
== opaque
) {
1691 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1698 void qemu_system_reset(void)
1700 QEMUResetEntry
*re
, *nre
;
1702 /* reset all devices */
1703 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1704 re
->func(re
->opaque
);
1706 monitor_protocol_event(QEVENT_RESET
, NULL
);
1707 cpu_synchronize_all_post_reset();
1710 void qemu_system_reset_request(void)
1713 shutdown_requested
= 1;
1715 reset_requested
= 1;
1717 if (cpu_single_env
) {
1718 cpu_single_env
->stopped
= 1;
1720 qemu_notify_event();
1723 void qemu_system_shutdown_request(void)
1725 shutdown_requested
= 1;
1726 qemu_notify_event();
1729 void qemu_system_powerdown_request(void)
1731 powerdown_requested
= 1;
1732 qemu_notify_event();
1735 void main_loop_wait(int nonblocking
)
1737 IOHandlerRecord
*ioh
;
1738 fd_set rfds
, wfds
, xfds
;
1746 timeout
= qemu_calculate_timeout();
1747 qemu_bh_update_timeout(&timeout
);
1750 os_host_main_loop_wait(&timeout
);
1752 /* poll any events */
1753 /* XXX: separate device handlers from system ones */
1758 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1762 (!ioh
->fd_read_poll
||
1763 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1764 FD_SET(ioh
->fd
, &rfds
);
1768 if (ioh
->fd_write
) {
1769 FD_SET(ioh
->fd
, &wfds
);
1775 tv
.tv_sec
= timeout
/ 1000;
1776 tv
.tv_usec
= (timeout
% 1000) * 1000;
1778 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1780 qemu_mutex_unlock_iothread();
1781 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1782 qemu_mutex_lock_iothread();
1784 IOHandlerRecord
*pioh
;
1786 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1788 QLIST_REMOVE(ioh
, next
);
1792 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1793 ioh
->fd_read(ioh
->opaque
);
1794 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
1795 FD_CLR(ioh
->fd
, &rfds
);
1797 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1798 ioh
->fd_write(ioh
->opaque
);
1803 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1805 qemu_run_all_timers();
1807 /* Check bottom-halves last in case any of the earlier events triggered
1813 static int vm_can_run(void)
1815 if (powerdown_requested
)
1817 if (reset_requested
)
1819 if (shutdown_requested
)
1821 if (debug_requested
)
1826 qemu_irq qemu_system_powerdown
;
1828 static void main_loop(void)
1832 if (kvm_enabled()) {
1834 cpu_disable_ticks();
1838 qemu_main_loop_start();
1842 bool nonblocking
= false;
1843 #ifdef CONFIG_PROFILER
1846 #ifndef CONFIG_IOTHREAD
1847 nonblocking
= tcg_cpu_exec();
1849 #ifdef CONFIG_PROFILER
1850 ti
= profile_getclock();
1852 main_loop_wait(nonblocking
);
1853 #ifdef CONFIG_PROFILER
1854 dev_time
+= profile_getclock() - ti
;
1856 } while (vm_can_run());
1858 if ((r
= qemu_debug_requested())) {
1861 if (qemu_shutdown_requested()) {
1862 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1869 if (qemu_reset_requested()) {
1871 qemu_system_reset();
1874 if (qemu_powerdown_requested()) {
1875 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1876 qemu_irq_raise(qemu_system_powerdown
);
1878 if ((r
= qemu_vmstop_requested())) {
1885 static void version(void)
1887 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1890 static void help(int exitcode
)
1892 const char *options_help
=
1893 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1895 #define DEFHEADING(text) stringify(text) "\n"
1896 #include "qemu-options.def"
1902 printf("usage: %s [options] [disk_image]\n"
1904 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1907 "During emulation, the following keys are useful:\n"
1908 "ctrl-alt-f toggle full screen\n"
1909 "ctrl-alt-n switch to virtual console 'n'\n"
1910 "ctrl-alt toggle mouse and keyboard grab\n"
1912 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1918 #define HAS_ARG 0x0001
1920 typedef struct QEMUOption
{
1927 static const QEMUOption qemu_options
[] = {
1928 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1929 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1930 { option, opt_arg, opt_enum, arch_mask },
1931 #define DEFHEADING(text)
1932 #include "qemu-options.def"
1938 static void select_vgahw (const char *p
)
1943 vga_interface_type
= VGA_NONE
;
1944 if (strstart(p
, "std", &opts
)) {
1945 vga_interface_type
= VGA_STD
;
1946 } else if (strstart(p
, "cirrus", &opts
)) {
1947 vga_interface_type
= VGA_CIRRUS
;
1948 } else if (strstart(p
, "vmware", &opts
)) {
1949 vga_interface_type
= VGA_VMWARE
;
1950 } else if (strstart(p
, "xenfb", &opts
)) {
1951 vga_interface_type
= VGA_XENFB
;
1952 } else if (!strstart(p
, "none", &opts
)) {
1954 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1958 const char *nextopt
;
1960 if (strstart(opts
, ",retrace=", &nextopt
)) {
1962 if (strstart(opts
, "dumb", &nextopt
))
1963 vga_retrace_method
= VGA_RETRACE_DUMB
;
1964 else if (strstart(opts
, "precise", &nextopt
))
1965 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1966 else goto invalid_vga
;
1967 } else goto invalid_vga
;
1972 static int balloon_parse(const char *arg
)
1976 if (strcmp(arg
, "none") == 0) {
1980 if (!strncmp(arg
, "virtio", 6)) {
1981 if (arg
[6] == ',') {
1982 /* have params -> parse them */
1983 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
1987 /* create empty opts */
1988 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1990 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1997 char *qemu_find_file(int type
, const char *name
)
2003 /* If name contains path separators then try it as a straight path. */
2004 if ((strchr(name
, '/') || strchr(name
, '\\'))
2005 && access(name
, R_OK
) == 0) {
2006 return qemu_strdup(name
);
2009 case QEMU_FILE_TYPE_BIOS
:
2012 case QEMU_FILE_TYPE_KEYMAP
:
2013 subdir
= "keymaps/";
2018 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2019 buf
= qemu_mallocz(len
);
2020 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2021 if (access(buf
, R_OK
)) {
2028 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2030 return qdev_device_help(opts
);
2033 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2037 dev
= qdev_device_add(opts
);
2043 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2045 CharDriverState
*chr
;
2047 chr
= qemu_chr_open_opts(opts
, NULL
);
2054 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2057 ret
= qemu_fsdev_add(opts
);
2063 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2065 CharDriverState
*chr
;
2066 const char *chardev
;
2070 mode
= qemu_opt_get(opts
, "mode");
2074 if (strcmp(mode
, "readline") == 0) {
2075 flags
= MONITOR_USE_READLINE
;
2076 } else if (strcmp(mode
, "control") == 0) {
2077 flags
= MONITOR_USE_CONTROL
;
2079 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2083 if (qemu_opt_get_bool(opts
, "default", 0))
2084 flags
|= MONITOR_IS_DEFAULT
;
2086 chardev
= qemu_opt_get(opts
, "chardev");
2087 chr
= qemu_chr_find(chardev
);
2089 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2093 monitor_init(chr
, flags
);
2097 static void monitor_parse(const char *optarg
, const char *mode
)
2099 static int monitor_device_index
= 0;
2105 if (strstart(optarg
, "chardev:", &p
)) {
2106 snprintf(label
, sizeof(label
), "%s", p
);
2108 snprintf(label
, sizeof(label
), "compat_monitor%d",
2109 monitor_device_index
);
2110 if (monitor_device_index
== 0) {
2113 opts
= qemu_chr_parse_compat(label
, optarg
);
2115 fprintf(stderr
, "parse error: %s\n", optarg
);
2120 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2122 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2125 qemu_opt_set(opts
, "mode", mode
);
2126 qemu_opt_set(opts
, "chardev", label
);
2128 qemu_opt_set(opts
, "default", "on");
2129 monitor_device_index
++;
2132 struct device_config
{
2134 DEV_USB
, /* -usbdevice */
2136 DEV_SERIAL
, /* -serial */
2137 DEV_PARALLEL
, /* -parallel */
2138 DEV_VIRTCON
, /* -virtioconsole */
2139 DEV_DEBUGCON
, /* -debugcon */
2141 const char *cmdline
;
2142 QTAILQ_ENTRY(device_config
) next
;
2144 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2146 static void add_device_config(int type
, const char *cmdline
)
2148 struct device_config
*conf
;
2150 conf
= qemu_mallocz(sizeof(*conf
));
2152 conf
->cmdline
= cmdline
;
2153 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2156 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2158 struct device_config
*conf
;
2161 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2162 if (conf
->type
!= type
)
2164 rc
= func(conf
->cmdline
);
2171 static int serial_parse(const char *devname
)
2173 static int index
= 0;
2176 if (strcmp(devname
, "none") == 0)
2178 if (index
== MAX_SERIAL_PORTS
) {
2179 fprintf(stderr
, "qemu: too many serial ports\n");
2182 snprintf(label
, sizeof(label
), "serial%d", index
);
2183 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2184 if (!serial_hds
[index
]) {
2185 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2186 devname
, strerror(errno
));
2193 static int parallel_parse(const char *devname
)
2195 static int index
= 0;
2198 if (strcmp(devname
, "none") == 0)
2200 if (index
== MAX_PARALLEL_PORTS
) {
2201 fprintf(stderr
, "qemu: too many parallel ports\n");
2204 snprintf(label
, sizeof(label
), "parallel%d", index
);
2205 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2206 if (!parallel_hds
[index
]) {
2207 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2208 devname
, strerror(errno
));
2215 static int virtcon_parse(const char *devname
)
2217 static int index
= 0;
2219 QemuOpts
*bus_opts
, *dev_opts
;
2221 if (strcmp(devname
, "none") == 0)
2223 if (index
== MAX_VIRTIO_CONSOLES
) {
2224 fprintf(stderr
, "qemu: too many virtio consoles\n");
2228 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2229 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2231 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2232 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2234 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2235 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2236 if (!virtcon_hds
[index
]) {
2237 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2238 devname
, strerror(errno
));
2241 qemu_opt_set(dev_opts
, "chardev", label
);
2247 static int debugcon_parse(const char *devname
)
2251 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2254 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2256 fprintf(stderr
, "qemu: already have a debugcon device\n");
2259 qemu_opt_set(opts
, "driver", "isa-debugcon");
2260 qemu_opt_set(opts
, "chardev", "debugcon");
2264 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2265 const char **poptarg
, int *poptind
)
2267 const QEMUOption
*popt
;
2268 int optind
= *poptind
;
2269 char *r
= argv
[optind
];
2272 loc_set_cmdline(argv
, optind
, 1);
2274 /* Treat --foo the same as -foo. */
2277 popt
= qemu_options
;
2280 error_report("invalid option");
2283 if (!strcmp(popt
->name
, r
+ 1))
2287 if (popt
->flags
& HAS_ARG
) {
2288 if (optind
>= argc
) {
2289 error_report("requires an argument");
2292 optarg
= argv
[optind
++];
2293 loc_set_cmdline(argv
, optind
- 2, 2);
2304 int main(int argc
, char **argv
, char **envp
)
2306 const char *gdbstub_dev
= NULL
;
2308 int snapshot
, linux_boot
;
2309 const char *icount_option
= NULL
;
2310 const char *initrd_filename
;
2311 const char *kernel_filename
, *kernel_cmdline
;
2312 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2314 DisplayChangeListener
*dcl
;
2315 int cyls
, heads
, secs
, translation
;
2316 QemuOpts
*hda_opts
= NULL
, *opts
;
2319 const char *loadvm
= NULL
;
2320 QEMUMachine
*machine
;
2321 const char *cpu_model
;
2323 const char *pid_file
= NULL
;
2324 const char *incoming
= NULL
;
2325 int show_vnc_port
= 0;
2328 error_set_progname(argv
[0]);
2332 qemu_cache_utils_init(envp
);
2334 QLIST_INIT (&vm_change_state_head
);
2335 os_setup_early_signal_handling();
2337 module_call_init(MODULE_INIT_MACHINE
);
2338 machine
= find_default_machine();
2340 initrd_filename
= NULL
;
2343 kernel_filename
= NULL
;
2344 kernel_cmdline
= "";
2345 cyls
= heads
= secs
= 0;
2346 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2348 for (i
= 0; i
< MAX_NODES
; i
++) {
2350 node_cpumask
[i
] = 0;
2353 assigned_devices_index
= 0;
2361 /* first pass of option parsing */
2363 while (optind
< argc
) {
2364 if (argv
[optind
][0] != '-') {
2369 const QEMUOption
*popt
;
2371 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2372 switch (popt
->index
) {
2373 case QEMU_OPTION_nodefconfig
:
2383 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2384 if (ret
< 0 && ret
!= -ENOENT
) {
2388 ret
= qemu_read_config_file(arch_config_name
);
2389 if (ret
< 0 && ret
!= -ENOENT
) {
2395 /* second pass of option parsing */
2400 if (argv
[optind
][0] != '-') {
2401 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2403 const QEMUOption
*popt
;
2405 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2406 if (!(popt
->arch_mask
& arch_type
)) {
2407 printf("Option %s not supported for this target\n", popt
->name
);
2410 switch(popt
->index
) {
2412 machine
= find_machine(optarg
);
2415 printf("Supported machines are:\n");
2416 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2418 printf("%-10s %s (alias of %s)\n",
2419 m
->alias
, m
->desc
, m
->name
);
2420 printf("%-10s %s%s\n",
2422 m
->is_default
? " (default)" : "");
2424 exit(*optarg
!= '?');
2427 case QEMU_OPTION_cpu
:
2428 /* hw initialization will check this */
2429 if (*optarg
== '?') {
2430 list_cpus(stdout
, &fprintf
, optarg
);
2436 case QEMU_OPTION_initrd
:
2437 initrd_filename
= optarg
;
2439 case QEMU_OPTION_hda
:
2441 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2443 hda_opts
= drive_add(optarg
, HD_ALIAS
2444 ",cyls=%d,heads=%d,secs=%d%s",
2445 0, cyls
, heads
, secs
,
2446 translation
== BIOS_ATA_TRANSLATION_LBA
?
2448 translation
== BIOS_ATA_TRANSLATION_NONE
?
2449 ",trans=none" : "");
2451 case QEMU_OPTION_hdb
:
2452 case QEMU_OPTION_hdc
:
2453 case QEMU_OPTION_hdd
:
2454 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2456 case QEMU_OPTION_drive
:
2457 drive_add(NULL
, "%s", optarg
);
2459 case QEMU_OPTION_set
:
2460 if (qemu_set_option(optarg
) != 0)
2463 case QEMU_OPTION_global
:
2464 if (qemu_global_option(optarg
) != 0)
2467 case QEMU_OPTION_mtdblock
:
2468 drive_add(optarg
, MTD_ALIAS
);
2470 case QEMU_OPTION_sd
:
2471 drive_add(optarg
, SD_ALIAS
);
2473 case QEMU_OPTION_pflash
:
2474 drive_add(optarg
, PFLASH_ALIAS
);
2476 case QEMU_OPTION_snapshot
:
2479 case QEMU_OPTION_hdachs
:
2483 cyls
= strtol(p
, (char **)&p
, 0);
2484 if (cyls
< 1 || cyls
> 16383)
2489 heads
= strtol(p
, (char **)&p
, 0);
2490 if (heads
< 1 || heads
> 16)
2495 secs
= strtol(p
, (char **)&p
, 0);
2496 if (secs
< 1 || secs
> 63)
2500 if (!strcmp(p
, "none"))
2501 translation
= BIOS_ATA_TRANSLATION_NONE
;
2502 else if (!strcmp(p
, "lba"))
2503 translation
= BIOS_ATA_TRANSLATION_LBA
;
2504 else if (!strcmp(p
, "auto"))
2505 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2508 } else if (*p
!= '\0') {
2510 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2513 if (hda_opts
!= NULL
) {
2515 snprintf(num
, sizeof(num
), "%d", cyls
);
2516 qemu_opt_set(hda_opts
, "cyls", num
);
2517 snprintf(num
, sizeof(num
), "%d", heads
);
2518 qemu_opt_set(hda_opts
, "heads", num
);
2519 snprintf(num
, sizeof(num
), "%d", secs
);
2520 qemu_opt_set(hda_opts
, "secs", num
);
2521 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2522 qemu_opt_set(hda_opts
, "trans", "lba");
2523 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2524 qemu_opt_set(hda_opts
, "trans", "none");
2528 case QEMU_OPTION_numa
:
2529 if (nb_numa_nodes
>= MAX_NODES
) {
2530 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2535 case QEMU_OPTION_nographic
:
2536 display_type
= DT_NOGRAPHIC
;
2538 #ifdef CONFIG_CURSES
2539 case QEMU_OPTION_curses
:
2540 display_type
= DT_CURSES
;
2543 case QEMU_OPTION_portrait
:
2546 case QEMU_OPTION_kernel
:
2547 kernel_filename
= optarg
;
2549 case QEMU_OPTION_append
:
2550 kernel_cmdline
= optarg
;
2552 case QEMU_OPTION_cdrom
:
2553 drive_add(optarg
, CDROM_ALIAS
);
2555 case QEMU_OPTION_boot
:
2557 static const char * const params
[] = {
2558 "order", "once", "menu", NULL
2560 char buf
[sizeof(boot_devices
)];
2561 char *standard_boot_devices
;
2564 if (!strchr(optarg
, '=')) {
2566 pstrcpy(buf
, sizeof(buf
), optarg
);
2567 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2569 "qemu: unknown boot parameter '%s' in '%s'\n",
2575 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2576 validate_bootdevices(buf
);
2577 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2580 if (get_param_value(buf
, sizeof(buf
),
2582 validate_bootdevices(buf
);
2583 standard_boot_devices
= qemu_strdup(boot_devices
);
2584 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2585 qemu_register_reset(restore_boot_devices
,
2586 standard_boot_devices
);
2588 if (get_param_value(buf
, sizeof(buf
),
2590 if (!strcmp(buf
, "on")) {
2592 } else if (!strcmp(buf
, "off")) {
2596 "qemu: invalid option value '%s'\n",
2604 case QEMU_OPTION_fda
:
2605 case QEMU_OPTION_fdb
:
2606 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2608 case QEMU_OPTION_no_fd_bootchk
:
2611 case QEMU_OPTION_netdev
:
2612 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2616 case QEMU_OPTION_net
:
2617 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2622 case QEMU_OPTION_tftp
:
2623 legacy_tftp_prefix
= optarg
;
2625 case QEMU_OPTION_bootp
:
2626 legacy_bootp_filename
= optarg
;
2628 case QEMU_OPTION_redir
:
2629 if (net_slirp_redir(optarg
) < 0)
2633 case QEMU_OPTION_bt
:
2634 add_device_config(DEV_BT
, optarg
);
2636 case QEMU_OPTION_audio_help
:
2637 if (!(audio_available())) {
2638 printf("Option %s not supported for this target\n", popt
->name
);
2644 case QEMU_OPTION_soundhw
:
2645 if (!(audio_available())) {
2646 printf("Option %s not supported for this target\n", popt
->name
);
2649 select_soundhw (optarg
);
2654 case QEMU_OPTION_version
:
2658 case QEMU_OPTION_m
: {
2662 value
= strtoul(optarg
, &ptr
, 10);
2664 case 0: case 'M': case 'm':
2671 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2675 /* On 32-bit hosts, QEMU is limited by virtual address space */
2676 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2677 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2680 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2681 fprintf(stderr
, "qemu: ram size too large\n");
2687 case QEMU_OPTION_mempath
:
2691 case QEMU_OPTION_mem_prealloc
:
2696 set_cpu_log(optarg
);
2699 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2701 case QEMU_OPTION_gdb
:
2702 gdbstub_dev
= optarg
;
2707 case QEMU_OPTION_bios
:
2710 case QEMU_OPTION_singlestep
:
2717 keyboard_layout
= optarg
;
2719 case QEMU_OPTION_localtime
:
2722 case QEMU_OPTION_vga
:
2723 select_vgahw (optarg
);
2730 w
= strtol(p
, (char **)&p
, 10);
2733 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2739 h
= strtol(p
, (char **)&p
, 10);
2744 depth
= strtol(p
, (char **)&p
, 10);
2745 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2746 depth
!= 24 && depth
!= 32)
2748 } else if (*p
== '\0') {
2749 depth
= graphic_depth
;
2756 graphic_depth
= depth
;
2759 case QEMU_OPTION_echr
:
2762 term_escape_char
= strtol(optarg
, &r
, 0);
2764 printf("Bad argument to echr\n");
2767 case QEMU_OPTION_monitor
:
2768 monitor_parse(optarg
, "readline");
2769 default_monitor
= 0;
2771 case QEMU_OPTION_qmp
:
2772 monitor_parse(optarg
, "control");
2773 default_monitor
= 0;
2775 case QEMU_OPTION_mon
:
2776 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
2780 default_monitor
= 0;
2782 case QEMU_OPTION_chardev
:
2783 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
2789 case QEMU_OPTION_fsdev
:
2790 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
2792 fprintf(stderr
, "parse error: %s\n", optarg
);
2796 case QEMU_OPTION_virtfs
: {
2797 char *arg_fsdev
= NULL
;
2798 char *arg_9p
= NULL
;
2801 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
2803 fprintf(stderr
, "parse error: %s\n", optarg
);
2807 len
= strlen(",id=,path=");
2808 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2809 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2810 len
+= strlen(qemu_opt_get(opts
, "path"));
2811 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2814 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
2819 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
2820 qemu_opt_get(opts
, "fstype"),
2821 qemu_opt_get(opts
, "mount_tag"),
2822 qemu_opt_get(opts
, "path"));
2824 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2825 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2826 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2829 fprintf(stderr
, "No memory to parse -device for %s\n",
2834 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2835 qemu_opt_get(opts
, "mount_tag"),
2836 qemu_opt_get(opts
, "mount_tag"));
2838 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
2839 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2843 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
2844 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2848 qemu_free(arg_fsdev
);
2853 case QEMU_OPTION_serial
:
2854 add_device_config(DEV_SERIAL
, optarg
);
2856 if (strncmp(optarg
, "mon:", 4) == 0) {
2857 default_monitor
= 0;
2860 case QEMU_OPTION_watchdog
:
2863 "qemu: only one watchdog option may be given\n");
2868 case QEMU_OPTION_watchdog_action
:
2869 if (select_watchdog_action(optarg
) == -1) {
2870 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2874 case QEMU_OPTION_virtiocon
:
2875 add_device_config(DEV_VIRTCON
, optarg
);
2876 default_virtcon
= 0;
2877 if (strncmp(optarg
, "mon:", 4) == 0) {
2878 default_monitor
= 0;
2881 case QEMU_OPTION_parallel
:
2882 add_device_config(DEV_PARALLEL
, optarg
);
2883 default_parallel
= 0;
2884 if (strncmp(optarg
, "mon:", 4) == 0) {
2885 default_monitor
= 0;
2888 case QEMU_OPTION_debugcon
:
2889 add_device_config(DEV_DEBUGCON
, optarg
);
2891 case QEMU_OPTION_loadvm
:
2894 case QEMU_OPTION_full_screen
:
2898 case QEMU_OPTION_no_frame
:
2901 case QEMU_OPTION_alt_grab
:
2904 case QEMU_OPTION_ctrl_grab
:
2907 case QEMU_OPTION_no_quit
:
2910 case QEMU_OPTION_sdl
:
2911 display_type
= DT_SDL
;
2914 case QEMU_OPTION_pidfile
:
2917 case QEMU_OPTION_win2k_hack
:
2918 win2k_install_hack
= 1;
2920 case QEMU_OPTION_rtc_td_hack
:
2923 case QEMU_OPTION_acpitable
:
2924 do_acpitable_option(optarg
);
2926 case QEMU_OPTION_smbios
:
2927 do_smbios_option(optarg
);
2930 case QEMU_OPTION_enable_kvm
:
2934 case QEMU_OPTION_no_kvm
:
2937 case QEMU_OPTION_no_kvm_irqchip
: {
2942 case QEMU_OPTION_no_kvm_pit
: {
2946 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2947 kvm_pit_reinject
= 0;
2950 case QEMU_OPTION_enable_nesting
: {
2954 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2955 case QEMU_OPTION_pcidevice
:
2956 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
2957 fprintf(stderr
, "Too many assigned devices\n");
2960 assigned_devices
[assigned_devices_index
] = optarg
;
2961 assigned_devices_index
++;
2964 case QEMU_OPTION_usb
:
2967 case QEMU_OPTION_usbdevice
:
2969 add_device_config(DEV_USB
, optarg
);
2971 case QEMU_OPTION_device
:
2972 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
2976 case QEMU_OPTION_smp
:
2979 fprintf(stderr
, "Invalid number of CPUs\n");
2982 if (max_cpus
< smp_cpus
) {
2983 fprintf(stderr
, "maxcpus must be equal to or greater than "
2987 if (max_cpus
> 255) {
2988 fprintf(stderr
, "Unsupported number of maxcpus\n");
2992 case QEMU_OPTION_vnc
:
2993 display_type
= DT_VNC
;
2994 vnc_display
= optarg
;
2996 case QEMU_OPTION_no_acpi
:
2999 case QEMU_OPTION_no_hpet
:
3002 case QEMU_OPTION_balloon
:
3003 if (balloon_parse(optarg
) < 0) {
3004 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3008 case QEMU_OPTION_no_reboot
:
3011 case QEMU_OPTION_no_shutdown
:
3014 case QEMU_OPTION_show_cursor
:
3017 case QEMU_OPTION_uuid
:
3018 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3019 fprintf(stderr
, "Fail to parse UUID string."
3020 " Wrong format.\n");
3024 case QEMU_OPTION_option_rom
:
3025 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3026 fprintf(stderr
, "Too many option ROMs\n");
3029 option_rom
[nb_option_roms
] = optarg
;
3032 case QEMU_OPTION_semihosting
:
3033 semihosting_enabled
= 1;
3035 case QEMU_OPTION_tdf
:
3038 case QEMU_OPTION_kvm_shadow_memory
:
3039 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3041 case QEMU_OPTION_name
:
3042 qemu_name
= qemu_strdup(optarg
);
3044 char *p
= strchr(qemu_name
, ',');
3047 if (strncmp(p
, "process=", 8)) {
3048 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3052 os_set_proc_name(p
);
3056 case QEMU_OPTION_prom_env
:
3057 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3058 fprintf(stderr
, "Too many prom variables\n");
3061 prom_envs
[nb_prom_envs
] = optarg
;
3064 case QEMU_OPTION_old_param
:
3067 case QEMU_OPTION_clock
:
3068 configure_alarms(optarg
);
3070 case QEMU_OPTION_startdate
:
3071 configure_rtc_date_offset(optarg
, 1);
3073 case QEMU_OPTION_rtc
:
3074 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3078 configure_rtc(opts
);
3080 case QEMU_OPTION_tb_size
:
3081 tb_size
= strtol(optarg
, NULL
, 0);
3085 case QEMU_OPTION_icount
:
3086 icount_option
= optarg
;
3088 case QEMU_OPTION_incoming
:
3091 case QEMU_OPTION_nodefaults
:
3093 default_parallel
= 0;
3094 default_virtcon
= 0;
3095 default_monitor
= 0;
3103 case QEMU_OPTION_nvram
:
3107 case QEMU_OPTION_xen_domid
:
3108 if (!(xen_available())) {
3109 printf("Option %s not supported for this target\n", popt
->name
);
3112 xen_domid
= atoi(optarg
);
3114 case QEMU_OPTION_xen_create
:
3115 if (!(xen_available())) {
3116 printf("Option %s not supported for this target\n", popt
->name
);
3119 xen_mode
= XEN_CREATE
;
3121 case QEMU_OPTION_xen_attach
:
3122 if (!(xen_available())) {
3123 printf("Option %s not supported for this target\n", popt
->name
);
3126 xen_mode
= XEN_ATTACH
;
3128 case QEMU_OPTION_readconfig
:
3130 int ret
= qemu_read_config_file(optarg
);
3132 fprintf(stderr
, "read config %s: %s\n", optarg
,
3138 case QEMU_OPTION_writeconfig
:
3141 if (strcmp(optarg
, "-") == 0) {
3144 fp
= fopen(optarg
, "w");
3146 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3150 qemu_config_write(fp
);
3155 os_parse_cmd_args(popt
->index
, optarg
);
3161 /* If no data_dir is specified then try to find it relative to the
3164 data_dir
= os_find_datadir(argv
[0]);
3166 /* If all else fails use the install patch specified when building. */
3168 data_dir
= CONFIG_QEMU_DATADIR
;
3172 * Default to max_cpus = smp_cpus, in case the user doesn't
3173 * specify a max_cpus value.
3176 max_cpus
= smp_cpus
;
3178 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3179 if (smp_cpus
> machine
->max_cpus
) {
3180 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3181 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3186 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3187 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3189 if (machine
->no_serial
) {
3192 if (machine
->no_parallel
) {
3193 default_parallel
= 0;
3195 if (!machine
->use_virtcon
) {
3196 default_virtcon
= 0;
3198 if (machine
->no_vga
) {
3201 if (machine
->no_floppy
) {
3204 if (machine
->no_cdrom
) {
3207 if (machine
->no_sdcard
) {
3211 if (display_type
== DT_NOGRAPHIC
) {
3212 if (default_parallel
)
3213 add_device_config(DEV_PARALLEL
, "null");
3214 if (default_serial
&& default_monitor
) {
3215 add_device_config(DEV_SERIAL
, "mon:stdio");
3216 } else if (default_virtcon
&& default_monitor
) {
3217 add_device_config(DEV_VIRTCON
, "mon:stdio");
3220 add_device_config(DEV_SERIAL
, "stdio");
3221 if (default_virtcon
)
3222 add_device_config(DEV_VIRTCON
, "stdio");
3223 if (default_monitor
)
3224 monitor_parse("stdio", "readline");
3228 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3229 if (default_parallel
)
3230 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3231 if (default_monitor
)
3232 monitor_parse("vc:80Cx24C", "readline");
3233 if (default_virtcon
)
3234 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3237 vga_interface_type
= VGA_CIRRUS
;
3241 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3244 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
3251 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3257 int ret
= kvm_init(smp_cpus
);
3259 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
3260 if (!kvm_available()) {
3261 printf("KVM not supported for this target\n");
3263 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3268 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
3274 if (qemu_init_main_loop()) {
3275 fprintf(stderr
, "qemu_init_main_loop failed\n");
3278 linux_boot
= (kernel_filename
!= NULL
);
3280 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3281 fprintf(stderr
, "-append only allowed with -kernel option\n");
3285 if (!linux_boot
&& initrd_filename
!= NULL
) {
3286 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3290 os_set_line_buffering();
3292 if (init_timer_alarm() < 0) {
3293 fprintf(stderr
, "could not initialize alarm timer\n");
3296 configure_icount(icount_option
);
3298 if (net_init_clients() < 0) {
3302 /* init the bluetooth world */
3303 if (foreach_device_config(DEV_BT
, bt_parse
))
3306 /* init the memory */
3308 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3310 /* init the dynamic translator */
3311 cpu_exec_init_all(tb_size
* 1024 * 1024);
3313 bdrv_init_with_whitelist();
3317 if (default_cdrom
) {
3318 /* we always create the cdrom drive, even if no disk is there */
3319 drive_add(NULL
, CDROM_ALIAS
);
3322 if (default_floppy
) {
3323 /* we always create at least one floppy */
3324 drive_add(NULL
, FD_ALIAS
, 0);
3327 if (default_sdcard
) {
3328 /* we always create one sd slot, even if no card is in it */
3329 drive_add(NULL
, SD_ALIAS
);
3332 /* open the virtual block devices */
3334 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3335 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3338 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3341 if (nb_numa_nodes
> 0) {
3344 if (nb_numa_nodes
> smp_cpus
) {
3345 nb_numa_nodes
= smp_cpus
;
3348 /* If no memory size if given for any node, assume the default case
3349 * and distribute the available memory equally across all nodes
3351 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3352 if (node_mem
[i
] != 0)
3355 if (i
== nb_numa_nodes
) {
3356 uint64_t usedmem
= 0;
3358 /* On Linux, the each node's border has to be 8MB aligned,
3359 * the final node gets the rest.
3361 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3362 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3363 usedmem
+= node_mem
[i
];
3365 node_mem
[i
] = ram_size
- usedmem
;
3368 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3369 if (node_cpumask
[i
] != 0)
3372 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3373 * must cope with this anyway, because there are BIOSes out there in
3374 * real machines which also use this scheme.
3376 if (i
== nb_numa_nodes
) {
3377 for (i
= 0; i
< smp_cpus
; i
++) {
3378 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3383 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
3387 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3389 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3391 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3393 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3396 module_call_init(MODULE_INIT_DEVICE
);
3398 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3402 i
= select_watchdog(watchdog
);
3404 exit (i
== 1 ? 1 : 0);
3407 if (machine
->compat_props
) {
3408 qdev_prop_register_global_list(machine
->compat_props
);
3412 machine
->init(ram_size
, boot_devices
,
3413 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3415 cpu_synchronize_all_post_init();
3417 /* must be after terminal init, SDL library changes signal handlers */
3418 os_setup_signal_handling();
3422 current_machine
= machine
;
3424 /* init USB devices */
3426 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3430 /* init generic devices */
3431 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3434 net_check_clients();
3436 /* just use the first displaystate for the moment */
3437 ds
= get_displaystate();
3439 if (display_type
== DT_DEFAULT
) {
3440 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3441 display_type
= DT_SDL
;
3443 display_type
= DT_VNC
;
3444 vnc_display
= "localhost:0,to=99";
3450 switch (display_type
) {
3453 #if defined(CONFIG_CURSES)
3455 curses_display_init(ds
, full_screen
);
3458 #if defined(CONFIG_SDL)
3460 sdl_display_init(ds
, full_screen
, no_frame
);
3462 #elif defined(CONFIG_COCOA)
3464 cocoa_display_init(ds
, full_screen
);
3468 vnc_display_init(ds
);
3469 if (vnc_display_open(ds
, vnc_display
) < 0)
3472 if (show_vnc_port
) {
3473 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3481 dcl
= ds
->listeners
;
3482 while (dcl
!= NULL
) {
3483 if (dcl
->dpy_refresh
!= NULL
) {
3484 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3485 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3491 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3492 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3493 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3496 text_consoles_set_display(ds
);
3498 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3499 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3504 qdev_machine_creation_done();
3506 if (rom_load_all() != 0) {
3507 fprintf(stderr
, "rom loading failed\n");
3511 qemu_system_reset();
3513 if (load_vmstate(loadvm
) < 0) {
3519 qemu_start_incoming_migration(incoming
);
3520 } else if (autostart
) {