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__)
62 #include <sys/prctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
81 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83 extern int madvise(caddr_t
, size_t, int);
88 #if defined(__OpenBSD__)
92 #if defined(CONFIG_VDE)
93 #include <libvdeplug.h>
101 #if defined(__APPLE__) || defined(main)
103 int qemu_main(int argc
, char **argv
, char **envp
);
104 int main(int argc
, char **argv
)
106 return qemu_main(argc
, argv
, NULL
);
109 #define main qemu_main
111 #endif /* CONFIG_SDL */
115 #define main qemu_main
116 #endif /* CONFIG_COCOA */
119 #include "hw/boards.h"
121 #include "hw/pcmcia.h"
126 #include "hw/watchdog.h"
127 #include "hw/smbios.h"
130 #include "hw/loader.h"
133 #include "net/slirp.h"
138 #include "qemu-timer.h"
139 #include "qemu-char.h"
140 #include "cache-utils.h"
142 #include "block_int.h"
143 #include "block-migration.h"
145 #include "audio/audio.h"
146 #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 /***********************************************************/
294 /* x86 ISA bus support */
296 target_phys_addr_t isa_mem_base
= 0;
299 static void set_proc_name(const char *s
)
301 #if defined(__linux__) && defined(PR_SET_NAME)
305 name
[sizeof(name
) - 1] = 0;
306 strncpy(name
, s
, sizeof(name
));
307 /* Could rewrite argv[0] too, but that's a bit more complicated.
308 This simple way is enough for `top'. */
309 prctl(PR_SET_NAME
, name
);
316 static QEMUBalloonEvent
*qemu_balloon_event
;
317 void *qemu_balloon_event_opaque
;
319 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
321 qemu_balloon_event
= func
;
322 qemu_balloon_event_opaque
= opaque
;
325 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
327 if (qemu_balloon_event
) {
328 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
335 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
337 if (qemu_balloon_event
) {
338 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
346 /***********************************************************/
347 /* real time host monotonic timer */
349 /* compute with 96 bit intermediate result: (a*b)/c */
350 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
355 #ifdef HOST_WORDS_BIGENDIAN
365 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
366 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
369 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
373 /***********************************************************/
374 /* host time/date access */
375 void qemu_get_timedate(struct tm
*tm
, int offset
)
382 if (rtc_date_offset
== -1) {
386 ret
= localtime(&ti
);
388 ti
-= rtc_date_offset
;
392 memcpy(tm
, ret
, sizeof(struct tm
));
395 int qemu_timedate_diff(struct tm
*tm
)
399 if (rtc_date_offset
== -1)
401 seconds
= mktimegm(tm
);
403 seconds
= mktime(tm
);
405 seconds
= mktimegm(tm
) + rtc_date_offset
;
407 return seconds
- time(NULL
);
410 void rtc_change_mon_event(struct tm
*tm
)
414 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
415 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
416 qobject_decref(data
);
419 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
421 time_t rtc_start_date
;
424 if (!strcmp(startdate
, "now") && legacy
) {
425 rtc_date_offset
= -1;
427 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
435 } else if (sscanf(startdate
, "%d-%d-%d",
447 rtc_start_date
= mktimegm(&tm
);
448 if (rtc_start_date
== -1) {
450 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
451 "'2006-06-17T16:01:21' or '2006-06-17'\n");
454 rtc_date_offset
= time(NULL
) - rtc_start_date
;
458 static void configure_rtc(QemuOpts
*opts
)
462 value
= qemu_opt_get(opts
, "base");
464 if (!strcmp(value
, "utc")) {
466 } else if (!strcmp(value
, "localtime")) {
469 configure_rtc_date_offset(value
, 0);
472 value
= qemu_opt_get(opts
, "clock");
474 if (!strcmp(value
, "host")) {
475 rtc_clock
= host_clock
;
476 } else if (!strcmp(value
, "vm")) {
477 rtc_clock
= vm_clock
;
479 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
483 value
= qemu_opt_get(opts
, "driftfix");
485 if (!strcmp(value
, "slew")) {
487 } else if (!strcmp(value
, "none")) {
490 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
497 static void socket_cleanup(void)
502 static int socket_init(void)
507 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
509 err
= WSAGetLastError();
510 fprintf(stderr
, "WSAStartup: %d\n", err
);
513 atexit(socket_cleanup
);
518 /***********************************************************/
519 /* Bluetooth support */
522 static struct HCIInfo
*hci_table
[MAX_NICS
];
524 static struct bt_vlan_s
{
525 struct bt_scatternet_s net
;
527 struct bt_vlan_s
*next
;
530 /* find or alloc a new bluetooth "VLAN" */
531 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
533 struct bt_vlan_s
**pvlan
, *vlan
;
534 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
538 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
540 pvlan
= &first_bt_vlan
;
541 while (*pvlan
!= NULL
)
542 pvlan
= &(*pvlan
)->next
;
547 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
551 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
556 static struct HCIInfo null_hci
= {
557 .cmd_send
= null_hci_send
,
558 .sco_send
= null_hci_send
,
559 .acl_send
= null_hci_send
,
560 .bdaddr_set
= null_hci_addr_set
,
563 struct HCIInfo
*qemu_next_hci(void)
565 if (cur_hci
== nb_hcis
)
568 return hci_table
[cur_hci
++];
571 static struct HCIInfo
*hci_init(const char *str
)
574 struct bt_scatternet_s
*vlan
= 0;
576 if (!strcmp(str
, "null"))
579 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
581 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
582 else if (!strncmp(str
, "hci", 3)) {
585 if (!strncmp(str
+ 3, ",vlan=", 6)) {
586 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
591 vlan
= qemu_find_bt_vlan(0);
593 return bt_new_hci(vlan
);
596 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
601 static int bt_hci_parse(const char *str
)
606 if (nb_hcis
>= MAX_NICS
) {
607 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
620 bdaddr
.b
[5] = 0x56 + nb_hcis
;
621 hci
->bdaddr_set(hci
, bdaddr
.b
);
623 hci_table
[nb_hcis
++] = hci
;
628 static void bt_vhci_add(int vlan_id
)
630 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
633 fprintf(stderr
, "qemu: warning: adding a VHCI to "
634 "an empty scatternet %i\n", vlan_id
);
636 bt_vhci_init(bt_new_hci(vlan
));
639 static struct bt_device_s
*bt_device_add(const char *opt
)
641 struct bt_scatternet_s
*vlan
;
643 char *endp
= strstr(opt
, ",vlan=");
644 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
647 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
650 vlan_id
= strtol(endp
+ 6, &endp
, 0);
652 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
657 vlan
= qemu_find_bt_vlan(vlan_id
);
660 fprintf(stderr
, "qemu: warning: adding a slave device to "
661 "an empty scatternet %i\n", vlan_id
);
663 if (!strcmp(devname
, "keyboard"))
664 return bt_keyboard_init(vlan
);
666 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
670 static int bt_parse(const char *opt
)
672 const char *endp
, *p
;
675 if (strstart(opt
, "hci", &endp
)) {
676 if (!*endp
|| *endp
== ',') {
678 if (!strstart(endp
, ",vlan=", 0))
681 return bt_hci_parse(opt
);
683 } else if (strstart(opt
, "vhci", &endp
)) {
684 if (!*endp
|| *endp
== ',') {
686 if (strstart(endp
, ",vlan=", &p
)) {
687 vlan
= strtol(p
, (char **) &endp
, 0);
689 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
693 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
702 } else if (strstart(opt
, "device:", &endp
))
703 return !bt_device_add(endp
);
705 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
709 /***********************************************************/
710 /* QEMU Block devices */
712 #define HD_ALIAS "index=%d,media=disk"
713 #define CDROM_ALIAS "index=2,media=cdrom"
714 #define FD_ALIAS "index=%d,if=floppy"
715 #define PFLASH_ALIAS "if=pflash"
716 #define MTD_ALIAS "if=mtd"
717 #define SD_ALIAS "index=0,if=sd"
719 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
726 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
729 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
731 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
732 __FUNCTION__
, optstr
);
736 qemu_opt_set(opts
, "file", file
);
740 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
744 /* seek interface, bus and unit */
746 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
747 if (dinfo
->type
== type
&&
756 DriveInfo
*drive_get_by_id(const char *id
)
760 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
761 if (strcmp(id
, dinfo
->id
))
768 int drive_get_max_bus(BlockInterfaceType type
)
774 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
775 if(dinfo
->type
== type
&&
776 dinfo
->bus
> max_bus
)
777 max_bus
= dinfo
->bus
;
782 const char *drive_get_serial(BlockDriverState
*bdrv
)
786 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
787 if (dinfo
->bdrv
== bdrv
)
788 return dinfo
->serial
;
794 BlockInterfaceErrorAction
drive_get_on_error(
795 BlockDriverState
*bdrv
, int is_read
)
799 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
800 if (dinfo
->bdrv
== bdrv
)
801 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
804 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
807 static void bdrv_format_print(void *opaque
, const char *name
)
809 fprintf(stderr
, " %s", name
);
812 void drive_uninit(DriveInfo
*dinfo
)
814 qemu_opts_del(dinfo
->opts
);
815 bdrv_delete(dinfo
->bdrv
);
816 QTAILQ_REMOVE(&drives
, dinfo
, next
);
820 static int parse_block_error_action(const char *buf
, int is_read
)
822 if (!strcmp(buf
, "ignore")) {
823 return BLOCK_ERR_IGNORE
;
824 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
825 return BLOCK_ERR_STOP_ENOSPC
;
826 } else if (!strcmp(buf
, "stop")) {
827 return BLOCK_ERR_STOP_ANY
;
828 } else if (!strcmp(buf
, "report")) {
829 return BLOCK_ERR_REPORT
;
831 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
832 buf
, is_read
? "read" : "write");
837 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
841 const char *file
= NULL
;
844 const char *mediastr
= "";
845 BlockInterfaceType type
;
846 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
848 int cyls
, heads
, secs
, translation
;
849 BlockDriver
*drv
= NULL
;
850 QEMUMachine
*machine
= opaque
;
857 int on_read_error
, on_write_error
;
864 translation
= BIOS_ATA_TRANSLATION_AUTO
;
867 if (machine
&& machine
->use_scsi
) {
869 max_devs
= MAX_SCSI_DEVS
;
870 pstrcpy(devname
, sizeof(devname
), "scsi");
873 max_devs
= MAX_IDE_DEVS
;
874 pstrcpy(devname
, sizeof(devname
), "ide");
878 /* extract parameters */
879 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
880 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
881 index
= qemu_opt_get_number(opts
, "index", -1);
883 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
884 heads
= qemu_opt_get_number(opts
, "heads", 0);
885 secs
= qemu_opt_get_number(opts
, "secs", 0);
887 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
888 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
890 file
= qemu_opt_get(opts
, "file");
891 serial
= qemu_opt_get(opts
, "serial");
893 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
894 pstrcpy(devname
, sizeof(devname
), buf
);
895 if (!strcmp(buf
, "ide")) {
897 max_devs
= MAX_IDE_DEVS
;
898 } else if (!strcmp(buf
, "scsi")) {
900 max_devs
= MAX_SCSI_DEVS
;
901 } else if (!strcmp(buf
, "floppy")) {
904 } else if (!strcmp(buf
, "pflash")) {
907 } else if (!strcmp(buf
, "mtd")) {
910 } else if (!strcmp(buf
, "sd")) {
913 } else if (!strcmp(buf
, "virtio")) {
916 } else if (!strcmp(buf
, "xen")) {
919 } else if (!strcmp(buf
, "none")) {
923 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
928 if (cyls
|| heads
|| secs
) {
929 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
930 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
933 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
934 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
937 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
938 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
943 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
946 "qemu: '%s' trans must be used with cyls,heads and secs\n",
950 if (!strcmp(buf
, "none"))
951 translation
= BIOS_ATA_TRANSLATION_NONE
;
952 else if (!strcmp(buf
, "lba"))
953 translation
= BIOS_ATA_TRANSLATION_LBA
;
954 else if (!strcmp(buf
, "auto"))
955 translation
= BIOS_ATA_TRANSLATION_AUTO
;
957 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
962 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
963 if (!strcmp(buf
, "disk")) {
965 } else if (!strcmp(buf
, "cdrom")) {
966 if (cyls
|| secs
|| heads
) {
968 "qemu: '%s' invalid physical CHS format\n", buf
);
973 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
978 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
979 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
981 else if (!strcmp(buf
, "writethrough"))
983 else if (!strcmp(buf
, "writeback"))
986 fprintf(stderr
, "qemu: invalid cache option\n");
991 #ifdef CONFIG_LINUX_AIO
992 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
993 if (!strcmp(buf
, "threads"))
995 else if (!strcmp(buf
, "native"))
998 fprintf(stderr
, "qemu: invalid aio option\n");
1004 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1005 if (strcmp(buf
, "?") == 0) {
1006 fprintf(stderr
, "qemu: Supported formats:");
1007 bdrv_iterate_format(bdrv_format_print
, NULL
);
1008 fprintf(stderr
, "\n");
1011 drv
= bdrv_find_whitelisted_format(buf
);
1013 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1018 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1019 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1020 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1021 fprintf(stderr
, "werror is no supported by this format\n");
1025 on_write_error
= parse_block_error_action(buf
, 0);
1026 if (on_write_error
< 0) {
1031 on_read_error
= BLOCK_ERR_REPORT
;
1032 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1033 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1034 fprintf(stderr
, "rerror is no supported by this format\n");
1038 on_read_error
= parse_block_error_action(buf
, 1);
1039 if (on_read_error
< 0) {
1044 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1045 if (type
!= IF_VIRTIO
) {
1046 fprintf(stderr
, "addr is not supported\n");
1051 /* compute bus and unit according index */
1054 if (bus_id
!= 0 || unit_id
!= -1) {
1056 "qemu: index cannot be used with bus and unit\n");
1064 unit_id
= index
% max_devs
;
1065 bus_id
= index
/ max_devs
;
1069 /* if user doesn't specify a unit_id,
1070 * try to find the first free
1073 if (unit_id
== -1) {
1075 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1077 if (max_devs
&& unit_id
>= max_devs
) {
1078 unit_id
-= max_devs
;
1086 if (max_devs
&& unit_id
>= max_devs
) {
1087 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1088 unit_id
, max_devs
- 1);
1093 * ignore multiple definitions
1096 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1103 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1104 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1105 dinfo
->id
= qemu_strdup(buf
);
1107 /* no id supplied -> create one */
1108 dinfo
->id
= qemu_mallocz(32);
1109 if (type
== IF_IDE
|| type
== IF_SCSI
)
1110 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1112 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1113 devname
, bus_id
, mediastr
, unit_id
);
1115 snprintf(dinfo
->id
, 32, "%s%s%i",
1116 devname
, mediastr
, unit_id
);
1118 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1119 dinfo
->devaddr
= devaddr
;
1121 dinfo
->bus
= bus_id
;
1122 dinfo
->unit
= unit_id
;
1123 dinfo
->on_read_error
= on_read_error
;
1124 dinfo
->on_write_error
= on_write_error
;
1127 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1128 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1138 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1139 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1143 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1148 /* FIXME: This isn't really a floppy, but it's a reasonable
1151 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1157 /* add virtio block device */
1158 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1159 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1160 qemu_opt_set(opts
, "drive", dinfo
->id
);
1162 qemu_opt_set(opts
, "addr", devaddr
);
1173 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1174 cache
= 2; /* always use write-back with snapshot */
1176 if (cache
== 0) /* no caching */
1177 bdrv_flags
|= BDRV_O_NOCACHE
;
1178 else if (cache
== 2) /* write-back */
1179 bdrv_flags
|= BDRV_O_CACHE_WB
;
1182 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1184 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1188 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1189 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1194 * cdrom is read-only. Set it now, after above interface checking
1195 * since readonly attribute not explicitly required, so no error.
1197 if (media
== MEDIA_CDROM
) {
1200 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1202 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1203 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1204 file
, strerror(errno
));
1208 if (bdrv_key_required(dinfo
->bdrv
))
1214 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1216 QEMUMachine
*machine
= opaque
;
1217 int fatal_error
= 0;
1219 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1226 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1228 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1229 qemu_opt_set(opts
, "snapshot", "on");
1234 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1236 boot_set_handler
= func
;
1237 boot_set_opaque
= opaque
;
1240 int qemu_boot_set(const char *boot_devices
)
1242 if (!boot_set_handler
) {
1245 return boot_set_handler(boot_set_opaque
, boot_devices
);
1248 static int parse_bootdevices(char *devices
)
1250 /* We just do some generic consistency checks */
1254 for (p
= devices
; *p
!= '\0'; p
++) {
1255 /* Allowed boot devices are:
1256 * a-b: floppy disk drives
1257 * c-f: IDE disk drives
1258 * g-m: machine implementation dependant drives
1259 * n-p: network devices
1260 * It's up to each machine implementation to check if the given boot
1261 * devices match the actual hardware implementation and firmware
1264 if (*p
< 'a' || *p
> 'p') {
1265 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1268 if (bitmap
& (1 << (*p
- 'a'))) {
1269 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1272 bitmap
|= 1 << (*p
- 'a');
1277 static void restore_boot_devices(void *opaque
)
1279 char *standard_boot_devices
= opaque
;
1281 qemu_boot_set(standard_boot_devices
);
1283 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1284 qemu_free(standard_boot_devices
);
1287 static void numa_add(const char *optarg
)
1291 unsigned long long value
, endvalue
;
1294 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1295 if (!strcmp(option
, "node")) {
1296 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1297 nodenr
= nb_numa_nodes
;
1299 nodenr
= strtoull(option
, NULL
, 10);
1302 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1303 node_mem
[nodenr
] = 0;
1305 value
= strtoull(option
, &endptr
, 0);
1307 case 0: case 'M': case 'm':
1314 node_mem
[nodenr
] = value
;
1316 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1317 node_cpumask
[nodenr
] = 0;
1319 value
= strtoull(option
, &endptr
, 10);
1322 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1324 if (*endptr
== '-') {
1325 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1326 if (endvalue
>= 63) {
1329 "only 63 CPUs in NUMA mode supported.\n");
1331 value
= (2ULL << endvalue
) - (1ULL << value
);
1333 value
= 1ULL << value
;
1336 node_cpumask
[nodenr
] = value
;
1343 static void smp_parse(const char *optarg
)
1345 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1349 smp
= strtoul(optarg
, &endptr
, 10);
1350 if (endptr
!= optarg
) {
1351 if (*endptr
== ',') {
1355 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1356 sockets
= strtoull(option
, NULL
, 10);
1357 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1358 cores
= strtoull(option
, NULL
, 10);
1359 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1360 threads
= strtoull(option
, NULL
, 10);
1361 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1362 max_cpus
= strtoull(option
, NULL
, 10);
1364 /* compute missing values, prefer sockets over cores over threads */
1365 if (smp
== 0 || sockets
== 0) {
1366 sockets
= sockets
> 0 ? sockets
: 1;
1367 cores
= cores
> 0 ? cores
: 1;
1368 threads
= threads
> 0 ? threads
: 1;
1370 smp
= cores
* threads
* sockets
;
1374 threads
= threads
> 0 ? threads
: 1;
1375 cores
= smp
/ (sockets
* threads
);
1378 threads
= smp
/ (cores
* sockets
);
1383 smp_cores
= cores
> 0 ? cores
: 1;
1384 smp_threads
= threads
> 0 ? threads
: 1;
1386 max_cpus
= smp_cpus
;
1389 /***********************************************************/
1392 static int usb_device_add(const char *devname
, int is_hotplug
)
1395 USBDevice
*dev
= NULL
;
1400 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1401 dev
= usbdevice_create(devname
);
1405 /* the other ones */
1406 if (strstart(devname
, "host:", &p
)) {
1407 dev
= usb_host_device_open(p
);
1408 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1409 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1410 bt_new_hci(qemu_find_bt_vlan(0)));
1421 static int usb_device_del(const char *devname
)
1426 if (strstart(devname
, "host:", &p
))
1427 return usb_host_device_close(p
);
1432 p
= strchr(devname
, '.');
1435 bus_num
= strtoul(devname
, NULL
, 0);
1436 addr
= strtoul(p
+ 1, NULL
, 0);
1438 return usb_device_delete_addr(bus_num
, addr
);
1441 static int usb_parse(const char *cmdline
)
1444 r
= usb_device_add(cmdline
, 0);
1446 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1451 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1453 const char *devname
= qdict_get_str(qdict
, "devname");
1454 if (usb_device_add(devname
, 1) < 0) {
1455 error_report("could not add USB device '%s'", devname
);
1459 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1461 const char *devname
= qdict_get_str(qdict
, "devname");
1462 if (usb_device_del(devname
) < 0) {
1463 error_report("could not delete USB device '%s'", devname
);
1467 /***********************************************************/
1468 /* PCMCIA/Cardbus */
1470 static struct pcmcia_socket_entry_s
{
1471 PCMCIASocket
*socket
;
1472 struct pcmcia_socket_entry_s
*next
;
1473 } *pcmcia_sockets
= 0;
1475 void pcmcia_socket_register(PCMCIASocket
*socket
)
1477 struct pcmcia_socket_entry_s
*entry
;
1479 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1480 entry
->socket
= socket
;
1481 entry
->next
= pcmcia_sockets
;
1482 pcmcia_sockets
= entry
;
1485 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1487 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1489 ptr
= &pcmcia_sockets
;
1490 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1491 if (entry
->socket
== socket
) {
1497 void pcmcia_info(Monitor
*mon
)
1499 struct pcmcia_socket_entry_s
*iter
;
1501 if (!pcmcia_sockets
)
1502 monitor_printf(mon
, "No PCMCIA sockets\n");
1504 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1505 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1506 iter
->socket
->attached
? iter
->socket
->card_string
:
1510 /***********************************************************/
1513 typedef struct IOHandlerRecord
{
1515 IOCanReadHandler
*fd_read_poll
;
1517 IOHandler
*fd_write
;
1520 /* temporary data */
1522 QLIST_ENTRY(IOHandlerRecord
) next
;
1525 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1526 QLIST_HEAD_INITIALIZER(io_handlers
);
1529 /* XXX: fd_read_poll should be suppressed, but an API change is
1530 necessary in the character devices to suppress fd_can_read(). */
1531 int qemu_set_fd_handler2(int fd
,
1532 IOCanReadHandler
*fd_read_poll
,
1534 IOHandler
*fd_write
,
1537 IOHandlerRecord
*ioh
;
1539 if (!fd_read
&& !fd_write
) {
1540 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1541 if (ioh
->fd
== fd
) {
1547 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1551 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1552 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1555 ioh
->fd_read_poll
= fd_read_poll
;
1556 ioh
->fd_read
= fd_read
;
1557 ioh
->fd_write
= fd_write
;
1558 ioh
->opaque
= opaque
;
1564 int qemu_set_fd_handler(int fd
,
1566 IOHandler
*fd_write
,
1569 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1573 /***********************************************************/
1574 /* Polling handling */
1576 typedef struct PollingEntry
{
1579 struct PollingEntry
*next
;
1582 static PollingEntry
*first_polling_entry
;
1584 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1586 PollingEntry
**ppe
, *pe
;
1587 pe
= qemu_mallocz(sizeof(PollingEntry
));
1589 pe
->opaque
= opaque
;
1590 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1595 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1597 PollingEntry
**ppe
, *pe
;
1598 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1600 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1608 /***********************************************************/
1609 /* Wait objects support */
1610 typedef struct WaitObjects
{
1612 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1613 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1614 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1617 static WaitObjects wait_objects
= {0};
1619 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1621 WaitObjects
*w
= &wait_objects
;
1623 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1625 w
->events
[w
->num
] = handle
;
1626 w
->func
[w
->num
] = func
;
1627 w
->opaque
[w
->num
] = opaque
;
1632 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1635 WaitObjects
*w
= &wait_objects
;
1638 for (i
= 0; i
< w
->num
; i
++) {
1639 if (w
->events
[i
] == handle
)
1642 w
->events
[i
] = w
->events
[i
+ 1];
1643 w
->func
[i
] = w
->func
[i
+ 1];
1644 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1652 /***********************************************************/
1653 /* machine registration */
1655 static QEMUMachine
*first_machine
= NULL
;
1656 QEMUMachine
*current_machine
= NULL
;
1658 int qemu_register_machine(QEMUMachine
*m
)
1661 pm
= &first_machine
;
1669 static QEMUMachine
*find_machine(const char *name
)
1673 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1674 if (!strcmp(m
->name
, name
))
1676 if (m
->alias
&& !strcmp(m
->alias
, name
))
1682 static QEMUMachine
*find_default_machine(void)
1686 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1687 if (m
->is_default
) {
1694 /***********************************************************/
1695 /* main execution loop */
1697 static void gui_update(void *opaque
)
1699 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1700 DisplayState
*ds
= opaque
;
1701 DisplayChangeListener
*dcl
= ds
->listeners
;
1703 qemu_flush_coalesced_mmio_buffer();
1706 while (dcl
!= NULL
) {
1707 if (dcl
->gui_timer_interval
&&
1708 dcl
->gui_timer_interval
< interval
)
1709 interval
= dcl
->gui_timer_interval
;
1712 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1715 static void nographic_update(void *opaque
)
1717 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1719 qemu_flush_coalesced_mmio_buffer();
1720 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1723 struct vm_change_state_entry
{
1724 VMChangeStateHandler
*cb
;
1726 QLIST_ENTRY (vm_change_state_entry
) entries
;
1729 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1731 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1734 VMChangeStateEntry
*e
;
1736 e
= qemu_mallocz(sizeof (*e
));
1740 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1744 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1746 QLIST_REMOVE (e
, entries
);
1750 void vm_state_notify(int running
, int reason
)
1752 VMChangeStateEntry
*e
;
1754 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1755 e
->cb(e
->opaque
, running
, reason
);
1764 vm_state_notify(1, 0);
1769 /* reset/shutdown handler */
1771 typedef struct QEMUResetEntry
{
1772 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1773 QEMUResetHandler
*func
;
1777 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1778 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1779 static int reset_requested
;
1780 static int shutdown_requested
;
1781 static int powerdown_requested
;
1782 int debug_requested
;
1783 static int vmstop_requested
;
1785 int qemu_shutdown_requested(void)
1787 int r
= shutdown_requested
;
1788 shutdown_requested
= 0;
1792 int qemu_reset_requested(void)
1794 int r
= reset_requested
;
1795 reset_requested
= 0;
1799 int qemu_powerdown_requested(void)
1801 int r
= powerdown_requested
;
1802 powerdown_requested
= 0;
1806 static int qemu_debug_requested(void)
1808 int r
= debug_requested
;
1809 debug_requested
= 0;
1813 static int qemu_vmstop_requested(void)
1815 int r
= vmstop_requested
;
1816 vmstop_requested
= 0;
1820 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1822 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1825 re
->opaque
= opaque
;
1826 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1829 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1833 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1834 if (re
->func
== func
&& re
->opaque
== opaque
) {
1835 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1842 void qemu_system_reset(void)
1844 QEMUResetEntry
*re
, *nre
;
1846 /* reset all devices */
1847 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1848 re
->func(re
->opaque
);
1850 monitor_protocol_event(QEVENT_RESET
, NULL
);
1851 cpu_synchronize_all_post_reset();
1854 void qemu_system_reset_request(void)
1857 shutdown_requested
= 1;
1859 reset_requested
= 1;
1861 qemu_notify_event();
1864 void qemu_system_shutdown_request(void)
1866 shutdown_requested
= 1;
1867 qemu_notify_event();
1870 void qemu_system_powerdown_request(void)
1872 powerdown_requested
= 1;
1873 qemu_notify_event();
1877 static void host_main_loop_wait(int *timeout
)
1883 /* XXX: need to suppress polling by better using win32 events */
1885 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1886 ret
|= pe
->func(pe
->opaque
);
1890 WaitObjects
*w
= &wait_objects
;
1892 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1893 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1894 if (w
->func
[ret
- WAIT_OBJECT_0
])
1895 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1897 /* Check for additional signaled events */
1898 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1900 /* Check if event is signaled */
1901 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1902 if(ret2
== WAIT_OBJECT_0
) {
1904 w
->func
[i
](w
->opaque
[i
]);
1905 } else if (ret2
== WAIT_TIMEOUT
) {
1907 err
= GetLastError();
1908 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1911 } else if (ret
== WAIT_TIMEOUT
) {
1913 err
= GetLastError();
1914 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1921 static void host_main_loop_wait(int *timeout
)
1926 void main_loop_wait(int nonblocking
)
1928 IOHandlerRecord
*ioh
;
1929 fd_set rfds
, wfds
, xfds
;
1937 timeout
= qemu_calculate_timeout();
1938 qemu_bh_update_timeout(&timeout
);
1941 host_main_loop_wait(&timeout
);
1943 /* poll any events */
1944 /* XXX: separate device handlers from system ones */
1949 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1953 (!ioh
->fd_read_poll
||
1954 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1955 FD_SET(ioh
->fd
, &rfds
);
1959 if (ioh
->fd_write
) {
1960 FD_SET(ioh
->fd
, &wfds
);
1966 tv
.tv_sec
= timeout
/ 1000;
1967 tv
.tv_usec
= (timeout
% 1000) * 1000;
1969 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1971 qemu_mutex_unlock_iothread();
1972 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1973 qemu_mutex_lock_iothread();
1975 IOHandlerRecord
*pioh
;
1977 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1979 QLIST_REMOVE(ioh
, next
);
1983 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1984 ioh
->fd_read(ioh
->opaque
);
1986 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1987 ioh
->fd_write(ioh
->opaque
);
1992 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1994 qemu_run_all_timers();
1996 /* Check bottom-halves last in case any of the earlier events triggered
2002 static int vm_can_run(void)
2004 if (powerdown_requested
)
2006 if (reset_requested
)
2008 if (shutdown_requested
)
2010 if (debug_requested
)
2015 qemu_irq qemu_system_powerdown
;
2017 static void main_loop(void)
2021 #ifdef CONFIG_IOTHREAD
2022 qemu_system_ready
= 1;
2023 qemu_cond_broadcast(&qemu_system_cond
);
2028 bool nonblocking
= false;
2029 #ifdef CONFIG_PROFILER
2032 #ifndef CONFIG_IOTHREAD
2033 nonblocking
= tcg_cpu_exec();
2035 #ifdef CONFIG_PROFILER
2036 ti
= profile_getclock();
2038 main_loop_wait(nonblocking
);
2039 #ifdef CONFIG_PROFILER
2040 dev_time
+= profile_getclock() - ti
;
2042 } while (vm_can_run());
2044 if ((r
= qemu_debug_requested())) {
2047 if (qemu_shutdown_requested()) {
2048 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2055 if (qemu_reset_requested()) {
2057 qemu_system_reset();
2060 if (qemu_powerdown_requested()) {
2061 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2062 qemu_irq_raise(qemu_system_powerdown
);
2064 if ((r
= qemu_vmstop_requested())) {
2071 static void version(void)
2073 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2076 static void help(int exitcode
)
2078 const char *options_help
=
2079 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2081 #define DEFHEADING(text) stringify(text) "\n"
2082 #include "qemu-options.h"
2088 printf("usage: %s [options] [disk_image]\n"
2090 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2093 "During emulation, the following keys are useful:\n"
2094 "ctrl-alt-f toggle full screen\n"
2095 "ctrl-alt-n switch to virtual console 'n'\n"
2096 "ctrl-alt toggle mouse and keyboard grab\n"
2098 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2104 #define HAS_ARG 0x0001
2107 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2109 #define DEFHEADING(text)
2110 #include "qemu-options.h"
2116 typedef struct QEMUOption
{
2123 static const QEMUOption qemu_options
[] = {
2124 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2125 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2126 { option, opt_arg, opt_enum, arch_mask },
2127 #define DEFHEADING(text)
2128 #include "qemu-options.h"
2134 static void select_vgahw (const char *p
)
2139 vga_interface_type
= VGA_NONE
;
2140 if (strstart(p
, "std", &opts
)) {
2141 vga_interface_type
= VGA_STD
;
2142 } else if (strstart(p
, "cirrus", &opts
)) {
2143 vga_interface_type
= VGA_CIRRUS
;
2144 } else if (strstart(p
, "vmware", &opts
)) {
2145 vga_interface_type
= VGA_VMWARE
;
2146 } else if (strstart(p
, "xenfb", &opts
)) {
2147 vga_interface_type
= VGA_XENFB
;
2148 } else if (!strstart(p
, "none", &opts
)) {
2150 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2154 const char *nextopt
;
2156 if (strstart(opts
, ",retrace=", &nextopt
)) {
2158 if (strstart(opts
, "dumb", &nextopt
))
2159 vga_retrace_method
= VGA_RETRACE_DUMB
;
2160 else if (strstart(opts
, "precise", &nextopt
))
2161 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2162 else goto invalid_vga
;
2163 } else goto invalid_vga
;
2168 static int balloon_parse(const char *arg
)
2172 if (strcmp(arg
, "none") == 0) {
2176 if (!strncmp(arg
, "virtio", 6)) {
2177 if (arg
[6] == ',') {
2178 /* have params -> parse them */
2179 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2183 /* create empty opts */
2184 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2186 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2194 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2196 exit(STATUS_CONTROL_C_EXIT
);
2203 static void termsig_handler(int signal
)
2205 qemu_system_shutdown_request();
2208 static void sigchld_handler(int signal
)
2210 waitpid(-1, NULL
, WNOHANG
);
2213 static void sighandler_setup(void)
2215 struct sigaction act
;
2217 memset(&act
, 0, sizeof(act
));
2218 act
.sa_handler
= termsig_handler
;
2219 sigaction(SIGINT
, &act
, NULL
);
2220 sigaction(SIGHUP
, &act
, NULL
);
2221 sigaction(SIGTERM
, &act
, NULL
);
2223 act
.sa_handler
= sigchld_handler
;
2224 act
.sa_flags
= SA_NOCLDSTOP
;
2225 sigaction(SIGCHLD
, &act
, NULL
);
2231 /* Look for support files in the same directory as the executable. */
2232 static char *find_datadir(const char *argv0
)
2238 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2245 while (p
!= buf
&& *p
!= '\\')
2248 if (access(buf
, R_OK
) == 0) {
2249 return qemu_strdup(buf
);
2255 /* Find a likely location for support files using the location of the binary.
2256 For installed binaries this will be "$bindir/../share/qemu". When
2257 running from the build tree this will be "$bindir/../pc-bios". */
2258 #define SHARE_SUFFIX "/share/qemu"
2259 #define BUILD_SUFFIX "/pc-bios"
2260 static char *find_datadir(const char *argv0
)
2268 #if defined(__linux__)
2271 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2277 #elif defined(__FreeBSD__)
2280 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
2287 /* If we don't have any way of figuring out the actual executable
2288 location then try argv[0]. */
2290 p
= realpath(argv0
, buf
);
2298 max_len
= strlen(dir
) +
2299 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2300 res
= qemu_mallocz(max_len
);
2301 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2302 if (access(res
, R_OK
)) {
2303 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2304 if (access(res
, R_OK
)) {
2316 char *qemu_find_file(int type
, const char *name
)
2322 /* If name contains path separators then try it as a straight path. */
2323 if ((strchr(name
, '/') || strchr(name
, '\\'))
2324 && access(name
, R_OK
) == 0) {
2325 return qemu_strdup(name
);
2328 case QEMU_FILE_TYPE_BIOS
:
2331 case QEMU_FILE_TYPE_KEYMAP
:
2332 subdir
= "keymaps/";
2337 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2338 buf
= qemu_mallocz(len
);
2339 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2340 if (access(buf
, R_OK
)) {
2347 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2349 return qdev_device_help(opts
);
2352 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2356 dev
= qdev_device_add(opts
);
2362 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2364 CharDriverState
*chr
;
2366 chr
= qemu_chr_open_opts(opts
, NULL
);
2372 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2374 CharDriverState
*chr
;
2375 const char *chardev
;
2379 mode
= qemu_opt_get(opts
, "mode");
2383 if (strcmp(mode
, "readline") == 0) {
2384 flags
= MONITOR_USE_READLINE
;
2385 } else if (strcmp(mode
, "control") == 0) {
2386 flags
= MONITOR_USE_CONTROL
;
2388 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2392 if (qemu_opt_get_bool(opts
, "default", 0))
2393 flags
|= MONITOR_IS_DEFAULT
;
2395 chardev
= qemu_opt_get(opts
, "chardev");
2396 chr
= qemu_chr_find(chardev
);
2398 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2402 monitor_init(chr
, flags
);
2406 static void monitor_parse(const char *optarg
, const char *mode
)
2408 static int monitor_device_index
= 0;
2414 if (strstart(optarg
, "chardev:", &p
)) {
2415 snprintf(label
, sizeof(label
), "%s", p
);
2417 if (monitor_device_index
) {
2418 snprintf(label
, sizeof(label
), "monitor%d",
2419 monitor_device_index
);
2421 snprintf(label
, sizeof(label
), "monitor");
2424 opts
= qemu_chr_parse_compat(label
, optarg
);
2426 fprintf(stderr
, "parse error: %s\n", optarg
);
2431 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2433 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2436 qemu_opt_set(opts
, "mode", mode
);
2437 qemu_opt_set(opts
, "chardev", label
);
2439 qemu_opt_set(opts
, "default", "on");
2440 monitor_device_index
++;
2443 struct device_config
{
2445 DEV_USB
, /* -usbdevice */
2447 DEV_SERIAL
, /* -serial */
2448 DEV_PARALLEL
, /* -parallel */
2449 DEV_VIRTCON
, /* -virtioconsole */
2450 DEV_DEBUGCON
, /* -debugcon */
2452 const char *cmdline
;
2453 QTAILQ_ENTRY(device_config
) next
;
2455 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2457 static void add_device_config(int type
, const char *cmdline
)
2459 struct device_config
*conf
;
2461 conf
= qemu_mallocz(sizeof(*conf
));
2463 conf
->cmdline
= cmdline
;
2464 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2467 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2469 struct device_config
*conf
;
2472 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2473 if (conf
->type
!= type
)
2475 rc
= func(conf
->cmdline
);
2482 static int serial_parse(const char *devname
)
2484 static int index
= 0;
2487 if (strcmp(devname
, "none") == 0)
2489 if (index
== MAX_SERIAL_PORTS
) {
2490 fprintf(stderr
, "qemu: too many serial ports\n");
2493 snprintf(label
, sizeof(label
), "serial%d", index
);
2494 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2495 if (!serial_hds
[index
]) {
2496 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2497 devname
, strerror(errno
));
2504 static int parallel_parse(const char *devname
)
2506 static int index
= 0;
2509 if (strcmp(devname
, "none") == 0)
2511 if (index
== MAX_PARALLEL_PORTS
) {
2512 fprintf(stderr
, "qemu: too many parallel ports\n");
2515 snprintf(label
, sizeof(label
), "parallel%d", index
);
2516 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2517 if (!parallel_hds
[index
]) {
2518 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2519 devname
, strerror(errno
));
2526 static int virtcon_parse(const char *devname
)
2528 static int index
= 0;
2530 QemuOpts
*bus_opts
, *dev_opts
;
2532 if (strcmp(devname
, "none") == 0)
2534 if (index
== MAX_VIRTIO_CONSOLES
) {
2535 fprintf(stderr
, "qemu: too many virtio consoles\n");
2539 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2540 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2542 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2543 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2545 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2546 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2547 if (!virtcon_hds
[index
]) {
2548 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2549 devname
, strerror(errno
));
2552 qemu_opt_set(dev_opts
, "chardev", label
);
2558 static int debugcon_parse(const char *devname
)
2562 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2565 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2567 fprintf(stderr
, "qemu: already have a debugcon device\n");
2570 qemu_opt_set(opts
, "driver", "isa-debugcon");
2571 qemu_opt_set(opts
, "chardev", "debugcon");
2575 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2576 const char **poptarg
, int *poptind
)
2578 const QEMUOption
*popt
;
2579 int optind
= *poptind
;
2580 char *r
= argv
[optind
];
2583 loc_set_cmdline(argv
, optind
, 1);
2585 /* Treat --foo the same as -foo. */
2588 popt
= qemu_options
;
2591 error_report("invalid option");
2594 if (!strcmp(popt
->name
, r
+ 1))
2598 if (popt
->flags
& HAS_ARG
) {
2599 if (optind
>= argc
) {
2600 error_report("requires an argument");
2603 optarg
= argv
[optind
++];
2604 loc_set_cmdline(argv
, optind
- 2, 2);
2615 int main(int argc
, char **argv
, char **envp
)
2617 const char *gdbstub_dev
= NULL
;
2618 uint32_t boot_devices_bitmap
= 0;
2620 int snapshot
, linux_boot
, net_boot
;
2621 const char *icount_option
= NULL
;
2622 const char *initrd_filename
;
2623 const char *kernel_filename
, *kernel_cmdline
;
2624 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2626 DisplayChangeListener
*dcl
;
2627 int cyls
, heads
, secs
, translation
;
2628 QemuOpts
*hda_opts
= NULL
, *opts
;
2631 const char *loadvm
= NULL
;
2632 QEMUMachine
*machine
;
2633 const char *cpu_model
;
2638 const char *pid_file
= NULL
;
2639 const char *incoming
= NULL
;
2642 struct passwd
*pwd
= NULL
;
2643 const char *chroot_dir
= NULL
;
2644 const char *run_as
= NULL
;
2646 int show_vnc_port
= 0;
2649 error_set_progname(argv
[0]);
2653 qemu_cache_utils_init(envp
);
2655 QLIST_INIT (&vm_change_state_head
);
2658 struct sigaction act
;
2659 sigfillset(&act
.sa_mask
);
2661 act
.sa_handler
= SIG_IGN
;
2662 sigaction(SIGPIPE
, &act
, NULL
);
2665 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2666 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2667 QEMU to run on a single CPU */
2672 h
= GetCurrentProcess();
2673 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2674 for(i
= 0; i
< 32; i
++) {
2675 if (mask
& (1 << i
))
2680 SetProcessAffinityMask(h
, mask
);
2686 module_call_init(MODULE_INIT_MACHINE
);
2687 machine
= find_default_machine();
2689 initrd_filename
= NULL
;
2692 kernel_filename
= NULL
;
2693 kernel_cmdline
= "";
2694 cyls
= heads
= secs
= 0;
2695 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2697 for (i
= 0; i
< MAX_NODES
; i
++) {
2699 node_cpumask
[i
] = 0;
2708 /* first pass of option parsing */
2710 while (optind
< argc
) {
2711 if (argv
[optind
][0] != '-') {
2716 const QEMUOption
*popt
;
2718 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2719 switch (popt
->index
) {
2720 case QEMU_OPTION_nodefconfig
:
2731 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2732 fp
= fopen(fname
, "r");
2734 if (qemu_config_parse(fp
, fname
) != 0) {
2740 fname
= arch_config_name
;
2741 fp
= fopen(fname
, "r");
2743 if (qemu_config_parse(fp
, fname
) != 0) {
2751 /* second pass of option parsing */
2756 if (argv
[optind
][0] != '-') {
2757 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2759 const QEMUOption
*popt
;
2761 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2762 if (!(popt
->arch_mask
& arch_type
)) {
2763 printf("Option %s not supported for this target\n", popt
->name
);
2766 switch(popt
->index
) {
2768 machine
= find_machine(optarg
);
2771 printf("Supported machines are:\n");
2772 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2774 printf("%-10s %s (alias of %s)\n",
2775 m
->alias
, m
->desc
, m
->name
);
2776 printf("%-10s %s%s\n",
2778 m
->is_default
? " (default)" : "");
2780 exit(*optarg
!= '?');
2783 case QEMU_OPTION_cpu
:
2784 /* hw initialization will check this */
2785 if (*optarg
== '?') {
2786 /* XXX: implement xxx_cpu_list for targets that still miss it */
2787 #if defined(cpu_list_id)
2788 cpu_list_id(stdout
, &fprintf
, optarg
);
2789 #elif defined(cpu_list)
2790 cpu_list(stdout
, &fprintf
); /* deprecated */
2797 case QEMU_OPTION_initrd
:
2798 initrd_filename
= optarg
;
2800 case QEMU_OPTION_hda
:
2802 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2804 hda_opts
= drive_add(optarg
, HD_ALIAS
2805 ",cyls=%d,heads=%d,secs=%d%s",
2806 0, cyls
, heads
, secs
,
2807 translation
== BIOS_ATA_TRANSLATION_LBA
?
2809 translation
== BIOS_ATA_TRANSLATION_NONE
?
2810 ",trans=none" : "");
2812 case QEMU_OPTION_hdb
:
2813 case QEMU_OPTION_hdc
:
2814 case QEMU_OPTION_hdd
:
2815 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2817 case QEMU_OPTION_drive
:
2818 drive_add(NULL
, "%s", optarg
);
2820 case QEMU_OPTION_set
:
2821 if (qemu_set_option(optarg
) != 0)
2824 case QEMU_OPTION_global
:
2825 if (qemu_global_option(optarg
) != 0)
2828 case QEMU_OPTION_mtdblock
:
2829 drive_add(optarg
, MTD_ALIAS
);
2831 case QEMU_OPTION_sd
:
2832 drive_add(optarg
, SD_ALIAS
);
2834 case QEMU_OPTION_pflash
:
2835 drive_add(optarg
, PFLASH_ALIAS
);
2837 case QEMU_OPTION_snapshot
:
2840 case QEMU_OPTION_hdachs
:
2844 cyls
= strtol(p
, (char **)&p
, 0);
2845 if (cyls
< 1 || cyls
> 16383)
2850 heads
= strtol(p
, (char **)&p
, 0);
2851 if (heads
< 1 || heads
> 16)
2856 secs
= strtol(p
, (char **)&p
, 0);
2857 if (secs
< 1 || secs
> 63)
2861 if (!strcmp(p
, "none"))
2862 translation
= BIOS_ATA_TRANSLATION_NONE
;
2863 else if (!strcmp(p
, "lba"))
2864 translation
= BIOS_ATA_TRANSLATION_LBA
;
2865 else if (!strcmp(p
, "auto"))
2866 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2869 } else if (*p
!= '\0') {
2871 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2874 if (hda_opts
!= NULL
) {
2876 snprintf(num
, sizeof(num
), "%d", cyls
);
2877 qemu_opt_set(hda_opts
, "cyls", num
);
2878 snprintf(num
, sizeof(num
), "%d", heads
);
2879 qemu_opt_set(hda_opts
, "heads", num
);
2880 snprintf(num
, sizeof(num
), "%d", secs
);
2881 qemu_opt_set(hda_opts
, "secs", num
);
2882 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2883 qemu_opt_set(hda_opts
, "trans", "lba");
2884 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2885 qemu_opt_set(hda_opts
, "trans", "none");
2889 case QEMU_OPTION_numa
:
2890 if (nb_numa_nodes
>= MAX_NODES
) {
2891 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2896 case QEMU_OPTION_nographic
:
2897 display_type
= DT_NOGRAPHIC
;
2899 #ifdef CONFIG_CURSES
2900 case QEMU_OPTION_curses
:
2901 display_type
= DT_CURSES
;
2904 case QEMU_OPTION_portrait
:
2907 case QEMU_OPTION_kernel
:
2908 kernel_filename
= optarg
;
2910 case QEMU_OPTION_append
:
2911 kernel_cmdline
= optarg
;
2913 case QEMU_OPTION_cdrom
:
2914 drive_add(optarg
, CDROM_ALIAS
);
2916 case QEMU_OPTION_boot
:
2918 static const char * const params
[] = {
2919 "order", "once", "menu", NULL
2921 char buf
[sizeof(boot_devices
)];
2922 char *standard_boot_devices
;
2925 if (!strchr(optarg
, '=')) {
2927 pstrcpy(buf
, sizeof(buf
), optarg
);
2928 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2930 "qemu: unknown boot parameter '%s' in '%s'\n",
2936 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2937 boot_devices_bitmap
= parse_bootdevices(buf
);
2938 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2941 if (get_param_value(buf
, sizeof(buf
),
2943 boot_devices_bitmap
|= parse_bootdevices(buf
);
2944 standard_boot_devices
= qemu_strdup(boot_devices
);
2945 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2946 qemu_register_reset(restore_boot_devices
,
2947 standard_boot_devices
);
2949 if (get_param_value(buf
, sizeof(buf
),
2951 if (!strcmp(buf
, "on")) {
2953 } else if (!strcmp(buf
, "off")) {
2957 "qemu: invalid option value '%s'\n",
2965 case QEMU_OPTION_fda
:
2966 case QEMU_OPTION_fdb
:
2967 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2969 case QEMU_OPTION_no_fd_bootchk
:
2972 case QEMU_OPTION_netdev
:
2973 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2977 case QEMU_OPTION_net
:
2978 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2983 case QEMU_OPTION_tftp
:
2984 legacy_tftp_prefix
= optarg
;
2986 case QEMU_OPTION_bootp
:
2987 legacy_bootp_filename
= optarg
;
2990 case QEMU_OPTION_smb
:
2991 if (net_slirp_smb(optarg
) < 0)
2995 case QEMU_OPTION_redir
:
2996 if (net_slirp_redir(optarg
) < 0)
3000 case QEMU_OPTION_bt
:
3001 add_device_config(DEV_BT
, optarg
);
3003 case QEMU_OPTION_audio_help
:
3004 if (!(audio_available())) {
3005 printf("Option %s not supported for this target\n", popt
->name
);
3011 case QEMU_OPTION_soundhw
:
3012 if (!(audio_available())) {
3013 printf("Option %s not supported for this target\n", popt
->name
);
3016 select_soundhw (optarg
);
3021 case QEMU_OPTION_version
:
3025 case QEMU_OPTION_m
: {
3029 value
= strtoul(optarg
, &ptr
, 10);
3031 case 0: case 'M': case 'm':
3038 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3042 /* On 32-bit hosts, QEMU is limited by virtual address space */
3043 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3044 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3047 if (value
!= (uint64_t)(ram_addr_t
)value
) {
3048 fprintf(stderr
, "qemu: ram size too large\n");
3054 case QEMU_OPTION_mempath
:
3058 case QEMU_OPTION_mem_prealloc
:
3063 set_cpu_log(optarg
);
3066 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3068 case QEMU_OPTION_gdb
:
3069 gdbstub_dev
= optarg
;
3074 case QEMU_OPTION_bios
:
3077 case QEMU_OPTION_singlestep
:
3084 keyboard_layout
= optarg
;
3086 case QEMU_OPTION_localtime
:
3089 case QEMU_OPTION_vga
:
3090 select_vgahw (optarg
);
3097 w
= strtol(p
, (char **)&p
, 10);
3100 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3106 h
= strtol(p
, (char **)&p
, 10);
3111 depth
= strtol(p
, (char **)&p
, 10);
3112 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3113 depth
!= 24 && depth
!= 32)
3115 } else if (*p
== '\0') {
3116 depth
= graphic_depth
;
3123 graphic_depth
= depth
;
3126 case QEMU_OPTION_echr
:
3129 term_escape_char
= strtol(optarg
, &r
, 0);
3131 printf("Bad argument to echr\n");
3134 case QEMU_OPTION_monitor
:
3135 monitor_parse(optarg
, "readline");
3136 default_monitor
= 0;
3138 case QEMU_OPTION_qmp
:
3139 monitor_parse(optarg
, "control");
3140 default_monitor
= 0;
3142 case QEMU_OPTION_mon
:
3143 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3145 fprintf(stderr
, "parse error: %s\n", optarg
);
3148 default_monitor
= 0;
3150 case QEMU_OPTION_chardev
:
3151 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3153 fprintf(stderr
, "parse error: %s\n", optarg
);
3157 case QEMU_OPTION_serial
:
3158 add_device_config(DEV_SERIAL
, optarg
);
3160 if (strncmp(optarg
, "mon:", 4) == 0) {
3161 default_monitor
= 0;
3164 case QEMU_OPTION_watchdog
:
3167 "qemu: only one watchdog option may be given\n");
3172 case QEMU_OPTION_watchdog_action
:
3173 if (select_watchdog_action(optarg
) == -1) {
3174 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3178 case QEMU_OPTION_virtiocon
:
3179 add_device_config(DEV_VIRTCON
, optarg
);
3180 default_virtcon
= 0;
3181 if (strncmp(optarg
, "mon:", 4) == 0) {
3182 default_monitor
= 0;
3185 case QEMU_OPTION_parallel
:
3186 add_device_config(DEV_PARALLEL
, optarg
);
3187 default_parallel
= 0;
3188 if (strncmp(optarg
, "mon:", 4) == 0) {
3189 default_monitor
= 0;
3192 case QEMU_OPTION_debugcon
:
3193 add_device_config(DEV_DEBUGCON
, optarg
);
3195 case QEMU_OPTION_loadvm
:
3198 case QEMU_OPTION_full_screen
:
3202 case QEMU_OPTION_no_frame
:
3205 case QEMU_OPTION_alt_grab
:
3208 case QEMU_OPTION_ctrl_grab
:
3211 case QEMU_OPTION_no_quit
:
3214 case QEMU_OPTION_sdl
:
3215 display_type
= DT_SDL
;
3218 case QEMU_OPTION_pidfile
:
3221 case QEMU_OPTION_win2k_hack
:
3222 win2k_install_hack
= 1;
3224 case QEMU_OPTION_rtc_td_hack
:
3227 case QEMU_OPTION_acpitable
:
3228 do_acpitable_option(optarg
);
3230 case QEMU_OPTION_smbios
:
3231 do_smbios_option(optarg
);
3233 case QEMU_OPTION_enable_kvm
:
3234 if (!(kvm_available())) {
3235 printf("Option %s not supported for this target\n", popt
->name
);
3240 case QEMU_OPTION_usb
:
3243 case QEMU_OPTION_usbdevice
:
3245 add_device_config(DEV_USB
, optarg
);
3247 case QEMU_OPTION_device
:
3248 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3252 case QEMU_OPTION_smp
:
3255 fprintf(stderr
, "Invalid number of CPUs\n");
3258 if (max_cpus
< smp_cpus
) {
3259 fprintf(stderr
, "maxcpus must be equal to or greater than "
3263 if (max_cpus
> 255) {
3264 fprintf(stderr
, "Unsupported number of maxcpus\n");
3268 case QEMU_OPTION_vnc
:
3269 display_type
= DT_VNC
;
3270 vnc_display
= optarg
;
3272 case QEMU_OPTION_no_acpi
:
3275 case QEMU_OPTION_no_hpet
:
3278 case QEMU_OPTION_balloon
:
3279 if (balloon_parse(optarg
) < 0) {
3280 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3284 case QEMU_OPTION_no_reboot
:
3287 case QEMU_OPTION_no_shutdown
:
3290 case QEMU_OPTION_show_cursor
:
3293 case QEMU_OPTION_uuid
:
3294 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3295 fprintf(stderr
, "Fail to parse UUID string."
3296 " Wrong format.\n");
3301 case QEMU_OPTION_daemonize
:
3305 case QEMU_OPTION_option_rom
:
3306 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3307 fprintf(stderr
, "Too many option ROMs\n");
3310 option_rom
[nb_option_roms
] = optarg
;
3313 case QEMU_OPTION_semihosting
:
3314 semihosting_enabled
= 1;
3316 case QEMU_OPTION_name
:
3317 qemu_name
= qemu_strdup(optarg
);
3319 char *p
= strchr(qemu_name
, ',');
3322 if (strncmp(p
, "process=", 8)) {
3323 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3331 case QEMU_OPTION_prom_env
:
3332 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3333 fprintf(stderr
, "Too many prom variables\n");
3336 prom_envs
[nb_prom_envs
] = optarg
;
3339 case QEMU_OPTION_old_param
:
3342 case QEMU_OPTION_clock
:
3343 configure_alarms(optarg
);
3345 case QEMU_OPTION_startdate
:
3346 configure_rtc_date_offset(optarg
, 1);
3348 case QEMU_OPTION_rtc
:
3349 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3351 fprintf(stderr
, "parse error: %s\n", optarg
);
3354 configure_rtc(opts
);
3356 case QEMU_OPTION_tb_size
:
3357 tb_size
= strtol(optarg
, NULL
, 0);
3361 case QEMU_OPTION_icount
:
3362 icount_option
= optarg
;
3364 case QEMU_OPTION_incoming
:
3367 case QEMU_OPTION_nodefaults
:
3369 default_parallel
= 0;
3370 default_virtcon
= 0;
3371 default_monitor
= 0;
3379 case QEMU_OPTION_chroot
:
3380 chroot_dir
= optarg
;
3382 case QEMU_OPTION_runas
:
3386 case QEMU_OPTION_xen_domid
:
3387 if (!(xen_available())) {
3388 printf("Option %s not supported for this target\n", popt
->name
);
3391 xen_domid
= atoi(optarg
);
3393 case QEMU_OPTION_xen_create
:
3394 if (!(xen_available())) {
3395 printf("Option %s not supported for this target\n", popt
->name
);
3398 xen_mode
= XEN_CREATE
;
3400 case QEMU_OPTION_xen_attach
:
3401 if (!(xen_available())) {
3402 printf("Option %s not supported for this target\n", popt
->name
);
3405 xen_mode
= XEN_ATTACH
;
3407 case QEMU_OPTION_readconfig
:
3410 fp
= fopen(optarg
, "r");
3412 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3415 if (qemu_config_parse(fp
, optarg
) != 0) {
3421 case QEMU_OPTION_writeconfig
:
3424 if (strcmp(optarg
, "-") == 0) {
3427 fp
= fopen(optarg
, "w");
3429 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3433 qemu_config_write(fp
);
3442 /* If no data_dir is specified then try to find it relative to the
3445 data_dir
= find_datadir(argv
[0]);
3447 /* If all else fails use the install patch specified when building. */
3449 data_dir
= CONFIG_QEMU_SHAREDIR
;
3453 * Default to max_cpus = smp_cpus, in case the user doesn't
3454 * specify a max_cpus value.
3457 max_cpus
= smp_cpus
;
3459 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3460 if (smp_cpus
> machine
->max_cpus
) {
3461 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3462 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3467 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3468 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3470 if (machine
->no_serial
) {
3473 if (machine
->no_parallel
) {
3474 default_parallel
= 0;
3476 if (!machine
->use_virtcon
) {
3477 default_virtcon
= 0;
3479 if (machine
->no_vga
) {
3482 if (machine
->no_floppy
) {
3485 if (machine
->no_cdrom
) {
3488 if (machine
->no_sdcard
) {
3492 if (display_type
== DT_NOGRAPHIC
) {
3493 if (default_parallel
)
3494 add_device_config(DEV_PARALLEL
, "null");
3495 if (default_serial
&& default_monitor
) {
3496 add_device_config(DEV_SERIAL
, "mon:stdio");
3497 } else if (default_virtcon
&& default_monitor
) {
3498 add_device_config(DEV_VIRTCON
, "mon:stdio");
3501 add_device_config(DEV_SERIAL
, "stdio");
3502 if (default_virtcon
)
3503 add_device_config(DEV_VIRTCON
, "stdio");
3504 if (default_monitor
)
3505 monitor_parse("stdio", "readline");
3509 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3510 if (default_parallel
)
3511 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3512 if (default_monitor
)
3513 monitor_parse("vc:80Cx24C", "readline");
3514 if (default_virtcon
)
3515 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3518 vga_interface_type
= VGA_CIRRUS
;
3520 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3527 if (pipe(fds
) == -1)
3538 len
= read(fds
[0], &status
, 1);
3539 if (len
== -1 && (errno
== EINTR
))
3544 else if (status
== 1) {
3545 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3553 qemu_set_cloexec(fds
[1]);
3565 signal(SIGTSTP
, SIG_IGN
);
3566 signal(SIGTTOU
, SIG_IGN
);
3567 signal(SIGTTIN
, SIG_IGN
);
3571 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3575 if (write(fds
[1], &status
, 1) != 1) {
3576 perror("daemonize. Writing to pipe\n");
3580 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3584 if (kvm_enabled()) {
3587 ret
= kvm_init(smp_cpus
);
3589 fprintf(stderr
, "failed to initialize KVM\n");
3594 if (qemu_init_main_loop()) {
3595 fprintf(stderr
, "qemu_init_main_loop failed\n");
3598 linux_boot
= (kernel_filename
!= NULL
);
3600 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3601 fprintf(stderr
, "-append only allowed with -kernel option\n");
3605 if (!linux_boot
&& initrd_filename
!= NULL
) {
3606 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3611 /* Win32 doesn't support line-buffering and requires size >= 2 */
3612 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3615 if (init_timer_alarm() < 0) {
3616 fprintf(stderr
, "could not initialize alarm timer\n");
3619 configure_icount(icount_option
);
3625 if (net_init_clients() < 0) {
3629 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
3630 net_set_boot_mask(net_boot
);
3632 /* init the bluetooth world */
3633 if (foreach_device_config(DEV_BT
, bt_parse
))
3636 /* init the memory */
3638 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3640 /* init the dynamic translator */
3641 cpu_exec_init_all(tb_size
* 1024 * 1024);
3643 bdrv_init_with_whitelist();
3647 if (default_cdrom
) {
3648 /* we always create the cdrom drive, even if no disk is there */
3649 drive_add(NULL
, CDROM_ALIAS
);
3652 if (default_floppy
) {
3653 /* we always create at least one floppy */
3654 drive_add(NULL
, FD_ALIAS
, 0);
3657 if (default_sdcard
) {
3658 /* we always create one sd slot, even if no card is in it */
3659 drive_add(NULL
, SD_ALIAS
);
3662 /* open the virtual block devices */
3664 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3665 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3668 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3671 if (nb_numa_nodes
> 0) {
3674 if (nb_numa_nodes
> smp_cpus
) {
3675 nb_numa_nodes
= smp_cpus
;
3678 /* If no memory size if given for any node, assume the default case
3679 * and distribute the available memory equally across all nodes
3681 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3682 if (node_mem
[i
] != 0)
3685 if (i
== nb_numa_nodes
) {
3686 uint64_t usedmem
= 0;
3688 /* On Linux, the each node's border has to be 8MB aligned,
3689 * the final node gets the rest.
3691 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3692 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3693 usedmem
+= node_mem
[i
];
3695 node_mem
[i
] = ram_size
- usedmem
;
3698 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3699 if (node_cpumask
[i
] != 0)
3702 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3703 * must cope with this anyway, because there are BIOSes out there in
3704 * real machines which also use this scheme.
3706 if (i
== nb_numa_nodes
) {
3707 for (i
= 0; i
< smp_cpus
; i
++) {
3708 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3713 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3715 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3717 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3719 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3722 module_call_init(MODULE_INIT_DEVICE
);
3724 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3728 i
= select_watchdog(watchdog
);
3730 exit (i
== 1 ? 1 : 0);
3733 if (machine
->compat_props
) {
3734 qdev_prop_register_global_list(machine
->compat_props
);
3738 machine
->init(ram_size
, boot_devices
,
3739 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3741 cpu_synchronize_all_post_init();
3744 /* must be after terminal init, SDL library changes signal handlers */
3750 current_machine
= machine
;
3752 /* init USB devices */
3754 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3758 /* init generic devices */
3759 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3762 net_check_clients();
3764 /* just use the first displaystate for the moment */
3765 ds
= get_displaystate();
3767 if (display_type
== DT_DEFAULT
) {
3768 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3769 display_type
= DT_SDL
;
3771 display_type
= DT_VNC
;
3772 vnc_display
= "localhost:0,to=99";
3778 switch (display_type
) {
3781 #if defined(CONFIG_CURSES)
3783 curses_display_init(ds
, full_screen
);
3786 #if defined(CONFIG_SDL)
3788 sdl_display_init(ds
, full_screen
, no_frame
);
3790 #elif defined(CONFIG_COCOA)
3792 cocoa_display_init(ds
, full_screen
);
3796 vnc_display_init(ds
);
3797 if (vnc_display_open(ds
, vnc_display
) < 0)
3800 if (show_vnc_port
) {
3801 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3809 dcl
= ds
->listeners
;
3810 while (dcl
!= NULL
) {
3811 if (dcl
->dpy_refresh
!= NULL
) {
3812 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3813 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3818 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3819 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3820 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3823 text_consoles_set_display(ds
);
3825 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3828 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3829 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3834 qdev_machine_creation_done();
3836 if (rom_load_all() != 0) {
3837 fprintf(stderr
, "rom loading failed\n");
3841 qemu_system_reset();
3843 if (load_vmstate(loadvm
) < 0) {
3849 qemu_start_incoming_migration(incoming
);
3850 } else if (autostart
) {
3860 len
= write(fds
[1], &status
, 1);
3861 if (len
== -1 && (errno
== EINTR
))
3868 perror("not able to chdir to /");
3871 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3877 pwd
= getpwnam(run_as
);
3879 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3885 if (chroot(chroot_dir
) < 0) {
3886 fprintf(stderr
, "chroot failed\n");
3890 perror("not able to chdir to /");
3896 if (setgid(pwd
->pw_gid
) < 0) {
3897 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3900 if (setuid(pwd
->pw_uid
) < 0) {
3901 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3904 if (setuid(0) != -1) {
3905 fprintf(stderr
, "Dropping privileges failed\n");