4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <sys/prctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
81 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83 extern int madvise(caddr_t
, size_t, int);
88 #if defined(__OpenBSD__)
92 #if defined(CONFIG_VDE)
93 #include <libvdeplug.h>
96 #if defined(CONFIG_UUID)
97 #include <uuid/uuid.h>
105 #if defined(__APPLE__) || defined(main)
107 int qemu_main(int argc
, char **argv
, char **envp
);
108 int main(int argc
, char **argv
)
110 return qemu_main(argc
, argv
, NULL
);
113 #define main qemu_main
115 #endif /* CONFIG_SDL */
119 #define main qemu_main
120 #endif /* CONFIG_COCOA */
123 #include "hw/boards.h"
125 #include "hw/pcmcia.h"
127 #include "hw/audiodev.h"
131 #include "hw/watchdog.h"
132 #include "hw/smbios.h"
135 #include "hw/loader.h"
138 #include "net/slirp.h"
143 #include "qemu-timer.h"
144 #include "qemu-char.h"
145 #include "cache-utils.h"
147 #include "block_int.h"
148 #include "block-migration.h"
150 #include "audio/audio.h"
151 #include "migration.h"
154 #include "qemu-option.h"
155 #include "qemu-config.h"
156 #include "qemu-objects.h"
157 #include "hw/device-assignment.h"
162 #include "exec-all.h"
164 #include "qemu_socket.h"
166 #include "slirp/libslirp.h"
168 #include "qemu-queue.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir
;
178 const char *bios_name
= NULL
;
179 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
180 to store the VM snapshots */
181 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
182 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
183 DriveInfo
*extboot_drive
= NULL
;
184 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
185 DisplayType display_type
= DT_DEFAULT
;
186 const char* keyboard_layout
= NULL
;
188 const char *mem_path
= NULL
;
190 int mem_prealloc
= 0; /* force preallocation of physical target memory */
193 NICInfo nd_table
[MAX_NICS
];
196 static int rtc_utc
= 1;
197 static int rtc_date_offset
= -1; /* -1 means no change */
198 QEMUClock
*rtc_clock
;
199 int vga_interface_type
= VGA_NONE
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
223 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
224 int assigned_devices_index
;
229 const char *vnc_display
;
230 int acpi_enabled
= 1;
238 int graphic_rotate
= 0;
239 uint8_t irq0override
= 1;
243 const char *watchdog
;
244 const char *option_rom
[MAX_OPTION_ROMS
];
246 int semihosting_enabled
= 0;
247 int time_drift_fix
= 0;
248 unsigned int kvm_shadow_memory
= 0;
252 const char *qemu_name
;
255 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
256 unsigned int nb_prom_envs
= 0;
257 const char *prom_envs
[MAX_PROM_ENVS
];
259 const char *nvram
= NULL
;
263 uint64_t node_mem
[MAX_NODES
];
264 uint64_t node_cpumask
[MAX_NODES
];
266 static CPUState
*cur_cpu
;
267 static CPUState
*next_cpu
;
268 static QEMUTimer
*nographic_timer
;
270 uint8_t qemu_uuid
[16];
272 static QEMUBootSetHandler
*boot_set_handler
;
273 static void *boot_set_opaque
;
276 #define SIG_IPI (SIGRTMIN+4)
278 #define SIG_IPI SIGUSR1
281 static int default_serial
= 1;
282 static int default_parallel
= 1;
283 static int default_virtcon
= 1;
284 static int default_monitor
= 1;
285 static int default_vga
= 1;
286 static int default_floppy
= 1;
287 static int default_cdrom
= 1;
288 static int default_sdcard
= 1;
289 static int default_qmp
= 1;
295 { .driver
= "isa-serial", .flag
= &default_serial
},
296 { .driver
= "isa-parallel", .flag
= &default_parallel
},
297 { .driver
= "isa-fdc", .flag
= &default_floppy
},
298 { .driver
= "ide-drive", .flag
= &default_cdrom
},
299 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
300 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
302 { .driver
= "VGA", .flag
= &default_vga
},
303 { .driver
= "cirrus-vga", .flag
= &default_vga
},
304 { .driver
= "vmware-svga", .flag
= &default_vga
},
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
323 /* x86 ISA bus support */
325 target_phys_addr_t isa_mem_base
= 0;
328 /***********************************************************/
329 void hw_error(const char *fmt
, ...)
335 fprintf(stderr
, "qemu: hardware error: ");
336 vfprintf(stderr
, fmt
, ap
);
337 fprintf(stderr
, "\n");
338 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
339 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
341 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
343 cpu_dump_state(env
, stderr
, fprintf
, 0);
350 static void set_proc_name(const char *s
)
352 #if defined(__linux__) && defined(PR_SET_NAME)
356 name
[sizeof(name
) - 1] = 0;
357 strncpy(name
, s
, sizeof(name
));
358 /* Could rewrite argv[0] too, but that's a bit more complicated.
359 This simple way is enough for `top'. */
360 prctl(PR_SET_NAME
, name
);
367 static QEMUBalloonEvent
*qemu_balloon_event
;
368 void *qemu_balloon_event_opaque
;
370 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
372 qemu_balloon_event
= func
;
373 qemu_balloon_event_opaque
= opaque
;
376 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
378 if (qemu_balloon_event
) {
379 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
386 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
388 if (qemu_balloon_event
) {
389 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
397 /***********************************************************/
398 /* real time host monotonic timer */
400 /* compute with 96 bit intermediate result: (a*b)/c */
401 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
406 #ifdef HOST_WORDS_BIGENDIAN
416 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
417 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
420 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
424 /***********************************************************/
425 /* host time/date access */
426 void qemu_get_timedate(struct tm
*tm
, int offset
)
433 if (rtc_date_offset
== -1) {
437 ret
= localtime(&ti
);
439 ti
-= rtc_date_offset
;
443 memcpy(tm
, ret
, sizeof(struct tm
));
446 int qemu_timedate_diff(struct tm
*tm
)
450 if (rtc_date_offset
== -1)
452 seconds
= mktimegm(tm
);
454 seconds
= mktime(tm
);
456 seconds
= mktimegm(tm
) + rtc_date_offset
;
458 return seconds
- time(NULL
);
461 void rtc_change_mon_event(struct tm
*tm
)
465 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
466 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
467 qobject_decref(data
);
470 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
472 time_t rtc_start_date
;
475 if (!strcmp(startdate
, "now") && legacy
) {
476 rtc_date_offset
= -1;
478 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
486 } else if (sscanf(startdate
, "%d-%d-%d",
498 rtc_start_date
= mktimegm(&tm
);
499 if (rtc_start_date
== -1) {
501 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
502 "'2006-06-17T16:01:21' or '2006-06-17'\n");
505 rtc_date_offset
= time(NULL
) - rtc_start_date
;
509 static void configure_rtc(QemuOpts
*opts
)
513 value
= qemu_opt_get(opts
, "base");
515 if (!strcmp(value
, "utc")) {
517 } else if (!strcmp(value
, "localtime")) {
520 configure_rtc_date_offset(value
, 0);
523 value
= qemu_opt_get(opts
, "clock");
525 if (!strcmp(value
, "host")) {
526 rtc_clock
= host_clock
;
527 } else if (!strcmp(value
, "vm")) {
528 rtc_clock
= vm_clock
;
530 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
534 #ifdef CONFIG_TARGET_I386
535 value
= qemu_opt_get(opts
, "driftfix");
537 if (!strcmp(buf
, "slew")) {
539 } else if (!strcmp(buf
, "none")) {
542 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
550 static void socket_cleanup(Notifier
*obj
)
555 static int socket_init(void)
559 static Notifier notifier
= { .notify
= socket_cleanup
};
561 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
563 err
= WSAGetLastError();
564 fprintf(stderr
, "WSAStartup: %d\n", err
);
567 exit_notifier_add(¬ifier
);
572 /*********************/
574 /*********************/
576 static NotifierList exit_notifiers
= NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
578 void exit_notifier_add(Notifier
*notifier
)
580 notifier_list_add(&exit_notifiers
, notifier
);
583 void exit_notifier_remove(Notifier
*notifier
)
585 notifier_list_remove(&exit_notifiers
, notifier
);
588 static void exit_notifier_notify(void)
590 notifier_list_notify(&exit_notifiers
);
593 static void exit_notifier_init(void)
595 atexit(exit_notifier_notify
);
598 /***********************************************************/
599 /* Bluetooth support */
602 static struct HCIInfo
*hci_table
[MAX_NICS
];
604 static struct bt_vlan_s
{
605 struct bt_scatternet_s net
;
607 struct bt_vlan_s
*next
;
610 /* find or alloc a new bluetooth "VLAN" */
611 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
613 struct bt_vlan_s
**pvlan
, *vlan
;
614 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
618 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
620 pvlan
= &first_bt_vlan
;
621 while (*pvlan
!= NULL
)
622 pvlan
= &(*pvlan
)->next
;
627 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
631 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
636 static struct HCIInfo null_hci
= {
637 .cmd_send
= null_hci_send
,
638 .sco_send
= null_hci_send
,
639 .acl_send
= null_hci_send
,
640 .bdaddr_set
= null_hci_addr_set
,
643 struct HCIInfo
*qemu_next_hci(void)
645 if (cur_hci
== nb_hcis
)
648 return hci_table
[cur_hci
++];
651 static struct HCIInfo
*hci_init(const char *str
)
654 struct bt_scatternet_s
*vlan
= 0;
656 if (!strcmp(str
, "null"))
659 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
661 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
662 else if (!strncmp(str
, "hci", 3)) {
665 if (!strncmp(str
+ 3, ",vlan=", 6)) {
666 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
671 vlan
= qemu_find_bt_vlan(0);
673 return bt_new_hci(vlan
);
676 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
681 static int bt_hci_parse(const char *str
)
686 if (nb_hcis
>= MAX_NICS
) {
687 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
700 bdaddr
.b
[5] = 0x56 + nb_hcis
;
701 hci
->bdaddr_set(hci
, bdaddr
.b
);
703 hci_table
[nb_hcis
++] = hci
;
708 static void bt_vhci_add(int vlan_id
)
710 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
713 fprintf(stderr
, "qemu: warning: adding a VHCI to "
714 "an empty scatternet %i\n", vlan_id
);
716 bt_vhci_init(bt_new_hci(vlan
));
719 static struct bt_device_s
*bt_device_add(const char *opt
)
721 struct bt_scatternet_s
*vlan
;
723 char *endp
= strstr(opt
, ",vlan=");
724 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
727 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
730 vlan_id
= strtol(endp
+ 6, &endp
, 0);
732 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
737 vlan
= qemu_find_bt_vlan(vlan_id
);
740 fprintf(stderr
, "qemu: warning: adding a slave device to "
741 "an empty scatternet %i\n", vlan_id
);
743 if (!strcmp(devname
, "keyboard"))
744 return bt_keyboard_init(vlan
);
746 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
750 static int bt_parse(const char *opt
)
752 const char *endp
, *p
;
755 if (strstart(opt
, "hci", &endp
)) {
756 if (!*endp
|| *endp
== ',') {
758 if (!strstart(endp
, ",vlan=", 0))
761 return bt_hci_parse(opt
);
763 } else if (strstart(opt
, "vhci", &endp
)) {
764 if (!*endp
|| *endp
== ',') {
766 if (strstart(endp
, ",vlan=", &p
)) {
767 vlan
= strtol(p
, (char **) &endp
, 0);
769 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
773 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
782 } else if (strstart(opt
, "device:", &endp
))
783 return !bt_device_add(endp
);
785 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
789 /***********************************************************/
790 /* QEMU Block devices */
792 #define HD_ALIAS "index=%d,media=disk"
793 #define CDROM_ALIAS "index=2,media=cdrom"
794 #define FD_ALIAS "index=%d,if=floppy"
795 #define PFLASH_ALIAS "if=pflash"
796 #define MTD_ALIAS "if=mtd"
797 #define SD_ALIAS "index=0,if=sd"
799 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
806 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
809 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
811 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
812 __FUNCTION__
, optstr
);
816 qemu_opt_set(opts
, "file", file
);
820 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
824 /* seek interface, bus and unit */
826 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
827 if (dinfo
->type
== type
&&
836 DriveInfo
*drive_get_by_id(const char *id
)
840 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
841 if (strcmp(id
, dinfo
->id
))
848 int drive_get_max_bus(BlockInterfaceType type
)
854 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
855 if(dinfo
->type
== type
&&
856 dinfo
->bus
> max_bus
)
857 max_bus
= dinfo
->bus
;
862 const char *drive_get_serial(BlockDriverState
*bdrv
)
866 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
867 if (dinfo
->bdrv
== bdrv
)
868 return dinfo
->serial
;
874 BlockInterfaceErrorAction
drive_get_on_error(
875 BlockDriverState
*bdrv
, int is_read
)
879 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
880 if (dinfo
->bdrv
== bdrv
)
881 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
884 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
887 static void bdrv_format_print(void *opaque
, const char *name
)
889 fprintf(stderr
, " %s", name
);
892 void drive_uninit(DriveInfo
*dinfo
)
894 qemu_opts_del(dinfo
->opts
);
895 bdrv_delete(dinfo
->bdrv
);
896 QTAILQ_REMOVE(&drives
, dinfo
, next
);
900 static int parse_block_error_action(const char *buf
, int is_read
)
902 if (!strcmp(buf
, "ignore")) {
903 return BLOCK_ERR_IGNORE
;
904 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
905 return BLOCK_ERR_STOP_ENOSPC
;
906 } else if (!strcmp(buf
, "stop")) {
907 return BLOCK_ERR_STOP_ANY
;
908 } else if (!strcmp(buf
, "report")) {
909 return BLOCK_ERR_REPORT
;
911 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
912 buf
, is_read
? "read" : "write");
917 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
921 const char *file
= NULL
;
924 const char *mediastr
= "";
925 BlockInterfaceType type
;
926 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
928 int cyls
, heads
, secs
, translation
;
929 BlockDriver
*drv
= NULL
;
930 QEMUMachine
*machine
= opaque
;
937 int on_read_error
, on_write_error
;
945 translation
= BIOS_ATA_TRANSLATION_AUTO
;
948 if (machine
&& machine
->use_scsi
) {
950 max_devs
= MAX_SCSI_DEVS
;
951 pstrcpy(devname
, sizeof(devname
), "scsi");
954 max_devs
= MAX_IDE_DEVS
;
955 pstrcpy(devname
, sizeof(devname
), "ide");
959 /* extract parameters */
960 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
961 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
962 index
= qemu_opt_get_number(opts
, "index", -1);
964 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
965 heads
= qemu_opt_get_number(opts
, "heads", 0);
966 secs
= qemu_opt_get_number(opts
, "secs", 0);
968 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
969 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
971 file
= qemu_opt_get(opts
, "file");
972 serial
= qemu_opt_get(opts
, "serial");
974 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
975 pstrcpy(devname
, sizeof(devname
), buf
);
976 if (!strcmp(buf
, "ide")) {
978 max_devs
= MAX_IDE_DEVS
;
979 } else if (!strcmp(buf
, "scsi")) {
981 max_devs
= MAX_SCSI_DEVS
;
982 } else if (!strcmp(buf
, "floppy")) {
985 } else if (!strcmp(buf
, "pflash")) {
988 } else if (!strcmp(buf
, "mtd")) {
991 } else if (!strcmp(buf
, "sd")) {
994 } else if (!strcmp(buf
, "virtio")) {
997 } else if (!strcmp(buf
, "xen")) {
1000 } else if (!strcmp(buf
, "none")) {
1004 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1009 if (cyls
|| heads
|| secs
) {
1010 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
1011 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1014 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
1015 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1018 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
1019 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1024 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1027 "qemu: '%s' trans must be used with cyls,heads and secs\n",
1031 if (!strcmp(buf
, "none"))
1032 translation
= BIOS_ATA_TRANSLATION_NONE
;
1033 else if (!strcmp(buf
, "lba"))
1034 translation
= BIOS_ATA_TRANSLATION_LBA
;
1035 else if (!strcmp(buf
, "auto"))
1036 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1038 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1043 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1044 if (!strcmp(buf
, "disk")) {
1046 } else if (!strcmp(buf
, "cdrom")) {
1047 if (cyls
|| secs
|| heads
) {
1049 "qemu: '%s' invalid physical CHS format\n", buf
);
1052 media
= MEDIA_CDROM
;
1054 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1059 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1060 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1062 else if (!strcmp(buf
, "writethrough"))
1064 else if (!strcmp(buf
, "writeback"))
1067 fprintf(stderr
, "qemu: invalid cache option\n");
1072 #ifdef CONFIG_LINUX_AIO
1073 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1074 if (!strcmp(buf
, "threads"))
1076 else if (!strcmp(buf
, "native"))
1079 fprintf(stderr
, "qemu: invalid aio option\n");
1085 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1086 if (strcmp(buf
, "?") == 0) {
1087 fprintf(stderr
, "qemu: Supported formats:");
1088 bdrv_iterate_format(bdrv_format_print
, NULL
);
1089 fprintf(stderr
, "\n");
1092 drv
= bdrv_find_whitelisted_format(buf
);
1094 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1099 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
1100 if (is_extboot
&& extboot_drive
) {
1101 fprintf(stderr
, "qemu: two bootable drives specified\n");
1105 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1106 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1107 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1108 fprintf(stderr
, "werror is no supported by this format\n");
1112 on_write_error
= parse_block_error_action(buf
, 0);
1113 if (on_write_error
< 0) {
1118 on_read_error
= BLOCK_ERR_REPORT
;
1119 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1120 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1121 fprintf(stderr
, "rerror is no supported by this format\n");
1125 on_read_error
= parse_block_error_action(buf
, 1);
1126 if (on_read_error
< 0) {
1131 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1132 if (type
!= IF_VIRTIO
) {
1133 fprintf(stderr
, "addr is not supported\n");
1138 /* compute bus and unit according index */
1141 if (bus_id
!= 0 || unit_id
!= -1) {
1143 "qemu: index cannot be used with bus and unit\n");
1151 unit_id
= index
% max_devs
;
1152 bus_id
= index
/ max_devs
;
1156 /* if user doesn't specify a unit_id,
1157 * try to find the first free
1160 if (unit_id
== -1) {
1162 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1164 if (max_devs
&& unit_id
>= max_devs
) {
1165 unit_id
-= max_devs
;
1173 if (max_devs
&& unit_id
>= max_devs
) {
1174 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1175 unit_id
, max_devs
- 1);
1180 * ignore multiple definitions
1183 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1190 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1191 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1192 dinfo
->id
= qemu_strdup(buf
);
1194 /* no id supplied -> create one */
1195 dinfo
->id
= qemu_mallocz(32);
1196 if (type
== IF_IDE
|| type
== IF_SCSI
)
1197 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1199 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1200 devname
, bus_id
, mediastr
, unit_id
);
1202 snprintf(dinfo
->id
, 32, "%s%s%i",
1203 devname
, mediastr
, unit_id
);
1205 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1206 dinfo
->devaddr
= devaddr
;
1208 dinfo
->bus
= bus_id
;
1209 dinfo
->unit
= unit_id
;
1210 dinfo
->on_read_error
= on_read_error
;
1211 dinfo
->on_write_error
= on_write_error
;
1214 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1215 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1217 extboot_drive
= dinfo
;
1228 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1229 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1233 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1238 /* FIXME: This isn't really a floppy, but it's a reasonable
1241 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1247 /* add virtio block device */
1248 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1249 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1250 qemu_opt_set(opts
, "drive", dinfo
->id
);
1252 qemu_opt_set(opts
, "addr", devaddr
);
1263 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1264 cache
= 2; /* always use write-back with snapshot */
1266 if (cache
== 0) /* no caching */
1267 bdrv_flags
|= BDRV_O_NOCACHE
;
1268 else if (cache
== 2) /* write-back */
1269 bdrv_flags
|= BDRV_O_CACHE_WB
;
1272 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1274 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1278 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1279 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1284 * cdrom is read-only. Set it now, after above interface checking
1285 * since readonly attribute not explicitly required, so no error.
1287 if (media
== MEDIA_CDROM
) {
1290 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1292 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1293 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1294 file
, strerror(errno
));
1298 if (bdrv_key_required(dinfo
->bdrv
))
1304 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1306 QEMUMachine
*machine
= opaque
;
1307 int fatal_error
= 0;
1309 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1316 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1318 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1319 qemu_opt_set(opts
, "snapshot", "on");
1324 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1326 boot_set_handler
= func
;
1327 boot_set_opaque
= opaque
;
1330 int qemu_boot_set(const char *boot_devices
)
1332 if (!boot_set_handler
) {
1335 return boot_set_handler(boot_set_opaque
, boot_devices
);
1338 static int parse_bootdevices(char *devices
)
1340 /* We just do some generic consistency checks */
1344 for (p
= devices
; *p
!= '\0'; p
++) {
1345 /* Allowed boot devices are:
1346 * a-b: floppy disk drives
1347 * c-f: IDE disk drives
1348 * g-m: machine implementation dependant drives
1349 * n-p: network devices
1350 * It's up to each machine implementation to check if the given boot
1351 * devices match the actual hardware implementation and firmware
1354 if (*p
< 'a' || *p
> 'p') {
1355 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1358 if (bitmap
& (1 << (*p
- 'a'))) {
1359 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1362 bitmap
|= 1 << (*p
- 'a');
1367 static void restore_boot_devices(void *opaque
)
1369 char *standard_boot_devices
= opaque
;
1371 qemu_boot_set(standard_boot_devices
);
1373 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1374 qemu_free(standard_boot_devices
);
1377 static void numa_add(const char *optarg
)
1381 unsigned long long value
, endvalue
;
1384 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1385 if (!strcmp(option
, "node")) {
1386 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1387 nodenr
= nb_numa_nodes
;
1389 nodenr
= strtoull(option
, NULL
, 10);
1392 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1393 node_mem
[nodenr
] = 0;
1395 value
= strtoull(option
, &endptr
, 0);
1397 case 0: case 'M': case 'm':
1404 node_mem
[nodenr
] = value
;
1406 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1407 node_cpumask
[nodenr
] = 0;
1409 value
= strtoull(option
, &endptr
, 10);
1412 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1414 if (*endptr
== '-') {
1415 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1416 if (endvalue
>= 63) {
1419 "only 63 CPUs in NUMA mode supported.\n");
1421 value
= (2ULL << endvalue
) - (1ULL << value
);
1423 value
= 1ULL << value
;
1426 node_cpumask
[nodenr
] = value
;
1433 static void smp_parse(const char *optarg
)
1435 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1439 smp
= strtoul(optarg
, &endptr
, 10);
1440 if (endptr
!= optarg
) {
1441 if (*endptr
== ',') {
1445 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1446 sockets
= strtoull(option
, NULL
, 10);
1447 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1448 cores
= strtoull(option
, NULL
, 10);
1449 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1450 threads
= strtoull(option
, NULL
, 10);
1451 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1452 max_cpus
= strtoull(option
, NULL
, 10);
1454 /* compute missing values, prefer sockets over cores over threads */
1455 if (smp
== 0 || sockets
== 0) {
1456 sockets
= sockets
> 0 ? sockets
: 1;
1457 cores
= cores
> 0 ? cores
: 1;
1458 threads
= threads
> 0 ? threads
: 1;
1460 smp
= cores
* threads
* sockets
;
1464 threads
= threads
> 0 ? threads
: 1;
1465 cores
= smp
/ (sockets
* threads
);
1468 threads
= smp
/ (cores
* sockets
);
1473 smp_cores
= cores
> 0 ? cores
: 1;
1474 smp_threads
= threads
> 0 ? threads
: 1;
1476 max_cpus
= smp_cpus
;
1479 /***********************************************************/
1482 static int usb_device_add(const char *devname
, int is_hotplug
)
1485 USBDevice
*dev
= NULL
;
1490 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1491 dev
= usbdevice_create(devname
);
1495 /* the other ones */
1496 if (strstart(devname
, "host:", &p
)) {
1497 dev
= usb_host_device_open(p
);
1498 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1499 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1500 bt_new_hci(qemu_find_bt_vlan(0)));
1511 static int usb_device_del(const char *devname
)
1516 if (strstart(devname
, "host:", &p
))
1517 return usb_host_device_close(p
);
1522 p
= strchr(devname
, '.');
1525 bus_num
= strtoul(devname
, NULL
, 0);
1526 addr
= strtoul(p
+ 1, NULL
, 0);
1528 return usb_device_delete_addr(bus_num
, addr
);
1531 static int usb_parse(const char *cmdline
)
1534 r
= usb_device_add(cmdline
, 0);
1536 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1541 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1543 const char *devname
= qdict_get_str(qdict
, "devname");
1544 if (usb_device_add(devname
, 1) < 0) {
1545 error_report("could not add USB device '%s'", devname
);
1549 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1551 const char *devname
= qdict_get_str(qdict
, "devname");
1552 if (usb_device_del(devname
) < 0) {
1553 error_report("could not delete USB device '%s'", devname
);
1557 /***********************************************************/
1558 /* PCMCIA/Cardbus */
1560 static struct pcmcia_socket_entry_s
{
1561 PCMCIASocket
*socket
;
1562 struct pcmcia_socket_entry_s
*next
;
1563 } *pcmcia_sockets
= 0;
1565 void pcmcia_socket_register(PCMCIASocket
*socket
)
1567 struct pcmcia_socket_entry_s
*entry
;
1569 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1570 entry
->socket
= socket
;
1571 entry
->next
= pcmcia_sockets
;
1572 pcmcia_sockets
= entry
;
1575 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1577 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1579 ptr
= &pcmcia_sockets
;
1580 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1581 if (entry
->socket
== socket
) {
1587 void pcmcia_info(Monitor
*mon
)
1589 struct pcmcia_socket_entry_s
*iter
;
1591 if (!pcmcia_sockets
)
1592 monitor_printf(mon
, "No PCMCIA sockets\n");
1594 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1595 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1596 iter
->socket
->attached
? iter
->socket
->card_string
:
1600 /***********************************************************/
1603 typedef struct IOHandlerRecord
{
1605 IOCanReadHandler
*fd_read_poll
;
1607 IOHandler
*fd_write
;
1610 /* temporary data */
1612 QLIST_ENTRY(IOHandlerRecord
) next
;
1615 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1616 QLIST_HEAD_INITIALIZER(io_handlers
);
1619 /* XXX: fd_read_poll should be suppressed, but an API change is
1620 necessary in the character devices to suppress fd_can_read(). */
1621 int qemu_set_fd_handler2(int fd
,
1622 IOCanReadHandler
*fd_read_poll
,
1624 IOHandler
*fd_write
,
1627 IOHandlerRecord
*ioh
;
1629 if (!fd_read
&& !fd_write
) {
1630 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1631 if (ioh
->fd
== fd
) {
1637 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1641 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1642 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1645 ioh
->fd_read_poll
= fd_read_poll
;
1646 ioh
->fd_read
= fd_read
;
1647 ioh
->fd_write
= fd_write
;
1648 ioh
->opaque
= opaque
;
1651 qemu_notify_event();
1655 int qemu_set_fd_handler(int fd
,
1657 IOHandler
*fd_write
,
1660 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1664 /***********************************************************/
1665 /* Polling handling */
1667 typedef struct PollingEntry
{
1670 struct PollingEntry
*next
;
1673 static PollingEntry
*first_polling_entry
;
1675 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1677 PollingEntry
**ppe
, *pe
;
1678 pe
= qemu_mallocz(sizeof(PollingEntry
));
1680 pe
->opaque
= opaque
;
1681 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1686 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1688 PollingEntry
**ppe
, *pe
;
1689 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1691 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1699 /***********************************************************/
1700 /* Wait objects support */
1701 typedef struct WaitObjects
{
1703 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1704 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1705 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1708 static WaitObjects wait_objects
= {0};
1710 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1712 WaitObjects
*w
= &wait_objects
;
1714 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1716 w
->events
[w
->num
] = handle
;
1717 w
->func
[w
->num
] = func
;
1718 w
->opaque
[w
->num
] = opaque
;
1723 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1726 WaitObjects
*w
= &wait_objects
;
1729 for (i
= 0; i
< w
->num
; i
++) {
1730 if (w
->events
[i
] == handle
)
1733 w
->events
[i
] = w
->events
[i
+ 1];
1734 w
->func
[i
] = w
->func
[i
+ 1];
1735 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1743 /***********************************************************/
1744 /* ram save/restore */
1746 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1747 #define RAM_SAVE_FLAG_COMPRESS 0x02
1748 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1749 #define RAM_SAVE_FLAG_PAGE 0x08
1750 #define RAM_SAVE_FLAG_EOS 0x10
1752 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1754 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1755 uint32_t *array
= (uint32_t *)page
;
1758 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1759 if (array
[i
] != val
)
1766 static int ram_save_block(QEMUFile
*f
)
1768 static ram_addr_t current_addr
= 0;
1769 ram_addr_t saved_addr
= current_addr
;
1770 ram_addr_t addr
= 0;
1773 while (addr
< last_ram_offset
) {
1774 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1777 cpu_physical_memory_reset_dirty(current_addr
,
1778 current_addr
+ TARGET_PAGE_SIZE
,
1779 MIGRATION_DIRTY_FLAG
);
1781 p
= qemu_get_ram_ptr(current_addr
);
1783 if (is_dup_page(p
, *p
)) {
1784 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1785 qemu_put_byte(f
, *p
);
1787 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1788 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1794 addr
+= TARGET_PAGE_SIZE
;
1795 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1801 static uint64_t bytes_transferred
;
1803 static ram_addr_t
ram_save_remaining(void)
1806 ram_addr_t count
= 0;
1808 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1809 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1816 uint64_t ram_bytes_remaining(void)
1818 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1821 uint64_t ram_bytes_transferred(void)
1823 return bytes_transferred
;
1826 uint64_t ram_bytes_total(void)
1828 return last_ram_offset
;
1831 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1834 uint64_t bytes_transferred_last
;
1836 uint64_t expected_time
= 0;
1839 cpu_physical_memory_set_dirty_tracking(0);
1843 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1844 qemu_file_set_error(f
);
1849 bytes_transferred
= 0;
1851 /* Make sure all dirty bits are set */
1852 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1853 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1854 cpu_physical_memory_set_dirty(addr
);
1857 /* Enable dirty memory tracking */
1858 cpu_physical_memory_set_dirty_tracking(1);
1860 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1863 bytes_transferred_last
= bytes_transferred
;
1864 bwidth
= qemu_get_clock_ns(rt_clock
);
1866 while (!qemu_file_rate_limit(f
)) {
1869 ret
= ram_save_block(f
);
1870 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1871 if (ret
== 0) /* no more blocks */
1875 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1876 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1878 /* if we haven't transferred anything this round, force expected_time to a
1879 * a very high value, but without crashing */
1883 /* try transferring iterative blocks of memory */
1885 /* flush all remaining blocks regardless of rate limiting */
1886 while (ram_save_block(f
) != 0) {
1887 bytes_transferred
+= TARGET_PAGE_SIZE
;
1889 cpu_physical_memory_set_dirty_tracking(0);
1892 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1894 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1896 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1899 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1904 if (version_id
!= 3)
1908 addr
= qemu_get_be64(f
);
1910 flags
= addr
& ~TARGET_PAGE_MASK
;
1911 addr
&= TARGET_PAGE_MASK
;
1913 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1914 if (addr
!= last_ram_offset
)
1918 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1919 uint8_t ch
= qemu_get_byte(f
);
1920 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1923 (!kvm_enabled() || kvm_has_sync_mmu())) {
1924 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1927 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1928 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1930 if (qemu_file_has_error(f
)) {
1933 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1938 void qemu_service_io(void)
1940 qemu_notify_event();
1943 /***********************************************************/
1944 /* machine registration */
1946 static QEMUMachine
*first_machine
= NULL
;
1947 QEMUMachine
*current_machine
= NULL
;
1949 int qemu_register_machine(QEMUMachine
*m
)
1952 pm
= &first_machine
;
1960 static QEMUMachine
*find_machine(const char *name
)
1964 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1965 if (!strcmp(m
->name
, name
))
1967 if (m
->alias
&& !strcmp(m
->alias
, name
))
1973 static QEMUMachine
*find_default_machine(void)
1977 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1978 if (m
->is_default
) {
1985 /***********************************************************/
1986 /* main execution loop */
1988 static void gui_update(void *opaque
)
1990 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1991 DisplayState
*ds
= opaque
;
1992 DisplayChangeListener
*dcl
= ds
->listeners
;
1994 qemu_flush_coalesced_mmio_buffer();
1997 while (dcl
!= NULL
) {
1998 if (dcl
->gui_timer_interval
&&
1999 dcl
->gui_timer_interval
< interval
)
2000 interval
= dcl
->gui_timer_interval
;
2003 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
2006 static void nographic_update(void *opaque
)
2008 uint64_t interval
= GUI_REFRESH_INTERVAL
;
2010 qemu_flush_coalesced_mmio_buffer();
2011 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
2014 void cpu_synchronize_all_states(void)
2018 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2019 cpu_synchronize_state(cpu
);
2023 void cpu_synchronize_all_post_reset(void)
2027 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2028 cpu_synchronize_post_reset(cpu
);
2032 void cpu_synchronize_all_post_init(void)
2036 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
2037 cpu_synchronize_post_init(cpu
);
2041 struct vm_change_state_entry
{
2042 VMChangeStateHandler
*cb
;
2044 QLIST_ENTRY (vm_change_state_entry
) entries
;
2047 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
2049 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
2052 VMChangeStateEntry
*e
;
2054 e
= qemu_mallocz(sizeof (*e
));
2058 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2062 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2064 QLIST_REMOVE (e
, entries
);
2068 static void vm_state_notify(int running
, int reason
)
2070 VMChangeStateEntry
*e
;
2072 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2073 e
->cb(e
->opaque
, running
, reason
);
2077 static void resume_all_vcpus(void);
2078 static void pause_all_vcpus(void);
2085 vm_state_notify(1, 0);
2090 /* reset/shutdown handler */
2092 typedef struct QEMUResetEntry
{
2093 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2094 QEMUResetHandler
*func
;
2098 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2099 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2100 static int reset_requested
;
2101 static int shutdown_requested
;
2102 static int powerdown_requested
;
2103 static int debug_requested
;
2104 static int vmstop_requested
;
2106 int qemu_no_shutdown(void)
2108 int r
= no_shutdown
;
2113 int qemu_shutdown_requested(void)
2115 int r
= shutdown_requested
;
2116 shutdown_requested
= 0;
2120 int qemu_reset_requested(void)
2122 int r
= reset_requested
;
2123 reset_requested
= 0;
2127 int qemu_powerdown_requested(void)
2129 int r
= powerdown_requested
;
2130 powerdown_requested
= 0;
2134 static int qemu_debug_requested(void)
2136 int r
= debug_requested
;
2137 debug_requested
= 0;
2141 static int qemu_vmstop_requested(void)
2143 int r
= vmstop_requested
;
2144 vmstop_requested
= 0;
2148 static void do_vm_stop(int reason
)
2151 cpu_disable_ticks();
2154 vm_state_notify(0, reason
);
2155 monitor_protocol_event(QEVENT_STOP
, NULL
);
2159 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2161 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2164 re
->opaque
= opaque
;
2165 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2168 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2172 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2173 if (re
->func
== func
&& re
->opaque
== opaque
) {
2174 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2181 void qemu_system_reset(void)
2183 QEMUResetEntry
*re
, *nre
;
2185 /* reset all devices */
2186 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2187 re
->func(re
->opaque
);
2189 monitor_protocol_event(QEVENT_RESET
, NULL
);
2190 cpu_synchronize_all_post_reset();
2193 void qemu_system_reset_request(void)
2196 shutdown_requested
= 1;
2198 reset_requested
= 1;
2200 if (cpu_single_env
) {
2201 cpu_single_env
->stopped
= 1;
2203 qemu_notify_event();
2206 void qemu_system_shutdown_request(void)
2208 shutdown_requested
= 1;
2209 qemu_notify_event();
2212 void qemu_system_powerdown_request(void)
2214 powerdown_requested
= 1;
2215 qemu_notify_event();
2218 static int cpu_can_run(CPUState
*env
)
2229 static int cpu_has_work(CPUState
*env
)
2237 if (qemu_cpu_has_work(env
))
2242 static int tcg_has_work(void)
2246 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2247 if (cpu_has_work(env
))
2253 static int io_thread_fd
= -1;
2255 static void qemu_event_increment(void)
2257 /* Write 8 bytes to be compatible with eventfd. */
2258 static uint64_t val
= 1;
2261 if (io_thread_fd
== -1)
2265 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2266 } while (ret
< 0 && errno
== EINTR
);
2268 /* EAGAIN is fine, a read must be pending. */
2269 if (ret
< 0 && errno
!= EAGAIN
) {
2270 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2276 static void qemu_event_read(void *opaque
)
2278 int fd
= (unsigned long)opaque
;
2282 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2284 len
= read(fd
, buffer
, sizeof(buffer
));
2285 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2288 static int qemu_event_init(void)
2293 err
= qemu_eventfd(fds
);
2297 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2301 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2305 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2306 (void *)(unsigned long)fds
[0]);
2308 io_thread_fd
= fds
[1];
2317 HANDLE qemu_event_handle
;
2319 static void dummy_event_handler(void *opaque
)
2323 static int qemu_event_init(void)
2325 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2326 if (!qemu_event_handle
) {
2327 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2330 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2334 static void qemu_event_increment(void)
2336 if (!SetEvent(qemu_event_handle
)) {
2337 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2344 #ifndef CONFIG_IOTHREAD
2345 static int qemu_init_main_loop(void)
2347 return qemu_event_init();
2350 void qemu_init_vcpu(void *_env
)
2352 CPUState
*env
= _env
;
2354 env
->nr_cores
= smp_cores
;
2355 env
->nr_threads
= smp_threads
;
2361 int qemu_cpu_self(void *env
)
2366 static void resume_all_vcpus(void)
2370 static void pause_all_vcpus(void)
2374 void qemu_cpu_kick(void *env
)
2379 void qemu_notify_event(void)
2381 CPUState
*env
= cpu_single_env
;
2383 if (kvm_enabled()) {
2384 qemu_kvm_notify_work();
2388 qemu_event_increment ();
2392 if (next_cpu
&& env
!= next_cpu
) {
2397 #if defined(KVM_UPSTREAM) || !defined(CONFIG_KVM)
2398 void qemu_mutex_lock_iothread(void) {}
2399 void qemu_mutex_unlock_iothread(void) {}
2402 void vm_stop(int reason
)
2407 #else /* CONFIG_IOTHREAD */
2409 #include "qemu-thread.h"
2411 QemuMutex qemu_global_mutex
;
2412 static QemuMutex qemu_fair_mutex
;
2414 static QemuThread io_thread
;
2416 static QemuThread
*tcg_cpu_thread
;
2417 static QemuCond
*tcg_halt_cond
;
2419 static int qemu_system_ready
;
2421 static QemuCond qemu_cpu_cond
;
2423 static QemuCond qemu_system_cond
;
2424 static QemuCond qemu_pause_cond
;
2426 static void tcg_block_io_signals(void);
2427 static void kvm_block_io_signals(CPUState
*env
);
2428 static void unblock_io_signals(void);
2430 static int qemu_init_main_loop(void)
2434 ret
= qemu_event_init();
2438 qemu_cond_init(&qemu_pause_cond
);
2439 qemu_mutex_init(&qemu_fair_mutex
);
2440 qemu_mutex_init(&qemu_global_mutex
);
2441 qemu_mutex_lock(&qemu_global_mutex
);
2443 unblock_io_signals();
2444 qemu_thread_self(&io_thread
);
2449 static void qemu_wait_io_event_common(CPUState
*env
)
2454 qemu_cond_signal(&qemu_pause_cond
);
2458 static void qemu_wait_io_event(CPUState
*env
)
2460 while (!tcg_has_work())
2461 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2463 qemu_mutex_unlock(&qemu_global_mutex
);
2466 * Users of qemu_global_mutex can be starved, having no chance
2467 * to acquire it since this path will get to it first.
2468 * So use another lock to provide fairness.
2470 qemu_mutex_lock(&qemu_fair_mutex
);
2471 qemu_mutex_unlock(&qemu_fair_mutex
);
2473 qemu_mutex_lock(&qemu_global_mutex
);
2474 qemu_wait_io_event_common(env
);
2477 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2484 ts
.tv_sec
= timeout
/ 1000;
2485 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2487 sigemptyset(&waitset
);
2488 sigaddset(&waitset
, SIG_IPI
);
2490 qemu_mutex_unlock(&qemu_global_mutex
);
2491 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2493 qemu_mutex_lock(&qemu_global_mutex
);
2495 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2496 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2501 static void qemu_kvm_wait_io_event(CPUState
*env
)
2503 while (!cpu_has_work(env
))
2504 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2506 qemu_kvm_eat_signal(env
, 0);
2507 qemu_wait_io_event_common(env
);
2510 static int qemu_cpu_exec(CPUState
*env
);
2512 static void *kvm_cpu_thread_fn(void *arg
)
2514 CPUState
*env
= arg
;
2516 qemu_thread_self(env
->thread
);
2520 kvm_block_io_signals(env
);
2522 /* signal CPU creation */
2523 qemu_mutex_lock(&qemu_global_mutex
);
2525 qemu_cond_signal(&qemu_cpu_cond
);
2527 /* and wait for machine initialization */
2528 while (!qemu_system_ready
)
2529 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2532 if (cpu_can_run(env
))
2534 qemu_kvm_wait_io_event(env
);
2540 static bool tcg_cpu_exec(void);
2542 static void *tcg_cpu_thread_fn(void *arg
)
2544 CPUState
*env
= arg
;
2546 tcg_block_io_signals();
2547 qemu_thread_self(env
->thread
);
2549 /* signal CPU creation */
2550 qemu_mutex_lock(&qemu_global_mutex
);
2551 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2553 qemu_cond_signal(&qemu_cpu_cond
);
2555 /* and wait for machine initialization */
2556 while (!qemu_system_ready
)
2557 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2561 qemu_wait_io_event(cur_cpu
);
2567 void qemu_cpu_kick(void *_env
)
2569 CPUState
*env
= _env
;
2570 qemu_cond_broadcast(env
->halt_cond
);
2572 qemu_thread_signal(env
->thread
, SIG_IPI
);
2575 int qemu_cpu_self(void *_env
)
2577 CPUState
*env
= _env
;
2580 qemu_thread_self(&this);
2582 return qemu_thread_equal(&this, env
->thread
);
2585 static void cpu_signal(int sig
)
2588 cpu_exit(cpu_single_env
);
2591 static void tcg_block_io_signals(void)
2594 struct sigaction sigact
;
2597 sigaddset(&set
, SIGUSR2
);
2598 sigaddset(&set
, SIGIO
);
2599 sigaddset(&set
, SIGALRM
);
2600 sigaddset(&set
, SIGCHLD
);
2601 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2604 sigaddset(&set
, SIG_IPI
);
2605 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2607 memset(&sigact
, 0, sizeof(sigact
));
2608 sigact
.sa_handler
= cpu_signal
;
2609 sigaction(SIG_IPI
, &sigact
, NULL
);
2612 static void dummy_signal(int sig
)
2616 static void kvm_block_io_signals(CPUState
*env
)
2620 struct sigaction sigact
;
2623 sigaddset(&set
, SIGUSR2
);
2624 sigaddset(&set
, SIGIO
);
2625 sigaddset(&set
, SIGALRM
);
2626 sigaddset(&set
, SIGCHLD
);
2627 sigaddset(&set
, SIG_IPI
);
2628 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2630 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2631 sigdelset(&set
, SIG_IPI
);
2633 memset(&sigact
, 0, sizeof(sigact
));
2634 sigact
.sa_handler
= dummy_signal
;
2635 sigaction(SIG_IPI
, &sigact
, NULL
);
2637 r
= kvm_set_signal_mask(env
, &set
);
2639 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2644 static void unblock_io_signals(void)
2649 sigaddset(&set
, SIGUSR2
);
2650 sigaddset(&set
, SIGIO
);
2651 sigaddset(&set
, SIGALRM
);
2652 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2655 sigaddset(&set
, SIG_IPI
);
2656 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2659 static void qemu_signal_lock(unsigned int msecs
)
2661 qemu_mutex_lock(&qemu_fair_mutex
);
2663 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2664 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2665 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2668 qemu_mutex_unlock(&qemu_fair_mutex
);
2671 void qemu_mutex_lock_iothread(void)
2673 if (kvm_enabled()) {
2674 qemu_mutex_lock(&qemu_fair_mutex
);
2675 qemu_mutex_lock(&qemu_global_mutex
);
2676 qemu_mutex_unlock(&qemu_fair_mutex
);
2678 qemu_signal_lock(100);
2681 void qemu_mutex_unlock_iothread(void)
2683 qemu_mutex_unlock(&qemu_global_mutex
);
2686 static int all_vcpus_paused(void)
2688 CPUState
*penv
= first_cpu
;
2693 penv
= (CPUState
*)penv
->next_cpu
;
2699 static void pause_all_vcpus(void)
2701 CPUState
*penv
= first_cpu
;
2705 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2706 qemu_cpu_kick(penv
);
2707 penv
= (CPUState
*)penv
->next_cpu
;
2710 while (!all_vcpus_paused()) {
2711 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2714 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2715 penv
= (CPUState
*)penv
->next_cpu
;
2720 static void resume_all_vcpus(void)
2722 CPUState
*penv
= first_cpu
;
2727 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2728 qemu_cpu_kick(penv
);
2729 penv
= (CPUState
*)penv
->next_cpu
;
2733 static void tcg_init_vcpu(void *_env
)
2735 CPUState
*env
= _env
;
2736 /* share a single thread for all cpus with TCG */
2737 if (!tcg_cpu_thread
) {
2738 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2739 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2740 qemu_cond_init(env
->halt_cond
);
2741 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2742 while (env
->created
== 0)
2743 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2744 tcg_cpu_thread
= env
->thread
;
2745 tcg_halt_cond
= env
->halt_cond
;
2747 env
->thread
= tcg_cpu_thread
;
2748 env
->halt_cond
= tcg_halt_cond
;
2752 static void kvm_start_vcpu(CPUState
*env
)
2754 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2755 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2756 qemu_cond_init(env
->halt_cond
);
2757 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2758 while (env
->created
== 0)
2759 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2762 void qemu_init_vcpu(void *_env
)
2764 CPUState
*env
= _env
;
2766 env
->nr_cores
= smp_cores
;
2767 env
->nr_threads
= smp_threads
;
2769 kvm_start_vcpu(env
);
2774 void qemu_notify_event(void)
2776 qemu_event_increment();
2779 static void qemu_system_vmstop_request(int reason
)
2781 vmstop_requested
= reason
;
2782 qemu_notify_event();
2785 void vm_stop(int reason
)
2788 qemu_thread_self(&me
);
2790 if (!qemu_thread_equal(&me
, &io_thread
)) {
2791 qemu_system_vmstop_request(reason
);
2793 * FIXME: should not return to device code in case
2794 * vm_stop() has been requested.
2796 if (cpu_single_env
) {
2797 cpu_exit(cpu_single_env
);
2798 cpu_single_env
->stop
= 1;
2809 static void host_main_loop_wait(int *timeout
)
2815 /* XXX: need to suppress polling by better using win32 events */
2817 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2818 ret
|= pe
->func(pe
->opaque
);
2822 WaitObjects
*w
= &wait_objects
;
2824 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2825 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2826 if (w
->func
[ret
- WAIT_OBJECT_0
])
2827 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2829 /* Check for additional signaled events */
2830 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2832 /* Check if event is signaled */
2833 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2834 if(ret2
== WAIT_OBJECT_0
) {
2836 w
->func
[i
](w
->opaque
[i
]);
2837 } else if (ret2
== WAIT_TIMEOUT
) {
2839 err
= GetLastError();
2840 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2843 } else if (ret
== WAIT_TIMEOUT
) {
2845 err
= GetLastError();
2846 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2853 static void host_main_loop_wait(int *timeout
)
2858 void main_loop_wait(int nonblocking
)
2860 IOHandlerRecord
*ioh
;
2861 fd_set rfds
, wfds
, xfds
;
2869 timeout
= qemu_calculate_timeout();
2870 qemu_bh_update_timeout(&timeout
);
2873 host_main_loop_wait(&timeout
);
2875 /* poll any events */
2876 /* XXX: separate device handlers from system ones */
2881 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2885 (!ioh
->fd_read_poll
||
2886 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2887 FD_SET(ioh
->fd
, &rfds
);
2891 if (ioh
->fd_write
) {
2892 FD_SET(ioh
->fd
, &wfds
);
2898 tv
.tv_sec
= timeout
/ 1000;
2899 tv
.tv_usec
= (timeout
% 1000) * 1000;
2901 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2903 qemu_mutex_unlock_iothread();
2904 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2905 qemu_mutex_lock_iothread();
2907 IOHandlerRecord
*pioh
;
2909 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2911 QLIST_REMOVE(ioh
, next
);
2915 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2916 ioh
->fd_read(ioh
->opaque
);
2917 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
2918 FD_CLR(ioh
->fd
, &rfds
);
2920 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2921 ioh
->fd_write(ioh
->opaque
);
2926 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2928 qemu_run_all_timers();
2930 /* Check bottom-halves last in case any of the earlier events triggered
2936 static int qemu_cpu_exec(CPUState
*env
)
2939 #ifdef CONFIG_PROFILER
2943 #ifdef CONFIG_PROFILER
2944 ti
= profile_getclock();
2949 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2950 env
->icount_decr
.u16
.low
= 0;
2951 env
->icount_extra
= 0;
2952 count
= qemu_icount_round (qemu_next_deadline());
2953 qemu_icount
+= count
;
2954 decr
= (count
> 0xffff) ? 0xffff : count
;
2956 env
->icount_decr
.u16
.low
= decr
;
2957 env
->icount_extra
= count
;
2959 ret
= cpu_exec(env
);
2960 #ifdef CONFIG_PROFILER
2961 qemu_time
+= profile_getclock() - ti
;
2964 /* Fold pending instructions back into the
2965 instruction counter, and clear the interrupt flag. */
2966 qemu_icount
-= (env
->icount_decr
.u16
.low
2967 + env
->icount_extra
);
2968 env
->icount_decr
.u32
= 0;
2969 env
->icount_extra
= 0;
2974 static bool tcg_cpu_exec(void)
2978 if (next_cpu
== NULL
)
2979 next_cpu
= first_cpu
;
2980 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2981 CPUState
*env
= cur_cpu
= next_cpu
;
2983 qemu_clock_enable(vm_clock
,
2984 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2986 if (qemu_alarm_pending())
2988 if (cpu_can_run(env
))
2989 ret
= qemu_cpu_exec(env
);
2993 if (ret
== EXCP_DEBUG
) {
2994 gdb_set_stop_cpu(env
);
2995 debug_requested
= 1;
2999 return tcg_has_work();
3002 static int vm_can_run(void)
3004 if (powerdown_requested
)
3006 if (reset_requested
)
3008 if (shutdown_requested
)
3010 if (debug_requested
)
3015 qemu_irq qemu_system_powerdown
;
3017 static void main_loop(void)
3021 if (kvm_enabled()) {
3023 cpu_disable_ticks();
3027 #ifdef CONFIG_IOTHREAD
3028 qemu_system_ready
= 1;
3029 qemu_cond_broadcast(&qemu_system_cond
);
3034 bool nonblocking
= false;
3035 #ifdef CONFIG_PROFILER
3038 #ifndef CONFIG_IOTHREAD
3039 nonblocking
= tcg_cpu_exec();
3041 #ifdef CONFIG_PROFILER
3042 ti
= profile_getclock();
3044 main_loop_wait(nonblocking
);
3045 #ifdef CONFIG_PROFILER
3046 dev_time
+= profile_getclock() - ti
;
3048 } while (vm_can_run());
3050 if (qemu_debug_requested()) {
3051 vm_stop(EXCP_DEBUG
);
3053 if (qemu_shutdown_requested()) {
3054 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
3061 if (qemu_reset_requested()) {
3063 qemu_system_reset();
3066 if (qemu_powerdown_requested()) {
3067 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
3068 qemu_irq_raise(qemu_system_powerdown
);
3070 if ((r
= qemu_vmstop_requested())) {
3077 static void version(void)
3079 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3082 static void help(int exitcode
)
3084 const char *options_help
=
3085 #define DEF(option, opt_arg, opt_enum, opt_help) \
3087 #define DEFHEADING(text) stringify(text) "\n"
3088 #include "qemu-options.h"
3094 printf("usage: %s [options] [disk_image]\n"
3096 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3099 "During emulation, the following keys are useful:\n"
3100 "ctrl-alt-f toggle full screen\n"
3101 "ctrl-alt-n switch to virtual console 'n'\n"
3102 "ctrl-alt toggle mouse and keyboard grab\n"
3104 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3110 #define HAS_ARG 0x0001
3113 #define DEF(option, opt_arg, opt_enum, opt_help) \
3115 #define DEFHEADING(text)
3116 #include "qemu-options.h"
3122 typedef struct QEMUOption
{
3128 static const QEMUOption qemu_options
[] = {
3129 { "h", 0, QEMU_OPTION_h
},
3130 #define DEF(option, opt_arg, opt_enum, opt_help) \
3131 { option, opt_arg, opt_enum },
3132 #define DEFHEADING(text)
3133 #include "qemu-options.h"
3141 struct soundhw soundhw
[] = {
3142 #ifdef HAS_AUDIO_CHOICE
3143 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3149 { .init_isa
= pcspk_audio_init
}
3156 "Creative Sound Blaster 16",
3159 { .init_isa
= SB16_init
}
3163 #ifdef CONFIG_CS4231A
3169 { .init_isa
= cs4231a_init
}
3177 "Yamaha YMF262 (OPL3)",
3179 "Yamaha YM3812 (OPL2)",
3183 { .init_isa
= Adlib_init
}
3190 "Gravis Ultrasound GF1",
3193 { .init_isa
= GUS_init
}
3200 "Intel 82801AA AC97 Audio",
3203 { .init_pci
= ac97_init
}
3207 #ifdef CONFIG_ES1370
3210 "ENSONIQ AudioPCI ES1370",
3213 { .init_pci
= es1370_init
}
3217 #endif /* HAS_AUDIO_CHOICE */
3219 { NULL
, NULL
, 0, 0, { NULL
} }
3222 static void select_soundhw (const char *optarg
)
3226 if (*optarg
== '?') {
3229 printf ("Valid sound card names (comma separated):\n");
3230 for (c
= soundhw
; c
->name
; ++c
) {
3231 printf ("%-11s %s\n", c
->name
, c
->descr
);
3233 printf ("\n-soundhw all will enable all of the above\n");
3234 exit (*optarg
!= '?');
3242 if (!strcmp (optarg
, "all")) {
3243 for (c
= soundhw
; c
->name
; ++c
) {
3251 e
= strchr (p
, ',');
3252 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3254 for (c
= soundhw
; c
->name
; ++c
) {
3255 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3264 "Unknown sound card name (too big to show)\n");
3267 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3272 p
+= l
+ (e
!= NULL
);
3276 goto show_valid_cards
;
3281 static void select_vgahw (const char *p
)
3286 vga_interface_type
= VGA_NONE
;
3287 if (strstart(p
, "std", &opts
)) {
3288 vga_interface_type
= VGA_STD
;
3289 } else if (strstart(p
, "cirrus", &opts
)) {
3290 vga_interface_type
= VGA_CIRRUS
;
3291 } else if (strstart(p
, "vmware", &opts
)) {
3292 vga_interface_type
= VGA_VMWARE
;
3293 } else if (strstart(p
, "xenfb", &opts
)) {
3294 vga_interface_type
= VGA_XENFB
;
3295 } else if (!strstart(p
, "none", &opts
)) {
3297 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3301 const char *nextopt
;
3303 if (strstart(opts
, ",retrace=", &nextopt
)) {
3305 if (strstart(opts
, "dumb", &nextopt
))
3306 vga_retrace_method
= VGA_RETRACE_DUMB
;
3307 else if (strstart(opts
, "precise", &nextopt
))
3308 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3309 else goto invalid_vga
;
3310 } else goto invalid_vga
;
3316 static int balloon_parse(const char *arg
)
3320 if (strcmp(arg
, "none") == 0) {
3324 if (!strncmp(arg
, "virtio", 6)) {
3325 if (arg
[6] == ',') {
3326 /* have params -> parse them */
3327 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3331 /* create empty opts */
3332 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3334 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3343 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3345 exit(STATUS_CONTROL_C_EXIT
);
3350 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3354 if(strlen(str
) != 36)
3357 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3358 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3359 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3365 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3373 static void termsig_handler(int signal
)
3375 qemu_system_shutdown_request();
3378 static void sigchld_handler(int signal
)
3380 waitpid(-1, NULL
, WNOHANG
);
3383 static void sighandler_setup(void)
3385 struct sigaction act
;
3387 memset(&act
, 0, sizeof(act
));
3388 act
.sa_handler
= termsig_handler
;
3389 sigaction(SIGINT
, &act
, NULL
);
3390 sigaction(SIGHUP
, &act
, NULL
);
3391 sigaction(SIGTERM
, &act
, NULL
);
3393 act
.sa_handler
= sigchld_handler
;
3394 act
.sa_flags
= SA_NOCLDSTOP
;
3395 sigaction(SIGCHLD
, &act
, NULL
);
3401 /* Look for support files in the same directory as the executable. */
3402 static char *find_datadir(const char *argv0
)
3408 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3415 while (p
!= buf
&& *p
!= '\\')
3418 if (access(buf
, R_OK
) == 0) {
3419 return qemu_strdup(buf
);
3425 /* Find a likely location for support files using the location of the binary.
3426 For installed binaries this will be "$bindir/../share/qemu". When
3427 running from the build tree this will be "$bindir/../pc-bios". */
3428 #define SHARE_SUFFIX "/share/qemu"
3429 #define BUILD_SUFFIX "/pc-bios"
3430 static char *find_datadir(const char *argv0
)
3438 #if defined(__linux__)
3441 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3447 #elif defined(__FreeBSD__)
3450 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3457 /* If we don't have any way of figuring out the actual executable
3458 location then try argv[0]. */
3460 p
= realpath(argv0
, buf
);
3468 max_len
= strlen(dir
) +
3469 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3470 res
= qemu_mallocz(max_len
);
3471 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3472 if (access(res
, R_OK
)) {
3473 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3474 if (access(res
, R_OK
)) {
3486 char *qemu_find_file(int type
, const char *name
)
3492 /* If name contains path separators then try it as a straight path. */
3493 if ((strchr(name
, '/') || strchr(name
, '\\'))
3494 && access(name
, R_OK
) == 0) {
3495 return qemu_strdup(name
);
3498 case QEMU_FILE_TYPE_BIOS
:
3501 case QEMU_FILE_TYPE_KEYMAP
:
3502 subdir
= "keymaps/";
3507 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3508 buf
= qemu_mallocz(len
);
3509 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3510 if (access(buf
, R_OK
)) {
3517 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3519 return qdev_device_help(opts
);
3522 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3526 dev
= qdev_device_add(opts
);
3532 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3534 CharDriverState
*chr
;
3536 chr
= qemu_chr_open_opts(opts
, NULL
);
3542 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3544 CharDriverState
*chr
;
3545 const char *chardev
;
3549 mode
= qemu_opt_get(opts
, "mode");
3553 if (strcmp(mode
, "readline") == 0) {
3554 flags
= MONITOR_USE_READLINE
;
3555 } else if (strcmp(mode
, "control") == 0) {
3556 flags
= MONITOR_USE_CONTROL
;
3558 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3562 if (qemu_opt_get_bool(opts
, "default", 0))
3563 flags
|= MONITOR_IS_DEFAULT
;
3565 chardev
= qemu_opt_get(opts
, "chardev");
3566 chr
= qemu_chr_find(chardev
);
3568 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3572 monitor_init(chr
, flags
);
3576 static void monitor_parse(const char *optarg
, const char *mode
)
3578 static int monitor_device_index
= 0;
3584 if (strstart(optarg
, "chardev:", &p
)) {
3585 snprintf(label
, sizeof(label
), "%s", p
);
3587 if (monitor_device_index
) {
3588 snprintf(label
, sizeof(label
), "monitor%d",
3589 monitor_device_index
);
3591 snprintf(label
, sizeof(label
), "monitor");
3594 opts
= qemu_chr_parse_compat(label
, optarg
);
3596 fprintf(stderr
, "parse error: %s\n", optarg
);
3601 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3603 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3606 qemu_opt_set(opts
, "mode", mode
);
3607 qemu_opt_set(opts
, "chardev", label
);
3609 qemu_opt_set(opts
, "default", "on");
3610 monitor_device_index
++;
3613 struct device_config
{
3615 DEV_USB
, /* -usbdevice */
3617 DEV_SERIAL
, /* -serial */
3618 DEV_PARALLEL
, /* -parallel */
3619 DEV_VIRTCON
, /* -virtioconsole */
3620 DEV_DEBUGCON
, /* -debugcon */
3622 const char *cmdline
;
3623 QTAILQ_ENTRY(device_config
) next
;
3625 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3627 static void add_device_config(int type
, const char *cmdline
)
3629 struct device_config
*conf
;
3631 conf
= qemu_mallocz(sizeof(*conf
));
3633 conf
->cmdline
= cmdline
;
3634 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3637 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3639 struct device_config
*conf
;
3642 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3643 if (conf
->type
!= type
)
3645 rc
= func(conf
->cmdline
);
3652 static int serial_parse(const char *devname
)
3654 static int index
= 0;
3657 if (strcmp(devname
, "none") == 0)
3659 if (index
== MAX_SERIAL_PORTS
) {
3660 fprintf(stderr
, "qemu: too many serial ports\n");
3663 snprintf(label
, sizeof(label
), "serial%d", index
);
3664 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3665 if (!serial_hds
[index
]) {
3666 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3667 devname
, strerror(errno
));
3674 static int parallel_parse(const char *devname
)
3676 static int index
= 0;
3679 if (strcmp(devname
, "none") == 0)
3681 if (index
== MAX_PARALLEL_PORTS
) {
3682 fprintf(stderr
, "qemu: too many parallel ports\n");
3685 snprintf(label
, sizeof(label
), "parallel%d", index
);
3686 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3687 if (!parallel_hds
[index
]) {
3688 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3689 devname
, strerror(errno
));
3696 static int virtcon_parse(const char *devname
)
3698 static int index
= 0;
3700 QemuOpts
*bus_opts
, *dev_opts
;
3702 if (strcmp(devname
, "none") == 0)
3704 if (index
== MAX_VIRTIO_CONSOLES
) {
3705 fprintf(stderr
, "qemu: too many virtio consoles\n");
3709 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3710 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3712 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3713 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3715 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3716 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3717 if (!virtcon_hds
[index
]) {
3718 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3719 devname
, strerror(errno
));
3722 qemu_opt_set(dev_opts
, "chardev", label
);
3728 static int debugcon_parse(const char *devname
)
3732 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3735 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3737 fprintf(stderr
, "qemu: already have a debugcon device\n");
3740 qemu_opt_set(opts
, "driver", "isa-debugcon");
3741 qemu_opt_set(opts
, "chardev", "debugcon");
3745 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3746 const char **poptarg
, int *poptind
)
3748 const QEMUOption
*popt
;
3749 int optind
= *poptind
;
3750 char *r
= argv
[optind
];
3753 loc_set_cmdline(argv
, optind
, 1);
3755 /* Treat --foo the same as -foo. */
3758 popt
= qemu_options
;
3761 error_report("invalid option");
3764 if (!strcmp(popt
->name
, r
+ 1))
3768 if (popt
->flags
& HAS_ARG
) {
3769 if (optind
>= argc
) {
3770 error_report("requires an argument");
3773 optarg
= argv
[optind
++];
3774 loc_set_cmdline(argv
, optind
- 2, 2);
3785 static void qmp_add_default(void)
3789 static uint8_t null_uuid
[16];
3792 home
= getenv("HOME");
3797 if (memcmp(qemu_uuid
, null_uuid
, sizeof(null_uuid
)) == 0) {
3798 #if defined(CONFIG_UUID)
3799 uuid_generate(uuid
);
3804 memcpy(uuid
, qemu_uuid
, sizeof(qemu_uuid
));
3807 snprintf(buffer
, sizeof(buffer
), "%s/.qemu", home
);
3808 if (mkdir(buffer
, 0755) == -1 && errno
!= EEXIST
) {
3809 fprintf(stderr
, "could not open default QMP port\n");
3813 snprintf(buffer
, sizeof(buffer
), "%s/.qemu/qmp", home
);
3814 if (mkdir(buffer
, 0755) == -1 && errno
!= EEXIST
) {
3815 fprintf(stderr
, "could not open default QMP port\n");
3819 snprintf(buffer
, sizeof(buffer
),
3820 "unix:%s/.qemu/qmp/" UUID_FMT
".sock,server,nowait",
3822 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
3823 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
3824 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
3825 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
3827 monitor_parse(buffer
, "control");
3830 int main(int argc
, char **argv
, char **envp
)
3832 const char *gdbstub_dev
= NULL
;
3833 uint32_t boot_devices_bitmap
= 0;
3835 int snapshot
, linux_boot
, net_boot
;
3836 const char *icount_option
= NULL
;
3837 const char *initrd_filename
;
3838 const char *kernel_filename
, *kernel_cmdline
;
3839 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3841 DisplayChangeListener
*dcl
;
3842 int cyls
, heads
, secs
, translation
;
3843 QemuOpts
*hda_opts
= NULL
, *opts
;
3846 const char *loadvm
= NULL
;
3847 QEMUMachine
*machine
;
3848 const char *cpu_model
;
3853 const char *pid_file
= NULL
;
3854 const char *incoming
= NULL
;
3857 struct passwd
*pwd
= NULL
;
3858 const char *chroot_dir
= NULL
;
3859 const char *run_as
= NULL
;
3862 int show_vnc_port
= 0;
3865 error_set_progname(argv
[0]);
3867 exit_notifier_init();
3871 qemu_cache_utils_init(envp
);
3873 QLIST_INIT (&vm_change_state_head
);
3876 struct sigaction act
;
3877 sigfillset(&act
.sa_mask
);
3879 act
.sa_handler
= SIG_IGN
;
3880 sigaction(SIGPIPE
, &act
, NULL
);
3883 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3884 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3885 QEMU to run on a single CPU */
3890 h
= GetCurrentProcess();
3891 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3892 for(i
= 0; i
< 32; i
++) {
3893 if (mask
& (1 << i
))
3898 SetProcessAffinityMask(h
, mask
);
3904 module_call_init(MODULE_INIT_MACHINE
);
3905 machine
= find_default_machine();
3907 initrd_filename
= NULL
;
3910 kernel_filename
= NULL
;
3911 kernel_cmdline
= "";
3912 cyls
= heads
= secs
= 0;
3913 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3915 for (i
= 0; i
< MAX_NODES
; i
++) {
3917 node_cpumask
[i
] = 0;
3920 assigned_devices_index
= 0;
3928 /* first pass of option parsing */
3930 while (optind
< argc
) {
3931 if (argv
[optind
][0] != '-') {
3936 const QEMUOption
*popt
;
3938 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3939 switch (popt
->index
) {
3940 case QEMU_OPTION_nodefconfig
:
3951 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3952 fp
= fopen(fname
, "r");
3954 if (qemu_config_parse(fp
, fname
) != 0) {
3960 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3961 fp
= fopen(fname
, "r");
3963 if (qemu_config_parse(fp
, fname
) != 0) {
3969 #if defined(cpudef_setup)
3970 cpudef_setup(); /* parse cpu definitions in target config file */
3973 /* second pass of option parsing */
3978 if (argv
[optind
][0] != '-') {
3979 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3981 const QEMUOption
*popt
;
3983 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3984 switch(popt
->index
) {
3986 machine
= find_machine(optarg
);
3989 printf("Supported machines are:\n");
3990 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3992 printf("%-10s %s (alias of %s)\n",
3993 m
->alias
, m
->desc
, m
->name
);
3994 printf("%-10s %s%s\n",
3996 m
->is_default
? " (default)" : "");
3998 exit(*optarg
!= '?');
4001 case QEMU_OPTION_cpu
:
4002 /* hw initialization will check this */
4003 if (*optarg
== '?') {
4004 /* XXX: implement xxx_cpu_list for targets that still miss it */
4005 #if defined(cpu_list_id)
4006 cpu_list_id(stdout
, &fprintf
, optarg
);
4007 #elif defined(cpu_list)
4008 cpu_list(stdout
, &fprintf
); /* deprecated */
4015 case QEMU_OPTION_initrd
:
4016 initrd_filename
= optarg
;
4018 case QEMU_OPTION_hda
:
4020 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4022 hda_opts
= drive_add(optarg
, HD_ALIAS
4023 ",cyls=%d,heads=%d,secs=%d%s",
4024 0, cyls
, heads
, secs
,
4025 translation
== BIOS_ATA_TRANSLATION_LBA
?
4027 translation
== BIOS_ATA_TRANSLATION_NONE
?
4028 ",trans=none" : "");
4030 case QEMU_OPTION_hdb
:
4031 case QEMU_OPTION_hdc
:
4032 case QEMU_OPTION_hdd
:
4033 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4035 case QEMU_OPTION_drive
:
4036 drive_add(NULL
, "%s", optarg
);
4038 case QEMU_OPTION_set
:
4039 if (qemu_set_option(optarg
) != 0)
4042 case QEMU_OPTION_global
:
4043 if (qemu_global_option(optarg
) != 0)
4046 case QEMU_OPTION_mtdblock
:
4047 drive_add(optarg
, MTD_ALIAS
);
4049 case QEMU_OPTION_sd
:
4050 drive_add(optarg
, SD_ALIAS
);
4052 case QEMU_OPTION_pflash
:
4053 drive_add(optarg
, PFLASH_ALIAS
);
4055 case QEMU_OPTION_snapshot
:
4058 case QEMU_OPTION_hdachs
:
4062 cyls
= strtol(p
, (char **)&p
, 0);
4063 if (cyls
< 1 || cyls
> 16383)
4068 heads
= strtol(p
, (char **)&p
, 0);
4069 if (heads
< 1 || heads
> 16)
4074 secs
= strtol(p
, (char **)&p
, 0);
4075 if (secs
< 1 || secs
> 63)
4079 if (!strcmp(p
, "none"))
4080 translation
= BIOS_ATA_TRANSLATION_NONE
;
4081 else if (!strcmp(p
, "lba"))
4082 translation
= BIOS_ATA_TRANSLATION_LBA
;
4083 else if (!strcmp(p
, "auto"))
4084 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4087 } else if (*p
!= '\0') {
4089 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4092 if (hda_opts
!= NULL
) {
4094 snprintf(num
, sizeof(num
), "%d", cyls
);
4095 qemu_opt_set(hda_opts
, "cyls", num
);
4096 snprintf(num
, sizeof(num
), "%d", heads
);
4097 qemu_opt_set(hda_opts
, "heads", num
);
4098 snprintf(num
, sizeof(num
), "%d", secs
);
4099 qemu_opt_set(hda_opts
, "secs", num
);
4100 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4101 qemu_opt_set(hda_opts
, "trans", "lba");
4102 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4103 qemu_opt_set(hda_opts
, "trans", "none");
4107 case QEMU_OPTION_numa
:
4108 if (nb_numa_nodes
>= MAX_NODES
) {
4109 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4114 case QEMU_OPTION_nographic
:
4115 display_type
= DT_NOGRAPHIC
;
4117 #ifdef CONFIG_CURSES
4118 case QEMU_OPTION_curses
:
4119 display_type
= DT_CURSES
;
4122 case QEMU_OPTION_portrait
:
4125 case QEMU_OPTION_kernel
:
4126 kernel_filename
= optarg
;
4128 case QEMU_OPTION_append
:
4129 kernel_cmdline
= optarg
;
4131 case QEMU_OPTION_cdrom
:
4132 drive_add(optarg
, CDROM_ALIAS
);
4134 case QEMU_OPTION_boot
:
4136 static const char * const params
[] = {
4137 "order", "once", "menu", NULL
4139 char buf
[sizeof(boot_devices
)];
4140 char *standard_boot_devices
;
4143 if (!strchr(optarg
, '=')) {
4145 pstrcpy(buf
, sizeof(buf
), optarg
);
4146 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4148 "qemu: unknown boot parameter '%s' in '%s'\n",
4154 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4155 boot_devices_bitmap
= parse_bootdevices(buf
);
4156 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4159 if (get_param_value(buf
, sizeof(buf
),
4161 boot_devices_bitmap
|= parse_bootdevices(buf
);
4162 standard_boot_devices
= qemu_strdup(boot_devices
);
4163 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4164 qemu_register_reset(restore_boot_devices
,
4165 standard_boot_devices
);
4167 if (get_param_value(buf
, sizeof(buf
),
4169 if (!strcmp(buf
, "on")) {
4171 } else if (!strcmp(buf
, "off")) {
4175 "qemu: invalid option value '%s'\n",
4183 case QEMU_OPTION_fda
:
4184 case QEMU_OPTION_fdb
:
4185 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4188 case QEMU_OPTION_no_fd_bootchk
:
4192 case QEMU_OPTION_netdev
:
4193 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4197 case QEMU_OPTION_net
:
4198 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4203 case QEMU_OPTION_tftp
:
4204 legacy_tftp_prefix
= optarg
;
4206 case QEMU_OPTION_bootp
:
4207 legacy_bootp_filename
= optarg
;
4210 case QEMU_OPTION_smb
:
4211 if (net_slirp_smb(optarg
) < 0)
4215 case QEMU_OPTION_redir
:
4216 if (net_slirp_redir(optarg
) < 0)
4220 case QEMU_OPTION_bt
:
4221 add_device_config(DEV_BT
, optarg
);
4224 case QEMU_OPTION_audio_help
:
4228 case QEMU_OPTION_soundhw
:
4229 select_soundhw (optarg
);
4235 case QEMU_OPTION_version
:
4239 case QEMU_OPTION_m
: {
4243 value
= strtoul(optarg
, &ptr
, 10);
4245 case 0: case 'M': case 'm':
4252 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4256 /* On 32-bit hosts, QEMU is limited by virtual address space */
4257 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4258 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4261 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4262 fprintf(stderr
, "qemu: ram size too large\n");
4268 case QEMU_OPTION_mempath
:
4272 case QEMU_OPTION_mem_prealloc
:
4279 const CPULogItem
*item
;
4281 mask
= cpu_str_to_log_mask(optarg
);
4283 printf("Log items (comma separated):\n");
4284 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4285 printf("%-10s %s\n", item
->name
, item
->help
);
4293 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4295 case QEMU_OPTION_gdb
:
4296 gdbstub_dev
= optarg
;
4301 case QEMU_OPTION_bios
:
4304 case QEMU_OPTION_singlestep
:
4311 keyboard_layout
= optarg
;
4313 case QEMU_OPTION_localtime
:
4316 case QEMU_OPTION_vga
:
4317 select_vgahw (optarg
);
4319 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4325 w
= strtol(p
, (char **)&p
, 10);
4328 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4334 h
= strtol(p
, (char **)&p
, 10);
4339 depth
= strtol(p
, (char **)&p
, 10);
4340 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4341 depth
!= 24 && depth
!= 32)
4343 } else if (*p
== '\0') {
4344 depth
= graphic_depth
;
4351 graphic_depth
= depth
;
4355 case QEMU_OPTION_echr
:
4358 term_escape_char
= strtol(optarg
, &r
, 0);
4360 printf("Bad argument to echr\n");
4363 case QEMU_OPTION_monitor
:
4364 monitor_parse(optarg
, "readline");
4365 default_monitor
= 0;
4367 case QEMU_OPTION_qmp
:
4368 monitor_parse(optarg
, "control");
4371 case QEMU_OPTION_mon
:
4372 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4374 fprintf(stderr
, "parse error: %s\n", optarg
);
4378 mode
= qemu_opt_get(opts
, "mode");
4379 if (mode
== NULL
|| strcmp(mode
, "readline") == 0) {
4380 default_monitor
= 0;
4381 } else if (strcmp(mode
, "control") == 0) {
4386 case QEMU_OPTION_chardev
:
4387 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4389 fprintf(stderr
, "parse error: %s\n", optarg
);
4393 case QEMU_OPTION_serial
:
4394 add_device_config(DEV_SERIAL
, optarg
);
4396 if (strncmp(optarg
, "mon:", 4) == 0) {
4397 default_monitor
= 0;
4400 case QEMU_OPTION_watchdog
:
4403 "qemu: only one watchdog option may be given\n");
4408 case QEMU_OPTION_watchdog_action
:
4409 if (select_watchdog_action(optarg
) == -1) {
4410 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4414 case QEMU_OPTION_virtiocon
:
4415 add_device_config(DEV_VIRTCON
, optarg
);
4416 default_virtcon
= 0;
4417 if (strncmp(optarg
, "mon:", 4) == 0) {
4418 default_monitor
= 0;
4421 case QEMU_OPTION_parallel
:
4422 add_device_config(DEV_PARALLEL
, optarg
);
4423 default_parallel
= 0;
4424 if (strncmp(optarg
, "mon:", 4) == 0) {
4425 default_monitor
= 0;
4428 case QEMU_OPTION_debugcon
:
4429 add_device_config(DEV_DEBUGCON
, optarg
);
4431 case QEMU_OPTION_loadvm
:
4434 case QEMU_OPTION_full_screen
:
4438 case QEMU_OPTION_no_frame
:
4441 case QEMU_OPTION_alt_grab
:
4444 case QEMU_OPTION_ctrl_grab
:
4447 case QEMU_OPTION_no_quit
:
4450 case QEMU_OPTION_sdl
:
4451 display_type
= DT_SDL
;
4454 case QEMU_OPTION_pidfile
:
4458 case QEMU_OPTION_win2k_hack
:
4459 win2k_install_hack
= 1;
4461 case QEMU_OPTION_rtc_td_hack
:
4464 case QEMU_OPTION_acpitable
:
4465 if(acpi_table_add(optarg
) < 0) {
4466 fprintf(stderr
, "Wrong acpi table provided\n");
4470 case QEMU_OPTION_smbios
:
4471 if(smbios_entry_add(optarg
) < 0) {
4472 fprintf(stderr
, "Wrong smbios provided\n");
4479 case QEMU_OPTION_enable_kvm
:
4483 case QEMU_OPTION_no_kvm
:
4486 case QEMU_OPTION_no_kvm_irqchip
: {
4491 case QEMU_OPTION_no_kvm_pit
: {
4495 case QEMU_OPTION_no_kvm_pit_reinjection
: {
4496 kvm_pit_reinject
= 0;
4499 case QEMU_OPTION_enable_nesting
: {
4503 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4504 case QEMU_OPTION_pcidevice
:
4505 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
4506 fprintf(stderr
, "Too many assigned devices\n");
4509 assigned_devices
[assigned_devices_index
] = optarg
;
4510 assigned_devices_index
++;
4514 case QEMU_OPTION_usb
:
4517 case QEMU_OPTION_usbdevice
:
4519 add_device_config(DEV_USB
, optarg
);
4521 case QEMU_OPTION_device
:
4522 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4526 case QEMU_OPTION_smp
:
4529 fprintf(stderr
, "Invalid number of CPUs\n");
4532 if (max_cpus
< smp_cpus
) {
4533 fprintf(stderr
, "maxcpus must be equal to or greater than "
4537 if (max_cpus
> 255) {
4538 fprintf(stderr
, "Unsupported number of maxcpus\n");
4542 case QEMU_OPTION_vnc
:
4543 display_type
= DT_VNC
;
4544 vnc_display
= optarg
;
4547 case QEMU_OPTION_no_acpi
:
4550 case QEMU_OPTION_no_hpet
:
4553 case QEMU_OPTION_balloon
:
4554 if (balloon_parse(optarg
) < 0) {
4555 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4560 case QEMU_OPTION_no_reboot
:
4563 case QEMU_OPTION_no_shutdown
:
4566 case QEMU_OPTION_show_cursor
:
4569 case QEMU_OPTION_uuid
:
4570 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4571 fprintf(stderr
, "Fail to parse UUID string."
4572 " Wrong format.\n");
4577 case QEMU_OPTION_daemonize
:
4581 case QEMU_OPTION_option_rom
:
4582 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4583 fprintf(stderr
, "Too many option ROMs\n");
4586 option_rom
[nb_option_roms
] = optarg
;
4589 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4590 case QEMU_OPTION_semihosting
:
4591 semihosting_enabled
= 1;
4594 case QEMU_OPTION_tdf
:
4597 case QEMU_OPTION_kvm_shadow_memory
:
4598 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
4600 case QEMU_OPTION_name
:
4601 qemu_name
= qemu_strdup(optarg
);
4603 char *p
= strchr(qemu_name
, ',');
4606 if (strncmp(p
, "process=", 8)) {
4607 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4615 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4616 case QEMU_OPTION_prom_env
:
4617 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4618 fprintf(stderr
, "Too many prom variables\n");
4621 prom_envs
[nb_prom_envs
] = optarg
;
4626 case QEMU_OPTION_old_param
:
4630 case QEMU_OPTION_clock
:
4631 configure_alarms(optarg
);
4633 case QEMU_OPTION_startdate
:
4634 configure_rtc_date_offset(optarg
, 1);
4636 case QEMU_OPTION_rtc
:
4637 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4639 fprintf(stderr
, "parse error: %s\n", optarg
);
4642 configure_rtc(opts
);
4644 case QEMU_OPTION_tb_size
:
4645 tb_size
= strtol(optarg
, NULL
, 0);
4649 case QEMU_OPTION_icount
:
4650 icount_option
= optarg
;
4652 case QEMU_OPTION_incoming
:
4655 case QEMU_OPTION_nodefaults
:
4657 default_parallel
= 0;
4658 default_virtcon
= 0;
4659 default_monitor
= 0;
4668 case QEMU_OPTION_chroot
:
4669 chroot_dir
= optarg
;
4671 case QEMU_OPTION_runas
:
4674 case QEMU_OPTION_nvram
:
4679 case QEMU_OPTION_xen_domid
:
4680 xen_domid
= atoi(optarg
);
4682 case QEMU_OPTION_xen_create
:
4683 xen_mode
= XEN_CREATE
;
4685 case QEMU_OPTION_xen_attach
:
4686 xen_mode
= XEN_ATTACH
;
4689 case QEMU_OPTION_readconfig
:
4692 fp
= fopen(optarg
, "r");
4694 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4697 if (qemu_config_parse(fp
, optarg
) != 0) {
4703 case QEMU_OPTION_writeconfig
:
4706 if (strcmp(optarg
, "-") == 0) {
4709 fp
= fopen(optarg
, "w");
4711 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4715 qemu_config_write(fp
);
4724 /* If no data_dir is specified then try to find it relative to the
4727 data_dir
= find_datadir(argv
[0]);
4729 /* If all else fails use the install patch specified when building. */
4731 data_dir
= CONFIG_QEMU_SHAREDIR
;
4735 * Default to max_cpus = smp_cpus, in case the user doesn't
4736 * specify a max_cpus value.
4739 max_cpus
= smp_cpus
;
4741 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4742 if (smp_cpus
> machine
->max_cpus
) {
4743 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4744 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4749 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4750 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4752 if (machine
->no_serial
) {
4755 if (machine
->no_parallel
) {
4756 default_parallel
= 0;
4758 if (!machine
->use_virtcon
) {
4759 default_virtcon
= 0;
4761 if (machine
->no_vga
) {
4764 if (machine
->no_floppy
) {
4767 if (machine
->no_cdrom
) {
4770 if (machine
->no_sdcard
) {
4774 if (display_type
== DT_NOGRAPHIC
) {
4775 if (default_parallel
)
4776 add_device_config(DEV_PARALLEL
, "null");
4777 if (default_serial
&& default_monitor
) {
4778 add_device_config(DEV_SERIAL
, "mon:stdio");
4779 } else if (default_virtcon
&& default_monitor
) {
4780 add_device_config(DEV_VIRTCON
, "mon:stdio");
4783 add_device_config(DEV_SERIAL
, "stdio");
4784 if (default_virtcon
)
4785 add_device_config(DEV_VIRTCON
, "stdio");
4786 if (default_monitor
)
4787 monitor_parse("stdio", "readline");
4791 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4792 if (default_parallel
)
4793 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4794 if (default_monitor
)
4795 monitor_parse("vc:80Cx24C", "readline");
4796 if (default_virtcon
)
4797 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4803 vga_interface_type
= VGA_CIRRUS
;
4805 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4812 if (pipe(fds
) == -1)
4823 len
= read(fds
[0], &status
, 1);
4824 if (len
== -1 && (errno
== EINTR
))
4829 else if (status
== 1) {
4830 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4838 qemu_set_cloexec(fds
[1]);
4850 signal(SIGTSTP
, SIG_IGN
);
4851 signal(SIGTTOU
, SIG_IGN
);
4852 signal(SIGTTIN
, SIG_IGN
);
4856 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4860 if (write(fds
[1], &status
, 1) != 1) {
4861 perror("daemonize. Writing to pipe\n");
4865 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4869 if (kvm_enabled()) {
4872 ret
= kvm_init(smp_cpus
);
4874 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
4875 fprintf(stderr
, "failed to initialize KVM\n");
4879 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
4885 if (qemu_init_main_loop()) {
4886 fprintf(stderr
, "qemu_init_main_loop failed\n");
4889 linux_boot
= (kernel_filename
!= NULL
);
4891 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4892 fprintf(stderr
, "-append only allowed with -kernel option\n");
4896 if (!linux_boot
&& initrd_filename
!= NULL
) {
4897 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4902 /* Win32 doesn't support line-buffering and requires size >= 2 */
4903 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4906 if (init_timer_alarm() < 0) {
4907 fprintf(stderr
, "could not initialize alarm timer\n");
4910 configure_icount(icount_option
);
4916 if (net_init_clients() < 0) {
4920 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4921 net_set_boot_mask(net_boot
);
4923 /* init the bluetooth world */
4924 if (foreach_device_config(DEV_BT
, bt_parse
))
4927 /* init the memory */
4929 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4931 /* init the dynamic translator */
4932 cpu_exec_init_all(tb_size
* 1024 * 1024);
4934 bdrv_init_with_whitelist();
4938 if (default_cdrom
) {
4939 /* we always create the cdrom drive, even if no disk is there */
4940 drive_add(NULL
, CDROM_ALIAS
);
4943 if (default_floppy
) {
4944 /* we always create at least one floppy */
4945 drive_add(NULL
, FD_ALIAS
, 0);
4948 if (default_sdcard
) {
4949 /* we always create one sd slot, even if no card is in it */
4950 drive_add(NULL
, SD_ALIAS
);
4953 /* open the virtual block devices */
4955 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4956 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4959 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4962 if (nb_numa_nodes
> 0) {
4965 if (nb_numa_nodes
> smp_cpus
) {
4966 nb_numa_nodes
= smp_cpus
;
4969 /* If no memory size if given for any node, assume the default case
4970 * and distribute the available memory equally across all nodes
4972 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4973 if (node_mem
[i
] != 0)
4976 if (i
== nb_numa_nodes
) {
4977 uint64_t usedmem
= 0;
4979 /* On Linux, the each node's border has to be 8MB aligned,
4980 * the final node gets the rest.
4982 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4983 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4984 usedmem
+= node_mem
[i
];
4986 node_mem
[i
] = ram_size
- usedmem
;
4989 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4990 if (node_cpumask
[i
] != 0)
4993 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4994 * must cope with this anyway, because there are BIOSes out there in
4995 * real machines which also use this scheme.
4997 if (i
== nb_numa_nodes
) {
4998 for (i
= 0; i
< smp_cpus
; i
++) {
4999 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5004 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
5006 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
5008 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
5010 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
5013 module_call_init(MODULE_INIT_DEVICE
);
5015 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
5019 i
= select_watchdog(watchdog
);
5021 exit (i
== 1 ? 1 : 0);
5024 if (machine
->compat_props
) {
5025 qdev_prop_register_global_list(machine
->compat_props
);
5029 machine
->init(ram_size
, boot_devices
,
5030 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5032 cpu_synchronize_all_post_init();
5035 /* must be after terminal init, SDL library changes signal handlers */
5039 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5040 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5041 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5047 current_machine
= machine
;
5049 /* init USB devices */
5051 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5055 /* init generic devices */
5056 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5059 net_check_clients();
5061 /* just use the first displaystate for the moment */
5062 ds
= get_displaystate();
5064 if (display_type
== DT_DEFAULT
) {
5065 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5066 display_type
= DT_SDL
;
5068 display_type
= DT_VNC
;
5069 vnc_display
= "localhost:0,to=99";
5075 switch (display_type
) {
5078 #if defined(CONFIG_CURSES)
5080 curses_display_init(ds
, full_screen
);
5083 #if defined(CONFIG_SDL)
5085 sdl_display_init(ds
, full_screen
, no_frame
);
5087 #elif defined(CONFIG_COCOA)
5089 cocoa_display_init(ds
, full_screen
);
5093 vnc_display_init(ds
);
5094 if (vnc_display_open(ds
, vnc_display
) < 0)
5097 if (show_vnc_port
) {
5098 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5106 dcl
= ds
->listeners
;
5107 while (dcl
!= NULL
) {
5108 if (dcl
->dpy_refresh
!= NULL
) {
5109 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5110 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5115 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5116 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5117 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5120 text_consoles_set_display(ds
);
5122 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
5125 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5126 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5131 qdev_machine_creation_done();
5133 if (rom_load_all() != 0) {
5134 fprintf(stderr
, "rom loading failed\n");
5138 qemu_system_reset();
5140 if (load_vmstate(loadvm
) < 0) {
5146 qemu_start_incoming_migration(incoming
);
5147 } else if (autostart
) {
5157 len
= write(fds
[1], &status
, 1);
5158 if (len
== -1 && (errno
== EINTR
))
5165 perror("not able to chdir to /");
5168 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
5174 pwd
= getpwnam(run_as
);
5176 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5182 if (chroot(chroot_dir
) < 0) {
5183 fprintf(stderr
, "chroot failed\n");
5187 perror("not able to chdir to /");
5193 if (setgid(pwd
->pw_gid
) < 0) {
5194 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5197 if (setuid(pwd
->pw_uid
) < 0) {
5198 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5201 if (setuid(0) != -1) {
5202 fprintf(stderr
, "Dropping privileges failed\n");