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
);
497 /***********************************************************/
498 /* Bluetooth support */
501 static struct HCIInfo
*hci_table
[MAX_NICS
];
503 static struct bt_vlan_s
{
504 struct bt_scatternet_s net
;
506 struct bt_vlan_s
*next
;
509 /* find or alloc a new bluetooth "VLAN" */
510 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
512 struct bt_vlan_s
**pvlan
, *vlan
;
513 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
517 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
519 pvlan
= &first_bt_vlan
;
520 while (*pvlan
!= NULL
)
521 pvlan
= &(*pvlan
)->next
;
526 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
530 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
535 static struct HCIInfo null_hci
= {
536 .cmd_send
= null_hci_send
,
537 .sco_send
= null_hci_send
,
538 .acl_send
= null_hci_send
,
539 .bdaddr_set
= null_hci_addr_set
,
542 struct HCIInfo
*qemu_next_hci(void)
544 if (cur_hci
== nb_hcis
)
547 return hci_table
[cur_hci
++];
550 static struct HCIInfo
*hci_init(const char *str
)
553 struct bt_scatternet_s
*vlan
= 0;
555 if (!strcmp(str
, "null"))
558 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
560 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
561 else if (!strncmp(str
, "hci", 3)) {
564 if (!strncmp(str
+ 3, ",vlan=", 6)) {
565 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
570 vlan
= qemu_find_bt_vlan(0);
572 return bt_new_hci(vlan
);
575 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
580 static int bt_hci_parse(const char *str
)
585 if (nb_hcis
>= MAX_NICS
) {
586 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
599 bdaddr
.b
[5] = 0x56 + nb_hcis
;
600 hci
->bdaddr_set(hci
, bdaddr
.b
);
602 hci_table
[nb_hcis
++] = hci
;
607 static void bt_vhci_add(int vlan_id
)
609 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
612 fprintf(stderr
, "qemu: warning: adding a VHCI to "
613 "an empty scatternet %i\n", vlan_id
);
615 bt_vhci_init(bt_new_hci(vlan
));
618 static struct bt_device_s
*bt_device_add(const char *opt
)
620 struct bt_scatternet_s
*vlan
;
622 char *endp
= strstr(opt
, ",vlan=");
623 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
626 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
629 vlan_id
= strtol(endp
+ 6, &endp
, 0);
631 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
636 vlan
= qemu_find_bt_vlan(vlan_id
);
639 fprintf(stderr
, "qemu: warning: adding a slave device to "
640 "an empty scatternet %i\n", vlan_id
);
642 if (!strcmp(devname
, "keyboard"))
643 return bt_keyboard_init(vlan
);
645 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
649 static int bt_parse(const char *opt
)
651 const char *endp
, *p
;
654 if (strstart(opt
, "hci", &endp
)) {
655 if (!*endp
|| *endp
== ',') {
657 if (!strstart(endp
, ",vlan=", 0))
660 return bt_hci_parse(opt
);
662 } else if (strstart(opt
, "vhci", &endp
)) {
663 if (!*endp
|| *endp
== ',') {
665 if (strstart(endp
, ",vlan=", &p
)) {
666 vlan
= strtol(p
, (char **) &endp
, 0);
668 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
672 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
681 } else if (strstart(opt
, "device:", &endp
))
682 return !bt_device_add(endp
);
684 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
688 /***********************************************************/
689 /* QEMU Block devices */
691 #define HD_ALIAS "index=%d,media=disk"
692 #define CDROM_ALIAS "index=2,media=cdrom"
693 #define FD_ALIAS "index=%d,if=floppy"
694 #define PFLASH_ALIAS "if=pflash"
695 #define MTD_ALIAS "if=mtd"
696 #define SD_ALIAS "index=0,if=sd"
698 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
705 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
708 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
710 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
711 __FUNCTION__
, optstr
);
715 qemu_opt_set(opts
, "file", file
);
719 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
723 /* seek interface, bus and unit */
725 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
726 if (dinfo
->type
== type
&&
735 DriveInfo
*drive_get_by_id(const char *id
)
739 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
740 if (strcmp(id
, dinfo
->id
))
747 int drive_get_max_bus(BlockInterfaceType type
)
753 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
754 if(dinfo
->type
== type
&&
755 dinfo
->bus
> max_bus
)
756 max_bus
= dinfo
->bus
;
761 const char *drive_get_serial(BlockDriverState
*bdrv
)
765 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
766 if (dinfo
->bdrv
== bdrv
)
767 return dinfo
->serial
;
773 BlockInterfaceErrorAction
drive_get_on_error(
774 BlockDriverState
*bdrv
, int is_read
)
778 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
779 if (dinfo
->bdrv
== bdrv
)
780 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
783 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
786 static void bdrv_format_print(void *opaque
, const char *name
)
788 fprintf(stderr
, " %s", name
);
791 void drive_uninit(DriveInfo
*dinfo
)
793 qemu_opts_del(dinfo
->opts
);
794 bdrv_delete(dinfo
->bdrv
);
795 QTAILQ_REMOVE(&drives
, dinfo
, next
);
799 static int parse_block_error_action(const char *buf
, int is_read
)
801 if (!strcmp(buf
, "ignore")) {
802 return BLOCK_ERR_IGNORE
;
803 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
804 return BLOCK_ERR_STOP_ENOSPC
;
805 } else if (!strcmp(buf
, "stop")) {
806 return BLOCK_ERR_STOP_ANY
;
807 } else if (!strcmp(buf
, "report")) {
808 return BLOCK_ERR_REPORT
;
810 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
811 buf
, is_read
? "read" : "write");
816 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
820 const char *file
= NULL
;
823 const char *mediastr
= "";
824 BlockInterfaceType type
;
825 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
827 int cyls
, heads
, secs
, translation
;
828 BlockDriver
*drv
= NULL
;
829 QEMUMachine
*machine
= opaque
;
836 int on_read_error
, on_write_error
;
843 translation
= BIOS_ATA_TRANSLATION_AUTO
;
846 if (machine
&& machine
->use_scsi
) {
848 max_devs
= MAX_SCSI_DEVS
;
849 pstrcpy(devname
, sizeof(devname
), "scsi");
852 max_devs
= MAX_IDE_DEVS
;
853 pstrcpy(devname
, sizeof(devname
), "ide");
857 /* extract parameters */
858 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
859 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
860 index
= qemu_opt_get_number(opts
, "index", -1);
862 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
863 heads
= qemu_opt_get_number(opts
, "heads", 0);
864 secs
= qemu_opt_get_number(opts
, "secs", 0);
866 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
867 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
869 file
= qemu_opt_get(opts
, "file");
870 serial
= qemu_opt_get(opts
, "serial");
872 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
873 pstrcpy(devname
, sizeof(devname
), buf
);
874 if (!strcmp(buf
, "ide")) {
876 max_devs
= MAX_IDE_DEVS
;
877 } else if (!strcmp(buf
, "scsi")) {
879 max_devs
= MAX_SCSI_DEVS
;
880 } else if (!strcmp(buf
, "floppy")) {
883 } else if (!strcmp(buf
, "pflash")) {
886 } else if (!strcmp(buf
, "mtd")) {
889 } else if (!strcmp(buf
, "sd")) {
892 } else if (!strcmp(buf
, "virtio")) {
895 } else if (!strcmp(buf
, "xen")) {
898 } else if (!strcmp(buf
, "none")) {
902 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
907 if (cyls
|| heads
|| secs
) {
908 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
909 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
912 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
913 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
916 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
917 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
922 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
925 "qemu: '%s' trans must be used with cyls,heads and secs\n",
929 if (!strcmp(buf
, "none"))
930 translation
= BIOS_ATA_TRANSLATION_NONE
;
931 else if (!strcmp(buf
, "lba"))
932 translation
= BIOS_ATA_TRANSLATION_LBA
;
933 else if (!strcmp(buf
, "auto"))
934 translation
= BIOS_ATA_TRANSLATION_AUTO
;
936 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
941 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
942 if (!strcmp(buf
, "disk")) {
944 } else if (!strcmp(buf
, "cdrom")) {
945 if (cyls
|| secs
|| heads
) {
947 "qemu: '%s' invalid physical CHS format\n", buf
);
952 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
957 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
958 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
960 else if (!strcmp(buf
, "writethrough"))
962 else if (!strcmp(buf
, "writeback"))
965 fprintf(stderr
, "qemu: invalid cache option\n");
970 #ifdef CONFIG_LINUX_AIO
971 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
972 if (!strcmp(buf
, "threads"))
974 else if (!strcmp(buf
, "native"))
977 fprintf(stderr
, "qemu: invalid aio option\n");
983 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
984 if (strcmp(buf
, "?") == 0) {
985 fprintf(stderr
, "qemu: Supported formats:");
986 bdrv_iterate_format(bdrv_format_print
, NULL
);
987 fprintf(stderr
, "\n");
990 drv
= bdrv_find_whitelisted_format(buf
);
992 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
997 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
998 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
999 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1000 fprintf(stderr
, "werror is no supported by this format\n");
1004 on_write_error
= parse_block_error_action(buf
, 0);
1005 if (on_write_error
< 0) {
1010 on_read_error
= BLOCK_ERR_REPORT
;
1011 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1012 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1013 fprintf(stderr
, "rerror is no supported by this format\n");
1017 on_read_error
= parse_block_error_action(buf
, 1);
1018 if (on_read_error
< 0) {
1023 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1024 if (type
!= IF_VIRTIO
) {
1025 fprintf(stderr
, "addr is not supported\n");
1030 /* compute bus and unit according index */
1033 if (bus_id
!= 0 || unit_id
!= -1) {
1035 "qemu: index cannot be used with bus and unit\n");
1043 unit_id
= index
% max_devs
;
1044 bus_id
= index
/ max_devs
;
1048 /* if user doesn't specify a unit_id,
1049 * try to find the first free
1052 if (unit_id
== -1) {
1054 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1056 if (max_devs
&& unit_id
>= max_devs
) {
1057 unit_id
-= max_devs
;
1065 if (max_devs
&& unit_id
>= max_devs
) {
1066 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1067 unit_id
, max_devs
- 1);
1072 * ignore multiple definitions
1075 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1082 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1083 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1084 dinfo
->id
= qemu_strdup(buf
);
1086 /* no id supplied -> create one */
1087 dinfo
->id
= qemu_mallocz(32);
1088 if (type
== IF_IDE
|| type
== IF_SCSI
)
1089 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1091 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1092 devname
, bus_id
, mediastr
, unit_id
);
1094 snprintf(dinfo
->id
, 32, "%s%s%i",
1095 devname
, mediastr
, unit_id
);
1097 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1098 dinfo
->devaddr
= devaddr
;
1100 dinfo
->bus
= bus_id
;
1101 dinfo
->unit
= unit_id
;
1102 dinfo
->on_read_error
= on_read_error
;
1103 dinfo
->on_write_error
= on_write_error
;
1106 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1107 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1117 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1118 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1122 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1127 /* FIXME: This isn't really a floppy, but it's a reasonable
1130 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1136 /* add virtio block device */
1137 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1138 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1139 qemu_opt_set(opts
, "drive", dinfo
->id
);
1141 qemu_opt_set(opts
, "addr", devaddr
);
1152 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1153 cache
= 2; /* always use write-back with snapshot */
1155 if (cache
== 0) /* no caching */
1156 bdrv_flags
|= BDRV_O_NOCACHE
;
1157 else if (cache
== 2) /* write-back */
1158 bdrv_flags
|= BDRV_O_CACHE_WB
;
1161 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1163 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1166 if (media
== MEDIA_CDROM
) {
1167 /* CDROM is fine for any interface, don't check. */
1169 } else if (ro
== 1) {
1170 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1171 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1176 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1178 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1179 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1180 file
, strerror(errno
));
1184 if (bdrv_key_required(dinfo
->bdrv
))
1190 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1192 QEMUMachine
*machine
= opaque
;
1193 int fatal_error
= 0;
1195 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1202 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1204 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1205 qemu_opt_set(opts
, "snapshot", "on");
1210 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1212 boot_set_handler
= func
;
1213 boot_set_opaque
= opaque
;
1216 int qemu_boot_set(const char *boot_devices
)
1218 if (!boot_set_handler
) {
1221 return boot_set_handler(boot_set_opaque
, boot_devices
);
1224 static int parse_bootdevices(char *devices
)
1226 /* We just do some generic consistency checks */
1230 for (p
= devices
; *p
!= '\0'; p
++) {
1231 /* Allowed boot devices are:
1232 * a-b: floppy disk drives
1233 * c-f: IDE disk drives
1234 * g-m: machine implementation dependant drives
1235 * n-p: network devices
1236 * It's up to each machine implementation to check if the given boot
1237 * devices match the actual hardware implementation and firmware
1240 if (*p
< 'a' || *p
> 'p') {
1241 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1244 if (bitmap
& (1 << (*p
- 'a'))) {
1245 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1248 bitmap
|= 1 << (*p
- 'a');
1253 static void restore_boot_devices(void *opaque
)
1255 char *standard_boot_devices
= opaque
;
1257 qemu_boot_set(standard_boot_devices
);
1259 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1260 qemu_free(standard_boot_devices
);
1263 static void numa_add(const char *optarg
)
1267 unsigned long long value
, endvalue
;
1270 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1271 if (!strcmp(option
, "node")) {
1272 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1273 nodenr
= nb_numa_nodes
;
1275 nodenr
= strtoull(option
, NULL
, 10);
1278 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1279 node_mem
[nodenr
] = 0;
1281 value
= strtoull(option
, &endptr
, 0);
1283 case 0: case 'M': case 'm':
1290 node_mem
[nodenr
] = value
;
1292 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1293 node_cpumask
[nodenr
] = 0;
1295 value
= strtoull(option
, &endptr
, 10);
1298 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1300 if (*endptr
== '-') {
1301 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1302 if (endvalue
>= 63) {
1305 "only 63 CPUs in NUMA mode supported.\n");
1307 value
= (2ULL << endvalue
) - (1ULL << value
);
1309 value
= 1ULL << value
;
1312 node_cpumask
[nodenr
] = value
;
1319 static void smp_parse(const char *optarg
)
1321 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1325 smp
= strtoul(optarg
, &endptr
, 10);
1326 if (endptr
!= optarg
) {
1327 if (*endptr
== ',') {
1331 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1332 sockets
= strtoull(option
, NULL
, 10);
1333 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1334 cores
= strtoull(option
, NULL
, 10);
1335 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1336 threads
= strtoull(option
, NULL
, 10);
1337 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1338 max_cpus
= strtoull(option
, NULL
, 10);
1340 /* compute missing values, prefer sockets over cores over threads */
1341 if (smp
== 0 || sockets
== 0) {
1342 sockets
= sockets
> 0 ? sockets
: 1;
1343 cores
= cores
> 0 ? cores
: 1;
1344 threads
= threads
> 0 ? threads
: 1;
1346 smp
= cores
* threads
* sockets
;
1350 threads
= threads
> 0 ? threads
: 1;
1351 cores
= smp
/ (sockets
* threads
);
1354 threads
= smp
/ (cores
* sockets
);
1359 smp_cores
= cores
> 0 ? cores
: 1;
1360 smp_threads
= threads
> 0 ? threads
: 1;
1362 max_cpus
= smp_cpus
;
1365 /***********************************************************/
1368 static int usb_device_add(const char *devname
, int is_hotplug
)
1371 USBDevice
*dev
= NULL
;
1376 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1377 dev
= usbdevice_create(devname
);
1381 /* the other ones */
1382 if (strstart(devname
, "host:", &p
)) {
1383 dev
= usb_host_device_open(p
);
1384 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1385 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1386 bt_new_hci(qemu_find_bt_vlan(0)));
1397 static int usb_device_del(const char *devname
)
1402 if (strstart(devname
, "host:", &p
))
1403 return usb_host_device_close(p
);
1408 p
= strchr(devname
, '.');
1411 bus_num
= strtoul(devname
, NULL
, 0);
1412 addr
= strtoul(p
+ 1, NULL
, 0);
1414 return usb_device_delete_addr(bus_num
, addr
);
1417 static int usb_parse(const char *cmdline
)
1420 r
= usb_device_add(cmdline
, 0);
1422 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1427 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1429 const char *devname
= qdict_get_str(qdict
, "devname");
1430 if (usb_device_add(devname
, 1) < 0) {
1431 error_report("could not add USB device '%s'", devname
);
1435 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1437 const char *devname
= qdict_get_str(qdict
, "devname");
1438 if (usb_device_del(devname
) < 0) {
1439 error_report("could not delete USB device '%s'", devname
);
1443 /***********************************************************/
1444 /* PCMCIA/Cardbus */
1446 static struct pcmcia_socket_entry_s
{
1447 PCMCIASocket
*socket
;
1448 struct pcmcia_socket_entry_s
*next
;
1449 } *pcmcia_sockets
= 0;
1451 void pcmcia_socket_register(PCMCIASocket
*socket
)
1453 struct pcmcia_socket_entry_s
*entry
;
1455 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1456 entry
->socket
= socket
;
1457 entry
->next
= pcmcia_sockets
;
1458 pcmcia_sockets
= entry
;
1461 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1463 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1465 ptr
= &pcmcia_sockets
;
1466 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1467 if (entry
->socket
== socket
) {
1473 void pcmcia_info(Monitor
*mon
)
1475 struct pcmcia_socket_entry_s
*iter
;
1477 if (!pcmcia_sockets
)
1478 monitor_printf(mon
, "No PCMCIA sockets\n");
1480 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1481 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1482 iter
->socket
->attached
? iter
->socket
->card_string
:
1486 /***********************************************************/
1489 typedef struct IOHandlerRecord
{
1491 IOCanReadHandler
*fd_read_poll
;
1493 IOHandler
*fd_write
;
1496 /* temporary data */
1498 QLIST_ENTRY(IOHandlerRecord
) next
;
1501 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1502 QLIST_HEAD_INITIALIZER(io_handlers
);
1505 /* XXX: fd_read_poll should be suppressed, but an API change is
1506 necessary in the character devices to suppress fd_can_read(). */
1507 int qemu_set_fd_handler2(int fd
,
1508 IOCanReadHandler
*fd_read_poll
,
1510 IOHandler
*fd_write
,
1513 IOHandlerRecord
*ioh
;
1515 if (!fd_read
&& !fd_write
) {
1516 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1517 if (ioh
->fd
== fd
) {
1523 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1527 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1528 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1531 ioh
->fd_read_poll
= fd_read_poll
;
1532 ioh
->fd_read
= fd_read
;
1533 ioh
->fd_write
= fd_write
;
1534 ioh
->opaque
= opaque
;
1540 int qemu_set_fd_handler(int fd
,
1542 IOHandler
*fd_write
,
1545 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1549 /***********************************************************/
1550 /* Polling handling */
1552 typedef struct PollingEntry
{
1555 struct PollingEntry
*next
;
1558 static PollingEntry
*first_polling_entry
;
1560 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1562 PollingEntry
**ppe
, *pe
;
1563 pe
= qemu_mallocz(sizeof(PollingEntry
));
1565 pe
->opaque
= opaque
;
1566 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1571 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1573 PollingEntry
**ppe
, *pe
;
1574 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1576 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1584 /***********************************************************/
1585 /* Wait objects support */
1586 typedef struct WaitObjects
{
1588 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1589 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1590 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1593 static WaitObjects wait_objects
= {0};
1595 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1597 WaitObjects
*w
= &wait_objects
;
1599 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1601 w
->events
[w
->num
] = handle
;
1602 w
->func
[w
->num
] = func
;
1603 w
->opaque
[w
->num
] = opaque
;
1608 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1611 WaitObjects
*w
= &wait_objects
;
1614 for (i
= 0; i
< w
->num
; i
++) {
1615 if (w
->events
[i
] == handle
)
1618 w
->events
[i
] = w
->events
[i
+ 1];
1619 w
->func
[i
] = w
->func
[i
+ 1];
1620 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1628 /***********************************************************/
1629 /* machine registration */
1631 static QEMUMachine
*first_machine
= NULL
;
1632 QEMUMachine
*current_machine
= NULL
;
1634 int qemu_register_machine(QEMUMachine
*m
)
1637 pm
= &first_machine
;
1645 static QEMUMachine
*find_machine(const char *name
)
1649 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1650 if (!strcmp(m
->name
, name
))
1652 if (m
->alias
&& !strcmp(m
->alias
, name
))
1658 static QEMUMachine
*find_default_machine(void)
1662 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1663 if (m
->is_default
) {
1670 /***********************************************************/
1671 /* main execution loop */
1673 static void gui_update(void *opaque
)
1675 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1676 DisplayState
*ds
= opaque
;
1677 DisplayChangeListener
*dcl
= ds
->listeners
;
1679 qemu_flush_coalesced_mmio_buffer();
1682 while (dcl
!= NULL
) {
1683 if (dcl
->gui_timer_interval
&&
1684 dcl
->gui_timer_interval
< interval
)
1685 interval
= dcl
->gui_timer_interval
;
1688 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1691 static void nographic_update(void *opaque
)
1693 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1695 qemu_flush_coalesced_mmio_buffer();
1696 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1699 struct vm_change_state_entry
{
1700 VMChangeStateHandler
*cb
;
1702 QLIST_ENTRY (vm_change_state_entry
) entries
;
1705 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1707 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1710 VMChangeStateEntry
*e
;
1712 e
= qemu_mallocz(sizeof (*e
));
1716 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1720 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1722 QLIST_REMOVE (e
, entries
);
1726 void vm_state_notify(int running
, int reason
)
1728 VMChangeStateEntry
*e
;
1730 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1731 e
->cb(e
->opaque
, running
, reason
);
1740 vm_state_notify(1, 0);
1745 /* reset/shutdown handler */
1747 typedef struct QEMUResetEntry
{
1748 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1749 QEMUResetHandler
*func
;
1753 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1754 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1755 static int reset_requested
;
1756 static int shutdown_requested
;
1757 static int powerdown_requested
;
1758 int debug_requested
;
1759 static int vmstop_requested
;
1761 int qemu_shutdown_requested(void)
1763 int r
= shutdown_requested
;
1764 shutdown_requested
= 0;
1768 int qemu_reset_requested(void)
1770 int r
= reset_requested
;
1771 reset_requested
= 0;
1775 int qemu_powerdown_requested(void)
1777 int r
= powerdown_requested
;
1778 powerdown_requested
= 0;
1782 static int qemu_debug_requested(void)
1784 int r
= debug_requested
;
1785 debug_requested
= 0;
1789 static int qemu_vmstop_requested(void)
1791 int r
= vmstop_requested
;
1792 vmstop_requested
= 0;
1796 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1798 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1801 re
->opaque
= opaque
;
1802 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1805 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1809 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1810 if (re
->func
== func
&& re
->opaque
== opaque
) {
1811 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1818 void qemu_system_reset(void)
1820 QEMUResetEntry
*re
, *nre
;
1822 /* reset all devices */
1823 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1824 re
->func(re
->opaque
);
1826 monitor_protocol_event(QEVENT_RESET
, NULL
);
1827 cpu_synchronize_all_post_reset();
1830 void qemu_system_reset_request(void)
1833 shutdown_requested
= 1;
1835 reset_requested
= 1;
1837 qemu_notify_event();
1840 void qemu_system_shutdown_request(void)
1842 shutdown_requested
= 1;
1843 qemu_notify_event();
1846 void qemu_system_powerdown_request(void)
1848 powerdown_requested
= 1;
1849 qemu_notify_event();
1853 static void host_main_loop_wait(int *timeout
)
1859 /* XXX: need to suppress polling by better using win32 events */
1861 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1862 ret
|= pe
->func(pe
->opaque
);
1866 WaitObjects
*w
= &wait_objects
;
1868 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1869 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1870 if (w
->func
[ret
- WAIT_OBJECT_0
])
1871 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1873 /* Check for additional signaled events */
1874 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1876 /* Check if event is signaled */
1877 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1878 if(ret2
== WAIT_OBJECT_0
) {
1880 w
->func
[i
](w
->opaque
[i
]);
1881 } else if (ret2
== WAIT_TIMEOUT
) {
1883 err
= GetLastError();
1884 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1887 } else if (ret
== WAIT_TIMEOUT
) {
1889 err
= GetLastError();
1890 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1897 static void host_main_loop_wait(int *timeout
)
1902 void main_loop_wait(int nonblocking
)
1904 IOHandlerRecord
*ioh
;
1905 fd_set rfds
, wfds
, xfds
;
1913 timeout
= qemu_calculate_timeout();
1914 qemu_bh_update_timeout(&timeout
);
1917 host_main_loop_wait(&timeout
);
1919 /* poll any events */
1920 /* XXX: separate device handlers from system ones */
1925 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1929 (!ioh
->fd_read_poll
||
1930 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1931 FD_SET(ioh
->fd
, &rfds
);
1935 if (ioh
->fd_write
) {
1936 FD_SET(ioh
->fd
, &wfds
);
1942 tv
.tv_sec
= timeout
/ 1000;
1943 tv
.tv_usec
= (timeout
% 1000) * 1000;
1945 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1947 qemu_mutex_unlock_iothread();
1948 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1949 qemu_mutex_lock_iothread();
1951 IOHandlerRecord
*pioh
;
1953 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1955 QLIST_REMOVE(ioh
, next
);
1959 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1960 ioh
->fd_read(ioh
->opaque
);
1962 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1963 ioh
->fd_write(ioh
->opaque
);
1968 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1970 qemu_run_all_timers();
1972 /* Check bottom-halves last in case any of the earlier events triggered
1978 static int vm_can_run(void)
1980 if (powerdown_requested
)
1982 if (reset_requested
)
1984 if (shutdown_requested
)
1986 if (debug_requested
)
1991 qemu_irq qemu_system_powerdown
;
1993 static void main_loop(void)
1997 #ifdef CONFIG_IOTHREAD
1998 qemu_system_ready
= 1;
1999 qemu_cond_broadcast(&qemu_system_cond
);
2004 bool nonblocking
= false;
2005 #ifdef CONFIG_PROFILER
2008 #ifndef CONFIG_IOTHREAD
2009 nonblocking
= tcg_cpu_exec();
2011 #ifdef CONFIG_PROFILER
2012 ti
= profile_getclock();
2014 main_loop_wait(nonblocking
);
2015 #ifdef CONFIG_PROFILER
2016 dev_time
+= profile_getclock() - ti
;
2018 } while (vm_can_run());
2020 if ((r
= qemu_debug_requested())) {
2023 if (qemu_shutdown_requested()) {
2024 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2031 if (qemu_reset_requested()) {
2033 qemu_system_reset();
2036 if (qemu_powerdown_requested()) {
2037 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2038 qemu_irq_raise(qemu_system_powerdown
);
2040 if ((r
= qemu_vmstop_requested())) {
2047 static void version(void)
2049 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2052 static void help(int exitcode
)
2054 const char *options_help
=
2055 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2057 #define DEFHEADING(text) stringify(text) "\n"
2058 #include "qemu-options.h"
2064 printf("usage: %s [options] [disk_image]\n"
2066 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2069 "During emulation, the following keys are useful:\n"
2070 "ctrl-alt-f toggle full screen\n"
2071 "ctrl-alt-n switch to virtual console 'n'\n"
2072 "ctrl-alt toggle mouse and keyboard grab\n"
2074 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2080 #define HAS_ARG 0x0001
2083 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2085 #define DEFHEADING(text)
2086 #include "qemu-options.h"
2092 typedef struct QEMUOption
{
2099 static const QEMUOption qemu_options
[] = {
2100 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2101 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2102 { option, opt_arg, opt_enum, arch_mask },
2103 #define DEFHEADING(text)
2104 #include "qemu-options.h"
2110 static void select_vgahw (const char *p
)
2115 vga_interface_type
= VGA_NONE
;
2116 if (strstart(p
, "std", &opts
)) {
2117 vga_interface_type
= VGA_STD
;
2118 } else if (strstart(p
, "cirrus", &opts
)) {
2119 vga_interface_type
= VGA_CIRRUS
;
2120 } else if (strstart(p
, "vmware", &opts
)) {
2121 vga_interface_type
= VGA_VMWARE
;
2122 } else if (strstart(p
, "xenfb", &opts
)) {
2123 vga_interface_type
= VGA_XENFB
;
2124 } else if (!strstart(p
, "none", &opts
)) {
2126 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2130 const char *nextopt
;
2132 if (strstart(opts
, ",retrace=", &nextopt
)) {
2134 if (strstart(opts
, "dumb", &nextopt
))
2135 vga_retrace_method
= VGA_RETRACE_DUMB
;
2136 else if (strstart(opts
, "precise", &nextopt
))
2137 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2138 else goto invalid_vga
;
2139 } else goto invalid_vga
;
2144 static int balloon_parse(const char *arg
)
2148 if (strcmp(arg
, "none") == 0) {
2152 if (!strncmp(arg
, "virtio", 6)) {
2153 if (arg
[6] == ',') {
2154 /* have params -> parse them */
2155 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2159 /* create empty opts */
2160 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2162 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2170 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2172 exit(STATUS_CONTROL_C_EXIT
);
2179 static void termsig_handler(int signal
)
2181 qemu_system_shutdown_request();
2184 static void sigchld_handler(int signal
)
2186 waitpid(-1, NULL
, WNOHANG
);
2189 static void sighandler_setup(void)
2191 struct sigaction act
;
2193 memset(&act
, 0, sizeof(act
));
2194 act
.sa_handler
= termsig_handler
;
2195 sigaction(SIGINT
, &act
, NULL
);
2196 sigaction(SIGHUP
, &act
, NULL
);
2197 sigaction(SIGTERM
, &act
, NULL
);
2199 act
.sa_handler
= sigchld_handler
;
2200 act
.sa_flags
= SA_NOCLDSTOP
;
2201 sigaction(SIGCHLD
, &act
, NULL
);
2207 /* Look for support files in the same directory as the executable. */
2208 static char *find_datadir(const char *argv0
)
2214 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2221 while (p
!= buf
&& *p
!= '\\')
2224 if (access(buf
, R_OK
) == 0) {
2225 return qemu_strdup(buf
);
2231 /* Find a likely location for support files using the location of the binary.
2232 For installed binaries this will be "$bindir/../share/qemu". When
2233 running from the build tree this will be "$bindir/../pc-bios". */
2234 #define SHARE_SUFFIX "/share/qemu"
2235 #define BUILD_SUFFIX "/pc-bios"
2236 static char *find_datadir(const char *argv0
)
2244 #if defined(__linux__)
2247 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2253 #elif defined(__FreeBSD__)
2255 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2256 size_t len
= sizeof(buf
) - 1;
2259 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2261 buf
[sizeof(buf
) - 1] = '\0';
2266 /* If we don't have any way of figuring out the actual executable
2267 location then try argv[0]. */
2269 p
= realpath(argv0
, buf
);
2277 max_len
= strlen(dir
) +
2278 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2279 res
= qemu_mallocz(max_len
);
2280 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2281 if (access(res
, R_OK
)) {
2282 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2283 if (access(res
, R_OK
)) {
2295 char *qemu_find_file(int type
, const char *name
)
2301 /* If name contains path separators then try it as a straight path. */
2302 if ((strchr(name
, '/') || strchr(name
, '\\'))
2303 && access(name
, R_OK
) == 0) {
2304 return qemu_strdup(name
);
2307 case QEMU_FILE_TYPE_BIOS
:
2310 case QEMU_FILE_TYPE_KEYMAP
:
2311 subdir
= "keymaps/";
2316 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2317 buf
= qemu_mallocz(len
);
2318 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2319 if (access(buf
, R_OK
)) {
2326 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2328 return qdev_device_help(opts
);
2331 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2335 dev
= qdev_device_add(opts
);
2341 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2343 CharDriverState
*chr
;
2345 chr
= qemu_chr_open_opts(opts
, NULL
);
2351 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2353 CharDriverState
*chr
;
2354 const char *chardev
;
2358 mode
= qemu_opt_get(opts
, "mode");
2362 if (strcmp(mode
, "readline") == 0) {
2363 flags
= MONITOR_USE_READLINE
;
2364 } else if (strcmp(mode
, "control") == 0) {
2365 flags
= MONITOR_USE_CONTROL
;
2367 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2371 if (qemu_opt_get_bool(opts
, "default", 0))
2372 flags
|= MONITOR_IS_DEFAULT
;
2374 chardev
= qemu_opt_get(opts
, "chardev");
2375 chr
= qemu_chr_find(chardev
);
2377 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2381 monitor_init(chr
, flags
);
2385 static void monitor_parse(const char *optarg
, const char *mode
)
2387 static int monitor_device_index
= 0;
2393 if (strstart(optarg
, "chardev:", &p
)) {
2394 snprintf(label
, sizeof(label
), "%s", p
);
2396 if (monitor_device_index
) {
2397 snprintf(label
, sizeof(label
), "monitor%d",
2398 monitor_device_index
);
2400 snprintf(label
, sizeof(label
), "monitor");
2403 opts
= qemu_chr_parse_compat(label
, optarg
);
2405 fprintf(stderr
, "parse error: %s\n", optarg
);
2410 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2412 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2415 qemu_opt_set(opts
, "mode", mode
);
2416 qemu_opt_set(opts
, "chardev", label
);
2418 qemu_opt_set(opts
, "default", "on");
2419 monitor_device_index
++;
2422 struct device_config
{
2424 DEV_USB
, /* -usbdevice */
2426 DEV_SERIAL
, /* -serial */
2427 DEV_PARALLEL
, /* -parallel */
2428 DEV_VIRTCON
, /* -virtioconsole */
2429 DEV_DEBUGCON
, /* -debugcon */
2431 const char *cmdline
;
2432 QTAILQ_ENTRY(device_config
) next
;
2434 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2436 static void add_device_config(int type
, const char *cmdline
)
2438 struct device_config
*conf
;
2440 conf
= qemu_mallocz(sizeof(*conf
));
2442 conf
->cmdline
= cmdline
;
2443 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2446 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2448 struct device_config
*conf
;
2451 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2452 if (conf
->type
!= type
)
2454 rc
= func(conf
->cmdline
);
2461 static int serial_parse(const char *devname
)
2463 static int index
= 0;
2466 if (strcmp(devname
, "none") == 0)
2468 if (index
== MAX_SERIAL_PORTS
) {
2469 fprintf(stderr
, "qemu: too many serial ports\n");
2472 snprintf(label
, sizeof(label
), "serial%d", index
);
2473 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2474 if (!serial_hds
[index
]) {
2475 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2476 devname
, strerror(errno
));
2483 static int parallel_parse(const char *devname
)
2485 static int index
= 0;
2488 if (strcmp(devname
, "none") == 0)
2490 if (index
== MAX_PARALLEL_PORTS
) {
2491 fprintf(stderr
, "qemu: too many parallel ports\n");
2494 snprintf(label
, sizeof(label
), "parallel%d", index
);
2495 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2496 if (!parallel_hds
[index
]) {
2497 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2498 devname
, strerror(errno
));
2505 static int virtcon_parse(const char *devname
)
2507 static int index
= 0;
2509 QemuOpts
*bus_opts
, *dev_opts
;
2511 if (strcmp(devname
, "none") == 0)
2513 if (index
== MAX_VIRTIO_CONSOLES
) {
2514 fprintf(stderr
, "qemu: too many virtio consoles\n");
2518 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2519 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2521 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2522 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2524 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2525 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2526 if (!virtcon_hds
[index
]) {
2527 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2528 devname
, strerror(errno
));
2531 qemu_opt_set(dev_opts
, "chardev", label
);
2537 static int debugcon_parse(const char *devname
)
2541 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2544 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2546 fprintf(stderr
, "qemu: already have a debugcon device\n");
2549 qemu_opt_set(opts
, "driver", "isa-debugcon");
2550 qemu_opt_set(opts
, "chardev", "debugcon");
2554 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2555 const char **poptarg
, int *poptind
)
2557 const QEMUOption
*popt
;
2558 int optind
= *poptind
;
2559 char *r
= argv
[optind
];
2562 loc_set_cmdline(argv
, optind
, 1);
2564 /* Treat --foo the same as -foo. */
2567 popt
= qemu_options
;
2570 error_report("invalid option");
2573 if (!strcmp(popt
->name
, r
+ 1))
2577 if (popt
->flags
& HAS_ARG
) {
2578 if (optind
>= argc
) {
2579 error_report("requires an argument");
2582 optarg
= argv
[optind
++];
2583 loc_set_cmdline(argv
, optind
- 2, 2);
2594 int main(int argc
, char **argv
, char **envp
)
2596 const char *gdbstub_dev
= NULL
;
2597 uint32_t boot_devices_bitmap
= 0;
2599 int snapshot
, linux_boot
, net_boot
;
2600 const char *icount_option
= NULL
;
2601 const char *initrd_filename
;
2602 const char *kernel_filename
, *kernel_cmdline
;
2603 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2605 DisplayChangeListener
*dcl
;
2606 int cyls
, heads
, secs
, translation
;
2607 QemuOpts
*hda_opts
= NULL
, *opts
;
2610 const char *loadvm
= NULL
;
2611 QEMUMachine
*machine
;
2612 const char *cpu_model
;
2617 const char *pid_file
= NULL
;
2618 const char *incoming
= NULL
;
2621 struct passwd
*pwd
= NULL
;
2622 const char *chroot_dir
= NULL
;
2623 const char *run_as
= NULL
;
2625 int show_vnc_port
= 0;
2628 error_set_progname(argv
[0]);
2632 qemu_cache_utils_init(envp
);
2634 QLIST_INIT (&vm_change_state_head
);
2637 struct sigaction act
;
2638 sigfillset(&act
.sa_mask
);
2640 act
.sa_handler
= SIG_IGN
;
2641 sigaction(SIGPIPE
, &act
, NULL
);
2644 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2645 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2646 QEMU to run on a single CPU */
2651 h
= GetCurrentProcess();
2652 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2653 for(i
= 0; i
< 32; i
++) {
2654 if (mask
& (1 << i
))
2659 SetProcessAffinityMask(h
, mask
);
2665 module_call_init(MODULE_INIT_MACHINE
);
2666 machine
= find_default_machine();
2668 initrd_filename
= NULL
;
2671 kernel_filename
= NULL
;
2672 kernel_cmdline
= "";
2673 cyls
= heads
= secs
= 0;
2674 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2676 for (i
= 0; i
< MAX_NODES
; i
++) {
2678 node_cpumask
[i
] = 0;
2687 /* first pass of option parsing */
2689 while (optind
< argc
) {
2690 if (argv
[optind
][0] != '-') {
2695 const QEMUOption
*popt
;
2697 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2698 switch (popt
->index
) {
2699 case QEMU_OPTION_nodefconfig
:
2710 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
2711 fp
= fopen(fname
, "r");
2713 if (qemu_config_parse(fp
, fname
) != 0) {
2719 fname
= arch_config_name
;
2720 fp
= fopen(fname
, "r");
2722 if (qemu_config_parse(fp
, fname
) != 0) {
2730 /* second pass of option parsing */
2735 if (argv
[optind
][0] != '-') {
2736 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2738 const QEMUOption
*popt
;
2740 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2741 if (!(popt
->arch_mask
& arch_type
)) {
2742 printf("Option %s not supported for this target\n", popt
->name
);
2745 switch(popt
->index
) {
2747 machine
= find_machine(optarg
);
2750 printf("Supported machines are:\n");
2751 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2753 printf("%-10s %s (alias of %s)\n",
2754 m
->alias
, m
->desc
, m
->name
);
2755 printf("%-10s %s%s\n",
2757 m
->is_default
? " (default)" : "");
2759 exit(*optarg
!= '?');
2762 case QEMU_OPTION_cpu
:
2763 /* hw initialization will check this */
2764 if (*optarg
== '?') {
2765 /* XXX: implement xxx_cpu_list for targets that still miss it */
2766 #if defined(cpu_list_id)
2767 cpu_list_id(stdout
, &fprintf
, optarg
);
2768 #elif defined(cpu_list)
2769 cpu_list(stdout
, &fprintf
); /* deprecated */
2776 case QEMU_OPTION_initrd
:
2777 initrd_filename
= optarg
;
2779 case QEMU_OPTION_hda
:
2781 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2783 hda_opts
= drive_add(optarg
, HD_ALIAS
2784 ",cyls=%d,heads=%d,secs=%d%s",
2785 0, cyls
, heads
, secs
,
2786 translation
== BIOS_ATA_TRANSLATION_LBA
?
2788 translation
== BIOS_ATA_TRANSLATION_NONE
?
2789 ",trans=none" : "");
2791 case QEMU_OPTION_hdb
:
2792 case QEMU_OPTION_hdc
:
2793 case QEMU_OPTION_hdd
:
2794 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2796 case QEMU_OPTION_drive
:
2797 drive_add(NULL
, "%s", optarg
);
2799 case QEMU_OPTION_set
:
2800 if (qemu_set_option(optarg
) != 0)
2803 case QEMU_OPTION_global
:
2804 if (qemu_global_option(optarg
) != 0)
2807 case QEMU_OPTION_mtdblock
:
2808 drive_add(optarg
, MTD_ALIAS
);
2810 case QEMU_OPTION_sd
:
2811 drive_add(optarg
, SD_ALIAS
);
2813 case QEMU_OPTION_pflash
:
2814 drive_add(optarg
, PFLASH_ALIAS
);
2816 case QEMU_OPTION_snapshot
:
2819 case QEMU_OPTION_hdachs
:
2823 cyls
= strtol(p
, (char **)&p
, 0);
2824 if (cyls
< 1 || cyls
> 16383)
2829 heads
= strtol(p
, (char **)&p
, 0);
2830 if (heads
< 1 || heads
> 16)
2835 secs
= strtol(p
, (char **)&p
, 0);
2836 if (secs
< 1 || secs
> 63)
2840 if (!strcmp(p
, "none"))
2841 translation
= BIOS_ATA_TRANSLATION_NONE
;
2842 else if (!strcmp(p
, "lba"))
2843 translation
= BIOS_ATA_TRANSLATION_LBA
;
2844 else if (!strcmp(p
, "auto"))
2845 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2848 } else if (*p
!= '\0') {
2850 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2853 if (hda_opts
!= NULL
) {
2855 snprintf(num
, sizeof(num
), "%d", cyls
);
2856 qemu_opt_set(hda_opts
, "cyls", num
);
2857 snprintf(num
, sizeof(num
), "%d", heads
);
2858 qemu_opt_set(hda_opts
, "heads", num
);
2859 snprintf(num
, sizeof(num
), "%d", secs
);
2860 qemu_opt_set(hda_opts
, "secs", num
);
2861 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2862 qemu_opt_set(hda_opts
, "trans", "lba");
2863 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2864 qemu_opt_set(hda_opts
, "trans", "none");
2868 case QEMU_OPTION_numa
:
2869 if (nb_numa_nodes
>= MAX_NODES
) {
2870 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2875 case QEMU_OPTION_nographic
:
2876 display_type
= DT_NOGRAPHIC
;
2878 #ifdef CONFIG_CURSES
2879 case QEMU_OPTION_curses
:
2880 display_type
= DT_CURSES
;
2883 case QEMU_OPTION_portrait
:
2886 case QEMU_OPTION_kernel
:
2887 kernel_filename
= optarg
;
2889 case QEMU_OPTION_append
:
2890 kernel_cmdline
= optarg
;
2892 case QEMU_OPTION_cdrom
:
2893 drive_add(optarg
, CDROM_ALIAS
);
2895 case QEMU_OPTION_boot
:
2897 static const char * const params
[] = {
2898 "order", "once", "menu", NULL
2900 char buf
[sizeof(boot_devices
)];
2901 char *standard_boot_devices
;
2904 if (!strchr(optarg
, '=')) {
2906 pstrcpy(buf
, sizeof(buf
), optarg
);
2907 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2909 "qemu: unknown boot parameter '%s' in '%s'\n",
2915 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2916 boot_devices_bitmap
= parse_bootdevices(buf
);
2917 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2920 if (get_param_value(buf
, sizeof(buf
),
2922 boot_devices_bitmap
|= parse_bootdevices(buf
);
2923 standard_boot_devices
= qemu_strdup(boot_devices
);
2924 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2925 qemu_register_reset(restore_boot_devices
,
2926 standard_boot_devices
);
2928 if (get_param_value(buf
, sizeof(buf
),
2930 if (!strcmp(buf
, "on")) {
2932 } else if (!strcmp(buf
, "off")) {
2936 "qemu: invalid option value '%s'\n",
2944 case QEMU_OPTION_fda
:
2945 case QEMU_OPTION_fdb
:
2946 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2948 case QEMU_OPTION_no_fd_bootchk
:
2951 case QEMU_OPTION_netdev
:
2952 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2956 case QEMU_OPTION_net
:
2957 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2962 case QEMU_OPTION_tftp
:
2963 legacy_tftp_prefix
= optarg
;
2965 case QEMU_OPTION_bootp
:
2966 legacy_bootp_filename
= optarg
;
2969 case QEMU_OPTION_smb
:
2970 if (net_slirp_smb(optarg
) < 0)
2974 case QEMU_OPTION_redir
:
2975 if (net_slirp_redir(optarg
) < 0)
2979 case QEMU_OPTION_bt
:
2980 add_device_config(DEV_BT
, optarg
);
2982 case QEMU_OPTION_audio_help
:
2983 if (!(audio_available())) {
2984 printf("Option %s not supported for this target\n", popt
->name
);
2990 case QEMU_OPTION_soundhw
:
2991 if (!(audio_available())) {
2992 printf("Option %s not supported for this target\n", popt
->name
);
2995 select_soundhw (optarg
);
3000 case QEMU_OPTION_version
:
3004 case QEMU_OPTION_m
: {
3008 value
= strtoul(optarg
, &ptr
, 10);
3010 case 0: case 'M': case 'm':
3017 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3021 /* On 32-bit hosts, QEMU is limited by virtual address space */
3022 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3023 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3026 if (value
!= (uint64_t)(ram_addr_t
)value
) {
3027 fprintf(stderr
, "qemu: ram size too large\n");
3033 case QEMU_OPTION_mempath
:
3037 case QEMU_OPTION_mem_prealloc
:
3042 set_cpu_log(optarg
);
3045 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3047 case QEMU_OPTION_gdb
:
3048 gdbstub_dev
= optarg
;
3053 case QEMU_OPTION_bios
:
3056 case QEMU_OPTION_singlestep
:
3063 keyboard_layout
= optarg
;
3065 case QEMU_OPTION_localtime
:
3068 case QEMU_OPTION_vga
:
3069 select_vgahw (optarg
);
3076 w
= strtol(p
, (char **)&p
, 10);
3079 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3085 h
= strtol(p
, (char **)&p
, 10);
3090 depth
= strtol(p
, (char **)&p
, 10);
3091 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3092 depth
!= 24 && depth
!= 32)
3094 } else if (*p
== '\0') {
3095 depth
= graphic_depth
;
3102 graphic_depth
= depth
;
3105 case QEMU_OPTION_echr
:
3108 term_escape_char
= strtol(optarg
, &r
, 0);
3110 printf("Bad argument to echr\n");
3113 case QEMU_OPTION_monitor
:
3114 monitor_parse(optarg
, "readline");
3115 default_monitor
= 0;
3117 case QEMU_OPTION_qmp
:
3118 monitor_parse(optarg
, "control");
3119 default_monitor
= 0;
3121 case QEMU_OPTION_mon
:
3122 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3124 fprintf(stderr
, "parse error: %s\n", optarg
);
3127 default_monitor
= 0;
3129 case QEMU_OPTION_chardev
:
3130 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3132 fprintf(stderr
, "parse error: %s\n", optarg
);
3136 case QEMU_OPTION_serial
:
3137 add_device_config(DEV_SERIAL
, optarg
);
3139 if (strncmp(optarg
, "mon:", 4) == 0) {
3140 default_monitor
= 0;
3143 case QEMU_OPTION_watchdog
:
3146 "qemu: only one watchdog option may be given\n");
3151 case QEMU_OPTION_watchdog_action
:
3152 if (select_watchdog_action(optarg
) == -1) {
3153 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3157 case QEMU_OPTION_virtiocon
:
3158 add_device_config(DEV_VIRTCON
, optarg
);
3159 default_virtcon
= 0;
3160 if (strncmp(optarg
, "mon:", 4) == 0) {
3161 default_monitor
= 0;
3164 case QEMU_OPTION_parallel
:
3165 add_device_config(DEV_PARALLEL
, optarg
);
3166 default_parallel
= 0;
3167 if (strncmp(optarg
, "mon:", 4) == 0) {
3168 default_monitor
= 0;
3171 case QEMU_OPTION_debugcon
:
3172 add_device_config(DEV_DEBUGCON
, optarg
);
3174 case QEMU_OPTION_loadvm
:
3177 case QEMU_OPTION_full_screen
:
3181 case QEMU_OPTION_no_frame
:
3184 case QEMU_OPTION_alt_grab
:
3187 case QEMU_OPTION_ctrl_grab
:
3190 case QEMU_OPTION_no_quit
:
3193 case QEMU_OPTION_sdl
:
3194 display_type
= DT_SDL
;
3197 case QEMU_OPTION_pidfile
:
3200 case QEMU_OPTION_win2k_hack
:
3201 win2k_install_hack
= 1;
3203 case QEMU_OPTION_rtc_td_hack
:
3206 case QEMU_OPTION_acpitable
:
3207 do_acpitable_option(optarg
);
3209 case QEMU_OPTION_smbios
:
3210 do_smbios_option(optarg
);
3212 case QEMU_OPTION_enable_kvm
:
3213 if (!(kvm_available())) {
3214 printf("Option %s not supported for this target\n", popt
->name
);
3219 case QEMU_OPTION_usb
:
3222 case QEMU_OPTION_usbdevice
:
3224 add_device_config(DEV_USB
, optarg
);
3226 case QEMU_OPTION_device
:
3227 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3231 case QEMU_OPTION_smp
:
3234 fprintf(stderr
, "Invalid number of CPUs\n");
3237 if (max_cpus
< smp_cpus
) {
3238 fprintf(stderr
, "maxcpus must be equal to or greater than "
3242 if (max_cpus
> 255) {
3243 fprintf(stderr
, "Unsupported number of maxcpus\n");
3247 case QEMU_OPTION_vnc
:
3248 display_type
= DT_VNC
;
3249 vnc_display
= optarg
;
3251 case QEMU_OPTION_no_acpi
:
3254 case QEMU_OPTION_no_hpet
:
3257 case QEMU_OPTION_balloon
:
3258 if (balloon_parse(optarg
) < 0) {
3259 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3263 case QEMU_OPTION_no_reboot
:
3266 case QEMU_OPTION_no_shutdown
:
3269 case QEMU_OPTION_show_cursor
:
3272 case QEMU_OPTION_uuid
:
3273 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3274 fprintf(stderr
, "Fail to parse UUID string."
3275 " Wrong format.\n");
3280 case QEMU_OPTION_daemonize
:
3284 case QEMU_OPTION_option_rom
:
3285 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3286 fprintf(stderr
, "Too many option ROMs\n");
3289 option_rom
[nb_option_roms
] = optarg
;
3292 case QEMU_OPTION_semihosting
:
3293 semihosting_enabled
= 1;
3295 case QEMU_OPTION_name
:
3296 qemu_name
= qemu_strdup(optarg
);
3298 char *p
= strchr(qemu_name
, ',');
3301 if (strncmp(p
, "process=", 8)) {
3302 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3310 case QEMU_OPTION_prom_env
:
3311 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3312 fprintf(stderr
, "Too many prom variables\n");
3315 prom_envs
[nb_prom_envs
] = optarg
;
3318 case QEMU_OPTION_old_param
:
3321 case QEMU_OPTION_clock
:
3322 configure_alarms(optarg
);
3324 case QEMU_OPTION_startdate
:
3325 configure_rtc_date_offset(optarg
, 1);
3327 case QEMU_OPTION_rtc
:
3328 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3330 fprintf(stderr
, "parse error: %s\n", optarg
);
3333 configure_rtc(opts
);
3335 case QEMU_OPTION_tb_size
:
3336 tb_size
= strtol(optarg
, NULL
, 0);
3340 case QEMU_OPTION_icount
:
3341 icount_option
= optarg
;
3343 case QEMU_OPTION_incoming
:
3346 case QEMU_OPTION_nodefaults
:
3348 default_parallel
= 0;
3349 default_virtcon
= 0;
3350 default_monitor
= 0;
3358 case QEMU_OPTION_chroot
:
3359 chroot_dir
= optarg
;
3361 case QEMU_OPTION_runas
:
3365 case QEMU_OPTION_xen_domid
:
3366 if (!(xen_available())) {
3367 printf("Option %s not supported for this target\n", popt
->name
);
3370 xen_domid
= atoi(optarg
);
3372 case QEMU_OPTION_xen_create
:
3373 if (!(xen_available())) {
3374 printf("Option %s not supported for this target\n", popt
->name
);
3377 xen_mode
= XEN_CREATE
;
3379 case QEMU_OPTION_xen_attach
:
3380 if (!(xen_available())) {
3381 printf("Option %s not supported for this target\n", popt
->name
);
3384 xen_mode
= XEN_ATTACH
;
3386 case QEMU_OPTION_readconfig
:
3389 fp
= fopen(optarg
, "r");
3391 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3394 if (qemu_config_parse(fp
, optarg
) != 0) {
3400 case QEMU_OPTION_writeconfig
:
3403 if (strcmp(optarg
, "-") == 0) {
3406 fp
= fopen(optarg
, "w");
3408 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3412 qemu_config_write(fp
);
3421 /* If no data_dir is specified then try to find it relative to the
3424 data_dir
= find_datadir(argv
[0]);
3426 /* If all else fails use the install patch specified when building. */
3428 data_dir
= CONFIG_QEMU_SHAREDIR
;
3432 * Default to max_cpus = smp_cpus, in case the user doesn't
3433 * specify a max_cpus value.
3436 max_cpus
= smp_cpus
;
3438 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3439 if (smp_cpus
> machine
->max_cpus
) {
3440 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3441 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3446 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3447 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3449 if (machine
->no_serial
) {
3452 if (machine
->no_parallel
) {
3453 default_parallel
= 0;
3455 if (!machine
->use_virtcon
) {
3456 default_virtcon
= 0;
3458 if (machine
->no_vga
) {
3461 if (machine
->no_floppy
) {
3464 if (machine
->no_cdrom
) {
3467 if (machine
->no_sdcard
) {
3471 if (display_type
== DT_NOGRAPHIC
) {
3472 if (default_parallel
)
3473 add_device_config(DEV_PARALLEL
, "null");
3474 if (default_serial
&& default_monitor
) {
3475 add_device_config(DEV_SERIAL
, "mon:stdio");
3476 } else if (default_virtcon
&& default_monitor
) {
3477 add_device_config(DEV_VIRTCON
, "mon:stdio");
3480 add_device_config(DEV_SERIAL
, "stdio");
3481 if (default_virtcon
)
3482 add_device_config(DEV_VIRTCON
, "stdio");
3483 if (default_monitor
)
3484 monitor_parse("stdio", "readline");
3488 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3489 if (default_parallel
)
3490 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3491 if (default_monitor
)
3492 monitor_parse("vc:80Cx24C", "readline");
3493 if (default_virtcon
)
3494 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3497 vga_interface_type
= VGA_CIRRUS
;
3499 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3506 if (pipe(fds
) == -1)
3517 len
= read(fds
[0], &status
, 1);
3518 if (len
== -1 && (errno
== EINTR
))
3523 else if (status
== 1) {
3524 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3532 qemu_set_cloexec(fds
[1]);
3544 signal(SIGTSTP
, SIG_IGN
);
3545 signal(SIGTTOU
, SIG_IGN
);
3546 signal(SIGTTIN
, SIG_IGN
);
3550 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3554 if (write(fds
[1], &status
, 1) != 1) {
3555 perror("daemonize. Writing to pipe\n");
3559 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3563 if (kvm_enabled()) {
3566 ret
= kvm_init(smp_cpus
);
3568 fprintf(stderr
, "failed to initialize KVM\n");
3573 if (qemu_init_main_loop()) {
3574 fprintf(stderr
, "qemu_init_main_loop failed\n");
3577 linux_boot
= (kernel_filename
!= NULL
);
3579 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3580 fprintf(stderr
, "-append only allowed with -kernel option\n");
3584 if (!linux_boot
&& initrd_filename
!= NULL
) {
3585 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3590 /* Win32 doesn't support line-buffering and requires size >= 2 */
3591 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3594 if (init_timer_alarm() < 0) {
3595 fprintf(stderr
, "could not initialize alarm timer\n");
3598 configure_icount(icount_option
);
3602 if (net_init_clients() < 0) {
3606 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
3607 net_set_boot_mask(net_boot
);
3609 /* init the bluetooth world */
3610 if (foreach_device_config(DEV_BT
, bt_parse
))
3613 /* init the memory */
3615 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3617 /* init the dynamic translator */
3618 cpu_exec_init_all(tb_size
* 1024 * 1024);
3620 bdrv_init_with_whitelist();
3624 if (default_cdrom
) {
3625 /* we always create the cdrom drive, even if no disk is there */
3626 drive_add(NULL
, CDROM_ALIAS
);
3629 if (default_floppy
) {
3630 /* we always create at least one floppy */
3631 drive_add(NULL
, FD_ALIAS
, 0);
3634 if (default_sdcard
) {
3635 /* we always create one sd slot, even if no card is in it */
3636 drive_add(NULL
, SD_ALIAS
);
3639 /* open the virtual block devices */
3641 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3642 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3645 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3648 if (nb_numa_nodes
> 0) {
3651 if (nb_numa_nodes
> smp_cpus
) {
3652 nb_numa_nodes
= smp_cpus
;
3655 /* If no memory size if given for any node, assume the default case
3656 * and distribute the available memory equally across all nodes
3658 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3659 if (node_mem
[i
] != 0)
3662 if (i
== nb_numa_nodes
) {
3663 uint64_t usedmem
= 0;
3665 /* On Linux, the each node's border has to be 8MB aligned,
3666 * the final node gets the rest.
3668 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3669 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3670 usedmem
+= node_mem
[i
];
3672 node_mem
[i
] = ram_size
- usedmem
;
3675 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3676 if (node_cpumask
[i
] != 0)
3679 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3680 * must cope with this anyway, because there are BIOSes out there in
3681 * real machines which also use this scheme.
3683 if (i
== nb_numa_nodes
) {
3684 for (i
= 0; i
< smp_cpus
; i
++) {
3685 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3690 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3692 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3694 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3696 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3699 module_call_init(MODULE_INIT_DEVICE
);
3701 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3705 i
= select_watchdog(watchdog
);
3707 exit (i
== 1 ? 1 : 0);
3710 if (machine
->compat_props
) {
3711 qdev_prop_register_global_list(machine
->compat_props
);
3715 machine
->init(ram_size
, boot_devices
,
3716 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3718 cpu_synchronize_all_post_init();
3721 /* must be after terminal init, SDL library changes signal handlers */
3727 current_machine
= machine
;
3729 /* init USB devices */
3731 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3735 /* init generic devices */
3736 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3739 net_check_clients();
3741 /* just use the first displaystate for the moment */
3742 ds
= get_displaystate();
3744 if (display_type
== DT_DEFAULT
) {
3745 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3746 display_type
= DT_SDL
;
3748 display_type
= DT_VNC
;
3749 vnc_display
= "localhost:0,to=99";
3755 switch (display_type
) {
3758 #if defined(CONFIG_CURSES)
3760 curses_display_init(ds
, full_screen
);
3763 #if defined(CONFIG_SDL)
3765 sdl_display_init(ds
, full_screen
, no_frame
);
3767 #elif defined(CONFIG_COCOA)
3769 cocoa_display_init(ds
, full_screen
);
3773 vnc_display_init(ds
);
3774 if (vnc_display_open(ds
, vnc_display
) < 0)
3777 if (show_vnc_port
) {
3778 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3786 dcl
= ds
->listeners
;
3787 while (dcl
!= NULL
) {
3788 if (dcl
->dpy_refresh
!= NULL
) {
3789 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3790 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3795 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3796 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3797 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3800 text_consoles_set_display(ds
);
3802 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
3805 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3806 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3811 qdev_machine_creation_done();
3813 if (rom_load_all() != 0) {
3814 fprintf(stderr
, "rom loading failed\n");
3818 qemu_system_reset();
3820 if (load_vmstate(loadvm
) < 0) {
3826 qemu_start_incoming_migration(incoming
);
3827 } else if (autostart
) {
3837 len
= write(fds
[1], &status
, 1);
3838 if (len
== -1 && (errno
== EINTR
))
3845 perror("not able to chdir to /");
3848 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3854 pwd
= getpwnam(run_as
);
3856 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3862 if (chroot(chroot_dir
) < 0) {
3863 fprintf(stderr
, "chroot failed\n");
3867 perror("not able to chdir to /");
3873 if (setgid(pwd
->pw_gid
) < 0) {
3874 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3877 if (setuid(pwd
->pw_uid
) < 0) {
3878 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3881 if (setuid(0) != -1) {
3882 fprintf(stderr
, "Dropping privileges failed\n");