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"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
54 #include <sys/sysctl.h>
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_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
80 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
81 discussion about Solaris header problems */
82 extern int madvise(caddr_t
, size_t, int);
87 #if defined(__OpenBSD__)
91 #if defined(CONFIG_VDE)
92 #include <libvdeplug.h>
100 #if defined(__APPLE__) || defined(main)
102 int qemu_main(int argc
, char **argv
, char **envp
);
103 int main(int argc
, char **argv
)
105 return qemu_main(argc
, argv
, NULL
);
108 #define main qemu_main
110 #endif /* CONFIG_SDL */
114 #define main qemu_main
115 #endif /* CONFIG_COCOA */
118 #include "hw/boards.h"
120 #include "hw/pcmcia.h"
125 #include "hw/watchdog.h"
126 #include "hw/smbios.h"
129 #include "hw/loader.h"
132 #include "net/slirp.h"
137 #include "qemu-timer.h"
138 #include "qemu-char.h"
139 #include "cache-utils.h"
141 #include "block_int.h"
142 #include "block-migration.h"
144 #include "audio/audio.h"
145 #include "migration.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-objects.h"
150 #include "qemu-options.h"
151 #include "hw/device-assignment.h"
153 #include "fsdev/qemu-fsdev.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
164 #include "arch_init.h"
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir
;
174 const char *bios_name
= NULL
;
175 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
176 to store the VM snapshots */
177 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
178 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
179 DriveInfo
*extboot_drive
= NULL
;
180 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
181 DisplayType display_type
= DT_DEFAULT
;
182 const char* keyboard_layout
= NULL
;
184 const char *mem_path
= NULL
;
186 int mem_prealloc
= 0; /* force preallocation of physical target memory */
189 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_NONE
;
196 static int full_screen
= 0;
198 static int no_frame
= 0;
201 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
202 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
203 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
204 int win2k_install_hack
= 0;
208 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
209 int assigned_devices_index
;
214 const char *vnc_display
;
215 int acpi_enabled
= 1;
223 int graphic_rotate
= 0;
224 uint8_t irq0override
= 1;
228 const char *watchdog
;
229 const char *option_rom
[MAX_OPTION_ROMS
];
231 int semihosting_enabled
= 0;
232 int time_drift_fix
= 0;
233 unsigned int kvm_shadow_memory
= 0;
235 const char *qemu_name
;
238 unsigned int nb_prom_envs
= 0;
239 const char *prom_envs
[MAX_PROM_ENVS
];
240 const char *nvram
= NULL
;
244 uint64_t node_mem
[MAX_NODES
];
245 uint64_t node_cpumask
[MAX_NODES
];
247 static QEMUTimer
*nographic_timer
;
249 uint8_t qemu_uuid
[16];
251 static QEMUBootSetHandler
*boot_set_handler
;
252 static void *boot_set_opaque
;
256 enum xen_mode xen_mode
= XEN_EMULATE
;
258 static int default_serial
= 1;
259 static int default_parallel
= 1;
260 static int default_virtcon
= 1;
261 static int default_monitor
= 1;
262 static int default_vga
= 1;
263 static int default_floppy
= 1;
264 static int default_cdrom
= 1;
265 static int default_sdcard
= 1;
271 { .driver
= "isa-serial", .flag
= &default_serial
},
272 { .driver
= "isa-parallel", .flag
= &default_parallel
},
273 { .driver
= "isa-fdc", .flag
= &default_floppy
},
274 { .driver
= "ide-drive", .flag
= &default_cdrom
},
275 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
276 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
277 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
278 { .driver
= "VGA", .flag
= &default_vga
},
279 { .driver
= "cirrus-vga", .flag
= &default_vga
},
280 { .driver
= "vmware-svga", .flag
= &default_vga
},
283 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
285 const char *driver
= qemu_opt_get(opts
, "driver");
290 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
291 if (strcmp(default_list
[i
].driver
, driver
) != 0)
293 *(default_list
[i
].flag
) = 0;
298 /***********************************************************/
300 static void set_proc_name(const char *s
)
302 #if defined(__linux__) && defined(PR_SET_NAME)
306 name
[sizeof(name
) - 1] = 0;
307 strncpy(name
, s
, sizeof(name
));
308 /* Could rewrite argv[0] too, but that's a bit more complicated.
309 This simple way is enough for `top'. */
310 prctl(PR_SET_NAME
, name
);
314 /***********************************************************/
315 /* real time host monotonic timer */
317 /* compute with 96 bit intermediate result: (a*b)/c */
318 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
323 #ifdef HOST_WORDS_BIGENDIAN
333 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
334 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
337 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
341 /***********************************************************/
342 /* host time/date access */
343 void qemu_get_timedate(struct tm
*tm
, int offset
)
350 if (rtc_date_offset
== -1) {
354 ret
= localtime(&ti
);
356 ti
-= rtc_date_offset
;
360 memcpy(tm
, ret
, sizeof(struct tm
));
363 int qemu_timedate_diff(struct tm
*tm
)
367 if (rtc_date_offset
== -1)
369 seconds
= mktimegm(tm
);
371 seconds
= mktime(tm
);
373 seconds
= mktimegm(tm
) + rtc_date_offset
;
375 return seconds
- time(NULL
);
378 void rtc_change_mon_event(struct tm
*tm
)
382 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
383 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
384 qobject_decref(data
);
387 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
389 time_t rtc_start_date
;
392 if (!strcmp(startdate
, "now") && legacy
) {
393 rtc_date_offset
= -1;
395 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
403 } else if (sscanf(startdate
, "%d-%d-%d",
415 rtc_start_date
= mktimegm(&tm
);
416 if (rtc_start_date
== -1) {
418 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
419 "'2006-06-17T16:01:21' or '2006-06-17'\n");
422 rtc_date_offset
= time(NULL
) - rtc_start_date
;
426 static void configure_rtc(QemuOpts
*opts
)
430 value
= qemu_opt_get(opts
, "base");
432 if (!strcmp(value
, "utc")) {
434 } else if (!strcmp(value
, "localtime")) {
437 configure_rtc_date_offset(value
, 0);
440 value
= qemu_opt_get(opts
, "clock");
442 if (!strcmp(value
, "host")) {
443 rtc_clock
= host_clock
;
444 } else if (!strcmp(value
, "vm")) {
445 rtc_clock
= vm_clock
;
447 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
451 value
= qemu_opt_get(opts
, "driftfix");
453 if (!strcmp(value
, "slew")) {
455 } else if (!strcmp(value
, "none")) {
458 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
464 /***********************************************************/
465 /* Bluetooth support */
468 static struct HCIInfo
*hci_table
[MAX_NICS
];
470 static struct bt_vlan_s
{
471 struct bt_scatternet_s net
;
473 struct bt_vlan_s
*next
;
476 /* find or alloc a new bluetooth "VLAN" */
477 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
479 struct bt_vlan_s
**pvlan
, *vlan
;
480 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
484 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
486 pvlan
= &first_bt_vlan
;
487 while (*pvlan
!= NULL
)
488 pvlan
= &(*pvlan
)->next
;
493 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
497 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
502 static struct HCIInfo null_hci
= {
503 .cmd_send
= null_hci_send
,
504 .sco_send
= null_hci_send
,
505 .acl_send
= null_hci_send
,
506 .bdaddr_set
= null_hci_addr_set
,
509 struct HCIInfo
*qemu_next_hci(void)
511 if (cur_hci
== nb_hcis
)
514 return hci_table
[cur_hci
++];
517 static struct HCIInfo
*hci_init(const char *str
)
520 struct bt_scatternet_s
*vlan
= 0;
522 if (!strcmp(str
, "null"))
525 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
527 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
528 else if (!strncmp(str
, "hci", 3)) {
531 if (!strncmp(str
+ 3, ",vlan=", 6)) {
532 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
537 vlan
= qemu_find_bt_vlan(0);
539 return bt_new_hci(vlan
);
542 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
547 static int bt_hci_parse(const char *str
)
552 if (nb_hcis
>= MAX_NICS
) {
553 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
566 bdaddr
.b
[5] = 0x56 + nb_hcis
;
567 hci
->bdaddr_set(hci
, bdaddr
.b
);
569 hci_table
[nb_hcis
++] = hci
;
574 static void bt_vhci_add(int vlan_id
)
576 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
579 fprintf(stderr
, "qemu: warning: adding a VHCI to "
580 "an empty scatternet %i\n", vlan_id
);
582 bt_vhci_init(bt_new_hci(vlan
));
585 static struct bt_device_s
*bt_device_add(const char *opt
)
587 struct bt_scatternet_s
*vlan
;
589 char *endp
= strstr(opt
, ",vlan=");
590 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
593 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
596 vlan_id
= strtol(endp
+ 6, &endp
, 0);
598 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
603 vlan
= qemu_find_bt_vlan(vlan_id
);
606 fprintf(stderr
, "qemu: warning: adding a slave device to "
607 "an empty scatternet %i\n", vlan_id
);
609 if (!strcmp(devname
, "keyboard"))
610 return bt_keyboard_init(vlan
);
612 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
616 static int bt_parse(const char *opt
)
618 const char *endp
, *p
;
621 if (strstart(opt
, "hci", &endp
)) {
622 if (!*endp
|| *endp
== ',') {
624 if (!strstart(endp
, ",vlan=", 0))
627 return bt_hci_parse(opt
);
629 } else if (strstart(opt
, "vhci", &endp
)) {
630 if (!*endp
|| *endp
== ',') {
632 if (strstart(endp
, ",vlan=", &p
)) {
633 vlan
= strtol(p
, (char **) &endp
, 0);
635 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
639 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
648 } else if (strstart(opt
, "device:", &endp
))
649 return !bt_device_add(endp
);
651 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
655 /***********************************************************/
656 /* QEMU Block devices */
658 #define HD_ALIAS "index=%d,media=disk"
659 #define CDROM_ALIAS "index=2,media=cdrom"
660 #define FD_ALIAS "index=%d,if=floppy"
661 #define PFLASH_ALIAS "if=pflash"
662 #define MTD_ALIAS "if=mtd"
663 #define SD_ALIAS "index=0,if=sd"
665 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
672 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
675 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
680 qemu_opt_set(opts
, "file", file
);
684 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
688 /* seek interface, bus and unit */
690 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
691 if (dinfo
->type
== type
&&
700 DriveInfo
*drive_get_by_id(const char *id
)
704 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
705 if (strcmp(id
, dinfo
->id
))
712 int drive_get_max_bus(BlockInterfaceType type
)
718 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
719 if(dinfo
->type
== type
&&
720 dinfo
->bus
> max_bus
)
721 max_bus
= dinfo
->bus
;
726 const char *drive_get_serial(BlockDriverState
*bdrv
)
730 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
731 if (dinfo
->bdrv
== bdrv
)
732 return dinfo
->serial
;
738 BlockInterfaceErrorAction
drive_get_on_error(
739 BlockDriverState
*bdrv
, int is_read
)
743 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
744 if (dinfo
->bdrv
== bdrv
)
745 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
748 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
751 static void bdrv_format_print(void *opaque
, const char *name
)
753 fprintf(stderr
, " %s", name
);
756 void drive_uninit(DriveInfo
*dinfo
)
758 qemu_opts_del(dinfo
->opts
);
759 bdrv_delete(dinfo
->bdrv
);
760 QTAILQ_REMOVE(&drives
, dinfo
, next
);
764 static int parse_block_error_action(const char *buf
, int is_read
)
766 if (!strcmp(buf
, "ignore")) {
767 return BLOCK_ERR_IGNORE
;
768 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
769 return BLOCK_ERR_STOP_ENOSPC
;
770 } else if (!strcmp(buf
, "stop")) {
771 return BLOCK_ERR_STOP_ANY
;
772 } else if (!strcmp(buf
, "report")) {
773 return BLOCK_ERR_REPORT
;
775 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
776 buf
, is_read
? "read" : "write");
781 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
785 const char *file
= NULL
;
788 const char *mediastr
= "";
789 BlockInterfaceType type
;
790 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
792 int cyls
, heads
, secs
, translation
;
793 BlockDriver
*drv
= NULL
;
794 QEMUMachine
*machine
= opaque
;
799 int on_read_error
, on_write_error
;
807 translation
= BIOS_ATA_TRANSLATION_AUTO
;
809 if (machine
&& machine
->use_scsi
) {
811 max_devs
= MAX_SCSI_DEVS
;
812 pstrcpy(devname
, sizeof(devname
), "scsi");
815 max_devs
= MAX_IDE_DEVS
;
816 pstrcpy(devname
, sizeof(devname
), "ide");
820 /* extract parameters */
821 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
822 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
823 index
= qemu_opt_get_number(opts
, "index", -1);
825 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
826 heads
= qemu_opt_get_number(opts
, "heads", 0);
827 secs
= qemu_opt_get_number(opts
, "secs", 0);
829 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
830 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
832 file
= qemu_opt_get(opts
, "file");
833 serial
= qemu_opt_get(opts
, "serial");
835 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
836 pstrcpy(devname
, sizeof(devname
), buf
);
837 if (!strcmp(buf
, "ide")) {
839 max_devs
= MAX_IDE_DEVS
;
840 } else if (!strcmp(buf
, "scsi")) {
842 max_devs
= MAX_SCSI_DEVS
;
843 } else if (!strcmp(buf
, "floppy")) {
846 } else if (!strcmp(buf
, "pflash")) {
849 } else if (!strcmp(buf
, "mtd")) {
852 } else if (!strcmp(buf
, "sd")) {
855 } else if (!strcmp(buf
, "virtio")) {
858 } else if (!strcmp(buf
, "xen")) {
861 } else if (!strcmp(buf
, "none")) {
865 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
870 if (cyls
|| heads
|| secs
) {
871 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
872 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
875 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
876 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
879 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
880 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
885 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
888 "qemu: '%s' trans must be used with cyls,heads and secs\n",
892 if (!strcmp(buf
, "none"))
893 translation
= BIOS_ATA_TRANSLATION_NONE
;
894 else if (!strcmp(buf
, "lba"))
895 translation
= BIOS_ATA_TRANSLATION_LBA
;
896 else if (!strcmp(buf
, "auto"))
897 translation
= BIOS_ATA_TRANSLATION_AUTO
;
899 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
904 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
905 if (!strcmp(buf
, "disk")) {
907 } else if (!strcmp(buf
, "cdrom")) {
908 if (cyls
|| secs
|| heads
) {
910 "qemu: '%s' invalid physical CHS format\n", buf
);
915 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
920 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
921 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
922 bdrv_flags
|= BDRV_O_NOCACHE
;
923 } else if (!strcmp(buf
, "writeback")) {
924 bdrv_flags
|= BDRV_O_CACHE_WB
;
925 } else if (!strcmp(buf
, "unsafe")) {
926 bdrv_flags
|= BDRV_O_CACHE_WB
;
927 bdrv_flags
|= BDRV_O_NO_FLUSH
;
928 } else if (!strcmp(buf
, "writethrough")) {
929 /* this is the default */
931 fprintf(stderr
, "qemu: invalid cache option\n");
936 #ifdef CONFIG_LINUX_AIO
937 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
938 if (!strcmp(buf
, "native")) {
939 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
940 } else if (!strcmp(buf
, "threads")) {
941 /* this is the default */
943 fprintf(stderr
, "qemu: invalid aio option\n");
949 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
950 if (strcmp(buf
, "?") == 0) {
951 fprintf(stderr
, "qemu: Supported formats:");
952 bdrv_iterate_format(bdrv_format_print
, NULL
);
953 fprintf(stderr
, "\n");
956 drv
= bdrv_find_whitelisted_format(buf
);
958 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
963 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
964 if (is_extboot
&& extboot_drive
) {
965 fprintf(stderr
, "qemu: two bootable drives specified\n");
969 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
970 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
971 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
972 fprintf(stderr
, "werror is no supported by this format\n");
976 on_write_error
= parse_block_error_action(buf
, 0);
977 if (on_write_error
< 0) {
982 on_read_error
= BLOCK_ERR_REPORT
;
983 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
984 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
985 fprintf(stderr
, "rerror is no supported by this format\n");
989 on_read_error
= parse_block_error_action(buf
, 1);
990 if (on_read_error
< 0) {
995 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
996 if (type
!= IF_VIRTIO
) {
997 fprintf(stderr
, "addr is not supported\n");
1002 /* compute bus and unit according index */
1005 if (bus_id
!= 0 || unit_id
!= -1) {
1007 "qemu: index cannot be used with bus and unit\n");
1015 unit_id
= index
% max_devs
;
1016 bus_id
= index
/ max_devs
;
1020 /* if user doesn't specify a unit_id,
1021 * try to find the first free
1024 if (unit_id
== -1) {
1026 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1028 if (max_devs
&& unit_id
>= max_devs
) {
1029 unit_id
-= max_devs
;
1037 if (max_devs
&& unit_id
>= max_devs
) {
1038 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1039 unit_id
, max_devs
- 1);
1044 * ignore multiple definitions
1047 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1054 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1055 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1056 dinfo
->id
= qemu_strdup(buf
);
1058 /* no id supplied -> create one */
1059 dinfo
->id
= qemu_mallocz(32);
1060 if (type
== IF_IDE
|| type
== IF_SCSI
)
1061 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1063 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1064 devname
, bus_id
, mediastr
, unit_id
);
1066 snprintf(dinfo
->id
, 32, "%s%s%i",
1067 devname
, mediastr
, unit_id
);
1069 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1070 dinfo
->devaddr
= devaddr
;
1072 dinfo
->bus
= bus_id
;
1073 dinfo
->unit
= unit_id
;
1074 dinfo
->on_read_error
= on_read_error
;
1075 dinfo
->on_write_error
= on_write_error
;
1078 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1079 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1081 extboot_drive
= dinfo
;
1092 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1093 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1097 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1102 /* FIXME: This isn't really a floppy, but it's a reasonable
1105 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1111 /* add virtio block device */
1112 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1113 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1114 qemu_opt_set(opts
, "drive", dinfo
->id
);
1116 qemu_opt_set(opts
, "addr", devaddr
);
1126 /* always use cache=unsafe with snapshot */
1127 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
1128 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
1131 if (media
== MEDIA_CDROM
) {
1132 /* CDROM is fine for any interface, don't check. */
1134 } else if (ro
== 1) {
1135 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
1136 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1141 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1143 if (bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1144 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1145 file
, strerror(errno
));
1149 if (bdrv_key_required(dinfo
->bdrv
))
1155 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1157 QEMUMachine
*machine
= opaque
;
1158 int fatal_error
= 0;
1160 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1167 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1169 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1170 qemu_opt_set(opts
, "snapshot", "on");
1175 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1177 boot_set_handler
= func
;
1178 boot_set_opaque
= opaque
;
1181 int qemu_boot_set(const char *boot_devices
)
1183 if (!boot_set_handler
) {
1186 return boot_set_handler(boot_set_opaque
, boot_devices
);
1189 static void validate_bootdevices(char *devices
)
1191 /* We just do some generic consistency checks */
1195 for (p
= devices
; *p
!= '\0'; p
++) {
1196 /* Allowed boot devices are:
1197 * a-b: floppy disk drives
1198 * c-f: IDE disk drives
1199 * g-m: machine implementation dependant drives
1200 * n-p: network devices
1201 * It's up to each machine implementation to check if the given boot
1202 * devices match the actual hardware implementation and firmware
1205 if (*p
< 'a' || *p
> 'p') {
1206 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1209 if (bitmap
& (1 << (*p
- 'a'))) {
1210 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1213 bitmap
|= 1 << (*p
- 'a');
1217 static void restore_boot_devices(void *opaque
)
1219 char *standard_boot_devices
= opaque
;
1220 static int first
= 1;
1222 /* Restore boot order and remove ourselves after the first boot */
1228 qemu_boot_set(standard_boot_devices
);
1230 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1231 qemu_free(standard_boot_devices
);
1234 static void numa_add(const char *optarg
)
1238 unsigned long long value
, endvalue
;
1241 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1242 if (!strcmp(option
, "node")) {
1243 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1244 nodenr
= nb_numa_nodes
;
1246 nodenr
= strtoull(option
, NULL
, 10);
1249 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1250 node_mem
[nodenr
] = 0;
1252 value
= strtoull(option
, &endptr
, 0);
1254 case 0: case 'M': case 'm':
1261 node_mem
[nodenr
] = value
;
1263 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1264 node_cpumask
[nodenr
] = 0;
1266 value
= strtoull(option
, &endptr
, 10);
1269 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1271 if (*endptr
== '-') {
1272 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1273 if (endvalue
>= 63) {
1276 "only 63 CPUs in NUMA mode supported.\n");
1278 value
= (2ULL << endvalue
) - (1ULL << value
);
1280 value
= 1ULL << value
;
1283 node_cpumask
[nodenr
] = value
;
1290 static void smp_parse(const char *optarg
)
1292 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1296 smp
= strtoul(optarg
, &endptr
, 10);
1297 if (endptr
!= optarg
) {
1298 if (*endptr
== ',') {
1302 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1303 sockets
= strtoull(option
, NULL
, 10);
1304 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1305 cores
= strtoull(option
, NULL
, 10);
1306 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1307 threads
= strtoull(option
, NULL
, 10);
1308 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1309 max_cpus
= strtoull(option
, NULL
, 10);
1311 /* compute missing values, prefer sockets over cores over threads */
1312 if (smp
== 0 || sockets
== 0) {
1313 sockets
= sockets
> 0 ? sockets
: 1;
1314 cores
= cores
> 0 ? cores
: 1;
1315 threads
= threads
> 0 ? threads
: 1;
1317 smp
= cores
* threads
* sockets
;
1321 threads
= threads
> 0 ? threads
: 1;
1322 cores
= smp
/ (sockets
* threads
);
1325 threads
= smp
/ (cores
* sockets
);
1330 smp_cores
= cores
> 0 ? cores
: 1;
1331 smp_threads
= threads
> 0 ? threads
: 1;
1333 max_cpus
= smp_cpus
;
1336 /***********************************************************/
1339 static int usb_device_add(const char *devname
, int is_hotplug
)
1342 USBDevice
*dev
= NULL
;
1347 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1348 dev
= usbdevice_create(devname
);
1352 /* the other ones */
1353 if (strstart(devname
, "host:", &p
)) {
1354 dev
= usb_host_device_open(p
);
1355 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1356 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1357 bt_new_hci(qemu_find_bt_vlan(0)));
1368 static int usb_device_del(const char *devname
)
1373 if (strstart(devname
, "host:", &p
))
1374 return usb_host_device_close(p
);
1379 p
= strchr(devname
, '.');
1382 bus_num
= strtoul(devname
, NULL
, 0);
1383 addr
= strtoul(p
+ 1, NULL
, 0);
1385 return usb_device_delete_addr(bus_num
, addr
);
1388 static int usb_parse(const char *cmdline
)
1391 r
= usb_device_add(cmdline
, 0);
1393 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1398 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1400 const char *devname
= qdict_get_str(qdict
, "devname");
1401 if (usb_device_add(devname
, 1) < 0) {
1402 error_report("could not add USB device '%s'", devname
);
1406 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1408 const char *devname
= qdict_get_str(qdict
, "devname");
1409 if (usb_device_del(devname
) < 0) {
1410 error_report("could not delete USB device '%s'", devname
);
1414 /***********************************************************/
1415 /* PCMCIA/Cardbus */
1417 static struct pcmcia_socket_entry_s
{
1418 PCMCIASocket
*socket
;
1419 struct pcmcia_socket_entry_s
*next
;
1420 } *pcmcia_sockets
= 0;
1422 void pcmcia_socket_register(PCMCIASocket
*socket
)
1424 struct pcmcia_socket_entry_s
*entry
;
1426 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1427 entry
->socket
= socket
;
1428 entry
->next
= pcmcia_sockets
;
1429 pcmcia_sockets
= entry
;
1432 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1434 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1436 ptr
= &pcmcia_sockets
;
1437 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1438 if (entry
->socket
== socket
) {
1444 void pcmcia_info(Monitor
*mon
)
1446 struct pcmcia_socket_entry_s
*iter
;
1448 if (!pcmcia_sockets
)
1449 monitor_printf(mon
, "No PCMCIA sockets\n");
1451 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1452 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1453 iter
->socket
->attached
? iter
->socket
->card_string
:
1457 /***********************************************************/
1460 typedef struct IOHandlerRecord
{
1462 IOCanReadHandler
*fd_read_poll
;
1464 IOHandler
*fd_write
;
1467 /* temporary data */
1469 QLIST_ENTRY(IOHandlerRecord
) next
;
1472 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1473 QLIST_HEAD_INITIALIZER(io_handlers
);
1476 /* XXX: fd_read_poll should be suppressed, but an API change is
1477 necessary in the character devices to suppress fd_can_read(). */
1478 int qemu_set_fd_handler2(int fd
,
1479 IOCanReadHandler
*fd_read_poll
,
1481 IOHandler
*fd_write
,
1484 IOHandlerRecord
*ioh
;
1486 if (!fd_read
&& !fd_write
) {
1487 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1488 if (ioh
->fd
== fd
) {
1494 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1498 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1499 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1502 ioh
->fd_read_poll
= fd_read_poll
;
1503 ioh
->fd_read
= fd_read
;
1504 ioh
->fd_write
= fd_write
;
1505 ioh
->opaque
= opaque
;
1508 qemu_notify_event();
1512 int qemu_set_fd_handler(int fd
,
1514 IOHandler
*fd_write
,
1517 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1520 /***********************************************************/
1521 /* machine registration */
1523 static QEMUMachine
*first_machine
= NULL
;
1524 QEMUMachine
*current_machine
= NULL
;
1526 int qemu_register_machine(QEMUMachine
*m
)
1529 pm
= &first_machine
;
1537 static QEMUMachine
*find_machine(const char *name
)
1541 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1542 if (!strcmp(m
->name
, name
))
1544 if (m
->alias
&& !strcmp(m
->alias
, name
))
1550 static QEMUMachine
*find_default_machine(void)
1554 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1555 if (m
->is_default
) {
1562 /***********************************************************/
1563 /* main execution loop */
1565 static void gui_update(void *opaque
)
1567 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1568 DisplayState
*ds
= opaque
;
1569 DisplayChangeListener
*dcl
= ds
->listeners
;
1571 qemu_flush_coalesced_mmio_buffer();
1574 while (dcl
!= NULL
) {
1575 if (dcl
->gui_timer_interval
&&
1576 dcl
->gui_timer_interval
< interval
)
1577 interval
= dcl
->gui_timer_interval
;
1580 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1583 static void nographic_update(void *opaque
)
1585 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1587 qemu_flush_coalesced_mmio_buffer();
1588 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1591 struct vm_change_state_entry
{
1592 VMChangeStateHandler
*cb
;
1594 QLIST_ENTRY (vm_change_state_entry
) entries
;
1597 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1599 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1602 VMChangeStateEntry
*e
;
1604 e
= qemu_mallocz(sizeof (*e
));
1608 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1612 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1614 QLIST_REMOVE (e
, entries
);
1618 void vm_state_notify(int running
, int reason
)
1620 VMChangeStateEntry
*e
;
1622 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1623 e
->cb(e
->opaque
, running
, reason
);
1632 vm_state_notify(1, 0);
1634 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1638 /* reset/shutdown handler */
1640 typedef struct QEMUResetEntry
{
1641 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1642 QEMUResetHandler
*func
;
1646 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1647 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1648 static int reset_requested
;
1649 static int shutdown_requested
;
1650 static int powerdown_requested
;
1651 int debug_requested
;
1652 int vmstop_requested
;
1654 int qemu_no_shutdown(void)
1656 int r
= no_shutdown
;
1661 int qemu_shutdown_requested(void)
1663 int r
= shutdown_requested
;
1664 shutdown_requested
= 0;
1668 int qemu_reset_requested(void)
1670 int r
= reset_requested
;
1671 reset_requested
= 0;
1675 int qemu_powerdown_requested(void)
1677 int r
= powerdown_requested
;
1678 powerdown_requested
= 0;
1682 static int qemu_debug_requested(void)
1684 int r
= debug_requested
;
1685 debug_requested
= 0;
1689 static int qemu_vmstop_requested(void)
1691 int r
= vmstop_requested
;
1692 vmstop_requested
= 0;
1696 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1698 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1701 re
->opaque
= opaque
;
1702 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1705 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1709 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1710 if (re
->func
== func
&& re
->opaque
== opaque
) {
1711 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1718 void qemu_system_reset(void)
1720 QEMUResetEntry
*re
, *nre
;
1722 /* reset all devices */
1723 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1724 re
->func(re
->opaque
);
1726 monitor_protocol_event(QEVENT_RESET
, NULL
);
1727 cpu_synchronize_all_post_reset();
1730 void qemu_system_reset_request(void)
1733 shutdown_requested
= 1;
1735 reset_requested
= 1;
1737 if (cpu_single_env
) {
1738 cpu_single_env
->stopped
= 1;
1740 qemu_notify_event();
1743 void qemu_system_shutdown_request(void)
1745 shutdown_requested
= 1;
1746 qemu_notify_event();
1749 void qemu_system_powerdown_request(void)
1751 powerdown_requested
= 1;
1752 qemu_notify_event();
1755 void main_loop_wait(int nonblocking
)
1757 IOHandlerRecord
*ioh
;
1758 fd_set rfds
, wfds
, xfds
;
1766 timeout
= qemu_calculate_timeout();
1767 qemu_bh_update_timeout(&timeout
);
1770 os_host_main_loop_wait(&timeout
);
1772 /* poll any events */
1773 /* XXX: separate device handlers from system ones */
1778 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1782 (!ioh
->fd_read_poll
||
1783 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1784 FD_SET(ioh
->fd
, &rfds
);
1788 if (ioh
->fd_write
) {
1789 FD_SET(ioh
->fd
, &wfds
);
1795 tv
.tv_sec
= timeout
/ 1000;
1796 tv
.tv_usec
= (timeout
% 1000) * 1000;
1798 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1800 qemu_mutex_unlock_iothread();
1801 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1802 qemu_mutex_lock_iothread();
1804 IOHandlerRecord
*pioh
;
1806 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1808 QLIST_REMOVE(ioh
, next
);
1812 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1813 ioh
->fd_read(ioh
->opaque
);
1814 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
1815 FD_CLR(ioh
->fd
, &rfds
);
1817 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1818 ioh
->fd_write(ioh
->opaque
);
1823 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1825 qemu_run_all_timers();
1827 /* Check bottom-halves last in case any of the earlier events triggered
1833 static int vm_can_run(void)
1835 if (powerdown_requested
)
1837 if (reset_requested
)
1839 if (shutdown_requested
)
1841 if (debug_requested
)
1846 qemu_irq qemu_system_powerdown
;
1848 static void main_loop(void)
1852 if (kvm_enabled()) {
1854 cpu_disable_ticks();
1858 qemu_main_loop_start();
1862 bool nonblocking
= false;
1863 #ifdef CONFIG_PROFILER
1866 #ifndef CONFIG_IOTHREAD
1867 nonblocking
= tcg_cpu_exec();
1869 #ifdef CONFIG_PROFILER
1870 ti
= profile_getclock();
1872 main_loop_wait(nonblocking
);
1873 #ifdef CONFIG_PROFILER
1874 dev_time
+= profile_getclock() - ti
;
1876 } while (vm_can_run());
1878 if ((r
= qemu_debug_requested())) {
1881 if (qemu_shutdown_requested()) {
1882 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1889 if (qemu_reset_requested()) {
1891 qemu_system_reset();
1894 if (qemu_powerdown_requested()) {
1895 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1896 qemu_irq_raise(qemu_system_powerdown
);
1898 if ((r
= qemu_vmstop_requested())) {
1905 static void version(void)
1907 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1910 static void help(int exitcode
)
1912 const char *options_help
=
1913 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1915 #define DEFHEADING(text) stringify(text) "\n"
1916 #include "qemu-options.def"
1922 printf("usage: %s [options] [disk_image]\n"
1924 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1927 "During emulation, the following keys are useful:\n"
1928 "ctrl-alt-f toggle full screen\n"
1929 "ctrl-alt-n switch to virtual console 'n'\n"
1930 "ctrl-alt toggle mouse and keyboard grab\n"
1932 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1938 #define HAS_ARG 0x0001
1940 typedef struct QEMUOption
{
1947 static const QEMUOption qemu_options
[] = {
1948 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1949 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1950 { option, opt_arg, opt_enum, arch_mask },
1951 #define DEFHEADING(text)
1952 #include "qemu-options.def"
1958 static void select_vgahw (const char *p
)
1963 vga_interface_type
= VGA_NONE
;
1964 if (strstart(p
, "std", &opts
)) {
1965 vga_interface_type
= VGA_STD
;
1966 } else if (strstart(p
, "cirrus", &opts
)) {
1967 vga_interface_type
= VGA_CIRRUS
;
1968 } else if (strstart(p
, "vmware", &opts
)) {
1969 vga_interface_type
= VGA_VMWARE
;
1970 } else if (strstart(p
, "xenfb", &opts
)) {
1971 vga_interface_type
= VGA_XENFB
;
1972 } else if (!strstart(p
, "none", &opts
)) {
1974 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1978 const char *nextopt
;
1980 if (strstart(opts
, ",retrace=", &nextopt
)) {
1982 if (strstart(opts
, "dumb", &nextopt
))
1983 vga_retrace_method
= VGA_RETRACE_DUMB
;
1984 else if (strstart(opts
, "precise", &nextopt
))
1985 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1986 else goto invalid_vga
;
1987 } else goto invalid_vga
;
1992 static int balloon_parse(const char *arg
)
1996 if (strcmp(arg
, "none") == 0) {
2000 if (!strncmp(arg
, "virtio", 6)) {
2001 if (arg
[6] == ',') {
2002 /* have params -> parse them */
2003 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
2007 /* create empty opts */
2008 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2010 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
2017 char *qemu_find_file(int type
, const char *name
)
2023 /* If name contains path separators then try it as a straight path. */
2024 if ((strchr(name
, '/') || strchr(name
, '\\'))
2025 && access(name
, R_OK
) == 0) {
2026 return qemu_strdup(name
);
2029 case QEMU_FILE_TYPE_BIOS
:
2032 case QEMU_FILE_TYPE_KEYMAP
:
2033 subdir
= "keymaps/";
2038 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2039 buf
= qemu_mallocz(len
);
2040 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2041 if (access(buf
, R_OK
)) {
2048 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2050 return qdev_device_help(opts
);
2053 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2057 dev
= qdev_device_add(opts
);
2063 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2065 CharDriverState
*chr
;
2067 chr
= qemu_chr_open_opts(opts
, NULL
);
2074 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2077 ret
= qemu_fsdev_add(opts
);
2083 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2085 CharDriverState
*chr
;
2086 const char *chardev
;
2090 mode
= qemu_opt_get(opts
, "mode");
2094 if (strcmp(mode
, "readline") == 0) {
2095 flags
= MONITOR_USE_READLINE
;
2096 } else if (strcmp(mode
, "control") == 0) {
2097 flags
= MONITOR_USE_CONTROL
;
2099 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2103 if (qemu_opt_get_bool(opts
, "default", 0))
2104 flags
|= MONITOR_IS_DEFAULT
;
2106 chardev
= qemu_opt_get(opts
, "chardev");
2107 chr
= qemu_chr_find(chardev
);
2109 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2113 monitor_init(chr
, flags
);
2117 static void monitor_parse(const char *optarg
, const char *mode
)
2119 static int monitor_device_index
= 0;
2125 if (strstart(optarg
, "chardev:", &p
)) {
2126 snprintf(label
, sizeof(label
), "%s", p
);
2128 snprintf(label
, sizeof(label
), "compat_monitor%d",
2129 monitor_device_index
);
2130 if (monitor_device_index
== 0) {
2133 opts
= qemu_chr_parse_compat(label
, optarg
);
2135 fprintf(stderr
, "parse error: %s\n", optarg
);
2140 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2142 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2145 qemu_opt_set(opts
, "mode", mode
);
2146 qemu_opt_set(opts
, "chardev", label
);
2148 qemu_opt_set(opts
, "default", "on");
2149 monitor_device_index
++;
2152 struct device_config
{
2154 DEV_USB
, /* -usbdevice */
2156 DEV_SERIAL
, /* -serial */
2157 DEV_PARALLEL
, /* -parallel */
2158 DEV_VIRTCON
, /* -virtioconsole */
2159 DEV_DEBUGCON
, /* -debugcon */
2161 const char *cmdline
;
2162 QTAILQ_ENTRY(device_config
) next
;
2164 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2166 static void add_device_config(int type
, const char *cmdline
)
2168 struct device_config
*conf
;
2170 conf
= qemu_mallocz(sizeof(*conf
));
2172 conf
->cmdline
= cmdline
;
2173 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2176 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2178 struct device_config
*conf
;
2181 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2182 if (conf
->type
!= type
)
2184 rc
= func(conf
->cmdline
);
2191 static int serial_parse(const char *devname
)
2193 static int index
= 0;
2196 if (strcmp(devname
, "none") == 0)
2198 if (index
== MAX_SERIAL_PORTS
) {
2199 fprintf(stderr
, "qemu: too many serial ports\n");
2202 snprintf(label
, sizeof(label
), "serial%d", index
);
2203 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2204 if (!serial_hds
[index
]) {
2205 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2206 devname
, strerror(errno
));
2213 static int parallel_parse(const char *devname
)
2215 static int index
= 0;
2218 if (strcmp(devname
, "none") == 0)
2220 if (index
== MAX_PARALLEL_PORTS
) {
2221 fprintf(stderr
, "qemu: too many parallel ports\n");
2224 snprintf(label
, sizeof(label
), "parallel%d", index
);
2225 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2226 if (!parallel_hds
[index
]) {
2227 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2228 devname
, strerror(errno
));
2235 static int virtcon_parse(const char *devname
)
2237 static int index
= 0;
2239 QemuOpts
*bus_opts
, *dev_opts
;
2241 if (strcmp(devname
, "none") == 0)
2243 if (index
== MAX_VIRTIO_CONSOLES
) {
2244 fprintf(stderr
, "qemu: too many virtio consoles\n");
2248 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2249 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2251 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2252 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2254 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2255 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2256 if (!virtcon_hds
[index
]) {
2257 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2258 devname
, strerror(errno
));
2261 qemu_opt_set(dev_opts
, "chardev", label
);
2267 static int debugcon_parse(const char *devname
)
2271 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2274 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2276 fprintf(stderr
, "qemu: already have a debugcon device\n");
2279 qemu_opt_set(opts
, "driver", "isa-debugcon");
2280 qemu_opt_set(opts
, "chardev", "debugcon");
2284 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2285 const char **poptarg
, int *poptind
)
2287 const QEMUOption
*popt
;
2288 int optind
= *poptind
;
2289 char *r
= argv
[optind
];
2292 loc_set_cmdline(argv
, optind
, 1);
2294 /* Treat --foo the same as -foo. */
2297 popt
= qemu_options
;
2300 error_report("invalid option");
2303 if (!strcmp(popt
->name
, r
+ 1))
2307 if (popt
->flags
& HAS_ARG
) {
2308 if (optind
>= argc
) {
2309 error_report("requires an argument");
2312 optarg
= argv
[optind
++];
2313 loc_set_cmdline(argv
, optind
- 2, 2);
2324 int main(int argc
, char **argv
, char **envp
)
2326 const char *gdbstub_dev
= NULL
;
2328 int snapshot
, linux_boot
;
2329 const char *icount_option
= NULL
;
2330 const char *initrd_filename
;
2331 const char *kernel_filename
, *kernel_cmdline
;
2332 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2334 DisplayChangeListener
*dcl
;
2335 int cyls
, heads
, secs
, translation
;
2336 QemuOpts
*hda_opts
= NULL
, *opts
;
2339 const char *loadvm
= NULL
;
2340 QEMUMachine
*machine
;
2341 const char *cpu_model
;
2346 const char *pid_file
= NULL
;
2347 const char *incoming
= NULL
;
2351 int show_vnc_port
= 0;
2354 error_set_progname(argv
[0]);
2358 qemu_cache_utils_init(envp
);
2360 QLIST_INIT (&vm_change_state_head
);
2361 os_setup_early_signal_handling();
2363 module_call_init(MODULE_INIT_MACHINE
);
2364 machine
= find_default_machine();
2366 initrd_filename
= NULL
;
2369 kernel_filename
= NULL
;
2370 kernel_cmdline
= "";
2371 cyls
= heads
= secs
= 0;
2372 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2374 for (i
= 0; i
< MAX_NODES
; i
++) {
2376 node_cpumask
[i
] = 0;
2379 assigned_devices_index
= 0;
2387 /* first pass of option parsing */
2389 while (optind
< argc
) {
2390 if (argv
[optind
][0] != '-') {
2395 const QEMUOption
*popt
;
2397 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2398 switch (popt
->index
) {
2399 case QEMU_OPTION_nodefconfig
:
2409 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2410 if (ret
< 0 && ret
!= -ENOENT
) {
2414 ret
= qemu_read_config_file(arch_config_name
);
2415 if (ret
< 0 && ret
!= -ENOENT
) {
2421 /* second pass of option parsing */
2426 if (argv
[optind
][0] != '-') {
2427 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2429 const QEMUOption
*popt
;
2431 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2432 if (!(popt
->arch_mask
& arch_type
)) {
2433 printf("Option %s not supported for this target\n", popt
->name
);
2436 switch(popt
->index
) {
2438 machine
= find_machine(optarg
);
2441 printf("Supported machines are:\n");
2442 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2444 printf("%-10s %s (alias of %s)\n",
2445 m
->alias
, m
->desc
, m
->name
);
2446 printf("%-10s %s%s\n",
2448 m
->is_default
? " (default)" : "");
2450 exit(*optarg
!= '?');
2453 case QEMU_OPTION_cpu
:
2454 /* hw initialization will check this */
2455 if (*optarg
== '?') {
2456 list_cpus(stdout
, &fprintf
, optarg
);
2462 case QEMU_OPTION_initrd
:
2463 initrd_filename
= optarg
;
2465 case QEMU_OPTION_hda
:
2467 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2469 hda_opts
= drive_add(optarg
, HD_ALIAS
2470 ",cyls=%d,heads=%d,secs=%d%s",
2471 0, cyls
, heads
, secs
,
2472 translation
== BIOS_ATA_TRANSLATION_LBA
?
2474 translation
== BIOS_ATA_TRANSLATION_NONE
?
2475 ",trans=none" : "");
2477 case QEMU_OPTION_hdb
:
2478 case QEMU_OPTION_hdc
:
2479 case QEMU_OPTION_hdd
:
2480 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2482 case QEMU_OPTION_drive
:
2483 drive_add(NULL
, "%s", optarg
);
2485 case QEMU_OPTION_set
:
2486 if (qemu_set_option(optarg
) != 0)
2489 case QEMU_OPTION_global
:
2490 if (qemu_global_option(optarg
) != 0)
2493 case QEMU_OPTION_mtdblock
:
2494 drive_add(optarg
, MTD_ALIAS
);
2496 case QEMU_OPTION_sd
:
2497 drive_add(optarg
, SD_ALIAS
);
2499 case QEMU_OPTION_pflash
:
2500 drive_add(optarg
, PFLASH_ALIAS
);
2502 case QEMU_OPTION_snapshot
:
2505 case QEMU_OPTION_hdachs
:
2509 cyls
= strtol(p
, (char **)&p
, 0);
2510 if (cyls
< 1 || cyls
> 16383)
2515 heads
= strtol(p
, (char **)&p
, 0);
2516 if (heads
< 1 || heads
> 16)
2521 secs
= strtol(p
, (char **)&p
, 0);
2522 if (secs
< 1 || secs
> 63)
2526 if (!strcmp(p
, "none"))
2527 translation
= BIOS_ATA_TRANSLATION_NONE
;
2528 else if (!strcmp(p
, "lba"))
2529 translation
= BIOS_ATA_TRANSLATION_LBA
;
2530 else if (!strcmp(p
, "auto"))
2531 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2534 } else if (*p
!= '\0') {
2536 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2539 if (hda_opts
!= NULL
) {
2541 snprintf(num
, sizeof(num
), "%d", cyls
);
2542 qemu_opt_set(hda_opts
, "cyls", num
);
2543 snprintf(num
, sizeof(num
), "%d", heads
);
2544 qemu_opt_set(hda_opts
, "heads", num
);
2545 snprintf(num
, sizeof(num
), "%d", secs
);
2546 qemu_opt_set(hda_opts
, "secs", num
);
2547 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2548 qemu_opt_set(hda_opts
, "trans", "lba");
2549 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2550 qemu_opt_set(hda_opts
, "trans", "none");
2554 case QEMU_OPTION_numa
:
2555 if (nb_numa_nodes
>= MAX_NODES
) {
2556 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2561 case QEMU_OPTION_nographic
:
2562 display_type
= DT_NOGRAPHIC
;
2564 #ifdef CONFIG_CURSES
2565 case QEMU_OPTION_curses
:
2566 display_type
= DT_CURSES
;
2569 case QEMU_OPTION_portrait
:
2572 case QEMU_OPTION_kernel
:
2573 kernel_filename
= optarg
;
2575 case QEMU_OPTION_append
:
2576 kernel_cmdline
= optarg
;
2578 case QEMU_OPTION_cdrom
:
2579 drive_add(optarg
, CDROM_ALIAS
);
2581 case QEMU_OPTION_boot
:
2583 static const char * const params
[] = {
2584 "order", "once", "menu", NULL
2586 char buf
[sizeof(boot_devices
)];
2587 char *standard_boot_devices
;
2590 if (!strchr(optarg
, '=')) {
2592 pstrcpy(buf
, sizeof(buf
), optarg
);
2593 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2595 "qemu: unknown boot parameter '%s' in '%s'\n",
2601 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2602 validate_bootdevices(buf
);
2603 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2606 if (get_param_value(buf
, sizeof(buf
),
2608 validate_bootdevices(buf
);
2609 standard_boot_devices
= qemu_strdup(boot_devices
);
2610 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2611 qemu_register_reset(restore_boot_devices
,
2612 standard_boot_devices
);
2614 if (get_param_value(buf
, sizeof(buf
),
2616 if (!strcmp(buf
, "on")) {
2618 } else if (!strcmp(buf
, "off")) {
2622 "qemu: invalid option value '%s'\n",
2630 case QEMU_OPTION_fda
:
2631 case QEMU_OPTION_fdb
:
2632 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2634 case QEMU_OPTION_no_fd_bootchk
:
2637 case QEMU_OPTION_netdev
:
2638 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2642 case QEMU_OPTION_net
:
2643 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2648 case QEMU_OPTION_tftp
:
2649 legacy_tftp_prefix
= optarg
;
2651 case QEMU_OPTION_bootp
:
2652 legacy_bootp_filename
= optarg
;
2654 case QEMU_OPTION_redir
:
2655 if (net_slirp_redir(optarg
) < 0)
2659 case QEMU_OPTION_bt
:
2660 add_device_config(DEV_BT
, optarg
);
2662 case QEMU_OPTION_audio_help
:
2663 if (!(audio_available())) {
2664 printf("Option %s not supported for this target\n", popt
->name
);
2670 case QEMU_OPTION_soundhw
:
2671 if (!(audio_available())) {
2672 printf("Option %s not supported for this target\n", popt
->name
);
2675 select_soundhw (optarg
);
2680 case QEMU_OPTION_version
:
2684 case QEMU_OPTION_m
: {
2688 value
= strtoul(optarg
, &ptr
, 10);
2690 case 0: case 'M': case 'm':
2697 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2701 /* On 32-bit hosts, QEMU is limited by virtual address space */
2702 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2703 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2706 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2707 fprintf(stderr
, "qemu: ram size too large\n");
2713 case QEMU_OPTION_mempath
:
2717 case QEMU_OPTION_mem_prealloc
:
2722 set_cpu_log(optarg
);
2725 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2727 case QEMU_OPTION_gdb
:
2728 gdbstub_dev
= optarg
;
2733 case QEMU_OPTION_bios
:
2736 case QEMU_OPTION_singlestep
:
2743 keyboard_layout
= optarg
;
2745 case QEMU_OPTION_localtime
:
2748 case QEMU_OPTION_vga
:
2749 select_vgahw (optarg
);
2756 w
= strtol(p
, (char **)&p
, 10);
2759 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2765 h
= strtol(p
, (char **)&p
, 10);
2770 depth
= strtol(p
, (char **)&p
, 10);
2771 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2772 depth
!= 24 && depth
!= 32)
2774 } else if (*p
== '\0') {
2775 depth
= graphic_depth
;
2782 graphic_depth
= depth
;
2785 case QEMU_OPTION_echr
:
2788 term_escape_char
= strtol(optarg
, &r
, 0);
2790 printf("Bad argument to echr\n");
2793 case QEMU_OPTION_monitor
:
2794 monitor_parse(optarg
, "readline");
2795 default_monitor
= 0;
2797 case QEMU_OPTION_qmp
:
2798 monitor_parse(optarg
, "control");
2799 default_monitor
= 0;
2801 case QEMU_OPTION_mon
:
2802 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
2806 default_monitor
= 0;
2808 case QEMU_OPTION_chardev
:
2809 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
2815 case QEMU_OPTION_fsdev
:
2816 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
2818 fprintf(stderr
, "parse error: %s\n", optarg
);
2822 case QEMU_OPTION_virtfs
: {
2823 char *arg_fsdev
= NULL
;
2824 char *arg_9p
= NULL
;
2827 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
2829 fprintf(stderr
, "parse error: %s\n", optarg
);
2833 len
= strlen(",id=,path=");
2834 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2835 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2836 len
+= strlen(qemu_opt_get(opts
, "path"));
2837 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2840 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
2845 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
2846 qemu_opt_get(opts
, "fstype"),
2847 qemu_opt_get(opts
, "mount_tag"),
2848 qemu_opt_get(opts
, "path"));
2850 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2851 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2852 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2855 fprintf(stderr
, "No memory to parse -device for %s\n",
2860 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2861 qemu_opt_get(opts
, "mount_tag"),
2862 qemu_opt_get(opts
, "mount_tag"));
2864 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
2865 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2869 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
2870 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2874 qemu_free(arg_fsdev
);
2879 case QEMU_OPTION_serial
:
2880 add_device_config(DEV_SERIAL
, optarg
);
2882 if (strncmp(optarg
, "mon:", 4) == 0) {
2883 default_monitor
= 0;
2886 case QEMU_OPTION_watchdog
:
2889 "qemu: only one watchdog option may be given\n");
2894 case QEMU_OPTION_watchdog_action
:
2895 if (select_watchdog_action(optarg
) == -1) {
2896 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2900 case QEMU_OPTION_virtiocon
:
2901 add_device_config(DEV_VIRTCON
, optarg
);
2902 default_virtcon
= 0;
2903 if (strncmp(optarg
, "mon:", 4) == 0) {
2904 default_monitor
= 0;
2907 case QEMU_OPTION_parallel
:
2908 add_device_config(DEV_PARALLEL
, optarg
);
2909 default_parallel
= 0;
2910 if (strncmp(optarg
, "mon:", 4) == 0) {
2911 default_monitor
= 0;
2914 case QEMU_OPTION_debugcon
:
2915 add_device_config(DEV_DEBUGCON
, optarg
);
2917 case QEMU_OPTION_loadvm
:
2920 case QEMU_OPTION_full_screen
:
2924 case QEMU_OPTION_no_frame
:
2927 case QEMU_OPTION_alt_grab
:
2930 case QEMU_OPTION_ctrl_grab
:
2933 case QEMU_OPTION_no_quit
:
2936 case QEMU_OPTION_sdl
:
2937 display_type
= DT_SDL
;
2940 case QEMU_OPTION_pidfile
:
2943 case QEMU_OPTION_win2k_hack
:
2944 win2k_install_hack
= 1;
2946 case QEMU_OPTION_rtc_td_hack
:
2949 case QEMU_OPTION_acpitable
:
2950 do_acpitable_option(optarg
);
2952 case QEMU_OPTION_smbios
:
2953 do_smbios_option(optarg
);
2956 case QEMU_OPTION_enable_kvm
:
2960 case QEMU_OPTION_no_kvm
:
2963 case QEMU_OPTION_no_kvm_irqchip
: {
2968 case QEMU_OPTION_no_kvm_pit
: {
2972 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2973 kvm_pit_reinject
= 0;
2976 case QEMU_OPTION_enable_nesting
: {
2980 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
2981 case QEMU_OPTION_pcidevice
:
2982 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
2983 fprintf(stderr
, "Too many assigned devices\n");
2986 assigned_devices
[assigned_devices_index
] = optarg
;
2987 assigned_devices_index
++;
2990 case QEMU_OPTION_usb
:
2993 case QEMU_OPTION_usbdevice
:
2995 add_device_config(DEV_USB
, optarg
);
2997 case QEMU_OPTION_device
:
2998 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
3002 case QEMU_OPTION_smp
:
3005 fprintf(stderr
, "Invalid number of CPUs\n");
3008 if (max_cpus
< smp_cpus
) {
3009 fprintf(stderr
, "maxcpus must be equal to or greater than "
3013 if (max_cpus
> 255) {
3014 fprintf(stderr
, "Unsupported number of maxcpus\n");
3018 case QEMU_OPTION_vnc
:
3019 display_type
= DT_VNC
;
3020 vnc_display
= optarg
;
3022 case QEMU_OPTION_no_acpi
:
3025 case QEMU_OPTION_no_hpet
:
3028 case QEMU_OPTION_balloon
:
3029 if (balloon_parse(optarg
) < 0) {
3030 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3034 case QEMU_OPTION_no_reboot
:
3037 case QEMU_OPTION_no_shutdown
:
3040 case QEMU_OPTION_show_cursor
:
3043 case QEMU_OPTION_uuid
:
3044 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3045 fprintf(stderr
, "Fail to parse UUID string."
3046 " Wrong format.\n");
3051 case QEMU_OPTION_daemonize
:
3055 case QEMU_OPTION_option_rom
:
3056 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3057 fprintf(stderr
, "Too many option ROMs\n");
3060 option_rom
[nb_option_roms
] = optarg
;
3063 case QEMU_OPTION_semihosting
:
3064 semihosting_enabled
= 1;
3066 case QEMU_OPTION_tdf
:
3069 case QEMU_OPTION_kvm_shadow_memory
:
3070 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3072 case QEMU_OPTION_name
:
3073 qemu_name
= qemu_strdup(optarg
);
3075 char *p
= strchr(qemu_name
, ',');
3078 if (strncmp(p
, "process=", 8)) {
3079 fprintf(stderr
, "Unknown subargument %s to -name", p
);
3087 case QEMU_OPTION_prom_env
:
3088 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3089 fprintf(stderr
, "Too many prom variables\n");
3092 prom_envs
[nb_prom_envs
] = optarg
;
3095 case QEMU_OPTION_old_param
:
3098 case QEMU_OPTION_clock
:
3099 configure_alarms(optarg
);
3101 case QEMU_OPTION_startdate
:
3102 configure_rtc_date_offset(optarg
, 1);
3104 case QEMU_OPTION_rtc
:
3105 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3109 configure_rtc(opts
);
3111 case QEMU_OPTION_tb_size
:
3112 tb_size
= strtol(optarg
, NULL
, 0);
3116 case QEMU_OPTION_icount
:
3117 icount_option
= optarg
;
3119 case QEMU_OPTION_incoming
:
3122 case QEMU_OPTION_nodefaults
:
3124 default_parallel
= 0;
3125 default_virtcon
= 0;
3126 default_monitor
= 0;
3134 case QEMU_OPTION_nvram
:
3138 case QEMU_OPTION_xen_domid
:
3139 if (!(xen_available())) {
3140 printf("Option %s not supported for this target\n", popt
->name
);
3143 xen_domid
= atoi(optarg
);
3145 case QEMU_OPTION_xen_create
:
3146 if (!(xen_available())) {
3147 printf("Option %s not supported for this target\n", popt
->name
);
3150 xen_mode
= XEN_CREATE
;
3152 case QEMU_OPTION_xen_attach
:
3153 if (!(xen_available())) {
3154 printf("Option %s not supported for this target\n", popt
->name
);
3157 xen_mode
= XEN_ATTACH
;
3159 case QEMU_OPTION_readconfig
:
3161 int ret
= qemu_read_config_file(optarg
);
3163 fprintf(stderr
, "read config %s: %s\n", optarg
,
3169 case QEMU_OPTION_writeconfig
:
3172 if (strcmp(optarg
, "-") == 0) {
3175 fp
= fopen(optarg
, "w");
3177 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3181 qemu_config_write(fp
);
3186 os_parse_cmd_args(popt
->index
, optarg
);
3192 /* If no data_dir is specified then try to find it relative to the
3195 data_dir
= os_find_datadir(argv
[0]);
3197 /* If all else fails use the install patch specified when building. */
3199 data_dir
= CONFIG_QEMU_DATADIR
;
3203 * Default to max_cpus = smp_cpus, in case the user doesn't
3204 * specify a max_cpus value.
3207 max_cpus
= smp_cpus
;
3209 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3210 if (smp_cpus
> machine
->max_cpus
) {
3211 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3212 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3217 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3218 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3220 if (machine
->no_serial
) {
3223 if (machine
->no_parallel
) {
3224 default_parallel
= 0;
3226 if (!machine
->use_virtcon
) {
3227 default_virtcon
= 0;
3229 if (machine
->no_vga
) {
3232 if (machine
->no_floppy
) {
3235 if (machine
->no_cdrom
) {
3238 if (machine
->no_sdcard
) {
3242 if (display_type
== DT_NOGRAPHIC
) {
3243 if (default_parallel
)
3244 add_device_config(DEV_PARALLEL
, "null");
3245 if (default_serial
&& default_monitor
) {
3246 add_device_config(DEV_SERIAL
, "mon:stdio");
3247 } else if (default_virtcon
&& default_monitor
) {
3248 add_device_config(DEV_VIRTCON
, "mon:stdio");
3251 add_device_config(DEV_SERIAL
, "stdio");
3252 if (default_virtcon
)
3253 add_device_config(DEV_VIRTCON
, "stdio");
3254 if (default_monitor
)
3255 monitor_parse("stdio", "readline");
3259 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3260 if (default_parallel
)
3261 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3262 if (default_monitor
)
3263 monitor_parse("vc:80Cx24C", "readline");
3264 if (default_virtcon
)
3265 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3268 vga_interface_type
= VGA_CIRRUS
;
3272 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3275 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
3284 if (pipe(fds
) == -1)
3295 len
= read(fds
[0], &status
, 1);
3296 if (len
== -1 && (errno
== EINTR
))
3301 else if (status
== 1) {
3302 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3310 qemu_set_cloexec(fds
[1]);
3322 signal(SIGTSTP
, SIG_IGN
);
3323 signal(SIGTTOU
, SIG_IGN
);
3324 signal(SIGTTIN
, SIG_IGN
);
3328 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3332 if (write(fds
[1], &status
, 1) != 1) {
3333 perror("daemonize. Writing to pipe\n");
3337 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3342 int ret
= kvm_init(smp_cpus
);
3344 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
3345 if (!kvm_available()) {
3346 printf("KVM not supported for this target\n");
3348 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3353 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
3359 if (qemu_init_main_loop()) {
3360 fprintf(stderr
, "qemu_init_main_loop failed\n");
3363 linux_boot
= (kernel_filename
!= NULL
);
3365 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3366 fprintf(stderr
, "-append only allowed with -kernel option\n");
3370 if (!linux_boot
&& initrd_filename
!= NULL
) {
3371 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3376 /* Win32 doesn't support line-buffering and requires size >= 2 */
3377 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3380 if (init_timer_alarm() < 0) {
3381 fprintf(stderr
, "could not initialize alarm timer\n");
3384 configure_icount(icount_option
);
3386 if (net_init_clients() < 0) {
3390 /* init the bluetooth world */
3391 if (foreach_device_config(DEV_BT
, bt_parse
))
3394 /* init the memory */
3396 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3398 /* init the dynamic translator */
3399 cpu_exec_init_all(tb_size
* 1024 * 1024);
3401 bdrv_init_with_whitelist();
3405 if (default_cdrom
) {
3406 /* we always create the cdrom drive, even if no disk is there */
3407 drive_add(NULL
, CDROM_ALIAS
);
3410 if (default_floppy
) {
3411 /* we always create at least one floppy */
3412 drive_add(NULL
, FD_ALIAS
, 0);
3415 if (default_sdcard
) {
3416 /* we always create one sd slot, even if no card is in it */
3417 drive_add(NULL
, SD_ALIAS
);
3420 /* open the virtual block devices */
3422 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3423 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3426 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3429 if (nb_numa_nodes
> 0) {
3432 if (nb_numa_nodes
> smp_cpus
) {
3433 nb_numa_nodes
= smp_cpus
;
3436 /* If no memory size if given for any node, assume the default case
3437 * and distribute the available memory equally across all nodes
3439 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3440 if (node_mem
[i
] != 0)
3443 if (i
== nb_numa_nodes
) {
3444 uint64_t usedmem
= 0;
3446 /* On Linux, the each node's border has to be 8MB aligned,
3447 * the final node gets the rest.
3449 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3450 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3451 usedmem
+= node_mem
[i
];
3453 node_mem
[i
] = ram_size
- usedmem
;
3456 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3457 if (node_cpumask
[i
] != 0)
3460 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3461 * must cope with this anyway, because there are BIOSes out there in
3462 * real machines which also use this scheme.
3464 if (i
== nb_numa_nodes
) {
3465 for (i
= 0; i
< smp_cpus
; i
++) {
3466 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3471 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
3475 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3477 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3479 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3481 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3484 module_call_init(MODULE_INIT_DEVICE
);
3486 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3490 i
= select_watchdog(watchdog
);
3492 exit (i
== 1 ? 1 : 0);
3495 if (machine
->compat_props
) {
3496 qdev_prop_register_global_list(machine
->compat_props
);
3500 machine
->init(ram_size
, boot_devices
,
3501 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3503 cpu_synchronize_all_post_init();
3505 /* must be after terminal init, SDL library changes signal handlers */
3506 os_setup_signal_handling();
3510 current_machine
= machine
;
3512 /* init USB devices */
3514 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3518 /* init generic devices */
3519 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3522 net_check_clients();
3524 /* just use the first displaystate for the moment */
3525 ds
= get_displaystate();
3527 if (display_type
== DT_DEFAULT
) {
3528 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3529 display_type
= DT_SDL
;
3531 display_type
= DT_VNC
;
3532 vnc_display
= "localhost:0,to=99";
3538 switch (display_type
) {
3541 #if defined(CONFIG_CURSES)
3543 curses_display_init(ds
, full_screen
);
3546 #if defined(CONFIG_SDL)
3548 sdl_display_init(ds
, full_screen
, no_frame
);
3550 #elif defined(CONFIG_COCOA)
3552 cocoa_display_init(ds
, full_screen
);
3556 vnc_display_init(ds
);
3557 if (vnc_display_open(ds
, vnc_display
) < 0)
3560 if (show_vnc_port
) {
3561 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3569 dcl
= ds
->listeners
;
3570 while (dcl
!= NULL
) {
3571 if (dcl
->dpy_refresh
!= NULL
) {
3572 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3573 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3579 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3580 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3581 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3584 text_consoles_set_display(ds
);
3586 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3587 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3592 qdev_machine_creation_done();
3594 if (rom_load_all() != 0) {
3595 fprintf(stderr
, "rom loading failed\n");
3599 qemu_system_reset();
3601 if (load_vmstate(loadvm
) < 0) {
3607 qemu_start_incoming_migration(incoming
);
3608 } else if (autostart
) {
3618 len
= write(fds
[1], &status
, 1);
3619 if (len
== -1 && (errno
== EINTR
))
3626 perror("not able to chdir to /");
3629 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3635 os_change_process_uid();