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 <sys/ethernet.h>
69 #include <sys/sockio.h>
70 #include <netinet/arp.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_icmp.h> // must come after ip.h
74 #include <netinet/udp.h>
75 #include <netinet/tcp.h>
83 #if defined(CONFIG_VDE)
84 #include <libvdeplug.h>
92 #if defined(__APPLE__) || defined(main)
94 int qemu_main(int argc
, char **argv
, char **envp
);
95 int main(int argc
, char **argv
)
97 return qemu_main(argc
, argv
, NULL
);
100 #define main qemu_main
102 #endif /* CONFIG_SDL */
106 #define main qemu_main
107 #endif /* CONFIG_COCOA */
112 #include "hw/boards.h"
114 #include "hw/pcmcia.h"
119 #include "hw/watchdog.h"
120 #include "hw/smbios.h"
123 #include "hw/loader.h"
126 #include "net/slirp.h"
131 #include "qemu-timer.h"
132 #include "qemu-char.h"
133 #include "cache-utils.h"
134 #include "blockdev.h"
135 #include "hw/block-common.h"
136 #include "block-migration.h"
138 #include "audio/audio.h"
139 #include "migration.h"
142 #include "qemu-option.h"
143 #include "qemu-config.h"
144 #include "qemu-options.h"
145 #include "qmp-commands.h"
146 #include "main-loop.h"
148 #include "fsdev/qemu-fsdev.h"
154 #include "qemu_socket.h"
156 #include "slirp/libslirp.h"
159 #include "trace/control.h"
160 #include "qemu-queue.h"
162 #include "arch_init.h"
165 #include "ui/qemu-spice.h"
168 //#define DEBUG_SLIRP
170 #define DEFAULT_RAM_SIZE 128
172 #define MAX_VIRTIO_CONSOLES 1
174 static const char *data_dir
;
175 const char *bios_name
= NULL
;
176 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
177 DisplayType display_type
= DT_DEFAULT
;
178 int display_remote
= 0;
179 const char* keyboard_layout
= NULL
;
181 const char *mem_path
= NULL
;
183 int mem_prealloc
= 0; /* force preallocation of physical target memory */
186 NICInfo nd_table
[MAX_NICS
];
188 static int rtc_utc
= 1;
189 static int rtc_date_offset
= -1; /* -1 means no change */
190 QEMUClock
*rtc_clock
;
191 int vga_interface_type
= VGA_NONE
;
192 static int full_screen
= 0;
194 static int no_frame
= 0;
197 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
198 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
199 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
200 int win2k_install_hack
= 0;
208 const char *vnc_display
;
210 int acpi_enabled
= 1;
216 int graphic_rotate
= 0;
217 const char *watchdog
;
218 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
220 int semihosting_enabled
= 0;
222 const char *qemu_name
;
225 unsigned int nb_prom_envs
= 0;
226 const char *prom_envs
[MAX_PROM_ENVS
];
228 uint8_t *boot_splash_filedata
;
229 int boot_splash_filedata_size
;
230 uint8_t qemu_extra_params_fw
[2];
232 typedef struct FWBootEntry FWBootEntry
;
235 QTAILQ_ENTRY(FWBootEntry
) link
;
241 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
244 uint64_t node_mem
[MAX_NODES
];
245 unsigned long *node_cpumask
[MAX_NODES
];
247 uint8_t qemu_uuid
[16];
249 static QEMUBootSetHandler
*boot_set_handler
;
250 static void *boot_set_opaque
;
252 static NotifierList exit_notifiers
=
253 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
255 static NotifierList machine_init_done_notifiers
=
256 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
258 static int tcg_allowed
= 1;
262 enum xen_mode xen_mode
= XEN_EMULATE
;
263 static int tcg_tb_size
;
265 static int default_serial
= 1;
266 static int default_parallel
= 1;
267 static int default_virtcon
= 1;
268 static int default_monitor
= 1;
269 static int default_floppy
= 1;
270 static int default_cdrom
= 1;
271 static int default_sdcard
= 1;
272 static int default_vga
= 1;
278 { .driver
= "isa-serial", .flag
= &default_serial
},
279 { .driver
= "isa-parallel", .flag
= &default_parallel
},
280 { .driver
= "isa-fdc", .flag
= &default_floppy
},
281 { .driver
= "ide-cd", .flag
= &default_cdrom
},
282 { .driver
= "ide-hd", .flag
= &default_cdrom
},
283 { .driver
= "ide-drive", .flag
= &default_cdrom
},
284 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
285 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
286 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
287 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
288 { .driver
= "VGA", .flag
= &default_vga
},
289 { .driver
= "isa-vga", .flag
= &default_vga
},
290 { .driver
= "cirrus-vga", .flag
= &default_vga
},
291 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
292 { .driver
= "vmware-svga", .flag
= &default_vga
},
293 { .driver
= "qxl-vga", .flag
= &default_vga
},
296 static void res_free(void)
298 if (boot_splash_filedata
!= NULL
) {
299 g_free(boot_splash_filedata
);
300 boot_splash_filedata
= NULL
;
304 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
306 const char *driver
= qemu_opt_get(opts
, "driver");
311 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
312 if (strcmp(default_list
[i
].driver
, driver
) != 0)
314 *(default_list
[i
].flag
) = 0;
319 /***********************************************************/
322 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
327 } RunStateTransition
;
329 static const RunStateTransition runstate_transitions_def
[] = {
331 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
333 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
334 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
336 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
337 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
339 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
340 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
342 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
343 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
345 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
346 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
348 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
349 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
350 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
352 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
353 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
355 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
357 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
358 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
359 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
360 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
361 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
362 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
363 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
364 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
365 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
367 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
369 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
370 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
372 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
373 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
374 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
375 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
377 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
378 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
380 { RUN_STATE_MAX
, RUN_STATE_MAX
},
383 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
385 bool runstate_check(RunState state
)
387 return current_run_state
== state
;
390 void runstate_init(void)
392 const RunStateTransition
*p
;
394 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
396 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
397 runstate_valid_transitions
[p
->from
][p
->to
] = true;
401 /* This function will abort() on invalid state transitions */
402 void runstate_set(RunState new_state
)
404 assert(new_state
< RUN_STATE_MAX
);
406 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
407 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
408 RunState_lookup
[current_run_state
],
409 RunState_lookup
[new_state
]);
413 current_run_state
= new_state
;
416 int runstate_is_running(void)
418 return runstate_check(RUN_STATE_RUNNING
);
421 StatusInfo
*qmp_query_status(Error
**errp
)
423 StatusInfo
*info
= g_malloc0(sizeof(*info
));
425 info
->running
= runstate_is_running();
426 info
->singlestep
= singlestep
;
427 info
->status
= current_run_state
;
432 /***********************************************************/
433 /* real time host monotonic timer */
435 /***********************************************************/
436 /* host time/date access */
437 void qemu_get_timedate(struct tm
*tm
, int offset
)
444 if (rtc_date_offset
== -1) {
448 ret
= localtime(&ti
);
450 ti
-= rtc_date_offset
;
454 memcpy(tm
, ret
, sizeof(struct tm
));
457 int qemu_timedate_diff(struct tm
*tm
)
461 if (rtc_date_offset
== -1)
463 seconds
= mktimegm(tm
);
466 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
467 seconds
= mktime(&tmp
);
470 seconds
= mktimegm(tm
) + rtc_date_offset
;
472 return seconds
- time(NULL
);
475 void rtc_change_mon_event(struct tm
*tm
)
479 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
480 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
481 qobject_decref(data
);
484 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
486 time_t rtc_start_date
;
489 if (!strcmp(startdate
, "now") && legacy
) {
490 rtc_date_offset
= -1;
492 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
500 } else if (sscanf(startdate
, "%d-%d-%d",
512 rtc_start_date
= mktimegm(&tm
);
513 if (rtc_start_date
== -1) {
515 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
516 "'2006-06-17T16:01:21' or '2006-06-17'\n");
519 rtc_date_offset
= time(NULL
) - rtc_start_date
;
523 static void configure_rtc(QemuOpts
*opts
)
527 value
= qemu_opt_get(opts
, "base");
529 if (!strcmp(value
, "utc")) {
531 } else if (!strcmp(value
, "localtime")) {
534 configure_rtc_date_offset(value
, 0);
537 value
= qemu_opt_get(opts
, "clock");
539 if (!strcmp(value
, "host")) {
540 rtc_clock
= host_clock
;
541 } else if (!strcmp(value
, "rt")) {
542 rtc_clock
= rt_clock
;
543 } else if (!strcmp(value
, "vm")) {
544 rtc_clock
= vm_clock
;
546 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
550 value
= qemu_opt_get(opts
, "driftfix");
552 if (!strcmp(value
, "slew")) {
553 static GlobalProperty slew_lost_ticks
[] = {
555 .driver
= "mc146818rtc",
556 .property
= "lost_tick_policy",
559 { /* end of list */ }
562 qdev_prop_register_global_list(slew_lost_ticks
);
563 } else if (!strcmp(value
, "none")) {
564 /* discard is default */
566 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
572 /***********************************************************/
573 /* Bluetooth support */
576 static struct HCIInfo
*hci_table
[MAX_NICS
];
578 static struct bt_vlan_s
{
579 struct bt_scatternet_s net
;
581 struct bt_vlan_s
*next
;
584 /* find or alloc a new bluetooth "VLAN" */
585 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
587 struct bt_vlan_s
**pvlan
, *vlan
;
588 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
592 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
594 pvlan
= &first_bt_vlan
;
595 while (*pvlan
!= NULL
)
596 pvlan
= &(*pvlan
)->next
;
601 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
605 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
610 static struct HCIInfo null_hci
= {
611 .cmd_send
= null_hci_send
,
612 .sco_send
= null_hci_send
,
613 .acl_send
= null_hci_send
,
614 .bdaddr_set
= null_hci_addr_set
,
617 struct HCIInfo
*qemu_next_hci(void)
619 if (cur_hci
== nb_hcis
)
622 return hci_table
[cur_hci
++];
625 static struct HCIInfo
*hci_init(const char *str
)
628 struct bt_scatternet_s
*vlan
= 0;
630 if (!strcmp(str
, "null"))
633 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
635 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
636 else if (!strncmp(str
, "hci", 3)) {
639 if (!strncmp(str
+ 3, ",vlan=", 6)) {
640 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
645 vlan
= qemu_find_bt_vlan(0);
647 return bt_new_hci(vlan
);
650 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
655 static int bt_hci_parse(const char *str
)
660 if (nb_hcis
>= MAX_NICS
) {
661 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
674 bdaddr
.b
[5] = 0x56 + nb_hcis
;
675 hci
->bdaddr_set(hci
, bdaddr
.b
);
677 hci_table
[nb_hcis
++] = hci
;
682 static void bt_vhci_add(int vlan_id
)
684 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
687 fprintf(stderr
, "qemu: warning: adding a VHCI to "
688 "an empty scatternet %i\n", vlan_id
);
690 bt_vhci_init(bt_new_hci(vlan
));
693 static struct bt_device_s
*bt_device_add(const char *opt
)
695 struct bt_scatternet_s
*vlan
;
697 char *endp
= strstr(opt
, ",vlan=");
698 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
701 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
704 vlan_id
= strtol(endp
+ 6, &endp
, 0);
706 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
711 vlan
= qemu_find_bt_vlan(vlan_id
);
714 fprintf(stderr
, "qemu: warning: adding a slave device to "
715 "an empty scatternet %i\n", vlan_id
);
717 if (!strcmp(devname
, "keyboard"))
718 return bt_keyboard_init(vlan
);
720 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
724 static int bt_parse(const char *opt
)
726 const char *endp
, *p
;
729 if (strstart(opt
, "hci", &endp
)) {
730 if (!*endp
|| *endp
== ',') {
732 if (!strstart(endp
, ",vlan=", 0))
735 return bt_hci_parse(opt
);
737 } else if (strstart(opt
, "vhci", &endp
)) {
738 if (!*endp
|| *endp
== ',') {
740 if (strstart(endp
, ",vlan=", &p
)) {
741 vlan
= strtol(p
, (char **) &endp
, 0);
743 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
747 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
756 } else if (strstart(opt
, "device:", &endp
))
757 return !bt_device_add(endp
);
759 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
763 /***********************************************************/
764 /* QEMU Block devices */
766 #define HD_OPTS "media=disk"
767 #define CDROM_OPTS "media=cdrom"
769 #define PFLASH_OPTS ""
773 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
775 int *use_scsi
= opaque
;
777 return drive_init(opts
, *use_scsi
) == NULL
;
780 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
782 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
783 qemu_opt_set(opts
, "snapshot", "on");
788 static void default_drive(int enable
, int snapshot
, int use_scsi
,
789 BlockInterfaceType type
, int index
,
794 if (type
== IF_DEFAULT
) {
795 type
= use_scsi
? IF_SCSI
: IF_IDE
;
798 if (!enable
|| drive_get_by_index(type
, index
)) {
802 opts
= drive_add(type
, index
, NULL
, optstr
);
804 drive_enable_snapshot(opts
, NULL
);
806 if (!drive_init(opts
, use_scsi
)) {
811 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
813 boot_set_handler
= func
;
814 boot_set_opaque
= opaque
;
817 int qemu_boot_set(const char *boot_devices
)
819 if (!boot_set_handler
) {
822 return boot_set_handler(boot_set_opaque
, boot_devices
);
825 static void validate_bootdevices(char *devices
)
827 /* We just do some generic consistency checks */
831 for (p
= devices
; *p
!= '\0'; p
++) {
832 /* Allowed boot devices are:
833 * a-b: floppy disk drives
834 * c-f: IDE disk drives
835 * g-m: machine implementation dependent drives
836 * n-p: network devices
837 * It's up to each machine implementation to check if the given boot
838 * devices match the actual hardware implementation and firmware
841 if (*p
< 'a' || *p
> 'p') {
842 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
845 if (bitmap
& (1 << (*p
- 'a'))) {
846 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
849 bitmap
|= 1 << (*p
- 'a');
853 static void restore_boot_devices(void *opaque
)
855 char *standard_boot_devices
= opaque
;
856 static int first
= 1;
858 /* Restore boot order and remove ourselves after the first boot */
864 qemu_boot_set(standard_boot_devices
);
866 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
867 g_free(standard_boot_devices
);
870 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
873 FWBootEntry
*node
, *i
;
879 assert(dev
!= NULL
|| suffix
!= NULL
);
881 node
= g_malloc0(sizeof(FWBootEntry
));
882 node
->bootindex
= bootindex
;
883 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
886 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
887 if (i
->bootindex
== bootindex
) {
888 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
890 } else if (i
->bootindex
< bootindex
) {
893 QTAILQ_INSERT_BEFORE(i
, node
, link
);
896 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
900 * This function returns null terminated string that consist of new line
901 * separated device paths.
903 * memory pointed by "size" is assigned total length of the array in bytes
906 char *get_boot_devices_list(uint32_t *size
)
912 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
913 char *devpath
= NULL
, *bootpath
;
917 devpath
= qdev_get_fw_dev_path(i
->dev
);
921 if (i
->suffix
&& devpath
) {
922 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
924 bootpath
= g_malloc(bootpathlen
);
925 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
927 } else if (devpath
) {
931 bootpath
= g_strdup(i
->suffix
);
935 list
[total
-1] = '\n';
937 len
= strlen(bootpath
) + 1;
938 list
= g_realloc(list
, total
+ len
);
939 memcpy(&list
[total
], bootpath
, len
);
949 static void numa_add(const char *optarg
)
953 unsigned long long value
, endvalue
;
956 value
= endvalue
= 0ULL;
958 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
959 if (!strcmp(option
, "node")) {
960 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
961 nodenr
= nb_numa_nodes
;
963 nodenr
= strtoull(option
, NULL
, 10);
966 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
967 node_mem
[nodenr
] = 0;
970 sval
= strtosz(option
, &endptr
);
971 if (sval
< 0 || *endptr
) {
972 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
975 node_mem
[nodenr
] = sval
;
977 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
978 value
= strtoull(option
, &endptr
, 10);
979 if (*endptr
== '-') {
980 endvalue
= strtoull(endptr
+1, &endptr
, 10);
985 if (!(endvalue
< MAX_CPUMASK_BITS
)) {
986 endvalue
= MAX_CPUMASK_BITS
- 1;
988 "A max of %d CPUs are supported in a guest\n",
992 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
999 static void smp_parse(const char *optarg
)
1001 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1005 smp
= strtoul(optarg
, &endptr
, 10);
1006 if (endptr
!= optarg
) {
1007 if (*endptr
== ',') {
1011 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1012 sockets
= strtoull(option
, NULL
, 10);
1013 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1014 cores
= strtoull(option
, NULL
, 10);
1015 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1016 threads
= strtoull(option
, NULL
, 10);
1017 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1018 max_cpus
= strtoull(option
, NULL
, 10);
1020 /* compute missing values, prefer sockets over cores over threads */
1021 if (smp
== 0 || sockets
== 0) {
1022 sockets
= sockets
> 0 ? sockets
: 1;
1023 cores
= cores
> 0 ? cores
: 1;
1024 threads
= threads
> 0 ? threads
: 1;
1026 smp
= cores
* threads
* sockets
;
1030 threads
= threads
> 0 ? threads
: 1;
1031 cores
= smp
/ (sockets
* threads
);
1033 threads
= smp
/ (cores
* sockets
);
1037 smp_cores
= cores
> 0 ? cores
: 1;
1038 smp_threads
= threads
> 0 ? threads
: 1;
1040 max_cpus
= smp_cpus
;
1043 /***********************************************************/
1046 static int usb_device_add(const char *devname
)
1049 USBDevice
*dev
= NULL
;
1054 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1055 dev
= usbdevice_create(devname
);
1059 /* the other ones */
1060 #ifndef CONFIG_LINUX
1061 /* only the linux version is qdev-ified, usb-bsd still needs this */
1062 if (strstart(devname
, "host:", &p
)) {
1063 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1066 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1067 dev
= usb_bt_init(usb_bus_find(-1),
1068 devname
[2] ? hci_init(p
)
1069 : bt_new_hci(qemu_find_bt_vlan(0)));
1080 static int usb_device_del(const char *devname
)
1085 if (strstart(devname
, "host:", &p
))
1086 return usb_host_device_close(p
);
1091 p
= strchr(devname
, '.');
1094 bus_num
= strtoul(devname
, NULL
, 0);
1095 addr
= strtoul(p
+ 1, NULL
, 0);
1097 return usb_device_delete_addr(bus_num
, addr
);
1100 static int usb_parse(const char *cmdline
)
1103 r
= usb_device_add(cmdline
);
1105 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1110 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1112 const char *devname
= qdict_get_str(qdict
, "devname");
1113 if (usb_device_add(devname
) < 0) {
1114 error_report("could not add USB device '%s'", devname
);
1118 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1120 const char *devname
= qdict_get_str(qdict
, "devname");
1121 if (usb_device_del(devname
) < 0) {
1122 error_report("could not delete USB device '%s'", devname
);
1126 /***********************************************************/
1127 /* PCMCIA/Cardbus */
1129 static struct pcmcia_socket_entry_s
{
1130 PCMCIASocket
*socket
;
1131 struct pcmcia_socket_entry_s
*next
;
1132 } *pcmcia_sockets
= 0;
1134 void pcmcia_socket_register(PCMCIASocket
*socket
)
1136 struct pcmcia_socket_entry_s
*entry
;
1138 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1139 entry
->socket
= socket
;
1140 entry
->next
= pcmcia_sockets
;
1141 pcmcia_sockets
= entry
;
1144 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1146 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1148 ptr
= &pcmcia_sockets
;
1149 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1150 if (entry
->socket
== socket
) {
1156 void pcmcia_info(Monitor
*mon
)
1158 struct pcmcia_socket_entry_s
*iter
;
1160 if (!pcmcia_sockets
)
1161 monitor_printf(mon
, "No PCMCIA sockets\n");
1163 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1164 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1165 iter
->socket
->attached
? iter
->socket
->card_string
:
1169 /***********************************************************/
1170 /* machine registration */
1172 static QEMUMachine
*first_machine
= NULL
;
1173 QEMUMachine
*current_machine
= NULL
;
1175 int qemu_register_machine(QEMUMachine
*m
)
1178 pm
= &first_machine
;
1186 static QEMUMachine
*find_machine(const char *name
)
1190 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1191 if (!strcmp(m
->name
, name
))
1193 if (m
->alias
&& !strcmp(m
->alias
, name
))
1199 QEMUMachine
*find_default_machine(void)
1203 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1204 if (m
->is_default
) {
1211 /***********************************************************/
1212 /* main execution loop */
1214 static void gui_update(void *opaque
)
1216 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1217 DisplayState
*ds
= opaque
;
1218 DisplayChangeListener
*dcl
= ds
->listeners
;
1222 while (dcl
!= NULL
) {
1223 if (dcl
->gui_timer_interval
&&
1224 dcl
->gui_timer_interval
< interval
)
1225 interval
= dcl
->gui_timer_interval
;
1228 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1231 struct vm_change_state_entry
{
1232 VMChangeStateHandler
*cb
;
1234 QLIST_ENTRY (vm_change_state_entry
) entries
;
1237 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1239 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1242 VMChangeStateEntry
*e
;
1244 e
= g_malloc0(sizeof (*e
));
1248 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1252 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1254 QLIST_REMOVE (e
, entries
);
1258 void vm_state_notify(int running
, RunState state
)
1260 VMChangeStateEntry
*e
;
1262 trace_vm_state_notify(running
, state
);
1264 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1265 e
->cb(e
->opaque
, running
, state
);
1271 if (!runstate_is_running()) {
1273 runstate_set(RUN_STATE_RUNNING
);
1274 vm_state_notify(1, RUN_STATE_RUNNING
);
1276 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1280 /* reset/shutdown handler */
1282 typedef struct QEMUResetEntry
{
1283 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1284 QEMUResetHandler
*func
;
1288 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1289 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1290 static int reset_requested
;
1291 static int shutdown_requested
, shutdown_signal
= -1;
1292 static pid_t shutdown_pid
;
1293 static int powerdown_requested
;
1294 static int debug_requested
;
1295 static int suspend_requested
;
1296 static NotifierList suspend_notifiers
=
1297 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1298 static NotifierList wakeup_notifiers
=
1299 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1300 static uint32_t wakeup_reason_mask
= ~0;
1301 static RunState vmstop_requested
= RUN_STATE_MAX
;
1303 int qemu_shutdown_requested_get(void)
1305 return shutdown_requested
;
1308 int qemu_reset_requested_get(void)
1310 return reset_requested
;
1313 int qemu_shutdown_requested(void)
1315 int r
= shutdown_requested
;
1316 shutdown_requested
= 0;
1320 void qemu_kill_report(void)
1322 if (!qtest_enabled() && shutdown_signal
!= -1) {
1323 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1324 if (shutdown_pid
== 0) {
1325 /* This happens for eg ^C at the terminal, so it's worth
1326 * avoiding printing an odd message in that case.
1328 fputc('\n', stderr
);
1330 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1332 shutdown_signal
= -1;
1336 int qemu_reset_requested(void)
1338 int r
= reset_requested
;
1339 reset_requested
= 0;
1343 static int qemu_suspend_requested(void)
1345 int r
= suspend_requested
;
1346 suspend_requested
= 0;
1350 int qemu_powerdown_requested(void)
1352 int r
= powerdown_requested
;
1353 powerdown_requested
= 0;
1357 static int qemu_debug_requested(void)
1359 int r
= debug_requested
;
1360 debug_requested
= 0;
1364 /* We use RUN_STATE_MAX but any invalid value will do */
1365 static bool qemu_vmstop_requested(RunState
*r
)
1367 if (vmstop_requested
< RUN_STATE_MAX
) {
1368 *r
= vmstop_requested
;
1369 vmstop_requested
= RUN_STATE_MAX
;
1376 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1378 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1381 re
->opaque
= opaque
;
1382 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1385 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1389 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1390 if (re
->func
== func
&& re
->opaque
== opaque
) {
1391 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1398 void qemu_system_reset(bool report
)
1400 QEMUResetEntry
*re
, *nre
;
1402 /* reset all devices */
1403 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1404 re
->func(re
->opaque
);
1407 monitor_protocol_event(QEVENT_RESET
, NULL
);
1409 cpu_synchronize_all_post_reset();
1412 void qemu_system_reset_request(void)
1415 shutdown_requested
= 1;
1417 reset_requested
= 1;
1420 qemu_notify_event();
1423 static void qemu_system_suspend(void)
1426 notifier_list_notify(&suspend_notifiers
, NULL
);
1427 runstate_set(RUN_STATE_SUSPENDED
);
1428 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1431 void qemu_system_suspend_request(void)
1433 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1436 suspend_requested
= 1;
1438 qemu_notify_event();
1441 void qemu_register_suspend_notifier(Notifier
*notifier
)
1443 notifier_list_add(&suspend_notifiers
, notifier
);
1446 void qemu_system_wakeup_request(WakeupReason reason
)
1448 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1451 if (!(wakeup_reason_mask
& (1 << reason
))) {
1454 runstate_set(RUN_STATE_RUNNING
);
1455 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1456 notifier_list_notify(&wakeup_notifiers
, &reason
);
1457 reset_requested
= 1;
1458 qemu_notify_event();
1461 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1464 wakeup_reason_mask
|= (1 << reason
);
1466 wakeup_reason_mask
&= ~(1 << reason
);
1470 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1472 notifier_list_add(&wakeup_notifiers
, notifier
);
1475 void qemu_system_killed(int signal
, pid_t pid
)
1477 shutdown_signal
= signal
;
1480 qemu_system_shutdown_request();
1483 void qemu_system_shutdown_request(void)
1485 shutdown_requested
= 1;
1486 qemu_notify_event();
1489 void qemu_system_powerdown_request(void)
1491 powerdown_requested
= 1;
1492 qemu_notify_event();
1495 void qemu_system_debug_request(void)
1497 debug_requested
= 1;
1498 qemu_notify_event();
1501 void qemu_system_vmstop_request(RunState state
)
1503 vmstop_requested
= state
;
1504 qemu_notify_event();
1507 qemu_irq qemu_system_powerdown
;
1509 static bool main_loop_should_exit(void)
1512 if (qemu_debug_requested()) {
1513 vm_stop(RUN_STATE_DEBUG
);
1515 if (qemu_suspend_requested()) {
1516 qemu_system_suspend();
1518 if (qemu_shutdown_requested()) {
1520 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1522 vm_stop(RUN_STATE_SHUTDOWN
);
1527 if (qemu_reset_requested()) {
1529 cpu_synchronize_all_states();
1530 qemu_system_reset(VMRESET_REPORT
);
1532 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1533 runstate_check(RUN_STATE_SHUTDOWN
)) {
1534 runstate_set(RUN_STATE_PAUSED
);
1537 if (qemu_powerdown_requested()) {
1538 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1539 qemu_irq_raise(qemu_system_powerdown
);
1541 if (qemu_vmstop_requested(&r
)) {
1547 static void main_loop(void)
1551 #ifdef CONFIG_PROFILER
1555 nonblocking
= !kvm_enabled() && last_io
> 0;
1556 #ifdef CONFIG_PROFILER
1557 ti
= profile_getclock();
1559 last_io
= main_loop_wait(nonblocking
);
1560 #ifdef CONFIG_PROFILER
1561 dev_time
+= profile_getclock() - ti
;
1563 } while (!main_loop_should_exit());
1566 static void version(void)
1568 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1571 static void help(int exitcode
)
1574 printf("usage: %s [options] [disk_image]\n\n"
1575 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1576 error_get_progname());
1578 #define QEMU_OPTIONS_GENERATE_HELP
1579 #include "qemu-options-wrapper.h"
1581 printf("\nDuring emulation, the following keys are useful:\n"
1582 "ctrl-alt-f toggle full screen\n"
1583 "ctrl-alt-n switch to virtual console 'n'\n"
1584 "ctrl-alt toggle mouse and keyboard grab\n"
1586 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1591 #define HAS_ARG 0x0001
1593 typedef struct QEMUOption
{
1600 static const QEMUOption qemu_options
[] = {
1601 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1602 #define QEMU_OPTIONS_GENERATE_OPTIONS
1603 #include "qemu-options-wrapper.h"
1607 static bool vga_available(void)
1609 return qdev_exists("VGA") || qdev_exists("isa-vga");
1612 static bool cirrus_vga_available(void)
1614 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1617 static bool vmware_vga_available(void)
1619 return qdev_exists("vmware-svga");
1622 static void select_vgahw (const char *p
)
1626 vga_interface_type
= VGA_NONE
;
1627 if (strstart(p
, "std", &opts
)) {
1628 if (vga_available()) {
1629 vga_interface_type
= VGA_STD
;
1631 fprintf(stderr
, "Error: standard VGA not available\n");
1634 } else if (strstart(p
, "cirrus", &opts
)) {
1635 if (cirrus_vga_available()) {
1636 vga_interface_type
= VGA_CIRRUS
;
1638 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1641 } else if (strstart(p
, "vmware", &opts
)) {
1642 if (vmware_vga_available()) {
1643 vga_interface_type
= VGA_VMWARE
;
1645 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1648 } else if (strstart(p
, "xenfb", &opts
)) {
1649 vga_interface_type
= VGA_XENFB
;
1650 } else if (strstart(p
, "qxl", &opts
)) {
1651 vga_interface_type
= VGA_QXL
;
1652 } else if (!strstart(p
, "none", &opts
)) {
1654 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1658 const char *nextopt
;
1660 if (strstart(opts
, ",retrace=", &nextopt
)) {
1662 if (strstart(opts
, "dumb", &nextopt
))
1663 vga_retrace_method
= VGA_RETRACE_DUMB
;
1664 else if (strstart(opts
, "precise", &nextopt
))
1665 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1666 else goto invalid_vga
;
1667 } else goto invalid_vga
;
1672 static DisplayType
select_display(const char *p
)
1675 DisplayType display
= DT_DEFAULT
;
1677 if (strstart(p
, "sdl", &opts
)) {
1681 const char *nextopt
;
1683 if (strstart(opts
, ",frame=", &nextopt
)) {
1685 if (strstart(opts
, "on", &nextopt
)) {
1687 } else if (strstart(opts
, "off", &nextopt
)) {
1690 goto invalid_sdl_args
;
1692 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1694 if (strstart(opts
, "on", &nextopt
)) {
1696 } else if (strstart(opts
, "off", &nextopt
)) {
1699 goto invalid_sdl_args
;
1701 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1703 if (strstart(opts
, "on", &nextopt
)) {
1705 } else if (strstart(opts
, "off", &nextopt
)) {
1708 goto invalid_sdl_args
;
1710 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1712 if (strstart(opts
, "on", &nextopt
)) {
1714 } else if (strstart(opts
, "off", &nextopt
)) {
1717 goto invalid_sdl_args
;
1721 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1727 fprintf(stderr
, "SDL support is disabled\n");
1730 } else if (strstart(p
, "vnc", &opts
)) {
1735 const char *nextopt
;
1737 if (strstart(opts
, "=", &nextopt
)) {
1738 vnc_display
= nextopt
;
1742 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1746 fprintf(stderr
, "VNC support is disabled\n");
1749 } else if (strstart(p
, "curses", &opts
)) {
1750 #ifdef CONFIG_CURSES
1751 display
= DT_CURSES
;
1753 fprintf(stderr
, "Curses support is disabled\n");
1756 } else if (strstart(p
, "none", &opts
)) {
1759 fprintf(stderr
, "Unknown display type: %s\n", p
);
1766 static int balloon_parse(const char *arg
)
1770 if (strcmp(arg
, "none") == 0) {
1774 if (!strncmp(arg
, "virtio", 6)) {
1775 if (arg
[6] == ',') {
1776 /* have params -> parse them */
1777 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1781 /* create empty opts */
1782 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0, NULL
);
1784 qemu_opt_set(opts
, "driver", "virtio-balloon");
1791 char *qemu_find_file(int type
, const char *name
)
1797 /* Try the name as a straight path first */
1798 if (access(name
, R_OK
) == 0) {
1799 return g_strdup(name
);
1802 case QEMU_FILE_TYPE_BIOS
:
1805 case QEMU_FILE_TYPE_KEYMAP
:
1806 subdir
= "keymaps/";
1811 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1812 buf
= g_malloc0(len
);
1813 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1814 if (access(buf
, R_OK
)) {
1821 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1823 return qdev_device_help(opts
);
1826 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1830 dev
= qdev_device_add(opts
);
1836 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1838 CharDriverState
*chr
;
1840 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1846 #ifdef CONFIG_VIRTFS
1847 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1850 ret
= qemu_fsdev_add(opts
);
1856 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1858 CharDriverState
*chr
;
1859 const char *chardev
;
1863 mode
= qemu_opt_get(opts
, "mode");
1867 if (strcmp(mode
, "readline") == 0) {
1868 flags
= MONITOR_USE_READLINE
;
1869 } else if (strcmp(mode
, "control") == 0) {
1870 flags
= MONITOR_USE_CONTROL
;
1872 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1876 if (qemu_opt_get_bool(opts
, "pretty", 0))
1877 flags
|= MONITOR_USE_PRETTY
;
1879 if (qemu_opt_get_bool(opts
, "default", 0))
1880 flags
|= MONITOR_IS_DEFAULT
;
1882 chardev
= qemu_opt_get(opts
, "chardev");
1883 chr
= qemu_chr_find(chardev
);
1885 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1889 monitor_init(chr
, flags
);
1893 static void monitor_parse(const char *optarg
, const char *mode
)
1895 static int monitor_device_index
= 0;
1901 if (strstart(optarg
, "chardev:", &p
)) {
1902 snprintf(label
, sizeof(label
), "%s", p
);
1904 snprintf(label
, sizeof(label
), "compat_monitor%d",
1905 monitor_device_index
);
1906 if (monitor_device_index
== 0) {
1909 opts
= qemu_chr_parse_compat(label
, optarg
);
1911 fprintf(stderr
, "parse error: %s\n", optarg
);
1916 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
1918 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1921 qemu_opt_set(opts
, "mode", mode
);
1922 qemu_opt_set(opts
, "chardev", label
);
1924 qemu_opt_set(opts
, "default", "on");
1925 monitor_device_index
++;
1928 struct device_config
{
1930 DEV_USB
, /* -usbdevice */
1932 DEV_SERIAL
, /* -serial */
1933 DEV_PARALLEL
, /* -parallel */
1934 DEV_VIRTCON
, /* -virtioconsole */
1935 DEV_DEBUGCON
, /* -debugcon */
1936 DEV_GDB
, /* -gdb, -s */
1938 const char *cmdline
;
1940 QTAILQ_ENTRY(device_config
) next
;
1942 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1944 static void add_device_config(int type
, const char *cmdline
)
1946 struct device_config
*conf
;
1948 conf
= g_malloc0(sizeof(*conf
));
1950 conf
->cmdline
= cmdline
;
1951 loc_save(&conf
->loc
);
1952 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1955 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1957 struct device_config
*conf
;
1960 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1961 if (conf
->type
!= type
)
1963 loc_push_restore(&conf
->loc
);
1964 rc
= func(conf
->cmdline
);
1965 loc_pop(&conf
->loc
);
1972 static int serial_parse(const char *devname
)
1974 static int index
= 0;
1977 if (strcmp(devname
, "none") == 0)
1979 if (index
== MAX_SERIAL_PORTS
) {
1980 fprintf(stderr
, "qemu: too many serial ports\n");
1983 snprintf(label
, sizeof(label
), "serial%d", index
);
1984 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1985 if (!serial_hds
[index
]) {
1986 fprintf(stderr
, "qemu: could not connect serial device"
1987 " to character backend '%s'\n", devname
);
1994 static int parallel_parse(const char *devname
)
1996 static int index
= 0;
1999 if (strcmp(devname
, "none") == 0)
2001 if (index
== MAX_PARALLEL_PORTS
) {
2002 fprintf(stderr
, "qemu: too many parallel ports\n");
2005 snprintf(label
, sizeof(label
), "parallel%d", index
);
2006 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2007 if (!parallel_hds
[index
]) {
2008 fprintf(stderr
, "qemu: could not connect parallel device"
2009 " to character backend '%s'\n", devname
);
2016 static int virtcon_parse(const char *devname
)
2018 QemuOptsList
*device
= qemu_find_opts("device");
2019 static int index
= 0;
2021 QemuOpts
*bus_opts
, *dev_opts
;
2023 if (strcmp(devname
, "none") == 0)
2025 if (index
== MAX_VIRTIO_CONSOLES
) {
2026 fprintf(stderr
, "qemu: too many virtio consoles\n");
2030 bus_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2031 if (arch_type
== QEMU_ARCH_S390X
) {
2032 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2034 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2037 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2038 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2040 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2041 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2042 if (!virtcon_hds
[index
]) {
2043 fprintf(stderr
, "qemu: could not connect virtio console"
2044 " to character backend '%s'\n", devname
);
2047 qemu_opt_set(dev_opts
, "chardev", label
);
2053 static int debugcon_parse(const char *devname
)
2057 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2060 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2062 fprintf(stderr
, "qemu: already have a debugcon device\n");
2065 qemu_opt_set(opts
, "driver", "isa-debugcon");
2066 qemu_opt_set(opts
, "chardev", "debugcon");
2070 static QEMUMachine
*machine_parse(const char *name
)
2072 QEMUMachine
*m
, *machine
= NULL
;
2075 machine
= find_machine(name
);
2080 printf("Supported machines are:\n");
2081 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2083 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2085 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2086 m
->is_default
? " (default)" : "");
2088 exit(!name
|| !is_help_option(name
));
2091 static int tcg_init(void)
2093 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2098 const char *opt_name
;
2100 int (*available
)(void);
2104 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2105 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2106 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2107 { "qtest", "QTest", qtest_available
, qtest_init
, &qtest_allowed
},
2110 static int configure_accelerator(void)
2112 const char *p
= NULL
;
2115 bool accel_initialised
= 0;
2116 bool init_failed
= 0;
2118 QemuOptsList
*list
= qemu_find_opts("machine");
2119 if (!QTAILQ_EMPTY(&list
->head
)) {
2120 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2124 /* Use the default "accelerator", tcg */
2128 while (!accel_initialised
&& *p
!= '\0') {
2132 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2133 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2134 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2135 *(accel_list
[i
].allowed
) = 1;
2136 ret
= accel_list
[i
].init();
2139 if (!accel_list
[i
].available()) {
2140 printf("%s not supported for this target\n",
2141 accel_list
[i
].name
);
2143 fprintf(stderr
, "failed to initialize %s: %s\n",
2147 *(accel_list
[i
].allowed
) = 0;
2149 accel_initialised
= 1;
2154 if (i
== ARRAY_SIZE(accel_list
)) {
2155 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2159 if (!accel_initialised
) {
2160 fprintf(stderr
, "No accelerator found!\n");
2165 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2168 return !accel_initialised
;
2171 void qemu_add_exit_notifier(Notifier
*notify
)
2173 notifier_list_add(&exit_notifiers
, notify
);
2176 void qemu_remove_exit_notifier(Notifier
*notify
)
2178 notifier_remove(notify
);
2181 static void qemu_run_exit_notifiers(void)
2183 notifier_list_notify(&exit_notifiers
, NULL
);
2186 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2188 notifier_list_add(&machine_init_done_notifiers
, notify
);
2191 static void qemu_run_machine_init_done_notifiers(void)
2193 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2196 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2197 const char **poptarg
, int *poptind
)
2199 const QEMUOption
*popt
;
2200 int optind
= *poptind
;
2201 char *r
= argv
[optind
];
2204 loc_set_cmdline(argv
, optind
, 1);
2206 /* Treat --foo the same as -foo. */
2209 popt
= qemu_options
;
2212 error_report("invalid option");
2215 if (!strcmp(popt
->name
, r
+ 1))
2219 if (popt
->flags
& HAS_ARG
) {
2220 if (optind
>= argc
) {
2221 error_report("requires an argument");
2224 optarg
= argv
[optind
++];
2225 loc_set_cmdline(argv
, optind
- 2, 2);
2236 static gpointer
malloc_and_trace(gsize n_bytes
)
2238 void *ptr
= malloc(n_bytes
);
2239 trace_g_malloc(n_bytes
, ptr
);
2243 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2245 void *ptr
= realloc(mem
, n_bytes
);
2246 trace_g_realloc(mem
, n_bytes
, ptr
);
2250 static void free_and_trace(gpointer mem
)
2256 int qemu_init_main_loop(void)
2258 return main_loop_init();
2261 int main(int argc
, char **argv
, char **envp
)
2264 int snapshot
, linux_boot
;
2265 const char *icount_option
= NULL
;
2266 const char *initrd_filename
;
2267 const char *kernel_filename
, *kernel_cmdline
;
2268 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2270 DisplayChangeListener
*dcl
;
2271 int cyls
, heads
, secs
, translation
;
2272 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2273 QemuOptsList
*olist
;
2276 const char *loadvm
= NULL
;
2277 QEMUMachine
*machine
;
2278 const char *cpu_model
;
2279 const char *vga_model
= "none";
2280 const char *pid_file
= NULL
;
2281 const char *incoming
= NULL
;
2283 int show_vnc_port
= 0;
2285 bool defconfig
= true;
2286 bool userconfig
= true;
2287 const char *log_mask
= NULL
;
2288 const char *log_file
= NULL
;
2289 GMemVTable mem_trace
= {
2290 .malloc
= malloc_and_trace
,
2291 .realloc
= realloc_and_trace
,
2292 .free
= free_and_trace
,
2294 const char *trace_events
= NULL
;
2295 const char *trace_file
= NULL
;
2297 atexit(qemu_run_exit_notifiers
);
2298 error_set_progname(argv
[0]);
2300 g_mem_set_vtable(&mem_trace
);
2301 if (!g_thread_supported()) {
2302 #if !GLIB_CHECK_VERSION(2, 31, 0)
2303 g_thread_init(NULL
);
2305 fprintf(stderr
, "glib threading failed to initialize.\n");
2310 module_call_init(MODULE_INIT_QOM
);
2315 rtc_clock
= host_clock
;
2317 qemu_cache_utils_init(envp
);
2319 QLIST_INIT (&vm_change_state_head
);
2320 os_setup_early_signal_handling();
2322 module_call_init(MODULE_INIT_MACHINE
);
2323 machine
= find_default_machine();
2327 cyls
= heads
= secs
= 0;
2328 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2330 for (i
= 0; i
< MAX_NODES
; i
++) {
2332 node_cpumask
[i
] = bitmap_new(MAX_CPUMASK_BITS
);
2340 /* first pass of option parsing */
2342 while (optind
< argc
) {
2343 if (argv
[optind
][0] != '-') {
2348 const QEMUOption
*popt
;
2350 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2351 switch (popt
->index
) {
2352 case QEMU_OPTION_nodefconfig
:
2355 case QEMU_OPTION_nouserconfig
:
2364 ret
= qemu_read_default_config_files(userconfig
);
2370 /* second pass of option parsing */
2375 if (argv
[optind
][0] != '-') {
2376 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2378 const QEMUOption
*popt
;
2380 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2381 if (!(popt
->arch_mask
& arch_type
)) {
2382 printf("Option %s not supported for this target\n", popt
->name
);
2385 switch(popt
->index
) {
2387 machine
= machine_parse(optarg
);
2389 case QEMU_OPTION_cpu
:
2390 /* hw initialization will check this */
2393 case QEMU_OPTION_hda
:
2397 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2399 snprintf(buf
, sizeof(buf
),
2400 "%s,cyls=%d,heads=%d,secs=%d%s",
2401 HD_OPTS
, cyls
, heads
, secs
,
2402 translation
== BIOS_ATA_TRANSLATION_LBA
?
2404 translation
== BIOS_ATA_TRANSLATION_NONE
?
2405 ",trans=none" : "");
2406 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2409 case QEMU_OPTION_hdb
:
2410 case QEMU_OPTION_hdc
:
2411 case QEMU_OPTION_hdd
:
2412 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2415 case QEMU_OPTION_drive
:
2416 if (drive_def(optarg
) == NULL
) {
2420 case QEMU_OPTION_set
:
2421 if (qemu_set_option(optarg
) != 0)
2424 case QEMU_OPTION_global
:
2425 if (qemu_global_option(optarg
) != 0)
2428 case QEMU_OPTION_mtdblock
:
2429 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2431 case QEMU_OPTION_sd
:
2432 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2434 case QEMU_OPTION_pflash
:
2435 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2437 case QEMU_OPTION_snapshot
:
2440 case QEMU_OPTION_hdachs
:
2444 cyls
= strtol(p
, (char **)&p
, 0);
2445 if (cyls
< 1 || cyls
> 16383)
2450 heads
= strtol(p
, (char **)&p
, 0);
2451 if (heads
< 1 || heads
> 16)
2456 secs
= strtol(p
, (char **)&p
, 0);
2457 if (secs
< 1 || secs
> 63)
2461 if (!strcmp(p
, "none"))
2462 translation
= BIOS_ATA_TRANSLATION_NONE
;
2463 else if (!strcmp(p
, "lba"))
2464 translation
= BIOS_ATA_TRANSLATION_LBA
;
2465 else if (!strcmp(p
, "auto"))
2466 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2469 } else if (*p
!= '\0') {
2471 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2474 if (hda_opts
!= NULL
) {
2476 snprintf(num
, sizeof(num
), "%d", cyls
);
2477 qemu_opt_set(hda_opts
, "cyls", num
);
2478 snprintf(num
, sizeof(num
), "%d", heads
);
2479 qemu_opt_set(hda_opts
, "heads", num
);
2480 snprintf(num
, sizeof(num
), "%d", secs
);
2481 qemu_opt_set(hda_opts
, "secs", num
);
2482 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2483 qemu_opt_set(hda_opts
, "trans", "lba");
2484 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2485 qemu_opt_set(hda_opts
, "trans", "none");
2489 case QEMU_OPTION_numa
:
2490 if (nb_numa_nodes
>= MAX_NODES
) {
2491 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2496 case QEMU_OPTION_display
:
2497 display_type
= select_display(optarg
);
2499 case QEMU_OPTION_nographic
:
2500 display_type
= DT_NOGRAPHIC
;
2502 case QEMU_OPTION_curses
:
2503 #ifdef CONFIG_CURSES
2504 display_type
= DT_CURSES
;
2506 fprintf(stderr
, "Curses support is disabled\n");
2510 case QEMU_OPTION_portrait
:
2511 graphic_rotate
= 90;
2513 case QEMU_OPTION_rotate
:
2514 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2515 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2516 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2518 "qemu: only 90, 180, 270 deg rotation is available\n");
2522 case QEMU_OPTION_kernel
:
2523 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2525 case QEMU_OPTION_initrd
:
2526 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2528 case QEMU_OPTION_append
:
2529 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2531 case QEMU_OPTION_dtb
:
2532 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
2534 case QEMU_OPTION_cdrom
:
2535 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2537 case QEMU_OPTION_boot
:
2539 static const char * const params
[] = {
2540 "order", "once", "menu",
2541 "splash", "splash-time", NULL
2543 char buf
[sizeof(boot_devices
)];
2544 char *standard_boot_devices
;
2547 if (!strchr(optarg
, '=')) {
2549 pstrcpy(buf
, sizeof(buf
), optarg
);
2550 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2552 "qemu: unknown boot parameter '%s' in '%s'\n",
2558 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2559 validate_bootdevices(buf
);
2560 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2563 if (get_param_value(buf
, sizeof(buf
),
2565 validate_bootdevices(buf
);
2566 standard_boot_devices
= g_strdup(boot_devices
);
2567 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2568 qemu_register_reset(restore_boot_devices
,
2569 standard_boot_devices
);
2571 if (get_param_value(buf
, sizeof(buf
),
2573 if (!strcmp(buf
, "on")) {
2575 } else if (!strcmp(buf
, "off")) {
2579 "qemu: invalid option value '%s'\n",
2584 qemu_opts_parse(qemu_find_opts("boot-opts"),
2589 case QEMU_OPTION_fda
:
2590 case QEMU_OPTION_fdb
:
2591 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2594 case QEMU_OPTION_no_fd_bootchk
:
2597 case QEMU_OPTION_netdev
:
2598 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2602 case QEMU_OPTION_net
:
2603 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2607 #ifdef CONFIG_LIBISCSI
2608 case QEMU_OPTION_iscsi
:
2609 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2616 case QEMU_OPTION_tftp
:
2617 legacy_tftp_prefix
= optarg
;
2619 case QEMU_OPTION_bootp
:
2620 legacy_bootp_filename
= optarg
;
2622 case QEMU_OPTION_redir
:
2623 if (net_slirp_redir(optarg
) < 0)
2627 case QEMU_OPTION_bt
:
2628 add_device_config(DEV_BT
, optarg
);
2630 case QEMU_OPTION_audio_help
:
2631 if (!(audio_available())) {
2632 printf("Option %s not supported for this target\n", popt
->name
);
2638 case QEMU_OPTION_soundhw
:
2639 if (!(audio_available())) {
2640 printf("Option %s not supported for this target\n", popt
->name
);
2643 select_soundhw (optarg
);
2648 case QEMU_OPTION_version
:
2652 case QEMU_OPTION_m
: {
2656 value
= strtosz(optarg
, &end
);
2657 if (value
< 0 || *end
) {
2658 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2662 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2663 fprintf(stderr
, "qemu: ram size too large\n");
2669 case QEMU_OPTION_mempath
:
2673 case QEMU_OPTION_mem_prealloc
:
2684 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2686 case QEMU_OPTION_gdb
:
2687 add_device_config(DEV_GDB
, optarg
);
2692 case QEMU_OPTION_bios
:
2695 case QEMU_OPTION_singlestep
:
2702 keyboard_layout
= optarg
;
2704 case QEMU_OPTION_localtime
:
2707 case QEMU_OPTION_vga
:
2716 w
= strtol(p
, (char **)&p
, 10);
2719 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2725 h
= strtol(p
, (char **)&p
, 10);
2730 depth
= strtol(p
, (char **)&p
, 10);
2731 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2732 depth
!= 24 && depth
!= 32)
2734 } else if (*p
== '\0') {
2735 depth
= graphic_depth
;
2742 graphic_depth
= depth
;
2745 case QEMU_OPTION_echr
:
2748 term_escape_char
= strtol(optarg
, &r
, 0);
2750 printf("Bad argument to echr\n");
2753 case QEMU_OPTION_monitor
:
2754 monitor_parse(optarg
, "readline");
2755 default_monitor
= 0;
2757 case QEMU_OPTION_qmp
:
2758 monitor_parse(optarg
, "control");
2759 default_monitor
= 0;
2761 case QEMU_OPTION_mon
:
2762 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2766 default_monitor
= 0;
2768 case QEMU_OPTION_chardev
:
2769 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2774 case QEMU_OPTION_fsdev
:
2775 olist
= qemu_find_opts("fsdev");
2777 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2780 opts
= qemu_opts_parse(olist
, optarg
, 1);
2782 fprintf(stderr
, "parse error: %s\n", optarg
);
2786 case QEMU_OPTION_virtfs
: {
2789 const char *writeout
, *sock_fd
, *socket
;
2791 olist
= qemu_find_opts("virtfs");
2793 fprintf(stderr
, "virtfs 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 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2803 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2804 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2807 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2808 qemu_opt_get(opts
, "mount_tag"),
2811 fprintf(stderr
, "duplicate fsdev id: %s\n",
2812 qemu_opt_get(opts
, "mount_tag"));
2816 writeout
= qemu_opt_get(opts
, "writeout");
2818 #ifdef CONFIG_SYNC_FILE_RANGE
2819 qemu_opt_set(fsdev
, "writeout", writeout
);
2821 fprintf(stderr
, "writeout=immediate not supported on "
2826 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2827 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2828 qemu_opt_set(fsdev
, "security_model",
2829 qemu_opt_get(opts
, "security_model"));
2830 socket
= qemu_opt_get(opts
, "socket");
2832 qemu_opt_set(fsdev
, "socket", socket
);
2834 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2836 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2839 qemu_opt_set_bool(fsdev
, "readonly",
2840 qemu_opt_get_bool(opts
, "readonly", 0));
2841 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2843 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2844 qemu_opt_set(device
, "fsdev",
2845 qemu_opt_get(opts
, "mount_tag"));
2846 qemu_opt_set(device
, "mount_tag",
2847 qemu_opt_get(opts
, "mount_tag"));
2850 case QEMU_OPTION_virtfs_synth
: {
2854 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
2857 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2860 qemu_opt_set(fsdev
, "fsdriver", "synth");
2862 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2864 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2865 qemu_opt_set(device
, "fsdev", "v_synth");
2866 qemu_opt_set(device
, "mount_tag", "v_synth");
2869 case QEMU_OPTION_serial
:
2870 add_device_config(DEV_SERIAL
, optarg
);
2872 if (strncmp(optarg
, "mon:", 4) == 0) {
2873 default_monitor
= 0;
2876 case QEMU_OPTION_watchdog
:
2879 "qemu: only one watchdog option may be given\n");
2884 case QEMU_OPTION_watchdog_action
:
2885 if (select_watchdog_action(optarg
) == -1) {
2886 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2890 case QEMU_OPTION_virtiocon
:
2891 add_device_config(DEV_VIRTCON
, optarg
);
2892 default_virtcon
= 0;
2893 if (strncmp(optarg
, "mon:", 4) == 0) {
2894 default_monitor
= 0;
2897 case QEMU_OPTION_parallel
:
2898 add_device_config(DEV_PARALLEL
, optarg
);
2899 default_parallel
= 0;
2900 if (strncmp(optarg
, "mon:", 4) == 0) {
2901 default_monitor
= 0;
2904 case QEMU_OPTION_debugcon
:
2905 add_device_config(DEV_DEBUGCON
, optarg
);
2907 case QEMU_OPTION_loadvm
:
2910 case QEMU_OPTION_full_screen
:
2914 case QEMU_OPTION_no_frame
:
2917 case QEMU_OPTION_alt_grab
:
2920 case QEMU_OPTION_ctrl_grab
:
2923 case QEMU_OPTION_no_quit
:
2926 case QEMU_OPTION_sdl
:
2927 display_type
= DT_SDL
;
2930 case QEMU_OPTION_no_frame
:
2931 case QEMU_OPTION_alt_grab
:
2932 case QEMU_OPTION_ctrl_grab
:
2933 case QEMU_OPTION_no_quit
:
2934 case QEMU_OPTION_sdl
:
2935 fprintf(stderr
, "SDL support is disabled\n");
2938 case QEMU_OPTION_pidfile
:
2941 case QEMU_OPTION_win2k_hack
:
2942 win2k_install_hack
= 1;
2944 case QEMU_OPTION_rtc_td_hack
: {
2945 static GlobalProperty slew_lost_ticks
[] = {
2947 .driver
= "mc146818rtc",
2948 .property
= "lost_tick_policy",
2951 { /* end of list */ }
2954 qdev_prop_register_global_list(slew_lost_ticks
);
2957 case QEMU_OPTION_acpitable
:
2958 do_acpitable_option(optarg
);
2960 case QEMU_OPTION_smbios
:
2961 do_smbios_option(optarg
);
2963 case QEMU_OPTION_enable_kvm
:
2964 olist
= qemu_find_opts("machine");
2965 qemu_opts_parse(olist
, "accel=kvm", 0);
2967 case QEMU_OPTION_machine
:
2968 olist
= qemu_find_opts("machine");
2969 opts
= qemu_opts_parse(olist
, optarg
, 1);
2971 fprintf(stderr
, "parse error: %s\n", optarg
);
2974 optarg
= qemu_opt_get(opts
, "type");
2976 machine
= machine_parse(optarg
);
2979 case QEMU_OPTION_usb
:
2982 case QEMU_OPTION_usbdevice
:
2984 add_device_config(DEV_USB
, optarg
);
2986 case QEMU_OPTION_device
:
2987 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2991 case QEMU_OPTION_smp
:
2994 fprintf(stderr
, "Invalid number of CPUs\n");
2997 if (max_cpus
< smp_cpus
) {
2998 fprintf(stderr
, "maxcpus must be equal to or greater than "
3002 if (max_cpus
> 255) {
3003 fprintf(stderr
, "Unsupported number of maxcpus\n");
3007 case QEMU_OPTION_vnc
:
3010 vnc_display
= optarg
;
3012 fprintf(stderr
, "VNC support is disabled\n");
3016 case QEMU_OPTION_no_acpi
:
3019 case QEMU_OPTION_no_hpet
:
3022 case QEMU_OPTION_balloon
:
3023 if (balloon_parse(optarg
) < 0) {
3024 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3028 case QEMU_OPTION_no_reboot
:
3031 case QEMU_OPTION_no_shutdown
:
3034 case QEMU_OPTION_show_cursor
:
3037 case QEMU_OPTION_uuid
:
3038 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3039 fprintf(stderr
, "Fail to parse UUID string."
3040 " Wrong format.\n");
3044 case QEMU_OPTION_option_rom
:
3045 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3046 fprintf(stderr
, "Too many option ROMs\n");
3049 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3050 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3051 option_rom
[nb_option_roms
].bootindex
=
3052 qemu_opt_get_number(opts
, "bootindex", -1);
3053 if (!option_rom
[nb_option_roms
].name
) {
3054 fprintf(stderr
, "Option ROM file is not specified\n");
3059 case QEMU_OPTION_semihosting
:
3060 semihosting_enabled
= 1;
3062 case QEMU_OPTION_name
:
3063 qemu_name
= g_strdup(optarg
);
3065 char *p
= strchr(qemu_name
, ',');
3068 if (strncmp(p
, "process=", 8)) {
3069 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3073 os_set_proc_name(p
);
3077 case QEMU_OPTION_prom_env
:
3078 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3079 fprintf(stderr
, "Too many prom variables\n");
3082 prom_envs
[nb_prom_envs
] = optarg
;
3085 case QEMU_OPTION_old_param
:
3088 case QEMU_OPTION_clock
:
3089 configure_alarms(optarg
);
3091 case QEMU_OPTION_startdate
:
3092 configure_rtc_date_offset(optarg
, 1);
3094 case QEMU_OPTION_rtc
:
3095 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3099 configure_rtc(opts
);
3101 case QEMU_OPTION_tb_size
:
3102 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3103 if (tcg_tb_size
< 0) {
3107 case QEMU_OPTION_icount
:
3108 icount_option
= optarg
;
3110 case QEMU_OPTION_incoming
:
3112 runstate_set(RUN_STATE_INMIGRATE
);
3114 case QEMU_OPTION_nodefaults
:
3116 default_parallel
= 0;
3117 default_virtcon
= 0;
3118 default_monitor
= 0;
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
);
3194 case QEMU_OPTION_qtest
:
3195 qtest_chrdev
= optarg
;
3197 case QEMU_OPTION_qtest_log
:
3201 os_parse_cmd_args(popt
->index
, optarg
);
3207 if (machine
->hw_version
) {
3208 qemu_set_version(machine
->hw_version
);
3211 /* Init CPU def lists, based on config
3212 * - Must be called after all the qemu_read_config_file() calls
3213 * - Must be called before list_cpus()
3214 * - Must be called before machine->init()
3218 if (cpu_model
&& is_help_option(cpu_model
)) {
3219 list_cpus(stdout
, &fprintf
, cpu_model
);
3223 /* Open the logfile at this point, if necessary. We can't open the logfile
3224 * when encountering either of the logging options (-d or -D) because the
3225 * other one may be encountered later on the command line, changing the
3226 * location or level of logging.
3230 set_cpu_log_filename(log_file
);
3232 set_cpu_log(log_mask
);
3235 if (!trace_backend_init(trace_events
, trace_file
)) {
3239 /* If no data_dir is specified then try to find it relative to the
3242 data_dir
= os_find_datadir(argv
[0]);
3244 /* If all else fails use the install path specified when building. */
3246 data_dir
= CONFIG_QEMU_DATADIR
;
3249 if (machine
== NULL
) {
3250 fprintf(stderr
, "No machine found.\n");
3255 * Default to max_cpus = smp_cpus, in case the user doesn't
3256 * specify a max_cpus value.
3259 max_cpus
= smp_cpus
;
3261 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3262 if (smp_cpus
> machine
->max_cpus
) {
3263 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3264 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3270 * Get the default machine options from the machine if it is not already
3271 * specified either by the configuration file or by the command line.
3273 if (machine
->default_machine_opts
) {
3274 qemu_opts_set_defaults(qemu_find_opts("machine"),
3275 machine
->default_machine_opts
, 0);
3278 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3279 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3281 if (machine
->no_serial
) {
3284 if (machine
->no_parallel
) {
3285 default_parallel
= 0;
3287 if (!machine
->use_virtcon
) {
3288 default_virtcon
= 0;
3290 if (machine
->no_floppy
) {
3293 if (machine
->no_cdrom
) {
3296 if (machine
->no_sdcard
) {
3300 if (display_type
== DT_NOGRAPHIC
) {
3301 if (default_parallel
)
3302 add_device_config(DEV_PARALLEL
, "null");
3303 if (default_serial
&& default_monitor
) {
3304 add_device_config(DEV_SERIAL
, "mon:stdio");
3305 } else if (default_virtcon
&& default_monitor
) {
3306 add_device_config(DEV_VIRTCON
, "mon:stdio");
3309 add_device_config(DEV_SERIAL
, "stdio");
3310 if (default_virtcon
)
3311 add_device_config(DEV_VIRTCON
, "stdio");
3312 if (default_monitor
)
3313 monitor_parse("stdio", "readline");
3317 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3318 if (default_parallel
)
3319 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3320 if (default_monitor
)
3321 monitor_parse("vc:80Cx24C", "readline");
3322 if (default_virtcon
)
3323 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3328 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3330 #ifdef CONFIG_VIRTFS
3331 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3338 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3343 /* init the memory */
3344 if (ram_size
== 0) {
3345 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3348 configure_accelerator();
3350 qemu_init_cpu_loop();
3351 if (qemu_init_main_loop()) {
3352 fprintf(stderr
, "qemu_init_main_loop failed\n");
3356 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3358 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3359 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3360 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3362 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3365 if (!kernel_cmdline
) {
3366 kernel_cmdline
= "";
3369 linux_boot
= (kernel_filename
!= NULL
);
3371 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3372 fprintf(stderr
, "-append only allowed with -kernel option\n");
3376 if (!linux_boot
&& initrd_filename
!= NULL
) {
3377 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3381 if (!linux_boot
&& machine_opts
&& qemu_opt_get(machine_opts
, "dtb")) {
3382 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
3386 os_set_line_buffering();
3388 if (init_timer_alarm() < 0) {
3389 fprintf(stderr
, "could not initialize alarm timer\n");
3394 /* spice needs the timers to be initialized by this point */
3398 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3399 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3402 configure_icount(icount_option
);
3404 if (net_init_clients() < 0) {
3408 /* init the bluetooth world */
3409 if (foreach_device_config(DEV_BT
, bt_parse
))
3412 if (!xen_enabled()) {
3413 /* On 32-bit hosts, QEMU is limited by virtual address space */
3414 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3415 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3420 cpu_exec_init_all();
3422 bdrv_init_with_whitelist();
3426 /* open the virtual block devices */
3428 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3429 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3432 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3433 IF_DEFAULT
, 2, CDROM_OPTS
);
3434 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3435 IF_FLOPPY
, 0, FD_OPTS
);
3436 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3439 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
3441 if (nb_numa_nodes
> 0) {
3444 if (nb_numa_nodes
> MAX_NODES
) {
3445 nb_numa_nodes
= MAX_NODES
;
3448 /* If no memory size if given for any node, assume the default case
3449 * and distribute the available memory equally across all nodes
3451 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3452 if (node_mem
[i
] != 0)
3455 if (i
== nb_numa_nodes
) {
3456 uint64_t usedmem
= 0;
3458 /* On Linux, the each node's border has to be 8MB aligned,
3459 * the final node gets the rest.
3461 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3462 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3463 usedmem
+= node_mem
[i
];
3465 node_mem
[i
] = ram_size
- usedmem
;
3468 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3469 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
3473 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3474 * must cope with this anyway, because there are BIOSes out there in
3475 * real machines which also use this scheme.
3477 if (i
== nb_numa_nodes
) {
3478 for (i
= 0; i
< max_cpus
; i
++) {
3479 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
3484 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3488 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3490 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3492 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3494 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3497 /* If no default VGA is requested, the default is "none". */
3498 if (default_vga
&& cirrus_vga_available()) {
3499 vga_model
= "cirrus";
3501 select_vgahw(vga_model
);
3503 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3507 i
= select_watchdog(watchdog
);
3509 exit (i
== 1 ? 1 : 0);
3512 if (machine
->compat_props
) {
3513 qdev_prop_register_global_list(machine
->compat_props
);
3517 qdev_machine_init();
3519 machine
->init(ram_size
, boot_devices
,
3520 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3522 cpu_synchronize_all_post_init();
3526 current_machine
= machine
;
3528 /* init USB devices */
3530 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3534 /* init generic devices */
3535 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3538 net_check_clients();
3540 /* just use the first displaystate for the moment */
3541 ds
= get_displaystate();
3545 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3546 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3547 display_type
= DT_SDL
;
3548 #elif defined(CONFIG_VNC)
3549 vnc_display
= "localhost:0,to=99";
3552 display_type
= DT_NONE
;
3557 /* init local displays */
3558 switch (display_type
) {
3561 #if defined(CONFIG_CURSES)
3563 curses_display_init(ds
, full_screen
);
3566 #if defined(CONFIG_SDL)
3568 sdl_display_init(ds
, full_screen
, no_frame
);
3570 #elif defined(CONFIG_COCOA)
3572 cocoa_display_init(ds
, full_screen
);
3579 /* must be after terminal init, SDL library changes signal handlers */
3580 os_setup_signal_handling();
3583 /* init remote displays */
3585 vnc_display_init(ds
);
3586 if (vnc_display_open(ds
, vnc_display
) < 0) {
3587 fprintf(stderr
, "Failed to start VNC server on `%s'\n",
3592 if (show_vnc_port
) {
3593 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3598 if (using_spice
&& !qxl_enabled
) {
3599 qemu_spice_display_init(ds
);
3605 dcl
= ds
->listeners
;
3606 while (dcl
!= NULL
) {
3607 if (dcl
->dpy_refresh
!= NULL
) {
3608 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3609 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3614 text_consoles_set_display(ds
);
3616 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3620 qdev_machine_creation_done();
3622 if (rom_load_all() != 0) {
3623 fprintf(stderr
, "rom loading failed\n");
3627 /* TODO: once all bus devices are qdevified, this should be done
3628 * when bus is created by qdev.c */
3629 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3630 qemu_run_machine_init_done_notifiers();
3632 qemu_system_reset(VMRESET_SILENT
);
3634 if (load_vmstate(loadvm
) < 0) {
3641 int ret
= qemu_start_incoming_migration(incoming
, &errp
);
3643 if (error_is_set(&errp
)) {
3644 fprintf(stderr
, "Migrate: %s\n", error_get_pretty(errp
));
3647 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3651 } else if (autostart
) {