dump: Propagate errors into qmp_dump_guest_memory()
[qemu/ar7.git] / vl.c
blobaee73e192f58d14192bef68eafe1feae23fd3a16
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/pcmcia.h"
67 #include "hw/i386/pc.h"
68 #include "hw/isa/isa.h"
69 #include "hw/bt.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/i386/smbios.h"
72 #include "hw/xen/xen.h"
73 #include "hw/qdev.h"
74 #include "hw/loader.h"
75 #include "monitor/qdev.h"
76 #include "sysemu/bt.h"
77 #include "net/net.h"
78 #include "net/slirp.h"
79 #include "monitor/monitor.h"
80 #include "ui/console.h"
81 #include "sysemu/sysemu.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
100 #ifdef CONFIG_VIRTFS
101 #include "fsdev/qemu-fsdev.h"
102 #endif
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
110 #include "trace.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 DisplayType display_type = DT_DEFAULT;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
139 int nb_nics;
140 NICInfo nd_table[MAX_NICS];
141 int autostart;
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
148 int no_quit = 0;
149 #ifdef CONFIG_GTK
150 static bool grab_on_hover;
151 #endif
152 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
153 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
154 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
155 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
156 int win2k_install_hack = 0;
157 int singlestep = 0;
158 int smp_cpus = 1;
159 int max_cpus = 0;
160 int smp_cores = 1;
161 int smp_threads = 1;
162 #ifdef CONFIG_VNC
163 const char *vnc_display;
164 #endif
165 int acpi_enabled = 1;
166 int no_hpet = 0;
167 int fd_bootchk = 1;
168 static int no_reboot;
169 int no_shutdown = 0;
170 int cursor_hide = 1;
171 int graphic_rotate = 0;
172 const char *watchdog;
173 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
174 int nb_option_roms;
175 int semihosting_enabled = 0;
176 int old_param = 0;
177 const char *qemu_name;
178 int alt_grab = 0;
179 int ctrl_grab = 0;
180 unsigned int nb_prom_envs = 0;
181 const char *prom_envs[MAX_PROM_ENVS];
182 int boot_menu;
183 bool boot_strict;
184 uint8_t *boot_splash_filedata;
185 size_t boot_splash_filedata_size;
186 uint8_t qemu_extra_params_fw[2];
188 int icount_align_option;
190 int nb_numa_nodes;
191 int max_numa_nodeid;
192 NodeInfo numa_info[MAX_NODES];
194 uint8_t qemu_uuid[16];
195 bool qemu_uuid_set;
197 static QEMUBootSetHandler *boot_set_handler;
198 static void *boot_set_opaque;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 bool xen_allowed;
207 uint32_t xen_domid;
208 enum xen_mode xen_mode = XEN_EMULATE;
210 static int has_defaults = 1;
211 static int default_serial = 1;
212 static int default_parallel = 1;
213 static int default_virtcon = 1;
214 static int default_sclp = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
221 static struct {
222 const char *driver;
223 int *flag;
224 } default_list[] = {
225 { .driver = "isa-serial", .flag = &default_serial },
226 { .driver = "isa-parallel", .flag = &default_parallel },
227 { .driver = "isa-fdc", .flag = &default_floppy },
228 { .driver = "ide-cd", .flag = &default_cdrom },
229 { .driver = "ide-hd", .flag = &default_cdrom },
230 { .driver = "ide-drive", .flag = &default_cdrom },
231 { .driver = "scsi-cd", .flag = &default_cdrom },
232 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
233 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
234 { .driver = "virtio-serial", .flag = &default_virtcon },
235 { .driver = "VGA", .flag = &default_vga },
236 { .driver = "isa-vga", .flag = &default_vga },
237 { .driver = "cirrus-vga", .flag = &default_vga },
238 { .driver = "isa-cirrus-vga", .flag = &default_vga },
239 { .driver = "vmware-svga", .flag = &default_vga },
240 { .driver = "qxl-vga", .flag = &default_vga },
243 static QemuOptsList qemu_rtc_opts = {
244 .name = "rtc",
245 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
246 .desc = {
248 .name = "base",
249 .type = QEMU_OPT_STRING,
251 .name = "clock",
252 .type = QEMU_OPT_STRING,
254 .name = "driftfix",
255 .type = QEMU_OPT_STRING,
257 { /* end of list */ }
261 static QemuOptsList qemu_sandbox_opts = {
262 .name = "sandbox",
263 .implied_opt_name = "enable",
264 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
265 .desc = {
267 .name = "enable",
268 .type = QEMU_OPT_BOOL,
270 { /* end of list */ }
274 static QemuOptsList qemu_trace_opts = {
275 .name = "trace",
276 .implied_opt_name = "trace",
277 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
278 .desc = {
280 .name = "events",
281 .type = QEMU_OPT_STRING,
283 .name = "file",
284 .type = QEMU_OPT_STRING,
286 { /* end of list */ }
290 static QemuOptsList qemu_option_rom_opts = {
291 .name = "option-rom",
292 .implied_opt_name = "romfile",
293 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
294 .desc = {
296 .name = "bootindex",
297 .type = QEMU_OPT_NUMBER,
298 }, {
299 .name = "romfile",
300 .type = QEMU_OPT_STRING,
302 { /* end of list */ }
306 static QemuOptsList qemu_machine_opts = {
307 .name = "machine",
308 .implied_opt_name = "type",
309 .merge_lists = true,
310 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
311 .desc = {
313 .name = "type",
314 .type = QEMU_OPT_STRING,
315 .help = "emulated machine"
316 }, {
317 .name = "accel",
318 .type = QEMU_OPT_STRING,
319 .help = "accelerator list",
320 }, {
321 .name = "kernel_irqchip",
322 .type = QEMU_OPT_BOOL,
323 .help = "use KVM in-kernel irqchip",
324 }, {
325 .name = "kvm_shadow_mem",
326 .type = QEMU_OPT_SIZE,
327 .help = "KVM shadow MMU size",
328 }, {
329 .name = "kernel",
330 .type = QEMU_OPT_STRING,
331 .help = "Linux kernel image file",
332 }, {
333 .name = "initrd",
334 .type = QEMU_OPT_STRING,
335 .help = "Linux initial ramdisk file",
336 }, {
337 .name = "append",
338 .type = QEMU_OPT_STRING,
339 .help = "Linux kernel command line",
340 }, {
341 .name = "dtb",
342 .type = QEMU_OPT_STRING,
343 .help = "Linux kernel device tree file",
344 }, {
345 .name = "dumpdtb",
346 .type = QEMU_OPT_STRING,
347 .help = "Dump current dtb to a file and quit",
348 }, {
349 .name = "phandle_start",
350 .type = QEMU_OPT_NUMBER,
351 .help = "The first phandle ID we may generate dynamically",
352 }, {
353 .name = "dt_compatible",
354 .type = QEMU_OPT_STRING,
355 .help = "Overrides the \"compatible\" property of the dt root node",
356 }, {
357 .name = "dump-guest-core",
358 .type = QEMU_OPT_BOOL,
359 .help = "Include guest memory in a core dump",
360 }, {
361 .name = "mem-merge",
362 .type = QEMU_OPT_BOOL,
363 .help = "enable/disable memory merge support",
365 .name = "usb",
366 .type = QEMU_OPT_BOOL,
367 .help = "Set on/off to enable/disable usb",
369 .name = "firmware",
370 .type = QEMU_OPT_STRING,
371 .help = "firmware image",
373 .name = "kvm-type",
374 .type = QEMU_OPT_STRING,
375 .help = "Specifies the KVM virtualization mode (HV, PR)",
377 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
378 .type = QEMU_OPT_SIZE,
379 .help = "maximum ram below the 4G boundary (32bit boundary)",
381 .name = "iommu",
382 .type = QEMU_OPT_BOOL,
383 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
385 { /* End of list */ }
389 static QemuOptsList qemu_boot_opts = {
390 .name = "boot-opts",
391 .implied_opt_name = "order",
392 .merge_lists = true,
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
394 .desc = {
396 .name = "order",
397 .type = QEMU_OPT_STRING,
398 }, {
399 .name = "once",
400 .type = QEMU_OPT_STRING,
401 }, {
402 .name = "menu",
403 .type = QEMU_OPT_BOOL,
404 }, {
405 .name = "splash",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "splash-time",
409 .type = QEMU_OPT_STRING,
410 }, {
411 .name = "reboot-timeout",
412 .type = QEMU_OPT_STRING,
413 }, {
414 .name = "strict",
415 .type = QEMU_OPT_BOOL,
417 { /*End of list */ }
421 static QemuOptsList qemu_add_fd_opts = {
422 .name = "add-fd",
423 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
424 .desc = {
426 .name = "fd",
427 .type = QEMU_OPT_NUMBER,
428 .help = "file descriptor of which a duplicate is added to fd set",
430 .name = "set",
431 .type = QEMU_OPT_NUMBER,
432 .help = "ID of the fd set to add fd to",
434 .name = "opaque",
435 .type = QEMU_OPT_STRING,
436 .help = "free-form string used to describe fd",
438 { /* end of list */ }
442 static QemuOptsList qemu_object_opts = {
443 .name = "object",
444 .implied_opt_name = "qom-type",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
446 .desc = {
451 static QemuOptsList qemu_tpmdev_opts = {
452 .name = "tpmdev",
453 .implied_opt_name = "type",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
455 .desc = {
456 /* options are defined in the TPM backends */
457 { /* end of list */ }
461 static QemuOptsList qemu_realtime_opts = {
462 .name = "realtime",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
464 .desc = {
466 .name = "mlock",
467 .type = QEMU_OPT_BOOL,
469 { /* end of list */ }
473 static QemuOptsList qemu_msg_opts = {
474 .name = "msg",
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
476 .desc = {
478 .name = "timestamp",
479 .type = QEMU_OPT_BOOL,
481 { /* end of list */ }
485 static QemuOptsList qemu_name_opts = {
486 .name = "name",
487 .implied_opt_name = "guest",
488 .merge_lists = true,
489 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
490 .desc = {
492 .name = "guest",
493 .type = QEMU_OPT_STRING,
494 .help = "Sets the name of the guest.\n"
495 "This name will be displayed in the SDL window caption.\n"
496 "The name will also be used for the VNC server",
497 }, {
498 .name = "process",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the name of the QEMU process, as shown in top etc",
501 }, {
502 .name = "debug-threads",
503 .type = QEMU_OPT_BOOL,
504 .help = "When enabled, name the individual threads; defaults off.\n"
505 "NOTE: The thread names are for debugging and not a\n"
506 "stable API.",
508 { /* End of list */ }
512 static QemuOptsList qemu_mem_opts = {
513 .name = "memory",
514 .implied_opt_name = "size",
515 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
516 .merge_lists = true,
517 .desc = {
519 .name = "size",
520 .type = QEMU_OPT_SIZE,
523 .name = "slots",
524 .type = QEMU_OPT_NUMBER,
527 .name = "maxmem",
528 .type = QEMU_OPT_SIZE,
530 { /* end of list */ }
534 static QemuOptsList qemu_icount_opts = {
535 .name = "icount",
536 .implied_opt_name = "shift",
537 .merge_lists = true,
538 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
539 .desc = {
541 .name = "shift",
542 .type = QEMU_OPT_STRING,
543 }, {
544 .name = "align",
545 .type = QEMU_OPT_BOOL,
547 { /* end of list */ }
552 * Get machine options
554 * Returns: machine options (never null).
556 QemuOpts *qemu_get_machine_opts(void)
558 return qemu_find_opts_singleton("machine");
561 const char *qemu_get_vm_name(void)
563 return qemu_name;
566 static void res_free(void)
568 if (boot_splash_filedata != NULL) {
569 g_free(boot_splash_filedata);
570 boot_splash_filedata = NULL;
574 static int default_driver_check(QemuOpts *opts, void *opaque)
576 const char *driver = qemu_opt_get(opts, "driver");
577 int i;
579 if (!driver)
580 return 0;
581 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
582 if (strcmp(default_list[i].driver, driver) != 0)
583 continue;
584 *(default_list[i].flag) = 0;
586 return 0;
589 /***********************************************************/
590 /* QEMU state */
592 static RunState current_run_state = RUN_STATE_PRELAUNCH;
594 /* We use RUN_STATE_MAX but any invalid value will do */
595 static RunState vmstop_requested = RUN_STATE_MAX;
596 static QemuMutex vmstop_lock;
598 typedef struct {
599 RunState from;
600 RunState to;
601 } RunStateTransition;
603 static const RunStateTransition runstate_transitions_def[] = {
604 /* from -> to */
605 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
606 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
609 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
611 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
612 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
615 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
618 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
621 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
624 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
625 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
627 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
628 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
630 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
632 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
633 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
634 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
635 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
636 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
637 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
638 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
639 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
640 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
641 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
643 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
645 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
646 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
649 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
650 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
651 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
653 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
654 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
657 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_MAX, RUN_STATE_MAX },
662 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
664 bool runstate_check(RunState state)
666 return current_run_state == state;
669 static void runstate_init(void)
671 const RunStateTransition *p;
673 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
674 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
675 runstate_valid_transitions[p->from][p->to] = true;
678 qemu_mutex_init(&vmstop_lock);
681 /* This function will abort() on invalid state transitions */
682 void runstate_set(RunState new_state)
684 assert(new_state < RUN_STATE_MAX);
686 if (!runstate_valid_transitions[current_run_state][new_state]) {
687 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
688 RunState_lookup[current_run_state],
689 RunState_lookup[new_state]);
690 abort();
692 trace_runstate_set(new_state);
693 current_run_state = new_state;
696 int runstate_is_running(void)
698 return runstate_check(RUN_STATE_RUNNING);
701 bool runstate_needs_reset(void)
703 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
704 runstate_check(RUN_STATE_SHUTDOWN);
707 StatusInfo *qmp_query_status(Error **errp)
709 StatusInfo *info = g_malloc0(sizeof(*info));
711 info->running = runstate_is_running();
712 info->singlestep = singlestep;
713 info->status = current_run_state;
715 return info;
718 static bool qemu_vmstop_requested(RunState *r)
720 qemu_mutex_lock(&vmstop_lock);
721 *r = vmstop_requested;
722 vmstop_requested = RUN_STATE_MAX;
723 qemu_mutex_unlock(&vmstop_lock);
724 return *r < RUN_STATE_MAX;
727 void qemu_system_vmstop_request_prepare(void)
729 qemu_mutex_lock(&vmstop_lock);
732 void qemu_system_vmstop_request(RunState state)
734 vmstop_requested = state;
735 qemu_mutex_unlock(&vmstop_lock);
736 qemu_notify_event();
739 void vm_start(void)
741 RunState requested;
743 qemu_vmstop_requested(&requested);
744 if (runstate_is_running() && requested == RUN_STATE_MAX) {
745 return;
748 /* Ensure that a STOP/RESUME pair of events is emitted if a
749 * vmstop request was pending. The BLOCK_IO_ERROR event, for
750 * example, according to documentation is always followed by
751 * the STOP event.
753 if (runstate_is_running()) {
754 qapi_event_send_stop(&error_abort);
755 } else {
756 cpu_enable_ticks();
757 runstate_set(RUN_STATE_RUNNING);
758 vm_state_notify(1, RUN_STATE_RUNNING);
759 resume_all_vcpus();
762 qapi_event_send_resume(&error_abort);
766 /***********************************************************/
767 /* real time host monotonic timer */
769 /***********************************************************/
770 /* host time/date access */
771 void qemu_get_timedate(struct tm *tm, int offset)
773 time_t ti;
775 time(&ti);
776 ti += offset;
777 if (rtc_date_offset == -1) {
778 if (rtc_utc)
779 gmtime_r(&ti, tm);
780 else
781 localtime_r(&ti, tm);
782 } else {
783 ti -= rtc_date_offset;
784 gmtime_r(&ti, tm);
788 int qemu_timedate_diff(struct tm *tm)
790 time_t seconds;
792 if (rtc_date_offset == -1)
793 if (rtc_utc)
794 seconds = mktimegm(tm);
795 else {
796 struct tm tmp = *tm;
797 tmp.tm_isdst = -1; /* use timezone to figure it out */
798 seconds = mktime(&tmp);
800 else
801 seconds = mktimegm(tm) + rtc_date_offset;
803 return seconds - time(NULL);
806 static void configure_rtc_date_offset(const char *startdate, int legacy)
808 time_t rtc_start_date;
809 struct tm tm;
811 if (!strcmp(startdate, "now") && legacy) {
812 rtc_date_offset = -1;
813 } else {
814 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
815 &tm.tm_year,
816 &tm.tm_mon,
817 &tm.tm_mday,
818 &tm.tm_hour,
819 &tm.tm_min,
820 &tm.tm_sec) == 6) {
821 /* OK */
822 } else if (sscanf(startdate, "%d-%d-%d",
823 &tm.tm_year,
824 &tm.tm_mon,
825 &tm.tm_mday) == 3) {
826 tm.tm_hour = 0;
827 tm.tm_min = 0;
828 tm.tm_sec = 0;
829 } else {
830 goto date_fail;
832 tm.tm_year -= 1900;
833 tm.tm_mon--;
834 rtc_start_date = mktimegm(&tm);
835 if (rtc_start_date == -1) {
836 date_fail:
837 fprintf(stderr, "Invalid date format. Valid formats are:\n"
838 "'2006-06-17T16:01:21' or '2006-06-17'\n");
839 exit(1);
841 rtc_date_offset = time(NULL) - rtc_start_date;
845 static void configure_rtc(QemuOpts *opts)
847 const char *value;
849 value = qemu_opt_get(opts, "base");
850 if (value) {
851 if (!strcmp(value, "utc")) {
852 rtc_utc = 1;
853 } else if (!strcmp(value, "localtime")) {
854 rtc_utc = 0;
855 } else {
856 configure_rtc_date_offset(value, 0);
859 value = qemu_opt_get(opts, "clock");
860 if (value) {
861 if (!strcmp(value, "host")) {
862 rtc_clock = QEMU_CLOCK_HOST;
863 } else if (!strcmp(value, "rt")) {
864 rtc_clock = QEMU_CLOCK_REALTIME;
865 } else if (!strcmp(value, "vm")) {
866 rtc_clock = QEMU_CLOCK_VIRTUAL;
867 } else {
868 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
869 exit(1);
872 value = qemu_opt_get(opts, "driftfix");
873 if (value) {
874 if (!strcmp(value, "slew")) {
875 static GlobalProperty slew_lost_ticks[] = {
877 .driver = "mc146818rtc",
878 .property = "lost_tick_policy",
879 .value = "slew",
881 { /* end of list */ }
884 qdev_prop_register_global_list(slew_lost_ticks);
885 } else if (!strcmp(value, "none")) {
886 /* discard is default */
887 } else {
888 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
889 exit(1);
894 /***********************************************************/
895 /* Bluetooth support */
896 static int nb_hcis;
897 static int cur_hci;
898 static struct HCIInfo *hci_table[MAX_NICS];
900 struct HCIInfo *qemu_next_hci(void)
902 if (cur_hci == nb_hcis)
903 return &null_hci;
905 return hci_table[cur_hci++];
908 static int bt_hci_parse(const char *str)
910 struct HCIInfo *hci;
911 bdaddr_t bdaddr;
913 if (nb_hcis >= MAX_NICS) {
914 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
915 return -1;
918 hci = hci_init(str);
919 if (!hci)
920 return -1;
922 bdaddr.b[0] = 0x52;
923 bdaddr.b[1] = 0x54;
924 bdaddr.b[2] = 0x00;
925 bdaddr.b[3] = 0x12;
926 bdaddr.b[4] = 0x34;
927 bdaddr.b[5] = 0x56 + nb_hcis;
928 hci->bdaddr_set(hci, bdaddr.b);
930 hci_table[nb_hcis++] = hci;
932 return 0;
935 static void bt_vhci_add(int vlan_id)
937 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
939 if (!vlan->slave)
940 fprintf(stderr, "qemu: warning: adding a VHCI to "
941 "an empty scatternet %i\n", vlan_id);
943 bt_vhci_init(bt_new_hci(vlan));
946 static struct bt_device_s *bt_device_add(const char *opt)
948 struct bt_scatternet_s *vlan;
949 int vlan_id = 0;
950 char *endp = strstr(opt, ",vlan=");
951 int len = (endp ? endp - opt : strlen(opt)) + 1;
952 char devname[10];
954 pstrcpy(devname, MIN(sizeof(devname), len), opt);
956 if (endp) {
957 vlan_id = strtol(endp + 6, &endp, 0);
958 if (*endp) {
959 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
960 return 0;
964 vlan = qemu_find_bt_vlan(vlan_id);
966 if (!vlan->slave)
967 fprintf(stderr, "qemu: warning: adding a slave device to "
968 "an empty scatternet %i\n", vlan_id);
970 if (!strcmp(devname, "keyboard"))
971 return bt_keyboard_init(vlan);
973 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
974 return 0;
977 static int bt_parse(const char *opt)
979 const char *endp, *p;
980 int vlan;
982 if (strstart(opt, "hci", &endp)) {
983 if (!*endp || *endp == ',') {
984 if (*endp)
985 if (!strstart(endp, ",vlan=", 0))
986 opt = endp + 1;
988 return bt_hci_parse(opt);
990 } else if (strstart(opt, "vhci", &endp)) {
991 if (!*endp || *endp == ',') {
992 if (*endp) {
993 if (strstart(endp, ",vlan=", &p)) {
994 vlan = strtol(p, (char **) &endp, 0);
995 if (*endp) {
996 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
997 return 1;
999 } else {
1000 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1001 return 1;
1003 } else
1004 vlan = 0;
1006 bt_vhci_add(vlan);
1007 return 0;
1009 } else if (strstart(opt, "device:", &endp))
1010 return !bt_device_add(endp);
1012 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1013 return 1;
1016 static int parse_sandbox(QemuOpts *opts, void *opaque)
1018 /* FIXME: change this to true for 1.3 */
1019 if (qemu_opt_get_bool(opts, "enable", false)) {
1020 #ifdef CONFIG_SECCOMP
1021 if (seccomp_start() < 0) {
1022 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1023 "failed to install seccomp syscall filter in the kernel");
1024 return -1;
1026 #else
1027 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1028 "sandboxing request but seccomp is not compiled into this build");
1029 return -1;
1030 #endif
1033 return 0;
1036 static int parse_name(QemuOpts *opts, void *opaque)
1038 const char *proc_name;
1040 if (qemu_opt_get(opts, "debug-threads")) {
1041 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1043 qemu_name = qemu_opt_get(opts, "guest");
1045 proc_name = qemu_opt_get(opts, "process");
1046 if (proc_name) {
1047 os_set_proc_name(proc_name);
1050 return 0;
1053 bool usb_enabled(bool default_usb)
1055 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1056 has_defaults && default_usb);
1059 #ifndef _WIN32
1060 static int parse_add_fd(QemuOpts *opts, void *opaque)
1062 int fd, dupfd, flags;
1063 int64_t fdset_id;
1064 const char *fd_opaque = NULL;
1066 fd = qemu_opt_get_number(opts, "fd", -1);
1067 fdset_id = qemu_opt_get_number(opts, "set", -1);
1068 fd_opaque = qemu_opt_get(opts, "opaque");
1070 if (fd < 0) {
1071 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1072 "fd option is required and must be non-negative");
1073 return -1;
1076 if (fd <= STDERR_FILENO) {
1077 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1078 "fd cannot be a standard I/O stream");
1079 return -1;
1083 * All fds inherited across exec() necessarily have FD_CLOEXEC
1084 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1086 flags = fcntl(fd, F_GETFD);
1087 if (flags == -1 || (flags & FD_CLOEXEC)) {
1088 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1089 "fd is not valid or already in use");
1090 return -1;
1093 if (fdset_id < 0) {
1094 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1095 "set option is required and must be non-negative");
1096 return -1;
1099 #ifdef F_DUPFD_CLOEXEC
1100 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1101 #else
1102 dupfd = dup(fd);
1103 if (dupfd != -1) {
1104 qemu_set_cloexec(dupfd);
1106 #endif
1107 if (dupfd == -1) {
1108 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1109 "Error duplicating fd: %s", strerror(errno));
1110 return -1;
1113 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1114 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1115 fd_opaque, NULL);
1117 return 0;
1120 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1122 int fd;
1124 fd = qemu_opt_get_number(opts, "fd", -1);
1125 close(fd);
1127 return 0;
1129 #endif
1131 /***********************************************************/
1132 /* QEMU Block devices */
1134 #define HD_OPTS "media=disk"
1135 #define CDROM_OPTS "media=cdrom"
1136 #define FD_OPTS ""
1137 #define PFLASH_OPTS ""
1138 #define MTD_OPTS ""
1139 #define SD_OPTS ""
1141 static int drive_init_func(QemuOpts *opts, void *opaque)
1143 BlockInterfaceType *block_default_type = opaque;
1145 return drive_new(opts, *block_default_type) == NULL;
1148 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1150 if (qemu_opt_get(opts, "snapshot") == NULL) {
1151 qemu_opt_set(opts, "snapshot", "on");
1153 return 0;
1156 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1157 int index, const char *optstr)
1159 QemuOpts *opts;
1160 DriveInfo *dinfo;
1162 if (!enable || drive_get_by_index(type, index)) {
1163 return;
1166 opts = drive_add(type, index, NULL, optstr);
1167 if (snapshot) {
1168 drive_enable_snapshot(opts, NULL);
1171 dinfo = drive_new(opts, type);
1172 if (!dinfo) {
1173 exit(1);
1175 dinfo->is_default = true;
1179 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1181 boot_set_handler = func;
1182 boot_set_opaque = opaque;
1185 int qemu_boot_set(const char *boot_order)
1187 if (!boot_set_handler) {
1188 return -EINVAL;
1190 return boot_set_handler(boot_set_opaque, boot_order);
1193 static void validate_bootdevices(const char *devices)
1195 /* We just do some generic consistency checks */
1196 const char *p;
1197 int bitmap = 0;
1199 for (p = devices; *p != '\0'; p++) {
1200 /* Allowed boot devices are:
1201 * a-b: floppy disk drives
1202 * c-f: IDE disk drives
1203 * g-m: machine implementation dependent drives
1204 * n-p: network devices
1205 * It's up to each machine implementation to check if the given boot
1206 * devices match the actual hardware implementation and firmware
1207 * features.
1209 if (*p < 'a' || *p > 'p') {
1210 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1211 exit(1);
1213 if (bitmap & (1 << (*p - 'a'))) {
1214 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1215 exit(1);
1217 bitmap |= 1 << (*p - 'a');
1221 static void restore_boot_order(void *opaque)
1223 char *normal_boot_order = opaque;
1224 static int first = 1;
1226 /* Restore boot order and remove ourselves after the first boot */
1227 if (first) {
1228 first = 0;
1229 return;
1232 qemu_boot_set(normal_boot_order);
1234 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1235 g_free(normal_boot_order);
1238 static QemuOptsList qemu_smp_opts = {
1239 .name = "smp-opts",
1240 .implied_opt_name = "cpus",
1241 .merge_lists = true,
1242 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1243 .desc = {
1245 .name = "cpus",
1246 .type = QEMU_OPT_NUMBER,
1247 }, {
1248 .name = "sockets",
1249 .type = QEMU_OPT_NUMBER,
1250 }, {
1251 .name = "cores",
1252 .type = QEMU_OPT_NUMBER,
1253 }, {
1254 .name = "threads",
1255 .type = QEMU_OPT_NUMBER,
1256 }, {
1257 .name = "maxcpus",
1258 .type = QEMU_OPT_NUMBER,
1260 { /*End of list */ }
1264 static void smp_parse(QemuOpts *opts)
1266 if (opts) {
1268 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1269 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1270 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1271 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1273 /* compute missing values, prefer sockets over cores over threads */
1274 if (cpus == 0 || sockets == 0) {
1275 sockets = sockets > 0 ? sockets : 1;
1276 cores = cores > 0 ? cores : 1;
1277 threads = threads > 0 ? threads : 1;
1278 if (cpus == 0) {
1279 cpus = cores * threads * sockets;
1281 } else {
1282 if (cores == 0) {
1283 threads = threads > 0 ? threads : 1;
1284 cores = cpus / (sockets * threads);
1285 } else {
1286 threads = cpus / (cores * sockets);
1290 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1292 smp_cpus = cpus;
1293 smp_cores = cores > 0 ? cores : 1;
1294 smp_threads = threads > 0 ? threads : 1;
1298 if (max_cpus == 0) {
1299 max_cpus = smp_cpus;
1302 if (max_cpus > MAX_CPUMASK_BITS) {
1303 fprintf(stderr, "Unsupported number of maxcpus\n");
1304 exit(1);
1306 if (max_cpus < smp_cpus) {
1307 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1308 exit(1);
1313 static void realtime_init(void)
1315 if (enable_mlock) {
1316 if (os_mlock() < 0) {
1317 fprintf(stderr, "qemu: locking memory failed\n");
1318 exit(1);
1324 static void configure_msg(QemuOpts *opts)
1326 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1329 /***********************************************************/
1330 /* USB devices */
1332 static int usb_device_add(const char *devname)
1334 USBDevice *dev = NULL;
1335 #ifndef CONFIG_LINUX
1336 const char *p;
1337 #endif
1339 if (!usb_enabled(false)) {
1340 return -1;
1343 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1344 dev = usbdevice_create(devname);
1345 if (dev)
1346 goto done;
1348 /* the other ones */
1349 #ifndef CONFIG_LINUX
1350 /* only the linux version is qdev-ified, usb-bsd still needs this */
1351 if (strstart(devname, "host:", &p)) {
1352 dev = usb_host_device_open(usb_bus_find(-1), p);
1354 #endif
1355 if (!dev)
1356 return -1;
1358 done:
1359 return 0;
1362 static int usb_device_del(const char *devname)
1364 int bus_num, addr;
1365 const char *p;
1367 if (strstart(devname, "host:", &p)) {
1368 return -1;
1371 if (!usb_enabled(false)) {
1372 return -1;
1375 p = strchr(devname, '.');
1376 if (!p)
1377 return -1;
1378 bus_num = strtoul(devname, NULL, 0);
1379 addr = strtoul(p + 1, NULL, 0);
1381 return usb_device_delete_addr(bus_num, addr);
1384 static int usb_parse(const char *cmdline)
1386 int r;
1387 r = usb_device_add(cmdline);
1388 if (r < 0) {
1389 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1391 return r;
1394 void do_usb_add(Monitor *mon, const QDict *qdict)
1396 const char *devname = qdict_get_str(qdict, "devname");
1397 if (usb_device_add(devname) < 0) {
1398 error_report("could not add USB device '%s'", devname);
1402 void do_usb_del(Monitor *mon, const QDict *qdict)
1404 const char *devname = qdict_get_str(qdict, "devname");
1405 if (usb_device_del(devname) < 0) {
1406 error_report("could not delete USB device '%s'", devname);
1410 /***********************************************************/
1411 /* PCMCIA/Cardbus */
1413 static struct pcmcia_socket_entry_s {
1414 PCMCIASocket *socket;
1415 struct pcmcia_socket_entry_s *next;
1416 } *pcmcia_sockets = 0;
1418 void pcmcia_socket_register(PCMCIASocket *socket)
1420 struct pcmcia_socket_entry_s *entry;
1422 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1423 entry->socket = socket;
1424 entry->next = pcmcia_sockets;
1425 pcmcia_sockets = entry;
1428 void pcmcia_socket_unregister(PCMCIASocket *socket)
1430 struct pcmcia_socket_entry_s *entry, **ptr;
1432 ptr = &pcmcia_sockets;
1433 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1434 if (entry->socket == socket) {
1435 *ptr = entry->next;
1436 g_free(entry);
1440 void pcmcia_info(Monitor *mon, const QDict *qdict)
1442 struct pcmcia_socket_entry_s *iter;
1444 if (!pcmcia_sockets)
1445 monitor_printf(mon, "No PCMCIA sockets\n");
1447 for (iter = pcmcia_sockets; iter; iter = iter->next)
1448 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1449 iter->socket->attached ? iter->socket->card_string :
1450 "Empty");
1453 /***********************************************************/
1454 /* machine registration */
1456 MachineState *current_machine;
1458 static void machine_class_init(ObjectClass *oc, void *data)
1460 MachineClass *mc = MACHINE_CLASS(oc);
1461 QEMUMachine *qm = data;
1463 mc->name = qm->name;
1464 mc->alias = qm->alias;
1465 mc->desc = qm->desc;
1466 mc->init = qm->init;
1467 mc->reset = qm->reset;
1468 mc->hot_add_cpu = qm->hot_add_cpu;
1469 mc->kvm_type = qm->kvm_type;
1470 mc->block_default_type = qm->block_default_type;
1471 mc->units_per_default_bus = qm->units_per_default_bus;
1472 mc->max_cpus = qm->max_cpus;
1473 mc->no_serial = qm->no_serial;
1474 mc->no_parallel = qm->no_parallel;
1475 mc->use_virtcon = qm->use_virtcon;
1476 mc->use_sclp = qm->use_sclp;
1477 mc->no_floppy = qm->no_floppy;
1478 mc->no_cdrom = qm->no_cdrom;
1479 mc->no_sdcard = qm->no_sdcard;
1480 mc->is_default = qm->is_default;
1481 mc->default_machine_opts = qm->default_machine_opts;
1482 mc->default_boot_order = qm->default_boot_order;
1483 mc->compat_props = qm->compat_props;
1484 mc->hw_version = qm->hw_version;
1487 int qemu_register_machine(QEMUMachine *m)
1489 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1490 TypeInfo ti = {
1491 .name = name,
1492 .parent = TYPE_MACHINE,
1493 .class_init = machine_class_init,
1494 .class_data = (void *)m,
1497 type_register(&ti);
1498 g_free(name);
1500 return 0;
1503 static MachineClass *find_machine(const char *name)
1505 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1506 MachineClass *mc = NULL;
1508 for (el = machines; el; el = el->next) {
1509 MachineClass *temp = el->data;
1511 if (!strcmp(temp->name, name)) {
1512 mc = temp;
1513 break;
1515 if (temp->alias &&
1516 !strcmp(temp->alias, name)) {
1517 mc = temp;
1518 break;
1522 g_slist_free(machines);
1523 return mc;
1526 MachineClass *find_default_machine(void)
1528 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1529 MachineClass *mc = NULL;
1531 for (el = machines; el; el = el->next) {
1532 MachineClass *temp = el->data;
1534 if (temp->is_default) {
1535 mc = temp;
1536 break;
1540 g_slist_free(machines);
1541 return mc;
1544 MachineInfoList *qmp_query_machines(Error **errp)
1546 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1547 MachineInfoList *mach_list = NULL;
1549 for (el = machines; el; el = el->next) {
1550 MachineClass *mc = el->data;
1551 MachineInfoList *entry;
1552 MachineInfo *info;
1554 info = g_malloc0(sizeof(*info));
1555 if (mc->is_default) {
1556 info->has_is_default = true;
1557 info->is_default = true;
1560 if (mc->alias) {
1561 info->has_alias = true;
1562 info->alias = g_strdup(mc->alias);
1565 info->name = g_strdup(mc->name);
1566 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1568 entry = g_malloc0(sizeof(*entry));
1569 entry->value = info;
1570 entry->next = mach_list;
1571 mach_list = entry;
1574 g_slist_free(machines);
1575 return mach_list;
1578 /***********************************************************/
1579 /* main execution loop */
1581 struct vm_change_state_entry {
1582 VMChangeStateHandler *cb;
1583 void *opaque;
1584 QLIST_ENTRY (vm_change_state_entry) entries;
1587 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1589 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1590 void *opaque)
1592 VMChangeStateEntry *e;
1594 e = g_malloc0(sizeof (*e));
1596 e->cb = cb;
1597 e->opaque = opaque;
1598 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1599 return e;
1602 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1604 QLIST_REMOVE (e, entries);
1605 g_free (e);
1608 void vm_state_notify(int running, RunState state)
1610 VMChangeStateEntry *e, *next;
1612 trace_vm_state_notify(running, state);
1614 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1615 e->cb(e->opaque, running, state);
1619 /* reset/shutdown handler */
1621 typedef struct QEMUResetEntry {
1622 QTAILQ_ENTRY(QEMUResetEntry) entry;
1623 QEMUResetHandler *func;
1624 void *opaque;
1625 } QEMUResetEntry;
1627 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1628 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1629 static int reset_requested;
1630 static int shutdown_requested, shutdown_signal = -1;
1631 static pid_t shutdown_pid;
1632 static int powerdown_requested;
1633 static int debug_requested;
1634 static int suspend_requested;
1635 static WakeupReason wakeup_reason;
1636 static NotifierList powerdown_notifiers =
1637 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1638 static NotifierList suspend_notifiers =
1639 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1640 static NotifierList wakeup_notifiers =
1641 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1642 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1644 int qemu_shutdown_requested_get(void)
1646 return shutdown_requested;
1649 int qemu_reset_requested_get(void)
1651 return reset_requested;
1654 static int qemu_shutdown_requested(void)
1656 int r = shutdown_requested;
1657 shutdown_requested = 0;
1658 return r;
1661 static void qemu_kill_report(void)
1663 if (!qtest_driver() && shutdown_signal != -1) {
1664 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1665 if (shutdown_pid == 0) {
1666 /* This happens for eg ^C at the terminal, so it's worth
1667 * avoiding printing an odd message in that case.
1669 fputc('\n', stderr);
1670 } else {
1671 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1673 shutdown_signal = -1;
1677 static int qemu_reset_requested(void)
1679 int r = reset_requested;
1680 reset_requested = 0;
1681 return r;
1684 static int qemu_suspend_requested(void)
1686 int r = suspend_requested;
1687 suspend_requested = 0;
1688 return r;
1691 static WakeupReason qemu_wakeup_requested(void)
1693 return wakeup_reason;
1696 static int qemu_powerdown_requested(void)
1698 int r = powerdown_requested;
1699 powerdown_requested = 0;
1700 return r;
1703 static int qemu_debug_requested(void)
1705 int r = debug_requested;
1706 debug_requested = 0;
1707 return r;
1710 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1712 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1714 re->func = func;
1715 re->opaque = opaque;
1716 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1719 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1721 QEMUResetEntry *re;
1723 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1724 if (re->func == func && re->opaque == opaque) {
1725 QTAILQ_REMOVE(&reset_handlers, re, entry);
1726 g_free(re);
1727 return;
1732 void qemu_devices_reset(void)
1734 QEMUResetEntry *re, *nre;
1736 /* reset all devices */
1737 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1738 re->func(re->opaque);
1742 void qemu_system_reset(bool report)
1744 MachineClass *mc;
1746 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1748 if (mc && mc->reset) {
1749 mc->reset();
1750 } else {
1751 qemu_devices_reset();
1753 if (report) {
1754 qapi_event_send_reset(&error_abort);
1756 cpu_synchronize_all_post_reset();
1759 void qemu_system_reset_request(void)
1761 if (no_reboot) {
1762 shutdown_requested = 1;
1763 } else {
1764 reset_requested = 1;
1766 cpu_stop_current();
1767 qemu_notify_event();
1770 static void qemu_system_suspend(void)
1772 pause_all_vcpus();
1773 notifier_list_notify(&suspend_notifiers, NULL);
1774 runstate_set(RUN_STATE_SUSPENDED);
1775 qapi_event_send_suspend(&error_abort);
1778 void qemu_system_suspend_request(void)
1780 if (runstate_check(RUN_STATE_SUSPENDED)) {
1781 return;
1783 suspend_requested = 1;
1784 cpu_stop_current();
1785 qemu_notify_event();
1788 void qemu_register_suspend_notifier(Notifier *notifier)
1790 notifier_list_add(&suspend_notifiers, notifier);
1793 void qemu_system_wakeup_request(WakeupReason reason)
1795 trace_system_wakeup_request(reason);
1797 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1798 return;
1800 if (!(wakeup_reason_mask & (1 << reason))) {
1801 return;
1803 runstate_set(RUN_STATE_RUNNING);
1804 wakeup_reason = reason;
1805 qemu_notify_event();
1808 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1810 if (enabled) {
1811 wakeup_reason_mask |= (1 << reason);
1812 } else {
1813 wakeup_reason_mask &= ~(1 << reason);
1817 void qemu_register_wakeup_notifier(Notifier *notifier)
1819 notifier_list_add(&wakeup_notifiers, notifier);
1822 void qemu_system_killed(int signal, pid_t pid)
1824 shutdown_signal = signal;
1825 shutdown_pid = pid;
1826 no_shutdown = 0;
1827 qemu_system_shutdown_request();
1830 void qemu_system_shutdown_request(void)
1832 trace_qemu_system_shutdown_request();
1833 shutdown_requested = 1;
1834 qemu_notify_event();
1837 static void qemu_system_powerdown(void)
1839 qapi_event_send_powerdown(&error_abort);
1840 notifier_list_notify(&powerdown_notifiers, NULL);
1843 void qemu_system_powerdown_request(void)
1845 trace_qemu_system_powerdown_request();
1846 powerdown_requested = 1;
1847 qemu_notify_event();
1850 void qemu_register_powerdown_notifier(Notifier *notifier)
1852 notifier_list_add(&powerdown_notifiers, notifier);
1855 void qemu_system_debug_request(void)
1857 debug_requested = 1;
1858 qemu_notify_event();
1861 static bool main_loop_should_exit(void)
1863 RunState r;
1864 if (qemu_debug_requested()) {
1865 vm_stop(RUN_STATE_DEBUG);
1867 if (qemu_suspend_requested()) {
1868 qemu_system_suspend();
1870 if (qemu_shutdown_requested()) {
1871 qemu_kill_report();
1872 qapi_event_send_shutdown(&error_abort);
1873 if (no_shutdown) {
1874 vm_stop(RUN_STATE_SHUTDOWN);
1875 } else {
1876 return true;
1879 if (qemu_reset_requested()) {
1880 pause_all_vcpus();
1881 cpu_synchronize_all_states();
1882 qemu_system_reset(VMRESET_REPORT);
1883 resume_all_vcpus();
1884 if (runstate_needs_reset()) {
1885 runstate_set(RUN_STATE_PAUSED);
1888 if (qemu_wakeup_requested()) {
1889 pause_all_vcpus();
1890 cpu_synchronize_all_states();
1891 qemu_system_reset(VMRESET_SILENT);
1892 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1893 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1894 resume_all_vcpus();
1895 qapi_event_send_wakeup(&error_abort);
1897 if (qemu_powerdown_requested()) {
1898 qemu_system_powerdown();
1900 if (qemu_vmstop_requested(&r)) {
1901 vm_stop(r);
1903 return false;
1906 static void main_loop(void)
1908 bool nonblocking;
1909 int last_io = 0;
1910 #ifdef CONFIG_PROFILER
1911 int64_t ti;
1912 #endif
1913 do {
1914 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1915 #ifdef CONFIG_PROFILER
1916 ti = profile_getclock();
1917 #endif
1918 last_io = main_loop_wait(nonblocking);
1919 #ifdef CONFIG_PROFILER
1920 dev_time += profile_getclock() - ti;
1921 #endif
1922 } while (!main_loop_should_exit());
1925 static void version(void)
1927 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1930 static void help(int exitcode)
1932 version();
1933 printf("usage: %s [options] [disk_image]\n\n"
1934 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1935 error_get_progname());
1937 #define QEMU_OPTIONS_GENERATE_HELP
1938 #include "qemu-options-wrapper.h"
1940 printf("\nDuring emulation, the following keys are useful:\n"
1941 "ctrl-alt-f toggle full screen\n"
1942 "ctrl-alt-n switch to virtual console 'n'\n"
1943 "ctrl-alt toggle mouse and keyboard grab\n"
1944 "\n"
1945 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1947 exit(exitcode);
1950 #define HAS_ARG 0x0001
1952 typedef struct QEMUOption {
1953 const char *name;
1954 int flags;
1955 int index;
1956 uint32_t arch_mask;
1957 } QEMUOption;
1959 static const QEMUOption qemu_options[] = {
1960 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1961 #define QEMU_OPTIONS_GENERATE_OPTIONS
1962 #include "qemu-options-wrapper.h"
1963 { NULL },
1966 static bool vga_available(void)
1968 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1971 static bool cirrus_vga_available(void)
1973 return object_class_by_name("cirrus-vga")
1974 || object_class_by_name("isa-cirrus-vga");
1977 static bool vmware_vga_available(void)
1979 return object_class_by_name("vmware-svga");
1982 static bool qxl_vga_available(void)
1984 return object_class_by_name("qxl-vga");
1987 static bool tcx_vga_available(void)
1989 return object_class_by_name("SUNW,tcx");
1992 static bool cg3_vga_available(void)
1994 return object_class_by_name("cgthree");
1997 static void select_vgahw (const char *p)
1999 const char *opts;
2001 assert(vga_interface_type == VGA_NONE);
2002 if (strstart(p, "std", &opts)) {
2003 if (vga_available()) {
2004 vga_interface_type = VGA_STD;
2005 } else {
2006 fprintf(stderr, "Error: standard VGA not available\n");
2007 exit(0);
2009 } else if (strstart(p, "cirrus", &opts)) {
2010 if (cirrus_vga_available()) {
2011 vga_interface_type = VGA_CIRRUS;
2012 } else {
2013 fprintf(stderr, "Error: Cirrus VGA not available\n");
2014 exit(0);
2016 } else if (strstart(p, "vmware", &opts)) {
2017 if (vmware_vga_available()) {
2018 vga_interface_type = VGA_VMWARE;
2019 } else {
2020 fprintf(stderr, "Error: VMWare SVGA not available\n");
2021 exit(0);
2023 } else if (strstart(p, "xenfb", &opts)) {
2024 vga_interface_type = VGA_XENFB;
2025 } else if (strstart(p, "qxl", &opts)) {
2026 if (qxl_vga_available()) {
2027 vga_interface_type = VGA_QXL;
2028 } else {
2029 fprintf(stderr, "Error: QXL VGA not available\n");
2030 exit(0);
2032 } else if (strstart(p, "tcx", &opts)) {
2033 if (tcx_vga_available()) {
2034 vga_interface_type = VGA_TCX;
2035 } else {
2036 fprintf(stderr, "Error: TCX framebuffer not available\n");
2037 exit(0);
2039 } else if (strstart(p, "cg3", &opts)) {
2040 if (cg3_vga_available()) {
2041 vga_interface_type = VGA_CG3;
2042 } else {
2043 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2044 exit(0);
2046 } else if (!strstart(p, "none", &opts)) {
2047 invalid_vga:
2048 fprintf(stderr, "Unknown vga type: %s\n", p);
2049 exit(1);
2051 while (*opts) {
2052 const char *nextopt;
2054 if (strstart(opts, ",retrace=", &nextopt)) {
2055 opts = nextopt;
2056 if (strstart(opts, "dumb", &nextopt))
2057 vga_retrace_method = VGA_RETRACE_DUMB;
2058 else if (strstart(opts, "precise", &nextopt))
2059 vga_retrace_method = VGA_RETRACE_PRECISE;
2060 else goto invalid_vga;
2061 } else goto invalid_vga;
2062 opts = nextopt;
2066 static DisplayType select_display(const char *p)
2068 const char *opts;
2069 DisplayType display = DT_DEFAULT;
2071 if (strstart(p, "sdl", &opts)) {
2072 #ifdef CONFIG_SDL
2073 display = DT_SDL;
2074 while (*opts) {
2075 const char *nextopt;
2077 if (strstart(opts, ",frame=", &nextopt)) {
2078 opts = nextopt;
2079 if (strstart(opts, "on", &nextopt)) {
2080 no_frame = 0;
2081 } else if (strstart(opts, "off", &nextopt)) {
2082 no_frame = 1;
2083 } else {
2084 goto invalid_sdl_args;
2086 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2087 opts = nextopt;
2088 if (strstart(opts, "on", &nextopt)) {
2089 alt_grab = 1;
2090 } else if (strstart(opts, "off", &nextopt)) {
2091 alt_grab = 0;
2092 } else {
2093 goto invalid_sdl_args;
2095 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2096 opts = nextopt;
2097 if (strstart(opts, "on", &nextopt)) {
2098 ctrl_grab = 1;
2099 } else if (strstart(opts, "off", &nextopt)) {
2100 ctrl_grab = 0;
2101 } else {
2102 goto invalid_sdl_args;
2104 } else if (strstart(opts, ",window_close=", &nextopt)) {
2105 opts = nextopt;
2106 if (strstart(opts, "on", &nextopt)) {
2107 no_quit = 0;
2108 } else if (strstart(opts, "off", &nextopt)) {
2109 no_quit = 1;
2110 } else {
2111 goto invalid_sdl_args;
2113 } else {
2114 invalid_sdl_args:
2115 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2116 exit(1);
2118 opts = nextopt;
2120 #else
2121 fprintf(stderr, "SDL support is disabled\n");
2122 exit(1);
2123 #endif
2124 } else if (strstart(p, "vnc", &opts)) {
2125 #ifdef CONFIG_VNC
2126 display_remote++;
2128 if (*opts) {
2129 const char *nextopt;
2131 if (strstart(opts, "=", &nextopt)) {
2132 vnc_display = nextopt;
2135 if (!vnc_display) {
2136 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2137 exit(1);
2139 #else
2140 fprintf(stderr, "VNC support is disabled\n");
2141 exit(1);
2142 #endif
2143 } else if (strstart(p, "curses", &opts)) {
2144 #ifdef CONFIG_CURSES
2145 display = DT_CURSES;
2146 #else
2147 fprintf(stderr, "Curses support is disabled\n");
2148 exit(1);
2149 #endif
2150 } else if (strstart(p, "gtk", &opts)) {
2151 #ifdef CONFIG_GTK
2152 display = DT_GTK;
2153 while (*opts) {
2154 const char *nextopt;
2156 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2157 opts = nextopt;
2158 if (strstart(opts, "on", &nextopt)) {
2159 grab_on_hover = true;
2160 } else if (strstart(opts, "off", &nextopt)) {
2161 grab_on_hover = false;
2162 } else {
2163 goto invalid_gtk_args;
2165 } else {
2166 invalid_gtk_args:
2167 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2168 exit(1);
2170 opts = nextopt;
2172 #else
2173 fprintf(stderr, "GTK support is disabled\n");
2174 exit(1);
2175 #endif
2176 } else if (strstart(p, "none", &opts)) {
2177 display = DT_NONE;
2178 } else {
2179 fprintf(stderr, "Unknown display type: %s\n", p);
2180 exit(1);
2183 return display;
2186 static int balloon_parse(const char *arg)
2188 QemuOpts *opts;
2190 if (strcmp(arg, "none") == 0) {
2191 return 0;
2194 if (!strncmp(arg, "virtio", 6)) {
2195 if (arg[6] == ',') {
2196 /* have params -> parse them */
2197 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2198 if (!opts)
2199 return -1;
2200 } else {
2201 /* create empty opts */
2202 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2203 &error_abort);
2205 qemu_opt_set(opts, "driver", "virtio-balloon");
2206 return 0;
2209 return -1;
2212 char *qemu_find_file(int type, const char *name)
2214 int i;
2215 const char *subdir;
2216 char *buf;
2218 /* Try the name as a straight path first */
2219 if (access(name, R_OK) == 0) {
2220 trace_load_file(name, name);
2221 return g_strdup(name);
2224 switch (type) {
2225 case QEMU_FILE_TYPE_BIOS:
2226 subdir = "";
2227 break;
2228 case QEMU_FILE_TYPE_KEYMAP:
2229 subdir = "keymaps/";
2230 break;
2231 default:
2232 abort();
2235 for (i = 0; i < data_dir_idx; i++) {
2236 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2237 if (access(buf, R_OK) == 0) {
2238 trace_load_file(name, buf);
2239 return buf;
2241 g_free(buf);
2243 return NULL;
2246 static int device_help_func(QemuOpts *opts, void *opaque)
2248 return qdev_device_help(opts);
2251 static int device_init_func(QemuOpts *opts, void *opaque)
2253 DeviceState *dev;
2255 dev = qdev_device_add(opts);
2256 if (!dev)
2257 return -1;
2258 object_unref(OBJECT(dev));
2259 return 0;
2262 static int chardev_init_func(QemuOpts *opts, void *opaque)
2264 Error *local_err = NULL;
2266 qemu_chr_new_from_opts(opts, NULL, &local_err);
2267 if (local_err) {
2268 error_report("%s", error_get_pretty(local_err));
2269 error_free(local_err);
2270 return -1;
2272 return 0;
2275 #ifdef CONFIG_VIRTFS
2276 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2278 int ret;
2279 ret = qemu_fsdev_add(opts);
2281 return ret;
2283 #endif
2285 static int mon_init_func(QemuOpts *opts, void *opaque)
2287 CharDriverState *chr;
2288 const char *chardev;
2289 const char *mode;
2290 int flags;
2292 mode = qemu_opt_get(opts, "mode");
2293 if (mode == NULL) {
2294 mode = "readline";
2296 if (strcmp(mode, "readline") == 0) {
2297 flags = MONITOR_USE_READLINE;
2298 } else if (strcmp(mode, "control") == 0) {
2299 flags = MONITOR_USE_CONTROL;
2300 } else {
2301 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2302 exit(1);
2305 if (qemu_opt_get_bool(opts, "pretty", 0))
2306 flags |= MONITOR_USE_PRETTY;
2308 if (qemu_opt_get_bool(opts, "default", 0))
2309 flags |= MONITOR_IS_DEFAULT;
2311 chardev = qemu_opt_get(opts, "chardev");
2312 chr = qemu_chr_find(chardev);
2313 if (chr == NULL) {
2314 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2315 exit(1);
2318 qemu_chr_fe_claim_no_fail(chr);
2319 monitor_init(chr, flags);
2320 return 0;
2323 static void monitor_parse(const char *optarg, const char *mode)
2325 static int monitor_device_index = 0;
2326 QemuOpts *opts;
2327 const char *p;
2328 char label[32];
2329 int def = 0;
2331 if (strstart(optarg, "chardev:", &p)) {
2332 snprintf(label, sizeof(label), "%s", p);
2333 } else {
2334 snprintf(label, sizeof(label), "compat_monitor%d",
2335 monitor_device_index);
2336 if (monitor_device_index == 0) {
2337 def = 1;
2339 opts = qemu_chr_parse_compat(label, optarg);
2340 if (!opts) {
2341 fprintf(stderr, "parse error: %s\n", optarg);
2342 exit(1);
2346 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2347 if (!opts) {
2348 fprintf(stderr, "duplicate chardev: %s\n", label);
2349 exit(1);
2351 qemu_opt_set(opts, "mode", mode);
2352 qemu_opt_set(opts, "chardev", label);
2353 if (def)
2354 qemu_opt_set(opts, "default", "on");
2355 monitor_device_index++;
2358 struct device_config {
2359 enum {
2360 DEV_USB, /* -usbdevice */
2361 DEV_BT, /* -bt */
2362 DEV_SERIAL, /* -serial */
2363 DEV_PARALLEL, /* -parallel */
2364 DEV_VIRTCON, /* -virtioconsole */
2365 DEV_DEBUGCON, /* -debugcon */
2366 DEV_GDB, /* -gdb, -s */
2367 DEV_SCLP, /* s390 sclp */
2368 } type;
2369 const char *cmdline;
2370 Location loc;
2371 QTAILQ_ENTRY(device_config) next;
2374 static QTAILQ_HEAD(, device_config) device_configs =
2375 QTAILQ_HEAD_INITIALIZER(device_configs);
2377 static void add_device_config(int type, const char *cmdline)
2379 struct device_config *conf;
2381 conf = g_malloc0(sizeof(*conf));
2382 conf->type = type;
2383 conf->cmdline = cmdline;
2384 loc_save(&conf->loc);
2385 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2388 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2390 struct device_config *conf;
2391 int rc;
2393 QTAILQ_FOREACH(conf, &device_configs, next) {
2394 if (conf->type != type)
2395 continue;
2396 loc_push_restore(&conf->loc);
2397 rc = func(conf->cmdline);
2398 loc_pop(&conf->loc);
2399 if (rc) {
2400 return rc;
2403 return 0;
2406 static int serial_parse(const char *devname)
2408 static int index = 0;
2409 char label[32];
2411 if (strcmp(devname, "none") == 0)
2412 return 0;
2413 if (index == MAX_SERIAL_PORTS) {
2414 fprintf(stderr, "qemu: too many serial ports\n");
2415 exit(1);
2417 snprintf(label, sizeof(label), "serial%d", index);
2418 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2419 if (!serial_hds[index]) {
2420 fprintf(stderr, "qemu: could not connect serial device"
2421 " to character backend '%s'\n", devname);
2422 return -1;
2424 index++;
2425 return 0;
2428 static int parallel_parse(const char *devname)
2430 static int index = 0;
2431 char label[32];
2433 if (strcmp(devname, "none") == 0)
2434 return 0;
2435 if (index == MAX_PARALLEL_PORTS) {
2436 fprintf(stderr, "qemu: too many parallel ports\n");
2437 exit(1);
2439 snprintf(label, sizeof(label), "parallel%d", index);
2440 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2441 if (!parallel_hds[index]) {
2442 fprintf(stderr, "qemu: could not connect parallel device"
2443 " to character backend '%s'\n", devname);
2444 return -1;
2446 index++;
2447 return 0;
2450 static int virtcon_parse(const char *devname)
2452 QemuOptsList *device = qemu_find_opts("device");
2453 static int index = 0;
2454 char label[32];
2455 QemuOpts *bus_opts, *dev_opts;
2457 if (strcmp(devname, "none") == 0)
2458 return 0;
2459 if (index == MAX_VIRTIO_CONSOLES) {
2460 fprintf(stderr, "qemu: too many virtio consoles\n");
2461 exit(1);
2464 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2465 if (arch_type == QEMU_ARCH_S390X) {
2466 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2467 } else {
2468 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2471 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2472 qemu_opt_set(dev_opts, "driver", "virtconsole");
2474 snprintf(label, sizeof(label), "virtcon%d", index);
2475 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2476 if (!virtcon_hds[index]) {
2477 fprintf(stderr, "qemu: could not connect virtio console"
2478 " to character backend '%s'\n", devname);
2479 return -1;
2481 qemu_opt_set(dev_opts, "chardev", label);
2483 index++;
2484 return 0;
2487 static int sclp_parse(const char *devname)
2489 QemuOptsList *device = qemu_find_opts("device");
2490 static int index = 0;
2491 char label[32];
2492 QemuOpts *dev_opts;
2494 if (strcmp(devname, "none") == 0) {
2495 return 0;
2497 if (index == MAX_SCLP_CONSOLES) {
2498 fprintf(stderr, "qemu: too many sclp consoles\n");
2499 exit(1);
2502 assert(arch_type == QEMU_ARCH_S390X);
2504 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2505 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2507 snprintf(label, sizeof(label), "sclpcon%d", index);
2508 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2509 if (!sclp_hds[index]) {
2510 fprintf(stderr, "qemu: could not connect sclp console"
2511 " to character backend '%s'\n", devname);
2512 return -1;
2514 qemu_opt_set(dev_opts, "chardev", label);
2516 index++;
2517 return 0;
2520 static int debugcon_parse(const char *devname)
2522 QemuOpts *opts;
2524 if (!qemu_chr_new("debugcon", devname, NULL)) {
2525 exit(1);
2527 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2528 if (!opts) {
2529 fprintf(stderr, "qemu: already have a debugcon device\n");
2530 exit(1);
2532 qemu_opt_set(opts, "driver", "isa-debugcon");
2533 qemu_opt_set(opts, "chardev", "debugcon");
2534 return 0;
2537 static MachineClass *machine_parse(const char *name)
2539 MachineClass *mc = NULL;
2540 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2542 if (name) {
2543 mc = find_machine(name);
2545 if (mc) {
2546 return mc;
2548 if (name && !is_help_option(name)) {
2549 error_report("Unsupported machine type");
2550 error_printf("Use -machine help to list supported machines!\n");
2551 } else {
2552 printf("Supported machines are:\n");
2553 for (el = machines; el; el = el->next) {
2554 MachineClass *mc = el->data;
2555 if (mc->alias) {
2556 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2558 printf("%-20s %s%s\n", mc->name, mc->desc,
2559 mc->is_default ? " (default)" : "");
2563 g_slist_free(machines);
2564 exit(!name || !is_help_option(name));
2567 void qemu_add_exit_notifier(Notifier *notify)
2569 notifier_list_add(&exit_notifiers, notify);
2572 void qemu_remove_exit_notifier(Notifier *notify)
2574 notifier_remove(notify);
2577 static void qemu_run_exit_notifiers(void)
2579 notifier_list_notify(&exit_notifiers, NULL);
2582 void qemu_add_machine_init_done_notifier(Notifier *notify)
2584 notifier_list_add(&machine_init_done_notifiers, notify);
2587 static void qemu_run_machine_init_done_notifiers(void)
2589 notifier_list_notify(&machine_init_done_notifiers, NULL);
2592 static const QEMUOption *lookup_opt(int argc, char **argv,
2593 const char **poptarg, int *poptind)
2595 const QEMUOption *popt;
2596 int optind = *poptind;
2597 char *r = argv[optind];
2598 const char *optarg;
2600 loc_set_cmdline(argv, optind, 1);
2601 optind++;
2602 /* Treat --foo the same as -foo. */
2603 if (r[1] == '-')
2604 r++;
2605 popt = qemu_options;
2606 for(;;) {
2607 if (!popt->name) {
2608 error_report("invalid option");
2609 exit(1);
2611 if (!strcmp(popt->name, r + 1))
2612 break;
2613 popt++;
2615 if (popt->flags & HAS_ARG) {
2616 if (optind >= argc) {
2617 error_report("requires an argument");
2618 exit(1);
2620 optarg = argv[optind++];
2621 loc_set_cmdline(argv, optind - 2, 2);
2622 } else {
2623 optarg = NULL;
2626 *poptarg = optarg;
2627 *poptind = optind;
2629 return popt;
2632 static gpointer malloc_and_trace(gsize n_bytes)
2634 void *ptr = malloc(n_bytes);
2635 trace_g_malloc(n_bytes, ptr);
2636 return ptr;
2639 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2641 void *ptr = realloc(mem, n_bytes);
2642 trace_g_realloc(mem, n_bytes, ptr);
2643 return ptr;
2646 static void free_and_trace(gpointer mem)
2648 trace_g_free(mem);
2649 free(mem);
2652 static int machine_set_property(const char *name, const char *value,
2653 void *opaque)
2655 Object *obj = OBJECT(opaque);
2656 StringInputVisitor *siv;
2657 Error *local_err = NULL;
2658 char *c, *qom_name;
2660 if (strcmp(name, "type") == 0) {
2661 return 0;
2664 qom_name = g_strdup(name);
2665 c = qom_name;
2666 while (*c++) {
2667 if (*c == '_') {
2668 *c = '-';
2672 siv = string_input_visitor_new(value);
2673 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2674 string_input_visitor_cleanup(siv);
2675 g_free(qom_name);
2677 if (local_err) {
2678 qerror_report_err(local_err);
2679 error_free(local_err);
2680 return -1;
2683 return 0;
2686 static int object_create(QemuOpts *opts, void *opaque)
2688 Error *err = NULL;
2689 char *type = NULL;
2690 char *id = NULL;
2691 void *dummy = NULL;
2692 OptsVisitor *ov;
2693 QDict *pdict;
2695 ov = opts_visitor_new(opts);
2696 pdict = qemu_opts_to_qdict(opts, NULL);
2698 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2699 if (err) {
2700 goto out;
2703 qdict_del(pdict, "qom-type");
2704 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2705 if (err) {
2706 goto out;
2709 qdict_del(pdict, "id");
2710 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2711 if (err) {
2712 goto out;
2715 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2716 if (err) {
2717 goto out;
2719 visit_end_struct(opts_get_visitor(ov), &err);
2720 if (err) {
2721 qmp_object_del(id, NULL);
2724 out:
2725 opts_visitor_cleanup(ov);
2727 QDECREF(pdict);
2728 g_free(id);
2729 g_free(type);
2730 g_free(dummy);
2731 if (err) {
2732 qerror_report_err(err);
2733 error_free(err);
2734 return -1;
2736 return 0;
2739 int main(int argc, char **argv, char **envp)
2741 int i;
2742 int snapshot, linux_boot;
2743 const char *initrd_filename;
2744 const char *kernel_filename, *kernel_cmdline;
2745 const char *boot_order;
2746 DisplayState *ds;
2747 int cyls, heads, secs, translation;
2748 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2749 QemuOptsList *olist;
2750 int optind;
2751 const char *optarg;
2752 const char *loadvm = NULL;
2753 MachineClass *machine_class;
2754 const char *cpu_model;
2755 const char *vga_model = NULL;
2756 const char *qtest_chrdev = NULL;
2757 const char *qtest_log = NULL;
2758 const char *pid_file = NULL;
2759 const char *incoming = NULL;
2760 #ifdef CONFIG_VNC
2761 int show_vnc_port = 0;
2762 #endif
2763 bool defconfig = true;
2764 bool userconfig = true;
2765 const char *log_mask = NULL;
2766 const char *log_file = NULL;
2767 GMemVTable mem_trace = {
2768 .malloc = malloc_and_trace,
2769 .realloc = realloc_and_trace,
2770 .free = free_and_trace,
2772 const char *trace_events = NULL;
2773 const char *trace_file = NULL;
2774 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2775 1024 * 1024;
2776 ram_addr_t maxram_size = default_ram_size;
2777 uint64_t ram_slots = 0;
2778 FILE *vmstate_dump_file = NULL;
2779 Error *main_loop_err = NULL;
2781 atexit(qemu_run_exit_notifiers);
2782 error_set_progname(argv[0]);
2783 qemu_init_exec_dir(argv[0]);
2785 g_mem_set_vtable(&mem_trace);
2787 module_call_init(MODULE_INIT_QOM);
2789 qemu_add_opts(&qemu_drive_opts);
2790 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2791 qemu_add_drive_opts(&qemu_common_drive_opts);
2792 qemu_add_drive_opts(&qemu_drive_opts);
2793 qemu_add_opts(&qemu_chardev_opts);
2794 qemu_add_opts(&qemu_device_opts);
2795 qemu_add_opts(&qemu_netdev_opts);
2796 qemu_add_opts(&qemu_net_opts);
2797 qemu_add_opts(&qemu_rtc_opts);
2798 qemu_add_opts(&qemu_global_opts);
2799 qemu_add_opts(&qemu_mon_opts);
2800 qemu_add_opts(&qemu_trace_opts);
2801 qemu_add_opts(&qemu_option_rom_opts);
2802 qemu_add_opts(&qemu_machine_opts);
2803 qemu_add_opts(&qemu_mem_opts);
2804 qemu_add_opts(&qemu_smp_opts);
2805 qemu_add_opts(&qemu_boot_opts);
2806 qemu_add_opts(&qemu_sandbox_opts);
2807 qemu_add_opts(&qemu_add_fd_opts);
2808 qemu_add_opts(&qemu_object_opts);
2809 qemu_add_opts(&qemu_tpmdev_opts);
2810 qemu_add_opts(&qemu_realtime_opts);
2811 qemu_add_opts(&qemu_msg_opts);
2812 qemu_add_opts(&qemu_name_opts);
2813 qemu_add_opts(&qemu_numa_opts);
2814 qemu_add_opts(&qemu_icount_opts);
2816 runstate_init();
2818 rtc_clock = QEMU_CLOCK_HOST;
2820 QLIST_INIT (&vm_change_state_head);
2821 os_setup_early_signal_handling();
2823 module_call_init(MODULE_INIT_MACHINE);
2824 machine_class = find_default_machine();
2825 cpu_model = NULL;
2826 ram_size = default_ram_size;
2827 snapshot = 0;
2828 cyls = heads = secs = 0;
2829 translation = BIOS_ATA_TRANSLATION_AUTO;
2831 for (i = 0; i < MAX_NODES; i++) {
2832 numa_info[i].node_mem = 0;
2833 numa_info[i].present = false;
2834 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2837 nb_numa_nodes = 0;
2838 max_numa_nodeid = 0;
2839 nb_nics = 0;
2841 bdrv_init_with_whitelist();
2843 autostart = 1;
2845 /* first pass of option parsing */
2846 optind = 1;
2847 while (optind < argc) {
2848 if (argv[optind][0] != '-') {
2849 /* disk image */
2850 optind++;
2851 } else {
2852 const QEMUOption *popt;
2854 popt = lookup_opt(argc, argv, &optarg, &optind);
2855 switch (popt->index) {
2856 case QEMU_OPTION_nodefconfig:
2857 defconfig = false;
2858 break;
2859 case QEMU_OPTION_nouserconfig:
2860 userconfig = false;
2861 break;
2866 if (defconfig) {
2867 int ret;
2868 ret = qemu_read_default_config_files(userconfig);
2869 if (ret < 0) {
2870 exit(1);
2874 /* second pass of option parsing */
2875 optind = 1;
2876 for(;;) {
2877 if (optind >= argc)
2878 break;
2879 if (argv[optind][0] != '-') {
2880 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2881 } else {
2882 const QEMUOption *popt;
2884 popt = lookup_opt(argc, argv, &optarg, &optind);
2885 if (!(popt->arch_mask & arch_type)) {
2886 printf("Option %s not supported for this target\n", popt->name);
2887 exit(1);
2889 switch(popt->index) {
2890 case QEMU_OPTION_M:
2891 machine_class = machine_parse(optarg);
2892 break;
2893 case QEMU_OPTION_no_kvm_irqchip: {
2894 olist = qemu_find_opts("machine");
2895 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2896 break;
2898 case QEMU_OPTION_cpu:
2899 /* hw initialization will check this */
2900 cpu_model = optarg;
2901 break;
2902 case QEMU_OPTION_hda:
2904 char buf[256];
2905 if (cyls == 0)
2906 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2907 else
2908 snprintf(buf, sizeof(buf),
2909 "%s,cyls=%d,heads=%d,secs=%d%s",
2910 HD_OPTS , cyls, heads, secs,
2911 translation == BIOS_ATA_TRANSLATION_LBA ?
2912 ",trans=lba" :
2913 translation == BIOS_ATA_TRANSLATION_NONE ?
2914 ",trans=none" : "");
2915 drive_add(IF_DEFAULT, 0, optarg, buf);
2916 break;
2918 case QEMU_OPTION_hdb:
2919 case QEMU_OPTION_hdc:
2920 case QEMU_OPTION_hdd:
2921 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2922 HD_OPTS);
2923 break;
2924 case QEMU_OPTION_drive:
2925 if (drive_def(optarg) == NULL) {
2926 exit(1);
2928 break;
2929 case QEMU_OPTION_set:
2930 if (qemu_set_option(optarg) != 0)
2931 exit(1);
2932 break;
2933 case QEMU_OPTION_global:
2934 if (qemu_global_option(optarg) != 0)
2935 exit(1);
2936 break;
2937 case QEMU_OPTION_mtdblock:
2938 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2939 break;
2940 case QEMU_OPTION_sd:
2941 drive_add(IF_SD, -1, optarg, SD_OPTS);
2942 break;
2943 case QEMU_OPTION_pflash:
2944 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2945 break;
2946 case QEMU_OPTION_snapshot:
2947 snapshot = 1;
2948 break;
2949 case QEMU_OPTION_hdachs:
2951 const char *p;
2952 p = optarg;
2953 cyls = strtol(p, (char **)&p, 0);
2954 if (cyls < 1 || cyls > 16383)
2955 goto chs_fail;
2956 if (*p != ',')
2957 goto chs_fail;
2958 p++;
2959 heads = strtol(p, (char **)&p, 0);
2960 if (heads < 1 || heads > 16)
2961 goto chs_fail;
2962 if (*p != ',')
2963 goto chs_fail;
2964 p++;
2965 secs = strtol(p, (char **)&p, 0);
2966 if (secs < 1 || secs > 63)
2967 goto chs_fail;
2968 if (*p == ',') {
2969 p++;
2970 if (!strcmp(p, "large")) {
2971 translation = BIOS_ATA_TRANSLATION_LARGE;
2972 } else if (!strcmp(p, "rechs")) {
2973 translation = BIOS_ATA_TRANSLATION_RECHS;
2974 } else if (!strcmp(p, "none")) {
2975 translation = BIOS_ATA_TRANSLATION_NONE;
2976 } else if (!strcmp(p, "lba")) {
2977 translation = BIOS_ATA_TRANSLATION_LBA;
2978 } else if (!strcmp(p, "auto")) {
2979 translation = BIOS_ATA_TRANSLATION_AUTO;
2980 } else {
2981 goto chs_fail;
2983 } else if (*p != '\0') {
2984 chs_fail:
2985 fprintf(stderr, "qemu: invalid physical CHS format\n");
2986 exit(1);
2988 if (hda_opts != NULL) {
2989 char num[16];
2990 snprintf(num, sizeof(num), "%d", cyls);
2991 qemu_opt_set(hda_opts, "cyls", num);
2992 snprintf(num, sizeof(num), "%d", heads);
2993 qemu_opt_set(hda_opts, "heads", num);
2994 snprintf(num, sizeof(num), "%d", secs);
2995 qemu_opt_set(hda_opts, "secs", num);
2996 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2997 qemu_opt_set(hda_opts, "trans", "large");
2998 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2999 qemu_opt_set(hda_opts, "trans", "rechs");
3000 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3001 qemu_opt_set(hda_opts, "trans", "lba");
3002 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3003 qemu_opt_set(hda_opts, "trans", "none");
3007 break;
3008 case QEMU_OPTION_numa:
3009 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3010 if (!opts) {
3011 exit(1);
3013 break;
3014 case QEMU_OPTION_display:
3015 display_type = select_display(optarg);
3016 break;
3017 case QEMU_OPTION_nographic:
3018 display_type = DT_NOGRAPHIC;
3019 break;
3020 case QEMU_OPTION_curses:
3021 #ifdef CONFIG_CURSES
3022 display_type = DT_CURSES;
3023 #else
3024 fprintf(stderr, "Curses support is disabled\n");
3025 exit(1);
3026 #endif
3027 break;
3028 case QEMU_OPTION_portrait:
3029 graphic_rotate = 90;
3030 break;
3031 case QEMU_OPTION_rotate:
3032 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3033 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3034 graphic_rotate != 180 && graphic_rotate != 270) {
3035 fprintf(stderr,
3036 "qemu: only 90, 180, 270 deg rotation is available\n");
3037 exit(1);
3039 break;
3040 case QEMU_OPTION_kernel:
3041 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3042 break;
3043 case QEMU_OPTION_initrd:
3044 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3045 break;
3046 case QEMU_OPTION_append:
3047 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3048 break;
3049 case QEMU_OPTION_dtb:
3050 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3051 break;
3052 case QEMU_OPTION_cdrom:
3053 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3054 break;
3055 case QEMU_OPTION_boot:
3056 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3057 if (!opts) {
3058 exit(1);
3060 break;
3061 case QEMU_OPTION_fda:
3062 case QEMU_OPTION_fdb:
3063 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3064 optarg, FD_OPTS);
3065 break;
3066 case QEMU_OPTION_no_fd_bootchk:
3067 fd_bootchk = 0;
3068 break;
3069 case QEMU_OPTION_netdev:
3070 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3071 exit(1);
3073 break;
3074 case QEMU_OPTION_net:
3075 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3076 exit(1);
3078 break;
3079 #ifdef CONFIG_LIBISCSI
3080 case QEMU_OPTION_iscsi:
3081 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3082 if (!opts) {
3083 exit(1);
3085 break;
3086 #endif
3087 #ifdef CONFIG_SLIRP
3088 case QEMU_OPTION_tftp:
3089 legacy_tftp_prefix = optarg;
3090 break;
3091 case QEMU_OPTION_bootp:
3092 legacy_bootp_filename = optarg;
3093 break;
3094 case QEMU_OPTION_redir:
3095 if (net_slirp_redir(optarg) < 0)
3096 exit(1);
3097 break;
3098 #endif
3099 case QEMU_OPTION_bt:
3100 add_device_config(DEV_BT, optarg);
3101 break;
3102 case QEMU_OPTION_audio_help:
3103 AUD_help ();
3104 exit (0);
3105 break;
3106 case QEMU_OPTION_soundhw:
3107 select_soundhw (optarg);
3108 break;
3109 case QEMU_OPTION_h:
3110 help(0);
3111 break;
3112 case QEMU_OPTION_version:
3113 version();
3114 exit(0);
3115 break;
3116 case QEMU_OPTION_m: {
3117 uint64_t sz;
3118 const char *mem_str;
3119 const char *maxmem_str, *slots_str;
3121 opts = qemu_opts_parse(qemu_find_opts("memory"),
3122 optarg, 1);
3123 if (!opts) {
3124 exit(EXIT_FAILURE);
3127 mem_str = qemu_opt_get(opts, "size");
3128 if (!mem_str) {
3129 error_report("invalid -m option, missing 'size' option");
3130 exit(EXIT_FAILURE);
3132 if (!*mem_str) {
3133 error_report("missing 'size' option value");
3134 exit(EXIT_FAILURE);
3137 sz = qemu_opt_get_size(opts, "size", ram_size);
3139 /* Fix up legacy suffix-less format */
3140 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3141 uint64_t overflow_check = sz;
3143 sz <<= 20;
3144 if ((sz >> 20) != overflow_check) {
3145 error_report("too large 'size' option value");
3146 exit(EXIT_FAILURE);
3150 /* backward compatibility behaviour for case "-m 0" */
3151 if (sz == 0) {
3152 sz = default_ram_size;
3155 sz = QEMU_ALIGN_UP(sz, 8192);
3156 ram_size = sz;
3157 if (ram_size != sz) {
3158 error_report("ram size too large");
3159 exit(EXIT_FAILURE);
3161 maxram_size = ram_size;
3163 maxmem_str = qemu_opt_get(opts, "maxmem");
3164 slots_str = qemu_opt_get(opts, "slots");
3165 if (maxmem_str && slots_str) {
3166 uint64_t slots;
3168 sz = qemu_opt_get_size(opts, "maxmem", 0);
3169 if (sz < ram_size) {
3170 error_report("invalid -m option value: maxmem "
3171 "(0x%" PRIx64 ") <= initial memory (0x"
3172 RAM_ADDR_FMT ")", sz, ram_size);
3173 exit(EXIT_FAILURE);
3176 slots = qemu_opt_get_number(opts, "slots", 0);
3177 if ((sz > ram_size) && !slots) {
3178 error_report("invalid -m option value: maxmem "
3179 "(0x%" PRIx64 ") more than initial memory (0x"
3180 RAM_ADDR_FMT ") but no hotplug slots where "
3181 "specified", sz, ram_size);
3182 exit(EXIT_FAILURE);
3185 if ((sz <= ram_size) && slots) {
3186 error_report("invalid -m option value: %"
3187 PRIu64 " hotplug slots where specified but "
3188 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3189 RAM_ADDR_FMT ")", slots, sz, ram_size);
3190 exit(EXIT_FAILURE);
3192 maxram_size = sz;
3193 ram_slots = slots;
3194 } else if ((!maxmem_str && slots_str) ||
3195 (maxmem_str && !slots_str)) {
3196 error_report("invalid -m option value: missing "
3197 "'%s' option", slots_str ? "maxmem" : "slots");
3198 exit(EXIT_FAILURE);
3200 break;
3202 #ifdef CONFIG_TPM
3203 case QEMU_OPTION_tpmdev:
3204 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3205 exit(1);
3207 break;
3208 #endif
3209 case QEMU_OPTION_mempath:
3210 mem_path = optarg;
3211 break;
3212 case QEMU_OPTION_mem_prealloc:
3213 mem_prealloc = 1;
3214 break;
3215 case QEMU_OPTION_d:
3216 log_mask = optarg;
3217 break;
3218 case QEMU_OPTION_D:
3219 log_file = optarg;
3220 break;
3221 case QEMU_OPTION_s:
3222 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3223 break;
3224 case QEMU_OPTION_gdb:
3225 add_device_config(DEV_GDB, optarg);
3226 break;
3227 case QEMU_OPTION_L:
3228 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3229 data_dir[data_dir_idx++] = optarg;
3231 break;
3232 case QEMU_OPTION_bios:
3233 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3234 break;
3235 case QEMU_OPTION_singlestep:
3236 singlestep = 1;
3237 break;
3238 case QEMU_OPTION_S:
3239 autostart = 0;
3240 break;
3241 case QEMU_OPTION_k:
3242 keyboard_layout = optarg;
3243 break;
3244 case QEMU_OPTION_localtime:
3245 rtc_utc = 0;
3246 break;
3247 case QEMU_OPTION_vga:
3248 vga_model = optarg;
3249 default_vga = 0;
3250 break;
3251 case QEMU_OPTION_g:
3253 const char *p;
3254 int w, h, depth;
3255 p = optarg;
3256 w = strtol(p, (char **)&p, 10);
3257 if (w <= 0) {
3258 graphic_error:
3259 fprintf(stderr, "qemu: invalid resolution or depth\n");
3260 exit(1);
3262 if (*p != 'x')
3263 goto graphic_error;
3264 p++;
3265 h = strtol(p, (char **)&p, 10);
3266 if (h <= 0)
3267 goto graphic_error;
3268 if (*p == 'x') {
3269 p++;
3270 depth = strtol(p, (char **)&p, 10);
3271 if (depth != 8 && depth != 15 && depth != 16 &&
3272 depth != 24 && depth != 32)
3273 goto graphic_error;
3274 } else if (*p == '\0') {
3275 depth = graphic_depth;
3276 } else {
3277 goto graphic_error;
3280 graphic_width = w;
3281 graphic_height = h;
3282 graphic_depth = depth;
3284 break;
3285 case QEMU_OPTION_echr:
3287 char *r;
3288 term_escape_char = strtol(optarg, &r, 0);
3289 if (r == optarg)
3290 printf("Bad argument to echr\n");
3291 break;
3293 case QEMU_OPTION_monitor:
3294 default_monitor = 0;
3295 if (strncmp(optarg, "none", 4)) {
3296 monitor_parse(optarg, "readline");
3298 break;
3299 case QEMU_OPTION_qmp:
3300 monitor_parse(optarg, "control");
3301 default_monitor = 0;
3302 break;
3303 case QEMU_OPTION_mon:
3304 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3305 if (!opts) {
3306 exit(1);
3308 default_monitor = 0;
3309 break;
3310 case QEMU_OPTION_chardev:
3311 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3312 if (!opts) {
3313 exit(1);
3315 break;
3316 case QEMU_OPTION_fsdev:
3317 olist = qemu_find_opts("fsdev");
3318 if (!olist) {
3319 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3320 exit(1);
3322 opts = qemu_opts_parse(olist, optarg, 1);
3323 if (!opts) {
3324 exit(1);
3326 break;
3327 case QEMU_OPTION_virtfs: {
3328 QemuOpts *fsdev;
3329 QemuOpts *device;
3330 const char *writeout, *sock_fd, *socket;
3332 olist = qemu_find_opts("virtfs");
3333 if (!olist) {
3334 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3335 exit(1);
3337 opts = qemu_opts_parse(olist, optarg, 1);
3338 if (!opts) {
3339 exit(1);
3342 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3343 qemu_opt_get(opts, "mount_tag") == NULL) {
3344 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3345 exit(1);
3347 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3348 qemu_opt_get(opts, "mount_tag"),
3349 1, NULL);
3350 if (!fsdev) {
3351 fprintf(stderr, "duplicate fsdev id: %s\n",
3352 qemu_opt_get(opts, "mount_tag"));
3353 exit(1);
3356 writeout = qemu_opt_get(opts, "writeout");
3357 if (writeout) {
3358 #ifdef CONFIG_SYNC_FILE_RANGE
3359 qemu_opt_set(fsdev, "writeout", writeout);
3360 #else
3361 fprintf(stderr, "writeout=immediate not supported on "
3362 "this platform\n");
3363 exit(1);
3364 #endif
3366 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3367 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3368 qemu_opt_set(fsdev, "security_model",
3369 qemu_opt_get(opts, "security_model"));
3370 socket = qemu_opt_get(opts, "socket");
3371 if (socket) {
3372 qemu_opt_set(fsdev, "socket", socket);
3374 sock_fd = qemu_opt_get(opts, "sock_fd");
3375 if (sock_fd) {
3376 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3379 qemu_opt_set_bool(fsdev, "readonly",
3380 qemu_opt_get_bool(opts, "readonly", 0));
3381 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3382 &error_abort);
3383 qemu_opt_set(device, "driver", "virtio-9p-pci");
3384 qemu_opt_set(device, "fsdev",
3385 qemu_opt_get(opts, "mount_tag"));
3386 qemu_opt_set(device, "mount_tag",
3387 qemu_opt_get(opts, "mount_tag"));
3388 break;
3390 case QEMU_OPTION_virtfs_synth: {
3391 QemuOpts *fsdev;
3392 QemuOpts *device;
3394 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3395 1, NULL);
3396 if (!fsdev) {
3397 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3398 exit(1);
3400 qemu_opt_set(fsdev, "fsdriver", "synth");
3402 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3403 &error_abort);
3404 qemu_opt_set(device, "driver", "virtio-9p-pci");
3405 qemu_opt_set(device, "fsdev", "v_synth");
3406 qemu_opt_set(device, "mount_tag", "v_synth");
3407 break;
3409 case QEMU_OPTION_serial:
3410 add_device_config(DEV_SERIAL, optarg);
3411 default_serial = 0;
3412 if (strncmp(optarg, "mon:", 4) == 0) {
3413 default_monitor = 0;
3415 break;
3416 case QEMU_OPTION_watchdog:
3417 if (watchdog) {
3418 fprintf(stderr,
3419 "qemu: only one watchdog option may be given\n");
3420 return 1;
3422 watchdog = optarg;
3423 break;
3424 case QEMU_OPTION_watchdog_action:
3425 if (select_watchdog_action(optarg) == -1) {
3426 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3427 exit(1);
3429 break;
3430 case QEMU_OPTION_virtiocon:
3431 add_device_config(DEV_VIRTCON, optarg);
3432 default_virtcon = 0;
3433 if (strncmp(optarg, "mon:", 4) == 0) {
3434 default_monitor = 0;
3436 break;
3437 case QEMU_OPTION_parallel:
3438 add_device_config(DEV_PARALLEL, optarg);
3439 default_parallel = 0;
3440 if (strncmp(optarg, "mon:", 4) == 0) {
3441 default_monitor = 0;
3443 break;
3444 case QEMU_OPTION_debugcon:
3445 add_device_config(DEV_DEBUGCON, optarg);
3446 break;
3447 case QEMU_OPTION_loadvm:
3448 loadvm = optarg;
3449 break;
3450 case QEMU_OPTION_full_screen:
3451 full_screen = 1;
3452 break;
3453 case QEMU_OPTION_no_frame:
3454 no_frame = 1;
3455 break;
3456 case QEMU_OPTION_alt_grab:
3457 alt_grab = 1;
3458 break;
3459 case QEMU_OPTION_ctrl_grab:
3460 ctrl_grab = 1;
3461 break;
3462 case QEMU_OPTION_no_quit:
3463 no_quit = 1;
3464 break;
3465 case QEMU_OPTION_sdl:
3466 #ifdef CONFIG_SDL
3467 display_type = DT_SDL;
3468 break;
3469 #else
3470 fprintf(stderr, "SDL support is disabled\n");
3471 exit(1);
3472 #endif
3473 case QEMU_OPTION_pidfile:
3474 pid_file = optarg;
3475 break;
3476 case QEMU_OPTION_win2k_hack:
3477 win2k_install_hack = 1;
3478 break;
3479 case QEMU_OPTION_rtc_td_hack: {
3480 static GlobalProperty slew_lost_ticks[] = {
3482 .driver = "mc146818rtc",
3483 .property = "lost_tick_policy",
3484 .value = "slew",
3486 { /* end of list */ }
3489 qdev_prop_register_global_list(slew_lost_ticks);
3490 break;
3492 case QEMU_OPTION_acpitable:
3493 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3494 if (!opts) {
3495 exit(1);
3497 do_acpitable_option(opts);
3498 break;
3499 case QEMU_OPTION_smbios:
3500 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3501 if (!opts) {
3502 exit(1);
3504 do_smbios_option(opts);
3505 break;
3506 case QEMU_OPTION_enable_kvm:
3507 olist = qemu_find_opts("machine");
3508 qemu_opts_parse(olist, "accel=kvm", 0);
3509 break;
3510 case QEMU_OPTION_machine:
3511 olist = qemu_find_opts("machine");
3512 opts = qemu_opts_parse(olist, optarg, 1);
3513 if (!opts) {
3514 exit(1);
3516 optarg = qemu_opt_get(opts, "type");
3517 if (optarg) {
3518 machine_class = machine_parse(optarg);
3520 break;
3521 case QEMU_OPTION_no_kvm:
3522 olist = qemu_find_opts("machine");
3523 qemu_opts_parse(olist, "accel=tcg", 0);
3524 break;
3525 case QEMU_OPTION_no_kvm_pit: {
3526 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3527 "separately.\n");
3528 break;
3530 case QEMU_OPTION_no_kvm_pit_reinjection: {
3531 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3533 .driver = "kvm-pit",
3534 .property = "lost_tick_policy",
3535 .value = "discard",
3537 { /* end of list */ }
3540 fprintf(stderr, "Warning: option deprecated, use "
3541 "lost_tick_policy property of kvm-pit instead.\n");
3542 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3543 break;
3545 case QEMU_OPTION_usb:
3546 olist = qemu_find_opts("machine");
3547 qemu_opts_parse(olist, "usb=on", 0);
3548 break;
3549 case QEMU_OPTION_usbdevice:
3550 olist = qemu_find_opts("machine");
3551 qemu_opts_parse(olist, "usb=on", 0);
3552 add_device_config(DEV_USB, optarg);
3553 break;
3554 case QEMU_OPTION_device:
3555 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3556 exit(1);
3558 break;
3559 case QEMU_OPTION_smp:
3560 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3561 exit(1);
3563 break;
3564 case QEMU_OPTION_vnc:
3565 #ifdef CONFIG_VNC
3566 display_remote++;
3567 vnc_display = optarg;
3568 #else
3569 fprintf(stderr, "VNC support is disabled\n");
3570 exit(1);
3571 #endif
3572 break;
3573 case QEMU_OPTION_no_acpi:
3574 acpi_enabled = 0;
3575 break;
3576 case QEMU_OPTION_no_hpet:
3577 no_hpet = 1;
3578 break;
3579 case QEMU_OPTION_balloon:
3580 if (balloon_parse(optarg) < 0) {
3581 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3582 exit(1);
3584 break;
3585 case QEMU_OPTION_no_reboot:
3586 no_reboot = 1;
3587 break;
3588 case QEMU_OPTION_no_shutdown:
3589 no_shutdown = 1;
3590 break;
3591 case QEMU_OPTION_show_cursor:
3592 cursor_hide = 0;
3593 break;
3594 case QEMU_OPTION_uuid:
3595 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3596 fprintf(stderr, "Fail to parse UUID string."
3597 " Wrong format.\n");
3598 exit(1);
3600 qemu_uuid_set = true;
3601 break;
3602 case QEMU_OPTION_option_rom:
3603 if (nb_option_roms >= MAX_OPTION_ROMS) {
3604 fprintf(stderr, "Too many option ROMs\n");
3605 exit(1);
3607 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3608 if (!opts) {
3609 exit(1);
3611 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3612 option_rom[nb_option_roms].bootindex =
3613 qemu_opt_get_number(opts, "bootindex", -1);
3614 if (!option_rom[nb_option_roms].name) {
3615 fprintf(stderr, "Option ROM file is not specified\n");
3616 exit(1);
3618 nb_option_roms++;
3619 break;
3620 case QEMU_OPTION_semihosting:
3621 semihosting_enabled = 1;
3622 break;
3623 case QEMU_OPTION_tdf:
3624 fprintf(stderr, "Warning: user space PIT time drift fix "
3625 "is no longer supported.\n");
3626 break;
3627 case QEMU_OPTION_name:
3628 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3629 if (!opts) {
3630 exit(1);
3632 break;
3633 case QEMU_OPTION_prom_env:
3634 if (nb_prom_envs >= MAX_PROM_ENVS) {
3635 fprintf(stderr, "Too many prom variables\n");
3636 exit(1);
3638 prom_envs[nb_prom_envs] = optarg;
3639 nb_prom_envs++;
3640 break;
3641 case QEMU_OPTION_old_param:
3642 old_param = 1;
3643 break;
3644 case QEMU_OPTION_clock:
3645 /* Clock options no longer exist. Keep this option for
3646 * backward compatibility.
3648 break;
3649 case QEMU_OPTION_startdate:
3650 configure_rtc_date_offset(optarg, 1);
3651 break;
3652 case QEMU_OPTION_rtc:
3653 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3654 if (!opts) {
3655 exit(1);
3657 configure_rtc(opts);
3658 break;
3659 case QEMU_OPTION_tb_size:
3660 tcg_tb_size = strtol(optarg, NULL, 0);
3661 if (tcg_tb_size < 0) {
3662 tcg_tb_size = 0;
3664 break;
3665 case QEMU_OPTION_icount:
3666 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3667 optarg, 1);
3668 if (!icount_opts) {
3669 exit(1);
3671 break;
3672 case QEMU_OPTION_incoming:
3673 incoming = optarg;
3674 runstate_set(RUN_STATE_INMIGRATE);
3675 break;
3676 case QEMU_OPTION_nodefaults:
3677 has_defaults = 0;
3678 break;
3679 case QEMU_OPTION_xen_domid:
3680 if (!(xen_available())) {
3681 printf("Option %s not supported for this target\n", popt->name);
3682 exit(1);
3684 xen_domid = atoi(optarg);
3685 break;
3686 case QEMU_OPTION_xen_create:
3687 if (!(xen_available())) {
3688 printf("Option %s not supported for this target\n", popt->name);
3689 exit(1);
3691 xen_mode = XEN_CREATE;
3692 break;
3693 case QEMU_OPTION_xen_attach:
3694 if (!(xen_available())) {
3695 printf("Option %s not supported for this target\n", popt->name);
3696 exit(1);
3698 xen_mode = XEN_ATTACH;
3699 break;
3700 case QEMU_OPTION_trace:
3702 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3703 if (!opts) {
3704 exit(1);
3706 trace_events = qemu_opt_get(opts, "events");
3707 trace_file = qemu_opt_get(opts, "file");
3708 break;
3710 case QEMU_OPTION_readconfig:
3712 int ret = qemu_read_config_file(optarg);
3713 if (ret < 0) {
3714 fprintf(stderr, "read config %s: %s\n", optarg,
3715 strerror(-ret));
3716 exit(1);
3718 break;
3720 case QEMU_OPTION_spice:
3721 olist = qemu_find_opts("spice");
3722 if (!olist) {
3723 fprintf(stderr, "spice is not supported by this qemu build.\n");
3724 exit(1);
3726 opts = qemu_opts_parse(olist, optarg, 0);
3727 if (!opts) {
3728 exit(1);
3730 display_remote++;
3731 break;
3732 case QEMU_OPTION_writeconfig:
3734 FILE *fp;
3735 if (strcmp(optarg, "-") == 0) {
3736 fp = stdout;
3737 } else {
3738 fp = fopen(optarg, "w");
3739 if (fp == NULL) {
3740 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3741 exit(1);
3744 qemu_config_write(fp);
3745 if (fp != stdout) {
3746 fclose(fp);
3748 break;
3750 case QEMU_OPTION_qtest:
3751 qtest_chrdev = optarg;
3752 break;
3753 case QEMU_OPTION_qtest_log:
3754 qtest_log = optarg;
3755 break;
3756 case QEMU_OPTION_sandbox:
3757 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3758 if (!opts) {
3759 exit(1);
3761 break;
3762 case QEMU_OPTION_add_fd:
3763 #ifndef _WIN32
3764 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3765 if (!opts) {
3766 exit(1);
3768 #else
3769 error_report("File descriptor passing is disabled on this "
3770 "platform");
3771 exit(1);
3772 #endif
3773 break;
3774 case QEMU_OPTION_object:
3775 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3776 if (!opts) {
3777 exit(1);
3779 break;
3780 case QEMU_OPTION_realtime:
3781 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3782 if (!opts) {
3783 exit(1);
3785 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3786 break;
3787 case QEMU_OPTION_msg:
3788 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3789 if (!opts) {
3790 exit(1);
3792 configure_msg(opts);
3793 break;
3794 case QEMU_OPTION_dump_vmstate:
3795 vmstate_dump_file = fopen(optarg, "w");
3796 if (vmstate_dump_file == NULL) {
3797 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3798 exit(1);
3800 break;
3801 default:
3802 os_parse_cmd_args(popt->index, optarg);
3806 loc_set_none();
3808 os_daemonize();
3810 if (qemu_init_main_loop(&main_loop_err)) {
3811 error_report("%s", error_get_pretty(main_loop_err));
3812 exit(1);
3815 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3816 exit(1);
3819 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3820 exit(1);
3823 #ifndef _WIN32
3824 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3825 exit(1);
3828 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3829 exit(1);
3831 #endif
3833 if (machine_class == NULL) {
3834 fprintf(stderr, "No machine specified, and there is no default.\n"
3835 "Use -machine help to list supported machines!\n");
3836 exit(1);
3839 current_machine = MACHINE(object_new(object_class_get_name(
3840 OBJECT_CLASS(machine_class))));
3841 object_property_add_child(object_get_root(), "machine",
3842 OBJECT(current_machine), &error_abort);
3843 cpu_exec_init_all();
3845 if (machine_class->hw_version) {
3846 qemu_set_version(machine_class->hw_version);
3849 /* Init CPU def lists, based on config
3850 * - Must be called after all the qemu_read_config_file() calls
3851 * - Must be called before list_cpus()
3852 * - Must be called before machine->init()
3854 cpudef_init();
3856 if (cpu_model && is_help_option(cpu_model)) {
3857 list_cpus(stdout, &fprintf, cpu_model);
3858 exit(0);
3861 /* Open the logfile at this point, if necessary. We can't open the logfile
3862 * when encountering either of the logging options (-d or -D) because the
3863 * other one may be encountered later on the command line, changing the
3864 * location or level of logging.
3866 if (log_mask) {
3867 int mask;
3868 if (log_file) {
3869 qemu_set_log_filename(log_file);
3872 mask = qemu_str_to_log_mask(log_mask);
3873 if (!mask) {
3874 qemu_print_log_usage(stdout);
3875 exit(1);
3877 qemu_set_log(mask);
3880 if (!is_daemonized()) {
3881 if (!trace_init_backends(trace_events, trace_file)) {
3882 exit(1);
3886 /* If no data_dir is specified then try to find it relative to the
3887 executable path. */
3888 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3889 data_dir[data_dir_idx] = os_find_datadir();
3890 if (data_dir[data_dir_idx] != NULL) {
3891 data_dir_idx++;
3894 /* If all else fails use the install path specified when building. */
3895 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3896 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3899 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3901 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3902 if (smp_cpus > machine_class->max_cpus) {
3903 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3904 "supported by machine `%s' (%d)\n", smp_cpus,
3905 machine_class->name, machine_class->max_cpus);
3906 exit(1);
3910 * Get the default machine options from the machine if it is not already
3911 * specified either by the configuration file or by the command line.
3913 if (machine_class->default_machine_opts) {
3914 qemu_opts_set_defaults(qemu_find_opts("machine"),
3915 machine_class->default_machine_opts, 0);
3918 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3919 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3921 if (!vga_model && !default_vga) {
3922 vga_interface_type = VGA_DEVICE;
3924 if (!has_defaults || machine_class->no_serial) {
3925 default_serial = 0;
3927 if (!has_defaults || machine_class->no_parallel) {
3928 default_parallel = 0;
3930 if (!has_defaults || !machine_class->use_virtcon) {
3931 default_virtcon = 0;
3933 if (!has_defaults || !machine_class->use_sclp) {
3934 default_sclp = 0;
3936 if (!has_defaults || machine_class->no_floppy) {
3937 default_floppy = 0;
3939 if (!has_defaults || machine_class->no_cdrom) {
3940 default_cdrom = 0;
3942 if (!has_defaults || machine_class->no_sdcard) {
3943 default_sdcard = 0;
3945 if (!has_defaults) {
3946 default_monitor = 0;
3947 default_net = 0;
3948 default_vga = 0;
3951 if (is_daemonized()) {
3952 /* According to documentation and historically, -nographic redirects
3953 * serial port, parallel port and monitor to stdio, which does not work
3954 * with -daemonize. We can redirect these to null instead, but since
3955 * -nographic is legacy, let's just error out.
3956 * We disallow -nographic only if all other ports are not redirected
3957 * explicitly, to not break existing legacy setups which uses
3958 * -nographic _and_ redirects all ports explicitly - this is valid
3959 * usage, -nographic is just a no-op in this case.
3961 if (display_type == DT_NOGRAPHIC
3962 && (default_parallel || default_serial
3963 || default_monitor || default_virtcon)) {
3964 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3965 exit(1);
3967 #ifdef CONFIG_CURSES
3968 if (display_type == DT_CURSES) {
3969 fprintf(stderr, "curses display can not be used with -daemonize\n");
3970 exit(1);
3972 #endif
3975 if (display_type == DT_NOGRAPHIC) {
3976 if (default_parallel)
3977 add_device_config(DEV_PARALLEL, "null");
3978 if (default_serial && default_monitor) {
3979 add_device_config(DEV_SERIAL, "mon:stdio");
3980 } else if (default_virtcon && default_monitor) {
3981 add_device_config(DEV_VIRTCON, "mon:stdio");
3982 } else if (default_sclp && default_monitor) {
3983 add_device_config(DEV_SCLP, "mon:stdio");
3984 } else {
3985 if (default_serial)
3986 add_device_config(DEV_SERIAL, "stdio");
3987 if (default_virtcon)
3988 add_device_config(DEV_VIRTCON, "stdio");
3989 if (default_sclp) {
3990 add_device_config(DEV_SCLP, "stdio");
3992 if (default_monitor)
3993 monitor_parse("stdio", "readline");
3995 } else {
3996 if (default_serial)
3997 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3998 if (default_parallel)
3999 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4000 if (default_monitor)
4001 monitor_parse("vc:80Cx24C", "readline");
4002 if (default_virtcon)
4003 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4004 if (default_sclp) {
4005 add_device_config(DEV_SCLP, "vc:80Cx24C");
4009 if (display_type == DT_DEFAULT && !display_remote) {
4010 #if defined(CONFIG_GTK)
4011 display_type = DT_GTK;
4012 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4013 display_type = DT_SDL;
4014 #elif defined(CONFIG_VNC)
4015 vnc_display = "localhost:0,to=99";
4016 show_vnc_port = 1;
4017 #else
4018 display_type = DT_NONE;
4019 #endif
4022 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4023 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4024 "for SDL, ignoring option\n");
4026 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4027 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4028 "ignoring option\n");
4031 #if defined(CONFIG_GTK)
4032 if (display_type == DT_GTK) {
4033 early_gtk_display_init();
4035 #endif
4037 socket_init();
4039 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4040 exit(1);
4041 #ifdef CONFIG_VIRTFS
4042 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4043 exit(1);
4045 #endif
4047 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4048 os_pidfile_error();
4049 exit(1);
4052 /* store value for the future use */
4053 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4055 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4056 != 0) {
4057 exit(0);
4060 if (qemu_opts_foreach(qemu_find_opts("object"),
4061 object_create, NULL, 0) != 0) {
4062 exit(1);
4065 machine_opts = qemu_get_machine_opts();
4066 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4067 1) < 0) {
4068 object_unref(OBJECT(current_machine));
4069 exit(1);
4072 configure_accelerator(current_machine);
4074 if (qtest_chrdev) {
4075 Error *local_err = NULL;
4076 qtest_init(qtest_chrdev, qtest_log, &local_err);
4077 if (local_err) {
4078 error_report("%s", error_get_pretty(local_err));
4079 error_free(local_err);
4080 exit(1);
4084 machine_opts = qemu_get_machine_opts();
4085 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4086 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4087 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4088 bios_name = qemu_opt_get(machine_opts, "firmware");
4090 boot_order = machine_class->default_boot_order;
4091 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4092 if (opts) {
4093 char *normal_boot_order;
4094 const char *order, *once;
4096 order = qemu_opt_get(opts, "order");
4097 if (order) {
4098 validate_bootdevices(order);
4099 boot_order = order;
4102 once = qemu_opt_get(opts, "once");
4103 if (once) {
4104 validate_bootdevices(once);
4105 normal_boot_order = g_strdup(boot_order);
4106 boot_order = once;
4107 qemu_register_reset(restore_boot_order, normal_boot_order);
4110 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4111 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4114 if (!kernel_cmdline) {
4115 kernel_cmdline = "";
4116 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4119 linux_boot = (kernel_filename != NULL);
4121 if (!linux_boot && *kernel_cmdline != '\0') {
4122 fprintf(stderr, "-append only allowed with -kernel option\n");
4123 exit(1);
4126 if (!linux_boot && initrd_filename != NULL) {
4127 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4128 exit(1);
4131 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4132 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4133 exit(1);
4136 os_set_line_buffering();
4138 qemu_init_cpu_loop();
4139 qemu_mutex_lock_iothread();
4141 #ifdef CONFIG_SPICE
4142 /* spice needs the timers to be initialized by this point */
4143 qemu_spice_init();
4144 #endif
4146 cpu_ticks_init();
4147 if (icount_opts) {
4148 if (kvm_enabled() || xen_enabled()) {
4149 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4150 exit(1);
4152 configure_icount(icount_opts, &error_abort);
4153 qemu_opts_del(icount_opts);
4156 /* clean up network at qemu process termination */
4157 atexit(&net_cleanup);
4159 if (net_init_clients() < 0) {
4160 exit(1);
4163 #ifdef CONFIG_TPM
4164 if (tpm_init() < 0) {
4165 exit(1);
4167 #endif
4169 /* init the bluetooth world */
4170 if (foreach_device_config(DEV_BT, bt_parse))
4171 exit(1);
4173 if (!xen_enabled()) {
4174 /* On 32-bit hosts, QEMU is limited by virtual address space */
4175 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4176 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4177 exit(1);
4181 blk_mig_init();
4182 ram_mig_init();
4184 /* If the currently selected machine wishes to override the units-per-bus
4185 * property of its default HBA interface type, do so now. */
4186 if (machine_class->units_per_default_bus) {
4187 override_max_devs(machine_class->block_default_type,
4188 machine_class->units_per_default_bus);
4191 /* open the virtual block devices */
4192 if (snapshot)
4193 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4194 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4195 &machine_class->block_default_type, 1) != 0) {
4196 exit(1);
4199 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4200 CDROM_OPTS);
4201 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4202 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4204 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4205 NULL, 1) != 0) {
4206 exit(1);
4209 set_numa_nodes();
4211 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4212 exit(1);
4215 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4216 exit(1);
4217 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4218 exit(1);
4219 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4220 exit(1);
4221 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4222 exit(1);
4224 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4225 exit(1);
4227 /* If no default VGA is requested, the default is "none". */
4228 if (default_vga) {
4229 if (cirrus_vga_available()) {
4230 vga_model = "cirrus";
4231 } else if (vga_available()) {
4232 vga_model = "std";
4235 if (vga_model) {
4236 select_vgahw(vga_model);
4239 if (watchdog) {
4240 i = select_watchdog(watchdog);
4241 if (i > 0)
4242 exit (i == 1 ? 1 : 0);
4245 if (machine_class->compat_props) {
4246 qdev_prop_register_global_list(machine_class->compat_props);
4248 qemu_add_globals();
4250 qdev_machine_init();
4252 current_machine->ram_size = ram_size;
4253 current_machine->maxram_size = maxram_size;
4254 current_machine->ram_slots = ram_slots;
4255 current_machine->boot_order = boot_order;
4256 current_machine->cpu_model = cpu_model;
4258 machine_class->init(current_machine);
4260 realtime_init();
4262 audio_init();
4264 cpu_synchronize_all_post_init();
4266 set_numa_modes();
4268 /* init USB devices */
4269 if (usb_enabled(false)) {
4270 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4271 exit(1);
4274 /* init generic devices */
4275 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4276 exit(1);
4278 /* Did we create any drives that we failed to create a device for? */
4279 drive_check_orphaned();
4281 net_check_clients();
4283 ds = init_displaystate();
4285 /* init local displays */
4286 switch (display_type) {
4287 case DT_NOGRAPHIC:
4288 (void)ds; /* avoid warning if no display is configured */
4289 break;
4290 #if defined(CONFIG_CURSES)
4291 case DT_CURSES:
4292 curses_display_init(ds, full_screen);
4293 break;
4294 #endif
4295 #if defined(CONFIG_SDL)
4296 case DT_SDL:
4297 sdl_display_init(ds, full_screen, no_frame);
4298 break;
4299 #elif defined(CONFIG_COCOA)
4300 case DT_SDL:
4301 cocoa_display_init(ds, full_screen);
4302 break;
4303 #endif
4304 #if defined(CONFIG_GTK)
4305 case DT_GTK:
4306 gtk_display_init(ds, full_screen, grab_on_hover);
4307 break;
4308 #endif
4309 default:
4310 break;
4313 /* must be after terminal init, SDL library changes signal handlers */
4314 os_setup_signal_handling();
4316 #ifdef CONFIG_VNC
4317 /* init remote displays */
4318 if (vnc_display) {
4319 Error *local_err = NULL;
4320 vnc_display_init(ds);
4321 vnc_display_open(ds, vnc_display, &local_err);
4322 if (local_err != NULL) {
4323 error_report("Failed to start VNC server on `%s': %s",
4324 vnc_display, error_get_pretty(local_err));
4325 error_free(local_err);
4326 exit(1);
4329 if (show_vnc_port) {
4330 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4333 #endif
4334 #ifdef CONFIG_SPICE
4335 if (using_spice) {
4336 qemu_spice_display_init();
4338 #endif
4340 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4341 exit(1);
4344 qdev_machine_creation_done();
4346 if (rom_load_all() != 0) {
4347 fprintf(stderr, "rom loading failed\n");
4348 exit(1);
4351 /* TODO: once all bus devices are qdevified, this should be done
4352 * when bus is created by qdev.c */
4353 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4354 qemu_run_machine_init_done_notifiers();
4356 /* Done notifiers can load ROMs */
4357 rom_load_done();
4359 qemu_system_reset(VMRESET_SILENT);
4360 if (loadvm) {
4361 if (load_vmstate(loadvm) < 0) {
4362 autostart = 0;
4366 qdev_prop_check_globals();
4367 if (vmstate_dump_file) {
4368 /* dump and exit */
4369 dump_vmstate_json_to_file(vmstate_dump_file);
4370 return 0;
4373 if (incoming) {
4374 Error *local_err = NULL;
4375 qemu_start_incoming_migration(incoming, &local_err);
4376 if (local_err) {
4377 error_report("-incoming %s: %s", incoming,
4378 error_get_pretty(local_err));
4379 error_free(local_err);
4380 exit(1);
4382 } else if (autostart) {
4383 vm_start();
4386 os_setup_post();
4388 if (is_daemonized()) {
4389 if (!trace_init_backends(trace_events, trace_file)) {
4390 exit(1);
4394 main_loop();
4395 bdrv_close_all();
4396 pause_all_vcpus();
4397 res_free();
4398 #ifdef CONFIG_TPM
4399 tpm_cleanup();
4400 #endif
4402 return 0;