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;
225 unsigned int kvm_shadow_memory
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
233 uint8_t *boot_splash_filedata
;
234 int boot_splash_filedata_size
;
235 uint8_t qemu_extra_params_fw
[2];
237 typedef struct FWBootEntry FWBootEntry
;
240 QTAILQ_ENTRY(FWBootEntry
) link
;
246 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
249 uint64_t node_mem
[MAX_NODES
];
250 uint64_t node_cpumask
[MAX_NODES
];
252 uint8_t qemu_uuid
[16];
254 static QEMUBootSetHandler
*boot_set_handler
;
255 static void *boot_set_opaque
;
257 static NotifierList exit_notifiers
=
258 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
260 static NotifierList machine_init_done_notifiers
=
261 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
263 static int tcg_allowed
= 1;
267 enum xen_mode xen_mode
= XEN_EMULATE
;
268 static int tcg_tb_size
;
270 static int default_serial
= 1;
271 static int default_parallel
= 1;
272 static int default_virtcon
= 1;
273 static int default_monitor
= 1;
274 static int default_floppy
= 1;
275 static int default_cdrom
= 1;
276 static int default_sdcard
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "isa-fdc", .flag
= &default_floppy
},
285 { .driver
= "ide-cd", .flag
= &default_cdrom
},
286 { .driver
= "ide-hd", .flag
= &default_cdrom
},
287 { .driver
= "ide-drive", .flag
= &default_cdrom
},
288 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
289 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
294 static void res_free(void)
296 if (boot_splash_filedata
!= NULL
) {
297 g_free(boot_splash_filedata
);
298 boot_splash_filedata
= NULL
;
302 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
304 const char *driver
= qemu_opt_get(opts
, "driver");
309 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
310 if (strcmp(default_list
[i
].driver
, driver
) != 0)
312 *(default_list
[i
].flag
) = 0;
317 /***********************************************************/
320 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
325 } RunStateTransition
;
327 static const RunStateTransition runstate_transitions_def
[] = {
329 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
331 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
332 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
334 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
335 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
337 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
338 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
340 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
341 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
343 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
344 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
346 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
347 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
348 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
350 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
351 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
353 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
355 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
356 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
357 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
358 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
359 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
360 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
361 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
362 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
363 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
365 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
367 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
368 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
370 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
371 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
373 { RUN_STATE_MAX
, RUN_STATE_MAX
},
376 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
378 bool runstate_check(RunState state
)
380 return current_run_state
== state
;
383 void runstate_init(void)
385 const RunStateTransition
*p
;
387 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
389 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
390 runstate_valid_transitions
[p
->from
][p
->to
] = true;
394 /* This function will abort() on invalid state transitions */
395 void runstate_set(RunState new_state
)
397 assert(new_state
< RUN_STATE_MAX
);
399 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
400 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
401 RunState_lookup
[current_run_state
],
402 RunState_lookup
[new_state
]);
406 current_run_state
= new_state
;
409 int runstate_is_running(void)
411 return runstate_check(RUN_STATE_RUNNING
);
414 StatusInfo
*qmp_query_status(Error
**errp
)
416 StatusInfo
*info
= g_malloc0(sizeof(*info
));
418 info
->running
= runstate_is_running();
419 info
->singlestep
= singlestep
;
420 info
->status
= current_run_state
;
425 /***********************************************************/
426 /* real time host monotonic timer */
428 /***********************************************************/
429 /* host time/date access */
430 void qemu_get_timedate(struct tm
*tm
, int offset
)
437 if (rtc_date_offset
== -1) {
441 ret
= localtime(&ti
);
443 ti
-= rtc_date_offset
;
447 memcpy(tm
, ret
, sizeof(struct tm
));
450 int qemu_timedate_diff(struct tm
*tm
)
454 if (rtc_date_offset
== -1)
456 seconds
= mktimegm(tm
);
459 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
460 seconds
= mktime(&tmp
);
463 seconds
= mktimegm(tm
) + rtc_date_offset
;
465 return seconds
- time(NULL
);
468 void rtc_change_mon_event(struct tm
*tm
)
472 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
473 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
474 qobject_decref(data
);
477 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
479 time_t rtc_start_date
;
482 if (!strcmp(startdate
, "now") && legacy
) {
483 rtc_date_offset
= -1;
485 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
493 } else if (sscanf(startdate
, "%d-%d-%d",
505 rtc_start_date
= mktimegm(&tm
);
506 if (rtc_start_date
== -1) {
508 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
509 "'2006-06-17T16:01:21' or '2006-06-17'\n");
512 rtc_date_offset
= time(NULL
) - rtc_start_date
;
516 static void configure_rtc(QemuOpts
*opts
)
520 value
= qemu_opt_get(opts
, "base");
522 if (!strcmp(value
, "utc")) {
524 } else if (!strcmp(value
, "localtime")) {
527 configure_rtc_date_offset(value
, 0);
530 value
= qemu_opt_get(opts
, "clock");
532 if (!strcmp(value
, "host")) {
533 rtc_clock
= host_clock
;
534 } else if (!strcmp(value
, "vm")) {
535 rtc_clock
= vm_clock
;
537 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
541 value
= qemu_opt_get(opts
, "driftfix");
543 if (!strcmp(value
, "slew")) {
545 } else if (!strcmp(value
, "none")) {
548 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
554 /***********************************************************/
555 /* Bluetooth support */
558 static struct HCIInfo
*hci_table
[MAX_NICS
];
560 static struct bt_vlan_s
{
561 struct bt_scatternet_s net
;
563 struct bt_vlan_s
*next
;
566 /* find or alloc a new bluetooth "VLAN" */
567 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
569 struct bt_vlan_s
**pvlan
, *vlan
;
570 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
574 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
576 pvlan
= &first_bt_vlan
;
577 while (*pvlan
!= NULL
)
578 pvlan
= &(*pvlan
)->next
;
583 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
587 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
592 static struct HCIInfo null_hci
= {
593 .cmd_send
= null_hci_send
,
594 .sco_send
= null_hci_send
,
595 .acl_send
= null_hci_send
,
596 .bdaddr_set
= null_hci_addr_set
,
599 struct HCIInfo
*qemu_next_hci(void)
601 if (cur_hci
== nb_hcis
)
604 return hci_table
[cur_hci
++];
607 static struct HCIInfo
*hci_init(const char *str
)
610 struct bt_scatternet_s
*vlan
= 0;
612 if (!strcmp(str
, "null"))
615 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
617 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
618 else if (!strncmp(str
, "hci", 3)) {
621 if (!strncmp(str
+ 3, ",vlan=", 6)) {
622 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
627 vlan
= qemu_find_bt_vlan(0);
629 return bt_new_hci(vlan
);
632 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
637 static int bt_hci_parse(const char *str
)
642 if (nb_hcis
>= MAX_NICS
) {
643 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
656 bdaddr
.b
[5] = 0x56 + nb_hcis
;
657 hci
->bdaddr_set(hci
, bdaddr
.b
);
659 hci_table
[nb_hcis
++] = hci
;
664 static void bt_vhci_add(int vlan_id
)
666 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
669 fprintf(stderr
, "qemu: warning: adding a VHCI to "
670 "an empty scatternet %i\n", vlan_id
);
672 bt_vhci_init(bt_new_hci(vlan
));
675 static struct bt_device_s
*bt_device_add(const char *opt
)
677 struct bt_scatternet_s
*vlan
;
679 char *endp
= strstr(opt
, ",vlan=");
680 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
683 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
686 vlan_id
= strtol(endp
+ 6, &endp
, 0);
688 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
693 vlan
= qemu_find_bt_vlan(vlan_id
);
696 fprintf(stderr
, "qemu: warning: adding a slave device to "
697 "an empty scatternet %i\n", vlan_id
);
699 if (!strcmp(devname
, "keyboard"))
700 return bt_keyboard_init(vlan
);
702 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
706 static int bt_parse(const char *opt
)
708 const char *endp
, *p
;
711 if (strstart(opt
, "hci", &endp
)) {
712 if (!*endp
|| *endp
== ',') {
714 if (!strstart(endp
, ",vlan=", 0))
717 return bt_hci_parse(opt
);
719 } else if (strstart(opt
, "vhci", &endp
)) {
720 if (!*endp
|| *endp
== ',') {
722 if (strstart(endp
, ",vlan=", &p
)) {
723 vlan
= strtol(p
, (char **) &endp
, 0);
725 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
729 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
738 } else if (strstart(opt
, "device:", &endp
))
739 return !bt_device_add(endp
);
741 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
745 /***********************************************************/
746 /* QEMU Block devices */
748 #define HD_OPTS "media=disk"
749 #define CDROM_OPTS "media=cdrom"
751 #define PFLASH_OPTS ""
755 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
757 int *use_scsi
= opaque
;
759 return drive_init(opts
, *use_scsi
) == NULL
;
762 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
764 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
765 qemu_opt_set(opts
, "snapshot", "on");
770 static void default_drive(int enable
, int snapshot
, int use_scsi
,
771 BlockInterfaceType type
, int index
,
776 if (type
== IF_DEFAULT
) {
777 type
= use_scsi
? IF_SCSI
: IF_IDE
;
780 if (!enable
|| drive_get_by_index(type
, index
)) {
784 opts
= drive_add(type
, index
, NULL
, optstr
);
786 drive_enable_snapshot(opts
, NULL
);
788 if (!drive_init(opts
, use_scsi
)) {
793 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
795 boot_set_handler
= func
;
796 boot_set_opaque
= opaque
;
799 int qemu_boot_set(const char *boot_devices
)
801 if (!boot_set_handler
) {
804 return boot_set_handler(boot_set_opaque
, boot_devices
);
807 static void validate_bootdevices(char *devices
)
809 /* We just do some generic consistency checks */
813 for (p
= devices
; *p
!= '\0'; p
++) {
814 /* Allowed boot devices are:
815 * a-b: floppy disk drives
816 * c-f: IDE disk drives
817 * g-m: machine implementation dependent drives
818 * n-p: network devices
819 * It's up to each machine implementation to check if the given boot
820 * devices match the actual hardware implementation and firmware
823 if (*p
< 'a' || *p
> 'p') {
824 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
827 if (bitmap
& (1 << (*p
- 'a'))) {
828 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
831 bitmap
|= 1 << (*p
- 'a');
835 static void restore_boot_devices(void *opaque
)
837 char *standard_boot_devices
= opaque
;
838 static int first
= 1;
840 /* Restore boot order and remove ourselves after the first boot */
846 qemu_boot_set(standard_boot_devices
);
848 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
849 g_free(standard_boot_devices
);
852 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
855 FWBootEntry
*node
, *i
;
861 assert(dev
!= NULL
|| suffix
!= NULL
);
863 node
= g_malloc0(sizeof(FWBootEntry
));
864 node
->bootindex
= bootindex
;
865 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
868 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
869 if (i
->bootindex
== bootindex
) {
870 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
872 } else if (i
->bootindex
< bootindex
) {
875 QTAILQ_INSERT_BEFORE(i
, node
, link
);
878 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
882 * This function returns null terminated string that consist of new line
883 * separated device paths.
885 * memory pointed by "size" is assigned total length of the array in bytes
888 char *get_boot_devices_list(uint32_t *size
)
894 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
895 char *devpath
= NULL
, *bootpath
;
899 devpath
= qdev_get_fw_dev_path(i
->dev
);
903 if (i
->suffix
&& devpath
) {
904 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
906 bootpath
= g_malloc(bootpathlen
);
907 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
909 } else if (devpath
) {
913 bootpath
= g_strdup(i
->suffix
);
917 list
[total
-1] = '\n';
919 len
= strlen(bootpath
) + 1;
920 list
= g_realloc(list
, total
+ len
);
921 memcpy(&list
[total
], bootpath
, len
);
931 static void numa_add(const char *optarg
)
935 unsigned long long value
, endvalue
;
938 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
939 if (!strcmp(option
, "node")) {
940 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
941 nodenr
= nb_numa_nodes
;
943 nodenr
= strtoull(option
, NULL
, 10);
946 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
947 node_mem
[nodenr
] = 0;
950 sval
= strtosz(option
, &endptr
);
951 if (sval
< 0 || *endptr
) {
952 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
955 node_mem
[nodenr
] = sval
;
957 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
958 node_cpumask
[nodenr
] = 0;
960 value
= strtoull(option
, &endptr
, 10);
963 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
965 if (*endptr
== '-') {
966 endvalue
= strtoull(endptr
+1, &endptr
, 10);
967 if (endvalue
>= 63) {
970 "only 63 CPUs in NUMA mode supported.\n");
972 value
= (2ULL << endvalue
) - (1ULL << value
);
974 value
= 1ULL << value
;
977 node_cpumask
[nodenr
] = value
;
984 static void smp_parse(const char *optarg
)
986 int smp
, sockets
= 0, threads
= 0, cores
= 0;
990 smp
= strtoul(optarg
, &endptr
, 10);
991 if (endptr
!= optarg
) {
992 if (*endptr
== ',') {
996 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
997 sockets
= strtoull(option
, NULL
, 10);
998 if (get_param_value(option
, 128, "cores", endptr
) != 0)
999 cores
= strtoull(option
, NULL
, 10);
1000 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1001 threads
= strtoull(option
, NULL
, 10);
1002 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1003 max_cpus
= strtoull(option
, NULL
, 10);
1005 /* compute missing values, prefer sockets over cores over threads */
1006 if (smp
== 0 || sockets
== 0) {
1007 sockets
= sockets
> 0 ? sockets
: 1;
1008 cores
= cores
> 0 ? cores
: 1;
1009 threads
= threads
> 0 ? threads
: 1;
1011 smp
= cores
* threads
* sockets
;
1015 threads
= threads
> 0 ? threads
: 1;
1016 cores
= smp
/ (sockets
* threads
);
1018 threads
= smp
/ (cores
* sockets
);
1022 smp_cores
= cores
> 0 ? cores
: 1;
1023 smp_threads
= threads
> 0 ? threads
: 1;
1025 max_cpus
= smp_cpus
;
1028 /***********************************************************/
1031 static int usb_device_add(const char *devname
)
1034 USBDevice
*dev
= NULL
;
1039 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1040 dev
= usbdevice_create(devname
);
1044 /* the other ones */
1045 #ifndef CONFIG_LINUX
1046 /* only the linux version is qdev-ified, usb-bsd still needs this */
1047 if (strstart(devname
, "host:", &p
)) {
1048 dev
= usb_host_device_open(p
);
1051 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1052 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1053 bt_new_hci(qemu_find_bt_vlan(0)));
1064 static int usb_device_del(const char *devname
)
1069 if (strstart(devname
, "host:", &p
))
1070 return usb_host_device_close(p
);
1075 p
= strchr(devname
, '.');
1078 bus_num
= strtoul(devname
, NULL
, 0);
1079 addr
= strtoul(p
+ 1, NULL
, 0);
1081 return usb_device_delete_addr(bus_num
, addr
);
1084 static int usb_parse(const char *cmdline
)
1087 r
= usb_device_add(cmdline
);
1089 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1094 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1096 const char *devname
= qdict_get_str(qdict
, "devname");
1097 if (usb_device_add(devname
) < 0) {
1098 error_report("could not add USB device '%s'", devname
);
1102 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1104 const char *devname
= qdict_get_str(qdict
, "devname");
1105 if (usb_device_del(devname
) < 0) {
1106 error_report("could not delete USB device '%s'", devname
);
1110 /***********************************************************/
1111 /* PCMCIA/Cardbus */
1113 static struct pcmcia_socket_entry_s
{
1114 PCMCIASocket
*socket
;
1115 struct pcmcia_socket_entry_s
*next
;
1116 } *pcmcia_sockets
= 0;
1118 void pcmcia_socket_register(PCMCIASocket
*socket
)
1120 struct pcmcia_socket_entry_s
*entry
;
1122 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1123 entry
->socket
= socket
;
1124 entry
->next
= pcmcia_sockets
;
1125 pcmcia_sockets
= entry
;
1128 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1130 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1132 ptr
= &pcmcia_sockets
;
1133 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1134 if (entry
->socket
== socket
) {
1140 void pcmcia_info(Monitor
*mon
)
1142 struct pcmcia_socket_entry_s
*iter
;
1144 if (!pcmcia_sockets
)
1145 monitor_printf(mon
, "No PCMCIA sockets\n");
1147 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1148 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1149 iter
->socket
->attached
? iter
->socket
->card_string
:
1153 /***********************************************************/
1154 /* machine registration */
1156 static QEMUMachine
*first_machine
= NULL
;
1157 QEMUMachine
*current_machine
= NULL
;
1159 int qemu_register_machine(QEMUMachine
*m
)
1162 pm
= &first_machine
;
1170 static QEMUMachine
*find_machine(const char *name
)
1174 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1175 if (!strcmp(m
->name
, name
))
1177 if (m
->alias
&& !strcmp(m
->alias
, name
))
1183 static QEMUMachine
*find_default_machine(void)
1187 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1188 if (m
->is_default
) {
1195 /***********************************************************/
1196 /* main execution loop */
1198 static void gui_update(void *opaque
)
1200 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1201 DisplayState
*ds
= opaque
;
1202 DisplayChangeListener
*dcl
= ds
->listeners
;
1206 while (dcl
!= NULL
) {
1207 if (dcl
->gui_timer_interval
&&
1208 dcl
->gui_timer_interval
< interval
)
1209 interval
= dcl
->gui_timer_interval
;
1212 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1215 struct vm_change_state_entry
{
1216 VMChangeStateHandler
*cb
;
1218 QLIST_ENTRY (vm_change_state_entry
) entries
;
1221 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1223 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1226 VMChangeStateEntry
*e
;
1228 e
= g_malloc0(sizeof (*e
));
1232 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1236 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1238 QLIST_REMOVE (e
, entries
);
1242 void vm_state_notify(int running
, RunState state
)
1244 VMChangeStateEntry
*e
;
1246 trace_vm_state_notify(running
, state
);
1248 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1249 e
->cb(e
->opaque
, running
, state
);
1255 if (!runstate_is_running()) {
1257 runstate_set(RUN_STATE_RUNNING
);
1258 vm_state_notify(1, RUN_STATE_RUNNING
);
1260 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1264 /* reset/shutdown handler */
1266 typedef struct QEMUResetEntry
{
1267 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1268 QEMUResetHandler
*func
;
1272 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1273 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1274 static int reset_requested
;
1275 static int shutdown_requested
, shutdown_signal
= -1;
1276 static pid_t shutdown_pid
;
1277 static int powerdown_requested
;
1278 static int debug_requested
;
1279 static RunState vmstop_requested
= RUN_STATE_MAX
;
1281 int qemu_shutdown_requested_get(void)
1283 return shutdown_requested
;
1286 int qemu_reset_requested_get(void)
1288 return reset_requested
;
1291 int qemu_shutdown_requested(void)
1293 int r
= shutdown_requested
;
1294 shutdown_requested
= 0;
1298 void qemu_kill_report(void)
1300 if (shutdown_signal
!= -1) {
1301 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1302 if (shutdown_pid
== 0) {
1303 /* This happens for eg ^C at the terminal, so it's worth
1304 * avoiding printing an odd message in that case.
1306 fputc('\n', stderr
);
1308 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1310 shutdown_signal
= -1;
1314 int qemu_reset_requested(void)
1316 int r
= reset_requested
;
1317 reset_requested
= 0;
1321 int qemu_powerdown_requested(void)
1323 int r
= powerdown_requested
;
1324 powerdown_requested
= 0;
1328 static int qemu_debug_requested(void)
1330 int r
= debug_requested
;
1331 debug_requested
= 0;
1335 /* We use RUN_STATE_MAX but any invalid value will do */
1336 static bool qemu_vmstop_requested(RunState
*r
)
1338 if (vmstop_requested
< RUN_STATE_MAX
) {
1339 *r
= vmstop_requested
;
1340 vmstop_requested
= RUN_STATE_MAX
;
1347 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1349 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1352 re
->opaque
= opaque
;
1353 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1356 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1360 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1361 if (re
->func
== func
&& re
->opaque
== opaque
) {
1362 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1369 void qemu_system_reset(bool report
)
1371 QEMUResetEntry
*re
, *nre
;
1373 /* reset all devices */
1374 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1375 re
->func(re
->opaque
);
1378 monitor_protocol_event(QEVENT_RESET
, NULL
);
1380 cpu_synchronize_all_post_reset();
1383 void qemu_system_reset_request(void)
1386 shutdown_requested
= 1;
1388 reset_requested
= 1;
1391 qemu_notify_event();
1394 void qemu_system_killed(int signal
, pid_t pid
)
1396 shutdown_signal
= signal
;
1399 qemu_system_shutdown_request();
1402 void qemu_system_shutdown_request(void)
1404 shutdown_requested
= 1;
1405 qemu_notify_event();
1408 void qemu_system_powerdown_request(void)
1410 powerdown_requested
= 1;
1411 qemu_notify_event();
1414 void qemu_system_debug_request(void)
1416 debug_requested
= 1;
1417 qemu_notify_event();
1420 void qemu_system_vmstop_request(RunState state
)
1422 vmstop_requested
= state
;
1423 qemu_notify_event();
1426 qemu_irq qemu_system_powerdown
;
1428 static bool main_loop_should_exit(void)
1431 if (qemu_debug_requested()) {
1432 vm_stop(RUN_STATE_DEBUG
);
1434 if (qemu_shutdown_requested()) {
1436 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1438 vm_stop(RUN_STATE_SHUTDOWN
);
1443 if (qemu_reset_requested()) {
1445 cpu_synchronize_all_states();
1446 qemu_system_reset(VMRESET_REPORT
);
1448 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1449 runstate_check(RUN_STATE_SHUTDOWN
)) {
1450 runstate_set(RUN_STATE_PAUSED
);
1453 if (qemu_powerdown_requested()) {
1454 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1455 qemu_irq_raise(qemu_system_powerdown
);
1457 if (qemu_vmstop_requested(&r
)) {
1463 static void main_loop(void)
1467 #ifdef CONFIG_PROFILER
1471 nonblocking
= !kvm_enabled() && last_io
> 0;
1472 #ifdef CONFIG_PROFILER
1473 ti
= profile_getclock();
1475 last_io
= main_loop_wait(nonblocking
);
1476 #ifdef CONFIG_PROFILER
1477 dev_time
+= profile_getclock() - ti
;
1479 } while (!main_loop_should_exit());
1482 static void version(void)
1484 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1487 static void help(int exitcode
)
1490 printf("usage: %s [options] [disk_image]\n\n"
1491 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1492 error_get_progname());
1494 #define QEMU_OPTIONS_GENERATE_HELP
1495 #include "qemu-options-wrapper.h"
1497 printf("\nDuring emulation, the following keys are useful:\n"
1498 "ctrl-alt-f toggle full screen\n"
1499 "ctrl-alt-n switch to virtual console 'n'\n"
1500 "ctrl-alt toggle mouse and keyboard grab\n"
1502 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1507 #define HAS_ARG 0x0001
1509 typedef struct QEMUOption
{
1516 static const QEMUOption qemu_options
[] = {
1517 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1518 #define QEMU_OPTIONS_GENERATE_OPTIONS
1519 #include "qemu-options-wrapper.h"
1523 static bool vga_available(void)
1525 return qdev_exists("VGA") || qdev_exists("isa-vga");
1528 static bool cirrus_vga_available(void)
1530 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1533 static bool vmware_vga_available(void)
1535 return qdev_exists("vmware-svga");
1538 static void select_vgahw (const char *p
)
1542 vga_interface_type
= VGA_NONE
;
1543 if (strstart(p
, "std", &opts
)) {
1544 if (vga_available()) {
1545 vga_interface_type
= VGA_STD
;
1547 fprintf(stderr
, "Error: standard VGA not available\n");
1550 } else if (strstart(p
, "cirrus", &opts
)) {
1551 if (cirrus_vga_available()) {
1552 vga_interface_type
= VGA_CIRRUS
;
1554 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1557 } else if (strstart(p
, "vmware", &opts
)) {
1558 if (vmware_vga_available()) {
1559 vga_interface_type
= VGA_VMWARE
;
1561 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1564 } else if (strstart(p
, "xenfb", &opts
)) {
1565 vga_interface_type
= VGA_XENFB
;
1566 } else if (strstart(p
, "qxl", &opts
)) {
1567 vga_interface_type
= VGA_QXL
;
1568 } else if (!strstart(p
, "none", &opts
)) {
1570 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1574 const char *nextopt
;
1576 if (strstart(opts
, ",retrace=", &nextopt
)) {
1578 if (strstart(opts
, "dumb", &nextopt
))
1579 vga_retrace_method
= VGA_RETRACE_DUMB
;
1580 else if (strstart(opts
, "precise", &nextopt
))
1581 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1582 else goto invalid_vga
;
1583 } else goto invalid_vga
;
1588 static DisplayType
select_display(const char *p
)
1591 DisplayType display
= DT_DEFAULT
;
1593 if (strstart(p
, "sdl", &opts
)) {
1597 const char *nextopt
;
1599 if (strstart(opts
, ",frame=", &nextopt
)) {
1601 if (strstart(opts
, "on", &nextopt
)) {
1603 } else if (strstart(opts
, "off", &nextopt
)) {
1606 goto invalid_sdl_args
;
1608 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1610 if (strstart(opts
, "on", &nextopt
)) {
1612 } else if (strstart(opts
, "off", &nextopt
)) {
1615 goto invalid_sdl_args
;
1617 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1619 if (strstart(opts
, "on", &nextopt
)) {
1621 } else if (strstart(opts
, "off", &nextopt
)) {
1624 goto invalid_sdl_args
;
1626 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1628 if (strstart(opts
, "on", &nextopt
)) {
1630 } else if (strstart(opts
, "off", &nextopt
)) {
1633 goto invalid_sdl_args
;
1637 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1643 fprintf(stderr
, "SDL support is disabled\n");
1646 } else if (strstart(p
, "vnc", &opts
)) {
1651 const char *nextopt
;
1653 if (strstart(opts
, "=", &nextopt
)) {
1654 vnc_display
= nextopt
;
1658 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1662 fprintf(stderr
, "VNC support is disabled\n");
1665 } else if (strstart(p
, "curses", &opts
)) {
1666 #ifdef CONFIG_CURSES
1667 display
= DT_CURSES
;
1669 fprintf(stderr
, "Curses support is disabled\n");
1672 } else if (strstart(p
, "none", &opts
)) {
1675 fprintf(stderr
, "Unknown display type: %s\n", p
);
1682 static int balloon_parse(const char *arg
)
1686 if (strcmp(arg
, "none") == 0) {
1690 if (!strncmp(arg
, "virtio", 6)) {
1691 if (arg
[6] == ',') {
1692 /* have params -> parse them */
1693 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1697 /* create empty opts */
1698 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1700 qemu_opt_set(opts
, "driver", "virtio-balloon");
1707 char *qemu_find_file(int type
, const char *name
)
1713 /* If name contains path separators then try it as a straight path. */
1714 if ((strchr(name
, '/') || strchr(name
, '\\'))
1715 && access(name
, R_OK
) == 0) {
1716 return g_strdup(name
);
1719 case QEMU_FILE_TYPE_BIOS
:
1722 case QEMU_FILE_TYPE_KEYMAP
:
1723 subdir
= "keymaps/";
1728 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1729 buf
= g_malloc0(len
);
1730 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1731 if (access(buf
, R_OK
)) {
1738 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1740 return qdev_device_help(opts
);
1743 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1747 dev
= qdev_device_add(opts
);
1753 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1755 CharDriverState
*chr
;
1757 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1763 #ifdef CONFIG_VIRTFS
1764 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1767 ret
= qemu_fsdev_add(opts
);
1773 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1775 CharDriverState
*chr
;
1776 const char *chardev
;
1780 mode
= qemu_opt_get(opts
, "mode");
1784 if (strcmp(mode
, "readline") == 0) {
1785 flags
= MONITOR_USE_READLINE
;
1786 } else if (strcmp(mode
, "control") == 0) {
1787 flags
= MONITOR_USE_CONTROL
;
1789 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1793 if (qemu_opt_get_bool(opts
, "pretty", 0))
1794 flags
|= MONITOR_USE_PRETTY
;
1796 if (qemu_opt_get_bool(opts
, "default", 0))
1797 flags
|= MONITOR_IS_DEFAULT
;
1799 chardev
= qemu_opt_get(opts
, "chardev");
1800 chr
= qemu_chr_find(chardev
);
1802 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1806 monitor_init(chr
, flags
);
1810 static void monitor_parse(const char *optarg
, const char *mode
)
1812 static int monitor_device_index
= 0;
1818 if (strstart(optarg
, "chardev:", &p
)) {
1819 snprintf(label
, sizeof(label
), "%s", p
);
1821 snprintf(label
, sizeof(label
), "compat_monitor%d",
1822 monitor_device_index
);
1823 if (monitor_device_index
== 0) {
1826 opts
= qemu_chr_parse_compat(label
, optarg
);
1828 fprintf(stderr
, "parse error: %s\n", optarg
);
1833 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1835 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1838 qemu_opt_set(opts
, "mode", mode
);
1839 qemu_opt_set(opts
, "chardev", label
);
1841 qemu_opt_set(opts
, "default", "on");
1842 monitor_device_index
++;
1845 struct device_config
{
1847 DEV_USB
, /* -usbdevice */
1849 DEV_SERIAL
, /* -serial */
1850 DEV_PARALLEL
, /* -parallel */
1851 DEV_VIRTCON
, /* -virtioconsole */
1852 DEV_DEBUGCON
, /* -debugcon */
1854 const char *cmdline
;
1855 QTAILQ_ENTRY(device_config
) next
;
1857 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1859 static void add_device_config(int type
, const char *cmdline
)
1861 struct device_config
*conf
;
1863 conf
= g_malloc0(sizeof(*conf
));
1865 conf
->cmdline
= cmdline
;
1866 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1869 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1871 struct device_config
*conf
;
1874 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1875 if (conf
->type
!= type
)
1877 rc
= func(conf
->cmdline
);
1884 static int serial_parse(const char *devname
)
1886 static int index
= 0;
1889 if (strcmp(devname
, "none") == 0)
1891 if (index
== MAX_SERIAL_PORTS
) {
1892 fprintf(stderr
, "qemu: too many serial ports\n");
1895 snprintf(label
, sizeof(label
), "serial%d", index
);
1896 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1897 if (!serial_hds
[index
]) {
1898 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1899 devname
, strerror(errno
));
1906 static int parallel_parse(const char *devname
)
1908 static int index
= 0;
1911 if (strcmp(devname
, "none") == 0)
1913 if (index
== MAX_PARALLEL_PORTS
) {
1914 fprintf(stderr
, "qemu: too many parallel ports\n");
1917 snprintf(label
, sizeof(label
), "parallel%d", index
);
1918 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1919 if (!parallel_hds
[index
]) {
1920 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1921 devname
, strerror(errno
));
1928 static int virtcon_parse(const char *devname
)
1930 QemuOptsList
*device
= qemu_find_opts("device");
1931 static int index
= 0;
1933 QemuOpts
*bus_opts
, *dev_opts
;
1935 if (strcmp(devname
, "none") == 0)
1937 if (index
== MAX_VIRTIO_CONSOLES
) {
1938 fprintf(stderr
, "qemu: too many virtio consoles\n");
1942 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1943 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
1945 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1946 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1948 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1949 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1950 if (!virtcon_hds
[index
]) {
1951 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1952 devname
, strerror(errno
));
1955 qemu_opt_set(dev_opts
, "chardev", label
);
1961 static int debugcon_parse(const char *devname
)
1965 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
1968 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1970 fprintf(stderr
, "qemu: already have a debugcon device\n");
1973 qemu_opt_set(opts
, "driver", "isa-debugcon");
1974 qemu_opt_set(opts
, "chardev", "debugcon");
1978 static QEMUMachine
*machine_parse(const char *name
)
1980 QEMUMachine
*m
, *machine
= NULL
;
1983 machine
= find_machine(name
);
1988 printf("Supported machines are:\n");
1989 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1991 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
1993 printf("%-10s %s%s\n", m
->name
, m
->desc
,
1994 m
->is_default
? " (default)" : "");
1996 exit(!name
|| *name
!= '?');
1999 static int tcg_init(void)
2001 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2006 const char *opt_name
;
2008 int (*available
)(void);
2012 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2013 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2014 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2017 static int configure_accelerator(void)
2019 const char *p
= NULL
;
2022 bool accel_initalised
= 0;
2023 bool init_failed
= 0;
2025 QemuOptsList
*list
= qemu_find_opts("machine");
2026 if (!QTAILQ_EMPTY(&list
->head
)) {
2027 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2031 #ifdef CONFIG_KVM_OPTIONS
2032 /* Use the default "accelerator", kvm */
2035 /* Use the default "accelerator", tcg */
2040 while (!accel_initalised
&& *p
!= '\0') {
2044 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2045 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2046 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2047 *(accel_list
[i
].allowed
) = 1;
2048 ret
= accel_list
[i
].init();
2051 if (!accel_list
[i
].available()) {
2052 printf("%s not supported for this target\n",
2053 accel_list
[i
].name
);
2055 fprintf(stderr
, "failed to initialize %s: %s\n",
2059 *(accel_list
[i
].allowed
) = 0;
2061 accel_initalised
= 1;
2066 if (i
== ARRAY_SIZE(accel_list
)) {
2067 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2071 if (!accel_initalised
) {
2072 fprintf(stderr
, "No accelerator found!\n");
2077 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2080 return !accel_initalised
;
2083 void qemu_add_exit_notifier(Notifier
*notify
)
2085 notifier_list_add(&exit_notifiers
, notify
);
2088 void qemu_remove_exit_notifier(Notifier
*notify
)
2090 notifier_list_remove(&exit_notifiers
, notify
);
2093 static void qemu_run_exit_notifiers(void)
2095 notifier_list_notify(&exit_notifiers
, NULL
);
2098 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2100 notifier_list_add(&machine_init_done_notifiers
, notify
);
2103 static void qemu_run_machine_init_done_notifiers(void)
2105 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2108 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2109 const char **poptarg
, int *poptind
)
2111 const QEMUOption
*popt
;
2112 int optind
= *poptind
;
2113 char *r
= argv
[optind
];
2116 loc_set_cmdline(argv
, optind
, 1);
2118 /* Treat --foo the same as -foo. */
2121 popt
= qemu_options
;
2124 error_report("invalid option");
2127 if (!strcmp(popt
->name
, r
+ 1))
2131 if (popt
->flags
& HAS_ARG
) {
2132 if (optind
>= argc
) {
2133 error_report("requires an argument");
2136 optarg
= argv
[optind
++];
2137 loc_set_cmdline(argv
, optind
- 2, 2);
2148 static gpointer
malloc_and_trace(gsize n_bytes
)
2150 void *ptr
= malloc(n_bytes
);
2151 trace_g_malloc(n_bytes
, ptr
);
2155 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2157 void *ptr
= realloc(mem
, n_bytes
);
2158 trace_g_realloc(mem
, n_bytes
, ptr
);
2162 static void free_and_trace(gpointer mem
)
2168 #ifdef CONFIG_KVM_OPTIONS
2169 int kvm_irqchip
= 1;
2170 int kvm_pit_reinject
= 1;
2173 int main(int argc
, char **argv
, char **envp
)
2175 const char *gdbstub_dev
= NULL
;
2177 int snapshot
, linux_boot
;
2178 const char *icount_option
= NULL
;
2179 const char *initrd_filename
;
2180 const char *kernel_filename
, *kernel_cmdline
;
2181 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2183 DisplayChangeListener
*dcl
;
2184 int cyls
, heads
, secs
, translation
;
2185 QemuOpts
*hda_opts
= NULL
, *opts
;
2186 QemuOptsList
*olist
;
2189 const char *loadvm
= NULL
;
2190 QEMUMachine
*machine
;
2191 const char *cpu_model
;
2192 const char *vga_model
= NULL
;
2193 const char *pid_file
= NULL
;
2194 const char *incoming
= NULL
;
2196 int show_vnc_port
= 0;
2199 const char *log_mask
= NULL
;
2200 const char *log_file
= NULL
;
2201 GMemVTable mem_trace
= {
2202 .malloc
= malloc_and_trace
,
2203 .realloc
= realloc_and_trace
,
2204 .free
= free_and_trace
,
2206 const char *trace_events
= NULL
;
2207 const char *trace_file
= NULL
;
2209 atexit(qemu_run_exit_notifiers
);
2210 error_set_progname(argv
[0]);
2212 g_mem_set_vtable(&mem_trace
);
2213 if (!g_thread_supported()) {
2214 #if !GLIB_CHECK_VERSION(2, 31, 0)
2215 g_thread_init(NULL
);
2217 fprintf(stderr
, "glib threading failed to initialize.\n");
2225 rtc_clock
= host_clock
;
2227 qemu_cache_utils_init(envp
);
2229 QLIST_INIT (&vm_change_state_head
);
2230 os_setup_early_signal_handling();
2232 module_call_init(MODULE_INIT_MACHINE
);
2233 machine
= find_default_machine();
2235 initrd_filename
= NULL
;
2238 kernel_filename
= NULL
;
2239 kernel_cmdline
= "";
2240 cyls
= heads
= secs
= 0;
2241 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2243 for (i
= 0; i
< MAX_NODES
; i
++) {
2245 node_cpumask
[i
] = 0;
2253 /* first pass of option parsing */
2255 while (optind
< argc
) {
2256 if (argv
[optind
][0] != '-') {
2261 const QEMUOption
*popt
;
2263 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2264 switch (popt
->index
) {
2265 case QEMU_OPTION_nodefconfig
:
2275 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2276 if (ret
< 0 && ret
!= -ENOENT
) {
2280 ret
= qemu_read_config_file(arch_config_name
);
2281 if (ret
< 0 && ret
!= -ENOENT
) {
2287 /* second pass of option parsing */
2292 if (argv
[optind
][0] != '-') {
2293 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2295 const QEMUOption
*popt
;
2297 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2298 if (!(popt
->arch_mask
& arch_type
)) {
2299 printf("Option %s not supported for this target\n", popt
->name
);
2302 switch(popt
->index
) {
2304 machine
= machine_parse(optarg
);
2306 case QEMU_OPTION_cpu
:
2307 /* hw initialization will check this */
2308 if (*optarg
== '?') {
2309 list_cpus(stdout
, &fprintf
, optarg
);
2315 case QEMU_OPTION_initrd
:
2316 initrd_filename
= optarg
;
2318 case QEMU_OPTION_hda
:
2322 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2324 snprintf(buf
, sizeof(buf
),
2325 "%s,cyls=%d,heads=%d,secs=%d%s",
2326 HD_OPTS
, cyls
, heads
, secs
,
2327 translation
== BIOS_ATA_TRANSLATION_LBA
?
2329 translation
== BIOS_ATA_TRANSLATION_NONE
?
2330 ",trans=none" : "");
2331 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2334 case QEMU_OPTION_hdb
:
2335 case QEMU_OPTION_hdc
:
2336 case QEMU_OPTION_hdd
:
2337 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2340 case QEMU_OPTION_drive
:
2341 if (drive_def(optarg
) == NULL
) {
2345 case QEMU_OPTION_set
:
2346 if (qemu_set_option(optarg
) != 0)
2349 case QEMU_OPTION_global
:
2350 if (qemu_global_option(optarg
) != 0)
2353 case QEMU_OPTION_mtdblock
:
2354 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2356 case QEMU_OPTION_sd
:
2357 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2359 case QEMU_OPTION_pflash
:
2360 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2362 case QEMU_OPTION_snapshot
:
2365 case QEMU_OPTION_hdachs
:
2369 cyls
= strtol(p
, (char **)&p
, 0);
2370 if (cyls
< 1 || cyls
> 16383)
2375 heads
= strtol(p
, (char **)&p
, 0);
2376 if (heads
< 1 || heads
> 16)
2381 secs
= strtol(p
, (char **)&p
, 0);
2382 if (secs
< 1 || secs
> 63)
2386 if (!strcmp(p
, "none"))
2387 translation
= BIOS_ATA_TRANSLATION_NONE
;
2388 else if (!strcmp(p
, "lba"))
2389 translation
= BIOS_ATA_TRANSLATION_LBA
;
2390 else if (!strcmp(p
, "auto"))
2391 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2394 } else if (*p
!= '\0') {
2396 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2399 if (hda_opts
!= NULL
) {
2401 snprintf(num
, sizeof(num
), "%d", cyls
);
2402 qemu_opt_set(hda_opts
, "cyls", num
);
2403 snprintf(num
, sizeof(num
), "%d", heads
);
2404 qemu_opt_set(hda_opts
, "heads", num
);
2405 snprintf(num
, sizeof(num
), "%d", secs
);
2406 qemu_opt_set(hda_opts
, "secs", num
);
2407 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2408 qemu_opt_set(hda_opts
, "trans", "lba");
2409 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2410 qemu_opt_set(hda_opts
, "trans", "none");
2414 case QEMU_OPTION_numa
:
2415 if (nb_numa_nodes
>= MAX_NODES
) {
2416 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2421 case QEMU_OPTION_display
:
2422 display_type
= select_display(optarg
);
2424 case QEMU_OPTION_nographic
:
2425 display_type
= DT_NOGRAPHIC
;
2427 case QEMU_OPTION_curses
:
2428 #ifdef CONFIG_CURSES
2429 display_type
= DT_CURSES
;
2431 fprintf(stderr
, "Curses support is disabled\n");
2435 case QEMU_OPTION_portrait
:
2436 graphic_rotate
= 90;
2438 case QEMU_OPTION_rotate
:
2439 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2440 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2441 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2443 "qemu: only 90, 180, 270 deg rotation is available\n");
2447 case QEMU_OPTION_kernel
:
2448 kernel_filename
= optarg
;
2450 case QEMU_OPTION_append
:
2451 kernel_cmdline
= optarg
;
2453 case QEMU_OPTION_cdrom
:
2454 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2456 case QEMU_OPTION_boot
:
2458 static const char * const params
[] = {
2459 "order", "once", "menu",
2460 "splash", "splash-time", NULL
2462 char buf
[sizeof(boot_devices
)];
2463 char *standard_boot_devices
;
2466 if (!strchr(optarg
, '=')) {
2468 pstrcpy(buf
, sizeof(buf
), optarg
);
2469 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2471 "qemu: unknown boot parameter '%s' in '%s'\n",
2477 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2478 validate_bootdevices(buf
);
2479 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2482 if (get_param_value(buf
, sizeof(buf
),
2484 validate_bootdevices(buf
);
2485 standard_boot_devices
= g_strdup(boot_devices
);
2486 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2487 qemu_register_reset(restore_boot_devices
,
2488 standard_boot_devices
);
2490 if (get_param_value(buf
, sizeof(buf
),
2492 if (!strcmp(buf
, "on")) {
2494 } else if (!strcmp(buf
, "off")) {
2498 "qemu: invalid option value '%s'\n",
2503 qemu_opts_parse(qemu_find_opts("boot-opts"),
2508 case QEMU_OPTION_fda
:
2509 case QEMU_OPTION_fdb
:
2510 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2513 case QEMU_OPTION_no_fd_bootchk
:
2516 case QEMU_OPTION_netdev
:
2517 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2521 case QEMU_OPTION_net
:
2522 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2527 case QEMU_OPTION_tftp
:
2528 legacy_tftp_prefix
= optarg
;
2530 case QEMU_OPTION_bootp
:
2531 legacy_bootp_filename
= optarg
;
2533 case QEMU_OPTION_redir
:
2534 if (net_slirp_redir(optarg
) < 0)
2538 case QEMU_OPTION_bt
:
2539 add_device_config(DEV_BT
, optarg
);
2541 case QEMU_OPTION_audio_help
:
2542 if (!(audio_available())) {
2543 printf("Option %s not supported for this target\n", popt
->name
);
2549 case QEMU_OPTION_soundhw
:
2550 if (!(audio_available())) {
2551 printf("Option %s not supported for this target\n", popt
->name
);
2554 select_soundhw (optarg
);
2559 case QEMU_OPTION_version
:
2563 case QEMU_OPTION_m
: {
2567 value
= strtosz(optarg
, &end
);
2568 if (value
< 0 || *end
) {
2569 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2573 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2574 fprintf(stderr
, "qemu: ram size too large\n");
2580 case QEMU_OPTION_mempath
:
2584 case QEMU_OPTION_mem_prealloc
:
2595 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2597 case QEMU_OPTION_gdb
:
2598 gdbstub_dev
= optarg
;
2603 case QEMU_OPTION_bios
:
2606 case QEMU_OPTION_singlestep
:
2613 keyboard_layout
= optarg
;
2615 case QEMU_OPTION_localtime
:
2618 case QEMU_OPTION_vga
:
2626 w
= strtol(p
, (char **)&p
, 10);
2629 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2635 h
= strtol(p
, (char **)&p
, 10);
2640 depth
= strtol(p
, (char **)&p
, 10);
2641 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2642 depth
!= 24 && depth
!= 32)
2644 } else if (*p
== '\0') {
2645 depth
= graphic_depth
;
2652 graphic_depth
= depth
;
2655 case QEMU_OPTION_echr
:
2658 term_escape_char
= strtol(optarg
, &r
, 0);
2660 printf("Bad argument to echr\n");
2663 case QEMU_OPTION_monitor
:
2664 monitor_parse(optarg
, "readline");
2665 default_monitor
= 0;
2667 case QEMU_OPTION_qmp
:
2668 monitor_parse(optarg
, "control");
2669 default_monitor
= 0;
2671 case QEMU_OPTION_mon
:
2672 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2676 default_monitor
= 0;
2678 case QEMU_OPTION_chardev
:
2679 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2684 case QEMU_OPTION_fsdev
:
2685 olist
= qemu_find_opts("fsdev");
2687 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2690 opts
= qemu_opts_parse(olist
, optarg
, 1);
2692 fprintf(stderr
, "parse error: %s\n", optarg
);
2696 case QEMU_OPTION_virtfs
: {
2699 const char *writeout
, *sock_fd
, *socket
;
2701 olist
= qemu_find_opts("virtfs");
2703 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2706 opts
= qemu_opts_parse(olist
, optarg
, 1);
2708 fprintf(stderr
, "parse error: %s\n", optarg
);
2712 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2713 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2714 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2717 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2718 qemu_opt_get(opts
, "mount_tag"), 1);
2720 fprintf(stderr
, "duplicate fsdev id: %s\n",
2721 qemu_opt_get(opts
, "mount_tag"));
2725 writeout
= qemu_opt_get(opts
, "writeout");
2727 #ifdef CONFIG_SYNC_FILE_RANGE
2728 qemu_opt_set(fsdev
, "writeout", writeout
);
2730 fprintf(stderr
, "writeout=immediate not supported on "
2735 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2736 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2737 qemu_opt_set(fsdev
, "security_model",
2738 qemu_opt_get(opts
, "security_model"));
2739 socket
= qemu_opt_get(opts
, "socket");
2741 qemu_opt_set(fsdev
, "socket", socket
);
2743 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2745 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2748 qemu_opt_set_bool(fsdev
, "readonly",
2749 qemu_opt_get_bool(opts
, "readonly", 0));
2750 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2751 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2752 qemu_opt_set(device
, "fsdev",
2753 qemu_opt_get(opts
, "mount_tag"));
2754 qemu_opt_set(device
, "mount_tag",
2755 qemu_opt_get(opts
, "mount_tag"));
2758 case QEMU_OPTION_virtfs_synth
: {
2762 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2764 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2767 qemu_opt_set(fsdev
, "fsdriver", "synth");
2769 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2770 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2771 qemu_opt_set(device
, "fsdev", "v_synth");
2772 qemu_opt_set(device
, "mount_tag", "v_synth");
2775 case QEMU_OPTION_serial
:
2776 add_device_config(DEV_SERIAL
, optarg
);
2778 if (strncmp(optarg
, "mon:", 4) == 0) {
2779 default_monitor
= 0;
2782 case QEMU_OPTION_watchdog
:
2785 "qemu: only one watchdog option may be given\n");
2790 case QEMU_OPTION_watchdog_action
:
2791 if (select_watchdog_action(optarg
) == -1) {
2792 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2796 case QEMU_OPTION_virtiocon
:
2797 add_device_config(DEV_VIRTCON
, optarg
);
2798 default_virtcon
= 0;
2799 if (strncmp(optarg
, "mon:", 4) == 0) {
2800 default_monitor
= 0;
2803 case QEMU_OPTION_parallel
:
2804 add_device_config(DEV_PARALLEL
, optarg
);
2805 default_parallel
= 0;
2806 if (strncmp(optarg
, "mon:", 4) == 0) {
2807 default_monitor
= 0;
2810 case QEMU_OPTION_debugcon
:
2811 add_device_config(DEV_DEBUGCON
, optarg
);
2813 case QEMU_OPTION_loadvm
:
2816 case QEMU_OPTION_full_screen
:
2820 case QEMU_OPTION_no_frame
:
2823 case QEMU_OPTION_alt_grab
:
2826 case QEMU_OPTION_ctrl_grab
:
2829 case QEMU_OPTION_no_quit
:
2832 case QEMU_OPTION_sdl
:
2833 display_type
= DT_SDL
;
2836 case QEMU_OPTION_no_frame
:
2837 case QEMU_OPTION_alt_grab
:
2838 case QEMU_OPTION_ctrl_grab
:
2839 case QEMU_OPTION_no_quit
:
2840 case QEMU_OPTION_sdl
:
2841 fprintf(stderr
, "SDL support is disabled\n");
2844 case QEMU_OPTION_pidfile
:
2847 case QEMU_OPTION_win2k_hack
:
2848 win2k_install_hack
= 1;
2850 case QEMU_OPTION_rtc_td_hack
:
2853 case QEMU_OPTION_acpitable
:
2854 do_acpitable_option(optarg
);
2856 case QEMU_OPTION_smbios
:
2857 do_smbios_option(optarg
);
2859 case QEMU_OPTION_enable_kvm
:
2860 olist
= qemu_find_opts("machine");
2861 qemu_opts_reset(olist
);
2862 qemu_opts_parse(olist
, "accel=kvm", 0);
2864 case QEMU_OPTION_machine
:
2865 olist
= qemu_find_opts("machine");
2866 qemu_opts_reset(olist
);
2867 opts
= qemu_opts_parse(olist
, optarg
, 1);
2869 fprintf(stderr
, "parse error: %s\n", optarg
);
2872 optarg
= qemu_opt_get(opts
, "type");
2874 machine
= machine_parse(optarg
);
2877 case QEMU_OPTION_no_kvm
:
2878 olist
= qemu_find_opts("machine");
2879 qemu_opts_reset(olist
);
2880 qemu_opts_parse(olist
, "accel=tcg", 0);
2882 #ifdef CONFIG_KVM_OPTIONS
2883 case QEMU_OPTION_no_kvm_irqchip
: {
2887 case QEMU_OPTION_no_kvm_pit
: {
2888 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
2892 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2893 kvm_pit_reinject
= 0;
2897 case QEMU_OPTION_usb
:
2900 case QEMU_OPTION_usbdevice
:
2902 add_device_config(DEV_USB
, optarg
);
2904 case QEMU_OPTION_device
:
2905 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2909 case QEMU_OPTION_smp
:
2912 fprintf(stderr
, "Invalid number of CPUs\n");
2915 if (max_cpus
< smp_cpus
) {
2916 fprintf(stderr
, "maxcpus must be equal to or greater than "
2920 if (max_cpus
> 255) {
2921 fprintf(stderr
, "Unsupported number of maxcpus\n");
2925 case QEMU_OPTION_vnc
:
2928 vnc_display
= optarg
;
2930 fprintf(stderr
, "VNC support is disabled\n");
2934 case QEMU_OPTION_no_acpi
:
2937 case QEMU_OPTION_no_hpet
:
2940 case QEMU_OPTION_balloon
:
2941 if (balloon_parse(optarg
) < 0) {
2942 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2946 case QEMU_OPTION_no_reboot
:
2949 case QEMU_OPTION_no_shutdown
:
2952 case QEMU_OPTION_show_cursor
:
2955 case QEMU_OPTION_uuid
:
2956 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2957 fprintf(stderr
, "Fail to parse UUID string."
2958 " Wrong format.\n");
2962 case QEMU_OPTION_option_rom
:
2963 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2964 fprintf(stderr
, "Too many option ROMs\n");
2967 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2968 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2969 option_rom
[nb_option_roms
].bootindex
=
2970 qemu_opt_get_number(opts
, "bootindex", -1);
2971 if (!option_rom
[nb_option_roms
].name
) {
2972 fprintf(stderr
, "Option ROM file is not specified\n");
2977 case QEMU_OPTION_semihosting
:
2978 semihosting_enabled
= 1;
2980 case QEMU_OPTION_tdf
:
2981 fprintf(stderr
, "Warning: user space PIT time drift fix "
2982 "is no longer supported.\n");
2984 case QEMU_OPTION_kvm_shadow_memory
:
2985 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
2987 case QEMU_OPTION_name
:
2988 qemu_name
= g_strdup(optarg
);
2990 char *p
= strchr(qemu_name
, ',');
2993 if (strncmp(p
, "process=", 8)) {
2994 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2998 os_set_proc_name(p
);
3002 case QEMU_OPTION_prom_env
:
3003 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3004 fprintf(stderr
, "Too many prom variables\n");
3007 prom_envs
[nb_prom_envs
] = optarg
;
3010 case QEMU_OPTION_old_param
:
3013 case QEMU_OPTION_clock
:
3014 configure_alarms(optarg
);
3016 case QEMU_OPTION_startdate
:
3017 configure_rtc_date_offset(optarg
, 1);
3019 case QEMU_OPTION_rtc
:
3020 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3024 configure_rtc(opts
);
3026 case QEMU_OPTION_tb_size
:
3027 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3028 if (tcg_tb_size
< 0) {
3032 case QEMU_OPTION_icount
:
3033 icount_option
= optarg
;
3035 case QEMU_OPTION_incoming
:
3038 case QEMU_OPTION_nodefaults
:
3040 default_parallel
= 0;
3041 default_virtcon
= 0;
3042 default_monitor
= 0;
3049 case QEMU_OPTION_xen_domid
:
3050 if (!(xen_available())) {
3051 printf("Option %s not supported for this target\n", popt
->name
);
3054 xen_domid
= atoi(optarg
);
3056 case QEMU_OPTION_xen_create
:
3057 if (!(xen_available())) {
3058 printf("Option %s not supported for this target\n", popt
->name
);
3061 xen_mode
= XEN_CREATE
;
3063 case QEMU_OPTION_xen_attach
:
3064 if (!(xen_available())) {
3065 printf("Option %s not supported for this target\n", popt
->name
);
3068 xen_mode
= XEN_ATTACH
;
3070 case QEMU_OPTION_trace
:
3072 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3076 trace_events
= qemu_opt_get(opts
, "events");
3077 trace_file
= qemu_opt_get(opts
, "file");
3080 case QEMU_OPTION_readconfig
:
3082 int ret
= qemu_read_config_file(optarg
);
3084 fprintf(stderr
, "read config %s: %s\n", optarg
,
3090 case QEMU_OPTION_spice
:
3091 olist
= qemu_find_opts("spice");
3093 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3096 opts
= qemu_opts_parse(olist
, optarg
, 0);
3098 fprintf(stderr
, "parse error: %s\n", optarg
);
3102 case QEMU_OPTION_writeconfig
:
3105 if (strcmp(optarg
, "-") == 0) {
3108 fp
= fopen(optarg
, "w");
3110 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3114 qemu_config_write(fp
);
3119 os_parse_cmd_args(popt
->index
, optarg
);
3125 /* Open the logfile at this point, if necessary. We can't open the logfile
3126 * when encountering either of the logging options (-d or -D) because the
3127 * other one may be encountered later on the command line, changing the
3128 * location or level of logging.
3132 set_cpu_log_filename(log_file
);
3134 set_cpu_log(log_mask
);
3137 if (!trace_backend_init(trace_events
, trace_file
)) {
3141 /* If no data_dir is specified then try to find it relative to the
3144 data_dir
= os_find_datadir(argv
[0]);
3146 /* If all else fails use the install path specified when building. */
3148 data_dir
= CONFIG_QEMU_DATADIR
;
3151 if (machine
== NULL
) {
3152 fprintf(stderr
, "No machine found.\n");
3157 * Default to max_cpus = smp_cpus, in case the user doesn't
3158 * specify a max_cpus value.
3161 max_cpus
= smp_cpus
;
3163 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3164 if (smp_cpus
> machine
->max_cpus
) {
3165 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3166 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3172 * Get the default machine options from the machine if it is not already
3173 * specified either by the configuration file or by the command line.
3175 if (machine
->default_machine_opts
) {
3176 QemuOptsList
*list
= qemu_find_opts("machine");
3177 const char *p
= NULL
;
3179 if (!QTAILQ_EMPTY(&list
->head
)) {
3180 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3183 qemu_opts_reset(list
);
3184 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3186 fprintf(stderr
, "parse error for machine %s: %s\n",
3187 machine
->name
, machine
->default_machine_opts
);
3193 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3194 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3196 if (machine
->no_serial
) {
3199 if (machine
->no_parallel
) {
3200 default_parallel
= 0;
3202 if (!machine
->use_virtcon
) {
3203 default_virtcon
= 0;
3205 if (machine
->no_floppy
) {
3208 if (machine
->no_cdrom
) {
3211 if (machine
->no_sdcard
) {
3215 if (display_type
== DT_NOGRAPHIC
) {
3216 if (default_parallel
)
3217 add_device_config(DEV_PARALLEL
, "null");
3218 if (default_serial
&& default_monitor
) {
3219 add_device_config(DEV_SERIAL
, "mon:stdio");
3220 } else if (default_virtcon
&& default_monitor
) {
3221 add_device_config(DEV_VIRTCON
, "mon:stdio");
3224 add_device_config(DEV_SERIAL
, "stdio");
3225 if (default_virtcon
)
3226 add_device_config(DEV_VIRTCON
, "stdio");
3227 if (default_monitor
)
3228 monitor_parse("stdio", "readline");
3232 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3233 if (default_parallel
)
3234 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3235 if (default_monitor
)
3236 monitor_parse("vc:80Cx24C", "readline");
3237 if (default_virtcon
)
3238 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3243 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3245 #ifdef CONFIG_VIRTFS
3246 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3253 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3258 /* init the memory */
3259 if (ram_size
== 0) {
3260 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3263 configure_accelerator();
3265 qemu_init_cpu_loop();
3266 if (qemu_init_main_loop()) {
3267 fprintf(stderr
, "qemu_init_main_loop failed\n");
3270 linux_boot
= (kernel_filename
!= NULL
);
3272 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3273 fprintf(stderr
, "-append only allowed with -kernel option\n");
3277 if (!linux_boot
&& initrd_filename
!= NULL
) {
3278 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3282 os_set_line_buffering();
3284 if (init_timer_alarm() < 0) {
3285 fprintf(stderr
, "could not initialize alarm timer\n");
3289 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3290 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3293 configure_icount(icount_option
);
3295 if (net_init_clients() < 0) {
3299 /* init the bluetooth world */
3300 if (foreach_device_config(DEV_BT
, bt_parse
))
3303 if (!xen_enabled()) {
3304 /* On 32-bit hosts, QEMU is limited by virtual address space */
3305 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3306 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3311 cpu_exec_init_all();
3313 bdrv_init_with_whitelist();
3317 /* open the virtual block devices */
3319 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3320 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3323 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3324 IF_DEFAULT
, 2, CDROM_OPTS
);
3325 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3326 IF_FLOPPY
, 0, FD_OPTS
);
3327 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3330 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3333 if (nb_numa_nodes
> 0) {
3336 if (nb_numa_nodes
> MAX_NODES
) {
3337 nb_numa_nodes
= MAX_NODES
;
3340 /* If no memory size if given for any node, assume the default case
3341 * and distribute the available memory equally across all nodes
3343 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3344 if (node_mem
[i
] != 0)
3347 if (i
== nb_numa_nodes
) {
3348 uint64_t usedmem
= 0;
3350 /* On Linux, the each node's border has to be 8MB aligned,
3351 * the final node gets the rest.
3353 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3354 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3355 usedmem
+= node_mem
[i
];
3357 node_mem
[i
] = ram_size
- usedmem
;
3360 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3361 if (node_cpumask
[i
] != 0)
3364 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3365 * must cope with this anyway, because there are BIOSes out there in
3366 * real machines which also use this scheme.
3368 if (i
== nb_numa_nodes
) {
3369 for (i
= 0; i
< max_cpus
; i
++) {
3370 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3375 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3379 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3381 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3383 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3385 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3388 module_call_init(MODULE_INIT_DEVICE
);
3390 /* must be after qdev registration but before machine init */
3392 select_vgahw(vga_model
);
3393 } else if (cirrus_vga_available()) {
3394 select_vgahw("cirrus");
3396 select_vgahw("none");
3399 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3403 i
= select_watchdog(watchdog
);
3405 exit (i
== 1 ? 1 : 0);
3408 if (machine
->compat_props
) {
3409 qdev_prop_register_global_list(machine
->compat_props
);
3413 qdev_machine_init();
3415 machine
->init(ram_size
, boot_devices
,
3416 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3418 cpu_synchronize_all_post_init();
3422 current_machine
= machine
;
3424 /* init USB devices */
3426 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3430 /* init generic devices */
3431 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3434 net_check_clients();
3436 /* just use the first displaystate for the moment */
3437 ds
= get_displaystate();
3441 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3442 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3443 display_type
= DT_SDL
;
3444 #elif defined(CONFIG_VNC)
3445 vnc_display
= "localhost:0,to=99";
3448 display_type
= DT_NONE
;
3453 /* init local displays */
3454 switch (display_type
) {
3457 #if defined(CONFIG_CURSES)
3459 curses_display_init(ds
, full_screen
);
3462 #if defined(CONFIG_SDL)
3464 sdl_display_init(ds
, full_screen
, no_frame
);
3466 #elif defined(CONFIG_COCOA)
3468 cocoa_display_init(ds
, full_screen
);
3475 /* must be after terminal init, SDL library changes signal handlers */
3476 os_setup_signal_handling();
3479 /* init remote displays */
3481 vnc_display_init(ds
);
3482 if (vnc_display_open(ds
, vnc_display
) < 0)
3485 if (show_vnc_port
) {
3486 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3491 if (using_spice
&& !qxl_enabled
) {
3492 qemu_spice_display_init(ds
);
3498 dcl
= ds
->listeners
;
3499 while (dcl
!= NULL
) {
3500 if (dcl
->dpy_refresh
!= NULL
) {
3501 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3502 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3507 text_consoles_set_display(ds
);
3509 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3510 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3515 qdev_machine_creation_done();
3517 if (rom_load_all() != 0) {
3518 fprintf(stderr
, "rom loading failed\n");
3522 /* TODO: once all bus devices are qdevified, this should be done
3523 * when bus is created by qdev.c */
3524 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3525 qemu_run_machine_init_done_notifiers();
3527 qemu_system_reset(VMRESET_SILENT
);
3529 if (load_vmstate(loadvm
) < 0) {
3535 runstate_set(RUN_STATE_INMIGRATE
);
3536 int ret
= qemu_start_incoming_migration(incoming
);
3538 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3542 } else if (autostart
) {