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"
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 static QEMUTimer
*nographic_timer
;
254 uint8_t qemu_uuid
[16];
256 static QEMUBootSetHandler
*boot_set_handler
;
257 static void *boot_set_opaque
;
259 static NotifierList exit_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
262 static NotifierList machine_init_done_notifiers
=
263 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
265 static int tcg_allowed
= 1;
269 enum xen_mode xen_mode
= XEN_EMULATE
;
270 static int tcg_tb_size
;
272 static int default_serial
= 1;
273 static int default_parallel
= 1;
274 static int default_virtcon
= 1;
275 static int default_monitor
= 1;
276 static int default_vga
= 1;
277 static int default_floppy
= 1;
278 static int default_cdrom
= 1;
279 static int default_sdcard
= 1;
285 { .driver
= "isa-serial", .flag
= &default_serial
},
286 { .driver
= "isa-parallel", .flag
= &default_parallel
},
287 { .driver
= "isa-fdc", .flag
= &default_floppy
},
288 { .driver
= "ide-cd", .flag
= &default_cdrom
},
289 { .driver
= "ide-hd", .flag
= &default_cdrom
},
290 { .driver
= "ide-drive", .flag
= &default_cdrom
},
291 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
292 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
293 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
294 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
295 { .driver
= "VGA", .flag
= &default_vga
},
296 { .driver
= "cirrus-vga", .flag
= &default_vga
},
297 { .driver
= "vmware-svga", .flag
= &default_vga
},
298 { .driver
= "isa-vga", .flag
= &default_vga
},
299 { .driver
= "qxl-vga", .flag
= &default_vga
},
302 static void res_free(void)
304 if (boot_splash_filedata
!= NULL
) {
305 g_free(boot_splash_filedata
);
306 boot_splash_filedata
= NULL
;
310 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
312 const char *driver
= qemu_opt_get(opts
, "driver");
317 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
318 if (strcmp(default_list
[i
].driver
, driver
) != 0)
320 *(default_list
[i
].flag
) = 0;
325 /***********************************************************/
328 static RunState current_run_state
= RSTATE_NO_STATE
;
333 } RunStateTransition
;
335 static const RunStateTransition runstate_transitions_def
[] = {
337 { RSTATE_NO_STATE
, RSTATE_RUNNING
},
338 { RSTATE_NO_STATE
, RSTATE_IN_MIGRATE
},
339 { RSTATE_NO_STATE
, RSTATE_PRE_LAUNCH
},
341 { RSTATE_DEBUG
, RSTATE_RUNNING
},
343 { RSTATE_IN_MIGRATE
, RSTATE_RUNNING
},
344 { RSTATE_IN_MIGRATE
, RSTATE_PRE_LAUNCH
},
346 { RSTATE_PANICKED
, RSTATE_PAUSED
},
348 { RSTATE_IO_ERROR
, RSTATE_RUNNING
},
350 { RSTATE_PAUSED
, RSTATE_RUNNING
},
352 { RSTATE_POST_MIGRATE
, RSTATE_RUNNING
},
354 { RSTATE_PRE_LAUNCH
, RSTATE_RUNNING
},
355 { RSTATE_PRE_LAUNCH
, RSTATE_POST_MIGRATE
},
357 { RSTATE_PRE_MIGRATE
, RSTATE_RUNNING
},
358 { RSTATE_PRE_MIGRATE
, RSTATE_POST_MIGRATE
},
360 { RSTATE_RESTORE
, RSTATE_RUNNING
},
362 { RSTATE_RUNNING
, RSTATE_DEBUG
},
363 { RSTATE_RUNNING
, RSTATE_PANICKED
},
364 { RSTATE_RUNNING
, RSTATE_IO_ERROR
},
365 { RSTATE_RUNNING
, RSTATE_PAUSED
},
366 { RSTATE_RUNNING
, RSTATE_PRE_MIGRATE
},
367 { RSTATE_RUNNING
, RSTATE_RESTORE
},
368 { RSTATE_RUNNING
, RSTATE_SAVEVM
},
369 { RSTATE_RUNNING
, RSTATE_SHUTDOWN
},
370 { RSTATE_RUNNING
, RSTATE_WATCHDOG
},
372 { RSTATE_SAVEVM
, RSTATE_RUNNING
},
374 { RSTATE_SHUTDOWN
, RSTATE_PAUSED
},
376 { RSTATE_WATCHDOG
, RSTATE_RUNNING
},
378 { RSTATE_MAX
, RSTATE_MAX
},
381 static bool runstate_valid_transitions
[RSTATE_MAX
][RSTATE_MAX
];
383 static const char *const runstate_name_tbl
[RSTATE_MAX
] = {
384 [RSTATE_DEBUG
] = "debug",
385 [RSTATE_IN_MIGRATE
] = "incoming-migration",
386 [RSTATE_PANICKED
] = "internal-error",
387 [RSTATE_IO_ERROR
] = "io-error",
388 [RSTATE_PAUSED
] = "paused",
389 [RSTATE_POST_MIGRATE
] = "post-migrate",
390 [RSTATE_PRE_LAUNCH
] = "prelaunch",
391 [RSTATE_PRE_MIGRATE
] = "finish-migrate",
392 [RSTATE_RESTORE
] = "restore-vm",
393 [RSTATE_RUNNING
] = "running",
394 [RSTATE_SAVEVM
] = "save-vm",
395 [RSTATE_SHUTDOWN
] = "shutdown",
396 [RSTATE_WATCHDOG
] = "watchdog",
399 bool runstate_check(RunState state
)
401 return current_run_state
== state
;
404 void runstate_init(void)
406 const RunStateTransition
*p
;
408 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
410 for (p
= &runstate_transitions_def
[0]; p
->from
!= RSTATE_MAX
; p
++) {
411 runstate_valid_transitions
[p
->from
][p
->to
] = true;
415 /* This function will abort() on invalid state transitions */
416 void runstate_set(RunState new_state
)
418 if (new_state
>= RSTATE_MAX
||
419 !runstate_valid_transitions
[current_run_state
][new_state
]) {
420 fprintf(stderr
, "invalid runstate transition\n");
424 current_run_state
= new_state
;
427 const char *runstate_as_string(void)
429 assert(current_run_state
> RSTATE_NO_STATE
&&
430 current_run_state
< RSTATE_MAX
);
431 return runstate_name_tbl
[current_run_state
];
434 int runstate_is_running(void)
436 return runstate_check(RSTATE_RUNNING
);
439 /***********************************************************/
440 /* real time host monotonic timer */
442 /***********************************************************/
443 /* host time/date access */
444 void qemu_get_timedate(struct tm
*tm
, int offset
)
451 if (rtc_date_offset
== -1) {
455 ret
= localtime(&ti
);
457 ti
-= rtc_date_offset
;
461 memcpy(tm
, ret
, sizeof(struct tm
));
464 int qemu_timedate_diff(struct tm
*tm
)
468 if (rtc_date_offset
== -1)
470 seconds
= mktimegm(tm
);
472 seconds
= mktime(tm
);
474 seconds
= mktimegm(tm
) + rtc_date_offset
;
476 return seconds
- time(NULL
);
479 void rtc_change_mon_event(struct tm
*tm
)
483 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
484 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
485 qobject_decref(data
);
488 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
490 time_t rtc_start_date
;
493 if (!strcmp(startdate
, "now") && legacy
) {
494 rtc_date_offset
= -1;
496 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
504 } else if (sscanf(startdate
, "%d-%d-%d",
516 rtc_start_date
= mktimegm(&tm
);
517 if (rtc_start_date
== -1) {
519 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
520 "'2006-06-17T16:01:21' or '2006-06-17'\n");
523 rtc_date_offset
= time(NULL
) - rtc_start_date
;
527 static void configure_rtc(QemuOpts
*opts
)
531 value
= qemu_opt_get(opts
, "base");
533 if (!strcmp(value
, "utc")) {
535 } else if (!strcmp(value
, "localtime")) {
538 configure_rtc_date_offset(value
, 0);
541 value
= qemu_opt_get(opts
, "clock");
543 if (!strcmp(value
, "host")) {
544 rtc_clock
= host_clock
;
545 } else if (!strcmp(value
, "vm")) {
546 rtc_clock
= vm_clock
;
548 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
552 value
= qemu_opt_get(opts
, "driftfix");
554 if (!strcmp(value
, "slew")) {
556 } else if (!strcmp(value
, "none")) {
559 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
565 /***********************************************************/
566 /* Bluetooth support */
569 static struct HCIInfo
*hci_table
[MAX_NICS
];
571 static struct bt_vlan_s
{
572 struct bt_scatternet_s net
;
574 struct bt_vlan_s
*next
;
577 /* find or alloc a new bluetooth "VLAN" */
578 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
580 struct bt_vlan_s
**pvlan
, *vlan
;
581 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
585 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
587 pvlan
= &first_bt_vlan
;
588 while (*pvlan
!= NULL
)
589 pvlan
= &(*pvlan
)->next
;
594 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
598 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
603 static struct HCIInfo null_hci
= {
604 .cmd_send
= null_hci_send
,
605 .sco_send
= null_hci_send
,
606 .acl_send
= null_hci_send
,
607 .bdaddr_set
= null_hci_addr_set
,
610 struct HCIInfo
*qemu_next_hci(void)
612 if (cur_hci
== nb_hcis
)
615 return hci_table
[cur_hci
++];
618 static struct HCIInfo
*hci_init(const char *str
)
621 struct bt_scatternet_s
*vlan
= 0;
623 if (!strcmp(str
, "null"))
626 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
628 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
629 else if (!strncmp(str
, "hci", 3)) {
632 if (!strncmp(str
+ 3, ",vlan=", 6)) {
633 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
638 vlan
= qemu_find_bt_vlan(0);
640 return bt_new_hci(vlan
);
643 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
648 static int bt_hci_parse(const char *str
)
653 if (nb_hcis
>= MAX_NICS
) {
654 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
667 bdaddr
.b
[5] = 0x56 + nb_hcis
;
668 hci
->bdaddr_set(hci
, bdaddr
.b
);
670 hci_table
[nb_hcis
++] = hci
;
675 static void bt_vhci_add(int vlan_id
)
677 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
680 fprintf(stderr
, "qemu: warning: adding a VHCI to "
681 "an empty scatternet %i\n", vlan_id
);
683 bt_vhci_init(bt_new_hci(vlan
));
686 static struct bt_device_s
*bt_device_add(const char *opt
)
688 struct bt_scatternet_s
*vlan
;
690 char *endp
= strstr(opt
, ",vlan=");
691 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
694 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
697 vlan_id
= strtol(endp
+ 6, &endp
, 0);
699 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
704 vlan
= qemu_find_bt_vlan(vlan_id
);
707 fprintf(stderr
, "qemu: warning: adding a slave device to "
708 "an empty scatternet %i\n", vlan_id
);
710 if (!strcmp(devname
, "keyboard"))
711 return bt_keyboard_init(vlan
);
713 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
717 static int bt_parse(const char *opt
)
719 const char *endp
, *p
;
722 if (strstart(opt
, "hci", &endp
)) {
723 if (!*endp
|| *endp
== ',') {
725 if (!strstart(endp
, ",vlan=", 0))
728 return bt_hci_parse(opt
);
730 } else if (strstart(opt
, "vhci", &endp
)) {
731 if (!*endp
|| *endp
== ',') {
733 if (strstart(endp
, ",vlan=", &p
)) {
734 vlan
= strtol(p
, (char **) &endp
, 0);
736 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
740 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
749 } else if (strstart(opt
, "device:", &endp
))
750 return !bt_device_add(endp
);
752 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
756 /***********************************************************/
757 /* QEMU Block devices */
759 #define HD_OPTS "media=disk"
760 #define CDROM_OPTS "media=cdrom"
762 #define PFLASH_OPTS ""
766 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
768 int *use_scsi
= opaque
;
770 return drive_init(opts
, *use_scsi
) == NULL
;
773 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
775 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
776 qemu_opt_set(opts
, "snapshot", "on");
781 static void default_drive(int enable
, int snapshot
, int use_scsi
,
782 BlockInterfaceType type
, int index
,
787 if (type
== IF_DEFAULT
) {
788 type
= use_scsi
? IF_SCSI
: IF_IDE
;
791 if (!enable
|| drive_get_by_index(type
, index
)) {
795 opts
= drive_add(type
, index
, NULL
, optstr
);
797 drive_enable_snapshot(opts
, NULL
);
799 if (!drive_init(opts
, use_scsi
)) {
804 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
806 boot_set_handler
= func
;
807 boot_set_opaque
= opaque
;
810 int qemu_boot_set(const char *boot_devices
)
812 if (!boot_set_handler
) {
815 return boot_set_handler(boot_set_opaque
, boot_devices
);
818 static void validate_bootdevices(char *devices
)
820 /* We just do some generic consistency checks */
824 for (p
= devices
; *p
!= '\0'; p
++) {
825 /* Allowed boot devices are:
826 * a-b: floppy disk drives
827 * c-f: IDE disk drives
828 * g-m: machine implementation dependant drives
829 * n-p: network devices
830 * It's up to each machine implementation to check if the given boot
831 * devices match the actual hardware implementation and firmware
834 if (*p
< 'a' || *p
> 'p') {
835 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
838 if (bitmap
& (1 << (*p
- 'a'))) {
839 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
842 bitmap
|= 1 << (*p
- 'a');
846 static void restore_boot_devices(void *opaque
)
848 char *standard_boot_devices
= opaque
;
849 static int first
= 1;
851 /* Restore boot order and remove ourselves after the first boot */
857 qemu_boot_set(standard_boot_devices
);
859 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
860 g_free(standard_boot_devices
);
863 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
866 FWBootEntry
*node
, *i
;
872 assert(dev
!= NULL
|| suffix
!= NULL
);
874 node
= g_malloc0(sizeof(FWBootEntry
));
875 node
->bootindex
= bootindex
;
876 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
879 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
880 if (i
->bootindex
== bootindex
) {
881 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
883 } else if (i
->bootindex
< bootindex
) {
886 QTAILQ_INSERT_BEFORE(i
, node
, link
);
889 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
893 * This function returns null terminated string that consist of new line
894 * separated device paths.
896 * memory pointed by "size" is assigned total length of the array in bytes
899 char *get_boot_devices_list(uint32_t *size
)
905 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
906 char *devpath
= NULL
, *bootpath
;
910 devpath
= qdev_get_fw_dev_path(i
->dev
);
914 if (i
->suffix
&& devpath
) {
915 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
917 bootpath
= g_malloc(bootpathlen
);
918 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
920 } else if (devpath
) {
923 bootpath
= g_strdup(i
->suffix
);
928 list
[total
-1] = '\n';
930 len
= strlen(bootpath
) + 1;
931 list
= g_realloc(list
, total
+ len
);
932 memcpy(&list
[total
], bootpath
, len
);
942 static void numa_add(const char *optarg
)
946 unsigned long long value
, endvalue
;
949 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
950 if (!strcmp(option
, "node")) {
951 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
952 nodenr
= nb_numa_nodes
;
954 nodenr
= strtoull(option
, NULL
, 10);
957 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
958 node_mem
[nodenr
] = 0;
961 sval
= strtosz(option
, NULL
);
963 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
966 node_mem
[nodenr
] = sval
;
968 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
969 node_cpumask
[nodenr
] = 0;
971 value
= strtoull(option
, &endptr
, 10);
974 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
976 if (*endptr
== '-') {
977 endvalue
= strtoull(endptr
+1, &endptr
, 10);
978 if (endvalue
>= 63) {
981 "only 63 CPUs in NUMA mode supported.\n");
983 value
= (2ULL << endvalue
) - (1ULL << value
);
985 value
= 1ULL << value
;
988 node_cpumask
[nodenr
] = value
;
995 static void smp_parse(const char *optarg
)
997 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1001 smp
= strtoul(optarg
, &endptr
, 10);
1002 if (endptr
!= optarg
) {
1003 if (*endptr
== ',') {
1007 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1008 sockets
= strtoull(option
, NULL
, 10);
1009 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1010 cores
= strtoull(option
, NULL
, 10);
1011 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1012 threads
= strtoull(option
, NULL
, 10);
1013 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1014 max_cpus
= strtoull(option
, NULL
, 10);
1016 /* compute missing values, prefer sockets over cores over threads */
1017 if (smp
== 0 || sockets
== 0) {
1018 sockets
= sockets
> 0 ? sockets
: 1;
1019 cores
= cores
> 0 ? cores
: 1;
1020 threads
= threads
> 0 ? threads
: 1;
1022 smp
= cores
* threads
* sockets
;
1026 threads
= threads
> 0 ? threads
: 1;
1027 cores
= smp
/ (sockets
* threads
);
1029 threads
= smp
/ (cores
* sockets
);
1033 smp_cores
= cores
> 0 ? cores
: 1;
1034 smp_threads
= threads
> 0 ? threads
: 1;
1036 max_cpus
= smp_cpus
;
1039 /***********************************************************/
1042 static int usb_device_add(const char *devname
)
1045 USBDevice
*dev
= NULL
;
1050 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1051 dev
= usbdevice_create(devname
);
1055 /* the other ones */
1056 #ifndef CONFIG_LINUX
1057 /* only the linux version is qdev-ified, usb-bsd still needs this */
1058 if (strstart(devname
, "host:", &p
)) {
1059 dev
= usb_host_device_open(p
);
1062 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1063 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1064 bt_new_hci(qemu_find_bt_vlan(0)));
1075 static int usb_device_del(const char *devname
)
1080 if (strstart(devname
, "host:", &p
))
1081 return usb_host_device_close(p
);
1086 p
= strchr(devname
, '.');
1089 bus_num
= strtoul(devname
, NULL
, 0);
1090 addr
= strtoul(p
+ 1, NULL
, 0);
1092 return usb_device_delete_addr(bus_num
, addr
);
1095 static int usb_parse(const char *cmdline
)
1098 r
= usb_device_add(cmdline
);
1100 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1105 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1107 const char *devname
= qdict_get_str(qdict
, "devname");
1108 if (usb_device_add(devname
) < 0) {
1109 error_report("could not add USB device '%s'", devname
);
1113 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1115 const char *devname
= qdict_get_str(qdict
, "devname");
1116 if (usb_device_del(devname
) < 0) {
1117 error_report("could not delete USB device '%s'", devname
);
1121 /***********************************************************/
1122 /* PCMCIA/Cardbus */
1124 static struct pcmcia_socket_entry_s
{
1125 PCMCIASocket
*socket
;
1126 struct pcmcia_socket_entry_s
*next
;
1127 } *pcmcia_sockets
= 0;
1129 void pcmcia_socket_register(PCMCIASocket
*socket
)
1131 struct pcmcia_socket_entry_s
*entry
;
1133 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1134 entry
->socket
= socket
;
1135 entry
->next
= pcmcia_sockets
;
1136 pcmcia_sockets
= entry
;
1139 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1141 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1143 ptr
= &pcmcia_sockets
;
1144 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1145 if (entry
->socket
== socket
) {
1151 void pcmcia_info(Monitor
*mon
)
1153 struct pcmcia_socket_entry_s
*iter
;
1155 if (!pcmcia_sockets
)
1156 monitor_printf(mon
, "No PCMCIA sockets\n");
1158 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1159 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1160 iter
->socket
->attached
? iter
->socket
->card_string
:
1164 /***********************************************************/
1165 /* machine registration */
1167 static QEMUMachine
*first_machine
= NULL
;
1168 QEMUMachine
*current_machine
= NULL
;
1170 int qemu_register_machine(QEMUMachine
*m
)
1173 pm
= &first_machine
;
1181 static QEMUMachine
*find_machine(const char *name
)
1185 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1186 if (!strcmp(m
->name
, name
))
1188 if (m
->alias
&& !strcmp(m
->alias
, name
))
1194 static QEMUMachine
*find_default_machine(void)
1198 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1199 if (m
->is_default
) {
1206 /***********************************************************/
1207 /* main execution loop */
1209 static void gui_update(void *opaque
)
1211 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1212 DisplayState
*ds
= opaque
;
1213 DisplayChangeListener
*dcl
= ds
->listeners
;
1215 qemu_flush_coalesced_mmio_buffer();
1218 while (dcl
!= NULL
) {
1219 if (dcl
->gui_timer_interval
&&
1220 dcl
->gui_timer_interval
< interval
)
1221 interval
= dcl
->gui_timer_interval
;
1224 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1227 static void nographic_update(void *opaque
)
1229 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1231 qemu_flush_coalesced_mmio_buffer();
1232 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1235 struct vm_change_state_entry
{
1236 VMChangeStateHandler
*cb
;
1238 QLIST_ENTRY (vm_change_state_entry
) entries
;
1241 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1243 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1246 VMChangeStateEntry
*e
;
1248 e
= g_malloc0(sizeof (*e
));
1252 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1256 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1258 QLIST_REMOVE (e
, entries
);
1262 void vm_state_notify(int running
, RunState state
)
1264 VMChangeStateEntry
*e
;
1266 trace_vm_state_notify(running
, state
);
1268 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1269 e
->cb(e
->opaque
, running
, state
);
1275 if (!runstate_is_running()) {
1277 runstate_set(RSTATE_RUNNING
);
1278 vm_state_notify(1, RSTATE_RUNNING
);
1280 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1284 /* reset/shutdown handler */
1286 typedef struct QEMUResetEntry
{
1287 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1288 QEMUResetHandler
*func
;
1292 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1293 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1294 static int reset_requested
;
1295 static int shutdown_requested
, shutdown_signal
= -1;
1296 static pid_t shutdown_pid
;
1297 static int powerdown_requested
;
1298 static int debug_requested
;
1299 static RunState vmstop_requested
= RSTATE_NO_STATE
;
1301 int qemu_shutdown_requested_get(void)
1303 return shutdown_requested
;
1306 int qemu_reset_requested_get(void)
1308 return reset_requested
;
1311 int qemu_shutdown_requested(void)
1313 int r
= shutdown_requested
;
1314 shutdown_requested
= 0;
1318 void qemu_kill_report(void)
1320 if (shutdown_signal
!= -1) {
1321 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1322 if (shutdown_pid
== 0) {
1323 /* This happens for eg ^C at the terminal, so it's worth
1324 * avoiding printing an odd message in that case.
1326 fputc('\n', stderr
);
1328 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1330 shutdown_signal
= -1;
1334 int qemu_reset_requested(void)
1336 int r
= reset_requested
;
1337 reset_requested
= 0;
1341 int qemu_powerdown_requested(void)
1343 int r
= powerdown_requested
;
1344 powerdown_requested
= 0;
1348 static int qemu_debug_requested(void)
1350 int r
= debug_requested
;
1351 debug_requested
= 0;
1355 static RunState
qemu_vmstop_requested(void)
1357 RunState s
= vmstop_requested
;
1358 vmstop_requested
= RSTATE_NO_STATE
;
1362 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1364 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1367 re
->opaque
= opaque
;
1368 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1371 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1375 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1376 if (re
->func
== func
&& re
->opaque
== opaque
) {
1377 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1384 void qemu_system_reset(bool report
)
1386 QEMUResetEntry
*re
, *nre
;
1388 /* reset all devices */
1389 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1390 re
->func(re
->opaque
);
1393 monitor_protocol_event(QEVENT_RESET
, NULL
);
1395 cpu_synchronize_all_post_reset();
1398 void qemu_system_reset_request(void)
1401 shutdown_requested
= 1;
1403 reset_requested
= 1;
1406 qemu_notify_event();
1409 void qemu_system_killed(int signal
, pid_t pid
)
1411 shutdown_signal
= signal
;
1414 qemu_system_shutdown_request();
1417 void qemu_system_shutdown_request(void)
1419 shutdown_requested
= 1;
1420 qemu_notify_event();
1423 void qemu_system_powerdown_request(void)
1425 powerdown_requested
= 1;
1426 qemu_notify_event();
1429 void qemu_system_debug_request(void)
1431 debug_requested
= 1;
1432 qemu_notify_event();
1435 void qemu_system_vmstop_request(RunState state
)
1437 vmstop_requested
= state
;
1438 qemu_notify_event();
1441 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1442 static int n_poll_fds
;
1443 static int max_priority
;
1445 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1446 fd_set
*xfds
, struct timeval
*tv
)
1448 GMainContext
*context
= g_main_context_default();
1450 int timeout
= 0, cur_timeout
;
1452 g_main_context_prepare(context
, &max_priority
);
1454 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1455 poll_fds
, ARRAY_SIZE(poll_fds
));
1456 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1458 for (i
= 0; i
< n_poll_fds
; i
++) {
1459 GPollFD
*p
= &poll_fds
[i
];
1461 if ((p
->events
& G_IO_IN
)) {
1462 FD_SET(p
->fd
, rfds
);
1463 *max_fd
= MAX(*max_fd
, p
->fd
);
1465 if ((p
->events
& G_IO_OUT
)) {
1466 FD_SET(p
->fd
, wfds
);
1467 *max_fd
= MAX(*max_fd
, p
->fd
);
1469 if ((p
->events
& G_IO_ERR
)) {
1470 FD_SET(p
->fd
, xfds
);
1471 *max_fd
= MAX(*max_fd
, p
->fd
);
1475 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1476 if (timeout
>= 0 && timeout
< cur_timeout
) {
1477 tv
->tv_sec
= timeout
/ 1000;
1478 tv
->tv_usec
= (timeout
% 1000) * 1000;
1482 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1485 GMainContext
*context
= g_main_context_default();
1490 for (i
= 0; i
< n_poll_fds
; i
++) {
1491 GPollFD
*p
= &poll_fds
[i
];
1493 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1494 p
->revents
|= G_IO_IN
;
1496 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1497 p
->revents
|= G_IO_OUT
;
1499 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1500 p
->revents
|= G_IO_ERR
;
1505 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1506 g_main_context_dispatch(context
);
1510 int main_loop_wait(int nonblocking
)
1512 fd_set rfds
, wfds
, xfds
;
1520 timeout
= qemu_calculate_timeout();
1521 qemu_bh_update_timeout(&timeout
);
1524 os_host_main_loop_wait(&timeout
);
1526 tv
.tv_sec
= timeout
/ 1000;
1527 tv
.tv_usec
= (timeout
% 1000) * 1000;
1529 /* poll any events */
1530 /* XXX: separate device handlers from system ones */
1536 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1537 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1538 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1541 qemu_mutex_unlock_iothread();
1544 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1547 qemu_mutex_lock_iothread();
1550 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1551 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1552 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1554 qemu_run_all_timers();
1556 /* Check bottom-halves last in case any of the earlier events triggered
1563 qemu_irq qemu_system_powerdown
;
1565 static void main_loop(void)
1568 int last_io
__attribute__ ((unused
)) = 0;
1569 #ifdef CONFIG_PROFILER
1574 qemu_main_loop_start();
1577 nonblocking
= !kvm_enabled() && last_io
> 0;
1578 #ifdef CONFIG_PROFILER
1579 ti
= profile_getclock();
1581 last_io
= main_loop_wait(nonblocking
);
1582 #ifdef CONFIG_PROFILER
1583 dev_time
+= profile_getclock() - ti
;
1586 if (qemu_debug_requested()) {
1587 vm_stop(RSTATE_DEBUG
);
1589 if (qemu_shutdown_requested()) {
1591 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1593 vm_stop(RSTATE_SHUTDOWN
);
1597 if (qemu_reset_requested()) {
1599 cpu_synchronize_all_states();
1600 qemu_system_reset(VMRESET_REPORT
);
1602 if (runstate_check(RSTATE_PANICKED
) ||
1603 runstate_check(RSTATE_SHUTDOWN
)) {
1604 runstate_set(RSTATE_PAUSED
);
1607 if (qemu_powerdown_requested()) {
1608 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1609 qemu_irq_raise(qemu_system_powerdown
);
1611 if ((r
= qemu_vmstop_requested())) {
1619 static void version(void)
1621 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1624 static void help(int exitcode
)
1626 const char *options_help
=
1627 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1629 #define DEFHEADING(text) stringify(text) "\n"
1630 #include "qemu-options.def"
1636 printf("usage: %s [options] [disk_image]\n"
1638 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1641 "During emulation, the following keys are useful:\n"
1642 "ctrl-alt-f toggle full screen\n"
1643 "ctrl-alt-n switch to virtual console 'n'\n"
1644 "ctrl-alt toggle mouse and keyboard grab\n"
1646 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1652 #define HAS_ARG 0x0001
1654 typedef struct QEMUOption
{
1661 static const QEMUOption qemu_options
[] = {
1662 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1663 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1664 { option, opt_arg, opt_enum, arch_mask },
1665 #define DEFHEADING(text)
1666 #include "qemu-options.def"
1672 static void select_vgahw (const char *p
)
1677 vga_interface_type
= VGA_NONE
;
1678 if (strstart(p
, "std", &opts
)) {
1679 vga_interface_type
= VGA_STD
;
1680 } else if (strstart(p
, "cirrus", &opts
)) {
1681 vga_interface_type
= VGA_CIRRUS
;
1682 } else if (strstart(p
, "vmware", &opts
)) {
1683 vga_interface_type
= VGA_VMWARE
;
1684 } else if (strstart(p
, "xenfb", &opts
)) {
1685 vga_interface_type
= VGA_XENFB
;
1686 } else if (strstart(p
, "qxl", &opts
)) {
1687 vga_interface_type
= VGA_QXL
;
1688 } else if (!strstart(p
, "none", &opts
)) {
1690 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1694 const char *nextopt
;
1696 if (strstart(opts
, ",retrace=", &nextopt
)) {
1698 if (strstart(opts
, "dumb", &nextopt
))
1699 vga_retrace_method
= VGA_RETRACE_DUMB
;
1700 else if (strstart(opts
, "precise", &nextopt
))
1701 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1702 else goto invalid_vga
;
1703 } else goto invalid_vga
;
1708 static DisplayType
select_display(const char *p
)
1711 DisplayType display
= DT_DEFAULT
;
1713 if (strstart(p
, "sdl", &opts
)) {
1717 const char *nextopt
;
1719 if (strstart(opts
, ",frame=", &nextopt
)) {
1721 if (strstart(opts
, "on", &nextopt
)) {
1723 } else if (strstart(opts
, "off", &nextopt
)) {
1726 goto invalid_sdl_args
;
1728 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1730 if (strstart(opts
, "on", &nextopt
)) {
1732 } else if (strstart(opts
, "off", &nextopt
)) {
1735 goto invalid_sdl_args
;
1737 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1739 if (strstart(opts
, "on", &nextopt
)) {
1741 } else if (strstart(opts
, "off", &nextopt
)) {
1744 goto invalid_sdl_args
;
1746 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1748 if (strstart(opts
, "on", &nextopt
)) {
1750 } else if (strstart(opts
, "off", &nextopt
)) {
1753 goto invalid_sdl_args
;
1757 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1763 fprintf(stderr
, "SDL support is disabled\n");
1766 } else if (strstart(p
, "vnc", &opts
)) {
1771 const char *nextopt
;
1773 if (strstart(opts
, "=", &nextopt
)) {
1774 vnc_display
= nextopt
;
1778 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1782 fprintf(stderr
, "VNC support is disabled\n");
1785 } else if (strstart(p
, "curses", &opts
)) {
1786 #ifdef CONFIG_CURSES
1787 display
= DT_CURSES
;
1789 fprintf(stderr
, "Curses support is disabled\n");
1792 } else if (strstart(p
, "none", &opts
)) {
1795 fprintf(stderr
, "Unknown display type: %s\n", p
);
1802 static int balloon_parse(const char *arg
)
1806 if (strcmp(arg
, "none") == 0) {
1810 if (!strncmp(arg
, "virtio", 6)) {
1811 if (arg
[6] == ',') {
1812 /* have params -> parse them */
1813 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1817 /* create empty opts */
1818 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1820 qemu_opt_set(opts
, "driver", "virtio-balloon");
1827 char *qemu_find_file(int type
, const char *name
)
1833 /* If name contains path separators then try it as a straight path. */
1834 if ((strchr(name
, '/') || strchr(name
, '\\'))
1835 && access(name
, R_OK
) == 0) {
1836 return g_strdup(name
);
1839 case QEMU_FILE_TYPE_BIOS
:
1842 case QEMU_FILE_TYPE_KEYMAP
:
1843 subdir
= "keymaps/";
1848 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1849 buf
= g_malloc0(len
);
1850 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1851 if (access(buf
, R_OK
)) {
1858 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1860 return qdev_device_help(opts
);
1863 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1867 dev
= qdev_device_add(opts
);
1873 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1875 CharDriverState
*chr
;
1877 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1883 #ifdef CONFIG_VIRTFS
1884 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1887 ret
= qemu_fsdev_add(opts
);
1893 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1895 CharDriverState
*chr
;
1896 const char *chardev
;
1900 mode
= qemu_opt_get(opts
, "mode");
1904 if (strcmp(mode
, "readline") == 0) {
1905 flags
= MONITOR_USE_READLINE
;
1906 } else if (strcmp(mode
, "control") == 0) {
1907 flags
= MONITOR_USE_CONTROL
;
1909 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1913 if (qemu_opt_get_bool(opts
, "pretty", 0))
1914 flags
|= MONITOR_USE_PRETTY
;
1916 if (qemu_opt_get_bool(opts
, "default", 0))
1917 flags
|= MONITOR_IS_DEFAULT
;
1919 chardev
= qemu_opt_get(opts
, "chardev");
1920 chr
= qemu_chr_find(chardev
);
1922 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1926 monitor_init(chr
, flags
);
1930 static void monitor_parse(const char *optarg
, const char *mode
)
1932 static int monitor_device_index
= 0;
1938 if (strstart(optarg
, "chardev:", &p
)) {
1939 snprintf(label
, sizeof(label
), "%s", p
);
1941 snprintf(label
, sizeof(label
), "compat_monitor%d",
1942 monitor_device_index
);
1943 if (monitor_device_index
== 0) {
1946 opts
= qemu_chr_parse_compat(label
, optarg
);
1948 fprintf(stderr
, "parse error: %s\n", optarg
);
1953 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1955 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1958 qemu_opt_set(opts
, "mode", mode
);
1959 qemu_opt_set(opts
, "chardev", label
);
1961 qemu_opt_set(opts
, "default", "on");
1962 monitor_device_index
++;
1965 struct device_config
{
1967 DEV_USB
, /* -usbdevice */
1969 DEV_SERIAL
, /* -serial */
1970 DEV_PARALLEL
, /* -parallel */
1971 DEV_VIRTCON
, /* -virtioconsole */
1972 DEV_DEBUGCON
, /* -debugcon */
1974 const char *cmdline
;
1975 QTAILQ_ENTRY(device_config
) next
;
1977 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1979 static void add_device_config(int type
, const char *cmdline
)
1981 struct device_config
*conf
;
1983 conf
= g_malloc0(sizeof(*conf
));
1985 conf
->cmdline
= cmdline
;
1986 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1989 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1991 struct device_config
*conf
;
1994 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1995 if (conf
->type
!= type
)
1997 rc
= func(conf
->cmdline
);
2004 static int serial_parse(const char *devname
)
2006 static int index
= 0;
2009 if (strcmp(devname
, "none") == 0)
2011 if (index
== MAX_SERIAL_PORTS
) {
2012 fprintf(stderr
, "qemu: too many serial ports\n");
2015 snprintf(label
, sizeof(label
), "serial%d", index
);
2016 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2017 if (!serial_hds
[index
]) {
2018 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2019 devname
, strerror(errno
));
2026 static int parallel_parse(const char *devname
)
2028 static int index
= 0;
2031 if (strcmp(devname
, "none") == 0)
2033 if (index
== MAX_PARALLEL_PORTS
) {
2034 fprintf(stderr
, "qemu: too many parallel ports\n");
2037 snprintf(label
, sizeof(label
), "parallel%d", index
);
2038 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2039 if (!parallel_hds
[index
]) {
2040 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2041 devname
, strerror(errno
));
2048 static int virtcon_parse(const char *devname
)
2050 QemuOptsList
*device
= qemu_find_opts("device");
2051 static int index
= 0;
2053 QemuOpts
*bus_opts
, *dev_opts
;
2055 if (strcmp(devname
, "none") == 0)
2057 if (index
== MAX_VIRTIO_CONSOLES
) {
2058 fprintf(stderr
, "qemu: too many virtio consoles\n");
2062 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2063 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2065 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2066 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2068 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2069 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2070 if (!virtcon_hds
[index
]) {
2071 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2072 devname
, strerror(errno
));
2075 qemu_opt_set(dev_opts
, "chardev", label
);
2081 static int debugcon_parse(const char *devname
)
2085 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2088 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2090 fprintf(stderr
, "qemu: already have a debugcon device\n");
2093 qemu_opt_set(opts
, "driver", "isa-debugcon");
2094 qemu_opt_set(opts
, "chardev", "debugcon");
2098 static QEMUMachine
*machine_parse(const char *name
)
2100 QEMUMachine
*m
, *machine
= NULL
;
2103 machine
= find_machine(name
);
2108 printf("Supported machines are:\n");
2109 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2111 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2113 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2114 m
->is_default
? " (default)" : "");
2116 exit(!name
|| *name
!= '?');
2119 static int tcg_init(void)
2121 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2126 const char *opt_name
;
2128 int (*available
)(void);
2132 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2133 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2134 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2137 static int configure_accelerator(void)
2139 const char *p
= NULL
;
2142 bool accel_initalised
= 0;
2143 bool init_failed
= 0;
2145 QemuOptsList
*list
= qemu_find_opts("machine");
2146 if (!QTAILQ_EMPTY(&list
->head
)) {
2147 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2152 /* Use the default "accelerator", kvm */
2155 /* Use the default "accelerator", tcg */
2160 while (!accel_initalised
&& *p
!= '\0') {
2164 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2165 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2166 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2167 *(accel_list
[i
].allowed
) = 1;
2168 ret
= accel_list
[i
].init();
2171 if (!accel_list
[i
].available()) {
2172 printf("%s not supported for this target\n",
2173 accel_list
[i
].name
);
2175 fprintf(stderr
, "failed to initialize %s: %s\n",
2179 *(accel_list
[i
].allowed
) = 0;
2181 accel_initalised
= 1;
2186 if (i
== ARRAY_SIZE(accel_list
)) {
2187 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2191 if (!accel_initalised
) {
2192 fprintf(stderr
, "No accelerator found!\n");
2197 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2200 return !accel_initalised
;
2203 void qemu_add_exit_notifier(Notifier
*notify
)
2205 notifier_list_add(&exit_notifiers
, notify
);
2208 void qemu_remove_exit_notifier(Notifier
*notify
)
2210 notifier_list_remove(&exit_notifiers
, notify
);
2213 static void qemu_run_exit_notifiers(void)
2215 notifier_list_notify(&exit_notifiers
, NULL
);
2218 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2220 notifier_list_add(&machine_init_done_notifiers
, notify
);
2223 static void qemu_run_machine_init_done_notifiers(void)
2225 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2228 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2229 const char **poptarg
, int *poptind
)
2231 const QEMUOption
*popt
;
2232 int optind
= *poptind
;
2233 char *r
= argv
[optind
];
2236 loc_set_cmdline(argv
, optind
, 1);
2238 /* Treat --foo the same as -foo. */
2241 popt
= qemu_options
;
2244 error_report("invalid option");
2247 if (!strcmp(popt
->name
, r
+ 1))
2251 if (popt
->flags
& HAS_ARG
) {
2252 if (optind
>= argc
) {
2253 error_report("requires an argument");
2256 optarg
= argv
[optind
++];
2257 loc_set_cmdline(argv
, optind
- 2, 2);
2268 static gpointer
malloc_and_trace(gsize n_bytes
)
2270 void *ptr
= malloc(n_bytes
);
2271 trace_g_malloc(n_bytes
, ptr
);
2275 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2277 void *ptr
= realloc(mem
, n_bytes
);
2278 trace_g_realloc(mem
, n_bytes
, ptr
);
2282 static void free_and_trace(gpointer mem
)
2288 int main(int argc
, char **argv
, char **envp
)
2290 const char *gdbstub_dev
= NULL
;
2292 int snapshot
, linux_boot
;
2293 const char *icount_option
= NULL
;
2294 const char *initrd_filename
;
2295 const char *kernel_filename
, *kernel_cmdline
;
2296 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2298 DisplayChangeListener
*dcl
;
2299 int cyls
, heads
, secs
, translation
;
2300 QemuOpts
*hda_opts
= NULL
, *opts
;
2301 QemuOptsList
*olist
;
2304 const char *loadvm
= NULL
;
2305 QEMUMachine
*machine
;
2306 const char *cpu_model
;
2307 const char *pid_file
= NULL
;
2308 const char *incoming
= NULL
;
2310 int show_vnc_port
= 0;
2313 const char *log_mask
= NULL
;
2314 const char *log_file
= NULL
;
2315 GMemVTable mem_trace
= {
2316 .malloc
= malloc_and_trace
,
2317 .realloc
= realloc_and_trace
,
2318 .free
= free_and_trace
,
2320 const char *trace_events
= NULL
;
2321 const char *trace_file
= NULL
;
2323 atexit(qemu_run_exit_notifiers
);
2324 error_set_progname(argv
[0]);
2326 g_mem_set_vtable(&mem_trace
);
2327 g_thread_init(NULL
);
2333 qemu_cache_utils_init(envp
);
2335 QLIST_INIT (&vm_change_state_head
);
2336 os_setup_early_signal_handling();
2338 module_call_init(MODULE_INIT_MACHINE
);
2339 machine
= find_default_machine();
2341 initrd_filename
= NULL
;
2344 kernel_filename
= NULL
;
2345 kernel_cmdline
= "";
2346 cyls
= heads
= secs
= 0;
2347 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2349 for (i
= 0; i
< MAX_NODES
; i
++) {
2351 node_cpumask
[i
] = 0;
2359 /* first pass of option parsing */
2361 while (optind
< argc
) {
2362 if (argv
[optind
][0] != '-') {
2367 const QEMUOption
*popt
;
2369 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2370 switch (popt
->index
) {
2371 case QEMU_OPTION_nodefconfig
:
2381 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2382 if (ret
< 0 && ret
!= -ENOENT
) {
2386 ret
= qemu_read_config_file(arch_config_name
);
2387 if (ret
< 0 && ret
!= -ENOENT
) {
2393 /* second pass of option parsing */
2398 if (argv
[optind
][0] != '-') {
2399 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2401 const QEMUOption
*popt
;
2403 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2404 if (!(popt
->arch_mask
& arch_type
)) {
2405 printf("Option %s not supported for this target\n", popt
->name
);
2408 switch(popt
->index
) {
2410 machine
= machine_parse(optarg
);
2412 case QEMU_OPTION_cpu
:
2413 /* hw initialization will check this */
2414 if (*optarg
== '?') {
2415 list_cpus(stdout
, &fprintf
, optarg
);
2421 case QEMU_OPTION_initrd
:
2422 initrd_filename
= optarg
;
2424 case QEMU_OPTION_hda
:
2428 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2430 snprintf(buf
, sizeof(buf
),
2431 "%s,cyls=%d,heads=%d,secs=%d%s",
2432 HD_OPTS
, cyls
, heads
, secs
,
2433 translation
== BIOS_ATA_TRANSLATION_LBA
?
2435 translation
== BIOS_ATA_TRANSLATION_NONE
?
2436 ",trans=none" : "");
2437 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2440 case QEMU_OPTION_hdb
:
2441 case QEMU_OPTION_hdc
:
2442 case QEMU_OPTION_hdd
:
2443 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2446 case QEMU_OPTION_drive
:
2447 if (drive_def(optarg
) == NULL
) {
2451 case QEMU_OPTION_set
:
2452 if (qemu_set_option(optarg
) != 0)
2455 case QEMU_OPTION_global
:
2456 if (qemu_global_option(optarg
) != 0)
2459 case QEMU_OPTION_mtdblock
:
2460 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2462 case QEMU_OPTION_sd
:
2463 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2465 case QEMU_OPTION_pflash
:
2466 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2468 case QEMU_OPTION_snapshot
:
2471 case QEMU_OPTION_hdachs
:
2475 cyls
= strtol(p
, (char **)&p
, 0);
2476 if (cyls
< 1 || cyls
> 16383)
2481 heads
= strtol(p
, (char **)&p
, 0);
2482 if (heads
< 1 || heads
> 16)
2487 secs
= strtol(p
, (char **)&p
, 0);
2488 if (secs
< 1 || secs
> 63)
2492 if (!strcmp(p
, "none"))
2493 translation
= BIOS_ATA_TRANSLATION_NONE
;
2494 else if (!strcmp(p
, "lba"))
2495 translation
= BIOS_ATA_TRANSLATION_LBA
;
2496 else if (!strcmp(p
, "auto"))
2497 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2500 } else if (*p
!= '\0') {
2502 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2505 if (hda_opts
!= NULL
) {
2507 snprintf(num
, sizeof(num
), "%d", cyls
);
2508 qemu_opt_set(hda_opts
, "cyls", num
);
2509 snprintf(num
, sizeof(num
), "%d", heads
);
2510 qemu_opt_set(hda_opts
, "heads", num
);
2511 snprintf(num
, sizeof(num
), "%d", secs
);
2512 qemu_opt_set(hda_opts
, "secs", num
);
2513 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2514 qemu_opt_set(hda_opts
, "trans", "lba");
2515 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2516 qemu_opt_set(hda_opts
, "trans", "none");
2520 case QEMU_OPTION_numa
:
2521 if (nb_numa_nodes
>= MAX_NODES
) {
2522 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2527 case QEMU_OPTION_display
:
2528 display_type
= select_display(optarg
);
2530 case QEMU_OPTION_nographic
:
2531 display_type
= DT_NOGRAPHIC
;
2533 case QEMU_OPTION_curses
:
2534 #ifdef CONFIG_CURSES
2535 display_type
= DT_CURSES
;
2537 fprintf(stderr
, "Curses support is disabled\n");
2541 case QEMU_OPTION_portrait
:
2542 graphic_rotate
= 90;
2544 case QEMU_OPTION_rotate
:
2545 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2546 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2547 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2549 "qemu: only 90, 180, 270 deg rotation is available\n");
2553 case QEMU_OPTION_kernel
:
2554 kernel_filename
= optarg
;
2556 case QEMU_OPTION_append
:
2557 kernel_cmdline
= optarg
;
2559 case QEMU_OPTION_cdrom
:
2560 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2562 case QEMU_OPTION_boot
:
2564 static const char * const params
[] = {
2565 "order", "once", "menu",
2566 "splash", "splash-time", NULL
2568 char buf
[sizeof(boot_devices
)];
2569 char *standard_boot_devices
;
2572 if (!strchr(optarg
, '=')) {
2574 pstrcpy(buf
, sizeof(buf
), optarg
);
2575 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2577 "qemu: unknown boot parameter '%s' in '%s'\n",
2583 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2584 validate_bootdevices(buf
);
2585 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2588 if (get_param_value(buf
, sizeof(buf
),
2590 validate_bootdevices(buf
);
2591 standard_boot_devices
= g_strdup(boot_devices
);
2592 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2593 qemu_register_reset(restore_boot_devices
,
2594 standard_boot_devices
);
2596 if (get_param_value(buf
, sizeof(buf
),
2598 if (!strcmp(buf
, "on")) {
2600 } else if (!strcmp(buf
, "off")) {
2604 "qemu: invalid option value '%s'\n",
2609 qemu_opts_parse(qemu_find_opts("boot-opts"),
2614 case QEMU_OPTION_fda
:
2615 case QEMU_OPTION_fdb
:
2616 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2619 case QEMU_OPTION_no_fd_bootchk
:
2622 case QEMU_OPTION_netdev
:
2623 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2627 case QEMU_OPTION_net
:
2628 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2633 case QEMU_OPTION_tftp
:
2634 legacy_tftp_prefix
= optarg
;
2636 case QEMU_OPTION_bootp
:
2637 legacy_bootp_filename
= optarg
;
2639 case QEMU_OPTION_redir
:
2640 if (net_slirp_redir(optarg
) < 0)
2644 case QEMU_OPTION_bt
:
2645 add_device_config(DEV_BT
, optarg
);
2647 case QEMU_OPTION_audio_help
:
2648 if (!(audio_available())) {
2649 printf("Option %s not supported for this target\n", popt
->name
);
2655 case QEMU_OPTION_soundhw
:
2656 if (!(audio_available())) {
2657 printf("Option %s not supported for this target\n", popt
->name
);
2660 select_soundhw (optarg
);
2665 case QEMU_OPTION_version
:
2669 case QEMU_OPTION_m
: {
2672 value
= strtosz(optarg
, NULL
);
2674 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2678 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2679 fprintf(stderr
, "qemu: ram size too large\n");
2685 case QEMU_OPTION_mempath
:
2689 case QEMU_OPTION_mem_prealloc
:
2700 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2702 case QEMU_OPTION_gdb
:
2703 gdbstub_dev
= optarg
;
2708 case QEMU_OPTION_bios
:
2711 case QEMU_OPTION_singlestep
:
2718 keyboard_layout
= optarg
;
2720 case QEMU_OPTION_localtime
:
2723 case QEMU_OPTION_vga
:
2724 select_vgahw (optarg
);
2731 w
= strtol(p
, (char **)&p
, 10);
2734 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2740 h
= strtol(p
, (char **)&p
, 10);
2745 depth
= strtol(p
, (char **)&p
, 10);
2746 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2747 depth
!= 24 && depth
!= 32)
2749 } else if (*p
== '\0') {
2750 depth
= graphic_depth
;
2757 graphic_depth
= depth
;
2760 case QEMU_OPTION_echr
:
2763 term_escape_char
= strtol(optarg
, &r
, 0);
2765 printf("Bad argument to echr\n");
2768 case QEMU_OPTION_monitor
:
2769 monitor_parse(optarg
, "readline");
2770 default_monitor
= 0;
2772 case QEMU_OPTION_qmp
:
2773 monitor_parse(optarg
, "control");
2774 default_monitor
= 0;
2776 case QEMU_OPTION_mon
:
2777 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2781 default_monitor
= 0;
2783 case QEMU_OPTION_chardev
:
2784 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2789 case QEMU_OPTION_fsdev
:
2790 olist
= qemu_find_opts("fsdev");
2792 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2795 opts
= qemu_opts_parse(olist
, optarg
, 1);
2797 fprintf(stderr
, "parse error: %s\n", optarg
);
2801 case QEMU_OPTION_virtfs
: {
2805 olist
= qemu_find_opts("virtfs");
2807 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2810 opts
= qemu_opts_parse(olist
, optarg
, 1);
2812 fprintf(stderr
, "parse error: %s\n", optarg
);
2816 if (qemu_opt_get(opts
, "fstype") == NULL
||
2817 qemu_opt_get(opts
, "mount_tag") == NULL
||
2818 qemu_opt_get(opts
, "path") == NULL
||
2819 qemu_opt_get(opts
, "security_model") == NULL
) {
2820 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2821 "security_model=[mapped|passthrough|none],"
2822 "mount_tag=tag.\n");
2826 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2827 qemu_opt_get(opts
, "mount_tag"), 1);
2829 fprintf(stderr
, "duplicate fsdev id: %s\n",
2830 qemu_opt_get(opts
, "mount_tag"));
2833 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2834 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2835 qemu_opt_set(fsdev
, "security_model",
2836 qemu_opt_get(opts
, "security_model"));
2838 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2839 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2840 qemu_opt_set(device
, "fsdev",
2841 qemu_opt_get(opts
, "mount_tag"));
2842 qemu_opt_set(device
, "mount_tag",
2843 qemu_opt_get(opts
, "mount_tag"));
2846 case QEMU_OPTION_serial
:
2847 add_device_config(DEV_SERIAL
, optarg
);
2849 if (strncmp(optarg
, "mon:", 4) == 0) {
2850 default_monitor
= 0;
2853 case QEMU_OPTION_watchdog
:
2856 "qemu: only one watchdog option may be given\n");
2861 case QEMU_OPTION_watchdog_action
:
2862 if (select_watchdog_action(optarg
) == -1) {
2863 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2867 case QEMU_OPTION_virtiocon
:
2868 add_device_config(DEV_VIRTCON
, optarg
);
2869 default_virtcon
= 0;
2870 if (strncmp(optarg
, "mon:", 4) == 0) {
2871 default_monitor
= 0;
2874 case QEMU_OPTION_parallel
:
2875 add_device_config(DEV_PARALLEL
, optarg
);
2876 default_parallel
= 0;
2877 if (strncmp(optarg
, "mon:", 4) == 0) {
2878 default_monitor
= 0;
2881 case QEMU_OPTION_debugcon
:
2882 add_device_config(DEV_DEBUGCON
, optarg
);
2884 case QEMU_OPTION_loadvm
:
2887 case QEMU_OPTION_full_screen
:
2891 case QEMU_OPTION_no_frame
:
2894 case QEMU_OPTION_alt_grab
:
2897 case QEMU_OPTION_ctrl_grab
:
2900 case QEMU_OPTION_no_quit
:
2903 case QEMU_OPTION_sdl
:
2904 display_type
= DT_SDL
;
2907 case QEMU_OPTION_no_frame
:
2908 case QEMU_OPTION_alt_grab
:
2909 case QEMU_OPTION_ctrl_grab
:
2910 case QEMU_OPTION_no_quit
:
2911 case QEMU_OPTION_sdl
:
2912 fprintf(stderr
, "SDL support is disabled\n");
2915 case QEMU_OPTION_pidfile
:
2918 case QEMU_OPTION_win2k_hack
:
2919 win2k_install_hack
= 1;
2921 case QEMU_OPTION_rtc_td_hack
:
2924 case QEMU_OPTION_acpitable
:
2925 do_acpitable_option(optarg
);
2927 case QEMU_OPTION_smbios
:
2928 do_smbios_option(optarg
);
2930 case QEMU_OPTION_enable_kvm
:
2931 olist
= qemu_find_opts("machine");
2932 qemu_opts_reset(olist
);
2933 qemu_opts_parse(olist
, "accel=kvm", 0);
2935 case QEMU_OPTION_machine
:
2936 olist
= qemu_find_opts("machine");
2937 qemu_opts_reset(olist
);
2938 opts
= qemu_opts_parse(olist
, optarg
, 1);
2940 fprintf(stderr
, "parse error: %s\n", optarg
);
2943 optarg
= qemu_opt_get(opts
, "type");
2945 machine
= machine_parse(optarg
);
2948 case QEMU_OPTION_no_kvm
:
2949 olist
= qemu_find_opts("machine");
2950 qemu_opts_reset(olist
);
2951 qemu_opts_parse(olist
, "accel=tcg", 0);
2954 case QEMU_OPTION_no_kvm_irqchip
: {
2959 case QEMU_OPTION_no_kvm_pit
: {
2963 case QEMU_OPTION_no_kvm_pit_reinjection
: {
2964 kvm_pit_reinject
= 0;
2968 case QEMU_OPTION_usb
:
2971 case QEMU_OPTION_usbdevice
:
2973 add_device_config(DEV_USB
, optarg
);
2975 case QEMU_OPTION_device
:
2976 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2980 case QEMU_OPTION_smp
:
2983 fprintf(stderr
, "Invalid number of CPUs\n");
2986 if (max_cpus
< smp_cpus
) {
2987 fprintf(stderr
, "maxcpus must be equal to or greater than "
2991 if (max_cpus
> 255) {
2992 fprintf(stderr
, "Unsupported number of maxcpus\n");
2996 case QEMU_OPTION_vnc
:
2999 vnc_display
= optarg
;
3001 fprintf(stderr
, "VNC support is disabled\n");
3005 case QEMU_OPTION_no_acpi
:
3008 case QEMU_OPTION_no_hpet
:
3011 case QEMU_OPTION_balloon
:
3012 if (balloon_parse(optarg
) < 0) {
3013 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3017 case QEMU_OPTION_no_reboot
:
3020 case QEMU_OPTION_no_shutdown
:
3023 case QEMU_OPTION_show_cursor
:
3026 case QEMU_OPTION_uuid
:
3027 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3028 fprintf(stderr
, "Fail to parse UUID string."
3029 " Wrong format.\n");
3033 case QEMU_OPTION_option_rom
:
3034 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3035 fprintf(stderr
, "Too many option ROMs\n");
3038 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3039 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3040 option_rom
[nb_option_roms
].bootindex
=
3041 qemu_opt_get_number(opts
, "bootindex", -1);
3042 if (!option_rom
[nb_option_roms
].name
) {
3043 fprintf(stderr
, "Option ROM file is not specified\n");
3048 case QEMU_OPTION_semihosting
:
3049 semihosting_enabled
= 1;
3051 case QEMU_OPTION_tdf
:
3054 case QEMU_OPTION_kvm_shadow_memory
:
3055 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
3057 case QEMU_OPTION_name
:
3058 qemu_name
= g_strdup(optarg
);
3060 char *p
= strchr(qemu_name
, ',');
3063 if (strncmp(p
, "process=", 8)) {
3064 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3068 os_set_proc_name(p
);
3072 case QEMU_OPTION_prom_env
:
3073 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3074 fprintf(stderr
, "Too many prom variables\n");
3077 prom_envs
[nb_prom_envs
] = optarg
;
3080 case QEMU_OPTION_old_param
:
3083 case QEMU_OPTION_clock
:
3084 configure_alarms(optarg
);
3086 case QEMU_OPTION_startdate
:
3087 configure_rtc_date_offset(optarg
, 1);
3089 case QEMU_OPTION_rtc
:
3090 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3094 configure_rtc(opts
);
3096 case QEMU_OPTION_tb_size
:
3097 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3098 if (tcg_tb_size
< 0) {
3102 case QEMU_OPTION_icount
:
3103 icount_option
= optarg
;
3105 case QEMU_OPTION_incoming
:
3108 case QEMU_OPTION_nodefaults
:
3110 default_parallel
= 0;
3111 default_virtcon
= 0;
3112 default_monitor
= 0;
3119 case QEMU_OPTION_xen_domid
:
3120 if (!(xen_available())) {
3121 printf("Option %s not supported for this target\n", popt
->name
);
3124 xen_domid
= atoi(optarg
);
3126 case QEMU_OPTION_xen_create
:
3127 if (!(xen_available())) {
3128 printf("Option %s not supported for this target\n", popt
->name
);
3131 xen_mode
= XEN_CREATE
;
3133 case QEMU_OPTION_xen_attach
:
3134 if (!(xen_available())) {
3135 printf("Option %s not supported for this target\n", popt
->name
);
3138 xen_mode
= XEN_ATTACH
;
3140 case QEMU_OPTION_trace
:
3142 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3146 trace_events
= qemu_opt_get(opts
, "events");
3147 trace_file
= qemu_opt_get(opts
, "file");
3150 case QEMU_OPTION_readconfig
:
3152 int ret
= qemu_read_config_file(optarg
);
3154 fprintf(stderr
, "read config %s: %s\n", optarg
,
3160 case QEMU_OPTION_spice
:
3161 olist
= qemu_find_opts("spice");
3163 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3166 opts
= qemu_opts_parse(olist
, optarg
, 0);
3168 fprintf(stderr
, "parse error: %s\n", optarg
);
3172 case QEMU_OPTION_writeconfig
:
3175 if (strcmp(optarg
, "-") == 0) {
3178 fp
= fopen(optarg
, "w");
3180 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3184 qemu_config_write(fp
);
3189 os_parse_cmd_args(popt
->index
, optarg
);
3195 /* Open the logfile at this point, if necessary. We can't open the logfile
3196 * when encountering either of the logging options (-d or -D) because the
3197 * other one may be encountered later on the command line, changing the
3198 * location or level of logging.
3202 set_cpu_log_filename(log_file
);
3204 set_cpu_log(log_mask
);
3207 if (!trace_backend_init(trace_events
, trace_file
)) {
3211 /* If no data_dir is specified then try to find it relative to the
3214 data_dir
= os_find_datadir(argv
[0]);
3216 /* If all else fails use the install path specified when building. */
3218 data_dir
= CONFIG_QEMU_DATADIR
;
3222 * Default to max_cpus = smp_cpus, in case the user doesn't
3223 * specify a max_cpus value.
3226 max_cpus
= smp_cpus
;
3228 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3229 if (smp_cpus
> machine
->max_cpus
) {
3230 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3231 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3237 * Get the default machine options from the machine if it is not already
3238 * specified either by the configuration file or by the command line.
3240 if (machine
->default_machine_opts
) {
3241 QemuOptsList
*list
= qemu_find_opts("machine");
3242 const char *p
= NULL
;
3244 if (!QTAILQ_EMPTY(&list
->head
)) {
3245 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3248 qemu_opts_reset(list
);
3249 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3251 fprintf(stderr
, "parse error for machine %s: %s\n",
3252 machine
->name
, machine
->default_machine_opts
);
3258 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3259 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3261 if (machine
->no_serial
) {
3264 if (machine
->no_parallel
) {
3265 default_parallel
= 0;
3267 if (!machine
->use_virtcon
) {
3268 default_virtcon
= 0;
3270 if (machine
->no_vga
) {
3273 if (machine
->no_floppy
) {
3276 if (machine
->no_cdrom
) {
3279 if (machine
->no_sdcard
) {
3283 if (display_type
== DT_NOGRAPHIC
) {
3284 if (default_parallel
)
3285 add_device_config(DEV_PARALLEL
, "null");
3286 if (default_serial
&& default_monitor
) {
3287 add_device_config(DEV_SERIAL
, "mon:stdio");
3288 } else if (default_virtcon
&& default_monitor
) {
3289 add_device_config(DEV_VIRTCON
, "mon:stdio");
3292 add_device_config(DEV_SERIAL
, "stdio");
3293 if (default_virtcon
)
3294 add_device_config(DEV_VIRTCON
, "stdio");
3295 if (default_monitor
)
3296 monitor_parse("stdio", "readline");
3300 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3301 if (default_parallel
)
3302 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3303 if (default_monitor
)
3304 monitor_parse("vc:80Cx24C", "readline");
3305 if (default_virtcon
)
3306 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3309 vga_interface_type
= VGA_CIRRUS
;
3313 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3315 #ifdef CONFIG_VIRTFS
3316 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3323 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3328 /* init the memory */
3329 if (ram_size
== 0) {
3330 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3333 configure_accelerator();
3335 if (qemu_init_main_loop()) {
3336 fprintf(stderr
, "qemu_init_main_loop failed\n");
3339 linux_boot
= (kernel_filename
!= NULL
);
3341 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3342 fprintf(stderr
, "-append only allowed with -kernel option\n");
3346 if (!linux_boot
&& initrd_filename
!= NULL
) {
3347 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3351 os_set_line_buffering();
3353 if (init_timer_alarm() < 0) {
3354 fprintf(stderr
, "could not initialize alarm timer\n");
3357 configure_icount(icount_option
);
3359 if (net_init_clients() < 0) {
3363 /* init the bluetooth world */
3364 if (foreach_device_config(DEV_BT
, bt_parse
))
3367 if (!xen_enabled()) {
3368 /* On 32-bit hosts, QEMU is limited by virtual address space */
3369 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3370 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3375 cpu_exec_init_all();
3377 bdrv_init_with_whitelist();
3381 /* open the virtual block devices */
3383 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3384 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3387 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3388 IF_DEFAULT
, 2, CDROM_OPTS
);
3389 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3390 IF_FLOPPY
, 0, FD_OPTS
);
3391 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3394 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3397 if (nb_numa_nodes
> 0) {
3400 if (nb_numa_nodes
> MAX_NODES
) {
3401 nb_numa_nodes
= MAX_NODES
;
3404 /* If no memory size if given for any node, assume the default case
3405 * and distribute the available memory equally across all nodes
3407 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3408 if (node_mem
[i
] != 0)
3411 if (i
== nb_numa_nodes
) {
3412 uint64_t usedmem
= 0;
3414 /* On Linux, the each node's border has to be 8MB aligned,
3415 * the final node gets the rest.
3417 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3418 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3419 usedmem
+= node_mem
[i
];
3421 node_mem
[i
] = ram_size
- usedmem
;
3424 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3425 if (node_cpumask
[i
] != 0)
3428 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3429 * must cope with this anyway, because there are BIOSes out there in
3430 * real machines which also use this scheme.
3432 if (i
== nb_numa_nodes
) {
3433 for (i
= 0; i
< smp_cpus
; i
++) {
3434 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3439 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3443 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3445 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3447 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3449 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3452 module_call_init(MODULE_INIT_DEVICE
);
3454 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3458 i
= select_watchdog(watchdog
);
3460 exit (i
== 1 ? 1 : 0);
3463 if (machine
->compat_props
) {
3464 qdev_prop_register_global_list(machine
->compat_props
);
3468 machine
->init(ram_size
, boot_devices
,
3469 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3471 cpu_synchronize_all_post_init();
3475 current_machine
= machine
;
3477 /* init USB devices */
3479 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3483 /* init generic devices */
3484 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3487 net_check_clients();
3489 /* just use the first displaystate for the moment */
3490 ds
= get_displaystate();
3494 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3495 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3496 display_type
= DT_SDL
;
3497 #elif defined(CONFIG_VNC)
3498 vnc_display
= "localhost:0,to=99";
3501 display_type
= DT_NONE
;
3506 /* init local displays */
3507 switch (display_type
) {
3510 #if defined(CONFIG_CURSES)
3512 curses_display_init(ds
, full_screen
);
3515 #if defined(CONFIG_SDL)
3517 sdl_display_init(ds
, full_screen
, no_frame
);
3519 #elif defined(CONFIG_COCOA)
3521 cocoa_display_init(ds
, full_screen
);
3528 /* must be after terminal init, SDL library changes signal handlers */
3529 os_setup_signal_handling();
3532 /* init remote displays */
3534 vnc_display_init(ds
);
3535 if (vnc_display_open(ds
, vnc_display
) < 0)
3538 if (show_vnc_port
) {
3539 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3544 if (using_spice
&& !qxl_enabled
) {
3545 qemu_spice_display_init(ds
);
3551 dcl
= ds
->listeners
;
3552 while (dcl
!= NULL
) {
3553 if (dcl
->dpy_refresh
!= NULL
) {
3554 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3555 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3560 if (ds
->gui_timer
== NULL
) {
3561 nographic_timer
= qemu_new_timer_ms(rt_clock
, nographic_update
, NULL
);
3562 qemu_mod_timer(nographic_timer
, qemu_get_clock_ms(rt_clock
));
3564 text_consoles_set_display(ds
);
3566 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3567 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3572 qdev_machine_creation_done();
3574 if (rom_load_all() != 0) {
3575 fprintf(stderr
, "rom loading failed\n");
3579 /* TODO: once all bus devices are qdevified, this should be done
3580 * when bus is created by qdev.c */
3581 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3582 qemu_run_machine_init_done_notifiers();
3584 qemu_system_reset(VMRESET_SILENT
);
3586 if (load_vmstate(loadvm
) < 0) {
3592 runstate_set(RSTATE_IN_MIGRATE
);
3593 int ret
= qemu_start_incoming_migration(incoming
);
3595 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3599 } else if (autostart
) {
3602 runstate_set(RSTATE_PRE_LAUNCH
);