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"
159 #include "qemu_socket.h"
161 #include "slirp/libslirp.h"
164 #include "trace/control.h"
165 #include "qemu-queue.h"
167 #include "arch_init.h"
169 #include "ui/qemu-spice.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 #define MAX_VIRTIO_CONSOLES 1
178 static const char *data_dir
;
179 const char *bios_name
= NULL
;
180 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
181 DisplayType display_type
= DT_DEFAULT
;
182 int display_remote
= 0;
183 const char* keyboard_layout
= NULL
;
185 const char *mem_path
= NULL
;
187 int mem_prealloc
= 0; /* force preallocation of physical target memory */
190 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 QEMUClock
*rtc_clock
;
195 int vga_interface_type
= VGA_NONE
;
196 static int full_screen
= 0;
198 static int no_frame
= 0;
201 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
202 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
203 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
204 int win2k_install_hack
= 0;
212 const char *vnc_display
;
214 int acpi_enabled
= 1;
220 int graphic_rotate
= 0;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint8_t *boot_splash_filedata
;
233 int boot_splash_filedata_size
;
234 uint8_t qemu_extra_params_fw
[2];
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 uint8_t qemu_uuid
[16];
253 static QEMUBootSetHandler
*boot_set_handler
;
254 static void *boot_set_opaque
;
256 static NotifierList exit_notifiers
=
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
259 static NotifierList machine_init_done_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 static int tcg_allowed
= 1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
267 static int tcg_tb_size
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_floppy
= 1;
274 static int default_cdrom
= 1;
275 static int default_sdcard
= 1;
281 { .driver
= "isa-serial", .flag
= &default_serial
},
282 { .driver
= "isa-parallel", .flag
= &default_parallel
},
283 { .driver
= "isa-fdc", .flag
= &default_floppy
},
284 { .driver
= "ide-cd", .flag
= &default_cdrom
},
285 { .driver
= "ide-hd", .flag
= &default_cdrom
},
286 { .driver
= "ide-drive", .flag
= &default_cdrom
},
287 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
288 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
289 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
293 static void res_free(void)
295 if (boot_splash_filedata
!= NULL
) {
296 g_free(boot_splash_filedata
);
297 boot_splash_filedata
= NULL
;
301 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
303 const char *driver
= qemu_opt_get(opts
, "driver");
308 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
309 if (strcmp(default_list
[i
].driver
, driver
) != 0)
311 *(default_list
[i
].flag
) = 0;
316 /***********************************************************/
319 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
324 } RunStateTransition
;
326 static const RunStateTransition runstate_transitions_def
[] = {
328 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
330 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
331 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
333 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
334 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
336 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
337 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
339 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
340 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
342 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
343 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
345 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
346 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
347 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
349 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
350 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
352 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
354 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
355 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
356 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
357 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
358 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
359 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
360 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
361 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
362 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
364 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
366 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
367 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
369 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
370 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
372 { RUN_STATE_MAX
, RUN_STATE_MAX
},
375 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
377 bool runstate_check(RunState state
)
379 return current_run_state
== state
;
382 void runstate_init(void)
384 const RunStateTransition
*p
;
386 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
388 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
389 runstate_valid_transitions
[p
->from
][p
->to
] = true;
393 /* This function will abort() on invalid state transitions */
394 void runstate_set(RunState new_state
)
396 assert(new_state
< RUN_STATE_MAX
);
398 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
399 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
400 RunState_lookup
[current_run_state
],
401 RunState_lookup
[new_state
]);
405 current_run_state
= new_state
;
408 int runstate_is_running(void)
410 return runstate_check(RUN_STATE_RUNNING
);
413 StatusInfo
*qmp_query_status(Error
**errp
)
415 StatusInfo
*info
= g_malloc0(sizeof(*info
));
417 info
->running
= runstate_is_running();
418 info
->singlestep
= singlestep
;
419 info
->status
= current_run_state
;
424 /***********************************************************/
425 /* real time host monotonic timer */
427 /***********************************************************/
428 /* host time/date access */
429 void qemu_get_timedate(struct tm
*tm
, int offset
)
436 if (rtc_date_offset
== -1) {
440 ret
= localtime(&ti
);
442 ti
-= rtc_date_offset
;
446 memcpy(tm
, ret
, sizeof(struct tm
));
449 int qemu_timedate_diff(struct tm
*tm
)
453 if (rtc_date_offset
== -1)
455 seconds
= mktimegm(tm
);
458 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
459 seconds
= mktime(&tmp
);
462 seconds
= mktimegm(tm
) + rtc_date_offset
;
464 return seconds
- time(NULL
);
467 void rtc_change_mon_event(struct tm
*tm
)
471 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
472 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
473 qobject_decref(data
);
476 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
478 time_t rtc_start_date
;
481 if (!strcmp(startdate
, "now") && legacy
) {
482 rtc_date_offset
= -1;
484 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
492 } else if (sscanf(startdate
, "%d-%d-%d",
504 rtc_start_date
= mktimegm(&tm
);
505 if (rtc_start_date
== -1) {
507 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
508 "'2006-06-17T16:01:21' or '2006-06-17'\n");
511 rtc_date_offset
= time(NULL
) - rtc_start_date
;
515 static void configure_rtc(QemuOpts
*opts
)
519 value
= qemu_opt_get(opts
, "base");
521 if (!strcmp(value
, "utc")) {
523 } else if (!strcmp(value
, "localtime")) {
526 configure_rtc_date_offset(value
, 0);
529 value
= qemu_opt_get(opts
, "clock");
531 if (!strcmp(value
, "host")) {
532 rtc_clock
= host_clock
;
533 } else if (!strcmp(value
, "rt")) {
534 rtc_clock
= rt_clock
;
535 } else if (!strcmp(value
, "vm")) {
536 rtc_clock
= vm_clock
;
538 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
542 value
= qemu_opt_get(opts
, "driftfix");
544 if (!strcmp(value
, "slew")) {
545 static GlobalProperty slew_lost_ticks
[] = {
547 .driver
= "mc146818rtc",
548 .property
= "lost_tick_policy",
551 { /* end of list */ }
554 qdev_prop_register_global_list(slew_lost_ticks
);
555 } else if (!strcmp(value
, "none")) {
556 /* discard is default */
558 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
564 /***********************************************************/
565 /* Bluetooth support */
568 static struct HCIInfo
*hci_table
[MAX_NICS
];
570 static struct bt_vlan_s
{
571 struct bt_scatternet_s net
;
573 struct bt_vlan_s
*next
;
576 /* find or alloc a new bluetooth "VLAN" */
577 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
579 struct bt_vlan_s
**pvlan
, *vlan
;
580 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
584 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
586 pvlan
= &first_bt_vlan
;
587 while (*pvlan
!= NULL
)
588 pvlan
= &(*pvlan
)->next
;
593 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
597 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
602 static struct HCIInfo null_hci
= {
603 .cmd_send
= null_hci_send
,
604 .sco_send
= null_hci_send
,
605 .acl_send
= null_hci_send
,
606 .bdaddr_set
= null_hci_addr_set
,
609 struct HCIInfo
*qemu_next_hci(void)
611 if (cur_hci
== nb_hcis
)
614 return hci_table
[cur_hci
++];
617 static struct HCIInfo
*hci_init(const char *str
)
620 struct bt_scatternet_s
*vlan
= 0;
622 if (!strcmp(str
, "null"))
625 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
627 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
628 else if (!strncmp(str
, "hci", 3)) {
631 if (!strncmp(str
+ 3, ",vlan=", 6)) {
632 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
637 vlan
= qemu_find_bt_vlan(0);
639 return bt_new_hci(vlan
);
642 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
647 static int bt_hci_parse(const char *str
)
652 if (nb_hcis
>= MAX_NICS
) {
653 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
666 bdaddr
.b
[5] = 0x56 + nb_hcis
;
667 hci
->bdaddr_set(hci
, bdaddr
.b
);
669 hci_table
[nb_hcis
++] = hci
;
674 static void bt_vhci_add(int vlan_id
)
676 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
679 fprintf(stderr
, "qemu: warning: adding a VHCI to "
680 "an empty scatternet %i\n", vlan_id
);
682 bt_vhci_init(bt_new_hci(vlan
));
685 static struct bt_device_s
*bt_device_add(const char *opt
)
687 struct bt_scatternet_s
*vlan
;
689 char *endp
= strstr(opt
, ",vlan=");
690 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
693 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
696 vlan_id
= strtol(endp
+ 6, &endp
, 0);
698 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
703 vlan
= qemu_find_bt_vlan(vlan_id
);
706 fprintf(stderr
, "qemu: warning: adding a slave device to "
707 "an empty scatternet %i\n", vlan_id
);
709 if (!strcmp(devname
, "keyboard"))
710 return bt_keyboard_init(vlan
);
712 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
716 static int bt_parse(const char *opt
)
718 const char *endp
, *p
;
721 if (strstart(opt
, "hci", &endp
)) {
722 if (!*endp
|| *endp
== ',') {
724 if (!strstart(endp
, ",vlan=", 0))
727 return bt_hci_parse(opt
);
729 } else if (strstart(opt
, "vhci", &endp
)) {
730 if (!*endp
|| *endp
== ',') {
732 if (strstart(endp
, ",vlan=", &p
)) {
733 vlan
= strtol(p
, (char **) &endp
, 0);
735 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
739 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
748 } else if (strstart(opt
, "device:", &endp
))
749 return !bt_device_add(endp
);
751 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
755 /***********************************************************/
756 /* QEMU Block devices */
758 #define HD_OPTS "media=disk"
759 #define CDROM_OPTS "media=cdrom"
761 #define PFLASH_OPTS ""
765 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
767 int *use_scsi
= opaque
;
769 return drive_init(opts
, *use_scsi
) == NULL
;
772 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
774 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
775 qemu_opt_set(opts
, "snapshot", "on");
780 static void default_drive(int enable
, int snapshot
, int use_scsi
,
781 BlockInterfaceType type
, int index
,
786 if (type
== IF_DEFAULT
) {
787 type
= use_scsi
? IF_SCSI
: IF_IDE
;
790 if (!enable
|| drive_get_by_index(type
, index
)) {
794 opts
= drive_add(type
, index
, NULL
, optstr
);
796 drive_enable_snapshot(opts
, NULL
);
798 if (!drive_init(opts
, use_scsi
)) {
803 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
805 boot_set_handler
= func
;
806 boot_set_opaque
= opaque
;
809 int qemu_boot_set(const char *boot_devices
)
811 if (!boot_set_handler
) {
814 return boot_set_handler(boot_set_opaque
, boot_devices
);
817 static void validate_bootdevices(char *devices
)
819 /* We just do some generic consistency checks */
823 for (p
= devices
; *p
!= '\0'; p
++) {
824 /* Allowed boot devices are:
825 * a-b: floppy disk drives
826 * c-f: IDE disk drives
827 * g-m: machine implementation dependent drives
828 * n-p: network devices
829 * It's up to each machine implementation to check if the given boot
830 * devices match the actual hardware implementation and firmware
833 if (*p
< 'a' || *p
> 'p') {
834 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
837 if (bitmap
& (1 << (*p
- 'a'))) {
838 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
841 bitmap
|= 1 << (*p
- 'a');
845 static void restore_boot_devices(void *opaque
)
847 char *standard_boot_devices
= opaque
;
848 static int first
= 1;
850 /* Restore boot order and remove ourselves after the first boot */
856 qemu_boot_set(standard_boot_devices
);
858 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
859 g_free(standard_boot_devices
);
862 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
865 FWBootEntry
*node
, *i
;
871 assert(dev
!= NULL
|| suffix
!= NULL
);
873 node
= g_malloc0(sizeof(FWBootEntry
));
874 node
->bootindex
= bootindex
;
875 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
878 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
879 if (i
->bootindex
== bootindex
) {
880 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
882 } else if (i
->bootindex
< bootindex
) {
885 QTAILQ_INSERT_BEFORE(i
, node
, link
);
888 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
892 * This function returns null terminated string that consist of new line
893 * separated device paths.
895 * memory pointed by "size" is assigned total length of the array in bytes
898 char *get_boot_devices_list(uint32_t *size
)
904 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
905 char *devpath
= NULL
, *bootpath
;
909 devpath
= qdev_get_fw_dev_path(i
->dev
);
913 if (i
->suffix
&& devpath
) {
914 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
916 bootpath
= g_malloc(bootpathlen
);
917 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
919 } else if (devpath
) {
923 bootpath
= g_strdup(i
->suffix
);
927 list
[total
-1] = '\n';
929 len
= strlen(bootpath
) + 1;
930 list
= g_realloc(list
, total
+ len
);
931 memcpy(&list
[total
], bootpath
, len
);
941 static void numa_add(const char *optarg
)
945 unsigned long long value
, endvalue
;
948 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
949 if (!strcmp(option
, "node")) {
950 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
951 nodenr
= nb_numa_nodes
;
953 nodenr
= strtoull(option
, NULL
, 10);
956 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
957 node_mem
[nodenr
] = 0;
960 sval
= strtosz(option
, &endptr
);
961 if (sval
< 0 || *endptr
) {
962 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
965 node_mem
[nodenr
] = sval
;
967 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
968 node_cpumask
[nodenr
] = 0;
970 value
= strtoull(option
, &endptr
, 10);
973 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
975 if (*endptr
== '-') {
976 endvalue
= strtoull(endptr
+1, &endptr
, 10);
977 if (endvalue
>= 63) {
980 "only 63 CPUs in NUMA mode supported.\n");
982 value
= (2ULL << endvalue
) - (1ULL << value
);
984 value
= 1ULL << value
;
987 node_cpumask
[nodenr
] = value
;
994 static void smp_parse(const char *optarg
)
996 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1000 smp
= strtoul(optarg
, &endptr
, 10);
1001 if (endptr
!= optarg
) {
1002 if (*endptr
== ',') {
1006 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1007 sockets
= strtoull(option
, NULL
, 10);
1008 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1009 cores
= strtoull(option
, NULL
, 10);
1010 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1011 threads
= strtoull(option
, NULL
, 10);
1012 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1013 max_cpus
= strtoull(option
, NULL
, 10);
1015 /* compute missing values, prefer sockets over cores over threads */
1016 if (smp
== 0 || sockets
== 0) {
1017 sockets
= sockets
> 0 ? sockets
: 1;
1018 cores
= cores
> 0 ? cores
: 1;
1019 threads
= threads
> 0 ? threads
: 1;
1021 smp
= cores
* threads
* sockets
;
1025 threads
= threads
> 0 ? threads
: 1;
1026 cores
= smp
/ (sockets
* threads
);
1028 threads
= smp
/ (cores
* sockets
);
1032 smp_cores
= cores
> 0 ? cores
: 1;
1033 smp_threads
= threads
> 0 ? threads
: 1;
1035 max_cpus
= smp_cpus
;
1038 /***********************************************************/
1041 static int usb_device_add(const char *devname
)
1044 USBDevice
*dev
= NULL
;
1049 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1050 dev
= usbdevice_create(devname
);
1054 /* the other ones */
1055 #ifndef CONFIG_LINUX
1056 /* only the linux version is qdev-ified, usb-bsd still needs this */
1057 if (strstart(devname
, "host:", &p
)) {
1058 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1061 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1062 dev
= usb_bt_init(usb_bus_find(-1),
1063 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 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
;
1217 while (dcl
!= NULL
) {
1218 if (dcl
->gui_timer_interval
&&
1219 dcl
->gui_timer_interval
< interval
)
1220 interval
= dcl
->gui_timer_interval
;
1223 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1226 struct vm_change_state_entry
{
1227 VMChangeStateHandler
*cb
;
1229 QLIST_ENTRY (vm_change_state_entry
) entries
;
1232 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1234 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1237 VMChangeStateEntry
*e
;
1239 e
= g_malloc0(sizeof (*e
));
1243 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1247 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1249 QLIST_REMOVE (e
, entries
);
1253 void vm_state_notify(int running
, RunState state
)
1255 VMChangeStateEntry
*e
;
1257 trace_vm_state_notify(running
, state
);
1259 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1260 e
->cb(e
->opaque
, running
, state
);
1266 if (!runstate_is_running()) {
1268 runstate_set(RUN_STATE_RUNNING
);
1269 vm_state_notify(1, RUN_STATE_RUNNING
);
1271 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1275 /* reset/shutdown handler */
1277 typedef struct QEMUResetEntry
{
1278 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1279 QEMUResetHandler
*func
;
1283 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1284 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1285 static int reset_requested
;
1286 static int shutdown_requested
, shutdown_signal
= -1;
1287 static pid_t shutdown_pid
;
1288 static int powerdown_requested
;
1289 static int debug_requested
;
1290 static int suspend_requested
;
1291 static bool is_suspended
;
1292 static NotifierList suspend_notifiers
=
1293 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1294 static NotifierList wakeup_notifiers
=
1295 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1296 static uint32_t wakeup_reason_mask
= ~0;
1297 static RunState vmstop_requested
= RUN_STATE_MAX
;
1299 int qemu_shutdown_requested_get(void)
1301 return shutdown_requested
;
1304 int qemu_reset_requested_get(void)
1306 return reset_requested
;
1309 int qemu_shutdown_requested(void)
1311 int r
= shutdown_requested
;
1312 shutdown_requested
= 0;
1316 void qemu_kill_report(void)
1318 if (!qtest_enabled() && shutdown_signal
!= -1) {
1319 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1320 if (shutdown_pid
== 0) {
1321 /* This happens for eg ^C at the terminal, so it's worth
1322 * avoiding printing an odd message in that case.
1324 fputc('\n', stderr
);
1326 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1328 shutdown_signal
= -1;
1332 int qemu_reset_requested(void)
1334 int r
= reset_requested
;
1335 reset_requested
= 0;
1339 static int qemu_suspend_requested(void)
1341 int r
= suspend_requested
;
1342 suspend_requested
= 0;
1346 int qemu_powerdown_requested(void)
1348 int r
= powerdown_requested
;
1349 powerdown_requested
= 0;
1353 static int qemu_debug_requested(void)
1355 int r
= debug_requested
;
1356 debug_requested
= 0;
1360 /* We use RUN_STATE_MAX but any invalid value will do */
1361 static bool qemu_vmstop_requested(RunState
*r
)
1363 if (vmstop_requested
< RUN_STATE_MAX
) {
1364 *r
= vmstop_requested
;
1365 vmstop_requested
= RUN_STATE_MAX
;
1372 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1374 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1377 re
->opaque
= opaque
;
1378 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1381 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1385 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1386 if (re
->func
== func
&& re
->opaque
== opaque
) {
1387 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1394 void qemu_system_reset(bool report
)
1396 QEMUResetEntry
*re
, *nre
;
1398 /* reset all devices */
1399 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1400 re
->func(re
->opaque
);
1403 monitor_protocol_event(QEVENT_RESET
, NULL
);
1405 cpu_synchronize_all_post_reset();
1408 void qemu_system_reset_request(void)
1411 shutdown_requested
= 1;
1413 reset_requested
= 1;
1416 qemu_notify_event();
1419 static void qemu_system_suspend(void)
1422 notifier_list_notify(&suspend_notifiers
, NULL
);
1423 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1424 is_suspended
= true;
1427 void qemu_system_suspend_request(void)
1432 suspend_requested
= 1;
1434 qemu_notify_event();
1437 void qemu_register_suspend_notifier(Notifier
*notifier
)
1439 notifier_list_add(&suspend_notifiers
, notifier
);
1442 void qemu_system_wakeup_request(WakeupReason reason
)
1444 if (!is_suspended
) {
1447 if (!(wakeup_reason_mask
& (1 << reason
))) {
1450 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1451 notifier_list_notify(&wakeup_notifiers
, &reason
);
1452 reset_requested
= 1;
1453 qemu_notify_event();
1454 is_suspended
= false;
1457 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1460 wakeup_reason_mask
|= (1 << reason
);
1462 wakeup_reason_mask
&= ~(1 << reason
);
1466 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1468 notifier_list_add(&wakeup_notifiers
, notifier
);
1471 void qemu_system_killed(int signal
, pid_t pid
)
1473 shutdown_signal
= signal
;
1476 qemu_system_shutdown_request();
1479 void qemu_system_shutdown_request(void)
1481 shutdown_requested
= 1;
1482 qemu_notify_event();
1485 void qemu_system_powerdown_request(void)
1487 powerdown_requested
= 1;
1488 qemu_notify_event();
1491 void qemu_system_debug_request(void)
1493 debug_requested
= 1;
1494 qemu_notify_event();
1497 void qemu_system_vmstop_request(RunState state
)
1499 vmstop_requested
= state
;
1500 qemu_notify_event();
1503 qemu_irq qemu_system_powerdown
;
1505 static bool main_loop_should_exit(void)
1508 if (qemu_debug_requested()) {
1509 vm_stop(RUN_STATE_DEBUG
);
1511 if (qemu_suspend_requested()) {
1512 qemu_system_suspend();
1514 if (qemu_shutdown_requested()) {
1516 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1518 vm_stop(RUN_STATE_SHUTDOWN
);
1523 if (qemu_reset_requested()) {
1525 cpu_synchronize_all_states();
1526 qemu_system_reset(VMRESET_REPORT
);
1528 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1529 runstate_check(RUN_STATE_SHUTDOWN
)) {
1530 runstate_set(RUN_STATE_PAUSED
);
1533 if (qemu_powerdown_requested()) {
1534 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1535 qemu_irq_raise(qemu_system_powerdown
);
1537 if (qemu_vmstop_requested(&r
)) {
1543 static void main_loop(void)
1547 #ifdef CONFIG_PROFILER
1551 nonblocking
= !kvm_enabled() && last_io
> 0;
1552 #ifdef CONFIG_PROFILER
1553 ti
= profile_getclock();
1555 last_io
= main_loop_wait(nonblocking
);
1556 #ifdef CONFIG_PROFILER
1557 dev_time
+= profile_getclock() - ti
;
1559 } while (!main_loop_should_exit());
1562 static void version(void)
1564 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1567 static void help(int exitcode
)
1570 printf("usage: %s [options] [disk_image]\n\n"
1571 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1572 error_get_progname());
1574 #define QEMU_OPTIONS_GENERATE_HELP
1575 #include "qemu-options-wrapper.h"
1577 printf("\nDuring emulation, the following keys are useful:\n"
1578 "ctrl-alt-f toggle full screen\n"
1579 "ctrl-alt-n switch to virtual console 'n'\n"
1580 "ctrl-alt toggle mouse and keyboard grab\n"
1582 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1587 #define HAS_ARG 0x0001
1589 typedef struct QEMUOption
{
1596 static const QEMUOption qemu_options
[] = {
1597 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1598 #define QEMU_OPTIONS_GENERATE_OPTIONS
1599 #include "qemu-options-wrapper.h"
1603 static bool vga_available(void)
1605 return qdev_exists("VGA") || qdev_exists("isa-vga");
1608 static bool cirrus_vga_available(void)
1610 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1613 static bool vmware_vga_available(void)
1615 return qdev_exists("vmware-svga");
1618 static void select_vgahw (const char *p
)
1622 vga_interface_type
= VGA_NONE
;
1623 if (strstart(p
, "std", &opts
)) {
1624 if (vga_available()) {
1625 vga_interface_type
= VGA_STD
;
1627 fprintf(stderr
, "Error: standard VGA not available\n");
1630 } else if (strstart(p
, "cirrus", &opts
)) {
1631 if (cirrus_vga_available()) {
1632 vga_interface_type
= VGA_CIRRUS
;
1634 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1637 } else if (strstart(p
, "vmware", &opts
)) {
1638 if (vmware_vga_available()) {
1639 vga_interface_type
= VGA_VMWARE
;
1641 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1644 } else if (strstart(p
, "xenfb", &opts
)) {
1645 vga_interface_type
= VGA_XENFB
;
1646 } else if (strstart(p
, "qxl", &opts
)) {
1647 vga_interface_type
= VGA_QXL
;
1648 } else if (!strstart(p
, "none", &opts
)) {
1650 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1654 const char *nextopt
;
1656 if (strstart(opts
, ",retrace=", &nextopt
)) {
1658 if (strstart(opts
, "dumb", &nextopt
))
1659 vga_retrace_method
= VGA_RETRACE_DUMB
;
1660 else if (strstart(opts
, "precise", &nextopt
))
1661 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1662 else goto invalid_vga
;
1663 } else goto invalid_vga
;
1668 static DisplayType
select_display(const char *p
)
1671 DisplayType display
= DT_DEFAULT
;
1673 if (strstart(p
, "sdl", &opts
)) {
1677 const char *nextopt
;
1679 if (strstart(opts
, ",frame=", &nextopt
)) {
1681 if (strstart(opts
, "on", &nextopt
)) {
1683 } else if (strstart(opts
, "off", &nextopt
)) {
1686 goto invalid_sdl_args
;
1688 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1690 if (strstart(opts
, "on", &nextopt
)) {
1692 } else if (strstart(opts
, "off", &nextopt
)) {
1695 goto invalid_sdl_args
;
1697 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1699 if (strstart(opts
, "on", &nextopt
)) {
1701 } else if (strstart(opts
, "off", &nextopt
)) {
1704 goto invalid_sdl_args
;
1706 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1708 if (strstart(opts
, "on", &nextopt
)) {
1710 } else if (strstart(opts
, "off", &nextopt
)) {
1713 goto invalid_sdl_args
;
1717 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1723 fprintf(stderr
, "SDL support is disabled\n");
1726 } else if (strstart(p
, "vnc", &opts
)) {
1731 const char *nextopt
;
1733 if (strstart(opts
, "=", &nextopt
)) {
1734 vnc_display
= nextopt
;
1738 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1742 fprintf(stderr
, "VNC support is disabled\n");
1745 } else if (strstart(p
, "curses", &opts
)) {
1746 #ifdef CONFIG_CURSES
1747 display
= DT_CURSES
;
1749 fprintf(stderr
, "Curses support is disabled\n");
1752 } else if (strstart(p
, "none", &opts
)) {
1755 fprintf(stderr
, "Unknown display type: %s\n", p
);
1762 static int balloon_parse(const char *arg
)
1766 if (strcmp(arg
, "none") == 0) {
1770 if (!strncmp(arg
, "virtio", 6)) {
1771 if (arg
[6] == ',') {
1772 /* have params -> parse them */
1773 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1777 /* create empty opts */
1778 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1780 qemu_opt_set(opts
, "driver", "virtio-balloon");
1787 char *qemu_find_file(int type
, const char *name
)
1793 /* If name contains path separators then try it as a straight path. */
1794 if ((strchr(name
, '/') || strchr(name
, '\\'))
1795 && access(name
, R_OK
) == 0) {
1796 return g_strdup(name
);
1799 case QEMU_FILE_TYPE_BIOS
:
1802 case QEMU_FILE_TYPE_KEYMAP
:
1803 subdir
= "keymaps/";
1808 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1809 buf
= g_malloc0(len
);
1810 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1811 if (access(buf
, R_OK
)) {
1818 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1820 return qdev_device_help(opts
);
1823 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1827 dev
= qdev_device_add(opts
);
1833 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1835 CharDriverState
*chr
;
1837 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1843 #ifdef CONFIG_VIRTFS
1844 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1847 ret
= qemu_fsdev_add(opts
);
1853 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1855 CharDriverState
*chr
;
1856 const char *chardev
;
1860 mode
= qemu_opt_get(opts
, "mode");
1864 if (strcmp(mode
, "readline") == 0) {
1865 flags
= MONITOR_USE_READLINE
;
1866 } else if (strcmp(mode
, "control") == 0) {
1867 flags
= MONITOR_USE_CONTROL
;
1869 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1873 if (qemu_opt_get_bool(opts
, "pretty", 0))
1874 flags
|= MONITOR_USE_PRETTY
;
1876 if (qemu_opt_get_bool(opts
, "default", 0))
1877 flags
|= MONITOR_IS_DEFAULT
;
1879 chardev
= qemu_opt_get(opts
, "chardev");
1880 chr
= qemu_chr_find(chardev
);
1882 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1886 monitor_init(chr
, flags
);
1890 static void monitor_parse(const char *optarg
, const char *mode
)
1892 static int monitor_device_index
= 0;
1898 if (strstart(optarg
, "chardev:", &p
)) {
1899 snprintf(label
, sizeof(label
), "%s", p
);
1901 snprintf(label
, sizeof(label
), "compat_monitor%d",
1902 monitor_device_index
);
1903 if (monitor_device_index
== 0) {
1906 opts
= qemu_chr_parse_compat(label
, optarg
);
1908 fprintf(stderr
, "parse error: %s\n", optarg
);
1913 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1915 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1918 qemu_opt_set(opts
, "mode", mode
);
1919 qemu_opt_set(opts
, "chardev", label
);
1921 qemu_opt_set(opts
, "default", "on");
1922 monitor_device_index
++;
1925 struct device_config
{
1927 DEV_USB
, /* -usbdevice */
1929 DEV_SERIAL
, /* -serial */
1930 DEV_PARALLEL
, /* -parallel */
1931 DEV_VIRTCON
, /* -virtioconsole */
1932 DEV_DEBUGCON
, /* -debugcon */
1933 DEV_GDB
, /* -gdb, -s */
1935 const char *cmdline
;
1937 QTAILQ_ENTRY(device_config
) next
;
1939 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1941 static void add_device_config(int type
, const char *cmdline
)
1943 struct device_config
*conf
;
1945 conf
= g_malloc0(sizeof(*conf
));
1947 conf
->cmdline
= cmdline
;
1948 loc_save(&conf
->loc
);
1949 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1952 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1954 struct device_config
*conf
;
1957 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1958 if (conf
->type
!= type
)
1960 loc_push_restore(&conf
->loc
);
1961 rc
= func(conf
->cmdline
);
1962 loc_pop(&conf
->loc
);
1969 static int serial_parse(const char *devname
)
1971 static int index
= 0;
1974 if (strcmp(devname
, "none") == 0)
1976 if (index
== MAX_SERIAL_PORTS
) {
1977 fprintf(stderr
, "qemu: too many serial ports\n");
1980 snprintf(label
, sizeof(label
), "serial%d", index
);
1981 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1982 if (!serial_hds
[index
]) {
1983 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1984 devname
, strerror(errno
));
1991 static int parallel_parse(const char *devname
)
1993 static int index
= 0;
1996 if (strcmp(devname
, "none") == 0)
1998 if (index
== MAX_PARALLEL_PORTS
) {
1999 fprintf(stderr
, "qemu: too many parallel ports\n");
2002 snprintf(label
, sizeof(label
), "parallel%d", index
);
2003 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2004 if (!parallel_hds
[index
]) {
2005 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2006 devname
, strerror(errno
));
2013 static int virtcon_parse(const char *devname
)
2015 QemuOptsList
*device
= qemu_find_opts("device");
2016 static int index
= 0;
2018 QemuOpts
*bus_opts
, *dev_opts
;
2020 if (strcmp(devname
, "none") == 0)
2022 if (index
== MAX_VIRTIO_CONSOLES
) {
2023 fprintf(stderr
, "qemu: too many virtio consoles\n");
2027 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2028 if (arch_type
== QEMU_ARCH_S390X
) {
2029 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2031 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2034 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2035 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2037 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2038 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2039 if (!virtcon_hds
[index
]) {
2040 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2041 devname
, strerror(errno
));
2044 qemu_opt_set(dev_opts
, "chardev", label
);
2050 static int debugcon_parse(const char *devname
)
2054 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2057 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2059 fprintf(stderr
, "qemu: already have a debugcon device\n");
2062 qemu_opt_set(opts
, "driver", "isa-debugcon");
2063 qemu_opt_set(opts
, "chardev", "debugcon");
2067 static QEMUMachine
*machine_parse(const char *name
)
2069 QEMUMachine
*m
, *machine
= NULL
;
2072 machine
= find_machine(name
);
2077 printf("Supported machines are:\n");
2078 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2080 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2082 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2083 m
->is_default
? " (default)" : "");
2085 exit(!name
|| *name
!= '?');
2088 static int tcg_init(void)
2090 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2095 const char *opt_name
;
2097 int (*available
)(void);
2101 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2102 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2103 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2104 { "qtest", "QTest", qtest_available
, qtest_init
, &qtest_allowed
},
2107 static int configure_accelerator(void)
2109 const char *p
= NULL
;
2112 bool accel_initialised
= 0;
2113 bool init_failed
= 0;
2115 QemuOptsList
*list
= qemu_find_opts("machine");
2116 if (!QTAILQ_EMPTY(&list
->head
)) {
2117 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2121 /* Use the default "accelerator", tcg */
2125 while (!accel_initialised
&& *p
!= '\0') {
2129 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2130 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2131 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2132 *(accel_list
[i
].allowed
) = 1;
2133 ret
= accel_list
[i
].init();
2136 if (!accel_list
[i
].available()) {
2137 printf("%s not supported for this target\n",
2138 accel_list
[i
].name
);
2140 fprintf(stderr
, "failed to initialize %s: %s\n",
2144 *(accel_list
[i
].allowed
) = 0;
2146 accel_initialised
= 1;
2151 if (i
== ARRAY_SIZE(accel_list
)) {
2152 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2156 if (!accel_initialised
) {
2157 fprintf(stderr
, "No accelerator found!\n");
2162 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2165 return !accel_initialised
;
2168 void qemu_add_exit_notifier(Notifier
*notify
)
2170 notifier_list_add(&exit_notifiers
, notify
);
2173 void qemu_remove_exit_notifier(Notifier
*notify
)
2175 notifier_remove(notify
);
2178 static void qemu_run_exit_notifiers(void)
2180 notifier_list_notify(&exit_notifiers
, NULL
);
2183 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2185 notifier_list_add(&machine_init_done_notifiers
, notify
);
2188 static void qemu_run_machine_init_done_notifiers(void)
2190 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2193 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2194 const char **poptarg
, int *poptind
)
2196 const QEMUOption
*popt
;
2197 int optind
= *poptind
;
2198 char *r
= argv
[optind
];
2201 loc_set_cmdline(argv
, optind
, 1);
2203 /* Treat --foo the same as -foo. */
2206 popt
= qemu_options
;
2209 error_report("invalid option");
2212 if (!strcmp(popt
->name
, r
+ 1))
2216 if (popt
->flags
& HAS_ARG
) {
2217 if (optind
>= argc
) {
2218 error_report("requires an argument");
2221 optarg
= argv
[optind
++];
2222 loc_set_cmdline(argv
, optind
- 2, 2);
2233 static gpointer
malloc_and_trace(gsize n_bytes
)
2235 void *ptr
= malloc(n_bytes
);
2236 trace_g_malloc(n_bytes
, ptr
);
2240 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2242 void *ptr
= realloc(mem
, n_bytes
);
2243 trace_g_realloc(mem
, n_bytes
, ptr
);
2247 static void free_and_trace(gpointer mem
)
2253 int qemu_init_main_loop(void)
2255 return main_loop_init();
2258 int main(int argc
, char **argv
, char **envp
)
2261 int snapshot
, linux_boot
;
2262 const char *icount_option
= NULL
;
2263 const char *initrd_filename
;
2264 const char *kernel_filename
, *kernel_cmdline
;
2265 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2267 DisplayChangeListener
*dcl
;
2268 int cyls
, heads
, secs
, translation
;
2269 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2270 QemuOptsList
*olist
;
2273 const char *loadvm
= NULL
;
2274 QEMUMachine
*machine
;
2275 const char *cpu_model
;
2276 const char *vga_model
= NULL
;
2277 const char *pid_file
= NULL
;
2278 const char *incoming
= NULL
;
2280 int show_vnc_port
= 0;
2283 const char *log_mask
= NULL
;
2284 const char *log_file
= NULL
;
2285 GMemVTable mem_trace
= {
2286 .malloc
= malloc_and_trace
,
2287 .realloc
= realloc_and_trace
,
2288 .free
= free_and_trace
,
2290 const char *trace_events
= NULL
;
2291 const char *trace_file
= NULL
;
2293 atexit(qemu_run_exit_notifiers
);
2294 error_set_progname(argv
[0]);
2296 g_mem_set_vtable(&mem_trace
);
2297 if (!g_thread_supported()) {
2298 #if !GLIB_CHECK_VERSION(2, 31, 0)
2299 g_thread_init(NULL
);
2301 fprintf(stderr
, "glib threading failed to initialize.\n");
2306 module_call_init(MODULE_INIT_QOM
);
2311 rtc_clock
= host_clock
;
2313 qemu_cache_utils_init(envp
);
2315 QLIST_INIT (&vm_change_state_head
);
2316 os_setup_early_signal_handling();
2318 module_call_init(MODULE_INIT_MACHINE
);
2319 machine
= find_default_machine();
2323 cyls
= heads
= secs
= 0;
2324 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2326 for (i
= 0; i
< MAX_NODES
; i
++) {
2328 node_cpumask
[i
] = 0;
2336 /* first pass of option parsing */
2338 while (optind
< argc
) {
2339 if (argv
[optind
][0] != '-') {
2344 const QEMUOption
*popt
;
2346 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2347 switch (popt
->index
) {
2348 case QEMU_OPTION_nodefconfig
:
2358 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2359 if (ret
< 0 && ret
!= -ENOENT
) {
2363 ret
= qemu_read_config_file(arch_config_name
);
2364 if (ret
< 0 && ret
!= -ENOENT
) {
2369 /* second pass of option parsing */
2374 if (argv
[optind
][0] != '-') {
2375 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2377 const QEMUOption
*popt
;
2379 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2380 if (!(popt
->arch_mask
& arch_type
)) {
2381 printf("Option %s not supported for this target\n", popt
->name
);
2384 switch(popt
->index
) {
2386 machine
= machine_parse(optarg
);
2388 case QEMU_OPTION_cpu
:
2389 /* hw initialization will check this */
2392 case QEMU_OPTION_hda
:
2396 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2398 snprintf(buf
, sizeof(buf
),
2399 "%s,cyls=%d,heads=%d,secs=%d%s",
2400 HD_OPTS
, cyls
, heads
, secs
,
2401 translation
== BIOS_ATA_TRANSLATION_LBA
?
2403 translation
== BIOS_ATA_TRANSLATION_NONE
?
2404 ",trans=none" : "");
2405 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2408 case QEMU_OPTION_hdb
:
2409 case QEMU_OPTION_hdc
:
2410 case QEMU_OPTION_hdd
:
2411 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2414 case QEMU_OPTION_drive
:
2415 if (drive_def(optarg
) == NULL
) {
2419 case QEMU_OPTION_set
:
2420 if (qemu_set_option(optarg
) != 0)
2423 case QEMU_OPTION_global
:
2424 if (qemu_global_option(optarg
) != 0)
2427 case QEMU_OPTION_mtdblock
:
2428 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2430 case QEMU_OPTION_sd
:
2431 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2433 case QEMU_OPTION_pflash
:
2434 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2436 case QEMU_OPTION_snapshot
:
2439 case QEMU_OPTION_hdachs
:
2443 cyls
= strtol(p
, (char **)&p
, 0);
2444 if (cyls
< 1 || cyls
> 16383)
2449 heads
= strtol(p
, (char **)&p
, 0);
2450 if (heads
< 1 || heads
> 16)
2455 secs
= strtol(p
, (char **)&p
, 0);
2456 if (secs
< 1 || secs
> 63)
2460 if (!strcmp(p
, "none"))
2461 translation
= BIOS_ATA_TRANSLATION_NONE
;
2462 else if (!strcmp(p
, "lba"))
2463 translation
= BIOS_ATA_TRANSLATION_LBA
;
2464 else if (!strcmp(p
, "auto"))
2465 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2468 } else if (*p
!= '\0') {
2470 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2473 if (hda_opts
!= NULL
) {
2475 snprintf(num
, sizeof(num
), "%d", cyls
);
2476 qemu_opt_set(hda_opts
, "cyls", num
);
2477 snprintf(num
, sizeof(num
), "%d", heads
);
2478 qemu_opt_set(hda_opts
, "heads", num
);
2479 snprintf(num
, sizeof(num
), "%d", secs
);
2480 qemu_opt_set(hda_opts
, "secs", num
);
2481 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2482 qemu_opt_set(hda_opts
, "trans", "lba");
2483 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2484 qemu_opt_set(hda_opts
, "trans", "none");
2488 case QEMU_OPTION_numa
:
2489 if (nb_numa_nodes
>= MAX_NODES
) {
2490 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2495 case QEMU_OPTION_display
:
2496 display_type
= select_display(optarg
);
2498 case QEMU_OPTION_nographic
:
2499 display_type
= DT_NOGRAPHIC
;
2501 case QEMU_OPTION_curses
:
2502 #ifdef CONFIG_CURSES
2503 display_type
= DT_CURSES
;
2505 fprintf(stderr
, "Curses support is disabled\n");
2509 case QEMU_OPTION_portrait
:
2510 graphic_rotate
= 90;
2512 case QEMU_OPTION_rotate
:
2513 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2514 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2515 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2517 "qemu: only 90, 180, 270 deg rotation is available\n");
2521 case QEMU_OPTION_kernel
:
2522 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2524 case QEMU_OPTION_initrd
:
2525 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2527 case QEMU_OPTION_append
:
2528 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2530 case QEMU_OPTION_dtb
:
2531 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
2533 case QEMU_OPTION_cdrom
:
2534 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2536 case QEMU_OPTION_boot
:
2538 static const char * const params
[] = {
2539 "order", "once", "menu",
2540 "splash", "splash-time", NULL
2542 char buf
[sizeof(boot_devices
)];
2543 char *standard_boot_devices
;
2546 if (!strchr(optarg
, '=')) {
2548 pstrcpy(buf
, sizeof(buf
), optarg
);
2549 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2551 "qemu: unknown boot parameter '%s' in '%s'\n",
2557 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2558 validate_bootdevices(buf
);
2559 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2562 if (get_param_value(buf
, sizeof(buf
),
2564 validate_bootdevices(buf
);
2565 standard_boot_devices
= g_strdup(boot_devices
);
2566 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2567 qemu_register_reset(restore_boot_devices
,
2568 standard_boot_devices
);
2570 if (get_param_value(buf
, sizeof(buf
),
2572 if (!strcmp(buf
, "on")) {
2574 } else if (!strcmp(buf
, "off")) {
2578 "qemu: invalid option value '%s'\n",
2583 qemu_opts_parse(qemu_find_opts("boot-opts"),
2588 case QEMU_OPTION_fda
:
2589 case QEMU_OPTION_fdb
:
2590 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2593 case QEMU_OPTION_no_fd_bootchk
:
2596 case QEMU_OPTION_netdev
:
2597 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2601 case QEMU_OPTION_net
:
2602 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2606 #ifdef CONFIG_LIBISCSI
2607 case QEMU_OPTION_iscsi
:
2608 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2615 case QEMU_OPTION_tftp
:
2616 legacy_tftp_prefix
= optarg
;
2618 case QEMU_OPTION_bootp
:
2619 legacy_bootp_filename
= optarg
;
2621 case QEMU_OPTION_redir
:
2622 if (net_slirp_redir(optarg
) < 0)
2626 case QEMU_OPTION_bt
:
2627 add_device_config(DEV_BT
, optarg
);
2629 case QEMU_OPTION_audio_help
:
2630 if (!(audio_available())) {
2631 printf("Option %s not supported for this target\n", popt
->name
);
2637 case QEMU_OPTION_soundhw
:
2638 if (!(audio_available())) {
2639 printf("Option %s not supported for this target\n", popt
->name
);
2642 select_soundhw (optarg
);
2647 case QEMU_OPTION_version
:
2651 case QEMU_OPTION_m
: {
2655 value
= strtosz(optarg
, &end
);
2656 if (value
< 0 || *end
) {
2657 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2661 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2662 fprintf(stderr
, "qemu: ram size too large\n");
2668 case QEMU_OPTION_mempath
:
2672 case QEMU_OPTION_mem_prealloc
:
2683 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2685 case QEMU_OPTION_gdb
:
2686 add_device_config(DEV_GDB
, optarg
);
2691 case QEMU_OPTION_bios
:
2694 case QEMU_OPTION_singlestep
:
2701 keyboard_layout
= optarg
;
2703 case QEMU_OPTION_localtime
:
2706 case QEMU_OPTION_vga
:
2714 w
= strtol(p
, (char **)&p
, 10);
2717 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2723 h
= strtol(p
, (char **)&p
, 10);
2728 depth
= strtol(p
, (char **)&p
, 10);
2729 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2730 depth
!= 24 && depth
!= 32)
2732 } else if (*p
== '\0') {
2733 depth
= graphic_depth
;
2740 graphic_depth
= depth
;
2743 case QEMU_OPTION_echr
:
2746 term_escape_char
= strtol(optarg
, &r
, 0);
2748 printf("Bad argument to echr\n");
2751 case QEMU_OPTION_monitor
:
2752 monitor_parse(optarg
, "readline");
2753 default_monitor
= 0;
2755 case QEMU_OPTION_qmp
:
2756 monitor_parse(optarg
, "control");
2757 default_monitor
= 0;
2759 case QEMU_OPTION_mon
:
2760 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2764 default_monitor
= 0;
2766 case QEMU_OPTION_chardev
:
2767 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2772 case QEMU_OPTION_fsdev
:
2773 olist
= qemu_find_opts("fsdev");
2775 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2778 opts
= qemu_opts_parse(olist
, optarg
, 1);
2780 fprintf(stderr
, "parse error: %s\n", optarg
);
2784 case QEMU_OPTION_virtfs
: {
2787 const char *writeout
, *sock_fd
, *socket
;
2789 olist
= qemu_find_opts("virtfs");
2791 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2794 opts
= qemu_opts_parse(olist
, optarg
, 1);
2796 fprintf(stderr
, "parse error: %s\n", optarg
);
2800 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2801 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2802 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2805 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2806 qemu_opt_get(opts
, "mount_tag"), 1);
2808 fprintf(stderr
, "duplicate fsdev id: %s\n",
2809 qemu_opt_get(opts
, "mount_tag"));
2813 writeout
= qemu_opt_get(opts
, "writeout");
2815 #ifdef CONFIG_SYNC_FILE_RANGE
2816 qemu_opt_set(fsdev
, "writeout", writeout
);
2818 fprintf(stderr
, "writeout=immediate not supported on "
2823 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2824 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2825 qemu_opt_set(fsdev
, "security_model",
2826 qemu_opt_get(opts
, "security_model"));
2827 socket
= qemu_opt_get(opts
, "socket");
2829 qemu_opt_set(fsdev
, "socket", socket
);
2831 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2833 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2836 qemu_opt_set_bool(fsdev
, "readonly",
2837 qemu_opt_get_bool(opts
, "readonly", 0));
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_virtfs_synth
: {
2850 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2852 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2855 qemu_opt_set(fsdev
, "fsdriver", "synth");
2857 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2858 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2859 qemu_opt_set(device
, "fsdev", "v_synth");
2860 qemu_opt_set(device
, "mount_tag", "v_synth");
2863 case QEMU_OPTION_serial
:
2864 add_device_config(DEV_SERIAL
, optarg
);
2866 if (strncmp(optarg
, "mon:", 4) == 0) {
2867 default_monitor
= 0;
2870 case QEMU_OPTION_watchdog
:
2873 "qemu: only one watchdog option may be given\n");
2878 case QEMU_OPTION_watchdog_action
:
2879 if (select_watchdog_action(optarg
) == -1) {
2880 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2884 case QEMU_OPTION_virtiocon
:
2885 add_device_config(DEV_VIRTCON
, optarg
);
2886 default_virtcon
= 0;
2887 if (strncmp(optarg
, "mon:", 4) == 0) {
2888 default_monitor
= 0;
2891 case QEMU_OPTION_parallel
:
2892 add_device_config(DEV_PARALLEL
, optarg
);
2893 default_parallel
= 0;
2894 if (strncmp(optarg
, "mon:", 4) == 0) {
2895 default_monitor
= 0;
2898 case QEMU_OPTION_debugcon
:
2899 add_device_config(DEV_DEBUGCON
, optarg
);
2901 case QEMU_OPTION_loadvm
:
2904 case QEMU_OPTION_full_screen
:
2908 case QEMU_OPTION_no_frame
:
2911 case QEMU_OPTION_alt_grab
:
2914 case QEMU_OPTION_ctrl_grab
:
2917 case QEMU_OPTION_no_quit
:
2920 case QEMU_OPTION_sdl
:
2921 display_type
= DT_SDL
;
2924 case QEMU_OPTION_no_frame
:
2925 case QEMU_OPTION_alt_grab
:
2926 case QEMU_OPTION_ctrl_grab
:
2927 case QEMU_OPTION_no_quit
:
2928 case QEMU_OPTION_sdl
:
2929 fprintf(stderr
, "SDL support is disabled\n");
2932 case QEMU_OPTION_pidfile
:
2935 case QEMU_OPTION_win2k_hack
:
2936 win2k_install_hack
= 1;
2938 case QEMU_OPTION_rtc_td_hack
: {
2939 static GlobalProperty slew_lost_ticks
[] = {
2941 .driver
= "mc146818rtc",
2942 .property
= "lost_tick_policy",
2945 { /* end of list */ }
2948 qdev_prop_register_global_list(slew_lost_ticks
);
2951 case QEMU_OPTION_acpitable
:
2952 do_acpitable_option(optarg
);
2954 case QEMU_OPTION_smbios
:
2955 do_smbios_option(optarg
);
2957 case QEMU_OPTION_enable_kvm
:
2958 olist
= qemu_find_opts("machine");
2959 qemu_opts_parse(olist
, "accel=kvm", 0);
2961 case QEMU_OPTION_machine
:
2962 olist
= qemu_find_opts("machine");
2963 opts
= qemu_opts_parse(olist
, optarg
, 1);
2965 fprintf(stderr
, "parse error: %s\n", optarg
);
2968 optarg
= qemu_opt_get(opts
, "type");
2970 machine
= machine_parse(optarg
);
2973 case QEMU_OPTION_usb
:
2976 case QEMU_OPTION_usbdevice
:
2978 add_device_config(DEV_USB
, optarg
);
2980 case QEMU_OPTION_device
:
2981 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2985 case QEMU_OPTION_smp
:
2988 fprintf(stderr
, "Invalid number of CPUs\n");
2991 if (max_cpus
< smp_cpus
) {
2992 fprintf(stderr
, "maxcpus must be equal to or greater than "
2996 if (max_cpus
> 255) {
2997 fprintf(stderr
, "Unsupported number of maxcpus\n");
3001 case QEMU_OPTION_vnc
:
3004 vnc_display
= optarg
;
3006 fprintf(stderr
, "VNC support is disabled\n");
3010 case QEMU_OPTION_no_acpi
:
3013 case QEMU_OPTION_no_hpet
:
3016 case QEMU_OPTION_balloon
:
3017 if (balloon_parse(optarg
) < 0) {
3018 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3022 case QEMU_OPTION_no_reboot
:
3025 case QEMU_OPTION_no_shutdown
:
3028 case QEMU_OPTION_show_cursor
:
3031 case QEMU_OPTION_uuid
:
3032 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3033 fprintf(stderr
, "Fail to parse UUID string."
3034 " Wrong format.\n");
3038 case QEMU_OPTION_option_rom
:
3039 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3040 fprintf(stderr
, "Too many option ROMs\n");
3043 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3044 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3045 option_rom
[nb_option_roms
].bootindex
=
3046 qemu_opt_get_number(opts
, "bootindex", -1);
3047 if (!option_rom
[nb_option_roms
].name
) {
3048 fprintf(stderr
, "Option ROM file is not specified\n");
3053 case QEMU_OPTION_semihosting
:
3054 semihosting_enabled
= 1;
3056 case QEMU_OPTION_name
:
3057 qemu_name
= g_strdup(optarg
);
3059 char *p
= strchr(qemu_name
, ',');
3062 if (strncmp(p
, "process=", 8)) {
3063 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3067 os_set_proc_name(p
);
3071 case QEMU_OPTION_prom_env
:
3072 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3073 fprintf(stderr
, "Too many prom variables\n");
3076 prom_envs
[nb_prom_envs
] = optarg
;
3079 case QEMU_OPTION_old_param
:
3082 case QEMU_OPTION_clock
:
3083 configure_alarms(optarg
);
3085 case QEMU_OPTION_startdate
:
3086 configure_rtc_date_offset(optarg
, 1);
3088 case QEMU_OPTION_rtc
:
3089 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3093 configure_rtc(opts
);
3095 case QEMU_OPTION_tb_size
:
3096 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3097 if (tcg_tb_size
< 0) {
3101 case QEMU_OPTION_icount
:
3102 icount_option
= optarg
;
3104 case QEMU_OPTION_incoming
:
3106 runstate_set(RUN_STATE_INMIGRATE
);
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
);
3188 case QEMU_OPTION_qtest
:
3189 qtest_chrdev
= optarg
;
3191 case QEMU_OPTION_qtest_log
:
3195 os_parse_cmd_args(popt
->index
, optarg
);
3201 /* Init CPU def lists, based on config
3202 * - Must be called after all the qemu_read_config_file() calls
3203 * - Must be called before list_cpus()
3204 * - Must be called before machine->init()
3208 if (cpu_model
&& *cpu_model
== '?') {
3209 list_cpus(stdout
, &fprintf
, cpu_model
);
3213 /* Open the logfile at this point, if necessary. We can't open the logfile
3214 * when encountering either of the logging options (-d or -D) because the
3215 * other one may be encountered later on the command line, changing the
3216 * location or level of logging.
3220 set_cpu_log_filename(log_file
);
3222 set_cpu_log(log_mask
);
3225 if (!trace_backend_init(trace_events
, trace_file
)) {
3229 /* If no data_dir is specified then try to find it relative to the
3232 data_dir
= os_find_datadir(argv
[0]);
3234 /* If all else fails use the install path specified when building. */
3236 data_dir
= CONFIG_QEMU_DATADIR
;
3239 if (machine
== NULL
) {
3240 fprintf(stderr
, "No machine found.\n");
3245 * Default to max_cpus = smp_cpus, in case the user doesn't
3246 * specify a max_cpus value.
3249 max_cpus
= smp_cpus
;
3251 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3252 if (smp_cpus
> machine
->max_cpus
) {
3253 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3254 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3260 * Get the default machine options from the machine if it is not already
3261 * specified either by the configuration file or by the command line.
3263 if (machine
->default_machine_opts
) {
3264 qemu_opts_set_defaults(qemu_find_opts("machine"),
3265 machine
->default_machine_opts
, 0);
3268 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3269 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3271 if (machine
->no_serial
) {
3274 if (machine
->no_parallel
) {
3275 default_parallel
= 0;
3277 if (!machine
->use_virtcon
) {
3278 default_virtcon
= 0;
3280 if (machine
->no_floppy
) {
3283 if (machine
->no_cdrom
) {
3286 if (machine
->no_sdcard
) {
3290 if (display_type
== DT_NOGRAPHIC
) {
3291 if (default_parallel
)
3292 add_device_config(DEV_PARALLEL
, "null");
3293 if (default_serial
&& default_monitor
) {
3294 add_device_config(DEV_SERIAL
, "mon:stdio");
3295 } else if (default_virtcon
&& default_monitor
) {
3296 add_device_config(DEV_VIRTCON
, "mon:stdio");
3299 add_device_config(DEV_SERIAL
, "stdio");
3300 if (default_virtcon
)
3301 add_device_config(DEV_VIRTCON
, "stdio");
3302 if (default_monitor
)
3303 monitor_parse("stdio", "readline");
3307 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3308 if (default_parallel
)
3309 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3310 if (default_monitor
)
3311 monitor_parse("vc:80Cx24C", "readline");
3312 if (default_virtcon
)
3313 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3318 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3320 #ifdef CONFIG_VIRTFS
3321 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3328 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3333 /* init the memory */
3334 if (ram_size
== 0) {
3335 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3338 configure_accelerator();
3340 qemu_init_cpu_loop();
3341 if (qemu_init_main_loop()) {
3342 fprintf(stderr
, "qemu_init_main_loop failed\n");
3346 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3348 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3349 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3350 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3352 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3355 if (!kernel_cmdline
) {
3356 kernel_cmdline
= "";
3359 linux_boot
= (kernel_filename
!= NULL
);
3361 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3362 fprintf(stderr
, "-append only allowed with -kernel option\n");
3366 if (!linux_boot
&& initrd_filename
!= NULL
) {
3367 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3371 if (!linux_boot
&& machine_opts
&& qemu_opt_get(machine_opts
, "dtb")) {
3372 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
3376 os_set_line_buffering();
3378 if (init_timer_alarm() < 0) {
3379 fprintf(stderr
, "could not initialize alarm timer\n");
3384 /* spice needs the timers to be initialized by this point */
3388 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3389 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3392 configure_icount(icount_option
);
3394 if (net_init_clients() < 0) {
3398 /* init the bluetooth world */
3399 if (foreach_device_config(DEV_BT
, bt_parse
))
3402 if (!xen_enabled()) {
3403 /* On 32-bit hosts, QEMU is limited by virtual address space */
3404 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3405 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3410 cpu_exec_init_all();
3412 bdrv_init_with_whitelist();
3416 /* open the virtual block devices */
3418 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3419 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3422 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3423 IF_DEFAULT
, 2, CDROM_OPTS
);
3424 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3425 IF_FLOPPY
, 0, FD_OPTS
);
3426 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3429 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3432 if (nb_numa_nodes
> 0) {
3435 if (nb_numa_nodes
> MAX_NODES
) {
3436 nb_numa_nodes
= MAX_NODES
;
3439 /* If no memory size if given for any node, assume the default case
3440 * and distribute the available memory equally across all nodes
3442 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3443 if (node_mem
[i
] != 0)
3446 if (i
== nb_numa_nodes
) {
3447 uint64_t usedmem
= 0;
3449 /* On Linux, the each node's border has to be 8MB aligned,
3450 * the final node gets the rest.
3452 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3453 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3454 usedmem
+= node_mem
[i
];
3456 node_mem
[i
] = ram_size
- usedmem
;
3459 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3460 if (node_cpumask
[i
] != 0)
3463 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3464 * must cope with this anyway, because there are BIOSes out there in
3465 * real machines which also use this scheme.
3467 if (i
== nb_numa_nodes
) {
3468 for (i
= 0; i
< max_cpus
; i
++) {
3469 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3474 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3478 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3480 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3482 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3484 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3487 /* must be after qdev registration but before machine init */
3489 select_vgahw(vga_model
);
3490 } else if (cirrus_vga_available()) {
3491 select_vgahw("cirrus");
3493 select_vgahw("none");
3496 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3500 i
= select_watchdog(watchdog
);
3502 exit (i
== 1 ? 1 : 0);
3505 if (machine
->compat_props
) {
3506 qdev_prop_register_global_list(machine
->compat_props
);
3510 qdev_machine_init();
3512 machine
->init(ram_size
, boot_devices
,
3513 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3515 cpu_synchronize_all_post_init();
3519 current_machine
= machine
;
3521 /* init USB devices */
3523 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3527 /* init generic devices */
3528 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3531 net_check_clients();
3533 /* just use the first displaystate for the moment */
3534 ds
= get_displaystate();
3538 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3539 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3540 display_type
= DT_SDL
;
3541 #elif defined(CONFIG_VNC)
3542 vnc_display
= "localhost:0,to=99";
3545 display_type
= DT_NONE
;
3550 /* init local displays */
3551 switch (display_type
) {
3554 #if defined(CONFIG_CURSES)
3556 curses_display_init(ds
, full_screen
);
3559 #if defined(CONFIG_SDL)
3561 sdl_display_init(ds
, full_screen
, no_frame
);
3563 #elif defined(CONFIG_COCOA)
3565 cocoa_display_init(ds
, full_screen
);
3572 /* must be after terminal init, SDL library changes signal handlers */
3573 os_setup_signal_handling();
3576 /* init remote displays */
3578 vnc_display_init(ds
);
3579 if (vnc_display_open(ds
, vnc_display
) < 0)
3582 if (show_vnc_port
) {
3583 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3588 if (using_spice
&& !qxl_enabled
) {
3589 qemu_spice_display_init(ds
);
3595 dcl
= ds
->listeners
;
3596 while (dcl
!= NULL
) {
3597 if (dcl
->dpy_refresh
!= NULL
) {
3598 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3599 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3604 text_consoles_set_display(ds
);
3606 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3610 qdev_machine_creation_done();
3612 if (rom_load_all() != 0) {
3613 fprintf(stderr
, "rom loading failed\n");
3617 /* TODO: once all bus devices are qdevified, this should be done
3618 * when bus is created by qdev.c */
3619 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3620 qemu_run_machine_init_done_notifiers();
3622 qemu_system_reset(VMRESET_SILENT
);
3624 if (load_vmstate(loadvm
) < 0) {
3630 int ret
= qemu_start_incoming_migration(incoming
);
3632 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3636 } else if (autostart
) {