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"
121 #define DEFAULT_RAM_SIZE 128
123 #define MAX_VIRTIO_CONSOLES 1
124 #define MAX_SCLP_CONSOLES 1
126 static const char *data_dir
[16];
127 static int data_dir_idx
;
128 const char *bios_name
= NULL
;
129 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
130 DisplayType display_type
= DT_DEFAULT
;
131 static int display_remote
;
132 const char* keyboard_layout
= NULL
;
134 const char *mem_path
= NULL
;
135 int mem_prealloc
= 0; /* force preallocation of physical target memory */
137 NICInfo nd_table
[MAX_NICS
];
139 static int rtc_utc
= 1;
140 static int rtc_date_offset
= -1; /* -1 means no change */
141 QEMUClockType rtc_clock
;
142 int vga_interface_type
= VGA_NONE
;
143 static int full_screen
= 0;
144 static int no_frame
= 0;
147 static bool grab_on_hover
;
149 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
150 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
151 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
152 CharDriverState
*sclp_hds
[MAX_SCLP_CONSOLES
];
153 int win2k_install_hack
= 0;
160 const char *vnc_display
;
162 int acpi_enabled
= 1;
165 static int no_reboot
;
168 int graphic_rotate
= 0;
169 const char *watchdog
;
170 QEMUOptionRom option_rom
[MAX_OPTION_ROMS
];
172 int semihosting_enabled
= 0;
174 const char *qemu_name
;
177 unsigned int nb_prom_envs
= 0;
178 const char *prom_envs
[MAX_PROM_ENVS
];
180 static bool boot_strict
;
181 uint8_t *boot_splash_filedata
;
182 size_t boot_splash_filedata_size
;
183 uint8_t qemu_extra_params_fw
[2];
185 typedef struct FWBootEntry FWBootEntry
;
188 QTAILQ_ENTRY(FWBootEntry
) link
;
194 static QTAILQ_HEAD(, FWBootEntry
) fw_boot_order
=
195 QTAILQ_HEAD_INITIALIZER(fw_boot_order
);
198 NodeInfo numa_info
[MAX_NODES
];
200 uint8_t qemu_uuid
[16];
203 static QEMUBootSetHandler
*boot_set_handler
;
204 static void *boot_set_opaque
;
206 static NotifierList exit_notifiers
=
207 NOTIFIER_LIST_INITIALIZER(exit_notifiers
);
209 static NotifierList machine_init_done_notifiers
=
210 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
212 static bool tcg_allowed
= true;
215 enum xen_mode xen_mode
= XEN_EMULATE
;
216 static int tcg_tb_size
;
218 static int has_defaults
= 1;
219 static int default_serial
= 1;
220 static int default_parallel
= 1;
221 static int default_virtcon
= 1;
222 static int default_sclp
= 1;
223 static int default_monitor
= 1;
224 static int default_floppy
= 1;
225 static int default_cdrom
= 1;
226 static int default_sdcard
= 1;
227 static int default_vga
= 1;
233 { .driver
= "isa-serial", .flag
= &default_serial
},
234 { .driver
= "isa-parallel", .flag
= &default_parallel
},
235 { .driver
= "isa-fdc", .flag
= &default_floppy
},
236 { .driver
= "ide-cd", .flag
= &default_cdrom
},
237 { .driver
= "ide-hd", .flag
= &default_cdrom
},
238 { .driver
= "ide-drive", .flag
= &default_cdrom
},
239 { .driver
= "scsi-cd", .flag
= &default_cdrom
},
240 { .driver
= "virtio-serial-pci", .flag
= &default_virtcon
},
241 { .driver
= "virtio-serial-s390", .flag
= &default_virtcon
},
242 { .driver
= "virtio-serial", .flag
= &default_virtcon
},
243 { .driver
= "VGA", .flag
= &default_vga
},
244 { .driver
= "isa-vga", .flag
= &default_vga
},
245 { .driver
= "cirrus-vga", .flag
= &default_vga
},
246 { .driver
= "isa-cirrus-vga", .flag
= &default_vga
},
247 { .driver
= "vmware-svga", .flag
= &default_vga
},
248 { .driver
= "qxl-vga", .flag
= &default_vga
},
251 static QemuOptsList qemu_rtc_opts
= {
253 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts
.head
),
257 .type
= QEMU_OPT_STRING
,
260 .type
= QEMU_OPT_STRING
,
263 .type
= QEMU_OPT_STRING
,
265 { /* end of list */ }
269 static QemuOptsList qemu_sandbox_opts
= {
271 .implied_opt_name
= "enable",
272 .head
= QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts
.head
),
276 .type
= QEMU_OPT_BOOL
,
278 { /* end of list */ }
282 static QemuOptsList qemu_trace_opts
= {
284 .implied_opt_name
= "trace",
285 .head
= QTAILQ_HEAD_INITIALIZER(qemu_trace_opts
.head
),
289 .type
= QEMU_OPT_STRING
,
292 .type
= QEMU_OPT_STRING
,
294 { /* end of list */ }
298 static QemuOptsList qemu_option_rom_opts
= {
299 .name
= "option-rom",
300 .implied_opt_name
= "romfile",
301 .head
= QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts
.head
),
305 .type
= QEMU_OPT_NUMBER
,
308 .type
= QEMU_OPT_STRING
,
310 { /* end of list */ }
314 static QemuOptsList qemu_machine_opts
= {
316 .implied_opt_name
= "type",
318 .head
= QTAILQ_HEAD_INITIALIZER(qemu_machine_opts
.head
),
322 .type
= QEMU_OPT_STRING
,
323 .help
= "emulated machine"
326 .type
= QEMU_OPT_STRING
,
327 .help
= "accelerator list",
329 .name
= "kernel_irqchip",
330 .type
= QEMU_OPT_BOOL
,
331 .help
= "use KVM in-kernel irqchip",
333 .name
= "kvm_shadow_mem",
334 .type
= QEMU_OPT_SIZE
,
335 .help
= "KVM shadow MMU size",
338 .type
= QEMU_OPT_STRING
,
339 .help
= "Linux kernel image file",
342 .type
= QEMU_OPT_STRING
,
343 .help
= "Linux initial ramdisk file",
346 .type
= QEMU_OPT_STRING
,
347 .help
= "Linux kernel command line",
350 .type
= QEMU_OPT_STRING
,
351 .help
= "Linux kernel device tree file",
354 .type
= QEMU_OPT_STRING
,
355 .help
= "Dump current dtb to a file and quit",
357 .name
= "phandle_start",
358 .type
= QEMU_OPT_NUMBER
,
359 .help
= "The first phandle ID we may generate dynamically",
361 .name
= "dt_compatible",
362 .type
= QEMU_OPT_STRING
,
363 .help
= "Overrides the \"compatible\" property of the dt root node",
365 .name
= "dump-guest-core",
366 .type
= QEMU_OPT_BOOL
,
367 .help
= "Include guest memory in a core dump",
370 .type
= QEMU_OPT_BOOL
,
371 .help
= "enable/disable memory merge support",
374 .type
= QEMU_OPT_BOOL
,
375 .help
= "Set on/off to enable/disable usb",
378 .type
= QEMU_OPT_STRING
,
379 .help
= "firmware image",
382 .type
= QEMU_OPT_STRING
,
383 .help
= "Specifies the KVM virtualization mode (HV, PR)",
385 { /* End of list */ }
389 static QemuOptsList qemu_boot_opts
= {
391 .implied_opt_name
= "order",
393 .head
= QTAILQ_HEAD_INITIALIZER(qemu_boot_opts
.head
),
397 .type
= QEMU_OPT_STRING
,
400 .type
= QEMU_OPT_STRING
,
403 .type
= QEMU_OPT_BOOL
,
406 .type
= QEMU_OPT_STRING
,
408 .name
= "splash-time",
409 .type
= QEMU_OPT_STRING
,
411 .name
= "reboot-timeout",
412 .type
= QEMU_OPT_STRING
,
415 .type
= QEMU_OPT_BOOL
,
421 static QemuOptsList qemu_add_fd_opts
= {
423 .head
= QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts
.head
),
427 .type
= QEMU_OPT_NUMBER
,
428 .help
= "file descriptor of which a duplicate is added to fd set",
431 .type
= QEMU_OPT_NUMBER
,
432 .help
= "ID of the fd set to add fd to",
435 .type
= QEMU_OPT_STRING
,
436 .help
= "free-form string used to describe fd",
438 { /* end of list */ }
442 static QemuOptsList qemu_object_opts
= {
444 .implied_opt_name
= "qom-type",
445 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
451 static QemuOptsList qemu_tpmdev_opts
= {
453 .implied_opt_name
= "type",
454 .head
= QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts
.head
),
456 /* options are defined in the TPM backends */
457 { /* end of list */ }
461 static QemuOptsList qemu_realtime_opts
= {
463 .head
= QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts
.head
),
467 .type
= QEMU_OPT_BOOL
,
469 { /* end of list */ }
473 static QemuOptsList qemu_msg_opts
= {
475 .head
= QTAILQ_HEAD_INITIALIZER(qemu_msg_opts
.head
),
479 .type
= QEMU_OPT_BOOL
,
481 { /* end of list */ }
485 static QemuOptsList qemu_name_opts
= {
487 .implied_opt_name
= "guest",
489 .head
= QTAILQ_HEAD_INITIALIZER(qemu_name_opts
.head
),
493 .type
= QEMU_OPT_STRING
,
494 .help
= "Sets the name of the guest.\n"
495 "This name will be displayed in the SDL window caption.\n"
496 "The name will also be used for the VNC server",
499 .type
= QEMU_OPT_STRING
,
500 .help
= "Sets the name of the QEMU process, as shown in top etc",
502 .name
= "debug-threads",
503 .type
= QEMU_OPT_BOOL
,
504 .help
= "When enabled, name the individual threads; defaults off.\n"
505 "NOTE: The thread names are for debugging and not a\n"
508 { /* End of list */ }
512 static QemuOptsList qemu_mem_opts
= {
514 .implied_opt_name
= "size",
515 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mem_opts
.head
),
520 .type
= QEMU_OPT_SIZE
,
524 .type
= QEMU_OPT_NUMBER
,
528 .type
= QEMU_OPT_SIZE
,
530 { /* end of list */ }
535 * Get machine options
537 * Returns: machine options (never null).
539 QemuOpts
*qemu_get_machine_opts(void)
541 return qemu_find_opts_singleton("machine");
544 const char *qemu_get_vm_name(void)
549 static void res_free(void)
551 if (boot_splash_filedata
!= NULL
) {
552 g_free(boot_splash_filedata
);
553 boot_splash_filedata
= NULL
;
557 static int default_driver_check(QemuOpts
*opts
, void *opaque
)
559 const char *driver
= qemu_opt_get(opts
, "driver");
564 for (i
= 0; i
< ARRAY_SIZE(default_list
); i
++) {
565 if (strcmp(default_list
[i
].driver
, driver
) != 0)
567 *(default_list
[i
].flag
) = 0;
572 /***********************************************************/
575 static RunState current_run_state
= RUN_STATE_PRELAUNCH
;
580 } RunStateTransition
;
582 static const RunStateTransition runstate_transitions_def
[] = {
584 { RUN_STATE_DEBUG
, RUN_STATE_RUNNING
},
585 { RUN_STATE_DEBUG
, RUN_STATE_FINISH_MIGRATE
},
587 { RUN_STATE_INMIGRATE
, RUN_STATE_RUNNING
},
588 { RUN_STATE_INMIGRATE
, RUN_STATE_PAUSED
},
590 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_PAUSED
},
591 { RUN_STATE_INTERNAL_ERROR
, RUN_STATE_FINISH_MIGRATE
},
593 { RUN_STATE_IO_ERROR
, RUN_STATE_RUNNING
},
594 { RUN_STATE_IO_ERROR
, RUN_STATE_FINISH_MIGRATE
},
596 { RUN_STATE_PAUSED
, RUN_STATE_RUNNING
},
597 { RUN_STATE_PAUSED
, RUN_STATE_FINISH_MIGRATE
},
599 { RUN_STATE_POSTMIGRATE
, RUN_STATE_RUNNING
},
600 { RUN_STATE_POSTMIGRATE
, RUN_STATE_FINISH_MIGRATE
},
602 { RUN_STATE_PRELAUNCH
, RUN_STATE_RUNNING
},
603 { RUN_STATE_PRELAUNCH
, RUN_STATE_FINISH_MIGRATE
},
604 { RUN_STATE_PRELAUNCH
, RUN_STATE_INMIGRATE
},
606 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_RUNNING
},
607 { RUN_STATE_FINISH_MIGRATE
, RUN_STATE_POSTMIGRATE
},
609 { RUN_STATE_RESTORE_VM
, RUN_STATE_RUNNING
},
611 { RUN_STATE_RUNNING
, RUN_STATE_DEBUG
},
612 { RUN_STATE_RUNNING
, RUN_STATE_INTERNAL_ERROR
},
613 { RUN_STATE_RUNNING
, RUN_STATE_IO_ERROR
},
614 { RUN_STATE_RUNNING
, RUN_STATE_PAUSED
},
615 { RUN_STATE_RUNNING
, RUN_STATE_FINISH_MIGRATE
},
616 { RUN_STATE_RUNNING
, RUN_STATE_RESTORE_VM
},
617 { RUN_STATE_RUNNING
, RUN_STATE_SAVE_VM
},
618 { RUN_STATE_RUNNING
, RUN_STATE_SHUTDOWN
},
619 { RUN_STATE_RUNNING
, RUN_STATE_WATCHDOG
},
620 { RUN_STATE_RUNNING
, RUN_STATE_GUEST_PANICKED
},
622 { RUN_STATE_SAVE_VM
, RUN_STATE_RUNNING
},
624 { RUN_STATE_SHUTDOWN
, RUN_STATE_PAUSED
},
625 { RUN_STATE_SHUTDOWN
, RUN_STATE_FINISH_MIGRATE
},
627 { RUN_STATE_DEBUG
, RUN_STATE_SUSPENDED
},
628 { RUN_STATE_RUNNING
, RUN_STATE_SUSPENDED
},
629 { RUN_STATE_SUSPENDED
, RUN_STATE_RUNNING
},
630 { RUN_STATE_SUSPENDED
, RUN_STATE_FINISH_MIGRATE
},
632 { RUN_STATE_WATCHDOG
, RUN_STATE_RUNNING
},
633 { RUN_STATE_WATCHDOG
, RUN_STATE_FINISH_MIGRATE
},
635 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_RUNNING
},
636 { RUN_STATE_GUEST_PANICKED
, RUN_STATE_FINISH_MIGRATE
},
638 { RUN_STATE_MAX
, RUN_STATE_MAX
},
641 static bool runstate_valid_transitions
[RUN_STATE_MAX
][RUN_STATE_MAX
];
643 bool runstate_check(RunState state
)
645 return current_run_state
== state
;
648 static void runstate_init(void)
650 const RunStateTransition
*p
;
652 memset(&runstate_valid_transitions
, 0, sizeof(runstate_valid_transitions
));
654 for (p
= &runstate_transitions_def
[0]; p
->from
!= RUN_STATE_MAX
; p
++) {
655 runstate_valid_transitions
[p
->from
][p
->to
] = true;
659 /* This function will abort() on invalid state transitions */
660 void runstate_set(RunState new_state
)
662 assert(new_state
< RUN_STATE_MAX
);
664 if (!runstate_valid_transitions
[current_run_state
][new_state
]) {
665 fprintf(stderr
, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
666 RunState_lookup
[current_run_state
],
667 RunState_lookup
[new_state
]);
670 trace_runstate_set(new_state
);
671 current_run_state
= new_state
;
674 int runstate_is_running(void)
676 return runstate_check(RUN_STATE_RUNNING
);
679 bool runstate_needs_reset(void)
681 return runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
682 runstate_check(RUN_STATE_SHUTDOWN
);
685 StatusInfo
*qmp_query_status(Error
**errp
)
687 StatusInfo
*info
= g_malloc0(sizeof(*info
));
689 info
->running
= runstate_is_running();
690 info
->singlestep
= singlestep
;
691 info
->status
= current_run_state
;
696 /***********************************************************/
697 /* real time host monotonic timer */
699 /***********************************************************/
700 /* host time/date access */
701 void qemu_get_timedate(struct tm
*tm
, int offset
)
707 if (rtc_date_offset
== -1) {
711 localtime_r(&ti
, tm
);
713 ti
-= rtc_date_offset
;
718 int qemu_timedate_diff(struct tm
*tm
)
722 if (rtc_date_offset
== -1)
724 seconds
= mktimegm(tm
);
727 tmp
.tm_isdst
= -1; /* use timezone to figure it out */
728 seconds
= mktime(&tmp
);
731 seconds
= mktimegm(tm
) + rtc_date_offset
;
733 return seconds
- time(NULL
);
736 void rtc_change_mon_event(struct tm
*tm
)
740 data
= qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm
));
741 monitor_protocol_event(QEVENT_RTC_CHANGE
, data
);
742 qobject_decref(data
);
745 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
747 time_t rtc_start_date
;
750 if (!strcmp(startdate
, "now") && legacy
) {
751 rtc_date_offset
= -1;
753 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
761 } else if (sscanf(startdate
, "%d-%d-%d",
773 rtc_start_date
= mktimegm(&tm
);
774 if (rtc_start_date
== -1) {
776 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
777 "'2006-06-17T16:01:21' or '2006-06-17'\n");
780 rtc_date_offset
= time(NULL
) - rtc_start_date
;
784 static void configure_rtc(QemuOpts
*opts
)
788 value
= qemu_opt_get(opts
, "base");
790 if (!strcmp(value
, "utc")) {
792 } else if (!strcmp(value
, "localtime")) {
795 configure_rtc_date_offset(value
, 0);
798 value
= qemu_opt_get(opts
, "clock");
800 if (!strcmp(value
, "host")) {
801 rtc_clock
= QEMU_CLOCK_HOST
;
802 } else if (!strcmp(value
, "rt")) {
803 rtc_clock
= QEMU_CLOCK_REALTIME
;
804 } else if (!strcmp(value
, "vm")) {
805 rtc_clock
= QEMU_CLOCK_VIRTUAL
;
807 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
811 value
= qemu_opt_get(opts
, "driftfix");
813 if (!strcmp(value
, "slew")) {
814 static GlobalProperty slew_lost_ticks
[] = {
816 .driver
= "mc146818rtc",
817 .property
= "lost_tick_policy",
820 { /* end of list */ }
823 qdev_prop_register_global_list(slew_lost_ticks
);
824 } else if (!strcmp(value
, "none")) {
825 /* discard is default */
827 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
833 /***********************************************************/
834 /* Bluetooth support */
837 static struct HCIInfo
*hci_table
[MAX_NICS
];
839 struct HCIInfo
*qemu_next_hci(void)
841 if (cur_hci
== nb_hcis
)
844 return hci_table
[cur_hci
++];
847 static int bt_hci_parse(const char *str
)
852 if (nb_hcis
>= MAX_NICS
) {
853 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
866 bdaddr
.b
[5] = 0x56 + nb_hcis
;
867 hci
->bdaddr_set(hci
, bdaddr
.b
);
869 hci_table
[nb_hcis
++] = hci
;
874 static void bt_vhci_add(int vlan_id
)
876 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
879 fprintf(stderr
, "qemu: warning: adding a VHCI to "
880 "an empty scatternet %i\n", vlan_id
);
882 bt_vhci_init(bt_new_hci(vlan
));
885 static struct bt_device_s
*bt_device_add(const char *opt
)
887 struct bt_scatternet_s
*vlan
;
889 char *endp
= strstr(opt
, ",vlan=");
890 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
893 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
896 vlan_id
= strtol(endp
+ 6, &endp
, 0);
898 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
903 vlan
= qemu_find_bt_vlan(vlan_id
);
906 fprintf(stderr
, "qemu: warning: adding a slave device to "
907 "an empty scatternet %i\n", vlan_id
);
909 if (!strcmp(devname
, "keyboard"))
910 return bt_keyboard_init(vlan
);
912 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
916 static int bt_parse(const char *opt
)
918 const char *endp
, *p
;
921 if (strstart(opt
, "hci", &endp
)) {
922 if (!*endp
|| *endp
== ',') {
924 if (!strstart(endp
, ",vlan=", 0))
927 return bt_hci_parse(opt
);
929 } else if (strstart(opt
, "vhci", &endp
)) {
930 if (!*endp
|| *endp
== ',') {
932 if (strstart(endp
, ",vlan=", &p
)) {
933 vlan
= strtol(p
, (char **) &endp
, 0);
935 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
939 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
948 } else if (strstart(opt
, "device:", &endp
))
949 return !bt_device_add(endp
);
951 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
955 static int parse_sandbox(QemuOpts
*opts
, void *opaque
)
957 /* FIXME: change this to true for 1.3 */
958 if (qemu_opt_get_bool(opts
, "enable", false)) {
959 #ifdef CONFIG_SECCOMP
960 if (seccomp_start() < 0) {
961 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
962 "failed to install seccomp syscall filter in the kernel");
966 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
967 "sandboxing request but seccomp is not compiled into this build");
975 static int parse_name(QemuOpts
*opts
, void *opaque
)
977 const char *proc_name
;
979 if (qemu_opt_get(opts
, "debug-threads")) {
980 qemu_thread_naming(qemu_opt_get_bool(opts
, "debug-threads", false));
982 qemu_name
= qemu_opt_get(opts
, "guest");
984 proc_name
= qemu_opt_get(opts
, "process");
986 os_set_proc_name(proc_name
);
992 bool usb_enabled(bool default_usb
)
994 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
995 has_defaults
&& default_usb
);
999 static int parse_add_fd(QemuOpts
*opts
, void *opaque
)
1001 int fd
, dupfd
, flags
;
1003 const char *fd_opaque
= NULL
;
1005 fd
= qemu_opt_get_number(opts
, "fd", -1);
1006 fdset_id
= qemu_opt_get_number(opts
, "set", -1);
1007 fd_opaque
= qemu_opt_get(opts
, "opaque");
1010 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1011 "fd option is required and must be non-negative");
1015 if (fd
<= STDERR_FILENO
) {
1016 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1017 "fd cannot be a standard I/O stream");
1022 * All fds inherited across exec() necessarily have FD_CLOEXEC
1023 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1025 flags
= fcntl(fd
, F_GETFD
);
1026 if (flags
== -1 || (flags
& FD_CLOEXEC
)) {
1027 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1028 "fd is not valid or already in use");
1033 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1034 "set option is required and must be non-negative");
1038 #ifdef F_DUPFD_CLOEXEC
1039 dupfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 0);
1043 qemu_set_cloexec(dupfd
);
1047 qerror_report(ERROR_CLASS_GENERIC_ERROR
,
1048 "Error duplicating fd: %s", strerror(errno
));
1052 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1053 monitor_fdset_add_fd(dupfd
, true, fdset_id
, fd_opaque
? true : false,
1059 static int cleanup_add_fd(QemuOpts
*opts
, void *opaque
)
1063 fd
= qemu_opt_get_number(opts
, "fd", -1);
1070 /***********************************************************/
1071 /* QEMU Block devices */
1073 #define HD_OPTS "media=disk"
1074 #define CDROM_OPTS "media=cdrom"
1076 #define PFLASH_OPTS ""
1080 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
1082 BlockInterfaceType
*block_default_type
= opaque
;
1084 return drive_new(opts
, *block_default_type
) == NULL
;
1087 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
1089 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
1090 qemu_opt_set(opts
, "snapshot", "on");
1095 static void default_drive(int enable
, int snapshot
, BlockInterfaceType type
,
1096 int index
, const char *optstr
)
1100 if (!enable
|| drive_get_by_index(type
, index
)) {
1104 opts
= drive_add(type
, index
, NULL
, optstr
);
1106 drive_enable_snapshot(opts
, NULL
);
1108 if (!drive_new(opts
, type
)) {
1113 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1115 boot_set_handler
= func
;
1116 boot_set_opaque
= opaque
;
1119 int qemu_boot_set(const char *boot_order
)
1121 if (!boot_set_handler
) {
1124 return boot_set_handler(boot_set_opaque
, boot_order
);
1127 static void validate_bootdevices(const char *devices
)
1129 /* We just do some generic consistency checks */
1133 for (p
= devices
; *p
!= '\0'; p
++) {
1134 /* Allowed boot devices are:
1135 * a-b: floppy disk drives
1136 * c-f: IDE disk drives
1137 * g-m: machine implementation dependent drives
1138 * n-p: network devices
1139 * It's up to each machine implementation to check if the given boot
1140 * devices match the actual hardware implementation and firmware
1143 if (*p
< 'a' || *p
> 'p') {
1144 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
1147 if (bitmap
& (1 << (*p
- 'a'))) {
1148 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
1151 bitmap
|= 1 << (*p
- 'a');
1155 static void restore_boot_order(void *opaque
)
1157 char *normal_boot_order
= opaque
;
1158 static int first
= 1;
1160 /* Restore boot order and remove ourselves after the first boot */
1166 qemu_boot_set(normal_boot_order
);
1168 qemu_unregister_reset(restore_boot_order
, normal_boot_order
);
1169 g_free(normal_boot_order
);
1172 void add_boot_device_path(int32_t bootindex
, DeviceState
*dev
,
1175 FWBootEntry
*node
, *i
;
1177 if (bootindex
< 0) {
1181 assert(dev
!= NULL
|| suffix
!= NULL
);
1183 node
= g_malloc0(sizeof(FWBootEntry
));
1184 node
->bootindex
= bootindex
;
1185 node
->suffix
= g_strdup(suffix
);
1188 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1189 if (i
->bootindex
== bootindex
) {
1190 fprintf(stderr
, "Two devices with same boot index %d\n", bootindex
);
1192 } else if (i
->bootindex
< bootindex
) {
1195 QTAILQ_INSERT_BEFORE(i
, node
, link
);
1198 QTAILQ_INSERT_TAIL(&fw_boot_order
, node
, link
);
1201 DeviceState
*get_boot_device(uint32_t position
)
1203 uint32_t counter
= 0;
1204 FWBootEntry
*i
= NULL
;
1205 DeviceState
*res
= NULL
;
1207 if (!QTAILQ_EMPTY(&fw_boot_order
)) {
1208 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1209 if (counter
== position
) {
1220 * This function returns null terminated string that consist of new line
1221 * separated device paths.
1223 * memory pointed by "size" is assigned total length of the array in bytes
1226 char *get_boot_devices_list(size_t *size
, bool ignore_suffixes
)
1232 QTAILQ_FOREACH(i
, &fw_boot_order
, link
) {
1233 char *devpath
= NULL
, *bootpath
;
1237 devpath
= qdev_get_fw_dev_path(i
->dev
);
1241 if (i
->suffix
&& !ignore_suffixes
&& devpath
) {
1242 size_t bootpathlen
= strlen(devpath
) + strlen(i
->suffix
) + 1;
1244 bootpath
= g_malloc(bootpathlen
);
1245 snprintf(bootpath
, bootpathlen
, "%s%s", devpath
, i
->suffix
);
1247 } else if (devpath
) {
1249 } else if (!ignore_suffixes
) {
1251 bootpath
= g_strdup(i
->suffix
);
1253 bootpath
= g_strdup("");
1257 list
[total
-1] = '\n';
1259 len
= strlen(bootpath
) + 1;
1260 list
= g_realloc(list
, total
+ len
);
1261 memcpy(&list
[total
], bootpath
, len
);
1268 if (boot_strict
&& *size
> 0) {
1269 list
[total
-1] = '\n';
1270 list
= g_realloc(list
, total
+ 5);
1271 memcpy(&list
[total
], "HALT", 5);
1277 static QemuOptsList qemu_smp_opts
= {
1279 .implied_opt_name
= "cpus",
1280 .merge_lists
= true,
1281 .head
= QTAILQ_HEAD_INITIALIZER(qemu_smp_opts
.head
),
1285 .type
= QEMU_OPT_NUMBER
,
1288 .type
= QEMU_OPT_NUMBER
,
1291 .type
= QEMU_OPT_NUMBER
,
1294 .type
= QEMU_OPT_NUMBER
,
1297 .type
= QEMU_OPT_NUMBER
,
1299 { /*End of list */ }
1303 static void smp_parse(QemuOpts
*opts
)
1307 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
1308 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
1309 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
1310 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
1312 /* compute missing values, prefer sockets over cores over threads */
1313 if (cpus
== 0 || sockets
== 0) {
1314 sockets
= sockets
> 0 ? sockets
: 1;
1315 cores
= cores
> 0 ? cores
: 1;
1316 threads
= threads
> 0 ? threads
: 1;
1318 cpus
= cores
* threads
* sockets
;
1322 threads
= threads
> 0 ? threads
: 1;
1323 cores
= cpus
/ (sockets
* threads
);
1325 threads
= cpus
/ (cores
* sockets
);
1329 max_cpus
= qemu_opt_get_number(opts
, "maxcpus", 0);
1332 smp_cores
= cores
> 0 ? cores
: 1;
1333 smp_threads
= threads
> 0 ? threads
: 1;
1337 if (max_cpus
== 0) {
1338 max_cpus
= smp_cpus
;
1341 if (max_cpus
> MAX_CPUMASK_BITS
) {
1342 fprintf(stderr
, "Unsupported number of maxcpus\n");
1345 if (max_cpus
< smp_cpus
) {
1346 fprintf(stderr
, "maxcpus must be equal to or greater than smp\n");
1352 static void configure_realtime(QemuOpts
*opts
)
1356 enable_mlock
= qemu_opt_get_bool(opts
, "mlock", true);
1359 if (os_mlock() < 0) {
1360 fprintf(stderr
, "qemu: locking memory failed\n");
1367 static void configure_msg(QemuOpts
*opts
)
1369 enable_timestamp_msg
= qemu_opt_get_bool(opts
, "timestamp", true);
1372 /***********************************************************/
1375 static int usb_device_add(const char *devname
)
1377 USBDevice
*dev
= NULL
;
1378 #ifndef CONFIG_LINUX
1382 if (!usb_enabled(false)) {
1386 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1387 dev
= usbdevice_create(devname
);
1391 /* the other ones */
1392 #ifndef CONFIG_LINUX
1393 /* only the linux version is qdev-ified, usb-bsd still needs this */
1394 if (strstart(devname
, "host:", &p
)) {
1395 dev
= usb_host_device_open(usb_bus_find(-1), p
);
1405 static int usb_device_del(const char *devname
)
1410 if (strstart(devname
, "host:", &p
)) {
1414 if (!usb_enabled(false)) {
1418 p
= strchr(devname
, '.');
1421 bus_num
= strtoul(devname
, NULL
, 0);
1422 addr
= strtoul(p
+ 1, NULL
, 0);
1424 return usb_device_delete_addr(bus_num
, addr
);
1427 static int usb_parse(const char *cmdline
)
1430 r
= usb_device_add(cmdline
);
1432 fprintf(stderr
, "qemu: could not add USB device '%s'\n", cmdline
);
1437 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
1439 const char *devname
= qdict_get_str(qdict
, "devname");
1440 if (usb_device_add(devname
) < 0) {
1441 error_report("could not add USB device '%s'", devname
);
1445 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
1447 const char *devname
= qdict_get_str(qdict
, "devname");
1448 if (usb_device_del(devname
) < 0) {
1449 error_report("could not delete USB device '%s'", devname
);
1453 /***********************************************************/
1454 /* PCMCIA/Cardbus */
1456 static struct pcmcia_socket_entry_s
{
1457 PCMCIASocket
*socket
;
1458 struct pcmcia_socket_entry_s
*next
;
1459 } *pcmcia_sockets
= 0;
1461 void pcmcia_socket_register(PCMCIASocket
*socket
)
1463 struct pcmcia_socket_entry_s
*entry
;
1465 entry
= g_malloc(sizeof(struct pcmcia_socket_entry_s
));
1466 entry
->socket
= socket
;
1467 entry
->next
= pcmcia_sockets
;
1468 pcmcia_sockets
= entry
;
1471 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
1473 struct pcmcia_socket_entry_s
*entry
, **ptr
;
1475 ptr
= &pcmcia_sockets
;
1476 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
1477 if (entry
->socket
== socket
) {
1483 void pcmcia_info(Monitor
*mon
, const QDict
*qdict
)
1485 struct pcmcia_socket_entry_s
*iter
;
1487 if (!pcmcia_sockets
)
1488 monitor_printf(mon
, "No PCMCIA sockets\n");
1490 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
1491 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
1492 iter
->socket
->attached
? iter
->socket
->card_string
:
1496 /***********************************************************/
1497 /* machine registration */
1499 MachineState
*current_machine
;
1501 static void machine_class_init(ObjectClass
*oc
, void *data
)
1503 MachineClass
*mc
= MACHINE_CLASS(oc
);
1504 QEMUMachine
*qm
= data
;
1506 mc
->name
= qm
->name
;
1507 mc
->alias
= qm
->alias
;
1508 mc
->desc
= qm
->desc
;
1509 mc
->init
= qm
->init
;
1510 mc
->reset
= qm
->reset
;
1511 mc
->hot_add_cpu
= qm
->hot_add_cpu
;
1512 mc
->kvm_type
= qm
->kvm_type
;
1513 mc
->block_default_type
= qm
->block_default_type
;
1514 mc
->max_cpus
= qm
->max_cpus
;
1515 mc
->no_serial
= qm
->no_serial
;
1516 mc
->no_parallel
= qm
->no_parallel
;
1517 mc
->use_virtcon
= qm
->use_virtcon
;
1518 mc
->use_sclp
= qm
->use_sclp
;
1519 mc
->no_floppy
= qm
->no_floppy
;
1520 mc
->no_cdrom
= qm
->no_cdrom
;
1521 mc
->no_sdcard
= qm
->no_sdcard
;
1522 mc
->is_default
= qm
->is_default
;
1523 mc
->default_machine_opts
= qm
->default_machine_opts
;
1524 mc
->default_boot_order
= qm
->default_boot_order
;
1525 mc
->compat_props
= qm
->compat_props
;
1526 mc
->hw_version
= qm
->hw_version
;
1529 int qemu_register_machine(QEMUMachine
*m
)
1531 char *name
= g_strconcat(m
->name
, TYPE_MACHINE_SUFFIX
, NULL
);
1534 .parent
= TYPE_MACHINE
,
1535 .class_init
= machine_class_init
,
1536 .class_data
= (void *)m
,
1545 static MachineClass
*find_machine(const char *name
)
1547 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1548 MachineClass
*mc
= NULL
;
1550 for (el
= machines
; el
; el
= el
->next
) {
1551 MachineClass
*temp
= el
->data
;
1553 if (!strcmp(temp
->name
, name
)) {
1558 !strcmp(temp
->alias
, name
)) {
1564 g_slist_free(machines
);
1568 MachineClass
*find_default_machine(void)
1570 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1571 MachineClass
*mc
= NULL
;
1573 for (el
= machines
; el
; el
= el
->next
) {
1574 MachineClass
*temp
= el
->data
;
1576 if (temp
->is_default
) {
1582 g_slist_free(machines
);
1586 MachineInfoList
*qmp_query_machines(Error
**errp
)
1588 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
1589 MachineInfoList
*mach_list
= NULL
;
1591 for (el
= machines
; el
; el
= el
->next
) {
1592 MachineClass
*mc
= el
->data
;
1593 MachineInfoList
*entry
;
1596 info
= g_malloc0(sizeof(*info
));
1597 if (mc
->is_default
) {
1598 info
->has_is_default
= true;
1599 info
->is_default
= true;
1603 info
->has_alias
= true;
1604 info
->alias
= g_strdup(mc
->alias
);
1607 info
->name
= g_strdup(mc
->name
);
1608 info
->cpu_max
= !mc
->max_cpus
? 1 : mc
->max_cpus
;
1610 entry
= g_malloc0(sizeof(*entry
));
1611 entry
->value
= info
;
1612 entry
->next
= mach_list
;
1616 g_slist_free(machines
);
1620 /***********************************************************/
1621 /* main execution loop */
1623 struct vm_change_state_entry
{
1624 VMChangeStateHandler
*cb
;
1626 QLIST_ENTRY (vm_change_state_entry
) entries
;
1629 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
1631 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
1634 VMChangeStateEntry
*e
;
1636 e
= g_malloc0(sizeof (*e
));
1640 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
1644 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
1646 QLIST_REMOVE (e
, entries
);
1650 void vm_state_notify(int running
, RunState state
)
1652 VMChangeStateEntry
*e
;
1654 trace_vm_state_notify(running
, state
);
1656 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
1657 e
->cb(e
->opaque
, running
, state
);
1663 if (!runstate_is_running()) {
1665 runstate_set(RUN_STATE_RUNNING
);
1666 vm_state_notify(1, RUN_STATE_RUNNING
);
1668 monitor_protocol_event(QEVENT_RESUME
, NULL
);
1672 /* reset/shutdown handler */
1674 typedef struct QEMUResetEntry
{
1675 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
1676 QEMUResetHandler
*func
;
1680 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
1681 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
1682 static int reset_requested
;
1683 static int shutdown_requested
, shutdown_signal
= -1;
1684 static pid_t shutdown_pid
;
1685 static int powerdown_requested
;
1686 static int debug_requested
;
1687 static int suspend_requested
;
1688 static WakeupReason wakeup_reason
;
1689 static NotifierList powerdown_notifiers
=
1690 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers
);
1691 static NotifierList suspend_notifiers
=
1692 NOTIFIER_LIST_INITIALIZER(suspend_notifiers
);
1693 static NotifierList wakeup_notifiers
=
1694 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers
);
1695 static uint32_t wakeup_reason_mask
= ~(1 << QEMU_WAKEUP_REASON_NONE
);
1696 static RunState vmstop_requested
= RUN_STATE_MAX
;
1698 int qemu_shutdown_requested_get(void)
1700 return shutdown_requested
;
1703 int qemu_reset_requested_get(void)
1705 return reset_requested
;
1708 static int qemu_shutdown_requested(void)
1710 int r
= shutdown_requested
;
1711 shutdown_requested
= 0;
1715 static void qemu_kill_report(void)
1717 if (!qtest_driver() && shutdown_signal
!= -1) {
1718 fprintf(stderr
, "qemu: terminating on signal %d", shutdown_signal
);
1719 if (shutdown_pid
== 0) {
1720 /* This happens for eg ^C at the terminal, so it's worth
1721 * avoiding printing an odd message in that case.
1723 fputc('\n', stderr
);
1725 fprintf(stderr
, " from pid " FMT_pid
"\n", shutdown_pid
);
1727 shutdown_signal
= -1;
1731 static int qemu_reset_requested(void)
1733 int r
= reset_requested
;
1734 reset_requested
= 0;
1738 static int qemu_suspend_requested(void)
1740 int r
= suspend_requested
;
1741 suspend_requested
= 0;
1745 static WakeupReason
qemu_wakeup_requested(void)
1747 return wakeup_reason
;
1750 static int qemu_powerdown_requested(void)
1752 int r
= powerdown_requested
;
1753 powerdown_requested
= 0;
1757 static int qemu_debug_requested(void)
1759 int r
= debug_requested
;
1760 debug_requested
= 0;
1764 /* We use RUN_STATE_MAX but any invalid value will do */
1765 static bool qemu_vmstop_requested(RunState
*r
)
1767 if (vmstop_requested
< RUN_STATE_MAX
) {
1768 *r
= vmstop_requested
;
1769 vmstop_requested
= RUN_STATE_MAX
;
1776 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1778 QEMUResetEntry
*re
= g_malloc0(sizeof(QEMUResetEntry
));
1781 re
->opaque
= opaque
;
1782 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
1785 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
1789 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
1790 if (re
->func
== func
&& re
->opaque
== opaque
) {
1791 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
1798 void qemu_devices_reset(void)
1800 QEMUResetEntry
*re
, *nre
;
1802 /* reset all devices */
1803 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
1804 re
->func(re
->opaque
);
1808 void qemu_system_reset(bool report
)
1812 mc
= current_machine
? MACHINE_GET_CLASS(current_machine
) : NULL
;
1814 if (mc
&& mc
->reset
) {
1817 qemu_devices_reset();
1820 monitor_protocol_event(QEVENT_RESET
, NULL
);
1822 cpu_synchronize_all_post_reset();
1825 void qemu_system_reset_request(void)
1828 shutdown_requested
= 1;
1830 reset_requested
= 1;
1833 qemu_notify_event();
1836 static void qemu_system_suspend(void)
1839 notifier_list_notify(&suspend_notifiers
, NULL
);
1840 runstate_set(RUN_STATE_SUSPENDED
);
1841 monitor_protocol_event(QEVENT_SUSPEND
, NULL
);
1844 void qemu_system_suspend_request(void)
1846 if (runstate_check(RUN_STATE_SUSPENDED
)) {
1849 suspend_requested
= 1;
1851 qemu_notify_event();
1854 void qemu_register_suspend_notifier(Notifier
*notifier
)
1856 notifier_list_add(&suspend_notifiers
, notifier
);
1859 void qemu_system_wakeup_request(WakeupReason reason
)
1861 trace_system_wakeup_request(reason
);
1863 if (!runstate_check(RUN_STATE_SUSPENDED
)) {
1866 if (!(wakeup_reason_mask
& (1 << reason
))) {
1869 runstate_set(RUN_STATE_RUNNING
);
1870 wakeup_reason
= reason
;
1871 qemu_notify_event();
1874 void qemu_system_wakeup_enable(WakeupReason reason
, bool enabled
)
1877 wakeup_reason_mask
|= (1 << reason
);
1879 wakeup_reason_mask
&= ~(1 << reason
);
1883 void qemu_register_wakeup_notifier(Notifier
*notifier
)
1885 notifier_list_add(&wakeup_notifiers
, notifier
);
1888 void qemu_system_killed(int signal
, pid_t pid
)
1890 shutdown_signal
= signal
;
1893 qemu_system_shutdown_request();
1896 void qemu_system_shutdown_request(void)
1898 shutdown_requested
= 1;
1899 qemu_notify_event();
1902 static void qemu_system_powerdown(void)
1904 monitor_protocol_event(QEVENT_POWERDOWN
, NULL
);
1905 notifier_list_notify(&powerdown_notifiers
, NULL
);
1908 void qemu_system_powerdown_request(void)
1910 powerdown_requested
= 1;
1911 qemu_notify_event();
1914 void qemu_register_powerdown_notifier(Notifier
*notifier
)
1916 notifier_list_add(&powerdown_notifiers
, notifier
);
1919 void qemu_system_debug_request(void)
1921 debug_requested
= 1;
1922 qemu_notify_event();
1925 void qemu_system_vmstop_request(RunState state
)
1927 vmstop_requested
= state
;
1928 qemu_notify_event();
1931 static bool main_loop_should_exit(void)
1934 if (qemu_debug_requested()) {
1935 vm_stop(RUN_STATE_DEBUG
);
1937 if (qemu_suspend_requested()) {
1938 qemu_system_suspend();
1940 if (qemu_shutdown_requested()) {
1942 monitor_protocol_event(QEVENT_SHUTDOWN
, NULL
);
1944 vm_stop(RUN_STATE_SHUTDOWN
);
1949 if (qemu_reset_requested()) {
1951 cpu_synchronize_all_states();
1952 qemu_system_reset(VMRESET_REPORT
);
1954 if (runstate_needs_reset()) {
1955 runstate_set(RUN_STATE_PAUSED
);
1958 if (qemu_wakeup_requested()) {
1960 cpu_synchronize_all_states();
1961 qemu_system_reset(VMRESET_SILENT
);
1962 notifier_list_notify(&wakeup_notifiers
, &wakeup_reason
);
1963 wakeup_reason
= QEMU_WAKEUP_REASON_NONE
;
1965 monitor_protocol_event(QEVENT_WAKEUP
, NULL
);
1967 if (qemu_powerdown_requested()) {
1968 qemu_system_powerdown();
1970 if (qemu_vmstop_requested(&r
)) {
1976 static void main_loop(void)
1980 #ifdef CONFIG_PROFILER
1984 nonblocking
= !kvm_enabled() && !xen_enabled() && last_io
> 0;
1985 #ifdef CONFIG_PROFILER
1986 ti
= profile_getclock();
1988 last_io
= main_loop_wait(nonblocking
);
1989 #ifdef CONFIG_PROFILER
1990 dev_time
+= profile_getclock() - ti
;
1992 } while (!main_loop_should_exit());
1995 static void version(void)
1997 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
2000 static void help(int exitcode
)
2003 printf("usage: %s [options] [disk_image]\n\n"
2004 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2005 error_get_progname());
2007 #define QEMU_OPTIONS_GENERATE_HELP
2008 #include "qemu-options-wrapper.h"
2010 printf("\nDuring emulation, the following keys are useful:\n"
2011 "ctrl-alt-f toggle full screen\n"
2012 "ctrl-alt-n switch to virtual console 'n'\n"
2013 "ctrl-alt toggle mouse and keyboard grab\n"
2015 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2020 #define HAS_ARG 0x0001
2022 typedef struct QEMUOption
{
2029 static const QEMUOption qemu_options
[] = {
2030 { "h", 0, QEMU_OPTION_h
, QEMU_ARCH_ALL
},
2031 #define QEMU_OPTIONS_GENERATE_OPTIONS
2032 #include "qemu-options-wrapper.h"
2036 static bool vga_available(void)
2038 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2041 static bool cirrus_vga_available(void)
2043 return object_class_by_name("cirrus-vga")
2044 || object_class_by_name("isa-cirrus-vga");
2047 static bool vmware_vga_available(void)
2049 return object_class_by_name("vmware-svga");
2052 static bool qxl_vga_available(void)
2054 return object_class_by_name("qxl-vga");
2057 static bool tcx_vga_available(void)
2059 return object_class_by_name("SUNW,tcx");
2062 static bool cg3_vga_available(void)
2064 return object_class_by_name("cgthree");
2067 static void select_vgahw (const char *p
)
2071 assert(vga_interface_type
== VGA_NONE
);
2072 if (strstart(p
, "std", &opts
)) {
2073 if (vga_available()) {
2074 vga_interface_type
= VGA_STD
;
2076 fprintf(stderr
, "Error: standard VGA not available\n");
2079 } else if (strstart(p
, "cirrus", &opts
)) {
2080 if (cirrus_vga_available()) {
2081 vga_interface_type
= VGA_CIRRUS
;
2083 fprintf(stderr
, "Error: Cirrus VGA not available\n");
2086 } else if (strstart(p
, "vmware", &opts
)) {
2087 if (vmware_vga_available()) {
2088 vga_interface_type
= VGA_VMWARE
;
2090 fprintf(stderr
, "Error: VMWare SVGA not available\n");
2093 } else if (strstart(p
, "xenfb", &opts
)) {
2094 vga_interface_type
= VGA_XENFB
;
2095 } else if (strstart(p
, "qxl", &opts
)) {
2096 if (qxl_vga_available()) {
2097 vga_interface_type
= VGA_QXL
;
2099 fprintf(stderr
, "Error: QXL VGA not available\n");
2102 } else if (strstart(p
, "tcx", &opts
)) {
2103 if (tcx_vga_available()) {
2104 vga_interface_type
= VGA_TCX
;
2106 fprintf(stderr
, "Error: TCX framebuffer not available\n");
2109 } else if (strstart(p
, "cg3", &opts
)) {
2110 if (cg3_vga_available()) {
2111 vga_interface_type
= VGA_CG3
;
2113 fprintf(stderr
, "Error: CG3 framebuffer not available\n");
2116 } else if (!strstart(p
, "none", &opts
)) {
2118 fprintf(stderr
, "Unknown vga type: %s\n", p
);
2122 const char *nextopt
;
2124 if (strstart(opts
, ",retrace=", &nextopt
)) {
2126 if (strstart(opts
, "dumb", &nextopt
))
2127 vga_retrace_method
= VGA_RETRACE_DUMB
;
2128 else if (strstart(opts
, "precise", &nextopt
))
2129 vga_retrace_method
= VGA_RETRACE_PRECISE
;
2130 else goto invalid_vga
;
2131 } else goto invalid_vga
;
2136 static DisplayType
select_display(const char *p
)
2139 DisplayType display
= DT_DEFAULT
;
2141 if (strstart(p
, "sdl", &opts
)) {
2145 const char *nextopt
;
2147 if (strstart(opts
, ",frame=", &nextopt
)) {
2149 if (strstart(opts
, "on", &nextopt
)) {
2151 } else if (strstart(opts
, "off", &nextopt
)) {
2154 goto invalid_sdl_args
;
2156 } else if (strstart(opts
, ",alt_grab=", &nextopt
)) {
2158 if (strstart(opts
, "on", &nextopt
)) {
2160 } else if (strstart(opts
, "off", &nextopt
)) {
2163 goto invalid_sdl_args
;
2165 } else if (strstart(opts
, ",ctrl_grab=", &nextopt
)) {
2167 if (strstart(opts
, "on", &nextopt
)) {
2169 } else if (strstart(opts
, "off", &nextopt
)) {
2172 goto invalid_sdl_args
;
2174 } else if (strstart(opts
, ",window_close=", &nextopt
)) {
2176 if (strstart(opts
, "on", &nextopt
)) {
2178 } else if (strstart(opts
, "off", &nextopt
)) {
2181 goto invalid_sdl_args
;
2185 fprintf(stderr
, "Invalid SDL option string: %s\n", p
);
2191 fprintf(stderr
, "SDL support is disabled\n");
2194 } else if (strstart(p
, "vnc", &opts
)) {
2199 const char *nextopt
;
2201 if (strstart(opts
, "=", &nextopt
)) {
2202 vnc_display
= nextopt
;
2206 fprintf(stderr
, "VNC requires a display argument vnc=<display>\n");
2210 fprintf(stderr
, "VNC support is disabled\n");
2213 } else if (strstart(p
, "curses", &opts
)) {
2214 #ifdef CONFIG_CURSES
2215 display
= DT_CURSES
;
2217 fprintf(stderr
, "Curses support is disabled\n");
2220 } else if (strstart(p
, "gtk", &opts
)) {
2224 const char *nextopt
;
2226 if (strstart(opts
, ",grab_on_hover=", &nextopt
)) {
2228 if (strstart(opts
, "on", &nextopt
)) {
2229 grab_on_hover
= true;
2230 } else if (strstart(opts
, "off", &nextopt
)) {
2231 grab_on_hover
= false;
2233 goto invalid_gtk_args
;
2237 fprintf(stderr
, "Invalid GTK option string: %s\n", p
);
2243 fprintf(stderr
, "GTK support is disabled\n");
2246 } else if (strstart(p
, "none", &opts
)) {
2249 fprintf(stderr
, "Unknown display type: %s\n", p
);
2256 static int balloon_parse(const char *arg
)
2260 if (strcmp(arg
, "none") == 0) {
2264 if (!strncmp(arg
, "virtio", 6)) {
2265 if (arg
[6] == ',') {
2266 /* have params -> parse them */
2267 opts
= qemu_opts_parse(qemu_find_opts("device"), arg
+7, 0);
2271 /* create empty opts */
2272 opts
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
2275 qemu_opt_set(opts
, "driver", "virtio-balloon");
2282 char *qemu_find_file(int type
, const char *name
)
2288 /* Try the name as a straight path first */
2289 if (access(name
, R_OK
) == 0) {
2290 trace_load_file(name
, name
);
2291 return g_strdup(name
);
2295 case QEMU_FILE_TYPE_BIOS
:
2298 case QEMU_FILE_TYPE_KEYMAP
:
2299 subdir
= "keymaps/";
2305 for (i
= 0; i
< data_dir_idx
; i
++) {
2306 buf
= g_strdup_printf("%s/%s%s", data_dir
[i
], subdir
, name
);
2307 if (access(buf
, R_OK
) == 0) {
2308 trace_load_file(name
, buf
);
2316 static int device_help_func(QemuOpts
*opts
, void *opaque
)
2318 return qdev_device_help(opts
);
2321 static int device_init_func(QemuOpts
*opts
, void *opaque
)
2325 dev
= qdev_device_add(opts
);
2328 object_unref(OBJECT(dev
));
2332 static int chardev_init_func(QemuOpts
*opts
, void *opaque
)
2334 Error
*local_err
= NULL
;
2336 qemu_chr_new_from_opts(opts
, NULL
, &local_err
);
2338 error_report("%s", error_get_pretty(local_err
));
2339 error_free(local_err
);
2345 #ifdef CONFIG_VIRTFS
2346 static int fsdev_init_func(QemuOpts
*opts
, void *opaque
)
2349 ret
= qemu_fsdev_add(opts
);
2355 static int mon_init_func(QemuOpts
*opts
, void *opaque
)
2357 CharDriverState
*chr
;
2358 const char *chardev
;
2362 mode
= qemu_opt_get(opts
, "mode");
2366 if (strcmp(mode
, "readline") == 0) {
2367 flags
= MONITOR_USE_READLINE
;
2368 } else if (strcmp(mode
, "control") == 0) {
2369 flags
= MONITOR_USE_CONTROL
;
2371 fprintf(stderr
, "unknown monitor mode \"%s\"\n", mode
);
2375 if (qemu_opt_get_bool(opts
, "pretty", 0))
2376 flags
|= MONITOR_USE_PRETTY
;
2378 if (qemu_opt_get_bool(opts
, "default", 0))
2379 flags
|= MONITOR_IS_DEFAULT
;
2381 chardev
= qemu_opt_get(opts
, "chardev");
2382 chr
= qemu_chr_find(chardev
);
2384 fprintf(stderr
, "chardev \"%s\" not found\n", chardev
);
2388 qemu_chr_fe_claim_no_fail(chr
);
2389 monitor_init(chr
, flags
);
2393 static void monitor_parse(const char *optarg
, const char *mode
)
2395 static int monitor_device_index
= 0;
2401 if (strstart(optarg
, "chardev:", &p
)) {
2402 snprintf(label
, sizeof(label
), "%s", p
);
2404 snprintf(label
, sizeof(label
), "compat_monitor%d",
2405 monitor_device_index
);
2406 if (monitor_device_index
== 0) {
2409 opts
= qemu_chr_parse_compat(label
, optarg
);
2411 fprintf(stderr
, "parse error: %s\n", optarg
);
2416 opts
= qemu_opts_create(qemu_find_opts("mon"), label
, 1, NULL
);
2418 fprintf(stderr
, "duplicate chardev: %s\n", label
);
2421 qemu_opt_set(opts
, "mode", mode
);
2422 qemu_opt_set(opts
, "chardev", label
);
2424 qemu_opt_set(opts
, "default", "on");
2425 monitor_device_index
++;
2428 struct device_config
{
2430 DEV_USB
, /* -usbdevice */
2432 DEV_SERIAL
, /* -serial */
2433 DEV_PARALLEL
, /* -parallel */
2434 DEV_VIRTCON
, /* -virtioconsole */
2435 DEV_DEBUGCON
, /* -debugcon */
2436 DEV_GDB
, /* -gdb, -s */
2437 DEV_SCLP
, /* s390 sclp */
2439 const char *cmdline
;
2441 QTAILQ_ENTRY(device_config
) next
;
2444 static QTAILQ_HEAD(, device_config
) device_configs
=
2445 QTAILQ_HEAD_INITIALIZER(device_configs
);
2447 static void add_device_config(int type
, const char *cmdline
)
2449 struct device_config
*conf
;
2451 conf
= g_malloc0(sizeof(*conf
));
2453 conf
->cmdline
= cmdline
;
2454 loc_save(&conf
->loc
);
2455 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
2458 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
2460 struct device_config
*conf
;
2463 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
2464 if (conf
->type
!= type
)
2466 loc_push_restore(&conf
->loc
);
2467 rc
= func(conf
->cmdline
);
2468 loc_pop(&conf
->loc
);
2475 static int serial_parse(const char *devname
)
2477 static int index
= 0;
2480 if (strcmp(devname
, "none") == 0)
2482 if (index
== MAX_SERIAL_PORTS
) {
2483 fprintf(stderr
, "qemu: too many serial ports\n");
2486 snprintf(label
, sizeof(label
), "serial%d", index
);
2487 serial_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2488 if (!serial_hds
[index
]) {
2489 fprintf(stderr
, "qemu: could not connect serial device"
2490 " to character backend '%s'\n", devname
);
2497 static int parallel_parse(const char *devname
)
2499 static int index
= 0;
2502 if (strcmp(devname
, "none") == 0)
2504 if (index
== MAX_PARALLEL_PORTS
) {
2505 fprintf(stderr
, "qemu: too many parallel ports\n");
2508 snprintf(label
, sizeof(label
), "parallel%d", index
);
2509 parallel_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2510 if (!parallel_hds
[index
]) {
2511 fprintf(stderr
, "qemu: could not connect parallel device"
2512 " to character backend '%s'\n", devname
);
2519 static int virtcon_parse(const char *devname
)
2521 QemuOptsList
*device
= qemu_find_opts("device");
2522 static int index
= 0;
2524 QemuOpts
*bus_opts
, *dev_opts
;
2526 if (strcmp(devname
, "none") == 0)
2528 if (index
== MAX_VIRTIO_CONSOLES
) {
2529 fprintf(stderr
, "qemu: too many virtio consoles\n");
2533 bus_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2534 if (arch_type
== QEMU_ARCH_S390X
) {
2535 qemu_opt_set(bus_opts
, "driver", "virtio-serial-s390");
2537 qemu_opt_set(bus_opts
, "driver", "virtio-serial-pci");
2540 dev_opts
= qemu_opts_create(device
, NULL
, 0, &error_abort
);
2541 qemu_opt_set(dev_opts
, "driver", "virtconsole");
2543 snprintf(label
, sizeof(label
), "virtcon%d", index
);
2544 virtcon_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2545 if (!virtcon_hds
[index
]) {
2546 fprintf(stderr
, "qemu: could not connect virtio console"
2547 " to character backend '%s'\n", devname
);
2550 qemu_opt_set(dev_opts
, "chardev", label
);
2556 static int sclp_parse(const char *devname
)
2558 QemuOptsList
*device
= qemu_find_opts("device");
2559 static int index
= 0;
2563 if (strcmp(devname
, "none") == 0) {
2566 if (index
== MAX_SCLP_CONSOLES
) {
2567 fprintf(stderr
, "qemu: too many sclp consoles\n");
2571 assert(arch_type
== QEMU_ARCH_S390X
);
2573 dev_opts
= qemu_opts_create(device
, NULL
, 0, NULL
);
2574 qemu_opt_set(dev_opts
, "driver", "sclpconsole");
2576 snprintf(label
, sizeof(label
), "sclpcon%d", index
);
2577 sclp_hds
[index
] = qemu_chr_new(label
, devname
, NULL
);
2578 if (!sclp_hds
[index
]) {
2579 fprintf(stderr
, "qemu: could not connect sclp console"
2580 " to character backend '%s'\n", devname
);
2583 qemu_opt_set(dev_opts
, "chardev", label
);
2589 static int debugcon_parse(const char *devname
)
2593 if (!qemu_chr_new("debugcon", devname
, NULL
)) {
2596 opts
= qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL
);
2598 fprintf(stderr
, "qemu: already have a debugcon device\n");
2601 qemu_opt_set(opts
, "driver", "isa-debugcon");
2602 qemu_opt_set(opts
, "chardev", "debugcon");
2606 static MachineClass
*machine_parse(const char *name
)
2608 MachineClass
*mc
= NULL
;
2609 GSList
*el
, *machines
= object_class_get_list(TYPE_MACHINE
, false);
2612 mc
= find_machine(name
);
2617 if (name
&& !is_help_option(name
)) {
2618 error_report("Unsupported machine type");
2619 error_printf("Use -machine help to list supported machines!\n");
2621 printf("Supported machines are:\n");
2622 for (el
= machines
; el
; el
= el
->next
) {
2623 MachineClass
*mc
= el
->data
;
2625 printf("%-20s %s (alias of %s)\n", mc
->alias
, mc
->desc
, mc
->name
);
2627 printf("%-20s %s%s\n", mc
->name
, mc
->desc
,
2628 mc
->is_default
? " (default)" : "");
2632 g_slist_free(machines
);
2633 exit(!name
|| !is_help_option(name
));
2636 static int tcg_init(MachineClass
*mc
)
2638 tcg_exec_init(tcg_tb_size
* 1024 * 1024);
2643 const char *opt_name
;
2645 int (*available
)(void);
2646 int (*init
)(MachineClass
*mc
);
2649 { "tcg", "tcg", tcg_available
, tcg_init
, &tcg_allowed
},
2650 { "xen", "Xen", xen_available
, xen_init
, &xen_allowed
},
2651 { "kvm", "KVM", kvm_available
, kvm_init
, &kvm_allowed
},
2652 { "qtest", "QTest", qtest_available
, qtest_init_accel
, &qtest_allowed
},
2655 static int configure_accelerator(MachineClass
*mc
)
2660 bool accel_initialised
= false;
2661 bool init_failed
= false;
2663 p
= qemu_opt_get(qemu_get_machine_opts(), "accel");
2665 /* Use the default "accelerator", tcg */
2669 while (!accel_initialised
&& *p
!= '\0') {
2673 p
= get_opt_name(buf
, sizeof (buf
), p
, ':');
2674 for (i
= 0; i
< ARRAY_SIZE(accel_list
); i
++) {
2675 if (strcmp(accel_list
[i
].opt_name
, buf
) == 0) {
2676 if (!accel_list
[i
].available()) {
2677 printf("%s not supported for this target\n",
2678 accel_list
[i
].name
);
2681 *(accel_list
[i
].allowed
) = true;
2682 ret
= accel_list
[i
].init(mc
);
2685 fprintf(stderr
, "failed to initialize %s: %s\n",
2688 *(accel_list
[i
].allowed
) = false;
2690 accel_initialised
= true;
2695 if (i
== ARRAY_SIZE(accel_list
)) {
2696 fprintf(stderr
, "\"%s\" accelerator does not exist.\n", buf
);
2700 if (!accel_initialised
) {
2702 fprintf(stderr
, "No accelerator found!\n");
2708 fprintf(stderr
, "Back to %s accelerator.\n", accel_list
[i
].name
);
2711 return !accel_initialised
;
2714 void qemu_add_exit_notifier(Notifier
*notify
)
2716 notifier_list_add(&exit_notifiers
, notify
);
2719 void qemu_remove_exit_notifier(Notifier
*notify
)
2721 notifier_remove(notify
);
2724 static void qemu_run_exit_notifiers(void)
2726 notifier_list_notify(&exit_notifiers
, NULL
);
2729 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
2731 notifier_list_add(&machine_init_done_notifiers
, notify
);
2734 static void qemu_run_machine_init_done_notifiers(void)
2736 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
2739 static const QEMUOption
*lookup_opt(int argc
, char **argv
,
2740 const char **poptarg
, int *poptind
)
2742 const QEMUOption
*popt
;
2743 int optind
= *poptind
;
2744 char *r
= argv
[optind
];
2747 loc_set_cmdline(argv
, optind
, 1);
2749 /* Treat --foo the same as -foo. */
2752 popt
= qemu_options
;
2755 error_report("invalid option");
2758 if (!strcmp(popt
->name
, r
+ 1))
2762 if (popt
->flags
& HAS_ARG
) {
2763 if (optind
>= argc
) {
2764 error_report("requires an argument");
2767 optarg
= argv
[optind
++];
2768 loc_set_cmdline(argv
, optind
- 2, 2);
2779 static gpointer
malloc_and_trace(gsize n_bytes
)
2781 void *ptr
= malloc(n_bytes
);
2782 trace_g_malloc(n_bytes
, ptr
);
2786 static gpointer
realloc_and_trace(gpointer mem
, gsize n_bytes
)
2788 void *ptr
= realloc(mem
, n_bytes
);
2789 trace_g_realloc(mem
, n_bytes
, ptr
);
2793 static void free_and_trace(gpointer mem
)
2799 static int object_set_property(const char *name
, const char *value
, void *opaque
)
2801 Object
*obj
= OBJECT(opaque
);
2802 StringInputVisitor
*siv
;
2803 Error
*local_err
= NULL
;
2805 if (strcmp(name
, "qom-type") == 0 || strcmp(name
, "id") == 0 ||
2806 strcmp(name
, "type") == 0) {
2810 siv
= string_input_visitor_new(value
);
2811 object_property_set(obj
, string_input_get_visitor(siv
), name
, &local_err
);
2812 string_input_visitor_cleanup(siv
);
2815 qerror_report_err(local_err
);
2816 error_free(local_err
);
2823 static int object_create(QemuOpts
*opts
, void *opaque
)
2832 ov
= opts_visitor_new(opts
);
2833 pdict
= qemu_opts_to_qdict(opts
, NULL
);
2835 visit_start_struct(opts_get_visitor(ov
), &dummy
, NULL
, NULL
, 0, &err
);
2840 qdict_del(pdict
, "qom-type");
2841 visit_type_str(opts_get_visitor(ov
), &type
, "qom-type", &err
);
2846 qdict_del(pdict
, "id");
2847 visit_type_str(opts_get_visitor(ov
), &id
, "id", &err
);
2852 object_add(type
, id
, pdict
, opts_get_visitor(ov
), &err
);
2856 visit_end_struct(opts_get_visitor(ov
), &err
);
2858 qmp_object_del(id
, NULL
);
2862 opts_visitor_cleanup(ov
);
2869 qerror_report_err(err
);
2875 int main(int argc
, char **argv
, char **envp
)
2878 int snapshot
, linux_boot
;
2879 const char *icount_option
= NULL
;
2880 const char *initrd_filename
;
2881 const char *kernel_filename
, *kernel_cmdline
;
2882 const char *boot_order
;
2884 int cyls
, heads
, secs
, translation
;
2885 QemuOpts
*hda_opts
= NULL
, *opts
, *machine_opts
;
2886 QemuOptsList
*olist
;
2889 const char *loadvm
= NULL
;
2890 MachineClass
*machine_class
;
2891 const char *cpu_model
;
2892 const char *vga_model
= NULL
;
2893 const char *qtest_chrdev
= NULL
;
2894 const char *qtest_log
= NULL
;
2895 const char *pid_file
= NULL
;
2896 const char *incoming
= NULL
;
2898 int show_vnc_port
= 0;
2900 bool defconfig
= true;
2901 bool userconfig
= true;
2902 const char *log_mask
= NULL
;
2903 const char *log_file
= NULL
;
2904 GMemVTable mem_trace
= {
2905 .malloc
= malloc_and_trace
,
2906 .realloc
= realloc_and_trace
,
2907 .free
= free_and_trace
,
2909 const char *trace_events
= NULL
;
2910 const char *trace_file
= NULL
;
2911 const ram_addr_t default_ram_size
= (ram_addr_t
)DEFAULT_RAM_SIZE
*
2913 ram_addr_t maxram_size
= default_ram_size
;
2914 uint64_t ram_slots
= 0;
2916 atexit(qemu_run_exit_notifiers
);
2917 error_set_progname(argv
[0]);
2918 qemu_init_exec_dir(argv
[0]);
2920 g_mem_set_vtable(&mem_trace
);
2922 module_call_init(MODULE_INIT_QOM
);
2924 qemu_add_opts(&qemu_drive_opts
);
2925 qemu_add_drive_opts(&qemu_legacy_drive_opts
);
2926 qemu_add_drive_opts(&qemu_common_drive_opts
);
2927 qemu_add_drive_opts(&qemu_drive_opts
);
2928 qemu_add_opts(&qemu_chardev_opts
);
2929 qemu_add_opts(&qemu_device_opts
);
2930 qemu_add_opts(&qemu_netdev_opts
);
2931 qemu_add_opts(&qemu_net_opts
);
2932 qemu_add_opts(&qemu_rtc_opts
);
2933 qemu_add_opts(&qemu_global_opts
);
2934 qemu_add_opts(&qemu_mon_opts
);
2935 qemu_add_opts(&qemu_trace_opts
);
2936 qemu_add_opts(&qemu_option_rom_opts
);
2937 qemu_add_opts(&qemu_machine_opts
);
2938 qemu_add_opts(&qemu_mem_opts
);
2939 qemu_add_opts(&qemu_smp_opts
);
2940 qemu_add_opts(&qemu_boot_opts
);
2941 qemu_add_opts(&qemu_sandbox_opts
);
2942 qemu_add_opts(&qemu_add_fd_opts
);
2943 qemu_add_opts(&qemu_object_opts
);
2944 qemu_add_opts(&qemu_tpmdev_opts
);
2945 qemu_add_opts(&qemu_realtime_opts
);
2946 qemu_add_opts(&qemu_msg_opts
);
2947 qemu_add_opts(&qemu_name_opts
);
2948 qemu_add_opts(&qemu_numa_opts
);
2952 rtc_clock
= QEMU_CLOCK_HOST
;
2954 qemu_init_auxval(envp
);
2955 qemu_cache_utils_init();
2957 QLIST_INIT (&vm_change_state_head
);
2958 os_setup_early_signal_handling();
2960 module_call_init(MODULE_INIT_MACHINE
);
2961 machine_class
= find_default_machine();
2963 ram_size
= default_ram_size
;
2965 cyls
= heads
= secs
= 0;
2966 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2968 for (i
= 0; i
< MAX_NODES
; i
++) {
2969 numa_info
[i
].node_mem
= 0;
2970 bitmap_zero(numa_info
[i
].node_cpu
, MAX_CPUMASK_BITS
);
2976 bdrv_init_with_whitelist();
2980 /* first pass of option parsing */
2982 while (optind
< argc
) {
2983 if (argv
[optind
][0] != '-') {
2987 const QEMUOption
*popt
;
2989 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
2990 switch (popt
->index
) {
2991 case QEMU_OPTION_nodefconfig
:
2994 case QEMU_OPTION_nouserconfig
:
3003 ret
= qemu_read_default_config_files(userconfig
);
3009 /* second pass of option parsing */
3014 if (argv
[optind
][0] != '-') {
3015 hda_opts
= drive_add(IF_DEFAULT
, 0, argv
[optind
++], HD_OPTS
);
3017 const QEMUOption
*popt
;
3019 popt
= lookup_opt(argc
, argv
, &optarg
, &optind
);
3020 if (!(popt
->arch_mask
& arch_type
)) {
3021 printf("Option %s not supported for this target\n", popt
->name
);
3024 switch(popt
->index
) {
3026 machine_class
= machine_parse(optarg
);
3028 case QEMU_OPTION_no_kvm_irqchip
: {
3029 olist
= qemu_find_opts("machine");
3030 qemu_opts_parse(olist
, "kernel_irqchip=off", 0);
3033 case QEMU_OPTION_cpu
:
3034 /* hw initialization will check this */
3037 case QEMU_OPTION_hda
:
3041 snprintf(buf
, sizeof(buf
), "%s", HD_OPTS
);
3043 snprintf(buf
, sizeof(buf
),
3044 "%s,cyls=%d,heads=%d,secs=%d%s",
3045 HD_OPTS
, cyls
, heads
, secs
,
3046 translation
== BIOS_ATA_TRANSLATION_LBA
?
3048 translation
== BIOS_ATA_TRANSLATION_NONE
?
3049 ",trans=none" : "");
3050 drive_add(IF_DEFAULT
, 0, optarg
, buf
);
3053 case QEMU_OPTION_hdb
:
3054 case QEMU_OPTION_hdc
:
3055 case QEMU_OPTION_hdd
:
3056 drive_add(IF_DEFAULT
, popt
->index
- QEMU_OPTION_hda
, optarg
,
3059 case QEMU_OPTION_drive
:
3060 if (drive_def(optarg
) == NULL
) {
3064 case QEMU_OPTION_set
:
3065 if (qemu_set_option(optarg
) != 0)
3068 case QEMU_OPTION_global
:
3069 if (qemu_global_option(optarg
) != 0)
3072 case QEMU_OPTION_mtdblock
:
3073 drive_add(IF_MTD
, -1, optarg
, MTD_OPTS
);
3075 case QEMU_OPTION_sd
:
3076 drive_add(IF_SD
, -1, optarg
, SD_OPTS
);
3078 case QEMU_OPTION_pflash
:
3079 drive_add(IF_PFLASH
, -1, optarg
, PFLASH_OPTS
);
3081 case QEMU_OPTION_snapshot
:
3084 case QEMU_OPTION_hdachs
:
3088 cyls
= strtol(p
, (char **)&p
, 0);
3089 if (cyls
< 1 || cyls
> 16383)
3094 heads
= strtol(p
, (char **)&p
, 0);
3095 if (heads
< 1 || heads
> 16)
3100 secs
= strtol(p
, (char **)&p
, 0);
3101 if (secs
< 1 || secs
> 63)
3105 if (!strcmp(p
, "large")) {
3106 translation
= BIOS_ATA_TRANSLATION_LARGE
;
3107 } else if (!strcmp(p
, "rechs")) {
3108 translation
= BIOS_ATA_TRANSLATION_RECHS
;
3109 } else if (!strcmp(p
, "none")) {
3110 translation
= BIOS_ATA_TRANSLATION_NONE
;
3111 } else if (!strcmp(p
, "lba")) {
3112 translation
= BIOS_ATA_TRANSLATION_LBA
;
3113 } else if (!strcmp(p
, "auto")) {
3114 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3118 } else if (*p
!= '\0') {
3120 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3123 if (hda_opts
!= NULL
) {
3125 snprintf(num
, sizeof(num
), "%d", cyls
);
3126 qemu_opt_set(hda_opts
, "cyls", num
);
3127 snprintf(num
, sizeof(num
), "%d", heads
);
3128 qemu_opt_set(hda_opts
, "heads", num
);
3129 snprintf(num
, sizeof(num
), "%d", secs
);
3130 qemu_opt_set(hda_opts
, "secs", num
);
3131 if (translation
== BIOS_ATA_TRANSLATION_LARGE
) {
3132 qemu_opt_set(hda_opts
, "trans", "large");
3133 } else if (translation
== BIOS_ATA_TRANSLATION_RECHS
) {
3134 qemu_opt_set(hda_opts
, "trans", "rechs");
3135 } else if (translation
== BIOS_ATA_TRANSLATION_LBA
) {
3136 qemu_opt_set(hda_opts
, "trans", "lba");
3137 } else if (translation
== BIOS_ATA_TRANSLATION_NONE
) {
3138 qemu_opt_set(hda_opts
, "trans", "none");
3143 case QEMU_OPTION_numa
:
3144 opts
= qemu_opts_parse(qemu_find_opts("numa"), optarg
, 1);
3149 case QEMU_OPTION_display
:
3150 display_type
= select_display(optarg
);
3152 case QEMU_OPTION_nographic
:
3153 display_type
= DT_NOGRAPHIC
;
3155 case QEMU_OPTION_curses
:
3156 #ifdef CONFIG_CURSES
3157 display_type
= DT_CURSES
;
3159 fprintf(stderr
, "Curses support is disabled\n");
3163 case QEMU_OPTION_portrait
:
3164 graphic_rotate
= 90;
3166 case QEMU_OPTION_rotate
:
3167 graphic_rotate
= strtol(optarg
, (char **) &optarg
, 10);
3168 if (graphic_rotate
!= 0 && graphic_rotate
!= 90 &&
3169 graphic_rotate
!= 180 && graphic_rotate
!= 270) {
3171 "qemu: only 90, 180, 270 deg rotation is available\n");
3175 case QEMU_OPTION_kernel
:
3176 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg
);
3178 case QEMU_OPTION_initrd
:
3179 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg
);
3181 case QEMU_OPTION_append
:
3182 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg
);
3184 case QEMU_OPTION_dtb
:
3185 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg
);
3187 case QEMU_OPTION_cdrom
:
3188 drive_add(IF_DEFAULT
, 2, optarg
, CDROM_OPTS
);
3190 case QEMU_OPTION_boot
:
3191 opts
= qemu_opts_parse(qemu_find_opts("boot-opts"), optarg
, 1);
3196 case QEMU_OPTION_fda
:
3197 case QEMU_OPTION_fdb
:
3198 drive_add(IF_FLOPPY
, popt
->index
- QEMU_OPTION_fda
,
3201 case QEMU_OPTION_no_fd_bootchk
:
3204 case QEMU_OPTION_netdev
:
3205 if (net_client_parse(qemu_find_opts("netdev"), optarg
) == -1) {
3209 case QEMU_OPTION_net
:
3210 if (net_client_parse(qemu_find_opts("net"), optarg
) == -1) {
3214 #ifdef CONFIG_LIBISCSI
3215 case QEMU_OPTION_iscsi
:
3216 opts
= qemu_opts_parse(qemu_find_opts("iscsi"), optarg
, 0);
3223 case QEMU_OPTION_tftp
:
3224 legacy_tftp_prefix
= optarg
;
3226 case QEMU_OPTION_bootp
:
3227 legacy_bootp_filename
= optarg
;
3229 case QEMU_OPTION_redir
:
3230 if (net_slirp_redir(optarg
) < 0)
3234 case QEMU_OPTION_bt
:
3235 add_device_config(DEV_BT
, optarg
);
3237 case QEMU_OPTION_audio_help
:
3241 case QEMU_OPTION_soundhw
:
3242 select_soundhw (optarg
);
3247 case QEMU_OPTION_version
:
3251 case QEMU_OPTION_m
: {
3253 const char *mem_str
;
3254 const char *maxmem_str
, *slots_str
;
3256 opts
= qemu_opts_parse(qemu_find_opts("memory"),
3262 mem_str
= qemu_opt_get(opts
, "size");
3264 error_report("invalid -m option, missing 'size' option");
3268 error_report("missing 'size' option value");
3272 sz
= qemu_opt_get_size(opts
, "size", ram_size
);
3274 /* Fix up legacy suffix-less format */
3275 if (g_ascii_isdigit(mem_str
[strlen(mem_str
) - 1])) {
3276 uint64_t overflow_check
= sz
;
3279 if ((sz
>> 20) != overflow_check
) {
3280 error_report("too large 'size' option value");
3285 /* backward compatibility behaviour for case "-m 0" */
3287 sz
= default_ram_size
;
3290 sz
= QEMU_ALIGN_UP(sz
, 8192);
3292 if (ram_size
!= sz
) {
3293 error_report("ram size too large");
3297 maxmem_str
= qemu_opt_get(opts
, "maxmem");
3298 slots_str
= qemu_opt_get(opts
, "slots");
3299 if (maxmem_str
&& slots_str
) {
3302 sz
= qemu_opt_get_size(opts
, "maxmem", 0);
3303 if (sz
< ram_size
) {
3304 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3305 "(%" PRIu64
") <= initial memory ("
3306 RAM_ADDR_FMT
")\n", sz
, ram_size
);
3310 slots
= qemu_opt_get_number(opts
, "slots", 0);
3311 if ((sz
> ram_size
) && !slots
) {
3312 fprintf(stderr
, "qemu: invalid -m option value: maxmem "
3313 "(%" PRIu64
") more than initial memory ("
3314 RAM_ADDR_FMT
") but no hotplug slots where "
3315 "specified\n", sz
, ram_size
);
3319 if ((sz
<= ram_size
) && slots
) {
3320 fprintf(stderr
, "qemu: invalid -m option value: %"
3321 PRIu64
" hotplug slots where specified but "
3322 "maxmem (%" PRIu64
") <= initial memory ("
3323 RAM_ADDR_FMT
")\n", slots
, sz
, ram_size
);
3328 } else if ((!maxmem_str
&& slots_str
) ||
3329 (maxmem_str
&& !slots_str
)) {
3330 fprintf(stderr
, "qemu: invalid -m option value: missing "
3331 "'%s' option\n", slots_str
? "maxmem" : "slots");
3337 case QEMU_OPTION_tpmdev
:
3338 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg
) < 0) {
3343 case QEMU_OPTION_mempath
:
3346 case QEMU_OPTION_mem_prealloc
:
3356 add_device_config(DEV_GDB
, "tcp::" DEFAULT_GDBSTUB_PORT
);
3358 case QEMU_OPTION_gdb
:
3359 add_device_config(DEV_GDB
, optarg
);
3362 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
3363 data_dir
[data_dir_idx
++] = optarg
;
3366 case QEMU_OPTION_bios
:
3367 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg
);
3369 case QEMU_OPTION_singlestep
:
3376 keyboard_layout
= optarg
;
3378 case QEMU_OPTION_localtime
:
3381 case QEMU_OPTION_vga
:
3390 w
= strtol(p
, (char **)&p
, 10);
3393 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3399 h
= strtol(p
, (char **)&p
, 10);
3404 depth
= strtol(p
, (char **)&p
, 10);
3405 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3406 depth
!= 24 && depth
!= 32)
3408 } else if (*p
== '\0') {
3409 depth
= graphic_depth
;
3416 graphic_depth
= depth
;
3419 case QEMU_OPTION_echr
:
3422 term_escape_char
= strtol(optarg
, &r
, 0);
3424 printf("Bad argument to echr\n");
3427 case QEMU_OPTION_monitor
:
3428 default_monitor
= 0;
3429 if (strncmp(optarg
, "none", 4)) {
3430 monitor_parse(optarg
, "readline");
3433 case QEMU_OPTION_qmp
:
3434 monitor_parse(optarg
, "control");
3435 default_monitor
= 0;
3437 case QEMU_OPTION_mon
:
3438 opts
= qemu_opts_parse(qemu_find_opts("mon"), optarg
, 1);
3442 default_monitor
= 0;
3444 case QEMU_OPTION_chardev
:
3445 opts
= qemu_opts_parse(qemu_find_opts("chardev"), optarg
, 1);
3450 case QEMU_OPTION_fsdev
:
3451 olist
= qemu_find_opts("fsdev");
3453 fprintf(stderr
, "fsdev is not supported by this qemu build.\n");
3456 opts
= qemu_opts_parse(olist
, optarg
, 1);
3461 case QEMU_OPTION_virtfs
: {
3464 const char *writeout
, *sock_fd
, *socket
;
3466 olist
= qemu_find_opts("virtfs");
3468 fprintf(stderr
, "virtfs is not supported by this qemu build.\n");
3471 opts
= qemu_opts_parse(olist
, optarg
, 1);
3476 if (qemu_opt_get(opts
, "fsdriver") == NULL
||
3477 qemu_opt_get(opts
, "mount_tag") == NULL
) {
3478 fprintf(stderr
, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3481 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"),
3482 qemu_opt_get(opts
, "mount_tag"),
3485 fprintf(stderr
, "duplicate fsdev id: %s\n",
3486 qemu_opt_get(opts
, "mount_tag"));
3490 writeout
= qemu_opt_get(opts
, "writeout");
3492 #ifdef CONFIG_SYNC_FILE_RANGE
3493 qemu_opt_set(fsdev
, "writeout", writeout
);
3495 fprintf(stderr
, "writeout=immediate not supported on "
3500 qemu_opt_set(fsdev
, "fsdriver", qemu_opt_get(opts
, "fsdriver"));
3501 qemu_opt_set(fsdev
, "path", qemu_opt_get(opts
, "path"));
3502 qemu_opt_set(fsdev
, "security_model",
3503 qemu_opt_get(opts
, "security_model"));
3504 socket
= qemu_opt_get(opts
, "socket");
3506 qemu_opt_set(fsdev
, "socket", socket
);
3508 sock_fd
= qemu_opt_get(opts
, "sock_fd");
3510 qemu_opt_set(fsdev
, "sock_fd", sock_fd
);
3513 qemu_opt_set_bool(fsdev
, "readonly",
3514 qemu_opt_get_bool(opts
, "readonly", 0));
3515 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3517 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3518 qemu_opt_set(device
, "fsdev",
3519 qemu_opt_get(opts
, "mount_tag"));
3520 qemu_opt_set(device
, "mount_tag",
3521 qemu_opt_get(opts
, "mount_tag"));
3524 case QEMU_OPTION_virtfs_synth
: {
3528 fsdev
= qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3531 fprintf(stderr
, "duplicate option: %s\n", "virtfs_synth");
3534 qemu_opt_set(fsdev
, "fsdriver", "synth");
3536 device
= qemu_opts_create(qemu_find_opts("device"), NULL
, 0,
3538 qemu_opt_set(device
, "driver", "virtio-9p-pci");
3539 qemu_opt_set(device
, "fsdev", "v_synth");
3540 qemu_opt_set(device
, "mount_tag", "v_synth");
3543 case QEMU_OPTION_serial
:
3544 add_device_config(DEV_SERIAL
, optarg
);
3546 if (strncmp(optarg
, "mon:", 4) == 0) {
3547 default_monitor
= 0;
3550 case QEMU_OPTION_watchdog
:
3553 "qemu: only one watchdog option may be given\n");
3558 case QEMU_OPTION_watchdog_action
:
3559 if (select_watchdog_action(optarg
) == -1) {
3560 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
3564 case QEMU_OPTION_virtiocon
:
3565 add_device_config(DEV_VIRTCON
, optarg
);
3566 default_virtcon
= 0;
3567 if (strncmp(optarg
, "mon:", 4) == 0) {
3568 default_monitor
= 0;
3571 case QEMU_OPTION_parallel
:
3572 add_device_config(DEV_PARALLEL
, optarg
);
3573 default_parallel
= 0;
3574 if (strncmp(optarg
, "mon:", 4) == 0) {
3575 default_monitor
= 0;
3578 case QEMU_OPTION_debugcon
:
3579 add_device_config(DEV_DEBUGCON
, optarg
);
3581 case QEMU_OPTION_loadvm
:
3584 case QEMU_OPTION_full_screen
:
3587 case QEMU_OPTION_no_frame
:
3590 case QEMU_OPTION_alt_grab
:
3593 case QEMU_OPTION_ctrl_grab
:
3596 case QEMU_OPTION_no_quit
:
3599 case QEMU_OPTION_sdl
:
3601 display_type
= DT_SDL
;
3604 fprintf(stderr
, "SDL support is disabled\n");
3607 case QEMU_OPTION_pidfile
:
3610 case QEMU_OPTION_win2k_hack
:
3611 win2k_install_hack
= 1;
3613 case QEMU_OPTION_rtc_td_hack
: {
3614 static GlobalProperty slew_lost_ticks
[] = {
3616 .driver
= "mc146818rtc",
3617 .property
= "lost_tick_policy",
3620 { /* end of list */ }
3623 qdev_prop_register_global_list(slew_lost_ticks
);
3626 case QEMU_OPTION_acpitable
:
3627 opts
= qemu_opts_parse(qemu_find_opts("acpi"), optarg
, 1);
3631 do_acpitable_option(opts
);
3633 case QEMU_OPTION_smbios
:
3634 opts
= qemu_opts_parse(qemu_find_opts("smbios"), optarg
, 0);
3638 do_smbios_option(opts
);
3640 case QEMU_OPTION_enable_kvm
:
3641 olist
= qemu_find_opts("machine");
3642 qemu_opts_parse(olist
, "accel=kvm", 0);
3644 case QEMU_OPTION_machine
:
3645 olist
= qemu_find_opts("machine");
3646 opts
= qemu_opts_parse(olist
, optarg
, 1);
3650 optarg
= qemu_opt_get(opts
, "type");
3652 machine_class
= machine_parse(optarg
);
3655 case QEMU_OPTION_no_kvm
:
3656 olist
= qemu_find_opts("machine");
3657 qemu_opts_parse(olist
, "accel=tcg", 0);
3659 case QEMU_OPTION_no_kvm_pit
: {
3660 fprintf(stderr
, "Warning: KVM PIT can no longer be disabled "
3664 case QEMU_OPTION_no_kvm_pit_reinjection
: {
3665 static GlobalProperty kvm_pit_lost_tick_policy
[] = {
3667 .driver
= "kvm-pit",
3668 .property
= "lost_tick_policy",
3671 { /* end of list */ }
3674 fprintf(stderr
, "Warning: option deprecated, use "
3675 "lost_tick_policy property of kvm-pit instead.\n");
3676 qdev_prop_register_global_list(kvm_pit_lost_tick_policy
);
3679 case QEMU_OPTION_usb
:
3680 olist
= qemu_find_opts("machine");
3681 qemu_opts_parse(olist
, "usb=on", 0);
3683 case QEMU_OPTION_usbdevice
:
3684 olist
= qemu_find_opts("machine");
3685 qemu_opts_parse(olist
, "usb=on", 0);
3686 add_device_config(DEV_USB
, optarg
);
3688 case QEMU_OPTION_device
:
3689 if (!qemu_opts_parse(qemu_find_opts("device"), optarg
, 1)) {
3693 case QEMU_OPTION_smp
:
3694 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg
, 1)) {
3698 case QEMU_OPTION_vnc
:
3701 vnc_display
= optarg
;
3703 fprintf(stderr
, "VNC support is disabled\n");
3707 case QEMU_OPTION_no_acpi
:
3710 case QEMU_OPTION_no_hpet
:
3713 case QEMU_OPTION_balloon
:
3714 if (balloon_parse(optarg
) < 0) {
3715 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
3719 case QEMU_OPTION_no_reboot
:
3722 case QEMU_OPTION_no_shutdown
:
3725 case QEMU_OPTION_show_cursor
:
3728 case QEMU_OPTION_uuid
:
3729 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
3730 fprintf(stderr
, "Fail to parse UUID string."
3731 " Wrong format.\n");
3734 qemu_uuid_set
= true;
3736 case QEMU_OPTION_option_rom
:
3737 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
3738 fprintf(stderr
, "Too many option ROMs\n");
3741 opts
= qemu_opts_parse(qemu_find_opts("option-rom"), optarg
, 1);
3745 option_rom
[nb_option_roms
].name
= qemu_opt_get(opts
, "romfile");
3746 option_rom
[nb_option_roms
].bootindex
=
3747 qemu_opt_get_number(opts
, "bootindex", -1);
3748 if (!option_rom
[nb_option_roms
].name
) {
3749 fprintf(stderr
, "Option ROM file is not specified\n");
3754 case QEMU_OPTION_semihosting
:
3755 semihosting_enabled
= 1;
3757 case QEMU_OPTION_tdf
:
3758 fprintf(stderr
, "Warning: user space PIT time drift fix "
3759 "is no longer supported.\n");
3761 case QEMU_OPTION_name
:
3762 opts
= qemu_opts_parse(qemu_find_opts("name"), optarg
, 1);
3767 case QEMU_OPTION_prom_env
:
3768 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
3769 fprintf(stderr
, "Too many prom variables\n");
3772 prom_envs
[nb_prom_envs
] = optarg
;
3775 case QEMU_OPTION_old_param
:
3778 case QEMU_OPTION_clock
:
3779 /* Clock options no longer exist. Keep this option for
3780 * backward compatibility.
3783 case QEMU_OPTION_startdate
:
3784 configure_rtc_date_offset(optarg
, 1);
3786 case QEMU_OPTION_rtc
:
3787 opts
= qemu_opts_parse(qemu_find_opts("rtc"), optarg
, 0);
3791 configure_rtc(opts
);
3793 case QEMU_OPTION_tb_size
:
3794 tcg_tb_size
= strtol(optarg
, NULL
, 0);
3795 if (tcg_tb_size
< 0) {
3799 case QEMU_OPTION_icount
:
3800 icount_option
= optarg
;
3802 case QEMU_OPTION_incoming
:
3804 runstate_set(RUN_STATE_INMIGRATE
);
3806 case QEMU_OPTION_nodefaults
:
3809 case QEMU_OPTION_xen_domid
:
3810 if (!(xen_available())) {
3811 printf("Option %s not supported for this target\n", popt
->name
);
3814 xen_domid
= atoi(optarg
);
3816 case QEMU_OPTION_xen_create
:
3817 if (!(xen_available())) {
3818 printf("Option %s not supported for this target\n", popt
->name
);
3821 xen_mode
= XEN_CREATE
;
3823 case QEMU_OPTION_xen_attach
:
3824 if (!(xen_available())) {
3825 printf("Option %s not supported for this target\n", popt
->name
);
3828 xen_mode
= XEN_ATTACH
;
3830 case QEMU_OPTION_trace
:
3832 opts
= qemu_opts_parse(qemu_find_opts("trace"), optarg
, 0);
3836 trace_events
= qemu_opt_get(opts
, "events");
3837 trace_file
= qemu_opt_get(opts
, "file");
3840 case QEMU_OPTION_readconfig
:
3842 int ret
= qemu_read_config_file(optarg
);
3844 fprintf(stderr
, "read config %s: %s\n", optarg
,
3850 case QEMU_OPTION_spice
:
3851 olist
= qemu_find_opts("spice");
3853 fprintf(stderr
, "spice is not supported by this qemu build.\n");
3856 opts
= qemu_opts_parse(olist
, optarg
, 0);
3862 case QEMU_OPTION_writeconfig
:
3865 if (strcmp(optarg
, "-") == 0) {
3868 fp
= fopen(optarg
, "w");
3870 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
3874 qemu_config_write(fp
);
3880 case QEMU_OPTION_qtest
:
3881 qtest_chrdev
= optarg
;
3883 case QEMU_OPTION_qtest_log
:
3886 case QEMU_OPTION_sandbox
:
3887 opts
= qemu_opts_parse(qemu_find_opts("sandbox"), optarg
, 1);
3892 case QEMU_OPTION_add_fd
:
3894 opts
= qemu_opts_parse(qemu_find_opts("add-fd"), optarg
, 0);
3899 error_report("File descriptor passing is disabled on this "
3904 case QEMU_OPTION_object
:
3905 opts
= qemu_opts_parse(qemu_find_opts("object"), optarg
, 1);
3910 case QEMU_OPTION_realtime
:
3911 opts
= qemu_opts_parse(qemu_find_opts("realtime"), optarg
, 0);
3915 configure_realtime(opts
);
3917 case QEMU_OPTION_msg
:
3918 opts
= qemu_opts_parse(qemu_find_opts("msg"), optarg
, 0);
3922 configure_msg(opts
);
3925 os_parse_cmd_args(popt
->index
, optarg
);
3933 if (qemu_init_main_loop()) {
3934 fprintf(stderr
, "qemu_init_main_loop failed\n");
3938 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox
, NULL
, 0)) {
3942 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name
, NULL
, 1)) {
3947 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd
, NULL
, 1)) {
3951 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd
, NULL
, 1)) {
3956 if (machine_class
== NULL
) {
3957 fprintf(stderr
, "No machine specified, and there is no default.\n"
3958 "Use -machine help to list supported machines!\n");
3962 cpu_exec_init_all();
3964 current_machine
= MACHINE(object_new(object_class_get_name(
3965 OBJECT_CLASS(machine_class
))));
3966 object_property_add_child(object_get_root(), "machine",
3967 OBJECT(current_machine
), &error_abort
);
3969 if (machine_class
->hw_version
) {
3970 qemu_set_version(machine_class
->hw_version
);
3973 if (qemu_opts_foreach(qemu_find_opts("object"),
3974 object_create
, NULL
, 0) != 0) {
3978 /* Init CPU def lists, based on config
3979 * - Must be called after all the qemu_read_config_file() calls
3980 * - Must be called before list_cpus()
3981 * - Must be called before machine->init()
3985 if (cpu_model
&& is_help_option(cpu_model
)) {
3986 list_cpus(stdout
, &fprintf
, cpu_model
);
3990 /* Open the logfile at this point, if necessary. We can't open the logfile
3991 * when encountering either of the logging options (-d or -D) because the
3992 * other one may be encountered later on the command line, changing the
3993 * location or level of logging.
3998 qemu_set_log_filename(log_file
);
4001 mask
= qemu_str_to_log_mask(log_mask
);
4003 qemu_print_log_usage(stdout
);
4009 if (!is_daemonized()) {
4010 if (!trace_init_backends(trace_events
, trace_file
)) {
4015 /* If no data_dir is specified then try to find it relative to the
4017 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4018 data_dir
[data_dir_idx
] = os_find_datadir();
4019 if (data_dir
[data_dir_idx
] != NULL
) {
4023 /* If all else fails use the install path specified when building. */
4024 if (data_dir_idx
< ARRAY_SIZE(data_dir
)) {
4025 data_dir
[data_dir_idx
++] = CONFIG_QEMU_DATADIR
;
4028 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL
));
4030 machine_class
->max_cpus
= machine_class
->max_cpus
?: 1; /* Default to UP */
4031 if (smp_cpus
> machine_class
->max_cpus
) {
4032 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4033 "supported by machine `%s' (%d)\n", smp_cpus
,
4034 machine_class
->name
, machine_class
->max_cpus
);
4039 * Get the default machine options from the machine if it is not already
4040 * specified either by the configuration file or by the command line.
4042 if (machine_class
->default_machine_opts
) {
4043 qemu_opts_set_defaults(qemu_find_opts("machine"),
4044 machine_class
->default_machine_opts
, 0);
4047 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check
, NULL
, 0);
4048 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check
, NULL
, 0);
4050 if (!vga_model
&& !default_vga
) {
4051 vga_interface_type
= VGA_DEVICE
;
4053 if (!has_defaults
|| machine_class
->no_serial
) {
4056 if (!has_defaults
|| machine_class
->no_parallel
) {
4057 default_parallel
= 0;
4059 if (!has_defaults
|| !machine_class
->use_virtcon
) {
4060 default_virtcon
= 0;
4062 if (!has_defaults
|| !machine_class
->use_sclp
) {
4065 if (!has_defaults
|| machine_class
->no_floppy
) {
4068 if (!has_defaults
|| machine_class
->no_cdrom
) {
4071 if (!has_defaults
|| machine_class
->no_sdcard
) {
4074 if (!has_defaults
) {
4075 default_monitor
= 0;
4080 if (is_daemonized()) {
4081 /* According to documentation and historically, -nographic redirects
4082 * serial port, parallel port and monitor to stdio, which does not work
4083 * with -daemonize. We can redirect these to null instead, but since
4084 * -nographic is legacy, let's just error out.
4085 * We disallow -nographic only if all other ports are not redirected
4086 * explicitly, to not break existing legacy setups which uses
4087 * -nographic _and_ redirects all ports explicitly - this is valid
4088 * usage, -nographic is just a no-op in this case.
4090 if (display_type
== DT_NOGRAPHIC
4091 && (default_parallel
|| default_serial
4092 || default_monitor
|| default_virtcon
)) {
4093 fprintf(stderr
, "-nographic can not be used with -daemonize\n");
4096 #ifdef CONFIG_CURSES
4097 if (display_type
== DT_CURSES
) {
4098 fprintf(stderr
, "curses display can not be used with -daemonize\n");
4104 if (display_type
== DT_NOGRAPHIC
) {
4105 if (default_parallel
)
4106 add_device_config(DEV_PARALLEL
, "null");
4107 if (default_serial
&& default_monitor
) {
4108 add_device_config(DEV_SERIAL
, "mon:stdio");
4109 } else if (default_virtcon
&& default_monitor
) {
4110 add_device_config(DEV_VIRTCON
, "mon:stdio");
4111 } else if (default_sclp
&& default_monitor
) {
4112 add_device_config(DEV_SCLP
, "mon:stdio");
4115 add_device_config(DEV_SERIAL
, "stdio");
4116 if (default_virtcon
)
4117 add_device_config(DEV_VIRTCON
, "stdio");
4119 add_device_config(DEV_SCLP
, "stdio");
4121 if (default_monitor
)
4122 monitor_parse("stdio", "readline");
4126 add_device_config(DEV_SERIAL
, "vc:80Cx24C");
4127 if (default_parallel
)
4128 add_device_config(DEV_PARALLEL
, "vc:80Cx24C");
4129 if (default_monitor
)
4130 monitor_parse("vc:80Cx24C", "readline");
4131 if (default_virtcon
)
4132 add_device_config(DEV_VIRTCON
, "vc:80Cx24C");
4134 add_device_config(DEV_SCLP
, "vc:80Cx24C");
4138 if (display_type
== DT_DEFAULT
&& !display_remote
) {
4139 #if defined(CONFIG_GTK)
4140 display_type
= DT_GTK
;
4141 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4142 display_type
= DT_SDL
;
4143 #elif defined(CONFIG_VNC)
4144 vnc_display
= "localhost:0,to=99";
4147 display_type
= DT_NONE
;
4151 if ((no_frame
|| alt_grab
|| ctrl_grab
) && display_type
!= DT_SDL
) {
4152 fprintf(stderr
, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4153 "for SDL, ignoring option\n");
4155 if (no_quit
&& (display_type
!= DT_GTK
&& display_type
!= DT_SDL
)) {
4156 fprintf(stderr
, "-no-quit is only valid for GTK and SDL, "
4157 "ignoring option\n");
4160 #if defined(CONFIG_GTK)
4161 if (display_type
== DT_GTK
) {
4162 early_gtk_display_init();
4168 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func
, NULL
, 1) != 0)
4170 #ifdef CONFIG_VIRTFS
4171 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func
, NULL
, 1) != 0) {
4176 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
4181 /* store value for the future use */
4182 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size
);
4184 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func
, NULL
, 0)
4189 machine_opts
= qemu_get_machine_opts();
4190 if (qemu_opt_foreach(machine_opts
, object_set_property
, current_machine
,
4192 object_unref(OBJECT(current_machine
));
4196 configure_accelerator(machine_class
);
4199 Error
*local_err
= NULL
;
4200 qtest_init(qtest_chrdev
, qtest_log
, &local_err
);
4202 error_report("%s", error_get_pretty(local_err
));
4203 error_free(local_err
);
4208 machine_opts
= qemu_get_machine_opts();
4209 kernel_filename
= qemu_opt_get(machine_opts
, "kernel");
4210 initrd_filename
= qemu_opt_get(machine_opts
, "initrd");
4211 kernel_cmdline
= qemu_opt_get(machine_opts
, "append");
4212 bios_name
= qemu_opt_get(machine_opts
, "firmware");
4214 boot_order
= machine_class
->default_boot_order
;
4215 opts
= qemu_opts_find(qemu_find_opts("boot-opts"), NULL
);
4217 char *normal_boot_order
;
4218 const char *order
, *once
;
4220 order
= qemu_opt_get(opts
, "order");
4222 validate_bootdevices(order
);
4226 once
= qemu_opt_get(opts
, "once");
4228 validate_bootdevices(once
);
4229 normal_boot_order
= g_strdup(boot_order
);
4231 qemu_register_reset(restore_boot_order
, normal_boot_order
);
4234 boot_menu
= qemu_opt_get_bool(opts
, "menu", boot_menu
);
4235 boot_strict
= qemu_opt_get_bool(opts
, "strict", false);
4238 if (!kernel_cmdline
) {
4239 kernel_cmdline
= "";
4240 current_machine
->kernel_cmdline
= (char *)kernel_cmdline
;
4243 linux_boot
= (kernel_filename
!= NULL
);
4245 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
4246 fprintf(stderr
, "-append only allowed with -kernel option\n");
4250 if (!linux_boot
&& initrd_filename
!= NULL
) {
4251 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
4255 if (!linux_boot
&& qemu_opt_get(machine_opts
, "dtb")) {
4256 fprintf(stderr
, "-dtb only allowed with -kernel option\n");
4260 os_set_line_buffering();
4262 qemu_init_cpu_loop();
4263 qemu_mutex_lock_iothread();
4266 /* spice needs the timers to be initialized by this point */
4270 if (icount_option
&& (kvm_enabled() || xen_enabled())) {
4271 fprintf(stderr
, "-icount is not allowed with kvm or xen\n");
4274 configure_icount(icount_option
);
4276 /* clean up network at qemu process termination */
4277 atexit(&net_cleanup
);
4279 if (net_init_clients() < 0) {
4284 if (tpm_init() < 0) {
4289 /* init the bluetooth world */
4290 if (foreach_device_config(DEV_BT
, bt_parse
))
4293 if (!xen_enabled()) {
4294 /* On 32-bit hosts, QEMU is limited by virtual address space */
4295 if (ram_size
> (2047 << 20) && HOST_LONG_BITS
== 32) {
4296 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4304 /* open the virtual block devices */
4306 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot
, NULL
, 0);
4307 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func
,
4308 &machine_class
->block_default_type
, 1) != 0) {
4312 default_drive(default_cdrom
, snapshot
, machine_class
->block_default_type
, 2,
4314 default_drive(default_floppy
, snapshot
, IF_FLOPPY
, 0, FD_OPTS
);
4315 default_drive(default_sdcard
, snapshot
, IF_SD
, 0, SD_OPTS
);
4317 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func
,
4324 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func
, NULL
, 1) != 0) {
4328 if (foreach_device_config(DEV_SERIAL
, serial_parse
) < 0)
4330 if (foreach_device_config(DEV_PARALLEL
, parallel_parse
) < 0)
4332 if (foreach_device_config(DEV_VIRTCON
, virtcon_parse
) < 0)
4334 if (foreach_device_config(DEV_SCLP
, sclp_parse
) < 0) {
4337 if (foreach_device_config(DEV_DEBUGCON
, debugcon_parse
) < 0)
4340 /* If no default VGA is requested, the default is "none". */
4342 if (cirrus_vga_available()) {
4343 vga_model
= "cirrus";
4344 } else if (vga_available()) {
4349 select_vgahw(vga_model
);
4353 i
= select_watchdog(watchdog
);
4355 exit (i
== 1 ? 1 : 0);
4358 if (machine_class
->compat_props
) {
4359 qdev_prop_register_global_list(machine_class
->compat_props
);
4363 qdev_machine_init();
4365 current_machine
->ram_size
= ram_size
;
4366 current_machine
->maxram_size
= maxram_size
;
4367 current_machine
->ram_slots
= ram_slots
;
4368 current_machine
->boot_order
= boot_order
;
4369 current_machine
->cpu_model
= cpu_model
;
4371 machine_class
->init(current_machine
);
4375 cpu_synchronize_all_post_init();
4379 /* init USB devices */
4380 if (usb_enabled(false)) {
4381 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
4385 /* init generic devices */
4386 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func
, NULL
, 1) != 0)
4389 net_check_clients();
4391 ds
= init_displaystate();
4393 /* init local displays */
4394 switch (display_type
) {
4396 (void)ds
; /* avoid warning if no display is configured */
4398 #if defined(CONFIG_CURSES)
4400 curses_display_init(ds
, full_screen
);
4403 #if defined(CONFIG_SDL)
4405 sdl_display_init(ds
, full_screen
, no_frame
);
4407 #elif defined(CONFIG_COCOA)
4409 cocoa_display_init(ds
, full_screen
);
4412 #if defined(CONFIG_GTK)
4414 gtk_display_init(ds
, full_screen
, grab_on_hover
);
4421 /* must be after terminal init, SDL library changes signal handlers */
4422 os_setup_signal_handling();
4425 /* init remote displays */
4427 Error
*local_err
= NULL
;
4428 vnc_display_init(ds
);
4429 vnc_display_open(ds
, vnc_display
, &local_err
);
4430 if (local_err
!= NULL
) {
4431 error_report("Failed to start VNC server on `%s': %s",
4432 vnc_display
, error_get_pretty(local_err
));
4433 error_free(local_err
);
4437 if (show_vnc_port
) {
4438 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
4444 qemu_spice_display_init();
4448 if (foreach_device_config(DEV_GDB
, gdbserver_start
) < 0) {
4452 qdev_machine_creation_done();
4454 if (rom_load_all() != 0) {
4455 fprintf(stderr
, "rom loading failed\n");
4459 /* TODO: once all bus devices are qdevified, this should be done
4460 * when bus is created by qdev.c */
4461 qemu_register_reset(qbus_reset_all_fn
, sysbus_get_default());
4462 qemu_run_machine_init_done_notifiers();
4464 /* Done notifiers can load ROMs */
4467 qemu_system_reset(VMRESET_SILENT
);
4469 if (load_vmstate(loadvm
) < 0) {
4474 qdev_prop_check_global();
4477 Error
*local_err
= NULL
;
4478 qemu_start_incoming_migration(incoming
, &local_err
);
4480 error_report("-incoming %s: %s", incoming
,
4481 error_get_pretty(local_err
));
4482 error_free(local_err
);
4485 } else if (autostart
) {
4491 if (is_daemonized()) {
4492 if (!trace_init_backends(trace_events
, trace_file
)) {