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>
93 # define CONFIG_CHROOT
101 #if defined(__APPLE__) || defined(main)
103 int qemu_main(int argc
, char **argv
);
104 int main(int argc
, char **argv
)
106 return qemu_main(argc
, argv
);
109 #define main qemu_main
111 #endif /* CONFIG_SDL */
115 #define main qemu_main
116 #endif /* CONFIG_COCOA */
121 #include "hw/boards.h"
123 #include "hw/pcmcia.h"
124 #include "hw/i386/pc.h"
125 #include "hw/isa/isa.h"
127 #include "sysemu/watchdog.h"
128 #include "hw/i386/smbios.h"
129 #include "hw/xen/xen.h"
131 #include "hw/loader.h"
132 #include "monitor/qdev.h"
133 #include "sysemu/bt.h"
135 #include "net/slirp.h"
136 #include "monitor/monitor.h"
137 #include "ui/console.h"
138 #include "sysemu/sysemu.h"
139 #include "exec/gdbstub.h"
140 #include "qemu/timer.h"
141 #include "sysemu/char.h"
142 #include "qemu/cache-utils.h"
143 #include "sysemu/blockdev.h"
144 #include "hw/block/block.h"
145 #include "migration/block.h"
146 #include "sysemu/tpm.h"
147 #include "sysemu/dma.h"
148 #include "audio/audio.h"
149 #include "migration/migration.h"
150 #include "sysemu/kvm.h"
151 #include "qapi/qmp/qjson.h"
152 #include "qemu/option.h"
153 #include "qemu/config-file.h"
154 #include "qemu-options.h"
155 #include "qmp-commands.h"
156 #include "qemu/main-loop.h"
158 #include "fsdev/qemu-fsdev.h"
160 #include "sysemu/qtest.h"
162 #include "disas/disas.h"
164 #include "qemu/sockets.h"
166 #include "slirp/libslirp.h"
169 #include "trace/control.h"
170 #include "qemu/queue.h"
171 #include "sysemu/cpus.h"
172 #include "sysemu/arch_init.h"
173 #include "qemu/osdep.h"
175 #include "ui/qemu-spice.h"
176 #include "qapi/string-input-visitor.h"
179 //#define DEBUG_SLIRP
181 #define DEFAULT_RAM_SIZE 128
183 #define MAX_VIRTIO_CONSOLES 1
184 #define MAX_SCLP_CONSOLES 1
186 static const char *data_dir
[16];
187 static int data_dir_idx
;
188 const char *bios_name
= NULL
;
189 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
190 DisplayType display_type
= DT_DEFAULT
;
191 static int display_remote
;
192 const char* keyboard_layout
= NULL
;
194 const char *mem_path
= NULL
;
195 int mem_prealloc
= 0; /* force preallocation of physical target memory */
197 NICInfo nd_table
[MAX_NICS
];
199 static int rtc_utc
= 1;
200 static int rtc_date_offset
= -1; /* -1 means no change */
201 QEMUClockType rtc_clock
;
202 int vga_interface_type
= VGA_NONE
;
203 static int full_screen
= 0;
204 static int no_frame
= 0;
206 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
207 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
208 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
209 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
210 int win2k_install_hack
= 0;
217 const char *vnc_display
;
219 int acpi_enabled
= 1;
222 static int no_reboot
;
225 int graphic_rotate
= 0;
226 const char *watchdog
;
227 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
229 int semihosting_enabled
= 0;
231 const char *qemu_name
;
234 unsigned int nb_prom_envs
= 0;
235 const char *prom_envs
[MAX_PROM_ENVS
];
237 static bool boot_strict
;
238 uint8_t *boot_splash_filedata
;
239 size_t boot_splash_filedata_size
;
240 uint8_t qemu_extra_params_fw
[2];
242 typedef struct FWBootEntry FWBootEntry
;
245 QTAILQ_ENTRY(FWBootEntry
) link
;
251 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
252 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
255 uint64_t node_mem
[MAX_NODES
];
256 unsigned long *node_cpumask
[MAX_NODES
];
258 uint8_t qemu_uuid
[16];
261 /* Trace unassigned memory or i/o accesses. */
262 bool trace_unassigned
;
264 static QEMUBootSetHandler
*boot_set_handler
;
265 static void *boot_set_opaque
;
267 static NotifierList exit_notifiers
=
268 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
270 static NotifierList machine_init_done_notifiers
=
271 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
273 static bool tcg_allowed
= true;
276 enum xen_mode xen_mode
= XEN_EMULATE
;
277 static int tcg_tb_size
;
279 static int default_serial
= 1;
280 static int default_parallel
= 1;
281 static int default_virtcon
= 1;
282 static int default_sclp
= 1;
283 static int default_monitor
= 1;
284 static int default_floppy
= 1;
285 static int default_cdrom
= 1;
286 static int default_sdcard
= 1;
287 static int default_vga
= 1;
293 { .driver
= "isa-serial", .flag
= &default_serial
},
294 { .driver
= "isa-parallel", .flag
= &default_parallel
},
295 { .driver
= "isa-fdc", .flag
= &default_floppy
},
296 { .driver
= "ide-cd", .flag
= &default_cdrom
},
297 { .driver
= "ide-hd", .flag
= &default_cdrom
},
298 { .driver
= "ide-drive", .flag
= &default_cdrom
},
299 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
300 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
301 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
302 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
303 { .driver
= "VGA", .flag
= &default_vga
},
304 { .driver
= "isa-vga", .flag
= &default_vga
},
305 { .driver
= "cirrus-vga", .flag
= &default_vga
},
306 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
307 { .driver
= "vmware-svga", .flag
= &default_vga
},
308 { .driver
= "qxl-vga", .flag
= &default_vga
},
311 static QemuOptsList qemu_rtc_opts
= {
313 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
317 .type
= QEMU_OPT_STRING
,
320 .type
= QEMU_OPT_STRING
,
323 .type
= QEMU_OPT_STRING
,
325 { /* end of list */ }
329 static QemuOptsList qemu_sandbox_opts
= {
331 .implied_opt_name
= "enable",
332 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
336 .type
= QEMU_OPT_BOOL
,
338 { /* end of list */ }
342 static QemuOptsList qemu_trace_opts
= {
344 .implied_opt_name
= "trace",
345 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
349 .type
= QEMU_OPT_STRING
,
352 .type
= QEMU_OPT_STRING
,
354 { /* end of list */ }
358 static QemuOptsList qemu_option_rom_opts
= {
359 .name
= "option-rom",
360 .implied_opt_name
= "romfile",
361 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
365 .type
= QEMU_OPT_NUMBER
,
368 .type
= QEMU_OPT_STRING
,
370 { /* end of list */ }
374 static QemuOptsList qemu_machine_opts
= {
376 .implied_opt_name
= "type",
378 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
382 .type
= QEMU_OPT_STRING
,
383 .help
= "emulated machine"
386 .type
= QEMU_OPT_STRING
,
387 .help
= "accelerator list",
389 .name
= "kernel_irqchip",
390 .type
= QEMU_OPT_BOOL
,
391 .help
= "use KVM in-kernel irqchip",
393 .name
= "kvm_shadow_mem",
394 .type
= QEMU_OPT_SIZE
,
395 .help
= "KVM shadow MMU size",
398 .type
= QEMU_OPT_STRING
,
399 .help
= "Linux kernel image file",
402 .type
= QEMU_OPT_STRING
,
403 .help
= "Linux initial ramdisk file",
406 .type
= QEMU_OPT_STRING
,
407 .help
= "Linux kernel command line",
410 .type
= QEMU_OPT_STRING
,
411 .help
= "Linux kernel device tree file",
414 .type
= QEMU_OPT_STRING
,
415 .help
= "Dump current dtb to a file and quit",
417 .name
= "phandle_start",
418 .type
= QEMU_OPT_NUMBER
,
419 .help
= "The first phandle ID we may generate dynamically",
421 .name
= "dt_compatible",
422 .type
= QEMU_OPT_STRING
,
423 .help
= "Overrides the \"compatible\" property of the dt root node",
425 .name
= "dump-guest-core",
426 .type
= QEMU_OPT_BOOL
,
427 .help
= "Include guest memory in a core dump",
430 .type
= QEMU_OPT_BOOL
,
431 .help
= "enable/disable memory merge support",
434 .type
= QEMU_OPT_BOOL
,
435 .help
= "Set on/off to enable/disable usb",
438 .type
= QEMU_OPT_STRING
,
439 .help
= "firmware image",
441 { /* End of list */ }
445 static QemuOptsList qemu_boot_opts
= {
447 .implied_opt_name
= "order",
449 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
453 .type
= QEMU_OPT_STRING
,
456 .type
= QEMU_OPT_STRING
,
459 .type
= QEMU_OPT_BOOL
,
462 .type
= QEMU_OPT_STRING
,
464 .name
= "splash-time",
465 .type
= QEMU_OPT_STRING
,
467 .name
= "reboot-timeout",
468 .type
= QEMU_OPT_STRING
,
471 .type
= QEMU_OPT_BOOL
,
477 static QemuOptsList qemu_add_fd_opts
= {
479 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
483 .type
= QEMU_OPT_NUMBER
,
484 .help
= "file descriptor of which a duplicate is added to fd set",
487 .type
= QEMU_OPT_NUMBER
,
488 .help
= "ID of the fd set to add fd to",
491 .type
= QEMU_OPT_STRING
,
492 .help
= "free-form string used to describe fd",
494 { /* end of list */ }
498 static QemuOptsList qemu_object_opts
= {
500 .implied_opt_name
= "qom-type",
501 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
507 static QemuOptsList qemu_tpmdev_opts
= {
509 .implied_opt_name
= "type",
510 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
512 /* options are defined in the TPM backends */
513 { /* end of list */ }
517 static QemuOptsList qemu_realtime_opts
= {
519 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
523 .type
= QEMU_OPT_BOOL
,
525 { /* end of list */ }
529 static QemuOptsList qemu_msg_opts
= {
531 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
535 .type
= QEMU_OPT_BOOL
,
537 { /* end of list */ }
542 * Get machine options
544 * Returns: machine options (never null).
546 QemuOpts
*qemu_get_machine_opts(void)
551 list
= qemu_find_opts("machine");
553 opts
= qemu_opts_find(list
, NULL
);
555 opts
= qemu_opts_create(list
, NULL
, 0, &error_abort
);
560 const char *qemu_get_vm_name(void)
565 static void res_free(void)
567 if (boot_splash_filedata
!= NULL
) {
568 g_free(boot_splash_filedata
);
569 boot_splash_filedata
= NULL
;
573 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
575 const char *driver
= qemu_opt_get(opts
, "driver");
580 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
581 if (strcmp(default_list
[i
].driver
, driver
) != 0)
583 *(default_list
[i
].flag
) = 0;
588 /***********************************************************/
591 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
596 } RunStateTransition
;
598 static const RunStateTransition runstate_transitions_def
[] = {
600 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
601 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
603 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
604 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
606 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
607 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
609 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
610 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
612 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
613 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
615 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
616 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
618 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
619 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
620 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
622 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
623 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
625 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
627 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
628 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
629 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
630 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
631 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
632 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
633 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
634 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
635 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
636 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
638 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
640 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
641 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
643 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
644 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
645 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
646 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
648 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
649 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
651 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
652 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
654 { RUN_STATE_MAX
, RUN_STATE_MAX
},
657 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
659 bool runstate_check(RunState state
)
661 return current_run_state
== state
;
664 static void runstate_init(void)
666 const RunStateTransition
*p
;
668 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
670 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
671 runstate_valid_transitions
[p
->from
][p
->to
] = true;
675 /* This function will abort() on invalid state transitions */
676 void runstate_set(RunState new_state
)
678 assert(new_state
< RUN_STATE_MAX
);
680 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
681 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
682 RunState_lookup
[current_run_state
],
683 RunState_lookup
[new_state
]);
686 trace_runstate_set(new_state
);
687 current_run_state
= new_state
;
690 int runstate_is_running(void)
692 return runstate_check(RUN_STATE_RUNNING
);
695 bool runstate_needs_reset(void)
697 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
698 runstate_check(RUN_STATE_SHUTDOWN
);
701 StatusInfo
*qmp_query_status(Error
**errp
)
703 StatusInfo
*info
= g_malloc0(sizeof(*info
));
705 info
->running
= runstate_is_running();
706 info
->singlestep
= singlestep
;
707 info
->status
= current_run_state
;
712 /***********************************************************/
713 /* real time host monotonic timer */
715 /***********************************************************/
716 /* host time/date access */
717 void qemu_get_timedate(struct tm
*tm
, int offset
)
723 if (rtc_date_offset
== -1) {
727 localtime_r(&ti
, tm
);
729 ti
-= rtc_date_offset
;
734 int qemu_timedate_diff(struct tm
*tm
)
738 if (rtc_date_offset
== -1)
740 seconds
= mktimegm(tm
);
743 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
744 seconds
= mktime(&tmp
);
747 seconds
= mktimegm(tm
) + rtc_date_offset
;
749 return seconds
- time(NULL
);
752 void rtc_change_mon_event(struct tm
*tm
)
756 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
757 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
758 qobject_decref(data
);
761 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
763 time_t rtc_start_date
;
766 if (!strcmp(startdate
, "now") && legacy
) {
767 rtc_date_offset
= -1;
769 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
777 } else if (sscanf(startdate
, "%d-%d-%d",
789 rtc_start_date
= mktimegm(&tm
);
790 if (rtc_start_date
== -1) {
792 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
793 "'2006-06-17T16:01:21' or '2006-06-17'\n");
796 rtc_date_offset
= time(NULL
) - rtc_start_date
;
800 static void configure_rtc(QemuOpts
*opts
)
804 value
= qemu_opt_get(opts
, "base");
806 if (!strcmp(value
, "utc")) {
808 } else if (!strcmp(value
, "localtime")) {
811 configure_rtc_date_offset(value
, 0);
814 value
= qemu_opt_get(opts
, "clock");
816 if (!strcmp(value
, "host")) {
817 rtc_clock
= QEMU_CLOCK_HOST
;
818 } else if (!strcmp(value
, "rt")) {
819 rtc_clock
= QEMU_CLOCK_REALTIME
;
820 } else if (!strcmp(value
, "vm")) {
821 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
823 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
827 value
= qemu_opt_get(opts
, "driftfix");
829 if (!strcmp(value
, "slew")) {
830 static GlobalProperty slew_lost_ticks
[] = {
832 .driver
= "mc146818rtc",
833 .property
= "lost_tick_policy",
836 { /* end of list */ }
839 qdev_prop_register_global_list(slew_lost_ticks
);
840 } else if (!strcmp(value
, "none")) {
841 /* discard is default */
843 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
849 /***********************************************************/
850 /* Bluetooth support */
853 static struct HCIInfo
*hci_table
[MAX_NICS
];
855 struct HCIInfo
*qemu_next_hci(void)
857 if (cur_hci
== nb_hcis
)
860 return hci_table
[cur_hci
++];
863 static int bt_hci_parse(const char *str
)
868 if (nb_hcis
>= MAX_NICS
) {
869 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
882 bdaddr
.b
[5] = 0x56 + nb_hcis
;
883 hci
->bdaddr_set(hci
, bdaddr
.b
);
885 hci_table
[nb_hcis
++] = hci
;
890 static void bt_vhci_add(int vlan_id
)
892 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
895 fprintf(stderr
, "qemu: warning: adding a VHCI to "
896 "an empty scatternet %i\n", vlan_id
);
898 bt_vhci_init(bt_new_hci(vlan
));
901 static struct bt_device_s
*bt_device_add(const char *opt
)
903 struct bt_scatternet_s
*vlan
;
905 char *endp
= strstr(opt
, ",vlan=");
906 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
909 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
912 vlan_id
= strtol(endp
+ 6, &endp
, 0);
914 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
919 vlan
= qemu_find_bt_vlan(vlan_id
);
922 fprintf(stderr
, "qemu: warning: adding a slave device to "
923 "an empty scatternet %i\n", vlan_id
);
925 if (!strcmp(devname
, "keyboard"))
926 return bt_keyboard_init(vlan
);
928 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
932 static int bt_parse(const char *opt
)
934 const char *endp
, *p
;
937 if (strstart(opt
, "hci", &endp
)) {
938 if (!*endp
|| *endp
== ',') {
940 if (!strstart(endp
, ",vlan=", 0))
943 return bt_hci_parse(opt
);
945 } else if (strstart(opt
, "vhci", &endp
)) {
946 if (!*endp
|| *endp
== ',') {
948 if (strstart(endp
, ",vlan=", &p
)) {
949 vlan
= strtol(p
, (char **) &endp
, 0);
951 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
955 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
964 } else if (strstart(opt
, "device:", &endp
))
965 return !bt_device_add(endp
);
967 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
971 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
973 /* FIXME: change this to true for 1.3 */
974 if (qemu_opt_get_bool(opts
, "enable", false)) {
975 #ifdef CONFIG_SECCOMP
976 if (seccomp_start() < 0) {
977 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
978 "failed to install seccomp syscall filter in the kernel");
982 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
983 "sandboxing request but seccomp is not compiled into this build");
991 bool usb_enabled(bool default_usb
)
993 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb", default_usb
);
997 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
999 int fd
, dupfd
, flags
;
1001 const char *fd_opaque
= NULL
;
1003 fd
= qemu_opt_get_number(opts
, "fd", -1);
1004 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1005 fd_opaque
= qemu_opt_get(opts
, "opaque");
1008 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1009 "fd option is required and must be non-negative");
1013 if (fd
<= STDERR_FILENO
) {
1014 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1015 "fd cannot be a standard I/O stream");
1020 * All fds inherited across exec() necessarily have FD_CLOEXEC
1021 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1023 flags
= fcntl(fd
, F_GETFD
);
1024 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1025 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1026 "fd is not valid or already in use");
1031 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1032 "set option is required and must be non-negative");
1036 #ifdef F_DUPFD_CLOEXEC
1037 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1041 qemu_set_cloexec(dupfd
);
1045 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1046 "Error duplicating fd: %s", strerror(errno
));
1050 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1051 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1057 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1061 fd
= qemu_opt_get_number(opts
, "fd", -1);
1068 /***********************************************************/
1069 /* QEMU Block devices */
1071 #define HD_OPTS "media=disk"
1072 #define CDROM_OPTS "media=cdrom"
1074 #define PFLASH_OPTS ""
1078 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1080 BlockInterfaceType
*block_default_type
= opaque
;
1082 return drive_init(opts
, *block_default_type
) == NULL
;
1085 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1087 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1088 qemu_opt_set(opts
, "snapshot", "on");
1093 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1094 int index
, const char *optstr
)
1098 if (!enable
|| drive_get_by_index(type
, index
)) {
1102 opts
= drive_add(type
, index
, NULL
, optstr
);
1104 drive_enable_snapshot(opts
, NULL
);
1106 if (!drive_init(opts
, type
)) {
1111 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1113 boot_set_handler
= func
;
1114 boot_set_opaque
= opaque
;
1117 int qemu_boot_set(const char *boot_order
)
1119 if (!boot_set_handler
) {
1122 return boot_set_handler(boot_set_opaque
, boot_order
);
1125 static void validate_bootdevices(const char *devices
)
1127 /* We just do some generic consistency checks */
1131 for (p
= devices
; *p
!= '\0'; p
++) {
1132 /* Allowed boot devices are:
1133 * a-b: floppy disk drives
1134 * c-f: IDE disk drives
1135 * g-m: machine implementation dependent drives
1136 * n-p: network devices
1137 * It's up to each machine implementation to check if the given boot
1138 * devices match the actual hardware implementation and firmware
1141 if (*p
< 'a' || *p
> 'p') {
1142 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1145 if (bitmap
& (1 << (*p
- 'a'))) {
1146 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1149 bitmap
|= 1 << (*p
- 'a');
1153 static void restore_boot_order(void *opaque
)
1155 char *normal_boot_order
= opaque
;
1156 static int first
= 1;
1158 /* Restore boot order and remove ourselves after the first boot */
1164 qemu_boot_set(normal_boot_order
);
1166 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1167 g_free(normal_boot_order
);
1170 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1173 FWBootEntry
*node
, *i
;
1175 if (bootindex
< 0) {
1179 assert(dev
!= NULL
|| suffix
!= NULL
);
1181 node
= g_malloc0(sizeof(FWBootEntry
));
1182 node
->bootindex
= bootindex
;
1183 node
->suffix
= g_strdup(suffix
);
1186 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1187 if (i
->bootindex
== bootindex
) {
1188 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1190 } else if (i
->bootindex
< bootindex
) {
1193 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1196 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1199 DeviceState
*get_boot_device(uint32_t position
)
1201 uint32_t counter
= 0;
1202 FWBootEntry
*i
= NULL
;
1203 DeviceState
*res
= NULL
;
1205 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1206 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1207 if (counter
== position
) {
1218 * This function returns null terminated string that consist of new line
1219 * separated device paths.
1221 * memory pointed by "size" is assigned total length of the array in bytes
1224 char *get_boot_devices_list(size_t *size
)
1230 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1231 char *devpath
= NULL
, *bootpath
;
1235 devpath
= qdev_get_fw_dev_path(i
->dev
);
1239 if (i
->suffix
&& devpath
) {
1240 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1242 bootpath
= g_malloc(bootpathlen
);
1243 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1245 } else if (devpath
) {
1249 bootpath
= g_strdup(i
->suffix
);
1253 list
[total
-1] = '\n';
1255 len
= strlen(bootpath
) + 1;
1256 list
= g_realloc(list
, total
+ len
);
1257 memcpy(&list
[total
], bootpath
, len
);
1264 if (boot_strict
&& *size
> 0) {
1265 list
[total
-1] = '\n';
1266 list
= g_realloc(list
, total
+ 5);
1267 memcpy(&list
[total
], "HALT", 5);
1273 static void numa_node_parse_cpus(int nodenr
, const char *cpus
)
1276 unsigned long long value
, endvalue
;
1278 /* Empty CPU range strings will be considered valid, they will simply
1279 * not set any bit in the CPU bitmap.
1285 if (parse_uint(cpus
, &value
, &endptr
, 10) < 0) {
1288 if (*endptr
== '-') {
1289 if (parse_uint_full(endptr
+ 1, &endvalue
, 10) < 0) {
1292 } else if (*endptr
== '\0') {
1298 if (endvalue
>= MAX_CPUMASK_BITS
) {
1299 endvalue
= MAX_CPUMASK_BITS
- 1;
1301 "qemu: NUMA: A max of %d VCPUs are supported\n",
1305 if (endvalue
< value
) {
1309 bitmap_set(node_cpumask
[nodenr
], value
, endvalue
-value
+1);
1313 fprintf(stderr
, "qemu: Invalid NUMA CPU range: %s\n", cpus
);
1317 static void numa_add(const char *optarg
)
1321 unsigned long long nodenr
;
1323 optarg
= get_opt_name(option
, 128, optarg
, ',');
1324 if (*optarg
== ',') {
1327 if (!strcmp(option
, "node")) {
1329 if (nb_numa_nodes
>= MAX_NODES
) {
1330 fprintf(stderr
, "qemu: too many NUMA nodes\n");
1334 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
1335 nodenr
= nb_numa_nodes
;
1337 if (parse_uint_full(option
, &nodenr
, 10) < 0) {
1338 fprintf(stderr
, "qemu: Invalid NUMA nodeid: %s\n", option
);
1343 if (nodenr
>= MAX_NODES
) {
1344 fprintf(stderr
, "qemu: invalid NUMA nodeid: %llu\n", nodenr
);
1348 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
1349 node_mem
[nodenr
] = 0;
1352 sval
= strtosz(option
, &endptr
);
1353 if (sval
< 0 || *endptr
) {
1354 fprintf(stderr
, "qemu: invalid numa mem size: %s\n", optarg
);
1357 node_mem
[nodenr
] = sval
;
1359 if (get_param_value(option
, 128, "cpus", optarg
) != 0) {
1360 numa_node_parse_cpus(nodenr
, option
);
1364 fprintf(stderr
, "Invalid -numa option: %s\n", option
);
1369 static QemuOptsList qemu_smp_opts
= {
1371 .implied_opt_name
= "cpus",
1372 .merge_lists
= true,
1373 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1377 .type
= QEMU_OPT_NUMBER
,
1380 .type
= QEMU_OPT_NUMBER
,
1383 .type
= QEMU_OPT_NUMBER
,
1386 .type
= QEMU_OPT_NUMBER
,
1389 .type
= QEMU_OPT_NUMBER
,
1391 { /*End of list */ }
1395 static void smp_parse(QemuOpts
*opts
)
1399 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1400 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1401 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1402 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1404 /* compute missing values, prefer sockets over cores over threads */
1405 if (cpus
== 0 || sockets
== 0) {
1406 sockets
= sockets
> 0 ? sockets
: 1;
1407 cores
= cores
> 0 ? cores
: 1;
1408 threads
= threads
> 0 ? threads
: 1;
1410 cpus
= cores
* threads
* sockets
;
1414 threads
= threads
> 0 ? threads
: 1;
1415 cores
= cpus
/ (sockets
* threads
);
1417 threads
= cpus
/ (cores
* sockets
);
1421 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1424 smp_cores
= cores
> 0 ? cores
: 1;
1425 smp_threads
= threads
> 0 ? threads
: 1;
1429 if (max_cpus
== 0) {
1430 max_cpus
= smp_cpus
;
1433 if (max_cpus
> 255) {
1434 fprintf(stderr
, "Unsupported number of maxcpus\n");
1437 if (max_cpus
< smp_cpus
) {
1438 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1444 static void configure_realtime(QemuOpts
*opts
)
1448 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1451 if (os_mlock() < 0) {
1452 fprintf(stderr
, "qemu: locking memory failed\n");
1459 static void configure_msg(QemuOpts
*opts
)
1461 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1464 /***********************************************************/
1467 static int usb_device_add(const char *devname
)
1469 USBDevice
*dev
= NULL
;
1470 #ifndef CONFIG_LINUX
1474 if (!usb_enabled(false)) {
1478 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1479 dev
= usbdevice_create(devname
);
1483 /* the other ones */
1484 #ifndef CONFIG_LINUX
1485 /* only the linux version is qdev-ified, usb-bsd still needs this */
1486 if (strstart(devname
, "host:", &p
)) {
1487 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1497 static int usb_device_del(const char *devname
)
1502 if (strstart(devname
, "host:", &p
)) {
1506 if (!usb_enabled(false)) {
1510 p
= strchr(devname
, '.');
1513 bus_num
= strtoul(devname
, NULL
, 0);
1514 addr
= strtoul(p
+ 1, NULL
, 0);
1516 return usb_device_delete_addr(bus_num
, addr
);
1519 static int usb_parse(const char *cmdline
)
1522 r
= usb_device_add(cmdline
);
1524 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1529 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1531 const char *devname
= qdict_get_str(qdict
, "devname");
1532 if (usb_device_add(devname
) < 0) {
1533 error_report("could not add USB device '%s'", devname
);
1537 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1539 const char *devname
= qdict_get_str(qdict
, "devname");
1540 if (usb_device_del(devname
) < 0) {
1541 error_report("could not delete USB device '%s'", devname
);
1545 /***********************************************************/
1546 /* PCMCIA/Cardbus */
1548 static struct pcmcia_socket_entry_s
{
1549 PCMCIASocket
*socket
;
1550 struct pcmcia_socket_entry_s
*next
;
1551 } *pcmcia_sockets
= 0;
1553 void pcmcia_socket_register(PCMCIASocket
*socket
)
1555 struct pcmcia_socket_entry_s
*entry
;
1557 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1558 entry
->socket
= socket
;
1559 entry
->next
= pcmcia_sockets
;
1560 pcmcia_sockets
= entry
;
1563 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1565 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1567 ptr
= &pcmcia_sockets
;
1568 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1569 if (entry
->socket
== socket
) {
1575 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1577 struct pcmcia_socket_entry_s
*iter
;
1579 if (!pcmcia_sockets
)
1580 monitor_printf(mon
, "No PCMCIA sockets\n");
1582 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1583 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1584 iter
->socket
->attached
? iter
->socket
->card_string
:
1588 /***********************************************************/
1589 /* machine registration */
1591 static QEMUMachine
*first_machine
= NULL
;
1592 QEMUMachine
*current_machine
= NULL
;
1594 int qemu_register_machine(QEMUMachine
*m
)
1597 pm
= &first_machine
;
1605 static QEMUMachine
*find_machine(const char *name
)
1609 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1610 if (!strcmp(m
->name
, name
))
1612 if (m
->alias
&& !strcmp(m
->alias
, name
))
1618 QEMUMachine
*find_default_machine(void)
1622 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
1623 if (m
->is_default
) {
1630 MachineInfoList
*qmp_query_machines(Error
**errp
)
1632 MachineInfoList
*mach_list
= NULL
;
1635 for (m
= first_machine
; m
; m
= m
->next
) {
1636 MachineInfoList
*entry
;
1639 info
= g_malloc0(sizeof(*info
));
1640 if (m
->is_default
) {
1641 info
->has_is_default
= true;
1642 info
->is_default
= true;
1646 info
->has_alias
= true;
1647 info
->alias
= g_strdup(m
->alias
);
1650 info
->name
= g_strdup(m
->name
);
1651 info
->cpu_max
= !m
->max_cpus
? 1 : m
->max_cpus
;
1653 entry
= g_malloc0(sizeof(*entry
));
1654 entry
->value
= info
;
1655 entry
->next
= mach_list
;
1662 /***********************************************************/
1663 /* main execution loop */
1665 struct vm_change_state_entry
{
1666 VMChangeStateHandler
*cb
;
1668 QLIST_ENTRY (vm_change_state_entry
) entries
;
1671 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1673 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1676 VMChangeStateEntry
*e
;
1678 e
= g_malloc0(sizeof (*e
));
1682 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1686 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1688 QLIST_REMOVE (e
, entries
);
1692 void vm_state_notify(int running
, RunState state
)
1694 VMChangeStateEntry
*e
;
1696 trace_vm_state_notify(running
, state
);
1698 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1699 e
->cb(e
->opaque
, running
, state
);
1705 if (!runstate_is_running()) {
1707 runstate_set(RUN_STATE_RUNNING
);
1708 vm_state_notify(1, RUN_STATE_RUNNING
);
1710 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1714 /* reset/shutdown handler */
1716 typedef struct QEMUResetEntry
{
1717 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1718 QEMUResetHandler
*func
;
1722 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1723 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1724 static int reset_requested
;
1725 static int shutdown_requested
, shutdown_signal
= -1;
1726 static pid_t shutdown_pid
;
1727 static int powerdown_requested
;
1728 static int debug_requested
;
1729 static int suspend_requested
;
1730 static WakeupReason wakeup_reason
;
1731 static NotifierList powerdown_notifiers
=
1732 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1733 static NotifierList suspend_notifiers
=
1734 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1735 static NotifierList wakeup_notifiers
=
1736 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1737 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1738 static RunState vmstop_requested
= RUN_STATE_MAX
;
1740 int qemu_shutdown_requested_get(void)
1742 return shutdown_requested
;
1745 int qemu_reset_requested_get(void)
1747 return reset_requested
;
1750 static int qemu_shutdown_requested(void)
1752 int r
= shutdown_requested
;
1753 shutdown_requested
= 0;
1757 static void qemu_kill_report(void)
1759 if (!qtest_enabled() && shutdown_signal
!= -1) {
1760 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1761 if (shutdown_pid
== 0) {
1762 /* This happens for eg ^C at the terminal, so it's worth
1763 * avoiding printing an odd message in that case.
1765 fputc('\n', stderr
);
1767 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1769 shutdown_signal
= -1;
1773 static int qemu_reset_requested(void)
1775 int r
= reset_requested
;
1776 reset_requested
= 0;
1780 static int qemu_suspend_requested(void)
1782 int r
= suspend_requested
;
1783 suspend_requested
= 0;
1787 static WakeupReason
qemu_wakeup_requested(void)
1789 return wakeup_reason
;
1792 static int qemu_powerdown_requested(void)
1794 int r
= powerdown_requested
;
1795 powerdown_requested
= 0;
1799 static int qemu_debug_requested(void)
1801 int r
= debug_requested
;
1802 debug_requested
= 0;
1806 /* We use RUN_STATE_MAX but any invalid value will do */
1807 static bool qemu_vmstop_requested(RunState
*r
)
1809 if (vmstop_requested
< RUN_STATE_MAX
) {
1810 *r
= vmstop_requested
;
1811 vmstop_requested
= RUN_STATE_MAX
;
1818 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1820 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1823 re
->opaque
= opaque
;
1824 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1827 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1831 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1832 if (re
->func
== func
&& re
->opaque
== opaque
) {
1833 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1840 void qemu_devices_reset(void)
1842 QEMUResetEntry
*re
, *nre
;
1844 /* reset all devices */
1845 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1846 re
->func(re
->opaque
);
1850 void qemu_system_reset(bool report
)
1852 if (current_machine
&& current_machine
->reset
) {
1853 current_machine
->reset();
1855 qemu_devices_reset();
1858 monitor_protocol_event(QEVENT_RESET
, NULL
);
1860 cpu_synchronize_all_post_reset();
1863 void qemu_system_reset_request(void)
1866 shutdown_requested
= 1;
1868 reset_requested
= 1;
1871 qemu_notify_event();
1874 static void qemu_system_suspend(void)
1877 notifier_list_notify(&suspend_notifiers
, NULL
);
1878 runstate_set(RUN_STATE_SUSPENDED
);
1879 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1882 void qemu_system_suspend_request(void)
1884 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1887 suspend_requested
= 1;
1889 qemu_notify_event();
1892 void qemu_register_suspend_notifier(Notifier
*notifier
)
1894 notifier_list_add(&suspend_notifiers
, notifier
);
1897 void qemu_system_wakeup_request(WakeupReason reason
)
1899 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1902 if (!(wakeup_reason_mask
& (1 << reason
))) {
1905 runstate_set(RUN_STATE_RUNNING
);
1906 wakeup_reason
= reason
;
1907 qemu_notify_event();
1910 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1913 wakeup_reason_mask
|= (1 << reason
);
1915 wakeup_reason_mask
&= ~(1 << reason
);
1919 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1921 notifier_list_add(&wakeup_notifiers
, notifier
);
1924 void qemu_system_killed(int signal
, pid_t pid
)
1926 shutdown_signal
= signal
;
1929 qemu_system_shutdown_request();
1932 void qemu_system_shutdown_request(void)
1934 shutdown_requested
= 1;
1935 qemu_notify_event();
1938 static void qemu_system_powerdown(void)
1940 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1941 notifier_list_notify(&powerdown_notifiers
, NULL
);
1944 void qemu_system_powerdown_request(void)
1946 powerdown_requested
= 1;
1947 qemu_notify_event();
1950 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1952 notifier_list_add(&powerdown_notifiers
, notifier
);
1955 void qemu_system_debug_request(void)
1957 debug_requested
= 1;
1958 qemu_notify_event();
1961 void qemu_system_vmstop_request(RunState state
)
1963 vmstop_requested
= state
;
1964 qemu_notify_event();
1967 static bool main_loop_should_exit(void)
1970 if (qemu_debug_requested()) {
1971 vm_stop(RUN_STATE_DEBUG
);
1973 if (qemu_suspend_requested()) {
1974 qemu_system_suspend();
1976 if (qemu_shutdown_requested()) {
1978 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1980 vm_stop(RUN_STATE_SHUTDOWN
);
1985 if (qemu_reset_requested()) {
1987 cpu_synchronize_all_states();
1988 qemu_system_reset(VMRESET_REPORT
);
1990 if (runstate_needs_reset()) {
1991 runstate_set(RUN_STATE_PAUSED
);
1994 if (qemu_wakeup_requested()) {
1996 cpu_synchronize_all_states();
1997 qemu_system_reset(VMRESET_SILENT
);
1998 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1999 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
2001 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
2003 if (qemu_powerdown_requested()) {
2004 qemu_system_powerdown();
2006 if (qemu_vmstop_requested(&r
)) {
2012 static void main_loop(void)
2016 #ifdef CONFIG_PROFILER
2020 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2021 #ifdef CONFIG_PROFILER
2022 ti
= profile_getclock();
2024 last_io
= main_loop_wait(nonblocking
);
2025 #ifdef CONFIG_PROFILER
2026 dev_time
+= profile_getclock() - ti
;
2028 } while (!main_loop_should_exit());
2031 static void version(void)
2033 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2036 static void QEMU_NORETURN
help(int exitcode
)
2039 printf("usage: %s [options] [disk_image]\n\n"
2040 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2041 error_get_progname());
2043 #define QEMU_OPTIONS_GENERATE_HELP
2044 #include "qemu-options-wrapper.h"
2046 printf("\nDuring emulation, the following keys are useful:\n"
2047 "ctrl-alt-f toggle full screen\n"
2048 "ctrl-alt-n switch to virtual console 'n'\n"
2049 "ctrl-alt toggle mouse and keyboard grab\n"
2051 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2056 #define HAS_ARG 0x0001
2058 typedef struct QEMUOption
{
2065 static const QEMUOption qemu_options
[] = {
2066 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2067 #define QEMU_OPTIONS_GENERATE_OPTIONS
2068 #include "qemu-options-wrapper.h"
2072 static bool vga_available(void)
2074 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2077 static bool cirrus_vga_available(void)
2079 return object_class_by_name("cirrus-vga")
2080 || object_class_by_name("isa-cirrus-vga");
2083 static bool vmware_vga_available(void)
2085 return object_class_by_name("vmware-svga");
2088 static bool qxl_vga_available(void)
2090 return object_class_by_name("qxl-vga");
2093 static void select_vgahw (const char *p
)
2097 vga_interface_type
= VGA_NONE
;
2098 if (strstart(p
, "std", &opts
)) {
2099 if (vga_available()) {
2100 vga_interface_type
= VGA_STD
;
2102 fprintf(stderr
, "Error: standard VGA not available\n");
2105 } else if (strstart(p
, "cirrus", &opts
)) {
2106 if (cirrus_vga_available()) {
2107 vga_interface_type
= VGA_CIRRUS
;
2109 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2112 } else if (strstart(p
, "vmware", &opts
)) {
2113 if (vmware_vga_available()) {
2114 vga_interface_type
= VGA_VMWARE
;
2116 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2119 } else if (strstart(p
, "xenfb", &opts
)) {
2120 vga_interface_type
= VGA_XENFB
;
2121 } else if (strstart(p
, "qxl", &opts
)) {
2122 if (qxl_vga_available()) {
2123 vga_interface_type
= VGA_QXL
;
2125 fprintf(stderr
, "Error: QXL VGA not available\n");
2128 } else if (!strstart(p
, "none", &opts
)) {
2130 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2134 const char *nextopt
;
2136 if (strstart(opts
, ",retrace=", &nextopt
)) {
2138 if (strstart(opts
, "dumb", &nextopt
))
2139 vga_retrace_method
= VGA_RETRACE_DUMB
;
2140 else if (strstart(opts
, "precise", &nextopt
))
2141 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2142 else goto invalid_vga
;
2143 } else goto invalid_vga
;
2148 static DisplayType
select_display(const char *p
)
2151 DisplayType display
= DT_DEFAULT
;
2153 if (strstart(p
, "sdl", &opts
)) {
2157 const char *nextopt
;
2159 if (strstart(opts
, ",frame=", &nextopt
)) {
2161 if (strstart(opts
, "on", &nextopt
)) {
2163 } else if (strstart(opts
, "off", &nextopt
)) {
2166 goto invalid_sdl_args
;
2168 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2170 if (strstart(opts
, "on", &nextopt
)) {
2172 } else if (strstart(opts
, "off", &nextopt
)) {
2175 goto invalid_sdl_args
;
2177 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2179 if (strstart(opts
, "on", &nextopt
)) {
2181 } else if (strstart(opts
, "off", &nextopt
)) {
2184 goto invalid_sdl_args
;
2186 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2188 if (strstart(opts
, "on", &nextopt
)) {
2190 } else if (strstart(opts
, "off", &nextopt
)) {
2193 goto invalid_sdl_args
;
2197 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2203 fprintf(stderr
, "SDL support is disabled\n");
2206 } else if (strstart(p
, "vnc", &opts
)) {
2211 const char *nextopt
;
2213 if (strstart(opts
, "=", &nextopt
)) {
2214 vnc_display
= nextopt
;
2218 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2222 fprintf(stderr
, "VNC support is disabled\n");
2225 } else if (strstart(p
, "curses", &opts
)) {
2226 #ifdef CONFIG_CURSES
2227 display
= DT_CURSES
;
2229 fprintf(stderr
, "Curses support is disabled\n");
2232 } else if (strstart(p
, "gtk", &opts
)) {
2236 fprintf(stderr
, "GTK support is disabled\n");
2239 } else if (strstart(p
, "none", &opts
)) {
2242 fprintf(stderr
, "Unknown display type: %s\n", p
);
2249 static int balloon_parse(const char *arg
)
2253 if (strcmp(arg
, "none") == 0) {
2257 if (!strncmp(arg
, "virtio", 6)) {
2258 if (arg
[6] == ',') {
2259 /* have params -> parse them */
2260 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2264 /* create empty opts */
2265 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2268 qemu_opt_set(opts
, "driver", "virtio-balloon");
2275 #if defined(CONFIG_RUBY)
2276 /* Ruby interface for QEMU. See README.EXT for programming example. */
2280 static VALUE
qemu_open(int argc
, VALUE
*argv
, VALUE klass
)
2283 for (i
= 0; i
< argc
; i
++) {
2284 argv
[i
] = StringValue(argv
[i
]);
2285 fprintf(stderr
, "argv[%d] = %s\n", i
, StringValuePtr(argv
[i
]));
2287 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2288 //~ mode = 0666; /* default value */
2290 return INT2FIX(argc
);
2293 void Init_qemu(void)
2295 printf("%s\n", __func__
);
2296 VALUE cQEMU
= rb_define_class("QEMU", rb_cObject
);
2297 rb_define_singleton_method(cQEMU
, "run", qemu_open
, -1);
2302 #endif /* CONFIG_RUBY */
2304 #if defined(CONFIG_DLL)
2305 BOOL APIENTRY
DllMain(HINSTANCE hModule
, DWORD ul_reason_for_call
, LPVOID data
)
2309 #endif /* CONFIG_DLL */
2311 char *qemu_find_file(int type
, const char *name
)
2317 /* Try the name as a straight path first */
2318 if (access(name
, R_OK
) == 0) {
2319 trace_load_file(name
, name
);
2320 return g_strdup(name
);
2324 case QEMU_FILE_TYPE_BIOS
:
2327 case QEMU_FILE_TYPE_KEYMAP
:
2328 subdir
= "keymaps/";
2334 for (i
= 0; i
< data_dir_idx
; i
++) {
2335 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2336 if (access(buf
, R_OK
) == 0) {
2337 trace_load_file(name
, buf
);
2345 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2347 return qdev_device_help(opts
);
2350 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2354 dev
= qdev_device_add(opts
);
2357 object_unref(OBJECT(dev
));
2361 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2363 Error
*local_err
= NULL
;
2365 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2366 if (error_is_set(&local_err
)) {
2367 error_report("%s", error_get_pretty(local_err
));
2368 error_free(local_err
);
2374 #ifdef CONFIG_VIRTFS
2375 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2378 ret
= qemu_fsdev_add(opts
);
2384 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2386 CharDriverState
*chr
;
2387 const char *chardev
;
2391 mode
= qemu_opt_get(opts
, "mode");
2395 if (strcmp(mode
, "readline") == 0) {
2396 flags
= MONITOR_USE_READLINE
;
2397 } else if (strcmp(mode
, "control") == 0) {
2398 flags
= MONITOR_USE_CONTROL
;
2400 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2404 if (qemu_opt_get_bool(opts
, "pretty", 0))
2405 flags
|= MONITOR_USE_PRETTY
;
2407 if (qemu_opt_get_bool(opts
, "default", 0))
2408 flags
|= MONITOR_IS_DEFAULT
;
2410 chardev
= qemu_opt_get(opts
, "chardev");
2411 chr
= qemu_chr_find(chardev
);
2413 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2417 qemu_chr_fe_claim_no_fail(chr
);
2418 monitor_init(chr
, flags
);
2422 static void monitor_parse(const char *optarg
, const char *mode
)
2424 static int monitor_device_index
= 0;
2430 if (strstart(optarg
, "chardev:", &p
)) {
2431 snprintf(label
, sizeof(label
), "%s", p
);
2433 snprintf(label
, sizeof(label
), "compat_monitor%d",
2434 monitor_device_index
);
2435 if (monitor_device_index
== 0) {
2438 opts
= qemu_chr_parse_compat(label
, optarg
);
2440 fprintf(stderr
, "parse error: %s\n", optarg
);
2445 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2447 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2450 qemu_opt_set(opts
, "mode", mode
);
2451 qemu_opt_set(opts
, "chardev", label
);
2453 qemu_opt_set(opts
, "default", "on");
2454 monitor_device_index
++;
2457 struct device_config
{
2459 DEV_USB
, /* -usbdevice */
2461 DEV_SERIAL
, /* -serial */
2462 DEV_PARALLEL
, /* -parallel */
2463 DEV_VIRTCON
, /* -virtioconsole */
2464 DEV_DEBUGCON
, /* -debugcon */
2465 DEV_GDB
, /* -gdb, -s */
2466 DEV_SCLP
, /* s390 sclp */
2468 const char *cmdline
;
2470 QTAILQ_ENTRY(device_config
) next
;
2473 static QTAILQ_HEAD(, device_config
) device_configs
=
2474 QTAILQ_HEAD_INITIALIZER(device_configs
);
2476 static void add_device_config(int type
, const char *cmdline
)
2478 struct device_config
*conf
;
2480 conf
= g_malloc0(sizeof(*conf
));
2482 conf
->cmdline
= cmdline
;
2483 loc_save(&conf
->loc
);
2484 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2487 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2489 struct device_config
*conf
;
2492 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2493 if (conf
->type
!= type
)
2495 loc_push_restore(&conf
->loc
);
2496 rc
= func(conf
->cmdline
);
2497 loc_pop(&conf
->loc
);
2504 static int serial_parse(const char *devname
)
2506 static int index
= 0;
2509 if (strcmp(devname
, "none") == 0)
2511 if (index
== MAX_SERIAL_PORTS
) {
2512 fprintf(stderr
, "qemu: too many serial ports\n");
2515 snprintf(label
, sizeof(label
), "serial%d", index
);
2516 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2517 if (!serial_hds
[index
]) {
2518 fprintf(stderr
, "qemu: could not connect serial device"
2519 " to character backend '%s'\n", devname
);
2526 static int parallel_parse(const char *devname
)
2528 static int index
= 0;
2531 if (strcmp(devname
, "none") == 0)
2533 if (index
== MAX_PARALLEL_PORTS
) {
2534 fprintf(stderr
, "qemu: too many parallel ports\n");
2537 snprintf(label
, sizeof(label
), "parallel%d", index
);
2538 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2539 if (!parallel_hds
[index
]) {
2540 fprintf(stderr
, "qemu: could not connect parallel device"
2541 " to character backend '%s'\n", devname
);
2548 static int virtcon_parse(const char *devname
)
2550 QemuOptsList
*device
= qemu_find_opts("device");
2551 static int index
= 0;
2553 QemuOpts
*bus_opts
, *dev_opts
;
2555 if (strcmp(devname
, "none") == 0)
2557 if (index
== MAX_VIRTIO_CONSOLES
) {
2558 fprintf(stderr
, "qemu: too many virtio consoles\n");
2562 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2563 if (arch_type
== QEMU_ARCH_S390X
) {
2564 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2566 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2569 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2570 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2572 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2573 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2574 if (!virtcon_hds
[index
]) {
2575 fprintf(stderr
, "qemu: could not connect virtio console"
2576 " to character backend '%s'\n", devname
);
2579 qemu_opt_set(dev_opts
, "chardev", label
);
2585 static int sclp_parse(const char *devname
)
2587 QemuOptsList
*device
= qemu_find_opts("device");
2588 static int index
= 0;
2592 if (strcmp(devname
, "none") == 0) {
2595 if (index
== MAX_SCLP_CONSOLES
) {
2596 fprintf(stderr
, "qemu: too many sclp consoles\n");
2600 assert(arch_type
== QEMU_ARCH_S390X
);
2602 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2603 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2605 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2606 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2607 if (!sclp_hds
[index
]) {
2608 fprintf(stderr
, "qemu: could not connect sclp console"
2609 " to character backend '%s'\n", devname
);
2612 qemu_opt_set(dev_opts
, "chardev", label
);
2618 static int debugcon_parse(const char *devname
)
2622 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2625 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2627 fprintf(stderr
, "qemu: already have a debugcon device\n");
2630 qemu_opt_set(opts
, "driver", "isa-debugcon");
2631 qemu_opt_set(opts
, "chardev", "debugcon");
2635 static QEMUMachine
*machine_parse(const char *name
)
2637 QEMUMachine
*m
, *machine
= NULL
;
2640 machine
= find_machine(name
);
2645 printf("Supported machines are:\n");
2646 for (m
= first_machine
; m
!= NULL
; m
= m
->next
) {
2648 printf("%-20s %s (alias of %s)\n", m
->alias
, m
->desc
, m
->name
);
2650 printf("%-20s %s%s\n", m
->name
, m
->desc
,
2651 m
->is_default
? " (default)" : "");
2653 exit(!name
|| !is_help_option(name
));
2656 static int tcg_init(void)
2658 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2663 const char *opt_name
;
2665 int (*available
)(void);
2669 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2670 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2671 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2672 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2675 static int configure_accelerator(void)
2680 bool accel_initialised
= false;
2681 bool init_failed
= false;
2683 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2685 /* Use the default "accelerator", tcg */
2689 while (!accel_initialised
&& *p
!= '\0') {
2693 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2694 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2695 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2696 if (!accel_list
[i
].available()) {
2697 printf("%s not supported for this target\n",
2698 accel_list
[i
].name
);
2701 *(accel_list
[i
].allowed
) = true;
2702 ret
= accel_list
[i
].init();
2705 fprintf(stderr
, "failed to initialize %s: %s\n",
2708 *(accel_list
[i
].allowed
) = false;
2710 accel_initialised
= true;
2715 if (i
== ARRAY_SIZE(accel_list
)) {
2716 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2720 if (!accel_initialised
) {
2722 fprintf(stderr
, "No accelerator found!\n");
2728 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2731 return !accel_initialised
;
2734 void qemu_add_exit_notifier(Notifier
*notify
)
2736 notifier_list_add(&exit_notifiers
, notify
);
2739 void qemu_remove_exit_notifier(Notifier
*notify
)
2741 notifier_remove(notify
);
2744 static void qemu_run_exit_notifiers(void)
2746 notifier_list_notify(&exit_notifiers
, NULL
);
2749 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2751 notifier_list_add(&machine_init_done_notifiers
, notify
);
2754 static void qemu_run_machine_init_done_notifiers(void)
2756 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2759 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2760 const char **poptarg
, int *poptind
)
2762 const QEMUOption
*popt
;
2763 int optind
= *poptind
;
2764 char *r
= argv
[optind
];
2767 loc_set_cmdline(argv
, optind
, 1);
2769 /* Treat --foo the same as -foo. */
2772 popt
= qemu_options
;
2775 error_report("invalid option");
2778 if (!strcmp(popt
->name
, r
+ 1))
2782 if (popt
->flags
& HAS_ARG
) {
2783 if (optind
>= argc
) {
2784 error_report("requires an argument");
2787 optarg
= argv
[optind
++];
2788 loc_set_cmdline(argv
, optind
- 2, 2);
2799 static gpointer
malloc_and_trace(gsize n_bytes
)
2801 void *ptr
= malloc(n_bytes
);
2802 trace_g_malloc(n_bytes
, ptr
);
2806 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2808 void *ptr
= realloc(mem
, n_bytes
);
2809 trace_g_realloc(mem
, n_bytes
, ptr
);
2813 static void free_and_trace(gpointer mem
)
2819 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2821 Object
*obj
= OBJECT(opaque
);
2822 StringInputVisitor
*siv
;
2823 Error
*local_err
= NULL
;
2825 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0) {
2829 siv
= string_input_visitor_new(value
);
2830 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2831 string_input_visitor_cleanup(siv
);
2834 qerror_report_err(local_err
);
2835 error_free(local_err
);
2842 static int object_create(QemuOpts
*opts
, void *opaque
)
2844 const char *type
= qemu_opt_get(opts
, "qom-type");
2845 const char *id
= qemu_opts_id(opts
);
2848 g_assert(type
!= NULL
);
2851 qerror_report(QERR_MISSING_PARAMETER
, "id");
2855 obj
= object_new(type
);
2856 if (qemu_opt_foreach(opts
, object_set_property
, obj
, 1) < 0) {
2861 object_property_add_child(container_get(object_get_root(), "/objects"),
2867 int main(int argc
, char **argv
)
2870 int snapshot
, linux_boot
;
2871 const char *icount_option
= NULL
;
2872 const char *initrd_filename
;
2873 const char *kernel_filename
, *kernel_cmdline
;
2874 const char *boot_order
;
2876 int cyls
, heads
, secs
, translation
;
2877 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2878 QemuOptsList
*olist
;
2881 const char *loadvm
= NULL
;
2882 QEMUMachine
*machine
;
2883 const char *cpu_model
;
2884 const char *vga_model
= "none";
2885 const char *qtest_chrdev
= NULL
;
2886 const char *qtest_log
= NULL
;
2887 const char *pid_file
= NULL
;
2888 const char *incoming
= NULL
;
2890 int show_vnc_port
= 0;
2892 bool defconfig
= true;
2893 bool userconfig
= true;
2894 const char *log_mask
= NULL
;
2895 const char *log_file
= NULL
;
2896 GMemVTable mem_trace
= {
2897 .malloc
= malloc_and_trace
,
2898 .realloc
= realloc_and_trace
,
2899 .free
= free_and_trace
,
2901 const char *trace_events
= NULL
;
2902 const char *trace_file
= NULL
;
2904 atexit(qemu_run_exit_notifiers
);
2905 error_set_progname(argv
[0]);
2907 g_mem_set_vtable(&mem_trace
);
2908 if (!g_thread_supported()) {
2909 #if !GLIB_CHECK_VERSION(2, 31, 0)
2910 g_thread_init(NULL
);
2912 fprintf(stderr
, "glib threading failed to initialize.\n");
2917 module_call_init(MODULE_INIT_QOM
);
2919 qemu_add_opts(&qemu_drive_opts
);
2920 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2921 qemu_add_drive_opts(&qemu_common_drive_opts
);
2922 qemu_add_drive_opts(&qemu_drive_opts
);
2923 qemu_add_opts(&qemu_chardev_opts
);
2924 qemu_add_opts(&qemu_device_opts
);
2925 qemu_add_opts(&qemu_netdev_opts
);
2926 qemu_add_opts(&qemu_net_opts
);
2927 qemu_add_opts(&qemu_rtc_opts
);
2928 qemu_add_opts(&qemu_global_opts
);
2929 qemu_add_opts(&qemu_mon_opts
);
2930 qemu_add_opts(&qemu_trace_opts
);
2931 qemu_add_opts(&qemu_option_rom_opts
);
2932 qemu_add_opts(&qemu_machine_opts
);
2933 qemu_add_opts(&qemu_smp_opts
);
2934 qemu_add_opts(&qemu_boot_opts
);
2935 qemu_add_opts(&qemu_sandbox_opts
);
2936 qemu_add_opts(&qemu_add_fd_opts
);
2937 qemu_add_opts(&qemu_object_opts
);
2938 qemu_add_opts(&qemu_tpmdev_opts
);
2939 qemu_add_opts(&qemu_realtime_opts
);
2940 qemu_add_opts(&qemu_msg_opts
);
2945 rtc_clock
= QEMU_CLOCK_HOST
;
2947 qemu_cache_utils_init();
2949 QLIST_INIT (&vm_change_state_head
);
2950 os_setup_early_signal_handling();
2952 module_call_init(MODULE_INIT_MACHINE
);
2954 optarg
= strrchr(argv
[0], '/');
2956 machine
= find_machine(optarg
+ 1);
2959 machine
= find_default_machine();
2964 cyls
= heads
= secs
= 0;
2965 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2967 for (i
= 0; i
< MAX_NODES
; i
++) {
2969 node_cpumask
[i
] = bitmap_new(MAX_CPUMASK_BITS
);
2975 bdrv_init_with_whitelist();
2979 /* first pass of option parsing */
2981 while (optind
< argc
) {
2982 if (argv
[optind
][0] != '-') {
2987 const QEMUOption
*popt
;
2989 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2990 switch (popt
->index
) {
2991 case QEMU_OPTION_nodefconfig
:
2994 case QEMU_OPTION_nouserconfig
:
3003 ret
= qemu_read_default_config_files(userconfig
);
3009 /* second pass of option parsing */
3014 if (argv
[optind
][0] != '-') {
3015 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3017 const QEMUOption
*popt
;
3019 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3020 if (!(popt
->arch_mask
& arch_type
)) {
3021 printf("Option %s not supported for this target\n", popt
->name
);
3024 switch(popt
->index
) {
3026 machine
= machine_parse(optarg
);
3028 case QEMU_OPTION_no_kvm_irqchip
: {
3029 olist
= qemu_find_opts("machine");
3030 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3033 case QEMU_OPTION_cpu
:
3034 /* hw initialization will check this */
3037 case QEMU_OPTION_hda
:
3041 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3043 snprintf(buf
, sizeof(buf
),
3044 "%s,cyls=%d,heads=%d,secs=%d%s",
3045 HD_OPTS
, cyls
, heads
, secs
,
3046 translation
== BIOS_ATA_TRANSLATION_LBA
?
3048 translation
== BIOS_ATA_TRANSLATION_NONE
?
3049 ",trans=none" : "");
3050 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3053 case QEMU_OPTION_hdb
:
3054 case QEMU_OPTION_hdc
:
3055 case QEMU_OPTION_hdd
:
3056 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3059 case QEMU_OPTION_drive
:
3060 if (drive_def(optarg
) == NULL
) {
3064 case QEMU_OPTION_set
:
3065 if (qemu_set_option(optarg
) != 0)
3068 case QEMU_OPTION_global
:
3069 if (qemu_global_option(optarg
) != 0)
3072 case QEMU_OPTION_mtdblock
:
3073 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3075 case QEMU_OPTION_sd
:
3076 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3078 case QEMU_OPTION_pflash
:
3079 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3081 case QEMU_OPTION_snapshot
:
3084 case QEMU_OPTION_hdachs
:
3088 cyls
= strtol(p
, (char **)&p
, 0);
3089 if (cyls
< 1 || cyls
> 16383)
3094 heads
= strtol(p
, (char **)&p
, 0);
3095 if (heads
< 1 || heads
> 16)
3100 secs
= strtol(p
, (char **)&p
, 0);
3101 if (secs
< 1 || secs
> 63)
3105 if (!strcmp(p
, "none"))
3106 translation
= BIOS_ATA_TRANSLATION_NONE
;
3107 else if (!strcmp(p
, "lba"))
3108 translation
= BIOS_ATA_TRANSLATION_LBA
;
3109 else if (!strcmp(p
, "auto"))
3110 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3113 } else if (*p
!= '\0') {
3115 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3118 if (hda_opts
!= NULL
) {
3120 snprintf(num
, sizeof(num
), "%d", cyls
);
3121 qemu_opt_set(hda_opts
, "cyls", num
);
3122 snprintf(num
, sizeof(num
), "%d", heads
);
3123 qemu_opt_set(hda_opts
, "heads", num
);
3124 snprintf(num
, sizeof(num
), "%d", secs
);
3125 qemu_opt_set(hda_opts
, "secs", num
);
3126 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
3127 qemu_opt_set(hda_opts
, "trans", "lba");
3128 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
3129 qemu_opt_set(hda_opts
, "trans", "none");
3133 case QEMU_OPTION_numa
:
3136 case QEMU_OPTION_display
:
3137 display_type
= select_display(optarg
);
3139 case QEMU_OPTION_nographic
:
3140 display_type
= DT_NOGRAPHIC
;
3142 case QEMU_OPTION_curses
:
3143 #ifdef CONFIG_CURSES
3144 display_type
= DT_CURSES
;
3146 fprintf(stderr
, "Curses support is disabled\n");
3150 case QEMU_OPTION_portrait
:
3151 graphic_rotate
= 90;
3153 case QEMU_OPTION_rotate
:
3154 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3155 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3156 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3158 "qemu: only 90, 180, 270 deg rotation is available\n");
3162 case QEMU_OPTION_kernel
:
3163 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3165 case QEMU_OPTION_initrd
:
3166 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3168 case QEMU_OPTION_append
:
3169 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3171 case QEMU_OPTION_dtb
:
3172 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3174 case QEMU_OPTION_cdrom
:
3175 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3177 case QEMU_OPTION_boot
:
3178 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3183 case QEMU_OPTION_fda
:
3184 case QEMU_OPTION_fdb
:
3185 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3188 case QEMU_OPTION_no_fd_bootchk
:
3191 case QEMU_OPTION_netdev
:
3192 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3196 case QEMU_OPTION_net
:
3197 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3201 #ifdef CONFIG_LIBISCSI
3202 case QEMU_OPTION_iscsi
:
3203 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3210 case QEMU_OPTION_tftp
:
3211 legacy_tftp_prefix
= optarg
;
3213 case QEMU_OPTION_bootp
:
3214 legacy_bootp_filename
= optarg
;
3216 case QEMU_OPTION_redir
:
3217 if (net_slirp_redir(optarg
) < 0)
3221 case QEMU_OPTION_bt
:
3222 add_device_config(DEV_BT
, optarg
);
3224 case QEMU_OPTION_audio_help
:
3228 case QEMU_OPTION_soundhw
:
3229 select_soundhw (optarg
);
3234 case QEMU_OPTION_version
:
3238 case QEMU_OPTION_m
: {
3243 value
= strtosz(optarg
, &end
);
3244 if (value
< 0 || *end
) {
3245 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
3248 sz
= QEMU_ALIGN_UP((uint64_t)value
, 8192);
3250 if (ram_size
!= sz
) {
3251 fprintf(stderr
, "qemu: ram size too large\n");
3257 case QEMU_OPTION_tpmdev
:
3258 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3263 case QEMU_OPTION_mempath
:
3266 case QEMU_OPTION_mem_prealloc
:
3276 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3278 case QEMU_OPTION_gdb
:
3279 add_device_config(DEV_GDB
, optarg
);
3282 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3283 data_dir
[data_dir_idx
++] = optarg
;
3286 case QEMU_OPTION_bios
:
3287 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3289 case QEMU_OPTION_singlestep
:
3296 keyboard_layout
= optarg
;
3298 case QEMU_OPTION_localtime
:
3301 case QEMU_OPTION_vga
:
3310 w
= strtol(p
, (char **)&p
, 10);
3313 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3319 h
= strtol(p
, (char **)&p
, 10);
3324 depth
= strtol(p
, (char **)&p
, 10);
3325 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3326 depth
!= 24 && depth
!= 32)
3328 } else if (*p
== '\0') {
3329 depth
= graphic_depth
;
3336 graphic_depth
= depth
;
3339 case QEMU_OPTION_echr
:
3342 term_escape_char
= strtol(optarg
, &r
, 0);
3344 printf("Bad argument to echr\n");
3347 case QEMU_OPTION_monitor
:
3348 default_monitor
= 0;
3349 if (strncmp(optarg
, "none", 4)) {
3350 monitor_parse(optarg
, "readline");
3353 case QEMU_OPTION_qmp
:
3354 monitor_parse(optarg
, "control");
3355 default_monitor
= 0;
3357 case QEMU_OPTION_mon
:
3358 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3362 default_monitor
= 0;
3364 case QEMU_OPTION_chardev
:
3365 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3370 case QEMU_OPTION_fsdev
:
3371 olist
= qemu_find_opts("fsdev");
3373 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3376 opts
= qemu_opts_parse(olist
, optarg
, 1);
3381 case QEMU_OPTION_virtfs
: {
3384 const char *writeout
, *sock_fd
, *socket
;
3386 olist
= qemu_find_opts("virtfs");
3388 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3391 opts
= qemu_opts_parse(olist
, optarg
, 1);
3396 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3397 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3398 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3401 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3402 qemu_opt_get(opts
, "mount_tag"),
3405 fprintf(stderr
, "duplicate fsdev id: %s\n",
3406 qemu_opt_get(opts
, "mount_tag"));
3410 writeout
= qemu_opt_get(opts
, "writeout");
3412 #ifdef CONFIG_SYNC_FILE_RANGE
3413 qemu_opt_set(fsdev
, "writeout", writeout
);
3415 fprintf(stderr
, "writeout=immediate not supported on "
3420 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3421 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3422 qemu_opt_set(fsdev
, "security_model",
3423 qemu_opt_get(opts
, "security_model"));
3424 socket
= qemu_opt_get(opts
, "socket");
3426 qemu_opt_set(fsdev
, "socket", socket
);
3428 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3430 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3433 qemu_opt_set_bool(fsdev
, "readonly",
3434 qemu_opt_get_bool(opts
, "readonly", 0));
3435 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3437 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3438 qemu_opt_set(device
, "fsdev",
3439 qemu_opt_get(opts
, "mount_tag"));
3440 qemu_opt_set(device
, "mount_tag",
3441 qemu_opt_get(opts
, "mount_tag"));
3444 case QEMU_OPTION_virtfs_synth
: {
3448 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3451 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3454 qemu_opt_set(fsdev
, "fsdriver", "synth");
3456 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3458 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3459 qemu_opt_set(device
, "fsdev", "v_synth");
3460 qemu_opt_set(device
, "mount_tag", "v_synth");
3463 case QEMU_OPTION_serial
:
3464 add_device_config(DEV_SERIAL
, optarg
);
3466 if (strncmp(optarg
, "mon:", 4) == 0) {
3467 default_monitor
= 0;
3470 case QEMU_OPTION_watchdog
:
3473 "qemu: only one watchdog option may be given\n");
3478 case QEMU_OPTION_watchdog_action
:
3479 if (select_watchdog_action(optarg
) == -1) {
3480 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3484 case QEMU_OPTION_virtiocon
:
3485 add_device_config(DEV_VIRTCON
, optarg
);
3486 default_virtcon
= 0;
3487 if (strncmp(optarg
, "mon:", 4) == 0) {
3488 default_monitor
= 0;
3491 case QEMU_OPTION_parallel
:
3492 add_device_config(DEV_PARALLEL
, optarg
);
3493 default_parallel
= 0;
3494 if (strncmp(optarg
, "mon:", 4) == 0) {
3495 default_monitor
= 0;
3498 case QEMU_OPTION_debugcon
:
3499 add_device_config(DEV_DEBUGCON
, optarg
);
3501 case QEMU_OPTION_loadvm
:
3504 case QEMU_OPTION_full_screen
:
3507 case QEMU_OPTION_no_frame
:
3510 case QEMU_OPTION_alt_grab
:
3513 case QEMU_OPTION_ctrl_grab
:
3516 case QEMU_OPTION_no_quit
:
3519 case QEMU_OPTION_sdl
:
3521 display_type
= DT_SDL
;
3524 fprintf(stderr
, "SDL support is disabled\n");
3527 case QEMU_OPTION_pidfile
:
3530 case QEMU_OPTION_win2k_hack
:
3531 win2k_install_hack
= 1;
3533 case QEMU_OPTION_rtc_td_hack
: {
3534 static GlobalProperty slew_lost_ticks
[] = {
3536 .driver
= "mc146818rtc",
3537 .property
= "lost_tick_policy",
3540 { /* end of list */ }
3543 qdev_prop_register_global_list(slew_lost_ticks
);
3546 case QEMU_OPTION_acpitable
:
3547 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3551 do_acpitable_option(opts
);
3553 case QEMU_OPTION_smbios
:
3554 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3558 do_smbios_option(opts
);
3560 case QEMU_OPTION_enable_kvm
:
3561 olist
= qemu_find_opts("machine");
3562 qemu_opts_parse(olist
, "accel=kvm", 0);
3564 case QEMU_OPTION_machine
:
3565 olist
= qemu_find_opts("machine");
3566 opts
= qemu_opts_parse(olist
, optarg
, 1);
3570 optarg
= qemu_opt_get(opts
, "type");
3572 machine
= machine_parse(optarg
);
3575 case QEMU_OPTION_no_kvm
:
3576 olist
= qemu_find_opts("machine");
3577 qemu_opts_parse(olist
, "accel=tcg", 0);
3579 case QEMU_OPTION_no_kvm_pit
: {
3580 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3584 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3585 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3587 .driver
= "kvm-pit",
3588 .property
= "lost_tick_policy",
3591 { /* end of list */ }
3594 fprintf(stderr
, "Warning: option deprecated, use "
3595 "lost_tick_policy property of kvm-pit instead.\n");
3596 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3599 case QEMU_OPTION_usb
:
3600 olist
= qemu_find_opts("machine");
3601 qemu_opts_parse(olist
, "usb=on", 0);
3603 case QEMU_OPTION_usbdevice
:
3604 olist
= qemu_find_opts("machine");
3605 qemu_opts_parse(olist
, "usb=on", 0);
3606 add_device_config(DEV_USB
, optarg
);
3608 case QEMU_OPTION_device
:
3609 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3613 case QEMU_OPTION_smp
:
3614 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3618 case QEMU_OPTION_vnc
:
3621 vnc_display
= optarg
;
3623 fprintf(stderr
, "VNC support is disabled\n");
3627 case QEMU_OPTION_no_acpi
:
3630 case QEMU_OPTION_no_hpet
:
3633 case QEMU_OPTION_balloon
:
3634 if (balloon_parse(optarg
) < 0) {
3635 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3639 case QEMU_OPTION_no_reboot
:
3642 case QEMU_OPTION_no_shutdown
:
3645 case QEMU_OPTION_show_cursor
:
3648 case QEMU_OPTION_uuid
:
3649 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3650 fprintf(stderr
, "Fail to parse UUID string."
3651 " Wrong format.\n");
3654 qemu_uuid_set
= true;
3656 case QEMU_OPTION_option_rom
:
3657 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3658 fprintf(stderr
, "Too many option ROMs\n");
3661 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3665 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3666 option_rom
[nb_option_roms
].bootindex
=
3667 qemu_opt_get_number(opts
, "bootindex", -1);
3668 if (!option_rom
[nb_option_roms
].name
) {
3669 fprintf(stderr
, "Option ROM file is not specified\n");
3674 case QEMU_OPTION_semihosting
:
3675 semihosting_enabled
= 1;
3677 case QEMU_OPTION_tdf
:
3678 fprintf(stderr
, "Warning: user space PIT time drift fix "
3679 "is no longer supported.\n");
3681 case QEMU_OPTION_name
:
3682 qemu_name
= g_strdup(optarg
);
3684 char *p
= strchr(qemu_name
, ',');
3687 if (strncmp(p
, "process=", 8)) {
3688 fprintf(stderr
, "Unknown subargument %s to -name\n", p
);
3692 os_set_proc_name(p
);
3696 case QEMU_OPTION_prom_env
:
3697 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3698 fprintf(stderr
, "Too many prom variables\n");
3701 prom_envs
[nb_prom_envs
] = optarg
;
3704 case QEMU_OPTION_old_param
:
3707 case QEMU_OPTION_clock
:
3708 /* Clock options no longer exist. Keep this option for
3709 * backward compatibility.
3712 case QEMU_OPTION_startdate
:
3713 configure_rtc_date_offset(optarg
, 1);
3715 case QEMU_OPTION_rtc
:
3716 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3720 configure_rtc(opts
);
3722 case QEMU_OPTION_tb_size
:
3723 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3724 if (tcg_tb_size
< 0) {
3728 case QEMU_OPTION_icount
:
3729 icount_option
= optarg
;
3731 case QEMU_OPTION_incoming
:
3733 runstate_set(RUN_STATE_INMIGRATE
);
3735 case QEMU_OPTION_nodefaults
:
3737 default_parallel
= 0;
3738 default_virtcon
= 0;
3740 default_monitor
= 0;
3747 case QEMU_OPTION_xen_domid
:
3748 if (!(xen_available())) {
3749 printf("Option %s not supported for this target\n", popt
->name
);
3752 xen_domid
= atoi(optarg
);
3754 case QEMU_OPTION_xen_create
:
3755 if (!(xen_available())) {
3756 printf("Option %s not supported for this target\n", popt
->name
);
3759 xen_mode
= XEN_CREATE
;
3761 case QEMU_OPTION_xen_attach
:
3762 if (!(xen_available())) {
3763 printf("Option %s not supported for this target\n", popt
->name
);
3766 xen_mode
= XEN_ATTACH
;
3768 case QEMU_OPTION_trace
:
3770 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3774 trace_events
= qemu_opt_get(opts
, "events");
3775 trace_file
= qemu_opt_get(opts
, "file");
3778 case QEMU_OPTION_trace_unassigned
:
3779 trace_unassigned
= true;
3781 case QEMU_OPTION_readconfig
:
3783 int ret
= qemu_read_config_file(optarg
);
3785 fprintf(stderr
, "read config %s: %s\n", optarg
,
3791 case QEMU_OPTION_spice
:
3792 olist
= qemu_find_opts("spice");
3794 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3797 opts
= qemu_opts_parse(olist
, optarg
, 0);
3803 case QEMU_OPTION_writeconfig
:
3806 if (strcmp(optarg
, "-") == 0) {
3809 fp
= fopen(optarg
, "w");
3811 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3815 qemu_config_write(fp
);
3819 case QEMU_OPTION_qtest
:
3820 qtest_chrdev
= optarg
;
3822 case QEMU_OPTION_qtest_log
:
3825 case QEMU_OPTION_sandbox
:
3826 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3831 case QEMU_OPTION_add_fd
:
3833 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3838 error_report("File descriptor passing is disabled on this "
3843 case QEMU_OPTION_object
:
3844 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3849 case QEMU_OPTION_realtime
:
3850 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3854 configure_realtime(opts
);
3856 case QEMU_OPTION_msg
:
3857 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3861 configure_msg(opts
);
3864 os_parse_cmd_args(popt
->index
, optarg
);
3870 if (qemu_init_main_loop()) {
3871 fprintf(stderr
, "qemu_init_main_loop failed\n");
3875 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3880 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3884 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3889 if (machine
== NULL
) {
3890 fprintf(stderr
, "No machine found.\n");
3894 if (machine
->hw_version
) {
3895 qemu_set_version(machine
->hw_version
);
3898 if (qemu_opts_foreach(qemu_find_opts("object"),
3899 object_create
, NULL
, 0) != 0) {
3903 /* Init CPU def lists, based on config
3904 * - Must be called after all the qemu_read_config_file() calls
3905 * - Must be called before list_cpus()
3906 * - Must be called before machine->init()
3910 if (cpu_model
&& is_help_option(cpu_model
)) {
3911 list_cpus(stdout
, &fprintf
, cpu_model
);
3915 /* Open the logfile at this point, if necessary. We can't open the logfile
3916 * when encountering either of the logging options (-d or -D) because the
3917 * other one may be encountered later on the command line, changing the
3918 * location or level of logging.
3923 qemu_set_log_filename(log_file
);
3926 mask
= qemu_str_to_log_mask(log_mask
);
3928 qemu_print_log_usage(stdout
);
3934 if (!trace_backend_init(trace_events
, trace_file
)) {
3938 /* If no data_dir is specified then try to find it relative to the
3940 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3941 data_dir
[data_dir_idx
] = os_find_datadir(argv
[0]);
3942 if (data_dir
[data_dir_idx
] != NULL
) {
3946 /* If all else fails use the install path specified when building. */
3947 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3948 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3951 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3953 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
3954 if (smp_cpus
> machine
->max_cpus
) {
3955 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3956 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
3962 * Get the default machine options from the machine if it is not already
3963 * specified either by the configuration file or by the command line.
3965 if (machine
->default_machine_opts
) {
3966 qemu_opts_set_defaults(qemu_find_opts("machine"),
3967 machine
->default_machine_opts
, 0);
3970 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3971 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3973 if (machine
->no_serial
) {
3976 if (machine
->no_parallel
) {
3977 default_parallel
= 0;
3979 if (!machine
->use_virtcon
) {
3980 default_virtcon
= 0;
3982 if (!machine
->use_sclp
) {
3985 if (machine
->no_floppy
) {
3988 if (machine
->no_cdrom
) {
3991 if (machine
->no_sdcard
) {
3995 if (is_daemonized()) {
3996 /* According to documentation and historically, -nographic redirects
3997 * serial port, parallel port and monitor to stdio, which does not work
3998 * with -daemonize. We can redirect these to null instead, but since
3999 * -nographic is legacy, let's just error out.
4000 * We disallow -nographic only if all other ports are not redirected
4001 * explicitly, to not break existing legacy setups which uses
4002 * -nographic _and_ redirects all ports explicitly - this is valid
4003 * usage, -nographic is just a no-op in this case.
4005 if (display_type
== DT_NOGRAPHIC
4006 && (default_parallel
|| default_serial
4007 || default_monitor
|| default_virtcon
)) {
4008 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4011 #ifdef CONFIG_CURSES
4012 if (display_type
== DT_CURSES
) {
4013 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4019 if (display_type
== DT_NOGRAPHIC
) {
4020 if (default_parallel
)
4021 add_device_config(DEV_PARALLEL
, "null");
4022 if (default_serial
&& default_monitor
) {
4023 add_device_config(DEV_SERIAL
, "mon:stdio");
4024 } else if (default_virtcon
&& default_monitor
) {
4025 add_device_config(DEV_VIRTCON
, "mon:stdio");
4026 } else if (default_sclp
&& default_monitor
) {
4027 add_device_config(DEV_SCLP
, "mon:stdio");
4030 add_device_config(DEV_SERIAL
, "stdio");
4031 if (default_virtcon
)
4032 add_device_config(DEV_VIRTCON
, "stdio");
4034 add_device_config(DEV_SCLP
, "stdio");
4036 if (default_monitor
)
4037 monitor_parse("stdio", "readline");
4041 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4042 if (default_parallel
)
4043 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4044 if (default_monitor
)
4045 monitor_parse("vc:80Cx24C", "readline");
4046 if (default_virtcon
)
4047 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4049 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4053 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4054 #if defined(CONFIG_GTK)
4055 display_type
= DT_GTK
;
4056 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4057 display_type
= DT_SDL
;
4058 #elif defined(CONFIG_VNC)
4059 vnc_display
= "localhost:0,to=99";
4062 display_type
= DT_NONE
;
4066 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4067 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4068 "for SDL, ignoring option\n");
4070 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4071 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4072 "ignoring option\n");
4075 #if defined(CONFIG_GTK)
4076 if (display_type
== DT_GTK
) {
4077 early_gtk_display_init();
4083 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4085 #ifdef CONFIG_VIRTFS
4086 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4093 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4098 /* init the memory */
4099 if (ram_size
== 0) {
4100 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
4103 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4108 configure_accelerator();
4111 qtest_init(qtest_chrdev
, qtest_log
);
4114 machine_opts
= qemu_get_machine_opts();
4115 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4116 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4117 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4118 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4120 boot_order
= machine
->default_boot_order
;
4121 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4123 char *normal_boot_order
;
4124 const char *order
, *once
;
4126 order
= qemu_opt_get(opts
, "order");
4128 validate_bootdevices(order
);
4132 once
= qemu_opt_get(opts
, "once");
4134 validate_bootdevices(once
);
4135 normal_boot_order
= g_strdup(boot_order
);
4137 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4140 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4141 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4144 if (!kernel_cmdline
) {
4145 kernel_cmdline
= "";
4148 linux_boot
= (kernel_filename
!= NULL
);
4150 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4151 fprintf(stderr
, "-append only allowed with -kernel option\n");
4155 if (!linux_boot
&& initrd_filename
!= NULL
) {
4156 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4160 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4161 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4165 os_set_line_buffering();
4167 qemu_init_cpu_loop();
4168 qemu_mutex_lock_iothread();
4171 /* spice needs the timers to be initialized by this point */
4175 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4176 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4179 configure_icount(icount_option
);
4181 /* clean up network at qemu process termination */
4182 atexit(&net_cleanup
);
4184 if (net_init_clients() < 0) {
4189 if (tpm_init() < 0) {
4194 /* init the bluetooth world */
4195 if (foreach_device_config(DEV_BT
, bt_parse
))
4198 if (!xen_enabled()) {
4199 /* On 32-bit hosts, QEMU is limited by virtual address space */
4200 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4201 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4206 cpu_exec_init_all();
4210 /* open the virtual block devices */
4212 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4213 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4214 &machine
->block_default_type
, 1) != 0) {
4218 default_drive(default_cdrom
, snapshot
, machine
->block_default_type
, 2,
4220 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4221 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4223 register_savevm_live(NULL
, "ram", 0, 4, &savevm_ram_handlers
, NULL
);
4225 if (nb_numa_nodes
> 0) {
4228 if (nb_numa_nodes
> MAX_NODES
) {
4229 nb_numa_nodes
= MAX_NODES
;
4232 /* If no memory size if given for any node, assume the default case
4233 * and distribute the available memory equally across all nodes
4235 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4236 if (node_mem
[i
] != 0)
4239 if (i
== nb_numa_nodes
) {
4240 uint64_t usedmem
= 0;
4242 /* On Linux, the each node's border has to be 8MB aligned,
4243 * the final node gets the rest.
4245 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
4246 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
4247 usedmem
+= node_mem
[i
];
4249 node_mem
[i
] = ram_size
- usedmem
;
4252 for (i
= 0; i
< nb_numa_nodes
; i
++) {
4253 if (!bitmap_empty(node_cpumask
[i
], MAX_CPUMASK_BITS
)) {
4257 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4258 * must cope with this anyway, because there are BIOSes out there in
4259 * real machines which also use this scheme.
4261 if (i
== nb_numa_nodes
) {
4262 for (i
= 0; i
< max_cpus
; i
++) {
4263 set_bit(i
, node_cpumask
[i
% nb_numa_nodes
]);
4268 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4272 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4274 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4276 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4278 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4281 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4284 /* If no default VGA is requested, the default is "none". */
4286 if (cirrus_vga_available()) {
4287 vga_model
= "cirrus";
4288 } else if (vga_available()) {
4292 select_vgahw(vga_model
);
4295 i
= select_watchdog(watchdog
);
4297 exit (i
== 1 ? 1 : 0);
4300 if (machine
->compat_props
) {
4301 qdev_prop_register_global_list(machine
->compat_props
);
4305 qdev_machine_init();
4307 QEMUMachineInitArgs args
= { .machine
= machine
,
4308 .ram_size
= ram_size
,
4309 .boot_order
= boot_order
,
4310 .kernel_filename
= kernel_filename
,
4311 .kernel_cmdline
= kernel_cmdline
,
4312 .initrd_filename
= initrd_filename
,
4313 .cpu_model
= cpu_model
};
4314 machine
->init(&args
);
4318 cpu_synchronize_all_post_init();
4322 current_machine
= machine
;
4324 /* init USB devices */
4325 if (usb_enabled(false)) {
4326 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4330 /* init generic devices */
4331 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4334 net_check_clients();
4336 ds
= init_displaystate();
4338 /* init local displays */
4339 switch (display_type
) {
4341 (void)ds
; /* avoid warning if no display is configured */
4343 #if defined(CONFIG_CURSES)
4345 curses_display_init(ds
, full_screen
);
4348 #if defined(CONFIG_SDL)
4350 sdl_display_init(ds
, full_screen
, no_frame
);
4352 #elif defined(CONFIG_COCOA)
4354 cocoa_display_init(ds
, full_screen
);
4357 #if defined(CONFIG_GTK)
4359 gtk_display_init(ds
, full_screen
);
4366 /* must be after terminal init, SDL library changes signal handlers */
4367 os_setup_signal_handling();
4370 /* init remote displays */
4372 Error
*local_err
= NULL
;
4373 vnc_display_init(ds
);
4374 vnc_display_open(ds
, vnc_display
, &local_err
);
4375 if (local_err
!= NULL
) {
4376 error_report("Failed to start VNC server on `%s': %s",
4377 vnc_display
, error_get_pretty(local_err
));
4378 error_free(local_err
);
4382 if (show_vnc_port
) {
4383 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4389 qemu_spice_display_init();
4393 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4397 qdev_machine_creation_done();
4399 if (rom_load_all() != 0) {
4400 fprintf(stderr
, "rom loading failed\n");
4404 /* TODO: once all bus devices are qdevified, this should be done
4405 * when bus is created by qdev.c */
4406 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4407 qemu_run_machine_init_done_notifiers();
4409 /* Done notifiers can load ROMs */
4412 qemu_system_reset(VMRESET_SILENT
);
4414 if (load_vmstate(loadvm
) < 0) {
4420 Error
*local_err
= NULL
;
4421 qemu_start_incoming_migration(incoming
, &local_err
);
4423 error_report("-incoming %s: %s", incoming
,
4424 error_get_pretty(local_err
));
4425 error_free(local_err
);
4428 } else if (autostart
) {