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(Notifier
*obj
)
541 static int socket_init(void)
545 static Notifier notifier
= { .notify
= socket_cleanup
};
547 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
549 err
= WSAGetLastError();
550 fprintf(stderr
, "WSAStartup: %d\n", err
);
553 exit_notifier_add(¬ifier
);
558 /*********************/
560 /*********************/
562 static NotifierList exit_notifiers
= NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
564 void exit_notifier_add(Notifier
*notifier
)
566 notifier_list_add(&exit_notifiers
, notifier
);
569 void exit_notifier_remove(Notifier
*notifier
)
571 notifier_list_remove(&exit_notifiers
, notifier
);
574 static void exit_notifier_notify(void)
576 notifier_list_notify(&exit_notifiers
);
579 static void exit_notifier_init(void)
581 atexit(exit_notifier_notify
);
584 /***********************************************************/
585 /* Bluetooth support */
588 static struct HCIInfo
*hci_table
[MAX_NICS
];
590 static struct bt_vlan_s
{
591 struct bt_scatternet_s net
;
593 struct bt_vlan_s
*next
;
596 /* find or alloc a new bluetooth "VLAN" */
597 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
599 struct bt_vlan_s
**pvlan
, *vlan
;
600 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
604 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
606 pvlan
= &first_bt_vlan
;
607 while (*pvlan
!= NULL
)
608 pvlan
= &(*pvlan
)->next
;
613 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
617 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
622 static struct HCIInfo null_hci
= {
623 .cmd_send
= null_hci_send
,
624 .sco_send
= null_hci_send
,
625 .acl_send
= null_hci_send
,
626 .bdaddr_set
= null_hci_addr_set
,
629 struct HCIInfo
*qemu_next_hci(void)
631 if (cur_hci
== nb_hcis
)
634 return hci_table
[cur_hci
++];
637 static struct HCIInfo
*hci_init(const char *str
)
640 struct bt_scatternet_s
*vlan
= 0;
642 if (!strcmp(str
, "null"))
645 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
647 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
648 else if (!strncmp(str
, "hci", 3)) {
651 if (!strncmp(str
+ 3, ",vlan=", 6)) {
652 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
657 vlan
= qemu_find_bt_vlan(0);
659 return bt_new_hci(vlan
);
662 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
667 static int bt_hci_parse(const char *str
)
672 if (nb_hcis
>= MAX_NICS
) {
673 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
686 bdaddr
.b
[5] = 0x56 + nb_hcis
;
687 hci
->bdaddr_set(hci
, bdaddr
.b
);
689 hci_table
[nb_hcis
++] = hci
;
694 static void bt_vhci_add(int vlan_id
)
696 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
699 fprintf(stderr
, "qemu: warning: adding a VHCI to "
700 "an empty scatternet %i\n", vlan_id
);
702 bt_vhci_init(bt_new_hci(vlan
));
705 static struct bt_device_s
*bt_device_add(const char *opt
)
707 struct bt_scatternet_s
*vlan
;
709 char *endp
= strstr(opt
, ",vlan=");
710 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
713 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
716 vlan_id
= strtol(endp
+ 6, &endp
, 0);
718 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
723 vlan
= qemu_find_bt_vlan(vlan_id
);
726 fprintf(stderr
, "qemu: warning: adding a slave device to "
727 "an empty scatternet %i\n", vlan_id
);
729 if (!strcmp(devname
, "keyboard"))
730 return bt_keyboard_init(vlan
);
732 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
736 static int bt_parse(const char *opt
)
738 const char *endp
, *p
;
741 if (strstart(opt
, "hci", &endp
)) {
742 if (!*endp
|| *endp
== ',') {
744 if (!strstart(endp
, ",vlan=", 0))
747 return bt_hci_parse(opt
);
749 } else if (strstart(opt
, "vhci", &endp
)) {
750 if (!*endp
|| *endp
== ',') {
752 if (strstart(endp
, ",vlan=", &p
)) {
753 vlan
= strtol(p
, (char **) &endp
, 0);
755 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
759 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
768 } else if (strstart(opt
, "device:", &endp
))
769 return !bt_device_add(endp
);
771 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
775 /***********************************************************/
776 /* QEMU Block devices */
778 #define HD_ALIAS "index=%d,media=disk"
779 #define CDROM_ALIAS "index=2,media=cdrom"
780 #define FD_ALIAS "index=%d,if=floppy"
781 #define PFLASH_ALIAS "if=pflash"
782 #define MTD_ALIAS "if=mtd"
783 #define SD_ALIAS "index=0,if=sd"
785 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
792 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
795 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
797 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
798 __FUNCTION__
, optstr
);
802 qemu_opt_set(opts
, "file", file
);
806 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
810 /* seek interface, bus and unit */
812 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
813 if (dinfo
->type
== type
&&
822 DriveInfo
*drive_get_by_id(const char *id
)
826 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
827 if (strcmp(id
, dinfo
->id
))
834 int drive_get_max_bus(BlockInterfaceType type
)
840 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
841 if(dinfo
->type
== type
&&
842 dinfo
->bus
> max_bus
)
843 max_bus
= dinfo
->bus
;
848 const char *drive_get_serial(BlockDriverState
*bdrv
)
852 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
853 if (dinfo
->bdrv
== bdrv
)
854 return dinfo
->serial
;
860 BlockInterfaceErrorAction
drive_get_on_error(
861 BlockDriverState
*bdrv
, int is_read
)
865 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
866 if (dinfo
->bdrv
== bdrv
)
867 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
870 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
873 static void bdrv_format_print(void *opaque
, const char *name
)
875 fprintf(stderr
, " %s", name
);
878 void drive_uninit(DriveInfo
*dinfo
)
880 qemu_opts_del(dinfo
->opts
);
881 bdrv_delete(dinfo
->bdrv
);
882 QTAILQ_REMOVE(&drives
, dinfo
, next
);
886 static int parse_block_error_action(const char *buf
, int is_read
)
888 if (!strcmp(buf
, "ignore")) {
889 return BLOCK_ERR_IGNORE
;
890 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
891 return BLOCK_ERR_STOP_ENOSPC
;
892 } else if (!strcmp(buf
, "stop")) {
893 return BLOCK_ERR_STOP_ANY
;
894 } else if (!strcmp(buf
, "report")) {
895 return BLOCK_ERR_REPORT
;
897 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
898 buf
, is_read
? "read" : "write");
903 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
907 const char *file
= NULL
;
910 const char *mediastr
= "";
911 BlockInterfaceType type
;
912 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
914 int cyls
, heads
, secs
, translation
;
915 BlockDriver
*drv
= NULL
;
916 QEMUMachine
*machine
= opaque
;
923 int on_read_error
, on_write_error
;
930 translation
= BIOS_ATA_TRANSLATION_AUTO
;
933 if (machine
&& machine
->use_scsi
) {
935 max_devs
= MAX_SCSI_DEVS
;
936 pstrcpy(devname
, sizeof(devname
), "scsi");
939 max_devs
= MAX_IDE_DEVS
;
940 pstrcpy(devname
, sizeof(devname
), "ide");
944 /* extract parameters */
945 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
946 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
947 index
= qemu_opt_get_number(opts
, "index", -1);
949 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
950 heads
= qemu_opt_get_number(opts
, "heads", 0);
951 secs
= qemu_opt_get_number(opts
, "secs", 0);
953 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
954 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
956 file
= qemu_opt_get(opts
, "file");
957 serial
= qemu_opt_get(opts
, "serial");
959 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
960 pstrcpy(devname
, sizeof(devname
), buf
);
961 if (!strcmp(buf
, "ide")) {
963 max_devs
= MAX_IDE_DEVS
;
964 } else if (!strcmp(buf
, "scsi")) {
966 max_devs
= MAX_SCSI_DEVS
;
967 } else if (!strcmp(buf
, "floppy")) {
970 } else if (!strcmp(buf
, "pflash")) {
973 } else if (!strcmp(buf
, "mtd")) {
976 } else if (!strcmp(buf
, "sd")) {
979 } else if (!strcmp(buf
, "virtio")) {
982 } else if (!strcmp(buf
, "xen")) {
985 } else if (!strcmp(buf
, "none")) {
989 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
994 if (cyls
|| heads
|| secs
) {
995 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
996 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
999 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1000 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1003 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1004 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1009 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1012 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1016 if (!strcmp(buf
, "none"))
1017 translation
= BIOS_ATA_TRANSLATION_NONE
;
1018 else if (!strcmp(buf
, "lba"))
1019 translation
= BIOS_ATA_TRANSLATION_LBA
;
1020 else if (!strcmp(buf
, "auto"))
1021 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1023 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1028 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1029 if (!strcmp(buf
, "disk")) {
1031 } else if (!strcmp(buf
, "cdrom")) {
1032 if (cyls
|| secs
|| heads
) {
1034 "qemu: '%s' invalid physical CHS format\n", buf
);
1037 media
= MEDIA_CDROM
;
1039 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1044 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1045 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1047 else if (!strcmp(buf
, "writethrough"))
1049 else if (!strcmp(buf
, "writeback"))
1052 fprintf(stderr
, "qemu: invalid cache option\n");
1057 #ifdef CONFIG_LINUX_AIO
1058 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1059 if (!strcmp(buf
, "threads"))
1061 else if (!strcmp(buf
, "native"))
1064 fprintf(stderr
, "qemu: invalid aio option\n");
1070 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1071 if (strcmp(buf
, "?") == 0) {
1072 fprintf(stderr
, "qemu: Supported formats:");
1073 bdrv_iterate_format(bdrv_format_print
, NULL
);
1074 fprintf(stderr
, "\n");
1077 drv
= bdrv_find_whitelisted_format(buf
);
1079 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1084 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1085 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1086 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1087 fprintf(stderr
, "werror is no supported by this format\n");
1091 on_write_error
= parse_block_error_action(buf
, 0);
1092 if (on_write_error
< 0) {
1097 on_read_error
= BLOCK_ERR_REPORT
;
1098 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1099 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1100 fprintf(stderr
, "rerror is no supported by this format\n");
1104 on_read_error
= parse_block_error_action(buf
, 1);
1105 if (on_read_error
< 0) {
1110 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1111 if (type
!= IF_VIRTIO
) {
1112 fprintf(stderr
, "addr is not supported\n");
1117 /* compute bus and unit according index */
1120 if (bus_id
!= 0 || unit_id
!= -1) {
1122 "qemu: index cannot be used with bus and unit\n");
1130 unit_id
= index
% max_devs
;
1131 bus_id
= index
/ max_devs
;
1135 /* if user doesn't specify a unit_id,
1136 * try to find the first free
1139 if (unit_id
== -1) {
1141 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1143 if (max_devs
&& unit_id
>= max_devs
) {
1144 unit_id
-= max_devs
;
1152 if (max_devs
&& unit_id
>= max_devs
) {
1153 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1154 unit_id
, max_devs
- 1);
1159 * ignore multiple definitions
1162 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1169 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1170 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1171 dinfo
->id
= qemu_strdup(buf
);
1173 /* no id supplied -> create one */
1174 dinfo
->id
= qemu_mallocz(32);
1175 if (type
== IF_IDE
|| type
== IF_SCSI
)
1176 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1178 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1179 devname
, bus_id
, mediastr
, unit_id
);
1181 snprintf(dinfo
->id
, 32, "%s%s%i",
1182 devname
, mediastr
, unit_id
);
1184 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1185 dinfo
->devaddr
= devaddr
;
1187 dinfo
->bus
= bus_id
;
1188 dinfo
->unit
= unit_id
;
1189 dinfo
->on_read_error
= on_read_error
;
1190 dinfo
->on_write_error
= on_write_error
;
1193 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1194 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1204 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1205 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1209 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1214 /* FIXME: This isn't really a floppy, but it's a reasonable
1217 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1223 /* add virtio block device */
1224 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1225 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1226 qemu_opt_set(opts
, "drive", dinfo
->id
);
1228 qemu_opt_set(opts
, "addr", devaddr
);
1239 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1240 cache
= 2; /* always use write-back with snapshot */
1242 if (cache
== 0) /* no caching */
1243 bdrv_flags
|= BDRV_O_NOCACHE
;
1244 else if (cache
== 2) /* write-back */
1245 bdrv_flags
|= BDRV_O_CACHE_WB
;
1248 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1250 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1254 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1255 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1260 * cdrom is read-only. Set it now, after above interface checking
1261 * since readonly attribute not explicitly required, so no error.
1263 if (media
== MEDIA_CDROM
) {
1266 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1268 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1269 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1270 file
, strerror(errno
));
1274 if (bdrv_key_required(dinfo
->bdrv
))
1280 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1282 QEMUMachine
*machine
= opaque
;
1283 int fatal_error
= 0;
1285 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1292 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1294 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1295 qemu_opt_set(opts
, "snapshot", "on");
1300 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1302 boot_set_handler
= func
;
1303 boot_set_opaque
= opaque
;
1306 int qemu_boot_set(const char *boot_devices
)
1308 if (!boot_set_handler
) {
1311 return boot_set_handler(boot_set_opaque
, boot_devices
);
1314 static int parse_bootdevices(char *devices
)
1316 /* We just do some generic consistency checks */
1320 for (p
= devices
; *p
!= '\0'; p
++) {
1321 /* Allowed boot devices are:
1322 * a-b: floppy disk drives
1323 * c-f: IDE disk drives
1324 * g-m: machine implementation dependant drives
1325 * n-p: network devices
1326 * It's up to each machine implementation to check if the given boot
1327 * devices match the actual hardware implementation and firmware
1330 if (*p
< 'a' || *p
> 'p') {
1331 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1334 if (bitmap
& (1 << (*p
- 'a'))) {
1335 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1338 bitmap
|= 1 << (*p
- 'a');
1343 static void restore_boot_devices(void *opaque
)
1345 char *standard_boot_devices
= opaque
;
1347 qemu_boot_set(standard_boot_devices
);
1349 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1350 qemu_free(standard_boot_devices
);
1353 static void numa_add(const char *optarg
)
1357 unsigned long long value
, endvalue
;
1360 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1361 if (!strcmp(option
, "node")) {
1362 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1363 nodenr
= nb_numa_nodes
;
1365 nodenr
= strtoull(option
, NULL
, 10);
1368 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1369 node_mem
[nodenr
] = 0;
1371 value
= strtoull(option
, &endptr
, 0);
1373 case 0: case 'M': case 'm':
1380 node_mem
[nodenr
] = value
;
1382 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1383 node_cpumask
[nodenr
] = 0;
1385 value
= strtoull(option
, &endptr
, 10);
1388 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1390 if (*endptr
== '-') {
1391 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1392 if (endvalue
>= 63) {
1395 "only 63 CPUs in NUMA mode supported.\n");
1397 value
= (2ULL << endvalue
) - (1ULL << value
);
1399 value
= 1ULL << value
;
1402 node_cpumask
[nodenr
] = value
;
1409 static void smp_parse(const char *optarg
)
1411 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1415 smp
= strtoul(optarg
, &endptr
, 10);
1416 if (endptr
!= optarg
) {
1417 if (*endptr
== ',') {
1421 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1422 sockets
= strtoull(option
, NULL
, 10);
1423 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1424 cores
= strtoull(option
, NULL
, 10);
1425 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1426 threads
= strtoull(option
, NULL
, 10);
1427 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1428 max_cpus
= strtoull(option
, NULL
, 10);
1430 /* compute missing values, prefer sockets over cores over threads */
1431 if (smp
== 0 || sockets
== 0) {
1432 sockets
= sockets
> 0 ? sockets
: 1;
1433 cores
= cores
> 0 ? cores
: 1;
1434 threads
= threads
> 0 ? threads
: 1;
1436 smp
= cores
* threads
* sockets
;
1440 threads
= threads
> 0 ? threads
: 1;
1441 cores
= smp
/ (sockets
* threads
);
1444 threads
= smp
/ (cores
* sockets
);
1449 smp_cores
= cores
> 0 ? cores
: 1;
1450 smp_threads
= threads
> 0 ? threads
: 1;
1452 max_cpus
= smp_cpus
;
1455 /***********************************************************/
1458 static int usb_device_add(const char *devname
, int is_hotplug
)
1461 USBDevice
*dev
= NULL
;
1466 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1467 dev
= usbdevice_create(devname
);
1471 /* the other ones */
1472 if (strstart(devname
, "host:", &p
)) {
1473 dev
= usb_host_device_open(p
);
1474 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1475 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1476 bt_new_hci(qemu_find_bt_vlan(0)));
1487 static int usb_device_del(const char *devname
)
1492 if (strstart(devname
, "host:", &p
))
1493 return usb_host_device_close(p
);
1498 p
= strchr(devname
, '.');
1501 bus_num
= strtoul(devname
, NULL
, 0);
1502 addr
= strtoul(p
+ 1, NULL
, 0);
1504 return usb_device_delete_addr(bus_num
, addr
);
1507 static int usb_parse(const char *cmdline
)
1510 r
= usb_device_add(cmdline
, 0);
1512 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1517 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1519 const char *devname
= qdict_get_str(qdict
, "devname");
1520 if (usb_device_add(devname
, 1) < 0) {
1521 error_report("could not add USB device '%s'", devname
);
1525 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1527 const char *devname
= qdict_get_str(qdict
, "devname");
1528 if (usb_device_del(devname
) < 0) {
1529 error_report("could not delete USB device '%s'", devname
);
1533 /***********************************************************/
1534 /* PCMCIA/Cardbus */
1536 static struct pcmcia_socket_entry_s
{
1537 PCMCIASocket
*socket
;
1538 struct pcmcia_socket_entry_s
*next
;
1539 } *pcmcia_sockets
= 0;
1541 void pcmcia_socket_register(PCMCIASocket
*socket
)
1543 struct pcmcia_socket_entry_s
*entry
;
1545 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1546 entry
->socket
= socket
;
1547 entry
->next
= pcmcia_sockets
;
1548 pcmcia_sockets
= entry
;
1551 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1553 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1555 ptr
= &pcmcia_sockets
;
1556 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1557 if (entry
->socket
== socket
) {
1563 void pcmcia_info(Monitor
*mon
)
1565 struct pcmcia_socket_entry_s
*iter
;
1567 if (!pcmcia_sockets
)
1568 monitor_printf(mon
, "No PCMCIA sockets\n");
1570 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1571 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1572 iter
->socket
->attached
? iter
->socket
->card_string
:
1576 /***********************************************************/
1579 typedef struct IOHandlerRecord
{
1581 IOCanReadHandler
*fd_read_poll
;
1583 IOHandler
*fd_write
;
1586 /* temporary data */
1588 QLIST_ENTRY(IOHandlerRecord
) next
;
1591 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1592 QLIST_HEAD_INITIALIZER(io_handlers
);
1595 /* XXX: fd_read_poll should be suppressed, but an API change is
1596 necessary in the character devices to suppress fd_can_read(). */
1597 int qemu_set_fd_handler2(int fd
,
1598 IOCanReadHandler
*fd_read_poll
,
1600 IOHandler
*fd_write
,
1603 IOHandlerRecord
*ioh
;
1605 if (!fd_read
&& !fd_write
) {
1606 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1607 if (ioh
->fd
== fd
) {
1613 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1617 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1618 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1621 ioh
->fd_read_poll
= fd_read_poll
;
1622 ioh
->fd_read
= fd_read
;
1623 ioh
->fd_write
= fd_write
;
1624 ioh
->opaque
= opaque
;
1630 int qemu_set_fd_handler(int fd
,
1632 IOHandler
*fd_write
,
1635 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1639 /***********************************************************/
1640 /* Polling handling */
1642 typedef struct PollingEntry
{
1645 struct PollingEntry
*next
;
1648 static PollingEntry
*first_polling_entry
;
1650 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1652 PollingEntry
**ppe
, *pe
;
1653 pe
= qemu_mallocz(sizeof(PollingEntry
));
1655 pe
->opaque
= opaque
;
1656 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1661 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1663 PollingEntry
**ppe
, *pe
;
1664 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1666 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1674 /***********************************************************/
1675 /* Wait objects support */
1676 typedef struct WaitObjects
{
1678 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1679 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1680 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1683 static WaitObjects wait_objects
= {0};
1685 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1687 WaitObjects
*w
= &wait_objects
;
1689 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1691 w
->events
[w
->num
] = handle
;
1692 w
->func
[w
->num
] = func
;
1693 w
->opaque
[w
->num
] = opaque
;
1698 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1701 WaitObjects
*w
= &wait_objects
;
1704 for (i
= 0; i
< w
->num
; i
++) {
1705 if (w
->events
[i
] == handle
)
1708 w
->events
[i
] = w
->events
[i
+ 1];
1709 w
->func
[i
] = w
->func
[i
+ 1];
1710 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1718 /***********************************************************/
1719 /* ram save/restore */
1721 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1722 #define RAM_SAVE_FLAG_COMPRESS 0x02
1723 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1724 #define RAM_SAVE_FLAG_PAGE 0x08
1725 #define RAM_SAVE_FLAG_EOS 0x10
1727 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1729 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1730 uint32_t *array
= (uint32_t *)page
;
1733 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1734 if (array
[i
] != val
)
1741 static int ram_save_block(QEMUFile
*f
)
1743 static ram_addr_t current_addr
= 0;
1744 ram_addr_t saved_addr
= current_addr
;
1745 ram_addr_t addr
= 0;
1748 while (addr
< last_ram_offset
) {
1749 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1752 cpu_physical_memory_reset_dirty(current_addr
,
1753 current_addr
+ TARGET_PAGE_SIZE
,
1754 MIGRATION_DIRTY_FLAG
);
1756 p
= qemu_get_ram_ptr(current_addr
);
1758 if (is_dup_page(p
, *p
)) {
1759 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1760 qemu_put_byte(f
, *p
);
1762 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1763 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1769 addr
+= TARGET_PAGE_SIZE
;
1770 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1776 static uint64_t bytes_transferred
;
1778 static ram_addr_t
ram_save_remaining(void)
1781 ram_addr_t count
= 0;
1783 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1784 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1791 uint64_t ram_bytes_remaining(void)
1793 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1796 uint64_t ram_bytes_transferred(void)
1798 return bytes_transferred
;
1801 uint64_t ram_bytes_total(void)
1803 return last_ram_offset
;
1806 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1809 uint64_t bytes_transferred_last
;
1811 uint64_t expected_time
= 0;
1814 cpu_physical_memory_set_dirty_tracking(0);
1818 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1819 qemu_file_set_error(f
);
1824 bytes_transferred
= 0;
1826 /* Make sure all dirty bits are set */
1827 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1828 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1829 cpu_physical_memory_set_dirty(addr
);
1832 /* Enable dirty memory tracking */
1833 cpu_physical_memory_set_dirty_tracking(1);
1835 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1838 bytes_transferred_last
= bytes_transferred
;
1839 bwidth
= qemu_get_clock_ns(rt_clock
);
1841 while (!qemu_file_rate_limit(f
)) {
1844 ret
= ram_save_block(f
);
1845 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1846 if (ret
== 0) /* no more blocks */
1850 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1851 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1853 /* if we haven't transferred anything this round, force expected_time to a
1854 * a very high value, but without crashing */
1858 /* try transferring iterative blocks of memory */
1860 /* flush all remaining blocks regardless of rate limiting */
1861 while (ram_save_block(f
) != 0) {
1862 bytes_transferred
+= TARGET_PAGE_SIZE
;
1864 cpu_physical_memory_set_dirty_tracking(0);
1867 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1869 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1871 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1874 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1879 if (version_id
!= 3)
1883 addr
= qemu_get_be64(f
);
1885 flags
= addr
& ~TARGET_PAGE_MASK
;
1886 addr
&= TARGET_PAGE_MASK
;
1888 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1889 if (addr
!= last_ram_offset
)
1893 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1894 uint8_t ch
= qemu_get_byte(f
);
1895 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1898 (!kvm_enabled() || kvm_has_sync_mmu())) {
1899 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1902 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1903 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1905 if (qemu_file_has_error(f
)) {
1908 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1913 void qemu_service_io(void)
1915 qemu_notify_event();
1918 /***********************************************************/
1919 /* machine registration */
1921 static QEMUMachine
*first_machine
= NULL
;
1922 QEMUMachine
*current_machine
= NULL
;
1924 int qemu_register_machine(QEMUMachine
*m
)
1927 pm
= &first_machine
;
1935 static QEMUMachine
*find_machine(const char *name
)
1939 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1940 if (!strcmp(m
->name
, name
))
1942 if (m
->alias
&& !strcmp(m
->alias
, name
))
1948 static QEMUMachine
*find_default_machine(void)
1952 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1953 if (m
->is_default
) {
1960 /***********************************************************/
1961 /* main execution loop */
1963 static void gui_update(void *opaque
)
1965 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1966 DisplayState
*ds
= opaque
;
1967 DisplayChangeListener
*dcl
= ds
->listeners
;
1969 qemu_flush_coalesced_mmio_buffer();
1972 while (dcl
!= NULL
) {
1973 if (dcl
->gui_timer_interval
&&
1974 dcl
->gui_timer_interval
< interval
)
1975 interval
= dcl
->gui_timer_interval
;
1978 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1981 static void nographic_update(void *opaque
)
1983 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1985 qemu_flush_coalesced_mmio_buffer();
1986 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1989 void cpu_synchronize_all_states(void)
1993 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1994 cpu_synchronize_state(cpu
);
1998 void cpu_synchronize_all_post_reset(void)
2002 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2003 cpu_synchronize_post_reset(cpu
);
2007 void cpu_synchronize_all_post_init(void)
2011 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2012 cpu_synchronize_post_init(cpu
);
2016 struct vm_change_state_entry
{
2017 VMChangeStateHandler
*cb
;
2019 QLIST_ENTRY (vm_change_state_entry
) entries
;
2022 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2024 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2027 VMChangeStateEntry
*e
;
2029 e
= qemu_mallocz(sizeof (*e
));
2033 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2037 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2039 QLIST_REMOVE (e
, entries
);
2043 static void vm_state_notify(int running
, int reason
)
2045 VMChangeStateEntry
*e
;
2047 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2048 e
->cb(e
->opaque
, running
, reason
);
2052 static void resume_all_vcpus(void);
2053 static void pause_all_vcpus(void);
2060 vm_state_notify(1, 0);
2065 /* reset/shutdown handler */
2067 typedef struct QEMUResetEntry
{
2068 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2069 QEMUResetHandler
*func
;
2073 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2074 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2075 static int reset_requested
;
2076 static int shutdown_requested
;
2077 static int powerdown_requested
;
2078 static int debug_requested
;
2079 static int vmstop_requested
;
2081 int qemu_shutdown_requested(void)
2083 int r
= shutdown_requested
;
2084 shutdown_requested
= 0;
2088 int qemu_reset_requested(void)
2090 int r
= reset_requested
;
2091 reset_requested
= 0;
2095 int qemu_powerdown_requested(void)
2097 int r
= powerdown_requested
;
2098 powerdown_requested
= 0;
2102 static int qemu_debug_requested(void)
2104 int r
= debug_requested
;
2105 debug_requested
= 0;
2109 static int qemu_vmstop_requested(void)
2111 int r
= vmstop_requested
;
2112 vmstop_requested
= 0;
2116 static void do_vm_stop(int reason
)
2119 cpu_disable_ticks();
2122 vm_state_notify(0, reason
);
2123 monitor_protocol_event(QEVENT_STOP
, NULL
);
2127 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2129 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2132 re
->opaque
= opaque
;
2133 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2136 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2140 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2141 if (re
->func
== func
&& re
->opaque
== opaque
) {
2142 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2149 void qemu_system_reset(void)
2151 QEMUResetEntry
*re
, *nre
;
2153 /* reset all devices */
2154 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2155 re
->func(re
->opaque
);
2157 monitor_protocol_event(QEVENT_RESET
, NULL
);
2158 cpu_synchronize_all_post_reset();
2161 void qemu_system_reset_request(void)
2164 shutdown_requested
= 1;
2166 reset_requested
= 1;
2168 qemu_notify_event();
2171 void qemu_system_shutdown_request(void)
2173 shutdown_requested
= 1;
2174 qemu_notify_event();
2177 void qemu_system_powerdown_request(void)
2179 powerdown_requested
= 1;
2180 qemu_notify_event();
2183 static int cpu_can_run(CPUState
*env
)
2194 static int cpu_has_work(CPUState
*env
)
2202 if (qemu_cpu_has_work(env
))
2207 static int tcg_has_work(void)
2211 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2212 if (cpu_has_work(env
))
2218 static int io_thread_fd
= -1;
2220 static void qemu_event_increment(void)
2222 /* Write 8 bytes to be compatible with eventfd. */
2223 static uint64_t val
= 1;
2226 if (io_thread_fd
== -1)
2230 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2231 } while (ret
< 0 && errno
== EINTR
);
2233 /* EAGAIN is fine, a read must be pending. */
2234 if (ret
< 0 && errno
!= EAGAIN
) {
2235 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2241 static void qemu_event_read(void *opaque
)
2243 int fd
= (unsigned long)opaque
;
2247 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2249 len
= read(fd
, buffer
, sizeof(buffer
));
2250 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2253 static int qemu_event_init(void)
2258 err
= qemu_eventfd(fds
);
2262 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2266 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2270 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2271 (void *)(unsigned long)fds
[0]);
2273 io_thread_fd
= fds
[1];
2282 HANDLE qemu_event_handle
;
2284 static void dummy_event_handler(void *opaque
)
2288 static int qemu_event_init(void)
2290 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2291 if (!qemu_event_handle
) {
2292 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2295 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2299 static void qemu_event_increment(void)
2301 if (!SetEvent(qemu_event_handle
)) {
2302 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2309 #ifndef CONFIG_IOTHREAD
2310 static int qemu_init_main_loop(void)
2312 return qemu_event_init();
2315 void qemu_init_vcpu(void *_env
)
2317 CPUState
*env
= _env
;
2319 env
->nr_cores
= smp_cores
;
2320 env
->nr_threads
= smp_threads
;
2326 int qemu_cpu_self(void *env
)
2331 static void resume_all_vcpus(void)
2335 static void pause_all_vcpus(void)
2339 void qemu_cpu_kick(void *env
)
2344 void qemu_notify_event(void)
2346 CPUState
*env
= cpu_single_env
;
2348 qemu_event_increment ();
2352 if (next_cpu
&& env
!= next_cpu
) {
2357 void qemu_mutex_lock_iothread(void) {}
2358 void qemu_mutex_unlock_iothread(void) {}
2360 void vm_stop(int reason
)
2365 #else /* CONFIG_IOTHREAD */
2367 #include "qemu-thread.h"
2369 QemuMutex qemu_global_mutex
;
2370 static QemuMutex qemu_fair_mutex
;
2372 static QemuThread io_thread
;
2374 static QemuThread
*tcg_cpu_thread
;
2375 static QemuCond
*tcg_halt_cond
;
2377 static int qemu_system_ready
;
2379 static QemuCond qemu_cpu_cond
;
2381 static QemuCond qemu_system_cond
;
2382 static QemuCond qemu_pause_cond
;
2384 static void tcg_block_io_signals(void);
2385 static void kvm_block_io_signals(CPUState
*env
);
2386 static void unblock_io_signals(void);
2388 static int qemu_init_main_loop(void)
2392 ret
= qemu_event_init();
2396 qemu_cond_init(&qemu_pause_cond
);
2397 qemu_mutex_init(&qemu_fair_mutex
);
2398 qemu_mutex_init(&qemu_global_mutex
);
2399 qemu_mutex_lock(&qemu_global_mutex
);
2401 unblock_io_signals();
2402 qemu_thread_self(&io_thread
);
2407 static void qemu_wait_io_event_common(CPUState
*env
)
2412 qemu_cond_signal(&qemu_pause_cond
);
2416 static void qemu_wait_io_event(CPUState
*env
)
2418 while (!tcg_has_work())
2419 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2421 qemu_mutex_unlock(&qemu_global_mutex
);
2424 * Users of qemu_global_mutex can be starved, having no chance
2425 * to acquire it since this path will get to it first.
2426 * So use another lock to provide fairness.
2428 qemu_mutex_lock(&qemu_fair_mutex
);
2429 qemu_mutex_unlock(&qemu_fair_mutex
);
2431 qemu_mutex_lock(&qemu_global_mutex
);
2432 qemu_wait_io_event_common(env
);
2435 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2442 ts
.tv_sec
= timeout
/ 1000;
2443 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2445 sigemptyset(&waitset
);
2446 sigaddset(&waitset
, SIG_IPI
);
2448 qemu_mutex_unlock(&qemu_global_mutex
);
2449 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2451 qemu_mutex_lock(&qemu_global_mutex
);
2453 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2454 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2459 static void qemu_kvm_wait_io_event(CPUState
*env
)
2461 while (!cpu_has_work(env
))
2462 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2464 qemu_kvm_eat_signal(env
, 0);
2465 qemu_wait_io_event_common(env
);
2468 static int qemu_cpu_exec(CPUState
*env
);
2470 static void *kvm_cpu_thread_fn(void *arg
)
2472 CPUState
*env
= arg
;
2474 qemu_thread_self(env
->thread
);
2478 kvm_block_io_signals(env
);
2480 /* signal CPU creation */
2481 qemu_mutex_lock(&qemu_global_mutex
);
2483 qemu_cond_signal(&qemu_cpu_cond
);
2485 /* and wait for machine initialization */
2486 while (!qemu_system_ready
)
2487 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2490 if (cpu_can_run(env
))
2492 qemu_kvm_wait_io_event(env
);
2498 static bool tcg_cpu_exec(void);
2500 static void *tcg_cpu_thread_fn(void *arg
)
2502 CPUState
*env
= arg
;
2504 tcg_block_io_signals();
2505 qemu_thread_self(env
->thread
);
2507 /* signal CPU creation */
2508 qemu_mutex_lock(&qemu_global_mutex
);
2509 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2511 qemu_cond_signal(&qemu_cpu_cond
);
2513 /* and wait for machine initialization */
2514 while (!qemu_system_ready
)
2515 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2519 qemu_wait_io_event(cur_cpu
);
2525 void qemu_cpu_kick(void *_env
)
2527 CPUState
*env
= _env
;
2528 qemu_cond_broadcast(env
->halt_cond
);
2530 qemu_thread_signal(env
->thread
, SIG_IPI
);
2533 int qemu_cpu_self(void *_env
)
2535 CPUState
*env
= _env
;
2538 qemu_thread_self(&this);
2540 return qemu_thread_equal(&this, env
->thread
);
2543 static void cpu_signal(int sig
)
2546 cpu_exit(cpu_single_env
);
2549 static void tcg_block_io_signals(void)
2552 struct sigaction sigact
;
2555 sigaddset(&set
, SIGUSR2
);
2556 sigaddset(&set
, SIGIO
);
2557 sigaddset(&set
, SIGALRM
);
2558 sigaddset(&set
, SIGCHLD
);
2559 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2562 sigaddset(&set
, SIG_IPI
);
2563 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2565 memset(&sigact
, 0, sizeof(sigact
));
2566 sigact
.sa_handler
= cpu_signal
;
2567 sigaction(SIG_IPI
, &sigact
, NULL
);
2570 static void dummy_signal(int sig
)
2574 static void kvm_block_io_signals(CPUState
*env
)
2578 struct sigaction sigact
;
2581 sigaddset(&set
, SIGUSR2
);
2582 sigaddset(&set
, SIGIO
);
2583 sigaddset(&set
, SIGALRM
);
2584 sigaddset(&set
, SIGCHLD
);
2585 sigaddset(&set
, SIG_IPI
);
2586 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2588 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2589 sigdelset(&set
, SIG_IPI
);
2591 memset(&sigact
, 0, sizeof(sigact
));
2592 sigact
.sa_handler
= dummy_signal
;
2593 sigaction(SIG_IPI
, &sigact
, NULL
);
2595 r
= kvm_set_signal_mask(env
, &set
);
2597 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2602 static void unblock_io_signals(void)
2607 sigaddset(&set
, SIGUSR2
);
2608 sigaddset(&set
, SIGIO
);
2609 sigaddset(&set
, SIGALRM
);
2610 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2613 sigaddset(&set
, SIG_IPI
);
2614 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2617 static void qemu_signal_lock(unsigned int msecs
)
2619 qemu_mutex_lock(&qemu_fair_mutex
);
2621 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2622 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2623 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2626 qemu_mutex_unlock(&qemu_fair_mutex
);
2629 void qemu_mutex_lock_iothread(void)
2631 if (kvm_enabled()) {
2632 qemu_mutex_lock(&qemu_fair_mutex
);
2633 qemu_mutex_lock(&qemu_global_mutex
);
2634 qemu_mutex_unlock(&qemu_fair_mutex
);
2636 qemu_signal_lock(100);
2639 void qemu_mutex_unlock_iothread(void)
2641 qemu_mutex_unlock(&qemu_global_mutex
);
2644 static int all_vcpus_paused(void)
2646 CPUState
*penv
= first_cpu
;
2651 penv
= (CPUState
*)penv
->next_cpu
;
2657 static void pause_all_vcpus(void)
2659 CPUState
*penv
= first_cpu
;
2663 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2664 qemu_cpu_kick(penv
);
2665 penv
= (CPUState
*)penv
->next_cpu
;
2668 while (!all_vcpus_paused()) {
2669 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2672 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2673 penv
= (CPUState
*)penv
->next_cpu
;
2678 static void resume_all_vcpus(void)
2680 CPUState
*penv
= first_cpu
;
2685 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2686 qemu_cpu_kick(penv
);
2687 penv
= (CPUState
*)penv
->next_cpu
;
2691 static void tcg_init_vcpu(void *_env
)
2693 CPUState
*env
= _env
;
2694 /* share a single thread for all cpus with TCG */
2695 if (!tcg_cpu_thread
) {
2696 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2697 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2698 qemu_cond_init(env
->halt_cond
);
2699 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2700 while (env
->created
== 0)
2701 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2702 tcg_cpu_thread
= env
->thread
;
2703 tcg_halt_cond
= env
->halt_cond
;
2705 env
->thread
= tcg_cpu_thread
;
2706 env
->halt_cond
= tcg_halt_cond
;
2710 static void kvm_start_vcpu(CPUState
*env
)
2712 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2713 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2714 qemu_cond_init(env
->halt_cond
);
2715 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2716 while (env
->created
== 0)
2717 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2720 void qemu_init_vcpu(void *_env
)
2722 CPUState
*env
= _env
;
2724 env
->nr_cores
= smp_cores
;
2725 env
->nr_threads
= smp_threads
;
2727 kvm_start_vcpu(env
);
2732 void qemu_notify_event(void)
2734 qemu_event_increment();
2737 static void qemu_system_vmstop_request(int reason
)
2739 vmstop_requested
= reason
;
2740 qemu_notify_event();
2743 void vm_stop(int reason
)
2746 qemu_thread_self(&me
);
2748 if (!qemu_thread_equal(&me
, &io_thread
)) {
2749 qemu_system_vmstop_request(reason
);
2751 * FIXME: should not return to device code in case
2752 * vm_stop() has been requested.
2754 if (cpu_single_env
) {
2755 cpu_exit(cpu_single_env
);
2756 cpu_single_env
->stop
= 1;
2767 static void host_main_loop_wait(int *timeout
)
2773 /* XXX: need to suppress polling by better using win32 events */
2775 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2776 ret
|= pe
->func(pe
->opaque
);
2780 WaitObjects
*w
= &wait_objects
;
2782 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2783 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2784 if (w
->func
[ret
- WAIT_OBJECT_0
])
2785 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2787 /* Check for additional signaled events */
2788 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2790 /* Check if event is signaled */
2791 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2792 if(ret2
== WAIT_OBJECT_0
) {
2794 w
->func
[i
](w
->opaque
[i
]);
2795 } else if (ret2
== WAIT_TIMEOUT
) {
2797 err
= GetLastError();
2798 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2801 } else if (ret
== WAIT_TIMEOUT
) {
2803 err
= GetLastError();
2804 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2811 static void host_main_loop_wait(int *timeout
)
2816 void main_loop_wait(int nonblocking
)
2818 IOHandlerRecord
*ioh
;
2819 fd_set rfds
, wfds
, xfds
;
2827 timeout
= qemu_calculate_timeout();
2828 qemu_bh_update_timeout(&timeout
);
2831 host_main_loop_wait(&timeout
);
2833 /* poll any events */
2834 /* XXX: separate device handlers from system ones */
2839 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2843 (!ioh
->fd_read_poll
||
2844 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2845 FD_SET(ioh
->fd
, &rfds
);
2849 if (ioh
->fd_write
) {
2850 FD_SET(ioh
->fd
, &wfds
);
2856 tv
.tv_sec
= timeout
/ 1000;
2857 tv
.tv_usec
= (timeout
% 1000) * 1000;
2859 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2861 qemu_mutex_unlock_iothread();
2862 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2863 qemu_mutex_lock_iothread();
2865 IOHandlerRecord
*pioh
;
2867 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2869 QLIST_REMOVE(ioh
, next
);
2873 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2874 ioh
->fd_read(ioh
->opaque
);
2876 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2877 ioh
->fd_write(ioh
->opaque
);
2882 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2884 qemu_run_all_timers();
2886 /* Check bottom-halves last in case any of the earlier events triggered
2892 static int qemu_cpu_exec(CPUState
*env
)
2895 #ifdef CONFIG_PROFILER
2899 #ifdef CONFIG_PROFILER
2900 ti
= profile_getclock();
2905 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2906 env
->icount_decr
.u16
.low
= 0;
2907 env
->icount_extra
= 0;
2908 count
= qemu_icount_round (qemu_next_deadline());
2909 qemu_icount
+= count
;
2910 decr
= (count
> 0xffff) ? 0xffff : count
;
2912 env
->icount_decr
.u16
.low
= decr
;
2913 env
->icount_extra
= count
;
2915 ret
= cpu_exec(env
);
2916 #ifdef CONFIG_PROFILER
2917 qemu_time
+= profile_getclock() - ti
;
2920 /* Fold pending instructions back into the
2921 instruction counter, and clear the interrupt flag. */
2922 qemu_icount
-= (env
->icount_decr
.u16
.low
2923 + env
->icount_extra
);
2924 env
->icount_decr
.u32
= 0;
2925 env
->icount_extra
= 0;
2930 static bool tcg_cpu_exec(void)
2934 if (next_cpu
== NULL
)
2935 next_cpu
= first_cpu
;
2936 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2937 CPUState
*env
= cur_cpu
= next_cpu
;
2939 qemu_clock_enable(vm_clock
,
2940 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2942 if (qemu_alarm_pending())
2944 if (cpu_can_run(env
))
2945 ret
= qemu_cpu_exec(env
);
2949 if (ret
== EXCP_DEBUG
) {
2950 gdb_set_stop_cpu(env
);
2951 debug_requested
= 1;
2955 return tcg_has_work();
2958 static int vm_can_run(void)
2960 if (powerdown_requested
)
2962 if (reset_requested
)
2964 if (shutdown_requested
)
2966 if (debug_requested
)
2971 qemu_irq qemu_system_powerdown
;
2973 static void main_loop(void)
2977 #ifdef CONFIG_IOTHREAD
2978 qemu_system_ready
= 1;
2979 qemu_cond_broadcast(&qemu_system_cond
);
2984 bool nonblocking
= false;
2985 #ifdef CONFIG_PROFILER
2988 #ifndef CONFIG_IOTHREAD
2989 nonblocking
= tcg_cpu_exec();
2991 #ifdef CONFIG_PROFILER
2992 ti
= profile_getclock();
2994 main_loop_wait(nonblocking
);
2995 #ifdef CONFIG_PROFILER
2996 dev_time
+= profile_getclock() - ti
;
2998 } while (vm_can_run());
3000 if (qemu_debug_requested()) {
3001 vm_stop(EXCP_DEBUG
);
3003 if (qemu_shutdown_requested()) {
3004 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3011 if (qemu_reset_requested()) {
3013 qemu_system_reset();
3016 if (qemu_powerdown_requested()) {
3017 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3018 qemu_irq_raise(qemu_system_powerdown
);
3020 if ((r
= qemu_vmstop_requested())) {
3027 static void version(void)
3029 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3032 static void help(int exitcode
)
3034 const char *options_help
=
3035 #define DEF(option, opt_arg, opt_enum, opt_help) \
3037 #define DEFHEADING(text) stringify(text) "\n"
3038 #include "qemu-options.h"
3044 printf("usage: %s [options] [disk_image]\n"
3046 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3049 "During emulation, the following keys are useful:\n"
3050 "ctrl-alt-f toggle full screen\n"
3051 "ctrl-alt-n switch to virtual console 'n'\n"
3052 "ctrl-alt toggle mouse and keyboard grab\n"
3054 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3060 #define HAS_ARG 0x0001
3063 #define DEF(option, opt_arg, opt_enum, opt_help) \
3065 #define DEFHEADING(text)
3066 #include "qemu-options.h"
3072 typedef struct QEMUOption
{
3078 static const QEMUOption qemu_options
[] = {
3079 { "h", 0, QEMU_OPTION_h
},
3080 #define DEF(option, opt_arg, opt_enum, opt_help) \
3081 { option, opt_arg, opt_enum },
3082 #define DEFHEADING(text)
3083 #include "qemu-options.h"
3091 struct soundhw soundhw
[] = {
3092 #ifdef HAS_AUDIO_CHOICE
3093 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3099 { .init_isa
= pcspk_audio_init
}
3106 "Creative Sound Blaster 16",
3109 { .init_isa
= SB16_init
}
3113 #ifdef CONFIG_CS4231A
3119 { .init_isa
= cs4231a_init
}
3127 "Yamaha YMF262 (OPL3)",
3129 "Yamaha YM3812 (OPL2)",
3133 { .init_isa
= Adlib_init
}
3140 "Gravis Ultrasound GF1",
3143 { .init_isa
= GUS_init
}
3150 "Intel 82801AA AC97 Audio",
3153 { .init_pci
= ac97_init
}
3157 #ifdef CONFIG_ES1370
3160 "ENSONIQ AudioPCI ES1370",
3163 { .init_pci
= es1370_init
}
3167 #endif /* HAS_AUDIO_CHOICE */
3169 { NULL
, NULL
, 0, 0, { NULL
} }
3172 static void select_soundhw (const char *optarg
)
3176 if (*optarg
== '?') {
3179 printf ("Valid sound card names (comma separated):\n");
3180 for (c
= soundhw
; c
->name
; ++c
) {
3181 printf ("%-11s %s\n", c
->name
, c
->descr
);
3183 printf ("\n-soundhw all will enable all of the above\n");
3184 exit (*optarg
!= '?');
3192 if (!strcmp (optarg
, "all")) {
3193 for (c
= soundhw
; c
->name
; ++c
) {
3201 e
= strchr (p
, ',');
3202 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3204 for (c
= soundhw
; c
->name
; ++c
) {
3205 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3214 "Unknown sound card name (too big to show)\n");
3217 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3222 p
+= l
+ (e
!= NULL
);
3226 goto show_valid_cards
;
3231 static void select_vgahw (const char *p
)
3236 vga_interface_type
= VGA_NONE
;
3237 if (strstart(p
, "std", &opts
)) {
3238 vga_interface_type
= VGA_STD
;
3239 } else if (strstart(p
, "cirrus", &opts
)) {
3240 vga_interface_type
= VGA_CIRRUS
;
3241 } else if (strstart(p
, "vmware", &opts
)) {
3242 vga_interface_type
= VGA_VMWARE
;
3243 } else if (strstart(p
, "xenfb", &opts
)) {
3244 vga_interface_type
= VGA_XENFB
;
3245 } else if (!strstart(p
, "none", &opts
)) {
3247 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3251 const char *nextopt
;
3253 if (strstart(opts
, ",retrace=", &nextopt
)) {
3255 if (strstart(opts
, "dumb", &nextopt
))
3256 vga_retrace_method
= VGA_RETRACE_DUMB
;
3257 else if (strstart(opts
, "precise", &nextopt
))
3258 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3259 else goto invalid_vga
;
3260 } else goto invalid_vga
;
3266 static int balloon_parse(const char *arg
)
3270 if (strcmp(arg
, "none") == 0) {
3274 if (!strncmp(arg
, "virtio", 6)) {
3275 if (arg
[6] == ',') {
3276 /* have params -> parse them */
3277 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3281 /* create empty opts */
3282 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3284 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3293 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3295 exit(STATUS_CONTROL_C_EXIT
);
3300 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3304 if(strlen(str
) != 36)
3307 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3308 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3309 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3315 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3323 static void termsig_handler(int signal
)
3325 qemu_system_shutdown_request();
3328 static void sigchld_handler(int signal
)
3330 waitpid(-1, NULL
, WNOHANG
);
3333 static void sighandler_setup(void)
3335 struct sigaction act
;
3337 memset(&act
, 0, sizeof(act
));
3338 act
.sa_handler
= termsig_handler
;
3339 sigaction(SIGINT
, &act
, NULL
);
3340 sigaction(SIGHUP
, &act
, NULL
);
3341 sigaction(SIGTERM
, &act
, NULL
);
3343 act
.sa_handler
= sigchld_handler
;
3344 act
.sa_flags
= SA_NOCLDSTOP
;
3345 sigaction(SIGCHLD
, &act
, NULL
);
3351 /* Look for support files in the same directory as the executable. */
3352 static char *find_datadir(const char *argv0
)
3358 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3365 while (p
!= buf
&& *p
!= '\\')
3368 if (access(buf
, R_OK
) == 0) {
3369 return qemu_strdup(buf
);
3375 /* Find a likely location for support files using the location of the binary.
3376 For installed binaries this will be "$bindir/../share/qemu". When
3377 running from the build tree this will be "$bindir/../pc-bios". */
3378 #define SHARE_SUFFIX "/share/qemu"
3379 #define BUILD_SUFFIX "/pc-bios"
3380 static char *find_datadir(const char *argv0
)
3388 #if defined(__linux__)
3391 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3397 #elif defined(__FreeBSD__)
3400 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3407 /* If we don't have any way of figuring out the actual executable
3408 location then try argv[0]. */
3410 p
= realpath(argv0
, buf
);
3418 max_len
= strlen(dir
) +
3419 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3420 res
= qemu_mallocz(max_len
);
3421 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3422 if (access(res
, R_OK
)) {
3423 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3424 if (access(res
, R_OK
)) {
3436 char *qemu_find_file(int type
, const char *name
)
3442 /* If name contains path separators then try it as a straight path. */
3443 if ((strchr(name
, '/') || strchr(name
, '\\'))
3444 && access(name
, R_OK
) == 0) {
3445 return qemu_strdup(name
);
3448 case QEMU_FILE_TYPE_BIOS
:
3451 case QEMU_FILE_TYPE_KEYMAP
:
3452 subdir
= "keymaps/";
3457 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3458 buf
= qemu_mallocz(len
);
3459 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3460 if (access(buf
, R_OK
)) {
3467 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3469 return qdev_device_help(opts
);
3472 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3476 dev
= qdev_device_add(opts
);
3482 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3484 CharDriverState
*chr
;
3486 chr
= qemu_chr_open_opts(opts
, NULL
);
3492 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3494 CharDriverState
*chr
;
3495 const char *chardev
;
3499 mode
= qemu_opt_get(opts
, "mode");
3503 if (strcmp(mode
, "readline") == 0) {
3504 flags
= MONITOR_USE_READLINE
;
3505 } else if (strcmp(mode
, "control") == 0) {
3506 flags
= MONITOR_USE_CONTROL
;
3508 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3512 if (qemu_opt_get_bool(opts
, "default", 0))
3513 flags
|= MONITOR_IS_DEFAULT
;
3515 chardev
= qemu_opt_get(opts
, "chardev");
3516 chr
= qemu_chr_find(chardev
);
3518 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3522 monitor_init(chr
, flags
);
3526 static void monitor_parse(const char *optarg
, const char *mode
)
3528 static int monitor_device_index
= 0;
3534 if (strstart(optarg
, "chardev:", &p
)) {
3535 snprintf(label
, sizeof(label
), "%s", p
);
3537 if (monitor_device_index
) {
3538 snprintf(label
, sizeof(label
), "monitor%d",
3539 monitor_device_index
);
3541 snprintf(label
, sizeof(label
), "monitor");
3544 opts
= qemu_chr_parse_compat(label
, optarg
);
3546 fprintf(stderr
, "parse error: %s\n", optarg
);
3551 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3553 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3556 qemu_opt_set(opts
, "mode", mode
);
3557 qemu_opt_set(opts
, "chardev", label
);
3559 qemu_opt_set(opts
, "default", "on");
3560 monitor_device_index
++;
3563 struct device_config
{
3565 DEV_USB
, /* -usbdevice */
3567 DEV_SERIAL
, /* -serial */
3568 DEV_PARALLEL
, /* -parallel */
3569 DEV_VIRTCON
, /* -virtioconsole */
3570 DEV_DEBUGCON
, /* -debugcon */
3572 const char *cmdline
;
3573 QTAILQ_ENTRY(device_config
) next
;
3575 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3577 static void add_device_config(int type
, const char *cmdline
)
3579 struct device_config
*conf
;
3581 conf
= qemu_mallocz(sizeof(*conf
));
3583 conf
->cmdline
= cmdline
;
3584 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3587 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3589 struct device_config
*conf
;
3592 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3593 if (conf
->type
!= type
)
3595 rc
= func(conf
->cmdline
);
3602 static int serial_parse(const char *devname
)
3604 static int index
= 0;
3607 if (strcmp(devname
, "none") == 0)
3609 if (index
== MAX_SERIAL_PORTS
) {
3610 fprintf(stderr
, "qemu: too many serial ports\n");
3613 snprintf(label
, sizeof(label
), "serial%d", index
);
3614 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3615 if (!serial_hds
[index
]) {
3616 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3617 devname
, strerror(errno
));
3624 static int parallel_parse(const char *devname
)
3626 static int index
= 0;
3629 if (strcmp(devname
, "none") == 0)
3631 if (index
== MAX_PARALLEL_PORTS
) {
3632 fprintf(stderr
, "qemu: too many parallel ports\n");
3635 snprintf(label
, sizeof(label
), "parallel%d", index
);
3636 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3637 if (!parallel_hds
[index
]) {
3638 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3639 devname
, strerror(errno
));
3646 static int virtcon_parse(const char *devname
)
3648 static int index
= 0;
3650 QemuOpts
*bus_opts
, *dev_opts
;
3652 if (strcmp(devname
, "none") == 0)
3654 if (index
== MAX_VIRTIO_CONSOLES
) {
3655 fprintf(stderr
, "qemu: too many virtio consoles\n");
3659 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3660 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3662 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3663 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3665 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3666 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3667 if (!virtcon_hds
[index
]) {
3668 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3669 devname
, strerror(errno
));
3672 qemu_opt_set(dev_opts
, "chardev", label
);
3678 static int debugcon_parse(const char *devname
)
3682 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3685 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3687 fprintf(stderr
, "qemu: already have a debugcon device\n");
3690 qemu_opt_set(opts
, "driver", "isa-debugcon");
3691 qemu_opt_set(opts
, "chardev", "debugcon");
3695 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3696 const char **poptarg
, int *poptind
)
3698 const QEMUOption
*popt
;
3699 int optind
= *poptind
;
3700 char *r
= argv
[optind
];
3703 loc_set_cmdline(argv
, optind
, 1);
3705 /* Treat --foo the same as -foo. */
3708 popt
= qemu_options
;
3711 error_report("invalid option");
3714 if (!strcmp(popt
->name
, r
+ 1))
3718 if (popt
->flags
& HAS_ARG
) {
3719 if (optind
>= argc
) {
3720 error_report("requires an argument");
3723 optarg
= argv
[optind
++];
3724 loc_set_cmdline(argv
, optind
- 2, 2);
3735 int main(int argc
, char **argv
, char **envp
)
3737 const char *gdbstub_dev
= NULL
;
3738 uint32_t boot_devices_bitmap
= 0;
3740 int snapshot
, linux_boot
, net_boot
;
3741 const char *icount_option
= NULL
;
3742 const char *initrd_filename
;
3743 const char *kernel_filename
, *kernel_cmdline
;
3744 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3746 DisplayChangeListener
*dcl
;
3747 int cyls
, heads
, secs
, translation
;
3748 QemuOpts
*hda_opts
= NULL
, *opts
;
3751 const char *loadvm
= NULL
;
3752 QEMUMachine
*machine
;
3753 const char *cpu_model
;
3758 const char *pid_file
= NULL
;
3759 const char *incoming
= NULL
;
3762 struct passwd
*pwd
= NULL
;
3763 const char *chroot_dir
= NULL
;
3764 const char *run_as
= NULL
;
3767 int show_vnc_port
= 0;
3770 error_set_progname(argv
[0]);
3772 exit_notifier_init();
3776 qemu_cache_utils_init(envp
);
3778 QLIST_INIT (&vm_change_state_head
);
3781 struct sigaction act
;
3782 sigfillset(&act
.sa_mask
);
3784 act
.sa_handler
= SIG_IGN
;
3785 sigaction(SIGPIPE
, &act
, NULL
);
3788 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3789 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3790 QEMU to run on a single CPU */
3795 h
= GetCurrentProcess();
3796 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3797 for(i
= 0; i
< 32; i
++) {
3798 if (mask
& (1 << i
))
3803 SetProcessAffinityMask(h
, mask
);
3809 module_call_init(MODULE_INIT_MACHINE
);
3810 machine
= find_default_machine();
3812 initrd_filename
= NULL
;
3815 kernel_filename
= NULL
;
3816 kernel_cmdline
= "";
3817 cyls
= heads
= secs
= 0;
3818 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3820 for (i
= 0; i
< MAX_NODES
; i
++) {
3822 node_cpumask
[i
] = 0;
3831 /* first pass of option parsing */
3833 while (optind
< argc
) {
3834 if (argv
[optind
][0] != '-') {
3839 const QEMUOption
*popt
;
3841 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3842 switch (popt
->index
) {
3843 case QEMU_OPTION_nodefconfig
:
3854 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3855 fp
= fopen(fname
, "r");
3857 if (qemu_config_parse(fp
, fname
) != 0) {
3863 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3864 fp
= fopen(fname
, "r");
3866 if (qemu_config_parse(fp
, fname
) != 0) {
3872 #if defined(cpudef_setup)
3873 cpudef_setup(); /* parse cpu definitions in target config file */
3876 /* second pass of option parsing */
3881 if (argv
[optind
][0] != '-') {
3882 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3884 const QEMUOption
*popt
;
3886 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3887 switch(popt
->index
) {
3889 machine
= find_machine(optarg
);
3892 printf("Supported machines are:\n");
3893 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3895 printf("%-10s %s (alias of %s)\n",
3896 m
->alias
, m
->desc
, m
->name
);
3897 printf("%-10s %s%s\n",
3899 m
->is_default
? " (default)" : "");
3901 exit(*optarg
!= '?');
3904 case QEMU_OPTION_cpu
:
3905 /* hw initialization will check this */
3906 if (*optarg
== '?') {
3907 /* XXX: implement xxx_cpu_list for targets that still miss it */
3908 #if defined(cpu_list_id)
3909 cpu_list_id(stdout
, &fprintf
, optarg
);
3910 #elif defined(cpu_list)
3911 cpu_list(stdout
, &fprintf
); /* deprecated */
3918 case QEMU_OPTION_initrd
:
3919 initrd_filename
= optarg
;
3921 case QEMU_OPTION_hda
:
3923 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3925 hda_opts
= drive_add(optarg
, HD_ALIAS
3926 ",cyls=%d,heads=%d,secs=%d%s",
3927 0, cyls
, heads
, secs
,
3928 translation
== BIOS_ATA_TRANSLATION_LBA
?
3930 translation
== BIOS_ATA_TRANSLATION_NONE
?
3931 ",trans=none" : "");
3933 case QEMU_OPTION_hdb
:
3934 case QEMU_OPTION_hdc
:
3935 case QEMU_OPTION_hdd
:
3936 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3938 case QEMU_OPTION_drive
:
3939 drive_add(NULL
, "%s", optarg
);
3941 case QEMU_OPTION_set
:
3942 if (qemu_set_option(optarg
) != 0)
3945 case QEMU_OPTION_global
:
3946 if (qemu_global_option(optarg
) != 0)
3949 case QEMU_OPTION_mtdblock
:
3950 drive_add(optarg
, MTD_ALIAS
);
3952 case QEMU_OPTION_sd
:
3953 drive_add(optarg
, SD_ALIAS
);
3955 case QEMU_OPTION_pflash
:
3956 drive_add(optarg
, PFLASH_ALIAS
);
3958 case QEMU_OPTION_snapshot
:
3961 case QEMU_OPTION_hdachs
:
3965 cyls
= strtol(p
, (char **)&p
, 0);
3966 if (cyls
< 1 || cyls
> 16383)
3971 heads
= strtol(p
, (char **)&p
, 0);
3972 if (heads
< 1 || heads
> 16)
3977 secs
= strtol(p
, (char **)&p
, 0);
3978 if (secs
< 1 || secs
> 63)
3982 if (!strcmp(p
, "none"))
3983 translation
= BIOS_ATA_TRANSLATION_NONE
;
3984 else if (!strcmp(p
, "lba"))
3985 translation
= BIOS_ATA_TRANSLATION_LBA
;
3986 else if (!strcmp(p
, "auto"))
3987 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3990 } else if (*p
!= '\0') {
3992 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3995 if (hda_opts
!= NULL
) {
3997 snprintf(num
, sizeof(num
), "%d", cyls
);
3998 qemu_opt_set(hda_opts
, "cyls", num
);
3999 snprintf(num
, sizeof(num
), "%d", heads
);
4000 qemu_opt_set(hda_opts
, "heads", num
);
4001 snprintf(num
, sizeof(num
), "%d", secs
);
4002 qemu_opt_set(hda_opts
, "secs", num
);
4003 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4004 qemu_opt_set(hda_opts
, "trans", "lba");
4005 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4006 qemu_opt_set(hda_opts
, "trans", "none");
4010 case QEMU_OPTION_numa
:
4011 if (nb_numa_nodes
>= MAX_NODES
) {
4012 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4017 case QEMU_OPTION_nographic
:
4018 display_type
= DT_NOGRAPHIC
;
4020 #ifdef CONFIG_CURSES
4021 case QEMU_OPTION_curses
:
4022 display_type
= DT_CURSES
;
4025 case QEMU_OPTION_portrait
:
4028 case QEMU_OPTION_kernel
:
4029 kernel_filename
= optarg
;
4031 case QEMU_OPTION_append
:
4032 kernel_cmdline
= optarg
;
4034 case QEMU_OPTION_cdrom
:
4035 drive_add(optarg
, CDROM_ALIAS
);
4037 case QEMU_OPTION_boot
:
4039 static const char * const params
[] = {
4040 "order", "once", "menu", NULL
4042 char buf
[sizeof(boot_devices
)];
4043 char *standard_boot_devices
;
4046 if (!strchr(optarg
, '=')) {
4048 pstrcpy(buf
, sizeof(buf
), optarg
);
4049 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4051 "qemu: unknown boot parameter '%s' in '%s'\n",
4057 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4058 boot_devices_bitmap
= parse_bootdevices(buf
);
4059 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4062 if (get_param_value(buf
, sizeof(buf
),
4064 boot_devices_bitmap
|= parse_bootdevices(buf
);
4065 standard_boot_devices
= qemu_strdup(boot_devices
);
4066 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4067 qemu_register_reset(restore_boot_devices
,
4068 standard_boot_devices
);
4070 if (get_param_value(buf
, sizeof(buf
),
4072 if (!strcmp(buf
, "on")) {
4074 } else if (!strcmp(buf
, "off")) {
4078 "qemu: invalid option value '%s'\n",
4086 case QEMU_OPTION_fda
:
4087 case QEMU_OPTION_fdb
:
4088 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4091 case QEMU_OPTION_no_fd_bootchk
:
4095 case QEMU_OPTION_netdev
:
4096 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4100 case QEMU_OPTION_net
:
4101 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4106 case QEMU_OPTION_tftp
:
4107 legacy_tftp_prefix
= optarg
;
4109 case QEMU_OPTION_bootp
:
4110 legacy_bootp_filename
= optarg
;
4113 case QEMU_OPTION_smb
:
4114 if (net_slirp_smb(optarg
) < 0)
4118 case QEMU_OPTION_redir
:
4119 if (net_slirp_redir(optarg
) < 0)
4123 case QEMU_OPTION_bt
:
4124 add_device_config(DEV_BT
, optarg
);
4127 case QEMU_OPTION_audio_help
:
4131 case QEMU_OPTION_soundhw
:
4132 select_soundhw (optarg
);
4138 case QEMU_OPTION_version
:
4142 case QEMU_OPTION_m
: {
4146 value
= strtoul(optarg
, &ptr
, 10);
4148 case 0: case 'M': case 'm':
4155 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4159 /* On 32-bit hosts, QEMU is limited by virtual address space */
4160 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4161 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4164 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4165 fprintf(stderr
, "qemu: ram size too large\n");
4171 case QEMU_OPTION_mempath
:
4175 case QEMU_OPTION_mem_prealloc
:
4182 const CPULogItem
*item
;
4184 mask
= cpu_str_to_log_mask(optarg
);
4186 printf("Log items (comma separated):\n");
4187 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4188 printf("%-10s %s\n", item
->name
, item
->help
);
4196 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4198 case QEMU_OPTION_gdb
:
4199 gdbstub_dev
= optarg
;
4204 case QEMU_OPTION_bios
:
4207 case QEMU_OPTION_singlestep
:
4214 keyboard_layout
= optarg
;
4216 case QEMU_OPTION_localtime
:
4219 case QEMU_OPTION_vga
:
4220 select_vgahw (optarg
);
4222 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4228 w
= strtol(p
, (char **)&p
, 10);
4231 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4237 h
= strtol(p
, (char **)&p
, 10);
4242 depth
= strtol(p
, (char **)&p
, 10);
4243 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4244 depth
!= 24 && depth
!= 32)
4246 } else if (*p
== '\0') {
4247 depth
= graphic_depth
;
4254 graphic_depth
= depth
;
4258 case QEMU_OPTION_echr
:
4261 term_escape_char
= strtol(optarg
, &r
, 0);
4263 printf("Bad argument to echr\n");
4266 case QEMU_OPTION_monitor
:
4267 monitor_parse(optarg
, "readline");
4268 default_monitor
= 0;
4270 case QEMU_OPTION_qmp
:
4271 monitor_parse(optarg
, "control");
4272 default_monitor
= 0;
4274 case QEMU_OPTION_mon
:
4275 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4277 fprintf(stderr
, "parse error: %s\n", optarg
);
4280 default_monitor
= 0;
4282 case QEMU_OPTION_chardev
:
4283 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4285 fprintf(stderr
, "parse error: %s\n", optarg
);
4289 case QEMU_OPTION_serial
:
4290 add_device_config(DEV_SERIAL
, optarg
);
4292 if (strncmp(optarg
, "mon:", 4) == 0) {
4293 default_monitor
= 0;
4296 case QEMU_OPTION_watchdog
:
4299 "qemu: only one watchdog option may be given\n");
4304 case QEMU_OPTION_watchdog_action
:
4305 if (select_watchdog_action(optarg
) == -1) {
4306 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4310 case QEMU_OPTION_virtiocon
:
4311 add_device_config(DEV_VIRTCON
, optarg
);
4312 default_virtcon
= 0;
4313 if (strncmp(optarg
, "mon:", 4) == 0) {
4314 default_monitor
= 0;
4317 case QEMU_OPTION_parallel
:
4318 add_device_config(DEV_PARALLEL
, optarg
);
4319 default_parallel
= 0;
4320 if (strncmp(optarg
, "mon:", 4) == 0) {
4321 default_monitor
= 0;
4324 case QEMU_OPTION_debugcon
:
4325 add_device_config(DEV_DEBUGCON
, optarg
);
4327 case QEMU_OPTION_loadvm
:
4330 case QEMU_OPTION_full_screen
:
4334 case QEMU_OPTION_no_frame
:
4337 case QEMU_OPTION_alt_grab
:
4340 case QEMU_OPTION_ctrl_grab
:
4343 case QEMU_OPTION_no_quit
:
4346 case QEMU_OPTION_sdl
:
4347 display_type
= DT_SDL
;
4350 case QEMU_OPTION_pidfile
:
4354 case QEMU_OPTION_win2k_hack
:
4355 win2k_install_hack
= 1;
4357 case QEMU_OPTION_rtc_td_hack
:
4360 case QEMU_OPTION_acpitable
:
4361 if(acpi_table_add(optarg
) < 0) {
4362 fprintf(stderr
, "Wrong acpi table provided\n");
4366 case QEMU_OPTION_smbios
:
4367 if(smbios_entry_add(optarg
) < 0) {
4368 fprintf(stderr
, "Wrong smbios provided\n");
4374 case QEMU_OPTION_enable_kvm
:
4378 case QEMU_OPTION_usb
:
4381 case QEMU_OPTION_usbdevice
:
4383 add_device_config(DEV_USB
, optarg
);
4385 case QEMU_OPTION_device
:
4386 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4390 case QEMU_OPTION_smp
:
4393 fprintf(stderr
, "Invalid number of CPUs\n");
4396 if (max_cpus
< smp_cpus
) {
4397 fprintf(stderr
, "maxcpus must be equal to or greater than "
4401 if (max_cpus
> 255) {
4402 fprintf(stderr
, "Unsupported number of maxcpus\n");
4406 case QEMU_OPTION_vnc
:
4407 display_type
= DT_VNC
;
4408 vnc_display
= optarg
;
4411 case QEMU_OPTION_no_acpi
:
4414 case QEMU_OPTION_no_hpet
:
4417 case QEMU_OPTION_balloon
:
4418 if (balloon_parse(optarg
) < 0) {
4419 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4424 case QEMU_OPTION_no_reboot
:
4427 case QEMU_OPTION_no_shutdown
:
4430 case QEMU_OPTION_show_cursor
:
4433 case QEMU_OPTION_uuid
:
4434 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4435 fprintf(stderr
, "Fail to parse UUID string."
4436 " Wrong format.\n");
4441 case QEMU_OPTION_daemonize
:
4445 case QEMU_OPTION_option_rom
:
4446 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4447 fprintf(stderr
, "Too many option ROMs\n");
4450 option_rom
[nb_option_roms
] = optarg
;
4453 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4454 case QEMU_OPTION_semihosting
:
4455 semihosting_enabled
= 1;
4458 case QEMU_OPTION_name
:
4459 qemu_name
= qemu_strdup(optarg
);
4461 char *p
= strchr(qemu_name
, ',');
4464 if (strncmp(p
, "process=", 8)) {
4465 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4473 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4474 case QEMU_OPTION_prom_env
:
4475 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4476 fprintf(stderr
, "Too many prom variables\n");
4479 prom_envs
[nb_prom_envs
] = optarg
;
4484 case QEMU_OPTION_old_param
:
4488 case QEMU_OPTION_clock
:
4489 configure_alarms(optarg
);
4491 case QEMU_OPTION_startdate
:
4492 configure_rtc_date_offset(optarg
, 1);
4494 case QEMU_OPTION_rtc
:
4495 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4497 fprintf(stderr
, "parse error: %s\n", optarg
);
4500 configure_rtc(opts
);
4502 case QEMU_OPTION_tb_size
:
4503 tb_size
= strtol(optarg
, NULL
, 0);
4507 case QEMU_OPTION_icount
:
4508 icount_option
= optarg
;
4510 case QEMU_OPTION_incoming
:
4513 case QEMU_OPTION_nodefaults
:
4515 default_parallel
= 0;
4516 default_virtcon
= 0;
4517 default_monitor
= 0;
4525 case QEMU_OPTION_chroot
:
4526 chroot_dir
= optarg
;
4528 case QEMU_OPTION_runas
:
4533 case QEMU_OPTION_xen_domid
:
4534 xen_domid
= atoi(optarg
);
4536 case QEMU_OPTION_xen_create
:
4537 xen_mode
= XEN_CREATE
;
4539 case QEMU_OPTION_xen_attach
:
4540 xen_mode
= XEN_ATTACH
;
4543 case QEMU_OPTION_readconfig
:
4546 fp
= fopen(optarg
, "r");
4548 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4551 if (qemu_config_parse(fp
, optarg
) != 0) {
4557 case QEMU_OPTION_writeconfig
:
4560 if (strcmp(optarg
, "-") == 0) {
4563 fp
= fopen(optarg
, "w");
4565 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4569 qemu_config_write(fp
);
4578 /* If no data_dir is specified then try to find it relative to the
4581 data_dir
= find_datadir(argv
[0]);
4583 /* If all else fails use the install patch specified when building. */
4585 data_dir
= CONFIG_QEMU_SHAREDIR
;
4589 * Default to max_cpus = smp_cpus, in case the user doesn't
4590 * specify a max_cpus value.
4593 max_cpus
= smp_cpus
;
4595 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4596 if (smp_cpus
> machine
->max_cpus
) {
4597 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4598 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4603 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4604 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4606 if (machine
->no_serial
) {
4609 if (machine
->no_parallel
) {
4610 default_parallel
= 0;
4612 if (!machine
->use_virtcon
) {
4613 default_virtcon
= 0;
4615 if (machine
->no_vga
) {
4618 if (machine
->no_floppy
) {
4621 if (machine
->no_cdrom
) {
4624 if (machine
->no_sdcard
) {
4628 if (display_type
== DT_NOGRAPHIC
) {
4629 if (default_parallel
)
4630 add_device_config(DEV_PARALLEL
, "null");
4631 if (default_serial
&& default_monitor
) {
4632 add_device_config(DEV_SERIAL
, "mon:stdio");
4633 } else if (default_virtcon
&& default_monitor
) {
4634 add_device_config(DEV_VIRTCON
, "mon:stdio");
4637 add_device_config(DEV_SERIAL
, "stdio");
4638 if (default_virtcon
)
4639 add_device_config(DEV_VIRTCON
, "stdio");
4640 if (default_monitor
)
4641 monitor_parse("stdio", "readline");
4645 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4646 if (default_parallel
)
4647 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4648 if (default_monitor
)
4649 monitor_parse("vc:80Cx24C", "readline");
4650 if (default_virtcon
)
4651 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4654 vga_interface_type
= VGA_CIRRUS
;
4656 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4663 if (pipe(fds
) == -1)
4674 len
= read(fds
[0], &status
, 1);
4675 if (len
== -1 && (errno
== EINTR
))
4680 else if (status
== 1) {
4681 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4689 qemu_set_cloexec(fds
[1]);
4701 signal(SIGTSTP
, SIG_IGN
);
4702 signal(SIGTTOU
, SIG_IGN
);
4703 signal(SIGTTIN
, SIG_IGN
);
4707 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4711 if (write(fds
[1], &status
, 1) != 1) {
4712 perror("daemonize. Writing to pipe\n");
4716 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4720 if (kvm_enabled()) {
4723 ret
= kvm_init(smp_cpus
);
4725 fprintf(stderr
, "failed to initialize KVM\n");
4730 if (qemu_init_main_loop()) {
4731 fprintf(stderr
, "qemu_init_main_loop failed\n");
4734 linux_boot
= (kernel_filename
!= NULL
);
4736 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4737 fprintf(stderr
, "-append only allowed with -kernel option\n");
4741 if (!linux_boot
&& initrd_filename
!= NULL
) {
4742 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4747 /* Win32 doesn't support line-buffering and requires size >= 2 */
4748 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4751 if (init_timer_alarm() < 0) {
4752 fprintf(stderr
, "could not initialize alarm timer\n");
4755 configure_icount(icount_option
);
4761 if (net_init_clients() < 0) {
4765 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4766 net_set_boot_mask(net_boot
);
4768 /* init the bluetooth world */
4769 if (foreach_device_config(DEV_BT
, bt_parse
))
4772 /* init the memory */
4774 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4776 /* init the dynamic translator */
4777 cpu_exec_init_all(tb_size
* 1024 * 1024);
4779 bdrv_init_with_whitelist();
4783 if (default_cdrom
) {
4784 /* we always create the cdrom drive, even if no disk is there */
4785 drive_add(NULL
, CDROM_ALIAS
);
4788 if (default_floppy
) {
4789 /* we always create at least one floppy */
4790 drive_add(NULL
, FD_ALIAS
, 0);
4793 if (default_sdcard
) {
4794 /* we always create one sd slot, even if no card is in it */
4795 drive_add(NULL
, SD_ALIAS
);
4798 /* open the virtual block devices */
4800 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4801 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4804 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4807 if (nb_numa_nodes
> 0) {
4810 if (nb_numa_nodes
> smp_cpus
) {
4811 nb_numa_nodes
= smp_cpus
;
4814 /* If no memory size if given for any node, assume the default case
4815 * and distribute the available memory equally across all nodes
4817 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4818 if (node_mem
[i
] != 0)
4821 if (i
== nb_numa_nodes
) {
4822 uint64_t usedmem
= 0;
4824 /* On Linux, the each node's border has to be 8MB aligned,
4825 * the final node gets the rest.
4827 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4828 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4829 usedmem
+= node_mem
[i
];
4831 node_mem
[i
] = ram_size
- usedmem
;
4834 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4835 if (node_cpumask
[i
] != 0)
4838 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4839 * must cope with this anyway, because there are BIOSes out there in
4840 * real machines which also use this scheme.
4842 if (i
== nb_numa_nodes
) {
4843 for (i
= 0; i
< smp_cpus
; i
++) {
4844 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4849 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4851 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4853 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4855 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4858 module_call_init(MODULE_INIT_DEVICE
);
4860 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4864 i
= select_watchdog(watchdog
);
4866 exit (i
== 1 ? 1 : 0);
4869 if (machine
->compat_props
) {
4870 qdev_prop_register_global_list(machine
->compat_props
);
4874 machine
->init(ram_size
, boot_devices
,
4875 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4877 cpu_synchronize_all_post_init();
4880 /* must be after terminal init, SDL library changes signal handlers */
4884 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4885 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4886 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4892 current_machine
= machine
;
4894 /* init USB devices */
4896 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4900 /* init generic devices */
4901 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4904 net_check_clients();
4906 /* just use the first displaystate for the moment */
4907 ds
= get_displaystate();
4909 if (display_type
== DT_DEFAULT
) {
4910 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4911 display_type
= DT_SDL
;
4913 display_type
= DT_VNC
;
4914 vnc_display
= "localhost:0,to=99";
4920 switch (display_type
) {
4923 #if defined(CONFIG_CURSES)
4925 curses_display_init(ds
, full_screen
);
4928 #if defined(CONFIG_SDL)
4930 sdl_display_init(ds
, full_screen
, no_frame
);
4932 #elif defined(CONFIG_COCOA)
4934 cocoa_display_init(ds
, full_screen
);
4938 vnc_display_init(ds
);
4939 if (vnc_display_open(ds
, vnc_display
) < 0)
4942 if (show_vnc_port
) {
4943 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4951 dcl
= ds
->listeners
;
4952 while (dcl
!= NULL
) {
4953 if (dcl
->dpy_refresh
!= NULL
) {
4954 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
4955 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
4960 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
4961 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
4962 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
4965 text_consoles_set_display(ds
);
4967 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
4970 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
4971 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
4976 qdev_machine_creation_done();
4978 if (rom_load_all() != 0) {
4979 fprintf(stderr
, "rom loading failed\n");
4983 qemu_system_reset();
4985 if (load_vmstate(loadvm
) < 0) {
4991 qemu_start_incoming_migration(incoming
);
4992 } else if (autostart
) {
5002 len
= write(fds
[1], &status
, 1);
5003 if (len
== -1 && (errno
== EINTR
))
5010 perror("not able to chdir to /");
5013 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5019 pwd
= getpwnam(run_as
);
5021 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5027 if (chroot(chroot_dir
) < 0) {
5028 fprintf(stderr
, "chroot failed\n");
5032 perror("not able to chdir to /");
5038 if (setgid(pwd
->pw_gid
) < 0) {
5039 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5042 if (setuid(pwd
->pw_uid
) < 0) {
5043 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5046 if (setuid(0) != -1) {
5047 fprintf(stderr
, "Dropping privileges failed\n");