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
)
454 if (rtc_date_offset
== -1) {
458 localtime_r(&ti
, tm
);
460 ti
-= rtc_date_offset
;
465 int qemu_timedate_diff(struct tm
*tm
)
469 if (rtc_date_offset
== -1)
471 seconds
= mktimegm(tm
);
474 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
475 seconds
= mktime(&tmp
);
478 seconds
= mktimegm(tm
) + rtc_date_offset
;
480 return seconds
- time(NULL
);
483 void rtc_change_mon_event(struct tm
*tm
)
487 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
488 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
489 qobject_decref(data
);
492 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
494 time_t rtc_start_date
;
497 if (!strcmp(startdate
, "now") && legacy
) {
498 rtc_date_offset
= -1;
500 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
508 } else if (sscanf(startdate
, "%d-%d-%d",
520 rtc_start_date
= mktimegm(&tm
);
521 if (rtc_start_date
== -1) {
523 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
524 "'2006-06-17T16:01:21' or '2006-06-17'\n");
527 rtc_date_offset
= time(NULL
) - rtc_start_date
;
531 static void configure_rtc(QemuOpts
*opts
)
535 value
= qemu_opt_get(opts
, "base");
537 if (!strcmp(value
, "utc")) {
539 } else if (!strcmp(value
, "localtime")) {
542 configure_rtc_date_offset(value
, 0);
545 value
= qemu_opt_get(opts
, "clock");
547 if (!strcmp(value
, "host")) {
548 rtc_clock
= host_clock
;
549 } else if (!strcmp(value
, "rt")) {
550 rtc_clock
= rt_clock
;
551 } else if (!strcmp(value
, "vm")) {
552 rtc_clock
= vm_clock
;
554 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
558 value
= qemu_opt_get(opts
, "driftfix");
560 if (!strcmp(value
, "slew")) {
561 static GlobalProperty slew_lost_ticks
[] = {
563 .driver
= "mc146818rtc",
564 .property
= "lost_tick_policy",
567 { /* end of list */ }
570 qdev_prop_register_global_list(slew_lost_ticks
);
571 } else if (!strcmp(value
, "none")) {
572 /* discard is default */
574 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
580 /***********************************************************/
581 /* Bluetooth support */
584 static struct HCIInfo
*hci_table
[MAX_NICS
];
586 static struct bt_vlan_s
{
587 struct bt_scatternet_s net
;
589 struct bt_vlan_s
*next
;
592 /* find or alloc a new bluetooth "VLAN" */
593 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
595 struct bt_vlan_s
**pvlan
, *vlan
;
596 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
600 vlan
= g_malloc0(sizeof(struct bt_vlan_s
));
602 pvlan
= &first_bt_vlan
;
603 while (*pvlan
!= NULL
)
604 pvlan
= &(*pvlan
)->next
;
609 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
613 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
618 static struct HCIInfo null_hci
= {
619 .cmd_send
= null_hci_send
,
620 .sco_send
= null_hci_send
,
621 .acl_send
= null_hci_send
,
622 .bdaddr_set
= null_hci_addr_set
,
625 struct HCIInfo
*qemu_next_hci(void)
627 if (cur_hci
== nb_hcis
)
630 return hci_table
[cur_hci
++];
633 static struct HCIInfo
*hci_init(const char *str
)
636 struct bt_scatternet_s
*vlan
= 0;
638 if (!strcmp(str
, "null"))
641 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
643 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
644 else if (!strncmp(str
, "hci", 3)) {
647 if (!strncmp(str
+ 3, ",vlan=", 6)) {
648 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
653 vlan
= qemu_find_bt_vlan(0);
655 return bt_new_hci(vlan
);
658 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
663 static int bt_hci_parse(const char *str
)
668 if (nb_hcis
>= MAX_NICS
) {
669 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
682 bdaddr
.b
[5] = 0x56 + nb_hcis
;
683 hci
->bdaddr_set(hci
, bdaddr
.b
);
685 hci_table
[nb_hcis
++] = hci
;
690 static void bt_vhci_add(int vlan_id
)
692 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
695 fprintf(stderr
, "qemu: warning: adding a VHCI to "
696 "an empty scatternet %i\n", vlan_id
);
698 bt_vhci_init(bt_new_hci(vlan
));
701 static struct bt_device_s
*bt_device_add(const char *opt
)
703 struct bt_scatternet_s
*vlan
;
705 char *endp
= strstr(opt
, ",vlan=");
706 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
709 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
712 vlan_id
= strtol(endp
+ 6, &endp
, 0);
714 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
719 vlan
= qemu_find_bt_vlan(vlan_id
);
722 fprintf(stderr
, "qemu: warning: adding a slave device to "
723 "an empty scatternet %i\n", vlan_id
);
725 if (!strcmp(devname
, "keyboard"))
726 return bt_keyboard_init(vlan
);
728 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
732 static int bt_parse(const char *opt
)
734 const char *endp
, *p
;
737 if (strstart(opt
, "hci", &endp
)) {
738 if (!*endp
|| *endp
== ',') {
740 if (!strstart(endp
, ",vlan=", 0))
743 return bt_hci_parse(opt
);
745 } else if (strstart(opt
, "vhci", &endp
)) {
746 if (!*endp
|| *endp
== ',') {
748 if (strstart(endp
, ",vlan=", &p
)) {
749 vlan
= strtol(p
, (char **) &endp
, 0);
751 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
755 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
764 } else if (strstart(opt
, "device:", &endp
))
765 return !bt_device_add(endp
);
767 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
771 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
773 /* FIXME: change this to true for 1.3 */
774 if (qemu_opt_get_bool(opts
, "enable", false)) {
775 #ifdef CONFIG_SECCOMP
776 if (seccomp_start() < 0) {
777 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
778 "failed to install seccomp syscall filter in the kernel");
782 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
783 "sandboxing request but seccomp is not compiled into this build");
791 /*********QEMU USB setting******/
792 bool usb_enabled(bool default_usb
)
795 mach_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
797 return qemu_opt_get_bool(mach_opts
, "usb", default_usb
);
803 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
805 int fd
, dupfd
, flags
;
807 const char *fd_opaque
= NULL
;
809 fd
= qemu_opt_get_number(opts
, "fd", -1);
810 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
811 fd_opaque
= qemu_opt_get(opts
, "opaque");
814 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
815 "fd option is required and must be non-negative");
819 if (fd
<= STDERR_FILENO
) {
820 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
821 "fd cannot be a standard I/O stream");
826 * All fds inherited across exec() necessarily have FD_CLOEXEC
827 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
829 flags
= fcntl(fd
, F_GETFD
);
830 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
831 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
832 "fd is not valid or already in use");
837 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
838 "set option is required and must be non-negative");
842 #ifdef F_DUPFD_CLOEXEC
843 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
847 qemu_set_cloexec(dupfd
);
851 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
852 "Error duplicating fd: %s", strerror(errno
));
856 /* add the duplicate fd, and optionally the opaque string, to the fd set */
857 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
863 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
867 fd
= qemu_opt_get_number(opts
, "fd", -1);
874 /***********************************************************/
875 /* QEMU Block devices */
877 #define HD_OPTS "media=disk"
878 #define CDROM_OPTS "media=cdrom"
880 #define PFLASH_OPTS ""
884 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
886 BlockInterfaceType
*block_default_type
= opaque
;
888 return drive_init(opts
, *block_default_type
) == NULL
;
891 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
893 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
894 qemu_opt_set(opts
, "snapshot", "on");
899 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
900 int index
, const char *optstr
)
904 if (!enable
|| drive_get_by_index(type
, index
)) {
908 opts
= drive_add(type
, index
, NULL
, optstr
);
910 drive_enable_snapshot(opts
, NULL
);
912 if (!drive_init(opts
, type
)) {
917 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
919 boot_set_handler
= func
;
920 boot_set_opaque
= opaque
;
923 int qemu_boot_set(const char *boot_devices
)
925 if (!boot_set_handler
) {
928 return boot_set_handler(boot_set_opaque
, boot_devices
);
931 static void validate_bootdevices(char *devices
)
933 /* We just do some generic consistency checks */
937 for (p
= devices
; *p
!= '\0'; p
++) {
938 /* Allowed boot devices are:
939 * a-b: floppy disk drives
940 * c-f: IDE disk drives
941 * g-m: machine implementation dependent drives
942 * n-p: network devices
943 * It's up to each machine implementation to check if the given boot
944 * devices match the actual hardware implementation and firmware
947 if (*p
< 'a' || *p
> 'p') {
948 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
951 if (bitmap
& (1 << (*p
- 'a'))) {
952 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
955 bitmap
|= 1 << (*p
- 'a');
959 static void restore_boot_devices(void *opaque
)
961 char *standard_boot_devices
= opaque
;
962 static int first
= 1;
964 /* Restore boot order and remove ourselves after the first boot */
970 qemu_boot_set(standard_boot_devices
);
972 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
973 g_free(standard_boot_devices
);
976 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
979 FWBootEntry
*node
, *i
;
985 assert(dev
!= NULL
|| suffix
!= NULL
);
987 node
= g_malloc0(sizeof(FWBootEntry
));
988 node
->bootindex
= bootindex
;
989 node
->suffix
= suffix
? g_strdup(suffix
) : NULL
;
992 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
993 if (i
->bootindex
== bootindex
) {
994 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
996 } else if (i
->bootindex
< bootindex
) {
999 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1002 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1006 * This function returns null terminated string that consist of new line
1007 * separated device paths.
1009 * memory pointed by "size" is assigned total length of the array in bytes
1012 char *get_boot_devices_list(uint32_t *size
)
1018 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1019 char *devpath
= NULL
, *bootpath
;
1023 devpath
= qdev_get_fw_dev_path(i
->dev
);
1027 if (i
->suffix
&& devpath
) {
1028 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1030 bootpath
= g_malloc(bootpathlen
);
1031 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1033 } else if (devpath
) {
1037 bootpath
= g_strdup(i
->suffix
);
1041 list
[total
-1] = '\n';
1043 len
= strlen(bootpath
) + 1;
1044 list
= g_realloc(list
, total
+ len
);
1045 memcpy(&list
[total
], bootpath
, len
);
1055 static void numa_add(const char *optarg
)
1059 unsigned long long value
, endvalue
;
1062 value
= endvalue
= 0ULL;
1064 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
1065 if (!strcmp(option
, "node")) {
1066 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1067 nodenr
= nb_numa_nodes
;
1069 nodenr
= strtoull(option
, NULL
, 10);
1072 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1073 node_mem
[nodenr
] = 0;
1076 sval
= strtosz(option
, &endptr
);
1077 if (sval
< 0 || *endptr
) {
1078 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
1081 node_mem
[nodenr
] = sval
;
1083 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
1084 value
= strtoull(option
, &endptr
, 10);
1085 if (*endptr
== '-') {
1086 endvalue
= strtoull(endptr
+1, &endptr
, 10);
1091 if (!(endvalue
< MAX_CPUMASK_BITS
)) {
1092 endvalue
= MAX_CPUMASK_BITS
- 1;
1094 "A max of %d CPUs are supported in a guest\n",
1098 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
1104 static void smp_parse(const char *optarg
)
1106 int smp
, sockets
= 0, threads
= 0, cores
= 0;
1110 smp
= strtoul(optarg
, &endptr
, 10);
1111 if (endptr
!= optarg
) {
1112 if (*endptr
== ',') {
1116 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
1117 sockets
= strtoull(option
, NULL
, 10);
1118 if (get_param_value(option
, 128, "cores", endptr
) != 0)
1119 cores
= strtoull(option
, NULL
, 10);
1120 if (get_param_value(option
, 128, "threads", endptr
) != 0)
1121 threads
= strtoull(option
, NULL
, 10);
1122 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
1123 max_cpus
= strtoull(option
, NULL
, 10);
1125 /* compute missing values, prefer sockets over cores over threads */
1126 if (smp
== 0 || sockets
== 0) {
1127 sockets
= sockets
> 0 ? sockets
: 1;
1128 cores
= cores
> 0 ? cores
: 1;
1129 threads
= threads
> 0 ? threads
: 1;
1131 smp
= cores
* threads
* sockets
;
1135 threads
= threads
> 0 ? threads
: 1;
1136 cores
= smp
/ (sockets
* threads
);
1138 threads
= smp
/ (cores
* sockets
);
1142 smp_cores
= cores
> 0 ? cores
: 1;
1143 smp_threads
= threads
> 0 ? threads
: 1;
1145 max_cpus
= smp_cpus
;
1148 /***********************************************************/
1151 static int usb_device_add(const char *devname
)
1154 USBDevice
*dev
= NULL
;
1156 if (!usb_enabled(false)) {
1160 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1161 dev
= usbdevice_create(devname
);
1165 /* the other ones */
1166 #ifndef CONFIG_LINUX
1167 /* only the linux version is qdev-ified, usb-bsd still needs this */
1168 if (strstart(devname
, "host:", &p
)) {
1169 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1172 if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
1173 dev
= usb_bt_init(usb_bus_find(-1),
1174 devname
[2] ? hci_init(p
)
1175 : bt_new_hci(qemu_find_bt_vlan(0)));
1186 static int usb_device_del(const char *devname
)
1191 if (strstart(devname
, "host:", &p
))
1192 return usb_host_device_close(p
);
1194 if (!usb_enabled(false)) {
1198 p
= strchr(devname
, '.');
1201 bus_num
= strtoul(devname
, NULL
, 0);
1202 addr
= strtoul(p
+ 1, NULL
, 0);
1204 return usb_device_delete_addr(bus_num
, addr
);
1207 static int usb_parse(const char *cmdline
)
1210 r
= usb_device_add(cmdline
);
1212 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1217 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1219 const char *devname
= qdict_get_str(qdict
, "devname");
1220 if (usb_device_add(devname
) < 0) {
1221 error_report("could not add USB device '%s'", devname
);
1225 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1227 const char *devname
= qdict_get_str(qdict
, "devname");
1228 if (usb_device_del(devname
) < 0) {
1229 error_report("could not delete USB device '%s'", devname
);
1233 /***********************************************************/
1234 /* PCMCIA/Cardbus */
1236 static struct pcmcia_socket_entry_s
{
1237 PCMCIASocket
*socket
;
1238 struct pcmcia_socket_entry_s
*next
;
1239 } *pcmcia_sockets
= 0;
1241 void pcmcia_socket_register(PCMCIASocket
*socket
)
1243 struct pcmcia_socket_entry_s
*entry
;
1245 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1246 entry
->socket
= socket
;
1247 entry
->next
= pcmcia_sockets
;
1248 pcmcia_sockets
= entry
;
1251 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1253 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1255 ptr
= &pcmcia_sockets
;
1256 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1257 if (entry
->socket
== socket
) {
1263 void pcmcia_info(Monitor
*mon
)
1265 struct pcmcia_socket_entry_s
*iter
;
1267 if (!pcmcia_sockets
)
1268 monitor_printf(mon
, "No PCMCIA sockets\n");
1270 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1271 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1272 iter
->socket
->attached
? iter
->socket
->card_string
:
1276 /***********************************************************/
1277 /* machine registration */
1279 static QEMUMachine
*first_machine
= NULL
;
1280 QEMUMachine
*current_machine
= NULL
;
1282 int qemu_register_machine(QEMUMachine
*m
)
1285 pm
= &first_machine
;
1293 static QEMUMachine
*find_machine(const char *name
)
1297 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1298 if (!strcmp(m
->name
, name
))
1300 if (m
->alias
&& !strcmp(m
->alias
, name
))
1306 QEMUMachine
*find_default_machine(void)
1310 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1311 if (m
->is_default
) {
1318 MachineInfoList
*qmp_query_machines(Error
**errp
)
1320 MachineInfoList
*mach_list
= NULL
;
1323 for (m
= first_machine
; m
; m
= m
->next
) {
1324 MachineInfoList
*entry
;
1327 info
= g_malloc0(sizeof(*info
));
1328 if (m
->is_default
) {
1329 info
->has_is_default
= true;
1330 info
->is_default
= true;
1334 info
->has_alias
= true;
1335 info
->alias
= g_strdup(m
->alias
);
1338 info
->name
= g_strdup(m
->name
);
1340 entry
= g_malloc0(sizeof(*entry
));
1341 entry
->value
= info
;
1342 entry
->next
= mach_list
;
1349 /***********************************************************/
1350 /* main execution loop */
1352 static void gui_update(void *opaque
)
1354 uint64_t interval
= GUI_REFRESH_INTERVAL
;
1355 DisplayState
*ds
= opaque
;
1356 DisplayChangeListener
*dcl
;
1360 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1361 if (dcl
->gui_timer_interval
&&
1362 dcl
->gui_timer_interval
< interval
)
1363 interval
= dcl
->gui_timer_interval
;
1365 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock_ms(rt_clock
));
1368 void gui_setup_refresh(DisplayState
*ds
)
1370 DisplayChangeListener
*dcl
;
1371 bool need_timer
= false;
1372 bool have_gfx
= false;
1373 bool have_text
= false;
1375 QLIST_FOREACH(dcl
, &ds
->listeners
, next
) {
1376 if (dcl
->dpy_refresh
!= NULL
) {
1379 if (dcl
->dpy_gfx_update
!= NULL
) {
1382 if (dcl
->dpy_text_update
!= NULL
) {
1387 if (need_timer
&& ds
->gui_timer
== NULL
) {
1388 ds
->gui_timer
= qemu_new_timer_ms(rt_clock
, gui_update
, ds
);
1389 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock_ms(rt_clock
));
1391 if (!need_timer
&& ds
->gui_timer
!= NULL
) {
1392 qemu_del_timer(ds
->gui_timer
);
1393 qemu_free_timer(ds
->gui_timer
);
1394 ds
->gui_timer
= NULL
;
1397 ds
->have_gfx
= have_gfx
;
1398 ds
->have_text
= have_text
;
1401 struct vm_change_state_entry
{
1402 VMChangeStateHandler
*cb
;
1404 QLIST_ENTRY (vm_change_state_entry
) entries
;
1407 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1409 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1412 VMChangeStateEntry
*e
;
1414 e
= g_malloc0(sizeof (*e
));
1418 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1422 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1424 QLIST_REMOVE (e
, entries
);
1428 void vm_state_notify(int running
, RunState state
)
1430 VMChangeStateEntry
*e
;
1432 trace_vm_state_notify(running
, state
);
1434 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1435 e
->cb(e
->opaque
, running
, state
);
1441 if (!runstate_is_running()) {
1443 runstate_set(RUN_STATE_RUNNING
);
1444 vm_state_notify(1, RUN_STATE_RUNNING
);
1446 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1450 /* reset/shutdown handler */
1452 typedef struct QEMUResetEntry
{
1453 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1454 QEMUResetHandler
*func
;
1458 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1459 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1460 static int reset_requested
;
1461 static int shutdown_requested
, shutdown_signal
= -1;
1462 static pid_t shutdown_pid
;
1463 static int powerdown_requested
;
1464 static int debug_requested
;
1465 static int suspend_requested
;
1466 static int wakeup_requested
;
1467 static NotifierList powerdown_notifiers
=
1468 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1469 static NotifierList suspend_notifiers
=
1470 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1471 static NotifierList wakeup_notifiers
=
1472 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1473 static uint32_t wakeup_reason_mask
= ~0;
1474 static RunState vmstop_requested
= RUN_STATE_MAX
;
1476 int qemu_shutdown_requested_get(void)
1478 return shutdown_requested
;
1481 int qemu_reset_requested_get(void)
1483 return reset_requested
;
1486 static int qemu_shutdown_requested(void)
1488 int r
= shutdown_requested
;
1489 shutdown_requested
= 0;
1493 static void qemu_kill_report(void)
1495 if (!qtest_enabled() && shutdown_signal
!= -1) {
1496 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1497 if (shutdown_pid
== 0) {
1498 /* This happens for eg ^C at the terminal, so it's worth
1499 * avoiding printing an odd message in that case.
1501 fputc('\n', stderr
);
1503 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1505 shutdown_signal
= -1;
1509 static int qemu_reset_requested(void)
1511 int r
= reset_requested
;
1512 reset_requested
= 0;
1516 static int qemu_suspend_requested(void)
1518 int r
= suspend_requested
;
1519 suspend_requested
= 0;
1523 static int qemu_wakeup_requested(void)
1525 int r
= wakeup_requested
;
1526 wakeup_requested
= 0;
1530 static int qemu_powerdown_requested(void)
1532 int r
= powerdown_requested
;
1533 powerdown_requested
= 0;
1537 static int qemu_debug_requested(void)
1539 int r
= debug_requested
;
1540 debug_requested
= 0;
1544 /* We use RUN_STATE_MAX but any invalid value will do */
1545 static bool qemu_vmstop_requested(RunState
*r
)
1547 if (vmstop_requested
< RUN_STATE_MAX
) {
1548 *r
= vmstop_requested
;
1549 vmstop_requested
= RUN_STATE_MAX
;
1556 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1558 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1561 re
->opaque
= opaque
;
1562 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1565 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1569 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1570 if (re
->func
== func
&& re
->opaque
== opaque
) {
1571 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1578 void qemu_devices_reset(void)
1580 QEMUResetEntry
*re
, *nre
;
1582 /* reset all devices */
1583 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1584 re
->func(re
->opaque
);
1588 void qemu_system_reset(bool report
)
1590 if (current_machine
&& current_machine
->reset
) {
1591 current_machine
->reset();
1593 qemu_devices_reset();
1596 monitor_protocol_event(QEVENT_RESET
, NULL
);
1598 cpu_synchronize_all_post_reset();
1601 void qemu_system_reset_request(void)
1604 shutdown_requested
= 1;
1606 reset_requested
= 1;
1609 qemu_notify_event();
1612 static void qemu_system_suspend(void)
1615 notifier_list_notify(&suspend_notifiers
, NULL
);
1616 runstate_set(RUN_STATE_SUSPENDED
);
1617 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1620 void qemu_system_suspend_request(void)
1622 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1625 suspend_requested
= 1;
1627 qemu_notify_event();
1630 void qemu_register_suspend_notifier(Notifier
*notifier
)
1632 notifier_list_add(&suspend_notifiers
, notifier
);
1635 void qemu_system_wakeup_request(WakeupReason reason
)
1637 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1640 if (!(wakeup_reason_mask
& (1 << reason
))) {
1643 runstate_set(RUN_STATE_RUNNING
);
1644 notifier_list_notify(&wakeup_notifiers
, &reason
);
1645 wakeup_requested
= 1;
1646 qemu_notify_event();
1649 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1652 wakeup_reason_mask
|= (1 << reason
);
1654 wakeup_reason_mask
&= ~(1 << reason
);
1658 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1660 notifier_list_add(&wakeup_notifiers
, notifier
);
1663 void qemu_system_killed(int signal
, pid_t pid
)
1665 shutdown_signal
= signal
;
1668 qemu_system_shutdown_request();
1671 void qemu_system_shutdown_request(void)
1673 shutdown_requested
= 1;
1674 qemu_notify_event();
1677 static void qemu_system_powerdown(void)
1679 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1680 notifier_list_notify(&powerdown_notifiers
, NULL
);
1683 void qemu_system_powerdown_request(void)
1685 powerdown_requested
= 1;
1686 qemu_notify_event();
1689 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1691 notifier_list_add(&powerdown_notifiers
, notifier
);
1694 void qemu_system_debug_request(void)
1696 debug_requested
= 1;
1697 qemu_notify_event();
1700 void qemu_system_vmstop_request(RunState state
)
1702 vmstop_requested
= state
;
1703 qemu_notify_event();
1706 static bool main_loop_should_exit(void)
1709 if (qemu_debug_requested()) {
1710 vm_stop(RUN_STATE_DEBUG
);
1712 if (qemu_suspend_requested()) {
1713 qemu_system_suspend();
1715 if (qemu_shutdown_requested()) {
1717 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1719 vm_stop(RUN_STATE_SHUTDOWN
);
1724 if (qemu_reset_requested()) {
1726 cpu_synchronize_all_states();
1727 qemu_system_reset(VMRESET_REPORT
);
1729 if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1730 runstate_check(RUN_STATE_SHUTDOWN
)) {
1731 runstate_set(RUN_STATE_PAUSED
);
1734 if (qemu_wakeup_requested()) {
1736 cpu_synchronize_all_states();
1737 qemu_system_reset(VMRESET_SILENT
);
1739 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1741 if (qemu_powerdown_requested()) {
1742 qemu_system_powerdown();
1744 if (qemu_vmstop_requested(&r
)) {
1750 static void main_loop(void)
1754 #ifdef CONFIG_PROFILER
1758 nonblocking
= !kvm_enabled() && last_io
> 0;
1759 #ifdef CONFIG_PROFILER
1760 ti
= profile_getclock();
1762 last_io
= main_loop_wait(nonblocking
);
1763 #ifdef CONFIG_PROFILER
1764 dev_time
+= profile_getclock() - ti
;
1766 } while (!main_loop_should_exit());
1769 static void version(void)
1771 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1774 static void help(int exitcode
)
1777 printf("usage: %s [options] [disk_image]\n\n"
1778 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1779 error_get_progname());
1781 #define QEMU_OPTIONS_GENERATE_HELP
1782 #include "qemu-options-wrapper.h"
1784 printf("\nDuring emulation, the following keys are useful:\n"
1785 "ctrl-alt-f toggle full screen\n"
1786 "ctrl-alt-n switch to virtual console 'n'\n"
1787 "ctrl-alt toggle mouse and keyboard grab\n"
1789 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1794 #define HAS_ARG 0x0001
1796 typedef struct QEMUOption
{
1803 static const QEMUOption qemu_options
[] = {
1804 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1805 #define QEMU_OPTIONS_GENERATE_OPTIONS
1806 #include "qemu-options-wrapper.h"
1810 static bool vga_available(void)
1812 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1815 static bool cirrus_vga_available(void)
1817 return object_class_by_name("cirrus-vga")
1818 || object_class_by_name("isa-cirrus-vga");
1821 static bool vmware_vga_available(void)
1823 return object_class_by_name("vmware-svga");
1826 static bool qxl_vga_available(void)
1828 return object_class_by_name("qxl-vga");
1831 static void select_vgahw (const char *p
)
1835 vga_interface_type
= VGA_NONE
;
1836 if (strstart(p
, "std", &opts
)) {
1837 if (vga_available()) {
1838 vga_interface_type
= VGA_STD
;
1840 fprintf(stderr
, "Error: standard VGA not available\n");
1843 } else if (strstart(p
, "cirrus", &opts
)) {
1844 if (cirrus_vga_available()) {
1845 vga_interface_type
= VGA_CIRRUS
;
1847 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1850 } else if (strstart(p
, "vmware", &opts
)) {
1851 if (vmware_vga_available()) {
1852 vga_interface_type
= VGA_VMWARE
;
1854 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1857 } else if (strstart(p
, "xenfb", &opts
)) {
1858 vga_interface_type
= VGA_XENFB
;
1859 } else if (strstart(p
, "qxl", &opts
)) {
1860 if (qxl_vga_available()) {
1861 vga_interface_type
= VGA_QXL
;
1863 fprintf(stderr
, "Error: QXL VGA not available\n");
1866 } else if (!strstart(p
, "none", &opts
)) {
1868 fprintf(stderr
, "Unknown vga type: %s\n", p
);
1872 const char *nextopt
;
1874 if (strstart(opts
, ",retrace=", &nextopt
)) {
1876 if (strstart(opts
, "dumb", &nextopt
))
1877 vga_retrace_method
= VGA_RETRACE_DUMB
;
1878 else if (strstart(opts
, "precise", &nextopt
))
1879 vga_retrace_method
= VGA_RETRACE_PRECISE
;
1880 else goto invalid_vga
;
1881 } else goto invalid_vga
;
1886 static DisplayType
select_display(const char *p
)
1889 DisplayType display
= DT_DEFAULT
;
1891 if (strstart(p
, "sdl", &opts
)) {
1895 const char *nextopt
;
1897 if (strstart(opts
, ",frame=", &nextopt
)) {
1899 if (strstart(opts
, "on", &nextopt
)) {
1901 } else if (strstart(opts
, "off", &nextopt
)) {
1904 goto invalid_sdl_args
;
1906 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
1908 if (strstart(opts
, "on", &nextopt
)) {
1910 } else if (strstart(opts
, "off", &nextopt
)) {
1913 goto invalid_sdl_args
;
1915 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
1917 if (strstart(opts
, "on", &nextopt
)) {
1919 } else if (strstart(opts
, "off", &nextopt
)) {
1922 goto invalid_sdl_args
;
1924 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
1926 if (strstart(opts
, "on", &nextopt
)) {
1928 } else if (strstart(opts
, "off", &nextopt
)) {
1931 goto invalid_sdl_args
;
1935 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
1941 fprintf(stderr
, "SDL support is disabled\n");
1944 } else if (strstart(p
, "vnc", &opts
)) {
1949 const char *nextopt
;
1951 if (strstart(opts
, "=", &nextopt
)) {
1952 vnc_display
= nextopt
;
1956 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
1960 fprintf(stderr
, "VNC support is disabled\n");
1963 } else if (strstart(p
, "curses", &opts
)) {
1964 #ifdef CONFIG_CURSES
1965 display
= DT_CURSES
;
1967 fprintf(stderr
, "Curses support is disabled\n");
1970 } else if (strstart(p
, "none", &opts
)) {
1973 fprintf(stderr
, "Unknown display type: %s\n", p
);
1980 static int balloon_parse(const char *arg
)
1984 if (strcmp(arg
, "none") == 0) {
1988 if (!strncmp(arg
, "virtio", 6)) {
1989 if (arg
[6] == ',') {
1990 /* have params -> parse them */
1991 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
1995 /* create empty opts */
1996 opts
= qemu_opts_create_nofail(qemu_find_opts("device"));
1998 qemu_opt_set(opts
, "driver", "virtio-balloon");
2005 char *qemu_find_file(int type
, const char *name
)
2011 /* Try the name as a straight path first */
2012 if (access(name
, R_OK
) == 0) {
2013 return g_strdup(name
);
2016 case QEMU_FILE_TYPE_BIOS
:
2019 case QEMU_FILE_TYPE_KEYMAP
:
2020 subdir
= "keymaps/";
2025 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
2026 buf
= g_malloc0(len
);
2027 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
2028 if (access(buf
, R_OK
)) {
2035 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2037 return qdev_device_help(opts
);
2040 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2044 dev
= qdev_device_add(opts
);
2050 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2052 CharDriverState
*chr
;
2054 chr
= qemu_chr_new_from_opts(opts
, NULL
);
2060 #ifdef CONFIG_VIRTFS
2061 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2064 ret
= qemu_fsdev_add(opts
);
2070 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2072 CharDriverState
*chr
;
2073 const char *chardev
;
2077 mode
= qemu_opt_get(opts
, "mode");
2081 if (strcmp(mode
, "readline") == 0) {
2082 flags
= MONITOR_USE_READLINE
;
2083 } else if (strcmp(mode
, "control") == 0) {
2084 flags
= MONITOR_USE_CONTROL
;
2086 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2090 if (qemu_opt_get_bool(opts
, "pretty", 0))
2091 flags
|= MONITOR_USE_PRETTY
;
2093 if (qemu_opt_get_bool(opts
, "default", 0))
2094 flags
|= MONITOR_IS_DEFAULT
;
2096 chardev
= qemu_opt_get(opts
, "chardev");
2097 chr
= qemu_chr_find(chardev
);
2099 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2103 monitor_init(chr
, flags
);
2107 static void monitor_parse(const char *optarg
, const char *mode
)
2109 static int monitor_device_index
= 0;
2115 if (strstart(optarg
, "chardev:", &p
)) {
2116 snprintf(label
, sizeof(label
), "%s", p
);
2118 snprintf(label
, sizeof(label
), "compat_monitor%d",
2119 monitor_device_index
);
2120 if (monitor_device_index
== 0) {
2123 opts
= qemu_chr_parse_compat(label
, optarg
);
2125 fprintf(stderr
, "parse error: %s\n", optarg
);
2130 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2132 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2135 qemu_opt_set(opts
, "mode", mode
);
2136 qemu_opt_set(opts
, "chardev", label
);
2138 qemu_opt_set(opts
, "default", "on");
2139 monitor_device_index
++;
2142 struct device_config
{
2144 DEV_USB
, /* -usbdevice */
2146 DEV_SERIAL
, /* -serial */
2147 DEV_PARALLEL
, /* -parallel */
2148 DEV_VIRTCON
, /* -virtioconsole */
2149 DEV_DEBUGCON
, /* -debugcon */
2150 DEV_GDB
, /* -gdb, -s */
2152 const char *cmdline
;
2154 QTAILQ_ENTRY(device_config
) next
;
2157 static QTAILQ_HEAD(, device_config
) device_configs
=
2158 QTAILQ_HEAD_INITIALIZER(device_configs
);
2160 static void add_device_config(int type
, const char *cmdline
)
2162 struct device_config
*conf
;
2164 conf
= g_malloc0(sizeof(*conf
));
2166 conf
->cmdline
= cmdline
;
2167 loc_save(&conf
->loc
);
2168 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2171 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2173 struct device_config
*conf
;
2176 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2177 if (conf
->type
!= type
)
2179 loc_push_restore(&conf
->loc
);
2180 rc
= func(conf
->cmdline
);
2181 loc_pop(&conf
->loc
);
2188 static int serial_parse(const char *devname
)
2190 static int index
= 0;
2193 if (strcmp(devname
, "none") == 0)
2195 if (index
== MAX_SERIAL_PORTS
) {
2196 fprintf(stderr
, "qemu: too many serial ports\n");
2199 snprintf(label
, sizeof(label
), "serial%d", index
);
2200 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2201 if (!serial_hds
[index
]) {
2202 fprintf(stderr
, "qemu: could not connect serial device"
2203 " to character backend '%s'\n", devname
);
2210 static int parallel_parse(const char *devname
)
2212 static int index
= 0;
2215 if (strcmp(devname
, "none") == 0)
2217 if (index
== MAX_PARALLEL_PORTS
) {
2218 fprintf(stderr
, "qemu: too many parallel ports\n");
2221 snprintf(label
, sizeof(label
), "parallel%d", index
);
2222 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2223 if (!parallel_hds
[index
]) {
2224 fprintf(stderr
, "qemu: could not connect parallel device"
2225 " to character backend '%s'\n", devname
);
2232 static int virtcon_parse(const char *devname
)
2234 QemuOptsList
*device
= qemu_find_opts("device");
2235 static int index
= 0;
2237 QemuOpts
*bus_opts
, *dev_opts
;
2239 if (strcmp(devname
, "none") == 0)
2241 if (index
== MAX_VIRTIO_CONSOLES
) {
2242 fprintf(stderr
, "qemu: too many virtio consoles\n");
2246 bus_opts
= qemu_opts_create_nofail(device
);
2247 if (arch_type
== QEMU_ARCH_S390X
) {
2248 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2250 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2253 dev_opts
= qemu_opts_create_nofail(device
);
2254 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2256 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2257 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2258 if (!virtcon_hds
[index
]) {
2259 fprintf(stderr
, "qemu: could not connect virtio console"
2260 " to character backend '%s'\n", devname
);
2263 qemu_opt_set(dev_opts
, "chardev", label
);
2269 static int debugcon_parse(const char *devname
)
2273 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2276 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2278 fprintf(stderr
, "qemu: already have a debugcon device\n");
2281 qemu_opt_set(opts
, "driver", "isa-debugcon");
2282 qemu_opt_set(opts
, "chardev", "debugcon");
2286 static QEMUMachine
*machine_parse(const char *name
)
2288 QEMUMachine
*m
, *machine
= NULL
;
2291 machine
= find_machine(name
);
2296 printf("Supported machines are:\n");
2297 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2299 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2301 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2302 m
->is_default
? " (default)" : "");
2304 exit(!name
|| !is_help_option(name
));
2307 static int tcg_init(void)
2309 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2314 const char *opt_name
;
2316 int (*available
)(void);
2320 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2321 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2322 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2323 { "qtest", "QTest", qtest_available
, qtest_init
, &qtest_allowed
},
2326 static int configure_accelerator(void)
2328 const char *p
= NULL
;
2331 bool accel_initialised
= 0;
2332 bool init_failed
= 0;
2334 QemuOptsList
*list
= qemu_find_opts("machine");
2335 if (!QTAILQ_EMPTY(&list
->head
)) {
2336 p
= qemu_opt_get(QTAILQ_FIRST(&list
->head
), "accel");
2340 /* Use the default "accelerator", tcg */
2344 while (!accel_initialised
&& *p
!= '\0') {
2348 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2349 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2350 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2351 *(accel_list
[i
].allowed
) = 1;
2352 ret
= accel_list
[i
].init();
2355 if (!accel_list
[i
].available()) {
2356 printf("%s not supported for this target\n",
2357 accel_list
[i
].name
);
2359 fprintf(stderr
, "failed to initialize %s: %s\n",
2363 *(accel_list
[i
].allowed
) = 0;
2365 accel_initialised
= 1;
2370 if (i
== ARRAY_SIZE(accel_list
)) {
2371 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2375 if (!accel_initialised
) {
2376 fprintf(stderr
, "No accelerator found!\n");
2381 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2384 return !accel_initialised
;
2387 void qemu_add_exit_notifier(Notifier
*notify
)
2389 notifier_list_add(&exit_notifiers
, notify
);
2392 void qemu_remove_exit_notifier(Notifier
*notify
)
2394 notifier_remove(notify
);
2397 static void qemu_run_exit_notifiers(void)
2399 notifier_list_notify(&exit_notifiers
, NULL
);
2402 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2404 notifier_list_add(&machine_init_done_notifiers
, notify
);
2407 static void qemu_run_machine_init_done_notifiers(void)
2409 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2412 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2413 const char **poptarg
, int *poptind
)
2415 const QEMUOption
*popt
;
2416 int optind
= *poptind
;
2417 char *r
= argv
[optind
];
2420 loc_set_cmdline(argv
, optind
, 1);
2422 /* Treat --foo the same as -foo. */
2425 popt
= qemu_options
;
2428 error_report("invalid option");
2431 if (!strcmp(popt
->name
, r
+ 1))
2435 if (popt
->flags
& HAS_ARG
) {
2436 if (optind
>= argc
) {
2437 error_report("requires an argument");
2440 optarg
= argv
[optind
++];
2441 loc_set_cmdline(argv
, optind
- 2, 2);
2452 static gpointer
malloc_and_trace(gsize n_bytes
)
2454 void *ptr
= malloc(n_bytes
);
2455 trace_g_malloc(n_bytes
, ptr
);
2459 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2461 void *ptr
= realloc(mem
, n_bytes
);
2462 trace_g_realloc(mem
, n_bytes
, ptr
);
2466 static void free_and_trace(gpointer mem
)
2472 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2474 Object
*obj
= OBJECT(opaque
);
2475 StringInputVisitor
*siv
;
2476 Error
*local_err
= NULL
;
2478 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0) {
2482 siv
= string_input_visitor_new(value
);
2483 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2484 string_input_visitor_cleanup(siv
);
2487 qerror_report_err(local_err
);
2488 error_free(local_err
);
2495 static int object_create(QemuOpts
*opts
, void *opaque
)
2497 const char *type
= qemu_opt_get(opts
, "qom-type");
2498 const char *id
= qemu_opts_id(opts
);
2501 g_assert(type
!= NULL
);
2504 qerror_report(QERR_MISSING_PARAMETER
, "id");
2508 obj
= object_new(type
);
2509 if (qemu_opt_foreach(opts
, object_set_property
, obj
, 1) < 0) {
2513 object_property_add_child(container_get(object_get_root(), "/objects"),
2519 int main(int argc
, char **argv
, char **envp
)
2522 int snapshot
, linux_boot
;
2523 const char *icount_option
= NULL
;
2524 const char *initrd_filename
;
2525 const char *kernel_filename
, *kernel_cmdline
;
2526 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
2528 int cyls
, heads
, secs
, translation
;
2529 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2530 QemuOptsList
*olist
;
2533 const char *loadvm
= NULL
;
2534 QEMUMachine
*machine
;
2535 const char *cpu_model
;
2536 const char *vga_model
= "none";
2537 const char *pid_file
= NULL
;
2538 const char *incoming
= NULL
;
2540 int show_vnc_port
= 0;
2542 bool defconfig
= true;
2543 bool userconfig
= true;
2544 const char *log_mask
= NULL
;
2545 const char *log_file
= NULL
;
2546 GMemVTable mem_trace
= {
2547 .malloc
= malloc_and_trace
,
2548 .realloc
= realloc_and_trace
,
2549 .free
= free_and_trace
,
2551 const char *trace_events
= NULL
;
2552 const char *trace_file
= NULL
;
2554 atexit(qemu_run_exit_notifiers
);
2555 error_set_progname(argv
[0]);
2557 g_mem_set_vtable(&mem_trace
);
2558 if (!g_thread_supported()) {
2559 #if !GLIB_CHECK_VERSION(2, 31, 0)
2560 g_thread_init(NULL
);
2562 fprintf(stderr
, "glib threading failed to initialize.\n");
2567 module_call_init(MODULE_INIT_QOM
);
2572 rtc_clock
= host_clock
;
2574 qemu_cache_utils_init(envp
);
2576 QLIST_INIT (&vm_change_state_head
);
2577 os_setup_early_signal_handling();
2579 module_call_init(MODULE_INIT_MACHINE
);
2580 machine
= find_default_machine();
2584 cyls
= heads
= secs
= 0;
2585 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2587 for (i
= 0; i
< MAX_NODES
; i
++) {
2589 node_cpumask
[i
] = bitmap_new(MAX_CPUMASK_BITS
);
2597 /* first pass of option parsing */
2599 while (optind
< argc
) {
2600 if (argv
[optind
][0] != '-') {
2605 const QEMUOption
*popt
;
2607 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2608 switch (popt
->index
) {
2609 case QEMU_OPTION_nodefconfig
:
2612 case QEMU_OPTION_nouserconfig
:
2621 ret
= qemu_read_default_config_files(userconfig
);
2627 /* second pass of option parsing */
2632 if (argv
[optind
][0] != '-') {
2633 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2635 const QEMUOption
*popt
;
2637 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2638 if (!(popt
->arch_mask
& arch_type
)) {
2639 printf("Option %s not supported for this target\n", popt
->name
);
2642 switch(popt
->index
) {
2644 machine
= machine_parse(optarg
);
2646 case QEMU_OPTION_no_kvm_irqchip
: {
2647 olist
= qemu_find_opts("machine");
2648 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2651 case QEMU_OPTION_cpu
:
2652 /* hw initialization will check this */
2655 case QEMU_OPTION_hda
:
2659 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2661 snprintf(buf
, sizeof(buf
),
2662 "%s,cyls=%d,heads=%d,secs=%d%s",
2663 HD_OPTS
, cyls
, heads
, secs
,
2664 translation
== BIOS_ATA_TRANSLATION_LBA
?
2666 translation
== BIOS_ATA_TRANSLATION_NONE
?
2667 ",trans=none" : "");
2668 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2671 case QEMU_OPTION_hdb
:
2672 case QEMU_OPTION_hdc
:
2673 case QEMU_OPTION_hdd
:
2674 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2677 case QEMU_OPTION_drive
:
2678 if (drive_def(optarg
) == NULL
) {
2682 case QEMU_OPTION_set
:
2683 if (qemu_set_option(optarg
) != 0)
2686 case QEMU_OPTION_global
:
2687 if (qemu_global_option(optarg
) != 0)
2690 case QEMU_OPTION_mtdblock
:
2691 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2693 case QEMU_OPTION_sd
:
2694 drive_add(IF_SD
, 0, optarg
, SD_OPTS
);
2696 case QEMU_OPTION_pflash
:
2697 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2699 case QEMU_OPTION_snapshot
:
2702 case QEMU_OPTION_hdachs
:
2706 cyls
= strtol(p
, (char **)&p
, 0);
2707 if (cyls
< 1 || cyls
> 16383)
2712 heads
= strtol(p
, (char **)&p
, 0);
2713 if (heads
< 1 || heads
> 16)
2718 secs
= strtol(p
, (char **)&p
, 0);
2719 if (secs
< 1 || secs
> 63)
2723 if (!strcmp(p
, "none"))
2724 translation
= BIOS_ATA_TRANSLATION_NONE
;
2725 else if (!strcmp(p
, "lba"))
2726 translation
= BIOS_ATA_TRANSLATION_LBA
;
2727 else if (!strcmp(p
, "auto"))
2728 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2731 } else if (*p
!= '\0') {
2733 fprintf(stderr
, "qemu: invalid physical CHS format\n");
2736 if (hda_opts
!= NULL
) {
2738 snprintf(num
, sizeof(num
), "%d", cyls
);
2739 qemu_opt_set(hda_opts
, "cyls", num
);
2740 snprintf(num
, sizeof(num
), "%d", heads
);
2741 qemu_opt_set(hda_opts
, "heads", num
);
2742 snprintf(num
, sizeof(num
), "%d", secs
);
2743 qemu_opt_set(hda_opts
, "secs", num
);
2744 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
2745 qemu_opt_set(hda_opts
, "trans", "lba");
2746 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
2747 qemu_opt_set(hda_opts
, "trans", "none");
2751 case QEMU_OPTION_numa
:
2752 if (nb_numa_nodes
>= MAX_NODES
) {
2753 fprintf(stderr
, "qemu: too many NUMA nodes\n");
2758 case QEMU_OPTION_display
:
2759 display_type
= select_display(optarg
);
2761 case QEMU_OPTION_nographic
:
2762 display_type
= DT_NOGRAPHIC
;
2764 case QEMU_OPTION_curses
:
2765 #ifdef CONFIG_CURSES
2766 display_type
= DT_CURSES
;
2768 fprintf(stderr
, "Curses support is disabled\n");
2772 case QEMU_OPTION_portrait
:
2773 graphic_rotate
= 90;
2775 case QEMU_OPTION_rotate
:
2776 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
2777 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
2778 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
2780 "qemu: only 90, 180, 270 deg rotation is available\n");
2784 case QEMU_OPTION_kernel
:
2785 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
2787 case QEMU_OPTION_initrd
:
2788 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
2790 case QEMU_OPTION_append
:
2791 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
2793 case QEMU_OPTION_dtb
:
2794 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
2796 case QEMU_OPTION_cdrom
:
2797 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
2799 case QEMU_OPTION_boot
:
2801 static const char * const params
[] = {
2802 "order", "once", "menu",
2803 "splash", "splash-time",
2804 "reboot-timeout", NULL
2806 char buf
[sizeof(boot_devices
)];
2807 char *standard_boot_devices
;
2810 if (!strchr(optarg
, '=')) {
2812 pstrcpy(buf
, sizeof(buf
), optarg
);
2813 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
2815 "qemu: unknown boot parameter '%s' in '%s'\n",
2821 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
2822 validate_bootdevices(buf
);
2823 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2826 if (get_param_value(buf
, sizeof(buf
),
2828 validate_bootdevices(buf
);
2829 standard_boot_devices
= g_strdup(boot_devices
);
2830 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
2831 qemu_register_reset(restore_boot_devices
,
2832 standard_boot_devices
);
2834 if (get_param_value(buf
, sizeof(buf
),
2836 if (!strcmp(buf
, "on")) {
2838 } else if (!strcmp(buf
, "off")) {
2842 "qemu: invalid option value '%s'\n",
2847 qemu_opts_parse(qemu_find_opts("boot-opts"),
2852 case QEMU_OPTION_fda
:
2853 case QEMU_OPTION_fdb
:
2854 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
2857 case QEMU_OPTION_no_fd_bootchk
:
2860 case QEMU_OPTION_netdev
:
2861 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
2865 case QEMU_OPTION_net
:
2866 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
2870 #ifdef CONFIG_LIBISCSI
2871 case QEMU_OPTION_iscsi
:
2872 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
2879 case QEMU_OPTION_tftp
:
2880 legacy_tftp_prefix
= optarg
;
2882 case QEMU_OPTION_bootp
:
2883 legacy_bootp_filename
= optarg
;
2885 case QEMU_OPTION_redir
:
2886 if (net_slirp_redir(optarg
) < 0)
2890 case QEMU_OPTION_bt
:
2891 add_device_config(DEV_BT
, optarg
);
2893 case QEMU_OPTION_audio_help
:
2894 if (!(audio_available())) {
2895 printf("Option %s not supported for this target\n", popt
->name
);
2901 case QEMU_OPTION_soundhw
:
2902 if (!(audio_available())) {
2903 printf("Option %s not supported for this target\n", popt
->name
);
2906 select_soundhw (optarg
);
2911 case QEMU_OPTION_version
:
2915 case QEMU_OPTION_m
: {
2920 value
= strtosz(optarg
, &end
);
2921 if (value
< 0 || *end
) {
2922 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
2925 sz
= QEMU_ALIGN_UP((uint64_t)value
, 8192);
2927 if (ram_size
!= sz
) {
2928 fprintf(stderr
, "qemu: ram size too large\n");
2933 case QEMU_OPTION_mempath
:
2937 case QEMU_OPTION_mem_prealloc
:
2948 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
2950 case QEMU_OPTION_gdb
:
2951 add_device_config(DEV_GDB
, optarg
);
2956 case QEMU_OPTION_bios
:
2959 case QEMU_OPTION_singlestep
:
2966 keyboard_layout
= optarg
;
2968 case QEMU_OPTION_localtime
:
2971 case QEMU_OPTION_vga
:
2980 w
= strtol(p
, (char **)&p
, 10);
2983 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2989 h
= strtol(p
, (char **)&p
, 10);
2994 depth
= strtol(p
, (char **)&p
, 10);
2995 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2996 depth
!= 24 && depth
!= 32)
2998 } else if (*p
== '\0') {
2999 depth
= graphic_depth
;
3006 graphic_depth
= depth
;
3009 case QEMU_OPTION_echr
:
3012 term_escape_char
= strtol(optarg
, &r
, 0);
3014 printf("Bad argument to echr\n");
3017 case QEMU_OPTION_monitor
:
3018 monitor_parse(optarg
, "readline");
3019 default_monitor
= 0;
3021 case QEMU_OPTION_qmp
:
3022 monitor_parse(optarg
, "control");
3023 default_monitor
= 0;
3025 case QEMU_OPTION_mon
:
3026 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3030 default_monitor
= 0;
3032 case QEMU_OPTION_chardev
:
3033 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3038 case QEMU_OPTION_fsdev
:
3039 olist
= qemu_find_opts("fsdev");
3041 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3044 opts
= qemu_opts_parse(olist
, optarg
, 1);
3046 fprintf(stderr
, "parse error: %s\n", optarg
);
3050 case QEMU_OPTION_virtfs
: {
3053 const char *writeout
, *sock_fd
, *socket
;
3055 olist
= qemu_find_opts("virtfs");
3057 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3060 opts
= qemu_opts_parse(olist
, optarg
, 1);
3062 fprintf(stderr
, "parse error: %s\n", optarg
);
3066 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3067 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3068 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3071 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3072 qemu_opt_get(opts
, "mount_tag"),
3075 fprintf(stderr
, "duplicate fsdev id: %s\n",
3076 qemu_opt_get(opts
, "mount_tag"));
3080 writeout
= qemu_opt_get(opts
, "writeout");
3082 #ifdef CONFIG_SYNC_FILE_RANGE
3083 qemu_opt_set(fsdev
, "writeout", writeout
);
3085 fprintf(stderr
, "writeout=immediate not supported on "
3090 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3091 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3092 qemu_opt_set(fsdev
, "security_model",
3093 qemu_opt_get(opts
, "security_model"));
3094 socket
= qemu_opt_get(opts
, "socket");
3096 qemu_opt_set(fsdev
, "socket", socket
);
3098 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3100 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3103 qemu_opt_set_bool(fsdev
, "readonly",
3104 qemu_opt_get_bool(opts
, "readonly", 0));
3105 device
= qemu_opts_create_nofail(qemu_find_opts("device"));
3106 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3107 qemu_opt_set(device
, "fsdev",
3108 qemu_opt_get(opts
, "mount_tag"));
3109 qemu_opt_set(device
, "mount_tag",
3110 qemu_opt_get(opts
, "mount_tag"));
3113 case QEMU_OPTION_virtfs_synth
: {
3117 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3120 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3123 qemu_opt_set(fsdev
, "fsdriver", "synth");
3125 device
= qemu_opts_create_nofail(qemu_find_opts("device"));
3126 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3127 qemu_opt_set(device
, "fsdev", "v_synth");
3128 qemu_opt_set(device
, "mount_tag", "v_synth");
3131 case QEMU_OPTION_serial
:
3132 add_device_config(DEV_SERIAL
, optarg
);
3134 if (strncmp(optarg
, "mon:", 4) == 0) {
3135 default_monitor
= 0;
3138 case QEMU_OPTION_watchdog
:
3141 "qemu: only one watchdog option may be given\n");
3146 case QEMU_OPTION_watchdog_action
:
3147 if (select_watchdog_action(optarg
) == -1) {
3148 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3152 case QEMU_OPTION_virtiocon
:
3153 add_device_config(DEV_VIRTCON
, optarg
);
3154 default_virtcon
= 0;
3155 if (strncmp(optarg
, "mon:", 4) == 0) {
3156 default_monitor
= 0;
3159 case QEMU_OPTION_parallel
:
3160 add_device_config(DEV_PARALLEL
, optarg
);
3161 default_parallel
= 0;
3162 if (strncmp(optarg
, "mon:", 4) == 0) {
3163 default_monitor
= 0;
3166 case QEMU_OPTION_debugcon
:
3167 add_device_config(DEV_DEBUGCON
, optarg
);
3169 case QEMU_OPTION_loadvm
:
3172 case QEMU_OPTION_full_screen
:
3176 case QEMU_OPTION_no_frame
:
3179 case QEMU_OPTION_alt_grab
:
3182 case QEMU_OPTION_ctrl_grab
:
3185 case QEMU_OPTION_no_quit
:
3188 case QEMU_OPTION_sdl
:
3189 display_type
= DT_SDL
;
3192 case QEMU_OPTION_no_frame
:
3193 case QEMU_OPTION_alt_grab
:
3194 case QEMU_OPTION_ctrl_grab
:
3195 case QEMU_OPTION_no_quit
:
3196 case QEMU_OPTION_sdl
:
3197 fprintf(stderr
, "SDL support is disabled\n");
3200 case QEMU_OPTION_pidfile
:
3203 case QEMU_OPTION_win2k_hack
:
3204 win2k_install_hack
= 1;
3206 case QEMU_OPTION_rtc_td_hack
: {
3207 static GlobalProperty slew_lost_ticks
[] = {
3209 .driver
= "mc146818rtc",
3210 .property
= "lost_tick_policy",
3213 { /* end of list */ }
3216 qdev_prop_register_global_list(slew_lost_ticks
);
3219 case QEMU_OPTION_acpitable
:
3220 do_acpitable_option(optarg
);
3222 case QEMU_OPTION_smbios
:
3223 do_smbios_option(optarg
);
3225 case QEMU_OPTION_enable_kvm
:
3226 olist
= qemu_find_opts("machine");
3227 qemu_opts_parse(olist
, "accel=kvm", 0);
3229 case QEMU_OPTION_machine
:
3230 olist
= qemu_find_opts("machine");
3231 opts
= qemu_opts_parse(olist
, optarg
, 1);
3233 fprintf(stderr
, "parse error: %s\n", optarg
);
3236 optarg
= qemu_opt_get(opts
, "type");
3238 machine
= machine_parse(optarg
);
3241 case QEMU_OPTION_no_kvm
:
3242 olist
= qemu_find_opts("machine");
3243 qemu_opts_parse(olist
, "accel=tcg", 0);
3245 case QEMU_OPTION_no_kvm_pit
: {
3246 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3250 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3251 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3253 .driver
= "kvm-pit",
3254 .property
= "lost_tick_policy",
3257 { /* end of list */ }
3260 fprintf(stderr
, "Warning: option deprecated, use "
3261 "lost_tick_policy property of kvm-pit instead.\n");
3262 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3265 case QEMU_OPTION_usb
:
3266 olist
= qemu_find_opts("machine");
3267 qemu_opts_parse(olist
, "usb=on", 0);
3269 case QEMU_OPTION_usbdevice
:
3270 olist
= qemu_find_opts("machine");
3271 qemu_opts_parse(olist
, "usb=on", 0);
3272 add_device_config(DEV_USB
, optarg
);
3274 case QEMU_OPTION_device
:
3275 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3279 case QEMU_OPTION_smp
:
3282 fprintf(stderr
, "Invalid number of CPUs\n");
3285 if (max_cpus
< smp_cpus
) {
3286 fprintf(stderr
, "maxcpus must be equal to or greater than "
3290 if (max_cpus
> 255) {
3291 fprintf(stderr
, "Unsupported number of maxcpus\n");
3295 case QEMU_OPTION_vnc
:
3298 vnc_display
= optarg
;
3300 fprintf(stderr
, "VNC support is disabled\n");
3304 case QEMU_OPTION_no_acpi
:
3307 case QEMU_OPTION_no_hpet
:
3310 case QEMU_OPTION_balloon
:
3311 if (balloon_parse(optarg
) < 0) {
3312 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3316 case QEMU_OPTION_no_reboot
:
3319 case QEMU_OPTION_no_shutdown
:
3322 case QEMU_OPTION_show_cursor
:
3325 case QEMU_OPTION_uuid
:
3326 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3327 fprintf(stderr
, "Fail to parse UUID string."
3328 " Wrong format.\n");
3332 case QEMU_OPTION_option_rom
:
3333 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3334 fprintf(stderr
, "Too many option ROMs\n");
3337 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3338 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3339 option_rom
[nb_option_roms
].bootindex
=
3340 qemu_opt_get_number(opts
, "bootindex", -1);
3341 if (!option_rom
[nb_option_roms
].name
) {
3342 fprintf(stderr
, "Option ROM file is not specified\n");
3347 case QEMU_OPTION_semihosting
:
3348 semihosting_enabled
= 1;
3350 case QEMU_OPTION_tdf
:
3351 fprintf(stderr
, "Warning: user space PIT time drift fix "
3352 "is no longer supported.\n");
3354 case QEMU_OPTION_name
:
3355 qemu_name
= g_strdup(optarg
);
3357 char *p
= strchr(qemu_name
, ',');
3360 if (strncmp(p
, "process=", 8)) {
3361 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3365 os_set_proc_name(p
);
3369 case QEMU_OPTION_prom_env
:
3370 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3371 fprintf(stderr
, "Too many prom variables\n");
3374 prom_envs
[nb_prom_envs
] = optarg
;
3377 case QEMU_OPTION_old_param
:
3380 case QEMU_OPTION_clock
:
3381 configure_alarms(optarg
);
3383 case QEMU_OPTION_startdate
:
3384 configure_rtc_date_offset(optarg
, 1);
3386 case QEMU_OPTION_rtc
:
3387 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3391 configure_rtc(opts
);
3393 case QEMU_OPTION_tb_size
:
3394 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3395 if (tcg_tb_size
< 0) {
3399 case QEMU_OPTION_icount
:
3400 icount_option
= optarg
;
3402 case QEMU_OPTION_incoming
:
3404 runstate_set(RUN_STATE_INMIGRATE
);
3406 case QEMU_OPTION_nodefaults
:
3408 default_parallel
= 0;
3409 default_virtcon
= 0;
3410 default_monitor
= 0;
3417 case QEMU_OPTION_xen_domid
:
3418 if (!(xen_available())) {
3419 printf("Option %s not supported for this target\n", popt
->name
);
3422 xen_domid
= atoi(optarg
);
3424 case QEMU_OPTION_xen_create
:
3425 if (!(xen_available())) {
3426 printf("Option %s not supported for this target\n", popt
->name
);
3429 xen_mode
= XEN_CREATE
;
3431 case QEMU_OPTION_xen_attach
:
3432 if (!(xen_available())) {
3433 printf("Option %s not supported for this target\n", popt
->name
);
3436 xen_mode
= XEN_ATTACH
;
3438 case QEMU_OPTION_trace
:
3440 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3444 trace_events
= qemu_opt_get(opts
, "events");
3445 trace_file
= qemu_opt_get(opts
, "file");
3448 case QEMU_OPTION_readconfig
:
3450 int ret
= qemu_read_config_file(optarg
);
3452 fprintf(stderr
, "read config %s: %s\n", optarg
,
3458 case QEMU_OPTION_spice
:
3459 olist
= qemu_find_opts("spice");
3461 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3464 opts
= qemu_opts_parse(olist
, optarg
, 0);
3466 fprintf(stderr
, "parse error: %s\n", optarg
);
3470 case QEMU_OPTION_writeconfig
:
3473 if (strcmp(optarg
, "-") == 0) {
3476 fp
= fopen(optarg
, "w");
3478 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3482 qemu_config_write(fp
);
3486 case QEMU_OPTION_qtest
:
3487 qtest_chrdev
= optarg
;
3489 case QEMU_OPTION_qtest_log
:
3492 case QEMU_OPTION_sandbox
:
3493 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3498 case QEMU_OPTION_add_fd
:
3500 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3505 error_report("File descriptor passing is disabled on this "
3510 case QEMU_OPTION_object
:
3511 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3514 os_parse_cmd_args(popt
->index
, optarg
);
3520 if (qemu_init_main_loop()) {
3521 fprintf(stderr
, "qemu_init_main_loop failed\n");
3525 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3530 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3534 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3539 if (machine
== NULL
) {
3540 fprintf(stderr
, "No machine found.\n");
3544 if (machine
->hw_version
) {
3545 qemu_set_version(machine
->hw_version
);
3548 if (qemu_opts_foreach(qemu_find_opts("object"),
3549 object_create
, NULL
, 0) != 0) {
3553 /* Init CPU def lists, based on config
3554 * - Must be called after all the qemu_read_config_file() calls
3555 * - Must be called before list_cpus()
3556 * - Must be called before machine->init()
3560 if (cpu_model
&& is_help_option(cpu_model
)) {
3561 list_cpus(stdout
, &fprintf
, cpu_model
);
3565 /* Open the logfile at this point, if necessary. We can't open the logfile
3566 * when encountering either of the logging options (-d or -D) because the
3567 * other one may be encountered later on the command line, changing the
3568 * location or level of logging.
3572 set_cpu_log_filename(log_file
);
3574 set_cpu_log(log_mask
);
3577 if (!trace_backend_init(trace_events
, trace_file
)) {
3581 /* If no data_dir is specified then try to find it relative to the
3584 data_dir
= os_find_datadir(argv
[0]);
3586 /* If all else fails use the install path specified when building. */
3588 data_dir
= CONFIG_QEMU_DATADIR
;
3592 * Default to max_cpus = smp_cpus, in case the user doesn't
3593 * specify a max_cpus value.
3596 max_cpus
= smp_cpus
;
3598 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3599 if (smp_cpus
> machine
->max_cpus
) {
3600 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3601 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3607 * Get the default machine options from the machine if it is not already
3608 * specified either by the configuration file or by the command line.
3610 if (machine
->default_machine_opts
) {
3611 qemu_opts_set_defaults(qemu_find_opts("machine"),
3612 machine
->default_machine_opts
, 0);
3615 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3616 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3618 if (machine
->no_serial
) {
3621 if (machine
->no_parallel
) {
3622 default_parallel
= 0;
3624 if (!machine
->use_virtcon
) {
3625 default_virtcon
= 0;
3627 if (machine
->no_floppy
) {
3630 if (machine
->no_cdrom
) {
3633 if (machine
->no_sdcard
) {
3637 if (is_daemonized()) {
3638 /* According to documentation and historically, -nographic redirects
3639 * serial port, parallel port and monitor to stdio, which does not work
3640 * with -daemonize. We can redirect these to null instead, but since
3641 * -nographic is legacy, let's just error out.
3642 * We disallow -nographic only if all other ports are not redirected
3643 * explicitly, to not break existing legacy setups which uses
3644 * -nographic _and_ redirects all ports explicitly - this is valid
3645 * usage, -nographic is just a no-op in this case.
3647 if (display_type
== DT_NOGRAPHIC
3648 && (default_parallel
|| default_serial
3649 || default_monitor
|| default_virtcon
)) {
3650 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
3653 #ifdef CONFIG_CURSES
3654 if (display_type
== DT_CURSES
) {
3655 fprintf(stderr
, "curses display can not be used with -daemonize\n");
3661 if (display_type
== DT_NOGRAPHIC
) {
3662 if (default_parallel
)
3663 add_device_config(DEV_PARALLEL
, "null");
3664 if (default_serial
&& default_monitor
) {
3665 add_device_config(DEV_SERIAL
, "mon:stdio");
3666 } else if (default_virtcon
&& default_monitor
) {
3667 add_device_config(DEV_VIRTCON
, "mon:stdio");
3670 add_device_config(DEV_SERIAL
, "stdio");
3671 if (default_virtcon
)
3672 add_device_config(DEV_VIRTCON
, "stdio");
3673 if (default_monitor
)
3674 monitor_parse("stdio", "readline");
3678 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
3679 if (default_parallel
)
3680 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
3681 if (default_monitor
)
3682 monitor_parse("vc:80Cx24C", "readline");
3683 if (default_virtcon
)
3684 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
3689 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
3691 #ifdef CONFIG_VIRTFS
3692 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
3699 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
3704 /* init the memory */
3705 if (ram_size
== 0) {
3706 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3709 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
3714 configure_accelerator();
3716 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
3718 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
3719 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
3720 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
3722 kernel_filename
= initrd_filename
= kernel_cmdline
= NULL
;
3725 if (!kernel_cmdline
) {
3726 kernel_cmdline
= "";
3729 linux_boot
= (kernel_filename
!= NULL
);
3731 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
3732 fprintf(stderr
, "-append only allowed with -kernel option\n");
3736 if (!linux_boot
&& initrd_filename
!= NULL
) {
3737 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
3741 if (!linux_boot
&& machine_opts
&& qemu_opt_get(machine_opts
, "dtb")) {
3742 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
3746 os_set_line_buffering();
3748 qemu_init_cpu_loop();
3749 qemu_mutex_lock_iothread();
3752 /* spice needs the timers to be initialized by this point */
3756 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
3757 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
3760 configure_icount(icount_option
);
3762 /* clean up network at qemu process termination */
3763 atexit(&net_cleanup
);
3765 if (net_init_clients() < 0) {
3769 /* init the bluetooth world */
3770 if (foreach_device_config(DEV_BT
, bt_parse
))
3773 if (!xen_enabled()) {
3774 /* On 32-bit hosts, QEMU is limited by virtual address space */
3775 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
3776 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
3781 cpu_exec_init_all();
3783 bdrv_init_with_whitelist();
3787 /* open the virtual block devices */
3789 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
3790 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
3791 &machine
->block_default_type
, 1) != 0) {
3795 default_drive(default_cdrom
, snapshot
, machine
->block_default_type
, 2,
3797 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
3798 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
3800 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
3802 if (nb_numa_nodes
> 0) {
3805 if (nb_numa_nodes
> MAX_NODES
) {
3806 nb_numa_nodes
= MAX_NODES
;
3809 /* If no memory size if given for any node, assume the default case
3810 * and distribute the available memory equally across all nodes
3812 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3813 if (node_mem
[i
] != 0)
3816 if (i
== nb_numa_nodes
) {
3817 uint64_t usedmem
= 0;
3819 /* On Linux, the each node's border has to be 8MB aligned,
3820 * the final node gets the rest.
3822 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
3823 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
3824 usedmem
+= node_mem
[i
];
3826 node_mem
[i
] = ram_size
- usedmem
;
3829 for (i
= 0; i
< nb_numa_nodes
; i
++) {
3830 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
3834 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3835 * must cope with this anyway, because there are BIOSes out there in
3836 * real machines which also use this scheme.
3838 if (i
== nb_numa_nodes
) {
3839 for (i
= 0; i
< max_cpus
; i
++) {
3840 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
3845 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
3849 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
3851 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
3853 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
3855 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
3858 /* If no default VGA is requested, the default is "none". */
3860 if (cirrus_vga_available()) {
3861 vga_model
= "cirrus";
3862 } else if (vga_available()) {
3866 select_vgahw(vga_model
);
3869 i
= select_watchdog(watchdog
);
3871 exit (i
== 1 ? 1 : 0);
3874 if (machine
->compat_props
) {
3875 qdev_prop_register_global_list(machine
->compat_props
);
3879 qdev_machine_init();
3881 QEMUMachineInitArgs args
= { .ram_size
= ram_size
,
3882 .boot_device
= boot_devices
,
3883 .kernel_filename
= kernel_filename
,
3884 .kernel_cmdline
= kernel_cmdline
,
3885 .initrd_filename
= initrd_filename
,
3886 .cpu_model
= cpu_model
};
3887 machine
->init(&args
);
3889 cpu_synchronize_all_post_init();
3893 current_machine
= machine
;
3895 /* init USB devices */
3896 if (usb_enabled(false)) {
3897 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
3901 /* init generic devices */
3902 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
3905 net_check_clients();
3907 /* just use the first displaystate for the moment */
3908 ds
= get_displaystate();
3912 if (display_type
== DT_DEFAULT
&& !display_remote
) {
3913 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3914 display_type
= DT_SDL
;
3915 #elif defined(CONFIG_VNC)
3916 vnc_display
= "localhost:0,to=99";
3919 display_type
= DT_NONE
;
3924 /* init local displays */
3925 switch (display_type
) {
3928 #if defined(CONFIG_CURSES)
3930 curses_display_init(ds
, full_screen
);
3933 #if defined(CONFIG_SDL)
3935 sdl_display_init(ds
, full_screen
, no_frame
);
3937 #elif defined(CONFIG_COCOA)
3939 cocoa_display_init(ds
, full_screen
);
3946 /* must be after terminal init, SDL library changes signal handlers */
3947 os_setup_signal_handling();
3950 /* init remote displays */
3952 Error
*local_err
= NULL
;
3953 vnc_display_init(ds
);
3954 vnc_display_open(ds
, vnc_display
, &local_err
);
3955 if (local_err
!= NULL
) {
3956 fprintf(stderr
, "Failed to start VNC server on `%s': %s\n",
3957 vnc_display
, error_get_pretty(local_err
));
3958 error_free(local_err
);
3962 if (show_vnc_port
) {
3963 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
3968 if (using_spice
&& !qxl_enabled
) {
3969 qemu_spice_display_init(ds
);
3974 text_consoles_set_display(ds
);
3976 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
3980 qdev_machine_creation_done();
3982 if (rom_load_all() != 0) {
3983 fprintf(stderr
, "rom loading failed\n");
3987 /* TODO: once all bus devices are qdevified, this should be done
3988 * when bus is created by qdev.c */
3989 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
3990 qemu_run_machine_init_done_notifiers();
3992 qemu_system_reset(VMRESET_SILENT
);
3994 if (load_vmstate(loadvm
) < 0) {
4000 Error
*local_err
= NULL
;
4001 qemu_start_incoming_migration(incoming
, &local_err
);
4003 fprintf(stderr
, "-incoming %s: %s\n", incoming
, error_get_pretty(local_err
));
4004 error_free(local_err
);
4007 } else if (autostart
) {