4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
147 #include "qemu-option.h"
148 #include "qemu-config.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "main-loop.h"
153 #include "fsdev/qemu-fsdev.h"
158 #include "qemu_socket.h"
160 #include "slirp/libslirp.h"
163 #include "trace/control.h"
164 #include "qemu-queue.h"
166 #include "arch_init.h"
168 #include "ui/qemu-spice.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 #define MAX_VIRTIO_CONSOLES 1
177 static const char *data_dir
;
178 const char *bios_name
= NULL
;
179 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
180 DisplayType display_type
= DT_DEFAULT
;
181 int display_remote
= 0;
182 const char* keyboard_layout
= NULL
;
184 const char *mem_path
= NULL
;
186 int mem_prealloc
= 0; /* force preallocation of physical target memory */
189 NICInfo nd_table
[MAX_NICS
];
191 static int rtc_utc
= 1;
192 static int rtc_date_offset
= -1; /* -1 means no change */
193 QEMUClock
*rtc_clock
;
194 int vga_interface_type
= VGA_NONE
;
195 static int full_screen
= 0;
197 static int no_frame
= 0;
200 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
201 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
202 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
203 int win2k_install_hack
= 0;
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;
225 const char *qemu_name
;
228 unsigned int nb_prom_envs
= 0;
229 const char *prom_envs
[MAX_PROM_ENVS
];
231 uint8_t *boot_splash_filedata
;
232 int boot_splash_filedata_size
;
233 uint8_t qemu_extra_params_fw
[2];
235 typedef struct FWBootEntry FWBootEntry
;
238 QTAILQ_ENTRY(FWBootEntry
) link
;
244 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
247 uint64_t node_mem
[MAX_NODES
];
248 uint64_t node_cpumask
[MAX_NODES
];
250 uint8_t qemu_uuid
[16];
252 static QEMUBootSetHandler
*boot_set_handler
;
253 static void *boot_set_opaque
;
255 static NotifierList exit_notifiers
=
256 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
258 static NotifierList machine_init_done_notifiers
=
259 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
261 static int tcg_allowed
= 1;
265 enum xen_mode xen_mode
= XEN_EMULATE
;
266 static int tcg_tb_size
;
268 static int default_serial
= 1;
269 static int default_parallel
= 1;
270 static int default_virtcon
= 1;
271 static int default_monitor
= 1;
272 static int default_floppy
= 1;
273 static int default_cdrom
= 1;
274 static int default_sdcard
= 1;
280 { .driver
= "isa-serial", .flag
= &default_serial
},
281 { .driver
= "isa-parallel", .flag
= &default_parallel
},
282 { .driver
= "isa-fdc", .flag
= &default_floppy
},
283 { .driver
= "ide-cd", .flag
= &default_cdrom
},
284 { .driver
= "ide-hd", .flag
= &default_cdrom
},
285 { .driver
= "ide-drive", .flag
= &default_cdrom
},
286 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
287 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
288 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
289 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
292 static void res_free(void)
294 if (boot_splash_filedata
!= NULL
) {
295 g_free(boot_splash_filedata
);
296 boot_splash_filedata
= NULL
;
300 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
302 const char *driver
= qemu_opt_get(opts
, "driver");
307 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
308 if (strcmp(default_list
[i
].driver
, driver
) != 0)
310 *(default_list
[i
].flag
) = 0;
315 /***********************************************************/
318 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
323 } RunStateTransition
;
325 static const RunStateTransition runstate_transitions_def
[] = {
327 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
329 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
330 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
332 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
333 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
335 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
336 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
338 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
339 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
341 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
342 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
344 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
345 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
346 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
348 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
349 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
351 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
353 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
354 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
355 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
356 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
357 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
358 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
359 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
360 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
361 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
363 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
365 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
366 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
368 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
369 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
371 { RUN_STATE_MAX
, RUN_STATE_MAX
},
374 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
376 bool runstate_check(RunState state
)
378 return current_run_state
== state
;
381 void runstate_init(void)
383 const RunStateTransition
*p
;
385 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
387 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
388 runstate_valid_transitions
[p
->from
][p
->to
] = true;
392 /* This function will abort() on invalid state transitions */
393 void runstate_set(RunState new_state
)
395 assert(new_state
< RUN_STATE_MAX
);
397 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
398 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
399 RunState_lookup
[current_run_state
],
400 RunState_lookup
[new_state
]);
404 current_run_state
= new_state
;
407 int runstate_is_running(void)
409 return runstate_check(RUN_STATE_RUNNING
);
412 StatusInfo
*qmp_query_status(Error
**errp
)
414 StatusInfo
*info
= g_malloc0(sizeof(*info
));
416 info
->running
= runstate_is_running();
417 info
->singlestep
= singlestep
;
418 info
->status
= current_run_state
;
423 /***********************************************************/
424 /* real time host monotonic timer */
426 /***********************************************************/
427 /* host time/date access */
428 void qemu_get_timedate(struct tm
*tm
, int offset
)
435 if (rtc_date_offset
== -1) {
439 ret
= localtime(&ti
);
441 ti
-= rtc_date_offset
;
445 memcpy(tm
, ret
, sizeof(struct tm
));
448 int qemu_timedate_diff(struct tm
*tm
)
452 if (rtc_date_offset
== -1)
454 seconds
= mktimegm(tm
);
457 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
458 seconds
= mktime(&tmp
);
461 seconds
= mktimegm(tm
) + rtc_date_offset
;
463 return seconds
- time(NULL
);
466 void rtc_change_mon_event(struct tm
*tm
)
470 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
471 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
472 qobject_decref(data
);
475 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
477 time_t rtc_start_date
;
480 if (!strcmp(startdate
, "now") && legacy
) {
481 rtc_date_offset
= -1;
483 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
491 } else if (sscanf(startdate
, "%d-%d-%d",
503 rtc_start_date
= mktimegm(&tm
);
504 if (rtc_start_date
== -1) {
506 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
507 "'2006-06-17T16:01:21' or '2006-06-17'\n");
510 rtc_date_offset
= time(NULL
) - rtc_start_date
;
514 static void configure_rtc(QemuOpts
*opts
)
518 value
= qemu_opt_get(opts
, "base");
520 if (!strcmp(value
, "utc")) {
522 } else if (!strcmp(value
, "localtime")) {
525 configure_rtc_date_offset(value
, 0);
528 value
= qemu_opt_get(opts
, "clock");
530 if (!strcmp(value
, "host")) {
531 rtc_clock
= host_clock
;
532 } else if (!strcmp(value
, "vm")) {
533 rtc_clock
= vm_clock
;
535 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
539 value
= qemu_opt_get(opts
, "driftfix");
541 if (!strcmp(value
, "slew")) {
542 static GlobalProperty slew_lost_ticks
[] = {
544 .driver
= "mc146818rtc",
545 .property
= "lost_tick_policy",
548 { /* end of list */ }
551 qdev_prop_register_global_list(slew_lost_ticks
);
552 } else if (!strcmp(value
, "none")) {
553 /* discard is default */
555 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
561 /***********************************************************/
562 /* Bluetooth support */
565 static struct HCIInfo
*hci_table
[MAX_NICS
];
567 static struct bt_vlan_s
{
568 struct bt_scatternet_s net
;
570 struct bt_vlan_s
*next
;
573 /* find or alloc a new bluetooth "VLAN" */
574 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
576 struct bt_vlan_s
**pvlan
, *vlan
;
577 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
581 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
583 pvlan
= &first_bt_vlan
;
584 while (*pvlan
!= NULL
)
585 pvlan
= &(*pvlan
)->next
;
590 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
594 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
599 static struct HCIInfo null_hci
= {
600 .cmd_send
= null_hci_send
,
601 .sco_send
= null_hci_send
,
602 .acl_send
= null_hci_send
,
603 .bdaddr_set
= null_hci_addr_set
,
606 struct HCIInfo
*qemu_next_hci(void)
608 if (cur_hci
== nb_hcis
)
611 return hci_table
[cur_hci
++];
614 static struct HCIInfo
*hci_init(const char *str
)
617 struct bt_scatternet_s
*vlan
= 0;
619 if (!strcmp(str
, "null"))
622 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
624 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
625 else if (!strncmp(str
, "hci", 3)) {
628 if (!strncmp(str
+ 3, ",vlan=", 6)) {
629 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
634 vlan
= qemu_find_bt_vlan(0);
636 return bt_new_hci(vlan
);
639 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
644 static int bt_hci_parse(const char *str
)
649 if (nb_hcis
>= MAX_NICS
) {
650 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
663 bdaddr
.b
[5] = 0x56 + nb_hcis
;
664 hci
->bdaddr_set(hci
, bdaddr
.b
);
666 hci_table
[nb_hcis
++] = hci
;
671 static void bt_vhci_add(int vlan_id
)
673 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
676 fprintf(stderr
, "qemu: warning: adding a VHCI to "
677 "an empty scatternet %i\n", vlan_id
);
679 bt_vhci_init(bt_new_hci(vlan
));
682 static struct bt_device_s
*bt_device_add(const char *opt
)
684 struct bt_scatternet_s
*vlan
;
686 char *endp
= strstr(opt
, ",vlan=");
687 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
690 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
693 vlan_id
= strtol(endp
+ 6, &endp
, 0);
695 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
700 vlan
= qemu_find_bt_vlan(vlan_id
);
703 fprintf(stderr
, "qemu: warning: adding a slave device to "
704 "an empty scatternet %i\n", vlan_id
);
706 if (!strcmp(devname
, "keyboard"))
707 return bt_keyboard_init(vlan
);
709 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
713 static int bt_parse(const char *opt
)
715 const char *endp
, *p
;
718 if (strstart(opt
, "hci", &endp
)) {
719 if (!*endp
|| *endp
== ',') {
721 if (!strstart(endp
, ",vlan=", 0))
724 return bt_hci_parse(opt
);
726 } else if (strstart(opt
, "vhci", &endp
)) {
727 if (!*endp
|| *endp
== ',') {
729 if (strstart(endp
, ",vlan=", &p
)) {
730 vlan
= strtol(p
, (char **) &endp
, 0);
732 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
736 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
745 } else if (strstart(opt
, "device:", &endp
))
746 return !bt_device_add(endp
);
748 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
752 /***********************************************************/
753 /* QEMU Block devices */
755 #define HD_OPTS "media=disk"
756 #define CDROM_OPTS "media=cdrom"
758 #define PFLASH_OPTS ""
762 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
764 int *use_scsi
= opaque
;
766 return drive_init(opts
, *use_scsi
) == NULL
;
769 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
771 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
772 qemu_opt_set(opts
, "snapshot", "on");
777 static void default_drive(int enable
, int snapshot
, int use_scsi
,
778 BlockInterfaceType type
, int index
,
783 if (type
== IF_DEFAULT
) {
784 type
= use_scsi
? IF_SCSI
: IF_IDE
;
787 if (!enable
|| drive_get_by_index(type
, index
)) {
791 opts
= drive_add(type
, index
, NULL
, optstr
);
793 drive_enable_snapshot(opts
, NULL
);
795 if (!drive_init(opts
, use_scsi
)) {
800 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
802 boot_set_handler
= func
;
803 boot_set_opaque
= opaque
;
806 int qemu_boot_set(const char *boot_devices
)
808 if (!boot_set_handler
) {
811 return boot_set_handler(boot_set_opaque
, boot_devices
);
814 static void validate_bootdevices(char *devices
)
816 /* We just do some generic consistency checks */
820 for (p
= devices
; *p
!= '\0'; p
++) {
821 /* Allowed boot devices are:
822 * a-b: floppy disk drives
823 * c-f: IDE disk drives
824 * g-m: machine implementation dependent drives
825 * n-p: network devices
826 * It's up to each machine implementation to check if the given boot
827 * devices match the actual hardware implementation and firmware
830 if (*p
< 'a' || *p
> 'p') {
831 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
834 if (bitmap
& (1 << (*p
- 'a'))) {
835 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
838 bitmap
|= 1 << (*p
- 'a');
842 static void restore_boot_devices(void *opaque
)
844 char *standard_boot_devices
= opaque
;
845 static int first
= 1;
847 /* Restore boot order and remove ourselves after the first boot */
853 qemu_boot_set(standard_boot_devices
);
855 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
856 g_free(standard_boot_devices
);
859 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
862 FWBootEntry
*node
, *i
;
868 assert(dev
!= NULL
|| suffix
!= NULL
);
870 node
= g_malloc0(sizeof(FWBootEntry
));
871 node
->bootindex
= bootindex
;
872 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
875 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
876 if (i
->bootindex
== bootindex
) {
877 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
879 } else if (i
->bootindex
< bootindex
) {
882 QTAILQ_INSERT_BEFORE(i
, node
, link
);
885 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
889 * This function returns null terminated string that consist of new line
890 * separated device paths.
892 * memory pointed by "size" is assigned total length of the array in bytes
895 char *get_boot_devices_list(uint32_t *size
)
901 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
902 char *devpath
= NULL
, *bootpath
;
906 devpath
= qdev_get_fw_dev_path(i
->dev
);
910 if (i
->suffix
&& devpath
) {
911 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
913 bootpath
= g_malloc(bootpathlen
);
914 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
916 } else if (devpath
) {
920 bootpath
= g_strdup(i
->suffix
);
924 list
[total
-1] = '\n';
926 len
= strlen(bootpath
) + 1;
927 list
= g_realloc(list
, total
+ len
);
928 memcpy(&list
[total
], bootpath
, len
);
938 static void numa_add(const char *optarg
)
942 unsigned long long value
, endvalue
;
945 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
946 if (!strcmp(option
, "node")) {
947 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
948 nodenr
= nb_numa_nodes
;
950 nodenr
= strtoull(option
, NULL
, 10);
953 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
954 node_mem
[nodenr
] = 0;
957 sval
= strtosz(option
, &endptr
);
958 if (sval
< 0 || *endptr
) {
959 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
962 node_mem
[nodenr
] = sval
;
964 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
965 node_cpumask
[nodenr
] = 0;
967 value
= strtoull(option
, &endptr
, 10);
970 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
972 if (*endptr
== '-') {
973 endvalue
= strtoull(endptr
+1, &endptr
, 10);
974 if (endvalue
>= 63) {
977 "only 63 CPUs in NUMA mode supported.\n");
979 value
= (2ULL << endvalue
) - (1ULL << value
);
981 value
= 1ULL << value
;
984 node_cpumask
[nodenr
] = value
;
991 static void smp_parse(const char *optarg
)
993 int smp
, sockets
= 0, threads
= 0, cores
= 0;
997 smp
= strtoul(optarg
, &endptr
, 10);
998 if (endptr
!= optarg
) {
999 if (*endptr
== ',') {
1003 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1004 sockets
= strtoull(option
, NULL
, 10);
1005 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1006 cores
= strtoull(option
, NULL
, 10);
1007 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1008 threads
= strtoull(option
, NULL
, 10);
1009 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1010 max_cpus
= strtoull(option
, NULL
, 10);
1012 /* compute missing values, prefer sockets over cores over threads */
1013 if (smp
== 0 || sockets
== 0) {
1014 sockets
= sockets
> 0 ? sockets
: 1;
1015 cores
= cores
> 0 ? cores
: 1;
1016 threads
= threads
> 0 ? threads
: 1;
1018 smp
= cores
* threads
* sockets
;
1022 threads
= threads
> 0 ? threads
: 1;
1023 cores
= smp
/ (sockets
* threads
);
1025 threads
= smp
/ (cores
* sockets
);
1029 smp_cores
= cores
> 0 ? cores
: 1;
1030 smp_threads
= threads
> 0 ? threads
: 1;
1032 max_cpus
= smp_cpus
;
1035 /***********************************************************/
1038 static int usb_device_add(const char *devname
)
1041 USBDevice
*dev
= NULL
;
1046 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1047 dev
= usbdevice_create(devname
);
1051 /* the other ones */
1052 #ifndef CONFIG_LINUX
1053 /* only the linux version is qdev-ified, usb-bsd still needs this */
1054 if (strstart(devname
, "host:", &p
)) {
1055 dev
= usb_host_device_open(p
);
1058 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1059 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1060 bt_new_hci(qemu_find_bt_vlan(0)));
1071 static int usb_device_del(const char *devname
)
1076 if (strstart(devname
, "host:", &p
))
1077 return usb_host_device_close(p
);
1082 p
= strchr(devname
, '.');
1085 bus_num
= strtoul(devname
, NULL
, 0);
1086 addr
= strtoul(p
+ 1, NULL
, 0);
1088 return usb_device_delete_addr(bus_num
, addr
);
1091 static int usb_parse(const char *cmdline
)
1094 r
= usb_device_add(cmdline
);
1096 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1101 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1103 const char *devname
= qdict_get_str(qdict
, "devname");
1104 if (usb_device_add(devname
) < 0) {
1105 error_report("could not add USB device '%s'", devname
);
1109 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1111 const char *devname
= qdict_get_str(qdict
, "devname");
1112 if (usb_device_del(devname
) < 0) {
1113 error_report("could not delete USB device '%s'", devname
);
1117 /***********************************************************/
1118 /* PCMCIA/Cardbus */
1120 static struct pcmcia_socket_entry_s
{
1121 PCMCIASocket
*socket
;
1122 struct pcmcia_socket_entry_s
*next
;
1123 } *pcmcia_sockets
= 0;
1125 void pcmcia_socket_register(PCMCIASocket
*socket
)
1127 struct pcmcia_socket_entry_s
*entry
;
1129 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1130 entry
->socket
= socket
;
1131 entry
->next
= pcmcia_sockets
;
1132 pcmcia_sockets
= entry
;
1135 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1137 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1139 ptr
= &pcmcia_sockets
;
1140 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1141 if (entry
->socket
== socket
) {
1147 void pcmcia_info(Monitor
*mon
)
1149 struct pcmcia_socket_entry_s
*iter
;
1151 if (!pcmcia_sockets
)
1152 monitor_printf(mon
, "No PCMCIA sockets\n");
1154 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1155 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1156 iter
->socket
->attached
? iter
->socket
->card_string
:
1160 /***********************************************************/
1161 /* machine registration */
1163 static QEMUMachine
*first_machine
= NULL
;
1164 QEMUMachine
*current_machine
= NULL
;
1166 int qemu_register_machine(QEMUMachine
*m
)
1169 pm
= &first_machine
;
1177 static QEMUMachine
*find_machine(const char *name
)
1181 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1182 if (!strcmp(m
->name
, name
))
1184 if (m
->alias
&& !strcmp(m
->alias
, name
))
1190 QEMUMachine
*find_default_machine(void)
1194 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1195 if (m
->is_default
) {
1202 /***********************************************************/
1203 /* main execution loop */
1205 static void gui_update(void *opaque
)
1207 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1208 DisplayState
*ds
= opaque
;
1209 DisplayChangeListener
*dcl
= ds
->listeners
;
1213 while (dcl
!= NULL
) {
1214 if (dcl
->gui_timer_interval
&&
1215 dcl
->gui_timer_interval
< interval
)
1216 interval
= dcl
->gui_timer_interval
;
1219 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1222 struct vm_change_state_entry
{
1223 VMChangeStateHandler
*cb
;
1225 QLIST_ENTRY (vm_change_state_entry
) entries
;
1228 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1230 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1233 VMChangeStateEntry
*e
;
1235 e
= g_malloc0(sizeof (*e
));
1239 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1243 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1245 QLIST_REMOVE (e
, entries
);
1249 void vm_state_notify(int running
, RunState state
)
1251 VMChangeStateEntry
*e
;
1253 trace_vm_state_notify(running
, state
);
1255 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1256 e
->cb(e
->opaque
, running
, state
);
1262 if (!runstate_is_running()) {
1264 runstate_set(RUN_STATE_RUNNING
);
1265 vm_state_notify(1, RUN_STATE_RUNNING
);
1267 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1271 /* reset/shutdown handler */
1273 typedef struct QEMUResetEntry
{
1274 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1275 QEMUResetHandler
*func
;
1279 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1280 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1281 static int reset_requested
;
1282 static int shutdown_requested
, shutdown_signal
= -1;
1283 static pid_t shutdown_pid
;
1284 static int powerdown_requested
;
1285 static int debug_requested
;
1286 static RunState vmstop_requested
= RUN_STATE_MAX
;
1288 int qemu_shutdown_requested_get(void)
1290 return shutdown_requested
;
1293 int qemu_reset_requested_get(void)
1295 return reset_requested
;
1298 int qemu_shutdown_requested(void)
1300 int r
= shutdown_requested
;
1301 shutdown_requested
= 0;
1305 void qemu_kill_report(void)
1307 if (shutdown_signal
!= -1) {
1308 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1309 if (shutdown_pid
== 0) {
1310 /* This happens for eg ^C at the terminal, so it's worth
1311 * avoiding printing an odd message in that case.
1313 fputc('\n', stderr
);
1315 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1317 shutdown_signal
= -1;
1321 int qemu_reset_requested(void)
1323 int r
= reset_requested
;
1324 reset_requested
= 0;
1328 int qemu_powerdown_requested(void)
1330 int r
= powerdown_requested
;
1331 powerdown_requested
= 0;
1335 static int qemu_debug_requested(void)
1337 int r
= debug_requested
;
1338 debug_requested
= 0;
1342 /* We use RUN_STATE_MAX but any invalid value will do */
1343 static bool qemu_vmstop_requested(RunState
*r
)
1345 if (vmstop_requested
< RUN_STATE_MAX
) {
1346 *r
= vmstop_requested
;
1347 vmstop_requested
= RUN_STATE_MAX
;
1354 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1356 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1359 re
->opaque
= opaque
;
1360 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1363 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1367 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1368 if (re
->func
== func
&& re
->opaque
== opaque
) {
1369 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1376 void qemu_system_reset(bool report
)
1378 QEMUResetEntry
*re
, *nre
;
1380 /* reset all devices */
1381 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1382 re
->func(re
->opaque
);
1385 monitor_protocol_event(QEVENT_RESET
, NULL
);
1387 cpu_synchronize_all_post_reset();
1390 void qemu_system_reset_request(void)
1393 shutdown_requested
= 1;
1395 reset_requested
= 1;
1398 qemu_notify_event();
1401 void qemu_system_killed(int signal
, pid_t pid
)
1403 shutdown_signal
= signal
;
1406 qemu_system_shutdown_request();
1409 void qemu_system_shutdown_request(void)
1411 shutdown_requested
= 1;
1412 qemu_notify_event();
1415 void qemu_system_powerdown_request(void)
1417 powerdown_requested
= 1;
1418 qemu_notify_event();
1421 void qemu_system_debug_request(void)
1423 debug_requested
= 1;
1424 qemu_notify_event();
1427 void qemu_system_vmstop_request(RunState state
)
1429 vmstop_requested
= state
;
1430 qemu_notify_event();
1433 qemu_irq qemu_system_powerdown
;
1435 static bool main_loop_should_exit(void)
1438 if (qemu_debug_requested()) {
1439 vm_stop(RUN_STATE_DEBUG
);
1441 if (qemu_shutdown_requested()) {
1443 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1445 vm_stop(RUN_STATE_SHUTDOWN
);
1450 if (qemu_reset_requested()) {
1452 cpu_synchronize_all_states();
1453 qemu_system_reset(VMRESET_REPORT
);
1455 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1456 runstate_check(RUN_STATE_SHUTDOWN
)) {
1457 runstate_set(RUN_STATE_PAUSED
);
1460 if (qemu_powerdown_requested()) {
1461 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1462 qemu_irq_raise(qemu_system_powerdown
);
1464 if (qemu_vmstop_requested(&r
)) {
1470 static void main_loop(void)
1474 #ifdef CONFIG_PROFILER
1478 nonblocking
= !kvm_enabled() && last_io
> 0;
1479 #ifdef CONFIG_PROFILER
1480 ti
= profile_getclock();
1482 last_io
= main_loop_wait(nonblocking
);
1483 #ifdef CONFIG_PROFILER
1484 dev_time
+= profile_getclock() - ti
;
1486 } while (!main_loop_should_exit());
1489 static void version(void)
1491 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1494 static void help(int exitcode
)
1497 printf("usage: %s [options] [disk_image]\n\n"
1498 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1499 error_get_progname());
1501 #define QEMU_OPTIONS_GENERATE_HELP
1502 #include "qemu-options-wrapper.h"
1504 printf("\nDuring emulation, the following keys are useful:\n"
1505 "ctrl-alt-f toggle full screen\n"
1506 "ctrl-alt-n switch to virtual console 'n'\n"
1507 "ctrl-alt toggle mouse and keyboard grab\n"
1509 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1514 #define HAS_ARG 0x0001
1516 typedef struct QEMUOption
{
1523 static const QEMUOption qemu_options
[] = {
1524 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1525 #define QEMU_OPTIONS_GENERATE_OPTIONS
1526 #include "qemu-options-wrapper.h"
1530 static bool vga_available(void)
1532 return qdev_exists("VGA") || qdev_exists("isa-vga");
1535 static bool cirrus_vga_available(void)
1537 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1540 static bool vmware_vga_available(void)
1542 return qdev_exists("vmware-svga");
1545 static void select_vgahw (const char *p
)
1549 vga_interface_type
= VGA_NONE
;
1550 if (strstart(p
, "std", &opts
)) {
1551 if (vga_available()) {
1552 vga_interface_type
= VGA_STD
;
1554 fprintf(stderr
, "Error: standard VGA not available\n");
1557 } else if (strstart(p
, "cirrus", &opts
)) {
1558 if (cirrus_vga_available()) {
1559 vga_interface_type
= VGA_CIRRUS
;
1561 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1564 } else if (strstart(p
, "vmware", &opts
)) {
1565 if (vmware_vga_available()) {
1566 vga_interface_type
= VGA_VMWARE
;
1568 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1571 } else if (strstart(p
, "xenfb", &opts
)) {
1572 vga_interface_type
= VGA_XENFB
;
1573 } else if (strstart(p
, "qxl", &opts
)) {
1574 vga_interface_type
= VGA_QXL
;
1575 } else if (!strstart(p
, "none", &opts
)) {
1577 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1581 const char *nextopt
;
1583 if (strstart(opts
, ",retrace=", &nextopt
)) {
1585 if (strstart(opts
, "dumb", &nextopt
))
1586 vga_retrace_method
= VGA_RETRACE_DUMB
;
1587 else if (strstart(opts
, "precise", &nextopt
))
1588 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1589 else goto invalid_vga
;
1590 } else goto invalid_vga
;
1595 static DisplayType
select_display(const char *p
)
1598 DisplayType display
= DT_DEFAULT
;
1600 if (strstart(p
, "sdl", &opts
)) {
1604 const char *nextopt
;
1606 if (strstart(opts
, ",frame=", &nextopt
)) {
1608 if (strstart(opts
, "on", &nextopt
)) {
1610 } else if (strstart(opts
, "off", &nextopt
)) {
1613 goto invalid_sdl_args
;
1615 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1617 if (strstart(opts
, "on", &nextopt
)) {
1619 } else if (strstart(opts
, "off", &nextopt
)) {
1622 goto invalid_sdl_args
;
1624 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1626 if (strstart(opts
, "on", &nextopt
)) {
1628 } else if (strstart(opts
, "off", &nextopt
)) {
1631 goto invalid_sdl_args
;
1633 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1635 if (strstart(opts
, "on", &nextopt
)) {
1637 } else if (strstart(opts
, "off", &nextopt
)) {
1640 goto invalid_sdl_args
;
1644 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1650 fprintf(stderr
, "SDL support is disabled\n");
1653 } else if (strstart(p
, "vnc", &opts
)) {
1658 const char *nextopt
;
1660 if (strstart(opts
, "=", &nextopt
)) {
1661 vnc_display
= nextopt
;
1665 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1669 fprintf(stderr
, "VNC support is disabled\n");
1672 } else if (strstart(p
, "curses", &opts
)) {
1673 #ifdef CONFIG_CURSES
1674 display
= DT_CURSES
;
1676 fprintf(stderr
, "Curses support is disabled\n");
1679 } else if (strstart(p
, "none", &opts
)) {
1682 fprintf(stderr
, "Unknown display type: %s\n", p
);
1689 static int balloon_parse(const char *arg
)
1693 if (strcmp(arg
, "none") == 0) {
1697 if (!strncmp(arg
, "virtio", 6)) {
1698 if (arg
[6] == ',') {
1699 /* have params -> parse them */
1700 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1704 /* create empty opts */
1705 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1707 qemu_opt_set(opts
, "driver", "virtio-balloon");
1714 char *qemu_find_file(int type
, const char *name
)
1720 /* If name contains path separators then try it as a straight path. */
1721 if ((strchr(name
, '/') || strchr(name
, '\\'))
1722 && access(name
, R_OK
) == 0) {
1723 return g_strdup(name
);
1726 case QEMU_FILE_TYPE_BIOS
:
1729 case QEMU_FILE_TYPE_KEYMAP
:
1730 subdir
= "keymaps/";
1735 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1736 buf
= g_malloc0(len
);
1737 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1738 if (access(buf
, R_OK
)) {
1745 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1747 return qdev_device_help(opts
);
1750 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1754 dev
= qdev_device_add(opts
);
1760 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1762 CharDriverState
*chr
;
1764 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1770 #ifdef CONFIG_VIRTFS
1771 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1774 ret
= qemu_fsdev_add(opts
);
1780 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1782 CharDriverState
*chr
;
1783 const char *chardev
;
1787 mode
= qemu_opt_get(opts
, "mode");
1791 if (strcmp(mode
, "readline") == 0) {
1792 flags
= MONITOR_USE_READLINE
;
1793 } else if (strcmp(mode
, "control") == 0) {
1794 flags
= MONITOR_USE_CONTROL
;
1796 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1800 if (qemu_opt_get_bool(opts
, "pretty", 0))
1801 flags
|= MONITOR_USE_PRETTY
;
1803 if (qemu_opt_get_bool(opts
, "default", 0))
1804 flags
|= MONITOR_IS_DEFAULT
;
1806 chardev
= qemu_opt_get(opts
, "chardev");
1807 chr
= qemu_chr_find(chardev
);
1809 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1813 monitor_init(chr
, flags
);
1817 static void monitor_parse(const char *optarg
, const char *mode
)
1819 static int monitor_device_index
= 0;
1825 if (strstart(optarg
, "chardev:", &p
)) {
1826 snprintf(label
, sizeof(label
), "%s", p
);
1828 snprintf(label
, sizeof(label
), "compat_monitor%d",
1829 monitor_device_index
);
1830 if (monitor_device_index
== 0) {
1833 opts
= qemu_chr_parse_compat(label
, optarg
);
1835 fprintf(stderr
, "parse error: %s\n", optarg
);
1840 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1842 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1845 qemu_opt_set(opts
, "mode", mode
);
1846 qemu_opt_set(opts
, "chardev", label
);
1848 qemu_opt_set(opts
, "default", "on");
1849 monitor_device_index
++;
1852 struct device_config
{
1854 DEV_USB
, /* -usbdevice */
1856 DEV_SERIAL
, /* -serial */
1857 DEV_PARALLEL
, /* -parallel */
1858 DEV_VIRTCON
, /* -virtioconsole */
1859 DEV_DEBUGCON
, /* -debugcon */
1860 DEV_GDB
, /* -gdb, -s */
1862 const char *cmdline
;
1864 QTAILQ_ENTRY(device_config
) next
;
1866 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1868 static void add_device_config(int type
, const char *cmdline
)
1870 struct device_config
*conf
;
1872 conf
= g_malloc0(sizeof(*conf
));
1874 conf
->cmdline
= cmdline
;
1875 loc_save(&conf
->loc
);
1876 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1879 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1881 struct device_config
*conf
;
1884 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1885 if (conf
->type
!= type
)
1887 loc_push_restore(&conf
->loc
);
1888 rc
= func(conf
->cmdline
);
1889 loc_pop(&conf
->loc
);
1896 static int serial_parse(const char *devname
)
1898 static int index
= 0;
1901 if (strcmp(devname
, "none") == 0)
1903 if (index
== MAX_SERIAL_PORTS
) {
1904 fprintf(stderr
, "qemu: too many serial ports\n");
1907 snprintf(label
, sizeof(label
), "serial%d", index
);
1908 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1909 if (!serial_hds
[index
]) {
1910 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1911 devname
, strerror(errno
));
1918 static int parallel_parse(const char *devname
)
1920 static int index
= 0;
1923 if (strcmp(devname
, "none") == 0)
1925 if (index
== MAX_PARALLEL_PORTS
) {
1926 fprintf(stderr
, "qemu: too many parallel ports\n");
1929 snprintf(label
, sizeof(label
), "parallel%d", index
);
1930 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1931 if (!parallel_hds
[index
]) {
1932 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
1933 devname
, strerror(errno
));
1940 static int virtcon_parse(const char *devname
)
1942 QemuOptsList
*device
= qemu_find_opts("device");
1943 static int index
= 0;
1945 QemuOpts
*bus_opts
, *dev_opts
;
1947 if (strcmp(devname
, "none") == 0)
1949 if (index
== MAX_VIRTIO_CONSOLES
) {
1950 fprintf(stderr
, "qemu: too many virtio consoles\n");
1954 bus_opts
= qemu_opts_create(device
, NULL
, 0);
1955 if (arch_type
== QEMU_ARCH_S390X
) {
1956 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
1958 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
1961 dev_opts
= qemu_opts_create(device
, NULL
, 0);
1962 qemu_opt_set(dev_opts
, "driver", "virtconsole");
1964 snprintf(label
, sizeof(label
), "virtcon%d", index
);
1965 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1966 if (!virtcon_hds
[index
]) {
1967 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
1968 devname
, strerror(errno
));
1971 qemu_opt_set(dev_opts
, "chardev", label
);
1977 static int debugcon_parse(const char *devname
)
1981 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
1984 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
1986 fprintf(stderr
, "qemu: already have a debugcon device\n");
1989 qemu_opt_set(opts
, "driver", "isa-debugcon");
1990 qemu_opt_set(opts
, "chardev", "debugcon");
1994 static QEMUMachine
*machine_parse(const char *name
)
1996 QEMUMachine
*m
, *machine
= NULL
;
1999 machine
= find_machine(name
);
2004 printf("Supported machines are:\n");
2005 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2007 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2009 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2010 m
->is_default
? " (default)" : "");
2012 exit(!name
|| *name
!= '?');
2015 static int tcg_init(void)
2017 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2022 const char *opt_name
;
2024 int (*available
)(void);
2028 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2029 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2030 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2033 static int configure_accelerator(void)
2035 const char *p
= NULL
;
2038 bool accel_initialised
= 0;
2039 bool init_failed
= 0;
2041 QemuOptsList
*list
= qemu_find_opts("machine");
2042 if (!QTAILQ_EMPTY(&list
->head
)) {
2043 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2047 /* Use the default "accelerator", tcg */
2051 while (!accel_initialised
&& *p
!= '\0') {
2055 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2056 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2057 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2058 *(accel_list
[i
].allowed
) = 1;
2059 ret
= accel_list
[i
].init();
2062 if (!accel_list
[i
].available()) {
2063 printf("%s not supported for this target\n",
2064 accel_list
[i
].name
);
2066 fprintf(stderr
, "failed to initialize %s: %s\n",
2070 *(accel_list
[i
].allowed
) = 0;
2072 accel_initialised
= 1;
2077 if (i
== ARRAY_SIZE(accel_list
)) {
2078 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2082 if (!accel_initialised
) {
2083 fprintf(stderr
, "No accelerator found!\n");
2088 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2091 return !accel_initialised
;
2094 void qemu_add_exit_notifier(Notifier
*notify
)
2096 notifier_list_add(&exit_notifiers
, notify
);
2099 void qemu_remove_exit_notifier(Notifier
*notify
)
2101 notifier_remove(notify
);
2104 static void qemu_run_exit_notifiers(void)
2106 notifier_list_notify(&exit_notifiers
, NULL
);
2109 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2111 notifier_list_add(&machine_init_done_notifiers
, notify
);
2114 static void qemu_run_machine_init_done_notifiers(void)
2116 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2119 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2120 const char **poptarg
, int *poptind
)
2122 const QEMUOption
*popt
;
2123 int optind
= *poptind
;
2124 char *r
= argv
[optind
];
2127 loc_set_cmdline(argv
, optind
, 1);
2129 /* Treat --foo the same as -foo. */
2132 popt
= qemu_options
;
2135 error_report("invalid option");
2138 if (!strcmp(popt
->name
, r
+ 1))
2142 if (popt
->flags
& HAS_ARG
) {
2143 if (optind
>= argc
) {
2144 error_report("requires an argument");
2147 optarg
= argv
[optind
++];
2148 loc_set_cmdline(argv
, optind
- 2, 2);
2159 static gpointer
malloc_and_trace(gsize n_bytes
)
2161 void *ptr
= malloc(n_bytes
);
2162 trace_g_malloc(n_bytes
, ptr
);
2166 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2168 void *ptr
= realloc(mem
, n_bytes
);
2169 trace_g_realloc(mem
, n_bytes
, ptr
);
2173 static void free_and_trace(gpointer mem
)
2179 int qemu_init_main_loop(void)
2181 return main_loop_init();
2184 int main(int argc
, char **argv
, char **envp
)
2187 int snapshot
, linux_boot
;
2188 const char *icount_option
= NULL
;
2189 const char *initrd_filename
;
2190 const char *kernel_filename
, *kernel_cmdline
;
2191 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2193 DisplayChangeListener
*dcl
;
2194 int cyls
, heads
, secs
, translation
;
2195 QemuOpts
*hda_opts
= NULL
, *opts
;
2196 QemuOptsList
*olist
;
2199 const char *loadvm
= NULL
;
2200 QEMUMachine
*machine
;
2201 const char *cpu_model
;
2202 const char *vga_model
= NULL
;
2203 const char *pid_file
= NULL
;
2204 const char *incoming
= NULL
;
2206 int show_vnc_port
= 0;
2209 const char *log_mask
= NULL
;
2210 const char *log_file
= NULL
;
2211 GMemVTable mem_trace
= {
2212 .malloc
= malloc_and_trace
,
2213 .realloc
= realloc_and_trace
,
2214 .free
= free_and_trace
,
2216 const char *trace_events
= NULL
;
2217 const char *trace_file
= NULL
;
2219 atexit(qemu_run_exit_notifiers
);
2220 error_set_progname(argv
[0]);
2222 g_mem_set_vtable(&mem_trace
);
2223 if (!g_thread_supported()) {
2224 #if !GLIB_CHECK_VERSION(2, 31, 0)
2225 g_thread_init(NULL
);
2227 fprintf(stderr
, "glib threading failed to initialize.\n");
2235 rtc_clock
= host_clock
;
2237 qemu_cache_utils_init(envp
);
2239 QLIST_INIT (&vm_change_state_head
);
2240 os_setup_early_signal_handling();
2242 module_call_init(MODULE_INIT_MACHINE
);
2243 machine
= find_default_machine();
2247 cyls
= heads
= secs
= 0;
2248 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2250 for (i
= 0; i
< MAX_NODES
; i
++) {
2252 node_cpumask
[i
] = 0;
2260 /* first pass of option parsing */
2262 while (optind
< argc
) {
2263 if (argv
[optind
][0] != '-') {
2268 const QEMUOption
*popt
;
2270 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2271 switch (popt
->index
) {
2272 case QEMU_OPTION_nodefconfig
:
2282 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2283 if (ret
< 0 && ret
!= -ENOENT
) {
2287 ret
= qemu_read_config_file(arch_config_name
);
2288 if (ret
< 0 && ret
!= -ENOENT
) {
2294 /* second pass of option parsing */
2299 if (argv
[optind
][0] != '-') {
2300 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2302 const QEMUOption
*popt
;
2304 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2305 if (!(popt
->arch_mask
& arch_type
)) {
2306 printf("Option %s not supported for this target\n", popt
->name
);
2309 switch(popt
->index
) {
2311 machine
= machine_parse(optarg
);
2313 case QEMU_OPTION_cpu
:
2314 /* hw initialization will check this */
2315 if (*optarg
== '?') {
2316 list_cpus(stdout
, &fprintf
, optarg
);
2322 case QEMU_OPTION_hda
:
2326 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2328 snprintf(buf
, sizeof(buf
),
2329 "%s,cyls=%d,heads=%d,secs=%d%s",
2330 HD_OPTS
, cyls
, heads
, secs
,
2331 translation
== BIOS_ATA_TRANSLATION_LBA
?
2333 translation
== BIOS_ATA_TRANSLATION_NONE
?
2334 ",trans=none" : "");
2335 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2338 case QEMU_OPTION_hdb
:
2339 case QEMU_OPTION_hdc
:
2340 case QEMU_OPTION_hdd
:
2341 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2344 case QEMU_OPTION_drive
:
2345 if (drive_def(optarg
) == NULL
) {
2349 case QEMU_OPTION_set
:
2350 if (qemu_set_option(optarg
) != 0)
2353 case QEMU_OPTION_global
:
2354 if (qemu_global_option(optarg
) != 0)
2357 case QEMU_OPTION_mtdblock
:
2358 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2360 case QEMU_OPTION_sd
:
2361 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2363 case QEMU_OPTION_pflash
:
2364 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2366 case QEMU_OPTION_snapshot
:
2369 case QEMU_OPTION_hdachs
:
2373 cyls
= strtol(p
, (char **)&p
, 0);
2374 if (cyls
< 1 || cyls
> 16383)
2379 heads
= strtol(p
, (char **)&p
, 0);
2380 if (heads
< 1 || heads
> 16)
2385 secs
= strtol(p
, (char **)&p
, 0);
2386 if (secs
< 1 || secs
> 63)
2390 if (!strcmp(p
, "none"))
2391 translation
= BIOS_ATA_TRANSLATION_NONE
;
2392 else if (!strcmp(p
, "lba"))
2393 translation
= BIOS_ATA_TRANSLATION_LBA
;
2394 else if (!strcmp(p
, "auto"))
2395 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2398 } else if (*p
!= '\0') {
2400 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2403 if (hda_opts
!= NULL
) {
2405 snprintf(num
, sizeof(num
), "%d", cyls
);
2406 qemu_opt_set(hda_opts
, "cyls", num
);
2407 snprintf(num
, sizeof(num
), "%d", heads
);
2408 qemu_opt_set(hda_opts
, "heads", num
);
2409 snprintf(num
, sizeof(num
), "%d", secs
);
2410 qemu_opt_set(hda_opts
, "secs", num
);
2411 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2412 qemu_opt_set(hda_opts
, "trans", "lba");
2413 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2414 qemu_opt_set(hda_opts
, "trans", "none");
2418 case QEMU_OPTION_numa
:
2419 if (nb_numa_nodes
>= MAX_NODES
) {
2420 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2425 case QEMU_OPTION_display
:
2426 display_type
= select_display(optarg
);
2428 case QEMU_OPTION_nographic
:
2429 display_type
= DT_NOGRAPHIC
;
2431 case QEMU_OPTION_curses
:
2432 #ifdef CONFIG_CURSES
2433 display_type
= DT_CURSES
;
2435 fprintf(stderr
, "Curses support is disabled\n");
2439 case QEMU_OPTION_portrait
:
2440 graphic_rotate
= 90;
2442 case QEMU_OPTION_rotate
:
2443 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2444 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2445 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2447 "qemu: only 90, 180, 270 deg rotation is available\n");
2451 case QEMU_OPTION_kernel
:
2452 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2454 case QEMU_OPTION_initrd
:
2455 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2457 case QEMU_OPTION_append
:
2458 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2460 case QEMU_OPTION_cdrom
:
2461 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2463 case QEMU_OPTION_boot
:
2465 static const char * const params
[] = {
2466 "order", "once", "menu",
2467 "splash", "splash-time", NULL
2469 char buf
[sizeof(boot_devices
)];
2470 char *standard_boot_devices
;
2473 if (!strchr(optarg
, '=')) {
2475 pstrcpy(buf
, sizeof(buf
), optarg
);
2476 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2478 "qemu: unknown boot parameter '%s' in '%s'\n",
2484 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2485 validate_bootdevices(buf
);
2486 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2489 if (get_param_value(buf
, sizeof(buf
),
2491 validate_bootdevices(buf
);
2492 standard_boot_devices
= g_strdup(boot_devices
);
2493 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2494 qemu_register_reset(restore_boot_devices
,
2495 standard_boot_devices
);
2497 if (get_param_value(buf
, sizeof(buf
),
2499 if (!strcmp(buf
, "on")) {
2501 } else if (!strcmp(buf
, "off")) {
2505 "qemu: invalid option value '%s'\n",
2510 qemu_opts_parse(qemu_find_opts("boot-opts"),
2515 case QEMU_OPTION_fda
:
2516 case QEMU_OPTION_fdb
:
2517 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2520 case QEMU_OPTION_no_fd_bootchk
:
2523 case QEMU_OPTION_netdev
:
2524 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2528 case QEMU_OPTION_net
:
2529 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2533 #ifdef CONFIG_LIBISCSI
2534 case QEMU_OPTION_iscsi
:
2535 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2542 case QEMU_OPTION_tftp
:
2543 legacy_tftp_prefix
= optarg
;
2545 case QEMU_OPTION_bootp
:
2546 legacy_bootp_filename
= optarg
;
2548 case QEMU_OPTION_redir
:
2549 if (net_slirp_redir(optarg
) < 0)
2553 case QEMU_OPTION_bt
:
2554 add_device_config(DEV_BT
, optarg
);
2556 case QEMU_OPTION_audio_help
:
2557 if (!(audio_available())) {
2558 printf("Option %s not supported for this target\n", popt
->name
);
2564 case QEMU_OPTION_soundhw
:
2565 if (!(audio_available())) {
2566 printf("Option %s not supported for this target\n", popt
->name
);
2569 select_soundhw (optarg
);
2574 case QEMU_OPTION_version
:
2578 case QEMU_OPTION_m
: {
2582 value
= strtosz(optarg
, &end
);
2583 if (value
< 0 || *end
) {
2584 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2588 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2589 fprintf(stderr
, "qemu: ram size too large\n");
2595 case QEMU_OPTION_mempath
:
2599 case QEMU_OPTION_mem_prealloc
:
2610 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2612 case QEMU_OPTION_gdb
:
2613 add_device_config(DEV_GDB
, optarg
);
2618 case QEMU_OPTION_bios
:
2621 case QEMU_OPTION_singlestep
:
2628 keyboard_layout
= optarg
;
2630 case QEMU_OPTION_localtime
:
2633 case QEMU_OPTION_vga
:
2641 w
= strtol(p
, (char **)&p
, 10);
2644 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2650 h
= strtol(p
, (char **)&p
, 10);
2655 depth
= strtol(p
, (char **)&p
, 10);
2656 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2657 depth
!= 24 && depth
!= 32)
2659 } else if (*p
== '\0') {
2660 depth
= graphic_depth
;
2667 graphic_depth
= depth
;
2670 case QEMU_OPTION_echr
:
2673 term_escape_char
= strtol(optarg
, &r
, 0);
2675 printf("Bad argument to echr\n");
2678 case QEMU_OPTION_monitor
:
2679 monitor_parse(optarg
, "readline");
2680 default_monitor
= 0;
2682 case QEMU_OPTION_qmp
:
2683 monitor_parse(optarg
, "control");
2684 default_monitor
= 0;
2686 case QEMU_OPTION_mon
:
2687 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2691 default_monitor
= 0;
2693 case QEMU_OPTION_chardev
:
2694 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2699 case QEMU_OPTION_fsdev
:
2700 olist
= qemu_find_opts("fsdev");
2702 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2705 opts
= qemu_opts_parse(olist
, optarg
, 1);
2707 fprintf(stderr
, "parse error: %s\n", optarg
);
2711 case QEMU_OPTION_virtfs
: {
2714 const char *writeout
, *sock_fd
, *socket
;
2716 olist
= qemu_find_opts("virtfs");
2718 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2721 opts
= qemu_opts_parse(olist
, optarg
, 1);
2723 fprintf(stderr
, "parse error: %s\n", optarg
);
2727 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2728 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2729 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2732 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2733 qemu_opt_get(opts
, "mount_tag"), 1);
2735 fprintf(stderr
, "duplicate fsdev id: %s\n",
2736 qemu_opt_get(opts
, "mount_tag"));
2740 writeout
= qemu_opt_get(opts
, "writeout");
2742 #ifdef CONFIG_SYNC_FILE_RANGE
2743 qemu_opt_set(fsdev
, "writeout", writeout
);
2745 fprintf(stderr
, "writeout=immediate not supported on "
2750 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2751 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2752 qemu_opt_set(fsdev
, "security_model",
2753 qemu_opt_get(opts
, "security_model"));
2754 socket
= qemu_opt_get(opts
, "socket");
2756 qemu_opt_set(fsdev
, "socket", socket
);
2758 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2760 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2763 qemu_opt_set_bool(fsdev
, "readonly",
2764 qemu_opt_get_bool(opts
, "readonly", 0));
2765 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2766 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2767 qemu_opt_set(device
, "fsdev",
2768 qemu_opt_get(opts
, "mount_tag"));
2769 qemu_opt_set(device
, "mount_tag",
2770 qemu_opt_get(opts
, "mount_tag"));
2773 case QEMU_OPTION_virtfs_synth
: {
2777 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2779 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2782 qemu_opt_set(fsdev
, "fsdriver", "synth");
2784 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2785 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2786 qemu_opt_set(device
, "fsdev", "v_synth");
2787 qemu_opt_set(device
, "mount_tag", "v_synth");
2790 case QEMU_OPTION_serial
:
2791 add_device_config(DEV_SERIAL
, optarg
);
2793 if (strncmp(optarg
, "mon:", 4) == 0) {
2794 default_monitor
= 0;
2797 case QEMU_OPTION_watchdog
:
2800 "qemu: only one watchdog option may be given\n");
2805 case QEMU_OPTION_watchdog_action
:
2806 if (select_watchdog_action(optarg
) == -1) {
2807 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2811 case QEMU_OPTION_virtiocon
:
2812 add_device_config(DEV_VIRTCON
, optarg
);
2813 default_virtcon
= 0;
2814 if (strncmp(optarg
, "mon:", 4) == 0) {
2815 default_monitor
= 0;
2818 case QEMU_OPTION_parallel
:
2819 add_device_config(DEV_PARALLEL
, optarg
);
2820 default_parallel
= 0;
2821 if (strncmp(optarg
, "mon:", 4) == 0) {
2822 default_monitor
= 0;
2825 case QEMU_OPTION_debugcon
:
2826 add_device_config(DEV_DEBUGCON
, optarg
);
2828 case QEMU_OPTION_loadvm
:
2831 case QEMU_OPTION_full_screen
:
2835 case QEMU_OPTION_no_frame
:
2838 case QEMU_OPTION_alt_grab
:
2841 case QEMU_OPTION_ctrl_grab
:
2844 case QEMU_OPTION_no_quit
:
2847 case QEMU_OPTION_sdl
:
2848 display_type
= DT_SDL
;
2851 case QEMU_OPTION_no_frame
:
2852 case QEMU_OPTION_alt_grab
:
2853 case QEMU_OPTION_ctrl_grab
:
2854 case QEMU_OPTION_no_quit
:
2855 case QEMU_OPTION_sdl
:
2856 fprintf(stderr
, "SDL support is disabled\n");
2859 case QEMU_OPTION_pidfile
:
2862 case QEMU_OPTION_win2k_hack
:
2863 win2k_install_hack
= 1;
2865 case QEMU_OPTION_rtc_td_hack
: {
2866 static GlobalProperty slew_lost_ticks
[] = {
2868 .driver
= "mc146818rtc",
2869 .property
= "lost_tick_policy",
2872 { /* end of list */ }
2875 qdev_prop_register_global_list(slew_lost_ticks
);
2878 case QEMU_OPTION_acpitable
:
2879 do_acpitable_option(optarg
);
2881 case QEMU_OPTION_smbios
:
2882 do_smbios_option(optarg
);
2884 case QEMU_OPTION_enable_kvm
:
2885 olist
= qemu_find_opts("machine");
2886 qemu_opts_parse(olist
, "accel=kvm", 0);
2888 case QEMU_OPTION_machine
:
2889 olist
= qemu_find_opts("machine");
2890 opts
= qemu_opts_parse(olist
, optarg
, 1);
2892 fprintf(stderr
, "parse error: %s\n", optarg
);
2895 optarg
= qemu_opt_get(opts
, "type");
2897 machine
= machine_parse(optarg
);
2900 case QEMU_OPTION_usb
:
2903 case QEMU_OPTION_usbdevice
:
2905 add_device_config(DEV_USB
, optarg
);
2907 case QEMU_OPTION_device
:
2908 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2912 case QEMU_OPTION_smp
:
2915 fprintf(stderr
, "Invalid number of CPUs\n");
2918 if (max_cpus
< smp_cpus
) {
2919 fprintf(stderr
, "maxcpus must be equal to or greater than "
2923 if (max_cpus
> 255) {
2924 fprintf(stderr
, "Unsupported number of maxcpus\n");
2928 case QEMU_OPTION_vnc
:
2931 vnc_display
= optarg
;
2933 fprintf(stderr
, "VNC support is disabled\n");
2937 case QEMU_OPTION_no_acpi
:
2940 case QEMU_OPTION_no_hpet
:
2943 case QEMU_OPTION_balloon
:
2944 if (balloon_parse(optarg
) < 0) {
2945 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2949 case QEMU_OPTION_no_reboot
:
2952 case QEMU_OPTION_no_shutdown
:
2955 case QEMU_OPTION_show_cursor
:
2958 case QEMU_OPTION_uuid
:
2959 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2960 fprintf(stderr
, "Fail to parse UUID string."
2961 " Wrong format.\n");
2965 case QEMU_OPTION_option_rom
:
2966 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2967 fprintf(stderr
, "Too many option ROMs\n");
2970 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2971 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2972 option_rom
[nb_option_roms
].bootindex
=
2973 qemu_opt_get_number(opts
, "bootindex", -1);
2974 if (!option_rom
[nb_option_roms
].name
) {
2975 fprintf(stderr
, "Option ROM file is not specified\n");
2980 case QEMU_OPTION_semihosting
:
2981 semihosting_enabled
= 1;
2983 case QEMU_OPTION_name
:
2984 qemu_name
= g_strdup(optarg
);
2986 char *p
= strchr(qemu_name
, ',');
2989 if (strncmp(p
, "process=", 8)) {
2990 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
2994 os_set_proc_name(p
);
2998 case QEMU_OPTION_prom_env
:
2999 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3000 fprintf(stderr
, "Too many prom variables\n");
3003 prom_envs
[nb_prom_envs
] = optarg
;
3006 case QEMU_OPTION_old_param
:
3009 case QEMU_OPTION_clock
:
3010 configure_alarms(optarg
);
3012 case QEMU_OPTION_startdate
:
3013 configure_rtc_date_offset(optarg
, 1);
3015 case QEMU_OPTION_rtc
:
3016 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3020 configure_rtc(opts
);
3022 case QEMU_OPTION_tb_size
:
3023 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3024 if (tcg_tb_size
< 0) {
3028 case QEMU_OPTION_icount
:
3029 icount_option
= optarg
;
3031 case QEMU_OPTION_incoming
:
3034 case QEMU_OPTION_nodefaults
:
3036 default_parallel
= 0;
3037 default_virtcon
= 0;
3038 default_monitor
= 0;
3045 case QEMU_OPTION_xen_domid
:
3046 if (!(xen_available())) {
3047 printf("Option %s not supported for this target\n", popt
->name
);
3050 xen_domid
= atoi(optarg
);
3052 case QEMU_OPTION_xen_create
:
3053 if (!(xen_available())) {
3054 printf("Option %s not supported for this target\n", popt
->name
);
3057 xen_mode
= XEN_CREATE
;
3059 case QEMU_OPTION_xen_attach
:
3060 if (!(xen_available())) {
3061 printf("Option %s not supported for this target\n", popt
->name
);
3064 xen_mode
= XEN_ATTACH
;
3066 case QEMU_OPTION_trace
:
3068 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3072 trace_events
= qemu_opt_get(opts
, "events");
3073 trace_file
= qemu_opt_get(opts
, "file");
3076 case QEMU_OPTION_readconfig
:
3078 int ret
= qemu_read_config_file(optarg
);
3080 fprintf(stderr
, "read config %s: %s\n", optarg
,
3086 case QEMU_OPTION_spice
:
3087 olist
= qemu_find_opts("spice");
3089 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3092 opts
= qemu_opts_parse(olist
, optarg
, 0);
3094 fprintf(stderr
, "parse error: %s\n", optarg
);
3098 case QEMU_OPTION_writeconfig
:
3101 if (strcmp(optarg
, "-") == 0) {
3104 fp
= fopen(optarg
, "w");
3106 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3110 qemu_config_write(fp
);
3115 os_parse_cmd_args(popt
->index
, optarg
);
3121 /* Open the logfile at this point, if necessary. We can't open the logfile
3122 * when encountering either of the logging options (-d or -D) because the
3123 * other one may be encountered later on the command line, changing the
3124 * location or level of logging.
3128 set_cpu_log_filename(log_file
);
3130 set_cpu_log(log_mask
);
3133 if (!trace_backend_init(trace_events
, trace_file
)) {
3137 /* If no data_dir is specified then try to find it relative to the
3140 data_dir
= os_find_datadir(argv
[0]);
3142 /* If all else fails use the install path specified when building. */
3144 data_dir
= CONFIG_QEMU_DATADIR
;
3147 if (machine
== NULL
) {
3148 fprintf(stderr
, "No machine found.\n");
3153 * Default to max_cpus = smp_cpus, in case the user doesn't
3154 * specify a max_cpus value.
3157 max_cpus
= smp_cpus
;
3159 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3160 if (smp_cpus
> machine
->max_cpus
) {
3161 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3162 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3168 * Get the default machine options from the machine if it is not already
3169 * specified either by the configuration file or by the command line.
3171 if (machine
->default_machine_opts
) {
3172 qemu_opts_set_defaults(qemu_find_opts("machine"),
3173 machine
->default_machine_opts
, 0);
3176 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3177 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3179 if (machine
->no_serial
) {
3182 if (machine
->no_parallel
) {
3183 default_parallel
= 0;
3185 if (!machine
->use_virtcon
) {
3186 default_virtcon
= 0;
3188 if (machine
->no_floppy
) {
3191 if (machine
->no_cdrom
) {
3194 if (machine
->no_sdcard
) {
3198 if (display_type
== DT_NOGRAPHIC
) {
3199 if (default_parallel
)
3200 add_device_config(DEV_PARALLEL
, "null");
3201 if (default_serial
&& default_monitor
) {
3202 add_device_config(DEV_SERIAL
, "mon:stdio");
3203 } else if (default_virtcon
&& default_monitor
) {
3204 add_device_config(DEV_VIRTCON
, "mon:stdio");
3207 add_device_config(DEV_SERIAL
, "stdio");
3208 if (default_virtcon
)
3209 add_device_config(DEV_VIRTCON
, "stdio");
3210 if (default_monitor
)
3211 monitor_parse("stdio", "readline");
3215 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3216 if (default_parallel
)
3217 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3218 if (default_monitor
)
3219 monitor_parse("vc:80Cx24C", "readline");
3220 if (default_virtcon
)
3221 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3226 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3228 #ifdef CONFIG_VIRTFS
3229 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3236 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3241 /* init the memory */
3242 if (ram_size
== 0) {
3243 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3246 configure_accelerator();
3248 qemu_init_cpu_loop();
3249 if (qemu_init_main_loop()) {
3250 fprintf(stderr
, "qemu_init_main_loop failed\n");
3254 kernel_filename
= qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3256 initrd_filename
= qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3258 kernel_cmdline
= qemu_opt_get(qemu_opts_find(qemu_find_opts("machine"),
3260 if (!kernel_cmdline
) {
3261 kernel_cmdline
= "";
3264 linux_boot
= (kernel_filename
!= NULL
);
3266 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3267 fprintf(stderr
, "-append only allowed with -kernel option\n");
3271 if (!linux_boot
&& initrd_filename
!= NULL
) {
3272 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3276 os_set_line_buffering();
3278 if (init_timer_alarm() < 0) {
3279 fprintf(stderr
, "could not initialize alarm timer\n");
3283 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3284 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3287 configure_icount(icount_option
);
3289 if (net_init_clients() < 0) {
3293 /* init the bluetooth world */
3294 if (foreach_device_config(DEV_BT
, bt_parse
))
3297 if (!xen_enabled()) {
3298 /* On 32-bit hosts, QEMU is limited by virtual address space */
3299 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3300 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3305 cpu_exec_init_all();
3307 bdrv_init_with_whitelist();
3311 /* open the virtual block devices */
3313 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3314 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3317 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3318 IF_DEFAULT
, 2, CDROM_OPTS
);
3319 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3320 IF_FLOPPY
, 0, FD_OPTS
);
3321 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3324 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3327 if (nb_numa_nodes
> 0) {
3330 if (nb_numa_nodes
> MAX_NODES
) {
3331 nb_numa_nodes
= MAX_NODES
;
3334 /* If no memory size if given for any node, assume the default case
3335 * and distribute the available memory equally across all nodes
3337 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3338 if (node_mem
[i
] != 0)
3341 if (i
== nb_numa_nodes
) {
3342 uint64_t usedmem
= 0;
3344 /* On Linux, the each node's border has to be 8MB aligned,
3345 * the final node gets the rest.
3347 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3348 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3349 usedmem
+= node_mem
[i
];
3351 node_mem
[i
] = ram_size
- usedmem
;
3354 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3355 if (node_cpumask
[i
] != 0)
3358 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3359 * must cope with this anyway, because there are BIOSes out there in
3360 * real machines which also use this scheme.
3362 if (i
== nb_numa_nodes
) {
3363 for (i
= 0; i
< max_cpus
; i
++) {
3364 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3369 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3373 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3375 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3377 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3379 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3382 module_call_init(MODULE_INIT_QOM
);
3384 /* must be after qdev registration but before machine init */
3386 select_vgahw(vga_model
);
3387 } else if (cirrus_vga_available()) {
3388 select_vgahw("cirrus");
3390 select_vgahw("none");
3393 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3397 i
= select_watchdog(watchdog
);
3399 exit (i
== 1 ? 1 : 0);
3402 if (machine
->compat_props
) {
3403 qdev_prop_register_global_list(machine
->compat_props
);
3407 qdev_machine_init();
3409 machine
->init(ram_size
, boot_devices
,
3410 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3412 cpu_synchronize_all_post_init();
3416 current_machine
= machine
;
3418 /* init USB devices */
3420 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3424 /* init generic devices */
3425 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3428 net_check_clients();
3430 /* just use the first displaystate for the moment */
3431 ds
= get_displaystate();
3435 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3436 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3437 display_type
= DT_SDL
;
3438 #elif defined(CONFIG_VNC)
3439 vnc_display
= "localhost:0,to=99";
3442 display_type
= DT_NONE
;
3447 /* init local displays */
3448 switch (display_type
) {
3451 #if defined(CONFIG_CURSES)
3453 curses_display_init(ds
, full_screen
);
3456 #if defined(CONFIG_SDL)
3458 sdl_display_init(ds
, full_screen
, no_frame
);
3460 #elif defined(CONFIG_COCOA)
3462 cocoa_display_init(ds
, full_screen
);
3469 /* must be after terminal init, SDL library changes signal handlers */
3470 os_setup_signal_handling();
3473 /* init remote displays */
3475 vnc_display_init(ds
);
3476 if (vnc_display_open(ds
, vnc_display
) < 0)
3479 if (show_vnc_port
) {
3480 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3485 if (using_spice
&& !qxl_enabled
) {
3486 qemu_spice_display_init(ds
);
3492 dcl
= ds
->listeners
;
3493 while (dcl
!= NULL
) {
3494 if (dcl
->dpy_refresh
!= NULL
) {
3495 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3496 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3501 text_consoles_set_display(ds
);
3503 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3507 qdev_machine_creation_done();
3509 if (rom_load_all() != 0) {
3510 fprintf(stderr
, "rom loading failed\n");
3514 /* TODO: once all bus devices are qdevified, this should be done
3515 * when bus is created by qdev.c */
3516 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3517 qemu_run_machine_init_done_notifiers();
3519 qemu_system_reset(VMRESET_SILENT
);
3521 if (load_vmstate(loadvm
) < 0) {
3527 runstate_set(RUN_STATE_INMIGRATE
);
3528 int ret
= qemu_start_incoming_migration(incoming
);
3530 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3534 } else if (autostart
) {