target-arm: move AArch32 SCR into security reglist
[qemu.git] / vl.c
blob7cdfd49ae0766314179734e1fdfa822f5b54d18f
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 static int display_remote;
133 const char* keyboard_layout = NULL;
134 ram_addr_t ram_size;
135 const char *mem_path = NULL;
136 int mem_prealloc = 0; /* force preallocation of physical target memory */
137 bool enable_mlock = false;
138 int nb_nics;
139 NICInfo nd_table[MAX_NICS];
140 int autostart;
141 static int rtc_utc = 1;
142 static int rtc_date_offset = -1; /* -1 means no change */
143 QEMUClockType rtc_clock;
144 int vga_interface_type = VGA_NONE;
145 static int full_screen = 0;
146 static int no_frame = 0;
147 int no_quit = 0;
148 #ifdef CONFIG_GTK
149 static bool grab_on_hover;
150 #endif
151 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
152 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
153 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
154 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
155 int win2k_install_hack = 0;
156 int singlestep = 0;
157 int smp_cpus = 1;
158 int max_cpus = 0;
159 int smp_cores = 1;
160 int smp_threads = 1;
161 #ifdef CONFIG_VNC
162 const char *vnc_display;
163 #endif
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int semihosting_enabled = 0;
175 int old_param = 0;
176 const char *qemu_name;
177 int alt_grab = 0;
178 int ctrl_grab = 0;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 bool boot_strict;
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
187 int icount_align_option;
189 int nb_numa_nodes;
190 int max_numa_nodeid;
191 NodeInfo numa_info[MAX_NODES];
193 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
194 * little-endian "wire format" described in the SMBIOS 2.6 specification.
196 uint8_t qemu_uuid[16];
197 bool qemu_uuid_set;
199 static QEMUBootSetHandler *boot_set_handler;
200 static void *boot_set_opaque;
202 static NotifierList exit_notifiers =
203 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
205 static NotifierList machine_init_done_notifiers =
206 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
208 bool xen_allowed;
209 uint32_t xen_domid;
210 enum xen_mode xen_mode = XEN_EMULATE;
212 static int has_defaults = 1;
213 static int default_serial = 1;
214 static int default_parallel = 1;
215 static int default_virtcon = 1;
216 static int default_sclp = 1;
217 static int default_monitor = 1;
218 static int default_floppy = 1;
219 static int default_cdrom = 1;
220 static int default_sdcard = 1;
221 static int default_vga = 1;
223 static struct {
224 const char *driver;
225 int *flag;
226 } default_list[] = {
227 { .driver = "isa-serial", .flag = &default_serial },
228 { .driver = "isa-parallel", .flag = &default_parallel },
229 { .driver = "isa-fdc", .flag = &default_floppy },
230 { .driver = "ide-cd", .flag = &default_cdrom },
231 { .driver = "ide-hd", .flag = &default_cdrom },
232 { .driver = "ide-drive", .flag = &default_cdrom },
233 { .driver = "scsi-cd", .flag = &default_cdrom },
234 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
235 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
236 { .driver = "virtio-serial", .flag = &default_virtcon },
237 { .driver = "VGA", .flag = &default_vga },
238 { .driver = "isa-vga", .flag = &default_vga },
239 { .driver = "cirrus-vga", .flag = &default_vga },
240 { .driver = "isa-cirrus-vga", .flag = &default_vga },
241 { .driver = "vmware-svga", .flag = &default_vga },
242 { .driver = "qxl-vga", .flag = &default_vga },
245 static QemuOptsList qemu_rtc_opts = {
246 .name = "rtc",
247 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
248 .desc = {
250 .name = "base",
251 .type = QEMU_OPT_STRING,
253 .name = "clock",
254 .type = QEMU_OPT_STRING,
256 .name = "driftfix",
257 .type = QEMU_OPT_STRING,
259 { /* end of list */ }
263 static QemuOptsList qemu_sandbox_opts = {
264 .name = "sandbox",
265 .implied_opt_name = "enable",
266 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
267 .desc = {
269 .name = "enable",
270 .type = QEMU_OPT_BOOL,
272 { /* end of list */ }
276 static QemuOptsList qemu_trace_opts = {
277 .name = "trace",
278 .implied_opt_name = "trace",
279 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
280 .desc = {
282 .name = "events",
283 .type = QEMU_OPT_STRING,
285 .name = "file",
286 .type = QEMU_OPT_STRING,
288 { /* end of list */ }
292 static QemuOptsList qemu_option_rom_opts = {
293 .name = "option-rom",
294 .implied_opt_name = "romfile",
295 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
296 .desc = {
298 .name = "bootindex",
299 .type = QEMU_OPT_NUMBER,
300 }, {
301 .name = "romfile",
302 .type = QEMU_OPT_STRING,
304 { /* end of list */ }
308 static QemuOptsList qemu_machine_opts = {
309 .name = "machine",
310 .implied_opt_name = "type",
311 .merge_lists = true,
312 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
313 .desc = {
315 .name = "type",
316 .type = QEMU_OPT_STRING,
317 .help = "emulated machine"
318 }, {
319 .name = "accel",
320 .type = QEMU_OPT_STRING,
321 .help = "accelerator list",
322 }, {
323 .name = "kernel_irqchip",
324 .type = QEMU_OPT_BOOL,
325 .help = "use KVM in-kernel irqchip",
326 }, {
327 .name = "kvm_shadow_mem",
328 .type = QEMU_OPT_SIZE,
329 .help = "KVM shadow MMU size",
330 }, {
331 .name = "kernel",
332 .type = QEMU_OPT_STRING,
333 .help = "Linux kernel image file",
334 }, {
335 .name = "initrd",
336 .type = QEMU_OPT_STRING,
337 .help = "Linux initial ramdisk file",
338 }, {
339 .name = "append",
340 .type = QEMU_OPT_STRING,
341 .help = "Linux kernel command line",
342 }, {
343 .name = "dtb",
344 .type = QEMU_OPT_STRING,
345 .help = "Linux kernel device tree file",
346 }, {
347 .name = "dumpdtb",
348 .type = QEMU_OPT_STRING,
349 .help = "Dump current dtb to a file and quit",
350 }, {
351 .name = "phandle_start",
352 .type = QEMU_OPT_NUMBER,
353 .help = "The first phandle ID we may generate dynamically",
354 }, {
355 .name = "dt_compatible",
356 .type = QEMU_OPT_STRING,
357 .help = "Overrides the \"compatible\" property of the dt root node",
358 }, {
359 .name = "dump-guest-core",
360 .type = QEMU_OPT_BOOL,
361 .help = "Include guest memory in a core dump",
362 }, {
363 .name = "mem-merge",
364 .type = QEMU_OPT_BOOL,
365 .help = "enable/disable memory merge support",
367 .name = "usb",
368 .type = QEMU_OPT_BOOL,
369 .help = "Set on/off to enable/disable usb",
371 .name = "firmware",
372 .type = QEMU_OPT_STRING,
373 .help = "firmware image",
375 .name = "kvm-type",
376 .type = QEMU_OPT_STRING,
377 .help = "Specifies the KVM virtualization mode (HV, PR)",
379 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
380 .type = QEMU_OPT_SIZE,
381 .help = "maximum ram below the 4G boundary (32bit boundary)",
382 }, {
383 .name = PC_MACHINE_VMPORT,
384 .type = QEMU_OPT_STRING,
385 .help = "Enable vmport (pc & q35)",
387 .name = "iommu",
388 .type = QEMU_OPT_BOOL,
389 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
391 { /* End of list */ }
395 static QemuOptsList qemu_boot_opts = {
396 .name = "boot-opts",
397 .implied_opt_name = "order",
398 .merge_lists = true,
399 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
400 .desc = {
402 .name = "order",
403 .type = QEMU_OPT_STRING,
404 }, {
405 .name = "once",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "menu",
409 .type = QEMU_OPT_BOOL,
410 }, {
411 .name = "splash",
412 .type = QEMU_OPT_STRING,
413 }, {
414 .name = "splash-time",
415 .type = QEMU_OPT_STRING,
416 }, {
417 .name = "reboot-timeout",
418 .type = QEMU_OPT_STRING,
419 }, {
420 .name = "strict",
421 .type = QEMU_OPT_BOOL,
423 { /*End of list */ }
427 static QemuOptsList qemu_add_fd_opts = {
428 .name = "add-fd",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
430 .desc = {
432 .name = "fd",
433 .type = QEMU_OPT_NUMBER,
434 .help = "file descriptor of which a duplicate is added to fd set",
436 .name = "set",
437 .type = QEMU_OPT_NUMBER,
438 .help = "ID of the fd set to add fd to",
440 .name = "opaque",
441 .type = QEMU_OPT_STRING,
442 .help = "free-form string used to describe fd",
444 { /* end of list */ }
448 static QemuOptsList qemu_object_opts = {
449 .name = "object",
450 .implied_opt_name = "qom-type",
451 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
452 .desc = {
457 static QemuOptsList qemu_tpmdev_opts = {
458 .name = "tpmdev",
459 .implied_opt_name = "type",
460 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
461 .desc = {
462 /* options are defined in the TPM backends */
463 { /* end of list */ }
467 static QemuOptsList qemu_realtime_opts = {
468 .name = "realtime",
469 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
470 .desc = {
472 .name = "mlock",
473 .type = QEMU_OPT_BOOL,
475 { /* end of list */ }
479 static QemuOptsList qemu_msg_opts = {
480 .name = "msg",
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
482 .desc = {
484 .name = "timestamp",
485 .type = QEMU_OPT_BOOL,
487 { /* end of list */ }
491 static QemuOptsList qemu_name_opts = {
492 .name = "name",
493 .implied_opt_name = "guest",
494 .merge_lists = true,
495 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
496 .desc = {
498 .name = "guest",
499 .type = QEMU_OPT_STRING,
500 .help = "Sets the name of the guest.\n"
501 "This name will be displayed in the SDL window caption.\n"
502 "The name will also be used for the VNC server",
503 }, {
504 .name = "process",
505 .type = QEMU_OPT_STRING,
506 .help = "Sets the name of the QEMU process, as shown in top etc",
507 }, {
508 .name = "debug-threads",
509 .type = QEMU_OPT_BOOL,
510 .help = "When enabled, name the individual threads; defaults off.\n"
511 "NOTE: The thread names are for debugging and not a\n"
512 "stable API.",
514 { /* End of list */ }
518 static QemuOptsList qemu_mem_opts = {
519 .name = "memory",
520 .implied_opt_name = "size",
521 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
522 .merge_lists = true,
523 .desc = {
525 .name = "size",
526 .type = QEMU_OPT_SIZE,
529 .name = "slots",
530 .type = QEMU_OPT_NUMBER,
533 .name = "maxmem",
534 .type = QEMU_OPT_SIZE,
536 { /* end of list */ }
540 static QemuOptsList qemu_icount_opts = {
541 .name = "icount",
542 .implied_opt_name = "shift",
543 .merge_lists = true,
544 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
545 .desc = {
547 .name = "shift",
548 .type = QEMU_OPT_STRING,
549 }, {
550 .name = "align",
551 .type = QEMU_OPT_BOOL,
553 { /* end of list */ }
557 static QemuOptsList qemu_semihosting_config_opts = {
558 .name = "semihosting-config",
559 .implied_opt_name = "enable",
560 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
561 .desc = {
563 .name = "enable",
564 .type = QEMU_OPT_BOOL,
565 }, {
566 .name = "target",
567 .type = QEMU_OPT_STRING,
569 { /* end of list */ }
574 * Get machine options
576 * Returns: machine options (never null).
578 QemuOpts *qemu_get_machine_opts(void)
580 return qemu_find_opts_singleton("machine");
583 const char *qemu_get_vm_name(void)
585 return qemu_name;
588 static void res_free(void)
590 if (boot_splash_filedata != NULL) {
591 g_free(boot_splash_filedata);
592 boot_splash_filedata = NULL;
596 static int default_driver_check(QemuOpts *opts, void *opaque)
598 const char *driver = qemu_opt_get(opts, "driver");
599 int i;
601 if (!driver)
602 return 0;
603 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
604 if (strcmp(default_list[i].driver, driver) != 0)
605 continue;
606 *(default_list[i].flag) = 0;
608 return 0;
611 /***********************************************************/
612 /* QEMU state */
614 static RunState current_run_state = RUN_STATE_PRELAUNCH;
616 /* We use RUN_STATE_MAX but any invalid value will do */
617 static RunState vmstop_requested = RUN_STATE_MAX;
618 static QemuMutex vmstop_lock;
620 typedef struct {
621 RunState from;
622 RunState to;
623 } RunStateTransition;
625 static const RunStateTransition runstate_transitions_def[] = {
626 /* from -> to */
627 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
628 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
631 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
633 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
634 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
637 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
639 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
640 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
643 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
646 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
647 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
649 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
650 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
652 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
654 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
655 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
656 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
657 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
658 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
659 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
660 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
661 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
662 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
663 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
665 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
667 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
668 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
670 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
671 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
672 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
673 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
675 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
676 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
678 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
679 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
681 { RUN_STATE_MAX, RUN_STATE_MAX },
684 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
686 bool runstate_check(RunState state)
688 return current_run_state == state;
691 static void runstate_init(void)
693 const RunStateTransition *p;
695 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
696 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
697 runstate_valid_transitions[p->from][p->to] = true;
700 qemu_mutex_init(&vmstop_lock);
703 /* This function will abort() on invalid state transitions */
704 void runstate_set(RunState new_state)
706 assert(new_state < RUN_STATE_MAX);
708 if (!runstate_valid_transitions[current_run_state][new_state]) {
709 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
710 RunState_lookup[current_run_state],
711 RunState_lookup[new_state]);
712 abort();
714 trace_runstate_set(new_state);
715 current_run_state = new_state;
718 int runstate_is_running(void)
720 return runstate_check(RUN_STATE_RUNNING);
723 bool runstate_needs_reset(void)
725 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
726 runstate_check(RUN_STATE_SHUTDOWN);
729 StatusInfo *qmp_query_status(Error **errp)
731 StatusInfo *info = g_malloc0(sizeof(*info));
733 info->running = runstate_is_running();
734 info->singlestep = singlestep;
735 info->status = current_run_state;
737 return info;
740 static bool qemu_vmstop_requested(RunState *r)
742 qemu_mutex_lock(&vmstop_lock);
743 *r = vmstop_requested;
744 vmstop_requested = RUN_STATE_MAX;
745 qemu_mutex_unlock(&vmstop_lock);
746 return *r < RUN_STATE_MAX;
749 void qemu_system_vmstop_request_prepare(void)
751 qemu_mutex_lock(&vmstop_lock);
754 void qemu_system_vmstop_request(RunState state)
756 vmstop_requested = state;
757 qemu_mutex_unlock(&vmstop_lock);
758 qemu_notify_event();
761 void vm_start(void)
763 RunState requested;
765 qemu_vmstop_requested(&requested);
766 if (runstate_is_running() && requested == RUN_STATE_MAX) {
767 return;
770 /* Ensure that a STOP/RESUME pair of events is emitted if a
771 * vmstop request was pending. The BLOCK_IO_ERROR event, for
772 * example, according to documentation is always followed by
773 * the STOP event.
775 if (runstate_is_running()) {
776 qapi_event_send_stop(&error_abort);
777 } else {
778 cpu_enable_ticks();
779 runstate_set(RUN_STATE_RUNNING);
780 vm_state_notify(1, RUN_STATE_RUNNING);
781 resume_all_vcpus();
784 qapi_event_send_resume(&error_abort);
788 /***********************************************************/
789 /* real time host monotonic timer */
791 /***********************************************************/
792 /* host time/date access */
793 void qemu_get_timedate(struct tm *tm, int offset)
795 time_t ti;
797 time(&ti);
798 ti += offset;
799 if (rtc_date_offset == -1) {
800 if (rtc_utc)
801 gmtime_r(&ti, tm);
802 else
803 localtime_r(&ti, tm);
804 } else {
805 ti -= rtc_date_offset;
806 gmtime_r(&ti, tm);
810 int qemu_timedate_diff(struct tm *tm)
812 time_t seconds;
814 if (rtc_date_offset == -1)
815 if (rtc_utc)
816 seconds = mktimegm(tm);
817 else {
818 struct tm tmp = *tm;
819 tmp.tm_isdst = -1; /* use timezone to figure it out */
820 seconds = mktime(&tmp);
822 else
823 seconds = mktimegm(tm) + rtc_date_offset;
825 return seconds - time(NULL);
828 static void configure_rtc_date_offset(const char *startdate, int legacy)
830 time_t rtc_start_date;
831 struct tm tm;
833 if (!strcmp(startdate, "now") && legacy) {
834 rtc_date_offset = -1;
835 } else {
836 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
837 &tm.tm_year,
838 &tm.tm_mon,
839 &tm.tm_mday,
840 &tm.tm_hour,
841 &tm.tm_min,
842 &tm.tm_sec) == 6) {
843 /* OK */
844 } else if (sscanf(startdate, "%d-%d-%d",
845 &tm.tm_year,
846 &tm.tm_mon,
847 &tm.tm_mday) == 3) {
848 tm.tm_hour = 0;
849 tm.tm_min = 0;
850 tm.tm_sec = 0;
851 } else {
852 goto date_fail;
854 tm.tm_year -= 1900;
855 tm.tm_mon--;
856 rtc_start_date = mktimegm(&tm);
857 if (rtc_start_date == -1) {
858 date_fail:
859 fprintf(stderr, "Invalid date format. Valid formats are:\n"
860 "'2006-06-17T16:01:21' or '2006-06-17'\n");
861 exit(1);
863 rtc_date_offset = time(NULL) - rtc_start_date;
867 static void configure_rtc(QemuOpts *opts)
869 const char *value;
871 value = qemu_opt_get(opts, "base");
872 if (value) {
873 if (!strcmp(value, "utc")) {
874 rtc_utc = 1;
875 } else if (!strcmp(value, "localtime")) {
876 rtc_utc = 0;
877 } else {
878 configure_rtc_date_offset(value, 0);
881 value = qemu_opt_get(opts, "clock");
882 if (value) {
883 if (!strcmp(value, "host")) {
884 rtc_clock = QEMU_CLOCK_HOST;
885 } else if (!strcmp(value, "rt")) {
886 rtc_clock = QEMU_CLOCK_REALTIME;
887 } else if (!strcmp(value, "vm")) {
888 rtc_clock = QEMU_CLOCK_VIRTUAL;
889 } else {
890 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
891 exit(1);
894 value = qemu_opt_get(opts, "driftfix");
895 if (value) {
896 if (!strcmp(value, "slew")) {
897 static GlobalProperty slew_lost_ticks[] = {
899 .driver = "mc146818rtc",
900 .property = "lost_tick_policy",
901 .value = "slew",
903 { /* end of list */ }
906 qdev_prop_register_global_list(slew_lost_ticks);
907 } else if (!strcmp(value, "none")) {
908 /* discard is default */
909 } else {
910 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
911 exit(1);
916 /***********************************************************/
917 /* Bluetooth support */
918 static int nb_hcis;
919 static int cur_hci;
920 static struct HCIInfo *hci_table[MAX_NICS];
922 struct HCIInfo *qemu_next_hci(void)
924 if (cur_hci == nb_hcis)
925 return &null_hci;
927 return hci_table[cur_hci++];
930 static int bt_hci_parse(const char *str)
932 struct HCIInfo *hci;
933 bdaddr_t bdaddr;
935 if (nb_hcis >= MAX_NICS) {
936 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
937 return -1;
940 hci = hci_init(str);
941 if (!hci)
942 return -1;
944 bdaddr.b[0] = 0x52;
945 bdaddr.b[1] = 0x54;
946 bdaddr.b[2] = 0x00;
947 bdaddr.b[3] = 0x12;
948 bdaddr.b[4] = 0x34;
949 bdaddr.b[5] = 0x56 + nb_hcis;
950 hci->bdaddr_set(hci, bdaddr.b);
952 hci_table[nb_hcis++] = hci;
954 return 0;
957 static void bt_vhci_add(int vlan_id)
959 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
961 if (!vlan->slave)
962 fprintf(stderr, "qemu: warning: adding a VHCI to "
963 "an empty scatternet %i\n", vlan_id);
965 bt_vhci_init(bt_new_hci(vlan));
968 static struct bt_device_s *bt_device_add(const char *opt)
970 struct bt_scatternet_s *vlan;
971 int vlan_id = 0;
972 char *endp = strstr(opt, ",vlan=");
973 int len = (endp ? endp - opt : strlen(opt)) + 1;
974 char devname[10];
976 pstrcpy(devname, MIN(sizeof(devname), len), opt);
978 if (endp) {
979 vlan_id = strtol(endp + 6, &endp, 0);
980 if (*endp) {
981 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
982 return 0;
986 vlan = qemu_find_bt_vlan(vlan_id);
988 if (!vlan->slave)
989 fprintf(stderr, "qemu: warning: adding a slave device to "
990 "an empty scatternet %i\n", vlan_id);
992 if (!strcmp(devname, "keyboard"))
993 return bt_keyboard_init(vlan);
995 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
996 return 0;
999 static int bt_parse(const char *opt)
1001 const char *endp, *p;
1002 int vlan;
1004 if (strstart(opt, "hci", &endp)) {
1005 if (!*endp || *endp == ',') {
1006 if (*endp)
1007 if (!strstart(endp, ",vlan=", 0))
1008 opt = endp + 1;
1010 return bt_hci_parse(opt);
1012 } else if (strstart(opt, "vhci", &endp)) {
1013 if (!*endp || *endp == ',') {
1014 if (*endp) {
1015 if (strstart(endp, ",vlan=", &p)) {
1016 vlan = strtol(p, (char **) &endp, 0);
1017 if (*endp) {
1018 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1019 return 1;
1021 } else {
1022 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1023 return 1;
1025 } else
1026 vlan = 0;
1028 bt_vhci_add(vlan);
1029 return 0;
1031 } else if (strstart(opt, "device:", &endp))
1032 return !bt_device_add(endp);
1034 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1035 return 1;
1038 static int parse_sandbox(QemuOpts *opts, void *opaque)
1040 /* FIXME: change this to true for 1.3 */
1041 if (qemu_opt_get_bool(opts, "enable", false)) {
1042 #ifdef CONFIG_SECCOMP
1043 if (seccomp_start() < 0) {
1044 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1045 "failed to install seccomp syscall filter in the kernel");
1046 return -1;
1048 #else
1049 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1050 "sandboxing request but seccomp is not compiled into this build");
1051 return -1;
1052 #endif
1055 return 0;
1058 static int parse_name(QemuOpts *opts, void *opaque)
1060 const char *proc_name;
1062 if (qemu_opt_get(opts, "debug-threads")) {
1063 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1065 qemu_name = qemu_opt_get(opts, "guest");
1067 proc_name = qemu_opt_get(opts, "process");
1068 if (proc_name) {
1069 os_set_proc_name(proc_name);
1072 return 0;
1075 bool usb_enabled(bool default_usb)
1077 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1078 has_defaults && default_usb);
1081 #ifndef _WIN32
1082 static int parse_add_fd(QemuOpts *opts, void *opaque)
1084 int fd, dupfd, flags;
1085 int64_t fdset_id;
1086 const char *fd_opaque = NULL;
1088 fd = qemu_opt_get_number(opts, "fd", -1);
1089 fdset_id = qemu_opt_get_number(opts, "set", -1);
1090 fd_opaque = qemu_opt_get(opts, "opaque");
1092 if (fd < 0) {
1093 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1094 "fd option is required and must be non-negative");
1095 return -1;
1098 if (fd <= STDERR_FILENO) {
1099 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1100 "fd cannot be a standard I/O stream");
1101 return -1;
1105 * All fds inherited across exec() necessarily have FD_CLOEXEC
1106 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1108 flags = fcntl(fd, F_GETFD);
1109 if (flags == -1 || (flags & FD_CLOEXEC)) {
1110 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1111 "fd is not valid or already in use");
1112 return -1;
1115 if (fdset_id < 0) {
1116 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1117 "set option is required and must be non-negative");
1118 return -1;
1121 #ifdef F_DUPFD_CLOEXEC
1122 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1123 #else
1124 dupfd = dup(fd);
1125 if (dupfd != -1) {
1126 qemu_set_cloexec(dupfd);
1128 #endif
1129 if (dupfd == -1) {
1130 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1131 "Error duplicating fd: %s", strerror(errno));
1132 return -1;
1135 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1136 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1137 fd_opaque, NULL);
1139 return 0;
1142 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1144 int fd;
1146 fd = qemu_opt_get_number(opts, "fd", -1);
1147 close(fd);
1149 return 0;
1151 #endif
1153 /***********************************************************/
1154 /* QEMU Block devices */
1156 #define HD_OPTS "media=disk"
1157 #define CDROM_OPTS "media=cdrom"
1158 #define FD_OPTS ""
1159 #define PFLASH_OPTS ""
1160 #define MTD_OPTS ""
1161 #define SD_OPTS ""
1163 static int drive_init_func(QemuOpts *opts, void *opaque)
1165 BlockInterfaceType *block_default_type = opaque;
1167 return drive_new(opts, *block_default_type) == NULL;
1170 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1172 if (qemu_opt_get(opts, "snapshot") == NULL) {
1173 qemu_opt_set(opts, "snapshot", "on");
1175 return 0;
1178 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1179 int index, const char *optstr)
1181 QemuOpts *opts;
1182 DriveInfo *dinfo;
1184 if (!enable || drive_get_by_index(type, index)) {
1185 return;
1188 opts = drive_add(type, index, NULL, optstr);
1189 if (snapshot) {
1190 drive_enable_snapshot(opts, NULL);
1193 dinfo = drive_new(opts, type);
1194 if (!dinfo) {
1195 exit(1);
1197 dinfo->is_default = true;
1201 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1203 boot_set_handler = func;
1204 boot_set_opaque = opaque;
1207 int qemu_boot_set(const char *boot_order)
1209 if (!boot_set_handler) {
1210 return -EINVAL;
1212 return boot_set_handler(boot_set_opaque, boot_order);
1215 static void validate_bootdevices(const char *devices)
1217 /* We just do some generic consistency checks */
1218 const char *p;
1219 int bitmap = 0;
1221 for (p = devices; *p != '\0'; p++) {
1222 /* Allowed boot devices are:
1223 * a-b: floppy disk drives
1224 * c-f: IDE disk drives
1225 * g-m: machine implementation dependent drives
1226 * n-p: network devices
1227 * It's up to each machine implementation to check if the given boot
1228 * devices match the actual hardware implementation and firmware
1229 * features.
1231 if (*p < 'a' || *p > 'p') {
1232 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1233 exit(1);
1235 if (bitmap & (1 << (*p - 'a'))) {
1236 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1237 exit(1);
1239 bitmap |= 1 << (*p - 'a');
1243 static void restore_boot_order(void *opaque)
1245 char *normal_boot_order = opaque;
1246 static int first = 1;
1248 /* Restore boot order and remove ourselves after the first boot */
1249 if (first) {
1250 first = 0;
1251 return;
1254 qemu_boot_set(normal_boot_order);
1256 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1257 g_free(normal_boot_order);
1260 static QemuOptsList qemu_smp_opts = {
1261 .name = "smp-opts",
1262 .implied_opt_name = "cpus",
1263 .merge_lists = true,
1264 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1265 .desc = {
1267 .name = "cpus",
1268 .type = QEMU_OPT_NUMBER,
1269 }, {
1270 .name = "sockets",
1271 .type = QEMU_OPT_NUMBER,
1272 }, {
1273 .name = "cores",
1274 .type = QEMU_OPT_NUMBER,
1275 }, {
1276 .name = "threads",
1277 .type = QEMU_OPT_NUMBER,
1278 }, {
1279 .name = "maxcpus",
1280 .type = QEMU_OPT_NUMBER,
1282 { /*End of list */ }
1286 static void smp_parse(QemuOpts *opts)
1288 if (opts) {
1290 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1291 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1292 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1293 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1295 /* compute missing values, prefer sockets over cores over threads */
1296 if (cpus == 0 || sockets == 0) {
1297 sockets = sockets > 0 ? sockets : 1;
1298 cores = cores > 0 ? cores : 1;
1299 threads = threads > 0 ? threads : 1;
1300 if (cpus == 0) {
1301 cpus = cores * threads * sockets;
1303 } else {
1304 if (cores == 0) {
1305 threads = threads > 0 ? threads : 1;
1306 cores = cpus / (sockets * threads);
1307 } else {
1308 threads = cpus / (cores * sockets);
1312 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1314 smp_cpus = cpus;
1315 smp_cores = cores > 0 ? cores : 1;
1316 smp_threads = threads > 0 ? threads : 1;
1320 if (max_cpus == 0) {
1321 max_cpus = smp_cpus;
1324 if (max_cpus > MAX_CPUMASK_BITS) {
1325 fprintf(stderr, "Unsupported number of maxcpus\n");
1326 exit(1);
1328 if (max_cpus < smp_cpus) {
1329 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1330 exit(1);
1335 static void realtime_init(void)
1337 if (enable_mlock) {
1338 if (os_mlock() < 0) {
1339 fprintf(stderr, "qemu: locking memory failed\n");
1340 exit(1);
1346 static void configure_msg(QemuOpts *opts)
1348 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1351 /***********************************************************/
1352 /* USB devices */
1354 static int usb_device_add(const char *devname)
1356 USBDevice *dev = NULL;
1357 #ifndef CONFIG_LINUX
1358 const char *p;
1359 #endif
1361 if (!usb_enabled(false)) {
1362 return -1;
1365 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1366 dev = usbdevice_create(devname);
1367 if (dev)
1368 goto done;
1370 /* the other ones */
1371 #ifndef CONFIG_LINUX
1372 /* only the linux version is qdev-ified, usb-bsd still needs this */
1373 if (strstart(devname, "host:", &p)) {
1374 dev = usb_host_device_open(usb_bus_find(-1), p);
1376 #endif
1377 if (!dev)
1378 return -1;
1380 done:
1381 return 0;
1384 static int usb_device_del(const char *devname)
1386 int bus_num, addr;
1387 const char *p;
1389 if (strstart(devname, "host:", &p)) {
1390 return -1;
1393 if (!usb_enabled(false)) {
1394 return -1;
1397 p = strchr(devname, '.');
1398 if (!p)
1399 return -1;
1400 bus_num = strtoul(devname, NULL, 0);
1401 addr = strtoul(p + 1, NULL, 0);
1403 return usb_device_delete_addr(bus_num, addr);
1406 static int usb_parse(const char *cmdline)
1408 int r;
1409 r = usb_device_add(cmdline);
1410 if (r < 0) {
1411 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1413 return r;
1416 void do_usb_add(Monitor *mon, const QDict *qdict)
1418 const char *devname = qdict_get_str(qdict, "devname");
1419 if (usb_device_add(devname) < 0) {
1420 error_report("could not add USB device '%s'", devname);
1424 void do_usb_del(Monitor *mon, const QDict *qdict)
1426 const char *devname = qdict_get_str(qdict, "devname");
1427 if (usb_device_del(devname) < 0) {
1428 error_report("could not delete USB device '%s'", devname);
1432 /***********************************************************/
1433 /* machine registration */
1435 MachineState *current_machine;
1437 static void machine_class_init(ObjectClass *oc, void *data)
1439 MachineClass *mc = MACHINE_CLASS(oc);
1440 QEMUMachine *qm = data;
1442 mc->family = qm->family;
1443 mc->name = qm->name;
1444 mc->alias = qm->alias;
1445 mc->desc = qm->desc;
1446 mc->init = qm->init;
1447 mc->reset = qm->reset;
1448 mc->hot_add_cpu = qm->hot_add_cpu;
1449 mc->kvm_type = qm->kvm_type;
1450 mc->block_default_type = qm->block_default_type;
1451 mc->units_per_default_bus = qm->units_per_default_bus;
1452 mc->max_cpus = qm->max_cpus;
1453 mc->no_serial = qm->no_serial;
1454 mc->no_parallel = qm->no_parallel;
1455 mc->use_virtcon = qm->use_virtcon;
1456 mc->use_sclp = qm->use_sclp;
1457 mc->no_floppy = qm->no_floppy;
1458 mc->no_cdrom = qm->no_cdrom;
1459 mc->no_sdcard = qm->no_sdcard;
1460 mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1461 mc->is_default = qm->is_default;
1462 mc->default_machine_opts = qm->default_machine_opts;
1463 mc->default_boot_order = qm->default_boot_order;
1464 mc->default_display = qm->default_display;
1465 mc->compat_props = qm->compat_props;
1466 mc->hw_version = qm->hw_version;
1469 int qemu_register_machine(QEMUMachine *m)
1471 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1472 TypeInfo ti = {
1473 .name = name,
1474 .parent = TYPE_MACHINE,
1475 .class_init = machine_class_init,
1476 .class_data = (void *)m,
1479 type_register(&ti);
1480 g_free(name);
1482 return 0;
1485 static MachineClass *find_machine(const char *name)
1487 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1488 MachineClass *mc = NULL;
1490 for (el = machines; el; el = el->next) {
1491 MachineClass *temp = el->data;
1493 if (!strcmp(temp->name, name)) {
1494 mc = temp;
1495 break;
1497 if (temp->alias &&
1498 !strcmp(temp->alias, name)) {
1499 mc = temp;
1500 break;
1504 g_slist_free(machines);
1505 return mc;
1508 MachineClass *find_default_machine(void)
1510 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1511 MachineClass *mc = NULL;
1513 for (el = machines; el; el = el->next) {
1514 MachineClass *temp = el->data;
1516 if (temp->is_default) {
1517 mc = temp;
1518 break;
1522 g_slist_free(machines);
1523 return mc;
1526 MachineInfoList *qmp_query_machines(Error **errp)
1528 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1529 MachineInfoList *mach_list = NULL;
1531 for (el = machines; el; el = el->next) {
1532 MachineClass *mc = el->data;
1533 MachineInfoList *entry;
1534 MachineInfo *info;
1536 info = g_malloc0(sizeof(*info));
1537 if (mc->is_default) {
1538 info->has_is_default = true;
1539 info->is_default = true;
1542 if (mc->alias) {
1543 info->has_alias = true;
1544 info->alias = g_strdup(mc->alias);
1547 info->name = g_strdup(mc->name);
1548 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1550 entry = g_malloc0(sizeof(*entry));
1551 entry->value = info;
1552 entry->next = mach_list;
1553 mach_list = entry;
1556 g_slist_free(machines);
1557 return mach_list;
1560 /***********************************************************/
1561 /* main execution loop */
1563 struct vm_change_state_entry {
1564 VMChangeStateHandler *cb;
1565 void *opaque;
1566 QLIST_ENTRY (vm_change_state_entry) entries;
1569 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1571 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1572 void *opaque)
1574 VMChangeStateEntry *e;
1576 e = g_malloc0(sizeof (*e));
1578 e->cb = cb;
1579 e->opaque = opaque;
1580 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1581 return e;
1584 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1586 QLIST_REMOVE (e, entries);
1587 g_free (e);
1590 void vm_state_notify(int running, RunState state)
1592 VMChangeStateEntry *e, *next;
1594 trace_vm_state_notify(running, state);
1596 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1597 e->cb(e->opaque, running, state);
1601 /* reset/shutdown handler */
1603 typedef struct QEMUResetEntry {
1604 QTAILQ_ENTRY(QEMUResetEntry) entry;
1605 QEMUResetHandler *func;
1606 void *opaque;
1607 } QEMUResetEntry;
1609 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1610 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1611 static int reset_requested;
1612 static int shutdown_requested, shutdown_signal = -1;
1613 static pid_t shutdown_pid;
1614 static int powerdown_requested;
1615 static int debug_requested;
1616 static int suspend_requested;
1617 static WakeupReason wakeup_reason;
1618 static NotifierList powerdown_notifiers =
1619 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1620 static NotifierList suspend_notifiers =
1621 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1622 static NotifierList wakeup_notifiers =
1623 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1624 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1626 int qemu_shutdown_requested_get(void)
1628 return shutdown_requested;
1631 int qemu_reset_requested_get(void)
1633 return reset_requested;
1636 static int qemu_shutdown_requested(void)
1638 return atomic_xchg(&shutdown_requested, 0);
1641 static void qemu_kill_report(void)
1643 if (!qtest_driver() && shutdown_signal != -1) {
1644 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1645 if (shutdown_pid == 0) {
1646 /* This happens for eg ^C at the terminal, so it's worth
1647 * avoiding printing an odd message in that case.
1649 fputc('\n', stderr);
1650 } else {
1651 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1653 shutdown_signal = -1;
1657 static int qemu_reset_requested(void)
1659 int r = reset_requested;
1660 reset_requested = 0;
1661 return r;
1664 static int qemu_suspend_requested(void)
1666 int r = suspend_requested;
1667 suspend_requested = 0;
1668 return r;
1671 static WakeupReason qemu_wakeup_requested(void)
1673 return wakeup_reason;
1676 static int qemu_powerdown_requested(void)
1678 int r = powerdown_requested;
1679 powerdown_requested = 0;
1680 return r;
1683 static int qemu_debug_requested(void)
1685 int r = debug_requested;
1686 debug_requested = 0;
1687 return r;
1690 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1692 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1694 re->func = func;
1695 re->opaque = opaque;
1696 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1699 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1701 QEMUResetEntry *re;
1703 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1704 if (re->func == func && re->opaque == opaque) {
1705 QTAILQ_REMOVE(&reset_handlers, re, entry);
1706 g_free(re);
1707 return;
1712 void qemu_devices_reset(void)
1714 QEMUResetEntry *re, *nre;
1716 /* reset all devices */
1717 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1718 re->func(re->opaque);
1722 void qemu_system_reset(bool report)
1724 MachineClass *mc;
1726 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1728 if (mc && mc->reset) {
1729 mc->reset();
1730 } else {
1731 qemu_devices_reset();
1733 if (report) {
1734 qapi_event_send_reset(&error_abort);
1736 cpu_synchronize_all_post_reset();
1739 void qemu_system_reset_request(void)
1741 if (no_reboot) {
1742 shutdown_requested = 1;
1743 } else {
1744 reset_requested = 1;
1746 cpu_stop_current();
1747 qemu_notify_event();
1750 static void qemu_system_suspend(void)
1752 pause_all_vcpus();
1753 notifier_list_notify(&suspend_notifiers, NULL);
1754 runstate_set(RUN_STATE_SUSPENDED);
1755 qapi_event_send_suspend(&error_abort);
1758 void qemu_system_suspend_request(void)
1760 if (runstate_check(RUN_STATE_SUSPENDED)) {
1761 return;
1763 suspend_requested = 1;
1764 cpu_stop_current();
1765 qemu_notify_event();
1768 void qemu_register_suspend_notifier(Notifier *notifier)
1770 notifier_list_add(&suspend_notifiers, notifier);
1773 void qemu_system_wakeup_request(WakeupReason reason)
1775 trace_system_wakeup_request(reason);
1777 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1778 return;
1780 if (!(wakeup_reason_mask & (1 << reason))) {
1781 return;
1783 runstate_set(RUN_STATE_RUNNING);
1784 wakeup_reason = reason;
1785 qemu_notify_event();
1788 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1790 if (enabled) {
1791 wakeup_reason_mask |= (1 << reason);
1792 } else {
1793 wakeup_reason_mask &= ~(1 << reason);
1797 void qemu_register_wakeup_notifier(Notifier *notifier)
1799 notifier_list_add(&wakeup_notifiers, notifier);
1802 void qemu_system_killed(int signal, pid_t pid)
1804 shutdown_signal = signal;
1805 shutdown_pid = pid;
1806 no_shutdown = 0;
1807 qemu_system_shutdown_request();
1810 void qemu_system_shutdown_request(void)
1812 trace_qemu_system_shutdown_request();
1813 shutdown_requested = 1;
1814 qemu_notify_event();
1817 static void qemu_system_powerdown(void)
1819 qapi_event_send_powerdown(&error_abort);
1820 notifier_list_notify(&powerdown_notifiers, NULL);
1823 void qemu_system_powerdown_request(void)
1825 trace_qemu_system_powerdown_request();
1826 powerdown_requested = 1;
1827 qemu_notify_event();
1830 void qemu_register_powerdown_notifier(Notifier *notifier)
1832 notifier_list_add(&powerdown_notifiers, notifier);
1835 void qemu_system_debug_request(void)
1837 debug_requested = 1;
1838 qemu_notify_event();
1841 static bool main_loop_should_exit(void)
1843 RunState r;
1844 if (qemu_debug_requested()) {
1845 vm_stop(RUN_STATE_DEBUG);
1847 if (qemu_suspend_requested()) {
1848 qemu_system_suspend();
1850 if (qemu_shutdown_requested()) {
1851 qemu_kill_report();
1852 qapi_event_send_shutdown(&error_abort);
1853 if (no_shutdown) {
1854 vm_stop(RUN_STATE_SHUTDOWN);
1855 } else {
1856 return true;
1859 if (qemu_reset_requested()) {
1860 pause_all_vcpus();
1861 cpu_synchronize_all_states();
1862 qemu_system_reset(VMRESET_REPORT);
1863 resume_all_vcpus();
1864 if (runstate_needs_reset()) {
1865 runstate_set(RUN_STATE_PAUSED);
1868 if (qemu_wakeup_requested()) {
1869 pause_all_vcpus();
1870 cpu_synchronize_all_states();
1871 qemu_system_reset(VMRESET_SILENT);
1872 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1873 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1874 resume_all_vcpus();
1875 qapi_event_send_wakeup(&error_abort);
1877 if (qemu_powerdown_requested()) {
1878 qemu_system_powerdown();
1880 if (qemu_vmstop_requested(&r)) {
1881 vm_stop(r);
1883 return false;
1886 static void main_loop(void)
1888 bool nonblocking;
1889 int last_io = 0;
1890 #ifdef CONFIG_PROFILER
1891 int64_t ti;
1892 #endif
1893 do {
1894 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1895 #ifdef CONFIG_PROFILER
1896 ti = profile_getclock();
1897 #endif
1898 last_io = main_loop_wait(nonblocking);
1899 #ifdef CONFIG_PROFILER
1900 dev_time += profile_getclock() - ti;
1901 #endif
1902 } while (!main_loop_should_exit());
1905 static void version(void)
1907 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1910 static void help(int exitcode)
1912 version();
1913 printf("usage: %s [options] [disk_image]\n\n"
1914 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1915 error_get_progname());
1917 #define QEMU_OPTIONS_GENERATE_HELP
1918 #include "qemu-options-wrapper.h"
1920 printf("\nDuring emulation, the following keys are useful:\n"
1921 "ctrl-alt-f toggle full screen\n"
1922 "ctrl-alt-n switch to virtual console 'n'\n"
1923 "ctrl-alt toggle mouse and keyboard grab\n"
1924 "\n"
1925 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1927 exit(exitcode);
1930 #define HAS_ARG 0x0001
1932 typedef struct QEMUOption {
1933 const char *name;
1934 int flags;
1935 int index;
1936 uint32_t arch_mask;
1937 } QEMUOption;
1939 static const QEMUOption qemu_options[] = {
1940 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1941 #define QEMU_OPTIONS_GENERATE_OPTIONS
1942 #include "qemu-options-wrapper.h"
1943 { NULL },
1946 static bool vga_available(void)
1948 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1951 static bool cirrus_vga_available(void)
1953 return object_class_by_name("cirrus-vga")
1954 || object_class_by_name("isa-cirrus-vga");
1957 static bool vmware_vga_available(void)
1959 return object_class_by_name("vmware-svga");
1962 static bool qxl_vga_available(void)
1964 return object_class_by_name("qxl-vga");
1967 static bool tcx_vga_available(void)
1969 return object_class_by_name("SUNW,tcx");
1972 static bool cg3_vga_available(void)
1974 return object_class_by_name("cgthree");
1977 static void select_vgahw (const char *p)
1979 const char *opts;
1981 assert(vga_interface_type == VGA_NONE);
1982 if (strstart(p, "std", &opts)) {
1983 if (vga_available()) {
1984 vga_interface_type = VGA_STD;
1985 } else {
1986 fprintf(stderr, "Error: standard VGA not available\n");
1987 exit(0);
1989 } else if (strstart(p, "cirrus", &opts)) {
1990 if (cirrus_vga_available()) {
1991 vga_interface_type = VGA_CIRRUS;
1992 } else {
1993 fprintf(stderr, "Error: Cirrus VGA not available\n");
1994 exit(0);
1996 } else if (strstart(p, "vmware", &opts)) {
1997 if (vmware_vga_available()) {
1998 vga_interface_type = VGA_VMWARE;
1999 } else {
2000 fprintf(stderr, "Error: VMWare SVGA not available\n");
2001 exit(0);
2003 } else if (strstart(p, "xenfb", &opts)) {
2004 vga_interface_type = VGA_XENFB;
2005 } else if (strstart(p, "qxl", &opts)) {
2006 if (qxl_vga_available()) {
2007 vga_interface_type = VGA_QXL;
2008 } else {
2009 fprintf(stderr, "Error: QXL VGA not available\n");
2010 exit(0);
2012 } else if (strstart(p, "tcx", &opts)) {
2013 if (tcx_vga_available()) {
2014 vga_interface_type = VGA_TCX;
2015 } else {
2016 fprintf(stderr, "Error: TCX framebuffer not available\n");
2017 exit(0);
2019 } else if (strstart(p, "cg3", &opts)) {
2020 if (cg3_vga_available()) {
2021 vga_interface_type = VGA_CG3;
2022 } else {
2023 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2024 exit(0);
2026 } else if (!strstart(p, "none", &opts)) {
2027 invalid_vga:
2028 fprintf(stderr, "Unknown vga type: %s\n", p);
2029 exit(1);
2031 while (*opts) {
2032 const char *nextopt;
2034 if (strstart(opts, ",retrace=", &nextopt)) {
2035 opts = nextopt;
2036 if (strstart(opts, "dumb", &nextopt))
2037 vga_retrace_method = VGA_RETRACE_DUMB;
2038 else if (strstart(opts, "precise", &nextopt))
2039 vga_retrace_method = VGA_RETRACE_PRECISE;
2040 else goto invalid_vga;
2041 } else goto invalid_vga;
2042 opts = nextopt;
2046 static DisplayType select_display(const char *p)
2048 const char *opts;
2049 DisplayType display = DT_DEFAULT;
2051 if (strstart(p, "sdl", &opts)) {
2052 #ifdef CONFIG_SDL
2053 display = DT_SDL;
2054 while (*opts) {
2055 const char *nextopt;
2057 if (strstart(opts, ",frame=", &nextopt)) {
2058 opts = nextopt;
2059 if (strstart(opts, "on", &nextopt)) {
2060 no_frame = 0;
2061 } else if (strstart(opts, "off", &nextopt)) {
2062 no_frame = 1;
2063 } else {
2064 goto invalid_sdl_args;
2066 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2067 opts = nextopt;
2068 if (strstart(opts, "on", &nextopt)) {
2069 alt_grab = 1;
2070 } else if (strstart(opts, "off", &nextopt)) {
2071 alt_grab = 0;
2072 } else {
2073 goto invalid_sdl_args;
2075 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2076 opts = nextopt;
2077 if (strstart(opts, "on", &nextopt)) {
2078 ctrl_grab = 1;
2079 } else if (strstart(opts, "off", &nextopt)) {
2080 ctrl_grab = 0;
2081 } else {
2082 goto invalid_sdl_args;
2084 } else if (strstart(opts, ",window_close=", &nextopt)) {
2085 opts = nextopt;
2086 if (strstart(opts, "on", &nextopt)) {
2087 no_quit = 0;
2088 } else if (strstart(opts, "off", &nextopt)) {
2089 no_quit = 1;
2090 } else {
2091 goto invalid_sdl_args;
2093 } else {
2094 invalid_sdl_args:
2095 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2096 exit(1);
2098 opts = nextopt;
2100 #else
2101 fprintf(stderr, "SDL support is disabled\n");
2102 exit(1);
2103 #endif
2104 } else if (strstart(p, "vnc", &opts)) {
2105 #ifdef CONFIG_VNC
2106 display_remote++;
2108 if (*opts) {
2109 const char *nextopt;
2111 if (strstart(opts, "=", &nextopt)) {
2112 vnc_display = nextopt;
2115 if (!vnc_display) {
2116 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2117 exit(1);
2119 #else
2120 fprintf(stderr, "VNC support is disabled\n");
2121 exit(1);
2122 #endif
2123 } else if (strstart(p, "curses", &opts)) {
2124 #ifdef CONFIG_CURSES
2125 display = DT_CURSES;
2126 #else
2127 fprintf(stderr, "Curses support is disabled\n");
2128 exit(1);
2129 #endif
2130 } else if (strstart(p, "gtk", &opts)) {
2131 #ifdef CONFIG_GTK
2132 display = DT_GTK;
2133 while (*opts) {
2134 const char *nextopt;
2136 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2137 opts = nextopt;
2138 if (strstart(opts, "on", &nextopt)) {
2139 grab_on_hover = true;
2140 } else if (strstart(opts, "off", &nextopt)) {
2141 grab_on_hover = false;
2142 } else {
2143 goto invalid_gtk_args;
2145 } else {
2146 invalid_gtk_args:
2147 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2148 exit(1);
2150 opts = nextopt;
2152 #else
2153 fprintf(stderr, "GTK support is disabled\n");
2154 exit(1);
2155 #endif
2156 } else if (strstart(p, "none", &opts)) {
2157 display = DT_NONE;
2158 } else {
2159 fprintf(stderr, "Unknown display type: %s\n", p);
2160 exit(1);
2163 return display;
2166 static int balloon_parse(const char *arg)
2168 QemuOpts *opts;
2170 if (strcmp(arg, "none") == 0) {
2171 return 0;
2174 if (!strncmp(arg, "virtio", 6)) {
2175 if (arg[6] == ',') {
2176 /* have params -> parse them */
2177 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2178 if (!opts)
2179 return -1;
2180 } else {
2181 /* create empty opts */
2182 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2183 &error_abort);
2185 qemu_opt_set(opts, "driver", "virtio-balloon");
2186 return 0;
2189 return -1;
2192 char *qemu_find_file(int type, const char *name)
2194 int i;
2195 const char *subdir;
2196 char *buf;
2198 /* Try the name as a straight path first */
2199 if (access(name, R_OK) == 0) {
2200 trace_load_file(name, name);
2201 return g_strdup(name);
2204 switch (type) {
2205 case QEMU_FILE_TYPE_BIOS:
2206 subdir = "";
2207 break;
2208 case QEMU_FILE_TYPE_KEYMAP:
2209 subdir = "keymaps/";
2210 break;
2211 default:
2212 abort();
2215 for (i = 0; i < data_dir_idx; i++) {
2216 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2217 if (access(buf, R_OK) == 0) {
2218 trace_load_file(name, buf);
2219 return buf;
2221 g_free(buf);
2223 return NULL;
2226 static int device_help_func(QemuOpts *opts, void *opaque)
2228 return qdev_device_help(opts);
2231 static int device_init_func(QemuOpts *opts, void *opaque)
2233 DeviceState *dev;
2235 dev = qdev_device_add(opts);
2236 if (!dev)
2237 return -1;
2238 object_unref(OBJECT(dev));
2239 return 0;
2242 static int chardev_init_func(QemuOpts *opts, void *opaque)
2244 Error *local_err = NULL;
2246 qemu_chr_new_from_opts(opts, NULL, &local_err);
2247 if (local_err) {
2248 error_report("%s", error_get_pretty(local_err));
2249 error_free(local_err);
2250 return -1;
2252 return 0;
2255 #ifdef CONFIG_VIRTFS
2256 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2258 int ret;
2259 ret = qemu_fsdev_add(opts);
2261 return ret;
2263 #endif
2265 static int mon_init_func(QemuOpts *opts, void *opaque)
2267 CharDriverState *chr;
2268 const char *chardev;
2269 const char *mode;
2270 int flags;
2272 mode = qemu_opt_get(opts, "mode");
2273 if (mode == NULL) {
2274 mode = "readline";
2276 if (strcmp(mode, "readline") == 0) {
2277 flags = MONITOR_USE_READLINE;
2278 } else if (strcmp(mode, "control") == 0) {
2279 flags = MONITOR_USE_CONTROL;
2280 } else {
2281 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2282 exit(1);
2285 if (qemu_opt_get_bool(opts, "pretty", 0))
2286 flags |= MONITOR_USE_PRETTY;
2288 if (qemu_opt_get_bool(opts, "default", 0))
2289 flags |= MONITOR_IS_DEFAULT;
2291 chardev = qemu_opt_get(opts, "chardev");
2292 chr = qemu_chr_find(chardev);
2293 if (chr == NULL) {
2294 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2295 exit(1);
2298 qemu_chr_fe_claim_no_fail(chr);
2299 monitor_init(chr, flags);
2300 return 0;
2303 static void monitor_parse(const char *optarg, const char *mode)
2305 static int monitor_device_index = 0;
2306 QemuOpts *opts;
2307 const char *p;
2308 char label[32];
2309 int def = 0;
2311 if (strstart(optarg, "chardev:", &p)) {
2312 snprintf(label, sizeof(label), "%s", p);
2313 } else {
2314 snprintf(label, sizeof(label), "compat_monitor%d",
2315 monitor_device_index);
2316 if (monitor_device_index == 0) {
2317 def = 1;
2319 opts = qemu_chr_parse_compat(label, optarg);
2320 if (!opts) {
2321 fprintf(stderr, "parse error: %s\n", optarg);
2322 exit(1);
2326 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2327 if (!opts) {
2328 fprintf(stderr, "duplicate chardev: %s\n", label);
2329 exit(1);
2331 qemu_opt_set(opts, "mode", mode);
2332 qemu_opt_set(opts, "chardev", label);
2333 if (def)
2334 qemu_opt_set(opts, "default", "on");
2335 monitor_device_index++;
2338 struct device_config {
2339 enum {
2340 DEV_USB, /* -usbdevice */
2341 DEV_BT, /* -bt */
2342 DEV_SERIAL, /* -serial */
2343 DEV_PARALLEL, /* -parallel */
2344 DEV_VIRTCON, /* -virtioconsole */
2345 DEV_DEBUGCON, /* -debugcon */
2346 DEV_GDB, /* -gdb, -s */
2347 DEV_SCLP, /* s390 sclp */
2348 } type;
2349 const char *cmdline;
2350 Location loc;
2351 QTAILQ_ENTRY(device_config) next;
2354 static QTAILQ_HEAD(, device_config) device_configs =
2355 QTAILQ_HEAD_INITIALIZER(device_configs);
2357 static void add_device_config(int type, const char *cmdline)
2359 struct device_config *conf;
2361 conf = g_malloc0(sizeof(*conf));
2362 conf->type = type;
2363 conf->cmdline = cmdline;
2364 loc_save(&conf->loc);
2365 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2368 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2370 struct device_config *conf;
2371 int rc;
2373 QTAILQ_FOREACH(conf, &device_configs, next) {
2374 if (conf->type != type)
2375 continue;
2376 loc_push_restore(&conf->loc);
2377 rc = func(conf->cmdline);
2378 loc_pop(&conf->loc);
2379 if (rc) {
2380 return rc;
2383 return 0;
2386 static int serial_parse(const char *devname)
2388 static int index = 0;
2389 char label[32];
2391 if (strcmp(devname, "none") == 0)
2392 return 0;
2393 if (index == MAX_SERIAL_PORTS) {
2394 fprintf(stderr, "qemu: too many serial ports\n");
2395 exit(1);
2397 snprintf(label, sizeof(label), "serial%d", index);
2398 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2399 if (!serial_hds[index]) {
2400 fprintf(stderr, "qemu: could not connect serial device"
2401 " to character backend '%s'\n", devname);
2402 return -1;
2404 index++;
2405 return 0;
2408 static int parallel_parse(const char *devname)
2410 static int index = 0;
2411 char label[32];
2413 if (strcmp(devname, "none") == 0)
2414 return 0;
2415 if (index == MAX_PARALLEL_PORTS) {
2416 fprintf(stderr, "qemu: too many parallel ports\n");
2417 exit(1);
2419 snprintf(label, sizeof(label), "parallel%d", index);
2420 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2421 if (!parallel_hds[index]) {
2422 fprintf(stderr, "qemu: could not connect parallel device"
2423 " to character backend '%s'\n", devname);
2424 return -1;
2426 index++;
2427 return 0;
2430 static int virtcon_parse(const char *devname)
2432 QemuOptsList *device = qemu_find_opts("device");
2433 static int index = 0;
2434 char label[32];
2435 QemuOpts *bus_opts, *dev_opts;
2437 if (strcmp(devname, "none") == 0)
2438 return 0;
2439 if (index == MAX_VIRTIO_CONSOLES) {
2440 fprintf(stderr, "qemu: too many virtio consoles\n");
2441 exit(1);
2444 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2445 if (arch_type == QEMU_ARCH_S390X) {
2446 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2447 } else {
2448 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2451 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2452 qemu_opt_set(dev_opts, "driver", "virtconsole");
2454 snprintf(label, sizeof(label), "virtcon%d", index);
2455 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2456 if (!virtcon_hds[index]) {
2457 fprintf(stderr, "qemu: could not connect virtio console"
2458 " to character backend '%s'\n", devname);
2459 return -1;
2461 qemu_opt_set(dev_opts, "chardev", label);
2463 index++;
2464 return 0;
2467 static int sclp_parse(const char *devname)
2469 QemuOptsList *device = qemu_find_opts("device");
2470 static int index = 0;
2471 char label[32];
2472 QemuOpts *dev_opts;
2474 if (strcmp(devname, "none") == 0) {
2475 return 0;
2477 if (index == MAX_SCLP_CONSOLES) {
2478 fprintf(stderr, "qemu: too many sclp consoles\n");
2479 exit(1);
2482 assert(arch_type == QEMU_ARCH_S390X);
2484 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2485 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2487 snprintf(label, sizeof(label), "sclpcon%d", index);
2488 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2489 if (!sclp_hds[index]) {
2490 fprintf(stderr, "qemu: could not connect sclp console"
2491 " to character backend '%s'\n", devname);
2492 return -1;
2494 qemu_opt_set(dev_opts, "chardev", label);
2496 index++;
2497 return 0;
2500 static int debugcon_parse(const char *devname)
2502 QemuOpts *opts;
2504 if (!qemu_chr_new("debugcon", devname, NULL)) {
2505 exit(1);
2507 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2508 if (!opts) {
2509 fprintf(stderr, "qemu: already have a debugcon device\n");
2510 exit(1);
2512 qemu_opt_set(opts, "driver", "isa-debugcon");
2513 qemu_opt_set(opts, "chardev", "debugcon");
2514 return 0;
2517 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2519 const MachineClass *mc1 = a, *mc2 = b;
2520 int res;
2522 if (mc1->family == NULL) {
2523 if (mc2->family == NULL) {
2524 /* Compare standalone machine types against each other; they sort
2525 * in increasing order.
2527 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2528 object_class_get_name(OBJECT_CLASS(mc2)));
2531 /* Standalone machine types sort after families. */
2532 return 1;
2535 if (mc2->family == NULL) {
2536 /* Families sort before standalone machine types. */
2537 return -1;
2540 /* Families sort between each other alphabetically increasingly. */
2541 res = strcmp(mc1->family, mc2->family);
2542 if (res != 0) {
2543 return res;
2546 /* Within the same family, machine types sort in decreasing order. */
2547 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2548 object_class_get_name(OBJECT_CLASS(mc1)));
2551 static MachineClass *machine_parse(const char *name)
2553 MachineClass *mc = NULL;
2554 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2556 if (name) {
2557 mc = find_machine(name);
2559 if (mc) {
2560 return mc;
2562 if (name && !is_help_option(name)) {
2563 error_report("Unsupported machine type");
2564 error_printf("Use -machine help to list supported machines!\n");
2565 } else {
2566 printf("Supported machines are:\n");
2567 machines = g_slist_sort(machines, machine_class_cmp);
2568 for (el = machines; el; el = el->next) {
2569 MachineClass *mc = el->data;
2570 if (mc->alias) {
2571 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2573 printf("%-20s %s%s\n", mc->name, mc->desc,
2574 mc->is_default ? " (default)" : "");
2578 g_slist_free(machines);
2579 exit(!name || !is_help_option(name));
2582 void qemu_add_exit_notifier(Notifier *notify)
2584 notifier_list_add(&exit_notifiers, notify);
2587 void qemu_remove_exit_notifier(Notifier *notify)
2589 notifier_remove(notify);
2592 static void qemu_run_exit_notifiers(void)
2594 notifier_list_notify(&exit_notifiers, NULL);
2597 void qemu_add_machine_init_done_notifier(Notifier *notify)
2599 notifier_list_add(&machine_init_done_notifiers, notify);
2602 static void qemu_run_machine_init_done_notifiers(void)
2604 notifier_list_notify(&machine_init_done_notifiers, NULL);
2607 static const QEMUOption *lookup_opt(int argc, char **argv,
2608 const char **poptarg, int *poptind)
2610 const QEMUOption *popt;
2611 int optind = *poptind;
2612 char *r = argv[optind];
2613 const char *optarg;
2615 loc_set_cmdline(argv, optind, 1);
2616 optind++;
2617 /* Treat --foo the same as -foo. */
2618 if (r[1] == '-')
2619 r++;
2620 popt = qemu_options;
2621 for(;;) {
2622 if (!popt->name) {
2623 error_report("invalid option");
2624 exit(1);
2626 if (!strcmp(popt->name, r + 1))
2627 break;
2628 popt++;
2630 if (popt->flags & HAS_ARG) {
2631 if (optind >= argc) {
2632 error_report("requires an argument");
2633 exit(1);
2635 optarg = argv[optind++];
2636 loc_set_cmdline(argv, optind - 2, 2);
2637 } else {
2638 optarg = NULL;
2641 *poptarg = optarg;
2642 *poptind = optind;
2644 return popt;
2647 static gpointer malloc_and_trace(gsize n_bytes)
2649 void *ptr = malloc(n_bytes);
2650 trace_g_malloc(n_bytes, ptr);
2651 return ptr;
2654 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2656 void *ptr = realloc(mem, n_bytes);
2657 trace_g_realloc(mem, n_bytes, ptr);
2658 return ptr;
2661 static void free_and_trace(gpointer mem)
2663 trace_g_free(mem);
2664 free(mem);
2667 static int machine_set_property(const char *name, const char *value,
2668 void *opaque)
2670 Object *obj = OBJECT(opaque);
2671 StringInputVisitor *siv;
2672 Error *local_err = NULL;
2673 char *c, *qom_name;
2675 if (strcmp(name, "type") == 0) {
2676 return 0;
2679 qom_name = g_strdup(name);
2680 c = qom_name;
2681 while (*c++) {
2682 if (*c == '_') {
2683 *c = '-';
2687 siv = string_input_visitor_new(value);
2688 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2689 string_input_visitor_cleanup(siv);
2690 g_free(qom_name);
2692 if (local_err) {
2693 qerror_report_err(local_err);
2694 error_free(local_err);
2695 return -1;
2698 return 0;
2701 static int object_create(QemuOpts *opts, void *opaque)
2703 Error *err = NULL;
2704 char *type = NULL;
2705 char *id = NULL;
2706 void *dummy = NULL;
2707 OptsVisitor *ov;
2708 QDict *pdict;
2710 ov = opts_visitor_new(opts);
2711 pdict = qemu_opts_to_qdict(opts, NULL);
2713 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2714 if (err) {
2715 goto out;
2718 qdict_del(pdict, "qom-type");
2719 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2720 if (err) {
2721 goto out;
2724 qdict_del(pdict, "id");
2725 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2726 if (err) {
2727 goto out;
2730 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2731 if (err) {
2732 goto out;
2734 visit_end_struct(opts_get_visitor(ov), &err);
2735 if (err) {
2736 qmp_object_del(id, NULL);
2739 out:
2740 opts_visitor_cleanup(ov);
2742 QDECREF(pdict);
2743 g_free(id);
2744 g_free(type);
2745 g_free(dummy);
2746 if (err) {
2747 qerror_report_err(err);
2748 error_free(err);
2749 return -1;
2751 return 0;
2754 int main(int argc, char **argv, char **envp)
2756 int i;
2757 int snapshot, linux_boot;
2758 const char *initrd_filename;
2759 const char *kernel_filename, *kernel_cmdline;
2760 const char *boot_order;
2761 DisplayState *ds;
2762 int cyls, heads, secs, translation;
2763 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2764 QemuOptsList *olist;
2765 int optind;
2766 const char *optarg;
2767 const char *loadvm = NULL;
2768 MachineClass *machine_class;
2769 const char *cpu_model;
2770 const char *vga_model = NULL;
2771 const char *qtest_chrdev = NULL;
2772 const char *qtest_log = NULL;
2773 const char *pid_file = NULL;
2774 const char *incoming = NULL;
2775 #ifdef CONFIG_VNC
2776 int show_vnc_port = 0;
2777 #endif
2778 bool defconfig = true;
2779 bool userconfig = true;
2780 const char *log_mask = NULL;
2781 const char *log_file = NULL;
2782 GMemVTable mem_trace = {
2783 .malloc = malloc_and_trace,
2784 .realloc = realloc_and_trace,
2785 .free = free_and_trace,
2787 const char *trace_events = NULL;
2788 const char *trace_file = NULL;
2789 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2790 1024 * 1024;
2791 ram_addr_t maxram_size = default_ram_size;
2792 uint64_t ram_slots = 0;
2793 FILE *vmstate_dump_file = NULL;
2794 Error *main_loop_err = NULL;
2796 atexit(qemu_run_exit_notifiers);
2797 error_set_progname(argv[0]);
2798 qemu_init_exec_dir(argv[0]);
2800 g_mem_set_vtable(&mem_trace);
2802 module_call_init(MODULE_INIT_QOM);
2804 qemu_add_opts(&qemu_drive_opts);
2805 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2806 qemu_add_drive_opts(&qemu_common_drive_opts);
2807 qemu_add_drive_opts(&qemu_drive_opts);
2808 qemu_add_opts(&qemu_chardev_opts);
2809 qemu_add_opts(&qemu_device_opts);
2810 qemu_add_opts(&qemu_netdev_opts);
2811 qemu_add_opts(&qemu_net_opts);
2812 qemu_add_opts(&qemu_rtc_opts);
2813 qemu_add_opts(&qemu_global_opts);
2814 qemu_add_opts(&qemu_mon_opts);
2815 qemu_add_opts(&qemu_trace_opts);
2816 qemu_add_opts(&qemu_option_rom_opts);
2817 qemu_add_opts(&qemu_machine_opts);
2818 qemu_add_opts(&qemu_mem_opts);
2819 qemu_add_opts(&qemu_smp_opts);
2820 qemu_add_opts(&qemu_boot_opts);
2821 qemu_add_opts(&qemu_sandbox_opts);
2822 qemu_add_opts(&qemu_add_fd_opts);
2823 qemu_add_opts(&qemu_object_opts);
2824 qemu_add_opts(&qemu_tpmdev_opts);
2825 qemu_add_opts(&qemu_realtime_opts);
2826 qemu_add_opts(&qemu_msg_opts);
2827 qemu_add_opts(&qemu_name_opts);
2828 qemu_add_opts(&qemu_numa_opts);
2829 qemu_add_opts(&qemu_icount_opts);
2830 qemu_add_opts(&qemu_semihosting_config_opts);
2832 runstate_init();
2834 rtc_clock = QEMU_CLOCK_HOST;
2836 QLIST_INIT (&vm_change_state_head);
2837 os_setup_early_signal_handling();
2839 module_call_init(MODULE_INIT_MACHINE);
2840 machine_class = find_default_machine();
2841 cpu_model = NULL;
2842 ram_size = default_ram_size;
2843 snapshot = 0;
2844 cyls = heads = secs = 0;
2845 translation = BIOS_ATA_TRANSLATION_AUTO;
2847 for (i = 0; i < MAX_NODES; i++) {
2848 numa_info[i].node_mem = 0;
2849 numa_info[i].present = false;
2850 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2853 nb_numa_nodes = 0;
2854 max_numa_nodeid = 0;
2855 nb_nics = 0;
2857 bdrv_init_with_whitelist();
2859 autostart = 1;
2861 /* first pass of option parsing */
2862 optind = 1;
2863 while (optind < argc) {
2864 if (argv[optind][0] != '-') {
2865 /* disk image */
2866 optind++;
2867 } else {
2868 const QEMUOption *popt;
2870 popt = lookup_opt(argc, argv, &optarg, &optind);
2871 switch (popt->index) {
2872 case QEMU_OPTION_nodefconfig:
2873 defconfig = false;
2874 break;
2875 case QEMU_OPTION_nouserconfig:
2876 userconfig = false;
2877 break;
2882 if (defconfig) {
2883 int ret;
2884 ret = qemu_read_default_config_files(userconfig);
2885 if (ret < 0) {
2886 exit(1);
2890 /* second pass of option parsing */
2891 optind = 1;
2892 for(;;) {
2893 if (optind >= argc)
2894 break;
2895 if (argv[optind][0] != '-') {
2896 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2897 } else {
2898 const QEMUOption *popt;
2900 popt = lookup_opt(argc, argv, &optarg, &optind);
2901 if (!(popt->arch_mask & arch_type)) {
2902 printf("Option %s not supported for this target\n", popt->name);
2903 exit(1);
2905 switch(popt->index) {
2906 case QEMU_OPTION_M:
2907 machine_class = machine_parse(optarg);
2908 break;
2909 case QEMU_OPTION_no_kvm_irqchip: {
2910 olist = qemu_find_opts("machine");
2911 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2912 break;
2914 case QEMU_OPTION_cpu:
2915 /* hw initialization will check this */
2916 cpu_model = optarg;
2917 break;
2918 case QEMU_OPTION_hda:
2920 char buf[256];
2921 if (cyls == 0)
2922 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2923 else
2924 snprintf(buf, sizeof(buf),
2925 "%s,cyls=%d,heads=%d,secs=%d%s",
2926 HD_OPTS , cyls, heads, secs,
2927 translation == BIOS_ATA_TRANSLATION_LBA ?
2928 ",trans=lba" :
2929 translation == BIOS_ATA_TRANSLATION_NONE ?
2930 ",trans=none" : "");
2931 drive_add(IF_DEFAULT, 0, optarg, buf);
2932 break;
2934 case QEMU_OPTION_hdb:
2935 case QEMU_OPTION_hdc:
2936 case QEMU_OPTION_hdd:
2937 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2938 HD_OPTS);
2939 break;
2940 case QEMU_OPTION_drive:
2941 if (drive_def(optarg) == NULL) {
2942 exit(1);
2944 break;
2945 case QEMU_OPTION_set:
2946 if (qemu_set_option(optarg) != 0)
2947 exit(1);
2948 break;
2949 case QEMU_OPTION_global:
2950 if (qemu_global_option(optarg) != 0)
2951 exit(1);
2952 break;
2953 case QEMU_OPTION_mtdblock:
2954 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2955 break;
2956 case QEMU_OPTION_sd:
2957 drive_add(IF_SD, -1, optarg, SD_OPTS);
2958 break;
2959 case QEMU_OPTION_pflash:
2960 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2961 break;
2962 case QEMU_OPTION_snapshot:
2963 snapshot = 1;
2964 break;
2965 case QEMU_OPTION_hdachs:
2967 const char *p;
2968 p = optarg;
2969 cyls = strtol(p, (char **)&p, 0);
2970 if (cyls < 1 || cyls > 16383)
2971 goto chs_fail;
2972 if (*p != ',')
2973 goto chs_fail;
2974 p++;
2975 heads = strtol(p, (char **)&p, 0);
2976 if (heads < 1 || heads > 16)
2977 goto chs_fail;
2978 if (*p != ',')
2979 goto chs_fail;
2980 p++;
2981 secs = strtol(p, (char **)&p, 0);
2982 if (secs < 1 || secs > 63)
2983 goto chs_fail;
2984 if (*p == ',') {
2985 p++;
2986 if (!strcmp(p, "large")) {
2987 translation = BIOS_ATA_TRANSLATION_LARGE;
2988 } else if (!strcmp(p, "rechs")) {
2989 translation = BIOS_ATA_TRANSLATION_RECHS;
2990 } else if (!strcmp(p, "none")) {
2991 translation = BIOS_ATA_TRANSLATION_NONE;
2992 } else if (!strcmp(p, "lba")) {
2993 translation = BIOS_ATA_TRANSLATION_LBA;
2994 } else if (!strcmp(p, "auto")) {
2995 translation = BIOS_ATA_TRANSLATION_AUTO;
2996 } else {
2997 goto chs_fail;
2999 } else if (*p != '\0') {
3000 chs_fail:
3001 fprintf(stderr, "qemu: invalid physical CHS format\n");
3002 exit(1);
3004 if (hda_opts != NULL) {
3005 char num[16];
3006 snprintf(num, sizeof(num), "%d", cyls);
3007 qemu_opt_set(hda_opts, "cyls", num);
3008 snprintf(num, sizeof(num), "%d", heads);
3009 qemu_opt_set(hda_opts, "heads", num);
3010 snprintf(num, sizeof(num), "%d", secs);
3011 qemu_opt_set(hda_opts, "secs", num);
3012 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3013 qemu_opt_set(hda_opts, "trans", "large");
3014 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3015 qemu_opt_set(hda_opts, "trans", "rechs");
3016 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3017 qemu_opt_set(hda_opts, "trans", "lba");
3018 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3019 qemu_opt_set(hda_opts, "trans", "none");
3023 break;
3024 case QEMU_OPTION_numa:
3025 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3026 if (!opts) {
3027 exit(1);
3029 break;
3030 case QEMU_OPTION_display:
3031 display_type = select_display(optarg);
3032 break;
3033 case QEMU_OPTION_nographic:
3034 display_type = DT_NOGRAPHIC;
3035 break;
3036 case QEMU_OPTION_curses:
3037 #ifdef CONFIG_CURSES
3038 display_type = DT_CURSES;
3039 #else
3040 fprintf(stderr, "Curses support is disabled\n");
3041 exit(1);
3042 #endif
3043 break;
3044 case QEMU_OPTION_portrait:
3045 graphic_rotate = 90;
3046 break;
3047 case QEMU_OPTION_rotate:
3048 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3049 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3050 graphic_rotate != 180 && graphic_rotate != 270) {
3051 fprintf(stderr,
3052 "qemu: only 90, 180, 270 deg rotation is available\n");
3053 exit(1);
3055 break;
3056 case QEMU_OPTION_kernel:
3057 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3058 break;
3059 case QEMU_OPTION_initrd:
3060 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3061 break;
3062 case QEMU_OPTION_append:
3063 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3064 break;
3065 case QEMU_OPTION_dtb:
3066 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3067 break;
3068 case QEMU_OPTION_cdrom:
3069 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3070 break;
3071 case QEMU_OPTION_boot:
3072 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3073 if (!opts) {
3074 exit(1);
3076 break;
3077 case QEMU_OPTION_fda:
3078 case QEMU_OPTION_fdb:
3079 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3080 optarg, FD_OPTS);
3081 break;
3082 case QEMU_OPTION_no_fd_bootchk:
3083 fd_bootchk = 0;
3084 break;
3085 case QEMU_OPTION_netdev:
3086 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3087 exit(1);
3089 break;
3090 case QEMU_OPTION_net:
3091 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3092 exit(1);
3094 break;
3095 #ifdef CONFIG_LIBISCSI
3096 case QEMU_OPTION_iscsi:
3097 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3098 if (!opts) {
3099 exit(1);
3101 break;
3102 #endif
3103 #ifdef CONFIG_SLIRP
3104 case QEMU_OPTION_tftp:
3105 legacy_tftp_prefix = optarg;
3106 break;
3107 case QEMU_OPTION_bootp:
3108 legacy_bootp_filename = optarg;
3109 break;
3110 case QEMU_OPTION_redir:
3111 if (net_slirp_redir(optarg) < 0)
3112 exit(1);
3113 break;
3114 #endif
3115 case QEMU_OPTION_bt:
3116 add_device_config(DEV_BT, optarg);
3117 break;
3118 case QEMU_OPTION_audio_help:
3119 AUD_help ();
3120 exit (0);
3121 break;
3122 case QEMU_OPTION_soundhw:
3123 select_soundhw (optarg);
3124 break;
3125 case QEMU_OPTION_h:
3126 help(0);
3127 break;
3128 case QEMU_OPTION_version:
3129 version();
3130 exit(0);
3131 break;
3132 case QEMU_OPTION_m: {
3133 uint64_t sz;
3134 const char *mem_str;
3135 const char *maxmem_str, *slots_str;
3137 opts = qemu_opts_parse(qemu_find_opts("memory"),
3138 optarg, 1);
3139 if (!opts) {
3140 exit(EXIT_FAILURE);
3143 mem_str = qemu_opt_get(opts, "size");
3144 if (!mem_str) {
3145 error_report("invalid -m option, missing 'size' option");
3146 exit(EXIT_FAILURE);
3148 if (!*mem_str) {
3149 error_report("missing 'size' option value");
3150 exit(EXIT_FAILURE);
3153 sz = qemu_opt_get_size(opts, "size", ram_size);
3155 /* Fix up legacy suffix-less format */
3156 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3157 uint64_t overflow_check = sz;
3159 sz <<= 20;
3160 if ((sz >> 20) != overflow_check) {
3161 error_report("too large 'size' option value");
3162 exit(EXIT_FAILURE);
3166 /* backward compatibility behaviour for case "-m 0" */
3167 if (sz == 0) {
3168 sz = default_ram_size;
3171 sz = QEMU_ALIGN_UP(sz, 8192);
3172 ram_size = sz;
3173 if (ram_size != sz) {
3174 error_report("ram size too large");
3175 exit(EXIT_FAILURE);
3177 maxram_size = ram_size;
3179 maxmem_str = qemu_opt_get(opts, "maxmem");
3180 slots_str = qemu_opt_get(opts, "slots");
3181 if (maxmem_str && slots_str) {
3182 uint64_t slots;
3184 sz = qemu_opt_get_size(opts, "maxmem", 0);
3185 if (sz < ram_size) {
3186 error_report("invalid -m option value: maxmem "
3187 "(0x%" PRIx64 ") <= initial memory (0x"
3188 RAM_ADDR_FMT ")", sz, ram_size);
3189 exit(EXIT_FAILURE);
3192 slots = qemu_opt_get_number(opts, "slots", 0);
3193 if ((sz > ram_size) && !slots) {
3194 error_report("invalid -m option value: maxmem "
3195 "(0x%" PRIx64 ") more than initial memory (0x"
3196 RAM_ADDR_FMT ") but no hotplug slots where "
3197 "specified", sz, ram_size);
3198 exit(EXIT_FAILURE);
3201 if ((sz <= ram_size) && slots) {
3202 error_report("invalid -m option value: %"
3203 PRIu64 " hotplug slots where specified but "
3204 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3205 RAM_ADDR_FMT ")", slots, sz, ram_size);
3206 exit(EXIT_FAILURE);
3208 maxram_size = sz;
3209 ram_slots = slots;
3210 } else if ((!maxmem_str && slots_str) ||
3211 (maxmem_str && !slots_str)) {
3212 error_report("invalid -m option value: missing "
3213 "'%s' option", slots_str ? "maxmem" : "slots");
3214 exit(EXIT_FAILURE);
3216 break;
3218 #ifdef CONFIG_TPM
3219 case QEMU_OPTION_tpmdev:
3220 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3221 exit(1);
3223 break;
3224 #endif
3225 case QEMU_OPTION_mempath:
3226 mem_path = optarg;
3227 break;
3228 case QEMU_OPTION_mem_prealloc:
3229 mem_prealloc = 1;
3230 break;
3231 case QEMU_OPTION_d:
3232 log_mask = optarg;
3233 break;
3234 case QEMU_OPTION_D:
3235 log_file = optarg;
3236 break;
3237 case QEMU_OPTION_s:
3238 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3239 break;
3240 case QEMU_OPTION_gdb:
3241 add_device_config(DEV_GDB, optarg);
3242 break;
3243 case QEMU_OPTION_L:
3244 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3245 data_dir[data_dir_idx++] = optarg;
3247 break;
3248 case QEMU_OPTION_bios:
3249 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3250 break;
3251 case QEMU_OPTION_singlestep:
3252 singlestep = 1;
3253 break;
3254 case QEMU_OPTION_S:
3255 autostart = 0;
3256 break;
3257 case QEMU_OPTION_k:
3258 keyboard_layout = optarg;
3259 break;
3260 case QEMU_OPTION_localtime:
3261 rtc_utc = 0;
3262 break;
3263 case QEMU_OPTION_vga:
3264 vga_model = optarg;
3265 default_vga = 0;
3266 break;
3267 case QEMU_OPTION_g:
3269 const char *p;
3270 int w, h, depth;
3271 p = optarg;
3272 w = strtol(p, (char **)&p, 10);
3273 if (w <= 0) {
3274 graphic_error:
3275 fprintf(stderr, "qemu: invalid resolution or depth\n");
3276 exit(1);
3278 if (*p != 'x')
3279 goto graphic_error;
3280 p++;
3281 h = strtol(p, (char **)&p, 10);
3282 if (h <= 0)
3283 goto graphic_error;
3284 if (*p == 'x') {
3285 p++;
3286 depth = strtol(p, (char **)&p, 10);
3287 if (depth != 8 && depth != 15 && depth != 16 &&
3288 depth != 24 && depth != 32)
3289 goto graphic_error;
3290 } else if (*p == '\0') {
3291 depth = graphic_depth;
3292 } else {
3293 goto graphic_error;
3296 graphic_width = w;
3297 graphic_height = h;
3298 graphic_depth = depth;
3300 break;
3301 case QEMU_OPTION_echr:
3303 char *r;
3304 term_escape_char = strtol(optarg, &r, 0);
3305 if (r == optarg)
3306 printf("Bad argument to echr\n");
3307 break;
3309 case QEMU_OPTION_monitor:
3310 default_monitor = 0;
3311 if (strncmp(optarg, "none", 4)) {
3312 monitor_parse(optarg, "readline");
3314 break;
3315 case QEMU_OPTION_qmp:
3316 monitor_parse(optarg, "control");
3317 default_monitor = 0;
3318 break;
3319 case QEMU_OPTION_mon:
3320 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3321 if (!opts) {
3322 exit(1);
3324 default_monitor = 0;
3325 break;
3326 case QEMU_OPTION_chardev:
3327 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3328 if (!opts) {
3329 exit(1);
3331 break;
3332 case QEMU_OPTION_fsdev:
3333 olist = qemu_find_opts("fsdev");
3334 if (!olist) {
3335 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3336 exit(1);
3338 opts = qemu_opts_parse(olist, optarg, 1);
3339 if (!opts) {
3340 exit(1);
3342 break;
3343 case QEMU_OPTION_virtfs: {
3344 QemuOpts *fsdev;
3345 QemuOpts *device;
3346 const char *writeout, *sock_fd, *socket;
3348 olist = qemu_find_opts("virtfs");
3349 if (!olist) {
3350 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3351 exit(1);
3353 opts = qemu_opts_parse(olist, optarg, 1);
3354 if (!opts) {
3355 exit(1);
3358 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3359 qemu_opt_get(opts, "mount_tag") == NULL) {
3360 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3361 exit(1);
3363 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3364 qemu_opt_get(opts, "mount_tag"),
3365 1, NULL);
3366 if (!fsdev) {
3367 fprintf(stderr, "duplicate fsdev id: %s\n",
3368 qemu_opt_get(opts, "mount_tag"));
3369 exit(1);
3372 writeout = qemu_opt_get(opts, "writeout");
3373 if (writeout) {
3374 #ifdef CONFIG_SYNC_FILE_RANGE
3375 qemu_opt_set(fsdev, "writeout", writeout);
3376 #else
3377 fprintf(stderr, "writeout=immediate not supported on "
3378 "this platform\n");
3379 exit(1);
3380 #endif
3382 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3383 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3384 qemu_opt_set(fsdev, "security_model",
3385 qemu_opt_get(opts, "security_model"));
3386 socket = qemu_opt_get(opts, "socket");
3387 if (socket) {
3388 qemu_opt_set(fsdev, "socket", socket);
3390 sock_fd = qemu_opt_get(opts, "sock_fd");
3391 if (sock_fd) {
3392 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3395 qemu_opt_set_bool(fsdev, "readonly",
3396 qemu_opt_get_bool(opts, "readonly", 0));
3397 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3398 &error_abort);
3399 qemu_opt_set(device, "driver", "virtio-9p-pci");
3400 qemu_opt_set(device, "fsdev",
3401 qemu_opt_get(opts, "mount_tag"));
3402 qemu_opt_set(device, "mount_tag",
3403 qemu_opt_get(opts, "mount_tag"));
3404 break;
3406 case QEMU_OPTION_virtfs_synth: {
3407 QemuOpts *fsdev;
3408 QemuOpts *device;
3410 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3411 1, NULL);
3412 if (!fsdev) {
3413 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3414 exit(1);
3416 qemu_opt_set(fsdev, "fsdriver", "synth");
3418 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3419 &error_abort);
3420 qemu_opt_set(device, "driver", "virtio-9p-pci");
3421 qemu_opt_set(device, "fsdev", "v_synth");
3422 qemu_opt_set(device, "mount_tag", "v_synth");
3423 break;
3425 case QEMU_OPTION_serial:
3426 add_device_config(DEV_SERIAL, optarg);
3427 default_serial = 0;
3428 if (strncmp(optarg, "mon:", 4) == 0) {
3429 default_monitor = 0;
3431 break;
3432 case QEMU_OPTION_watchdog:
3433 if (watchdog) {
3434 fprintf(stderr,
3435 "qemu: only one watchdog option may be given\n");
3436 return 1;
3438 watchdog = optarg;
3439 break;
3440 case QEMU_OPTION_watchdog_action:
3441 if (select_watchdog_action(optarg) == -1) {
3442 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3443 exit(1);
3445 break;
3446 case QEMU_OPTION_virtiocon:
3447 add_device_config(DEV_VIRTCON, optarg);
3448 default_virtcon = 0;
3449 if (strncmp(optarg, "mon:", 4) == 0) {
3450 default_monitor = 0;
3452 break;
3453 case QEMU_OPTION_parallel:
3454 add_device_config(DEV_PARALLEL, optarg);
3455 default_parallel = 0;
3456 if (strncmp(optarg, "mon:", 4) == 0) {
3457 default_monitor = 0;
3459 break;
3460 case QEMU_OPTION_debugcon:
3461 add_device_config(DEV_DEBUGCON, optarg);
3462 break;
3463 case QEMU_OPTION_loadvm:
3464 loadvm = optarg;
3465 break;
3466 case QEMU_OPTION_full_screen:
3467 full_screen = 1;
3468 break;
3469 case QEMU_OPTION_no_frame:
3470 no_frame = 1;
3471 break;
3472 case QEMU_OPTION_alt_grab:
3473 alt_grab = 1;
3474 break;
3475 case QEMU_OPTION_ctrl_grab:
3476 ctrl_grab = 1;
3477 break;
3478 case QEMU_OPTION_no_quit:
3479 no_quit = 1;
3480 break;
3481 case QEMU_OPTION_sdl:
3482 #ifdef CONFIG_SDL
3483 display_type = DT_SDL;
3484 break;
3485 #else
3486 fprintf(stderr, "SDL support is disabled\n");
3487 exit(1);
3488 #endif
3489 case QEMU_OPTION_pidfile:
3490 pid_file = optarg;
3491 break;
3492 case QEMU_OPTION_win2k_hack:
3493 win2k_install_hack = 1;
3494 break;
3495 case QEMU_OPTION_rtc_td_hack: {
3496 static GlobalProperty slew_lost_ticks[] = {
3498 .driver = "mc146818rtc",
3499 .property = "lost_tick_policy",
3500 .value = "slew",
3502 { /* end of list */ }
3505 qdev_prop_register_global_list(slew_lost_ticks);
3506 break;
3508 case QEMU_OPTION_acpitable:
3509 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3510 if (!opts) {
3511 exit(1);
3513 do_acpitable_option(opts);
3514 break;
3515 case QEMU_OPTION_smbios:
3516 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3517 if (!opts) {
3518 exit(1);
3520 do_smbios_option(opts);
3521 break;
3522 case QEMU_OPTION_enable_kvm:
3523 olist = qemu_find_opts("machine");
3524 qemu_opts_parse(olist, "accel=kvm", 0);
3525 break;
3526 case QEMU_OPTION_machine:
3527 olist = qemu_find_opts("machine");
3528 opts = qemu_opts_parse(olist, optarg, 1);
3529 if (!opts) {
3530 exit(1);
3532 optarg = qemu_opt_get(opts, "type");
3533 if (optarg) {
3534 machine_class = machine_parse(optarg);
3536 break;
3537 case QEMU_OPTION_no_kvm:
3538 olist = qemu_find_opts("machine");
3539 qemu_opts_parse(olist, "accel=tcg", 0);
3540 break;
3541 case QEMU_OPTION_no_kvm_pit: {
3542 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3543 "separately.\n");
3544 break;
3546 case QEMU_OPTION_no_kvm_pit_reinjection: {
3547 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3549 .driver = "kvm-pit",
3550 .property = "lost_tick_policy",
3551 .value = "discard",
3553 { /* end of list */ }
3556 fprintf(stderr, "Warning: option deprecated, use "
3557 "lost_tick_policy property of kvm-pit instead.\n");
3558 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3559 break;
3561 case QEMU_OPTION_usb:
3562 olist = qemu_find_opts("machine");
3563 qemu_opts_parse(olist, "usb=on", 0);
3564 break;
3565 case QEMU_OPTION_usbdevice:
3566 olist = qemu_find_opts("machine");
3567 qemu_opts_parse(olist, "usb=on", 0);
3568 add_device_config(DEV_USB, optarg);
3569 break;
3570 case QEMU_OPTION_device:
3571 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3572 exit(1);
3574 break;
3575 case QEMU_OPTION_smp:
3576 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3577 exit(1);
3579 break;
3580 case QEMU_OPTION_vnc:
3581 #ifdef CONFIG_VNC
3582 display_remote++;
3583 vnc_display = optarg;
3584 #else
3585 fprintf(stderr, "VNC support is disabled\n");
3586 exit(1);
3587 #endif
3588 break;
3589 case QEMU_OPTION_no_acpi:
3590 acpi_enabled = 0;
3591 break;
3592 case QEMU_OPTION_no_hpet:
3593 no_hpet = 1;
3594 break;
3595 case QEMU_OPTION_balloon:
3596 if (balloon_parse(optarg) < 0) {
3597 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3598 exit(1);
3600 break;
3601 case QEMU_OPTION_no_reboot:
3602 no_reboot = 1;
3603 break;
3604 case QEMU_OPTION_no_shutdown:
3605 no_shutdown = 1;
3606 break;
3607 case QEMU_OPTION_show_cursor:
3608 cursor_hide = 0;
3609 break;
3610 case QEMU_OPTION_uuid:
3611 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3612 fprintf(stderr, "Fail to parse UUID string."
3613 " Wrong format.\n");
3614 exit(1);
3616 qemu_uuid_set = true;
3617 break;
3618 case QEMU_OPTION_option_rom:
3619 if (nb_option_roms >= MAX_OPTION_ROMS) {
3620 fprintf(stderr, "Too many option ROMs\n");
3621 exit(1);
3623 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3624 if (!opts) {
3625 exit(1);
3627 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3628 option_rom[nb_option_roms].bootindex =
3629 qemu_opt_get_number(opts, "bootindex", -1);
3630 if (!option_rom[nb_option_roms].name) {
3631 fprintf(stderr, "Option ROM file is not specified\n");
3632 exit(1);
3634 nb_option_roms++;
3635 break;
3636 case QEMU_OPTION_semihosting:
3637 semihosting_enabled = 1;
3638 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3639 break;
3640 case QEMU_OPTION_semihosting_config:
3641 semihosting_enabled = 1;
3642 opts = qemu_opts_parse(qemu_find_opts("semihosting-config"),
3643 optarg, 0);
3644 if (opts != NULL) {
3645 semihosting_enabled = qemu_opt_get_bool(opts, "enable",
3646 true);
3647 const char *target = qemu_opt_get(opts, "target");
3648 if (target != NULL) {
3649 if (strcmp("native", target) == 0) {
3650 semihosting_target = SEMIHOSTING_TARGET_NATIVE;
3651 } else if (strcmp("gdb", target) == 0) {
3652 semihosting_target = SEMIHOSTING_TARGET_GDB;
3653 } else if (strcmp("auto", target) == 0) {
3654 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3655 } else {
3656 fprintf(stderr, "Unsupported semihosting-config"
3657 " %s\n",
3658 optarg);
3659 exit(1);
3661 } else {
3662 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3664 } else {
3665 fprintf(stderr, "Unsupported semihosting-config %s\n",
3666 optarg);
3667 exit(1);
3669 break;
3670 case QEMU_OPTION_tdf:
3671 fprintf(stderr, "Warning: user space PIT time drift fix "
3672 "is no longer supported.\n");
3673 break;
3674 case QEMU_OPTION_name:
3675 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3676 if (!opts) {
3677 exit(1);
3679 break;
3680 case QEMU_OPTION_prom_env:
3681 if (nb_prom_envs >= MAX_PROM_ENVS) {
3682 fprintf(stderr, "Too many prom variables\n");
3683 exit(1);
3685 prom_envs[nb_prom_envs] = optarg;
3686 nb_prom_envs++;
3687 break;
3688 case QEMU_OPTION_old_param:
3689 old_param = 1;
3690 break;
3691 case QEMU_OPTION_clock:
3692 /* Clock options no longer exist. Keep this option for
3693 * backward compatibility.
3695 break;
3696 case QEMU_OPTION_startdate:
3697 configure_rtc_date_offset(optarg, 1);
3698 break;
3699 case QEMU_OPTION_rtc:
3700 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3701 if (!opts) {
3702 exit(1);
3704 configure_rtc(opts);
3705 break;
3706 case QEMU_OPTION_tb_size:
3707 tcg_tb_size = strtol(optarg, NULL, 0);
3708 if (tcg_tb_size < 0) {
3709 tcg_tb_size = 0;
3711 break;
3712 case QEMU_OPTION_icount:
3713 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3714 optarg, 1);
3715 if (!icount_opts) {
3716 exit(1);
3718 break;
3719 case QEMU_OPTION_incoming:
3720 incoming = optarg;
3721 runstate_set(RUN_STATE_INMIGRATE);
3722 break;
3723 case QEMU_OPTION_nodefaults:
3724 has_defaults = 0;
3725 break;
3726 case QEMU_OPTION_xen_domid:
3727 if (!(xen_available())) {
3728 printf("Option %s not supported for this target\n", popt->name);
3729 exit(1);
3731 xen_domid = atoi(optarg);
3732 break;
3733 case QEMU_OPTION_xen_create:
3734 if (!(xen_available())) {
3735 printf("Option %s not supported for this target\n", popt->name);
3736 exit(1);
3738 xen_mode = XEN_CREATE;
3739 break;
3740 case QEMU_OPTION_xen_attach:
3741 if (!(xen_available())) {
3742 printf("Option %s not supported for this target\n", popt->name);
3743 exit(1);
3745 xen_mode = XEN_ATTACH;
3746 break;
3747 case QEMU_OPTION_trace:
3749 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3750 if (!opts) {
3751 exit(1);
3753 trace_events = qemu_opt_get(opts, "events");
3754 trace_file = qemu_opt_get(opts, "file");
3755 break;
3757 case QEMU_OPTION_readconfig:
3759 int ret = qemu_read_config_file(optarg);
3760 if (ret < 0) {
3761 fprintf(stderr, "read config %s: %s\n", optarg,
3762 strerror(-ret));
3763 exit(1);
3765 break;
3767 case QEMU_OPTION_spice:
3768 olist = qemu_find_opts("spice");
3769 if (!olist) {
3770 fprintf(stderr, "spice is not supported by this qemu build.\n");
3771 exit(1);
3773 opts = qemu_opts_parse(olist, optarg, 0);
3774 if (!opts) {
3775 exit(1);
3777 display_remote++;
3778 break;
3779 case QEMU_OPTION_writeconfig:
3781 FILE *fp;
3782 if (strcmp(optarg, "-") == 0) {
3783 fp = stdout;
3784 } else {
3785 fp = fopen(optarg, "w");
3786 if (fp == NULL) {
3787 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3788 exit(1);
3791 qemu_config_write(fp);
3792 if (fp != stdout) {
3793 fclose(fp);
3795 break;
3797 case QEMU_OPTION_qtest:
3798 qtest_chrdev = optarg;
3799 break;
3800 case QEMU_OPTION_qtest_log:
3801 qtest_log = optarg;
3802 break;
3803 case QEMU_OPTION_sandbox:
3804 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3805 if (!opts) {
3806 exit(1);
3808 break;
3809 case QEMU_OPTION_add_fd:
3810 #ifndef _WIN32
3811 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3812 if (!opts) {
3813 exit(1);
3815 #else
3816 error_report("File descriptor passing is disabled on this "
3817 "platform");
3818 exit(1);
3819 #endif
3820 break;
3821 case QEMU_OPTION_object:
3822 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3823 if (!opts) {
3824 exit(1);
3826 break;
3827 case QEMU_OPTION_realtime:
3828 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3829 if (!opts) {
3830 exit(1);
3832 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3833 break;
3834 case QEMU_OPTION_msg:
3835 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3836 if (!opts) {
3837 exit(1);
3839 configure_msg(opts);
3840 break;
3841 case QEMU_OPTION_dump_vmstate:
3842 if (vmstate_dump_file) {
3843 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3844 "option may be given\n");
3845 exit(1);
3847 vmstate_dump_file = fopen(optarg, "w");
3848 if (vmstate_dump_file == NULL) {
3849 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3850 exit(1);
3852 break;
3853 default:
3854 os_parse_cmd_args(popt->index, optarg);
3858 loc_set_none();
3860 os_daemonize();
3862 if (qemu_init_main_loop(&main_loop_err)) {
3863 error_report("%s", error_get_pretty(main_loop_err));
3864 exit(1);
3867 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3868 exit(1);
3871 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3872 exit(1);
3875 #ifndef _WIN32
3876 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3877 exit(1);
3880 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3881 exit(1);
3883 #endif
3885 if (machine_class == NULL) {
3886 fprintf(stderr, "No machine specified, and there is no default.\n"
3887 "Use -machine help to list supported machines!\n");
3888 exit(1);
3891 current_machine = MACHINE(object_new(object_class_get_name(
3892 OBJECT_CLASS(machine_class))));
3893 object_property_add_child(object_get_root(), "machine",
3894 OBJECT(current_machine), &error_abort);
3895 cpu_exec_init_all();
3897 if (machine_class->hw_version) {
3898 qemu_set_version(machine_class->hw_version);
3901 /* Init CPU def lists, based on config
3902 * - Must be called after all the qemu_read_config_file() calls
3903 * - Must be called before list_cpus()
3904 * - Must be called before machine->init()
3906 cpudef_init();
3908 if (cpu_model && is_help_option(cpu_model)) {
3909 list_cpus(stdout, &fprintf, cpu_model);
3910 exit(0);
3913 /* Open the logfile at this point, if necessary. We can't open the logfile
3914 * when encountering either of the logging options (-d or -D) because the
3915 * other one may be encountered later on the command line, changing the
3916 * location or level of logging.
3918 if (log_mask) {
3919 int mask;
3920 if (log_file) {
3921 qemu_set_log_filename(log_file);
3924 mask = qemu_str_to_log_mask(log_mask);
3925 if (!mask) {
3926 qemu_print_log_usage(stdout);
3927 exit(1);
3929 qemu_set_log(mask);
3932 if (!is_daemonized()) {
3933 if (!trace_init_backends(trace_events, trace_file)) {
3934 exit(1);
3938 /* If no data_dir is specified then try to find it relative to the
3939 executable path. */
3940 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3941 data_dir[data_dir_idx] = os_find_datadir();
3942 if (data_dir[data_dir_idx] != NULL) {
3943 data_dir_idx++;
3946 /* If all else fails use the install path specified when building. */
3947 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3948 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3951 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3953 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3954 if (smp_cpus > machine_class->max_cpus) {
3955 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3956 "supported by machine `%s' (%d)\n", smp_cpus,
3957 machine_class->name, machine_class->max_cpus);
3958 exit(1);
3962 * Get the default machine options from the machine if it is not already
3963 * specified either by the configuration file or by the command line.
3965 if (machine_class->default_machine_opts) {
3966 qemu_opts_set_defaults(qemu_find_opts("machine"),
3967 machine_class->default_machine_opts, 0);
3970 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3971 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3973 if (!vga_model && !default_vga) {
3974 vga_interface_type = VGA_DEVICE;
3976 if (!has_defaults || machine_class->no_serial) {
3977 default_serial = 0;
3979 if (!has_defaults || machine_class->no_parallel) {
3980 default_parallel = 0;
3982 if (!has_defaults || !machine_class->use_virtcon) {
3983 default_virtcon = 0;
3985 if (!has_defaults || !machine_class->use_sclp) {
3986 default_sclp = 0;
3988 if (!has_defaults || machine_class->no_floppy) {
3989 default_floppy = 0;
3991 if (!has_defaults || machine_class->no_cdrom) {
3992 default_cdrom = 0;
3994 if (!has_defaults || machine_class->no_sdcard) {
3995 default_sdcard = 0;
3997 if (!has_defaults) {
3998 default_monitor = 0;
3999 default_net = 0;
4000 default_vga = 0;
4003 if (is_daemonized()) {
4004 /* According to documentation and historically, -nographic redirects
4005 * serial port, parallel port and monitor to stdio, which does not work
4006 * with -daemonize. We can redirect these to null instead, but since
4007 * -nographic is legacy, let's just error out.
4008 * We disallow -nographic only if all other ports are not redirected
4009 * explicitly, to not break existing legacy setups which uses
4010 * -nographic _and_ redirects all ports explicitly - this is valid
4011 * usage, -nographic is just a no-op in this case.
4013 if (display_type == DT_NOGRAPHIC
4014 && (default_parallel || default_serial
4015 || default_monitor || default_virtcon)) {
4016 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4017 exit(1);
4019 #ifdef CONFIG_CURSES
4020 if (display_type == DT_CURSES) {
4021 fprintf(stderr, "curses display can not be used with -daemonize\n");
4022 exit(1);
4024 #endif
4027 if (display_type == DT_NOGRAPHIC) {
4028 if (default_parallel)
4029 add_device_config(DEV_PARALLEL, "null");
4030 if (default_serial && default_monitor) {
4031 add_device_config(DEV_SERIAL, "mon:stdio");
4032 } else if (default_virtcon && default_monitor) {
4033 add_device_config(DEV_VIRTCON, "mon:stdio");
4034 } else if (default_sclp && default_monitor) {
4035 add_device_config(DEV_SCLP, "mon:stdio");
4036 } else {
4037 if (default_serial)
4038 add_device_config(DEV_SERIAL, "stdio");
4039 if (default_virtcon)
4040 add_device_config(DEV_VIRTCON, "stdio");
4041 if (default_sclp) {
4042 add_device_config(DEV_SCLP, "stdio");
4044 if (default_monitor)
4045 monitor_parse("stdio", "readline");
4047 } else {
4048 if (default_serial)
4049 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4050 if (default_parallel)
4051 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4052 if (default_monitor)
4053 monitor_parse("vc:80Cx24C", "readline");
4054 if (default_virtcon)
4055 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4056 if (default_sclp) {
4057 add_device_config(DEV_SCLP, "vc:80Cx24C");
4061 if (display_type == DT_DEFAULT && !display_remote) {
4062 #if defined(CONFIG_GTK)
4063 display_type = DT_GTK;
4064 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4065 display_type = DT_SDL;
4066 #elif defined(CONFIG_VNC)
4067 vnc_display = "localhost:0,to=99";
4068 show_vnc_port = 1;
4069 #else
4070 display_type = DT_NONE;
4071 #endif
4074 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4075 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4076 "for SDL, ignoring option\n");
4078 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4079 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4080 "ignoring option\n");
4083 #if defined(CONFIG_GTK)
4084 if (display_type == DT_GTK) {
4085 early_gtk_display_init();
4087 #endif
4089 socket_init();
4091 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4092 exit(1);
4093 #ifdef CONFIG_VIRTFS
4094 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4095 exit(1);
4097 #endif
4099 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4100 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
4101 exit(1);
4104 /* store value for the future use */
4105 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4107 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4108 != 0) {
4109 exit(0);
4112 if (qemu_opts_foreach(qemu_find_opts("object"),
4113 object_create, NULL, 0) != 0) {
4114 exit(1);
4117 machine_opts = qemu_get_machine_opts();
4118 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4119 1) < 0) {
4120 object_unref(OBJECT(current_machine));
4121 exit(1);
4124 configure_accelerator(current_machine);
4126 if (qtest_chrdev) {
4127 Error *local_err = NULL;
4128 qtest_init(qtest_chrdev, qtest_log, &local_err);
4129 if (local_err) {
4130 error_report("%s", error_get_pretty(local_err));
4131 error_free(local_err);
4132 exit(1);
4136 machine_opts = qemu_get_machine_opts();
4137 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4138 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4139 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4140 bios_name = qemu_opt_get(machine_opts, "firmware");
4142 boot_order = machine_class->default_boot_order;
4143 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4144 if (opts) {
4145 char *normal_boot_order;
4146 const char *order, *once;
4148 order = qemu_opt_get(opts, "order");
4149 if (order) {
4150 validate_bootdevices(order);
4151 boot_order = order;
4154 once = qemu_opt_get(opts, "once");
4155 if (once) {
4156 validate_bootdevices(once);
4157 normal_boot_order = g_strdup(boot_order);
4158 boot_order = once;
4159 qemu_register_reset(restore_boot_order, normal_boot_order);
4162 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4163 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4166 if (!kernel_cmdline) {
4167 kernel_cmdline = "";
4168 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4171 linux_boot = (kernel_filename != NULL);
4173 if (!linux_boot && *kernel_cmdline != '\0') {
4174 fprintf(stderr, "-append only allowed with -kernel option\n");
4175 exit(1);
4178 if (!linux_boot && initrd_filename != NULL) {
4179 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4180 exit(1);
4183 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4184 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4185 exit(1);
4188 os_set_line_buffering();
4190 qemu_init_cpu_loop();
4191 qemu_mutex_lock_iothread();
4193 #ifdef CONFIG_SPICE
4194 /* spice needs the timers to be initialized by this point */
4195 qemu_spice_init();
4196 #endif
4198 cpu_ticks_init();
4199 if (icount_opts) {
4200 if (kvm_enabled() || xen_enabled()) {
4201 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4202 exit(1);
4204 configure_icount(icount_opts, &error_abort);
4205 qemu_opts_del(icount_opts);
4208 /* clean up network at qemu process termination */
4209 atexit(&net_cleanup);
4211 if (net_init_clients() < 0) {
4212 exit(1);
4215 #ifdef CONFIG_TPM
4216 if (tpm_init() < 0) {
4217 exit(1);
4219 #endif
4221 /* init the bluetooth world */
4222 if (foreach_device_config(DEV_BT, bt_parse))
4223 exit(1);
4225 if (!xen_enabled()) {
4226 /* On 32-bit hosts, QEMU is limited by virtual address space */
4227 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4228 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4229 exit(1);
4233 blk_mig_init();
4234 ram_mig_init();
4236 /* If the currently selected machine wishes to override the units-per-bus
4237 * property of its default HBA interface type, do so now. */
4238 if (machine_class->units_per_default_bus) {
4239 override_max_devs(machine_class->block_default_type,
4240 machine_class->units_per_default_bus);
4243 /* open the virtual block devices */
4244 if (snapshot)
4245 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4246 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4247 &machine_class->block_default_type, 1) != 0) {
4248 exit(1);
4251 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4252 CDROM_OPTS);
4253 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4254 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4256 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4257 NULL, 1) != 0) {
4258 exit(1);
4261 set_numa_nodes();
4263 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4264 exit(1);
4267 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4268 exit(1);
4269 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4270 exit(1);
4271 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4272 exit(1);
4273 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4274 exit(1);
4276 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4277 exit(1);
4279 /* If no default VGA is requested, the default is "none". */
4280 if (default_vga) {
4281 if (machine_class->default_display) {
4282 vga_model = machine_class->default_display;
4283 } else if (cirrus_vga_available()) {
4284 vga_model = "cirrus";
4285 } else if (vga_available()) {
4286 vga_model = "std";
4289 if (vga_model) {
4290 select_vgahw(vga_model);
4293 if (watchdog) {
4294 i = select_watchdog(watchdog);
4295 if (i > 0)
4296 exit (i == 1 ? 1 : 0);
4299 if (machine_class->compat_props) {
4300 qdev_prop_register_global_list(machine_class->compat_props);
4302 qemu_add_globals();
4304 qdev_machine_init();
4306 current_machine->ram_size = ram_size;
4307 current_machine->maxram_size = maxram_size;
4308 current_machine->ram_slots = ram_slots;
4309 current_machine->boot_order = boot_order;
4310 current_machine->cpu_model = cpu_model;
4312 machine_class->init(current_machine);
4314 realtime_init();
4316 audio_init();
4318 cpu_synchronize_all_post_init();
4320 set_numa_modes();
4322 /* init USB devices */
4323 if (usb_enabled(false)) {
4324 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4325 exit(1);
4328 /* init generic devices */
4329 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4330 exit(1);
4332 /* Did we create any drives that we failed to create a device for? */
4333 drive_check_orphaned();
4335 net_check_clients();
4337 ds = init_displaystate();
4339 /* init local displays */
4340 switch (display_type) {
4341 case DT_NOGRAPHIC:
4342 (void)ds; /* avoid warning if no display is configured */
4343 break;
4344 #if defined(CONFIG_CURSES)
4345 case DT_CURSES:
4346 curses_display_init(ds, full_screen);
4347 break;
4348 #endif
4349 #if defined(CONFIG_SDL)
4350 case DT_SDL:
4351 sdl_display_init(ds, full_screen, no_frame);
4352 break;
4353 #elif defined(CONFIG_COCOA)
4354 case DT_SDL:
4355 cocoa_display_init(ds, full_screen);
4356 break;
4357 #endif
4358 #if defined(CONFIG_GTK)
4359 case DT_GTK:
4360 gtk_display_init(ds, full_screen, grab_on_hover);
4361 break;
4362 #endif
4363 default:
4364 break;
4367 /* must be after terminal init, SDL library changes signal handlers */
4368 os_setup_signal_handling();
4370 #ifdef CONFIG_VNC
4371 /* init remote displays */
4372 if (vnc_display) {
4373 Error *local_err = NULL;
4374 vnc_display_init(ds);
4375 vnc_display_open(ds, vnc_display, &local_err);
4376 if (local_err != NULL) {
4377 error_report("Failed to start VNC server on `%s': %s",
4378 vnc_display, error_get_pretty(local_err));
4379 error_free(local_err);
4380 exit(1);
4383 if (show_vnc_port) {
4384 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4387 #endif
4388 #ifdef CONFIG_SPICE
4389 if (using_spice) {
4390 qemu_spice_display_init();
4392 #endif
4394 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4395 exit(1);
4398 qdev_machine_creation_done();
4400 if (rom_load_all() != 0) {
4401 fprintf(stderr, "rom loading failed\n");
4402 exit(1);
4405 /* TODO: once all bus devices are qdevified, this should be done
4406 * when bus is created by qdev.c */
4407 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4408 qemu_run_machine_init_done_notifiers();
4410 /* Done notifiers can load ROMs */
4411 rom_load_done();
4413 qemu_system_reset(VMRESET_SILENT);
4414 if (loadvm) {
4415 if (load_vmstate(loadvm) < 0) {
4416 autostart = 0;
4420 qdev_prop_check_globals();
4421 if (vmstate_dump_file) {
4422 /* dump and exit */
4423 dump_vmstate_json_to_file(vmstate_dump_file);
4424 return 0;
4427 if (incoming) {
4428 Error *local_err = NULL;
4429 qemu_start_incoming_migration(incoming, &local_err);
4430 if (local_err) {
4431 error_report("-incoming %s: %s", incoming,
4432 error_get_pretty(local_err));
4433 error_free(local_err);
4434 exit(1);
4436 } else if (autostart) {
4437 vm_start();
4440 os_setup_post();
4442 if (is_daemonized()) {
4443 if (!trace_init_backends(trace_events, trace_file)) {
4444 exit(1);
4448 main_loop();
4449 bdrv_close_all();
4450 pause_all_vcpus();
4451 res_free();
4452 #ifdef CONFIG_TPM
4453 tpm_cleanup();
4454 #endif
4456 return 0;