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"
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 const char *watchdog
;
221 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
224 int time_drift_fix
= 0;
225 unsigned int kvm_shadow_memory
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
232 const char *nvram
= NULL
;
234 uint8_t *boot_splash_filedata
;
235 int boot_splash_filedata_size
;
236 uint8_t qemu_extra_params_fw
[2];
238 typedef struct FWBootEntry FWBootEntry
;
241 QTAILQ_ENTRY(FWBootEntry
) link
;
247 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
250 uint64_t node_mem
[MAX_NODES
];
251 uint64_t node_cpumask
[MAX_NODES
];
253 static QEMUTimer
*nographic_timer
;
255 uint8_t qemu_uuid
[16];
257 static QEMUBootSetHandler
*boot_set_handler
;
258 static void *boot_set_opaque
;
260 static NotifierList exit_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
263 static NotifierList machine_init_done_notifiers
=
264 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
266 static int tcg_allowed
= 1;
270 enum xen_mode xen_mode
= XEN_EMULATE
;
271 static int tcg_tb_size
;
273 static int default_serial
= 1;
274 static int default_parallel
= 1;
275 static int default_virtcon
= 1;
276 static int default_monitor
= 1;
277 static int default_vga
= 1;
278 static int default_floppy
= 1;
279 static int default_cdrom
= 1;
280 static int default_sdcard
= 1;
286 { .driver
= "isa-serial", .flag
= &default_serial
},
287 { .driver
= "isa-parallel", .flag
= &default_parallel
},
288 { .driver
= "isa-fdc", .flag
= &default_floppy
},
289 { .driver
= "ide-cd", .flag
= &default_cdrom
},
290 { .driver
= "ide-hd", .flag
= &default_cdrom
},
291 { .driver
= "ide-drive", .flag
= &default_cdrom
},
292 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
293 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
294 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
295 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
296 { .driver
= "VGA", .flag
= &default_vga
},
297 { .driver
= "cirrus-vga", .flag
= &default_vga
},
298 { .driver
= "vmware-svga", .flag
= &default_vga
},
299 { .driver
= "isa-vga", .flag
= &default_vga
},
300 { .driver
= "qxl-vga", .flag
= &default_vga
},
303 static void res_free(void)
305 if (boot_splash_filedata
!= NULL
) {
306 g_free(boot_splash_filedata
);
307 boot_splash_filedata
= NULL
;
311 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
313 const char *driver
= qemu_opt_get(opts
, "driver");
318 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
319 if (strcmp(default_list
[i
].driver
, driver
) != 0)
321 *(default_list
[i
].flag
) = 0;
326 /***********************************************************/
329 static RunState current_run_state
= RSTATE_NO_STATE
;
334 } RunStateTransition
;
336 static const RunStateTransition runstate_transitions_def
[] = {
338 { RSTATE_NO_STATE
, RSTATE_RUNNING
},
339 { RSTATE_NO_STATE
, RSTATE_IN_MIGRATE
},
340 { RSTATE_NO_STATE
, RSTATE_PRE_LAUNCH
},
342 { RSTATE_DEBUG
, RSTATE_RUNNING
},
344 { RSTATE_IN_MIGRATE
, RSTATE_RUNNING
},
345 { RSTATE_IN_MIGRATE
, RSTATE_PRE_LAUNCH
},
347 { RSTATE_PANICKED
, RSTATE_PAUSED
},
349 { RSTATE_IO_ERROR
, RSTATE_RUNNING
},
351 { RSTATE_PAUSED
, RSTATE_RUNNING
},
353 { RSTATE_POST_MIGRATE
, RSTATE_RUNNING
},
355 { RSTATE_PRE_LAUNCH
, RSTATE_RUNNING
},
356 { RSTATE_PRE_LAUNCH
, RSTATE_POST_MIGRATE
},
358 { RSTATE_PRE_MIGRATE
, RSTATE_RUNNING
},
359 { RSTATE_PRE_MIGRATE
, RSTATE_POST_MIGRATE
},
361 { RSTATE_RESTORE
, RSTATE_RUNNING
},
363 { RSTATE_RUNNING
, RSTATE_DEBUG
},
364 { RSTATE_RUNNING
, RSTATE_PANICKED
},
365 { RSTATE_RUNNING
, RSTATE_IO_ERROR
},
366 { RSTATE_RUNNING
, RSTATE_PAUSED
},
367 { RSTATE_RUNNING
, RSTATE_PRE_MIGRATE
},
368 { RSTATE_RUNNING
, RSTATE_RESTORE
},
369 { RSTATE_RUNNING
, RSTATE_SAVEVM
},
370 { RSTATE_RUNNING
, RSTATE_SHUTDOWN
},
371 { RSTATE_RUNNING
, RSTATE_WATCHDOG
},
373 { RSTATE_SAVEVM
, RSTATE_RUNNING
},
375 { RSTATE_SHUTDOWN
, RSTATE_PAUSED
},
377 { RSTATE_WATCHDOG
, RSTATE_RUNNING
},
379 { RSTATE_MAX
, RSTATE_MAX
},
382 static bool runstate_valid_transitions
[RSTATE_MAX
][RSTATE_MAX
];
384 static const char *const runstate_name_tbl
[RSTATE_MAX
] = {
385 [RSTATE_DEBUG
] = "debug",
386 [RSTATE_IN_MIGRATE
] = "incoming-migration",
387 [RSTATE_PANICKED
] = "internal-error",
388 [RSTATE_IO_ERROR
] = "io-error",
389 [RSTATE_PAUSED
] = "paused",
390 [RSTATE_POST_MIGRATE
] = "post-migrate",
391 [RSTATE_PRE_LAUNCH
] = "prelaunch",
392 [RSTATE_PRE_MIGRATE
] = "finish-migrate",
393 [RSTATE_RESTORE
] = "restore-vm",
394 [RSTATE_RUNNING
] = "running",
395 [RSTATE_SAVEVM
] = "save-vm",
396 [RSTATE_SHUTDOWN
] = "shutdown",
397 [RSTATE_WATCHDOG
] = "watchdog",
400 bool runstate_check(RunState state
)
402 return current_run_state
== state
;
405 void runstate_init(void)
407 const RunStateTransition
*p
;
409 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
411 for (p
= &runstate_transitions_def
[0]; p
->from
!= RSTATE_MAX
; p
++) {
412 runstate_valid_transitions
[p
->from
][p
->to
] = true;
416 /* This function will abort() on invalid state transitions */
417 void runstate_set(RunState new_state
)
419 if (new_state
>= RSTATE_MAX
||
420 !runstate_valid_transitions
[current_run_state
][new_state
]) {
421 fprintf(stderr
, "invalid runstate transition\n");
425 current_run_state
= new_state
;
428 const char *runstate_as_string(void)
430 assert(current_run_state
> RSTATE_NO_STATE
&&
431 current_run_state
< RSTATE_MAX
);
432 return runstate_name_tbl
[current_run_state
];
435 int runstate_is_running(void)
437 return runstate_check(RSTATE_RUNNING
);
440 /***********************************************************/
441 /* real time host monotonic timer */
443 /***********************************************************/
444 /* host time/date access */
445 void qemu_get_timedate(struct tm
*tm
, int offset
)
452 if (rtc_date_offset
== -1) {
456 ret
= localtime(&ti
);
458 ti
-= rtc_date_offset
;
462 memcpy(tm
, ret
, sizeof(struct tm
));
465 int qemu_timedate_diff(struct tm
*tm
)
469 if (rtc_date_offset
== -1)
471 seconds
= mktimegm(tm
);
473 seconds
= mktime(tm
);
475 seconds
= mktimegm(tm
) + rtc_date_offset
;
477 return seconds
- time(NULL
);
480 void rtc_change_mon_event(struct tm
*tm
)
484 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
485 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
486 qobject_decref(data
);
489 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
491 time_t rtc_start_date
;
494 if (!strcmp(startdate
, "now") && legacy
) {
495 rtc_date_offset
= -1;
497 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
505 } else if (sscanf(startdate
, "%d-%d-%d",
517 rtc_start_date
= mktimegm(&tm
);
518 if (rtc_start_date
== -1) {
520 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
521 "'2006-06-17T16:01:21' or '2006-06-17'\n");
524 rtc_date_offset
= time(NULL
) - rtc_start_date
;
528 static void configure_rtc(QemuOpts
*opts
)
532 value
= qemu_opt_get(opts
, "base");
534 if (!strcmp(value
, "utc")) {
536 } else if (!strcmp(value
, "localtime")) {
539 configure_rtc_date_offset(value
, 0);
542 value
= qemu_opt_get(opts
, "clock");
544 if (!strcmp(value
, "host")) {
545 rtc_clock
= host_clock
;
546 } else if (!strcmp(value
, "vm")) {
547 rtc_clock
= vm_clock
;
549 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
553 value
= qemu_opt_get(opts
, "driftfix");
555 if (!strcmp(value
, "slew")) {
557 } else if (!strcmp(value
, "none")) {
560 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
566 /***********************************************************/
567 /* Bluetooth support */
570 static struct HCIInfo
*hci_table
[MAX_NICS
];
572 static struct bt_vlan_s
{
573 struct bt_scatternet_s net
;
575 struct bt_vlan_s
*next
;
578 /* find or alloc a new bluetooth "VLAN" */
579 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
581 struct bt_vlan_s
**pvlan
, *vlan
;
582 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
586 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
588 pvlan
= &first_bt_vlan
;
589 while (*pvlan
!= NULL
)
590 pvlan
= &(*pvlan
)->next
;
595 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
599 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
604 static struct HCIInfo null_hci
= {
605 .cmd_send
= null_hci_send
,
606 .sco_send
= null_hci_send
,
607 .acl_send
= null_hci_send
,
608 .bdaddr_set
= null_hci_addr_set
,
611 struct HCIInfo
*qemu_next_hci(void)
613 if (cur_hci
== nb_hcis
)
616 return hci_table
[cur_hci
++];
619 static struct HCIInfo
*hci_init(const char *str
)
622 struct bt_scatternet_s
*vlan
= 0;
624 if (!strcmp(str
, "null"))
627 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
629 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
630 else if (!strncmp(str
, "hci", 3)) {
633 if (!strncmp(str
+ 3, ",vlan=", 6)) {
634 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
639 vlan
= qemu_find_bt_vlan(0);
641 return bt_new_hci(vlan
);
644 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
649 static int bt_hci_parse(const char *str
)
654 if (nb_hcis
>= MAX_NICS
) {
655 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
668 bdaddr
.b
[5] = 0x56 + nb_hcis
;
669 hci
->bdaddr_set(hci
, bdaddr
.b
);
671 hci_table
[nb_hcis
++] = hci
;
676 static void bt_vhci_add(int vlan_id
)
678 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
681 fprintf(stderr
, "qemu: warning: adding a VHCI to "
682 "an empty scatternet %i\n", vlan_id
);
684 bt_vhci_init(bt_new_hci(vlan
));
687 static struct bt_device_s
*bt_device_add(const char *opt
)
689 struct bt_scatternet_s
*vlan
;
691 char *endp
= strstr(opt
, ",vlan=");
692 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
695 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
698 vlan_id
= strtol(endp
+ 6, &endp
, 0);
700 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
705 vlan
= qemu_find_bt_vlan(vlan_id
);
708 fprintf(stderr
, "qemu: warning: adding a slave device to "
709 "an empty scatternet %i\n", vlan_id
);
711 if (!strcmp(devname
, "keyboard"))
712 return bt_keyboard_init(vlan
);
714 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
718 static int bt_parse(const char *opt
)
720 const char *endp
, *p
;
723 if (strstart(opt
, "hci", &endp
)) {
724 if (!*endp
|| *endp
== ',') {
726 if (!strstart(endp
, ",vlan=", 0))
729 return bt_hci_parse(opt
);
731 } else if (strstart(opt
, "vhci", &endp
)) {
732 if (!*endp
|| *endp
== ',') {
734 if (strstart(endp
, ",vlan=", &p
)) {
735 vlan
= strtol(p
, (char **) &endp
, 0);
737 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
741 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
750 } else if (strstart(opt
, "device:", &endp
))
751 return !bt_device_add(endp
);
753 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
757 /***********************************************************/
758 /* QEMU Block devices */
760 #define HD_OPTS "media=disk"
761 #define CDROM_OPTS "media=cdrom"
763 #define PFLASH_OPTS ""
767 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
769 int *use_scsi
= opaque
;
771 return drive_init(opts
, *use_scsi
) == NULL
;
774 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
776 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
777 qemu_opt_set(opts
, "snapshot", "on");
782 static void default_drive(int enable
, int snapshot
, int use_scsi
,
783 BlockInterfaceType type
, int index
,
788 if (type
== IF_DEFAULT
) {
789 type
= use_scsi
? IF_SCSI
: IF_IDE
;
792 if (!enable
|| drive_get_by_index(type
, index
)) {
796 opts
= drive_add(type
, index
, NULL
, optstr
);
798 drive_enable_snapshot(opts
, NULL
);
800 if (!drive_init(opts
, use_scsi
)) {
805 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
807 boot_set_handler
= func
;
808 boot_set_opaque
= opaque
;
811 int qemu_boot_set(const char *boot_devices
)
813 if (!boot_set_handler
) {
816 return boot_set_handler(boot_set_opaque
, boot_devices
);
819 static void validate_bootdevices(char *devices
)
821 /* We just do some generic consistency checks */
825 for (p
= devices
; *p
!= '\0'; p
++) {
826 /* Allowed boot devices are:
827 * a-b: floppy disk drives
828 * c-f: IDE disk drives
829 * g-m: machine implementation dependant drives
830 * n-p: network devices
831 * It's up to each machine implementation to check if the given boot
832 * devices match the actual hardware implementation and firmware
835 if (*p
< 'a' || *p
> 'p') {
836 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
839 if (bitmap
& (1 << (*p
- 'a'))) {
840 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
843 bitmap
|= 1 << (*p
- 'a');
847 static void restore_boot_devices(void *opaque
)
849 char *standard_boot_devices
= opaque
;
850 static int first
= 1;
852 /* Restore boot order and remove ourselves after the first boot */
858 qemu_boot_set(standard_boot_devices
);
860 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
861 g_free(standard_boot_devices
);
864 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
867 FWBootEntry
*node
, *i
;
873 assert(dev
!= NULL
|| suffix
!= NULL
);
875 node
= g_malloc0(sizeof(FWBootEntry
));
876 node
->bootindex
= bootindex
;
877 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
880 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
881 if (i
->bootindex
== bootindex
) {
882 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
884 } else if (i
->bootindex
< bootindex
) {
887 QTAILQ_INSERT_BEFORE(i
, node
, link
);
890 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
894 * This function returns null terminated string that consist of new line
895 * separated device paths.
897 * memory pointed by "size" is assigned total length of the array in bytes
900 char *get_boot_devices_list(uint32_t *size
)
906 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
907 char *devpath
= NULL
, *bootpath
;
911 devpath
= qdev_get_fw_dev_path(i
->dev
);
915 if (i
->suffix
&& devpath
) {
916 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
918 bootpath
= g_malloc(bootpathlen
);
919 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
921 } else if (devpath
) {
924 bootpath
= g_strdup(i
->suffix
);
929 list
[total
-1] = '\n';
931 len
= strlen(bootpath
) + 1;
932 list
= g_realloc(list
, total
+ len
);
933 memcpy(&list
[total
], bootpath
, len
);
943 static void numa_add(const char *optarg
)
947 unsigned long long value
, endvalue
;
950 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
951 if (!strcmp(option
, "node")) {
952 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
953 nodenr
= nb_numa_nodes
;
955 nodenr
= strtoull(option
, NULL
, 10);
958 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
959 node_mem
[nodenr
] = 0;
962 sval
= strtosz(option
, NULL
);
964 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
967 node_mem
[nodenr
] = sval
;
969 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
970 node_cpumask
[nodenr
] = 0;
972 value
= strtoull(option
, &endptr
, 10);
975 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
977 if (*endptr
== '-') {
978 endvalue
= strtoull(endptr
+1, &endptr
, 10);
979 if (endvalue
>= 63) {
982 "only 63 CPUs in NUMA mode supported.\n");
984 value
= (2ULL << endvalue
) - (1ULL << value
);
986 value
= 1ULL << value
;
989 node_cpumask
[nodenr
] = value
;
996 static void smp_parse(const char *optarg
)
998 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1002 smp
= strtoul(optarg
, &endptr
, 10);
1003 if (endptr
!= optarg
) {
1004 if (*endptr
== ',') {
1008 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1009 sockets
= strtoull(option
, NULL
, 10);
1010 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1011 cores
= strtoull(option
, NULL
, 10);
1012 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1013 threads
= strtoull(option
, NULL
, 10);
1014 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1015 max_cpus
= strtoull(option
, NULL
, 10);
1017 /* compute missing values, prefer sockets over cores over threads */
1018 if (smp
== 0 || sockets
== 0) {
1019 sockets
= sockets
> 0 ? sockets
: 1;
1020 cores
= cores
> 0 ? cores
: 1;
1021 threads
= threads
> 0 ? threads
: 1;
1023 smp
= cores
* threads
* sockets
;
1027 threads
= threads
> 0 ? threads
: 1;
1028 cores
= smp
/ (sockets
* threads
);
1030 threads
= smp
/ (cores
* sockets
);
1034 smp_cores
= cores
> 0 ? cores
: 1;
1035 smp_threads
= threads
> 0 ? threads
: 1;
1037 max_cpus
= smp_cpus
;
1040 /***********************************************************/
1043 static int usb_device_add(const char *devname
)
1046 USBDevice
*dev
= NULL
;
1051 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1052 dev
= usbdevice_create(devname
);
1056 /* the other ones */
1057 #ifndef CONFIG_LINUX
1058 /* only the linux version is qdev-ified, usb-bsd still needs this */
1059 if (strstart(devname
, "host:", &p
)) {
1060 dev
= usb_host_device_open(p
);
1063 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1064 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1065 bt_new_hci(qemu_find_bt_vlan(0)));
1076 static int usb_device_del(const char *devname
)
1081 if (strstart(devname
, "host:", &p
))
1082 return usb_host_device_close(p
);
1087 p
= strchr(devname
, '.');
1090 bus_num
= strtoul(devname
, NULL
, 0);
1091 addr
= strtoul(p
+ 1, NULL
, 0);
1093 return usb_device_delete_addr(bus_num
, addr
);
1096 static int usb_parse(const char *cmdline
)
1099 r
= usb_device_add(cmdline
);
1101 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1106 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1108 const char *devname
= qdict_get_str(qdict
, "devname");
1109 if (usb_device_add(devname
) < 0) {
1110 error_report("could not add USB device '%s'", devname
);
1114 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1116 const char *devname
= qdict_get_str(qdict
, "devname");
1117 if (usb_device_del(devname
) < 0) {
1118 error_report("could not delete USB device '%s'", devname
);
1122 /***********************************************************/
1123 /* PCMCIA/Cardbus */
1125 static struct pcmcia_socket_entry_s
{
1126 PCMCIASocket
*socket
;
1127 struct pcmcia_socket_entry_s
*next
;
1128 } *pcmcia_sockets
= 0;
1130 void pcmcia_socket_register(PCMCIASocket
*socket
)
1132 struct pcmcia_socket_entry_s
*entry
;
1134 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1135 entry
->socket
= socket
;
1136 entry
->next
= pcmcia_sockets
;
1137 pcmcia_sockets
= entry
;
1140 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1142 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1144 ptr
= &pcmcia_sockets
;
1145 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1146 if (entry
->socket
== socket
) {
1152 void pcmcia_info(Monitor
*mon
)
1154 struct pcmcia_socket_entry_s
*iter
;
1156 if (!pcmcia_sockets
)
1157 monitor_printf(mon
, "No PCMCIA sockets\n");
1159 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1160 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1161 iter
->socket
->attached
? iter
->socket
->card_string
:
1165 /***********************************************************/
1166 /* machine registration */
1168 static QEMUMachine
*first_machine
= NULL
;
1169 QEMUMachine
*current_machine
= NULL
;
1171 int qemu_register_machine(QEMUMachine
*m
)
1174 pm
= &first_machine
;
1182 static QEMUMachine
*find_machine(const char *name
)
1186 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1187 if (!strcmp(m
->name
, name
))
1189 if (m
->alias
&& !strcmp(m
->alias
, name
))
1195 static QEMUMachine
*find_default_machine(void)
1199 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1200 if (m
->is_default
) {
1207 /***********************************************************/
1208 /* main execution loop */
1210 static void gui_update(void *opaque
)
1212 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1213 DisplayState
*ds
= opaque
;
1214 DisplayChangeListener
*dcl
= ds
->listeners
;
1216 qemu_flush_coalesced_mmio_buffer();
1219 while (dcl
!= NULL
) {
1220 if (dcl
->gui_timer_interval
&&
1221 dcl
->gui_timer_interval
< interval
)
1222 interval
= dcl
->gui_timer_interval
;
1225 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1228 static void nographic_update(void *opaque
)
1230 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1232 qemu_flush_coalesced_mmio_buffer();
1233 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1236 struct vm_change_state_entry
{
1237 VMChangeStateHandler
*cb
;
1239 QLIST_ENTRY (vm_change_state_entry
) entries
;
1242 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1244 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1247 VMChangeStateEntry
*e
;
1249 e
= g_malloc0(sizeof (*e
));
1253 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1257 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1259 QLIST_REMOVE (e
, entries
);
1263 void vm_state_notify(int running
, RunState state
)
1265 VMChangeStateEntry
*e
;
1267 trace_vm_state_notify(running
, state
);
1269 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1270 e
->cb(e
->opaque
, running
, state
);
1276 if (!runstate_is_running()) {
1278 runstate_set(RSTATE_RUNNING
);
1279 vm_state_notify(1, RSTATE_RUNNING
);
1281 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1285 /* reset/shutdown handler */
1287 typedef struct QEMUResetEntry
{
1288 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1289 QEMUResetHandler
*func
;
1293 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1294 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1295 static int reset_requested
;
1296 static int shutdown_requested
, shutdown_signal
= -1;
1297 static pid_t shutdown_pid
;
1298 static int powerdown_requested
;
1299 static int debug_requested
;
1300 static RunState vmstop_requested
= RSTATE_NO_STATE
;
1302 int qemu_shutdown_requested_get(void)
1304 return shutdown_requested
;
1307 int qemu_reset_requested_get(void)
1309 return reset_requested
;
1312 int qemu_shutdown_requested(void)
1314 int r
= shutdown_requested
;
1315 shutdown_requested
= 0;
1319 void qemu_kill_report(void)
1321 if (shutdown_signal
!= -1) {
1322 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1323 if (shutdown_pid
== 0) {
1324 /* This happens for eg ^C at the terminal, so it's worth
1325 * avoiding printing an odd message in that case.
1327 fputc('\n', stderr
);
1329 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1331 shutdown_signal
= -1;
1335 int qemu_reset_requested(void)
1337 int r
= reset_requested
;
1338 reset_requested
= 0;
1342 int qemu_powerdown_requested(void)
1344 int r
= powerdown_requested
;
1345 powerdown_requested
= 0;
1349 static int qemu_debug_requested(void)
1351 int r
= debug_requested
;
1352 debug_requested
= 0;
1356 static RunState
qemu_vmstop_requested(void)
1358 RunState s
= vmstop_requested
;
1359 vmstop_requested
= RSTATE_NO_STATE
;
1363 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1365 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1368 re
->opaque
= opaque
;
1369 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1372 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1376 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1377 if (re
->func
== func
&& re
->opaque
== opaque
) {
1378 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1385 void qemu_system_reset(bool report
)
1387 QEMUResetEntry
*re
, *nre
;
1389 /* reset all devices */
1390 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1391 re
->func(re
->opaque
);
1394 monitor_protocol_event(QEVENT_RESET
, NULL
);
1396 cpu_synchronize_all_post_reset();
1399 void qemu_system_reset_request(void)
1402 shutdown_requested
= 1;
1404 reset_requested
= 1;
1407 qemu_notify_event();
1410 void qemu_system_killed(int signal
, pid_t pid
)
1412 shutdown_signal
= signal
;
1415 qemu_system_shutdown_request();
1418 void qemu_system_shutdown_request(void)
1420 shutdown_requested
= 1;
1421 qemu_notify_event();
1424 void qemu_system_powerdown_request(void)
1426 powerdown_requested
= 1;
1427 qemu_notify_event();
1430 void qemu_system_debug_request(void)
1432 debug_requested
= 1;
1433 qemu_notify_event();
1436 void qemu_system_vmstop_request(RunState state
)
1438 vmstop_requested
= state
;
1439 qemu_notify_event();
1442 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1443 static int n_poll_fds
;
1444 static int max_priority
;
1446 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1447 fd_set
*xfds
, struct timeval
*tv
)
1449 GMainContext
*context
= g_main_context_default();
1451 int timeout
= 0, cur_timeout
;
1453 g_main_context_prepare(context
, &max_priority
);
1455 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1456 poll_fds
, ARRAY_SIZE(poll_fds
));
1457 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1459 for (i
= 0; i
< n_poll_fds
; i
++) {
1460 GPollFD
*p
= &poll_fds
[i
];
1462 if ((p
->events
& G_IO_IN
)) {
1463 FD_SET(p
->fd
, rfds
);
1464 *max_fd
= MAX(*max_fd
, p
->fd
);
1466 if ((p
->events
& G_IO_OUT
)) {
1467 FD_SET(p
->fd
, wfds
);
1468 *max_fd
= MAX(*max_fd
, p
->fd
);
1470 if ((p
->events
& G_IO_ERR
)) {
1471 FD_SET(p
->fd
, xfds
);
1472 *max_fd
= MAX(*max_fd
, p
->fd
);
1476 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1477 if (timeout
>= 0 && timeout
< cur_timeout
) {
1478 tv
->tv_sec
= timeout
/ 1000;
1479 tv
->tv_usec
= (timeout
% 1000) * 1000;
1483 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1486 GMainContext
*context
= g_main_context_default();
1491 for (i
= 0; i
< n_poll_fds
; i
++) {
1492 GPollFD
*p
= &poll_fds
[i
];
1494 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1495 p
->revents
|= G_IO_IN
;
1497 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1498 p
->revents
|= G_IO_OUT
;
1500 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1501 p
->revents
|= G_IO_ERR
;
1506 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1507 g_main_context_dispatch(context
);
1511 int main_loop_wait(int nonblocking
)
1513 fd_set rfds
, wfds
, xfds
;
1521 timeout
= qemu_calculate_timeout();
1522 qemu_bh_update_timeout(&timeout
);
1525 os_host_main_loop_wait(&timeout
);
1527 tv
.tv_sec
= timeout
/ 1000;
1528 tv
.tv_usec
= (timeout
% 1000) * 1000;
1530 /* poll any events */
1531 /* XXX: separate device handlers from system ones */
1537 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1538 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1539 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1542 qemu_mutex_unlock_iothread();
1545 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1548 qemu_mutex_lock_iothread();
1551 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1552 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1553 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1555 qemu_run_all_timers();
1557 /* Check bottom-halves last in case any of the earlier events triggered
1564 qemu_irq qemu_system_powerdown
;
1566 static void main_loop(void)
1569 int last_io
__attribute__ ((unused
)) = 0;
1570 #ifdef CONFIG_PROFILER
1575 qemu_main_loop_start();
1578 nonblocking
= !kvm_enabled() && last_io
> 0;
1579 #ifdef CONFIG_PROFILER
1580 ti
= profile_getclock();
1582 last_io
= main_loop_wait(nonblocking
);
1583 #ifdef CONFIG_PROFILER
1584 dev_time
+= profile_getclock() - ti
;
1587 if (qemu_debug_requested()) {
1588 vm_stop(RSTATE_DEBUG
);
1590 if (qemu_shutdown_requested()) {
1592 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1594 vm_stop(RSTATE_SHUTDOWN
);
1598 if (qemu_reset_requested()) {
1600 cpu_synchronize_all_states();
1601 qemu_system_reset(VMRESET_REPORT
);
1603 if (runstate_check(RSTATE_PANICKED
) ||
1604 runstate_check(RSTATE_SHUTDOWN
)) {
1605 runstate_set(RSTATE_PAUSED
);
1608 if (qemu_powerdown_requested()) {
1609 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1610 qemu_irq_raise(qemu_system_powerdown
);
1612 if ((r
= qemu_vmstop_requested())) {
1620 static void version(void)
1622 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1625 static void help(int exitcode
)
1627 const char *options_help
=
1628 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1630 #define DEFHEADING(text) stringify(text) "\n"
1631 #include "qemu-options.def"
1637 printf("usage: %s [options] [disk_image]\n"
1639 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1642 "During emulation, the following keys are useful:\n"
1643 "ctrl-alt-f toggle full screen\n"
1644 "ctrl-alt-n switch to virtual console 'n'\n"
1645 "ctrl-alt toggle mouse and keyboard grab\n"
1647 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1653 #define HAS_ARG 0x0001
1655 typedef struct QEMUOption
{
1662 static const QEMUOption qemu_options
[] = {
1663 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1664 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1665 { option, opt_arg, opt_enum, arch_mask },
1666 #define DEFHEADING(text)
1667 #include "qemu-options.def"
1673 static void select_vgahw (const char *p
)
1678 vga_interface_type
= VGA_NONE
;
1679 if (strstart(p
, "std", &opts
)) {
1680 vga_interface_type
= VGA_STD
;
1681 } else if (strstart(p
, "cirrus", &opts
)) {
1682 vga_interface_type
= VGA_CIRRUS
;
1683 } else if (strstart(p
, "vmware", &opts
)) {
1684 vga_interface_type
= VGA_VMWARE
;
1685 } else if (strstart(p
, "xenfb", &opts
)) {
1686 vga_interface_type
= VGA_XENFB
;
1687 } else if (strstart(p
, "qxl", &opts
)) {
1688 vga_interface_type
= VGA_QXL
;
1689 } else if (!strstart(p
, "none", &opts
)) {
1691 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1695 const char *nextopt
;
1697 if (strstart(opts
, ",retrace=", &nextopt
)) {
1699 if (strstart(opts
, "dumb", &nextopt
))
1700 vga_retrace_method
= VGA_RETRACE_DUMB
;
1701 else if (strstart(opts
, "precise", &nextopt
))
1702 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1703 else goto invalid_vga
;
1704 } else goto invalid_vga
;
1709 static DisplayType
select_display(const char *p
)
1712 DisplayType display
= DT_DEFAULT
;
1714 if (strstart(p
, "sdl", &opts
)) {
1718 const char *nextopt
;
1720 if (strstart(opts
, ",frame=", &nextopt
)) {
1722 if (strstart(opts
, "on", &nextopt
)) {
1724 } else if (strstart(opts
, "off", &nextopt
)) {
1727 goto invalid_sdl_args
;
1729 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1731 if (strstart(opts
, "on", &nextopt
)) {
1733 } else if (strstart(opts
, "off", &nextopt
)) {
1736 goto invalid_sdl_args
;
1738 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1740 if (strstart(opts
, "on", &nextopt
)) {
1742 } else if (strstart(opts
, "off", &nextopt
)) {
1745 goto invalid_sdl_args
;
1747 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1749 if (strstart(opts
, "on", &nextopt
)) {
1751 } else if (strstart(opts
, "off", &nextopt
)) {
1754 goto invalid_sdl_args
;
1758 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1764 fprintf(stderr
, "SDL support is disabled\n");
1767 } else if (strstart(p
, "vnc", &opts
)) {
1772 const char *nextopt
;
1774 if (strstart(opts
, "=", &nextopt
)) {
1775 vnc_display
= nextopt
;
1779 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1783 fprintf(stderr
, "VNC support is disabled\n");
1786 } else if (strstart(p
, "curses", &opts
)) {
1787 #ifdef CONFIG_CURSES
1788 display
= DT_CURSES
;
1790 fprintf(stderr
, "Curses support is disabled\n");
1793 } else if (strstart(p
, "none", &opts
)) {
1796 fprintf(stderr
, "Unknown display type: %s\n", p
);
1803 static int balloon_parse(const char *arg
)
1807 if (strcmp(arg
, "none") == 0) {
1811 if (!strncmp(arg
, "virtio", 6)) {
1812 if (arg
[6] == ',') {
1813 /* have params -> parse them */
1814 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1818 /* create empty opts */
1819 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1821 qemu_opt_set(opts
, "driver", "virtio-balloon");
1828 char *qemu_find_file(int type
, const char *name
)
1834 /* If name contains path separators then try it as a straight path. */
1835 if ((strchr(name
, '/') || strchr(name
, '\\'))
1836 && access(name
, R_OK
) == 0) {
1837 return g_strdup(name
);
1840 case QEMU_FILE_TYPE_BIOS
:
1843 case QEMU_FILE_TYPE_KEYMAP
:
1844 subdir
= "keymaps/";
1849 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1850 buf
= g_malloc0(len
);
1851 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1852 if (access(buf
, R_OK
)) {
1859 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1861 return qdev_device_help(opts
);
1864 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1868 dev
= qdev_device_add(opts
);
1874 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1876 CharDriverState
*chr
;
1878 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1884 #ifdef CONFIG_VIRTFS
1885 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1888 ret
= qemu_fsdev_add(opts
);
1894 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1896 CharDriverState
*chr
;
1897 const char *chardev
;
1901 mode
= qemu_opt_get(opts
, "mode");
1905 if (strcmp(mode
, "readline") == 0) {
1906 flags
= MONITOR_USE_READLINE
;
1907 } else if (strcmp(mode
, "control") == 0) {
1908 flags
= MONITOR_USE_CONTROL
;
1910 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1914 if (qemu_opt_get_bool(opts
, "pretty", 0))
1915 flags
|= MONITOR_USE_PRETTY
;
1917 if (qemu_opt_get_bool(opts
, "default", 0))
1918 flags
|= MONITOR_IS_DEFAULT
;
1920 chardev
= qemu_opt_get(opts
, "chardev");
1921 chr
= qemu_chr_find(chardev
);
1923 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1927 monitor_init(chr
, flags
);
1931 static void monitor_parse(const char *optarg
, const char *mode
)
1933 static int monitor_device_index
= 0;
1939 if (strstart(optarg
, "chardev:", &p
)) {
1940 snprintf(label
, sizeof(label
), "%s", p
);
1942 snprintf(label
, sizeof(label
), "compat_monitor%d",
1943 monitor_device_index
);
1944 if (monitor_device_index
== 0) {
1947 opts
= qemu_chr_parse_compat(label
, optarg
);
1949 fprintf(stderr
, "parse error: %s\n", optarg
);
1954 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1956 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1959 qemu_opt_set(opts
, "mode", mode
);
1960 qemu_opt_set(opts
, "chardev", label
);
1962 qemu_opt_set(opts
, "default", "on");
1963 monitor_device_index
++;
1966 struct device_config
{
1968 DEV_USB
, /* -usbdevice */
1970 DEV_SERIAL
, /* -serial */
1971 DEV_PARALLEL
, /* -parallel */
1972 DEV_VIRTCON
, /* -virtioconsole */
1973 DEV_DEBUGCON
, /* -debugcon */
1975 const char *cmdline
;
1976 QTAILQ_ENTRY(device_config
) next
;
1978 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1980 static void add_device_config(int type
, const char *cmdline
)
1982 struct device_config
*conf
;
1984 conf
= g_malloc0(sizeof(*conf
));
1986 conf
->cmdline
= cmdline
;
1987 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1990 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1992 struct device_config
*conf
;
1995 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1996 if (conf
->type
!= type
)
1998 rc
= func(conf
->cmdline
);
2005 static int serial_parse(const char *devname
)
2007 static int index
= 0;
2010 if (strcmp(devname
, "none") == 0)
2012 if (index
== MAX_SERIAL_PORTS
) {
2013 fprintf(stderr
, "qemu: too many serial ports\n");
2016 snprintf(label
, sizeof(label
), "serial%d", index
);
2017 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2018 if (!serial_hds
[index
]) {
2019 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2020 devname
, strerror(errno
));
2027 static int parallel_parse(const char *devname
)
2029 static int index
= 0;
2032 if (strcmp(devname
, "none") == 0)
2034 if (index
== MAX_PARALLEL_PORTS
) {
2035 fprintf(stderr
, "qemu: too many parallel ports\n");
2038 snprintf(label
, sizeof(label
), "parallel%d", index
);
2039 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2040 if (!parallel_hds
[index
]) {
2041 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2042 devname
, strerror(errno
));
2049 static int virtcon_parse(const char *devname
)
2051 QemuOptsList
*device
= qemu_find_opts("device");
2052 static int index
= 0;
2054 QemuOpts
*bus_opts
, *dev_opts
;
2056 if (strcmp(devname
, "none") == 0)
2058 if (index
== MAX_VIRTIO_CONSOLES
) {
2059 fprintf(stderr
, "qemu: too many virtio consoles\n");
2063 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2064 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2066 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2067 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2069 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2070 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2071 if (!virtcon_hds
[index
]) {
2072 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2073 devname
, strerror(errno
));
2076 qemu_opt_set(dev_opts
, "chardev", label
);
2082 static int debugcon_parse(const char *devname
)
2086 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2089 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2091 fprintf(stderr
, "qemu: already have a debugcon device\n");
2094 qemu_opt_set(opts
, "driver", "isa-debugcon");
2095 qemu_opt_set(opts
, "chardev", "debugcon");
2099 static QEMUMachine
*machine_parse(const char *name
)
2101 QEMUMachine
*m
, *machine
= NULL
;
2104 machine
= find_machine(name
);
2109 printf("Supported machines are:\n");
2110 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2112 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2114 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2115 m
->is_default
? " (default)" : "");
2117 exit(!name
|| *name
!= '?');
2120 static int tcg_init(void)
2122 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2127 const char *opt_name
;
2129 int (*available
)(void);
2133 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2134 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2135 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2138 static int configure_accelerator(void)
2140 const char *p
= NULL
;
2143 bool accel_initalised
= 0;
2144 bool init_failed
= 0;
2146 QemuOptsList
*list
= qemu_find_opts("machine");
2147 if (!QTAILQ_EMPTY(&list
->head
)) {
2148 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2153 /* Use the default "accelerator", kvm */
2156 /* Use the default "accelerator", tcg */
2161 while (!accel_initalised
&& *p
!= '\0') {
2165 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2166 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2167 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2168 *(accel_list
[i
].allowed
) = 1;
2169 ret
= accel_list
[i
].init();
2172 if (!accel_list
[i
].available()) {
2173 printf("%s not supported for this target\n",
2174 accel_list
[i
].name
);
2176 fprintf(stderr
, "failed to initialize %s: %s\n",
2180 *(accel_list
[i
].allowed
) = 0;
2182 accel_initalised
= 1;
2187 if (i
== ARRAY_SIZE(accel_list
)) {
2188 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2192 if (!accel_initalised
) {
2193 fprintf(stderr
, "No accelerator found!\n");
2198 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2201 return !accel_initalised
;
2204 void qemu_add_exit_notifier(Notifier
*notify
)
2206 notifier_list_add(&exit_notifiers
, notify
);
2209 void qemu_remove_exit_notifier(Notifier
*notify
)
2211 notifier_list_remove(&exit_notifiers
, notify
);
2214 static void qemu_run_exit_notifiers(void)
2216 notifier_list_notify(&exit_notifiers
, NULL
);
2219 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2221 notifier_list_add(&machine_init_done_notifiers
, notify
);
2224 static void qemu_run_machine_init_done_notifiers(void)
2226 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2229 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2230 const char **poptarg
, int *poptind
)
2232 const QEMUOption
*popt
;
2233 int optind
= *poptind
;
2234 char *r
= argv
[optind
];
2237 loc_set_cmdline(argv
, optind
, 1);
2239 /* Treat --foo the same as -foo. */
2242 popt
= qemu_options
;
2245 error_report("invalid option");
2248 if (!strcmp(popt
->name
, r
+ 1))
2252 if (popt
->flags
& HAS_ARG
) {
2253 if (optind
>= argc
) {
2254 error_report("requires an argument");
2257 optarg
= argv
[optind
++];
2258 loc_set_cmdline(argv
, optind
- 2, 2);
2269 static gpointer
malloc_and_trace(gsize n_bytes
)
2271 void *ptr
= malloc(n_bytes
);
2272 trace_g_malloc(n_bytes
, ptr
);
2276 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2278 void *ptr
= realloc(mem
, n_bytes
);
2279 trace_g_realloc(mem
, n_bytes
, ptr
);
2283 static void free_and_trace(gpointer mem
)
2289 int main(int argc
, char **argv
, char **envp
)
2291 const char *gdbstub_dev
= NULL
;
2293 int snapshot
, linux_boot
;
2294 const char *icount_option
= NULL
;
2295 const char *initrd_filename
;
2296 const char *kernel_filename
, *kernel_cmdline
;
2297 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2299 DisplayChangeListener
*dcl
;
2300 int cyls
, heads
, secs
, translation
;
2301 QemuOpts
*hda_opts
= NULL
, *opts
;
2302 QemuOptsList
*olist
;
2305 const char *loadvm
= NULL
;
2306 QEMUMachine
*machine
;
2307 const char *cpu_model
;
2308 const char *pid_file
= NULL
;
2309 const char *incoming
= NULL
;
2311 int show_vnc_port
= 0;
2314 const char *log_mask
= NULL
;
2315 const char *log_file
= NULL
;
2316 GMemVTable mem_trace
= {
2317 .malloc
= malloc_and_trace
,
2318 .realloc
= realloc_and_trace
,
2319 .free
= free_and_trace
,
2321 const char *trace_events
= NULL
;
2322 const char *trace_file
= NULL
;
2324 atexit(qemu_run_exit_notifiers
);
2325 error_set_progname(argv
[0]);
2327 g_mem_set_vtable(&mem_trace
);
2328 g_thread_init(NULL
);
2334 qemu_cache_utils_init(envp
);
2336 QLIST_INIT (&vm_change_state_head
);
2337 os_setup_early_signal_handling();
2339 module_call_init(MODULE_INIT_MACHINE
);
2340 machine
= find_default_machine();
2342 initrd_filename
= NULL
;
2345 kernel_filename
= NULL
;
2346 kernel_cmdline
= "";
2347 cyls
= heads
= secs
= 0;
2348 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2350 for (i
= 0; i
< MAX_NODES
; i
++) {
2352 node_cpumask
[i
] = 0;
2360 /* first pass of option parsing */
2362 while (optind
< argc
) {
2363 if (argv
[optind
][0] != '-') {
2368 const QEMUOption
*popt
;
2370 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2371 switch (popt
->index
) {
2372 case QEMU_OPTION_nodefconfig
:
2382 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2383 if (ret
< 0 && ret
!= -ENOENT
) {
2387 ret
= qemu_read_config_file(arch_config_name
);
2388 if (ret
< 0 && ret
!= -ENOENT
) {
2394 /* second pass of option parsing */
2399 if (argv
[optind
][0] != '-') {
2400 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2402 const QEMUOption
*popt
;
2404 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2405 if (!(popt
->arch_mask
& arch_type
)) {
2406 printf("Option %s not supported for this target\n", popt
->name
);
2409 switch(popt
->index
) {
2411 machine
= machine_parse(optarg
);
2413 case QEMU_OPTION_cpu
:
2414 /* hw initialization will check this */
2415 if (*optarg
== '?') {
2416 list_cpus(stdout
, &fprintf
, optarg
);
2422 case QEMU_OPTION_initrd
:
2423 initrd_filename
= optarg
;
2425 case QEMU_OPTION_hda
:
2429 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2431 snprintf(buf
, sizeof(buf
),
2432 "%s,cyls=%d,heads=%d,secs=%d%s",
2433 HD_OPTS
, cyls
, heads
, secs
,
2434 translation
== BIOS_ATA_TRANSLATION_LBA
?
2436 translation
== BIOS_ATA_TRANSLATION_NONE
?
2437 ",trans=none" : "");
2438 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2441 case QEMU_OPTION_hdb
:
2442 case QEMU_OPTION_hdc
:
2443 case QEMU_OPTION_hdd
:
2444 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2447 case QEMU_OPTION_drive
:
2448 if (drive_def(optarg
) == NULL
) {
2452 case QEMU_OPTION_set
:
2453 if (qemu_set_option(optarg
) != 0)
2456 case QEMU_OPTION_global
:
2457 if (qemu_global_option(optarg
) != 0)
2460 case QEMU_OPTION_mtdblock
:
2461 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2463 case QEMU_OPTION_sd
:
2464 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2466 case QEMU_OPTION_pflash
:
2467 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2469 case QEMU_OPTION_snapshot
:
2472 case QEMU_OPTION_hdachs
:
2476 cyls
= strtol(p
, (char **)&p
, 0);
2477 if (cyls
< 1 || cyls
> 16383)
2482 heads
= strtol(p
, (char **)&p
, 0);
2483 if (heads
< 1 || heads
> 16)
2488 secs
= strtol(p
, (char **)&p
, 0);
2489 if (secs
< 1 || secs
> 63)
2493 if (!strcmp(p
, "none"))
2494 translation
= BIOS_ATA_TRANSLATION_NONE
;
2495 else if (!strcmp(p
, "lba"))
2496 translation
= BIOS_ATA_TRANSLATION_LBA
;
2497 else if (!strcmp(p
, "auto"))
2498 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2501 } else if (*p
!= '\0') {
2503 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2506 if (hda_opts
!= NULL
) {
2508 snprintf(num
, sizeof(num
), "%d", cyls
);
2509 qemu_opt_set(hda_opts
, "cyls", num
);
2510 snprintf(num
, sizeof(num
), "%d", heads
);
2511 qemu_opt_set(hda_opts
, "heads", num
);
2512 snprintf(num
, sizeof(num
), "%d", secs
);
2513 qemu_opt_set(hda_opts
, "secs", num
);
2514 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2515 qemu_opt_set(hda_opts
, "trans", "lba");
2516 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2517 qemu_opt_set(hda_opts
, "trans", "none");
2521 case QEMU_OPTION_numa
:
2522 if (nb_numa_nodes
>= MAX_NODES
) {
2523 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2528 case QEMU_OPTION_display
:
2529 display_type
= select_display(optarg
);
2531 case QEMU_OPTION_nographic
:
2532 display_type
= DT_NOGRAPHIC
;
2534 case QEMU_OPTION_curses
:
2535 #ifdef CONFIG_CURSES
2536 display_type
= DT_CURSES
;
2538 fprintf(stderr
, "Curses support is disabled\n");
2542 case QEMU_OPTION_portrait
:
2543 graphic_rotate
= 90;
2545 case QEMU_OPTION_rotate
:
2546 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2547 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2548 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2550 "qemu: only 90, 180, 270 deg rotation is available\n");
2554 case QEMU_OPTION_kernel
:
2555 kernel_filename
= optarg
;
2557 case QEMU_OPTION_append
:
2558 kernel_cmdline
= optarg
;
2560 case QEMU_OPTION_cdrom
:
2561 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2563 case QEMU_OPTION_boot
:
2565 static const char * const params
[] = {
2566 "order", "once", "menu",
2567 "splash", "splash-time", NULL
2569 char buf
[sizeof(boot_devices
)];
2570 char *standard_boot_devices
;
2573 if (!strchr(optarg
, '=')) {
2575 pstrcpy(buf
, sizeof(buf
), optarg
);
2576 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2578 "qemu: unknown boot parameter '%s' in '%s'\n",
2584 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2585 validate_bootdevices(buf
);
2586 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2589 if (get_param_value(buf
, sizeof(buf
),
2591 validate_bootdevices(buf
);
2592 standard_boot_devices
= g_strdup(boot_devices
);
2593 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2594 qemu_register_reset(restore_boot_devices
,
2595 standard_boot_devices
);
2597 if (get_param_value(buf
, sizeof(buf
),
2599 if (!strcmp(buf
, "on")) {
2601 } else if (!strcmp(buf
, "off")) {
2605 "qemu: invalid option value '%s'\n",
2610 qemu_opts_parse(qemu_find_opts("boot-opts"),
2615 case QEMU_OPTION_fda
:
2616 case QEMU_OPTION_fdb
:
2617 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2620 case QEMU_OPTION_no_fd_bootchk
:
2623 case QEMU_OPTION_netdev
:
2624 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2628 case QEMU_OPTION_net
:
2629 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2634 case QEMU_OPTION_tftp
:
2635 legacy_tftp_prefix
= optarg
;
2637 case QEMU_OPTION_bootp
:
2638 legacy_bootp_filename
= optarg
;
2640 case QEMU_OPTION_redir
:
2641 if (net_slirp_redir(optarg
) < 0)
2645 case QEMU_OPTION_bt
:
2646 add_device_config(DEV_BT
, optarg
);
2648 case QEMU_OPTION_audio_help
:
2649 if (!(audio_available())) {
2650 printf("Option %s not supported for this target\n", popt
->name
);
2656 case QEMU_OPTION_soundhw
:
2657 if (!(audio_available())) {
2658 printf("Option %s not supported for this target\n", popt
->name
);
2661 select_soundhw (optarg
);
2666 case QEMU_OPTION_version
:
2670 case QEMU_OPTION_m
: {
2673 value
= strtosz(optarg
, NULL
);
2675 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2679 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2680 fprintf(stderr
, "qemu: ram size too large\n");
2686 case QEMU_OPTION_mempath
:
2690 case QEMU_OPTION_mem_prealloc
:
2701 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2703 case QEMU_OPTION_gdb
:
2704 gdbstub_dev
= optarg
;
2709 case QEMU_OPTION_bios
:
2712 case QEMU_OPTION_singlestep
:
2719 keyboard_layout
= optarg
;
2721 case QEMU_OPTION_localtime
:
2724 case QEMU_OPTION_vga
:
2725 select_vgahw (optarg
);
2732 w
= strtol(p
, (char **)&p
, 10);
2735 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2741 h
= strtol(p
, (char **)&p
, 10);
2746 depth
= strtol(p
, (char **)&p
, 10);
2747 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2748 depth
!= 24 && depth
!= 32)
2750 } else if (*p
== '\0') {
2751 depth
= graphic_depth
;
2758 graphic_depth
= depth
;
2761 case QEMU_OPTION_echr
:
2764 term_escape_char
= strtol(optarg
, &r
, 0);
2766 printf("Bad argument to echr\n");
2769 case QEMU_OPTION_monitor
:
2770 monitor_parse(optarg
, "readline");
2771 default_monitor
= 0;
2773 case QEMU_OPTION_qmp
:
2774 monitor_parse(optarg
, "control");
2775 default_monitor
= 0;
2777 case QEMU_OPTION_mon
:
2778 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2782 default_monitor
= 0;
2784 case QEMU_OPTION_chardev
:
2785 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2790 case QEMU_OPTION_fsdev
:
2791 olist
= qemu_find_opts("fsdev");
2793 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2796 opts
= qemu_opts_parse(olist
, optarg
, 1);
2798 fprintf(stderr
, "parse error: %s\n", optarg
);
2802 case QEMU_OPTION_virtfs
: {
2806 olist
= qemu_find_opts("virtfs");
2808 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2811 opts
= qemu_opts_parse(olist
, optarg
, 1);
2813 fprintf(stderr
, "parse error: %s\n", optarg
);
2817 if (qemu_opt_get(opts
, "fstype") == NULL
||
2818 qemu_opt_get(opts
, "mount_tag") == NULL
||
2819 qemu_opt_get(opts
, "path") == NULL
||
2820 qemu_opt_get(opts
, "security_model") == NULL
) {
2821 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2822 "security_model=[mapped|passthrough|none],"
2823 "mount_tag=tag.\n");
2827 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2828 qemu_opt_get(opts
, "mount_tag"), 1);
2830 fprintf(stderr
, "duplicate fsdev id: %s\n",
2831 qemu_opt_get(opts
, "mount_tag"));
2834 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2835 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2836 qemu_opt_set(fsdev
, "security_model",
2837 qemu_opt_get(opts
, "security_model"));
2839 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2840 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2841 qemu_opt_set(device
, "fsdev",
2842 qemu_opt_get(opts
, "mount_tag"));
2843 qemu_opt_set(device
, "mount_tag",
2844 qemu_opt_get(opts
, "mount_tag"));
2847 case QEMU_OPTION_serial
:
2848 add_device_config(DEV_SERIAL
, optarg
);
2850 if (strncmp(optarg
, "mon:", 4) == 0) {
2851 default_monitor
= 0;
2854 case QEMU_OPTION_watchdog
:
2857 "qemu: only one watchdog option may be given\n");
2862 case QEMU_OPTION_watchdog_action
:
2863 if (select_watchdog_action(optarg
) == -1) {
2864 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2868 case QEMU_OPTION_virtiocon
:
2869 add_device_config(DEV_VIRTCON
, optarg
);
2870 default_virtcon
= 0;
2871 if (strncmp(optarg
, "mon:", 4) == 0) {
2872 default_monitor
= 0;
2875 case QEMU_OPTION_parallel
:
2876 add_device_config(DEV_PARALLEL
, optarg
);
2877 default_parallel
= 0;
2878 if (strncmp(optarg
, "mon:", 4) == 0) {
2879 default_monitor
= 0;
2882 case QEMU_OPTION_debugcon
:
2883 add_device_config(DEV_DEBUGCON
, optarg
);
2885 case QEMU_OPTION_loadvm
:
2888 case QEMU_OPTION_full_screen
:
2892 case QEMU_OPTION_no_frame
:
2895 case QEMU_OPTION_alt_grab
:
2898 case QEMU_OPTION_ctrl_grab
:
2901 case QEMU_OPTION_no_quit
:
2904 case QEMU_OPTION_sdl
:
2905 display_type
= DT_SDL
;
2908 case QEMU_OPTION_no_frame
:
2909 case QEMU_OPTION_alt_grab
:
2910 case QEMU_OPTION_ctrl_grab
:
2911 case QEMU_OPTION_no_quit
:
2912 case QEMU_OPTION_sdl
:
2913 fprintf(stderr
, "SDL support is disabled\n");
2916 case QEMU_OPTION_pidfile
:
2919 case QEMU_OPTION_win2k_hack
:
2920 win2k_install_hack
= 1;
2922 case QEMU_OPTION_rtc_td_hack
:
2925 case QEMU_OPTION_acpitable
:
2926 do_acpitable_option(optarg
);
2928 case QEMU_OPTION_smbios
:
2929 do_smbios_option(optarg
);
2931 case QEMU_OPTION_enable_kvm
:
2932 olist
= qemu_find_opts("machine");
2933 qemu_opts_reset(olist
);
2934 qemu_opts_parse(olist
, "accel=kvm", 0);
2936 case QEMU_OPTION_machine
:
2937 olist
= qemu_find_opts("machine");
2938 qemu_opts_reset(olist
);
2939 opts
= qemu_opts_parse(olist
, optarg
, 1);
2941 fprintf(stderr
, "parse error: %s\n", optarg
);
2944 optarg
= qemu_opt_get(opts
, "type");
2946 machine
= machine_parse(optarg
);
2949 case QEMU_OPTION_no_kvm
:
2950 olist
= qemu_find_opts("machine");
2951 qemu_opts_reset(olist
);
2952 qemu_opts_parse(olist
, "accel=tcg", 0);
2955 case QEMU_OPTION_no_kvm_irqchip
: {
2960 case QEMU_OPTION_no_kvm_pit
: {
2964 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2965 kvm_pit_reinject
= 0;
2969 case QEMU_OPTION_usb
:
2972 case QEMU_OPTION_usbdevice
:
2974 add_device_config(DEV_USB
, optarg
);
2976 case QEMU_OPTION_device
:
2977 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2981 case QEMU_OPTION_smp
:
2984 fprintf(stderr
, "Invalid number of CPUs\n");
2987 if (max_cpus
< smp_cpus
) {
2988 fprintf(stderr
, "maxcpus must be equal to or greater than "
2992 if (max_cpus
> 255) {
2993 fprintf(stderr
, "Unsupported number of maxcpus\n");
2997 case QEMU_OPTION_vnc
:
3000 vnc_display
= optarg
;
3002 fprintf(stderr
, "VNC support is disabled\n");
3006 case QEMU_OPTION_no_acpi
:
3009 case QEMU_OPTION_no_hpet
:
3012 case QEMU_OPTION_balloon
:
3013 if (balloon_parse(optarg
) < 0) {
3014 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3018 case QEMU_OPTION_no_reboot
:
3021 case QEMU_OPTION_no_shutdown
:
3024 case QEMU_OPTION_show_cursor
:
3027 case QEMU_OPTION_uuid
:
3028 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3029 fprintf(stderr
, "Fail to parse UUID string."
3030 " Wrong format.\n");
3034 case QEMU_OPTION_option_rom
:
3035 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3036 fprintf(stderr
, "Too many option ROMs\n");
3039 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3040 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3041 option_rom
[nb_option_roms
].bootindex
=
3042 qemu_opt_get_number(opts
, "bootindex", -1);
3043 if (!option_rom
[nb_option_roms
].name
) {
3044 fprintf(stderr
, "Option ROM file is not specified\n");
3049 case QEMU_OPTION_semihosting
:
3050 semihosting_enabled
= 1;
3052 case QEMU_OPTION_tdf
:
3055 case QEMU_OPTION_kvm_shadow_memory
:
3056 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3058 case QEMU_OPTION_name
:
3059 qemu_name
= g_strdup(optarg
);
3061 char *p
= strchr(qemu_name
, ',');
3064 if (strncmp(p
, "process=", 8)) {
3065 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3069 os_set_proc_name(p
);
3073 case QEMU_OPTION_prom_env
:
3074 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3075 fprintf(stderr
, "Too many prom variables\n");
3078 prom_envs
[nb_prom_envs
] = optarg
;
3081 case QEMU_OPTION_old_param
:
3084 case QEMU_OPTION_clock
:
3085 configure_alarms(optarg
);
3087 case QEMU_OPTION_startdate
:
3088 configure_rtc_date_offset(optarg
, 1);
3090 case QEMU_OPTION_rtc
:
3091 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3095 configure_rtc(opts
);
3097 case QEMU_OPTION_tb_size
:
3098 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3099 if (tcg_tb_size
< 0) {
3103 case QEMU_OPTION_icount
:
3104 icount_option
= optarg
;
3106 case QEMU_OPTION_incoming
:
3109 case QEMU_OPTION_nodefaults
:
3111 default_parallel
= 0;
3112 default_virtcon
= 0;
3113 default_monitor
= 0;
3121 case QEMU_OPTION_nvram
:
3125 case QEMU_OPTION_xen_domid
:
3126 if (!(xen_available())) {
3127 printf("Option %s not supported for this target\n", popt
->name
);
3130 xen_domid
= atoi(optarg
);
3132 case QEMU_OPTION_xen_create
:
3133 if (!(xen_available())) {
3134 printf("Option %s not supported for this target\n", popt
->name
);
3137 xen_mode
= XEN_CREATE
;
3139 case QEMU_OPTION_xen_attach
:
3140 if (!(xen_available())) {
3141 printf("Option %s not supported for this target\n", popt
->name
);
3144 xen_mode
= XEN_ATTACH
;
3146 case QEMU_OPTION_trace
:
3148 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3152 trace_events
= qemu_opt_get(opts
, "events");
3153 trace_file
= qemu_opt_get(opts
, "file");
3156 case QEMU_OPTION_readconfig
:
3158 int ret
= qemu_read_config_file(optarg
);
3160 fprintf(stderr
, "read config %s: %s\n", optarg
,
3166 case QEMU_OPTION_spice
:
3167 olist
= qemu_find_opts("spice");
3169 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3172 opts
= qemu_opts_parse(olist
, optarg
, 0);
3174 fprintf(stderr
, "parse error: %s\n", optarg
);
3178 case QEMU_OPTION_writeconfig
:
3181 if (strcmp(optarg
, "-") == 0) {
3184 fp
= fopen(optarg
, "w");
3186 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3190 qemu_config_write(fp
);
3195 os_parse_cmd_args(popt
->index
, optarg
);
3201 /* Open the logfile at this point, if necessary. We can't open the logfile
3202 * when encountering either of the logging options (-d or -D) because the
3203 * other one may be encountered later on the command line, changing the
3204 * location or level of logging.
3208 set_cpu_log_filename(log_file
);
3210 set_cpu_log(log_mask
);
3213 if (!trace_backend_init(trace_events
, trace_file
)) {
3217 /* If no data_dir is specified then try to find it relative to the
3220 data_dir
= os_find_datadir(argv
[0]);
3222 /* If all else fails use the install path specified when building. */
3224 data_dir
= CONFIG_QEMU_DATADIR
;
3228 * Default to max_cpus = smp_cpus, in case the user doesn't
3229 * specify a max_cpus value.
3232 max_cpus
= smp_cpus
;
3234 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3235 if (smp_cpus
> machine
->max_cpus
) {
3236 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3237 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3243 * Get the default machine options from the machine if it is not already
3244 * specified either by the configuration file or by the command line.
3246 if (machine
->default_machine_opts
) {
3247 QemuOptsList
*list
= qemu_find_opts("machine");
3248 const char *p
= NULL
;
3250 if (!QTAILQ_EMPTY(&list
->head
)) {
3251 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3254 qemu_opts_reset(list
);
3255 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3257 fprintf(stderr
, "parse error for machine %s: %s\n",
3258 machine
->name
, machine
->default_machine_opts
);
3264 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3265 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3267 if (machine
->no_serial
) {
3270 if (machine
->no_parallel
) {
3271 default_parallel
= 0;
3273 if (!machine
->use_virtcon
) {
3274 default_virtcon
= 0;
3276 if (machine
->no_vga
) {
3279 if (machine
->no_floppy
) {
3282 if (machine
->no_cdrom
) {
3285 if (machine
->no_sdcard
) {
3289 if (display_type
== DT_NOGRAPHIC
) {
3290 if (default_parallel
)
3291 add_device_config(DEV_PARALLEL
, "null");
3292 if (default_serial
&& default_monitor
) {
3293 add_device_config(DEV_SERIAL
, "mon:stdio");
3294 } else if (default_virtcon
&& default_monitor
) {
3295 add_device_config(DEV_VIRTCON
, "mon:stdio");
3298 add_device_config(DEV_SERIAL
, "stdio");
3299 if (default_virtcon
)
3300 add_device_config(DEV_VIRTCON
, "stdio");
3301 if (default_monitor
)
3302 monitor_parse("stdio", "readline");
3306 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3307 if (default_parallel
)
3308 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3309 if (default_monitor
)
3310 monitor_parse("vc:80Cx24C", "readline");
3311 if (default_virtcon
)
3312 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3315 vga_interface_type
= VGA_CIRRUS
;
3319 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3321 #ifdef CONFIG_VIRTFS
3322 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3329 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3334 /* init the memory */
3335 if (ram_size
== 0) {
3336 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3339 configure_accelerator();
3341 if (qemu_init_main_loop()) {
3342 fprintf(stderr
, "qemu_init_main_loop failed\n");
3345 linux_boot
= (kernel_filename
!= NULL
);
3347 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3348 fprintf(stderr
, "-append only allowed with -kernel option\n");
3352 if (!linux_boot
&& initrd_filename
!= NULL
) {
3353 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3357 os_set_line_buffering();
3359 if (init_timer_alarm() < 0) {
3360 fprintf(stderr
, "could not initialize alarm timer\n");
3363 configure_icount(icount_option
);
3365 if (net_init_clients() < 0) {
3369 /* init the bluetooth world */
3370 if (foreach_device_config(DEV_BT
, bt_parse
))
3373 if (!xen_enabled()) {
3374 /* On 32-bit hosts, QEMU is limited by virtual address space */
3375 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3376 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3381 cpu_exec_init_all();
3383 bdrv_init_with_whitelist();
3387 /* open the virtual block devices */
3389 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3390 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3393 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3394 IF_DEFAULT
, 2, CDROM_OPTS
);
3395 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3396 IF_FLOPPY
, 0, FD_OPTS
);
3397 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3400 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3403 if (nb_numa_nodes
> 0) {
3406 if (nb_numa_nodes
> MAX_NODES
) {
3407 nb_numa_nodes
= MAX_NODES
;
3410 /* If no memory size if given for any node, assume the default case
3411 * and distribute the available memory equally across all nodes
3413 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3414 if (node_mem
[i
] != 0)
3417 if (i
== nb_numa_nodes
) {
3418 uint64_t usedmem
= 0;
3420 /* On Linux, the each node's border has to be 8MB aligned,
3421 * the final node gets the rest.
3423 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3424 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3425 usedmem
+= node_mem
[i
];
3427 node_mem
[i
] = ram_size
- usedmem
;
3430 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3431 if (node_cpumask
[i
] != 0)
3434 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3435 * must cope with this anyway, because there are BIOSes out there in
3436 * real machines which also use this scheme.
3438 if (i
== nb_numa_nodes
) {
3439 for (i
= 0; i
< smp_cpus
; i
++) {
3440 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3445 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3449 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3451 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3453 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3455 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3458 module_call_init(MODULE_INIT_DEVICE
);
3460 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3464 i
= select_watchdog(watchdog
);
3466 exit (i
== 1 ? 1 : 0);
3469 if (machine
->compat_props
) {
3470 qdev_prop_register_global_list(machine
->compat_props
);
3474 machine
->init(ram_size
, boot_devices
,
3475 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3477 cpu_synchronize_all_post_init();
3481 current_machine
= machine
;
3483 /* init USB devices */
3485 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3489 /* init generic devices */
3490 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3493 net_check_clients();
3495 /* just use the first displaystate for the moment */
3496 ds
= get_displaystate();
3500 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3501 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3502 display_type
= DT_SDL
;
3503 #elif defined(CONFIG_VNC)
3504 vnc_display
= "localhost:0,to=99";
3507 display_type
= DT_NONE
;
3512 /* init local displays */
3513 switch (display_type
) {
3516 #if defined(CONFIG_CURSES)
3518 curses_display_init(ds
, full_screen
);
3521 #if defined(CONFIG_SDL)
3523 sdl_display_init(ds
, full_screen
, no_frame
);
3525 #elif defined(CONFIG_COCOA)
3527 cocoa_display_init(ds
, full_screen
);
3534 /* must be after terminal init, SDL library changes signal handlers */
3535 os_setup_signal_handling();
3538 /* init remote displays */
3540 vnc_display_init(ds
);
3541 if (vnc_display_open(ds
, vnc_display
) < 0)
3544 if (show_vnc_port
) {
3545 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3550 if (using_spice
&& !qxl_enabled
) {
3551 qemu_spice_display_init(ds
);
3557 dcl
= ds
->listeners
;
3558 while (dcl
!= NULL
) {
3559 if (dcl
->dpy_refresh
!= NULL
) {
3560 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3561 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3566 if (ds
->gui_timer
== NULL
) {
3567 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3568 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3570 text_consoles_set_display(ds
);
3572 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3573 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3578 qdev_machine_creation_done();
3580 if (rom_load_all() != 0) {
3581 fprintf(stderr
, "rom loading failed\n");
3585 /* TODO: once all bus devices are qdevified, this should be done
3586 * when bus is created by qdev.c */
3587 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3588 qemu_run_machine_init_done_notifiers();
3590 qemu_system_reset(VMRESET_SILENT
);
3592 if (load_vmstate(loadvm
) < 0) {
3598 runstate_set(RSTATE_IN_MIGRATE
);
3599 int ret
= qemu_start_incoming_migration(incoming
);
3601 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3605 } else if (autostart
) {
3608 runstate_set(RSTATE_PRE_LAUNCH
);