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 int suspend_requested
;
1287 static bool is_suspended
;
1288 static NotifierList suspend_notifiers
=
1289 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1290 static NotifierList wakeup_notifiers
=
1291 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1292 static uint32_t wakeup_reason_mask
= ~0;
1293 static RunState vmstop_requested
= RUN_STATE_MAX
;
1295 int qemu_shutdown_requested_get(void)
1297 return shutdown_requested
;
1300 int qemu_reset_requested_get(void)
1302 return reset_requested
;
1305 int qemu_shutdown_requested(void)
1307 int r
= shutdown_requested
;
1308 shutdown_requested
= 0;
1312 void qemu_kill_report(void)
1314 if (shutdown_signal
!= -1) {
1315 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1316 if (shutdown_pid
== 0) {
1317 /* This happens for eg ^C at the terminal, so it's worth
1318 * avoiding printing an odd message in that case.
1320 fputc('\n', stderr
);
1322 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1324 shutdown_signal
= -1;
1328 int qemu_reset_requested(void)
1330 int r
= reset_requested
;
1331 reset_requested
= 0;
1335 static int qemu_suspend_requested(void)
1337 int r
= suspend_requested
;
1338 suspend_requested
= 0;
1342 int qemu_powerdown_requested(void)
1344 int r
= powerdown_requested
;
1345 powerdown_requested
= 0;
1349 static int qemu_debug_requested(void)
1351 int r
= debug_requested
;
1352 debug_requested
= 0;
1356 /* We use RUN_STATE_MAX but any invalid value will do */
1357 static bool qemu_vmstop_requested(RunState
*r
)
1359 if (vmstop_requested
< RUN_STATE_MAX
) {
1360 *r
= vmstop_requested
;
1361 vmstop_requested
= RUN_STATE_MAX
;
1368 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1370 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1373 re
->opaque
= opaque
;
1374 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1377 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1381 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1382 if (re
->func
== func
&& re
->opaque
== opaque
) {
1383 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1390 void qemu_system_reset(bool report
)
1392 QEMUResetEntry
*re
, *nre
;
1394 /* reset all devices */
1395 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1396 re
->func(re
->opaque
);
1399 monitor_protocol_event(QEVENT_RESET
, NULL
);
1401 cpu_synchronize_all_post_reset();
1404 void qemu_system_reset_request(void)
1407 shutdown_requested
= 1;
1409 reset_requested
= 1;
1412 qemu_notify_event();
1415 static void qemu_system_suspend(void)
1418 notifier_list_notify(&suspend_notifiers
, NULL
);
1419 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1420 is_suspended
= true;
1423 void qemu_system_suspend_request(void)
1428 suspend_requested
= 1;
1430 qemu_notify_event();
1433 void qemu_register_suspend_notifier(Notifier
*notifier
)
1435 notifier_list_add(&suspend_notifiers
, notifier
);
1438 void qemu_system_wakeup_request(WakeupReason reason
)
1440 if (!is_suspended
) {
1443 if (!(wakeup_reason_mask
& (1 << reason
))) {
1446 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1447 notifier_list_notify(&wakeup_notifiers
, &reason
);
1448 reset_requested
= 1;
1449 qemu_notify_event();
1450 is_suspended
= false;
1453 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1456 wakeup_reason_mask
|= (1 << reason
);
1458 wakeup_reason_mask
&= ~(1 << reason
);
1462 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1464 notifier_list_add(&wakeup_notifiers
, notifier
);
1467 void qemu_system_killed(int signal
, pid_t pid
)
1469 shutdown_signal
= signal
;
1472 qemu_system_shutdown_request();
1475 void qemu_system_shutdown_request(void)
1477 shutdown_requested
= 1;
1478 qemu_notify_event();
1481 void qemu_system_powerdown_request(void)
1483 powerdown_requested
= 1;
1484 qemu_notify_event();
1487 void qemu_system_debug_request(void)
1489 debug_requested
= 1;
1490 qemu_notify_event();
1493 void qemu_system_vmstop_request(RunState state
)
1495 vmstop_requested
= state
;
1496 qemu_notify_event();
1499 qemu_irq qemu_system_powerdown
;
1501 static bool main_loop_should_exit(void)
1504 if (qemu_debug_requested()) {
1505 vm_stop(RUN_STATE_DEBUG
);
1507 if (qemu_suspend_requested()) {
1508 qemu_system_suspend();
1510 if (qemu_shutdown_requested()) {
1512 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1514 vm_stop(RUN_STATE_SHUTDOWN
);
1519 if (qemu_reset_requested()) {
1521 cpu_synchronize_all_states();
1522 qemu_system_reset(VMRESET_REPORT
);
1524 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1525 runstate_check(RUN_STATE_SHUTDOWN
)) {
1526 runstate_set(RUN_STATE_PAUSED
);
1529 if (qemu_powerdown_requested()) {
1530 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1531 qemu_irq_raise(qemu_system_powerdown
);
1533 if (qemu_vmstop_requested(&r
)) {
1539 static void main_loop(void)
1543 #ifdef CONFIG_PROFILER
1547 nonblocking
= !kvm_enabled() && last_io
> 0;
1548 #ifdef CONFIG_PROFILER
1549 ti
= profile_getclock();
1551 last_io
= main_loop_wait(nonblocking
);
1552 #ifdef CONFIG_PROFILER
1553 dev_time
+= profile_getclock() - ti
;
1555 } while (!main_loop_should_exit());
1558 static void version(void)
1560 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1563 static void help(int exitcode
)
1566 printf("usage: %s [options] [disk_image]\n\n"
1567 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1568 error_get_progname());
1570 #define QEMU_OPTIONS_GENERATE_HELP
1571 #include "qemu-options-wrapper.h"
1573 printf("\nDuring emulation, the following keys are useful:\n"
1574 "ctrl-alt-f toggle full screen\n"
1575 "ctrl-alt-n switch to virtual console 'n'\n"
1576 "ctrl-alt toggle mouse and keyboard grab\n"
1578 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1583 #define HAS_ARG 0x0001
1585 typedef struct QEMUOption
{
1592 static const QEMUOption qemu_options
[] = {
1593 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1594 #define QEMU_OPTIONS_GENERATE_OPTIONS
1595 #include "qemu-options-wrapper.h"
1599 static bool vga_available(void)
1601 return qdev_exists("VGA") || qdev_exists("isa-vga");
1604 static bool cirrus_vga_available(void)
1606 return qdev_exists("cirrus-vga") || qdev_exists("isa-cirrus-vga");
1609 static bool vmware_vga_available(void)
1611 return qdev_exists("vmware-svga");
1614 static void select_vgahw (const char *p
)
1618 vga_interface_type
= VGA_NONE
;
1619 if (strstart(p
, "std", &opts
)) {
1620 if (vga_available()) {
1621 vga_interface_type
= VGA_STD
;
1623 fprintf(stderr
, "Error: standard VGA not available\n");
1626 } else if (strstart(p
, "cirrus", &opts
)) {
1627 if (cirrus_vga_available()) {
1628 vga_interface_type
= VGA_CIRRUS
;
1630 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1633 } else if (strstart(p
, "vmware", &opts
)) {
1634 if (vmware_vga_available()) {
1635 vga_interface_type
= VGA_VMWARE
;
1637 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1640 } else if (strstart(p
, "xenfb", &opts
)) {
1641 vga_interface_type
= VGA_XENFB
;
1642 } else if (strstart(p
, "qxl", &opts
)) {
1643 vga_interface_type
= VGA_QXL
;
1644 } else if (!strstart(p
, "none", &opts
)) {
1646 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1650 const char *nextopt
;
1652 if (strstart(opts
, ",retrace=", &nextopt
)) {
1654 if (strstart(opts
, "dumb", &nextopt
))
1655 vga_retrace_method
= VGA_RETRACE_DUMB
;
1656 else if (strstart(opts
, "precise", &nextopt
))
1657 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1658 else goto invalid_vga
;
1659 } else goto invalid_vga
;
1664 static DisplayType
select_display(const char *p
)
1667 DisplayType display
= DT_DEFAULT
;
1669 if (strstart(p
, "sdl", &opts
)) {
1673 const char *nextopt
;
1675 if (strstart(opts
, ",frame=", &nextopt
)) {
1677 if (strstart(opts
, "on", &nextopt
)) {
1679 } else if (strstart(opts
, "off", &nextopt
)) {
1682 goto invalid_sdl_args
;
1684 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1686 if (strstart(opts
, "on", &nextopt
)) {
1688 } else if (strstart(opts
, "off", &nextopt
)) {
1691 goto invalid_sdl_args
;
1693 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1695 if (strstart(opts
, "on", &nextopt
)) {
1697 } else if (strstart(opts
, "off", &nextopt
)) {
1700 goto invalid_sdl_args
;
1702 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1704 if (strstart(opts
, "on", &nextopt
)) {
1706 } else if (strstart(opts
, "off", &nextopt
)) {
1709 goto invalid_sdl_args
;
1713 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1719 fprintf(stderr
, "SDL support is disabled\n");
1722 } else if (strstart(p
, "vnc", &opts
)) {
1727 const char *nextopt
;
1729 if (strstart(opts
, "=", &nextopt
)) {
1730 vnc_display
= nextopt
;
1734 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1738 fprintf(stderr
, "VNC support is disabled\n");
1741 } else if (strstart(p
, "curses", &opts
)) {
1742 #ifdef CONFIG_CURSES
1743 display
= DT_CURSES
;
1745 fprintf(stderr
, "Curses support is disabled\n");
1748 } else if (strstart(p
, "none", &opts
)) {
1751 fprintf(stderr
, "Unknown display type: %s\n", p
);
1758 static int balloon_parse(const char *arg
)
1762 if (strcmp(arg
, "none") == 0) {
1766 if (!strncmp(arg
, "virtio", 6)) {
1767 if (arg
[6] == ',') {
1768 /* have params -> parse them */
1769 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1773 /* create empty opts */
1774 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1776 qemu_opt_set(opts
, "driver", "virtio-balloon");
1783 char *qemu_find_file(int type
, const char *name
)
1789 /* If name contains path separators then try it as a straight path. */
1790 if ((strchr(name
, '/') || strchr(name
, '\\'))
1791 && access(name
, R_OK
) == 0) {
1792 return g_strdup(name
);
1795 case QEMU_FILE_TYPE_BIOS
:
1798 case QEMU_FILE_TYPE_KEYMAP
:
1799 subdir
= "keymaps/";
1804 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1805 buf
= g_malloc0(len
);
1806 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1807 if (access(buf
, R_OK
)) {
1814 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1816 return qdev_device_help(opts
);
1819 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1823 dev
= qdev_device_add(opts
);
1829 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1831 CharDriverState
*chr
;
1833 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1839 #ifdef CONFIG_VIRTFS
1840 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1843 ret
= qemu_fsdev_add(opts
);
1849 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1851 CharDriverState
*chr
;
1852 const char *chardev
;
1856 mode
= qemu_opt_get(opts
, "mode");
1860 if (strcmp(mode
, "readline") == 0) {
1861 flags
= MONITOR_USE_READLINE
;
1862 } else if (strcmp(mode
, "control") == 0) {
1863 flags
= MONITOR_USE_CONTROL
;
1865 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1869 if (qemu_opt_get_bool(opts
, "pretty", 0))
1870 flags
|= MONITOR_USE_PRETTY
;
1872 if (qemu_opt_get_bool(opts
, "default", 0))
1873 flags
|= MONITOR_IS_DEFAULT
;
1875 chardev
= qemu_opt_get(opts
, "chardev");
1876 chr
= qemu_chr_find(chardev
);
1878 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1882 monitor_init(chr
, flags
);
1886 static void monitor_parse(const char *optarg
, const char *mode
)
1888 static int monitor_device_index
= 0;
1894 if (strstart(optarg
, "chardev:", &p
)) {
1895 snprintf(label
, sizeof(label
), "%s", p
);
1897 snprintf(label
, sizeof(label
), "compat_monitor%d",
1898 monitor_device_index
);
1899 if (monitor_device_index
== 0) {
1902 opts
= qemu_chr_parse_compat(label
, optarg
);
1904 fprintf(stderr
, "parse error: %s\n", optarg
);
1909 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1911 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1914 qemu_opt_set(opts
, "mode", mode
);
1915 qemu_opt_set(opts
, "chardev", label
);
1917 qemu_opt_set(opts
, "default", "on");
1918 monitor_device_index
++;
1921 struct device_config
{
1923 DEV_USB
, /* -usbdevice */
1925 DEV_SERIAL
, /* -serial */
1926 DEV_PARALLEL
, /* -parallel */
1927 DEV_VIRTCON
, /* -virtioconsole */
1928 DEV_DEBUGCON
, /* -debugcon */
1929 DEV_GDB
, /* -gdb, -s */
1931 const char *cmdline
;
1933 QTAILQ_ENTRY(device_config
) next
;
1935 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1937 static void add_device_config(int type
, const char *cmdline
)
1939 struct device_config
*conf
;
1941 conf
= g_malloc0(sizeof(*conf
));
1943 conf
->cmdline
= cmdline
;
1944 loc_save(&conf
->loc
);
1945 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1948 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1950 struct device_config
*conf
;
1953 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1954 if (conf
->type
!= type
)
1956 loc_push_restore(&conf
->loc
);
1957 rc
= func(conf
->cmdline
);
1958 loc_pop(&conf
->loc
);
1965 static int serial_parse(const char *devname
)
1967 static int index
= 0;
1970 if (strcmp(devname
, "none") == 0)
1972 if (index
== MAX_SERIAL_PORTS
) {
1973 fprintf(stderr
, "qemu: too many serial ports\n");
1976 snprintf(label
, sizeof(label
), "serial%d", index
);
1977 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1978 if (!serial_hds
[index
]) {
1979 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
1980 devname
, strerror(errno
));
1987 static int parallel_parse(const char *devname
)
1989 static int index
= 0;
1992 if (strcmp(devname
, "none") == 0)
1994 if (index
== MAX_PARALLEL_PORTS
) {
1995 fprintf(stderr
, "qemu: too many parallel ports\n");
1998 snprintf(label
, sizeof(label
), "parallel%d", index
);
1999 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2000 if (!parallel_hds
[index
]) {
2001 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2002 devname
, strerror(errno
));
2009 static int virtcon_parse(const char *devname
)
2011 QemuOptsList
*device
= qemu_find_opts("device");
2012 static int index
= 0;
2014 QemuOpts
*bus_opts
, *dev_opts
;
2016 if (strcmp(devname
, "none") == 0)
2018 if (index
== MAX_VIRTIO_CONSOLES
) {
2019 fprintf(stderr
, "qemu: too many virtio consoles\n");
2023 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2024 if (arch_type
== QEMU_ARCH_S390X
) {
2025 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2027 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2030 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2031 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2033 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2034 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2035 if (!virtcon_hds
[index
]) {
2036 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2037 devname
, strerror(errno
));
2040 qemu_opt_set(dev_opts
, "chardev", label
);
2046 static int debugcon_parse(const char *devname
)
2050 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2053 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2055 fprintf(stderr
, "qemu: already have a debugcon device\n");
2058 qemu_opt_set(opts
, "driver", "isa-debugcon");
2059 qemu_opt_set(opts
, "chardev", "debugcon");
2063 static QEMUMachine
*machine_parse(const char *name
)
2065 QEMUMachine
*m
, *machine
= NULL
;
2068 machine
= find_machine(name
);
2073 printf("Supported machines are:\n");
2074 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2076 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2078 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2079 m
->is_default
? " (default)" : "");
2081 exit(!name
|| *name
!= '?');
2084 static int tcg_init(void)
2086 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2091 const char *opt_name
;
2093 int (*available
)(void);
2097 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2098 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2099 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2102 static int configure_accelerator(void)
2104 const char *p
= NULL
;
2107 bool accel_initialised
= 0;
2108 bool init_failed
= 0;
2110 QemuOptsList
*list
= qemu_find_opts("machine");
2111 if (!QTAILQ_EMPTY(&list
->head
)) {
2112 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2116 /* Use the default "accelerator", tcg */
2120 while (!accel_initialised
&& *p
!= '\0') {
2124 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2125 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2126 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2127 *(accel_list
[i
].allowed
) = 1;
2128 ret
= accel_list
[i
].init();
2131 if (!accel_list
[i
].available()) {
2132 printf("%s not supported for this target\n",
2133 accel_list
[i
].name
);
2135 fprintf(stderr
, "failed to initialize %s: %s\n",
2139 *(accel_list
[i
].allowed
) = 0;
2141 accel_initialised
= 1;
2146 if (i
== ARRAY_SIZE(accel_list
)) {
2147 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2151 if (!accel_initialised
) {
2152 fprintf(stderr
, "No accelerator found!\n");
2157 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2160 return !accel_initialised
;
2163 void qemu_add_exit_notifier(Notifier
*notify
)
2165 notifier_list_add(&exit_notifiers
, notify
);
2168 void qemu_remove_exit_notifier(Notifier
*notify
)
2170 notifier_remove(notify
);
2173 static void qemu_run_exit_notifiers(void)
2175 notifier_list_notify(&exit_notifiers
, NULL
);
2178 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2180 notifier_list_add(&machine_init_done_notifiers
, notify
);
2183 static void qemu_run_machine_init_done_notifiers(void)
2185 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2188 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2189 const char **poptarg
, int *poptind
)
2191 const QEMUOption
*popt
;
2192 int optind
= *poptind
;
2193 char *r
= argv
[optind
];
2196 loc_set_cmdline(argv
, optind
, 1);
2198 /* Treat --foo the same as -foo. */
2201 popt
= qemu_options
;
2204 error_report("invalid option");
2207 if (!strcmp(popt
->name
, r
+ 1))
2211 if (popt
->flags
& HAS_ARG
) {
2212 if (optind
>= argc
) {
2213 error_report("requires an argument");
2216 optarg
= argv
[optind
++];
2217 loc_set_cmdline(argv
, optind
- 2, 2);
2228 static gpointer
malloc_and_trace(gsize n_bytes
)
2230 void *ptr
= malloc(n_bytes
);
2231 trace_g_malloc(n_bytes
, ptr
);
2235 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2237 void *ptr
= realloc(mem
, n_bytes
);
2238 trace_g_realloc(mem
, n_bytes
, ptr
);
2242 static void free_and_trace(gpointer mem
)
2248 int qemu_init_main_loop(void)
2250 return main_loop_init();
2253 int main(int argc
, char **argv
, char **envp
)
2256 int snapshot
, linux_boot
;
2257 const char *icount_option
= NULL
;
2258 const char *initrd_filename
;
2259 const char *kernel_filename
, *kernel_cmdline
;
2260 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2262 DisplayChangeListener
*dcl
;
2263 int cyls
, heads
, secs
, translation
;
2264 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2265 QemuOptsList
*olist
;
2268 const char *loadvm
= NULL
;
2269 QEMUMachine
*machine
;
2270 const char *cpu_model
;
2271 const char *vga_model
= NULL
;
2272 const char *pid_file
= NULL
;
2273 const char *incoming
= NULL
;
2275 int show_vnc_port
= 0;
2278 const char *log_mask
= NULL
;
2279 const char *log_file
= NULL
;
2280 GMemVTable mem_trace
= {
2281 .malloc
= malloc_and_trace
,
2282 .realloc
= realloc_and_trace
,
2283 .free
= free_and_trace
,
2285 const char *trace_events
= NULL
;
2286 const char *trace_file
= NULL
;
2288 atexit(qemu_run_exit_notifiers
);
2289 error_set_progname(argv
[0]);
2291 g_mem_set_vtable(&mem_trace
);
2292 if (!g_thread_supported()) {
2293 #if !GLIB_CHECK_VERSION(2, 31, 0)
2294 g_thread_init(NULL
);
2296 fprintf(stderr
, "glib threading failed to initialize.\n");
2304 rtc_clock
= host_clock
;
2306 qemu_cache_utils_init(envp
);
2308 QLIST_INIT (&vm_change_state_head
);
2309 os_setup_early_signal_handling();
2311 module_call_init(MODULE_INIT_MACHINE
);
2312 machine
= find_default_machine();
2316 cyls
= heads
= secs
= 0;
2317 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2319 for (i
= 0; i
< MAX_NODES
; i
++) {
2321 node_cpumask
[i
] = 0;
2329 /* first pass of option parsing */
2331 while (optind
< argc
) {
2332 if (argv
[optind
][0] != '-') {
2337 const QEMUOption
*popt
;
2339 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2340 switch (popt
->index
) {
2341 case QEMU_OPTION_nodefconfig
:
2351 ret
= qemu_read_config_file(CONFIG_QEMU_CONFDIR
"/qemu.conf");
2352 if (ret
< 0 && ret
!= -ENOENT
) {
2356 ret
= qemu_read_config_file(arch_config_name
);
2357 if (ret
< 0 && ret
!= -ENOENT
) {
2363 /* second pass of option parsing */
2368 if (argv
[optind
][0] != '-') {
2369 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2371 const QEMUOption
*popt
;
2373 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2374 if (!(popt
->arch_mask
& arch_type
)) {
2375 printf("Option %s not supported for this target\n", popt
->name
);
2378 switch(popt
->index
) {
2380 machine
= machine_parse(optarg
);
2382 case QEMU_OPTION_cpu
:
2383 /* hw initialization will check this */
2384 if (*optarg
== '?') {
2385 list_cpus(stdout
, &fprintf
, optarg
);
2391 case QEMU_OPTION_hda
:
2395 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2397 snprintf(buf
, sizeof(buf
),
2398 "%s,cyls=%d,heads=%d,secs=%d%s",
2399 HD_OPTS
, cyls
, heads
, secs
,
2400 translation
== BIOS_ATA_TRANSLATION_LBA
?
2402 translation
== BIOS_ATA_TRANSLATION_NONE
?
2403 ",trans=none" : "");
2404 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2407 case QEMU_OPTION_hdb
:
2408 case QEMU_OPTION_hdc
:
2409 case QEMU_OPTION_hdd
:
2410 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2413 case QEMU_OPTION_drive
:
2414 if (drive_def(optarg
) == NULL
) {
2418 case QEMU_OPTION_set
:
2419 if (qemu_set_option(optarg
) != 0)
2422 case QEMU_OPTION_global
:
2423 if (qemu_global_option(optarg
) != 0)
2426 case QEMU_OPTION_mtdblock
:
2427 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2429 case QEMU_OPTION_sd
:
2430 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2432 case QEMU_OPTION_pflash
:
2433 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2435 case QEMU_OPTION_snapshot
:
2438 case QEMU_OPTION_hdachs
:
2442 cyls
= strtol(p
, (char **)&p
, 0);
2443 if (cyls
< 1 || cyls
> 16383)
2448 heads
= strtol(p
, (char **)&p
, 0);
2449 if (heads
< 1 || heads
> 16)
2454 secs
= strtol(p
, (char **)&p
, 0);
2455 if (secs
< 1 || secs
> 63)
2459 if (!strcmp(p
, "none"))
2460 translation
= BIOS_ATA_TRANSLATION_NONE
;
2461 else if (!strcmp(p
, "lba"))
2462 translation
= BIOS_ATA_TRANSLATION_LBA
;
2463 else if (!strcmp(p
, "auto"))
2464 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2467 } else if (*p
!= '\0') {
2469 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2472 if (hda_opts
!= NULL
) {
2474 snprintf(num
, sizeof(num
), "%d", cyls
);
2475 qemu_opt_set(hda_opts
, "cyls", num
);
2476 snprintf(num
, sizeof(num
), "%d", heads
);
2477 qemu_opt_set(hda_opts
, "heads", num
);
2478 snprintf(num
, sizeof(num
), "%d", secs
);
2479 qemu_opt_set(hda_opts
, "secs", num
);
2480 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2481 qemu_opt_set(hda_opts
, "trans", "lba");
2482 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2483 qemu_opt_set(hda_opts
, "trans", "none");
2487 case QEMU_OPTION_numa
:
2488 if (nb_numa_nodes
>= MAX_NODES
) {
2489 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2494 case QEMU_OPTION_display
:
2495 display_type
= select_display(optarg
);
2497 case QEMU_OPTION_nographic
:
2498 display_type
= DT_NOGRAPHIC
;
2500 case QEMU_OPTION_curses
:
2501 #ifdef CONFIG_CURSES
2502 display_type
= DT_CURSES
;
2504 fprintf(stderr
, "Curses support is disabled\n");
2508 case QEMU_OPTION_portrait
:
2509 graphic_rotate
= 90;
2511 case QEMU_OPTION_rotate
:
2512 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2513 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2514 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2516 "qemu: only 90, 180, 270 deg rotation is available\n");
2520 case QEMU_OPTION_kernel
:
2521 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2523 case QEMU_OPTION_initrd
:
2524 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2526 case QEMU_OPTION_append
:
2527 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2529 case QEMU_OPTION_cdrom
:
2530 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2532 case QEMU_OPTION_boot
:
2534 static const char * const params
[] = {
2535 "order", "once", "menu",
2536 "splash", "splash-time", NULL
2538 char buf
[sizeof(boot_devices
)];
2539 char *standard_boot_devices
;
2542 if (!strchr(optarg
, '=')) {
2544 pstrcpy(buf
, sizeof(buf
), optarg
);
2545 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2547 "qemu: unknown boot parameter '%s' in '%s'\n",
2553 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2554 validate_bootdevices(buf
);
2555 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2558 if (get_param_value(buf
, sizeof(buf
),
2560 validate_bootdevices(buf
);
2561 standard_boot_devices
= g_strdup(boot_devices
);
2562 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2563 qemu_register_reset(restore_boot_devices
,
2564 standard_boot_devices
);
2566 if (get_param_value(buf
, sizeof(buf
),
2568 if (!strcmp(buf
, "on")) {
2570 } else if (!strcmp(buf
, "off")) {
2574 "qemu: invalid option value '%s'\n",
2579 qemu_opts_parse(qemu_find_opts("boot-opts"),
2584 case QEMU_OPTION_fda
:
2585 case QEMU_OPTION_fdb
:
2586 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2589 case QEMU_OPTION_no_fd_bootchk
:
2592 case QEMU_OPTION_netdev
:
2593 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2597 case QEMU_OPTION_net
:
2598 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2602 #ifdef CONFIG_LIBISCSI
2603 case QEMU_OPTION_iscsi
:
2604 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2611 case QEMU_OPTION_tftp
:
2612 legacy_tftp_prefix
= optarg
;
2614 case QEMU_OPTION_bootp
:
2615 legacy_bootp_filename
= optarg
;
2617 case QEMU_OPTION_redir
:
2618 if (net_slirp_redir(optarg
) < 0)
2622 case QEMU_OPTION_bt
:
2623 add_device_config(DEV_BT
, optarg
);
2625 case QEMU_OPTION_audio_help
:
2626 if (!(audio_available())) {
2627 printf("Option %s not supported for this target\n", popt
->name
);
2633 case QEMU_OPTION_soundhw
:
2634 if (!(audio_available())) {
2635 printf("Option %s not supported for this target\n", popt
->name
);
2638 select_soundhw (optarg
);
2643 case QEMU_OPTION_version
:
2647 case QEMU_OPTION_m
: {
2651 value
= strtosz(optarg
, &end
);
2652 if (value
< 0 || *end
) {
2653 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2657 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2658 fprintf(stderr
, "qemu: ram size too large\n");
2664 case QEMU_OPTION_mempath
:
2668 case QEMU_OPTION_mem_prealloc
:
2679 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2681 case QEMU_OPTION_gdb
:
2682 add_device_config(DEV_GDB
, optarg
);
2687 case QEMU_OPTION_bios
:
2690 case QEMU_OPTION_singlestep
:
2697 keyboard_layout
= optarg
;
2699 case QEMU_OPTION_localtime
:
2702 case QEMU_OPTION_vga
:
2710 w
= strtol(p
, (char **)&p
, 10);
2713 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2719 h
= strtol(p
, (char **)&p
, 10);
2724 depth
= strtol(p
, (char **)&p
, 10);
2725 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2726 depth
!= 24 && depth
!= 32)
2728 } else if (*p
== '\0') {
2729 depth
= graphic_depth
;
2736 graphic_depth
= depth
;
2739 case QEMU_OPTION_echr
:
2742 term_escape_char
= strtol(optarg
, &r
, 0);
2744 printf("Bad argument to echr\n");
2747 case QEMU_OPTION_monitor
:
2748 monitor_parse(optarg
, "readline");
2749 default_monitor
= 0;
2751 case QEMU_OPTION_qmp
:
2752 monitor_parse(optarg
, "control");
2753 default_monitor
= 0;
2755 case QEMU_OPTION_mon
:
2756 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2760 default_monitor
= 0;
2762 case QEMU_OPTION_chardev
:
2763 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2768 case QEMU_OPTION_fsdev
:
2769 olist
= qemu_find_opts("fsdev");
2771 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2774 opts
= qemu_opts_parse(olist
, optarg
, 1);
2776 fprintf(stderr
, "parse error: %s\n", optarg
);
2780 case QEMU_OPTION_virtfs
: {
2783 const char *writeout
, *sock_fd
, *socket
;
2785 olist
= qemu_find_opts("virtfs");
2787 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2790 opts
= qemu_opts_parse(olist
, optarg
, 1);
2792 fprintf(stderr
, "parse error: %s\n", optarg
);
2796 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
2797 qemu_opt_get(opts
, "mount_tag") == NULL
) {
2798 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
2801 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2802 qemu_opt_get(opts
, "mount_tag"), 1);
2804 fprintf(stderr
, "duplicate fsdev id: %s\n",
2805 qemu_opt_get(opts
, "mount_tag"));
2809 writeout
= qemu_opt_get(opts
, "writeout");
2811 #ifdef CONFIG_SYNC_FILE_RANGE
2812 qemu_opt_set(fsdev
, "writeout", writeout
);
2814 fprintf(stderr
, "writeout=immediate not supported on "
2819 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
2820 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2821 qemu_opt_set(fsdev
, "security_model",
2822 qemu_opt_get(opts
, "security_model"));
2823 socket
= qemu_opt_get(opts
, "socket");
2825 qemu_opt_set(fsdev
, "socket", socket
);
2827 sock_fd
= qemu_opt_get(opts
, "sock_fd");
2829 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
2832 qemu_opt_set_bool(fsdev
, "readonly",
2833 qemu_opt_get_bool(opts
, "readonly", 0));
2834 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2835 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2836 qemu_opt_set(device
, "fsdev",
2837 qemu_opt_get(opts
, "mount_tag"));
2838 qemu_opt_set(device
, "mount_tag",
2839 qemu_opt_get(opts
, "mount_tag"));
2842 case QEMU_OPTION_virtfs_synth
: {
2846 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth", 1);
2848 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
2851 qemu_opt_set(fsdev
, "fsdriver", "synth");
2853 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2854 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2855 qemu_opt_set(device
, "fsdev", "v_synth");
2856 qemu_opt_set(device
, "mount_tag", "v_synth");
2859 case QEMU_OPTION_serial
:
2860 add_device_config(DEV_SERIAL
, optarg
);
2862 if (strncmp(optarg
, "mon:", 4) == 0) {
2863 default_monitor
= 0;
2866 case QEMU_OPTION_watchdog
:
2869 "qemu: only one watchdog option may be given\n");
2874 case QEMU_OPTION_watchdog_action
:
2875 if (select_watchdog_action(optarg
) == -1) {
2876 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2880 case QEMU_OPTION_virtiocon
:
2881 add_device_config(DEV_VIRTCON
, optarg
);
2882 default_virtcon
= 0;
2883 if (strncmp(optarg
, "mon:", 4) == 0) {
2884 default_monitor
= 0;
2887 case QEMU_OPTION_parallel
:
2888 add_device_config(DEV_PARALLEL
, optarg
);
2889 default_parallel
= 0;
2890 if (strncmp(optarg
, "mon:", 4) == 0) {
2891 default_monitor
= 0;
2894 case QEMU_OPTION_debugcon
:
2895 add_device_config(DEV_DEBUGCON
, optarg
);
2897 case QEMU_OPTION_loadvm
:
2900 case QEMU_OPTION_full_screen
:
2904 case QEMU_OPTION_no_frame
:
2907 case QEMU_OPTION_alt_grab
:
2910 case QEMU_OPTION_ctrl_grab
:
2913 case QEMU_OPTION_no_quit
:
2916 case QEMU_OPTION_sdl
:
2917 display_type
= DT_SDL
;
2920 case QEMU_OPTION_no_frame
:
2921 case QEMU_OPTION_alt_grab
:
2922 case QEMU_OPTION_ctrl_grab
:
2923 case QEMU_OPTION_no_quit
:
2924 case QEMU_OPTION_sdl
:
2925 fprintf(stderr
, "SDL support is disabled\n");
2928 case QEMU_OPTION_pidfile
:
2931 case QEMU_OPTION_win2k_hack
:
2932 win2k_install_hack
= 1;
2934 case QEMU_OPTION_rtc_td_hack
: {
2935 static GlobalProperty slew_lost_ticks
[] = {
2937 .driver
= "mc146818rtc",
2938 .property
= "lost_tick_policy",
2941 { /* end of list */ }
2944 qdev_prop_register_global_list(slew_lost_ticks
);
2947 case QEMU_OPTION_acpitable
:
2948 do_acpitable_option(optarg
);
2950 case QEMU_OPTION_smbios
:
2951 do_smbios_option(optarg
);
2953 case QEMU_OPTION_enable_kvm
:
2954 olist
= qemu_find_opts("machine");
2955 qemu_opts_parse(olist
, "accel=kvm", 0);
2957 case QEMU_OPTION_machine
:
2958 olist
= qemu_find_opts("machine");
2959 opts
= qemu_opts_parse(olist
, optarg
, 1);
2961 fprintf(stderr
, "parse error: %s\n", optarg
);
2964 optarg
= qemu_opt_get(opts
, "type");
2966 machine
= machine_parse(optarg
);
2969 case QEMU_OPTION_usb
:
2972 case QEMU_OPTION_usbdevice
:
2974 add_device_config(DEV_USB
, optarg
);
2976 case QEMU_OPTION_device
:
2977 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2981 case QEMU_OPTION_smp
:
2984 fprintf(stderr
, "Invalid number of CPUs\n");
2987 if (max_cpus
< smp_cpus
) {
2988 fprintf(stderr
, "maxcpus must be equal to or greater than "
2992 if (max_cpus
> 255) {
2993 fprintf(stderr
, "Unsupported number of maxcpus\n");
2997 case QEMU_OPTION_vnc
:
3000 vnc_display
= optarg
;
3002 fprintf(stderr
, "VNC support is disabled\n");
3006 case QEMU_OPTION_no_acpi
:
3009 case QEMU_OPTION_no_hpet
:
3012 case QEMU_OPTION_balloon
:
3013 if (balloon_parse(optarg
) < 0) {
3014 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3018 case QEMU_OPTION_no_reboot
:
3021 case QEMU_OPTION_no_shutdown
:
3024 case QEMU_OPTION_show_cursor
:
3027 case QEMU_OPTION_uuid
:
3028 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3029 fprintf(stderr
, "Fail to parse UUID string."
3030 " Wrong format.\n");
3034 case QEMU_OPTION_option_rom
:
3035 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3036 fprintf(stderr
, "Too many option ROMs\n");
3039 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3040 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3041 option_rom
[nb_option_roms
].bootindex
=
3042 qemu_opt_get_number(opts
, "bootindex", -1);
3043 if (!option_rom
[nb_option_roms
].name
) {
3044 fprintf(stderr
, "Option ROM file is not specified\n");
3049 case QEMU_OPTION_semihosting
:
3050 semihosting_enabled
= 1;
3052 case QEMU_OPTION_name
:
3053 qemu_name
= g_strdup(optarg
);
3055 char *p
= strchr(qemu_name
, ',');
3058 if (strncmp(p
, "process=", 8)) {
3059 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3063 os_set_proc_name(p
);
3067 case QEMU_OPTION_prom_env
:
3068 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3069 fprintf(stderr
, "Too many prom variables\n");
3072 prom_envs
[nb_prom_envs
] = optarg
;
3075 case QEMU_OPTION_old_param
:
3078 case QEMU_OPTION_clock
:
3079 configure_alarms(optarg
);
3081 case QEMU_OPTION_startdate
:
3082 configure_rtc_date_offset(optarg
, 1);
3084 case QEMU_OPTION_rtc
:
3085 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3089 configure_rtc(opts
);
3091 case QEMU_OPTION_tb_size
:
3092 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3093 if (tcg_tb_size
< 0) {
3097 case QEMU_OPTION_icount
:
3098 icount_option
= optarg
;
3100 case QEMU_OPTION_incoming
:
3103 case QEMU_OPTION_nodefaults
:
3105 default_parallel
= 0;
3106 default_virtcon
= 0;
3107 default_monitor
= 0;
3114 case QEMU_OPTION_xen_domid
:
3115 if (!(xen_available())) {
3116 printf("Option %s not supported for this target\n", popt
->name
);
3119 xen_domid
= atoi(optarg
);
3121 case QEMU_OPTION_xen_create
:
3122 if (!(xen_available())) {
3123 printf("Option %s not supported for this target\n", popt
->name
);
3126 xen_mode
= XEN_CREATE
;
3128 case QEMU_OPTION_xen_attach
:
3129 if (!(xen_available())) {
3130 printf("Option %s not supported for this target\n", popt
->name
);
3133 xen_mode
= XEN_ATTACH
;
3135 case QEMU_OPTION_trace
:
3137 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3141 trace_events
= qemu_opt_get(opts
, "events");
3142 trace_file
= qemu_opt_get(opts
, "file");
3145 case QEMU_OPTION_readconfig
:
3147 int ret
= qemu_read_config_file(optarg
);
3149 fprintf(stderr
, "read config %s: %s\n", optarg
,
3155 case QEMU_OPTION_spice
:
3156 olist
= qemu_find_opts("spice");
3158 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3161 opts
= qemu_opts_parse(olist
, optarg
, 0);
3163 fprintf(stderr
, "parse error: %s\n", optarg
);
3167 case QEMU_OPTION_writeconfig
:
3170 if (strcmp(optarg
, "-") == 0) {
3173 fp
= fopen(optarg
, "w");
3175 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3179 qemu_config_write(fp
);
3184 os_parse_cmd_args(popt
->index
, optarg
);
3190 /* Open the logfile at this point, if necessary. We can't open the logfile
3191 * when encountering either of the logging options (-d or -D) because the
3192 * other one may be encountered later on the command line, changing the
3193 * location or level of logging.
3197 set_cpu_log_filename(log_file
);
3199 set_cpu_log(log_mask
);
3202 if (!trace_backend_init(trace_events
, trace_file
)) {
3206 /* If no data_dir is specified then try to find it relative to the
3209 data_dir
= os_find_datadir(argv
[0]);
3211 /* If all else fails use the install path specified when building. */
3213 data_dir
= CONFIG_QEMU_DATADIR
;
3216 if (machine
== NULL
) {
3217 fprintf(stderr
, "No machine found.\n");
3222 * Default to max_cpus = smp_cpus, in case the user doesn't
3223 * specify a max_cpus value.
3226 max_cpus
= smp_cpus
;
3228 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3229 if (smp_cpus
> machine
->max_cpus
) {
3230 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3231 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3237 * Get the default machine options from the machine if it is not already
3238 * specified either by the configuration file or by the command line.
3240 if (machine
->default_machine_opts
) {
3241 qemu_opts_set_defaults(qemu_find_opts("machine"),
3242 machine
->default_machine_opts
, 0);
3245 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3246 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3248 if (machine
->no_serial
) {
3251 if (machine
->no_parallel
) {
3252 default_parallel
= 0;
3254 if (!machine
->use_virtcon
) {
3255 default_virtcon
= 0;
3257 if (machine
->no_floppy
) {
3260 if (machine
->no_cdrom
) {
3263 if (machine
->no_sdcard
) {
3267 if (display_type
== DT_NOGRAPHIC
) {
3268 if (default_parallel
)
3269 add_device_config(DEV_PARALLEL
, "null");
3270 if (default_serial
&& default_monitor
) {
3271 add_device_config(DEV_SERIAL
, "mon:stdio");
3272 } else if (default_virtcon
&& default_monitor
) {
3273 add_device_config(DEV_VIRTCON
, "mon:stdio");
3276 add_device_config(DEV_SERIAL
, "stdio");
3277 if (default_virtcon
)
3278 add_device_config(DEV_VIRTCON
, "stdio");
3279 if (default_monitor
)
3280 monitor_parse("stdio", "readline");
3284 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3285 if (default_parallel
)
3286 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3287 if (default_monitor
)
3288 monitor_parse("vc:80Cx24C", "readline");
3289 if (default_virtcon
)
3290 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3295 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3297 #ifdef CONFIG_VIRTFS
3298 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3305 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3310 /* init the memory */
3311 if (ram_size
== 0) {
3312 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3315 configure_accelerator();
3317 qemu_init_cpu_loop();
3318 if (qemu_init_main_loop()) {
3319 fprintf(stderr
, "qemu_init_main_loop failed\n");
3323 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3325 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3326 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3327 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3329 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3332 if (!kernel_cmdline
) {
3333 kernel_cmdline
= "";
3336 linux_boot
= (kernel_filename
!= NULL
);
3338 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3339 fprintf(stderr
, "-append only allowed with -kernel option\n");
3343 if (!linux_boot
&& initrd_filename
!= NULL
) {
3344 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3348 os_set_line_buffering();
3350 if (init_timer_alarm() < 0) {
3351 fprintf(stderr
, "could not initialize alarm timer\n");
3355 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3356 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3359 configure_icount(icount_option
);
3361 if (net_init_clients() < 0) {
3365 /* init the bluetooth world */
3366 if (foreach_device_config(DEV_BT
, bt_parse
))
3369 if (!xen_enabled()) {
3370 /* On 32-bit hosts, QEMU is limited by virtual address space */
3371 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3372 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3377 cpu_exec_init_all();
3379 bdrv_init_with_whitelist();
3383 /* open the virtual block devices */
3385 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3386 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3389 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3390 IF_DEFAULT
, 2, CDROM_OPTS
);
3391 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3392 IF_FLOPPY
, 0, FD_OPTS
);
3393 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3396 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3399 if (nb_numa_nodes
> 0) {
3402 if (nb_numa_nodes
> MAX_NODES
) {
3403 nb_numa_nodes
= MAX_NODES
;
3406 /* If no memory size if given for any node, assume the default case
3407 * and distribute the available memory equally across all nodes
3409 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3410 if (node_mem
[i
] != 0)
3413 if (i
== nb_numa_nodes
) {
3414 uint64_t usedmem
= 0;
3416 /* On Linux, the each node's border has to be 8MB aligned,
3417 * the final node gets the rest.
3419 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3420 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3421 usedmem
+= node_mem
[i
];
3423 node_mem
[i
] = ram_size
- usedmem
;
3426 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3427 if (node_cpumask
[i
] != 0)
3430 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3431 * must cope with this anyway, because there are BIOSes out there in
3432 * real machines which also use this scheme.
3434 if (i
== nb_numa_nodes
) {
3435 for (i
= 0; i
< max_cpus
; i
++) {
3436 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3441 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3445 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3447 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3449 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3451 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3454 module_call_init(MODULE_INIT_QOM
);
3456 /* must be after qdev registration but before machine init */
3458 select_vgahw(vga_model
);
3459 } else if (cirrus_vga_available()) {
3460 select_vgahw("cirrus");
3462 select_vgahw("none");
3465 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3469 i
= select_watchdog(watchdog
);
3471 exit (i
== 1 ? 1 : 0);
3474 if (machine
->compat_props
) {
3475 qdev_prop_register_global_list(machine
->compat_props
);
3479 qdev_machine_init();
3481 machine
->init(ram_size
, boot_devices
,
3482 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3484 cpu_synchronize_all_post_init();
3488 current_machine
= machine
;
3490 /* init USB devices */
3492 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3496 /* init generic devices */
3497 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3500 net_check_clients();
3502 /* just use the first displaystate for the moment */
3503 ds
= get_displaystate();
3507 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3508 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3509 display_type
= DT_SDL
;
3510 #elif defined(CONFIG_VNC)
3511 vnc_display
= "localhost:0,to=99";
3514 display_type
= DT_NONE
;
3519 /* init local displays */
3520 switch (display_type
) {
3523 #if defined(CONFIG_CURSES)
3525 curses_display_init(ds
, full_screen
);
3528 #if defined(CONFIG_SDL)
3530 sdl_display_init(ds
, full_screen
, no_frame
);
3532 #elif defined(CONFIG_COCOA)
3534 cocoa_display_init(ds
, full_screen
);
3541 /* must be after terminal init, SDL library changes signal handlers */
3542 os_setup_signal_handling();
3545 /* init remote displays */
3547 vnc_display_init(ds
);
3548 if (vnc_display_open(ds
, vnc_display
) < 0)
3551 if (show_vnc_port
) {
3552 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3557 if (using_spice
&& !qxl_enabled
) {
3558 qemu_spice_display_init(ds
);
3564 dcl
= ds
->listeners
;
3565 while (dcl
!= NULL
) {
3566 if (dcl
->dpy_refresh
!= NULL
) {
3567 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3568 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3573 text_consoles_set_display(ds
);
3575 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3579 qdev_machine_creation_done();
3581 if (rom_load_all() != 0) {
3582 fprintf(stderr
, "rom loading failed\n");
3586 /* TODO: once all bus devices are qdevified, this should be done
3587 * when bus is created by qdev.c */
3588 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3589 qemu_run_machine_init_done_notifiers();
3591 qemu_system_reset(VMRESET_SILENT
);
3593 if (load_vmstate(loadvm
) < 0) {
3599 runstate_set(RUN_STATE_INMIGRATE
);
3600 int ret
= qemu_start_incoming_migration(incoming
);
3602 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3606 } else if (autostart
) {