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 void validate_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');
1214 static void restore_boot_devices(void *opaque
)
1216 char *standard_boot_devices
= opaque
;
1218 qemu_boot_set(standard_boot_devices
);
1220 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1221 qemu_free(standard_boot_devices
);
1224 static void numa_add(const char *optarg
)
1228 unsigned long long value
, endvalue
;
1231 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1232 if (!strcmp(option
, "node")) {
1233 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1234 nodenr
= nb_numa_nodes
;
1236 nodenr
= strtoull(option
, NULL
, 10);
1239 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1240 node_mem
[nodenr
] = 0;
1242 value
= strtoull(option
, &endptr
, 0);
1244 case 0: case 'M': case 'm':
1251 node_mem
[nodenr
] = value
;
1253 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1254 node_cpumask
[nodenr
] = 0;
1256 value
= strtoull(option
, &endptr
, 10);
1259 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1261 if (*endptr
== '-') {
1262 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1263 if (endvalue
>= 63) {
1266 "only 63 CPUs in NUMA mode supported.\n");
1268 value
= (2ULL << endvalue
) - (1ULL << value
);
1270 value
= 1ULL << value
;
1273 node_cpumask
[nodenr
] = value
;
1280 static void smp_parse(const char *optarg
)
1282 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1286 smp
= strtoul(optarg
, &endptr
, 10);
1287 if (endptr
!= optarg
) {
1288 if (*endptr
== ',') {
1292 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1293 sockets
= strtoull(option
, NULL
, 10);
1294 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1295 cores
= strtoull(option
, NULL
, 10);
1296 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1297 threads
= strtoull(option
, NULL
, 10);
1298 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1299 max_cpus
= strtoull(option
, NULL
, 10);
1301 /* compute missing values, prefer sockets over cores over threads */
1302 if (smp
== 0 || sockets
== 0) {
1303 sockets
= sockets
> 0 ? sockets
: 1;
1304 cores
= cores
> 0 ? cores
: 1;
1305 threads
= threads
> 0 ? threads
: 1;
1307 smp
= cores
* threads
* sockets
;
1311 threads
= threads
> 0 ? threads
: 1;
1312 cores
= smp
/ (sockets
* threads
);
1315 threads
= smp
/ (cores
* sockets
);
1320 smp_cores
= cores
> 0 ? cores
: 1;
1321 smp_threads
= threads
> 0 ? threads
: 1;
1323 max_cpus
= smp_cpus
;
1326 /***********************************************************/
1329 static int usb_device_add(const char *devname
, int is_hotplug
)
1332 USBDevice
*dev
= NULL
;
1337 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1338 dev
= usbdevice_create(devname
);
1342 /* the other ones */
1343 if (strstart(devname
, "host:", &p
)) {
1344 dev
= usb_host_device_open(p
);
1345 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1346 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1347 bt_new_hci(qemu_find_bt_vlan(0)));
1358 static int usb_device_del(const char *devname
)
1363 if (strstart(devname
, "host:", &p
))
1364 return usb_host_device_close(p
);
1369 p
= strchr(devname
, '.');
1372 bus_num
= strtoul(devname
, NULL
, 0);
1373 addr
= strtoul(p
+ 1, NULL
, 0);
1375 return usb_device_delete_addr(bus_num
, addr
);
1378 static int usb_parse(const char *cmdline
)
1381 r
= usb_device_add(cmdline
, 0);
1383 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1388 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1390 const char *devname
= qdict_get_str(qdict
, "devname");
1391 if (usb_device_add(devname
, 1) < 0) {
1392 error_report("could not add USB device '%s'", devname
);
1396 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1398 const char *devname
= qdict_get_str(qdict
, "devname");
1399 if (usb_device_del(devname
) < 0) {
1400 error_report("could not delete USB device '%s'", devname
);
1404 /***********************************************************/
1405 /* PCMCIA/Cardbus */
1407 static struct pcmcia_socket_entry_s
{
1408 PCMCIASocket
*socket
;
1409 struct pcmcia_socket_entry_s
*next
;
1410 } *pcmcia_sockets
= 0;
1412 void pcmcia_socket_register(PCMCIASocket
*socket
)
1414 struct pcmcia_socket_entry_s
*entry
;
1416 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1417 entry
->socket
= socket
;
1418 entry
->next
= pcmcia_sockets
;
1419 pcmcia_sockets
= entry
;
1422 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1424 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1426 ptr
= &pcmcia_sockets
;
1427 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1428 if (entry
->socket
== socket
) {
1434 void pcmcia_info(Monitor
*mon
)
1436 struct pcmcia_socket_entry_s
*iter
;
1438 if (!pcmcia_sockets
)
1439 monitor_printf(mon
, "No PCMCIA sockets\n");
1441 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1442 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1443 iter
->socket
->attached
? iter
->socket
->card_string
:
1447 /***********************************************************/
1450 typedef struct IOHandlerRecord
{
1452 IOCanReadHandler
*fd_read_poll
;
1454 IOHandler
*fd_write
;
1457 /* temporary data */
1459 QLIST_ENTRY(IOHandlerRecord
) next
;
1462 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1463 QLIST_HEAD_INITIALIZER(io_handlers
);
1466 /* XXX: fd_read_poll should be suppressed, but an API change is
1467 necessary in the character devices to suppress fd_can_read(). */
1468 int qemu_set_fd_handler2(int fd
,
1469 IOCanReadHandler
*fd_read_poll
,
1471 IOHandler
*fd_write
,
1474 IOHandlerRecord
*ioh
;
1476 if (!fd_read
&& !fd_write
) {
1477 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1478 if (ioh
->fd
== fd
) {
1484 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1488 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1489 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1492 ioh
->fd_read_poll
= fd_read_poll
;
1493 ioh
->fd_read
= fd_read
;
1494 ioh
->fd_write
= fd_write
;
1495 ioh
->opaque
= opaque
;
1501 int qemu_set_fd_handler(int fd
,
1503 IOHandler
*fd_write
,
1506 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1510 /***********************************************************/
1511 /* Polling handling */
1513 typedef struct PollingEntry
{
1516 struct PollingEntry
*next
;
1519 static PollingEntry
*first_polling_entry
;
1521 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1523 PollingEntry
**ppe
, *pe
;
1524 pe
= qemu_mallocz(sizeof(PollingEntry
));
1526 pe
->opaque
= opaque
;
1527 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1532 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1534 PollingEntry
**ppe
, *pe
;
1535 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1537 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1545 /***********************************************************/
1546 /* Wait objects support */
1547 typedef struct WaitObjects
{
1549 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1550 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1551 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1554 static WaitObjects wait_objects
= {0};
1556 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1558 WaitObjects
*w
= &wait_objects
;
1560 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1562 w
->events
[w
->num
] = handle
;
1563 w
->func
[w
->num
] = func
;
1564 w
->opaque
[w
->num
] = opaque
;
1569 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1572 WaitObjects
*w
= &wait_objects
;
1575 for (i
= 0; i
< w
->num
; i
++) {
1576 if (w
->events
[i
] == handle
)
1579 w
->events
[i
] = w
->events
[i
+ 1];
1580 w
->func
[i
] = w
->func
[i
+ 1];
1581 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1589 /***********************************************************/
1590 /* machine registration */
1592 static QEMUMachine
*first_machine
= NULL
;
1593 QEMUMachine
*current_machine
= NULL
;
1595 int qemu_register_machine(QEMUMachine
*m
)
1598 pm
= &first_machine
;
1606 static QEMUMachine
*find_machine(const char *name
)
1610 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1611 if (!strcmp(m
->name
, name
))
1613 if (m
->alias
&& !strcmp(m
->alias
, name
))
1619 static QEMUMachine
*find_default_machine(void)
1623 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1624 if (m
->is_default
) {
1631 /***********************************************************/
1632 /* main execution loop */
1634 static void gui_update(void *opaque
)
1636 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1637 DisplayState
*ds
= opaque
;
1638 DisplayChangeListener
*dcl
= ds
->listeners
;
1640 qemu_flush_coalesced_mmio_buffer();
1643 while (dcl
!= NULL
) {
1644 if (dcl
->gui_timer_interval
&&
1645 dcl
->gui_timer_interval
< interval
)
1646 interval
= dcl
->gui_timer_interval
;
1649 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1652 static void nographic_update(void *opaque
)
1654 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1656 qemu_flush_coalesced_mmio_buffer();
1657 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1660 struct vm_change_state_entry
{
1661 VMChangeStateHandler
*cb
;
1663 QLIST_ENTRY (vm_change_state_entry
) entries
;
1666 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1668 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1671 VMChangeStateEntry
*e
;
1673 e
= qemu_mallocz(sizeof (*e
));
1677 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1681 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1683 QLIST_REMOVE (e
, entries
);
1687 void vm_state_notify(int running
, int reason
)
1689 VMChangeStateEntry
*e
;
1691 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1692 e
->cb(e
->opaque
, running
, reason
);
1701 vm_state_notify(1, 0);
1706 /* reset/shutdown handler */
1708 typedef struct QEMUResetEntry
{
1709 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1710 QEMUResetHandler
*func
;
1714 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1715 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1716 static int reset_requested
;
1717 static int shutdown_requested
;
1718 static int powerdown_requested
;
1719 int debug_requested
;
1720 static int vmstop_requested
;
1722 int qemu_shutdown_requested(void)
1724 int r
= shutdown_requested
;
1725 shutdown_requested
= 0;
1729 int qemu_reset_requested(void)
1731 int r
= reset_requested
;
1732 reset_requested
= 0;
1736 int qemu_powerdown_requested(void)
1738 int r
= powerdown_requested
;
1739 powerdown_requested
= 0;
1743 static int qemu_debug_requested(void)
1745 int r
= debug_requested
;
1746 debug_requested
= 0;
1750 static int qemu_vmstop_requested(void)
1752 int r
= vmstop_requested
;
1753 vmstop_requested
= 0;
1757 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1759 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1762 re
->opaque
= opaque
;
1763 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1766 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1770 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1771 if (re
->func
== func
&& re
->opaque
== opaque
) {
1772 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1779 void qemu_system_reset(void)
1781 QEMUResetEntry
*re
, *nre
;
1783 /* reset all devices */
1784 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1785 re
->func(re
->opaque
);
1787 monitor_protocol_event(QEVENT_RESET
, NULL
);
1788 cpu_synchronize_all_post_reset();
1791 void qemu_system_reset_request(void)
1794 shutdown_requested
= 1;
1796 reset_requested
= 1;
1798 qemu_notify_event();
1801 void qemu_system_shutdown_request(void)
1803 shutdown_requested
= 1;
1804 qemu_notify_event();
1807 void qemu_system_powerdown_request(void)
1809 powerdown_requested
= 1;
1810 qemu_notify_event();
1814 static void host_main_loop_wait(int *timeout
)
1820 /* XXX: need to suppress polling by better using win32 events */
1822 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1823 ret
|= pe
->func(pe
->opaque
);
1827 WaitObjects
*w
= &wait_objects
;
1829 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1830 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1831 if (w
->func
[ret
- WAIT_OBJECT_0
])
1832 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1834 /* Check for additional signaled events */
1835 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1837 /* Check if event is signaled */
1838 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1839 if(ret2
== WAIT_OBJECT_0
) {
1841 w
->func
[i
](w
->opaque
[i
]);
1842 } else if (ret2
== WAIT_TIMEOUT
) {
1844 err
= GetLastError();
1845 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1848 } else if (ret
== WAIT_TIMEOUT
) {
1850 err
= GetLastError();
1851 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1858 static void host_main_loop_wait(int *timeout
)
1863 void main_loop_wait(int nonblocking
)
1865 IOHandlerRecord
*ioh
;
1866 fd_set rfds
, wfds
, xfds
;
1874 timeout
= qemu_calculate_timeout();
1875 qemu_bh_update_timeout(&timeout
);
1878 host_main_loop_wait(&timeout
);
1880 /* poll any events */
1881 /* XXX: separate device handlers from system ones */
1886 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1890 (!ioh
->fd_read_poll
||
1891 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1892 FD_SET(ioh
->fd
, &rfds
);
1896 if (ioh
->fd_write
) {
1897 FD_SET(ioh
->fd
, &wfds
);
1903 tv
.tv_sec
= timeout
/ 1000;
1904 tv
.tv_usec
= (timeout
% 1000) * 1000;
1906 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1908 qemu_mutex_unlock_iothread();
1909 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1910 qemu_mutex_lock_iothread();
1912 IOHandlerRecord
*pioh
;
1914 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1916 QLIST_REMOVE(ioh
, next
);
1920 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1921 ioh
->fd_read(ioh
->opaque
);
1923 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1924 ioh
->fd_write(ioh
->opaque
);
1929 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1931 qemu_run_all_timers();
1933 /* Check bottom-halves last in case any of the earlier events triggered
1939 static int vm_can_run(void)
1941 if (powerdown_requested
)
1943 if (reset_requested
)
1945 if (shutdown_requested
)
1947 if (debug_requested
)
1952 qemu_irq qemu_system_powerdown
;
1954 static void main_loop(void)
1958 #ifdef CONFIG_IOTHREAD
1959 qemu_system_ready
= 1;
1960 qemu_cond_broadcast(&qemu_system_cond
);
1965 bool nonblocking
= false;
1966 #ifdef CONFIG_PROFILER
1969 #ifndef CONFIG_IOTHREAD
1970 nonblocking
= tcg_cpu_exec();
1972 #ifdef CONFIG_PROFILER
1973 ti
= profile_getclock();
1975 main_loop_wait(nonblocking
);
1976 #ifdef CONFIG_PROFILER
1977 dev_time
+= profile_getclock() - ti
;
1979 } while (vm_can_run());
1981 if ((r
= qemu_debug_requested())) {
1984 if (qemu_shutdown_requested()) {
1985 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1992 if (qemu_reset_requested()) {
1994 qemu_system_reset();
1997 if (qemu_powerdown_requested()) {
1998 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1999 qemu_irq_raise(qemu_system_powerdown
);
2001 if ((r
= qemu_vmstop_requested())) {
2008 static void version(void)
2010 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2013 static void help(int exitcode
)
2015 const char *options_help
=
2016 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2018 #define DEFHEADING(text) stringify(text) "\n"
2019 #include "qemu-options.h"
2025 printf("usage: %s [options] [disk_image]\n"
2027 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2030 "During emulation, the following keys are useful:\n"
2031 "ctrl-alt-f toggle full screen\n"
2032 "ctrl-alt-n switch to virtual console 'n'\n"
2033 "ctrl-alt toggle mouse and keyboard grab\n"
2035 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2041 #define HAS_ARG 0x0001
2044 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2046 #define DEFHEADING(text)
2047 #include "qemu-options.h"
2053 typedef struct QEMUOption
{
2060 static const QEMUOption qemu_options
[] = {
2061 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2062 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2063 { option, opt_arg, opt_enum, arch_mask },
2064 #define DEFHEADING(text)
2065 #include "qemu-options.h"
2071 static void select_vgahw (const char *p
)
2076 vga_interface_type
= VGA_NONE
;
2077 if (strstart(p
, "std", &opts
)) {
2078 vga_interface_type
= VGA_STD
;
2079 } else if (strstart(p
, "cirrus", &opts
)) {
2080 vga_interface_type
= VGA_CIRRUS
;
2081 } else if (strstart(p
, "vmware", &opts
)) {
2082 vga_interface_type
= VGA_VMWARE
;
2083 } else if (strstart(p
, "xenfb", &opts
)) {
2084 vga_interface_type
= VGA_XENFB
;
2085 } else if (!strstart(p
, "none", &opts
)) {
2087 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2091 const char *nextopt
;
2093 if (strstart(opts
, ",retrace=", &nextopt
)) {
2095 if (strstart(opts
, "dumb", &nextopt
))
2096 vga_retrace_method
= VGA_RETRACE_DUMB
;
2097 else if (strstart(opts
, "precise", &nextopt
))
2098 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2099 else goto invalid_vga
;
2100 } else goto invalid_vga
;
2105 static int balloon_parse(const char *arg
)
2109 if (strcmp(arg
, "none") == 0) {
2113 if (!strncmp(arg
, "virtio", 6)) {
2114 if (arg
[6] == ',') {
2115 /* have params -> parse them */
2116 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2120 /* create empty opts */
2121 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2123 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2131 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2133 exit(STATUS_CONTROL_C_EXIT
);
2140 static void termsig_handler(int signal
)
2142 qemu_system_shutdown_request();
2145 static void sigchld_handler(int signal
)
2147 waitpid(-1, NULL
, WNOHANG
);
2150 static void sighandler_setup(void)
2152 struct sigaction act
;
2154 memset(&act
, 0, sizeof(act
));
2155 act
.sa_handler
= termsig_handler
;
2156 sigaction(SIGINT
, &act
, NULL
);
2157 sigaction(SIGHUP
, &act
, NULL
);
2158 sigaction(SIGTERM
, &act
, NULL
);
2160 act
.sa_handler
= sigchld_handler
;
2161 act
.sa_flags
= SA_NOCLDSTOP
;
2162 sigaction(SIGCHLD
, &act
, NULL
);
2168 /* Look for support files in the same directory as the executable. */
2169 static char *find_datadir(const char *argv0
)
2175 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2182 while (p
!= buf
&& *p
!= '\\')
2185 if (access(buf
, R_OK
) == 0) {
2186 return qemu_strdup(buf
);
2192 /* Find a likely location for support files using the location of the binary.
2193 For installed binaries this will be "$bindir/../share/qemu". When
2194 running from the build tree this will be "$bindir/../pc-bios". */
2195 #define SHARE_SUFFIX "/share/qemu"
2196 #define BUILD_SUFFIX "/pc-bios"
2197 static char *find_datadir(const char *argv0
)
2205 #if defined(__linux__)
2208 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2214 #elif defined(__FreeBSD__)
2216 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2217 size_t len
= sizeof(buf
) - 1;
2220 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2222 buf
[sizeof(buf
) - 1] = '\0';
2227 /* If we don't have any way of figuring out the actual executable
2228 location then try argv[0]. */
2230 p
= realpath(argv0
, buf
);
2238 max_len
= strlen(dir
) +
2239 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2240 res
= qemu_mallocz(max_len
);
2241 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2242 if (access(res
, R_OK
)) {
2243 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2244 if (access(res
, R_OK
)) {
2256 char *qemu_find_file(int type
, const char *name
)
2262 /* If name contains path separators then try it as a straight path. */
2263 if ((strchr(name
, '/') || strchr(name
, '\\'))
2264 && access(name
, R_OK
) == 0) {
2265 return qemu_strdup(name
);
2268 case QEMU_FILE_TYPE_BIOS
:
2271 case QEMU_FILE_TYPE_KEYMAP
:
2272 subdir
= "keymaps/";
2277 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2278 buf
= qemu_mallocz(len
);
2279 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2280 if (access(buf
, R_OK
)) {
2287 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2289 return qdev_device_help(opts
);
2292 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2296 dev
= qdev_device_add(opts
);
2302 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2304 CharDriverState
*chr
;
2306 chr
= qemu_chr_open_opts(opts
, NULL
);
2312 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2314 CharDriverState
*chr
;
2315 const char *chardev
;
2319 mode
= qemu_opt_get(opts
, "mode");
2323 if (strcmp(mode
, "readline") == 0) {
2324 flags
= MONITOR_USE_READLINE
;
2325 } else if (strcmp(mode
, "control") == 0) {
2326 flags
= MONITOR_USE_CONTROL
;
2328 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2332 if (qemu_opt_get_bool(opts
, "default", 0))
2333 flags
|= MONITOR_IS_DEFAULT
;
2335 chardev
= qemu_opt_get(opts
, "chardev");
2336 chr
= qemu_chr_find(chardev
);
2338 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2342 monitor_init(chr
, flags
);
2346 static void monitor_parse(const char *optarg
, const char *mode
)
2348 static int monitor_device_index
= 0;
2354 if (strstart(optarg
, "chardev:", &p
)) {
2355 snprintf(label
, sizeof(label
), "%s", p
);
2357 if (monitor_device_index
) {
2358 snprintf(label
, sizeof(label
), "monitor%d",
2359 monitor_device_index
);
2361 snprintf(label
, sizeof(label
), "monitor");
2364 opts
= qemu_chr_parse_compat(label
, optarg
);
2366 fprintf(stderr
, "parse error: %s\n", optarg
);
2371 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2373 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2376 qemu_opt_set(opts
, "mode", mode
);
2377 qemu_opt_set(opts
, "chardev", label
);
2379 qemu_opt_set(opts
, "default", "on");
2380 monitor_device_index
++;
2383 struct device_config
{
2385 DEV_USB
, /* -usbdevice */
2387 DEV_SERIAL
, /* -serial */
2388 DEV_PARALLEL
, /* -parallel */
2389 DEV_VIRTCON
, /* -virtioconsole */
2390 DEV_DEBUGCON
, /* -debugcon */
2392 const char *cmdline
;
2393 QTAILQ_ENTRY(device_config
) next
;
2395 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2397 static void add_device_config(int type
, const char *cmdline
)
2399 struct device_config
*conf
;
2401 conf
= qemu_mallocz(sizeof(*conf
));
2403 conf
->cmdline
= cmdline
;
2404 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2407 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2409 struct device_config
*conf
;
2412 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2413 if (conf
->type
!= type
)
2415 rc
= func(conf
->cmdline
);
2422 static int serial_parse(const char *devname
)
2424 static int index
= 0;
2427 if (strcmp(devname
, "none") == 0)
2429 if (index
== MAX_SERIAL_PORTS
) {
2430 fprintf(stderr
, "qemu: too many serial ports\n");
2433 snprintf(label
, sizeof(label
), "serial%d", index
);
2434 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2435 if (!serial_hds
[index
]) {
2436 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2437 devname
, strerror(errno
));
2444 static int parallel_parse(const char *devname
)
2446 static int index
= 0;
2449 if (strcmp(devname
, "none") == 0)
2451 if (index
== MAX_PARALLEL_PORTS
) {
2452 fprintf(stderr
, "qemu: too many parallel ports\n");
2455 snprintf(label
, sizeof(label
), "parallel%d", index
);
2456 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2457 if (!parallel_hds
[index
]) {
2458 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2459 devname
, strerror(errno
));
2466 static int virtcon_parse(const char *devname
)
2468 static int index
= 0;
2470 QemuOpts
*bus_opts
, *dev_opts
;
2472 if (strcmp(devname
, "none") == 0)
2474 if (index
== MAX_VIRTIO_CONSOLES
) {
2475 fprintf(stderr
, "qemu: too many virtio consoles\n");
2479 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2480 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2482 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2483 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2485 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2486 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2487 if (!virtcon_hds
[index
]) {
2488 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2489 devname
, strerror(errno
));
2492 qemu_opt_set(dev_opts
, "chardev", label
);
2498 static int debugcon_parse(const char *devname
)
2502 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2505 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2507 fprintf(stderr
, "qemu: already have a debugcon device\n");
2510 qemu_opt_set(opts
, "driver", "isa-debugcon");
2511 qemu_opt_set(opts
, "chardev", "debugcon");
2515 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2516 const char **poptarg
, int *poptind
)
2518 const QEMUOption
*popt
;
2519 int optind
= *poptind
;
2520 char *r
= argv
[optind
];
2523 loc_set_cmdline(argv
, optind
, 1);
2525 /* Treat --foo the same as -foo. */
2528 popt
= qemu_options
;
2531 error_report("invalid option");
2534 if (!strcmp(popt
->name
, r
+ 1))
2538 if (popt
->flags
& HAS_ARG
) {
2539 if (optind
>= argc
) {
2540 error_report("requires an argument");
2543 optarg
= argv
[optind
++];
2544 loc_set_cmdline(argv
, optind
- 2, 2);
2555 int main(int argc
, char **argv
, char **envp
)
2557 const char *gdbstub_dev
= NULL
;
2559 int snapshot
, linux_boot
;
2560 const char *icount_option
= NULL
;
2561 const char *initrd_filename
;
2562 const char *kernel_filename
, *kernel_cmdline
;
2563 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2565 DisplayChangeListener
*dcl
;
2566 int cyls
, heads
, secs
, translation
;
2567 QemuOpts
*hda_opts
= NULL
, *opts
;
2570 const char *loadvm
= NULL
;
2571 QEMUMachine
*machine
;
2572 const char *cpu_model
;
2577 const char *pid_file
= NULL
;
2578 const char *incoming
= NULL
;
2581 struct passwd
*pwd
= NULL
;
2582 const char *chroot_dir
= NULL
;
2583 const char *run_as
= NULL
;
2585 int show_vnc_port
= 0;
2588 error_set_progname(argv
[0]);
2592 qemu_cache_utils_init(envp
);
2594 QLIST_INIT (&vm_change_state_head
);
2597 struct sigaction act
;
2598 sigfillset(&act
.sa_mask
);
2600 act
.sa_handler
= SIG_IGN
;
2601 sigaction(SIGPIPE
, &act
, NULL
);
2604 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2605 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2606 QEMU to run on a single CPU */
2611 h
= GetCurrentProcess();
2612 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2613 for(i
= 0; i
< 32; i
++) {
2614 if (mask
& (1 << i
))
2619 SetProcessAffinityMask(h
, mask
);
2625 module_call_init(MODULE_INIT_MACHINE
);
2626 machine
= find_default_machine();
2628 initrd_filename
= NULL
;
2631 kernel_filename
= NULL
;
2632 kernel_cmdline
= "";
2633 cyls
= heads
= secs
= 0;
2634 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2636 for (i
= 0; i
< MAX_NODES
; i
++) {
2638 node_cpumask
[i
] = 0;
2647 /* first pass of option parsing */
2649 while (optind
< argc
) {
2650 if (argv
[optind
][0] != '-') {
2655 const QEMUOption
*popt
;
2657 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2658 switch (popt
->index
) {
2659 case QEMU_OPTION_nodefconfig
:
2670 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2671 fp
= fopen(fname
, "r");
2673 if (qemu_config_parse(fp
, fname
) != 0) {
2679 fname
= arch_config_name
;
2680 fp
= fopen(fname
, "r");
2682 if (qemu_config_parse(fp
, fname
) != 0) {
2690 /* second pass of option parsing */
2695 if (argv
[optind
][0] != '-') {
2696 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2698 const QEMUOption
*popt
;
2700 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2701 if (!(popt
->arch_mask
& arch_type
)) {
2702 printf("Option %s not supported for this target\n", popt
->name
);
2705 switch(popt
->index
) {
2707 machine
= find_machine(optarg
);
2710 printf("Supported machines are:\n");
2711 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2713 printf("%-10s %s (alias of %s)\n",
2714 m
->alias
, m
->desc
, m
->name
);
2715 printf("%-10s %s%s\n",
2717 m
->is_default
? " (default)" : "");
2719 exit(*optarg
!= '?');
2722 case QEMU_OPTION_cpu
:
2723 /* hw initialization will check this */
2724 if (*optarg
== '?') {
2725 /* XXX: implement xxx_cpu_list for targets that still miss it */
2726 #if defined(cpu_list_id)
2727 cpu_list_id(stdout
, &fprintf
, optarg
);
2728 #elif defined(cpu_list)
2729 cpu_list(stdout
, &fprintf
); /* deprecated */
2736 case QEMU_OPTION_initrd
:
2737 initrd_filename
= optarg
;
2739 case QEMU_OPTION_hda
:
2741 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2743 hda_opts
= drive_add(optarg
, HD_ALIAS
2744 ",cyls=%d,heads=%d,secs=%d%s",
2745 0, cyls
, heads
, secs
,
2746 translation
== BIOS_ATA_TRANSLATION_LBA
?
2748 translation
== BIOS_ATA_TRANSLATION_NONE
?
2749 ",trans=none" : "");
2751 case QEMU_OPTION_hdb
:
2752 case QEMU_OPTION_hdc
:
2753 case QEMU_OPTION_hdd
:
2754 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2756 case QEMU_OPTION_drive
:
2757 drive_add(NULL
, "%s", optarg
);
2759 case QEMU_OPTION_set
:
2760 if (qemu_set_option(optarg
) != 0)
2763 case QEMU_OPTION_global
:
2764 if (qemu_global_option(optarg
) != 0)
2767 case QEMU_OPTION_mtdblock
:
2768 drive_add(optarg
, MTD_ALIAS
);
2770 case QEMU_OPTION_sd
:
2771 drive_add(optarg
, SD_ALIAS
);
2773 case QEMU_OPTION_pflash
:
2774 drive_add(optarg
, PFLASH_ALIAS
);
2776 case QEMU_OPTION_snapshot
:
2779 case QEMU_OPTION_hdachs
:
2783 cyls
= strtol(p
, (char **)&p
, 0);
2784 if (cyls
< 1 || cyls
> 16383)
2789 heads
= strtol(p
, (char **)&p
, 0);
2790 if (heads
< 1 || heads
> 16)
2795 secs
= strtol(p
, (char **)&p
, 0);
2796 if (secs
< 1 || secs
> 63)
2800 if (!strcmp(p
, "none"))
2801 translation
= BIOS_ATA_TRANSLATION_NONE
;
2802 else if (!strcmp(p
, "lba"))
2803 translation
= BIOS_ATA_TRANSLATION_LBA
;
2804 else if (!strcmp(p
, "auto"))
2805 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2808 } else if (*p
!= '\0') {
2810 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2813 if (hda_opts
!= NULL
) {
2815 snprintf(num
, sizeof(num
), "%d", cyls
);
2816 qemu_opt_set(hda_opts
, "cyls", num
);
2817 snprintf(num
, sizeof(num
), "%d", heads
);
2818 qemu_opt_set(hda_opts
, "heads", num
);
2819 snprintf(num
, sizeof(num
), "%d", secs
);
2820 qemu_opt_set(hda_opts
, "secs", num
);
2821 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2822 qemu_opt_set(hda_opts
, "trans", "lba");
2823 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2824 qemu_opt_set(hda_opts
, "trans", "none");
2828 case QEMU_OPTION_numa
:
2829 if (nb_numa_nodes
>= MAX_NODES
) {
2830 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2835 case QEMU_OPTION_nographic
:
2836 display_type
= DT_NOGRAPHIC
;
2838 #ifdef CONFIG_CURSES
2839 case QEMU_OPTION_curses
:
2840 display_type
= DT_CURSES
;
2843 case QEMU_OPTION_portrait
:
2846 case QEMU_OPTION_kernel
:
2847 kernel_filename
= optarg
;
2849 case QEMU_OPTION_append
:
2850 kernel_cmdline
= optarg
;
2852 case QEMU_OPTION_cdrom
:
2853 drive_add(optarg
, CDROM_ALIAS
);
2855 case QEMU_OPTION_boot
:
2857 static const char * const params
[] = {
2858 "order", "once", "menu", NULL
2860 char buf
[sizeof(boot_devices
)];
2861 char *standard_boot_devices
;
2864 if (!strchr(optarg
, '=')) {
2866 pstrcpy(buf
, sizeof(buf
), optarg
);
2867 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2869 "qemu: unknown boot parameter '%s' in '%s'\n",
2875 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2876 validate_bootdevices(buf
);
2877 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2880 if (get_param_value(buf
, sizeof(buf
),
2882 validate_bootdevices(buf
);
2883 standard_boot_devices
= qemu_strdup(boot_devices
);
2884 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2885 qemu_register_reset(restore_boot_devices
,
2886 standard_boot_devices
);
2888 if (get_param_value(buf
, sizeof(buf
),
2890 if (!strcmp(buf
, "on")) {
2892 } else if (!strcmp(buf
, "off")) {
2896 "qemu: invalid option value '%s'\n",
2904 case QEMU_OPTION_fda
:
2905 case QEMU_OPTION_fdb
:
2906 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2908 case QEMU_OPTION_no_fd_bootchk
:
2911 case QEMU_OPTION_netdev
:
2912 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2916 case QEMU_OPTION_net
:
2917 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2922 case QEMU_OPTION_tftp
:
2923 legacy_tftp_prefix
= optarg
;
2925 case QEMU_OPTION_bootp
:
2926 legacy_bootp_filename
= optarg
;
2929 case QEMU_OPTION_smb
:
2930 if (net_slirp_smb(optarg
) < 0)
2934 case QEMU_OPTION_redir
:
2935 if (net_slirp_redir(optarg
) < 0)
2939 case QEMU_OPTION_bt
:
2940 add_device_config(DEV_BT
, optarg
);
2942 case QEMU_OPTION_audio_help
:
2943 if (!(audio_available())) {
2944 printf("Option %s not supported for this target\n", popt
->name
);
2950 case QEMU_OPTION_soundhw
:
2951 if (!(audio_available())) {
2952 printf("Option %s not supported for this target\n", popt
->name
);
2955 select_soundhw (optarg
);
2960 case QEMU_OPTION_version
:
2964 case QEMU_OPTION_m
: {
2968 value
= strtoul(optarg
, &ptr
, 10);
2970 case 0: case 'M': case 'm':
2977 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2981 /* On 32-bit hosts, QEMU is limited by virtual address space */
2982 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2983 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2986 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2987 fprintf(stderr
, "qemu: ram size too large\n");
2993 case QEMU_OPTION_mempath
:
2997 case QEMU_OPTION_mem_prealloc
:
3002 set_cpu_log(optarg
);
3005 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3007 case QEMU_OPTION_gdb
:
3008 gdbstub_dev
= optarg
;
3013 case QEMU_OPTION_bios
:
3016 case QEMU_OPTION_singlestep
:
3023 keyboard_layout
= optarg
;
3025 case QEMU_OPTION_localtime
:
3028 case QEMU_OPTION_vga
:
3029 select_vgahw (optarg
);
3036 w
= strtol(p
, (char **)&p
, 10);
3039 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3045 h
= strtol(p
, (char **)&p
, 10);
3050 depth
= strtol(p
, (char **)&p
, 10);
3051 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3052 depth
!= 24 && depth
!= 32)
3054 } else if (*p
== '\0') {
3055 depth
= graphic_depth
;
3062 graphic_depth
= depth
;
3065 case QEMU_OPTION_echr
:
3068 term_escape_char
= strtol(optarg
, &r
, 0);
3070 printf("Bad argument to echr\n");
3073 case QEMU_OPTION_monitor
:
3074 monitor_parse(optarg
, "readline");
3075 default_monitor
= 0;
3077 case QEMU_OPTION_qmp
:
3078 monitor_parse(optarg
, "control");
3079 default_monitor
= 0;
3081 case QEMU_OPTION_mon
:
3082 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3084 fprintf(stderr
, "parse error: %s\n", optarg
);
3087 default_monitor
= 0;
3089 case QEMU_OPTION_chardev
:
3090 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3092 fprintf(stderr
, "parse error: %s\n", optarg
);
3096 case QEMU_OPTION_serial
:
3097 add_device_config(DEV_SERIAL
, optarg
);
3099 if (strncmp(optarg
, "mon:", 4) == 0) {
3100 default_monitor
= 0;
3103 case QEMU_OPTION_watchdog
:
3106 "qemu: only one watchdog option may be given\n");
3111 case QEMU_OPTION_watchdog_action
:
3112 if (select_watchdog_action(optarg
) == -1) {
3113 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3117 case QEMU_OPTION_virtiocon
:
3118 add_device_config(DEV_VIRTCON
, optarg
);
3119 default_virtcon
= 0;
3120 if (strncmp(optarg
, "mon:", 4) == 0) {
3121 default_monitor
= 0;
3124 case QEMU_OPTION_parallel
:
3125 add_device_config(DEV_PARALLEL
, optarg
);
3126 default_parallel
= 0;
3127 if (strncmp(optarg
, "mon:", 4) == 0) {
3128 default_monitor
= 0;
3131 case QEMU_OPTION_debugcon
:
3132 add_device_config(DEV_DEBUGCON
, optarg
);
3134 case QEMU_OPTION_loadvm
:
3137 case QEMU_OPTION_full_screen
:
3141 case QEMU_OPTION_no_frame
:
3144 case QEMU_OPTION_alt_grab
:
3147 case QEMU_OPTION_ctrl_grab
:
3150 case QEMU_OPTION_no_quit
:
3153 case QEMU_OPTION_sdl
:
3154 display_type
= DT_SDL
;
3157 case QEMU_OPTION_pidfile
:
3160 case QEMU_OPTION_win2k_hack
:
3161 win2k_install_hack
= 1;
3163 case QEMU_OPTION_rtc_td_hack
:
3166 case QEMU_OPTION_acpitable
:
3167 do_acpitable_option(optarg
);
3169 case QEMU_OPTION_smbios
:
3170 do_smbios_option(optarg
);
3172 case QEMU_OPTION_enable_kvm
:
3173 if (!(kvm_available())) {
3174 printf("Option %s not supported for this target\n", popt
->name
);
3179 case QEMU_OPTION_usb
:
3182 case QEMU_OPTION_usbdevice
:
3184 add_device_config(DEV_USB
, optarg
);
3186 case QEMU_OPTION_device
:
3187 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3191 case QEMU_OPTION_smp
:
3194 fprintf(stderr
, "Invalid number of CPUs\n");
3197 if (max_cpus
< smp_cpus
) {
3198 fprintf(stderr
, "maxcpus must be equal to or greater than "
3202 if (max_cpus
> 255) {
3203 fprintf(stderr
, "Unsupported number of maxcpus\n");
3207 case QEMU_OPTION_vnc
:
3208 display_type
= DT_VNC
;
3209 vnc_display
= optarg
;
3211 case QEMU_OPTION_no_acpi
:
3214 case QEMU_OPTION_no_hpet
:
3217 case QEMU_OPTION_balloon
:
3218 if (balloon_parse(optarg
) < 0) {
3219 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3223 case QEMU_OPTION_no_reboot
:
3226 case QEMU_OPTION_no_shutdown
:
3229 case QEMU_OPTION_show_cursor
:
3232 case QEMU_OPTION_uuid
:
3233 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3234 fprintf(stderr
, "Fail to parse UUID string."
3235 " Wrong format.\n");
3240 case QEMU_OPTION_daemonize
:
3244 case QEMU_OPTION_option_rom
:
3245 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3246 fprintf(stderr
, "Too many option ROMs\n");
3249 option_rom
[nb_option_roms
] = optarg
;
3252 case QEMU_OPTION_semihosting
:
3253 semihosting_enabled
= 1;
3255 case QEMU_OPTION_name
:
3256 qemu_name
= qemu_strdup(optarg
);
3258 char *p
= strchr(qemu_name
, ',');
3261 if (strncmp(p
, "process=", 8)) {
3262 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3270 case QEMU_OPTION_prom_env
:
3271 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3272 fprintf(stderr
, "Too many prom variables\n");
3275 prom_envs
[nb_prom_envs
] = optarg
;
3278 case QEMU_OPTION_old_param
:
3281 case QEMU_OPTION_clock
:
3282 configure_alarms(optarg
);
3284 case QEMU_OPTION_startdate
:
3285 configure_rtc_date_offset(optarg
, 1);
3287 case QEMU_OPTION_rtc
:
3288 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3290 fprintf(stderr
, "parse error: %s\n", optarg
);
3293 configure_rtc(opts
);
3295 case QEMU_OPTION_tb_size
:
3296 tb_size
= strtol(optarg
, NULL
, 0);
3300 case QEMU_OPTION_icount
:
3301 icount_option
= optarg
;
3303 case QEMU_OPTION_incoming
:
3306 case QEMU_OPTION_nodefaults
:
3308 default_parallel
= 0;
3309 default_virtcon
= 0;
3310 default_monitor
= 0;
3318 case QEMU_OPTION_chroot
:
3319 chroot_dir
= optarg
;
3321 case QEMU_OPTION_runas
:
3325 case QEMU_OPTION_xen_domid
:
3326 if (!(xen_available())) {
3327 printf("Option %s not supported for this target\n", popt
->name
);
3330 xen_domid
= atoi(optarg
);
3332 case QEMU_OPTION_xen_create
:
3333 if (!(xen_available())) {
3334 printf("Option %s not supported for this target\n", popt
->name
);
3337 xen_mode
= XEN_CREATE
;
3339 case QEMU_OPTION_xen_attach
:
3340 if (!(xen_available())) {
3341 printf("Option %s not supported for this target\n", popt
->name
);
3344 xen_mode
= XEN_ATTACH
;
3346 case QEMU_OPTION_readconfig
:
3349 fp
= fopen(optarg
, "r");
3351 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3354 if (qemu_config_parse(fp
, optarg
) != 0) {
3360 case QEMU_OPTION_writeconfig
:
3363 if (strcmp(optarg
, "-") == 0) {
3366 fp
= fopen(optarg
, "w");
3368 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3372 qemu_config_write(fp
);
3381 /* If no data_dir is specified then try to find it relative to the
3384 data_dir
= find_datadir(argv
[0]);
3386 /* If all else fails use the install patch specified when building. */
3388 data_dir
= CONFIG_QEMU_SHAREDIR
;
3392 * Default to max_cpus = smp_cpus, in case the user doesn't
3393 * specify a max_cpus value.
3396 max_cpus
= smp_cpus
;
3398 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3399 if (smp_cpus
> machine
->max_cpus
) {
3400 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3401 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3406 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3407 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3409 if (machine
->no_serial
) {
3412 if (machine
->no_parallel
) {
3413 default_parallel
= 0;
3415 if (!machine
->use_virtcon
) {
3416 default_virtcon
= 0;
3418 if (machine
->no_vga
) {
3421 if (machine
->no_floppy
) {
3424 if (machine
->no_cdrom
) {
3427 if (machine
->no_sdcard
) {
3431 if (display_type
== DT_NOGRAPHIC
) {
3432 if (default_parallel
)
3433 add_device_config(DEV_PARALLEL
, "null");
3434 if (default_serial
&& default_monitor
) {
3435 add_device_config(DEV_SERIAL
, "mon:stdio");
3436 } else if (default_virtcon
&& default_monitor
) {
3437 add_device_config(DEV_VIRTCON
, "mon:stdio");
3440 add_device_config(DEV_SERIAL
, "stdio");
3441 if (default_virtcon
)
3442 add_device_config(DEV_VIRTCON
, "stdio");
3443 if (default_monitor
)
3444 monitor_parse("stdio", "readline");
3448 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3449 if (default_parallel
)
3450 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3451 if (default_monitor
)
3452 monitor_parse("vc:80Cx24C", "readline");
3453 if (default_virtcon
)
3454 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3457 vga_interface_type
= VGA_CIRRUS
;
3459 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3466 if (pipe(fds
) == -1)
3477 len
= read(fds
[0], &status
, 1);
3478 if (len
== -1 && (errno
== EINTR
))
3483 else if (status
== 1) {
3484 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3492 qemu_set_cloexec(fds
[1]);
3504 signal(SIGTSTP
, SIG_IGN
);
3505 signal(SIGTTOU
, SIG_IGN
);
3506 signal(SIGTTIN
, SIG_IGN
);
3510 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3514 if (write(fds
[1], &status
, 1) != 1) {
3515 perror("daemonize. Writing to pipe\n");
3519 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3523 if (kvm_enabled()) {
3526 ret
= kvm_init(smp_cpus
);
3528 fprintf(stderr
, "failed to initialize KVM\n");
3533 if (qemu_init_main_loop()) {
3534 fprintf(stderr
, "qemu_init_main_loop failed\n");
3537 linux_boot
= (kernel_filename
!= NULL
);
3539 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3540 fprintf(stderr
, "-append only allowed with -kernel option\n");
3544 if (!linux_boot
&& initrd_filename
!= NULL
) {
3545 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3550 /* Win32 doesn't support line-buffering and requires size >= 2 */
3551 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3554 if (init_timer_alarm() < 0) {
3555 fprintf(stderr
, "could not initialize alarm timer\n");
3558 configure_icount(icount_option
);
3562 if (net_init_clients() < 0) {
3566 /* init the bluetooth world */
3567 if (foreach_device_config(DEV_BT
, bt_parse
))
3570 /* init the memory */
3572 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3574 /* init the dynamic translator */
3575 cpu_exec_init_all(tb_size
* 1024 * 1024);
3577 bdrv_init_with_whitelist();
3581 if (default_cdrom
) {
3582 /* we always create the cdrom drive, even if no disk is there */
3583 drive_add(NULL
, CDROM_ALIAS
);
3586 if (default_floppy
) {
3587 /* we always create at least one floppy */
3588 drive_add(NULL
, FD_ALIAS
, 0);
3591 if (default_sdcard
) {
3592 /* we always create one sd slot, even if no card is in it */
3593 drive_add(NULL
, SD_ALIAS
);
3596 /* open the virtual block devices */
3598 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3599 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3602 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3605 if (nb_numa_nodes
> 0) {
3608 if (nb_numa_nodes
> smp_cpus
) {
3609 nb_numa_nodes
= smp_cpus
;
3612 /* If no memory size if given for any node, assume the default case
3613 * and distribute the available memory equally across all nodes
3615 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3616 if (node_mem
[i
] != 0)
3619 if (i
== nb_numa_nodes
) {
3620 uint64_t usedmem
= 0;
3622 /* On Linux, the each node's border has to be 8MB aligned,
3623 * the final node gets the rest.
3625 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3626 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3627 usedmem
+= node_mem
[i
];
3629 node_mem
[i
] = ram_size
- usedmem
;
3632 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3633 if (node_cpumask
[i
] != 0)
3636 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3637 * must cope with this anyway, because there are BIOSes out there in
3638 * real machines which also use this scheme.
3640 if (i
== nb_numa_nodes
) {
3641 for (i
= 0; i
< smp_cpus
; i
++) {
3642 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3647 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3649 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3651 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3653 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3656 module_call_init(MODULE_INIT_DEVICE
);
3658 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3662 i
= select_watchdog(watchdog
);
3664 exit (i
== 1 ? 1 : 0);
3667 if (machine
->compat_props
) {
3668 qdev_prop_register_global_list(machine
->compat_props
);
3672 machine
->init(ram_size
, boot_devices
,
3673 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3675 cpu_synchronize_all_post_init();
3678 /* must be after terminal init, SDL library changes signal handlers */
3684 current_machine
= machine
;
3686 /* init USB devices */
3688 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3692 /* init generic devices */
3693 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3696 net_check_clients();
3698 /* just use the first displaystate for the moment */
3699 ds
= get_displaystate();
3701 if (display_type
== DT_DEFAULT
) {
3702 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3703 display_type
= DT_SDL
;
3705 display_type
= DT_VNC
;
3706 vnc_display
= "localhost:0,to=99";
3712 switch (display_type
) {
3715 #if defined(CONFIG_CURSES)
3717 curses_display_init(ds
, full_screen
);
3720 #if defined(CONFIG_SDL)
3722 sdl_display_init(ds
, full_screen
, no_frame
);
3724 #elif defined(CONFIG_COCOA)
3726 cocoa_display_init(ds
, full_screen
);
3730 vnc_display_init(ds
);
3731 if (vnc_display_open(ds
, vnc_display
) < 0)
3734 if (show_vnc_port
) {
3735 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3743 dcl
= ds
->listeners
;
3744 while (dcl
!= NULL
) {
3745 if (dcl
->dpy_refresh
!= NULL
) {
3746 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3747 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3752 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3753 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3754 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3757 text_consoles_set_display(ds
);
3759 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3762 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3763 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3768 qdev_machine_creation_done();
3770 if (rom_load_all() != 0) {
3771 fprintf(stderr
, "rom loading failed\n");
3775 qemu_system_reset();
3777 if (load_vmstate(loadvm
) < 0) {
3783 qemu_start_incoming_migration(incoming
);
3784 } else if (autostart
) {
3794 len
= write(fds
[1], &status
, 1);
3795 if (len
== -1 && (errno
== EINTR
))
3802 perror("not able to chdir to /");
3805 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3811 pwd
= getpwnam(run_as
);
3813 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3819 if (chroot(chroot_dir
) < 0) {
3820 fprintf(stderr
, "chroot failed\n");
3824 perror("not able to chdir to /");
3830 if (setgid(pwd
->pw_gid
) < 0) {
3831 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3834 if (setuid(pwd
->pw_uid
) < 0) {
3835 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3838 if (setuid(0) != -1) {
3839 fprintf(stderr
, "Dropping privileges failed\n");