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
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include "qemu-seccomp.h"
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
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"
139 #include "blockdev.h"
140 #include "hw/block-common.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
153 #include "fsdev/qemu-fsdev.h"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
164 #include "trace/control.h"
165 #include "qemu-queue.h"
167 #include "arch_init.h"
170 #include "ui/qemu-spice.h"
173 //#define DEBUG_SLIRP
175 #define DEFAULT_RAM_SIZE 128
177 #define MAX_VIRTIO_CONSOLES 1
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
182 DisplayType display_type
= DT_DEFAULT
;
183 int display_remote
= 0;
184 const char* keyboard_layout
= NULL
;
186 const char *mem_path
= NULL
;
188 int mem_prealloc
= 0; /* force preallocation of physical target memory */
191 NICInfo nd_table
[MAX_NICS
];
193 static int rtc_utc
= 1;
194 static int rtc_date_offset
= -1; /* -1 means no change */
195 QEMUClock
*rtc_clock
;
196 int vga_interface_type
= VGA_NONE
;
197 static int full_screen
= 0;
199 static int no_frame
= 0;
202 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
203 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
204 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
205 int win2k_install_hack
= 0;
213 const char *vnc_display
;
215 int acpi_enabled
= 1;
221 int graphic_rotate
= 0;
222 const char *watchdog
;
223 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
225 int semihosting_enabled
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
233 uint8_t *boot_splash_filedata
;
234 int boot_splash_filedata_size
;
235 uint8_t qemu_extra_params_fw
[2];
237 typedef struct FWBootEntry FWBootEntry
;
240 QTAILQ_ENTRY(FWBootEntry
) link
;
246 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
249 uint64_t node_mem
[MAX_NODES
];
250 unsigned long *node_cpumask
[MAX_NODES
];
252 uint8_t qemu_uuid
[16];
254 static QEMUBootSetHandler
*boot_set_handler
;
255 static void *boot_set_opaque
;
257 static NotifierList exit_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
260 static NotifierList machine_init_done_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
263 static int tcg_allowed
= 1;
267 enum xen_mode xen_mode
= XEN_EMULATE
;
268 static int tcg_tb_size
;
270 static int default_serial
= 1;
271 static int default_parallel
= 1;
272 static int default_virtcon
= 1;
273 static int default_monitor
= 1;
274 static int default_floppy
= 1;
275 static int default_cdrom
= 1;
276 static int default_sdcard
= 1;
277 static int default_vga
= 1;
283 { .driver
= "isa-serial", .flag
= &default_serial
},
284 { .driver
= "isa-parallel", .flag
= &default_parallel
},
285 { .driver
= "isa-fdc", .flag
= &default_floppy
},
286 { .driver
= "ide-cd", .flag
= &default_cdrom
},
287 { .driver
= "ide-hd", .flag
= &default_cdrom
},
288 { .driver
= "ide-drive", .flag
= &default_cdrom
},
289 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
290 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "isa-vga", .flag
= &default_vga
},
295 { .driver
= "cirrus-vga", .flag
= &default_vga
},
296 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
297 { .driver
= "vmware-svga", .flag
= &default_vga
},
298 { .driver
= "qxl-vga", .flag
= &default_vga
},
301 const char *qemu_get_vm_name(void)
306 static void res_free(void)
308 if (boot_splash_filedata
!= NULL
) {
309 g_free(boot_splash_filedata
);
310 boot_splash_filedata
= NULL
;
314 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
316 const char *driver
= qemu_opt_get(opts
, "driver");
321 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
322 if (strcmp(default_list
[i
].driver
, driver
) != 0)
324 *(default_list
[i
].flag
) = 0;
329 /***********************************************************/
332 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
337 } RunStateTransition
;
339 static const RunStateTransition runstate_transitions_def
[] = {
341 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
343 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
344 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
346 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
347 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
349 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
350 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
352 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
353 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
355 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
356 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
358 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
359 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
360 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
362 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
363 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
365 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
367 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
368 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
369 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
370 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
371 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
372 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
373 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
374 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
375 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
377 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
379 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
380 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
382 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
383 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
384 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
385 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
387 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
388 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
390 { RUN_STATE_MAX
, RUN_STATE_MAX
},
393 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
395 bool runstate_check(RunState state
)
397 return current_run_state
== state
;
400 void runstate_init(void)
402 const RunStateTransition
*p
;
404 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
406 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
407 runstate_valid_transitions
[p
->from
][p
->to
] = true;
411 /* This function will abort() on invalid state transitions */
412 void runstate_set(RunState new_state
)
414 assert(new_state
< RUN_STATE_MAX
);
416 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
417 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
418 RunState_lookup
[current_run_state
],
419 RunState_lookup
[new_state
]);
423 current_run_state
= new_state
;
426 int runstate_is_running(void)
428 return runstate_check(RUN_STATE_RUNNING
);
431 StatusInfo
*qmp_query_status(Error
**errp
)
433 StatusInfo
*info
= g_malloc0(sizeof(*info
));
435 info
->running
= runstate_is_running();
436 info
->singlestep
= singlestep
;
437 info
->status
= current_run_state
;
442 /***********************************************************/
443 /* real time host monotonic timer */
445 /***********************************************************/
446 /* host time/date access */
447 void qemu_get_timedate(struct tm
*tm
, int offset
)
454 if (rtc_date_offset
== -1) {
458 ret
= localtime(&ti
);
460 ti
-= rtc_date_offset
;
464 memcpy(tm
, ret
, sizeof(struct tm
));
467 int qemu_timedate_diff(struct tm
*tm
)
471 if (rtc_date_offset
== -1)
473 seconds
= mktimegm(tm
);
476 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
477 seconds
= mktime(&tmp
);
480 seconds
= mktimegm(tm
) + rtc_date_offset
;
482 return seconds
- time(NULL
);
485 void rtc_change_mon_event(struct tm
*tm
)
489 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
490 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
491 qobject_decref(data
);
494 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
496 time_t rtc_start_date
;
499 if (!strcmp(startdate
, "now") && legacy
) {
500 rtc_date_offset
= -1;
502 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
510 } else if (sscanf(startdate
, "%d-%d-%d",
522 rtc_start_date
= mktimegm(&tm
);
523 if (rtc_start_date
== -1) {
525 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
526 "'2006-06-17T16:01:21' or '2006-06-17'\n");
529 rtc_date_offset
= time(NULL
) - rtc_start_date
;
533 static void configure_rtc(QemuOpts
*opts
)
537 value
= qemu_opt_get(opts
, "base");
539 if (!strcmp(value
, "utc")) {
541 } else if (!strcmp(value
, "localtime")) {
544 configure_rtc_date_offset(value
, 0);
547 value
= qemu_opt_get(opts
, "clock");
549 if (!strcmp(value
, "host")) {
550 rtc_clock
= host_clock
;
551 } else if (!strcmp(value
, "rt")) {
552 rtc_clock
= rt_clock
;
553 } else if (!strcmp(value
, "vm")) {
554 rtc_clock
= vm_clock
;
556 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
560 value
= qemu_opt_get(opts
, "driftfix");
562 if (!strcmp(value
, "slew")) {
563 static GlobalProperty slew_lost_ticks
[] = {
565 .driver
= "mc146818rtc",
566 .property
= "lost_tick_policy",
569 { /* end of list */ }
572 qdev_prop_register_global_list(slew_lost_ticks
);
573 } else if (!strcmp(value
, "none")) {
574 /* discard is default */
576 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
582 /***********************************************************/
583 /* Bluetooth support */
586 static struct HCIInfo
*hci_table
[MAX_NICS
];
588 static struct bt_vlan_s
{
589 struct bt_scatternet_s net
;
591 struct bt_vlan_s
*next
;
594 /* find or alloc a new bluetooth "VLAN" */
595 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
597 struct bt_vlan_s
**pvlan
, *vlan
;
598 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
602 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
604 pvlan
= &first_bt_vlan
;
605 while (*pvlan
!= NULL
)
606 pvlan
= &(*pvlan
)->next
;
611 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
615 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
620 static struct HCIInfo null_hci
= {
621 .cmd_send
= null_hci_send
,
622 .sco_send
= null_hci_send
,
623 .acl_send
= null_hci_send
,
624 .bdaddr_set
= null_hci_addr_set
,
627 struct HCIInfo
*qemu_next_hci(void)
629 if (cur_hci
== nb_hcis
)
632 return hci_table
[cur_hci
++];
635 static struct HCIInfo
*hci_init(const char *str
)
638 struct bt_scatternet_s
*vlan
= 0;
640 if (!strcmp(str
, "null"))
643 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
645 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
646 else if (!strncmp(str
, "hci", 3)) {
649 if (!strncmp(str
+ 3, ",vlan=", 6)) {
650 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
655 vlan
= qemu_find_bt_vlan(0);
657 return bt_new_hci(vlan
);
660 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
665 static int bt_hci_parse(const char *str
)
670 if (nb_hcis
>= MAX_NICS
) {
671 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
684 bdaddr
.b
[5] = 0x56 + nb_hcis
;
685 hci
->bdaddr_set(hci
, bdaddr
.b
);
687 hci_table
[nb_hcis
++] = hci
;
692 static void bt_vhci_add(int vlan_id
)
694 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
697 fprintf(stderr
, "qemu: warning: adding a VHCI to "
698 "an empty scatternet %i\n", vlan_id
);
700 bt_vhci_init(bt_new_hci(vlan
));
703 static struct bt_device_s
*bt_device_add(const char *opt
)
705 struct bt_scatternet_s
*vlan
;
707 char *endp
= strstr(opt
, ",vlan=");
708 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
711 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
714 vlan_id
= strtol(endp
+ 6, &endp
, 0);
716 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
721 vlan
= qemu_find_bt_vlan(vlan_id
);
724 fprintf(stderr
, "qemu: warning: adding a slave device to "
725 "an empty scatternet %i\n", vlan_id
);
727 if (!strcmp(devname
, "keyboard"))
728 return bt_keyboard_init(vlan
);
730 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
734 static int bt_parse(const char *opt
)
736 const char *endp
, *p
;
739 if (strstart(opt
, "hci", &endp
)) {
740 if (!*endp
|| *endp
== ',') {
742 if (!strstart(endp
, ",vlan=", 0))
745 return bt_hci_parse(opt
);
747 } else if (strstart(opt
, "vhci", &endp
)) {
748 if (!*endp
|| *endp
== ',') {
750 if (strstart(endp
, ",vlan=", &p
)) {
751 vlan
= strtol(p
, (char **) &endp
, 0);
753 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
757 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
766 } else if (strstart(opt
, "device:", &endp
))
767 return !bt_device_add(endp
);
769 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
773 /***********************************************************/
774 /* QEMU Block devices */
776 #define HD_OPTS "media=disk"
777 #define CDROM_OPTS "media=cdrom"
779 #define PFLASH_OPTS ""
783 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
785 int *use_scsi
= opaque
;
787 return drive_init(opts
, *use_scsi
) == NULL
;
790 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
792 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
793 qemu_opt_set(opts
, "snapshot", "on");
798 static void default_drive(int enable
, int snapshot
, int use_scsi
,
799 BlockInterfaceType type
, int index
,
804 if (type
== IF_DEFAULT
) {
805 type
= use_scsi
? IF_SCSI
: IF_IDE
;
808 if (!enable
|| drive_get_by_index(type
, index
)) {
812 opts
= drive_add(type
, index
, NULL
, optstr
);
814 drive_enable_snapshot(opts
, NULL
);
816 if (!drive_init(opts
, use_scsi
)) {
821 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
823 boot_set_handler
= func
;
824 boot_set_opaque
= opaque
;
827 int qemu_boot_set(const char *boot_devices
)
829 if (!boot_set_handler
) {
832 return boot_set_handler(boot_set_opaque
, boot_devices
);
835 static void validate_bootdevices(char *devices
)
837 /* We just do some generic consistency checks */
841 for (p
= devices
; *p
!= '\0'; p
++) {
842 /* Allowed boot devices are:
843 * a-b: floppy disk drives
844 * c-f: IDE disk drives
845 * g-m: machine implementation dependent drives
846 * n-p: network devices
847 * It's up to each machine implementation to check if the given boot
848 * devices match the actual hardware implementation and firmware
851 if (*p
< 'a' || *p
> 'p') {
852 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
855 if (bitmap
& (1 << (*p
- 'a'))) {
856 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
859 bitmap
|= 1 << (*p
- 'a');
863 static void restore_boot_devices(void *opaque
)
865 char *standard_boot_devices
= opaque
;
866 static int first
= 1;
868 /* Restore boot order and remove ourselves after the first boot */
874 qemu_boot_set(standard_boot_devices
);
876 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
877 g_free(standard_boot_devices
);
880 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
883 FWBootEntry
*node
, *i
;
889 assert(dev
!= NULL
|| suffix
!= NULL
);
891 node
= g_malloc0(sizeof(FWBootEntry
));
892 node
->bootindex
= bootindex
;
893 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
896 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
897 if (i
->bootindex
== bootindex
) {
898 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
900 } else if (i
->bootindex
< bootindex
) {
903 QTAILQ_INSERT_BEFORE(i
, node
, link
);
906 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
910 * This function returns null terminated string that consist of new line
911 * separated device paths.
913 * memory pointed by "size" is assigned total length of the array in bytes
916 char *get_boot_devices_list(uint32_t *size
)
922 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
923 char *devpath
= NULL
, *bootpath
;
927 devpath
= qdev_get_fw_dev_path(i
->dev
);
931 if (i
->suffix
&& devpath
) {
932 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
934 bootpath
= g_malloc(bootpathlen
);
935 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
937 } else if (devpath
) {
941 bootpath
= g_strdup(i
->suffix
);
945 list
[total
-1] = '\n';
947 len
= strlen(bootpath
) + 1;
948 list
= g_realloc(list
, total
+ len
);
949 memcpy(&list
[total
], bootpath
, len
);
959 static void numa_add(const char *optarg
)
963 unsigned long long value
, endvalue
;
966 value
= endvalue
= 0ULL;
968 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
969 if (!strcmp(option
, "node")) {
970 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
971 nodenr
= nb_numa_nodes
;
973 nodenr
= strtoull(option
, NULL
, 10);
976 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
977 node_mem
[nodenr
] = 0;
980 sval
= strtosz(option
, &endptr
);
981 if (sval
< 0 || *endptr
) {
982 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
985 node_mem
[nodenr
] = sval
;
987 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
988 value
= strtoull(option
, &endptr
, 10);
989 if (*endptr
== '-') {
990 endvalue
= strtoull(endptr
+1, &endptr
, 10);
995 if (!(endvalue
< MAX_CPUMASK_BITS
)) {
996 endvalue
= MAX_CPUMASK_BITS
- 1;
998 "A max of %d CPUs are supported in a guest\n",
1002 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
1009 static void smp_parse(const char *optarg
)
1011 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1015 smp
= strtoul(optarg
, &endptr
, 10);
1016 if (endptr
!= optarg
) {
1017 if (*endptr
== ',') {
1021 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1022 sockets
= strtoull(option
, NULL
, 10);
1023 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1024 cores
= strtoull(option
, NULL
, 10);
1025 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1026 threads
= strtoull(option
, NULL
, 10);
1027 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1028 max_cpus
= strtoull(option
, NULL
, 10);
1030 /* compute missing values, prefer sockets over cores over threads */
1031 if (smp
== 0 || sockets
== 0) {
1032 sockets
= sockets
> 0 ? sockets
: 1;
1033 cores
= cores
> 0 ? cores
: 1;
1034 threads
= threads
> 0 ? threads
: 1;
1036 smp
= cores
* threads
* sockets
;
1040 threads
= threads
> 0 ? threads
: 1;
1041 cores
= smp
/ (sockets
* threads
);
1043 threads
= smp
/ (cores
* sockets
);
1047 smp_cores
= cores
> 0 ? cores
: 1;
1048 smp_threads
= threads
> 0 ? threads
: 1;
1050 max_cpus
= smp_cpus
;
1053 /***********************************************************/
1056 static int usb_device_add(const char *devname
)
1059 USBDevice
*dev
= NULL
;
1064 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1065 dev
= usbdevice_create(devname
);
1069 /* the other ones */
1070 #ifndef CONFIG_LINUX
1071 /* only the linux version is qdev-ified, usb-bsd still needs this */
1072 if (strstart(devname
, "host:", &p
)) {
1073 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1076 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1077 dev
= usb_bt_init(usb_bus_find(-1),
1078 devname
[2] ? hci_init(p
)
1079 : bt_new_hci(qemu_find_bt_vlan(0)));
1090 static int usb_device_del(const char *devname
)
1095 if (strstart(devname
, "host:", &p
))
1096 return usb_host_device_close(p
);
1101 p
= strchr(devname
, '.');
1104 bus_num
= strtoul(devname
, NULL
, 0);
1105 addr
= strtoul(p
+ 1, NULL
, 0);
1107 return usb_device_delete_addr(bus_num
, addr
);
1110 static int usb_parse(const char *cmdline
)
1113 r
= usb_device_add(cmdline
);
1115 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1120 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1122 const char *devname
= qdict_get_str(qdict
, "devname");
1123 if (usb_device_add(devname
) < 0) {
1124 error_report("could not add USB device '%s'", devname
);
1128 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1130 const char *devname
= qdict_get_str(qdict
, "devname");
1131 if (usb_device_del(devname
) < 0) {
1132 error_report("could not delete USB device '%s'", devname
);
1136 /***********************************************************/
1137 /* PCMCIA/Cardbus */
1139 static struct pcmcia_socket_entry_s
{
1140 PCMCIASocket
*socket
;
1141 struct pcmcia_socket_entry_s
*next
;
1142 } *pcmcia_sockets
= 0;
1144 void pcmcia_socket_register(PCMCIASocket
*socket
)
1146 struct pcmcia_socket_entry_s
*entry
;
1148 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1149 entry
->socket
= socket
;
1150 entry
->next
= pcmcia_sockets
;
1151 pcmcia_sockets
= entry
;
1154 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1156 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1158 ptr
= &pcmcia_sockets
;
1159 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1160 if (entry
->socket
== socket
) {
1166 void pcmcia_info(Monitor
*mon
)
1168 struct pcmcia_socket_entry_s
*iter
;
1170 if (!pcmcia_sockets
)
1171 monitor_printf(mon
, "No PCMCIA sockets\n");
1173 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1174 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1175 iter
->socket
->attached
? iter
->socket
->card_string
:
1179 /***********************************************************/
1180 /* machine registration */
1182 static QEMUMachine
*first_machine
= NULL
;
1183 QEMUMachine
*current_machine
= NULL
;
1185 int qemu_register_machine(QEMUMachine
*m
)
1188 pm
= &first_machine
;
1196 static QEMUMachine
*find_machine(const char *name
)
1200 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1201 if (!strcmp(m
->name
, name
))
1203 if (m
->alias
&& !strcmp(m
->alias
, name
))
1209 QEMUMachine
*find_default_machine(void)
1213 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1214 if (m
->is_default
) {
1221 MachineInfoList
*qmp_query_machines(Error
**errp
)
1223 MachineInfoList
*mach_list
= NULL
;
1226 for (m
= first_machine
; m
; m
= m
->next
) {
1227 MachineInfoList
*entry
;
1230 info
= g_malloc0(sizeof(*info
));
1231 if (m
->is_default
) {
1232 info
->has_is_default
= true;
1233 info
->is_default
= true;
1237 info
->has_alias
= true;
1238 info
->alias
= g_strdup(m
->alias
);
1241 info
->name
= g_strdup(m
->name
);
1243 entry
= g_malloc0(sizeof(*entry
));
1244 entry
->value
= info
;
1245 entry
->next
= mach_list
;
1252 /***********************************************************/
1253 /* main execution loop */
1255 static void gui_update(void *opaque
)
1257 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1258 DisplayState
*ds
= opaque
;
1259 DisplayChangeListener
*dcl
= ds
->listeners
;
1263 while (dcl
!= NULL
) {
1264 if (dcl
->gui_timer_interval
&&
1265 dcl
->gui_timer_interval
< interval
)
1266 interval
= dcl
->gui_timer_interval
;
1269 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1272 struct vm_change_state_entry
{
1273 VMChangeStateHandler
*cb
;
1275 QLIST_ENTRY (vm_change_state_entry
) entries
;
1278 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1280 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1283 VMChangeStateEntry
*e
;
1285 e
= g_malloc0(sizeof (*e
));
1289 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1293 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1295 QLIST_REMOVE (e
, entries
);
1299 void vm_state_notify(int running
, RunState state
)
1301 VMChangeStateEntry
*e
;
1303 trace_vm_state_notify(running
, state
);
1305 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1306 e
->cb(e
->opaque
, running
, state
);
1312 if (!runstate_is_running()) {
1314 runstate_set(RUN_STATE_RUNNING
);
1315 vm_state_notify(1, RUN_STATE_RUNNING
);
1317 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1321 /* reset/shutdown handler */
1323 typedef struct QEMUResetEntry
{
1324 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1325 QEMUResetHandler
*func
;
1329 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1330 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1331 static int reset_requested
;
1332 static int shutdown_requested
, shutdown_signal
= -1;
1333 static pid_t shutdown_pid
;
1334 static int powerdown_requested
;
1335 static int debug_requested
;
1336 static int suspend_requested
;
1337 static int wakeup_requested
;
1338 static NotifierList suspend_notifiers
=
1339 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1340 static NotifierList wakeup_notifiers
=
1341 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1342 static uint32_t wakeup_reason_mask
= ~0;
1343 static RunState vmstop_requested
= RUN_STATE_MAX
;
1345 int qemu_shutdown_requested_get(void)
1347 return shutdown_requested
;
1350 int qemu_reset_requested_get(void)
1352 return reset_requested
;
1355 int qemu_shutdown_requested(void)
1357 int r
= shutdown_requested
;
1358 shutdown_requested
= 0;
1362 void qemu_kill_report(void)
1364 if (!qtest_enabled() && shutdown_signal
!= -1) {
1365 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1366 if (shutdown_pid
== 0) {
1367 /* This happens for eg ^C at the terminal, so it's worth
1368 * avoiding printing an odd message in that case.
1370 fputc('\n', stderr
);
1372 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1374 shutdown_signal
= -1;
1378 int qemu_reset_requested(void)
1380 int r
= reset_requested
;
1381 reset_requested
= 0;
1385 static int qemu_suspend_requested(void)
1387 int r
= suspend_requested
;
1388 suspend_requested
= 0;
1392 static int qemu_wakeup_requested(void)
1394 int r
= wakeup_requested
;
1395 wakeup_requested
= 0;
1399 int qemu_powerdown_requested(void)
1401 int r
= powerdown_requested
;
1402 powerdown_requested
= 0;
1406 static int qemu_debug_requested(void)
1408 int r
= debug_requested
;
1409 debug_requested
= 0;
1413 /* We use RUN_STATE_MAX but any invalid value will do */
1414 static bool qemu_vmstop_requested(RunState
*r
)
1416 if (vmstop_requested
< RUN_STATE_MAX
) {
1417 *r
= vmstop_requested
;
1418 vmstop_requested
= RUN_STATE_MAX
;
1425 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1427 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1430 re
->opaque
= opaque
;
1431 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1434 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1438 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1439 if (re
->func
== func
&& re
->opaque
== opaque
) {
1440 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1447 void qemu_system_reset(bool report
)
1449 QEMUResetEntry
*re
, *nre
;
1451 /* reset all devices */
1452 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1453 re
->func(re
->opaque
);
1456 monitor_protocol_event(QEVENT_RESET
, NULL
);
1458 cpu_synchronize_all_post_reset();
1461 void qemu_system_reset_request(void)
1464 shutdown_requested
= 1;
1466 reset_requested
= 1;
1469 qemu_notify_event();
1472 static void qemu_system_suspend(void)
1475 notifier_list_notify(&suspend_notifiers
, NULL
);
1476 runstate_set(RUN_STATE_SUSPENDED
);
1477 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1480 void qemu_system_suspend_request(void)
1482 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1485 suspend_requested
= 1;
1487 qemu_notify_event();
1490 void qemu_register_suspend_notifier(Notifier
*notifier
)
1492 notifier_list_add(&suspend_notifiers
, notifier
);
1495 void qemu_system_wakeup_request(WakeupReason reason
)
1497 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1500 if (!(wakeup_reason_mask
& (1 << reason
))) {
1503 runstate_set(RUN_STATE_RUNNING
);
1504 notifier_list_notify(&wakeup_notifiers
, &reason
);
1505 wakeup_requested
= 1;
1506 qemu_notify_event();
1509 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1512 wakeup_reason_mask
|= (1 << reason
);
1514 wakeup_reason_mask
&= ~(1 << reason
);
1518 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1520 notifier_list_add(&wakeup_notifiers
, notifier
);
1523 void qemu_system_killed(int signal
, pid_t pid
)
1525 shutdown_signal
= signal
;
1528 qemu_system_shutdown_request();
1531 void qemu_system_shutdown_request(void)
1533 shutdown_requested
= 1;
1534 qemu_notify_event();
1537 void qemu_system_powerdown_request(void)
1539 powerdown_requested
= 1;
1540 qemu_notify_event();
1543 void qemu_system_debug_request(void)
1545 debug_requested
= 1;
1546 qemu_notify_event();
1549 void qemu_system_vmstop_request(RunState state
)
1551 vmstop_requested
= state
;
1552 qemu_notify_event();
1555 qemu_irq qemu_system_powerdown
;
1557 static bool main_loop_should_exit(void)
1560 if (qemu_debug_requested()) {
1561 vm_stop(RUN_STATE_DEBUG
);
1563 if (qemu_suspend_requested()) {
1564 qemu_system_suspend();
1566 if (qemu_shutdown_requested()) {
1568 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1570 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_wakeup_requested()) {
1587 cpu_synchronize_all_states();
1588 qemu_system_reset(VMRESET_SILENT
);
1590 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1592 if (qemu_powerdown_requested()) {
1593 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1594 qemu_irq_raise(qemu_system_powerdown
);
1596 if (qemu_vmstop_requested(&r
)) {
1602 static void main_loop(void)
1606 #ifdef CONFIG_PROFILER
1610 nonblocking
= !kvm_enabled() && last_io
> 0;
1611 #ifdef CONFIG_PROFILER
1612 ti
= profile_getclock();
1614 last_io
= main_loop_wait(nonblocking
);
1615 #ifdef CONFIG_PROFILER
1616 dev_time
+= profile_getclock() - ti
;
1618 } while (!main_loop_should_exit());
1621 static void version(void)
1623 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1626 static void help(int exitcode
)
1629 printf("usage: %s [options] [disk_image]\n\n"
1630 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1631 error_get_progname());
1633 #define QEMU_OPTIONS_GENERATE_HELP
1634 #include "qemu-options-wrapper.h"
1636 printf("\nDuring emulation, the following keys are useful:\n"
1637 "ctrl-alt-f toggle full screen\n"
1638 "ctrl-alt-n switch to virtual console 'n'\n"
1639 "ctrl-alt toggle mouse and keyboard grab\n"
1641 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1646 #define HAS_ARG 0x0001
1648 typedef struct QEMUOption
{
1655 static const QEMUOption qemu_options
[] = {
1656 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1657 #define QEMU_OPTIONS_GENERATE_OPTIONS
1658 #include "qemu-options-wrapper.h"
1662 static bool vga_available(void)
1664 return qdev_exists("VGA") || qdev_exists("isa-vga");
1667 static bool cirrus_vga_available(void)
1669 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1672 static bool vmware_vga_available(void)
1674 return qdev_exists("vmware-svga");
1677 static void select_vgahw (const char *p
)
1681 vga_interface_type
= VGA_NONE
;
1682 if (strstart(p
, "std", &opts
)) {
1683 if (vga_available()) {
1684 vga_interface_type
= VGA_STD
;
1686 fprintf(stderr
, "Error: standard VGA not available\n");
1689 } else if (strstart(p
, "cirrus", &opts
)) {
1690 if (cirrus_vga_available()) {
1691 vga_interface_type
= VGA_CIRRUS
;
1693 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1696 } else if (strstart(p
, "vmware", &opts
)) {
1697 if (vmware_vga_available()) {
1698 vga_interface_type
= VGA_VMWARE
;
1700 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1703 } else if (strstart(p
, "xenfb", &opts
)) {
1704 vga_interface_type
= VGA_XENFB
;
1705 } else if (strstart(p
, "qxl", &opts
)) {
1706 vga_interface_type
= VGA_QXL
;
1707 } else if (!strstart(p
, "none", &opts
)) {
1709 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1713 const char *nextopt
;
1715 if (strstart(opts
, ",retrace=", &nextopt
)) {
1717 if (strstart(opts
, "dumb", &nextopt
))
1718 vga_retrace_method
= VGA_RETRACE_DUMB
;
1719 else if (strstart(opts
, "precise", &nextopt
))
1720 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1721 else goto invalid_vga
;
1722 } else goto invalid_vga
;
1727 static DisplayType
select_display(const char *p
)
1730 DisplayType display
= DT_DEFAULT
;
1732 if (strstart(p
, "sdl", &opts
)) {
1736 const char *nextopt
;
1738 if (strstart(opts
, ",frame=", &nextopt
)) {
1740 if (strstart(opts
, "on", &nextopt
)) {
1742 } else if (strstart(opts
, "off", &nextopt
)) {
1745 goto invalid_sdl_args
;
1747 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1749 if (strstart(opts
, "on", &nextopt
)) {
1751 } else if (strstart(opts
, "off", &nextopt
)) {
1754 goto invalid_sdl_args
;
1756 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1758 if (strstart(opts
, "on", &nextopt
)) {
1760 } else if (strstart(opts
, "off", &nextopt
)) {
1763 goto invalid_sdl_args
;
1765 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1767 if (strstart(opts
, "on", &nextopt
)) {
1769 } else if (strstart(opts
, "off", &nextopt
)) {
1772 goto invalid_sdl_args
;
1776 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1782 fprintf(stderr
, "SDL support is disabled\n");
1785 } else if (strstart(p
, "vnc", &opts
)) {
1790 const char *nextopt
;
1792 if (strstart(opts
, "=", &nextopt
)) {
1793 vnc_display
= nextopt
;
1797 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1801 fprintf(stderr
, "VNC support is disabled\n");
1804 } else if (strstart(p
, "curses", &opts
)) {
1805 #ifdef CONFIG_CURSES
1806 display
= DT_CURSES
;
1808 fprintf(stderr
, "Curses support is disabled\n");
1811 } else if (strstart(p
, "none", &opts
)) {
1814 fprintf(stderr
, "Unknown display type: %s\n", p
);
1821 static int balloon_parse(const char *arg
)
1825 if (strcmp(arg
, "none") == 0) {
1829 if (!strncmp(arg
, "virtio", 6)) {
1830 if (arg
[6] == ',') {
1831 /* have params -> parse them */
1832 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1836 /* create empty opts */
1837 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0, NULL
);
1839 qemu_opt_set(opts
, "driver", "virtio-balloon");
1846 char *qemu_find_file(int type
, const char *name
)
1852 /* Try the name as a straight path first */
1853 if (access(name
, R_OK
) == 0) {
1854 return g_strdup(name
);
1857 case QEMU_FILE_TYPE_BIOS
:
1860 case QEMU_FILE_TYPE_KEYMAP
:
1861 subdir
= "keymaps/";
1866 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1867 buf
= g_malloc0(len
);
1868 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1869 if (access(buf
, R_OK
)) {
1876 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1878 return qdev_device_help(opts
);
1881 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1885 dev
= qdev_device_add(opts
);
1891 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1893 CharDriverState
*chr
;
1895 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1901 #ifdef CONFIG_VIRTFS
1902 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1905 ret
= qemu_fsdev_add(opts
);
1911 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1913 CharDriverState
*chr
;
1914 const char *chardev
;
1918 mode
= qemu_opt_get(opts
, "mode");
1922 if (strcmp(mode
, "readline") == 0) {
1923 flags
= MONITOR_USE_READLINE
;
1924 } else if (strcmp(mode
, "control") == 0) {
1925 flags
= MONITOR_USE_CONTROL
;
1927 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1931 if (qemu_opt_get_bool(opts
, "pretty", 0))
1932 flags
|= MONITOR_USE_PRETTY
;
1934 if (qemu_opt_get_bool(opts
, "default", 0))
1935 flags
|= MONITOR_IS_DEFAULT
;
1937 chardev
= qemu_opt_get(opts
, "chardev");
1938 chr
= qemu_chr_find(chardev
);
1940 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1944 monitor_init(chr
, flags
);
1948 static void monitor_parse(const char *optarg
, const char *mode
)
1950 static int monitor_device_index
= 0;
1956 if (strstart(optarg
, "chardev:", &p
)) {
1957 snprintf(label
, sizeof(label
), "%s", p
);
1959 snprintf(label
, sizeof(label
), "compat_monitor%d",
1960 monitor_device_index
);
1961 if (monitor_device_index
== 0) {
1964 opts
= qemu_chr_parse_compat(label
, optarg
);
1966 fprintf(stderr
, "parse error: %s\n", optarg
);
1971 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
1973 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1976 qemu_opt_set(opts
, "mode", mode
);
1977 qemu_opt_set(opts
, "chardev", label
);
1979 qemu_opt_set(opts
, "default", "on");
1980 monitor_device_index
++;
1983 struct device_config
{
1985 DEV_USB
, /* -usbdevice */
1987 DEV_SERIAL
, /* -serial */
1988 DEV_PARALLEL
, /* -parallel */
1989 DEV_VIRTCON
, /* -virtioconsole */
1990 DEV_DEBUGCON
, /* -debugcon */
1991 DEV_GDB
, /* -gdb, -s */
1993 const char *cmdline
;
1995 QTAILQ_ENTRY(device_config
) next
;
1997 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1999 static void add_device_config(int type
, const char *cmdline
)
2001 struct device_config
*conf
;
2003 conf
= g_malloc0(sizeof(*conf
));
2005 conf
->cmdline
= cmdline
;
2006 loc_save(&conf
->loc
);
2007 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2010 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2012 struct device_config
*conf
;
2015 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2016 if (conf
->type
!= type
)
2018 loc_push_restore(&conf
->loc
);
2019 rc
= func(conf
->cmdline
);
2020 loc_pop(&conf
->loc
);
2027 static int serial_parse(const char *devname
)
2029 static int index
= 0;
2032 if (strcmp(devname
, "none") == 0)
2034 if (index
== MAX_SERIAL_PORTS
) {
2035 fprintf(stderr
, "qemu: too many serial ports\n");
2038 snprintf(label
, sizeof(label
), "serial%d", index
);
2039 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2040 if (!serial_hds
[index
]) {
2041 fprintf(stderr
, "qemu: could not connect serial device"
2042 " to character backend '%s'\n", devname
);
2049 static int parallel_parse(const char *devname
)
2051 static int index
= 0;
2054 if (strcmp(devname
, "none") == 0)
2056 if (index
== MAX_PARALLEL_PORTS
) {
2057 fprintf(stderr
, "qemu: too many parallel ports\n");
2060 snprintf(label
, sizeof(label
), "parallel%d", index
);
2061 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2062 if (!parallel_hds
[index
]) {
2063 fprintf(stderr
, "qemu: could not connect parallel device"
2064 " to character backend '%s'\n", devname
);
2071 static int virtcon_parse(const char *devname
)
2073 QemuOptsList
*device
= qemu_find_opts("device");
2074 static int index
= 0;
2076 QemuOpts
*bus_opts
, *dev_opts
;
2078 if (strcmp(devname
, "none") == 0)
2080 if (index
== MAX_VIRTIO_CONSOLES
) {
2081 fprintf(stderr
, "qemu: too many virtio consoles\n");
2085 bus_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2086 if (arch_type
== QEMU_ARCH_S390X
) {
2087 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2089 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2092 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2093 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2095 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2096 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2097 if (!virtcon_hds
[index
]) {
2098 fprintf(stderr
, "qemu: could not connect virtio console"
2099 " to character backend '%s'\n", devname
);
2102 qemu_opt_set(dev_opts
, "chardev", label
);
2108 static int debugcon_parse(const char *devname
)
2112 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2115 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2117 fprintf(stderr
, "qemu: already have a debugcon device\n");
2120 qemu_opt_set(opts
, "driver", "isa-debugcon");
2121 qemu_opt_set(opts
, "chardev", "debugcon");
2125 static QEMUMachine
*machine_parse(const char *name
)
2127 QEMUMachine
*m
, *machine
= NULL
;
2130 machine
= find_machine(name
);
2135 printf("Supported machines are:\n");
2136 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2138 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2140 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2141 m
->is_default
? " (default)" : "");
2143 exit(!name
|| !is_help_option(name
));
2146 static int tcg_init(void)
2148 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2153 const char *opt_name
;
2155 int (*available
)(void);
2159 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2160 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2161 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2162 { "qtest", "QTest", qtest_available
, qtest_init
, &qtest_allowed
},
2165 static int configure_accelerator(void)
2167 const char *p
= NULL
;
2170 bool accel_initialised
= 0;
2171 bool init_failed
= 0;
2173 QemuOptsList
*list
= qemu_find_opts("machine");
2174 if (!QTAILQ_EMPTY(&list
->head
)) {
2175 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2179 /* Use the default "accelerator", tcg */
2183 while (!accel_initialised
&& *p
!= '\0') {
2187 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2188 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2189 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2190 *(accel_list
[i
].allowed
) = 1;
2191 ret
= accel_list
[i
].init();
2194 if (!accel_list
[i
].available()) {
2195 printf("%s not supported for this target\n",
2196 accel_list
[i
].name
);
2198 fprintf(stderr
, "failed to initialize %s: %s\n",
2202 *(accel_list
[i
].allowed
) = 0;
2204 accel_initialised
= 1;
2209 if (i
== ARRAY_SIZE(accel_list
)) {
2210 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2214 if (!accel_initialised
) {
2215 fprintf(stderr
, "No accelerator found!\n");
2220 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2223 return !accel_initialised
;
2226 void qemu_add_exit_notifier(Notifier
*notify
)
2228 notifier_list_add(&exit_notifiers
, notify
);
2231 void qemu_remove_exit_notifier(Notifier
*notify
)
2233 notifier_remove(notify
);
2236 static void qemu_run_exit_notifiers(void)
2238 notifier_list_notify(&exit_notifiers
, NULL
);
2241 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2243 notifier_list_add(&machine_init_done_notifiers
, notify
);
2246 static void qemu_run_machine_init_done_notifiers(void)
2248 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2251 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2252 const char **poptarg
, int *poptind
)
2254 const QEMUOption
*popt
;
2255 int optind
= *poptind
;
2256 char *r
= argv
[optind
];
2259 loc_set_cmdline(argv
, optind
, 1);
2261 /* Treat --foo the same as -foo. */
2264 popt
= qemu_options
;
2267 error_report("invalid option");
2270 if (!strcmp(popt
->name
, r
+ 1))
2274 if (popt
->flags
& HAS_ARG
) {
2275 if (optind
>= argc
) {
2276 error_report("requires an argument");
2279 optarg
= argv
[optind
++];
2280 loc_set_cmdline(argv
, optind
- 2, 2);
2291 static gpointer
malloc_and_trace(gsize n_bytes
)
2293 void *ptr
= malloc(n_bytes
);
2294 trace_g_malloc(n_bytes
, ptr
);
2298 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2300 void *ptr
= realloc(mem
, n_bytes
);
2301 trace_g_realloc(mem
, n_bytes
, ptr
);
2305 static void free_and_trace(gpointer mem
)
2311 int qemu_init_main_loop(void)
2313 return main_loop_init();
2316 int main(int argc
, char **argv
, char **envp
)
2319 int snapshot
, linux_boot
;
2320 const char *icount_option
= NULL
;
2321 const char *initrd_filename
;
2322 const char *kernel_filename
, *kernel_cmdline
;
2323 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2325 DisplayChangeListener
*dcl
;
2326 int cyls
, heads
, secs
, translation
;
2327 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2328 QemuOptsList
*olist
;
2331 const char *loadvm
= NULL
;
2332 QEMUMachine
*machine
;
2333 const char *cpu_model
;
2334 const char *vga_model
= "none";
2335 const char *pid_file
= NULL
;
2336 const char *incoming
= NULL
;
2338 int show_vnc_port
= 0;
2340 bool defconfig
= true;
2341 bool userconfig
= true;
2342 const char *log_mask
= NULL
;
2343 const char *log_file
= NULL
;
2344 GMemVTable mem_trace
= {
2345 .malloc
= malloc_and_trace
,
2346 .realloc
= realloc_and_trace
,
2347 .free
= free_and_trace
,
2349 const char *trace_events
= NULL
;
2350 const char *trace_file
= NULL
;
2352 #ifdef CONFIG_SECCOMP
2353 if (seccomp_start() < 0) {
2355 "seccomp: failed to install syscall filter in the kernel\n");
2360 atexit(qemu_run_exit_notifiers
);
2361 error_set_progname(argv
[0]);
2363 g_mem_set_vtable(&mem_trace
);
2364 if (!g_thread_supported()) {
2365 #if !GLIB_CHECK_VERSION(2, 31, 0)
2366 g_thread_init(NULL
);
2368 fprintf(stderr
, "glib threading failed to initialize.\n");
2373 module_call_init(MODULE_INIT_QOM
);
2378 rtc_clock
= host_clock
;
2380 qemu_cache_utils_init(envp
);
2382 QLIST_INIT (&vm_change_state_head
);
2383 os_setup_early_signal_handling();
2385 module_call_init(MODULE_INIT_MACHINE
);
2386 machine
= find_default_machine();
2390 cyls
= heads
= secs
= 0;
2391 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2393 for (i
= 0; i
< MAX_NODES
; i
++) {
2395 node_cpumask
[i
] = bitmap_new(MAX_CPUMASK_BITS
);
2403 /* first pass of option parsing */
2405 while (optind
< argc
) {
2406 if (argv
[optind
][0] != '-') {
2411 const QEMUOption
*popt
;
2413 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2414 switch (popt
->index
) {
2415 case QEMU_OPTION_nodefconfig
:
2418 case QEMU_OPTION_nouserconfig
:
2427 ret
= qemu_read_default_config_files(userconfig
);
2433 /* second pass of option parsing */
2438 if (argv
[optind
][0] != '-') {
2439 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2441 const QEMUOption
*popt
;
2443 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2444 if (!(popt
->arch_mask
& arch_type
)) {
2445 printf("Option %s not supported for this target\n", popt
->name
);
2448 switch(popt
->index
) {
2450 machine
= machine_parse(optarg
);
2452 case QEMU_OPTION_cpu
:
2453 /* hw initialization will check this */
2456 case QEMU_OPTION_hda
:
2460 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2462 snprintf(buf
, sizeof(buf
),
2463 "%s,cyls=%d,heads=%d,secs=%d%s",
2464 HD_OPTS
, cyls
, heads
, secs
,
2465 translation
== BIOS_ATA_TRANSLATION_LBA
?
2467 translation
== BIOS_ATA_TRANSLATION_NONE
?
2468 ",trans=none" : "");
2469 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2472 case QEMU_OPTION_hdb
:
2473 case QEMU_OPTION_hdc
:
2474 case QEMU_OPTION_hdd
:
2475 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2478 case QEMU_OPTION_drive
:
2479 if (drive_def(optarg
) == NULL
) {
2483 case QEMU_OPTION_set
:
2484 if (qemu_set_option(optarg
) != 0)
2487 case QEMU_OPTION_global
:
2488 if (qemu_global_option(optarg
) != 0)
2491 case QEMU_OPTION_mtdblock
:
2492 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2494 case QEMU_OPTION_sd
:
2495 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2497 case QEMU_OPTION_pflash
:
2498 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2500 case QEMU_OPTION_snapshot
:
2503 case QEMU_OPTION_hdachs
:
2507 cyls
= strtol(p
, (char **)&p
, 0);
2508 if (cyls
< 1 || cyls
> 16383)
2513 heads
= strtol(p
, (char **)&p
, 0);
2514 if (heads
< 1 || heads
> 16)
2519 secs
= strtol(p
, (char **)&p
, 0);
2520 if (secs
< 1 || secs
> 63)
2524 if (!strcmp(p
, "none"))
2525 translation
= BIOS_ATA_TRANSLATION_NONE
;
2526 else if (!strcmp(p
, "lba"))
2527 translation
= BIOS_ATA_TRANSLATION_LBA
;
2528 else if (!strcmp(p
, "auto"))
2529 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2532 } else if (*p
!= '\0') {
2534 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2537 if (hda_opts
!= NULL
) {
2539 snprintf(num
, sizeof(num
), "%d", cyls
);
2540 qemu_opt_set(hda_opts
, "cyls", num
);
2541 snprintf(num
, sizeof(num
), "%d", heads
);
2542 qemu_opt_set(hda_opts
, "heads", num
);
2543 snprintf(num
, sizeof(num
), "%d", secs
);
2544 qemu_opt_set(hda_opts
, "secs", num
);
2545 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2546 qemu_opt_set(hda_opts
, "trans", "lba");
2547 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2548 qemu_opt_set(hda_opts
, "trans", "none");
2552 case QEMU_OPTION_numa
:
2553 if (nb_numa_nodes
>= MAX_NODES
) {
2554 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2559 case QEMU_OPTION_display
:
2560 display_type
= select_display(optarg
);
2562 case QEMU_OPTION_nographic
:
2563 display_type
= DT_NOGRAPHIC
;
2565 case QEMU_OPTION_curses
:
2566 #ifdef CONFIG_CURSES
2567 display_type
= DT_CURSES
;
2569 fprintf(stderr
, "Curses support is disabled\n");
2573 case QEMU_OPTION_portrait
:
2574 graphic_rotate
= 90;
2576 case QEMU_OPTION_rotate
:
2577 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2578 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2579 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2581 "qemu: only 90, 180, 270 deg rotation is available\n");
2585 case QEMU_OPTION_kernel
:
2586 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2588 case QEMU_OPTION_initrd
:
2589 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2591 case QEMU_OPTION_append
:
2592 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2594 case QEMU_OPTION_dtb
:
2595 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
2597 case QEMU_OPTION_cdrom
:
2598 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2600 case QEMU_OPTION_boot
:
2602 static const char * const params
[] = {
2603 "order", "once", "menu",
2604 "splash", "splash-time", NULL
2606 char buf
[sizeof(boot_devices
)];
2607 char *standard_boot_devices
;
2610 if (!strchr(optarg
, '=')) {
2612 pstrcpy(buf
, sizeof(buf
), optarg
);
2613 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2615 "qemu: unknown boot parameter '%s' in '%s'\n",
2621 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2622 validate_bootdevices(buf
);
2623 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2626 if (get_param_value(buf
, sizeof(buf
),
2628 validate_bootdevices(buf
);
2629 standard_boot_devices
= g_strdup(boot_devices
);
2630 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2631 qemu_register_reset(restore_boot_devices
,
2632 standard_boot_devices
);
2634 if (get_param_value(buf
, sizeof(buf
),
2636 if (!strcmp(buf
, "on")) {
2638 } else if (!strcmp(buf
, "off")) {
2642 "qemu: invalid option value '%s'\n",
2647 qemu_opts_parse(qemu_find_opts("boot-opts"),
2652 case QEMU_OPTION_fda
:
2653 case QEMU_OPTION_fdb
:
2654 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2657 case QEMU_OPTION_no_fd_bootchk
:
2660 case QEMU_OPTION_netdev
:
2661 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2665 case QEMU_OPTION_net
:
2666 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2670 #ifdef CONFIG_LIBISCSI
2671 case QEMU_OPTION_iscsi
:
2672 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2679 case QEMU_OPTION_tftp
:
2680 legacy_tftp_prefix
= optarg
;
2682 case QEMU_OPTION_bootp
:
2683 legacy_bootp_filename
= optarg
;
2685 case QEMU_OPTION_redir
:
2686 if (net_slirp_redir(optarg
) < 0)
2690 case QEMU_OPTION_bt
:
2691 add_device_config(DEV_BT
, optarg
);
2693 case QEMU_OPTION_audio_help
:
2694 if (!(audio_available())) {
2695 printf("Option %s not supported for this target\n", popt
->name
);
2701 case QEMU_OPTION_soundhw
:
2702 if (!(audio_available())) {
2703 printf("Option %s not supported for this target\n", popt
->name
);
2706 select_soundhw (optarg
);
2711 case QEMU_OPTION_version
:
2715 case QEMU_OPTION_m
: {
2719 value
= strtosz(optarg
, &end
);
2720 if (value
< 0 || *end
) {
2721 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2725 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2726 fprintf(stderr
, "qemu: ram size too large\n");
2732 case QEMU_OPTION_mempath
:
2736 case QEMU_OPTION_mem_prealloc
:
2747 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2749 case QEMU_OPTION_gdb
:
2750 add_device_config(DEV_GDB
, optarg
);
2755 case QEMU_OPTION_bios
:
2758 case QEMU_OPTION_singlestep
:
2765 keyboard_layout
= optarg
;
2767 case QEMU_OPTION_localtime
:
2770 case QEMU_OPTION_vga
:
2779 w
= strtol(p
, (char **)&p
, 10);
2782 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2788 h
= strtol(p
, (char **)&p
, 10);
2793 depth
= strtol(p
, (char **)&p
, 10);
2794 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2795 depth
!= 24 && depth
!= 32)
2797 } else if (*p
== '\0') {
2798 depth
= graphic_depth
;
2805 graphic_depth
= depth
;
2808 case QEMU_OPTION_echr
:
2811 term_escape_char
= strtol(optarg
, &r
, 0);
2813 printf("Bad argument to echr\n");
2816 case QEMU_OPTION_monitor
:
2817 monitor_parse(optarg
, "readline");
2818 default_monitor
= 0;
2820 case QEMU_OPTION_qmp
:
2821 monitor_parse(optarg
, "control");
2822 default_monitor
= 0;
2824 case QEMU_OPTION_mon
:
2825 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2829 default_monitor
= 0;
2831 case QEMU_OPTION_chardev
:
2832 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2837 case QEMU_OPTION_fsdev
:
2838 olist
= qemu_find_opts("fsdev");
2840 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2843 opts
= qemu_opts_parse(olist
, optarg
, 1);
2845 fprintf(stderr
, "parse error: %s\n", optarg
);
2849 case QEMU_OPTION_virtfs
: {
2852 const char *writeout
, *sock_fd
, *socket
;
2854 olist
= qemu_find_opts("virtfs");
2856 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2859 opts
= qemu_opts_parse(olist
, optarg
, 1);
2861 fprintf(stderr
, "parse error: %s\n", optarg
);
2865 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2866 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2867 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2870 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2871 qemu_opt_get(opts
, "mount_tag"),
2874 fprintf(stderr
, "duplicate fsdev id: %s\n",
2875 qemu_opt_get(opts
, "mount_tag"));
2879 writeout
= qemu_opt_get(opts
, "writeout");
2881 #ifdef CONFIG_SYNC_FILE_RANGE
2882 qemu_opt_set(fsdev
, "writeout", writeout
);
2884 fprintf(stderr
, "writeout=immediate not supported on "
2889 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2890 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2891 qemu_opt_set(fsdev
, "security_model",
2892 qemu_opt_get(opts
, "security_model"));
2893 socket
= qemu_opt_get(opts
, "socket");
2895 qemu_opt_set(fsdev
, "socket", socket
);
2897 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2899 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2902 qemu_opt_set_bool(fsdev
, "readonly",
2903 qemu_opt_get_bool(opts
, "readonly", 0));
2904 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2906 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2907 qemu_opt_set(device
, "fsdev",
2908 qemu_opt_get(opts
, "mount_tag"));
2909 qemu_opt_set(device
, "mount_tag",
2910 qemu_opt_get(opts
, "mount_tag"));
2913 case QEMU_OPTION_virtfs_synth
: {
2917 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2920 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2923 qemu_opt_set(fsdev
, "fsdriver", "synth");
2925 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2927 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2928 qemu_opt_set(device
, "fsdev", "v_synth");
2929 qemu_opt_set(device
, "mount_tag", "v_synth");
2932 case QEMU_OPTION_serial
:
2933 add_device_config(DEV_SERIAL
, optarg
);
2935 if (strncmp(optarg
, "mon:", 4) == 0) {
2936 default_monitor
= 0;
2939 case QEMU_OPTION_watchdog
:
2942 "qemu: only one watchdog option may be given\n");
2947 case QEMU_OPTION_watchdog_action
:
2948 if (select_watchdog_action(optarg
) == -1) {
2949 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2953 case QEMU_OPTION_virtiocon
:
2954 add_device_config(DEV_VIRTCON
, optarg
);
2955 default_virtcon
= 0;
2956 if (strncmp(optarg
, "mon:", 4) == 0) {
2957 default_monitor
= 0;
2960 case QEMU_OPTION_parallel
:
2961 add_device_config(DEV_PARALLEL
, optarg
);
2962 default_parallel
= 0;
2963 if (strncmp(optarg
, "mon:", 4) == 0) {
2964 default_monitor
= 0;
2967 case QEMU_OPTION_debugcon
:
2968 add_device_config(DEV_DEBUGCON
, optarg
);
2970 case QEMU_OPTION_loadvm
:
2973 case QEMU_OPTION_full_screen
:
2977 case QEMU_OPTION_no_frame
:
2980 case QEMU_OPTION_alt_grab
:
2983 case QEMU_OPTION_ctrl_grab
:
2986 case QEMU_OPTION_no_quit
:
2989 case QEMU_OPTION_sdl
:
2990 display_type
= DT_SDL
;
2993 case QEMU_OPTION_no_frame
:
2994 case QEMU_OPTION_alt_grab
:
2995 case QEMU_OPTION_ctrl_grab
:
2996 case QEMU_OPTION_no_quit
:
2997 case QEMU_OPTION_sdl
:
2998 fprintf(stderr
, "SDL support is disabled\n");
3001 case QEMU_OPTION_pidfile
:
3004 case QEMU_OPTION_win2k_hack
:
3005 win2k_install_hack
= 1;
3007 case QEMU_OPTION_rtc_td_hack
: {
3008 static GlobalProperty slew_lost_ticks
[] = {
3010 .driver
= "mc146818rtc",
3011 .property
= "lost_tick_policy",
3014 { /* end of list */ }
3017 qdev_prop_register_global_list(slew_lost_ticks
);
3020 case QEMU_OPTION_acpitable
:
3021 do_acpitable_option(optarg
);
3023 case QEMU_OPTION_smbios
:
3024 do_smbios_option(optarg
);
3026 case QEMU_OPTION_enable_kvm
:
3027 olist
= qemu_find_opts("machine");
3028 qemu_opts_parse(olist
, "accel=kvm", 0);
3030 case QEMU_OPTION_machine
:
3031 olist
= qemu_find_opts("machine");
3032 opts
= qemu_opts_parse(olist
, optarg
, 1);
3034 fprintf(stderr
, "parse error: %s\n", optarg
);
3037 optarg
= qemu_opt_get(opts
, "type");
3039 machine
= machine_parse(optarg
);
3042 case QEMU_OPTION_usb
:
3045 case QEMU_OPTION_usbdevice
:
3047 add_device_config(DEV_USB
, optarg
);
3049 case QEMU_OPTION_device
:
3050 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3054 case QEMU_OPTION_smp
:
3057 fprintf(stderr
, "Invalid number of CPUs\n");
3060 if (max_cpus
< smp_cpus
) {
3061 fprintf(stderr
, "maxcpus must be equal to or greater than "
3065 if (max_cpus
> 255) {
3066 fprintf(stderr
, "Unsupported number of maxcpus\n");
3070 case QEMU_OPTION_vnc
:
3073 vnc_display
= optarg
;
3075 fprintf(stderr
, "VNC support is disabled\n");
3079 case QEMU_OPTION_no_acpi
:
3082 case QEMU_OPTION_no_hpet
:
3085 case QEMU_OPTION_balloon
:
3086 if (balloon_parse(optarg
) < 0) {
3087 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3091 case QEMU_OPTION_no_reboot
:
3094 case QEMU_OPTION_no_shutdown
:
3097 case QEMU_OPTION_show_cursor
:
3100 case QEMU_OPTION_uuid
:
3101 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3102 fprintf(stderr
, "Fail to parse UUID string."
3103 " Wrong format.\n");
3107 case QEMU_OPTION_option_rom
:
3108 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3109 fprintf(stderr
, "Too many option ROMs\n");
3112 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3113 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3114 option_rom
[nb_option_roms
].bootindex
=
3115 qemu_opt_get_number(opts
, "bootindex", -1);
3116 if (!option_rom
[nb_option_roms
].name
) {
3117 fprintf(stderr
, "Option ROM file is not specified\n");
3122 case QEMU_OPTION_semihosting
:
3123 semihosting_enabled
= 1;
3125 case QEMU_OPTION_name
:
3126 qemu_name
= g_strdup(optarg
);
3128 char *p
= strchr(qemu_name
, ',');
3131 if (strncmp(p
, "process=", 8)) {
3132 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3136 os_set_proc_name(p
);
3140 case QEMU_OPTION_prom_env
:
3141 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3142 fprintf(stderr
, "Too many prom variables\n");
3145 prom_envs
[nb_prom_envs
] = optarg
;
3148 case QEMU_OPTION_old_param
:
3151 case QEMU_OPTION_clock
:
3152 configure_alarms(optarg
);
3154 case QEMU_OPTION_startdate
:
3155 configure_rtc_date_offset(optarg
, 1);
3157 case QEMU_OPTION_rtc
:
3158 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3162 configure_rtc(opts
);
3164 case QEMU_OPTION_tb_size
:
3165 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3166 if (tcg_tb_size
< 0) {
3170 case QEMU_OPTION_icount
:
3171 icount_option
= optarg
;
3173 case QEMU_OPTION_incoming
:
3175 runstate_set(RUN_STATE_INMIGRATE
);
3177 case QEMU_OPTION_nodefaults
:
3179 default_parallel
= 0;
3180 default_virtcon
= 0;
3181 default_monitor
= 0;
3188 case QEMU_OPTION_xen_domid
:
3189 if (!(xen_available())) {
3190 printf("Option %s not supported for this target\n", popt
->name
);
3193 xen_domid
= atoi(optarg
);
3195 case QEMU_OPTION_xen_create
:
3196 if (!(xen_available())) {
3197 printf("Option %s not supported for this target\n", popt
->name
);
3200 xen_mode
= XEN_CREATE
;
3202 case QEMU_OPTION_xen_attach
:
3203 if (!(xen_available())) {
3204 printf("Option %s not supported for this target\n", popt
->name
);
3207 xen_mode
= XEN_ATTACH
;
3209 case QEMU_OPTION_trace
:
3211 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3215 trace_events
= qemu_opt_get(opts
, "events");
3216 trace_file
= qemu_opt_get(opts
, "file");
3219 case QEMU_OPTION_readconfig
:
3221 int ret
= qemu_read_config_file(optarg
);
3223 fprintf(stderr
, "read config %s: %s\n", optarg
,
3229 case QEMU_OPTION_spice
:
3230 olist
= qemu_find_opts("spice");
3232 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3235 opts
= qemu_opts_parse(olist
, optarg
, 0);
3237 fprintf(stderr
, "parse error: %s\n", optarg
);
3241 case QEMU_OPTION_writeconfig
:
3244 if (strcmp(optarg
, "-") == 0) {
3247 fp
= fopen(optarg
, "w");
3249 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3253 qemu_config_write(fp
);
3257 case QEMU_OPTION_qtest
:
3258 qtest_chrdev
= optarg
;
3260 case QEMU_OPTION_qtest_log
:
3264 os_parse_cmd_args(popt
->index
, optarg
);
3270 if (machine
== NULL
) {
3271 fprintf(stderr
, "No machine found.\n");
3275 if (machine
->hw_version
) {
3276 qemu_set_version(machine
->hw_version
);
3279 /* Init CPU def lists, based on config
3280 * - Must be called after all the qemu_read_config_file() calls
3281 * - Must be called before list_cpus()
3282 * - Must be called before machine->init()
3286 if (cpu_model
&& is_help_option(cpu_model
)) {
3287 list_cpus(stdout
, &fprintf
, cpu_model
);
3291 /* Open the logfile at this point, if necessary. We can't open the logfile
3292 * when encountering either of the logging options (-d or -D) because the
3293 * other one may be encountered later on the command line, changing the
3294 * location or level of logging.
3298 set_cpu_log_filename(log_file
);
3300 set_cpu_log(log_mask
);
3303 if (!trace_backend_init(trace_events
, trace_file
)) {
3307 /* If no data_dir is specified then try to find it relative to the
3310 data_dir
= os_find_datadir(argv
[0]);
3312 /* If all else fails use the install path specified when building. */
3314 data_dir
= CONFIG_QEMU_DATADIR
;
3318 * Default to max_cpus = smp_cpus, in case the user doesn't
3319 * specify a max_cpus value.
3322 max_cpus
= smp_cpus
;
3324 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3325 if (smp_cpus
> machine
->max_cpus
) {
3326 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3327 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3333 * Get the default machine options from the machine if it is not already
3334 * specified either by the configuration file or by the command line.
3336 if (machine
->default_machine_opts
) {
3337 qemu_opts_set_defaults(qemu_find_opts("machine"),
3338 machine
->default_machine_opts
, 0);
3341 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3342 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3344 if (machine
->no_serial
) {
3347 if (machine
->no_parallel
) {
3348 default_parallel
= 0;
3350 if (!machine
->use_virtcon
) {
3351 default_virtcon
= 0;
3353 if (machine
->no_floppy
) {
3356 if (machine
->no_cdrom
) {
3359 if (machine
->no_sdcard
) {
3363 if (display_type
== DT_NOGRAPHIC
) {
3364 if (default_parallel
)
3365 add_device_config(DEV_PARALLEL
, "null");
3366 if (default_serial
&& default_monitor
) {
3367 add_device_config(DEV_SERIAL
, "mon:stdio");
3368 } else if (default_virtcon
&& default_monitor
) {
3369 add_device_config(DEV_VIRTCON
, "mon:stdio");
3372 add_device_config(DEV_SERIAL
, "stdio");
3373 if (default_virtcon
)
3374 add_device_config(DEV_VIRTCON
, "stdio");
3375 if (default_monitor
)
3376 monitor_parse("stdio", "readline");
3380 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3381 if (default_parallel
)
3382 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3383 if (default_monitor
)
3384 monitor_parse("vc:80Cx24C", "readline");
3385 if (default_virtcon
)
3386 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3391 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3393 #ifdef CONFIG_VIRTFS
3394 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3401 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3406 /* init the memory */
3407 if (ram_size
== 0) {
3408 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3411 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
3416 configure_accelerator();
3418 qemu_init_cpu_loop();
3419 if (qemu_init_main_loop()) {
3420 fprintf(stderr
, "qemu_init_main_loop failed\n");
3424 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3426 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3427 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3428 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3430 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3433 if (!kernel_cmdline
) {
3434 kernel_cmdline
= "";
3437 linux_boot
= (kernel_filename
!= NULL
);
3439 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3440 fprintf(stderr
, "-append only allowed with -kernel option\n");
3444 if (!linux_boot
&& initrd_filename
!= NULL
) {
3445 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3449 if (!linux_boot
&& machine_opts
&& qemu_opt_get(machine_opts
, "dtb")) {
3450 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
3454 os_set_line_buffering();
3456 if (init_timer_alarm() < 0) {
3457 fprintf(stderr
, "could not initialize alarm timer\n");
3462 /* spice needs the timers to be initialized by this point */
3466 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3467 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3470 configure_icount(icount_option
);
3472 if (net_init_clients() < 0) {
3476 /* init the bluetooth world */
3477 if (foreach_device_config(DEV_BT
, bt_parse
))
3480 if (!xen_enabled()) {
3481 /* On 32-bit hosts, QEMU is limited by virtual address space */
3482 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3483 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3488 cpu_exec_init_all();
3490 bdrv_init_with_whitelist();
3494 /* open the virtual block devices */
3496 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3497 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3500 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3501 IF_DEFAULT
, 2, CDROM_OPTS
);
3502 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3503 IF_FLOPPY
, 0, FD_OPTS
);
3504 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3507 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
3509 if (nb_numa_nodes
> 0) {
3512 if (nb_numa_nodes
> MAX_NODES
) {
3513 nb_numa_nodes
= MAX_NODES
;
3516 /* If no memory size if given for any node, assume the default case
3517 * and distribute the available memory equally across all nodes
3519 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3520 if (node_mem
[i
] != 0)
3523 if (i
== nb_numa_nodes
) {
3524 uint64_t usedmem
= 0;
3526 /* On Linux, the each node's border has to be 8MB aligned,
3527 * the final node gets the rest.
3529 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3530 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3531 usedmem
+= node_mem
[i
];
3533 node_mem
[i
] = ram_size
- usedmem
;
3536 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3537 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
3541 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3542 * must cope with this anyway, because there are BIOSes out there in
3543 * real machines which also use this scheme.
3545 if (i
== nb_numa_nodes
) {
3546 for (i
= 0; i
< max_cpus
; i
++) {
3547 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
3552 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3556 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3558 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3560 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3562 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3565 /* If no default VGA is requested, the default is "none". */
3566 if (default_vga
&& cirrus_vga_available()) {
3567 vga_model
= "cirrus";
3569 select_vgahw(vga_model
);
3572 i
= select_watchdog(watchdog
);
3574 exit (i
== 1 ? 1 : 0);
3577 if (machine
->compat_props
) {
3578 qdev_prop_register_global_list(machine
->compat_props
);
3582 qdev_machine_init();
3584 machine
->init(ram_size
, boot_devices
,
3585 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3587 cpu_synchronize_all_post_init();
3591 current_machine
= machine
;
3593 /* init USB devices */
3595 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3599 /* init generic devices */
3600 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3603 net_check_clients();
3605 /* just use the first displaystate for the moment */
3606 ds
= get_displaystate();
3610 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3611 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3612 display_type
= DT_SDL
;
3613 #elif defined(CONFIG_VNC)
3614 vnc_display
= "localhost:0,to=99";
3617 display_type
= DT_NONE
;
3622 /* init local displays */
3623 switch (display_type
) {
3626 #if defined(CONFIG_CURSES)
3628 curses_display_init(ds
, full_screen
);
3631 #if defined(CONFIG_SDL)
3633 sdl_display_init(ds
, full_screen
, no_frame
);
3635 #elif defined(CONFIG_COCOA)
3637 cocoa_display_init(ds
, full_screen
);
3644 /* must be after terminal init, SDL library changes signal handlers */
3645 os_setup_signal_handling();
3648 /* init remote displays */
3650 vnc_display_init(ds
);
3651 if (vnc_display_open(ds
, vnc_display
) < 0) {
3652 fprintf(stderr
, "Failed to start VNC server on `%s'\n",
3657 if (show_vnc_port
) {
3658 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3663 if (using_spice
&& !qxl_enabled
) {
3664 qemu_spice_display_init(ds
);
3670 dcl
= ds
->listeners
;
3671 while (dcl
!= NULL
) {
3672 if (dcl
->dpy_refresh
!= NULL
) {
3673 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3674 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3679 text_consoles_set_display(ds
);
3681 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3685 qdev_machine_creation_done();
3687 if (rom_load_all() != 0) {
3688 fprintf(stderr
, "rom loading failed\n");
3692 /* TODO: once all bus devices are qdevified, this should be done
3693 * when bus is created by qdev.c */
3694 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3695 qemu_run_machine_init_done_notifiers();
3697 qemu_system_reset(VMRESET_SILENT
);
3699 if (load_vmstate(loadvm
) < 0) {
3706 int ret
= qemu_start_incoming_migration(incoming
, &errp
);
3708 if (error_is_set(&errp
)) {
3709 fprintf(stderr
, "Migrate: %s\n", error_get_pretty(errp
));
3712 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3716 } else if (autostart
) {