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
];
189 static int rtc_utc
= 1;
190 static int rtc_date_offset
= -1; /* -1 means no change */
191 QEMUClock
*rtc_clock
;
192 int vga_interface_type
= VGA_NONE
;
193 static int full_screen
= 0;
195 static int no_frame
= 0;
198 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
199 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
200 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
201 int win2k_install_hack
= 0;
210 const char *vnc_display
;
212 int acpi_enabled
= 1;
218 int graphic_rotate
= 0;
219 uint8_t irq0override
= 1;
220 const char *watchdog
;
221 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
225 const char *qemu_name
;
228 unsigned int nb_prom_envs
= 0;
229 const char *prom_envs
[MAX_PROM_ENVS
];
231 uint8_t *boot_splash_filedata
;
232 int boot_splash_filedata_size
;
233 uint8_t qemu_extra_params_fw
[2];
235 typedef struct FWBootEntry FWBootEntry
;
238 QTAILQ_ENTRY(FWBootEntry
) link
;
244 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
247 uint64_t node_mem
[MAX_NODES
];
248 uint64_t node_cpumask
[MAX_NODES
];
250 static QEMUTimer
*nographic_timer
;
252 uint8_t qemu_uuid
[16];
254 static QEMUBootSetHandler
*boot_set_handler
;
255 static void *boot_set_opaque
;
257 static NotifierList exit_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
260 static NotifierList machine_init_done_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
263 static int tcg_allowed
= 1;
267 enum xen_mode xen_mode
= XEN_EMULATE
;
268 static int tcg_tb_size
;
270 static int default_serial
= 1;
271 static int default_parallel
= 1;
272 static int default_virtcon
= 1;
273 static int default_monitor
= 1;
274 static int default_vga
= 1;
275 static int default_floppy
= 1;
276 static int default_cdrom
= 1;
277 static int default_sdcard
= 1;
283 { .driver
= "isa-serial", .flag
= &default_serial
},
284 { .driver
= "isa-parallel", .flag
= &default_parallel
},
285 { .driver
= "isa-fdc", .flag
= &default_floppy
},
286 { .driver
= "ide-cd", .flag
= &default_cdrom
},
287 { .driver
= "ide-hd", .flag
= &default_cdrom
},
288 { .driver
= "ide-drive", .flag
= &default_cdrom
},
289 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
290 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "cirrus-vga", .flag
= &default_vga
},
295 { .driver
= "vmware-svga", .flag
= &default_vga
},
296 { .driver
= "isa-vga", .flag
= &default_vga
},
297 { .driver
= "qxl-vga", .flag
= &default_vga
},
300 static void res_free(void)
302 if (boot_splash_filedata
!= NULL
) {
303 g_free(boot_splash_filedata
);
304 boot_splash_filedata
= NULL
;
308 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
310 const char *driver
= qemu_opt_get(opts
, "driver");
315 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
316 if (strcmp(default_list
[i
].driver
, driver
) != 0)
318 *(default_list
[i
].flag
) = 0;
323 /***********************************************************/
326 static RunState current_run_state
= RSTATE_NO_STATE
;
331 } RunStateTransition
;
333 static const RunStateTransition runstate_transitions_def
[] = {
335 { RSTATE_NO_STATE
, RSTATE_RUNNING
},
336 { RSTATE_NO_STATE
, RSTATE_IN_MIGRATE
},
337 { RSTATE_NO_STATE
, RSTATE_PRE_LAUNCH
},
339 { RSTATE_DEBUG
, RSTATE_RUNNING
},
341 { RSTATE_IN_MIGRATE
, RSTATE_RUNNING
},
342 { RSTATE_IN_MIGRATE
, RSTATE_PRE_LAUNCH
},
344 { RSTATE_PANICKED
, RSTATE_PAUSED
},
346 { RSTATE_IO_ERROR
, RSTATE_RUNNING
},
348 { RSTATE_PAUSED
, RSTATE_RUNNING
},
350 { RSTATE_POST_MIGRATE
, RSTATE_RUNNING
},
352 { RSTATE_PRE_LAUNCH
, RSTATE_RUNNING
},
353 { RSTATE_PRE_LAUNCH
, RSTATE_POST_MIGRATE
},
355 { RSTATE_PRE_MIGRATE
, RSTATE_RUNNING
},
356 { RSTATE_PRE_MIGRATE
, RSTATE_POST_MIGRATE
},
358 { RSTATE_RESTORE
, RSTATE_RUNNING
},
360 { RSTATE_RUNNING
, RSTATE_DEBUG
},
361 { RSTATE_RUNNING
, RSTATE_PANICKED
},
362 { RSTATE_RUNNING
, RSTATE_IO_ERROR
},
363 { RSTATE_RUNNING
, RSTATE_PAUSED
},
364 { RSTATE_RUNNING
, RSTATE_PRE_MIGRATE
},
365 { RSTATE_RUNNING
, RSTATE_RESTORE
},
366 { RSTATE_RUNNING
, RSTATE_SAVEVM
},
367 { RSTATE_RUNNING
, RSTATE_SHUTDOWN
},
368 { RSTATE_RUNNING
, RSTATE_WATCHDOG
},
370 { RSTATE_SAVEVM
, RSTATE_RUNNING
},
372 { RSTATE_SHUTDOWN
, RSTATE_PAUSED
},
374 { RSTATE_WATCHDOG
, RSTATE_RUNNING
},
376 { RSTATE_MAX
, RSTATE_MAX
},
379 static bool runstate_valid_transitions
[RSTATE_MAX
][RSTATE_MAX
];
381 bool runstate_check(RunState state
)
383 return current_run_state
== state
;
386 void runstate_init(void)
388 const RunStateTransition
*p
;
390 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
392 for (p
= &runstate_transitions_def
[0]; p
->from
!= RSTATE_MAX
; p
++) {
393 runstate_valid_transitions
[p
->from
][p
->to
] = true;
397 /* This function will abort() on invalid state transitions */
398 void runstate_set(RunState new_state
)
400 if (new_state
>= RSTATE_MAX
||
401 !runstate_valid_transitions
[current_run_state
][new_state
]) {
402 fprintf(stderr
, "invalid runstate transition\n");
406 current_run_state
= new_state
;
409 int runstate_is_running(void)
411 return runstate_check(RSTATE_RUNNING
);
414 /***********************************************************/
415 /* real time host monotonic timer */
417 /***********************************************************/
418 /* host time/date access */
419 void qemu_get_timedate(struct tm
*tm
, int offset
)
426 if (rtc_date_offset
== -1) {
430 ret
= localtime(&ti
);
432 ti
-= rtc_date_offset
;
436 memcpy(tm
, ret
, sizeof(struct tm
));
439 int qemu_timedate_diff(struct tm
*tm
)
443 if (rtc_date_offset
== -1)
445 seconds
= mktimegm(tm
);
447 seconds
= mktime(tm
);
449 seconds
= mktimegm(tm
) + rtc_date_offset
;
451 return seconds
- time(NULL
);
454 void rtc_change_mon_event(struct tm
*tm
)
458 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
459 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
460 qobject_decref(data
);
463 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
465 time_t rtc_start_date
;
468 if (!strcmp(startdate
, "now") && legacy
) {
469 rtc_date_offset
= -1;
471 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
479 } else if (sscanf(startdate
, "%d-%d-%d",
491 rtc_start_date
= mktimegm(&tm
);
492 if (rtc_start_date
== -1) {
494 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
495 "'2006-06-17T16:01:21' or '2006-06-17'\n");
498 rtc_date_offset
= time(NULL
) - rtc_start_date
;
502 static void configure_rtc(QemuOpts
*opts
)
506 value
= qemu_opt_get(opts
, "base");
508 if (!strcmp(value
, "utc")) {
510 } else if (!strcmp(value
, "localtime")) {
513 configure_rtc_date_offset(value
, 0);
516 value
= qemu_opt_get(opts
, "clock");
518 if (!strcmp(value
, "host")) {
519 rtc_clock
= host_clock
;
520 } else if (!strcmp(value
, "vm")) {
521 rtc_clock
= vm_clock
;
523 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
527 value
= qemu_opt_get(opts
, "driftfix");
529 if (!strcmp(value
, "slew")) {
531 } else if (!strcmp(value
, "none")) {
534 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
540 /***********************************************************/
541 /* Bluetooth support */
544 static struct HCIInfo
*hci_table
[MAX_NICS
];
546 static struct bt_vlan_s
{
547 struct bt_scatternet_s net
;
549 struct bt_vlan_s
*next
;
552 /* find or alloc a new bluetooth "VLAN" */
553 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
555 struct bt_vlan_s
**pvlan
, *vlan
;
556 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
560 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
562 pvlan
= &first_bt_vlan
;
563 while (*pvlan
!= NULL
)
564 pvlan
= &(*pvlan
)->next
;
569 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
573 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
578 static struct HCIInfo null_hci
= {
579 .cmd_send
= null_hci_send
,
580 .sco_send
= null_hci_send
,
581 .acl_send
= null_hci_send
,
582 .bdaddr_set
= null_hci_addr_set
,
585 struct HCIInfo
*qemu_next_hci(void)
587 if (cur_hci
== nb_hcis
)
590 return hci_table
[cur_hci
++];
593 static struct HCIInfo
*hci_init(const char *str
)
596 struct bt_scatternet_s
*vlan
= 0;
598 if (!strcmp(str
, "null"))
601 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
603 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
604 else if (!strncmp(str
, "hci", 3)) {
607 if (!strncmp(str
+ 3, ",vlan=", 6)) {
608 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
613 vlan
= qemu_find_bt_vlan(0);
615 return bt_new_hci(vlan
);
618 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
623 static int bt_hci_parse(const char *str
)
628 if (nb_hcis
>= MAX_NICS
) {
629 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
642 bdaddr
.b
[5] = 0x56 + nb_hcis
;
643 hci
->bdaddr_set(hci
, bdaddr
.b
);
645 hci_table
[nb_hcis
++] = hci
;
650 static void bt_vhci_add(int vlan_id
)
652 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
655 fprintf(stderr
, "qemu: warning: adding a VHCI to "
656 "an empty scatternet %i\n", vlan_id
);
658 bt_vhci_init(bt_new_hci(vlan
));
661 static struct bt_device_s
*bt_device_add(const char *opt
)
663 struct bt_scatternet_s
*vlan
;
665 char *endp
= strstr(opt
, ",vlan=");
666 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
669 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
672 vlan_id
= strtol(endp
+ 6, &endp
, 0);
674 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
679 vlan
= qemu_find_bt_vlan(vlan_id
);
682 fprintf(stderr
, "qemu: warning: adding a slave device to "
683 "an empty scatternet %i\n", vlan_id
);
685 if (!strcmp(devname
, "keyboard"))
686 return bt_keyboard_init(vlan
);
688 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
692 static int bt_parse(const char *opt
)
694 const char *endp
, *p
;
697 if (strstart(opt
, "hci", &endp
)) {
698 if (!*endp
|| *endp
== ',') {
700 if (!strstart(endp
, ",vlan=", 0))
703 return bt_hci_parse(opt
);
705 } else if (strstart(opt
, "vhci", &endp
)) {
706 if (!*endp
|| *endp
== ',') {
708 if (strstart(endp
, ",vlan=", &p
)) {
709 vlan
= strtol(p
, (char **) &endp
, 0);
711 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
715 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
724 } else if (strstart(opt
, "device:", &endp
))
725 return !bt_device_add(endp
);
727 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
731 /***********************************************************/
732 /* QEMU Block devices */
734 #define HD_OPTS "media=disk"
735 #define CDROM_OPTS "media=cdrom"
737 #define PFLASH_OPTS ""
741 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
743 int *use_scsi
= opaque
;
745 return drive_init(opts
, *use_scsi
) == NULL
;
748 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
750 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
751 qemu_opt_set(opts
, "snapshot", "on");
756 static void default_drive(int enable
, int snapshot
, int use_scsi
,
757 BlockInterfaceType type
, int index
,
762 if (type
== IF_DEFAULT
) {
763 type
= use_scsi
? IF_SCSI
: IF_IDE
;
766 if (!enable
|| drive_get_by_index(type
, index
)) {
770 opts
= drive_add(type
, index
, NULL
, optstr
);
772 drive_enable_snapshot(opts
, NULL
);
774 if (!drive_init(opts
, use_scsi
)) {
779 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
781 boot_set_handler
= func
;
782 boot_set_opaque
= opaque
;
785 int qemu_boot_set(const char *boot_devices
)
787 if (!boot_set_handler
) {
790 return boot_set_handler(boot_set_opaque
, boot_devices
);
793 static void validate_bootdevices(char *devices
)
795 /* We just do some generic consistency checks */
799 for (p
= devices
; *p
!= '\0'; p
++) {
800 /* Allowed boot devices are:
801 * a-b: floppy disk drives
802 * c-f: IDE disk drives
803 * g-m: machine implementation dependant drives
804 * n-p: network devices
805 * It's up to each machine implementation to check if the given boot
806 * devices match the actual hardware implementation and firmware
809 if (*p
< 'a' || *p
> 'p') {
810 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
813 if (bitmap
& (1 << (*p
- 'a'))) {
814 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
817 bitmap
|= 1 << (*p
- 'a');
821 static void restore_boot_devices(void *opaque
)
823 char *standard_boot_devices
= opaque
;
824 static int first
= 1;
826 /* Restore boot order and remove ourselves after the first boot */
832 qemu_boot_set(standard_boot_devices
);
834 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
835 g_free(standard_boot_devices
);
838 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
841 FWBootEntry
*node
, *i
;
847 assert(dev
!= NULL
|| suffix
!= NULL
);
849 node
= g_malloc0(sizeof(FWBootEntry
));
850 node
->bootindex
= bootindex
;
851 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
854 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
855 if (i
->bootindex
== bootindex
) {
856 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
858 } else if (i
->bootindex
< bootindex
) {
861 QTAILQ_INSERT_BEFORE(i
, node
, link
);
864 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
868 * This function returns null terminated string that consist of new line
869 * separated device paths.
871 * memory pointed by "size" is assigned total length of the array in bytes
874 char *get_boot_devices_list(uint32_t *size
)
880 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
881 char *devpath
= NULL
, *bootpath
;
885 devpath
= qdev_get_fw_dev_path(i
->dev
);
889 if (i
->suffix
&& devpath
) {
890 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
892 bootpath
= g_malloc(bootpathlen
);
893 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
895 } else if (devpath
) {
898 bootpath
= g_strdup(i
->suffix
);
903 list
[total
-1] = '\n';
905 len
= strlen(bootpath
) + 1;
906 list
= g_realloc(list
, total
+ len
);
907 memcpy(&list
[total
], bootpath
, len
);
917 static void numa_add(const char *optarg
)
921 unsigned long long value
, endvalue
;
924 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
925 if (!strcmp(option
, "node")) {
926 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
927 nodenr
= nb_numa_nodes
;
929 nodenr
= strtoull(option
, NULL
, 10);
932 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
933 node_mem
[nodenr
] = 0;
936 sval
= strtosz(option
, NULL
);
938 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
941 node_mem
[nodenr
] = sval
;
943 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
944 node_cpumask
[nodenr
] = 0;
946 value
= strtoull(option
, &endptr
, 10);
949 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
951 if (*endptr
== '-') {
952 endvalue
= strtoull(endptr
+1, &endptr
, 10);
953 if (endvalue
>= 63) {
956 "only 63 CPUs in NUMA mode supported.\n");
958 value
= (2ULL << endvalue
) - (1ULL << value
);
960 value
= 1ULL << value
;
963 node_cpumask
[nodenr
] = value
;
970 static void smp_parse(const char *optarg
)
972 int smp
, sockets
= 0, threads
= 0, cores
= 0;
976 smp
= strtoul(optarg
, &endptr
, 10);
977 if (endptr
!= optarg
) {
978 if (*endptr
== ',') {
982 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
983 sockets
= strtoull(option
, NULL
, 10);
984 if (get_param_value(option
, 128, "cores", endptr
) != 0)
985 cores
= strtoull(option
, NULL
, 10);
986 if (get_param_value(option
, 128, "threads", endptr
) != 0)
987 threads
= strtoull(option
, NULL
, 10);
988 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
989 max_cpus
= strtoull(option
, NULL
, 10);
991 /* compute missing values, prefer sockets over cores over threads */
992 if (smp
== 0 || sockets
== 0) {
993 sockets
= sockets
> 0 ? sockets
: 1;
994 cores
= cores
> 0 ? cores
: 1;
995 threads
= threads
> 0 ? threads
: 1;
997 smp
= cores
* threads
* sockets
;
1001 threads
= threads
> 0 ? threads
: 1;
1002 cores
= smp
/ (sockets
* threads
);
1004 threads
= smp
/ (cores
* sockets
);
1008 smp_cores
= cores
> 0 ? cores
: 1;
1009 smp_threads
= threads
> 0 ? threads
: 1;
1011 max_cpus
= smp_cpus
;
1014 /***********************************************************/
1017 static int usb_device_add(const char *devname
)
1020 USBDevice
*dev
= NULL
;
1025 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1026 dev
= usbdevice_create(devname
);
1030 /* the other ones */
1031 #ifndef CONFIG_LINUX
1032 /* only the linux version is qdev-ified, usb-bsd still needs this */
1033 if (strstart(devname
, "host:", &p
)) {
1034 dev
= usb_host_device_open(p
);
1037 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1038 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1039 bt_new_hci(qemu_find_bt_vlan(0)));
1050 static int usb_device_del(const char *devname
)
1055 if (strstart(devname
, "host:", &p
))
1056 return usb_host_device_close(p
);
1061 p
= strchr(devname
, '.');
1064 bus_num
= strtoul(devname
, NULL
, 0);
1065 addr
= strtoul(p
+ 1, NULL
, 0);
1067 return usb_device_delete_addr(bus_num
, addr
);
1070 static int usb_parse(const char *cmdline
)
1073 r
= usb_device_add(cmdline
);
1075 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1080 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1082 const char *devname
= qdict_get_str(qdict
, "devname");
1083 if (usb_device_add(devname
) < 0) {
1084 error_report("could not add USB device '%s'", devname
);
1088 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1090 const char *devname
= qdict_get_str(qdict
, "devname");
1091 if (usb_device_del(devname
) < 0) {
1092 error_report("could not delete USB device '%s'", devname
);
1096 /***********************************************************/
1097 /* PCMCIA/Cardbus */
1099 static struct pcmcia_socket_entry_s
{
1100 PCMCIASocket
*socket
;
1101 struct pcmcia_socket_entry_s
*next
;
1102 } *pcmcia_sockets
= 0;
1104 void pcmcia_socket_register(PCMCIASocket
*socket
)
1106 struct pcmcia_socket_entry_s
*entry
;
1108 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1109 entry
->socket
= socket
;
1110 entry
->next
= pcmcia_sockets
;
1111 pcmcia_sockets
= entry
;
1114 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1116 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1118 ptr
= &pcmcia_sockets
;
1119 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1120 if (entry
->socket
== socket
) {
1126 void pcmcia_info(Monitor
*mon
)
1128 struct pcmcia_socket_entry_s
*iter
;
1130 if (!pcmcia_sockets
)
1131 monitor_printf(mon
, "No PCMCIA sockets\n");
1133 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1134 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1135 iter
->socket
->attached
? iter
->socket
->card_string
:
1139 /***********************************************************/
1140 /* machine registration */
1142 static QEMUMachine
*first_machine
= NULL
;
1143 QEMUMachine
*current_machine
= NULL
;
1145 int qemu_register_machine(QEMUMachine
*m
)
1148 pm
= &first_machine
;
1156 static QEMUMachine
*find_machine(const char *name
)
1160 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1161 if (!strcmp(m
->name
, name
))
1163 if (m
->alias
&& !strcmp(m
->alias
, name
))
1169 static QEMUMachine
*find_default_machine(void)
1173 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1174 if (m
->is_default
) {
1181 /***********************************************************/
1182 /* main execution loop */
1184 static void gui_update(void *opaque
)
1186 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1187 DisplayState
*ds
= opaque
;
1188 DisplayChangeListener
*dcl
= ds
->listeners
;
1190 qemu_flush_coalesced_mmio_buffer();
1193 while (dcl
!= NULL
) {
1194 if (dcl
->gui_timer_interval
&&
1195 dcl
->gui_timer_interval
< interval
)
1196 interval
= dcl
->gui_timer_interval
;
1199 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1202 static void nographic_update(void *opaque
)
1204 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1206 qemu_flush_coalesced_mmio_buffer();
1207 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1210 struct vm_change_state_entry
{
1211 VMChangeStateHandler
*cb
;
1213 QLIST_ENTRY (vm_change_state_entry
) entries
;
1216 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1218 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1221 VMChangeStateEntry
*e
;
1223 e
= g_malloc0(sizeof (*e
));
1227 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1231 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1233 QLIST_REMOVE (e
, entries
);
1237 void vm_state_notify(int running
, RunState state
)
1239 VMChangeStateEntry
*e
;
1241 trace_vm_state_notify(running
, state
);
1243 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1244 e
->cb(e
->opaque
, running
, state
);
1250 if (!runstate_is_running()) {
1252 runstate_set(RSTATE_RUNNING
);
1253 vm_state_notify(1, RSTATE_RUNNING
);
1255 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1259 /* reset/shutdown handler */
1261 typedef struct QEMUResetEntry
{
1262 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1263 QEMUResetHandler
*func
;
1267 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1268 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1269 static int reset_requested
;
1270 static int shutdown_requested
, shutdown_signal
= -1;
1271 static pid_t shutdown_pid
;
1272 static int powerdown_requested
;
1273 static int debug_requested
;
1274 static RunState vmstop_requested
= RSTATE_NO_STATE
;
1276 int qemu_shutdown_requested_get(void)
1278 return shutdown_requested
;
1281 int qemu_reset_requested_get(void)
1283 return reset_requested
;
1286 int qemu_shutdown_requested(void)
1288 int r
= shutdown_requested
;
1289 shutdown_requested
= 0;
1293 void qemu_kill_report(void)
1295 if (shutdown_signal
!= -1) {
1296 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1297 if (shutdown_pid
== 0) {
1298 /* This happens for eg ^C at the terminal, so it's worth
1299 * avoiding printing an odd message in that case.
1301 fputc('\n', stderr
);
1303 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1305 shutdown_signal
= -1;
1309 int qemu_reset_requested(void)
1311 int r
= reset_requested
;
1312 reset_requested
= 0;
1316 int qemu_powerdown_requested(void)
1318 int r
= powerdown_requested
;
1319 powerdown_requested
= 0;
1323 static int qemu_debug_requested(void)
1325 int r
= debug_requested
;
1326 debug_requested
= 0;
1330 static RunState
qemu_vmstop_requested(void)
1332 RunState s
= vmstop_requested
;
1333 vmstop_requested
= RSTATE_NO_STATE
;
1337 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1339 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1342 re
->opaque
= opaque
;
1343 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1346 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1350 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1351 if (re
->func
== func
&& re
->opaque
== opaque
) {
1352 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1359 void qemu_system_reset(bool report
)
1361 QEMUResetEntry
*re
, *nre
;
1363 /* reset all devices */
1364 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1365 re
->func(re
->opaque
);
1368 monitor_protocol_event(QEVENT_RESET
, NULL
);
1370 cpu_synchronize_all_post_reset();
1373 void qemu_system_reset_request(void)
1376 shutdown_requested
= 1;
1378 reset_requested
= 1;
1381 qemu_notify_event();
1384 void qemu_system_killed(int signal
, pid_t pid
)
1386 shutdown_signal
= signal
;
1388 qemu_system_shutdown_request();
1391 void qemu_system_shutdown_request(void)
1393 shutdown_requested
= 1;
1394 qemu_notify_event();
1397 void qemu_system_powerdown_request(void)
1399 powerdown_requested
= 1;
1400 qemu_notify_event();
1403 void qemu_system_debug_request(void)
1405 debug_requested
= 1;
1406 qemu_notify_event();
1409 void qemu_system_vmstop_request(RunState state
)
1411 vmstop_requested
= state
;
1412 qemu_notify_event();
1415 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1416 static int n_poll_fds
;
1417 static int max_priority
;
1419 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1420 fd_set
*xfds
, struct timeval
*tv
)
1422 GMainContext
*context
= g_main_context_default();
1424 int timeout
= 0, cur_timeout
;
1426 g_main_context_prepare(context
, &max_priority
);
1428 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1429 poll_fds
, ARRAY_SIZE(poll_fds
));
1430 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1432 for (i
= 0; i
< n_poll_fds
; i
++) {
1433 GPollFD
*p
= &poll_fds
[i
];
1435 if ((p
->events
& G_IO_IN
)) {
1436 FD_SET(p
->fd
, rfds
);
1437 *max_fd
= MAX(*max_fd
, p
->fd
);
1439 if ((p
->events
& G_IO_OUT
)) {
1440 FD_SET(p
->fd
, wfds
);
1441 *max_fd
= MAX(*max_fd
, p
->fd
);
1443 if ((p
->events
& G_IO_ERR
)) {
1444 FD_SET(p
->fd
, xfds
);
1445 *max_fd
= MAX(*max_fd
, p
->fd
);
1449 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1450 if (timeout
>= 0 && timeout
< cur_timeout
) {
1451 tv
->tv_sec
= timeout
/ 1000;
1452 tv
->tv_usec
= (timeout
% 1000) * 1000;
1456 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1459 GMainContext
*context
= g_main_context_default();
1464 for (i
= 0; i
< n_poll_fds
; i
++) {
1465 GPollFD
*p
= &poll_fds
[i
];
1467 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1468 p
->revents
|= G_IO_IN
;
1470 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1471 p
->revents
|= G_IO_OUT
;
1473 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1474 p
->revents
|= G_IO_ERR
;
1479 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1480 g_main_context_dispatch(context
);
1484 int main_loop_wait(int nonblocking
)
1486 fd_set rfds
, wfds
, xfds
;
1494 timeout
= qemu_calculate_timeout();
1495 qemu_bh_update_timeout(&timeout
);
1498 os_host_main_loop_wait(&timeout
);
1500 tv
.tv_sec
= timeout
/ 1000;
1501 tv
.tv_usec
= (timeout
% 1000) * 1000;
1503 /* poll any events */
1504 /* XXX: separate device handlers from system ones */
1510 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1511 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1512 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1515 qemu_mutex_unlock_iothread();
1518 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1521 qemu_mutex_lock_iothread();
1524 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1525 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1526 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1528 qemu_run_all_timers();
1530 /* Check bottom-halves last in case any of the earlier events triggered
1537 qemu_irq qemu_system_powerdown
;
1539 static void main_loop(void)
1542 int last_io
__attribute__ ((unused
)) = 0;
1543 #ifdef CONFIG_PROFILER
1548 qemu_main_loop_start();
1551 nonblocking
= !kvm_enabled() && last_io
> 0;
1552 #ifdef CONFIG_PROFILER
1553 ti
= profile_getclock();
1555 last_io
= main_loop_wait(nonblocking
);
1556 #ifdef CONFIG_PROFILER
1557 dev_time
+= profile_getclock() - ti
;
1560 if (qemu_debug_requested()) {
1561 vm_stop(RSTATE_DEBUG
);
1563 if (qemu_shutdown_requested()) {
1565 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1567 vm_stop(RSTATE_SHUTDOWN
);
1571 if (qemu_reset_requested()) {
1573 cpu_synchronize_all_states();
1574 qemu_system_reset(VMRESET_REPORT
);
1577 if (qemu_powerdown_requested()) {
1578 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1579 qemu_irq_raise(qemu_system_powerdown
);
1581 if ((r
= qemu_vmstop_requested())) {
1589 static void version(void)
1591 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1594 static void help(int exitcode
)
1596 const char *options_help
=
1597 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1599 #define DEFHEADING(text) stringify(text) "\n"
1600 #include "qemu-options.def"
1606 printf("usage: %s [options] [disk_image]\n"
1608 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1611 "During emulation, the following keys are useful:\n"
1612 "ctrl-alt-f toggle full screen\n"
1613 "ctrl-alt-n switch to virtual console 'n'\n"
1614 "ctrl-alt toggle mouse and keyboard grab\n"
1616 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1622 #define HAS_ARG 0x0001
1624 typedef struct QEMUOption
{
1631 static const QEMUOption qemu_options
[] = {
1632 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1633 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1634 { option, opt_arg, opt_enum, arch_mask },
1635 #define DEFHEADING(text)
1636 #include "qemu-options.def"
1642 static void select_vgahw (const char *p
)
1647 vga_interface_type
= VGA_NONE
;
1648 if (strstart(p
, "std", &opts
)) {
1649 vga_interface_type
= VGA_STD
;
1650 } else if (strstart(p
, "cirrus", &opts
)) {
1651 vga_interface_type
= VGA_CIRRUS
;
1652 } else if (strstart(p
, "vmware", &opts
)) {
1653 vga_interface_type
= VGA_VMWARE
;
1654 } else if (strstart(p
, "xenfb", &opts
)) {
1655 vga_interface_type
= VGA_XENFB
;
1656 } else if (strstart(p
, "qxl", &opts
)) {
1657 vga_interface_type
= VGA_QXL
;
1658 } else if (!strstart(p
, "none", &opts
)) {
1660 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1664 const char *nextopt
;
1666 if (strstart(opts
, ",retrace=", &nextopt
)) {
1668 if (strstart(opts
, "dumb", &nextopt
))
1669 vga_retrace_method
= VGA_RETRACE_DUMB
;
1670 else if (strstart(opts
, "precise", &nextopt
))
1671 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1672 else goto invalid_vga
;
1673 } else goto invalid_vga
;
1678 static DisplayType
select_display(const char *p
)
1681 DisplayType display
= DT_DEFAULT
;
1683 if (strstart(p
, "sdl", &opts
)) {
1687 const char *nextopt
;
1689 if (strstart(opts
, ",frame=", &nextopt
)) {
1691 if (strstart(opts
, "on", &nextopt
)) {
1693 } else if (strstart(opts
, "off", &nextopt
)) {
1696 goto invalid_sdl_args
;
1698 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1700 if (strstart(opts
, "on", &nextopt
)) {
1702 } else if (strstart(opts
, "off", &nextopt
)) {
1705 goto invalid_sdl_args
;
1707 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1709 if (strstart(opts
, "on", &nextopt
)) {
1711 } else if (strstart(opts
, "off", &nextopt
)) {
1714 goto invalid_sdl_args
;
1716 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1718 if (strstart(opts
, "on", &nextopt
)) {
1720 } else if (strstart(opts
, "off", &nextopt
)) {
1723 goto invalid_sdl_args
;
1727 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1733 fprintf(stderr
, "SDL support is disabled\n");
1736 } else if (strstart(p
, "vnc", &opts
)) {
1741 const char *nextopt
;
1743 if (strstart(opts
, "=", &nextopt
)) {
1744 vnc_display
= nextopt
;
1748 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1752 fprintf(stderr
, "VNC support is disabled\n");
1755 } else if (strstart(p
, "curses", &opts
)) {
1756 #ifdef CONFIG_CURSES
1757 display
= DT_CURSES
;
1759 fprintf(stderr
, "Curses support is disabled\n");
1762 } else if (strstart(p
, "none", &opts
)) {
1765 fprintf(stderr
, "Unknown display type: %s\n", p
);
1772 static int balloon_parse(const char *arg
)
1776 if (strcmp(arg
, "none") == 0) {
1780 if (!strncmp(arg
, "virtio", 6)) {
1781 if (arg
[6] == ',') {
1782 /* have params -> parse them */
1783 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1787 /* create empty opts */
1788 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1790 qemu_opt_set(opts
, "driver", "virtio-balloon");
1797 char *qemu_find_file(int type
, const char *name
)
1803 /* If name contains path separators then try it as a straight path. */
1804 if ((strchr(name
, '/') || strchr(name
, '\\'))
1805 && access(name
, R_OK
) == 0) {
1806 return g_strdup(name
);
1809 case QEMU_FILE_TYPE_BIOS
:
1812 case QEMU_FILE_TYPE_KEYMAP
:
1813 subdir
= "keymaps/";
1818 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1819 buf
= g_malloc0(len
);
1820 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1821 if (access(buf
, R_OK
)) {
1828 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1830 return qdev_device_help(opts
);
1833 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1837 dev
= qdev_device_add(opts
);
1843 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1845 CharDriverState
*chr
;
1847 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1853 #ifdef CONFIG_VIRTFS
1854 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1857 ret
= qemu_fsdev_add(opts
);
1863 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1865 CharDriverState
*chr
;
1866 const char *chardev
;
1870 mode
= qemu_opt_get(opts
, "mode");
1874 if (strcmp(mode
, "readline") == 0) {
1875 flags
= MONITOR_USE_READLINE
;
1876 } else if (strcmp(mode
, "control") == 0) {
1877 flags
= MONITOR_USE_CONTROL
;
1879 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1883 if (qemu_opt_get_bool(opts
, "pretty", 0))
1884 flags
|= MONITOR_USE_PRETTY
;
1886 if (qemu_opt_get_bool(opts
, "default", 0))
1887 flags
|= MONITOR_IS_DEFAULT
;
1889 chardev
= qemu_opt_get(opts
, "chardev");
1890 chr
= qemu_chr_find(chardev
);
1892 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1896 monitor_init(chr
, flags
);
1900 static void monitor_parse(const char *optarg
, const char *mode
)
1902 static int monitor_device_index
= 0;
1908 if (strstart(optarg
, "chardev:", &p
)) {
1909 snprintf(label
, sizeof(label
), "%s", p
);
1911 snprintf(label
, sizeof(label
), "compat_monitor%d",
1912 monitor_device_index
);
1913 if (monitor_device_index
== 0) {
1916 opts
= qemu_chr_parse_compat(label
, optarg
);
1918 fprintf(stderr
, "parse error: %s\n", optarg
);
1923 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1925 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1928 qemu_opt_set(opts
, "mode", mode
);
1929 qemu_opt_set(opts
, "chardev", label
);
1931 qemu_opt_set(opts
, "default", "on");
1932 monitor_device_index
++;
1935 struct device_config
{
1937 DEV_USB
, /* -usbdevice */
1939 DEV_SERIAL
, /* -serial */
1940 DEV_PARALLEL
, /* -parallel */
1941 DEV_VIRTCON
, /* -virtioconsole */
1942 DEV_DEBUGCON
, /* -debugcon */
1944 const char *cmdline
;
1945 QTAILQ_ENTRY(device_config
) next
;
1947 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1949 static void add_device_config(int type
, const char *cmdline
)
1951 struct device_config
*conf
;
1953 conf
= g_malloc0(sizeof(*conf
));
1955 conf
->cmdline
= cmdline
;
1956 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1959 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1961 struct device_config
*conf
;
1964 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1965 if (conf
->type
!= type
)
1967 rc
= func(conf
->cmdline
);
1974 static int serial_parse(const char *devname
)
1976 static int index
= 0;
1979 if (strcmp(devname
, "none") == 0)
1981 if (index
== MAX_SERIAL_PORTS
) {
1982 fprintf(stderr
, "qemu: too many serial ports\n");
1985 snprintf(label
, sizeof(label
), "serial%d", index
);
1986 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1987 if (!serial_hds
[index
]) {
1988 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1989 devname
, strerror(errno
));
1996 static int parallel_parse(const char *devname
)
1998 static int index
= 0;
2001 if (strcmp(devname
, "none") == 0)
2003 if (index
== MAX_PARALLEL_PORTS
) {
2004 fprintf(stderr
, "qemu: too many parallel ports\n");
2007 snprintf(label
, sizeof(label
), "parallel%d", index
);
2008 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2009 if (!parallel_hds
[index
]) {
2010 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2011 devname
, strerror(errno
));
2018 static int virtcon_parse(const char *devname
)
2020 QemuOptsList
*device
= qemu_find_opts("device");
2021 static int index
= 0;
2023 QemuOpts
*bus_opts
, *dev_opts
;
2025 if (strcmp(devname
, "none") == 0)
2027 if (index
== MAX_VIRTIO_CONSOLES
) {
2028 fprintf(stderr
, "qemu: too many virtio consoles\n");
2032 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2033 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2035 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2036 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2038 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2039 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2040 if (!virtcon_hds
[index
]) {
2041 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2042 devname
, strerror(errno
));
2045 qemu_opt_set(dev_opts
, "chardev", label
);
2051 static int debugcon_parse(const char *devname
)
2055 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2058 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2060 fprintf(stderr
, "qemu: already have a debugcon device\n");
2063 qemu_opt_set(opts
, "driver", "isa-debugcon");
2064 qemu_opt_set(opts
, "chardev", "debugcon");
2068 static QEMUMachine
*machine_parse(const char *name
)
2070 QEMUMachine
*m
, *machine
= NULL
;
2073 machine
= find_machine(name
);
2078 printf("Supported machines are:\n");
2079 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2081 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2083 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2084 m
->is_default
? " (default)" : "");
2086 exit(!name
|| *name
!= '?');
2089 static int tcg_init(void)
2091 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2096 const char *opt_name
;
2098 int (*available
)(void);
2102 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2103 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2104 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2107 static int configure_accelerator(void)
2109 const char *p
= NULL
;
2112 bool accel_initalised
= 0;
2113 bool init_failed
= 0;
2115 QemuOptsList
*list
= qemu_find_opts("machine");
2116 if (!QTAILQ_EMPTY(&list
->head
)) {
2117 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2121 /* Use the default "accelerator", tcg */
2125 while (!accel_initalised
&& *p
!= '\0') {
2129 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2130 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2131 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2132 *(accel_list
[i
].allowed
) = 1;
2133 ret
= accel_list
[i
].init();
2136 if (!accel_list
[i
].available()) {
2137 printf("%s not supported for this target\n",
2138 accel_list
[i
].name
);
2140 fprintf(stderr
, "failed to initialize %s: %s\n",
2144 *(accel_list
[i
].allowed
) = 0;
2146 accel_initalised
= 1;
2151 if (i
== ARRAY_SIZE(accel_list
)) {
2152 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2156 if (!accel_initalised
) {
2157 fprintf(stderr
, "No accelerator found!\n");
2162 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2165 return !accel_initalised
;
2168 void qemu_add_exit_notifier(Notifier
*notify
)
2170 notifier_list_add(&exit_notifiers
, notify
);
2173 void qemu_remove_exit_notifier(Notifier
*notify
)
2175 notifier_list_remove(&exit_notifiers
, notify
);
2178 static void qemu_run_exit_notifiers(void)
2180 notifier_list_notify(&exit_notifiers
, NULL
);
2183 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2185 notifier_list_add(&machine_init_done_notifiers
, notify
);
2188 static void qemu_run_machine_init_done_notifiers(void)
2190 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2193 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2194 const char **poptarg
, int *poptind
)
2196 const QEMUOption
*popt
;
2197 int optind
= *poptind
;
2198 char *r
= argv
[optind
];
2201 loc_set_cmdline(argv
, optind
, 1);
2203 /* Treat --foo the same as -foo. */
2206 popt
= qemu_options
;
2209 error_report("invalid option");
2212 if (!strcmp(popt
->name
, r
+ 1))
2216 if (popt
->flags
& HAS_ARG
) {
2217 if (optind
>= argc
) {
2218 error_report("requires an argument");
2221 optarg
= argv
[optind
++];
2222 loc_set_cmdline(argv
, optind
- 2, 2);
2233 static gpointer
malloc_and_trace(gsize n_bytes
)
2235 void *ptr
= malloc(n_bytes
);
2236 trace_g_malloc(n_bytes
, ptr
);
2240 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2242 void *ptr
= realloc(mem
, n_bytes
);
2243 trace_g_realloc(mem
, n_bytes
, ptr
);
2247 static void free_and_trace(gpointer mem
)
2253 int main(int argc
, char **argv
, char **envp
)
2255 const char *gdbstub_dev
= NULL
;
2257 int snapshot
, linux_boot
;
2258 const char *icount_option
= NULL
;
2259 const char *initrd_filename
;
2260 const char *kernel_filename
, *kernel_cmdline
;
2261 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2263 DisplayChangeListener
*dcl
;
2264 int cyls
, heads
, secs
, translation
;
2265 QemuOpts
*hda_opts
= NULL
, *opts
;
2266 QemuOptsList
*olist
;
2269 const char *loadvm
= NULL
;
2270 QEMUMachine
*machine
;
2271 const char *cpu_model
;
2272 const char *pid_file
= NULL
;
2273 const char *incoming
= NULL
;
2275 int show_vnc_port
= 0;
2278 const char *log_mask
= NULL
;
2279 const char *log_file
= NULL
;
2280 GMemVTable mem_trace
= {
2281 .malloc
= malloc_and_trace
,
2282 .realloc
= realloc_and_trace
,
2283 .free
= free_and_trace
,
2285 const char *trace_events
= NULL
;
2286 const char *trace_file
= NULL
;
2288 atexit(qemu_run_exit_notifiers
);
2289 error_set_progname(argv
[0]);
2291 g_mem_set_vtable(&mem_trace
);
2292 g_thread_init(NULL
);
2298 qemu_cache_utils_init(envp
);
2300 QLIST_INIT (&vm_change_state_head
);
2301 os_setup_early_signal_handling();
2303 module_call_init(MODULE_INIT_MACHINE
);
2304 machine
= find_default_machine();
2306 initrd_filename
= NULL
;
2309 kernel_filename
= NULL
;
2310 kernel_cmdline
= "";
2311 cyls
= heads
= secs
= 0;
2312 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2314 for (i
= 0; i
< MAX_NODES
; i
++) {
2316 node_cpumask
[i
] = 0;
2324 /* first pass of option parsing */
2326 while (optind
< argc
) {
2327 if (argv
[optind
][0] != '-') {
2332 const QEMUOption
*popt
;
2334 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2335 switch (popt
->index
) {
2336 case QEMU_OPTION_nodefconfig
:
2346 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2347 if (ret
< 0 && ret
!= -ENOENT
) {
2351 ret
= qemu_read_config_file(arch_config_name
);
2352 if (ret
< 0 && ret
!= -ENOENT
) {
2358 /* second pass of option parsing */
2363 if (argv
[optind
][0] != '-') {
2364 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2366 const QEMUOption
*popt
;
2368 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2369 if (!(popt
->arch_mask
& arch_type
)) {
2370 printf("Option %s not supported for this target\n", popt
->name
);
2373 switch(popt
->index
) {
2375 machine
= machine_parse(optarg
);
2377 case QEMU_OPTION_cpu
:
2378 /* hw initialization will check this */
2379 if (*optarg
== '?') {
2380 list_cpus(stdout
, &fprintf
, optarg
);
2386 case QEMU_OPTION_initrd
:
2387 initrd_filename
= optarg
;
2389 case QEMU_OPTION_hda
:
2393 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2395 snprintf(buf
, sizeof(buf
),
2396 "%s,cyls=%d,heads=%d,secs=%d%s",
2397 HD_OPTS
, cyls
, heads
, secs
,
2398 translation
== BIOS_ATA_TRANSLATION_LBA
?
2400 translation
== BIOS_ATA_TRANSLATION_NONE
?
2401 ",trans=none" : "");
2402 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2405 case QEMU_OPTION_hdb
:
2406 case QEMU_OPTION_hdc
:
2407 case QEMU_OPTION_hdd
:
2408 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2411 case QEMU_OPTION_drive
:
2412 if (drive_def(optarg
) == NULL
) {
2416 case QEMU_OPTION_set
:
2417 if (qemu_set_option(optarg
) != 0)
2420 case QEMU_OPTION_global
:
2421 if (qemu_global_option(optarg
) != 0)
2424 case QEMU_OPTION_mtdblock
:
2425 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2427 case QEMU_OPTION_sd
:
2428 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2430 case QEMU_OPTION_pflash
:
2431 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2433 case QEMU_OPTION_snapshot
:
2436 case QEMU_OPTION_hdachs
:
2440 cyls
= strtol(p
, (char **)&p
, 0);
2441 if (cyls
< 1 || cyls
> 16383)
2446 heads
= strtol(p
, (char **)&p
, 0);
2447 if (heads
< 1 || heads
> 16)
2452 secs
= strtol(p
, (char **)&p
, 0);
2453 if (secs
< 1 || secs
> 63)
2457 if (!strcmp(p
, "none"))
2458 translation
= BIOS_ATA_TRANSLATION_NONE
;
2459 else if (!strcmp(p
, "lba"))
2460 translation
= BIOS_ATA_TRANSLATION_LBA
;
2461 else if (!strcmp(p
, "auto"))
2462 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2465 } else if (*p
!= '\0') {
2467 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2470 if (hda_opts
!= NULL
) {
2472 snprintf(num
, sizeof(num
), "%d", cyls
);
2473 qemu_opt_set(hda_opts
, "cyls", num
);
2474 snprintf(num
, sizeof(num
), "%d", heads
);
2475 qemu_opt_set(hda_opts
, "heads", num
);
2476 snprintf(num
, sizeof(num
), "%d", secs
);
2477 qemu_opt_set(hda_opts
, "secs", num
);
2478 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2479 qemu_opt_set(hda_opts
, "trans", "lba");
2480 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2481 qemu_opt_set(hda_opts
, "trans", "none");
2485 case QEMU_OPTION_numa
:
2486 if (nb_numa_nodes
>= MAX_NODES
) {
2487 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2492 case QEMU_OPTION_display
:
2493 display_type
= select_display(optarg
);
2495 case QEMU_OPTION_nographic
:
2496 display_type
= DT_NOGRAPHIC
;
2498 case QEMU_OPTION_curses
:
2499 #ifdef CONFIG_CURSES
2500 display_type
= DT_CURSES
;
2502 fprintf(stderr
, "Curses support is disabled\n");
2506 case QEMU_OPTION_portrait
:
2507 graphic_rotate
= 90;
2509 case QEMU_OPTION_rotate
:
2510 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2511 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2512 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2514 "qemu: only 90, 180, 270 deg rotation is available\n");
2518 case QEMU_OPTION_kernel
:
2519 kernel_filename
= optarg
;
2521 case QEMU_OPTION_append
:
2522 kernel_cmdline
= optarg
;
2524 case QEMU_OPTION_cdrom
:
2525 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2527 case QEMU_OPTION_boot
:
2529 static const char * const params
[] = {
2530 "order", "once", "menu",
2531 "splash", "splash-time", NULL
2533 char buf
[sizeof(boot_devices
)];
2534 char *standard_boot_devices
;
2537 if (!strchr(optarg
, '=')) {
2539 pstrcpy(buf
, sizeof(buf
), optarg
);
2540 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2542 "qemu: unknown boot parameter '%s' in '%s'\n",
2548 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2549 validate_bootdevices(buf
);
2550 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2553 if (get_param_value(buf
, sizeof(buf
),
2555 validate_bootdevices(buf
);
2556 standard_boot_devices
= g_strdup(boot_devices
);
2557 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2558 qemu_register_reset(restore_boot_devices
,
2559 standard_boot_devices
);
2561 if (get_param_value(buf
, sizeof(buf
),
2563 if (!strcmp(buf
, "on")) {
2565 } else if (!strcmp(buf
, "off")) {
2569 "qemu: invalid option value '%s'\n",
2574 qemu_opts_parse(qemu_find_opts("boot-opts"),
2579 case QEMU_OPTION_fda
:
2580 case QEMU_OPTION_fdb
:
2581 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2584 case QEMU_OPTION_no_fd_bootchk
:
2587 case QEMU_OPTION_netdev
:
2588 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2592 case QEMU_OPTION_net
:
2593 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2598 case QEMU_OPTION_tftp
:
2599 legacy_tftp_prefix
= optarg
;
2601 case QEMU_OPTION_bootp
:
2602 legacy_bootp_filename
= optarg
;
2604 case QEMU_OPTION_redir
:
2605 if (net_slirp_redir(optarg
) < 0)
2609 case QEMU_OPTION_bt
:
2610 add_device_config(DEV_BT
, optarg
);
2612 case QEMU_OPTION_audio_help
:
2613 if (!(audio_available())) {
2614 printf("Option %s not supported for this target\n", popt
->name
);
2620 case QEMU_OPTION_soundhw
:
2621 if (!(audio_available())) {
2622 printf("Option %s not supported for this target\n", popt
->name
);
2625 select_soundhw (optarg
);
2630 case QEMU_OPTION_version
:
2634 case QEMU_OPTION_m
: {
2637 value
= strtosz(optarg
, NULL
);
2639 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2643 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2644 fprintf(stderr
, "qemu: ram size too large\n");
2650 case QEMU_OPTION_mempath
:
2654 case QEMU_OPTION_mem_prealloc
:
2665 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2667 case QEMU_OPTION_gdb
:
2668 gdbstub_dev
= optarg
;
2673 case QEMU_OPTION_bios
:
2676 case QEMU_OPTION_singlestep
:
2683 keyboard_layout
= optarg
;
2685 case QEMU_OPTION_localtime
:
2688 case QEMU_OPTION_vga
:
2689 select_vgahw (optarg
);
2696 w
= strtol(p
, (char **)&p
, 10);
2699 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2705 h
= strtol(p
, (char **)&p
, 10);
2710 depth
= strtol(p
, (char **)&p
, 10);
2711 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2712 depth
!= 24 && depth
!= 32)
2714 } else if (*p
== '\0') {
2715 depth
= graphic_depth
;
2722 graphic_depth
= depth
;
2725 case QEMU_OPTION_echr
:
2728 term_escape_char
= strtol(optarg
, &r
, 0);
2730 printf("Bad argument to echr\n");
2733 case QEMU_OPTION_monitor
:
2734 monitor_parse(optarg
, "readline");
2735 default_monitor
= 0;
2737 case QEMU_OPTION_qmp
:
2738 monitor_parse(optarg
, "control");
2739 default_monitor
= 0;
2741 case QEMU_OPTION_mon
:
2742 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2746 default_monitor
= 0;
2748 case QEMU_OPTION_chardev
:
2749 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2754 case QEMU_OPTION_fsdev
:
2755 olist
= qemu_find_opts("fsdev");
2757 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2760 opts
= qemu_opts_parse(olist
, optarg
, 1);
2762 fprintf(stderr
, "parse error: %s\n", optarg
);
2766 case QEMU_OPTION_virtfs
: {
2770 olist
= qemu_find_opts("virtfs");
2772 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2775 opts
= qemu_opts_parse(olist
, optarg
, 1);
2777 fprintf(stderr
, "parse error: %s\n", optarg
);
2781 if (qemu_opt_get(opts
, "fstype") == NULL
||
2782 qemu_opt_get(opts
, "mount_tag") == NULL
||
2783 qemu_opt_get(opts
, "path") == NULL
||
2784 qemu_opt_get(opts
, "security_model") == NULL
) {
2785 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2786 "security_model=[mapped|passthrough|none],"
2787 "mount_tag=tag.\n");
2791 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2792 qemu_opt_get(opts
, "mount_tag"), 1);
2794 fprintf(stderr
, "duplicate fsdev id: %s\n",
2795 qemu_opt_get(opts
, "mount_tag"));
2798 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2799 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2800 qemu_opt_set(fsdev
, "security_model",
2801 qemu_opt_get(opts
, "security_model"));
2803 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2804 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2805 qemu_opt_set(device
, "fsdev",
2806 qemu_opt_get(opts
, "mount_tag"));
2807 qemu_opt_set(device
, "mount_tag",
2808 qemu_opt_get(opts
, "mount_tag"));
2811 case QEMU_OPTION_serial
:
2812 add_device_config(DEV_SERIAL
, optarg
);
2814 if (strncmp(optarg
, "mon:", 4) == 0) {
2815 default_monitor
= 0;
2818 case QEMU_OPTION_watchdog
:
2821 "qemu: only one watchdog option may be given\n");
2826 case QEMU_OPTION_watchdog_action
:
2827 if (select_watchdog_action(optarg
) == -1) {
2828 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2832 case QEMU_OPTION_virtiocon
:
2833 add_device_config(DEV_VIRTCON
, optarg
);
2834 default_virtcon
= 0;
2835 if (strncmp(optarg
, "mon:", 4) == 0) {
2836 default_monitor
= 0;
2839 case QEMU_OPTION_parallel
:
2840 add_device_config(DEV_PARALLEL
, optarg
);
2841 default_parallel
= 0;
2842 if (strncmp(optarg
, "mon:", 4) == 0) {
2843 default_monitor
= 0;
2846 case QEMU_OPTION_debugcon
:
2847 add_device_config(DEV_DEBUGCON
, optarg
);
2849 case QEMU_OPTION_loadvm
:
2852 case QEMU_OPTION_full_screen
:
2856 case QEMU_OPTION_no_frame
:
2859 case QEMU_OPTION_alt_grab
:
2862 case QEMU_OPTION_ctrl_grab
:
2865 case QEMU_OPTION_no_quit
:
2868 case QEMU_OPTION_sdl
:
2869 display_type
= DT_SDL
;
2872 case QEMU_OPTION_no_frame
:
2873 case QEMU_OPTION_alt_grab
:
2874 case QEMU_OPTION_ctrl_grab
:
2875 case QEMU_OPTION_no_quit
:
2876 case QEMU_OPTION_sdl
:
2877 fprintf(stderr
, "SDL support is disabled\n");
2880 case QEMU_OPTION_pidfile
:
2883 case QEMU_OPTION_win2k_hack
:
2884 win2k_install_hack
= 1;
2886 case QEMU_OPTION_rtc_td_hack
:
2889 case QEMU_OPTION_acpitable
:
2890 do_acpitable_option(optarg
);
2892 case QEMU_OPTION_smbios
:
2893 do_smbios_option(optarg
);
2895 case QEMU_OPTION_enable_kvm
:
2896 olist
= qemu_find_opts("machine");
2897 qemu_opts_reset(olist
);
2898 qemu_opts_parse(olist
, "accel=kvm", 0);
2900 case QEMU_OPTION_machine
:
2901 olist
= qemu_find_opts("machine");
2902 qemu_opts_reset(olist
);
2903 opts
= qemu_opts_parse(olist
, optarg
, 1);
2905 fprintf(stderr
, "parse error: %s\n", optarg
);
2908 optarg
= qemu_opt_get(opts
, "type");
2910 machine
= machine_parse(optarg
);
2913 case QEMU_OPTION_usb
:
2916 case QEMU_OPTION_usbdevice
:
2918 add_device_config(DEV_USB
, optarg
);
2920 case QEMU_OPTION_device
:
2921 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2925 case QEMU_OPTION_smp
:
2928 fprintf(stderr
, "Invalid number of CPUs\n");
2931 if (max_cpus
< smp_cpus
) {
2932 fprintf(stderr
, "maxcpus must be equal to or greater than "
2936 if (max_cpus
> 255) {
2937 fprintf(stderr
, "Unsupported number of maxcpus\n");
2941 case QEMU_OPTION_vnc
:
2944 vnc_display
= optarg
;
2946 fprintf(stderr
, "VNC support is disabled\n");
2950 case QEMU_OPTION_no_acpi
:
2953 case QEMU_OPTION_no_hpet
:
2956 case QEMU_OPTION_balloon
:
2957 if (balloon_parse(optarg
) < 0) {
2958 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2962 case QEMU_OPTION_no_reboot
:
2965 case QEMU_OPTION_no_shutdown
:
2968 case QEMU_OPTION_show_cursor
:
2971 case QEMU_OPTION_uuid
:
2972 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2973 fprintf(stderr
, "Fail to parse UUID string."
2974 " Wrong format.\n");
2978 case QEMU_OPTION_option_rom
:
2979 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2980 fprintf(stderr
, "Too many option ROMs\n");
2983 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2984 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2985 option_rom
[nb_option_roms
].bootindex
=
2986 qemu_opt_get_number(opts
, "bootindex", -1);
2987 if (!option_rom
[nb_option_roms
].name
) {
2988 fprintf(stderr
, "Option ROM file is not specified\n");
2993 case QEMU_OPTION_semihosting
:
2994 semihosting_enabled
= 1;
2996 case QEMU_OPTION_name
:
2997 qemu_name
= g_strdup(optarg
);
2999 char *p
= strchr(qemu_name
, ',');
3002 if (strncmp(p
, "process=", 8)) {
3003 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3007 os_set_proc_name(p
);
3011 case QEMU_OPTION_prom_env
:
3012 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3013 fprintf(stderr
, "Too many prom variables\n");
3016 prom_envs
[nb_prom_envs
] = optarg
;
3019 case QEMU_OPTION_old_param
:
3022 case QEMU_OPTION_clock
:
3023 configure_alarms(optarg
);
3025 case QEMU_OPTION_startdate
:
3026 configure_rtc_date_offset(optarg
, 1);
3028 case QEMU_OPTION_rtc
:
3029 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3033 configure_rtc(opts
);
3035 case QEMU_OPTION_tb_size
:
3036 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3037 if (tcg_tb_size
< 0) {
3041 case QEMU_OPTION_icount
:
3042 icount_option
= optarg
;
3044 case QEMU_OPTION_incoming
:
3047 case QEMU_OPTION_nodefaults
:
3049 default_parallel
= 0;
3050 default_virtcon
= 0;
3051 default_monitor
= 0;
3058 case QEMU_OPTION_xen_domid
:
3059 if (!(xen_available())) {
3060 printf("Option %s not supported for this target\n", popt
->name
);
3063 xen_domid
= atoi(optarg
);
3065 case QEMU_OPTION_xen_create
:
3066 if (!(xen_available())) {
3067 printf("Option %s not supported for this target\n", popt
->name
);
3070 xen_mode
= XEN_CREATE
;
3072 case QEMU_OPTION_xen_attach
:
3073 if (!(xen_available())) {
3074 printf("Option %s not supported for this target\n", popt
->name
);
3077 xen_mode
= XEN_ATTACH
;
3079 case QEMU_OPTION_trace
:
3081 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3085 trace_events
= qemu_opt_get(opts
, "events");
3086 trace_file
= qemu_opt_get(opts
, "file");
3089 case QEMU_OPTION_readconfig
:
3091 int ret
= qemu_read_config_file(optarg
);
3093 fprintf(stderr
, "read config %s: %s\n", optarg
,
3099 case QEMU_OPTION_spice
:
3100 olist
= qemu_find_opts("spice");
3102 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3105 opts
= qemu_opts_parse(olist
, optarg
, 0);
3107 fprintf(stderr
, "parse error: %s\n", optarg
);
3111 case QEMU_OPTION_writeconfig
:
3114 if (strcmp(optarg
, "-") == 0) {
3117 fp
= fopen(optarg
, "w");
3119 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3123 qemu_config_write(fp
);
3128 os_parse_cmd_args(popt
->index
, optarg
);
3134 /* Open the logfile at this point, if necessary. We can't open the logfile
3135 * when encountering either of the logging options (-d or -D) because the
3136 * other one may be encountered later on the command line, changing the
3137 * location or level of logging.
3141 set_cpu_log_filename(log_file
);
3143 set_cpu_log(log_mask
);
3146 if (!trace_backend_init(trace_events
, trace_file
)) {
3150 /* If no data_dir is specified then try to find it relative to the
3153 data_dir
= os_find_datadir(argv
[0]);
3155 /* If all else fails use the install path specified when building. */
3157 data_dir
= CONFIG_QEMU_DATADIR
;
3161 * Default to max_cpus = smp_cpus, in case the user doesn't
3162 * specify a max_cpus value.
3165 max_cpus
= smp_cpus
;
3167 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3168 if (smp_cpus
> machine
->max_cpus
) {
3169 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3170 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3176 * Get the default machine options from the machine if it is not already
3177 * specified either by the configuration file or by the command line.
3179 if (machine
->default_machine_opts
) {
3180 QemuOptsList
*list
= qemu_find_opts("machine");
3181 const char *p
= NULL
;
3183 if (!QTAILQ_EMPTY(&list
->head
)) {
3184 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3187 qemu_opts_reset(list
);
3188 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3190 fprintf(stderr
, "parse error for machine %s: %s\n",
3191 machine
->name
, machine
->default_machine_opts
);
3197 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3198 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3200 if (machine
->no_serial
) {
3203 if (machine
->no_parallel
) {
3204 default_parallel
= 0;
3206 if (!machine
->use_virtcon
) {
3207 default_virtcon
= 0;
3209 if (machine
->no_vga
) {
3212 if (machine
->no_floppy
) {
3215 if (machine
->no_cdrom
) {
3218 if (machine
->no_sdcard
) {
3222 if (display_type
== DT_NOGRAPHIC
) {
3223 if (default_parallel
)
3224 add_device_config(DEV_PARALLEL
, "null");
3225 if (default_serial
&& default_monitor
) {
3226 add_device_config(DEV_SERIAL
, "mon:stdio");
3227 } else if (default_virtcon
&& default_monitor
) {
3228 add_device_config(DEV_VIRTCON
, "mon:stdio");
3231 add_device_config(DEV_SERIAL
, "stdio");
3232 if (default_virtcon
)
3233 add_device_config(DEV_VIRTCON
, "stdio");
3234 if (default_monitor
)
3235 monitor_parse("stdio", "readline");
3239 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3240 if (default_parallel
)
3241 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3242 if (default_monitor
)
3243 monitor_parse("vc:80Cx24C", "readline");
3244 if (default_virtcon
)
3245 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3248 vga_interface_type
= VGA_CIRRUS
;
3252 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3254 #ifdef CONFIG_VIRTFS
3255 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3262 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3267 /* init the memory */
3268 if (ram_size
== 0) {
3269 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3272 configure_accelerator();
3274 if (qemu_init_main_loop()) {
3275 fprintf(stderr
, "qemu_init_main_loop failed\n");
3278 linux_boot
= (kernel_filename
!= NULL
);
3280 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3281 fprintf(stderr
, "-append only allowed with -kernel option\n");
3285 if (!linux_boot
&& initrd_filename
!= NULL
) {
3286 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3290 os_set_line_buffering();
3292 if (init_timer_alarm() < 0) {
3293 fprintf(stderr
, "could not initialize alarm timer\n");
3296 configure_icount(icount_option
);
3298 if (net_init_clients() < 0) {
3302 /* init the bluetooth world */
3303 if (foreach_device_config(DEV_BT
, bt_parse
))
3306 if (!xen_enabled()) {
3307 /* On 32-bit hosts, QEMU is limited by virtual address space */
3308 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3309 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3314 cpu_exec_init_all();
3316 bdrv_init_with_whitelist();
3320 /* open the virtual block devices */
3322 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3323 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3326 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3327 IF_DEFAULT
, 2, CDROM_OPTS
);
3328 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3329 IF_FLOPPY
, 0, FD_OPTS
);
3330 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3333 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3336 if (nb_numa_nodes
> 0) {
3339 if (nb_numa_nodes
> MAX_NODES
) {
3340 nb_numa_nodes
= MAX_NODES
;
3343 /* If no memory size if given for any node, assume the default case
3344 * and distribute the available memory equally across all nodes
3346 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3347 if (node_mem
[i
] != 0)
3350 if (i
== nb_numa_nodes
) {
3351 uint64_t usedmem
= 0;
3353 /* On Linux, the each node's border has to be 8MB aligned,
3354 * the final node gets the rest.
3356 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3357 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3358 usedmem
+= node_mem
[i
];
3360 node_mem
[i
] = ram_size
- usedmem
;
3363 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3364 if (node_cpumask
[i
] != 0)
3367 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3368 * must cope with this anyway, because there are BIOSes out there in
3369 * real machines which also use this scheme.
3371 if (i
== nb_numa_nodes
) {
3372 for (i
= 0; i
< smp_cpus
; i
++) {
3373 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3378 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3382 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3384 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3386 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3388 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3391 module_call_init(MODULE_INIT_DEVICE
);
3393 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3397 i
= select_watchdog(watchdog
);
3399 exit (i
== 1 ? 1 : 0);
3402 if (machine
->compat_props
) {
3403 qdev_prop_register_global_list(machine
->compat_props
);
3407 machine
->init(ram_size
, boot_devices
,
3408 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3410 cpu_synchronize_all_post_init();
3414 current_machine
= machine
;
3416 /* init USB devices */
3418 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3422 /* init generic devices */
3423 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3426 net_check_clients();
3428 /* just use the first displaystate for the moment */
3429 ds
= get_displaystate();
3433 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3434 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3435 display_type
= DT_SDL
;
3436 #elif defined(CONFIG_VNC)
3437 vnc_display
= "localhost:0,to=99";
3440 display_type
= DT_NONE
;
3445 /* init local displays */
3446 switch (display_type
) {
3449 #if defined(CONFIG_CURSES)
3451 curses_display_init(ds
, full_screen
);
3454 #if defined(CONFIG_SDL)
3456 sdl_display_init(ds
, full_screen
, no_frame
);
3458 #elif defined(CONFIG_COCOA)
3460 cocoa_display_init(ds
, full_screen
);
3467 /* must be after terminal init, SDL library changes signal handlers */
3468 os_setup_signal_handling();
3471 /* init remote displays */
3473 vnc_display_init(ds
);
3474 if (vnc_display_open(ds
, vnc_display
) < 0)
3477 if (show_vnc_port
) {
3478 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3483 if (using_spice
&& !qxl_enabled
) {
3484 qemu_spice_display_init(ds
);
3490 dcl
= ds
->listeners
;
3491 while (dcl
!= NULL
) {
3492 if (dcl
->dpy_refresh
!= NULL
) {
3493 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3494 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3499 if (ds
->gui_timer
== NULL
) {
3500 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3501 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3503 text_consoles_set_display(ds
);
3505 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3506 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3511 qdev_machine_creation_done();
3513 if (rom_load_all() != 0) {
3514 fprintf(stderr
, "rom loading failed\n");
3518 /* TODO: once all bus devices are qdevified, this should be done
3519 * when bus is created by qdev.c */
3520 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3521 qemu_run_machine_init_done_notifiers();
3523 qemu_system_reset(VMRESET_SILENT
);
3525 if (load_vmstate(loadvm
) < 0) {
3531 runstate_set(RSTATE_IN_MIGRATE
);
3532 int ret
= qemu_start_incoming_migration(incoming
);
3534 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3538 } else if (autostart
) {
3541 runstate_set(RSTATE_PRE_LAUNCH
);