4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 int display_remote
= 0;
181 const char* keyboard_layout
= NULL
;
183 const char *mem_path
= NULL
;
185 int mem_prealloc
= 0; /* force preallocation of physical target memory */
188 NICInfo nd_table
[MAX_NICS
];
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
194 static int full_screen
= 0;
196 static int no_frame
= 0;
199 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
200 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
201 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
202 int win2k_install_hack
= 0;
211 const char *vnc_display
;
213 int acpi_enabled
= 1;
219 int graphic_rotate
= 0;
220 const char *watchdog
;
221 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
223 int semihosting_enabled
= 0;
224 int time_drift_fix
= 0;
225 unsigned int kvm_shadow_memory
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
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_vga
= 1;
275 static int default_floppy
= 1;
276 static int default_cdrom
= 1;
277 static int default_sdcard
= 1;
283 { .driver
= "isa-serial", .flag
= &default_serial
},
284 { .driver
= "isa-parallel", .flag
= &default_parallel
},
285 { .driver
= "isa-fdc", .flag
= &default_floppy
},
286 { .driver
= "ide-cd", .flag
= &default_cdrom
},
287 { .driver
= "ide-hd", .flag
= &default_cdrom
},
288 { .driver
= "ide-drive", .flag
= &default_cdrom
},
289 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
290 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 { .driver
= "VGA", .flag
= &default_vga
},
294 { .driver
= "cirrus-vga", .flag
= &default_vga
},
295 { .driver
= "vmware-svga", .flag
= &default_vga
},
296 { .driver
= "isa-vga", .flag
= &default_vga
},
297 { .driver
= "qxl-vga", .flag
= &default_vga
},
300 static void res_free(void)
302 if (boot_splash_filedata
!= NULL
) {
303 g_free(boot_splash_filedata
);
304 boot_splash_filedata
= NULL
;
308 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
310 const char *driver
= qemu_opt_get(opts
, "driver");
315 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
316 if (strcmp(default_list
[i
].driver
, driver
) != 0)
318 *(default_list
[i
].flag
) = 0;
323 /***********************************************************/
326 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
331 } RunStateTransition
;
333 static const RunStateTransition runstate_transitions_def
[] = {
335 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
337 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
338 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
340 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
342 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
344 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
346 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
348 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
349 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
350 { RUN_STATE_PRELAUNCH
, RUN_STATE_POSTMIGRATE
},
352 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
353 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
355 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
357 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
358 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
359 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
360 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
361 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
362 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
363 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
364 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
365 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
367 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
369 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
371 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
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 if (new_state
>= RUN_STATE_MAX
||
398 !runstate_valid_transitions
[current_run_state
][new_state
]) {
399 fprintf(stderr
, "invalid runstate transition\n");
403 current_run_state
= new_state
;
406 int runstate_is_running(void)
408 return runstate_check(RUN_STATE_RUNNING
);
411 StatusInfo
*qmp_query_status(Error
**errp
)
413 StatusInfo
*info
= g_malloc0(sizeof(*info
));
415 info
->running
= runstate_is_running();
416 info
->singlestep
= singlestep
;
417 info
->status
= current_run_state
;
422 /***********************************************************/
423 /* real time host monotonic timer */
425 /***********************************************************/
426 /* host time/date access */
427 void qemu_get_timedate(struct tm
*tm
, int offset
)
434 if (rtc_date_offset
== -1) {
438 ret
= localtime(&ti
);
440 ti
-= rtc_date_offset
;
444 memcpy(tm
, ret
, sizeof(struct tm
));
447 int qemu_timedate_diff(struct tm
*tm
)
451 if (rtc_date_offset
== -1)
453 seconds
= mktimegm(tm
);
455 seconds
= mktime(tm
);
457 seconds
= mktimegm(tm
) + rtc_date_offset
;
459 return seconds
- time(NULL
);
462 void rtc_change_mon_event(struct tm
*tm
)
466 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
467 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
468 qobject_decref(data
);
471 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
473 time_t rtc_start_date
;
476 if (!strcmp(startdate
, "now") && legacy
) {
477 rtc_date_offset
= -1;
479 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
487 } else if (sscanf(startdate
, "%d-%d-%d",
499 rtc_start_date
= mktimegm(&tm
);
500 if (rtc_start_date
== -1) {
502 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
503 "'2006-06-17T16:01:21' or '2006-06-17'\n");
506 rtc_date_offset
= time(NULL
) - rtc_start_date
;
510 static void configure_rtc(QemuOpts
*opts
)
514 value
= qemu_opt_get(opts
, "base");
516 if (!strcmp(value
, "utc")) {
518 } else if (!strcmp(value
, "localtime")) {
521 configure_rtc_date_offset(value
, 0);
524 value
= qemu_opt_get(opts
, "clock");
526 if (!strcmp(value
, "host")) {
527 rtc_clock
= host_clock
;
528 } else if (!strcmp(value
, "vm")) {
529 rtc_clock
= vm_clock
;
531 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
535 value
= qemu_opt_get(opts
, "driftfix");
537 if (!strcmp(value
, "slew")) {
539 } else if (!strcmp(value
, "none")) {
542 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
548 /***********************************************************/
549 /* Bluetooth support */
552 static struct HCIInfo
*hci_table
[MAX_NICS
];
554 static struct bt_vlan_s
{
555 struct bt_scatternet_s net
;
557 struct bt_vlan_s
*next
;
560 /* find or alloc a new bluetooth "VLAN" */
561 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
563 struct bt_vlan_s
**pvlan
, *vlan
;
564 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
568 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
570 pvlan
= &first_bt_vlan
;
571 while (*pvlan
!= NULL
)
572 pvlan
= &(*pvlan
)->next
;
577 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
581 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
586 static struct HCIInfo null_hci
= {
587 .cmd_send
= null_hci_send
,
588 .sco_send
= null_hci_send
,
589 .acl_send
= null_hci_send
,
590 .bdaddr_set
= null_hci_addr_set
,
593 struct HCIInfo
*qemu_next_hci(void)
595 if (cur_hci
== nb_hcis
)
598 return hci_table
[cur_hci
++];
601 static struct HCIInfo
*hci_init(const char *str
)
604 struct bt_scatternet_s
*vlan
= 0;
606 if (!strcmp(str
, "null"))
609 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
611 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
612 else if (!strncmp(str
, "hci", 3)) {
615 if (!strncmp(str
+ 3, ",vlan=", 6)) {
616 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
621 vlan
= qemu_find_bt_vlan(0);
623 return bt_new_hci(vlan
);
626 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
631 static int bt_hci_parse(const char *str
)
636 if (nb_hcis
>= MAX_NICS
) {
637 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
650 bdaddr
.b
[5] = 0x56 + nb_hcis
;
651 hci
->bdaddr_set(hci
, bdaddr
.b
);
653 hci_table
[nb_hcis
++] = hci
;
658 static void bt_vhci_add(int vlan_id
)
660 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
663 fprintf(stderr
, "qemu: warning: adding a VHCI to "
664 "an empty scatternet %i\n", vlan_id
);
666 bt_vhci_init(bt_new_hci(vlan
));
669 static struct bt_device_s
*bt_device_add(const char *opt
)
671 struct bt_scatternet_s
*vlan
;
673 char *endp
= strstr(opt
, ",vlan=");
674 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
677 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
680 vlan_id
= strtol(endp
+ 6, &endp
, 0);
682 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
687 vlan
= qemu_find_bt_vlan(vlan_id
);
690 fprintf(stderr
, "qemu: warning: adding a slave device to "
691 "an empty scatternet %i\n", vlan_id
);
693 if (!strcmp(devname
, "keyboard"))
694 return bt_keyboard_init(vlan
);
696 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
700 static int bt_parse(const char *opt
)
702 const char *endp
, *p
;
705 if (strstart(opt
, "hci", &endp
)) {
706 if (!*endp
|| *endp
== ',') {
708 if (!strstart(endp
, ",vlan=", 0))
711 return bt_hci_parse(opt
);
713 } else if (strstart(opt
, "vhci", &endp
)) {
714 if (!*endp
|| *endp
== ',') {
716 if (strstart(endp
, ",vlan=", &p
)) {
717 vlan
= strtol(p
, (char **) &endp
, 0);
719 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
723 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
732 } else if (strstart(opt
, "device:", &endp
))
733 return !bt_device_add(endp
);
735 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
739 /***********************************************************/
740 /* QEMU Block devices */
742 #define HD_OPTS "media=disk"
743 #define CDROM_OPTS "media=cdrom"
745 #define PFLASH_OPTS ""
749 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
751 int *use_scsi
= opaque
;
753 return drive_init(opts
, *use_scsi
) == NULL
;
756 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
758 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
759 qemu_opt_set(opts
, "snapshot", "on");
764 static void default_drive(int enable
, int snapshot
, int use_scsi
,
765 BlockInterfaceType type
, int index
,
770 if (type
== IF_DEFAULT
) {
771 type
= use_scsi
? IF_SCSI
: IF_IDE
;
774 if (!enable
|| drive_get_by_index(type
, index
)) {
778 opts
= drive_add(type
, index
, NULL
, optstr
);
780 drive_enable_snapshot(opts
, NULL
);
782 if (!drive_init(opts
, use_scsi
)) {
787 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
789 boot_set_handler
= func
;
790 boot_set_opaque
= opaque
;
793 int qemu_boot_set(const char *boot_devices
)
795 if (!boot_set_handler
) {
798 return boot_set_handler(boot_set_opaque
, boot_devices
);
801 static void validate_bootdevices(char *devices
)
803 /* We just do some generic consistency checks */
807 for (p
= devices
; *p
!= '\0'; p
++) {
808 /* Allowed boot devices are:
809 * a-b: floppy disk drives
810 * c-f: IDE disk drives
811 * g-m: machine implementation dependant drives
812 * n-p: network devices
813 * It's up to each machine implementation to check if the given boot
814 * devices match the actual hardware implementation and firmware
817 if (*p
< 'a' || *p
> 'p') {
818 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
821 if (bitmap
& (1 << (*p
- 'a'))) {
822 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
825 bitmap
|= 1 << (*p
- 'a');
829 static void restore_boot_devices(void *opaque
)
831 char *standard_boot_devices
= opaque
;
832 static int first
= 1;
834 /* Restore boot order and remove ourselves after the first boot */
840 qemu_boot_set(standard_boot_devices
);
842 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
843 g_free(standard_boot_devices
);
846 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
849 FWBootEntry
*node
, *i
;
855 assert(dev
!= NULL
|| suffix
!= NULL
);
857 node
= g_malloc0(sizeof(FWBootEntry
));
858 node
->bootindex
= bootindex
;
859 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
862 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
863 if (i
->bootindex
== bootindex
) {
864 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
866 } else if (i
->bootindex
< bootindex
) {
869 QTAILQ_INSERT_BEFORE(i
, node
, link
);
872 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
876 * This function returns null terminated string that consist of new line
877 * separated device paths.
879 * memory pointed by "size" is assigned total length of the array in bytes
882 char *get_boot_devices_list(uint32_t *size
)
888 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
889 char *devpath
= NULL
, *bootpath
;
893 devpath
= qdev_get_fw_dev_path(i
->dev
);
897 if (i
->suffix
&& devpath
) {
898 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
900 bootpath
= g_malloc(bootpathlen
);
901 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
903 } else if (devpath
) {
906 bootpath
= g_strdup(i
->suffix
);
911 list
[total
-1] = '\n';
913 len
= strlen(bootpath
) + 1;
914 list
= g_realloc(list
, total
+ len
);
915 memcpy(&list
[total
], bootpath
, len
);
925 static void numa_add(const char *optarg
)
929 unsigned long long value
, endvalue
;
932 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
933 if (!strcmp(option
, "node")) {
934 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
935 nodenr
= nb_numa_nodes
;
937 nodenr
= strtoull(option
, NULL
, 10);
940 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
941 node_mem
[nodenr
] = 0;
944 sval
= strtosz(option
, NULL
);
946 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
949 node_mem
[nodenr
] = sval
;
951 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
952 node_cpumask
[nodenr
] = 0;
954 value
= strtoull(option
, &endptr
, 10);
957 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
959 if (*endptr
== '-') {
960 endvalue
= strtoull(endptr
+1, &endptr
, 10);
961 if (endvalue
>= 63) {
964 "only 63 CPUs in NUMA mode supported.\n");
966 value
= (2ULL << endvalue
) - (1ULL << value
);
968 value
= 1ULL << value
;
971 node_cpumask
[nodenr
] = value
;
978 static void smp_parse(const char *optarg
)
980 int smp
, sockets
= 0, threads
= 0, cores
= 0;
984 smp
= strtoul(optarg
, &endptr
, 10);
985 if (endptr
!= optarg
) {
986 if (*endptr
== ',') {
990 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
991 sockets
= strtoull(option
, NULL
, 10);
992 if (get_param_value(option
, 128, "cores", endptr
) != 0)
993 cores
= strtoull(option
, NULL
, 10);
994 if (get_param_value(option
, 128, "threads", endptr
) != 0)
995 threads
= strtoull(option
, NULL
, 10);
996 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
997 max_cpus
= strtoull(option
, NULL
, 10);
999 /* compute missing values, prefer sockets over cores over threads */
1000 if (smp
== 0 || sockets
== 0) {
1001 sockets
= sockets
> 0 ? sockets
: 1;
1002 cores
= cores
> 0 ? cores
: 1;
1003 threads
= threads
> 0 ? threads
: 1;
1005 smp
= cores
* threads
* sockets
;
1009 threads
= threads
> 0 ? threads
: 1;
1010 cores
= smp
/ (sockets
* threads
);
1012 threads
= smp
/ (cores
* sockets
);
1016 smp_cores
= cores
> 0 ? cores
: 1;
1017 smp_threads
= threads
> 0 ? threads
: 1;
1019 max_cpus
= smp_cpus
;
1022 /***********************************************************/
1025 static int usb_device_add(const char *devname
)
1028 USBDevice
*dev
= NULL
;
1033 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1034 dev
= usbdevice_create(devname
);
1038 /* the other ones */
1039 #ifndef CONFIG_LINUX
1040 /* only the linux version is qdev-ified, usb-bsd still needs this */
1041 if (strstart(devname
, "host:", &p
)) {
1042 dev
= usb_host_device_open(p
);
1045 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1046 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1047 bt_new_hci(qemu_find_bt_vlan(0)));
1058 static int usb_device_del(const char *devname
)
1063 if (strstart(devname
, "host:", &p
))
1064 return usb_host_device_close(p
);
1069 p
= strchr(devname
, '.');
1072 bus_num
= strtoul(devname
, NULL
, 0);
1073 addr
= strtoul(p
+ 1, NULL
, 0);
1075 return usb_device_delete_addr(bus_num
, addr
);
1078 static int usb_parse(const char *cmdline
)
1081 r
= usb_device_add(cmdline
);
1083 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1088 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1090 const char *devname
= qdict_get_str(qdict
, "devname");
1091 if (usb_device_add(devname
) < 0) {
1092 error_report("could not add USB device '%s'", devname
);
1096 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1098 const char *devname
= qdict_get_str(qdict
, "devname");
1099 if (usb_device_del(devname
) < 0) {
1100 error_report("could not delete USB device '%s'", devname
);
1104 /***********************************************************/
1105 /* PCMCIA/Cardbus */
1107 static struct pcmcia_socket_entry_s
{
1108 PCMCIASocket
*socket
;
1109 struct pcmcia_socket_entry_s
*next
;
1110 } *pcmcia_sockets
= 0;
1112 void pcmcia_socket_register(PCMCIASocket
*socket
)
1114 struct pcmcia_socket_entry_s
*entry
;
1116 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1117 entry
->socket
= socket
;
1118 entry
->next
= pcmcia_sockets
;
1119 pcmcia_sockets
= entry
;
1122 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1124 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1126 ptr
= &pcmcia_sockets
;
1127 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1128 if (entry
->socket
== socket
) {
1134 void pcmcia_info(Monitor
*mon
)
1136 struct pcmcia_socket_entry_s
*iter
;
1138 if (!pcmcia_sockets
)
1139 monitor_printf(mon
, "No PCMCIA sockets\n");
1141 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1142 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1143 iter
->socket
->attached
? iter
->socket
->card_string
:
1147 /***********************************************************/
1148 /* machine registration */
1150 static QEMUMachine
*first_machine
= NULL
;
1151 QEMUMachine
*current_machine
= NULL
;
1153 int qemu_register_machine(QEMUMachine
*m
)
1156 pm
= &first_machine
;
1164 static QEMUMachine
*find_machine(const char *name
)
1168 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1169 if (!strcmp(m
->name
, name
))
1171 if (m
->alias
&& !strcmp(m
->alias
, name
))
1177 static QEMUMachine
*find_default_machine(void)
1181 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1182 if (m
->is_default
) {
1189 /***********************************************************/
1190 /* main execution loop */
1192 static void gui_update(void *opaque
)
1194 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1195 DisplayState
*ds
= opaque
;
1196 DisplayChangeListener
*dcl
= ds
->listeners
;
1200 while (dcl
!= NULL
) {
1201 if (dcl
->gui_timer_interval
&&
1202 dcl
->gui_timer_interval
< interval
)
1203 interval
= dcl
->gui_timer_interval
;
1206 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1209 struct vm_change_state_entry
{
1210 VMChangeStateHandler
*cb
;
1212 QLIST_ENTRY (vm_change_state_entry
) entries
;
1215 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1217 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1220 VMChangeStateEntry
*e
;
1222 e
= g_malloc0(sizeof (*e
));
1226 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1230 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1232 QLIST_REMOVE (e
, entries
);
1236 void vm_state_notify(int running
, RunState state
)
1238 VMChangeStateEntry
*e
;
1240 trace_vm_state_notify(running
, state
);
1242 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1243 e
->cb(e
->opaque
, running
, state
);
1249 if (!runstate_is_running()) {
1251 runstate_set(RUN_STATE_RUNNING
);
1252 vm_state_notify(1, RUN_STATE_RUNNING
);
1254 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1258 /* reset/shutdown handler */
1260 typedef struct QEMUResetEntry
{
1261 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1262 QEMUResetHandler
*func
;
1266 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1267 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1268 static int reset_requested
;
1269 static int shutdown_requested
, shutdown_signal
= -1;
1270 static pid_t shutdown_pid
;
1271 static int powerdown_requested
;
1272 static int debug_requested
;
1273 static RunState vmstop_requested
= RUN_STATE_MAX
;
1275 int qemu_shutdown_requested_get(void)
1277 return shutdown_requested
;
1280 int qemu_reset_requested_get(void)
1282 return reset_requested
;
1285 int qemu_shutdown_requested(void)
1287 int r
= shutdown_requested
;
1288 shutdown_requested
= 0;
1292 void qemu_kill_report(void)
1294 if (shutdown_signal
!= -1) {
1295 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1296 if (shutdown_pid
== 0) {
1297 /* This happens for eg ^C at the terminal, so it's worth
1298 * avoiding printing an odd message in that case.
1300 fputc('\n', stderr
);
1302 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1304 shutdown_signal
= -1;
1308 int qemu_reset_requested(void)
1310 int r
= reset_requested
;
1311 reset_requested
= 0;
1315 int qemu_powerdown_requested(void)
1317 int r
= powerdown_requested
;
1318 powerdown_requested
= 0;
1322 static int qemu_debug_requested(void)
1324 int r
= debug_requested
;
1325 debug_requested
= 0;
1329 /* We use RUN_STATE_MAX but any invalid value will do */
1330 static bool qemu_vmstop_requested(RunState
*r
)
1332 if (vmstop_requested
< RUN_STATE_MAX
) {
1333 *r
= vmstop_requested
;
1334 vmstop_requested
= RUN_STATE_MAX
;
1341 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1343 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1346 re
->opaque
= opaque
;
1347 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1350 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1354 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1355 if (re
->func
== func
&& re
->opaque
== opaque
) {
1356 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1363 void qemu_system_reset(bool report
)
1365 QEMUResetEntry
*re
, *nre
;
1367 /* reset all devices */
1368 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1369 re
->func(re
->opaque
);
1372 monitor_protocol_event(QEVENT_RESET
, NULL
);
1374 cpu_synchronize_all_post_reset();
1377 void qemu_system_reset_request(void)
1380 shutdown_requested
= 1;
1382 reset_requested
= 1;
1385 qemu_notify_event();
1388 void qemu_system_killed(int signal
, pid_t pid
)
1390 shutdown_signal
= signal
;
1393 qemu_system_shutdown_request();
1396 void qemu_system_shutdown_request(void)
1398 shutdown_requested
= 1;
1399 qemu_notify_event();
1402 void qemu_system_powerdown_request(void)
1404 powerdown_requested
= 1;
1405 qemu_notify_event();
1408 void qemu_system_debug_request(void)
1410 debug_requested
= 1;
1411 qemu_notify_event();
1414 void qemu_system_vmstop_request(RunState state
)
1416 vmstop_requested
= state
;
1417 qemu_notify_event();
1420 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1421 static int n_poll_fds
;
1422 static int max_priority
;
1424 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1425 fd_set
*xfds
, struct timeval
*tv
)
1427 GMainContext
*context
= g_main_context_default();
1429 int timeout
= 0, cur_timeout
;
1431 g_main_context_prepare(context
, &max_priority
);
1433 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1434 poll_fds
, ARRAY_SIZE(poll_fds
));
1435 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1437 for (i
= 0; i
< n_poll_fds
; i
++) {
1438 GPollFD
*p
= &poll_fds
[i
];
1440 if ((p
->events
& G_IO_IN
)) {
1441 FD_SET(p
->fd
, rfds
);
1442 *max_fd
= MAX(*max_fd
, p
->fd
);
1444 if ((p
->events
& G_IO_OUT
)) {
1445 FD_SET(p
->fd
, wfds
);
1446 *max_fd
= MAX(*max_fd
, p
->fd
);
1448 if ((p
->events
& G_IO_ERR
)) {
1449 FD_SET(p
->fd
, xfds
);
1450 *max_fd
= MAX(*max_fd
, p
->fd
);
1454 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1455 if (timeout
>= 0 && timeout
< cur_timeout
) {
1456 tv
->tv_sec
= timeout
/ 1000;
1457 tv
->tv_usec
= (timeout
% 1000) * 1000;
1461 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1464 GMainContext
*context
= g_main_context_default();
1469 for (i
= 0; i
< n_poll_fds
; i
++) {
1470 GPollFD
*p
= &poll_fds
[i
];
1472 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1473 p
->revents
|= G_IO_IN
;
1475 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1476 p
->revents
|= G_IO_OUT
;
1478 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1479 p
->revents
|= G_IO_ERR
;
1484 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1485 g_main_context_dispatch(context
);
1489 int main_loop_wait(int nonblocking
)
1491 fd_set rfds
, wfds
, xfds
;
1499 timeout
= qemu_calculate_timeout();
1500 qemu_bh_update_timeout(&timeout
);
1503 os_host_main_loop_wait(&timeout
);
1505 tv
.tv_sec
= timeout
/ 1000;
1506 tv
.tv_usec
= (timeout
% 1000) * 1000;
1508 /* poll any events */
1509 /* XXX: separate device handlers from system ones */
1515 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1516 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1517 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1520 qemu_mutex_unlock_iothread();
1523 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1526 qemu_mutex_lock_iothread();
1529 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1530 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1531 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1533 qemu_run_all_timers();
1535 /* Check bottom-halves last in case any of the earlier events triggered
1542 qemu_irq qemu_system_powerdown
;
1544 static void main_loop(void)
1547 int last_io
__attribute__ ((unused
)) = 0;
1548 #ifdef CONFIG_PROFILER
1553 qemu_main_loop_start();
1556 nonblocking
= !kvm_enabled() && last_io
> 0;
1557 #ifdef CONFIG_PROFILER
1558 ti
= profile_getclock();
1560 last_io
= main_loop_wait(nonblocking
);
1561 #ifdef CONFIG_PROFILER
1562 dev_time
+= profile_getclock() - ti
;
1565 if (qemu_debug_requested()) {
1566 vm_stop(RUN_STATE_DEBUG
);
1568 if (qemu_shutdown_requested()) {
1570 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1572 vm_stop(RUN_STATE_SHUTDOWN
);
1576 if (qemu_reset_requested()) {
1578 cpu_synchronize_all_states();
1579 qemu_system_reset(VMRESET_REPORT
);
1581 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1582 runstate_check(RUN_STATE_SHUTDOWN
)) {
1583 runstate_set(RUN_STATE_PAUSED
);
1586 if (qemu_powerdown_requested()) {
1587 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1588 qemu_irq_raise(qemu_system_powerdown
);
1590 if (qemu_vmstop_requested(&r
)) {
1598 static void version(void)
1600 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1603 static void help(int exitcode
)
1605 const char *options_help
=
1606 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1608 #define DEFHEADING(text) stringify(text) "\n"
1609 #include "qemu-options.def"
1615 printf("usage: %s [options] [disk_image]\n"
1617 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1620 "During emulation, the following keys are useful:\n"
1621 "ctrl-alt-f toggle full screen\n"
1622 "ctrl-alt-n switch to virtual console 'n'\n"
1623 "ctrl-alt toggle mouse and keyboard grab\n"
1625 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1631 #define HAS_ARG 0x0001
1633 typedef struct QEMUOption
{
1640 static const QEMUOption qemu_options
[] = {
1641 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1642 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1643 { option, opt_arg, opt_enum, arch_mask },
1644 #define DEFHEADING(text)
1645 #include "qemu-options.def"
1651 static void select_vgahw (const char *p
)
1656 vga_interface_type
= VGA_NONE
;
1657 if (strstart(p
, "std", &opts
)) {
1658 vga_interface_type
= VGA_STD
;
1659 } else if (strstart(p
, "cirrus", &opts
)) {
1660 vga_interface_type
= VGA_CIRRUS
;
1661 } else if (strstart(p
, "vmware", &opts
)) {
1662 vga_interface_type
= VGA_VMWARE
;
1663 } else if (strstart(p
, "xenfb", &opts
)) {
1664 vga_interface_type
= VGA_XENFB
;
1665 } else if (strstart(p
, "qxl", &opts
)) {
1666 vga_interface_type
= VGA_QXL
;
1667 } else if (!strstart(p
, "none", &opts
)) {
1669 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1673 const char *nextopt
;
1675 if (strstart(opts
, ",retrace=", &nextopt
)) {
1677 if (strstart(opts
, "dumb", &nextopt
))
1678 vga_retrace_method
= VGA_RETRACE_DUMB
;
1679 else if (strstart(opts
, "precise", &nextopt
))
1680 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1681 else goto invalid_vga
;
1682 } else goto invalid_vga
;
1687 static DisplayType
select_display(const char *p
)
1690 DisplayType display
= DT_DEFAULT
;
1692 if (strstart(p
, "sdl", &opts
)) {
1696 const char *nextopt
;
1698 if (strstart(opts
, ",frame=", &nextopt
)) {
1700 if (strstart(opts
, "on", &nextopt
)) {
1702 } else if (strstart(opts
, "off", &nextopt
)) {
1705 goto invalid_sdl_args
;
1707 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1709 if (strstart(opts
, "on", &nextopt
)) {
1711 } else if (strstart(opts
, "off", &nextopt
)) {
1714 goto invalid_sdl_args
;
1716 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1718 if (strstart(opts
, "on", &nextopt
)) {
1720 } else if (strstart(opts
, "off", &nextopt
)) {
1723 goto invalid_sdl_args
;
1725 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1727 if (strstart(opts
, "on", &nextopt
)) {
1729 } else if (strstart(opts
, "off", &nextopt
)) {
1732 goto invalid_sdl_args
;
1736 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1742 fprintf(stderr
, "SDL support is disabled\n");
1745 } else if (strstart(p
, "vnc", &opts
)) {
1750 const char *nextopt
;
1752 if (strstart(opts
, "=", &nextopt
)) {
1753 vnc_display
= nextopt
;
1757 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1761 fprintf(stderr
, "VNC support is disabled\n");
1764 } else if (strstart(p
, "curses", &opts
)) {
1765 #ifdef CONFIG_CURSES
1766 display
= DT_CURSES
;
1768 fprintf(stderr
, "Curses support is disabled\n");
1771 } else if (strstart(p
, "none", &opts
)) {
1774 fprintf(stderr
, "Unknown display type: %s\n", p
);
1781 static int balloon_parse(const char *arg
)
1785 if (strcmp(arg
, "none") == 0) {
1789 if (!strncmp(arg
, "virtio", 6)) {
1790 if (arg
[6] == ',') {
1791 /* have params -> parse them */
1792 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1796 /* create empty opts */
1797 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1799 qemu_opt_set(opts
, "driver", "virtio-balloon");
1806 char *qemu_find_file(int type
, const char *name
)
1812 /* If name contains path separators then try it as a straight path. */
1813 if ((strchr(name
, '/') || strchr(name
, '\\'))
1814 && access(name
, R_OK
) == 0) {
1815 return g_strdup(name
);
1818 case QEMU_FILE_TYPE_BIOS
:
1821 case QEMU_FILE_TYPE_KEYMAP
:
1822 subdir
= "keymaps/";
1827 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1828 buf
= g_malloc0(len
);
1829 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1830 if (access(buf
, R_OK
)) {
1837 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1839 return qdev_device_help(opts
);
1842 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1846 dev
= qdev_device_add(opts
);
1852 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1854 CharDriverState
*chr
;
1856 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1862 #ifdef CONFIG_VIRTFS
1863 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1866 ret
= qemu_fsdev_add(opts
);
1872 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1874 CharDriverState
*chr
;
1875 const char *chardev
;
1879 mode
= qemu_opt_get(opts
, "mode");
1883 if (strcmp(mode
, "readline") == 0) {
1884 flags
= MONITOR_USE_READLINE
;
1885 } else if (strcmp(mode
, "control") == 0) {
1886 flags
= MONITOR_USE_CONTROL
;
1888 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1892 if (qemu_opt_get_bool(opts
, "pretty", 0))
1893 flags
|= MONITOR_USE_PRETTY
;
1895 if (qemu_opt_get_bool(opts
, "default", 0))
1896 flags
|= MONITOR_IS_DEFAULT
;
1898 chardev
= qemu_opt_get(opts
, "chardev");
1899 chr
= qemu_chr_find(chardev
);
1901 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1905 monitor_init(chr
, flags
);
1909 static void monitor_parse(const char *optarg
, const char *mode
)
1911 static int monitor_device_index
= 0;
1917 if (strstart(optarg
, "chardev:", &p
)) {
1918 snprintf(label
, sizeof(label
), "%s", p
);
1920 snprintf(label
, sizeof(label
), "compat_monitor%d",
1921 monitor_device_index
);
1922 if (monitor_device_index
== 0) {
1925 opts
= qemu_chr_parse_compat(label
, optarg
);
1927 fprintf(stderr
, "parse error: %s\n", optarg
);
1932 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1934 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1937 qemu_opt_set(opts
, "mode", mode
);
1938 qemu_opt_set(opts
, "chardev", label
);
1940 qemu_opt_set(opts
, "default", "on");
1941 monitor_device_index
++;
1944 struct device_config
{
1946 DEV_USB
, /* -usbdevice */
1948 DEV_SERIAL
, /* -serial */
1949 DEV_PARALLEL
, /* -parallel */
1950 DEV_VIRTCON
, /* -virtioconsole */
1951 DEV_DEBUGCON
, /* -debugcon */
1953 const char *cmdline
;
1954 QTAILQ_ENTRY(device_config
) next
;
1956 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1958 static void add_device_config(int type
, const char *cmdline
)
1960 struct device_config
*conf
;
1962 conf
= g_malloc0(sizeof(*conf
));
1964 conf
->cmdline
= cmdline
;
1965 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1968 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1970 struct device_config
*conf
;
1973 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1974 if (conf
->type
!= type
)
1976 rc
= func(conf
->cmdline
);
1983 static int serial_parse(const char *devname
)
1985 static int index
= 0;
1988 if (strcmp(devname
, "none") == 0)
1990 if (index
== MAX_SERIAL_PORTS
) {
1991 fprintf(stderr
, "qemu: too many serial ports\n");
1994 snprintf(label
, sizeof(label
), "serial%d", index
);
1995 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1996 if (!serial_hds
[index
]) {
1997 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1998 devname
, strerror(errno
));
2005 static int parallel_parse(const char *devname
)
2007 static int index
= 0;
2010 if (strcmp(devname
, "none") == 0)
2012 if (index
== MAX_PARALLEL_PORTS
) {
2013 fprintf(stderr
, "qemu: too many parallel ports\n");
2016 snprintf(label
, sizeof(label
), "parallel%d", index
);
2017 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2018 if (!parallel_hds
[index
]) {
2019 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2020 devname
, strerror(errno
));
2027 static int virtcon_parse(const char *devname
)
2029 QemuOptsList
*device
= qemu_find_opts("device");
2030 static int index
= 0;
2032 QemuOpts
*bus_opts
, *dev_opts
;
2034 if (strcmp(devname
, "none") == 0)
2036 if (index
== MAX_VIRTIO_CONSOLES
) {
2037 fprintf(stderr
, "qemu: too many virtio consoles\n");
2041 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2042 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2044 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2045 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2047 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2048 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2049 if (!virtcon_hds
[index
]) {
2050 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2051 devname
, strerror(errno
));
2054 qemu_opt_set(dev_opts
, "chardev", label
);
2060 static int debugcon_parse(const char *devname
)
2064 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2067 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2069 fprintf(stderr
, "qemu: already have a debugcon device\n");
2072 qemu_opt_set(opts
, "driver", "isa-debugcon");
2073 qemu_opt_set(opts
, "chardev", "debugcon");
2077 static QEMUMachine
*machine_parse(const char *name
)
2079 QEMUMachine
*m
, *machine
= NULL
;
2082 machine
= find_machine(name
);
2087 printf("Supported machines are:\n");
2088 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2090 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2092 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2093 m
->is_default
? " (default)" : "");
2095 exit(!name
|| *name
!= '?');
2098 static int tcg_init(void)
2100 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2105 const char *opt_name
;
2107 int (*available
)(void);
2111 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2112 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2113 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2116 static int configure_accelerator(void)
2118 const char *p
= NULL
;
2121 bool accel_initalised
= 0;
2122 bool init_failed
= 0;
2124 QemuOptsList
*list
= qemu_find_opts("machine");
2125 if (!QTAILQ_EMPTY(&list
->head
)) {
2126 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2130 /* Use the default "accelerator", tcg */
2134 while (!accel_initalised
&& *p
!= '\0') {
2138 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2139 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2140 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2141 *(accel_list
[i
].allowed
) = 1;
2142 ret
= accel_list
[i
].init();
2145 if (!accel_list
[i
].available()) {
2146 printf("%s not supported for this target\n",
2147 accel_list
[i
].name
);
2149 fprintf(stderr
, "failed to initialize %s: %s\n",
2153 *(accel_list
[i
].allowed
) = 0;
2155 accel_initalised
= 1;
2160 if (i
== ARRAY_SIZE(accel_list
)) {
2161 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2165 if (!accel_initalised
) {
2166 fprintf(stderr
, "No accelerator found!\n");
2171 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2174 return !accel_initalised
;
2177 void qemu_add_exit_notifier(Notifier
*notify
)
2179 notifier_list_add(&exit_notifiers
, notify
);
2182 void qemu_remove_exit_notifier(Notifier
*notify
)
2184 notifier_list_remove(&exit_notifiers
, notify
);
2187 static void qemu_run_exit_notifiers(void)
2189 notifier_list_notify(&exit_notifiers
, NULL
);
2192 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2194 notifier_list_add(&machine_init_done_notifiers
, notify
);
2197 static void qemu_run_machine_init_done_notifiers(void)
2199 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2202 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2203 const char **poptarg
, int *poptind
)
2205 const QEMUOption
*popt
;
2206 int optind
= *poptind
;
2207 char *r
= argv
[optind
];
2210 loc_set_cmdline(argv
, optind
, 1);
2212 /* Treat --foo the same as -foo. */
2215 popt
= qemu_options
;
2218 error_report("invalid option");
2221 if (!strcmp(popt
->name
, r
+ 1))
2225 if (popt
->flags
& HAS_ARG
) {
2226 if (optind
>= argc
) {
2227 error_report("requires an argument");
2230 optarg
= argv
[optind
++];
2231 loc_set_cmdline(argv
, optind
- 2, 2);
2242 static gpointer
malloc_and_trace(gsize n_bytes
)
2244 void *ptr
= malloc(n_bytes
);
2245 trace_g_malloc(n_bytes
, ptr
);
2249 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2251 void *ptr
= realloc(mem
, n_bytes
);
2252 trace_g_realloc(mem
, n_bytes
, ptr
);
2256 static void free_and_trace(gpointer mem
)
2262 #ifdef CONFIG_KVM_OPTIONS
2263 int kvm_irqchip
= 1;
2265 int kvm_pit_reinject
= 1;
2268 int main(int argc
, char **argv
, char **envp
)
2270 const char *gdbstub_dev
= NULL
;
2272 int snapshot
, linux_boot
;
2273 const char *icount_option
= NULL
;
2274 const char *initrd_filename
;
2275 const char *kernel_filename
, *kernel_cmdline
;
2276 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2278 DisplayChangeListener
*dcl
;
2279 int cyls
, heads
, secs
, translation
;
2280 QemuOpts
*hda_opts
= NULL
, *opts
;
2281 QemuOptsList
*olist
;
2284 const char *loadvm
= NULL
;
2285 QEMUMachine
*machine
;
2286 const char *cpu_model
;
2287 const char *pid_file
= NULL
;
2288 const char *incoming
= NULL
;
2290 int show_vnc_port
= 0;
2293 const char *log_mask
= NULL
;
2294 const char *log_file
= NULL
;
2295 GMemVTable mem_trace
= {
2296 .malloc
= malloc_and_trace
,
2297 .realloc
= realloc_and_trace
,
2298 .free
= free_and_trace
,
2300 const char *trace_events
= NULL
;
2301 const char *trace_file
= NULL
;
2303 atexit(qemu_run_exit_notifiers
);
2304 error_set_progname(argv
[0]);
2306 g_mem_set_vtable(&mem_trace
);
2307 g_thread_init(NULL
);
2313 qemu_cache_utils_init(envp
);
2315 QLIST_INIT (&vm_change_state_head
);
2316 os_setup_early_signal_handling();
2318 module_call_init(MODULE_INIT_MACHINE
);
2319 machine
= find_default_machine();
2321 initrd_filename
= NULL
;
2324 kernel_filename
= NULL
;
2325 kernel_cmdline
= "";
2326 cyls
= heads
= secs
= 0;
2327 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2329 for (i
= 0; i
< MAX_NODES
; i
++) {
2331 node_cpumask
[i
] = 0;
2339 /* first pass of option parsing */
2341 while (optind
< argc
) {
2342 if (argv
[optind
][0] != '-') {
2347 const QEMUOption
*popt
;
2349 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2350 switch (popt
->index
) {
2351 case QEMU_OPTION_nodefconfig
:
2361 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2362 if (ret
< 0 && ret
!= -ENOENT
) {
2366 ret
= qemu_read_config_file(arch_config_name
);
2367 if (ret
< 0 && ret
!= -ENOENT
) {
2373 /* second pass of option parsing */
2378 if (argv
[optind
][0] != '-') {
2379 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2381 const QEMUOption
*popt
;
2383 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2384 if (!(popt
->arch_mask
& arch_type
)) {
2385 printf("Option %s not supported for this target\n", popt
->name
);
2388 switch(popt
->index
) {
2390 machine
= machine_parse(optarg
);
2392 case QEMU_OPTION_cpu
:
2393 /* hw initialization will check this */
2394 if (*optarg
== '?') {
2395 list_cpus(stdout
, &fprintf
, optarg
);
2401 case QEMU_OPTION_initrd
:
2402 initrd_filename
= optarg
;
2404 case QEMU_OPTION_hda
:
2408 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2410 snprintf(buf
, sizeof(buf
),
2411 "%s,cyls=%d,heads=%d,secs=%d%s",
2412 HD_OPTS
, cyls
, heads
, secs
,
2413 translation
== BIOS_ATA_TRANSLATION_LBA
?
2415 translation
== BIOS_ATA_TRANSLATION_NONE
?
2416 ",trans=none" : "");
2417 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2420 case QEMU_OPTION_hdb
:
2421 case QEMU_OPTION_hdc
:
2422 case QEMU_OPTION_hdd
:
2423 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2426 case QEMU_OPTION_drive
:
2427 if (drive_def(optarg
) == NULL
) {
2431 case QEMU_OPTION_set
:
2432 if (qemu_set_option(optarg
) != 0)
2435 case QEMU_OPTION_global
:
2436 if (qemu_global_option(optarg
) != 0)
2439 case QEMU_OPTION_mtdblock
:
2440 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2442 case QEMU_OPTION_sd
:
2443 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2445 case QEMU_OPTION_pflash
:
2446 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2448 case QEMU_OPTION_snapshot
:
2451 case QEMU_OPTION_hdachs
:
2455 cyls
= strtol(p
, (char **)&p
, 0);
2456 if (cyls
< 1 || cyls
> 16383)
2461 heads
= strtol(p
, (char **)&p
, 0);
2462 if (heads
< 1 || heads
> 16)
2467 secs
= strtol(p
, (char **)&p
, 0);
2468 if (secs
< 1 || secs
> 63)
2472 if (!strcmp(p
, "none"))
2473 translation
= BIOS_ATA_TRANSLATION_NONE
;
2474 else if (!strcmp(p
, "lba"))
2475 translation
= BIOS_ATA_TRANSLATION_LBA
;
2476 else if (!strcmp(p
, "auto"))
2477 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2480 } else if (*p
!= '\0') {
2482 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2485 if (hda_opts
!= NULL
) {
2487 snprintf(num
, sizeof(num
), "%d", cyls
);
2488 qemu_opt_set(hda_opts
, "cyls", num
);
2489 snprintf(num
, sizeof(num
), "%d", heads
);
2490 qemu_opt_set(hda_opts
, "heads", num
);
2491 snprintf(num
, sizeof(num
), "%d", secs
);
2492 qemu_opt_set(hda_opts
, "secs", num
);
2493 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2494 qemu_opt_set(hda_opts
, "trans", "lba");
2495 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2496 qemu_opt_set(hda_opts
, "trans", "none");
2500 case QEMU_OPTION_numa
:
2501 if (nb_numa_nodes
>= MAX_NODES
) {
2502 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2507 case QEMU_OPTION_display
:
2508 display_type
= select_display(optarg
);
2510 case QEMU_OPTION_nographic
:
2511 display_type
= DT_NOGRAPHIC
;
2513 case QEMU_OPTION_curses
:
2514 #ifdef CONFIG_CURSES
2515 display_type
= DT_CURSES
;
2517 fprintf(stderr
, "Curses support is disabled\n");
2521 case QEMU_OPTION_portrait
:
2522 graphic_rotate
= 90;
2524 case QEMU_OPTION_rotate
:
2525 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2526 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2527 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2529 "qemu: only 90, 180, 270 deg rotation is available\n");
2533 case QEMU_OPTION_kernel
:
2534 kernel_filename
= optarg
;
2536 case QEMU_OPTION_append
:
2537 kernel_cmdline
= optarg
;
2539 case QEMU_OPTION_cdrom
:
2540 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2542 case QEMU_OPTION_boot
:
2544 static const char * const params
[] = {
2545 "order", "once", "menu",
2546 "splash", "splash-time", NULL
2548 char buf
[sizeof(boot_devices
)];
2549 char *standard_boot_devices
;
2552 if (!strchr(optarg
, '=')) {
2554 pstrcpy(buf
, sizeof(buf
), optarg
);
2555 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2557 "qemu: unknown boot parameter '%s' in '%s'\n",
2563 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2564 validate_bootdevices(buf
);
2565 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2568 if (get_param_value(buf
, sizeof(buf
),
2570 validate_bootdevices(buf
);
2571 standard_boot_devices
= g_strdup(boot_devices
);
2572 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2573 qemu_register_reset(restore_boot_devices
,
2574 standard_boot_devices
);
2576 if (get_param_value(buf
, sizeof(buf
),
2578 if (!strcmp(buf
, "on")) {
2580 } else if (!strcmp(buf
, "off")) {
2584 "qemu: invalid option value '%s'\n",
2589 qemu_opts_parse(qemu_find_opts("boot-opts"),
2594 case QEMU_OPTION_fda
:
2595 case QEMU_OPTION_fdb
:
2596 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2599 case QEMU_OPTION_no_fd_bootchk
:
2602 case QEMU_OPTION_netdev
:
2603 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2607 case QEMU_OPTION_net
:
2608 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2613 case QEMU_OPTION_tftp
:
2614 legacy_tftp_prefix
= optarg
;
2616 case QEMU_OPTION_bootp
:
2617 legacy_bootp_filename
= optarg
;
2619 case QEMU_OPTION_redir
:
2620 if (net_slirp_redir(optarg
) < 0)
2624 case QEMU_OPTION_bt
:
2625 add_device_config(DEV_BT
, optarg
);
2627 case QEMU_OPTION_audio_help
:
2628 if (!(audio_available())) {
2629 printf("Option %s not supported for this target\n", popt
->name
);
2635 case QEMU_OPTION_soundhw
:
2636 if (!(audio_available())) {
2637 printf("Option %s not supported for this target\n", popt
->name
);
2640 select_soundhw (optarg
);
2645 case QEMU_OPTION_version
:
2649 case QEMU_OPTION_m
: {
2652 value
= strtosz(optarg
, NULL
);
2654 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2658 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2659 fprintf(stderr
, "qemu: ram size too large\n");
2665 case QEMU_OPTION_mempath
:
2669 case QEMU_OPTION_mem_prealloc
:
2680 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2682 case QEMU_OPTION_gdb
:
2683 gdbstub_dev
= optarg
;
2688 case QEMU_OPTION_bios
:
2691 case QEMU_OPTION_singlestep
:
2698 keyboard_layout
= optarg
;
2700 case QEMU_OPTION_localtime
:
2703 case QEMU_OPTION_vga
:
2704 select_vgahw (optarg
);
2711 w
= strtol(p
, (char **)&p
, 10);
2714 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2720 h
= strtol(p
, (char **)&p
, 10);
2725 depth
= strtol(p
, (char **)&p
, 10);
2726 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2727 depth
!= 24 && depth
!= 32)
2729 } else if (*p
== '\0') {
2730 depth
= graphic_depth
;
2737 graphic_depth
= depth
;
2740 case QEMU_OPTION_echr
:
2743 term_escape_char
= strtol(optarg
, &r
, 0);
2745 printf("Bad argument to echr\n");
2748 case QEMU_OPTION_monitor
:
2749 monitor_parse(optarg
, "readline");
2750 default_monitor
= 0;
2752 case QEMU_OPTION_qmp
:
2753 monitor_parse(optarg
, "control");
2754 default_monitor
= 0;
2756 case QEMU_OPTION_mon
:
2757 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2761 default_monitor
= 0;
2763 case QEMU_OPTION_chardev
:
2764 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2769 case QEMU_OPTION_fsdev
:
2770 olist
= qemu_find_opts("fsdev");
2772 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2775 opts
= qemu_opts_parse(olist
, optarg
, 1);
2777 fprintf(stderr
, "parse error: %s\n", optarg
);
2781 case QEMU_OPTION_virtfs
: {
2785 olist
= qemu_find_opts("virtfs");
2787 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2790 opts
= qemu_opts_parse(olist
, optarg
, 1);
2792 fprintf(stderr
, "parse error: %s\n", optarg
);
2796 if (qemu_opt_get(opts
, "fstype") == NULL
||
2797 qemu_opt_get(opts
, "mount_tag") == NULL
||
2798 qemu_opt_get(opts
, "path") == NULL
||
2799 qemu_opt_get(opts
, "security_model") == NULL
) {
2800 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2801 "security_model=[mapped|passthrough|none],"
2802 "mount_tag=tag.\n");
2806 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2807 qemu_opt_get(opts
, "mount_tag"), 1);
2809 fprintf(stderr
, "duplicate fsdev id: %s\n",
2810 qemu_opt_get(opts
, "mount_tag"));
2813 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2814 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2815 qemu_opt_set(fsdev
, "security_model",
2816 qemu_opt_get(opts
, "security_model"));
2818 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2819 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2820 qemu_opt_set(device
, "fsdev",
2821 qemu_opt_get(opts
, "mount_tag"));
2822 qemu_opt_set(device
, "mount_tag",
2823 qemu_opt_get(opts
, "mount_tag"));
2826 case QEMU_OPTION_serial
:
2827 add_device_config(DEV_SERIAL
, optarg
);
2829 if (strncmp(optarg
, "mon:", 4) == 0) {
2830 default_monitor
= 0;
2833 case QEMU_OPTION_watchdog
:
2836 "qemu: only one watchdog option may be given\n");
2841 case QEMU_OPTION_watchdog_action
:
2842 if (select_watchdog_action(optarg
) == -1) {
2843 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2847 case QEMU_OPTION_virtiocon
:
2848 add_device_config(DEV_VIRTCON
, optarg
);
2849 default_virtcon
= 0;
2850 if (strncmp(optarg
, "mon:", 4) == 0) {
2851 default_monitor
= 0;
2854 case QEMU_OPTION_parallel
:
2855 add_device_config(DEV_PARALLEL
, optarg
);
2856 default_parallel
= 0;
2857 if (strncmp(optarg
, "mon:", 4) == 0) {
2858 default_monitor
= 0;
2861 case QEMU_OPTION_debugcon
:
2862 add_device_config(DEV_DEBUGCON
, optarg
);
2864 case QEMU_OPTION_loadvm
:
2867 case QEMU_OPTION_full_screen
:
2871 case QEMU_OPTION_no_frame
:
2874 case QEMU_OPTION_alt_grab
:
2877 case QEMU_OPTION_ctrl_grab
:
2880 case QEMU_OPTION_no_quit
:
2883 case QEMU_OPTION_sdl
:
2884 display_type
= DT_SDL
;
2887 case QEMU_OPTION_no_frame
:
2888 case QEMU_OPTION_alt_grab
:
2889 case QEMU_OPTION_ctrl_grab
:
2890 case QEMU_OPTION_no_quit
:
2891 case QEMU_OPTION_sdl
:
2892 fprintf(stderr
, "SDL support is disabled\n");
2895 case QEMU_OPTION_pidfile
:
2898 case QEMU_OPTION_win2k_hack
:
2899 win2k_install_hack
= 1;
2901 case QEMU_OPTION_rtc_td_hack
:
2904 case QEMU_OPTION_acpitable
:
2905 do_acpitable_option(optarg
);
2907 case QEMU_OPTION_smbios
:
2908 do_smbios_option(optarg
);
2910 case QEMU_OPTION_enable_kvm
:
2911 olist
= qemu_find_opts("machine");
2912 qemu_opts_reset(olist
);
2913 qemu_opts_parse(olist
, "accel=kvm", 0);
2915 case QEMU_OPTION_machine
:
2916 olist
= qemu_find_opts("machine");
2917 qemu_opts_reset(olist
);
2918 opts
= qemu_opts_parse(olist
, optarg
, 1);
2920 fprintf(stderr
, "parse error: %s\n", optarg
);
2923 optarg
= qemu_opt_get(opts
, "type");
2925 machine
= machine_parse(optarg
);
2928 case QEMU_OPTION_no_kvm
:
2929 olist
= qemu_find_opts("machine");
2930 qemu_opts_reset(olist
);
2931 qemu_opts_parse(olist
, "accel=tcg", 0);
2933 #ifdef CONFIG_KVM_OPTIONS
2934 case QEMU_OPTION_no_kvm_irqchip
: {
2939 case QEMU_OPTION_no_kvm_pit
: {
2943 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2944 kvm_pit_reinject
= 0;
2948 case QEMU_OPTION_usb
:
2951 case QEMU_OPTION_usbdevice
:
2953 add_device_config(DEV_USB
, optarg
);
2955 case QEMU_OPTION_device
:
2956 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2960 case QEMU_OPTION_smp
:
2963 fprintf(stderr
, "Invalid number of CPUs\n");
2966 if (max_cpus
< smp_cpus
) {
2967 fprintf(stderr
, "maxcpus must be equal to or greater than "
2971 if (max_cpus
> 255) {
2972 fprintf(stderr
, "Unsupported number of maxcpus\n");
2976 case QEMU_OPTION_vnc
:
2979 vnc_display
= optarg
;
2981 fprintf(stderr
, "VNC support is disabled\n");
2985 case QEMU_OPTION_no_acpi
:
2988 case QEMU_OPTION_no_hpet
:
2991 case QEMU_OPTION_balloon
:
2992 if (balloon_parse(optarg
) < 0) {
2993 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2997 case QEMU_OPTION_no_reboot
:
3000 case QEMU_OPTION_no_shutdown
:
3003 case QEMU_OPTION_show_cursor
:
3006 case QEMU_OPTION_uuid
:
3007 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3008 fprintf(stderr
, "Fail to parse UUID string."
3009 " Wrong format.\n");
3013 case QEMU_OPTION_option_rom
:
3014 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3015 fprintf(stderr
, "Too many option ROMs\n");
3018 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3019 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3020 option_rom
[nb_option_roms
].bootindex
=
3021 qemu_opt_get_number(opts
, "bootindex", -1);
3022 if (!option_rom
[nb_option_roms
].name
) {
3023 fprintf(stderr
, "Option ROM file is not specified\n");
3028 case QEMU_OPTION_semihosting
:
3029 semihosting_enabled
= 1;
3031 case QEMU_OPTION_tdf
:
3034 case QEMU_OPTION_kvm_shadow_memory
:
3035 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3037 case QEMU_OPTION_name
:
3038 qemu_name
= g_strdup(optarg
);
3040 char *p
= strchr(qemu_name
, ',');
3043 if (strncmp(p
, "process=", 8)) {
3044 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3048 os_set_proc_name(p
);
3052 case QEMU_OPTION_prom_env
:
3053 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3054 fprintf(stderr
, "Too many prom variables\n");
3057 prom_envs
[nb_prom_envs
] = optarg
;
3060 case QEMU_OPTION_old_param
:
3063 case QEMU_OPTION_clock
:
3064 configure_alarms(optarg
);
3066 case QEMU_OPTION_startdate
:
3067 configure_rtc_date_offset(optarg
, 1);
3069 case QEMU_OPTION_rtc
:
3070 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3074 configure_rtc(opts
);
3076 case QEMU_OPTION_tb_size
:
3077 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3078 if (tcg_tb_size
< 0) {
3082 case QEMU_OPTION_icount
:
3083 icount_option
= optarg
;
3085 case QEMU_OPTION_incoming
:
3088 case QEMU_OPTION_nodefaults
:
3090 default_parallel
= 0;
3091 default_virtcon
= 0;
3092 default_monitor
= 0;
3099 case QEMU_OPTION_xen_domid
:
3100 if (!(xen_available())) {
3101 printf("Option %s not supported for this target\n", popt
->name
);
3104 xen_domid
= atoi(optarg
);
3106 case QEMU_OPTION_xen_create
:
3107 if (!(xen_available())) {
3108 printf("Option %s not supported for this target\n", popt
->name
);
3111 xen_mode
= XEN_CREATE
;
3113 case QEMU_OPTION_xen_attach
:
3114 if (!(xen_available())) {
3115 printf("Option %s not supported for this target\n", popt
->name
);
3118 xen_mode
= XEN_ATTACH
;
3120 case QEMU_OPTION_trace
:
3122 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3126 trace_events
= qemu_opt_get(opts
, "events");
3127 trace_file
= qemu_opt_get(opts
, "file");
3130 case QEMU_OPTION_readconfig
:
3132 int ret
= qemu_read_config_file(optarg
);
3134 fprintf(stderr
, "read config %s: %s\n", optarg
,
3140 case QEMU_OPTION_spice
:
3141 olist
= qemu_find_opts("spice");
3143 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3146 opts
= qemu_opts_parse(olist
, optarg
, 0);
3148 fprintf(stderr
, "parse error: %s\n", optarg
);
3152 case QEMU_OPTION_writeconfig
:
3155 if (strcmp(optarg
, "-") == 0) {
3158 fp
= fopen(optarg
, "w");
3160 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3164 qemu_config_write(fp
);
3169 os_parse_cmd_args(popt
->index
, optarg
);
3175 /* Open the logfile at this point, if necessary. We can't open the logfile
3176 * when encountering either of the logging options (-d or -D) because the
3177 * other one may be encountered later on the command line, changing the
3178 * location or level of logging.
3182 set_cpu_log_filename(log_file
);
3184 set_cpu_log(log_mask
);
3187 if (!trace_backend_init(trace_events
, trace_file
)) {
3191 /* If no data_dir is specified then try to find it relative to the
3194 data_dir
= os_find_datadir(argv
[0]);
3196 /* If all else fails use the install path specified when building. */
3198 data_dir
= CONFIG_QEMU_DATADIR
;
3202 * Default to max_cpus = smp_cpus, in case the user doesn't
3203 * specify a max_cpus value.
3206 max_cpus
= smp_cpus
;
3208 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3209 if (smp_cpus
> machine
->max_cpus
) {
3210 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3211 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3217 * Get the default machine options from the machine if it is not already
3218 * specified either by the configuration file or by the command line.
3220 if (machine
->default_machine_opts
) {
3221 QemuOptsList
*list
= qemu_find_opts("machine");
3222 const char *p
= NULL
;
3224 if (!QTAILQ_EMPTY(&list
->head
)) {
3225 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3228 qemu_opts_reset(list
);
3229 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3231 fprintf(stderr
, "parse error for machine %s: %s\n",
3232 machine
->name
, machine
->default_machine_opts
);
3238 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3239 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3241 if (machine
->no_serial
) {
3244 if (machine
->no_parallel
) {
3245 default_parallel
= 0;
3247 if (!machine
->use_virtcon
) {
3248 default_virtcon
= 0;
3250 if (machine
->no_vga
) {
3253 if (machine
->no_floppy
) {
3256 if (machine
->no_cdrom
) {
3259 if (machine
->no_sdcard
) {
3263 if (display_type
== DT_NOGRAPHIC
) {
3264 if (default_parallel
)
3265 add_device_config(DEV_PARALLEL
, "null");
3266 if (default_serial
&& default_monitor
) {
3267 add_device_config(DEV_SERIAL
, "mon:stdio");
3268 } else if (default_virtcon
&& default_monitor
) {
3269 add_device_config(DEV_VIRTCON
, "mon:stdio");
3272 add_device_config(DEV_SERIAL
, "stdio");
3273 if (default_virtcon
)
3274 add_device_config(DEV_VIRTCON
, "stdio");
3275 if (default_monitor
)
3276 monitor_parse("stdio", "readline");
3280 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3281 if (default_parallel
)
3282 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3283 if (default_monitor
)
3284 monitor_parse("vc:80Cx24C", "readline");
3285 if (default_virtcon
)
3286 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3289 vga_interface_type
= VGA_CIRRUS
;
3293 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3295 #ifdef CONFIG_VIRTFS
3296 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3303 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3308 /* init the memory */
3309 if (ram_size
== 0) {
3310 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3313 configure_accelerator();
3315 if (qemu_init_main_loop()) {
3316 fprintf(stderr
, "qemu_init_main_loop failed\n");
3319 linux_boot
= (kernel_filename
!= NULL
);
3321 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3322 fprintf(stderr
, "-append only allowed with -kernel option\n");
3326 if (!linux_boot
&& initrd_filename
!= NULL
) {
3327 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3331 os_set_line_buffering();
3333 if (init_timer_alarm() < 0) {
3334 fprintf(stderr
, "could not initialize alarm timer\n");
3337 configure_icount(icount_option
);
3339 if (net_init_clients() < 0) {
3343 /* init the bluetooth world */
3344 if (foreach_device_config(DEV_BT
, bt_parse
))
3347 if (!xen_enabled()) {
3348 /* On 32-bit hosts, QEMU is limited by virtual address space */
3349 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3350 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3355 cpu_exec_init_all();
3357 bdrv_init_with_whitelist();
3361 /* open the virtual block devices */
3363 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3364 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3367 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3368 IF_DEFAULT
, 2, CDROM_OPTS
);
3369 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3370 IF_FLOPPY
, 0, FD_OPTS
);
3371 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3374 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3377 if (nb_numa_nodes
> 0) {
3380 if (nb_numa_nodes
> MAX_NODES
) {
3381 nb_numa_nodes
= MAX_NODES
;
3384 /* If no memory size if given for any node, assume the default case
3385 * and distribute the available memory equally across all nodes
3387 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3388 if (node_mem
[i
] != 0)
3391 if (i
== nb_numa_nodes
) {
3392 uint64_t usedmem
= 0;
3394 /* On Linux, the each node's border has to be 8MB aligned,
3395 * the final node gets the rest.
3397 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3398 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3399 usedmem
+= node_mem
[i
];
3401 node_mem
[i
] = ram_size
- usedmem
;
3404 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3405 if (node_cpumask
[i
] != 0)
3408 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3409 * must cope with this anyway, because there are BIOSes out there in
3410 * real machines which also use this scheme.
3412 if (i
== nb_numa_nodes
) {
3413 for (i
= 0; i
< smp_cpus
; i
++) {
3414 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3419 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3423 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3425 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3427 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3429 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3432 module_call_init(MODULE_INIT_DEVICE
);
3434 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3438 i
= select_watchdog(watchdog
);
3440 exit (i
== 1 ? 1 : 0);
3443 if (machine
->compat_props
) {
3444 qdev_prop_register_global_list(machine
->compat_props
);
3448 machine
->init(ram_size
, boot_devices
,
3449 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3451 cpu_synchronize_all_post_init();
3455 current_machine
= machine
;
3457 /* init USB devices */
3459 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3463 /* init generic devices */
3464 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3467 net_check_clients();
3469 /* just use the first displaystate for the moment */
3470 ds
= get_displaystate();
3474 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3475 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3476 display_type
= DT_SDL
;
3477 #elif defined(CONFIG_VNC)
3478 vnc_display
= "localhost:0,to=99";
3481 display_type
= DT_NONE
;
3486 /* init local displays */
3487 switch (display_type
) {
3490 #if defined(CONFIG_CURSES)
3492 curses_display_init(ds
, full_screen
);
3495 #if defined(CONFIG_SDL)
3497 sdl_display_init(ds
, full_screen
, no_frame
);
3499 #elif defined(CONFIG_COCOA)
3501 cocoa_display_init(ds
, full_screen
);
3508 /* must be after terminal init, SDL library changes signal handlers */
3509 os_setup_signal_handling();
3512 /* init remote displays */
3514 vnc_display_init(ds
);
3515 if (vnc_display_open(ds
, vnc_display
) < 0)
3518 if (show_vnc_port
) {
3519 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3524 if (using_spice
&& !qxl_enabled
) {
3525 qemu_spice_display_init(ds
);
3531 dcl
= ds
->listeners
;
3532 while (dcl
!= NULL
) {
3533 if (dcl
->dpy_refresh
!= NULL
) {
3534 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3535 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3540 text_consoles_set_display(ds
);
3542 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3543 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3548 qdev_machine_creation_done();
3550 if (rom_load_all() != 0) {
3551 fprintf(stderr
, "rom loading failed\n");
3555 /* TODO: once all bus devices are qdevified, this should be done
3556 * when bus is created by qdev.c */
3557 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3558 qemu_run_machine_init_done_notifiers();
3560 qemu_system_reset(VMRESET_SILENT
);
3562 if (load_vmstate(loadvm
) < 0) {
3568 runstate_set(RUN_STATE_INMIGRATE
);
3569 int ret
= qemu_start_incoming_migration(incoming
);
3571 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3575 } else if (autostart
) {