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"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <sys/prctl.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
82 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84 extern int madvise(caddr_t
, size_t, int);
89 #if defined(__OpenBSD__)
93 #if defined(CONFIG_VDE)
94 #include <libvdeplug.h>
102 #if defined(__APPLE__) || defined(main)
104 int qemu_main(int argc
, char **argv
, char **envp
);
105 int main(int argc
, char **argv
)
107 return qemu_main(argc
, argv
, NULL
);
110 #define main qemu_main
112 #endif /* CONFIG_SDL */
116 #define main qemu_main
117 #endif /* CONFIG_COCOA */
120 #include "hw/boards.h"
122 #include "hw/pcmcia.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "block_int.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
150 #include "qemu-option.h"
151 #include "qemu-config.h"
152 #include "qemu-objects.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "qemu-queue.h"
162 #include "arch_init.h"
165 //#define DEBUG_SLIRP
167 #define DEFAULT_RAM_SIZE 128
169 #define MAX_VIRTIO_CONSOLES 1
171 static const char *data_dir
;
172 const char *bios_name
= NULL
;
173 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
174 to store the VM snapshots */
175 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
176 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
177 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
178 DisplayType display_type
= DT_DEFAULT
;
179 const char* keyboard_layout
= NULL
;
181 const char *mem_path
= NULL
;
183 int mem_prealloc
= 0; /* force preallocation of physical target memory */
186 NICInfo nd_table
[MAX_NICS
];
189 static int rtc_utc
= 1;
190 static int rtc_date_offset
= -1; /* -1 means no change */
191 QEMUClock
*rtc_clock
;
192 int vga_interface_type
= VGA_NONE
;
193 static int full_screen
= 0;
195 static int no_frame
= 0;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
201 int win2k_install_hack
= 0;
209 const char *vnc_display
;
210 int acpi_enabled
= 1;
216 int graphic_rotate
= 0;
217 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 const char *option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
234 uint64_t node_mem
[MAX_NODES
];
235 uint64_t node_cpumask
[MAX_NODES
];
237 static QEMUTimer
*nographic_timer
;
239 uint8_t qemu_uuid
[16];
241 static QEMUBootSetHandler
*boot_set_handler
;
242 static void *boot_set_opaque
;
246 enum xen_mode xen_mode
= XEN_EMULATE
;
249 #define SIG_IPI (SIGRTMIN+4)
251 #define SIG_IPI SIGUSR1
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 /***********************************************************/
296 static void set_proc_name(const char *s
)
298 #if defined(__linux__) && defined(PR_SET_NAME)
302 name
[sizeof(name
) - 1] = 0;
303 strncpy(name
, s
, sizeof(name
));
304 /* Could rewrite argv[0] too, but that's a bit more complicated.
305 This simple way is enough for `top'. */
306 prctl(PR_SET_NAME
, name
);
313 static QEMUBalloonEvent
*qemu_balloon_event
;
314 void *qemu_balloon_event_opaque
;
316 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
318 qemu_balloon_event
= func
;
319 qemu_balloon_event_opaque
= opaque
;
322 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
324 if (qemu_balloon_event
) {
325 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
332 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
334 if (qemu_balloon_event
) {
335 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
343 /***********************************************************/
344 /* real time host monotonic timer */
346 /* compute with 96 bit intermediate result: (a*b)/c */
347 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
352 #ifdef HOST_WORDS_BIGENDIAN
362 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
363 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
366 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
370 /***********************************************************/
371 /* host time/date access */
372 void qemu_get_timedate(struct tm
*tm
, int offset
)
379 if (rtc_date_offset
== -1) {
383 ret
= localtime(&ti
);
385 ti
-= rtc_date_offset
;
389 memcpy(tm
, ret
, sizeof(struct tm
));
392 int qemu_timedate_diff(struct tm
*tm
)
396 if (rtc_date_offset
== -1)
398 seconds
= mktimegm(tm
);
400 seconds
= mktime(tm
);
402 seconds
= mktimegm(tm
) + rtc_date_offset
;
404 return seconds
- time(NULL
);
407 void rtc_change_mon_event(struct tm
*tm
)
411 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
412 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
413 qobject_decref(data
);
416 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
418 time_t rtc_start_date
;
421 if (!strcmp(startdate
, "now") && legacy
) {
422 rtc_date_offset
= -1;
424 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
432 } else if (sscanf(startdate
, "%d-%d-%d",
444 rtc_start_date
= mktimegm(&tm
);
445 if (rtc_start_date
== -1) {
447 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
448 "'2006-06-17T16:01:21' or '2006-06-17'\n");
451 rtc_date_offset
= time(NULL
) - rtc_start_date
;
455 static void configure_rtc(QemuOpts
*opts
)
459 value
= qemu_opt_get(opts
, "base");
461 if (!strcmp(value
, "utc")) {
463 } else if (!strcmp(value
, "localtime")) {
466 configure_rtc_date_offset(value
, 0);
469 value
= qemu_opt_get(opts
, "clock");
471 if (!strcmp(value
, "host")) {
472 rtc_clock
= host_clock
;
473 } else if (!strcmp(value
, "vm")) {
474 rtc_clock
= vm_clock
;
476 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
480 value
= qemu_opt_get(opts
, "driftfix");
482 if (!strcmp(value
, "slew")) {
484 } else if (!strcmp(value
, "none")) {
487 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
493 /***********************************************************/
494 /* Bluetooth support */
497 static struct HCIInfo
*hci_table
[MAX_NICS
];
499 static struct bt_vlan_s
{
500 struct bt_scatternet_s net
;
502 struct bt_vlan_s
*next
;
505 /* find or alloc a new bluetooth "VLAN" */
506 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
508 struct bt_vlan_s
**pvlan
, *vlan
;
509 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
513 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
515 pvlan
= &first_bt_vlan
;
516 while (*pvlan
!= NULL
)
517 pvlan
= &(*pvlan
)->next
;
522 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
526 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
531 static struct HCIInfo null_hci
= {
532 .cmd_send
= null_hci_send
,
533 .sco_send
= null_hci_send
,
534 .acl_send
= null_hci_send
,
535 .bdaddr_set
= null_hci_addr_set
,
538 struct HCIInfo
*qemu_next_hci(void)
540 if (cur_hci
== nb_hcis
)
543 return hci_table
[cur_hci
++];
546 static struct HCIInfo
*hci_init(const char *str
)
549 struct bt_scatternet_s
*vlan
= 0;
551 if (!strcmp(str
, "null"))
554 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
556 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
557 else if (!strncmp(str
, "hci", 3)) {
560 if (!strncmp(str
+ 3, ",vlan=", 6)) {
561 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
566 vlan
= qemu_find_bt_vlan(0);
568 return bt_new_hci(vlan
);
571 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
576 static int bt_hci_parse(const char *str
)
581 if (nb_hcis
>= MAX_NICS
) {
582 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
595 bdaddr
.b
[5] = 0x56 + nb_hcis
;
596 hci
->bdaddr_set(hci
, bdaddr
.b
);
598 hci_table
[nb_hcis
++] = hci
;
603 static void bt_vhci_add(int vlan_id
)
605 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
608 fprintf(stderr
, "qemu: warning: adding a VHCI to "
609 "an empty scatternet %i\n", vlan_id
);
611 bt_vhci_init(bt_new_hci(vlan
));
614 static struct bt_device_s
*bt_device_add(const char *opt
)
616 struct bt_scatternet_s
*vlan
;
618 char *endp
= strstr(opt
, ",vlan=");
619 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
622 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
625 vlan_id
= strtol(endp
+ 6, &endp
, 0);
627 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
632 vlan
= qemu_find_bt_vlan(vlan_id
);
635 fprintf(stderr
, "qemu: warning: adding a slave device to "
636 "an empty scatternet %i\n", vlan_id
);
638 if (!strcmp(devname
, "keyboard"))
639 return bt_keyboard_init(vlan
);
641 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
645 static int bt_parse(const char *opt
)
647 const char *endp
, *p
;
650 if (strstart(opt
, "hci", &endp
)) {
651 if (!*endp
|| *endp
== ',') {
653 if (!strstart(endp
, ",vlan=", 0))
656 return bt_hci_parse(opt
);
658 } else if (strstart(opt
, "vhci", &endp
)) {
659 if (!*endp
|| *endp
== ',') {
661 if (strstart(endp
, ",vlan=", &p
)) {
662 vlan
= strtol(p
, (char **) &endp
, 0);
664 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
668 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
677 } else if (strstart(opt
, "device:", &endp
))
678 return !bt_device_add(endp
);
680 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
684 /***********************************************************/
685 /* QEMU Block devices */
687 #define HD_ALIAS "index=%d,media=disk"
688 #define CDROM_ALIAS "index=2,media=cdrom"
689 #define FD_ALIAS "index=%d,if=floppy"
690 #define PFLASH_ALIAS "if=pflash"
691 #define MTD_ALIAS "if=mtd"
692 #define SD_ALIAS "index=0,if=sd"
694 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
701 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
704 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
706 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
707 __FUNCTION__
, optstr
);
711 qemu_opt_set(opts
, "file", file
);
715 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
719 /* seek interface, bus and unit */
721 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
722 if (dinfo
->type
== type
&&
731 DriveInfo
*drive_get_by_id(const char *id
)
735 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
736 if (strcmp(id
, dinfo
->id
))
743 int drive_get_max_bus(BlockInterfaceType type
)
749 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
750 if(dinfo
->type
== type
&&
751 dinfo
->bus
> max_bus
)
752 max_bus
= dinfo
->bus
;
757 const char *drive_get_serial(BlockDriverState
*bdrv
)
761 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
762 if (dinfo
->bdrv
== bdrv
)
763 return dinfo
->serial
;
769 BlockInterfaceErrorAction
drive_get_on_error(
770 BlockDriverState
*bdrv
, int is_read
)
774 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
775 if (dinfo
->bdrv
== bdrv
)
776 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
779 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
782 static void bdrv_format_print(void *opaque
, const char *name
)
784 fprintf(stderr
, " %s", name
);
787 void drive_uninit(DriveInfo
*dinfo
)
789 qemu_opts_del(dinfo
->opts
);
790 bdrv_delete(dinfo
->bdrv
);
791 QTAILQ_REMOVE(&drives
, dinfo
, next
);
795 static int parse_block_error_action(const char *buf
, int is_read
)
797 if (!strcmp(buf
, "ignore")) {
798 return BLOCK_ERR_IGNORE
;
799 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
800 return BLOCK_ERR_STOP_ENOSPC
;
801 } else if (!strcmp(buf
, "stop")) {
802 return BLOCK_ERR_STOP_ANY
;
803 } else if (!strcmp(buf
, "report")) {
804 return BLOCK_ERR_REPORT
;
806 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
807 buf
, is_read
? "read" : "write");
812 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
816 const char *file
= NULL
;
819 const char *mediastr
= "";
820 BlockInterfaceType type
;
821 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
823 int cyls
, heads
, secs
, translation
;
824 BlockDriver
*drv
= NULL
;
825 QEMUMachine
*machine
= opaque
;
832 int on_read_error
, on_write_error
;
839 translation
= BIOS_ATA_TRANSLATION_AUTO
;
842 if (machine
&& machine
->use_scsi
) {
844 max_devs
= MAX_SCSI_DEVS
;
845 pstrcpy(devname
, sizeof(devname
), "scsi");
848 max_devs
= MAX_IDE_DEVS
;
849 pstrcpy(devname
, sizeof(devname
), "ide");
853 /* extract parameters */
854 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
855 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
856 index
= qemu_opt_get_number(opts
, "index", -1);
858 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
859 heads
= qemu_opt_get_number(opts
, "heads", 0);
860 secs
= qemu_opt_get_number(opts
, "secs", 0);
862 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
863 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
865 file
= qemu_opt_get(opts
, "file");
866 serial
= qemu_opt_get(opts
, "serial");
868 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
869 pstrcpy(devname
, sizeof(devname
), buf
);
870 if (!strcmp(buf
, "ide")) {
872 max_devs
= MAX_IDE_DEVS
;
873 } else if (!strcmp(buf
, "scsi")) {
875 max_devs
= MAX_SCSI_DEVS
;
876 } else if (!strcmp(buf
, "floppy")) {
879 } else if (!strcmp(buf
, "pflash")) {
882 } else if (!strcmp(buf
, "mtd")) {
885 } else if (!strcmp(buf
, "sd")) {
888 } else if (!strcmp(buf
, "virtio")) {
891 } else if (!strcmp(buf
, "xen")) {
894 } else if (!strcmp(buf
, "none")) {
898 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
903 if (cyls
|| heads
|| secs
) {
904 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
905 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
908 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
909 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
912 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
913 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
918 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
921 "qemu: '%s' trans must be used with cyls,heads and secs\n",
925 if (!strcmp(buf
, "none"))
926 translation
= BIOS_ATA_TRANSLATION_NONE
;
927 else if (!strcmp(buf
, "lba"))
928 translation
= BIOS_ATA_TRANSLATION_LBA
;
929 else if (!strcmp(buf
, "auto"))
930 translation
= BIOS_ATA_TRANSLATION_AUTO
;
932 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
937 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
938 if (!strcmp(buf
, "disk")) {
940 } else if (!strcmp(buf
, "cdrom")) {
941 if (cyls
|| secs
|| heads
) {
943 "qemu: '%s' invalid physical CHS format\n", buf
);
948 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
953 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
954 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
956 else if (!strcmp(buf
, "writethrough"))
958 else if (!strcmp(buf
, "writeback"))
961 fprintf(stderr
, "qemu: invalid cache option\n");
966 #ifdef CONFIG_LINUX_AIO
967 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
968 if (!strcmp(buf
, "threads"))
970 else if (!strcmp(buf
, "native"))
973 fprintf(stderr
, "qemu: invalid aio option\n");
979 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
980 if (strcmp(buf
, "?") == 0) {
981 fprintf(stderr
, "qemu: Supported formats:");
982 bdrv_iterate_format(bdrv_format_print
, NULL
);
983 fprintf(stderr
, "\n");
986 drv
= bdrv_find_whitelisted_format(buf
);
988 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
993 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
994 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
995 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
996 fprintf(stderr
, "werror is no supported by this format\n");
1000 on_write_error
= parse_block_error_action(buf
, 0);
1001 if (on_write_error
< 0) {
1006 on_read_error
= BLOCK_ERR_REPORT
;
1007 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1008 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1009 fprintf(stderr
, "rerror is no supported by this format\n");
1013 on_read_error
= parse_block_error_action(buf
, 1);
1014 if (on_read_error
< 0) {
1019 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1020 if (type
!= IF_VIRTIO
) {
1021 fprintf(stderr
, "addr is not supported\n");
1026 /* compute bus and unit according index */
1029 if (bus_id
!= 0 || unit_id
!= -1) {
1031 "qemu: index cannot be used with bus and unit\n");
1039 unit_id
= index
% max_devs
;
1040 bus_id
= index
/ max_devs
;
1044 /* if user doesn't specify a unit_id,
1045 * try to find the first free
1048 if (unit_id
== -1) {
1050 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1052 if (max_devs
&& unit_id
>= max_devs
) {
1053 unit_id
-= max_devs
;
1061 if (max_devs
&& unit_id
>= max_devs
) {
1062 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1063 unit_id
, max_devs
- 1);
1068 * ignore multiple definitions
1071 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1078 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1079 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1080 dinfo
->id
= qemu_strdup(buf
);
1082 /* no id supplied -> create one */
1083 dinfo
->id
= qemu_mallocz(32);
1084 if (type
== IF_IDE
|| type
== IF_SCSI
)
1085 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1087 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1088 devname
, bus_id
, mediastr
, unit_id
);
1090 snprintf(dinfo
->id
, 32, "%s%s%i",
1091 devname
, mediastr
, unit_id
);
1093 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1094 dinfo
->devaddr
= devaddr
;
1096 dinfo
->bus
= bus_id
;
1097 dinfo
->unit
= unit_id
;
1098 dinfo
->on_read_error
= on_read_error
;
1099 dinfo
->on_write_error
= on_write_error
;
1102 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1103 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1113 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1114 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1118 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1123 /* FIXME: This isn't really a floppy, but it's a reasonable
1126 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1132 /* add virtio block device */
1133 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1134 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1135 qemu_opt_set(opts
, "drive", dinfo
->id
);
1137 qemu_opt_set(opts
, "addr", devaddr
);
1148 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1149 cache
= 2; /* always use write-back with snapshot */
1151 if (cache
== 0) /* no caching */
1152 bdrv_flags
|= BDRV_O_NOCACHE
;
1153 else if (cache
== 2) /* write-back */
1154 bdrv_flags
|= BDRV_O_CACHE_WB
;
1157 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1159 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1162 if (media
== MEDIA_CDROM
) {
1163 /* CDROM is fine for any interface, don't check. */
1165 } else if (ro
== 1) {
1166 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1167 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1172 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1174 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1175 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1176 file
, strerror(errno
));
1180 if (bdrv_key_required(dinfo
->bdrv
))
1186 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1188 QEMUMachine
*machine
= opaque
;
1189 int fatal_error
= 0;
1191 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1198 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1200 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1201 qemu_opt_set(opts
, "snapshot", "on");
1206 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1208 boot_set_handler
= func
;
1209 boot_set_opaque
= opaque
;
1212 int qemu_boot_set(const char *boot_devices
)
1214 if (!boot_set_handler
) {
1217 return boot_set_handler(boot_set_opaque
, boot_devices
);
1220 static int parse_bootdevices(char *devices
)
1222 /* We just do some generic consistency checks */
1226 for (p
= devices
; *p
!= '\0'; p
++) {
1227 /* Allowed boot devices are:
1228 * a-b: floppy disk drives
1229 * c-f: IDE disk drives
1230 * g-m: machine implementation dependant drives
1231 * n-p: network devices
1232 * It's up to each machine implementation to check if the given boot
1233 * devices match the actual hardware implementation and firmware
1236 if (*p
< 'a' || *p
> 'p') {
1237 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1240 if (bitmap
& (1 << (*p
- 'a'))) {
1241 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1244 bitmap
|= 1 << (*p
- 'a');
1249 static void restore_boot_devices(void *opaque
)
1251 char *standard_boot_devices
= opaque
;
1253 qemu_boot_set(standard_boot_devices
);
1255 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1256 qemu_free(standard_boot_devices
);
1259 static void numa_add(const char *optarg
)
1263 unsigned long long value
, endvalue
;
1266 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1267 if (!strcmp(option
, "node")) {
1268 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1269 nodenr
= nb_numa_nodes
;
1271 nodenr
= strtoull(option
, NULL
, 10);
1274 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1275 node_mem
[nodenr
] = 0;
1277 value
= strtoull(option
, &endptr
, 0);
1279 case 0: case 'M': case 'm':
1286 node_mem
[nodenr
] = value
;
1288 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1289 node_cpumask
[nodenr
] = 0;
1291 value
= strtoull(option
, &endptr
, 10);
1294 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1296 if (*endptr
== '-') {
1297 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1298 if (endvalue
>= 63) {
1301 "only 63 CPUs in NUMA mode supported.\n");
1303 value
= (2ULL << endvalue
) - (1ULL << value
);
1305 value
= 1ULL << value
;
1308 node_cpumask
[nodenr
] = value
;
1315 static void smp_parse(const char *optarg
)
1317 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1321 smp
= strtoul(optarg
, &endptr
, 10);
1322 if (endptr
!= optarg
) {
1323 if (*endptr
== ',') {
1327 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1328 sockets
= strtoull(option
, NULL
, 10);
1329 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1330 cores
= strtoull(option
, NULL
, 10);
1331 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1332 threads
= strtoull(option
, NULL
, 10);
1333 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1334 max_cpus
= strtoull(option
, NULL
, 10);
1336 /* compute missing values, prefer sockets over cores over threads */
1337 if (smp
== 0 || sockets
== 0) {
1338 sockets
= sockets
> 0 ? sockets
: 1;
1339 cores
= cores
> 0 ? cores
: 1;
1340 threads
= threads
> 0 ? threads
: 1;
1342 smp
= cores
* threads
* sockets
;
1346 threads
= threads
> 0 ? threads
: 1;
1347 cores
= smp
/ (sockets
* threads
);
1350 threads
= smp
/ (cores
* sockets
);
1355 smp_cores
= cores
> 0 ? cores
: 1;
1356 smp_threads
= threads
> 0 ? threads
: 1;
1358 max_cpus
= smp_cpus
;
1361 /***********************************************************/
1364 static int usb_device_add(const char *devname
, int is_hotplug
)
1367 USBDevice
*dev
= NULL
;
1372 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1373 dev
= usbdevice_create(devname
);
1377 /* the other ones */
1378 if (strstart(devname
, "host:", &p
)) {
1379 dev
= usb_host_device_open(p
);
1380 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1381 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1382 bt_new_hci(qemu_find_bt_vlan(0)));
1393 static int usb_device_del(const char *devname
)
1398 if (strstart(devname
, "host:", &p
))
1399 return usb_host_device_close(p
);
1404 p
= strchr(devname
, '.');
1407 bus_num
= strtoul(devname
, NULL
, 0);
1408 addr
= strtoul(p
+ 1, NULL
, 0);
1410 return usb_device_delete_addr(bus_num
, addr
);
1413 static int usb_parse(const char *cmdline
)
1416 r
= usb_device_add(cmdline
, 0);
1418 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1423 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1425 const char *devname
= qdict_get_str(qdict
, "devname");
1426 if (usb_device_add(devname
, 1) < 0) {
1427 error_report("could not add USB device '%s'", devname
);
1431 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1433 const char *devname
= qdict_get_str(qdict
, "devname");
1434 if (usb_device_del(devname
) < 0) {
1435 error_report("could not delete USB device '%s'", devname
);
1439 /***********************************************************/
1440 /* PCMCIA/Cardbus */
1442 static struct pcmcia_socket_entry_s
{
1443 PCMCIASocket
*socket
;
1444 struct pcmcia_socket_entry_s
*next
;
1445 } *pcmcia_sockets
= 0;
1447 void pcmcia_socket_register(PCMCIASocket
*socket
)
1449 struct pcmcia_socket_entry_s
*entry
;
1451 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1452 entry
->socket
= socket
;
1453 entry
->next
= pcmcia_sockets
;
1454 pcmcia_sockets
= entry
;
1457 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1459 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1461 ptr
= &pcmcia_sockets
;
1462 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1463 if (entry
->socket
== socket
) {
1469 void pcmcia_info(Monitor
*mon
)
1471 struct pcmcia_socket_entry_s
*iter
;
1473 if (!pcmcia_sockets
)
1474 monitor_printf(mon
, "No PCMCIA sockets\n");
1476 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1477 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1478 iter
->socket
->attached
? iter
->socket
->card_string
:
1482 /***********************************************************/
1485 typedef struct IOHandlerRecord
{
1487 IOCanReadHandler
*fd_read_poll
;
1489 IOHandler
*fd_write
;
1492 /* temporary data */
1494 QLIST_ENTRY(IOHandlerRecord
) next
;
1497 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1498 QLIST_HEAD_INITIALIZER(io_handlers
);
1501 /* XXX: fd_read_poll should be suppressed, but an API change is
1502 necessary in the character devices to suppress fd_can_read(). */
1503 int qemu_set_fd_handler2(int fd
,
1504 IOCanReadHandler
*fd_read_poll
,
1506 IOHandler
*fd_write
,
1509 IOHandlerRecord
*ioh
;
1511 if (!fd_read
&& !fd_write
) {
1512 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1513 if (ioh
->fd
== fd
) {
1519 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1523 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1524 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1527 ioh
->fd_read_poll
= fd_read_poll
;
1528 ioh
->fd_read
= fd_read
;
1529 ioh
->fd_write
= fd_write
;
1530 ioh
->opaque
= opaque
;
1536 int qemu_set_fd_handler(int fd
,
1538 IOHandler
*fd_write
,
1541 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1545 /***********************************************************/
1546 /* Polling handling */
1548 typedef struct PollingEntry
{
1551 struct PollingEntry
*next
;
1554 static PollingEntry
*first_polling_entry
;
1556 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1558 PollingEntry
**ppe
, *pe
;
1559 pe
= qemu_mallocz(sizeof(PollingEntry
));
1561 pe
->opaque
= opaque
;
1562 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1567 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1569 PollingEntry
**ppe
, *pe
;
1570 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1572 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1580 /***********************************************************/
1581 /* Wait objects support */
1582 typedef struct WaitObjects
{
1584 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1585 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1586 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1589 static WaitObjects wait_objects
= {0};
1591 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1593 WaitObjects
*w
= &wait_objects
;
1595 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1597 w
->events
[w
->num
] = handle
;
1598 w
->func
[w
->num
] = func
;
1599 w
->opaque
[w
->num
] = opaque
;
1604 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1607 WaitObjects
*w
= &wait_objects
;
1610 for (i
= 0; i
< w
->num
; i
++) {
1611 if (w
->events
[i
] == handle
)
1614 w
->events
[i
] = w
->events
[i
+ 1];
1615 w
->func
[i
] = w
->func
[i
+ 1];
1616 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1624 /***********************************************************/
1625 /* machine registration */
1627 static QEMUMachine
*first_machine
= NULL
;
1628 QEMUMachine
*current_machine
= NULL
;
1630 int qemu_register_machine(QEMUMachine
*m
)
1633 pm
= &first_machine
;
1641 static QEMUMachine
*find_machine(const char *name
)
1645 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1646 if (!strcmp(m
->name
, name
))
1648 if (m
->alias
&& !strcmp(m
->alias
, name
))
1654 static QEMUMachine
*find_default_machine(void)
1658 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1659 if (m
->is_default
) {
1666 /***********************************************************/
1667 /* main execution loop */
1669 static void gui_update(void *opaque
)
1671 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1672 DisplayState
*ds
= opaque
;
1673 DisplayChangeListener
*dcl
= ds
->listeners
;
1675 qemu_flush_coalesced_mmio_buffer();
1678 while (dcl
!= NULL
) {
1679 if (dcl
->gui_timer_interval
&&
1680 dcl
->gui_timer_interval
< interval
)
1681 interval
= dcl
->gui_timer_interval
;
1684 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1687 static void nographic_update(void *opaque
)
1689 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1691 qemu_flush_coalesced_mmio_buffer();
1692 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1695 struct vm_change_state_entry
{
1696 VMChangeStateHandler
*cb
;
1698 QLIST_ENTRY (vm_change_state_entry
) entries
;
1701 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1703 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1706 VMChangeStateEntry
*e
;
1708 e
= qemu_mallocz(sizeof (*e
));
1712 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1716 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1718 QLIST_REMOVE (e
, entries
);
1722 void vm_state_notify(int running
, int reason
)
1724 VMChangeStateEntry
*e
;
1726 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1727 e
->cb(e
->opaque
, running
, reason
);
1736 vm_state_notify(1, 0);
1741 /* reset/shutdown handler */
1743 typedef struct QEMUResetEntry
{
1744 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1745 QEMUResetHandler
*func
;
1749 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1750 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1751 static int reset_requested
;
1752 static int shutdown_requested
;
1753 static int powerdown_requested
;
1754 int debug_requested
;
1755 static int vmstop_requested
;
1757 int qemu_shutdown_requested(void)
1759 int r
= shutdown_requested
;
1760 shutdown_requested
= 0;
1764 int qemu_reset_requested(void)
1766 int r
= reset_requested
;
1767 reset_requested
= 0;
1771 int qemu_powerdown_requested(void)
1773 int r
= powerdown_requested
;
1774 powerdown_requested
= 0;
1778 static int qemu_debug_requested(void)
1780 int r
= debug_requested
;
1781 debug_requested
= 0;
1785 static int qemu_vmstop_requested(void)
1787 int r
= vmstop_requested
;
1788 vmstop_requested
= 0;
1792 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1794 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1797 re
->opaque
= opaque
;
1798 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1801 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1805 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1806 if (re
->func
== func
&& re
->opaque
== opaque
) {
1807 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1814 void qemu_system_reset(void)
1816 QEMUResetEntry
*re
, *nre
;
1818 /* reset all devices */
1819 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1820 re
->func(re
->opaque
);
1822 monitor_protocol_event(QEVENT_RESET
, NULL
);
1823 cpu_synchronize_all_post_reset();
1826 void qemu_system_reset_request(void)
1829 shutdown_requested
= 1;
1831 reset_requested
= 1;
1833 qemu_notify_event();
1836 void qemu_system_shutdown_request(void)
1838 shutdown_requested
= 1;
1839 qemu_notify_event();
1842 void qemu_system_powerdown_request(void)
1844 powerdown_requested
= 1;
1845 qemu_notify_event();
1849 static void host_main_loop_wait(int *timeout
)
1855 /* XXX: need to suppress polling by better using win32 events */
1857 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1858 ret
|= pe
->func(pe
->opaque
);
1862 WaitObjects
*w
= &wait_objects
;
1864 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1865 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1866 if (w
->func
[ret
- WAIT_OBJECT_0
])
1867 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1869 /* Check for additional signaled events */
1870 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1872 /* Check if event is signaled */
1873 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1874 if(ret2
== WAIT_OBJECT_0
) {
1876 w
->func
[i
](w
->opaque
[i
]);
1877 } else if (ret2
== WAIT_TIMEOUT
) {
1879 err
= GetLastError();
1880 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1883 } else if (ret
== WAIT_TIMEOUT
) {
1885 err
= GetLastError();
1886 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1893 static void host_main_loop_wait(int *timeout
)
1898 void main_loop_wait(int nonblocking
)
1900 IOHandlerRecord
*ioh
;
1901 fd_set rfds
, wfds
, xfds
;
1909 timeout
= qemu_calculate_timeout();
1910 qemu_bh_update_timeout(&timeout
);
1913 host_main_loop_wait(&timeout
);
1915 /* poll any events */
1916 /* XXX: separate device handlers from system ones */
1921 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1925 (!ioh
->fd_read_poll
||
1926 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1927 FD_SET(ioh
->fd
, &rfds
);
1931 if (ioh
->fd_write
) {
1932 FD_SET(ioh
->fd
, &wfds
);
1938 tv
.tv_sec
= timeout
/ 1000;
1939 tv
.tv_usec
= (timeout
% 1000) * 1000;
1941 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1943 qemu_mutex_unlock_iothread();
1944 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1945 qemu_mutex_lock_iothread();
1947 IOHandlerRecord
*pioh
;
1949 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1951 QLIST_REMOVE(ioh
, next
);
1955 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1956 ioh
->fd_read(ioh
->opaque
);
1958 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1959 ioh
->fd_write(ioh
->opaque
);
1964 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1966 qemu_run_all_timers();
1968 /* Check bottom-halves last in case any of the earlier events triggered
1974 static int vm_can_run(void)
1976 if (powerdown_requested
)
1978 if (reset_requested
)
1980 if (shutdown_requested
)
1982 if (debug_requested
)
1987 qemu_irq qemu_system_powerdown
;
1989 static void main_loop(void)
1993 #ifdef CONFIG_IOTHREAD
1994 qemu_system_ready
= 1;
1995 qemu_cond_broadcast(&qemu_system_cond
);
2000 bool nonblocking
= false;
2001 #ifdef CONFIG_PROFILER
2004 #ifndef CONFIG_IOTHREAD
2005 nonblocking
= tcg_cpu_exec();
2007 #ifdef CONFIG_PROFILER
2008 ti
= profile_getclock();
2010 main_loop_wait(nonblocking
);
2011 #ifdef CONFIG_PROFILER
2012 dev_time
+= profile_getclock() - ti
;
2014 } while (vm_can_run());
2016 if ((r
= qemu_debug_requested())) {
2019 if (qemu_shutdown_requested()) {
2020 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2027 if (qemu_reset_requested()) {
2029 qemu_system_reset();
2032 if (qemu_powerdown_requested()) {
2033 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2034 qemu_irq_raise(qemu_system_powerdown
);
2036 if ((r
= qemu_vmstop_requested())) {
2043 static void version(void)
2045 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2048 static void help(int exitcode
)
2050 const char *options_help
=
2051 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2053 #define DEFHEADING(text) stringify(text) "\n"
2054 #include "qemu-options.h"
2060 printf("usage: %s [options] [disk_image]\n"
2062 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2065 "During emulation, the following keys are useful:\n"
2066 "ctrl-alt-f toggle full screen\n"
2067 "ctrl-alt-n switch to virtual console 'n'\n"
2068 "ctrl-alt toggle mouse and keyboard grab\n"
2070 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2076 #define HAS_ARG 0x0001
2079 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2081 #define DEFHEADING(text)
2082 #include "qemu-options.h"
2088 typedef struct QEMUOption
{
2095 static const QEMUOption qemu_options
[] = {
2096 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2097 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2098 { option, opt_arg, opt_enum, arch_mask },
2099 #define DEFHEADING(text)
2100 #include "qemu-options.h"
2106 static void select_vgahw (const char *p
)
2111 vga_interface_type
= VGA_NONE
;
2112 if (strstart(p
, "std", &opts
)) {
2113 vga_interface_type
= VGA_STD
;
2114 } else if (strstart(p
, "cirrus", &opts
)) {
2115 vga_interface_type
= VGA_CIRRUS
;
2116 } else if (strstart(p
, "vmware", &opts
)) {
2117 vga_interface_type
= VGA_VMWARE
;
2118 } else if (strstart(p
, "xenfb", &opts
)) {
2119 vga_interface_type
= VGA_XENFB
;
2120 } else if (!strstart(p
, "none", &opts
)) {
2122 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2126 const char *nextopt
;
2128 if (strstart(opts
, ",retrace=", &nextopt
)) {
2130 if (strstart(opts
, "dumb", &nextopt
))
2131 vga_retrace_method
= VGA_RETRACE_DUMB
;
2132 else if (strstart(opts
, "precise", &nextopt
))
2133 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2134 else goto invalid_vga
;
2135 } else goto invalid_vga
;
2140 static int balloon_parse(const char *arg
)
2144 if (strcmp(arg
, "none") == 0) {
2148 if (!strncmp(arg
, "virtio", 6)) {
2149 if (arg
[6] == ',') {
2150 /* have params -> parse them */
2151 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2155 /* create empty opts */
2156 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2158 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2166 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2168 exit(STATUS_CONTROL_C_EXIT
);
2175 static void termsig_handler(int signal
)
2177 qemu_system_shutdown_request();
2180 static void sigchld_handler(int signal
)
2182 waitpid(-1, NULL
, WNOHANG
);
2185 static void sighandler_setup(void)
2187 struct sigaction act
;
2189 memset(&act
, 0, sizeof(act
));
2190 act
.sa_handler
= termsig_handler
;
2191 sigaction(SIGINT
, &act
, NULL
);
2192 sigaction(SIGHUP
, &act
, NULL
);
2193 sigaction(SIGTERM
, &act
, NULL
);
2195 act
.sa_handler
= sigchld_handler
;
2196 act
.sa_flags
= SA_NOCLDSTOP
;
2197 sigaction(SIGCHLD
, &act
, NULL
);
2203 /* Look for support files in the same directory as the executable. */
2204 static char *find_datadir(const char *argv0
)
2210 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2217 while (p
!= buf
&& *p
!= '\\')
2220 if (access(buf
, R_OK
) == 0) {
2221 return qemu_strdup(buf
);
2227 /* Find a likely location for support files using the location of the binary.
2228 For installed binaries this will be "$bindir/../share/qemu". When
2229 running from the build tree this will be "$bindir/../pc-bios". */
2230 #define SHARE_SUFFIX "/share/qemu"
2231 #define BUILD_SUFFIX "/pc-bios"
2232 static char *find_datadir(const char *argv0
)
2240 #if defined(__linux__)
2243 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2249 #elif defined(__FreeBSD__)
2251 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2252 size_t len
= sizeof(buf
) - 1;
2255 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2257 buf
[sizeof(buf
) - 1] = '\0';
2262 /* If we don't have any way of figuring out the actual executable
2263 location then try argv[0]. */
2265 p
= realpath(argv0
, buf
);
2273 max_len
= strlen(dir
) +
2274 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2275 res
= qemu_mallocz(max_len
);
2276 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2277 if (access(res
, R_OK
)) {
2278 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2279 if (access(res
, R_OK
)) {
2291 char *qemu_find_file(int type
, const char *name
)
2297 /* If name contains path separators then try it as a straight path. */
2298 if ((strchr(name
, '/') || strchr(name
, '\\'))
2299 && access(name
, R_OK
) == 0) {
2300 return qemu_strdup(name
);
2303 case QEMU_FILE_TYPE_BIOS
:
2306 case QEMU_FILE_TYPE_KEYMAP
:
2307 subdir
= "keymaps/";
2312 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2313 buf
= qemu_mallocz(len
);
2314 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2315 if (access(buf
, R_OK
)) {
2322 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2324 return qdev_device_help(opts
);
2327 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2331 dev
= qdev_device_add(opts
);
2337 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2339 CharDriverState
*chr
;
2341 chr
= qemu_chr_open_opts(opts
, NULL
);
2347 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2349 CharDriverState
*chr
;
2350 const char *chardev
;
2354 mode
= qemu_opt_get(opts
, "mode");
2358 if (strcmp(mode
, "readline") == 0) {
2359 flags
= MONITOR_USE_READLINE
;
2360 } else if (strcmp(mode
, "control") == 0) {
2361 flags
= MONITOR_USE_CONTROL
;
2363 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2367 if (qemu_opt_get_bool(opts
, "default", 0))
2368 flags
|= MONITOR_IS_DEFAULT
;
2370 chardev
= qemu_opt_get(opts
, "chardev");
2371 chr
= qemu_chr_find(chardev
);
2373 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2377 monitor_init(chr
, flags
);
2381 static void monitor_parse(const char *optarg
, const char *mode
)
2383 static int monitor_device_index
= 0;
2389 if (strstart(optarg
, "chardev:", &p
)) {
2390 snprintf(label
, sizeof(label
), "%s", p
);
2392 if (monitor_device_index
) {
2393 snprintf(label
, sizeof(label
), "monitor%d",
2394 monitor_device_index
);
2396 snprintf(label
, sizeof(label
), "monitor");
2399 opts
= qemu_chr_parse_compat(label
, optarg
);
2401 fprintf(stderr
, "parse error: %s\n", optarg
);
2406 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2408 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2411 qemu_opt_set(opts
, "mode", mode
);
2412 qemu_opt_set(opts
, "chardev", label
);
2414 qemu_opt_set(opts
, "default", "on");
2415 monitor_device_index
++;
2418 struct device_config
{
2420 DEV_USB
, /* -usbdevice */
2422 DEV_SERIAL
, /* -serial */
2423 DEV_PARALLEL
, /* -parallel */
2424 DEV_VIRTCON
, /* -virtioconsole */
2425 DEV_DEBUGCON
, /* -debugcon */
2427 const char *cmdline
;
2428 QTAILQ_ENTRY(device_config
) next
;
2430 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2432 static void add_device_config(int type
, const char *cmdline
)
2434 struct device_config
*conf
;
2436 conf
= qemu_mallocz(sizeof(*conf
));
2438 conf
->cmdline
= cmdline
;
2439 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2442 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2444 struct device_config
*conf
;
2447 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2448 if (conf
->type
!= type
)
2450 rc
= func(conf
->cmdline
);
2457 static int serial_parse(const char *devname
)
2459 static int index
= 0;
2462 if (strcmp(devname
, "none") == 0)
2464 if (index
== MAX_SERIAL_PORTS
) {
2465 fprintf(stderr
, "qemu: too many serial ports\n");
2468 snprintf(label
, sizeof(label
), "serial%d", index
);
2469 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2470 if (!serial_hds
[index
]) {
2471 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2472 devname
, strerror(errno
));
2479 static int parallel_parse(const char *devname
)
2481 static int index
= 0;
2484 if (strcmp(devname
, "none") == 0)
2486 if (index
== MAX_PARALLEL_PORTS
) {
2487 fprintf(stderr
, "qemu: too many parallel ports\n");
2490 snprintf(label
, sizeof(label
), "parallel%d", index
);
2491 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2492 if (!parallel_hds
[index
]) {
2493 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2494 devname
, strerror(errno
));
2501 static int virtcon_parse(const char *devname
)
2503 static int index
= 0;
2505 QemuOpts
*bus_opts
, *dev_opts
;
2507 if (strcmp(devname
, "none") == 0)
2509 if (index
== MAX_VIRTIO_CONSOLES
) {
2510 fprintf(stderr
, "qemu: too many virtio consoles\n");
2514 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2515 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2517 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2518 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2520 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2521 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2522 if (!virtcon_hds
[index
]) {
2523 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2524 devname
, strerror(errno
));
2527 qemu_opt_set(dev_opts
, "chardev", label
);
2533 static int debugcon_parse(const char *devname
)
2537 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2540 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2542 fprintf(stderr
, "qemu: already have a debugcon device\n");
2545 qemu_opt_set(opts
, "driver", "isa-debugcon");
2546 qemu_opt_set(opts
, "chardev", "debugcon");
2550 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2551 const char **poptarg
, int *poptind
)
2553 const QEMUOption
*popt
;
2554 int optind
= *poptind
;
2555 char *r
= argv
[optind
];
2558 loc_set_cmdline(argv
, optind
, 1);
2560 /* Treat --foo the same as -foo. */
2563 popt
= qemu_options
;
2566 error_report("invalid option");
2569 if (!strcmp(popt
->name
, r
+ 1))
2573 if (popt
->flags
& HAS_ARG
) {
2574 if (optind
>= argc
) {
2575 error_report("requires an argument");
2578 optarg
= argv
[optind
++];
2579 loc_set_cmdline(argv
, optind
- 2, 2);
2590 int main(int argc
, char **argv
, char **envp
)
2592 const char *gdbstub_dev
= NULL
;
2593 uint32_t boot_devices_bitmap
= 0;
2595 int snapshot
, linux_boot
, net_boot
;
2596 const char *icount_option
= NULL
;
2597 const char *initrd_filename
;
2598 const char *kernel_filename
, *kernel_cmdline
;
2599 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2601 DisplayChangeListener
*dcl
;
2602 int cyls
, heads
, secs
, translation
;
2603 QemuOpts
*hda_opts
= NULL
, *opts
;
2606 const char *loadvm
= NULL
;
2607 QEMUMachine
*machine
;
2608 const char *cpu_model
;
2613 const char *pid_file
= NULL
;
2614 const char *incoming
= NULL
;
2617 struct passwd
*pwd
= NULL
;
2618 const char *chroot_dir
= NULL
;
2619 const char *run_as
= NULL
;
2621 int show_vnc_port
= 0;
2624 error_set_progname(argv
[0]);
2628 qemu_cache_utils_init(envp
);
2630 QLIST_INIT (&vm_change_state_head
);
2633 struct sigaction act
;
2634 sigfillset(&act
.sa_mask
);
2636 act
.sa_handler
= SIG_IGN
;
2637 sigaction(SIGPIPE
, &act
, NULL
);
2640 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2641 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2642 QEMU to run on a single CPU */
2647 h
= GetCurrentProcess();
2648 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2649 for(i
= 0; i
< 32; i
++) {
2650 if (mask
& (1 << i
))
2655 SetProcessAffinityMask(h
, mask
);
2661 module_call_init(MODULE_INIT_MACHINE
);
2662 machine
= find_default_machine();
2664 initrd_filename
= NULL
;
2667 kernel_filename
= NULL
;
2668 kernel_cmdline
= "";
2669 cyls
= heads
= secs
= 0;
2670 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2672 for (i
= 0; i
< MAX_NODES
; i
++) {
2674 node_cpumask
[i
] = 0;
2683 /* first pass of option parsing */
2685 while (optind
< argc
) {
2686 if (argv
[optind
][0] != '-') {
2691 const QEMUOption
*popt
;
2693 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2694 switch (popt
->index
) {
2695 case QEMU_OPTION_nodefconfig
:
2706 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2707 fp
= fopen(fname
, "r");
2709 if (qemu_config_parse(fp
, fname
) != 0) {
2715 fname
= arch_config_name
;
2716 fp
= fopen(fname
, "r");
2718 if (qemu_config_parse(fp
, fname
) != 0) {
2726 /* second pass of option parsing */
2731 if (argv
[optind
][0] != '-') {
2732 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2734 const QEMUOption
*popt
;
2736 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2737 if (!(popt
->arch_mask
& arch_type
)) {
2738 printf("Option %s not supported for this target\n", popt
->name
);
2741 switch(popt
->index
) {
2743 machine
= find_machine(optarg
);
2746 printf("Supported machines are:\n");
2747 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2749 printf("%-10s %s (alias of %s)\n",
2750 m
->alias
, m
->desc
, m
->name
);
2751 printf("%-10s %s%s\n",
2753 m
->is_default
? " (default)" : "");
2755 exit(*optarg
!= '?');
2758 case QEMU_OPTION_cpu
:
2759 /* hw initialization will check this */
2760 if (*optarg
== '?') {
2761 /* XXX: implement xxx_cpu_list for targets that still miss it */
2762 #if defined(cpu_list_id)
2763 cpu_list_id(stdout
, &fprintf
, optarg
);
2764 #elif defined(cpu_list)
2765 cpu_list(stdout
, &fprintf
); /* deprecated */
2772 case QEMU_OPTION_initrd
:
2773 initrd_filename
= optarg
;
2775 case QEMU_OPTION_hda
:
2777 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2779 hda_opts
= drive_add(optarg
, HD_ALIAS
2780 ",cyls=%d,heads=%d,secs=%d%s",
2781 0, cyls
, heads
, secs
,
2782 translation
== BIOS_ATA_TRANSLATION_LBA
?
2784 translation
== BIOS_ATA_TRANSLATION_NONE
?
2785 ",trans=none" : "");
2787 case QEMU_OPTION_hdb
:
2788 case QEMU_OPTION_hdc
:
2789 case QEMU_OPTION_hdd
:
2790 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2792 case QEMU_OPTION_drive
:
2793 drive_add(NULL
, "%s", optarg
);
2795 case QEMU_OPTION_set
:
2796 if (qemu_set_option(optarg
) != 0)
2799 case QEMU_OPTION_global
:
2800 if (qemu_global_option(optarg
) != 0)
2803 case QEMU_OPTION_mtdblock
:
2804 drive_add(optarg
, MTD_ALIAS
);
2806 case QEMU_OPTION_sd
:
2807 drive_add(optarg
, SD_ALIAS
);
2809 case QEMU_OPTION_pflash
:
2810 drive_add(optarg
, PFLASH_ALIAS
);
2812 case QEMU_OPTION_snapshot
:
2815 case QEMU_OPTION_hdachs
:
2819 cyls
= strtol(p
, (char **)&p
, 0);
2820 if (cyls
< 1 || cyls
> 16383)
2825 heads
= strtol(p
, (char **)&p
, 0);
2826 if (heads
< 1 || heads
> 16)
2831 secs
= strtol(p
, (char **)&p
, 0);
2832 if (secs
< 1 || secs
> 63)
2836 if (!strcmp(p
, "none"))
2837 translation
= BIOS_ATA_TRANSLATION_NONE
;
2838 else if (!strcmp(p
, "lba"))
2839 translation
= BIOS_ATA_TRANSLATION_LBA
;
2840 else if (!strcmp(p
, "auto"))
2841 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2844 } else if (*p
!= '\0') {
2846 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2849 if (hda_opts
!= NULL
) {
2851 snprintf(num
, sizeof(num
), "%d", cyls
);
2852 qemu_opt_set(hda_opts
, "cyls", num
);
2853 snprintf(num
, sizeof(num
), "%d", heads
);
2854 qemu_opt_set(hda_opts
, "heads", num
);
2855 snprintf(num
, sizeof(num
), "%d", secs
);
2856 qemu_opt_set(hda_opts
, "secs", num
);
2857 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2858 qemu_opt_set(hda_opts
, "trans", "lba");
2859 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2860 qemu_opt_set(hda_opts
, "trans", "none");
2864 case QEMU_OPTION_numa
:
2865 if (nb_numa_nodes
>= MAX_NODES
) {
2866 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2871 case QEMU_OPTION_nographic
:
2872 display_type
= DT_NOGRAPHIC
;
2874 #ifdef CONFIG_CURSES
2875 case QEMU_OPTION_curses
:
2876 display_type
= DT_CURSES
;
2879 case QEMU_OPTION_portrait
:
2882 case QEMU_OPTION_kernel
:
2883 kernel_filename
= optarg
;
2885 case QEMU_OPTION_append
:
2886 kernel_cmdline
= optarg
;
2888 case QEMU_OPTION_cdrom
:
2889 drive_add(optarg
, CDROM_ALIAS
);
2891 case QEMU_OPTION_boot
:
2893 static const char * const params
[] = {
2894 "order", "once", "menu", NULL
2896 char buf
[sizeof(boot_devices
)];
2897 char *standard_boot_devices
;
2900 if (!strchr(optarg
, '=')) {
2902 pstrcpy(buf
, sizeof(buf
), optarg
);
2903 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2905 "qemu: unknown boot parameter '%s' in '%s'\n",
2911 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2912 boot_devices_bitmap
= parse_bootdevices(buf
);
2913 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2916 if (get_param_value(buf
, sizeof(buf
),
2918 boot_devices_bitmap
|= parse_bootdevices(buf
);
2919 standard_boot_devices
= qemu_strdup(boot_devices
);
2920 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2921 qemu_register_reset(restore_boot_devices
,
2922 standard_boot_devices
);
2924 if (get_param_value(buf
, sizeof(buf
),
2926 if (!strcmp(buf
, "on")) {
2928 } else if (!strcmp(buf
, "off")) {
2932 "qemu: invalid option value '%s'\n",
2940 case QEMU_OPTION_fda
:
2941 case QEMU_OPTION_fdb
:
2942 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2944 case QEMU_OPTION_no_fd_bootchk
:
2947 case QEMU_OPTION_netdev
:
2948 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2952 case QEMU_OPTION_net
:
2953 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2958 case QEMU_OPTION_tftp
:
2959 legacy_tftp_prefix
= optarg
;
2961 case QEMU_OPTION_bootp
:
2962 legacy_bootp_filename
= optarg
;
2965 case QEMU_OPTION_smb
:
2966 if (net_slirp_smb(optarg
) < 0)
2970 case QEMU_OPTION_redir
:
2971 if (net_slirp_redir(optarg
) < 0)
2975 case QEMU_OPTION_bt
:
2976 add_device_config(DEV_BT
, optarg
);
2978 case QEMU_OPTION_audio_help
:
2979 if (!(audio_available())) {
2980 printf("Option %s not supported for this target\n", popt
->name
);
2986 case QEMU_OPTION_soundhw
:
2987 if (!(audio_available())) {
2988 printf("Option %s not supported for this target\n", popt
->name
);
2991 select_soundhw (optarg
);
2996 case QEMU_OPTION_version
:
3000 case QEMU_OPTION_m
: {
3004 value
= strtoul(optarg
, &ptr
, 10);
3006 case 0: case 'M': case 'm':
3013 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3017 /* On 32-bit hosts, QEMU is limited by virtual address space */
3018 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3019 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3022 if (value
!= (uint64_t)(ram_addr_t
)value
) {
3023 fprintf(stderr
, "qemu: ram size too large\n");
3029 case QEMU_OPTION_mempath
:
3033 case QEMU_OPTION_mem_prealloc
:
3038 set_cpu_log(optarg
);
3041 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3043 case QEMU_OPTION_gdb
:
3044 gdbstub_dev
= optarg
;
3049 case QEMU_OPTION_bios
:
3052 case QEMU_OPTION_singlestep
:
3059 keyboard_layout
= optarg
;
3061 case QEMU_OPTION_localtime
:
3064 case QEMU_OPTION_vga
:
3065 select_vgahw (optarg
);
3072 w
= strtol(p
, (char **)&p
, 10);
3075 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3081 h
= strtol(p
, (char **)&p
, 10);
3086 depth
= strtol(p
, (char **)&p
, 10);
3087 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3088 depth
!= 24 && depth
!= 32)
3090 } else if (*p
== '\0') {
3091 depth
= graphic_depth
;
3098 graphic_depth
= depth
;
3101 case QEMU_OPTION_echr
:
3104 term_escape_char
= strtol(optarg
, &r
, 0);
3106 printf("Bad argument to echr\n");
3109 case QEMU_OPTION_monitor
:
3110 monitor_parse(optarg
, "readline");
3111 default_monitor
= 0;
3113 case QEMU_OPTION_qmp
:
3114 monitor_parse(optarg
, "control");
3115 default_monitor
= 0;
3117 case QEMU_OPTION_mon
:
3118 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3120 fprintf(stderr
, "parse error: %s\n", optarg
);
3123 default_monitor
= 0;
3125 case QEMU_OPTION_chardev
:
3126 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3128 fprintf(stderr
, "parse error: %s\n", optarg
);
3132 case QEMU_OPTION_serial
:
3133 add_device_config(DEV_SERIAL
, optarg
);
3135 if (strncmp(optarg
, "mon:", 4) == 0) {
3136 default_monitor
= 0;
3139 case QEMU_OPTION_watchdog
:
3142 "qemu: only one watchdog option may be given\n");
3147 case QEMU_OPTION_watchdog_action
:
3148 if (select_watchdog_action(optarg
) == -1) {
3149 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3153 case QEMU_OPTION_virtiocon
:
3154 add_device_config(DEV_VIRTCON
, optarg
);
3155 default_virtcon
= 0;
3156 if (strncmp(optarg
, "mon:", 4) == 0) {
3157 default_monitor
= 0;
3160 case QEMU_OPTION_parallel
:
3161 add_device_config(DEV_PARALLEL
, optarg
);
3162 default_parallel
= 0;
3163 if (strncmp(optarg
, "mon:", 4) == 0) {
3164 default_monitor
= 0;
3167 case QEMU_OPTION_debugcon
:
3168 add_device_config(DEV_DEBUGCON
, optarg
);
3170 case QEMU_OPTION_loadvm
:
3173 case QEMU_OPTION_full_screen
:
3177 case QEMU_OPTION_no_frame
:
3180 case QEMU_OPTION_alt_grab
:
3183 case QEMU_OPTION_ctrl_grab
:
3186 case QEMU_OPTION_no_quit
:
3189 case QEMU_OPTION_sdl
:
3190 display_type
= DT_SDL
;
3193 case QEMU_OPTION_pidfile
:
3196 case QEMU_OPTION_win2k_hack
:
3197 win2k_install_hack
= 1;
3199 case QEMU_OPTION_rtc_td_hack
:
3202 case QEMU_OPTION_acpitable
:
3203 do_acpitable_option(optarg
);
3205 case QEMU_OPTION_smbios
:
3206 do_smbios_option(optarg
);
3208 case QEMU_OPTION_enable_kvm
:
3209 if (!(kvm_available())) {
3210 printf("Option %s not supported for this target\n", popt
->name
);
3215 case QEMU_OPTION_usb
:
3218 case QEMU_OPTION_usbdevice
:
3220 add_device_config(DEV_USB
, optarg
);
3222 case QEMU_OPTION_device
:
3223 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3227 case QEMU_OPTION_smp
:
3230 fprintf(stderr
, "Invalid number of CPUs\n");
3233 if (max_cpus
< smp_cpus
) {
3234 fprintf(stderr
, "maxcpus must be equal to or greater than "
3238 if (max_cpus
> 255) {
3239 fprintf(stderr
, "Unsupported number of maxcpus\n");
3243 case QEMU_OPTION_vnc
:
3244 display_type
= DT_VNC
;
3245 vnc_display
= optarg
;
3247 case QEMU_OPTION_no_acpi
:
3250 case QEMU_OPTION_no_hpet
:
3253 case QEMU_OPTION_balloon
:
3254 if (balloon_parse(optarg
) < 0) {
3255 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3259 case QEMU_OPTION_no_reboot
:
3262 case QEMU_OPTION_no_shutdown
:
3265 case QEMU_OPTION_show_cursor
:
3268 case QEMU_OPTION_uuid
:
3269 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3270 fprintf(stderr
, "Fail to parse UUID string."
3271 " Wrong format.\n");
3276 case QEMU_OPTION_daemonize
:
3280 case QEMU_OPTION_option_rom
:
3281 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3282 fprintf(stderr
, "Too many option ROMs\n");
3285 option_rom
[nb_option_roms
] = optarg
;
3288 case QEMU_OPTION_semihosting
:
3289 semihosting_enabled
= 1;
3291 case QEMU_OPTION_name
:
3292 qemu_name
= qemu_strdup(optarg
);
3294 char *p
= strchr(qemu_name
, ',');
3297 if (strncmp(p
, "process=", 8)) {
3298 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3306 case QEMU_OPTION_prom_env
:
3307 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3308 fprintf(stderr
, "Too many prom variables\n");
3311 prom_envs
[nb_prom_envs
] = optarg
;
3314 case QEMU_OPTION_old_param
:
3317 case QEMU_OPTION_clock
:
3318 configure_alarms(optarg
);
3320 case QEMU_OPTION_startdate
:
3321 configure_rtc_date_offset(optarg
, 1);
3323 case QEMU_OPTION_rtc
:
3324 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3326 fprintf(stderr
, "parse error: %s\n", optarg
);
3329 configure_rtc(opts
);
3331 case QEMU_OPTION_tb_size
:
3332 tb_size
= strtol(optarg
, NULL
, 0);
3336 case QEMU_OPTION_icount
:
3337 icount_option
= optarg
;
3339 case QEMU_OPTION_incoming
:
3342 case QEMU_OPTION_nodefaults
:
3344 default_parallel
= 0;
3345 default_virtcon
= 0;
3346 default_monitor
= 0;
3354 case QEMU_OPTION_chroot
:
3355 chroot_dir
= optarg
;
3357 case QEMU_OPTION_runas
:
3361 case QEMU_OPTION_xen_domid
:
3362 if (!(xen_available())) {
3363 printf("Option %s not supported for this target\n", popt
->name
);
3366 xen_domid
= atoi(optarg
);
3368 case QEMU_OPTION_xen_create
:
3369 if (!(xen_available())) {
3370 printf("Option %s not supported for this target\n", popt
->name
);
3373 xen_mode
= XEN_CREATE
;
3375 case QEMU_OPTION_xen_attach
:
3376 if (!(xen_available())) {
3377 printf("Option %s not supported for this target\n", popt
->name
);
3380 xen_mode
= XEN_ATTACH
;
3382 case QEMU_OPTION_readconfig
:
3385 fp
= fopen(optarg
, "r");
3387 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3390 if (qemu_config_parse(fp
, optarg
) != 0) {
3396 case QEMU_OPTION_writeconfig
:
3399 if (strcmp(optarg
, "-") == 0) {
3402 fp
= fopen(optarg
, "w");
3404 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3408 qemu_config_write(fp
);
3417 /* If no data_dir is specified then try to find it relative to the
3420 data_dir
= find_datadir(argv
[0]);
3422 /* If all else fails use the install patch specified when building. */
3424 data_dir
= CONFIG_QEMU_SHAREDIR
;
3428 * Default to max_cpus = smp_cpus, in case the user doesn't
3429 * specify a max_cpus value.
3432 max_cpus
= smp_cpus
;
3434 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3435 if (smp_cpus
> machine
->max_cpus
) {
3436 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3437 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3442 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3443 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3445 if (machine
->no_serial
) {
3448 if (machine
->no_parallel
) {
3449 default_parallel
= 0;
3451 if (!machine
->use_virtcon
) {
3452 default_virtcon
= 0;
3454 if (machine
->no_vga
) {
3457 if (machine
->no_floppy
) {
3460 if (machine
->no_cdrom
) {
3463 if (machine
->no_sdcard
) {
3467 if (display_type
== DT_NOGRAPHIC
) {
3468 if (default_parallel
)
3469 add_device_config(DEV_PARALLEL
, "null");
3470 if (default_serial
&& default_monitor
) {
3471 add_device_config(DEV_SERIAL
, "mon:stdio");
3472 } else if (default_virtcon
&& default_monitor
) {
3473 add_device_config(DEV_VIRTCON
, "mon:stdio");
3476 add_device_config(DEV_SERIAL
, "stdio");
3477 if (default_virtcon
)
3478 add_device_config(DEV_VIRTCON
, "stdio");
3479 if (default_monitor
)
3480 monitor_parse("stdio", "readline");
3484 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3485 if (default_parallel
)
3486 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3487 if (default_monitor
)
3488 monitor_parse("vc:80Cx24C", "readline");
3489 if (default_virtcon
)
3490 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3493 vga_interface_type
= VGA_CIRRUS
;
3495 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3502 if (pipe(fds
) == -1)
3513 len
= read(fds
[0], &status
, 1);
3514 if (len
== -1 && (errno
== EINTR
))
3519 else if (status
== 1) {
3520 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3528 qemu_set_cloexec(fds
[1]);
3540 signal(SIGTSTP
, SIG_IGN
);
3541 signal(SIGTTOU
, SIG_IGN
);
3542 signal(SIGTTIN
, SIG_IGN
);
3546 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3550 if (write(fds
[1], &status
, 1) != 1) {
3551 perror("daemonize. Writing to pipe\n");
3555 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3559 if (kvm_enabled()) {
3562 ret
= kvm_init(smp_cpus
);
3564 fprintf(stderr
, "failed to initialize KVM\n");
3569 if (qemu_init_main_loop()) {
3570 fprintf(stderr
, "qemu_init_main_loop failed\n");
3573 linux_boot
= (kernel_filename
!= NULL
);
3575 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3576 fprintf(stderr
, "-append only allowed with -kernel option\n");
3580 if (!linux_boot
&& initrd_filename
!= NULL
) {
3581 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3586 /* Win32 doesn't support line-buffering and requires size >= 2 */
3587 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3590 if (init_timer_alarm() < 0) {
3591 fprintf(stderr
, "could not initialize alarm timer\n");
3594 configure_icount(icount_option
);
3598 if (net_init_clients() < 0) {
3602 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
3603 net_set_boot_mask(net_boot
);
3605 /* init the bluetooth world */
3606 if (foreach_device_config(DEV_BT
, bt_parse
))
3609 /* init the memory */
3611 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3613 /* init the dynamic translator */
3614 cpu_exec_init_all(tb_size
* 1024 * 1024);
3616 bdrv_init_with_whitelist();
3620 if (default_cdrom
) {
3621 /* we always create the cdrom drive, even if no disk is there */
3622 drive_add(NULL
, CDROM_ALIAS
);
3625 if (default_floppy
) {
3626 /* we always create at least one floppy */
3627 drive_add(NULL
, FD_ALIAS
, 0);
3630 if (default_sdcard
) {
3631 /* we always create one sd slot, even if no card is in it */
3632 drive_add(NULL
, SD_ALIAS
);
3635 /* open the virtual block devices */
3637 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3638 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3641 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3644 if (nb_numa_nodes
> 0) {
3647 if (nb_numa_nodes
> smp_cpus
) {
3648 nb_numa_nodes
= smp_cpus
;
3651 /* If no memory size if given for any node, assume the default case
3652 * and distribute the available memory equally across all nodes
3654 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3655 if (node_mem
[i
] != 0)
3658 if (i
== nb_numa_nodes
) {
3659 uint64_t usedmem
= 0;
3661 /* On Linux, the each node's border has to be 8MB aligned,
3662 * the final node gets the rest.
3664 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3665 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3666 usedmem
+= node_mem
[i
];
3668 node_mem
[i
] = ram_size
- usedmem
;
3671 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3672 if (node_cpumask
[i
] != 0)
3675 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3676 * must cope with this anyway, because there are BIOSes out there in
3677 * real machines which also use this scheme.
3679 if (i
== nb_numa_nodes
) {
3680 for (i
= 0; i
< smp_cpus
; i
++) {
3681 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3686 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3688 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3690 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3692 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3695 module_call_init(MODULE_INIT_DEVICE
);
3697 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3701 i
= select_watchdog(watchdog
);
3703 exit (i
== 1 ? 1 : 0);
3706 if (machine
->compat_props
) {
3707 qdev_prop_register_global_list(machine
->compat_props
);
3711 machine
->init(ram_size
, boot_devices
,
3712 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3714 cpu_synchronize_all_post_init();
3717 /* must be after terminal init, SDL library changes signal handlers */
3723 current_machine
= machine
;
3725 /* init USB devices */
3727 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3731 /* init generic devices */
3732 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3735 net_check_clients();
3737 /* just use the first displaystate for the moment */
3738 ds
= get_displaystate();
3740 if (display_type
== DT_DEFAULT
) {
3741 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3742 display_type
= DT_SDL
;
3744 display_type
= DT_VNC
;
3745 vnc_display
= "localhost:0,to=99";
3751 switch (display_type
) {
3754 #if defined(CONFIG_CURSES)
3756 curses_display_init(ds
, full_screen
);
3759 #if defined(CONFIG_SDL)
3761 sdl_display_init(ds
, full_screen
, no_frame
);
3763 #elif defined(CONFIG_COCOA)
3765 cocoa_display_init(ds
, full_screen
);
3769 vnc_display_init(ds
);
3770 if (vnc_display_open(ds
, vnc_display
) < 0)
3773 if (show_vnc_port
) {
3774 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3782 dcl
= ds
->listeners
;
3783 while (dcl
!= NULL
) {
3784 if (dcl
->dpy_refresh
!= NULL
) {
3785 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3786 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3791 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3792 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3793 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3796 text_consoles_set_display(ds
);
3798 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3801 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3802 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3807 qdev_machine_creation_done();
3809 if (rom_load_all() != 0) {
3810 fprintf(stderr
, "rom loading failed\n");
3814 qemu_system_reset();
3816 if (load_vmstate(loadvm
) < 0) {
3822 qemu_start_incoming_migration(incoming
);
3823 } else if (autostart
) {
3833 len
= write(fds
[1], &status
, 1);
3834 if (len
== -1 && (errno
== EINTR
))
3841 perror("not able to chdir to /");
3844 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3850 pwd
= getpwnam(run_as
);
3852 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3858 if (chroot(chroot_dir
) < 0) {
3859 fprintf(stderr
, "chroot failed\n");
3863 perror("not able to chdir to /");
3869 if (setgid(pwd
->pw_gid
) < 0) {
3870 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3873 if (setuid(pwd
->pw_uid
) < 0) {
3874 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3877 if (setuid(0) != -1) {
3878 fprintf(stderr
, "Dropping privileges failed\n");