4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #include <arpa/inet.h>
49 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
64 #include <linux/ppdev.h>
65 #include <linux/parport.h>
69 #include <sys/ethernet.h>
70 #include <sys/sockio.h>
71 #include <netinet/arp.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_icmp.h> // must come after ip.h
75 #include <netinet/udp.h>
76 #include <netinet/tcp.h>
84 #if defined(__OpenBSD__)
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
97 #if defined(__APPLE__) || defined(main)
99 int qemu_main(int argc
, char **argv
, char **envp
);
100 int main(int argc
, char **argv
)
102 return qemu_main(argc
, argv
, NULL
);
105 #define main qemu_main
107 #endif /* CONFIG_SDL */
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
117 #include "hw/boards.h"
119 #include "hw/pcmcia.h"
124 #include "hw/watchdog.h"
125 #include "hw/smbios.h"
128 #include "hw/loader.h"
131 #include "net/slirp.h"
136 #include "qemu-timer.h"
137 #include "qemu-char.h"
138 #include "cache-utils.h"
140 #include "blockdev.h"
141 #include "block-migration.h"
143 #include "audio/audio.h"
144 #include "migration.h"
146 #include "qemu-option.h"
147 #include "qemu-config.h"
148 #include "qemu-objects.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
152 #include "fsdev/qemu-fsdev.h"
157 #include "qemu_socket.h"
159 #include "slirp/libslirp.h"
162 #include "trace/control.h"
163 #include "qemu-queue.h"
165 #include "arch_init.h"
167 #include "ui/qemu-spice.h"
170 //#define DEBUG_SLIRP
172 #define DEFAULT_RAM_SIZE 128
174 #define MAX_VIRTIO_CONSOLES 1
176 static const char *data_dir
;
177 const char *bios_name
= NULL
;
178 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
179 DisplayType display_type
= DT_DEFAULT
;
180 int display_remote
= 0;
181 const char* keyboard_layout
= NULL
;
183 const char *mem_path
= NULL
;
185 int mem_prealloc
= 0; /* force preallocation of physical target memory */
188 NICInfo nd_table
[MAX_NICS
];
190 static int rtc_utc
= 1;
191 static int rtc_date_offset
= -1; /* -1 means no change */
192 QEMUClock
*rtc_clock
;
193 int vga_interface_type
= VGA_NONE
;
194 static int full_screen
= 0;
196 static int no_frame
= 0;
199 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
200 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
201 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
202 int win2k_install_hack
= 0;
211 const char *vnc_display
;
213 int acpi_enabled
= 1;
219 int graphic_rotate
= 0;
220 uint8_t irq0override
= 1;
221 const char *watchdog
;
222 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
226 const char *qemu_name
;
229 unsigned int nb_prom_envs
= 0;
230 const char *prom_envs
[MAX_PROM_ENVS
];
232 uint8_t *boot_splash_filedata
;
233 int boot_splash_filedata_size
;
234 uint8_t qemu_extra_params_fw
[2];
236 typedef struct FWBootEntry FWBootEntry
;
239 QTAILQ_ENTRY(FWBootEntry
) link
;
245 QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
= QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
248 uint64_t node_mem
[MAX_NODES
];
249 uint64_t node_cpumask
[MAX_NODES
];
251 uint8_t qemu_uuid
[16];
253 static QEMUBootSetHandler
*boot_set_handler
;
254 static void *boot_set_opaque
;
256 static NotifierList exit_notifiers
=
257 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
259 static NotifierList machine_init_done_notifiers
=
260 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
262 static int tcg_allowed
= 1;
266 enum xen_mode xen_mode
= XEN_EMULATE
;
267 static int tcg_tb_size
;
269 static int default_serial
= 1;
270 static int default_parallel
= 1;
271 static int default_virtcon
= 1;
272 static int default_monitor
= 1;
273 static int default_vga
= 1;
274 static int default_floppy
= 1;
275 static int default_cdrom
= 1;
276 static int default_sdcard
= 1;
282 { .driver
= "isa-serial", .flag
= &default_serial
},
283 { .driver
= "isa-parallel", .flag
= &default_parallel
},
284 { .driver
= "isa-fdc", .flag
= &default_floppy
},
285 { .driver
= "ide-cd", .flag
= &default_cdrom
},
286 { .driver
= "ide-hd", .flag
= &default_cdrom
},
287 { .driver
= "ide-drive", .flag
= &default_cdrom
},
288 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
289 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
290 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
291 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
292 { .driver
= "VGA", .flag
= &default_vga
},
293 { .driver
= "cirrus-vga", .flag
= &default_vga
},
294 { .driver
= "vmware-svga", .flag
= &default_vga
},
295 { .driver
= "isa-vga", .flag
= &default_vga
},
296 { .driver
= "qxl-vga", .flag
= &default_vga
},
299 static void res_free(void)
301 if (boot_splash_filedata
!= NULL
) {
302 g_free(boot_splash_filedata
);
303 boot_splash_filedata
= NULL
;
307 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
309 const char *driver
= qemu_opt_get(opts
, "driver");
314 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
315 if (strcmp(default_list
[i
].driver
, driver
) != 0)
317 *(default_list
[i
].flag
) = 0;
322 /***********************************************************/
325 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
330 } RunStateTransition
;
332 static const RunStateTransition runstate_transitions_def
[] = {
334 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
336 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
337 { RUN_STATE_INMIGRATE
, RUN_STATE_PRELAUNCH
},
339 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
341 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
343 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
344 { RUN_STATE_PAUSED
, RUN_STATE_POSTMIGRATE
},
346 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
348 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
349 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
350 { RUN_STATE_PRELAUNCH
, RUN_STATE_POSTMIGRATE
},
352 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
353 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
355 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
357 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
358 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
359 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
360 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
361 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
362 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
363 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
364 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
365 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
367 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
369 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
371 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
373 { RUN_STATE_MAX
, RUN_STATE_MAX
},
376 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
378 bool runstate_check(RunState state
)
380 return current_run_state
== state
;
383 void runstate_init(void)
385 const RunStateTransition
*p
;
387 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
389 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
390 runstate_valid_transitions
[p
->from
][p
->to
] = true;
394 /* This function will abort() on invalid state transitions */
395 void runstate_set(RunState new_state
)
397 assert(new_state
< RUN_STATE_MAX
);
399 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
400 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
401 RunState_lookup
[current_run_state
],
402 RunState_lookup
[new_state
]);
406 current_run_state
= new_state
;
409 int runstate_is_running(void)
411 return runstate_check(RUN_STATE_RUNNING
);
414 StatusInfo
*qmp_query_status(Error
**errp
)
416 StatusInfo
*info
= g_malloc0(sizeof(*info
));
418 info
->running
= runstate_is_running();
419 info
->singlestep
= singlestep
;
420 info
->status
= current_run_state
;
425 /***********************************************************/
426 /* real time host monotonic timer */
428 /***********************************************************/
429 /* host time/date access */
430 void qemu_get_timedate(struct tm
*tm
, int offset
)
437 if (rtc_date_offset
== -1) {
441 ret
= localtime(&ti
);
443 ti
-= rtc_date_offset
;
447 memcpy(tm
, ret
, sizeof(struct tm
));
450 int qemu_timedate_diff(struct tm
*tm
)
454 if (rtc_date_offset
== -1)
456 seconds
= mktimegm(tm
);
458 seconds
= mktime(tm
);
460 seconds
= mktimegm(tm
) + rtc_date_offset
;
462 return seconds
- time(NULL
);
465 void rtc_change_mon_event(struct tm
*tm
)
469 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
470 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
471 qobject_decref(data
);
474 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
476 time_t rtc_start_date
;
479 if (!strcmp(startdate
, "now") && legacy
) {
480 rtc_date_offset
= -1;
482 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
490 } else if (sscanf(startdate
, "%d-%d-%d",
502 rtc_start_date
= mktimegm(&tm
);
503 if (rtc_start_date
== -1) {
505 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
506 "'2006-06-17T16:01:21' or '2006-06-17'\n");
509 rtc_date_offset
= time(NULL
) - rtc_start_date
;
513 static void configure_rtc(QemuOpts
*opts
)
517 value
= qemu_opt_get(opts
, "base");
519 if (!strcmp(value
, "utc")) {
521 } else if (!strcmp(value
, "localtime")) {
524 configure_rtc_date_offset(value
, 0);
527 value
= qemu_opt_get(opts
, "clock");
529 if (!strcmp(value
, "host")) {
530 rtc_clock
= host_clock
;
531 } else if (!strcmp(value
, "vm")) {
532 rtc_clock
= vm_clock
;
534 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
538 value
= qemu_opt_get(opts
, "driftfix");
540 if (!strcmp(value
, "slew")) {
542 } else if (!strcmp(value
, "none")) {
545 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
551 /***********************************************************/
552 /* Bluetooth support */
555 static struct HCIInfo
*hci_table
[MAX_NICS
];
557 static struct bt_vlan_s
{
558 struct bt_scatternet_s net
;
560 struct bt_vlan_s
*next
;
563 /* find or alloc a new bluetooth "VLAN" */
564 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
566 struct bt_vlan_s
**pvlan
, *vlan
;
567 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
571 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
573 pvlan
= &first_bt_vlan
;
574 while (*pvlan
!= NULL
)
575 pvlan
= &(*pvlan
)->next
;
580 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
584 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
589 static struct HCIInfo null_hci
= {
590 .cmd_send
= null_hci_send
,
591 .sco_send
= null_hci_send
,
592 .acl_send
= null_hci_send
,
593 .bdaddr_set
= null_hci_addr_set
,
596 struct HCIInfo
*qemu_next_hci(void)
598 if (cur_hci
== nb_hcis
)
601 return hci_table
[cur_hci
++];
604 static struct HCIInfo
*hci_init(const char *str
)
607 struct bt_scatternet_s
*vlan
= 0;
609 if (!strcmp(str
, "null"))
612 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
614 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
615 else if (!strncmp(str
, "hci", 3)) {
618 if (!strncmp(str
+ 3, ",vlan=", 6)) {
619 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
624 vlan
= qemu_find_bt_vlan(0);
626 return bt_new_hci(vlan
);
629 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
634 static int bt_hci_parse(const char *str
)
639 if (nb_hcis
>= MAX_NICS
) {
640 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
653 bdaddr
.b
[5] = 0x56 + nb_hcis
;
654 hci
->bdaddr_set(hci
, bdaddr
.b
);
656 hci_table
[nb_hcis
++] = hci
;
661 static void bt_vhci_add(int vlan_id
)
663 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
666 fprintf(stderr
, "qemu: warning: adding a VHCI to "
667 "an empty scatternet %i\n", vlan_id
);
669 bt_vhci_init(bt_new_hci(vlan
));
672 static struct bt_device_s
*bt_device_add(const char *opt
)
674 struct bt_scatternet_s
*vlan
;
676 char *endp
= strstr(opt
, ",vlan=");
677 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
680 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
683 vlan_id
= strtol(endp
+ 6, &endp
, 0);
685 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
690 vlan
= qemu_find_bt_vlan(vlan_id
);
693 fprintf(stderr
, "qemu: warning: adding a slave device to "
694 "an empty scatternet %i\n", vlan_id
);
696 if (!strcmp(devname
, "keyboard"))
697 return bt_keyboard_init(vlan
);
699 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
703 static int bt_parse(const char *opt
)
705 const char *endp
, *p
;
708 if (strstart(opt
, "hci", &endp
)) {
709 if (!*endp
|| *endp
== ',') {
711 if (!strstart(endp
, ",vlan=", 0))
714 return bt_hci_parse(opt
);
716 } else if (strstart(opt
, "vhci", &endp
)) {
717 if (!*endp
|| *endp
== ',') {
719 if (strstart(endp
, ",vlan=", &p
)) {
720 vlan
= strtol(p
, (char **) &endp
, 0);
722 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
726 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
735 } else if (strstart(opt
, "device:", &endp
))
736 return !bt_device_add(endp
);
738 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
742 /***********************************************************/
743 /* QEMU Block devices */
745 #define HD_OPTS "media=disk"
746 #define CDROM_OPTS "media=cdrom"
748 #define PFLASH_OPTS ""
752 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
754 int *use_scsi
= opaque
;
756 return drive_init(opts
, *use_scsi
) == NULL
;
759 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
761 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
762 qemu_opt_set(opts
, "snapshot", "on");
767 static void default_drive(int enable
, int snapshot
, int use_scsi
,
768 BlockInterfaceType type
, int index
,
773 if (type
== IF_DEFAULT
) {
774 type
= use_scsi
? IF_SCSI
: IF_IDE
;
777 if (!enable
|| drive_get_by_index(type
, index
)) {
781 opts
= drive_add(type
, index
, NULL
, optstr
);
783 drive_enable_snapshot(opts
, NULL
);
785 if (!drive_init(opts
, use_scsi
)) {
790 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
792 boot_set_handler
= func
;
793 boot_set_opaque
= opaque
;
796 int qemu_boot_set(const char *boot_devices
)
798 if (!boot_set_handler
) {
801 return boot_set_handler(boot_set_opaque
, boot_devices
);
804 static void validate_bootdevices(char *devices
)
806 /* We just do some generic consistency checks */
810 for (p
= devices
; *p
!= '\0'; p
++) {
811 /* Allowed boot devices are:
812 * a-b: floppy disk drives
813 * c-f: IDE disk drives
814 * g-m: machine implementation dependant drives
815 * n-p: network devices
816 * It's up to each machine implementation to check if the given boot
817 * devices match the actual hardware implementation and firmware
820 if (*p
< 'a' || *p
> 'p') {
821 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
824 if (bitmap
& (1 << (*p
- 'a'))) {
825 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
828 bitmap
|= 1 << (*p
- 'a');
832 static void restore_boot_devices(void *opaque
)
834 char *standard_boot_devices
= opaque
;
835 static int first
= 1;
837 /* Restore boot order and remove ourselves after the first boot */
843 qemu_boot_set(standard_boot_devices
);
845 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
846 g_free(standard_boot_devices
);
849 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
852 FWBootEntry
*node
, *i
;
858 assert(dev
!= NULL
|| suffix
!= NULL
);
860 node
= g_malloc0(sizeof(FWBootEntry
));
861 node
->bootindex
= bootindex
;
862 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
865 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
866 if (i
->bootindex
== bootindex
) {
867 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
869 } else if (i
->bootindex
< bootindex
) {
872 QTAILQ_INSERT_BEFORE(i
, node
, link
);
875 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
879 * This function returns null terminated string that consist of new line
880 * separated device paths.
882 * memory pointed by "size" is assigned total length of the array in bytes
885 char *get_boot_devices_list(uint32_t *size
)
891 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
892 char *devpath
= NULL
, *bootpath
;
896 devpath
= qdev_get_fw_dev_path(i
->dev
);
900 if (i
->suffix
&& devpath
) {
901 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
903 bootpath
= g_malloc(bootpathlen
);
904 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
906 } else if (devpath
) {
909 bootpath
= g_strdup(i
->suffix
);
914 list
[total
-1] = '\n';
916 len
= strlen(bootpath
) + 1;
917 list
= g_realloc(list
, total
+ len
);
918 memcpy(&list
[total
], bootpath
, len
);
928 static void numa_add(const char *optarg
)
932 unsigned long long value
, endvalue
;
935 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
936 if (!strcmp(option
, "node")) {
937 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
938 nodenr
= nb_numa_nodes
;
940 nodenr
= strtoull(option
, NULL
, 10);
943 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
944 node_mem
[nodenr
] = 0;
947 sval
= strtosz(option
, NULL
);
949 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
952 node_mem
[nodenr
] = sval
;
954 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
955 node_cpumask
[nodenr
] = 0;
957 value
= strtoull(option
, &endptr
, 10);
960 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
962 if (*endptr
== '-') {
963 endvalue
= strtoull(endptr
+1, &endptr
, 10);
964 if (endvalue
>= 63) {
967 "only 63 CPUs in NUMA mode supported.\n");
969 value
= (2ULL << endvalue
) - (1ULL << value
);
971 value
= 1ULL << value
;
974 node_cpumask
[nodenr
] = value
;
981 static void smp_parse(const char *optarg
)
983 int smp
, sockets
= 0, threads
= 0, cores
= 0;
987 smp
= strtoul(optarg
, &endptr
, 10);
988 if (endptr
!= optarg
) {
989 if (*endptr
== ',') {
993 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
994 sockets
= strtoull(option
, NULL
, 10);
995 if (get_param_value(option
, 128, "cores", endptr
) != 0)
996 cores
= strtoull(option
, NULL
, 10);
997 if (get_param_value(option
, 128, "threads", endptr
) != 0)
998 threads
= strtoull(option
, NULL
, 10);
999 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1000 max_cpus
= strtoull(option
, NULL
, 10);
1002 /* compute missing values, prefer sockets over cores over threads */
1003 if (smp
== 0 || sockets
== 0) {
1004 sockets
= sockets
> 0 ? sockets
: 1;
1005 cores
= cores
> 0 ? cores
: 1;
1006 threads
= threads
> 0 ? threads
: 1;
1008 smp
= cores
* threads
* sockets
;
1012 threads
= threads
> 0 ? threads
: 1;
1013 cores
= smp
/ (sockets
* threads
);
1015 threads
= smp
/ (cores
* sockets
);
1019 smp_cores
= cores
> 0 ? cores
: 1;
1020 smp_threads
= threads
> 0 ? threads
: 1;
1022 max_cpus
= smp_cpus
;
1025 /***********************************************************/
1028 static int usb_device_add(const char *devname
)
1031 USBDevice
*dev
= NULL
;
1036 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1037 dev
= usbdevice_create(devname
);
1041 /* the other ones */
1042 #ifndef CONFIG_LINUX
1043 /* only the linux version is qdev-ified, usb-bsd still needs this */
1044 if (strstart(devname
, "host:", &p
)) {
1045 dev
= usb_host_device_open(p
);
1048 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1049 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
1050 bt_new_hci(qemu_find_bt_vlan(0)));
1061 static int usb_device_del(const char *devname
)
1066 if (strstart(devname
, "host:", &p
))
1067 return usb_host_device_close(p
);
1072 p
= strchr(devname
, '.');
1075 bus_num
= strtoul(devname
, NULL
, 0);
1076 addr
= strtoul(p
+ 1, NULL
, 0);
1078 return usb_device_delete_addr(bus_num
, addr
);
1081 static int usb_parse(const char *cmdline
)
1084 r
= usb_device_add(cmdline
);
1086 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1091 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1093 const char *devname
= qdict_get_str(qdict
, "devname");
1094 if (usb_device_add(devname
) < 0) {
1095 error_report("could not add USB device '%s'", devname
);
1099 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1101 const char *devname
= qdict_get_str(qdict
, "devname");
1102 if (usb_device_del(devname
) < 0) {
1103 error_report("could not delete USB device '%s'", devname
);
1107 /***********************************************************/
1108 /* PCMCIA/Cardbus */
1110 static struct pcmcia_socket_entry_s
{
1111 PCMCIASocket
*socket
;
1112 struct pcmcia_socket_entry_s
*next
;
1113 } *pcmcia_sockets
= 0;
1115 void pcmcia_socket_register(PCMCIASocket
*socket
)
1117 struct pcmcia_socket_entry_s
*entry
;
1119 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1120 entry
->socket
= socket
;
1121 entry
->next
= pcmcia_sockets
;
1122 pcmcia_sockets
= entry
;
1125 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1127 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1129 ptr
= &pcmcia_sockets
;
1130 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1131 if (entry
->socket
== socket
) {
1137 void pcmcia_info(Monitor
*mon
)
1139 struct pcmcia_socket_entry_s
*iter
;
1141 if (!pcmcia_sockets
)
1142 monitor_printf(mon
, "No PCMCIA sockets\n");
1144 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1145 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1146 iter
->socket
->attached
? iter
->socket
->card_string
:
1150 /***********************************************************/
1151 /* machine registration */
1153 static QEMUMachine
*first_machine
= NULL
;
1154 QEMUMachine
*current_machine
= NULL
;
1156 int qemu_register_machine(QEMUMachine
*m
)
1159 pm
= &first_machine
;
1167 static QEMUMachine
*find_machine(const char *name
)
1171 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1172 if (!strcmp(m
->name
, name
))
1174 if (m
->alias
&& !strcmp(m
->alias
, name
))
1180 static QEMUMachine
*find_default_machine(void)
1184 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1185 if (m
->is_default
) {
1192 /***********************************************************/
1193 /* main execution loop */
1195 static void gui_update(void *opaque
)
1197 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1198 DisplayState
*ds
= opaque
;
1199 DisplayChangeListener
*dcl
= ds
->listeners
;
1203 while (dcl
!= NULL
) {
1204 if (dcl
->gui_timer_interval
&&
1205 dcl
->gui_timer_interval
< interval
)
1206 interval
= dcl
->gui_timer_interval
;
1209 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1212 struct vm_change_state_entry
{
1213 VMChangeStateHandler
*cb
;
1215 QLIST_ENTRY (vm_change_state_entry
) entries
;
1218 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1220 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1223 VMChangeStateEntry
*e
;
1225 e
= g_malloc0(sizeof (*e
));
1229 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1233 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1235 QLIST_REMOVE (e
, entries
);
1239 void vm_state_notify(int running
, RunState state
)
1241 VMChangeStateEntry
*e
;
1243 trace_vm_state_notify(running
, state
);
1245 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1246 e
->cb(e
->opaque
, running
, state
);
1252 if (!runstate_is_running()) {
1254 runstate_set(RUN_STATE_RUNNING
);
1255 vm_state_notify(1, RUN_STATE_RUNNING
);
1257 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1261 /* reset/shutdown handler */
1263 typedef struct QEMUResetEntry
{
1264 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1265 QEMUResetHandler
*func
;
1269 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1270 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1271 static int reset_requested
;
1272 static int shutdown_requested
, shutdown_signal
= -1;
1273 static pid_t shutdown_pid
;
1274 static int powerdown_requested
;
1275 static int debug_requested
;
1276 static RunState vmstop_requested
= RUN_STATE_MAX
;
1278 int qemu_shutdown_requested_get(void)
1280 return shutdown_requested
;
1283 int qemu_reset_requested_get(void)
1285 return reset_requested
;
1288 int qemu_shutdown_requested(void)
1290 int r
= shutdown_requested
;
1291 shutdown_requested
= 0;
1295 void qemu_kill_report(void)
1297 if (shutdown_signal
!= -1) {
1298 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1299 if (shutdown_pid
== 0) {
1300 /* This happens for eg ^C at the terminal, so it's worth
1301 * avoiding printing an odd message in that case.
1303 fputc('\n', stderr
);
1305 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1307 shutdown_signal
= -1;
1311 int qemu_reset_requested(void)
1313 int r
= reset_requested
;
1314 reset_requested
= 0;
1318 int qemu_powerdown_requested(void)
1320 int r
= powerdown_requested
;
1321 powerdown_requested
= 0;
1325 static int qemu_debug_requested(void)
1327 int r
= debug_requested
;
1328 debug_requested
= 0;
1332 /* We use RUN_STATE_MAX but any invalid value will do */
1333 static bool qemu_vmstop_requested(RunState
*r
)
1335 if (vmstop_requested
< RUN_STATE_MAX
) {
1336 *r
= vmstop_requested
;
1337 vmstop_requested
= RUN_STATE_MAX
;
1344 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1346 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1349 re
->opaque
= opaque
;
1350 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1353 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1357 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1358 if (re
->func
== func
&& re
->opaque
== opaque
) {
1359 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1366 void qemu_system_reset(bool report
)
1368 QEMUResetEntry
*re
, *nre
;
1370 /* reset all devices */
1371 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1372 re
->func(re
->opaque
);
1375 monitor_protocol_event(QEVENT_RESET
, NULL
);
1377 cpu_synchronize_all_post_reset();
1380 void qemu_system_reset_request(void)
1383 shutdown_requested
= 1;
1385 reset_requested
= 1;
1388 qemu_notify_event();
1391 void qemu_system_killed(int signal
, pid_t pid
)
1393 shutdown_signal
= signal
;
1396 qemu_system_shutdown_request();
1399 void qemu_system_shutdown_request(void)
1401 shutdown_requested
= 1;
1402 qemu_notify_event();
1405 void qemu_system_powerdown_request(void)
1407 powerdown_requested
= 1;
1408 qemu_notify_event();
1411 void qemu_system_debug_request(void)
1413 debug_requested
= 1;
1414 qemu_notify_event();
1417 void qemu_system_vmstop_request(RunState state
)
1419 vmstop_requested
= state
;
1420 qemu_notify_event();
1423 static GPollFD poll_fds
[1024 * 2]; /* this is probably overkill */
1424 static int n_poll_fds
;
1425 static int max_priority
;
1427 static void glib_select_fill(int *max_fd
, fd_set
*rfds
, fd_set
*wfds
,
1428 fd_set
*xfds
, struct timeval
*tv
)
1430 GMainContext
*context
= g_main_context_default();
1432 int timeout
= 0, cur_timeout
;
1434 g_main_context_prepare(context
, &max_priority
);
1436 n_poll_fds
= g_main_context_query(context
, max_priority
, &timeout
,
1437 poll_fds
, ARRAY_SIZE(poll_fds
));
1438 g_assert(n_poll_fds
<= ARRAY_SIZE(poll_fds
));
1440 for (i
= 0; i
< n_poll_fds
; i
++) {
1441 GPollFD
*p
= &poll_fds
[i
];
1443 if ((p
->events
& G_IO_IN
)) {
1444 FD_SET(p
->fd
, rfds
);
1445 *max_fd
= MAX(*max_fd
, p
->fd
);
1447 if ((p
->events
& G_IO_OUT
)) {
1448 FD_SET(p
->fd
, wfds
);
1449 *max_fd
= MAX(*max_fd
, p
->fd
);
1451 if ((p
->events
& G_IO_ERR
)) {
1452 FD_SET(p
->fd
, xfds
);
1453 *max_fd
= MAX(*max_fd
, p
->fd
);
1457 cur_timeout
= (tv
->tv_sec
* 1000) + ((tv
->tv_usec
+ 500) / 1000);
1458 if (timeout
>= 0 && timeout
< cur_timeout
) {
1459 tv
->tv_sec
= timeout
/ 1000;
1460 tv
->tv_usec
= (timeout
% 1000) * 1000;
1464 static void glib_select_poll(fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
1467 GMainContext
*context
= g_main_context_default();
1472 for (i
= 0; i
< n_poll_fds
; i
++) {
1473 GPollFD
*p
= &poll_fds
[i
];
1475 if ((p
->events
& G_IO_IN
) && FD_ISSET(p
->fd
, rfds
)) {
1476 p
->revents
|= G_IO_IN
;
1478 if ((p
->events
& G_IO_OUT
) && FD_ISSET(p
->fd
, wfds
)) {
1479 p
->revents
|= G_IO_OUT
;
1481 if ((p
->events
& G_IO_ERR
) && FD_ISSET(p
->fd
, xfds
)) {
1482 p
->revents
|= G_IO_ERR
;
1487 if (g_main_context_check(context
, max_priority
, poll_fds
, n_poll_fds
)) {
1488 g_main_context_dispatch(context
);
1492 int main_loop_wait(int nonblocking
)
1494 fd_set rfds
, wfds
, xfds
;
1502 timeout
= qemu_calculate_timeout();
1503 qemu_bh_update_timeout(&timeout
);
1506 os_host_main_loop_wait(&timeout
);
1508 tv
.tv_sec
= timeout
/ 1000;
1509 tv
.tv_usec
= (timeout
% 1000) * 1000;
1511 /* poll any events */
1512 /* XXX: separate device handlers from system ones */
1518 qemu_iohandler_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1519 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1520 glib_select_fill(&nfds
, &rfds
, &wfds
, &xfds
, &tv
);
1523 qemu_mutex_unlock_iothread();
1526 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1529 qemu_mutex_lock_iothread();
1532 qemu_iohandler_poll(&rfds
, &wfds
, &xfds
, ret
);
1533 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1534 glib_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
1536 qemu_run_all_timers();
1538 /* Check bottom-halves last in case any of the earlier events triggered
1545 qemu_irq qemu_system_powerdown
;
1547 static void main_loop(void)
1550 int last_io
__attribute__ ((unused
)) = 0;
1551 #ifdef CONFIG_PROFILER
1556 qemu_main_loop_start();
1559 nonblocking
= !kvm_enabled() && last_io
> 0;
1560 #ifdef CONFIG_PROFILER
1561 ti
= profile_getclock();
1563 last_io
= main_loop_wait(nonblocking
);
1564 #ifdef CONFIG_PROFILER
1565 dev_time
+= profile_getclock() - ti
;
1568 if (qemu_debug_requested()) {
1569 vm_stop(RUN_STATE_DEBUG
);
1571 if (qemu_shutdown_requested()) {
1573 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1575 vm_stop(RUN_STATE_SHUTDOWN
);
1579 if (qemu_reset_requested()) {
1581 cpu_synchronize_all_states();
1582 qemu_system_reset(VMRESET_REPORT
);
1584 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1585 runstate_check(RUN_STATE_SHUTDOWN
)) {
1586 runstate_set(RUN_STATE_PAUSED
);
1589 if (qemu_powerdown_requested()) {
1590 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1591 qemu_irq_raise(qemu_system_powerdown
);
1593 if (qemu_vmstop_requested(&r
)) {
1601 static void version(void)
1603 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1606 static void help(int exitcode
)
1608 const char *options_help
=
1609 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1611 #define DEFHEADING(text) stringify(text) "\n"
1612 #include "qemu-options.def"
1618 printf("usage: %s [options] [disk_image]\n"
1620 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
1623 "During emulation, the following keys are useful:\n"
1624 "ctrl-alt-f toggle full screen\n"
1625 "ctrl-alt-n switch to virtual console 'n'\n"
1626 "ctrl-alt toggle mouse and keyboard grab\n"
1628 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1634 #define HAS_ARG 0x0001
1636 typedef struct QEMUOption
{
1643 static const QEMUOption qemu_options
[] = {
1644 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1645 #define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1646 { option, opt_arg, opt_enum, arch_mask },
1647 #define DEFHEADING(text)
1648 #include "qemu-options.def"
1654 static void select_vgahw (const char *p
)
1659 vga_interface_type
= VGA_NONE
;
1660 if (strstart(p
, "std", &opts
)) {
1661 vga_interface_type
= VGA_STD
;
1662 } else if (strstart(p
, "cirrus", &opts
)) {
1663 vga_interface_type
= VGA_CIRRUS
;
1664 } else if (strstart(p
, "vmware", &opts
)) {
1665 vga_interface_type
= VGA_VMWARE
;
1666 } else if (strstart(p
, "xenfb", &opts
)) {
1667 vga_interface_type
= VGA_XENFB
;
1668 } else if (strstart(p
, "qxl", &opts
)) {
1669 vga_interface_type
= VGA_QXL
;
1670 } else if (!strstart(p
, "none", &opts
)) {
1672 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1676 const char *nextopt
;
1678 if (strstart(opts
, ",retrace=", &nextopt
)) {
1680 if (strstart(opts
, "dumb", &nextopt
))
1681 vga_retrace_method
= VGA_RETRACE_DUMB
;
1682 else if (strstart(opts
, "precise", &nextopt
))
1683 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1684 else goto invalid_vga
;
1685 } else goto invalid_vga
;
1690 static DisplayType
select_display(const char *p
)
1693 DisplayType display
= DT_DEFAULT
;
1695 if (strstart(p
, "sdl", &opts
)) {
1699 const char *nextopt
;
1701 if (strstart(opts
, ",frame=", &nextopt
)) {
1703 if (strstart(opts
, "on", &nextopt
)) {
1705 } else if (strstart(opts
, "off", &nextopt
)) {
1708 goto invalid_sdl_args
;
1710 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1712 if (strstart(opts
, "on", &nextopt
)) {
1714 } else if (strstart(opts
, "off", &nextopt
)) {
1717 goto invalid_sdl_args
;
1719 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1721 if (strstart(opts
, "on", &nextopt
)) {
1723 } else if (strstart(opts
, "off", &nextopt
)) {
1726 goto invalid_sdl_args
;
1728 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1730 if (strstart(opts
, "on", &nextopt
)) {
1732 } else if (strstart(opts
, "off", &nextopt
)) {
1735 goto invalid_sdl_args
;
1739 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1745 fprintf(stderr
, "SDL support is disabled\n");
1748 } else if (strstart(p
, "vnc", &opts
)) {
1753 const char *nextopt
;
1755 if (strstart(opts
, "=", &nextopt
)) {
1756 vnc_display
= nextopt
;
1760 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1764 fprintf(stderr
, "VNC support is disabled\n");
1767 } else if (strstart(p
, "curses", &opts
)) {
1768 #ifdef CONFIG_CURSES
1769 display
= DT_CURSES
;
1771 fprintf(stderr
, "Curses support is disabled\n");
1774 } else if (strstart(p
, "none", &opts
)) {
1777 fprintf(stderr
, "Unknown display type: %s\n", p
);
1784 static int balloon_parse(const char *arg
)
1788 if (strcmp(arg
, "none") == 0) {
1792 if (!strncmp(arg
, "virtio", 6)) {
1793 if (arg
[6] == ',') {
1794 /* have params -> parse them */
1795 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1799 /* create empty opts */
1800 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
1802 qemu_opt_set(opts
, "driver", "virtio-balloon");
1809 char *qemu_find_file(int type
, const char *name
)
1815 /* If name contains path separators then try it as a straight path. */
1816 if ((strchr(name
, '/') || strchr(name
, '\\'))
1817 && access(name
, R_OK
) == 0) {
1818 return g_strdup(name
);
1821 case QEMU_FILE_TYPE_BIOS
:
1824 case QEMU_FILE_TYPE_KEYMAP
:
1825 subdir
= "keymaps/";
1830 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
1831 buf
= g_malloc0(len
);
1832 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
1833 if (access(buf
, R_OK
)) {
1840 static int device_help_func(QemuOpts
*opts
, void *opaque
)
1842 return qdev_device_help(opts
);
1845 static int device_init_func(QemuOpts
*opts
, void *opaque
)
1849 dev
= qdev_device_add(opts
);
1855 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
1857 CharDriverState
*chr
;
1859 chr
= qemu_chr_new_from_opts(opts
, NULL
);
1865 #ifdef CONFIG_VIRTFS
1866 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
1869 ret
= qemu_fsdev_add(opts
);
1875 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
1877 CharDriverState
*chr
;
1878 const char *chardev
;
1882 mode
= qemu_opt_get(opts
, "mode");
1886 if (strcmp(mode
, "readline") == 0) {
1887 flags
= MONITOR_USE_READLINE
;
1888 } else if (strcmp(mode
, "control") == 0) {
1889 flags
= MONITOR_USE_CONTROL
;
1891 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
1895 if (qemu_opt_get_bool(opts
, "pretty", 0))
1896 flags
|= MONITOR_USE_PRETTY
;
1898 if (qemu_opt_get_bool(opts
, "default", 0))
1899 flags
|= MONITOR_IS_DEFAULT
;
1901 chardev
= qemu_opt_get(opts
, "chardev");
1902 chr
= qemu_chr_find(chardev
);
1904 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
1908 monitor_init(chr
, flags
);
1912 static void monitor_parse(const char *optarg
, const char *mode
)
1914 static int monitor_device_index
= 0;
1920 if (strstart(optarg
, "chardev:", &p
)) {
1921 snprintf(label
, sizeof(label
), "%s", p
);
1923 snprintf(label
, sizeof(label
), "compat_monitor%d",
1924 monitor_device_index
);
1925 if (monitor_device_index
== 0) {
1928 opts
= qemu_chr_parse_compat(label
, optarg
);
1930 fprintf(stderr
, "parse error: %s\n", optarg
);
1935 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1);
1937 fprintf(stderr
, "duplicate chardev: %s\n", label
);
1940 qemu_opt_set(opts
, "mode", mode
);
1941 qemu_opt_set(opts
, "chardev", label
);
1943 qemu_opt_set(opts
, "default", "on");
1944 monitor_device_index
++;
1947 struct device_config
{
1949 DEV_USB
, /* -usbdevice */
1951 DEV_SERIAL
, /* -serial */
1952 DEV_PARALLEL
, /* -parallel */
1953 DEV_VIRTCON
, /* -virtioconsole */
1954 DEV_DEBUGCON
, /* -debugcon */
1956 const char *cmdline
;
1957 QTAILQ_ENTRY(device_config
) next
;
1959 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
1961 static void add_device_config(int type
, const char *cmdline
)
1963 struct device_config
*conf
;
1965 conf
= g_malloc0(sizeof(*conf
));
1967 conf
->cmdline
= cmdline
;
1968 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
1971 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
1973 struct device_config
*conf
;
1976 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
1977 if (conf
->type
!= type
)
1979 rc
= func(conf
->cmdline
);
1986 static int serial_parse(const char *devname
)
1988 static int index
= 0;
1991 if (strcmp(devname
, "none") == 0)
1993 if (index
== MAX_SERIAL_PORTS
) {
1994 fprintf(stderr
, "qemu: too many serial ports\n");
1997 snprintf(label
, sizeof(label
), "serial%d", index
);
1998 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
1999 if (!serial_hds
[index
]) {
2000 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
2001 devname
, strerror(errno
));
2008 static int parallel_parse(const char *devname
)
2010 static int index
= 0;
2013 if (strcmp(devname
, "none") == 0)
2015 if (index
== MAX_PARALLEL_PORTS
) {
2016 fprintf(stderr
, "qemu: too many parallel ports\n");
2019 snprintf(label
, sizeof(label
), "parallel%d", index
);
2020 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2021 if (!parallel_hds
[index
]) {
2022 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
2023 devname
, strerror(errno
));
2030 static int virtcon_parse(const char *devname
)
2032 QemuOptsList
*device
= qemu_find_opts("device");
2033 static int index
= 0;
2035 QemuOpts
*bus_opts
, *dev_opts
;
2037 if (strcmp(devname
, "none") == 0)
2039 if (index
== MAX_VIRTIO_CONSOLES
) {
2040 fprintf(stderr
, "qemu: too many virtio consoles\n");
2044 bus_opts
= qemu_opts_create(device
, NULL
, 0);
2045 qemu_opt_set(bus_opts
, "driver", "virtio-serial");
2047 dev_opts
= qemu_opts_create(device
, NULL
, 0);
2048 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2050 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2051 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2052 if (!virtcon_hds
[index
]) {
2053 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
2054 devname
, strerror(errno
));
2057 qemu_opt_set(dev_opts
, "chardev", label
);
2063 static int debugcon_parse(const char *devname
)
2067 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2070 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
2072 fprintf(stderr
, "qemu: already have a debugcon device\n");
2075 qemu_opt_set(opts
, "driver", "isa-debugcon");
2076 qemu_opt_set(opts
, "chardev", "debugcon");
2080 static QEMUMachine
*machine_parse(const char *name
)
2082 QEMUMachine
*m
, *machine
= NULL
;
2085 machine
= find_machine(name
);
2090 printf("Supported machines are:\n");
2091 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2093 printf("%-10s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2095 printf("%-10s %s%s\n", m
->name
, m
->desc
,
2096 m
->is_default
? " (default)" : "");
2098 exit(!name
|| *name
!= '?');
2101 static int tcg_init(void)
2103 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2108 const char *opt_name
;
2110 int (*available
)(void);
2114 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2115 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2116 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2119 static int configure_accelerator(void)
2121 const char *p
= NULL
;
2124 bool accel_initalised
= 0;
2125 bool init_failed
= 0;
2127 QemuOptsList
*list
= qemu_find_opts("machine");
2128 if (!QTAILQ_EMPTY(&list
->head
)) {
2129 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2133 /* Use the default "accelerator", tcg */
2137 while (!accel_initalised
&& *p
!= '\0') {
2141 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2142 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2143 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2144 *(accel_list
[i
].allowed
) = 1;
2145 ret
= accel_list
[i
].init();
2148 if (!accel_list
[i
].available()) {
2149 printf("%s not supported for this target\n",
2150 accel_list
[i
].name
);
2152 fprintf(stderr
, "failed to initialize %s: %s\n",
2156 *(accel_list
[i
].allowed
) = 0;
2158 accel_initalised
= 1;
2163 if (i
== ARRAY_SIZE(accel_list
)) {
2164 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2168 if (!accel_initalised
) {
2169 fprintf(stderr
, "No accelerator found!\n");
2174 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2177 return !accel_initalised
;
2180 void qemu_add_exit_notifier(Notifier
*notify
)
2182 notifier_list_add(&exit_notifiers
, notify
);
2185 void qemu_remove_exit_notifier(Notifier
*notify
)
2187 notifier_list_remove(&exit_notifiers
, notify
);
2190 static void qemu_run_exit_notifiers(void)
2192 notifier_list_notify(&exit_notifiers
, NULL
);
2195 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2197 notifier_list_add(&machine_init_done_notifiers
, notify
);
2200 static void qemu_run_machine_init_done_notifiers(void)
2202 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2205 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2206 const char **poptarg
, int *poptind
)
2208 const QEMUOption
*popt
;
2209 int optind
= *poptind
;
2210 char *r
= argv
[optind
];
2213 loc_set_cmdline(argv
, optind
, 1);
2215 /* Treat --foo the same as -foo. */
2218 popt
= qemu_options
;
2221 error_report("invalid option");
2224 if (!strcmp(popt
->name
, r
+ 1))
2228 if (popt
->flags
& HAS_ARG
) {
2229 if (optind
>= argc
) {
2230 error_report("requires an argument");
2233 optarg
= argv
[optind
++];
2234 loc_set_cmdline(argv
, optind
- 2, 2);
2245 static gpointer
malloc_and_trace(gsize n_bytes
)
2247 void *ptr
= malloc(n_bytes
);
2248 trace_g_malloc(n_bytes
, ptr
);
2252 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2254 void *ptr
= realloc(mem
, n_bytes
);
2255 trace_g_realloc(mem
, n_bytes
, ptr
);
2259 static void free_and_trace(gpointer mem
)
2265 int main(int argc
, char **argv
, char **envp
)
2267 const char *gdbstub_dev
= NULL
;
2269 int snapshot
, linux_boot
;
2270 const char *icount_option
= NULL
;
2271 const char *initrd_filename
;
2272 const char *kernel_filename
, *kernel_cmdline
;
2273 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2275 DisplayChangeListener
*dcl
;
2276 int cyls
, heads
, secs
, translation
;
2277 QemuOpts
*hda_opts
= NULL
, *opts
;
2278 QemuOptsList
*olist
;
2281 const char *loadvm
= NULL
;
2282 QEMUMachine
*machine
;
2283 const char *cpu_model
;
2284 const char *pid_file
= NULL
;
2285 const char *incoming
= NULL
;
2287 int show_vnc_port
= 0;
2290 const char *log_mask
= NULL
;
2291 const char *log_file
= NULL
;
2292 GMemVTable mem_trace
= {
2293 .malloc
= malloc_and_trace
,
2294 .realloc
= realloc_and_trace
,
2295 .free
= free_and_trace
,
2297 const char *trace_events
= NULL
;
2298 const char *trace_file
= NULL
;
2300 atexit(qemu_run_exit_notifiers
);
2301 error_set_progname(argv
[0]);
2303 g_mem_set_vtable(&mem_trace
);
2304 g_thread_init(NULL
);
2310 qemu_cache_utils_init(envp
);
2312 QLIST_INIT (&vm_change_state_head
);
2313 os_setup_early_signal_handling();
2315 module_call_init(MODULE_INIT_MACHINE
);
2316 machine
= find_default_machine();
2318 initrd_filename
= NULL
;
2321 kernel_filename
= NULL
;
2322 kernel_cmdline
= "";
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
) {
2370 /* second pass of option parsing */
2375 if (argv
[optind
][0] != '-') {
2376 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2378 const QEMUOption
*popt
;
2380 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2381 if (!(popt
->arch_mask
& arch_type
)) {
2382 printf("Option %s not supported for this target\n", popt
->name
);
2385 switch(popt
->index
) {
2387 machine
= machine_parse(optarg
);
2389 case QEMU_OPTION_cpu
:
2390 /* hw initialization will check this */
2391 if (*optarg
== '?') {
2392 list_cpus(stdout
, &fprintf
, optarg
);
2398 case QEMU_OPTION_initrd
:
2399 initrd_filename
= optarg
;
2401 case QEMU_OPTION_hda
:
2405 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2407 snprintf(buf
, sizeof(buf
),
2408 "%s,cyls=%d,heads=%d,secs=%d%s",
2409 HD_OPTS
, cyls
, heads
, secs
,
2410 translation
== BIOS_ATA_TRANSLATION_LBA
?
2412 translation
== BIOS_ATA_TRANSLATION_NONE
?
2413 ",trans=none" : "");
2414 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2417 case QEMU_OPTION_hdb
:
2418 case QEMU_OPTION_hdc
:
2419 case QEMU_OPTION_hdd
:
2420 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2423 case QEMU_OPTION_drive
:
2424 if (drive_def(optarg
) == NULL
) {
2428 case QEMU_OPTION_set
:
2429 if (qemu_set_option(optarg
) != 0)
2432 case QEMU_OPTION_global
:
2433 if (qemu_global_option(optarg
) != 0)
2436 case QEMU_OPTION_mtdblock
:
2437 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2439 case QEMU_OPTION_sd
:
2440 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2442 case QEMU_OPTION_pflash
:
2443 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2445 case QEMU_OPTION_snapshot
:
2448 case QEMU_OPTION_hdachs
:
2452 cyls
= strtol(p
, (char **)&p
, 0);
2453 if (cyls
< 1 || cyls
> 16383)
2458 heads
= strtol(p
, (char **)&p
, 0);
2459 if (heads
< 1 || heads
> 16)
2464 secs
= strtol(p
, (char **)&p
, 0);
2465 if (secs
< 1 || secs
> 63)
2469 if (!strcmp(p
, "none"))
2470 translation
= BIOS_ATA_TRANSLATION_NONE
;
2471 else if (!strcmp(p
, "lba"))
2472 translation
= BIOS_ATA_TRANSLATION_LBA
;
2473 else if (!strcmp(p
, "auto"))
2474 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2477 } else if (*p
!= '\0') {
2479 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2482 if (hda_opts
!= NULL
) {
2484 snprintf(num
, sizeof(num
), "%d", cyls
);
2485 qemu_opt_set(hda_opts
, "cyls", num
);
2486 snprintf(num
, sizeof(num
), "%d", heads
);
2487 qemu_opt_set(hda_opts
, "heads", num
);
2488 snprintf(num
, sizeof(num
), "%d", secs
);
2489 qemu_opt_set(hda_opts
, "secs", num
);
2490 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2491 qemu_opt_set(hda_opts
, "trans", "lba");
2492 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2493 qemu_opt_set(hda_opts
, "trans", "none");
2497 case QEMU_OPTION_numa
:
2498 if (nb_numa_nodes
>= MAX_NODES
) {
2499 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2504 case QEMU_OPTION_display
:
2505 display_type
= select_display(optarg
);
2507 case QEMU_OPTION_nographic
:
2508 display_type
= DT_NOGRAPHIC
;
2510 case QEMU_OPTION_curses
:
2511 #ifdef CONFIG_CURSES
2512 display_type
= DT_CURSES
;
2514 fprintf(stderr
, "Curses support is disabled\n");
2518 case QEMU_OPTION_portrait
:
2519 graphic_rotate
= 90;
2521 case QEMU_OPTION_rotate
:
2522 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2523 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2524 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2526 "qemu: only 90, 180, 270 deg rotation is available\n");
2530 case QEMU_OPTION_kernel
:
2531 kernel_filename
= optarg
;
2533 case QEMU_OPTION_append
:
2534 kernel_cmdline
= optarg
;
2536 case QEMU_OPTION_cdrom
:
2537 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2539 case QEMU_OPTION_boot
:
2541 static const char * const params
[] = {
2542 "order", "once", "menu",
2543 "splash", "splash-time", NULL
2545 char buf
[sizeof(boot_devices
)];
2546 char *standard_boot_devices
;
2549 if (!strchr(optarg
, '=')) {
2551 pstrcpy(buf
, sizeof(buf
), optarg
);
2552 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2554 "qemu: unknown boot parameter '%s' in '%s'\n",
2560 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2561 validate_bootdevices(buf
);
2562 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2565 if (get_param_value(buf
, sizeof(buf
),
2567 validate_bootdevices(buf
);
2568 standard_boot_devices
= g_strdup(boot_devices
);
2569 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2570 qemu_register_reset(restore_boot_devices
,
2571 standard_boot_devices
);
2573 if (get_param_value(buf
, sizeof(buf
),
2575 if (!strcmp(buf
, "on")) {
2577 } else if (!strcmp(buf
, "off")) {
2581 "qemu: invalid option value '%s'\n",
2586 qemu_opts_parse(qemu_find_opts("boot-opts"),
2591 case QEMU_OPTION_fda
:
2592 case QEMU_OPTION_fdb
:
2593 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2596 case QEMU_OPTION_no_fd_bootchk
:
2599 case QEMU_OPTION_netdev
:
2600 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2604 case QEMU_OPTION_net
:
2605 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2610 case QEMU_OPTION_tftp
:
2611 legacy_tftp_prefix
= optarg
;
2613 case QEMU_OPTION_bootp
:
2614 legacy_bootp_filename
= optarg
;
2616 case QEMU_OPTION_redir
:
2617 if (net_slirp_redir(optarg
) < 0)
2621 case QEMU_OPTION_bt
:
2622 add_device_config(DEV_BT
, optarg
);
2624 case QEMU_OPTION_audio_help
:
2625 if (!(audio_available())) {
2626 printf("Option %s not supported for this target\n", popt
->name
);
2632 case QEMU_OPTION_soundhw
:
2633 if (!(audio_available())) {
2634 printf("Option %s not supported for this target\n", popt
->name
);
2637 select_soundhw (optarg
);
2642 case QEMU_OPTION_version
:
2646 case QEMU_OPTION_m
: {
2649 value
= strtosz(optarg
, NULL
);
2651 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2655 if (value
!= (uint64_t)(ram_addr_t
)value
) {
2656 fprintf(stderr
, "qemu: ram size too large\n");
2662 case QEMU_OPTION_mempath
:
2666 case QEMU_OPTION_mem_prealloc
:
2677 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
2679 case QEMU_OPTION_gdb
:
2680 gdbstub_dev
= optarg
;
2685 case QEMU_OPTION_bios
:
2688 case QEMU_OPTION_singlestep
:
2695 keyboard_layout
= optarg
;
2697 case QEMU_OPTION_localtime
:
2700 case QEMU_OPTION_vga
:
2701 select_vgahw (optarg
);
2708 w
= strtol(p
, (char **)&p
, 10);
2711 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2717 h
= strtol(p
, (char **)&p
, 10);
2722 depth
= strtol(p
, (char **)&p
, 10);
2723 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2724 depth
!= 24 && depth
!= 32)
2726 } else if (*p
== '\0') {
2727 depth
= graphic_depth
;
2734 graphic_depth
= depth
;
2737 case QEMU_OPTION_echr
:
2740 term_escape_char
= strtol(optarg
, &r
, 0);
2742 printf("Bad argument to echr\n");
2745 case QEMU_OPTION_monitor
:
2746 monitor_parse(optarg
, "readline");
2747 default_monitor
= 0;
2749 case QEMU_OPTION_qmp
:
2750 monitor_parse(optarg
, "control");
2751 default_monitor
= 0;
2753 case QEMU_OPTION_mon
:
2754 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
2758 default_monitor
= 0;
2760 case QEMU_OPTION_chardev
:
2761 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
2766 case QEMU_OPTION_fsdev
:
2767 olist
= qemu_find_opts("fsdev");
2769 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
2772 opts
= qemu_opts_parse(olist
, optarg
, 1);
2774 fprintf(stderr
, "parse error: %s\n", optarg
);
2778 case QEMU_OPTION_virtfs
: {
2782 olist
= qemu_find_opts("virtfs");
2784 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
2787 opts
= qemu_opts_parse(olist
, optarg
, 1);
2789 fprintf(stderr
, "parse error: %s\n", optarg
);
2793 if (qemu_opt_get(opts
, "fstype") == NULL
||
2794 qemu_opt_get(opts
, "mount_tag") == NULL
||
2795 qemu_opt_get(opts
, "path") == NULL
||
2796 qemu_opt_get(opts
, "security_model") == NULL
) {
2797 fprintf(stderr
, "Usage: -virtfs fstype,path=/share_path/,"
2798 "security_model=[mapped|passthrough|none],"
2799 "mount_tag=tag.\n");
2803 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
2804 qemu_opt_get(opts
, "mount_tag"), 1);
2806 fprintf(stderr
, "duplicate fsdev id: %s\n",
2807 qemu_opt_get(opts
, "mount_tag"));
2810 qemu_opt_set(fsdev
, "fstype", qemu_opt_get(opts
, "fstype"));
2811 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
2812 qemu_opt_set(fsdev
, "security_model",
2813 qemu_opt_get(opts
, "security_model"));
2815 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0);
2816 qemu_opt_set(device
, "driver", "virtio-9p-pci");
2817 qemu_opt_set(device
, "fsdev",
2818 qemu_opt_get(opts
, "mount_tag"));
2819 qemu_opt_set(device
, "mount_tag",
2820 qemu_opt_get(opts
, "mount_tag"));
2823 case QEMU_OPTION_serial
:
2824 add_device_config(DEV_SERIAL
, optarg
);
2826 if (strncmp(optarg
, "mon:", 4) == 0) {
2827 default_monitor
= 0;
2830 case QEMU_OPTION_watchdog
:
2833 "qemu: only one watchdog option may be given\n");
2838 case QEMU_OPTION_watchdog_action
:
2839 if (select_watchdog_action(optarg
) == -1) {
2840 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
2844 case QEMU_OPTION_virtiocon
:
2845 add_device_config(DEV_VIRTCON
, optarg
);
2846 default_virtcon
= 0;
2847 if (strncmp(optarg
, "mon:", 4) == 0) {
2848 default_monitor
= 0;
2851 case QEMU_OPTION_parallel
:
2852 add_device_config(DEV_PARALLEL
, optarg
);
2853 default_parallel
= 0;
2854 if (strncmp(optarg
, "mon:", 4) == 0) {
2855 default_monitor
= 0;
2858 case QEMU_OPTION_debugcon
:
2859 add_device_config(DEV_DEBUGCON
, optarg
);
2861 case QEMU_OPTION_loadvm
:
2864 case QEMU_OPTION_full_screen
:
2868 case QEMU_OPTION_no_frame
:
2871 case QEMU_OPTION_alt_grab
:
2874 case QEMU_OPTION_ctrl_grab
:
2877 case QEMU_OPTION_no_quit
:
2880 case QEMU_OPTION_sdl
:
2881 display_type
= DT_SDL
;
2884 case QEMU_OPTION_no_frame
:
2885 case QEMU_OPTION_alt_grab
:
2886 case QEMU_OPTION_ctrl_grab
:
2887 case QEMU_OPTION_no_quit
:
2888 case QEMU_OPTION_sdl
:
2889 fprintf(stderr
, "SDL support is disabled\n");
2892 case QEMU_OPTION_pidfile
:
2895 case QEMU_OPTION_win2k_hack
:
2896 win2k_install_hack
= 1;
2898 case QEMU_OPTION_rtc_td_hack
:
2901 case QEMU_OPTION_acpitable
:
2902 do_acpitable_option(optarg
);
2904 case QEMU_OPTION_smbios
:
2905 do_smbios_option(optarg
);
2907 case QEMU_OPTION_enable_kvm
:
2908 olist
= qemu_find_opts("machine");
2909 qemu_opts_reset(olist
);
2910 qemu_opts_parse(olist
, "accel=kvm", 0);
2912 case QEMU_OPTION_machine
:
2913 olist
= qemu_find_opts("machine");
2914 qemu_opts_reset(olist
);
2915 opts
= qemu_opts_parse(olist
, optarg
, 1);
2917 fprintf(stderr
, "parse error: %s\n", optarg
);
2920 optarg
= qemu_opt_get(opts
, "type");
2922 machine
= machine_parse(optarg
);
2925 case QEMU_OPTION_usb
:
2928 case QEMU_OPTION_usbdevice
:
2930 add_device_config(DEV_USB
, optarg
);
2932 case QEMU_OPTION_device
:
2933 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
2937 case QEMU_OPTION_smp
:
2940 fprintf(stderr
, "Invalid number of CPUs\n");
2943 if (max_cpus
< smp_cpus
) {
2944 fprintf(stderr
, "maxcpus must be equal to or greater than "
2948 if (max_cpus
> 255) {
2949 fprintf(stderr
, "Unsupported number of maxcpus\n");
2953 case QEMU_OPTION_vnc
:
2956 vnc_display
= optarg
;
2958 fprintf(stderr
, "VNC support is disabled\n");
2962 case QEMU_OPTION_no_acpi
:
2965 case QEMU_OPTION_no_hpet
:
2968 case QEMU_OPTION_balloon
:
2969 if (balloon_parse(optarg
) < 0) {
2970 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
2974 case QEMU_OPTION_no_reboot
:
2977 case QEMU_OPTION_no_shutdown
:
2980 case QEMU_OPTION_show_cursor
:
2983 case QEMU_OPTION_uuid
:
2984 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
2985 fprintf(stderr
, "Fail to parse UUID string."
2986 " Wrong format.\n");
2990 case QEMU_OPTION_option_rom
:
2991 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
2992 fprintf(stderr
, "Too many option ROMs\n");
2995 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
2996 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
2997 option_rom
[nb_option_roms
].bootindex
=
2998 qemu_opt_get_number(opts
, "bootindex", -1);
2999 if (!option_rom
[nb_option_roms
].name
) {
3000 fprintf(stderr
, "Option ROM file is not specified\n");
3005 case QEMU_OPTION_semihosting
:
3006 semihosting_enabled
= 1;
3008 case QEMU_OPTION_name
:
3009 qemu_name
= g_strdup(optarg
);
3011 char *p
= strchr(qemu_name
, ',');
3014 if (strncmp(p
, "process=", 8)) {
3015 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3019 os_set_proc_name(p
);
3023 case QEMU_OPTION_prom_env
:
3024 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3025 fprintf(stderr
, "Too many prom variables\n");
3028 prom_envs
[nb_prom_envs
] = optarg
;
3031 case QEMU_OPTION_old_param
:
3034 case QEMU_OPTION_clock
:
3035 configure_alarms(optarg
);
3037 case QEMU_OPTION_startdate
:
3038 configure_rtc_date_offset(optarg
, 1);
3040 case QEMU_OPTION_rtc
:
3041 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3045 configure_rtc(opts
);
3047 case QEMU_OPTION_tb_size
:
3048 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3049 if (tcg_tb_size
< 0) {
3053 case QEMU_OPTION_icount
:
3054 icount_option
= optarg
;
3056 case QEMU_OPTION_incoming
:
3059 case QEMU_OPTION_nodefaults
:
3061 default_parallel
= 0;
3062 default_virtcon
= 0;
3063 default_monitor
= 0;
3070 case QEMU_OPTION_xen_domid
:
3071 if (!(xen_available())) {
3072 printf("Option %s not supported for this target\n", popt
->name
);
3075 xen_domid
= atoi(optarg
);
3077 case QEMU_OPTION_xen_create
:
3078 if (!(xen_available())) {
3079 printf("Option %s not supported for this target\n", popt
->name
);
3082 xen_mode
= XEN_CREATE
;
3084 case QEMU_OPTION_xen_attach
:
3085 if (!(xen_available())) {
3086 printf("Option %s not supported for this target\n", popt
->name
);
3089 xen_mode
= XEN_ATTACH
;
3091 case QEMU_OPTION_trace
:
3093 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3097 trace_events
= qemu_opt_get(opts
, "events");
3098 trace_file
= qemu_opt_get(opts
, "file");
3101 case QEMU_OPTION_readconfig
:
3103 int ret
= qemu_read_config_file(optarg
);
3105 fprintf(stderr
, "read config %s: %s\n", optarg
,
3111 case QEMU_OPTION_spice
:
3112 olist
= qemu_find_opts("spice");
3114 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3117 opts
= qemu_opts_parse(olist
, optarg
, 0);
3119 fprintf(stderr
, "parse error: %s\n", optarg
);
3123 case QEMU_OPTION_writeconfig
:
3126 if (strcmp(optarg
, "-") == 0) {
3129 fp
= fopen(optarg
, "w");
3131 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3135 qemu_config_write(fp
);
3140 os_parse_cmd_args(popt
->index
, optarg
);
3146 /* Open the logfile at this point, if necessary. We can't open the logfile
3147 * when encountering either of the logging options (-d or -D) because the
3148 * other one may be encountered later on the command line, changing the
3149 * location or level of logging.
3153 set_cpu_log_filename(log_file
);
3155 set_cpu_log(log_mask
);
3158 if (!trace_backend_init(trace_events
, trace_file
)) {
3162 /* If no data_dir is specified then try to find it relative to the
3165 data_dir
= os_find_datadir(argv
[0]);
3167 /* If all else fails use the install path specified when building. */
3169 data_dir
= CONFIG_QEMU_DATADIR
;
3173 * Default to max_cpus = smp_cpus, in case the user doesn't
3174 * specify a max_cpus value.
3177 max_cpus
= smp_cpus
;
3179 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3180 if (smp_cpus
> machine
->max_cpus
) {
3181 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3182 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3188 * Get the default machine options from the machine if it is not already
3189 * specified either by the configuration file or by the command line.
3191 if (machine
->default_machine_opts
) {
3192 QemuOptsList
*list
= qemu_find_opts("machine");
3193 const char *p
= NULL
;
3195 if (!QTAILQ_EMPTY(&list
->head
)) {
3196 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
3199 qemu_opts_reset(list
);
3200 opts
= qemu_opts_parse(list
, machine
->default_machine_opts
, 0);
3202 fprintf(stderr
, "parse error for machine %s: %s\n",
3203 machine
->name
, machine
->default_machine_opts
);
3209 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3210 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3212 if (machine
->no_serial
) {
3215 if (machine
->no_parallel
) {
3216 default_parallel
= 0;
3218 if (!machine
->use_virtcon
) {
3219 default_virtcon
= 0;
3221 if (machine
->no_vga
) {
3224 if (machine
->no_floppy
) {
3227 if (machine
->no_cdrom
) {
3230 if (machine
->no_sdcard
) {
3234 if (display_type
== DT_NOGRAPHIC
) {
3235 if (default_parallel
)
3236 add_device_config(DEV_PARALLEL
, "null");
3237 if (default_serial
&& default_monitor
) {
3238 add_device_config(DEV_SERIAL
, "mon:stdio");
3239 } else if (default_virtcon
&& default_monitor
) {
3240 add_device_config(DEV_VIRTCON
, "mon:stdio");
3243 add_device_config(DEV_SERIAL
, "stdio");
3244 if (default_virtcon
)
3245 add_device_config(DEV_VIRTCON
, "stdio");
3246 if (default_monitor
)
3247 monitor_parse("stdio", "readline");
3251 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3252 if (default_parallel
)
3253 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3254 if (default_monitor
)
3255 monitor_parse("vc:80Cx24C", "readline");
3256 if (default_virtcon
)
3257 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3260 vga_interface_type
= VGA_CIRRUS
;
3264 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3266 #ifdef CONFIG_VIRTFS
3267 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3274 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3279 /* init the memory */
3280 if (ram_size
== 0) {
3281 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3284 configure_accelerator();
3286 if (qemu_init_main_loop()) {
3287 fprintf(stderr
, "qemu_init_main_loop failed\n");
3290 linux_boot
= (kernel_filename
!= NULL
);
3292 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3293 fprintf(stderr
, "-append only allowed with -kernel option\n");
3297 if (!linux_boot
&& initrd_filename
!= NULL
) {
3298 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3302 os_set_line_buffering();
3304 if (init_timer_alarm() < 0) {
3305 fprintf(stderr
, "could not initialize alarm timer\n");
3308 configure_icount(icount_option
);
3310 if (net_init_clients() < 0) {
3314 /* init the bluetooth world */
3315 if (foreach_device_config(DEV_BT
, bt_parse
))
3318 if (!xen_enabled()) {
3319 /* On 32-bit hosts, QEMU is limited by virtual address space */
3320 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3321 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3326 cpu_exec_init_all();
3328 bdrv_init_with_whitelist();
3332 /* open the virtual block devices */
3334 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3335 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
, &machine
->use_scsi
, 1) != 0)
3338 default_drive(default_cdrom
, snapshot
, machine
->use_scsi
,
3339 IF_DEFAULT
, 2, CDROM_OPTS
);
3340 default_drive(default_floppy
, snapshot
, machine
->use_scsi
,
3341 IF_FLOPPY
, 0, FD_OPTS
);
3342 default_drive(default_sdcard
, snapshot
, machine
->use_scsi
,
3345 register_savevm_live(NULL
, "ram", 0, 4, NULL
, ram_save_live
, NULL
,
3348 if (nb_numa_nodes
> 0) {
3351 if (nb_numa_nodes
> MAX_NODES
) {
3352 nb_numa_nodes
= MAX_NODES
;
3355 /* If no memory size if given for any node, assume the default case
3356 * and distribute the available memory equally across all nodes
3358 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3359 if (node_mem
[i
] != 0)
3362 if (i
== nb_numa_nodes
) {
3363 uint64_t usedmem
= 0;
3365 /* On Linux, the each node's border has to be 8MB aligned,
3366 * the final node gets the rest.
3368 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3369 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3370 usedmem
+= node_mem
[i
];
3372 node_mem
[i
] = ram_size
- usedmem
;
3375 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3376 if (node_cpumask
[i
] != 0)
3379 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3380 * must cope with this anyway, because there are BIOSes out there in
3381 * real machines which also use this scheme.
3383 if (i
== nb_numa_nodes
) {
3384 for (i
= 0; i
< smp_cpus
; i
++) {
3385 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
3390 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3394 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3396 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3398 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3400 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3403 module_call_init(MODULE_INIT_DEVICE
);
3405 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0) != 0)
3409 i
= select_watchdog(watchdog
);
3411 exit (i
== 1 ? 1 : 0);
3414 if (machine
->compat_props
) {
3415 qdev_prop_register_global_list(machine
->compat_props
);
3419 machine
->init(ram_size
, boot_devices
,
3420 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
3422 cpu_synchronize_all_post_init();
3426 current_machine
= machine
;
3428 /* init USB devices */
3430 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3434 /* init generic devices */
3435 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3438 net_check_clients();
3440 /* just use the first displaystate for the moment */
3441 ds
= get_displaystate();
3445 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3446 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3447 display_type
= DT_SDL
;
3448 #elif defined(CONFIG_VNC)
3449 vnc_display
= "localhost:0,to=99";
3452 display_type
= DT_NONE
;
3457 /* init local displays */
3458 switch (display_type
) {
3461 #if defined(CONFIG_CURSES)
3463 curses_display_init(ds
, full_screen
);
3466 #if defined(CONFIG_SDL)
3468 sdl_display_init(ds
, full_screen
, no_frame
);
3470 #elif defined(CONFIG_COCOA)
3472 cocoa_display_init(ds
, full_screen
);
3479 /* must be after terminal init, SDL library changes signal handlers */
3480 os_setup_signal_handling();
3483 /* init remote displays */
3485 vnc_display_init(ds
);
3486 if (vnc_display_open(ds
, vnc_display
) < 0)
3489 if (show_vnc_port
) {
3490 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3495 if (using_spice
&& !qxl_enabled
) {
3496 qemu_spice_display_init(ds
);
3502 dcl
= ds
->listeners
;
3503 while (dcl
!= NULL
) {
3504 if (dcl
->dpy_refresh
!= NULL
) {
3505 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
3506 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
3511 text_consoles_set_display(ds
);
3513 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
3514 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
3519 qdev_machine_creation_done();
3521 if (rom_load_all() != 0) {
3522 fprintf(stderr
, "rom loading failed\n");
3526 /* TODO: once all bus devices are qdevified, this should be done
3527 * when bus is created by qdev.c */
3528 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3529 qemu_run_machine_init_done_notifiers();
3531 qemu_system_reset(VMRESET_SILENT
);
3533 if (load_vmstate(loadvm
) < 0) {
3539 runstate_set(RUN_STATE_INMIGRATE
);
3540 int ret
= qemu_start_incoming_migration(incoming
);
3542 fprintf(stderr
, "Migration failed. Exit code %s(%d), exiting.\n",
3546 } else if (autostart
) {