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 "config-host.h"
34 #include "sysemu/seccomp.h"
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
42 #if defined(__APPLE__) || defined(main)
44 int qemu_main(int argc
, char **argv
);
45 int main(int argc
, char **argv
)
47 return qemu_main(argc
, argv
);
50 #define main qemu_main
52 #endif /* CONFIG_SDL */
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
61 #include "qemu/sockets.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
100 #include "fsdev/qemu-fsdev.h"
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir
[16];
128 static int data_dir_idx
;
129 const char *bios_name
= NULL
;
130 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
131 DisplayType display_type
= DT_DEFAULT
;
132 static int display_remote
;
133 const char* keyboard_layout
= NULL
;
135 const char *mem_path
= NULL
;
136 int mem_prealloc
= 0; /* force preallocation of physical target memory */
137 bool enable_mlock
= false;
139 NICInfo nd_table
[MAX_NICS
];
141 static int rtc_utc
= 1;
142 static int rtc_date_offset
= -1; /* -1 means no change */
143 QEMUClockType rtc_clock
;
144 int vga_interface_type
= VGA_NONE
;
145 static int full_screen
= 0;
146 static int no_frame
= 0;
149 static bool grab_on_hover
;
151 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
152 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
153 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
154 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
155 int win2k_install_hack
= 0;
162 const char *vnc_display
;
164 int acpi_enabled
= 1;
167 static int no_reboot
;
170 int graphic_rotate
= 0;
171 const char *watchdog
;
172 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
174 int semihosting_enabled
= 0;
176 const char *qemu_name
;
179 unsigned int nb_prom_envs
= 0;
180 const char *prom_envs
[MAX_PROM_ENVS
];
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 int icount_align_option
;
191 NodeInfo numa_info
[MAX_NODES
];
193 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 uint8_t qemu_uuid
[16];
199 /* Trace unassigned memory or i/o accesses. */
200 bool trace_unassigned
;
202 static QEMUBootSetHandler
*boot_set_handler
;
203 static void *boot_set_opaque
;
205 static NotifierList exit_notifiers
=
206 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
208 static NotifierList machine_init_done_notifiers
=
209 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
213 enum xen_mode xen_mode
= XEN_EMULATE
;
215 static int has_defaults
= 1;
216 static int default_serial
= 1;
217 static int default_parallel
= 1;
218 static int default_virtcon
= 1;
219 static int default_sclp
= 1;
220 static int default_monitor
= 1;
221 static int default_floppy
= 1;
222 static int default_cdrom
= 1;
223 static int default_sdcard
= 1;
224 static int default_vga
= 1;
230 { .driver
= "isa-serial", .flag
= &default_serial
},
231 { .driver
= "isa-parallel", .flag
= &default_parallel
},
232 { .driver
= "isa-fdc", .flag
= &default_floppy
},
233 { .driver
= "ide-cd", .flag
= &default_cdrom
},
234 { .driver
= "ide-hd", .flag
= &default_cdrom
},
235 { .driver
= "ide-drive", .flag
= &default_cdrom
},
236 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
237 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
238 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
239 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
240 { .driver
= "VGA", .flag
= &default_vga
},
241 { .driver
= "isa-vga", .flag
= &default_vga
},
242 { .driver
= "cirrus-vga", .flag
= &default_vga
},
243 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
244 { .driver
= "vmware-svga", .flag
= &default_vga
},
245 { .driver
= "qxl-vga", .flag
= &default_vga
},
248 static QemuOptsList qemu_rtc_opts
= {
250 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
254 .type
= QEMU_OPT_STRING
,
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_STRING
,
262 { /* end of list */ }
266 static QemuOptsList qemu_sandbox_opts
= {
268 .implied_opt_name
= "enable",
269 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
273 .type
= QEMU_OPT_BOOL
,
275 { /* end of list */ }
279 static QemuOptsList qemu_trace_opts
= {
281 .implied_opt_name
= "trace",
282 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
286 .type
= QEMU_OPT_STRING
,
289 .type
= QEMU_OPT_STRING
,
291 { /* end of list */ }
295 static QemuOptsList qemu_option_rom_opts
= {
296 .name
= "option-rom",
297 .implied_opt_name
= "romfile",
298 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
302 .type
= QEMU_OPT_NUMBER
,
305 .type
= QEMU_OPT_STRING
,
307 { /* end of list */ }
311 static QemuOptsList qemu_machine_opts
= {
313 .implied_opt_name
= "type",
315 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
319 .type
= QEMU_OPT_STRING
,
320 .help
= "emulated machine"
323 .type
= QEMU_OPT_STRING
,
324 .help
= "accelerator list",
326 .name
= "kernel_irqchip",
327 .type
= QEMU_OPT_BOOL
,
328 .help
= "use KVM in-kernel irqchip",
330 .name
= "kvm_shadow_mem",
331 .type
= QEMU_OPT_SIZE
,
332 .help
= "KVM shadow MMU size",
335 .type
= QEMU_OPT_STRING
,
336 .help
= "Linux kernel image file",
339 .type
= QEMU_OPT_STRING
,
340 .help
= "Linux initial ramdisk file",
343 .type
= QEMU_OPT_STRING
,
344 .help
= "Linux kernel command line",
347 .type
= QEMU_OPT_STRING
,
348 .help
= "Linux kernel device tree file",
351 .type
= QEMU_OPT_STRING
,
352 .help
= "Dump current dtb to a file and quit",
354 .name
= "phandle_start",
355 .type
= QEMU_OPT_NUMBER
,
356 .help
= "The first phandle ID we may generate dynamically",
358 .name
= "dt_compatible",
359 .type
= QEMU_OPT_STRING
,
360 .help
= "Overrides the \"compatible\" property of the dt root node",
362 .name
= "dump-guest-core",
363 .type
= QEMU_OPT_BOOL
,
364 .help
= "Include guest memory in a core dump",
367 .type
= QEMU_OPT_BOOL
,
368 .help
= "enable/disable memory merge support",
371 .type
= QEMU_OPT_BOOL
,
372 .help
= "Set on/off to enable/disable usb",
375 .type
= QEMU_OPT_STRING
,
376 .help
= "firmware image",
379 .type
= QEMU_OPT_STRING
,
380 .help
= "Specifies the KVM virtualization mode (HV, PR)",
382 .name
= PC_MACHINE_MAX_RAM_BELOW_4G
,
383 .type
= QEMU_OPT_SIZE
,
384 .help
= "maximum ram below the 4G boundary (32bit boundary)",
386 .name
= PC_MACHINE_VMPORT
,
387 .type
= QEMU_OPT_BOOL
,
388 .help
= "Enable vmport (pc & q35)",
391 .type
= QEMU_OPT_BOOL
,
392 .help
= "Set on/off to enable/disable Intel IOMMU (VT-d)",
394 { /* End of list */ }
398 static QemuOptsList qemu_boot_opts
= {
400 .implied_opt_name
= "order",
402 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
406 .type
= QEMU_OPT_STRING
,
409 .type
= QEMU_OPT_STRING
,
412 .type
= QEMU_OPT_BOOL
,
415 .type
= QEMU_OPT_STRING
,
417 .name
= "splash-time",
418 .type
= QEMU_OPT_STRING
,
420 .name
= "reboot-timeout",
421 .type
= QEMU_OPT_STRING
,
424 .type
= QEMU_OPT_BOOL
,
430 static QemuOptsList qemu_add_fd_opts
= {
432 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
436 .type
= QEMU_OPT_NUMBER
,
437 .help
= "file descriptor of which a duplicate is added to fd set",
440 .type
= QEMU_OPT_NUMBER
,
441 .help
= "ID of the fd set to add fd to",
444 .type
= QEMU_OPT_STRING
,
445 .help
= "free-form string used to describe fd",
447 { /* end of list */ }
451 static QemuOptsList qemu_object_opts
= {
453 .implied_opt_name
= "qom-type",
454 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
460 static QemuOptsList qemu_tpmdev_opts
= {
462 .implied_opt_name
= "type",
463 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
465 /* options are defined in the TPM backends */
466 { /* end of list */ }
470 static QemuOptsList qemu_realtime_opts
= {
472 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
476 .type
= QEMU_OPT_BOOL
,
478 { /* end of list */ }
482 static QemuOptsList qemu_msg_opts
= {
484 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
488 .type
= QEMU_OPT_BOOL
,
490 { /* end of list */ }
494 static QemuOptsList qemu_name_opts
= {
496 .implied_opt_name
= "guest",
498 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
502 .type
= QEMU_OPT_STRING
,
503 .help
= "Sets the name of the guest.\n"
504 "This name will be displayed in the SDL window caption.\n"
505 "The name will also be used for the VNC server",
508 .type
= QEMU_OPT_STRING
,
509 .help
= "Sets the name of the QEMU process, as shown in top etc",
511 .name
= "debug-threads",
512 .type
= QEMU_OPT_BOOL
,
513 .help
= "When enabled, name the individual threads; defaults off.\n"
514 "NOTE: The thread names are for debugging and not a\n"
517 { /* End of list */ }
521 static QemuOptsList qemu_mem_opts
= {
523 .implied_opt_name
= "size",
524 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
529 .type
= QEMU_OPT_SIZE
,
533 .type
= QEMU_OPT_NUMBER
,
537 .type
= QEMU_OPT_SIZE
,
539 { /* end of list */ }
543 static QemuOptsList qemu_icount_opts
= {
545 .implied_opt_name
= "shift",
547 .head
= QTAILQ_HEAD_INITIALIZER(qemu_icount_opts
.head
),
551 .type
= QEMU_OPT_STRING
,
554 .type
= QEMU_OPT_BOOL
,
556 { /* end of list */ }
561 * Get machine options
563 * Returns: machine options (never null).
565 QemuOpts
*qemu_get_machine_opts(void)
567 return qemu_find_opts_singleton("machine");
570 const char *qemu_get_vm_name(void)
575 static void res_free(void)
577 if (boot_splash_filedata
!= NULL
) {
578 g_free(boot_splash_filedata
);
579 boot_splash_filedata
= NULL
;
583 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
585 const char *driver
= qemu_opt_get(opts
, "driver");
590 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
591 if (strcmp(default_list
[i
].driver
, driver
) != 0)
593 *(default_list
[i
].flag
) = 0;
598 /***********************************************************/
601 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
603 /* We use RUN_STATE_MAX but any invalid value will do */
604 static RunState vmstop_requested
= RUN_STATE_MAX
;
605 static QemuMutex vmstop_lock
;
610 } RunStateTransition
;
612 static const RunStateTransition runstate_transitions_def
[] = {
614 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
615 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
617 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
618 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
620 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
621 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
623 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
624 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
626 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
627 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
629 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
630 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
632 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
633 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
634 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
636 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
637 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
639 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
641 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
642 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
643 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
644 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
645 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
646 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
647 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
648 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
649 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
650 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
652 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
654 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
655 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
657 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
658 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
659 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
660 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
662 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
663 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
665 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
666 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
668 { RUN_STATE_MAX
, RUN_STATE_MAX
},
671 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
673 bool runstate_check(RunState state
)
675 return current_run_state
== state
;
678 static void runstate_init(void)
680 const RunStateTransition
*p
;
682 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
683 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
684 runstate_valid_transitions
[p
->from
][p
->to
] = true;
687 qemu_mutex_init(&vmstop_lock
);
690 /* This function will abort() on invalid state transitions */
691 void runstate_set(RunState new_state
)
693 assert(new_state
< RUN_STATE_MAX
);
695 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
696 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
697 RunState_lookup
[current_run_state
],
698 RunState_lookup
[new_state
]);
701 trace_runstate_set(new_state
);
702 current_run_state
= new_state
;
705 int runstate_is_running(void)
707 return runstate_check(RUN_STATE_RUNNING
);
710 bool runstate_needs_reset(void)
712 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
713 runstate_check(RUN_STATE_SHUTDOWN
);
716 StatusInfo
*qmp_query_status(Error
**errp
)
718 StatusInfo
*info
= g_malloc0(sizeof(*info
));
720 info
->running
= runstate_is_running();
721 info
->singlestep
= singlestep
;
722 info
->status
= current_run_state
;
727 static bool qemu_vmstop_requested(RunState
*r
)
729 qemu_mutex_lock(&vmstop_lock
);
730 *r
= vmstop_requested
;
731 vmstop_requested
= RUN_STATE_MAX
;
732 qemu_mutex_unlock(&vmstop_lock
);
733 return *r
< RUN_STATE_MAX
;
736 void qemu_system_vmstop_request_prepare(void)
738 qemu_mutex_lock(&vmstop_lock
);
741 void qemu_system_vmstop_request(RunState state
)
743 vmstop_requested
= state
;
744 qemu_mutex_unlock(&vmstop_lock
);
752 qemu_vmstop_requested(&requested
);
753 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
757 /* Ensure that a STOP/RESUME pair of events is emitted if a
758 * vmstop request was pending. The BLOCK_IO_ERROR event, for
759 * example, according to documentation is always followed by
762 if (runstate_is_running()) {
763 qapi_event_send_stop(&error_abort
);
766 runstate_set(RUN_STATE_RUNNING
);
767 vm_state_notify(1, RUN_STATE_RUNNING
);
771 qapi_event_send_resume(&error_abort
);
775 /***********************************************************/
776 /* real time host monotonic timer */
778 /***********************************************************/
779 /* host time/date access */
780 void qemu_get_timedate(struct tm
*tm
, int offset
)
786 if (rtc_date_offset
== -1) {
790 localtime_r(&ti
, tm
);
792 ti
-= rtc_date_offset
;
797 int qemu_timedate_diff(struct tm
*tm
)
801 if (rtc_date_offset
== -1)
803 seconds
= mktimegm(tm
);
806 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
807 seconds
= mktime(&tmp
);
810 seconds
= mktimegm(tm
) + rtc_date_offset
;
812 return seconds
- time(NULL
);
815 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
817 time_t rtc_start_date
;
820 if (!strcmp(startdate
, "now") && legacy
) {
821 rtc_date_offset
= -1;
823 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
831 } else if (sscanf(startdate
, "%d-%d-%d",
843 rtc_start_date
= mktimegm(&tm
);
844 if (rtc_start_date
== -1) {
846 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
847 "'2006-06-17T16:01:21' or '2006-06-17'\n");
850 rtc_date_offset
= time(NULL
) - rtc_start_date
;
854 static void configure_rtc(QemuOpts
*opts
)
858 value
= qemu_opt_get(opts
, "base");
860 if (!strcmp(value
, "utc")) {
862 } else if (!strcmp(value
, "localtime")) {
865 configure_rtc_date_offset(value
, 0);
868 value
= qemu_opt_get(opts
, "clock");
870 if (!strcmp(value
, "host")) {
871 rtc_clock
= QEMU_CLOCK_HOST
;
872 } else if (!strcmp(value
, "rt")) {
873 rtc_clock
= QEMU_CLOCK_REALTIME
;
874 } else if (!strcmp(value
, "vm")) {
875 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
877 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
881 value
= qemu_opt_get(opts
, "driftfix");
883 if (!strcmp(value
, "slew")) {
884 static GlobalProperty slew_lost_ticks
[] = {
886 .driver
= "mc146818rtc",
887 .property
= "lost_tick_policy",
890 { /* end of list */ }
893 qdev_prop_register_global_list(slew_lost_ticks
);
894 } else if (!strcmp(value
, "none")) {
895 /* discard is default */
897 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
903 /***********************************************************/
904 /* Bluetooth support */
907 static struct HCIInfo
*hci_table
[MAX_NICS
];
909 struct HCIInfo
*qemu_next_hci(void)
911 if (cur_hci
== nb_hcis
)
914 return hci_table
[cur_hci
++];
917 static int bt_hci_parse(const char *str
)
922 if (nb_hcis
>= MAX_NICS
) {
923 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
936 bdaddr
.b
[5] = 0x56 + nb_hcis
;
937 hci
->bdaddr_set(hci
, bdaddr
.b
);
939 hci_table
[nb_hcis
++] = hci
;
944 static void bt_vhci_add(int vlan_id
)
946 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
949 fprintf(stderr
, "qemu: warning: adding a VHCI to "
950 "an empty scatternet %i\n", vlan_id
);
952 bt_vhci_init(bt_new_hci(vlan
));
955 static struct bt_device_s
*bt_device_add(const char *opt
)
957 struct bt_scatternet_s
*vlan
;
959 char *endp
= strstr(opt
, ",vlan=");
960 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
963 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
966 vlan_id
= strtol(endp
+ 6, &endp
, 0);
968 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
973 vlan
= qemu_find_bt_vlan(vlan_id
);
976 fprintf(stderr
, "qemu: warning: adding a slave device to "
977 "an empty scatternet %i\n", vlan_id
);
979 if (!strcmp(devname
, "keyboard"))
980 return bt_keyboard_init(vlan
);
982 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
986 static int bt_parse(const char *opt
)
988 const char *endp
, *p
;
991 if (strstart(opt
, "hci", &endp
)) {
992 if (!*endp
|| *endp
== ',') {
994 if (!strstart(endp
, ",vlan=", 0))
997 return bt_hci_parse(opt
);
999 } else if (strstart(opt
, "vhci", &endp
)) {
1000 if (!*endp
|| *endp
== ',') {
1002 if (strstart(endp
, ",vlan=", &p
)) {
1003 vlan
= strtol(p
, (char **) &endp
, 0);
1005 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1009 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1018 } else if (strstart(opt
, "device:", &endp
))
1019 return !bt_device_add(endp
);
1021 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1025 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1027 /* FIXME: change this to true for 1.3 */
1028 if (qemu_opt_get_bool(opts
, "enable", false)) {
1029 #ifdef CONFIG_SECCOMP
1030 if (seccomp_start() < 0) {
1031 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1032 "failed to install seccomp syscall filter in the kernel");
1036 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1037 "sandboxing request but seccomp is not compiled into this build");
1045 static int parse_name(QemuOpts
*opts
, void *opaque
)
1047 const char *proc_name
;
1049 if (qemu_opt_get(opts
, "debug-threads")) {
1050 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1052 qemu_name
= qemu_opt_get(opts
, "guest");
1054 proc_name
= qemu_opt_get(opts
, "process");
1056 os_set_proc_name(proc_name
);
1062 bool usb_enabled(bool default_usb
)
1064 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1065 has_defaults
&& default_usb
);
1069 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1071 int fd
, dupfd
, flags
;
1073 const char *fd_opaque
= NULL
;
1075 fd
= qemu_opt_get_number(opts
, "fd", -1);
1076 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1077 fd_opaque
= qemu_opt_get(opts
, "opaque");
1080 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1081 "fd option is required and must be non-negative");
1085 if (fd
<= STDERR_FILENO
) {
1086 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1087 "fd cannot be a standard I/O stream");
1092 * All fds inherited across exec() necessarily have FD_CLOEXEC
1093 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1095 flags
= fcntl(fd
, F_GETFD
);
1096 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1097 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1098 "fd is not valid or already in use");
1103 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1104 "set option is required and must be non-negative");
1108 #ifdef F_DUPFD_CLOEXEC
1109 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1113 qemu_set_cloexec(dupfd
);
1117 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1118 "Error duplicating fd: %s", strerror(errno
));
1122 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1123 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1129 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1133 fd
= qemu_opt_get_number(opts
, "fd", -1);
1140 /***********************************************************/
1141 /* QEMU Block devices */
1143 #define HD_OPTS "media=disk"
1144 #define CDROM_OPTS "media=cdrom"
1146 #define PFLASH_OPTS ""
1150 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1152 BlockInterfaceType
*block_default_type
= opaque
;
1154 return drive_new(opts
, *block_default_type
) == NULL
;
1157 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1159 if (qemu_opt_get(opts
, "snapshot") == NULL
) {
1160 qemu_opt_set(opts
, "snapshot", "on");
1165 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1166 int index
, const char *optstr
)
1171 if (!enable
|| drive_get_by_index(type
, index
)) {
1175 opts
= drive_add(type
, index
, NULL
, optstr
);
1177 drive_enable_snapshot(opts
, NULL
);
1180 dinfo
= drive_new(opts
, type
);
1184 dinfo
->is_default
= true;
1188 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1190 boot_set_handler
= func
;
1191 boot_set_opaque
= opaque
;
1194 int qemu_boot_set(const char *boot_order
)
1196 if (!boot_set_handler
) {
1199 return boot_set_handler(boot_set_opaque
, boot_order
);
1202 static void validate_bootdevices(const char *devices
)
1204 /* We just do some generic consistency checks */
1208 for (p
= devices
; *p
!= '\0'; p
++) {
1209 /* Allowed boot devices are:
1210 * a-b: floppy disk drives
1211 * c-f: IDE disk drives
1212 * g-m: machine implementation dependent drives
1213 * n-p: network devices
1214 * It's up to each machine implementation to check if the given boot
1215 * devices match the actual hardware implementation and firmware
1218 if (*p
< 'a' || *p
> 'p') {
1219 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1222 if (bitmap
& (1 << (*p
- 'a'))) {
1223 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1226 bitmap
|= 1 << (*p
- 'a');
1230 static void restore_boot_order(void *opaque
)
1232 char *normal_boot_order
= opaque
;
1233 static int first
= 1;
1235 /* Restore boot order and remove ourselves after the first boot */
1241 qemu_boot_set(normal_boot_order
);
1243 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1244 g_free(normal_boot_order
);
1247 static QemuOptsList qemu_smp_opts
= {
1249 .implied_opt_name
= "cpus",
1250 .merge_lists
= true,
1251 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1255 .type
= QEMU_OPT_NUMBER
,
1258 .type
= QEMU_OPT_NUMBER
,
1261 .type
= QEMU_OPT_NUMBER
,
1264 .type
= QEMU_OPT_NUMBER
,
1267 .type
= QEMU_OPT_NUMBER
,
1269 { /*End of list */ }
1273 static void smp_parse(QemuOpts
*opts
)
1277 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1278 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1279 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1280 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1282 /* compute missing values, prefer sockets over cores over threads */
1283 if (cpus
== 0 || sockets
== 0) {
1284 sockets
= sockets
> 0 ? sockets
: 1;
1285 cores
= cores
> 0 ? cores
: 1;
1286 threads
= threads
> 0 ? threads
: 1;
1288 cpus
= cores
* threads
* sockets
;
1292 threads
= threads
> 0 ? threads
: 1;
1293 cores
= cpus
/ (sockets
* threads
);
1295 threads
= cpus
/ (cores
* sockets
);
1299 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1302 smp_cores
= cores
> 0 ? cores
: 1;
1303 smp_threads
= threads
> 0 ? threads
: 1;
1307 if (max_cpus
== 0) {
1308 max_cpus
= smp_cpus
;
1311 if (max_cpus
> MAX_CPUMASK_BITS
) {
1312 fprintf(stderr
, "Unsupported number of maxcpus\n");
1315 if (max_cpus
< smp_cpus
) {
1316 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1322 static void realtime_init(void)
1325 if (os_mlock() < 0) {
1326 fprintf(stderr
, "qemu: locking memory failed\n");
1333 static void configure_msg(QemuOpts
*opts
)
1335 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1338 /***********************************************************/
1341 static int usb_device_add(const char *devname
)
1343 USBDevice
*dev
= NULL
;
1344 #ifndef CONFIG_LINUX
1348 if (!usb_enabled(false)) {
1352 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1353 dev
= usbdevice_create(devname
);
1357 /* the other ones */
1358 #ifndef CONFIG_LINUX
1359 /* only the linux version is qdev-ified, usb-bsd still needs this */
1360 if (strstart(devname
, "host:", &p
)) {
1361 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1371 static int usb_device_del(const char *devname
)
1376 if (strstart(devname
, "host:", &p
)) {
1380 if (!usb_enabled(false)) {
1384 p
= strchr(devname
, '.');
1387 bus_num
= strtoul(devname
, NULL
, 0);
1388 addr
= strtoul(p
+ 1, NULL
, 0);
1390 return usb_device_delete_addr(bus_num
, addr
);
1393 static int usb_parse(const char *cmdline
)
1396 r
= usb_device_add(cmdline
);
1398 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1403 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1405 const char *devname
= qdict_get_str(qdict
, "devname");
1406 if (usb_device_add(devname
) < 0) {
1407 error_report("could not add USB device '%s'", devname
);
1411 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1413 const char *devname
= qdict_get_str(qdict
, "devname");
1414 if (usb_device_del(devname
) < 0) {
1415 error_report("could not delete USB device '%s'", devname
);
1419 /***********************************************************/
1420 /* machine registration */
1422 MachineState
*current_machine
;
1424 static void machine_class_init(ObjectClass
*oc
, void *data
)
1426 MachineClass
*mc
= MACHINE_CLASS(oc
);
1427 QEMUMachine
*qm
= data
;
1429 mc
->family
= qm
->family
;
1430 mc
->name
= qm
->name
;
1431 mc
->alias
= qm
->alias
;
1432 mc
->desc
= qm
->desc
;
1433 mc
->init
= qm
->init
;
1434 mc
->reset
= qm
->reset
;
1435 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1436 mc
->kvm_type
= qm
->kvm_type
;
1437 mc
->block_default_type
= qm
->block_default_type
;
1438 mc
->units_per_default_bus
= qm
->units_per_default_bus
;
1439 mc
->max_cpus
= qm
->max_cpus
;
1440 mc
->no_serial
= qm
->no_serial
;
1441 mc
->no_parallel
= qm
->no_parallel
;
1442 mc
->use_virtcon
= qm
->use_virtcon
;
1443 mc
->use_sclp
= qm
->use_sclp
;
1444 mc
->no_floppy
= qm
->no_floppy
;
1445 mc
->no_cdrom
= qm
->no_cdrom
;
1446 mc
->no_sdcard
= qm
->no_sdcard
;
1447 mc
->has_dynamic_sysbus
= qm
->has_dynamic_sysbus
;
1448 mc
->is_default
= qm
->is_default
;
1449 mc
->default_machine_opts
= qm
->default_machine_opts
;
1450 mc
->default_boot_order
= qm
->default_boot_order
;
1451 mc
->default_display
= qm
->default_display
;
1452 mc
->compat_props
= qm
->compat_props
;
1453 mc
->hw_version
= qm
->hw_version
;
1456 int qemu_register_machine(QEMUMachine
*m
)
1458 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1461 .parent
= TYPE_MACHINE
,
1462 .class_init
= machine_class_init
,
1463 .class_data
= (void *)m
,
1472 static MachineClass
*find_machine(const char *name
)
1474 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1475 MachineClass
*mc
= NULL
;
1477 for (el
= machines
; el
; el
= el
->next
) {
1478 MachineClass
*temp
= el
->data
;
1480 if (!strcmp(temp
->name
, name
)) {
1485 !strcmp(temp
->alias
, name
)) {
1491 g_slist_free(machines
);
1495 MachineClass
*find_default_machine(void)
1497 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1498 MachineClass
*mc
= NULL
;
1500 for (el
= machines
; el
; el
= el
->next
) {
1501 MachineClass
*temp
= el
->data
;
1503 if (temp
->is_default
) {
1509 g_slist_free(machines
);
1513 MachineInfoList
*qmp_query_machines(Error
**errp
)
1515 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1516 MachineInfoList
*mach_list
= NULL
;
1518 for (el
= machines
; el
; el
= el
->next
) {
1519 MachineClass
*mc
= el
->data
;
1520 MachineInfoList
*entry
;
1523 info
= g_malloc0(sizeof(*info
));
1524 if (mc
->is_default
) {
1525 info
->has_is_default
= true;
1526 info
->is_default
= true;
1530 info
->has_alias
= true;
1531 info
->alias
= g_strdup(mc
->alias
);
1534 info
->name
= g_strdup(mc
->name
);
1535 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1537 entry
= g_malloc0(sizeof(*entry
));
1538 entry
->value
= info
;
1539 entry
->next
= mach_list
;
1543 g_slist_free(machines
);
1547 /***********************************************************/
1548 /* main execution loop */
1550 struct vm_change_state_entry
{
1551 VMChangeStateHandler
*cb
;
1553 QLIST_ENTRY (vm_change_state_entry
) entries
;
1556 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1558 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1561 VMChangeStateEntry
*e
;
1563 e
= g_malloc0(sizeof (*e
));
1567 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1571 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1573 QLIST_REMOVE (e
, entries
);
1577 void vm_state_notify(int running
, RunState state
)
1579 VMChangeStateEntry
*e
, *next
;
1581 trace_vm_state_notify(running
, state
);
1583 QLIST_FOREACH_SAFE(e
, &vm_change_state_head
, entries
, next
) {
1584 e
->cb(e
->opaque
, running
, state
);
1588 /* reset/shutdown handler */
1590 typedef struct QEMUResetEntry
{
1591 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1592 QEMUResetHandler
*func
;
1596 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1597 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1598 static int reset_requested
;
1599 static int shutdown_requested
, shutdown_signal
= -1;
1600 static pid_t shutdown_pid
;
1601 static int powerdown_requested
;
1602 static int debug_requested
;
1603 static int suspend_requested
;
1604 static WakeupReason wakeup_reason
;
1605 static NotifierList powerdown_notifiers
=
1606 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1607 static NotifierList suspend_notifiers
=
1608 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1609 static NotifierList wakeup_notifiers
=
1610 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1611 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1613 int qemu_shutdown_requested_get(void)
1615 return shutdown_requested
;
1618 int qemu_reset_requested_get(void)
1620 return reset_requested
;
1623 static int qemu_shutdown_requested(void)
1625 return atomic_xchg(&shutdown_requested
, 0);
1628 static void qemu_kill_report(void)
1630 if (!qtest_driver() && shutdown_signal
!= -1) {
1631 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1632 if (shutdown_pid
== 0) {
1633 /* This happens for eg ^C at the terminal, so it's worth
1634 * avoiding printing an odd message in that case.
1636 fputc('\n', stderr
);
1638 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1640 shutdown_signal
= -1;
1644 static int qemu_reset_requested(void)
1646 int r
= reset_requested
;
1647 reset_requested
= 0;
1651 static int qemu_suspend_requested(void)
1653 int r
= suspend_requested
;
1654 suspend_requested
= 0;
1658 static WakeupReason
qemu_wakeup_requested(void)
1660 return wakeup_reason
;
1663 static int qemu_powerdown_requested(void)
1665 int r
= powerdown_requested
;
1666 powerdown_requested
= 0;
1670 static int qemu_debug_requested(void)
1672 int r
= debug_requested
;
1673 debug_requested
= 0;
1677 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1679 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1682 re
->opaque
= opaque
;
1683 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1686 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1690 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1691 if (re
->func
== func
&& re
->opaque
== opaque
) {
1692 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1699 void qemu_devices_reset(void)
1701 QEMUResetEntry
*re
, *nre
;
1703 /* reset all devices */
1704 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1705 re
->func(re
->opaque
);
1709 void qemu_system_reset(bool report
)
1713 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1715 if (mc
&& mc
->reset
) {
1718 qemu_devices_reset();
1721 qapi_event_send_reset(&error_abort
);
1723 cpu_synchronize_all_post_reset();
1726 void qemu_system_reset_request(void)
1729 shutdown_requested
= 1;
1731 reset_requested
= 1;
1734 qemu_notify_event();
1737 static void qemu_system_suspend(void)
1740 notifier_list_notify(&suspend_notifiers
, NULL
);
1741 runstate_set(RUN_STATE_SUSPENDED
);
1742 qapi_event_send_suspend(&error_abort
);
1745 void qemu_system_suspend_request(void)
1747 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1750 suspend_requested
= 1;
1752 qemu_notify_event();
1755 void qemu_register_suspend_notifier(Notifier
*notifier
)
1757 notifier_list_add(&suspend_notifiers
, notifier
);
1760 void qemu_system_wakeup_request(WakeupReason reason
)
1762 trace_system_wakeup_request(reason
);
1764 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1767 if (!(wakeup_reason_mask
& (1 << reason
))) {
1770 runstate_set(RUN_STATE_RUNNING
);
1771 wakeup_reason
= reason
;
1772 qemu_notify_event();
1775 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1778 wakeup_reason_mask
|= (1 << reason
);
1780 wakeup_reason_mask
&= ~(1 << reason
);
1784 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1786 notifier_list_add(&wakeup_notifiers
, notifier
);
1789 void qemu_system_killed(int signal
, pid_t pid
)
1791 shutdown_signal
= signal
;
1794 qemu_system_shutdown_request();
1797 void qemu_system_shutdown_request(void)
1799 trace_qemu_system_shutdown_request();
1800 shutdown_requested
= 1;
1801 qemu_notify_event();
1804 static void qemu_system_powerdown(void)
1806 qapi_event_send_powerdown(&error_abort
);
1807 notifier_list_notify(&powerdown_notifiers
, NULL
);
1810 void qemu_system_powerdown_request(void)
1812 trace_qemu_system_powerdown_request();
1813 powerdown_requested
= 1;
1814 qemu_notify_event();
1817 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1819 notifier_list_add(&powerdown_notifiers
, notifier
);
1822 void qemu_system_debug_request(void)
1824 debug_requested
= 1;
1825 qemu_notify_event();
1828 static bool main_loop_should_exit(void)
1831 if (qemu_debug_requested()) {
1832 vm_stop(RUN_STATE_DEBUG
);
1834 if (qemu_suspend_requested()) {
1835 qemu_system_suspend();
1837 if (qemu_shutdown_requested()) {
1839 qapi_event_send_shutdown(&error_abort
);
1841 vm_stop(RUN_STATE_SHUTDOWN
);
1846 if (qemu_reset_requested()) {
1848 cpu_synchronize_all_states();
1849 qemu_system_reset(VMRESET_REPORT
);
1851 if (runstate_needs_reset()) {
1852 runstate_set(RUN_STATE_PAUSED
);
1855 if (qemu_wakeup_requested()) {
1857 cpu_synchronize_all_states();
1858 qemu_system_reset(VMRESET_SILENT
);
1859 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1860 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1862 qapi_event_send_wakeup(&error_abort
);
1864 if (qemu_powerdown_requested()) {
1865 qemu_system_powerdown();
1867 if (qemu_vmstop_requested(&r
)) {
1873 static void main_loop(void)
1877 #ifdef CONFIG_PROFILER
1881 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1882 #ifdef CONFIG_PROFILER
1883 ti
= profile_getclock();
1885 last_io
= main_loop_wait(nonblocking
);
1886 #ifdef CONFIG_PROFILER
1887 dev_time
+= profile_getclock() - ti
;
1889 } while (!main_loop_should_exit());
1892 static void version(void)
1894 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
1897 static void QEMU_NORETURN
help(int exitcode
)
1900 printf("usage: %s [options] [disk_image]\n\n"
1901 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1902 error_get_progname());
1904 #define QEMU_OPTIONS_GENERATE_HELP
1905 #include "qemu-options-wrapper.h"
1907 printf("\nDuring emulation, the following keys are useful:\n"
1908 "ctrl-alt-f toggle full screen\n"
1909 "ctrl-alt-n switch to virtual console 'n'\n"
1910 "ctrl-alt toggle mouse and keyboard grab\n"
1912 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1917 #define HAS_ARG 0x0001
1919 typedef struct QEMUOption
{
1926 static const QEMUOption qemu_options
[] = {
1927 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
1928 #define QEMU_OPTIONS_GENERATE_OPTIONS
1929 #include "qemu-options-wrapper.h"
1933 static bool vga_available(void)
1935 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1938 static bool cirrus_vga_available(void)
1940 return object_class_by_name("cirrus-vga")
1941 || object_class_by_name("isa-cirrus-vga");
1944 static bool vmware_vga_available(void)
1946 return object_class_by_name("vmware-svga");
1949 static bool qxl_vga_available(void)
1951 return object_class_by_name("qxl-vga");
1954 static bool tcx_vga_available(void)
1956 return object_class_by_name("SUNW,tcx");
1959 static bool cg3_vga_available(void)
1961 return object_class_by_name("cgthree");
1964 static void select_vgahw (const char *p
)
1968 assert(vga_interface_type
== VGA_NONE
);
1969 if (strstart(p
, "std", &opts
)) {
1970 if (vga_available()) {
1971 vga_interface_type
= VGA_STD
;
1973 fprintf(stderr
, "Error: standard VGA not available\n");
1976 } else if (strstart(p
, "cirrus", &opts
)) {
1977 if (cirrus_vga_available()) {
1978 vga_interface_type
= VGA_CIRRUS
;
1980 fprintf(stderr
, "Error: Cirrus VGA not available\n");
1983 } else if (strstart(p
, "vmware", &opts
)) {
1984 if (vmware_vga_available()) {
1985 vga_interface_type
= VGA_VMWARE
;
1987 fprintf(stderr
, "Error: VMWare SVGA not available\n");
1990 } else if (strstart(p
, "xenfb", &opts
)) {
1991 vga_interface_type
= VGA_XENFB
;
1992 } else if (strstart(p
, "qxl", &opts
)) {
1993 if (qxl_vga_available()) {
1994 vga_interface_type
= VGA_QXL
;
1996 fprintf(stderr
, "Error: QXL VGA not available\n");
1999 } else if (strstart(p
, "tcx", &opts
)) {
2000 if (tcx_vga_available()) {
2001 vga_interface_type
= VGA_TCX
;
2003 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2006 } else if (strstart(p
, "cg3", &opts
)) {
2007 if (cg3_vga_available()) {
2008 vga_interface_type
= VGA_CG3
;
2010 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2013 } else if (!strstart(p
, "none", &opts
)) {
2015 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2019 const char *nextopt
;
2021 if (strstart(opts
, ",retrace=", &nextopt
)) {
2023 if (strstart(opts
, "dumb", &nextopt
))
2024 vga_retrace_method
= VGA_RETRACE_DUMB
;
2025 else if (strstart(opts
, "precise", &nextopt
))
2026 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2027 else goto invalid_vga
;
2028 } else goto invalid_vga
;
2033 static DisplayType
select_display(const char *p
)
2036 DisplayType display
= DT_DEFAULT
;
2038 if (strstart(p
, "sdl", &opts
)) {
2042 const char *nextopt
;
2044 if (strstart(opts
, ",frame=", &nextopt
)) {
2046 if (strstart(opts
, "on", &nextopt
)) {
2048 } else if (strstart(opts
, "off", &nextopt
)) {
2051 goto invalid_sdl_args
;
2053 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2055 if (strstart(opts
, "on", &nextopt
)) {
2057 } else if (strstart(opts
, "off", &nextopt
)) {
2060 goto invalid_sdl_args
;
2062 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2064 if (strstart(opts
, "on", &nextopt
)) {
2066 } else if (strstart(opts
, "off", &nextopt
)) {
2069 goto invalid_sdl_args
;
2071 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2073 if (strstart(opts
, "on", &nextopt
)) {
2075 } else if (strstart(opts
, "off", &nextopt
)) {
2078 goto invalid_sdl_args
;
2082 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2088 fprintf(stderr
, "SDL support is disabled\n");
2091 } else if (strstart(p
, "vnc", &opts
)) {
2096 const char *nextopt
;
2098 if (strstart(opts
, "=", &nextopt
)) {
2099 vnc_display
= nextopt
;
2103 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2107 fprintf(stderr
, "VNC support is disabled\n");
2110 } else if (strstart(p
, "curses", &opts
)) {
2111 #ifdef CONFIG_CURSES
2112 display
= DT_CURSES
;
2114 fprintf(stderr
, "Curses support is disabled\n");
2117 } else if (strstart(p
, "gtk", &opts
)) {
2121 const char *nextopt
;
2123 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2125 if (strstart(opts
, "on", &nextopt
)) {
2126 grab_on_hover
= true;
2127 } else if (strstart(opts
, "off", &nextopt
)) {
2128 grab_on_hover
= false;
2130 goto invalid_gtk_args
;
2134 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2140 fprintf(stderr
, "GTK support is disabled\n");
2143 } else if (strstart(p
, "none", &opts
)) {
2146 fprintf(stderr
, "Unknown display type: %s\n", p
);
2153 static int balloon_parse(const char *arg
)
2157 if (strcmp(arg
, "none") == 0) {
2161 if (!strncmp(arg
, "virtio", 6)) {
2162 if (arg
[6] == ',') {
2163 /* have params -> parse them */
2164 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2168 /* create empty opts */
2169 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2172 qemu_opt_set(opts
, "driver", "virtio-balloon");
2179 #if defined(CONFIG_RUBY)
2180 /* Ruby interface for QEMU. See README.EXT for programming example. */
2184 static VALUE
qemu_open(int argc
, VALUE
*argv
, VALUE klass
)
2187 for (i
= 0; i
< argc
; i
++) {
2188 argv
[i
] = StringValue(argv
[i
]);
2189 fprintf(stderr
, "argv[%d] = %s\n", i
, StringValuePtr(argv
[i
]));
2191 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2192 //~ mode = 0666; /* default value */
2194 return INT2FIX(argc
);
2197 void Init_qemu(void)
2199 printf("%s\n", __func__
);
2200 VALUE cQEMU
= rb_define_class("QEMU", rb_cObject
);
2201 rb_define_singleton_method(cQEMU
, "run", qemu_open
, -1);
2206 #endif /* CONFIG_RUBY */
2208 #if defined(CONFIG_DLL)
2209 BOOL APIENTRY
DllMain(HINSTANCE hModule
, DWORD ul_reason_for_call
, LPVOID data
)
2213 #endif /* CONFIG_DLL */
2215 char *qemu_find_file(int type
, const char *name
)
2221 /* Try the name as a straight path first */
2222 if (access(name
, R_OK
) == 0) {
2223 trace_load_file(name
, name
);
2224 return g_strdup(name
);
2228 case QEMU_FILE_TYPE_BIOS
:
2231 case QEMU_FILE_TYPE_KEYMAP
:
2232 subdir
= "keymaps/";
2238 for (i
= 0; i
< data_dir_idx
; i
++) {
2239 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2240 if (access(buf
, R_OK
) == 0) {
2241 trace_load_file(name
, buf
);
2249 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2251 return qdev_device_help(opts
);
2254 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2258 dev
= qdev_device_add(opts
);
2261 object_unref(OBJECT(dev
));
2265 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2267 Error
*local_err
= NULL
;
2269 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2271 error_report("%s", error_get_pretty(local_err
));
2272 error_free(local_err
);
2278 #ifdef CONFIG_VIRTFS
2279 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2282 ret
= qemu_fsdev_add(opts
);
2288 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2290 CharDriverState
*chr
;
2291 const char *chardev
;
2295 mode
= qemu_opt_get(opts
, "mode");
2299 if (strcmp(mode
, "readline") == 0) {
2300 flags
= MONITOR_USE_READLINE
;
2301 } else if (strcmp(mode
, "control") == 0) {
2302 flags
= MONITOR_USE_CONTROL
;
2304 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2308 if (qemu_opt_get_bool(opts
, "pretty", 0))
2309 flags
|= MONITOR_USE_PRETTY
;
2311 if (qemu_opt_get_bool(opts
, "default", 0))
2312 flags
|= MONITOR_IS_DEFAULT
;
2314 chardev
= qemu_opt_get(opts
, "chardev");
2315 chr
= qemu_chr_find(chardev
);
2317 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2321 qemu_chr_fe_claim_no_fail(chr
);
2322 monitor_init(chr
, flags
);
2326 static void monitor_parse(const char *optarg
, const char *mode
)
2328 static int monitor_device_index
= 0;
2334 if (strstart(optarg
, "chardev:", &p
)) {
2335 snprintf(label
, sizeof(label
), "%s", p
);
2337 snprintf(label
, sizeof(label
), "compat_monitor%d",
2338 monitor_device_index
);
2339 if (monitor_device_index
== 0) {
2342 opts
= qemu_chr_parse_compat(label
, optarg
);
2344 fprintf(stderr
, "parse error: %s\n", optarg
);
2349 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2351 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2354 qemu_opt_set(opts
, "mode", mode
);
2355 qemu_opt_set(opts
, "chardev", label
);
2357 qemu_opt_set(opts
, "default", "on");
2358 monitor_device_index
++;
2361 struct device_config
{
2363 DEV_USB
, /* -usbdevice */
2365 DEV_SERIAL
, /* -serial */
2366 DEV_PARALLEL
, /* -parallel */
2367 DEV_VIRTCON
, /* -virtioconsole */
2368 DEV_DEBUGCON
, /* -debugcon */
2369 DEV_GDB
, /* -gdb, -s */
2370 DEV_SCLP
, /* s390 sclp */
2372 const char *cmdline
;
2374 QTAILQ_ENTRY(device_config
) next
;
2377 static QTAILQ_HEAD(, device_config
) device_configs
=
2378 QTAILQ_HEAD_INITIALIZER(device_configs
);
2380 static void add_device_config(int type
, const char *cmdline
)
2382 struct device_config
*conf
;
2384 conf
= g_malloc0(sizeof(*conf
));
2386 conf
->cmdline
= cmdline
;
2387 loc_save(&conf
->loc
);
2388 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2391 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2393 struct device_config
*conf
;
2396 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2397 if (conf
->type
!= type
)
2399 loc_push_restore(&conf
->loc
);
2400 rc
= func(conf
->cmdline
);
2401 loc_pop(&conf
->loc
);
2409 static int serial_parse(const char *devname
)
2411 static int index
= 0;
2414 if (strcmp(devname
, "none") == 0)
2416 if (index
== MAX_SERIAL_PORTS
) {
2417 fprintf(stderr
, "qemu: too many serial ports\n");
2420 snprintf(label
, sizeof(label
), "serial%d", index
);
2421 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2422 if (!serial_hds
[index
]) {
2423 fprintf(stderr
, "qemu: could not connect serial device"
2424 " to character backend '%s'\n", devname
);
2431 static int parallel_parse(const char *devname
)
2433 static int index
= 0;
2436 if (strcmp(devname
, "none") == 0)
2438 if (index
== MAX_PARALLEL_PORTS
) {
2439 fprintf(stderr
, "qemu: too many parallel ports\n");
2442 snprintf(label
, sizeof(label
), "parallel%d", index
);
2443 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2444 if (!parallel_hds
[index
]) {
2445 fprintf(stderr
, "qemu: could not connect parallel device"
2446 " to character backend '%s'\n", devname
);
2453 static int virtcon_parse(const char *devname
)
2455 QemuOptsList
*device
= qemu_find_opts("device");
2456 static int index
= 0;
2458 QemuOpts
*bus_opts
, *dev_opts
;
2460 if (strcmp(devname
, "none") == 0)
2462 if (index
== MAX_VIRTIO_CONSOLES
) {
2463 fprintf(stderr
, "qemu: too many virtio consoles\n");
2467 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2468 if (arch_type
== QEMU_ARCH_S390X
) {
2469 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2471 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2474 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2475 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2477 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2478 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2479 if (!virtcon_hds
[index
]) {
2480 fprintf(stderr
, "qemu: could not connect virtio console"
2481 " to character backend '%s'\n", devname
);
2484 qemu_opt_set(dev_opts
, "chardev", label
);
2490 static int sclp_parse(const char *devname
)
2492 QemuOptsList
*device
= qemu_find_opts("device");
2493 static int index
= 0;
2497 if (strcmp(devname
, "none") == 0) {
2500 if (index
== MAX_SCLP_CONSOLES
) {
2501 fprintf(stderr
, "qemu: too many sclp consoles\n");
2505 assert(arch_type
== QEMU_ARCH_S390X
);
2507 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2508 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2510 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2511 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2512 if (!sclp_hds
[index
]) {
2513 fprintf(stderr
, "qemu: could not connect sclp console"
2514 " to character backend '%s'\n", devname
);
2517 qemu_opt_set(dev_opts
, "chardev", label
);
2523 static int debugcon_parse(const char *devname
)
2527 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2530 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2532 fprintf(stderr
, "qemu: already have a debugcon device\n");
2535 qemu_opt_set(opts
, "driver", "isa-debugcon");
2536 qemu_opt_set(opts
, "chardev", "debugcon");
2540 static gint
machine_class_cmp(gconstpointer a
, gconstpointer b
)
2542 const MachineClass
*mc1
= a
, *mc2
= b
;
2545 if (mc1
->family
== NULL
) {
2546 if (mc2
->family
== NULL
) {
2547 /* Compare standalone machine types against each other; they sort
2548 * in increasing order.
2550 return strcmp(object_class_get_name(OBJECT_CLASS(mc1
)),
2551 object_class_get_name(OBJECT_CLASS(mc2
)));
2554 /* Standalone machine types sort after families. */
2558 if (mc2
->family
== NULL
) {
2559 /* Families sort before standalone machine types. */
2563 /* Families sort between each other alphabetically increasingly. */
2564 res
= strcmp(mc1
->family
, mc2
->family
);
2569 /* Within the same family, machine types sort in decreasing order. */
2570 return strcmp(object_class_get_name(OBJECT_CLASS(mc2
)),
2571 object_class_get_name(OBJECT_CLASS(mc1
)));
2574 static MachineClass
*machine_parse(const char *name
)
2576 MachineClass
*mc
= NULL
;
2577 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2580 mc
= find_machine(name
);
2585 if (name
&& !is_help_option(name
)) {
2586 error_report("Unsupported machine type");
2587 error_printf("Use -machine help to list supported machines!\n");
2589 printf("Supported machines are:\n");
2590 machines
= g_slist_sort(machines
, machine_class_cmp
);
2591 for (el
= machines
; el
; el
= el
->next
) {
2592 MachineClass
*mc
= el
->data
;
2594 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2596 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2597 mc
->is_default
? " (default)" : "");
2601 g_slist_free(machines
);
2602 exit(!name
|| !is_help_option(name
));
2605 void qemu_add_exit_notifier(Notifier
*notify
)
2607 notifier_list_add(&exit_notifiers
, notify
);
2610 void qemu_remove_exit_notifier(Notifier
*notify
)
2612 notifier_remove(notify
);
2615 static void qemu_run_exit_notifiers(void)
2617 notifier_list_notify(&exit_notifiers
, NULL
);
2620 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2622 notifier_list_add(&machine_init_done_notifiers
, notify
);
2625 static void qemu_run_machine_init_done_notifiers(void)
2627 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2630 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2631 const char **poptarg
, int *poptind
)
2633 const QEMUOption
*popt
;
2634 int optind
= *poptind
;
2635 char *r
= argv
[optind
];
2638 loc_set_cmdline(argv
, optind
, 1);
2640 /* Treat --foo the same as -foo. */
2643 popt
= qemu_options
;
2646 error_report("invalid option");
2649 if (!strcmp(popt
->name
, r
+ 1))
2653 if (popt
->flags
& HAS_ARG
) {
2654 if (optind
>= argc
) {
2655 error_report("requires an argument");
2658 optarg
= argv
[optind
++];
2659 loc_set_cmdline(argv
, optind
- 2, 2);
2670 static gpointer
malloc_and_trace(gsize n_bytes
)
2672 void *ptr
= malloc(n_bytes
);
2673 trace_g_malloc(n_bytes
, ptr
);
2677 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2679 void *ptr
= realloc(mem
, n_bytes
);
2680 trace_g_realloc(mem
, n_bytes
, ptr
);
2684 static void free_and_trace(gpointer mem
)
2690 static int machine_set_property(const char *name
, const char *value
,
2693 Object
*obj
= OBJECT(opaque
);
2694 StringInputVisitor
*siv
;
2695 Error
*local_err
= NULL
;
2698 if (strcmp(name
, "type") == 0) {
2702 qom_name
= g_strdup(name
);
2710 siv
= string_input_visitor_new(value
);
2711 object_property_set(obj
, string_input_get_visitor(siv
), qom_name
, &local_err
);
2712 string_input_visitor_cleanup(siv
);
2716 qerror_report_err(local_err
);
2717 error_free(local_err
);
2724 static int object_create(QemuOpts
*opts
, void *opaque
)
2733 ov
= opts_visitor_new(opts
);
2734 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2736 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2741 qdict_del(pdict
, "qom-type");
2742 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2747 qdict_del(pdict
, "id");
2748 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2753 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2757 visit_end_struct(opts_get_visitor(ov
), &err
);
2759 qmp_object_del(id
, NULL
);
2763 opts_visitor_cleanup(ov
);
2770 qerror_report_err(err
);
2777 int main(int argc
, char **argv
)
2780 int snapshot
, linux_boot
;
2781 const char *initrd_filename
;
2782 const char *kernel_filename
, *kernel_cmdline
;
2783 const char *boot_order
;
2785 int cyls
, heads
, secs
, translation
;
2786 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
, *icount_opts
= NULL
;
2787 QemuOptsList
*olist
;
2790 const char *loadvm
= NULL
;
2791 MachineClass
*machine_class
;
2792 const char *cpu_model
;
2793 const char *vga_model
= NULL
;
2794 const char *qtest_chrdev
= NULL
;
2795 const char *qtest_log
= NULL
;
2796 const char *pid_file
= NULL
;
2797 const char *incoming
= NULL
;
2799 int show_vnc_port
= 0;
2801 bool defconfig
= true;
2802 bool userconfig
= true;
2803 const char *log_mask
= NULL
;
2804 const char *log_file
= NULL
;
2805 GMemVTable mem_trace
= {
2806 .malloc
= malloc_and_trace
,
2807 .realloc
= realloc_and_trace
,
2808 .free
= free_and_trace
,
2810 const char *trace_events
= NULL
;
2811 const char *trace_file
= NULL
;
2812 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2814 ram_addr_t maxram_size
= default_ram_size
;
2815 uint64_t ram_slots
= 0;
2816 FILE *vmstate_dump_file
= NULL
;
2817 Error
*main_loop_err
= NULL
;
2819 atexit(qemu_run_exit_notifiers
);
2820 error_set_progname(argv
[0]);
2821 qemu_init_exec_dir(argv
[0]);
2823 g_mem_set_vtable(&mem_trace
);
2825 module_call_init(MODULE_INIT_QOM
);
2827 qemu_add_opts(&qemu_drive_opts
);
2828 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2829 qemu_add_drive_opts(&qemu_common_drive_opts
);
2830 qemu_add_drive_opts(&qemu_drive_opts
);
2831 qemu_add_opts(&qemu_chardev_opts
);
2832 qemu_add_opts(&qemu_device_opts
);
2833 qemu_add_opts(&qemu_netdev_opts
);
2834 qemu_add_opts(&qemu_net_opts
);
2835 qemu_add_opts(&qemu_rtc_opts
);
2836 qemu_add_opts(&qemu_global_opts
);
2837 qemu_add_opts(&qemu_mon_opts
);
2838 qemu_add_opts(&qemu_trace_opts
);
2839 qemu_add_opts(&qemu_option_rom_opts
);
2840 qemu_add_opts(&qemu_machine_opts
);
2841 qemu_add_opts(&qemu_mem_opts
);
2842 qemu_add_opts(&qemu_smp_opts
);
2843 qemu_add_opts(&qemu_boot_opts
);
2844 qemu_add_opts(&qemu_sandbox_opts
);
2845 qemu_add_opts(&qemu_add_fd_opts
);
2846 qemu_add_opts(&qemu_object_opts
);
2847 qemu_add_opts(&qemu_tpmdev_opts
);
2848 qemu_add_opts(&qemu_realtime_opts
);
2849 qemu_add_opts(&qemu_msg_opts
);
2850 qemu_add_opts(&qemu_name_opts
);
2851 qemu_add_opts(&qemu_numa_opts
);
2852 qemu_add_opts(&qemu_icount_opts
);
2856 rtc_clock
= QEMU_CLOCK_HOST
;
2858 QLIST_INIT (&vm_change_state_head
);
2859 os_setup_early_signal_handling();
2861 module_call_init(MODULE_INIT_MACHINE
);
2863 optarg
= strrchr(argv
[0], '/');
2865 machine_class
= find_machine(optarg
+ 1);
2867 if (!machine_class
) {
2868 machine_class
= find_default_machine();
2871 ram_size
= default_ram_size
;
2873 cyls
= heads
= secs
= 0;
2874 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2876 for (i
= 0; i
< MAX_NODES
; i
++) {
2877 numa_info
[i
].node_mem
= 0;
2878 numa_info
[i
].present
= false;
2879 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2883 max_numa_nodeid
= 0;
2886 bdrv_init_with_whitelist();
2890 /* first pass of option parsing */
2892 while (optind
< argc
) {
2893 if (argv
[optind
][0] != '-') {
2897 const QEMUOption
*popt
;
2899 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2900 switch (popt
->index
) {
2901 case QEMU_OPTION_nodefconfig
:
2904 case QEMU_OPTION_nouserconfig
:
2913 ret
= qemu_read_default_config_files(userconfig
);
2919 /* second pass of option parsing */
2924 if (argv
[optind
][0] != '-') {
2925 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
2927 const QEMUOption
*popt
;
2929 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2930 if (!(popt
->arch_mask
& arch_type
)) {
2931 printf("Option %s not supported for this target\n", popt
->name
);
2934 switch(popt
->index
) {
2936 machine_class
= machine_parse(optarg
);
2938 case QEMU_OPTION_no_kvm_irqchip
: {
2939 olist
= qemu_find_opts("machine");
2940 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
2943 case QEMU_OPTION_cpu
:
2944 /* hw initialization will check this */
2947 case QEMU_OPTION_hda
:
2951 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
2953 snprintf(buf
, sizeof(buf
),
2954 "%s,cyls=%d,heads=%d,secs=%d%s",
2955 HD_OPTS
, cyls
, heads
, secs
,
2956 translation
== BIOS_ATA_TRANSLATION_LBA
?
2958 translation
== BIOS_ATA_TRANSLATION_NONE
?
2959 ",trans=none" : "");
2960 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
2963 case QEMU_OPTION_hdb
:
2964 case QEMU_OPTION_hdc
:
2965 case QEMU_OPTION_hdd
:
2966 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
2969 case QEMU_OPTION_drive
:
2970 if (drive_def(optarg
) == NULL
) {
2974 case QEMU_OPTION_set
:
2975 if (qemu_set_option(optarg
) != 0)
2978 case QEMU_OPTION_global
:
2979 if (qemu_global_option(optarg
) != 0)
2982 case QEMU_OPTION_mtdblock
:
2983 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
2985 case QEMU_OPTION_sd
:
2986 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
2988 case QEMU_OPTION_pflash
:
2989 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
2991 case QEMU_OPTION_snapshot
:
2994 case QEMU_OPTION_hdachs
:
2998 cyls
= strtol(p
, (char **)&p
, 0);
2999 if (cyls
< 1 || cyls
> 16383)
3004 heads
= strtol(p
, (char **)&p
, 0);
3005 if (heads
< 1 || heads
> 16)
3010 secs
= strtol(p
, (char **)&p
, 0);
3011 if (secs
< 1 || secs
> 63)
3015 if (!strcmp(p
, "large")) {
3016 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3017 } else if (!strcmp(p
, "rechs")) {
3018 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3019 } else if (!strcmp(p
, "none")) {
3020 translation
= BIOS_ATA_TRANSLATION_NONE
;
3021 } else if (!strcmp(p
, "lba")) {
3022 translation
= BIOS_ATA_TRANSLATION_LBA
;
3023 } else if (!strcmp(p
, "auto")) {
3024 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3028 } else if (*p
!= '\0') {
3030 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3033 if (hda_opts
!= NULL
) {
3035 snprintf(num
, sizeof(num
), "%d", cyls
);
3036 qemu_opt_set(hda_opts
, "cyls", num
);
3037 snprintf(num
, sizeof(num
), "%d", heads
);
3038 qemu_opt_set(hda_opts
, "heads", num
);
3039 snprintf(num
, sizeof(num
), "%d", secs
);
3040 qemu_opt_set(hda_opts
, "secs", num
);
3041 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3042 qemu_opt_set(hda_opts
, "trans", "large");
3043 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3044 qemu_opt_set(hda_opts
, "trans", "rechs");
3045 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3046 qemu_opt_set(hda_opts
, "trans", "lba");
3047 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3048 qemu_opt_set(hda_opts
, "trans", "none");
3053 case QEMU_OPTION_numa
:
3054 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3059 case QEMU_OPTION_display
:
3060 display_type
= select_display(optarg
);
3062 case QEMU_OPTION_nographic
:
3063 display_type
= DT_NOGRAPHIC
;
3065 case QEMU_OPTION_curses
:
3066 #ifdef CONFIG_CURSES
3067 display_type
= DT_CURSES
;
3069 fprintf(stderr
, "Curses support is disabled\n");
3073 case QEMU_OPTION_portrait
:
3074 graphic_rotate
= 90;
3076 case QEMU_OPTION_rotate
:
3077 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3078 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3079 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3081 "qemu: only 90, 180, 270 deg rotation is available\n");
3085 case QEMU_OPTION_kernel
:
3086 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3088 case QEMU_OPTION_initrd
:
3089 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3091 case QEMU_OPTION_append
:
3092 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3094 case QEMU_OPTION_dtb
:
3095 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3097 case QEMU_OPTION_cdrom
:
3098 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3100 case QEMU_OPTION_boot
:
3101 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3106 case QEMU_OPTION_fda
:
3107 case QEMU_OPTION_fdb
:
3108 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3111 case QEMU_OPTION_no_fd_bootchk
:
3114 case QEMU_OPTION_netdev
:
3115 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3119 case QEMU_OPTION_net
:
3120 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3124 #ifdef CONFIG_LIBISCSI
3125 case QEMU_OPTION_iscsi
:
3126 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3133 case QEMU_OPTION_tftp
:
3134 legacy_tftp_prefix
= optarg
;
3136 case QEMU_OPTION_bootp
:
3137 legacy_bootp_filename
= optarg
;
3139 case QEMU_OPTION_redir
:
3140 if (net_slirp_redir(optarg
) < 0)
3144 case QEMU_OPTION_bt
:
3145 add_device_config(DEV_BT
, optarg
);
3147 case QEMU_OPTION_audio_help
:
3151 case QEMU_OPTION_soundhw
:
3152 select_soundhw (optarg
);
3157 case QEMU_OPTION_version
:
3161 case QEMU_OPTION_m
: {
3163 const char *mem_str
;
3164 const char *maxmem_str
, *slots_str
;
3166 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3172 mem_str
= qemu_opt_get(opts
, "size");
3174 error_report("invalid -m option, missing 'size' option");
3178 error_report("missing 'size' option value");
3182 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3184 /* Fix up legacy suffix-less format */
3185 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3186 uint64_t overflow_check
= sz
;
3189 if ((sz
>> 20) != overflow_check
) {
3190 error_report("too large 'size' option value");
3195 /* backward compatibility behaviour for case "-m 0" */
3197 sz
= default_ram_size
;
3200 sz
= QEMU_ALIGN_UP(sz
, 8192);
3202 if (ram_size
!= sz
) {
3203 error_report("ram size too large");
3206 maxram_size
= ram_size
;
3208 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3209 slots_str
= qemu_opt_get(opts
, "slots");
3210 if (maxmem_str
&& slots_str
) {
3213 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3214 if (sz
< ram_size
) {
3215 error_report("invalid -m option value: maxmem "
3216 "(0x%" PRIx64
") <= initial memory (0x"
3217 RAM_ADDR_FMT
")", sz
, ram_size
);
3221 slots
= qemu_opt_get_number(opts
, "slots", 0);
3222 if ((sz
> ram_size
) && !slots
) {
3223 error_report("invalid -m option value: maxmem "
3224 "(0x%" PRIx64
") more than initial memory (0x"
3225 RAM_ADDR_FMT
") but no hotplug slots where "
3226 "specified", sz
, ram_size
);
3230 if ((sz
<= ram_size
) && slots
) {
3231 error_report("invalid -m option value: %"
3232 PRIu64
" hotplug slots where specified but "
3233 "maxmem (0x%" PRIx64
") <= initial memory (0x"
3234 RAM_ADDR_FMT
")", slots
, sz
, ram_size
);
3239 } else if ((!maxmem_str
&& slots_str
) ||
3240 (maxmem_str
&& !slots_str
)) {
3241 error_report("invalid -m option value: missing "
3242 "'%s' option", slots_str
? "maxmem" : "slots");
3248 case QEMU_OPTION_tpmdev
:
3249 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3254 case QEMU_OPTION_mempath
:
3257 case QEMU_OPTION_mem_prealloc
:
3267 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3269 case QEMU_OPTION_gdb
:
3270 add_device_config(DEV_GDB
, optarg
);
3273 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3274 data_dir
[data_dir_idx
++] = optarg
;
3277 case QEMU_OPTION_bios
:
3278 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3280 case QEMU_OPTION_singlestep
:
3287 keyboard_layout
= optarg
;
3289 case QEMU_OPTION_localtime
:
3292 case QEMU_OPTION_vga
:
3301 w
= strtol(p
, (char **)&p
, 10);
3304 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3310 h
= strtol(p
, (char **)&p
, 10);
3315 depth
= strtol(p
, (char **)&p
, 10);
3316 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3317 depth
!= 24 && depth
!= 32)
3319 } else if (*p
== '\0') {
3320 depth
= graphic_depth
;
3327 graphic_depth
= depth
;
3330 case QEMU_OPTION_echr
:
3333 term_escape_char
= strtol(optarg
, &r
, 0);
3335 printf("Bad argument to echr\n");
3338 case QEMU_OPTION_monitor
:
3339 default_monitor
= 0;
3340 if (strncmp(optarg
, "none", 4)) {
3341 monitor_parse(optarg
, "readline");
3344 case QEMU_OPTION_qmp
:
3345 monitor_parse(optarg
, "control");
3346 default_monitor
= 0;
3348 case QEMU_OPTION_mon
:
3349 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3353 default_monitor
= 0;
3355 case QEMU_OPTION_chardev
:
3356 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3361 case QEMU_OPTION_fsdev
:
3362 olist
= qemu_find_opts("fsdev");
3364 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3367 opts
= qemu_opts_parse(olist
, optarg
, 1);
3372 case QEMU_OPTION_virtfs
: {
3375 const char *writeout
, *sock_fd
, *socket
;
3377 olist
= qemu_find_opts("virtfs");
3379 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3382 opts
= qemu_opts_parse(olist
, optarg
, 1);
3387 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3388 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3389 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3392 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3393 qemu_opt_get(opts
, "mount_tag"),
3396 fprintf(stderr
, "duplicate fsdev id: %s\n",
3397 qemu_opt_get(opts
, "mount_tag"));
3401 writeout
= qemu_opt_get(opts
, "writeout");
3403 #ifdef CONFIG_SYNC_FILE_RANGE
3404 qemu_opt_set(fsdev
, "writeout", writeout
);
3406 fprintf(stderr
, "writeout=immediate not supported on "
3411 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3412 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3413 qemu_opt_set(fsdev
, "security_model",
3414 qemu_opt_get(opts
, "security_model"));
3415 socket
= qemu_opt_get(opts
, "socket");
3417 qemu_opt_set(fsdev
, "socket", socket
);
3419 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3421 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3424 qemu_opt_set_bool(fsdev
, "readonly",
3425 qemu_opt_get_bool(opts
, "readonly", 0));
3426 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3428 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3429 qemu_opt_set(device
, "fsdev",
3430 qemu_opt_get(opts
, "mount_tag"));
3431 qemu_opt_set(device
, "mount_tag",
3432 qemu_opt_get(opts
, "mount_tag"));
3435 case QEMU_OPTION_virtfs_synth
: {
3439 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3442 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3445 qemu_opt_set(fsdev
, "fsdriver", "synth");
3447 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3449 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3450 qemu_opt_set(device
, "fsdev", "v_synth");
3451 qemu_opt_set(device
, "mount_tag", "v_synth");
3454 case QEMU_OPTION_serial
:
3455 add_device_config(DEV_SERIAL
, optarg
);
3457 if (strncmp(optarg
, "mon:", 4) == 0) {
3458 default_monitor
= 0;
3461 case QEMU_OPTION_watchdog
:
3464 "qemu: only one watchdog option may be given\n");
3469 case QEMU_OPTION_watchdog_action
:
3470 if (select_watchdog_action(optarg
) == -1) {
3471 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3475 case QEMU_OPTION_virtiocon
:
3476 add_device_config(DEV_VIRTCON
, optarg
);
3477 default_virtcon
= 0;
3478 if (strncmp(optarg
, "mon:", 4) == 0) {
3479 default_monitor
= 0;
3482 case QEMU_OPTION_parallel
:
3483 add_device_config(DEV_PARALLEL
, optarg
);
3484 default_parallel
= 0;
3485 if (strncmp(optarg
, "mon:", 4) == 0) {
3486 default_monitor
= 0;
3489 case QEMU_OPTION_debugcon
:
3490 add_device_config(DEV_DEBUGCON
, optarg
);
3492 case QEMU_OPTION_loadvm
:
3495 case QEMU_OPTION_full_screen
:
3498 case QEMU_OPTION_no_frame
:
3501 case QEMU_OPTION_alt_grab
:
3504 case QEMU_OPTION_ctrl_grab
:
3507 case QEMU_OPTION_no_quit
:
3510 case QEMU_OPTION_sdl
:
3512 display_type
= DT_SDL
;
3515 fprintf(stderr
, "SDL support is disabled\n");
3518 case QEMU_OPTION_pidfile
:
3521 case QEMU_OPTION_win2k_hack
:
3522 win2k_install_hack
= 1;
3524 case QEMU_OPTION_rtc_td_hack
: {
3525 static GlobalProperty slew_lost_ticks
[] = {
3527 .driver
= "mc146818rtc",
3528 .property
= "lost_tick_policy",
3531 { /* end of list */ }
3534 qdev_prop_register_global_list(slew_lost_ticks
);
3537 case QEMU_OPTION_acpitable
:
3538 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3542 do_acpitable_option(opts
);
3544 case QEMU_OPTION_smbios
:
3545 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3549 do_smbios_option(opts
);
3551 case QEMU_OPTION_enable_kvm
:
3552 olist
= qemu_find_opts("machine");
3553 qemu_opts_parse(olist
, "accel=kvm", 0);
3555 case QEMU_OPTION_machine
:
3556 olist
= qemu_find_opts("machine");
3557 opts
= qemu_opts_parse(olist
, optarg
, 1);
3561 optarg
= qemu_opt_get(opts
, "type");
3563 machine_class
= machine_parse(optarg
);
3566 case QEMU_OPTION_no_kvm
:
3567 olist
= qemu_find_opts("machine");
3568 qemu_opts_parse(olist
, "accel=tcg", 0);
3570 case QEMU_OPTION_no_kvm_pit
: {
3571 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3575 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3576 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3578 .driver
= "kvm-pit",
3579 .property
= "lost_tick_policy",
3582 { /* end of list */ }
3585 fprintf(stderr
, "Warning: option deprecated, use "
3586 "lost_tick_policy property of kvm-pit instead.\n");
3587 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3590 case QEMU_OPTION_usb
:
3591 olist
= qemu_find_opts("machine");
3592 qemu_opts_parse(olist
, "usb=on", 0);
3594 case QEMU_OPTION_usbdevice
:
3595 olist
= qemu_find_opts("machine");
3596 qemu_opts_parse(olist
, "usb=on", 0);
3597 add_device_config(DEV_USB
, optarg
);
3599 case QEMU_OPTION_device
:
3600 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3604 case QEMU_OPTION_smp
:
3605 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3609 case QEMU_OPTION_vnc
:
3612 vnc_display
= optarg
;
3614 fprintf(stderr
, "VNC support is disabled\n");
3618 case QEMU_OPTION_no_acpi
:
3621 case QEMU_OPTION_no_hpet
:
3624 case QEMU_OPTION_balloon
:
3625 if (balloon_parse(optarg
) < 0) {
3626 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3630 case QEMU_OPTION_no_reboot
:
3633 case QEMU_OPTION_no_shutdown
:
3636 case QEMU_OPTION_show_cursor
:
3639 case QEMU_OPTION_uuid
:
3640 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3641 fprintf(stderr
, "Fail to parse UUID string."
3642 " Wrong format.\n");
3645 qemu_uuid_set
= true;
3647 case QEMU_OPTION_option_rom
:
3648 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3649 fprintf(stderr
, "Too many option ROMs\n");
3652 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3656 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3657 option_rom
[nb_option_roms
].bootindex
=
3658 qemu_opt_get_number(opts
, "bootindex", -1);
3659 if (!option_rom
[nb_option_roms
].name
) {
3660 fprintf(stderr
, "Option ROM file is not specified\n");
3665 case QEMU_OPTION_semihosting
:
3666 semihosting_enabled
= 1;
3668 case QEMU_OPTION_tdf
:
3669 fprintf(stderr
, "Warning: user space PIT time drift fix "
3670 "is no longer supported.\n");
3672 case QEMU_OPTION_name
:
3673 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3678 case QEMU_OPTION_prom_env
:
3679 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3680 fprintf(stderr
, "Too many prom variables\n");
3683 prom_envs
[nb_prom_envs
] = optarg
;
3686 case QEMU_OPTION_old_param
:
3689 case QEMU_OPTION_clock
:
3690 /* Clock options no longer exist. Keep this option for
3691 * backward compatibility.
3694 case QEMU_OPTION_startdate
:
3695 configure_rtc_date_offset(optarg
, 1);
3697 case QEMU_OPTION_rtc
:
3698 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3702 configure_rtc(opts
);
3704 case QEMU_OPTION_tb_size
:
3705 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3706 if (tcg_tb_size
< 0) {
3710 case QEMU_OPTION_icount
:
3711 icount_opts
= qemu_opts_parse(qemu_find_opts("icount"),
3717 case QEMU_OPTION_incoming
:
3719 runstate_set(RUN_STATE_INMIGRATE
);
3721 case QEMU_OPTION_nodefaults
:
3724 case QEMU_OPTION_xen_domid
:
3725 if (!(xen_available())) {
3726 printf("Option %s not supported for this target\n", popt
->name
);
3729 xen_domid
= atoi(optarg
);
3731 case QEMU_OPTION_xen_create
:
3732 if (!(xen_available())) {
3733 printf("Option %s not supported for this target\n", popt
->name
);
3736 xen_mode
= XEN_CREATE
;
3738 case QEMU_OPTION_xen_attach
:
3739 if (!(xen_available())) {
3740 printf("Option %s not supported for this target\n", popt
->name
);
3743 xen_mode
= XEN_ATTACH
;
3745 case QEMU_OPTION_trace
:
3747 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3751 trace_events
= qemu_opt_get(opts
, "events");
3752 trace_file
= qemu_opt_get(opts
, "file");
3755 case QEMU_OPTION_trace_unassigned
:
3756 trace_unassigned
= true;
3758 case QEMU_OPTION_readconfig
:
3760 int ret
= qemu_read_config_file(optarg
);
3762 fprintf(stderr
, "read config %s: %s\n", optarg
,
3768 case QEMU_OPTION_spice
:
3769 olist
= qemu_find_opts("spice");
3771 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3774 opts
= qemu_opts_parse(olist
, optarg
, 0);
3780 case QEMU_OPTION_writeconfig
:
3783 if (strcmp(optarg
, "-") == 0) {
3786 fp
= fopen(optarg
, "w");
3788 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3792 qemu_config_write(fp
);
3798 case QEMU_OPTION_qtest
:
3799 qtest_chrdev
= optarg
;
3801 case QEMU_OPTION_qtest_log
:
3804 case QEMU_OPTION_sandbox
:
3805 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3810 case QEMU_OPTION_add_fd
:
3812 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3817 error_report("File descriptor passing is disabled on this "
3822 case QEMU_OPTION_object
:
3823 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3828 case QEMU_OPTION_realtime
:
3829 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3833 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
3835 case QEMU_OPTION_msg
:
3836 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3840 configure_msg(opts
);
3842 case QEMU_OPTION_dump_vmstate
:
3843 if (vmstate_dump_file
) {
3844 fprintf(stderr
, "qemu: only one '-dump-vmstate' "
3845 "option may be given\n");
3848 vmstate_dump_file
= fopen(optarg
, "w");
3849 if (vmstate_dump_file
== NULL
) {
3850 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3855 os_parse_cmd_args(popt
->index
, optarg
);
3863 if (qemu_init_main_loop(&main_loop_err
)) {
3864 error_report("%s", error_get_pretty(main_loop_err
));
3868 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3872 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3877 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3881 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3886 if (machine_class
== NULL
) {
3887 fprintf(stderr
, "No machine specified, and there is no default.\n"
3888 "Use -machine help to list supported machines!\n");
3892 current_machine
= MACHINE(object_new(object_class_get_name(
3893 OBJECT_CLASS(machine_class
))));
3894 object_property_add_child(object_get_root(), "machine",
3895 OBJECT(current_machine
), &error_abort
);
3896 cpu_exec_init_all();
3898 if (machine_class
->hw_version
) {
3899 qemu_set_version(machine_class
->hw_version
);
3902 /* Init CPU def lists, based on config
3903 * - Must be called after all the qemu_read_config_file() calls
3904 * - Must be called before list_cpus()
3905 * - Must be called before machine->init()
3909 if (cpu_model
&& is_help_option(cpu_model
)) {
3910 list_cpus(stdout
, &fprintf
, cpu_model
);
3914 /* Open the logfile at this point, if necessary. We can't open the logfile
3915 * when encountering either of the logging options (-d or -D) because the
3916 * other one may be encountered later on the command line, changing the
3917 * location or level of logging.
3922 qemu_set_log_filename(log_file
);
3925 mask
= qemu_str_to_log_mask(log_mask
);
3927 qemu_print_log_usage(stdout
);
3933 if (!is_daemonized()) {
3934 if (!trace_init_backends(trace_events
, trace_file
)) {
3939 /* If no data_dir is specified then try to find it relative to the
3941 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3942 data_dir
[data_dir_idx
] = os_find_datadir();
3943 if (data_dir
[data_dir_idx
] != NULL
) {
3947 /* If all else fails use the install path specified when building. */
3948 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3949 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
3952 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
3954 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
3955 if (smp_cpus
> machine_class
->max_cpus
) {
3956 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
3957 "supported by machine `%s' (%d)\n", smp_cpus
,
3958 machine_class
->name
, machine_class
->max_cpus
);
3963 * Get the default machine options from the machine if it is not already
3964 * specified either by the configuration file or by the command line.
3966 if (machine_class
->default_machine_opts
) {
3967 qemu_opts_set_defaults(qemu_find_opts("machine"),
3968 machine_class
->default_machine_opts
, 0);
3971 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
3972 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
3974 if (!vga_model
&& !default_vga
) {
3975 vga_interface_type
= VGA_DEVICE
;
3977 if (!has_defaults
|| machine_class
->no_serial
) {
3980 if (!has_defaults
|| machine_class
->no_parallel
) {
3981 default_parallel
= 0;
3983 if (!has_defaults
|| !machine_class
->use_virtcon
) {
3984 default_virtcon
= 0;
3986 if (!has_defaults
|| !machine_class
->use_sclp
) {
3989 if (!has_defaults
|| machine_class
->no_floppy
) {
3992 if (!has_defaults
|| machine_class
->no_cdrom
) {
3995 if (!has_defaults
|| machine_class
->no_sdcard
) {
3998 if (!has_defaults
) {
3999 default_monitor
= 0;
4004 if (is_daemonized()) {
4005 /* According to documentation and historically, -nographic redirects
4006 * serial port, parallel port and monitor to stdio, which does not work
4007 * with -daemonize. We can redirect these to null instead, but since
4008 * -nographic is legacy, let's just error out.
4009 * We disallow -nographic only if all other ports are not redirected
4010 * explicitly, to not break existing legacy setups which uses
4011 * -nographic _and_ redirects all ports explicitly - this is valid
4012 * usage, -nographic is just a no-op in this case.
4014 if (display_type
== DT_NOGRAPHIC
4015 && (default_parallel
|| default_serial
4016 || default_monitor
|| default_virtcon
)) {
4017 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4020 #ifdef CONFIG_CURSES
4021 if (display_type
== DT_CURSES
) {
4022 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4028 if (display_type
== DT_NOGRAPHIC
) {
4029 if (default_parallel
)
4030 add_device_config(DEV_PARALLEL
, "null");
4031 if (default_serial
&& default_monitor
) {
4032 add_device_config(DEV_SERIAL
, "mon:stdio");
4033 } else if (default_virtcon
&& default_monitor
) {
4034 add_device_config(DEV_VIRTCON
, "mon:stdio");
4035 } else if (default_sclp
&& default_monitor
) {
4036 add_device_config(DEV_SCLP
, "mon:stdio");
4039 add_device_config(DEV_SERIAL
, "stdio");
4040 if (default_virtcon
)
4041 add_device_config(DEV_VIRTCON
, "stdio");
4043 add_device_config(DEV_SCLP
, "stdio");
4045 if (default_monitor
)
4046 monitor_parse("stdio", "readline");
4050 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4051 if (default_parallel
)
4052 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4053 if (default_monitor
)
4054 monitor_parse("vc:80Cx24C", "readline");
4055 if (default_virtcon
)
4056 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4058 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4062 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4063 #if defined(CONFIG_GTK)
4064 display_type
= DT_GTK
;
4065 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4066 display_type
= DT_SDL
;
4067 #elif defined(CONFIG_VNC)
4068 vnc_display
= "localhost:0,to=99";
4071 display_type
= DT_NONE
;
4075 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4076 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4077 "for SDL, ignoring option\n");
4079 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4080 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4081 "ignoring option\n");
4084 #if defined(CONFIG_GTK)
4085 if (display_type
== DT_GTK
) {
4086 early_gtk_display_init();
4092 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4094 #ifdef CONFIG_VIRTFS
4095 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4100 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4101 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
4105 /* store value for the future use */
4106 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4108 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4113 if (qemu_opts_foreach(qemu_find_opts("object"),
4114 object_create
, NULL
, 0) != 0) {
4118 machine_opts
= qemu_get_machine_opts();
4119 if (qemu_opt_foreach(machine_opts
, machine_set_property
, current_machine
,
4121 object_unref(OBJECT(current_machine
));
4125 configure_accelerator(current_machine
);
4128 Error
*local_err
= NULL
;
4129 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4131 error_report("%s", error_get_pretty(local_err
));
4132 error_free(local_err
);
4137 machine_opts
= qemu_get_machine_opts();
4138 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4139 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4140 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4141 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4143 boot_order
= machine_class
->default_boot_order
;
4144 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4146 char *normal_boot_order
;
4147 const char *order
, *once
;
4149 order
= qemu_opt_get(opts
, "order");
4151 validate_bootdevices(order
);
4155 once
= qemu_opt_get(opts
, "once");
4157 validate_bootdevices(once
);
4158 normal_boot_order
= g_strdup(boot_order
);
4160 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4163 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4164 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4167 if (!kernel_cmdline
) {
4168 kernel_cmdline
= "";
4169 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4172 linux_boot
= (kernel_filename
!= NULL
);
4174 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4175 fprintf(stderr
, "-append only allowed with -kernel option\n");
4179 if (!linux_boot
&& initrd_filename
!= NULL
) {
4180 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4184 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4185 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4189 os_set_line_buffering();
4191 qemu_init_cpu_loop();
4192 qemu_mutex_lock_iothread();
4195 /* spice needs the timers to be initialized by this point */
4201 if (kvm_enabled() || xen_enabled()) {
4202 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4205 configure_icount(icount_opts
, &error_abort
);
4206 qemu_opts_del(icount_opts
);
4209 /* clean up network at qemu process termination */
4210 atexit(&net_cleanup
);
4212 if (net_init_clients() < 0) {
4217 if (tpm_init() < 0) {
4222 /* init the bluetooth world */
4223 if (foreach_device_config(DEV_BT
, bt_parse
))
4226 if (!xen_enabled()) {
4227 /* On 32-bit hosts, QEMU is limited by virtual address space */
4228 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4229 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4237 /* If the currently selected machine wishes to override the units-per-bus
4238 * property of its default HBA interface type, do so now. */
4239 if (machine_class
->units_per_default_bus
) {
4240 override_max_devs(machine_class
->block_default_type
,
4241 machine_class
->units_per_default_bus
);
4244 /* open the virtual block devices */
4246 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4247 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4248 &machine_class
->block_default_type
, 1) != 0) {
4252 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4254 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4255 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4257 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4264 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4268 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4270 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4272 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4274 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4277 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4280 /* If no default VGA is requested, the default is "none". */
4282 if (machine_class
->default_display
) {
4283 vga_model
= machine_class
->default_display
;
4284 } else if (cirrus_vga_available()) {
4285 vga_model
= "cirrus";
4286 } else if (vga_available()) {
4291 select_vgahw(vga_model
);
4295 i
= select_watchdog(watchdog
);
4297 exit (i
== 1 ? 1 : 0);
4300 if (machine_class
->compat_props
) {
4301 qdev_prop_register_global_list(machine_class
->compat_props
);
4305 qdev_machine_init();
4307 current_machine
->ram_size
= ram_size
;
4308 current_machine
->maxram_size
= maxram_size
;
4309 current_machine
->ram_slots
= ram_slots
;
4310 current_machine
->boot_order
= boot_order
;
4311 current_machine
->cpu_model
= cpu_model
;
4313 machine_class
->init(current_machine
);
4319 cpu_synchronize_all_post_init();
4323 /* init USB devices */
4324 if (usb_enabled(false)) {
4325 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4329 /* init generic devices */
4330 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4333 /* Did we create any drives that we failed to create a device for? */
4334 drive_check_orphaned();
4336 net_check_clients();
4338 ds
= init_displaystate();
4340 /* init local displays */
4341 switch (display_type
) {
4343 (void)ds
; /* avoid warning if no display is configured */
4345 #if defined(CONFIG_CURSES)
4347 curses_display_init(ds
, full_screen
);
4350 #if defined(CONFIG_SDL)
4352 sdl_display_init(ds
, full_screen
, no_frame
);
4354 #elif defined(CONFIG_COCOA)
4356 cocoa_display_init(ds
, full_screen
);
4359 #if defined(CONFIG_GTK)
4361 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4368 /* must be after terminal init, SDL library changes signal handlers */
4369 os_setup_signal_handling();
4372 /* init remote displays */
4374 Error
*local_err
= NULL
;
4375 vnc_display_init(ds
);
4376 vnc_display_open(ds
, vnc_display
, &local_err
);
4377 if (local_err
!= NULL
) {
4378 error_report("Failed to start VNC server on `%s': %s",
4379 vnc_display
, error_get_pretty(local_err
));
4380 error_free(local_err
);
4384 if (show_vnc_port
) {
4385 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4391 qemu_spice_display_init();
4395 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4399 qdev_machine_creation_done();
4401 if (rom_load_all() != 0) {
4402 fprintf(stderr
, "rom loading failed\n");
4406 /* TODO: once all bus devices are qdevified, this should be done
4407 * when bus is created by qdev.c */
4408 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4409 qemu_run_machine_init_done_notifiers();
4411 /* Done notifiers can load ROMs */
4414 qemu_system_reset(VMRESET_SILENT
);
4416 if (load_vmstate(loadvm
) < 0) {
4421 qdev_prop_check_globals();
4422 if (vmstate_dump_file
) {
4424 dump_vmstate_json_to_file(vmstate_dump_file
);
4429 Error
*local_err
= NULL
;
4430 qemu_start_incoming_migration(incoming
, &local_err
);
4432 error_report("-incoming %s: %s", incoming
,
4433 error_get_pretty(local_err
));
4434 error_free(local_err
);
4437 } else if (autostart
) {
4443 if (is_daemonized()) {
4444 if (!trace_init_backends(trace_events
, trace_file
)) {