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
31 #include "qemu/bitmap.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
68 #include "sysemu/seccomp.h"
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
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"
132 #include "monitor/monitor.h"
133 #include "ui/console.h"
134 #include "sysemu/sysemu.h"
135 #include "exec/gdbstub.h"
136 #include "qemu/timer.h"
137 #include "char/char.h"
138 #include "qemu/cache-utils.h"
139 #include "sysemu/blockdev.h"
140 #include "hw/block-common.h"
141 #include "migration/block.h"
142 #include "sysemu/dma.h"
143 #include "audio/audio.h"
144 #include "migration/migration.h"
145 #include "sysemu/kvm.h"
146 #include "qapi/qmp/qjson.h"
147 #include "qemu/option.h"
148 #include "qemu/config-file.h"
149 #include "qemu-options.h"
150 #include "qmp-commands.h"
151 #include "qemu/main-loop.h"
153 #include "fsdev/qemu-fsdev.h"
155 #include "sysemu/qtest.h"
157 #include "disas/disas.h"
159 #include "qemu/sockets.h"
161 #include "slirp/libslirp.h"
164 #include "trace/control.h"
165 #include "qemu/queue.h"
166 #include "sysemu/cpus.h"
167 #include "sysemu/arch_init.h"
168 #include "qemu/osdep.h"
170 #include "ui/qemu-spice.h"
171 #include "qapi/string-input-visitor.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 #define MAX_VIRTIO_CONSOLES 1
180 static const char *data_dir
;
181 const char *bios_name
= NULL
;
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 DisplayType display_type
= DT_DEFAULT
;
184 static int display_remote
;
185 const char* keyboard_layout
= NULL
;
187 const char *mem_path
= NULL
;
189 int mem_prealloc
= 0; /* force preallocation of physical target memory */
192 NICInfo nd_table
[MAX_NICS
];
194 static int rtc_utc
= 1;
195 static int rtc_date_offset
= -1; /* -1 means no change */
196 QEMUClock
*rtc_clock
;
197 int vga_interface_type
= VGA_NONE
;
198 static int full_screen
= 0;
200 static int no_frame
= 0;
203 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
204 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
205 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
206 int win2k_install_hack
= 0;
213 const char *vnc_display
;
215 int acpi_enabled
= 1;
218 static int no_reboot
;
221 int graphic_rotate
= 0;
222 const char *watchdog
;
223 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
225 int semihosting_enabled
= 0;
227 const char *qemu_name
;
230 unsigned int nb_prom_envs
= 0;
231 const char *prom_envs
[MAX_PROM_ENVS
];
233 uint8_t *boot_splash_filedata
;
234 int boot_splash_filedata_size
;
235 uint8_t qemu_extra_params_fw
[2];
237 typedef struct FWBootEntry FWBootEntry
;
240 QTAILQ_ENTRY(FWBootEntry
) link
;
246 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
247 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
250 uint64_t node_mem
[MAX_NODES
];
251 unsigned long *node_cpumask
[MAX_NODES
];
253 uint8_t qemu_uuid
[16];
255 static QEMUBootSetHandler
*boot_set_handler
;
256 static void *boot_set_opaque
;
258 static NotifierList exit_notifiers
=
259 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
261 static NotifierList machine_init_done_notifiers
=
262 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
264 static int tcg_allowed
= 1;
268 enum xen_mode xen_mode
= XEN_EMULATE
;
269 static int tcg_tb_size
;
271 static int default_serial
= 1;
272 static int default_parallel
= 1;
273 static int default_virtcon
= 1;
274 static int default_monitor
= 1;
275 static int default_floppy
= 1;
276 static int default_cdrom
= 1;
277 static int default_sdcard
= 1;
278 static int default_vga
= 1;
284 { .driver
= "isa-serial", .flag
= &default_serial
},
285 { .driver
= "isa-parallel", .flag
= &default_parallel
},
286 { .driver
= "isa-fdc", .flag
= &default_floppy
},
287 { .driver
= "ide-cd", .flag
= &default_cdrom
},
288 { .driver
= "ide-hd", .flag
= &default_cdrom
},
289 { .driver
= "ide-drive", .flag
= &default_cdrom
},
290 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
291 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
292 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
293 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
294 { .driver
= "VGA", .flag
= &default_vga
},
295 { .driver
= "isa-vga", .flag
= &default_vga
},
296 { .driver
= "cirrus-vga", .flag
= &default_vga
},
297 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
298 { .driver
= "vmware-svga", .flag
= &default_vga
},
299 { .driver
= "qxl-vga", .flag
= &default_vga
},
302 const char *qemu_get_vm_name(void)
307 static void res_free(void)
309 if (boot_splash_filedata
!= NULL
) {
310 g_free(boot_splash_filedata
);
311 boot_splash_filedata
= NULL
;
315 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
317 const char *driver
= qemu_opt_get(opts
, "driver");
322 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
323 if (strcmp(default_list
[i
].driver
, driver
) != 0)
325 *(default_list
[i
].flag
) = 0;
330 /***********************************************************/
333 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
338 } RunStateTransition
;
340 static const RunStateTransition runstate_transitions_def
[] = {
342 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
344 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
345 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
347 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
348 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
350 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
351 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
353 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
354 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
356 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
357 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
359 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
360 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
361 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
363 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
364 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
366 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
368 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
369 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
370 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
371 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
372 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
373 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
374 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
375 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
376 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
378 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
380 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
381 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
383 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
384 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
385 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
386 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
388 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
389 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
391 { RUN_STATE_MAX
, RUN_STATE_MAX
},
394 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
396 bool runstate_check(RunState state
)
398 return current_run_state
== state
;
401 static void runstate_init(void)
403 const RunStateTransition
*p
;
405 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
407 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
408 runstate_valid_transitions
[p
->from
][p
->to
] = true;
412 /* This function will abort() on invalid state transitions */
413 void runstate_set(RunState new_state
)
415 assert(new_state
< RUN_STATE_MAX
);
417 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
418 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
419 RunState_lookup
[current_run_state
],
420 RunState_lookup
[new_state
]);
424 current_run_state
= new_state
;
427 int runstate_is_running(void)
429 return runstate_check(RUN_STATE_RUNNING
);
432 StatusInfo
*qmp_query_status(Error
**errp
)
434 StatusInfo
*info
= g_malloc0(sizeof(*info
));
436 info
->running
= runstate_is_running();
437 info
->singlestep
= singlestep
;
438 info
->status
= current_run_state
;
443 /***********************************************************/
444 /* real time host monotonic timer */
446 /***********************************************************/
447 /* host time/date access */
448 void qemu_get_timedate(struct tm
*tm
, int offset
)
455 if (rtc_date_offset
== -1) {
459 ret
= localtime(&ti
);
461 ti
-= rtc_date_offset
;
465 memcpy(tm
, ret
, sizeof(struct tm
));
468 int qemu_timedate_diff(struct tm
*tm
)
472 if (rtc_date_offset
== -1)
474 seconds
= mktimegm(tm
);
477 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
478 seconds
= mktime(&tmp
);
481 seconds
= mktimegm(tm
) + rtc_date_offset
;
483 return seconds
- time(NULL
);
486 void rtc_change_mon_event(struct tm
*tm
)
490 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
491 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
492 qobject_decref(data
);
495 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
497 time_t rtc_start_date
;
500 if (!strcmp(startdate
, "now") && legacy
) {
501 rtc_date_offset
= -1;
503 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
511 } else if (sscanf(startdate
, "%d-%d-%d",
523 rtc_start_date
= mktimegm(&tm
);
524 if (rtc_start_date
== -1) {
526 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
527 "'2006-06-17T16:01:21' or '2006-06-17'\n");
530 rtc_date_offset
= time(NULL
) - rtc_start_date
;
534 static void configure_rtc(QemuOpts
*opts
)
538 value
= qemu_opt_get(opts
, "base");
540 if (!strcmp(value
, "utc")) {
542 } else if (!strcmp(value
, "localtime")) {
545 configure_rtc_date_offset(value
, 0);
548 value
= qemu_opt_get(opts
, "clock");
550 if (!strcmp(value
, "host")) {
551 rtc_clock
= host_clock
;
552 } else if (!strcmp(value
, "rt")) {
553 rtc_clock
= rt_clock
;
554 } else if (!strcmp(value
, "vm")) {
555 rtc_clock
= vm_clock
;
557 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
561 value
= qemu_opt_get(opts
, "driftfix");
563 if (!strcmp(value
, "slew")) {
564 static GlobalProperty slew_lost_ticks
[] = {
566 .driver
= "mc146818rtc",
567 .property
= "lost_tick_policy",
570 { /* end of list */ }
573 qdev_prop_register_global_list(slew_lost_ticks
);
574 } else if (!strcmp(value
, "none")) {
575 /* discard is default */
577 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
583 /***********************************************************/
584 /* Bluetooth support */
587 static struct HCIInfo
*hci_table
[MAX_NICS
];
589 static struct bt_vlan_s
{
590 struct bt_scatternet_s net
;
592 struct bt_vlan_s
*next
;
595 /* find or alloc a new bluetooth "VLAN" */
596 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
598 struct bt_vlan_s
**pvlan
, *vlan
;
599 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
603 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
605 pvlan
= &first_bt_vlan
;
606 while (*pvlan
!= NULL
)
607 pvlan
= &(*pvlan
)->next
;
612 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
616 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
621 static struct HCIInfo null_hci
= {
622 .cmd_send
= null_hci_send
,
623 .sco_send
= null_hci_send
,
624 .acl_send
= null_hci_send
,
625 .bdaddr_set
= null_hci_addr_set
,
628 struct HCIInfo
*qemu_next_hci(void)
630 if (cur_hci
== nb_hcis
)
633 return hci_table
[cur_hci
++];
636 static struct HCIInfo
*hci_init(const char *str
)
639 struct bt_scatternet_s
*vlan
= 0;
641 if (!strcmp(str
, "null"))
644 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
646 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
647 else if (!strncmp(str
, "hci", 3)) {
650 if (!strncmp(str
+ 3, ",vlan=", 6)) {
651 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
656 vlan
= qemu_find_bt_vlan(0);
658 return bt_new_hci(vlan
);
661 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
666 static int bt_hci_parse(const char *str
)
671 if (nb_hcis
>= MAX_NICS
) {
672 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
685 bdaddr
.b
[5] = 0x56 + nb_hcis
;
686 hci
->bdaddr_set(hci
, bdaddr
.b
);
688 hci_table
[nb_hcis
++] = hci
;
693 static void bt_vhci_add(int vlan_id
)
695 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
698 fprintf(stderr
, "qemu: warning: adding a VHCI to "
699 "an empty scatternet %i\n", vlan_id
);
701 bt_vhci_init(bt_new_hci(vlan
));
704 static struct bt_device_s
*bt_device_add(const char *opt
)
706 struct bt_scatternet_s
*vlan
;
708 char *endp
= strstr(opt
, ",vlan=");
709 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
712 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
715 vlan_id
= strtol(endp
+ 6, &endp
, 0);
717 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
722 vlan
= qemu_find_bt_vlan(vlan_id
);
725 fprintf(stderr
, "qemu: warning: adding a slave device to "
726 "an empty scatternet %i\n", vlan_id
);
728 if (!strcmp(devname
, "keyboard"))
729 return bt_keyboard_init(vlan
);
731 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
735 static int bt_parse(const char *opt
)
737 const char *endp
, *p
;
740 if (strstart(opt
, "hci", &endp
)) {
741 if (!*endp
|| *endp
== ',') {
743 if (!strstart(endp
, ",vlan=", 0))
746 return bt_hci_parse(opt
);
748 } else if (strstart(opt
, "vhci", &endp
)) {
749 if (!*endp
|| *endp
== ',') {
751 if (strstart(endp
, ",vlan=", &p
)) {
752 vlan
= strtol(p
, (char **) &endp
, 0);
754 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
758 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
767 } else if (strstart(opt
, "device:", &endp
))
768 return !bt_device_add(endp
);
770 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
774 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
776 /* FIXME: change this to true for 1.3 */
777 if (qemu_opt_get_bool(opts
, "enable", false)) {
778 #ifdef CONFIG_SECCOMP
779 if (seccomp_start() < 0) {
780 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
781 "failed to install seccomp syscall filter in the kernel");
785 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
786 "sandboxing request but seccomp is not compiled into this build");
794 /*********QEMU USB setting******/
795 bool usb_enabled(bool default_usb
)
798 mach_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
800 return qemu_opt_get_bool(mach_opts
, "usb", default_usb
);
806 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
808 int fd
, dupfd
, flags
;
810 const char *fd_opaque
= NULL
;
812 fd
= qemu_opt_get_number(opts
, "fd", -1);
813 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
814 fd_opaque
= qemu_opt_get(opts
, "opaque");
817 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
818 "fd option is required and must be non-negative");
822 if (fd
<= STDERR_FILENO
) {
823 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
824 "fd cannot be a standard I/O stream");
829 * All fds inherited across exec() necessarily have FD_CLOEXEC
830 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
832 flags
= fcntl(fd
, F_GETFD
);
833 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
834 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
835 "fd is not valid or already in use");
840 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
841 "set option is required and must be non-negative");
845 #ifdef F_DUPFD_CLOEXEC
846 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
850 qemu_set_cloexec(dupfd
);
854 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
855 "Error duplicating fd: %s", strerror(errno
));
859 /* add the duplicate fd, and optionally the opaque string, to the fd set */
860 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
866 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
870 fd
= qemu_opt_get_number(opts
, "fd", -1);
877 /***********************************************************/
878 /* QEMU Block devices */
880 #define HD_OPTS "media=disk"
881 #define CDROM_OPTS "media=cdrom"
883 #define PFLASH_OPTS ""
887 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
889 BlockInterfaceType
*block_default_type
= opaque
;
891 return drive_init(opts
, *block_default_type
) == NULL
;
894 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
896 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
897 qemu_opt_set(opts
, "snapshot", "on");
902 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
903 int index
, const char *optstr
)
907 if (!enable
|| drive_get_by_index(type
, index
)) {
911 opts
= drive_add(type
, index
, NULL
, optstr
);
913 drive_enable_snapshot(opts
, NULL
);
915 if (!drive_init(opts
, type
)) {
920 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
922 boot_set_handler
= func
;
923 boot_set_opaque
= opaque
;
926 int qemu_boot_set(const char *boot_devices
)
928 if (!boot_set_handler
) {
931 return boot_set_handler(boot_set_opaque
, boot_devices
);
934 static void validate_bootdevices(char *devices
)
936 /* We just do some generic consistency checks */
940 for (p
= devices
; *p
!= '\0'; p
++) {
941 /* Allowed boot devices are:
942 * a-b: floppy disk drives
943 * c-f: IDE disk drives
944 * g-m: machine implementation dependent drives
945 * n-p: network devices
946 * It's up to each machine implementation to check if the given boot
947 * devices match the actual hardware implementation and firmware
950 if (*p
< 'a' || *p
> 'p') {
951 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
954 if (bitmap
& (1 << (*p
- 'a'))) {
955 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
958 bitmap
|= 1 << (*p
- 'a');
962 static void restore_boot_devices(void *opaque
)
964 char *standard_boot_devices
= opaque
;
965 static int first
= 1;
967 /* Restore boot order and remove ourselves after the first boot */
973 qemu_boot_set(standard_boot_devices
);
975 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
976 g_free(standard_boot_devices
);
979 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
982 FWBootEntry
*node
, *i
;
988 assert(dev
!= NULL
|| suffix
!= NULL
);
990 node
= g_malloc0(sizeof(FWBootEntry
));
991 node
->bootindex
= bootindex
;
992 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
995 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
996 if (i
->bootindex
== bootindex
) {
997 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
999 } else if (i
->bootindex
< bootindex
) {
1002 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1005 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1009 * This function returns null terminated string that consist of new line
1010 * separated device paths.
1012 * memory pointed by "size" is assigned total length of the array in bytes
1015 char *get_boot_devices_list(uint32_t *size
)
1021 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1022 char *devpath
= NULL
, *bootpath
;
1026 devpath
= qdev_get_fw_dev_path(i
->dev
);
1030 if (i
->suffix
&& devpath
) {
1031 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1033 bootpath
= g_malloc(bootpathlen
);
1034 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1036 } else if (devpath
) {
1040 bootpath
= g_strdup(i
->suffix
);
1044 list
[total
-1] = '\n';
1046 len
= strlen(bootpath
) + 1;
1047 list
= g_realloc(list
, total
+ len
);
1048 memcpy(&list
[total
], bootpath
, len
);
1058 static void numa_add(const char *optarg
)
1062 unsigned long long value
, endvalue
;
1065 value
= endvalue
= 0ULL;
1067 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1068 if (!strcmp(option
, "node")) {
1069 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1070 nodenr
= nb_numa_nodes
;
1072 nodenr
= strtoull(option
, NULL
, 10);
1075 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1076 node_mem
[nodenr
] = 0;
1079 sval
= strtosz(option
, &endptr
);
1080 if (sval
< 0 || *endptr
) {
1081 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
1084 node_mem
[nodenr
] = sval
;
1086 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
1087 value
= strtoull(option
, &endptr
, 10);
1088 if (*endptr
== '-') {
1089 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1094 if (!(endvalue
< MAX_CPUMASK_BITS
)) {
1095 endvalue
= MAX_CPUMASK_BITS
- 1;
1097 "A max of %d CPUs are supported in a guest\n",
1101 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
1107 static void smp_parse(const char *optarg
)
1109 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1113 smp
= strtoul(optarg
, &endptr
, 10);
1114 if (endptr
!= optarg
) {
1115 if (*endptr
== ',') {
1119 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1120 sockets
= strtoull(option
, NULL
, 10);
1121 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1122 cores
= strtoull(option
, NULL
, 10);
1123 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1124 threads
= strtoull(option
, NULL
, 10);
1125 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1126 max_cpus
= strtoull(option
, NULL
, 10);
1128 /* compute missing values, prefer sockets over cores over threads */
1129 if (smp
== 0 || sockets
== 0) {
1130 sockets
= sockets
> 0 ? sockets
: 1;
1131 cores
= cores
> 0 ? cores
: 1;
1132 threads
= threads
> 0 ? threads
: 1;
1134 smp
= cores
* threads
* sockets
;
1138 threads
= threads
> 0 ? threads
: 1;
1139 cores
= smp
/ (sockets
* threads
);
1141 threads
= smp
/ (cores
* sockets
);
1145 smp_cores
= cores
> 0 ? cores
: 1;
1146 smp_threads
= threads
> 0 ? threads
: 1;
1148 max_cpus
= smp_cpus
;
1151 /***********************************************************/
1154 static int usb_device_add(const char *devname
)
1157 USBDevice
*dev
= NULL
;
1159 if (!usb_enabled(false)) {
1163 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1164 dev
= usbdevice_create(devname
);
1168 /* the other ones */
1169 #ifndef CONFIG_LINUX
1170 /* only the linux version is qdev-ified, usb-bsd still needs this */
1171 if (strstart(devname
, "host:", &p
)) {
1172 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1175 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1176 dev
= usb_bt_init(usb_bus_find(-1),
1177 devname
[2] ? hci_init(p
)
1178 : bt_new_hci(qemu_find_bt_vlan(0)));
1189 static int usb_device_del(const char *devname
)
1194 if (strstart(devname
, "host:", &p
))
1195 return usb_host_device_close(p
);
1197 if (!usb_enabled(false)) {
1201 p
= strchr(devname
, '.');
1204 bus_num
= strtoul(devname
, NULL
, 0);
1205 addr
= strtoul(p
+ 1, NULL
, 0);
1207 return usb_device_delete_addr(bus_num
, addr
);
1210 static int usb_parse(const char *cmdline
)
1213 r
= usb_device_add(cmdline
);
1215 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1220 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1222 const char *devname
= qdict_get_str(qdict
, "devname");
1223 if (usb_device_add(devname
) < 0) {
1224 error_report("could not add USB device '%s'", devname
);
1228 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1230 const char *devname
= qdict_get_str(qdict
, "devname");
1231 if (usb_device_del(devname
) < 0) {
1232 error_report("could not delete USB device '%s'", devname
);
1236 /***********************************************************/
1237 /* PCMCIA/Cardbus */
1239 static struct pcmcia_socket_entry_s
{
1240 PCMCIASocket
*socket
;
1241 struct pcmcia_socket_entry_s
*next
;
1242 } *pcmcia_sockets
= 0;
1244 void pcmcia_socket_register(PCMCIASocket
*socket
)
1246 struct pcmcia_socket_entry_s
*entry
;
1248 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1249 entry
->socket
= socket
;
1250 entry
->next
= pcmcia_sockets
;
1251 pcmcia_sockets
= entry
;
1254 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1256 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1258 ptr
= &pcmcia_sockets
;
1259 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1260 if (entry
->socket
== socket
) {
1266 void pcmcia_info(Monitor
*mon
)
1268 struct pcmcia_socket_entry_s
*iter
;
1270 if (!pcmcia_sockets
)
1271 monitor_printf(mon
, "No PCMCIA sockets\n");
1273 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1274 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1275 iter
->socket
->attached
? iter
->socket
->card_string
:
1279 /***********************************************************/
1280 /* machine registration */
1282 static QEMUMachine
*first_machine
= NULL
;
1283 QEMUMachine
*current_machine
= NULL
;
1285 int qemu_register_machine(QEMUMachine
*m
)
1288 pm
= &first_machine
;
1296 static QEMUMachine
*find_machine(const char *name
)
1300 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1301 if (!strcmp(m
->name
, name
))
1303 if (m
->alias
&& !strcmp(m
->alias
, name
))
1309 QEMUMachine
*find_default_machine(void)
1313 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1314 if (m
->is_default
) {
1321 MachineInfoList
*qmp_query_machines(Error
**errp
)
1323 MachineInfoList
*mach_list
= NULL
;
1326 for (m
= first_machine
; m
; m
= m
->next
) {
1327 MachineInfoList
*entry
;
1330 info
= g_malloc0(sizeof(*info
));
1331 if (m
->is_default
) {
1332 info
->has_is_default
= true;
1333 info
->is_default
= true;
1337 info
->has_alias
= true;
1338 info
->alias
= g_strdup(m
->alias
);
1341 info
->name
= g_strdup(m
->name
);
1343 entry
= g_malloc0(sizeof(*entry
));
1344 entry
->value
= info
;
1345 entry
->next
= mach_list
;
1352 /***********************************************************/
1353 /* main execution loop */
1355 static void gui_update(void *opaque
)
1357 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1358 DisplayState
*ds
= opaque
;
1359 DisplayChangeListener
*dcl
;
1363 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1364 if (dcl
->gui_timer_interval
&&
1365 dcl
->gui_timer_interval
< interval
)
1366 interval
= dcl
->gui_timer_interval
;
1368 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1371 void gui_setup_refresh(DisplayState
*ds
)
1373 DisplayChangeListener
*dcl
;
1374 bool need_timer
= false;
1375 bool have_gfx
= false;
1376 bool have_text
= false;
1378 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1379 if (dcl
->dpy_refresh
!= NULL
) {
1382 if (dcl
->dpy_gfx_update
!= NULL
) {
1385 if (dcl
->dpy_text_update
!= NULL
) {
1390 if (need_timer
&& ds
->gui_timer
== NULL
) {
1391 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
1392 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
1394 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
1395 qemu_del_timer(ds
->gui_timer
);
1396 qemu_free_timer(ds
->gui_timer
);
1397 ds
->gui_timer
= NULL
;
1400 ds
->have_gfx
= have_gfx
;
1401 ds
->have_text
= have_text
;
1404 struct vm_change_state_entry
{
1405 VMChangeStateHandler
*cb
;
1407 QLIST_ENTRY (vm_change_state_entry
) entries
;
1410 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1412 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1415 VMChangeStateEntry
*e
;
1417 e
= g_malloc0(sizeof (*e
));
1421 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1425 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1427 QLIST_REMOVE (e
, entries
);
1431 void vm_state_notify(int running
, RunState state
)
1433 VMChangeStateEntry
*e
;
1435 trace_vm_state_notify(running
, state
);
1437 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1438 e
->cb(e
->opaque
, running
, state
);
1444 if (!runstate_is_running()) {
1446 runstate_set(RUN_STATE_RUNNING
);
1447 vm_state_notify(1, RUN_STATE_RUNNING
);
1449 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1453 /* reset/shutdown handler */
1455 typedef struct QEMUResetEntry
{
1456 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1457 QEMUResetHandler
*func
;
1461 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1462 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1463 static int reset_requested
;
1464 static int shutdown_requested
, shutdown_signal
= -1;
1465 static pid_t shutdown_pid
;
1466 static int powerdown_requested
;
1467 static int debug_requested
;
1468 static int suspend_requested
;
1469 static int wakeup_requested
;
1470 static NotifierList powerdown_notifiers
=
1471 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1472 static NotifierList suspend_notifiers
=
1473 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1474 static NotifierList wakeup_notifiers
=
1475 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1476 static uint32_t wakeup_reason_mask
= ~0;
1477 static RunState vmstop_requested
= RUN_STATE_MAX
;
1479 int qemu_shutdown_requested_get(void)
1481 return shutdown_requested
;
1484 int qemu_reset_requested_get(void)
1486 return reset_requested
;
1489 static int qemu_shutdown_requested(void)
1491 int r
= shutdown_requested
;
1492 shutdown_requested
= 0;
1496 static void qemu_kill_report(void)
1498 if (!qtest_enabled() && shutdown_signal
!= -1) {
1499 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1500 if (shutdown_pid
== 0) {
1501 /* This happens for eg ^C at the terminal, so it's worth
1502 * avoiding printing an odd message in that case.
1504 fputc('\n', stderr
);
1506 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1508 shutdown_signal
= -1;
1512 static int qemu_reset_requested(void)
1514 int r
= reset_requested
;
1515 reset_requested
= 0;
1519 static int qemu_suspend_requested(void)
1521 int r
= suspend_requested
;
1522 suspend_requested
= 0;
1526 static int qemu_wakeup_requested(void)
1528 int r
= wakeup_requested
;
1529 wakeup_requested
= 0;
1533 static int qemu_powerdown_requested(void)
1535 int r
= powerdown_requested
;
1536 powerdown_requested
= 0;
1540 static int qemu_debug_requested(void)
1542 int r
= debug_requested
;
1543 debug_requested
= 0;
1547 /* We use RUN_STATE_MAX but any invalid value will do */
1548 static bool qemu_vmstop_requested(RunState
*r
)
1550 if (vmstop_requested
< RUN_STATE_MAX
) {
1551 *r
= vmstop_requested
;
1552 vmstop_requested
= RUN_STATE_MAX
;
1559 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1561 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1564 re
->opaque
= opaque
;
1565 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1568 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1572 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1573 if (re
->func
== func
&& re
->opaque
== opaque
) {
1574 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1581 void qemu_devices_reset(void)
1583 QEMUResetEntry
*re
, *nre
;
1585 /* reset all devices */
1586 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1587 re
->func(re
->opaque
);
1591 void qemu_system_reset(bool report
)
1593 if (current_machine
&& current_machine
->reset
) {
1594 current_machine
->reset();
1596 qemu_devices_reset();
1599 monitor_protocol_event(QEVENT_RESET
, NULL
);
1601 cpu_synchronize_all_post_reset();
1604 void qemu_system_reset_request(void)
1607 shutdown_requested
= 1;
1609 reset_requested
= 1;
1612 qemu_notify_event();
1615 static void qemu_system_suspend(void)
1618 notifier_list_notify(&suspend_notifiers
, NULL
);
1619 runstate_set(RUN_STATE_SUSPENDED
);
1620 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1623 void qemu_system_suspend_request(void)
1625 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1628 suspend_requested
= 1;
1630 qemu_notify_event();
1633 void qemu_register_suspend_notifier(Notifier
*notifier
)
1635 notifier_list_add(&suspend_notifiers
, notifier
);
1638 void qemu_system_wakeup_request(WakeupReason reason
)
1640 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1643 if (!(wakeup_reason_mask
& (1 << reason
))) {
1646 runstate_set(RUN_STATE_RUNNING
);
1647 notifier_list_notify(&wakeup_notifiers
, &reason
);
1648 wakeup_requested
= 1;
1649 qemu_notify_event();
1652 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1655 wakeup_reason_mask
|= (1 << reason
);
1657 wakeup_reason_mask
&= ~(1 << reason
);
1661 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1663 notifier_list_add(&wakeup_notifiers
, notifier
);
1666 void qemu_system_killed(int signal
, pid_t pid
)
1668 shutdown_signal
= signal
;
1671 qemu_system_shutdown_request();
1674 void qemu_system_shutdown_request(void)
1676 shutdown_requested
= 1;
1677 qemu_notify_event();
1680 static void qemu_system_powerdown(void)
1682 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1683 notifier_list_notify(&powerdown_notifiers
, NULL
);
1686 void qemu_system_powerdown_request(void)
1688 powerdown_requested
= 1;
1689 qemu_notify_event();
1692 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1694 notifier_list_add(&powerdown_notifiers
, notifier
);
1697 void qemu_system_debug_request(void)
1699 debug_requested
= 1;
1700 qemu_notify_event();
1703 void qemu_system_vmstop_request(RunState state
)
1705 vmstop_requested
= state
;
1706 qemu_notify_event();
1709 static bool main_loop_should_exit(void)
1712 if (qemu_debug_requested()) {
1713 vm_stop(RUN_STATE_DEBUG
);
1715 if (qemu_suspend_requested()) {
1716 qemu_system_suspend();
1718 if (qemu_shutdown_requested()) {
1720 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1722 vm_stop(RUN_STATE_SHUTDOWN
);
1727 if (qemu_reset_requested()) {
1729 cpu_synchronize_all_states();
1730 qemu_system_reset(VMRESET_REPORT
);
1732 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1733 runstate_check(RUN_STATE_SHUTDOWN
)) {
1734 runstate_set(RUN_STATE_PAUSED
);
1737 if (qemu_wakeup_requested()) {
1739 cpu_synchronize_all_states();
1740 qemu_system_reset(VMRESET_SILENT
);
1742 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1744 if (qemu_powerdown_requested()) {
1745 qemu_system_powerdown();
1747 if (qemu_vmstop_requested(&r
)) {
1753 static void main_loop(void)
1757 #ifdef CONFIG_PROFILER
1761 nonblocking
= !kvm_enabled() && last_io
> 0;
1762 #ifdef CONFIG_PROFILER
1763 ti
= profile_getclock();
1765 last_io
= main_loop_wait(nonblocking
);
1766 #ifdef CONFIG_PROFILER
1767 dev_time
+= profile_getclock() - ti
;
1769 } while (!main_loop_should_exit());
1772 static void version(void)
1774 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1777 static void help(int exitcode
)
1780 printf("usage: %s [options] [disk_image]\n\n"
1781 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1782 error_get_progname());
1784 #define QEMU_OPTIONS_GENERATE_HELP
1785 #include "qemu-options-wrapper.h"
1787 printf("\nDuring emulation, the following keys are useful:\n"
1788 "ctrl-alt-f toggle full screen\n"
1789 "ctrl-alt-n switch to virtual console 'n'\n"
1790 "ctrl-alt toggle mouse and keyboard grab\n"
1792 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1797 #define HAS_ARG 0x0001
1799 typedef struct QEMUOption
{
1806 static const QEMUOption qemu_options
[] = {
1807 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1808 #define QEMU_OPTIONS_GENERATE_OPTIONS
1809 #include "qemu-options-wrapper.h"
1813 static bool vga_available(void)
1815 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1818 static bool cirrus_vga_available(void)
1820 return object_class_by_name("cirrus-vga")
1821 || object_class_by_name("isa-cirrus-vga");
1824 static bool vmware_vga_available(void)
1826 return object_class_by_name("vmware-svga");
1829 static bool qxl_vga_available(void)
1831 return object_class_by_name("qxl-vga");
1834 static void select_vgahw (const char *p
)
1838 vga_interface_type
= VGA_NONE
;
1839 if (strstart(p
, "std", &opts
)) {
1840 if (vga_available()) {
1841 vga_interface_type
= VGA_STD
;
1843 fprintf(stderr
, "Error: standard VGA not available\n");
1846 } else if (strstart(p
, "cirrus", &opts
)) {
1847 if (cirrus_vga_available()) {
1848 vga_interface_type
= VGA_CIRRUS
;
1850 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1853 } else if (strstart(p
, "vmware", &opts
)) {
1854 if (vmware_vga_available()) {
1855 vga_interface_type
= VGA_VMWARE
;
1857 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1860 } else if (strstart(p
, "xenfb", &opts
)) {
1861 vga_interface_type
= VGA_XENFB
;
1862 } else if (strstart(p
, "qxl", &opts
)) {
1863 if (qxl_vga_available()) {
1864 vga_interface_type
= VGA_QXL
;
1866 fprintf(stderr
, "Error: QXL VGA not available\n");
1869 } else if (!strstart(p
, "none", &opts
)) {
1871 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1875 const char *nextopt
;
1877 if (strstart(opts
, ",retrace=", &nextopt
)) {
1879 if (strstart(opts
, "dumb", &nextopt
))
1880 vga_retrace_method
= VGA_RETRACE_DUMB
;
1881 else if (strstart(opts
, "precise", &nextopt
))
1882 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1883 else goto invalid_vga
;
1884 } else goto invalid_vga
;
1889 static DisplayType
select_display(const char *p
)
1892 DisplayType display
= DT_DEFAULT
;
1894 if (strstart(p
, "sdl", &opts
)) {
1898 const char *nextopt
;
1900 if (strstart(opts
, ",frame=", &nextopt
)) {
1902 if (strstart(opts
, "on", &nextopt
)) {
1904 } else if (strstart(opts
, "off", &nextopt
)) {
1907 goto invalid_sdl_args
;
1909 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1911 if (strstart(opts
, "on", &nextopt
)) {
1913 } else if (strstart(opts
, "off", &nextopt
)) {
1916 goto invalid_sdl_args
;
1918 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1920 if (strstart(opts
, "on", &nextopt
)) {
1922 } else if (strstart(opts
, "off", &nextopt
)) {
1925 goto invalid_sdl_args
;
1927 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1929 if (strstart(opts
, "on", &nextopt
)) {
1931 } else if (strstart(opts
, "off", &nextopt
)) {
1934 goto invalid_sdl_args
;
1938 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1944 fprintf(stderr
, "SDL support is disabled\n");
1947 } else if (strstart(p
, "vnc", &opts
)) {
1952 const char *nextopt
;
1954 if (strstart(opts
, "=", &nextopt
)) {
1955 vnc_display
= nextopt
;
1959 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1963 fprintf(stderr
, "VNC support is disabled\n");
1966 } else if (strstart(p
, "curses", &opts
)) {
1967 #ifdef CONFIG_CURSES
1968 display
= DT_CURSES
;
1970 fprintf(stderr
, "Curses support is disabled\n");
1973 } else if (strstart(p
, "none", &opts
)) {
1976 fprintf(stderr
, "Unknown display type: %s\n", p
);
1983 static int balloon_parse(const char *arg
)
1987 if (strcmp(arg
, "none") == 0) {
1991 if (!strncmp(arg
, "virtio", 6)) {
1992 if (arg
[6] == ',') {
1993 /* have params -> parse them */
1994 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1998 /* create empty opts */
1999 opts
= qemu_opts_create_nofail(qemu_find_opts("device"));
2001 qemu_opt_set(opts
, "driver", "virtio-balloon");
2008 char *qemu_find_file(int type
, const char *name
)
2014 /* Try the name as a straight path first */
2015 if (access(name
, R_OK
) == 0) {
2016 return g_strdup(name
);
2019 case QEMU_FILE_TYPE_BIOS
:
2022 case QEMU_FILE_TYPE_KEYMAP
:
2023 subdir
= "keymaps/";
2028 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2029 buf
= g_malloc0(len
);
2030 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2031 if (access(buf
, R_OK
)) {
2038 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2040 return qdev_device_help(opts
);
2043 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2047 dev
= qdev_device_add(opts
);
2053 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2055 CharDriverState
*chr
;
2057 chr
= qemu_chr_new_from_opts(opts
, NULL
);
2063 #ifdef CONFIG_VIRTFS
2064 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2067 ret
= qemu_fsdev_add(opts
);
2073 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2075 CharDriverState
*chr
;
2076 const char *chardev
;
2080 mode
= qemu_opt_get(opts
, "mode");
2084 if (strcmp(mode
, "readline") == 0) {
2085 flags
= MONITOR_USE_READLINE
;
2086 } else if (strcmp(mode
, "control") == 0) {
2087 flags
= MONITOR_USE_CONTROL
;
2089 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2093 if (qemu_opt_get_bool(opts
, "pretty", 0))
2094 flags
|= MONITOR_USE_PRETTY
;
2096 if (qemu_opt_get_bool(opts
, "default", 0))
2097 flags
|= MONITOR_IS_DEFAULT
;
2099 chardev
= qemu_opt_get(opts
, "chardev");
2100 chr
= qemu_chr_find(chardev
);
2102 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2106 monitor_init(chr
, flags
);
2110 static void monitor_parse(const char *optarg
, const char *mode
)
2112 static int monitor_device_index
= 0;
2118 if (strstart(optarg
, "chardev:", &p
)) {
2119 snprintf(label
, sizeof(label
), "%s", p
);
2121 snprintf(label
, sizeof(label
), "compat_monitor%d",
2122 monitor_device_index
);
2123 if (monitor_device_index
== 0) {
2126 opts
= qemu_chr_parse_compat(label
, optarg
);
2128 fprintf(stderr
, "parse error: %s\n", optarg
);
2133 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2135 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2138 qemu_opt_set(opts
, "mode", mode
);
2139 qemu_opt_set(opts
, "chardev", label
);
2141 qemu_opt_set(opts
, "default", "on");
2142 monitor_device_index
++;
2145 struct device_config
{
2147 DEV_USB
, /* -usbdevice */
2149 DEV_SERIAL
, /* -serial */
2150 DEV_PARALLEL
, /* -parallel */
2151 DEV_VIRTCON
, /* -virtioconsole */
2152 DEV_DEBUGCON
, /* -debugcon */
2153 DEV_GDB
, /* -gdb, -s */
2155 const char *cmdline
;
2157 QTAILQ_ENTRY(device_config
) next
;
2160 static QTAILQ_HEAD(, device_config
) device_configs
=
2161 QTAILQ_HEAD_INITIALIZER(device_configs
);
2163 static void add_device_config(int type
, const char *cmdline
)
2165 struct device_config
*conf
;
2167 conf
= g_malloc0(sizeof(*conf
));
2169 conf
->cmdline
= cmdline
;
2170 loc_save(&conf
->loc
);
2171 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2174 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2176 struct device_config
*conf
;
2179 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2180 if (conf
->type
!= type
)
2182 loc_push_restore(&conf
->loc
);
2183 rc
= func(conf
->cmdline
);
2184 loc_pop(&conf
->loc
);
2191 static int serial_parse(const char *devname
)
2193 static int index
= 0;
2196 if (strcmp(devname
, "none") == 0)
2198 if (index
== MAX_SERIAL_PORTS
) {
2199 fprintf(stderr
, "qemu: too many serial ports\n");
2202 snprintf(label
, sizeof(label
), "serial%d", index
);
2203 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2204 if (!serial_hds
[index
]) {
2205 fprintf(stderr
, "qemu: could not connect serial device"
2206 " to character backend '%s'\n", devname
);
2213 static int parallel_parse(const char *devname
)
2215 static int index
= 0;
2218 if (strcmp(devname
, "none") == 0)
2220 if (index
== MAX_PARALLEL_PORTS
) {
2221 fprintf(stderr
, "qemu: too many parallel ports\n");
2224 snprintf(label
, sizeof(label
), "parallel%d", index
);
2225 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2226 if (!parallel_hds
[index
]) {
2227 fprintf(stderr
, "qemu: could not connect parallel device"
2228 " to character backend '%s'\n", devname
);
2235 static int virtcon_parse(const char *devname
)
2237 QemuOptsList
*device
= qemu_find_opts("device");
2238 static int index
= 0;
2240 QemuOpts
*bus_opts
, *dev_opts
;
2242 if (strcmp(devname
, "none") == 0)
2244 if (index
== MAX_VIRTIO_CONSOLES
) {
2245 fprintf(stderr
, "qemu: too many virtio consoles\n");
2249 bus_opts
= qemu_opts_create_nofail(device
);
2250 if (arch_type
== QEMU_ARCH_S390X
) {
2251 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2253 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2256 dev_opts
= qemu_opts_create_nofail(device
);
2257 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2259 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2260 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2261 if (!virtcon_hds
[index
]) {
2262 fprintf(stderr
, "qemu: could not connect virtio console"
2263 " to character backend '%s'\n", devname
);
2266 qemu_opt_set(dev_opts
, "chardev", label
);
2272 static int debugcon_parse(const char *devname
)
2276 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2279 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2281 fprintf(stderr
, "qemu: already have a debugcon device\n");
2284 qemu_opt_set(opts
, "driver", "isa-debugcon");
2285 qemu_opt_set(opts
, "chardev", "debugcon");
2289 static QEMUMachine
*machine_parse(const char *name
)
2291 QEMUMachine
*m
, *machine
= NULL
;
2294 machine
= find_machine(name
);
2299 printf("Supported machines are:\n");
2300 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2302 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2304 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2305 m
->is_default
? " (default)" : "");
2307 exit(!name
|| !is_help_option(name
));
2310 static int tcg_init(void)
2312 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2317 const char *opt_name
;
2319 int (*available
)(void);
2323 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2324 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2325 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2326 { "qtest", "QTest", qtest_available
, qtest_init
, &qtest_allowed
},
2329 static int configure_accelerator(void)
2331 const char *p
= NULL
;
2334 bool accel_initialised
= 0;
2335 bool init_failed
= 0;
2337 QemuOptsList
*list
= qemu_find_opts("machine");
2338 if (!QTAILQ_EMPTY(&list
->head
)) {
2339 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2343 /* Use the default "accelerator", tcg */
2347 while (!accel_initialised
&& *p
!= '\0') {
2351 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2352 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2353 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2354 *(accel_list
[i
].allowed
) = 1;
2355 ret
= accel_list
[i
].init();
2358 if (!accel_list
[i
].available()) {
2359 printf("%s not supported for this target\n",
2360 accel_list
[i
].name
);
2362 fprintf(stderr
, "failed to initialize %s: %s\n",
2366 *(accel_list
[i
].allowed
) = 0;
2368 accel_initialised
= 1;
2373 if (i
== ARRAY_SIZE(accel_list
)) {
2374 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2378 if (!accel_initialised
) {
2379 fprintf(stderr
, "No accelerator found!\n");
2384 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2387 return !accel_initialised
;
2390 void qemu_add_exit_notifier(Notifier
*notify
)
2392 notifier_list_add(&exit_notifiers
, notify
);
2395 void qemu_remove_exit_notifier(Notifier
*notify
)
2397 notifier_remove(notify
);
2400 static void qemu_run_exit_notifiers(void)
2402 notifier_list_notify(&exit_notifiers
, NULL
);
2405 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2407 notifier_list_add(&machine_init_done_notifiers
, notify
);
2410 static void qemu_run_machine_init_done_notifiers(void)
2412 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2415 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2416 const char **poptarg
, int *poptind
)
2418 const QEMUOption
*popt
;
2419 int optind
= *poptind
;
2420 char *r
= argv
[optind
];
2423 loc_set_cmdline(argv
, optind
, 1);
2425 /* Treat --foo the same as -foo. */
2428 popt
= qemu_options
;
2431 error_report("invalid option");
2434 if (!strcmp(popt
->name
, r
+ 1))
2438 if (popt
->flags
& HAS_ARG
) {
2439 if (optind
>= argc
) {
2440 error_report("requires an argument");
2443 optarg
= argv
[optind
++];
2444 loc_set_cmdline(argv
, optind
- 2, 2);
2455 static gpointer
malloc_and_trace(gsize n_bytes
)
2457 void *ptr
= malloc(n_bytes
);
2458 trace_g_malloc(n_bytes
, ptr
);
2462 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2464 void *ptr
= realloc(mem
, n_bytes
);
2465 trace_g_realloc(mem
, n_bytes
, ptr
);
2469 static void free_and_trace(gpointer mem
)
2475 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2477 Object
*obj
= OBJECT(opaque
);
2478 StringInputVisitor
*siv
;
2479 Error
*local_err
= NULL
;
2481 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0) {
2485 siv
= string_input_visitor_new(value
);
2486 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2487 string_input_visitor_cleanup(siv
);
2490 qerror_report_err(local_err
);
2491 error_free(local_err
);
2498 static int object_create(QemuOpts
*opts
, void *opaque
)
2500 const char *type
= qemu_opt_get(opts
, "qom-type");
2501 const char *id
= qemu_opts_id(opts
);
2504 g_assert(type
!= NULL
);
2507 qerror_report(QERR_MISSING_PARAMETER
, "id");
2511 obj
= object_new(type
);
2512 if (qemu_opt_foreach(opts
, object_set_property
, obj
, 1) < 0) {
2516 object_property_add_child(container_get(object_get_root(), "/objects"),
2522 int main(int argc
, char **argv
, char **envp
)
2525 int snapshot
, linux_boot
;
2526 const char *icount_option
= NULL
;
2527 const char *initrd_filename
;
2528 const char *kernel_filename
, *kernel_cmdline
;
2529 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2531 int cyls
, heads
, secs
, translation
;
2532 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2533 QemuOptsList
*olist
;
2536 const char *loadvm
= NULL
;
2537 QEMUMachine
*machine
;
2538 const char *cpu_model
;
2539 const char *vga_model
= "none";
2540 const char *pid_file
= NULL
;
2541 const char *incoming
= NULL
;
2543 int show_vnc_port
= 0;
2545 bool defconfig
= true;
2546 bool userconfig
= true;
2547 const char *log_mask
= NULL
;
2548 const char *log_file
= NULL
;
2549 GMemVTable mem_trace
= {
2550 .malloc
= malloc_and_trace
,
2551 .realloc
= realloc_and_trace
,
2552 .free
= free_and_trace
,
2554 const char *trace_events
= NULL
;
2555 const char *trace_file
= NULL
;
2557 atexit(qemu_run_exit_notifiers
);
2558 error_set_progname(argv
[0]);
2560 g_mem_set_vtable(&mem_trace
);
2561 if (!g_thread_supported()) {
2562 #if !GLIB_CHECK_VERSION(2, 31, 0)
2563 g_thread_init(NULL
);
2565 fprintf(stderr
, "glib threading failed to initialize.\n");
2570 module_call_init(MODULE_INIT_QOM
);
2575 rtc_clock
= host_clock
;
2577 qemu_cache_utils_init(envp
);
2579 QLIST_INIT (&vm_change_state_head
);
2580 os_setup_early_signal_handling();
2582 module_call_init(MODULE_INIT_MACHINE
);
2583 machine
= find_default_machine();
2587 cyls
= heads
= secs
= 0;
2588 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2590 for (i
= 0; i
< MAX_NODES
; i
++) {
2592 node_cpumask
[i
] = bitmap_new(MAX_CPUMASK_BITS
);
2600 /* first pass of option parsing */
2602 while (optind
< argc
) {
2603 if (argv
[optind
][0] != '-') {
2608 const QEMUOption
*popt
;
2610 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2611 switch (popt
->index
) {
2612 case QEMU_OPTION_nodefconfig
:
2615 case QEMU_OPTION_nouserconfig
:
2624 ret
= qemu_read_default_config_files(userconfig
);
2630 /* second pass of option parsing */
2635 if (argv
[optind
][0] != '-') {
2636 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2638 const QEMUOption
*popt
;
2640 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2641 if (!(popt
->arch_mask
& arch_type
)) {
2642 printf("Option %s not supported for this target\n", popt
->name
);
2645 switch(popt
->index
) {
2647 machine
= machine_parse(optarg
);
2649 case QEMU_OPTION_no_kvm_irqchip
: {
2650 olist
= qemu_find_opts("machine");
2651 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2654 case QEMU_OPTION_cpu
:
2655 /* hw initialization will check this */
2658 case QEMU_OPTION_hda
:
2662 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2664 snprintf(buf
, sizeof(buf
),
2665 "%s,cyls=%d,heads=%d,secs=%d%s",
2666 HD_OPTS
, cyls
, heads
, secs
,
2667 translation
== BIOS_ATA_TRANSLATION_LBA
?
2669 translation
== BIOS_ATA_TRANSLATION_NONE
?
2670 ",trans=none" : "");
2671 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2674 case QEMU_OPTION_hdb
:
2675 case QEMU_OPTION_hdc
:
2676 case QEMU_OPTION_hdd
:
2677 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2680 case QEMU_OPTION_drive
:
2681 if (drive_def(optarg
) == NULL
) {
2685 case QEMU_OPTION_set
:
2686 if (qemu_set_option(optarg
) != 0)
2689 case QEMU_OPTION_global
:
2690 if (qemu_global_option(optarg
) != 0)
2693 case QEMU_OPTION_mtdblock
:
2694 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2696 case QEMU_OPTION_sd
:
2697 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2699 case QEMU_OPTION_pflash
:
2700 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2702 case QEMU_OPTION_snapshot
:
2705 case QEMU_OPTION_hdachs
:
2709 cyls
= strtol(p
, (char **)&p
, 0);
2710 if (cyls
< 1 || cyls
> 16383)
2715 heads
= strtol(p
, (char **)&p
, 0);
2716 if (heads
< 1 || heads
> 16)
2721 secs
= strtol(p
, (char **)&p
, 0);
2722 if (secs
< 1 || secs
> 63)
2726 if (!strcmp(p
, "none"))
2727 translation
= BIOS_ATA_TRANSLATION_NONE
;
2728 else if (!strcmp(p
, "lba"))
2729 translation
= BIOS_ATA_TRANSLATION_LBA
;
2730 else if (!strcmp(p
, "auto"))
2731 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2734 } else if (*p
!= '\0') {
2736 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2739 if (hda_opts
!= NULL
) {
2741 snprintf(num
, sizeof(num
), "%d", cyls
);
2742 qemu_opt_set(hda_opts
, "cyls", num
);
2743 snprintf(num
, sizeof(num
), "%d", heads
);
2744 qemu_opt_set(hda_opts
, "heads", num
);
2745 snprintf(num
, sizeof(num
), "%d", secs
);
2746 qemu_opt_set(hda_opts
, "secs", num
);
2747 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2748 qemu_opt_set(hda_opts
, "trans", "lba");
2749 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2750 qemu_opt_set(hda_opts
, "trans", "none");
2754 case QEMU_OPTION_numa
:
2755 if (nb_numa_nodes
>= MAX_NODES
) {
2756 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2761 case QEMU_OPTION_display
:
2762 display_type
= select_display(optarg
);
2764 case QEMU_OPTION_nographic
:
2765 display_type
= DT_NOGRAPHIC
;
2767 case QEMU_OPTION_curses
:
2768 #ifdef CONFIG_CURSES
2769 display_type
= DT_CURSES
;
2771 fprintf(stderr
, "Curses support is disabled\n");
2775 case QEMU_OPTION_portrait
:
2776 graphic_rotate
= 90;
2778 case QEMU_OPTION_rotate
:
2779 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2780 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2781 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2783 "qemu: only 90, 180, 270 deg rotation is available\n");
2787 case QEMU_OPTION_kernel
:
2788 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2790 case QEMU_OPTION_initrd
:
2791 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2793 case QEMU_OPTION_append
:
2794 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2796 case QEMU_OPTION_dtb
:
2797 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
2799 case QEMU_OPTION_cdrom
:
2800 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2802 case QEMU_OPTION_boot
:
2804 static const char * const params
[] = {
2805 "order", "once", "menu",
2806 "splash", "splash-time",
2807 "reboot-timeout", NULL
2809 char buf
[sizeof(boot_devices
)];
2810 char *standard_boot_devices
;
2813 if (!strchr(optarg
, '=')) {
2815 pstrcpy(buf
, sizeof(buf
), optarg
);
2816 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2818 "qemu: unknown boot parameter '%s' in '%s'\n",
2824 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2825 validate_bootdevices(buf
);
2826 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2829 if (get_param_value(buf
, sizeof(buf
),
2831 validate_bootdevices(buf
);
2832 standard_boot_devices
= g_strdup(boot_devices
);
2833 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2834 qemu_register_reset(restore_boot_devices
,
2835 standard_boot_devices
);
2837 if (get_param_value(buf
, sizeof(buf
),
2839 if (!strcmp(buf
, "on")) {
2841 } else if (!strcmp(buf
, "off")) {
2845 "qemu: invalid option value '%s'\n",
2850 qemu_opts_parse(qemu_find_opts("boot-opts"),
2855 case QEMU_OPTION_fda
:
2856 case QEMU_OPTION_fdb
:
2857 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2860 case QEMU_OPTION_no_fd_bootchk
:
2863 case QEMU_OPTION_netdev
:
2864 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2868 case QEMU_OPTION_net
:
2869 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2873 #ifdef CONFIG_LIBISCSI
2874 case QEMU_OPTION_iscsi
:
2875 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2882 case QEMU_OPTION_tftp
:
2883 legacy_tftp_prefix
= optarg
;
2885 case QEMU_OPTION_bootp
:
2886 legacy_bootp_filename
= optarg
;
2888 case QEMU_OPTION_redir
:
2889 if (net_slirp_redir(optarg
) < 0)
2893 case QEMU_OPTION_bt
:
2894 add_device_config(DEV_BT
, optarg
);
2896 case QEMU_OPTION_audio_help
:
2897 if (!(audio_available())) {
2898 printf("Option %s not supported for this target\n", popt
->name
);
2904 case QEMU_OPTION_soundhw
:
2905 if (!(audio_available())) {
2906 printf("Option %s not supported for this target\n", popt
->name
);
2909 select_soundhw (optarg
);
2914 case QEMU_OPTION_version
:
2918 case QEMU_OPTION_m
: {
2923 value
= strtosz(optarg
, &end
);
2924 if (value
< 0 || *end
) {
2925 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2928 sz
= QEMU_ALIGN_UP((uint64_t)value
, 8192);
2930 if (ram_size
!= sz
) {
2931 fprintf(stderr
, "qemu: ram size too large\n");
2936 case QEMU_OPTION_mempath
:
2940 case QEMU_OPTION_mem_prealloc
:
2951 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2953 case QEMU_OPTION_gdb
:
2954 add_device_config(DEV_GDB
, optarg
);
2959 case QEMU_OPTION_bios
:
2962 case QEMU_OPTION_singlestep
:
2969 keyboard_layout
= optarg
;
2971 case QEMU_OPTION_localtime
:
2974 case QEMU_OPTION_vga
:
2983 w
= strtol(p
, (char **)&p
, 10);
2986 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2992 h
= strtol(p
, (char **)&p
, 10);
2997 depth
= strtol(p
, (char **)&p
, 10);
2998 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2999 depth
!= 24 && depth
!= 32)
3001 } else if (*p
== '\0') {
3002 depth
= graphic_depth
;
3009 graphic_depth
= depth
;
3012 case QEMU_OPTION_echr
:
3015 term_escape_char
= strtol(optarg
, &r
, 0);
3017 printf("Bad argument to echr\n");
3020 case QEMU_OPTION_monitor
:
3021 monitor_parse(optarg
, "readline");
3022 default_monitor
= 0;
3024 case QEMU_OPTION_qmp
:
3025 monitor_parse(optarg
, "control");
3026 default_monitor
= 0;
3028 case QEMU_OPTION_mon
:
3029 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3033 default_monitor
= 0;
3035 case QEMU_OPTION_chardev
:
3036 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3041 case QEMU_OPTION_fsdev
:
3042 olist
= qemu_find_opts("fsdev");
3044 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3047 opts
= qemu_opts_parse(olist
, optarg
, 1);
3049 fprintf(stderr
, "parse error: %s\n", optarg
);
3053 case QEMU_OPTION_virtfs
: {
3056 const char *writeout
, *sock_fd
, *socket
;
3058 olist
= qemu_find_opts("virtfs");
3060 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3063 opts
= qemu_opts_parse(olist
, optarg
, 1);
3065 fprintf(stderr
, "parse error: %s\n", optarg
);
3069 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3070 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3071 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3074 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3075 qemu_opt_get(opts
, "mount_tag"),
3078 fprintf(stderr
, "duplicate fsdev id: %s\n",
3079 qemu_opt_get(opts
, "mount_tag"));
3083 writeout
= qemu_opt_get(opts
, "writeout");
3085 #ifdef CONFIG_SYNC_FILE_RANGE
3086 qemu_opt_set(fsdev
, "writeout", writeout
);
3088 fprintf(stderr
, "writeout=immediate not supported on "
3093 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3094 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3095 qemu_opt_set(fsdev
, "security_model",
3096 qemu_opt_get(opts
, "security_model"));
3097 socket
= qemu_opt_get(opts
, "socket");
3099 qemu_opt_set(fsdev
, "socket", socket
);
3101 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3103 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3106 qemu_opt_set_bool(fsdev
, "readonly",
3107 qemu_opt_get_bool(opts
, "readonly", 0));
3108 device
= qemu_opts_create_nofail(qemu_find_opts("device"));
3109 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3110 qemu_opt_set(device
, "fsdev",
3111 qemu_opt_get(opts
, "mount_tag"));
3112 qemu_opt_set(device
, "mount_tag",
3113 qemu_opt_get(opts
, "mount_tag"));
3116 case QEMU_OPTION_virtfs_synth
: {
3120 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3123 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3126 qemu_opt_set(fsdev
, "fsdriver", "synth");
3128 device
= qemu_opts_create_nofail(qemu_find_opts("device"));
3129 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3130 qemu_opt_set(device
, "fsdev", "v_synth");
3131 qemu_opt_set(device
, "mount_tag", "v_synth");
3134 case QEMU_OPTION_serial
:
3135 add_device_config(DEV_SERIAL
, optarg
);
3137 if (strncmp(optarg
, "mon:", 4) == 0) {
3138 default_monitor
= 0;
3141 case QEMU_OPTION_watchdog
:
3144 "qemu: only one watchdog option may be given\n");
3149 case QEMU_OPTION_watchdog_action
:
3150 if (select_watchdog_action(optarg
) == -1) {
3151 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3155 case QEMU_OPTION_virtiocon
:
3156 add_device_config(DEV_VIRTCON
, optarg
);
3157 default_virtcon
= 0;
3158 if (strncmp(optarg
, "mon:", 4) == 0) {
3159 default_monitor
= 0;
3162 case QEMU_OPTION_parallel
:
3163 add_device_config(DEV_PARALLEL
, optarg
);
3164 default_parallel
= 0;
3165 if (strncmp(optarg
, "mon:", 4) == 0) {
3166 default_monitor
= 0;
3169 case QEMU_OPTION_debugcon
:
3170 add_device_config(DEV_DEBUGCON
, optarg
);
3172 case QEMU_OPTION_loadvm
:
3175 case QEMU_OPTION_full_screen
:
3179 case QEMU_OPTION_no_frame
:
3182 case QEMU_OPTION_alt_grab
:
3185 case QEMU_OPTION_ctrl_grab
:
3188 case QEMU_OPTION_no_quit
:
3191 case QEMU_OPTION_sdl
:
3192 display_type
= DT_SDL
;
3195 case QEMU_OPTION_no_frame
:
3196 case QEMU_OPTION_alt_grab
:
3197 case QEMU_OPTION_ctrl_grab
:
3198 case QEMU_OPTION_no_quit
:
3199 case QEMU_OPTION_sdl
:
3200 fprintf(stderr
, "SDL support is disabled\n");
3203 case QEMU_OPTION_pidfile
:
3206 case QEMU_OPTION_win2k_hack
:
3207 win2k_install_hack
= 1;
3209 case QEMU_OPTION_rtc_td_hack
: {
3210 static GlobalProperty slew_lost_ticks
[] = {
3212 .driver
= "mc146818rtc",
3213 .property
= "lost_tick_policy",
3216 { /* end of list */ }
3219 qdev_prop_register_global_list(slew_lost_ticks
);
3222 case QEMU_OPTION_acpitable
:
3223 do_acpitable_option(optarg
);
3225 case QEMU_OPTION_smbios
:
3226 do_smbios_option(optarg
);
3228 case QEMU_OPTION_enable_kvm
:
3229 olist
= qemu_find_opts("machine");
3230 qemu_opts_parse(olist
, "accel=kvm", 0);
3232 case QEMU_OPTION_machine
:
3233 olist
= qemu_find_opts("machine");
3234 opts
= qemu_opts_parse(olist
, optarg
, 1);
3236 fprintf(stderr
, "parse error: %s\n", optarg
);
3239 optarg
= qemu_opt_get(opts
, "type");
3241 machine
= machine_parse(optarg
);
3244 case QEMU_OPTION_no_kvm
:
3245 olist
= qemu_find_opts("machine");
3246 qemu_opts_parse(olist
, "accel=tcg", 0);
3248 case QEMU_OPTION_no_kvm_pit
: {
3249 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3253 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3254 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3256 .driver
= "kvm-pit",
3257 .property
= "lost_tick_policy",
3260 { /* end of list */ }
3263 fprintf(stderr
, "Warning: option deprecated, use "
3264 "lost_tick_policy property of kvm-pit instead.\n");
3265 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3268 case QEMU_OPTION_usb
:
3269 olist
= qemu_find_opts("machine");
3270 qemu_opts_parse(olist
, "usb=on", 0);
3272 case QEMU_OPTION_usbdevice
:
3273 olist
= qemu_find_opts("machine");
3274 qemu_opts_parse(olist
, "usb=on", 0);
3275 add_device_config(DEV_USB
, optarg
);
3277 case QEMU_OPTION_device
:
3278 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3282 case QEMU_OPTION_smp
:
3285 fprintf(stderr
, "Invalid number of CPUs\n");
3288 if (max_cpus
< smp_cpus
) {
3289 fprintf(stderr
, "maxcpus must be equal to or greater than "
3293 if (max_cpus
> 255) {
3294 fprintf(stderr
, "Unsupported number of maxcpus\n");
3298 case QEMU_OPTION_vnc
:
3301 vnc_display
= optarg
;
3303 fprintf(stderr
, "VNC support is disabled\n");
3307 case QEMU_OPTION_no_acpi
:
3310 case QEMU_OPTION_no_hpet
:
3313 case QEMU_OPTION_balloon
:
3314 if (balloon_parse(optarg
) < 0) {
3315 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3319 case QEMU_OPTION_no_reboot
:
3322 case QEMU_OPTION_no_shutdown
:
3325 case QEMU_OPTION_show_cursor
:
3328 case QEMU_OPTION_uuid
:
3329 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3330 fprintf(stderr
, "Fail to parse UUID string."
3331 " Wrong format.\n");
3335 case QEMU_OPTION_option_rom
:
3336 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3337 fprintf(stderr
, "Too many option ROMs\n");
3340 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3341 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3342 option_rom
[nb_option_roms
].bootindex
=
3343 qemu_opt_get_number(opts
, "bootindex", -1);
3344 if (!option_rom
[nb_option_roms
].name
) {
3345 fprintf(stderr
, "Option ROM file is not specified\n");
3350 case QEMU_OPTION_semihosting
:
3351 semihosting_enabled
= 1;
3353 case QEMU_OPTION_tdf
:
3354 fprintf(stderr
, "Warning: user space PIT time drift fix "
3355 "is no longer supported.\n");
3357 case QEMU_OPTION_name
:
3358 qemu_name
= g_strdup(optarg
);
3360 char *p
= strchr(qemu_name
, ',');
3363 if (strncmp(p
, "process=", 8)) {
3364 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3368 os_set_proc_name(p
);
3372 case QEMU_OPTION_prom_env
:
3373 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3374 fprintf(stderr
, "Too many prom variables\n");
3377 prom_envs
[nb_prom_envs
] = optarg
;
3380 case QEMU_OPTION_old_param
:
3383 case QEMU_OPTION_clock
:
3384 configure_alarms(optarg
);
3386 case QEMU_OPTION_startdate
:
3387 configure_rtc_date_offset(optarg
, 1);
3389 case QEMU_OPTION_rtc
:
3390 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3394 configure_rtc(opts
);
3396 case QEMU_OPTION_tb_size
:
3397 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3398 if (tcg_tb_size
< 0) {
3402 case QEMU_OPTION_icount
:
3403 icount_option
= optarg
;
3405 case QEMU_OPTION_incoming
:
3407 runstate_set(RUN_STATE_INMIGRATE
);
3409 case QEMU_OPTION_nodefaults
:
3411 default_parallel
= 0;
3412 default_virtcon
= 0;
3413 default_monitor
= 0;
3420 case QEMU_OPTION_xen_domid
:
3421 if (!(xen_available())) {
3422 printf("Option %s not supported for this target\n", popt
->name
);
3425 xen_domid
= atoi(optarg
);
3427 case QEMU_OPTION_xen_create
:
3428 if (!(xen_available())) {
3429 printf("Option %s not supported for this target\n", popt
->name
);
3432 xen_mode
= XEN_CREATE
;
3434 case QEMU_OPTION_xen_attach
:
3435 if (!(xen_available())) {
3436 printf("Option %s not supported for this target\n", popt
->name
);
3439 xen_mode
= XEN_ATTACH
;
3441 case QEMU_OPTION_trace
:
3443 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3447 trace_events
= qemu_opt_get(opts
, "events");
3448 trace_file
= qemu_opt_get(opts
, "file");
3451 case QEMU_OPTION_readconfig
:
3453 int ret
= qemu_read_config_file(optarg
);
3455 fprintf(stderr
, "read config %s: %s\n", optarg
,
3461 case QEMU_OPTION_spice
:
3462 olist
= qemu_find_opts("spice");
3464 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3467 opts
= qemu_opts_parse(olist
, optarg
, 0);
3469 fprintf(stderr
, "parse error: %s\n", optarg
);
3473 case QEMU_OPTION_writeconfig
:
3476 if (strcmp(optarg
, "-") == 0) {
3479 fp
= fopen(optarg
, "w");
3481 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3485 qemu_config_write(fp
);
3489 case QEMU_OPTION_qtest
:
3490 qtest_chrdev
= optarg
;
3492 case QEMU_OPTION_qtest_log
:
3495 case QEMU_OPTION_sandbox
:
3496 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3501 case QEMU_OPTION_add_fd
:
3503 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3508 error_report("File descriptor passing is disabled on this "
3513 case QEMU_OPTION_object
:
3514 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3517 os_parse_cmd_args(popt
->index
, optarg
);
3523 if (qemu_init_main_loop()) {
3524 fprintf(stderr
, "qemu_init_main_loop failed\n");
3528 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3533 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3537 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3542 if (machine
== NULL
) {
3543 fprintf(stderr
, "No machine found.\n");
3547 if (machine
->hw_version
) {
3548 qemu_set_version(machine
->hw_version
);
3551 if (qemu_opts_foreach(qemu_find_opts("object"),
3552 object_create
, NULL
, 0) != 0) {
3556 /* Init CPU def lists, based on config
3557 * - Must be called after all the qemu_read_config_file() calls
3558 * - Must be called before list_cpus()
3559 * - Must be called before machine->init()
3563 if (cpu_model
&& is_help_option(cpu_model
)) {
3564 list_cpus(stdout
, &fprintf
, cpu_model
);
3568 /* Open the logfile at this point, if necessary. We can't open the logfile
3569 * when encountering either of the logging options (-d or -D) because the
3570 * other one may be encountered later on the command line, changing the
3571 * location or level of logging.
3575 set_cpu_log_filename(log_file
);
3577 set_cpu_log(log_mask
);
3580 if (!trace_backend_init(trace_events
, trace_file
)) {
3584 /* If no data_dir is specified then try to find it relative to the
3587 data_dir
= os_find_datadir(argv
[0]);
3589 /* If all else fails use the install path specified when building. */
3591 data_dir
= CONFIG_QEMU_DATADIR
;
3595 * Default to max_cpus = smp_cpus, in case the user doesn't
3596 * specify a max_cpus value.
3599 max_cpus
= smp_cpus
;
3601 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3602 if (smp_cpus
> machine
->max_cpus
) {
3603 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3604 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3610 * Get the default machine options from the machine if it is not already
3611 * specified either by the configuration file or by the command line.
3613 if (machine
->default_machine_opts
) {
3614 qemu_opts_set_defaults(qemu_find_opts("machine"),
3615 machine
->default_machine_opts
, 0);
3618 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3619 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3621 if (machine
->no_serial
) {
3624 if (machine
->no_parallel
) {
3625 default_parallel
= 0;
3627 if (!machine
->use_virtcon
) {
3628 default_virtcon
= 0;
3630 if (machine
->no_floppy
) {
3633 if (machine
->no_cdrom
) {
3636 if (machine
->no_sdcard
) {
3640 if (is_daemonized()) {
3641 /* According to documentation and historically, -nographic redirects
3642 * serial port, parallel port and monitor to stdio, which does not work
3643 * with -daemonize. We can redirect these to null instead, but since
3644 * -nographic is legacy, let's just error out.
3645 * We disallow -nographic only if all other ports are not redirected
3646 * explicitly, to not break existing legacy setups which uses
3647 * -nographic _and_ redirects all ports explicitly - this is valid
3648 * usage, -nographic is just a no-op in this case.
3650 if (display_type
== DT_NOGRAPHIC
3651 && (default_parallel
|| default_serial
3652 || default_monitor
|| default_virtcon
)) {
3653 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3656 #ifdef CONFIG_CURSES
3657 if (display_type
== DT_CURSES
) {
3658 fprintf(stderr
, "curses display can not be used with -daemonize\n");
3664 if (display_type
== DT_NOGRAPHIC
) {
3665 if (default_parallel
)
3666 add_device_config(DEV_PARALLEL
, "null");
3667 if (default_serial
&& default_monitor
) {
3668 add_device_config(DEV_SERIAL
, "mon:stdio");
3669 } else if (default_virtcon
&& default_monitor
) {
3670 add_device_config(DEV_VIRTCON
, "mon:stdio");
3673 add_device_config(DEV_SERIAL
, "stdio");
3674 if (default_virtcon
)
3675 add_device_config(DEV_VIRTCON
, "stdio");
3676 if (default_monitor
)
3677 monitor_parse("stdio", "readline");
3681 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3682 if (default_parallel
)
3683 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3684 if (default_monitor
)
3685 monitor_parse("vc:80Cx24C", "readline");
3686 if (default_virtcon
)
3687 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3692 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3694 #ifdef CONFIG_VIRTFS
3695 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3702 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3707 /* init the memory */
3708 if (ram_size
== 0) {
3709 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3712 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
3717 configure_accelerator();
3719 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3721 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3722 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3723 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3725 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3728 if (!kernel_cmdline
) {
3729 kernel_cmdline
= "";
3732 linux_boot
= (kernel_filename
!= NULL
);
3734 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3735 fprintf(stderr
, "-append only allowed with -kernel option\n");
3739 if (!linux_boot
&& initrd_filename
!= NULL
) {
3740 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3744 if (!linux_boot
&& machine_opts
&& qemu_opt_get(machine_opts
, "dtb")) {
3745 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
3749 os_set_line_buffering();
3751 qemu_init_cpu_loop();
3752 qemu_mutex_lock_iothread();
3755 /* spice needs the timers to be initialized by this point */
3759 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3760 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3763 configure_icount(icount_option
);
3765 /* clean up network at qemu process termination */
3766 atexit(&net_cleanup
);
3768 if (net_init_clients() < 0) {
3772 /* init the bluetooth world */
3773 if (foreach_device_config(DEV_BT
, bt_parse
))
3776 if (!xen_enabled()) {
3777 /* On 32-bit hosts, QEMU is limited by virtual address space */
3778 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3779 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3784 cpu_exec_init_all();
3786 bdrv_init_with_whitelist();
3790 /* open the virtual block devices */
3792 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3793 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
3794 &machine
->block_default_type
, 1) != 0) {
3798 default_drive(default_cdrom
, snapshot
, machine
->block_default_type
, 2,
3800 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
3801 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
3803 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
3805 if (nb_numa_nodes
> 0) {
3808 if (nb_numa_nodes
> MAX_NODES
) {
3809 nb_numa_nodes
= MAX_NODES
;
3812 /* If no memory size if given for any node, assume the default case
3813 * and distribute the available memory equally across all nodes
3815 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3816 if (node_mem
[i
] != 0)
3819 if (i
== nb_numa_nodes
) {
3820 uint64_t usedmem
= 0;
3822 /* On Linux, the each node's border has to be 8MB aligned,
3823 * the final node gets the rest.
3825 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3826 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3827 usedmem
+= node_mem
[i
];
3829 node_mem
[i
] = ram_size
- usedmem
;
3832 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3833 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
3837 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3838 * must cope with this anyway, because there are BIOSes out there in
3839 * real machines which also use this scheme.
3841 if (i
== nb_numa_nodes
) {
3842 for (i
= 0; i
< max_cpus
; i
++) {
3843 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
3848 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3852 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3854 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3856 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3858 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3861 /* If no default VGA is requested, the default is "none". */
3863 if (cirrus_vga_available()) {
3864 vga_model
= "cirrus";
3865 } else if (vga_available()) {
3869 select_vgahw(vga_model
);
3872 i
= select_watchdog(watchdog
);
3874 exit (i
== 1 ? 1 : 0);
3877 if (machine
->compat_props
) {
3878 qdev_prop_register_global_list(machine
->compat_props
);
3882 qdev_machine_init();
3884 QEMUMachineInitArgs args
= { .ram_size
= ram_size
,
3885 .boot_device
= boot_devices
,
3886 .kernel_filename
= kernel_filename
,
3887 .kernel_cmdline
= kernel_cmdline
,
3888 .initrd_filename
= initrd_filename
,
3889 .cpu_model
= cpu_model
};
3890 machine
->init(&args
);
3892 cpu_synchronize_all_post_init();
3896 current_machine
= machine
;
3898 /* init USB devices */
3899 if (usb_enabled(false)) {
3900 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3904 /* init generic devices */
3905 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3908 net_check_clients();
3910 /* just use the first displaystate for the moment */
3911 ds
= get_displaystate();
3915 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3916 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3917 display_type
= DT_SDL
;
3918 #elif defined(CONFIG_VNC)
3919 vnc_display
= "localhost:0,to=99";
3922 display_type
= DT_NONE
;
3927 /* init local displays */
3928 switch (display_type
) {
3931 #if defined(CONFIG_CURSES)
3933 curses_display_init(ds
, full_screen
);
3936 #if defined(CONFIG_SDL)
3938 sdl_display_init(ds
, full_screen
, no_frame
);
3940 #elif defined(CONFIG_COCOA)
3942 cocoa_display_init(ds
, full_screen
);
3949 /* must be after terminal init, SDL library changes signal handlers */
3950 os_setup_signal_handling();
3953 /* init remote displays */
3955 Error
*local_err
= NULL
;
3956 vnc_display_init(ds
);
3957 vnc_display_open(ds
, vnc_display
, &local_err
);
3958 if (local_err
!= NULL
) {
3959 fprintf(stderr
, "Failed to start VNC server on `%s': %s\n",
3960 vnc_display
, error_get_pretty(local_err
));
3961 error_free(local_err
);
3965 if (show_vnc_port
) {
3966 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3971 if (using_spice
&& !qxl_enabled
) {
3972 qemu_spice_display_init(ds
);
3977 text_consoles_set_display(ds
);
3979 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3983 qdev_machine_creation_done();
3985 if (rom_load_all() != 0) {
3986 fprintf(stderr
, "rom loading failed\n");
3990 /* TODO: once all bus devices are qdevified, this should be done
3991 * when bus is created by qdev.c */
3992 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3993 qemu_run_machine_init_done_notifiers();
3995 qemu_system_reset(VMRESET_SILENT
);
3997 if (load_vmstate(loadvm
) < 0) {
4003 Error
*local_err
= NULL
;
4004 qemu_start_incoming_migration(incoming
, &local_err
);
4006 fprintf(stderr
, "-incoming %s: %s\n", incoming
, error_get_pretty(local_err
));
4007 error_free(local_err
);
4010 } else if (autostart
) {