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"
149 #include "qemu-option.h"
150 #include "qemu-config.h"
151 #include "qemu-objects.h"
155 #include "qemu_socket.h"
157 #include "slirp/libslirp.h"
159 #include "qemu-queue.h"
161 #include "arch_init.h"
164 //#define DEBUG_SLIRP
166 #define DEFAULT_RAM_SIZE 128
168 #define MAX_VIRTIO_CONSOLES 1
170 static const char *data_dir
;
171 const char *bios_name
= NULL
;
172 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
173 to store the VM snapshots */
174 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
175 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
176 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
177 DisplayType display_type
= DT_DEFAULT
;
178 const char* keyboard_layout
= NULL
;
180 const char *mem_path
= NULL
;
182 int mem_prealloc
= 0; /* force preallocation of physical target memory */
185 NICInfo nd_table
[MAX_NICS
];
188 static int rtc_utc
= 1;
189 static int rtc_date_offset
= -1; /* -1 means no change */
190 QEMUClock
*rtc_clock
;
191 int vga_interface_type
= VGA_NONE
;
192 static int full_screen
= 0;
194 static int no_frame
= 0;
197 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
198 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
199 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
200 int win2k_install_hack
= 0;
208 const char *vnc_display
;
209 int acpi_enabled
= 1;
215 int graphic_rotate
= 0;
216 uint8_t irq0override
= 1;
220 const char *watchdog
;
221 const char *option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
225 const char *qemu_name
;
228 unsigned int nb_prom_envs
= 0;
229 const char *prom_envs
[MAX_PROM_ENVS
];
233 uint64_t node_mem
[MAX_NODES
];
234 uint64_t node_cpumask
[MAX_NODES
];
236 static QEMUTimer
*nographic_timer
;
238 uint8_t qemu_uuid
[16];
240 static QEMUBootSetHandler
*boot_set_handler
;
241 static void *boot_set_opaque
;
245 enum xen_mode xen_mode
= XEN_EMULATE
;
248 #define SIG_IPI (SIGRTMIN+4)
250 #define SIG_IPI SIGUSR1
253 static int default_serial
= 1;
254 static int default_parallel
= 1;
255 static int default_virtcon
= 1;
256 static int default_monitor
= 1;
257 static int default_vga
= 1;
258 static int default_floppy
= 1;
259 static int default_cdrom
= 1;
260 static int default_sdcard
= 1;
266 { .driver
= "isa-serial", .flag
= &default_serial
},
267 { .driver
= "isa-parallel", .flag
= &default_parallel
},
268 { .driver
= "isa-fdc", .flag
= &default_floppy
},
269 { .driver
= "ide-drive", .flag
= &default_cdrom
},
270 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
271 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
272 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
273 { .driver
= "VGA", .flag
= &default_vga
},
274 { .driver
= "cirrus-vga", .flag
= &default_vga
},
275 { .driver
= "vmware-svga", .flag
= &default_vga
},
278 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
280 const char *driver
= qemu_opt_get(opts
, "driver");
285 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
286 if (strcmp(default_list
[i
].driver
, driver
) != 0)
288 *(default_list
[i
].flag
) = 0;
293 /***********************************************************/
295 static void set_proc_name(const char *s
)
297 #if defined(__linux__) && defined(PR_SET_NAME)
301 name
[sizeof(name
) - 1] = 0;
302 strncpy(name
, s
, sizeof(name
));
303 /* Could rewrite argv[0] too, but that's a bit more complicated.
304 This simple way is enough for `top'. */
305 prctl(PR_SET_NAME
, name
);
309 /***********************************************************/
310 /* real time host monotonic timer */
312 /* compute with 96 bit intermediate result: (a*b)/c */
313 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
318 #ifdef HOST_WORDS_BIGENDIAN
328 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
329 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
332 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
336 /***********************************************************/
337 /* host time/date access */
338 void qemu_get_timedate(struct tm
*tm
, int offset
)
345 if (rtc_date_offset
== -1) {
349 ret
= localtime(&ti
);
351 ti
-= rtc_date_offset
;
355 memcpy(tm
, ret
, sizeof(struct tm
));
358 int qemu_timedate_diff(struct tm
*tm
)
362 if (rtc_date_offset
== -1)
364 seconds
= mktimegm(tm
);
366 seconds
= mktime(tm
);
368 seconds
= mktimegm(tm
) + rtc_date_offset
;
370 return seconds
- time(NULL
);
373 void rtc_change_mon_event(struct tm
*tm
)
377 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
378 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
379 qobject_decref(data
);
382 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
384 time_t rtc_start_date
;
387 if (!strcmp(startdate
, "now") && legacy
) {
388 rtc_date_offset
= -1;
390 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
398 } else if (sscanf(startdate
, "%d-%d-%d",
410 rtc_start_date
= mktimegm(&tm
);
411 if (rtc_start_date
== -1) {
413 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
414 "'2006-06-17T16:01:21' or '2006-06-17'\n");
417 rtc_date_offset
= time(NULL
) - rtc_start_date
;
421 static void configure_rtc(QemuOpts
*opts
)
425 value
= qemu_opt_get(opts
, "base");
427 if (!strcmp(value
, "utc")) {
429 } else if (!strcmp(value
, "localtime")) {
432 configure_rtc_date_offset(value
, 0);
435 value
= qemu_opt_get(opts
, "clock");
437 if (!strcmp(value
, "host")) {
438 rtc_clock
= host_clock
;
439 } else if (!strcmp(value
, "vm")) {
440 rtc_clock
= vm_clock
;
442 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
446 value
= qemu_opt_get(opts
, "driftfix");
448 if (!strcmp(value
, "slew")) {
450 } else if (!strcmp(value
, "none")) {
453 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
459 /***********************************************************/
460 /* Bluetooth support */
463 static struct HCIInfo
*hci_table
[MAX_NICS
];
465 static struct bt_vlan_s
{
466 struct bt_scatternet_s net
;
468 struct bt_vlan_s
*next
;
471 /* find or alloc a new bluetooth "VLAN" */
472 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
474 struct bt_vlan_s
**pvlan
, *vlan
;
475 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
479 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
481 pvlan
= &first_bt_vlan
;
482 while (*pvlan
!= NULL
)
483 pvlan
= &(*pvlan
)->next
;
488 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
492 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
497 static struct HCIInfo null_hci
= {
498 .cmd_send
= null_hci_send
,
499 .sco_send
= null_hci_send
,
500 .acl_send
= null_hci_send
,
501 .bdaddr_set
= null_hci_addr_set
,
504 struct HCIInfo
*qemu_next_hci(void)
506 if (cur_hci
== nb_hcis
)
509 return hci_table
[cur_hci
++];
512 static struct HCIInfo
*hci_init(const char *str
)
515 struct bt_scatternet_s
*vlan
= 0;
517 if (!strcmp(str
, "null"))
520 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
522 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
523 else if (!strncmp(str
, "hci", 3)) {
526 if (!strncmp(str
+ 3, ",vlan=", 6)) {
527 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
532 vlan
= qemu_find_bt_vlan(0);
534 return bt_new_hci(vlan
);
537 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
542 static int bt_hci_parse(const char *str
)
547 if (nb_hcis
>= MAX_NICS
) {
548 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
561 bdaddr
.b
[5] = 0x56 + nb_hcis
;
562 hci
->bdaddr_set(hci
, bdaddr
.b
);
564 hci_table
[nb_hcis
++] = hci
;
569 static void bt_vhci_add(int vlan_id
)
571 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
574 fprintf(stderr
, "qemu: warning: adding a VHCI to "
575 "an empty scatternet %i\n", vlan_id
);
577 bt_vhci_init(bt_new_hci(vlan
));
580 static struct bt_device_s
*bt_device_add(const char *opt
)
582 struct bt_scatternet_s
*vlan
;
584 char *endp
= strstr(opt
, ",vlan=");
585 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
588 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
591 vlan_id
= strtol(endp
+ 6, &endp
, 0);
593 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
598 vlan
= qemu_find_bt_vlan(vlan_id
);
601 fprintf(stderr
, "qemu: warning: adding a slave device to "
602 "an empty scatternet %i\n", vlan_id
);
604 if (!strcmp(devname
, "keyboard"))
605 return bt_keyboard_init(vlan
);
607 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
611 static int bt_parse(const char *opt
)
613 const char *endp
, *p
;
616 if (strstart(opt
, "hci", &endp
)) {
617 if (!*endp
|| *endp
== ',') {
619 if (!strstart(endp
, ",vlan=", 0))
622 return bt_hci_parse(opt
);
624 } else if (strstart(opt
, "vhci", &endp
)) {
625 if (!*endp
|| *endp
== ',') {
627 if (strstart(endp
, ",vlan=", &p
)) {
628 vlan
= strtol(p
, (char **) &endp
, 0);
630 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
634 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
643 } else if (strstart(opt
, "device:", &endp
))
644 return !bt_device_add(endp
);
646 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
650 /***********************************************************/
651 /* QEMU Block devices */
653 #define HD_ALIAS "index=%d,media=disk"
654 #define CDROM_ALIAS "index=2,media=cdrom"
655 #define FD_ALIAS "index=%d,if=floppy"
656 #define PFLASH_ALIAS "if=pflash"
657 #define MTD_ALIAS "if=mtd"
658 #define SD_ALIAS "index=0,if=sd"
660 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
667 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
670 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
672 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
673 __FUNCTION__
, optstr
);
677 qemu_opt_set(opts
, "file", file
);
681 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
685 /* seek interface, bus and unit */
687 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
688 if (dinfo
->type
== type
&&
697 DriveInfo
*drive_get_by_id(const char *id
)
701 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
702 if (strcmp(id
, dinfo
->id
))
709 int drive_get_max_bus(BlockInterfaceType type
)
715 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
716 if(dinfo
->type
== type
&&
717 dinfo
->bus
> max_bus
)
718 max_bus
= dinfo
->bus
;
723 const char *drive_get_serial(BlockDriverState
*bdrv
)
727 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
728 if (dinfo
->bdrv
== bdrv
)
729 return dinfo
->serial
;
735 BlockInterfaceErrorAction
drive_get_on_error(
736 BlockDriverState
*bdrv
, int is_read
)
740 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
741 if (dinfo
->bdrv
== bdrv
)
742 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
745 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
748 static void bdrv_format_print(void *opaque
, const char *name
)
750 fprintf(stderr
, " %s", name
);
753 void drive_uninit(DriveInfo
*dinfo
)
755 qemu_opts_del(dinfo
->opts
);
756 bdrv_delete(dinfo
->bdrv
);
757 QTAILQ_REMOVE(&drives
, dinfo
, next
);
761 static int parse_block_error_action(const char *buf
, int is_read
)
763 if (!strcmp(buf
, "ignore")) {
764 return BLOCK_ERR_IGNORE
;
765 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
766 return BLOCK_ERR_STOP_ENOSPC
;
767 } else if (!strcmp(buf
, "stop")) {
768 return BLOCK_ERR_STOP_ANY
;
769 } else if (!strcmp(buf
, "report")) {
770 return BLOCK_ERR_REPORT
;
772 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
773 buf
, is_read
? "read" : "write");
778 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
782 const char *file
= NULL
;
785 const char *mediastr
= "";
786 BlockInterfaceType type
;
787 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
789 int cyls
, heads
, secs
, translation
;
790 BlockDriver
*drv
= NULL
;
791 QEMUMachine
*machine
= opaque
;
798 int on_read_error
, on_write_error
;
805 translation
= BIOS_ATA_TRANSLATION_AUTO
;
808 if (machine
&& machine
->use_scsi
) {
810 max_devs
= MAX_SCSI_DEVS
;
811 pstrcpy(devname
, sizeof(devname
), "scsi");
814 max_devs
= MAX_IDE_DEVS
;
815 pstrcpy(devname
, sizeof(devname
), "ide");
819 /* extract parameters */
820 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
821 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
822 index
= qemu_opt_get_number(opts
, "index", -1);
824 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
825 heads
= qemu_opt_get_number(opts
, "heads", 0);
826 secs
= qemu_opt_get_number(opts
, "secs", 0);
828 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
829 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
831 file
= qemu_opt_get(opts
, "file");
832 serial
= qemu_opt_get(opts
, "serial");
834 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
835 pstrcpy(devname
, sizeof(devname
), buf
);
836 if (!strcmp(buf
, "ide")) {
838 max_devs
= MAX_IDE_DEVS
;
839 } else if (!strcmp(buf
, "scsi")) {
841 max_devs
= MAX_SCSI_DEVS
;
842 } else if (!strcmp(buf
, "floppy")) {
845 } else if (!strcmp(buf
, "pflash")) {
848 } else if (!strcmp(buf
, "mtd")) {
851 } else if (!strcmp(buf
, "sd")) {
854 } else if (!strcmp(buf
, "virtio")) {
857 } else if (!strcmp(buf
, "xen")) {
860 } else if (!strcmp(buf
, "none")) {
864 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
869 if (cyls
|| heads
|| secs
) {
870 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
871 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
874 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
875 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
878 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
879 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
884 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
887 "qemu: '%s' trans must be used with cyls,heads and secs\n",
891 if (!strcmp(buf
, "none"))
892 translation
= BIOS_ATA_TRANSLATION_NONE
;
893 else if (!strcmp(buf
, "lba"))
894 translation
= BIOS_ATA_TRANSLATION_LBA
;
895 else if (!strcmp(buf
, "auto"))
896 translation
= BIOS_ATA_TRANSLATION_AUTO
;
898 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
903 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
904 if (!strcmp(buf
, "disk")) {
906 } else if (!strcmp(buf
, "cdrom")) {
907 if (cyls
|| secs
|| heads
) {
909 "qemu: '%s' invalid physical CHS format\n", buf
);
914 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
919 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
920 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
922 else if (!strcmp(buf
, "writethrough"))
924 else if (!strcmp(buf
, "writeback"))
927 fprintf(stderr
, "qemu: invalid cache option\n");
932 #ifdef CONFIG_LINUX_AIO
933 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
934 if (!strcmp(buf
, "threads"))
936 else if (!strcmp(buf
, "native"))
939 fprintf(stderr
, "qemu: invalid aio option\n");
945 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
946 if (strcmp(buf
, "?") == 0) {
947 fprintf(stderr
, "qemu: Supported formats:");
948 bdrv_iterate_format(bdrv_format_print
, NULL
);
949 fprintf(stderr
, "\n");
952 drv
= bdrv_find_whitelisted_format(buf
);
954 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
959 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
960 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
961 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
962 fprintf(stderr
, "werror is no supported by this format\n");
966 on_write_error
= parse_block_error_action(buf
, 0);
967 if (on_write_error
< 0) {
972 on_read_error
= BLOCK_ERR_REPORT
;
973 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
974 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
975 fprintf(stderr
, "rerror is no supported by this format\n");
979 on_read_error
= parse_block_error_action(buf
, 1);
980 if (on_read_error
< 0) {
985 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
986 if (type
!= IF_VIRTIO
) {
987 fprintf(stderr
, "addr is not supported\n");
992 /* compute bus and unit according index */
995 if (bus_id
!= 0 || unit_id
!= -1) {
997 "qemu: index cannot be used with bus and unit\n");
1005 unit_id
= index
% max_devs
;
1006 bus_id
= index
/ max_devs
;
1010 /* if user doesn't specify a unit_id,
1011 * try to find the first free
1014 if (unit_id
== -1) {
1016 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1018 if (max_devs
&& unit_id
>= max_devs
) {
1019 unit_id
-= max_devs
;
1027 if (max_devs
&& unit_id
>= max_devs
) {
1028 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1029 unit_id
, max_devs
- 1);
1034 * ignore multiple definitions
1037 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1044 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1045 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1046 dinfo
->id
= qemu_strdup(buf
);
1048 /* no id supplied -> create one */
1049 dinfo
->id
= qemu_mallocz(32);
1050 if (type
== IF_IDE
|| type
== IF_SCSI
)
1051 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1053 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1054 devname
, bus_id
, mediastr
, unit_id
);
1056 snprintf(dinfo
->id
, 32, "%s%s%i",
1057 devname
, mediastr
, unit_id
);
1059 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1060 dinfo
->devaddr
= devaddr
;
1062 dinfo
->bus
= bus_id
;
1063 dinfo
->unit
= unit_id
;
1064 dinfo
->on_read_error
= on_read_error
;
1065 dinfo
->on_write_error
= on_write_error
;
1068 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1069 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1079 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1080 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1084 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1089 /* FIXME: This isn't really a floppy, but it's a reasonable
1092 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1098 /* add virtio block device */
1099 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1100 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1101 qemu_opt_set(opts
, "drive", dinfo
->id
);
1103 qemu_opt_set(opts
, "addr", devaddr
);
1114 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1115 cache
= 2; /* always use write-back with snapshot */
1117 if (cache
== 0) /* no caching */
1118 bdrv_flags
|= BDRV_O_NOCACHE
;
1119 else if (cache
== 2) /* write-back */
1120 bdrv_flags
|= BDRV_O_CACHE_WB
;
1123 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1125 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1128 if (media
== MEDIA_CDROM
) {
1129 /* CDROM is fine for any interface, don't check. */
1131 } else if (ro
== 1) {
1132 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1133 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1138 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1140 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1141 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1142 file
, strerror(errno
));
1146 if (bdrv_key_required(dinfo
->bdrv
))
1152 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1154 QEMUMachine
*machine
= opaque
;
1155 int fatal_error
= 0;
1157 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1164 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1166 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1167 qemu_opt_set(opts
, "snapshot", "on");
1172 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1174 boot_set_handler
= func
;
1175 boot_set_opaque
= opaque
;
1178 int qemu_boot_set(const char *boot_devices
)
1180 if (!boot_set_handler
) {
1183 return boot_set_handler(boot_set_opaque
, boot_devices
);
1186 static int parse_bootdevices(char *devices
)
1188 /* We just do some generic consistency checks */
1192 for (p
= devices
; *p
!= '\0'; p
++) {
1193 /* Allowed boot devices are:
1194 * a-b: floppy disk drives
1195 * c-f: IDE disk drives
1196 * g-m: machine implementation dependant drives
1197 * n-p: network devices
1198 * It's up to each machine implementation to check if the given boot
1199 * devices match the actual hardware implementation and firmware
1202 if (*p
< 'a' || *p
> 'p') {
1203 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1206 if (bitmap
& (1 << (*p
- 'a'))) {
1207 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1210 bitmap
|= 1 << (*p
- 'a');
1215 static void restore_boot_devices(void *opaque
)
1217 char *standard_boot_devices
= opaque
;
1219 qemu_boot_set(standard_boot_devices
);
1221 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1222 qemu_free(standard_boot_devices
);
1225 static void numa_add(const char *optarg
)
1229 unsigned long long value
, endvalue
;
1232 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1233 if (!strcmp(option
, "node")) {
1234 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1235 nodenr
= nb_numa_nodes
;
1237 nodenr
= strtoull(option
, NULL
, 10);
1240 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1241 node_mem
[nodenr
] = 0;
1243 value
= strtoull(option
, &endptr
, 0);
1245 case 0: case 'M': case 'm':
1252 node_mem
[nodenr
] = value
;
1254 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1255 node_cpumask
[nodenr
] = 0;
1257 value
= strtoull(option
, &endptr
, 10);
1260 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1262 if (*endptr
== '-') {
1263 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1264 if (endvalue
>= 63) {
1267 "only 63 CPUs in NUMA mode supported.\n");
1269 value
= (2ULL << endvalue
) - (1ULL << value
);
1271 value
= 1ULL << value
;
1274 node_cpumask
[nodenr
] = value
;
1281 static void smp_parse(const char *optarg
)
1283 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1287 smp
= strtoul(optarg
, &endptr
, 10);
1288 if (endptr
!= optarg
) {
1289 if (*endptr
== ',') {
1293 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1294 sockets
= strtoull(option
, NULL
, 10);
1295 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1296 cores
= strtoull(option
, NULL
, 10);
1297 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1298 threads
= strtoull(option
, NULL
, 10);
1299 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1300 max_cpus
= strtoull(option
, NULL
, 10);
1302 /* compute missing values, prefer sockets over cores over threads */
1303 if (smp
== 0 || sockets
== 0) {
1304 sockets
= sockets
> 0 ? sockets
: 1;
1305 cores
= cores
> 0 ? cores
: 1;
1306 threads
= threads
> 0 ? threads
: 1;
1308 smp
= cores
* threads
* sockets
;
1312 threads
= threads
> 0 ? threads
: 1;
1313 cores
= smp
/ (sockets
* threads
);
1316 threads
= smp
/ (cores
* sockets
);
1321 smp_cores
= cores
> 0 ? cores
: 1;
1322 smp_threads
= threads
> 0 ? threads
: 1;
1324 max_cpus
= smp_cpus
;
1327 /***********************************************************/
1330 static int usb_device_add(const char *devname
, int is_hotplug
)
1333 USBDevice
*dev
= NULL
;
1338 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1339 dev
= usbdevice_create(devname
);
1343 /* the other ones */
1344 if (strstart(devname
, "host:", &p
)) {
1345 dev
= usb_host_device_open(p
);
1346 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1347 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1348 bt_new_hci(qemu_find_bt_vlan(0)));
1359 static int usb_device_del(const char *devname
)
1364 if (strstart(devname
, "host:", &p
))
1365 return usb_host_device_close(p
);
1370 p
= strchr(devname
, '.');
1373 bus_num
= strtoul(devname
, NULL
, 0);
1374 addr
= strtoul(p
+ 1, NULL
, 0);
1376 return usb_device_delete_addr(bus_num
, addr
);
1379 static int usb_parse(const char *cmdline
)
1382 r
= usb_device_add(cmdline
, 0);
1384 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1389 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1391 const char *devname
= qdict_get_str(qdict
, "devname");
1392 if (usb_device_add(devname
, 1) < 0) {
1393 error_report("could not add USB device '%s'", devname
);
1397 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1399 const char *devname
= qdict_get_str(qdict
, "devname");
1400 if (usb_device_del(devname
) < 0) {
1401 error_report("could not delete USB device '%s'", devname
);
1405 /***********************************************************/
1406 /* PCMCIA/Cardbus */
1408 static struct pcmcia_socket_entry_s
{
1409 PCMCIASocket
*socket
;
1410 struct pcmcia_socket_entry_s
*next
;
1411 } *pcmcia_sockets
= 0;
1413 void pcmcia_socket_register(PCMCIASocket
*socket
)
1415 struct pcmcia_socket_entry_s
*entry
;
1417 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1418 entry
->socket
= socket
;
1419 entry
->next
= pcmcia_sockets
;
1420 pcmcia_sockets
= entry
;
1423 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1425 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1427 ptr
= &pcmcia_sockets
;
1428 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1429 if (entry
->socket
== socket
) {
1435 void pcmcia_info(Monitor
*mon
)
1437 struct pcmcia_socket_entry_s
*iter
;
1439 if (!pcmcia_sockets
)
1440 monitor_printf(mon
, "No PCMCIA sockets\n");
1442 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1443 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1444 iter
->socket
->attached
? iter
->socket
->card_string
:
1448 /***********************************************************/
1451 typedef struct IOHandlerRecord
{
1453 IOCanReadHandler
*fd_read_poll
;
1455 IOHandler
*fd_write
;
1458 /* temporary data */
1460 QLIST_ENTRY(IOHandlerRecord
) next
;
1463 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1464 QLIST_HEAD_INITIALIZER(io_handlers
);
1467 /* XXX: fd_read_poll should be suppressed, but an API change is
1468 necessary in the character devices to suppress fd_can_read(). */
1469 int qemu_set_fd_handler2(int fd
,
1470 IOCanReadHandler
*fd_read_poll
,
1472 IOHandler
*fd_write
,
1475 IOHandlerRecord
*ioh
;
1477 if (!fd_read
&& !fd_write
) {
1478 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1479 if (ioh
->fd
== fd
) {
1485 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1489 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1490 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1493 ioh
->fd_read_poll
= fd_read_poll
;
1494 ioh
->fd_read
= fd_read
;
1495 ioh
->fd_write
= fd_write
;
1496 ioh
->opaque
= opaque
;
1502 int qemu_set_fd_handler(int fd
,
1504 IOHandler
*fd_write
,
1507 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1511 /***********************************************************/
1512 /* Polling handling */
1514 typedef struct PollingEntry
{
1517 struct PollingEntry
*next
;
1520 static PollingEntry
*first_polling_entry
;
1522 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1524 PollingEntry
**ppe
, *pe
;
1525 pe
= qemu_mallocz(sizeof(PollingEntry
));
1527 pe
->opaque
= opaque
;
1528 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1533 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1535 PollingEntry
**ppe
, *pe
;
1536 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1538 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1546 /***********************************************************/
1547 /* Wait objects support */
1548 typedef struct WaitObjects
{
1550 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1551 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1552 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1555 static WaitObjects wait_objects
= {0};
1557 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1559 WaitObjects
*w
= &wait_objects
;
1561 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1563 w
->events
[w
->num
] = handle
;
1564 w
->func
[w
->num
] = func
;
1565 w
->opaque
[w
->num
] = opaque
;
1570 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1573 WaitObjects
*w
= &wait_objects
;
1576 for (i
= 0; i
< w
->num
; i
++) {
1577 if (w
->events
[i
] == handle
)
1580 w
->events
[i
] = w
->events
[i
+ 1];
1581 w
->func
[i
] = w
->func
[i
+ 1];
1582 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1590 /***********************************************************/
1591 /* machine registration */
1593 static QEMUMachine
*first_machine
= NULL
;
1594 QEMUMachine
*current_machine
= NULL
;
1596 int qemu_register_machine(QEMUMachine
*m
)
1599 pm
= &first_machine
;
1607 static QEMUMachine
*find_machine(const char *name
)
1611 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1612 if (!strcmp(m
->name
, name
))
1614 if (m
->alias
&& !strcmp(m
->alias
, name
))
1620 static QEMUMachine
*find_default_machine(void)
1624 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1625 if (m
->is_default
) {
1632 /***********************************************************/
1633 /* main execution loop */
1635 static void gui_update(void *opaque
)
1637 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1638 DisplayState
*ds
= opaque
;
1639 DisplayChangeListener
*dcl
= ds
->listeners
;
1641 qemu_flush_coalesced_mmio_buffer();
1644 while (dcl
!= NULL
) {
1645 if (dcl
->gui_timer_interval
&&
1646 dcl
->gui_timer_interval
< interval
)
1647 interval
= dcl
->gui_timer_interval
;
1650 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1653 static void nographic_update(void *opaque
)
1655 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1657 qemu_flush_coalesced_mmio_buffer();
1658 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1661 struct vm_change_state_entry
{
1662 VMChangeStateHandler
*cb
;
1664 QLIST_ENTRY (vm_change_state_entry
) entries
;
1667 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1669 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1672 VMChangeStateEntry
*e
;
1674 e
= qemu_mallocz(sizeof (*e
));
1678 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1682 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1684 QLIST_REMOVE (e
, entries
);
1688 void vm_state_notify(int running
, int reason
)
1690 VMChangeStateEntry
*e
;
1692 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1693 e
->cb(e
->opaque
, running
, reason
);
1702 vm_state_notify(1, 0);
1707 /* reset/shutdown handler */
1709 typedef struct QEMUResetEntry
{
1710 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1711 QEMUResetHandler
*func
;
1715 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1716 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1717 static int reset_requested
;
1718 static int shutdown_requested
;
1719 static int powerdown_requested
;
1720 int debug_requested
;
1721 static int vmstop_requested
;
1723 int qemu_shutdown_requested(void)
1725 int r
= shutdown_requested
;
1726 shutdown_requested
= 0;
1730 int qemu_reset_requested(void)
1732 int r
= reset_requested
;
1733 reset_requested
= 0;
1737 int qemu_powerdown_requested(void)
1739 int r
= powerdown_requested
;
1740 powerdown_requested
= 0;
1744 static int qemu_debug_requested(void)
1746 int r
= debug_requested
;
1747 debug_requested
= 0;
1751 static int qemu_vmstop_requested(void)
1753 int r
= vmstop_requested
;
1754 vmstop_requested
= 0;
1758 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1760 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1763 re
->opaque
= opaque
;
1764 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1767 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1771 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1772 if (re
->func
== func
&& re
->opaque
== opaque
) {
1773 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1780 void qemu_system_reset(void)
1782 QEMUResetEntry
*re
, *nre
;
1784 /* reset all devices */
1785 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1786 re
->func(re
->opaque
);
1788 monitor_protocol_event(QEVENT_RESET
, NULL
);
1789 cpu_synchronize_all_post_reset();
1792 void qemu_system_reset_request(void)
1795 shutdown_requested
= 1;
1797 reset_requested
= 1;
1799 qemu_notify_event();
1802 void qemu_system_shutdown_request(void)
1804 shutdown_requested
= 1;
1805 qemu_notify_event();
1808 void qemu_system_powerdown_request(void)
1810 powerdown_requested
= 1;
1811 qemu_notify_event();
1815 static void host_main_loop_wait(int *timeout
)
1821 /* XXX: need to suppress polling by better using win32 events */
1823 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1824 ret
|= pe
->func(pe
->opaque
);
1828 WaitObjects
*w
= &wait_objects
;
1830 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1831 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1832 if (w
->func
[ret
- WAIT_OBJECT_0
])
1833 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1835 /* Check for additional signaled events */
1836 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1838 /* Check if event is signaled */
1839 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1840 if(ret2
== WAIT_OBJECT_0
) {
1842 w
->func
[i
](w
->opaque
[i
]);
1843 } else if (ret2
== WAIT_TIMEOUT
) {
1845 err
= GetLastError();
1846 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1849 } else if (ret
== WAIT_TIMEOUT
) {
1851 err
= GetLastError();
1852 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1859 static void host_main_loop_wait(int *timeout
)
1864 void main_loop_wait(int nonblocking
)
1866 IOHandlerRecord
*ioh
;
1867 fd_set rfds
, wfds
, xfds
;
1875 timeout
= qemu_calculate_timeout();
1876 qemu_bh_update_timeout(&timeout
);
1879 host_main_loop_wait(&timeout
);
1881 /* poll any events */
1882 /* XXX: separate device handlers from system ones */
1887 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1891 (!ioh
->fd_read_poll
||
1892 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1893 FD_SET(ioh
->fd
, &rfds
);
1897 if (ioh
->fd_write
) {
1898 FD_SET(ioh
->fd
, &wfds
);
1904 tv
.tv_sec
= timeout
/ 1000;
1905 tv
.tv_usec
= (timeout
% 1000) * 1000;
1907 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1909 qemu_mutex_unlock_iothread();
1910 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1911 qemu_mutex_lock_iothread();
1913 IOHandlerRecord
*pioh
;
1915 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1917 QLIST_REMOVE(ioh
, next
);
1921 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1922 ioh
->fd_read(ioh
->opaque
);
1924 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1925 ioh
->fd_write(ioh
->opaque
);
1930 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1932 qemu_run_all_timers();
1934 /* Check bottom-halves last in case any of the earlier events triggered
1940 static int vm_can_run(void)
1942 if (powerdown_requested
)
1944 if (reset_requested
)
1946 if (shutdown_requested
)
1948 if (debug_requested
)
1953 qemu_irq qemu_system_powerdown
;
1955 static void main_loop(void)
1959 #ifdef CONFIG_IOTHREAD
1960 qemu_system_ready
= 1;
1961 qemu_cond_broadcast(&qemu_system_cond
);
1966 bool nonblocking
= false;
1967 #ifdef CONFIG_PROFILER
1970 #ifndef CONFIG_IOTHREAD
1971 nonblocking
= tcg_cpu_exec();
1973 #ifdef CONFIG_PROFILER
1974 ti
= profile_getclock();
1976 main_loop_wait(nonblocking
);
1977 #ifdef CONFIG_PROFILER
1978 dev_time
+= profile_getclock() - ti
;
1980 } while (vm_can_run());
1982 if ((r
= qemu_debug_requested())) {
1985 if (qemu_shutdown_requested()) {
1986 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1993 if (qemu_reset_requested()) {
1995 qemu_system_reset();
1998 if (qemu_powerdown_requested()) {
1999 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2000 qemu_irq_raise(qemu_system_powerdown
);
2002 if ((r
= qemu_vmstop_requested())) {
2009 static void version(void)
2011 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2014 static void help(int exitcode
)
2016 const char *options_help
=
2017 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2019 #define DEFHEADING(text) stringify(text) "\n"
2020 #include "qemu-options.h"
2026 printf("usage: %s [options] [disk_image]\n"
2028 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2031 "During emulation, the following keys are useful:\n"
2032 "ctrl-alt-f toggle full screen\n"
2033 "ctrl-alt-n switch to virtual console 'n'\n"
2034 "ctrl-alt toggle mouse and keyboard grab\n"
2036 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2042 #define HAS_ARG 0x0001
2045 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2047 #define DEFHEADING(text)
2048 #include "qemu-options.h"
2054 typedef struct QEMUOption
{
2061 static const QEMUOption qemu_options
[] = {
2062 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2063 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2064 { option, opt_arg, opt_enum, arch_mask },
2065 #define DEFHEADING(text)
2066 #include "qemu-options.h"
2072 static void select_vgahw (const char *p
)
2077 vga_interface_type
= VGA_NONE
;
2078 if (strstart(p
, "std", &opts
)) {
2079 vga_interface_type
= VGA_STD
;
2080 } else if (strstart(p
, "cirrus", &opts
)) {
2081 vga_interface_type
= VGA_CIRRUS
;
2082 } else if (strstart(p
, "vmware", &opts
)) {
2083 vga_interface_type
= VGA_VMWARE
;
2084 } else if (strstart(p
, "xenfb", &opts
)) {
2085 vga_interface_type
= VGA_XENFB
;
2086 } else if (!strstart(p
, "none", &opts
)) {
2088 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2092 const char *nextopt
;
2094 if (strstart(opts
, ",retrace=", &nextopt
)) {
2096 if (strstart(opts
, "dumb", &nextopt
))
2097 vga_retrace_method
= VGA_RETRACE_DUMB
;
2098 else if (strstart(opts
, "precise", &nextopt
))
2099 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2100 else goto invalid_vga
;
2101 } else goto invalid_vga
;
2106 static int balloon_parse(const char *arg
)
2110 if (strcmp(arg
, "none") == 0) {
2114 if (!strncmp(arg
, "virtio", 6)) {
2115 if (arg
[6] == ',') {
2116 /* have params -> parse them */
2117 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2121 /* create empty opts */
2122 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2124 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2132 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2134 exit(STATUS_CONTROL_C_EXIT
);
2141 static void termsig_handler(int signal
)
2143 qemu_system_shutdown_request();
2146 static void sigchld_handler(int signal
)
2148 waitpid(-1, NULL
, WNOHANG
);
2151 static void sighandler_setup(void)
2153 struct sigaction act
;
2155 memset(&act
, 0, sizeof(act
));
2156 act
.sa_handler
= termsig_handler
;
2157 sigaction(SIGINT
, &act
, NULL
);
2158 sigaction(SIGHUP
, &act
, NULL
);
2159 sigaction(SIGTERM
, &act
, NULL
);
2161 act
.sa_handler
= sigchld_handler
;
2162 act
.sa_flags
= SA_NOCLDSTOP
;
2163 sigaction(SIGCHLD
, &act
, NULL
);
2169 /* Look for support files in the same directory as the executable. */
2170 static char *find_datadir(const char *argv0
)
2176 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2183 while (p
!= buf
&& *p
!= '\\')
2186 if (access(buf
, R_OK
) == 0) {
2187 return qemu_strdup(buf
);
2193 /* Find a likely location for support files using the location of the binary.
2194 For installed binaries this will be "$bindir/../share/qemu". When
2195 running from the build tree this will be "$bindir/../pc-bios". */
2196 #define SHARE_SUFFIX "/share/qemu"
2197 #define BUILD_SUFFIX "/pc-bios"
2198 static char *find_datadir(const char *argv0
)
2206 #if defined(__linux__)
2209 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2215 #elif defined(__FreeBSD__)
2217 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2218 size_t len
= sizeof(buf
) - 1;
2221 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2223 buf
[sizeof(buf
) - 1] = '\0';
2228 /* If we don't have any way of figuring out the actual executable
2229 location then try argv[0]. */
2231 p
= realpath(argv0
, buf
);
2239 max_len
= strlen(dir
) +
2240 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2241 res
= qemu_mallocz(max_len
);
2242 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2243 if (access(res
, R_OK
)) {
2244 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2245 if (access(res
, R_OK
)) {
2257 char *qemu_find_file(int type
, const char *name
)
2263 /* If name contains path separators then try it as a straight path. */
2264 if ((strchr(name
, '/') || strchr(name
, '\\'))
2265 && access(name
, R_OK
) == 0) {
2266 return qemu_strdup(name
);
2269 case QEMU_FILE_TYPE_BIOS
:
2272 case QEMU_FILE_TYPE_KEYMAP
:
2273 subdir
= "keymaps/";
2278 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2279 buf
= qemu_mallocz(len
);
2280 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2281 if (access(buf
, R_OK
)) {
2288 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2290 return qdev_device_help(opts
);
2293 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2297 dev
= qdev_device_add(opts
);
2303 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2305 CharDriverState
*chr
;
2307 chr
= qemu_chr_open_opts(opts
, NULL
);
2313 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2315 CharDriverState
*chr
;
2316 const char *chardev
;
2320 mode
= qemu_opt_get(opts
, "mode");
2324 if (strcmp(mode
, "readline") == 0) {
2325 flags
= MONITOR_USE_READLINE
;
2326 } else if (strcmp(mode
, "control") == 0) {
2327 flags
= MONITOR_USE_CONTROL
;
2329 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2333 if (qemu_opt_get_bool(opts
, "default", 0))
2334 flags
|= MONITOR_IS_DEFAULT
;
2336 chardev
= qemu_opt_get(opts
, "chardev");
2337 chr
= qemu_chr_find(chardev
);
2339 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2343 monitor_init(chr
, flags
);
2347 static void monitor_parse(const char *optarg
, const char *mode
)
2349 static int monitor_device_index
= 0;
2355 if (strstart(optarg
, "chardev:", &p
)) {
2356 snprintf(label
, sizeof(label
), "%s", p
);
2358 if (monitor_device_index
) {
2359 snprintf(label
, sizeof(label
), "monitor%d",
2360 monitor_device_index
);
2362 snprintf(label
, sizeof(label
), "monitor");
2365 opts
= qemu_chr_parse_compat(label
, optarg
);
2367 fprintf(stderr
, "parse error: %s\n", optarg
);
2372 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2374 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2377 qemu_opt_set(opts
, "mode", mode
);
2378 qemu_opt_set(opts
, "chardev", label
);
2380 qemu_opt_set(opts
, "default", "on");
2381 monitor_device_index
++;
2384 struct device_config
{
2386 DEV_USB
, /* -usbdevice */
2388 DEV_SERIAL
, /* -serial */
2389 DEV_PARALLEL
, /* -parallel */
2390 DEV_VIRTCON
, /* -virtioconsole */
2391 DEV_DEBUGCON
, /* -debugcon */
2393 const char *cmdline
;
2394 QTAILQ_ENTRY(device_config
) next
;
2396 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2398 static void add_device_config(int type
, const char *cmdline
)
2400 struct device_config
*conf
;
2402 conf
= qemu_mallocz(sizeof(*conf
));
2404 conf
->cmdline
= cmdline
;
2405 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2408 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2410 struct device_config
*conf
;
2413 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2414 if (conf
->type
!= type
)
2416 rc
= func(conf
->cmdline
);
2423 static int serial_parse(const char *devname
)
2425 static int index
= 0;
2428 if (strcmp(devname
, "none") == 0)
2430 if (index
== MAX_SERIAL_PORTS
) {
2431 fprintf(stderr
, "qemu: too many serial ports\n");
2434 snprintf(label
, sizeof(label
), "serial%d", index
);
2435 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2436 if (!serial_hds
[index
]) {
2437 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2438 devname
, strerror(errno
));
2445 static int parallel_parse(const char *devname
)
2447 static int index
= 0;
2450 if (strcmp(devname
, "none") == 0)
2452 if (index
== MAX_PARALLEL_PORTS
) {
2453 fprintf(stderr
, "qemu: too many parallel ports\n");
2456 snprintf(label
, sizeof(label
), "parallel%d", index
);
2457 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2458 if (!parallel_hds
[index
]) {
2459 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2460 devname
, strerror(errno
));
2467 static int virtcon_parse(const char *devname
)
2469 static int index
= 0;
2471 QemuOpts
*bus_opts
, *dev_opts
;
2473 if (strcmp(devname
, "none") == 0)
2475 if (index
== MAX_VIRTIO_CONSOLES
) {
2476 fprintf(stderr
, "qemu: too many virtio consoles\n");
2480 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2481 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2483 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2484 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2486 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2487 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2488 if (!virtcon_hds
[index
]) {
2489 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2490 devname
, strerror(errno
));
2493 qemu_opt_set(dev_opts
, "chardev", label
);
2499 static int debugcon_parse(const char *devname
)
2503 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2506 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2508 fprintf(stderr
, "qemu: already have a debugcon device\n");
2511 qemu_opt_set(opts
, "driver", "isa-debugcon");
2512 qemu_opt_set(opts
, "chardev", "debugcon");
2516 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2517 const char **poptarg
, int *poptind
)
2519 const QEMUOption
*popt
;
2520 int optind
= *poptind
;
2521 char *r
= argv
[optind
];
2524 loc_set_cmdline(argv
, optind
, 1);
2526 /* Treat --foo the same as -foo. */
2529 popt
= qemu_options
;
2532 error_report("invalid option");
2535 if (!strcmp(popt
->name
, r
+ 1))
2539 if (popt
->flags
& HAS_ARG
) {
2540 if (optind
>= argc
) {
2541 error_report("requires an argument");
2544 optarg
= argv
[optind
++];
2545 loc_set_cmdline(argv
, optind
- 2, 2);
2556 int main(int argc
, char **argv
, char **envp
)
2558 const char *gdbstub_dev
= NULL
;
2559 uint32_t boot_devices_bitmap
= 0;
2561 int snapshot
, linux_boot
, net_boot
;
2562 const char *icount_option
= NULL
;
2563 const char *initrd_filename
;
2564 const char *kernel_filename
, *kernel_cmdline
;
2565 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2567 DisplayChangeListener
*dcl
;
2568 int cyls
, heads
, secs
, translation
;
2569 QemuOpts
*hda_opts
= NULL
, *opts
;
2572 const char *loadvm
= NULL
;
2573 QEMUMachine
*machine
;
2574 const char *cpu_model
;
2579 const char *pid_file
= NULL
;
2580 const char *incoming
= NULL
;
2583 struct passwd
*pwd
= NULL
;
2584 const char *chroot_dir
= NULL
;
2585 const char *run_as
= NULL
;
2587 int show_vnc_port
= 0;
2590 error_set_progname(argv
[0]);
2594 qemu_cache_utils_init(envp
);
2596 QLIST_INIT (&vm_change_state_head
);
2599 struct sigaction act
;
2600 sigfillset(&act
.sa_mask
);
2602 act
.sa_handler
= SIG_IGN
;
2603 sigaction(SIGPIPE
, &act
, NULL
);
2606 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2607 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2608 QEMU to run on a single CPU */
2613 h
= GetCurrentProcess();
2614 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2615 for(i
= 0; i
< 32; i
++) {
2616 if (mask
& (1 << i
))
2621 SetProcessAffinityMask(h
, mask
);
2627 module_call_init(MODULE_INIT_MACHINE
);
2628 machine
= find_default_machine();
2630 initrd_filename
= NULL
;
2633 kernel_filename
= NULL
;
2634 kernel_cmdline
= "";
2635 cyls
= heads
= secs
= 0;
2636 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2638 for (i
= 0; i
< MAX_NODES
; i
++) {
2640 node_cpumask
[i
] = 0;
2649 /* first pass of option parsing */
2651 while (optind
< argc
) {
2652 if (argv
[optind
][0] != '-') {
2657 const QEMUOption
*popt
;
2659 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2660 switch (popt
->index
) {
2661 case QEMU_OPTION_nodefconfig
:
2672 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2673 fp
= fopen(fname
, "r");
2675 if (qemu_config_parse(fp
, fname
) != 0) {
2681 fname
= arch_config_name
;
2682 fp
= fopen(fname
, "r");
2684 if (qemu_config_parse(fp
, fname
) != 0) {
2692 /* second pass of option parsing */
2697 if (argv
[optind
][0] != '-') {
2698 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2700 const QEMUOption
*popt
;
2702 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2703 if (!(popt
->arch_mask
& arch_type
)) {
2704 printf("Option %s not supported for this target\n", popt
->name
);
2707 switch(popt
->index
) {
2709 machine
= find_machine(optarg
);
2712 printf("Supported machines are:\n");
2713 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2715 printf("%-10s %s (alias of %s)\n",
2716 m
->alias
, m
->desc
, m
->name
);
2717 printf("%-10s %s%s\n",
2719 m
->is_default
? " (default)" : "");
2721 exit(*optarg
!= '?');
2724 case QEMU_OPTION_cpu
:
2725 /* hw initialization will check this */
2726 if (*optarg
== '?') {
2727 /* XXX: implement xxx_cpu_list for targets that still miss it */
2728 #if defined(cpu_list_id)
2729 cpu_list_id(stdout
, &fprintf
, optarg
);
2730 #elif defined(cpu_list)
2731 cpu_list(stdout
, &fprintf
); /* deprecated */
2738 case QEMU_OPTION_initrd
:
2739 initrd_filename
= optarg
;
2741 case QEMU_OPTION_hda
:
2743 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2745 hda_opts
= drive_add(optarg
, HD_ALIAS
2746 ",cyls=%d,heads=%d,secs=%d%s",
2747 0, cyls
, heads
, secs
,
2748 translation
== BIOS_ATA_TRANSLATION_LBA
?
2750 translation
== BIOS_ATA_TRANSLATION_NONE
?
2751 ",trans=none" : "");
2753 case QEMU_OPTION_hdb
:
2754 case QEMU_OPTION_hdc
:
2755 case QEMU_OPTION_hdd
:
2756 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2758 case QEMU_OPTION_drive
:
2759 drive_add(NULL
, "%s", optarg
);
2761 case QEMU_OPTION_set
:
2762 if (qemu_set_option(optarg
) != 0)
2765 case QEMU_OPTION_global
:
2766 if (qemu_global_option(optarg
) != 0)
2769 case QEMU_OPTION_mtdblock
:
2770 drive_add(optarg
, MTD_ALIAS
);
2772 case QEMU_OPTION_sd
:
2773 drive_add(optarg
, SD_ALIAS
);
2775 case QEMU_OPTION_pflash
:
2776 drive_add(optarg
, PFLASH_ALIAS
);
2778 case QEMU_OPTION_snapshot
:
2781 case QEMU_OPTION_hdachs
:
2785 cyls
= strtol(p
, (char **)&p
, 0);
2786 if (cyls
< 1 || cyls
> 16383)
2791 heads
= strtol(p
, (char **)&p
, 0);
2792 if (heads
< 1 || heads
> 16)
2797 secs
= strtol(p
, (char **)&p
, 0);
2798 if (secs
< 1 || secs
> 63)
2802 if (!strcmp(p
, "none"))
2803 translation
= BIOS_ATA_TRANSLATION_NONE
;
2804 else if (!strcmp(p
, "lba"))
2805 translation
= BIOS_ATA_TRANSLATION_LBA
;
2806 else if (!strcmp(p
, "auto"))
2807 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2810 } else if (*p
!= '\0') {
2812 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2815 if (hda_opts
!= NULL
) {
2817 snprintf(num
, sizeof(num
), "%d", cyls
);
2818 qemu_opt_set(hda_opts
, "cyls", num
);
2819 snprintf(num
, sizeof(num
), "%d", heads
);
2820 qemu_opt_set(hda_opts
, "heads", num
);
2821 snprintf(num
, sizeof(num
), "%d", secs
);
2822 qemu_opt_set(hda_opts
, "secs", num
);
2823 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2824 qemu_opt_set(hda_opts
, "trans", "lba");
2825 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2826 qemu_opt_set(hda_opts
, "trans", "none");
2830 case QEMU_OPTION_numa
:
2831 if (nb_numa_nodes
>= MAX_NODES
) {
2832 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2837 case QEMU_OPTION_nographic
:
2838 display_type
= DT_NOGRAPHIC
;
2840 #ifdef CONFIG_CURSES
2841 case QEMU_OPTION_curses
:
2842 display_type
= DT_CURSES
;
2845 case QEMU_OPTION_portrait
:
2848 case QEMU_OPTION_kernel
:
2849 kernel_filename
= optarg
;
2851 case QEMU_OPTION_append
:
2852 kernel_cmdline
= optarg
;
2854 case QEMU_OPTION_cdrom
:
2855 drive_add(optarg
, CDROM_ALIAS
);
2857 case QEMU_OPTION_boot
:
2859 static const char * const params
[] = {
2860 "order", "once", "menu", NULL
2862 char buf
[sizeof(boot_devices
)];
2863 char *standard_boot_devices
;
2866 if (!strchr(optarg
, '=')) {
2868 pstrcpy(buf
, sizeof(buf
), optarg
);
2869 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2871 "qemu: unknown boot parameter '%s' in '%s'\n",
2877 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2878 boot_devices_bitmap
= parse_bootdevices(buf
);
2879 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2882 if (get_param_value(buf
, sizeof(buf
),
2884 boot_devices_bitmap
|= parse_bootdevices(buf
);
2885 standard_boot_devices
= qemu_strdup(boot_devices
);
2886 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2887 qemu_register_reset(restore_boot_devices
,
2888 standard_boot_devices
);
2890 if (get_param_value(buf
, sizeof(buf
),
2892 if (!strcmp(buf
, "on")) {
2894 } else if (!strcmp(buf
, "off")) {
2898 "qemu: invalid option value '%s'\n",
2906 case QEMU_OPTION_fda
:
2907 case QEMU_OPTION_fdb
:
2908 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2910 case QEMU_OPTION_no_fd_bootchk
:
2913 case QEMU_OPTION_netdev
:
2914 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2918 case QEMU_OPTION_net
:
2919 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2924 case QEMU_OPTION_tftp
:
2925 legacy_tftp_prefix
= optarg
;
2927 case QEMU_OPTION_bootp
:
2928 legacy_bootp_filename
= optarg
;
2931 case QEMU_OPTION_smb
:
2932 if (net_slirp_smb(optarg
) < 0)
2936 case QEMU_OPTION_redir
:
2937 if (net_slirp_redir(optarg
) < 0)
2941 case QEMU_OPTION_bt
:
2942 add_device_config(DEV_BT
, optarg
);
2944 case QEMU_OPTION_audio_help
:
2945 if (!(audio_available())) {
2946 printf("Option %s not supported for this target\n", popt
->name
);
2952 case QEMU_OPTION_soundhw
:
2953 if (!(audio_available())) {
2954 printf("Option %s not supported for this target\n", popt
->name
);
2957 select_soundhw (optarg
);
2962 case QEMU_OPTION_version
:
2966 case QEMU_OPTION_m
: {
2970 value
= strtoul(optarg
, &ptr
, 10);
2972 case 0: case 'M': case 'm':
2979 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2983 /* On 32-bit hosts, QEMU is limited by virtual address space */
2984 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2985 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2988 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2989 fprintf(stderr
, "qemu: ram size too large\n");
2995 case QEMU_OPTION_mempath
:
2999 case QEMU_OPTION_mem_prealloc
:
3004 set_cpu_log(optarg
);
3007 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3009 case QEMU_OPTION_gdb
:
3010 gdbstub_dev
= optarg
;
3015 case QEMU_OPTION_bios
:
3018 case QEMU_OPTION_singlestep
:
3025 keyboard_layout
= optarg
;
3027 case QEMU_OPTION_localtime
:
3030 case QEMU_OPTION_vga
:
3031 select_vgahw (optarg
);
3038 w
= strtol(p
, (char **)&p
, 10);
3041 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3047 h
= strtol(p
, (char **)&p
, 10);
3052 depth
= strtol(p
, (char **)&p
, 10);
3053 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3054 depth
!= 24 && depth
!= 32)
3056 } else if (*p
== '\0') {
3057 depth
= graphic_depth
;
3064 graphic_depth
= depth
;
3067 case QEMU_OPTION_echr
:
3070 term_escape_char
= strtol(optarg
, &r
, 0);
3072 printf("Bad argument to echr\n");
3075 case QEMU_OPTION_monitor
:
3076 monitor_parse(optarg
, "readline");
3077 default_monitor
= 0;
3079 case QEMU_OPTION_qmp
:
3080 monitor_parse(optarg
, "control");
3081 default_monitor
= 0;
3083 case QEMU_OPTION_mon
:
3084 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3086 fprintf(stderr
, "parse error: %s\n", optarg
);
3089 default_monitor
= 0;
3091 case QEMU_OPTION_chardev
:
3092 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3094 fprintf(stderr
, "parse error: %s\n", optarg
);
3098 case QEMU_OPTION_serial
:
3099 add_device_config(DEV_SERIAL
, optarg
);
3101 if (strncmp(optarg
, "mon:", 4) == 0) {
3102 default_monitor
= 0;
3105 case QEMU_OPTION_watchdog
:
3108 "qemu: only one watchdog option may be given\n");
3113 case QEMU_OPTION_watchdog_action
:
3114 if (select_watchdog_action(optarg
) == -1) {
3115 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3119 case QEMU_OPTION_virtiocon
:
3120 add_device_config(DEV_VIRTCON
, optarg
);
3121 default_virtcon
= 0;
3122 if (strncmp(optarg
, "mon:", 4) == 0) {
3123 default_monitor
= 0;
3126 case QEMU_OPTION_parallel
:
3127 add_device_config(DEV_PARALLEL
, optarg
);
3128 default_parallel
= 0;
3129 if (strncmp(optarg
, "mon:", 4) == 0) {
3130 default_monitor
= 0;
3133 case QEMU_OPTION_debugcon
:
3134 add_device_config(DEV_DEBUGCON
, optarg
);
3136 case QEMU_OPTION_loadvm
:
3139 case QEMU_OPTION_full_screen
:
3143 case QEMU_OPTION_no_frame
:
3146 case QEMU_OPTION_alt_grab
:
3149 case QEMU_OPTION_ctrl_grab
:
3152 case QEMU_OPTION_no_quit
:
3155 case QEMU_OPTION_sdl
:
3156 display_type
= DT_SDL
;
3159 case QEMU_OPTION_pidfile
:
3162 case QEMU_OPTION_win2k_hack
:
3163 win2k_install_hack
= 1;
3165 case QEMU_OPTION_rtc_td_hack
:
3168 case QEMU_OPTION_acpitable
:
3169 do_acpitable_option(optarg
);
3171 case QEMU_OPTION_smbios
:
3172 do_smbios_option(optarg
);
3174 case QEMU_OPTION_enable_kvm
:
3175 if (!(kvm_available())) {
3176 printf("Option %s not supported for this target\n", popt
->name
);
3181 case QEMU_OPTION_usb
:
3184 case QEMU_OPTION_usbdevice
:
3186 add_device_config(DEV_USB
, optarg
);
3188 case QEMU_OPTION_device
:
3189 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3193 case QEMU_OPTION_smp
:
3196 fprintf(stderr
, "Invalid number of CPUs\n");
3199 if (max_cpus
< smp_cpus
) {
3200 fprintf(stderr
, "maxcpus must be equal to or greater than "
3204 if (max_cpus
> 255) {
3205 fprintf(stderr
, "Unsupported number of maxcpus\n");
3209 case QEMU_OPTION_vnc
:
3210 display_type
= DT_VNC
;
3211 vnc_display
= optarg
;
3213 case QEMU_OPTION_no_acpi
:
3216 case QEMU_OPTION_no_hpet
:
3219 case QEMU_OPTION_balloon
:
3220 if (balloon_parse(optarg
) < 0) {
3221 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3225 case QEMU_OPTION_no_reboot
:
3228 case QEMU_OPTION_no_shutdown
:
3231 case QEMU_OPTION_show_cursor
:
3234 case QEMU_OPTION_uuid
:
3235 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3236 fprintf(stderr
, "Fail to parse UUID string."
3237 " Wrong format.\n");
3242 case QEMU_OPTION_daemonize
:
3246 case QEMU_OPTION_option_rom
:
3247 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3248 fprintf(stderr
, "Too many option ROMs\n");
3251 option_rom
[nb_option_roms
] = optarg
;
3254 case QEMU_OPTION_semihosting
:
3255 semihosting_enabled
= 1;
3257 case QEMU_OPTION_name
:
3258 qemu_name
= qemu_strdup(optarg
);
3260 char *p
= strchr(qemu_name
, ',');
3263 if (strncmp(p
, "process=", 8)) {
3264 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3272 case QEMU_OPTION_prom_env
:
3273 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3274 fprintf(stderr
, "Too many prom variables\n");
3277 prom_envs
[nb_prom_envs
] = optarg
;
3280 case QEMU_OPTION_old_param
:
3283 case QEMU_OPTION_clock
:
3284 configure_alarms(optarg
);
3286 case QEMU_OPTION_startdate
:
3287 configure_rtc_date_offset(optarg
, 1);
3289 case QEMU_OPTION_rtc
:
3290 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3292 fprintf(stderr
, "parse error: %s\n", optarg
);
3295 configure_rtc(opts
);
3297 case QEMU_OPTION_tb_size
:
3298 tb_size
= strtol(optarg
, NULL
, 0);
3302 case QEMU_OPTION_icount
:
3303 icount_option
= optarg
;
3305 case QEMU_OPTION_incoming
:
3308 case QEMU_OPTION_nodefaults
:
3310 default_parallel
= 0;
3311 default_virtcon
= 0;
3312 default_monitor
= 0;
3320 case QEMU_OPTION_chroot
:
3321 chroot_dir
= optarg
;
3323 case QEMU_OPTION_runas
:
3327 case QEMU_OPTION_xen_domid
:
3328 if (!(xen_available())) {
3329 printf("Option %s not supported for this target\n", popt
->name
);
3332 xen_domid
= atoi(optarg
);
3334 case QEMU_OPTION_xen_create
:
3335 if (!(xen_available())) {
3336 printf("Option %s not supported for this target\n", popt
->name
);
3339 xen_mode
= XEN_CREATE
;
3341 case QEMU_OPTION_xen_attach
:
3342 if (!(xen_available())) {
3343 printf("Option %s not supported for this target\n", popt
->name
);
3346 xen_mode
= XEN_ATTACH
;
3348 case QEMU_OPTION_readconfig
:
3351 fp
= fopen(optarg
, "r");
3353 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3356 if (qemu_config_parse(fp
, optarg
) != 0) {
3362 case QEMU_OPTION_writeconfig
:
3365 if (strcmp(optarg
, "-") == 0) {
3368 fp
= fopen(optarg
, "w");
3370 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3374 qemu_config_write(fp
);
3383 /* If no data_dir is specified then try to find it relative to the
3386 data_dir
= find_datadir(argv
[0]);
3388 /* If all else fails use the install patch specified when building. */
3390 data_dir
= CONFIG_QEMU_SHAREDIR
;
3394 * Default to max_cpus = smp_cpus, in case the user doesn't
3395 * specify a max_cpus value.
3398 max_cpus
= smp_cpus
;
3400 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3401 if (smp_cpus
> machine
->max_cpus
) {
3402 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3403 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3408 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3409 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3411 if (machine
->no_serial
) {
3414 if (machine
->no_parallel
) {
3415 default_parallel
= 0;
3417 if (!machine
->use_virtcon
) {
3418 default_virtcon
= 0;
3420 if (machine
->no_vga
) {
3423 if (machine
->no_floppy
) {
3426 if (machine
->no_cdrom
) {
3429 if (machine
->no_sdcard
) {
3433 if (display_type
== DT_NOGRAPHIC
) {
3434 if (default_parallel
)
3435 add_device_config(DEV_PARALLEL
, "null");
3436 if (default_serial
&& default_monitor
) {
3437 add_device_config(DEV_SERIAL
, "mon:stdio");
3438 } else if (default_virtcon
&& default_monitor
) {
3439 add_device_config(DEV_VIRTCON
, "mon:stdio");
3442 add_device_config(DEV_SERIAL
, "stdio");
3443 if (default_virtcon
)
3444 add_device_config(DEV_VIRTCON
, "stdio");
3445 if (default_monitor
)
3446 monitor_parse("stdio", "readline");
3450 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3451 if (default_parallel
)
3452 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3453 if (default_monitor
)
3454 monitor_parse("vc:80Cx24C", "readline");
3455 if (default_virtcon
)
3456 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3459 vga_interface_type
= VGA_CIRRUS
;
3461 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3468 if (pipe(fds
) == -1)
3479 len
= read(fds
[0], &status
, 1);
3480 if (len
== -1 && (errno
== EINTR
))
3485 else if (status
== 1) {
3486 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3494 qemu_set_cloexec(fds
[1]);
3506 signal(SIGTSTP
, SIG_IGN
);
3507 signal(SIGTTOU
, SIG_IGN
);
3508 signal(SIGTTIN
, SIG_IGN
);
3512 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3516 if (write(fds
[1], &status
, 1) != 1) {
3517 perror("daemonize. Writing to pipe\n");
3521 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3525 if (kvm_enabled()) {
3528 ret
= kvm_init(smp_cpus
);
3530 fprintf(stderr
, "failed to initialize KVM\n");
3535 if (qemu_init_main_loop()) {
3536 fprintf(stderr
, "qemu_init_main_loop failed\n");
3539 linux_boot
= (kernel_filename
!= NULL
);
3541 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3542 fprintf(stderr
, "-append only allowed with -kernel option\n");
3546 if (!linux_boot
&& initrd_filename
!= NULL
) {
3547 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3552 /* Win32 doesn't support line-buffering and requires size >= 2 */
3553 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3556 if (init_timer_alarm() < 0) {
3557 fprintf(stderr
, "could not initialize alarm timer\n");
3560 configure_icount(icount_option
);
3564 if (net_init_clients() < 0) {
3568 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
3569 net_set_boot_mask(net_boot
);
3571 /* init the bluetooth world */
3572 if (foreach_device_config(DEV_BT
, bt_parse
))
3575 /* init the memory */
3577 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3579 /* init the dynamic translator */
3580 cpu_exec_init_all(tb_size
* 1024 * 1024);
3582 bdrv_init_with_whitelist();
3586 if (default_cdrom
) {
3587 /* we always create the cdrom drive, even if no disk is there */
3588 drive_add(NULL
, CDROM_ALIAS
);
3591 if (default_floppy
) {
3592 /* we always create at least one floppy */
3593 drive_add(NULL
, FD_ALIAS
, 0);
3596 if (default_sdcard
) {
3597 /* we always create one sd slot, even if no card is in it */
3598 drive_add(NULL
, SD_ALIAS
);
3601 /* open the virtual block devices */
3603 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3604 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3607 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3610 if (nb_numa_nodes
> 0) {
3613 if (nb_numa_nodes
> smp_cpus
) {
3614 nb_numa_nodes
= smp_cpus
;
3617 /* If no memory size if given for any node, assume the default case
3618 * and distribute the available memory equally across all nodes
3620 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3621 if (node_mem
[i
] != 0)
3624 if (i
== nb_numa_nodes
) {
3625 uint64_t usedmem
= 0;
3627 /* On Linux, the each node's border has to be 8MB aligned,
3628 * the final node gets the rest.
3630 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3631 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3632 usedmem
+= node_mem
[i
];
3634 node_mem
[i
] = ram_size
- usedmem
;
3637 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3638 if (node_cpumask
[i
] != 0)
3641 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3642 * must cope with this anyway, because there are BIOSes out there in
3643 * real machines which also use this scheme.
3645 if (i
== nb_numa_nodes
) {
3646 for (i
= 0; i
< smp_cpus
; i
++) {
3647 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3652 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3654 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3656 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3658 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3661 module_call_init(MODULE_INIT_DEVICE
);
3663 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3667 i
= select_watchdog(watchdog
);
3669 exit (i
== 1 ? 1 : 0);
3672 if (machine
->compat_props
) {
3673 qdev_prop_register_global_list(machine
->compat_props
);
3677 machine
->init(ram_size
, boot_devices
,
3678 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3680 cpu_synchronize_all_post_init();
3683 /* must be after terminal init, SDL library changes signal handlers */
3689 current_machine
= machine
;
3691 /* init USB devices */
3693 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3697 /* init generic devices */
3698 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3701 net_check_clients();
3703 /* just use the first displaystate for the moment */
3704 ds
= get_displaystate();
3706 if (display_type
== DT_DEFAULT
) {
3707 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3708 display_type
= DT_SDL
;
3710 display_type
= DT_VNC
;
3711 vnc_display
= "localhost:0,to=99";
3717 switch (display_type
) {
3720 #if defined(CONFIG_CURSES)
3722 curses_display_init(ds
, full_screen
);
3725 #if defined(CONFIG_SDL)
3727 sdl_display_init(ds
, full_screen
, no_frame
);
3729 #elif defined(CONFIG_COCOA)
3731 cocoa_display_init(ds
, full_screen
);
3735 vnc_display_init(ds
);
3736 if (vnc_display_open(ds
, vnc_display
) < 0)
3739 if (show_vnc_port
) {
3740 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3748 dcl
= ds
->listeners
;
3749 while (dcl
!= NULL
) {
3750 if (dcl
->dpy_refresh
!= NULL
) {
3751 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3752 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3757 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3758 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3759 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3762 text_consoles_set_display(ds
);
3764 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3767 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3768 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3773 qdev_machine_creation_done();
3775 if (rom_load_all() != 0) {
3776 fprintf(stderr
, "rom loading failed\n");
3780 qemu_system_reset();
3782 if (load_vmstate(loadvm
) < 0) {
3788 qemu_start_incoming_migration(incoming
);
3789 } else if (autostart
) {
3799 len
= write(fds
[1], &status
, 1);
3800 if (len
== -1 && (errno
== EINTR
))
3807 perror("not able to chdir to /");
3810 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3816 pwd
= getpwnam(run_as
);
3818 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3824 if (chroot(chroot_dir
) < 0) {
3825 fprintf(stderr
, "chroot failed\n");
3829 perror("not able to chdir to /");
3835 if (setgid(pwd
->pw_gid
) < 0) {
3836 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3839 if (setuid(pwd
->pw_uid
) < 0) {
3840 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3843 if (setuid(0) != -1) {
3844 fprintf(stderr
, "Dropping privileges failed\n");