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(Notifier
*obj
)
545 static int socket_init(void)
549 static Notifier notifier
= { .notify
= socket_cleanup
};
551 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
553 err
= WSAGetLastError();
554 fprintf(stderr
, "WSAStartup: %d\n", err
);
557 exit_notifier_add(¬ifier
);
562 /***********************************************************/
563 /* Bluetooth support */
566 static struct HCIInfo
*hci_table
[MAX_NICS
];
568 static struct bt_vlan_s
{
569 struct bt_scatternet_s net
;
571 struct bt_vlan_s
*next
;
574 /* find or alloc a new bluetooth "VLAN" */
575 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
577 struct bt_vlan_s
**pvlan
, *vlan
;
578 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
582 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
584 pvlan
= &first_bt_vlan
;
585 while (*pvlan
!= NULL
)
586 pvlan
= &(*pvlan
)->next
;
591 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
595 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
600 static struct HCIInfo null_hci
= {
601 .cmd_send
= null_hci_send
,
602 .sco_send
= null_hci_send
,
603 .acl_send
= null_hci_send
,
604 .bdaddr_set
= null_hci_addr_set
,
607 struct HCIInfo
*qemu_next_hci(void)
609 if (cur_hci
== nb_hcis
)
612 return hci_table
[cur_hci
++];
615 static struct HCIInfo
*hci_init(const char *str
)
618 struct bt_scatternet_s
*vlan
= 0;
620 if (!strcmp(str
, "null"))
623 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
625 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
626 else if (!strncmp(str
, "hci", 3)) {
629 if (!strncmp(str
+ 3, ",vlan=", 6)) {
630 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
635 vlan
= qemu_find_bt_vlan(0);
637 return bt_new_hci(vlan
);
640 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
645 static int bt_hci_parse(const char *str
)
650 if (nb_hcis
>= MAX_NICS
) {
651 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
664 bdaddr
.b
[5] = 0x56 + nb_hcis
;
665 hci
->bdaddr_set(hci
, bdaddr
.b
);
667 hci_table
[nb_hcis
++] = hci
;
672 static void bt_vhci_add(int vlan_id
)
674 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
677 fprintf(stderr
, "qemu: warning: adding a VHCI to "
678 "an empty scatternet %i\n", vlan_id
);
680 bt_vhci_init(bt_new_hci(vlan
));
683 static struct bt_device_s
*bt_device_add(const char *opt
)
685 struct bt_scatternet_s
*vlan
;
687 char *endp
= strstr(opt
, ",vlan=");
688 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
691 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
694 vlan_id
= strtol(endp
+ 6, &endp
, 0);
696 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
701 vlan
= qemu_find_bt_vlan(vlan_id
);
704 fprintf(stderr
, "qemu: warning: adding a slave device to "
705 "an empty scatternet %i\n", vlan_id
);
707 if (!strcmp(devname
, "keyboard"))
708 return bt_keyboard_init(vlan
);
710 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
714 static int bt_parse(const char *opt
)
716 const char *endp
, *p
;
719 if (strstart(opt
, "hci", &endp
)) {
720 if (!*endp
|| *endp
== ',') {
722 if (!strstart(endp
, ",vlan=", 0))
725 return bt_hci_parse(opt
);
727 } else if (strstart(opt
, "vhci", &endp
)) {
728 if (!*endp
|| *endp
== ',') {
730 if (strstart(endp
, ",vlan=", &p
)) {
731 vlan
= strtol(p
, (char **) &endp
, 0);
733 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
737 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
746 } else if (strstart(opt
, "device:", &endp
))
747 return !bt_device_add(endp
);
749 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
753 /***********************************************************/
754 /* QEMU Block devices */
756 #define HD_ALIAS "index=%d,media=disk"
757 #define CDROM_ALIAS "index=2,media=cdrom"
758 #define FD_ALIAS "index=%d,if=floppy"
759 #define PFLASH_ALIAS "if=pflash"
760 #define MTD_ALIAS "if=mtd"
761 #define SD_ALIAS "index=0,if=sd"
763 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
770 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
773 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
775 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
776 __FUNCTION__
, optstr
);
780 qemu_opt_set(opts
, "file", file
);
784 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
788 /* seek interface, bus and unit */
790 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
791 if (dinfo
->type
== type
&&
800 DriveInfo
*drive_get_by_id(const char *id
)
804 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
805 if (strcmp(id
, dinfo
->id
))
812 int drive_get_max_bus(BlockInterfaceType type
)
818 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
819 if(dinfo
->type
== type
&&
820 dinfo
->bus
> max_bus
)
821 max_bus
= dinfo
->bus
;
826 const char *drive_get_serial(BlockDriverState
*bdrv
)
830 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
831 if (dinfo
->bdrv
== bdrv
)
832 return dinfo
->serial
;
838 BlockInterfaceErrorAction
drive_get_on_error(
839 BlockDriverState
*bdrv
, int is_read
)
843 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
844 if (dinfo
->bdrv
== bdrv
)
845 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
848 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
851 static void bdrv_format_print(void *opaque
, const char *name
)
853 fprintf(stderr
, " %s", name
);
856 void drive_uninit(DriveInfo
*dinfo
)
858 qemu_opts_del(dinfo
->opts
);
859 bdrv_delete(dinfo
->bdrv
);
860 QTAILQ_REMOVE(&drives
, dinfo
, next
);
864 static int parse_block_error_action(const char *buf
, int is_read
)
866 if (!strcmp(buf
, "ignore")) {
867 return BLOCK_ERR_IGNORE
;
868 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
869 return BLOCK_ERR_STOP_ENOSPC
;
870 } else if (!strcmp(buf
, "stop")) {
871 return BLOCK_ERR_STOP_ANY
;
872 } else if (!strcmp(buf
, "report")) {
873 return BLOCK_ERR_REPORT
;
875 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
876 buf
, is_read
? "read" : "write");
881 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
885 const char *file
= NULL
;
888 const char *mediastr
= "";
889 BlockInterfaceType type
;
890 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
892 int cyls
, heads
, secs
, translation
;
893 BlockDriver
*drv
= NULL
;
894 QEMUMachine
*machine
= opaque
;
901 int on_read_error
, on_write_error
;
908 translation
= BIOS_ATA_TRANSLATION_AUTO
;
911 if (machine
&& machine
->use_scsi
) {
913 max_devs
= MAX_SCSI_DEVS
;
914 pstrcpy(devname
, sizeof(devname
), "scsi");
917 max_devs
= MAX_IDE_DEVS
;
918 pstrcpy(devname
, sizeof(devname
), "ide");
922 /* extract parameters */
923 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
924 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
925 index
= qemu_opt_get_number(opts
, "index", -1);
927 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
928 heads
= qemu_opt_get_number(opts
, "heads", 0);
929 secs
= qemu_opt_get_number(opts
, "secs", 0);
931 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
932 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
934 file
= qemu_opt_get(opts
, "file");
935 serial
= qemu_opt_get(opts
, "serial");
937 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
938 pstrcpy(devname
, sizeof(devname
), buf
);
939 if (!strcmp(buf
, "ide")) {
941 max_devs
= MAX_IDE_DEVS
;
942 } else if (!strcmp(buf
, "scsi")) {
944 max_devs
= MAX_SCSI_DEVS
;
945 } else if (!strcmp(buf
, "floppy")) {
948 } else if (!strcmp(buf
, "pflash")) {
951 } else if (!strcmp(buf
, "mtd")) {
954 } else if (!strcmp(buf
, "sd")) {
957 } else if (!strcmp(buf
, "virtio")) {
960 } else if (!strcmp(buf
, "xen")) {
963 } else if (!strcmp(buf
, "none")) {
967 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
972 if (cyls
|| heads
|| secs
) {
973 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
974 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
977 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
978 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
981 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
982 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
987 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
990 "qemu: '%s' trans must be used with cyls,heads and secs\n",
994 if (!strcmp(buf
, "none"))
995 translation
= BIOS_ATA_TRANSLATION_NONE
;
996 else if (!strcmp(buf
, "lba"))
997 translation
= BIOS_ATA_TRANSLATION_LBA
;
998 else if (!strcmp(buf
, "auto"))
999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1001 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1006 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1007 if (!strcmp(buf
, "disk")) {
1009 } else if (!strcmp(buf
, "cdrom")) {
1010 if (cyls
|| secs
|| heads
) {
1012 "qemu: '%s' invalid physical CHS format\n", buf
);
1015 media
= MEDIA_CDROM
;
1017 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1022 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1023 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1025 else if (!strcmp(buf
, "writethrough"))
1027 else if (!strcmp(buf
, "writeback"))
1030 fprintf(stderr
, "qemu: invalid cache option\n");
1035 #ifdef CONFIG_LINUX_AIO
1036 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1037 if (!strcmp(buf
, "threads"))
1039 else if (!strcmp(buf
, "native"))
1042 fprintf(stderr
, "qemu: invalid aio option\n");
1048 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1049 if (strcmp(buf
, "?") == 0) {
1050 fprintf(stderr
, "qemu: Supported formats:");
1051 bdrv_iterate_format(bdrv_format_print
, NULL
);
1052 fprintf(stderr
, "\n");
1055 drv
= bdrv_find_whitelisted_format(buf
);
1057 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1062 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1063 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1064 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1065 fprintf(stderr
, "werror is no supported by this format\n");
1069 on_write_error
= parse_block_error_action(buf
, 0);
1070 if (on_write_error
< 0) {
1075 on_read_error
= BLOCK_ERR_REPORT
;
1076 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1077 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1078 fprintf(stderr
, "rerror is no supported by this format\n");
1082 on_read_error
= parse_block_error_action(buf
, 1);
1083 if (on_read_error
< 0) {
1088 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1089 if (type
!= IF_VIRTIO
) {
1090 fprintf(stderr
, "addr is not supported\n");
1095 /* compute bus and unit according index */
1098 if (bus_id
!= 0 || unit_id
!= -1) {
1100 "qemu: index cannot be used with bus and unit\n");
1108 unit_id
= index
% max_devs
;
1109 bus_id
= index
/ max_devs
;
1113 /* if user doesn't specify a unit_id,
1114 * try to find the first free
1117 if (unit_id
== -1) {
1119 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1121 if (max_devs
&& unit_id
>= max_devs
) {
1122 unit_id
-= max_devs
;
1130 if (max_devs
&& unit_id
>= max_devs
) {
1131 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1132 unit_id
, max_devs
- 1);
1137 * ignore multiple definitions
1140 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1147 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1148 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1149 dinfo
->id
= qemu_strdup(buf
);
1151 /* no id supplied -> create one */
1152 dinfo
->id
= qemu_mallocz(32);
1153 if (type
== IF_IDE
|| type
== IF_SCSI
)
1154 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1156 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1157 devname
, bus_id
, mediastr
, unit_id
);
1159 snprintf(dinfo
->id
, 32, "%s%s%i",
1160 devname
, mediastr
, unit_id
);
1162 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1163 dinfo
->devaddr
= devaddr
;
1165 dinfo
->bus
= bus_id
;
1166 dinfo
->unit
= unit_id
;
1167 dinfo
->on_read_error
= on_read_error
;
1168 dinfo
->on_write_error
= on_write_error
;
1171 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1172 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1182 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1183 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1187 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1192 /* FIXME: This isn't really a floppy, but it's a reasonable
1195 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1201 /* add virtio block device */
1202 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1203 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1204 qemu_opt_set(opts
, "drive", dinfo
->id
);
1206 qemu_opt_set(opts
, "addr", devaddr
);
1217 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1218 cache
= 2; /* always use write-back with snapshot */
1220 if (cache
== 0) /* no caching */
1221 bdrv_flags
|= BDRV_O_NOCACHE
;
1222 else if (cache
== 2) /* write-back */
1223 bdrv_flags
|= BDRV_O_CACHE_WB
;
1226 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1228 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1232 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1233 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1238 * cdrom is read-only. Set it now, after above interface checking
1239 * since readonly attribute not explicitly required, so no error.
1241 if (media
== MEDIA_CDROM
) {
1244 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1246 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1247 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1248 file
, strerror(errno
));
1252 if (bdrv_key_required(dinfo
->bdrv
))
1258 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1260 QEMUMachine
*machine
= opaque
;
1261 int fatal_error
= 0;
1263 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1270 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1272 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1273 qemu_opt_set(opts
, "snapshot", "on");
1278 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1280 boot_set_handler
= func
;
1281 boot_set_opaque
= opaque
;
1284 int qemu_boot_set(const char *boot_devices
)
1286 if (!boot_set_handler
) {
1289 return boot_set_handler(boot_set_opaque
, boot_devices
);
1292 static int parse_bootdevices(char *devices
)
1294 /* We just do some generic consistency checks */
1298 for (p
= devices
; *p
!= '\0'; p
++) {
1299 /* Allowed boot devices are:
1300 * a-b: floppy disk drives
1301 * c-f: IDE disk drives
1302 * g-m: machine implementation dependant drives
1303 * n-p: network devices
1304 * It's up to each machine implementation to check if the given boot
1305 * devices match the actual hardware implementation and firmware
1308 if (*p
< 'a' || *p
> 'p') {
1309 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1312 if (bitmap
& (1 << (*p
- 'a'))) {
1313 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1316 bitmap
|= 1 << (*p
- 'a');
1321 static void restore_boot_devices(void *opaque
)
1323 char *standard_boot_devices
= opaque
;
1325 qemu_boot_set(standard_boot_devices
);
1327 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1328 qemu_free(standard_boot_devices
);
1331 static void numa_add(const char *optarg
)
1335 unsigned long long value
, endvalue
;
1338 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1339 if (!strcmp(option
, "node")) {
1340 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1341 nodenr
= nb_numa_nodes
;
1343 nodenr
= strtoull(option
, NULL
, 10);
1346 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1347 node_mem
[nodenr
] = 0;
1349 value
= strtoull(option
, &endptr
, 0);
1351 case 0: case 'M': case 'm':
1358 node_mem
[nodenr
] = value
;
1360 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1361 node_cpumask
[nodenr
] = 0;
1363 value
= strtoull(option
, &endptr
, 10);
1366 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1368 if (*endptr
== '-') {
1369 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1370 if (endvalue
>= 63) {
1373 "only 63 CPUs in NUMA mode supported.\n");
1375 value
= (2ULL << endvalue
) - (1ULL << value
);
1377 value
= 1ULL << value
;
1380 node_cpumask
[nodenr
] = value
;
1387 static void smp_parse(const char *optarg
)
1389 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1393 smp
= strtoul(optarg
, &endptr
, 10);
1394 if (endptr
!= optarg
) {
1395 if (*endptr
== ',') {
1399 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1400 sockets
= strtoull(option
, NULL
, 10);
1401 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1402 cores
= strtoull(option
, NULL
, 10);
1403 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1404 threads
= strtoull(option
, NULL
, 10);
1405 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1406 max_cpus
= strtoull(option
, NULL
, 10);
1408 /* compute missing values, prefer sockets over cores over threads */
1409 if (smp
== 0 || sockets
== 0) {
1410 sockets
= sockets
> 0 ? sockets
: 1;
1411 cores
= cores
> 0 ? cores
: 1;
1412 threads
= threads
> 0 ? threads
: 1;
1414 smp
= cores
* threads
* sockets
;
1418 threads
= threads
> 0 ? threads
: 1;
1419 cores
= smp
/ (sockets
* threads
);
1422 threads
= smp
/ (cores
* sockets
);
1427 smp_cores
= cores
> 0 ? cores
: 1;
1428 smp_threads
= threads
> 0 ? threads
: 1;
1430 max_cpus
= smp_cpus
;
1433 /***********************************************************/
1436 static int usb_device_add(const char *devname
, int is_hotplug
)
1439 USBDevice
*dev
= NULL
;
1444 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1445 dev
= usbdevice_create(devname
);
1449 /* the other ones */
1450 if (strstart(devname
, "host:", &p
)) {
1451 dev
= usb_host_device_open(p
);
1452 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1453 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1454 bt_new_hci(qemu_find_bt_vlan(0)));
1465 static int usb_device_del(const char *devname
)
1470 if (strstart(devname
, "host:", &p
))
1471 return usb_host_device_close(p
);
1476 p
= strchr(devname
, '.');
1479 bus_num
= strtoul(devname
, NULL
, 0);
1480 addr
= strtoul(p
+ 1, NULL
, 0);
1482 return usb_device_delete_addr(bus_num
, addr
);
1485 static int usb_parse(const char *cmdline
)
1488 r
= usb_device_add(cmdline
, 0);
1490 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1495 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1497 const char *devname
= qdict_get_str(qdict
, "devname");
1498 if (usb_device_add(devname
, 1) < 0) {
1499 error_report("could not add USB device '%s'", devname
);
1503 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1505 const char *devname
= qdict_get_str(qdict
, "devname");
1506 if (usb_device_del(devname
) < 0) {
1507 error_report("could not delete USB device '%s'", devname
);
1511 /***********************************************************/
1512 /* PCMCIA/Cardbus */
1514 static struct pcmcia_socket_entry_s
{
1515 PCMCIASocket
*socket
;
1516 struct pcmcia_socket_entry_s
*next
;
1517 } *pcmcia_sockets
= 0;
1519 void pcmcia_socket_register(PCMCIASocket
*socket
)
1521 struct pcmcia_socket_entry_s
*entry
;
1523 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1524 entry
->socket
= socket
;
1525 entry
->next
= pcmcia_sockets
;
1526 pcmcia_sockets
= entry
;
1529 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1531 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1533 ptr
= &pcmcia_sockets
;
1534 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1535 if (entry
->socket
== socket
) {
1541 void pcmcia_info(Monitor
*mon
)
1543 struct pcmcia_socket_entry_s
*iter
;
1545 if (!pcmcia_sockets
)
1546 monitor_printf(mon
, "No PCMCIA sockets\n");
1548 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1549 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1550 iter
->socket
->attached
? iter
->socket
->card_string
:
1554 /***********************************************************/
1557 typedef struct IOHandlerRecord
{
1559 IOCanReadHandler
*fd_read_poll
;
1561 IOHandler
*fd_write
;
1564 /* temporary data */
1566 QLIST_ENTRY(IOHandlerRecord
) next
;
1569 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1570 QLIST_HEAD_INITIALIZER(io_handlers
);
1573 /* XXX: fd_read_poll should be suppressed, but an API change is
1574 necessary in the character devices to suppress fd_can_read(). */
1575 int qemu_set_fd_handler2(int fd
,
1576 IOCanReadHandler
*fd_read_poll
,
1578 IOHandler
*fd_write
,
1581 IOHandlerRecord
*ioh
;
1583 if (!fd_read
&& !fd_write
) {
1584 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1585 if (ioh
->fd
== fd
) {
1591 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1595 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1596 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1599 ioh
->fd_read_poll
= fd_read_poll
;
1600 ioh
->fd_read
= fd_read
;
1601 ioh
->fd_write
= fd_write
;
1602 ioh
->opaque
= opaque
;
1608 int qemu_set_fd_handler(int fd
,
1610 IOHandler
*fd_write
,
1613 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1617 /***********************************************************/
1618 /* Polling handling */
1620 typedef struct PollingEntry
{
1623 struct PollingEntry
*next
;
1626 static PollingEntry
*first_polling_entry
;
1628 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1630 PollingEntry
**ppe
, *pe
;
1631 pe
= qemu_mallocz(sizeof(PollingEntry
));
1633 pe
->opaque
= opaque
;
1634 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1639 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1641 PollingEntry
**ppe
, *pe
;
1642 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1644 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1652 /***********************************************************/
1653 /* Wait objects support */
1654 typedef struct WaitObjects
{
1656 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1657 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1658 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1661 static WaitObjects wait_objects
= {0};
1663 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1665 WaitObjects
*w
= &wait_objects
;
1667 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1669 w
->events
[w
->num
] = handle
;
1670 w
->func
[w
->num
] = func
;
1671 w
->opaque
[w
->num
] = opaque
;
1676 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1679 WaitObjects
*w
= &wait_objects
;
1682 for (i
= 0; i
< w
->num
; i
++) {
1683 if (w
->events
[i
] == handle
)
1686 w
->events
[i
] = w
->events
[i
+ 1];
1687 w
->func
[i
] = w
->func
[i
+ 1];
1688 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1696 /***********************************************************/
1697 /* ram save/restore */
1699 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1700 #define RAM_SAVE_FLAG_COMPRESS 0x02
1701 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1702 #define RAM_SAVE_FLAG_PAGE 0x08
1703 #define RAM_SAVE_FLAG_EOS 0x10
1705 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1707 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1708 uint32_t *array
= (uint32_t *)page
;
1711 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1712 if (array
[i
] != val
)
1719 static int ram_save_block(QEMUFile
*f
)
1721 static ram_addr_t current_addr
= 0;
1722 ram_addr_t saved_addr
= current_addr
;
1723 ram_addr_t addr
= 0;
1726 while (addr
< last_ram_offset
) {
1727 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1730 cpu_physical_memory_reset_dirty(current_addr
,
1731 current_addr
+ TARGET_PAGE_SIZE
,
1732 MIGRATION_DIRTY_FLAG
);
1734 p
= qemu_get_ram_ptr(current_addr
);
1736 if (is_dup_page(p
, *p
)) {
1737 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1738 qemu_put_byte(f
, *p
);
1740 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1741 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1747 addr
+= TARGET_PAGE_SIZE
;
1748 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1754 static uint64_t bytes_transferred
;
1756 static ram_addr_t
ram_save_remaining(void)
1759 ram_addr_t count
= 0;
1761 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1762 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1769 uint64_t ram_bytes_remaining(void)
1771 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1774 uint64_t ram_bytes_transferred(void)
1776 return bytes_transferred
;
1779 uint64_t ram_bytes_total(void)
1781 return last_ram_offset
;
1784 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1787 uint64_t bytes_transferred_last
;
1789 uint64_t expected_time
= 0;
1792 cpu_physical_memory_set_dirty_tracking(0);
1796 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1797 qemu_file_set_error(f
);
1802 bytes_transferred
= 0;
1804 /* Make sure all dirty bits are set */
1805 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1806 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1807 cpu_physical_memory_set_dirty(addr
);
1810 /* Enable dirty memory tracking */
1811 cpu_physical_memory_set_dirty_tracking(1);
1813 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1816 bytes_transferred_last
= bytes_transferred
;
1817 bwidth
= qemu_get_clock_ns(rt_clock
);
1819 while (!qemu_file_rate_limit(f
)) {
1822 ret
= ram_save_block(f
);
1823 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1824 if (ret
== 0) /* no more blocks */
1828 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1829 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1831 /* if we haven't transferred anything this round, force expected_time to a
1832 * a very high value, but without crashing */
1836 /* try transferring iterative blocks of memory */
1838 /* flush all remaining blocks regardless of rate limiting */
1839 while (ram_save_block(f
) != 0) {
1840 bytes_transferred
+= TARGET_PAGE_SIZE
;
1842 cpu_physical_memory_set_dirty_tracking(0);
1845 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1847 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1849 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1852 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1857 if (version_id
!= 3)
1861 addr
= qemu_get_be64(f
);
1863 flags
= addr
& ~TARGET_PAGE_MASK
;
1864 addr
&= TARGET_PAGE_MASK
;
1866 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1867 if (addr
!= last_ram_offset
)
1871 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1872 uint8_t ch
= qemu_get_byte(f
);
1873 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1876 (!kvm_enabled() || kvm_has_sync_mmu())) {
1877 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1880 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1881 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1883 if (qemu_file_has_error(f
)) {
1886 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1891 void qemu_service_io(void)
1893 qemu_notify_event();
1896 /***********************************************************/
1897 /* machine registration */
1899 static QEMUMachine
*first_machine
= NULL
;
1900 QEMUMachine
*current_machine
= NULL
;
1902 int qemu_register_machine(QEMUMachine
*m
)
1905 pm
= &first_machine
;
1913 static QEMUMachine
*find_machine(const char *name
)
1917 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1918 if (!strcmp(m
->name
, name
))
1920 if (m
->alias
&& !strcmp(m
->alias
, name
))
1926 static QEMUMachine
*find_default_machine(void)
1930 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1931 if (m
->is_default
) {
1938 /***********************************************************/
1939 /* main execution loop */
1941 static void gui_update(void *opaque
)
1943 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1944 DisplayState
*ds
= opaque
;
1945 DisplayChangeListener
*dcl
= ds
->listeners
;
1947 qemu_flush_coalesced_mmio_buffer();
1950 while (dcl
!= NULL
) {
1951 if (dcl
->gui_timer_interval
&&
1952 dcl
->gui_timer_interval
< interval
)
1953 interval
= dcl
->gui_timer_interval
;
1956 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1959 static void nographic_update(void *opaque
)
1961 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1963 qemu_flush_coalesced_mmio_buffer();
1964 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1967 void cpu_synchronize_all_states(void)
1971 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1972 cpu_synchronize_state(cpu
);
1976 void cpu_synchronize_all_post_reset(void)
1980 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1981 cpu_synchronize_post_reset(cpu
);
1985 void cpu_synchronize_all_post_init(void)
1989 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1990 cpu_synchronize_post_init(cpu
);
1994 struct vm_change_state_entry
{
1995 VMChangeStateHandler
*cb
;
1997 QLIST_ENTRY (vm_change_state_entry
) entries
;
2000 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2002 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2005 VMChangeStateEntry
*e
;
2007 e
= qemu_mallocz(sizeof (*e
));
2011 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2015 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2017 QLIST_REMOVE (e
, entries
);
2021 static void vm_state_notify(int running
, int reason
)
2023 VMChangeStateEntry
*e
;
2025 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2026 e
->cb(e
->opaque
, running
, reason
);
2030 static void resume_all_vcpus(void);
2031 static void pause_all_vcpus(void);
2038 vm_state_notify(1, 0);
2043 /* reset/shutdown handler */
2045 typedef struct QEMUResetEntry
{
2046 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2047 QEMUResetHandler
*func
;
2051 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2052 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2053 static int reset_requested
;
2054 static int shutdown_requested
;
2055 static int powerdown_requested
;
2056 static int debug_requested
;
2057 static int vmstop_requested
;
2059 int qemu_shutdown_requested(void)
2061 int r
= shutdown_requested
;
2062 shutdown_requested
= 0;
2066 int qemu_reset_requested(void)
2068 int r
= reset_requested
;
2069 reset_requested
= 0;
2073 int qemu_powerdown_requested(void)
2075 int r
= powerdown_requested
;
2076 powerdown_requested
= 0;
2080 static int qemu_debug_requested(void)
2082 int r
= debug_requested
;
2083 debug_requested
= 0;
2087 static int qemu_vmstop_requested(void)
2089 int r
= vmstop_requested
;
2090 vmstop_requested
= 0;
2094 static void do_vm_stop(int reason
)
2097 cpu_disable_ticks();
2100 vm_state_notify(0, reason
);
2101 monitor_protocol_event(QEVENT_STOP
, NULL
);
2105 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2107 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2110 re
->opaque
= opaque
;
2111 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2114 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2118 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2119 if (re
->func
== func
&& re
->opaque
== opaque
) {
2120 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2127 void qemu_system_reset(void)
2129 QEMUResetEntry
*re
, *nre
;
2131 /* reset all devices */
2132 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2133 re
->func(re
->opaque
);
2135 monitor_protocol_event(QEVENT_RESET
, NULL
);
2136 cpu_synchronize_all_post_reset();
2139 void qemu_system_reset_request(void)
2142 shutdown_requested
= 1;
2144 reset_requested
= 1;
2146 qemu_notify_event();
2149 void qemu_system_shutdown_request(void)
2151 shutdown_requested
= 1;
2152 qemu_notify_event();
2155 void qemu_system_powerdown_request(void)
2157 powerdown_requested
= 1;
2158 qemu_notify_event();
2161 static int cpu_can_run(CPUState
*env
)
2172 static int cpu_has_work(CPUState
*env
)
2180 if (qemu_cpu_has_work(env
))
2185 static int tcg_has_work(void)
2189 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2190 if (cpu_has_work(env
))
2196 static int io_thread_fd
= -1;
2198 static void qemu_event_increment(void)
2200 /* Write 8 bytes to be compatible with eventfd. */
2201 static uint64_t val
= 1;
2204 if (io_thread_fd
== -1)
2208 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2209 } while (ret
< 0 && errno
== EINTR
);
2211 /* EAGAIN is fine, a read must be pending. */
2212 if (ret
< 0 && errno
!= EAGAIN
) {
2213 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2219 static void qemu_event_read(void *opaque
)
2221 int fd
= (unsigned long)opaque
;
2225 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2227 len
= read(fd
, buffer
, sizeof(buffer
));
2228 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2231 static int qemu_event_init(void)
2236 err
= qemu_eventfd(fds
);
2240 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2244 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2248 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2249 (void *)(unsigned long)fds
[0]);
2251 io_thread_fd
= fds
[1];
2260 HANDLE qemu_event_handle
;
2262 static void dummy_event_handler(void *opaque
)
2266 static int qemu_event_init(void)
2268 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2269 if (!qemu_event_handle
) {
2270 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2273 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2277 static void qemu_event_increment(void)
2279 if (!SetEvent(qemu_event_handle
)) {
2280 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2287 #ifndef CONFIG_IOTHREAD
2288 static int qemu_init_main_loop(void)
2290 return qemu_event_init();
2293 void qemu_init_vcpu(void *_env
)
2295 CPUState
*env
= _env
;
2297 env
->nr_cores
= smp_cores
;
2298 env
->nr_threads
= smp_threads
;
2304 int qemu_cpu_self(void *env
)
2309 static void resume_all_vcpus(void)
2313 static void pause_all_vcpus(void)
2317 void qemu_cpu_kick(void *env
)
2322 void qemu_notify_event(void)
2324 CPUState
*env
= cpu_single_env
;
2326 qemu_event_increment ();
2330 if (next_cpu
&& env
!= next_cpu
) {
2335 void qemu_mutex_lock_iothread(void) {}
2336 void qemu_mutex_unlock_iothread(void) {}
2338 void vm_stop(int reason
)
2343 #else /* CONFIG_IOTHREAD */
2345 #include "qemu-thread.h"
2347 QemuMutex qemu_global_mutex
;
2348 static QemuMutex qemu_fair_mutex
;
2350 static QemuThread io_thread
;
2352 static QemuThread
*tcg_cpu_thread
;
2353 static QemuCond
*tcg_halt_cond
;
2355 static int qemu_system_ready
;
2357 static QemuCond qemu_cpu_cond
;
2359 static QemuCond qemu_system_cond
;
2360 static QemuCond qemu_pause_cond
;
2362 static void tcg_block_io_signals(void);
2363 static void kvm_block_io_signals(CPUState
*env
);
2364 static void unblock_io_signals(void);
2366 static int qemu_init_main_loop(void)
2370 ret
= qemu_event_init();
2374 qemu_cond_init(&qemu_pause_cond
);
2375 qemu_mutex_init(&qemu_fair_mutex
);
2376 qemu_mutex_init(&qemu_global_mutex
);
2377 qemu_mutex_lock(&qemu_global_mutex
);
2379 unblock_io_signals();
2380 qemu_thread_self(&io_thread
);
2385 static void qemu_wait_io_event_common(CPUState
*env
)
2390 qemu_cond_signal(&qemu_pause_cond
);
2394 static void qemu_wait_io_event(CPUState
*env
)
2396 while (!tcg_has_work())
2397 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2399 qemu_mutex_unlock(&qemu_global_mutex
);
2402 * Users of qemu_global_mutex can be starved, having no chance
2403 * to acquire it since this path will get to it first.
2404 * So use another lock to provide fairness.
2406 qemu_mutex_lock(&qemu_fair_mutex
);
2407 qemu_mutex_unlock(&qemu_fair_mutex
);
2409 qemu_mutex_lock(&qemu_global_mutex
);
2410 qemu_wait_io_event_common(env
);
2413 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2420 ts
.tv_sec
= timeout
/ 1000;
2421 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2423 sigemptyset(&waitset
);
2424 sigaddset(&waitset
, SIG_IPI
);
2426 qemu_mutex_unlock(&qemu_global_mutex
);
2427 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2429 qemu_mutex_lock(&qemu_global_mutex
);
2431 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2432 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2437 static void qemu_kvm_wait_io_event(CPUState
*env
)
2439 while (!cpu_has_work(env
))
2440 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2442 qemu_kvm_eat_signal(env
, 0);
2443 qemu_wait_io_event_common(env
);
2446 static int qemu_cpu_exec(CPUState
*env
);
2448 static void *kvm_cpu_thread_fn(void *arg
)
2450 CPUState
*env
= arg
;
2452 qemu_thread_self(env
->thread
);
2456 kvm_block_io_signals(env
);
2458 /* signal CPU creation */
2459 qemu_mutex_lock(&qemu_global_mutex
);
2461 qemu_cond_signal(&qemu_cpu_cond
);
2463 /* and wait for machine initialization */
2464 while (!qemu_system_ready
)
2465 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2468 if (cpu_can_run(env
))
2470 qemu_kvm_wait_io_event(env
);
2476 static bool tcg_cpu_exec(void);
2478 static void *tcg_cpu_thread_fn(void *arg
)
2480 CPUState
*env
= arg
;
2482 tcg_block_io_signals();
2483 qemu_thread_self(env
->thread
);
2485 /* signal CPU creation */
2486 qemu_mutex_lock(&qemu_global_mutex
);
2487 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2489 qemu_cond_signal(&qemu_cpu_cond
);
2491 /* and wait for machine initialization */
2492 while (!qemu_system_ready
)
2493 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2497 qemu_wait_io_event(cur_cpu
);
2503 void qemu_cpu_kick(void *_env
)
2505 CPUState
*env
= _env
;
2506 qemu_cond_broadcast(env
->halt_cond
);
2508 qemu_thread_signal(env
->thread
, SIG_IPI
);
2511 int qemu_cpu_self(void *_env
)
2513 CPUState
*env
= _env
;
2516 qemu_thread_self(&this);
2518 return qemu_thread_equal(&this, env
->thread
);
2521 static void cpu_signal(int sig
)
2524 cpu_exit(cpu_single_env
);
2527 static void tcg_block_io_signals(void)
2530 struct sigaction sigact
;
2533 sigaddset(&set
, SIGUSR2
);
2534 sigaddset(&set
, SIGIO
);
2535 sigaddset(&set
, SIGALRM
);
2536 sigaddset(&set
, SIGCHLD
);
2537 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2540 sigaddset(&set
, SIG_IPI
);
2541 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2543 memset(&sigact
, 0, sizeof(sigact
));
2544 sigact
.sa_handler
= cpu_signal
;
2545 sigaction(SIG_IPI
, &sigact
, NULL
);
2548 static void dummy_signal(int sig
)
2552 static void kvm_block_io_signals(CPUState
*env
)
2556 struct sigaction sigact
;
2559 sigaddset(&set
, SIGUSR2
);
2560 sigaddset(&set
, SIGIO
);
2561 sigaddset(&set
, SIGALRM
);
2562 sigaddset(&set
, SIGCHLD
);
2563 sigaddset(&set
, SIG_IPI
);
2564 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2566 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2567 sigdelset(&set
, SIG_IPI
);
2569 memset(&sigact
, 0, sizeof(sigact
));
2570 sigact
.sa_handler
= dummy_signal
;
2571 sigaction(SIG_IPI
, &sigact
, NULL
);
2573 r
= kvm_set_signal_mask(env
, &set
);
2575 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2580 static void unblock_io_signals(void)
2585 sigaddset(&set
, SIGUSR2
);
2586 sigaddset(&set
, SIGIO
);
2587 sigaddset(&set
, SIGALRM
);
2588 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2591 sigaddset(&set
, SIG_IPI
);
2592 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2595 static void qemu_signal_lock(unsigned int msecs
)
2597 qemu_mutex_lock(&qemu_fair_mutex
);
2599 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2600 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2601 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2604 qemu_mutex_unlock(&qemu_fair_mutex
);
2607 void qemu_mutex_lock_iothread(void)
2609 if (kvm_enabled()) {
2610 qemu_mutex_lock(&qemu_fair_mutex
);
2611 qemu_mutex_lock(&qemu_global_mutex
);
2612 qemu_mutex_unlock(&qemu_fair_mutex
);
2614 qemu_signal_lock(100);
2617 void qemu_mutex_unlock_iothread(void)
2619 qemu_mutex_unlock(&qemu_global_mutex
);
2622 static int all_vcpus_paused(void)
2624 CPUState
*penv
= first_cpu
;
2629 penv
= (CPUState
*)penv
->next_cpu
;
2635 static void pause_all_vcpus(void)
2637 CPUState
*penv
= first_cpu
;
2641 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2642 qemu_cpu_kick(penv
);
2643 penv
= (CPUState
*)penv
->next_cpu
;
2646 while (!all_vcpus_paused()) {
2647 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2650 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2651 penv
= (CPUState
*)penv
->next_cpu
;
2656 static void resume_all_vcpus(void)
2658 CPUState
*penv
= first_cpu
;
2663 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2664 qemu_cpu_kick(penv
);
2665 penv
= (CPUState
*)penv
->next_cpu
;
2669 static void tcg_init_vcpu(void *_env
)
2671 CPUState
*env
= _env
;
2672 /* share a single thread for all cpus with TCG */
2673 if (!tcg_cpu_thread
) {
2674 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2675 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2676 qemu_cond_init(env
->halt_cond
);
2677 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2678 while (env
->created
== 0)
2679 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2680 tcg_cpu_thread
= env
->thread
;
2681 tcg_halt_cond
= env
->halt_cond
;
2683 env
->thread
= tcg_cpu_thread
;
2684 env
->halt_cond
= tcg_halt_cond
;
2688 static void kvm_start_vcpu(CPUState
*env
)
2690 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2691 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2692 qemu_cond_init(env
->halt_cond
);
2693 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2694 while (env
->created
== 0)
2695 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2698 void qemu_init_vcpu(void *_env
)
2700 CPUState
*env
= _env
;
2702 env
->nr_cores
= smp_cores
;
2703 env
->nr_threads
= smp_threads
;
2705 kvm_start_vcpu(env
);
2710 void qemu_notify_event(void)
2712 qemu_event_increment();
2715 static void qemu_system_vmstop_request(int reason
)
2717 vmstop_requested
= reason
;
2718 qemu_notify_event();
2721 void vm_stop(int reason
)
2724 qemu_thread_self(&me
);
2726 if (!qemu_thread_equal(&me
, &io_thread
)) {
2727 qemu_system_vmstop_request(reason
);
2729 * FIXME: should not return to device code in case
2730 * vm_stop() has been requested.
2732 if (cpu_single_env
) {
2733 cpu_exit(cpu_single_env
);
2734 cpu_single_env
->stop
= 1;
2745 static void host_main_loop_wait(int *timeout
)
2751 /* XXX: need to suppress polling by better using win32 events */
2753 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2754 ret
|= pe
->func(pe
->opaque
);
2758 WaitObjects
*w
= &wait_objects
;
2760 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2761 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2762 if (w
->func
[ret
- WAIT_OBJECT_0
])
2763 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2765 /* Check for additional signaled events */
2766 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2768 /* Check if event is signaled */
2769 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2770 if(ret2
== WAIT_OBJECT_0
) {
2772 w
->func
[i
](w
->opaque
[i
]);
2773 } else if (ret2
== WAIT_TIMEOUT
) {
2775 err
= GetLastError();
2776 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2779 } else if (ret
== WAIT_TIMEOUT
) {
2781 err
= GetLastError();
2782 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2789 static void host_main_loop_wait(int *timeout
)
2794 void main_loop_wait(int nonblocking
)
2796 IOHandlerRecord
*ioh
;
2797 fd_set rfds
, wfds
, xfds
;
2805 timeout
= qemu_calculate_timeout();
2806 qemu_bh_update_timeout(&timeout
);
2809 host_main_loop_wait(&timeout
);
2811 /* poll any events */
2812 /* XXX: separate device handlers from system ones */
2817 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2821 (!ioh
->fd_read_poll
||
2822 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2823 FD_SET(ioh
->fd
, &rfds
);
2827 if (ioh
->fd_write
) {
2828 FD_SET(ioh
->fd
, &wfds
);
2834 tv
.tv_sec
= timeout
/ 1000;
2835 tv
.tv_usec
= (timeout
% 1000) * 1000;
2837 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2839 qemu_mutex_unlock_iothread();
2840 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2841 qemu_mutex_lock_iothread();
2843 IOHandlerRecord
*pioh
;
2845 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2847 QLIST_REMOVE(ioh
, next
);
2851 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2852 ioh
->fd_read(ioh
->opaque
);
2854 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2855 ioh
->fd_write(ioh
->opaque
);
2860 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2862 qemu_run_all_timers();
2864 /* Check bottom-halves last in case any of the earlier events triggered
2870 static int qemu_cpu_exec(CPUState
*env
)
2873 #ifdef CONFIG_PROFILER
2877 #ifdef CONFIG_PROFILER
2878 ti
= profile_getclock();
2883 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2884 env
->icount_decr
.u16
.low
= 0;
2885 env
->icount_extra
= 0;
2886 count
= qemu_icount_round (qemu_next_deadline());
2887 qemu_icount
+= count
;
2888 decr
= (count
> 0xffff) ? 0xffff : count
;
2890 env
->icount_decr
.u16
.low
= decr
;
2891 env
->icount_extra
= count
;
2893 ret
= cpu_exec(env
);
2894 #ifdef CONFIG_PROFILER
2895 qemu_time
+= profile_getclock() - ti
;
2898 /* Fold pending instructions back into the
2899 instruction counter, and clear the interrupt flag. */
2900 qemu_icount
-= (env
->icount_decr
.u16
.low
2901 + env
->icount_extra
);
2902 env
->icount_decr
.u32
= 0;
2903 env
->icount_extra
= 0;
2908 static bool tcg_cpu_exec(void)
2912 if (next_cpu
== NULL
)
2913 next_cpu
= first_cpu
;
2914 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2915 CPUState
*env
= cur_cpu
= next_cpu
;
2917 qemu_clock_enable(vm_clock
,
2918 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2920 if (qemu_alarm_pending())
2922 if (cpu_can_run(env
))
2923 ret
= qemu_cpu_exec(env
);
2927 if (ret
== EXCP_DEBUG
) {
2928 gdb_set_stop_cpu(env
);
2929 debug_requested
= 1;
2933 return tcg_has_work();
2936 static int vm_can_run(void)
2938 if (powerdown_requested
)
2940 if (reset_requested
)
2942 if (shutdown_requested
)
2944 if (debug_requested
)
2949 qemu_irq qemu_system_powerdown
;
2951 static void main_loop(void)
2955 #ifdef CONFIG_IOTHREAD
2956 qemu_system_ready
= 1;
2957 qemu_cond_broadcast(&qemu_system_cond
);
2962 bool nonblocking
= false;
2963 #ifdef CONFIG_PROFILER
2966 #ifndef CONFIG_IOTHREAD
2967 nonblocking
= tcg_cpu_exec();
2969 #ifdef CONFIG_PROFILER
2970 ti
= profile_getclock();
2972 main_loop_wait(nonblocking
);
2973 #ifdef CONFIG_PROFILER
2974 dev_time
+= profile_getclock() - ti
;
2976 } while (vm_can_run());
2978 if (qemu_debug_requested()) {
2979 vm_stop(EXCP_DEBUG
);
2981 if (qemu_shutdown_requested()) {
2982 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2989 if (qemu_reset_requested()) {
2991 qemu_system_reset();
2994 if (qemu_powerdown_requested()) {
2995 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2996 qemu_irq_raise(qemu_system_powerdown
);
2998 if ((r
= qemu_vmstop_requested())) {
3005 static void version(void)
3007 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3010 static void help(int exitcode
)
3012 const char *options_help
=
3013 #define DEF(option, opt_arg, opt_enum, opt_help) \
3015 #define DEFHEADING(text) stringify(text) "\n"
3016 #include "qemu-options.h"
3022 printf("usage: %s [options] [disk_image]\n"
3024 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3027 "During emulation, the following keys are useful:\n"
3028 "ctrl-alt-f toggle full screen\n"
3029 "ctrl-alt-n switch to virtual console 'n'\n"
3030 "ctrl-alt toggle mouse and keyboard grab\n"
3032 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3038 #define HAS_ARG 0x0001
3041 #define DEF(option, opt_arg, opt_enum, opt_help) \
3043 #define DEFHEADING(text)
3044 #include "qemu-options.h"
3050 typedef struct QEMUOption
{
3056 static const QEMUOption qemu_options
[] = {
3057 { "h", 0, QEMU_OPTION_h
},
3058 #define DEF(option, opt_arg, opt_enum, opt_help) \
3059 { option, opt_arg, opt_enum },
3060 #define DEFHEADING(text)
3061 #include "qemu-options.h"
3069 struct soundhw soundhw
[] = {
3070 #ifdef HAS_AUDIO_CHOICE
3071 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3077 { .init_isa
= pcspk_audio_init
}
3084 "Creative Sound Blaster 16",
3087 { .init_isa
= SB16_init
}
3091 #ifdef CONFIG_CS4231A
3097 { .init_isa
= cs4231a_init
}
3105 "Yamaha YMF262 (OPL3)",
3107 "Yamaha YM3812 (OPL2)",
3111 { .init_isa
= Adlib_init
}
3118 "Gravis Ultrasound GF1",
3121 { .init_isa
= GUS_init
}
3128 "Intel 82801AA AC97 Audio",
3131 { .init_pci
= ac97_init
}
3135 #ifdef CONFIG_ES1370
3138 "ENSONIQ AudioPCI ES1370",
3141 { .init_pci
= es1370_init
}
3145 #endif /* HAS_AUDIO_CHOICE */
3147 { NULL
, NULL
, 0, 0, { NULL
} }
3150 static void select_soundhw (const char *optarg
)
3154 if (*optarg
== '?') {
3157 printf ("Valid sound card names (comma separated):\n");
3158 for (c
= soundhw
; c
->name
; ++c
) {
3159 printf ("%-11s %s\n", c
->name
, c
->descr
);
3161 printf ("\n-soundhw all will enable all of the above\n");
3162 exit (*optarg
!= '?');
3170 if (!strcmp (optarg
, "all")) {
3171 for (c
= soundhw
; c
->name
; ++c
) {
3179 e
= strchr (p
, ',');
3180 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3182 for (c
= soundhw
; c
->name
; ++c
) {
3183 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3192 "Unknown sound card name (too big to show)\n");
3195 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3200 p
+= l
+ (e
!= NULL
);
3204 goto show_valid_cards
;
3209 static void select_vgahw (const char *p
)
3214 vga_interface_type
= VGA_NONE
;
3215 if (strstart(p
, "std", &opts
)) {
3216 vga_interface_type
= VGA_STD
;
3217 } else if (strstart(p
, "cirrus", &opts
)) {
3218 vga_interface_type
= VGA_CIRRUS
;
3219 } else if (strstart(p
, "vmware", &opts
)) {
3220 vga_interface_type
= VGA_VMWARE
;
3221 } else if (strstart(p
, "xenfb", &opts
)) {
3222 vga_interface_type
= VGA_XENFB
;
3223 } else if (!strstart(p
, "none", &opts
)) {
3225 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3229 const char *nextopt
;
3231 if (strstart(opts
, ",retrace=", &nextopt
)) {
3233 if (strstart(opts
, "dumb", &nextopt
))
3234 vga_retrace_method
= VGA_RETRACE_DUMB
;
3235 else if (strstart(opts
, "precise", &nextopt
))
3236 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3237 else goto invalid_vga
;
3238 } else goto invalid_vga
;
3244 static int balloon_parse(const char *arg
)
3248 if (strcmp(arg
, "none") == 0) {
3252 if (!strncmp(arg
, "virtio", 6)) {
3253 if (arg
[6] == ',') {
3254 /* have params -> parse them */
3255 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3259 /* create empty opts */
3260 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3262 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3271 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3273 exit(STATUS_CONTROL_C_EXIT
);
3278 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3282 if(strlen(str
) != 36)
3285 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3286 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3287 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3293 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3301 static void termsig_handler(int signal
)
3303 qemu_system_shutdown_request();
3306 static void sigchld_handler(int signal
)
3308 waitpid(-1, NULL
, WNOHANG
);
3311 static void sighandler_setup(void)
3313 struct sigaction act
;
3315 memset(&act
, 0, sizeof(act
));
3316 act
.sa_handler
= termsig_handler
;
3317 sigaction(SIGINT
, &act
, NULL
);
3318 sigaction(SIGHUP
, &act
, NULL
);
3319 sigaction(SIGTERM
, &act
, NULL
);
3321 act
.sa_handler
= sigchld_handler
;
3322 act
.sa_flags
= SA_NOCLDSTOP
;
3323 sigaction(SIGCHLD
, &act
, NULL
);
3329 /* Look for support files in the same directory as the executable. */
3330 static char *find_datadir(const char *argv0
)
3336 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3343 while (p
!= buf
&& *p
!= '\\')
3346 if (access(buf
, R_OK
) == 0) {
3347 return qemu_strdup(buf
);
3353 /* Find a likely location for support files using the location of the binary.
3354 For installed binaries this will be "$bindir/../share/qemu". When
3355 running from the build tree this will be "$bindir/../pc-bios". */
3356 #define SHARE_SUFFIX "/share/qemu"
3357 #define BUILD_SUFFIX "/pc-bios"
3358 static char *find_datadir(const char *argv0
)
3366 #if defined(__linux__)
3369 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3375 #elif defined(__FreeBSD__)
3378 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3385 /* If we don't have any way of figuring out the actual executable
3386 location then try argv[0]. */
3388 p
= realpath(argv0
, buf
);
3396 max_len
= strlen(dir
) +
3397 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3398 res
= qemu_mallocz(max_len
);
3399 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3400 if (access(res
, R_OK
)) {
3401 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3402 if (access(res
, R_OK
)) {
3414 char *qemu_find_file(int type
, const char *name
)
3420 /* If name contains path separators then try it as a straight path. */
3421 if ((strchr(name
, '/') || strchr(name
, '\\'))
3422 && access(name
, R_OK
) == 0) {
3423 return qemu_strdup(name
);
3426 case QEMU_FILE_TYPE_BIOS
:
3429 case QEMU_FILE_TYPE_KEYMAP
:
3430 subdir
= "keymaps/";
3435 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3436 buf
= qemu_mallocz(len
);
3437 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3438 if (access(buf
, R_OK
)) {
3445 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3447 return qdev_device_help(opts
);
3450 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3454 dev
= qdev_device_add(opts
);
3460 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3462 CharDriverState
*chr
;
3464 chr
= qemu_chr_open_opts(opts
, NULL
);
3470 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3472 CharDriverState
*chr
;
3473 const char *chardev
;
3477 mode
= qemu_opt_get(opts
, "mode");
3481 if (strcmp(mode
, "readline") == 0) {
3482 flags
= MONITOR_USE_READLINE
;
3483 } else if (strcmp(mode
, "control") == 0) {
3484 flags
= MONITOR_USE_CONTROL
;
3486 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3490 if (qemu_opt_get_bool(opts
, "default", 0))
3491 flags
|= MONITOR_IS_DEFAULT
;
3493 chardev
= qemu_opt_get(opts
, "chardev");
3494 chr
= qemu_chr_find(chardev
);
3496 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3500 monitor_init(chr
, flags
);
3504 static void monitor_parse(const char *optarg
, const char *mode
)
3506 static int monitor_device_index
= 0;
3512 if (strstart(optarg
, "chardev:", &p
)) {
3513 snprintf(label
, sizeof(label
), "%s", p
);
3515 if (monitor_device_index
) {
3516 snprintf(label
, sizeof(label
), "monitor%d",
3517 monitor_device_index
);
3519 snprintf(label
, sizeof(label
), "monitor");
3522 opts
= qemu_chr_parse_compat(label
, optarg
);
3524 fprintf(stderr
, "parse error: %s\n", optarg
);
3529 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3531 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3534 qemu_opt_set(opts
, "mode", mode
);
3535 qemu_opt_set(opts
, "chardev", label
);
3537 qemu_opt_set(opts
, "default", "on");
3538 monitor_device_index
++;
3541 struct device_config
{
3543 DEV_USB
, /* -usbdevice */
3545 DEV_SERIAL
, /* -serial */
3546 DEV_PARALLEL
, /* -parallel */
3547 DEV_VIRTCON
, /* -virtioconsole */
3548 DEV_DEBUGCON
, /* -debugcon */
3550 const char *cmdline
;
3551 QTAILQ_ENTRY(device_config
) next
;
3553 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3555 static void add_device_config(int type
, const char *cmdline
)
3557 struct device_config
*conf
;
3559 conf
= qemu_mallocz(sizeof(*conf
));
3561 conf
->cmdline
= cmdline
;
3562 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3565 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3567 struct device_config
*conf
;
3570 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3571 if (conf
->type
!= type
)
3573 rc
= func(conf
->cmdline
);
3580 static int serial_parse(const char *devname
)
3582 static int index
= 0;
3585 if (strcmp(devname
, "none") == 0)
3587 if (index
== MAX_SERIAL_PORTS
) {
3588 fprintf(stderr
, "qemu: too many serial ports\n");
3591 snprintf(label
, sizeof(label
), "serial%d", index
);
3592 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3593 if (!serial_hds
[index
]) {
3594 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3595 devname
, strerror(errno
));
3602 static int parallel_parse(const char *devname
)
3604 static int index
= 0;
3607 if (strcmp(devname
, "none") == 0)
3609 if (index
== MAX_PARALLEL_PORTS
) {
3610 fprintf(stderr
, "qemu: too many parallel ports\n");
3613 snprintf(label
, sizeof(label
), "parallel%d", index
);
3614 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3615 if (!parallel_hds
[index
]) {
3616 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3617 devname
, strerror(errno
));
3624 static int virtcon_parse(const char *devname
)
3626 static int index
= 0;
3628 QemuOpts
*bus_opts
, *dev_opts
;
3630 if (strcmp(devname
, "none") == 0)
3632 if (index
== MAX_VIRTIO_CONSOLES
) {
3633 fprintf(stderr
, "qemu: too many virtio consoles\n");
3637 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3638 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3640 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3641 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3643 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3644 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3645 if (!virtcon_hds
[index
]) {
3646 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3647 devname
, strerror(errno
));
3650 qemu_opt_set(dev_opts
, "chardev", label
);
3656 static int debugcon_parse(const char *devname
)
3660 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3663 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3665 fprintf(stderr
, "qemu: already have a debugcon device\n");
3668 qemu_opt_set(opts
, "driver", "isa-debugcon");
3669 qemu_opt_set(opts
, "chardev", "debugcon");
3673 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3674 const char **poptarg
, int *poptind
)
3676 const QEMUOption
*popt
;
3677 int optind
= *poptind
;
3678 char *r
= argv
[optind
];
3681 loc_set_cmdline(argv
, optind
, 1);
3683 /* Treat --foo the same as -foo. */
3686 popt
= qemu_options
;
3689 error_report("invalid option");
3692 if (!strcmp(popt
->name
, r
+ 1))
3696 if (popt
->flags
& HAS_ARG
) {
3697 if (optind
>= argc
) {
3698 error_report("requires an argument");
3701 optarg
= argv
[optind
++];
3702 loc_set_cmdline(argv
, optind
- 2, 2);
3713 static void qmp_add_default(void)
3717 static uint8_t null_uuid
[16];
3721 home
= getenv("HOME");
3726 if (memcmp(qemu_uuid
, null_uuid
, sizeof(null_uuid
)) == 0) {
3727 #if defined(CONFIG_UUID)
3728 uuid_generate(uuid
);
3733 memcpy(uuid
, qemu_uuid
, sizeof(qemu_uuid
));
3736 snprintf(buffer
, sizeof(buffer
), "%s/.qemu", home
);
3738 ret
= mkdir(buffer
);
3740 ret
= mkdir(buffer
, 0755);
3742 if (ret
== -1 && errno
!= EEXIST
) {
3743 fprintf(stderr
, "could not open default QMP port\n");
3747 snprintf(buffer
, sizeof(buffer
), "%s/.qemu/qmp", home
);
3749 ret
= mkdir(buffer
);
3751 ret
= mkdir(buffer
, 0755);
3753 if (ret
== -1 && errno
!= EEXIST
) {
3754 fprintf(stderr
, "could not open default QMP port\n");
3758 snprintf(buffer
, sizeof(buffer
),
3759 "unix:%s/.qemu/qmp/" UUID_FMT
".sock,server,nowait",
3761 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
3762 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
3763 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
3764 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
3766 monitor_parse(buffer
, "control");
3769 int main(int argc
, char **argv
, char **envp
)
3771 const char *gdbstub_dev
= NULL
;
3772 uint32_t boot_devices_bitmap
= 0;
3774 int snapshot
, linux_boot
, net_boot
;
3775 const char *icount_option
= NULL
;
3776 const char *initrd_filename
;
3777 const char *kernel_filename
, *kernel_cmdline
;
3778 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3780 DisplayChangeListener
*dcl
;
3781 int cyls
, heads
, secs
, translation
;
3782 QemuOpts
*hda_opts
= NULL
, *opts
;
3785 const char *loadvm
= NULL
;
3786 QEMUMachine
*machine
;
3787 const char *cpu_model
;
3792 const char *pid_file
= NULL
;
3793 const char *incoming
= NULL
;
3796 struct passwd
*pwd
= NULL
;
3797 const char *chroot_dir
= NULL
;
3798 const char *run_as
= NULL
;
3801 int show_vnc_port
= 0;
3804 error_set_progname(argv
[0]);
3806 exit_notifier_init();
3810 qemu_cache_utils_init(envp
);
3812 QLIST_INIT (&vm_change_state_head
);
3815 struct sigaction act
;
3816 sigfillset(&act
.sa_mask
);
3818 act
.sa_handler
= SIG_IGN
;
3819 sigaction(SIGPIPE
, &act
, NULL
);
3822 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3823 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3824 QEMU to run on a single CPU */
3829 h
= GetCurrentProcess();
3830 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3831 for(i
= 0; i
< 32; i
++) {
3832 if (mask
& (1 << i
))
3837 SetProcessAffinityMask(h
, mask
);
3843 module_call_init(MODULE_INIT_MACHINE
);
3844 machine
= find_default_machine();
3846 initrd_filename
= NULL
;
3849 kernel_filename
= NULL
;
3850 kernel_cmdline
= "";
3851 cyls
= heads
= secs
= 0;
3852 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3854 for (i
= 0; i
< MAX_NODES
; i
++) {
3856 node_cpumask
[i
] = 0;
3865 /* first pass of option parsing */
3867 while (optind
< argc
) {
3868 if (argv
[optind
][0] != '-') {
3873 const QEMUOption
*popt
;
3875 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3876 switch (popt
->index
) {
3877 case QEMU_OPTION_nodefconfig
:
3888 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3889 fp
= fopen(fname
, "r");
3891 if (qemu_config_parse(fp
, fname
) != 0) {
3897 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3898 fp
= fopen(fname
, "r");
3900 if (qemu_config_parse(fp
, fname
) != 0) {
3906 #if defined(cpudef_setup)
3907 cpudef_setup(); /* parse cpu definitions in target config file */
3910 /* second pass of option parsing */
3915 if (argv
[optind
][0] != '-') {
3916 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3918 const QEMUOption
*popt
;
3920 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3921 switch(popt
->index
) {
3923 machine
= find_machine(optarg
);
3926 printf("Supported machines are:\n");
3927 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3929 printf("%-10s %s (alias of %s)\n",
3930 m
->alias
, m
->desc
, m
->name
);
3931 printf("%-10s %s%s\n",
3933 m
->is_default
? " (default)" : "");
3935 exit(*optarg
!= '?');
3938 case QEMU_OPTION_cpu
:
3939 /* hw initialization will check this */
3940 if (*optarg
== '?') {
3941 /* XXX: implement xxx_cpu_list for targets that still miss it */
3942 #if defined(cpu_list_id)
3943 cpu_list_id(stdout
, &fprintf
, optarg
);
3944 #elif defined(cpu_list)
3945 cpu_list(stdout
, &fprintf
); /* deprecated */
3952 case QEMU_OPTION_initrd
:
3953 initrd_filename
= optarg
;
3955 case QEMU_OPTION_hda
:
3957 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3959 hda_opts
= drive_add(optarg
, HD_ALIAS
3960 ",cyls=%d,heads=%d,secs=%d%s",
3961 0, cyls
, heads
, secs
,
3962 translation
== BIOS_ATA_TRANSLATION_LBA
?
3964 translation
== BIOS_ATA_TRANSLATION_NONE
?
3965 ",trans=none" : "");
3967 case QEMU_OPTION_hdb
:
3968 case QEMU_OPTION_hdc
:
3969 case QEMU_OPTION_hdd
:
3970 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3972 case QEMU_OPTION_drive
:
3973 drive_add(NULL
, "%s", optarg
);
3975 case QEMU_OPTION_set
:
3976 if (qemu_set_option(optarg
) != 0)
3979 case QEMU_OPTION_global
:
3980 if (qemu_global_option(optarg
) != 0)
3983 case QEMU_OPTION_mtdblock
:
3984 drive_add(optarg
, MTD_ALIAS
);
3986 case QEMU_OPTION_sd
:
3987 drive_add(optarg
, SD_ALIAS
);
3989 case QEMU_OPTION_pflash
:
3990 drive_add(optarg
, PFLASH_ALIAS
);
3992 case QEMU_OPTION_snapshot
:
3995 case QEMU_OPTION_hdachs
:
3999 cyls
= strtol(p
, (char **)&p
, 0);
4000 if (cyls
< 1 || cyls
> 16383)
4005 heads
= strtol(p
, (char **)&p
, 0);
4006 if (heads
< 1 || heads
> 16)
4011 secs
= strtol(p
, (char **)&p
, 0);
4012 if (secs
< 1 || secs
> 63)
4016 if (!strcmp(p
, "none"))
4017 translation
= BIOS_ATA_TRANSLATION_NONE
;
4018 else if (!strcmp(p
, "lba"))
4019 translation
= BIOS_ATA_TRANSLATION_LBA
;
4020 else if (!strcmp(p
, "auto"))
4021 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4024 } else if (*p
!= '\0') {
4026 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4029 if (hda_opts
!= NULL
) {
4031 snprintf(num
, sizeof(num
), "%d", cyls
);
4032 qemu_opt_set(hda_opts
, "cyls", num
);
4033 snprintf(num
, sizeof(num
), "%d", heads
);
4034 qemu_opt_set(hda_opts
, "heads", num
);
4035 snprintf(num
, sizeof(num
), "%d", secs
);
4036 qemu_opt_set(hda_opts
, "secs", num
);
4037 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4038 qemu_opt_set(hda_opts
, "trans", "lba");
4039 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4040 qemu_opt_set(hda_opts
, "trans", "none");
4044 case QEMU_OPTION_numa
:
4045 if (nb_numa_nodes
>= MAX_NODES
) {
4046 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4051 case QEMU_OPTION_nographic
:
4052 display_type
= DT_NOGRAPHIC
;
4054 #ifdef CONFIG_CURSES
4055 case QEMU_OPTION_curses
:
4056 display_type
= DT_CURSES
;
4059 case QEMU_OPTION_portrait
:
4062 case QEMU_OPTION_kernel
:
4063 kernel_filename
= optarg
;
4065 case QEMU_OPTION_append
:
4066 kernel_cmdline
= optarg
;
4068 case QEMU_OPTION_cdrom
:
4069 drive_add(optarg
, CDROM_ALIAS
);
4071 case QEMU_OPTION_boot
:
4073 static const char * const params
[] = {
4074 "order", "once", "menu", NULL
4076 char buf
[sizeof(boot_devices
)];
4077 char *standard_boot_devices
;
4080 if (!strchr(optarg
, '=')) {
4082 pstrcpy(buf
, sizeof(buf
), optarg
);
4083 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4085 "qemu: unknown boot parameter '%s' in '%s'\n",
4091 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4092 boot_devices_bitmap
= parse_bootdevices(buf
);
4093 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4096 if (get_param_value(buf
, sizeof(buf
),
4098 boot_devices_bitmap
|= parse_bootdevices(buf
);
4099 standard_boot_devices
= qemu_strdup(boot_devices
);
4100 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4101 qemu_register_reset(restore_boot_devices
,
4102 standard_boot_devices
);
4104 if (get_param_value(buf
, sizeof(buf
),
4106 if (!strcmp(buf
, "on")) {
4108 } else if (!strcmp(buf
, "off")) {
4112 "qemu: invalid option value '%s'\n",
4120 case QEMU_OPTION_fda
:
4121 case QEMU_OPTION_fdb
:
4122 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4125 case QEMU_OPTION_no_fd_bootchk
:
4129 case QEMU_OPTION_netdev
:
4130 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4134 case QEMU_OPTION_net
:
4135 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4140 case QEMU_OPTION_tftp
:
4141 legacy_tftp_prefix
= optarg
;
4143 case QEMU_OPTION_bootp
:
4144 legacy_bootp_filename
= optarg
;
4147 case QEMU_OPTION_smb
:
4148 if (net_slirp_smb(optarg
) < 0)
4152 case QEMU_OPTION_redir
:
4153 if (net_slirp_redir(optarg
) < 0)
4157 case QEMU_OPTION_bt
:
4158 add_device_config(DEV_BT
, optarg
);
4161 case QEMU_OPTION_audio_help
:
4165 case QEMU_OPTION_soundhw
:
4166 select_soundhw (optarg
);
4172 case QEMU_OPTION_version
:
4176 case QEMU_OPTION_m
: {
4180 value
= strtoul(optarg
, &ptr
, 10);
4182 case 0: case 'M': case 'm':
4189 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4193 /* On 32-bit hosts, QEMU is limited by virtual address space */
4194 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4195 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4198 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4199 fprintf(stderr
, "qemu: ram size too large\n");
4205 case QEMU_OPTION_mempath
:
4209 case QEMU_OPTION_mem_prealloc
:
4216 const CPULogItem
*item
;
4218 mask
= cpu_str_to_log_mask(optarg
);
4220 printf("Log items (comma separated):\n");
4221 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4222 printf("%-10s %s\n", item
->name
, item
->help
);
4230 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4232 case QEMU_OPTION_gdb
:
4233 gdbstub_dev
= optarg
;
4238 case QEMU_OPTION_bios
:
4241 case QEMU_OPTION_singlestep
:
4248 keyboard_layout
= optarg
;
4250 case QEMU_OPTION_localtime
:
4253 case QEMU_OPTION_vga
:
4254 select_vgahw (optarg
);
4256 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4262 w
= strtol(p
, (char **)&p
, 10);
4265 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4271 h
= strtol(p
, (char **)&p
, 10);
4276 depth
= strtol(p
, (char **)&p
, 10);
4277 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4278 depth
!= 24 && depth
!= 32)
4280 } else if (*p
== '\0') {
4281 depth
= graphic_depth
;
4288 graphic_depth
= depth
;
4292 case QEMU_OPTION_echr
:
4295 term_escape_char
= strtol(optarg
, &r
, 0);
4297 printf("Bad argument to echr\n");
4300 case QEMU_OPTION_monitor
:
4301 monitor_parse(optarg
, "readline");
4302 default_monitor
= 0;
4304 case QEMU_OPTION_qmp
:
4305 monitor_parse(optarg
, "control");
4308 case QEMU_OPTION_mon
:
4309 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4311 fprintf(stderr
, "parse error: %s\n", optarg
);
4315 mode
= qemu_opt_get(opts
, "mode");
4316 if (mode
== NULL
|| strcmp(mode
, "readline") == 0) {
4317 default_monitor
= 0;
4318 } else if (strcmp(mode
, "control") == 0) {
4323 case QEMU_OPTION_chardev
:
4324 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4326 fprintf(stderr
, "parse error: %s\n", optarg
);
4330 case QEMU_OPTION_serial
:
4331 add_device_config(DEV_SERIAL
, optarg
);
4333 if (strncmp(optarg
, "mon:", 4) == 0) {
4334 default_monitor
= 0;
4337 case QEMU_OPTION_watchdog
:
4340 "qemu: only one watchdog option may be given\n");
4345 case QEMU_OPTION_watchdog_action
:
4346 if (select_watchdog_action(optarg
) == -1) {
4347 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4351 case QEMU_OPTION_virtiocon
:
4352 add_device_config(DEV_VIRTCON
, optarg
);
4353 default_virtcon
= 0;
4354 if (strncmp(optarg
, "mon:", 4) == 0) {
4355 default_monitor
= 0;
4358 case QEMU_OPTION_parallel
:
4359 add_device_config(DEV_PARALLEL
, optarg
);
4360 default_parallel
= 0;
4361 if (strncmp(optarg
, "mon:", 4) == 0) {
4362 default_monitor
= 0;
4365 case QEMU_OPTION_debugcon
:
4366 add_device_config(DEV_DEBUGCON
, optarg
);
4368 case QEMU_OPTION_loadvm
:
4371 case QEMU_OPTION_full_screen
:
4375 case QEMU_OPTION_no_frame
:
4378 case QEMU_OPTION_alt_grab
:
4381 case QEMU_OPTION_ctrl_grab
:
4384 case QEMU_OPTION_no_quit
:
4387 case QEMU_OPTION_sdl
:
4388 display_type
= DT_SDL
;
4391 case QEMU_OPTION_pidfile
:
4395 case QEMU_OPTION_win2k_hack
:
4396 win2k_install_hack
= 1;
4398 case QEMU_OPTION_rtc_td_hack
:
4401 case QEMU_OPTION_acpitable
:
4402 if(acpi_table_add(optarg
) < 0) {
4403 fprintf(stderr
, "Wrong acpi table provided\n");
4407 case QEMU_OPTION_smbios
:
4408 if(smbios_entry_add(optarg
) < 0) {
4409 fprintf(stderr
, "Wrong smbios provided\n");
4415 case QEMU_OPTION_enable_kvm
:
4419 case QEMU_OPTION_usb
:
4422 case QEMU_OPTION_usbdevice
:
4424 add_device_config(DEV_USB
, optarg
);
4426 case QEMU_OPTION_device
:
4427 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4431 case QEMU_OPTION_smp
:
4434 fprintf(stderr
, "Invalid number of CPUs\n");
4437 if (max_cpus
< smp_cpus
) {
4438 fprintf(stderr
, "maxcpus must be equal to or greater than "
4442 if (max_cpus
> 255) {
4443 fprintf(stderr
, "Unsupported number of maxcpus\n");
4447 case QEMU_OPTION_vnc
:
4448 display_type
= DT_VNC
;
4449 vnc_display
= optarg
;
4452 case QEMU_OPTION_no_acpi
:
4455 case QEMU_OPTION_no_hpet
:
4458 case QEMU_OPTION_balloon
:
4459 if (balloon_parse(optarg
) < 0) {
4460 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4465 case QEMU_OPTION_no_reboot
:
4468 case QEMU_OPTION_no_shutdown
:
4471 case QEMU_OPTION_show_cursor
:
4474 case QEMU_OPTION_uuid
:
4475 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4476 fprintf(stderr
, "Fail to parse UUID string."
4477 " Wrong format.\n");
4482 case QEMU_OPTION_daemonize
:
4486 case QEMU_OPTION_option_rom
:
4487 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4488 fprintf(stderr
, "Too many option ROMs\n");
4491 option_rom
[nb_option_roms
] = optarg
;
4494 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4495 case QEMU_OPTION_semihosting
:
4496 semihosting_enabled
= 1;
4499 case QEMU_OPTION_name
:
4500 qemu_name
= qemu_strdup(optarg
);
4502 char *p
= strchr(qemu_name
, ',');
4505 if (strncmp(p
, "process=", 8)) {
4506 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4514 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4515 case QEMU_OPTION_prom_env
:
4516 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4517 fprintf(stderr
, "Too many prom variables\n");
4520 prom_envs
[nb_prom_envs
] = optarg
;
4525 case QEMU_OPTION_old_param
:
4529 case QEMU_OPTION_clock
:
4530 configure_alarms(optarg
);
4532 case QEMU_OPTION_startdate
:
4533 configure_rtc_date_offset(optarg
, 1);
4535 case QEMU_OPTION_rtc
:
4536 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4538 fprintf(stderr
, "parse error: %s\n", optarg
);
4541 configure_rtc(opts
);
4543 case QEMU_OPTION_tb_size
:
4544 tb_size
= strtol(optarg
, NULL
, 0);
4548 case QEMU_OPTION_icount
:
4549 icount_option
= optarg
;
4551 case QEMU_OPTION_incoming
:
4554 case QEMU_OPTION_nodefaults
:
4556 default_parallel
= 0;
4557 default_virtcon
= 0;
4558 default_monitor
= 0;
4567 case QEMU_OPTION_chroot
:
4568 chroot_dir
= optarg
;
4570 case QEMU_OPTION_runas
:
4575 case QEMU_OPTION_xen_domid
:
4576 xen_domid
= atoi(optarg
);
4578 case QEMU_OPTION_xen_create
:
4579 xen_mode
= XEN_CREATE
;
4581 case QEMU_OPTION_xen_attach
:
4582 xen_mode
= XEN_ATTACH
;
4585 case QEMU_OPTION_readconfig
:
4588 fp
= fopen(optarg
, "r");
4590 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4593 if (qemu_config_parse(fp
, optarg
) != 0) {
4599 case QEMU_OPTION_writeconfig
:
4602 if (strcmp(optarg
, "-") == 0) {
4605 fp
= fopen(optarg
, "w");
4607 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4611 qemu_config_write(fp
);
4620 /* If no data_dir is specified then try to find it relative to the
4623 data_dir
= find_datadir(argv
[0]);
4625 /* If all else fails use the install patch specified when building. */
4627 data_dir
= CONFIG_QEMU_SHAREDIR
;
4631 * Default to max_cpus = smp_cpus, in case the user doesn't
4632 * specify a max_cpus value.
4635 max_cpus
= smp_cpus
;
4637 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4638 if (smp_cpus
> machine
->max_cpus
) {
4639 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4640 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4645 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4646 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4648 if (machine
->no_serial
) {
4651 if (machine
->no_parallel
) {
4652 default_parallel
= 0;
4654 if (!machine
->use_virtcon
) {
4655 default_virtcon
= 0;
4657 if (machine
->no_vga
) {
4660 if (machine
->no_floppy
) {
4663 if (machine
->no_cdrom
) {
4666 if (machine
->no_sdcard
) {
4670 if (display_type
== DT_NOGRAPHIC
) {
4671 if (default_parallel
)
4672 add_device_config(DEV_PARALLEL
, "null");
4673 if (default_serial
&& default_monitor
) {
4674 add_device_config(DEV_SERIAL
, "mon:stdio");
4675 } else if (default_virtcon
&& default_monitor
) {
4676 add_device_config(DEV_VIRTCON
, "mon:stdio");
4679 add_device_config(DEV_SERIAL
, "stdio");
4680 if (default_virtcon
)
4681 add_device_config(DEV_VIRTCON
, "stdio");
4682 if (default_monitor
)
4683 monitor_parse("stdio", "readline");
4687 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4688 if (default_parallel
)
4689 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4690 if (default_monitor
)
4691 monitor_parse("vc:80Cx24C", "readline");
4692 if (default_virtcon
)
4693 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4699 vga_interface_type
= VGA_CIRRUS
;
4701 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4708 if (pipe(fds
) == -1)
4719 len
= read(fds
[0], &status
, 1);
4720 if (len
== -1 && (errno
== EINTR
))
4725 else if (status
== 1) {
4726 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4734 qemu_set_cloexec(fds
[1]);
4746 signal(SIGTSTP
, SIG_IGN
);
4747 signal(SIGTTOU
, SIG_IGN
);
4748 signal(SIGTTIN
, SIG_IGN
);
4752 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4756 if (write(fds
[1], &status
, 1) != 1) {
4757 perror("daemonize. Writing to pipe\n");
4761 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4765 if (kvm_enabled()) {
4768 ret
= kvm_init(smp_cpus
);
4770 fprintf(stderr
, "failed to initialize KVM\n");
4775 if (qemu_init_main_loop()) {
4776 fprintf(stderr
, "qemu_init_main_loop failed\n");
4779 linux_boot
= (kernel_filename
!= NULL
);
4781 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4782 fprintf(stderr
, "-append only allowed with -kernel option\n");
4786 if (!linux_boot
&& initrd_filename
!= NULL
) {
4787 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4792 /* Win32 doesn't support line-buffering and requires size >= 2 */
4793 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4796 if (init_timer_alarm() < 0) {
4797 fprintf(stderr
, "could not initialize alarm timer\n");
4800 configure_icount(icount_option
);
4806 if (net_init_clients() < 0) {
4810 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4811 net_set_boot_mask(net_boot
);
4813 /* init the bluetooth world */
4814 if (foreach_device_config(DEV_BT
, bt_parse
))
4817 /* init the memory */
4819 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4821 /* init the dynamic translator */
4822 cpu_exec_init_all(tb_size
* 1024 * 1024);
4824 bdrv_init_with_whitelist();
4828 if (default_cdrom
) {
4829 /* we always create the cdrom drive, even if no disk is there */
4830 drive_add(NULL
, CDROM_ALIAS
);
4833 if (default_floppy
) {
4834 /* we always create at least one floppy */
4835 drive_add(NULL
, FD_ALIAS
, 0);
4838 if (default_sdcard
) {
4839 /* we always create one sd slot, even if no card is in it */
4840 drive_add(NULL
, SD_ALIAS
);
4843 /* open the virtual block devices */
4845 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4846 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4849 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4852 if (nb_numa_nodes
> 0) {
4855 if (nb_numa_nodes
> smp_cpus
) {
4856 nb_numa_nodes
= smp_cpus
;
4859 /* If no memory size if given for any node, assume the default case
4860 * and distribute the available memory equally across all nodes
4862 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4863 if (node_mem
[i
] != 0)
4866 if (i
== nb_numa_nodes
) {
4867 uint64_t usedmem
= 0;
4869 /* On Linux, the each node's border has to be 8MB aligned,
4870 * the final node gets the rest.
4872 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4873 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4874 usedmem
+= node_mem
[i
];
4876 node_mem
[i
] = ram_size
- usedmem
;
4879 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4880 if (node_cpumask
[i
] != 0)
4883 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4884 * must cope with this anyway, because there are BIOSes out there in
4885 * real machines which also use this scheme.
4887 if (i
== nb_numa_nodes
) {
4888 for (i
= 0; i
< smp_cpus
; i
++) {
4889 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4894 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4896 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4898 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4900 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4903 module_call_init(MODULE_INIT_DEVICE
);
4905 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4909 i
= select_watchdog(watchdog
);
4911 exit (i
== 1 ? 1 : 0);
4914 if (machine
->compat_props
) {
4915 qdev_prop_register_global_list(machine
->compat_props
);
4919 machine
->init(ram_size
, boot_devices
,
4920 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4922 cpu_synchronize_all_post_init();
4925 /* must be after terminal init, SDL library changes signal handlers */
4929 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4930 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4931 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4937 current_machine
= machine
;
4939 /* init USB devices */
4941 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4945 /* init generic devices */
4946 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4949 net_check_clients();
4951 /* just use the first displaystate for the moment */
4952 ds
= get_displaystate();
4954 if (display_type
== DT_DEFAULT
) {
4955 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4956 display_type
= DT_SDL
;
4958 display_type
= DT_VNC
;
4959 vnc_display
= "localhost:0,to=99";
4965 switch (display_type
) {
4968 #if defined(CONFIG_CURSES)
4970 curses_display_init(ds
, full_screen
);
4973 #if defined(CONFIG_SDL)
4975 sdl_display_init(ds
, full_screen
, no_frame
);
4977 #elif defined(CONFIG_COCOA)
4979 cocoa_display_init(ds
, full_screen
);
4983 vnc_display_init(ds
);
4984 if (vnc_display_open(ds
, vnc_display
) < 0)
4987 if (show_vnc_port
) {
4988 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4996 dcl
= ds
->listeners
;
4997 while (dcl
!= NULL
) {
4998 if (dcl
->dpy_refresh
!= NULL
) {
4999 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5000 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5005 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5006 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5007 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5010 text_consoles_set_display(ds
);
5012 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5015 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5016 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5021 qdev_machine_creation_done();
5023 if (rom_load_all() != 0) {
5024 fprintf(stderr
, "rom loading failed\n");
5028 qemu_system_reset();
5030 if (load_vmstate(loadvm
) < 0) {
5036 qemu_start_incoming_migration(incoming
);
5037 } else if (autostart
) {
5047 len
= write(fds
[1], &status
, 1);
5048 if (len
== -1 && (errno
== EINTR
))
5055 perror("not able to chdir to /");
5058 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5064 pwd
= getpwnam(run_as
);
5066 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5072 if (chroot(chroot_dir
) < 0) {
5073 fprintf(stderr
, "chroot failed\n");
5077 perror("not able to chdir to /");
5083 if (setgid(pwd
->pw_gid
) < 0) {
5084 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5087 if (setuid(pwd
->pw_uid
) < 0) {
5088 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5091 if (setuid(0) != -1) {
5092 fprintf(stderr
, "Dropping privileges failed\n");