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;
281 { .driver
= "isa-serial", .flag
= &default_serial
},
282 { .driver
= "isa-parallel", .flag
= &default_parallel
},
283 { .driver
= "isa-fdc", .flag
= &default_floppy
},
284 { .driver
= "ide-drive", .flag
= &default_cdrom
},
285 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
286 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
287 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
288 { .driver
= "VGA", .flag
= &default_vga
},
289 { .driver
= "cirrus-vga", .flag
= &default_vga
},
290 { .driver
= "vmware-svga", .flag
= &default_vga
},
293 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
295 const char *driver
= qemu_opt_get(opts
, "driver");
300 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
301 if (strcmp(default_list
[i
].driver
, driver
) != 0)
303 *(default_list
[i
].flag
) = 0;
308 /***********************************************************/
309 /* x86 ISA bus support */
311 target_phys_addr_t isa_mem_base
= 0;
314 /***********************************************************/
315 void hw_error(const char *fmt
, ...)
321 fprintf(stderr
, "qemu: hardware error: ");
322 vfprintf(stderr
, fmt
, ap
);
323 fprintf(stderr
, "\n");
324 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
325 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
327 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
329 cpu_dump_state(env
, stderr
, fprintf
, 0);
336 static void set_proc_name(const char *s
)
338 #if defined(__linux__) && defined(PR_SET_NAME)
342 name
[sizeof(name
) - 1] = 0;
343 strncpy(name
, s
, sizeof(name
));
344 /* Could rewrite argv[0] too, but that's a bit more complicated.
345 This simple way is enough for `top'. */
346 prctl(PR_SET_NAME
, name
);
353 static QEMUBalloonEvent
*qemu_balloon_event
;
354 void *qemu_balloon_event_opaque
;
356 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
358 qemu_balloon_event
= func
;
359 qemu_balloon_event_opaque
= opaque
;
362 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
364 if (qemu_balloon_event
) {
365 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
372 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
374 if (qemu_balloon_event
) {
375 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
383 /***********************************************************/
384 /* real time host monotonic timer */
386 /* compute with 96 bit intermediate result: (a*b)/c */
387 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
392 #ifdef HOST_WORDS_BIGENDIAN
402 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
403 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
406 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
410 /***********************************************************/
411 /* host time/date access */
412 void qemu_get_timedate(struct tm
*tm
, int offset
)
419 if (rtc_date_offset
== -1) {
423 ret
= localtime(&ti
);
425 ti
-= rtc_date_offset
;
429 memcpy(tm
, ret
, sizeof(struct tm
));
432 int qemu_timedate_diff(struct tm
*tm
)
436 if (rtc_date_offset
== -1)
438 seconds
= mktimegm(tm
);
440 seconds
= mktime(tm
);
442 seconds
= mktimegm(tm
) + rtc_date_offset
;
444 return seconds
- time(NULL
);
447 void rtc_change_mon_event(struct tm
*tm
)
451 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
452 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
453 qobject_decref(data
);
456 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
458 time_t rtc_start_date
;
461 if (!strcmp(startdate
, "now") && legacy
) {
462 rtc_date_offset
= -1;
464 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
472 } else if (sscanf(startdate
, "%d-%d-%d",
484 rtc_start_date
= mktimegm(&tm
);
485 if (rtc_start_date
== -1) {
487 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
488 "'2006-06-17T16:01:21' or '2006-06-17'\n");
491 rtc_date_offset
= time(NULL
) - rtc_start_date
;
495 static void configure_rtc(QemuOpts
*opts
)
499 value
= qemu_opt_get(opts
, "base");
501 if (!strcmp(value
, "utc")) {
503 } else if (!strcmp(value
, "localtime")) {
506 configure_rtc_date_offset(value
, 0);
509 value
= qemu_opt_get(opts
, "clock");
511 if (!strcmp(value
, "host")) {
512 rtc_clock
= host_clock
;
513 } else if (!strcmp(value
, "vm")) {
514 rtc_clock
= vm_clock
;
516 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
520 #ifdef CONFIG_TARGET_I386
521 value
= qemu_opt_get(opts
, "driftfix");
523 if (!strcmp(buf
, "slew")) {
525 } else if (!strcmp(buf
, "none")) {
528 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
536 static void socket_cleanup(void)
541 static int socket_init(void)
546 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
548 err
= WSAGetLastError();
549 fprintf(stderr
, "WSAStartup: %d\n", err
);
552 atexit(socket_cleanup
);
557 /*********************/
559 /*********************/
561 static NotifierList exit_notifiers
= NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
563 void exit_notifier_add(Notifier
*notifier
)
565 notifier_list_add(&exit_notifiers
, notifier
);
568 void exit_notifier_remove(Notifier
*notifier
)
570 notifier_list_remove(&exit_notifiers
, notifier
);
573 static void exit_notifier_notify(void)
575 notifier_list_notify(&exit_notifiers
);
578 static void exit_notifier_init(void)
580 atexit(exit_notifier_notify
);
583 /***********************************************************/
584 /* Bluetooth support */
587 static struct HCIInfo
*hci_table
[MAX_NICS
];
589 static struct bt_vlan_s
{
590 struct bt_scatternet_s net
;
592 struct bt_vlan_s
*next
;
595 /* find or alloc a new bluetooth "VLAN" */
596 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
598 struct bt_vlan_s
**pvlan
, *vlan
;
599 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
603 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
605 pvlan
= &first_bt_vlan
;
606 while (*pvlan
!= NULL
)
607 pvlan
= &(*pvlan
)->next
;
612 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
616 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
621 static struct HCIInfo null_hci
= {
622 .cmd_send
= null_hci_send
,
623 .sco_send
= null_hci_send
,
624 .acl_send
= null_hci_send
,
625 .bdaddr_set
= null_hci_addr_set
,
628 struct HCIInfo
*qemu_next_hci(void)
630 if (cur_hci
== nb_hcis
)
633 return hci_table
[cur_hci
++];
636 static struct HCIInfo
*hci_init(const char *str
)
639 struct bt_scatternet_s
*vlan
= 0;
641 if (!strcmp(str
, "null"))
644 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
646 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
647 else if (!strncmp(str
, "hci", 3)) {
650 if (!strncmp(str
+ 3, ",vlan=", 6)) {
651 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
656 vlan
= qemu_find_bt_vlan(0);
658 return bt_new_hci(vlan
);
661 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
666 static int bt_hci_parse(const char *str
)
671 if (nb_hcis
>= MAX_NICS
) {
672 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
685 bdaddr
.b
[5] = 0x56 + nb_hcis
;
686 hci
->bdaddr_set(hci
, bdaddr
.b
);
688 hci_table
[nb_hcis
++] = hci
;
693 static void bt_vhci_add(int vlan_id
)
695 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
698 fprintf(stderr
, "qemu: warning: adding a VHCI to "
699 "an empty scatternet %i\n", vlan_id
);
701 bt_vhci_init(bt_new_hci(vlan
));
704 static struct bt_device_s
*bt_device_add(const char *opt
)
706 struct bt_scatternet_s
*vlan
;
708 char *endp
= strstr(opt
, ",vlan=");
709 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
712 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
715 vlan_id
= strtol(endp
+ 6, &endp
, 0);
717 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
722 vlan
= qemu_find_bt_vlan(vlan_id
);
725 fprintf(stderr
, "qemu: warning: adding a slave device to "
726 "an empty scatternet %i\n", vlan_id
);
728 if (!strcmp(devname
, "keyboard"))
729 return bt_keyboard_init(vlan
);
731 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
735 static int bt_parse(const char *opt
)
737 const char *endp
, *p
;
740 if (strstart(opt
, "hci", &endp
)) {
741 if (!*endp
|| *endp
== ',') {
743 if (!strstart(endp
, ",vlan=", 0))
746 return bt_hci_parse(opt
);
748 } else if (strstart(opt
, "vhci", &endp
)) {
749 if (!*endp
|| *endp
== ',') {
751 if (strstart(endp
, ",vlan=", &p
)) {
752 vlan
= strtol(p
, (char **) &endp
, 0);
754 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
758 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
767 } else if (strstart(opt
, "device:", &endp
))
768 return !bt_device_add(endp
);
770 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
774 /***********************************************************/
775 /* QEMU Block devices */
777 #define HD_ALIAS "index=%d,media=disk"
778 #define CDROM_ALIAS "index=2,media=cdrom"
779 #define FD_ALIAS "index=%d,if=floppy"
780 #define PFLASH_ALIAS "if=pflash"
781 #define MTD_ALIAS "if=mtd"
782 #define SD_ALIAS "index=0,if=sd"
784 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
791 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
794 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
796 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
797 __FUNCTION__
, optstr
);
801 qemu_opt_set(opts
, "file", file
);
805 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
809 /* seek interface, bus and unit */
811 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
812 if (dinfo
->type
== type
&&
821 DriveInfo
*drive_get_by_id(const char *id
)
825 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
826 if (strcmp(id
, dinfo
->id
))
833 int drive_get_max_bus(BlockInterfaceType type
)
839 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
840 if(dinfo
->type
== type
&&
841 dinfo
->bus
> max_bus
)
842 max_bus
= dinfo
->bus
;
847 const char *drive_get_serial(BlockDriverState
*bdrv
)
851 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
852 if (dinfo
->bdrv
== bdrv
)
853 return dinfo
->serial
;
859 BlockInterfaceErrorAction
drive_get_on_error(
860 BlockDriverState
*bdrv
, int is_read
)
864 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
865 if (dinfo
->bdrv
== bdrv
)
866 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
869 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
872 static void bdrv_format_print(void *opaque
, const char *name
)
874 fprintf(stderr
, " %s", name
);
877 void drive_uninit(DriveInfo
*dinfo
)
879 qemu_opts_del(dinfo
->opts
);
880 bdrv_delete(dinfo
->bdrv
);
881 QTAILQ_REMOVE(&drives
, dinfo
, next
);
885 static int parse_block_error_action(const char *buf
, int is_read
)
887 if (!strcmp(buf
, "ignore")) {
888 return BLOCK_ERR_IGNORE
;
889 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
890 return BLOCK_ERR_STOP_ENOSPC
;
891 } else if (!strcmp(buf
, "stop")) {
892 return BLOCK_ERR_STOP_ANY
;
893 } else if (!strcmp(buf
, "report")) {
894 return BLOCK_ERR_REPORT
;
896 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
897 buf
, is_read
? "read" : "write");
902 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
906 const char *file
= NULL
;
909 const char *mediastr
= "";
910 BlockInterfaceType type
;
911 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
913 int cyls
, heads
, secs
, translation
;
914 BlockDriver
*drv
= NULL
;
915 QEMUMachine
*machine
= opaque
;
922 int on_read_error
, on_write_error
;
929 translation
= BIOS_ATA_TRANSLATION_AUTO
;
932 if (machine
&& machine
->use_scsi
) {
934 max_devs
= MAX_SCSI_DEVS
;
935 pstrcpy(devname
, sizeof(devname
), "scsi");
938 max_devs
= MAX_IDE_DEVS
;
939 pstrcpy(devname
, sizeof(devname
), "ide");
943 /* extract parameters */
944 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
945 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
946 index
= qemu_opt_get_number(opts
, "index", -1);
948 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
949 heads
= qemu_opt_get_number(opts
, "heads", 0);
950 secs
= qemu_opt_get_number(opts
, "secs", 0);
952 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
953 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
955 file
= qemu_opt_get(opts
, "file");
956 serial
= qemu_opt_get(opts
, "serial");
958 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
959 pstrcpy(devname
, sizeof(devname
), buf
);
960 if (!strcmp(buf
, "ide")) {
962 max_devs
= MAX_IDE_DEVS
;
963 } else if (!strcmp(buf
, "scsi")) {
965 max_devs
= MAX_SCSI_DEVS
;
966 } else if (!strcmp(buf
, "floppy")) {
969 } else if (!strcmp(buf
, "pflash")) {
972 } else if (!strcmp(buf
, "mtd")) {
975 } else if (!strcmp(buf
, "sd")) {
978 } else if (!strcmp(buf
, "virtio")) {
981 } else if (!strcmp(buf
, "xen")) {
984 } else if (!strcmp(buf
, "none")) {
988 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
993 if (cyls
|| heads
|| secs
) {
994 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
995 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
998 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
999 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1002 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1003 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1008 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1011 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1015 if (!strcmp(buf
, "none"))
1016 translation
= BIOS_ATA_TRANSLATION_NONE
;
1017 else if (!strcmp(buf
, "lba"))
1018 translation
= BIOS_ATA_TRANSLATION_LBA
;
1019 else if (!strcmp(buf
, "auto"))
1020 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1022 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1027 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1028 if (!strcmp(buf
, "disk")) {
1030 } else if (!strcmp(buf
, "cdrom")) {
1031 if (cyls
|| secs
|| heads
) {
1033 "qemu: '%s' invalid physical CHS format\n", buf
);
1036 media
= MEDIA_CDROM
;
1038 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1043 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1044 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1046 else if (!strcmp(buf
, "writethrough"))
1048 else if (!strcmp(buf
, "writeback"))
1051 fprintf(stderr
, "qemu: invalid cache option\n");
1056 #ifdef CONFIG_LINUX_AIO
1057 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1058 if (!strcmp(buf
, "threads"))
1060 else if (!strcmp(buf
, "native"))
1063 fprintf(stderr
, "qemu: invalid aio option\n");
1069 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1070 if (strcmp(buf
, "?") == 0) {
1071 fprintf(stderr
, "qemu: Supported formats:");
1072 bdrv_iterate_format(bdrv_format_print
, NULL
);
1073 fprintf(stderr
, "\n");
1076 drv
= bdrv_find_whitelisted_format(buf
);
1078 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1083 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1084 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1085 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1086 fprintf(stderr
, "werror is no supported by this format\n");
1090 on_write_error
= parse_block_error_action(buf
, 0);
1091 if (on_write_error
< 0) {
1096 on_read_error
= BLOCK_ERR_REPORT
;
1097 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1098 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1099 fprintf(stderr
, "rerror is no supported by this format\n");
1103 on_read_error
= parse_block_error_action(buf
, 1);
1104 if (on_read_error
< 0) {
1109 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1110 if (type
!= IF_VIRTIO
) {
1111 fprintf(stderr
, "addr is not supported\n");
1116 /* compute bus and unit according index */
1119 if (bus_id
!= 0 || unit_id
!= -1) {
1121 "qemu: index cannot be used with bus and unit\n");
1129 unit_id
= index
% max_devs
;
1130 bus_id
= index
/ max_devs
;
1134 /* if user doesn't specify a unit_id,
1135 * try to find the first free
1138 if (unit_id
== -1) {
1140 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1142 if (max_devs
&& unit_id
>= max_devs
) {
1143 unit_id
-= max_devs
;
1151 if (max_devs
&& unit_id
>= max_devs
) {
1152 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1153 unit_id
, max_devs
- 1);
1158 * ignore multiple definitions
1161 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1168 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1169 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1170 dinfo
->id
= qemu_strdup(buf
);
1172 /* no id supplied -> create one */
1173 dinfo
->id
= qemu_mallocz(32);
1174 if (type
== IF_IDE
|| type
== IF_SCSI
)
1175 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1177 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1178 devname
, bus_id
, mediastr
, unit_id
);
1180 snprintf(dinfo
->id
, 32, "%s%s%i",
1181 devname
, mediastr
, unit_id
);
1183 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1184 dinfo
->devaddr
= devaddr
;
1186 dinfo
->bus
= bus_id
;
1187 dinfo
->unit
= unit_id
;
1188 dinfo
->on_read_error
= on_read_error
;
1189 dinfo
->on_write_error
= on_write_error
;
1192 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1193 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1203 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1204 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1208 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1213 /* FIXME: This isn't really a floppy, but it's a reasonable
1216 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1222 /* add virtio block device */
1223 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1224 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1225 qemu_opt_set(opts
, "drive", dinfo
->id
);
1227 qemu_opt_set(opts
, "addr", devaddr
);
1238 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1239 cache
= 2; /* always use write-back with snapshot */
1241 if (cache
== 0) /* no caching */
1242 bdrv_flags
|= BDRV_O_NOCACHE
;
1243 else if (cache
== 2) /* write-back */
1244 bdrv_flags
|= BDRV_O_CACHE_WB
;
1247 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1249 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1253 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1254 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1259 * cdrom is read-only. Set it now, after above interface checking
1260 * since readonly attribute not explicitly required, so no error.
1262 if (media
== MEDIA_CDROM
) {
1265 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1267 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1268 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1269 file
, strerror(errno
));
1273 if (bdrv_key_required(dinfo
->bdrv
))
1279 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1281 QEMUMachine
*machine
= opaque
;
1282 int fatal_error
= 0;
1284 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1291 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1293 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1294 qemu_opt_set(opts
, "snapshot", "on");
1299 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1301 boot_set_handler
= func
;
1302 boot_set_opaque
= opaque
;
1305 int qemu_boot_set(const char *boot_devices
)
1307 if (!boot_set_handler
) {
1310 return boot_set_handler(boot_set_opaque
, boot_devices
);
1313 static int parse_bootdevices(char *devices
)
1315 /* We just do some generic consistency checks */
1319 for (p
= devices
; *p
!= '\0'; p
++) {
1320 /* Allowed boot devices are:
1321 * a-b: floppy disk drives
1322 * c-f: IDE disk drives
1323 * g-m: machine implementation dependant drives
1324 * n-p: network devices
1325 * It's up to each machine implementation to check if the given boot
1326 * devices match the actual hardware implementation and firmware
1329 if (*p
< 'a' || *p
> 'p') {
1330 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1333 if (bitmap
& (1 << (*p
- 'a'))) {
1334 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1337 bitmap
|= 1 << (*p
- 'a');
1342 static void restore_boot_devices(void *opaque
)
1344 char *standard_boot_devices
= opaque
;
1346 qemu_boot_set(standard_boot_devices
);
1348 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1349 qemu_free(standard_boot_devices
);
1352 static void numa_add(const char *optarg
)
1356 unsigned long long value
, endvalue
;
1359 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1360 if (!strcmp(option
, "node")) {
1361 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1362 nodenr
= nb_numa_nodes
;
1364 nodenr
= strtoull(option
, NULL
, 10);
1367 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1368 node_mem
[nodenr
] = 0;
1370 value
= strtoull(option
, &endptr
, 0);
1372 case 0: case 'M': case 'm':
1379 node_mem
[nodenr
] = value
;
1381 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1382 node_cpumask
[nodenr
] = 0;
1384 value
= strtoull(option
, &endptr
, 10);
1387 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1389 if (*endptr
== '-') {
1390 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1391 if (endvalue
>= 63) {
1394 "only 63 CPUs in NUMA mode supported.\n");
1396 value
= (2ULL << endvalue
) - (1ULL << value
);
1398 value
= 1ULL << value
;
1401 node_cpumask
[nodenr
] = value
;
1408 static void smp_parse(const char *optarg
)
1410 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1414 smp
= strtoul(optarg
, &endptr
, 10);
1415 if (endptr
!= optarg
) {
1416 if (*endptr
== ',') {
1420 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1421 sockets
= strtoull(option
, NULL
, 10);
1422 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1423 cores
= strtoull(option
, NULL
, 10);
1424 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1425 threads
= strtoull(option
, NULL
, 10);
1426 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1427 max_cpus
= strtoull(option
, NULL
, 10);
1429 /* compute missing values, prefer sockets over cores over threads */
1430 if (smp
== 0 || sockets
== 0) {
1431 sockets
= sockets
> 0 ? sockets
: 1;
1432 cores
= cores
> 0 ? cores
: 1;
1433 threads
= threads
> 0 ? threads
: 1;
1435 smp
= cores
* threads
* sockets
;
1439 threads
= threads
> 0 ? threads
: 1;
1440 cores
= smp
/ (sockets
* threads
);
1443 threads
= smp
/ (cores
* sockets
);
1448 smp_cores
= cores
> 0 ? cores
: 1;
1449 smp_threads
= threads
> 0 ? threads
: 1;
1451 max_cpus
= smp_cpus
;
1454 /***********************************************************/
1457 static int usb_device_add(const char *devname
, int is_hotplug
)
1460 USBDevice
*dev
= NULL
;
1465 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1466 dev
= usbdevice_create(devname
);
1470 /* the other ones */
1471 if (strstart(devname
, "host:", &p
)) {
1472 dev
= usb_host_device_open(p
);
1473 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1474 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1475 bt_new_hci(qemu_find_bt_vlan(0)));
1486 static int usb_device_del(const char *devname
)
1491 if (strstart(devname
, "host:", &p
))
1492 return usb_host_device_close(p
);
1497 p
= strchr(devname
, '.');
1500 bus_num
= strtoul(devname
, NULL
, 0);
1501 addr
= strtoul(p
+ 1, NULL
, 0);
1503 return usb_device_delete_addr(bus_num
, addr
);
1506 static int usb_parse(const char *cmdline
)
1509 r
= usb_device_add(cmdline
, 0);
1511 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1516 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1518 const char *devname
= qdict_get_str(qdict
, "devname");
1519 if (usb_device_add(devname
, 1) < 0) {
1520 error_report("could not add USB device '%s'", devname
);
1524 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1526 const char *devname
= qdict_get_str(qdict
, "devname");
1527 if (usb_device_del(devname
) < 0) {
1528 error_report("could not delete USB device '%s'", devname
);
1532 /***********************************************************/
1533 /* PCMCIA/Cardbus */
1535 static struct pcmcia_socket_entry_s
{
1536 PCMCIASocket
*socket
;
1537 struct pcmcia_socket_entry_s
*next
;
1538 } *pcmcia_sockets
= 0;
1540 void pcmcia_socket_register(PCMCIASocket
*socket
)
1542 struct pcmcia_socket_entry_s
*entry
;
1544 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1545 entry
->socket
= socket
;
1546 entry
->next
= pcmcia_sockets
;
1547 pcmcia_sockets
= entry
;
1550 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1552 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1554 ptr
= &pcmcia_sockets
;
1555 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1556 if (entry
->socket
== socket
) {
1562 void pcmcia_info(Monitor
*mon
)
1564 struct pcmcia_socket_entry_s
*iter
;
1566 if (!pcmcia_sockets
)
1567 monitor_printf(mon
, "No PCMCIA sockets\n");
1569 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1570 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1571 iter
->socket
->attached
? iter
->socket
->card_string
:
1575 /***********************************************************/
1578 typedef struct IOHandlerRecord
{
1580 IOCanReadHandler
*fd_read_poll
;
1582 IOHandler
*fd_write
;
1585 /* temporary data */
1587 QLIST_ENTRY(IOHandlerRecord
) next
;
1590 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1591 QLIST_HEAD_INITIALIZER(io_handlers
);
1594 /* XXX: fd_read_poll should be suppressed, but an API change is
1595 necessary in the character devices to suppress fd_can_read(). */
1596 int qemu_set_fd_handler2(int fd
,
1597 IOCanReadHandler
*fd_read_poll
,
1599 IOHandler
*fd_write
,
1602 IOHandlerRecord
*ioh
;
1604 if (!fd_read
&& !fd_write
) {
1605 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1606 if (ioh
->fd
== fd
) {
1612 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1616 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1617 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1620 ioh
->fd_read_poll
= fd_read_poll
;
1621 ioh
->fd_read
= fd_read
;
1622 ioh
->fd_write
= fd_write
;
1623 ioh
->opaque
= opaque
;
1629 int qemu_set_fd_handler(int fd
,
1631 IOHandler
*fd_write
,
1634 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1638 /***********************************************************/
1639 /* Polling handling */
1641 typedef struct PollingEntry
{
1644 struct PollingEntry
*next
;
1647 static PollingEntry
*first_polling_entry
;
1649 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1651 PollingEntry
**ppe
, *pe
;
1652 pe
= qemu_mallocz(sizeof(PollingEntry
));
1654 pe
->opaque
= opaque
;
1655 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1660 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1662 PollingEntry
**ppe
, *pe
;
1663 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1665 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1673 /***********************************************************/
1674 /* Wait objects support */
1675 typedef struct WaitObjects
{
1677 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1678 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1679 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1682 static WaitObjects wait_objects
= {0};
1684 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1686 WaitObjects
*w
= &wait_objects
;
1688 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1690 w
->events
[w
->num
] = handle
;
1691 w
->func
[w
->num
] = func
;
1692 w
->opaque
[w
->num
] = opaque
;
1697 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1700 WaitObjects
*w
= &wait_objects
;
1703 for (i
= 0; i
< w
->num
; i
++) {
1704 if (w
->events
[i
] == handle
)
1707 w
->events
[i
] = w
->events
[i
+ 1];
1708 w
->func
[i
] = w
->func
[i
+ 1];
1709 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1717 /***********************************************************/
1718 /* ram save/restore */
1720 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1721 #define RAM_SAVE_FLAG_COMPRESS 0x02
1722 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1723 #define RAM_SAVE_FLAG_PAGE 0x08
1724 #define RAM_SAVE_FLAG_EOS 0x10
1726 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1728 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1729 uint32_t *array
= (uint32_t *)page
;
1732 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1733 if (array
[i
] != val
)
1740 static int ram_save_block(QEMUFile
*f
)
1742 static ram_addr_t current_addr
= 0;
1743 ram_addr_t saved_addr
= current_addr
;
1744 ram_addr_t addr
= 0;
1747 while (addr
< last_ram_offset
) {
1748 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1751 cpu_physical_memory_reset_dirty(current_addr
,
1752 current_addr
+ TARGET_PAGE_SIZE
,
1753 MIGRATION_DIRTY_FLAG
);
1755 p
= qemu_get_ram_ptr(current_addr
);
1757 if (is_dup_page(p
, *p
)) {
1758 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1759 qemu_put_byte(f
, *p
);
1761 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1762 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1768 addr
+= TARGET_PAGE_SIZE
;
1769 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1775 static uint64_t bytes_transferred
;
1777 static ram_addr_t
ram_save_remaining(void)
1780 ram_addr_t count
= 0;
1782 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1783 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1790 uint64_t ram_bytes_remaining(void)
1792 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1795 uint64_t ram_bytes_transferred(void)
1797 return bytes_transferred
;
1800 uint64_t ram_bytes_total(void)
1802 return last_ram_offset
;
1805 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1808 uint64_t bytes_transferred_last
;
1810 uint64_t expected_time
= 0;
1813 cpu_physical_memory_set_dirty_tracking(0);
1817 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1818 qemu_file_set_error(f
);
1823 bytes_transferred
= 0;
1825 /* Make sure all dirty bits are set */
1826 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1827 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1828 cpu_physical_memory_set_dirty(addr
);
1831 /* Enable dirty memory tracking */
1832 cpu_physical_memory_set_dirty_tracking(1);
1834 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1837 bytes_transferred_last
= bytes_transferred
;
1838 bwidth
= qemu_get_clock_ns(rt_clock
);
1840 while (!qemu_file_rate_limit(f
)) {
1843 ret
= ram_save_block(f
);
1844 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1845 if (ret
== 0) /* no more blocks */
1849 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1850 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1852 /* if we haven't transferred anything this round, force expected_time to a
1853 * a very high value, but without crashing */
1857 /* try transferring iterative blocks of memory */
1859 /* flush all remaining blocks regardless of rate limiting */
1860 while (ram_save_block(f
) != 0) {
1861 bytes_transferred
+= TARGET_PAGE_SIZE
;
1863 cpu_physical_memory_set_dirty_tracking(0);
1866 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1868 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1870 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1873 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1878 if (version_id
!= 3)
1882 addr
= qemu_get_be64(f
);
1884 flags
= addr
& ~TARGET_PAGE_MASK
;
1885 addr
&= TARGET_PAGE_MASK
;
1887 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1888 if (addr
!= last_ram_offset
)
1892 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1893 uint8_t ch
= qemu_get_byte(f
);
1894 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1897 (!kvm_enabled() || kvm_has_sync_mmu())) {
1898 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1901 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1902 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1904 if (qemu_file_has_error(f
)) {
1907 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1912 void qemu_service_io(void)
1914 qemu_notify_event();
1917 /***********************************************************/
1918 /* machine registration */
1920 static QEMUMachine
*first_machine
= NULL
;
1921 QEMUMachine
*current_machine
= NULL
;
1923 int qemu_register_machine(QEMUMachine
*m
)
1926 pm
= &first_machine
;
1934 static QEMUMachine
*find_machine(const char *name
)
1938 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1939 if (!strcmp(m
->name
, name
))
1941 if (m
->alias
&& !strcmp(m
->alias
, name
))
1947 static QEMUMachine
*find_default_machine(void)
1951 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1952 if (m
->is_default
) {
1959 /***********************************************************/
1960 /* main execution loop */
1962 static void gui_update(void *opaque
)
1964 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1965 DisplayState
*ds
= opaque
;
1966 DisplayChangeListener
*dcl
= ds
->listeners
;
1968 qemu_flush_coalesced_mmio_buffer();
1971 while (dcl
!= NULL
) {
1972 if (dcl
->gui_timer_interval
&&
1973 dcl
->gui_timer_interval
< interval
)
1974 interval
= dcl
->gui_timer_interval
;
1977 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1980 static void nographic_update(void *opaque
)
1982 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1984 qemu_flush_coalesced_mmio_buffer();
1985 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1988 void cpu_synchronize_all_states(void)
1992 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1993 cpu_synchronize_state(cpu
);
1997 void cpu_synchronize_all_post_reset(void)
2001 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2002 cpu_synchronize_post_reset(cpu
);
2006 void cpu_synchronize_all_post_init(void)
2010 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2011 cpu_synchronize_post_init(cpu
);
2015 struct vm_change_state_entry
{
2016 VMChangeStateHandler
*cb
;
2018 QLIST_ENTRY (vm_change_state_entry
) entries
;
2021 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2023 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2026 VMChangeStateEntry
*e
;
2028 e
= qemu_mallocz(sizeof (*e
));
2032 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2036 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2038 QLIST_REMOVE (e
, entries
);
2042 static void vm_state_notify(int running
, int reason
)
2044 VMChangeStateEntry
*e
;
2046 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2047 e
->cb(e
->opaque
, running
, reason
);
2051 static void resume_all_vcpus(void);
2052 static void pause_all_vcpus(void);
2059 vm_state_notify(1, 0);
2064 /* reset/shutdown handler */
2066 typedef struct QEMUResetEntry
{
2067 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2068 QEMUResetHandler
*func
;
2072 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2073 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2074 static int reset_requested
;
2075 static int shutdown_requested
;
2076 static int powerdown_requested
;
2077 static int debug_requested
;
2078 static int vmstop_requested
;
2080 int qemu_shutdown_requested(void)
2082 int r
= shutdown_requested
;
2083 shutdown_requested
= 0;
2087 int qemu_reset_requested(void)
2089 int r
= reset_requested
;
2090 reset_requested
= 0;
2094 int qemu_powerdown_requested(void)
2096 int r
= powerdown_requested
;
2097 powerdown_requested
= 0;
2101 static int qemu_debug_requested(void)
2103 int r
= debug_requested
;
2104 debug_requested
= 0;
2108 static int qemu_vmstop_requested(void)
2110 int r
= vmstop_requested
;
2111 vmstop_requested
= 0;
2115 static void do_vm_stop(int reason
)
2118 cpu_disable_ticks();
2121 vm_state_notify(0, reason
);
2122 monitor_protocol_event(QEVENT_STOP
, NULL
);
2126 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2128 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2131 re
->opaque
= opaque
;
2132 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2135 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2139 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2140 if (re
->func
== func
&& re
->opaque
== opaque
) {
2141 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2148 void qemu_system_reset(void)
2150 QEMUResetEntry
*re
, *nre
;
2152 /* reset all devices */
2153 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2154 re
->func(re
->opaque
);
2156 monitor_protocol_event(QEVENT_RESET
, NULL
);
2157 cpu_synchronize_all_post_reset();
2160 void qemu_system_reset_request(void)
2163 shutdown_requested
= 1;
2165 reset_requested
= 1;
2167 qemu_notify_event();
2170 void qemu_system_shutdown_request(void)
2172 shutdown_requested
= 1;
2173 qemu_notify_event();
2176 void qemu_system_powerdown_request(void)
2178 powerdown_requested
= 1;
2179 qemu_notify_event();
2182 static int cpu_can_run(CPUState
*env
)
2193 static int cpu_has_work(CPUState
*env
)
2201 if (qemu_cpu_has_work(env
))
2206 static int tcg_has_work(void)
2210 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2211 if (cpu_has_work(env
))
2217 static int io_thread_fd
= -1;
2219 static void qemu_event_increment(void)
2221 /* Write 8 bytes to be compatible with eventfd. */
2222 static uint64_t val
= 1;
2225 if (io_thread_fd
== -1)
2229 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2230 } while (ret
< 0 && errno
== EINTR
);
2232 /* EAGAIN is fine, a read must be pending. */
2233 if (ret
< 0 && errno
!= EAGAIN
) {
2234 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2240 static void qemu_event_read(void *opaque
)
2242 int fd
= (unsigned long)opaque
;
2246 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2248 len
= read(fd
, buffer
, sizeof(buffer
));
2249 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2252 static int qemu_event_init(void)
2257 err
= qemu_eventfd(fds
);
2261 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2265 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2269 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2270 (void *)(unsigned long)fds
[0]);
2272 io_thread_fd
= fds
[1];
2281 HANDLE qemu_event_handle
;
2283 static void dummy_event_handler(void *opaque
)
2287 static int qemu_event_init(void)
2289 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2290 if (!qemu_event_handle
) {
2291 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2294 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2298 static void qemu_event_increment(void)
2300 if (!SetEvent(qemu_event_handle
)) {
2301 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2308 #ifndef CONFIG_IOTHREAD
2309 static int qemu_init_main_loop(void)
2311 return qemu_event_init();
2314 void qemu_init_vcpu(void *_env
)
2316 CPUState
*env
= _env
;
2318 env
->nr_cores
= smp_cores
;
2319 env
->nr_threads
= smp_threads
;
2325 int qemu_cpu_self(void *env
)
2330 static void resume_all_vcpus(void)
2334 static void pause_all_vcpus(void)
2338 void qemu_cpu_kick(void *env
)
2343 void qemu_notify_event(void)
2345 CPUState
*env
= cpu_single_env
;
2347 qemu_event_increment ();
2351 if (next_cpu
&& env
!= next_cpu
) {
2356 void qemu_mutex_lock_iothread(void) {}
2357 void qemu_mutex_unlock_iothread(void) {}
2359 void vm_stop(int reason
)
2364 #else /* CONFIG_IOTHREAD */
2366 #include "qemu-thread.h"
2368 QemuMutex qemu_global_mutex
;
2369 static QemuMutex qemu_fair_mutex
;
2371 static QemuThread io_thread
;
2373 static QemuThread
*tcg_cpu_thread
;
2374 static QemuCond
*tcg_halt_cond
;
2376 static int qemu_system_ready
;
2378 static QemuCond qemu_cpu_cond
;
2380 static QemuCond qemu_system_cond
;
2381 static QemuCond qemu_pause_cond
;
2383 static void tcg_block_io_signals(void);
2384 static void kvm_block_io_signals(CPUState
*env
);
2385 static void unblock_io_signals(void);
2387 static int qemu_init_main_loop(void)
2391 ret
= qemu_event_init();
2395 qemu_cond_init(&qemu_pause_cond
);
2396 qemu_mutex_init(&qemu_fair_mutex
);
2397 qemu_mutex_init(&qemu_global_mutex
);
2398 qemu_mutex_lock(&qemu_global_mutex
);
2400 unblock_io_signals();
2401 qemu_thread_self(&io_thread
);
2406 static void qemu_wait_io_event_common(CPUState
*env
)
2411 qemu_cond_signal(&qemu_pause_cond
);
2415 static void qemu_wait_io_event(CPUState
*env
)
2417 while (!tcg_has_work())
2418 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2420 qemu_mutex_unlock(&qemu_global_mutex
);
2423 * Users of qemu_global_mutex can be starved, having no chance
2424 * to acquire it since this path will get to it first.
2425 * So use another lock to provide fairness.
2427 qemu_mutex_lock(&qemu_fair_mutex
);
2428 qemu_mutex_unlock(&qemu_fair_mutex
);
2430 qemu_mutex_lock(&qemu_global_mutex
);
2431 qemu_wait_io_event_common(env
);
2434 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2441 ts
.tv_sec
= timeout
/ 1000;
2442 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2444 sigemptyset(&waitset
);
2445 sigaddset(&waitset
, SIG_IPI
);
2447 qemu_mutex_unlock(&qemu_global_mutex
);
2448 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2450 qemu_mutex_lock(&qemu_global_mutex
);
2452 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2453 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2458 static void qemu_kvm_wait_io_event(CPUState
*env
)
2460 while (!cpu_has_work(env
))
2461 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2463 qemu_kvm_eat_signal(env
, 0);
2464 qemu_wait_io_event_common(env
);
2467 static int qemu_cpu_exec(CPUState
*env
);
2469 static void *kvm_cpu_thread_fn(void *arg
)
2471 CPUState
*env
= arg
;
2473 qemu_thread_self(env
->thread
);
2477 kvm_block_io_signals(env
);
2479 /* signal CPU creation */
2480 qemu_mutex_lock(&qemu_global_mutex
);
2482 qemu_cond_signal(&qemu_cpu_cond
);
2484 /* and wait for machine initialization */
2485 while (!qemu_system_ready
)
2486 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2489 if (cpu_can_run(env
))
2491 qemu_kvm_wait_io_event(env
);
2497 static bool tcg_cpu_exec(void);
2499 static void *tcg_cpu_thread_fn(void *arg
)
2501 CPUState
*env
= arg
;
2503 tcg_block_io_signals();
2504 qemu_thread_self(env
->thread
);
2506 /* signal CPU creation */
2507 qemu_mutex_lock(&qemu_global_mutex
);
2508 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2510 qemu_cond_signal(&qemu_cpu_cond
);
2512 /* and wait for machine initialization */
2513 while (!qemu_system_ready
)
2514 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2518 qemu_wait_io_event(cur_cpu
);
2524 void qemu_cpu_kick(void *_env
)
2526 CPUState
*env
= _env
;
2527 qemu_cond_broadcast(env
->halt_cond
);
2529 qemu_thread_signal(env
->thread
, SIG_IPI
);
2532 int qemu_cpu_self(void *_env
)
2534 CPUState
*env
= _env
;
2537 qemu_thread_self(&this);
2539 return qemu_thread_equal(&this, env
->thread
);
2542 static void cpu_signal(int sig
)
2545 cpu_exit(cpu_single_env
);
2548 static void tcg_block_io_signals(void)
2551 struct sigaction sigact
;
2554 sigaddset(&set
, SIGUSR2
);
2555 sigaddset(&set
, SIGIO
);
2556 sigaddset(&set
, SIGALRM
);
2557 sigaddset(&set
, SIGCHLD
);
2558 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2561 sigaddset(&set
, SIG_IPI
);
2562 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2564 memset(&sigact
, 0, sizeof(sigact
));
2565 sigact
.sa_handler
= cpu_signal
;
2566 sigaction(SIG_IPI
, &sigact
, NULL
);
2569 static void dummy_signal(int sig
)
2573 static void kvm_block_io_signals(CPUState
*env
)
2577 struct sigaction sigact
;
2580 sigaddset(&set
, SIGUSR2
);
2581 sigaddset(&set
, SIGIO
);
2582 sigaddset(&set
, SIGALRM
);
2583 sigaddset(&set
, SIGCHLD
);
2584 sigaddset(&set
, SIG_IPI
);
2585 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2587 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2588 sigdelset(&set
, SIG_IPI
);
2590 memset(&sigact
, 0, sizeof(sigact
));
2591 sigact
.sa_handler
= dummy_signal
;
2592 sigaction(SIG_IPI
, &sigact
, NULL
);
2594 r
= kvm_set_signal_mask(env
, &set
);
2596 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2601 static void unblock_io_signals(void)
2606 sigaddset(&set
, SIGUSR2
);
2607 sigaddset(&set
, SIGIO
);
2608 sigaddset(&set
, SIGALRM
);
2609 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2612 sigaddset(&set
, SIG_IPI
);
2613 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2616 static void qemu_signal_lock(unsigned int msecs
)
2618 qemu_mutex_lock(&qemu_fair_mutex
);
2620 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2621 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2622 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2625 qemu_mutex_unlock(&qemu_fair_mutex
);
2628 void qemu_mutex_lock_iothread(void)
2630 if (kvm_enabled()) {
2631 qemu_mutex_lock(&qemu_fair_mutex
);
2632 qemu_mutex_lock(&qemu_global_mutex
);
2633 qemu_mutex_unlock(&qemu_fair_mutex
);
2635 qemu_signal_lock(100);
2638 void qemu_mutex_unlock_iothread(void)
2640 qemu_mutex_unlock(&qemu_global_mutex
);
2643 static int all_vcpus_paused(void)
2645 CPUState
*penv
= first_cpu
;
2650 penv
= (CPUState
*)penv
->next_cpu
;
2656 static void pause_all_vcpus(void)
2658 CPUState
*penv
= first_cpu
;
2662 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2663 qemu_cpu_kick(penv
);
2664 penv
= (CPUState
*)penv
->next_cpu
;
2667 while (!all_vcpus_paused()) {
2668 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2671 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2672 penv
= (CPUState
*)penv
->next_cpu
;
2677 static void resume_all_vcpus(void)
2679 CPUState
*penv
= first_cpu
;
2684 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2685 qemu_cpu_kick(penv
);
2686 penv
= (CPUState
*)penv
->next_cpu
;
2690 static void tcg_init_vcpu(void *_env
)
2692 CPUState
*env
= _env
;
2693 /* share a single thread for all cpus with TCG */
2694 if (!tcg_cpu_thread
) {
2695 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2696 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2697 qemu_cond_init(env
->halt_cond
);
2698 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2699 while (env
->created
== 0)
2700 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2701 tcg_cpu_thread
= env
->thread
;
2702 tcg_halt_cond
= env
->halt_cond
;
2704 env
->thread
= tcg_cpu_thread
;
2705 env
->halt_cond
= tcg_halt_cond
;
2709 static void kvm_start_vcpu(CPUState
*env
)
2711 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2712 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2713 qemu_cond_init(env
->halt_cond
);
2714 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2715 while (env
->created
== 0)
2716 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2719 void qemu_init_vcpu(void *_env
)
2721 CPUState
*env
= _env
;
2723 env
->nr_cores
= smp_cores
;
2724 env
->nr_threads
= smp_threads
;
2726 kvm_start_vcpu(env
);
2731 void qemu_notify_event(void)
2733 qemu_event_increment();
2736 static void qemu_system_vmstop_request(int reason
)
2738 vmstop_requested
= reason
;
2739 qemu_notify_event();
2742 void vm_stop(int reason
)
2745 qemu_thread_self(&me
);
2747 if (!qemu_thread_equal(&me
, &io_thread
)) {
2748 qemu_system_vmstop_request(reason
);
2750 * FIXME: should not return to device code in case
2751 * vm_stop() has been requested.
2753 if (cpu_single_env
) {
2754 cpu_exit(cpu_single_env
);
2755 cpu_single_env
->stop
= 1;
2766 static void host_main_loop_wait(int *timeout
)
2772 /* XXX: need to suppress polling by better using win32 events */
2774 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2775 ret
|= pe
->func(pe
->opaque
);
2779 WaitObjects
*w
= &wait_objects
;
2781 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2782 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2783 if (w
->func
[ret
- WAIT_OBJECT_0
])
2784 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2786 /* Check for additional signaled events */
2787 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2789 /* Check if event is signaled */
2790 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2791 if(ret2
== WAIT_OBJECT_0
) {
2793 w
->func
[i
](w
->opaque
[i
]);
2794 } else if (ret2
== WAIT_TIMEOUT
) {
2796 err
= GetLastError();
2797 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2800 } else if (ret
== WAIT_TIMEOUT
) {
2802 err
= GetLastError();
2803 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2810 static void host_main_loop_wait(int *timeout
)
2815 void main_loop_wait(int nonblocking
)
2817 IOHandlerRecord
*ioh
;
2818 fd_set rfds
, wfds
, xfds
;
2826 timeout
= qemu_calculate_timeout();
2827 qemu_bh_update_timeout(&timeout
);
2830 host_main_loop_wait(&timeout
);
2832 /* poll any events */
2833 /* XXX: separate device handlers from system ones */
2838 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2842 (!ioh
->fd_read_poll
||
2843 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2844 FD_SET(ioh
->fd
, &rfds
);
2848 if (ioh
->fd_write
) {
2849 FD_SET(ioh
->fd
, &wfds
);
2855 tv
.tv_sec
= timeout
/ 1000;
2856 tv
.tv_usec
= (timeout
% 1000) * 1000;
2858 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2860 qemu_mutex_unlock_iothread();
2861 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2862 qemu_mutex_lock_iothread();
2864 IOHandlerRecord
*pioh
;
2866 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2868 QLIST_REMOVE(ioh
, next
);
2872 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2873 ioh
->fd_read(ioh
->opaque
);
2875 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2876 ioh
->fd_write(ioh
->opaque
);
2881 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2883 qemu_run_all_timers();
2885 /* Check bottom-halves last in case any of the earlier events triggered
2891 static int qemu_cpu_exec(CPUState
*env
)
2894 #ifdef CONFIG_PROFILER
2898 #ifdef CONFIG_PROFILER
2899 ti
= profile_getclock();
2904 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2905 env
->icount_decr
.u16
.low
= 0;
2906 env
->icount_extra
= 0;
2907 count
= qemu_icount_round (qemu_next_deadline());
2908 qemu_icount
+= count
;
2909 decr
= (count
> 0xffff) ? 0xffff : count
;
2911 env
->icount_decr
.u16
.low
= decr
;
2912 env
->icount_extra
= count
;
2914 ret
= cpu_exec(env
);
2915 #ifdef CONFIG_PROFILER
2916 qemu_time
+= profile_getclock() - ti
;
2919 /* Fold pending instructions back into the
2920 instruction counter, and clear the interrupt flag. */
2921 qemu_icount
-= (env
->icount_decr
.u16
.low
2922 + env
->icount_extra
);
2923 env
->icount_decr
.u32
= 0;
2924 env
->icount_extra
= 0;
2929 static bool tcg_cpu_exec(void)
2933 if (next_cpu
== NULL
)
2934 next_cpu
= first_cpu
;
2935 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2936 CPUState
*env
= cur_cpu
= next_cpu
;
2938 qemu_clock_enable(vm_clock
,
2939 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2941 if (qemu_alarm_pending())
2943 if (cpu_can_run(env
))
2944 ret
= qemu_cpu_exec(env
);
2948 if (ret
== EXCP_DEBUG
) {
2949 gdb_set_stop_cpu(env
);
2950 debug_requested
= 1;
2954 return tcg_has_work();
2957 static int vm_can_run(void)
2959 if (powerdown_requested
)
2961 if (reset_requested
)
2963 if (shutdown_requested
)
2965 if (debug_requested
)
2970 qemu_irq qemu_system_powerdown
;
2972 static void main_loop(void)
2976 #ifdef CONFIG_IOTHREAD
2977 qemu_system_ready
= 1;
2978 qemu_cond_broadcast(&qemu_system_cond
);
2983 bool nonblocking
= false;
2984 #ifdef CONFIG_PROFILER
2987 #ifndef CONFIG_IOTHREAD
2988 nonblocking
= tcg_cpu_exec();
2990 #ifdef CONFIG_PROFILER
2991 ti
= profile_getclock();
2993 main_loop_wait(nonblocking
);
2994 #ifdef CONFIG_PROFILER
2995 dev_time
+= profile_getclock() - ti
;
2997 } while (vm_can_run());
2999 if (qemu_debug_requested()) {
3000 vm_stop(EXCP_DEBUG
);
3002 if (qemu_shutdown_requested()) {
3003 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3010 if (qemu_reset_requested()) {
3012 qemu_system_reset();
3015 if (qemu_powerdown_requested()) {
3016 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3017 qemu_irq_raise(qemu_system_powerdown
);
3019 if ((r
= qemu_vmstop_requested())) {
3026 static void version(void)
3028 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3031 static void help(int exitcode
)
3033 const char *options_help
=
3034 #define DEF(option, opt_arg, opt_enum, opt_help) \
3036 #define DEFHEADING(text) stringify(text) "\n"
3037 #include "qemu-options.h"
3043 printf("usage: %s [options] [disk_image]\n"
3045 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3048 "During emulation, the following keys are useful:\n"
3049 "ctrl-alt-f toggle full screen\n"
3050 "ctrl-alt-n switch to virtual console 'n'\n"
3051 "ctrl-alt toggle mouse and keyboard grab\n"
3053 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3059 #define HAS_ARG 0x0001
3062 #define DEF(option, opt_arg, opt_enum, opt_help) \
3064 #define DEFHEADING(text)
3065 #include "qemu-options.h"
3071 typedef struct QEMUOption
{
3077 static const QEMUOption qemu_options
[] = {
3078 { "h", 0, QEMU_OPTION_h
},
3079 #define DEF(option, opt_arg, opt_enum, opt_help) \
3080 { option, opt_arg, opt_enum },
3081 #define DEFHEADING(text)
3082 #include "qemu-options.h"
3090 struct soundhw soundhw
[] = {
3091 #ifdef HAS_AUDIO_CHOICE
3092 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3098 { .init_isa
= pcspk_audio_init
}
3105 "Creative Sound Blaster 16",
3108 { .init_isa
= SB16_init
}
3112 #ifdef CONFIG_CS4231A
3118 { .init_isa
= cs4231a_init
}
3126 "Yamaha YMF262 (OPL3)",
3128 "Yamaha YM3812 (OPL2)",
3132 { .init_isa
= Adlib_init
}
3139 "Gravis Ultrasound GF1",
3142 { .init_isa
= GUS_init
}
3149 "Intel 82801AA AC97 Audio",
3152 { .init_pci
= ac97_init
}
3156 #ifdef CONFIG_ES1370
3159 "ENSONIQ AudioPCI ES1370",
3162 { .init_pci
= es1370_init
}
3166 #endif /* HAS_AUDIO_CHOICE */
3168 { NULL
, NULL
, 0, 0, { NULL
} }
3171 static void select_soundhw (const char *optarg
)
3175 if (*optarg
== '?') {
3178 printf ("Valid sound card names (comma separated):\n");
3179 for (c
= soundhw
; c
->name
; ++c
) {
3180 printf ("%-11s %s\n", c
->name
, c
->descr
);
3182 printf ("\n-soundhw all will enable all of the above\n");
3183 exit (*optarg
!= '?');
3191 if (!strcmp (optarg
, "all")) {
3192 for (c
= soundhw
; c
->name
; ++c
) {
3200 e
= strchr (p
, ',');
3201 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3203 for (c
= soundhw
; c
->name
; ++c
) {
3204 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3213 "Unknown sound card name (too big to show)\n");
3216 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3221 p
+= l
+ (e
!= NULL
);
3225 goto show_valid_cards
;
3230 static void select_vgahw (const char *p
)
3235 vga_interface_type
= VGA_NONE
;
3236 if (strstart(p
, "std", &opts
)) {
3237 vga_interface_type
= VGA_STD
;
3238 } else if (strstart(p
, "cirrus", &opts
)) {
3239 vga_interface_type
= VGA_CIRRUS
;
3240 } else if (strstart(p
, "vmware", &opts
)) {
3241 vga_interface_type
= VGA_VMWARE
;
3242 } else if (strstart(p
, "xenfb", &opts
)) {
3243 vga_interface_type
= VGA_XENFB
;
3244 } else if (!strstart(p
, "none", &opts
)) {
3246 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3250 const char *nextopt
;
3252 if (strstart(opts
, ",retrace=", &nextopt
)) {
3254 if (strstart(opts
, "dumb", &nextopt
))
3255 vga_retrace_method
= VGA_RETRACE_DUMB
;
3256 else if (strstart(opts
, "precise", &nextopt
))
3257 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3258 else goto invalid_vga
;
3259 } else goto invalid_vga
;
3265 static int balloon_parse(const char *arg
)
3269 if (strcmp(arg
, "none") == 0) {
3273 if (!strncmp(arg
, "virtio", 6)) {
3274 if (arg
[6] == ',') {
3275 /* have params -> parse them */
3276 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3280 /* create empty opts */
3281 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3283 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3292 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3294 exit(STATUS_CONTROL_C_EXIT
);
3299 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3303 if(strlen(str
) != 36)
3306 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3307 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3308 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3314 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3322 static void termsig_handler(int signal
)
3324 qemu_system_shutdown_request();
3327 static void sigchld_handler(int signal
)
3329 waitpid(-1, NULL
, WNOHANG
);
3332 static void sighandler_setup(void)
3334 struct sigaction act
;
3336 memset(&act
, 0, sizeof(act
));
3337 act
.sa_handler
= termsig_handler
;
3338 sigaction(SIGINT
, &act
, NULL
);
3339 sigaction(SIGHUP
, &act
, NULL
);
3340 sigaction(SIGTERM
, &act
, NULL
);
3342 act
.sa_handler
= sigchld_handler
;
3343 act
.sa_flags
= SA_NOCLDSTOP
;
3344 sigaction(SIGCHLD
, &act
, NULL
);
3350 /* Look for support files in the same directory as the executable. */
3351 static char *find_datadir(const char *argv0
)
3357 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3364 while (p
!= buf
&& *p
!= '\\')
3367 if (access(buf
, R_OK
) == 0) {
3368 return qemu_strdup(buf
);
3374 /* Find a likely location for support files using the location of the binary.
3375 For installed binaries this will be "$bindir/../share/qemu". When
3376 running from the build tree this will be "$bindir/../pc-bios". */
3377 #define SHARE_SUFFIX "/share/qemu"
3378 #define BUILD_SUFFIX "/pc-bios"
3379 static char *find_datadir(const char *argv0
)
3387 #if defined(__linux__)
3390 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3396 #elif defined(__FreeBSD__)
3399 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3406 /* If we don't have any way of figuring out the actual executable
3407 location then try argv[0]. */
3409 p
= realpath(argv0
, buf
);
3417 max_len
= strlen(dir
) +
3418 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3419 res
= qemu_mallocz(max_len
);
3420 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3421 if (access(res
, R_OK
)) {
3422 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3423 if (access(res
, R_OK
)) {
3435 char *qemu_find_file(int type
, const char *name
)
3441 /* If name contains path separators then try it as a straight path. */
3442 if ((strchr(name
, '/') || strchr(name
, '\\'))
3443 && access(name
, R_OK
) == 0) {
3444 return qemu_strdup(name
);
3447 case QEMU_FILE_TYPE_BIOS
:
3450 case QEMU_FILE_TYPE_KEYMAP
:
3451 subdir
= "keymaps/";
3456 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3457 buf
= qemu_mallocz(len
);
3458 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3459 if (access(buf
, R_OK
)) {
3466 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3468 return qdev_device_help(opts
);
3471 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3475 dev
= qdev_device_add(opts
);
3481 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3483 CharDriverState
*chr
;
3485 chr
= qemu_chr_open_opts(opts
, NULL
);
3491 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3493 CharDriverState
*chr
;
3494 const char *chardev
;
3498 mode
= qemu_opt_get(opts
, "mode");
3502 if (strcmp(mode
, "readline") == 0) {
3503 flags
= MONITOR_USE_READLINE
;
3504 } else if (strcmp(mode
, "control") == 0) {
3505 flags
= MONITOR_USE_CONTROL
;
3507 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3511 if (qemu_opt_get_bool(opts
, "default", 0))
3512 flags
|= MONITOR_IS_DEFAULT
;
3514 chardev
= qemu_opt_get(opts
, "chardev");
3515 chr
= qemu_chr_find(chardev
);
3517 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3521 monitor_init(chr
, flags
);
3525 static void monitor_parse(const char *optarg
, const char *mode
)
3527 static int monitor_device_index
= 0;
3533 if (strstart(optarg
, "chardev:", &p
)) {
3534 snprintf(label
, sizeof(label
), "%s", p
);
3536 if (monitor_device_index
) {
3537 snprintf(label
, sizeof(label
), "monitor%d",
3538 monitor_device_index
);
3540 snprintf(label
, sizeof(label
), "monitor");
3543 opts
= qemu_chr_parse_compat(label
, optarg
);
3545 fprintf(stderr
, "parse error: %s\n", optarg
);
3550 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3552 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3555 qemu_opt_set(opts
, "mode", mode
);
3556 qemu_opt_set(opts
, "chardev", label
);
3558 qemu_opt_set(opts
, "default", "on");
3559 monitor_device_index
++;
3562 struct device_config
{
3564 DEV_USB
, /* -usbdevice */
3566 DEV_SERIAL
, /* -serial */
3567 DEV_PARALLEL
, /* -parallel */
3568 DEV_VIRTCON
, /* -virtioconsole */
3569 DEV_DEBUGCON
, /* -debugcon */
3571 const char *cmdline
;
3572 QTAILQ_ENTRY(device_config
) next
;
3574 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3576 static void add_device_config(int type
, const char *cmdline
)
3578 struct device_config
*conf
;
3580 conf
= qemu_mallocz(sizeof(*conf
));
3582 conf
->cmdline
= cmdline
;
3583 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3586 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3588 struct device_config
*conf
;
3591 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3592 if (conf
->type
!= type
)
3594 rc
= func(conf
->cmdline
);
3601 static int serial_parse(const char *devname
)
3603 static int index
= 0;
3606 if (strcmp(devname
, "none") == 0)
3608 if (index
== MAX_SERIAL_PORTS
) {
3609 fprintf(stderr
, "qemu: too many serial ports\n");
3612 snprintf(label
, sizeof(label
), "serial%d", index
);
3613 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3614 if (!serial_hds
[index
]) {
3615 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3616 devname
, strerror(errno
));
3623 static int parallel_parse(const char *devname
)
3625 static int index
= 0;
3628 if (strcmp(devname
, "none") == 0)
3630 if (index
== MAX_PARALLEL_PORTS
) {
3631 fprintf(stderr
, "qemu: too many parallel ports\n");
3634 snprintf(label
, sizeof(label
), "parallel%d", index
);
3635 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3636 if (!parallel_hds
[index
]) {
3637 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3638 devname
, strerror(errno
));
3645 static int virtcon_parse(const char *devname
)
3647 static int index
= 0;
3649 QemuOpts
*bus_opts
, *dev_opts
;
3651 if (strcmp(devname
, "none") == 0)
3653 if (index
== MAX_VIRTIO_CONSOLES
) {
3654 fprintf(stderr
, "qemu: too many virtio consoles\n");
3658 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3659 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3661 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3662 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3664 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3665 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3666 if (!virtcon_hds
[index
]) {
3667 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3668 devname
, strerror(errno
));
3671 qemu_opt_set(dev_opts
, "chardev", label
);
3677 static int debugcon_parse(const char *devname
)
3681 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3684 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3686 fprintf(stderr
, "qemu: already have a debugcon device\n");
3689 qemu_opt_set(opts
, "driver", "isa-debugcon");
3690 qemu_opt_set(opts
, "chardev", "debugcon");
3694 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3695 const char **poptarg
, int *poptind
)
3697 const QEMUOption
*popt
;
3698 int optind
= *poptind
;
3699 char *r
= argv
[optind
];
3702 loc_set_cmdline(argv
, optind
, 1);
3704 /* Treat --foo the same as -foo. */
3707 popt
= qemu_options
;
3710 error_report("invalid option");
3713 if (!strcmp(popt
->name
, r
+ 1))
3717 if (popt
->flags
& HAS_ARG
) {
3718 if (optind
>= argc
) {
3719 error_report("requires an argument");
3722 optarg
= argv
[optind
++];
3723 loc_set_cmdline(argv
, optind
- 2, 2);
3734 int main(int argc
, char **argv
, char **envp
)
3736 const char *gdbstub_dev
= NULL
;
3737 uint32_t boot_devices_bitmap
= 0;
3739 int snapshot
, linux_boot
, net_boot
;
3740 const char *icount_option
= NULL
;
3741 const char *initrd_filename
;
3742 const char *kernel_filename
, *kernel_cmdline
;
3743 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3745 DisplayChangeListener
*dcl
;
3746 int cyls
, heads
, secs
, translation
;
3747 QemuOpts
*hda_opts
= NULL
, *opts
;
3750 const char *loadvm
= NULL
;
3751 QEMUMachine
*machine
;
3752 const char *cpu_model
;
3757 const char *pid_file
= NULL
;
3758 const char *incoming
= NULL
;
3761 struct passwd
*pwd
= NULL
;
3762 const char *chroot_dir
= NULL
;
3763 const char *run_as
= NULL
;
3766 int show_vnc_port
= 0;
3769 error_set_progname(argv
[0]);
3773 qemu_cache_utils_init(envp
);
3775 QLIST_INIT (&vm_change_state_head
);
3778 struct sigaction act
;
3779 sigfillset(&act
.sa_mask
);
3781 act
.sa_handler
= SIG_IGN
;
3782 sigaction(SIGPIPE
, &act
, NULL
);
3785 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3786 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3787 QEMU to run on a single CPU */
3792 h
= GetCurrentProcess();
3793 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3794 for(i
= 0; i
< 32; i
++) {
3795 if (mask
& (1 << i
))
3800 SetProcessAffinityMask(h
, mask
);
3806 module_call_init(MODULE_INIT_MACHINE
);
3807 machine
= find_default_machine();
3809 initrd_filename
= NULL
;
3812 kernel_filename
= NULL
;
3813 kernel_cmdline
= "";
3814 cyls
= heads
= secs
= 0;
3815 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3817 for (i
= 0; i
< MAX_NODES
; i
++) {
3819 node_cpumask
[i
] = 0;
3828 /* first pass of option parsing */
3830 while (optind
< argc
) {
3831 if (argv
[optind
][0] != '-') {
3836 const QEMUOption
*popt
;
3838 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3839 switch (popt
->index
) {
3840 case QEMU_OPTION_nodefconfig
:
3851 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3852 fp
= fopen(fname
, "r");
3854 if (qemu_config_parse(fp
, fname
) != 0) {
3860 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3861 fp
= fopen(fname
, "r");
3863 if (qemu_config_parse(fp
, fname
) != 0) {
3869 #if defined(cpudef_setup)
3870 cpudef_setup(); /* parse cpu definitions in target config file */
3873 /* second pass of option parsing */
3878 if (argv
[optind
][0] != '-') {
3879 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3881 const QEMUOption
*popt
;
3883 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3884 switch(popt
->index
) {
3886 machine
= find_machine(optarg
);
3889 printf("Supported machines are:\n");
3890 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3892 printf("%-10s %s (alias of %s)\n",
3893 m
->alias
, m
->desc
, m
->name
);
3894 printf("%-10s %s%s\n",
3896 m
->is_default
? " (default)" : "");
3898 exit(*optarg
!= '?');
3901 case QEMU_OPTION_cpu
:
3902 /* hw initialization will check this */
3903 if (*optarg
== '?') {
3904 /* XXX: implement xxx_cpu_list for targets that still miss it */
3905 #if defined(cpu_list_id)
3906 cpu_list_id(stdout
, &fprintf
, optarg
);
3907 #elif defined(cpu_list)
3908 cpu_list(stdout
, &fprintf
); /* deprecated */
3915 case QEMU_OPTION_initrd
:
3916 initrd_filename
= optarg
;
3918 case QEMU_OPTION_hda
:
3920 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3922 hda_opts
= drive_add(optarg
, HD_ALIAS
3923 ",cyls=%d,heads=%d,secs=%d%s",
3924 0, cyls
, heads
, secs
,
3925 translation
== BIOS_ATA_TRANSLATION_LBA
?
3927 translation
== BIOS_ATA_TRANSLATION_NONE
?
3928 ",trans=none" : "");
3930 case QEMU_OPTION_hdb
:
3931 case QEMU_OPTION_hdc
:
3932 case QEMU_OPTION_hdd
:
3933 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3935 case QEMU_OPTION_drive
:
3936 drive_add(NULL
, "%s", optarg
);
3938 case QEMU_OPTION_set
:
3939 if (qemu_set_option(optarg
) != 0)
3942 case QEMU_OPTION_global
:
3943 if (qemu_global_option(optarg
) != 0)
3946 case QEMU_OPTION_mtdblock
:
3947 drive_add(optarg
, MTD_ALIAS
);
3949 case QEMU_OPTION_sd
:
3950 drive_add(optarg
, SD_ALIAS
);
3952 case QEMU_OPTION_pflash
:
3953 drive_add(optarg
, PFLASH_ALIAS
);
3955 case QEMU_OPTION_snapshot
:
3958 case QEMU_OPTION_hdachs
:
3962 cyls
= strtol(p
, (char **)&p
, 0);
3963 if (cyls
< 1 || cyls
> 16383)
3968 heads
= strtol(p
, (char **)&p
, 0);
3969 if (heads
< 1 || heads
> 16)
3974 secs
= strtol(p
, (char **)&p
, 0);
3975 if (secs
< 1 || secs
> 63)
3979 if (!strcmp(p
, "none"))
3980 translation
= BIOS_ATA_TRANSLATION_NONE
;
3981 else if (!strcmp(p
, "lba"))
3982 translation
= BIOS_ATA_TRANSLATION_LBA
;
3983 else if (!strcmp(p
, "auto"))
3984 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3987 } else if (*p
!= '\0') {
3989 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3992 if (hda_opts
!= NULL
) {
3994 snprintf(num
, sizeof(num
), "%d", cyls
);
3995 qemu_opt_set(hda_opts
, "cyls", num
);
3996 snprintf(num
, sizeof(num
), "%d", heads
);
3997 qemu_opt_set(hda_opts
, "heads", num
);
3998 snprintf(num
, sizeof(num
), "%d", secs
);
3999 qemu_opt_set(hda_opts
, "secs", num
);
4000 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4001 qemu_opt_set(hda_opts
, "trans", "lba");
4002 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4003 qemu_opt_set(hda_opts
, "trans", "none");
4007 case QEMU_OPTION_numa
:
4008 if (nb_numa_nodes
>= MAX_NODES
) {
4009 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4014 case QEMU_OPTION_nographic
:
4015 display_type
= DT_NOGRAPHIC
;
4017 #ifdef CONFIG_CURSES
4018 case QEMU_OPTION_curses
:
4019 display_type
= DT_CURSES
;
4022 case QEMU_OPTION_portrait
:
4025 case QEMU_OPTION_kernel
:
4026 kernel_filename
= optarg
;
4028 case QEMU_OPTION_append
:
4029 kernel_cmdline
= optarg
;
4031 case QEMU_OPTION_cdrom
:
4032 drive_add(optarg
, CDROM_ALIAS
);
4034 case QEMU_OPTION_boot
:
4036 static const char * const params
[] = {
4037 "order", "once", "menu", NULL
4039 char buf
[sizeof(boot_devices
)];
4040 char *standard_boot_devices
;
4043 if (!strchr(optarg
, '=')) {
4045 pstrcpy(buf
, sizeof(buf
), optarg
);
4046 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4048 "qemu: unknown boot parameter '%s' in '%s'\n",
4054 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4055 boot_devices_bitmap
= parse_bootdevices(buf
);
4056 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4059 if (get_param_value(buf
, sizeof(buf
),
4061 boot_devices_bitmap
|= parse_bootdevices(buf
);
4062 standard_boot_devices
= qemu_strdup(boot_devices
);
4063 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4064 qemu_register_reset(restore_boot_devices
,
4065 standard_boot_devices
);
4067 if (get_param_value(buf
, sizeof(buf
),
4069 if (!strcmp(buf
, "on")) {
4071 } else if (!strcmp(buf
, "off")) {
4075 "qemu: invalid option value '%s'\n",
4083 case QEMU_OPTION_fda
:
4084 case QEMU_OPTION_fdb
:
4085 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4088 case QEMU_OPTION_no_fd_bootchk
:
4092 case QEMU_OPTION_netdev
:
4093 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4097 case QEMU_OPTION_net
:
4098 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4103 case QEMU_OPTION_tftp
:
4104 legacy_tftp_prefix
= optarg
;
4106 case QEMU_OPTION_bootp
:
4107 legacy_bootp_filename
= optarg
;
4110 case QEMU_OPTION_smb
:
4111 if (net_slirp_smb(optarg
) < 0)
4115 case QEMU_OPTION_redir
:
4116 if (net_slirp_redir(optarg
) < 0)
4120 case QEMU_OPTION_bt
:
4121 add_device_config(DEV_BT
, optarg
);
4124 case QEMU_OPTION_audio_help
:
4128 case QEMU_OPTION_soundhw
:
4129 select_soundhw (optarg
);
4135 case QEMU_OPTION_version
:
4139 case QEMU_OPTION_m
: {
4143 value
= strtoul(optarg
, &ptr
, 10);
4145 case 0: case 'M': case 'm':
4152 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4156 /* On 32-bit hosts, QEMU is limited by virtual address space */
4157 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4158 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4161 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4162 fprintf(stderr
, "qemu: ram size too large\n");
4168 case QEMU_OPTION_mempath
:
4172 case QEMU_OPTION_mem_prealloc
:
4179 const CPULogItem
*item
;
4181 mask
= cpu_str_to_log_mask(optarg
);
4183 printf("Log items (comma separated):\n");
4184 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4185 printf("%-10s %s\n", item
->name
, item
->help
);
4193 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4195 case QEMU_OPTION_gdb
:
4196 gdbstub_dev
= optarg
;
4201 case QEMU_OPTION_bios
:
4204 case QEMU_OPTION_singlestep
:
4211 keyboard_layout
= optarg
;
4213 case QEMU_OPTION_localtime
:
4216 case QEMU_OPTION_vga
:
4217 select_vgahw (optarg
);
4219 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4225 w
= strtol(p
, (char **)&p
, 10);
4228 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4234 h
= strtol(p
, (char **)&p
, 10);
4239 depth
= strtol(p
, (char **)&p
, 10);
4240 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4241 depth
!= 24 && depth
!= 32)
4243 } else if (*p
== '\0') {
4244 depth
= graphic_depth
;
4251 graphic_depth
= depth
;
4255 case QEMU_OPTION_echr
:
4258 term_escape_char
= strtol(optarg
, &r
, 0);
4260 printf("Bad argument to echr\n");
4263 case QEMU_OPTION_monitor
:
4264 monitor_parse(optarg
, "readline");
4265 default_monitor
= 0;
4267 case QEMU_OPTION_qmp
:
4268 monitor_parse(optarg
, "control");
4269 default_monitor
= 0;
4271 case QEMU_OPTION_mon
:
4272 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4274 fprintf(stderr
, "parse error: %s\n", optarg
);
4277 default_monitor
= 0;
4279 case QEMU_OPTION_chardev
:
4280 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4282 fprintf(stderr
, "parse error: %s\n", optarg
);
4286 case QEMU_OPTION_serial
:
4287 add_device_config(DEV_SERIAL
, optarg
);
4289 if (strncmp(optarg
, "mon:", 4) == 0) {
4290 default_monitor
= 0;
4293 case QEMU_OPTION_watchdog
:
4296 "qemu: only one watchdog option may be given\n");
4301 case QEMU_OPTION_watchdog_action
:
4302 if (select_watchdog_action(optarg
) == -1) {
4303 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4307 case QEMU_OPTION_virtiocon
:
4308 add_device_config(DEV_VIRTCON
, optarg
);
4309 default_virtcon
= 0;
4310 if (strncmp(optarg
, "mon:", 4) == 0) {
4311 default_monitor
= 0;
4314 case QEMU_OPTION_parallel
:
4315 add_device_config(DEV_PARALLEL
, optarg
);
4316 default_parallel
= 0;
4317 if (strncmp(optarg
, "mon:", 4) == 0) {
4318 default_monitor
= 0;
4321 case QEMU_OPTION_debugcon
:
4322 add_device_config(DEV_DEBUGCON
, optarg
);
4324 case QEMU_OPTION_loadvm
:
4327 case QEMU_OPTION_full_screen
:
4331 case QEMU_OPTION_no_frame
:
4334 case QEMU_OPTION_alt_grab
:
4337 case QEMU_OPTION_ctrl_grab
:
4340 case QEMU_OPTION_no_quit
:
4343 case QEMU_OPTION_sdl
:
4344 display_type
= DT_SDL
;
4347 case QEMU_OPTION_pidfile
:
4351 case QEMU_OPTION_win2k_hack
:
4352 win2k_install_hack
= 1;
4354 case QEMU_OPTION_rtc_td_hack
:
4357 case QEMU_OPTION_acpitable
:
4358 if(acpi_table_add(optarg
) < 0) {
4359 fprintf(stderr
, "Wrong acpi table provided\n");
4363 case QEMU_OPTION_smbios
:
4364 if(smbios_entry_add(optarg
) < 0) {
4365 fprintf(stderr
, "Wrong smbios provided\n");
4371 case QEMU_OPTION_enable_kvm
:
4375 case QEMU_OPTION_usb
:
4378 case QEMU_OPTION_usbdevice
:
4380 add_device_config(DEV_USB
, optarg
);
4382 case QEMU_OPTION_device
:
4383 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4387 case QEMU_OPTION_smp
:
4390 fprintf(stderr
, "Invalid number of CPUs\n");
4393 if (max_cpus
< smp_cpus
) {
4394 fprintf(stderr
, "maxcpus must be equal to or greater than "
4398 if (max_cpus
> 255) {
4399 fprintf(stderr
, "Unsupported number of maxcpus\n");
4403 case QEMU_OPTION_vnc
:
4404 display_type
= DT_VNC
;
4405 vnc_display
= optarg
;
4408 case QEMU_OPTION_no_acpi
:
4411 case QEMU_OPTION_no_hpet
:
4414 case QEMU_OPTION_balloon
:
4415 if (balloon_parse(optarg
) < 0) {
4416 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4421 case QEMU_OPTION_no_reboot
:
4424 case QEMU_OPTION_no_shutdown
:
4427 case QEMU_OPTION_show_cursor
:
4430 case QEMU_OPTION_uuid
:
4431 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4432 fprintf(stderr
, "Fail to parse UUID string."
4433 " Wrong format.\n");
4438 case QEMU_OPTION_daemonize
:
4442 case QEMU_OPTION_option_rom
:
4443 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4444 fprintf(stderr
, "Too many option ROMs\n");
4447 option_rom
[nb_option_roms
] = optarg
;
4450 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4451 case QEMU_OPTION_semihosting
:
4452 semihosting_enabled
= 1;
4455 case QEMU_OPTION_name
:
4456 qemu_name
= qemu_strdup(optarg
);
4458 char *p
= strchr(qemu_name
, ',');
4461 if (strncmp(p
, "process=", 8)) {
4462 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4470 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4471 case QEMU_OPTION_prom_env
:
4472 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4473 fprintf(stderr
, "Too many prom variables\n");
4476 prom_envs
[nb_prom_envs
] = optarg
;
4481 case QEMU_OPTION_old_param
:
4485 case QEMU_OPTION_clock
:
4486 configure_alarms(optarg
);
4488 case QEMU_OPTION_startdate
:
4489 configure_rtc_date_offset(optarg
, 1);
4491 case QEMU_OPTION_rtc
:
4492 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4494 fprintf(stderr
, "parse error: %s\n", optarg
);
4497 configure_rtc(opts
);
4499 case QEMU_OPTION_tb_size
:
4500 tb_size
= strtol(optarg
, NULL
, 0);
4504 case QEMU_OPTION_icount
:
4505 icount_option
= optarg
;
4507 case QEMU_OPTION_incoming
:
4510 case QEMU_OPTION_nodefaults
:
4512 default_parallel
= 0;
4513 default_virtcon
= 0;
4514 default_monitor
= 0;
4522 case QEMU_OPTION_chroot
:
4523 chroot_dir
= optarg
;
4525 case QEMU_OPTION_runas
:
4530 case QEMU_OPTION_xen_domid
:
4531 xen_domid
= atoi(optarg
);
4533 case QEMU_OPTION_xen_create
:
4534 xen_mode
= XEN_CREATE
;
4536 case QEMU_OPTION_xen_attach
:
4537 xen_mode
= XEN_ATTACH
;
4540 case QEMU_OPTION_readconfig
:
4543 fp
= fopen(optarg
, "r");
4545 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4548 if (qemu_config_parse(fp
, optarg
) != 0) {
4554 case QEMU_OPTION_writeconfig
:
4557 if (strcmp(optarg
, "-") == 0) {
4560 fp
= fopen(optarg
, "w");
4562 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4566 qemu_config_write(fp
);
4575 /* If no data_dir is specified then try to find it relative to the
4578 data_dir
= find_datadir(argv
[0]);
4580 /* If all else fails use the install patch specified when building. */
4582 data_dir
= CONFIG_QEMU_SHAREDIR
;
4586 * Default to max_cpus = smp_cpus, in case the user doesn't
4587 * specify a max_cpus value.
4590 max_cpus
= smp_cpus
;
4592 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4593 if (smp_cpus
> machine
->max_cpus
) {
4594 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4595 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4600 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4601 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4603 if (machine
->no_serial
) {
4606 if (machine
->no_parallel
) {
4607 default_parallel
= 0;
4609 if (!machine
->use_virtcon
) {
4610 default_virtcon
= 0;
4612 if (machine
->no_vga
) {
4615 if (machine
->no_floppy
) {
4618 if (machine
->no_cdrom
) {
4621 if (machine
->no_sdcard
) {
4625 if (display_type
== DT_NOGRAPHIC
) {
4626 if (default_parallel
)
4627 add_device_config(DEV_PARALLEL
, "null");
4628 if (default_serial
&& default_monitor
) {
4629 add_device_config(DEV_SERIAL
, "mon:stdio");
4630 } else if (default_virtcon
&& default_monitor
) {
4631 add_device_config(DEV_VIRTCON
, "mon:stdio");
4634 add_device_config(DEV_SERIAL
, "stdio");
4635 if (default_virtcon
)
4636 add_device_config(DEV_VIRTCON
, "stdio");
4637 if (default_monitor
)
4638 monitor_parse("stdio", "readline");
4642 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4643 if (default_parallel
)
4644 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4645 if (default_monitor
)
4646 monitor_parse("vc:80Cx24C", "readline");
4647 if (default_virtcon
)
4648 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4651 vga_interface_type
= VGA_CIRRUS
;
4653 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4660 if (pipe(fds
) == -1)
4671 len
= read(fds
[0], &status
, 1);
4672 if (len
== -1 && (errno
== EINTR
))
4677 else if (status
== 1) {
4678 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4686 qemu_set_cloexec(fds
[1]);
4698 signal(SIGTSTP
, SIG_IGN
);
4699 signal(SIGTTOU
, SIG_IGN
);
4700 signal(SIGTTIN
, SIG_IGN
);
4704 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4708 if (write(fds
[1], &status
, 1) != 1) {
4709 perror("daemonize. Writing to pipe\n");
4713 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4717 if (kvm_enabled()) {
4720 ret
= kvm_init(smp_cpus
);
4722 fprintf(stderr
, "failed to initialize KVM\n");
4727 if (qemu_init_main_loop()) {
4728 fprintf(stderr
, "qemu_init_main_loop failed\n");
4731 linux_boot
= (kernel_filename
!= NULL
);
4733 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4734 fprintf(stderr
, "-append only allowed with -kernel option\n");
4738 if (!linux_boot
&& initrd_filename
!= NULL
) {
4739 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4744 /* Win32 doesn't support line-buffering and requires size >= 2 */
4745 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4748 if (init_timer_alarm() < 0) {
4749 fprintf(stderr
, "could not initialize alarm timer\n");
4752 configure_icount(icount_option
);
4758 if (net_init_clients() < 0) {
4762 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4763 net_set_boot_mask(net_boot
);
4765 /* init the bluetooth world */
4766 if (foreach_device_config(DEV_BT
, bt_parse
))
4769 /* init the memory */
4771 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4773 /* init the dynamic translator */
4774 cpu_exec_init_all(tb_size
* 1024 * 1024);
4776 bdrv_init_with_whitelist();
4780 if (default_cdrom
) {
4781 /* we always create the cdrom drive, even if no disk is there */
4782 drive_add(NULL
, CDROM_ALIAS
);
4785 if (default_floppy
) {
4786 /* we always create at least one floppy */
4787 drive_add(NULL
, FD_ALIAS
, 0);
4790 if (default_sdcard
) {
4791 /* we always create one sd slot, even if no card is in it */
4792 drive_add(NULL
, SD_ALIAS
);
4795 /* open the virtual block devices */
4797 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4798 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4801 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4804 if (nb_numa_nodes
> 0) {
4807 if (nb_numa_nodes
> smp_cpus
) {
4808 nb_numa_nodes
= smp_cpus
;
4811 /* If no memory size if given for any node, assume the default case
4812 * and distribute the available memory equally across all nodes
4814 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4815 if (node_mem
[i
] != 0)
4818 if (i
== nb_numa_nodes
) {
4819 uint64_t usedmem
= 0;
4821 /* On Linux, the each node's border has to be 8MB aligned,
4822 * the final node gets the rest.
4824 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4825 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4826 usedmem
+= node_mem
[i
];
4828 node_mem
[i
] = ram_size
- usedmem
;
4831 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4832 if (node_cpumask
[i
] != 0)
4835 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4836 * must cope with this anyway, because there are BIOSes out there in
4837 * real machines which also use this scheme.
4839 if (i
== nb_numa_nodes
) {
4840 for (i
= 0; i
< smp_cpus
; i
++) {
4841 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4846 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4848 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4850 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4852 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4855 module_call_init(MODULE_INIT_DEVICE
);
4857 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4861 i
= select_watchdog(watchdog
);
4863 exit (i
== 1 ? 1 : 0);
4866 if (machine
->compat_props
) {
4867 qdev_prop_register_global_list(machine
->compat_props
);
4871 machine
->init(ram_size
, boot_devices
,
4872 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4874 cpu_synchronize_all_post_init();
4877 /* must be after terminal init, SDL library changes signal handlers */
4881 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4882 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4883 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4889 current_machine
= machine
;
4891 /* init USB devices */
4893 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4897 /* init generic devices */
4898 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4901 net_check_clients();
4903 /* just use the first displaystate for the moment */
4904 ds
= get_displaystate();
4906 if (display_type
== DT_DEFAULT
) {
4907 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4908 display_type
= DT_SDL
;
4910 display_type
= DT_VNC
;
4911 vnc_display
= "localhost:0,to=99";
4917 switch (display_type
) {
4920 #if defined(CONFIG_CURSES)
4922 curses_display_init(ds
, full_screen
);
4925 #if defined(CONFIG_SDL)
4927 sdl_display_init(ds
, full_screen
, no_frame
);
4929 #elif defined(CONFIG_COCOA)
4931 cocoa_display_init(ds
, full_screen
);
4935 vnc_display_init(ds
);
4936 if (vnc_display_open(ds
, vnc_display
) < 0)
4939 if (show_vnc_port
) {
4940 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4948 dcl
= ds
->listeners
;
4949 while (dcl
!= NULL
) {
4950 if (dcl
->dpy_refresh
!= NULL
) {
4951 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
4952 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
4957 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
4958 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
4959 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
4962 text_consoles_set_display(ds
);
4964 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
4967 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
4968 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
4973 qdev_machine_creation_done();
4975 if (rom_load_all() != 0) {
4976 fprintf(stderr
, "rom loading failed\n");
4980 qemu_system_reset();
4982 if (load_vmstate(loadvm
) < 0) {
4988 qemu_start_incoming_migration(incoming
);
4989 } else if (autostart
) {
4999 len
= write(fds
[1], &status
, 1);
5000 if (len
== -1 && (errno
== EINTR
))
5007 perror("not able to chdir to /");
5010 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5016 pwd
= getpwnam(run_as
);
5018 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5024 if (chroot(chroot_dir
) < 0) {
5025 fprintf(stderr
, "chroot failed\n");
5029 perror("not able to chdir to /");
5035 if (setgid(pwd
->pw_gid
) < 0) {
5036 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5039 if (setuid(pwd
->pw_uid
) < 0) {
5040 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5043 if (setuid(0) != -1) {
5044 fprintf(stderr
, "Dropping privileges failed\n");