hw/i386/acpi-build.c: Fix memory leak in acpi_build_tables_cleanup()
[qemu/ar7.git] / vl.c
blob35c1333eeeac3e77c50151b06a279ab26026b5e0
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 static int display_remote;
133 const char* keyboard_layout = NULL;
134 ram_addr_t ram_size;
135 const char *mem_path = NULL;
136 int mem_prealloc = 0; /* force preallocation of physical target memory */
137 bool enable_mlock = false;
138 int nb_nics;
139 NICInfo nd_table[MAX_NICS];
140 int autostart;
141 static int rtc_utc = 1;
142 static int rtc_date_offset = -1; /* -1 means no change */
143 QEMUClockType rtc_clock;
144 int vga_interface_type = VGA_NONE;
145 static int full_screen = 0;
146 static int no_frame = 0;
147 int no_quit = 0;
148 #ifdef CONFIG_GTK
149 static bool grab_on_hover;
150 #endif
151 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155 int win2k_install_hack = 0;
156 int singlestep = 0;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 #ifdef CONFIG_VNC
162 const char *vnc_display;
163 #endif
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int semihosting_enabled = 0;
175 int old_param = 0;
176 const char *qemu_name;
177 int alt_grab = 0;
178 int ctrl_grab = 0;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 bool boot_strict;
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
187 int icount_align_option;
189 int nb_numa_nodes;
190 int max_numa_nodeid;
191 NodeInfo numa_info[MAX_NODES];
193 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 uint8_t qemu_uuid[16];
197 bool qemu_uuid_set;
199 static QEMUBootSetHandler *boot_set_handler;
200 static void *boot_set_opaque;
202 static NotifierList exit_notifiers =
203 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
205 static NotifierList machine_init_done_notifiers =
206 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
208 bool xen_allowed;
209 uint32_t xen_domid;
210 enum xen_mode xen_mode = XEN_EMULATE;
212 static int has_defaults = 1;
213 static int default_serial = 1;
214 static int default_parallel = 1;
215 static int default_virtcon = 1;
216 static int default_sclp = 1;
217 static int default_monitor = 1;
218 static int default_floppy = 1;
219 static int default_cdrom = 1;
220 static int default_sdcard = 1;
221 static int default_vga = 1;
223 static struct {
224 const char *driver;
225 int *flag;
226 } default_list[] = {
227 { .driver = "isa-serial", .flag = &default_serial },
228 { .driver = "isa-parallel", .flag = &default_parallel },
229 { .driver = "isa-fdc", .flag = &default_floppy },
230 { .driver = "ide-cd", .flag = &default_cdrom },
231 { .driver = "ide-hd", .flag = &default_cdrom },
232 { .driver = "ide-drive", .flag = &default_cdrom },
233 { .driver = "scsi-cd", .flag = &default_cdrom },
234 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
235 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
236 { .driver = "virtio-serial", .flag = &default_virtcon },
237 { .driver = "VGA", .flag = &default_vga },
238 { .driver = "isa-vga", .flag = &default_vga },
239 { .driver = "cirrus-vga", .flag = &default_vga },
240 { .driver = "isa-cirrus-vga", .flag = &default_vga },
241 { .driver = "vmware-svga", .flag = &default_vga },
242 { .driver = "qxl-vga", .flag = &default_vga },
245 static QemuOptsList qemu_rtc_opts = {
246 .name = "rtc",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
248 .desc = {
250 .name = "base",
251 .type = QEMU_OPT_STRING,
253 .name = "clock",
254 .type = QEMU_OPT_STRING,
256 .name = "driftfix",
257 .type = QEMU_OPT_STRING,
259 { /* end of list */ }
263 static QemuOptsList qemu_sandbox_opts = {
264 .name = "sandbox",
265 .implied_opt_name = "enable",
266 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
267 .desc = {
269 .name = "enable",
270 .type = QEMU_OPT_BOOL,
272 { /* end of list */ }
276 static QemuOptsList qemu_trace_opts = {
277 .name = "trace",
278 .implied_opt_name = "trace",
279 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
280 .desc = {
282 .name = "events",
283 .type = QEMU_OPT_STRING,
285 .name = "file",
286 .type = QEMU_OPT_STRING,
288 { /* end of list */ }
292 static QemuOptsList qemu_option_rom_opts = {
293 .name = "option-rom",
294 .implied_opt_name = "romfile",
295 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
296 .desc = {
298 .name = "bootindex",
299 .type = QEMU_OPT_NUMBER,
300 }, {
301 .name = "romfile",
302 .type = QEMU_OPT_STRING,
304 { /* end of list */ }
308 static QemuOptsList qemu_machine_opts = {
309 .name = "machine",
310 .implied_opt_name = "type",
311 .merge_lists = true,
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
313 .desc = {
315 .name = "type",
316 .type = QEMU_OPT_STRING,
317 .help = "emulated machine"
318 }, {
319 .name = "accel",
320 .type = QEMU_OPT_STRING,
321 .help = "accelerator list",
322 }, {
323 .name = "kernel_irqchip",
324 .type = QEMU_OPT_BOOL,
325 .help = "use KVM in-kernel irqchip",
326 }, {
327 .name = "kvm_shadow_mem",
328 .type = QEMU_OPT_SIZE,
329 .help = "KVM shadow MMU size",
330 }, {
331 .name = "kernel",
332 .type = QEMU_OPT_STRING,
333 .help = "Linux kernel image file",
334 }, {
335 .name = "initrd",
336 .type = QEMU_OPT_STRING,
337 .help = "Linux initial ramdisk file",
338 }, {
339 .name = "append",
340 .type = QEMU_OPT_STRING,
341 .help = "Linux kernel command line",
342 }, {
343 .name = "dtb",
344 .type = QEMU_OPT_STRING,
345 .help = "Linux kernel device tree file",
346 }, {
347 .name = "dumpdtb",
348 .type = QEMU_OPT_STRING,
349 .help = "Dump current dtb to a file and quit",
350 }, {
351 .name = "phandle_start",
352 .type = QEMU_OPT_NUMBER,
353 .help = "The first phandle ID we may generate dynamically",
354 }, {
355 .name = "dt_compatible",
356 .type = QEMU_OPT_STRING,
357 .help = "Overrides the \"compatible\" property of the dt root node",
358 }, {
359 .name = "dump-guest-core",
360 .type = QEMU_OPT_BOOL,
361 .help = "Include guest memory in a core dump",
362 }, {
363 .name = "mem-merge",
364 .type = QEMU_OPT_BOOL,
365 .help = "enable/disable memory merge support",
367 .name = "usb",
368 .type = QEMU_OPT_BOOL,
369 .help = "Set on/off to enable/disable usb",
371 .name = "firmware",
372 .type = QEMU_OPT_STRING,
373 .help = "firmware image",
375 .name = "kvm-type",
376 .type = QEMU_OPT_STRING,
377 .help = "Specifies the KVM virtualization mode (HV, PR)",
379 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
380 .type = QEMU_OPT_SIZE,
381 .help = "maximum ram below the 4G boundary (32bit boundary)",
382 }, {
383 .name = PC_MACHINE_VMPORT,
384 .type = QEMU_OPT_BOOL,
385 .help = "Enable vmport (pc & q35)",
387 .name = "iommu",
388 .type = QEMU_OPT_BOOL,
389 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
391 { /* End of list */ }
395 static QemuOptsList qemu_boot_opts = {
396 .name = "boot-opts",
397 .implied_opt_name = "order",
398 .merge_lists = true,
399 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
400 .desc = {
402 .name = "order",
403 .type = QEMU_OPT_STRING,
404 }, {
405 .name = "once",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "menu",
409 .type = QEMU_OPT_BOOL,
410 }, {
411 .name = "splash",
412 .type = QEMU_OPT_STRING,
413 }, {
414 .name = "splash-time",
415 .type = QEMU_OPT_STRING,
416 }, {
417 .name = "reboot-timeout",
418 .type = QEMU_OPT_STRING,
419 }, {
420 .name = "strict",
421 .type = QEMU_OPT_BOOL,
423 { /*End of list */ }
427 static QemuOptsList qemu_add_fd_opts = {
428 .name = "add-fd",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
430 .desc = {
432 .name = "fd",
433 .type = QEMU_OPT_NUMBER,
434 .help = "file descriptor of which a duplicate is added to fd set",
436 .name = "set",
437 .type = QEMU_OPT_NUMBER,
438 .help = "ID of the fd set to add fd to",
440 .name = "opaque",
441 .type = QEMU_OPT_STRING,
442 .help = "free-form string used to describe fd",
444 { /* end of list */ }
448 static QemuOptsList qemu_object_opts = {
449 .name = "object",
450 .implied_opt_name = "qom-type",
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
452 .desc = {
457 static QemuOptsList qemu_tpmdev_opts = {
458 .name = "tpmdev",
459 .implied_opt_name = "type",
460 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
461 .desc = {
462 /* options are defined in the TPM backends */
463 { /* end of list */ }
467 static QemuOptsList qemu_realtime_opts = {
468 .name = "realtime",
469 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
470 .desc = {
472 .name = "mlock",
473 .type = QEMU_OPT_BOOL,
475 { /* end of list */ }
479 static QemuOptsList qemu_msg_opts = {
480 .name = "msg",
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
482 .desc = {
484 .name = "timestamp",
485 .type = QEMU_OPT_BOOL,
487 { /* end of list */ }
491 static QemuOptsList qemu_name_opts = {
492 .name = "name",
493 .implied_opt_name = "guest",
494 .merge_lists = true,
495 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
496 .desc = {
498 .name = "guest",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the name of the guest.\n"
501 "This name will be displayed in the SDL window caption.\n"
502 "The name will also be used for the VNC server",
503 }, {
504 .name = "process",
505 .type = QEMU_OPT_STRING,
506 .help = "Sets the name of the QEMU process, as shown in top etc",
507 }, {
508 .name = "debug-threads",
509 .type = QEMU_OPT_BOOL,
510 .help = "When enabled, name the individual threads; defaults off.\n"
511 "NOTE: The thread names are for debugging and not a\n"
512 "stable API.",
514 { /* End of list */ }
518 static QemuOptsList qemu_mem_opts = {
519 .name = "memory",
520 .implied_opt_name = "size",
521 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
522 .merge_lists = true,
523 .desc = {
525 .name = "size",
526 .type = QEMU_OPT_SIZE,
529 .name = "slots",
530 .type = QEMU_OPT_NUMBER,
533 .name = "maxmem",
534 .type = QEMU_OPT_SIZE,
536 { /* end of list */ }
540 static QemuOptsList qemu_icount_opts = {
541 .name = "icount",
542 .implied_opt_name = "shift",
543 .merge_lists = true,
544 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
545 .desc = {
547 .name = "shift",
548 .type = QEMU_OPT_STRING,
549 }, {
550 .name = "align",
551 .type = QEMU_OPT_BOOL,
553 { /* end of list */ }
558 * Get machine options
560 * Returns: machine options (never null).
562 QemuOpts *qemu_get_machine_opts(void)
564 return qemu_find_opts_singleton("machine");
567 const char *qemu_get_vm_name(void)
569 return qemu_name;
572 static void res_free(void)
574 if (boot_splash_filedata != NULL) {
575 g_free(boot_splash_filedata);
576 boot_splash_filedata = NULL;
580 static int default_driver_check(QemuOpts *opts, void *opaque)
582 const char *driver = qemu_opt_get(opts, "driver");
583 int i;
585 if (!driver)
586 return 0;
587 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
588 if (strcmp(default_list[i].driver, driver) != 0)
589 continue;
590 *(default_list[i].flag) = 0;
592 return 0;
595 /***********************************************************/
596 /* QEMU state */
598 static RunState current_run_state = RUN_STATE_PRELAUNCH;
600 /* We use RUN_STATE_MAX but any invalid value will do */
601 static RunState vmstop_requested = RUN_STATE_MAX;
602 static QemuMutex vmstop_lock;
604 typedef struct {
605 RunState from;
606 RunState to;
607 } RunStateTransition;
609 static const RunStateTransition runstate_transitions_def[] = {
610 /* from -> to */
611 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
612 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
615 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
617 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
618 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
621 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
624 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
627 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
629 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
630 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
631 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
633 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
634 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
636 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
638 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
639 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
640 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
641 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
642 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
644 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
645 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
646 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
647 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
649 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
651 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
652 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
654 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
655 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
656 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
657 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
660 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
662 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
663 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
665 { RUN_STATE_MAX, RUN_STATE_MAX },
668 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
670 bool runstate_check(RunState state)
672 return current_run_state == state;
675 static void runstate_init(void)
677 const RunStateTransition *p;
679 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
680 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
681 runstate_valid_transitions[p->from][p->to] = true;
684 qemu_mutex_init(&vmstop_lock);
687 /* This function will abort() on invalid state transitions */
688 void runstate_set(RunState new_state)
690 assert(new_state < RUN_STATE_MAX);
692 if (!runstate_valid_transitions[current_run_state][new_state]) {
693 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
694 RunState_lookup[current_run_state],
695 RunState_lookup[new_state]);
696 abort();
698 trace_runstate_set(new_state);
699 current_run_state = new_state;
702 int runstate_is_running(void)
704 return runstate_check(RUN_STATE_RUNNING);
707 bool runstate_needs_reset(void)
709 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
710 runstate_check(RUN_STATE_SHUTDOWN);
713 StatusInfo *qmp_query_status(Error **errp)
715 StatusInfo *info = g_malloc0(sizeof(*info));
717 info->running = runstate_is_running();
718 info->singlestep = singlestep;
719 info->status = current_run_state;
721 return info;
724 static bool qemu_vmstop_requested(RunState *r)
726 qemu_mutex_lock(&vmstop_lock);
727 *r = vmstop_requested;
728 vmstop_requested = RUN_STATE_MAX;
729 qemu_mutex_unlock(&vmstop_lock);
730 return *r < RUN_STATE_MAX;
733 void qemu_system_vmstop_request_prepare(void)
735 qemu_mutex_lock(&vmstop_lock);
738 void qemu_system_vmstop_request(RunState state)
740 vmstop_requested = state;
741 qemu_mutex_unlock(&vmstop_lock);
742 qemu_notify_event();
745 void vm_start(void)
747 RunState requested;
749 qemu_vmstop_requested(&requested);
750 if (runstate_is_running() && requested == RUN_STATE_MAX) {
751 return;
754 /* Ensure that a STOP/RESUME pair of events is emitted if a
755 * vmstop request was pending. The BLOCK_IO_ERROR event, for
756 * example, according to documentation is always followed by
757 * the STOP event.
759 if (runstate_is_running()) {
760 qapi_event_send_stop(&error_abort);
761 } else {
762 cpu_enable_ticks();
763 runstate_set(RUN_STATE_RUNNING);
764 vm_state_notify(1, RUN_STATE_RUNNING);
765 resume_all_vcpus();
768 qapi_event_send_resume(&error_abort);
772 /***********************************************************/
773 /* real time host monotonic timer */
775 /***********************************************************/
776 /* host time/date access */
777 void qemu_get_timedate(struct tm *tm, int offset)
779 time_t ti;
781 time(&ti);
782 ti += offset;
783 if (rtc_date_offset == -1) {
784 if (rtc_utc)
785 gmtime_r(&ti, tm);
786 else
787 localtime_r(&ti, tm);
788 } else {
789 ti -= rtc_date_offset;
790 gmtime_r(&ti, tm);
794 int qemu_timedate_diff(struct tm *tm)
796 time_t seconds;
798 if (rtc_date_offset == -1)
799 if (rtc_utc)
800 seconds = mktimegm(tm);
801 else {
802 struct tm tmp = *tm;
803 tmp.tm_isdst = -1; /* use timezone to figure it out */
804 seconds = mktime(&tmp);
806 else
807 seconds = mktimegm(tm) + rtc_date_offset;
809 return seconds - time(NULL);
812 static void configure_rtc_date_offset(const char *startdate, int legacy)
814 time_t rtc_start_date;
815 struct tm tm;
817 if (!strcmp(startdate, "now") && legacy) {
818 rtc_date_offset = -1;
819 } else {
820 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
821 &tm.tm_year,
822 &tm.tm_mon,
823 &tm.tm_mday,
824 &tm.tm_hour,
825 &tm.tm_min,
826 &tm.tm_sec) == 6) {
827 /* OK */
828 } else if (sscanf(startdate, "%d-%d-%d",
829 &tm.tm_year,
830 &tm.tm_mon,
831 &tm.tm_mday) == 3) {
832 tm.tm_hour = 0;
833 tm.tm_min = 0;
834 tm.tm_sec = 0;
835 } else {
836 goto date_fail;
838 tm.tm_year -= 1900;
839 tm.tm_mon--;
840 rtc_start_date = mktimegm(&tm);
841 if (rtc_start_date == -1) {
842 date_fail:
843 fprintf(stderr, "Invalid date format. Valid formats are:\n"
844 "'2006-06-17T16:01:21' or '2006-06-17'\n");
845 exit(1);
847 rtc_date_offset = time(NULL) - rtc_start_date;
851 static void configure_rtc(QemuOpts *opts)
853 const char *value;
855 value = qemu_opt_get(opts, "base");
856 if (value) {
857 if (!strcmp(value, "utc")) {
858 rtc_utc = 1;
859 } else if (!strcmp(value, "localtime")) {
860 rtc_utc = 0;
861 } else {
862 configure_rtc_date_offset(value, 0);
865 value = qemu_opt_get(opts, "clock");
866 if (value) {
867 if (!strcmp(value, "host")) {
868 rtc_clock = QEMU_CLOCK_HOST;
869 } else if (!strcmp(value, "rt")) {
870 rtc_clock = QEMU_CLOCK_REALTIME;
871 } else if (!strcmp(value, "vm")) {
872 rtc_clock = QEMU_CLOCK_VIRTUAL;
873 } else {
874 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
875 exit(1);
878 value = qemu_opt_get(opts, "driftfix");
879 if (value) {
880 if (!strcmp(value, "slew")) {
881 static GlobalProperty slew_lost_ticks[] = {
883 .driver = "mc146818rtc",
884 .property = "lost_tick_policy",
885 .value = "slew",
887 { /* end of list */ }
890 qdev_prop_register_global_list(slew_lost_ticks);
891 } else if (!strcmp(value, "none")) {
892 /* discard is default */
893 } else {
894 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
895 exit(1);
900 /***********************************************************/
901 /* Bluetooth support */
902 static int nb_hcis;
903 static int cur_hci;
904 static struct HCIInfo *hci_table[MAX_NICS];
906 struct HCIInfo *qemu_next_hci(void)
908 if (cur_hci == nb_hcis)
909 return &null_hci;
911 return hci_table[cur_hci++];
914 static int bt_hci_parse(const char *str)
916 struct HCIInfo *hci;
917 bdaddr_t bdaddr;
919 if (nb_hcis >= MAX_NICS) {
920 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
921 return -1;
924 hci = hci_init(str);
925 if (!hci)
926 return -1;
928 bdaddr.b[0] = 0x52;
929 bdaddr.b[1] = 0x54;
930 bdaddr.b[2] = 0x00;
931 bdaddr.b[3] = 0x12;
932 bdaddr.b[4] = 0x34;
933 bdaddr.b[5] = 0x56 + nb_hcis;
934 hci->bdaddr_set(hci, bdaddr.b);
936 hci_table[nb_hcis++] = hci;
938 return 0;
941 static void bt_vhci_add(int vlan_id)
943 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
945 if (!vlan->slave)
946 fprintf(stderr, "qemu: warning: adding a VHCI to "
947 "an empty scatternet %i\n", vlan_id);
949 bt_vhci_init(bt_new_hci(vlan));
952 static struct bt_device_s *bt_device_add(const char *opt)
954 struct bt_scatternet_s *vlan;
955 int vlan_id = 0;
956 char *endp = strstr(opt, ",vlan=");
957 int len = (endp ? endp - opt : strlen(opt)) + 1;
958 char devname[10];
960 pstrcpy(devname, MIN(sizeof(devname), len), opt);
962 if (endp) {
963 vlan_id = strtol(endp + 6, &endp, 0);
964 if (*endp) {
965 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
966 return 0;
970 vlan = qemu_find_bt_vlan(vlan_id);
972 if (!vlan->slave)
973 fprintf(stderr, "qemu: warning: adding a slave device to "
974 "an empty scatternet %i\n", vlan_id);
976 if (!strcmp(devname, "keyboard"))
977 return bt_keyboard_init(vlan);
979 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
980 return 0;
983 static int bt_parse(const char *opt)
985 const char *endp, *p;
986 int vlan;
988 if (strstart(opt, "hci", &endp)) {
989 if (!*endp || *endp == ',') {
990 if (*endp)
991 if (!strstart(endp, ",vlan=", 0))
992 opt = endp + 1;
994 return bt_hci_parse(opt);
996 } else if (strstart(opt, "vhci", &endp)) {
997 if (!*endp || *endp == ',') {
998 if (*endp) {
999 if (strstart(endp, ",vlan=", &p)) {
1000 vlan = strtol(p, (char **) &endp, 0);
1001 if (*endp) {
1002 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1003 return 1;
1005 } else {
1006 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1007 return 1;
1009 } else
1010 vlan = 0;
1012 bt_vhci_add(vlan);
1013 return 0;
1015 } else if (strstart(opt, "device:", &endp))
1016 return !bt_device_add(endp);
1018 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1019 return 1;
1022 static int parse_sandbox(QemuOpts *opts, void *opaque)
1024 /* FIXME: change this to true for 1.3 */
1025 if (qemu_opt_get_bool(opts, "enable", false)) {
1026 #ifdef CONFIG_SECCOMP
1027 if (seccomp_start() < 0) {
1028 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1029 "failed to install seccomp syscall filter in the kernel");
1030 return -1;
1032 #else
1033 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1034 "sandboxing request but seccomp is not compiled into this build");
1035 return -1;
1036 #endif
1039 return 0;
1042 static int parse_name(QemuOpts *opts, void *opaque)
1044 const char *proc_name;
1046 if (qemu_opt_get(opts, "debug-threads")) {
1047 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1049 qemu_name = qemu_opt_get(opts, "guest");
1051 proc_name = qemu_opt_get(opts, "process");
1052 if (proc_name) {
1053 os_set_proc_name(proc_name);
1056 return 0;
1059 bool usb_enabled(bool default_usb)
1061 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1062 has_defaults && default_usb);
1065 #ifndef _WIN32
1066 static int parse_add_fd(QemuOpts *opts, void *opaque)
1068 int fd, dupfd, flags;
1069 int64_t fdset_id;
1070 const char *fd_opaque = NULL;
1072 fd = qemu_opt_get_number(opts, "fd", -1);
1073 fdset_id = qemu_opt_get_number(opts, "set", -1);
1074 fd_opaque = qemu_opt_get(opts, "opaque");
1076 if (fd < 0) {
1077 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1078 "fd option is required and must be non-negative");
1079 return -1;
1082 if (fd <= STDERR_FILENO) {
1083 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1084 "fd cannot be a standard I/O stream");
1085 return -1;
1089 * All fds inherited across exec() necessarily have FD_CLOEXEC
1090 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1092 flags = fcntl(fd, F_GETFD);
1093 if (flags == -1 || (flags & FD_CLOEXEC)) {
1094 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1095 "fd is not valid or already in use");
1096 return -1;
1099 if (fdset_id < 0) {
1100 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1101 "set option is required and must be non-negative");
1102 return -1;
1105 #ifdef F_DUPFD_CLOEXEC
1106 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1107 #else
1108 dupfd = dup(fd);
1109 if (dupfd != -1) {
1110 qemu_set_cloexec(dupfd);
1112 #endif
1113 if (dupfd == -1) {
1114 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1115 "Error duplicating fd: %s", strerror(errno));
1116 return -1;
1119 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1120 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1121 fd_opaque, NULL);
1123 return 0;
1126 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1128 int fd;
1130 fd = qemu_opt_get_number(opts, "fd", -1);
1131 close(fd);
1133 return 0;
1135 #endif
1137 /***********************************************************/
1138 /* QEMU Block devices */
1140 #define HD_OPTS "media=disk"
1141 #define CDROM_OPTS "media=cdrom"
1142 #define FD_OPTS ""
1143 #define PFLASH_OPTS ""
1144 #define MTD_OPTS ""
1145 #define SD_OPTS ""
1147 static int drive_init_func(QemuOpts *opts, void *opaque)
1149 BlockInterfaceType *block_default_type = opaque;
1151 return drive_new(opts, *block_default_type) == NULL;
1154 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1156 if (qemu_opt_get(opts, "snapshot") == NULL) {
1157 qemu_opt_set(opts, "snapshot", "on");
1159 return 0;
1162 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1163 int index, const char *optstr)
1165 QemuOpts *opts;
1166 DriveInfo *dinfo;
1168 if (!enable || drive_get_by_index(type, index)) {
1169 return;
1172 opts = drive_add(type, index, NULL, optstr);
1173 if (snapshot) {
1174 drive_enable_snapshot(opts, NULL);
1177 dinfo = drive_new(opts, type);
1178 if (!dinfo) {
1179 exit(1);
1181 dinfo->is_default = true;
1185 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1187 boot_set_handler = func;
1188 boot_set_opaque = opaque;
1191 int qemu_boot_set(const char *boot_order)
1193 if (!boot_set_handler) {
1194 return -EINVAL;
1196 return boot_set_handler(boot_set_opaque, boot_order);
1199 static void validate_bootdevices(const char *devices)
1201 /* We just do some generic consistency checks */
1202 const char *p;
1203 int bitmap = 0;
1205 for (p = devices; *p != '\0'; p++) {
1206 /* Allowed boot devices are:
1207 * a-b: floppy disk drives
1208 * c-f: IDE disk drives
1209 * g-m: machine implementation dependent drives
1210 * n-p: network devices
1211 * It's up to each machine implementation to check if the given boot
1212 * devices match the actual hardware implementation and firmware
1213 * features.
1215 if (*p < 'a' || *p > 'p') {
1216 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1217 exit(1);
1219 if (bitmap & (1 << (*p - 'a'))) {
1220 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1221 exit(1);
1223 bitmap |= 1 << (*p - 'a');
1227 static void restore_boot_order(void *opaque)
1229 char *normal_boot_order = opaque;
1230 static int first = 1;
1232 /* Restore boot order and remove ourselves after the first boot */
1233 if (first) {
1234 first = 0;
1235 return;
1238 qemu_boot_set(normal_boot_order);
1240 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1241 g_free(normal_boot_order);
1244 static QemuOptsList qemu_smp_opts = {
1245 .name = "smp-opts",
1246 .implied_opt_name = "cpus",
1247 .merge_lists = true,
1248 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1249 .desc = {
1251 .name = "cpus",
1252 .type = QEMU_OPT_NUMBER,
1253 }, {
1254 .name = "sockets",
1255 .type = QEMU_OPT_NUMBER,
1256 }, {
1257 .name = "cores",
1258 .type = QEMU_OPT_NUMBER,
1259 }, {
1260 .name = "threads",
1261 .type = QEMU_OPT_NUMBER,
1262 }, {
1263 .name = "maxcpus",
1264 .type = QEMU_OPT_NUMBER,
1266 { /*End of list */ }
1270 static void smp_parse(QemuOpts *opts)
1272 if (opts) {
1274 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1275 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1276 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1277 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1279 /* compute missing values, prefer sockets over cores over threads */
1280 if (cpus == 0 || sockets == 0) {
1281 sockets = sockets > 0 ? sockets : 1;
1282 cores = cores > 0 ? cores : 1;
1283 threads = threads > 0 ? threads : 1;
1284 if (cpus == 0) {
1285 cpus = cores * threads * sockets;
1287 } else {
1288 if (cores == 0) {
1289 threads = threads > 0 ? threads : 1;
1290 cores = cpus / (sockets * threads);
1291 } else {
1292 threads = cpus / (cores * sockets);
1296 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1298 smp_cpus = cpus;
1299 smp_cores = cores > 0 ? cores : 1;
1300 smp_threads = threads > 0 ? threads : 1;
1304 if (max_cpus == 0) {
1305 max_cpus = smp_cpus;
1308 if (max_cpus > MAX_CPUMASK_BITS) {
1309 fprintf(stderr, "Unsupported number of maxcpus\n");
1310 exit(1);
1312 if (max_cpus < smp_cpus) {
1313 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1314 exit(1);
1319 static void realtime_init(void)
1321 if (enable_mlock) {
1322 if (os_mlock() < 0) {
1323 fprintf(stderr, "qemu: locking memory failed\n");
1324 exit(1);
1330 static void configure_msg(QemuOpts *opts)
1332 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1335 /***********************************************************/
1336 /* USB devices */
1338 static int usb_device_add(const char *devname)
1340 USBDevice *dev = NULL;
1341 #ifndef CONFIG_LINUX
1342 const char *p;
1343 #endif
1345 if (!usb_enabled(false)) {
1346 return -1;
1349 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1350 dev = usbdevice_create(devname);
1351 if (dev)
1352 goto done;
1354 /* the other ones */
1355 #ifndef CONFIG_LINUX
1356 /* only the linux version is qdev-ified, usb-bsd still needs this */
1357 if (strstart(devname, "host:", &p)) {
1358 dev = usb_host_device_open(usb_bus_find(-1), p);
1360 #endif
1361 if (!dev)
1362 return -1;
1364 done:
1365 return 0;
1368 static int usb_device_del(const char *devname)
1370 int bus_num, addr;
1371 const char *p;
1373 if (strstart(devname, "host:", &p)) {
1374 return -1;
1377 if (!usb_enabled(false)) {
1378 return -1;
1381 p = strchr(devname, '.');
1382 if (!p)
1383 return -1;
1384 bus_num = strtoul(devname, NULL, 0);
1385 addr = strtoul(p + 1, NULL, 0);
1387 return usb_device_delete_addr(bus_num, addr);
1390 static int usb_parse(const char *cmdline)
1392 int r;
1393 r = usb_device_add(cmdline);
1394 if (r < 0) {
1395 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1397 return r;
1400 void do_usb_add(Monitor *mon, const QDict *qdict)
1402 const char *devname = qdict_get_str(qdict, "devname");
1403 if (usb_device_add(devname) < 0) {
1404 error_report("could not add USB device '%s'", devname);
1408 void do_usb_del(Monitor *mon, const QDict *qdict)
1410 const char *devname = qdict_get_str(qdict, "devname");
1411 if (usb_device_del(devname) < 0) {
1412 error_report("could not delete USB device '%s'", devname);
1416 /***********************************************************/
1417 /* machine registration */
1419 MachineState *current_machine;
1421 static void machine_class_init(ObjectClass *oc, void *data)
1423 MachineClass *mc = MACHINE_CLASS(oc);
1424 QEMUMachine *qm = data;
1426 mc->family = qm->family;
1427 mc->name = qm->name;
1428 mc->alias = qm->alias;
1429 mc->desc = qm->desc;
1430 mc->init = qm->init;
1431 mc->reset = qm->reset;
1432 mc->hot_add_cpu = qm->hot_add_cpu;
1433 mc->kvm_type = qm->kvm_type;
1434 mc->block_default_type = qm->block_default_type;
1435 mc->units_per_default_bus = qm->units_per_default_bus;
1436 mc->max_cpus = qm->max_cpus;
1437 mc->no_serial = qm->no_serial;
1438 mc->no_parallel = qm->no_parallel;
1439 mc->use_virtcon = qm->use_virtcon;
1440 mc->use_sclp = qm->use_sclp;
1441 mc->no_floppy = qm->no_floppy;
1442 mc->no_cdrom = qm->no_cdrom;
1443 mc->no_sdcard = qm->no_sdcard;
1444 mc->is_default = qm->is_default;
1445 mc->default_machine_opts = qm->default_machine_opts;
1446 mc->default_boot_order = qm->default_boot_order;
1447 mc->compat_props = qm->compat_props;
1448 mc->hw_version = qm->hw_version;
1451 int qemu_register_machine(QEMUMachine *m)
1453 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1454 TypeInfo ti = {
1455 .name = name,
1456 .parent = TYPE_MACHINE,
1457 .class_init = machine_class_init,
1458 .class_data = (void *)m,
1461 type_register(&ti);
1462 g_free(name);
1464 return 0;
1467 static MachineClass *find_machine(const char *name)
1469 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1470 MachineClass *mc = NULL;
1472 for (el = machines; el; el = el->next) {
1473 MachineClass *temp = el->data;
1475 if (!strcmp(temp->name, name)) {
1476 mc = temp;
1477 break;
1479 if (temp->alias &&
1480 !strcmp(temp->alias, name)) {
1481 mc = temp;
1482 break;
1486 g_slist_free(machines);
1487 return mc;
1490 MachineClass *find_default_machine(void)
1492 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1493 MachineClass *mc = NULL;
1495 for (el = machines; el; el = el->next) {
1496 MachineClass *temp = el->data;
1498 if (temp->is_default) {
1499 mc = temp;
1500 break;
1504 g_slist_free(machines);
1505 return mc;
1508 MachineInfoList *qmp_query_machines(Error **errp)
1510 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1511 MachineInfoList *mach_list = NULL;
1513 for (el = machines; el; el = el->next) {
1514 MachineClass *mc = el->data;
1515 MachineInfoList *entry;
1516 MachineInfo *info;
1518 info = g_malloc0(sizeof(*info));
1519 if (mc->is_default) {
1520 info->has_is_default = true;
1521 info->is_default = true;
1524 if (mc->alias) {
1525 info->has_alias = true;
1526 info->alias = g_strdup(mc->alias);
1529 info->name = g_strdup(mc->name);
1530 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1532 entry = g_malloc0(sizeof(*entry));
1533 entry->value = info;
1534 entry->next = mach_list;
1535 mach_list = entry;
1538 g_slist_free(machines);
1539 return mach_list;
1542 /***********************************************************/
1543 /* main execution loop */
1545 struct vm_change_state_entry {
1546 VMChangeStateHandler *cb;
1547 void *opaque;
1548 QLIST_ENTRY (vm_change_state_entry) entries;
1551 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1553 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1554 void *opaque)
1556 VMChangeStateEntry *e;
1558 e = g_malloc0(sizeof (*e));
1560 e->cb = cb;
1561 e->opaque = opaque;
1562 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1563 return e;
1566 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1568 QLIST_REMOVE (e, entries);
1569 g_free (e);
1572 void vm_state_notify(int running, RunState state)
1574 VMChangeStateEntry *e, *next;
1576 trace_vm_state_notify(running, state);
1578 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1579 e->cb(e->opaque, running, state);
1583 /* reset/shutdown handler */
1585 typedef struct QEMUResetEntry {
1586 QTAILQ_ENTRY(QEMUResetEntry) entry;
1587 QEMUResetHandler *func;
1588 void *opaque;
1589 } QEMUResetEntry;
1591 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1592 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1593 static int reset_requested;
1594 static int shutdown_requested, shutdown_signal = -1;
1595 static pid_t shutdown_pid;
1596 static int powerdown_requested;
1597 static int debug_requested;
1598 static int suspend_requested;
1599 static WakeupReason wakeup_reason;
1600 static NotifierList powerdown_notifiers =
1601 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1602 static NotifierList suspend_notifiers =
1603 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1604 static NotifierList wakeup_notifiers =
1605 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1606 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1608 int qemu_shutdown_requested_get(void)
1610 return shutdown_requested;
1613 int qemu_reset_requested_get(void)
1615 return reset_requested;
1618 static int qemu_shutdown_requested(void)
1620 return atomic_xchg(&shutdown_requested, 0);
1623 static void qemu_kill_report(void)
1625 if (!qtest_driver() && shutdown_signal != -1) {
1626 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1627 if (shutdown_pid == 0) {
1628 /* This happens for eg ^C at the terminal, so it's worth
1629 * avoiding printing an odd message in that case.
1631 fputc('\n', stderr);
1632 } else {
1633 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1635 shutdown_signal = -1;
1639 static int qemu_reset_requested(void)
1641 int r = reset_requested;
1642 reset_requested = 0;
1643 return r;
1646 static int qemu_suspend_requested(void)
1648 int r = suspend_requested;
1649 suspend_requested = 0;
1650 return r;
1653 static WakeupReason qemu_wakeup_requested(void)
1655 return wakeup_reason;
1658 static int qemu_powerdown_requested(void)
1660 int r = powerdown_requested;
1661 powerdown_requested = 0;
1662 return r;
1665 static int qemu_debug_requested(void)
1667 int r = debug_requested;
1668 debug_requested = 0;
1669 return r;
1672 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1674 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1676 re->func = func;
1677 re->opaque = opaque;
1678 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1681 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1683 QEMUResetEntry *re;
1685 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1686 if (re->func == func && re->opaque == opaque) {
1687 QTAILQ_REMOVE(&reset_handlers, re, entry);
1688 g_free(re);
1689 return;
1694 void qemu_devices_reset(void)
1696 QEMUResetEntry *re, *nre;
1698 /* reset all devices */
1699 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1700 re->func(re->opaque);
1704 void qemu_system_reset(bool report)
1706 MachineClass *mc;
1708 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1710 if (mc && mc->reset) {
1711 mc->reset();
1712 } else {
1713 qemu_devices_reset();
1715 if (report) {
1716 qapi_event_send_reset(&error_abort);
1718 cpu_synchronize_all_post_reset();
1721 void qemu_system_reset_request(void)
1723 if (no_reboot) {
1724 shutdown_requested = 1;
1725 } else {
1726 reset_requested = 1;
1728 cpu_stop_current();
1729 qemu_notify_event();
1732 static void qemu_system_suspend(void)
1734 pause_all_vcpus();
1735 notifier_list_notify(&suspend_notifiers, NULL);
1736 runstate_set(RUN_STATE_SUSPENDED);
1737 qapi_event_send_suspend(&error_abort);
1740 void qemu_system_suspend_request(void)
1742 if (runstate_check(RUN_STATE_SUSPENDED)) {
1743 return;
1745 suspend_requested = 1;
1746 cpu_stop_current();
1747 qemu_notify_event();
1750 void qemu_register_suspend_notifier(Notifier *notifier)
1752 notifier_list_add(&suspend_notifiers, notifier);
1755 void qemu_system_wakeup_request(WakeupReason reason)
1757 trace_system_wakeup_request(reason);
1759 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1760 return;
1762 if (!(wakeup_reason_mask & (1 << reason))) {
1763 return;
1765 runstate_set(RUN_STATE_RUNNING);
1766 wakeup_reason = reason;
1767 qemu_notify_event();
1770 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1772 if (enabled) {
1773 wakeup_reason_mask |= (1 << reason);
1774 } else {
1775 wakeup_reason_mask &= ~(1 << reason);
1779 void qemu_register_wakeup_notifier(Notifier *notifier)
1781 notifier_list_add(&wakeup_notifiers, notifier);
1784 void qemu_system_killed(int signal, pid_t pid)
1786 shutdown_signal = signal;
1787 shutdown_pid = pid;
1788 no_shutdown = 0;
1789 qemu_system_shutdown_request();
1792 void qemu_system_shutdown_request(void)
1794 trace_qemu_system_shutdown_request();
1795 shutdown_requested = 1;
1796 qemu_notify_event();
1799 static void qemu_system_powerdown(void)
1801 qapi_event_send_powerdown(&error_abort);
1802 notifier_list_notify(&powerdown_notifiers, NULL);
1805 void qemu_system_powerdown_request(void)
1807 trace_qemu_system_powerdown_request();
1808 powerdown_requested = 1;
1809 qemu_notify_event();
1812 void qemu_register_powerdown_notifier(Notifier *notifier)
1814 notifier_list_add(&powerdown_notifiers, notifier);
1817 void qemu_system_debug_request(void)
1819 debug_requested = 1;
1820 qemu_notify_event();
1823 static bool main_loop_should_exit(void)
1825 RunState r;
1826 if (qemu_debug_requested()) {
1827 vm_stop(RUN_STATE_DEBUG);
1829 if (qemu_suspend_requested()) {
1830 qemu_system_suspend();
1832 if (qemu_shutdown_requested()) {
1833 qemu_kill_report();
1834 qapi_event_send_shutdown(&error_abort);
1835 if (no_shutdown) {
1836 vm_stop(RUN_STATE_SHUTDOWN);
1837 } else {
1838 return true;
1841 if (qemu_reset_requested()) {
1842 pause_all_vcpus();
1843 cpu_synchronize_all_states();
1844 qemu_system_reset(VMRESET_REPORT);
1845 resume_all_vcpus();
1846 if (runstate_needs_reset()) {
1847 runstate_set(RUN_STATE_PAUSED);
1850 if (qemu_wakeup_requested()) {
1851 pause_all_vcpus();
1852 cpu_synchronize_all_states();
1853 qemu_system_reset(VMRESET_SILENT);
1854 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1855 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1856 resume_all_vcpus();
1857 qapi_event_send_wakeup(&error_abort);
1859 if (qemu_powerdown_requested()) {
1860 qemu_system_powerdown();
1862 if (qemu_vmstop_requested(&r)) {
1863 vm_stop(r);
1865 return false;
1868 static void main_loop(void)
1870 bool nonblocking;
1871 int last_io = 0;
1872 #ifdef CONFIG_PROFILER
1873 int64_t ti;
1874 #endif
1875 do {
1876 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1877 #ifdef CONFIG_PROFILER
1878 ti = profile_getclock();
1879 #endif
1880 last_io = main_loop_wait(nonblocking);
1881 #ifdef CONFIG_PROFILER
1882 dev_time += profile_getclock() - ti;
1883 #endif
1884 } while (!main_loop_should_exit());
1887 static void version(void)
1889 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1892 static void help(int exitcode)
1894 version();
1895 printf("usage: %s [options] [disk_image]\n\n"
1896 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1897 error_get_progname());
1899 #define QEMU_OPTIONS_GENERATE_HELP
1900 #include "qemu-options-wrapper.h"
1902 printf("\nDuring emulation, the following keys are useful:\n"
1903 "ctrl-alt-f toggle full screen\n"
1904 "ctrl-alt-n switch to virtual console 'n'\n"
1905 "ctrl-alt toggle mouse and keyboard grab\n"
1906 "\n"
1907 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1909 exit(exitcode);
1912 #define HAS_ARG 0x0001
1914 typedef struct QEMUOption {
1915 const char *name;
1916 int flags;
1917 int index;
1918 uint32_t arch_mask;
1919 } QEMUOption;
1921 static const QEMUOption qemu_options[] = {
1922 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1923 #define QEMU_OPTIONS_GENERATE_OPTIONS
1924 #include "qemu-options-wrapper.h"
1925 { NULL },
1928 static bool vga_available(void)
1930 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1933 static bool cirrus_vga_available(void)
1935 return object_class_by_name("cirrus-vga")
1936 || object_class_by_name("isa-cirrus-vga");
1939 static bool vmware_vga_available(void)
1941 return object_class_by_name("vmware-svga");
1944 static bool qxl_vga_available(void)
1946 return object_class_by_name("qxl-vga");
1949 static bool tcx_vga_available(void)
1951 return object_class_by_name("SUNW,tcx");
1954 static bool cg3_vga_available(void)
1956 return object_class_by_name("cgthree");
1959 static void select_vgahw (const char *p)
1961 const char *opts;
1963 assert(vga_interface_type == VGA_NONE);
1964 if (strstart(p, "std", &opts)) {
1965 if (vga_available()) {
1966 vga_interface_type = VGA_STD;
1967 } else {
1968 fprintf(stderr, "Error: standard VGA not available\n");
1969 exit(0);
1971 } else if (strstart(p, "cirrus", &opts)) {
1972 if (cirrus_vga_available()) {
1973 vga_interface_type = VGA_CIRRUS;
1974 } else {
1975 fprintf(stderr, "Error: Cirrus VGA not available\n");
1976 exit(0);
1978 } else if (strstart(p, "vmware", &opts)) {
1979 if (vmware_vga_available()) {
1980 vga_interface_type = VGA_VMWARE;
1981 } else {
1982 fprintf(stderr, "Error: VMWare SVGA not available\n");
1983 exit(0);
1985 } else if (strstart(p, "xenfb", &opts)) {
1986 vga_interface_type = VGA_XENFB;
1987 } else if (strstart(p, "qxl", &opts)) {
1988 if (qxl_vga_available()) {
1989 vga_interface_type = VGA_QXL;
1990 } else {
1991 fprintf(stderr, "Error: QXL VGA not available\n");
1992 exit(0);
1994 } else if (strstart(p, "tcx", &opts)) {
1995 if (tcx_vga_available()) {
1996 vga_interface_type = VGA_TCX;
1997 } else {
1998 fprintf(stderr, "Error: TCX framebuffer not available\n");
1999 exit(0);
2001 } else if (strstart(p, "cg3", &opts)) {
2002 if (cg3_vga_available()) {
2003 vga_interface_type = VGA_CG3;
2004 } else {
2005 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2006 exit(0);
2008 } else if (!strstart(p, "none", &opts)) {
2009 invalid_vga:
2010 fprintf(stderr, "Unknown vga type: %s\n", p);
2011 exit(1);
2013 while (*opts) {
2014 const char *nextopt;
2016 if (strstart(opts, ",retrace=", &nextopt)) {
2017 opts = nextopt;
2018 if (strstart(opts, "dumb", &nextopt))
2019 vga_retrace_method = VGA_RETRACE_DUMB;
2020 else if (strstart(opts, "precise", &nextopt))
2021 vga_retrace_method = VGA_RETRACE_PRECISE;
2022 else goto invalid_vga;
2023 } else goto invalid_vga;
2024 opts = nextopt;
2028 static DisplayType select_display(const char *p)
2030 const char *opts;
2031 DisplayType display = DT_DEFAULT;
2033 if (strstart(p, "sdl", &opts)) {
2034 #ifdef CONFIG_SDL
2035 display = DT_SDL;
2036 while (*opts) {
2037 const char *nextopt;
2039 if (strstart(opts, ",frame=", &nextopt)) {
2040 opts = nextopt;
2041 if (strstart(opts, "on", &nextopt)) {
2042 no_frame = 0;
2043 } else if (strstart(opts, "off", &nextopt)) {
2044 no_frame = 1;
2045 } else {
2046 goto invalid_sdl_args;
2048 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2049 opts = nextopt;
2050 if (strstart(opts, "on", &nextopt)) {
2051 alt_grab = 1;
2052 } else if (strstart(opts, "off", &nextopt)) {
2053 alt_grab = 0;
2054 } else {
2055 goto invalid_sdl_args;
2057 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2058 opts = nextopt;
2059 if (strstart(opts, "on", &nextopt)) {
2060 ctrl_grab = 1;
2061 } else if (strstart(opts, "off", &nextopt)) {
2062 ctrl_grab = 0;
2063 } else {
2064 goto invalid_sdl_args;
2066 } else if (strstart(opts, ",window_close=", &nextopt)) {
2067 opts = nextopt;
2068 if (strstart(opts, "on", &nextopt)) {
2069 no_quit = 0;
2070 } else if (strstart(opts, "off", &nextopt)) {
2071 no_quit = 1;
2072 } else {
2073 goto invalid_sdl_args;
2075 } else {
2076 invalid_sdl_args:
2077 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2078 exit(1);
2080 opts = nextopt;
2082 #else
2083 fprintf(stderr, "SDL support is disabled\n");
2084 exit(1);
2085 #endif
2086 } else if (strstart(p, "vnc", &opts)) {
2087 #ifdef CONFIG_VNC
2088 display_remote++;
2090 if (*opts) {
2091 const char *nextopt;
2093 if (strstart(opts, "=", &nextopt)) {
2094 vnc_display = nextopt;
2097 if (!vnc_display) {
2098 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2099 exit(1);
2101 #else
2102 fprintf(stderr, "VNC support is disabled\n");
2103 exit(1);
2104 #endif
2105 } else if (strstart(p, "curses", &opts)) {
2106 #ifdef CONFIG_CURSES
2107 display = DT_CURSES;
2108 #else
2109 fprintf(stderr, "Curses support is disabled\n");
2110 exit(1);
2111 #endif
2112 } else if (strstart(p, "gtk", &opts)) {
2113 #ifdef CONFIG_GTK
2114 display = DT_GTK;
2115 while (*opts) {
2116 const char *nextopt;
2118 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2119 opts = nextopt;
2120 if (strstart(opts, "on", &nextopt)) {
2121 grab_on_hover = true;
2122 } else if (strstart(opts, "off", &nextopt)) {
2123 grab_on_hover = false;
2124 } else {
2125 goto invalid_gtk_args;
2127 } else {
2128 invalid_gtk_args:
2129 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2130 exit(1);
2132 opts = nextopt;
2134 #else
2135 fprintf(stderr, "GTK support is disabled\n");
2136 exit(1);
2137 #endif
2138 } else if (strstart(p, "none", &opts)) {
2139 display = DT_NONE;
2140 } else {
2141 fprintf(stderr, "Unknown display type: %s\n", p);
2142 exit(1);
2145 return display;
2148 static int balloon_parse(const char *arg)
2150 QemuOpts *opts;
2152 if (strcmp(arg, "none") == 0) {
2153 return 0;
2156 if (!strncmp(arg, "virtio", 6)) {
2157 if (arg[6] == ',') {
2158 /* have params -> parse them */
2159 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2160 if (!opts)
2161 return -1;
2162 } else {
2163 /* create empty opts */
2164 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2165 &error_abort);
2167 qemu_opt_set(opts, "driver", "virtio-balloon");
2168 return 0;
2171 return -1;
2174 char *qemu_find_file(int type, const char *name)
2176 int i;
2177 const char *subdir;
2178 char *buf;
2180 /* Try the name as a straight path first */
2181 if (access(name, R_OK) == 0) {
2182 trace_load_file(name, name);
2183 return g_strdup(name);
2186 switch (type) {
2187 case QEMU_FILE_TYPE_BIOS:
2188 subdir = "";
2189 break;
2190 case QEMU_FILE_TYPE_KEYMAP:
2191 subdir = "keymaps/";
2192 break;
2193 default:
2194 abort();
2197 for (i = 0; i < data_dir_idx; i++) {
2198 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2199 if (access(buf, R_OK) == 0) {
2200 trace_load_file(name, buf);
2201 return buf;
2203 g_free(buf);
2205 return NULL;
2208 static int device_help_func(QemuOpts *opts, void *opaque)
2210 return qdev_device_help(opts);
2213 static int device_init_func(QemuOpts *opts, void *opaque)
2215 DeviceState *dev;
2217 dev = qdev_device_add(opts);
2218 if (!dev)
2219 return -1;
2220 object_unref(OBJECT(dev));
2221 return 0;
2224 static int chardev_init_func(QemuOpts *opts, void *opaque)
2226 Error *local_err = NULL;
2228 qemu_chr_new_from_opts(opts, NULL, &local_err);
2229 if (local_err) {
2230 error_report("%s", error_get_pretty(local_err));
2231 error_free(local_err);
2232 return -1;
2234 return 0;
2237 #ifdef CONFIG_VIRTFS
2238 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2240 int ret;
2241 ret = qemu_fsdev_add(opts);
2243 return ret;
2245 #endif
2247 static int mon_init_func(QemuOpts *opts, void *opaque)
2249 CharDriverState *chr;
2250 const char *chardev;
2251 const char *mode;
2252 int flags;
2254 mode = qemu_opt_get(opts, "mode");
2255 if (mode == NULL) {
2256 mode = "readline";
2258 if (strcmp(mode, "readline") == 0) {
2259 flags = MONITOR_USE_READLINE;
2260 } else if (strcmp(mode, "control") == 0) {
2261 flags = MONITOR_USE_CONTROL;
2262 } else {
2263 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2264 exit(1);
2267 if (qemu_opt_get_bool(opts, "pretty", 0))
2268 flags |= MONITOR_USE_PRETTY;
2270 if (qemu_opt_get_bool(opts, "default", 0))
2271 flags |= MONITOR_IS_DEFAULT;
2273 chardev = qemu_opt_get(opts, "chardev");
2274 chr = qemu_chr_find(chardev);
2275 if (chr == NULL) {
2276 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2277 exit(1);
2280 qemu_chr_fe_claim_no_fail(chr);
2281 monitor_init(chr, flags);
2282 return 0;
2285 static void monitor_parse(const char *optarg, const char *mode)
2287 static int monitor_device_index = 0;
2288 QemuOpts *opts;
2289 const char *p;
2290 char label[32];
2291 int def = 0;
2293 if (strstart(optarg, "chardev:", &p)) {
2294 snprintf(label, sizeof(label), "%s", p);
2295 } else {
2296 snprintf(label, sizeof(label), "compat_monitor%d",
2297 monitor_device_index);
2298 if (monitor_device_index == 0) {
2299 def = 1;
2301 opts = qemu_chr_parse_compat(label, optarg);
2302 if (!opts) {
2303 fprintf(stderr, "parse error: %s\n", optarg);
2304 exit(1);
2308 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2309 if (!opts) {
2310 fprintf(stderr, "duplicate chardev: %s\n", label);
2311 exit(1);
2313 qemu_opt_set(opts, "mode", mode);
2314 qemu_opt_set(opts, "chardev", label);
2315 if (def)
2316 qemu_opt_set(opts, "default", "on");
2317 monitor_device_index++;
2320 struct device_config {
2321 enum {
2322 DEV_USB, /* -usbdevice */
2323 DEV_BT, /* -bt */
2324 DEV_SERIAL, /* -serial */
2325 DEV_PARALLEL, /* -parallel */
2326 DEV_VIRTCON, /* -virtioconsole */
2327 DEV_DEBUGCON, /* -debugcon */
2328 DEV_GDB, /* -gdb, -s */
2329 DEV_SCLP, /* s390 sclp */
2330 } type;
2331 const char *cmdline;
2332 Location loc;
2333 QTAILQ_ENTRY(device_config) next;
2336 static QTAILQ_HEAD(, device_config) device_configs =
2337 QTAILQ_HEAD_INITIALIZER(device_configs);
2339 static void add_device_config(int type, const char *cmdline)
2341 struct device_config *conf;
2343 conf = g_malloc0(sizeof(*conf));
2344 conf->type = type;
2345 conf->cmdline = cmdline;
2346 loc_save(&conf->loc);
2347 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2350 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2352 struct device_config *conf;
2353 int rc;
2355 QTAILQ_FOREACH(conf, &device_configs, next) {
2356 if (conf->type != type)
2357 continue;
2358 loc_push_restore(&conf->loc);
2359 rc = func(conf->cmdline);
2360 loc_pop(&conf->loc);
2361 if (rc) {
2362 return rc;
2365 return 0;
2368 static int serial_parse(const char *devname)
2370 static int index = 0;
2371 char label[32];
2373 if (strcmp(devname, "none") == 0)
2374 return 0;
2375 if (index == MAX_SERIAL_PORTS) {
2376 fprintf(stderr, "qemu: too many serial ports\n");
2377 exit(1);
2379 snprintf(label, sizeof(label), "serial%d", index);
2380 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2381 if (!serial_hds[index]) {
2382 fprintf(stderr, "qemu: could not connect serial device"
2383 " to character backend '%s'\n", devname);
2384 return -1;
2386 index++;
2387 return 0;
2390 static int parallel_parse(const char *devname)
2392 static int index = 0;
2393 char label[32];
2395 if (strcmp(devname, "none") == 0)
2396 return 0;
2397 if (index == MAX_PARALLEL_PORTS) {
2398 fprintf(stderr, "qemu: too many parallel ports\n");
2399 exit(1);
2401 snprintf(label, sizeof(label), "parallel%d", index);
2402 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2403 if (!parallel_hds[index]) {
2404 fprintf(stderr, "qemu: could not connect parallel device"
2405 " to character backend '%s'\n", devname);
2406 return -1;
2408 index++;
2409 return 0;
2412 static int virtcon_parse(const char *devname)
2414 QemuOptsList *device = qemu_find_opts("device");
2415 static int index = 0;
2416 char label[32];
2417 QemuOpts *bus_opts, *dev_opts;
2419 if (strcmp(devname, "none") == 0)
2420 return 0;
2421 if (index == MAX_VIRTIO_CONSOLES) {
2422 fprintf(stderr, "qemu: too many virtio consoles\n");
2423 exit(1);
2426 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2427 if (arch_type == QEMU_ARCH_S390X) {
2428 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2429 } else {
2430 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2433 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2434 qemu_opt_set(dev_opts, "driver", "virtconsole");
2436 snprintf(label, sizeof(label), "virtcon%d", index);
2437 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2438 if (!virtcon_hds[index]) {
2439 fprintf(stderr, "qemu: could not connect virtio console"
2440 " to character backend '%s'\n", devname);
2441 return -1;
2443 qemu_opt_set(dev_opts, "chardev", label);
2445 index++;
2446 return 0;
2449 static int sclp_parse(const char *devname)
2451 QemuOptsList *device = qemu_find_opts("device");
2452 static int index = 0;
2453 char label[32];
2454 QemuOpts *dev_opts;
2456 if (strcmp(devname, "none") == 0) {
2457 return 0;
2459 if (index == MAX_SCLP_CONSOLES) {
2460 fprintf(stderr, "qemu: too many sclp consoles\n");
2461 exit(1);
2464 assert(arch_type == QEMU_ARCH_S390X);
2466 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2467 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2469 snprintf(label, sizeof(label), "sclpcon%d", index);
2470 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2471 if (!sclp_hds[index]) {
2472 fprintf(stderr, "qemu: could not connect sclp console"
2473 " to character backend '%s'\n", devname);
2474 return -1;
2476 qemu_opt_set(dev_opts, "chardev", label);
2478 index++;
2479 return 0;
2482 static int debugcon_parse(const char *devname)
2484 QemuOpts *opts;
2486 if (!qemu_chr_new("debugcon", devname, NULL)) {
2487 exit(1);
2489 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2490 if (!opts) {
2491 fprintf(stderr, "qemu: already have a debugcon device\n");
2492 exit(1);
2494 qemu_opt_set(opts, "driver", "isa-debugcon");
2495 qemu_opt_set(opts, "chardev", "debugcon");
2496 return 0;
2499 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2501 const MachineClass *mc1 = a, *mc2 = b;
2502 int res;
2504 if (mc1->family == NULL) {
2505 if (mc2->family == NULL) {
2506 /* Compare standalone machine types against each other; they sort
2507 * in increasing order.
2509 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2510 object_class_get_name(OBJECT_CLASS(mc2)));
2513 /* Standalone machine types sort after families. */
2514 return 1;
2517 if (mc2->family == NULL) {
2518 /* Families sort before standalone machine types. */
2519 return -1;
2522 /* Families sort between each other alphabetically increasingly. */
2523 res = strcmp(mc1->family, mc2->family);
2524 if (res != 0) {
2525 return res;
2528 /* Within the same family, machine types sort in decreasing order. */
2529 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2530 object_class_get_name(OBJECT_CLASS(mc1)));
2533 static MachineClass *machine_parse(const char *name)
2535 MachineClass *mc = NULL;
2536 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2538 if (name) {
2539 mc = find_machine(name);
2541 if (mc) {
2542 return mc;
2544 if (name && !is_help_option(name)) {
2545 error_report("Unsupported machine type");
2546 error_printf("Use -machine help to list supported machines!\n");
2547 } else {
2548 printf("Supported machines are:\n");
2549 machines = g_slist_sort(machines, machine_class_cmp);
2550 for (el = machines; el; el = el->next) {
2551 MachineClass *mc = el->data;
2552 if (mc->alias) {
2553 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2555 printf("%-20s %s%s\n", mc->name, mc->desc,
2556 mc->is_default ? " (default)" : "");
2560 g_slist_free(machines);
2561 exit(!name || !is_help_option(name));
2564 void qemu_add_exit_notifier(Notifier *notify)
2566 notifier_list_add(&exit_notifiers, notify);
2569 void qemu_remove_exit_notifier(Notifier *notify)
2571 notifier_remove(notify);
2574 static void qemu_run_exit_notifiers(void)
2576 notifier_list_notify(&exit_notifiers, NULL);
2579 void qemu_add_machine_init_done_notifier(Notifier *notify)
2581 notifier_list_add(&machine_init_done_notifiers, notify);
2584 static void qemu_run_machine_init_done_notifiers(void)
2586 notifier_list_notify(&machine_init_done_notifiers, NULL);
2589 static const QEMUOption *lookup_opt(int argc, char **argv,
2590 const char **poptarg, int *poptind)
2592 const QEMUOption *popt;
2593 int optind = *poptind;
2594 char *r = argv[optind];
2595 const char *optarg;
2597 loc_set_cmdline(argv, optind, 1);
2598 optind++;
2599 /* Treat --foo the same as -foo. */
2600 if (r[1] == '-')
2601 r++;
2602 popt = qemu_options;
2603 for(;;) {
2604 if (!popt->name) {
2605 error_report("invalid option");
2606 exit(1);
2608 if (!strcmp(popt->name, r + 1))
2609 break;
2610 popt++;
2612 if (popt->flags & HAS_ARG) {
2613 if (optind >= argc) {
2614 error_report("requires an argument");
2615 exit(1);
2617 optarg = argv[optind++];
2618 loc_set_cmdline(argv, optind - 2, 2);
2619 } else {
2620 optarg = NULL;
2623 *poptarg = optarg;
2624 *poptind = optind;
2626 return popt;
2629 static gpointer malloc_and_trace(gsize n_bytes)
2631 void *ptr = malloc(n_bytes);
2632 trace_g_malloc(n_bytes, ptr);
2633 return ptr;
2636 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2638 void *ptr = realloc(mem, n_bytes);
2639 trace_g_realloc(mem, n_bytes, ptr);
2640 return ptr;
2643 static void free_and_trace(gpointer mem)
2645 trace_g_free(mem);
2646 free(mem);
2649 static int machine_set_property(const char *name, const char *value,
2650 void *opaque)
2652 Object *obj = OBJECT(opaque);
2653 StringInputVisitor *siv;
2654 Error *local_err = NULL;
2655 char *c, *qom_name;
2657 if (strcmp(name, "type") == 0) {
2658 return 0;
2661 qom_name = g_strdup(name);
2662 c = qom_name;
2663 while (*c++) {
2664 if (*c == '_') {
2665 *c = '-';
2669 siv = string_input_visitor_new(value);
2670 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2671 string_input_visitor_cleanup(siv);
2672 g_free(qom_name);
2674 if (local_err) {
2675 qerror_report_err(local_err);
2676 error_free(local_err);
2677 return -1;
2680 return 0;
2683 static int object_create(QemuOpts *opts, void *opaque)
2685 Error *err = NULL;
2686 char *type = NULL;
2687 char *id = NULL;
2688 void *dummy = NULL;
2689 OptsVisitor *ov;
2690 QDict *pdict;
2692 ov = opts_visitor_new(opts);
2693 pdict = qemu_opts_to_qdict(opts, NULL);
2695 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2696 if (err) {
2697 goto out;
2700 qdict_del(pdict, "qom-type");
2701 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2702 if (err) {
2703 goto out;
2706 qdict_del(pdict, "id");
2707 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2708 if (err) {
2709 goto out;
2712 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2713 if (err) {
2714 goto out;
2716 visit_end_struct(opts_get_visitor(ov), &err);
2717 if (err) {
2718 qmp_object_del(id, NULL);
2721 out:
2722 opts_visitor_cleanup(ov);
2724 QDECREF(pdict);
2725 g_free(id);
2726 g_free(type);
2727 g_free(dummy);
2728 if (err) {
2729 qerror_report_err(err);
2730 error_free(err);
2731 return -1;
2733 return 0;
2736 int main(int argc, char **argv, char **envp)
2738 int i;
2739 int snapshot, linux_boot;
2740 const char *initrd_filename;
2741 const char *kernel_filename, *kernel_cmdline;
2742 const char *boot_order;
2743 DisplayState *ds;
2744 int cyls, heads, secs, translation;
2745 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2746 QemuOptsList *olist;
2747 int optind;
2748 const char *optarg;
2749 const char *loadvm = NULL;
2750 MachineClass *machine_class;
2751 const char *cpu_model;
2752 const char *vga_model = NULL;
2753 const char *qtest_chrdev = NULL;
2754 const char *qtest_log = NULL;
2755 const char *pid_file = NULL;
2756 const char *incoming = NULL;
2757 #ifdef CONFIG_VNC
2758 int show_vnc_port = 0;
2759 #endif
2760 bool defconfig = true;
2761 bool userconfig = true;
2762 const char *log_mask = NULL;
2763 const char *log_file = NULL;
2764 GMemVTable mem_trace = {
2765 .malloc = malloc_and_trace,
2766 .realloc = realloc_and_trace,
2767 .free = free_and_trace,
2769 const char *trace_events = NULL;
2770 const char *trace_file = NULL;
2771 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2772 1024 * 1024;
2773 ram_addr_t maxram_size = default_ram_size;
2774 uint64_t ram_slots = 0;
2775 FILE *vmstate_dump_file = NULL;
2776 Error *main_loop_err = NULL;
2778 atexit(qemu_run_exit_notifiers);
2779 error_set_progname(argv[0]);
2780 qemu_init_exec_dir(argv[0]);
2782 g_mem_set_vtable(&mem_trace);
2784 module_call_init(MODULE_INIT_QOM);
2786 qemu_add_opts(&qemu_drive_opts);
2787 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2788 qemu_add_drive_opts(&qemu_common_drive_opts);
2789 qemu_add_drive_opts(&qemu_drive_opts);
2790 qemu_add_opts(&qemu_chardev_opts);
2791 qemu_add_opts(&qemu_device_opts);
2792 qemu_add_opts(&qemu_netdev_opts);
2793 qemu_add_opts(&qemu_net_opts);
2794 qemu_add_opts(&qemu_rtc_opts);
2795 qemu_add_opts(&qemu_global_opts);
2796 qemu_add_opts(&qemu_mon_opts);
2797 qemu_add_opts(&qemu_trace_opts);
2798 qemu_add_opts(&qemu_option_rom_opts);
2799 qemu_add_opts(&qemu_machine_opts);
2800 qemu_add_opts(&qemu_mem_opts);
2801 qemu_add_opts(&qemu_smp_opts);
2802 qemu_add_opts(&qemu_boot_opts);
2803 qemu_add_opts(&qemu_sandbox_opts);
2804 qemu_add_opts(&qemu_add_fd_opts);
2805 qemu_add_opts(&qemu_object_opts);
2806 qemu_add_opts(&qemu_tpmdev_opts);
2807 qemu_add_opts(&qemu_realtime_opts);
2808 qemu_add_opts(&qemu_msg_opts);
2809 qemu_add_opts(&qemu_name_opts);
2810 qemu_add_opts(&qemu_numa_opts);
2811 qemu_add_opts(&qemu_icount_opts);
2813 runstate_init();
2815 rtc_clock = QEMU_CLOCK_HOST;
2817 QLIST_INIT (&vm_change_state_head);
2818 os_setup_early_signal_handling();
2820 module_call_init(MODULE_INIT_MACHINE);
2821 machine_class = find_default_machine();
2822 cpu_model = NULL;
2823 ram_size = default_ram_size;
2824 snapshot = 0;
2825 cyls = heads = secs = 0;
2826 translation = BIOS_ATA_TRANSLATION_AUTO;
2828 for (i = 0; i < MAX_NODES; i++) {
2829 numa_info[i].node_mem = 0;
2830 numa_info[i].present = false;
2831 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2834 nb_numa_nodes = 0;
2835 max_numa_nodeid = 0;
2836 nb_nics = 0;
2838 bdrv_init_with_whitelist();
2840 autostart = 1;
2842 /* first pass of option parsing */
2843 optind = 1;
2844 while (optind < argc) {
2845 if (argv[optind][0] != '-') {
2846 /* disk image */
2847 optind++;
2848 } else {
2849 const QEMUOption *popt;
2851 popt = lookup_opt(argc, argv, &optarg, &optind);
2852 switch (popt->index) {
2853 case QEMU_OPTION_nodefconfig:
2854 defconfig = false;
2855 break;
2856 case QEMU_OPTION_nouserconfig:
2857 userconfig = false;
2858 break;
2863 if (defconfig) {
2864 int ret;
2865 ret = qemu_read_default_config_files(userconfig);
2866 if (ret < 0) {
2867 exit(1);
2871 /* second pass of option parsing */
2872 optind = 1;
2873 for(;;) {
2874 if (optind >= argc)
2875 break;
2876 if (argv[optind][0] != '-') {
2877 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2878 } else {
2879 const QEMUOption *popt;
2881 popt = lookup_opt(argc, argv, &optarg, &optind);
2882 if (!(popt->arch_mask & arch_type)) {
2883 printf("Option %s not supported for this target\n", popt->name);
2884 exit(1);
2886 switch(popt->index) {
2887 case QEMU_OPTION_M:
2888 machine_class = machine_parse(optarg);
2889 break;
2890 case QEMU_OPTION_no_kvm_irqchip: {
2891 olist = qemu_find_opts("machine");
2892 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2893 break;
2895 case QEMU_OPTION_cpu:
2896 /* hw initialization will check this */
2897 cpu_model = optarg;
2898 break;
2899 case QEMU_OPTION_hda:
2901 char buf[256];
2902 if (cyls == 0)
2903 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2904 else
2905 snprintf(buf, sizeof(buf),
2906 "%s,cyls=%d,heads=%d,secs=%d%s",
2907 HD_OPTS , cyls, heads, secs,
2908 translation == BIOS_ATA_TRANSLATION_LBA ?
2909 ",trans=lba" :
2910 translation == BIOS_ATA_TRANSLATION_NONE ?
2911 ",trans=none" : "");
2912 drive_add(IF_DEFAULT, 0, optarg, buf);
2913 break;
2915 case QEMU_OPTION_hdb:
2916 case QEMU_OPTION_hdc:
2917 case QEMU_OPTION_hdd:
2918 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2919 HD_OPTS);
2920 break;
2921 case QEMU_OPTION_drive:
2922 if (drive_def(optarg) == NULL) {
2923 exit(1);
2925 break;
2926 case QEMU_OPTION_set:
2927 if (qemu_set_option(optarg) != 0)
2928 exit(1);
2929 break;
2930 case QEMU_OPTION_global:
2931 if (qemu_global_option(optarg) != 0)
2932 exit(1);
2933 break;
2934 case QEMU_OPTION_mtdblock:
2935 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2936 break;
2937 case QEMU_OPTION_sd:
2938 drive_add(IF_SD, -1, optarg, SD_OPTS);
2939 break;
2940 case QEMU_OPTION_pflash:
2941 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2942 break;
2943 case QEMU_OPTION_snapshot:
2944 snapshot = 1;
2945 break;
2946 case QEMU_OPTION_hdachs:
2948 const char *p;
2949 p = optarg;
2950 cyls = strtol(p, (char **)&p, 0);
2951 if (cyls < 1 || cyls > 16383)
2952 goto chs_fail;
2953 if (*p != ',')
2954 goto chs_fail;
2955 p++;
2956 heads = strtol(p, (char **)&p, 0);
2957 if (heads < 1 || heads > 16)
2958 goto chs_fail;
2959 if (*p != ',')
2960 goto chs_fail;
2961 p++;
2962 secs = strtol(p, (char **)&p, 0);
2963 if (secs < 1 || secs > 63)
2964 goto chs_fail;
2965 if (*p == ',') {
2966 p++;
2967 if (!strcmp(p, "large")) {
2968 translation = BIOS_ATA_TRANSLATION_LARGE;
2969 } else if (!strcmp(p, "rechs")) {
2970 translation = BIOS_ATA_TRANSLATION_RECHS;
2971 } else if (!strcmp(p, "none")) {
2972 translation = BIOS_ATA_TRANSLATION_NONE;
2973 } else if (!strcmp(p, "lba")) {
2974 translation = BIOS_ATA_TRANSLATION_LBA;
2975 } else if (!strcmp(p, "auto")) {
2976 translation = BIOS_ATA_TRANSLATION_AUTO;
2977 } else {
2978 goto chs_fail;
2980 } else if (*p != '\0') {
2981 chs_fail:
2982 fprintf(stderr, "qemu: invalid physical CHS format\n");
2983 exit(1);
2985 if (hda_opts != NULL) {
2986 char num[16];
2987 snprintf(num, sizeof(num), "%d", cyls);
2988 qemu_opt_set(hda_opts, "cyls", num);
2989 snprintf(num, sizeof(num), "%d", heads);
2990 qemu_opt_set(hda_opts, "heads", num);
2991 snprintf(num, sizeof(num), "%d", secs);
2992 qemu_opt_set(hda_opts, "secs", num);
2993 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2994 qemu_opt_set(hda_opts, "trans", "large");
2995 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2996 qemu_opt_set(hda_opts, "trans", "rechs");
2997 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
2998 qemu_opt_set(hda_opts, "trans", "lba");
2999 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3000 qemu_opt_set(hda_opts, "trans", "none");
3004 break;
3005 case QEMU_OPTION_numa:
3006 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3007 if (!opts) {
3008 exit(1);
3010 break;
3011 case QEMU_OPTION_display:
3012 display_type = select_display(optarg);
3013 break;
3014 case QEMU_OPTION_nographic:
3015 display_type = DT_NOGRAPHIC;
3016 break;
3017 case QEMU_OPTION_curses:
3018 #ifdef CONFIG_CURSES
3019 display_type = DT_CURSES;
3020 #else
3021 fprintf(stderr, "Curses support is disabled\n");
3022 exit(1);
3023 #endif
3024 break;
3025 case QEMU_OPTION_portrait:
3026 graphic_rotate = 90;
3027 break;
3028 case QEMU_OPTION_rotate:
3029 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3030 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3031 graphic_rotate != 180 && graphic_rotate != 270) {
3032 fprintf(stderr,
3033 "qemu: only 90, 180, 270 deg rotation is available\n");
3034 exit(1);
3036 break;
3037 case QEMU_OPTION_kernel:
3038 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3039 break;
3040 case QEMU_OPTION_initrd:
3041 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3042 break;
3043 case QEMU_OPTION_append:
3044 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3045 break;
3046 case QEMU_OPTION_dtb:
3047 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3048 break;
3049 case QEMU_OPTION_cdrom:
3050 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3051 break;
3052 case QEMU_OPTION_boot:
3053 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3054 if (!opts) {
3055 exit(1);
3057 break;
3058 case QEMU_OPTION_fda:
3059 case QEMU_OPTION_fdb:
3060 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3061 optarg, FD_OPTS);
3062 break;
3063 case QEMU_OPTION_no_fd_bootchk:
3064 fd_bootchk = 0;
3065 break;
3066 case QEMU_OPTION_netdev:
3067 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3068 exit(1);
3070 break;
3071 case QEMU_OPTION_net:
3072 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3073 exit(1);
3075 break;
3076 #ifdef CONFIG_LIBISCSI
3077 case QEMU_OPTION_iscsi:
3078 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3079 if (!opts) {
3080 exit(1);
3082 break;
3083 #endif
3084 #ifdef CONFIG_SLIRP
3085 case QEMU_OPTION_tftp:
3086 legacy_tftp_prefix = optarg;
3087 break;
3088 case QEMU_OPTION_bootp:
3089 legacy_bootp_filename = optarg;
3090 break;
3091 case QEMU_OPTION_redir:
3092 if (net_slirp_redir(optarg) < 0)
3093 exit(1);
3094 break;
3095 #endif
3096 case QEMU_OPTION_bt:
3097 add_device_config(DEV_BT, optarg);
3098 break;
3099 case QEMU_OPTION_audio_help:
3100 AUD_help ();
3101 exit (0);
3102 break;
3103 case QEMU_OPTION_soundhw:
3104 select_soundhw (optarg);
3105 break;
3106 case QEMU_OPTION_h:
3107 help(0);
3108 break;
3109 case QEMU_OPTION_version:
3110 version();
3111 exit(0);
3112 break;
3113 case QEMU_OPTION_m: {
3114 uint64_t sz;
3115 const char *mem_str;
3116 const char *maxmem_str, *slots_str;
3118 opts = qemu_opts_parse(qemu_find_opts("memory"),
3119 optarg, 1);
3120 if (!opts) {
3121 exit(EXIT_FAILURE);
3124 mem_str = qemu_opt_get(opts, "size");
3125 if (!mem_str) {
3126 error_report("invalid -m option, missing 'size' option");
3127 exit(EXIT_FAILURE);
3129 if (!*mem_str) {
3130 error_report("missing 'size' option value");
3131 exit(EXIT_FAILURE);
3134 sz = qemu_opt_get_size(opts, "size", ram_size);
3136 /* Fix up legacy suffix-less format */
3137 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3138 uint64_t overflow_check = sz;
3140 sz <<= 20;
3141 if ((sz >> 20) != overflow_check) {
3142 error_report("too large 'size' option value");
3143 exit(EXIT_FAILURE);
3147 /* backward compatibility behaviour for case "-m 0" */
3148 if (sz == 0) {
3149 sz = default_ram_size;
3152 sz = QEMU_ALIGN_UP(sz, 8192);
3153 ram_size = sz;
3154 if (ram_size != sz) {
3155 error_report("ram size too large");
3156 exit(EXIT_FAILURE);
3158 maxram_size = ram_size;
3160 maxmem_str = qemu_opt_get(opts, "maxmem");
3161 slots_str = qemu_opt_get(opts, "slots");
3162 if (maxmem_str && slots_str) {
3163 uint64_t slots;
3165 sz = qemu_opt_get_size(opts, "maxmem", 0);
3166 if (sz < ram_size) {
3167 error_report("invalid -m option value: maxmem "
3168 "(0x%" PRIx64 ") <= initial memory (0x"
3169 RAM_ADDR_FMT ")", sz, ram_size);
3170 exit(EXIT_FAILURE);
3173 slots = qemu_opt_get_number(opts, "slots", 0);
3174 if ((sz > ram_size) && !slots) {
3175 error_report("invalid -m option value: maxmem "
3176 "(0x%" PRIx64 ") more than initial memory (0x"
3177 RAM_ADDR_FMT ") but no hotplug slots where "
3178 "specified", sz, ram_size);
3179 exit(EXIT_FAILURE);
3182 if ((sz <= ram_size) && slots) {
3183 error_report("invalid -m option value: %"
3184 PRIu64 " hotplug slots where specified but "
3185 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3186 RAM_ADDR_FMT ")", slots, sz, ram_size);
3187 exit(EXIT_FAILURE);
3189 maxram_size = sz;
3190 ram_slots = slots;
3191 } else if ((!maxmem_str && slots_str) ||
3192 (maxmem_str && !slots_str)) {
3193 error_report("invalid -m option value: missing "
3194 "'%s' option", slots_str ? "maxmem" : "slots");
3195 exit(EXIT_FAILURE);
3197 break;
3199 #ifdef CONFIG_TPM
3200 case QEMU_OPTION_tpmdev:
3201 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3202 exit(1);
3204 break;
3205 #endif
3206 case QEMU_OPTION_mempath:
3207 mem_path = optarg;
3208 break;
3209 case QEMU_OPTION_mem_prealloc:
3210 mem_prealloc = 1;
3211 break;
3212 case QEMU_OPTION_d:
3213 log_mask = optarg;
3214 break;
3215 case QEMU_OPTION_D:
3216 log_file = optarg;
3217 break;
3218 case QEMU_OPTION_s:
3219 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3220 break;
3221 case QEMU_OPTION_gdb:
3222 add_device_config(DEV_GDB, optarg);
3223 break;
3224 case QEMU_OPTION_L:
3225 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3226 data_dir[data_dir_idx++] = optarg;
3228 break;
3229 case QEMU_OPTION_bios:
3230 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3231 break;
3232 case QEMU_OPTION_singlestep:
3233 singlestep = 1;
3234 break;
3235 case QEMU_OPTION_S:
3236 autostart = 0;
3237 break;
3238 case QEMU_OPTION_k:
3239 keyboard_layout = optarg;
3240 break;
3241 case QEMU_OPTION_localtime:
3242 rtc_utc = 0;
3243 break;
3244 case QEMU_OPTION_vga:
3245 vga_model = optarg;
3246 default_vga = 0;
3247 break;
3248 case QEMU_OPTION_g:
3250 const char *p;
3251 int w, h, depth;
3252 p = optarg;
3253 w = strtol(p, (char **)&p, 10);
3254 if (w <= 0) {
3255 graphic_error:
3256 fprintf(stderr, "qemu: invalid resolution or depth\n");
3257 exit(1);
3259 if (*p != 'x')
3260 goto graphic_error;
3261 p++;
3262 h = strtol(p, (char **)&p, 10);
3263 if (h <= 0)
3264 goto graphic_error;
3265 if (*p == 'x') {
3266 p++;
3267 depth = strtol(p, (char **)&p, 10);
3268 if (depth != 8 && depth != 15 && depth != 16 &&
3269 depth != 24 && depth != 32)
3270 goto graphic_error;
3271 } else if (*p == '\0') {
3272 depth = graphic_depth;
3273 } else {
3274 goto graphic_error;
3277 graphic_width = w;
3278 graphic_height = h;
3279 graphic_depth = depth;
3281 break;
3282 case QEMU_OPTION_echr:
3284 char *r;
3285 term_escape_char = strtol(optarg, &r, 0);
3286 if (r == optarg)
3287 printf("Bad argument to echr\n");
3288 break;
3290 case QEMU_OPTION_monitor:
3291 default_monitor = 0;
3292 if (strncmp(optarg, "none", 4)) {
3293 monitor_parse(optarg, "readline");
3295 break;
3296 case QEMU_OPTION_qmp:
3297 monitor_parse(optarg, "control");
3298 default_monitor = 0;
3299 break;
3300 case QEMU_OPTION_mon:
3301 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3302 if (!opts) {
3303 exit(1);
3305 default_monitor = 0;
3306 break;
3307 case QEMU_OPTION_chardev:
3308 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3309 if (!opts) {
3310 exit(1);
3312 break;
3313 case QEMU_OPTION_fsdev:
3314 olist = qemu_find_opts("fsdev");
3315 if (!olist) {
3316 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3317 exit(1);
3319 opts = qemu_opts_parse(olist, optarg, 1);
3320 if (!opts) {
3321 exit(1);
3323 break;
3324 case QEMU_OPTION_virtfs: {
3325 QemuOpts *fsdev;
3326 QemuOpts *device;
3327 const char *writeout, *sock_fd, *socket;
3329 olist = qemu_find_opts("virtfs");
3330 if (!olist) {
3331 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3332 exit(1);
3334 opts = qemu_opts_parse(olist, optarg, 1);
3335 if (!opts) {
3336 exit(1);
3339 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3340 qemu_opt_get(opts, "mount_tag") == NULL) {
3341 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3342 exit(1);
3344 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3345 qemu_opt_get(opts, "mount_tag"),
3346 1, NULL);
3347 if (!fsdev) {
3348 fprintf(stderr, "duplicate fsdev id: %s\n",
3349 qemu_opt_get(opts, "mount_tag"));
3350 exit(1);
3353 writeout = qemu_opt_get(opts, "writeout");
3354 if (writeout) {
3355 #ifdef CONFIG_SYNC_FILE_RANGE
3356 qemu_opt_set(fsdev, "writeout", writeout);
3357 #else
3358 fprintf(stderr, "writeout=immediate not supported on "
3359 "this platform\n");
3360 exit(1);
3361 #endif
3363 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3364 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3365 qemu_opt_set(fsdev, "security_model",
3366 qemu_opt_get(opts, "security_model"));
3367 socket = qemu_opt_get(opts, "socket");
3368 if (socket) {
3369 qemu_opt_set(fsdev, "socket", socket);
3371 sock_fd = qemu_opt_get(opts, "sock_fd");
3372 if (sock_fd) {
3373 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3376 qemu_opt_set_bool(fsdev, "readonly",
3377 qemu_opt_get_bool(opts, "readonly", 0));
3378 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3379 &error_abort);
3380 qemu_opt_set(device, "driver", "virtio-9p-pci");
3381 qemu_opt_set(device, "fsdev",
3382 qemu_opt_get(opts, "mount_tag"));
3383 qemu_opt_set(device, "mount_tag",
3384 qemu_opt_get(opts, "mount_tag"));
3385 break;
3387 case QEMU_OPTION_virtfs_synth: {
3388 QemuOpts *fsdev;
3389 QemuOpts *device;
3391 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3392 1, NULL);
3393 if (!fsdev) {
3394 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3395 exit(1);
3397 qemu_opt_set(fsdev, "fsdriver", "synth");
3399 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3400 &error_abort);
3401 qemu_opt_set(device, "driver", "virtio-9p-pci");
3402 qemu_opt_set(device, "fsdev", "v_synth");
3403 qemu_opt_set(device, "mount_tag", "v_synth");
3404 break;
3406 case QEMU_OPTION_serial:
3407 add_device_config(DEV_SERIAL, optarg);
3408 default_serial = 0;
3409 if (strncmp(optarg, "mon:", 4) == 0) {
3410 default_monitor = 0;
3412 break;
3413 case QEMU_OPTION_watchdog:
3414 if (watchdog) {
3415 fprintf(stderr,
3416 "qemu: only one watchdog option may be given\n");
3417 return 1;
3419 watchdog = optarg;
3420 break;
3421 case QEMU_OPTION_watchdog_action:
3422 if (select_watchdog_action(optarg) == -1) {
3423 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3424 exit(1);
3426 break;
3427 case QEMU_OPTION_virtiocon:
3428 add_device_config(DEV_VIRTCON, optarg);
3429 default_virtcon = 0;
3430 if (strncmp(optarg, "mon:", 4) == 0) {
3431 default_monitor = 0;
3433 break;
3434 case QEMU_OPTION_parallel:
3435 add_device_config(DEV_PARALLEL, optarg);
3436 default_parallel = 0;
3437 if (strncmp(optarg, "mon:", 4) == 0) {
3438 default_monitor = 0;
3440 break;
3441 case QEMU_OPTION_debugcon:
3442 add_device_config(DEV_DEBUGCON, optarg);
3443 break;
3444 case QEMU_OPTION_loadvm:
3445 loadvm = optarg;
3446 break;
3447 case QEMU_OPTION_full_screen:
3448 full_screen = 1;
3449 break;
3450 case QEMU_OPTION_no_frame:
3451 no_frame = 1;
3452 break;
3453 case QEMU_OPTION_alt_grab:
3454 alt_grab = 1;
3455 break;
3456 case QEMU_OPTION_ctrl_grab:
3457 ctrl_grab = 1;
3458 break;
3459 case QEMU_OPTION_no_quit:
3460 no_quit = 1;
3461 break;
3462 case QEMU_OPTION_sdl:
3463 #ifdef CONFIG_SDL
3464 display_type = DT_SDL;
3465 break;
3466 #else
3467 fprintf(stderr, "SDL support is disabled\n");
3468 exit(1);
3469 #endif
3470 case QEMU_OPTION_pidfile:
3471 pid_file = optarg;
3472 break;
3473 case QEMU_OPTION_win2k_hack:
3474 win2k_install_hack = 1;
3475 break;
3476 case QEMU_OPTION_rtc_td_hack: {
3477 static GlobalProperty slew_lost_ticks[] = {
3479 .driver = "mc146818rtc",
3480 .property = "lost_tick_policy",
3481 .value = "slew",
3483 { /* end of list */ }
3486 qdev_prop_register_global_list(slew_lost_ticks);
3487 break;
3489 case QEMU_OPTION_acpitable:
3490 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3491 if (!opts) {
3492 exit(1);
3494 do_acpitable_option(opts);
3495 break;
3496 case QEMU_OPTION_smbios:
3497 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3498 if (!opts) {
3499 exit(1);
3501 do_smbios_option(opts);
3502 break;
3503 case QEMU_OPTION_enable_kvm:
3504 olist = qemu_find_opts("machine");
3505 qemu_opts_parse(olist, "accel=kvm", 0);
3506 break;
3507 case QEMU_OPTION_machine:
3508 olist = qemu_find_opts("machine");
3509 opts = qemu_opts_parse(olist, optarg, 1);
3510 if (!opts) {
3511 exit(1);
3513 optarg = qemu_opt_get(opts, "type");
3514 if (optarg) {
3515 machine_class = machine_parse(optarg);
3517 break;
3518 case QEMU_OPTION_no_kvm:
3519 olist = qemu_find_opts("machine");
3520 qemu_opts_parse(olist, "accel=tcg", 0);
3521 break;
3522 case QEMU_OPTION_no_kvm_pit: {
3523 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3524 "separately.\n");
3525 break;
3527 case QEMU_OPTION_no_kvm_pit_reinjection: {
3528 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3530 .driver = "kvm-pit",
3531 .property = "lost_tick_policy",
3532 .value = "discard",
3534 { /* end of list */ }
3537 fprintf(stderr, "Warning: option deprecated, use "
3538 "lost_tick_policy property of kvm-pit instead.\n");
3539 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3540 break;
3542 case QEMU_OPTION_usb:
3543 olist = qemu_find_opts("machine");
3544 qemu_opts_parse(olist, "usb=on", 0);
3545 break;
3546 case QEMU_OPTION_usbdevice:
3547 olist = qemu_find_opts("machine");
3548 qemu_opts_parse(olist, "usb=on", 0);
3549 add_device_config(DEV_USB, optarg);
3550 break;
3551 case QEMU_OPTION_device:
3552 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3553 exit(1);
3555 break;
3556 case QEMU_OPTION_smp:
3557 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3558 exit(1);
3560 break;
3561 case QEMU_OPTION_vnc:
3562 #ifdef CONFIG_VNC
3563 display_remote++;
3564 vnc_display = optarg;
3565 #else
3566 fprintf(stderr, "VNC support is disabled\n");
3567 exit(1);
3568 #endif
3569 break;
3570 case QEMU_OPTION_no_acpi:
3571 acpi_enabled = 0;
3572 break;
3573 case QEMU_OPTION_no_hpet:
3574 no_hpet = 1;
3575 break;
3576 case QEMU_OPTION_balloon:
3577 if (balloon_parse(optarg) < 0) {
3578 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3579 exit(1);
3581 break;
3582 case QEMU_OPTION_no_reboot:
3583 no_reboot = 1;
3584 break;
3585 case QEMU_OPTION_no_shutdown:
3586 no_shutdown = 1;
3587 break;
3588 case QEMU_OPTION_show_cursor:
3589 cursor_hide = 0;
3590 break;
3591 case QEMU_OPTION_uuid:
3592 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3593 fprintf(stderr, "Fail to parse UUID string."
3594 " Wrong format.\n");
3595 exit(1);
3597 qemu_uuid_set = true;
3598 break;
3599 case QEMU_OPTION_option_rom:
3600 if (nb_option_roms >= MAX_OPTION_ROMS) {
3601 fprintf(stderr, "Too many option ROMs\n");
3602 exit(1);
3604 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3605 if (!opts) {
3606 exit(1);
3608 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3609 option_rom[nb_option_roms].bootindex =
3610 qemu_opt_get_number(opts, "bootindex", -1);
3611 if (!option_rom[nb_option_roms].name) {
3612 fprintf(stderr, "Option ROM file is not specified\n");
3613 exit(1);
3615 nb_option_roms++;
3616 break;
3617 case QEMU_OPTION_semihosting:
3618 semihosting_enabled = 1;
3619 break;
3620 case QEMU_OPTION_tdf:
3621 fprintf(stderr, "Warning: user space PIT time drift fix "
3622 "is no longer supported.\n");
3623 break;
3624 case QEMU_OPTION_name:
3625 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3626 if (!opts) {
3627 exit(1);
3629 break;
3630 case QEMU_OPTION_prom_env:
3631 if (nb_prom_envs >= MAX_PROM_ENVS) {
3632 fprintf(stderr, "Too many prom variables\n");
3633 exit(1);
3635 prom_envs[nb_prom_envs] = optarg;
3636 nb_prom_envs++;
3637 break;
3638 case QEMU_OPTION_old_param:
3639 old_param = 1;
3640 break;
3641 case QEMU_OPTION_clock:
3642 /* Clock options no longer exist. Keep this option for
3643 * backward compatibility.
3645 break;
3646 case QEMU_OPTION_startdate:
3647 configure_rtc_date_offset(optarg, 1);
3648 break;
3649 case QEMU_OPTION_rtc:
3650 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3651 if (!opts) {
3652 exit(1);
3654 configure_rtc(opts);
3655 break;
3656 case QEMU_OPTION_tb_size:
3657 tcg_tb_size = strtol(optarg, NULL, 0);
3658 if (tcg_tb_size < 0) {
3659 tcg_tb_size = 0;
3661 break;
3662 case QEMU_OPTION_icount:
3663 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3664 optarg, 1);
3665 if (!icount_opts) {
3666 exit(1);
3668 break;
3669 case QEMU_OPTION_incoming:
3670 incoming = optarg;
3671 runstate_set(RUN_STATE_INMIGRATE);
3672 break;
3673 case QEMU_OPTION_nodefaults:
3674 has_defaults = 0;
3675 break;
3676 case QEMU_OPTION_xen_domid:
3677 if (!(xen_available())) {
3678 printf("Option %s not supported for this target\n", popt->name);
3679 exit(1);
3681 xen_domid = atoi(optarg);
3682 break;
3683 case QEMU_OPTION_xen_create:
3684 if (!(xen_available())) {
3685 printf("Option %s not supported for this target\n", popt->name);
3686 exit(1);
3688 xen_mode = XEN_CREATE;
3689 break;
3690 case QEMU_OPTION_xen_attach:
3691 if (!(xen_available())) {
3692 printf("Option %s not supported for this target\n", popt->name);
3693 exit(1);
3695 xen_mode = XEN_ATTACH;
3696 break;
3697 case QEMU_OPTION_trace:
3699 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3700 if (!opts) {
3701 exit(1);
3703 trace_events = qemu_opt_get(opts, "events");
3704 trace_file = qemu_opt_get(opts, "file");
3705 break;
3707 case QEMU_OPTION_readconfig:
3709 int ret = qemu_read_config_file(optarg);
3710 if (ret < 0) {
3711 fprintf(stderr, "read config %s: %s\n", optarg,
3712 strerror(-ret));
3713 exit(1);
3715 break;
3717 case QEMU_OPTION_spice:
3718 olist = qemu_find_opts("spice");
3719 if (!olist) {
3720 fprintf(stderr, "spice is not supported by this qemu build.\n");
3721 exit(1);
3723 opts = qemu_opts_parse(olist, optarg, 0);
3724 if (!opts) {
3725 exit(1);
3727 display_remote++;
3728 break;
3729 case QEMU_OPTION_writeconfig:
3731 FILE *fp;
3732 if (strcmp(optarg, "-") == 0) {
3733 fp = stdout;
3734 } else {
3735 fp = fopen(optarg, "w");
3736 if (fp == NULL) {
3737 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3738 exit(1);
3741 qemu_config_write(fp);
3742 if (fp != stdout) {
3743 fclose(fp);
3745 break;
3747 case QEMU_OPTION_qtest:
3748 qtest_chrdev = optarg;
3749 break;
3750 case QEMU_OPTION_qtest_log:
3751 qtest_log = optarg;
3752 break;
3753 case QEMU_OPTION_sandbox:
3754 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3755 if (!opts) {
3756 exit(1);
3758 break;
3759 case QEMU_OPTION_add_fd:
3760 #ifndef _WIN32
3761 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3762 if (!opts) {
3763 exit(1);
3765 #else
3766 error_report("File descriptor passing is disabled on this "
3767 "platform");
3768 exit(1);
3769 #endif
3770 break;
3771 case QEMU_OPTION_object:
3772 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3773 if (!opts) {
3774 exit(1);
3776 break;
3777 case QEMU_OPTION_realtime:
3778 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3779 if (!opts) {
3780 exit(1);
3782 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3783 break;
3784 case QEMU_OPTION_msg:
3785 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3786 if (!opts) {
3787 exit(1);
3789 configure_msg(opts);
3790 break;
3791 case QEMU_OPTION_dump_vmstate:
3792 vmstate_dump_file = fopen(optarg, "w");
3793 if (vmstate_dump_file == NULL) {
3794 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3795 exit(1);
3797 break;
3798 default:
3799 os_parse_cmd_args(popt->index, optarg);
3803 loc_set_none();
3805 os_daemonize();
3807 if (qemu_init_main_loop(&main_loop_err)) {
3808 error_report("%s", error_get_pretty(main_loop_err));
3809 exit(1);
3812 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3813 exit(1);
3816 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3817 exit(1);
3820 #ifndef _WIN32
3821 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3822 exit(1);
3825 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3826 exit(1);
3828 #endif
3830 if (machine_class == NULL) {
3831 fprintf(stderr, "No machine specified, and there is no default.\n"
3832 "Use -machine help to list supported machines!\n");
3833 exit(1);
3836 current_machine = MACHINE(object_new(object_class_get_name(
3837 OBJECT_CLASS(machine_class))));
3838 object_property_add_child(object_get_root(), "machine",
3839 OBJECT(current_machine), &error_abort);
3840 cpu_exec_init_all();
3842 if (machine_class->hw_version) {
3843 qemu_set_version(machine_class->hw_version);
3846 /* Init CPU def lists, based on config
3847 * - Must be called after all the qemu_read_config_file() calls
3848 * - Must be called before list_cpus()
3849 * - Must be called before machine->init()
3851 cpudef_init();
3853 if (cpu_model && is_help_option(cpu_model)) {
3854 list_cpus(stdout, &fprintf, cpu_model);
3855 exit(0);
3858 /* Open the logfile at this point, if necessary. We can't open the logfile
3859 * when encountering either of the logging options (-d or -D) because the
3860 * other one may be encountered later on the command line, changing the
3861 * location or level of logging.
3863 if (log_mask) {
3864 int mask;
3865 if (log_file) {
3866 qemu_set_log_filename(log_file);
3869 mask = qemu_str_to_log_mask(log_mask);
3870 if (!mask) {
3871 qemu_print_log_usage(stdout);
3872 exit(1);
3874 qemu_set_log(mask);
3877 if (!is_daemonized()) {
3878 if (!trace_init_backends(trace_events, trace_file)) {
3879 exit(1);
3883 /* If no data_dir is specified then try to find it relative to the
3884 executable path. */
3885 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3886 data_dir[data_dir_idx] = os_find_datadir();
3887 if (data_dir[data_dir_idx] != NULL) {
3888 data_dir_idx++;
3891 /* If all else fails use the install path specified when building. */
3892 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3893 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3896 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3898 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3899 if (smp_cpus > machine_class->max_cpus) {
3900 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3901 "supported by machine `%s' (%d)\n", smp_cpus,
3902 machine_class->name, machine_class->max_cpus);
3903 exit(1);
3907 * Get the default machine options from the machine if it is not already
3908 * specified either by the configuration file or by the command line.
3910 if (machine_class->default_machine_opts) {
3911 qemu_opts_set_defaults(qemu_find_opts("machine"),
3912 machine_class->default_machine_opts, 0);
3915 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3916 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3918 if (!vga_model && !default_vga) {
3919 vga_interface_type = VGA_DEVICE;
3921 if (!has_defaults || machine_class->no_serial) {
3922 default_serial = 0;
3924 if (!has_defaults || machine_class->no_parallel) {
3925 default_parallel = 0;
3927 if (!has_defaults || !machine_class->use_virtcon) {
3928 default_virtcon = 0;
3930 if (!has_defaults || !machine_class->use_sclp) {
3931 default_sclp = 0;
3933 if (!has_defaults || machine_class->no_floppy) {
3934 default_floppy = 0;
3936 if (!has_defaults || machine_class->no_cdrom) {
3937 default_cdrom = 0;
3939 if (!has_defaults || machine_class->no_sdcard) {
3940 default_sdcard = 0;
3942 if (!has_defaults) {
3943 default_monitor = 0;
3944 default_net = 0;
3945 default_vga = 0;
3948 if (is_daemonized()) {
3949 /* According to documentation and historically, -nographic redirects
3950 * serial port, parallel port and monitor to stdio, which does not work
3951 * with -daemonize. We can redirect these to null instead, but since
3952 * -nographic is legacy, let's just error out.
3953 * We disallow -nographic only if all other ports are not redirected
3954 * explicitly, to not break existing legacy setups which uses
3955 * -nographic _and_ redirects all ports explicitly - this is valid
3956 * usage, -nographic is just a no-op in this case.
3958 if (display_type == DT_NOGRAPHIC
3959 && (default_parallel || default_serial
3960 || default_monitor || default_virtcon)) {
3961 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3962 exit(1);
3964 #ifdef CONFIG_CURSES
3965 if (display_type == DT_CURSES) {
3966 fprintf(stderr, "curses display can not be used with -daemonize\n");
3967 exit(1);
3969 #endif
3972 if (display_type == DT_NOGRAPHIC) {
3973 if (default_parallel)
3974 add_device_config(DEV_PARALLEL, "null");
3975 if (default_serial && default_monitor) {
3976 add_device_config(DEV_SERIAL, "mon:stdio");
3977 } else if (default_virtcon && default_monitor) {
3978 add_device_config(DEV_VIRTCON, "mon:stdio");
3979 } else if (default_sclp && default_monitor) {
3980 add_device_config(DEV_SCLP, "mon:stdio");
3981 } else {
3982 if (default_serial)
3983 add_device_config(DEV_SERIAL, "stdio");
3984 if (default_virtcon)
3985 add_device_config(DEV_VIRTCON, "stdio");
3986 if (default_sclp) {
3987 add_device_config(DEV_SCLP, "stdio");
3989 if (default_monitor)
3990 monitor_parse("stdio", "readline");
3992 } else {
3993 if (default_serial)
3994 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3995 if (default_parallel)
3996 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3997 if (default_monitor)
3998 monitor_parse("vc:80Cx24C", "readline");
3999 if (default_virtcon)
4000 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4001 if (default_sclp) {
4002 add_device_config(DEV_SCLP, "vc:80Cx24C");
4006 if (display_type == DT_DEFAULT && !display_remote) {
4007 #if defined(CONFIG_GTK)
4008 display_type = DT_GTK;
4009 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4010 display_type = DT_SDL;
4011 #elif defined(CONFIG_VNC)
4012 vnc_display = "localhost:0,to=99";
4013 show_vnc_port = 1;
4014 #else
4015 display_type = DT_NONE;
4016 #endif
4019 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4020 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4021 "for SDL, ignoring option\n");
4023 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4024 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4025 "ignoring option\n");
4028 #if defined(CONFIG_GTK)
4029 if (display_type == DT_GTK) {
4030 early_gtk_display_init();
4032 #endif
4034 socket_init();
4036 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4037 exit(1);
4038 #ifdef CONFIG_VIRTFS
4039 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4040 exit(1);
4042 #endif
4044 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4045 os_pidfile_error();
4046 exit(1);
4049 /* store value for the future use */
4050 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4052 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4053 != 0) {
4054 exit(0);
4057 if (qemu_opts_foreach(qemu_find_opts("object"),
4058 object_create, NULL, 0) != 0) {
4059 exit(1);
4062 machine_opts = qemu_get_machine_opts();
4063 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4064 1) < 0) {
4065 object_unref(OBJECT(current_machine));
4066 exit(1);
4069 configure_accelerator(current_machine);
4071 if (qtest_chrdev) {
4072 Error *local_err = NULL;
4073 qtest_init(qtest_chrdev, qtest_log, &local_err);
4074 if (local_err) {
4075 error_report("%s", error_get_pretty(local_err));
4076 error_free(local_err);
4077 exit(1);
4081 machine_opts = qemu_get_machine_opts();
4082 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4083 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4084 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4085 bios_name = qemu_opt_get(machine_opts, "firmware");
4087 boot_order = machine_class->default_boot_order;
4088 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4089 if (opts) {
4090 char *normal_boot_order;
4091 const char *order, *once;
4093 order = qemu_opt_get(opts, "order");
4094 if (order) {
4095 validate_bootdevices(order);
4096 boot_order = order;
4099 once = qemu_opt_get(opts, "once");
4100 if (once) {
4101 validate_bootdevices(once);
4102 normal_boot_order = g_strdup(boot_order);
4103 boot_order = once;
4104 qemu_register_reset(restore_boot_order, normal_boot_order);
4107 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4108 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4111 if (!kernel_cmdline) {
4112 kernel_cmdline = "";
4113 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4116 linux_boot = (kernel_filename != NULL);
4118 if (!linux_boot && *kernel_cmdline != '\0') {
4119 fprintf(stderr, "-append only allowed with -kernel option\n");
4120 exit(1);
4123 if (!linux_boot && initrd_filename != NULL) {
4124 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4125 exit(1);
4128 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4129 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4130 exit(1);
4133 os_set_line_buffering();
4135 qemu_init_cpu_loop();
4136 qemu_mutex_lock_iothread();
4138 #ifdef CONFIG_SPICE
4139 /* spice needs the timers to be initialized by this point */
4140 qemu_spice_init();
4141 #endif
4143 cpu_ticks_init();
4144 if (icount_opts) {
4145 if (kvm_enabled() || xen_enabled()) {
4146 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4147 exit(1);
4149 configure_icount(icount_opts, &error_abort);
4150 qemu_opts_del(icount_opts);
4153 /* clean up network at qemu process termination */
4154 atexit(&net_cleanup);
4156 if (net_init_clients() < 0) {
4157 exit(1);
4160 #ifdef CONFIG_TPM
4161 if (tpm_init() < 0) {
4162 exit(1);
4164 #endif
4166 /* init the bluetooth world */
4167 if (foreach_device_config(DEV_BT, bt_parse))
4168 exit(1);
4170 if (!xen_enabled()) {
4171 /* On 32-bit hosts, QEMU is limited by virtual address space */
4172 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4173 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4174 exit(1);
4178 blk_mig_init();
4179 ram_mig_init();
4181 /* If the currently selected machine wishes to override the units-per-bus
4182 * property of its default HBA interface type, do so now. */
4183 if (machine_class->units_per_default_bus) {
4184 override_max_devs(machine_class->block_default_type,
4185 machine_class->units_per_default_bus);
4188 /* open the virtual block devices */
4189 if (snapshot)
4190 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4191 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4192 &machine_class->block_default_type, 1) != 0) {
4193 exit(1);
4196 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4197 CDROM_OPTS);
4198 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4199 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4201 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4202 NULL, 1) != 0) {
4203 exit(1);
4206 set_numa_nodes();
4208 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4209 exit(1);
4212 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4213 exit(1);
4214 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4215 exit(1);
4216 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4217 exit(1);
4218 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4219 exit(1);
4221 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4222 exit(1);
4224 /* If no default VGA is requested, the default is "none". */
4225 if (default_vga) {
4226 if (cirrus_vga_available()) {
4227 vga_model = "cirrus";
4228 } else if (vga_available()) {
4229 vga_model = "std";
4232 if (vga_model) {
4233 select_vgahw(vga_model);
4236 if (watchdog) {
4237 i = select_watchdog(watchdog);
4238 if (i > 0)
4239 exit (i == 1 ? 1 : 0);
4242 if (machine_class->compat_props) {
4243 qdev_prop_register_global_list(machine_class->compat_props);
4245 qemu_add_globals();
4247 qdev_machine_init();
4249 current_machine->ram_size = ram_size;
4250 current_machine->maxram_size = maxram_size;
4251 current_machine->ram_slots = ram_slots;
4252 current_machine->boot_order = boot_order;
4253 current_machine->cpu_model = cpu_model;
4255 machine_class->init(current_machine);
4257 realtime_init();
4259 audio_init();
4261 cpu_synchronize_all_post_init();
4263 set_numa_modes();
4265 /* init USB devices */
4266 if (usb_enabled(false)) {
4267 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4268 exit(1);
4271 /* init generic devices */
4272 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4273 exit(1);
4275 /* Did we create any drives that we failed to create a device for? */
4276 drive_check_orphaned();
4278 net_check_clients();
4280 ds = init_displaystate();
4282 /* init local displays */
4283 switch (display_type) {
4284 case DT_NOGRAPHIC:
4285 (void)ds; /* avoid warning if no display is configured */
4286 break;
4287 #if defined(CONFIG_CURSES)
4288 case DT_CURSES:
4289 curses_display_init(ds, full_screen);
4290 break;
4291 #endif
4292 #if defined(CONFIG_SDL)
4293 case DT_SDL:
4294 sdl_display_init(ds, full_screen, no_frame);
4295 break;
4296 #elif defined(CONFIG_COCOA)
4297 case DT_SDL:
4298 cocoa_display_init(ds, full_screen);
4299 break;
4300 #endif
4301 #if defined(CONFIG_GTK)
4302 case DT_GTK:
4303 gtk_display_init(ds, full_screen, grab_on_hover);
4304 break;
4305 #endif
4306 default:
4307 break;
4310 /* must be after terminal init, SDL library changes signal handlers */
4311 os_setup_signal_handling();
4313 #ifdef CONFIG_VNC
4314 /* init remote displays */
4315 if (vnc_display) {
4316 Error *local_err = NULL;
4317 vnc_display_init(ds);
4318 vnc_display_open(ds, vnc_display, &local_err);
4319 if (local_err != NULL) {
4320 error_report("Failed to start VNC server on `%s': %s",
4321 vnc_display, error_get_pretty(local_err));
4322 error_free(local_err);
4323 exit(1);
4326 if (show_vnc_port) {
4327 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4330 #endif
4331 #ifdef CONFIG_SPICE
4332 if (using_spice) {
4333 qemu_spice_display_init();
4335 #endif
4337 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4338 exit(1);
4341 qdev_machine_creation_done();
4343 if (rom_load_all() != 0) {
4344 fprintf(stderr, "rom loading failed\n");
4345 exit(1);
4348 /* TODO: once all bus devices are qdevified, this should be done
4349 * when bus is created by qdev.c */
4350 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4351 qemu_run_machine_init_done_notifiers();
4353 /* Done notifiers can load ROMs */
4354 rom_load_done();
4356 qemu_system_reset(VMRESET_SILENT);
4357 if (loadvm) {
4358 if (load_vmstate(loadvm) < 0) {
4359 autostart = 0;
4363 qdev_prop_check_globals();
4364 if (vmstate_dump_file) {
4365 /* dump and exit */
4366 dump_vmstate_json_to_file(vmstate_dump_file);
4367 return 0;
4370 if (incoming) {
4371 Error *local_err = NULL;
4372 qemu_start_incoming_migration(incoming, &local_err);
4373 if (local_err) {
4374 error_report("-incoming %s: %s", incoming,
4375 error_get_pretty(local_err));
4376 error_free(local_err);
4377 exit(1);
4379 } else if (autostart) {
4380 vm_start();
4383 os_setup_post();
4385 if (is_daemonized()) {
4386 if (!trace_init_backends(trace_events, trace_file)) {
4387 exit(1);
4391 main_loop();
4392 bdrv_close_all();
4393 pause_all_vcpus();
4394 res_free();
4395 #ifdef CONFIG_TPM
4396 tpm_cleanup();
4397 #endif
4399 return 0;