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>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
79 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
80 discussion about Solaris header problems */
81 extern int madvise(caddr_t
, size_t, int);
86 #if defined(__OpenBSD__)
90 #if defined(CONFIG_VDE)
91 #include <libvdeplug.h>
99 #if defined(__APPLE__) || defined(main)
101 int qemu_main(int argc
, char **argv
, char **envp
);
102 int main(int argc
, char **argv
)
104 return qemu_main(argc
, argv
, NULL
);
107 #define main qemu_main
109 #endif /* CONFIG_SDL */
113 #define main qemu_main
114 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "block_int.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
151 #include "fsdev/qemu-fsdev.h"
156 #include "qemu_socket.h"
158 #include "slirp/libslirp.h"
160 #include "qemu-queue.h"
162 #include "arch_init.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
;
193 static int full_screen
= 0;
195 static int no_frame
= 0;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
201 int win2k_install_hack
= 0;
209 const char *vnc_display
;
210 int acpi_enabled
= 1;
216 int graphic_rotate
= 0;
217 uint8_t irq0override
= 1;
218 const char *watchdog
;
219 const char *option_rom
[MAX_OPTION_ROMS
];
221 int semihosting_enabled
= 0;
223 const char *qemu_name
;
226 unsigned int nb_prom_envs
= 0;
227 const char *prom_envs
[MAX_PROM_ENVS
];
231 uint64_t node_mem
[MAX_NODES
];
232 uint64_t node_cpumask
[MAX_NODES
];
234 static QEMUTimer
*nographic_timer
;
236 uint8_t qemu_uuid
[16];
238 static QEMUBootSetHandler
*boot_set_handler
;
239 static void *boot_set_opaque
;
243 enum xen_mode xen_mode
= XEN_EMULATE
;
245 static int default_serial
= 1;
246 static int default_parallel
= 1;
247 static int default_virtcon
= 1;
248 static int default_monitor
= 1;
249 static int default_vga
= 1;
250 static int default_floppy
= 1;
251 static int default_cdrom
= 1;
252 static int default_sdcard
= 1;
258 { .driver
= "isa-serial", .flag
= &default_serial
},
259 { .driver
= "isa-parallel", .flag
= &default_parallel
},
260 { .driver
= "isa-fdc", .flag
= &default_floppy
},
261 { .driver
= "ide-drive", .flag
= &default_cdrom
},
262 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
263 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
264 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
265 { .driver
= "VGA", .flag
= &default_vga
},
266 { .driver
= "cirrus-vga", .flag
= &default_vga
},
267 { .driver
= "vmware-svga", .flag
= &default_vga
},
270 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
272 const char *driver
= qemu_opt_get(opts
, "driver");
277 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
278 if (strcmp(default_list
[i
].driver
, driver
) != 0)
280 *(default_list
[i
].flag
) = 0;
285 /***********************************************************/
286 /* real time host monotonic timer */
288 /* compute with 96 bit intermediate result: (a*b)/c */
289 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
294 #ifdef HOST_WORDS_BIGENDIAN
304 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
305 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
308 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
312 /***********************************************************/
313 /* host time/date access */
314 void qemu_get_timedate(struct tm
*tm
, int offset
)
321 if (rtc_date_offset
== -1) {
325 ret
= localtime(&ti
);
327 ti
-= rtc_date_offset
;
331 memcpy(tm
, ret
, sizeof(struct tm
));
334 int qemu_timedate_diff(struct tm
*tm
)
338 if (rtc_date_offset
== -1)
340 seconds
= mktimegm(tm
);
342 seconds
= mktime(tm
);
344 seconds
= mktimegm(tm
) + rtc_date_offset
;
346 return seconds
- time(NULL
);
349 void rtc_change_mon_event(struct tm
*tm
)
353 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
354 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
355 qobject_decref(data
);
358 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
360 time_t rtc_start_date
;
363 if (!strcmp(startdate
, "now") && legacy
) {
364 rtc_date_offset
= -1;
366 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
374 } else if (sscanf(startdate
, "%d-%d-%d",
386 rtc_start_date
= mktimegm(&tm
);
387 if (rtc_start_date
== -1) {
389 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
390 "'2006-06-17T16:01:21' or '2006-06-17'\n");
393 rtc_date_offset
= time(NULL
) - rtc_start_date
;
397 static void configure_rtc(QemuOpts
*opts
)
401 value
= qemu_opt_get(opts
, "base");
403 if (!strcmp(value
, "utc")) {
405 } else if (!strcmp(value
, "localtime")) {
408 configure_rtc_date_offset(value
, 0);
411 value
= qemu_opt_get(opts
, "clock");
413 if (!strcmp(value
, "host")) {
414 rtc_clock
= host_clock
;
415 } else if (!strcmp(value
, "vm")) {
416 rtc_clock
= vm_clock
;
418 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
422 value
= qemu_opt_get(opts
, "driftfix");
424 if (!strcmp(value
, "slew")) {
426 } else if (!strcmp(value
, "none")) {
429 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
435 /***********************************************************/
436 /* Bluetooth support */
439 static struct HCIInfo
*hci_table
[MAX_NICS
];
441 static struct bt_vlan_s
{
442 struct bt_scatternet_s net
;
444 struct bt_vlan_s
*next
;
447 /* find or alloc a new bluetooth "VLAN" */
448 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
450 struct bt_vlan_s
**pvlan
, *vlan
;
451 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
455 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
457 pvlan
= &first_bt_vlan
;
458 while (*pvlan
!= NULL
)
459 pvlan
= &(*pvlan
)->next
;
464 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
468 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
473 static struct HCIInfo null_hci
= {
474 .cmd_send
= null_hci_send
,
475 .sco_send
= null_hci_send
,
476 .acl_send
= null_hci_send
,
477 .bdaddr_set
= null_hci_addr_set
,
480 struct HCIInfo
*qemu_next_hci(void)
482 if (cur_hci
== nb_hcis
)
485 return hci_table
[cur_hci
++];
488 static struct HCIInfo
*hci_init(const char *str
)
491 struct bt_scatternet_s
*vlan
= 0;
493 if (!strcmp(str
, "null"))
496 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
498 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
499 else if (!strncmp(str
, "hci", 3)) {
502 if (!strncmp(str
+ 3, ",vlan=", 6)) {
503 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
508 vlan
= qemu_find_bt_vlan(0);
510 return bt_new_hci(vlan
);
513 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
518 static int bt_hci_parse(const char *str
)
523 if (nb_hcis
>= MAX_NICS
) {
524 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
537 bdaddr
.b
[5] = 0x56 + nb_hcis
;
538 hci
->bdaddr_set(hci
, bdaddr
.b
);
540 hci_table
[nb_hcis
++] = hci
;
545 static void bt_vhci_add(int vlan_id
)
547 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
550 fprintf(stderr
, "qemu: warning: adding a VHCI to "
551 "an empty scatternet %i\n", vlan_id
);
553 bt_vhci_init(bt_new_hci(vlan
));
556 static struct bt_device_s
*bt_device_add(const char *opt
)
558 struct bt_scatternet_s
*vlan
;
560 char *endp
= strstr(opt
, ",vlan=");
561 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
564 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
567 vlan_id
= strtol(endp
+ 6, &endp
, 0);
569 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
574 vlan
= qemu_find_bt_vlan(vlan_id
);
577 fprintf(stderr
, "qemu: warning: adding a slave device to "
578 "an empty scatternet %i\n", vlan_id
);
580 if (!strcmp(devname
, "keyboard"))
581 return bt_keyboard_init(vlan
);
583 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
587 static int bt_parse(const char *opt
)
589 const char *endp
, *p
;
592 if (strstart(opt
, "hci", &endp
)) {
593 if (!*endp
|| *endp
== ',') {
595 if (!strstart(endp
, ",vlan=", 0))
598 return bt_hci_parse(opt
);
600 } else if (strstart(opt
, "vhci", &endp
)) {
601 if (!*endp
|| *endp
== ',') {
603 if (strstart(endp
, ",vlan=", &p
)) {
604 vlan
= strtol(p
, (char **) &endp
, 0);
606 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
610 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
619 } else if (strstart(opt
, "device:", &endp
))
620 return !bt_device_add(endp
);
622 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
626 /***********************************************************/
627 /* QEMU Block devices */
629 #define HD_ALIAS "index=%d,media=disk"
630 #define CDROM_ALIAS "index=2,media=cdrom"
631 #define FD_ALIAS "index=%d,if=floppy"
632 #define PFLASH_ALIAS "if=pflash"
633 #define MTD_ALIAS "if=mtd"
634 #define SD_ALIAS "index=0,if=sd"
636 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
643 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
646 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, 0);
651 qemu_opt_set(opts
, "file", file
);
655 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
659 /* seek interface, bus and unit */
661 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
662 if (dinfo
->type
== type
&&
671 DriveInfo
*drive_get_by_id(const char *id
)
675 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
676 if (strcmp(id
, dinfo
->id
))
683 int drive_get_max_bus(BlockInterfaceType type
)
689 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
690 if(dinfo
->type
== type
&&
691 dinfo
->bus
> max_bus
)
692 max_bus
= dinfo
->bus
;
697 const char *drive_get_serial(BlockDriverState
*bdrv
)
701 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
702 if (dinfo
->bdrv
== bdrv
)
703 return dinfo
->serial
;
709 BlockInterfaceErrorAction
drive_get_on_error(
710 BlockDriverState
*bdrv
, int is_read
)
714 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
715 if (dinfo
->bdrv
== bdrv
)
716 return is_read
? dinfo
->on_read_error
: dinfo
->on_write_error
;
719 return is_read
? BLOCK_ERR_REPORT
: BLOCK_ERR_STOP_ENOSPC
;
722 static void bdrv_format_print(void *opaque
, const char *name
)
724 fprintf(stderr
, " %s", name
);
727 void drive_uninit(DriveInfo
*dinfo
)
729 qemu_opts_del(dinfo
->opts
);
730 bdrv_delete(dinfo
->bdrv
);
731 QTAILQ_REMOVE(&drives
, dinfo
, next
);
735 static int parse_block_error_action(const char *buf
, int is_read
)
737 if (!strcmp(buf
, "ignore")) {
738 return BLOCK_ERR_IGNORE
;
739 } else if (!is_read
&& !strcmp(buf
, "enospc")) {
740 return BLOCK_ERR_STOP_ENOSPC
;
741 } else if (!strcmp(buf
, "stop")) {
742 return BLOCK_ERR_STOP_ANY
;
743 } else if (!strcmp(buf
, "report")) {
744 return BLOCK_ERR_REPORT
;
746 fprintf(stderr
, "qemu: '%s' invalid %s error action\n",
747 buf
, is_read
? "read" : "write");
752 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
756 const char *file
= NULL
;
759 const char *mediastr
= "";
760 BlockInterfaceType type
;
761 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
763 int cyls
, heads
, secs
, translation
;
764 BlockDriver
*drv
= NULL
;
765 QEMUMachine
*machine
= opaque
;
770 int on_read_error
, on_write_error
;
777 translation
= BIOS_ATA_TRANSLATION_AUTO
;
779 if (machine
&& machine
->use_scsi
) {
781 max_devs
= MAX_SCSI_DEVS
;
782 pstrcpy(devname
, sizeof(devname
), "scsi");
785 max_devs
= MAX_IDE_DEVS
;
786 pstrcpy(devname
, sizeof(devname
), "ide");
790 /* extract parameters */
791 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
792 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
793 index
= qemu_opt_get_number(opts
, "index", -1);
795 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
796 heads
= qemu_opt_get_number(opts
, "heads", 0);
797 secs
= qemu_opt_get_number(opts
, "secs", 0);
799 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
800 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
802 file
= qemu_opt_get(opts
, "file");
803 serial
= qemu_opt_get(opts
, "serial");
805 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
806 pstrcpy(devname
, sizeof(devname
), buf
);
807 if (!strcmp(buf
, "ide")) {
809 max_devs
= MAX_IDE_DEVS
;
810 } else if (!strcmp(buf
, "scsi")) {
812 max_devs
= MAX_SCSI_DEVS
;
813 } else if (!strcmp(buf
, "floppy")) {
816 } else if (!strcmp(buf
, "pflash")) {
819 } else if (!strcmp(buf
, "mtd")) {
822 } else if (!strcmp(buf
, "sd")) {
825 } else if (!strcmp(buf
, "virtio")) {
828 } else if (!strcmp(buf
, "xen")) {
831 } else if (!strcmp(buf
, "none")) {
835 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
840 if (cyls
|| heads
|| secs
) {
841 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
842 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
845 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
846 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
849 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
850 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
855 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
858 "qemu: '%s' trans must be used with cyls,heads and secs\n",
862 if (!strcmp(buf
, "none"))
863 translation
= BIOS_ATA_TRANSLATION_NONE
;
864 else if (!strcmp(buf
, "lba"))
865 translation
= BIOS_ATA_TRANSLATION_LBA
;
866 else if (!strcmp(buf
, "auto"))
867 translation
= BIOS_ATA_TRANSLATION_AUTO
;
869 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
874 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
875 if (!strcmp(buf
, "disk")) {
877 } else if (!strcmp(buf
, "cdrom")) {
878 if (cyls
|| secs
|| heads
) {
880 "qemu: '%s' invalid physical CHS format\n", buf
);
885 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
890 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
891 if (!strcmp(buf
, "off") || !strcmp(buf
, "none")) {
892 bdrv_flags
|= BDRV_O_NOCACHE
;
893 } else if (!strcmp(buf
, "writeback")) {
894 bdrv_flags
|= BDRV_O_CACHE_WB
;
895 } else if (!strcmp(buf
, "unsafe")) {
896 bdrv_flags
|= BDRV_O_CACHE_WB
;
897 bdrv_flags
|= BDRV_O_NO_FLUSH
;
898 } else if (!strcmp(buf
, "writethrough")) {
899 /* this is the default */
901 fprintf(stderr
, "qemu: invalid cache option\n");
906 #ifdef CONFIG_LINUX_AIO
907 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
908 if (!strcmp(buf
, "native")) {
909 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
910 } else if (!strcmp(buf
, "threads")) {
911 /* this is the default */
913 fprintf(stderr
, "qemu: invalid aio option\n");
919 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
920 if (strcmp(buf
, "?") == 0) {
921 fprintf(stderr
, "qemu: Supported formats:");
922 bdrv_iterate_format(bdrv_format_print
, NULL
);
923 fprintf(stderr
, "\n");
926 drv
= bdrv_find_whitelisted_format(buf
);
928 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
933 on_write_error
= BLOCK_ERR_STOP_ENOSPC
;
934 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
935 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
936 fprintf(stderr
, "werror is no supported by this format\n");
940 on_write_error
= parse_block_error_action(buf
, 0);
941 if (on_write_error
< 0) {
946 on_read_error
= BLOCK_ERR_REPORT
;
947 if ((buf
= qemu_opt_get(opts
, "rerror")) != NULL
) {
948 if (type
!= IF_IDE
&& type
!= IF_VIRTIO
&& type
!= IF_NONE
) {
949 fprintf(stderr
, "rerror is no supported by this format\n");
953 on_read_error
= parse_block_error_action(buf
, 1);
954 if (on_read_error
< 0) {
959 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
960 if (type
!= IF_VIRTIO
) {
961 fprintf(stderr
, "addr is not supported\n");
966 /* compute bus and unit according index */
969 if (bus_id
!= 0 || unit_id
!= -1) {
971 "qemu: index cannot be used with bus and unit\n");
979 unit_id
= index
% max_devs
;
980 bus_id
= index
/ max_devs
;
984 /* if user doesn't specify a unit_id,
985 * try to find the first free
990 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
992 if (max_devs
&& unit_id
>= max_devs
) {
1001 if (max_devs
&& unit_id
>= max_devs
) {
1002 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
1003 unit_id
, max_devs
- 1);
1008 * ignore multiple definitions
1011 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
1018 dinfo
= qemu_mallocz(sizeof(*dinfo
));
1019 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
1020 dinfo
->id
= qemu_strdup(buf
);
1022 /* no id supplied -> create one */
1023 dinfo
->id
= qemu_mallocz(32);
1024 if (type
== IF_IDE
|| type
== IF_SCSI
)
1025 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
1027 snprintf(dinfo
->id
, 32, "%s%i%s%i",
1028 devname
, bus_id
, mediastr
, unit_id
);
1030 snprintf(dinfo
->id
, 32, "%s%s%i",
1031 devname
, mediastr
, unit_id
);
1033 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
1034 dinfo
->devaddr
= devaddr
;
1036 dinfo
->bus
= bus_id
;
1037 dinfo
->unit
= unit_id
;
1038 dinfo
->on_read_error
= on_read_error
;
1039 dinfo
->on_write_error
= on_write_error
;
1042 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
1043 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
1053 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
1054 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
1058 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
1063 /* FIXME: This isn't really a floppy, but it's a reasonable
1066 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
1072 /* add virtio block device */
1073 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1074 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
1075 qemu_opt_set(opts
, "drive", dinfo
->id
);
1077 qemu_opt_set(opts
, "addr", devaddr
);
1087 /* always use cache=unsafe with snapshot */
1088 bdrv_flags
&= ~BDRV_O_CACHE_MASK
;
1089 bdrv_flags
|= (BDRV_O_SNAPSHOT
|BDRV_O_CACHE_WB
|BDRV_O_NO_FLUSH
);
1092 if (media
== MEDIA_CDROM
) {
1093 /* CDROM is fine for any interface, don't check. */
1095 } else if (ro
== 1) {
1096 if (type
!= IF_SCSI
&& type
!= IF_VIRTIO
&& type
!= IF_FLOPPY
&& type
!= IF_NONE
) {
1097 fprintf(stderr
, "qemu: readonly flag not supported for drive with this interface\n");
1102 bdrv_flags
|= ro
? 0 : BDRV_O_RDWR
;
1104 if (bdrv_open(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
1105 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
1106 file
, strerror(errno
));
1110 if (bdrv_key_required(dinfo
->bdrv
))
1116 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1118 QEMUMachine
*machine
= opaque
;
1119 int fatal_error
= 0;
1121 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
1128 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1130 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1131 qemu_opt_set(opts
, "snapshot", "on");
1136 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1138 boot_set_handler
= func
;
1139 boot_set_opaque
= opaque
;
1142 int qemu_boot_set(const char *boot_devices
)
1144 if (!boot_set_handler
) {
1147 return boot_set_handler(boot_set_opaque
, boot_devices
);
1150 static void validate_bootdevices(char *devices
)
1152 /* We just do some generic consistency checks */
1156 for (p
= devices
; *p
!= '\0'; p
++) {
1157 /* Allowed boot devices are:
1158 * a-b: floppy disk drives
1159 * c-f: IDE disk drives
1160 * g-m: machine implementation dependant drives
1161 * n-p: network devices
1162 * It's up to each machine implementation to check if the given boot
1163 * devices match the actual hardware implementation and firmware
1166 if (*p
< 'a' || *p
> 'p') {
1167 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1170 if (bitmap
& (1 << (*p
- 'a'))) {
1171 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1174 bitmap
|= 1 << (*p
- 'a');
1178 static void restore_boot_devices(void *opaque
)
1180 char *standard_boot_devices
= opaque
;
1181 static int first
= 1;
1183 /* Restore boot order and remove ourselves after the first boot */
1189 qemu_boot_set(standard_boot_devices
);
1191 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
1192 qemu_free(standard_boot_devices
);
1195 static void numa_add(const char *optarg
)
1199 unsigned long long value
, endvalue
;
1202 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1203 if (!strcmp(option
, "node")) {
1204 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1205 nodenr
= nb_numa_nodes
;
1207 nodenr
= strtoull(option
, NULL
, 10);
1210 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1211 node_mem
[nodenr
] = 0;
1213 value
= strtoull(option
, &endptr
, 0);
1215 case 0: case 'M': case 'm':
1222 node_mem
[nodenr
] = value
;
1224 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
1225 node_cpumask
[nodenr
] = 0;
1227 value
= strtoull(option
, &endptr
, 10);
1230 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
1232 if (*endptr
== '-') {
1233 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1234 if (endvalue
>= 63) {
1237 "only 63 CPUs in NUMA mode supported.\n");
1239 value
= (2ULL << endvalue
) - (1ULL << value
);
1241 value
= 1ULL << value
;
1244 node_cpumask
[nodenr
] = value
;
1251 static void smp_parse(const char *optarg
)
1253 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1257 smp
= strtoul(optarg
, &endptr
, 10);
1258 if (endptr
!= optarg
) {
1259 if (*endptr
== ',') {
1263 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1264 sockets
= strtoull(option
, NULL
, 10);
1265 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1266 cores
= strtoull(option
, NULL
, 10);
1267 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1268 threads
= strtoull(option
, NULL
, 10);
1269 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1270 max_cpus
= strtoull(option
, NULL
, 10);
1272 /* compute missing values, prefer sockets over cores over threads */
1273 if (smp
== 0 || sockets
== 0) {
1274 sockets
= sockets
> 0 ? sockets
: 1;
1275 cores
= cores
> 0 ? cores
: 1;
1276 threads
= threads
> 0 ? threads
: 1;
1278 smp
= cores
* threads
* sockets
;
1282 threads
= threads
> 0 ? threads
: 1;
1283 cores
= smp
/ (sockets
* threads
);
1286 threads
= smp
/ (cores
* sockets
);
1291 smp_cores
= cores
> 0 ? cores
: 1;
1292 smp_threads
= threads
> 0 ? threads
: 1;
1294 max_cpus
= smp_cpus
;
1297 /***********************************************************/
1300 static int usb_device_add(const char *devname
, int is_hotplug
)
1303 USBDevice
*dev
= NULL
;
1308 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1309 dev
= usbdevice_create(devname
);
1313 /* the other ones */
1314 if (strstart(devname
, "host:", &p
)) {
1315 dev
= usb_host_device_open(p
);
1316 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1317 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1318 bt_new_hci(qemu_find_bt_vlan(0)));
1329 static int usb_device_del(const char *devname
)
1334 if (strstart(devname
, "host:", &p
))
1335 return usb_host_device_close(p
);
1340 p
= strchr(devname
, '.');
1343 bus_num
= strtoul(devname
, NULL
, 0);
1344 addr
= strtoul(p
+ 1, NULL
, 0);
1346 return usb_device_delete_addr(bus_num
, addr
);
1349 static int usb_parse(const char *cmdline
)
1352 r
= usb_device_add(cmdline
, 0);
1354 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1359 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1361 const char *devname
= qdict_get_str(qdict
, "devname");
1362 if (usb_device_add(devname
, 1) < 0) {
1363 error_report("could not add USB device '%s'", devname
);
1367 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1369 const char *devname
= qdict_get_str(qdict
, "devname");
1370 if (usb_device_del(devname
) < 0) {
1371 error_report("could not delete USB device '%s'", devname
);
1375 /***********************************************************/
1376 /* PCMCIA/Cardbus */
1378 static struct pcmcia_socket_entry_s
{
1379 PCMCIASocket
*socket
;
1380 struct pcmcia_socket_entry_s
*next
;
1381 } *pcmcia_sockets
= 0;
1383 void pcmcia_socket_register(PCMCIASocket
*socket
)
1385 struct pcmcia_socket_entry_s
*entry
;
1387 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
1388 entry
->socket
= socket
;
1389 entry
->next
= pcmcia_sockets
;
1390 pcmcia_sockets
= entry
;
1393 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1395 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1397 ptr
= &pcmcia_sockets
;
1398 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1399 if (entry
->socket
== socket
) {
1405 void pcmcia_info(Monitor
*mon
)
1407 struct pcmcia_socket_entry_s
*iter
;
1409 if (!pcmcia_sockets
)
1410 monitor_printf(mon
, "No PCMCIA sockets\n");
1412 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1413 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1414 iter
->socket
->attached
? iter
->socket
->card_string
:
1418 /***********************************************************/
1421 typedef struct IOHandlerRecord
{
1423 IOCanReadHandler
*fd_read_poll
;
1425 IOHandler
*fd_write
;
1428 /* temporary data */
1430 QLIST_ENTRY(IOHandlerRecord
) next
;
1433 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
1434 QLIST_HEAD_INITIALIZER(io_handlers
);
1437 /* XXX: fd_read_poll should be suppressed, but an API change is
1438 necessary in the character devices to suppress fd_can_read(). */
1439 int qemu_set_fd_handler2(int fd
,
1440 IOCanReadHandler
*fd_read_poll
,
1442 IOHandler
*fd_write
,
1445 IOHandlerRecord
*ioh
;
1447 if (!fd_read
&& !fd_write
) {
1448 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1449 if (ioh
->fd
== fd
) {
1455 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1459 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1460 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1463 ioh
->fd_read_poll
= fd_read_poll
;
1464 ioh
->fd_read
= fd_read
;
1465 ioh
->fd_write
= fd_write
;
1466 ioh
->opaque
= opaque
;
1472 int qemu_set_fd_handler(int fd
,
1474 IOHandler
*fd_write
,
1477 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1480 /***********************************************************/
1481 /* machine registration */
1483 static QEMUMachine
*first_machine
= NULL
;
1484 QEMUMachine
*current_machine
= NULL
;
1486 int qemu_register_machine(QEMUMachine
*m
)
1489 pm
= &first_machine
;
1497 static QEMUMachine
*find_machine(const char *name
)
1501 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1502 if (!strcmp(m
->name
, name
))
1504 if (m
->alias
&& !strcmp(m
->alias
, name
))
1510 static QEMUMachine
*find_default_machine(void)
1514 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1515 if (m
->is_default
) {
1522 /***********************************************************/
1523 /* main execution loop */
1525 static void gui_update(void *opaque
)
1527 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1528 DisplayState
*ds
= opaque
;
1529 DisplayChangeListener
*dcl
= ds
->listeners
;
1531 qemu_flush_coalesced_mmio_buffer();
1534 while (dcl
!= NULL
) {
1535 if (dcl
->gui_timer_interval
&&
1536 dcl
->gui_timer_interval
< interval
)
1537 interval
= dcl
->gui_timer_interval
;
1540 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1543 static void nographic_update(void *opaque
)
1545 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1547 qemu_flush_coalesced_mmio_buffer();
1548 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1551 struct vm_change_state_entry
{
1552 VMChangeStateHandler
*cb
;
1554 QLIST_ENTRY (vm_change_state_entry
) entries
;
1557 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1559 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1562 VMChangeStateEntry
*e
;
1564 e
= qemu_mallocz(sizeof (*e
));
1568 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1572 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1574 QLIST_REMOVE (e
, entries
);
1578 void vm_state_notify(int running
, int reason
)
1580 VMChangeStateEntry
*e
;
1582 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1583 e
->cb(e
->opaque
, running
, reason
);
1592 vm_state_notify(1, 0);
1594 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1598 /* reset/shutdown handler */
1600 typedef struct QEMUResetEntry
{
1601 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1602 QEMUResetHandler
*func
;
1606 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1607 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1608 static int reset_requested
;
1609 static int shutdown_requested
;
1610 static int powerdown_requested
;
1611 int debug_requested
;
1612 int vmstop_requested
;
1614 int qemu_shutdown_requested(void)
1616 int r
= shutdown_requested
;
1617 shutdown_requested
= 0;
1621 int qemu_reset_requested(void)
1623 int r
= reset_requested
;
1624 reset_requested
= 0;
1628 int qemu_powerdown_requested(void)
1630 int r
= powerdown_requested
;
1631 powerdown_requested
= 0;
1635 static int qemu_debug_requested(void)
1637 int r
= debug_requested
;
1638 debug_requested
= 0;
1642 static int qemu_vmstop_requested(void)
1644 int r
= vmstop_requested
;
1645 vmstop_requested
= 0;
1649 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1651 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1654 re
->opaque
= opaque
;
1655 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1658 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1662 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1663 if (re
->func
== func
&& re
->opaque
== opaque
) {
1664 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1671 void qemu_system_reset(void)
1673 QEMUResetEntry
*re
, *nre
;
1675 /* reset all devices */
1676 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1677 re
->func(re
->opaque
);
1679 monitor_protocol_event(QEVENT_RESET
, NULL
);
1680 cpu_synchronize_all_post_reset();
1683 void qemu_system_reset_request(void)
1686 shutdown_requested
= 1;
1688 reset_requested
= 1;
1690 qemu_notify_event();
1693 void qemu_system_shutdown_request(void)
1695 shutdown_requested
= 1;
1696 qemu_notify_event();
1699 void qemu_system_powerdown_request(void)
1701 powerdown_requested
= 1;
1702 qemu_notify_event();
1705 void main_loop_wait(int nonblocking
)
1707 IOHandlerRecord
*ioh
;
1708 fd_set rfds
, wfds
, xfds
;
1716 timeout
= qemu_calculate_timeout();
1717 qemu_bh_update_timeout(&timeout
);
1720 os_host_main_loop_wait(&timeout
);
1722 /* poll any events */
1723 /* XXX: separate device handlers from system ones */
1728 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1732 (!ioh
->fd_read_poll
||
1733 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1734 FD_SET(ioh
->fd
, &rfds
);
1738 if (ioh
->fd_write
) {
1739 FD_SET(ioh
->fd
, &wfds
);
1745 tv
.tv_sec
= timeout
/ 1000;
1746 tv
.tv_usec
= (timeout
% 1000) * 1000;
1748 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1750 qemu_mutex_unlock_iothread();
1751 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1752 qemu_mutex_lock_iothread();
1754 IOHandlerRecord
*pioh
;
1756 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1758 QLIST_REMOVE(ioh
, next
);
1762 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1763 ioh
->fd_read(ioh
->opaque
);
1765 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1766 ioh
->fd_write(ioh
->opaque
);
1771 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1773 qemu_run_all_timers();
1775 /* Check bottom-halves last in case any of the earlier events triggered
1781 static int vm_can_run(void)
1783 if (powerdown_requested
)
1785 if (reset_requested
)
1787 if (shutdown_requested
)
1789 if (debug_requested
)
1794 qemu_irq qemu_system_powerdown
;
1796 static void main_loop(void)
1800 qemu_main_loop_start();
1804 bool nonblocking
= false;
1805 #ifdef CONFIG_PROFILER
1808 #ifndef CONFIG_IOTHREAD
1809 nonblocking
= tcg_cpu_exec();
1811 #ifdef CONFIG_PROFILER
1812 ti
= profile_getclock();
1814 main_loop_wait(nonblocking
);
1815 #ifdef CONFIG_PROFILER
1816 dev_time
+= profile_getclock() - ti
;
1818 } while (vm_can_run());
1820 if ((r
= qemu_debug_requested())) {
1823 if (qemu_shutdown_requested()) {
1824 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1831 if (qemu_reset_requested()) {
1833 qemu_system_reset();
1836 if (qemu_powerdown_requested()) {
1837 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1838 qemu_irq_raise(qemu_system_powerdown
);
1840 if ((r
= qemu_vmstop_requested())) {
1847 static void version(void)
1849 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1852 static void help(int exitcode
)
1854 const char *options_help
=
1855 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1857 #define DEFHEADING(text) stringify(text) "\n"
1858 #include "qemu-options.def"
1864 printf("usage: %s [options] [disk_image]\n"
1866 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1869 "During emulation, the following keys are useful:\n"
1870 "ctrl-alt-f toggle full screen\n"
1871 "ctrl-alt-n switch to virtual console 'n'\n"
1872 "ctrl-alt toggle mouse and keyboard grab\n"
1874 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1880 #define HAS_ARG 0x0001
1882 typedef struct QEMUOption
{
1889 static const QEMUOption qemu_options
[] = {
1890 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1891 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1892 { option, opt_arg, opt_enum, arch_mask },
1893 #define DEFHEADING(text)
1894 #include "qemu-options.def"
1900 static void select_vgahw (const char *p
)
1905 vga_interface_type
= VGA_NONE
;
1906 if (strstart(p
, "std", &opts
)) {
1907 vga_interface_type
= VGA_STD
;
1908 } else if (strstart(p
, "cirrus", &opts
)) {
1909 vga_interface_type
= VGA_CIRRUS
;
1910 } else if (strstart(p
, "vmware", &opts
)) {
1911 vga_interface_type
= VGA_VMWARE
;
1912 } else if (strstart(p
, "xenfb", &opts
)) {
1913 vga_interface_type
= VGA_XENFB
;
1914 } else if (!strstart(p
, "none", &opts
)) {
1916 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1920 const char *nextopt
;
1922 if (strstart(opts
, ",retrace=", &nextopt
)) {
1924 if (strstart(opts
, "dumb", &nextopt
))
1925 vga_retrace_method
= VGA_RETRACE_DUMB
;
1926 else if (strstart(opts
, "precise", &nextopt
))
1927 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1928 else goto invalid_vga
;
1929 } else goto invalid_vga
;
1934 static int balloon_parse(const char *arg
)
1938 if (strcmp(arg
, "none") == 0) {
1942 if (!strncmp(arg
, "virtio", 6)) {
1943 if (arg
[6] == ',') {
1944 /* have params -> parse them */
1945 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
1949 /* create empty opts */
1950 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1952 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1959 char *qemu_find_file(int type
, const char *name
)
1965 /* If name contains path separators then try it as a straight path. */
1966 if ((strchr(name
, '/') || strchr(name
, '\\'))
1967 && access(name
, R_OK
) == 0) {
1968 return qemu_strdup(name
);
1971 case QEMU_FILE_TYPE_BIOS
:
1974 case QEMU_FILE_TYPE_KEYMAP
:
1975 subdir
= "keymaps/";
1980 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1981 buf
= qemu_mallocz(len
);
1982 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1983 if (access(buf
, R_OK
)) {
1990 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1992 return qdev_device_help(opts
);
1995 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1999 dev
= qdev_device_add(opts
);
2005 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2007 CharDriverState
*chr
;
2009 chr
= qemu_chr_open_opts(opts
, NULL
);
2016 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2019 ret
= qemu_fsdev_add(opts
);
2025 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2027 CharDriverState
*chr
;
2028 const char *chardev
;
2032 mode
= qemu_opt_get(opts
, "mode");
2036 if (strcmp(mode
, "readline") == 0) {
2037 flags
= MONITOR_USE_READLINE
;
2038 } else if (strcmp(mode
, "control") == 0) {
2039 flags
= MONITOR_USE_CONTROL
;
2041 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2045 if (qemu_opt_get_bool(opts
, "default", 0))
2046 flags
|= MONITOR_IS_DEFAULT
;
2048 chardev
= qemu_opt_get(opts
, "chardev");
2049 chr
= qemu_chr_find(chardev
);
2051 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2055 monitor_init(chr
, flags
);
2059 static void monitor_parse(const char *optarg
, const char *mode
)
2061 static int monitor_device_index
= 0;
2067 if (strstart(optarg
, "chardev:", &p
)) {
2068 snprintf(label
, sizeof(label
), "%s", p
);
2070 snprintf(label
, sizeof(label
), "compat_monitor%d",
2071 monitor_device_index
);
2072 if (monitor_device_index
== 0) {
2075 opts
= qemu_chr_parse_compat(label
, optarg
);
2077 fprintf(stderr
, "parse error: %s\n", optarg
);
2082 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
2084 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2087 qemu_opt_set(opts
, "mode", mode
);
2088 qemu_opt_set(opts
, "chardev", label
);
2090 qemu_opt_set(opts
, "default", "on");
2091 monitor_device_index
++;
2094 struct device_config
{
2096 DEV_USB
, /* -usbdevice */
2098 DEV_SERIAL
, /* -serial */
2099 DEV_PARALLEL
, /* -parallel */
2100 DEV_VIRTCON
, /* -virtioconsole */
2101 DEV_DEBUGCON
, /* -debugcon */
2103 const char *cmdline
;
2104 QTAILQ_ENTRY(device_config
) next
;
2106 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
2108 static void add_device_config(int type
, const char *cmdline
)
2110 struct device_config
*conf
;
2112 conf
= qemu_mallocz(sizeof(*conf
));
2114 conf
->cmdline
= cmdline
;
2115 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2118 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2120 struct device_config
*conf
;
2123 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2124 if (conf
->type
!= type
)
2126 rc
= func(conf
->cmdline
);
2133 static int serial_parse(const char *devname
)
2135 static int index
= 0;
2138 if (strcmp(devname
, "none") == 0)
2140 if (index
== MAX_SERIAL_PORTS
) {
2141 fprintf(stderr
, "qemu: too many serial ports\n");
2144 snprintf(label
, sizeof(label
), "serial%d", index
);
2145 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2146 if (!serial_hds
[index
]) {
2147 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2148 devname
, strerror(errno
));
2155 static int parallel_parse(const char *devname
)
2157 static int index
= 0;
2160 if (strcmp(devname
, "none") == 0)
2162 if (index
== MAX_PARALLEL_PORTS
) {
2163 fprintf(stderr
, "qemu: too many parallel ports\n");
2166 snprintf(label
, sizeof(label
), "parallel%d", index
);
2167 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2168 if (!parallel_hds
[index
]) {
2169 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2170 devname
, strerror(errno
));
2177 static int virtcon_parse(const char *devname
)
2179 static int index
= 0;
2181 QemuOpts
*bus_opts
, *dev_opts
;
2183 if (strcmp(devname
, "none") == 0)
2185 if (index
== MAX_VIRTIO_CONSOLES
) {
2186 fprintf(stderr
, "qemu: too many virtio consoles\n");
2190 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2191 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2193 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2194 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2196 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2197 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2198 if (!virtcon_hds
[index
]) {
2199 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2200 devname
, strerror(errno
));
2203 qemu_opt_set(dev_opts
, "chardev", label
);
2209 static int debugcon_parse(const char *devname
)
2213 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2216 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2218 fprintf(stderr
, "qemu: already have a debugcon device\n");
2221 qemu_opt_set(opts
, "driver", "isa-debugcon");
2222 qemu_opt_set(opts
, "chardev", "debugcon");
2226 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2227 const char **poptarg
, int *poptind
)
2229 const QEMUOption
*popt
;
2230 int optind
= *poptind
;
2231 char *r
= argv
[optind
];
2234 loc_set_cmdline(argv
, optind
, 1);
2236 /* Treat --foo the same as -foo. */
2239 popt
= qemu_options
;
2242 error_report("invalid option");
2245 if (!strcmp(popt
->name
, r
+ 1))
2249 if (popt
->flags
& HAS_ARG
) {
2250 if (optind
>= argc
) {
2251 error_report("requires an argument");
2254 optarg
= argv
[optind
++];
2255 loc_set_cmdline(argv
, optind
- 2, 2);
2266 int main(int argc
, char **argv
, char **envp
)
2268 const char *gdbstub_dev
= NULL
;
2270 int snapshot
, linux_boot
;
2271 const char *icount_option
= NULL
;
2272 const char *initrd_filename
;
2273 const char *kernel_filename
, *kernel_cmdline
;
2274 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2276 DisplayChangeListener
*dcl
;
2277 int cyls
, heads
, secs
, translation
;
2278 QemuOpts
*hda_opts
= NULL
, *opts
;
2281 const char *loadvm
= NULL
;
2282 QEMUMachine
*machine
;
2283 const char *cpu_model
;
2285 const char *pid_file
= NULL
;
2286 const char *incoming
= NULL
;
2287 int show_vnc_port
= 0;
2290 error_set_progname(argv
[0]);
2294 qemu_cache_utils_init(envp
);
2296 QLIST_INIT (&vm_change_state_head
);
2297 os_setup_early_signal_handling();
2299 module_call_init(MODULE_INIT_MACHINE
);
2300 machine
= find_default_machine();
2302 initrd_filename
= NULL
;
2305 kernel_filename
= NULL
;
2306 kernel_cmdline
= "";
2307 cyls
= heads
= secs
= 0;
2308 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2310 for (i
= 0; i
< MAX_NODES
; i
++) {
2312 node_cpumask
[i
] = 0;
2321 /* first pass of option parsing */
2323 while (optind
< argc
) {
2324 if (argv
[optind
][0] != '-') {
2329 const QEMUOption
*popt
;
2331 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2332 switch (popt
->index
) {
2333 case QEMU_OPTION_nodefconfig
:
2343 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2344 if (ret
< 0 && ret
!= -ENOENT
) {
2348 ret
= qemu_read_config_file(arch_config_name
);
2349 if (ret
< 0 && ret
!= -ENOENT
) {
2355 /* second pass of option parsing */
2360 if (argv
[optind
][0] != '-') {
2361 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2363 const QEMUOption
*popt
;
2365 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2366 if (!(popt
->arch_mask
& arch_type
)) {
2367 printf("Option %s not supported for this target\n", popt
->name
);
2370 switch(popt
->index
) {
2372 machine
= find_machine(optarg
);
2375 printf("Supported machines are:\n");
2376 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2378 printf("%-10s %s (alias of %s)\n",
2379 m
->alias
, m
->desc
, m
->name
);
2380 printf("%-10s %s%s\n",
2382 m
->is_default
? " (default)" : "");
2384 exit(*optarg
!= '?');
2387 case QEMU_OPTION_cpu
:
2388 /* hw initialization will check this */
2389 if (*optarg
== '?') {
2390 list_cpus(stdout
, &fprintf
, optarg
);
2396 case QEMU_OPTION_initrd
:
2397 initrd_filename
= optarg
;
2399 case QEMU_OPTION_hda
:
2401 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2403 hda_opts
= drive_add(optarg
, HD_ALIAS
2404 ",cyls=%d,heads=%d,secs=%d%s",
2405 0, cyls
, heads
, secs
,
2406 translation
== BIOS_ATA_TRANSLATION_LBA
?
2408 translation
== BIOS_ATA_TRANSLATION_NONE
?
2409 ",trans=none" : "");
2411 case QEMU_OPTION_hdb
:
2412 case QEMU_OPTION_hdc
:
2413 case QEMU_OPTION_hdd
:
2414 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2416 case QEMU_OPTION_drive
:
2417 drive_add(NULL
, "%s", optarg
);
2419 case QEMU_OPTION_set
:
2420 if (qemu_set_option(optarg
) != 0)
2423 case QEMU_OPTION_global
:
2424 if (qemu_global_option(optarg
) != 0)
2427 case QEMU_OPTION_mtdblock
:
2428 drive_add(optarg
, MTD_ALIAS
);
2430 case QEMU_OPTION_sd
:
2431 drive_add(optarg
, SD_ALIAS
);
2433 case QEMU_OPTION_pflash
:
2434 drive_add(optarg
, PFLASH_ALIAS
);
2436 case QEMU_OPTION_snapshot
:
2439 case QEMU_OPTION_hdachs
:
2443 cyls
= strtol(p
, (char **)&p
, 0);
2444 if (cyls
< 1 || cyls
> 16383)
2449 heads
= strtol(p
, (char **)&p
, 0);
2450 if (heads
< 1 || heads
> 16)
2455 secs
= strtol(p
, (char **)&p
, 0);
2456 if (secs
< 1 || secs
> 63)
2460 if (!strcmp(p
, "none"))
2461 translation
= BIOS_ATA_TRANSLATION_NONE
;
2462 else if (!strcmp(p
, "lba"))
2463 translation
= BIOS_ATA_TRANSLATION_LBA
;
2464 else if (!strcmp(p
, "auto"))
2465 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2468 } else if (*p
!= '\0') {
2470 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2473 if (hda_opts
!= NULL
) {
2475 snprintf(num
, sizeof(num
), "%d", cyls
);
2476 qemu_opt_set(hda_opts
, "cyls", num
);
2477 snprintf(num
, sizeof(num
), "%d", heads
);
2478 qemu_opt_set(hda_opts
, "heads", num
);
2479 snprintf(num
, sizeof(num
), "%d", secs
);
2480 qemu_opt_set(hda_opts
, "secs", num
);
2481 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2482 qemu_opt_set(hda_opts
, "trans", "lba");
2483 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2484 qemu_opt_set(hda_opts
, "trans", "none");
2488 case QEMU_OPTION_numa
:
2489 if (nb_numa_nodes
>= MAX_NODES
) {
2490 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2495 case QEMU_OPTION_nographic
:
2496 display_type
= DT_NOGRAPHIC
;
2498 #ifdef CONFIG_CURSES
2499 case QEMU_OPTION_curses
:
2500 display_type
= DT_CURSES
;
2503 case QEMU_OPTION_portrait
:
2506 case QEMU_OPTION_kernel
:
2507 kernel_filename
= optarg
;
2509 case QEMU_OPTION_append
:
2510 kernel_cmdline
= optarg
;
2512 case QEMU_OPTION_cdrom
:
2513 drive_add(optarg
, CDROM_ALIAS
);
2515 case QEMU_OPTION_boot
:
2517 static const char * const params
[] = {
2518 "order", "once", "menu", NULL
2520 char buf
[sizeof(boot_devices
)];
2521 char *standard_boot_devices
;
2524 if (!strchr(optarg
, '=')) {
2526 pstrcpy(buf
, sizeof(buf
), optarg
);
2527 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2529 "qemu: unknown boot parameter '%s' in '%s'\n",
2535 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2536 validate_bootdevices(buf
);
2537 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2540 if (get_param_value(buf
, sizeof(buf
),
2542 validate_bootdevices(buf
);
2543 standard_boot_devices
= qemu_strdup(boot_devices
);
2544 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2545 qemu_register_reset(restore_boot_devices
,
2546 standard_boot_devices
);
2548 if (get_param_value(buf
, sizeof(buf
),
2550 if (!strcmp(buf
, "on")) {
2552 } else if (!strcmp(buf
, "off")) {
2556 "qemu: invalid option value '%s'\n",
2564 case QEMU_OPTION_fda
:
2565 case QEMU_OPTION_fdb
:
2566 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2568 case QEMU_OPTION_no_fd_bootchk
:
2571 case QEMU_OPTION_netdev
:
2572 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2576 case QEMU_OPTION_net
:
2577 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2582 case QEMU_OPTION_tftp
:
2583 legacy_tftp_prefix
= optarg
;
2585 case QEMU_OPTION_bootp
:
2586 legacy_bootp_filename
= optarg
;
2588 case QEMU_OPTION_redir
:
2589 if (net_slirp_redir(optarg
) < 0)
2593 case QEMU_OPTION_bt
:
2594 add_device_config(DEV_BT
, optarg
);
2596 case QEMU_OPTION_audio_help
:
2597 if (!(audio_available())) {
2598 printf("Option %s not supported for this target\n", popt
->name
);
2604 case QEMU_OPTION_soundhw
:
2605 if (!(audio_available())) {
2606 printf("Option %s not supported for this target\n", popt
->name
);
2609 select_soundhw (optarg
);
2614 case QEMU_OPTION_version
:
2618 case QEMU_OPTION_m
: {
2622 value
= strtoul(optarg
, &ptr
, 10);
2624 case 0: case 'M': case 'm':
2631 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2635 /* On 32-bit hosts, QEMU is limited by virtual address space */
2636 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2637 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2640 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2641 fprintf(stderr
, "qemu: ram size too large\n");
2647 case QEMU_OPTION_mempath
:
2651 case QEMU_OPTION_mem_prealloc
:
2656 set_cpu_log(optarg
);
2659 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2661 case QEMU_OPTION_gdb
:
2662 gdbstub_dev
= optarg
;
2667 case QEMU_OPTION_bios
:
2670 case QEMU_OPTION_singlestep
:
2677 keyboard_layout
= optarg
;
2679 case QEMU_OPTION_localtime
:
2682 case QEMU_OPTION_vga
:
2683 select_vgahw (optarg
);
2690 w
= strtol(p
, (char **)&p
, 10);
2693 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2699 h
= strtol(p
, (char **)&p
, 10);
2704 depth
= strtol(p
, (char **)&p
, 10);
2705 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2706 depth
!= 24 && depth
!= 32)
2708 } else if (*p
== '\0') {
2709 depth
= graphic_depth
;
2716 graphic_depth
= depth
;
2719 case QEMU_OPTION_echr
:
2722 term_escape_char
= strtol(optarg
, &r
, 0);
2724 printf("Bad argument to echr\n");
2727 case QEMU_OPTION_monitor
:
2728 monitor_parse(optarg
, "readline");
2729 default_monitor
= 0;
2731 case QEMU_OPTION_qmp
:
2732 monitor_parse(optarg
, "control");
2733 default_monitor
= 0;
2735 case QEMU_OPTION_mon
:
2736 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
2740 default_monitor
= 0;
2742 case QEMU_OPTION_chardev
:
2743 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
2749 case QEMU_OPTION_fsdev
:
2750 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
2752 fprintf(stderr
, "parse error: %s\n", optarg
);
2756 case QEMU_OPTION_virtfs
: {
2757 char *arg_fsdev
= NULL
;
2758 char *arg_9p
= NULL
;
2761 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
2763 fprintf(stderr
, "parse error: %s\n", optarg
);
2767 len
= strlen(",id=,path=");
2768 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2769 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2770 len
+= strlen(qemu_opt_get(opts
, "path"));
2771 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2774 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
2779 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
2780 qemu_opt_get(opts
, "fstype"),
2781 qemu_opt_get(opts
, "mount_tag"),
2782 qemu_opt_get(opts
, "path"));
2784 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2785 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2786 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2789 fprintf(stderr
, "No memory to parse -device for %s\n",
2794 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2795 qemu_opt_get(opts
, "mount_tag"),
2796 qemu_opt_get(opts
, "mount_tag"));
2798 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
2799 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2803 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
2804 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2808 qemu_free(arg_fsdev
);
2813 case QEMU_OPTION_serial
:
2814 add_device_config(DEV_SERIAL
, optarg
);
2816 if (strncmp(optarg
, "mon:", 4) == 0) {
2817 default_monitor
= 0;
2820 case QEMU_OPTION_watchdog
:
2823 "qemu: only one watchdog option may be given\n");
2828 case QEMU_OPTION_watchdog_action
:
2829 if (select_watchdog_action(optarg
) == -1) {
2830 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2834 case QEMU_OPTION_virtiocon
:
2835 add_device_config(DEV_VIRTCON
, optarg
);
2836 default_virtcon
= 0;
2837 if (strncmp(optarg
, "mon:", 4) == 0) {
2838 default_monitor
= 0;
2841 case QEMU_OPTION_parallel
:
2842 add_device_config(DEV_PARALLEL
, optarg
);
2843 default_parallel
= 0;
2844 if (strncmp(optarg
, "mon:", 4) == 0) {
2845 default_monitor
= 0;
2848 case QEMU_OPTION_debugcon
:
2849 add_device_config(DEV_DEBUGCON
, optarg
);
2851 case QEMU_OPTION_loadvm
:
2854 case QEMU_OPTION_full_screen
:
2858 case QEMU_OPTION_no_frame
:
2861 case QEMU_OPTION_alt_grab
:
2864 case QEMU_OPTION_ctrl_grab
:
2867 case QEMU_OPTION_no_quit
:
2870 case QEMU_OPTION_sdl
:
2871 display_type
= DT_SDL
;
2874 case QEMU_OPTION_pidfile
:
2877 case QEMU_OPTION_win2k_hack
:
2878 win2k_install_hack
= 1;
2880 case QEMU_OPTION_rtc_td_hack
:
2883 case QEMU_OPTION_acpitable
:
2884 do_acpitable_option(optarg
);
2886 case QEMU_OPTION_smbios
:
2887 do_smbios_option(optarg
);
2889 case QEMU_OPTION_enable_kvm
:
2892 case QEMU_OPTION_usb
:
2895 case QEMU_OPTION_usbdevice
:
2897 add_device_config(DEV_USB
, optarg
);
2899 case QEMU_OPTION_device
:
2900 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
2904 case QEMU_OPTION_smp
:
2907 fprintf(stderr
, "Invalid number of CPUs\n");
2910 if (max_cpus
< smp_cpus
) {
2911 fprintf(stderr
, "maxcpus must be equal to or greater than "
2915 if (max_cpus
> 255) {
2916 fprintf(stderr
, "Unsupported number of maxcpus\n");
2920 case QEMU_OPTION_vnc
:
2921 display_type
= DT_VNC
;
2922 vnc_display
= optarg
;
2924 case QEMU_OPTION_no_acpi
:
2927 case QEMU_OPTION_no_hpet
:
2930 case QEMU_OPTION_balloon
:
2931 if (balloon_parse(optarg
) < 0) {
2932 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2936 case QEMU_OPTION_no_reboot
:
2939 case QEMU_OPTION_no_shutdown
:
2942 case QEMU_OPTION_show_cursor
:
2945 case QEMU_OPTION_uuid
:
2946 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2947 fprintf(stderr
, "Fail to parse UUID string."
2948 " Wrong format.\n");
2952 case QEMU_OPTION_option_rom
:
2953 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2954 fprintf(stderr
, "Too many option ROMs\n");
2957 option_rom
[nb_option_roms
] = optarg
;
2960 case QEMU_OPTION_semihosting
:
2961 semihosting_enabled
= 1;
2963 case QEMU_OPTION_name
:
2964 qemu_name
= qemu_strdup(optarg
);
2966 char *p
= strchr(qemu_name
, ',');
2969 if (strncmp(p
, "process=", 8)) {
2970 fprintf(stderr
, "Unknown subargument %s to -name", p
);
2974 os_set_proc_name(p
);
2978 case QEMU_OPTION_prom_env
:
2979 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2980 fprintf(stderr
, "Too many prom variables\n");
2983 prom_envs
[nb_prom_envs
] = optarg
;
2986 case QEMU_OPTION_old_param
:
2989 case QEMU_OPTION_clock
:
2990 configure_alarms(optarg
);
2992 case QEMU_OPTION_startdate
:
2993 configure_rtc_date_offset(optarg
, 1);
2995 case QEMU_OPTION_rtc
:
2996 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
3000 configure_rtc(opts
);
3002 case QEMU_OPTION_tb_size
:
3003 tb_size
= strtol(optarg
, NULL
, 0);
3007 case QEMU_OPTION_icount
:
3008 icount_option
= optarg
;
3010 case QEMU_OPTION_incoming
:
3013 case QEMU_OPTION_nodefaults
:
3015 default_parallel
= 0;
3016 default_virtcon
= 0;
3017 default_monitor
= 0;
3024 case QEMU_OPTION_xen_domid
:
3025 if (!(xen_available())) {
3026 printf("Option %s not supported for this target\n", popt
->name
);
3029 xen_domid
= atoi(optarg
);
3031 case QEMU_OPTION_xen_create
:
3032 if (!(xen_available())) {
3033 printf("Option %s not supported for this target\n", popt
->name
);
3036 xen_mode
= XEN_CREATE
;
3038 case QEMU_OPTION_xen_attach
:
3039 if (!(xen_available())) {
3040 printf("Option %s not supported for this target\n", popt
->name
);
3043 xen_mode
= XEN_ATTACH
;
3045 case QEMU_OPTION_readconfig
:
3047 int ret
= qemu_read_config_file(optarg
);
3049 fprintf(stderr
, "read config %s: %s\n", optarg
,
3055 case QEMU_OPTION_writeconfig
:
3058 if (strcmp(optarg
, "-") == 0) {
3061 fp
= fopen(optarg
, "w");
3063 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3067 qemu_config_write(fp
);
3072 os_parse_cmd_args(popt
->index
, optarg
);
3078 /* If no data_dir is specified then try to find it relative to the
3081 data_dir
= os_find_datadir(argv
[0]);
3083 /* If all else fails use the install patch specified when building. */
3085 data_dir
= CONFIG_QEMU_DATADIR
;
3089 * Default to max_cpus = smp_cpus, in case the user doesn't
3090 * specify a max_cpus value.
3093 max_cpus
= smp_cpus
;
3095 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3096 if (smp_cpus
> machine
->max_cpus
) {
3097 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3098 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3103 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
3104 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
3106 if (machine
->no_serial
) {
3109 if (machine
->no_parallel
) {
3110 default_parallel
= 0;
3112 if (!machine
->use_virtcon
) {
3113 default_virtcon
= 0;
3115 if (machine
->no_vga
) {
3118 if (machine
->no_floppy
) {
3121 if (machine
->no_cdrom
) {
3124 if (machine
->no_sdcard
) {
3128 if (display_type
== DT_NOGRAPHIC
) {
3129 if (default_parallel
)
3130 add_device_config(DEV_PARALLEL
, "null");
3131 if (default_serial
&& default_monitor
) {
3132 add_device_config(DEV_SERIAL
, "mon:stdio");
3133 } else if (default_virtcon
&& default_monitor
) {
3134 add_device_config(DEV_VIRTCON
, "mon:stdio");
3137 add_device_config(DEV_SERIAL
, "stdio");
3138 if (default_virtcon
)
3139 add_device_config(DEV_VIRTCON
, "stdio");
3140 if (default_monitor
)
3141 monitor_parse("stdio", "readline");
3145 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3146 if (default_parallel
)
3147 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3148 if (default_monitor
)
3149 monitor_parse("vc:80Cx24C", "readline");
3150 if (default_virtcon
)
3151 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3154 vga_interface_type
= VGA_CIRRUS
;
3158 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3161 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
3168 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3174 int ret
= kvm_init(smp_cpus
);
3176 if (!kvm_available()) {
3177 printf("KVM not supported for this target\n");
3179 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3185 if (qemu_init_main_loop()) {
3186 fprintf(stderr
, "qemu_init_main_loop failed\n");
3189 linux_boot
= (kernel_filename
!= NULL
);
3191 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3192 fprintf(stderr
, "-append only allowed with -kernel option\n");
3196 if (!linux_boot
&& initrd_filename
!= NULL
) {
3197 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3201 os_set_line_buffering();
3203 if (init_timer_alarm() < 0) {
3204 fprintf(stderr
, "could not initialize alarm timer\n");
3207 configure_icount(icount_option
);
3209 if (net_init_clients() < 0) {
3213 /* init the bluetooth world */
3214 if (foreach_device_config(DEV_BT
, bt_parse
))
3217 /* init the memory */
3219 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3221 /* init the dynamic translator */
3222 cpu_exec_init_all(tb_size
* 1024 * 1024);
3224 bdrv_init_with_whitelist();
3228 if (default_cdrom
) {
3229 /* we always create the cdrom drive, even if no disk is there */
3230 drive_add(NULL
, CDROM_ALIAS
);
3233 if (default_floppy
) {
3234 /* we always create at least one floppy */
3235 drive_add(NULL
, FD_ALIAS
, 0);
3238 if (default_sdcard
) {
3239 /* we always create one sd slot, even if no card is in it */
3240 drive_add(NULL
, SD_ALIAS
);
3243 /* open the virtual block devices */
3245 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3246 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
3249 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3252 if (nb_numa_nodes
> 0) {
3255 if (nb_numa_nodes
> smp_cpus
) {
3256 nb_numa_nodes
= smp_cpus
;
3259 /* If no memory size if given for any node, assume the default case
3260 * and distribute the available memory equally across all nodes
3262 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3263 if (node_mem
[i
] != 0)
3266 if (i
== nb_numa_nodes
) {
3267 uint64_t usedmem
= 0;
3269 /* On Linux, the each node's border has to be 8MB aligned,
3270 * the final node gets the rest.
3272 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3273 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3274 usedmem
+= node_mem
[i
];
3276 node_mem
[i
] = ram_size
- usedmem
;
3279 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3280 if (node_cpumask
[i
] != 0)
3283 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3284 * must cope with this anyway, because there are BIOSes out there in
3285 * real machines which also use this scheme.
3287 if (i
== nb_numa_nodes
) {
3288 for (i
= 0; i
< smp_cpus
; i
++) {
3289 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3294 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
3298 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3300 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3302 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3304 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3307 module_call_init(MODULE_INIT_DEVICE
);
3309 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3313 i
= select_watchdog(watchdog
);
3315 exit (i
== 1 ? 1 : 0);
3318 if (machine
->compat_props
) {
3319 qdev_prop_register_global_list(machine
->compat_props
);
3323 machine
->init(ram_size
, boot_devices
,
3324 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3326 cpu_synchronize_all_post_init();
3328 /* must be after terminal init, SDL library changes signal handlers */
3329 os_setup_signal_handling();
3333 current_machine
= machine
;
3335 /* init USB devices */
3337 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3341 /* init generic devices */
3342 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3345 net_check_clients();
3347 /* just use the first displaystate for the moment */
3348 ds
= get_displaystate();
3350 if (display_type
== DT_DEFAULT
) {
3351 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3352 display_type
= DT_SDL
;
3354 display_type
= DT_VNC
;
3355 vnc_display
= "localhost:0,to=99";
3361 switch (display_type
) {
3364 #if defined(CONFIG_CURSES)
3366 curses_display_init(ds
, full_screen
);
3369 #if defined(CONFIG_SDL)
3371 sdl_display_init(ds
, full_screen
, no_frame
);
3373 #elif defined(CONFIG_COCOA)
3375 cocoa_display_init(ds
, full_screen
);
3379 vnc_display_init(ds
);
3380 if (vnc_display_open(ds
, vnc_display
) < 0)
3383 if (show_vnc_port
) {
3384 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3392 dcl
= ds
->listeners
;
3393 while (dcl
!= NULL
) {
3394 if (dcl
->dpy_refresh
!= NULL
) {
3395 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3396 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3402 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3403 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3404 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3407 text_consoles_set_display(ds
);
3409 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3410 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3415 qdev_machine_creation_done();
3417 if (rom_load_all() != 0) {
3418 fprintf(stderr
, "rom loading failed\n");
3422 qemu_system_reset();
3424 if (load_vmstate(loadvm
) < 0) {
3430 qemu_start_incoming_migration(incoming
);
3431 } else if (autostart
) {