4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <sys/prctl.h>
65 #include <linux/ppdev.h>
66 #include <linux/parport.h>
70 #include <sys/ethernet.h>
71 #include <sys/sockio.h>
72 #include <netinet/arp.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h> // must come after ip.h
77 #include <netinet/udp.h>
78 #include <netinet/tcp.h>
82 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84 extern int madvise(caddr_t
, size_t, int);
89 #if defined(__OpenBSD__)
93 #if defined(CONFIG_VDE)
94 #include <libvdeplug.h>
102 #if defined(__APPLE__) || defined(main)
104 int qemu_main(int argc
, char **argv
, char **envp
);
105 int main(int argc
, char **argv
)
107 return qemu_main(argc
, argv
, NULL
);
110 #define main qemu_main
112 #endif /* CONFIG_SDL */
116 #define main qemu_main
117 #endif /* CONFIG_COCOA */
120 #include "hw/boards.h"
122 #include "hw/pcmcia.h"
127 #include "hw/watchdog.h"
128 #include "hw/smbios.h"
131 #include "hw/loader.h"
134 #include "net/slirp.h"
139 #include "qemu-timer.h"
140 #include "qemu-char.h"
141 #include "cache-utils.h"
143 #include "blockdev.h"
144 #include "block-migration.h"
146 #include "audio/audio.h"
147 #include "migration.h"
149 #include "qemu-option.h"
150 #include "qemu-config.h"
151 #include "qemu-objects.h"
153 #include "fsdev/qemu-fsdev.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
162 #include "qemu-queue.h"
164 #include "arch_init.h"
167 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 128
171 #define MAX_VIRTIO_CONSOLES 1
173 static const char *data_dir
;
174 const char *bios_name
= NULL
;
175 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
176 DisplayType display_type
= DT_DEFAULT
;
177 const char* keyboard_layout
= NULL
;
179 const char *mem_path
= NULL
;
181 int mem_prealloc
= 0; /* force preallocation of physical target memory */
184 NICInfo nd_table
[MAX_NICS
];
187 static int rtc_utc
= 1;
188 static int rtc_date_offset
= -1; /* -1 means no change */
189 QEMUClock
*rtc_clock
;
190 int vga_interface_type
= VGA_NONE
;
191 static int full_screen
= 0;
193 static int no_frame
= 0;
196 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
197 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
198 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
199 int win2k_install_hack
= 0;
207 const char *vnc_display
;
208 int acpi_enabled
= 1;
214 int graphic_rotate
= 0;
215 uint8_t irq0override
= 1;
219 const char *watchdog
;
220 const char *option_rom
[MAX_OPTION_ROMS
];
222 int semihosting_enabled
= 0;
224 const char *qemu_name
;
227 unsigned int nb_prom_envs
= 0;
228 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint64_t node_mem
[MAX_NODES
];
233 uint64_t node_cpumask
[MAX_NODES
];
235 static QEMUTimer
*nographic_timer
;
237 uint8_t qemu_uuid
[16];
239 static QEMUBootSetHandler
*boot_set_handler
;
240 static void *boot_set_opaque
;
244 enum xen_mode xen_mode
= XEN_EMULATE
;
246 static int default_serial
= 1;
247 static int default_parallel
= 1;
248 static int default_virtcon
= 1;
249 static int default_monitor
= 1;
250 static int default_vga
= 1;
251 static int default_floppy
= 1;
252 static int default_cdrom
= 1;
253 static int default_sdcard
= 1;
259 { .driver
= "isa-serial", .flag
= &default_serial
},
260 { .driver
= "isa-parallel", .flag
= &default_parallel
},
261 { .driver
= "isa-fdc", .flag
= &default_floppy
},
262 { .driver
= "ide-drive", .flag
= &default_cdrom
},
263 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
264 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
265 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
266 { .driver
= "VGA", .flag
= &default_vga
},
267 { .driver
= "cirrus-vga", .flag
= &default_vga
},
268 { .driver
= "vmware-svga", .flag
= &default_vga
},
271 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
273 const char *driver
= qemu_opt_get(opts
, "driver");
278 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
279 if (strcmp(default_list
[i
].driver
, driver
) != 0)
281 *(default_list
[i
].flag
) = 0;
286 /***********************************************************/
288 static void set_proc_name(const char *s
)
290 #if defined(__linux__) && defined(PR_SET_NAME)
294 name
[sizeof(name
) - 1] = 0;
295 strncpy(name
, s
, sizeof(name
));
296 /* Could rewrite argv[0] too, but that's a bit more complicated.
297 This simple way is enough for `top'. */
298 prctl(PR_SET_NAME
, name
);
302 /***********************************************************/
303 /* real time host monotonic timer */
305 /* compute with 96 bit intermediate result: (a*b)/c */
306 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
311 #ifdef HOST_WORDS_BIGENDIAN
321 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
322 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
325 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
329 /***********************************************************/
330 /* host time/date access */
331 void qemu_get_timedate(struct tm
*tm
, int offset
)
338 if (rtc_date_offset
== -1) {
342 ret
= localtime(&ti
);
344 ti
-= rtc_date_offset
;
348 memcpy(tm
, ret
, sizeof(struct tm
));
351 int qemu_timedate_diff(struct tm
*tm
)
355 if (rtc_date_offset
== -1)
357 seconds
= mktimegm(tm
);
359 seconds
= mktime(tm
);
361 seconds
= mktimegm(tm
) + rtc_date_offset
;
363 return seconds
- time(NULL
);
366 void rtc_change_mon_event(struct tm
*tm
)
370 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
371 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
372 qobject_decref(data
);
375 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
377 time_t rtc_start_date
;
380 if (!strcmp(startdate
, "now") && legacy
) {
381 rtc_date_offset
= -1;
383 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
391 } else if (sscanf(startdate
, "%d-%d-%d",
403 rtc_start_date
= mktimegm(&tm
);
404 if (rtc_start_date
== -1) {
406 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
407 "'2006-06-17T16:01:21' or '2006-06-17'\n");
410 rtc_date_offset
= time(NULL
) - rtc_start_date
;
414 static void configure_rtc(QemuOpts
*opts
)
418 value
= qemu_opt_get(opts
, "base");
420 if (!strcmp(value
, "utc")) {
422 } else if (!strcmp(value
, "localtime")) {
425 configure_rtc_date_offset(value
, 0);
428 value
= qemu_opt_get(opts
, "clock");
430 if (!strcmp(value
, "host")) {
431 rtc_clock
= host_clock
;
432 } else if (!strcmp(value
, "vm")) {
433 rtc_clock
= vm_clock
;
435 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
439 value
= qemu_opt_get(opts
, "driftfix");
441 if (!strcmp(value
, "slew")) {
443 } else if (!strcmp(value
, "none")) {
446 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
452 /***********************************************************/
453 /* Bluetooth support */
456 static struct HCIInfo
*hci_table
[MAX_NICS
];
458 static struct bt_vlan_s
{
459 struct bt_scatternet_s net
;
461 struct bt_vlan_s
*next
;
464 /* find or alloc a new bluetooth "VLAN" */
465 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
467 struct bt_vlan_s
**pvlan
, *vlan
;
468 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
472 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
474 pvlan
= &first_bt_vlan
;
475 while (*pvlan
!= NULL
)
476 pvlan
= &(*pvlan
)->next
;
481 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
485 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
490 static struct HCIInfo null_hci
= {
491 .cmd_send
= null_hci_send
,
492 .sco_send
= null_hci_send
,
493 .acl_send
= null_hci_send
,
494 .bdaddr_set
= null_hci_addr_set
,
497 struct HCIInfo
*qemu_next_hci(void)
499 if (cur_hci
== nb_hcis
)
502 return hci_table
[cur_hci
++];
505 static struct HCIInfo
*hci_init(const char *str
)
508 struct bt_scatternet_s
*vlan
= 0;
510 if (!strcmp(str
, "null"))
513 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
515 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
516 else if (!strncmp(str
, "hci", 3)) {
519 if (!strncmp(str
+ 3, ",vlan=", 6)) {
520 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
525 vlan
= qemu_find_bt_vlan(0);
527 return bt_new_hci(vlan
);
530 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
535 static int bt_hci_parse(const char *str
)
540 if (nb_hcis
>= MAX_NICS
) {
541 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
554 bdaddr
.b
[5] = 0x56 + nb_hcis
;
555 hci
->bdaddr_set(hci
, bdaddr
.b
);
557 hci_table
[nb_hcis
++] = hci
;
562 static void bt_vhci_add(int vlan_id
)
564 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
567 fprintf(stderr
, "qemu: warning: adding a VHCI to "
568 "an empty scatternet %i\n", vlan_id
);
570 bt_vhci_init(bt_new_hci(vlan
));
573 static struct bt_device_s
*bt_device_add(const char *opt
)
575 struct bt_scatternet_s
*vlan
;
577 char *endp
= strstr(opt
, ",vlan=");
578 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
581 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
584 vlan_id
= strtol(endp
+ 6, &endp
, 0);
586 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
591 vlan
= qemu_find_bt_vlan(vlan_id
);
594 fprintf(stderr
, "qemu: warning: adding a slave device to "
595 "an empty scatternet %i\n", vlan_id
);
597 if (!strcmp(devname
, "keyboard"))
598 return bt_keyboard_init(vlan
);
600 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
604 static int bt_parse(const char *opt
)
606 const char *endp
, *p
;
609 if (strstart(opt
, "hci", &endp
)) {
610 if (!*endp
|| *endp
== ',') {
612 if (!strstart(endp
, ",vlan=", 0))
615 return bt_hci_parse(opt
);
617 } else if (strstart(opt
, "vhci", &endp
)) {
618 if (!*endp
|| *endp
== ',') {
620 if (strstart(endp
, ",vlan=", &p
)) {
621 vlan
= strtol(p
, (char **) &endp
, 0);
623 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
627 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
636 } else if (strstart(opt
, "device:", &endp
))
637 return !bt_device_add(endp
);
639 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
643 /***********************************************************/
644 /* QEMU Block devices */
646 #define HD_ALIAS "index=%d,media=disk"
647 #define CDROM_ALIAS "index=2,media=cdrom"
648 #define FD_ALIAS "index=%d,if=floppy"
649 #define PFLASH_ALIAS "if=pflash"
650 #define MTD_ALIAS "if=mtd"
651 #define SD_ALIAS "index=0,if=sd"
653 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
655 int *use_scsi
= opaque
;
658 if (drive_init(opts
, *use_scsi
, &fatal_error
) == NULL
) {
665 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
667 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
668 qemu_opt_set(opts
, "snapshot", "on");
673 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
675 boot_set_handler
= func
;
676 boot_set_opaque
= opaque
;
679 int qemu_boot_set(const char *boot_devices
)
681 if (!boot_set_handler
) {
684 return boot_set_handler(boot_set_opaque
, boot_devices
);
687 static void validate_bootdevices(char *devices
)
689 /* We just do some generic consistency checks */
693 for (p
= devices
; *p
!= '\0'; p
++) {
694 /* Allowed boot devices are:
695 * a-b: floppy disk drives
696 * c-f: IDE disk drives
697 * g-m: machine implementation dependant drives
698 * n-p: network devices
699 * It's up to each machine implementation to check if the given boot
700 * devices match the actual hardware implementation and firmware
703 if (*p
< 'a' || *p
> 'p') {
704 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
707 if (bitmap
& (1 << (*p
- 'a'))) {
708 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
711 bitmap
|= 1 << (*p
- 'a');
715 static void restore_boot_devices(void *opaque
)
717 char *standard_boot_devices
= opaque
;
718 static int first
= 1;
720 /* Restore boot order and remove ourselves after the first boot */
726 qemu_boot_set(standard_boot_devices
);
728 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
729 qemu_free(standard_boot_devices
);
732 static void numa_add(const char *optarg
)
736 unsigned long long value
, endvalue
;
739 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
740 if (!strcmp(option
, "node")) {
741 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
742 nodenr
= nb_numa_nodes
;
744 nodenr
= strtoull(option
, NULL
, 10);
747 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
748 node_mem
[nodenr
] = 0;
750 value
= strtoull(option
, &endptr
, 0);
752 case 0: case 'M': case 'm':
759 node_mem
[nodenr
] = value
;
761 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
762 node_cpumask
[nodenr
] = 0;
764 value
= strtoull(option
, &endptr
, 10);
767 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
769 if (*endptr
== '-') {
770 endvalue
= strtoull(endptr
+1, &endptr
, 10);
771 if (endvalue
>= 63) {
774 "only 63 CPUs in NUMA mode supported.\n");
776 value
= (2ULL << endvalue
) - (1ULL << value
);
778 value
= 1ULL << value
;
781 node_cpumask
[nodenr
] = value
;
788 static void smp_parse(const char *optarg
)
790 int smp
, sockets
= 0, threads
= 0, cores
= 0;
794 smp
= strtoul(optarg
, &endptr
, 10);
795 if (endptr
!= optarg
) {
796 if (*endptr
== ',') {
800 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
801 sockets
= strtoull(option
, NULL
, 10);
802 if (get_param_value(option
, 128, "cores", endptr
) != 0)
803 cores
= strtoull(option
, NULL
, 10);
804 if (get_param_value(option
, 128, "threads", endptr
) != 0)
805 threads
= strtoull(option
, NULL
, 10);
806 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
807 max_cpus
= strtoull(option
, NULL
, 10);
809 /* compute missing values, prefer sockets over cores over threads */
810 if (smp
== 0 || sockets
== 0) {
811 sockets
= sockets
> 0 ? sockets
: 1;
812 cores
= cores
> 0 ? cores
: 1;
813 threads
= threads
> 0 ? threads
: 1;
815 smp
= cores
* threads
* sockets
;
819 threads
= threads
> 0 ? threads
: 1;
820 cores
= smp
/ (sockets
* threads
);
823 threads
= smp
/ (cores
* sockets
);
828 smp_cores
= cores
> 0 ? cores
: 1;
829 smp_threads
= threads
> 0 ? threads
: 1;
834 /***********************************************************/
837 static int usb_device_add(const char *devname
)
840 USBDevice
*dev
= NULL
;
845 /* drivers with .usbdevice_name entry in USBDeviceInfo */
846 dev
= usbdevice_create(devname
);
851 if (strstart(devname
, "host:", &p
)) {
852 dev
= usb_host_device_open(p
);
853 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
854 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
855 bt_new_hci(qemu_find_bt_vlan(0)));
866 static int usb_device_del(const char *devname
)
871 if (strstart(devname
, "host:", &p
))
872 return usb_host_device_close(p
);
877 p
= strchr(devname
, '.');
880 bus_num
= strtoul(devname
, NULL
, 0);
881 addr
= strtoul(p
+ 1, NULL
, 0);
883 return usb_device_delete_addr(bus_num
, addr
);
886 static int usb_parse(const char *cmdline
)
889 r
= usb_device_add(cmdline
);
891 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
896 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
898 const char *devname
= qdict_get_str(qdict
, "devname");
899 if (usb_device_add(devname
) < 0) {
900 error_report("could not add USB device '%s'", devname
);
904 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
906 const char *devname
= qdict_get_str(qdict
, "devname");
907 if (usb_device_del(devname
) < 0) {
908 error_report("could not delete USB device '%s'", devname
);
912 /***********************************************************/
915 static struct pcmcia_socket_entry_s
{
916 PCMCIASocket
*socket
;
917 struct pcmcia_socket_entry_s
*next
;
918 } *pcmcia_sockets
= 0;
920 void pcmcia_socket_register(PCMCIASocket
*socket
)
922 struct pcmcia_socket_entry_s
*entry
;
924 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
925 entry
->socket
= socket
;
926 entry
->next
= pcmcia_sockets
;
927 pcmcia_sockets
= entry
;
930 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
932 struct pcmcia_socket_entry_s
*entry
, **ptr
;
934 ptr
= &pcmcia_sockets
;
935 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
936 if (entry
->socket
== socket
) {
942 void pcmcia_info(Monitor
*mon
)
944 struct pcmcia_socket_entry_s
*iter
;
947 monitor_printf(mon
, "No PCMCIA sockets\n");
949 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
950 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
951 iter
->socket
->attached
? iter
->socket
->card_string
:
955 /***********************************************************/
958 typedef struct IOHandlerRecord
{
960 IOCanReadHandler
*fd_read_poll
;
967 QLIST_ENTRY(IOHandlerRecord
) next
;
970 static QLIST_HEAD(, IOHandlerRecord
) io_handlers
=
971 QLIST_HEAD_INITIALIZER(io_handlers
);
974 /* XXX: fd_read_poll should be suppressed, but an API change is
975 necessary in the character devices to suppress fd_can_read(). */
976 int qemu_set_fd_handler2(int fd
,
977 IOCanReadHandler
*fd_read_poll
,
982 IOHandlerRecord
*ioh
;
984 if (!fd_read
&& !fd_write
) {
985 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
992 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
996 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
997 QLIST_INSERT_HEAD(&io_handlers
, ioh
, next
);
1000 ioh
->fd_read_poll
= fd_read_poll
;
1001 ioh
->fd_read
= fd_read
;
1002 ioh
->fd_write
= fd_write
;
1003 ioh
->opaque
= opaque
;
1009 int qemu_set_fd_handler(int fd
,
1011 IOHandler
*fd_write
,
1014 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
1018 /***********************************************************/
1019 /* Polling handling */
1021 typedef struct PollingEntry
{
1024 struct PollingEntry
*next
;
1027 static PollingEntry
*first_polling_entry
;
1029 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
1031 PollingEntry
**ppe
, *pe
;
1032 pe
= qemu_mallocz(sizeof(PollingEntry
));
1034 pe
->opaque
= opaque
;
1035 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
1040 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
1042 PollingEntry
**ppe
, *pe
;
1043 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
1045 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
1053 /***********************************************************/
1054 /* Wait objects support */
1055 typedef struct WaitObjects
{
1057 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
1058 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
1059 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
1062 static WaitObjects wait_objects
= {0};
1064 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1066 WaitObjects
*w
= &wait_objects
;
1068 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
1070 w
->events
[w
->num
] = handle
;
1071 w
->func
[w
->num
] = func
;
1072 w
->opaque
[w
->num
] = opaque
;
1077 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
1080 WaitObjects
*w
= &wait_objects
;
1083 for (i
= 0; i
< w
->num
; i
++) {
1084 if (w
->events
[i
] == handle
)
1087 w
->events
[i
] = w
->events
[i
+ 1];
1088 w
->func
[i
] = w
->func
[i
+ 1];
1089 w
->opaque
[i
] = w
->opaque
[i
+ 1];
1097 /***********************************************************/
1098 /* machine registration */
1100 static QEMUMachine
*first_machine
= NULL
;
1101 QEMUMachine
*current_machine
= NULL
;
1103 int qemu_register_machine(QEMUMachine
*m
)
1106 pm
= &first_machine
;
1114 static QEMUMachine
*find_machine(const char *name
)
1118 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1119 if (!strcmp(m
->name
, name
))
1121 if (m
->alias
&& !strcmp(m
->alias
, name
))
1127 static QEMUMachine
*find_default_machine(void)
1131 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1132 if (m
->is_default
) {
1139 /***********************************************************/
1140 /* main execution loop */
1142 static void gui_update(void *opaque
)
1144 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1145 DisplayState
*ds
= opaque
;
1146 DisplayChangeListener
*dcl
= ds
->listeners
;
1148 qemu_flush_coalesced_mmio_buffer();
1151 while (dcl
!= NULL
) {
1152 if (dcl
->gui_timer_interval
&&
1153 dcl
->gui_timer_interval
< interval
)
1154 interval
= dcl
->gui_timer_interval
;
1157 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
1160 static void nographic_update(void *opaque
)
1162 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1164 qemu_flush_coalesced_mmio_buffer();
1165 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
1168 struct vm_change_state_entry
{
1169 VMChangeStateHandler
*cb
;
1171 QLIST_ENTRY (vm_change_state_entry
) entries
;
1174 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1176 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1179 VMChangeStateEntry
*e
;
1181 e
= qemu_mallocz(sizeof (*e
));
1185 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1189 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1191 QLIST_REMOVE (e
, entries
);
1195 void vm_state_notify(int running
, int reason
)
1197 VMChangeStateEntry
*e
;
1199 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1200 e
->cb(e
->opaque
, running
, reason
);
1209 vm_state_notify(1, 0);
1211 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1215 /* reset/shutdown handler */
1217 typedef struct QEMUResetEntry
{
1218 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1219 QEMUResetHandler
*func
;
1223 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1224 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1225 static int reset_requested
;
1226 static int shutdown_requested
;
1227 static int powerdown_requested
;
1228 int debug_requested
;
1229 int vmstop_requested
;
1231 int qemu_shutdown_requested(void)
1233 int r
= shutdown_requested
;
1234 shutdown_requested
= 0;
1238 int qemu_reset_requested(void)
1240 int r
= reset_requested
;
1241 reset_requested
= 0;
1245 int qemu_powerdown_requested(void)
1247 int r
= powerdown_requested
;
1248 powerdown_requested
= 0;
1252 static int qemu_debug_requested(void)
1254 int r
= debug_requested
;
1255 debug_requested
= 0;
1259 static int qemu_vmstop_requested(void)
1261 int r
= vmstop_requested
;
1262 vmstop_requested
= 0;
1266 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1268 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1271 re
->opaque
= opaque
;
1272 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1275 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1279 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1280 if (re
->func
== func
&& re
->opaque
== opaque
) {
1281 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1288 void qemu_system_reset(void)
1290 QEMUResetEntry
*re
, *nre
;
1292 /* reset all devices */
1293 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1294 re
->func(re
->opaque
);
1296 monitor_protocol_event(QEVENT_RESET
, NULL
);
1297 cpu_synchronize_all_post_reset();
1300 void qemu_system_reset_request(void)
1303 shutdown_requested
= 1;
1305 reset_requested
= 1;
1307 qemu_notify_event();
1310 void qemu_system_shutdown_request(void)
1312 shutdown_requested
= 1;
1313 qemu_notify_event();
1316 void qemu_system_powerdown_request(void)
1318 powerdown_requested
= 1;
1319 qemu_notify_event();
1323 static void host_main_loop_wait(int *timeout
)
1329 /* XXX: need to suppress polling by better using win32 events */
1331 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
1332 ret
|= pe
->func(pe
->opaque
);
1336 WaitObjects
*w
= &wait_objects
;
1338 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
1339 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
1340 if (w
->func
[ret
- WAIT_OBJECT_0
])
1341 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
1343 /* Check for additional signaled events */
1344 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
1346 /* Check if event is signaled */
1347 ret2
= WaitForSingleObject(w
->events
[i
], 0);
1348 if(ret2
== WAIT_OBJECT_0
) {
1350 w
->func
[i
](w
->opaque
[i
]);
1351 } else if (ret2
== WAIT_TIMEOUT
) {
1353 err
= GetLastError();
1354 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
1357 } else if (ret
== WAIT_TIMEOUT
) {
1359 err
= GetLastError();
1360 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
1367 static void host_main_loop_wait(int *timeout
)
1372 void main_loop_wait(int nonblocking
)
1374 IOHandlerRecord
*ioh
;
1375 fd_set rfds
, wfds
, xfds
;
1383 timeout
= qemu_calculate_timeout();
1384 qemu_bh_update_timeout(&timeout
);
1387 host_main_loop_wait(&timeout
);
1389 /* poll any events */
1390 /* XXX: separate device handlers from system ones */
1395 QLIST_FOREACH(ioh
, &io_handlers
, next
) {
1399 (!ioh
->fd_read_poll
||
1400 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
1401 FD_SET(ioh
->fd
, &rfds
);
1405 if (ioh
->fd_write
) {
1406 FD_SET(ioh
->fd
, &wfds
);
1412 tv
.tv_sec
= timeout
/ 1000;
1413 tv
.tv_usec
= (timeout
% 1000) * 1000;
1415 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1417 qemu_mutex_unlock_iothread();
1418 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1419 qemu_mutex_lock_iothread();
1421 IOHandlerRecord
*pioh
;
1423 QLIST_FOREACH_SAFE(ioh
, &io_handlers
, next
, pioh
) {
1425 QLIST_REMOVE(ioh
, next
);
1429 if (ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
1430 ioh
->fd_read(ioh
->opaque
);
1432 if (ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
1433 ioh
->fd_write(ioh
->opaque
);
1438 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1440 qemu_run_all_timers();
1442 /* Check bottom-halves last in case any of the earlier events triggered
1448 static int vm_can_run(void)
1450 if (powerdown_requested
)
1452 if (reset_requested
)
1454 if (shutdown_requested
)
1456 if (debug_requested
)
1461 qemu_irq qemu_system_powerdown
;
1463 static void main_loop(void)
1467 qemu_main_loop_start();
1471 bool nonblocking
= false;
1472 #ifdef CONFIG_PROFILER
1475 #ifndef CONFIG_IOTHREAD
1476 nonblocking
= tcg_cpu_exec();
1478 #ifdef CONFIG_PROFILER
1479 ti
= profile_getclock();
1481 main_loop_wait(nonblocking
);
1482 #ifdef CONFIG_PROFILER
1483 dev_time
+= profile_getclock() - ti
;
1485 } while (vm_can_run());
1487 if ((r
= qemu_debug_requested())) {
1490 if (qemu_shutdown_requested()) {
1491 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1498 if (qemu_reset_requested()) {
1500 qemu_system_reset();
1503 if (qemu_powerdown_requested()) {
1504 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1505 qemu_irq_raise(qemu_system_powerdown
);
1507 if ((r
= qemu_vmstop_requested())) {
1515 static void version(void)
1517 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1520 static void help(int exitcode
)
1522 const char *options_help
=
1523 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1525 #define DEFHEADING(text) stringify(text) "\n"
1526 #include "qemu-options.h"
1532 printf("usage: %s [options] [disk_image]\n"
1534 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1537 "During emulation, the following keys are useful:\n"
1538 "ctrl-alt-f toggle full screen\n"
1539 "ctrl-alt-n switch to virtual console 'n'\n"
1540 "ctrl-alt toggle mouse and keyboard grab\n"
1542 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1548 #define HAS_ARG 0x0001
1551 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1553 #define DEFHEADING(text)
1554 #include "qemu-options.h"
1560 typedef struct QEMUOption
{
1567 static const QEMUOption qemu_options
[] = {
1568 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1569 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1570 { option, opt_arg, opt_enum, arch_mask },
1571 #define DEFHEADING(text)
1572 #include "qemu-options.h"
1578 static void select_vgahw (const char *p
)
1583 vga_interface_type
= VGA_NONE
;
1584 if (strstart(p
, "std", &opts
)) {
1585 vga_interface_type
= VGA_STD
;
1586 } else if (strstart(p
, "cirrus", &opts
)) {
1587 vga_interface_type
= VGA_CIRRUS
;
1588 } else if (strstart(p
, "vmware", &opts
)) {
1589 vga_interface_type
= VGA_VMWARE
;
1590 } else if (strstart(p
, "xenfb", &opts
)) {
1591 vga_interface_type
= VGA_XENFB
;
1592 } else if (!strstart(p
, "none", &opts
)) {
1594 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1598 const char *nextopt
;
1600 if (strstart(opts
, ",retrace=", &nextopt
)) {
1602 if (strstart(opts
, "dumb", &nextopt
))
1603 vga_retrace_method
= VGA_RETRACE_DUMB
;
1604 else if (strstart(opts
, "precise", &nextopt
))
1605 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1606 else goto invalid_vga
;
1607 } else goto invalid_vga
;
1612 static int balloon_parse(const char *arg
)
1616 if (strcmp(arg
, "none") == 0) {
1620 if (!strncmp(arg
, "virtio", 6)) {
1621 if (arg
[6] == ',') {
1622 /* have params -> parse them */
1623 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, 0);
1627 /* create empty opts */
1628 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1630 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
1638 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
1640 exit(STATUS_CONTROL_C_EXIT
);
1647 static void termsig_handler(int signal
)
1649 qemu_system_shutdown_request();
1652 static void sigchld_handler(int signal
)
1654 waitpid(-1, NULL
, WNOHANG
);
1657 static void sighandler_setup(void)
1659 struct sigaction act
;
1661 memset(&act
, 0, sizeof(act
));
1662 act
.sa_handler
= termsig_handler
;
1663 sigaction(SIGINT
, &act
, NULL
);
1664 sigaction(SIGHUP
, &act
, NULL
);
1665 sigaction(SIGTERM
, &act
, NULL
);
1667 act
.sa_handler
= sigchld_handler
;
1668 act
.sa_flags
= SA_NOCLDSTOP
;
1669 sigaction(SIGCHLD
, &act
, NULL
);
1675 /* Look for support files in the same directory as the executable. */
1676 static char *find_datadir(const char *argv0
)
1682 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
1689 while (p
!= buf
&& *p
!= '\\')
1692 if (access(buf
, R_OK
) == 0) {
1693 return qemu_strdup(buf
);
1699 /* Find a likely location for support files using the location of the binary.
1700 For installed binaries this will be "$bindir/../share/qemu". When
1701 running from the build tree this will be "$bindir/../pc-bios". */
1702 #define SHARE_SUFFIX "/share/qemu"
1703 #define BUILD_SUFFIX "/pc-bios"
1704 static char *find_datadir(const char *argv0
)
1712 #if defined(__linux__)
1715 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
1721 #elif defined(__FreeBSD__)
1723 static int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
1724 size_t len
= sizeof(buf
) - 1;
1727 if (!sysctl(mib
, sizeof(mib
)/sizeof(*mib
), buf
, &len
, NULL
, 0) &&
1729 buf
[sizeof(buf
) - 1] = '\0';
1734 /* If we don't have any way of figuring out the actual executable
1735 location then try argv[0]. */
1737 p
= realpath(argv0
, buf
);
1745 max_len
= strlen(dir
) +
1746 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
1747 res
= qemu_mallocz(max_len
);
1748 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
1749 if (access(res
, R_OK
)) {
1750 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
1751 if (access(res
, R_OK
)) {
1763 char *qemu_find_file(int type
, const char *name
)
1769 /* If name contains path separators then try it as a straight path. */
1770 if ((strchr(name
, '/') || strchr(name
, '\\'))
1771 && access(name
, R_OK
) == 0) {
1772 return qemu_strdup(name
);
1775 case QEMU_FILE_TYPE_BIOS
:
1778 case QEMU_FILE_TYPE_KEYMAP
:
1779 subdir
= "keymaps/";
1784 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1785 buf
= qemu_mallocz(len
);
1786 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1787 if (access(buf
, R_OK
)) {
1794 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1796 return qdev_device_help(opts
);
1799 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1803 dev
= qdev_device_add(opts
);
1809 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1811 CharDriverState
*chr
;
1813 chr
= qemu_chr_open_opts(opts
, NULL
);
1820 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1823 ret
= qemu_fsdev_add(opts
);
1829 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1831 CharDriverState
*chr
;
1832 const char *chardev
;
1836 mode
= qemu_opt_get(opts
, "mode");
1840 if (strcmp(mode
, "readline") == 0) {
1841 flags
= MONITOR_USE_READLINE
;
1842 } else if (strcmp(mode
, "control") == 0) {
1843 flags
= MONITOR_USE_CONTROL
;
1845 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1849 if (qemu_opt_get_bool(opts
, "default", 0))
1850 flags
|= MONITOR_IS_DEFAULT
;
1852 chardev
= qemu_opt_get(opts
, "chardev");
1853 chr
= qemu_chr_find(chardev
);
1855 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1859 monitor_init(chr
, flags
);
1863 static void monitor_parse(const char *optarg
, const char *mode
)
1865 static int monitor_device_index
= 0;
1871 if (strstart(optarg
, "chardev:", &p
)) {
1872 snprintf(label
, sizeof(label
), "%s", p
);
1874 snprintf(label
, sizeof(label
), "compat_monitor%d",
1875 monitor_device_index
);
1876 if (monitor_device_index
== 0) {
1879 opts
= qemu_chr_parse_compat(label
, optarg
);
1881 fprintf(stderr
, "parse error: %s\n", optarg
);
1886 opts
= qemu_opts_create(&qemu_mon_opts
, label
, 1);
1888 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1891 qemu_opt_set(opts
, "mode", mode
);
1892 qemu_opt_set(opts
, "chardev", label
);
1894 qemu_opt_set(opts
, "default", "on");
1895 monitor_device_index
++;
1898 struct device_config
{
1900 DEV_USB
, /* -usbdevice */
1902 DEV_SERIAL
, /* -serial */
1903 DEV_PARALLEL
, /* -parallel */
1904 DEV_VIRTCON
, /* -virtioconsole */
1905 DEV_DEBUGCON
, /* -debugcon */
1907 const char *cmdline
;
1908 QTAILQ_ENTRY(device_config
) next
;
1910 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1912 static void add_device_config(int type
, const char *cmdline
)
1914 struct device_config
*conf
;
1916 conf
= qemu_mallocz(sizeof(*conf
));
1918 conf
->cmdline
= cmdline
;
1919 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1922 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1924 struct device_config
*conf
;
1927 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1928 if (conf
->type
!= type
)
1930 rc
= func(conf
->cmdline
);
1937 static int serial_parse(const char *devname
)
1939 static int index
= 0;
1942 if (strcmp(devname
, "none") == 0)
1944 if (index
== MAX_SERIAL_PORTS
) {
1945 fprintf(stderr
, "qemu: too many serial ports\n");
1948 snprintf(label
, sizeof(label
), "serial%d", index
);
1949 serial_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1950 if (!serial_hds
[index
]) {
1951 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1952 devname
, strerror(errno
));
1959 static int parallel_parse(const char *devname
)
1961 static int index
= 0;
1964 if (strcmp(devname
, "none") == 0)
1966 if (index
== MAX_PARALLEL_PORTS
) {
1967 fprintf(stderr
, "qemu: too many parallel ports\n");
1970 snprintf(label
, sizeof(label
), "parallel%d", index
);
1971 parallel_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
1972 if (!parallel_hds
[index
]) {
1973 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1974 devname
, strerror(errno
));
1981 static int virtcon_parse(const char *devname
)
1983 static int index
= 0;
1985 QemuOpts
*bus_opts
, *dev_opts
;
1987 if (strcmp(devname
, "none") == 0)
1989 if (index
== MAX_VIRTIO_CONSOLES
) {
1990 fprintf(stderr
, "qemu: too many virtio consoles\n");
1994 bus_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1995 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1997 dev_opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
1998 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2000 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2001 virtcon_hds
[index
] = qemu_chr_open(label
, devname
, NULL
);
2002 if (!virtcon_hds
[index
]) {
2003 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2004 devname
, strerror(errno
));
2007 qemu_opt_set(dev_opts
, "chardev", label
);
2013 static int debugcon_parse(const char *devname
)
2017 if (!qemu_chr_open("debugcon", devname
, NULL
)) {
2020 opts
= qemu_opts_create(&qemu_device_opts
, "debugcon", 1);
2022 fprintf(stderr
, "qemu: already have a debugcon device\n");
2025 qemu_opt_set(opts
, "driver", "isa-debugcon");
2026 qemu_opt_set(opts
, "chardev", "debugcon");
2030 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2031 const char **poptarg
, int *poptind
)
2033 const QEMUOption
*popt
;
2034 int optind
= *poptind
;
2035 char *r
= argv
[optind
];
2038 loc_set_cmdline(argv
, optind
, 1);
2040 /* Treat --foo the same as -foo. */
2043 popt
= qemu_options
;
2046 error_report("invalid option");
2049 if (!strcmp(popt
->name
, r
+ 1))
2053 if (popt
->flags
& HAS_ARG
) {
2054 if (optind
>= argc
) {
2055 error_report("requires an argument");
2058 optarg
= argv
[optind
++];
2059 loc_set_cmdline(argv
, optind
- 2, 2);
2070 int main(int argc
, char **argv
, char **envp
)
2072 const char *gdbstub_dev
= NULL
;
2074 int snapshot
, linux_boot
;
2075 const char *icount_option
= NULL
;
2076 const char *initrd_filename
;
2077 const char *kernel_filename
, *kernel_cmdline
;
2078 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2080 DisplayChangeListener
*dcl
;
2081 int cyls
, heads
, secs
, translation
;
2082 QemuOpts
*hda_opts
= NULL
, *opts
;
2085 const char *loadvm
= NULL
;
2086 QEMUMachine
*machine
;
2087 const char *cpu_model
;
2092 const char *pid_file
= NULL
;
2093 const char *incoming
= NULL
;
2096 struct passwd
*pwd
= NULL
;
2097 const char *chroot_dir
= NULL
;
2098 const char *run_as
= NULL
;
2100 int show_vnc_port
= 0;
2103 error_set_progname(argv
[0]);
2107 qemu_cache_utils_init(envp
);
2109 QLIST_INIT (&vm_change_state_head
);
2112 struct sigaction act
;
2113 sigfillset(&act
.sa_mask
);
2115 act
.sa_handler
= SIG_IGN
;
2116 sigaction(SIGPIPE
, &act
, NULL
);
2119 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
2120 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2121 QEMU to run on a single CPU */
2126 h
= GetCurrentProcess();
2127 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
2128 for(i
= 0; i
< 32; i
++) {
2129 if (mask
& (1 << i
))
2134 SetProcessAffinityMask(h
, mask
);
2140 module_call_init(MODULE_INIT_MACHINE
);
2141 machine
= find_default_machine();
2143 initrd_filename
= NULL
;
2146 kernel_filename
= NULL
;
2147 kernel_cmdline
= "";
2148 cyls
= heads
= secs
= 0;
2149 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2151 for (i
= 0; i
< MAX_NODES
; i
++) {
2153 node_cpumask
[i
] = 0;
2162 /* first pass of option parsing */
2164 while (optind
< argc
) {
2165 if (argv
[optind
][0] != '-') {
2170 const QEMUOption
*popt
;
2172 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2173 switch (popt
->index
) {
2174 case QEMU_OPTION_nodefconfig
:
2184 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2185 if (ret
< 0 && ret
!= -ENOENT
) {
2189 ret
= qemu_read_config_file(arch_config_name
);
2190 if (ret
< 0 && ret
!= -ENOENT
) {
2196 /* second pass of option parsing */
2201 if (argv
[optind
][0] != '-') {
2202 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
2204 const QEMUOption
*popt
;
2206 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2207 if (!(popt
->arch_mask
& arch_type
)) {
2208 printf("Option %s not supported for this target\n", popt
->name
);
2211 switch(popt
->index
) {
2213 machine
= find_machine(optarg
);
2216 printf("Supported machines are:\n");
2217 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2219 printf("%-10s %s (alias of %s)\n",
2220 m
->alias
, m
->desc
, m
->name
);
2221 printf("%-10s %s%s\n",
2223 m
->is_default
? " (default)" : "");
2225 exit(*optarg
!= '?');
2228 case QEMU_OPTION_cpu
:
2229 /* hw initialization will check this */
2230 if (*optarg
== '?') {
2231 list_cpus(stdout
, &fprintf
, optarg
);
2237 case QEMU_OPTION_initrd
:
2238 initrd_filename
= optarg
;
2240 case QEMU_OPTION_hda
:
2242 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
2244 hda_opts
= drive_add(optarg
, HD_ALIAS
2245 ",cyls=%d,heads=%d,secs=%d%s",
2246 0, cyls
, heads
, secs
,
2247 translation
== BIOS_ATA_TRANSLATION_LBA
?
2249 translation
== BIOS_ATA_TRANSLATION_NONE
?
2250 ",trans=none" : "");
2252 case QEMU_OPTION_hdb
:
2253 case QEMU_OPTION_hdc
:
2254 case QEMU_OPTION_hdd
:
2255 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
2257 case QEMU_OPTION_drive
:
2258 drive_add(NULL
, "%s", optarg
);
2260 case QEMU_OPTION_set
:
2261 if (qemu_set_option(optarg
) != 0)
2264 case QEMU_OPTION_global
:
2265 if (qemu_global_option(optarg
) != 0)
2268 case QEMU_OPTION_mtdblock
:
2269 drive_add(optarg
, MTD_ALIAS
);
2271 case QEMU_OPTION_sd
:
2272 drive_add(optarg
, SD_ALIAS
);
2274 case QEMU_OPTION_pflash
:
2275 drive_add(optarg
, PFLASH_ALIAS
);
2277 case QEMU_OPTION_snapshot
:
2280 case QEMU_OPTION_hdachs
:
2284 cyls
= strtol(p
, (char **)&p
, 0);
2285 if (cyls
< 1 || cyls
> 16383)
2290 heads
= strtol(p
, (char **)&p
, 0);
2291 if (heads
< 1 || heads
> 16)
2296 secs
= strtol(p
, (char **)&p
, 0);
2297 if (secs
< 1 || secs
> 63)
2301 if (!strcmp(p
, "none"))
2302 translation
= BIOS_ATA_TRANSLATION_NONE
;
2303 else if (!strcmp(p
, "lba"))
2304 translation
= BIOS_ATA_TRANSLATION_LBA
;
2305 else if (!strcmp(p
, "auto"))
2306 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2309 } else if (*p
!= '\0') {
2311 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2314 if (hda_opts
!= NULL
) {
2316 snprintf(num
, sizeof(num
), "%d", cyls
);
2317 qemu_opt_set(hda_opts
, "cyls", num
);
2318 snprintf(num
, sizeof(num
), "%d", heads
);
2319 qemu_opt_set(hda_opts
, "heads", num
);
2320 snprintf(num
, sizeof(num
), "%d", secs
);
2321 qemu_opt_set(hda_opts
, "secs", num
);
2322 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2323 qemu_opt_set(hda_opts
, "trans", "lba");
2324 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2325 qemu_opt_set(hda_opts
, "trans", "none");
2329 case QEMU_OPTION_numa
:
2330 if (nb_numa_nodes
>= MAX_NODES
) {
2331 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2336 case QEMU_OPTION_nographic
:
2337 display_type
= DT_NOGRAPHIC
;
2339 #ifdef CONFIG_CURSES
2340 case QEMU_OPTION_curses
:
2341 display_type
= DT_CURSES
;
2344 case QEMU_OPTION_portrait
:
2347 case QEMU_OPTION_kernel
:
2348 kernel_filename
= optarg
;
2350 case QEMU_OPTION_append
:
2351 kernel_cmdline
= optarg
;
2353 case QEMU_OPTION_cdrom
:
2354 drive_add(optarg
, CDROM_ALIAS
);
2356 case QEMU_OPTION_boot
:
2358 static const char * const params
[] = {
2359 "order", "once", "menu", NULL
2361 char buf
[sizeof(boot_devices
)];
2362 char *standard_boot_devices
;
2365 if (!strchr(optarg
, '=')) {
2367 pstrcpy(buf
, sizeof(buf
), optarg
);
2368 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2370 "qemu: unknown boot parameter '%s' in '%s'\n",
2376 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2377 validate_bootdevices(buf
);
2378 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2381 if (get_param_value(buf
, sizeof(buf
),
2383 validate_bootdevices(buf
);
2384 standard_boot_devices
= qemu_strdup(boot_devices
);
2385 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2386 qemu_register_reset(restore_boot_devices
,
2387 standard_boot_devices
);
2389 if (get_param_value(buf
, sizeof(buf
),
2391 if (!strcmp(buf
, "on")) {
2393 } else if (!strcmp(buf
, "off")) {
2397 "qemu: invalid option value '%s'\n",
2405 case QEMU_OPTION_fda
:
2406 case QEMU_OPTION_fdb
:
2407 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
2409 case QEMU_OPTION_no_fd_bootchk
:
2412 case QEMU_OPTION_netdev
:
2413 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
2417 case QEMU_OPTION_net
:
2418 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
2423 case QEMU_OPTION_tftp
:
2424 legacy_tftp_prefix
= optarg
;
2426 case QEMU_OPTION_bootp
:
2427 legacy_bootp_filename
= optarg
;
2430 case QEMU_OPTION_smb
:
2431 if (net_slirp_smb(optarg
) < 0)
2435 case QEMU_OPTION_redir
:
2436 if (net_slirp_redir(optarg
) < 0)
2440 case QEMU_OPTION_bt
:
2441 add_device_config(DEV_BT
, optarg
);
2443 case QEMU_OPTION_audio_help
:
2444 if (!(audio_available())) {
2445 printf("Option %s not supported for this target\n", popt
->name
);
2451 case QEMU_OPTION_soundhw
:
2452 if (!(audio_available())) {
2453 printf("Option %s not supported for this target\n", popt
->name
);
2456 select_soundhw (optarg
);
2461 case QEMU_OPTION_version
:
2465 case QEMU_OPTION_m
: {
2469 value
= strtoul(optarg
, &ptr
, 10);
2471 case 0: case 'M': case 'm':
2478 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2482 /* On 32-bit hosts, QEMU is limited by virtual address space */
2483 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
2484 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
2487 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2488 fprintf(stderr
, "qemu: ram size too large\n");
2494 case QEMU_OPTION_mempath
:
2498 case QEMU_OPTION_mem_prealloc
:
2503 set_cpu_log(optarg
);
2506 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2508 case QEMU_OPTION_gdb
:
2509 gdbstub_dev
= optarg
;
2514 case QEMU_OPTION_bios
:
2517 case QEMU_OPTION_singlestep
:
2524 keyboard_layout
= optarg
;
2526 case QEMU_OPTION_localtime
:
2529 case QEMU_OPTION_vga
:
2530 select_vgahw (optarg
);
2537 w
= strtol(p
, (char **)&p
, 10);
2540 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2546 h
= strtol(p
, (char **)&p
, 10);
2551 depth
= strtol(p
, (char **)&p
, 10);
2552 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2553 depth
!= 24 && depth
!= 32)
2555 } else if (*p
== '\0') {
2556 depth
= graphic_depth
;
2563 graphic_depth
= depth
;
2566 case QEMU_OPTION_echr
:
2569 term_escape_char
= strtol(optarg
, &r
, 0);
2571 printf("Bad argument to echr\n");
2574 case QEMU_OPTION_monitor
:
2575 monitor_parse(optarg
, "readline");
2576 default_monitor
= 0;
2578 case QEMU_OPTION_qmp
:
2579 monitor_parse(optarg
, "control");
2580 default_monitor
= 0;
2582 case QEMU_OPTION_mon
:
2583 opts
= qemu_opts_parse(&qemu_mon_opts
, optarg
, 1);
2587 default_monitor
= 0;
2589 case QEMU_OPTION_chardev
:
2590 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, 1);
2596 case QEMU_OPTION_fsdev
:
2597 opts
= qemu_opts_parse(&qemu_fsdev_opts
, optarg
, 1);
2599 fprintf(stderr
, "parse error: %s\n", optarg
);
2603 case QEMU_OPTION_virtfs
: {
2604 char *arg_fsdev
= NULL
;
2605 char *arg_9p
= NULL
;
2608 opts
= qemu_opts_parse(&qemu_virtfs_opts
, optarg
, 1);
2610 fprintf(stderr
, "parse error: %s\n", optarg
);
2614 len
= strlen(",id=,path=");
2615 len
+= strlen(qemu_opt_get(opts
, "fstype"));
2616 len
+= strlen(qemu_opt_get(opts
, "mount_tag"));
2617 len
+= strlen(qemu_opt_get(opts
, "path"));
2618 arg_fsdev
= qemu_malloc((len
+ 1) * sizeof(*arg_fsdev
));
2621 fprintf(stderr
, "No memory to parse -fsdev for %s\n",
2626 sprintf(arg_fsdev
, "%s,id=%s,path=%s",
2627 qemu_opt_get(opts
, "fstype"),
2628 qemu_opt_get(opts
, "mount_tag"),
2629 qemu_opt_get(opts
, "path"));
2631 len
= strlen("virtio-9p-pci,fsdev=,mount_tag=");
2632 len
+= 2*strlen(qemu_opt_get(opts
, "mount_tag"));
2633 arg_9p
= qemu_malloc((len
+ 1) * sizeof(*arg_9p
));
2636 fprintf(stderr
, "No memory to parse -device for %s\n",
2641 sprintf(arg_9p
, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2642 qemu_opt_get(opts
, "mount_tag"),
2643 qemu_opt_get(opts
, "mount_tag"));
2645 if (!qemu_opts_parse(&qemu_fsdev_opts
, arg_fsdev
, 1)) {
2646 fprintf(stderr
, "parse error [fsdev]: %s\n", optarg
);
2650 if (!qemu_opts_parse(&qemu_device_opts
, arg_9p
, 1)) {
2651 fprintf(stderr
, "parse error [device]: %s\n", optarg
);
2655 qemu_free(arg_fsdev
);
2660 case QEMU_OPTION_serial
:
2661 add_device_config(DEV_SERIAL
, optarg
);
2663 if (strncmp(optarg
, "mon:", 4) == 0) {
2664 default_monitor
= 0;
2667 case QEMU_OPTION_watchdog
:
2670 "qemu: only one watchdog option may be given\n");
2675 case QEMU_OPTION_watchdog_action
:
2676 if (select_watchdog_action(optarg
) == -1) {
2677 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2681 case QEMU_OPTION_virtiocon
:
2682 add_device_config(DEV_VIRTCON
, optarg
);
2683 default_virtcon
= 0;
2684 if (strncmp(optarg
, "mon:", 4) == 0) {
2685 default_monitor
= 0;
2688 case QEMU_OPTION_parallel
:
2689 add_device_config(DEV_PARALLEL
, optarg
);
2690 default_parallel
= 0;
2691 if (strncmp(optarg
, "mon:", 4) == 0) {
2692 default_monitor
= 0;
2695 case QEMU_OPTION_debugcon
:
2696 add_device_config(DEV_DEBUGCON
, optarg
);
2698 case QEMU_OPTION_loadvm
:
2701 case QEMU_OPTION_full_screen
:
2705 case QEMU_OPTION_no_frame
:
2708 case QEMU_OPTION_alt_grab
:
2711 case QEMU_OPTION_ctrl_grab
:
2714 case QEMU_OPTION_no_quit
:
2717 case QEMU_OPTION_sdl
:
2718 display_type
= DT_SDL
;
2721 case QEMU_OPTION_pidfile
:
2724 case QEMU_OPTION_win2k_hack
:
2725 win2k_install_hack
= 1;
2727 case QEMU_OPTION_rtc_td_hack
:
2730 case QEMU_OPTION_acpitable
:
2731 do_acpitable_option(optarg
);
2733 case QEMU_OPTION_smbios
:
2734 do_smbios_option(optarg
);
2736 case QEMU_OPTION_enable_kvm
:
2739 case QEMU_OPTION_usb
:
2742 case QEMU_OPTION_usbdevice
:
2744 add_device_config(DEV_USB
, optarg
);
2746 case QEMU_OPTION_device
:
2747 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, 1)) {
2751 case QEMU_OPTION_smp
:
2754 fprintf(stderr
, "Invalid number of CPUs\n");
2757 if (max_cpus
< smp_cpus
) {
2758 fprintf(stderr
, "maxcpus must be equal to or greater than "
2762 if (max_cpus
> 255) {
2763 fprintf(stderr
, "Unsupported number of maxcpus\n");
2767 case QEMU_OPTION_vnc
:
2768 display_type
= DT_VNC
;
2769 vnc_display
= optarg
;
2771 case QEMU_OPTION_no_acpi
:
2774 case QEMU_OPTION_no_hpet
:
2777 case QEMU_OPTION_balloon
:
2778 if (balloon_parse(optarg
) < 0) {
2779 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2783 case QEMU_OPTION_no_reboot
:
2786 case QEMU_OPTION_no_shutdown
:
2789 case QEMU_OPTION_show_cursor
:
2792 case QEMU_OPTION_uuid
:
2793 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2794 fprintf(stderr
, "Fail to parse UUID string."
2795 " Wrong format.\n");
2800 case QEMU_OPTION_daemonize
:
2804 case QEMU_OPTION_option_rom
:
2805 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2806 fprintf(stderr
, "Too many option ROMs\n");
2809 option_rom
[nb_option_roms
] = optarg
;
2812 case QEMU_OPTION_semihosting
:
2813 semihosting_enabled
= 1;
2815 case QEMU_OPTION_name
:
2816 qemu_name
= qemu_strdup(optarg
);
2818 char *p
= strchr(qemu_name
, ',');
2821 if (strncmp(p
, "process=", 8)) {
2822 fprintf(stderr
, "Unknown subargument %s to -name", p
);
2830 case QEMU_OPTION_prom_env
:
2831 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2832 fprintf(stderr
, "Too many prom variables\n");
2835 prom_envs
[nb_prom_envs
] = optarg
;
2838 case QEMU_OPTION_old_param
:
2841 case QEMU_OPTION_clock
:
2842 configure_alarms(optarg
);
2844 case QEMU_OPTION_startdate
:
2845 configure_rtc_date_offset(optarg
, 1);
2847 case QEMU_OPTION_rtc
:
2848 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, 0);
2852 configure_rtc(opts
);
2854 case QEMU_OPTION_tb_size
:
2855 tb_size
= strtol(optarg
, NULL
, 0);
2859 case QEMU_OPTION_icount
:
2860 icount_option
= optarg
;
2862 case QEMU_OPTION_incoming
:
2865 case QEMU_OPTION_nodefaults
:
2867 default_parallel
= 0;
2868 default_virtcon
= 0;
2869 default_monitor
= 0;
2877 case QEMU_OPTION_chroot
:
2878 chroot_dir
= optarg
;
2880 case QEMU_OPTION_runas
:
2884 case QEMU_OPTION_xen_domid
:
2885 if (!(xen_available())) {
2886 printf("Option %s not supported for this target\n", popt
->name
);
2889 xen_domid
= atoi(optarg
);
2891 case QEMU_OPTION_xen_create
:
2892 if (!(xen_available())) {
2893 printf("Option %s not supported for this target\n", popt
->name
);
2896 xen_mode
= XEN_CREATE
;
2898 case QEMU_OPTION_xen_attach
:
2899 if (!(xen_available())) {
2900 printf("Option %s not supported for this target\n", popt
->name
);
2903 xen_mode
= XEN_ATTACH
;
2905 case QEMU_OPTION_readconfig
:
2907 int ret
= qemu_read_config_file(optarg
);
2909 fprintf(stderr
, "read config %s: %s\n", optarg
,
2915 case QEMU_OPTION_writeconfig
:
2918 if (strcmp(optarg
, "-") == 0) {
2921 fp
= fopen(optarg
, "w");
2923 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
2927 qemu_config_write(fp
);
2936 /* If no data_dir is specified then try to find it relative to the
2939 data_dir
= find_datadir(argv
[0]);
2941 /* If all else fails use the install patch specified when building. */
2943 data_dir
= CONFIG_QEMU_SHAREDIR
;
2947 * Default to max_cpus = smp_cpus, in case the user doesn't
2948 * specify a max_cpus value.
2951 max_cpus
= smp_cpus
;
2953 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
2954 if (smp_cpus
> machine
->max_cpus
) {
2955 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
2956 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
2961 qemu_opts_foreach(&qemu_device_opts
, default_driver_check
, NULL
, 0);
2962 qemu_opts_foreach(&qemu_global_opts
, default_driver_check
, NULL
, 0);
2964 if (machine
->no_serial
) {
2967 if (machine
->no_parallel
) {
2968 default_parallel
= 0;
2970 if (!machine
->use_virtcon
) {
2971 default_virtcon
= 0;
2973 if (machine
->no_vga
) {
2976 if (machine
->no_floppy
) {
2979 if (machine
->no_cdrom
) {
2982 if (machine
->no_sdcard
) {
2986 if (display_type
== DT_NOGRAPHIC
) {
2987 if (default_parallel
)
2988 add_device_config(DEV_PARALLEL
, "null");
2989 if (default_serial
&& default_monitor
) {
2990 add_device_config(DEV_SERIAL
, "mon:stdio");
2991 } else if (default_virtcon
&& default_monitor
) {
2992 add_device_config(DEV_VIRTCON
, "mon:stdio");
2995 add_device_config(DEV_SERIAL
, "stdio");
2996 if (default_virtcon
)
2997 add_device_config(DEV_VIRTCON
, "stdio");
2998 if (default_monitor
)
2999 monitor_parse("stdio", "readline");
3003 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3004 if (default_parallel
)
3005 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3006 if (default_monitor
)
3007 monitor_parse("vc:80Cx24C", "readline");
3008 if (default_virtcon
)
3009 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3012 vga_interface_type
= VGA_CIRRUS
;
3016 if (qemu_opts_foreach(&qemu_chardev_opts
, chardev_init_func
, NULL
, 1) != 0)
3019 if (qemu_opts_foreach(&qemu_fsdev_opts
, fsdev_init_func
, NULL
, 1) != 0) {
3028 if (pipe(fds
) == -1)
3039 len
= read(fds
[0], &status
, 1);
3040 if (len
== -1 && (errno
== EINTR
))
3045 else if (status
== 1) {
3046 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
3054 qemu_set_cloexec(fds
[1]);
3066 signal(SIGTSTP
, SIG_IGN
);
3067 signal(SIGTTOU
, SIG_IGN
);
3068 signal(SIGTTIN
, SIG_IGN
);
3072 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3076 if (write(fds
[1], &status
, 1) != 1) {
3077 perror("daemonize. Writing to pipe\n");
3081 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
3086 int ret
= kvm_init(smp_cpus
);
3088 if (!kvm_available()) {
3089 printf("KVM not supported for this target\n");
3091 fprintf(stderr
, "failed to initialize KVM: %s\n", strerror(-ret
));
3097 if (qemu_init_main_loop()) {
3098 fprintf(stderr
, "qemu_init_main_loop failed\n");
3101 linux_boot
= (kernel_filename
!= NULL
);
3103 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3104 fprintf(stderr
, "-append only allowed with -kernel option\n");
3108 if (!linux_boot
&& initrd_filename
!= NULL
) {
3109 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3114 /* Win32 doesn't support line-buffering and requires size >= 2 */
3115 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3118 if (init_timer_alarm() < 0) {
3119 fprintf(stderr
, "could not initialize alarm timer\n");
3122 configure_icount(icount_option
);
3124 if (net_init_clients() < 0) {
3128 /* init the bluetooth world */
3129 if (foreach_device_config(DEV_BT
, bt_parse
))
3132 /* init the memory */
3134 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3136 /* init the dynamic translator */
3137 cpu_exec_init_all(tb_size
* 1024 * 1024);
3139 bdrv_init_with_whitelist();
3143 if (default_cdrom
) {
3144 /* we always create the cdrom drive, even if no disk is there */
3145 drive_add(NULL
, CDROM_ALIAS
);
3148 if (default_floppy
) {
3149 /* we always create at least one floppy */
3150 drive_add(NULL
, FD_ALIAS
, 0);
3153 if (default_sdcard
) {
3154 /* we always create one sd slot, even if no card is in it */
3155 drive_add(NULL
, SD_ALIAS
);
3158 /* open the virtual block devices */
3160 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
3161 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, &machine
->use_scsi
, 1) != 0)
3164 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
3167 if (nb_numa_nodes
> 0) {
3170 if (nb_numa_nodes
> smp_cpus
) {
3171 nb_numa_nodes
= smp_cpus
;
3174 /* If no memory size if given for any node, assume the default case
3175 * and distribute the available memory equally across all nodes
3177 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3178 if (node_mem
[i
] != 0)
3181 if (i
== nb_numa_nodes
) {
3182 uint64_t usedmem
= 0;
3184 /* On Linux, the each node's border has to be 8MB aligned,
3185 * the final node gets the rest.
3187 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3188 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3189 usedmem
+= node_mem
[i
];
3191 node_mem
[i
] = ram_size
- usedmem
;
3194 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3195 if (node_cpumask
[i
] != 0)
3198 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3199 * must cope with this anyway, because there are BIOSes out there in
3200 * real machines which also use this scheme.
3202 if (i
== nb_numa_nodes
) {
3203 for (i
= 0; i
< smp_cpus
; i
++) {
3204 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3209 if (qemu_opts_foreach(&qemu_mon_opts
, mon_init_func
, NULL
, 1) != 0) {
3213 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3215 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3217 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3219 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3222 module_call_init(MODULE_INIT_DEVICE
);
3224 if (qemu_opts_foreach(&qemu_device_opts
, device_help_func
, NULL
, 0) != 0)
3228 i
= select_watchdog(watchdog
);
3230 exit (i
== 1 ? 1 : 0);
3233 if (machine
->compat_props
) {
3234 qdev_prop_register_global_list(machine
->compat_props
);
3238 machine
->init(ram_size
, boot_devices
,
3239 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3241 cpu_synchronize_all_post_init();
3244 /* must be after terminal init, SDL library changes signal handlers */
3250 current_machine
= machine
;
3252 /* init USB devices */
3254 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3258 /* init generic devices */
3259 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
3262 net_check_clients();
3264 /* just use the first displaystate for the moment */
3265 ds
= get_displaystate();
3267 if (display_type
== DT_DEFAULT
) {
3268 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3269 display_type
= DT_SDL
;
3271 display_type
= DT_VNC
;
3272 vnc_display
= "localhost:0,to=99";
3278 switch (display_type
) {
3281 #if defined(CONFIG_CURSES)
3283 curses_display_init(ds
, full_screen
);
3286 #if defined(CONFIG_SDL)
3288 sdl_display_init(ds
, full_screen
, no_frame
);
3290 #elif defined(CONFIG_COCOA)
3292 cocoa_display_init(ds
, full_screen
);
3296 vnc_display_init(ds
);
3297 if (vnc_display_open(ds
, vnc_display
) < 0)
3300 if (show_vnc_port
) {
3301 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3309 dcl
= ds
->listeners
;
3310 while (dcl
!= NULL
) {
3311 if (dcl
->dpy_refresh
!= NULL
) {
3312 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
3313 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
3318 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
3319 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
3320 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
3323 text_consoles_set_display(ds
);
3325 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3326 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3331 qdev_machine_creation_done();
3333 if (rom_load_all() != 0) {
3334 fprintf(stderr
, "rom loading failed\n");
3338 qemu_system_reset();
3340 if (load_vmstate(loadvm
) < 0) {
3346 qemu_start_incoming_migration(incoming
);
3347 } else if (autostart
) {
3357 len
= write(fds
[1], &status
, 1);
3358 if (len
== -1 && (errno
== EINTR
))
3365 perror("not able to chdir to /");
3368 TFR(fd
= qemu_open("/dev/null", O_RDWR
));
3374 pwd
= getpwnam(run_as
);
3376 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
3382 if (chroot(chroot_dir
) < 0) {
3383 fprintf(stderr
, "chroot failed\n");
3387 perror("not able to chdir to /");
3393 if (setgid(pwd
->pw_gid
) < 0) {
3394 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
3397 if (setuid(pwd
->pw_uid
) < 0) {
3398 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
3401 if (setuid(0) != -1) {
3402 fprintf(stderr
, "Dropping privileges failed\n");