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"
150 #include "qmp-commands.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 int display_remote
= 0;
181 const char* keyboard_layout
= NULL
;
183 const char *mem_path
= NULL
;
185 int mem_prealloc
= 0; /* force preallocation of physical target memory */
188 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 uint8_t qemu_uuid
[16];
253 static QEMUBootSetHandler
*boot_set_handler
;
254 static void *boot_set_opaque
;
256 static NotifierList exit_notifiers
=
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
259 static NotifierList machine_init_done_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 static int tcg_allowed
= 1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
267 static int tcg_tb_size
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_vga
= 1;
274 static int default_floppy
= 1;
275 static int default_cdrom
= 1;
276 static int default_sdcard
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "isa-fdc", .flag
= &default_floppy
},
285 { .driver
= "ide-cd", .flag
= &default_cdrom
},
286 { .driver
= "ide-hd", .flag
= &default_cdrom
},
287 { .driver
= "ide-drive", .flag
= &default_cdrom
},
288 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
289 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
292 { .driver
= "VGA", .flag
= &default_vga
},
293 { .driver
= "cirrus-vga", .flag
= &default_vga
},
294 { .driver
= "vmware-svga", .flag
= &default_vga
},
295 { .driver
= "isa-vga", .flag
= &default_vga
},
296 { .driver
= "qxl-vga", .flag
= &default_vga
},
299 static void res_free(void)
301 if (boot_splash_filedata
!= NULL
) {
302 g_free(boot_splash_filedata
);
303 boot_splash_filedata
= NULL
;
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
325 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
330 } RunStateTransition
;
332 static const RunStateTransition runstate_transitions_def
[] = {
334 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
336 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
337 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
339 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
341 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
343 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
345 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
347 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
348 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
349 { RUN_STATE_PRELAUNCH
, RUN_STATE_POSTMIGRATE
},
351 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
352 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
354 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
356 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
357 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
358 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
359 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
360 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
361 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
362 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
363 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
364 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
366 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
368 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
370 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
372 { RUN_STATE_MAX
, RUN_STATE_MAX
},
375 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
377 bool runstate_check(RunState state
)
379 return current_run_state
== state
;
382 void runstate_init(void)
384 const RunStateTransition
*p
;
386 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
388 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
389 runstate_valid_transitions
[p
->from
][p
->to
] = true;
393 /* This function will abort() on invalid state transitions */
394 void runstate_set(RunState new_state
)
396 if (new_state
>= RUN_STATE_MAX
||
397 !runstate_valid_transitions
[current_run_state
][new_state
]) {
398 fprintf(stderr
, "invalid runstate transition\n");
402 current_run_state
= new_state
;
405 int runstate_is_running(void)
407 return runstate_check(RUN_STATE_RUNNING
);
410 StatusInfo
*qmp_query_status(Error
**errp
)
412 StatusInfo
*info
= g_malloc0(sizeof(*info
));
414 info
->running
= runstate_is_running();
415 info
->singlestep
= singlestep
;
416 info
->status
= current_run_state
;
421 /***********************************************************/
422 /* real time host monotonic timer */
424 /***********************************************************/
425 /* host time/date access */
426 void qemu_get_timedate(struct tm
*tm
, int offset
)
433 if (rtc_date_offset
== -1) {
437 ret
= localtime(&ti
);
439 ti
-= rtc_date_offset
;
443 memcpy(tm
, ret
, sizeof(struct tm
));
446 int qemu_timedate_diff(struct tm
*tm
)
450 if (rtc_date_offset
== -1)
452 seconds
= mktimegm(tm
);
454 seconds
= mktime(tm
);
456 seconds
= mktimegm(tm
) + rtc_date_offset
;
458 return seconds
- time(NULL
);
461 void rtc_change_mon_event(struct tm
*tm
)
465 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
466 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
467 qobject_decref(data
);
470 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
472 time_t rtc_start_date
;
475 if (!strcmp(startdate
, "now") && legacy
) {
476 rtc_date_offset
= -1;
478 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
486 } else if (sscanf(startdate
, "%d-%d-%d",
498 rtc_start_date
= mktimegm(&tm
);
499 if (rtc_start_date
== -1) {
501 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
502 "'2006-06-17T16:01:21' or '2006-06-17'\n");
505 rtc_date_offset
= time(NULL
) - rtc_start_date
;
509 static void configure_rtc(QemuOpts
*opts
)
513 value
= qemu_opt_get(opts
, "base");
515 if (!strcmp(value
, "utc")) {
517 } else if (!strcmp(value
, "localtime")) {
520 configure_rtc_date_offset(value
, 0);
523 value
= qemu_opt_get(opts
, "clock");
525 if (!strcmp(value
, "host")) {
526 rtc_clock
= host_clock
;
527 } else if (!strcmp(value
, "vm")) {
528 rtc_clock
= vm_clock
;
530 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
534 value
= qemu_opt_get(opts
, "driftfix");
536 if (!strcmp(value
, "slew")) {
538 } else if (!strcmp(value
, "none")) {
541 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
547 /***********************************************************/
548 /* Bluetooth support */
551 static struct HCIInfo
*hci_table
[MAX_NICS
];
553 static struct bt_vlan_s
{
554 struct bt_scatternet_s net
;
556 struct bt_vlan_s
*next
;
559 /* find or alloc a new bluetooth "VLAN" */
560 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
562 struct bt_vlan_s
**pvlan
, *vlan
;
563 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
567 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
569 pvlan
= &first_bt_vlan
;
570 while (*pvlan
!= NULL
)
571 pvlan
= &(*pvlan
)->next
;
576 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
580 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
585 static struct HCIInfo null_hci
= {
586 .cmd_send
= null_hci_send
,
587 .sco_send
= null_hci_send
,
588 .acl_send
= null_hci_send
,
589 .bdaddr_set
= null_hci_addr_set
,
592 struct HCIInfo
*qemu_next_hci(void)
594 if (cur_hci
== nb_hcis
)
597 return hci_table
[cur_hci
++];
600 static struct HCIInfo
*hci_init(const char *str
)
603 struct bt_scatternet_s
*vlan
= 0;
605 if (!strcmp(str
, "null"))
608 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
610 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
611 else if (!strncmp(str
, "hci", 3)) {
614 if (!strncmp(str
+ 3, ",vlan=", 6)) {
615 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
620 vlan
= qemu_find_bt_vlan(0);
622 return bt_new_hci(vlan
);
625 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
630 static int bt_hci_parse(const char *str
)
635 if (nb_hcis
>= MAX_NICS
) {
636 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
649 bdaddr
.b
[5] = 0x56 + nb_hcis
;
650 hci
->bdaddr_set(hci
, bdaddr
.b
);
652 hci_table
[nb_hcis
++] = hci
;
657 static void bt_vhci_add(int vlan_id
)
659 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
662 fprintf(stderr
, "qemu: warning: adding a VHCI to "
663 "an empty scatternet %i\n", vlan_id
);
665 bt_vhci_init(bt_new_hci(vlan
));
668 static struct bt_device_s
*bt_device_add(const char *opt
)
670 struct bt_scatternet_s
*vlan
;
672 char *endp
= strstr(opt
, ",vlan=");
673 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
676 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
679 vlan_id
= strtol(endp
+ 6, &endp
, 0);
681 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
686 vlan
= qemu_find_bt_vlan(vlan_id
);
689 fprintf(stderr
, "qemu: warning: adding a slave device to "
690 "an empty scatternet %i\n", vlan_id
);
692 if (!strcmp(devname
, "keyboard"))
693 return bt_keyboard_init(vlan
);
695 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
699 static int bt_parse(const char *opt
)
701 const char *endp
, *p
;
704 if (strstart(opt
, "hci", &endp
)) {
705 if (!*endp
|| *endp
== ',') {
707 if (!strstart(endp
, ",vlan=", 0))
710 return bt_hci_parse(opt
);
712 } else if (strstart(opt
, "vhci", &endp
)) {
713 if (!*endp
|| *endp
== ',') {
715 if (strstart(endp
, ",vlan=", &p
)) {
716 vlan
= strtol(p
, (char **) &endp
, 0);
718 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
722 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
731 } else if (strstart(opt
, "device:", &endp
))
732 return !bt_device_add(endp
);
734 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
738 /***********************************************************/
739 /* QEMU Block devices */
741 #define HD_OPTS "media=disk"
742 #define CDROM_OPTS "media=cdrom"
744 #define PFLASH_OPTS ""
748 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
750 int *use_scsi
= opaque
;
752 return drive_init(opts
, *use_scsi
) == NULL
;
755 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
757 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
758 qemu_opt_set(opts
, "snapshot", "on");
763 static void default_drive(int enable
, int snapshot
, int use_scsi
,
764 BlockInterfaceType type
, int index
,
769 if (type
== IF_DEFAULT
) {
770 type
= use_scsi
? IF_SCSI
: IF_IDE
;
773 if (!enable
|| drive_get_by_index(type
, index
)) {
777 opts
= drive_add(type
, index
, NULL
, optstr
);
779 drive_enable_snapshot(opts
, NULL
);
781 if (!drive_init(opts
, use_scsi
)) {
786 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
788 boot_set_handler
= func
;
789 boot_set_opaque
= opaque
;
792 int qemu_boot_set(const char *boot_devices
)
794 if (!boot_set_handler
) {
797 return boot_set_handler(boot_set_opaque
, boot_devices
);
800 static void validate_bootdevices(char *devices
)
802 /* We just do some generic consistency checks */
806 for (p
= devices
; *p
!= '\0'; p
++) {
807 /* Allowed boot devices are:
808 * a-b: floppy disk drives
809 * c-f: IDE disk drives
810 * g-m: machine implementation dependant drives
811 * n-p: network devices
812 * It's up to each machine implementation to check if the given boot
813 * devices match the actual hardware implementation and firmware
816 if (*p
< 'a' || *p
> 'p') {
817 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
820 if (bitmap
& (1 << (*p
- 'a'))) {
821 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
824 bitmap
|= 1 << (*p
- 'a');
828 static void restore_boot_devices(void *opaque
)
830 char *standard_boot_devices
= opaque
;
831 static int first
= 1;
833 /* Restore boot order and remove ourselves after the first boot */
839 qemu_boot_set(standard_boot_devices
);
841 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
842 g_free(standard_boot_devices
);
845 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
848 FWBootEntry
*node
, *i
;
854 assert(dev
!= NULL
|| suffix
!= NULL
);
856 node
= g_malloc0(sizeof(FWBootEntry
));
857 node
->bootindex
= bootindex
;
858 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
861 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
862 if (i
->bootindex
== bootindex
) {
863 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
865 } else if (i
->bootindex
< bootindex
) {
868 QTAILQ_INSERT_BEFORE(i
, node
, link
);
871 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
875 * This function returns null terminated string that consist of new line
876 * separated device paths.
878 * memory pointed by "size" is assigned total length of the array in bytes
881 char *get_boot_devices_list(uint32_t *size
)
887 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
888 char *devpath
= NULL
, *bootpath
;
892 devpath
= qdev_get_fw_dev_path(i
->dev
);
896 if (i
->suffix
&& devpath
) {
897 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
899 bootpath
= g_malloc(bootpathlen
);
900 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
902 } else if (devpath
) {
905 bootpath
= g_strdup(i
->suffix
);
910 list
[total
-1] = '\n';
912 len
= strlen(bootpath
) + 1;
913 list
= g_realloc(list
, total
+ len
);
914 memcpy(&list
[total
], bootpath
, len
);
924 static void numa_add(const char *optarg
)
928 unsigned long long value
, endvalue
;
931 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
932 if (!strcmp(option
, "node")) {
933 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
934 nodenr
= nb_numa_nodes
;
936 nodenr
= strtoull(option
, NULL
, 10);
939 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
940 node_mem
[nodenr
] = 0;
943 sval
= strtosz(option
, NULL
);
945 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
948 node_mem
[nodenr
] = sval
;
950 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
951 node_cpumask
[nodenr
] = 0;
953 value
= strtoull(option
, &endptr
, 10);
956 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
958 if (*endptr
== '-') {
959 endvalue
= strtoull(endptr
+1, &endptr
, 10);
960 if (endvalue
>= 63) {
963 "only 63 CPUs in NUMA mode supported.\n");
965 value
= (2ULL << endvalue
) - (1ULL << value
);
967 value
= 1ULL << value
;
970 node_cpumask
[nodenr
] = value
;
977 static void smp_parse(const char *optarg
)
979 int smp
, sockets
= 0, threads
= 0, cores
= 0;
983 smp
= strtoul(optarg
, &endptr
, 10);
984 if (endptr
!= optarg
) {
985 if (*endptr
== ',') {
989 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
990 sockets
= strtoull(option
, NULL
, 10);
991 if (get_param_value(option
, 128, "cores", endptr
) != 0)
992 cores
= strtoull(option
, NULL
, 10);
993 if (get_param_value(option
, 128, "threads", endptr
) != 0)
994 threads
= strtoull(option
, NULL
, 10);
995 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
996 max_cpus
= strtoull(option
, NULL
, 10);
998 /* compute missing values, prefer sockets over cores over threads */
999 if (smp
== 0 || sockets
== 0) {
1000 sockets
= sockets
> 0 ? sockets
: 1;
1001 cores
= cores
> 0 ? cores
: 1;
1002 threads
= threads
> 0 ? threads
: 1;
1004 smp
= cores
* threads
* sockets
;
1008 threads
= threads
> 0 ? threads
: 1;
1009 cores
= smp
/ (sockets
* threads
);
1011 threads
= smp
/ (cores
* sockets
);
1015 smp_cores
= cores
> 0 ? cores
: 1;
1016 smp_threads
= threads
> 0 ? threads
: 1;
1018 max_cpus
= smp_cpus
;
1021 /***********************************************************/
1024 static int usb_device_add(const char *devname
)
1027 USBDevice
*dev
= NULL
;
1032 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1033 dev
= usbdevice_create(devname
);
1037 /* the other ones */
1038 #ifndef CONFIG_LINUX
1039 /* only the linux version is qdev-ified, usb-bsd still needs this */
1040 if (strstart(devname
, "host:", &p
)) {
1041 dev
= usb_host_device_open(p
);
1044 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1045 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1046 bt_new_hci(qemu_find_bt_vlan(0)));
1057 static int usb_device_del(const char *devname
)
1062 if (strstart(devname
, "host:", &p
))
1063 return usb_host_device_close(p
);
1068 p
= strchr(devname
, '.');
1071 bus_num
= strtoul(devname
, NULL
, 0);
1072 addr
= strtoul(p
+ 1, NULL
, 0);
1074 return usb_device_delete_addr(bus_num
, addr
);
1077 static int usb_parse(const char *cmdline
)
1080 r
= usb_device_add(cmdline
);
1082 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1087 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1089 const char *devname
= qdict_get_str(qdict
, "devname");
1090 if (usb_device_add(devname
) < 0) {
1091 error_report("could not add USB device '%s'", devname
);
1095 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1097 const char *devname
= qdict_get_str(qdict
, "devname");
1098 if (usb_device_del(devname
) < 0) {
1099 error_report("could not delete USB device '%s'", devname
);
1103 /***********************************************************/
1104 /* PCMCIA/Cardbus */
1106 static struct pcmcia_socket_entry_s
{
1107 PCMCIASocket
*socket
;
1108 struct pcmcia_socket_entry_s
*next
;
1109 } *pcmcia_sockets
= 0;
1111 void pcmcia_socket_register(PCMCIASocket
*socket
)
1113 struct pcmcia_socket_entry_s
*entry
;
1115 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1116 entry
->socket
= socket
;
1117 entry
->next
= pcmcia_sockets
;
1118 pcmcia_sockets
= entry
;
1121 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1123 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1125 ptr
= &pcmcia_sockets
;
1126 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1127 if (entry
->socket
== socket
) {
1133 void pcmcia_info(Monitor
*mon
)
1135 struct pcmcia_socket_entry_s
*iter
;
1137 if (!pcmcia_sockets
)
1138 monitor_printf(mon
, "No PCMCIA sockets\n");
1140 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1141 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1142 iter
->socket
->attached
? iter
->socket
->card_string
:
1146 /***********************************************************/
1147 /* machine registration */
1149 static QEMUMachine
*first_machine
= NULL
;
1150 QEMUMachine
*current_machine
= NULL
;
1152 int qemu_register_machine(QEMUMachine
*m
)
1155 pm
= &first_machine
;
1163 static QEMUMachine
*find_machine(const char *name
)
1167 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1168 if (!strcmp(m
->name
, name
))
1170 if (m
->alias
&& !strcmp(m
->alias
, name
))
1176 static QEMUMachine
*find_default_machine(void)
1180 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1181 if (m
->is_default
) {
1188 /***********************************************************/
1189 /* main execution loop */
1191 static void gui_update(void *opaque
)
1193 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1194 DisplayState
*ds
= opaque
;
1195 DisplayChangeListener
*dcl
= ds
->listeners
;
1199 while (dcl
!= NULL
) {
1200 if (dcl
->gui_timer_interval
&&
1201 dcl
->gui_timer_interval
< interval
)
1202 interval
= dcl
->gui_timer_interval
;
1205 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1208 struct vm_change_state_entry
{
1209 VMChangeStateHandler
*cb
;
1211 QLIST_ENTRY (vm_change_state_entry
) entries
;
1214 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1216 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1219 VMChangeStateEntry
*e
;
1221 e
= g_malloc0(sizeof (*e
));
1225 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1229 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1231 QLIST_REMOVE (e
, entries
);
1235 void vm_state_notify(int running
, RunState state
)
1237 VMChangeStateEntry
*e
;
1239 trace_vm_state_notify(running
, state
);
1241 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1242 e
->cb(e
->opaque
, running
, state
);
1248 if (!runstate_is_running()) {
1250 runstate_set(RUN_STATE_RUNNING
);
1251 vm_state_notify(1, RUN_STATE_RUNNING
);
1253 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1257 /* reset/shutdown handler */
1259 typedef struct QEMUResetEntry
{
1260 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1261 QEMUResetHandler
*func
;
1265 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1266 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1267 static int reset_requested
;
1268 static int shutdown_requested
, shutdown_signal
= -1;
1269 static pid_t shutdown_pid
;
1270 static int powerdown_requested
;
1271 static int debug_requested
;
1272 static RunState vmstop_requested
= RUN_STATE_MAX
;
1274 int qemu_shutdown_requested_get(void)
1276 return shutdown_requested
;
1279 int qemu_reset_requested_get(void)
1281 return reset_requested
;
1284 int qemu_shutdown_requested(void)
1286 int r
= shutdown_requested
;
1287 shutdown_requested
= 0;
1291 void qemu_kill_report(void)
1293 if (shutdown_signal
!= -1) {
1294 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1295 if (shutdown_pid
== 0) {
1296 /* This happens for eg ^C at the terminal, so it's worth
1297 * avoiding printing an odd message in that case.
1299 fputc('\n', stderr
);
1301 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1303 shutdown_signal
= -1;
1307 int qemu_reset_requested(void)
1309 int r
= reset_requested
;
1310 reset_requested
= 0;
1314 int qemu_powerdown_requested(void)
1316 int r
= powerdown_requested
;
1317 powerdown_requested
= 0;
1321 static int qemu_debug_requested(void)
1323 int r
= debug_requested
;
1324 debug_requested
= 0;
1328 /* We use RUN_STATE_MAX but any invalid value will do */
1329 static bool qemu_vmstop_requested(RunState
*r
)
1331 if (vmstop_requested
< RUN_STATE_MAX
) {
1332 *r
= vmstop_requested
;
1333 vmstop_requested
= RUN_STATE_MAX
;
1340 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1342 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1345 re
->opaque
= opaque
;
1346 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1349 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1353 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1354 if (re
->func
== func
&& re
->opaque
== opaque
) {
1355 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1362 void qemu_system_reset(bool report
)
1364 QEMUResetEntry
*re
, *nre
;
1366 /* reset all devices */
1367 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1368 re
->func(re
->opaque
);
1371 monitor_protocol_event(QEVENT_RESET
, NULL
);
1373 cpu_synchronize_all_post_reset();
1376 void qemu_system_reset_request(void)
1379 shutdown_requested
= 1;
1381 reset_requested
= 1;
1384 qemu_notify_event();
1387 void qemu_system_killed(int signal
, pid_t pid
)
1389 shutdown_signal
= signal
;
1392 qemu_system_shutdown_request();
1395 void qemu_system_shutdown_request(void)
1397 shutdown_requested
= 1;
1398 qemu_notify_event();
1401 void qemu_system_powerdown_request(void)
1403 powerdown_requested
= 1;
1404 qemu_notify_event();
1407 void qemu_system_debug_request(void)
1409 debug_requested
= 1;
1410 qemu_notify_event();
1413 void qemu_system_vmstop_request(RunState state
)
1415 vmstop_requested
= state
;
1416 qemu_notify_event();
1419 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1420 static int n_poll_fds
;
1421 static int max_priority
;
1423 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1424 fd_set
*xfds
, struct timeval
*tv
)
1426 GMainContext
*context
= g_main_context_default();
1428 int timeout
= 0, cur_timeout
;
1430 g_main_context_prepare(context
, &max_priority
);
1432 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1433 poll_fds
, ARRAY_SIZE(poll_fds
));
1434 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1436 for (i
= 0; i
< n_poll_fds
; i
++) {
1437 GPollFD
*p
= &poll_fds
[i
];
1439 if ((p
->events
& G_IO_IN
)) {
1440 FD_SET(p
->fd
, rfds
);
1441 *max_fd
= MAX(*max_fd
, p
->fd
);
1443 if ((p
->events
& G_IO_OUT
)) {
1444 FD_SET(p
->fd
, wfds
);
1445 *max_fd
= MAX(*max_fd
, p
->fd
);
1447 if ((p
->events
& G_IO_ERR
)) {
1448 FD_SET(p
->fd
, xfds
);
1449 *max_fd
= MAX(*max_fd
, p
->fd
);
1453 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1454 if (timeout
>= 0 && timeout
< cur_timeout
) {
1455 tv
->tv_sec
= timeout
/ 1000;
1456 tv
->tv_usec
= (timeout
% 1000) * 1000;
1460 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1463 GMainContext
*context
= g_main_context_default();
1468 for (i
= 0; i
< n_poll_fds
; i
++) {
1469 GPollFD
*p
= &poll_fds
[i
];
1471 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1472 p
->revents
|= G_IO_IN
;
1474 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1475 p
->revents
|= G_IO_OUT
;
1477 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1478 p
->revents
|= G_IO_ERR
;
1483 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1484 g_main_context_dispatch(context
);
1488 int main_loop_wait(int nonblocking
)
1490 fd_set rfds
, wfds
, xfds
;
1498 timeout
= qemu_calculate_timeout();
1499 qemu_bh_update_timeout(&timeout
);
1502 os_host_main_loop_wait(&timeout
);
1504 tv
.tv_sec
= timeout
/ 1000;
1505 tv
.tv_usec
= (timeout
% 1000) * 1000;
1507 /* poll any events */
1508 /* XXX: separate device handlers from system ones */
1514 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1515 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1516 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1519 qemu_mutex_unlock_iothread();
1522 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1525 qemu_mutex_lock_iothread();
1528 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1529 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1530 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1532 qemu_run_all_timers();
1534 /* Check bottom-halves last in case any of the earlier events triggered
1541 qemu_irq qemu_system_powerdown
;
1543 static void main_loop(void)
1546 int last_io
__attribute__ ((unused
)) = 0;
1547 #ifdef CONFIG_PROFILER
1552 qemu_main_loop_start();
1555 nonblocking
= !kvm_enabled() && last_io
> 0;
1556 #ifdef CONFIG_PROFILER
1557 ti
= profile_getclock();
1559 last_io
= main_loop_wait(nonblocking
);
1560 #ifdef CONFIG_PROFILER
1561 dev_time
+= profile_getclock() - ti
;
1564 if (qemu_debug_requested()) {
1565 vm_stop(RUN_STATE_DEBUG
);
1567 if (qemu_shutdown_requested()) {
1569 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1571 vm_stop(RUN_STATE_SHUTDOWN
);
1575 if (qemu_reset_requested()) {
1577 cpu_synchronize_all_states();
1578 qemu_system_reset(VMRESET_REPORT
);
1580 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1581 runstate_check(RUN_STATE_SHUTDOWN
)) {
1582 runstate_set(RUN_STATE_PAUSED
);
1585 if (qemu_powerdown_requested()) {
1586 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1587 qemu_irq_raise(qemu_system_powerdown
);
1589 if (qemu_vmstop_requested(&r
)) {
1597 static void version(void)
1599 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1602 static void help(int exitcode
)
1604 const char *options_help
=
1605 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1607 #define DEFHEADING(text) stringify(text) "\n"
1608 #include "qemu-options.def"
1614 printf("usage: %s [options] [disk_image]\n"
1616 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1619 "During emulation, the following keys are useful:\n"
1620 "ctrl-alt-f toggle full screen\n"
1621 "ctrl-alt-n switch to virtual console 'n'\n"
1622 "ctrl-alt toggle mouse and keyboard grab\n"
1624 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1630 #define HAS_ARG 0x0001
1632 typedef struct QEMUOption
{
1639 static const QEMUOption qemu_options
[] = {
1640 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1641 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1642 { option, opt_arg, opt_enum, arch_mask },
1643 #define DEFHEADING(text)
1644 #include "qemu-options.def"
1650 static void select_vgahw (const char *p
)
1655 vga_interface_type
= VGA_NONE
;
1656 if (strstart(p
, "std", &opts
)) {
1657 vga_interface_type
= VGA_STD
;
1658 } else if (strstart(p
, "cirrus", &opts
)) {
1659 vga_interface_type
= VGA_CIRRUS
;
1660 } else if (strstart(p
, "vmware", &opts
)) {
1661 vga_interface_type
= VGA_VMWARE
;
1662 } else if (strstart(p
, "xenfb", &opts
)) {
1663 vga_interface_type
= VGA_XENFB
;
1664 } else if (strstart(p
, "qxl", &opts
)) {
1665 vga_interface_type
= VGA_QXL
;
1666 } else if (!strstart(p
, "none", &opts
)) {
1668 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1672 const char *nextopt
;
1674 if (strstart(opts
, ",retrace=", &nextopt
)) {
1676 if (strstart(opts
, "dumb", &nextopt
))
1677 vga_retrace_method
= VGA_RETRACE_DUMB
;
1678 else if (strstart(opts
, "precise", &nextopt
))
1679 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1680 else goto invalid_vga
;
1681 } else goto invalid_vga
;
1686 static DisplayType
select_display(const char *p
)
1689 DisplayType display
= DT_DEFAULT
;
1691 if (strstart(p
, "sdl", &opts
)) {
1695 const char *nextopt
;
1697 if (strstart(opts
, ",frame=", &nextopt
)) {
1699 if (strstart(opts
, "on", &nextopt
)) {
1701 } else if (strstart(opts
, "off", &nextopt
)) {
1704 goto invalid_sdl_args
;
1706 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1708 if (strstart(opts
, "on", &nextopt
)) {
1710 } else if (strstart(opts
, "off", &nextopt
)) {
1713 goto invalid_sdl_args
;
1715 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1717 if (strstart(opts
, "on", &nextopt
)) {
1719 } else if (strstart(opts
, "off", &nextopt
)) {
1722 goto invalid_sdl_args
;
1724 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1726 if (strstart(opts
, "on", &nextopt
)) {
1728 } else if (strstart(opts
, "off", &nextopt
)) {
1731 goto invalid_sdl_args
;
1735 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1741 fprintf(stderr
, "SDL support is disabled\n");
1744 } else if (strstart(p
, "vnc", &opts
)) {
1749 const char *nextopt
;
1751 if (strstart(opts
, "=", &nextopt
)) {
1752 vnc_display
= nextopt
;
1756 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1760 fprintf(stderr
, "VNC support is disabled\n");
1763 } else if (strstart(p
, "curses", &opts
)) {
1764 #ifdef CONFIG_CURSES
1765 display
= DT_CURSES
;
1767 fprintf(stderr
, "Curses support is disabled\n");
1770 } else if (strstart(p
, "none", &opts
)) {
1773 fprintf(stderr
, "Unknown display type: %s\n", p
);
1780 static int balloon_parse(const char *arg
)
1784 if (strcmp(arg
, "none") == 0) {
1788 if (!strncmp(arg
, "virtio", 6)) {
1789 if (arg
[6] == ',') {
1790 /* have params -> parse them */
1791 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1795 /* create empty opts */
1796 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1798 qemu_opt_set(opts
, "driver", "virtio-balloon");
1805 char *qemu_find_file(int type
, const char *name
)
1811 /* If name contains path separators then try it as a straight path. */
1812 if ((strchr(name
, '/') || strchr(name
, '\\'))
1813 && access(name
, R_OK
) == 0) {
1814 return g_strdup(name
);
1817 case QEMU_FILE_TYPE_BIOS
:
1820 case QEMU_FILE_TYPE_KEYMAP
:
1821 subdir
= "keymaps/";
1826 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1827 buf
= g_malloc0(len
);
1828 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1829 if (access(buf
, R_OK
)) {
1836 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1838 return qdev_device_help(opts
);
1841 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1845 dev
= qdev_device_add(opts
);
1851 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1853 CharDriverState
*chr
;
1855 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1861 #ifdef CONFIG_VIRTFS
1862 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1865 ret
= qemu_fsdev_add(opts
);
1871 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1873 CharDriverState
*chr
;
1874 const char *chardev
;
1878 mode
= qemu_opt_get(opts
, "mode");
1882 if (strcmp(mode
, "readline") == 0) {
1883 flags
= MONITOR_USE_READLINE
;
1884 } else if (strcmp(mode
, "control") == 0) {
1885 flags
= MONITOR_USE_CONTROL
;
1887 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1891 if (qemu_opt_get_bool(opts
, "pretty", 0))
1892 flags
|= MONITOR_USE_PRETTY
;
1894 if (qemu_opt_get_bool(opts
, "default", 0))
1895 flags
|= MONITOR_IS_DEFAULT
;
1897 chardev
= qemu_opt_get(opts
, "chardev");
1898 chr
= qemu_chr_find(chardev
);
1900 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1904 monitor_init(chr
, flags
);
1908 static void monitor_parse(const char *optarg
, const char *mode
)
1910 static int monitor_device_index
= 0;
1916 if (strstart(optarg
, "chardev:", &p
)) {
1917 snprintf(label
, sizeof(label
), "%s", p
);
1919 snprintf(label
, sizeof(label
), "compat_monitor%d",
1920 monitor_device_index
);
1921 if (monitor_device_index
== 0) {
1924 opts
= qemu_chr_parse_compat(label
, optarg
);
1926 fprintf(stderr
, "parse error: %s\n", optarg
);
1931 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1933 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1936 qemu_opt_set(opts
, "mode", mode
);
1937 qemu_opt_set(opts
, "chardev", label
);
1939 qemu_opt_set(opts
, "default", "on");
1940 monitor_device_index
++;
1943 struct device_config
{
1945 DEV_USB
, /* -usbdevice */
1947 DEV_SERIAL
, /* -serial */
1948 DEV_PARALLEL
, /* -parallel */
1949 DEV_VIRTCON
, /* -virtioconsole */
1950 DEV_DEBUGCON
, /* -debugcon */
1952 const char *cmdline
;
1953 QTAILQ_ENTRY(device_config
) next
;
1955 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1957 static void add_device_config(int type
, const char *cmdline
)
1959 struct device_config
*conf
;
1961 conf
= g_malloc0(sizeof(*conf
));
1963 conf
->cmdline
= cmdline
;
1964 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1967 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1969 struct device_config
*conf
;
1972 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1973 if (conf
->type
!= type
)
1975 rc
= func(conf
->cmdline
);
1982 static int serial_parse(const char *devname
)
1984 static int index
= 0;
1987 if (strcmp(devname
, "none") == 0)
1989 if (index
== MAX_SERIAL_PORTS
) {
1990 fprintf(stderr
, "qemu: too many serial ports\n");
1993 snprintf(label
, sizeof(label
), "serial%d", index
);
1994 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1995 if (!serial_hds
[index
]) {
1996 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1997 devname
, strerror(errno
));
2004 static int parallel_parse(const char *devname
)
2006 static int index
= 0;
2009 if (strcmp(devname
, "none") == 0)
2011 if (index
== MAX_PARALLEL_PORTS
) {
2012 fprintf(stderr
, "qemu: too many parallel ports\n");
2015 snprintf(label
, sizeof(label
), "parallel%d", index
);
2016 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2017 if (!parallel_hds
[index
]) {
2018 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2019 devname
, strerror(errno
));
2026 static int virtcon_parse(const char *devname
)
2028 QemuOptsList
*device
= qemu_find_opts("device");
2029 static int index
= 0;
2031 QemuOpts
*bus_opts
, *dev_opts
;
2033 if (strcmp(devname
, "none") == 0)
2035 if (index
== MAX_VIRTIO_CONSOLES
) {
2036 fprintf(stderr
, "qemu: too many virtio consoles\n");
2040 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2041 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2043 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2044 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2046 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2047 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2048 if (!virtcon_hds
[index
]) {
2049 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2050 devname
, strerror(errno
));
2053 qemu_opt_set(dev_opts
, "chardev", label
);
2059 static int debugcon_parse(const char *devname
)
2063 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2066 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2068 fprintf(stderr
, "qemu: already have a debugcon device\n");
2071 qemu_opt_set(opts
, "driver", "isa-debugcon");
2072 qemu_opt_set(opts
, "chardev", "debugcon");
2076 static QEMUMachine
*machine_parse(const char *name
)
2078 QEMUMachine
*m
, *machine
= NULL
;
2081 machine
= find_machine(name
);
2086 printf("Supported machines are:\n");
2087 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2089 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2091 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2092 m
->is_default
? " (default)" : "");
2094 exit(!name
|| *name
!= '?');
2097 static int tcg_init(void)
2099 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2104 const char *opt_name
;
2106 int (*available
)(void);
2110 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2111 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2112 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2115 static int configure_accelerator(void)
2117 const char *p
= NULL
;
2120 bool accel_initalised
= 0;
2121 bool init_failed
= 0;
2123 QemuOptsList
*list
= qemu_find_opts("machine");
2124 if (!QTAILQ_EMPTY(&list
->head
)) {
2125 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2129 /* Use the default "accelerator", tcg */
2133 while (!accel_initalised
&& *p
!= '\0') {
2137 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2138 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2139 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2140 *(accel_list
[i
].allowed
) = 1;
2141 ret
= accel_list
[i
].init();
2144 if (!accel_list
[i
].available()) {
2145 printf("%s not supported for this target\n",
2146 accel_list
[i
].name
);
2148 fprintf(stderr
, "failed to initialize %s: %s\n",
2152 *(accel_list
[i
].allowed
) = 0;
2154 accel_initalised
= 1;
2159 if (i
== ARRAY_SIZE(accel_list
)) {
2160 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2164 if (!accel_initalised
) {
2165 fprintf(stderr
, "No accelerator found!\n");
2170 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2173 return !accel_initalised
;
2176 void qemu_add_exit_notifier(Notifier
*notify
)
2178 notifier_list_add(&exit_notifiers
, notify
);
2181 void qemu_remove_exit_notifier(Notifier
*notify
)
2183 notifier_list_remove(&exit_notifiers
, notify
);
2186 static void qemu_run_exit_notifiers(void)
2188 notifier_list_notify(&exit_notifiers
, NULL
);
2191 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2193 notifier_list_add(&machine_init_done_notifiers
, notify
);
2196 static void qemu_run_machine_init_done_notifiers(void)
2198 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2201 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2202 const char **poptarg
, int *poptind
)
2204 const QEMUOption
*popt
;
2205 int optind
= *poptind
;
2206 char *r
= argv
[optind
];
2209 loc_set_cmdline(argv
, optind
, 1);
2211 /* Treat --foo the same as -foo. */
2214 popt
= qemu_options
;
2217 error_report("invalid option");
2220 if (!strcmp(popt
->name
, r
+ 1))
2224 if (popt
->flags
& HAS_ARG
) {
2225 if (optind
>= argc
) {
2226 error_report("requires an argument");
2229 optarg
= argv
[optind
++];
2230 loc_set_cmdline(argv
, optind
- 2, 2);
2241 static gpointer
malloc_and_trace(gsize n_bytes
)
2243 void *ptr
= malloc(n_bytes
);
2244 trace_g_malloc(n_bytes
, ptr
);
2248 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2250 void *ptr
= realloc(mem
, n_bytes
);
2251 trace_g_realloc(mem
, n_bytes
, ptr
);
2255 static void free_and_trace(gpointer mem
)
2261 int main(int argc
, char **argv
, char **envp
)
2263 const char *gdbstub_dev
= NULL
;
2265 int snapshot
, linux_boot
;
2266 const char *icount_option
= NULL
;
2267 const char *initrd_filename
;
2268 const char *kernel_filename
, *kernel_cmdline
;
2269 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2271 DisplayChangeListener
*dcl
;
2272 int cyls
, heads
, secs
, translation
;
2273 QemuOpts
*hda_opts
= NULL
, *opts
;
2274 QemuOptsList
*olist
;
2277 const char *loadvm
= NULL
;
2278 QEMUMachine
*machine
;
2279 const char *cpu_model
;
2280 const char *pid_file
= NULL
;
2281 const char *incoming
= NULL
;
2283 int show_vnc_port
= 0;
2286 const char *log_mask
= NULL
;
2287 const char *log_file
= NULL
;
2288 GMemVTable mem_trace
= {
2289 .malloc
= malloc_and_trace
,
2290 .realloc
= realloc_and_trace
,
2291 .free
= free_and_trace
,
2293 const char *trace_events
= NULL
;
2294 const char *trace_file
= NULL
;
2296 atexit(qemu_run_exit_notifiers
);
2297 error_set_progname(argv
[0]);
2299 g_mem_set_vtable(&mem_trace
);
2300 g_thread_init(NULL
);
2306 qemu_cache_utils_init(envp
);
2308 QLIST_INIT (&vm_change_state_head
);
2309 os_setup_early_signal_handling();
2311 module_call_init(MODULE_INIT_MACHINE
);
2312 machine
= find_default_machine();
2314 initrd_filename
= NULL
;
2317 kernel_filename
= NULL
;
2318 kernel_cmdline
= "";
2319 cyls
= heads
= secs
= 0;
2320 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2322 for (i
= 0; i
< MAX_NODES
; i
++) {
2324 node_cpumask
[i
] = 0;
2332 /* first pass of option parsing */
2334 while (optind
< argc
) {
2335 if (argv
[optind
][0] != '-') {
2340 const QEMUOption
*popt
;
2342 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2343 switch (popt
->index
) {
2344 case QEMU_OPTION_nodefconfig
:
2354 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2355 if (ret
< 0 && ret
!= -ENOENT
) {
2359 ret
= qemu_read_config_file(arch_config_name
);
2360 if (ret
< 0 && ret
!= -ENOENT
) {
2366 /* second pass of option parsing */
2371 if (argv
[optind
][0] != '-') {
2372 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2374 const QEMUOption
*popt
;
2376 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2377 if (!(popt
->arch_mask
& arch_type
)) {
2378 printf("Option %s not supported for this target\n", popt
->name
);
2381 switch(popt
->index
) {
2383 machine
= machine_parse(optarg
);
2385 case QEMU_OPTION_cpu
:
2386 /* hw initialization will check this */
2387 if (*optarg
== '?') {
2388 list_cpus(stdout
, &fprintf
, optarg
);
2394 case QEMU_OPTION_initrd
:
2395 initrd_filename
= optarg
;
2397 case QEMU_OPTION_hda
:
2401 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2403 snprintf(buf
, sizeof(buf
),
2404 "%s,cyls=%d,heads=%d,secs=%d%s",
2405 HD_OPTS
, cyls
, heads
, secs
,
2406 translation
== BIOS_ATA_TRANSLATION_LBA
?
2408 translation
== BIOS_ATA_TRANSLATION_NONE
?
2409 ",trans=none" : "");
2410 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2413 case QEMU_OPTION_hdb
:
2414 case QEMU_OPTION_hdc
:
2415 case QEMU_OPTION_hdd
:
2416 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2419 case QEMU_OPTION_drive
:
2420 if (drive_def(optarg
) == NULL
) {
2424 case QEMU_OPTION_set
:
2425 if (qemu_set_option(optarg
) != 0)
2428 case QEMU_OPTION_global
:
2429 if (qemu_global_option(optarg
) != 0)
2432 case QEMU_OPTION_mtdblock
:
2433 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2435 case QEMU_OPTION_sd
:
2436 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2438 case QEMU_OPTION_pflash
:
2439 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2441 case QEMU_OPTION_snapshot
:
2444 case QEMU_OPTION_hdachs
:
2448 cyls
= strtol(p
, (char **)&p
, 0);
2449 if (cyls
< 1 || cyls
> 16383)
2454 heads
= strtol(p
, (char **)&p
, 0);
2455 if (heads
< 1 || heads
> 16)
2460 secs
= strtol(p
, (char **)&p
, 0);
2461 if (secs
< 1 || secs
> 63)
2465 if (!strcmp(p
, "none"))
2466 translation
= BIOS_ATA_TRANSLATION_NONE
;
2467 else if (!strcmp(p
, "lba"))
2468 translation
= BIOS_ATA_TRANSLATION_LBA
;
2469 else if (!strcmp(p
, "auto"))
2470 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2473 } else if (*p
!= '\0') {
2475 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2478 if (hda_opts
!= NULL
) {
2480 snprintf(num
, sizeof(num
), "%d", cyls
);
2481 qemu_opt_set(hda_opts
, "cyls", num
);
2482 snprintf(num
, sizeof(num
), "%d", heads
);
2483 qemu_opt_set(hda_opts
, "heads", num
);
2484 snprintf(num
, sizeof(num
), "%d", secs
);
2485 qemu_opt_set(hda_opts
, "secs", num
);
2486 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2487 qemu_opt_set(hda_opts
, "trans", "lba");
2488 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2489 qemu_opt_set(hda_opts
, "trans", "none");
2493 case QEMU_OPTION_numa
:
2494 if (nb_numa_nodes
>= MAX_NODES
) {
2495 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2500 case QEMU_OPTION_display
:
2501 display_type
= select_display(optarg
);
2503 case QEMU_OPTION_nographic
:
2504 display_type
= DT_NOGRAPHIC
;
2506 case QEMU_OPTION_curses
:
2507 #ifdef CONFIG_CURSES
2508 display_type
= DT_CURSES
;
2510 fprintf(stderr
, "Curses support is disabled\n");
2514 case QEMU_OPTION_portrait
:
2515 graphic_rotate
= 90;
2517 case QEMU_OPTION_rotate
:
2518 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2519 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2520 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2522 "qemu: only 90, 180, 270 deg rotation is available\n");
2526 case QEMU_OPTION_kernel
:
2527 kernel_filename
= optarg
;
2529 case QEMU_OPTION_append
:
2530 kernel_cmdline
= optarg
;
2532 case QEMU_OPTION_cdrom
:
2533 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2535 case QEMU_OPTION_boot
:
2537 static const char * const params
[] = {
2538 "order", "once", "menu",
2539 "splash", "splash-time", NULL
2541 char buf
[sizeof(boot_devices
)];
2542 char *standard_boot_devices
;
2545 if (!strchr(optarg
, '=')) {
2547 pstrcpy(buf
, sizeof(buf
), optarg
);
2548 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2550 "qemu: unknown boot parameter '%s' in '%s'\n",
2556 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2557 validate_bootdevices(buf
);
2558 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2561 if (get_param_value(buf
, sizeof(buf
),
2563 validate_bootdevices(buf
);
2564 standard_boot_devices
= g_strdup(boot_devices
);
2565 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2566 qemu_register_reset(restore_boot_devices
,
2567 standard_boot_devices
);
2569 if (get_param_value(buf
, sizeof(buf
),
2571 if (!strcmp(buf
, "on")) {
2573 } else if (!strcmp(buf
, "off")) {
2577 "qemu: invalid option value '%s'\n",
2582 qemu_opts_parse(qemu_find_opts("boot-opts"),
2587 case QEMU_OPTION_fda
:
2588 case QEMU_OPTION_fdb
:
2589 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2592 case QEMU_OPTION_no_fd_bootchk
:
2595 case QEMU_OPTION_netdev
:
2596 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2600 case QEMU_OPTION_net
:
2601 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2606 case QEMU_OPTION_tftp
:
2607 legacy_tftp_prefix
= optarg
;
2609 case QEMU_OPTION_bootp
:
2610 legacy_bootp_filename
= optarg
;
2612 case QEMU_OPTION_redir
:
2613 if (net_slirp_redir(optarg
) < 0)
2617 case QEMU_OPTION_bt
:
2618 add_device_config(DEV_BT
, optarg
);
2620 case QEMU_OPTION_audio_help
:
2621 if (!(audio_available())) {
2622 printf("Option %s not supported for this target\n", popt
->name
);
2628 case QEMU_OPTION_soundhw
:
2629 if (!(audio_available())) {
2630 printf("Option %s not supported for this target\n", popt
->name
);
2633 select_soundhw (optarg
);
2638 case QEMU_OPTION_version
:
2642 case QEMU_OPTION_m
: {
2645 value
= strtosz(optarg
, NULL
);
2647 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2651 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2652 fprintf(stderr
, "qemu: ram size too large\n");
2658 case QEMU_OPTION_mempath
:
2662 case QEMU_OPTION_mem_prealloc
:
2673 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2675 case QEMU_OPTION_gdb
:
2676 gdbstub_dev
= optarg
;
2681 case QEMU_OPTION_bios
:
2684 case QEMU_OPTION_singlestep
:
2691 keyboard_layout
= optarg
;
2693 case QEMU_OPTION_localtime
:
2696 case QEMU_OPTION_vga
:
2697 select_vgahw (optarg
);
2704 w
= strtol(p
, (char **)&p
, 10);
2707 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2713 h
= strtol(p
, (char **)&p
, 10);
2718 depth
= strtol(p
, (char **)&p
, 10);
2719 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2720 depth
!= 24 && depth
!= 32)
2722 } else if (*p
== '\0') {
2723 depth
= graphic_depth
;
2730 graphic_depth
= depth
;
2733 case QEMU_OPTION_echr
:
2736 term_escape_char
= strtol(optarg
, &r
, 0);
2738 printf("Bad argument to echr\n");
2741 case QEMU_OPTION_monitor
:
2742 monitor_parse(optarg
, "readline");
2743 default_monitor
= 0;
2745 case QEMU_OPTION_qmp
:
2746 monitor_parse(optarg
, "control");
2747 default_monitor
= 0;
2749 case QEMU_OPTION_mon
:
2750 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2754 default_monitor
= 0;
2756 case QEMU_OPTION_chardev
:
2757 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2762 case QEMU_OPTION_fsdev
:
2763 olist
= qemu_find_opts("fsdev");
2765 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2768 opts
= qemu_opts_parse(olist
, optarg
, 1);
2770 fprintf(stderr
, "parse error: %s\n", optarg
);
2774 case QEMU_OPTION_virtfs
: {
2778 olist
= qemu_find_opts("virtfs");
2780 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2783 opts
= qemu_opts_parse(olist
, optarg
, 1);
2785 fprintf(stderr
, "parse error: %s\n", optarg
);
2789 if (qemu_opt_get(opts
, "fstype") == NULL
||
2790 qemu_opt_get(opts
, "mount_tag") == NULL
||
2791 qemu_opt_get(opts
, "path") == NULL
||
2792 qemu_opt_get(opts
, "security_model") == NULL
) {
2793 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2794 "security_model=[mapped|passthrough|none],"
2795 "mount_tag=tag.\n");
2799 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2800 qemu_opt_get(opts
, "mount_tag"), 1);
2802 fprintf(stderr
, "duplicate fsdev id: %s\n",
2803 qemu_opt_get(opts
, "mount_tag"));
2806 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2807 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2808 qemu_opt_set(fsdev
, "security_model",
2809 qemu_opt_get(opts
, "security_model"));
2811 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2812 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2813 qemu_opt_set(device
, "fsdev",
2814 qemu_opt_get(opts
, "mount_tag"));
2815 qemu_opt_set(device
, "mount_tag",
2816 qemu_opt_get(opts
, "mount_tag"));
2819 case QEMU_OPTION_serial
:
2820 add_device_config(DEV_SERIAL
, optarg
);
2822 if (strncmp(optarg
, "mon:", 4) == 0) {
2823 default_monitor
= 0;
2826 case QEMU_OPTION_watchdog
:
2829 "qemu: only one watchdog option may be given\n");
2834 case QEMU_OPTION_watchdog_action
:
2835 if (select_watchdog_action(optarg
) == -1) {
2836 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2840 case QEMU_OPTION_virtiocon
:
2841 add_device_config(DEV_VIRTCON
, optarg
);
2842 default_virtcon
= 0;
2843 if (strncmp(optarg
, "mon:", 4) == 0) {
2844 default_monitor
= 0;
2847 case QEMU_OPTION_parallel
:
2848 add_device_config(DEV_PARALLEL
, optarg
);
2849 default_parallel
= 0;
2850 if (strncmp(optarg
, "mon:", 4) == 0) {
2851 default_monitor
= 0;
2854 case QEMU_OPTION_debugcon
:
2855 add_device_config(DEV_DEBUGCON
, optarg
);
2857 case QEMU_OPTION_loadvm
:
2860 case QEMU_OPTION_full_screen
:
2864 case QEMU_OPTION_no_frame
:
2867 case QEMU_OPTION_alt_grab
:
2870 case QEMU_OPTION_ctrl_grab
:
2873 case QEMU_OPTION_no_quit
:
2876 case QEMU_OPTION_sdl
:
2877 display_type
= DT_SDL
;
2880 case QEMU_OPTION_no_frame
:
2881 case QEMU_OPTION_alt_grab
:
2882 case QEMU_OPTION_ctrl_grab
:
2883 case QEMU_OPTION_no_quit
:
2884 case QEMU_OPTION_sdl
:
2885 fprintf(stderr
, "SDL support is disabled\n");
2888 case QEMU_OPTION_pidfile
:
2891 case QEMU_OPTION_win2k_hack
:
2892 win2k_install_hack
= 1;
2894 case QEMU_OPTION_rtc_td_hack
:
2897 case QEMU_OPTION_acpitable
:
2898 do_acpitable_option(optarg
);
2900 case QEMU_OPTION_smbios
:
2901 do_smbios_option(optarg
);
2903 case QEMU_OPTION_enable_kvm
:
2904 olist
= qemu_find_opts("machine");
2905 qemu_opts_reset(olist
);
2906 qemu_opts_parse(olist
, "accel=kvm", 0);
2908 case QEMU_OPTION_machine
:
2909 olist
= qemu_find_opts("machine");
2910 qemu_opts_reset(olist
);
2911 opts
= qemu_opts_parse(olist
, optarg
, 1);
2913 fprintf(stderr
, "parse error: %s\n", optarg
);
2916 optarg
= qemu_opt_get(opts
, "type");
2918 machine
= machine_parse(optarg
);
2921 case QEMU_OPTION_usb
:
2924 case QEMU_OPTION_usbdevice
:
2926 add_device_config(DEV_USB
, optarg
);
2928 case QEMU_OPTION_device
:
2929 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2933 case QEMU_OPTION_smp
:
2936 fprintf(stderr
, "Invalid number of CPUs\n");
2939 if (max_cpus
< smp_cpus
) {
2940 fprintf(stderr
, "maxcpus must be equal to or greater than "
2944 if (max_cpus
> 255) {
2945 fprintf(stderr
, "Unsupported number of maxcpus\n");
2949 case QEMU_OPTION_vnc
:
2952 vnc_display
= optarg
;
2954 fprintf(stderr
, "VNC support is disabled\n");
2958 case QEMU_OPTION_no_acpi
:
2961 case QEMU_OPTION_no_hpet
:
2964 case QEMU_OPTION_balloon
:
2965 if (balloon_parse(optarg
) < 0) {
2966 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2970 case QEMU_OPTION_no_reboot
:
2973 case QEMU_OPTION_no_shutdown
:
2976 case QEMU_OPTION_show_cursor
:
2979 case QEMU_OPTION_uuid
:
2980 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2981 fprintf(stderr
, "Fail to parse UUID string."
2982 " Wrong format.\n");
2986 case QEMU_OPTION_option_rom
:
2987 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2988 fprintf(stderr
, "Too many option ROMs\n");
2991 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2992 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2993 option_rom
[nb_option_roms
].bootindex
=
2994 qemu_opt_get_number(opts
, "bootindex", -1);
2995 if (!option_rom
[nb_option_roms
].name
) {
2996 fprintf(stderr
, "Option ROM file is not specified\n");
3001 case QEMU_OPTION_semihosting
:
3002 semihosting_enabled
= 1;
3004 case QEMU_OPTION_name
:
3005 qemu_name
= g_strdup(optarg
);
3007 char *p
= strchr(qemu_name
, ',');
3010 if (strncmp(p
, "process=", 8)) {
3011 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3015 os_set_proc_name(p
);
3019 case QEMU_OPTION_prom_env
:
3020 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3021 fprintf(stderr
, "Too many prom variables\n");
3024 prom_envs
[nb_prom_envs
] = optarg
;
3027 case QEMU_OPTION_old_param
:
3030 case QEMU_OPTION_clock
:
3031 configure_alarms(optarg
);
3033 case QEMU_OPTION_startdate
:
3034 configure_rtc_date_offset(optarg
, 1);
3036 case QEMU_OPTION_rtc
:
3037 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3041 configure_rtc(opts
);
3043 case QEMU_OPTION_tb_size
:
3044 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3045 if (tcg_tb_size
< 0) {
3049 case QEMU_OPTION_icount
:
3050 icount_option
= optarg
;
3052 case QEMU_OPTION_incoming
:
3055 case QEMU_OPTION_nodefaults
:
3057 default_parallel
= 0;
3058 default_virtcon
= 0;
3059 default_monitor
= 0;
3066 case QEMU_OPTION_xen_domid
:
3067 if (!(xen_available())) {
3068 printf("Option %s not supported for this target\n", popt
->name
);
3071 xen_domid
= atoi(optarg
);
3073 case QEMU_OPTION_xen_create
:
3074 if (!(xen_available())) {
3075 printf("Option %s not supported for this target\n", popt
->name
);
3078 xen_mode
= XEN_CREATE
;
3080 case QEMU_OPTION_xen_attach
:
3081 if (!(xen_available())) {
3082 printf("Option %s not supported for this target\n", popt
->name
);
3085 xen_mode
= XEN_ATTACH
;
3087 case QEMU_OPTION_trace
:
3089 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3093 trace_events
= qemu_opt_get(opts
, "events");
3094 trace_file
= qemu_opt_get(opts
, "file");
3097 case QEMU_OPTION_readconfig
:
3099 int ret
= qemu_read_config_file(optarg
);
3101 fprintf(stderr
, "read config %s: %s\n", optarg
,
3107 case QEMU_OPTION_spice
:
3108 olist
= qemu_find_opts("spice");
3110 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3113 opts
= qemu_opts_parse(olist
, optarg
, 0);
3115 fprintf(stderr
, "parse error: %s\n", optarg
);
3119 case QEMU_OPTION_writeconfig
:
3122 if (strcmp(optarg
, "-") == 0) {
3125 fp
= fopen(optarg
, "w");
3127 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3131 qemu_config_write(fp
);
3136 os_parse_cmd_args(popt
->index
, optarg
);
3142 /* Open the logfile at this point, if necessary. We can't open the logfile
3143 * when encountering either of the logging options (-d or -D) because the
3144 * other one may be encountered later on the command line, changing the
3145 * location or level of logging.
3149 set_cpu_log_filename(log_file
);
3151 set_cpu_log(log_mask
);
3154 if (!trace_backend_init(trace_events
, trace_file
)) {
3158 /* If no data_dir is specified then try to find it relative to the
3161 data_dir
= os_find_datadir(argv
[0]);
3163 /* If all else fails use the install path specified when building. */
3165 data_dir
= CONFIG_QEMU_DATADIR
;
3169 * Default to max_cpus = smp_cpus, in case the user doesn't
3170 * specify a max_cpus value.
3173 max_cpus
= smp_cpus
;
3175 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3176 if (smp_cpus
> machine
->max_cpus
) {
3177 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3178 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3184 * Get the default machine options from the machine if it is not already
3185 * specified either by the configuration file or by the command line.
3187 if (machine
->default_machine_opts
) {
3188 QemuOptsList
*list
= qemu_find_opts("machine");
3189 const char *p
= NULL
;
3191 if (!QTAILQ_EMPTY(&list
->head
)) {
3192 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3195 qemu_opts_reset(list
);
3196 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3198 fprintf(stderr
, "parse error for machine %s: %s\n",
3199 machine
->name
, machine
->default_machine_opts
);
3205 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3206 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3208 if (machine
->no_serial
) {
3211 if (machine
->no_parallel
) {
3212 default_parallel
= 0;
3214 if (!machine
->use_virtcon
) {
3215 default_virtcon
= 0;
3217 if (machine
->no_vga
) {
3220 if (machine
->no_floppy
) {
3223 if (machine
->no_cdrom
) {
3226 if (machine
->no_sdcard
) {
3230 if (display_type
== DT_NOGRAPHIC
) {
3231 if (default_parallel
)
3232 add_device_config(DEV_PARALLEL
, "null");
3233 if (default_serial
&& default_monitor
) {
3234 add_device_config(DEV_SERIAL
, "mon:stdio");
3235 } else if (default_virtcon
&& default_monitor
) {
3236 add_device_config(DEV_VIRTCON
, "mon:stdio");
3239 add_device_config(DEV_SERIAL
, "stdio");
3240 if (default_virtcon
)
3241 add_device_config(DEV_VIRTCON
, "stdio");
3242 if (default_monitor
)
3243 monitor_parse("stdio", "readline");
3247 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3248 if (default_parallel
)
3249 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3250 if (default_monitor
)
3251 monitor_parse("vc:80Cx24C", "readline");
3252 if (default_virtcon
)
3253 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3256 vga_interface_type
= VGA_CIRRUS
;
3260 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3262 #ifdef CONFIG_VIRTFS
3263 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3270 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3275 /* init the memory */
3276 if (ram_size
== 0) {
3277 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3280 configure_accelerator();
3282 if (qemu_init_main_loop()) {
3283 fprintf(stderr
, "qemu_init_main_loop failed\n");
3286 linux_boot
= (kernel_filename
!= NULL
);
3288 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3289 fprintf(stderr
, "-append only allowed with -kernel option\n");
3293 if (!linux_boot
&& initrd_filename
!= NULL
) {
3294 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3298 os_set_line_buffering();
3300 if (init_timer_alarm() < 0) {
3301 fprintf(stderr
, "could not initialize alarm timer\n");
3304 configure_icount(icount_option
);
3306 if (net_init_clients() < 0) {
3310 /* init the bluetooth world */
3311 if (foreach_device_config(DEV_BT
, bt_parse
))
3314 if (!xen_enabled()) {
3315 /* On 32-bit hosts, QEMU is limited by virtual address space */
3316 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3317 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3322 cpu_exec_init_all();
3324 bdrv_init_with_whitelist();
3328 /* open the virtual block devices */
3330 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3331 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3334 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3335 IF_DEFAULT
, 2, CDROM_OPTS
);
3336 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3337 IF_FLOPPY
, 0, FD_OPTS
);
3338 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3341 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3344 if (nb_numa_nodes
> 0) {
3347 if (nb_numa_nodes
> MAX_NODES
) {
3348 nb_numa_nodes
= MAX_NODES
;
3351 /* If no memory size if given for any node, assume the default case
3352 * and distribute the available memory equally across all nodes
3354 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3355 if (node_mem
[i
] != 0)
3358 if (i
== nb_numa_nodes
) {
3359 uint64_t usedmem
= 0;
3361 /* On Linux, the each node's border has to be 8MB aligned,
3362 * the final node gets the rest.
3364 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3365 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3366 usedmem
+= node_mem
[i
];
3368 node_mem
[i
] = ram_size
- usedmem
;
3371 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3372 if (node_cpumask
[i
] != 0)
3375 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3376 * must cope with this anyway, because there are BIOSes out there in
3377 * real machines which also use this scheme.
3379 if (i
== nb_numa_nodes
) {
3380 for (i
= 0; i
< smp_cpus
; i
++) {
3381 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3386 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3390 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3392 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3394 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3396 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3399 module_call_init(MODULE_INIT_DEVICE
);
3401 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3405 i
= select_watchdog(watchdog
);
3407 exit (i
== 1 ? 1 : 0);
3410 if (machine
->compat_props
) {
3411 qdev_prop_register_global_list(machine
->compat_props
);
3415 machine
->init(ram_size
, boot_devices
,
3416 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3418 cpu_synchronize_all_post_init();
3422 current_machine
= machine
;
3424 /* init USB devices */
3426 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3430 /* init generic devices */
3431 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3434 net_check_clients();
3436 /* just use the first displaystate for the moment */
3437 ds
= get_displaystate();
3441 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3442 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3443 display_type
= DT_SDL
;
3444 #elif defined(CONFIG_VNC)
3445 vnc_display
= "localhost:0,to=99";
3448 display_type
= DT_NONE
;
3453 /* init local displays */
3454 switch (display_type
) {
3457 #if defined(CONFIG_CURSES)
3459 curses_display_init(ds
, full_screen
);
3462 #if defined(CONFIG_SDL)
3464 sdl_display_init(ds
, full_screen
, no_frame
);
3466 #elif defined(CONFIG_COCOA)
3468 cocoa_display_init(ds
, full_screen
);
3475 /* must be after terminal init, SDL library changes signal handlers */
3476 os_setup_signal_handling();
3479 /* init remote displays */
3481 vnc_display_init(ds
);
3482 if (vnc_display_open(ds
, vnc_display
) < 0)
3485 if (show_vnc_port
) {
3486 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3491 if (using_spice
&& !qxl_enabled
) {
3492 qemu_spice_display_init(ds
);
3498 dcl
= ds
->listeners
;
3499 while (dcl
!= NULL
) {
3500 if (dcl
->dpy_refresh
!= NULL
) {
3501 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3502 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3507 text_consoles_set_display(ds
);
3509 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3510 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3515 qdev_machine_creation_done();
3517 if (rom_load_all() != 0) {
3518 fprintf(stderr
, "rom loading failed\n");
3522 /* TODO: once all bus devices are qdevified, this should be done
3523 * when bus is created by qdev.c */
3524 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3525 qemu_run_machine_init_done_notifiers();
3527 qemu_system_reset(VMRESET_SILENT
);
3529 if (load_vmstate(loadvm
) < 0) {
3535 runstate_set(RUN_STATE_INMIGRATE
);
3536 int ret
= qemu_start_incoming_migration(incoming
);
3538 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3542 } else if (autostart
) {