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 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint8_t *boot_splash_filedata
;
233 int boot_splash_filedata_size
;
234 uint8_t qemu_extra_params_fw
[2];
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 uint8_t qemu_uuid
[16];
253 static QEMUBootSetHandler
*boot_set_handler
;
254 static void *boot_set_opaque
;
256 static NotifierList exit_notifiers
=
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
259 static NotifierList machine_init_done_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 static int tcg_allowed
= 1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
267 static int tcg_tb_size
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_vga
= 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
},
292 { .driver
= "VGA", .flag
= &default_vga
},
293 { .driver
= "cirrus-vga", .flag
= &default_vga
},
294 { .driver
= "vmware-svga", .flag
= &default_vga
},
295 { .driver
= "isa-vga", .flag
= &default_vga
},
296 { .driver
= "qxl-vga", .flag
= &default_vga
},
299 static void res_free(void)
301 if (boot_splash_filedata
!= NULL
) {
302 g_free(boot_splash_filedata
);
303 boot_splash_filedata
= NULL
;
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
325 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
330 } RunStateTransition
;
332 static const RunStateTransition runstate_transitions_def
[] = {
334 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
336 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
337 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
339 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
340 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
342 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
343 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
345 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
346 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
348 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
349 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
351 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
352 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
353 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
355 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
356 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
358 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
360 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
361 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
362 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
363 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
364 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
365 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
366 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
367 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
368 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
370 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
372 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
373 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
375 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
376 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
378 { RUN_STATE_MAX
, RUN_STATE_MAX
},
381 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
383 bool runstate_check(RunState state
)
385 return current_run_state
== state
;
388 void runstate_init(void)
390 const RunStateTransition
*p
;
392 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
394 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
395 runstate_valid_transitions
[p
->from
][p
->to
] = true;
399 /* This function will abort() on invalid state transitions */
400 void runstate_set(RunState new_state
)
402 assert(new_state
< RUN_STATE_MAX
);
404 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
405 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
406 RunState_lookup
[current_run_state
],
407 RunState_lookup
[new_state
]);
411 current_run_state
= new_state
;
414 int runstate_is_running(void)
416 return runstate_check(RUN_STATE_RUNNING
);
419 StatusInfo
*qmp_query_status(Error
**errp
)
421 StatusInfo
*info
= g_malloc0(sizeof(*info
));
423 info
->running
= runstate_is_running();
424 info
->singlestep
= singlestep
;
425 info
->status
= current_run_state
;
430 /***********************************************************/
431 /* real time host monotonic timer */
433 /***********************************************************/
434 /* host time/date access */
435 void qemu_get_timedate(struct tm
*tm
, int offset
)
442 if (rtc_date_offset
== -1) {
446 ret
= localtime(&ti
);
448 ti
-= rtc_date_offset
;
452 memcpy(tm
, ret
, sizeof(struct tm
));
455 int qemu_timedate_diff(struct tm
*tm
)
459 if (rtc_date_offset
== -1)
461 seconds
= mktimegm(tm
);
463 seconds
= mktime(tm
);
465 seconds
= mktimegm(tm
) + rtc_date_offset
;
467 return seconds
- time(NULL
);
470 void rtc_change_mon_event(struct tm
*tm
)
474 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
475 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
476 qobject_decref(data
);
479 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
481 time_t rtc_start_date
;
484 if (!strcmp(startdate
, "now") && legacy
) {
485 rtc_date_offset
= -1;
487 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
495 } else if (sscanf(startdate
, "%d-%d-%d",
507 rtc_start_date
= mktimegm(&tm
);
508 if (rtc_start_date
== -1) {
510 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
511 "'2006-06-17T16:01:21' or '2006-06-17'\n");
514 rtc_date_offset
= time(NULL
) - rtc_start_date
;
518 static void configure_rtc(QemuOpts
*opts
)
522 value
= qemu_opt_get(opts
, "base");
524 if (!strcmp(value
, "utc")) {
526 } else if (!strcmp(value
, "localtime")) {
529 configure_rtc_date_offset(value
, 0);
532 value
= qemu_opt_get(opts
, "clock");
534 if (!strcmp(value
, "host")) {
535 rtc_clock
= host_clock
;
536 } else if (!strcmp(value
, "vm")) {
537 rtc_clock
= vm_clock
;
539 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
543 value
= qemu_opt_get(opts
, "driftfix");
545 if (!strcmp(value
, "slew")) {
547 } else if (!strcmp(value
, "none")) {
550 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
556 /***********************************************************/
557 /* Bluetooth support */
560 static struct HCIInfo
*hci_table
[MAX_NICS
];
562 static struct bt_vlan_s
{
563 struct bt_scatternet_s net
;
565 struct bt_vlan_s
*next
;
568 /* find or alloc a new bluetooth "VLAN" */
569 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
571 struct bt_vlan_s
**pvlan
, *vlan
;
572 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
576 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
578 pvlan
= &first_bt_vlan
;
579 while (*pvlan
!= NULL
)
580 pvlan
= &(*pvlan
)->next
;
585 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
589 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
594 static struct HCIInfo null_hci
= {
595 .cmd_send
= null_hci_send
,
596 .sco_send
= null_hci_send
,
597 .acl_send
= null_hci_send
,
598 .bdaddr_set
= null_hci_addr_set
,
601 struct HCIInfo
*qemu_next_hci(void)
603 if (cur_hci
== nb_hcis
)
606 return hci_table
[cur_hci
++];
609 static struct HCIInfo
*hci_init(const char *str
)
612 struct bt_scatternet_s
*vlan
= 0;
614 if (!strcmp(str
, "null"))
617 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
619 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
620 else if (!strncmp(str
, "hci", 3)) {
623 if (!strncmp(str
+ 3, ",vlan=", 6)) {
624 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
629 vlan
= qemu_find_bt_vlan(0);
631 return bt_new_hci(vlan
);
634 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
639 static int bt_hci_parse(const char *str
)
644 if (nb_hcis
>= MAX_NICS
) {
645 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
658 bdaddr
.b
[5] = 0x56 + nb_hcis
;
659 hci
->bdaddr_set(hci
, bdaddr
.b
);
661 hci_table
[nb_hcis
++] = hci
;
666 static void bt_vhci_add(int vlan_id
)
668 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
671 fprintf(stderr
, "qemu: warning: adding a VHCI to "
672 "an empty scatternet %i\n", vlan_id
);
674 bt_vhci_init(bt_new_hci(vlan
));
677 static struct bt_device_s
*bt_device_add(const char *opt
)
679 struct bt_scatternet_s
*vlan
;
681 char *endp
= strstr(opt
, ",vlan=");
682 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
685 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
688 vlan_id
= strtol(endp
+ 6, &endp
, 0);
690 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
695 vlan
= qemu_find_bt_vlan(vlan_id
);
698 fprintf(stderr
, "qemu: warning: adding a slave device to "
699 "an empty scatternet %i\n", vlan_id
);
701 if (!strcmp(devname
, "keyboard"))
702 return bt_keyboard_init(vlan
);
704 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
708 static int bt_parse(const char *opt
)
710 const char *endp
, *p
;
713 if (strstart(opt
, "hci", &endp
)) {
714 if (!*endp
|| *endp
== ',') {
716 if (!strstart(endp
, ",vlan=", 0))
719 return bt_hci_parse(opt
);
721 } else if (strstart(opt
, "vhci", &endp
)) {
722 if (!*endp
|| *endp
== ',') {
724 if (strstart(endp
, ",vlan=", &p
)) {
725 vlan
= strtol(p
, (char **) &endp
, 0);
727 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
731 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
740 } else if (strstart(opt
, "device:", &endp
))
741 return !bt_device_add(endp
);
743 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
747 /***********************************************************/
748 /* QEMU Block devices */
750 #define HD_OPTS "media=disk"
751 #define CDROM_OPTS "media=cdrom"
753 #define PFLASH_OPTS ""
757 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
759 int *use_scsi
= opaque
;
761 return drive_init(opts
, *use_scsi
) == NULL
;
764 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
766 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
767 qemu_opt_set(opts
, "snapshot", "on");
772 static void default_drive(int enable
, int snapshot
, int use_scsi
,
773 BlockInterfaceType type
, int index
,
778 if (type
== IF_DEFAULT
) {
779 type
= use_scsi
? IF_SCSI
: IF_IDE
;
782 if (!enable
|| drive_get_by_index(type
, index
)) {
786 opts
= drive_add(type
, index
, NULL
, optstr
);
788 drive_enable_snapshot(opts
, NULL
);
790 if (!drive_init(opts
, use_scsi
)) {
795 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
797 boot_set_handler
= func
;
798 boot_set_opaque
= opaque
;
801 int qemu_boot_set(const char *boot_devices
)
803 if (!boot_set_handler
) {
806 return boot_set_handler(boot_set_opaque
, boot_devices
);
809 static void validate_bootdevices(char *devices
)
811 /* We just do some generic consistency checks */
815 for (p
= devices
; *p
!= '\0'; p
++) {
816 /* Allowed boot devices are:
817 * a-b: floppy disk drives
818 * c-f: IDE disk drives
819 * g-m: machine implementation dependant drives
820 * n-p: network devices
821 * It's up to each machine implementation to check if the given boot
822 * devices match the actual hardware implementation and firmware
825 if (*p
< 'a' || *p
> 'p') {
826 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
829 if (bitmap
& (1 << (*p
- 'a'))) {
830 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
833 bitmap
|= 1 << (*p
- 'a');
837 static void restore_boot_devices(void *opaque
)
839 char *standard_boot_devices
= opaque
;
840 static int first
= 1;
842 /* Restore boot order and remove ourselves after the first boot */
848 qemu_boot_set(standard_boot_devices
);
850 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
851 g_free(standard_boot_devices
);
854 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
857 FWBootEntry
*node
, *i
;
863 assert(dev
!= NULL
|| suffix
!= NULL
);
865 node
= g_malloc0(sizeof(FWBootEntry
));
866 node
->bootindex
= bootindex
;
867 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
870 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
871 if (i
->bootindex
== bootindex
) {
872 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
874 } else if (i
->bootindex
< bootindex
) {
877 QTAILQ_INSERT_BEFORE(i
, node
, link
);
880 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
884 * This function returns null terminated string that consist of new line
885 * separated device paths.
887 * memory pointed by "size" is assigned total length of the array in bytes
890 char *get_boot_devices_list(uint32_t *size
)
896 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
897 char *devpath
= NULL
, *bootpath
;
901 devpath
= qdev_get_fw_dev_path(i
->dev
);
905 if (i
->suffix
&& devpath
) {
906 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
908 bootpath
= g_malloc(bootpathlen
);
909 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
911 } else if (devpath
) {
914 bootpath
= g_strdup(i
->suffix
);
919 list
[total
-1] = '\n';
921 len
= strlen(bootpath
) + 1;
922 list
= g_realloc(list
, total
+ len
);
923 memcpy(&list
[total
], bootpath
, len
);
933 static void numa_add(const char *optarg
)
937 unsigned long long value
, endvalue
;
940 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
941 if (!strcmp(option
, "node")) {
942 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
943 nodenr
= nb_numa_nodes
;
945 nodenr
= strtoull(option
, NULL
, 10);
948 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
949 node_mem
[nodenr
] = 0;
952 sval
= strtosz(option
, NULL
);
954 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
957 node_mem
[nodenr
] = sval
;
959 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
960 node_cpumask
[nodenr
] = 0;
962 value
= strtoull(option
, &endptr
, 10);
965 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
967 if (*endptr
== '-') {
968 endvalue
= strtoull(endptr
+1, &endptr
, 10);
969 if (endvalue
>= 63) {
972 "only 63 CPUs in NUMA mode supported.\n");
974 value
= (2ULL << endvalue
) - (1ULL << value
);
976 value
= 1ULL << value
;
979 node_cpumask
[nodenr
] = value
;
986 static void smp_parse(const char *optarg
)
988 int smp
, sockets
= 0, threads
= 0, cores
= 0;
992 smp
= strtoul(optarg
, &endptr
, 10);
993 if (endptr
!= optarg
) {
994 if (*endptr
== ',') {
998 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
999 sockets
= strtoull(option
, NULL
, 10);
1000 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1001 cores
= strtoull(option
, NULL
, 10);
1002 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1003 threads
= strtoull(option
, NULL
, 10);
1004 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1005 max_cpus
= strtoull(option
, NULL
, 10);
1007 /* compute missing values, prefer sockets over cores over threads */
1008 if (smp
== 0 || sockets
== 0) {
1009 sockets
= sockets
> 0 ? sockets
: 1;
1010 cores
= cores
> 0 ? cores
: 1;
1011 threads
= threads
> 0 ? threads
: 1;
1013 smp
= cores
* threads
* sockets
;
1017 threads
= threads
> 0 ? threads
: 1;
1018 cores
= smp
/ (sockets
* threads
);
1020 threads
= smp
/ (cores
* sockets
);
1024 smp_cores
= cores
> 0 ? cores
: 1;
1025 smp_threads
= threads
> 0 ? threads
: 1;
1027 max_cpus
= smp_cpus
;
1030 /***********************************************************/
1033 static int usb_device_add(const char *devname
)
1036 USBDevice
*dev
= NULL
;
1041 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1042 dev
= usbdevice_create(devname
);
1046 /* the other ones */
1047 #ifndef CONFIG_LINUX
1048 /* only the linux version is qdev-ified, usb-bsd still needs this */
1049 if (strstart(devname
, "host:", &p
)) {
1050 dev
= usb_host_device_open(p
);
1053 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1054 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1055 bt_new_hci(qemu_find_bt_vlan(0)));
1066 static int usb_device_del(const char *devname
)
1071 if (strstart(devname
, "host:", &p
))
1072 return usb_host_device_close(p
);
1077 p
= strchr(devname
, '.');
1080 bus_num
= strtoul(devname
, NULL
, 0);
1081 addr
= strtoul(p
+ 1, NULL
, 0);
1083 return usb_device_delete_addr(bus_num
, addr
);
1086 static int usb_parse(const char *cmdline
)
1089 r
= usb_device_add(cmdline
);
1091 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1096 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1098 const char *devname
= qdict_get_str(qdict
, "devname");
1099 if (usb_device_add(devname
) < 0) {
1100 error_report("could not add USB device '%s'", devname
);
1104 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1106 const char *devname
= qdict_get_str(qdict
, "devname");
1107 if (usb_device_del(devname
) < 0) {
1108 error_report("could not delete USB device '%s'", devname
);
1112 /***********************************************************/
1113 /* PCMCIA/Cardbus */
1115 static struct pcmcia_socket_entry_s
{
1116 PCMCIASocket
*socket
;
1117 struct pcmcia_socket_entry_s
*next
;
1118 } *pcmcia_sockets
= 0;
1120 void pcmcia_socket_register(PCMCIASocket
*socket
)
1122 struct pcmcia_socket_entry_s
*entry
;
1124 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1125 entry
->socket
= socket
;
1126 entry
->next
= pcmcia_sockets
;
1127 pcmcia_sockets
= entry
;
1130 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1132 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1134 ptr
= &pcmcia_sockets
;
1135 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1136 if (entry
->socket
== socket
) {
1142 void pcmcia_info(Monitor
*mon
)
1144 struct pcmcia_socket_entry_s
*iter
;
1146 if (!pcmcia_sockets
)
1147 monitor_printf(mon
, "No PCMCIA sockets\n");
1149 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1150 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1151 iter
->socket
->attached
? iter
->socket
->card_string
:
1155 /***********************************************************/
1156 /* machine registration */
1158 static QEMUMachine
*first_machine
= NULL
;
1159 QEMUMachine
*current_machine
= NULL
;
1161 int qemu_register_machine(QEMUMachine
*m
)
1164 pm
= &first_machine
;
1172 static QEMUMachine
*find_machine(const char *name
)
1176 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1177 if (!strcmp(m
->name
, name
))
1179 if (m
->alias
&& !strcmp(m
->alias
, name
))
1185 static QEMUMachine
*find_default_machine(void)
1189 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1190 if (m
->is_default
) {
1197 /***********************************************************/
1198 /* main execution loop */
1200 static void gui_update(void *opaque
)
1202 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1203 DisplayState
*ds
= opaque
;
1204 DisplayChangeListener
*dcl
= ds
->listeners
;
1208 while (dcl
!= NULL
) {
1209 if (dcl
->gui_timer_interval
&&
1210 dcl
->gui_timer_interval
< interval
)
1211 interval
= dcl
->gui_timer_interval
;
1214 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1217 struct vm_change_state_entry
{
1218 VMChangeStateHandler
*cb
;
1220 QLIST_ENTRY (vm_change_state_entry
) entries
;
1223 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1225 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1228 VMChangeStateEntry
*e
;
1230 e
= g_malloc0(sizeof (*e
));
1234 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1238 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1240 QLIST_REMOVE (e
, entries
);
1244 void vm_state_notify(int running
, RunState state
)
1246 VMChangeStateEntry
*e
;
1248 trace_vm_state_notify(running
, state
);
1250 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1251 e
->cb(e
->opaque
, running
, state
);
1257 if (!runstate_is_running()) {
1259 runstate_set(RUN_STATE_RUNNING
);
1260 vm_state_notify(1, RUN_STATE_RUNNING
);
1262 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1266 /* reset/shutdown handler */
1268 typedef struct QEMUResetEntry
{
1269 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1270 QEMUResetHandler
*func
;
1274 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1275 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1276 static int reset_requested
;
1277 static int shutdown_requested
, shutdown_signal
= -1;
1278 static pid_t shutdown_pid
;
1279 static int powerdown_requested
;
1280 static int debug_requested
;
1281 static RunState vmstop_requested
= RUN_STATE_MAX
;
1283 int qemu_shutdown_requested_get(void)
1285 return shutdown_requested
;
1288 int qemu_reset_requested_get(void)
1290 return reset_requested
;
1293 int qemu_shutdown_requested(void)
1295 int r
= shutdown_requested
;
1296 shutdown_requested
= 0;
1300 void qemu_kill_report(void)
1302 if (shutdown_signal
!= -1) {
1303 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1304 if (shutdown_pid
== 0) {
1305 /* This happens for eg ^C at the terminal, so it's worth
1306 * avoiding printing an odd message in that case.
1308 fputc('\n', stderr
);
1310 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1312 shutdown_signal
= -1;
1316 int qemu_reset_requested(void)
1318 int r
= reset_requested
;
1319 reset_requested
= 0;
1323 int qemu_powerdown_requested(void)
1325 int r
= powerdown_requested
;
1326 powerdown_requested
= 0;
1330 static int qemu_debug_requested(void)
1332 int r
= debug_requested
;
1333 debug_requested
= 0;
1337 /* We use RUN_STATE_MAX but any invalid value will do */
1338 static bool qemu_vmstop_requested(RunState
*r
)
1340 if (vmstop_requested
< RUN_STATE_MAX
) {
1341 *r
= vmstop_requested
;
1342 vmstop_requested
= RUN_STATE_MAX
;
1349 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1351 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1354 re
->opaque
= opaque
;
1355 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1358 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1362 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1363 if (re
->func
== func
&& re
->opaque
== opaque
) {
1364 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1371 void qemu_system_reset(bool report
)
1373 QEMUResetEntry
*re
, *nre
;
1375 /* reset all devices */
1376 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1377 re
->func(re
->opaque
);
1380 monitor_protocol_event(QEVENT_RESET
, NULL
);
1382 cpu_synchronize_all_post_reset();
1385 void qemu_system_reset_request(void)
1388 shutdown_requested
= 1;
1390 reset_requested
= 1;
1393 qemu_notify_event();
1396 void qemu_system_killed(int signal
, pid_t pid
)
1398 shutdown_signal
= signal
;
1401 qemu_system_shutdown_request();
1404 void qemu_system_shutdown_request(void)
1406 shutdown_requested
= 1;
1407 qemu_notify_event();
1410 void qemu_system_powerdown_request(void)
1412 powerdown_requested
= 1;
1413 qemu_notify_event();
1416 void qemu_system_debug_request(void)
1418 debug_requested
= 1;
1419 qemu_notify_event();
1422 void qemu_system_vmstop_request(RunState state
)
1424 vmstop_requested
= state
;
1425 qemu_notify_event();
1428 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1429 static int n_poll_fds
;
1430 static int max_priority
;
1432 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1433 fd_set
*xfds
, struct timeval
*tv
)
1435 GMainContext
*context
= g_main_context_default();
1437 int timeout
= 0, cur_timeout
;
1439 g_main_context_prepare(context
, &max_priority
);
1441 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1442 poll_fds
, ARRAY_SIZE(poll_fds
));
1443 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1445 for (i
= 0; i
< n_poll_fds
; i
++) {
1446 GPollFD
*p
= &poll_fds
[i
];
1448 if ((p
->events
& G_IO_IN
)) {
1449 FD_SET(p
->fd
, rfds
);
1450 *max_fd
= MAX(*max_fd
, p
->fd
);
1452 if ((p
->events
& G_IO_OUT
)) {
1453 FD_SET(p
->fd
, wfds
);
1454 *max_fd
= MAX(*max_fd
, p
->fd
);
1456 if ((p
->events
& G_IO_ERR
)) {
1457 FD_SET(p
->fd
, xfds
);
1458 *max_fd
= MAX(*max_fd
, p
->fd
);
1462 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1463 if (timeout
>= 0 && timeout
< cur_timeout
) {
1464 tv
->tv_sec
= timeout
/ 1000;
1465 tv
->tv_usec
= (timeout
% 1000) * 1000;
1469 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1472 GMainContext
*context
= g_main_context_default();
1477 for (i
= 0; i
< n_poll_fds
; i
++) {
1478 GPollFD
*p
= &poll_fds
[i
];
1480 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1481 p
->revents
|= G_IO_IN
;
1483 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1484 p
->revents
|= G_IO_OUT
;
1486 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1487 p
->revents
|= G_IO_ERR
;
1492 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1493 g_main_context_dispatch(context
);
1497 int main_loop_wait(int nonblocking
)
1499 fd_set rfds
, wfds
, xfds
;
1507 timeout
= qemu_calculate_timeout();
1508 qemu_bh_update_timeout(&timeout
);
1511 os_host_main_loop_wait(&timeout
);
1513 tv
.tv_sec
= timeout
/ 1000;
1514 tv
.tv_usec
= (timeout
% 1000) * 1000;
1516 /* poll any events */
1517 /* XXX: separate device handlers from system ones */
1523 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1524 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1525 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1528 qemu_mutex_unlock_iothread();
1531 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1534 qemu_mutex_lock_iothread();
1537 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1538 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1539 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1541 qemu_run_all_timers();
1543 /* Check bottom-halves last in case any of the earlier events triggered
1550 qemu_irq qemu_system_powerdown
;
1552 static void main_loop(void)
1555 int last_io
__attribute__ ((unused
)) = 0;
1556 #ifdef CONFIG_PROFILER
1561 qemu_main_loop_start();
1564 nonblocking
= !kvm_enabled() && last_io
> 0;
1565 #ifdef CONFIG_PROFILER
1566 ti
= profile_getclock();
1568 last_io
= main_loop_wait(nonblocking
);
1569 #ifdef CONFIG_PROFILER
1570 dev_time
+= profile_getclock() - ti
;
1573 if (qemu_debug_requested()) {
1574 vm_stop(RUN_STATE_DEBUG
);
1576 if (qemu_shutdown_requested()) {
1578 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1580 vm_stop(RUN_STATE_SHUTDOWN
);
1584 if (qemu_reset_requested()) {
1586 cpu_synchronize_all_states();
1587 qemu_system_reset(VMRESET_REPORT
);
1589 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1590 runstate_check(RUN_STATE_SHUTDOWN
)) {
1591 runstate_set(RUN_STATE_PAUSED
);
1594 if (qemu_powerdown_requested()) {
1595 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1596 qemu_irq_raise(qemu_system_powerdown
);
1598 if (qemu_vmstop_requested(&r
)) {
1606 static void version(void)
1608 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1611 static void help(int exitcode
)
1613 const char *options_help
=
1614 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1616 #define DEFHEADING(text) stringify(text) "\n"
1617 #include "qemu-options.def"
1623 printf("usage: %s [options] [disk_image]\n"
1625 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1628 "During emulation, the following keys are useful:\n"
1629 "ctrl-alt-f toggle full screen\n"
1630 "ctrl-alt-n switch to virtual console 'n'\n"
1631 "ctrl-alt toggle mouse and keyboard grab\n"
1633 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1639 #define HAS_ARG 0x0001
1641 typedef struct QEMUOption
{
1648 static const QEMUOption qemu_options
[] = {
1649 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1650 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1651 { option, opt_arg, opt_enum, arch_mask },
1652 #define DEFHEADING(text)
1653 #include "qemu-options.def"
1659 static void select_vgahw (const char *p
)
1664 vga_interface_type
= VGA_NONE
;
1665 if (strstart(p
, "std", &opts
)) {
1666 vga_interface_type
= VGA_STD
;
1667 } else if (strstart(p
, "cirrus", &opts
)) {
1668 vga_interface_type
= VGA_CIRRUS
;
1669 } else if (strstart(p
, "vmware", &opts
)) {
1670 vga_interface_type
= VGA_VMWARE
;
1671 } else if (strstart(p
, "xenfb", &opts
)) {
1672 vga_interface_type
= VGA_XENFB
;
1673 } else if (strstart(p
, "qxl", &opts
)) {
1674 vga_interface_type
= VGA_QXL
;
1675 } else if (!strstart(p
, "none", &opts
)) {
1677 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1681 const char *nextopt
;
1683 if (strstart(opts
, ",retrace=", &nextopt
)) {
1685 if (strstart(opts
, "dumb", &nextopt
))
1686 vga_retrace_method
= VGA_RETRACE_DUMB
;
1687 else if (strstart(opts
, "precise", &nextopt
))
1688 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1689 else goto invalid_vga
;
1690 } else goto invalid_vga
;
1695 static DisplayType
select_display(const char *p
)
1698 DisplayType display
= DT_DEFAULT
;
1700 if (strstart(p
, "sdl", &opts
)) {
1704 const char *nextopt
;
1706 if (strstart(opts
, ",frame=", &nextopt
)) {
1708 if (strstart(opts
, "on", &nextopt
)) {
1710 } else if (strstart(opts
, "off", &nextopt
)) {
1713 goto invalid_sdl_args
;
1715 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1717 if (strstart(opts
, "on", &nextopt
)) {
1719 } else if (strstart(opts
, "off", &nextopt
)) {
1722 goto invalid_sdl_args
;
1724 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1726 if (strstart(opts
, "on", &nextopt
)) {
1728 } else if (strstart(opts
, "off", &nextopt
)) {
1731 goto invalid_sdl_args
;
1733 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1735 if (strstart(opts
, "on", &nextopt
)) {
1737 } else if (strstart(opts
, "off", &nextopt
)) {
1740 goto invalid_sdl_args
;
1744 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1750 fprintf(stderr
, "SDL support is disabled\n");
1753 } else if (strstart(p
, "vnc", &opts
)) {
1758 const char *nextopt
;
1760 if (strstart(opts
, "=", &nextopt
)) {
1761 vnc_display
= nextopt
;
1765 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1769 fprintf(stderr
, "VNC support is disabled\n");
1772 } else if (strstart(p
, "curses", &opts
)) {
1773 #ifdef CONFIG_CURSES
1774 display
= DT_CURSES
;
1776 fprintf(stderr
, "Curses support is disabled\n");
1779 } else if (strstart(p
, "none", &opts
)) {
1782 fprintf(stderr
, "Unknown display type: %s\n", p
);
1789 static int balloon_parse(const char *arg
)
1793 if (strcmp(arg
, "none") == 0) {
1797 if (!strncmp(arg
, "virtio", 6)) {
1798 if (arg
[6] == ',') {
1799 /* have params -> parse them */
1800 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1804 /* create empty opts */
1805 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1807 qemu_opt_set(opts
, "driver", "virtio-balloon");
1814 char *qemu_find_file(int type
, const char *name
)
1820 /* If name contains path separators then try it as a straight path. */
1821 if ((strchr(name
, '/') || strchr(name
, '\\'))
1822 && access(name
, R_OK
) == 0) {
1823 return g_strdup(name
);
1826 case QEMU_FILE_TYPE_BIOS
:
1829 case QEMU_FILE_TYPE_KEYMAP
:
1830 subdir
= "keymaps/";
1835 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1836 buf
= g_malloc0(len
);
1837 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1838 if (access(buf
, R_OK
)) {
1845 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1847 return qdev_device_help(opts
);
1850 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1854 dev
= qdev_device_add(opts
);
1860 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1862 CharDriverState
*chr
;
1864 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1870 #ifdef CONFIG_VIRTFS
1871 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1874 ret
= qemu_fsdev_add(opts
);
1880 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1882 CharDriverState
*chr
;
1883 const char *chardev
;
1887 mode
= qemu_opt_get(opts
, "mode");
1891 if (strcmp(mode
, "readline") == 0) {
1892 flags
= MONITOR_USE_READLINE
;
1893 } else if (strcmp(mode
, "control") == 0) {
1894 flags
= MONITOR_USE_CONTROL
;
1896 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1900 if (qemu_opt_get_bool(opts
, "pretty", 0))
1901 flags
|= MONITOR_USE_PRETTY
;
1903 if (qemu_opt_get_bool(opts
, "default", 0))
1904 flags
|= MONITOR_IS_DEFAULT
;
1906 chardev
= qemu_opt_get(opts
, "chardev");
1907 chr
= qemu_chr_find(chardev
);
1909 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1913 monitor_init(chr
, flags
);
1917 static void monitor_parse(const char *optarg
, const char *mode
)
1919 static int monitor_device_index
= 0;
1925 if (strstart(optarg
, "chardev:", &p
)) {
1926 snprintf(label
, sizeof(label
), "%s", p
);
1928 snprintf(label
, sizeof(label
), "compat_monitor%d",
1929 monitor_device_index
);
1930 if (monitor_device_index
== 0) {
1933 opts
= qemu_chr_parse_compat(label
, optarg
);
1935 fprintf(stderr
, "parse error: %s\n", optarg
);
1940 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1942 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1945 qemu_opt_set(opts
, "mode", mode
);
1946 qemu_opt_set(opts
, "chardev", label
);
1948 qemu_opt_set(opts
, "default", "on");
1949 monitor_device_index
++;
1952 struct device_config
{
1954 DEV_USB
, /* -usbdevice */
1956 DEV_SERIAL
, /* -serial */
1957 DEV_PARALLEL
, /* -parallel */
1958 DEV_VIRTCON
, /* -virtioconsole */
1959 DEV_DEBUGCON
, /* -debugcon */
1961 const char *cmdline
;
1962 QTAILQ_ENTRY(device_config
) next
;
1964 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1966 static void add_device_config(int type
, const char *cmdline
)
1968 struct device_config
*conf
;
1970 conf
= g_malloc0(sizeof(*conf
));
1972 conf
->cmdline
= cmdline
;
1973 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1976 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1978 struct device_config
*conf
;
1981 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1982 if (conf
->type
!= type
)
1984 rc
= func(conf
->cmdline
);
1991 static int serial_parse(const char *devname
)
1993 static int index
= 0;
1996 if (strcmp(devname
, "none") == 0)
1998 if (index
== MAX_SERIAL_PORTS
) {
1999 fprintf(stderr
, "qemu: too many serial ports\n");
2002 snprintf(label
, sizeof(label
), "serial%d", index
);
2003 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2004 if (!serial_hds
[index
]) {
2005 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2006 devname
, strerror(errno
));
2013 static int parallel_parse(const char *devname
)
2015 static int index
= 0;
2018 if (strcmp(devname
, "none") == 0)
2020 if (index
== MAX_PARALLEL_PORTS
) {
2021 fprintf(stderr
, "qemu: too many parallel ports\n");
2024 snprintf(label
, sizeof(label
), "parallel%d", index
);
2025 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2026 if (!parallel_hds
[index
]) {
2027 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2028 devname
, strerror(errno
));
2035 static int virtcon_parse(const char *devname
)
2037 QemuOptsList
*device
= qemu_find_opts("device");
2038 static int index
= 0;
2040 QemuOpts
*bus_opts
, *dev_opts
;
2042 if (strcmp(devname
, "none") == 0)
2044 if (index
== MAX_VIRTIO_CONSOLES
) {
2045 fprintf(stderr
, "qemu: too many virtio consoles\n");
2049 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2050 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2052 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2053 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2055 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2056 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2057 if (!virtcon_hds
[index
]) {
2058 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2059 devname
, strerror(errno
));
2062 qemu_opt_set(dev_opts
, "chardev", label
);
2068 static int debugcon_parse(const char *devname
)
2072 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2075 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2077 fprintf(stderr
, "qemu: already have a debugcon device\n");
2080 qemu_opt_set(opts
, "driver", "isa-debugcon");
2081 qemu_opt_set(opts
, "chardev", "debugcon");
2085 static QEMUMachine
*machine_parse(const char *name
)
2087 QEMUMachine
*m
, *machine
= NULL
;
2090 machine
= find_machine(name
);
2095 printf("Supported machines are:\n");
2096 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2098 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2100 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2101 m
->is_default
? " (default)" : "");
2103 exit(!name
|| *name
!= '?');
2106 static int tcg_init(void)
2108 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2113 const char *opt_name
;
2115 int (*available
)(void);
2119 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2120 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2121 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2124 static int configure_accelerator(void)
2126 const char *p
= NULL
;
2129 bool accel_initalised
= 0;
2130 bool init_failed
= 0;
2132 QemuOptsList
*list
= qemu_find_opts("machine");
2133 if (!QTAILQ_EMPTY(&list
->head
)) {
2134 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2138 /* Use the default "accelerator", tcg */
2142 while (!accel_initalised
&& *p
!= '\0') {
2146 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2147 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2148 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2149 *(accel_list
[i
].allowed
) = 1;
2150 ret
= accel_list
[i
].init();
2153 if (!accel_list
[i
].available()) {
2154 printf("%s not supported for this target\n",
2155 accel_list
[i
].name
);
2157 fprintf(stderr
, "failed to initialize %s: %s\n",
2161 *(accel_list
[i
].allowed
) = 0;
2163 accel_initalised
= 1;
2168 if (i
== ARRAY_SIZE(accel_list
)) {
2169 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2173 if (!accel_initalised
) {
2174 fprintf(stderr
, "No accelerator found!\n");
2179 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2182 return !accel_initalised
;
2185 void qemu_add_exit_notifier(Notifier
*notify
)
2187 notifier_list_add(&exit_notifiers
, notify
);
2190 void qemu_remove_exit_notifier(Notifier
*notify
)
2192 notifier_list_remove(&exit_notifiers
, notify
);
2195 static void qemu_run_exit_notifiers(void)
2197 notifier_list_notify(&exit_notifiers
, NULL
);
2200 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2202 notifier_list_add(&machine_init_done_notifiers
, notify
);
2205 static void qemu_run_machine_init_done_notifiers(void)
2207 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2210 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2211 const char **poptarg
, int *poptind
)
2213 const QEMUOption
*popt
;
2214 int optind
= *poptind
;
2215 char *r
= argv
[optind
];
2218 loc_set_cmdline(argv
, optind
, 1);
2220 /* Treat --foo the same as -foo. */
2223 popt
= qemu_options
;
2226 error_report("invalid option");
2229 if (!strcmp(popt
->name
, r
+ 1))
2233 if (popt
->flags
& HAS_ARG
) {
2234 if (optind
>= argc
) {
2235 error_report("requires an argument");
2238 optarg
= argv
[optind
++];
2239 loc_set_cmdline(argv
, optind
- 2, 2);
2250 static gpointer
malloc_and_trace(gsize n_bytes
)
2252 void *ptr
= malloc(n_bytes
);
2253 trace_g_malloc(n_bytes
, ptr
);
2257 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2259 void *ptr
= realloc(mem
, n_bytes
);
2260 trace_g_realloc(mem
, n_bytes
, ptr
);
2264 static void free_and_trace(gpointer mem
)
2270 int main(int argc
, char **argv
, char **envp
)
2272 const char *gdbstub_dev
= NULL
;
2274 int snapshot
, linux_boot
;
2275 const char *icount_option
= NULL
;
2276 const char *initrd_filename
;
2277 const char *kernel_filename
, *kernel_cmdline
;
2278 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2280 DisplayChangeListener
*dcl
;
2281 int cyls
, heads
, secs
, translation
;
2282 QemuOpts
*hda_opts
= NULL
, *opts
;
2283 QemuOptsList
*olist
;
2286 const char *loadvm
= NULL
;
2287 QEMUMachine
*machine
;
2288 const char *cpu_model
;
2289 const char *pid_file
= NULL
;
2290 const char *incoming
= NULL
;
2292 int show_vnc_port
= 0;
2295 const char *log_mask
= NULL
;
2296 const char *log_file
= NULL
;
2297 GMemVTable mem_trace
= {
2298 .malloc
= malloc_and_trace
,
2299 .realloc
= realloc_and_trace
,
2300 .free
= free_and_trace
,
2302 const char *trace_events
= NULL
;
2303 const char *trace_file
= NULL
;
2305 atexit(qemu_run_exit_notifiers
);
2306 error_set_progname(argv
[0]);
2308 g_mem_set_vtable(&mem_trace
);
2309 g_thread_init(NULL
);
2314 rtc_clock
= host_clock
;
2316 qemu_cache_utils_init(envp
);
2318 QLIST_INIT (&vm_change_state_head
);
2319 os_setup_early_signal_handling();
2321 module_call_init(MODULE_INIT_MACHINE
);
2322 machine
= find_default_machine();
2324 initrd_filename
= NULL
;
2327 kernel_filename
= NULL
;
2328 kernel_cmdline
= "";
2329 cyls
= heads
= secs
= 0;
2330 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2332 for (i
= 0; i
< MAX_NODES
; i
++) {
2334 node_cpumask
[i
] = 0;
2342 /* first pass of option parsing */
2344 while (optind
< argc
) {
2345 if (argv
[optind
][0] != '-') {
2350 const QEMUOption
*popt
;
2352 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2353 switch (popt
->index
) {
2354 case QEMU_OPTION_nodefconfig
:
2364 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2365 if (ret
< 0 && ret
!= -ENOENT
) {
2369 ret
= qemu_read_config_file(arch_config_name
);
2370 if (ret
< 0 && ret
!= -ENOENT
) {
2376 /* second pass of option parsing */
2381 if (argv
[optind
][0] != '-') {
2382 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2384 const QEMUOption
*popt
;
2386 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2387 if (!(popt
->arch_mask
& arch_type
)) {
2388 printf("Option %s not supported for this target\n", popt
->name
);
2391 switch(popt
->index
) {
2393 machine
= machine_parse(optarg
);
2395 case QEMU_OPTION_cpu
:
2396 /* hw initialization will check this */
2397 if (*optarg
== '?') {
2398 list_cpus(stdout
, &fprintf
, optarg
);
2404 case QEMU_OPTION_initrd
:
2405 initrd_filename
= optarg
;
2407 case QEMU_OPTION_hda
:
2411 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2413 snprintf(buf
, sizeof(buf
),
2414 "%s,cyls=%d,heads=%d,secs=%d%s",
2415 HD_OPTS
, cyls
, heads
, secs
,
2416 translation
== BIOS_ATA_TRANSLATION_LBA
?
2418 translation
== BIOS_ATA_TRANSLATION_NONE
?
2419 ",trans=none" : "");
2420 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2423 case QEMU_OPTION_hdb
:
2424 case QEMU_OPTION_hdc
:
2425 case QEMU_OPTION_hdd
:
2426 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2429 case QEMU_OPTION_drive
:
2430 if (drive_def(optarg
) == NULL
) {
2434 case QEMU_OPTION_set
:
2435 if (qemu_set_option(optarg
) != 0)
2438 case QEMU_OPTION_global
:
2439 if (qemu_global_option(optarg
) != 0)
2442 case QEMU_OPTION_mtdblock
:
2443 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2445 case QEMU_OPTION_sd
:
2446 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2448 case QEMU_OPTION_pflash
:
2449 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2451 case QEMU_OPTION_snapshot
:
2454 case QEMU_OPTION_hdachs
:
2458 cyls
= strtol(p
, (char **)&p
, 0);
2459 if (cyls
< 1 || cyls
> 16383)
2464 heads
= strtol(p
, (char **)&p
, 0);
2465 if (heads
< 1 || heads
> 16)
2470 secs
= strtol(p
, (char **)&p
, 0);
2471 if (secs
< 1 || secs
> 63)
2475 if (!strcmp(p
, "none"))
2476 translation
= BIOS_ATA_TRANSLATION_NONE
;
2477 else if (!strcmp(p
, "lba"))
2478 translation
= BIOS_ATA_TRANSLATION_LBA
;
2479 else if (!strcmp(p
, "auto"))
2480 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2483 } else if (*p
!= '\0') {
2485 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2488 if (hda_opts
!= NULL
) {
2490 snprintf(num
, sizeof(num
), "%d", cyls
);
2491 qemu_opt_set(hda_opts
, "cyls", num
);
2492 snprintf(num
, sizeof(num
), "%d", heads
);
2493 qemu_opt_set(hda_opts
, "heads", num
);
2494 snprintf(num
, sizeof(num
), "%d", secs
);
2495 qemu_opt_set(hda_opts
, "secs", num
);
2496 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2497 qemu_opt_set(hda_opts
, "trans", "lba");
2498 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2499 qemu_opt_set(hda_opts
, "trans", "none");
2503 case QEMU_OPTION_numa
:
2504 if (nb_numa_nodes
>= MAX_NODES
) {
2505 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2510 case QEMU_OPTION_display
:
2511 display_type
= select_display(optarg
);
2513 case QEMU_OPTION_nographic
:
2514 display_type
= DT_NOGRAPHIC
;
2516 case QEMU_OPTION_curses
:
2517 #ifdef CONFIG_CURSES
2518 display_type
= DT_CURSES
;
2520 fprintf(stderr
, "Curses support is disabled\n");
2524 case QEMU_OPTION_portrait
:
2525 graphic_rotate
= 90;
2527 case QEMU_OPTION_rotate
:
2528 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2529 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2530 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2532 "qemu: only 90, 180, 270 deg rotation is available\n");
2536 case QEMU_OPTION_kernel
:
2537 kernel_filename
= optarg
;
2539 case QEMU_OPTION_append
:
2540 kernel_cmdline
= optarg
;
2542 case QEMU_OPTION_cdrom
:
2543 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2545 case QEMU_OPTION_boot
:
2547 static const char * const params
[] = {
2548 "order", "once", "menu",
2549 "splash", "splash-time", NULL
2551 char buf
[sizeof(boot_devices
)];
2552 char *standard_boot_devices
;
2555 if (!strchr(optarg
, '=')) {
2557 pstrcpy(buf
, sizeof(buf
), optarg
);
2558 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2560 "qemu: unknown boot parameter '%s' in '%s'\n",
2566 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2567 validate_bootdevices(buf
);
2568 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2571 if (get_param_value(buf
, sizeof(buf
),
2573 validate_bootdevices(buf
);
2574 standard_boot_devices
= g_strdup(boot_devices
);
2575 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2576 qemu_register_reset(restore_boot_devices
,
2577 standard_boot_devices
);
2579 if (get_param_value(buf
, sizeof(buf
),
2581 if (!strcmp(buf
, "on")) {
2583 } else if (!strcmp(buf
, "off")) {
2587 "qemu: invalid option value '%s'\n",
2592 qemu_opts_parse(qemu_find_opts("boot-opts"),
2597 case QEMU_OPTION_fda
:
2598 case QEMU_OPTION_fdb
:
2599 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2602 case QEMU_OPTION_no_fd_bootchk
:
2605 case QEMU_OPTION_netdev
:
2606 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2610 case QEMU_OPTION_net
:
2611 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2616 case QEMU_OPTION_tftp
:
2617 legacy_tftp_prefix
= optarg
;
2619 case QEMU_OPTION_bootp
:
2620 legacy_bootp_filename
= optarg
;
2622 case QEMU_OPTION_redir
:
2623 if (net_slirp_redir(optarg
) < 0)
2627 case QEMU_OPTION_bt
:
2628 add_device_config(DEV_BT
, optarg
);
2630 case QEMU_OPTION_audio_help
:
2631 if (!(audio_available())) {
2632 printf("Option %s not supported for this target\n", popt
->name
);
2638 case QEMU_OPTION_soundhw
:
2639 if (!(audio_available())) {
2640 printf("Option %s not supported for this target\n", popt
->name
);
2643 select_soundhw (optarg
);
2648 case QEMU_OPTION_version
:
2652 case QEMU_OPTION_m
: {
2655 value
= strtosz(optarg
, NULL
);
2657 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2661 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2662 fprintf(stderr
, "qemu: ram size too large\n");
2668 case QEMU_OPTION_mempath
:
2672 case QEMU_OPTION_mem_prealloc
:
2683 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2685 case QEMU_OPTION_gdb
:
2686 gdbstub_dev
= optarg
;
2691 case QEMU_OPTION_bios
:
2694 case QEMU_OPTION_singlestep
:
2701 keyboard_layout
= optarg
;
2703 case QEMU_OPTION_localtime
:
2706 case QEMU_OPTION_vga
:
2707 select_vgahw (optarg
);
2714 w
= strtol(p
, (char **)&p
, 10);
2717 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2723 h
= strtol(p
, (char **)&p
, 10);
2728 depth
= strtol(p
, (char **)&p
, 10);
2729 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2730 depth
!= 24 && depth
!= 32)
2732 } else if (*p
== '\0') {
2733 depth
= graphic_depth
;
2740 graphic_depth
= depth
;
2743 case QEMU_OPTION_echr
:
2746 term_escape_char
= strtol(optarg
, &r
, 0);
2748 printf("Bad argument to echr\n");
2751 case QEMU_OPTION_monitor
:
2752 monitor_parse(optarg
, "readline");
2753 default_monitor
= 0;
2755 case QEMU_OPTION_qmp
:
2756 monitor_parse(optarg
, "control");
2757 default_monitor
= 0;
2759 case QEMU_OPTION_mon
:
2760 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2764 default_monitor
= 0;
2766 case QEMU_OPTION_chardev
:
2767 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2772 case QEMU_OPTION_fsdev
:
2773 olist
= qemu_find_opts("fsdev");
2775 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2778 opts
= qemu_opts_parse(olist
, optarg
, 1);
2780 fprintf(stderr
, "parse error: %s\n", optarg
);
2784 case QEMU_OPTION_virtfs
: {
2787 const char *writeout
;
2789 olist
= qemu_find_opts("virtfs");
2791 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2794 opts
= qemu_opts_parse(olist
, optarg
, 1);
2796 fprintf(stderr
, "parse error: %s\n", optarg
);
2800 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2801 qemu_opt_get(opts
, "mount_tag") == NULL
||
2802 qemu_opt_get(opts
, "path") == NULL
) {
2803 fprintf(stderr
, "Usage: -virtfs fsdriver,path=/share_path/,"
2804 "[security_model={mapped|passthrough|none}],"
2805 "mount_tag=tag.\n");
2808 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2809 qemu_opt_get(opts
, "mount_tag"), 1);
2811 fprintf(stderr
, "duplicate fsdev id: %s\n",
2812 qemu_opt_get(opts
, "mount_tag"));
2816 writeout
= qemu_opt_get(opts
, "writeout");
2818 #ifdef CONFIG_SYNC_FILE_RANGE
2819 qemu_opt_set(fsdev
, "writeout", writeout
);
2821 fprintf(stderr
, "writeout=immediate not supported on "
2826 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2827 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2828 qemu_opt_set(fsdev
, "security_model",
2829 qemu_opt_get(opts
, "security_model"));
2831 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2832 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2833 qemu_opt_set(device
, "fsdev",
2834 qemu_opt_get(opts
, "mount_tag"));
2835 qemu_opt_set(device
, "mount_tag",
2836 qemu_opt_get(opts
, "mount_tag"));
2839 case QEMU_OPTION_serial
:
2840 add_device_config(DEV_SERIAL
, optarg
);
2842 if (strncmp(optarg
, "mon:", 4) == 0) {
2843 default_monitor
= 0;
2846 case QEMU_OPTION_watchdog
:
2849 "qemu: only one watchdog option may be given\n");
2854 case QEMU_OPTION_watchdog_action
:
2855 if (select_watchdog_action(optarg
) == -1) {
2856 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2860 case QEMU_OPTION_virtiocon
:
2861 add_device_config(DEV_VIRTCON
, optarg
);
2862 default_virtcon
= 0;
2863 if (strncmp(optarg
, "mon:", 4) == 0) {
2864 default_monitor
= 0;
2867 case QEMU_OPTION_parallel
:
2868 add_device_config(DEV_PARALLEL
, optarg
);
2869 default_parallel
= 0;
2870 if (strncmp(optarg
, "mon:", 4) == 0) {
2871 default_monitor
= 0;
2874 case QEMU_OPTION_debugcon
:
2875 add_device_config(DEV_DEBUGCON
, optarg
);
2877 case QEMU_OPTION_loadvm
:
2880 case QEMU_OPTION_full_screen
:
2884 case QEMU_OPTION_no_frame
:
2887 case QEMU_OPTION_alt_grab
:
2890 case QEMU_OPTION_ctrl_grab
:
2893 case QEMU_OPTION_no_quit
:
2896 case QEMU_OPTION_sdl
:
2897 display_type
= DT_SDL
;
2900 case QEMU_OPTION_no_frame
:
2901 case QEMU_OPTION_alt_grab
:
2902 case QEMU_OPTION_ctrl_grab
:
2903 case QEMU_OPTION_no_quit
:
2904 case QEMU_OPTION_sdl
:
2905 fprintf(stderr
, "SDL support is disabled\n");
2908 case QEMU_OPTION_pidfile
:
2911 case QEMU_OPTION_win2k_hack
:
2912 win2k_install_hack
= 1;
2914 case QEMU_OPTION_rtc_td_hack
:
2917 case QEMU_OPTION_acpitable
:
2918 do_acpitable_option(optarg
);
2920 case QEMU_OPTION_smbios
:
2921 do_smbios_option(optarg
);
2923 case QEMU_OPTION_enable_kvm
:
2924 olist
= qemu_find_opts("machine");
2925 qemu_opts_reset(olist
);
2926 qemu_opts_parse(olist
, "accel=kvm", 0);
2928 case QEMU_OPTION_machine
:
2929 olist
= qemu_find_opts("machine");
2930 qemu_opts_reset(olist
);
2931 opts
= qemu_opts_parse(olist
, optarg
, 1);
2933 fprintf(stderr
, "parse error: %s\n", optarg
);
2936 optarg
= qemu_opt_get(opts
, "type");
2938 machine
= machine_parse(optarg
);
2941 case QEMU_OPTION_usb
:
2944 case QEMU_OPTION_usbdevice
:
2946 add_device_config(DEV_USB
, optarg
);
2948 case QEMU_OPTION_device
:
2949 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2953 case QEMU_OPTION_smp
:
2956 fprintf(stderr
, "Invalid number of CPUs\n");
2959 if (max_cpus
< smp_cpus
) {
2960 fprintf(stderr
, "maxcpus must be equal to or greater than "
2964 if (max_cpus
> 255) {
2965 fprintf(stderr
, "Unsupported number of maxcpus\n");
2969 case QEMU_OPTION_vnc
:
2972 vnc_display
= optarg
;
2974 fprintf(stderr
, "VNC support is disabled\n");
2978 case QEMU_OPTION_no_acpi
:
2981 case QEMU_OPTION_no_hpet
:
2984 case QEMU_OPTION_balloon
:
2985 if (balloon_parse(optarg
) < 0) {
2986 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2990 case QEMU_OPTION_no_reboot
:
2993 case QEMU_OPTION_no_shutdown
:
2996 case QEMU_OPTION_show_cursor
:
2999 case QEMU_OPTION_uuid
:
3000 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3001 fprintf(stderr
, "Fail to parse UUID string."
3002 " Wrong format.\n");
3006 case QEMU_OPTION_option_rom
:
3007 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3008 fprintf(stderr
, "Too many option ROMs\n");
3011 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3012 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3013 option_rom
[nb_option_roms
].bootindex
=
3014 qemu_opt_get_number(opts
, "bootindex", -1);
3015 if (!option_rom
[nb_option_roms
].name
) {
3016 fprintf(stderr
, "Option ROM file is not specified\n");
3021 case QEMU_OPTION_semihosting
:
3022 semihosting_enabled
= 1;
3024 case QEMU_OPTION_name
:
3025 qemu_name
= g_strdup(optarg
);
3027 char *p
= strchr(qemu_name
, ',');
3030 if (strncmp(p
, "process=", 8)) {
3031 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3035 os_set_proc_name(p
);
3039 case QEMU_OPTION_prom_env
:
3040 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3041 fprintf(stderr
, "Too many prom variables\n");
3044 prom_envs
[nb_prom_envs
] = optarg
;
3047 case QEMU_OPTION_old_param
:
3050 case QEMU_OPTION_clock
:
3051 configure_alarms(optarg
);
3053 case QEMU_OPTION_startdate
:
3054 configure_rtc_date_offset(optarg
, 1);
3056 case QEMU_OPTION_rtc
:
3057 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3061 configure_rtc(opts
);
3063 case QEMU_OPTION_tb_size
:
3064 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3065 if (tcg_tb_size
< 0) {
3069 case QEMU_OPTION_icount
:
3070 icount_option
= optarg
;
3072 case QEMU_OPTION_incoming
:
3075 case QEMU_OPTION_nodefaults
:
3077 default_parallel
= 0;
3078 default_virtcon
= 0;
3079 default_monitor
= 0;
3086 case QEMU_OPTION_xen_domid
:
3087 if (!(xen_available())) {
3088 printf("Option %s not supported for this target\n", popt
->name
);
3091 xen_domid
= atoi(optarg
);
3093 case QEMU_OPTION_xen_create
:
3094 if (!(xen_available())) {
3095 printf("Option %s not supported for this target\n", popt
->name
);
3098 xen_mode
= XEN_CREATE
;
3100 case QEMU_OPTION_xen_attach
:
3101 if (!(xen_available())) {
3102 printf("Option %s not supported for this target\n", popt
->name
);
3105 xen_mode
= XEN_ATTACH
;
3107 case QEMU_OPTION_trace
:
3109 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3113 trace_events
= qemu_opt_get(opts
, "events");
3114 trace_file
= qemu_opt_get(opts
, "file");
3117 case QEMU_OPTION_readconfig
:
3119 int ret
= qemu_read_config_file(optarg
);
3121 fprintf(stderr
, "read config %s: %s\n", optarg
,
3127 case QEMU_OPTION_spice
:
3128 olist
= qemu_find_opts("spice");
3130 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3133 opts
= qemu_opts_parse(olist
, optarg
, 0);
3135 fprintf(stderr
, "parse error: %s\n", optarg
);
3139 case QEMU_OPTION_writeconfig
:
3142 if (strcmp(optarg
, "-") == 0) {
3145 fp
= fopen(optarg
, "w");
3147 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3151 qemu_config_write(fp
);
3156 os_parse_cmd_args(popt
->index
, optarg
);
3162 /* Open the logfile at this point, if necessary. We can't open the logfile
3163 * when encountering either of the logging options (-d or -D) because the
3164 * other one may be encountered later on the command line, changing the
3165 * location or level of logging.
3169 set_cpu_log_filename(log_file
);
3171 set_cpu_log(log_mask
);
3174 if (!trace_backend_init(trace_events
, trace_file
)) {
3178 /* If no data_dir is specified then try to find it relative to the
3181 data_dir
= os_find_datadir(argv
[0]);
3183 /* If all else fails use the install path specified when building. */
3185 data_dir
= CONFIG_QEMU_DATADIR
;
3189 * Default to max_cpus = smp_cpus, in case the user doesn't
3190 * specify a max_cpus value.
3193 max_cpus
= smp_cpus
;
3195 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3196 if (smp_cpus
> machine
->max_cpus
) {
3197 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3198 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3204 * Get the default machine options from the machine if it is not already
3205 * specified either by the configuration file or by the command line.
3207 if (machine
->default_machine_opts
) {
3208 QemuOptsList
*list
= qemu_find_opts("machine");
3209 const char *p
= NULL
;
3211 if (!QTAILQ_EMPTY(&list
->head
)) {
3212 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3215 qemu_opts_reset(list
);
3216 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3218 fprintf(stderr
, "parse error for machine %s: %s\n",
3219 machine
->name
, machine
->default_machine_opts
);
3225 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3226 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3228 if (machine
->no_serial
) {
3231 if (machine
->no_parallel
) {
3232 default_parallel
= 0;
3234 if (!machine
->use_virtcon
) {
3235 default_virtcon
= 0;
3237 if (machine
->no_vga
) {
3240 if (machine
->no_floppy
) {
3243 if (machine
->no_cdrom
) {
3246 if (machine
->no_sdcard
) {
3250 if (display_type
== DT_NOGRAPHIC
) {
3251 if (default_parallel
)
3252 add_device_config(DEV_PARALLEL
, "null");
3253 if (default_serial
&& default_monitor
) {
3254 add_device_config(DEV_SERIAL
, "mon:stdio");
3255 } else if (default_virtcon
&& default_monitor
) {
3256 add_device_config(DEV_VIRTCON
, "mon:stdio");
3259 add_device_config(DEV_SERIAL
, "stdio");
3260 if (default_virtcon
)
3261 add_device_config(DEV_VIRTCON
, "stdio");
3262 if (default_monitor
)
3263 monitor_parse("stdio", "readline");
3267 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3268 if (default_parallel
)
3269 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3270 if (default_monitor
)
3271 monitor_parse("vc:80Cx24C", "readline");
3272 if (default_virtcon
)
3273 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3276 vga_interface_type
= VGA_CIRRUS
;
3280 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3282 #ifdef CONFIG_VIRTFS
3283 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3290 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3295 /* init the memory */
3296 if (ram_size
== 0) {
3297 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3300 configure_accelerator();
3302 if (qemu_init_main_loop()) {
3303 fprintf(stderr
, "qemu_init_main_loop failed\n");
3306 linux_boot
= (kernel_filename
!= NULL
);
3308 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3309 fprintf(stderr
, "-append only allowed with -kernel option\n");
3313 if (!linux_boot
&& initrd_filename
!= NULL
) {
3314 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3318 os_set_line_buffering();
3320 if (init_timer_alarm() < 0) {
3321 fprintf(stderr
, "could not initialize alarm timer\n");
3324 configure_icount(icount_option
);
3326 if (net_init_clients() < 0) {
3330 /* init the bluetooth world */
3331 if (foreach_device_config(DEV_BT
, bt_parse
))
3334 if (!xen_enabled()) {
3335 /* On 32-bit hosts, QEMU is limited by virtual address space */
3336 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3337 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3342 cpu_exec_init_all();
3344 bdrv_init_with_whitelist();
3348 /* open the virtual block devices */
3350 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3351 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3354 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3355 IF_DEFAULT
, 2, CDROM_OPTS
);
3356 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3357 IF_FLOPPY
, 0, FD_OPTS
);
3358 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3361 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3364 if (nb_numa_nodes
> 0) {
3367 if (nb_numa_nodes
> MAX_NODES
) {
3368 nb_numa_nodes
= MAX_NODES
;
3371 /* If no memory size if given for any node, assume the default case
3372 * and distribute the available memory equally across all nodes
3374 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3375 if (node_mem
[i
] != 0)
3378 if (i
== nb_numa_nodes
) {
3379 uint64_t usedmem
= 0;
3381 /* On Linux, the each node's border has to be 8MB aligned,
3382 * the final node gets the rest.
3384 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3385 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3386 usedmem
+= node_mem
[i
];
3388 node_mem
[i
] = ram_size
- usedmem
;
3391 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3392 if (node_cpumask
[i
] != 0)
3395 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3396 * must cope with this anyway, because there are BIOSes out there in
3397 * real machines which also use this scheme.
3399 if (i
== nb_numa_nodes
) {
3400 for (i
= 0; i
< smp_cpus
; i
++) {
3401 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3406 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3410 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3412 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3414 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3416 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3419 module_call_init(MODULE_INIT_DEVICE
);
3421 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3425 i
= select_watchdog(watchdog
);
3427 exit (i
== 1 ? 1 : 0);
3430 if (machine
->compat_props
) {
3431 qdev_prop_register_global_list(machine
->compat_props
);
3435 machine
->init(ram_size
, boot_devices
,
3436 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3438 cpu_synchronize_all_post_init();
3442 current_machine
= machine
;
3444 /* init USB devices */
3446 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3450 /* init generic devices */
3451 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3454 net_check_clients();
3456 /* just use the first displaystate for the moment */
3457 ds
= get_displaystate();
3461 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3462 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3463 display_type
= DT_SDL
;
3464 #elif defined(CONFIG_VNC)
3465 vnc_display
= "localhost:0,to=99";
3468 display_type
= DT_NONE
;
3473 /* init local displays */
3474 switch (display_type
) {
3477 #if defined(CONFIG_CURSES)
3479 curses_display_init(ds
, full_screen
);
3482 #if defined(CONFIG_SDL)
3484 sdl_display_init(ds
, full_screen
, no_frame
);
3486 #elif defined(CONFIG_COCOA)
3488 cocoa_display_init(ds
, full_screen
);
3495 /* must be after terminal init, SDL library changes signal handlers */
3496 os_setup_signal_handling();
3499 /* init remote displays */
3501 vnc_display_init(ds
);
3502 if (vnc_display_open(ds
, vnc_display
) < 0)
3505 if (show_vnc_port
) {
3506 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3511 if (using_spice
&& !qxl_enabled
) {
3512 qemu_spice_display_init(ds
);
3518 dcl
= ds
->listeners
;
3519 while (dcl
!= NULL
) {
3520 if (dcl
->dpy_refresh
!= NULL
) {
3521 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3522 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3527 text_consoles_set_display(ds
);
3529 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3530 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3535 qdev_machine_creation_done();
3537 if (rom_load_all() != 0) {
3538 fprintf(stderr
, "rom loading failed\n");
3542 /* TODO: once all bus devices are qdevified, this should be done
3543 * when bus is created by qdev.c */
3544 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3545 qemu_run_machine_init_done_notifiers();
3547 qemu_system_reset(VMRESET_SILENT
);
3549 if (load_vmstate(loadvm
) < 0) {
3555 runstate_set(RUN_STATE_INMIGRATE
);
3556 int ret
= qemu_start_incoming_migration(incoming
);
3558 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3562 } else if (autostart
) {