4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
62 #include <sys/prctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_icmp.h> // must come after ip.h
76 #include <netinet/udp.h>
77 #include <netinet/tcp.h>
81 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
82 discussion about Solaris header problems */
83 extern int madvise(caddr_t
, size_t, int);
88 #if defined(__OpenBSD__)
92 #if defined(CONFIG_VDE)
93 #include <libvdeplug.h>
101 #if defined(__APPLE__) || defined(main)
103 int qemu_main(int argc
, char **argv
, char **envp
);
104 int main(int argc
, char **argv
)
106 return qemu_main(argc
, argv
, NULL
);
109 #define main qemu_main
111 #endif /* CONFIG_SDL */
115 #define main qemu_main
116 #endif /* CONFIG_COCOA */
119 #include "hw/boards.h"
121 #include "hw/pcmcia.h"
123 #include "hw/audiodev.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "block_int.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
150 #include "qemu-option.h"
151 #include "qemu-config.h"
152 #include "qemu-objects.h"
156 #include "exec-all.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
165 //#define DEBUG_SLIRP
167 #define DEFAULT_RAM_SIZE 128
169 #define MAX_VIRTIO_CONSOLES 1
171 static const char *data_dir
;
172 const char *bios_name
= NULL
;
173 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
174 to store the VM snapshots */
175 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
176 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
177 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
178 DisplayType display_type
= DT_DEFAULT
;
179 const char* keyboard_layout
= NULL
;
181 const char *mem_path
= NULL
;
183 int mem_prealloc
= 0; /* force preallocation of physical target memory */
186 NICInfo nd_table
[MAX_NICS
];
189 static int rtc_utc
= 1;
190 static int rtc_date_offset
= -1; /* -1 means no change */
191 QEMUClock
*rtc_clock
;
192 int vga_interface_type
= VGA_NONE
;
194 int graphic_width
= 1024;
195 int graphic_height
= 768;
196 int graphic_depth
= 8;
198 int graphic_width
= 800;
199 int graphic_height
= 600;
200 int graphic_depth
= 15;
202 static int full_screen
= 0;
204 static int no_frame
= 0;
207 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
208 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
209 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
211 int win2k_install_hack
= 0;
220 const char *vnc_display
;
221 int acpi_enabled
= 1;
227 int graphic_rotate
= 0;
228 uint8_t irq0override
= 1;
232 const char *watchdog
;
233 const char *option_rom
[MAX_OPTION_ROMS
];
235 int semihosting_enabled
= 0;
239 const char *qemu_name
;
242 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
243 unsigned int nb_prom_envs
= 0;
244 const char *prom_envs
[MAX_PROM_ENVS
];
249 uint64_t node_mem
[MAX_NODES
];
250 uint64_t node_cpumask
[MAX_NODES
];
252 static CPUState
*cur_cpu
;
253 static CPUState
*next_cpu
;
254 static QEMUTimer
*nographic_timer
;
256 uint8_t qemu_uuid
[16];
258 static QEMUBootSetHandler
*boot_set_handler
;
259 static void *boot_set_opaque
;
262 #define SIG_IPI (SIGRTMIN+4)
264 #define SIG_IPI SIGUSR1
267 static int default_serial
= 1;
268 static int default_parallel
= 1;
269 static int default_virtcon
= 1;
270 static int default_monitor
= 1;
271 static int default_vga
= 1;
272 static int default_floppy
= 1;
273 static int default_cdrom
= 1;
274 static int default_sdcard
= 1;
280 { .driver
= "isa-serial", .flag
= &default_serial
},
281 { .driver
= "isa-parallel", .flag
= &default_parallel
},
282 { .driver
= "isa-fdc", .flag
= &default_floppy
},
283 { .driver
= "ide-drive", .flag
= &default_cdrom
},
284 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
285 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
286 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
287 { .driver
= "VGA", .flag
= &default_vga
},
288 { .driver
= "cirrus-vga", .flag
= &default_vga
},
289 { .driver
= "vmware-svga", .flag
= &default_vga
},
292 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
294 const char *driver
= qemu_opt_get(opts
, "driver");
299 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
300 if (strcmp(default_list
[i
].driver
, driver
) != 0)
302 *(default_list
[i
].flag
) = 0;
307 /***********************************************************/
308 /* x86 ISA bus support */
310 target_phys_addr_t isa_mem_base
= 0;
313 /***********************************************************/
314 void hw_error(const char *fmt
, ...)
320 fprintf(stderr
, "qemu: hardware error: ");
321 vfprintf(stderr
, fmt
, ap
);
322 fprintf(stderr
, "\n");
323 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
324 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
326 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
328 cpu_dump_state(env
, stderr
, fprintf
, 0);
335 static void set_proc_name(const char *s
)
337 #if defined(__linux__) && defined(PR_SET_NAME)
341 name
[sizeof(name
) - 1] = 0;
342 strncpy(name
, s
, sizeof(name
));
343 /* Could rewrite argv[0] too, but that's a bit more complicated.
344 This simple way is enough for `top'. */
345 prctl(PR_SET_NAME
, name
);
352 static QEMUBalloonEvent
*qemu_balloon_event
;
353 void *qemu_balloon_event_opaque
;
355 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
357 qemu_balloon_event
= func
;
358 qemu_balloon_event_opaque
= opaque
;
361 int qemu_balloon(ram_addr_t target
, MonitorCompletion cb
, void *opaque
)
363 if (qemu_balloon_event
) {
364 qemu_balloon_event(qemu_balloon_event_opaque
, target
, cb
, opaque
);
371 int qemu_balloon_status(MonitorCompletion cb
, void *opaque
)
373 if (qemu_balloon_event
) {
374 qemu_balloon_event(qemu_balloon_event_opaque
, 0, cb
, opaque
);
382 /***********************************************************/
383 /* real time host monotonic timer */
385 /* compute with 96 bit intermediate result: (a*b)/c */
386 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
391 #ifdef HOST_WORDS_BIGENDIAN
401 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
402 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
405 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
409 /***********************************************************/
410 /* host time/date access */
411 void qemu_get_timedate(struct tm
*tm
, int offset
)
418 if (rtc_date_offset
== -1) {
422 ret
= localtime(&ti
);
424 ti
-= rtc_date_offset
;
428 memcpy(tm
, ret
, sizeof(struct tm
));
431 int qemu_timedate_diff(struct tm
*tm
)
435 if (rtc_date_offset
== -1)
437 seconds
= mktimegm(tm
);
439 seconds
= mktime(tm
);
441 seconds
= mktimegm(tm
) + rtc_date_offset
;
443 return seconds
- time(NULL
);
446 void rtc_change_mon_event(struct tm
*tm
)
450 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
451 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
452 qobject_decref(data
);
455 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
457 time_t rtc_start_date
;
460 if (!strcmp(startdate
, "now") && legacy
) {
461 rtc_date_offset
= -1;
463 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
471 } else if (sscanf(startdate
, "%d-%d-%d",
483 rtc_start_date
= mktimegm(&tm
);
484 if (rtc_start_date
== -1) {
486 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
487 "'2006-06-17T16:01:21' or '2006-06-17'\n");
490 rtc_date_offset
= time(NULL
) - rtc_start_date
;
494 static void configure_rtc(QemuOpts
*opts
)
498 value
= qemu_opt_get(opts
, "base");
500 if (!strcmp(value
, "utc")) {
502 } else if (!strcmp(value
, "localtime")) {
505 configure_rtc_date_offset(value
, 0);
508 value
= qemu_opt_get(opts
, "clock");
510 if (!strcmp(value
, "host")) {
511 rtc_clock
= host_clock
;
512 } else if (!strcmp(value
, "vm")) {
513 rtc_clock
= vm_clock
;
515 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
519 #ifdef CONFIG_TARGET_I386
520 value
= qemu_opt_get(opts
, "driftfix");
522 if (!strcmp(buf
, "slew")) {
524 } else if (!strcmp(buf
, "none")) {
527 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
535 static void socket_cleanup(void)
540 static int socket_init(void)
545 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
547 err
= WSAGetLastError();
548 fprintf(stderr
, "WSAStartup: %d\n", err
);
551 atexit(socket_cleanup
);
556 /***********************************************************/
557 /* Bluetooth support */
560 static struct HCIInfo
*hci_table
[MAX_NICS
];
562 static struct bt_vlan_s
{
563 struct bt_scatternet_s net
;
565 struct bt_vlan_s
*next
;
568 /* find or alloc a new bluetooth "VLAN" */
569 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
571 struct bt_vlan_s
**pvlan
, *vlan
;
572 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
576 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
578 pvlan
= &first_bt_vlan
;
579 while (*pvlan
!= NULL
)
580 pvlan
= &(*pvlan
)->next
;
585 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
589 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
594 static struct HCIInfo null_hci
= {
595 .cmd_send
= null_hci_send
,
596 .sco_send
= null_hci_send
,
597 .acl_send
= null_hci_send
,
598 .bdaddr_set
= null_hci_addr_set
,
601 struct HCIInfo
*qemu_next_hci(void)
603 if (cur_hci
== nb_hcis
)
606 return hci_table
[cur_hci
++];
609 static struct HCIInfo
*hci_init(const char *str
)
612 struct bt_scatternet_s
*vlan
= 0;
614 if (!strcmp(str
, "null"))
617 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
619 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
620 else if (!strncmp(str
, "hci", 3)) {
623 if (!strncmp(str
+ 3, ",vlan=", 6)) {
624 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
629 vlan
= qemu_find_bt_vlan(0);
631 return bt_new_hci(vlan
);
634 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
639 static int bt_hci_parse(const char *str
)
644 if (nb_hcis
>= MAX_NICS
) {
645 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
658 bdaddr
.b
[5] = 0x56 + nb_hcis
;
659 hci
->bdaddr_set(hci
, bdaddr
.b
);
661 hci_table
[nb_hcis
++] = hci
;
666 static void bt_vhci_add(int vlan_id
)
668 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
671 fprintf(stderr
, "qemu: warning: adding a VHCI to "
672 "an empty scatternet %i\n", vlan_id
);
674 bt_vhci_init(bt_new_hci(vlan
));
677 static struct bt_device_s
*bt_device_add(const char *opt
)
679 struct bt_scatternet_s
*vlan
;
681 char *endp
= strstr(opt
, ",vlan=");
682 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
685 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
688 vlan_id
= strtol(endp
+ 6, &endp
, 0);
690 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
695 vlan
= qemu_find_bt_vlan(vlan_id
);
698 fprintf(stderr
, "qemu: warning: adding a slave device to "
699 "an empty scatternet %i\n", vlan_id
);
701 if (!strcmp(devname
, "keyboard"))
702 return bt_keyboard_init(vlan
);
704 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
708 static int bt_parse(const char *opt
)
710 const char *endp
, *p
;
713 if (strstart(opt
, "hci", &endp
)) {
714 if (!*endp
|| *endp
== ',') {
716 if (!strstart(endp
, ",vlan=", 0))
719 return bt_hci_parse(opt
);
721 } else if (strstart(opt
, "vhci", &endp
)) {
722 if (!*endp
|| *endp
== ',') {
724 if (strstart(endp
, ",vlan=", &p
)) {
725 vlan
= strtol(p
, (char **) &endp
, 0);
727 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
731 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
740 } else if (strstart(opt
, "device:", &endp
))
741 return !bt_device_add(endp
);
743 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
747 /***********************************************************/
748 /* QEMU Block devices */
750 #define HD_ALIAS "index=%d,media=disk"
751 #define CDROM_ALIAS "index=2,media=cdrom"
752 #define FD_ALIAS "index=%d,if=floppy"
753 #define PFLASH_ALIAS "if=pflash"
754 #define MTD_ALIAS "if=mtd"
755 #define SD_ALIAS "index=0,if=sd"
757 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
764 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
767 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
769 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
770 __FUNCTION__
, optstr
);
774 qemu_opt_set(opts
, "file", file
);
778 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
782 /* seek interface, bus and unit */
784 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
785 if (dinfo
->type
== type
&&
794 DriveInfo
*drive_get_by_id(const char *id
)
798 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
799 if (strcmp(id
, dinfo
->id
))
806 int drive_get_max_bus(BlockInterfaceType type
)
812 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
813 if(dinfo
->type
== type
&&
814 dinfo
->bus
> max_bus
)
815 max_bus
= dinfo
->bus
;
820 const char *drive_get_serial(BlockDriverState
*bdrv
)
824 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
825 if (dinfo
->bdrv
== bdrv
)
826 return dinfo
->serial
;
832 BlockInterfaceErrorAction
drive_get_on_error(
833 BlockDriverState
*bdrv
, int is_read
)
837 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
838 if (dinfo
->bdrv
== bdrv
)
839 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
842 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
845 static void bdrv_format_print(void *opaque
, const char *name
)
847 fprintf(stderr
, " %s", name
);
850 void drive_uninit(DriveInfo
*dinfo
)
852 qemu_opts_del(dinfo
->opts
);
853 bdrv_delete(dinfo
->bdrv
);
854 QTAILQ_REMOVE(&drives
, dinfo
, next
);
858 static int parse_block_error_action(const char *buf
, int is_read
)
860 if (!strcmp(buf
, "ignore")) {
861 return BLOCK_ERR_IGNORE
;
862 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
863 return BLOCK_ERR_STOP_ENOSPC
;
864 } else if (!strcmp(buf
, "stop")) {
865 return BLOCK_ERR_STOP_ANY
;
866 } else if (!strcmp(buf
, "report")) {
867 return BLOCK_ERR_REPORT
;
869 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
870 buf
, is_read
? "read" : "write");
875 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
879 const char *file
= NULL
;
882 const char *mediastr
= "";
883 BlockInterfaceType type
;
884 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
886 int cyls
, heads
, secs
, translation
;
887 BlockDriver
*drv
= NULL
;
888 QEMUMachine
*machine
= opaque
;
895 int on_read_error
, on_write_error
;
902 translation
= BIOS_ATA_TRANSLATION_AUTO
;
905 if (machine
&& machine
->use_scsi
) {
907 max_devs
= MAX_SCSI_DEVS
;
908 pstrcpy(devname
, sizeof(devname
), "scsi");
911 max_devs
= MAX_IDE_DEVS
;
912 pstrcpy(devname
, sizeof(devname
), "ide");
916 /* extract parameters */
917 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
918 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
919 index
= qemu_opt_get_number(opts
, "index", -1);
921 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
922 heads
= qemu_opt_get_number(opts
, "heads", 0);
923 secs
= qemu_opt_get_number(opts
, "secs", 0);
925 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
926 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
928 file
= qemu_opt_get(opts
, "file");
929 serial
= qemu_opt_get(opts
, "serial");
931 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
932 pstrcpy(devname
, sizeof(devname
), buf
);
933 if (!strcmp(buf
, "ide")) {
935 max_devs
= MAX_IDE_DEVS
;
936 } else if (!strcmp(buf
, "scsi")) {
938 max_devs
= MAX_SCSI_DEVS
;
939 } else if (!strcmp(buf
, "floppy")) {
942 } else if (!strcmp(buf
, "pflash")) {
945 } else if (!strcmp(buf
, "mtd")) {
948 } else if (!strcmp(buf
, "sd")) {
951 } else if (!strcmp(buf
, "virtio")) {
954 } else if (!strcmp(buf
, "xen")) {
957 } else if (!strcmp(buf
, "none")) {
961 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
966 if (cyls
|| heads
|| secs
) {
967 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
968 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
971 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
972 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
975 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
976 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
981 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
984 "qemu: '%s' trans must be used with cyls,heads and secs\n",
988 if (!strcmp(buf
, "none"))
989 translation
= BIOS_ATA_TRANSLATION_NONE
;
990 else if (!strcmp(buf
, "lba"))
991 translation
= BIOS_ATA_TRANSLATION_LBA
;
992 else if (!strcmp(buf
, "auto"))
993 translation
= BIOS_ATA_TRANSLATION_AUTO
;
995 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
1000 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
1001 if (!strcmp(buf
, "disk")) {
1003 } else if (!strcmp(buf
, "cdrom")) {
1004 if (cyls
|| secs
|| heads
) {
1006 "qemu: '%s' invalid physical CHS format\n", buf
);
1009 media
= MEDIA_CDROM
;
1011 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
1016 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
1017 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
1019 else if (!strcmp(buf
, "writethrough"))
1021 else if (!strcmp(buf
, "writeback"))
1024 fprintf(stderr
, "qemu: invalid cache option\n");
1029 #ifdef CONFIG_LINUX_AIO
1030 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
1031 if (!strcmp(buf
, "threads"))
1033 else if (!strcmp(buf
, "native"))
1036 fprintf(stderr
, "qemu: invalid aio option\n");
1042 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
1043 if (strcmp(buf
, "?") == 0) {
1044 fprintf(stderr
, "qemu: Supported formats:");
1045 bdrv_iterate_format(bdrv_format_print
, NULL
);
1046 fprintf(stderr
, "\n");
1049 drv
= bdrv_find_whitelisted_format(buf
);
1051 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
1056 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
1057 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
1058 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
1059 fprintf(stderr
, "werror is no supported by this format\n");
1063 on_write_error
= parse_block_error_action(buf
, 0);
1064 if (on_write_error
< 0) {
1069 on_read_error
= BLOCK_ERR_REPORT
;
1070 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
1071 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
) {
1072 fprintf(stderr
, "rerror is no supported by this format\n");
1076 on_read_error
= parse_block_error_action(buf
, 1);
1077 if (on_read_error
< 0) {
1082 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
1083 if (type
!= IF_VIRTIO
) {
1084 fprintf(stderr
, "addr is not supported\n");
1089 /* compute bus and unit according index */
1092 if (bus_id
!= 0 || unit_id
!= -1) {
1094 "qemu: index cannot be used with bus and unit\n");
1102 unit_id
= index
% max_devs
;
1103 bus_id
= index
/ max_devs
;
1107 /* if user doesn't specify a unit_id,
1108 * try to find the first free
1111 if (unit_id
== -1) {
1113 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1115 if (max_devs
&& unit_id
>= max_devs
) {
1116 unit_id
-= max_devs
;
1124 if (max_devs
&& unit_id
>= max_devs
) {
1125 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1126 unit_id
, max_devs
- 1);
1131 * ignore multiple definitions
1134 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1141 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1142 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1143 dinfo
->id
= qemu_strdup(buf
);
1145 /* no id supplied -> create one */
1146 dinfo
->id
= qemu_mallocz(32);
1147 if (type
== IF_IDE
|| type
== IF_SCSI
)
1148 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1150 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1151 devname
, bus_id
, mediastr
, unit_id
);
1153 snprintf(dinfo
->id
, 32, "%s%s%i",
1154 devname
, mediastr
, unit_id
);
1156 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1157 dinfo
->devaddr
= devaddr
;
1159 dinfo
->bus
= bus_id
;
1160 dinfo
->unit
= unit_id
;
1161 dinfo
->on_read_error
= on_read_error
;
1162 dinfo
->on_write_error
= on_write_error
;
1165 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1166 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1176 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1177 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1181 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1186 /* FIXME: This isn't really a floppy, but it's a reasonable
1189 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1195 /* add virtio block device */
1196 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1197 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1198 qemu_opt_set(opts
, "drive", dinfo
->id
);
1200 qemu_opt_set(opts
, "addr", devaddr
);
1211 bdrv_flags
|= BDRV_O_SNAPSHOT
;
1212 cache
= 2; /* always use write-back with snapshot */
1214 if (cache
== 0) /* no caching */
1215 bdrv_flags
|= BDRV_O_NOCACHE
;
1216 else if (cache
== 2) /* write-back */
1217 bdrv_flags
|= BDRV_O_CACHE_WB
;
1220 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
1222 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
1226 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
) {
1227 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1232 * cdrom is read-only. Set it now, after above interface checking
1233 * since readonly attribute not explicitly required, so no error.
1235 if (media
== MEDIA_CDROM
) {
1238 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1240 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1241 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1242 file
, strerror(errno
));
1246 if (bdrv_key_required(dinfo
->bdrv
))
1252 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1254 QEMUMachine
*machine
= opaque
;
1255 int fatal_error
= 0;
1257 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1264 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1266 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1267 qemu_opt_set(opts
, "snapshot", "on");
1272 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1274 boot_set_handler
= func
;
1275 boot_set_opaque
= opaque
;
1278 int qemu_boot_set(const char *boot_devices
)
1280 if (!boot_set_handler
) {
1283 return boot_set_handler(boot_set_opaque
, boot_devices
);
1286 static int parse_bootdevices(char *devices
)
1288 /* We just do some generic consistency checks */
1292 for (p
= devices
; *p
!= '\0'; p
++) {
1293 /* Allowed boot devices are:
1294 * a-b: floppy disk drives
1295 * c-f: IDE disk drives
1296 * g-m: machine implementation dependant drives
1297 * n-p: network devices
1298 * It's up to each machine implementation to check if the given boot
1299 * devices match the actual hardware implementation and firmware
1302 if (*p
< 'a' || *p
> 'p') {
1303 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1306 if (bitmap
& (1 << (*p
- 'a'))) {
1307 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1310 bitmap
|= 1 << (*p
- 'a');
1315 static void restore_boot_devices(void *opaque
)
1317 char *standard_boot_devices
= opaque
;
1319 qemu_boot_set(standard_boot_devices
);
1321 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1322 qemu_free(standard_boot_devices
);
1325 static void numa_add(const char *optarg
)
1329 unsigned long long value
, endvalue
;
1332 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1333 if (!strcmp(option
, "node")) {
1334 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1335 nodenr
= nb_numa_nodes
;
1337 nodenr
= strtoull(option
, NULL
, 10);
1340 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1341 node_mem
[nodenr
] = 0;
1343 value
= strtoull(option
, &endptr
, 0);
1345 case 0: case 'M': case 'm':
1352 node_mem
[nodenr
] = value
;
1354 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1355 node_cpumask
[nodenr
] = 0;
1357 value
= strtoull(option
, &endptr
, 10);
1360 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1362 if (*endptr
== '-') {
1363 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1364 if (endvalue
>= 63) {
1367 "only 63 CPUs in NUMA mode supported.\n");
1369 value
= (2ULL << endvalue
) - (1ULL << value
);
1371 value
= 1ULL << value
;
1374 node_cpumask
[nodenr
] = value
;
1381 static void smp_parse(const char *optarg
)
1383 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1387 smp
= strtoul(optarg
, &endptr
, 10);
1388 if (endptr
!= optarg
) {
1389 if (*endptr
== ',') {
1393 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1394 sockets
= strtoull(option
, NULL
, 10);
1395 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1396 cores
= strtoull(option
, NULL
, 10);
1397 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1398 threads
= strtoull(option
, NULL
, 10);
1399 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1400 max_cpus
= strtoull(option
, NULL
, 10);
1402 /* compute missing values, prefer sockets over cores over threads */
1403 if (smp
== 0 || sockets
== 0) {
1404 sockets
= sockets
> 0 ? sockets
: 1;
1405 cores
= cores
> 0 ? cores
: 1;
1406 threads
= threads
> 0 ? threads
: 1;
1408 smp
= cores
* threads
* sockets
;
1412 threads
= threads
> 0 ? threads
: 1;
1413 cores
= smp
/ (sockets
* threads
);
1416 threads
= smp
/ (cores
* sockets
);
1421 smp_cores
= cores
> 0 ? cores
: 1;
1422 smp_threads
= threads
> 0 ? threads
: 1;
1424 max_cpus
= smp_cpus
;
1427 /***********************************************************/
1430 static int usb_device_add(const char *devname
, int is_hotplug
)
1433 USBDevice
*dev
= NULL
;
1438 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1439 dev
= usbdevice_create(devname
);
1443 /* the other ones */
1444 if (strstart(devname
, "host:", &p
)) {
1445 dev
= usb_host_device_open(p
);
1446 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1447 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1448 bt_new_hci(qemu_find_bt_vlan(0)));
1459 static int usb_device_del(const char *devname
)
1464 if (strstart(devname
, "host:", &p
))
1465 return usb_host_device_close(p
);
1470 p
= strchr(devname
, '.');
1473 bus_num
= strtoul(devname
, NULL
, 0);
1474 addr
= strtoul(p
+ 1, NULL
, 0);
1476 return usb_device_delete_addr(bus_num
, addr
);
1479 static int usb_parse(const char *cmdline
)
1482 r
= usb_device_add(cmdline
, 0);
1484 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1489 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1491 const char *devname
= qdict_get_str(qdict
, "devname");
1492 if (usb_device_add(devname
, 1) < 0) {
1493 error_report("could not add USB device '%s'", devname
);
1497 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1499 const char *devname
= qdict_get_str(qdict
, "devname");
1500 if (usb_device_del(devname
) < 0) {
1501 error_report("could not delete USB device '%s'", devname
);
1505 /***********************************************************/
1506 /* PCMCIA/Cardbus */
1508 static struct pcmcia_socket_entry_s
{
1509 PCMCIASocket
*socket
;
1510 struct pcmcia_socket_entry_s
*next
;
1511 } *pcmcia_sockets
= 0;
1513 void pcmcia_socket_register(PCMCIASocket
*socket
)
1515 struct pcmcia_socket_entry_s
*entry
;
1517 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1518 entry
->socket
= socket
;
1519 entry
->next
= pcmcia_sockets
;
1520 pcmcia_sockets
= entry
;
1523 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1525 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1527 ptr
= &pcmcia_sockets
;
1528 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1529 if (entry
->socket
== socket
) {
1535 void pcmcia_info(Monitor
*mon
)
1537 struct pcmcia_socket_entry_s
*iter
;
1539 if (!pcmcia_sockets
)
1540 monitor_printf(mon
, "No PCMCIA sockets\n");
1542 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1543 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1544 iter
->socket
->attached
? iter
->socket
->card_string
:
1548 /***********************************************************/
1551 typedef struct IOHandlerRecord
{
1553 IOCanReadHandler
*fd_read_poll
;
1555 IOHandler
*fd_write
;
1558 /* temporary data */
1560 QLIST_ENTRY(IOHandlerRecord
) next
;
1563 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1564 QLIST_HEAD_INITIALIZER(io_handlers
);
1567 /* XXX: fd_read_poll should be suppressed, but an API change is
1568 necessary in the character devices to suppress fd_can_read(). */
1569 int qemu_set_fd_handler2(int fd
,
1570 IOCanReadHandler
*fd_read_poll
,
1572 IOHandler
*fd_write
,
1575 IOHandlerRecord
*ioh
;
1577 if (!fd_read
&& !fd_write
) {
1578 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1579 if (ioh
->fd
== fd
) {
1585 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1589 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1590 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1593 ioh
->fd_read_poll
= fd_read_poll
;
1594 ioh
->fd_read
= fd_read
;
1595 ioh
->fd_write
= fd_write
;
1596 ioh
->opaque
= opaque
;
1602 int qemu_set_fd_handler(int fd
,
1604 IOHandler
*fd_write
,
1607 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1611 /***********************************************************/
1612 /* Polling handling */
1614 typedef struct PollingEntry
{
1617 struct PollingEntry
*next
;
1620 static PollingEntry
*first_polling_entry
;
1622 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1624 PollingEntry
**ppe
, *pe
;
1625 pe
= qemu_mallocz(sizeof(PollingEntry
));
1627 pe
->opaque
= opaque
;
1628 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1633 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1635 PollingEntry
**ppe
, *pe
;
1636 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1638 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1646 /***********************************************************/
1647 /* Wait objects support */
1648 typedef struct WaitObjects
{
1650 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1651 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1652 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1655 static WaitObjects wait_objects
= {0};
1657 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1659 WaitObjects
*w
= &wait_objects
;
1661 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1663 w
->events
[w
->num
] = handle
;
1664 w
->func
[w
->num
] = func
;
1665 w
->opaque
[w
->num
] = opaque
;
1670 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1673 WaitObjects
*w
= &wait_objects
;
1676 for (i
= 0; i
< w
->num
; i
++) {
1677 if (w
->events
[i
] == handle
)
1680 w
->events
[i
] = w
->events
[i
+ 1];
1681 w
->func
[i
] = w
->func
[i
+ 1];
1682 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1690 /***********************************************************/
1691 /* ram save/restore */
1693 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
1694 #define RAM_SAVE_FLAG_COMPRESS 0x02
1695 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
1696 #define RAM_SAVE_FLAG_PAGE 0x08
1697 #define RAM_SAVE_FLAG_EOS 0x10
1699 static int is_dup_page(uint8_t *page
, uint8_t ch
)
1701 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
1702 uint32_t *array
= (uint32_t *)page
;
1705 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
1706 if (array
[i
] != val
)
1713 static int ram_save_block(QEMUFile
*f
)
1715 static ram_addr_t current_addr
= 0;
1716 ram_addr_t saved_addr
= current_addr
;
1717 ram_addr_t addr
= 0;
1720 while (addr
< last_ram_offset
) {
1721 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
1724 cpu_physical_memory_reset_dirty(current_addr
,
1725 current_addr
+ TARGET_PAGE_SIZE
,
1726 MIGRATION_DIRTY_FLAG
);
1728 p
= qemu_get_ram_ptr(current_addr
);
1730 if (is_dup_page(p
, *p
)) {
1731 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
1732 qemu_put_byte(f
, *p
);
1734 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
1735 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
1741 addr
+= TARGET_PAGE_SIZE
;
1742 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
1748 static uint64_t bytes_transferred
;
1750 static ram_addr_t
ram_save_remaining(void)
1753 ram_addr_t count
= 0;
1755 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1756 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1763 uint64_t ram_bytes_remaining(void)
1765 return ram_save_remaining() * TARGET_PAGE_SIZE
;
1768 uint64_t ram_bytes_transferred(void)
1770 return bytes_transferred
;
1773 uint64_t ram_bytes_total(void)
1775 return last_ram_offset
;
1778 static int ram_save_live(Monitor
*mon
, QEMUFile
*f
, int stage
, void *opaque
)
1781 uint64_t bytes_transferred_last
;
1783 uint64_t expected_time
= 0;
1786 cpu_physical_memory_set_dirty_tracking(0);
1790 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
1791 qemu_file_set_error(f
);
1796 bytes_transferred
= 0;
1798 /* Make sure all dirty bits are set */
1799 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
1800 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
1801 cpu_physical_memory_set_dirty(addr
);
1804 /* Enable dirty memory tracking */
1805 cpu_physical_memory_set_dirty_tracking(1);
1807 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
1810 bytes_transferred_last
= bytes_transferred
;
1811 bwidth
= qemu_get_clock_ns(rt_clock
);
1813 while (!qemu_file_rate_limit(f
)) {
1816 ret
= ram_save_block(f
);
1817 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
1818 if (ret
== 0) /* no more blocks */
1822 bwidth
= qemu_get_clock_ns(rt_clock
) - bwidth
;
1823 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
1825 /* if we haven't transferred anything this round, force expected_time to a
1826 * a very high value, but without crashing */
1830 /* try transferring iterative blocks of memory */
1832 /* flush all remaining blocks regardless of rate limiting */
1833 while (ram_save_block(f
) != 0) {
1834 bytes_transferred
+= TARGET_PAGE_SIZE
;
1836 cpu_physical_memory_set_dirty_tracking(0);
1839 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
1841 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
1843 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
1846 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1851 if (version_id
!= 3)
1855 addr
= qemu_get_be64(f
);
1857 flags
= addr
& ~TARGET_PAGE_MASK
;
1858 addr
&= TARGET_PAGE_MASK
;
1860 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
1861 if (addr
!= last_ram_offset
)
1865 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
1866 uint8_t ch
= qemu_get_byte(f
);
1867 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
1870 (!kvm_enabled() || kvm_has_sync_mmu())) {
1871 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
1874 } else if (flags
& RAM_SAVE_FLAG_PAGE
) {
1875 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
1877 if (qemu_file_has_error(f
)) {
1880 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
1885 void qemu_service_io(void)
1887 qemu_notify_event();
1890 /***********************************************************/
1891 /* machine registration */
1893 static QEMUMachine
*first_machine
= NULL
;
1894 QEMUMachine
*current_machine
= NULL
;
1896 int qemu_register_machine(QEMUMachine
*m
)
1899 pm
= &first_machine
;
1907 static QEMUMachine
*find_machine(const char *name
)
1911 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1912 if (!strcmp(m
->name
, name
))
1914 if (m
->alias
&& !strcmp(m
->alias
, name
))
1920 static QEMUMachine
*find_default_machine(void)
1924 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1925 if (m
->is_default
) {
1932 /***********************************************************/
1933 /* main execution loop */
1935 static void gui_update(void *opaque
)
1937 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1938 DisplayState
*ds
= opaque
;
1939 DisplayChangeListener
*dcl
= ds
->listeners
;
1941 qemu_flush_coalesced_mmio_buffer();
1944 while (dcl
!= NULL
) {
1945 if (dcl
->gui_timer_interval
&&
1946 dcl
->gui_timer_interval
< interval
)
1947 interval
= dcl
->gui_timer_interval
;
1950 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1953 static void nographic_update(void *opaque
)
1955 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1957 qemu_flush_coalesced_mmio_buffer();
1958 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1961 void cpu_synchronize_all_states(void)
1965 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1966 cpu_synchronize_state(cpu
);
1970 void cpu_synchronize_all_post_reset(void)
1974 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1975 cpu_synchronize_post_reset(cpu
);
1979 void cpu_synchronize_all_post_init(void)
1983 for (cpu
= first_cpu
; cpu
; cpu
= cpu
->next_cpu
) {
1984 cpu_synchronize_post_init(cpu
);
1988 struct vm_change_state_entry
{
1989 VMChangeStateHandler
*cb
;
1991 QLIST_ENTRY (vm_change_state_entry
) entries
;
1994 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1996 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1999 VMChangeStateEntry
*e
;
2001 e
= qemu_mallocz(sizeof (*e
));
2005 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
2009 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
2011 QLIST_REMOVE (e
, entries
);
2015 static void vm_state_notify(int running
, int reason
)
2017 VMChangeStateEntry
*e
;
2019 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
2020 e
->cb(e
->opaque
, running
, reason
);
2024 static void resume_all_vcpus(void);
2025 static void pause_all_vcpus(void);
2032 vm_state_notify(1, 0);
2037 /* reset/shutdown handler */
2039 typedef struct QEMUResetEntry
{
2040 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
2041 QEMUResetHandler
*func
;
2045 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
2046 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
2047 static int reset_requested
;
2048 static int shutdown_requested
;
2049 static int powerdown_requested
;
2050 static int debug_requested
;
2051 static int vmstop_requested
;
2053 int qemu_shutdown_requested(void)
2055 int r
= shutdown_requested
;
2056 shutdown_requested
= 0;
2060 int qemu_reset_requested(void)
2062 int r
= reset_requested
;
2063 reset_requested
= 0;
2067 int qemu_powerdown_requested(void)
2069 int r
= powerdown_requested
;
2070 powerdown_requested
= 0;
2074 static int qemu_debug_requested(void)
2076 int r
= debug_requested
;
2077 debug_requested
= 0;
2081 static int qemu_vmstop_requested(void)
2083 int r
= vmstop_requested
;
2084 vmstop_requested
= 0;
2088 static void do_vm_stop(int reason
)
2091 cpu_disable_ticks();
2094 vm_state_notify(0, reason
);
2095 monitor_protocol_event(QEVENT_STOP
, NULL
);
2099 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2101 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2104 re
->opaque
= opaque
;
2105 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
2108 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
2112 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
2113 if (re
->func
== func
&& re
->opaque
== opaque
) {
2114 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
2121 void qemu_system_reset(void)
2123 QEMUResetEntry
*re
, *nre
;
2125 /* reset all devices */
2126 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
2127 re
->func(re
->opaque
);
2129 monitor_protocol_event(QEVENT_RESET
, NULL
);
2130 cpu_synchronize_all_post_reset();
2133 void qemu_system_reset_request(void)
2136 shutdown_requested
= 1;
2138 reset_requested
= 1;
2140 qemu_notify_event();
2143 void qemu_system_shutdown_request(void)
2145 shutdown_requested
= 1;
2146 qemu_notify_event();
2149 void qemu_system_powerdown_request(void)
2151 powerdown_requested
= 1;
2152 qemu_notify_event();
2155 static int cpu_can_run(CPUState
*env
)
2166 static int cpu_has_work(CPUState
*env
)
2174 if (qemu_cpu_has_work(env
))
2179 static int tcg_has_work(void)
2183 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2184 if (cpu_has_work(env
))
2190 static int io_thread_fd
= -1;
2192 static void qemu_event_increment(void)
2194 /* Write 8 bytes to be compatible with eventfd. */
2195 static uint64_t val
= 1;
2198 if (io_thread_fd
== -1)
2202 ret
= write(io_thread_fd
, &val
, sizeof(val
));
2203 } while (ret
< 0 && errno
== EINTR
);
2205 /* EAGAIN is fine, a read must be pending. */
2206 if (ret
< 0 && errno
!= EAGAIN
) {
2207 fprintf(stderr
, "qemu_event_increment: write() filed: %s\n",
2213 static void qemu_event_read(void *opaque
)
2215 int fd
= (unsigned long)opaque
;
2219 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
2221 len
= read(fd
, buffer
, sizeof(buffer
));
2222 } while ((len
== -1 && errno
== EINTR
) || len
== sizeof(buffer
));
2225 static int qemu_event_init(void)
2230 err
= qemu_eventfd(fds
);
2234 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
2238 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
2242 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
2243 (void *)(unsigned long)fds
[0]);
2245 io_thread_fd
= fds
[1];
2254 HANDLE qemu_event_handle
;
2256 static void dummy_event_handler(void *opaque
)
2260 static int qemu_event_init(void)
2262 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
2263 if (!qemu_event_handle
) {
2264 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
2267 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
2271 static void qemu_event_increment(void)
2273 if (!SetEvent(qemu_event_handle
)) {
2274 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
2281 #ifndef CONFIG_IOTHREAD
2282 static int qemu_init_main_loop(void)
2284 return qemu_event_init();
2287 void qemu_init_vcpu(void *_env
)
2289 CPUState
*env
= _env
;
2291 env
->nr_cores
= smp_cores
;
2292 env
->nr_threads
= smp_threads
;
2298 int qemu_cpu_self(void *env
)
2303 static void resume_all_vcpus(void)
2307 static void pause_all_vcpus(void)
2311 void qemu_cpu_kick(void *env
)
2316 void qemu_notify_event(void)
2318 CPUState
*env
= cpu_single_env
;
2320 qemu_event_increment ();
2324 if (next_cpu
&& env
!= next_cpu
) {
2329 void qemu_mutex_lock_iothread(void) {}
2330 void qemu_mutex_unlock_iothread(void) {}
2332 void vm_stop(int reason
)
2337 #else /* CONFIG_IOTHREAD */
2339 #include "qemu-thread.h"
2341 QemuMutex qemu_global_mutex
;
2342 static QemuMutex qemu_fair_mutex
;
2344 static QemuThread io_thread
;
2346 static QemuThread
*tcg_cpu_thread
;
2347 static QemuCond
*tcg_halt_cond
;
2349 static int qemu_system_ready
;
2351 static QemuCond qemu_cpu_cond
;
2353 static QemuCond qemu_system_cond
;
2354 static QemuCond qemu_pause_cond
;
2356 static void tcg_block_io_signals(void);
2357 static void kvm_block_io_signals(CPUState
*env
);
2358 static void unblock_io_signals(void);
2360 static int qemu_init_main_loop(void)
2364 ret
= qemu_event_init();
2368 qemu_cond_init(&qemu_pause_cond
);
2369 qemu_mutex_init(&qemu_fair_mutex
);
2370 qemu_mutex_init(&qemu_global_mutex
);
2371 qemu_mutex_lock(&qemu_global_mutex
);
2373 unblock_io_signals();
2374 qemu_thread_self(&io_thread
);
2379 static void qemu_wait_io_event_common(CPUState
*env
)
2384 qemu_cond_signal(&qemu_pause_cond
);
2388 static void qemu_wait_io_event(CPUState
*env
)
2390 while (!tcg_has_work())
2391 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2393 qemu_mutex_unlock(&qemu_global_mutex
);
2396 * Users of qemu_global_mutex can be starved, having no chance
2397 * to acquire it since this path will get to it first.
2398 * So use another lock to provide fairness.
2400 qemu_mutex_lock(&qemu_fair_mutex
);
2401 qemu_mutex_unlock(&qemu_fair_mutex
);
2403 qemu_mutex_lock(&qemu_global_mutex
);
2404 qemu_wait_io_event_common(env
);
2407 static void qemu_kvm_eat_signal(CPUState
*env
, int timeout
)
2414 ts
.tv_sec
= timeout
/ 1000;
2415 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
2417 sigemptyset(&waitset
);
2418 sigaddset(&waitset
, SIG_IPI
);
2420 qemu_mutex_unlock(&qemu_global_mutex
);
2421 r
= sigtimedwait(&waitset
, &siginfo
, &ts
);
2423 qemu_mutex_lock(&qemu_global_mutex
);
2425 if (r
== -1 && !(e
== EAGAIN
|| e
== EINTR
)) {
2426 fprintf(stderr
, "sigtimedwait: %s\n", strerror(e
));
2431 static void qemu_kvm_wait_io_event(CPUState
*env
)
2433 while (!cpu_has_work(env
))
2434 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
2436 qemu_kvm_eat_signal(env
, 0);
2437 qemu_wait_io_event_common(env
);
2440 static int qemu_cpu_exec(CPUState
*env
);
2442 static void *kvm_cpu_thread_fn(void *arg
)
2444 CPUState
*env
= arg
;
2446 qemu_thread_self(env
->thread
);
2450 kvm_block_io_signals(env
);
2452 /* signal CPU creation */
2453 qemu_mutex_lock(&qemu_global_mutex
);
2455 qemu_cond_signal(&qemu_cpu_cond
);
2457 /* and wait for machine initialization */
2458 while (!qemu_system_ready
)
2459 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2462 if (cpu_can_run(env
))
2464 qemu_kvm_wait_io_event(env
);
2470 static bool tcg_cpu_exec(void);
2472 static void *tcg_cpu_thread_fn(void *arg
)
2474 CPUState
*env
= arg
;
2476 tcg_block_io_signals();
2477 qemu_thread_self(env
->thread
);
2479 /* signal CPU creation */
2480 qemu_mutex_lock(&qemu_global_mutex
);
2481 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2483 qemu_cond_signal(&qemu_cpu_cond
);
2485 /* and wait for machine initialization */
2486 while (!qemu_system_ready
)
2487 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
2491 qemu_wait_io_event(cur_cpu
);
2497 void qemu_cpu_kick(void *_env
)
2499 CPUState
*env
= _env
;
2500 qemu_cond_broadcast(env
->halt_cond
);
2502 qemu_thread_signal(env
->thread
, SIG_IPI
);
2505 int qemu_cpu_self(void *_env
)
2507 CPUState
*env
= _env
;
2510 qemu_thread_self(&this);
2512 return qemu_thread_equal(&this, env
->thread
);
2515 static void cpu_signal(int sig
)
2518 cpu_exit(cpu_single_env
);
2521 static void tcg_block_io_signals(void)
2524 struct sigaction sigact
;
2527 sigaddset(&set
, SIGUSR2
);
2528 sigaddset(&set
, SIGIO
);
2529 sigaddset(&set
, SIGALRM
);
2530 sigaddset(&set
, SIGCHLD
);
2531 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2534 sigaddset(&set
, SIG_IPI
);
2535 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2537 memset(&sigact
, 0, sizeof(sigact
));
2538 sigact
.sa_handler
= cpu_signal
;
2539 sigaction(SIG_IPI
, &sigact
, NULL
);
2542 static void dummy_signal(int sig
)
2546 static void kvm_block_io_signals(CPUState
*env
)
2550 struct sigaction sigact
;
2553 sigaddset(&set
, SIGUSR2
);
2554 sigaddset(&set
, SIGIO
);
2555 sigaddset(&set
, SIGALRM
);
2556 sigaddset(&set
, SIGCHLD
);
2557 sigaddset(&set
, SIG_IPI
);
2558 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2560 pthread_sigmask(SIG_BLOCK
, NULL
, &set
);
2561 sigdelset(&set
, SIG_IPI
);
2563 memset(&sigact
, 0, sizeof(sigact
));
2564 sigact
.sa_handler
= dummy_signal
;
2565 sigaction(SIG_IPI
, &sigact
, NULL
);
2567 r
= kvm_set_signal_mask(env
, &set
);
2569 fprintf(stderr
, "kvm_set_signal_mask: %s\n", strerror(r
));
2574 static void unblock_io_signals(void)
2579 sigaddset(&set
, SIGUSR2
);
2580 sigaddset(&set
, SIGIO
);
2581 sigaddset(&set
, SIGALRM
);
2582 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
2585 sigaddset(&set
, SIG_IPI
);
2586 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
2589 static void qemu_signal_lock(unsigned int msecs
)
2591 qemu_mutex_lock(&qemu_fair_mutex
);
2593 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
2594 qemu_thread_signal(tcg_cpu_thread
, SIG_IPI
);
2595 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
2598 qemu_mutex_unlock(&qemu_fair_mutex
);
2601 void qemu_mutex_lock_iothread(void)
2603 if (kvm_enabled()) {
2604 qemu_mutex_lock(&qemu_fair_mutex
);
2605 qemu_mutex_lock(&qemu_global_mutex
);
2606 qemu_mutex_unlock(&qemu_fair_mutex
);
2608 qemu_signal_lock(100);
2611 void qemu_mutex_unlock_iothread(void)
2613 qemu_mutex_unlock(&qemu_global_mutex
);
2616 static int all_vcpus_paused(void)
2618 CPUState
*penv
= first_cpu
;
2623 penv
= (CPUState
*)penv
->next_cpu
;
2629 static void pause_all_vcpus(void)
2631 CPUState
*penv
= first_cpu
;
2635 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2636 qemu_cpu_kick(penv
);
2637 penv
= (CPUState
*)penv
->next_cpu
;
2640 while (!all_vcpus_paused()) {
2641 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
2644 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2645 penv
= (CPUState
*)penv
->next_cpu
;
2650 static void resume_all_vcpus(void)
2652 CPUState
*penv
= first_cpu
;
2657 qemu_thread_signal(penv
->thread
, SIG_IPI
);
2658 qemu_cpu_kick(penv
);
2659 penv
= (CPUState
*)penv
->next_cpu
;
2663 static void tcg_init_vcpu(void *_env
)
2665 CPUState
*env
= _env
;
2666 /* share a single thread for all cpus with TCG */
2667 if (!tcg_cpu_thread
) {
2668 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2669 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2670 qemu_cond_init(env
->halt_cond
);
2671 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
2672 while (env
->created
== 0)
2673 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2674 tcg_cpu_thread
= env
->thread
;
2675 tcg_halt_cond
= env
->halt_cond
;
2677 env
->thread
= tcg_cpu_thread
;
2678 env
->halt_cond
= tcg_halt_cond
;
2682 static void kvm_start_vcpu(CPUState
*env
)
2684 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
2685 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
2686 qemu_cond_init(env
->halt_cond
);
2687 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
2688 while (env
->created
== 0)
2689 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
2692 void qemu_init_vcpu(void *_env
)
2694 CPUState
*env
= _env
;
2696 env
->nr_cores
= smp_cores
;
2697 env
->nr_threads
= smp_threads
;
2699 kvm_start_vcpu(env
);
2704 void qemu_notify_event(void)
2706 qemu_event_increment();
2709 static void qemu_system_vmstop_request(int reason
)
2711 vmstop_requested
= reason
;
2712 qemu_notify_event();
2715 void vm_stop(int reason
)
2718 qemu_thread_self(&me
);
2720 if (!qemu_thread_equal(&me
, &io_thread
)) {
2721 qemu_system_vmstop_request(reason
);
2723 * FIXME: should not return to device code in case
2724 * vm_stop() has been requested.
2726 if (cpu_single_env
) {
2727 cpu_exit(cpu_single_env
);
2728 cpu_single_env
->stop
= 1;
2739 static void host_main_loop_wait(int *timeout
)
2745 /* XXX: need to suppress polling by better using win32 events */
2747 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
2748 ret
|= pe
->func(pe
->opaque
);
2752 WaitObjects
*w
= &wait_objects
;
2754 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
2755 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
2756 if (w
->func
[ret
- WAIT_OBJECT_0
])
2757 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
2759 /* Check for additional signaled events */
2760 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
2762 /* Check if event is signaled */
2763 ret2
= WaitForSingleObject(w
->events
[i
], 0);
2764 if(ret2
== WAIT_OBJECT_0
) {
2766 w
->func
[i
](w
->opaque
[i
]);
2767 } else if (ret2
== WAIT_TIMEOUT
) {
2769 err
= GetLastError();
2770 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
2773 } else if (ret
== WAIT_TIMEOUT
) {
2775 err
= GetLastError();
2776 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
2783 static void host_main_loop_wait(int *timeout
)
2788 void main_loop_wait(int nonblocking
)
2790 IOHandlerRecord
*ioh
;
2791 fd_set rfds
, wfds
, xfds
;
2799 timeout
= qemu_calculate_timeout();
2800 qemu_bh_update_timeout(&timeout
);
2803 host_main_loop_wait(&timeout
);
2805 /* poll any events */
2806 /* XXX: separate device handlers from system ones */
2811 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
2815 (!ioh
->fd_read_poll
||
2816 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
2817 FD_SET(ioh
->fd
, &rfds
);
2821 if (ioh
->fd_write
) {
2822 FD_SET(ioh
->fd
, &wfds
);
2828 tv
.tv_sec
= timeout
/ 1000;
2829 tv
.tv_usec
= (timeout
% 1000) * 1000;
2831 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2833 qemu_mutex_unlock_iothread();
2834 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2835 qemu_mutex_lock_iothread();
2837 IOHandlerRecord
*pioh
;
2839 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
2841 QLIST_REMOVE(ioh
, next
);
2845 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
2846 ioh
->fd_read(ioh
->opaque
);
2848 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
2849 ioh
->fd_write(ioh
->opaque
);
2854 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
2856 qemu_run_all_timers();
2858 /* Check bottom-halves last in case any of the earlier events triggered
2864 static int qemu_cpu_exec(CPUState
*env
)
2867 #ifdef CONFIG_PROFILER
2871 #ifdef CONFIG_PROFILER
2872 ti
= profile_getclock();
2877 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
2878 env
->icount_decr
.u16
.low
= 0;
2879 env
->icount_extra
= 0;
2880 count
= qemu_icount_round (qemu_next_deadline());
2881 qemu_icount
+= count
;
2882 decr
= (count
> 0xffff) ? 0xffff : count
;
2884 env
->icount_decr
.u16
.low
= decr
;
2885 env
->icount_extra
= count
;
2887 ret
= cpu_exec(env
);
2888 #ifdef CONFIG_PROFILER
2889 qemu_time
+= profile_getclock() - ti
;
2892 /* Fold pending instructions back into the
2893 instruction counter, and clear the interrupt flag. */
2894 qemu_icount
-= (env
->icount_decr
.u16
.low
2895 + env
->icount_extra
);
2896 env
->icount_decr
.u32
= 0;
2897 env
->icount_extra
= 0;
2902 static bool tcg_cpu_exec(void)
2906 if (next_cpu
== NULL
)
2907 next_cpu
= first_cpu
;
2908 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
2909 CPUState
*env
= cur_cpu
= next_cpu
;
2911 qemu_clock_enable(vm_clock
,
2912 (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
) == 0);
2914 if (qemu_alarm_pending())
2916 if (cpu_can_run(env
))
2917 ret
= qemu_cpu_exec(env
);
2921 if (ret
== EXCP_DEBUG
) {
2922 gdb_set_stop_cpu(env
);
2923 debug_requested
= 1;
2927 return tcg_has_work();
2930 static int vm_can_run(void)
2932 if (powerdown_requested
)
2934 if (reset_requested
)
2936 if (shutdown_requested
)
2938 if (debug_requested
)
2943 qemu_irq qemu_system_powerdown
;
2945 static void main_loop(void)
2949 #ifdef CONFIG_IOTHREAD
2950 qemu_system_ready
= 1;
2951 qemu_cond_broadcast(&qemu_system_cond
);
2956 bool nonblocking
= false;
2957 #ifdef CONFIG_PROFILER
2960 #ifndef CONFIG_IOTHREAD
2961 nonblocking
= tcg_cpu_exec();
2963 #ifdef CONFIG_PROFILER
2964 ti
= profile_getclock();
2966 main_loop_wait(nonblocking
);
2967 #ifdef CONFIG_PROFILER
2968 dev_time
+= profile_getclock() - ti
;
2970 } while (vm_can_run());
2972 if (qemu_debug_requested()) {
2973 vm_stop(EXCP_DEBUG
);
2975 if (qemu_shutdown_requested()) {
2976 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
2983 if (qemu_reset_requested()) {
2985 qemu_system_reset();
2988 if (qemu_powerdown_requested()) {
2989 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
2990 qemu_irq_raise(qemu_system_powerdown
);
2992 if ((r
= qemu_vmstop_requested())) {
2999 static void version(void)
3001 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3004 static void help(int exitcode
)
3006 const char *options_help
=
3007 #define DEF(option, opt_arg, opt_enum, opt_help) \
3009 #define DEFHEADING(text) stringify(text) "\n"
3010 #include "qemu-options.h"
3016 printf("usage: %s [options] [disk_image]\n"
3018 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3021 "During emulation, the following keys are useful:\n"
3022 "ctrl-alt-f toggle full screen\n"
3023 "ctrl-alt-n switch to virtual console 'n'\n"
3024 "ctrl-alt toggle mouse and keyboard grab\n"
3026 "When using -nographic, press 'ctrl-a h' to get some help.\n",
3032 #define HAS_ARG 0x0001
3035 #define DEF(option, opt_arg, opt_enum, opt_help) \
3037 #define DEFHEADING(text)
3038 #include "qemu-options.h"
3044 typedef struct QEMUOption
{
3050 static const QEMUOption qemu_options
[] = {
3051 { "h", 0, QEMU_OPTION_h
},
3052 #define DEF(option, opt_arg, opt_enum, opt_help) \
3053 { option, opt_arg, opt_enum },
3054 #define DEFHEADING(text)
3055 #include "qemu-options.h"
3063 struct soundhw soundhw
[] = {
3064 #ifdef HAS_AUDIO_CHOICE
3065 #if defined(TARGET_I386) || defined(TARGET_MIPS)
3071 { .init_isa
= pcspk_audio_init
}
3078 "Creative Sound Blaster 16",
3081 { .init_isa
= SB16_init
}
3085 #ifdef CONFIG_CS4231A
3091 { .init_isa
= cs4231a_init
}
3099 "Yamaha YMF262 (OPL3)",
3101 "Yamaha YM3812 (OPL2)",
3105 { .init_isa
= Adlib_init
}
3112 "Gravis Ultrasound GF1",
3115 { .init_isa
= GUS_init
}
3122 "Intel 82801AA AC97 Audio",
3125 { .init_pci
= ac97_init
}
3129 #ifdef CONFIG_ES1370
3132 "ENSONIQ AudioPCI ES1370",
3135 { .init_pci
= es1370_init
}
3139 #endif /* HAS_AUDIO_CHOICE */
3141 { NULL
, NULL
, 0, 0, { NULL
} }
3144 static void select_soundhw (const char *optarg
)
3148 if (*optarg
== '?') {
3151 printf ("Valid sound card names (comma separated):\n");
3152 for (c
= soundhw
; c
->name
; ++c
) {
3153 printf ("%-11s %s\n", c
->name
, c
->descr
);
3155 printf ("\n-soundhw all will enable all of the above\n");
3156 exit (*optarg
!= '?');
3164 if (!strcmp (optarg
, "all")) {
3165 for (c
= soundhw
; c
->name
; ++c
) {
3173 e
= strchr (p
, ',');
3174 l
= !e
? strlen (p
) : (size_t) (e
- p
);
3176 for (c
= soundhw
; c
->name
; ++c
) {
3177 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
3186 "Unknown sound card name (too big to show)\n");
3189 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
3194 p
+= l
+ (e
!= NULL
);
3198 goto show_valid_cards
;
3203 static void select_vgahw (const char *p
)
3208 vga_interface_type
= VGA_NONE
;
3209 if (strstart(p
, "std", &opts
)) {
3210 vga_interface_type
= VGA_STD
;
3211 } else if (strstart(p
, "cirrus", &opts
)) {
3212 vga_interface_type
= VGA_CIRRUS
;
3213 } else if (strstart(p
, "vmware", &opts
)) {
3214 vga_interface_type
= VGA_VMWARE
;
3215 } else if (strstart(p
, "xenfb", &opts
)) {
3216 vga_interface_type
= VGA_XENFB
;
3217 } else if (!strstart(p
, "none", &opts
)) {
3219 fprintf(stderr
, "Unknown vga type: %s\n", p
);
3223 const char *nextopt
;
3225 if (strstart(opts
, ",retrace=", &nextopt
)) {
3227 if (strstart(opts
, "dumb", &nextopt
))
3228 vga_retrace_method
= VGA_RETRACE_DUMB
;
3229 else if (strstart(opts
, "precise", &nextopt
))
3230 vga_retrace_method
= VGA_RETRACE_PRECISE
;
3231 else goto invalid_vga
;
3232 } else goto invalid_vga
;
3238 static int balloon_parse(const char *arg
)
3242 if (strcmp(arg
, "none") == 0) {
3246 if (!strncmp(arg
, "virtio", 6)) {
3247 if (arg
[6] == ',') {
3248 /* have params -> parse them */
3249 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
3253 /* create empty opts */
3254 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3256 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
3265 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
3267 exit(STATUS_CONTROL_C_EXIT
);
3272 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
3276 if(strlen(str
) != 36)
3279 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
3280 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
3281 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
3287 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
3295 static void termsig_handler(int signal
)
3297 qemu_system_shutdown_request();
3300 static void sigchld_handler(int signal
)
3302 waitpid(-1, NULL
, WNOHANG
);
3305 static void sighandler_setup(void)
3307 struct sigaction act
;
3309 memset(&act
, 0, sizeof(act
));
3310 act
.sa_handler
= termsig_handler
;
3311 sigaction(SIGINT
, &act
, NULL
);
3312 sigaction(SIGHUP
, &act
, NULL
);
3313 sigaction(SIGTERM
, &act
, NULL
);
3315 act
.sa_handler
= sigchld_handler
;
3316 act
.sa_flags
= SA_NOCLDSTOP
;
3317 sigaction(SIGCHLD
, &act
, NULL
);
3323 /* Look for support files in the same directory as the executable. */
3324 static char *find_datadir(const char *argv0
)
3330 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
3337 while (p
!= buf
&& *p
!= '\\')
3340 if (access(buf
, R_OK
) == 0) {
3341 return qemu_strdup(buf
);
3347 /* Find a likely location for support files using the location of the binary.
3348 For installed binaries this will be "$bindir/../share/qemu". When
3349 running from the build tree this will be "$bindir/../pc-bios". */
3350 #define SHARE_SUFFIX "/share/qemu"
3351 #define BUILD_SUFFIX "/pc-bios"
3352 static char *find_datadir(const char *argv0
)
3360 #if defined(__linux__)
3363 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
3369 #elif defined(__FreeBSD__)
3372 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
3379 /* If we don't have any way of figuring out the actual executable
3380 location then try argv[0]. */
3382 p
= realpath(argv0
, buf
);
3390 max_len
= strlen(dir
) +
3391 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
3392 res
= qemu_mallocz(max_len
);
3393 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
3394 if (access(res
, R_OK
)) {
3395 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
3396 if (access(res
, R_OK
)) {
3408 char *qemu_find_file(int type
, const char *name
)
3414 /* If name contains path separators then try it as a straight path. */
3415 if ((strchr(name
, '/') || strchr(name
, '\\'))
3416 && access(name
, R_OK
) == 0) {
3417 return qemu_strdup(name
);
3420 case QEMU_FILE_TYPE_BIOS
:
3423 case QEMU_FILE_TYPE_KEYMAP
:
3424 subdir
= "keymaps/";
3429 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
3430 buf
= qemu_mallocz(len
);
3431 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
3432 if (access(buf
, R_OK
)) {
3439 static int device_help_func(QemuOpts
*opts
, void *opaque
)
3441 return qdev_device_help(opts
);
3444 static int device_init_func(QemuOpts
*opts
, void *opaque
)
3448 dev
= qdev_device_add(opts
);
3454 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
3456 CharDriverState
*chr
;
3458 chr
= qemu_chr_open_opts(opts
, NULL
);
3464 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
3466 CharDriverState
*chr
;
3467 const char *chardev
;
3471 mode
= qemu_opt_get(opts
, "mode");
3475 if (strcmp(mode
, "readline") == 0) {
3476 flags
= MONITOR_USE_READLINE
;
3477 } else if (strcmp(mode
, "control") == 0) {
3478 flags
= MONITOR_USE_CONTROL
;
3480 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
3484 if (qemu_opt_get_bool(opts
, "default", 0))
3485 flags
|= MONITOR_IS_DEFAULT
;
3487 chardev
= qemu_opt_get(opts
, "chardev");
3488 chr
= qemu_chr_find(chardev
);
3490 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
3494 monitor_init(chr
, flags
);
3498 static void monitor_parse(const char *optarg
, const char *mode
)
3500 static int monitor_device_index
= 0;
3506 if (strstart(optarg
, "chardev:", &p
)) {
3507 snprintf(label
, sizeof(label
), "%s", p
);
3509 if (monitor_device_index
) {
3510 snprintf(label
, sizeof(label
), "monitor%d",
3511 monitor_device_index
);
3513 snprintf(label
, sizeof(label
), "monitor");
3516 opts
= qemu_chr_parse_compat(label
, optarg
);
3518 fprintf(stderr
, "parse error: %s\n", optarg
);
3523 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
3525 fprintf(stderr
, "duplicate chardev: %s\n", label
);
3528 qemu_opt_set(opts
, "mode", mode
);
3529 qemu_opt_set(opts
, "chardev", label
);
3531 qemu_opt_set(opts
, "default", "on");
3532 monitor_device_index
++;
3535 struct device_config
{
3537 DEV_USB
, /* -usbdevice */
3539 DEV_SERIAL
, /* -serial */
3540 DEV_PARALLEL
, /* -parallel */
3541 DEV_VIRTCON
, /* -virtioconsole */
3542 DEV_DEBUGCON
, /* -debugcon */
3544 const char *cmdline
;
3545 QTAILQ_ENTRY(device_config
) next
;
3547 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
3549 static void add_device_config(int type
, const char *cmdline
)
3551 struct device_config
*conf
;
3553 conf
= qemu_mallocz(sizeof(*conf
));
3555 conf
->cmdline
= cmdline
;
3556 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
3559 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
3561 struct device_config
*conf
;
3564 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
3565 if (conf
->type
!= type
)
3567 rc
= func(conf
->cmdline
);
3574 static int serial_parse(const char *devname
)
3576 static int index
= 0;
3579 if (strcmp(devname
, "none") == 0)
3581 if (index
== MAX_SERIAL_PORTS
) {
3582 fprintf(stderr
, "qemu: too many serial ports\n");
3585 snprintf(label
, sizeof(label
), "serial%d", index
);
3586 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3587 if (!serial_hds
[index
]) {
3588 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
3589 devname
, strerror(errno
));
3596 static int parallel_parse(const char *devname
)
3598 static int index
= 0;
3601 if (strcmp(devname
, "none") == 0)
3603 if (index
== MAX_PARALLEL_PORTS
) {
3604 fprintf(stderr
, "qemu: too many parallel ports\n");
3607 snprintf(label
, sizeof(label
), "parallel%d", index
);
3608 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3609 if (!parallel_hds
[index
]) {
3610 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
3611 devname
, strerror(errno
));
3618 static int virtcon_parse(const char *devname
)
3620 static int index
= 0;
3622 QemuOpts
*bus_opts
, *dev_opts
;
3624 if (strcmp(devname
, "none") == 0)
3626 if (index
== MAX_VIRTIO_CONSOLES
) {
3627 fprintf(stderr
, "qemu: too many virtio consoles\n");
3631 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3632 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
3634 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
3635 qemu_opt_set(dev_opts
, "driver", "virtconsole");
3637 snprintf(label
, sizeof(label
), "virtcon%d", index
);
3638 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
3639 if (!virtcon_hds
[index
]) {
3640 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
3641 devname
, strerror(errno
));
3644 qemu_opt_set(dev_opts
, "chardev", label
);
3650 static int debugcon_parse(const char *devname
)
3654 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
3657 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
3659 fprintf(stderr
, "qemu: already have a debugcon device\n");
3662 qemu_opt_set(opts
, "driver", "isa-debugcon");
3663 qemu_opt_set(opts
, "chardev", "debugcon");
3667 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
3668 const char **poptarg
, int *poptind
)
3670 const QEMUOption
*popt
;
3671 int optind
= *poptind
;
3672 char *r
= argv
[optind
];
3675 loc_set_cmdline(argv
, optind
, 1);
3677 /* Treat --foo the same as -foo. */
3680 popt
= qemu_options
;
3683 error_report("invalid option");
3686 if (!strcmp(popt
->name
, r
+ 1))
3690 if (popt
->flags
& HAS_ARG
) {
3691 if (optind
>= argc
) {
3692 error_report("requires an argument");
3695 optarg
= argv
[optind
++];
3696 loc_set_cmdline(argv
, optind
- 2, 2);
3707 int main(int argc
, char **argv
, char **envp
)
3709 const char *gdbstub_dev
= NULL
;
3710 uint32_t boot_devices_bitmap
= 0;
3712 int snapshot
, linux_boot
, net_boot
;
3713 const char *icount_option
= NULL
;
3714 const char *initrd_filename
;
3715 const char *kernel_filename
, *kernel_cmdline
;
3716 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
3718 DisplayChangeListener
*dcl
;
3719 int cyls
, heads
, secs
, translation
;
3720 QemuOpts
*hda_opts
= NULL
, *opts
;
3723 const char *loadvm
= NULL
;
3724 QEMUMachine
*machine
;
3725 const char *cpu_model
;
3730 const char *pid_file
= NULL
;
3731 const char *incoming
= NULL
;
3734 struct passwd
*pwd
= NULL
;
3735 const char *chroot_dir
= NULL
;
3736 const char *run_as
= NULL
;
3739 int show_vnc_port
= 0;
3742 error_set_progname(argv
[0]);
3746 qemu_cache_utils_init(envp
);
3748 QLIST_INIT (&vm_change_state_head
);
3751 struct sigaction act
;
3752 sigfillset(&act
.sa_mask
);
3754 act
.sa_handler
= SIG_IGN
;
3755 sigaction(SIGPIPE
, &act
, NULL
);
3758 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
3759 /* Note: cpu_interrupt() is currently not SMP safe, so we force
3760 QEMU to run on a single CPU */
3765 h
= GetCurrentProcess();
3766 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
3767 for(i
= 0; i
< 32; i
++) {
3768 if (mask
& (1 << i
))
3773 SetProcessAffinityMask(h
, mask
);
3779 module_call_init(MODULE_INIT_MACHINE
);
3780 machine
= find_default_machine();
3782 initrd_filename
= NULL
;
3785 kernel_filename
= NULL
;
3786 kernel_cmdline
= "";
3787 cyls
= heads
= secs
= 0;
3788 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3790 for (i
= 0; i
< MAX_NODES
; i
++) {
3792 node_cpumask
[i
] = 0;
3801 /* first pass of option parsing */
3803 while (optind
< argc
) {
3804 if (argv
[optind
][0] != '-') {
3809 const QEMUOption
*popt
;
3811 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3812 switch (popt
->index
) {
3813 case QEMU_OPTION_nodefconfig
:
3824 fname
= CONFIG_QEMU_CONFDIR
"/qemu.conf";
3825 fp
= fopen(fname
, "r");
3827 if (qemu_config_parse(fp
, fname
) != 0) {
3833 fname
= CONFIG_QEMU_CONFDIR
"/target-" TARGET_ARCH
".conf";
3834 fp
= fopen(fname
, "r");
3836 if (qemu_config_parse(fp
, fname
) != 0) {
3842 #if defined(cpudef_setup)
3843 cpudef_setup(); /* parse cpu definitions in target config file */
3846 /* second pass of option parsing */
3851 if (argv
[optind
][0] != '-') {
3852 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
3854 const QEMUOption
*popt
;
3856 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3857 switch(popt
->index
) {
3859 machine
= find_machine(optarg
);
3862 printf("Supported machines are:\n");
3863 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3865 printf("%-10s %s (alias of %s)\n",
3866 m
->alias
, m
->desc
, m
->name
);
3867 printf("%-10s %s%s\n",
3869 m
->is_default
? " (default)" : "");
3871 exit(*optarg
!= '?');
3874 case QEMU_OPTION_cpu
:
3875 /* hw initialization will check this */
3876 if (*optarg
== '?') {
3877 /* XXX: implement xxx_cpu_list for targets that still miss it */
3878 #if defined(cpu_list_id)
3879 cpu_list_id(stdout
, &fprintf
, optarg
);
3880 #elif defined(cpu_list)
3881 cpu_list(stdout
, &fprintf
); /* deprecated */
3888 case QEMU_OPTION_initrd
:
3889 initrd_filename
= optarg
;
3891 case QEMU_OPTION_hda
:
3893 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
3895 hda_opts
= drive_add(optarg
, HD_ALIAS
3896 ",cyls=%d,heads=%d,secs=%d%s",
3897 0, cyls
, heads
, secs
,
3898 translation
== BIOS_ATA_TRANSLATION_LBA
?
3900 translation
== BIOS_ATA_TRANSLATION_NONE
?
3901 ",trans=none" : "");
3903 case QEMU_OPTION_hdb
:
3904 case QEMU_OPTION_hdc
:
3905 case QEMU_OPTION_hdd
:
3906 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
3908 case QEMU_OPTION_drive
:
3909 drive_add(NULL
, "%s", optarg
);
3911 case QEMU_OPTION_set
:
3912 if (qemu_set_option(optarg
) != 0)
3915 case QEMU_OPTION_global
:
3916 if (qemu_global_option(optarg
) != 0)
3919 case QEMU_OPTION_mtdblock
:
3920 drive_add(optarg
, MTD_ALIAS
);
3922 case QEMU_OPTION_sd
:
3923 drive_add(optarg
, SD_ALIAS
);
3925 case QEMU_OPTION_pflash
:
3926 drive_add(optarg
, PFLASH_ALIAS
);
3928 case QEMU_OPTION_snapshot
:
3931 case QEMU_OPTION_hdachs
:
3935 cyls
= strtol(p
, (char **)&p
, 0);
3936 if (cyls
< 1 || cyls
> 16383)
3941 heads
= strtol(p
, (char **)&p
, 0);
3942 if (heads
< 1 || heads
> 16)
3947 secs
= strtol(p
, (char **)&p
, 0);
3948 if (secs
< 1 || secs
> 63)
3952 if (!strcmp(p
, "none"))
3953 translation
= BIOS_ATA_TRANSLATION_NONE
;
3954 else if (!strcmp(p
, "lba"))
3955 translation
= BIOS_ATA_TRANSLATION_LBA
;
3956 else if (!strcmp(p
, "auto"))
3957 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3960 } else if (*p
!= '\0') {
3962 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3965 if (hda_opts
!= NULL
) {
3967 snprintf(num
, sizeof(num
), "%d", cyls
);
3968 qemu_opt_set(hda_opts
, "cyls", num
);
3969 snprintf(num
, sizeof(num
), "%d", heads
);
3970 qemu_opt_set(hda_opts
, "heads", num
);
3971 snprintf(num
, sizeof(num
), "%d", secs
);
3972 qemu_opt_set(hda_opts
, "secs", num
);
3973 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
3974 qemu_opt_set(hda_opts
, "trans", "lba");
3975 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
3976 qemu_opt_set(hda_opts
, "trans", "none");
3980 case QEMU_OPTION_numa
:
3981 if (nb_numa_nodes
>= MAX_NODES
) {
3982 fprintf(stderr
, "qemu: too many NUMA nodes\n");
3987 case QEMU_OPTION_nographic
:
3988 display_type
= DT_NOGRAPHIC
;
3990 #ifdef CONFIG_CURSES
3991 case QEMU_OPTION_curses
:
3992 display_type
= DT_CURSES
;
3995 case QEMU_OPTION_portrait
:
3998 case QEMU_OPTION_kernel
:
3999 kernel_filename
= optarg
;
4001 case QEMU_OPTION_append
:
4002 kernel_cmdline
= optarg
;
4004 case QEMU_OPTION_cdrom
:
4005 drive_add(optarg
, CDROM_ALIAS
);
4007 case QEMU_OPTION_boot
:
4009 static const char * const params
[] = {
4010 "order", "once", "menu", NULL
4012 char buf
[sizeof(boot_devices
)];
4013 char *standard_boot_devices
;
4016 if (!strchr(optarg
, '=')) {
4018 pstrcpy(buf
, sizeof(buf
), optarg
);
4019 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4021 "qemu: unknown boot parameter '%s' in '%s'\n",
4027 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4028 boot_devices_bitmap
= parse_bootdevices(buf
);
4029 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4032 if (get_param_value(buf
, sizeof(buf
),
4034 boot_devices_bitmap
|= parse_bootdevices(buf
);
4035 standard_boot_devices
= qemu_strdup(boot_devices
);
4036 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4037 qemu_register_reset(restore_boot_devices
,
4038 standard_boot_devices
);
4040 if (get_param_value(buf
, sizeof(buf
),
4042 if (!strcmp(buf
, "on")) {
4044 } else if (!strcmp(buf
, "off")) {
4048 "qemu: invalid option value '%s'\n",
4056 case QEMU_OPTION_fda
:
4057 case QEMU_OPTION_fdb
:
4058 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4061 case QEMU_OPTION_no_fd_bootchk
:
4065 case QEMU_OPTION_netdev
:
4066 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4070 case QEMU_OPTION_net
:
4071 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4076 case QEMU_OPTION_tftp
:
4077 legacy_tftp_prefix
= optarg
;
4079 case QEMU_OPTION_bootp
:
4080 legacy_bootp_filename
= optarg
;
4083 case QEMU_OPTION_smb
:
4084 if (net_slirp_smb(optarg
) < 0)
4088 case QEMU_OPTION_redir
:
4089 if (net_slirp_redir(optarg
) < 0)
4093 case QEMU_OPTION_bt
:
4094 add_device_config(DEV_BT
, optarg
);
4097 case QEMU_OPTION_audio_help
:
4101 case QEMU_OPTION_soundhw
:
4102 select_soundhw (optarg
);
4108 case QEMU_OPTION_version
:
4112 case QEMU_OPTION_m
: {
4116 value
= strtoul(optarg
, &ptr
, 10);
4118 case 0: case 'M': case 'm':
4125 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4129 /* On 32-bit hosts, QEMU is limited by virtual address space */
4130 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4131 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4134 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4135 fprintf(stderr
, "qemu: ram size too large\n");
4141 case QEMU_OPTION_mempath
:
4145 case QEMU_OPTION_mem_prealloc
:
4152 const CPULogItem
*item
;
4154 mask
= cpu_str_to_log_mask(optarg
);
4156 printf("Log items (comma separated):\n");
4157 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4158 printf("%-10s %s\n", item
->name
, item
->help
);
4166 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4168 case QEMU_OPTION_gdb
:
4169 gdbstub_dev
= optarg
;
4174 case QEMU_OPTION_bios
:
4177 case QEMU_OPTION_singlestep
:
4184 keyboard_layout
= optarg
;
4186 case QEMU_OPTION_localtime
:
4189 case QEMU_OPTION_vga
:
4190 select_vgahw (optarg
);
4192 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4198 w
= strtol(p
, (char **)&p
, 10);
4201 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4207 h
= strtol(p
, (char **)&p
, 10);
4212 depth
= strtol(p
, (char **)&p
, 10);
4213 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4214 depth
!= 24 && depth
!= 32)
4216 } else if (*p
== '\0') {
4217 depth
= graphic_depth
;
4224 graphic_depth
= depth
;
4228 case QEMU_OPTION_echr
:
4231 term_escape_char
= strtol(optarg
, &r
, 0);
4233 printf("Bad argument to echr\n");
4236 case QEMU_OPTION_monitor
:
4237 monitor_parse(optarg
, "readline");
4238 default_monitor
= 0;
4240 case QEMU_OPTION_qmp
:
4241 monitor_parse(optarg
, "control");
4242 default_monitor
= 0;
4244 case QEMU_OPTION_mon
:
4245 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
4247 fprintf(stderr
, "parse error: %s\n", optarg
);
4250 default_monitor
= 0;
4252 case QEMU_OPTION_chardev
:
4253 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
4255 fprintf(stderr
, "parse error: %s\n", optarg
);
4259 case QEMU_OPTION_serial
:
4260 add_device_config(DEV_SERIAL
, optarg
);
4262 if (strncmp(optarg
, "mon:", 4) == 0) {
4263 default_monitor
= 0;
4266 case QEMU_OPTION_watchdog
:
4269 "qemu: only one watchdog option may be given\n");
4274 case QEMU_OPTION_watchdog_action
:
4275 if (select_watchdog_action(optarg
) == -1) {
4276 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
4280 case QEMU_OPTION_virtiocon
:
4281 add_device_config(DEV_VIRTCON
, optarg
);
4282 default_virtcon
= 0;
4283 if (strncmp(optarg
, "mon:", 4) == 0) {
4284 default_monitor
= 0;
4287 case QEMU_OPTION_parallel
:
4288 add_device_config(DEV_PARALLEL
, optarg
);
4289 default_parallel
= 0;
4290 if (strncmp(optarg
, "mon:", 4) == 0) {
4291 default_monitor
= 0;
4294 case QEMU_OPTION_debugcon
:
4295 add_device_config(DEV_DEBUGCON
, optarg
);
4297 case QEMU_OPTION_loadvm
:
4300 case QEMU_OPTION_full_screen
:
4304 case QEMU_OPTION_no_frame
:
4307 case QEMU_OPTION_alt_grab
:
4310 case QEMU_OPTION_ctrl_grab
:
4313 case QEMU_OPTION_no_quit
:
4316 case QEMU_OPTION_sdl
:
4317 display_type
= DT_SDL
;
4320 case QEMU_OPTION_pidfile
:
4324 case QEMU_OPTION_win2k_hack
:
4325 win2k_install_hack
= 1;
4327 case QEMU_OPTION_rtc_td_hack
:
4330 case QEMU_OPTION_acpitable
:
4331 if(acpi_table_add(optarg
) < 0) {
4332 fprintf(stderr
, "Wrong acpi table provided\n");
4336 case QEMU_OPTION_smbios
:
4337 if(smbios_entry_add(optarg
) < 0) {
4338 fprintf(stderr
, "Wrong smbios provided\n");
4344 case QEMU_OPTION_enable_kvm
:
4348 case QEMU_OPTION_usb
:
4351 case QEMU_OPTION_usbdevice
:
4353 add_device_config(DEV_USB
, optarg
);
4355 case QEMU_OPTION_device
:
4356 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
4360 case QEMU_OPTION_smp
:
4363 fprintf(stderr
, "Invalid number of CPUs\n");
4366 if (max_cpus
< smp_cpus
) {
4367 fprintf(stderr
, "maxcpus must be equal to or greater than "
4371 if (max_cpus
> 255) {
4372 fprintf(stderr
, "Unsupported number of maxcpus\n");
4376 case QEMU_OPTION_vnc
:
4377 display_type
= DT_VNC
;
4378 vnc_display
= optarg
;
4381 case QEMU_OPTION_no_acpi
:
4384 case QEMU_OPTION_no_hpet
:
4387 case QEMU_OPTION_balloon
:
4388 if (balloon_parse(optarg
) < 0) {
4389 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
4394 case QEMU_OPTION_no_reboot
:
4397 case QEMU_OPTION_no_shutdown
:
4400 case QEMU_OPTION_show_cursor
:
4403 case QEMU_OPTION_uuid
:
4404 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4405 fprintf(stderr
, "Fail to parse UUID string."
4406 " Wrong format.\n");
4411 case QEMU_OPTION_daemonize
:
4415 case QEMU_OPTION_option_rom
:
4416 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4417 fprintf(stderr
, "Too many option ROMs\n");
4420 option_rom
[nb_option_roms
] = optarg
;
4423 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4424 case QEMU_OPTION_semihosting
:
4425 semihosting_enabled
= 1;
4428 case QEMU_OPTION_name
:
4429 qemu_name
= qemu_strdup(optarg
);
4431 char *p
= strchr(qemu_name
, ',');
4434 if (strncmp(p
, "process=", 8)) {
4435 fprintf(stderr
, "Unknown subargument %s to -name", p
);
4443 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4444 case QEMU_OPTION_prom_env
:
4445 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4446 fprintf(stderr
, "Too many prom variables\n");
4449 prom_envs
[nb_prom_envs
] = optarg
;
4454 case QEMU_OPTION_old_param
:
4458 case QEMU_OPTION_clock
:
4459 configure_alarms(optarg
);
4461 case QEMU_OPTION_startdate
:
4462 configure_rtc_date_offset(optarg
, 1);
4464 case QEMU_OPTION_rtc
:
4465 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
4467 fprintf(stderr
, "parse error: %s\n", optarg
);
4470 configure_rtc(opts
);
4472 case QEMU_OPTION_tb_size
:
4473 tb_size
= strtol(optarg
, NULL
, 0);
4477 case QEMU_OPTION_icount
:
4478 icount_option
= optarg
;
4480 case QEMU_OPTION_incoming
:
4483 case QEMU_OPTION_nodefaults
:
4485 default_parallel
= 0;
4486 default_virtcon
= 0;
4487 default_monitor
= 0;
4495 case QEMU_OPTION_chroot
:
4496 chroot_dir
= optarg
;
4498 case QEMU_OPTION_runas
:
4503 case QEMU_OPTION_xen_domid
:
4504 xen_domid
= atoi(optarg
);
4506 case QEMU_OPTION_xen_create
:
4507 xen_mode
= XEN_CREATE
;
4509 case QEMU_OPTION_xen_attach
:
4510 xen_mode
= XEN_ATTACH
;
4513 case QEMU_OPTION_readconfig
:
4516 fp
= fopen(optarg
, "r");
4518 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4521 if (qemu_config_parse(fp
, optarg
) != 0) {
4527 case QEMU_OPTION_writeconfig
:
4530 if (strcmp(optarg
, "-") == 0) {
4533 fp
= fopen(optarg
, "w");
4535 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
4539 qemu_config_write(fp
);
4548 /* If no data_dir is specified then try to find it relative to the
4551 data_dir
= find_datadir(argv
[0]);
4553 /* If all else fails use the install patch specified when building. */
4555 data_dir
= CONFIG_QEMU_SHAREDIR
;
4559 * Default to max_cpus = smp_cpus, in case the user doesn't
4560 * specify a max_cpus value.
4563 max_cpus
= smp_cpus
;
4565 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4566 if (smp_cpus
> machine
->max_cpus
) {
4567 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4568 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4573 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
4574 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
4576 if (machine
->no_serial
) {
4579 if (machine
->no_parallel
) {
4580 default_parallel
= 0;
4582 if (!machine
->use_virtcon
) {
4583 default_virtcon
= 0;
4585 if (machine
->no_vga
) {
4588 if (machine
->no_floppy
) {
4591 if (machine
->no_cdrom
) {
4594 if (machine
->no_sdcard
) {
4598 if (display_type
== DT_NOGRAPHIC
) {
4599 if (default_parallel
)
4600 add_device_config(DEV_PARALLEL
, "null");
4601 if (default_serial
&& default_monitor
) {
4602 add_device_config(DEV_SERIAL
, "mon:stdio");
4603 } else if (default_virtcon
&& default_monitor
) {
4604 add_device_config(DEV_VIRTCON
, "mon:stdio");
4607 add_device_config(DEV_SERIAL
, "stdio");
4608 if (default_virtcon
)
4609 add_device_config(DEV_VIRTCON
, "stdio");
4610 if (default_monitor
)
4611 monitor_parse("stdio", "readline");
4615 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4616 if (default_parallel
)
4617 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4618 if (default_monitor
)
4619 monitor_parse("vc:80Cx24C", "readline");
4620 if (default_virtcon
)
4621 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4624 vga_interface_type
= VGA_CIRRUS
;
4626 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
4633 if (pipe(fds
) == -1)
4644 len
= read(fds
[0], &status
, 1);
4645 if (len
== -1 && (errno
== EINTR
))
4650 else if (status
== 1) {
4651 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
4659 qemu_set_cloexec(fds
[1]);
4671 signal(SIGTSTP
, SIG_IGN
);
4672 signal(SIGTTOU
, SIG_IGN
);
4673 signal(SIGTTIN
, SIG_IGN
);
4677 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4681 if (write(fds
[1], &status
, 1) != 1) {
4682 perror("daemonize. Writing to pipe\n");
4686 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4690 if (kvm_enabled()) {
4693 ret
= kvm_init(smp_cpus
);
4695 fprintf(stderr
, "failed to initialize KVM\n");
4700 if (qemu_init_main_loop()) {
4701 fprintf(stderr
, "qemu_init_main_loop failed\n");
4704 linux_boot
= (kernel_filename
!= NULL
);
4706 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4707 fprintf(stderr
, "-append only allowed with -kernel option\n");
4711 if (!linux_boot
&& initrd_filename
!= NULL
) {
4712 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4717 /* Win32 doesn't support line-buffering and requires size >= 2 */
4718 setvbuf(stdout
, NULL
, _IOLBF
, 0);
4721 if (init_timer_alarm() < 0) {
4722 fprintf(stderr
, "could not initialize alarm timer\n");
4725 configure_icount(icount_option
);
4731 if (net_init_clients() < 0) {
4735 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
4736 net_set_boot_mask(net_boot
);
4738 /* init the bluetooth world */
4739 if (foreach_device_config(DEV_BT
, bt_parse
))
4742 /* init the memory */
4744 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4746 /* init the dynamic translator */
4747 cpu_exec_init_all(tb_size
* 1024 * 1024);
4749 bdrv_init_with_whitelist();
4753 if (default_cdrom
) {
4754 /* we always create the cdrom drive, even if no disk is there */
4755 drive_add(NULL
, CDROM_ALIAS
);
4758 if (default_floppy
) {
4759 /* we always create at least one floppy */
4760 drive_add(NULL
, FD_ALIAS
, 0);
4763 if (default_sdcard
) {
4764 /* we always create one sd slot, even if no card is in it */
4765 drive_add(NULL
, SD_ALIAS
);
4768 /* open the virtual block devices */
4770 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
4771 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
4774 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
4777 if (nb_numa_nodes
> 0) {
4780 if (nb_numa_nodes
> smp_cpus
) {
4781 nb_numa_nodes
= smp_cpus
;
4784 /* If no memory size if given for any node, assume the default case
4785 * and distribute the available memory equally across all nodes
4787 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4788 if (node_mem
[i
] != 0)
4791 if (i
== nb_numa_nodes
) {
4792 uint64_t usedmem
= 0;
4794 /* On Linux, the each node's border has to be 8MB aligned,
4795 * the final node gets the rest.
4797 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4798 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4799 usedmem
+= node_mem
[i
];
4801 node_mem
[i
] = ram_size
- usedmem
;
4804 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4805 if (node_cpumask
[i
] != 0)
4808 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4809 * must cope with this anyway, because there are BIOSes out there in
4810 * real machines which also use this scheme.
4812 if (i
== nb_numa_nodes
) {
4813 for (i
= 0; i
< smp_cpus
; i
++) {
4814 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
4819 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4821 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4823 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4825 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4828 module_call_init(MODULE_INIT_DEVICE
);
4830 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
4834 i
= select_watchdog(watchdog
);
4836 exit (i
== 1 ? 1 : 0);
4839 if (machine
->compat_props
) {
4840 qdev_prop_register_global_list(machine
->compat_props
);
4844 machine
->init(ram_size
, boot_devices
,
4845 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
4847 cpu_synchronize_all_post_init();
4850 /* must be after terminal init, SDL library changes signal handlers */
4854 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
4855 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4856 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
4862 current_machine
= machine
;
4864 /* init USB devices */
4866 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4870 /* init generic devices */
4871 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
4874 net_check_clients();
4876 /* just use the first displaystate for the moment */
4877 ds
= get_displaystate();
4879 if (display_type
== DT_DEFAULT
) {
4880 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4881 display_type
= DT_SDL
;
4883 display_type
= DT_VNC
;
4884 vnc_display
= "localhost:0,to=99";
4890 switch (display_type
) {
4893 #if defined(CONFIG_CURSES)
4895 curses_display_init(ds
, full_screen
);
4898 #if defined(CONFIG_SDL)
4900 sdl_display_init(ds
, full_screen
, no_frame
);
4902 #elif defined(CONFIG_COCOA)
4904 cocoa_display_init(ds
, full_screen
);
4908 vnc_display_init(ds
);
4909 if (vnc_display_open(ds
, vnc_display
) < 0)
4912 if (show_vnc_port
) {
4913 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4921 dcl
= ds
->listeners
;
4922 while (dcl
!= NULL
) {
4923 if (dcl
->dpy_refresh
!= NULL
) {
4924 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
4925 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
4930 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
4931 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
4932 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
4935 text_consoles_set_display(ds
);
4937 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0)
4940 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
4941 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
4946 qdev_machine_creation_done();
4948 if (rom_load_all() != 0) {
4949 fprintf(stderr
, "rom loading failed\n");
4953 qemu_system_reset();
4955 if (load_vmstate(loadvm
) < 0) {
4961 qemu_start_incoming_migration(incoming
);
4962 } else if (autostart
) {
4972 len
= write(fds
[1], &status
, 1);
4973 if (len
== -1 && (errno
== EINTR
))
4980 perror("not able to chdir to /");
4983 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
4989 pwd
= getpwnam(run_as
);
4991 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
4997 if (chroot(chroot_dir
) < 0) {
4998 fprintf(stderr
, "chroot failed\n");
5002 perror("not able to chdir to /");
5008 if (setgid(pwd
->pw_gid
) < 0) {
5009 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5012 if (setuid(pwd
->pw_uid
) < 0) {
5013 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5016 if (setuid(0) != -1) {
5017 fprintf(stderr
, "Dropping privileges failed\n");