4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <sys/prctl.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
82 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84 extern int madvise(caddr_t
, size_t, int);
89 #if defined(__OpenBSD__)
93 #if defined(CONFIG_VDE)
94 #include <libvdeplug.h>
102 #if defined(__APPLE__) || defined(main)
104 int qemu_main(int argc
, char **argv
, char **envp
);
105 int main(int argc
, char **argv
)
107 return qemu_main(argc
, argv
, NULL
);
110 #define main qemu_main
112 #endif /* CONFIG_SDL */
116 #define main qemu_main
117 #endif /* CONFIG_COCOA */
120 #include "hw/boards.h"
122 #include "hw/pcmcia.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "block_int.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
150 #include "qemu-option.h"
151 #include "qemu-config.h"
152 #include "qemu-objects.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "qemu-queue.h"
162 #include "arch_init.h"
165 //#define DEBUG_SLIRP
167 #define DEFAULT_RAM_SIZE 128
169 #define MAX_VIRTIO_CONSOLES 1
171 static const char *data_dir
;
172 const char *bios_name
= NULL
;
173 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
174 to store the VM snapshots */
175 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
176 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
177 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
178 DisplayType display_type
= DT_DEFAULT
;
179 const char* keyboard_layout
= NULL
;
181 const char *mem_path
= NULL
;
183 int mem_prealloc
= 0; /* force preallocation of physical target memory */
186 NICInfo nd_table
[MAX_NICS
];
189 static int rtc_utc
= 1;
190 static int rtc_date_offset
= -1; /* -1 means no change */
191 QEMUClock
*rtc_clock
;
192 int vga_interface_type
= VGA_NONE
;
193 static int full_screen
= 0;
195 static int no_frame
= 0;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
201 int win2k_install_hack
= 0;
209 const char *vnc_display
;
210 int acpi_enabled
= 1;
216 int graphic_rotate
= 0;
217 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 const char *option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
234 uint64_t node_mem
[MAX_NODES
];
235 uint64_t node_cpumask
[MAX_NODES
];
237 static QEMUTimer
*nographic_timer
;
239 uint8_t qemu_uuid
[16];
241 static QEMUBootSetHandler
*boot_set_handler
;
242 static void *boot_set_opaque
;
246 enum xen_mode xen_mode
= XEN_EMULATE
;
249 #define SIG_IPI (SIGRTMIN+4)
251 #define SIG_IPI SIGUSR1
254 static int default_serial
= 1;
255 static int default_parallel
= 1;
256 static int default_virtcon
= 1;
257 static int default_monitor
= 1;
258 static int default_vga
= 1;
259 static int default_floppy
= 1;
260 static int default_cdrom
= 1;
261 static int default_sdcard
= 1;
267 { .driver
= "isa-serial", .flag
= &default_serial
},
268 { .driver
= "isa-parallel", .flag
= &default_parallel
},
269 { .driver
= "isa-fdc", .flag
= &default_floppy
},
270 { .driver
= "ide-drive", .flag
= &default_cdrom
},
271 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
272 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
273 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
274 { .driver
= "VGA", .flag
= &default_vga
},
275 { .driver
= "cirrus-vga", .flag
= &default_vga
},
276 { .driver
= "vmware-svga", .flag
= &default_vga
},
279 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
281 const char *driver
= qemu_opt_get(opts
, "driver");
286 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
287 if (strcmp(default_list
[i
].driver
, driver
) != 0)
289 *(default_list
[i
].flag
) = 0;
294 /***********************************************************/
295 /* x86 ISA bus support */
297 target_phys_addr_t isa_mem_base
= 0;
300 static void set_proc_name(const char *s
)
302 #if defined(__linux__) && defined(PR_SET_NAME)
306 name
[sizeof(name
) - 1] = 0;
307 strncpy(name
, s
, sizeof(name
));
308 /* Could rewrite argv[0] too, but that's a bit more complicated.
309 This simple way is enough for `top'. */
310 prctl(PR_SET_NAME
, name
);
317 static QEMUBalloonEvent
*qemu_balloon_event
;
318 void *qemu_balloon_event_opaque
;
320 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
322 qemu_balloon_event
= func
;
323 qemu_balloon_event_opaque
= opaque
;
326 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
328 if (qemu_balloon_event
) {
329 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
336 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
338 if (qemu_balloon_event
) {
339 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
347 /***********************************************************/
348 /* real time host monotonic timer */
350 /* compute with 96 bit intermediate result: (a*b)/c */
351 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
356 #ifdef HOST_WORDS_BIGENDIAN
366 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
367 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
370 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
374 /***********************************************************/
375 /* host time/date access */
376 void qemu_get_timedate(struct tm
*tm
, int offset
)
383 if (rtc_date_offset
== -1) {
387 ret
= localtime(&ti
);
389 ti
-= rtc_date_offset
;
393 memcpy(tm
, ret
, sizeof(struct tm
));
396 int qemu_timedate_diff(struct tm
*tm
)
400 if (rtc_date_offset
== -1)
402 seconds
= mktimegm(tm
);
404 seconds
= mktime(tm
);
406 seconds
= mktimegm(tm
) + rtc_date_offset
;
408 return seconds
- time(NULL
);
411 void rtc_change_mon_event(struct tm
*tm
)
415 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
416 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
417 qobject_decref(data
);
420 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
422 time_t rtc_start_date
;
425 if (!strcmp(startdate
, "now") && legacy
) {
426 rtc_date_offset
= -1;
428 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
436 } else if (sscanf(startdate
, "%d-%d-%d",
448 rtc_start_date
= mktimegm(&tm
);
449 if (rtc_start_date
== -1) {
451 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
452 "'2006-06-17T16:01:21' or '2006-06-17'\n");
455 rtc_date_offset
= time(NULL
) - rtc_start_date
;
459 static void configure_rtc(QemuOpts
*opts
)
463 value
= qemu_opt_get(opts
, "base");
465 if (!strcmp(value
, "utc")) {
467 } else if (!strcmp(value
, "localtime")) {
470 configure_rtc_date_offset(value
, 0);
473 value
= qemu_opt_get(opts
, "clock");
475 if (!strcmp(value
, "host")) {
476 rtc_clock
= host_clock
;
477 } else if (!strcmp(value
, "vm")) {
478 rtc_clock
= vm_clock
;
480 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
484 value
= qemu_opt_get(opts
, "driftfix");
486 if (!strcmp(value
, "slew")) {
488 } else if (!strcmp(value
, "none")) {
491 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
498 static void socket_cleanup(void)
503 static int socket_init(void)
508 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
510 err
= WSAGetLastError();
511 fprintf(stderr
, "WSAStartup: %d\n", err
);
514 atexit(socket_cleanup
);
519 /***********************************************************/
520 /* Bluetooth support */
523 static struct HCIInfo
*hci_table
[MAX_NICS
];
525 static struct bt_vlan_s
{
526 struct bt_scatternet_s net
;
528 struct bt_vlan_s
*next
;
531 /* find or alloc a new bluetooth "VLAN" */
532 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
534 struct bt_vlan_s
**pvlan
, *vlan
;
535 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
539 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
541 pvlan
= &first_bt_vlan
;
542 while (*pvlan
!= NULL
)
543 pvlan
= &(*pvlan
)->next
;
548 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
552 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
557 static struct HCIInfo null_hci
= {
558 .cmd_send
= null_hci_send
,
559 .sco_send
= null_hci_send
,
560 .acl_send
= null_hci_send
,
561 .bdaddr_set
= null_hci_addr_set
,
564 struct HCIInfo
*qemu_next_hci(void)
566 if (cur_hci
== nb_hcis
)
569 return hci_table
[cur_hci
++];
572 static struct HCIInfo
*hci_init(const char *str
)
575 struct bt_scatternet_s
*vlan
= 0;
577 if (!strcmp(str
, "null"))
580 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
582 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
583 else if (!strncmp(str
, "hci", 3)) {
586 if (!strncmp(str
+ 3, ",vlan=", 6)) {
587 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
592 vlan
= qemu_find_bt_vlan(0);
594 return bt_new_hci(vlan
);
597 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
602 static int bt_hci_parse(const char *str
)
607 if (nb_hcis
>= MAX_NICS
) {
608 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
621 bdaddr
.b
[5] = 0x56 + nb_hcis
;
622 hci
->bdaddr_set(hci
, bdaddr
.b
);
624 hci_table
[nb_hcis
++] = hci
;
629 static void bt_vhci_add(int vlan_id
)
631 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
634 fprintf(stderr
, "qemu: warning: adding a VHCI to "
635 "an empty scatternet %i\n", vlan_id
);
637 bt_vhci_init(bt_new_hci(vlan
));
640 static struct bt_device_s
*bt_device_add(const char *opt
)
642 struct bt_scatternet_s
*vlan
;
644 char *endp
= strstr(opt
, ",vlan=");
645 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
648 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
651 vlan_id
= strtol(endp
+ 6, &endp
, 0);
653 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
658 vlan
= qemu_find_bt_vlan(vlan_id
);
661 fprintf(stderr
, "qemu: warning: adding a slave device to "
662 "an empty scatternet %i\n", vlan_id
);
664 if (!strcmp(devname
, "keyboard"))
665 return bt_keyboard_init(vlan
);
667 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
671 static int bt_parse(const char *opt
)
673 const char *endp
, *p
;
676 if (strstart(opt
, "hci", &endp
)) {
677 if (!*endp
|| *endp
== ',') {
679 if (!strstart(endp
, ",vlan=", 0))
682 return bt_hci_parse(opt
);
684 } else if (strstart(opt
, "vhci", &endp
)) {
685 if (!*endp
|| *endp
== ',') {
687 if (strstart(endp
, ",vlan=", &p
)) {
688 vlan
= strtol(p
, (char **) &endp
, 0);
690 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
694 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
703 } else if (strstart(opt
, "device:", &endp
))
704 return !bt_device_add(endp
);
706 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
710 /***********************************************************/
711 /* QEMU Block devices */
713 #define HD_ALIAS "index=%d,media=disk"
714 #define CDROM_ALIAS "index=2,media=cdrom"
715 #define FD_ALIAS "index=%d,if=floppy"
716 #define PFLASH_ALIAS "if=pflash"
717 #define MTD_ALIAS "if=mtd"
718 #define SD_ALIAS "index=0,if=sd"
720 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
727 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
730 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
732 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
733 __FUNCTION__
, optstr
);
737 qemu_opt_set(opts
, "file", file
);
741 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
745 /* seek interface, bus and unit */
747 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
748 if (dinfo
->type
== type
&&
757 DriveInfo
*drive_get_by_id(const char *id
)
761 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
762 if (strcmp(id
, dinfo
->id
))
769 int drive_get_max_bus(BlockInterfaceType type
)
775 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
776 if(dinfo
->type
== type
&&
777 dinfo
->bus
> max_bus
)
778 max_bus
= dinfo
->bus
;
783 const char *drive_get_serial(BlockDriverState
*bdrv
)
787 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
788 if (dinfo
->bdrv
== bdrv
)
789 return dinfo
->serial
;
795 BlockInterfaceErrorAction
drive_get_on_error(
796 BlockDriverState
*bdrv
, int is_read
)
800 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
801 if (dinfo
->bdrv
== bdrv
)
802 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
805 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
808 static void bdrv_format_print(void *opaque
, const char *name
)
810 fprintf(stderr
, " %s", name
);
813 void drive_uninit(DriveInfo
*dinfo
)
815 qemu_opts_del(dinfo
->opts
);
816 bdrv_delete(dinfo
->bdrv
);
817 QTAILQ_REMOVE(&drives
, dinfo
, next
);
821 static int parse_block_error_action(const char *buf
, int is_read
)
823 if (!strcmp(buf
, "ignore")) {
824 return BLOCK_ERR_IGNORE
;
825 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
826 return BLOCK_ERR_STOP_ENOSPC
;
827 } else if (!strcmp(buf
, "stop")) {
828 return BLOCK_ERR_STOP_ANY
;
829 } else if (!strcmp(buf
, "report")) {
830 return BLOCK_ERR_REPORT
;
832 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
833 buf
, is_read
? "read" : "write");
838 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
842 const char *file
= NULL
;
845 const char *mediastr
= "";
846 BlockInterfaceType type
;
847 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
849 int cyls
, heads
, secs
, translation
;
850 BlockDriver
*drv
= NULL
;
851 QEMUMachine
*machine
= opaque
;
858 int on_read_error
, on_write_error
;
865 translation
= BIOS_ATA_TRANSLATION_AUTO
;
868 if (machine
&& machine
->use_scsi
) {
870 max_devs
= MAX_SCSI_DEVS
;
871 pstrcpy(devname
, sizeof(devname
), "scsi");
874 max_devs
= MAX_IDE_DEVS
;
875 pstrcpy(devname
, sizeof(devname
), "ide");
879 /* extract parameters */
880 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
881 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
882 index
= qemu_opt_get_number(opts
, "index", -1);
884 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
885 heads
= qemu_opt_get_number(opts
, "heads", 0);
886 secs
= qemu_opt_get_number(opts
, "secs", 0);
888 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
889 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
891 file
= qemu_opt_get(opts
, "file");
892 serial
= qemu_opt_get(opts
, "serial");
894 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
895 pstrcpy(devname
, sizeof(devname
), buf
);
896 if (!strcmp(buf
, "ide")) {
898 max_devs
= MAX_IDE_DEVS
;
899 } else if (!strcmp(buf
, "scsi")) {
901 max_devs
= MAX_SCSI_DEVS
;
902 } else if (!strcmp(buf
, "floppy")) {
905 } else if (!strcmp(buf
, "pflash")) {
908 } else if (!strcmp(buf
, "mtd")) {
911 } else if (!strcmp(buf
, "sd")) {
914 } else if (!strcmp(buf
, "virtio")) {
917 } else if (!strcmp(buf
, "xen")) {
920 } else if (!strcmp(buf
, "none")) {
924 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
929 if (cyls
|| heads
|| secs
) {
930 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
931 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
934 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
935 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
938 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
939 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
944 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
947 "qemu: '%s' trans must be used with cyls,heads and secs\n",
951 if (!strcmp(buf
, "none"))
952 translation
= BIOS_ATA_TRANSLATION_NONE
;
953 else if (!strcmp(buf
, "lba"))
954 translation
= BIOS_ATA_TRANSLATION_LBA
;
955 else if (!strcmp(buf
, "auto"))
956 translation
= BIOS_ATA_TRANSLATION_AUTO
;
958 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
963 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
964 if (!strcmp(buf
, "disk")) {
966 } else if (!strcmp(buf
, "cdrom")) {
967 if (cyls
|| secs
|| heads
) {
969 "qemu: '%s' invalid physical CHS format\n", buf
);
974 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
979 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
980 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
982 else if (!strcmp(buf
, "writethrough"))
984 else if (!strcmp(buf
, "writeback"))
987 fprintf(stderr
, "qemu: invalid cache option\n");
992 #ifdef CONFIG_LINUX_AIO
993 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
994 if (!strcmp(buf
, "threads"))
996 else if (!strcmp(buf
, "native"))
999 fprintf(stderr
, "qemu: invalid aio option\n");
1005 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1006 if (strcmp(buf
, "?") == 0) {
1007 fprintf(stderr
, "qemu: Supported formats:");
1008 bdrv_iterate_format(bdrv_format_print
, NULL
);
1009 fprintf(stderr
, "\n");
1012 drv
= bdrv_find_whitelisted_format(buf
);
1014 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1019 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1020 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1021 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1022 fprintf(stderr
, "werror is no supported by this format\n");
1026 on_write_error
= parse_block_error_action(buf
, 0);
1027 if (on_write_error
< 0) {
1032 on_read_error
= BLOCK_ERR_REPORT
;
1033 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1034 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1035 fprintf(stderr
, "rerror is no supported by this format\n");
1039 on_read_error
= parse_block_error_action(buf
, 1);
1040 if (on_read_error
< 0) {
1045 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1046 if (type
!= IF_VIRTIO
) {
1047 fprintf(stderr
, "addr is not supported\n");
1052 /* compute bus and unit according index */
1055 if (bus_id
!= 0 || unit_id
!= -1) {
1057 "qemu: index cannot be used with bus and unit\n");
1065 unit_id
= index
% max_devs
;
1066 bus_id
= index
/ max_devs
;
1070 /* if user doesn't specify a unit_id,
1071 * try to find the first free
1074 if (unit_id
== -1) {
1076 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1078 if (max_devs
&& unit_id
>= max_devs
) {
1079 unit_id
-= max_devs
;
1087 if (max_devs
&& unit_id
>= max_devs
) {
1088 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1089 unit_id
, max_devs
- 1);
1094 * ignore multiple definitions
1097 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1104 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1105 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1106 dinfo
->id
= qemu_strdup(buf
);
1108 /* no id supplied -> create one */
1109 dinfo
->id
= qemu_mallocz(32);
1110 if (type
== IF_IDE
|| type
== IF_SCSI
)
1111 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1113 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1114 devname
, bus_id
, mediastr
, unit_id
);
1116 snprintf(dinfo
->id
, 32, "%s%s%i",
1117 devname
, mediastr
, unit_id
);
1119 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1120 dinfo
->devaddr
= devaddr
;
1122 dinfo
->bus
= bus_id
;
1123 dinfo
->unit
= unit_id
;
1124 dinfo
->on_read_error
= on_read_error
;
1125 dinfo
->on_write_error
= on_write_error
;
1128 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1129 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1139 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1140 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1144 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1149 /* FIXME: This isn't really a floppy, but it's a reasonable
1152 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1158 /* add virtio block device */
1159 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1160 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1161 qemu_opt_set(opts
, "drive", dinfo
->id
);
1163 qemu_opt_set(opts
, "addr", devaddr
);
1174 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1175 cache
= 2; /* always use write-back with snapshot */
1177 if (cache
== 0) /* no caching */
1178 bdrv_flags
|= BDRV_O_NOCACHE
;
1179 else if (cache
== 2) /* write-back */
1180 bdrv_flags
|= BDRV_O_CACHE_WB
;
1183 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1185 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1189 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1190 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1195 * cdrom is read-only. Set it now, after above interface checking
1196 * since readonly attribute not explicitly required, so no error.
1198 if (media
== MEDIA_CDROM
) {
1201 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1203 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1204 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1205 file
, strerror(errno
));
1209 if (bdrv_key_required(dinfo
->bdrv
))
1215 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1217 QEMUMachine
*machine
= opaque
;
1218 int fatal_error
= 0;
1220 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1227 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1229 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1230 qemu_opt_set(opts
, "snapshot", "on");
1235 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1237 boot_set_handler
= func
;
1238 boot_set_opaque
= opaque
;
1241 int qemu_boot_set(const char *boot_devices
)
1243 if (!boot_set_handler
) {
1246 return boot_set_handler(boot_set_opaque
, boot_devices
);
1249 static int parse_bootdevices(char *devices
)
1251 /* We just do some generic consistency checks */
1255 for (p
= devices
; *p
!= '\0'; p
++) {
1256 /* Allowed boot devices are:
1257 * a-b: floppy disk drives
1258 * c-f: IDE disk drives
1259 * g-m: machine implementation dependant drives
1260 * n-p: network devices
1261 * It's up to each machine implementation to check if the given boot
1262 * devices match the actual hardware implementation and firmware
1265 if (*p
< 'a' || *p
> 'p') {
1266 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1269 if (bitmap
& (1 << (*p
- 'a'))) {
1270 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1273 bitmap
|= 1 << (*p
- 'a');
1278 static void restore_boot_devices(void *opaque
)
1280 char *standard_boot_devices
= opaque
;
1282 qemu_boot_set(standard_boot_devices
);
1284 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1285 qemu_free(standard_boot_devices
);
1288 static void numa_add(const char *optarg
)
1292 unsigned long long value
, endvalue
;
1295 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1296 if (!strcmp(option
, "node")) {
1297 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1298 nodenr
= nb_numa_nodes
;
1300 nodenr
= strtoull(option
, NULL
, 10);
1303 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1304 node_mem
[nodenr
] = 0;
1306 value
= strtoull(option
, &endptr
, 0);
1308 case 0: case 'M': case 'm':
1315 node_mem
[nodenr
] = value
;
1317 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1318 node_cpumask
[nodenr
] = 0;
1320 value
= strtoull(option
, &endptr
, 10);
1323 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1325 if (*endptr
== '-') {
1326 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1327 if (endvalue
>= 63) {
1330 "only 63 CPUs in NUMA mode supported.\n");
1332 value
= (2ULL << endvalue
) - (1ULL << value
);
1334 value
= 1ULL << value
;
1337 node_cpumask
[nodenr
] = value
;
1344 static void smp_parse(const char *optarg
)
1346 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1350 smp
= strtoul(optarg
, &endptr
, 10);
1351 if (endptr
!= optarg
) {
1352 if (*endptr
== ',') {
1356 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1357 sockets
= strtoull(option
, NULL
, 10);
1358 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1359 cores
= strtoull(option
, NULL
, 10);
1360 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1361 threads
= strtoull(option
, NULL
, 10);
1362 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1363 max_cpus
= strtoull(option
, NULL
, 10);
1365 /* compute missing values, prefer sockets over cores over threads */
1366 if (smp
== 0 || sockets
== 0) {
1367 sockets
= sockets
> 0 ? sockets
: 1;
1368 cores
= cores
> 0 ? cores
: 1;
1369 threads
= threads
> 0 ? threads
: 1;
1371 smp
= cores
* threads
* sockets
;
1375 threads
= threads
> 0 ? threads
: 1;
1376 cores
= smp
/ (sockets
* threads
);
1379 threads
= smp
/ (cores
* sockets
);
1384 smp_cores
= cores
> 0 ? cores
: 1;
1385 smp_threads
= threads
> 0 ? threads
: 1;
1387 max_cpus
= smp_cpus
;
1390 /***********************************************************/
1393 static int usb_device_add(const char *devname
, int is_hotplug
)
1396 USBDevice
*dev
= NULL
;
1401 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1402 dev
= usbdevice_create(devname
);
1406 /* the other ones */
1407 if (strstart(devname
, "host:", &p
)) {
1408 dev
= usb_host_device_open(p
);
1409 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1410 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1411 bt_new_hci(qemu_find_bt_vlan(0)));
1422 static int usb_device_del(const char *devname
)
1427 if (strstart(devname
, "host:", &p
))
1428 return usb_host_device_close(p
);
1433 p
= strchr(devname
, '.');
1436 bus_num
= strtoul(devname
, NULL
, 0);
1437 addr
= strtoul(p
+ 1, NULL
, 0);
1439 return usb_device_delete_addr(bus_num
, addr
);
1442 static int usb_parse(const char *cmdline
)
1445 r
= usb_device_add(cmdline
, 0);
1447 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1452 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1454 const char *devname
= qdict_get_str(qdict
, "devname");
1455 if (usb_device_add(devname
, 1) < 0) {
1456 error_report("could not add USB device '%s'", devname
);
1460 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1462 const char *devname
= qdict_get_str(qdict
, "devname");
1463 if (usb_device_del(devname
) < 0) {
1464 error_report("could not delete USB device '%s'", devname
);
1468 /***********************************************************/
1469 /* PCMCIA/Cardbus */
1471 static struct pcmcia_socket_entry_s
{
1472 PCMCIASocket
*socket
;
1473 struct pcmcia_socket_entry_s
*next
;
1474 } *pcmcia_sockets
= 0;
1476 void pcmcia_socket_register(PCMCIASocket
*socket
)
1478 struct pcmcia_socket_entry_s
*entry
;
1480 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1481 entry
->socket
= socket
;
1482 entry
->next
= pcmcia_sockets
;
1483 pcmcia_sockets
= entry
;
1486 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1488 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1490 ptr
= &pcmcia_sockets
;
1491 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1492 if (entry
->socket
== socket
) {
1498 void pcmcia_info(Monitor
*mon
)
1500 struct pcmcia_socket_entry_s
*iter
;
1502 if (!pcmcia_sockets
)
1503 monitor_printf(mon
, "No PCMCIA sockets\n");
1505 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1506 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1507 iter
->socket
->attached
? iter
->socket
->card_string
:
1511 /***********************************************************/
1514 typedef struct IOHandlerRecord
{
1516 IOCanReadHandler
*fd_read_poll
;
1518 IOHandler
*fd_write
;
1521 /* temporary data */
1523 QLIST_ENTRY(IOHandlerRecord
) next
;
1526 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1527 QLIST_HEAD_INITIALIZER(io_handlers
);
1530 /* XXX: fd_read_poll should be suppressed, but an API change is
1531 necessary in the character devices to suppress fd_can_read(). */
1532 int qemu_set_fd_handler2(int fd
,
1533 IOCanReadHandler
*fd_read_poll
,
1535 IOHandler
*fd_write
,
1538 IOHandlerRecord
*ioh
;
1540 if (!fd_read
&& !fd_write
) {
1541 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1542 if (ioh
->fd
== fd
) {
1548 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1552 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1553 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1556 ioh
->fd_read_poll
= fd_read_poll
;
1557 ioh
->fd_read
= fd_read
;
1558 ioh
->fd_write
= fd_write
;
1559 ioh
->opaque
= opaque
;
1565 int qemu_set_fd_handler(int fd
,
1567 IOHandler
*fd_write
,
1570 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1574 /***********************************************************/
1575 /* Polling handling */
1577 typedef struct PollingEntry
{
1580 struct PollingEntry
*next
;
1583 static PollingEntry
*first_polling_entry
;
1585 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1587 PollingEntry
**ppe
, *pe
;
1588 pe
= qemu_mallocz(sizeof(PollingEntry
));
1590 pe
->opaque
= opaque
;
1591 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1596 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1598 PollingEntry
**ppe
, *pe
;
1599 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1601 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1609 /***********************************************************/
1610 /* Wait objects support */
1611 typedef struct WaitObjects
{
1613 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1614 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1615 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1618 static WaitObjects wait_objects
= {0};
1620 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1622 WaitObjects
*w
= &wait_objects
;
1624 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1626 w
->events
[w
->num
] = handle
;
1627 w
->func
[w
->num
] = func
;
1628 w
->opaque
[w
->num
] = opaque
;
1633 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1636 WaitObjects
*w
= &wait_objects
;
1639 for (i
= 0; i
< w
->num
; i
++) {
1640 if (w
->events
[i
] == handle
)
1643 w
->events
[i
] = w
->events
[i
+ 1];
1644 w
->func
[i
] = w
->func
[i
+ 1];
1645 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1653 /***********************************************************/
1654 /* machine registration */
1656 static QEMUMachine
*first_machine
= NULL
;
1657 QEMUMachine
*current_machine
= NULL
;
1659 int qemu_register_machine(QEMUMachine
*m
)
1662 pm
= &first_machine
;
1670 static QEMUMachine
*find_machine(const char *name
)
1674 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1675 if (!strcmp(m
->name
, name
))
1677 if (m
->alias
&& !strcmp(m
->alias
, name
))
1683 static QEMUMachine
*find_default_machine(void)
1687 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1688 if (m
->is_default
) {
1695 /***********************************************************/
1696 /* main execution loop */
1698 static void gui_update(void *opaque
)
1700 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1701 DisplayState
*ds
= opaque
;
1702 DisplayChangeListener
*dcl
= ds
->listeners
;
1704 qemu_flush_coalesced_mmio_buffer();
1707 while (dcl
!= NULL
) {
1708 if (dcl
->gui_timer_interval
&&
1709 dcl
->gui_timer_interval
< interval
)
1710 interval
= dcl
->gui_timer_interval
;
1713 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1716 static void nographic_update(void *opaque
)
1718 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1720 qemu_flush_coalesced_mmio_buffer();
1721 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1724 struct vm_change_state_entry
{
1725 VMChangeStateHandler
*cb
;
1727 QLIST_ENTRY (vm_change_state_entry
) entries
;
1730 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1732 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1735 VMChangeStateEntry
*e
;
1737 e
= qemu_mallocz(sizeof (*e
));
1741 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1745 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1747 QLIST_REMOVE (e
, entries
);
1751 void vm_state_notify(int running
, int reason
)
1753 VMChangeStateEntry
*e
;
1755 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1756 e
->cb(e
->opaque
, running
, reason
);
1765 vm_state_notify(1, 0);
1770 /* reset/shutdown handler */
1772 typedef struct QEMUResetEntry
{
1773 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1774 QEMUResetHandler
*func
;
1778 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1779 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1780 static int reset_requested
;
1781 static int shutdown_requested
;
1782 static int powerdown_requested
;
1783 int debug_requested
;
1784 static int vmstop_requested
;
1786 int qemu_shutdown_requested(void)
1788 int r
= shutdown_requested
;
1789 shutdown_requested
= 0;
1793 int qemu_reset_requested(void)
1795 int r
= reset_requested
;
1796 reset_requested
= 0;
1800 int qemu_powerdown_requested(void)
1802 int r
= powerdown_requested
;
1803 powerdown_requested
= 0;
1807 static int qemu_debug_requested(void)
1809 int r
= debug_requested
;
1810 debug_requested
= 0;
1814 static int qemu_vmstop_requested(void)
1816 int r
= vmstop_requested
;
1817 vmstop_requested
= 0;
1821 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1823 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1826 re
->opaque
= opaque
;
1827 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1830 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1834 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1835 if (re
->func
== func
&& re
->opaque
== opaque
) {
1836 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1843 void qemu_system_reset(void)
1845 QEMUResetEntry
*re
, *nre
;
1847 /* reset all devices */
1848 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1849 re
->func(re
->opaque
);
1851 monitor_protocol_event(QEVENT_RESET
, NULL
);
1852 cpu_synchronize_all_post_reset();
1855 void qemu_system_reset_request(void)
1858 shutdown_requested
= 1;
1860 reset_requested
= 1;
1862 qemu_notify_event();
1865 void qemu_system_shutdown_request(void)
1867 shutdown_requested
= 1;
1868 qemu_notify_event();
1871 void qemu_system_powerdown_request(void)
1873 powerdown_requested
= 1;
1874 qemu_notify_event();
1878 static void host_main_loop_wait(int *timeout
)
1884 /* XXX: need to suppress polling by better using win32 events */
1886 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1887 ret
|= pe
->func(pe
->opaque
);
1891 WaitObjects
*w
= &wait_objects
;
1893 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1894 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1895 if (w
->func
[ret
- WAIT_OBJECT_0
])
1896 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1898 /* Check for additional signaled events */
1899 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1901 /* Check if event is signaled */
1902 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1903 if(ret2
== WAIT_OBJECT_0
) {
1905 w
->func
[i
](w
->opaque
[i
]);
1906 } else if (ret2
== WAIT_TIMEOUT
) {
1908 err
= GetLastError();
1909 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1912 } else if (ret
== WAIT_TIMEOUT
) {
1914 err
= GetLastError();
1915 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1922 static void host_main_loop_wait(int *timeout
)
1927 void main_loop_wait(int nonblocking
)
1929 IOHandlerRecord
*ioh
;
1930 fd_set rfds
, wfds
, xfds
;
1938 timeout
= qemu_calculate_timeout();
1939 qemu_bh_update_timeout(&timeout
);
1942 host_main_loop_wait(&timeout
);
1944 /* poll any events */
1945 /* XXX: separate device handlers from system ones */
1950 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1954 (!ioh
->fd_read_poll
||
1955 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1956 FD_SET(ioh
->fd
, &rfds
);
1960 if (ioh
->fd_write
) {
1961 FD_SET(ioh
->fd
, &wfds
);
1967 tv
.tv_sec
= timeout
/ 1000;
1968 tv
.tv_usec
= (timeout
% 1000) * 1000;
1970 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1972 qemu_mutex_unlock_iothread();
1973 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1974 qemu_mutex_lock_iothread();
1976 IOHandlerRecord
*pioh
;
1978 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1980 QLIST_REMOVE(ioh
, next
);
1984 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1985 ioh
->fd_read(ioh
->opaque
);
1987 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1988 ioh
->fd_write(ioh
->opaque
);
1993 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1995 qemu_run_all_timers();
1997 /* Check bottom-halves last in case any of the earlier events triggered
2003 static int vm_can_run(void)
2005 if (powerdown_requested
)
2007 if (reset_requested
)
2009 if (shutdown_requested
)
2011 if (debug_requested
)
2016 qemu_irq qemu_system_powerdown
;
2018 static void main_loop(void)
2022 #ifdef CONFIG_IOTHREAD
2023 qemu_system_ready
= 1;
2024 qemu_cond_broadcast(&qemu_system_cond
);
2029 bool nonblocking
= false;
2030 #ifdef CONFIG_PROFILER
2033 #ifndef CONFIG_IOTHREAD
2034 nonblocking
= tcg_cpu_exec();
2036 #ifdef CONFIG_PROFILER
2037 ti
= profile_getclock();
2039 main_loop_wait(nonblocking
);
2040 #ifdef CONFIG_PROFILER
2041 dev_time
+= profile_getclock() - ti
;
2043 } while (vm_can_run());
2045 if ((r
= qemu_debug_requested())) {
2048 if (qemu_shutdown_requested()) {
2049 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2056 if (qemu_reset_requested()) {
2058 qemu_system_reset();
2061 if (qemu_powerdown_requested()) {
2062 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2063 qemu_irq_raise(qemu_system_powerdown
);
2065 if ((r
= qemu_vmstop_requested())) {
2072 static void version(void)
2074 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2077 static void help(int exitcode
)
2079 const char *options_help
=
2080 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2082 #define DEFHEADING(text) stringify(text) "\n"
2083 #include "qemu-options.h"
2089 printf("usage: %s [options] [disk_image]\n"
2091 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2094 "During emulation, the following keys are useful:\n"
2095 "ctrl-alt-f toggle full screen\n"
2096 "ctrl-alt-n switch to virtual console 'n'\n"
2097 "ctrl-alt toggle mouse and keyboard grab\n"
2099 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2105 #define HAS_ARG 0x0001
2108 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2110 #define DEFHEADING(text)
2111 #include "qemu-options.h"
2117 typedef struct QEMUOption
{
2124 static const QEMUOption qemu_options
[] = {
2125 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2126 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2127 { option, opt_arg, opt_enum, arch_mask },
2128 #define DEFHEADING(text)
2129 #include "qemu-options.h"
2135 static void select_vgahw (const char *p
)
2140 vga_interface_type
= VGA_NONE
;
2141 if (strstart(p
, "std", &opts
)) {
2142 vga_interface_type
= VGA_STD
;
2143 } else if (strstart(p
, "cirrus", &opts
)) {
2144 vga_interface_type
= VGA_CIRRUS
;
2145 } else if (strstart(p
, "vmware", &opts
)) {
2146 vga_interface_type
= VGA_VMWARE
;
2147 } else if (strstart(p
, "xenfb", &opts
)) {
2148 vga_interface_type
= VGA_XENFB
;
2149 } else if (!strstart(p
, "none", &opts
)) {
2151 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2155 const char *nextopt
;
2157 if (strstart(opts
, ",retrace=", &nextopt
)) {
2159 if (strstart(opts
, "dumb", &nextopt
))
2160 vga_retrace_method
= VGA_RETRACE_DUMB
;
2161 else if (strstart(opts
, "precise", &nextopt
))
2162 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2163 else goto invalid_vga
;
2164 } else goto invalid_vga
;
2169 static int balloon_parse(const char *arg
)
2173 if (strcmp(arg
, "none") == 0) {
2177 if (!strncmp(arg
, "virtio", 6)) {
2178 if (arg
[6] == ',') {
2179 /* have params -> parse them */
2180 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2184 /* create empty opts */
2185 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2187 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2195 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2197 exit(STATUS_CONTROL_C_EXIT
);
2204 static void termsig_handler(int signal
)
2206 qemu_system_shutdown_request();
2209 static void sigchld_handler(int signal
)
2211 waitpid(-1, NULL
, WNOHANG
);
2214 static void sighandler_setup(void)
2216 struct sigaction act
;
2218 memset(&act
, 0, sizeof(act
));
2219 act
.sa_handler
= termsig_handler
;
2220 sigaction(SIGINT
, &act
, NULL
);
2221 sigaction(SIGHUP
, &act
, NULL
);
2222 sigaction(SIGTERM
, &act
, NULL
);
2224 act
.sa_handler
= sigchld_handler
;
2225 act
.sa_flags
= SA_NOCLDSTOP
;
2226 sigaction(SIGCHLD
, &act
, NULL
);
2232 /* Look for support files in the same directory as the executable. */
2233 static char *find_datadir(const char *argv0
)
2239 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2246 while (p
!= buf
&& *p
!= '\\')
2249 if (access(buf
, R_OK
) == 0) {
2250 return qemu_strdup(buf
);
2256 /* Find a likely location for support files using the location of the binary.
2257 For installed binaries this will be "$bindir/../share/qemu". When
2258 running from the build tree this will be "$bindir/../pc-bios". */
2259 #define SHARE_SUFFIX "/share/qemu"
2260 #define BUILD_SUFFIX "/pc-bios"
2261 static char *find_datadir(const char *argv0
)
2269 #if defined(__linux__)
2272 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2278 #elif defined(__FreeBSD__)
2280 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2281 size_t len
= sizeof(buf
) - 1;
2284 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2286 buf
[sizeof(buf
) - 1] = '\0';
2291 /* If we don't have any way of figuring out the actual executable
2292 location then try argv[0]. */
2294 p
= realpath(argv0
, buf
);
2302 max_len
= strlen(dir
) +
2303 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2304 res
= qemu_mallocz(max_len
);
2305 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2306 if (access(res
, R_OK
)) {
2307 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2308 if (access(res
, R_OK
)) {
2320 char *qemu_find_file(int type
, const char *name
)
2326 /* If name contains path separators then try it as a straight path. */
2327 if ((strchr(name
, '/') || strchr(name
, '\\'))
2328 && access(name
, R_OK
) == 0) {
2329 return qemu_strdup(name
);
2332 case QEMU_FILE_TYPE_BIOS
:
2335 case QEMU_FILE_TYPE_KEYMAP
:
2336 subdir
= "keymaps/";
2341 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2342 buf
= qemu_mallocz(len
);
2343 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2344 if (access(buf
, R_OK
)) {
2351 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2353 return qdev_device_help(opts
);
2356 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2360 dev
= qdev_device_add(opts
);
2366 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2368 CharDriverState
*chr
;
2370 chr
= qemu_chr_open_opts(opts
, NULL
);
2376 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2378 CharDriverState
*chr
;
2379 const char *chardev
;
2383 mode
= qemu_opt_get(opts
, "mode");
2387 if (strcmp(mode
, "readline") == 0) {
2388 flags
= MONITOR_USE_READLINE
;
2389 } else if (strcmp(mode
, "control") == 0) {
2390 flags
= MONITOR_USE_CONTROL
;
2392 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2396 if (qemu_opt_get_bool(opts
, "default", 0))
2397 flags
|= MONITOR_IS_DEFAULT
;
2399 chardev
= qemu_opt_get(opts
, "chardev");
2400 chr
= qemu_chr_find(chardev
);
2402 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2406 monitor_init(chr
, flags
);
2410 static void monitor_parse(const char *optarg
, const char *mode
)
2412 static int monitor_device_index
= 0;
2418 if (strstart(optarg
, "chardev:", &p
)) {
2419 snprintf(label
, sizeof(label
), "%s", p
);
2421 if (monitor_device_index
) {
2422 snprintf(label
, sizeof(label
), "monitor%d",
2423 monitor_device_index
);
2425 snprintf(label
, sizeof(label
), "monitor");
2428 opts
= qemu_chr_parse_compat(label
, optarg
);
2430 fprintf(stderr
, "parse error: %s\n", optarg
);
2435 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2437 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2440 qemu_opt_set(opts
, "mode", mode
);
2441 qemu_opt_set(opts
, "chardev", label
);
2443 qemu_opt_set(opts
, "default", "on");
2444 monitor_device_index
++;
2447 struct device_config
{
2449 DEV_USB
, /* -usbdevice */
2451 DEV_SERIAL
, /* -serial */
2452 DEV_PARALLEL
, /* -parallel */
2453 DEV_VIRTCON
, /* -virtioconsole */
2454 DEV_DEBUGCON
, /* -debugcon */
2456 const char *cmdline
;
2457 QTAILQ_ENTRY(device_config
) next
;
2459 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2461 static void add_device_config(int type
, const char *cmdline
)
2463 struct device_config
*conf
;
2465 conf
= qemu_mallocz(sizeof(*conf
));
2467 conf
->cmdline
= cmdline
;
2468 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2471 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2473 struct device_config
*conf
;
2476 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2477 if (conf
->type
!= type
)
2479 rc
= func(conf
->cmdline
);
2486 static int serial_parse(const char *devname
)
2488 static int index
= 0;
2491 if (strcmp(devname
, "none") == 0)
2493 if (index
== MAX_SERIAL_PORTS
) {
2494 fprintf(stderr
, "qemu: too many serial ports\n");
2497 snprintf(label
, sizeof(label
), "serial%d", index
);
2498 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2499 if (!serial_hds
[index
]) {
2500 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2501 devname
, strerror(errno
));
2508 static int parallel_parse(const char *devname
)
2510 static int index
= 0;
2513 if (strcmp(devname
, "none") == 0)
2515 if (index
== MAX_PARALLEL_PORTS
) {
2516 fprintf(stderr
, "qemu: too many parallel ports\n");
2519 snprintf(label
, sizeof(label
), "parallel%d", index
);
2520 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2521 if (!parallel_hds
[index
]) {
2522 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2523 devname
, strerror(errno
));
2530 static int virtcon_parse(const char *devname
)
2532 static int index
= 0;
2534 QemuOpts
*bus_opts
, *dev_opts
;
2536 if (strcmp(devname
, "none") == 0)
2538 if (index
== MAX_VIRTIO_CONSOLES
) {
2539 fprintf(stderr
, "qemu: too many virtio consoles\n");
2543 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2544 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2546 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2547 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2549 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2550 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2551 if (!virtcon_hds
[index
]) {
2552 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2553 devname
, strerror(errno
));
2556 qemu_opt_set(dev_opts
, "chardev", label
);
2562 static int debugcon_parse(const char *devname
)
2566 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2569 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2571 fprintf(stderr
, "qemu: already have a debugcon device\n");
2574 qemu_opt_set(opts
, "driver", "isa-debugcon");
2575 qemu_opt_set(opts
, "chardev", "debugcon");
2579 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2580 const char **poptarg
, int *poptind
)
2582 const QEMUOption
*popt
;
2583 int optind
= *poptind
;
2584 char *r
= argv
[optind
];
2587 loc_set_cmdline(argv
, optind
, 1);
2589 /* Treat --foo the same as -foo. */
2592 popt
= qemu_options
;
2595 error_report("invalid option");
2598 if (!strcmp(popt
->name
, r
+ 1))
2602 if (popt
->flags
& HAS_ARG
) {
2603 if (optind
>= argc
) {
2604 error_report("requires an argument");
2607 optarg
= argv
[optind
++];
2608 loc_set_cmdline(argv
, optind
- 2, 2);
2619 int main(int argc
, char **argv
, char **envp
)
2621 const char *gdbstub_dev
= NULL
;
2622 uint32_t boot_devices_bitmap
= 0;
2624 int snapshot
, linux_boot
, net_boot
;
2625 const char *icount_option
= NULL
;
2626 const char *initrd_filename
;
2627 const char *kernel_filename
, *kernel_cmdline
;
2628 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2630 DisplayChangeListener
*dcl
;
2631 int cyls
, heads
, secs
, translation
;
2632 QemuOpts
*hda_opts
= NULL
, *opts
;
2635 const char *loadvm
= NULL
;
2636 QEMUMachine
*machine
;
2637 const char *cpu_model
;
2642 const char *pid_file
= NULL
;
2643 const char *incoming
= NULL
;
2646 struct passwd
*pwd
= NULL
;
2647 const char *chroot_dir
= NULL
;
2648 const char *run_as
= NULL
;
2650 int show_vnc_port
= 0;
2653 error_set_progname(argv
[0]);
2657 qemu_cache_utils_init(envp
);
2659 QLIST_INIT (&vm_change_state_head
);
2662 struct sigaction act
;
2663 sigfillset(&act
.sa_mask
);
2665 act
.sa_handler
= SIG_IGN
;
2666 sigaction(SIGPIPE
, &act
, NULL
);
2669 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2670 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2671 QEMU to run on a single CPU */
2676 h
= GetCurrentProcess();
2677 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2678 for(i
= 0; i
< 32; i
++) {
2679 if (mask
& (1 << i
))
2684 SetProcessAffinityMask(h
, mask
);
2690 module_call_init(MODULE_INIT_MACHINE
);
2691 machine
= find_default_machine();
2693 initrd_filename
= NULL
;
2696 kernel_filename
= NULL
;
2697 kernel_cmdline
= "";
2698 cyls
= heads
= secs
= 0;
2699 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2701 for (i
= 0; i
< MAX_NODES
; i
++) {
2703 node_cpumask
[i
] = 0;
2712 /* first pass of option parsing */
2714 while (optind
< argc
) {
2715 if (argv
[optind
][0] != '-') {
2720 const QEMUOption
*popt
;
2722 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2723 switch (popt
->index
) {
2724 case QEMU_OPTION_nodefconfig
:
2735 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2736 fp
= fopen(fname
, "r");
2738 if (qemu_config_parse(fp
, fname
) != 0) {
2744 fname
= arch_config_name
;
2745 fp
= fopen(fname
, "r");
2747 if (qemu_config_parse(fp
, fname
) != 0) {
2755 /* second pass of option parsing */
2760 if (argv
[optind
][0] != '-') {
2761 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2763 const QEMUOption
*popt
;
2765 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2766 if (!(popt
->arch_mask
& arch_type
)) {
2767 printf("Option %s not supported for this target\n", popt
->name
);
2770 switch(popt
->index
) {
2772 machine
= find_machine(optarg
);
2775 printf("Supported machines are:\n");
2776 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2778 printf("%-10s %s (alias of %s)\n",
2779 m
->alias
, m
->desc
, m
->name
);
2780 printf("%-10s %s%s\n",
2782 m
->is_default
? " (default)" : "");
2784 exit(*optarg
!= '?');
2787 case QEMU_OPTION_cpu
:
2788 /* hw initialization will check this */
2789 if (*optarg
== '?') {
2790 /* XXX: implement xxx_cpu_list for targets that still miss it */
2791 #if defined(cpu_list_id)
2792 cpu_list_id(stdout
, &fprintf
, optarg
);
2793 #elif defined(cpu_list)
2794 cpu_list(stdout
, &fprintf
); /* deprecated */
2801 case QEMU_OPTION_initrd
:
2802 initrd_filename
= optarg
;
2804 case QEMU_OPTION_hda
:
2806 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2808 hda_opts
= drive_add(optarg
, HD_ALIAS
2809 ",cyls=%d,heads=%d,secs=%d%s",
2810 0, cyls
, heads
, secs
,
2811 translation
== BIOS_ATA_TRANSLATION_LBA
?
2813 translation
== BIOS_ATA_TRANSLATION_NONE
?
2814 ",trans=none" : "");
2816 case QEMU_OPTION_hdb
:
2817 case QEMU_OPTION_hdc
:
2818 case QEMU_OPTION_hdd
:
2819 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2821 case QEMU_OPTION_drive
:
2822 drive_add(NULL
, "%s", optarg
);
2824 case QEMU_OPTION_set
:
2825 if (qemu_set_option(optarg
) != 0)
2828 case QEMU_OPTION_global
:
2829 if (qemu_global_option(optarg
) != 0)
2832 case QEMU_OPTION_mtdblock
:
2833 drive_add(optarg
, MTD_ALIAS
);
2835 case QEMU_OPTION_sd
:
2836 drive_add(optarg
, SD_ALIAS
);
2838 case QEMU_OPTION_pflash
:
2839 drive_add(optarg
, PFLASH_ALIAS
);
2841 case QEMU_OPTION_snapshot
:
2844 case QEMU_OPTION_hdachs
:
2848 cyls
= strtol(p
, (char **)&p
, 0);
2849 if (cyls
< 1 || cyls
> 16383)
2854 heads
= strtol(p
, (char **)&p
, 0);
2855 if (heads
< 1 || heads
> 16)
2860 secs
= strtol(p
, (char **)&p
, 0);
2861 if (secs
< 1 || secs
> 63)
2865 if (!strcmp(p
, "none"))
2866 translation
= BIOS_ATA_TRANSLATION_NONE
;
2867 else if (!strcmp(p
, "lba"))
2868 translation
= BIOS_ATA_TRANSLATION_LBA
;
2869 else if (!strcmp(p
, "auto"))
2870 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2873 } else if (*p
!= '\0') {
2875 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2878 if (hda_opts
!= NULL
) {
2880 snprintf(num
, sizeof(num
), "%d", cyls
);
2881 qemu_opt_set(hda_opts
, "cyls", num
);
2882 snprintf(num
, sizeof(num
), "%d", heads
);
2883 qemu_opt_set(hda_opts
, "heads", num
);
2884 snprintf(num
, sizeof(num
), "%d", secs
);
2885 qemu_opt_set(hda_opts
, "secs", num
);
2886 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2887 qemu_opt_set(hda_opts
, "trans", "lba");
2888 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2889 qemu_opt_set(hda_opts
, "trans", "none");
2893 case QEMU_OPTION_numa
:
2894 if (nb_numa_nodes
>= MAX_NODES
) {
2895 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2900 case QEMU_OPTION_nographic
:
2901 display_type
= DT_NOGRAPHIC
;
2903 #ifdef CONFIG_CURSES
2904 case QEMU_OPTION_curses
:
2905 display_type
= DT_CURSES
;
2908 case QEMU_OPTION_portrait
:
2911 case QEMU_OPTION_kernel
:
2912 kernel_filename
= optarg
;
2914 case QEMU_OPTION_append
:
2915 kernel_cmdline
= optarg
;
2917 case QEMU_OPTION_cdrom
:
2918 drive_add(optarg
, CDROM_ALIAS
);
2920 case QEMU_OPTION_boot
:
2922 static const char * const params
[] = {
2923 "order", "once", "menu", NULL
2925 char buf
[sizeof(boot_devices
)];
2926 char *standard_boot_devices
;
2929 if (!strchr(optarg
, '=')) {
2931 pstrcpy(buf
, sizeof(buf
), optarg
);
2932 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2934 "qemu: unknown boot parameter '%s' in '%s'\n",
2940 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2941 boot_devices_bitmap
= parse_bootdevices(buf
);
2942 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2945 if (get_param_value(buf
, sizeof(buf
),
2947 boot_devices_bitmap
|= parse_bootdevices(buf
);
2948 standard_boot_devices
= qemu_strdup(boot_devices
);
2949 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2950 qemu_register_reset(restore_boot_devices
,
2951 standard_boot_devices
);
2953 if (get_param_value(buf
, sizeof(buf
),
2955 if (!strcmp(buf
, "on")) {
2957 } else if (!strcmp(buf
, "off")) {
2961 "qemu: invalid option value '%s'\n",
2969 case QEMU_OPTION_fda
:
2970 case QEMU_OPTION_fdb
:
2971 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2973 case QEMU_OPTION_no_fd_bootchk
:
2976 case QEMU_OPTION_netdev
:
2977 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2981 case QEMU_OPTION_net
:
2982 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2987 case QEMU_OPTION_tftp
:
2988 legacy_tftp_prefix
= optarg
;
2990 case QEMU_OPTION_bootp
:
2991 legacy_bootp_filename
= optarg
;
2994 case QEMU_OPTION_smb
:
2995 if (net_slirp_smb(optarg
) < 0)
2999 case QEMU_OPTION_redir
:
3000 if (net_slirp_redir(optarg
) < 0)
3004 case QEMU_OPTION_bt
:
3005 add_device_config(DEV_BT
, optarg
);
3007 case QEMU_OPTION_audio_help
:
3008 if (!(audio_available())) {
3009 printf("Option %s not supported for this target\n", popt
->name
);
3015 case QEMU_OPTION_soundhw
:
3016 if (!(audio_available())) {
3017 printf("Option %s not supported for this target\n", popt
->name
);
3020 select_soundhw (optarg
);
3025 case QEMU_OPTION_version
:
3029 case QEMU_OPTION_m
: {
3033 value
= strtoul(optarg
, &ptr
, 10);
3035 case 0: case 'M': case 'm':
3042 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3046 /* On 32-bit hosts, QEMU is limited by virtual address space */
3047 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3048 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3051 if (value
!= (uint64_t)(ram_addr_t
)value
) {
3052 fprintf(stderr
, "qemu: ram size too large\n");
3058 case QEMU_OPTION_mempath
:
3062 case QEMU_OPTION_mem_prealloc
:
3067 set_cpu_log(optarg
);
3070 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3072 case QEMU_OPTION_gdb
:
3073 gdbstub_dev
= optarg
;
3078 case QEMU_OPTION_bios
:
3081 case QEMU_OPTION_singlestep
:
3088 keyboard_layout
= optarg
;
3090 case QEMU_OPTION_localtime
:
3093 case QEMU_OPTION_vga
:
3094 select_vgahw (optarg
);
3101 w
= strtol(p
, (char **)&p
, 10);
3104 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3110 h
= strtol(p
, (char **)&p
, 10);
3115 depth
= strtol(p
, (char **)&p
, 10);
3116 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3117 depth
!= 24 && depth
!= 32)
3119 } else if (*p
== '\0') {
3120 depth
= graphic_depth
;
3127 graphic_depth
= depth
;
3130 case QEMU_OPTION_echr
:
3133 term_escape_char
= strtol(optarg
, &r
, 0);
3135 printf("Bad argument to echr\n");
3138 case QEMU_OPTION_monitor
:
3139 monitor_parse(optarg
, "readline");
3140 default_monitor
= 0;
3142 case QEMU_OPTION_qmp
:
3143 monitor_parse(optarg
, "control");
3144 default_monitor
= 0;
3146 case QEMU_OPTION_mon
:
3147 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3149 fprintf(stderr
, "parse error: %s\n", optarg
);
3152 default_monitor
= 0;
3154 case QEMU_OPTION_chardev
:
3155 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3157 fprintf(stderr
, "parse error: %s\n", optarg
);
3161 case QEMU_OPTION_serial
:
3162 add_device_config(DEV_SERIAL
, optarg
);
3164 if (strncmp(optarg
, "mon:", 4) == 0) {
3165 default_monitor
= 0;
3168 case QEMU_OPTION_watchdog
:
3171 "qemu: only one watchdog option may be given\n");
3176 case QEMU_OPTION_watchdog_action
:
3177 if (select_watchdog_action(optarg
) == -1) {
3178 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3182 case QEMU_OPTION_virtiocon
:
3183 add_device_config(DEV_VIRTCON
, optarg
);
3184 default_virtcon
= 0;
3185 if (strncmp(optarg
, "mon:", 4) == 0) {
3186 default_monitor
= 0;
3189 case QEMU_OPTION_parallel
:
3190 add_device_config(DEV_PARALLEL
, optarg
);
3191 default_parallel
= 0;
3192 if (strncmp(optarg
, "mon:", 4) == 0) {
3193 default_monitor
= 0;
3196 case QEMU_OPTION_debugcon
:
3197 add_device_config(DEV_DEBUGCON
, optarg
);
3199 case QEMU_OPTION_loadvm
:
3202 case QEMU_OPTION_full_screen
:
3206 case QEMU_OPTION_no_frame
:
3209 case QEMU_OPTION_alt_grab
:
3212 case QEMU_OPTION_ctrl_grab
:
3215 case QEMU_OPTION_no_quit
:
3218 case QEMU_OPTION_sdl
:
3219 display_type
= DT_SDL
;
3222 case QEMU_OPTION_pidfile
:
3225 case QEMU_OPTION_win2k_hack
:
3226 win2k_install_hack
= 1;
3228 case QEMU_OPTION_rtc_td_hack
:
3231 case QEMU_OPTION_acpitable
:
3232 do_acpitable_option(optarg
);
3234 case QEMU_OPTION_smbios
:
3235 do_smbios_option(optarg
);
3237 case QEMU_OPTION_enable_kvm
:
3238 if (!(kvm_available())) {
3239 printf("Option %s not supported for this target\n", popt
->name
);
3244 case QEMU_OPTION_usb
:
3247 case QEMU_OPTION_usbdevice
:
3249 add_device_config(DEV_USB
, optarg
);
3251 case QEMU_OPTION_device
:
3252 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3256 case QEMU_OPTION_smp
:
3259 fprintf(stderr
, "Invalid number of CPUs\n");
3262 if (max_cpus
< smp_cpus
) {
3263 fprintf(stderr
, "maxcpus must be equal to or greater than "
3267 if (max_cpus
> 255) {
3268 fprintf(stderr
, "Unsupported number of maxcpus\n");
3272 case QEMU_OPTION_vnc
:
3273 display_type
= DT_VNC
;
3274 vnc_display
= optarg
;
3276 case QEMU_OPTION_no_acpi
:
3279 case QEMU_OPTION_no_hpet
:
3282 case QEMU_OPTION_balloon
:
3283 if (balloon_parse(optarg
) < 0) {
3284 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3288 case QEMU_OPTION_no_reboot
:
3291 case QEMU_OPTION_no_shutdown
:
3294 case QEMU_OPTION_show_cursor
:
3297 case QEMU_OPTION_uuid
:
3298 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3299 fprintf(stderr
, "Fail to parse UUID string."
3300 " Wrong format.\n");
3305 case QEMU_OPTION_daemonize
:
3309 case QEMU_OPTION_option_rom
:
3310 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3311 fprintf(stderr
, "Too many option ROMs\n");
3314 option_rom
[nb_option_roms
] = optarg
;
3317 case QEMU_OPTION_semihosting
:
3318 semihosting_enabled
= 1;
3320 case QEMU_OPTION_name
:
3321 qemu_name
= qemu_strdup(optarg
);
3323 char *p
= strchr(qemu_name
, ',');
3326 if (strncmp(p
, "process=", 8)) {
3327 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3335 case QEMU_OPTION_prom_env
:
3336 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3337 fprintf(stderr
, "Too many prom variables\n");
3340 prom_envs
[nb_prom_envs
] = optarg
;
3343 case QEMU_OPTION_old_param
:
3346 case QEMU_OPTION_clock
:
3347 configure_alarms(optarg
);
3349 case QEMU_OPTION_startdate
:
3350 configure_rtc_date_offset(optarg
, 1);
3352 case QEMU_OPTION_rtc
:
3353 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3355 fprintf(stderr
, "parse error: %s\n", optarg
);
3358 configure_rtc(opts
);
3360 case QEMU_OPTION_tb_size
:
3361 tb_size
= strtol(optarg
, NULL
, 0);
3365 case QEMU_OPTION_icount
:
3366 icount_option
= optarg
;
3368 case QEMU_OPTION_incoming
:
3371 case QEMU_OPTION_nodefaults
:
3373 default_parallel
= 0;
3374 default_virtcon
= 0;
3375 default_monitor
= 0;
3383 case QEMU_OPTION_chroot
:
3384 chroot_dir
= optarg
;
3386 case QEMU_OPTION_runas
:
3390 case QEMU_OPTION_xen_domid
:
3391 if (!(xen_available())) {
3392 printf("Option %s not supported for this target\n", popt
->name
);
3395 xen_domid
= atoi(optarg
);
3397 case QEMU_OPTION_xen_create
:
3398 if (!(xen_available())) {
3399 printf("Option %s not supported for this target\n", popt
->name
);
3402 xen_mode
= XEN_CREATE
;
3404 case QEMU_OPTION_xen_attach
:
3405 if (!(xen_available())) {
3406 printf("Option %s not supported for this target\n", popt
->name
);
3409 xen_mode
= XEN_ATTACH
;
3411 case QEMU_OPTION_readconfig
:
3414 fp
= fopen(optarg
, "r");
3416 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3419 if (qemu_config_parse(fp
, optarg
) != 0) {
3425 case QEMU_OPTION_writeconfig
:
3428 if (strcmp(optarg
, "-") == 0) {
3431 fp
= fopen(optarg
, "w");
3433 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3437 qemu_config_write(fp
);
3446 /* If no data_dir is specified then try to find it relative to the
3449 data_dir
= find_datadir(argv
[0]);
3451 /* If all else fails use the install patch specified when building. */
3453 data_dir
= CONFIG_QEMU_SHAREDIR
;
3457 * Default to max_cpus = smp_cpus, in case the user doesn't
3458 * specify a max_cpus value.
3461 max_cpus
= smp_cpus
;
3463 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3464 if (smp_cpus
> machine
->max_cpus
) {
3465 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3466 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3471 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3472 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3474 if (machine
->no_serial
) {
3477 if (machine
->no_parallel
) {
3478 default_parallel
= 0;
3480 if (!machine
->use_virtcon
) {
3481 default_virtcon
= 0;
3483 if (machine
->no_vga
) {
3486 if (machine
->no_floppy
) {
3489 if (machine
->no_cdrom
) {
3492 if (machine
->no_sdcard
) {
3496 if (display_type
== DT_NOGRAPHIC
) {
3497 if (default_parallel
)
3498 add_device_config(DEV_PARALLEL
, "null");
3499 if (default_serial
&& default_monitor
) {
3500 add_device_config(DEV_SERIAL
, "mon:stdio");
3501 } else if (default_virtcon
&& default_monitor
) {
3502 add_device_config(DEV_VIRTCON
, "mon:stdio");
3505 add_device_config(DEV_SERIAL
, "stdio");
3506 if (default_virtcon
)
3507 add_device_config(DEV_VIRTCON
, "stdio");
3508 if (default_monitor
)
3509 monitor_parse("stdio", "readline");
3513 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3514 if (default_parallel
)
3515 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3516 if (default_monitor
)
3517 monitor_parse("vc:80Cx24C", "readline");
3518 if (default_virtcon
)
3519 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3522 vga_interface_type
= VGA_CIRRUS
;
3524 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3531 if (pipe(fds
) == -1)
3542 len
= read(fds
[0], &status
, 1);
3543 if (len
== -1 && (errno
== EINTR
))
3548 else if (status
== 1) {
3549 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3557 qemu_set_cloexec(fds
[1]);
3569 signal(SIGTSTP
, SIG_IGN
);
3570 signal(SIGTTOU
, SIG_IGN
);
3571 signal(SIGTTIN
, SIG_IGN
);
3575 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3579 if (write(fds
[1], &status
, 1) != 1) {
3580 perror("daemonize. Writing to pipe\n");
3584 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3588 if (kvm_enabled()) {
3591 ret
= kvm_init(smp_cpus
);
3593 fprintf(stderr
, "failed to initialize KVM\n");
3598 if (qemu_init_main_loop()) {
3599 fprintf(stderr
, "qemu_init_main_loop failed\n");
3602 linux_boot
= (kernel_filename
!= NULL
);
3604 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3605 fprintf(stderr
, "-append only allowed with -kernel option\n");
3609 if (!linux_boot
&& initrd_filename
!= NULL
) {
3610 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3615 /* Win32 doesn't support line-buffering and requires size >= 2 */
3616 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3619 if (init_timer_alarm() < 0) {
3620 fprintf(stderr
, "could not initialize alarm timer\n");
3623 configure_icount(icount_option
);
3629 if (net_init_clients() < 0) {
3633 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
3634 net_set_boot_mask(net_boot
);
3636 /* init the bluetooth world */
3637 if (foreach_device_config(DEV_BT
, bt_parse
))
3640 /* init the memory */
3642 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3644 /* init the dynamic translator */
3645 cpu_exec_init_all(tb_size
* 1024 * 1024);
3647 bdrv_init_with_whitelist();
3651 if (default_cdrom
) {
3652 /* we always create the cdrom drive, even if no disk is there */
3653 drive_add(NULL
, CDROM_ALIAS
);
3656 if (default_floppy
) {
3657 /* we always create at least one floppy */
3658 drive_add(NULL
, FD_ALIAS
, 0);
3661 if (default_sdcard
) {
3662 /* we always create one sd slot, even if no card is in it */
3663 drive_add(NULL
, SD_ALIAS
);
3666 /* open the virtual block devices */
3668 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3669 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3672 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3675 if (nb_numa_nodes
> 0) {
3678 if (nb_numa_nodes
> smp_cpus
) {
3679 nb_numa_nodes
= smp_cpus
;
3682 /* If no memory size if given for any node, assume the default case
3683 * and distribute the available memory equally across all nodes
3685 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3686 if (node_mem
[i
] != 0)
3689 if (i
== nb_numa_nodes
) {
3690 uint64_t usedmem
= 0;
3692 /* On Linux, the each node's border has to be 8MB aligned,
3693 * the final node gets the rest.
3695 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3696 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3697 usedmem
+= node_mem
[i
];
3699 node_mem
[i
] = ram_size
- usedmem
;
3702 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3703 if (node_cpumask
[i
] != 0)
3706 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3707 * must cope with this anyway, because there are BIOSes out there in
3708 * real machines which also use this scheme.
3710 if (i
== nb_numa_nodes
) {
3711 for (i
= 0; i
< smp_cpus
; i
++) {
3712 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3717 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3719 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3721 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3723 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3726 module_call_init(MODULE_INIT_DEVICE
);
3728 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3732 i
= select_watchdog(watchdog
);
3734 exit (i
== 1 ? 1 : 0);
3737 if (machine
->compat_props
) {
3738 qdev_prop_register_global_list(machine
->compat_props
);
3742 machine
->init(ram_size
, boot_devices
,
3743 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3745 cpu_synchronize_all_post_init();
3748 /* must be after terminal init, SDL library changes signal handlers */
3754 current_machine
= machine
;
3756 /* init USB devices */
3758 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3762 /* init generic devices */
3763 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3766 net_check_clients();
3768 /* just use the first displaystate for the moment */
3769 ds
= get_displaystate();
3771 if (display_type
== DT_DEFAULT
) {
3772 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3773 display_type
= DT_SDL
;
3775 display_type
= DT_VNC
;
3776 vnc_display
= "localhost:0,to=99";
3782 switch (display_type
) {
3785 #if defined(CONFIG_CURSES)
3787 curses_display_init(ds
, full_screen
);
3790 #if defined(CONFIG_SDL)
3792 sdl_display_init(ds
, full_screen
, no_frame
);
3794 #elif defined(CONFIG_COCOA)
3796 cocoa_display_init(ds
, full_screen
);
3800 vnc_display_init(ds
);
3801 if (vnc_display_open(ds
, vnc_display
) < 0)
3804 if (show_vnc_port
) {
3805 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3813 dcl
= ds
->listeners
;
3814 while (dcl
!= NULL
) {
3815 if (dcl
->dpy_refresh
!= NULL
) {
3816 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3817 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3822 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3823 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3824 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3827 text_consoles_set_display(ds
);
3829 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3832 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3833 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3838 qdev_machine_creation_done();
3840 if (rom_load_all() != 0) {
3841 fprintf(stderr
, "rom loading failed\n");
3845 qemu_system_reset();
3847 if (load_vmstate(loadvm
) < 0) {
3853 qemu_start_incoming_migration(incoming
);
3854 } else if (autostart
) {
3864 len
= write(fds
[1], &status
, 1);
3865 if (len
== -1 && (errno
== EINTR
))
3872 perror("not able to chdir to /");
3875 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3881 pwd
= getpwnam(run_as
);
3883 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3889 if (chroot(chroot_dir
) < 0) {
3890 fprintf(stderr
, "chroot failed\n");
3894 perror("not able to chdir to /");
3900 if (setgid(pwd
->pw_gid
) < 0) {
3901 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3904 if (setuid(pwd
->pw_uid
) < 0) {
3905 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3908 if (setuid(0) != -1) {
3909 fprintf(stderr
, "Dropping privileges failed\n");