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>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #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"
161 #include "trace/control.h"
162 #include "qemu-queue.h"
164 #include "arch_init.h"
166 #include "ui/qemu-spice.h"
169 //#define DEBUG_SLIRP
171 #define DEFAULT_RAM_SIZE 128
173 #define MAX_VIRTIO_CONSOLES 1
175 static const char *data_dir
;
176 const char *bios_name
= NULL
;
177 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
178 DisplayType display_type
= DT_DEFAULT
;
179 int display_remote
= 0;
180 const char* keyboard_layout
= NULL
;
182 const char *mem_path
= NULL
;
184 int mem_prealloc
= 0; /* force preallocation of physical target memory */
187 NICInfo nd_table
[MAX_NICS
];
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
194 static int full_screen
= 0;
196 static int no_frame
= 0;
199 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
200 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
201 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
202 int win2k_install_hack
= 0;
211 const char *vnc_display
;
213 int acpi_enabled
= 1;
219 int graphic_rotate
= 0;
220 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint8_t *boot_splash_filedata
;
233 int boot_splash_filedata_size
;
234 uint8_t qemu_extra_params_fw
[2];
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 static QEMUTimer
*nographic_timer
;
253 uint8_t qemu_uuid
[16];
255 static QEMUBootSetHandler
*boot_set_handler
;
256 static void *boot_set_opaque
;
258 static NotifierList exit_notifiers
=
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
261 static NotifierList machine_init_done_notifiers
=
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
264 static int tcg_allowed
= 1;
268 enum xen_mode xen_mode
= XEN_EMULATE
;
269 static int tcg_tb_size
;
271 static int default_serial
= 1;
272 static int default_parallel
= 1;
273 static int default_virtcon
= 1;
274 static int default_monitor
= 1;
275 static int default_vga
= 1;
276 static int default_floppy
= 1;
277 static int default_cdrom
= 1;
278 static int default_sdcard
= 1;
284 { .driver
= "isa-serial", .flag
= &default_serial
},
285 { .driver
= "isa-parallel", .flag
= &default_parallel
},
286 { .driver
= "isa-fdc", .flag
= &default_floppy
},
287 { .driver
= "ide-cd", .flag
= &default_cdrom
},
288 { .driver
= "ide-hd", .flag
= &default_cdrom
},
289 { .driver
= "ide-drive", .flag
= &default_cdrom
},
290 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
291 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
293 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
294 { .driver
= "VGA", .flag
= &default_vga
},
295 { .driver
= "cirrus-vga", .flag
= &default_vga
},
296 { .driver
= "vmware-svga", .flag
= &default_vga
},
297 { .driver
= "isa-vga", .flag
= &default_vga
},
298 { .driver
= "qxl-vga", .flag
= &default_vga
},
301 static void res_free(void)
303 if (boot_splash_filedata
!= NULL
) {
304 g_free(boot_splash_filedata
);
305 boot_splash_filedata
= NULL
;
309 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
311 const char *driver
= qemu_opt_get(opts
, "driver");
316 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
317 if (strcmp(default_list
[i
].driver
, driver
) != 0)
319 *(default_list
[i
].flag
) = 0;
324 /***********************************************************/
327 static RunState current_run_state
= RSTATE_NO_STATE
;
332 } RunStateTransition
;
334 static const RunStateTransition runstate_transitions_def
[] = {
336 { RSTATE_NO_STATE
, RSTATE_RUNNING
},
337 { RSTATE_NO_STATE
, RSTATE_IN_MIGRATE
},
338 { RSTATE_NO_STATE
, RSTATE_PRE_LAUNCH
},
340 { RSTATE_DEBUG
, RSTATE_RUNNING
},
342 { RSTATE_IN_MIGRATE
, RSTATE_RUNNING
},
343 { RSTATE_IN_MIGRATE
, RSTATE_PRE_LAUNCH
},
345 { RSTATE_PANICKED
, RSTATE_PAUSED
},
347 { RSTATE_IO_ERROR
, RSTATE_RUNNING
},
349 { RSTATE_PAUSED
, RSTATE_RUNNING
},
351 { RSTATE_POST_MIGRATE
, RSTATE_RUNNING
},
353 { RSTATE_PRE_LAUNCH
, RSTATE_RUNNING
},
354 { RSTATE_PRE_LAUNCH
, RSTATE_POST_MIGRATE
},
356 { RSTATE_PRE_MIGRATE
, RSTATE_RUNNING
},
357 { RSTATE_PRE_MIGRATE
, RSTATE_POST_MIGRATE
},
359 { RSTATE_RESTORE
, RSTATE_RUNNING
},
361 { RSTATE_RUNNING
, RSTATE_DEBUG
},
362 { RSTATE_RUNNING
, RSTATE_PANICKED
},
363 { RSTATE_RUNNING
, RSTATE_IO_ERROR
},
364 { RSTATE_RUNNING
, RSTATE_PAUSED
},
365 { RSTATE_RUNNING
, RSTATE_PRE_MIGRATE
},
366 { RSTATE_RUNNING
, RSTATE_RESTORE
},
367 { RSTATE_RUNNING
, RSTATE_SAVEVM
},
368 { RSTATE_RUNNING
, RSTATE_SHUTDOWN
},
369 { RSTATE_RUNNING
, RSTATE_WATCHDOG
},
371 { RSTATE_SAVEVM
, RSTATE_RUNNING
},
373 { RSTATE_SHUTDOWN
, RSTATE_PAUSED
},
375 { RSTATE_WATCHDOG
, RSTATE_RUNNING
},
377 { RSTATE_MAX
, RSTATE_MAX
},
380 static bool runstate_valid_transitions
[RSTATE_MAX
][RSTATE_MAX
];
382 bool runstate_check(RunState state
)
384 return current_run_state
== state
;
387 void runstate_init(void)
389 const RunStateTransition
*p
;
391 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
393 for (p
= &runstate_transitions_def
[0]; p
->from
!= RSTATE_MAX
; p
++) {
394 runstate_valid_transitions
[p
->from
][p
->to
] = true;
398 /* This function will abort() on invalid state transitions */
399 void runstate_set(RunState new_state
)
401 if (new_state
>= RSTATE_MAX
||
402 !runstate_valid_transitions
[current_run_state
][new_state
]) {
403 fprintf(stderr
, "invalid runstate transition\n");
407 current_run_state
= new_state
;
410 /***********************************************************/
411 /* real time host monotonic timer */
413 /***********************************************************/
414 /* host time/date access */
415 void qemu_get_timedate(struct tm
*tm
, int offset
)
422 if (rtc_date_offset
== -1) {
426 ret
= localtime(&ti
);
428 ti
-= rtc_date_offset
;
432 memcpy(tm
, ret
, sizeof(struct tm
));
435 int qemu_timedate_diff(struct tm
*tm
)
439 if (rtc_date_offset
== -1)
441 seconds
= mktimegm(tm
);
443 seconds
= mktime(tm
);
445 seconds
= mktimegm(tm
) + rtc_date_offset
;
447 return seconds
- time(NULL
);
450 void rtc_change_mon_event(struct tm
*tm
)
454 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
455 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
456 qobject_decref(data
);
459 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
461 time_t rtc_start_date
;
464 if (!strcmp(startdate
, "now") && legacy
) {
465 rtc_date_offset
= -1;
467 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
475 } else if (sscanf(startdate
, "%d-%d-%d",
487 rtc_start_date
= mktimegm(&tm
);
488 if (rtc_start_date
== -1) {
490 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
491 "'2006-06-17T16:01:21' or '2006-06-17'\n");
494 rtc_date_offset
= time(NULL
) - rtc_start_date
;
498 static void configure_rtc(QemuOpts
*opts
)
502 value
= qemu_opt_get(opts
, "base");
504 if (!strcmp(value
, "utc")) {
506 } else if (!strcmp(value
, "localtime")) {
509 configure_rtc_date_offset(value
, 0);
512 value
= qemu_opt_get(opts
, "clock");
514 if (!strcmp(value
, "host")) {
515 rtc_clock
= host_clock
;
516 } else if (!strcmp(value
, "vm")) {
517 rtc_clock
= vm_clock
;
519 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
523 value
= qemu_opt_get(opts
, "driftfix");
525 if (!strcmp(value
, "slew")) {
527 } else if (!strcmp(value
, "none")) {
530 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
536 /***********************************************************/
537 /* Bluetooth support */
540 static struct HCIInfo
*hci_table
[MAX_NICS
];
542 static struct bt_vlan_s
{
543 struct bt_scatternet_s net
;
545 struct bt_vlan_s
*next
;
548 /* find or alloc a new bluetooth "VLAN" */
549 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
551 struct bt_vlan_s
**pvlan
, *vlan
;
552 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
556 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
558 pvlan
= &first_bt_vlan
;
559 while (*pvlan
!= NULL
)
560 pvlan
= &(*pvlan
)->next
;
565 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
569 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
574 static struct HCIInfo null_hci
= {
575 .cmd_send
= null_hci_send
,
576 .sco_send
= null_hci_send
,
577 .acl_send
= null_hci_send
,
578 .bdaddr_set
= null_hci_addr_set
,
581 struct HCIInfo
*qemu_next_hci(void)
583 if (cur_hci
== nb_hcis
)
586 return hci_table
[cur_hci
++];
589 static struct HCIInfo
*hci_init(const char *str
)
592 struct bt_scatternet_s
*vlan
= 0;
594 if (!strcmp(str
, "null"))
597 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
599 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
600 else if (!strncmp(str
, "hci", 3)) {
603 if (!strncmp(str
+ 3, ",vlan=", 6)) {
604 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
609 vlan
= qemu_find_bt_vlan(0);
611 return bt_new_hci(vlan
);
614 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
619 static int bt_hci_parse(const char *str
)
624 if (nb_hcis
>= MAX_NICS
) {
625 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
638 bdaddr
.b
[5] = 0x56 + nb_hcis
;
639 hci
->bdaddr_set(hci
, bdaddr
.b
);
641 hci_table
[nb_hcis
++] = hci
;
646 static void bt_vhci_add(int vlan_id
)
648 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
651 fprintf(stderr
, "qemu: warning: adding a VHCI to "
652 "an empty scatternet %i\n", vlan_id
);
654 bt_vhci_init(bt_new_hci(vlan
));
657 static struct bt_device_s
*bt_device_add(const char *opt
)
659 struct bt_scatternet_s
*vlan
;
661 char *endp
= strstr(opt
, ",vlan=");
662 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
665 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
668 vlan_id
= strtol(endp
+ 6, &endp
, 0);
670 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
675 vlan
= qemu_find_bt_vlan(vlan_id
);
678 fprintf(stderr
, "qemu: warning: adding a slave device to "
679 "an empty scatternet %i\n", vlan_id
);
681 if (!strcmp(devname
, "keyboard"))
682 return bt_keyboard_init(vlan
);
684 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
688 static int bt_parse(const char *opt
)
690 const char *endp
, *p
;
693 if (strstart(opt
, "hci", &endp
)) {
694 if (!*endp
|| *endp
== ',') {
696 if (!strstart(endp
, ",vlan=", 0))
699 return bt_hci_parse(opt
);
701 } else if (strstart(opt
, "vhci", &endp
)) {
702 if (!*endp
|| *endp
== ',') {
704 if (strstart(endp
, ",vlan=", &p
)) {
705 vlan
= strtol(p
, (char **) &endp
, 0);
707 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
711 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
720 } else if (strstart(opt
, "device:", &endp
))
721 return !bt_device_add(endp
);
723 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
727 /***********************************************************/
728 /* QEMU Block devices */
730 #define HD_OPTS "media=disk"
731 #define CDROM_OPTS "media=cdrom"
733 #define PFLASH_OPTS ""
737 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
739 int *use_scsi
= opaque
;
741 return drive_init(opts
, *use_scsi
) == NULL
;
744 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
746 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
747 qemu_opt_set(opts
, "snapshot", "on");
752 static void default_drive(int enable
, int snapshot
, int use_scsi
,
753 BlockInterfaceType type
, int index
,
758 if (type
== IF_DEFAULT
) {
759 type
= use_scsi
? IF_SCSI
: IF_IDE
;
762 if (!enable
|| drive_get_by_index(type
, index
)) {
766 opts
= drive_add(type
, index
, NULL
, optstr
);
768 drive_enable_snapshot(opts
, NULL
);
770 if (!drive_init(opts
, use_scsi
)) {
775 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
777 boot_set_handler
= func
;
778 boot_set_opaque
= opaque
;
781 int qemu_boot_set(const char *boot_devices
)
783 if (!boot_set_handler
) {
786 return boot_set_handler(boot_set_opaque
, boot_devices
);
789 static void validate_bootdevices(char *devices
)
791 /* We just do some generic consistency checks */
795 for (p
= devices
; *p
!= '\0'; p
++) {
796 /* Allowed boot devices are:
797 * a-b: floppy disk drives
798 * c-f: IDE disk drives
799 * g-m: machine implementation dependant drives
800 * n-p: network devices
801 * It's up to each machine implementation to check if the given boot
802 * devices match the actual hardware implementation and firmware
805 if (*p
< 'a' || *p
> 'p') {
806 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
809 if (bitmap
& (1 << (*p
- 'a'))) {
810 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
813 bitmap
|= 1 << (*p
- 'a');
817 static void restore_boot_devices(void *opaque
)
819 char *standard_boot_devices
= opaque
;
820 static int first
= 1;
822 /* Restore boot order and remove ourselves after the first boot */
828 qemu_boot_set(standard_boot_devices
);
830 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
831 g_free(standard_boot_devices
);
834 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
837 FWBootEntry
*node
, *i
;
843 assert(dev
!= NULL
|| suffix
!= NULL
);
845 node
= g_malloc0(sizeof(FWBootEntry
));
846 node
->bootindex
= bootindex
;
847 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
850 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
851 if (i
->bootindex
== bootindex
) {
852 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
854 } else if (i
->bootindex
< bootindex
) {
857 QTAILQ_INSERT_BEFORE(i
, node
, link
);
860 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
864 * This function returns null terminated string that consist of new line
865 * separated device paths.
867 * memory pointed by "size" is assigned total length of the array in bytes
870 char *get_boot_devices_list(uint32_t *size
)
876 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
877 char *devpath
= NULL
, *bootpath
;
881 devpath
= qdev_get_fw_dev_path(i
->dev
);
885 if (i
->suffix
&& devpath
) {
886 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
888 bootpath
= g_malloc(bootpathlen
);
889 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
891 } else if (devpath
) {
894 bootpath
= g_strdup(i
->suffix
);
899 list
[total
-1] = '\n';
901 len
= strlen(bootpath
) + 1;
902 list
= g_realloc(list
, total
+ len
);
903 memcpy(&list
[total
], bootpath
, len
);
913 static void numa_add(const char *optarg
)
917 unsigned long long value
, endvalue
;
920 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
921 if (!strcmp(option
, "node")) {
922 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
923 nodenr
= nb_numa_nodes
;
925 nodenr
= strtoull(option
, NULL
, 10);
928 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
929 node_mem
[nodenr
] = 0;
932 sval
= strtosz(option
, NULL
);
934 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
937 node_mem
[nodenr
] = sval
;
939 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
940 node_cpumask
[nodenr
] = 0;
942 value
= strtoull(option
, &endptr
, 10);
945 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
947 if (*endptr
== '-') {
948 endvalue
= strtoull(endptr
+1, &endptr
, 10);
949 if (endvalue
>= 63) {
952 "only 63 CPUs in NUMA mode supported.\n");
954 value
= (2ULL << endvalue
) - (1ULL << value
);
956 value
= 1ULL << value
;
959 node_cpumask
[nodenr
] = value
;
966 static void smp_parse(const char *optarg
)
968 int smp
, sockets
= 0, threads
= 0, cores
= 0;
972 smp
= strtoul(optarg
, &endptr
, 10);
973 if (endptr
!= optarg
) {
974 if (*endptr
== ',') {
978 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
979 sockets
= strtoull(option
, NULL
, 10);
980 if (get_param_value(option
, 128, "cores", endptr
) != 0)
981 cores
= strtoull(option
, NULL
, 10);
982 if (get_param_value(option
, 128, "threads", endptr
) != 0)
983 threads
= strtoull(option
, NULL
, 10);
984 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
985 max_cpus
= strtoull(option
, NULL
, 10);
987 /* compute missing values, prefer sockets over cores over threads */
988 if (smp
== 0 || sockets
== 0) {
989 sockets
= sockets
> 0 ? sockets
: 1;
990 cores
= cores
> 0 ? cores
: 1;
991 threads
= threads
> 0 ? threads
: 1;
993 smp
= cores
* threads
* sockets
;
997 threads
= threads
> 0 ? threads
: 1;
998 cores
= smp
/ (sockets
* threads
);
1000 threads
= smp
/ (cores
* sockets
);
1004 smp_cores
= cores
> 0 ? cores
: 1;
1005 smp_threads
= threads
> 0 ? threads
: 1;
1007 max_cpus
= smp_cpus
;
1010 /***********************************************************/
1013 static int usb_device_add(const char *devname
)
1016 USBDevice
*dev
= NULL
;
1021 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1022 dev
= usbdevice_create(devname
);
1026 /* the other ones */
1027 #ifndef CONFIG_LINUX
1028 /* only the linux version is qdev-ified, usb-bsd still needs this */
1029 if (strstart(devname
, "host:", &p
)) {
1030 dev
= usb_host_device_open(p
);
1033 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1034 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1035 bt_new_hci(qemu_find_bt_vlan(0)));
1046 static int usb_device_del(const char *devname
)
1051 if (strstart(devname
, "host:", &p
))
1052 return usb_host_device_close(p
);
1057 p
= strchr(devname
, '.');
1060 bus_num
= strtoul(devname
, NULL
, 0);
1061 addr
= strtoul(p
+ 1, NULL
, 0);
1063 return usb_device_delete_addr(bus_num
, addr
);
1066 static int usb_parse(const char *cmdline
)
1069 r
= usb_device_add(cmdline
);
1071 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1076 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1078 const char *devname
= qdict_get_str(qdict
, "devname");
1079 if (usb_device_add(devname
) < 0) {
1080 error_report("could not add USB device '%s'", devname
);
1084 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1086 const char *devname
= qdict_get_str(qdict
, "devname");
1087 if (usb_device_del(devname
) < 0) {
1088 error_report("could not delete USB device '%s'", devname
);
1092 /***********************************************************/
1093 /* PCMCIA/Cardbus */
1095 static struct pcmcia_socket_entry_s
{
1096 PCMCIASocket
*socket
;
1097 struct pcmcia_socket_entry_s
*next
;
1098 } *pcmcia_sockets
= 0;
1100 void pcmcia_socket_register(PCMCIASocket
*socket
)
1102 struct pcmcia_socket_entry_s
*entry
;
1104 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1105 entry
->socket
= socket
;
1106 entry
->next
= pcmcia_sockets
;
1107 pcmcia_sockets
= entry
;
1110 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1112 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1114 ptr
= &pcmcia_sockets
;
1115 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1116 if (entry
->socket
== socket
) {
1122 void pcmcia_info(Monitor
*mon
)
1124 struct pcmcia_socket_entry_s
*iter
;
1126 if (!pcmcia_sockets
)
1127 monitor_printf(mon
, "No PCMCIA sockets\n");
1129 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1130 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1131 iter
->socket
->attached
? iter
->socket
->card_string
:
1135 /***********************************************************/
1136 /* machine registration */
1138 static QEMUMachine
*first_machine
= NULL
;
1139 QEMUMachine
*current_machine
= NULL
;
1141 int qemu_register_machine(QEMUMachine
*m
)
1144 pm
= &first_machine
;
1152 static QEMUMachine
*find_machine(const char *name
)
1156 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1157 if (!strcmp(m
->name
, name
))
1159 if (m
->alias
&& !strcmp(m
->alias
, name
))
1165 static QEMUMachine
*find_default_machine(void)
1169 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1170 if (m
->is_default
) {
1177 /***********************************************************/
1178 /* main execution loop */
1180 static void gui_update(void *opaque
)
1182 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1183 DisplayState
*ds
= opaque
;
1184 DisplayChangeListener
*dcl
= ds
->listeners
;
1186 qemu_flush_coalesced_mmio_buffer();
1189 while (dcl
!= NULL
) {
1190 if (dcl
->gui_timer_interval
&&
1191 dcl
->gui_timer_interval
< interval
)
1192 interval
= dcl
->gui_timer_interval
;
1195 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1198 static void nographic_update(void *opaque
)
1200 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1202 qemu_flush_coalesced_mmio_buffer();
1203 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1206 struct vm_change_state_entry
{
1207 VMChangeStateHandler
*cb
;
1209 QLIST_ENTRY (vm_change_state_entry
) entries
;
1212 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1214 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1217 VMChangeStateEntry
*e
;
1219 e
= g_malloc0(sizeof (*e
));
1223 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1227 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1229 QLIST_REMOVE (e
, entries
);
1233 void vm_state_notify(int running
, RunState state
)
1235 VMChangeStateEntry
*e
;
1237 trace_vm_state_notify(running
, state
);
1239 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1240 e
->cb(e
->opaque
, running
, state
);
1249 runstate_set(RSTATE_RUNNING
);
1250 vm_state_notify(1, RSTATE_RUNNING
);
1252 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1256 /* reset/shutdown handler */
1258 typedef struct QEMUResetEntry
{
1259 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1260 QEMUResetHandler
*func
;
1264 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1265 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1266 static int reset_requested
;
1267 static int shutdown_requested
, shutdown_signal
= -1;
1268 static pid_t shutdown_pid
;
1269 static int powerdown_requested
;
1270 static int debug_requested
;
1271 static RunState vmstop_requested
= RSTATE_NO_STATE
;
1273 int qemu_shutdown_requested_get(void)
1275 return shutdown_requested
;
1278 int qemu_reset_requested_get(void)
1280 return reset_requested
;
1283 int qemu_shutdown_requested(void)
1285 int r
= shutdown_requested
;
1286 shutdown_requested
= 0;
1290 void qemu_kill_report(void)
1292 if (shutdown_signal
!= -1) {
1293 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1294 if (shutdown_pid
== 0) {
1295 /* This happens for eg ^C at the terminal, so it's worth
1296 * avoiding printing an odd message in that case.
1298 fputc('\n', stderr
);
1300 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1302 shutdown_signal
= -1;
1306 int qemu_reset_requested(void)
1308 int r
= reset_requested
;
1309 reset_requested
= 0;
1313 int qemu_powerdown_requested(void)
1315 int r
= powerdown_requested
;
1316 powerdown_requested
= 0;
1320 static int qemu_debug_requested(void)
1322 int r
= debug_requested
;
1323 debug_requested
= 0;
1327 static RunState
qemu_vmstop_requested(void)
1329 RunState s
= vmstop_requested
;
1330 vmstop_requested
= RSTATE_NO_STATE
;
1334 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1336 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1339 re
->opaque
= opaque
;
1340 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1343 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1347 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1348 if (re
->func
== func
&& re
->opaque
== opaque
) {
1349 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1356 void qemu_system_reset(bool report
)
1358 QEMUResetEntry
*re
, *nre
;
1360 /* reset all devices */
1361 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1362 re
->func(re
->opaque
);
1365 monitor_protocol_event(QEVENT_RESET
, NULL
);
1367 cpu_synchronize_all_post_reset();
1370 void qemu_system_reset_request(void)
1373 shutdown_requested
= 1;
1375 reset_requested
= 1;
1378 qemu_notify_event();
1381 void qemu_system_killed(int signal
, pid_t pid
)
1383 shutdown_signal
= signal
;
1385 qemu_system_shutdown_request();
1388 void qemu_system_shutdown_request(void)
1390 shutdown_requested
= 1;
1391 qemu_notify_event();
1394 void qemu_system_powerdown_request(void)
1396 powerdown_requested
= 1;
1397 qemu_notify_event();
1400 void qemu_system_debug_request(void)
1402 debug_requested
= 1;
1403 qemu_notify_event();
1406 void qemu_system_vmstop_request(RunState state
)
1408 vmstop_requested
= state
;
1409 qemu_notify_event();
1412 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1413 static int n_poll_fds
;
1414 static int max_priority
;
1416 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1417 fd_set
*xfds
, struct timeval
*tv
)
1419 GMainContext
*context
= g_main_context_default();
1421 int timeout
= 0, cur_timeout
;
1423 g_main_context_prepare(context
, &max_priority
);
1425 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1426 poll_fds
, ARRAY_SIZE(poll_fds
));
1427 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1429 for (i
= 0; i
< n_poll_fds
; i
++) {
1430 GPollFD
*p
= &poll_fds
[i
];
1432 if ((p
->events
& G_IO_IN
)) {
1433 FD_SET(p
->fd
, rfds
);
1434 *max_fd
= MAX(*max_fd
, p
->fd
);
1436 if ((p
->events
& G_IO_OUT
)) {
1437 FD_SET(p
->fd
, wfds
);
1438 *max_fd
= MAX(*max_fd
, p
->fd
);
1440 if ((p
->events
& G_IO_ERR
)) {
1441 FD_SET(p
->fd
, xfds
);
1442 *max_fd
= MAX(*max_fd
, p
->fd
);
1446 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1447 if (timeout
>= 0 && timeout
< cur_timeout
) {
1448 tv
->tv_sec
= timeout
/ 1000;
1449 tv
->tv_usec
= (timeout
% 1000) * 1000;
1453 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1456 GMainContext
*context
= g_main_context_default();
1461 for (i
= 0; i
< n_poll_fds
; i
++) {
1462 GPollFD
*p
= &poll_fds
[i
];
1464 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1465 p
->revents
|= G_IO_IN
;
1467 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1468 p
->revents
|= G_IO_OUT
;
1470 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1471 p
->revents
|= G_IO_ERR
;
1476 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1477 g_main_context_dispatch(context
);
1481 int main_loop_wait(int nonblocking
)
1483 fd_set rfds
, wfds
, xfds
;
1491 timeout
= qemu_calculate_timeout();
1492 qemu_bh_update_timeout(&timeout
);
1495 os_host_main_loop_wait(&timeout
);
1497 tv
.tv_sec
= timeout
/ 1000;
1498 tv
.tv_usec
= (timeout
% 1000) * 1000;
1500 /* poll any events */
1501 /* XXX: separate device handlers from system ones */
1507 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1508 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1509 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1512 qemu_mutex_unlock_iothread();
1515 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1518 qemu_mutex_lock_iothread();
1521 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1522 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1523 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1525 qemu_run_all_timers();
1527 /* Check bottom-halves last in case any of the earlier events triggered
1534 qemu_irq qemu_system_powerdown
;
1536 static void main_loop(void)
1539 int last_io
__attribute__ ((unused
)) = 0;
1540 #ifdef CONFIG_PROFILER
1545 qemu_main_loop_start();
1548 nonblocking
= !kvm_enabled() && last_io
> 0;
1549 #ifdef CONFIG_PROFILER
1550 ti
= profile_getclock();
1552 last_io
= main_loop_wait(nonblocking
);
1553 #ifdef CONFIG_PROFILER
1554 dev_time
+= profile_getclock() - ti
;
1557 if (qemu_debug_requested()) {
1558 vm_stop(RSTATE_DEBUG
);
1560 if (qemu_shutdown_requested()) {
1562 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1564 vm_stop(RSTATE_SHUTDOWN
);
1568 if (qemu_reset_requested()) {
1570 cpu_synchronize_all_states();
1571 qemu_system_reset(VMRESET_REPORT
);
1574 if (qemu_powerdown_requested()) {
1575 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1576 qemu_irq_raise(qemu_system_powerdown
);
1578 if ((r
= qemu_vmstop_requested())) {
1586 static void version(void)
1588 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1591 static void help(int exitcode
)
1593 const char *options_help
=
1594 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1596 #define DEFHEADING(text) stringify(text) "\n"
1597 #include "qemu-options.def"
1603 printf("usage: %s [options] [disk_image]\n"
1605 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1608 "During emulation, the following keys are useful:\n"
1609 "ctrl-alt-f toggle full screen\n"
1610 "ctrl-alt-n switch to virtual console 'n'\n"
1611 "ctrl-alt toggle mouse and keyboard grab\n"
1613 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1619 #define HAS_ARG 0x0001
1621 typedef struct QEMUOption
{
1628 static const QEMUOption qemu_options
[] = {
1629 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1630 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1631 { option, opt_arg, opt_enum, arch_mask },
1632 #define DEFHEADING(text)
1633 #include "qemu-options.def"
1639 static void select_vgahw (const char *p
)
1644 vga_interface_type
= VGA_NONE
;
1645 if (strstart(p
, "std", &opts
)) {
1646 vga_interface_type
= VGA_STD
;
1647 } else if (strstart(p
, "cirrus", &opts
)) {
1648 vga_interface_type
= VGA_CIRRUS
;
1649 } else if (strstart(p
, "vmware", &opts
)) {
1650 vga_interface_type
= VGA_VMWARE
;
1651 } else if (strstart(p
, "xenfb", &opts
)) {
1652 vga_interface_type
= VGA_XENFB
;
1653 } else if (strstart(p
, "qxl", &opts
)) {
1654 vga_interface_type
= VGA_QXL
;
1655 } else if (!strstart(p
, "none", &opts
)) {
1657 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1661 const char *nextopt
;
1663 if (strstart(opts
, ",retrace=", &nextopt
)) {
1665 if (strstart(opts
, "dumb", &nextopt
))
1666 vga_retrace_method
= VGA_RETRACE_DUMB
;
1667 else if (strstart(opts
, "precise", &nextopt
))
1668 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1669 else goto invalid_vga
;
1670 } else goto invalid_vga
;
1675 static DisplayType
select_display(const char *p
)
1678 DisplayType display
= DT_DEFAULT
;
1680 if (strstart(p
, "sdl", &opts
)) {
1684 const char *nextopt
;
1686 if (strstart(opts
, ",frame=", &nextopt
)) {
1688 if (strstart(opts
, "on", &nextopt
)) {
1690 } else if (strstart(opts
, "off", &nextopt
)) {
1693 goto invalid_sdl_args
;
1695 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1697 if (strstart(opts
, "on", &nextopt
)) {
1699 } else if (strstart(opts
, "off", &nextopt
)) {
1702 goto invalid_sdl_args
;
1704 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1706 if (strstart(opts
, "on", &nextopt
)) {
1708 } else if (strstart(opts
, "off", &nextopt
)) {
1711 goto invalid_sdl_args
;
1713 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1715 if (strstart(opts
, "on", &nextopt
)) {
1717 } else if (strstart(opts
, "off", &nextopt
)) {
1720 goto invalid_sdl_args
;
1724 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1730 fprintf(stderr
, "SDL support is disabled\n");
1733 } else if (strstart(p
, "vnc", &opts
)) {
1738 const char *nextopt
;
1740 if (strstart(opts
, "=", &nextopt
)) {
1741 vnc_display
= nextopt
;
1745 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1749 fprintf(stderr
, "VNC support is disabled\n");
1752 } else if (strstart(p
, "curses", &opts
)) {
1753 #ifdef CONFIG_CURSES
1754 display
= DT_CURSES
;
1756 fprintf(stderr
, "Curses support is disabled\n");
1759 } else if (strstart(p
, "none", &opts
)) {
1762 fprintf(stderr
, "Unknown display type: %s\n", p
);
1769 static int balloon_parse(const char *arg
)
1773 if (strcmp(arg
, "none") == 0) {
1777 if (!strncmp(arg
, "virtio", 6)) {
1778 if (arg
[6] == ',') {
1779 /* have params -> parse them */
1780 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1784 /* create empty opts */
1785 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1787 qemu_opt_set(opts
, "driver", "virtio-balloon");
1794 char *qemu_find_file(int type
, const char *name
)
1800 /* If name contains path separators then try it as a straight path. */
1801 if ((strchr(name
, '/') || strchr(name
, '\\'))
1802 && access(name
, R_OK
) == 0) {
1803 return g_strdup(name
);
1806 case QEMU_FILE_TYPE_BIOS
:
1809 case QEMU_FILE_TYPE_KEYMAP
:
1810 subdir
= "keymaps/";
1815 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1816 buf
= g_malloc0(len
);
1817 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1818 if (access(buf
, R_OK
)) {
1825 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1827 return qdev_device_help(opts
);
1830 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1834 dev
= qdev_device_add(opts
);
1840 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1842 CharDriverState
*chr
;
1844 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1850 #ifdef CONFIG_VIRTFS
1851 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1854 ret
= qemu_fsdev_add(opts
);
1860 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1862 CharDriverState
*chr
;
1863 const char *chardev
;
1867 mode
= qemu_opt_get(opts
, "mode");
1871 if (strcmp(mode
, "readline") == 0) {
1872 flags
= MONITOR_USE_READLINE
;
1873 } else if (strcmp(mode
, "control") == 0) {
1874 flags
= MONITOR_USE_CONTROL
;
1876 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1880 if (qemu_opt_get_bool(opts
, "pretty", 0))
1881 flags
|= MONITOR_USE_PRETTY
;
1883 if (qemu_opt_get_bool(opts
, "default", 0))
1884 flags
|= MONITOR_IS_DEFAULT
;
1886 chardev
= qemu_opt_get(opts
, "chardev");
1887 chr
= qemu_chr_find(chardev
);
1889 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1893 monitor_init(chr
, flags
);
1897 static void monitor_parse(const char *optarg
, const char *mode
)
1899 static int monitor_device_index
= 0;
1905 if (strstart(optarg
, "chardev:", &p
)) {
1906 snprintf(label
, sizeof(label
), "%s", p
);
1908 snprintf(label
, sizeof(label
), "compat_monitor%d",
1909 monitor_device_index
);
1910 if (monitor_device_index
== 0) {
1913 opts
= qemu_chr_parse_compat(label
, optarg
);
1915 fprintf(stderr
, "parse error: %s\n", optarg
);
1920 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1922 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1925 qemu_opt_set(opts
, "mode", mode
);
1926 qemu_opt_set(opts
, "chardev", label
);
1928 qemu_opt_set(opts
, "default", "on");
1929 monitor_device_index
++;
1932 struct device_config
{
1934 DEV_USB
, /* -usbdevice */
1936 DEV_SERIAL
, /* -serial */
1937 DEV_PARALLEL
, /* -parallel */
1938 DEV_VIRTCON
, /* -virtioconsole */
1939 DEV_DEBUGCON
, /* -debugcon */
1941 const char *cmdline
;
1942 QTAILQ_ENTRY(device_config
) next
;
1944 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1946 static void add_device_config(int type
, const char *cmdline
)
1948 struct device_config
*conf
;
1950 conf
= g_malloc0(sizeof(*conf
));
1952 conf
->cmdline
= cmdline
;
1953 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1956 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1958 struct device_config
*conf
;
1961 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1962 if (conf
->type
!= type
)
1964 rc
= func(conf
->cmdline
);
1971 static int serial_parse(const char *devname
)
1973 static int index
= 0;
1976 if (strcmp(devname
, "none") == 0)
1978 if (index
== MAX_SERIAL_PORTS
) {
1979 fprintf(stderr
, "qemu: too many serial ports\n");
1982 snprintf(label
, sizeof(label
), "serial%d", index
);
1983 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1984 if (!serial_hds
[index
]) {
1985 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1986 devname
, strerror(errno
));
1993 static int parallel_parse(const char *devname
)
1995 static int index
= 0;
1998 if (strcmp(devname
, "none") == 0)
2000 if (index
== MAX_PARALLEL_PORTS
) {
2001 fprintf(stderr
, "qemu: too many parallel ports\n");
2004 snprintf(label
, sizeof(label
), "parallel%d", index
);
2005 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2006 if (!parallel_hds
[index
]) {
2007 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2008 devname
, strerror(errno
));
2015 static int virtcon_parse(const char *devname
)
2017 QemuOptsList
*device
= qemu_find_opts("device");
2018 static int index
= 0;
2020 QemuOpts
*bus_opts
, *dev_opts
;
2022 if (strcmp(devname
, "none") == 0)
2024 if (index
== MAX_VIRTIO_CONSOLES
) {
2025 fprintf(stderr
, "qemu: too many virtio consoles\n");
2029 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2030 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2032 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2033 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2035 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2036 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2037 if (!virtcon_hds
[index
]) {
2038 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2039 devname
, strerror(errno
));
2042 qemu_opt_set(dev_opts
, "chardev", label
);
2048 static int debugcon_parse(const char *devname
)
2052 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2055 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2057 fprintf(stderr
, "qemu: already have a debugcon device\n");
2060 qemu_opt_set(opts
, "driver", "isa-debugcon");
2061 qemu_opt_set(opts
, "chardev", "debugcon");
2065 static QEMUMachine
*machine_parse(const char *name
)
2067 QEMUMachine
*m
, *machine
= NULL
;
2070 machine
= find_machine(name
);
2075 printf("Supported machines are:\n");
2076 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2078 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2080 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2081 m
->is_default
? " (default)" : "");
2083 exit(!name
|| *name
!= '?');
2086 static int tcg_init(void)
2088 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2093 const char *opt_name
;
2095 int (*available
)(void);
2099 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2100 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2101 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2104 static int configure_accelerator(void)
2106 const char *p
= NULL
;
2109 bool accel_initalised
= 0;
2110 bool init_failed
= 0;
2112 QemuOptsList
*list
= qemu_find_opts("machine");
2113 if (!QTAILQ_EMPTY(&list
->head
)) {
2114 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2118 /* Use the default "accelerator", tcg */
2122 while (!accel_initalised
&& *p
!= '\0') {
2126 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2127 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2128 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2129 *(accel_list
[i
].allowed
) = 1;
2130 ret
= accel_list
[i
].init();
2133 if (!accel_list
[i
].available()) {
2134 printf("%s not supported for this target\n",
2135 accel_list
[i
].name
);
2137 fprintf(stderr
, "failed to initialize %s: %s\n",
2141 *(accel_list
[i
].allowed
) = 0;
2143 accel_initalised
= 1;
2148 if (i
== ARRAY_SIZE(accel_list
)) {
2149 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2153 if (!accel_initalised
) {
2154 fprintf(stderr
, "No accelerator found!\n");
2159 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2162 return !accel_initalised
;
2165 void qemu_add_exit_notifier(Notifier
*notify
)
2167 notifier_list_add(&exit_notifiers
, notify
);
2170 void qemu_remove_exit_notifier(Notifier
*notify
)
2172 notifier_list_remove(&exit_notifiers
, notify
);
2175 static void qemu_run_exit_notifiers(void)
2177 notifier_list_notify(&exit_notifiers
, NULL
);
2180 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2182 notifier_list_add(&machine_init_done_notifiers
, notify
);
2185 static void qemu_run_machine_init_done_notifiers(void)
2187 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2190 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2191 const char **poptarg
, int *poptind
)
2193 const QEMUOption
*popt
;
2194 int optind
= *poptind
;
2195 char *r
= argv
[optind
];
2198 loc_set_cmdline(argv
, optind
, 1);
2200 /* Treat --foo the same as -foo. */
2203 popt
= qemu_options
;
2206 error_report("invalid option");
2209 if (!strcmp(popt
->name
, r
+ 1))
2213 if (popt
->flags
& HAS_ARG
) {
2214 if (optind
>= argc
) {
2215 error_report("requires an argument");
2218 optarg
= argv
[optind
++];
2219 loc_set_cmdline(argv
, optind
- 2, 2);
2230 static gpointer
malloc_and_trace(gsize n_bytes
)
2232 void *ptr
= malloc(n_bytes
);
2233 trace_g_malloc(n_bytes
, ptr
);
2237 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2239 void *ptr
= realloc(mem
, n_bytes
);
2240 trace_g_realloc(mem
, n_bytes
, ptr
);
2244 static void free_and_trace(gpointer mem
)
2250 int main(int argc
, char **argv
, char **envp
)
2252 const char *gdbstub_dev
= NULL
;
2254 int snapshot
, linux_boot
;
2255 const char *icount_option
= NULL
;
2256 const char *initrd_filename
;
2257 const char *kernel_filename
, *kernel_cmdline
;
2258 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2260 DisplayChangeListener
*dcl
;
2261 int cyls
, heads
, secs
, translation
;
2262 QemuOpts
*hda_opts
= NULL
, *opts
;
2263 QemuOptsList
*olist
;
2266 const char *loadvm
= NULL
;
2267 QEMUMachine
*machine
;
2268 const char *cpu_model
;
2269 const char *pid_file
= NULL
;
2270 const char *incoming
= NULL
;
2272 int show_vnc_port
= 0;
2275 const char *log_mask
= NULL
;
2276 const char *log_file
= NULL
;
2277 GMemVTable mem_trace
= {
2278 .malloc
= malloc_and_trace
,
2279 .realloc
= realloc_and_trace
,
2280 .free
= free_and_trace
,
2282 const char *trace_events
= NULL
;
2283 const char *trace_file
= NULL
;
2285 atexit(qemu_run_exit_notifiers
);
2286 error_set_progname(argv
[0]);
2288 g_mem_set_vtable(&mem_trace
);
2289 g_thread_init(NULL
);
2295 qemu_cache_utils_init(envp
);
2297 QLIST_INIT (&vm_change_state_head
);
2298 os_setup_early_signal_handling();
2300 module_call_init(MODULE_INIT_MACHINE
);
2301 machine
= find_default_machine();
2303 initrd_filename
= NULL
;
2306 kernel_filename
= NULL
;
2307 kernel_cmdline
= "";
2308 cyls
= heads
= secs
= 0;
2309 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2311 for (i
= 0; i
< MAX_NODES
; i
++) {
2313 node_cpumask
[i
] = 0;
2321 /* first pass of option parsing */
2323 while (optind
< argc
) {
2324 if (argv
[optind
][0] != '-') {
2329 const QEMUOption
*popt
;
2331 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2332 switch (popt
->index
) {
2333 case QEMU_OPTION_nodefconfig
:
2343 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2344 if (ret
< 0 && ret
!= -ENOENT
) {
2348 ret
= qemu_read_config_file(arch_config_name
);
2349 if (ret
< 0 && ret
!= -ENOENT
) {
2355 /* second pass of option parsing */
2360 if (argv
[optind
][0] != '-') {
2361 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2363 const QEMUOption
*popt
;
2365 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2366 if (!(popt
->arch_mask
& arch_type
)) {
2367 printf("Option %s not supported for this target\n", popt
->name
);
2370 switch(popt
->index
) {
2372 machine
= machine_parse(optarg
);
2374 case QEMU_OPTION_cpu
:
2375 /* hw initialization will check this */
2376 if (*optarg
== '?') {
2377 list_cpus(stdout
, &fprintf
, optarg
);
2383 case QEMU_OPTION_initrd
:
2384 initrd_filename
= optarg
;
2386 case QEMU_OPTION_hda
:
2390 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2392 snprintf(buf
, sizeof(buf
),
2393 "%s,cyls=%d,heads=%d,secs=%d%s",
2394 HD_OPTS
, cyls
, heads
, secs
,
2395 translation
== BIOS_ATA_TRANSLATION_LBA
?
2397 translation
== BIOS_ATA_TRANSLATION_NONE
?
2398 ",trans=none" : "");
2399 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2402 case QEMU_OPTION_hdb
:
2403 case QEMU_OPTION_hdc
:
2404 case QEMU_OPTION_hdd
:
2405 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2408 case QEMU_OPTION_drive
:
2409 if (drive_def(optarg
) == NULL
) {
2413 case QEMU_OPTION_set
:
2414 if (qemu_set_option(optarg
) != 0)
2417 case QEMU_OPTION_global
:
2418 if (qemu_global_option(optarg
) != 0)
2421 case QEMU_OPTION_mtdblock
:
2422 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2424 case QEMU_OPTION_sd
:
2425 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2427 case QEMU_OPTION_pflash
:
2428 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2430 case QEMU_OPTION_snapshot
:
2433 case QEMU_OPTION_hdachs
:
2437 cyls
= strtol(p
, (char **)&p
, 0);
2438 if (cyls
< 1 || cyls
> 16383)
2443 heads
= strtol(p
, (char **)&p
, 0);
2444 if (heads
< 1 || heads
> 16)
2449 secs
= strtol(p
, (char **)&p
, 0);
2450 if (secs
< 1 || secs
> 63)
2454 if (!strcmp(p
, "none"))
2455 translation
= BIOS_ATA_TRANSLATION_NONE
;
2456 else if (!strcmp(p
, "lba"))
2457 translation
= BIOS_ATA_TRANSLATION_LBA
;
2458 else if (!strcmp(p
, "auto"))
2459 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2462 } else if (*p
!= '\0') {
2464 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2467 if (hda_opts
!= NULL
) {
2469 snprintf(num
, sizeof(num
), "%d", cyls
);
2470 qemu_opt_set(hda_opts
, "cyls", num
);
2471 snprintf(num
, sizeof(num
), "%d", heads
);
2472 qemu_opt_set(hda_opts
, "heads", num
);
2473 snprintf(num
, sizeof(num
), "%d", secs
);
2474 qemu_opt_set(hda_opts
, "secs", num
);
2475 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2476 qemu_opt_set(hda_opts
, "trans", "lba");
2477 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2478 qemu_opt_set(hda_opts
, "trans", "none");
2482 case QEMU_OPTION_numa
:
2483 if (nb_numa_nodes
>= MAX_NODES
) {
2484 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2489 case QEMU_OPTION_display
:
2490 display_type
= select_display(optarg
);
2492 case QEMU_OPTION_nographic
:
2493 display_type
= DT_NOGRAPHIC
;
2495 case QEMU_OPTION_curses
:
2496 #ifdef CONFIG_CURSES
2497 display_type
= DT_CURSES
;
2499 fprintf(stderr
, "Curses support is disabled\n");
2503 case QEMU_OPTION_portrait
:
2504 graphic_rotate
= 90;
2506 case QEMU_OPTION_rotate
:
2507 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2508 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2509 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2511 "qemu: only 90, 180, 270 deg rotation is available\n");
2515 case QEMU_OPTION_kernel
:
2516 kernel_filename
= optarg
;
2518 case QEMU_OPTION_append
:
2519 kernel_cmdline
= optarg
;
2521 case QEMU_OPTION_cdrom
:
2522 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2524 case QEMU_OPTION_boot
:
2526 static const char * const params
[] = {
2527 "order", "once", "menu",
2528 "splash", "splash-time", NULL
2530 char buf
[sizeof(boot_devices
)];
2531 char *standard_boot_devices
;
2534 if (!strchr(optarg
, '=')) {
2536 pstrcpy(buf
, sizeof(buf
), optarg
);
2537 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2539 "qemu: unknown boot parameter '%s' in '%s'\n",
2545 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2546 validate_bootdevices(buf
);
2547 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2550 if (get_param_value(buf
, sizeof(buf
),
2552 validate_bootdevices(buf
);
2553 standard_boot_devices
= g_strdup(boot_devices
);
2554 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2555 qemu_register_reset(restore_boot_devices
,
2556 standard_boot_devices
);
2558 if (get_param_value(buf
, sizeof(buf
),
2560 if (!strcmp(buf
, "on")) {
2562 } else if (!strcmp(buf
, "off")) {
2566 "qemu: invalid option value '%s'\n",
2571 qemu_opts_parse(qemu_find_opts("boot-opts"),
2576 case QEMU_OPTION_fda
:
2577 case QEMU_OPTION_fdb
:
2578 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2581 case QEMU_OPTION_no_fd_bootchk
:
2584 case QEMU_OPTION_netdev
:
2585 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2589 case QEMU_OPTION_net
:
2590 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2595 case QEMU_OPTION_tftp
:
2596 legacy_tftp_prefix
= optarg
;
2598 case QEMU_OPTION_bootp
:
2599 legacy_bootp_filename
= optarg
;
2601 case QEMU_OPTION_redir
:
2602 if (net_slirp_redir(optarg
) < 0)
2606 case QEMU_OPTION_bt
:
2607 add_device_config(DEV_BT
, optarg
);
2609 case QEMU_OPTION_audio_help
:
2610 if (!(audio_available())) {
2611 printf("Option %s not supported for this target\n", popt
->name
);
2617 case QEMU_OPTION_soundhw
:
2618 if (!(audio_available())) {
2619 printf("Option %s not supported for this target\n", popt
->name
);
2622 select_soundhw (optarg
);
2627 case QEMU_OPTION_version
:
2631 case QEMU_OPTION_m
: {
2634 value
= strtosz(optarg
, NULL
);
2636 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2640 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2641 fprintf(stderr
, "qemu: ram size too large\n");
2647 case QEMU_OPTION_mempath
:
2651 case QEMU_OPTION_mem_prealloc
:
2662 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2664 case QEMU_OPTION_gdb
:
2665 gdbstub_dev
= optarg
;
2670 case QEMU_OPTION_bios
:
2673 case QEMU_OPTION_singlestep
:
2680 keyboard_layout
= optarg
;
2682 case QEMU_OPTION_localtime
:
2685 case QEMU_OPTION_vga
:
2686 select_vgahw (optarg
);
2693 w
= strtol(p
, (char **)&p
, 10);
2696 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2702 h
= strtol(p
, (char **)&p
, 10);
2707 depth
= strtol(p
, (char **)&p
, 10);
2708 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2709 depth
!= 24 && depth
!= 32)
2711 } else if (*p
== '\0') {
2712 depth
= graphic_depth
;
2719 graphic_depth
= depth
;
2722 case QEMU_OPTION_echr
:
2725 term_escape_char
= strtol(optarg
, &r
, 0);
2727 printf("Bad argument to echr\n");
2730 case QEMU_OPTION_monitor
:
2731 monitor_parse(optarg
, "readline");
2732 default_monitor
= 0;
2734 case QEMU_OPTION_qmp
:
2735 monitor_parse(optarg
, "control");
2736 default_monitor
= 0;
2738 case QEMU_OPTION_mon
:
2739 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2743 default_monitor
= 0;
2745 case QEMU_OPTION_chardev
:
2746 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2751 case QEMU_OPTION_fsdev
:
2752 olist
= qemu_find_opts("fsdev");
2754 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2757 opts
= qemu_opts_parse(olist
, optarg
, 1);
2759 fprintf(stderr
, "parse error: %s\n", optarg
);
2763 case QEMU_OPTION_virtfs
: {
2767 olist
= qemu_find_opts("virtfs");
2769 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2772 opts
= qemu_opts_parse(olist
, optarg
, 1);
2774 fprintf(stderr
, "parse error: %s\n", optarg
);
2778 if (qemu_opt_get(opts
, "fstype") == NULL
||
2779 qemu_opt_get(opts
, "mount_tag") == NULL
||
2780 qemu_opt_get(opts
, "path") == NULL
||
2781 qemu_opt_get(opts
, "security_model") == NULL
) {
2782 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2783 "security_model=[mapped|passthrough|none],"
2784 "mount_tag=tag.\n");
2788 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2789 qemu_opt_get(opts
, "mount_tag"), 1);
2791 fprintf(stderr
, "duplicate fsdev id: %s\n",
2792 qemu_opt_get(opts
, "mount_tag"));
2795 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2796 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2797 qemu_opt_set(fsdev
, "security_model",
2798 qemu_opt_get(opts
, "security_model"));
2800 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2801 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2802 qemu_opt_set(device
, "fsdev",
2803 qemu_opt_get(opts
, "mount_tag"));
2804 qemu_opt_set(device
, "mount_tag",
2805 qemu_opt_get(opts
, "mount_tag"));
2808 case QEMU_OPTION_serial
:
2809 add_device_config(DEV_SERIAL
, optarg
);
2811 if (strncmp(optarg
, "mon:", 4) == 0) {
2812 default_monitor
= 0;
2815 case QEMU_OPTION_watchdog
:
2818 "qemu: only one watchdog option may be given\n");
2823 case QEMU_OPTION_watchdog_action
:
2824 if (select_watchdog_action(optarg
) == -1) {
2825 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2829 case QEMU_OPTION_virtiocon
:
2830 add_device_config(DEV_VIRTCON
, optarg
);
2831 default_virtcon
= 0;
2832 if (strncmp(optarg
, "mon:", 4) == 0) {
2833 default_monitor
= 0;
2836 case QEMU_OPTION_parallel
:
2837 add_device_config(DEV_PARALLEL
, optarg
);
2838 default_parallel
= 0;
2839 if (strncmp(optarg
, "mon:", 4) == 0) {
2840 default_monitor
= 0;
2843 case QEMU_OPTION_debugcon
:
2844 add_device_config(DEV_DEBUGCON
, optarg
);
2846 case QEMU_OPTION_loadvm
:
2849 case QEMU_OPTION_full_screen
:
2853 case QEMU_OPTION_no_frame
:
2856 case QEMU_OPTION_alt_grab
:
2859 case QEMU_OPTION_ctrl_grab
:
2862 case QEMU_OPTION_no_quit
:
2865 case QEMU_OPTION_sdl
:
2866 display_type
= DT_SDL
;
2869 case QEMU_OPTION_no_frame
:
2870 case QEMU_OPTION_alt_grab
:
2871 case QEMU_OPTION_ctrl_grab
:
2872 case QEMU_OPTION_no_quit
:
2873 case QEMU_OPTION_sdl
:
2874 fprintf(stderr
, "SDL support is disabled\n");
2877 case QEMU_OPTION_pidfile
:
2880 case QEMU_OPTION_win2k_hack
:
2881 win2k_install_hack
= 1;
2883 case QEMU_OPTION_rtc_td_hack
:
2886 case QEMU_OPTION_acpitable
:
2887 do_acpitable_option(optarg
);
2889 case QEMU_OPTION_smbios
:
2890 do_smbios_option(optarg
);
2892 case QEMU_OPTION_enable_kvm
:
2893 olist
= qemu_find_opts("machine");
2894 qemu_opts_reset(olist
);
2895 qemu_opts_parse(olist
, "accel=kvm", 0);
2897 case QEMU_OPTION_machine
:
2898 olist
= qemu_find_opts("machine");
2899 qemu_opts_reset(olist
);
2900 opts
= qemu_opts_parse(olist
, optarg
, 1);
2902 fprintf(stderr
, "parse error: %s\n", optarg
);
2905 optarg
= qemu_opt_get(opts
, "type");
2907 machine
= machine_parse(optarg
);
2910 case QEMU_OPTION_usb
:
2913 case QEMU_OPTION_usbdevice
:
2915 add_device_config(DEV_USB
, optarg
);
2917 case QEMU_OPTION_device
:
2918 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2922 case QEMU_OPTION_smp
:
2925 fprintf(stderr
, "Invalid number of CPUs\n");
2928 if (max_cpus
< smp_cpus
) {
2929 fprintf(stderr
, "maxcpus must be equal to or greater than "
2933 if (max_cpus
> 255) {
2934 fprintf(stderr
, "Unsupported number of maxcpus\n");
2938 case QEMU_OPTION_vnc
:
2941 vnc_display
= optarg
;
2943 fprintf(stderr
, "VNC support is disabled\n");
2947 case QEMU_OPTION_no_acpi
:
2950 case QEMU_OPTION_no_hpet
:
2953 case QEMU_OPTION_balloon
:
2954 if (balloon_parse(optarg
) < 0) {
2955 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2959 case QEMU_OPTION_no_reboot
:
2962 case QEMU_OPTION_no_shutdown
:
2965 case QEMU_OPTION_show_cursor
:
2968 case QEMU_OPTION_uuid
:
2969 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2970 fprintf(stderr
, "Fail to parse UUID string."
2971 " Wrong format.\n");
2975 case QEMU_OPTION_option_rom
:
2976 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2977 fprintf(stderr
, "Too many option ROMs\n");
2980 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2981 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2982 option_rom
[nb_option_roms
].bootindex
=
2983 qemu_opt_get_number(opts
, "bootindex", -1);
2984 if (!option_rom
[nb_option_roms
].name
) {
2985 fprintf(stderr
, "Option ROM file is not specified\n");
2990 case QEMU_OPTION_semihosting
:
2991 semihosting_enabled
= 1;
2993 case QEMU_OPTION_name
:
2994 qemu_name
= g_strdup(optarg
);
2996 char *p
= strchr(qemu_name
, ',');
2999 if (strncmp(p
, "process=", 8)) {
3000 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3004 os_set_proc_name(p
);
3008 case QEMU_OPTION_prom_env
:
3009 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3010 fprintf(stderr
, "Too many prom variables\n");
3013 prom_envs
[nb_prom_envs
] = optarg
;
3016 case QEMU_OPTION_old_param
:
3019 case QEMU_OPTION_clock
:
3020 configure_alarms(optarg
);
3022 case QEMU_OPTION_startdate
:
3023 configure_rtc_date_offset(optarg
, 1);
3025 case QEMU_OPTION_rtc
:
3026 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3030 configure_rtc(opts
);
3032 case QEMU_OPTION_tb_size
:
3033 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3034 if (tcg_tb_size
< 0) {
3038 case QEMU_OPTION_icount
:
3039 icount_option
= optarg
;
3041 case QEMU_OPTION_incoming
:
3044 case QEMU_OPTION_nodefaults
:
3046 default_parallel
= 0;
3047 default_virtcon
= 0;
3048 default_monitor
= 0;
3055 case QEMU_OPTION_xen_domid
:
3056 if (!(xen_available())) {
3057 printf("Option %s not supported for this target\n", popt
->name
);
3060 xen_domid
= atoi(optarg
);
3062 case QEMU_OPTION_xen_create
:
3063 if (!(xen_available())) {
3064 printf("Option %s not supported for this target\n", popt
->name
);
3067 xen_mode
= XEN_CREATE
;
3069 case QEMU_OPTION_xen_attach
:
3070 if (!(xen_available())) {
3071 printf("Option %s not supported for this target\n", popt
->name
);
3074 xen_mode
= XEN_ATTACH
;
3076 case QEMU_OPTION_trace
:
3078 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3082 trace_events
= qemu_opt_get(opts
, "events");
3083 trace_file
= qemu_opt_get(opts
, "file");
3086 case QEMU_OPTION_readconfig
:
3088 int ret
= qemu_read_config_file(optarg
);
3090 fprintf(stderr
, "read config %s: %s\n", optarg
,
3096 case QEMU_OPTION_spice
:
3097 olist
= qemu_find_opts("spice");
3099 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3102 opts
= qemu_opts_parse(olist
, optarg
, 0);
3104 fprintf(stderr
, "parse error: %s\n", optarg
);
3108 case QEMU_OPTION_writeconfig
:
3111 if (strcmp(optarg
, "-") == 0) {
3114 fp
= fopen(optarg
, "w");
3116 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3120 qemu_config_write(fp
);
3125 os_parse_cmd_args(popt
->index
, optarg
);
3131 /* Open the logfile at this point, if necessary. We can't open the logfile
3132 * when encountering either of the logging options (-d or -D) because the
3133 * other one may be encountered later on the command line, changing the
3134 * location or level of logging.
3138 set_cpu_log_filename(log_file
);
3140 set_cpu_log(log_mask
);
3143 if (!trace_backend_init(trace_events
, trace_file
)) {
3147 /* If no data_dir is specified then try to find it relative to the
3150 data_dir
= os_find_datadir(argv
[0]);
3152 /* If all else fails use the install path specified when building. */
3154 data_dir
= CONFIG_QEMU_DATADIR
;
3158 * Default to max_cpus = smp_cpus, in case the user doesn't
3159 * specify a max_cpus value.
3162 max_cpus
= smp_cpus
;
3164 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3165 if (smp_cpus
> machine
->max_cpus
) {
3166 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3167 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3173 * Get the default machine options from the machine if it is not already
3174 * specified either by the configuration file or by the command line.
3176 if (machine
->default_machine_opts
) {
3177 QemuOptsList
*list
= qemu_find_opts("machine");
3178 const char *p
= NULL
;
3180 if (!QTAILQ_EMPTY(&list
->head
)) {
3181 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3184 qemu_opts_reset(list
);
3185 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3187 fprintf(stderr
, "parse error for machine %s: %s\n",
3188 machine
->name
, machine
->default_machine_opts
);
3194 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3195 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3197 if (machine
->no_serial
) {
3200 if (machine
->no_parallel
) {
3201 default_parallel
= 0;
3203 if (!machine
->use_virtcon
) {
3204 default_virtcon
= 0;
3206 if (machine
->no_vga
) {
3209 if (machine
->no_floppy
) {
3212 if (machine
->no_cdrom
) {
3215 if (machine
->no_sdcard
) {
3219 if (display_type
== DT_NOGRAPHIC
) {
3220 if (default_parallel
)
3221 add_device_config(DEV_PARALLEL
, "null");
3222 if (default_serial
&& default_monitor
) {
3223 add_device_config(DEV_SERIAL
, "mon:stdio");
3224 } else if (default_virtcon
&& default_monitor
) {
3225 add_device_config(DEV_VIRTCON
, "mon:stdio");
3228 add_device_config(DEV_SERIAL
, "stdio");
3229 if (default_virtcon
)
3230 add_device_config(DEV_VIRTCON
, "stdio");
3231 if (default_monitor
)
3232 monitor_parse("stdio", "readline");
3236 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3237 if (default_parallel
)
3238 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3239 if (default_monitor
)
3240 monitor_parse("vc:80Cx24C", "readline");
3241 if (default_virtcon
)
3242 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3245 vga_interface_type
= VGA_CIRRUS
;
3249 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3251 #ifdef CONFIG_VIRTFS
3252 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3259 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3264 /* init the memory */
3265 if (ram_size
== 0) {
3266 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3269 configure_accelerator();
3271 if (qemu_init_main_loop()) {
3272 fprintf(stderr
, "qemu_init_main_loop failed\n");
3275 linux_boot
= (kernel_filename
!= NULL
);
3277 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3278 fprintf(stderr
, "-append only allowed with -kernel option\n");
3282 if (!linux_boot
&& initrd_filename
!= NULL
) {
3283 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3287 os_set_line_buffering();
3289 if (init_timer_alarm() < 0) {
3290 fprintf(stderr
, "could not initialize alarm timer\n");
3293 configure_icount(icount_option
);
3295 if (net_init_clients() < 0) {
3299 /* init the bluetooth world */
3300 if (foreach_device_config(DEV_BT
, bt_parse
))
3303 if (!xen_enabled()) {
3304 /* On 32-bit hosts, QEMU is limited by virtual address space */
3305 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3306 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3311 cpu_exec_init_all();
3313 bdrv_init_with_whitelist();
3317 /* open the virtual block devices */
3319 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3320 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3323 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3324 IF_DEFAULT
, 2, CDROM_OPTS
);
3325 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3326 IF_FLOPPY
, 0, FD_OPTS
);
3327 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3330 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3333 if (nb_numa_nodes
> 0) {
3336 if (nb_numa_nodes
> MAX_NODES
) {
3337 nb_numa_nodes
= MAX_NODES
;
3340 /* If no memory size if given for any node, assume the default case
3341 * and distribute the available memory equally across all nodes
3343 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3344 if (node_mem
[i
] != 0)
3347 if (i
== nb_numa_nodes
) {
3348 uint64_t usedmem
= 0;
3350 /* On Linux, the each node's border has to be 8MB aligned,
3351 * the final node gets the rest.
3353 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3354 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3355 usedmem
+= node_mem
[i
];
3357 node_mem
[i
] = ram_size
- usedmem
;
3360 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3361 if (node_cpumask
[i
] != 0)
3364 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3365 * must cope with this anyway, because there are BIOSes out there in
3366 * real machines which also use this scheme.
3368 if (i
== nb_numa_nodes
) {
3369 for (i
= 0; i
< smp_cpus
; i
++) {
3370 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3375 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3379 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3381 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3383 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3385 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3388 module_call_init(MODULE_INIT_DEVICE
);
3390 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3394 i
= select_watchdog(watchdog
);
3396 exit (i
== 1 ? 1 : 0);
3399 if (machine
->compat_props
) {
3400 qdev_prop_register_global_list(machine
->compat_props
);
3404 machine
->init(ram_size
, boot_devices
,
3405 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3407 cpu_synchronize_all_post_init();
3411 current_machine
= machine
;
3413 /* init USB devices */
3415 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3419 /* init generic devices */
3420 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3423 net_check_clients();
3425 /* just use the first displaystate for the moment */
3426 ds
= get_displaystate();
3430 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3431 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3432 display_type
= DT_SDL
;
3433 #elif defined(CONFIG_VNC)
3434 vnc_display
= "localhost:0,to=99";
3437 display_type
= DT_NONE
;
3442 /* init local displays */
3443 switch (display_type
) {
3446 #if defined(CONFIG_CURSES)
3448 curses_display_init(ds
, full_screen
);
3451 #if defined(CONFIG_SDL)
3453 sdl_display_init(ds
, full_screen
, no_frame
);
3455 #elif defined(CONFIG_COCOA)
3457 cocoa_display_init(ds
, full_screen
);
3464 /* must be after terminal init, SDL library changes signal handlers */
3465 os_setup_signal_handling();
3468 /* init remote displays */
3470 vnc_display_init(ds
);
3471 if (vnc_display_open(ds
, vnc_display
) < 0)
3474 if (show_vnc_port
) {
3475 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3480 if (using_spice
&& !qxl_enabled
) {
3481 qemu_spice_display_init(ds
);
3487 dcl
= ds
->listeners
;
3488 while (dcl
!= NULL
) {
3489 if (dcl
->dpy_refresh
!= NULL
) {
3490 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3491 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3496 if (ds
->gui_timer
== NULL
) {
3497 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3498 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3500 text_consoles_set_display(ds
);
3502 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3503 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3508 qdev_machine_creation_done();
3510 if (rom_load_all() != 0) {
3511 fprintf(stderr
, "rom loading failed\n");
3515 /* TODO: once all bus devices are qdevified, this should be done
3516 * when bus is created by qdev.c */
3517 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3518 qemu_run_machine_init_done_notifiers();
3520 qemu_system_reset(VMRESET_SILENT
);
3522 if (load_vmstate(loadvm
) < 0) {
3528 runstate_set(RSTATE_IN_MIGRATE
);
3529 int ret
= qemu_start_incoming_migration(incoming
);
3531 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3535 } else if (autostart
) {
3538 runstate_set(RSTATE_PRE_LAUNCH
);