4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <sys/prctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
81 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83 extern int madvise(caddr_t
, size_t, int);
88 #if defined(__OpenBSD__)
92 #if defined(CONFIG_VDE)
93 #include <libvdeplug.h>
96 #if defined(CONFIG_UUID)
97 #include <uuid/uuid.h>
105 #if defined(__APPLE__) || defined(main)
107 int qemu_main(int argc
, char **argv
, char **envp
);
108 int main(int argc
, char **argv
)
110 return qemu_main(argc
, argv
, NULL
);
113 #define main qemu_main
115 #endif /* CONFIG_SDL */
119 #define main qemu_main
120 #endif /* CONFIG_COCOA */
123 #include "hw/boards.h"
125 #include "hw/pcmcia.h"
127 #include "hw/audiodev.h"
131 #include "hw/watchdog.h"
132 #include "hw/smbios.h"
135 #include "hw/loader.h"
138 #include "net/slirp.h"
143 #include "qemu-timer.h"
144 #include "qemu-char.h"
145 #include "cache-utils.h"
147 #include "block_int.h"
148 #include "block-migration.h"
150 #include "audio/audio.h"
151 #include "migration.h"
154 #include "qemu-option.h"
155 #include "qemu-config.h"
156 #include "qemu-objects.h"
160 #include "exec-all.h"
162 #include "qemu_socket.h"
164 #include "slirp/libslirp.h"
166 #include "qemu-queue.h"
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir
;
176 const char *bios_name
= NULL
;
177 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
178 to store the VM snapshots */
179 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
180 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
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
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static QEMUTimer
*nographic_timer
;
260 uint8_t qemu_uuid
[16];
262 static QEMUBootSetHandler
*boot_set_handler
;
263 static void *boot_set_opaque
;
266 #define SIG_IPI (SIGRTMIN+4)
268 #define SIG_IPI SIGUSR1
271 static int default_serial
= 1;
272 static int default_parallel
= 1;
273 static int default_virtcon
= 1;
274 static int default_monitor
= 1;
275 static int default_vga
= 1;
276 static int default_floppy
= 1;
277 static int default_cdrom
= 1;
278 static int default_sdcard
= 1;
279 static int default_qmp
= 1;
285 { .driver
= "isa-serial", .flag
= &default_serial
},
286 { .driver
= "isa-parallel", .flag
= &default_parallel
},
287 { .driver
= "isa-fdc", .flag
= &default_floppy
},
288 { .driver
= "ide-drive", .flag
= &default_cdrom
},
289 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
292 { .driver
= "VGA", .flag
= &default_vga
},
293 { .driver
= "cirrus-vga", .flag
= &default_vga
},
294 { .driver
= "vmware-svga", .flag
= &default_vga
},
297 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
299 const char *driver
= qemu_opt_get(opts
, "driver");
304 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
305 if (strcmp(default_list
[i
].driver
, driver
) != 0)
307 *(default_list
[i
].flag
) = 0;
312 /***********************************************************/
313 /* x86 ISA bus support */
315 target_phys_addr_t isa_mem_base
= 0;
318 /***********************************************************/
319 void hw_error(const char *fmt
, ...)
325 fprintf(stderr
, "qemu: hardware error: ");
326 vfprintf(stderr
, fmt
, ap
);
327 fprintf(stderr
, "\n");
328 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
329 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
331 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
333 cpu_dump_state(env
, stderr
, fprintf
, 0);
340 static void set_proc_name(const char *s
)
342 #if defined(__linux__) && defined(PR_SET_NAME)
346 name
[sizeof(name
) - 1] = 0;
347 strncpy(name
, s
, sizeof(name
));
348 /* Could rewrite argv[0] too, but that's a bit more complicated.
349 This simple way is enough for `top'. */
350 prctl(PR_SET_NAME
, name
);
357 static QEMUBalloonEvent
*qemu_balloon_event
;
358 void *qemu_balloon_event_opaque
;
360 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
362 qemu_balloon_event
= func
;
363 qemu_balloon_event_opaque
= opaque
;
366 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
368 if (qemu_balloon_event
) {
369 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
376 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
378 if (qemu_balloon_event
) {
379 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
387 /***********************************************************/
388 /* real time host monotonic timer */
390 /* compute with 96 bit intermediate result: (a*b)/c */
391 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
396 #ifdef HOST_WORDS_BIGENDIAN
406 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
407 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
410 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
414 /***********************************************************/
415 /* host time/date access */
416 void qemu_get_timedate(struct tm
*tm
, int offset
)
423 if (rtc_date_offset
== -1) {
427 ret
= localtime(&ti
);
429 ti
-= rtc_date_offset
;
433 memcpy(tm
, ret
, sizeof(struct tm
));
436 int qemu_timedate_diff(struct tm
*tm
)
440 if (rtc_date_offset
== -1)
442 seconds
= mktimegm(tm
);
444 seconds
= mktime(tm
);
446 seconds
= mktimegm(tm
) + rtc_date_offset
;
448 return seconds
- time(NULL
);
451 void rtc_change_mon_event(struct tm
*tm
)
455 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
456 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
457 qobject_decref(data
);
460 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
462 time_t rtc_start_date
;
465 if (!strcmp(startdate
, "now") && legacy
) {
466 rtc_date_offset
= -1;
468 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
476 } else if (sscanf(startdate
, "%d-%d-%d",
488 rtc_start_date
= mktimegm(&tm
);
489 if (rtc_start_date
== -1) {
491 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
492 "'2006-06-17T16:01:21' or '2006-06-17'\n");
495 rtc_date_offset
= time(NULL
) - rtc_start_date
;
499 static void configure_rtc(QemuOpts
*opts
)
503 value
= qemu_opt_get(opts
, "base");
505 if (!strcmp(value
, "utc")) {
507 } else if (!strcmp(value
, "localtime")) {
510 configure_rtc_date_offset(value
, 0);
513 value
= qemu_opt_get(opts
, "clock");
515 if (!strcmp(value
, "host")) {
516 rtc_clock
= host_clock
;
517 } else if (!strcmp(value
, "vm")) {
518 rtc_clock
= vm_clock
;
520 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
524 #ifdef CONFIG_TARGET_I386
525 value
= qemu_opt_get(opts
, "driftfix");
527 if (!strcmp(buf
, "slew")) {
529 } else if (!strcmp(buf
, "none")) {
532 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
540 static void socket_cleanup(void)
545 static int socket_init(void)
550 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
552 err
= WSAGetLastError();
553 fprintf(stderr
, "WSAStartup: %d\n", err
);
556 atexit(socket_cleanup
);
561 /***********************************************************/
562 /* Bluetooth support */
565 static struct HCIInfo
*hci_table
[MAX_NICS
];
567 static struct bt_vlan_s
{
568 struct bt_scatternet_s net
;
570 struct bt_vlan_s
*next
;
573 /* find or alloc a new bluetooth "VLAN" */
574 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
576 struct bt_vlan_s
**pvlan
, *vlan
;
577 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
581 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
583 pvlan
= &first_bt_vlan
;
584 while (*pvlan
!= NULL
)
585 pvlan
= &(*pvlan
)->next
;
590 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
594 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
599 static struct HCIInfo null_hci
= {
600 .cmd_send
= null_hci_send
,
601 .sco_send
= null_hci_send
,
602 .acl_send
= null_hci_send
,
603 .bdaddr_set
= null_hci_addr_set
,
606 struct HCIInfo
*qemu_next_hci(void)
608 if (cur_hci
== nb_hcis
)
611 return hci_table
[cur_hci
++];
614 static struct HCIInfo
*hci_init(const char *str
)
617 struct bt_scatternet_s
*vlan
= 0;
619 if (!strcmp(str
, "null"))
622 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
624 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
625 else if (!strncmp(str
, "hci", 3)) {
628 if (!strncmp(str
+ 3, ",vlan=", 6)) {
629 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
634 vlan
= qemu_find_bt_vlan(0);
636 return bt_new_hci(vlan
);
639 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
644 static int bt_hci_parse(const char *str
)
649 if (nb_hcis
>= MAX_NICS
) {
650 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
663 bdaddr
.b
[5] = 0x56 + nb_hcis
;
664 hci
->bdaddr_set(hci
, bdaddr
.b
);
666 hci_table
[nb_hcis
++] = hci
;
671 static void bt_vhci_add(int vlan_id
)
673 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
676 fprintf(stderr
, "qemu: warning: adding a VHCI to "
677 "an empty scatternet %i\n", vlan_id
);
679 bt_vhci_init(bt_new_hci(vlan
));
682 static struct bt_device_s
*bt_device_add(const char *opt
)
684 struct bt_scatternet_s
*vlan
;
686 char *endp
= strstr(opt
, ",vlan=");
687 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
690 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
693 vlan_id
= strtol(endp
+ 6, &endp
, 0);
695 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
700 vlan
= qemu_find_bt_vlan(vlan_id
);
703 fprintf(stderr
, "qemu: warning: adding a slave device to "
704 "an empty scatternet %i\n", vlan_id
);
706 if (!strcmp(devname
, "keyboard"))
707 return bt_keyboard_init(vlan
);
709 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
713 static int bt_parse(const char *opt
)
715 const char *endp
, *p
;
718 if (strstart(opt
, "hci", &endp
)) {
719 if (!*endp
|| *endp
== ',') {
721 if (!strstart(endp
, ",vlan=", 0))
724 return bt_hci_parse(opt
);
726 } else if (strstart(opt
, "vhci", &endp
)) {
727 if (!*endp
|| *endp
== ',') {
729 if (strstart(endp
, ",vlan=", &p
)) {
730 vlan
= strtol(p
, (char **) &endp
, 0);
732 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
736 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
745 } else if (strstart(opt
, "device:", &endp
))
746 return !bt_device_add(endp
);
748 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
752 /***********************************************************/
753 /* QEMU Block devices */
755 #define HD_ALIAS "index=%d,media=disk"
756 #define CDROM_ALIAS "index=2,media=cdrom"
757 #define FD_ALIAS "index=%d,if=floppy"
758 #define PFLASH_ALIAS "if=pflash"
759 #define MTD_ALIAS "if=mtd"
760 #define SD_ALIAS "index=0,if=sd"
762 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
769 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
772 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
774 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
775 __FUNCTION__
, optstr
);
779 qemu_opt_set(opts
, "file", file
);
783 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
787 /* seek interface, bus and unit */
789 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
790 if (dinfo
->type
== type
&&
799 DriveInfo
*drive_get_by_id(const char *id
)
803 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
804 if (strcmp(id
, dinfo
->id
))
811 int drive_get_max_bus(BlockInterfaceType type
)
817 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
818 if(dinfo
->type
== type
&&
819 dinfo
->bus
> max_bus
)
820 max_bus
= dinfo
->bus
;
825 const char *drive_get_serial(BlockDriverState
*bdrv
)
829 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
830 if (dinfo
->bdrv
== bdrv
)
831 return dinfo
->serial
;
837 BlockInterfaceErrorAction
drive_get_on_error(
838 BlockDriverState
*bdrv
, int is_read
)
842 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
843 if (dinfo
->bdrv
== bdrv
)
844 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
847 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
850 static void bdrv_format_print(void *opaque
, const char *name
)
852 fprintf(stderr
, " %s", name
);
855 void drive_uninit(DriveInfo
*dinfo
)
857 qemu_opts_del(dinfo
->opts
);
858 bdrv_delete(dinfo
->bdrv
);
859 QTAILQ_REMOVE(&drives
, dinfo
, next
);
863 static int parse_block_error_action(const char *buf
, int is_read
)
865 if (!strcmp(buf
, "ignore")) {
866 return BLOCK_ERR_IGNORE
;
867 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
868 return BLOCK_ERR_STOP_ENOSPC
;
869 } else if (!strcmp(buf
, "stop")) {
870 return BLOCK_ERR_STOP_ANY
;
871 } else if (!strcmp(buf
, "report")) {
872 return BLOCK_ERR_REPORT
;
874 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
875 buf
, is_read
? "read" : "write");
880 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
884 const char *file
= NULL
;
887 const char *mediastr
= "";
888 BlockInterfaceType type
;
889 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
891 int cyls
, heads
, secs
, translation
;
892 BlockDriver
*drv
= NULL
;
893 QEMUMachine
*machine
= opaque
;
900 int on_read_error
, on_write_error
;
907 translation
= BIOS_ATA_TRANSLATION_AUTO
;
910 if (machine
&& machine
->use_scsi
) {
912 max_devs
= MAX_SCSI_DEVS
;
913 pstrcpy(devname
, sizeof(devname
), "scsi");
916 max_devs
= MAX_IDE_DEVS
;
917 pstrcpy(devname
, sizeof(devname
), "ide");
921 /* extract parameters */
922 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
923 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
924 index
= qemu_opt_get_number(opts
, "index", -1);
926 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
927 heads
= qemu_opt_get_number(opts
, "heads", 0);
928 secs
= qemu_opt_get_number(opts
, "secs", 0);
930 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
931 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
933 file
= qemu_opt_get(opts
, "file");
934 serial
= qemu_opt_get(opts
, "serial");
936 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
937 pstrcpy(devname
, sizeof(devname
), buf
);
938 if (!strcmp(buf
, "ide")) {
940 max_devs
= MAX_IDE_DEVS
;
941 } else if (!strcmp(buf
, "scsi")) {
943 max_devs
= MAX_SCSI_DEVS
;
944 } else if (!strcmp(buf
, "floppy")) {
947 } else if (!strcmp(buf
, "pflash")) {
950 } else if (!strcmp(buf
, "mtd")) {
953 } else if (!strcmp(buf
, "sd")) {
956 } else if (!strcmp(buf
, "virtio")) {
959 } else if (!strcmp(buf
, "xen")) {
962 } else if (!strcmp(buf
, "none")) {
966 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
971 if (cyls
|| heads
|| secs
) {
972 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
973 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
976 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
977 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
980 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
981 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
986 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
989 "qemu: '%s' trans must be used with cyls,heads and secs\n",
993 if (!strcmp(buf
, "none"))
994 translation
= BIOS_ATA_TRANSLATION_NONE
;
995 else if (!strcmp(buf
, "lba"))
996 translation
= BIOS_ATA_TRANSLATION_LBA
;
997 else if (!strcmp(buf
, "auto"))
998 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1000 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1005 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1006 if (!strcmp(buf
, "disk")) {
1008 } else if (!strcmp(buf
, "cdrom")) {
1009 if (cyls
|| secs
|| heads
) {
1011 "qemu: '%s' invalid physical CHS format\n", buf
);
1014 media
= MEDIA_CDROM
;
1016 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1021 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1022 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1024 else if (!strcmp(buf
, "writethrough"))
1026 else if (!strcmp(buf
, "writeback"))
1029 fprintf(stderr
, "qemu: invalid cache option\n");
1034 #ifdef CONFIG_LINUX_AIO
1035 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1036 if (!strcmp(buf
, "threads"))
1038 else if (!strcmp(buf
, "native"))
1041 fprintf(stderr
, "qemu: invalid aio option\n");
1047 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1048 if (strcmp(buf
, "?") == 0) {
1049 fprintf(stderr
, "qemu: Supported formats:");
1050 bdrv_iterate_format(bdrv_format_print
, NULL
);
1051 fprintf(stderr
, "\n");
1054 drv
= bdrv_find_whitelisted_format(buf
);
1056 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1061 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1062 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1063 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1064 fprintf(stderr
, "werror is no supported by this format\n");
1068 on_write_error
= parse_block_error_action(buf
, 0);
1069 if (on_write_error
< 0) {
1074 on_read_error
= BLOCK_ERR_REPORT
;
1075 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1076 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1077 fprintf(stderr
, "rerror is no supported by this format\n");
1081 on_read_error
= parse_block_error_action(buf
, 1);
1082 if (on_read_error
< 0) {
1087 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1088 if (type
!= IF_VIRTIO
) {
1089 fprintf(stderr
, "addr is not supported\n");
1094 /* compute bus and unit according index */
1097 if (bus_id
!= 0 || unit_id
!= -1) {
1099 "qemu: index cannot be used with bus and unit\n");
1107 unit_id
= index
% max_devs
;
1108 bus_id
= index
/ max_devs
;
1112 /* if user doesn't specify a unit_id,
1113 * try to find the first free
1116 if (unit_id
== -1) {
1118 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1120 if (max_devs
&& unit_id
>= max_devs
) {
1121 unit_id
-= max_devs
;
1129 if (max_devs
&& unit_id
>= max_devs
) {
1130 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1131 unit_id
, max_devs
- 1);
1136 * ignore multiple definitions
1139 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1146 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1147 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1148 dinfo
->id
= qemu_strdup(buf
);
1150 /* no id supplied -> create one */
1151 dinfo
->id
= qemu_mallocz(32);
1152 if (type
== IF_IDE
|| type
== IF_SCSI
)
1153 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1155 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1156 devname
, bus_id
, mediastr
, unit_id
);
1158 snprintf(dinfo
->id
, 32, "%s%s%i",
1159 devname
, mediastr
, unit_id
);
1161 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1162 dinfo
->devaddr
= devaddr
;
1164 dinfo
->bus
= bus_id
;
1165 dinfo
->unit
= unit_id
;
1166 dinfo
->on_read_error
= on_read_error
;
1167 dinfo
->on_write_error
= on_write_error
;
1170 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1171 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1181 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1182 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1186 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1191 /* FIXME: This isn't really a floppy, but it's a reasonable
1194 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1200 /* add virtio block device */
1201 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1202 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1203 qemu_opt_set(opts
, "drive", dinfo
->id
);
1205 qemu_opt_set(opts
, "addr", devaddr
);
1216 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1217 cache
= 2; /* always use write-back with snapshot */
1219 if (cache
== 0) /* no caching */
1220 bdrv_flags
|= BDRV_O_NOCACHE
;
1221 else if (cache
== 2) /* write-back */
1222 bdrv_flags
|= BDRV_O_CACHE_WB
;
1225 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1227 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1231 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1232 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1237 * cdrom is read-only. Set it now, after above interface checking
1238 * since readonly attribute not explicitly required, so no error.
1240 if (media
== MEDIA_CDROM
) {
1243 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1245 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1246 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1247 file
, strerror(errno
));
1251 if (bdrv_key_required(dinfo
->bdrv
))
1257 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1259 QEMUMachine
*machine
= opaque
;
1260 int fatal_error
= 0;
1262 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1269 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1271 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1272 qemu_opt_set(opts
, "snapshot", "on");
1277 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1279 boot_set_handler
= func
;
1280 boot_set_opaque
= opaque
;
1283 int qemu_boot_set(const char *boot_devices
)
1285 if (!boot_set_handler
) {
1288 return boot_set_handler(boot_set_opaque
, boot_devices
);
1291 static int parse_bootdevices(char *devices
)
1293 /* We just do some generic consistency checks */
1297 for (p
= devices
; *p
!= '\0'; p
++) {
1298 /* Allowed boot devices are:
1299 * a-b: floppy disk drives
1300 * c-f: IDE disk drives
1301 * g-m: machine implementation dependant drives
1302 * n-p: network devices
1303 * It's up to each machine implementation to check if the given boot
1304 * devices match the actual hardware implementation and firmware
1307 if (*p
< 'a' || *p
> 'p') {
1308 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1311 if (bitmap
& (1 << (*p
- 'a'))) {
1312 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1315 bitmap
|= 1 << (*p
- 'a');
1320 static void restore_boot_devices(void *opaque
)
1322 char *standard_boot_devices
= opaque
;
1324 qemu_boot_set(standard_boot_devices
);
1326 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1327 qemu_free(standard_boot_devices
);
1330 static void numa_add(const char *optarg
)
1334 unsigned long long value
, endvalue
;
1337 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1338 if (!strcmp(option
, "node")) {
1339 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1340 nodenr
= nb_numa_nodes
;
1342 nodenr
= strtoull(option
, NULL
, 10);
1345 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1346 node_mem
[nodenr
] = 0;
1348 value
= strtoull(option
, &endptr
, 0);
1350 case 0: case 'M': case 'm':
1357 node_mem
[nodenr
] = value
;
1359 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1360 node_cpumask
[nodenr
] = 0;
1362 value
= strtoull(option
, &endptr
, 10);
1365 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1367 if (*endptr
== '-') {
1368 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1369 if (endvalue
>= 63) {
1372 "only 63 CPUs in NUMA mode supported.\n");
1374 value
= (2ULL << endvalue
) - (1ULL << value
);
1376 value
= 1ULL << value
;
1379 node_cpumask
[nodenr
] = value
;
1386 static void smp_parse(const char *optarg
)
1388 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1392 smp
= strtoul(optarg
, &endptr
, 10);
1393 if (endptr
!= optarg
) {
1394 if (*endptr
== ',') {
1398 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1399 sockets
= strtoull(option
, NULL
, 10);
1400 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1401 cores
= strtoull(option
, NULL
, 10);
1402 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1403 threads
= strtoull(option
, NULL
, 10);
1404 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1405 max_cpus
= strtoull(option
, NULL
, 10);
1407 /* compute missing values, prefer sockets over cores over threads */
1408 if (smp
== 0 || sockets
== 0) {
1409 sockets
= sockets
> 0 ? sockets
: 1;
1410 cores
= cores
> 0 ? cores
: 1;
1411 threads
= threads
> 0 ? threads
: 1;
1413 smp
= cores
* threads
* sockets
;
1417 threads
= threads
> 0 ? threads
: 1;
1418 cores
= smp
/ (sockets
* threads
);
1421 threads
= smp
/ (cores
* sockets
);
1426 smp_cores
= cores
> 0 ? cores
: 1;
1427 smp_threads
= threads
> 0 ? threads
: 1;
1429 max_cpus
= smp_cpus
;
1432 /***********************************************************/
1435 static int usb_device_add(const char *devname
, int is_hotplug
)
1438 USBDevice
*dev
= NULL
;
1443 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1444 dev
= usbdevice_create(devname
);
1448 /* the other ones */
1449 if (strstart(devname
, "host:", &p
)) {
1450 dev
= usb_host_device_open(p
);
1451 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1452 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1453 bt_new_hci(qemu_find_bt_vlan(0)));
1464 static int usb_device_del(const char *devname
)
1469 if (strstart(devname
, "host:", &p
))
1470 return usb_host_device_close(p
);
1475 p
= strchr(devname
, '.');
1478 bus_num
= strtoul(devname
, NULL
, 0);
1479 addr
= strtoul(p
+ 1, NULL
, 0);
1481 return usb_device_delete_addr(bus_num
, addr
);
1484 static int usb_parse(const char *cmdline
)
1487 r
= usb_device_add(cmdline
, 0);
1489 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1494 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1496 const char *devname
= qdict_get_str(qdict
, "devname");
1497 if (usb_device_add(devname
, 1) < 0) {
1498 error_report("could not add USB device '%s'", devname
);
1502 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1504 const char *devname
= qdict_get_str(qdict
, "devname");
1505 if (usb_device_del(devname
) < 0) {
1506 error_report("could not delete USB device '%s'", devname
);
1510 /***********************************************************/
1511 /* PCMCIA/Cardbus */
1513 static struct pcmcia_socket_entry_s
{
1514 PCMCIASocket
*socket
;
1515 struct pcmcia_socket_entry_s
*next
;
1516 } *pcmcia_sockets
= 0;
1518 void pcmcia_socket_register(PCMCIASocket
*socket
)
1520 struct pcmcia_socket_entry_s
*entry
;
1522 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1523 entry
->socket
= socket
;
1524 entry
->next
= pcmcia_sockets
;
1525 pcmcia_sockets
= entry
;
1528 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1530 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1532 ptr
= &pcmcia_sockets
;
1533 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1534 if (entry
->socket
== socket
) {
1540 void pcmcia_info(Monitor
*mon
)
1542 struct pcmcia_socket_entry_s
*iter
;
1544 if (!pcmcia_sockets
)
1545 monitor_printf(mon
, "No PCMCIA sockets\n");
1547 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1548 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1549 iter
->socket
->attached
? iter
->socket
->card_string
:
1553 /***********************************************************/
1556 typedef struct IOHandlerRecord
{
1558 IOCanReadHandler
*fd_read_poll
;
1560 IOHandler
*fd_write
;
1563 /* temporary data */
1565 QLIST_ENTRY(IOHandlerRecord
) next
;
1568 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1569 QLIST_HEAD_INITIALIZER(io_handlers
);
1572 /* XXX: fd_read_poll should be suppressed, but an API change is
1573 necessary in the character devices to suppress fd_can_read(). */
1574 int qemu_set_fd_handler2(int fd
,
1575 IOCanReadHandler
*fd_read_poll
,
1577 IOHandler
*fd_write
,
1580 IOHandlerRecord
*ioh
;
1582 if (!fd_read
&& !fd_write
) {
1583 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1584 if (ioh
->fd
== fd
) {
1590 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1594 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1595 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1598 ioh
->fd_read_poll
= fd_read_poll
;
1599 ioh
->fd_read
= fd_read
;
1600 ioh
->fd_write
= fd_write
;
1601 ioh
->opaque
= opaque
;
1607 int qemu_set_fd_handler(int fd
,
1609 IOHandler
*fd_write
,
1612 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1616 /***********************************************************/
1617 /* Polling handling */
1619 typedef struct PollingEntry
{
1622 struct PollingEntry
*next
;
1625 static PollingEntry
*first_polling_entry
;
1627 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1629 PollingEntry
**ppe
, *pe
;
1630 pe
= qemu_mallocz(sizeof(PollingEntry
));
1632 pe
->opaque
= opaque
;
1633 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1638 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1640 PollingEntry
**ppe
, *pe
;
1641 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1643 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1651 /***********************************************************/
1652 /* Wait objects support */
1653 typedef struct WaitObjects
{
1655 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1656 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1657 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1660 static WaitObjects wait_objects
= {0};
1662 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1664 WaitObjects
*w
= &wait_objects
;
1666 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1668 w
->events
[w
->num
] = handle
;
1669 w
->func
[w
->num
] = func
;
1670 w
->opaque
[w
->num
] = opaque
;
1675 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1678 WaitObjects
*w
= &wait_objects
;
1681 for (i
= 0; i
< w
->num
; i
++) {
1682 if (w
->events
[i
] == handle
)
1685 w
->events
[i
] = w
->events
[i
+ 1];
1686 w
->func
[i
] = w
->func
[i
+ 1];
1687 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1695 /***********************************************************/
1696 /* ram save/restore */
1698 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1699 #define RAM_SAVE_FLAG_COMPRESS 0x02
1700 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1701 #define RAM_SAVE_FLAG_PAGE 0x08
1702 #define RAM_SAVE_FLAG_EOS 0x10
1704 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1706 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1707 uint32_t *array
= (uint32_t *)page
;
1710 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1711 if (array
[i
] != val
)
1718 static int ram_save_block(QEMUFile
*f
)
1720 static ram_addr_t current_addr
= 0;
1721 ram_addr_t saved_addr
= current_addr
;
1722 ram_addr_t addr
= 0;
1725 while (addr
< last_ram_offset
) {
1726 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1729 cpu_physical_memory_reset_dirty(current_addr
,
1730 current_addr
+ TARGET_PAGE_SIZE
,
1731 MIGRATION_DIRTY_FLAG
);
1733 p
= qemu_get_ram_ptr(current_addr
);
1735 if (is_dup_page(p
, *p
)) {
1736 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1737 qemu_put_byte(f
, *p
);
1739 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1740 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1746 addr
+= TARGET_PAGE_SIZE
;
1747 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1753 static uint64_t bytes_transferred
;
1755 static ram_addr_t
ram_save_remaining(void)
1758 ram_addr_t count
= 0;
1760 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1761 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1768 uint64_t ram_bytes_remaining(void)
1770 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1773 uint64_t ram_bytes_transferred(void)
1775 return bytes_transferred
;
1778 uint64_t ram_bytes_total(void)
1780 return last_ram_offset
;
1783 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1786 uint64_t bytes_transferred_last
;
1788 uint64_t expected_time
= 0;
1791 cpu_physical_memory_set_dirty_tracking(0);
1795 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1796 qemu_file_set_error(f
);
1801 bytes_transferred
= 0;
1803 /* Make sure all dirty bits are set */
1804 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1805 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1806 cpu_physical_memory_set_dirty(addr
);
1809 /* Enable dirty memory tracking */
1810 cpu_physical_memory_set_dirty_tracking(1);
1812 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1815 bytes_transferred_last
= bytes_transferred
;
1816 bwidth
= qemu_get_clock_ns(rt_clock
);
1818 while (!qemu_file_rate_limit(f
)) {
1821 ret
= ram_save_block(f
);
1822 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1823 if (ret
== 0) /* no more blocks */
1827 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1828 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1830 /* if we haven't transferred anything this round, force expected_time to a
1831 * a very high value, but without crashing */
1835 /* try transferring iterative blocks of memory */
1837 /* flush all remaining blocks regardless of rate limiting */
1838 while (ram_save_block(f
) != 0) {
1839 bytes_transferred
+= TARGET_PAGE_SIZE
;
1841 cpu_physical_memory_set_dirty_tracking(0);
1844 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1846 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1848 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1851 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1856 if (version_id
!= 3)
1860 addr
= qemu_get_be64(f
);
1862 flags
= addr
& ~TARGET_PAGE_MASK
;
1863 addr
&= TARGET_PAGE_MASK
;
1865 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1866 if (addr
!= last_ram_offset
)
1870 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1871 uint8_t ch
= qemu_get_byte(f
);
1872 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1875 (!kvm_enabled() || kvm_has_sync_mmu())) {
1876 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1879 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1880 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1882 if (qemu_file_has_error(f
)) {
1885 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1890 void qemu_service_io(void)
1892 qemu_notify_event();
1895 /***********************************************************/
1896 /* machine registration */
1898 static QEMUMachine
*first_machine
= NULL
;
1899 QEMUMachine
*current_machine
= NULL
;
1901 int qemu_register_machine(QEMUMachine
*m
)
1904 pm
= &first_machine
;
1912 static QEMUMachine
*find_machine(const char *name
)
1916 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1917 if (!strcmp(m
->name
, name
))
1919 if (m
->alias
&& !strcmp(m
->alias
, name
))
1925 static QEMUMachine
*find_default_machine(void)
1929 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1930 if (m
->is_default
) {
1937 /***********************************************************/
1938 /* main execution loop */
1940 static void gui_update(void *opaque
)
1942 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1943 DisplayState
*ds
= opaque
;
1944 DisplayChangeListener
*dcl
= ds
->listeners
;
1946 qemu_flush_coalesced_mmio_buffer();
1949 while (dcl
!= NULL
) {
1950 if (dcl
->gui_timer_interval
&&
1951 dcl
->gui_timer_interval
< interval
)
1952 interval
= dcl
->gui_timer_interval
;
1955 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1958 static void nographic_update(void *opaque
)
1960 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1962 qemu_flush_coalesced_mmio_buffer();
1963 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1966 void cpu_synchronize_all_states(void)
1970 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1971 cpu_synchronize_state(cpu
);
1975 void cpu_synchronize_all_post_reset(void)
1979 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1980 cpu_synchronize_post_reset(cpu
);
1984 void cpu_synchronize_all_post_init(void)
1988 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1989 cpu_synchronize_post_init(cpu
);
1993 struct vm_change_state_entry
{
1994 VMChangeStateHandler
*cb
;
1996 QLIST_ENTRY (vm_change_state_entry
) entries
;
1999 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2001 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2004 VMChangeStateEntry
*e
;
2006 e
= qemu_mallocz(sizeof (*e
));
2010 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2014 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2016 QLIST_REMOVE (e
, entries
);
2020 static void vm_state_notify(int running
, int reason
)
2022 VMChangeStateEntry
*e
;
2024 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2025 e
->cb(e
->opaque
, running
, reason
);
2029 static void resume_all_vcpus(void);
2030 static void pause_all_vcpus(void);
2037 vm_state_notify(1, 0);
2042 /* reset/shutdown handler */
2044 typedef struct QEMUResetEntry
{
2045 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2046 QEMUResetHandler
*func
;
2050 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2051 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2052 static int reset_requested
;
2053 static int shutdown_requested
;
2054 static int powerdown_requested
;
2055 static int debug_requested
;
2056 static int vmstop_requested
;
2058 int qemu_shutdown_requested(void)
2060 int r
= shutdown_requested
;
2061 shutdown_requested
= 0;
2065 int qemu_reset_requested(void)
2067 int r
= reset_requested
;
2068 reset_requested
= 0;
2072 int qemu_powerdown_requested(void)
2074 int r
= powerdown_requested
;
2075 powerdown_requested
= 0;
2079 static int qemu_debug_requested(void)
2081 int r
= debug_requested
;
2082 debug_requested
= 0;
2086 static int qemu_vmstop_requested(void)
2088 int r
= vmstop_requested
;
2089 vmstop_requested
= 0;
2093 static void do_vm_stop(int reason
)
2096 cpu_disable_ticks();
2099 vm_state_notify(0, reason
);
2100 monitor_protocol_event(QEVENT_STOP
, NULL
);
2104 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2106 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2109 re
->opaque
= opaque
;
2110 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2113 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2117 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2118 if (re
->func
== func
&& re
->opaque
== opaque
) {
2119 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2126 void qemu_system_reset(void)
2128 QEMUResetEntry
*re
, *nre
;
2130 /* reset all devices */
2131 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2132 re
->func(re
->opaque
);
2134 monitor_protocol_event(QEVENT_RESET
, NULL
);
2135 cpu_synchronize_all_post_reset();
2138 void qemu_system_reset_request(void)
2141 shutdown_requested
= 1;
2143 reset_requested
= 1;
2145 qemu_notify_event();
2148 void qemu_system_shutdown_request(void)
2150 shutdown_requested
= 1;
2151 qemu_notify_event();
2154 void qemu_system_powerdown_request(void)
2156 powerdown_requested
= 1;
2157 qemu_notify_event();
2160 static int cpu_can_run(CPUState
*env
)
2171 static int cpu_has_work(CPUState
*env
)
2179 if (qemu_cpu_has_work(env
))
2184 static int tcg_has_work(void)
2188 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2189 if (cpu_has_work(env
))
2195 static int io_thread_fd
= -1;
2197 static void qemu_event_increment(void)
2199 /* Write 8 bytes to be compatible with eventfd. */
2200 static uint64_t val
= 1;
2203 if (io_thread_fd
== -1)
2207 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2208 } while (ret
< 0 && errno
== EINTR
);
2210 /* EAGAIN is fine, a read must be pending. */
2211 if (ret
< 0 && errno
!= EAGAIN
) {
2212 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2218 static void qemu_event_read(void *opaque
)
2220 int fd
= (unsigned long)opaque
;
2224 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2226 len
= read(fd
, buffer
, sizeof(buffer
));
2227 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2230 static int qemu_event_init(void)
2235 err
= qemu_eventfd(fds
);
2239 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2243 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2247 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2248 (void *)(unsigned long)fds
[0]);
2250 io_thread_fd
= fds
[1];
2259 HANDLE qemu_event_handle
;
2261 static void dummy_event_handler(void *opaque
)
2265 static int qemu_event_init(void)
2267 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2268 if (!qemu_event_handle
) {
2269 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2272 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2276 static void qemu_event_increment(void)
2278 if (!SetEvent(qemu_event_handle
)) {
2279 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2286 #ifndef CONFIG_IOTHREAD
2287 static int qemu_init_main_loop(void)
2289 return qemu_event_init();
2292 void qemu_init_vcpu(void *_env
)
2294 CPUState
*env
= _env
;
2296 env
->nr_cores
= smp_cores
;
2297 env
->nr_threads
= smp_threads
;
2303 int qemu_cpu_self(void *env
)
2308 static void resume_all_vcpus(void)
2312 static void pause_all_vcpus(void)
2316 void qemu_cpu_kick(void *env
)
2321 void qemu_notify_event(void)
2323 CPUState
*env
= cpu_single_env
;
2325 qemu_event_increment ();
2329 if (next_cpu
&& env
!= next_cpu
) {
2334 void qemu_mutex_lock_iothread(void) {}
2335 void qemu_mutex_unlock_iothread(void) {}
2337 void vm_stop(int reason
)
2342 #else /* CONFIG_IOTHREAD */
2344 #include "qemu-thread.h"
2346 QemuMutex qemu_global_mutex
;
2347 static QemuMutex qemu_fair_mutex
;
2349 static QemuThread io_thread
;
2351 static QemuThread
*tcg_cpu_thread
;
2352 static QemuCond
*tcg_halt_cond
;
2354 static int qemu_system_ready
;
2356 static QemuCond qemu_cpu_cond
;
2358 static QemuCond qemu_system_cond
;
2359 static QemuCond qemu_pause_cond
;
2361 static void tcg_block_io_signals(void);
2362 static void kvm_block_io_signals(CPUState
*env
);
2363 static void unblock_io_signals(void);
2365 static int qemu_init_main_loop(void)
2369 ret
= qemu_event_init();
2373 qemu_cond_init(&qemu_pause_cond
);
2374 qemu_mutex_init(&qemu_fair_mutex
);
2375 qemu_mutex_init(&qemu_global_mutex
);
2376 qemu_mutex_lock(&qemu_global_mutex
);
2378 unblock_io_signals();
2379 qemu_thread_self(&io_thread
);
2384 static void qemu_wait_io_event_common(CPUState
*env
)
2389 qemu_cond_signal(&qemu_pause_cond
);
2393 static void qemu_wait_io_event(CPUState
*env
)
2395 while (!tcg_has_work())
2396 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2398 qemu_mutex_unlock(&qemu_global_mutex
);
2401 * Users of qemu_global_mutex can be starved, having no chance
2402 * to acquire it since this path will get to it first.
2403 * So use another lock to provide fairness.
2405 qemu_mutex_lock(&qemu_fair_mutex
);
2406 qemu_mutex_unlock(&qemu_fair_mutex
);
2408 qemu_mutex_lock(&qemu_global_mutex
);
2409 qemu_wait_io_event_common(env
);
2412 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2419 ts
.tv_sec
= timeout
/ 1000;
2420 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2422 sigemptyset(&waitset
);
2423 sigaddset(&waitset
, SIG_IPI
);
2425 qemu_mutex_unlock(&qemu_global_mutex
);
2426 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2428 qemu_mutex_lock(&qemu_global_mutex
);
2430 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2431 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2436 static void qemu_kvm_wait_io_event(CPUState
*env
)
2438 while (!cpu_has_work(env
))
2439 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2441 qemu_kvm_eat_signal(env
, 0);
2442 qemu_wait_io_event_common(env
);
2445 static int qemu_cpu_exec(CPUState
*env
);
2447 static void *kvm_cpu_thread_fn(void *arg
)
2449 CPUState
*env
= arg
;
2451 qemu_thread_self(env
->thread
);
2455 kvm_block_io_signals(env
);
2457 /* signal CPU creation */
2458 qemu_mutex_lock(&qemu_global_mutex
);
2460 qemu_cond_signal(&qemu_cpu_cond
);
2462 /* and wait for machine initialization */
2463 while (!qemu_system_ready
)
2464 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2467 if (cpu_can_run(env
))
2469 qemu_kvm_wait_io_event(env
);
2475 static bool tcg_cpu_exec(void);
2477 static void *tcg_cpu_thread_fn(void *arg
)
2479 CPUState
*env
= arg
;
2481 tcg_block_io_signals();
2482 qemu_thread_self(env
->thread
);
2484 /* signal CPU creation */
2485 qemu_mutex_lock(&qemu_global_mutex
);
2486 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2488 qemu_cond_signal(&qemu_cpu_cond
);
2490 /* and wait for machine initialization */
2491 while (!qemu_system_ready
)
2492 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2496 qemu_wait_io_event(cur_cpu
);
2502 void qemu_cpu_kick(void *_env
)
2504 CPUState
*env
= _env
;
2505 qemu_cond_broadcast(env
->halt_cond
);
2507 qemu_thread_signal(env
->thread
, SIG_IPI
);
2510 int qemu_cpu_self(void *_env
)
2512 CPUState
*env
= _env
;
2515 qemu_thread_self(&this);
2517 return qemu_thread_equal(&this, env
->thread
);
2520 static void cpu_signal(int sig
)
2523 cpu_exit(cpu_single_env
);
2526 static void tcg_block_io_signals(void)
2529 struct sigaction sigact
;
2532 sigaddset(&set
, SIGUSR2
);
2533 sigaddset(&set
, SIGIO
);
2534 sigaddset(&set
, SIGALRM
);
2535 sigaddset(&set
, SIGCHLD
);
2536 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2539 sigaddset(&set
, SIG_IPI
);
2540 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2542 memset(&sigact
, 0, sizeof(sigact
));
2543 sigact
.sa_handler
= cpu_signal
;
2544 sigaction(SIG_IPI
, &sigact
, NULL
);
2547 static void dummy_signal(int sig
)
2551 static void kvm_block_io_signals(CPUState
*env
)
2555 struct sigaction sigact
;
2558 sigaddset(&set
, SIGUSR2
);
2559 sigaddset(&set
, SIGIO
);
2560 sigaddset(&set
, SIGALRM
);
2561 sigaddset(&set
, SIGCHLD
);
2562 sigaddset(&set
, SIG_IPI
);
2563 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2565 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2566 sigdelset(&set
, SIG_IPI
);
2568 memset(&sigact
, 0, sizeof(sigact
));
2569 sigact
.sa_handler
= dummy_signal
;
2570 sigaction(SIG_IPI
, &sigact
, NULL
);
2572 r
= kvm_set_signal_mask(env
, &set
);
2574 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2579 static void unblock_io_signals(void)
2584 sigaddset(&set
, SIGUSR2
);
2585 sigaddset(&set
, SIGIO
);
2586 sigaddset(&set
, SIGALRM
);
2587 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2590 sigaddset(&set
, SIG_IPI
);
2591 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2594 static void qemu_signal_lock(unsigned int msecs
)
2596 qemu_mutex_lock(&qemu_fair_mutex
);
2598 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2599 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2600 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2603 qemu_mutex_unlock(&qemu_fair_mutex
);
2606 void qemu_mutex_lock_iothread(void)
2608 if (kvm_enabled()) {
2609 qemu_mutex_lock(&qemu_fair_mutex
);
2610 qemu_mutex_lock(&qemu_global_mutex
);
2611 qemu_mutex_unlock(&qemu_fair_mutex
);
2613 qemu_signal_lock(100);
2616 void qemu_mutex_unlock_iothread(void)
2618 qemu_mutex_unlock(&qemu_global_mutex
);
2621 static int all_vcpus_paused(void)
2623 CPUState
*penv
= first_cpu
;
2628 penv
= (CPUState
*)penv
->next_cpu
;
2634 static void pause_all_vcpus(void)
2636 CPUState
*penv
= first_cpu
;
2640 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2641 qemu_cpu_kick(penv
);
2642 penv
= (CPUState
*)penv
->next_cpu
;
2645 while (!all_vcpus_paused()) {
2646 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2649 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2650 penv
= (CPUState
*)penv
->next_cpu
;
2655 static void resume_all_vcpus(void)
2657 CPUState
*penv
= first_cpu
;
2662 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2663 qemu_cpu_kick(penv
);
2664 penv
= (CPUState
*)penv
->next_cpu
;
2668 static void tcg_init_vcpu(void *_env
)
2670 CPUState
*env
= _env
;
2671 /* share a single thread for all cpus with TCG */
2672 if (!tcg_cpu_thread
) {
2673 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2674 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2675 qemu_cond_init(env
->halt_cond
);
2676 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2677 while (env
->created
== 0)
2678 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2679 tcg_cpu_thread
= env
->thread
;
2680 tcg_halt_cond
= env
->halt_cond
;
2682 env
->thread
= tcg_cpu_thread
;
2683 env
->halt_cond
= tcg_halt_cond
;
2687 static void kvm_start_vcpu(CPUState
*env
)
2689 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2690 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2691 qemu_cond_init(env
->halt_cond
);
2692 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2693 while (env
->created
== 0)
2694 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2697 void qemu_init_vcpu(void *_env
)
2699 CPUState
*env
= _env
;
2701 env
->nr_cores
= smp_cores
;
2702 env
->nr_threads
= smp_threads
;
2704 kvm_start_vcpu(env
);
2709 void qemu_notify_event(void)
2711 qemu_event_increment();
2714 static void qemu_system_vmstop_request(int reason
)
2716 vmstop_requested
= reason
;
2717 qemu_notify_event();
2720 void vm_stop(int reason
)
2723 qemu_thread_self(&me
);
2725 if (!qemu_thread_equal(&me
, &io_thread
)) {
2726 qemu_system_vmstop_request(reason
);
2728 * FIXME: should not return to device code in case
2729 * vm_stop() has been requested.
2731 if (cpu_single_env
) {
2732 cpu_exit(cpu_single_env
);
2733 cpu_single_env
->stop
= 1;
2744 static void host_main_loop_wait(int *timeout
)
2750 /* XXX: need to suppress polling by better using win32 events */
2752 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2753 ret
|= pe
->func(pe
->opaque
);
2757 WaitObjects
*w
= &wait_objects
;
2759 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2760 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2761 if (w
->func
[ret
- WAIT_OBJECT_0
])
2762 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2764 /* Check for additional signaled events */
2765 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2767 /* Check if event is signaled */
2768 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2769 if(ret2
== WAIT_OBJECT_0
) {
2771 w
->func
[i
](w
->opaque
[i
]);
2772 } else if (ret2
== WAIT_TIMEOUT
) {
2774 err
= GetLastError();
2775 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2778 } else if (ret
== WAIT_TIMEOUT
) {
2780 err
= GetLastError();
2781 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2788 static void host_main_loop_wait(int *timeout
)
2793 void main_loop_wait(int nonblocking
)
2795 IOHandlerRecord
*ioh
;
2796 fd_set rfds
, wfds
, xfds
;
2804 timeout
= qemu_calculate_timeout();
2805 qemu_bh_update_timeout(&timeout
);
2808 host_main_loop_wait(&timeout
);
2810 /* poll any events */
2811 /* XXX: separate device handlers from system ones */
2816 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2820 (!ioh
->fd_read_poll
||
2821 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2822 FD_SET(ioh
->fd
, &rfds
);
2826 if (ioh
->fd_write
) {
2827 FD_SET(ioh
->fd
, &wfds
);
2833 tv
.tv_sec
= timeout
/ 1000;
2834 tv
.tv_usec
= (timeout
% 1000) * 1000;
2836 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2838 qemu_mutex_unlock_iothread();
2839 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2840 qemu_mutex_lock_iothread();
2842 IOHandlerRecord
*pioh
;
2844 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2846 QLIST_REMOVE(ioh
, next
);
2850 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2851 ioh
->fd_read(ioh
->opaque
);
2853 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2854 ioh
->fd_write(ioh
->opaque
);
2859 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2861 qemu_run_all_timers();
2863 /* Check bottom-halves last in case any of the earlier events triggered
2869 static int qemu_cpu_exec(CPUState
*env
)
2872 #ifdef CONFIG_PROFILER
2876 #ifdef CONFIG_PROFILER
2877 ti
= profile_getclock();
2882 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2883 env
->icount_decr
.u16
.low
= 0;
2884 env
->icount_extra
= 0;
2885 count
= qemu_icount_round (qemu_next_deadline());
2886 qemu_icount
+= count
;
2887 decr
= (count
> 0xffff) ? 0xffff : count
;
2889 env
->icount_decr
.u16
.low
= decr
;
2890 env
->icount_extra
= count
;
2892 ret
= cpu_exec(env
);
2893 #ifdef CONFIG_PROFILER
2894 qemu_time
+= profile_getclock() - ti
;
2897 /* Fold pending instructions back into the
2898 instruction counter, and clear the interrupt flag. */
2899 qemu_icount
-= (env
->icount_decr
.u16
.low
2900 + env
->icount_extra
);
2901 env
->icount_decr
.u32
= 0;
2902 env
->icount_extra
= 0;
2907 static bool tcg_cpu_exec(void)
2911 if (next_cpu
== NULL
)
2912 next_cpu
= first_cpu
;
2913 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2914 CPUState
*env
= cur_cpu
= next_cpu
;
2916 qemu_clock_enable(vm_clock
,
2917 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2919 if (qemu_alarm_pending())
2921 if (cpu_can_run(env
))
2922 ret
= qemu_cpu_exec(env
);
2926 if (ret
== EXCP_DEBUG
) {
2927 gdb_set_stop_cpu(env
);
2928 debug_requested
= 1;
2932 return tcg_has_work();
2935 static int vm_can_run(void)
2937 if (powerdown_requested
)
2939 if (reset_requested
)
2941 if (shutdown_requested
)
2943 if (debug_requested
)
2948 qemu_irq qemu_system_powerdown
;
2950 static void main_loop(void)
2954 #ifdef CONFIG_IOTHREAD
2955 qemu_system_ready
= 1;
2956 qemu_cond_broadcast(&qemu_system_cond
);
2961 bool nonblocking
= false;
2962 #ifdef CONFIG_PROFILER
2965 #ifndef CONFIG_IOTHREAD
2966 nonblocking
= tcg_cpu_exec();
2968 #ifdef CONFIG_PROFILER
2969 ti
= profile_getclock();
2971 main_loop_wait(nonblocking
);
2972 #ifdef CONFIG_PROFILER
2973 dev_time
+= profile_getclock() - ti
;
2975 } while (vm_can_run());
2977 if (qemu_debug_requested()) {
2978 vm_stop(EXCP_DEBUG
);
2980 if (qemu_shutdown_requested()) {
2981 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2988 if (qemu_reset_requested()) {
2990 qemu_system_reset();
2993 if (qemu_powerdown_requested()) {
2994 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2995 qemu_irq_raise(qemu_system_powerdown
);
2997 if ((r
= qemu_vmstop_requested())) {
3004 static void version(void)
3006 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3009 static void help(int exitcode
)
3011 const char *options_help
=
3012 #define DEF(option, opt_arg, opt_enum, opt_help) \
3014 #define DEFHEADING(text) stringify(text) "\n"
3015 #include "qemu-options.h"
3021 printf("usage: %s [options] [disk_image]\n"
3023 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3026 "During emulation, the following keys are useful:\n"
3027 "ctrl-alt-f toggle full screen\n"
3028 "ctrl-alt-n switch to virtual console 'n'\n"
3029 "ctrl-alt toggle mouse and keyboard grab\n"
3031 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3037 #define HAS_ARG 0x0001
3040 #define DEF(option, opt_arg, opt_enum, opt_help) \
3042 #define DEFHEADING(text)
3043 #include "qemu-options.h"
3049 typedef struct QEMUOption
{
3055 static const QEMUOption qemu_options
[] = {
3056 { "h", 0, QEMU_OPTION_h
},
3057 #define DEF(option, opt_arg, opt_enum, opt_help) \
3058 { option, opt_arg, opt_enum },
3059 #define DEFHEADING(text)
3060 #include "qemu-options.h"
3068 struct soundhw soundhw
[] = {
3069 #ifdef HAS_AUDIO_CHOICE
3070 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3076 { .init_isa
= pcspk_audio_init
}
3083 "Creative Sound Blaster 16",
3086 { .init_isa
= SB16_init
}
3090 #ifdef CONFIG_CS4231A
3096 { .init_isa
= cs4231a_init
}
3104 "Yamaha YMF262 (OPL3)",
3106 "Yamaha YM3812 (OPL2)",
3110 { .init_isa
= Adlib_init
}
3117 "Gravis Ultrasound GF1",
3120 { .init_isa
= GUS_init
}
3127 "Intel 82801AA AC97 Audio",
3130 { .init_pci
= ac97_init
}
3134 #ifdef CONFIG_ES1370
3137 "ENSONIQ AudioPCI ES1370",
3140 { .init_pci
= es1370_init
}
3144 #endif /* HAS_AUDIO_CHOICE */
3146 { NULL
, NULL
, 0, 0, { NULL
} }
3149 static void select_soundhw (const char *optarg
)
3153 if (*optarg
== '?') {
3156 printf ("Valid sound card names (comma separated):\n");
3157 for (c
= soundhw
; c
->name
; ++c
) {
3158 printf ("%-11s %s\n", c
->name
, c
->descr
);
3160 printf ("\n-soundhw all will enable all of the above\n");
3161 exit (*optarg
!= '?');
3169 if (!strcmp (optarg
, "all")) {
3170 for (c
= soundhw
; c
->name
; ++c
) {
3178 e
= strchr (p
, ',');
3179 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3181 for (c
= soundhw
; c
->name
; ++c
) {
3182 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3191 "Unknown sound card name (too big to show)\n");
3194 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3199 p
+= l
+ (e
!= NULL
);
3203 goto show_valid_cards
;
3208 static void select_vgahw (const char *p
)
3213 vga_interface_type
= VGA_NONE
;
3214 if (strstart(p
, "std", &opts
)) {
3215 vga_interface_type
= VGA_STD
;
3216 } else if (strstart(p
, "cirrus", &opts
)) {
3217 vga_interface_type
= VGA_CIRRUS
;
3218 } else if (strstart(p
, "vmware", &opts
)) {
3219 vga_interface_type
= VGA_VMWARE
;
3220 } else if (strstart(p
, "xenfb", &opts
)) {
3221 vga_interface_type
= VGA_XENFB
;
3222 } else if (!strstart(p
, "none", &opts
)) {
3224 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3228 const char *nextopt
;
3230 if (strstart(opts
, ",retrace=", &nextopt
)) {
3232 if (strstart(opts
, "dumb", &nextopt
))
3233 vga_retrace_method
= VGA_RETRACE_DUMB
;
3234 else if (strstart(opts
, "precise", &nextopt
))
3235 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3236 else goto invalid_vga
;
3237 } else goto invalid_vga
;
3243 static int balloon_parse(const char *arg
)
3247 if (strcmp(arg
, "none") == 0) {
3251 if (!strncmp(arg
, "virtio", 6)) {
3252 if (arg
[6] == ',') {
3253 /* have params -> parse them */
3254 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3258 /* create empty opts */
3259 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3261 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3270 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3272 exit(STATUS_CONTROL_C_EXIT
);
3277 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3281 if(strlen(str
) != 36)
3284 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3285 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3286 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3292 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3300 static void termsig_handler(int signal
)
3302 qemu_system_shutdown_request();
3305 static void sigchld_handler(int signal
)
3307 waitpid(-1, NULL
, WNOHANG
);
3310 static void sighandler_setup(void)
3312 struct sigaction act
;
3314 memset(&act
, 0, sizeof(act
));
3315 act
.sa_handler
= termsig_handler
;
3316 sigaction(SIGINT
, &act
, NULL
);
3317 sigaction(SIGHUP
, &act
, NULL
);
3318 sigaction(SIGTERM
, &act
, NULL
);
3320 act
.sa_handler
= sigchld_handler
;
3321 act
.sa_flags
= SA_NOCLDSTOP
;
3322 sigaction(SIGCHLD
, &act
, NULL
);
3328 /* Look for support files in the same directory as the executable. */
3329 static char *find_datadir(const char *argv0
)
3335 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3342 while (p
!= buf
&& *p
!= '\\')
3345 if (access(buf
, R_OK
) == 0) {
3346 return qemu_strdup(buf
);
3352 /* Find a likely location for support files using the location of the binary.
3353 For installed binaries this will be "$bindir/../share/qemu". When
3354 running from the build tree this will be "$bindir/../pc-bios". */
3355 #define SHARE_SUFFIX "/share/qemu"
3356 #define BUILD_SUFFIX "/pc-bios"
3357 static char *find_datadir(const char *argv0
)
3365 #if defined(__linux__)
3368 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3374 #elif defined(__FreeBSD__)
3377 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3384 /* If we don't have any way of figuring out the actual executable
3385 location then try argv[0]. */
3387 p
= realpath(argv0
, buf
);
3395 max_len
= strlen(dir
) +
3396 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3397 res
= qemu_mallocz(max_len
);
3398 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3399 if (access(res
, R_OK
)) {
3400 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3401 if (access(res
, R_OK
)) {
3413 char *qemu_find_file(int type
, const char *name
)
3419 /* If name contains path separators then try it as a straight path. */
3420 if ((strchr(name
, '/') || strchr(name
, '\\'))
3421 && access(name
, R_OK
) == 0) {
3422 return qemu_strdup(name
);
3425 case QEMU_FILE_TYPE_BIOS
:
3428 case QEMU_FILE_TYPE_KEYMAP
:
3429 subdir
= "keymaps/";
3434 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3435 buf
= qemu_mallocz(len
);
3436 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3437 if (access(buf
, R_OK
)) {
3444 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3446 return qdev_device_help(opts
);
3449 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3453 dev
= qdev_device_add(opts
);
3459 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3461 CharDriverState
*chr
;
3463 chr
= qemu_chr_open_opts(opts
, NULL
);
3469 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3471 CharDriverState
*chr
;
3472 const char *chardev
;
3476 mode
= qemu_opt_get(opts
, "mode");
3480 if (strcmp(mode
, "readline") == 0) {
3481 flags
= MONITOR_USE_READLINE
;
3482 } else if (strcmp(mode
, "control") == 0) {
3483 flags
= MONITOR_USE_CONTROL
;
3485 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3489 if (qemu_opt_get_bool(opts
, "default", 0))
3490 flags
|= MONITOR_IS_DEFAULT
;
3492 chardev
= qemu_opt_get(opts
, "chardev");
3493 chr
= qemu_chr_find(chardev
);
3495 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3499 monitor_init(chr
, flags
);
3503 static void monitor_parse(const char *optarg
, const char *mode
)
3505 static int monitor_device_index
= 0;
3511 if (strstart(optarg
, "chardev:", &p
)) {
3512 snprintf(label
, sizeof(label
), "%s", p
);
3514 if (monitor_device_index
) {
3515 snprintf(label
, sizeof(label
), "monitor%d",
3516 monitor_device_index
);
3518 snprintf(label
, sizeof(label
), "monitor");
3521 opts
= qemu_chr_parse_compat(label
, optarg
);
3523 fprintf(stderr
, "parse error: %s\n", optarg
);
3528 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3530 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3533 qemu_opt_set(opts
, "mode", mode
);
3534 qemu_opt_set(opts
, "chardev", label
);
3536 qemu_opt_set(opts
, "default", "on");
3537 monitor_device_index
++;
3540 struct device_config
{
3542 DEV_USB
, /* -usbdevice */
3544 DEV_SERIAL
, /* -serial */
3545 DEV_PARALLEL
, /* -parallel */
3546 DEV_VIRTCON
, /* -virtioconsole */
3547 DEV_DEBUGCON
, /* -debugcon */
3549 const char *cmdline
;
3550 QTAILQ_ENTRY(device_config
) next
;
3552 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3554 static void add_device_config(int type
, const char *cmdline
)
3556 struct device_config
*conf
;
3558 conf
= qemu_mallocz(sizeof(*conf
));
3560 conf
->cmdline
= cmdline
;
3561 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3564 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3566 struct device_config
*conf
;
3569 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3570 if (conf
->type
!= type
)
3572 rc
= func(conf
->cmdline
);
3579 static int serial_parse(const char *devname
)
3581 static int index
= 0;
3584 if (strcmp(devname
, "none") == 0)
3586 if (index
== MAX_SERIAL_PORTS
) {
3587 fprintf(stderr
, "qemu: too many serial ports\n");
3590 snprintf(label
, sizeof(label
), "serial%d", index
);
3591 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3592 if (!serial_hds
[index
]) {
3593 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3594 devname
, strerror(errno
));
3601 static int parallel_parse(const char *devname
)
3603 static int index
= 0;
3606 if (strcmp(devname
, "none") == 0)
3608 if (index
== MAX_PARALLEL_PORTS
) {
3609 fprintf(stderr
, "qemu: too many parallel ports\n");
3612 snprintf(label
, sizeof(label
), "parallel%d", index
);
3613 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3614 if (!parallel_hds
[index
]) {
3615 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3616 devname
, strerror(errno
));
3623 static int virtcon_parse(const char *devname
)
3625 static int index
= 0;
3627 QemuOpts
*bus_opts
, *dev_opts
;
3629 if (strcmp(devname
, "none") == 0)
3631 if (index
== MAX_VIRTIO_CONSOLES
) {
3632 fprintf(stderr
, "qemu: too many virtio consoles\n");
3636 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3637 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3639 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3640 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3642 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3643 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3644 if (!virtcon_hds
[index
]) {
3645 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3646 devname
, strerror(errno
));
3649 qemu_opt_set(dev_opts
, "chardev", label
);
3655 static int debugcon_parse(const char *devname
)
3659 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3662 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3664 fprintf(stderr
, "qemu: already have a debugcon device\n");
3667 qemu_opt_set(opts
, "driver", "isa-debugcon");
3668 qemu_opt_set(opts
, "chardev", "debugcon");
3672 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3673 const char **poptarg
, int *poptind
)
3675 const QEMUOption
*popt
;
3676 int optind
= *poptind
;
3677 char *r
= argv
[optind
];
3680 loc_set_cmdline(argv
, optind
, 1);
3682 /* Treat --foo the same as -foo. */
3685 popt
= qemu_options
;
3688 error_report("invalid option");
3691 if (!strcmp(popt
->name
, r
+ 1))
3695 if (popt
->flags
& HAS_ARG
) {
3696 if (optind
>= argc
) {
3697 error_report("requires an argument");
3700 optarg
= argv
[optind
++];
3701 loc_set_cmdline(argv
, optind
- 2, 2);
3712 static void qmp_add_default(void)
3716 static uint8_t null_uuid
[16];
3720 home
= getenv("HOME");
3725 if (memcmp(qemu_uuid
, null_uuid
, sizeof(null_uuid
)) == 0) {
3726 #if defined(CONFIG_UUID)
3727 uuid_generate(uuid
);
3732 memcpy(uuid
, qemu_uuid
, sizeof(qemu_uuid
));
3735 snprintf(buffer
, sizeof(buffer
), "%s/.qemu", home
);
3737 ret
= mkdir(buffer
);
3739 ret
= mkdir(buffer
, 0755);
3741 if (ret
== -1 && errno
!= EEXIST
) {
3742 fprintf(stderr
, "could not open default QMP port\n");
3746 snprintf(buffer
, sizeof(buffer
), "%s/.qemu/qmp", home
);
3748 ret
= mkdir(buffer
);
3750 ret
= mkdir(buffer
, 0755);
3752 if (ret
== -1 && errno
!= EEXIST
) {
3753 fprintf(stderr
, "could not open default QMP port\n");
3757 snprintf(buffer
, sizeof(buffer
),
3758 "unix:%s/.qemu/qmp/" UUID_FMT
".sock,server,nowait",
3760 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
3761 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
3762 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
3763 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
3765 monitor_parse(buffer
, "control");
3768 int main(int argc
, char **argv
, char **envp
)
3770 const char *gdbstub_dev
= NULL
;
3771 uint32_t boot_devices_bitmap
= 0;
3773 int snapshot
, linux_boot
, net_boot
;
3774 const char *icount_option
= NULL
;
3775 const char *initrd_filename
;
3776 const char *kernel_filename
, *kernel_cmdline
;
3777 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3779 DisplayChangeListener
*dcl
;
3780 int cyls
, heads
, secs
, translation
;
3781 QemuOpts
*hda_opts
= NULL
, *opts
;
3784 const char *loadvm
= NULL
;
3785 QEMUMachine
*machine
;
3786 const char *cpu_model
;
3791 const char *pid_file
= NULL
;
3792 const char *incoming
= NULL
;
3795 struct passwd
*pwd
= NULL
;
3796 const char *chroot_dir
= NULL
;
3797 const char *run_as
= NULL
;
3800 int show_vnc_port
= 0;
3803 error_set_progname(argv
[0]);
3807 qemu_cache_utils_init(envp
);
3809 QLIST_INIT (&vm_change_state_head
);
3812 struct sigaction act
;
3813 sigfillset(&act
.sa_mask
);
3815 act
.sa_handler
= SIG_IGN
;
3816 sigaction(SIGPIPE
, &act
, NULL
);
3819 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3820 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3821 QEMU to run on a single CPU */
3826 h
= GetCurrentProcess();
3827 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3828 for(i
= 0; i
< 32; i
++) {
3829 if (mask
& (1 << i
))
3834 SetProcessAffinityMask(h
, mask
);
3840 module_call_init(MODULE_INIT_MACHINE
);
3841 machine
= find_default_machine();
3843 initrd_filename
= NULL
;
3846 kernel_filename
= NULL
;
3847 kernel_cmdline
= "";
3848 cyls
= heads
= secs
= 0;
3849 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3851 for (i
= 0; i
< MAX_NODES
; i
++) {
3853 node_cpumask
[i
] = 0;
3862 /* first pass of option parsing */
3864 while (optind
< argc
) {
3865 if (argv
[optind
][0] != '-') {
3870 const QEMUOption
*popt
;
3872 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3873 switch (popt
->index
) {
3874 case QEMU_OPTION_nodefconfig
:
3885 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3886 fp
= fopen(fname
, "r");
3888 if (qemu_config_parse(fp
, fname
) != 0) {
3894 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3895 fp
= fopen(fname
, "r");
3897 if (qemu_config_parse(fp
, fname
) != 0) {
3903 #if defined(cpudef_setup)
3904 cpudef_setup(); /* parse cpu definitions in target config file */
3907 /* second pass of option parsing */
3912 if (argv
[optind
][0] != '-') {
3913 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3915 const QEMUOption
*popt
;
3917 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3918 switch(popt
->index
) {
3920 machine
= find_machine(optarg
);
3923 printf("Supported machines are:\n");
3924 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3926 printf("%-10s %s (alias of %s)\n",
3927 m
->alias
, m
->desc
, m
->name
);
3928 printf("%-10s %s%s\n",
3930 m
->is_default
? " (default)" : "");
3932 exit(*optarg
!= '?');
3935 case QEMU_OPTION_cpu
:
3936 /* hw initialization will check this */
3937 if (*optarg
== '?') {
3938 /* XXX: implement xxx_cpu_list for targets that still miss it */
3939 #if defined(cpu_list_id)
3940 cpu_list_id(stdout
, &fprintf
, optarg
);
3941 #elif defined(cpu_list)
3942 cpu_list(stdout
, &fprintf
); /* deprecated */
3949 case QEMU_OPTION_initrd
:
3950 initrd_filename
= optarg
;
3952 case QEMU_OPTION_hda
:
3954 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3956 hda_opts
= drive_add(optarg
, HD_ALIAS
3957 ",cyls=%d,heads=%d,secs=%d%s",
3958 0, cyls
, heads
, secs
,
3959 translation
== BIOS_ATA_TRANSLATION_LBA
?
3961 translation
== BIOS_ATA_TRANSLATION_NONE
?
3962 ",trans=none" : "");
3964 case QEMU_OPTION_hdb
:
3965 case QEMU_OPTION_hdc
:
3966 case QEMU_OPTION_hdd
:
3967 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3969 case QEMU_OPTION_drive
:
3970 drive_add(NULL
, "%s", optarg
);
3972 case QEMU_OPTION_set
:
3973 if (qemu_set_option(optarg
) != 0)
3976 case QEMU_OPTION_global
:
3977 if (qemu_global_option(optarg
) != 0)
3980 case QEMU_OPTION_mtdblock
:
3981 drive_add(optarg
, MTD_ALIAS
);
3983 case QEMU_OPTION_sd
:
3984 drive_add(optarg
, SD_ALIAS
);
3986 case QEMU_OPTION_pflash
:
3987 drive_add(optarg
, PFLASH_ALIAS
);
3989 case QEMU_OPTION_snapshot
:
3992 case QEMU_OPTION_hdachs
:
3996 cyls
= strtol(p
, (char **)&p
, 0);
3997 if (cyls
< 1 || cyls
> 16383)
4002 heads
= strtol(p
, (char **)&p
, 0);
4003 if (heads
< 1 || heads
> 16)
4008 secs
= strtol(p
, (char **)&p
, 0);
4009 if (secs
< 1 || secs
> 63)
4013 if (!strcmp(p
, "none"))
4014 translation
= BIOS_ATA_TRANSLATION_NONE
;
4015 else if (!strcmp(p
, "lba"))
4016 translation
= BIOS_ATA_TRANSLATION_LBA
;
4017 else if (!strcmp(p
, "auto"))
4018 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4021 } else if (*p
!= '\0') {
4023 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4026 if (hda_opts
!= NULL
) {
4028 snprintf(num
, sizeof(num
), "%d", cyls
);
4029 qemu_opt_set(hda_opts
, "cyls", num
);
4030 snprintf(num
, sizeof(num
), "%d", heads
);
4031 qemu_opt_set(hda_opts
, "heads", num
);
4032 snprintf(num
, sizeof(num
), "%d", secs
);
4033 qemu_opt_set(hda_opts
, "secs", num
);
4034 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4035 qemu_opt_set(hda_opts
, "trans", "lba");
4036 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4037 qemu_opt_set(hda_opts
, "trans", "none");
4041 case QEMU_OPTION_numa
:
4042 if (nb_numa_nodes
>= MAX_NODES
) {
4043 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4048 case QEMU_OPTION_nographic
:
4049 display_type
= DT_NOGRAPHIC
;
4051 #ifdef CONFIG_CURSES
4052 case QEMU_OPTION_curses
:
4053 display_type
= DT_CURSES
;
4056 case QEMU_OPTION_portrait
:
4059 case QEMU_OPTION_kernel
:
4060 kernel_filename
= optarg
;
4062 case QEMU_OPTION_append
:
4063 kernel_cmdline
= optarg
;
4065 case QEMU_OPTION_cdrom
:
4066 drive_add(optarg
, CDROM_ALIAS
);
4068 case QEMU_OPTION_boot
:
4070 static const char * const params
[] = {
4071 "order", "once", "menu", NULL
4073 char buf
[sizeof(boot_devices
)];
4074 char *standard_boot_devices
;
4077 if (!strchr(optarg
, '=')) {
4079 pstrcpy(buf
, sizeof(buf
), optarg
);
4080 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4082 "qemu: unknown boot parameter '%s' in '%s'\n",
4088 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4089 boot_devices_bitmap
= parse_bootdevices(buf
);
4090 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4093 if (get_param_value(buf
, sizeof(buf
),
4095 boot_devices_bitmap
|= parse_bootdevices(buf
);
4096 standard_boot_devices
= qemu_strdup(boot_devices
);
4097 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4098 qemu_register_reset(restore_boot_devices
,
4099 standard_boot_devices
);
4101 if (get_param_value(buf
, sizeof(buf
),
4103 if (!strcmp(buf
, "on")) {
4105 } else if (!strcmp(buf
, "off")) {
4109 "qemu: invalid option value '%s'\n",
4117 case QEMU_OPTION_fda
:
4118 case QEMU_OPTION_fdb
:
4119 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4122 case QEMU_OPTION_no_fd_bootchk
:
4126 case QEMU_OPTION_netdev
:
4127 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4131 case QEMU_OPTION_net
:
4132 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4137 case QEMU_OPTION_tftp
:
4138 legacy_tftp_prefix
= optarg
;
4140 case QEMU_OPTION_bootp
:
4141 legacy_bootp_filename
= optarg
;
4144 case QEMU_OPTION_smb
:
4145 if (net_slirp_smb(optarg
) < 0)
4149 case QEMU_OPTION_redir
:
4150 if (net_slirp_redir(optarg
) < 0)
4154 case QEMU_OPTION_bt
:
4155 add_device_config(DEV_BT
, optarg
);
4158 case QEMU_OPTION_audio_help
:
4162 case QEMU_OPTION_soundhw
:
4163 select_soundhw (optarg
);
4169 case QEMU_OPTION_version
:
4173 case QEMU_OPTION_m
: {
4177 value
= strtoul(optarg
, &ptr
, 10);
4179 case 0: case 'M': case 'm':
4186 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4190 /* On 32-bit hosts, QEMU is limited by virtual address space */
4191 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4192 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4195 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4196 fprintf(stderr
, "qemu: ram size too large\n");
4202 case QEMU_OPTION_mempath
:
4206 case QEMU_OPTION_mem_prealloc
:
4213 const CPULogItem
*item
;
4215 mask
= cpu_str_to_log_mask(optarg
);
4217 printf("Log items (comma separated):\n");
4218 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4219 printf("%-10s %s\n", item
->name
, item
->help
);
4227 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4229 case QEMU_OPTION_gdb
:
4230 gdbstub_dev
= optarg
;
4235 case QEMU_OPTION_bios
:
4238 case QEMU_OPTION_singlestep
:
4245 keyboard_layout
= optarg
;
4247 case QEMU_OPTION_localtime
:
4250 case QEMU_OPTION_vga
:
4251 select_vgahw (optarg
);
4253 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4259 w
= strtol(p
, (char **)&p
, 10);
4262 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4268 h
= strtol(p
, (char **)&p
, 10);
4273 depth
= strtol(p
, (char **)&p
, 10);
4274 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4275 depth
!= 24 && depth
!= 32)
4277 } else if (*p
== '\0') {
4278 depth
= graphic_depth
;
4285 graphic_depth
= depth
;
4289 case QEMU_OPTION_echr
:
4292 term_escape_char
= strtol(optarg
, &r
, 0);
4294 printf("Bad argument to echr\n");
4297 case QEMU_OPTION_monitor
:
4298 monitor_parse(optarg
, "readline");
4299 default_monitor
= 0;
4301 case QEMU_OPTION_qmp
:
4302 monitor_parse(optarg
, "control");
4305 case QEMU_OPTION_mon
:
4306 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4308 fprintf(stderr
, "parse error: %s\n", optarg
);
4312 mode
= qemu_opt_get(opts
, "mode");
4313 if (mode
== NULL
|| strcmp(mode
, "readline") == 0) {
4314 default_monitor
= 0;
4315 } else if (strcmp(mode
, "control") == 0) {
4320 case QEMU_OPTION_chardev
:
4321 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4323 fprintf(stderr
, "parse error: %s\n", optarg
);
4327 case QEMU_OPTION_serial
:
4328 add_device_config(DEV_SERIAL
, optarg
);
4330 if (strncmp(optarg
, "mon:", 4) == 0) {
4331 default_monitor
= 0;
4334 case QEMU_OPTION_watchdog
:
4337 "qemu: only one watchdog option may be given\n");
4342 case QEMU_OPTION_watchdog_action
:
4343 if (select_watchdog_action(optarg
) == -1) {
4344 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4348 case QEMU_OPTION_virtiocon
:
4349 add_device_config(DEV_VIRTCON
, optarg
);
4350 default_virtcon
= 0;
4351 if (strncmp(optarg
, "mon:", 4) == 0) {
4352 default_monitor
= 0;
4355 case QEMU_OPTION_parallel
:
4356 add_device_config(DEV_PARALLEL
, optarg
);
4357 default_parallel
= 0;
4358 if (strncmp(optarg
, "mon:", 4) == 0) {
4359 default_monitor
= 0;
4362 case QEMU_OPTION_debugcon
:
4363 add_device_config(DEV_DEBUGCON
, optarg
);
4365 case QEMU_OPTION_loadvm
:
4368 case QEMU_OPTION_full_screen
:
4372 case QEMU_OPTION_no_frame
:
4375 case QEMU_OPTION_alt_grab
:
4378 case QEMU_OPTION_ctrl_grab
:
4381 case QEMU_OPTION_no_quit
:
4384 case QEMU_OPTION_sdl
:
4385 display_type
= DT_SDL
;
4388 case QEMU_OPTION_pidfile
:
4392 case QEMU_OPTION_win2k_hack
:
4393 win2k_install_hack
= 1;
4395 case QEMU_OPTION_rtc_td_hack
:
4398 case QEMU_OPTION_acpitable
:
4399 if(acpi_table_add(optarg
) < 0) {
4400 fprintf(stderr
, "Wrong acpi table provided\n");
4404 case QEMU_OPTION_smbios
:
4405 if(smbios_entry_add(optarg
) < 0) {
4406 fprintf(stderr
, "Wrong smbios provided\n");
4412 case QEMU_OPTION_enable_kvm
:
4416 case QEMU_OPTION_usb
:
4419 case QEMU_OPTION_usbdevice
:
4421 add_device_config(DEV_USB
, optarg
);
4423 case QEMU_OPTION_device
:
4424 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4428 case QEMU_OPTION_smp
:
4431 fprintf(stderr
, "Invalid number of CPUs\n");
4434 if (max_cpus
< smp_cpus
) {
4435 fprintf(stderr
, "maxcpus must be equal to or greater than "
4439 if (max_cpus
> 255) {
4440 fprintf(stderr
, "Unsupported number of maxcpus\n");
4444 case QEMU_OPTION_vnc
:
4445 display_type
= DT_VNC
;
4446 vnc_display
= optarg
;
4449 case QEMU_OPTION_no_acpi
:
4452 case QEMU_OPTION_no_hpet
:
4455 case QEMU_OPTION_balloon
:
4456 if (balloon_parse(optarg
) < 0) {
4457 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4462 case QEMU_OPTION_no_reboot
:
4465 case QEMU_OPTION_no_shutdown
:
4468 case QEMU_OPTION_show_cursor
:
4471 case QEMU_OPTION_uuid
:
4472 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4473 fprintf(stderr
, "Fail to parse UUID string."
4474 " Wrong format.\n");
4479 case QEMU_OPTION_daemonize
:
4483 case QEMU_OPTION_option_rom
:
4484 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4485 fprintf(stderr
, "Too many option ROMs\n");
4488 option_rom
[nb_option_roms
] = optarg
;
4491 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4492 case QEMU_OPTION_semihosting
:
4493 semihosting_enabled
= 1;
4496 case QEMU_OPTION_name
:
4497 qemu_name
= qemu_strdup(optarg
);
4499 char *p
= strchr(qemu_name
, ',');
4502 if (strncmp(p
, "process=", 8)) {
4503 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4511 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4512 case QEMU_OPTION_prom_env
:
4513 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4514 fprintf(stderr
, "Too many prom variables\n");
4517 prom_envs
[nb_prom_envs
] = optarg
;
4522 case QEMU_OPTION_old_param
:
4526 case QEMU_OPTION_clock
:
4527 configure_alarms(optarg
);
4529 case QEMU_OPTION_startdate
:
4530 configure_rtc_date_offset(optarg
, 1);
4532 case QEMU_OPTION_rtc
:
4533 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4535 fprintf(stderr
, "parse error: %s\n", optarg
);
4538 configure_rtc(opts
);
4540 case QEMU_OPTION_tb_size
:
4541 tb_size
= strtol(optarg
, NULL
, 0);
4545 case QEMU_OPTION_icount
:
4546 icount_option
= optarg
;
4548 case QEMU_OPTION_incoming
:
4551 case QEMU_OPTION_nodefaults
:
4553 default_parallel
= 0;
4554 default_virtcon
= 0;
4555 default_monitor
= 0;
4564 case QEMU_OPTION_chroot
:
4565 chroot_dir
= optarg
;
4567 case QEMU_OPTION_runas
:
4572 case QEMU_OPTION_xen_domid
:
4573 xen_domid
= atoi(optarg
);
4575 case QEMU_OPTION_xen_create
:
4576 xen_mode
= XEN_CREATE
;
4578 case QEMU_OPTION_xen_attach
:
4579 xen_mode
= XEN_ATTACH
;
4582 case QEMU_OPTION_readconfig
:
4585 fp
= fopen(optarg
, "r");
4587 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4590 if (qemu_config_parse(fp
, optarg
) != 0) {
4596 case QEMU_OPTION_writeconfig
:
4599 if (strcmp(optarg
, "-") == 0) {
4602 fp
= fopen(optarg
, "w");
4604 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4608 qemu_config_write(fp
);
4617 /* If no data_dir is specified then try to find it relative to the
4620 data_dir
= find_datadir(argv
[0]);
4622 /* If all else fails use the install patch specified when building. */
4624 data_dir
= CONFIG_QEMU_SHAREDIR
;
4628 * Default to max_cpus = smp_cpus, in case the user doesn't
4629 * specify a max_cpus value.
4632 max_cpus
= smp_cpus
;
4634 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4635 if (smp_cpus
> machine
->max_cpus
) {
4636 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4637 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4642 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4643 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4645 if (machine
->no_serial
) {
4648 if (machine
->no_parallel
) {
4649 default_parallel
= 0;
4651 if (!machine
->use_virtcon
) {
4652 default_virtcon
= 0;
4654 if (machine
->no_vga
) {
4657 if (machine
->no_floppy
) {
4660 if (machine
->no_cdrom
) {
4663 if (machine
->no_sdcard
) {
4667 if (display_type
== DT_NOGRAPHIC
) {
4668 if (default_parallel
)
4669 add_device_config(DEV_PARALLEL
, "null");
4670 if (default_serial
&& default_monitor
) {
4671 add_device_config(DEV_SERIAL
, "mon:stdio");
4672 } else if (default_virtcon
&& default_monitor
) {
4673 add_device_config(DEV_VIRTCON
, "mon:stdio");
4676 add_device_config(DEV_SERIAL
, "stdio");
4677 if (default_virtcon
)
4678 add_device_config(DEV_VIRTCON
, "stdio");
4679 if (default_monitor
)
4680 monitor_parse("stdio", "readline");
4684 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4685 if (default_parallel
)
4686 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4687 if (default_monitor
)
4688 monitor_parse("vc:80Cx24C", "readline");
4689 if (default_virtcon
)
4690 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4696 vga_interface_type
= VGA_CIRRUS
;
4698 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4705 if (pipe(fds
) == -1)
4716 len
= read(fds
[0], &status
, 1);
4717 if (len
== -1 && (errno
== EINTR
))
4722 else if (status
== 1) {
4723 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4731 qemu_set_cloexec(fds
[1]);
4743 signal(SIGTSTP
, SIG_IGN
);
4744 signal(SIGTTOU
, SIG_IGN
);
4745 signal(SIGTTIN
, SIG_IGN
);
4749 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4753 if (write(fds
[1], &status
, 1) != 1) {
4754 perror("daemonize. Writing to pipe\n");
4758 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4762 if (kvm_enabled()) {
4765 ret
= kvm_init(smp_cpus
);
4767 fprintf(stderr
, "failed to initialize KVM\n");
4772 if (qemu_init_main_loop()) {
4773 fprintf(stderr
, "qemu_init_main_loop failed\n");
4776 linux_boot
= (kernel_filename
!= NULL
);
4778 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4779 fprintf(stderr
, "-append only allowed with -kernel option\n");
4783 if (!linux_boot
&& initrd_filename
!= NULL
) {
4784 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4789 /* Win32 doesn't support line-buffering and requires size >= 2 */
4790 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4793 if (init_timer_alarm() < 0) {
4794 fprintf(stderr
, "could not initialize alarm timer\n");
4797 configure_icount(icount_option
);
4803 if (net_init_clients() < 0) {
4807 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4808 net_set_boot_mask(net_boot
);
4810 /* init the bluetooth world */
4811 if (foreach_device_config(DEV_BT
, bt_parse
))
4814 /* init the memory */
4816 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4818 /* init the dynamic translator */
4819 cpu_exec_init_all(tb_size
* 1024 * 1024);
4821 bdrv_init_with_whitelist();
4825 if (default_cdrom
) {
4826 /* we always create the cdrom drive, even if no disk is there */
4827 drive_add(NULL
, CDROM_ALIAS
);
4830 if (default_floppy
) {
4831 /* we always create at least one floppy */
4832 drive_add(NULL
, FD_ALIAS
, 0);
4835 if (default_sdcard
) {
4836 /* we always create one sd slot, even if no card is in it */
4837 drive_add(NULL
, SD_ALIAS
);
4840 /* open the virtual block devices */
4842 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4843 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4846 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4849 if (nb_numa_nodes
> 0) {
4852 if (nb_numa_nodes
> smp_cpus
) {
4853 nb_numa_nodes
= smp_cpus
;
4856 /* If no memory size if given for any node, assume the default case
4857 * and distribute the available memory equally across all nodes
4859 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4860 if (node_mem
[i
] != 0)
4863 if (i
== nb_numa_nodes
) {
4864 uint64_t usedmem
= 0;
4866 /* On Linux, the each node's border has to be 8MB aligned,
4867 * the final node gets the rest.
4869 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4870 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4871 usedmem
+= node_mem
[i
];
4873 node_mem
[i
] = ram_size
- usedmem
;
4876 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4877 if (node_cpumask
[i
] != 0)
4880 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4881 * must cope with this anyway, because there are BIOSes out there in
4882 * real machines which also use this scheme.
4884 if (i
== nb_numa_nodes
) {
4885 for (i
= 0; i
< smp_cpus
; i
++) {
4886 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4891 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4893 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4895 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4897 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4900 module_call_init(MODULE_INIT_DEVICE
);
4902 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4906 i
= select_watchdog(watchdog
);
4908 exit (i
== 1 ? 1 : 0);
4911 if (machine
->compat_props
) {
4912 qdev_prop_register_global_list(machine
->compat_props
);
4916 machine
->init(ram_size
, boot_devices
,
4917 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4919 cpu_synchronize_all_post_init();
4922 /* must be after terminal init, SDL library changes signal handlers */
4926 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4927 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4928 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4934 current_machine
= machine
;
4936 /* init USB devices */
4938 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4942 /* init generic devices */
4943 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4946 net_check_clients();
4948 /* just use the first displaystate for the moment */
4949 ds
= get_displaystate();
4951 if (display_type
== DT_DEFAULT
) {
4952 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4953 display_type
= DT_SDL
;
4955 display_type
= DT_VNC
;
4956 vnc_display
= "localhost:0,to=99";
4962 switch (display_type
) {
4965 #if defined(CONFIG_CURSES)
4967 curses_display_init(ds
, full_screen
);
4970 #if defined(CONFIG_SDL)
4972 sdl_display_init(ds
, full_screen
, no_frame
);
4974 #elif defined(CONFIG_COCOA)
4976 cocoa_display_init(ds
, full_screen
);
4980 vnc_display_init(ds
);
4981 if (vnc_display_open(ds
, vnc_display
) < 0)
4984 if (show_vnc_port
) {
4985 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4993 dcl
= ds
->listeners
;
4994 while (dcl
!= NULL
) {
4995 if (dcl
->dpy_refresh
!= NULL
) {
4996 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
4997 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5002 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5003 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5004 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5007 text_consoles_set_display(ds
);
5009 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5012 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5013 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5018 qdev_machine_creation_done();
5020 if (rom_load_all() != 0) {
5021 fprintf(stderr
, "rom loading failed\n");
5025 qemu_system_reset();
5027 if (load_vmstate(loadvm
) < 0) {
5033 qemu_start_incoming_migration(incoming
);
5034 } else if (autostart
) {
5044 len
= write(fds
[1], &status
, 1);
5045 if (len
== -1 && (errno
== EINTR
))
5052 perror("not able to chdir to /");
5055 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5061 pwd
= getpwnam(run_as
);
5063 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5069 if (chroot(chroot_dir
) < 0) {
5070 fprintf(stderr
, "chroot failed\n");
5074 perror("not able to chdir to /");
5080 if (setgid(pwd
->pw_gid
) < 0) {
5081 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5084 if (setuid(pwd
->pw_uid
) < 0) {
5085 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5088 if (setuid(0) != -1) {
5089 fprintf(stderr
, "Dropping privileges failed\n");