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>
101 #if defined(__APPLE__) || defined(main)
103 int qemu_main(int argc
, char **argv
, char **envp
);
104 int main(int argc
, char **argv
)
106 return qemu_main(argc
, argv
, NULL
);
109 #define main qemu_main
111 #endif /* CONFIG_SDL */
115 #define main qemu_main
116 #endif /* CONFIG_COCOA */
119 #include "hw/boards.h"
121 #include "hw/pcmcia.h"
123 #include "hw/audiodev.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "block_int.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
150 #include "qemu-option.h"
151 #include "qemu-config.h"
152 #include "qemu-objects.h"
157 #include "exec-all.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
163 #include "qemu-queue.h"
166 //#define DEBUG_SLIRP
168 #define DEFAULT_RAM_SIZE 128
170 #define MAX_VIRTIO_CONSOLES 1
172 static const char *data_dir
;
173 const char *bios_name
= NULL
;
174 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 to store the VM snapshots */
176 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
177 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 const char* keyboard_layout
= NULL
;
182 const char *mem_path
= NULL
;
184 int mem_prealloc
= 0; /* force preallocation of physical target memory */
187 NICInfo nd_table
[MAX_NICS
];
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
195 int graphic_width
= 1024;
196 int graphic_height
= 768;
197 int graphic_depth
= 8;
199 int graphic_width
= 800;
200 int graphic_height
= 600;
201 int graphic_depth
= 15;
203 static int full_screen
= 0;
205 static int no_frame
= 0;
208 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
209 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
210 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
212 int win2k_install_hack
= 0;
221 const char *vnc_display
;
222 int acpi_enabled
= 1;
228 int graphic_rotate
= 0;
229 uint8_t irq0override
= 1;
233 const char *watchdog
;
234 const char *option_rom
[MAX_OPTION_ROMS
];
236 int semihosting_enabled
= 0;
240 const char *qemu_name
;
243 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
244 unsigned int nb_prom_envs
= 0;
245 const char *prom_envs
[MAX_PROM_ENVS
];
250 uint64_t node_mem
[MAX_NODES
];
251 uint64_t node_cpumask
[MAX_NODES
];
253 static CPUState
*cur_cpu
;
254 static CPUState
*next_cpu
;
255 static QEMUTimer
*nographic_timer
;
257 uint8_t qemu_uuid
[16];
259 static QEMUBootSetHandler
*boot_set_handler
;
260 static void *boot_set_opaque
;
263 #define SIG_IPI (SIGRTMIN+4)
265 #define SIG_IPI SIGUSR1
268 static int default_serial
= 1;
269 static int default_parallel
= 1;
270 static int default_virtcon
= 1;
271 static int default_monitor
= 1;
272 static int default_vga
= 1;
273 static int default_floppy
= 1;
274 static int default_cdrom
= 1;
275 static int default_sdcard
= 1;
276 static int default_qmp
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "isa-fdc", .flag
= &default_floppy
},
285 { .driver
= "ide-drive", .flag
= &default_cdrom
},
286 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
287 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
288 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
289 { .driver
= "VGA", .flag
= &default_vga
},
290 { .driver
= "cirrus-vga", .flag
= &default_vga
},
291 { .driver
= "vmware-svga", .flag
= &default_vga
},
294 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
296 const char *driver
= qemu_opt_get(opts
, "driver");
301 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
302 if (strcmp(default_list
[i
].driver
, driver
) != 0)
304 *(default_list
[i
].flag
) = 0;
309 /***********************************************************/
310 /* x86 ISA bus support */
312 target_phys_addr_t isa_mem_base
= 0;
315 /***********************************************************/
316 void hw_error(const char *fmt
, ...)
322 fprintf(stderr
, "qemu: hardware error: ");
323 vfprintf(stderr
, fmt
, ap
);
324 fprintf(stderr
, "\n");
325 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
326 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
328 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
330 cpu_dump_state(env
, stderr
, fprintf
, 0);
337 static void set_proc_name(const char *s
)
339 #if defined(__linux__) && defined(PR_SET_NAME)
343 name
[sizeof(name
) - 1] = 0;
344 strncpy(name
, s
, sizeof(name
));
345 /* Could rewrite argv[0] too, but that's a bit more complicated.
346 This simple way is enough for `top'. */
347 prctl(PR_SET_NAME
, name
);
354 static QEMUBalloonEvent
*qemu_balloon_event
;
355 void *qemu_balloon_event_opaque
;
357 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
359 qemu_balloon_event
= func
;
360 qemu_balloon_event_opaque
= opaque
;
363 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
365 if (qemu_balloon_event
) {
366 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
373 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
375 if (qemu_balloon_event
) {
376 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
384 /***********************************************************/
385 /* real time host monotonic timer */
387 /* compute with 96 bit intermediate result: (a*b)/c */
388 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
393 #ifdef HOST_WORDS_BIGENDIAN
403 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
404 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
407 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
411 /***********************************************************/
412 /* host time/date access */
413 void qemu_get_timedate(struct tm
*tm
, int offset
)
420 if (rtc_date_offset
== -1) {
424 ret
= localtime(&ti
);
426 ti
-= rtc_date_offset
;
430 memcpy(tm
, ret
, sizeof(struct tm
));
433 int qemu_timedate_diff(struct tm
*tm
)
437 if (rtc_date_offset
== -1)
439 seconds
= mktimegm(tm
);
441 seconds
= mktime(tm
);
443 seconds
= mktimegm(tm
) + rtc_date_offset
;
445 return seconds
- time(NULL
);
448 void rtc_change_mon_event(struct tm
*tm
)
452 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
453 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
454 qobject_decref(data
);
457 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
459 time_t rtc_start_date
;
462 if (!strcmp(startdate
, "now") && legacy
) {
463 rtc_date_offset
= -1;
465 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
473 } else if (sscanf(startdate
, "%d-%d-%d",
485 rtc_start_date
= mktimegm(&tm
);
486 if (rtc_start_date
== -1) {
488 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
489 "'2006-06-17T16:01:21' or '2006-06-17'\n");
492 rtc_date_offset
= time(NULL
) - rtc_start_date
;
496 static void configure_rtc(QemuOpts
*opts
)
500 value
= qemu_opt_get(opts
, "base");
502 if (!strcmp(value
, "utc")) {
504 } else if (!strcmp(value
, "localtime")) {
507 configure_rtc_date_offset(value
, 0);
510 value
= qemu_opt_get(opts
, "clock");
512 if (!strcmp(value
, "host")) {
513 rtc_clock
= host_clock
;
514 } else if (!strcmp(value
, "vm")) {
515 rtc_clock
= vm_clock
;
517 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
521 #ifdef CONFIG_TARGET_I386
522 value
= qemu_opt_get(opts
, "driftfix");
524 if (!strcmp(buf
, "slew")) {
526 } else if (!strcmp(buf
, "none")) {
529 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
537 static void socket_cleanup(Notifier
*obj
)
542 static int socket_init(void)
546 static Notifier notifier
= { .notify
= socket_cleanup
};
548 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
550 err
= WSAGetLastError();
551 fprintf(stderr
, "WSAStartup: %d\n", err
);
554 exit_notifier_add(¬ifier
);
559 /*********************/
561 /*********************/
563 static NotifierList exit_notifiers
= NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
565 void exit_notifier_add(Notifier
*notifier
)
567 notifier_list_add(&exit_notifiers
, notifier
);
570 void exit_notifier_remove(Notifier
*notifier
)
572 notifier_list_remove(&exit_notifiers
, notifier
);
575 static void exit_notifier_notify(void)
577 notifier_list_notify(&exit_notifiers
);
580 static void exit_notifier_init(void)
582 atexit(exit_notifier_notify
);
585 /***********************************************************/
586 /* Bluetooth support */
589 static struct HCIInfo
*hci_table
[MAX_NICS
];
591 static struct bt_vlan_s
{
592 struct bt_scatternet_s net
;
594 struct bt_vlan_s
*next
;
597 /* find or alloc a new bluetooth "VLAN" */
598 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
600 struct bt_vlan_s
**pvlan
, *vlan
;
601 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
605 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
607 pvlan
= &first_bt_vlan
;
608 while (*pvlan
!= NULL
)
609 pvlan
= &(*pvlan
)->next
;
614 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
618 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
623 static struct HCIInfo null_hci
= {
624 .cmd_send
= null_hci_send
,
625 .sco_send
= null_hci_send
,
626 .acl_send
= null_hci_send
,
627 .bdaddr_set
= null_hci_addr_set
,
630 struct HCIInfo
*qemu_next_hci(void)
632 if (cur_hci
== nb_hcis
)
635 return hci_table
[cur_hci
++];
638 static struct HCIInfo
*hci_init(const char *str
)
641 struct bt_scatternet_s
*vlan
= 0;
643 if (!strcmp(str
, "null"))
646 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
648 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
649 else if (!strncmp(str
, "hci", 3)) {
652 if (!strncmp(str
+ 3, ",vlan=", 6)) {
653 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
658 vlan
= qemu_find_bt_vlan(0);
660 return bt_new_hci(vlan
);
663 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
668 static int bt_hci_parse(const char *str
)
673 if (nb_hcis
>= MAX_NICS
) {
674 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
687 bdaddr
.b
[5] = 0x56 + nb_hcis
;
688 hci
->bdaddr_set(hci
, bdaddr
.b
);
690 hci_table
[nb_hcis
++] = hci
;
695 static void bt_vhci_add(int vlan_id
)
697 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
700 fprintf(stderr
, "qemu: warning: adding a VHCI to "
701 "an empty scatternet %i\n", vlan_id
);
703 bt_vhci_init(bt_new_hci(vlan
));
706 static struct bt_device_s
*bt_device_add(const char *opt
)
708 struct bt_scatternet_s
*vlan
;
710 char *endp
= strstr(opt
, ",vlan=");
711 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
714 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
717 vlan_id
= strtol(endp
+ 6, &endp
, 0);
719 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
724 vlan
= qemu_find_bt_vlan(vlan_id
);
727 fprintf(stderr
, "qemu: warning: adding a slave device to "
728 "an empty scatternet %i\n", vlan_id
);
730 if (!strcmp(devname
, "keyboard"))
731 return bt_keyboard_init(vlan
);
733 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
737 static int bt_parse(const char *opt
)
739 const char *endp
, *p
;
742 if (strstart(opt
, "hci", &endp
)) {
743 if (!*endp
|| *endp
== ',') {
745 if (!strstart(endp
, ",vlan=", 0))
748 return bt_hci_parse(opt
);
750 } else if (strstart(opt
, "vhci", &endp
)) {
751 if (!*endp
|| *endp
== ',') {
753 if (strstart(endp
, ",vlan=", &p
)) {
754 vlan
= strtol(p
, (char **) &endp
, 0);
756 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
760 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
769 } else if (strstart(opt
, "device:", &endp
))
770 return !bt_device_add(endp
);
772 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
776 /***********************************************************/
777 /* QEMU Block devices */
779 #define HD_ALIAS "index=%d,media=disk"
780 #define CDROM_ALIAS "index=2,media=cdrom"
781 #define FD_ALIAS "index=%d,if=floppy"
782 #define PFLASH_ALIAS "if=pflash"
783 #define MTD_ALIAS "if=mtd"
784 #define SD_ALIAS "index=0,if=sd"
786 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
793 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
796 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
798 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
799 __FUNCTION__
, optstr
);
803 qemu_opt_set(opts
, "file", file
);
807 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
811 /* seek interface, bus and unit */
813 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
814 if (dinfo
->type
== type
&&
823 DriveInfo
*drive_get_by_id(const char *id
)
827 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
828 if (strcmp(id
, dinfo
->id
))
835 int drive_get_max_bus(BlockInterfaceType type
)
841 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
842 if(dinfo
->type
== type
&&
843 dinfo
->bus
> max_bus
)
844 max_bus
= dinfo
->bus
;
849 const char *drive_get_serial(BlockDriverState
*bdrv
)
853 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
854 if (dinfo
->bdrv
== bdrv
)
855 return dinfo
->serial
;
861 BlockInterfaceErrorAction
drive_get_on_error(
862 BlockDriverState
*bdrv
, int is_read
)
866 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
867 if (dinfo
->bdrv
== bdrv
)
868 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
871 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
874 static void bdrv_format_print(void *opaque
, const char *name
)
876 fprintf(stderr
, " %s", name
);
879 void drive_uninit(DriveInfo
*dinfo
)
881 qemu_opts_del(dinfo
->opts
);
882 bdrv_delete(dinfo
->bdrv
);
883 QTAILQ_REMOVE(&drives
, dinfo
, next
);
887 static int parse_block_error_action(const char *buf
, int is_read
)
889 if (!strcmp(buf
, "ignore")) {
890 return BLOCK_ERR_IGNORE
;
891 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
892 return BLOCK_ERR_STOP_ENOSPC
;
893 } else if (!strcmp(buf
, "stop")) {
894 return BLOCK_ERR_STOP_ANY
;
895 } else if (!strcmp(buf
, "report")) {
896 return BLOCK_ERR_REPORT
;
898 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
899 buf
, is_read
? "read" : "write");
904 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
908 const char *file
= NULL
;
911 const char *mediastr
= "";
912 BlockInterfaceType type
;
913 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
915 int cyls
, heads
, secs
, translation
;
916 BlockDriver
*drv
= NULL
;
917 QEMUMachine
*machine
= opaque
;
924 int on_read_error
, on_write_error
;
931 translation
= BIOS_ATA_TRANSLATION_AUTO
;
934 if (machine
&& machine
->use_scsi
) {
936 max_devs
= MAX_SCSI_DEVS
;
937 pstrcpy(devname
, sizeof(devname
), "scsi");
940 max_devs
= MAX_IDE_DEVS
;
941 pstrcpy(devname
, sizeof(devname
), "ide");
945 /* extract parameters */
946 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
947 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
948 index
= qemu_opt_get_number(opts
, "index", -1);
950 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
951 heads
= qemu_opt_get_number(opts
, "heads", 0);
952 secs
= qemu_opt_get_number(opts
, "secs", 0);
954 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
955 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
957 file
= qemu_opt_get(opts
, "file");
958 serial
= qemu_opt_get(opts
, "serial");
960 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
961 pstrcpy(devname
, sizeof(devname
), buf
);
962 if (!strcmp(buf
, "ide")) {
964 max_devs
= MAX_IDE_DEVS
;
965 } else if (!strcmp(buf
, "scsi")) {
967 max_devs
= MAX_SCSI_DEVS
;
968 } else if (!strcmp(buf
, "floppy")) {
971 } else if (!strcmp(buf
, "pflash")) {
974 } else if (!strcmp(buf
, "mtd")) {
977 } else if (!strcmp(buf
, "sd")) {
980 } else if (!strcmp(buf
, "virtio")) {
983 } else if (!strcmp(buf
, "xen")) {
986 } else if (!strcmp(buf
, "none")) {
990 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
995 if (cyls
|| heads
|| secs
) {
996 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
997 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1000 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1001 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1004 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1005 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1010 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1013 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1017 if (!strcmp(buf
, "none"))
1018 translation
= BIOS_ATA_TRANSLATION_NONE
;
1019 else if (!strcmp(buf
, "lba"))
1020 translation
= BIOS_ATA_TRANSLATION_LBA
;
1021 else if (!strcmp(buf
, "auto"))
1022 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1024 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1029 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1030 if (!strcmp(buf
, "disk")) {
1032 } else if (!strcmp(buf
, "cdrom")) {
1033 if (cyls
|| secs
|| heads
) {
1035 "qemu: '%s' invalid physical CHS format\n", buf
);
1038 media
= MEDIA_CDROM
;
1040 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1045 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1046 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1048 else if (!strcmp(buf
, "writethrough"))
1050 else if (!strcmp(buf
, "writeback"))
1053 fprintf(stderr
, "qemu: invalid cache option\n");
1058 #ifdef CONFIG_LINUX_AIO
1059 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1060 if (!strcmp(buf
, "threads"))
1062 else if (!strcmp(buf
, "native"))
1065 fprintf(stderr
, "qemu: invalid aio option\n");
1071 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1072 if (strcmp(buf
, "?") == 0) {
1073 fprintf(stderr
, "qemu: Supported formats:");
1074 bdrv_iterate_format(bdrv_format_print
, NULL
);
1075 fprintf(stderr
, "\n");
1078 drv
= bdrv_find_whitelisted_format(buf
);
1080 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1085 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1086 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1087 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1088 fprintf(stderr
, "werror is no supported by this format\n");
1092 on_write_error
= parse_block_error_action(buf
, 0);
1093 if (on_write_error
< 0) {
1098 on_read_error
= BLOCK_ERR_REPORT
;
1099 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1100 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1101 fprintf(stderr
, "rerror is no supported by this format\n");
1105 on_read_error
= parse_block_error_action(buf
, 1);
1106 if (on_read_error
< 0) {
1111 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1112 if (type
!= IF_VIRTIO
) {
1113 fprintf(stderr
, "addr is not supported\n");
1118 /* compute bus and unit according index */
1121 if (bus_id
!= 0 || unit_id
!= -1) {
1123 "qemu: index cannot be used with bus and unit\n");
1131 unit_id
= index
% max_devs
;
1132 bus_id
= index
/ max_devs
;
1136 /* if user doesn't specify a unit_id,
1137 * try to find the first free
1140 if (unit_id
== -1) {
1142 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1144 if (max_devs
&& unit_id
>= max_devs
) {
1145 unit_id
-= max_devs
;
1153 if (max_devs
&& unit_id
>= max_devs
) {
1154 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1155 unit_id
, max_devs
- 1);
1160 * ignore multiple definitions
1163 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1170 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1171 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1172 dinfo
->id
= qemu_strdup(buf
);
1174 /* no id supplied -> create one */
1175 dinfo
->id
= qemu_mallocz(32);
1176 if (type
== IF_IDE
|| type
== IF_SCSI
)
1177 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1179 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1180 devname
, bus_id
, mediastr
, unit_id
);
1182 snprintf(dinfo
->id
, 32, "%s%s%i",
1183 devname
, mediastr
, unit_id
);
1185 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1186 dinfo
->devaddr
= devaddr
;
1188 dinfo
->bus
= bus_id
;
1189 dinfo
->unit
= unit_id
;
1190 dinfo
->on_read_error
= on_read_error
;
1191 dinfo
->on_write_error
= on_write_error
;
1194 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1195 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1205 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1206 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1210 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1215 /* FIXME: This isn't really a floppy, but it's a reasonable
1218 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1224 /* add virtio block device */
1225 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1226 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1227 qemu_opt_set(opts
, "drive", dinfo
->id
);
1229 qemu_opt_set(opts
, "addr", devaddr
);
1240 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1241 cache
= 2; /* always use write-back with snapshot */
1243 if (cache
== 0) /* no caching */
1244 bdrv_flags
|= BDRV_O_NOCACHE
;
1245 else if (cache
== 2) /* write-back */
1246 bdrv_flags
|= BDRV_O_CACHE_WB
;
1249 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1251 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1255 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1256 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1261 * cdrom is read-only. Set it now, after above interface checking
1262 * since readonly attribute not explicitly required, so no error.
1264 if (media
== MEDIA_CDROM
) {
1267 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1269 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1270 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1271 file
, strerror(errno
));
1275 if (bdrv_key_required(dinfo
->bdrv
))
1281 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1283 QEMUMachine
*machine
= opaque
;
1284 int fatal_error
= 0;
1286 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1293 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1295 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1296 qemu_opt_set(opts
, "snapshot", "on");
1301 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1303 boot_set_handler
= func
;
1304 boot_set_opaque
= opaque
;
1307 int qemu_boot_set(const char *boot_devices
)
1309 if (!boot_set_handler
) {
1312 return boot_set_handler(boot_set_opaque
, boot_devices
);
1315 static int parse_bootdevices(char *devices
)
1317 /* We just do some generic consistency checks */
1321 for (p
= devices
; *p
!= '\0'; p
++) {
1322 /* Allowed boot devices are:
1323 * a-b: floppy disk drives
1324 * c-f: IDE disk drives
1325 * g-m: machine implementation dependant drives
1326 * n-p: network devices
1327 * It's up to each machine implementation to check if the given boot
1328 * devices match the actual hardware implementation and firmware
1331 if (*p
< 'a' || *p
> 'p') {
1332 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1335 if (bitmap
& (1 << (*p
- 'a'))) {
1336 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1339 bitmap
|= 1 << (*p
- 'a');
1344 static void restore_boot_devices(void *opaque
)
1346 char *standard_boot_devices
= opaque
;
1348 qemu_boot_set(standard_boot_devices
);
1350 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1351 qemu_free(standard_boot_devices
);
1354 static void numa_add(const char *optarg
)
1358 unsigned long long value
, endvalue
;
1361 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1362 if (!strcmp(option
, "node")) {
1363 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1364 nodenr
= nb_numa_nodes
;
1366 nodenr
= strtoull(option
, NULL
, 10);
1369 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1370 node_mem
[nodenr
] = 0;
1372 value
= strtoull(option
, &endptr
, 0);
1374 case 0: case 'M': case 'm':
1381 node_mem
[nodenr
] = value
;
1383 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1384 node_cpumask
[nodenr
] = 0;
1386 value
= strtoull(option
, &endptr
, 10);
1389 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1391 if (*endptr
== '-') {
1392 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1393 if (endvalue
>= 63) {
1396 "only 63 CPUs in NUMA mode supported.\n");
1398 value
= (2ULL << endvalue
) - (1ULL << value
);
1400 value
= 1ULL << value
;
1403 node_cpumask
[nodenr
] = value
;
1410 static void smp_parse(const char *optarg
)
1412 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1416 smp
= strtoul(optarg
, &endptr
, 10);
1417 if (endptr
!= optarg
) {
1418 if (*endptr
== ',') {
1422 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1423 sockets
= strtoull(option
, NULL
, 10);
1424 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1425 cores
= strtoull(option
, NULL
, 10);
1426 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1427 threads
= strtoull(option
, NULL
, 10);
1428 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1429 max_cpus
= strtoull(option
, NULL
, 10);
1431 /* compute missing values, prefer sockets over cores over threads */
1432 if (smp
== 0 || sockets
== 0) {
1433 sockets
= sockets
> 0 ? sockets
: 1;
1434 cores
= cores
> 0 ? cores
: 1;
1435 threads
= threads
> 0 ? threads
: 1;
1437 smp
= cores
* threads
* sockets
;
1441 threads
= threads
> 0 ? threads
: 1;
1442 cores
= smp
/ (sockets
* threads
);
1445 threads
= smp
/ (cores
* sockets
);
1450 smp_cores
= cores
> 0 ? cores
: 1;
1451 smp_threads
= threads
> 0 ? threads
: 1;
1453 max_cpus
= smp_cpus
;
1456 /***********************************************************/
1459 static int usb_device_add(const char *devname
, int is_hotplug
)
1462 USBDevice
*dev
= NULL
;
1467 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1468 dev
= usbdevice_create(devname
);
1472 /* the other ones */
1473 if (strstart(devname
, "host:", &p
)) {
1474 dev
= usb_host_device_open(p
);
1475 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1476 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1477 bt_new_hci(qemu_find_bt_vlan(0)));
1488 static int usb_device_del(const char *devname
)
1493 if (strstart(devname
, "host:", &p
))
1494 return usb_host_device_close(p
);
1499 p
= strchr(devname
, '.');
1502 bus_num
= strtoul(devname
, NULL
, 0);
1503 addr
= strtoul(p
+ 1, NULL
, 0);
1505 return usb_device_delete_addr(bus_num
, addr
);
1508 static int usb_parse(const char *cmdline
)
1511 r
= usb_device_add(cmdline
, 0);
1513 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1518 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1520 const char *devname
= qdict_get_str(qdict
, "devname");
1521 if (usb_device_add(devname
, 1) < 0) {
1522 error_report("could not add USB device '%s'", devname
);
1526 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1528 const char *devname
= qdict_get_str(qdict
, "devname");
1529 if (usb_device_del(devname
) < 0) {
1530 error_report("could not delete USB device '%s'", devname
);
1534 /***********************************************************/
1535 /* PCMCIA/Cardbus */
1537 static struct pcmcia_socket_entry_s
{
1538 PCMCIASocket
*socket
;
1539 struct pcmcia_socket_entry_s
*next
;
1540 } *pcmcia_sockets
= 0;
1542 void pcmcia_socket_register(PCMCIASocket
*socket
)
1544 struct pcmcia_socket_entry_s
*entry
;
1546 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1547 entry
->socket
= socket
;
1548 entry
->next
= pcmcia_sockets
;
1549 pcmcia_sockets
= entry
;
1552 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1554 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1556 ptr
= &pcmcia_sockets
;
1557 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1558 if (entry
->socket
== socket
) {
1564 void pcmcia_info(Monitor
*mon
)
1566 struct pcmcia_socket_entry_s
*iter
;
1568 if (!pcmcia_sockets
)
1569 monitor_printf(mon
, "No PCMCIA sockets\n");
1571 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1572 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1573 iter
->socket
->attached
? iter
->socket
->card_string
:
1577 /***********************************************************/
1580 typedef struct IOHandlerRecord
{
1582 IOCanReadHandler
*fd_read_poll
;
1584 IOHandler
*fd_write
;
1587 /* temporary data */
1589 QLIST_ENTRY(IOHandlerRecord
) next
;
1592 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1593 QLIST_HEAD_INITIALIZER(io_handlers
);
1596 /* XXX: fd_read_poll should be suppressed, but an API change is
1597 necessary in the character devices to suppress fd_can_read(). */
1598 int qemu_set_fd_handler2(int fd
,
1599 IOCanReadHandler
*fd_read_poll
,
1601 IOHandler
*fd_write
,
1604 IOHandlerRecord
*ioh
;
1606 if (!fd_read
&& !fd_write
) {
1607 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1608 if (ioh
->fd
== fd
) {
1614 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1618 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1619 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1622 ioh
->fd_read_poll
= fd_read_poll
;
1623 ioh
->fd_read
= fd_read
;
1624 ioh
->fd_write
= fd_write
;
1625 ioh
->opaque
= opaque
;
1631 int qemu_set_fd_handler(int fd
,
1633 IOHandler
*fd_write
,
1636 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1640 /***********************************************************/
1641 /* Polling handling */
1643 typedef struct PollingEntry
{
1646 struct PollingEntry
*next
;
1649 static PollingEntry
*first_polling_entry
;
1651 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1653 PollingEntry
**ppe
, *pe
;
1654 pe
= qemu_mallocz(sizeof(PollingEntry
));
1656 pe
->opaque
= opaque
;
1657 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1662 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1664 PollingEntry
**ppe
, *pe
;
1665 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1667 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1675 /***********************************************************/
1676 /* Wait objects support */
1677 typedef struct WaitObjects
{
1679 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1680 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1681 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1684 static WaitObjects wait_objects
= {0};
1686 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1688 WaitObjects
*w
= &wait_objects
;
1690 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1692 w
->events
[w
->num
] = handle
;
1693 w
->func
[w
->num
] = func
;
1694 w
->opaque
[w
->num
] = opaque
;
1699 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1702 WaitObjects
*w
= &wait_objects
;
1705 for (i
= 0; i
< w
->num
; i
++) {
1706 if (w
->events
[i
] == handle
)
1709 w
->events
[i
] = w
->events
[i
+ 1];
1710 w
->func
[i
] = w
->func
[i
+ 1];
1711 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1719 /***********************************************************/
1720 /* ram save/restore */
1722 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1723 #define RAM_SAVE_FLAG_COMPRESS 0x02
1724 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1725 #define RAM_SAVE_FLAG_PAGE 0x08
1726 #define RAM_SAVE_FLAG_EOS 0x10
1728 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1730 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1731 uint32_t *array
= (uint32_t *)page
;
1734 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1735 if (array
[i
] != val
)
1742 static int ram_save_block(QEMUFile
*f
)
1744 static ram_addr_t current_addr
= 0;
1745 ram_addr_t saved_addr
= current_addr
;
1746 ram_addr_t addr
= 0;
1749 while (addr
< last_ram_offset
) {
1750 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1753 cpu_physical_memory_reset_dirty(current_addr
,
1754 current_addr
+ TARGET_PAGE_SIZE
,
1755 MIGRATION_DIRTY_FLAG
);
1757 p
= qemu_get_ram_ptr(current_addr
);
1759 if (is_dup_page(p
, *p
)) {
1760 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1761 qemu_put_byte(f
, *p
);
1763 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1764 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1770 addr
+= TARGET_PAGE_SIZE
;
1771 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1777 static uint64_t bytes_transferred
;
1779 static ram_addr_t
ram_save_remaining(void)
1782 ram_addr_t count
= 0;
1784 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1785 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1792 uint64_t ram_bytes_remaining(void)
1794 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1797 uint64_t ram_bytes_transferred(void)
1799 return bytes_transferred
;
1802 uint64_t ram_bytes_total(void)
1804 return last_ram_offset
;
1807 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1810 uint64_t bytes_transferred_last
;
1812 uint64_t expected_time
= 0;
1815 cpu_physical_memory_set_dirty_tracking(0);
1819 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1820 qemu_file_set_error(f
);
1825 bytes_transferred
= 0;
1827 /* Make sure all dirty bits are set */
1828 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1829 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1830 cpu_physical_memory_set_dirty(addr
);
1833 /* Enable dirty memory tracking */
1834 cpu_physical_memory_set_dirty_tracking(1);
1836 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1839 bytes_transferred_last
= bytes_transferred
;
1840 bwidth
= qemu_get_clock_ns(rt_clock
);
1842 while (!qemu_file_rate_limit(f
)) {
1845 ret
= ram_save_block(f
);
1846 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1847 if (ret
== 0) /* no more blocks */
1851 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1852 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1854 /* if we haven't transferred anything this round, force expected_time to a
1855 * a very high value, but without crashing */
1859 /* try transferring iterative blocks of memory */
1861 /* flush all remaining blocks regardless of rate limiting */
1862 while (ram_save_block(f
) != 0) {
1863 bytes_transferred
+= TARGET_PAGE_SIZE
;
1865 cpu_physical_memory_set_dirty_tracking(0);
1868 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1870 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1872 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1875 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1880 if (version_id
!= 3)
1884 addr
= qemu_get_be64(f
);
1886 flags
= addr
& ~TARGET_PAGE_MASK
;
1887 addr
&= TARGET_PAGE_MASK
;
1889 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1890 if (addr
!= last_ram_offset
)
1894 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1895 uint8_t ch
= qemu_get_byte(f
);
1896 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1899 (!kvm_enabled() || kvm_has_sync_mmu())) {
1900 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1903 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1904 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1906 if (qemu_file_has_error(f
)) {
1909 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1914 void qemu_service_io(void)
1916 qemu_notify_event();
1919 /***********************************************************/
1920 /* machine registration */
1922 static QEMUMachine
*first_machine
= NULL
;
1923 QEMUMachine
*current_machine
= NULL
;
1925 int qemu_register_machine(QEMUMachine
*m
)
1928 pm
= &first_machine
;
1936 static QEMUMachine
*find_machine(const char *name
)
1940 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1941 if (!strcmp(m
->name
, name
))
1943 if (m
->alias
&& !strcmp(m
->alias
, name
))
1949 static QEMUMachine
*find_default_machine(void)
1953 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1954 if (m
->is_default
) {
1961 /***********************************************************/
1962 /* main execution loop */
1964 static void gui_update(void *opaque
)
1966 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1967 DisplayState
*ds
= opaque
;
1968 DisplayChangeListener
*dcl
= ds
->listeners
;
1970 qemu_flush_coalesced_mmio_buffer();
1973 while (dcl
!= NULL
) {
1974 if (dcl
->gui_timer_interval
&&
1975 dcl
->gui_timer_interval
< interval
)
1976 interval
= dcl
->gui_timer_interval
;
1979 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1982 static void nographic_update(void *opaque
)
1984 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1986 qemu_flush_coalesced_mmio_buffer();
1987 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1990 void cpu_synchronize_all_states(void)
1994 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1995 cpu_synchronize_state(cpu
);
1999 void cpu_synchronize_all_post_reset(void)
2003 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2004 cpu_synchronize_post_reset(cpu
);
2008 void cpu_synchronize_all_post_init(void)
2012 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2013 cpu_synchronize_post_init(cpu
);
2017 struct vm_change_state_entry
{
2018 VMChangeStateHandler
*cb
;
2020 QLIST_ENTRY (vm_change_state_entry
) entries
;
2023 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2025 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2028 VMChangeStateEntry
*e
;
2030 e
= qemu_mallocz(sizeof (*e
));
2034 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2038 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2040 QLIST_REMOVE (e
, entries
);
2044 static void vm_state_notify(int running
, int reason
)
2046 VMChangeStateEntry
*e
;
2048 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2049 e
->cb(e
->opaque
, running
, reason
);
2053 static void resume_all_vcpus(void);
2054 static void pause_all_vcpus(void);
2061 vm_state_notify(1, 0);
2066 /* reset/shutdown handler */
2068 typedef struct QEMUResetEntry
{
2069 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2070 QEMUResetHandler
*func
;
2074 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2075 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2076 static int reset_requested
;
2077 static int shutdown_requested
;
2078 static int powerdown_requested
;
2079 static int debug_requested
;
2080 static int vmstop_requested
;
2082 int qemu_shutdown_requested(void)
2084 int r
= shutdown_requested
;
2085 shutdown_requested
= 0;
2089 int qemu_reset_requested(void)
2091 int r
= reset_requested
;
2092 reset_requested
= 0;
2096 int qemu_powerdown_requested(void)
2098 int r
= powerdown_requested
;
2099 powerdown_requested
= 0;
2103 static int qemu_debug_requested(void)
2105 int r
= debug_requested
;
2106 debug_requested
= 0;
2110 static int qemu_vmstop_requested(void)
2112 int r
= vmstop_requested
;
2113 vmstop_requested
= 0;
2117 static void do_vm_stop(int reason
)
2120 cpu_disable_ticks();
2123 vm_state_notify(0, reason
);
2124 monitor_protocol_event(QEVENT_STOP
, NULL
);
2128 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2130 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2133 re
->opaque
= opaque
;
2134 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2137 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2141 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2142 if (re
->func
== func
&& re
->opaque
== opaque
) {
2143 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2150 void qemu_system_reset(void)
2152 QEMUResetEntry
*re
, *nre
;
2154 /* reset all devices */
2155 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2156 re
->func(re
->opaque
);
2158 monitor_protocol_event(QEVENT_RESET
, NULL
);
2159 cpu_synchronize_all_post_reset();
2162 void qemu_system_reset_request(void)
2165 shutdown_requested
= 1;
2167 reset_requested
= 1;
2169 qemu_notify_event();
2172 void qemu_system_shutdown_request(void)
2174 shutdown_requested
= 1;
2175 qemu_notify_event();
2178 void qemu_system_powerdown_request(void)
2180 powerdown_requested
= 1;
2181 qemu_notify_event();
2184 static int cpu_can_run(CPUState
*env
)
2195 static int cpu_has_work(CPUState
*env
)
2203 if (qemu_cpu_has_work(env
))
2208 static int tcg_has_work(void)
2212 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2213 if (cpu_has_work(env
))
2219 static int io_thread_fd
= -1;
2221 static void qemu_event_increment(void)
2223 /* Write 8 bytes to be compatible with eventfd. */
2224 static uint64_t val
= 1;
2227 if (io_thread_fd
== -1)
2231 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2232 } while (ret
< 0 && errno
== EINTR
);
2234 /* EAGAIN is fine, a read must be pending. */
2235 if (ret
< 0 && errno
!= EAGAIN
) {
2236 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2242 static void qemu_event_read(void *opaque
)
2244 int fd
= (unsigned long)opaque
;
2248 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2250 len
= read(fd
, buffer
, sizeof(buffer
));
2251 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2254 static int qemu_event_init(void)
2259 err
= qemu_eventfd(fds
);
2263 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2267 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2271 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2272 (void *)(unsigned long)fds
[0]);
2274 io_thread_fd
= fds
[1];
2283 HANDLE qemu_event_handle
;
2285 static void dummy_event_handler(void *opaque
)
2289 static int qemu_event_init(void)
2291 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2292 if (!qemu_event_handle
) {
2293 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2296 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2300 static void qemu_event_increment(void)
2302 if (!SetEvent(qemu_event_handle
)) {
2303 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2310 #ifndef CONFIG_IOTHREAD
2311 static int qemu_init_main_loop(void)
2313 return qemu_event_init();
2316 void qemu_init_vcpu(void *_env
)
2318 CPUState
*env
= _env
;
2320 env
->nr_cores
= smp_cores
;
2321 env
->nr_threads
= smp_threads
;
2327 int qemu_cpu_self(void *env
)
2332 static void resume_all_vcpus(void)
2336 static void pause_all_vcpus(void)
2340 void qemu_cpu_kick(void *env
)
2345 void qemu_notify_event(void)
2347 CPUState
*env
= cpu_single_env
;
2349 qemu_event_increment ();
2353 if (next_cpu
&& env
!= next_cpu
) {
2358 void qemu_mutex_lock_iothread(void) {}
2359 void qemu_mutex_unlock_iothread(void) {}
2361 void vm_stop(int reason
)
2366 #else /* CONFIG_IOTHREAD */
2368 #include "qemu-thread.h"
2370 QemuMutex qemu_global_mutex
;
2371 static QemuMutex qemu_fair_mutex
;
2373 static QemuThread io_thread
;
2375 static QemuThread
*tcg_cpu_thread
;
2376 static QemuCond
*tcg_halt_cond
;
2378 static int qemu_system_ready
;
2380 static QemuCond qemu_cpu_cond
;
2382 static QemuCond qemu_system_cond
;
2383 static QemuCond qemu_pause_cond
;
2385 static void tcg_block_io_signals(void);
2386 static void kvm_block_io_signals(CPUState
*env
);
2387 static void unblock_io_signals(void);
2389 static int qemu_init_main_loop(void)
2393 ret
= qemu_event_init();
2397 qemu_cond_init(&qemu_pause_cond
);
2398 qemu_mutex_init(&qemu_fair_mutex
);
2399 qemu_mutex_init(&qemu_global_mutex
);
2400 qemu_mutex_lock(&qemu_global_mutex
);
2402 unblock_io_signals();
2403 qemu_thread_self(&io_thread
);
2408 static void qemu_wait_io_event_common(CPUState
*env
)
2413 qemu_cond_signal(&qemu_pause_cond
);
2417 static void qemu_wait_io_event(CPUState
*env
)
2419 while (!tcg_has_work())
2420 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2422 qemu_mutex_unlock(&qemu_global_mutex
);
2425 * Users of qemu_global_mutex can be starved, having no chance
2426 * to acquire it since this path will get to it first.
2427 * So use another lock to provide fairness.
2429 qemu_mutex_lock(&qemu_fair_mutex
);
2430 qemu_mutex_unlock(&qemu_fair_mutex
);
2432 qemu_mutex_lock(&qemu_global_mutex
);
2433 qemu_wait_io_event_common(env
);
2436 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2443 ts
.tv_sec
= timeout
/ 1000;
2444 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2446 sigemptyset(&waitset
);
2447 sigaddset(&waitset
, SIG_IPI
);
2449 qemu_mutex_unlock(&qemu_global_mutex
);
2450 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2452 qemu_mutex_lock(&qemu_global_mutex
);
2454 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2455 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2460 static void qemu_kvm_wait_io_event(CPUState
*env
)
2462 while (!cpu_has_work(env
))
2463 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2465 qemu_kvm_eat_signal(env
, 0);
2466 qemu_wait_io_event_common(env
);
2469 static int qemu_cpu_exec(CPUState
*env
);
2471 static void *kvm_cpu_thread_fn(void *arg
)
2473 CPUState
*env
= arg
;
2475 qemu_thread_self(env
->thread
);
2479 kvm_block_io_signals(env
);
2481 /* signal CPU creation */
2482 qemu_mutex_lock(&qemu_global_mutex
);
2484 qemu_cond_signal(&qemu_cpu_cond
);
2486 /* and wait for machine initialization */
2487 while (!qemu_system_ready
)
2488 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2491 if (cpu_can_run(env
))
2493 qemu_kvm_wait_io_event(env
);
2499 static bool tcg_cpu_exec(void);
2501 static void *tcg_cpu_thread_fn(void *arg
)
2503 CPUState
*env
= arg
;
2505 tcg_block_io_signals();
2506 qemu_thread_self(env
->thread
);
2508 /* signal CPU creation */
2509 qemu_mutex_lock(&qemu_global_mutex
);
2510 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2512 qemu_cond_signal(&qemu_cpu_cond
);
2514 /* and wait for machine initialization */
2515 while (!qemu_system_ready
)
2516 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2520 qemu_wait_io_event(cur_cpu
);
2526 void qemu_cpu_kick(void *_env
)
2528 CPUState
*env
= _env
;
2529 qemu_cond_broadcast(env
->halt_cond
);
2531 qemu_thread_signal(env
->thread
, SIG_IPI
);
2534 int qemu_cpu_self(void *_env
)
2536 CPUState
*env
= _env
;
2539 qemu_thread_self(&this);
2541 return qemu_thread_equal(&this, env
->thread
);
2544 static void cpu_signal(int sig
)
2547 cpu_exit(cpu_single_env
);
2550 static void tcg_block_io_signals(void)
2553 struct sigaction sigact
;
2556 sigaddset(&set
, SIGUSR2
);
2557 sigaddset(&set
, SIGIO
);
2558 sigaddset(&set
, SIGALRM
);
2559 sigaddset(&set
, SIGCHLD
);
2560 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2563 sigaddset(&set
, SIG_IPI
);
2564 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2566 memset(&sigact
, 0, sizeof(sigact
));
2567 sigact
.sa_handler
= cpu_signal
;
2568 sigaction(SIG_IPI
, &sigact
, NULL
);
2571 static void dummy_signal(int sig
)
2575 static void kvm_block_io_signals(CPUState
*env
)
2579 struct sigaction sigact
;
2582 sigaddset(&set
, SIGUSR2
);
2583 sigaddset(&set
, SIGIO
);
2584 sigaddset(&set
, SIGALRM
);
2585 sigaddset(&set
, SIGCHLD
);
2586 sigaddset(&set
, SIG_IPI
);
2587 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2589 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2590 sigdelset(&set
, SIG_IPI
);
2592 memset(&sigact
, 0, sizeof(sigact
));
2593 sigact
.sa_handler
= dummy_signal
;
2594 sigaction(SIG_IPI
, &sigact
, NULL
);
2596 r
= kvm_set_signal_mask(env
, &set
);
2598 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2603 static void unblock_io_signals(void)
2608 sigaddset(&set
, SIGUSR2
);
2609 sigaddset(&set
, SIGIO
);
2610 sigaddset(&set
, SIGALRM
);
2611 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2614 sigaddset(&set
, SIG_IPI
);
2615 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2618 static void qemu_signal_lock(unsigned int msecs
)
2620 qemu_mutex_lock(&qemu_fair_mutex
);
2622 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2623 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2624 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2627 qemu_mutex_unlock(&qemu_fair_mutex
);
2630 void qemu_mutex_lock_iothread(void)
2632 if (kvm_enabled()) {
2633 qemu_mutex_lock(&qemu_fair_mutex
);
2634 qemu_mutex_lock(&qemu_global_mutex
);
2635 qemu_mutex_unlock(&qemu_fair_mutex
);
2637 qemu_signal_lock(100);
2640 void qemu_mutex_unlock_iothread(void)
2642 qemu_mutex_unlock(&qemu_global_mutex
);
2645 static int all_vcpus_paused(void)
2647 CPUState
*penv
= first_cpu
;
2652 penv
= (CPUState
*)penv
->next_cpu
;
2658 static void pause_all_vcpus(void)
2660 CPUState
*penv
= first_cpu
;
2664 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2665 qemu_cpu_kick(penv
);
2666 penv
= (CPUState
*)penv
->next_cpu
;
2669 while (!all_vcpus_paused()) {
2670 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2673 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2674 penv
= (CPUState
*)penv
->next_cpu
;
2679 static void resume_all_vcpus(void)
2681 CPUState
*penv
= first_cpu
;
2686 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2687 qemu_cpu_kick(penv
);
2688 penv
= (CPUState
*)penv
->next_cpu
;
2692 static void tcg_init_vcpu(void *_env
)
2694 CPUState
*env
= _env
;
2695 /* share a single thread for all cpus with TCG */
2696 if (!tcg_cpu_thread
) {
2697 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2698 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2699 qemu_cond_init(env
->halt_cond
);
2700 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2701 while (env
->created
== 0)
2702 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2703 tcg_cpu_thread
= env
->thread
;
2704 tcg_halt_cond
= env
->halt_cond
;
2706 env
->thread
= tcg_cpu_thread
;
2707 env
->halt_cond
= tcg_halt_cond
;
2711 static void kvm_start_vcpu(CPUState
*env
)
2713 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2714 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2715 qemu_cond_init(env
->halt_cond
);
2716 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2717 while (env
->created
== 0)
2718 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2721 void qemu_init_vcpu(void *_env
)
2723 CPUState
*env
= _env
;
2725 env
->nr_cores
= smp_cores
;
2726 env
->nr_threads
= smp_threads
;
2728 kvm_start_vcpu(env
);
2733 void qemu_notify_event(void)
2735 qemu_event_increment();
2738 static void qemu_system_vmstop_request(int reason
)
2740 vmstop_requested
= reason
;
2741 qemu_notify_event();
2744 void vm_stop(int reason
)
2747 qemu_thread_self(&me
);
2749 if (!qemu_thread_equal(&me
, &io_thread
)) {
2750 qemu_system_vmstop_request(reason
);
2752 * FIXME: should not return to device code in case
2753 * vm_stop() has been requested.
2755 if (cpu_single_env
) {
2756 cpu_exit(cpu_single_env
);
2757 cpu_single_env
->stop
= 1;
2768 static void host_main_loop_wait(int *timeout
)
2774 /* XXX: need to suppress polling by better using win32 events */
2776 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2777 ret
|= pe
->func(pe
->opaque
);
2781 WaitObjects
*w
= &wait_objects
;
2783 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2784 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2785 if (w
->func
[ret
- WAIT_OBJECT_0
])
2786 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2788 /* Check for additional signaled events */
2789 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2791 /* Check if event is signaled */
2792 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2793 if(ret2
== WAIT_OBJECT_0
) {
2795 w
->func
[i
](w
->opaque
[i
]);
2796 } else if (ret2
== WAIT_TIMEOUT
) {
2798 err
= GetLastError();
2799 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2802 } else if (ret
== WAIT_TIMEOUT
) {
2804 err
= GetLastError();
2805 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2812 static void host_main_loop_wait(int *timeout
)
2817 void main_loop_wait(int nonblocking
)
2819 IOHandlerRecord
*ioh
;
2820 fd_set rfds
, wfds
, xfds
;
2828 timeout
= qemu_calculate_timeout();
2829 qemu_bh_update_timeout(&timeout
);
2832 host_main_loop_wait(&timeout
);
2834 /* poll any events */
2835 /* XXX: separate device handlers from system ones */
2840 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2844 (!ioh
->fd_read_poll
||
2845 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2846 FD_SET(ioh
->fd
, &rfds
);
2850 if (ioh
->fd_write
) {
2851 FD_SET(ioh
->fd
, &wfds
);
2857 tv
.tv_sec
= timeout
/ 1000;
2858 tv
.tv_usec
= (timeout
% 1000) * 1000;
2860 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2862 qemu_mutex_unlock_iothread();
2863 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2864 qemu_mutex_lock_iothread();
2866 IOHandlerRecord
*pioh
;
2868 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2870 QLIST_REMOVE(ioh
, next
);
2874 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2875 ioh
->fd_read(ioh
->opaque
);
2877 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2878 ioh
->fd_write(ioh
->opaque
);
2883 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2885 qemu_run_all_timers();
2887 /* Check bottom-halves last in case any of the earlier events triggered
2893 static int qemu_cpu_exec(CPUState
*env
)
2896 #ifdef CONFIG_PROFILER
2900 #ifdef CONFIG_PROFILER
2901 ti
= profile_getclock();
2906 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2907 env
->icount_decr
.u16
.low
= 0;
2908 env
->icount_extra
= 0;
2909 count
= qemu_icount_round (qemu_next_deadline());
2910 qemu_icount
+= count
;
2911 decr
= (count
> 0xffff) ? 0xffff : count
;
2913 env
->icount_decr
.u16
.low
= decr
;
2914 env
->icount_extra
= count
;
2916 ret
= cpu_exec(env
);
2917 #ifdef CONFIG_PROFILER
2918 qemu_time
+= profile_getclock() - ti
;
2921 /* Fold pending instructions back into the
2922 instruction counter, and clear the interrupt flag. */
2923 qemu_icount
-= (env
->icount_decr
.u16
.low
2924 + env
->icount_extra
);
2925 env
->icount_decr
.u32
= 0;
2926 env
->icount_extra
= 0;
2931 static bool tcg_cpu_exec(void)
2935 if (next_cpu
== NULL
)
2936 next_cpu
= first_cpu
;
2937 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2938 CPUState
*env
= cur_cpu
= next_cpu
;
2940 qemu_clock_enable(vm_clock
,
2941 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2943 if (qemu_alarm_pending())
2945 if (cpu_can_run(env
))
2946 ret
= qemu_cpu_exec(env
);
2950 if (ret
== EXCP_DEBUG
) {
2951 gdb_set_stop_cpu(env
);
2952 debug_requested
= 1;
2956 return tcg_has_work();
2959 static int vm_can_run(void)
2961 if (powerdown_requested
)
2963 if (reset_requested
)
2965 if (shutdown_requested
)
2967 if (debug_requested
)
2972 qemu_irq qemu_system_powerdown
;
2974 static void main_loop(void)
2978 #ifdef CONFIG_IOTHREAD
2979 qemu_system_ready
= 1;
2980 qemu_cond_broadcast(&qemu_system_cond
);
2985 bool nonblocking
= false;
2986 #ifdef CONFIG_PROFILER
2989 #ifndef CONFIG_IOTHREAD
2990 nonblocking
= tcg_cpu_exec();
2992 #ifdef CONFIG_PROFILER
2993 ti
= profile_getclock();
2995 main_loop_wait(nonblocking
);
2996 #ifdef CONFIG_PROFILER
2997 dev_time
+= profile_getclock() - ti
;
2999 } while (vm_can_run());
3001 if (qemu_debug_requested()) {
3002 vm_stop(EXCP_DEBUG
);
3004 if (qemu_shutdown_requested()) {
3005 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3012 if (qemu_reset_requested()) {
3014 qemu_system_reset();
3017 if (qemu_powerdown_requested()) {
3018 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3019 qemu_irq_raise(qemu_system_powerdown
);
3021 if ((r
= qemu_vmstop_requested())) {
3028 static void version(void)
3030 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3033 static void help(int exitcode
)
3035 const char *options_help
=
3036 #define DEF(option, opt_arg, opt_enum, opt_help) \
3038 #define DEFHEADING(text) stringify(text) "\n"
3039 #include "qemu-options.h"
3045 printf("usage: %s [options] [disk_image]\n"
3047 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3050 "During emulation, the following keys are useful:\n"
3051 "ctrl-alt-f toggle full screen\n"
3052 "ctrl-alt-n switch to virtual console 'n'\n"
3053 "ctrl-alt toggle mouse and keyboard grab\n"
3055 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3061 #define HAS_ARG 0x0001
3064 #define DEF(option, opt_arg, opt_enum, opt_help) \
3066 #define DEFHEADING(text)
3067 #include "qemu-options.h"
3073 typedef struct QEMUOption
{
3079 static const QEMUOption qemu_options
[] = {
3080 { "h", 0, QEMU_OPTION_h
},
3081 #define DEF(option, opt_arg, opt_enum, opt_help) \
3082 { option, opt_arg, opt_enum },
3083 #define DEFHEADING(text)
3084 #include "qemu-options.h"
3092 struct soundhw soundhw
[] = {
3093 #ifdef HAS_AUDIO_CHOICE
3094 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3100 { .init_isa
= pcspk_audio_init
}
3107 "Creative Sound Blaster 16",
3110 { .init_isa
= SB16_init
}
3114 #ifdef CONFIG_CS4231A
3120 { .init_isa
= cs4231a_init
}
3128 "Yamaha YMF262 (OPL3)",
3130 "Yamaha YM3812 (OPL2)",
3134 { .init_isa
= Adlib_init
}
3141 "Gravis Ultrasound GF1",
3144 { .init_isa
= GUS_init
}
3151 "Intel 82801AA AC97 Audio",
3154 { .init_pci
= ac97_init
}
3158 #ifdef CONFIG_ES1370
3161 "ENSONIQ AudioPCI ES1370",
3164 { .init_pci
= es1370_init
}
3168 #endif /* HAS_AUDIO_CHOICE */
3170 { NULL
, NULL
, 0, 0, { NULL
} }
3173 static void select_soundhw (const char *optarg
)
3177 if (*optarg
== '?') {
3180 printf ("Valid sound card names (comma separated):\n");
3181 for (c
= soundhw
; c
->name
; ++c
) {
3182 printf ("%-11s %s\n", c
->name
, c
->descr
);
3184 printf ("\n-soundhw all will enable all of the above\n");
3185 exit (*optarg
!= '?');
3193 if (!strcmp (optarg
, "all")) {
3194 for (c
= soundhw
; c
->name
; ++c
) {
3202 e
= strchr (p
, ',');
3203 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3205 for (c
= soundhw
; c
->name
; ++c
) {
3206 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3215 "Unknown sound card name (too big to show)\n");
3218 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3223 p
+= l
+ (e
!= NULL
);
3227 goto show_valid_cards
;
3232 static void select_vgahw (const char *p
)
3237 vga_interface_type
= VGA_NONE
;
3238 if (strstart(p
, "std", &opts
)) {
3239 vga_interface_type
= VGA_STD
;
3240 } else if (strstart(p
, "cirrus", &opts
)) {
3241 vga_interface_type
= VGA_CIRRUS
;
3242 } else if (strstart(p
, "vmware", &opts
)) {
3243 vga_interface_type
= VGA_VMWARE
;
3244 } else if (strstart(p
, "xenfb", &opts
)) {
3245 vga_interface_type
= VGA_XENFB
;
3246 } else if (!strstart(p
, "none", &opts
)) {
3248 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3252 const char *nextopt
;
3254 if (strstart(opts
, ",retrace=", &nextopt
)) {
3256 if (strstart(opts
, "dumb", &nextopt
))
3257 vga_retrace_method
= VGA_RETRACE_DUMB
;
3258 else if (strstart(opts
, "precise", &nextopt
))
3259 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3260 else goto invalid_vga
;
3261 } else goto invalid_vga
;
3267 static int balloon_parse(const char *arg
)
3271 if (strcmp(arg
, "none") == 0) {
3275 if (!strncmp(arg
, "virtio", 6)) {
3276 if (arg
[6] == ',') {
3277 /* have params -> parse them */
3278 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3282 /* create empty opts */
3283 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3285 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3294 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3296 exit(STATUS_CONTROL_C_EXIT
);
3301 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3305 if(strlen(str
) != 36)
3308 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3309 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3310 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3316 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3324 static void termsig_handler(int signal
)
3326 qemu_system_shutdown_request();
3329 static void sigchld_handler(int signal
)
3331 waitpid(-1, NULL
, WNOHANG
);
3334 static void sighandler_setup(void)
3336 struct sigaction act
;
3338 memset(&act
, 0, sizeof(act
));
3339 act
.sa_handler
= termsig_handler
;
3340 sigaction(SIGINT
, &act
, NULL
);
3341 sigaction(SIGHUP
, &act
, NULL
);
3342 sigaction(SIGTERM
, &act
, NULL
);
3344 act
.sa_handler
= sigchld_handler
;
3345 act
.sa_flags
= SA_NOCLDSTOP
;
3346 sigaction(SIGCHLD
, &act
, NULL
);
3352 /* Look for support files in the same directory as the executable. */
3353 static char *find_datadir(const char *argv0
)
3359 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3366 while (p
!= buf
&& *p
!= '\\')
3369 if (access(buf
, R_OK
) == 0) {
3370 return qemu_strdup(buf
);
3376 /* Find a likely location for support files using the location of the binary.
3377 For installed binaries this will be "$bindir/../share/qemu". When
3378 running from the build tree this will be "$bindir/../pc-bios". */
3379 #define SHARE_SUFFIX "/share/qemu"
3380 #define BUILD_SUFFIX "/pc-bios"
3381 static char *find_datadir(const char *argv0
)
3389 #if defined(__linux__)
3392 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3398 #elif defined(__FreeBSD__)
3401 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3408 /* If we don't have any way of figuring out the actual executable
3409 location then try argv[0]. */
3411 p
= realpath(argv0
, buf
);
3419 max_len
= strlen(dir
) +
3420 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3421 res
= qemu_mallocz(max_len
);
3422 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3423 if (access(res
, R_OK
)) {
3424 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3425 if (access(res
, R_OK
)) {
3437 char *qemu_find_file(int type
, const char *name
)
3443 /* If name contains path separators then try it as a straight path. */
3444 if ((strchr(name
, '/') || strchr(name
, '\\'))
3445 && access(name
, R_OK
) == 0) {
3446 return qemu_strdup(name
);
3449 case QEMU_FILE_TYPE_BIOS
:
3452 case QEMU_FILE_TYPE_KEYMAP
:
3453 subdir
= "keymaps/";
3458 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3459 buf
= qemu_mallocz(len
);
3460 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3461 if (access(buf
, R_OK
)) {
3468 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3470 return qdev_device_help(opts
);
3473 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3477 dev
= qdev_device_add(opts
);
3483 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3485 CharDriverState
*chr
;
3487 chr
= qemu_chr_open_opts(opts
, NULL
);
3493 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3495 CharDriverState
*chr
;
3496 const char *chardev
;
3500 mode
= qemu_opt_get(opts
, "mode");
3504 if (strcmp(mode
, "readline") == 0) {
3505 flags
= MONITOR_USE_READLINE
;
3506 } else if (strcmp(mode
, "control") == 0) {
3507 flags
= MONITOR_USE_CONTROL
;
3509 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3513 if (qemu_opt_get_bool(opts
, "default", 0))
3514 flags
|= MONITOR_IS_DEFAULT
;
3516 chardev
= qemu_opt_get(opts
, "chardev");
3517 chr
= qemu_chr_find(chardev
);
3519 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3523 monitor_init(chr
, flags
);
3527 static void monitor_parse(const char *optarg
, const char *mode
)
3529 static int monitor_device_index
= 0;
3535 if (strstart(optarg
, "chardev:", &p
)) {
3536 snprintf(label
, sizeof(label
), "%s", p
);
3538 if (monitor_device_index
) {
3539 snprintf(label
, sizeof(label
), "monitor%d",
3540 monitor_device_index
);
3542 snprintf(label
, sizeof(label
), "monitor");
3545 opts
= qemu_chr_parse_compat(label
, optarg
);
3547 fprintf(stderr
, "parse error: %s\n", optarg
);
3552 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3554 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3557 qemu_opt_set(opts
, "mode", mode
);
3558 qemu_opt_set(opts
, "chardev", label
);
3560 qemu_opt_set(opts
, "default", "on");
3561 monitor_device_index
++;
3564 struct device_config
{
3566 DEV_USB
, /* -usbdevice */
3568 DEV_SERIAL
, /* -serial */
3569 DEV_PARALLEL
, /* -parallel */
3570 DEV_VIRTCON
, /* -virtioconsole */
3571 DEV_DEBUGCON
, /* -debugcon */
3573 const char *cmdline
;
3574 QTAILQ_ENTRY(device_config
) next
;
3576 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3578 static void add_device_config(int type
, const char *cmdline
)
3580 struct device_config
*conf
;
3582 conf
= qemu_mallocz(sizeof(*conf
));
3584 conf
->cmdline
= cmdline
;
3585 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3588 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3590 struct device_config
*conf
;
3593 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3594 if (conf
->type
!= type
)
3596 rc
= func(conf
->cmdline
);
3603 static int serial_parse(const char *devname
)
3605 static int index
= 0;
3608 if (strcmp(devname
, "none") == 0)
3610 if (index
== MAX_SERIAL_PORTS
) {
3611 fprintf(stderr
, "qemu: too many serial ports\n");
3614 snprintf(label
, sizeof(label
), "serial%d", index
);
3615 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3616 if (!serial_hds
[index
]) {
3617 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3618 devname
, strerror(errno
));
3625 static int parallel_parse(const char *devname
)
3627 static int index
= 0;
3630 if (strcmp(devname
, "none") == 0)
3632 if (index
== MAX_PARALLEL_PORTS
) {
3633 fprintf(stderr
, "qemu: too many parallel ports\n");
3636 snprintf(label
, sizeof(label
), "parallel%d", index
);
3637 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3638 if (!parallel_hds
[index
]) {
3639 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3640 devname
, strerror(errno
));
3647 static int virtcon_parse(const char *devname
)
3649 static int index
= 0;
3651 QemuOpts
*bus_opts
, *dev_opts
;
3653 if (strcmp(devname
, "none") == 0)
3655 if (index
== MAX_VIRTIO_CONSOLES
) {
3656 fprintf(stderr
, "qemu: too many virtio consoles\n");
3660 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3661 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3663 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3664 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3666 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3667 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3668 if (!virtcon_hds
[index
]) {
3669 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3670 devname
, strerror(errno
));
3673 qemu_opt_set(dev_opts
, "chardev", label
);
3679 static int debugcon_parse(const char *devname
)
3683 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3686 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3688 fprintf(stderr
, "qemu: already have a debugcon device\n");
3691 qemu_opt_set(opts
, "driver", "isa-debugcon");
3692 qemu_opt_set(opts
, "chardev", "debugcon");
3696 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3697 const char **poptarg
, int *poptind
)
3699 const QEMUOption
*popt
;
3700 int optind
= *poptind
;
3701 char *r
= argv
[optind
];
3704 loc_set_cmdline(argv
, optind
, 1);
3706 /* Treat --foo the same as -foo. */
3709 popt
= qemu_options
;
3712 error_report("invalid option");
3715 if (!strcmp(popt
->name
, r
+ 1))
3719 if (popt
->flags
& HAS_ARG
) {
3720 if (optind
>= argc
) {
3721 error_report("requires an argument");
3724 optarg
= argv
[optind
++];
3725 loc_set_cmdline(argv
, optind
- 2, 2);
3736 int main(int argc
, char **argv
, char **envp
)
3738 const char *gdbstub_dev
= NULL
;
3739 uint32_t boot_devices_bitmap
= 0;
3741 int snapshot
, linux_boot
, net_boot
;
3742 const char *icount_option
= NULL
;
3743 const char *initrd_filename
;
3744 const char *kernel_filename
, *kernel_cmdline
;
3745 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3747 DisplayChangeListener
*dcl
;
3748 int cyls
, heads
, secs
, translation
;
3749 QemuOpts
*hda_opts
= NULL
, *opts
;
3752 const char *loadvm
= NULL
;
3753 QEMUMachine
*machine
;
3754 const char *cpu_model
;
3759 const char *pid_file
= NULL
;
3760 const char *incoming
= NULL
;
3763 struct passwd
*pwd
= NULL
;
3764 const char *chroot_dir
= NULL
;
3765 const char *run_as
= NULL
;
3768 int show_vnc_port
= 0;
3771 error_set_progname(argv
[0]);
3773 exit_notifier_init();
3777 qemu_cache_utils_init(envp
);
3779 QLIST_INIT (&vm_change_state_head
);
3782 struct sigaction act
;
3783 sigfillset(&act
.sa_mask
);
3785 act
.sa_handler
= SIG_IGN
;
3786 sigaction(SIGPIPE
, &act
, NULL
);
3789 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3790 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3791 QEMU to run on a single CPU */
3796 h
= GetCurrentProcess();
3797 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3798 for(i
= 0; i
< 32; i
++) {
3799 if (mask
& (1 << i
))
3804 SetProcessAffinityMask(h
, mask
);
3810 module_call_init(MODULE_INIT_MACHINE
);
3811 machine
= find_default_machine();
3813 initrd_filename
= NULL
;
3816 kernel_filename
= NULL
;
3817 kernel_cmdline
= "";
3818 cyls
= heads
= secs
= 0;
3819 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3821 for (i
= 0; i
< MAX_NODES
; i
++) {
3823 node_cpumask
[i
] = 0;
3832 /* first pass of option parsing */
3834 while (optind
< argc
) {
3835 if (argv
[optind
][0] != '-') {
3840 const QEMUOption
*popt
;
3842 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3843 switch (popt
->index
) {
3844 case QEMU_OPTION_nodefconfig
:
3855 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3856 fp
= fopen(fname
, "r");
3858 if (qemu_config_parse(fp
, fname
) != 0) {
3864 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3865 fp
= fopen(fname
, "r");
3867 if (qemu_config_parse(fp
, fname
) != 0) {
3873 #if defined(cpudef_setup)
3874 cpudef_setup(); /* parse cpu definitions in target config file */
3877 /* second pass of option parsing */
3882 if (argv
[optind
][0] != '-') {
3883 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3885 const QEMUOption
*popt
;
3887 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3888 switch(popt
->index
) {
3890 machine
= find_machine(optarg
);
3893 printf("Supported machines are:\n");
3894 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3896 printf("%-10s %s (alias of %s)\n",
3897 m
->alias
, m
->desc
, m
->name
);
3898 printf("%-10s %s%s\n",
3900 m
->is_default
? " (default)" : "");
3902 exit(*optarg
!= '?');
3905 case QEMU_OPTION_cpu
:
3906 /* hw initialization will check this */
3907 if (*optarg
== '?') {
3908 /* XXX: implement xxx_cpu_list for targets that still miss it */
3909 #if defined(cpu_list_id)
3910 cpu_list_id(stdout
, &fprintf
, optarg
);
3911 #elif defined(cpu_list)
3912 cpu_list(stdout
, &fprintf
); /* deprecated */
3919 case QEMU_OPTION_initrd
:
3920 initrd_filename
= optarg
;
3922 case QEMU_OPTION_hda
:
3924 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3926 hda_opts
= drive_add(optarg
, HD_ALIAS
3927 ",cyls=%d,heads=%d,secs=%d%s",
3928 0, cyls
, heads
, secs
,
3929 translation
== BIOS_ATA_TRANSLATION_LBA
?
3931 translation
== BIOS_ATA_TRANSLATION_NONE
?
3932 ",trans=none" : "");
3934 case QEMU_OPTION_hdb
:
3935 case QEMU_OPTION_hdc
:
3936 case QEMU_OPTION_hdd
:
3937 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3939 case QEMU_OPTION_drive
:
3940 drive_add(NULL
, "%s", optarg
);
3942 case QEMU_OPTION_set
:
3943 if (qemu_set_option(optarg
) != 0)
3946 case QEMU_OPTION_global
:
3947 if (qemu_global_option(optarg
) != 0)
3950 case QEMU_OPTION_mtdblock
:
3951 drive_add(optarg
, MTD_ALIAS
);
3953 case QEMU_OPTION_sd
:
3954 drive_add(optarg
, SD_ALIAS
);
3956 case QEMU_OPTION_pflash
:
3957 drive_add(optarg
, PFLASH_ALIAS
);
3959 case QEMU_OPTION_snapshot
:
3962 case QEMU_OPTION_hdachs
:
3966 cyls
= strtol(p
, (char **)&p
, 0);
3967 if (cyls
< 1 || cyls
> 16383)
3972 heads
= strtol(p
, (char **)&p
, 0);
3973 if (heads
< 1 || heads
> 16)
3978 secs
= strtol(p
, (char **)&p
, 0);
3979 if (secs
< 1 || secs
> 63)
3983 if (!strcmp(p
, "none"))
3984 translation
= BIOS_ATA_TRANSLATION_NONE
;
3985 else if (!strcmp(p
, "lba"))
3986 translation
= BIOS_ATA_TRANSLATION_LBA
;
3987 else if (!strcmp(p
, "auto"))
3988 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3991 } else if (*p
!= '\0') {
3993 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3996 if (hda_opts
!= NULL
) {
3998 snprintf(num
, sizeof(num
), "%d", cyls
);
3999 qemu_opt_set(hda_opts
, "cyls", num
);
4000 snprintf(num
, sizeof(num
), "%d", heads
);
4001 qemu_opt_set(hda_opts
, "heads", num
);
4002 snprintf(num
, sizeof(num
), "%d", secs
);
4003 qemu_opt_set(hda_opts
, "secs", num
);
4004 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4005 qemu_opt_set(hda_opts
, "trans", "lba");
4006 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4007 qemu_opt_set(hda_opts
, "trans", "none");
4011 case QEMU_OPTION_numa
:
4012 if (nb_numa_nodes
>= MAX_NODES
) {
4013 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4018 case QEMU_OPTION_nographic
:
4019 display_type
= DT_NOGRAPHIC
;
4021 #ifdef CONFIG_CURSES
4022 case QEMU_OPTION_curses
:
4023 display_type
= DT_CURSES
;
4026 case QEMU_OPTION_portrait
:
4029 case QEMU_OPTION_kernel
:
4030 kernel_filename
= optarg
;
4032 case QEMU_OPTION_append
:
4033 kernel_cmdline
= optarg
;
4035 case QEMU_OPTION_cdrom
:
4036 drive_add(optarg
, CDROM_ALIAS
);
4038 case QEMU_OPTION_boot
:
4040 static const char * const params
[] = {
4041 "order", "once", "menu", NULL
4043 char buf
[sizeof(boot_devices
)];
4044 char *standard_boot_devices
;
4047 if (!strchr(optarg
, '=')) {
4049 pstrcpy(buf
, sizeof(buf
), optarg
);
4050 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4052 "qemu: unknown boot parameter '%s' in '%s'\n",
4058 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4059 boot_devices_bitmap
= parse_bootdevices(buf
);
4060 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4063 if (get_param_value(buf
, sizeof(buf
),
4065 boot_devices_bitmap
|= parse_bootdevices(buf
);
4066 standard_boot_devices
= qemu_strdup(boot_devices
);
4067 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4068 qemu_register_reset(restore_boot_devices
,
4069 standard_boot_devices
);
4071 if (get_param_value(buf
, sizeof(buf
),
4073 if (!strcmp(buf
, "on")) {
4075 } else if (!strcmp(buf
, "off")) {
4079 "qemu: invalid option value '%s'\n",
4087 case QEMU_OPTION_fda
:
4088 case QEMU_OPTION_fdb
:
4089 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4092 case QEMU_OPTION_no_fd_bootchk
:
4096 case QEMU_OPTION_netdev
:
4097 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4101 case QEMU_OPTION_net
:
4102 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4107 case QEMU_OPTION_tftp
:
4108 legacy_tftp_prefix
= optarg
;
4110 case QEMU_OPTION_bootp
:
4111 legacy_bootp_filename
= optarg
;
4114 case QEMU_OPTION_smb
:
4115 if (net_slirp_smb(optarg
) < 0)
4119 case QEMU_OPTION_redir
:
4120 if (net_slirp_redir(optarg
) < 0)
4124 case QEMU_OPTION_bt
:
4125 add_device_config(DEV_BT
, optarg
);
4128 case QEMU_OPTION_audio_help
:
4132 case QEMU_OPTION_soundhw
:
4133 select_soundhw (optarg
);
4139 case QEMU_OPTION_version
:
4143 case QEMU_OPTION_m
: {
4147 value
= strtoul(optarg
, &ptr
, 10);
4149 case 0: case 'M': case 'm':
4156 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4160 /* On 32-bit hosts, QEMU is limited by virtual address space */
4161 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4162 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4165 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4166 fprintf(stderr
, "qemu: ram size too large\n");
4172 case QEMU_OPTION_mempath
:
4176 case QEMU_OPTION_mem_prealloc
:
4183 const CPULogItem
*item
;
4185 mask
= cpu_str_to_log_mask(optarg
);
4187 printf("Log items (comma separated):\n");
4188 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4189 printf("%-10s %s\n", item
->name
, item
->help
);
4197 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4199 case QEMU_OPTION_gdb
:
4200 gdbstub_dev
= optarg
;
4205 case QEMU_OPTION_bios
:
4208 case QEMU_OPTION_singlestep
:
4215 keyboard_layout
= optarg
;
4217 case QEMU_OPTION_localtime
:
4220 case QEMU_OPTION_vga
:
4221 select_vgahw (optarg
);
4223 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4229 w
= strtol(p
, (char **)&p
, 10);
4232 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4238 h
= strtol(p
, (char **)&p
, 10);
4243 depth
= strtol(p
, (char **)&p
, 10);
4244 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4245 depth
!= 24 && depth
!= 32)
4247 } else if (*p
== '\0') {
4248 depth
= graphic_depth
;
4255 graphic_depth
= depth
;
4259 case QEMU_OPTION_echr
:
4262 term_escape_char
= strtol(optarg
, &r
, 0);
4264 printf("Bad argument to echr\n");
4267 case QEMU_OPTION_monitor
:
4268 monitor_parse(optarg
, "readline");
4269 default_monitor
= 0;
4271 case QEMU_OPTION_qmp
:
4272 monitor_parse(optarg
, "control");
4275 case QEMU_OPTION_mon
:
4276 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4278 fprintf(stderr
, "parse error: %s\n", optarg
);
4282 mode
= qemu_opt_get(opts
, "mode");
4283 if (mode
== NULL
|| strcmp(mode
, "readline") == 0) {
4284 default_monitor
= 0;
4285 } else if (strcmp(mode
, "control") == 0) {
4290 case QEMU_OPTION_chardev
:
4291 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4293 fprintf(stderr
, "parse error: %s\n", optarg
);
4297 case QEMU_OPTION_serial
:
4298 add_device_config(DEV_SERIAL
, optarg
);
4300 if (strncmp(optarg
, "mon:", 4) == 0) {
4301 default_monitor
= 0;
4304 case QEMU_OPTION_watchdog
:
4307 "qemu: only one watchdog option may be given\n");
4312 case QEMU_OPTION_watchdog_action
:
4313 if (select_watchdog_action(optarg
) == -1) {
4314 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4318 case QEMU_OPTION_virtiocon
:
4319 add_device_config(DEV_VIRTCON
, optarg
);
4320 default_virtcon
= 0;
4321 if (strncmp(optarg
, "mon:", 4) == 0) {
4322 default_monitor
= 0;
4325 case QEMU_OPTION_parallel
:
4326 add_device_config(DEV_PARALLEL
, optarg
);
4327 default_parallel
= 0;
4328 if (strncmp(optarg
, "mon:", 4) == 0) {
4329 default_monitor
= 0;
4332 case QEMU_OPTION_debugcon
:
4333 add_device_config(DEV_DEBUGCON
, optarg
);
4335 case QEMU_OPTION_loadvm
:
4338 case QEMU_OPTION_full_screen
:
4342 case QEMU_OPTION_no_frame
:
4345 case QEMU_OPTION_alt_grab
:
4348 case QEMU_OPTION_ctrl_grab
:
4351 case QEMU_OPTION_no_quit
:
4354 case QEMU_OPTION_sdl
:
4355 display_type
= DT_SDL
;
4358 case QEMU_OPTION_pidfile
:
4362 case QEMU_OPTION_win2k_hack
:
4363 win2k_install_hack
= 1;
4365 case QEMU_OPTION_rtc_td_hack
:
4368 case QEMU_OPTION_acpitable
:
4369 if(acpi_table_add(optarg
) < 0) {
4370 fprintf(stderr
, "Wrong acpi table provided\n");
4374 case QEMU_OPTION_smbios
:
4375 if(smbios_entry_add(optarg
) < 0) {
4376 fprintf(stderr
, "Wrong smbios provided\n");
4382 case QEMU_OPTION_enable_kvm
:
4386 case QEMU_OPTION_usb
:
4389 case QEMU_OPTION_usbdevice
:
4391 add_device_config(DEV_USB
, optarg
);
4393 case QEMU_OPTION_device
:
4394 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4398 case QEMU_OPTION_smp
:
4401 fprintf(stderr
, "Invalid number of CPUs\n");
4404 if (max_cpus
< smp_cpus
) {
4405 fprintf(stderr
, "maxcpus must be equal to or greater than "
4409 if (max_cpus
> 255) {
4410 fprintf(stderr
, "Unsupported number of maxcpus\n");
4414 case QEMU_OPTION_vnc
:
4415 display_type
= DT_VNC
;
4416 vnc_display
= optarg
;
4419 case QEMU_OPTION_no_acpi
:
4422 case QEMU_OPTION_no_hpet
:
4425 case QEMU_OPTION_balloon
:
4426 if (balloon_parse(optarg
) < 0) {
4427 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4432 case QEMU_OPTION_no_reboot
:
4435 case QEMU_OPTION_no_shutdown
:
4438 case QEMU_OPTION_show_cursor
:
4441 case QEMU_OPTION_uuid
:
4442 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4443 fprintf(stderr
, "Fail to parse UUID string."
4444 " Wrong format.\n");
4449 case QEMU_OPTION_daemonize
:
4453 case QEMU_OPTION_option_rom
:
4454 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4455 fprintf(stderr
, "Too many option ROMs\n");
4458 option_rom
[nb_option_roms
] = optarg
;
4461 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4462 case QEMU_OPTION_semihosting
:
4463 semihosting_enabled
= 1;
4466 case QEMU_OPTION_name
:
4467 qemu_name
= qemu_strdup(optarg
);
4469 char *p
= strchr(qemu_name
, ',');
4472 if (strncmp(p
, "process=", 8)) {
4473 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4481 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4482 case QEMU_OPTION_prom_env
:
4483 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4484 fprintf(stderr
, "Too many prom variables\n");
4487 prom_envs
[nb_prom_envs
] = optarg
;
4492 case QEMU_OPTION_old_param
:
4496 case QEMU_OPTION_clock
:
4497 configure_alarms(optarg
);
4499 case QEMU_OPTION_startdate
:
4500 configure_rtc_date_offset(optarg
, 1);
4502 case QEMU_OPTION_rtc
:
4503 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4505 fprintf(stderr
, "parse error: %s\n", optarg
);
4508 configure_rtc(opts
);
4510 case QEMU_OPTION_tb_size
:
4511 tb_size
= strtol(optarg
, NULL
, 0);
4515 case QEMU_OPTION_icount
:
4516 icount_option
= optarg
;
4518 case QEMU_OPTION_incoming
:
4521 case QEMU_OPTION_nodefaults
:
4523 default_parallel
= 0;
4524 default_virtcon
= 0;
4525 default_monitor
= 0;
4534 case QEMU_OPTION_chroot
:
4535 chroot_dir
= optarg
;
4537 case QEMU_OPTION_runas
:
4542 case QEMU_OPTION_xen_domid
:
4543 xen_domid
= atoi(optarg
);
4545 case QEMU_OPTION_xen_create
:
4546 xen_mode
= XEN_CREATE
;
4548 case QEMU_OPTION_xen_attach
:
4549 xen_mode
= XEN_ATTACH
;
4552 case QEMU_OPTION_readconfig
:
4555 fp
= fopen(optarg
, "r");
4557 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4560 if (qemu_config_parse(fp
, optarg
) != 0) {
4566 case QEMU_OPTION_writeconfig
:
4569 if (strcmp(optarg
, "-") == 0) {
4572 fp
= fopen(optarg
, "w");
4574 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4578 qemu_config_write(fp
);
4587 /* If no data_dir is specified then try to find it relative to the
4590 data_dir
= find_datadir(argv
[0]);
4592 /* If all else fails use the install patch specified when building. */
4594 data_dir
= CONFIG_QEMU_SHAREDIR
;
4598 * Default to max_cpus = smp_cpus, in case the user doesn't
4599 * specify a max_cpus value.
4602 max_cpus
= smp_cpus
;
4604 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4605 if (smp_cpus
> machine
->max_cpus
) {
4606 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4607 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4612 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4613 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4615 if (machine
->no_serial
) {
4618 if (machine
->no_parallel
) {
4619 default_parallel
= 0;
4621 if (!machine
->use_virtcon
) {
4622 default_virtcon
= 0;
4624 if (machine
->no_vga
) {
4627 if (machine
->no_floppy
) {
4630 if (machine
->no_cdrom
) {
4633 if (machine
->no_sdcard
) {
4637 if (display_type
== DT_NOGRAPHIC
) {
4638 if (default_parallel
)
4639 add_device_config(DEV_PARALLEL
, "null");
4640 if (default_serial
&& default_monitor
) {
4641 add_device_config(DEV_SERIAL
, "mon:stdio");
4642 } else if (default_virtcon
&& default_monitor
) {
4643 add_device_config(DEV_VIRTCON
, "mon:stdio");
4646 add_device_config(DEV_SERIAL
, "stdio");
4647 if (default_virtcon
)
4648 add_device_config(DEV_VIRTCON
, "stdio");
4649 if (default_monitor
)
4650 monitor_parse("stdio", "readline");
4654 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4655 if (default_parallel
)
4656 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4657 if (default_monitor
)
4658 monitor_parse("vc:80Cx24C", "readline");
4659 if (default_virtcon
)
4660 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4663 vga_interface_type
= VGA_CIRRUS
;
4665 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4672 if (pipe(fds
) == -1)
4683 len
= read(fds
[0], &status
, 1);
4684 if (len
== -1 && (errno
== EINTR
))
4689 else if (status
== 1) {
4690 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4698 qemu_set_cloexec(fds
[1]);
4710 signal(SIGTSTP
, SIG_IGN
);
4711 signal(SIGTTOU
, SIG_IGN
);
4712 signal(SIGTTIN
, SIG_IGN
);
4716 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4720 if (write(fds
[1], &status
, 1) != 1) {
4721 perror("daemonize. Writing to pipe\n");
4725 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4729 if (kvm_enabled()) {
4732 ret
= kvm_init(smp_cpus
);
4734 fprintf(stderr
, "failed to initialize KVM\n");
4739 if (qemu_init_main_loop()) {
4740 fprintf(stderr
, "qemu_init_main_loop failed\n");
4743 linux_boot
= (kernel_filename
!= NULL
);
4745 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4746 fprintf(stderr
, "-append only allowed with -kernel option\n");
4750 if (!linux_boot
&& initrd_filename
!= NULL
) {
4751 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4756 /* Win32 doesn't support line-buffering and requires size >= 2 */
4757 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4760 if (init_timer_alarm() < 0) {
4761 fprintf(stderr
, "could not initialize alarm timer\n");
4764 configure_icount(icount_option
);
4770 if (net_init_clients() < 0) {
4774 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4775 net_set_boot_mask(net_boot
);
4777 /* init the bluetooth world */
4778 if (foreach_device_config(DEV_BT
, bt_parse
))
4781 /* init the memory */
4783 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4785 /* init the dynamic translator */
4786 cpu_exec_init_all(tb_size
* 1024 * 1024);
4788 bdrv_init_with_whitelist();
4792 if (default_cdrom
) {
4793 /* we always create the cdrom drive, even if no disk is there */
4794 drive_add(NULL
, CDROM_ALIAS
);
4797 if (default_floppy
) {
4798 /* we always create at least one floppy */
4799 drive_add(NULL
, FD_ALIAS
, 0);
4802 if (default_sdcard
) {
4803 /* we always create one sd slot, even if no card is in it */
4804 drive_add(NULL
, SD_ALIAS
);
4807 /* open the virtual block devices */
4809 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4810 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4813 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4816 if (nb_numa_nodes
> 0) {
4819 if (nb_numa_nodes
> smp_cpus
) {
4820 nb_numa_nodes
= smp_cpus
;
4823 /* If no memory size if given for any node, assume the default case
4824 * and distribute the available memory equally across all nodes
4826 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4827 if (node_mem
[i
] != 0)
4830 if (i
== nb_numa_nodes
) {
4831 uint64_t usedmem
= 0;
4833 /* On Linux, the each node's border has to be 8MB aligned,
4834 * the final node gets the rest.
4836 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4837 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4838 usedmem
+= node_mem
[i
];
4840 node_mem
[i
] = ram_size
- usedmem
;
4843 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4844 if (node_cpumask
[i
] != 0)
4847 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4848 * must cope with this anyway, because there are BIOSes out there in
4849 * real machines which also use this scheme.
4851 if (i
== nb_numa_nodes
) {
4852 for (i
= 0; i
< smp_cpus
; i
++) {
4853 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4858 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4860 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4862 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4864 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4867 module_call_init(MODULE_INIT_DEVICE
);
4869 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4873 i
= select_watchdog(watchdog
);
4875 exit (i
== 1 ? 1 : 0);
4878 if (machine
->compat_props
) {
4879 qdev_prop_register_global_list(machine
->compat_props
);
4883 machine
->init(ram_size
, boot_devices
,
4884 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4886 cpu_synchronize_all_post_init();
4889 /* must be after terminal init, SDL library changes signal handlers */
4893 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4894 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4895 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4901 current_machine
= machine
;
4903 /* init USB devices */
4905 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4909 /* init generic devices */
4910 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4913 net_check_clients();
4915 /* just use the first displaystate for the moment */
4916 ds
= get_displaystate();
4918 if (display_type
== DT_DEFAULT
) {
4919 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4920 display_type
= DT_SDL
;
4922 display_type
= DT_VNC
;
4923 vnc_display
= "localhost:0,to=99";
4929 switch (display_type
) {
4932 #if defined(CONFIG_CURSES)
4934 curses_display_init(ds
, full_screen
);
4937 #if defined(CONFIG_SDL)
4939 sdl_display_init(ds
, full_screen
, no_frame
);
4941 #elif defined(CONFIG_COCOA)
4943 cocoa_display_init(ds
, full_screen
);
4947 vnc_display_init(ds
);
4948 if (vnc_display_open(ds
, vnc_display
) < 0)
4951 if (show_vnc_port
) {
4952 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4960 dcl
= ds
->listeners
;
4961 while (dcl
!= NULL
) {
4962 if (dcl
->dpy_refresh
!= NULL
) {
4963 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
4964 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
4969 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
4970 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
4971 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
4974 text_consoles_set_display(ds
);
4976 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
4979 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
4980 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
4985 qdev_machine_creation_done();
4987 if (rom_load_all() != 0) {
4988 fprintf(stderr
, "rom loading failed\n");
4992 qemu_system_reset();
4994 if (load_vmstate(loadvm
) < 0) {
5000 qemu_start_incoming_migration(incoming
);
5001 } else if (autostart
) {
5011 len
= write(fds
[1], &status
, 1);
5012 if (len
== -1 && (errno
== EINTR
))
5019 perror("not able to chdir to /");
5022 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5028 pwd
= getpwnam(run_as
);
5030 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5036 if (chroot(chroot_dir
) < 0) {
5037 fprintf(stderr
, "chroot failed\n");
5041 perror("not able to chdir to /");
5047 if (setgid(pwd
->pw_gid
) < 0) {
5048 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5051 if (setuid(pwd
->pw_uid
) < 0) {
5052 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5055 if (setuid(0) != -1) {
5056 fprintf(stderr
, "Dropping privileges failed\n");