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
, char **envp
);
45 int main(int argc
, char **argv
)
47 return qemu_main(argc
, argv
, NULL
);
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"
65 #include "hw/pcmcia.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 "qemu/cache-utils.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
101 #include "fsdev/qemu-fsdev.h"
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir
[16];
129 static int data_dir_idx
;
130 const char *bios_name
= NULL
;
131 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
132 DisplayType display_type
= DT_DEFAULT
;
133 static int display_remote
;
134 const char* keyboard_layout
= NULL
;
136 const char *mem_path
= NULL
;
137 int mem_prealloc
= 0; /* force preallocation of physical target memory */
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
];
182 static bool boot_strict
;
183 uint8_t *boot_splash_filedata
;
184 size_t boot_splash_filedata_size
;
185 uint8_t qemu_extra_params_fw
[2];
187 typedef struct FWBootEntry FWBootEntry
;
190 QTAILQ_ENTRY(FWBootEntry
) link
;
196 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
197 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
200 NodeInfo numa_info
[MAX_NODES
];
202 uint8_t qemu_uuid
[16];
205 static QEMUBootSetHandler
*boot_set_handler
;
206 static void *boot_set_opaque
;
208 static NotifierList exit_notifiers
=
209 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
211 static NotifierList machine_init_done_notifiers
=
212 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
214 static bool tcg_allowed
= true;
217 enum xen_mode xen_mode
= XEN_EMULATE
;
218 static int tcg_tb_size
;
220 static int has_defaults
= 1;
221 static int default_serial
= 1;
222 static int default_parallel
= 1;
223 static int default_virtcon
= 1;
224 static int default_sclp
= 1;
225 static int default_monitor
= 1;
226 static int default_floppy
= 1;
227 static int default_cdrom
= 1;
228 static int default_sdcard
= 1;
229 static int default_vga
= 1;
235 { .driver
= "isa-serial", .flag
= &default_serial
},
236 { .driver
= "isa-parallel", .flag
= &default_parallel
},
237 { .driver
= "isa-fdc", .flag
= &default_floppy
},
238 { .driver
= "ide-cd", .flag
= &default_cdrom
},
239 { .driver
= "ide-hd", .flag
= &default_cdrom
},
240 { .driver
= "ide-drive", .flag
= &default_cdrom
},
241 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
242 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
243 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
244 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
245 { .driver
= "VGA", .flag
= &default_vga
},
246 { .driver
= "isa-vga", .flag
= &default_vga
},
247 { .driver
= "cirrus-vga", .flag
= &default_vga
},
248 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
249 { .driver
= "vmware-svga", .flag
= &default_vga
},
250 { .driver
= "qxl-vga", .flag
= &default_vga
},
253 static QemuOptsList qemu_rtc_opts
= {
255 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
259 .type
= QEMU_OPT_STRING
,
262 .type
= QEMU_OPT_STRING
,
265 .type
= QEMU_OPT_STRING
,
267 { /* end of list */ }
271 static QemuOptsList qemu_sandbox_opts
= {
273 .implied_opt_name
= "enable",
274 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
278 .type
= QEMU_OPT_BOOL
,
280 { /* end of list */ }
284 static QemuOptsList qemu_trace_opts
= {
286 .implied_opt_name
= "trace",
287 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
291 .type
= QEMU_OPT_STRING
,
294 .type
= QEMU_OPT_STRING
,
296 { /* end of list */ }
300 static QemuOptsList qemu_option_rom_opts
= {
301 .name
= "option-rom",
302 .implied_opt_name
= "romfile",
303 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
307 .type
= QEMU_OPT_NUMBER
,
310 .type
= QEMU_OPT_STRING
,
312 { /* end of list */ }
316 static QemuOptsList qemu_machine_opts
= {
318 .implied_opt_name
= "type",
320 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
324 .type
= QEMU_OPT_STRING
,
325 .help
= "emulated machine"
328 .type
= QEMU_OPT_STRING
,
329 .help
= "accelerator list",
331 .name
= "kernel_irqchip",
332 .type
= QEMU_OPT_BOOL
,
333 .help
= "use KVM in-kernel irqchip",
335 .name
= "kvm_shadow_mem",
336 .type
= QEMU_OPT_SIZE
,
337 .help
= "KVM shadow MMU size",
340 .type
= QEMU_OPT_STRING
,
341 .help
= "Linux kernel image file",
344 .type
= QEMU_OPT_STRING
,
345 .help
= "Linux initial ramdisk file",
348 .type
= QEMU_OPT_STRING
,
349 .help
= "Linux kernel command line",
352 .type
= QEMU_OPT_STRING
,
353 .help
= "Linux kernel device tree file",
356 .type
= QEMU_OPT_STRING
,
357 .help
= "Dump current dtb to a file and quit",
359 .name
= "phandle_start",
360 .type
= QEMU_OPT_NUMBER
,
361 .help
= "The first phandle ID we may generate dynamically",
363 .name
= "dt_compatible",
364 .type
= QEMU_OPT_STRING
,
365 .help
= "Overrides the \"compatible\" property of the dt root node",
367 .name
= "dump-guest-core",
368 .type
= QEMU_OPT_BOOL
,
369 .help
= "Include guest memory in a core dump",
372 .type
= QEMU_OPT_BOOL
,
373 .help
= "enable/disable memory merge support",
376 .type
= QEMU_OPT_BOOL
,
377 .help
= "Set on/off to enable/disable usb",
380 .type
= QEMU_OPT_STRING
,
381 .help
= "firmware image",
384 .type
= QEMU_OPT_STRING
,
385 .help
= "Specifies the KVM virtualization mode (HV, PR)",
387 { /* End of list */ }
391 static QemuOptsList qemu_boot_opts
= {
393 .implied_opt_name
= "order",
395 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
399 .type
= QEMU_OPT_STRING
,
402 .type
= QEMU_OPT_STRING
,
405 .type
= QEMU_OPT_BOOL
,
408 .type
= QEMU_OPT_STRING
,
410 .name
= "splash-time",
411 .type
= QEMU_OPT_STRING
,
413 .name
= "reboot-timeout",
414 .type
= QEMU_OPT_STRING
,
417 .type
= QEMU_OPT_BOOL
,
423 static QemuOptsList qemu_add_fd_opts
= {
425 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
429 .type
= QEMU_OPT_NUMBER
,
430 .help
= "file descriptor of which a duplicate is added to fd set",
433 .type
= QEMU_OPT_NUMBER
,
434 .help
= "ID of the fd set to add fd to",
437 .type
= QEMU_OPT_STRING
,
438 .help
= "free-form string used to describe fd",
440 { /* end of list */ }
444 static QemuOptsList qemu_object_opts
= {
446 .implied_opt_name
= "qom-type",
447 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
453 static QemuOptsList qemu_tpmdev_opts
= {
455 .implied_opt_name
= "type",
456 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
458 /* options are defined in the TPM backends */
459 { /* end of list */ }
463 static QemuOptsList qemu_realtime_opts
= {
465 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
469 .type
= QEMU_OPT_BOOL
,
471 { /* end of list */ }
475 static QemuOptsList qemu_msg_opts
= {
477 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
481 .type
= QEMU_OPT_BOOL
,
483 { /* end of list */ }
487 static QemuOptsList qemu_name_opts
= {
489 .implied_opt_name
= "guest",
491 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
495 .type
= QEMU_OPT_STRING
,
496 .help
= "Sets the name of the guest.\n"
497 "This name will be displayed in the SDL window caption.\n"
498 "The name will also be used for the VNC server",
501 .type
= QEMU_OPT_STRING
,
502 .help
= "Sets the name of the QEMU process, as shown in top etc",
504 .name
= "debug-threads",
505 .type
= QEMU_OPT_BOOL
,
506 .help
= "When enabled, name the individual threads; defaults off.\n"
507 "NOTE: The thread names are for debugging and not a\n"
510 { /* End of list */ }
514 static QemuOptsList qemu_mem_opts
= {
516 .implied_opt_name
= "size",
517 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
522 .type
= QEMU_OPT_SIZE
,
526 .type
= QEMU_OPT_NUMBER
,
530 .type
= QEMU_OPT_SIZE
,
532 { /* end of list */ }
537 * Get machine options
539 * Returns: machine options (never null).
541 QemuOpts
*qemu_get_machine_opts(void)
543 return qemu_find_opts_singleton("machine");
546 const char *qemu_get_vm_name(void)
551 static void res_free(void)
553 if (boot_splash_filedata
!= NULL
) {
554 g_free(boot_splash_filedata
);
555 boot_splash_filedata
= NULL
;
559 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
561 const char *driver
= qemu_opt_get(opts
, "driver");
566 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
567 if (strcmp(default_list
[i
].driver
, driver
) != 0)
569 *(default_list
[i
].flag
) = 0;
574 /***********************************************************/
577 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
579 /* We use RUN_STATE_MAX but any invalid value will do */
580 static RunState vmstop_requested
= RUN_STATE_MAX
;
581 static QemuMutex vmstop_lock
;
586 } RunStateTransition
;
588 static const RunStateTransition runstate_transitions_def
[] = {
590 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
591 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
593 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
594 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
596 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
597 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
599 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
600 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
603 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
605 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
606 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
608 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
609 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
610 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
612 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
613 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
615 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
617 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
618 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
619 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
620 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
621 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
622 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
623 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
624 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
625 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
626 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
628 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
630 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
631 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
633 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
634 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
635 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
636 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
638 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
639 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
641 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
642 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
644 { RUN_STATE_MAX
, RUN_STATE_MAX
},
647 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
649 bool runstate_check(RunState state
)
651 return current_run_state
== state
;
654 static void runstate_init(void)
656 const RunStateTransition
*p
;
658 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
659 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
660 runstate_valid_transitions
[p
->from
][p
->to
] = true;
663 qemu_mutex_init(&vmstop_lock
);
666 /* This function will abort() on invalid state transitions */
667 void runstate_set(RunState new_state
)
669 assert(new_state
< RUN_STATE_MAX
);
671 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
672 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
673 RunState_lookup
[current_run_state
],
674 RunState_lookup
[new_state
]);
677 trace_runstate_set(new_state
);
678 current_run_state
= new_state
;
681 int runstate_is_running(void)
683 return runstate_check(RUN_STATE_RUNNING
);
686 bool runstate_needs_reset(void)
688 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
689 runstate_check(RUN_STATE_SHUTDOWN
);
692 StatusInfo
*qmp_query_status(Error
**errp
)
694 StatusInfo
*info
= g_malloc0(sizeof(*info
));
696 info
->running
= runstate_is_running();
697 info
->singlestep
= singlestep
;
698 info
->status
= current_run_state
;
703 static bool qemu_vmstop_requested(RunState
*r
)
705 qemu_mutex_lock(&vmstop_lock
);
706 *r
= vmstop_requested
;
707 vmstop_requested
= RUN_STATE_MAX
;
708 qemu_mutex_unlock(&vmstop_lock
);
709 return *r
< RUN_STATE_MAX
;
712 void qemu_system_vmstop_request_prepare(void)
714 qemu_mutex_lock(&vmstop_lock
);
717 void qemu_system_vmstop_request(RunState state
)
719 vmstop_requested
= state
;
720 qemu_mutex_unlock(&vmstop_lock
);
728 qemu_vmstop_requested(&requested
);
729 if (runstate_is_running() && requested
== RUN_STATE_MAX
) {
733 /* Ensure that a STOP/RESUME pair of events is emitted if a
734 * vmstop request was pending. The BLOCK_IO_ERROR event, for
735 * example, according to documentation is always followed by
738 if (runstate_is_running()) {
739 qapi_event_send_stop(&error_abort
);
742 runstate_set(RUN_STATE_RUNNING
);
743 vm_state_notify(1, RUN_STATE_RUNNING
);
747 qapi_event_send_resume(&error_abort
);
751 /***********************************************************/
752 /* real time host monotonic timer */
754 /***********************************************************/
755 /* host time/date access */
756 void qemu_get_timedate(struct tm
*tm
, int offset
)
762 if (rtc_date_offset
== -1) {
766 localtime_r(&ti
, tm
);
768 ti
-= rtc_date_offset
;
773 int qemu_timedate_diff(struct tm
*tm
)
777 if (rtc_date_offset
== -1)
779 seconds
= mktimegm(tm
);
782 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
783 seconds
= mktime(&tmp
);
786 seconds
= mktimegm(tm
) + rtc_date_offset
;
788 return seconds
- time(NULL
);
791 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
793 time_t rtc_start_date
;
796 if (!strcmp(startdate
, "now") && legacy
) {
797 rtc_date_offset
= -1;
799 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
807 } else if (sscanf(startdate
, "%d-%d-%d",
819 rtc_start_date
= mktimegm(&tm
);
820 if (rtc_start_date
== -1) {
822 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
823 "'2006-06-17T16:01:21' or '2006-06-17'\n");
826 rtc_date_offset
= time(NULL
) - rtc_start_date
;
830 static void configure_rtc(QemuOpts
*opts
)
834 value
= qemu_opt_get(opts
, "base");
836 if (!strcmp(value
, "utc")) {
838 } else if (!strcmp(value
, "localtime")) {
841 configure_rtc_date_offset(value
, 0);
844 value
= qemu_opt_get(opts
, "clock");
846 if (!strcmp(value
, "host")) {
847 rtc_clock
= QEMU_CLOCK_HOST
;
848 } else if (!strcmp(value
, "rt")) {
849 rtc_clock
= QEMU_CLOCK_REALTIME
;
850 } else if (!strcmp(value
, "vm")) {
851 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
853 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
857 value
= qemu_opt_get(opts
, "driftfix");
859 if (!strcmp(value
, "slew")) {
860 static GlobalProperty slew_lost_ticks
[] = {
862 .driver
= "mc146818rtc",
863 .property
= "lost_tick_policy",
866 { /* end of list */ }
869 qdev_prop_register_global_list(slew_lost_ticks
);
870 } else if (!strcmp(value
, "none")) {
871 /* discard is default */
873 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
879 /***********************************************************/
880 /* Bluetooth support */
883 static struct HCIInfo
*hci_table
[MAX_NICS
];
885 struct HCIInfo
*qemu_next_hci(void)
887 if (cur_hci
== nb_hcis
)
890 return hci_table
[cur_hci
++];
893 static int bt_hci_parse(const char *str
)
898 if (nb_hcis
>= MAX_NICS
) {
899 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
912 bdaddr
.b
[5] = 0x56 + nb_hcis
;
913 hci
->bdaddr_set(hci
, bdaddr
.b
);
915 hci_table
[nb_hcis
++] = hci
;
920 static void bt_vhci_add(int vlan_id
)
922 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
925 fprintf(stderr
, "qemu: warning: adding a VHCI to "
926 "an empty scatternet %i\n", vlan_id
);
928 bt_vhci_init(bt_new_hci(vlan
));
931 static struct bt_device_s
*bt_device_add(const char *opt
)
933 struct bt_scatternet_s
*vlan
;
935 char *endp
= strstr(opt
, ",vlan=");
936 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
939 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
942 vlan_id
= strtol(endp
+ 6, &endp
, 0);
944 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
949 vlan
= qemu_find_bt_vlan(vlan_id
);
952 fprintf(stderr
, "qemu: warning: adding a slave device to "
953 "an empty scatternet %i\n", vlan_id
);
955 if (!strcmp(devname
, "keyboard"))
956 return bt_keyboard_init(vlan
);
958 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
962 static int bt_parse(const char *opt
)
964 const char *endp
, *p
;
967 if (strstart(opt
, "hci", &endp
)) {
968 if (!*endp
|| *endp
== ',') {
970 if (!strstart(endp
, ",vlan=", 0))
973 return bt_hci_parse(opt
);
975 } else if (strstart(opt
, "vhci", &endp
)) {
976 if (!*endp
|| *endp
== ',') {
978 if (strstart(endp
, ",vlan=", &p
)) {
979 vlan
= strtol(p
, (char **) &endp
, 0);
981 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
985 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
994 } else if (strstart(opt
, "device:", &endp
))
995 return !bt_device_add(endp
);
997 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1001 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
1003 /* FIXME: change this to true for 1.3 */
1004 if (qemu_opt_get_bool(opts
, "enable", false)) {
1005 #ifdef CONFIG_SECCOMP
1006 if (seccomp_start() < 0) {
1007 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1008 "failed to install seccomp syscall filter in the kernel");
1012 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1013 "sandboxing request but seccomp is not compiled into this build");
1021 static int parse_name(QemuOpts
*opts
, void *opaque
)
1023 const char *proc_name
;
1025 if (qemu_opt_get(opts
, "debug-threads")) {
1026 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
1028 qemu_name
= qemu_opt_get(opts
, "guest");
1030 proc_name
= qemu_opt_get(opts
, "process");
1032 os_set_proc_name(proc_name
);
1038 bool usb_enabled(bool default_usb
)
1040 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1041 has_defaults
&& default_usb
);
1045 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1047 int fd
, dupfd
, flags
;
1049 const char *fd_opaque
= NULL
;
1051 fd
= qemu_opt_get_number(opts
, "fd", -1);
1052 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1053 fd_opaque
= qemu_opt_get(opts
, "opaque");
1056 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1057 "fd option is required and must be non-negative");
1061 if (fd
<= STDERR_FILENO
) {
1062 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1063 "fd cannot be a standard I/O stream");
1068 * All fds inherited across exec() necessarily have FD_CLOEXEC
1069 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1071 flags
= fcntl(fd
, F_GETFD
);
1072 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1073 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1074 "fd is not valid or already in use");
1079 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1080 "set option is required and must be non-negative");
1084 #ifdef F_DUPFD_CLOEXEC
1085 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1089 qemu_set_cloexec(dupfd
);
1093 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1094 "Error duplicating fd: %s", strerror(errno
));
1098 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1099 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1105 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1109 fd
= qemu_opt_get_number(opts
, "fd", -1);
1116 /***********************************************************/
1117 /* QEMU Block devices */
1119 #define HD_OPTS "media=disk"
1120 #define CDROM_OPTS "media=cdrom"
1122 #define PFLASH_OPTS ""
1126 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1128 BlockInterfaceType
*block_default_type
= opaque
;
1130 return drive_new(opts
, *block_default_type
) == NULL
;
1133 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1135 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1136 qemu_opt_set(opts
, "snapshot", "on");
1141 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1142 int index
, const char *optstr
)
1146 if (!enable
|| drive_get_by_index(type
, index
)) {
1150 opts
= drive_add(type
, index
, NULL
, optstr
);
1152 drive_enable_snapshot(opts
, NULL
);
1154 if (!drive_new(opts
, type
)) {
1159 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1161 boot_set_handler
= func
;
1162 boot_set_opaque
= opaque
;
1165 int qemu_boot_set(const char *boot_order
)
1167 if (!boot_set_handler
) {
1170 return boot_set_handler(boot_set_opaque
, boot_order
);
1173 static void validate_bootdevices(const char *devices
)
1175 /* We just do some generic consistency checks */
1179 for (p
= devices
; *p
!= '\0'; p
++) {
1180 /* Allowed boot devices are:
1181 * a-b: floppy disk drives
1182 * c-f: IDE disk drives
1183 * g-m: machine implementation dependent drives
1184 * n-p: network devices
1185 * It's up to each machine implementation to check if the given boot
1186 * devices match the actual hardware implementation and firmware
1189 if (*p
< 'a' || *p
> 'p') {
1190 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1193 if (bitmap
& (1 << (*p
- 'a'))) {
1194 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1197 bitmap
|= 1 << (*p
- 'a');
1201 static void restore_boot_order(void *opaque
)
1203 char *normal_boot_order
= opaque
;
1204 static int first
= 1;
1206 /* Restore boot order and remove ourselves after the first boot */
1212 qemu_boot_set(normal_boot_order
);
1214 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1215 g_free(normal_boot_order
);
1218 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1221 FWBootEntry
*node
, *i
;
1223 if (bootindex
< 0) {
1227 assert(dev
!= NULL
|| suffix
!= NULL
);
1229 node
= g_malloc0(sizeof(FWBootEntry
));
1230 node
->bootindex
= bootindex
;
1231 node
->suffix
= g_strdup(suffix
);
1234 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1235 if (i
->bootindex
== bootindex
) {
1236 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1238 } else if (i
->bootindex
< bootindex
) {
1241 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1244 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1247 DeviceState
*get_boot_device(uint32_t position
)
1249 uint32_t counter
= 0;
1250 FWBootEntry
*i
= NULL
;
1251 DeviceState
*res
= NULL
;
1253 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1254 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1255 if (counter
== position
) {
1266 * This function returns null terminated string that consist of new line
1267 * separated device paths.
1269 * memory pointed by "size" is assigned total length of the array in bytes
1272 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1278 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1279 char *devpath
= NULL
, *bootpath
;
1283 devpath
= qdev_get_fw_dev_path(i
->dev
);
1287 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1288 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1290 bootpath
= g_malloc(bootpathlen
);
1291 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1293 } else if (devpath
) {
1295 } else if (!ignore_suffixes
) {
1297 bootpath
= g_strdup(i
->suffix
);
1299 bootpath
= g_strdup("");
1303 list
[total
-1] = '\n';
1305 len
= strlen(bootpath
) + 1;
1306 list
= g_realloc(list
, total
+ len
);
1307 memcpy(&list
[total
], bootpath
, len
);
1314 if (boot_strict
&& *size
> 0) {
1315 list
[total
-1] = '\n';
1316 list
= g_realloc(list
, total
+ 5);
1317 memcpy(&list
[total
], "HALT", 5);
1323 static QemuOptsList qemu_smp_opts
= {
1325 .implied_opt_name
= "cpus",
1326 .merge_lists
= true,
1327 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1331 .type
= QEMU_OPT_NUMBER
,
1334 .type
= QEMU_OPT_NUMBER
,
1337 .type
= QEMU_OPT_NUMBER
,
1340 .type
= QEMU_OPT_NUMBER
,
1343 .type
= QEMU_OPT_NUMBER
,
1345 { /*End of list */ }
1349 static void smp_parse(QemuOpts
*opts
)
1353 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1354 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1355 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1356 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1358 /* compute missing values, prefer sockets over cores over threads */
1359 if (cpus
== 0 || sockets
== 0) {
1360 sockets
= sockets
> 0 ? sockets
: 1;
1361 cores
= cores
> 0 ? cores
: 1;
1362 threads
= threads
> 0 ? threads
: 1;
1364 cpus
= cores
* threads
* sockets
;
1368 threads
= threads
> 0 ? threads
: 1;
1369 cores
= cpus
/ (sockets
* threads
);
1371 threads
= cpus
/ (cores
* sockets
);
1375 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1378 smp_cores
= cores
> 0 ? cores
: 1;
1379 smp_threads
= threads
> 0 ? threads
: 1;
1383 if (max_cpus
== 0) {
1384 max_cpus
= smp_cpus
;
1387 if (max_cpus
> MAX_CPUMASK_BITS
) {
1388 fprintf(stderr
, "Unsupported number of maxcpus\n");
1391 if (max_cpus
< smp_cpus
) {
1392 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1398 static void configure_realtime(QemuOpts
*opts
)
1402 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1405 if (os_mlock() < 0) {
1406 fprintf(stderr
, "qemu: locking memory failed\n");
1413 static void configure_msg(QemuOpts
*opts
)
1415 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1418 /***********************************************************/
1421 static int usb_device_add(const char *devname
)
1423 USBDevice
*dev
= NULL
;
1424 #ifndef CONFIG_LINUX
1428 if (!usb_enabled(false)) {
1432 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1433 dev
= usbdevice_create(devname
);
1437 /* the other ones */
1438 #ifndef CONFIG_LINUX
1439 /* only the linux version is qdev-ified, usb-bsd still needs this */
1440 if (strstart(devname
, "host:", &p
)) {
1441 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1451 static int usb_device_del(const char *devname
)
1456 if (strstart(devname
, "host:", &p
)) {
1460 if (!usb_enabled(false)) {
1464 p
= strchr(devname
, '.');
1467 bus_num
= strtoul(devname
, NULL
, 0);
1468 addr
= strtoul(p
+ 1, NULL
, 0);
1470 return usb_device_delete_addr(bus_num
, addr
);
1473 static int usb_parse(const char *cmdline
)
1476 r
= usb_device_add(cmdline
);
1478 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1483 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1485 const char *devname
= qdict_get_str(qdict
, "devname");
1486 if (usb_device_add(devname
) < 0) {
1487 error_report("could not add USB device '%s'", devname
);
1491 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1493 const char *devname
= qdict_get_str(qdict
, "devname");
1494 if (usb_device_del(devname
) < 0) {
1495 error_report("could not delete USB device '%s'", devname
);
1499 /***********************************************************/
1500 /* PCMCIA/Cardbus */
1502 static struct pcmcia_socket_entry_s
{
1503 PCMCIASocket
*socket
;
1504 struct pcmcia_socket_entry_s
*next
;
1505 } *pcmcia_sockets
= 0;
1507 void pcmcia_socket_register(PCMCIASocket
*socket
)
1509 struct pcmcia_socket_entry_s
*entry
;
1511 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1512 entry
->socket
= socket
;
1513 entry
->next
= pcmcia_sockets
;
1514 pcmcia_sockets
= entry
;
1517 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1519 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1521 ptr
= &pcmcia_sockets
;
1522 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1523 if (entry
->socket
== socket
) {
1529 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1531 struct pcmcia_socket_entry_s
*iter
;
1533 if (!pcmcia_sockets
)
1534 monitor_printf(mon
, "No PCMCIA sockets\n");
1536 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1537 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1538 iter
->socket
->attached
? iter
->socket
->card_string
:
1542 /***********************************************************/
1543 /* machine registration */
1545 MachineState
*current_machine
;
1547 static void machine_class_init(ObjectClass
*oc
, void *data
)
1549 MachineClass
*mc
= MACHINE_CLASS(oc
);
1550 QEMUMachine
*qm
= data
;
1552 mc
->name
= qm
->name
;
1553 mc
->alias
= qm
->alias
;
1554 mc
->desc
= qm
->desc
;
1555 mc
->init
= qm
->init
;
1556 mc
->reset
= qm
->reset
;
1557 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1558 mc
->kvm_type
= qm
->kvm_type
;
1559 mc
->block_default_type
= qm
->block_default_type
;
1560 mc
->max_cpus
= qm
->max_cpus
;
1561 mc
->no_serial
= qm
->no_serial
;
1562 mc
->no_parallel
= qm
->no_parallel
;
1563 mc
->use_virtcon
= qm
->use_virtcon
;
1564 mc
->use_sclp
= qm
->use_sclp
;
1565 mc
->no_floppy
= qm
->no_floppy
;
1566 mc
->no_cdrom
= qm
->no_cdrom
;
1567 mc
->no_sdcard
= qm
->no_sdcard
;
1568 mc
->is_default
= qm
->is_default
;
1569 mc
->default_machine_opts
= qm
->default_machine_opts
;
1570 mc
->default_boot_order
= qm
->default_boot_order
;
1571 mc
->compat_props
= qm
->compat_props
;
1572 mc
->hw_version
= qm
->hw_version
;
1575 int qemu_register_machine(QEMUMachine
*m
)
1577 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1580 .parent
= TYPE_MACHINE
,
1581 .class_init
= machine_class_init
,
1582 .class_data
= (void *)m
,
1591 static MachineClass
*find_machine(const char *name
)
1593 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1594 MachineClass
*mc
= NULL
;
1596 for (el
= machines
; el
; el
= el
->next
) {
1597 MachineClass
*temp
= el
->data
;
1599 if (!strcmp(temp
->name
, name
)) {
1604 !strcmp(temp
->alias
, name
)) {
1610 g_slist_free(machines
);
1614 MachineClass
*find_default_machine(void)
1616 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1617 MachineClass
*mc
= NULL
;
1619 for (el
= machines
; el
; el
= el
->next
) {
1620 MachineClass
*temp
= el
->data
;
1622 if (temp
->is_default
) {
1628 g_slist_free(machines
);
1632 MachineInfoList
*qmp_query_machines(Error
**errp
)
1634 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1635 MachineInfoList
*mach_list
= NULL
;
1637 for (el
= machines
; el
; el
= el
->next
) {
1638 MachineClass
*mc
= el
->data
;
1639 MachineInfoList
*entry
;
1642 info
= g_malloc0(sizeof(*info
));
1643 if (mc
->is_default
) {
1644 info
->has_is_default
= true;
1645 info
->is_default
= true;
1649 info
->has_alias
= true;
1650 info
->alias
= g_strdup(mc
->alias
);
1653 info
->name
= g_strdup(mc
->name
);
1654 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1656 entry
= g_malloc0(sizeof(*entry
));
1657 entry
->value
= info
;
1658 entry
->next
= mach_list
;
1662 g_slist_free(machines
);
1666 /***********************************************************/
1667 /* main execution loop */
1669 struct vm_change_state_entry
{
1670 VMChangeStateHandler
*cb
;
1672 QLIST_ENTRY (vm_change_state_entry
) entries
;
1675 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1677 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1680 VMChangeStateEntry
*e
;
1682 e
= g_malloc0(sizeof (*e
));
1686 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1690 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1692 QLIST_REMOVE (e
, entries
);
1696 void vm_state_notify(int running
, RunState state
)
1698 VMChangeStateEntry
*e
;
1700 trace_vm_state_notify(running
, state
);
1702 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1703 e
->cb(e
->opaque
, running
, state
);
1707 /* reset/shutdown handler */
1709 typedef struct QEMUResetEntry
{
1710 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1711 QEMUResetHandler
*func
;
1715 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1716 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1717 static int reset_requested
;
1718 static int shutdown_requested
, shutdown_signal
= -1;
1719 static pid_t shutdown_pid
;
1720 static int powerdown_requested
;
1721 static int debug_requested
;
1722 static int suspend_requested
;
1723 static WakeupReason wakeup_reason
;
1724 static NotifierList powerdown_notifiers
=
1725 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1726 static NotifierList suspend_notifiers
=
1727 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1728 static NotifierList wakeup_notifiers
=
1729 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1730 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1732 int qemu_shutdown_requested_get(void)
1734 return shutdown_requested
;
1737 int qemu_reset_requested_get(void)
1739 return reset_requested
;
1742 static int qemu_shutdown_requested(void)
1744 int r
= shutdown_requested
;
1745 shutdown_requested
= 0;
1749 static void qemu_kill_report(void)
1751 if (!qtest_driver() && shutdown_signal
!= -1) {
1752 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1753 if (shutdown_pid
== 0) {
1754 /* This happens for eg ^C at the terminal, so it's worth
1755 * avoiding printing an odd message in that case.
1757 fputc('\n', stderr
);
1759 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1761 shutdown_signal
= -1;
1765 static int qemu_reset_requested(void)
1767 int r
= reset_requested
;
1768 reset_requested
= 0;
1772 static int qemu_suspend_requested(void)
1774 int r
= suspend_requested
;
1775 suspend_requested
= 0;
1779 static WakeupReason
qemu_wakeup_requested(void)
1781 return wakeup_reason
;
1784 static int qemu_powerdown_requested(void)
1786 int r
= powerdown_requested
;
1787 powerdown_requested
= 0;
1791 static int qemu_debug_requested(void)
1793 int r
= debug_requested
;
1794 debug_requested
= 0;
1798 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1800 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1803 re
->opaque
= opaque
;
1804 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1807 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1811 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1812 if (re
->func
== func
&& re
->opaque
== opaque
) {
1813 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1820 void qemu_devices_reset(void)
1822 QEMUResetEntry
*re
, *nre
;
1824 /* reset all devices */
1825 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1826 re
->func(re
->opaque
);
1830 void qemu_system_reset(bool report
)
1834 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1836 if (mc
&& mc
->reset
) {
1839 qemu_devices_reset();
1842 qapi_event_send_reset(&error_abort
);
1844 cpu_synchronize_all_post_reset();
1847 void qemu_system_reset_request(void)
1850 shutdown_requested
= 1;
1852 reset_requested
= 1;
1855 qemu_notify_event();
1858 static void qemu_system_suspend(void)
1861 notifier_list_notify(&suspend_notifiers
, NULL
);
1862 runstate_set(RUN_STATE_SUSPENDED
);
1863 qapi_event_send_suspend(&error_abort
);
1866 void qemu_system_suspend_request(void)
1868 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1871 suspend_requested
= 1;
1873 qemu_notify_event();
1876 void qemu_register_suspend_notifier(Notifier
*notifier
)
1878 notifier_list_add(&suspend_notifiers
, notifier
);
1881 void qemu_system_wakeup_request(WakeupReason reason
)
1883 trace_system_wakeup_request(reason
);
1885 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1888 if (!(wakeup_reason_mask
& (1 << reason
))) {
1891 runstate_set(RUN_STATE_RUNNING
);
1892 wakeup_reason
= reason
;
1893 qemu_notify_event();
1896 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1899 wakeup_reason_mask
|= (1 << reason
);
1901 wakeup_reason_mask
&= ~(1 << reason
);
1905 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1907 notifier_list_add(&wakeup_notifiers
, notifier
);
1910 void qemu_system_killed(int signal
, pid_t pid
)
1912 shutdown_signal
= signal
;
1915 qemu_system_shutdown_request();
1918 void qemu_system_shutdown_request(void)
1920 shutdown_requested
= 1;
1921 qemu_notify_event();
1924 static void qemu_system_powerdown(void)
1926 qapi_event_send_powerdown(&error_abort
);
1927 notifier_list_notify(&powerdown_notifiers
, NULL
);
1930 void qemu_system_powerdown_request(void)
1932 powerdown_requested
= 1;
1933 qemu_notify_event();
1936 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1938 notifier_list_add(&powerdown_notifiers
, notifier
);
1941 void qemu_system_debug_request(void)
1943 debug_requested
= 1;
1944 qemu_notify_event();
1947 static bool main_loop_should_exit(void)
1950 if (qemu_debug_requested()) {
1951 vm_stop(RUN_STATE_DEBUG
);
1953 if (qemu_suspend_requested()) {
1954 qemu_system_suspend();
1956 if (qemu_shutdown_requested()) {
1958 qapi_event_send_shutdown(&error_abort
);
1960 vm_stop(RUN_STATE_SHUTDOWN
);
1965 if (qemu_reset_requested()) {
1967 cpu_synchronize_all_states();
1968 qemu_system_reset(VMRESET_REPORT
);
1970 if (runstate_needs_reset()) {
1971 runstate_set(RUN_STATE_PAUSED
);
1974 if (qemu_wakeup_requested()) {
1976 cpu_synchronize_all_states();
1977 qemu_system_reset(VMRESET_SILENT
);
1978 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1979 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1981 qapi_event_send_wakeup(&error_abort
);
1983 if (qemu_powerdown_requested()) {
1984 qemu_system_powerdown();
1986 if (qemu_vmstop_requested(&r
)) {
1992 static void main_loop(void)
1996 #ifdef CONFIG_PROFILER
2000 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
2001 #ifdef CONFIG_PROFILER
2002 ti
= profile_getclock();
2004 last_io
= main_loop_wait(nonblocking
);
2005 #ifdef CONFIG_PROFILER
2006 dev_time
+= profile_getclock() - ti
;
2008 } while (!main_loop_should_exit());
2011 static void version(void)
2013 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2016 static void help(int exitcode
)
2019 printf("usage: %s [options] [disk_image]\n\n"
2020 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2021 error_get_progname());
2023 #define QEMU_OPTIONS_GENERATE_HELP
2024 #include "qemu-options-wrapper.h"
2026 printf("\nDuring emulation, the following keys are useful:\n"
2027 "ctrl-alt-f toggle full screen\n"
2028 "ctrl-alt-n switch to virtual console 'n'\n"
2029 "ctrl-alt toggle mouse and keyboard grab\n"
2031 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2036 #define HAS_ARG 0x0001
2038 typedef struct QEMUOption
{
2045 static const QEMUOption qemu_options
[] = {
2046 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2047 #define QEMU_OPTIONS_GENERATE_OPTIONS
2048 #include "qemu-options-wrapper.h"
2052 static bool vga_available(void)
2054 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2057 static bool cirrus_vga_available(void)
2059 return object_class_by_name("cirrus-vga")
2060 || object_class_by_name("isa-cirrus-vga");
2063 static bool vmware_vga_available(void)
2065 return object_class_by_name("vmware-svga");
2068 static bool qxl_vga_available(void)
2070 return object_class_by_name("qxl-vga");
2073 static bool tcx_vga_available(void)
2075 return object_class_by_name("SUNW,tcx");
2078 static bool cg3_vga_available(void)
2080 return object_class_by_name("cgthree");
2083 static void select_vgahw (const char *p
)
2087 assert(vga_interface_type
== VGA_NONE
);
2088 if (strstart(p
, "std", &opts
)) {
2089 if (vga_available()) {
2090 vga_interface_type
= VGA_STD
;
2092 fprintf(stderr
, "Error: standard VGA not available\n");
2095 } else if (strstart(p
, "cirrus", &opts
)) {
2096 if (cirrus_vga_available()) {
2097 vga_interface_type
= VGA_CIRRUS
;
2099 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2102 } else if (strstart(p
, "vmware", &opts
)) {
2103 if (vmware_vga_available()) {
2104 vga_interface_type
= VGA_VMWARE
;
2106 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2109 } else if (strstart(p
, "xenfb", &opts
)) {
2110 vga_interface_type
= VGA_XENFB
;
2111 } else if (strstart(p
, "qxl", &opts
)) {
2112 if (qxl_vga_available()) {
2113 vga_interface_type
= VGA_QXL
;
2115 fprintf(stderr
, "Error: QXL VGA not available\n");
2118 } else if (strstart(p
, "tcx", &opts
)) {
2119 if (tcx_vga_available()) {
2120 vga_interface_type
= VGA_TCX
;
2122 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2125 } else if (strstart(p
, "cg3", &opts
)) {
2126 if (cg3_vga_available()) {
2127 vga_interface_type
= VGA_CG3
;
2129 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2132 } else if (!strstart(p
, "none", &opts
)) {
2134 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2138 const char *nextopt
;
2140 if (strstart(opts
, ",retrace=", &nextopt
)) {
2142 if (strstart(opts
, "dumb", &nextopt
))
2143 vga_retrace_method
= VGA_RETRACE_DUMB
;
2144 else if (strstart(opts
, "precise", &nextopt
))
2145 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2146 else goto invalid_vga
;
2147 } else goto invalid_vga
;
2152 static DisplayType
select_display(const char *p
)
2155 DisplayType display
= DT_DEFAULT
;
2157 if (strstart(p
, "sdl", &opts
)) {
2161 const char *nextopt
;
2163 if (strstart(opts
, ",frame=", &nextopt
)) {
2165 if (strstart(opts
, "on", &nextopt
)) {
2167 } else if (strstart(opts
, "off", &nextopt
)) {
2170 goto invalid_sdl_args
;
2172 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2174 if (strstart(opts
, "on", &nextopt
)) {
2176 } else if (strstart(opts
, "off", &nextopt
)) {
2179 goto invalid_sdl_args
;
2181 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2183 if (strstart(opts
, "on", &nextopt
)) {
2185 } else if (strstart(opts
, "off", &nextopt
)) {
2188 goto invalid_sdl_args
;
2190 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2192 if (strstart(opts
, "on", &nextopt
)) {
2194 } else if (strstart(opts
, "off", &nextopt
)) {
2197 goto invalid_sdl_args
;
2201 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2207 fprintf(stderr
, "SDL support is disabled\n");
2210 } else if (strstart(p
, "vnc", &opts
)) {
2215 const char *nextopt
;
2217 if (strstart(opts
, "=", &nextopt
)) {
2218 vnc_display
= nextopt
;
2222 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2226 fprintf(stderr
, "VNC support is disabled\n");
2229 } else if (strstart(p
, "curses", &opts
)) {
2230 #ifdef CONFIG_CURSES
2231 display
= DT_CURSES
;
2233 fprintf(stderr
, "Curses support is disabled\n");
2236 } else if (strstart(p
, "gtk", &opts
)) {
2240 const char *nextopt
;
2242 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2244 if (strstart(opts
, "on", &nextopt
)) {
2245 grab_on_hover
= true;
2246 } else if (strstart(opts
, "off", &nextopt
)) {
2247 grab_on_hover
= false;
2249 goto invalid_gtk_args
;
2253 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2259 fprintf(stderr
, "GTK support is disabled\n");
2262 } else if (strstart(p
, "none", &opts
)) {
2265 fprintf(stderr
, "Unknown display type: %s\n", p
);
2272 static int balloon_parse(const char *arg
)
2276 if (strcmp(arg
, "none") == 0) {
2280 if (!strncmp(arg
, "virtio", 6)) {
2281 if (arg
[6] == ',') {
2282 /* have params -> parse them */
2283 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2287 /* create empty opts */
2288 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2291 qemu_opt_set(opts
, "driver", "virtio-balloon");
2298 char *qemu_find_file(int type
, const char *name
)
2304 /* Try the name as a straight path first */
2305 if (access(name
, R_OK
) == 0) {
2306 trace_load_file(name
, name
);
2307 return g_strdup(name
);
2311 case QEMU_FILE_TYPE_BIOS
:
2314 case QEMU_FILE_TYPE_KEYMAP
:
2315 subdir
= "keymaps/";
2321 for (i
= 0; i
< data_dir_idx
; i
++) {
2322 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2323 if (access(buf
, R_OK
) == 0) {
2324 trace_load_file(name
, buf
);
2332 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2334 return qdev_device_help(opts
);
2337 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2341 dev
= qdev_device_add(opts
);
2344 object_unref(OBJECT(dev
));
2348 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2350 Error
*local_err
= NULL
;
2352 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2354 error_report("%s", error_get_pretty(local_err
));
2355 error_free(local_err
);
2361 #ifdef CONFIG_VIRTFS
2362 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2365 ret
= qemu_fsdev_add(opts
);
2371 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2373 CharDriverState
*chr
;
2374 const char *chardev
;
2378 mode
= qemu_opt_get(opts
, "mode");
2382 if (strcmp(mode
, "readline") == 0) {
2383 flags
= MONITOR_USE_READLINE
;
2384 } else if (strcmp(mode
, "control") == 0) {
2385 flags
= MONITOR_USE_CONTROL
;
2387 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2391 if (qemu_opt_get_bool(opts
, "pretty", 0))
2392 flags
|= MONITOR_USE_PRETTY
;
2394 if (qemu_opt_get_bool(opts
, "default", 0))
2395 flags
|= MONITOR_IS_DEFAULT
;
2397 chardev
= qemu_opt_get(opts
, "chardev");
2398 chr
= qemu_chr_find(chardev
);
2400 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2404 qemu_chr_fe_claim_no_fail(chr
);
2405 monitor_init(chr
, flags
);
2409 static void monitor_parse(const char *optarg
, const char *mode
)
2411 static int monitor_device_index
= 0;
2417 if (strstart(optarg
, "chardev:", &p
)) {
2418 snprintf(label
, sizeof(label
), "%s", p
);
2420 snprintf(label
, sizeof(label
), "compat_monitor%d",
2421 monitor_device_index
);
2422 if (monitor_device_index
== 0) {
2425 opts
= qemu_chr_parse_compat(label
, optarg
);
2427 fprintf(stderr
, "parse error: %s\n", optarg
);
2432 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2434 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2437 qemu_opt_set(opts
, "mode", mode
);
2438 qemu_opt_set(opts
, "chardev", label
);
2440 qemu_opt_set(opts
, "default", "on");
2441 monitor_device_index
++;
2444 struct device_config
{
2446 DEV_USB
, /* -usbdevice */
2448 DEV_SERIAL
, /* -serial */
2449 DEV_PARALLEL
, /* -parallel */
2450 DEV_VIRTCON
, /* -virtioconsole */
2451 DEV_DEBUGCON
, /* -debugcon */
2452 DEV_GDB
, /* -gdb, -s */
2453 DEV_SCLP
, /* s390 sclp */
2455 const char *cmdline
;
2457 QTAILQ_ENTRY(device_config
) next
;
2460 static QTAILQ_HEAD(, device_config
) device_configs
=
2461 QTAILQ_HEAD_INITIALIZER(device_configs
);
2463 static void add_device_config(int type
, const char *cmdline
)
2465 struct device_config
*conf
;
2467 conf
= g_malloc0(sizeof(*conf
));
2469 conf
->cmdline
= cmdline
;
2470 loc_save(&conf
->loc
);
2471 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2474 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2476 struct device_config
*conf
;
2479 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2480 if (conf
->type
!= type
)
2482 loc_push_restore(&conf
->loc
);
2483 rc
= func(conf
->cmdline
);
2484 loc_pop(&conf
->loc
);
2491 static int serial_parse(const char *devname
)
2493 static int index
= 0;
2496 if (strcmp(devname
, "none") == 0)
2498 if (index
== MAX_SERIAL_PORTS
) {
2499 fprintf(stderr
, "qemu: too many serial ports\n");
2502 snprintf(label
, sizeof(label
), "serial%d", index
);
2503 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2504 if (!serial_hds
[index
]) {
2505 fprintf(stderr
, "qemu: could not connect serial device"
2506 " to character backend '%s'\n", devname
);
2513 static int parallel_parse(const char *devname
)
2515 static int index
= 0;
2518 if (strcmp(devname
, "none") == 0)
2520 if (index
== MAX_PARALLEL_PORTS
) {
2521 fprintf(stderr
, "qemu: too many parallel ports\n");
2524 snprintf(label
, sizeof(label
), "parallel%d", index
);
2525 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2526 if (!parallel_hds
[index
]) {
2527 fprintf(stderr
, "qemu: could not connect parallel device"
2528 " to character backend '%s'\n", devname
);
2535 static int virtcon_parse(const char *devname
)
2537 QemuOptsList
*device
= qemu_find_opts("device");
2538 static int index
= 0;
2540 QemuOpts
*bus_opts
, *dev_opts
;
2542 if (strcmp(devname
, "none") == 0)
2544 if (index
== MAX_VIRTIO_CONSOLES
) {
2545 fprintf(stderr
, "qemu: too many virtio consoles\n");
2549 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2550 if (arch_type
== QEMU_ARCH_S390X
) {
2551 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2553 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2556 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2557 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2559 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2560 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2561 if (!virtcon_hds
[index
]) {
2562 fprintf(stderr
, "qemu: could not connect virtio console"
2563 " to character backend '%s'\n", devname
);
2566 qemu_opt_set(dev_opts
, "chardev", label
);
2572 static int sclp_parse(const char *devname
)
2574 QemuOptsList
*device
= qemu_find_opts("device");
2575 static int index
= 0;
2579 if (strcmp(devname
, "none") == 0) {
2582 if (index
== MAX_SCLP_CONSOLES
) {
2583 fprintf(stderr
, "qemu: too many sclp consoles\n");
2587 assert(arch_type
== QEMU_ARCH_S390X
);
2589 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2590 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2592 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2593 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2594 if (!sclp_hds
[index
]) {
2595 fprintf(stderr
, "qemu: could not connect sclp console"
2596 " to character backend '%s'\n", devname
);
2599 qemu_opt_set(dev_opts
, "chardev", label
);
2605 static int debugcon_parse(const char *devname
)
2609 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2612 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2614 fprintf(stderr
, "qemu: already have a debugcon device\n");
2617 qemu_opt_set(opts
, "driver", "isa-debugcon");
2618 qemu_opt_set(opts
, "chardev", "debugcon");
2622 static MachineClass
*machine_parse(const char *name
)
2624 MachineClass
*mc
= NULL
;
2625 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2628 mc
= find_machine(name
);
2633 if (name
&& !is_help_option(name
)) {
2634 error_report("Unsupported machine type");
2635 error_printf("Use -machine help to list supported machines!\n");
2637 printf("Supported machines are:\n");
2638 for (el
= machines
; el
; el
= el
->next
) {
2639 MachineClass
*mc
= el
->data
;
2641 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2643 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2644 mc
->is_default
? " (default)" : "");
2648 g_slist_free(machines
);
2649 exit(!name
|| !is_help_option(name
));
2652 static int tcg_init(MachineClass
*mc
)
2654 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2659 const char *opt_name
;
2661 int (*available
)(void);
2662 int (*init
)(MachineClass
*mc
);
2665 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2666 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2667 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2668 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2671 static int configure_accelerator(MachineClass
*mc
)
2676 bool accel_initialised
= false;
2677 bool init_failed
= false;
2679 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2681 /* Use the default "accelerator", tcg */
2685 while (!accel_initialised
&& *p
!= '\0') {
2689 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2690 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2691 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2692 if (!accel_list
[i
].available()) {
2693 printf("%s not supported for this target\n",
2694 accel_list
[i
].name
);
2697 *(accel_list
[i
].allowed
) = true;
2698 ret
= accel_list
[i
].init(mc
);
2701 fprintf(stderr
, "failed to initialize %s: %s\n",
2704 *(accel_list
[i
].allowed
) = false;
2706 accel_initialised
= true;
2711 if (i
== ARRAY_SIZE(accel_list
)) {
2712 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2716 if (!accel_initialised
) {
2718 fprintf(stderr
, "No accelerator found!\n");
2724 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2727 return !accel_initialised
;
2730 void qemu_add_exit_notifier(Notifier
*notify
)
2732 notifier_list_add(&exit_notifiers
, notify
);
2735 void qemu_remove_exit_notifier(Notifier
*notify
)
2737 notifier_remove(notify
);
2740 static void qemu_run_exit_notifiers(void)
2742 notifier_list_notify(&exit_notifiers
, NULL
);
2745 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2747 notifier_list_add(&machine_init_done_notifiers
, notify
);
2750 static void qemu_run_machine_init_done_notifiers(void)
2752 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2755 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2756 const char **poptarg
, int *poptind
)
2758 const QEMUOption
*popt
;
2759 int optind
= *poptind
;
2760 char *r
= argv
[optind
];
2763 loc_set_cmdline(argv
, optind
, 1);
2765 /* Treat --foo the same as -foo. */
2768 popt
= qemu_options
;
2771 error_report("invalid option");
2774 if (!strcmp(popt
->name
, r
+ 1))
2778 if (popt
->flags
& HAS_ARG
) {
2779 if (optind
>= argc
) {
2780 error_report("requires an argument");
2783 optarg
= argv
[optind
++];
2784 loc_set_cmdline(argv
, optind
- 2, 2);
2795 static gpointer
malloc_and_trace(gsize n_bytes
)
2797 void *ptr
= malloc(n_bytes
);
2798 trace_g_malloc(n_bytes
, ptr
);
2802 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2804 void *ptr
= realloc(mem
, n_bytes
);
2805 trace_g_realloc(mem
, n_bytes
, ptr
);
2809 static void free_and_trace(gpointer mem
)
2815 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2817 Object
*obj
= OBJECT(opaque
);
2818 StringInputVisitor
*siv
;
2819 Error
*local_err
= NULL
;
2821 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2822 strcmp(name
, "type") == 0) {
2826 siv
= string_input_visitor_new(value
);
2827 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2828 string_input_visitor_cleanup(siv
);
2831 qerror_report_err(local_err
);
2832 error_free(local_err
);
2839 static int object_create(QemuOpts
*opts
, void *opaque
)
2848 ov
= opts_visitor_new(opts
);
2849 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2851 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2856 qdict_del(pdict
, "qom-type");
2857 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2862 qdict_del(pdict
, "id");
2863 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2868 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2872 visit_end_struct(opts_get_visitor(ov
), &err
);
2874 qmp_object_del(id
, NULL
);
2878 opts_visitor_cleanup(ov
);
2885 qerror_report_err(err
);
2891 int main(int argc
, char **argv
, char **envp
)
2894 int snapshot
, linux_boot
;
2895 const char *icount_option
= NULL
;
2896 const char *initrd_filename
;
2897 const char *kernel_filename
, *kernel_cmdline
;
2898 const char *boot_order
;
2900 int cyls
, heads
, secs
, translation
;
2901 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2902 QemuOptsList
*olist
;
2905 const char *loadvm
= NULL
;
2906 MachineClass
*machine_class
;
2907 const char *cpu_model
;
2908 const char *vga_model
= NULL
;
2909 const char *qtest_chrdev
= NULL
;
2910 const char *qtest_log
= NULL
;
2911 const char *pid_file
= NULL
;
2912 const char *incoming
= NULL
;
2914 int show_vnc_port
= 0;
2916 bool defconfig
= true;
2917 bool userconfig
= true;
2918 const char *log_mask
= NULL
;
2919 const char *log_file
= NULL
;
2920 GMemVTable mem_trace
= {
2921 .malloc
= malloc_and_trace
,
2922 .realloc
= realloc_and_trace
,
2923 .free
= free_and_trace
,
2925 const char *trace_events
= NULL
;
2926 const char *trace_file
= NULL
;
2927 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2929 ram_addr_t maxram_size
= default_ram_size
;
2930 uint64_t ram_slots
= 0;
2932 atexit(qemu_run_exit_notifiers
);
2933 error_set_progname(argv
[0]);
2934 qemu_init_exec_dir(argv
[0]);
2936 g_mem_set_vtable(&mem_trace
);
2938 module_call_init(MODULE_INIT_QOM
);
2940 qemu_add_opts(&qemu_drive_opts
);
2941 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2942 qemu_add_drive_opts(&qemu_common_drive_opts
);
2943 qemu_add_drive_opts(&qemu_drive_opts
);
2944 qemu_add_opts(&qemu_chardev_opts
);
2945 qemu_add_opts(&qemu_device_opts
);
2946 qemu_add_opts(&qemu_netdev_opts
);
2947 qemu_add_opts(&qemu_net_opts
);
2948 qemu_add_opts(&qemu_rtc_opts
);
2949 qemu_add_opts(&qemu_global_opts
);
2950 qemu_add_opts(&qemu_mon_opts
);
2951 qemu_add_opts(&qemu_trace_opts
);
2952 qemu_add_opts(&qemu_option_rom_opts
);
2953 qemu_add_opts(&qemu_machine_opts
);
2954 qemu_add_opts(&qemu_mem_opts
);
2955 qemu_add_opts(&qemu_smp_opts
);
2956 qemu_add_opts(&qemu_boot_opts
);
2957 qemu_add_opts(&qemu_sandbox_opts
);
2958 qemu_add_opts(&qemu_add_fd_opts
);
2959 qemu_add_opts(&qemu_object_opts
);
2960 qemu_add_opts(&qemu_tpmdev_opts
);
2961 qemu_add_opts(&qemu_realtime_opts
);
2962 qemu_add_opts(&qemu_msg_opts
);
2963 qemu_add_opts(&qemu_name_opts
);
2964 qemu_add_opts(&qemu_numa_opts
);
2968 rtc_clock
= QEMU_CLOCK_HOST
;
2970 qemu_init_auxval(envp
);
2971 qemu_cache_utils_init();
2973 QLIST_INIT (&vm_change_state_head
);
2974 os_setup_early_signal_handling();
2976 module_call_init(MODULE_INIT_MACHINE
);
2977 machine_class
= find_default_machine();
2979 ram_size
= default_ram_size
;
2981 cyls
= heads
= secs
= 0;
2982 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2984 for (i
= 0; i
< MAX_NODES
; i
++) {
2985 numa_info
[i
].node_mem
= 0;
2986 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2992 bdrv_init_with_whitelist();
2996 /* first pass of option parsing */
2998 while (optind
< argc
) {
2999 if (argv
[optind
][0] != '-') {
3003 const QEMUOption
*popt
;
3005 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3006 switch (popt
->index
) {
3007 case QEMU_OPTION_nodefconfig
:
3010 case QEMU_OPTION_nouserconfig
:
3019 ret
= qemu_read_default_config_files(userconfig
);
3025 /* second pass of option parsing */
3030 if (argv
[optind
][0] != '-') {
3031 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3033 const QEMUOption
*popt
;
3035 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3036 if (!(popt
->arch_mask
& arch_type
)) {
3037 printf("Option %s not supported for this target\n", popt
->name
);
3040 switch(popt
->index
) {
3042 machine_class
= machine_parse(optarg
);
3044 case QEMU_OPTION_no_kvm_irqchip
: {
3045 olist
= qemu_find_opts("machine");
3046 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3049 case QEMU_OPTION_cpu
:
3050 /* hw initialization will check this */
3053 case QEMU_OPTION_hda
:
3057 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3059 snprintf(buf
, sizeof(buf
),
3060 "%s,cyls=%d,heads=%d,secs=%d%s",
3061 HD_OPTS
, cyls
, heads
, secs
,
3062 translation
== BIOS_ATA_TRANSLATION_LBA
?
3064 translation
== BIOS_ATA_TRANSLATION_NONE
?
3065 ",trans=none" : "");
3066 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3069 case QEMU_OPTION_hdb
:
3070 case QEMU_OPTION_hdc
:
3071 case QEMU_OPTION_hdd
:
3072 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3075 case QEMU_OPTION_drive
:
3076 if (drive_def(optarg
) == NULL
) {
3080 case QEMU_OPTION_set
:
3081 if (qemu_set_option(optarg
) != 0)
3084 case QEMU_OPTION_global
:
3085 if (qemu_global_option(optarg
) != 0)
3088 case QEMU_OPTION_mtdblock
:
3089 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3091 case QEMU_OPTION_sd
:
3092 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3094 case QEMU_OPTION_pflash
:
3095 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3097 case QEMU_OPTION_snapshot
:
3100 case QEMU_OPTION_hdachs
:
3104 cyls
= strtol(p
, (char **)&p
, 0);
3105 if (cyls
< 1 || cyls
> 16383)
3110 heads
= strtol(p
, (char **)&p
, 0);
3111 if (heads
< 1 || heads
> 16)
3116 secs
= strtol(p
, (char **)&p
, 0);
3117 if (secs
< 1 || secs
> 63)
3121 if (!strcmp(p
, "large")) {
3122 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3123 } else if (!strcmp(p
, "rechs")) {
3124 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3125 } else if (!strcmp(p
, "none")) {
3126 translation
= BIOS_ATA_TRANSLATION_NONE
;
3127 } else if (!strcmp(p
, "lba")) {
3128 translation
= BIOS_ATA_TRANSLATION_LBA
;
3129 } else if (!strcmp(p
, "auto")) {
3130 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3134 } else if (*p
!= '\0') {
3136 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3139 if (hda_opts
!= NULL
) {
3141 snprintf(num
, sizeof(num
), "%d", cyls
);
3142 qemu_opt_set(hda_opts
, "cyls", num
);
3143 snprintf(num
, sizeof(num
), "%d", heads
);
3144 qemu_opt_set(hda_opts
, "heads", num
);
3145 snprintf(num
, sizeof(num
), "%d", secs
);
3146 qemu_opt_set(hda_opts
, "secs", num
);
3147 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3148 qemu_opt_set(hda_opts
, "trans", "large");
3149 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3150 qemu_opt_set(hda_opts
, "trans", "rechs");
3151 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3152 qemu_opt_set(hda_opts
, "trans", "lba");
3153 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3154 qemu_opt_set(hda_opts
, "trans", "none");
3159 case QEMU_OPTION_numa
:
3160 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3165 case QEMU_OPTION_display
:
3166 display_type
= select_display(optarg
);
3168 case QEMU_OPTION_nographic
:
3169 display_type
= DT_NOGRAPHIC
;
3171 case QEMU_OPTION_curses
:
3172 #ifdef CONFIG_CURSES
3173 display_type
= DT_CURSES
;
3175 fprintf(stderr
, "Curses support is disabled\n");
3179 case QEMU_OPTION_portrait
:
3180 graphic_rotate
= 90;
3182 case QEMU_OPTION_rotate
:
3183 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3184 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3185 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3187 "qemu: only 90, 180, 270 deg rotation is available\n");
3191 case QEMU_OPTION_kernel
:
3192 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3194 case QEMU_OPTION_initrd
:
3195 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3197 case QEMU_OPTION_append
:
3198 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3200 case QEMU_OPTION_dtb
:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3203 case QEMU_OPTION_cdrom
:
3204 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3206 case QEMU_OPTION_boot
:
3207 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3212 case QEMU_OPTION_fda
:
3213 case QEMU_OPTION_fdb
:
3214 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3217 case QEMU_OPTION_no_fd_bootchk
:
3220 case QEMU_OPTION_netdev
:
3221 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3225 case QEMU_OPTION_net
:
3226 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3230 #ifdef CONFIG_LIBISCSI
3231 case QEMU_OPTION_iscsi
:
3232 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3239 case QEMU_OPTION_tftp
:
3240 legacy_tftp_prefix
= optarg
;
3242 case QEMU_OPTION_bootp
:
3243 legacy_bootp_filename
= optarg
;
3245 case QEMU_OPTION_redir
:
3246 if (net_slirp_redir(optarg
) < 0)
3250 case QEMU_OPTION_bt
:
3251 add_device_config(DEV_BT
, optarg
);
3253 case QEMU_OPTION_audio_help
:
3257 case QEMU_OPTION_soundhw
:
3258 select_soundhw (optarg
);
3263 case QEMU_OPTION_version
:
3267 case QEMU_OPTION_m
: {
3269 const char *mem_str
;
3270 const char *maxmem_str
, *slots_str
;
3272 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3278 mem_str
= qemu_opt_get(opts
, "size");
3280 error_report("invalid -m option, missing 'size' option");
3284 error_report("missing 'size' option value");
3288 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3290 /* Fix up legacy suffix-less format */
3291 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3292 uint64_t overflow_check
= sz
;
3295 if ((sz
>> 20) != overflow_check
) {
3296 error_report("too large 'size' option value");
3301 /* backward compatibility behaviour for case "-m 0" */
3303 sz
= default_ram_size
;
3306 sz
= QEMU_ALIGN_UP(sz
, 8192);
3308 if (ram_size
!= sz
) {
3309 error_report("ram size too large");
3313 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3314 slots_str
= qemu_opt_get(opts
, "slots");
3315 if (maxmem_str
&& slots_str
) {
3318 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3319 if (sz
< ram_size
) {
3320 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3321 "(%" PRIu64
") <= initial memory ("
3322 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3326 slots
= qemu_opt_get_number(opts
, "slots", 0);
3327 if ((sz
> ram_size
) && !slots
) {
3328 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3329 "(%" PRIu64
") more than initial memory ("
3330 RAM_ADDR_FMT
") but no hotplug slots where "
3331 "specified\n", sz
, ram_size
);
3335 if ((sz
<= ram_size
) && slots
) {
3336 fprintf(stderr
, "qemu: invalid -m option value: %"
3337 PRIu64
" hotplug slots where specified but "
3338 "maxmem (%" PRIu64
") <= initial memory ("
3339 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3344 } else if ((!maxmem_str
&& slots_str
) ||
3345 (maxmem_str
&& !slots_str
)) {
3346 fprintf(stderr
, "qemu: invalid -m option value: missing "
3347 "'%s' option\n", slots_str
? "maxmem" : "slots");
3353 case QEMU_OPTION_tpmdev
:
3354 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3359 case QEMU_OPTION_mempath
:
3362 case QEMU_OPTION_mem_prealloc
:
3372 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3374 case QEMU_OPTION_gdb
:
3375 add_device_config(DEV_GDB
, optarg
);
3378 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3379 data_dir
[data_dir_idx
++] = optarg
;
3382 case QEMU_OPTION_bios
:
3383 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3385 case QEMU_OPTION_singlestep
:
3392 keyboard_layout
= optarg
;
3394 case QEMU_OPTION_localtime
:
3397 case QEMU_OPTION_vga
:
3406 w
= strtol(p
, (char **)&p
, 10);
3409 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3415 h
= strtol(p
, (char **)&p
, 10);
3420 depth
= strtol(p
, (char **)&p
, 10);
3421 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3422 depth
!= 24 && depth
!= 32)
3424 } else if (*p
== '\0') {
3425 depth
= graphic_depth
;
3432 graphic_depth
= depth
;
3435 case QEMU_OPTION_echr
:
3438 term_escape_char
= strtol(optarg
, &r
, 0);
3440 printf("Bad argument to echr\n");
3443 case QEMU_OPTION_monitor
:
3444 default_monitor
= 0;
3445 if (strncmp(optarg
, "none", 4)) {
3446 monitor_parse(optarg
, "readline");
3449 case QEMU_OPTION_qmp
:
3450 monitor_parse(optarg
, "control");
3451 default_monitor
= 0;
3453 case QEMU_OPTION_mon
:
3454 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3458 default_monitor
= 0;
3460 case QEMU_OPTION_chardev
:
3461 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3466 case QEMU_OPTION_fsdev
:
3467 olist
= qemu_find_opts("fsdev");
3469 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3472 opts
= qemu_opts_parse(olist
, optarg
, 1);
3477 case QEMU_OPTION_virtfs
: {
3480 const char *writeout
, *sock_fd
, *socket
;
3482 olist
= qemu_find_opts("virtfs");
3484 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3487 opts
= qemu_opts_parse(olist
, optarg
, 1);
3492 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3493 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3494 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3497 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3498 qemu_opt_get(opts
, "mount_tag"),
3501 fprintf(stderr
, "duplicate fsdev id: %s\n",
3502 qemu_opt_get(opts
, "mount_tag"));
3506 writeout
= qemu_opt_get(opts
, "writeout");
3508 #ifdef CONFIG_SYNC_FILE_RANGE
3509 qemu_opt_set(fsdev
, "writeout", writeout
);
3511 fprintf(stderr
, "writeout=immediate not supported on "
3516 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3517 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3518 qemu_opt_set(fsdev
, "security_model",
3519 qemu_opt_get(opts
, "security_model"));
3520 socket
= qemu_opt_get(opts
, "socket");
3522 qemu_opt_set(fsdev
, "socket", socket
);
3524 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3526 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3529 qemu_opt_set_bool(fsdev
, "readonly",
3530 qemu_opt_get_bool(opts
, "readonly", 0));
3531 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3533 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3534 qemu_opt_set(device
, "fsdev",
3535 qemu_opt_get(opts
, "mount_tag"));
3536 qemu_opt_set(device
, "mount_tag",
3537 qemu_opt_get(opts
, "mount_tag"));
3540 case QEMU_OPTION_virtfs_synth
: {
3544 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3547 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3550 qemu_opt_set(fsdev
, "fsdriver", "synth");
3552 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3554 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3555 qemu_opt_set(device
, "fsdev", "v_synth");
3556 qemu_opt_set(device
, "mount_tag", "v_synth");
3559 case QEMU_OPTION_serial
:
3560 add_device_config(DEV_SERIAL
, optarg
);
3562 if (strncmp(optarg
, "mon:", 4) == 0) {
3563 default_monitor
= 0;
3566 case QEMU_OPTION_watchdog
:
3569 "qemu: only one watchdog option may be given\n");
3574 case QEMU_OPTION_watchdog_action
:
3575 if (select_watchdog_action(optarg
) == -1) {
3576 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3580 case QEMU_OPTION_virtiocon
:
3581 add_device_config(DEV_VIRTCON
, optarg
);
3582 default_virtcon
= 0;
3583 if (strncmp(optarg
, "mon:", 4) == 0) {
3584 default_monitor
= 0;
3587 case QEMU_OPTION_parallel
:
3588 add_device_config(DEV_PARALLEL
, optarg
);
3589 default_parallel
= 0;
3590 if (strncmp(optarg
, "mon:", 4) == 0) {
3591 default_monitor
= 0;
3594 case QEMU_OPTION_debugcon
:
3595 add_device_config(DEV_DEBUGCON
, optarg
);
3597 case QEMU_OPTION_loadvm
:
3600 case QEMU_OPTION_full_screen
:
3603 case QEMU_OPTION_no_frame
:
3606 case QEMU_OPTION_alt_grab
:
3609 case QEMU_OPTION_ctrl_grab
:
3612 case QEMU_OPTION_no_quit
:
3615 case QEMU_OPTION_sdl
:
3617 display_type
= DT_SDL
;
3620 fprintf(stderr
, "SDL support is disabled\n");
3623 case QEMU_OPTION_pidfile
:
3626 case QEMU_OPTION_win2k_hack
:
3627 win2k_install_hack
= 1;
3629 case QEMU_OPTION_rtc_td_hack
: {
3630 static GlobalProperty slew_lost_ticks
[] = {
3632 .driver
= "mc146818rtc",
3633 .property
= "lost_tick_policy",
3636 { /* end of list */ }
3639 qdev_prop_register_global_list(slew_lost_ticks
);
3642 case QEMU_OPTION_acpitable
:
3643 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3647 do_acpitable_option(opts
);
3649 case QEMU_OPTION_smbios
:
3650 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3654 do_smbios_option(opts
);
3656 case QEMU_OPTION_enable_kvm
:
3657 olist
= qemu_find_opts("machine");
3658 qemu_opts_parse(olist
, "accel=kvm", 0);
3660 case QEMU_OPTION_machine
:
3661 olist
= qemu_find_opts("machine");
3662 opts
= qemu_opts_parse(olist
, optarg
, 1);
3666 optarg
= qemu_opt_get(opts
, "type");
3668 machine_class
= machine_parse(optarg
);
3671 case QEMU_OPTION_no_kvm
:
3672 olist
= qemu_find_opts("machine");
3673 qemu_opts_parse(olist
, "accel=tcg", 0);
3675 case QEMU_OPTION_no_kvm_pit
: {
3676 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3680 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3681 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3683 .driver
= "kvm-pit",
3684 .property
= "lost_tick_policy",
3687 { /* end of list */ }
3690 fprintf(stderr
, "Warning: option deprecated, use "
3691 "lost_tick_policy property of kvm-pit instead.\n");
3692 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3695 case QEMU_OPTION_usb
:
3696 olist
= qemu_find_opts("machine");
3697 qemu_opts_parse(olist
, "usb=on", 0);
3699 case QEMU_OPTION_usbdevice
:
3700 olist
= qemu_find_opts("machine");
3701 qemu_opts_parse(olist
, "usb=on", 0);
3702 add_device_config(DEV_USB
, optarg
);
3704 case QEMU_OPTION_device
:
3705 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3709 case QEMU_OPTION_smp
:
3710 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3714 case QEMU_OPTION_vnc
:
3717 vnc_display
= optarg
;
3719 fprintf(stderr
, "VNC support is disabled\n");
3723 case QEMU_OPTION_no_acpi
:
3726 case QEMU_OPTION_no_hpet
:
3729 case QEMU_OPTION_balloon
:
3730 if (balloon_parse(optarg
) < 0) {
3731 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3735 case QEMU_OPTION_no_reboot
:
3738 case QEMU_OPTION_no_shutdown
:
3741 case QEMU_OPTION_show_cursor
:
3744 case QEMU_OPTION_uuid
:
3745 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3746 fprintf(stderr
, "Fail to parse UUID string."
3747 " Wrong format.\n");
3750 qemu_uuid_set
= true;
3752 case QEMU_OPTION_option_rom
:
3753 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3754 fprintf(stderr
, "Too many option ROMs\n");
3757 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3761 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3762 option_rom
[nb_option_roms
].bootindex
=
3763 qemu_opt_get_number(opts
, "bootindex", -1);
3764 if (!option_rom
[nb_option_roms
].name
) {
3765 fprintf(stderr
, "Option ROM file is not specified\n");
3770 case QEMU_OPTION_semihosting
:
3771 semihosting_enabled
= 1;
3773 case QEMU_OPTION_tdf
:
3774 fprintf(stderr
, "Warning: user space PIT time drift fix "
3775 "is no longer supported.\n");
3777 case QEMU_OPTION_name
:
3778 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3783 case QEMU_OPTION_prom_env
:
3784 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3785 fprintf(stderr
, "Too many prom variables\n");
3788 prom_envs
[nb_prom_envs
] = optarg
;
3791 case QEMU_OPTION_old_param
:
3794 case QEMU_OPTION_clock
:
3795 /* Clock options no longer exist. Keep this option for
3796 * backward compatibility.
3799 case QEMU_OPTION_startdate
:
3800 configure_rtc_date_offset(optarg
, 1);
3802 case QEMU_OPTION_rtc
:
3803 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3807 configure_rtc(opts
);
3809 case QEMU_OPTION_tb_size
:
3810 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3811 if (tcg_tb_size
< 0) {
3815 case QEMU_OPTION_icount
:
3816 icount_option
= optarg
;
3818 case QEMU_OPTION_incoming
:
3820 runstate_set(RUN_STATE_INMIGRATE
);
3822 case QEMU_OPTION_nodefaults
:
3825 case QEMU_OPTION_xen_domid
:
3826 if (!(xen_available())) {
3827 printf("Option %s not supported for this target\n", popt
->name
);
3830 xen_domid
= atoi(optarg
);
3832 case QEMU_OPTION_xen_create
:
3833 if (!(xen_available())) {
3834 printf("Option %s not supported for this target\n", popt
->name
);
3837 xen_mode
= XEN_CREATE
;
3839 case QEMU_OPTION_xen_attach
:
3840 if (!(xen_available())) {
3841 printf("Option %s not supported for this target\n", popt
->name
);
3844 xen_mode
= XEN_ATTACH
;
3846 case QEMU_OPTION_trace
:
3848 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3852 trace_events
= qemu_opt_get(opts
, "events");
3853 trace_file
= qemu_opt_get(opts
, "file");
3856 case QEMU_OPTION_readconfig
:
3858 int ret
= qemu_read_config_file(optarg
);
3860 fprintf(stderr
, "read config %s: %s\n", optarg
,
3866 case QEMU_OPTION_spice
:
3867 olist
= qemu_find_opts("spice");
3869 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3872 opts
= qemu_opts_parse(olist
, optarg
, 0);
3878 case QEMU_OPTION_writeconfig
:
3881 if (strcmp(optarg
, "-") == 0) {
3884 fp
= fopen(optarg
, "w");
3886 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3890 qemu_config_write(fp
);
3896 case QEMU_OPTION_qtest
:
3897 qtest_chrdev
= optarg
;
3899 case QEMU_OPTION_qtest_log
:
3902 case QEMU_OPTION_sandbox
:
3903 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3908 case QEMU_OPTION_add_fd
:
3910 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3915 error_report("File descriptor passing is disabled on this "
3920 case QEMU_OPTION_object
:
3921 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3926 case QEMU_OPTION_realtime
:
3927 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3931 configure_realtime(opts
);
3933 case QEMU_OPTION_msg
:
3934 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3938 configure_msg(opts
);
3941 os_parse_cmd_args(popt
->index
, optarg
);
3949 if (qemu_init_main_loop()) {
3950 fprintf(stderr
, "qemu_init_main_loop failed\n");
3954 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3958 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3963 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3967 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3972 if (machine_class
== NULL
) {
3973 fprintf(stderr
, "No machine specified, and there is no default.\n"
3974 "Use -machine help to list supported machines!\n");
3978 cpu_exec_init_all();
3980 current_machine
= MACHINE(object_new(object_class_get_name(
3981 OBJECT_CLASS(machine_class
))));
3982 object_property_add_child(object_get_root(), "machine",
3983 OBJECT(current_machine
), &error_abort
);
3985 if (machine_class
->hw_version
) {
3986 qemu_set_version(machine_class
->hw_version
);
3989 if (qemu_opts_foreach(qemu_find_opts("object"),
3990 object_create
, NULL
, 0) != 0) {
3994 /* Init CPU def lists, based on config
3995 * - Must be called after all the qemu_read_config_file() calls
3996 * - Must be called before list_cpus()
3997 * - Must be called before machine->init()
4001 if (cpu_model
&& is_help_option(cpu_model
)) {
4002 list_cpus(stdout
, &fprintf
, cpu_model
);
4006 /* Open the logfile at this point, if necessary. We can't open the logfile
4007 * when encountering either of the logging options (-d or -D) because the
4008 * other one may be encountered later on the command line, changing the
4009 * location or level of logging.
4014 qemu_set_log_filename(log_file
);
4017 mask
= qemu_str_to_log_mask(log_mask
);
4019 qemu_print_log_usage(stdout
);
4025 if (!is_daemonized()) {
4026 if (!trace_init_backends(trace_events
, trace_file
)) {
4031 /* If no data_dir is specified then try to find it relative to the
4033 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4034 data_dir
[data_dir_idx
] = os_find_datadir();
4035 if (data_dir
[data_dir_idx
] != NULL
) {
4039 /* If all else fails use the install path specified when building. */
4040 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4041 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4044 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4046 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4047 if (smp_cpus
> machine_class
->max_cpus
) {
4048 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4049 "supported by machine `%s' (%d)\n", smp_cpus
,
4050 machine_class
->name
, machine_class
->max_cpus
);
4055 * Get the default machine options from the machine if it is not already
4056 * specified either by the configuration file or by the command line.
4058 if (machine_class
->default_machine_opts
) {
4059 qemu_opts_set_defaults(qemu_find_opts("machine"),
4060 machine_class
->default_machine_opts
, 0);
4063 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4064 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4066 if (!vga_model
&& !default_vga
) {
4067 vga_interface_type
= VGA_DEVICE
;
4069 if (!has_defaults
|| machine_class
->no_serial
) {
4072 if (!has_defaults
|| machine_class
->no_parallel
) {
4073 default_parallel
= 0;
4075 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4076 default_virtcon
= 0;
4078 if (!has_defaults
|| !machine_class
->use_sclp
) {
4081 if (!has_defaults
|| machine_class
->no_floppy
) {
4084 if (!has_defaults
|| machine_class
->no_cdrom
) {
4087 if (!has_defaults
|| machine_class
->no_sdcard
) {
4090 if (!has_defaults
) {
4091 default_monitor
= 0;
4096 if (is_daemonized()) {
4097 /* According to documentation and historically, -nographic redirects
4098 * serial port, parallel port and monitor to stdio, which does not work
4099 * with -daemonize. We can redirect these to null instead, but since
4100 * -nographic is legacy, let's just error out.
4101 * We disallow -nographic only if all other ports are not redirected
4102 * explicitly, to not break existing legacy setups which uses
4103 * -nographic _and_ redirects all ports explicitly - this is valid
4104 * usage, -nographic is just a no-op in this case.
4106 if (display_type
== DT_NOGRAPHIC
4107 && (default_parallel
|| default_serial
4108 || default_monitor
|| default_virtcon
)) {
4109 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4112 #ifdef CONFIG_CURSES
4113 if (display_type
== DT_CURSES
) {
4114 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4120 if (display_type
== DT_NOGRAPHIC
) {
4121 if (default_parallel
)
4122 add_device_config(DEV_PARALLEL
, "null");
4123 if (default_serial
&& default_monitor
) {
4124 add_device_config(DEV_SERIAL
, "mon:stdio");
4125 } else if (default_virtcon
&& default_monitor
) {
4126 add_device_config(DEV_VIRTCON
, "mon:stdio");
4127 } else if (default_sclp
&& default_monitor
) {
4128 add_device_config(DEV_SCLP
, "mon:stdio");
4131 add_device_config(DEV_SERIAL
, "stdio");
4132 if (default_virtcon
)
4133 add_device_config(DEV_VIRTCON
, "stdio");
4135 add_device_config(DEV_SCLP
, "stdio");
4137 if (default_monitor
)
4138 monitor_parse("stdio", "readline");
4142 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4143 if (default_parallel
)
4144 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4145 if (default_monitor
)
4146 monitor_parse("vc:80Cx24C", "readline");
4147 if (default_virtcon
)
4148 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4150 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4154 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4155 #if defined(CONFIG_GTK)
4156 display_type
= DT_GTK
;
4157 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4158 display_type
= DT_SDL
;
4159 #elif defined(CONFIG_VNC)
4160 vnc_display
= "localhost:0,to=99";
4163 display_type
= DT_NONE
;
4167 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4168 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4169 "for SDL, ignoring option\n");
4171 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4172 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4173 "ignoring option\n");
4176 #if defined(CONFIG_GTK)
4177 if (display_type
== DT_GTK
) {
4178 early_gtk_display_init();
4184 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4186 #ifdef CONFIG_VIRTFS
4187 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4192 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4197 /* store value for the future use */
4198 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4200 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4205 machine_opts
= qemu_get_machine_opts();
4206 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4208 object_unref(OBJECT(current_machine
));
4212 configure_accelerator(machine_class
);
4215 Error
*local_err
= NULL
;
4216 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4218 error_report("%s", error_get_pretty(local_err
));
4219 error_free(local_err
);
4224 machine_opts
= qemu_get_machine_opts();
4225 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4226 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4227 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4228 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4230 boot_order
= machine_class
->default_boot_order
;
4231 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4233 char *normal_boot_order
;
4234 const char *order
, *once
;
4236 order
= qemu_opt_get(opts
, "order");
4238 validate_bootdevices(order
);
4242 once
= qemu_opt_get(opts
, "once");
4244 validate_bootdevices(once
);
4245 normal_boot_order
= g_strdup(boot_order
);
4247 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4250 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4251 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4254 if (!kernel_cmdline
) {
4255 kernel_cmdline
= "";
4256 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4259 linux_boot
= (kernel_filename
!= NULL
);
4261 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4262 fprintf(stderr
, "-append only allowed with -kernel option\n");
4266 if (!linux_boot
&& initrd_filename
!= NULL
) {
4267 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4271 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4272 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4276 os_set_line_buffering();
4278 qemu_init_cpu_loop();
4279 qemu_mutex_lock_iothread();
4282 /* spice needs the timers to be initialized by this point */
4286 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4287 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4290 configure_icount(icount_option
);
4292 /* clean up network at qemu process termination */
4293 atexit(&net_cleanup
);
4295 if (net_init_clients() < 0) {
4300 if (tpm_init() < 0) {
4305 /* init the bluetooth world */
4306 if (foreach_device_config(DEV_BT
, bt_parse
))
4309 if (!xen_enabled()) {
4310 /* On 32-bit hosts, QEMU is limited by virtual address space */
4311 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4312 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4320 /* open the virtual block devices */
4322 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4323 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4324 &machine_class
->block_default_type
, 1) != 0) {
4328 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4330 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4331 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4333 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4340 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4344 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4346 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4348 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4350 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4353 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4356 /* If no default VGA is requested, the default is "none". */
4358 if (cirrus_vga_available()) {
4359 vga_model
= "cirrus";
4360 } else if (vga_available()) {
4365 select_vgahw(vga_model
);
4369 i
= select_watchdog(watchdog
);
4371 exit (i
== 1 ? 1 : 0);
4374 if (machine_class
->compat_props
) {
4375 qdev_prop_register_global_list(machine_class
->compat_props
);
4379 qdev_machine_init();
4381 current_machine
->ram_size
= ram_size
;
4382 current_machine
->maxram_size
= maxram_size
;
4383 current_machine
->ram_slots
= ram_slots
;
4384 current_machine
->boot_order
= boot_order
;
4385 current_machine
->cpu_model
= cpu_model
;
4387 machine_class
->init(current_machine
);
4391 cpu_synchronize_all_post_init();
4395 /* init USB devices */
4396 if (usb_enabled(false)) {
4397 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4401 /* init generic devices */
4402 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4405 net_check_clients();
4407 ds
= init_displaystate();
4409 /* init local displays */
4410 switch (display_type
) {
4412 (void)ds
; /* avoid warning if no display is configured */
4414 #if defined(CONFIG_CURSES)
4416 curses_display_init(ds
, full_screen
);
4419 #if defined(CONFIG_SDL)
4421 sdl_display_init(ds
, full_screen
, no_frame
);
4423 #elif defined(CONFIG_COCOA)
4425 cocoa_display_init(ds
, full_screen
);
4428 #if defined(CONFIG_GTK)
4430 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4437 /* must be after terminal init, SDL library changes signal handlers */
4438 os_setup_signal_handling();
4441 /* init remote displays */
4443 Error
*local_err
= NULL
;
4444 vnc_display_init(ds
);
4445 vnc_display_open(ds
, vnc_display
, &local_err
);
4446 if (local_err
!= NULL
) {
4447 error_report("Failed to start VNC server on `%s': %s",
4448 vnc_display
, error_get_pretty(local_err
));
4449 error_free(local_err
);
4453 if (show_vnc_port
) {
4454 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4460 qemu_spice_display_init();
4464 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4468 qdev_machine_creation_done();
4470 if (rom_load_all() != 0) {
4471 fprintf(stderr
, "rom loading failed\n");
4475 /* TODO: once all bus devices are qdevified, this should be done
4476 * when bus is created by qdev.c */
4477 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4478 qemu_run_machine_init_done_notifiers();
4480 /* Done notifiers can load ROMs */
4483 qemu_system_reset(VMRESET_SILENT
);
4485 if (load_vmstate(loadvm
) < 0) {
4490 qdev_prop_check_global();
4493 Error
*local_err
= NULL
;
4494 qemu_start_incoming_migration(incoming
, &local_err
);
4496 error_report("-incoming %s: %s", incoming
,
4497 error_get_pretty(local_err
));
4498 error_free(local_err
);
4501 } else if (autostart
) {
4507 if (is_daemonized()) {
4508 if (!trace_init_backends(trace_events
, trace_file
)) {