4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <sys/prctl.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
82 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84 extern int madvise(caddr_t
, size_t, int);
89 #if defined(__OpenBSD__)
93 #if defined(CONFIG_VDE)
94 #include <libvdeplug.h>
102 #if defined(__APPLE__) || defined(main)
104 int qemu_main(int argc
, char **argv
, char **envp
);
105 int main(int argc
, char **argv
)
107 return qemu_main(argc
, argv
, NULL
);
110 #define main qemu_main
112 #endif /* CONFIG_SDL */
116 #define main qemu_main
117 #endif /* CONFIG_COCOA */
120 #include "hw/boards.h"
122 #include "hw/pcmcia.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "block_int.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
149 #include "qemu-option.h"
150 #include "qemu-config.h"
151 #include "qemu-objects.h"
152 #include "hw/device-assignment.h"
154 #include "fsdev/qemu-fsdev.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir
;
175 const char *bios_name
= NULL
;
176 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
177 to store the VM snapshots */
178 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
179 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
180 DriveInfo
*extboot_drive
= NULL
;
181 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
182 DisplayType display_type
= DT_DEFAULT
;
183 const char* keyboard_layout
= NULL
;
185 const char *mem_path
= NULL
;
187 int mem_prealloc
= 0; /* force preallocation of physical target memory */
190 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_NONE
;
197 static int full_screen
= 0;
199 static int no_frame
= 0;
202 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
203 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
204 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
205 int win2k_install_hack
= 0;
209 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
210 int assigned_devices_index
;
215 const char *vnc_display
;
216 int acpi_enabled
= 1;
224 int graphic_rotate
= 0;
225 uint8_t irq0override
= 1;
229 const char *watchdog
;
230 const char *option_rom
[MAX_OPTION_ROMS
];
232 int semihosting_enabled
= 0;
233 int time_drift_fix
= 0;
234 unsigned int kvm_shadow_memory
= 0;
236 const char *qemu_name
;
239 unsigned int nb_prom_envs
= 0;
240 const char *prom_envs
[MAX_PROM_ENVS
];
241 const char *nvram
= NULL
;
245 uint64_t node_mem
[MAX_NODES
];
246 uint64_t node_cpumask
[MAX_NODES
];
248 static QEMUTimer
*nographic_timer
;
250 uint8_t qemu_uuid
[16];
252 static QEMUBootSetHandler
*boot_set_handler
;
253 static void *boot_set_opaque
;
257 enum xen_mode xen_mode
= XEN_EMULATE
;
259 static int default_serial
= 1;
260 static int default_parallel
= 1;
261 static int default_virtcon
= 1;
262 static int default_monitor
= 1;
263 static int default_vga
= 1;
264 static int default_floppy
= 1;
265 static int default_cdrom
= 1;
266 static int default_sdcard
= 1;
272 { .driver
= "isa-serial", .flag
= &default_serial
},
273 { .driver
= "isa-parallel", .flag
= &default_parallel
},
274 { .driver
= "isa-fdc", .flag
= &default_floppy
},
275 { .driver
= "ide-drive", .flag
= &default_cdrom
},
276 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
277 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
278 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
279 { .driver
= "VGA", .flag
= &default_vga
},
280 { .driver
= "cirrus-vga", .flag
= &default_vga
},
281 { .driver
= "vmware-svga", .flag
= &default_vga
},
284 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
286 const char *driver
= qemu_opt_get(opts
, "driver");
291 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
292 if (strcmp(default_list
[i
].driver
, driver
) != 0)
294 *(default_list
[i
].flag
) = 0;
299 /***********************************************************/
301 static void set_proc_name(const char *s
)
303 #if defined(__linux__) && defined(PR_SET_NAME)
307 name
[sizeof(name
) - 1] = 0;
308 strncpy(name
, s
, sizeof(name
));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME
, name
);
315 /***********************************************************/
316 /* real time host monotonic timer */
318 /* compute with 96 bit intermediate result: (a*b)/c */
319 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
324 #ifdef HOST_WORDS_BIGENDIAN
334 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
335 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
338 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
342 /***********************************************************/
343 /* host time/date access */
344 void qemu_get_timedate(struct tm
*tm
, int offset
)
351 if (rtc_date_offset
== -1) {
355 ret
= localtime(&ti
);
357 ti
-= rtc_date_offset
;
361 memcpy(tm
, ret
, sizeof(struct tm
));
364 int qemu_timedate_diff(struct tm
*tm
)
368 if (rtc_date_offset
== -1)
370 seconds
= mktimegm(tm
);
372 seconds
= mktime(tm
);
374 seconds
= mktimegm(tm
) + rtc_date_offset
;
376 return seconds
- time(NULL
);
379 void rtc_change_mon_event(struct tm
*tm
)
383 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
384 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
385 qobject_decref(data
);
388 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
390 time_t rtc_start_date
;
393 if (!strcmp(startdate
, "now") && legacy
) {
394 rtc_date_offset
= -1;
396 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
404 } else if (sscanf(startdate
, "%d-%d-%d",
416 rtc_start_date
= mktimegm(&tm
);
417 if (rtc_start_date
== -1) {
419 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
420 "'2006-06-17T16:01:21' or '2006-06-17'\n");
423 rtc_date_offset
= time(NULL
) - rtc_start_date
;
427 static void configure_rtc(QemuOpts
*opts
)
431 value
= qemu_opt_get(opts
, "base");
433 if (!strcmp(value
, "utc")) {
435 } else if (!strcmp(value
, "localtime")) {
438 configure_rtc_date_offset(value
, 0);
441 value
= qemu_opt_get(opts
, "clock");
443 if (!strcmp(value
, "host")) {
444 rtc_clock
= host_clock
;
445 } else if (!strcmp(value
, "vm")) {
446 rtc_clock
= vm_clock
;
448 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
452 value
= qemu_opt_get(opts
, "driftfix");
454 if (!strcmp(value
, "slew")) {
456 } else if (!strcmp(value
, "none")) {
459 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
465 /***********************************************************/
466 /* Bluetooth support */
469 static struct HCIInfo
*hci_table
[MAX_NICS
];
471 static struct bt_vlan_s
{
472 struct bt_scatternet_s net
;
474 struct bt_vlan_s
*next
;
477 /* find or alloc a new bluetooth "VLAN" */
478 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
480 struct bt_vlan_s
**pvlan
, *vlan
;
481 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
485 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
487 pvlan
= &first_bt_vlan
;
488 while (*pvlan
!= NULL
)
489 pvlan
= &(*pvlan
)->next
;
494 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
498 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
503 static struct HCIInfo null_hci
= {
504 .cmd_send
= null_hci_send
,
505 .sco_send
= null_hci_send
,
506 .acl_send
= null_hci_send
,
507 .bdaddr_set
= null_hci_addr_set
,
510 struct HCIInfo
*qemu_next_hci(void)
512 if (cur_hci
== nb_hcis
)
515 return hci_table
[cur_hci
++];
518 static struct HCIInfo
*hci_init(const char *str
)
521 struct bt_scatternet_s
*vlan
= 0;
523 if (!strcmp(str
, "null"))
526 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
528 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
529 else if (!strncmp(str
, "hci", 3)) {
532 if (!strncmp(str
+ 3, ",vlan=", 6)) {
533 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
538 vlan
= qemu_find_bt_vlan(0);
540 return bt_new_hci(vlan
);
543 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
548 static int bt_hci_parse(const char *str
)
553 if (nb_hcis
>= MAX_NICS
) {
554 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
567 bdaddr
.b
[5] = 0x56 + nb_hcis
;
568 hci
->bdaddr_set(hci
, bdaddr
.b
);
570 hci_table
[nb_hcis
++] = hci
;
575 static void bt_vhci_add(int vlan_id
)
577 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
580 fprintf(stderr
, "qemu: warning: adding a VHCI to "
581 "an empty scatternet %i\n", vlan_id
);
583 bt_vhci_init(bt_new_hci(vlan
));
586 static struct bt_device_s
*bt_device_add(const char *opt
)
588 struct bt_scatternet_s
*vlan
;
590 char *endp
= strstr(opt
, ",vlan=");
591 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
594 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
597 vlan_id
= strtol(endp
+ 6, &endp
, 0);
599 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
604 vlan
= qemu_find_bt_vlan(vlan_id
);
607 fprintf(stderr
, "qemu: warning: adding a slave device to "
608 "an empty scatternet %i\n", vlan_id
);
610 if (!strcmp(devname
, "keyboard"))
611 return bt_keyboard_init(vlan
);
613 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
617 static int bt_parse(const char *opt
)
619 const char *endp
, *p
;
622 if (strstart(opt
, "hci", &endp
)) {
623 if (!*endp
|| *endp
== ',') {
625 if (!strstart(endp
, ",vlan=", 0))
628 return bt_hci_parse(opt
);
630 } else if (strstart(opt
, "vhci", &endp
)) {
631 if (!*endp
|| *endp
== ',') {
633 if (strstart(endp
, ",vlan=", &p
)) {
634 vlan
= strtol(p
, (char **) &endp
, 0);
636 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
640 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
649 } else if (strstart(opt
, "device:", &endp
))
650 return !bt_device_add(endp
);
652 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
656 /***********************************************************/
657 /* QEMU Block devices */
659 #define HD_ALIAS "index=%d,media=disk"
660 #define CDROM_ALIAS "index=2,media=cdrom"
661 #define FD_ALIAS "index=%d,if=floppy"
662 #define PFLASH_ALIAS "if=pflash"
663 #define MTD_ALIAS "if=mtd"
664 #define SD_ALIAS "index=0,if=sd"
666 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
673 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
676 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
681 qemu_opt_set(opts
, "file", file
);
685 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
689 /* seek interface, bus and unit */
691 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
692 if (dinfo
->type
== type
&&
701 DriveInfo
*drive_get_by_id(const char *id
)
705 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
706 if (strcmp(id
, dinfo
->id
))
713 int drive_get_max_bus(BlockInterfaceType type
)
719 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
720 if(dinfo
->type
== type
&&
721 dinfo
->bus
> max_bus
)
722 max_bus
= dinfo
->bus
;
727 const char *drive_get_serial(BlockDriverState
*bdrv
)
731 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
732 if (dinfo
->bdrv
== bdrv
)
733 return dinfo
->serial
;
739 BlockInterfaceErrorAction
drive_get_on_error(
740 BlockDriverState
*bdrv
, int is_read
)
744 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
745 if (dinfo
->bdrv
== bdrv
)
746 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
749 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
752 static void bdrv_format_print(void *opaque
, const char *name
)
754 fprintf(stderr
, " %s", name
);
757 void drive_uninit(DriveInfo
*dinfo
)
759 qemu_opts_del(dinfo
->opts
);
760 bdrv_delete(dinfo
->bdrv
);
761 QTAILQ_REMOVE(&drives
, dinfo
, next
);
765 static int parse_block_error_action(const char *buf
, int is_read
)
767 if (!strcmp(buf
, "ignore")) {
768 return BLOCK_ERR_IGNORE
;
769 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
770 return BLOCK_ERR_STOP_ENOSPC
;
771 } else if (!strcmp(buf
, "stop")) {
772 return BLOCK_ERR_STOP_ANY
;
773 } else if (!strcmp(buf
, "report")) {
774 return BLOCK_ERR_REPORT
;
776 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
777 buf
, is_read
? "read" : "write");
782 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
786 const char *file
= NULL
;
789 const char *mediastr
= "";
790 BlockInterfaceType type
;
791 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
793 int cyls
, heads
, secs
, translation
;
794 BlockDriver
*drv
= NULL
;
795 QEMUMachine
*machine
= opaque
;
800 int on_read_error
, on_write_error
;
808 translation
= BIOS_ATA_TRANSLATION_AUTO
;
810 if (machine
&& machine
->use_scsi
) {
812 max_devs
= MAX_SCSI_DEVS
;
813 pstrcpy(devname
, sizeof(devname
), "scsi");
816 max_devs
= MAX_IDE_DEVS
;
817 pstrcpy(devname
, sizeof(devname
), "ide");
821 /* extract parameters */
822 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
823 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
824 index
= qemu_opt_get_number(opts
, "index", -1);
826 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
827 heads
= qemu_opt_get_number(opts
, "heads", 0);
828 secs
= qemu_opt_get_number(opts
, "secs", 0);
830 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
831 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
833 file
= qemu_opt_get(opts
, "file");
834 serial
= qemu_opt_get(opts
, "serial");
836 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
837 pstrcpy(devname
, sizeof(devname
), buf
);
838 if (!strcmp(buf
, "ide")) {
840 max_devs
= MAX_IDE_DEVS
;
841 } else if (!strcmp(buf
, "scsi")) {
843 max_devs
= MAX_SCSI_DEVS
;
844 } else if (!strcmp(buf
, "floppy")) {
847 } else if (!strcmp(buf
, "pflash")) {
850 } else if (!strcmp(buf
, "mtd")) {
853 } else if (!strcmp(buf
, "sd")) {
856 } else if (!strcmp(buf
, "virtio")) {
859 } else if (!strcmp(buf
, "xen")) {
862 } else if (!strcmp(buf
, "none")) {
866 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
871 if (cyls
|| heads
|| secs
) {
872 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
873 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
876 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
877 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
880 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
881 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
886 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
889 "qemu: '%s' trans must be used with cyls,heads and secs\n",
893 if (!strcmp(buf
, "none"))
894 translation
= BIOS_ATA_TRANSLATION_NONE
;
895 else if (!strcmp(buf
, "lba"))
896 translation
= BIOS_ATA_TRANSLATION_LBA
;
897 else if (!strcmp(buf
, "auto"))
898 translation
= BIOS_ATA_TRANSLATION_AUTO
;
900 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
905 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
906 if (!strcmp(buf
, "disk")) {
908 } else if (!strcmp(buf
, "cdrom")) {
909 if (cyls
|| secs
|| heads
) {
911 "qemu: '%s' invalid physical CHS format\n", buf
);
916 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
921 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
922 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
923 bdrv_flags
|= BDRV_O_NOCACHE
;
924 } else if (!strcmp(buf
, "writeback")) {
925 bdrv_flags
|= BDRV_O_CACHE_WB
;
926 } else if (!strcmp(buf
, "writethrough")) {
927 /* this is the default */
929 fprintf(stderr
, "qemu: invalid cache option\n");
934 #ifdef CONFIG_LINUX_AIO
935 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
936 if (!strcmp(buf
, "native")) {
937 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
938 } else if (!strcmp(buf
, "threads")) {
939 /* this is the default */
941 fprintf(stderr
, "qemu: invalid aio option\n");
947 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
948 if (strcmp(buf
, "?") == 0) {
949 fprintf(stderr
, "qemu: Supported formats:");
950 bdrv_iterate_format(bdrv_format_print
, NULL
);
951 fprintf(stderr
, "\n");
954 drv
= bdrv_find_whitelisted_format(buf
);
956 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
961 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
962 if (is_extboot
&& extboot_drive
) {
963 fprintf(stderr
, "qemu: two bootable drives specified\n");
967 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
968 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
969 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
970 fprintf(stderr
, "werror is no supported by this format\n");
974 on_write_error
= parse_block_error_action(buf
, 0);
975 if (on_write_error
< 0) {
980 on_read_error
= BLOCK_ERR_REPORT
;
981 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
982 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
983 fprintf(stderr
, "rerror is no supported by this format\n");
987 on_read_error
= parse_block_error_action(buf
, 1);
988 if (on_read_error
< 0) {
993 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
994 if (type
!= IF_VIRTIO
) {
995 fprintf(stderr
, "addr is not supported\n");
1000 /* compute bus and unit according index */
1003 if (bus_id
!= 0 || unit_id
!= -1) {
1005 "qemu: index cannot be used with bus and unit\n");
1013 unit_id
= index
% max_devs
;
1014 bus_id
= index
/ max_devs
;
1018 /* if user doesn't specify a unit_id,
1019 * try to find the first free
1022 if (unit_id
== -1) {
1024 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1026 if (max_devs
&& unit_id
>= max_devs
) {
1027 unit_id
-= max_devs
;
1035 if (max_devs
&& unit_id
>= max_devs
) {
1036 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1037 unit_id
, max_devs
- 1);
1042 * ignore multiple definitions
1045 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1052 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1053 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1054 dinfo
->id
= qemu_strdup(buf
);
1056 /* no id supplied -> create one */
1057 dinfo
->id
= qemu_mallocz(32);
1058 if (type
== IF_IDE
|| type
== IF_SCSI
)
1059 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1061 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1062 devname
, bus_id
, mediastr
, unit_id
);
1064 snprintf(dinfo
->id
, 32, "%s%s%i",
1065 devname
, mediastr
, unit_id
);
1067 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1068 dinfo
->devaddr
= devaddr
;
1070 dinfo
->bus
= bus_id
;
1071 dinfo
->unit
= unit_id
;
1072 dinfo
->on_read_error
= on_read_error
;
1073 dinfo
->on_write_error
= on_write_error
;
1076 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1077 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1079 extboot_drive
= dinfo
;
1090 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1091 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1095 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1100 /* FIXME: This isn't really a floppy, but it's a reasonable
1103 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1109 /* add virtio block device */
1110 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1111 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1112 qemu_opt_set(opts
, "drive", dinfo
->id
);
1114 qemu_opt_set(opts
, "addr", devaddr
);
1124 /* always use write-back with snapshot */
1125 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
1126 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
);
1129 if (media
== MEDIA_CDROM
) {
1130 /* CDROM is fine for any interface, don't check. */
1132 } else if (ro
== 1) {
1133 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1134 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1139 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1141 if (bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1142 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1143 file
, strerror(errno
));
1147 if (bdrv_key_required(dinfo
->bdrv
))
1153 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1155 QEMUMachine
*machine
= opaque
;
1156 int fatal_error
= 0;
1158 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1165 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1167 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1168 qemu_opt_set(opts
, "snapshot", "on");
1173 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1175 boot_set_handler
= func
;
1176 boot_set_opaque
= opaque
;
1179 int qemu_boot_set(const char *boot_devices
)
1181 if (!boot_set_handler
) {
1184 return boot_set_handler(boot_set_opaque
, boot_devices
);
1187 static void validate_bootdevices(char *devices
)
1189 /* We just do some generic consistency checks */
1193 for (p
= devices
; *p
!= '\0'; p
++) {
1194 /* Allowed boot devices are:
1195 * a-b: floppy disk drives
1196 * c-f: IDE disk drives
1197 * g-m: machine implementation dependant drives
1198 * n-p: network devices
1199 * It's up to each machine implementation to check if the given boot
1200 * devices match the actual hardware implementation and firmware
1203 if (*p
< 'a' || *p
> 'p') {
1204 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1207 if (bitmap
& (1 << (*p
- 'a'))) {
1208 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1211 bitmap
|= 1 << (*p
- 'a');
1215 static void restore_boot_devices(void *opaque
)
1217 char *standard_boot_devices
= opaque
;
1218 static int first
= 1;
1220 /* Restore boot order and remove ourselves after the first boot */
1226 qemu_boot_set(standard_boot_devices
);
1228 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1229 qemu_free(standard_boot_devices
);
1232 static void numa_add(const char *optarg
)
1236 unsigned long long value
, endvalue
;
1239 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1240 if (!strcmp(option
, "node")) {
1241 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1242 nodenr
= nb_numa_nodes
;
1244 nodenr
= strtoull(option
, NULL
, 10);
1247 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1248 node_mem
[nodenr
] = 0;
1250 value
= strtoull(option
, &endptr
, 0);
1252 case 0: case 'M': case 'm':
1259 node_mem
[nodenr
] = value
;
1261 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1262 node_cpumask
[nodenr
] = 0;
1264 value
= strtoull(option
, &endptr
, 10);
1267 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1269 if (*endptr
== '-') {
1270 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1271 if (endvalue
>= 63) {
1274 "only 63 CPUs in NUMA mode supported.\n");
1276 value
= (2ULL << endvalue
) - (1ULL << value
);
1278 value
= 1ULL << value
;
1281 node_cpumask
[nodenr
] = value
;
1288 static void smp_parse(const char *optarg
)
1290 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1294 smp
= strtoul(optarg
, &endptr
, 10);
1295 if (endptr
!= optarg
) {
1296 if (*endptr
== ',') {
1300 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1301 sockets
= strtoull(option
, NULL
, 10);
1302 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1303 cores
= strtoull(option
, NULL
, 10);
1304 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1305 threads
= strtoull(option
, NULL
, 10);
1306 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1307 max_cpus
= strtoull(option
, NULL
, 10);
1309 /* compute missing values, prefer sockets over cores over threads */
1310 if (smp
== 0 || sockets
== 0) {
1311 sockets
= sockets
> 0 ? sockets
: 1;
1312 cores
= cores
> 0 ? cores
: 1;
1313 threads
= threads
> 0 ? threads
: 1;
1315 smp
= cores
* threads
* sockets
;
1319 threads
= threads
> 0 ? threads
: 1;
1320 cores
= smp
/ (sockets
* threads
);
1323 threads
= smp
/ (cores
* sockets
);
1328 smp_cores
= cores
> 0 ? cores
: 1;
1329 smp_threads
= threads
> 0 ? threads
: 1;
1331 max_cpus
= smp_cpus
;
1334 /***********************************************************/
1337 static int usb_device_add(const char *devname
, int is_hotplug
)
1340 USBDevice
*dev
= NULL
;
1345 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1346 dev
= usbdevice_create(devname
);
1350 /* the other ones */
1351 if (strstart(devname
, "host:", &p
)) {
1352 dev
= usb_host_device_open(p
);
1353 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1354 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1355 bt_new_hci(qemu_find_bt_vlan(0)));
1366 static int usb_device_del(const char *devname
)
1371 if (strstart(devname
, "host:", &p
))
1372 return usb_host_device_close(p
);
1377 p
= strchr(devname
, '.');
1380 bus_num
= strtoul(devname
, NULL
, 0);
1381 addr
= strtoul(p
+ 1, NULL
, 0);
1383 return usb_device_delete_addr(bus_num
, addr
);
1386 static int usb_parse(const char *cmdline
)
1389 r
= usb_device_add(cmdline
, 0);
1391 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1396 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1398 const char *devname
= qdict_get_str(qdict
, "devname");
1399 if (usb_device_add(devname
, 1) < 0) {
1400 error_report("could not add USB device '%s'", devname
);
1404 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1406 const char *devname
= qdict_get_str(qdict
, "devname");
1407 if (usb_device_del(devname
) < 0) {
1408 error_report("could not delete USB device '%s'", devname
);
1412 /***********************************************************/
1413 /* PCMCIA/Cardbus */
1415 static struct pcmcia_socket_entry_s
{
1416 PCMCIASocket
*socket
;
1417 struct pcmcia_socket_entry_s
*next
;
1418 } *pcmcia_sockets
= 0;
1420 void pcmcia_socket_register(PCMCIASocket
*socket
)
1422 struct pcmcia_socket_entry_s
*entry
;
1424 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1425 entry
->socket
= socket
;
1426 entry
->next
= pcmcia_sockets
;
1427 pcmcia_sockets
= entry
;
1430 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1432 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1434 ptr
= &pcmcia_sockets
;
1435 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1436 if (entry
->socket
== socket
) {
1442 void pcmcia_info(Monitor
*mon
)
1444 struct pcmcia_socket_entry_s
*iter
;
1446 if (!pcmcia_sockets
)
1447 monitor_printf(mon
, "No PCMCIA sockets\n");
1449 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1450 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1451 iter
->socket
->attached
? iter
->socket
->card_string
:
1455 /***********************************************************/
1458 typedef struct IOHandlerRecord
{
1460 IOCanReadHandler
*fd_read_poll
;
1462 IOHandler
*fd_write
;
1465 /* temporary data */
1467 QLIST_ENTRY(IOHandlerRecord
) next
;
1470 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1471 QLIST_HEAD_INITIALIZER(io_handlers
);
1474 /* XXX: fd_read_poll should be suppressed, but an API change is
1475 necessary in the character devices to suppress fd_can_read(). */
1476 int qemu_set_fd_handler2(int fd
,
1477 IOCanReadHandler
*fd_read_poll
,
1479 IOHandler
*fd_write
,
1482 IOHandlerRecord
*ioh
;
1484 if (!fd_read
&& !fd_write
) {
1485 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1486 if (ioh
->fd
== fd
) {
1492 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1496 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1497 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1500 ioh
->fd_read_poll
= fd_read_poll
;
1501 ioh
->fd_read
= fd_read
;
1502 ioh
->fd_write
= fd_write
;
1503 ioh
->opaque
= opaque
;
1506 qemu_notify_event();
1510 int qemu_set_fd_handler(int fd
,
1512 IOHandler
*fd_write
,
1515 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1519 /***********************************************************/
1520 /* Polling handling */
1522 typedef struct PollingEntry
{
1525 struct PollingEntry
*next
;
1528 static PollingEntry
*first_polling_entry
;
1530 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1532 PollingEntry
**ppe
, *pe
;
1533 pe
= qemu_mallocz(sizeof(PollingEntry
));
1535 pe
->opaque
= opaque
;
1536 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1541 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1543 PollingEntry
**ppe
, *pe
;
1544 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1546 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1554 /***********************************************************/
1555 /* Wait objects support */
1556 typedef struct WaitObjects
{
1558 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1559 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1560 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1563 static WaitObjects wait_objects
= {0};
1565 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1567 WaitObjects
*w
= &wait_objects
;
1569 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1571 w
->events
[w
->num
] = handle
;
1572 w
->func
[w
->num
] = func
;
1573 w
->opaque
[w
->num
] = opaque
;
1578 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1581 WaitObjects
*w
= &wait_objects
;
1584 for (i
= 0; i
< w
->num
; i
++) {
1585 if (w
->events
[i
] == handle
)
1588 w
->events
[i
] = w
->events
[i
+ 1];
1589 w
->func
[i
] = w
->func
[i
+ 1];
1590 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1598 /***********************************************************/
1599 /* machine registration */
1601 static QEMUMachine
*first_machine
= NULL
;
1602 QEMUMachine
*current_machine
= NULL
;
1604 int qemu_register_machine(QEMUMachine
*m
)
1607 pm
= &first_machine
;
1615 static QEMUMachine
*find_machine(const char *name
)
1619 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1620 if (!strcmp(m
->name
, name
))
1622 if (m
->alias
&& !strcmp(m
->alias
, name
))
1628 static QEMUMachine
*find_default_machine(void)
1632 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1633 if (m
->is_default
) {
1640 /***********************************************************/
1641 /* main execution loop */
1643 static void gui_update(void *opaque
)
1645 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1646 DisplayState
*ds
= opaque
;
1647 DisplayChangeListener
*dcl
= ds
->listeners
;
1649 qemu_flush_coalesced_mmio_buffer();
1652 while (dcl
!= NULL
) {
1653 if (dcl
->gui_timer_interval
&&
1654 dcl
->gui_timer_interval
< interval
)
1655 interval
= dcl
->gui_timer_interval
;
1658 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1661 static void nographic_update(void *opaque
)
1663 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1665 qemu_flush_coalesced_mmio_buffer();
1666 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1669 struct vm_change_state_entry
{
1670 VMChangeStateHandler
*cb
;
1672 QLIST_ENTRY (vm_change_state_entry
) entries
;
1675 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1677 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1680 VMChangeStateEntry
*e
;
1682 e
= qemu_mallocz(sizeof (*e
));
1686 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1690 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1692 QLIST_REMOVE (e
, entries
);
1696 void vm_state_notify(int running
, int reason
)
1698 VMChangeStateEntry
*e
;
1700 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1701 e
->cb(e
->opaque
, running
, reason
);
1710 vm_state_notify(1, 0);
1712 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1716 /* reset/shutdown handler */
1718 typedef struct QEMUResetEntry
{
1719 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1720 QEMUResetHandler
*func
;
1724 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1725 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1726 static int reset_requested
;
1727 static int shutdown_requested
;
1728 static int powerdown_requested
;
1729 int debug_requested
;
1730 int vmstop_requested
;
1732 int qemu_no_shutdown(void)
1734 int r
= no_shutdown
;
1739 int qemu_shutdown_requested(void)
1741 int r
= shutdown_requested
;
1742 shutdown_requested
= 0;
1746 int qemu_reset_requested(void)
1748 int r
= reset_requested
;
1749 reset_requested
= 0;
1753 int qemu_powerdown_requested(void)
1755 int r
= powerdown_requested
;
1756 powerdown_requested
= 0;
1760 static int qemu_debug_requested(void)
1762 int r
= debug_requested
;
1763 debug_requested
= 0;
1767 static int qemu_vmstop_requested(void)
1769 int r
= vmstop_requested
;
1770 vmstop_requested
= 0;
1774 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1776 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1779 re
->opaque
= opaque
;
1780 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1783 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1787 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1788 if (re
->func
== func
&& re
->opaque
== opaque
) {
1789 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1796 void qemu_system_reset(void)
1798 QEMUResetEntry
*re
, *nre
;
1800 /* reset all devices */
1801 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1802 re
->func(re
->opaque
);
1804 monitor_protocol_event(QEVENT_RESET
, NULL
);
1805 cpu_synchronize_all_post_reset();
1808 void qemu_system_reset_request(void)
1811 shutdown_requested
= 1;
1813 reset_requested
= 1;
1815 if (cpu_single_env
) {
1816 cpu_single_env
->stopped
= 1;
1818 qemu_notify_event();
1821 void qemu_system_shutdown_request(void)
1823 shutdown_requested
= 1;
1824 qemu_notify_event();
1827 void qemu_system_powerdown_request(void)
1829 powerdown_requested
= 1;
1830 qemu_notify_event();
1834 static void host_main_loop_wait(int *timeout
)
1840 /* XXX: need to suppress polling by better using win32 events */
1842 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1843 ret
|= pe
->func(pe
->opaque
);
1847 WaitObjects
*w
= &wait_objects
;
1849 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1850 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1851 if (w
->func
[ret
- WAIT_OBJECT_0
])
1852 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1854 /* Check for additional signaled events */
1855 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1857 /* Check if event is signaled */
1858 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1859 if(ret2
== WAIT_OBJECT_0
) {
1861 w
->func
[i
](w
->opaque
[i
]);
1862 } else if (ret2
== WAIT_TIMEOUT
) {
1864 err
= GetLastError();
1865 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1868 } else if (ret
== WAIT_TIMEOUT
) {
1870 err
= GetLastError();
1871 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1878 static void host_main_loop_wait(int *timeout
)
1883 void main_loop_wait(int nonblocking
)
1885 IOHandlerRecord
*ioh
;
1886 fd_set rfds
, wfds
, xfds
;
1894 timeout
= qemu_calculate_timeout();
1895 qemu_bh_update_timeout(&timeout
);
1898 host_main_loop_wait(&timeout
);
1900 /* poll any events */
1901 /* XXX: separate device handlers from system ones */
1906 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1910 (!ioh
->fd_read_poll
||
1911 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1912 FD_SET(ioh
->fd
, &rfds
);
1916 if (ioh
->fd_write
) {
1917 FD_SET(ioh
->fd
, &wfds
);
1923 tv
.tv_sec
= timeout
/ 1000;
1924 tv
.tv_usec
= (timeout
% 1000) * 1000;
1926 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1928 qemu_mutex_unlock_iothread();
1929 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1930 qemu_mutex_lock_iothread();
1932 IOHandlerRecord
*pioh
;
1934 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1936 QLIST_REMOVE(ioh
, next
);
1940 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1941 ioh
->fd_read(ioh
->opaque
);
1942 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
1943 FD_CLR(ioh
->fd
, &rfds
);
1945 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1946 ioh
->fd_write(ioh
->opaque
);
1951 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1953 qemu_run_all_timers();
1955 /* Check bottom-halves last in case any of the earlier events triggered
1961 static int vm_can_run(void)
1963 if (powerdown_requested
)
1965 if (reset_requested
)
1967 if (shutdown_requested
)
1969 if (debug_requested
)
1974 qemu_irq qemu_system_powerdown
;
1976 static void main_loop(void)
1980 if (kvm_enabled()) {
1982 cpu_disable_ticks();
1986 qemu_main_loop_start();
1990 bool nonblocking
= false;
1991 #ifdef CONFIG_PROFILER
1994 #ifndef CONFIG_IOTHREAD
1995 nonblocking
= tcg_cpu_exec();
1997 #ifdef CONFIG_PROFILER
1998 ti
= profile_getclock();
2000 main_loop_wait(nonblocking
);
2001 #ifdef CONFIG_PROFILER
2002 dev_time
+= profile_getclock() - ti
;
2004 } while (vm_can_run());
2006 if ((r
= qemu_debug_requested())) {
2009 if (qemu_shutdown_requested()) {
2010 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2017 if (qemu_reset_requested()) {
2019 qemu_system_reset();
2022 if (qemu_powerdown_requested()) {
2023 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2024 qemu_irq_raise(qemu_system_powerdown
);
2026 if ((r
= qemu_vmstop_requested())) {
2033 static void version(void)
2035 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2038 static void help(int exitcode
)
2040 const char *options_help
=
2041 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2043 #define DEFHEADING(text) stringify(text) "\n"
2044 #include "qemu-options.h"
2050 printf("usage: %s [options] [disk_image]\n"
2052 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
2055 "During emulation, the following keys are useful:\n"
2056 "ctrl-alt-f toggle full screen\n"
2057 "ctrl-alt-n switch to virtual console 'n'\n"
2058 "ctrl-alt toggle mouse and keyboard grab\n"
2060 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2066 #define HAS_ARG 0x0001
2069 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2071 #define DEFHEADING(text)
2072 #include "qemu-options.h"
2078 typedef struct QEMUOption
{
2085 static const QEMUOption qemu_options
[] = {
2086 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2087 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2088 { option, opt_arg, opt_enum, arch_mask },
2089 #define DEFHEADING(text)
2090 #include "qemu-options.h"
2096 static void select_vgahw (const char *p
)
2101 vga_interface_type
= VGA_NONE
;
2102 if (strstart(p
, "std", &opts
)) {
2103 vga_interface_type
= VGA_STD
;
2104 } else if (strstart(p
, "cirrus", &opts
)) {
2105 vga_interface_type
= VGA_CIRRUS
;
2106 } else if (strstart(p
, "vmware", &opts
)) {
2107 vga_interface_type
= VGA_VMWARE
;
2108 } else if (strstart(p
, "xenfb", &opts
)) {
2109 vga_interface_type
= VGA_XENFB
;
2110 } else if (!strstart(p
, "none", &opts
)) {
2112 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2116 const char *nextopt
;
2118 if (strstart(opts
, ",retrace=", &nextopt
)) {
2120 if (strstart(opts
, "dumb", &nextopt
))
2121 vga_retrace_method
= VGA_RETRACE_DUMB
;
2122 else if (strstart(opts
, "precise", &nextopt
))
2123 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2124 else goto invalid_vga
;
2125 } else goto invalid_vga
;
2130 static int balloon_parse(const char *arg
)
2134 if (strcmp(arg
, "none") == 0) {
2138 if (!strncmp(arg
, "virtio", 6)) {
2139 if (arg
[6] == ',') {
2140 /* have params -> parse them */
2141 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2145 /* create empty opts */
2146 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2148 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2156 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
2158 exit(STATUS_CONTROL_C_EXIT
);
2165 static void termsig_handler(int signal
)
2167 qemu_system_shutdown_request();
2170 static void sigchld_handler(int signal
)
2172 waitpid(-1, NULL
, WNOHANG
);
2175 static void sighandler_setup(void)
2177 struct sigaction act
;
2179 memset(&act
, 0, sizeof(act
));
2180 act
.sa_handler
= termsig_handler
;
2181 sigaction(SIGINT
, &act
, NULL
);
2182 sigaction(SIGHUP
, &act
, NULL
);
2183 sigaction(SIGTERM
, &act
, NULL
);
2185 act
.sa_handler
= sigchld_handler
;
2186 act
.sa_flags
= SA_NOCLDSTOP
;
2187 sigaction(SIGCHLD
, &act
, NULL
);
2193 /* Look for support files in the same directory as the executable. */
2194 static char *find_datadir(const char *argv0
)
2200 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
2207 while (p
!= buf
&& *p
!= '\\')
2210 if (access(buf
, R_OK
) == 0) {
2211 return qemu_strdup(buf
);
2217 /* Find a likely location for support files using the location of the binary.
2218 For installed binaries this will be "$bindir/../share/qemu". When
2219 running from the build tree this will be "$bindir/../pc-bios". */
2220 #define SHARE_SUFFIX "/share/qemu"
2221 #define BUILD_SUFFIX "/pc-bios"
2222 static char *find_datadir(const char *argv0
)
2230 #if defined(__linux__)
2233 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
2239 #elif defined(__FreeBSD__)
2241 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
2242 size_t len
= sizeof(buf
) - 1;
2245 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
2247 buf
[sizeof(buf
) - 1] = '\0';
2252 /* If we don't have any way of figuring out the actual executable
2253 location then try argv[0]. */
2255 p
= realpath(argv0
, buf
);
2263 max_len
= strlen(dir
) +
2264 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
2265 res
= qemu_mallocz(max_len
);
2266 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
2267 if (access(res
, R_OK
)) {
2268 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
2269 if (access(res
, R_OK
)) {
2281 char *qemu_find_file(int type
, const char *name
)
2287 /* If name contains path separators then try it as a straight path. */
2288 if ((strchr(name
, '/') || strchr(name
, '\\'))
2289 && access(name
, R_OK
) == 0) {
2290 return qemu_strdup(name
);
2293 case QEMU_FILE_TYPE_BIOS
:
2296 case QEMU_FILE_TYPE_KEYMAP
:
2297 subdir
= "keymaps/";
2302 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2303 buf
= qemu_mallocz(len
);
2304 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2305 if (access(buf
, R_OK
)) {
2312 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2314 return qdev_device_help(opts
);
2317 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2321 dev
= qdev_device_add(opts
);
2327 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2329 CharDriverState
*chr
;
2331 chr
= qemu_chr_open_opts(opts
, NULL
);
2338 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2341 ret
= qemu_fsdev_add(opts
);
2347 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2349 CharDriverState
*chr
;
2350 const char *chardev
;
2354 mode
= qemu_opt_get(opts
, "mode");
2358 if (strcmp(mode
, "readline") == 0) {
2359 flags
= MONITOR_USE_READLINE
;
2360 } else if (strcmp(mode
, "control") == 0) {
2361 flags
= MONITOR_USE_CONTROL
;
2363 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2367 if (qemu_opt_get_bool(opts
, "default", 0))
2368 flags
|= MONITOR_IS_DEFAULT
;
2370 chardev
= qemu_opt_get(opts
, "chardev");
2371 chr
= qemu_chr_find(chardev
);
2373 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2377 monitor_init(chr
, flags
);
2381 static void monitor_parse(const char *optarg
, const char *mode
)
2383 static int monitor_device_index
= 0;
2389 if (strstart(optarg
, "chardev:", &p
)) {
2390 snprintf(label
, sizeof(label
), "%s", p
);
2392 snprintf(label
, sizeof(label
), "compat_monitor%d",
2393 monitor_device_index
);
2394 if (monitor_device_index
== 0) {
2397 opts
= qemu_chr_parse_compat(label
, optarg
);
2399 fprintf(stderr
, "parse error: %s\n", optarg
);
2404 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2406 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2409 qemu_opt_set(opts
, "mode", mode
);
2410 qemu_opt_set(opts
, "chardev", label
);
2412 qemu_opt_set(opts
, "default", "on");
2413 monitor_device_index
++;
2416 struct device_config
{
2418 DEV_USB
, /* -usbdevice */
2420 DEV_SERIAL
, /* -serial */
2421 DEV_PARALLEL
, /* -parallel */
2422 DEV_VIRTCON
, /* -virtioconsole */
2423 DEV_DEBUGCON
, /* -debugcon */
2425 const char *cmdline
;
2426 QTAILQ_ENTRY(device_config
) next
;
2428 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2430 static void add_device_config(int type
, const char *cmdline
)
2432 struct device_config
*conf
;
2434 conf
= qemu_mallocz(sizeof(*conf
));
2436 conf
->cmdline
= cmdline
;
2437 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2440 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2442 struct device_config
*conf
;
2445 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2446 if (conf
->type
!= type
)
2448 rc
= func(conf
->cmdline
);
2455 static int serial_parse(const char *devname
)
2457 static int index
= 0;
2460 if (strcmp(devname
, "none") == 0)
2462 if (index
== MAX_SERIAL_PORTS
) {
2463 fprintf(stderr
, "qemu: too many serial ports\n");
2466 snprintf(label
, sizeof(label
), "serial%d", index
);
2467 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2468 if (!serial_hds
[index
]) {
2469 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2470 devname
, strerror(errno
));
2477 static int parallel_parse(const char *devname
)
2479 static int index
= 0;
2482 if (strcmp(devname
, "none") == 0)
2484 if (index
== MAX_PARALLEL_PORTS
) {
2485 fprintf(stderr
, "qemu: too many parallel ports\n");
2488 snprintf(label
, sizeof(label
), "parallel%d", index
);
2489 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2490 if (!parallel_hds
[index
]) {
2491 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2492 devname
, strerror(errno
));
2499 static int virtcon_parse(const char *devname
)
2501 static int index
= 0;
2503 QemuOpts
*bus_opts
, *dev_opts
;
2505 if (strcmp(devname
, "none") == 0)
2507 if (index
== MAX_VIRTIO_CONSOLES
) {
2508 fprintf(stderr
, "qemu: too many virtio consoles\n");
2512 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2513 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2515 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2516 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2518 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2519 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2520 if (!virtcon_hds
[index
]) {
2521 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2522 devname
, strerror(errno
));
2525 qemu_opt_set(dev_opts
, "chardev", label
);
2531 static int debugcon_parse(const char *devname
)
2535 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2538 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2540 fprintf(stderr
, "qemu: already have a debugcon device\n");
2543 qemu_opt_set(opts
, "driver", "isa-debugcon");
2544 qemu_opt_set(opts
, "chardev", "debugcon");
2548 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2549 const char **poptarg
, int *poptind
)
2551 const QEMUOption
*popt
;
2552 int optind
= *poptind
;
2553 char *r
= argv
[optind
];
2556 loc_set_cmdline(argv
, optind
, 1);
2558 /* Treat --foo the same as -foo. */
2561 popt
= qemu_options
;
2564 error_report("invalid option");
2567 if (!strcmp(popt
->name
, r
+ 1))
2571 if (popt
->flags
& HAS_ARG
) {
2572 if (optind
>= argc
) {
2573 error_report("requires an argument");
2576 optarg
= argv
[optind
++];
2577 loc_set_cmdline(argv
, optind
- 2, 2);
2588 int main(int argc
, char **argv
, char **envp
)
2590 const char *gdbstub_dev
= NULL
;
2592 int snapshot
, linux_boot
;
2593 const char *icount_option
= NULL
;
2594 const char *initrd_filename
;
2595 const char *kernel_filename
, *kernel_cmdline
;
2596 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2598 DisplayChangeListener
*dcl
;
2599 int cyls
, heads
, secs
, translation
;
2600 QemuOpts
*hda_opts
= NULL
, *opts
;
2603 const char *loadvm
= NULL
;
2604 QEMUMachine
*machine
;
2605 const char *cpu_model
;
2610 const char *pid_file
= NULL
;
2611 const char *incoming
= NULL
;
2614 struct passwd
*pwd
= NULL
;
2615 const char *chroot_dir
= NULL
;
2616 const char *run_as
= NULL
;
2618 int show_vnc_port
= 0;
2621 error_set_progname(argv
[0]);
2625 qemu_cache_utils_init(envp
);
2627 QLIST_INIT (&vm_change_state_head
);
2630 struct sigaction act
;
2631 sigfillset(&act
.sa_mask
);
2633 act
.sa_handler
= SIG_IGN
;
2634 sigaction(SIGPIPE
, &act
, NULL
);
2637 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2638 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2639 QEMU to run on a single CPU */
2644 h
= GetCurrentProcess();
2645 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2646 for(i
= 0; i
< 32; i
++) {
2647 if (mask
& (1 << i
))
2652 SetProcessAffinityMask(h
, mask
);
2658 module_call_init(MODULE_INIT_MACHINE
);
2659 machine
= find_default_machine();
2661 initrd_filename
= NULL
;
2664 kernel_filename
= NULL
;
2665 kernel_cmdline
= "";
2666 cyls
= heads
= secs
= 0;
2667 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2669 for (i
= 0; i
< MAX_NODES
; i
++) {
2671 node_cpumask
[i
] = 0;
2674 assigned_devices_index
= 0;
2682 /* first pass of option parsing */
2684 while (optind
< argc
) {
2685 if (argv
[optind
][0] != '-') {
2690 const QEMUOption
*popt
;
2692 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2693 switch (popt
->index
) {
2694 case QEMU_OPTION_nodefconfig
:
2704 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2705 if (ret
< 0 && ret
!= -ENOENT
) {
2709 ret
= qemu_read_config_file(arch_config_name
);
2710 if (ret
< 0 && ret
!= -ENOENT
) {
2716 /* second pass of option parsing */
2721 if (argv
[optind
][0] != '-') {
2722 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2724 const QEMUOption
*popt
;
2726 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2727 if (!(popt
->arch_mask
& arch_type
)) {
2728 printf("Option %s not supported for this target\n", popt
->name
);
2731 switch(popt
->index
) {
2733 machine
= find_machine(optarg
);
2736 printf("Supported machines are:\n");
2737 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2739 printf("%-10s %s (alias of %s)\n",
2740 m
->alias
, m
->desc
, m
->name
);
2741 printf("%-10s %s%s\n",
2743 m
->is_default
? " (default)" : "");
2745 exit(*optarg
!= '?');
2748 case QEMU_OPTION_cpu
:
2749 /* hw initialization will check this */
2750 if (*optarg
== '?') {
2751 list_cpus(stdout
, &fprintf
, optarg
);
2757 case QEMU_OPTION_initrd
:
2758 initrd_filename
= optarg
;
2760 case QEMU_OPTION_hda
:
2762 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2764 hda_opts
= drive_add(optarg
, HD_ALIAS
2765 ",cyls=%d,heads=%d,secs=%d%s",
2766 0, cyls
, heads
, secs
,
2767 translation
== BIOS_ATA_TRANSLATION_LBA
?
2769 translation
== BIOS_ATA_TRANSLATION_NONE
?
2770 ",trans=none" : "");
2772 case QEMU_OPTION_hdb
:
2773 case QEMU_OPTION_hdc
:
2774 case QEMU_OPTION_hdd
:
2775 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2777 case QEMU_OPTION_drive
:
2778 drive_add(NULL
, "%s", optarg
);
2780 case QEMU_OPTION_set
:
2781 if (qemu_set_option(optarg
) != 0)
2784 case QEMU_OPTION_global
:
2785 if (qemu_global_option(optarg
) != 0)
2788 case QEMU_OPTION_mtdblock
:
2789 drive_add(optarg
, MTD_ALIAS
);
2791 case QEMU_OPTION_sd
:
2792 drive_add(optarg
, SD_ALIAS
);
2794 case QEMU_OPTION_pflash
:
2795 drive_add(optarg
, PFLASH_ALIAS
);
2797 case QEMU_OPTION_snapshot
:
2800 case QEMU_OPTION_hdachs
:
2804 cyls
= strtol(p
, (char **)&p
, 0);
2805 if (cyls
< 1 || cyls
> 16383)
2810 heads
= strtol(p
, (char **)&p
, 0);
2811 if (heads
< 1 || heads
> 16)
2816 secs
= strtol(p
, (char **)&p
, 0);
2817 if (secs
< 1 || secs
> 63)
2821 if (!strcmp(p
, "none"))
2822 translation
= BIOS_ATA_TRANSLATION_NONE
;
2823 else if (!strcmp(p
, "lba"))
2824 translation
= BIOS_ATA_TRANSLATION_LBA
;
2825 else if (!strcmp(p
, "auto"))
2826 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2829 } else if (*p
!= '\0') {
2831 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2834 if (hda_opts
!= NULL
) {
2836 snprintf(num
, sizeof(num
), "%d", cyls
);
2837 qemu_opt_set(hda_opts
, "cyls", num
);
2838 snprintf(num
, sizeof(num
), "%d", heads
);
2839 qemu_opt_set(hda_opts
, "heads", num
);
2840 snprintf(num
, sizeof(num
), "%d", secs
);
2841 qemu_opt_set(hda_opts
, "secs", num
);
2842 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2843 qemu_opt_set(hda_opts
, "trans", "lba");
2844 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2845 qemu_opt_set(hda_opts
, "trans", "none");
2849 case QEMU_OPTION_numa
:
2850 if (nb_numa_nodes
>= MAX_NODES
) {
2851 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2856 case QEMU_OPTION_nographic
:
2857 display_type
= DT_NOGRAPHIC
;
2859 #ifdef CONFIG_CURSES
2860 case QEMU_OPTION_curses
:
2861 display_type
= DT_CURSES
;
2864 case QEMU_OPTION_portrait
:
2867 case QEMU_OPTION_kernel
:
2868 kernel_filename
= optarg
;
2870 case QEMU_OPTION_append
:
2871 kernel_cmdline
= optarg
;
2873 case QEMU_OPTION_cdrom
:
2874 drive_add(optarg
, CDROM_ALIAS
);
2876 case QEMU_OPTION_boot
:
2878 static const char * const params
[] = {
2879 "order", "once", "menu", NULL
2881 char buf
[sizeof(boot_devices
)];
2882 char *standard_boot_devices
;
2885 if (!strchr(optarg
, '=')) {
2887 pstrcpy(buf
, sizeof(buf
), optarg
);
2888 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2890 "qemu: unknown boot parameter '%s' in '%s'\n",
2896 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2897 validate_bootdevices(buf
);
2898 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2901 if (get_param_value(buf
, sizeof(buf
),
2903 validate_bootdevices(buf
);
2904 standard_boot_devices
= qemu_strdup(boot_devices
);
2905 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2906 qemu_register_reset(restore_boot_devices
,
2907 standard_boot_devices
);
2909 if (get_param_value(buf
, sizeof(buf
),
2911 if (!strcmp(buf
, "on")) {
2913 } else if (!strcmp(buf
, "off")) {
2917 "qemu: invalid option value '%s'\n",
2925 case QEMU_OPTION_fda
:
2926 case QEMU_OPTION_fdb
:
2927 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2929 case QEMU_OPTION_no_fd_bootchk
:
2932 case QEMU_OPTION_netdev
:
2933 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2937 case QEMU_OPTION_net
:
2938 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2943 case QEMU_OPTION_tftp
:
2944 legacy_tftp_prefix
= optarg
;
2946 case QEMU_OPTION_bootp
:
2947 legacy_bootp_filename
= optarg
;
2950 case QEMU_OPTION_smb
:
2951 if (net_slirp_smb(optarg
) < 0)
2955 case QEMU_OPTION_redir
:
2956 if (net_slirp_redir(optarg
) < 0)
2960 case QEMU_OPTION_bt
:
2961 add_device_config(DEV_BT
, optarg
);
2963 case QEMU_OPTION_audio_help
:
2964 if (!(audio_available())) {
2965 printf("Option %s not supported for this target\n", popt
->name
);
2971 case QEMU_OPTION_soundhw
:
2972 if (!(audio_available())) {
2973 printf("Option %s not supported for this target\n", popt
->name
);
2976 select_soundhw (optarg
);
2981 case QEMU_OPTION_version
:
2985 case QEMU_OPTION_m
: {
2989 value
= strtoul(optarg
, &ptr
, 10);
2991 case 0: case 'M': case 'm':
2998 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3002 /* On 32-bit hosts, QEMU is limited by virtual address space */
3003 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3004 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3007 if (value
!= (uint64_t)(ram_addr_t
)value
) {
3008 fprintf(stderr
, "qemu: ram size too large\n");
3014 case QEMU_OPTION_mempath
:
3018 case QEMU_OPTION_mem_prealloc
:
3023 set_cpu_log(optarg
);
3026 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
3028 case QEMU_OPTION_gdb
:
3029 gdbstub_dev
= optarg
;
3034 case QEMU_OPTION_bios
:
3037 case QEMU_OPTION_singlestep
:
3044 keyboard_layout
= optarg
;
3046 case QEMU_OPTION_localtime
:
3049 case QEMU_OPTION_vga
:
3050 select_vgahw (optarg
);
3057 w
= strtol(p
, (char **)&p
, 10);
3060 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3066 h
= strtol(p
, (char **)&p
, 10);
3071 depth
= strtol(p
, (char **)&p
, 10);
3072 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3073 depth
!= 24 && depth
!= 32)
3075 } else if (*p
== '\0') {
3076 depth
= graphic_depth
;
3083 graphic_depth
= depth
;
3086 case QEMU_OPTION_echr
:
3089 term_escape_char
= strtol(optarg
, &r
, 0);
3091 printf("Bad argument to echr\n");
3094 case QEMU_OPTION_monitor
:
3095 monitor_parse(optarg
, "readline");
3096 default_monitor
= 0;
3098 case QEMU_OPTION_qmp
:
3099 monitor_parse(optarg
, "control");
3100 default_monitor
= 0;
3102 case QEMU_OPTION_mon
:
3103 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
3107 default_monitor
= 0;
3109 case QEMU_OPTION_chardev
:
3110 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
3116 case QEMU_OPTION_fsdev
:
3117 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
3119 fprintf(stderr
, "parse error: %s\n", optarg
);
3123 case QEMU_OPTION_virtfs
: {
3124 char *arg_fsdev
= NULL
;
3125 char *arg_9p
= NULL
;
3128 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
3130 fprintf(stderr
, "parse error: %s\n", optarg
);
3134 len
= strlen(",id=,path=");
3135 len
+= strlen(qemu_opt_get(opts
, "fstype"));
3136 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
3137 len
+= strlen(qemu_opt_get(opts
, "path"));
3138 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
3141 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
3146 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
3147 qemu_opt_get(opts
, "fstype"),
3148 qemu_opt_get(opts
, "mount_tag"),
3149 qemu_opt_get(opts
, "path"));
3151 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
3152 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
3153 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
3156 fprintf(stderr
, "No memory to parse -device for %s\n",
3161 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
3162 qemu_opt_get(opts
, "mount_tag"),
3163 qemu_opt_get(opts
, "mount_tag"));
3165 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
3166 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
3170 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
3171 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
3175 qemu_free(arg_fsdev
);
3180 case QEMU_OPTION_serial
:
3181 add_device_config(DEV_SERIAL
, optarg
);
3183 if (strncmp(optarg
, "mon:", 4) == 0) {
3184 default_monitor
= 0;
3187 case QEMU_OPTION_watchdog
:
3190 "qemu: only one watchdog option may be given\n");
3195 case QEMU_OPTION_watchdog_action
:
3196 if (select_watchdog_action(optarg
) == -1) {
3197 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3201 case QEMU_OPTION_virtiocon
:
3202 add_device_config(DEV_VIRTCON
, optarg
);
3203 default_virtcon
= 0;
3204 if (strncmp(optarg
, "mon:", 4) == 0) {
3205 default_monitor
= 0;
3208 case QEMU_OPTION_parallel
:
3209 add_device_config(DEV_PARALLEL
, optarg
);
3210 default_parallel
= 0;
3211 if (strncmp(optarg
, "mon:", 4) == 0) {
3212 default_monitor
= 0;
3215 case QEMU_OPTION_debugcon
:
3216 add_device_config(DEV_DEBUGCON
, optarg
);
3218 case QEMU_OPTION_loadvm
:
3221 case QEMU_OPTION_full_screen
:
3225 case QEMU_OPTION_no_frame
:
3228 case QEMU_OPTION_alt_grab
:
3231 case QEMU_OPTION_ctrl_grab
:
3234 case QEMU_OPTION_no_quit
:
3237 case QEMU_OPTION_sdl
:
3238 display_type
= DT_SDL
;
3241 case QEMU_OPTION_pidfile
:
3244 case QEMU_OPTION_win2k_hack
:
3245 win2k_install_hack
= 1;
3247 case QEMU_OPTION_rtc_td_hack
:
3250 case QEMU_OPTION_acpitable
:
3251 do_acpitable_option(optarg
);
3253 case QEMU_OPTION_smbios
:
3254 do_smbios_option(optarg
);
3257 case QEMU_OPTION_enable_kvm
:
3261 case QEMU_OPTION_no_kvm
:
3264 case QEMU_OPTION_no_kvm_irqchip
: {
3269 case QEMU_OPTION_no_kvm_pit
: {
3273 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3274 kvm_pit_reinject
= 0;
3277 case QEMU_OPTION_enable_nesting
: {
3281 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
3282 case QEMU_OPTION_pcidevice
:
3283 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
3284 fprintf(stderr
, "Too many assigned devices\n");
3287 assigned_devices
[assigned_devices_index
] = optarg
;
3288 assigned_devices_index
++;
3291 case QEMU_OPTION_usb
:
3294 case QEMU_OPTION_usbdevice
:
3296 add_device_config(DEV_USB
, optarg
);
3298 case QEMU_OPTION_device
:
3299 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3303 case QEMU_OPTION_smp
:
3306 fprintf(stderr
, "Invalid number of CPUs\n");
3309 if (max_cpus
< smp_cpus
) {
3310 fprintf(stderr
, "maxcpus must be equal to or greater than "
3314 if (max_cpus
> 255) {
3315 fprintf(stderr
, "Unsupported number of maxcpus\n");
3319 case QEMU_OPTION_vnc
:
3320 display_type
= DT_VNC
;
3321 vnc_display
= optarg
;
3323 case QEMU_OPTION_no_acpi
:
3326 case QEMU_OPTION_no_hpet
:
3329 case QEMU_OPTION_balloon
:
3330 if (balloon_parse(optarg
) < 0) {
3331 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3335 case QEMU_OPTION_no_reboot
:
3338 case QEMU_OPTION_no_shutdown
:
3341 case QEMU_OPTION_show_cursor
:
3344 case QEMU_OPTION_uuid
:
3345 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3346 fprintf(stderr
, "Fail to parse UUID string."
3347 " Wrong format.\n");
3352 case QEMU_OPTION_daemonize
:
3356 case QEMU_OPTION_option_rom
:
3357 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3358 fprintf(stderr
, "Too many option ROMs\n");
3361 option_rom
[nb_option_roms
] = optarg
;
3364 case QEMU_OPTION_semihosting
:
3365 semihosting_enabled
= 1;
3367 case QEMU_OPTION_tdf
:
3370 case QEMU_OPTION_kvm_shadow_memory
:
3371 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3373 case QEMU_OPTION_name
:
3374 qemu_name
= qemu_strdup(optarg
);
3376 char *p
= strchr(qemu_name
, ',');
3379 if (strncmp(p
, "process=", 8)) {
3380 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3388 case QEMU_OPTION_prom_env
:
3389 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3390 fprintf(stderr
, "Too many prom variables\n");
3393 prom_envs
[nb_prom_envs
] = optarg
;
3396 case QEMU_OPTION_old_param
:
3399 case QEMU_OPTION_clock
:
3400 configure_alarms(optarg
);
3402 case QEMU_OPTION_startdate
:
3403 configure_rtc_date_offset(optarg
, 1);
3405 case QEMU_OPTION_rtc
:
3406 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3410 configure_rtc(opts
);
3412 case QEMU_OPTION_tb_size
:
3413 tb_size
= strtol(optarg
, NULL
, 0);
3417 case QEMU_OPTION_icount
:
3418 icount_option
= optarg
;
3420 case QEMU_OPTION_incoming
:
3423 case QEMU_OPTION_nodefaults
:
3425 default_parallel
= 0;
3426 default_virtcon
= 0;
3427 default_monitor
= 0;
3435 case QEMU_OPTION_chroot
:
3436 chroot_dir
= optarg
;
3438 case QEMU_OPTION_runas
:
3441 case QEMU_OPTION_nvram
:
3445 case QEMU_OPTION_xen_domid
:
3446 if (!(xen_available())) {
3447 printf("Option %s not supported for this target\n", popt
->name
);
3450 xen_domid
= atoi(optarg
);
3452 case QEMU_OPTION_xen_create
:
3453 if (!(xen_available())) {
3454 printf("Option %s not supported for this target\n", popt
->name
);
3457 xen_mode
= XEN_CREATE
;
3459 case QEMU_OPTION_xen_attach
:
3460 if (!(xen_available())) {
3461 printf("Option %s not supported for this target\n", popt
->name
);
3464 xen_mode
= XEN_ATTACH
;
3466 case QEMU_OPTION_readconfig
:
3468 int ret
= qemu_read_config_file(optarg
);
3470 fprintf(stderr
, "read config %s: %s\n", optarg
,
3476 case QEMU_OPTION_writeconfig
:
3479 if (strcmp(optarg
, "-") == 0) {
3482 fp
= fopen(optarg
, "w");
3484 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3488 qemu_config_write(fp
);
3497 /* If no data_dir is specified then try to find it relative to the
3500 data_dir
= find_datadir(argv
[0]);
3502 /* If all else fails use the install patch specified when building. */
3504 data_dir
= CONFIG_QEMU_SHAREDIR
;
3508 * Default to max_cpus = smp_cpus, in case the user doesn't
3509 * specify a max_cpus value.
3512 max_cpus
= smp_cpus
;
3514 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3515 if (smp_cpus
> machine
->max_cpus
) {
3516 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3517 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3522 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3523 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3525 if (machine
->no_serial
) {
3528 if (machine
->no_parallel
) {
3529 default_parallel
= 0;
3531 if (!machine
->use_virtcon
) {
3532 default_virtcon
= 0;
3534 if (machine
->no_vga
) {
3537 if (machine
->no_floppy
) {
3540 if (machine
->no_cdrom
) {
3543 if (machine
->no_sdcard
) {
3547 if (display_type
== DT_NOGRAPHIC
) {
3548 if (default_parallel
)
3549 add_device_config(DEV_PARALLEL
, "null");
3550 if (default_serial
&& default_monitor
) {
3551 add_device_config(DEV_SERIAL
, "mon:stdio");
3552 } else if (default_virtcon
&& default_monitor
) {
3553 add_device_config(DEV_VIRTCON
, "mon:stdio");
3556 add_device_config(DEV_SERIAL
, "stdio");
3557 if (default_virtcon
)
3558 add_device_config(DEV_VIRTCON
, "stdio");
3559 if (default_monitor
)
3560 monitor_parse("stdio", "readline");
3564 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3565 if (default_parallel
)
3566 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3567 if (default_monitor
)
3568 monitor_parse("vc:80Cx24C", "readline");
3569 if (default_virtcon
)
3570 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3573 vga_interface_type
= VGA_CIRRUS
;
3577 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3580 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
3589 if (pipe(fds
) == -1)
3600 len
= read(fds
[0], &status
, 1);
3601 if (len
== -1 && (errno
== EINTR
))
3606 else if (status
== 1) {
3607 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3615 qemu_set_cloexec(fds
[1]);
3627 signal(SIGTSTP
, SIG_IGN
);
3628 signal(SIGTTOU
, SIG_IGN
);
3629 signal(SIGTTIN
, SIG_IGN
);
3633 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3637 if (write(fds
[1], &status
, 1) != 1) {
3638 perror("daemonize. Writing to pipe\n");
3642 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3647 int ret
= kvm_init(smp_cpus
);
3649 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
3650 if (!kvm_available()) {
3651 printf("KVM not supported for this target\n");
3653 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3658 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
3664 if (qemu_init_main_loop()) {
3665 fprintf(stderr
, "qemu_init_main_loop failed\n");
3668 linux_boot
= (kernel_filename
!= NULL
);
3670 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3671 fprintf(stderr
, "-append only allowed with -kernel option\n");
3675 if (!linux_boot
&& initrd_filename
!= NULL
) {
3676 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3681 /* Win32 doesn't support line-buffering and requires size >= 2 */
3682 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3685 if (init_timer_alarm() < 0) {
3686 fprintf(stderr
, "could not initialize alarm timer\n");
3689 configure_icount(icount_option
);
3691 if (net_init_clients() < 0) {
3695 /* init the bluetooth world */
3696 if (foreach_device_config(DEV_BT
, bt_parse
))
3699 /* init the memory */
3701 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3703 /* init the dynamic translator */
3704 cpu_exec_init_all(tb_size
* 1024 * 1024);
3706 bdrv_init_with_whitelist();
3710 if (default_cdrom
) {
3711 /* we always create the cdrom drive, even if no disk is there */
3712 drive_add(NULL
, CDROM_ALIAS
);
3715 if (default_floppy
) {
3716 /* we always create at least one floppy */
3717 drive_add(NULL
, FD_ALIAS
, 0);
3720 if (default_sdcard
) {
3721 /* we always create one sd slot, even if no card is in it */
3722 drive_add(NULL
, SD_ALIAS
);
3725 /* open the virtual block devices */
3727 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3728 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3731 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3734 if (nb_numa_nodes
> 0) {
3737 if (nb_numa_nodes
> smp_cpus
) {
3738 nb_numa_nodes
= smp_cpus
;
3741 /* If no memory size if given for any node, assume the default case
3742 * and distribute the available memory equally across all nodes
3744 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3745 if (node_mem
[i
] != 0)
3748 if (i
== nb_numa_nodes
) {
3749 uint64_t usedmem
= 0;
3751 /* On Linux, the each node's border has to be 8MB aligned,
3752 * the final node gets the rest.
3754 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3755 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3756 usedmem
+= node_mem
[i
];
3758 node_mem
[i
] = ram_size
- usedmem
;
3761 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3762 if (node_cpumask
[i
] != 0)
3765 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3766 * must cope with this anyway, because there are BIOSes out there in
3767 * real machines which also use this scheme.
3769 if (i
== nb_numa_nodes
) {
3770 for (i
= 0; i
< smp_cpus
; i
++) {
3771 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3776 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
3780 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3782 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3784 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3786 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3789 module_call_init(MODULE_INIT_DEVICE
);
3791 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3795 i
= select_watchdog(watchdog
);
3797 exit (i
== 1 ? 1 : 0);
3800 if (machine
->compat_props
) {
3801 qdev_prop_register_global_list(machine
->compat_props
);
3805 machine
->init(ram_size
, boot_devices
,
3806 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3808 cpu_synchronize_all_post_init();
3811 /* must be after terminal init, SDL library changes signal handlers */
3817 current_machine
= machine
;
3819 /* init USB devices */
3821 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3825 /* init generic devices */
3826 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3829 net_check_clients();
3831 /* just use the first displaystate for the moment */
3832 ds
= get_displaystate();
3834 if (display_type
== DT_DEFAULT
) {
3835 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3836 display_type
= DT_SDL
;
3838 display_type
= DT_VNC
;
3839 vnc_display
= "localhost:0,to=99";
3845 switch (display_type
) {
3848 #if defined(CONFIG_CURSES)
3850 curses_display_init(ds
, full_screen
);
3853 #if defined(CONFIG_SDL)
3855 sdl_display_init(ds
, full_screen
, no_frame
);
3857 #elif defined(CONFIG_COCOA)
3859 cocoa_display_init(ds
, full_screen
);
3863 vnc_display_init(ds
);
3864 if (vnc_display_open(ds
, vnc_display
) < 0)
3867 if (show_vnc_port
) {
3868 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3876 dcl
= ds
->listeners
;
3877 while (dcl
!= NULL
) {
3878 if (dcl
->dpy_refresh
!= NULL
) {
3879 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3880 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3885 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3886 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3887 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3890 text_consoles_set_display(ds
);
3892 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3893 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3898 qdev_machine_creation_done();
3900 if (rom_load_all() != 0) {
3901 fprintf(stderr
, "rom loading failed\n");
3905 qemu_system_reset();
3907 if (load_vmstate(loadvm
) < 0) {
3913 qemu_start_incoming_migration(incoming
);
3914 } else if (autostart
) {
3924 len
= write(fds
[1], &status
, 1);
3925 if (len
== -1 && (errno
== EINTR
))
3932 perror("not able to chdir to /");
3935 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3941 pwd
= getpwnam(run_as
);
3943 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3949 if (chroot(chroot_dir
) < 0) {
3950 fprintf(stderr
, "chroot failed\n");
3954 perror("not able to chdir to /");
3960 if (setgid(pwd
->pw_gid
) < 0) {
3961 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3964 if (setuid(pwd
->pw_uid
) < 0) {
3965 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3968 if (setuid(0) != -1) {
3969 fprintf(stderr
, "Dropping privileges failed\n");