4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
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"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
163 #include "trace/control.h"
164 #include "qemu-queue.h"
166 #include "arch_init.h"
168 #include "ui/qemu-spice.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir
;
178 const char *bios_name
= NULL
;
179 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
180 DisplayType display_type
= DT_DEFAULT
;
181 int display_remote
= 0;
182 const char* keyboard_layout
= NULL
;
184 const char *mem_path
= NULL
;
186 int mem_prealloc
= 0; /* force preallocation of physical target memory */
189 NICInfo nd_table
[MAX_NICS
];
191 static int rtc_utc
= 1;
192 static int rtc_date_offset
= -1; /* -1 means no change */
193 QEMUClock
*rtc_clock
;
194 int vga_interface_type
= VGA_NONE
;
195 static int full_screen
= 0;
197 static int no_frame
= 0;
200 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
201 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
202 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
203 int win2k_install_hack
= 0;
212 const char *vnc_display
;
214 int acpi_enabled
= 1;
220 int graphic_rotate
= 0;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint8_t *boot_splash_filedata
;
233 int boot_splash_filedata_size
;
234 uint8_t qemu_extra_params_fw
[2];
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 uint8_t qemu_uuid
[16];
253 static QEMUBootSetHandler
*boot_set_handler
;
254 static void *boot_set_opaque
;
256 static NotifierList exit_notifiers
=
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
259 static NotifierList machine_init_done_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 static int tcg_allowed
= 1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
267 static int tcg_tb_size
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_floppy
= 1;
274 static int default_cdrom
= 1;
275 static int default_sdcard
= 1;
281 { .driver
= "isa-serial", .flag
= &default_serial
},
282 { .driver
= "isa-parallel", .flag
= &default_parallel
},
283 { .driver
= "isa-fdc", .flag
= &default_floppy
},
284 { .driver
= "ide-cd", .flag
= &default_cdrom
},
285 { .driver
= "ide-hd", .flag
= &default_cdrom
},
286 { .driver
= "ide-drive", .flag
= &default_cdrom
},
287 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
288 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
289 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 static void res_free(void)
295 if (boot_splash_filedata
!= NULL
) {
296 g_free(boot_splash_filedata
);
297 boot_splash_filedata
= NULL
;
301 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
303 const char *driver
= qemu_opt_get(opts
, "driver");
308 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
309 if (strcmp(default_list
[i
].driver
, driver
) != 0)
311 *(default_list
[i
].flag
) = 0;
316 /***********************************************************/
319 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
324 } RunStateTransition
;
326 static const RunStateTransition runstate_transitions_def
[] = {
328 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
330 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
331 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
333 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
334 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
336 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
337 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
339 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
340 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
342 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
343 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
345 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
346 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
347 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
349 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
350 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
352 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
354 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
355 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
356 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
357 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
358 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
359 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
360 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
361 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
362 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
364 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
366 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
367 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
369 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
370 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
372 { RUN_STATE_MAX
, RUN_STATE_MAX
},
375 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
377 bool runstate_check(RunState state
)
379 return current_run_state
== state
;
382 void runstate_init(void)
384 const RunStateTransition
*p
;
386 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
388 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
389 runstate_valid_transitions
[p
->from
][p
->to
] = true;
393 /* This function will abort() on invalid state transitions */
394 void runstate_set(RunState new_state
)
396 assert(new_state
< RUN_STATE_MAX
);
398 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
399 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
400 RunState_lookup
[current_run_state
],
401 RunState_lookup
[new_state
]);
405 current_run_state
= new_state
;
408 int runstate_is_running(void)
410 return runstate_check(RUN_STATE_RUNNING
);
413 StatusInfo
*qmp_query_status(Error
**errp
)
415 StatusInfo
*info
= g_malloc0(sizeof(*info
));
417 info
->running
= runstate_is_running();
418 info
->singlestep
= singlestep
;
419 info
->status
= current_run_state
;
424 /***********************************************************/
425 /* real time host monotonic timer */
427 /***********************************************************/
428 /* host time/date access */
429 void qemu_get_timedate(struct tm
*tm
, int offset
)
436 if (rtc_date_offset
== -1) {
440 ret
= localtime(&ti
);
442 ti
-= rtc_date_offset
;
446 memcpy(tm
, ret
, sizeof(struct tm
));
449 int qemu_timedate_diff(struct tm
*tm
)
453 if (rtc_date_offset
== -1)
455 seconds
= mktimegm(tm
);
458 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
459 seconds
= mktime(&tmp
);
462 seconds
= mktimegm(tm
) + rtc_date_offset
;
464 return seconds
- time(NULL
);
467 void rtc_change_mon_event(struct tm
*tm
)
471 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
472 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
473 qobject_decref(data
);
476 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
478 time_t rtc_start_date
;
481 if (!strcmp(startdate
, "now") && legacy
) {
482 rtc_date_offset
= -1;
484 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
492 } else if (sscanf(startdate
, "%d-%d-%d",
504 rtc_start_date
= mktimegm(&tm
);
505 if (rtc_start_date
== -1) {
507 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
508 "'2006-06-17T16:01:21' or '2006-06-17'\n");
511 rtc_date_offset
= time(NULL
) - rtc_start_date
;
515 static void configure_rtc(QemuOpts
*opts
)
519 value
= qemu_opt_get(opts
, "base");
521 if (!strcmp(value
, "utc")) {
523 } else if (!strcmp(value
, "localtime")) {
526 configure_rtc_date_offset(value
, 0);
529 value
= qemu_opt_get(opts
, "clock");
531 if (!strcmp(value
, "host")) {
532 rtc_clock
= host_clock
;
533 } else if (!strcmp(value
, "vm")) {
534 rtc_clock
= vm_clock
;
536 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
540 value
= qemu_opt_get(opts
, "driftfix");
542 if (!strcmp(value
, "slew")) {
544 } else if (!strcmp(value
, "none")) {
547 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
553 /***********************************************************/
554 /* Bluetooth support */
557 static struct HCIInfo
*hci_table
[MAX_NICS
];
559 static struct bt_vlan_s
{
560 struct bt_scatternet_s net
;
562 struct bt_vlan_s
*next
;
565 /* find or alloc a new bluetooth "VLAN" */
566 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
568 struct bt_vlan_s
**pvlan
, *vlan
;
569 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
573 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
575 pvlan
= &first_bt_vlan
;
576 while (*pvlan
!= NULL
)
577 pvlan
= &(*pvlan
)->next
;
582 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
586 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
591 static struct HCIInfo null_hci
= {
592 .cmd_send
= null_hci_send
,
593 .sco_send
= null_hci_send
,
594 .acl_send
= null_hci_send
,
595 .bdaddr_set
= null_hci_addr_set
,
598 struct HCIInfo
*qemu_next_hci(void)
600 if (cur_hci
== nb_hcis
)
603 return hci_table
[cur_hci
++];
606 static struct HCIInfo
*hci_init(const char *str
)
609 struct bt_scatternet_s
*vlan
= 0;
611 if (!strcmp(str
, "null"))
614 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
616 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
617 else if (!strncmp(str
, "hci", 3)) {
620 if (!strncmp(str
+ 3, ",vlan=", 6)) {
621 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
626 vlan
= qemu_find_bt_vlan(0);
628 return bt_new_hci(vlan
);
631 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
636 static int bt_hci_parse(const char *str
)
641 if (nb_hcis
>= MAX_NICS
) {
642 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
655 bdaddr
.b
[5] = 0x56 + nb_hcis
;
656 hci
->bdaddr_set(hci
, bdaddr
.b
);
658 hci_table
[nb_hcis
++] = hci
;
663 static void bt_vhci_add(int vlan_id
)
665 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
668 fprintf(stderr
, "qemu: warning: adding a VHCI to "
669 "an empty scatternet %i\n", vlan_id
);
671 bt_vhci_init(bt_new_hci(vlan
));
674 static struct bt_device_s
*bt_device_add(const char *opt
)
676 struct bt_scatternet_s
*vlan
;
678 char *endp
= strstr(opt
, ",vlan=");
679 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
682 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
685 vlan_id
= strtol(endp
+ 6, &endp
, 0);
687 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
692 vlan
= qemu_find_bt_vlan(vlan_id
);
695 fprintf(stderr
, "qemu: warning: adding a slave device to "
696 "an empty scatternet %i\n", vlan_id
);
698 if (!strcmp(devname
, "keyboard"))
699 return bt_keyboard_init(vlan
);
701 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
705 static int bt_parse(const char *opt
)
707 const char *endp
, *p
;
710 if (strstart(opt
, "hci", &endp
)) {
711 if (!*endp
|| *endp
== ',') {
713 if (!strstart(endp
, ",vlan=", 0))
716 return bt_hci_parse(opt
);
718 } else if (strstart(opt
, "vhci", &endp
)) {
719 if (!*endp
|| *endp
== ',') {
721 if (strstart(endp
, ",vlan=", &p
)) {
722 vlan
= strtol(p
, (char **) &endp
, 0);
724 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
728 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
737 } else if (strstart(opt
, "device:", &endp
))
738 return !bt_device_add(endp
);
740 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
744 /***********************************************************/
745 /* QEMU Block devices */
747 #define HD_OPTS "media=disk"
748 #define CDROM_OPTS "media=cdrom"
750 #define PFLASH_OPTS ""
754 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
756 int *use_scsi
= opaque
;
758 return drive_init(opts
, *use_scsi
) == NULL
;
761 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
763 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
764 qemu_opt_set(opts
, "snapshot", "on");
769 static void default_drive(int enable
, int snapshot
, int use_scsi
,
770 BlockInterfaceType type
, int index
,
775 if (type
== IF_DEFAULT
) {
776 type
= use_scsi
? IF_SCSI
: IF_IDE
;
779 if (!enable
|| drive_get_by_index(type
, index
)) {
783 opts
= drive_add(type
, index
, NULL
, optstr
);
785 drive_enable_snapshot(opts
, NULL
);
787 if (!drive_init(opts
, use_scsi
)) {
792 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
794 boot_set_handler
= func
;
795 boot_set_opaque
= opaque
;
798 int qemu_boot_set(const char *boot_devices
)
800 if (!boot_set_handler
) {
803 return boot_set_handler(boot_set_opaque
, boot_devices
);
806 static void validate_bootdevices(char *devices
)
808 /* We just do some generic consistency checks */
812 for (p
= devices
; *p
!= '\0'; p
++) {
813 /* Allowed boot devices are:
814 * a-b: floppy disk drives
815 * c-f: IDE disk drives
816 * g-m: machine implementation dependent drives
817 * n-p: network devices
818 * It's up to each machine implementation to check if the given boot
819 * devices match the actual hardware implementation and firmware
822 if (*p
< 'a' || *p
> 'p') {
823 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
826 if (bitmap
& (1 << (*p
- 'a'))) {
827 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
830 bitmap
|= 1 << (*p
- 'a');
834 static void restore_boot_devices(void *opaque
)
836 char *standard_boot_devices
= opaque
;
837 static int first
= 1;
839 /* Restore boot order and remove ourselves after the first boot */
845 qemu_boot_set(standard_boot_devices
);
847 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
848 g_free(standard_boot_devices
);
851 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
854 FWBootEntry
*node
, *i
;
860 assert(dev
!= NULL
|| suffix
!= NULL
);
862 node
= g_malloc0(sizeof(FWBootEntry
));
863 node
->bootindex
= bootindex
;
864 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
867 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
868 if (i
->bootindex
== bootindex
) {
869 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
871 } else if (i
->bootindex
< bootindex
) {
874 QTAILQ_INSERT_BEFORE(i
, node
, link
);
877 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
881 * This function returns null terminated string that consist of new line
882 * separated device paths.
884 * memory pointed by "size" is assigned total length of the array in bytes
887 char *get_boot_devices_list(uint32_t *size
)
893 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
894 char *devpath
= NULL
, *bootpath
;
898 devpath
= qdev_get_fw_dev_path(i
->dev
);
902 if (i
->suffix
&& devpath
) {
903 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
905 bootpath
= g_malloc(bootpathlen
);
906 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
908 } else if (devpath
) {
912 bootpath
= g_strdup(i
->suffix
);
916 list
[total
-1] = '\n';
918 len
= strlen(bootpath
) + 1;
919 list
= g_realloc(list
, total
+ len
);
920 memcpy(&list
[total
], bootpath
, len
);
930 static void numa_add(const char *optarg
)
934 unsigned long long value
, endvalue
;
937 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
938 if (!strcmp(option
, "node")) {
939 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
940 nodenr
= nb_numa_nodes
;
942 nodenr
= strtoull(option
, NULL
, 10);
945 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
946 node_mem
[nodenr
] = 0;
949 sval
= strtosz(option
, &endptr
);
950 if (sval
< 0 || *endptr
) {
951 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
954 node_mem
[nodenr
] = sval
;
956 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
957 node_cpumask
[nodenr
] = 0;
959 value
= strtoull(option
, &endptr
, 10);
962 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
964 if (*endptr
== '-') {
965 endvalue
= strtoull(endptr
+1, &endptr
, 10);
966 if (endvalue
>= 63) {
969 "only 63 CPUs in NUMA mode supported.\n");
971 value
= (2ULL << endvalue
) - (1ULL << value
);
973 value
= 1ULL << value
;
976 node_cpumask
[nodenr
] = value
;
983 static void smp_parse(const char *optarg
)
985 int smp
, sockets
= 0, threads
= 0, cores
= 0;
989 smp
= strtoul(optarg
, &endptr
, 10);
990 if (endptr
!= optarg
) {
991 if (*endptr
== ',') {
995 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
996 sockets
= strtoull(option
, NULL
, 10);
997 if (get_param_value(option
, 128, "cores", endptr
) != 0)
998 cores
= strtoull(option
, NULL
, 10);
999 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1000 threads
= strtoull(option
, NULL
, 10);
1001 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1002 max_cpus
= strtoull(option
, NULL
, 10);
1004 /* compute missing values, prefer sockets over cores over threads */
1005 if (smp
== 0 || sockets
== 0) {
1006 sockets
= sockets
> 0 ? sockets
: 1;
1007 cores
= cores
> 0 ? cores
: 1;
1008 threads
= threads
> 0 ? threads
: 1;
1010 smp
= cores
* threads
* sockets
;
1014 threads
= threads
> 0 ? threads
: 1;
1015 cores
= smp
/ (sockets
* threads
);
1017 threads
= smp
/ (cores
* sockets
);
1021 smp_cores
= cores
> 0 ? cores
: 1;
1022 smp_threads
= threads
> 0 ? threads
: 1;
1024 max_cpus
= smp_cpus
;
1027 /***********************************************************/
1030 static int usb_device_add(const char *devname
)
1033 USBDevice
*dev
= NULL
;
1038 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1039 dev
= usbdevice_create(devname
);
1043 /* the other ones */
1044 #ifndef CONFIG_LINUX
1045 /* only the linux version is qdev-ified, usb-bsd still needs this */
1046 if (strstart(devname
, "host:", &p
)) {
1047 dev
= usb_host_device_open(p
);
1050 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1051 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1052 bt_new_hci(qemu_find_bt_vlan(0)));
1063 static int usb_device_del(const char *devname
)
1068 if (strstart(devname
, "host:", &p
))
1069 return usb_host_device_close(p
);
1074 p
= strchr(devname
, '.');
1077 bus_num
= strtoul(devname
, NULL
, 0);
1078 addr
= strtoul(p
+ 1, NULL
, 0);
1080 return usb_device_delete_addr(bus_num
, addr
);
1083 static int usb_parse(const char *cmdline
)
1086 r
= usb_device_add(cmdline
);
1088 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1093 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1095 const char *devname
= qdict_get_str(qdict
, "devname");
1096 if (usb_device_add(devname
) < 0) {
1097 error_report("could not add USB device '%s'", devname
);
1101 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1103 const char *devname
= qdict_get_str(qdict
, "devname");
1104 if (usb_device_del(devname
) < 0) {
1105 error_report("could not delete USB device '%s'", devname
);
1109 /***********************************************************/
1110 /* PCMCIA/Cardbus */
1112 static struct pcmcia_socket_entry_s
{
1113 PCMCIASocket
*socket
;
1114 struct pcmcia_socket_entry_s
*next
;
1115 } *pcmcia_sockets
= 0;
1117 void pcmcia_socket_register(PCMCIASocket
*socket
)
1119 struct pcmcia_socket_entry_s
*entry
;
1121 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1122 entry
->socket
= socket
;
1123 entry
->next
= pcmcia_sockets
;
1124 pcmcia_sockets
= entry
;
1127 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1129 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1131 ptr
= &pcmcia_sockets
;
1132 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1133 if (entry
->socket
== socket
) {
1139 void pcmcia_info(Monitor
*mon
)
1141 struct pcmcia_socket_entry_s
*iter
;
1143 if (!pcmcia_sockets
)
1144 monitor_printf(mon
, "No PCMCIA sockets\n");
1146 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1147 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1148 iter
->socket
->attached
? iter
->socket
->card_string
:
1152 /***********************************************************/
1153 /* machine registration */
1155 static QEMUMachine
*first_machine
= NULL
;
1156 QEMUMachine
*current_machine
= NULL
;
1158 int qemu_register_machine(QEMUMachine
*m
)
1161 pm
= &first_machine
;
1169 static QEMUMachine
*find_machine(const char *name
)
1173 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1174 if (!strcmp(m
->name
, name
))
1176 if (m
->alias
&& !strcmp(m
->alias
, name
))
1182 static QEMUMachine
*find_default_machine(void)
1186 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1187 if (m
->is_default
) {
1194 /***********************************************************/
1195 /* main execution loop */
1197 static void gui_update(void *opaque
)
1199 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1200 DisplayState
*ds
= opaque
;
1201 DisplayChangeListener
*dcl
= ds
->listeners
;
1205 while (dcl
!= NULL
) {
1206 if (dcl
->gui_timer_interval
&&
1207 dcl
->gui_timer_interval
< interval
)
1208 interval
= dcl
->gui_timer_interval
;
1211 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1214 struct vm_change_state_entry
{
1215 VMChangeStateHandler
*cb
;
1217 QLIST_ENTRY (vm_change_state_entry
) entries
;
1220 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1222 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1225 VMChangeStateEntry
*e
;
1227 e
= g_malloc0(sizeof (*e
));
1231 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1235 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1237 QLIST_REMOVE (e
, entries
);
1241 void vm_state_notify(int running
, RunState state
)
1243 VMChangeStateEntry
*e
;
1245 trace_vm_state_notify(running
, state
);
1247 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1248 e
->cb(e
->opaque
, running
, state
);
1254 if (!runstate_is_running()) {
1256 runstate_set(RUN_STATE_RUNNING
);
1257 vm_state_notify(1, RUN_STATE_RUNNING
);
1259 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1263 /* reset/shutdown handler */
1265 typedef struct QEMUResetEntry
{
1266 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1267 QEMUResetHandler
*func
;
1271 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1272 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1273 static int reset_requested
;
1274 static int shutdown_requested
, shutdown_signal
= -1;
1275 static pid_t shutdown_pid
;
1276 static int powerdown_requested
;
1277 static int debug_requested
;
1278 static RunState vmstop_requested
= RUN_STATE_MAX
;
1280 int qemu_shutdown_requested_get(void)
1282 return shutdown_requested
;
1285 int qemu_reset_requested_get(void)
1287 return reset_requested
;
1290 int qemu_shutdown_requested(void)
1292 int r
= shutdown_requested
;
1293 shutdown_requested
= 0;
1297 void qemu_kill_report(void)
1299 if (shutdown_signal
!= -1) {
1300 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1301 if (shutdown_pid
== 0) {
1302 /* This happens for eg ^C at the terminal, so it's worth
1303 * avoiding printing an odd message in that case.
1305 fputc('\n', stderr
);
1307 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1309 shutdown_signal
= -1;
1313 int qemu_reset_requested(void)
1315 int r
= reset_requested
;
1316 reset_requested
= 0;
1320 int qemu_powerdown_requested(void)
1322 int r
= powerdown_requested
;
1323 powerdown_requested
= 0;
1327 static int qemu_debug_requested(void)
1329 int r
= debug_requested
;
1330 debug_requested
= 0;
1334 /* We use RUN_STATE_MAX but any invalid value will do */
1335 static bool qemu_vmstop_requested(RunState
*r
)
1337 if (vmstop_requested
< RUN_STATE_MAX
) {
1338 *r
= vmstop_requested
;
1339 vmstop_requested
= RUN_STATE_MAX
;
1346 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1348 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1351 re
->opaque
= opaque
;
1352 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1355 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1359 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1360 if (re
->func
== func
&& re
->opaque
== opaque
) {
1361 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1368 void qemu_system_reset(bool report
)
1370 QEMUResetEntry
*re
, *nre
;
1372 /* reset all devices */
1373 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1374 re
->func(re
->opaque
);
1377 monitor_protocol_event(QEVENT_RESET
, NULL
);
1379 cpu_synchronize_all_post_reset();
1382 void qemu_system_reset_request(void)
1385 shutdown_requested
= 1;
1387 reset_requested
= 1;
1390 qemu_notify_event();
1393 void qemu_system_killed(int signal
, pid_t pid
)
1395 shutdown_signal
= signal
;
1398 qemu_system_shutdown_request();
1401 void qemu_system_shutdown_request(void)
1403 shutdown_requested
= 1;
1404 qemu_notify_event();
1407 void qemu_system_powerdown_request(void)
1409 powerdown_requested
= 1;
1410 qemu_notify_event();
1413 void qemu_system_debug_request(void)
1415 debug_requested
= 1;
1416 qemu_notify_event();
1419 void qemu_system_vmstop_request(RunState state
)
1421 vmstop_requested
= state
;
1422 qemu_notify_event();
1425 qemu_irq qemu_system_powerdown
;
1427 static bool main_loop_should_exit(void)
1430 if (qemu_debug_requested()) {
1431 vm_stop(RUN_STATE_DEBUG
);
1433 if (qemu_shutdown_requested()) {
1435 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1437 vm_stop(RUN_STATE_SHUTDOWN
);
1442 if (qemu_reset_requested()) {
1444 cpu_synchronize_all_states();
1445 qemu_system_reset(VMRESET_REPORT
);
1447 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1448 runstate_check(RUN_STATE_SHUTDOWN
)) {
1449 runstate_set(RUN_STATE_PAUSED
);
1452 if (qemu_powerdown_requested()) {
1453 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1454 qemu_irq_raise(qemu_system_powerdown
);
1456 if (qemu_vmstop_requested(&r
)) {
1462 static void main_loop(void)
1466 #ifdef CONFIG_PROFILER
1470 nonblocking
= !kvm_enabled() && last_io
> 0;
1471 #ifdef CONFIG_PROFILER
1472 ti
= profile_getclock();
1474 last_io
= main_loop_wait(nonblocking
);
1475 #ifdef CONFIG_PROFILER
1476 dev_time
+= profile_getclock() - ti
;
1478 } while (!main_loop_should_exit());
1481 static void version(void)
1483 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1486 static void help(int exitcode
)
1489 printf("usage: %s [options] [disk_image]\n\n"
1490 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1491 error_get_progname());
1493 #define QEMU_OPTIONS_GENERATE_HELP
1494 #include "qemu-options-wrapper.h"
1496 printf("\nDuring emulation, the following keys are useful:\n"
1497 "ctrl-alt-f toggle full screen\n"
1498 "ctrl-alt-n switch to virtual console 'n'\n"
1499 "ctrl-alt toggle mouse and keyboard grab\n"
1501 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1506 #define HAS_ARG 0x0001
1508 typedef struct QEMUOption
{
1515 static const QEMUOption qemu_options
[] = {
1516 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1517 #define QEMU_OPTIONS_GENERATE_OPTIONS
1518 #include "qemu-options-wrapper.h"
1522 static bool vga_available(void)
1524 return qdev_exists("VGA") || qdev_exists("isa-vga");
1527 static bool cirrus_vga_available(void)
1529 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1532 static bool vmware_vga_available(void)
1534 return qdev_exists("vmware-svga");
1537 static void select_vgahw (const char *p
)
1541 vga_interface_type
= VGA_NONE
;
1542 if (strstart(p
, "std", &opts
)) {
1543 if (vga_available()) {
1544 vga_interface_type
= VGA_STD
;
1546 fprintf(stderr
, "Error: standard VGA not available\n");
1549 } else if (strstart(p
, "cirrus", &opts
)) {
1550 if (cirrus_vga_available()) {
1551 vga_interface_type
= VGA_CIRRUS
;
1553 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1556 } else if (strstart(p
, "vmware", &opts
)) {
1557 if (vmware_vga_available()) {
1558 vga_interface_type
= VGA_VMWARE
;
1560 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1563 } else if (strstart(p
, "xenfb", &opts
)) {
1564 vga_interface_type
= VGA_XENFB
;
1565 } else if (strstart(p
, "qxl", &opts
)) {
1566 vga_interface_type
= VGA_QXL
;
1567 } else if (!strstart(p
, "none", &opts
)) {
1569 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1573 const char *nextopt
;
1575 if (strstart(opts
, ",retrace=", &nextopt
)) {
1577 if (strstart(opts
, "dumb", &nextopt
))
1578 vga_retrace_method
= VGA_RETRACE_DUMB
;
1579 else if (strstart(opts
, "precise", &nextopt
))
1580 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1581 else goto invalid_vga
;
1582 } else goto invalid_vga
;
1587 static DisplayType
select_display(const char *p
)
1590 DisplayType display
= DT_DEFAULT
;
1592 if (strstart(p
, "sdl", &opts
)) {
1596 const char *nextopt
;
1598 if (strstart(opts
, ",frame=", &nextopt
)) {
1600 if (strstart(opts
, "on", &nextopt
)) {
1602 } else if (strstart(opts
, "off", &nextopt
)) {
1605 goto invalid_sdl_args
;
1607 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1609 if (strstart(opts
, "on", &nextopt
)) {
1611 } else if (strstart(opts
, "off", &nextopt
)) {
1614 goto invalid_sdl_args
;
1616 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1618 if (strstart(opts
, "on", &nextopt
)) {
1620 } else if (strstart(opts
, "off", &nextopt
)) {
1623 goto invalid_sdl_args
;
1625 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1627 if (strstart(opts
, "on", &nextopt
)) {
1629 } else if (strstart(opts
, "off", &nextopt
)) {
1632 goto invalid_sdl_args
;
1636 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1642 fprintf(stderr
, "SDL support is disabled\n");
1645 } else if (strstart(p
, "vnc", &opts
)) {
1650 const char *nextopt
;
1652 if (strstart(opts
, "=", &nextopt
)) {
1653 vnc_display
= nextopt
;
1657 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1661 fprintf(stderr
, "VNC support is disabled\n");
1664 } else if (strstart(p
, "curses", &opts
)) {
1665 #ifdef CONFIG_CURSES
1666 display
= DT_CURSES
;
1668 fprintf(stderr
, "Curses support is disabled\n");
1671 } else if (strstart(p
, "none", &opts
)) {
1674 fprintf(stderr
, "Unknown display type: %s\n", p
);
1681 static int balloon_parse(const char *arg
)
1685 if (strcmp(arg
, "none") == 0) {
1689 if (!strncmp(arg
, "virtio", 6)) {
1690 if (arg
[6] == ',') {
1691 /* have params -> parse them */
1692 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1696 /* create empty opts */
1697 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1699 qemu_opt_set(opts
, "driver", "virtio-balloon");
1706 char *qemu_find_file(int type
, const char *name
)
1712 /* If name contains path separators then try it as a straight path. */
1713 if ((strchr(name
, '/') || strchr(name
, '\\'))
1714 && access(name
, R_OK
) == 0) {
1715 return g_strdup(name
);
1718 case QEMU_FILE_TYPE_BIOS
:
1721 case QEMU_FILE_TYPE_KEYMAP
:
1722 subdir
= "keymaps/";
1727 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1728 buf
= g_malloc0(len
);
1729 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1730 if (access(buf
, R_OK
)) {
1737 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1739 return qdev_device_help(opts
);
1742 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1746 dev
= qdev_device_add(opts
);
1752 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1754 CharDriverState
*chr
;
1756 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1762 #ifdef CONFIG_VIRTFS
1763 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1766 ret
= qemu_fsdev_add(opts
);
1772 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1774 CharDriverState
*chr
;
1775 const char *chardev
;
1779 mode
= qemu_opt_get(opts
, "mode");
1783 if (strcmp(mode
, "readline") == 0) {
1784 flags
= MONITOR_USE_READLINE
;
1785 } else if (strcmp(mode
, "control") == 0) {
1786 flags
= MONITOR_USE_CONTROL
;
1788 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1792 if (qemu_opt_get_bool(opts
, "pretty", 0))
1793 flags
|= MONITOR_USE_PRETTY
;
1795 if (qemu_opt_get_bool(opts
, "default", 0))
1796 flags
|= MONITOR_IS_DEFAULT
;
1798 chardev
= qemu_opt_get(opts
, "chardev");
1799 chr
= qemu_chr_find(chardev
);
1801 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1805 monitor_init(chr
, flags
);
1809 static void monitor_parse(const char *optarg
, const char *mode
)
1811 static int monitor_device_index
= 0;
1817 if (strstart(optarg
, "chardev:", &p
)) {
1818 snprintf(label
, sizeof(label
), "%s", p
);
1820 snprintf(label
, sizeof(label
), "compat_monitor%d",
1821 monitor_device_index
);
1822 if (monitor_device_index
== 0) {
1825 opts
= qemu_chr_parse_compat(label
, optarg
);
1827 fprintf(stderr
, "parse error: %s\n", optarg
);
1832 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1834 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1837 qemu_opt_set(opts
, "mode", mode
);
1838 qemu_opt_set(opts
, "chardev", label
);
1840 qemu_opt_set(opts
, "default", "on");
1841 monitor_device_index
++;
1844 struct device_config
{
1846 DEV_USB
, /* -usbdevice */
1848 DEV_SERIAL
, /* -serial */
1849 DEV_PARALLEL
, /* -parallel */
1850 DEV_VIRTCON
, /* -virtioconsole */
1851 DEV_DEBUGCON
, /* -debugcon */
1853 const char *cmdline
;
1854 QTAILQ_ENTRY(device_config
) next
;
1856 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1858 static void add_device_config(int type
, const char *cmdline
)
1860 struct device_config
*conf
;
1862 conf
= g_malloc0(sizeof(*conf
));
1864 conf
->cmdline
= cmdline
;
1865 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1868 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1870 struct device_config
*conf
;
1873 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1874 if (conf
->type
!= type
)
1876 rc
= func(conf
->cmdline
);
1883 static int serial_parse(const char *devname
)
1885 static int index
= 0;
1888 if (strcmp(devname
, "none") == 0)
1890 if (index
== MAX_SERIAL_PORTS
) {
1891 fprintf(stderr
, "qemu: too many serial ports\n");
1894 snprintf(label
, sizeof(label
), "serial%d", index
);
1895 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1896 if (!serial_hds
[index
]) {
1897 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1898 devname
, strerror(errno
));
1905 static int parallel_parse(const char *devname
)
1907 static int index
= 0;
1910 if (strcmp(devname
, "none") == 0)
1912 if (index
== MAX_PARALLEL_PORTS
) {
1913 fprintf(stderr
, "qemu: too many parallel ports\n");
1916 snprintf(label
, sizeof(label
), "parallel%d", index
);
1917 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1918 if (!parallel_hds
[index
]) {
1919 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1920 devname
, strerror(errno
));
1927 static int virtcon_parse(const char *devname
)
1929 QemuOptsList
*device
= qemu_find_opts("device");
1930 static int index
= 0;
1932 QemuOpts
*bus_opts
, *dev_opts
;
1934 if (strcmp(devname
, "none") == 0)
1936 if (index
== MAX_VIRTIO_CONSOLES
) {
1937 fprintf(stderr
, "qemu: too many virtio consoles\n");
1941 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1942 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1944 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1945 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1947 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1948 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1949 if (!virtcon_hds
[index
]) {
1950 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1951 devname
, strerror(errno
));
1954 qemu_opt_set(dev_opts
, "chardev", label
);
1960 static int debugcon_parse(const char *devname
)
1964 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
1967 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1969 fprintf(stderr
, "qemu: already have a debugcon device\n");
1972 qemu_opt_set(opts
, "driver", "isa-debugcon");
1973 qemu_opt_set(opts
, "chardev", "debugcon");
1977 static QEMUMachine
*machine_parse(const char *name
)
1979 QEMUMachine
*m
, *machine
= NULL
;
1982 machine
= find_machine(name
);
1987 printf("Supported machines are:\n");
1988 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1990 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
1992 printf("%-10s %s%s\n", m
->name
, m
->desc
,
1993 m
->is_default
? " (default)" : "");
1995 exit(!name
|| *name
!= '?');
1998 static int tcg_init(void)
2000 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2005 const char *opt_name
;
2007 int (*available
)(void);
2011 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2012 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2013 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2016 static int configure_accelerator(void)
2018 const char *p
= NULL
;
2021 bool accel_initalised
= 0;
2022 bool init_failed
= 0;
2024 QemuOptsList
*list
= qemu_find_opts("machine");
2025 if (!QTAILQ_EMPTY(&list
->head
)) {
2026 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2030 /* Use the default "accelerator", tcg */
2034 while (!accel_initalised
&& *p
!= '\0') {
2038 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2039 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2040 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2041 *(accel_list
[i
].allowed
) = 1;
2042 ret
= accel_list
[i
].init();
2045 if (!accel_list
[i
].available()) {
2046 printf("%s not supported for this target\n",
2047 accel_list
[i
].name
);
2049 fprintf(stderr
, "failed to initialize %s: %s\n",
2053 *(accel_list
[i
].allowed
) = 0;
2055 accel_initalised
= 1;
2060 if (i
== ARRAY_SIZE(accel_list
)) {
2061 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2065 if (!accel_initalised
) {
2066 fprintf(stderr
, "No accelerator found!\n");
2071 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2074 return !accel_initalised
;
2077 void qemu_add_exit_notifier(Notifier
*notify
)
2079 notifier_list_add(&exit_notifiers
, notify
);
2082 void qemu_remove_exit_notifier(Notifier
*notify
)
2084 notifier_list_remove(&exit_notifiers
, notify
);
2087 static void qemu_run_exit_notifiers(void)
2089 notifier_list_notify(&exit_notifiers
, NULL
);
2092 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2094 notifier_list_add(&machine_init_done_notifiers
, notify
);
2097 static void qemu_run_machine_init_done_notifiers(void)
2099 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2102 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2103 const char **poptarg
, int *poptind
)
2105 const QEMUOption
*popt
;
2106 int optind
= *poptind
;
2107 char *r
= argv
[optind
];
2110 loc_set_cmdline(argv
, optind
, 1);
2112 /* Treat --foo the same as -foo. */
2115 popt
= qemu_options
;
2118 error_report("invalid option");
2121 if (!strcmp(popt
->name
, r
+ 1))
2125 if (popt
->flags
& HAS_ARG
) {
2126 if (optind
>= argc
) {
2127 error_report("requires an argument");
2130 optarg
= argv
[optind
++];
2131 loc_set_cmdline(argv
, optind
- 2, 2);
2142 static gpointer
malloc_and_trace(gsize n_bytes
)
2144 void *ptr
= malloc(n_bytes
);
2145 trace_g_malloc(n_bytes
, ptr
);
2149 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2151 void *ptr
= realloc(mem
, n_bytes
);
2152 trace_g_realloc(mem
, n_bytes
, ptr
);
2156 static void free_and_trace(gpointer mem
)
2162 int main(int argc
, char **argv
, char **envp
)
2164 const char *gdbstub_dev
= NULL
;
2166 int snapshot
, linux_boot
;
2167 const char *icount_option
= NULL
;
2168 const char *initrd_filename
;
2169 const char *kernel_filename
, *kernel_cmdline
;
2170 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2172 DisplayChangeListener
*dcl
;
2173 int cyls
, heads
, secs
, translation
;
2174 QemuOpts
*hda_opts
= NULL
, *opts
;
2175 QemuOptsList
*olist
;
2178 const char *loadvm
= NULL
;
2179 QEMUMachine
*machine
;
2180 const char *cpu_model
;
2181 const char *vga_model
= NULL
;
2182 const char *pid_file
= NULL
;
2183 const char *incoming
= NULL
;
2185 int show_vnc_port
= 0;
2188 const char *log_mask
= NULL
;
2189 const char *log_file
= NULL
;
2190 GMemVTable mem_trace
= {
2191 .malloc
= malloc_and_trace
,
2192 .realloc
= realloc_and_trace
,
2193 .free
= free_and_trace
,
2195 const char *trace_events
= NULL
;
2196 const char *trace_file
= NULL
;
2198 atexit(qemu_run_exit_notifiers
);
2199 error_set_progname(argv
[0]);
2201 g_mem_set_vtable(&mem_trace
);
2202 if (!g_thread_supported()) {
2203 #if !GLIB_CHECK_VERSION(2, 31, 0)
2204 g_thread_init(NULL
);
2206 fprintf(stderr
, "glib threading failed to initialize.\n");
2214 rtc_clock
= host_clock
;
2216 qemu_cache_utils_init(envp
);
2218 QLIST_INIT (&vm_change_state_head
);
2219 os_setup_early_signal_handling();
2221 module_call_init(MODULE_INIT_MACHINE
);
2222 machine
= find_default_machine();
2224 initrd_filename
= NULL
;
2227 kernel_filename
= NULL
;
2228 kernel_cmdline
= "";
2229 cyls
= heads
= secs
= 0;
2230 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2232 for (i
= 0; i
< MAX_NODES
; i
++) {
2234 node_cpumask
[i
] = 0;
2242 /* first pass of option parsing */
2244 while (optind
< argc
) {
2245 if (argv
[optind
][0] != '-') {
2250 const QEMUOption
*popt
;
2252 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2253 switch (popt
->index
) {
2254 case QEMU_OPTION_nodefconfig
:
2264 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2265 if (ret
< 0 && ret
!= -ENOENT
) {
2269 ret
= qemu_read_config_file(arch_config_name
);
2270 if (ret
< 0 && ret
!= -ENOENT
) {
2276 /* second pass of option parsing */
2281 if (argv
[optind
][0] != '-') {
2282 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2284 const QEMUOption
*popt
;
2286 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2287 if (!(popt
->arch_mask
& arch_type
)) {
2288 printf("Option %s not supported for this target\n", popt
->name
);
2291 switch(popt
->index
) {
2293 machine
= machine_parse(optarg
);
2295 case QEMU_OPTION_cpu
:
2296 /* hw initialization will check this */
2297 if (*optarg
== '?') {
2298 list_cpus(stdout
, &fprintf
, optarg
);
2304 case QEMU_OPTION_initrd
:
2305 initrd_filename
= optarg
;
2307 case QEMU_OPTION_hda
:
2311 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2313 snprintf(buf
, sizeof(buf
),
2314 "%s,cyls=%d,heads=%d,secs=%d%s",
2315 HD_OPTS
, cyls
, heads
, secs
,
2316 translation
== BIOS_ATA_TRANSLATION_LBA
?
2318 translation
== BIOS_ATA_TRANSLATION_NONE
?
2319 ",trans=none" : "");
2320 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2323 case QEMU_OPTION_hdb
:
2324 case QEMU_OPTION_hdc
:
2325 case QEMU_OPTION_hdd
:
2326 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2329 case QEMU_OPTION_drive
:
2330 if (drive_def(optarg
) == NULL
) {
2334 case QEMU_OPTION_set
:
2335 if (qemu_set_option(optarg
) != 0)
2338 case QEMU_OPTION_global
:
2339 if (qemu_global_option(optarg
) != 0)
2342 case QEMU_OPTION_mtdblock
:
2343 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2345 case QEMU_OPTION_sd
:
2346 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2348 case QEMU_OPTION_pflash
:
2349 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2351 case QEMU_OPTION_snapshot
:
2354 case QEMU_OPTION_hdachs
:
2358 cyls
= strtol(p
, (char **)&p
, 0);
2359 if (cyls
< 1 || cyls
> 16383)
2364 heads
= strtol(p
, (char **)&p
, 0);
2365 if (heads
< 1 || heads
> 16)
2370 secs
= strtol(p
, (char **)&p
, 0);
2371 if (secs
< 1 || secs
> 63)
2375 if (!strcmp(p
, "none"))
2376 translation
= BIOS_ATA_TRANSLATION_NONE
;
2377 else if (!strcmp(p
, "lba"))
2378 translation
= BIOS_ATA_TRANSLATION_LBA
;
2379 else if (!strcmp(p
, "auto"))
2380 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2383 } else if (*p
!= '\0') {
2385 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2388 if (hda_opts
!= NULL
) {
2390 snprintf(num
, sizeof(num
), "%d", cyls
);
2391 qemu_opt_set(hda_opts
, "cyls", num
);
2392 snprintf(num
, sizeof(num
), "%d", heads
);
2393 qemu_opt_set(hda_opts
, "heads", num
);
2394 snprintf(num
, sizeof(num
), "%d", secs
);
2395 qemu_opt_set(hda_opts
, "secs", num
);
2396 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2397 qemu_opt_set(hda_opts
, "trans", "lba");
2398 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2399 qemu_opt_set(hda_opts
, "trans", "none");
2403 case QEMU_OPTION_numa
:
2404 if (nb_numa_nodes
>= MAX_NODES
) {
2405 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2410 case QEMU_OPTION_display
:
2411 display_type
= select_display(optarg
);
2413 case QEMU_OPTION_nographic
:
2414 display_type
= DT_NOGRAPHIC
;
2416 case QEMU_OPTION_curses
:
2417 #ifdef CONFIG_CURSES
2418 display_type
= DT_CURSES
;
2420 fprintf(stderr
, "Curses support is disabled\n");
2424 case QEMU_OPTION_portrait
:
2425 graphic_rotate
= 90;
2427 case QEMU_OPTION_rotate
:
2428 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2429 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2430 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2432 "qemu: only 90, 180, 270 deg rotation is available\n");
2436 case QEMU_OPTION_kernel
:
2437 kernel_filename
= optarg
;
2439 case QEMU_OPTION_append
:
2440 kernel_cmdline
= optarg
;
2442 case QEMU_OPTION_cdrom
:
2443 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2445 case QEMU_OPTION_boot
:
2447 static const char * const params
[] = {
2448 "order", "once", "menu",
2449 "splash", "splash-time", NULL
2451 char buf
[sizeof(boot_devices
)];
2452 char *standard_boot_devices
;
2455 if (!strchr(optarg
, '=')) {
2457 pstrcpy(buf
, sizeof(buf
), optarg
);
2458 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2460 "qemu: unknown boot parameter '%s' in '%s'\n",
2466 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2467 validate_bootdevices(buf
);
2468 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2471 if (get_param_value(buf
, sizeof(buf
),
2473 validate_bootdevices(buf
);
2474 standard_boot_devices
= g_strdup(boot_devices
);
2475 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2476 qemu_register_reset(restore_boot_devices
,
2477 standard_boot_devices
);
2479 if (get_param_value(buf
, sizeof(buf
),
2481 if (!strcmp(buf
, "on")) {
2483 } else if (!strcmp(buf
, "off")) {
2487 "qemu: invalid option value '%s'\n",
2492 qemu_opts_parse(qemu_find_opts("boot-opts"),
2497 case QEMU_OPTION_fda
:
2498 case QEMU_OPTION_fdb
:
2499 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2502 case QEMU_OPTION_no_fd_bootchk
:
2505 case QEMU_OPTION_netdev
:
2506 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2510 case QEMU_OPTION_net
:
2511 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2516 case QEMU_OPTION_tftp
:
2517 legacy_tftp_prefix
= optarg
;
2519 case QEMU_OPTION_bootp
:
2520 legacy_bootp_filename
= optarg
;
2522 case QEMU_OPTION_redir
:
2523 if (net_slirp_redir(optarg
) < 0)
2527 case QEMU_OPTION_bt
:
2528 add_device_config(DEV_BT
, optarg
);
2530 case QEMU_OPTION_audio_help
:
2531 if (!(audio_available())) {
2532 printf("Option %s not supported for this target\n", popt
->name
);
2538 case QEMU_OPTION_soundhw
:
2539 if (!(audio_available())) {
2540 printf("Option %s not supported for this target\n", popt
->name
);
2543 select_soundhw (optarg
);
2548 case QEMU_OPTION_version
:
2552 case QEMU_OPTION_m
: {
2556 value
= strtosz(optarg
, &end
);
2557 if (value
< 0 || *end
) {
2558 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2562 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2563 fprintf(stderr
, "qemu: ram size too large\n");
2569 case QEMU_OPTION_mempath
:
2573 case QEMU_OPTION_mem_prealloc
:
2584 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2586 case QEMU_OPTION_gdb
:
2587 gdbstub_dev
= optarg
;
2592 case QEMU_OPTION_bios
:
2595 case QEMU_OPTION_singlestep
:
2602 keyboard_layout
= optarg
;
2604 case QEMU_OPTION_localtime
:
2607 case QEMU_OPTION_vga
:
2615 w
= strtol(p
, (char **)&p
, 10);
2618 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2624 h
= strtol(p
, (char **)&p
, 10);
2629 depth
= strtol(p
, (char **)&p
, 10);
2630 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2631 depth
!= 24 && depth
!= 32)
2633 } else if (*p
== '\0') {
2634 depth
= graphic_depth
;
2641 graphic_depth
= depth
;
2644 case QEMU_OPTION_echr
:
2647 term_escape_char
= strtol(optarg
, &r
, 0);
2649 printf("Bad argument to echr\n");
2652 case QEMU_OPTION_monitor
:
2653 monitor_parse(optarg
, "readline");
2654 default_monitor
= 0;
2656 case QEMU_OPTION_qmp
:
2657 monitor_parse(optarg
, "control");
2658 default_monitor
= 0;
2660 case QEMU_OPTION_mon
:
2661 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2665 default_monitor
= 0;
2667 case QEMU_OPTION_chardev
:
2668 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2673 case QEMU_OPTION_fsdev
:
2674 olist
= qemu_find_opts("fsdev");
2676 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2679 opts
= qemu_opts_parse(olist
, optarg
, 1);
2681 fprintf(stderr
, "parse error: %s\n", optarg
);
2685 case QEMU_OPTION_virtfs
: {
2688 const char *writeout
, *sock_fd
, *socket
;
2690 olist
= qemu_find_opts("virtfs");
2692 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2695 opts
= qemu_opts_parse(olist
, optarg
, 1);
2697 fprintf(stderr
, "parse error: %s\n", optarg
);
2701 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2702 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2703 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2706 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2707 qemu_opt_get(opts
, "mount_tag"), 1);
2709 fprintf(stderr
, "duplicate fsdev id: %s\n",
2710 qemu_opt_get(opts
, "mount_tag"));
2714 writeout
= qemu_opt_get(opts
, "writeout");
2716 #ifdef CONFIG_SYNC_FILE_RANGE
2717 qemu_opt_set(fsdev
, "writeout", writeout
);
2719 fprintf(stderr
, "writeout=immediate not supported on "
2724 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2725 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2726 qemu_opt_set(fsdev
, "security_model",
2727 qemu_opt_get(opts
, "security_model"));
2728 socket
= qemu_opt_get(opts
, "socket");
2730 qemu_opt_set(fsdev
, "socket", socket
);
2732 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2734 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2737 qemu_opt_set_bool(fsdev
, "readonly",
2738 qemu_opt_get_bool(opts
, "readonly", 0));
2739 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2740 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2741 qemu_opt_set(device
, "fsdev",
2742 qemu_opt_get(opts
, "mount_tag"));
2743 qemu_opt_set(device
, "mount_tag",
2744 qemu_opt_get(opts
, "mount_tag"));
2747 case QEMU_OPTION_virtfs_synth
: {
2751 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2753 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2756 qemu_opt_set(fsdev
, "fsdriver", "synth");
2758 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2759 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2760 qemu_opt_set(device
, "fsdev", "v_synth");
2761 qemu_opt_set(device
, "mount_tag", "v_synth");
2764 case QEMU_OPTION_serial
:
2765 add_device_config(DEV_SERIAL
, optarg
);
2767 if (strncmp(optarg
, "mon:", 4) == 0) {
2768 default_monitor
= 0;
2771 case QEMU_OPTION_watchdog
:
2774 "qemu: only one watchdog option may be given\n");
2779 case QEMU_OPTION_watchdog_action
:
2780 if (select_watchdog_action(optarg
) == -1) {
2781 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2785 case QEMU_OPTION_virtiocon
:
2786 add_device_config(DEV_VIRTCON
, optarg
);
2787 default_virtcon
= 0;
2788 if (strncmp(optarg
, "mon:", 4) == 0) {
2789 default_monitor
= 0;
2792 case QEMU_OPTION_parallel
:
2793 add_device_config(DEV_PARALLEL
, optarg
);
2794 default_parallel
= 0;
2795 if (strncmp(optarg
, "mon:", 4) == 0) {
2796 default_monitor
= 0;
2799 case QEMU_OPTION_debugcon
:
2800 add_device_config(DEV_DEBUGCON
, optarg
);
2802 case QEMU_OPTION_loadvm
:
2805 case QEMU_OPTION_full_screen
:
2809 case QEMU_OPTION_no_frame
:
2812 case QEMU_OPTION_alt_grab
:
2815 case QEMU_OPTION_ctrl_grab
:
2818 case QEMU_OPTION_no_quit
:
2821 case QEMU_OPTION_sdl
:
2822 display_type
= DT_SDL
;
2825 case QEMU_OPTION_no_frame
:
2826 case QEMU_OPTION_alt_grab
:
2827 case QEMU_OPTION_ctrl_grab
:
2828 case QEMU_OPTION_no_quit
:
2829 case QEMU_OPTION_sdl
:
2830 fprintf(stderr
, "SDL support is disabled\n");
2833 case QEMU_OPTION_pidfile
:
2836 case QEMU_OPTION_win2k_hack
:
2837 win2k_install_hack
= 1;
2839 case QEMU_OPTION_rtc_td_hack
:
2842 case QEMU_OPTION_acpitable
:
2843 do_acpitable_option(optarg
);
2845 case QEMU_OPTION_smbios
:
2846 do_smbios_option(optarg
);
2848 case QEMU_OPTION_enable_kvm
:
2849 olist
= qemu_find_opts("machine");
2850 qemu_opts_reset(olist
);
2851 qemu_opts_parse(olist
, "accel=kvm", 0);
2853 case QEMU_OPTION_machine
:
2854 olist
= qemu_find_opts("machine");
2855 qemu_opts_reset(olist
);
2856 opts
= qemu_opts_parse(olist
, optarg
, 1);
2858 fprintf(stderr
, "parse error: %s\n", optarg
);
2861 optarg
= qemu_opt_get(opts
, "type");
2863 machine
= machine_parse(optarg
);
2866 case QEMU_OPTION_usb
:
2869 case QEMU_OPTION_usbdevice
:
2871 add_device_config(DEV_USB
, optarg
);
2873 case QEMU_OPTION_device
:
2874 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2878 case QEMU_OPTION_smp
:
2881 fprintf(stderr
, "Invalid number of CPUs\n");
2884 if (max_cpus
< smp_cpus
) {
2885 fprintf(stderr
, "maxcpus must be equal to or greater than "
2889 if (max_cpus
> 255) {
2890 fprintf(stderr
, "Unsupported number of maxcpus\n");
2894 case QEMU_OPTION_vnc
:
2897 vnc_display
= optarg
;
2899 fprintf(stderr
, "VNC support is disabled\n");
2903 case QEMU_OPTION_no_acpi
:
2906 case QEMU_OPTION_no_hpet
:
2909 case QEMU_OPTION_balloon
:
2910 if (balloon_parse(optarg
) < 0) {
2911 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2915 case QEMU_OPTION_no_reboot
:
2918 case QEMU_OPTION_no_shutdown
:
2921 case QEMU_OPTION_show_cursor
:
2924 case QEMU_OPTION_uuid
:
2925 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2926 fprintf(stderr
, "Fail to parse UUID string."
2927 " Wrong format.\n");
2931 case QEMU_OPTION_option_rom
:
2932 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2933 fprintf(stderr
, "Too many option ROMs\n");
2936 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2937 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2938 option_rom
[nb_option_roms
].bootindex
=
2939 qemu_opt_get_number(opts
, "bootindex", -1);
2940 if (!option_rom
[nb_option_roms
].name
) {
2941 fprintf(stderr
, "Option ROM file is not specified\n");
2946 case QEMU_OPTION_semihosting
:
2947 semihosting_enabled
= 1;
2949 case QEMU_OPTION_name
:
2950 qemu_name
= g_strdup(optarg
);
2952 char *p
= strchr(qemu_name
, ',');
2955 if (strncmp(p
, "process=", 8)) {
2956 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2960 os_set_proc_name(p
);
2964 case QEMU_OPTION_prom_env
:
2965 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
2966 fprintf(stderr
, "Too many prom variables\n");
2969 prom_envs
[nb_prom_envs
] = optarg
;
2972 case QEMU_OPTION_old_param
:
2975 case QEMU_OPTION_clock
:
2976 configure_alarms(optarg
);
2978 case QEMU_OPTION_startdate
:
2979 configure_rtc_date_offset(optarg
, 1);
2981 case QEMU_OPTION_rtc
:
2982 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
2986 configure_rtc(opts
);
2988 case QEMU_OPTION_tb_size
:
2989 tcg_tb_size
= strtol(optarg
, NULL
, 0);
2990 if (tcg_tb_size
< 0) {
2994 case QEMU_OPTION_icount
:
2995 icount_option
= optarg
;
2997 case QEMU_OPTION_incoming
:
3000 case QEMU_OPTION_nodefaults
:
3002 default_parallel
= 0;
3003 default_virtcon
= 0;
3004 default_monitor
= 0;
3011 case QEMU_OPTION_xen_domid
:
3012 if (!(xen_available())) {
3013 printf("Option %s not supported for this target\n", popt
->name
);
3016 xen_domid
= atoi(optarg
);
3018 case QEMU_OPTION_xen_create
:
3019 if (!(xen_available())) {
3020 printf("Option %s not supported for this target\n", popt
->name
);
3023 xen_mode
= XEN_CREATE
;
3025 case QEMU_OPTION_xen_attach
:
3026 if (!(xen_available())) {
3027 printf("Option %s not supported for this target\n", popt
->name
);
3030 xen_mode
= XEN_ATTACH
;
3032 case QEMU_OPTION_trace
:
3034 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3038 trace_events
= qemu_opt_get(opts
, "events");
3039 trace_file
= qemu_opt_get(opts
, "file");
3042 case QEMU_OPTION_readconfig
:
3044 int ret
= qemu_read_config_file(optarg
);
3046 fprintf(stderr
, "read config %s: %s\n", optarg
,
3052 case QEMU_OPTION_spice
:
3053 olist
= qemu_find_opts("spice");
3055 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3058 opts
= qemu_opts_parse(olist
, optarg
, 0);
3060 fprintf(stderr
, "parse error: %s\n", optarg
);
3064 case QEMU_OPTION_writeconfig
:
3067 if (strcmp(optarg
, "-") == 0) {
3070 fp
= fopen(optarg
, "w");
3072 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3076 qemu_config_write(fp
);
3081 os_parse_cmd_args(popt
->index
, optarg
);
3087 /* Open the logfile at this point, if necessary. We can't open the logfile
3088 * when encountering either of the logging options (-d or -D) because the
3089 * other one may be encountered later on the command line, changing the
3090 * location or level of logging.
3094 set_cpu_log_filename(log_file
);
3096 set_cpu_log(log_mask
);
3099 if (!trace_backend_init(trace_events
, trace_file
)) {
3103 /* If no data_dir is specified then try to find it relative to the
3106 data_dir
= os_find_datadir(argv
[0]);
3108 /* If all else fails use the install path specified when building. */
3110 data_dir
= CONFIG_QEMU_DATADIR
;
3113 if (machine
== NULL
) {
3114 fprintf(stderr
, "No machine found.\n");
3119 * Default to max_cpus = smp_cpus, in case the user doesn't
3120 * specify a max_cpus value.
3123 max_cpus
= smp_cpus
;
3125 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3126 if (smp_cpus
> machine
->max_cpus
) {
3127 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3128 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3134 * Get the default machine options from the machine if it is not already
3135 * specified either by the configuration file or by the command line.
3137 if (machine
->default_machine_opts
) {
3138 QemuOptsList
*list
= qemu_find_opts("machine");
3139 const char *p
= NULL
;
3141 if (!QTAILQ_EMPTY(&list
->head
)) {
3142 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3145 qemu_opts_reset(list
);
3146 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3148 fprintf(stderr
, "parse error for machine %s: %s\n",
3149 machine
->name
, machine
->default_machine_opts
);
3155 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3156 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3158 if (machine
->no_serial
) {
3161 if (machine
->no_parallel
) {
3162 default_parallel
= 0;
3164 if (!machine
->use_virtcon
) {
3165 default_virtcon
= 0;
3167 if (machine
->no_floppy
) {
3170 if (machine
->no_cdrom
) {
3173 if (machine
->no_sdcard
) {
3177 if (display_type
== DT_NOGRAPHIC
) {
3178 if (default_parallel
)
3179 add_device_config(DEV_PARALLEL
, "null");
3180 if (default_serial
&& default_monitor
) {
3181 add_device_config(DEV_SERIAL
, "mon:stdio");
3182 } else if (default_virtcon
&& default_monitor
) {
3183 add_device_config(DEV_VIRTCON
, "mon:stdio");
3186 add_device_config(DEV_SERIAL
, "stdio");
3187 if (default_virtcon
)
3188 add_device_config(DEV_VIRTCON
, "stdio");
3189 if (default_monitor
)
3190 monitor_parse("stdio", "readline");
3194 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3195 if (default_parallel
)
3196 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3197 if (default_monitor
)
3198 monitor_parse("vc:80Cx24C", "readline");
3199 if (default_virtcon
)
3200 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3205 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3207 #ifdef CONFIG_VIRTFS
3208 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3215 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3220 /* init the memory */
3221 if (ram_size
== 0) {
3222 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3225 configure_accelerator();
3227 qemu_init_cpu_loop();
3228 if (qemu_init_main_loop()) {
3229 fprintf(stderr
, "qemu_init_main_loop failed\n");
3232 linux_boot
= (kernel_filename
!= NULL
);
3234 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3235 fprintf(stderr
, "-append only allowed with -kernel option\n");
3239 if (!linux_boot
&& initrd_filename
!= NULL
) {
3240 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3244 os_set_line_buffering();
3246 if (init_timer_alarm() < 0) {
3247 fprintf(stderr
, "could not initialize alarm timer\n");
3251 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3252 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3255 configure_icount(icount_option
);
3257 if (net_init_clients() < 0) {
3261 /* init the bluetooth world */
3262 if (foreach_device_config(DEV_BT
, bt_parse
))
3265 if (!xen_enabled()) {
3266 /* On 32-bit hosts, QEMU is limited by virtual address space */
3267 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3268 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3273 cpu_exec_init_all();
3275 bdrv_init_with_whitelist();
3279 /* open the virtual block devices */
3281 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3282 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3285 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3286 IF_DEFAULT
, 2, CDROM_OPTS
);
3287 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3288 IF_FLOPPY
, 0, FD_OPTS
);
3289 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3292 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3295 if (nb_numa_nodes
> 0) {
3298 if (nb_numa_nodes
> MAX_NODES
) {
3299 nb_numa_nodes
= MAX_NODES
;
3302 /* If no memory size if given for any node, assume the default case
3303 * and distribute the available memory equally across all nodes
3305 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3306 if (node_mem
[i
] != 0)
3309 if (i
== nb_numa_nodes
) {
3310 uint64_t usedmem
= 0;
3312 /* On Linux, the each node's border has to be 8MB aligned,
3313 * the final node gets the rest.
3315 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3316 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3317 usedmem
+= node_mem
[i
];
3319 node_mem
[i
] = ram_size
- usedmem
;
3322 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3323 if (node_cpumask
[i
] != 0)
3326 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3327 * must cope with this anyway, because there are BIOSes out there in
3328 * real machines which also use this scheme.
3330 if (i
== nb_numa_nodes
) {
3331 for (i
= 0; i
< max_cpus
; i
++) {
3332 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3337 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3341 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3343 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3345 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3347 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3350 module_call_init(MODULE_INIT_DEVICE
);
3352 /* must be after qdev registration but before machine init */
3354 select_vgahw(vga_model
);
3355 } else if (cirrus_vga_available()) {
3356 select_vgahw("cirrus");
3358 select_vgahw("none");
3361 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3365 i
= select_watchdog(watchdog
);
3367 exit (i
== 1 ? 1 : 0);
3370 if (machine
->compat_props
) {
3371 qdev_prop_register_global_list(machine
->compat_props
);
3375 qdev_machine_init();
3377 machine
->init(ram_size
, boot_devices
,
3378 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3380 cpu_synchronize_all_post_init();
3384 current_machine
= machine
;
3386 /* init USB devices */
3388 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3392 /* init generic devices */
3393 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3396 net_check_clients();
3398 /* just use the first displaystate for the moment */
3399 ds
= get_displaystate();
3403 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3404 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3405 display_type
= DT_SDL
;
3406 #elif defined(CONFIG_VNC)
3407 vnc_display
= "localhost:0,to=99";
3410 display_type
= DT_NONE
;
3415 /* init local displays */
3416 switch (display_type
) {
3419 #if defined(CONFIG_CURSES)
3421 curses_display_init(ds
, full_screen
);
3424 #if defined(CONFIG_SDL)
3426 sdl_display_init(ds
, full_screen
, no_frame
);
3428 #elif defined(CONFIG_COCOA)
3430 cocoa_display_init(ds
, full_screen
);
3437 /* must be after terminal init, SDL library changes signal handlers */
3438 os_setup_signal_handling();
3441 /* init remote displays */
3443 vnc_display_init(ds
);
3444 if (vnc_display_open(ds
, vnc_display
) < 0)
3447 if (show_vnc_port
) {
3448 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3453 if (using_spice
&& !qxl_enabled
) {
3454 qemu_spice_display_init(ds
);
3460 dcl
= ds
->listeners
;
3461 while (dcl
!= NULL
) {
3462 if (dcl
->dpy_refresh
!= NULL
) {
3463 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3464 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3469 text_consoles_set_display(ds
);
3471 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3472 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3477 qdev_machine_creation_done();
3479 if (rom_load_all() != 0) {
3480 fprintf(stderr
, "rom loading failed\n");
3484 /* TODO: once all bus devices are qdevified, this should be done
3485 * when bus is created by qdev.c */
3486 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3487 qemu_run_machine_init_done_notifiers();
3489 qemu_system_reset(VMRESET_SILENT
);
3491 if (load_vmstate(loadvm
) < 0) {
3497 runstate_set(RUN_STATE_INMIGRATE
);
3498 int ret
= qemu_start_incoming_migration(incoming
);
3500 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3504 } else if (autostart
) {