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 "blockdev.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 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
174 DisplayType display_type
= DT_DEFAULT
;
175 const char* keyboard_layout
= NULL
;
177 const char *mem_path
= NULL
;
179 int mem_prealloc
= 0; /* force preallocation of physical target memory */
182 NICInfo nd_table
[MAX_NICS
];
185 static int rtc_utc
= 1;
186 static int rtc_date_offset
= -1; /* -1 means no change */
187 QEMUClock
*rtc_clock
;
188 int vga_interface_type
= VGA_NONE
;
189 static int full_screen
= 0;
191 static int no_frame
= 0;
194 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
195 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
196 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
197 int win2k_install_hack
= 0;
205 const char *vnc_display
;
206 int acpi_enabled
= 1;
212 int graphic_rotate
= 0;
213 uint8_t irq0override
= 1;
214 const char *watchdog
;
215 const char *option_rom
[MAX_OPTION_ROMS
];
217 int semihosting_enabled
= 0;
219 const char *qemu_name
;
222 unsigned int nb_prom_envs
= 0;
223 const char *prom_envs
[MAX_PROM_ENVS
];
227 uint64_t node_mem
[MAX_NODES
];
228 uint64_t node_cpumask
[MAX_NODES
];
230 static QEMUTimer
*nographic_timer
;
232 uint8_t qemu_uuid
[16];
234 static QEMUBootSetHandler
*boot_set_handler
;
235 static void *boot_set_opaque
;
239 enum xen_mode xen_mode
= XEN_EMULATE
;
241 static int default_serial
= 1;
242 static int default_parallel
= 1;
243 static int default_virtcon
= 1;
244 static int default_monitor
= 1;
245 static int default_vga
= 1;
246 static int default_floppy
= 1;
247 static int default_cdrom
= 1;
248 static int default_sdcard
= 1;
254 { .driver
= "isa-serial", .flag
= &default_serial
},
255 { .driver
= "isa-parallel", .flag
= &default_parallel
},
256 { .driver
= "isa-fdc", .flag
= &default_floppy
},
257 { .driver
= "ide-drive", .flag
= &default_cdrom
},
258 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
259 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
260 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
261 { .driver
= "VGA", .flag
= &default_vga
},
262 { .driver
= "cirrus-vga", .flag
= &default_vga
},
263 { .driver
= "vmware-svga", .flag
= &default_vga
},
266 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
268 const char *driver
= qemu_opt_get(opts
, "driver");
273 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
274 if (strcmp(default_list
[i
].driver
, driver
) != 0)
276 *(default_list
[i
].flag
) = 0;
281 /***********************************************************/
282 /* real time host monotonic timer */
284 /* compute with 96 bit intermediate result: (a*b)/c */
285 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
290 #ifdef HOST_WORDS_BIGENDIAN
300 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
301 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
304 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
308 /***********************************************************/
309 /* host time/date access */
310 void qemu_get_timedate(struct tm
*tm
, int offset
)
317 if (rtc_date_offset
== -1) {
321 ret
= localtime(&ti
);
323 ti
-= rtc_date_offset
;
327 memcpy(tm
, ret
, sizeof(struct tm
));
330 int qemu_timedate_diff(struct tm
*tm
)
334 if (rtc_date_offset
== -1)
336 seconds
= mktimegm(tm
);
338 seconds
= mktime(tm
);
340 seconds
= mktimegm(tm
) + rtc_date_offset
;
342 return seconds
- time(NULL
);
345 void rtc_change_mon_event(struct tm
*tm
)
349 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
350 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
351 qobject_decref(data
);
354 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
356 time_t rtc_start_date
;
359 if (!strcmp(startdate
, "now") && legacy
) {
360 rtc_date_offset
= -1;
362 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
370 } else if (sscanf(startdate
, "%d-%d-%d",
382 rtc_start_date
= mktimegm(&tm
);
383 if (rtc_start_date
== -1) {
385 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
386 "'2006-06-17T16:01:21' or '2006-06-17'\n");
389 rtc_date_offset
= time(NULL
) - rtc_start_date
;
393 static void configure_rtc(QemuOpts
*opts
)
397 value
= qemu_opt_get(opts
, "base");
399 if (!strcmp(value
, "utc")) {
401 } else if (!strcmp(value
, "localtime")) {
404 configure_rtc_date_offset(value
, 0);
407 value
= qemu_opt_get(opts
, "clock");
409 if (!strcmp(value
, "host")) {
410 rtc_clock
= host_clock
;
411 } else if (!strcmp(value
, "vm")) {
412 rtc_clock
= vm_clock
;
414 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
418 value
= qemu_opt_get(opts
, "driftfix");
420 if (!strcmp(value
, "slew")) {
422 } else if (!strcmp(value
, "none")) {
425 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
431 /***********************************************************/
432 /* Bluetooth support */
435 static struct HCIInfo
*hci_table
[MAX_NICS
];
437 static struct bt_vlan_s
{
438 struct bt_scatternet_s net
;
440 struct bt_vlan_s
*next
;
443 /* find or alloc a new bluetooth "VLAN" */
444 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
446 struct bt_vlan_s
**pvlan
, *vlan
;
447 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
451 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
453 pvlan
= &first_bt_vlan
;
454 while (*pvlan
!= NULL
)
455 pvlan
= &(*pvlan
)->next
;
460 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
464 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
469 static struct HCIInfo null_hci
= {
470 .cmd_send
= null_hci_send
,
471 .sco_send
= null_hci_send
,
472 .acl_send
= null_hci_send
,
473 .bdaddr_set
= null_hci_addr_set
,
476 struct HCIInfo
*qemu_next_hci(void)
478 if (cur_hci
== nb_hcis
)
481 return hci_table
[cur_hci
++];
484 static struct HCIInfo
*hci_init(const char *str
)
487 struct bt_scatternet_s
*vlan
= 0;
489 if (!strcmp(str
, "null"))
492 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
494 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
495 else if (!strncmp(str
, "hci", 3)) {
498 if (!strncmp(str
+ 3, ",vlan=", 6)) {
499 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
504 vlan
= qemu_find_bt_vlan(0);
506 return bt_new_hci(vlan
);
509 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
514 static int bt_hci_parse(const char *str
)
519 if (nb_hcis
>= MAX_NICS
) {
520 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
533 bdaddr
.b
[5] = 0x56 + nb_hcis
;
534 hci
->bdaddr_set(hci
, bdaddr
.b
);
536 hci_table
[nb_hcis
++] = hci
;
541 static void bt_vhci_add(int vlan_id
)
543 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
546 fprintf(stderr
, "qemu: warning: adding a VHCI to "
547 "an empty scatternet %i\n", vlan_id
);
549 bt_vhci_init(bt_new_hci(vlan
));
552 static struct bt_device_s
*bt_device_add(const char *opt
)
554 struct bt_scatternet_s
*vlan
;
556 char *endp
= strstr(opt
, ",vlan=");
557 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
560 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
563 vlan_id
= strtol(endp
+ 6, &endp
, 0);
565 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
570 vlan
= qemu_find_bt_vlan(vlan_id
);
573 fprintf(stderr
, "qemu: warning: adding a slave device to "
574 "an empty scatternet %i\n", vlan_id
);
576 if (!strcmp(devname
, "keyboard"))
577 return bt_keyboard_init(vlan
);
579 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
583 static int bt_parse(const char *opt
)
585 const char *endp
, *p
;
588 if (strstart(opt
, "hci", &endp
)) {
589 if (!*endp
|| *endp
== ',') {
591 if (!strstart(endp
, ",vlan=", 0))
594 return bt_hci_parse(opt
);
596 } else if (strstart(opt
, "vhci", &endp
)) {
597 if (!*endp
|| *endp
== ',') {
599 if (strstart(endp
, ",vlan=", &p
)) {
600 vlan
= strtol(p
, (char **) &endp
, 0);
602 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
606 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
615 } else if (strstart(opt
, "device:", &endp
))
616 return !bt_device_add(endp
);
618 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
622 /***********************************************************/
623 /* QEMU Block devices */
625 #define HD_ALIAS "index=%d,media=disk"
626 #define CDROM_ALIAS "index=2,media=cdrom"
627 #define FD_ALIAS "index=%d,if=floppy"
628 #define PFLASH_ALIAS "if=pflash"
629 #define MTD_ALIAS "if=mtd"
630 #define SD_ALIAS "index=0,if=sd"
632 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
634 int *use_scsi
= opaque
;
637 if (drive_init(opts
, *use_scsi
, &fatal_error
) == NULL
) {
644 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
646 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
647 qemu_opt_set(opts
, "snapshot", "on");
652 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
654 boot_set_handler
= func
;
655 boot_set_opaque
= opaque
;
658 int qemu_boot_set(const char *boot_devices
)
660 if (!boot_set_handler
) {
663 return boot_set_handler(boot_set_opaque
, boot_devices
);
666 static void validate_bootdevices(char *devices
)
668 /* We just do some generic consistency checks */
672 for (p
= devices
; *p
!= '\0'; p
++) {
673 /* Allowed boot devices are:
674 * a-b: floppy disk drives
675 * c-f: IDE disk drives
676 * g-m: machine implementation dependant drives
677 * n-p: network devices
678 * It's up to each machine implementation to check if the given boot
679 * devices match the actual hardware implementation and firmware
682 if (*p
< 'a' || *p
> 'p') {
683 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
686 if (bitmap
& (1 << (*p
- 'a'))) {
687 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
690 bitmap
|= 1 << (*p
- 'a');
694 static void restore_boot_devices(void *opaque
)
696 char *standard_boot_devices
= opaque
;
697 static int first
= 1;
699 /* Restore boot order and remove ourselves after the first boot */
705 qemu_boot_set(standard_boot_devices
);
707 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
708 qemu_free(standard_boot_devices
);
711 static void numa_add(const char *optarg
)
715 unsigned long long value
, endvalue
;
718 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
719 if (!strcmp(option
, "node")) {
720 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
721 nodenr
= nb_numa_nodes
;
723 nodenr
= strtoull(option
, NULL
, 10);
726 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
727 node_mem
[nodenr
] = 0;
729 value
= strtoull(option
, &endptr
, 0);
731 case 0: case 'M': case 'm':
738 node_mem
[nodenr
] = value
;
740 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
741 node_cpumask
[nodenr
] = 0;
743 value
= strtoull(option
, &endptr
, 10);
746 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
748 if (*endptr
== '-') {
749 endvalue
= strtoull(endptr
+1, &endptr
, 10);
750 if (endvalue
>= 63) {
753 "only 63 CPUs in NUMA mode supported.\n");
755 value
= (2ULL << endvalue
) - (1ULL << value
);
757 value
= 1ULL << value
;
760 node_cpumask
[nodenr
] = value
;
767 static void smp_parse(const char *optarg
)
769 int smp
, sockets
= 0, threads
= 0, cores
= 0;
773 smp
= strtoul(optarg
, &endptr
, 10);
774 if (endptr
!= optarg
) {
775 if (*endptr
== ',') {
779 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
780 sockets
= strtoull(option
, NULL
, 10);
781 if (get_param_value(option
, 128, "cores", endptr
) != 0)
782 cores
= strtoull(option
, NULL
, 10);
783 if (get_param_value(option
, 128, "threads", endptr
) != 0)
784 threads
= strtoull(option
, NULL
, 10);
785 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
786 max_cpus
= strtoull(option
, NULL
, 10);
788 /* compute missing values, prefer sockets over cores over threads */
789 if (smp
== 0 || sockets
== 0) {
790 sockets
= sockets
> 0 ? sockets
: 1;
791 cores
= cores
> 0 ? cores
: 1;
792 threads
= threads
> 0 ? threads
: 1;
794 smp
= cores
* threads
* sockets
;
798 threads
= threads
> 0 ? threads
: 1;
799 cores
= smp
/ (sockets
* threads
);
802 threads
= smp
/ (cores
* sockets
);
807 smp_cores
= cores
> 0 ? cores
: 1;
808 smp_threads
= threads
> 0 ? threads
: 1;
813 /***********************************************************/
816 static int usb_device_add(const char *devname
)
819 USBDevice
*dev
= NULL
;
824 /* drivers with .usbdevice_name entry in USBDeviceInfo */
825 dev
= usbdevice_create(devname
);
830 if (strstart(devname
, "host:", &p
)) {
831 dev
= usb_host_device_open(p
);
832 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
833 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
834 bt_new_hci(qemu_find_bt_vlan(0)));
845 static int usb_device_del(const char *devname
)
850 if (strstart(devname
, "host:", &p
))
851 return usb_host_device_close(p
);
856 p
= strchr(devname
, '.');
859 bus_num
= strtoul(devname
, NULL
, 0);
860 addr
= strtoul(p
+ 1, NULL
, 0);
862 return usb_device_delete_addr(bus_num
, addr
);
865 static int usb_parse(const char *cmdline
)
868 r
= usb_device_add(cmdline
);
870 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
875 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
877 const char *devname
= qdict_get_str(qdict
, "devname");
878 if (usb_device_add(devname
) < 0) {
879 error_report("could not add USB device '%s'", devname
);
883 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
885 const char *devname
= qdict_get_str(qdict
, "devname");
886 if (usb_device_del(devname
) < 0) {
887 error_report("could not delete USB device '%s'", devname
);
891 /***********************************************************/
894 static struct pcmcia_socket_entry_s
{
895 PCMCIASocket
*socket
;
896 struct pcmcia_socket_entry_s
*next
;
897 } *pcmcia_sockets
= 0;
899 void pcmcia_socket_register(PCMCIASocket
*socket
)
901 struct pcmcia_socket_entry_s
*entry
;
903 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
904 entry
->socket
= socket
;
905 entry
->next
= pcmcia_sockets
;
906 pcmcia_sockets
= entry
;
909 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
911 struct pcmcia_socket_entry_s
*entry
, **ptr
;
913 ptr
= &pcmcia_sockets
;
914 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
915 if (entry
->socket
== socket
) {
921 void pcmcia_info(Monitor
*mon
)
923 struct pcmcia_socket_entry_s
*iter
;
926 monitor_printf(mon
, "No PCMCIA sockets\n");
928 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
929 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
930 iter
->socket
->attached
? iter
->socket
->card_string
:
934 /***********************************************************/
937 typedef struct IOHandlerRecord
{
939 IOCanReadHandler
*fd_read_poll
;
946 QLIST_ENTRY(IOHandlerRecord
) next
;
949 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
950 QLIST_HEAD_INITIALIZER(io_handlers
);
953 /* XXX: fd_read_poll should be suppressed, but an API change is
954 necessary in the character devices to suppress fd_can_read(). */
955 int qemu_set_fd_handler2(int fd
,
956 IOCanReadHandler
*fd_read_poll
,
961 IOHandlerRecord
*ioh
;
963 if (!fd_read
&& !fd_write
) {
964 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
971 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
975 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
976 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
979 ioh
->fd_read_poll
= fd_read_poll
;
980 ioh
->fd_read
= fd_read
;
981 ioh
->fd_write
= fd_write
;
982 ioh
->opaque
= opaque
;
988 int qemu_set_fd_handler(int fd
,
993 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
996 /***********************************************************/
997 /* machine registration */
999 static QEMUMachine
*first_machine
= NULL
;
1000 QEMUMachine
*current_machine
= NULL
;
1002 int qemu_register_machine(QEMUMachine
*m
)
1005 pm
= &first_machine
;
1013 static QEMUMachine
*find_machine(const char *name
)
1017 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1018 if (!strcmp(m
->name
, name
))
1020 if (m
->alias
&& !strcmp(m
->alias
, name
))
1026 static QEMUMachine
*find_default_machine(void)
1030 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1031 if (m
->is_default
) {
1038 /***********************************************************/
1039 /* main execution loop */
1041 static void gui_update(void *opaque
)
1043 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1044 DisplayState
*ds
= opaque
;
1045 DisplayChangeListener
*dcl
= ds
->listeners
;
1047 qemu_flush_coalesced_mmio_buffer();
1050 while (dcl
!= NULL
) {
1051 if (dcl
->gui_timer_interval
&&
1052 dcl
->gui_timer_interval
< interval
)
1053 interval
= dcl
->gui_timer_interval
;
1056 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1059 static void nographic_update(void *opaque
)
1061 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1063 qemu_flush_coalesced_mmio_buffer();
1064 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1067 struct vm_change_state_entry
{
1068 VMChangeStateHandler
*cb
;
1070 QLIST_ENTRY (vm_change_state_entry
) entries
;
1073 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1075 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1078 VMChangeStateEntry
*e
;
1080 e
= qemu_mallocz(sizeof (*e
));
1084 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1088 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1090 QLIST_REMOVE (e
, entries
);
1094 void vm_state_notify(int running
, int reason
)
1096 VMChangeStateEntry
*e
;
1098 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1099 e
->cb(e
->opaque
, running
, reason
);
1108 vm_state_notify(1, 0);
1110 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1114 /* reset/shutdown handler */
1116 typedef struct QEMUResetEntry
{
1117 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1118 QEMUResetHandler
*func
;
1122 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1123 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1124 static int reset_requested
;
1125 static int shutdown_requested
;
1126 static int powerdown_requested
;
1127 int debug_requested
;
1128 int vmstop_requested
;
1130 int qemu_shutdown_requested(void)
1132 int r
= shutdown_requested
;
1133 shutdown_requested
= 0;
1137 int qemu_reset_requested(void)
1139 int r
= reset_requested
;
1140 reset_requested
= 0;
1144 int qemu_powerdown_requested(void)
1146 int r
= powerdown_requested
;
1147 powerdown_requested
= 0;
1151 static int qemu_debug_requested(void)
1153 int r
= debug_requested
;
1154 debug_requested
= 0;
1158 static int qemu_vmstop_requested(void)
1160 int r
= vmstop_requested
;
1161 vmstop_requested
= 0;
1165 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1167 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1170 re
->opaque
= opaque
;
1171 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1174 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1178 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1179 if (re
->func
== func
&& re
->opaque
== opaque
) {
1180 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1187 void qemu_system_reset(void)
1189 QEMUResetEntry
*re
, *nre
;
1191 /* reset all devices */
1192 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1193 re
->func(re
->opaque
);
1195 monitor_protocol_event(QEVENT_RESET
, NULL
);
1196 cpu_synchronize_all_post_reset();
1199 void qemu_system_reset_request(void)
1202 shutdown_requested
= 1;
1204 reset_requested
= 1;
1206 qemu_notify_event();
1209 void qemu_system_shutdown_request(void)
1211 shutdown_requested
= 1;
1212 qemu_notify_event();
1215 void qemu_system_powerdown_request(void)
1217 powerdown_requested
= 1;
1218 qemu_notify_event();
1221 void main_loop_wait(int nonblocking
)
1223 IOHandlerRecord
*ioh
;
1224 fd_set rfds
, wfds
, xfds
;
1232 timeout
= qemu_calculate_timeout();
1233 qemu_bh_update_timeout(&timeout
);
1236 os_host_main_loop_wait(&timeout
);
1238 /* poll any events */
1239 /* XXX: separate device handlers from system ones */
1244 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1248 (!ioh
->fd_read_poll
||
1249 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1250 FD_SET(ioh
->fd
, &rfds
);
1254 if (ioh
->fd_write
) {
1255 FD_SET(ioh
->fd
, &wfds
);
1261 tv
.tv_sec
= timeout
/ 1000;
1262 tv
.tv_usec
= (timeout
% 1000) * 1000;
1264 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1266 qemu_mutex_unlock_iothread();
1267 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1268 qemu_mutex_lock_iothread();
1270 IOHandlerRecord
*pioh
;
1272 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1274 QLIST_REMOVE(ioh
, next
);
1278 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1279 ioh
->fd_read(ioh
->opaque
);
1281 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1282 ioh
->fd_write(ioh
->opaque
);
1287 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1289 qemu_run_all_timers();
1291 /* Check bottom-halves last in case any of the earlier events triggered
1297 static int vm_can_run(void)
1299 if (powerdown_requested
)
1301 if (reset_requested
)
1303 if (shutdown_requested
)
1305 if (debug_requested
)
1310 qemu_irq qemu_system_powerdown
;
1312 static void main_loop(void)
1316 qemu_main_loop_start();
1320 bool nonblocking
= false;
1321 #ifdef CONFIG_PROFILER
1324 #ifndef CONFIG_IOTHREAD
1325 nonblocking
= tcg_cpu_exec();
1327 #ifdef CONFIG_PROFILER
1328 ti
= profile_getclock();
1330 main_loop_wait(nonblocking
);
1331 #ifdef CONFIG_PROFILER
1332 dev_time
+= profile_getclock() - ti
;
1334 } while (vm_can_run());
1336 if ((r
= qemu_debug_requested())) {
1339 if (qemu_shutdown_requested()) {
1340 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1347 if (qemu_reset_requested()) {
1349 qemu_system_reset();
1352 if (qemu_powerdown_requested()) {
1353 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1354 qemu_irq_raise(qemu_system_powerdown
);
1356 if ((r
= qemu_vmstop_requested())) {
1364 static void version(void)
1366 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1369 static void help(int exitcode
)
1371 const char *options_help
=
1372 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1374 #define DEFHEADING(text) stringify(text) "\n"
1375 #include "qemu-options.def"
1381 printf("usage: %s [options] [disk_image]\n"
1383 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1386 "During emulation, the following keys are useful:\n"
1387 "ctrl-alt-f toggle full screen\n"
1388 "ctrl-alt-n switch to virtual console 'n'\n"
1389 "ctrl-alt toggle mouse and keyboard grab\n"
1391 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1397 #define HAS_ARG 0x0001
1399 typedef struct QEMUOption
{
1406 static const QEMUOption qemu_options
[] = {
1407 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1408 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1409 { option, opt_arg, opt_enum, arch_mask },
1410 #define DEFHEADING(text)
1411 #include "qemu-options.def"
1417 static void select_vgahw (const char *p
)
1422 vga_interface_type
= VGA_NONE
;
1423 if (strstart(p
, "std", &opts
)) {
1424 vga_interface_type
= VGA_STD
;
1425 } else if (strstart(p
, "cirrus", &opts
)) {
1426 vga_interface_type
= VGA_CIRRUS
;
1427 } else if (strstart(p
, "vmware", &opts
)) {
1428 vga_interface_type
= VGA_VMWARE
;
1429 } else if (strstart(p
, "xenfb", &opts
)) {
1430 vga_interface_type
= VGA_XENFB
;
1431 } else if (!strstart(p
, "none", &opts
)) {
1433 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1437 const char *nextopt
;
1439 if (strstart(opts
, ",retrace=", &nextopt
)) {
1441 if (strstart(opts
, "dumb", &nextopt
))
1442 vga_retrace_method
= VGA_RETRACE_DUMB
;
1443 else if (strstart(opts
, "precise", &nextopt
))
1444 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1445 else goto invalid_vga
;
1446 } else goto invalid_vga
;
1451 static int balloon_parse(const char *arg
)
1455 if (strcmp(arg
, "none") == 0) {
1459 if (!strncmp(arg
, "virtio", 6)) {
1460 if (arg
[6] == ',') {
1461 /* have params -> parse them */
1462 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
1466 /* create empty opts */
1467 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1469 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1476 char *qemu_find_file(int type
, const char *name
)
1482 /* If name contains path separators then try it as a straight path. */
1483 if ((strchr(name
, '/') || strchr(name
, '\\'))
1484 && access(name
, R_OK
) == 0) {
1485 return qemu_strdup(name
);
1488 case QEMU_FILE_TYPE_BIOS
:
1491 case QEMU_FILE_TYPE_KEYMAP
:
1492 subdir
= "keymaps/";
1497 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1498 buf
= qemu_mallocz(len
);
1499 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1500 if (access(buf
, R_OK
)) {
1507 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1509 return qdev_device_help(opts
);
1512 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1516 dev
= qdev_device_add(opts
);
1522 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1524 CharDriverState
*chr
;
1526 chr
= qemu_chr_open_opts(opts
, NULL
);
1533 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1536 ret
= qemu_fsdev_add(opts
);
1542 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1544 CharDriverState
*chr
;
1545 const char *chardev
;
1549 mode
= qemu_opt_get(opts
, "mode");
1553 if (strcmp(mode
, "readline") == 0) {
1554 flags
= MONITOR_USE_READLINE
;
1555 } else if (strcmp(mode
, "control") == 0) {
1556 flags
= MONITOR_USE_CONTROL
;
1558 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1562 if (qemu_opt_get_bool(opts
, "default", 0))
1563 flags
|= MONITOR_IS_DEFAULT
;
1565 chardev
= qemu_opt_get(opts
, "chardev");
1566 chr
= qemu_chr_find(chardev
);
1568 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1572 monitor_init(chr
, flags
);
1576 static void monitor_parse(const char *optarg
, const char *mode
)
1578 static int monitor_device_index
= 0;
1584 if (strstart(optarg
, "chardev:", &p
)) {
1585 snprintf(label
, sizeof(label
), "%s", p
);
1587 snprintf(label
, sizeof(label
), "compat_monitor%d",
1588 monitor_device_index
);
1589 if (monitor_device_index
== 0) {
1592 opts
= qemu_chr_parse_compat(label
, optarg
);
1594 fprintf(stderr
, "parse error: %s\n", optarg
);
1599 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
1601 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1604 qemu_opt_set(opts
, "mode", mode
);
1605 qemu_opt_set(opts
, "chardev", label
);
1607 qemu_opt_set(opts
, "default", "on");
1608 monitor_device_index
++;
1611 struct device_config
{
1613 DEV_USB
, /* -usbdevice */
1615 DEV_SERIAL
, /* -serial */
1616 DEV_PARALLEL
, /* -parallel */
1617 DEV_VIRTCON
, /* -virtioconsole */
1618 DEV_DEBUGCON
, /* -debugcon */
1620 const char *cmdline
;
1621 QTAILQ_ENTRY(device_config
) next
;
1623 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1625 static void add_device_config(int type
, const char *cmdline
)
1627 struct device_config
*conf
;
1629 conf
= qemu_mallocz(sizeof(*conf
));
1631 conf
->cmdline
= cmdline
;
1632 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1635 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1637 struct device_config
*conf
;
1640 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1641 if (conf
->type
!= type
)
1643 rc
= func(conf
->cmdline
);
1650 static int serial_parse(const char *devname
)
1652 static int index
= 0;
1655 if (strcmp(devname
, "none") == 0)
1657 if (index
== MAX_SERIAL_PORTS
) {
1658 fprintf(stderr
, "qemu: too many serial ports\n");
1661 snprintf(label
, sizeof(label
), "serial%d", index
);
1662 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1663 if (!serial_hds
[index
]) {
1664 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1665 devname
, strerror(errno
));
1672 static int parallel_parse(const char *devname
)
1674 static int index
= 0;
1677 if (strcmp(devname
, "none") == 0)
1679 if (index
== MAX_PARALLEL_PORTS
) {
1680 fprintf(stderr
, "qemu: too many parallel ports\n");
1683 snprintf(label
, sizeof(label
), "parallel%d", index
);
1684 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1685 if (!parallel_hds
[index
]) {
1686 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1687 devname
, strerror(errno
));
1694 static int virtcon_parse(const char *devname
)
1696 static int index
= 0;
1698 QemuOpts
*bus_opts
, *dev_opts
;
1700 if (strcmp(devname
, "none") == 0)
1702 if (index
== MAX_VIRTIO_CONSOLES
) {
1703 fprintf(stderr
, "qemu: too many virtio consoles\n");
1707 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1708 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1710 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1711 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1713 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1714 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1715 if (!virtcon_hds
[index
]) {
1716 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1717 devname
, strerror(errno
));
1720 qemu_opt_set(dev_opts
, "chardev", label
);
1726 static int debugcon_parse(const char *devname
)
1730 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
1733 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
1735 fprintf(stderr
, "qemu: already have a debugcon device\n");
1738 qemu_opt_set(opts
, "driver", "isa-debugcon");
1739 qemu_opt_set(opts
, "chardev", "debugcon");
1743 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
1744 const char **poptarg
, int *poptind
)
1746 const QEMUOption
*popt
;
1747 int optind
= *poptind
;
1748 char *r
= argv
[optind
];
1751 loc_set_cmdline(argv
, optind
, 1);
1753 /* Treat --foo the same as -foo. */
1756 popt
= qemu_options
;
1759 error_report("invalid option");
1762 if (!strcmp(popt
->name
, r
+ 1))
1766 if (popt
->flags
& HAS_ARG
) {
1767 if (optind
>= argc
) {
1768 error_report("requires an argument");
1771 optarg
= argv
[optind
++];
1772 loc_set_cmdline(argv
, optind
- 2, 2);
1783 int main(int argc
, char **argv
, char **envp
)
1785 const char *gdbstub_dev
= NULL
;
1787 int snapshot
, linux_boot
;
1788 const char *icount_option
= NULL
;
1789 const char *initrd_filename
;
1790 const char *kernel_filename
, *kernel_cmdline
;
1791 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
1793 DisplayChangeListener
*dcl
;
1794 int cyls
, heads
, secs
, translation
;
1795 QemuOpts
*hda_opts
= NULL
, *opts
;
1798 const char *loadvm
= NULL
;
1799 QEMUMachine
*machine
;
1800 const char *cpu_model
;
1802 const char *pid_file
= NULL
;
1803 const char *incoming
= NULL
;
1804 int show_vnc_port
= 0;
1807 error_set_progname(argv
[0]);
1811 qemu_cache_utils_init(envp
);
1813 QLIST_INIT (&vm_change_state_head
);
1814 os_setup_early_signal_handling();
1816 module_call_init(MODULE_INIT_MACHINE
);
1817 machine
= find_default_machine();
1819 initrd_filename
= NULL
;
1822 kernel_filename
= NULL
;
1823 kernel_cmdline
= "";
1824 cyls
= heads
= secs
= 0;
1825 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1827 for (i
= 0; i
< MAX_NODES
; i
++) {
1829 node_cpumask
[i
] = 0;
1838 /* first pass of option parsing */
1840 while (optind
< argc
) {
1841 if (argv
[optind
][0] != '-') {
1846 const QEMUOption
*popt
;
1848 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
1849 switch (popt
->index
) {
1850 case QEMU_OPTION_nodefconfig
:
1860 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
1861 if (ret
< 0 && ret
!= -ENOENT
) {
1865 ret
= qemu_read_config_file(arch_config_name
);
1866 if (ret
< 0 && ret
!= -ENOENT
) {
1872 /* second pass of option parsing */
1877 if (argv
[optind
][0] != '-') {
1878 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
1880 const QEMUOption
*popt
;
1882 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
1883 if (!(popt
->arch_mask
& arch_type
)) {
1884 printf("Option %s not supported for this target\n", popt
->name
);
1887 switch(popt
->index
) {
1889 machine
= find_machine(optarg
);
1892 printf("Supported machines are:\n");
1893 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1895 printf("%-10s %s (alias of %s)\n",
1896 m
->alias
, m
->desc
, m
->name
);
1897 printf("%-10s %s%s\n",
1899 m
->is_default
? " (default)" : "");
1901 exit(*optarg
!= '?');
1904 case QEMU_OPTION_cpu
:
1905 /* hw initialization will check this */
1906 if (*optarg
== '?') {
1907 list_cpus(stdout
, &fprintf
, optarg
);
1913 case QEMU_OPTION_initrd
:
1914 initrd_filename
= optarg
;
1916 case QEMU_OPTION_hda
:
1918 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
1920 hda_opts
= drive_add(optarg
, HD_ALIAS
1921 ",cyls=%d,heads=%d,secs=%d%s",
1922 0, cyls
, heads
, secs
,
1923 translation
== BIOS_ATA_TRANSLATION_LBA
?
1925 translation
== BIOS_ATA_TRANSLATION_NONE
?
1926 ",trans=none" : "");
1928 case QEMU_OPTION_hdb
:
1929 case QEMU_OPTION_hdc
:
1930 case QEMU_OPTION_hdd
:
1931 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
1933 case QEMU_OPTION_drive
:
1934 drive_add(NULL
, "%s", optarg
);
1936 case QEMU_OPTION_set
:
1937 if (qemu_set_option(optarg
) != 0)
1940 case QEMU_OPTION_global
:
1941 if (qemu_global_option(optarg
) != 0)
1944 case QEMU_OPTION_mtdblock
:
1945 drive_add(optarg
, MTD_ALIAS
);
1947 case QEMU_OPTION_sd
:
1948 drive_add(optarg
, SD_ALIAS
);
1950 case QEMU_OPTION_pflash
:
1951 drive_add(optarg
, PFLASH_ALIAS
);
1953 case QEMU_OPTION_snapshot
:
1956 case QEMU_OPTION_hdachs
:
1960 cyls
= strtol(p
, (char **)&p
, 0);
1961 if (cyls
< 1 || cyls
> 16383)
1966 heads
= strtol(p
, (char **)&p
, 0);
1967 if (heads
< 1 || heads
> 16)
1972 secs
= strtol(p
, (char **)&p
, 0);
1973 if (secs
< 1 || secs
> 63)
1977 if (!strcmp(p
, "none"))
1978 translation
= BIOS_ATA_TRANSLATION_NONE
;
1979 else if (!strcmp(p
, "lba"))
1980 translation
= BIOS_ATA_TRANSLATION_LBA
;
1981 else if (!strcmp(p
, "auto"))
1982 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1985 } else if (*p
!= '\0') {
1987 fprintf(stderr
, "qemu: invalid physical CHS format\n");
1990 if (hda_opts
!= NULL
) {
1992 snprintf(num
, sizeof(num
), "%d", cyls
);
1993 qemu_opt_set(hda_opts
, "cyls", num
);
1994 snprintf(num
, sizeof(num
), "%d", heads
);
1995 qemu_opt_set(hda_opts
, "heads", num
);
1996 snprintf(num
, sizeof(num
), "%d", secs
);
1997 qemu_opt_set(hda_opts
, "secs", num
);
1998 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
1999 qemu_opt_set(hda_opts
, "trans", "lba");
2000 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2001 qemu_opt_set(hda_opts
, "trans", "none");
2005 case QEMU_OPTION_numa
:
2006 if (nb_numa_nodes
>= MAX_NODES
) {
2007 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2012 case QEMU_OPTION_nographic
:
2013 display_type
= DT_NOGRAPHIC
;
2015 #ifdef CONFIG_CURSES
2016 case QEMU_OPTION_curses
:
2017 display_type
= DT_CURSES
;
2020 case QEMU_OPTION_portrait
:
2023 case QEMU_OPTION_kernel
:
2024 kernel_filename
= optarg
;
2026 case QEMU_OPTION_append
:
2027 kernel_cmdline
= optarg
;
2029 case QEMU_OPTION_cdrom
:
2030 drive_add(optarg
, CDROM_ALIAS
);
2032 case QEMU_OPTION_boot
:
2034 static const char * const params
[] = {
2035 "order", "once", "menu", NULL
2037 char buf
[sizeof(boot_devices
)];
2038 char *standard_boot_devices
;
2041 if (!strchr(optarg
, '=')) {
2043 pstrcpy(buf
, sizeof(buf
), optarg
);
2044 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2046 "qemu: unknown boot parameter '%s' in '%s'\n",
2052 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2053 validate_bootdevices(buf
);
2054 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2057 if (get_param_value(buf
, sizeof(buf
),
2059 validate_bootdevices(buf
);
2060 standard_boot_devices
= qemu_strdup(boot_devices
);
2061 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2062 qemu_register_reset(restore_boot_devices
,
2063 standard_boot_devices
);
2065 if (get_param_value(buf
, sizeof(buf
),
2067 if (!strcmp(buf
, "on")) {
2069 } else if (!strcmp(buf
, "off")) {
2073 "qemu: invalid option value '%s'\n",
2081 case QEMU_OPTION_fda
:
2082 case QEMU_OPTION_fdb
:
2083 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2085 case QEMU_OPTION_no_fd_bootchk
:
2088 case QEMU_OPTION_netdev
:
2089 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2093 case QEMU_OPTION_net
:
2094 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2099 case QEMU_OPTION_tftp
:
2100 legacy_tftp_prefix
= optarg
;
2102 case QEMU_OPTION_bootp
:
2103 legacy_bootp_filename
= optarg
;
2105 case QEMU_OPTION_redir
:
2106 if (net_slirp_redir(optarg
) < 0)
2110 case QEMU_OPTION_bt
:
2111 add_device_config(DEV_BT
, optarg
);
2113 case QEMU_OPTION_audio_help
:
2114 if (!(audio_available())) {
2115 printf("Option %s not supported for this target\n", popt
->name
);
2121 case QEMU_OPTION_soundhw
:
2122 if (!(audio_available())) {
2123 printf("Option %s not supported for this target\n", popt
->name
);
2126 select_soundhw (optarg
);
2131 case QEMU_OPTION_version
:
2135 case QEMU_OPTION_m
: {
2139 value
= strtoul(optarg
, &ptr
, 10);
2141 case 0: case 'M': case 'm':
2148 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2152 /* On 32-bit hosts, QEMU is limited by virtual address space */
2153 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2154 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2157 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2158 fprintf(stderr
, "qemu: ram size too large\n");
2164 case QEMU_OPTION_mempath
:
2168 case QEMU_OPTION_mem_prealloc
:
2173 set_cpu_log(optarg
);
2176 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2178 case QEMU_OPTION_gdb
:
2179 gdbstub_dev
= optarg
;
2184 case QEMU_OPTION_bios
:
2187 case QEMU_OPTION_singlestep
:
2194 keyboard_layout
= optarg
;
2196 case QEMU_OPTION_localtime
:
2199 case QEMU_OPTION_vga
:
2200 select_vgahw (optarg
);
2207 w
= strtol(p
, (char **)&p
, 10);
2210 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2216 h
= strtol(p
, (char **)&p
, 10);
2221 depth
= strtol(p
, (char **)&p
, 10);
2222 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2223 depth
!= 24 && depth
!= 32)
2225 } else if (*p
== '\0') {
2226 depth
= graphic_depth
;
2233 graphic_depth
= depth
;
2236 case QEMU_OPTION_echr
:
2239 term_escape_char
= strtol(optarg
, &r
, 0);
2241 printf("Bad argument to echr\n");
2244 case QEMU_OPTION_monitor
:
2245 monitor_parse(optarg
, "readline");
2246 default_monitor
= 0;
2248 case QEMU_OPTION_qmp
:
2249 monitor_parse(optarg
, "control");
2250 default_monitor
= 0;
2252 case QEMU_OPTION_mon
:
2253 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
2257 default_monitor
= 0;
2259 case QEMU_OPTION_chardev
:
2260 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
2266 case QEMU_OPTION_fsdev
:
2267 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
2269 fprintf(stderr
, "parse error: %s\n", optarg
);
2273 case QEMU_OPTION_virtfs
: {
2274 char *arg_fsdev
= NULL
;
2275 char *arg_9p
= NULL
;
2278 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
2280 fprintf(stderr
, "parse error: %s\n", optarg
);
2284 len
= strlen(",id=,path=");
2285 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2286 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2287 len
+= strlen(qemu_opt_get(opts
, "path"));
2288 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2291 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
2296 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
2297 qemu_opt_get(opts
, "fstype"),
2298 qemu_opt_get(opts
, "mount_tag"),
2299 qemu_opt_get(opts
, "path"));
2301 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2302 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2303 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2306 fprintf(stderr
, "No memory to parse -device for %s\n",
2311 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2312 qemu_opt_get(opts
, "mount_tag"),
2313 qemu_opt_get(opts
, "mount_tag"));
2315 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
2316 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2320 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
2321 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2325 qemu_free(arg_fsdev
);
2330 case QEMU_OPTION_serial
:
2331 add_device_config(DEV_SERIAL
, optarg
);
2333 if (strncmp(optarg
, "mon:", 4) == 0) {
2334 default_monitor
= 0;
2337 case QEMU_OPTION_watchdog
:
2340 "qemu: only one watchdog option may be given\n");
2345 case QEMU_OPTION_watchdog_action
:
2346 if (select_watchdog_action(optarg
) == -1) {
2347 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2351 case QEMU_OPTION_virtiocon
:
2352 add_device_config(DEV_VIRTCON
, optarg
);
2353 default_virtcon
= 0;
2354 if (strncmp(optarg
, "mon:", 4) == 0) {
2355 default_monitor
= 0;
2358 case QEMU_OPTION_parallel
:
2359 add_device_config(DEV_PARALLEL
, optarg
);
2360 default_parallel
= 0;
2361 if (strncmp(optarg
, "mon:", 4) == 0) {
2362 default_monitor
= 0;
2365 case QEMU_OPTION_debugcon
:
2366 add_device_config(DEV_DEBUGCON
, optarg
);
2368 case QEMU_OPTION_loadvm
:
2371 case QEMU_OPTION_full_screen
:
2375 case QEMU_OPTION_no_frame
:
2378 case QEMU_OPTION_alt_grab
:
2381 case QEMU_OPTION_ctrl_grab
:
2384 case QEMU_OPTION_no_quit
:
2387 case QEMU_OPTION_sdl
:
2388 display_type
= DT_SDL
;
2391 case QEMU_OPTION_pidfile
:
2394 case QEMU_OPTION_win2k_hack
:
2395 win2k_install_hack
= 1;
2397 case QEMU_OPTION_rtc_td_hack
:
2400 case QEMU_OPTION_acpitable
:
2401 do_acpitable_option(optarg
);
2403 case QEMU_OPTION_smbios
:
2404 do_smbios_option(optarg
);
2406 case QEMU_OPTION_enable_kvm
:
2409 case QEMU_OPTION_usb
:
2412 case QEMU_OPTION_usbdevice
:
2414 add_device_config(DEV_USB
, optarg
);
2416 case QEMU_OPTION_device
:
2417 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
2421 case QEMU_OPTION_smp
:
2424 fprintf(stderr
, "Invalid number of CPUs\n");
2427 if (max_cpus
< smp_cpus
) {
2428 fprintf(stderr
, "maxcpus must be equal to or greater than "
2432 if (max_cpus
> 255) {
2433 fprintf(stderr
, "Unsupported number of maxcpus\n");
2437 case QEMU_OPTION_vnc
:
2438 display_type
= DT_VNC
;
2439 vnc_display
= optarg
;
2441 case QEMU_OPTION_no_acpi
:
2444 case QEMU_OPTION_no_hpet
:
2447 case QEMU_OPTION_balloon
:
2448 if (balloon_parse(optarg
) < 0) {
2449 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2453 case QEMU_OPTION_no_reboot
:
2456 case QEMU_OPTION_no_shutdown
:
2459 case QEMU_OPTION_show_cursor
:
2462 case QEMU_OPTION_uuid
:
2463 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2464 fprintf(stderr
, "Fail to parse UUID string."
2465 " Wrong format.\n");
2469 case QEMU_OPTION_option_rom
:
2470 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2471 fprintf(stderr
, "Too many option ROMs\n");
2474 option_rom
[nb_option_roms
] = optarg
;
2477 case QEMU_OPTION_semihosting
:
2478 semihosting_enabled
= 1;
2480 case QEMU_OPTION_name
:
2481 qemu_name
= qemu_strdup(optarg
);
2483 char *p
= strchr(qemu_name
, ',');
2486 if (strncmp(p
, "process=", 8)) {
2487 fprintf(stderr
, "Unknown subargument %s to -name", p
);
2491 os_set_proc_name(p
);
2495 case QEMU_OPTION_prom_env
:
2496 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2497 fprintf(stderr
, "Too many prom variables\n");
2500 prom_envs
[nb_prom_envs
] = optarg
;
2503 case QEMU_OPTION_old_param
:
2506 case QEMU_OPTION_clock
:
2507 configure_alarms(optarg
);
2509 case QEMU_OPTION_startdate
:
2510 configure_rtc_date_offset(optarg
, 1);
2512 case QEMU_OPTION_rtc
:
2513 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
2517 configure_rtc(opts
);
2519 case QEMU_OPTION_tb_size
:
2520 tb_size
= strtol(optarg
, NULL
, 0);
2524 case QEMU_OPTION_icount
:
2525 icount_option
= optarg
;
2527 case QEMU_OPTION_incoming
:
2530 case QEMU_OPTION_nodefaults
:
2532 default_parallel
= 0;
2533 default_virtcon
= 0;
2534 default_monitor
= 0;
2541 case QEMU_OPTION_xen_domid
:
2542 if (!(xen_available())) {
2543 printf("Option %s not supported for this target\n", popt
->name
);
2546 xen_domid
= atoi(optarg
);
2548 case QEMU_OPTION_xen_create
:
2549 if (!(xen_available())) {
2550 printf("Option %s not supported for this target\n", popt
->name
);
2553 xen_mode
= XEN_CREATE
;
2555 case QEMU_OPTION_xen_attach
:
2556 if (!(xen_available())) {
2557 printf("Option %s not supported for this target\n", popt
->name
);
2560 xen_mode
= XEN_ATTACH
;
2562 case QEMU_OPTION_readconfig
:
2564 int ret
= qemu_read_config_file(optarg
);
2566 fprintf(stderr
, "read config %s: %s\n", optarg
,
2572 case QEMU_OPTION_writeconfig
:
2575 if (strcmp(optarg
, "-") == 0) {
2578 fp
= fopen(optarg
, "w");
2580 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2584 qemu_config_write(fp
);
2589 os_parse_cmd_args(popt
->index
, optarg
);
2595 /* If no data_dir is specified then try to find it relative to the
2598 data_dir
= os_find_datadir(argv
[0]);
2600 /* If all else fails use the install patch specified when building. */
2602 data_dir
= CONFIG_QEMU_DATADIR
;
2606 * Default to max_cpus = smp_cpus, in case the user doesn't
2607 * specify a max_cpus value.
2610 max_cpus
= smp_cpus
;
2612 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2613 if (smp_cpus
> machine
->max_cpus
) {
2614 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2615 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2620 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
2621 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
2623 if (machine
->no_serial
) {
2626 if (machine
->no_parallel
) {
2627 default_parallel
= 0;
2629 if (!machine
->use_virtcon
) {
2630 default_virtcon
= 0;
2632 if (machine
->no_vga
) {
2635 if (machine
->no_floppy
) {
2638 if (machine
->no_cdrom
) {
2641 if (machine
->no_sdcard
) {
2645 if (display_type
== DT_NOGRAPHIC
) {
2646 if (default_parallel
)
2647 add_device_config(DEV_PARALLEL
, "null");
2648 if (default_serial
&& default_monitor
) {
2649 add_device_config(DEV_SERIAL
, "mon:stdio");
2650 } else if (default_virtcon
&& default_monitor
) {
2651 add_device_config(DEV_VIRTCON
, "mon:stdio");
2654 add_device_config(DEV_SERIAL
, "stdio");
2655 if (default_virtcon
)
2656 add_device_config(DEV_VIRTCON
, "stdio");
2657 if (default_monitor
)
2658 monitor_parse("stdio", "readline");
2662 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
2663 if (default_parallel
)
2664 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
2665 if (default_monitor
)
2666 monitor_parse("vc:80Cx24C", "readline");
2667 if (default_virtcon
)
2668 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
2671 vga_interface_type
= VGA_CIRRUS
;
2675 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
2678 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
2685 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
2691 int ret
= kvm_init(smp_cpus
);
2693 if (!kvm_available()) {
2694 printf("KVM not supported for this target\n");
2696 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
2702 if (qemu_init_main_loop()) {
2703 fprintf(stderr
, "qemu_init_main_loop failed\n");
2706 linux_boot
= (kernel_filename
!= NULL
);
2708 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
2709 fprintf(stderr
, "-append only allowed with -kernel option\n");
2713 if (!linux_boot
&& initrd_filename
!= NULL
) {
2714 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
2718 os_set_line_buffering();
2720 if (init_timer_alarm() < 0) {
2721 fprintf(stderr
, "could not initialize alarm timer\n");
2724 configure_icount(icount_option
);
2726 if (net_init_clients() < 0) {
2730 /* init the bluetooth world */
2731 if (foreach_device_config(DEV_BT
, bt_parse
))
2734 /* init the memory */
2736 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2738 /* init the dynamic translator */
2739 cpu_exec_init_all(tb_size
* 1024 * 1024);
2741 bdrv_init_with_whitelist();
2745 if (default_cdrom
) {
2746 /* we always create the cdrom drive, even if no disk is there */
2747 drive_add(NULL
, CDROM_ALIAS
);
2750 if (default_floppy
) {
2751 /* we always create at least one floppy */
2752 drive_add(NULL
, FD_ALIAS
, 0);
2755 if (default_sdcard
) {
2756 /* we always create one sd slot, even if no card is in it */
2757 drive_add(NULL
, SD_ALIAS
);
2760 /* open the virtual block devices */
2762 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
2763 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, &machine
->use_scsi
, 1) != 0)
2766 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
2769 if (nb_numa_nodes
> 0) {
2772 if (nb_numa_nodes
> smp_cpus
) {
2773 nb_numa_nodes
= smp_cpus
;
2776 /* If no memory size if given for any node, assume the default case
2777 * and distribute the available memory equally across all nodes
2779 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2780 if (node_mem
[i
] != 0)
2783 if (i
== nb_numa_nodes
) {
2784 uint64_t usedmem
= 0;
2786 /* On Linux, the each node's border has to be 8MB aligned,
2787 * the final node gets the rest.
2789 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
2790 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
2791 usedmem
+= node_mem
[i
];
2793 node_mem
[i
] = ram_size
- usedmem
;
2796 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2797 if (node_cpumask
[i
] != 0)
2800 /* assigning the VCPUs round-robin is easier to implement, guest OSes
2801 * must cope with this anyway, because there are BIOSes out there in
2802 * real machines which also use this scheme.
2804 if (i
== nb_numa_nodes
) {
2805 for (i
= 0; i
< smp_cpus
; i
++) {
2806 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
2811 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
2815 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
2817 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
2819 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
2821 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
2824 module_call_init(MODULE_INIT_DEVICE
);
2826 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
2830 i
= select_watchdog(watchdog
);
2832 exit (i
== 1 ? 1 : 0);
2835 if (machine
->compat_props
) {
2836 qdev_prop_register_global_list(machine
->compat_props
);
2840 machine
->init(ram_size
, boot_devices
,
2841 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
2843 cpu_synchronize_all_post_init();
2845 /* must be after terminal init, SDL library changes signal handlers */
2846 os_setup_signal_handling();
2850 current_machine
= machine
;
2852 /* init USB devices */
2854 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
2858 /* init generic devices */
2859 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
2862 net_check_clients();
2864 /* just use the first displaystate for the moment */
2865 ds
= get_displaystate();
2867 if (display_type
== DT_DEFAULT
) {
2868 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
2869 display_type
= DT_SDL
;
2871 display_type
= DT_VNC
;
2872 vnc_display
= "localhost:0,to=99";
2878 switch (display_type
) {
2881 #if defined(CONFIG_CURSES)
2883 curses_display_init(ds
, full_screen
);
2886 #if defined(CONFIG_SDL)
2888 sdl_display_init(ds
, full_screen
, no_frame
);
2890 #elif defined(CONFIG_COCOA)
2892 cocoa_display_init(ds
, full_screen
);
2896 vnc_display_init(ds
);
2897 if (vnc_display_open(ds
, vnc_display
) < 0)
2900 if (show_vnc_port
) {
2901 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
2909 dcl
= ds
->listeners
;
2910 while (dcl
!= NULL
) {
2911 if (dcl
->dpy_refresh
!= NULL
) {
2912 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
2913 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
2919 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
2920 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
2921 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
2924 text_consoles_set_display(ds
);
2926 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
2927 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
2932 qdev_machine_creation_done();
2934 if (rom_load_all() != 0) {
2935 fprintf(stderr
, "rom loading failed\n");
2939 qemu_system_reset();
2941 if (load_vmstate(loadvm
) < 0) {
2947 qemu_start_incoming_migration(incoming
);
2948 } else if (autostart
) {