Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob2352c21b98b2c5f98469cfb56d1ac9777786e753
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);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv);
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 /* Trace unassigned memory or i/o accesses. */
200 bool trace_unassigned;
202 static QEMUBootSetHandler *boot_set_handler;
203 static void *boot_set_opaque;
205 static NotifierList exit_notifiers =
206 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
208 static NotifierList machine_init_done_notifiers =
209 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
211 bool xen_allowed;
212 uint32_t xen_domid;
213 enum xen_mode xen_mode = XEN_EMULATE;
215 static int has_defaults = 1;
216 static int default_serial = 1;
217 static int default_parallel = 1;
218 static int default_virtcon = 1;
219 static int default_sclp = 1;
220 static int default_monitor = 1;
221 static int default_floppy = 1;
222 static int default_cdrom = 1;
223 static int default_sdcard = 1;
224 static int default_vga = 1;
226 static struct {
227 const char *driver;
228 int *flag;
229 } default_list[] = {
230 { .driver = "isa-serial", .flag = &default_serial },
231 { .driver = "isa-parallel", .flag = &default_parallel },
232 { .driver = "isa-fdc", .flag = &default_floppy },
233 { .driver = "ide-cd", .flag = &default_cdrom },
234 { .driver = "ide-hd", .flag = &default_cdrom },
235 { .driver = "ide-drive", .flag = &default_cdrom },
236 { .driver = "scsi-cd", .flag = &default_cdrom },
237 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
238 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
239 { .driver = "virtio-serial", .flag = &default_virtcon },
240 { .driver = "VGA", .flag = &default_vga },
241 { .driver = "isa-vga", .flag = &default_vga },
242 { .driver = "cirrus-vga", .flag = &default_vga },
243 { .driver = "isa-cirrus-vga", .flag = &default_vga },
244 { .driver = "vmware-svga", .flag = &default_vga },
245 { .driver = "qxl-vga", .flag = &default_vga },
248 static QemuOptsList qemu_rtc_opts = {
249 .name = "rtc",
250 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
251 .desc = {
253 .name = "base",
254 .type = QEMU_OPT_STRING,
256 .name = "clock",
257 .type = QEMU_OPT_STRING,
259 .name = "driftfix",
260 .type = QEMU_OPT_STRING,
262 { /* end of list */ }
266 static QemuOptsList qemu_sandbox_opts = {
267 .name = "sandbox",
268 .implied_opt_name = "enable",
269 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
270 .desc = {
272 .name = "enable",
273 .type = QEMU_OPT_BOOL,
275 { /* end of list */ }
279 static QemuOptsList qemu_trace_opts = {
280 .name = "trace",
281 .implied_opt_name = "trace",
282 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
283 .desc = {
285 .name = "events",
286 .type = QEMU_OPT_STRING,
288 .name = "file",
289 .type = QEMU_OPT_STRING,
291 { /* end of list */ }
295 static QemuOptsList qemu_option_rom_opts = {
296 .name = "option-rom",
297 .implied_opt_name = "romfile",
298 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
299 .desc = {
301 .name = "bootindex",
302 .type = QEMU_OPT_NUMBER,
303 }, {
304 .name = "romfile",
305 .type = QEMU_OPT_STRING,
307 { /* end of list */ }
311 static QemuOptsList qemu_machine_opts = {
312 .name = "machine",
313 .implied_opt_name = "type",
314 .merge_lists = true,
315 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
316 .desc = {
318 .name = "type",
319 .type = QEMU_OPT_STRING,
320 .help = "emulated machine"
321 }, {
322 .name = "accel",
323 .type = QEMU_OPT_STRING,
324 .help = "accelerator list",
325 }, {
326 .name = "kernel_irqchip",
327 .type = QEMU_OPT_BOOL,
328 .help = "use KVM in-kernel irqchip",
329 }, {
330 .name = "kvm_shadow_mem",
331 .type = QEMU_OPT_SIZE,
332 .help = "KVM shadow MMU size",
333 }, {
334 .name = "kernel",
335 .type = QEMU_OPT_STRING,
336 .help = "Linux kernel image file",
337 }, {
338 .name = "initrd",
339 .type = QEMU_OPT_STRING,
340 .help = "Linux initial ramdisk file",
341 }, {
342 .name = "append",
343 .type = QEMU_OPT_STRING,
344 .help = "Linux kernel command line",
345 }, {
346 .name = "dtb",
347 .type = QEMU_OPT_STRING,
348 .help = "Linux kernel device tree file",
349 }, {
350 .name = "dumpdtb",
351 .type = QEMU_OPT_STRING,
352 .help = "Dump current dtb to a file and quit",
353 }, {
354 .name = "phandle_start",
355 .type = QEMU_OPT_NUMBER,
356 .help = "The first phandle ID we may generate dynamically",
357 }, {
358 .name = "dt_compatible",
359 .type = QEMU_OPT_STRING,
360 .help = "Overrides the \"compatible\" property of the dt root node",
361 }, {
362 .name = "dump-guest-core",
363 .type = QEMU_OPT_BOOL,
364 .help = "Include guest memory in a core dump",
365 }, {
366 .name = "mem-merge",
367 .type = QEMU_OPT_BOOL,
368 .help = "enable/disable memory merge support",
370 .name = "usb",
371 .type = QEMU_OPT_BOOL,
372 .help = "Set on/off to enable/disable usb",
374 .name = "firmware",
375 .type = QEMU_OPT_STRING,
376 .help = "firmware image",
378 .name = "kvm-type",
379 .type = QEMU_OPT_STRING,
380 .help = "Specifies the KVM virtualization mode (HV, PR)",
382 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
383 .type = QEMU_OPT_SIZE,
384 .help = "maximum ram below the 4G boundary (32bit boundary)",
385 }, {
386 .name = PC_MACHINE_VMPORT,
387 .type = QEMU_OPT_STRING,
388 .help = "Enable vmport (pc & q35)",
390 .name = "iommu",
391 .type = QEMU_OPT_BOOL,
392 .help = "Set on/off to enable/disable Intel IOMMU (VT-d)",
394 { /* End of list */ }
398 static QemuOptsList qemu_boot_opts = {
399 .name = "boot-opts",
400 .implied_opt_name = "order",
401 .merge_lists = true,
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
403 .desc = {
405 .name = "order",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "once",
409 .type = QEMU_OPT_STRING,
410 }, {
411 .name = "menu",
412 .type = QEMU_OPT_BOOL,
413 }, {
414 .name = "splash",
415 .type = QEMU_OPT_STRING,
416 }, {
417 .name = "splash-time",
418 .type = QEMU_OPT_STRING,
419 }, {
420 .name = "reboot-timeout",
421 .type = QEMU_OPT_STRING,
422 }, {
423 .name = "strict",
424 .type = QEMU_OPT_BOOL,
426 { /*End of list */ }
430 static QemuOptsList qemu_add_fd_opts = {
431 .name = "add-fd",
432 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
433 .desc = {
435 .name = "fd",
436 .type = QEMU_OPT_NUMBER,
437 .help = "file descriptor of which a duplicate is added to fd set",
439 .name = "set",
440 .type = QEMU_OPT_NUMBER,
441 .help = "ID of the fd set to add fd to",
443 .name = "opaque",
444 .type = QEMU_OPT_STRING,
445 .help = "free-form string used to describe fd",
447 { /* end of list */ }
451 static QemuOptsList qemu_object_opts = {
452 .name = "object",
453 .implied_opt_name = "qom-type",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
455 .desc = {
460 static QemuOptsList qemu_tpmdev_opts = {
461 .name = "tpmdev",
462 .implied_opt_name = "type",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
464 .desc = {
465 /* options are defined in the TPM backends */
466 { /* end of list */ }
470 static QemuOptsList qemu_realtime_opts = {
471 .name = "realtime",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
473 .desc = {
475 .name = "mlock",
476 .type = QEMU_OPT_BOOL,
478 { /* end of list */ }
482 static QemuOptsList qemu_msg_opts = {
483 .name = "msg",
484 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
485 .desc = {
487 .name = "timestamp",
488 .type = QEMU_OPT_BOOL,
490 { /* end of list */ }
494 static QemuOptsList qemu_name_opts = {
495 .name = "name",
496 .implied_opt_name = "guest",
497 .merge_lists = true,
498 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
499 .desc = {
501 .name = "guest",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the name of the guest.\n"
504 "This name will be displayed in the SDL window caption.\n"
505 "The name will also be used for the VNC server",
506 }, {
507 .name = "process",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets the name of the QEMU process, as shown in top etc",
510 }, {
511 .name = "debug-threads",
512 .type = QEMU_OPT_BOOL,
513 .help = "When enabled, name the individual threads; defaults off.\n"
514 "NOTE: The thread names are for debugging and not a\n"
515 "stable API.",
517 { /* End of list */ }
521 static QemuOptsList qemu_mem_opts = {
522 .name = "memory",
523 .implied_opt_name = "size",
524 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
525 .merge_lists = true,
526 .desc = {
528 .name = "size",
529 .type = QEMU_OPT_SIZE,
532 .name = "slots",
533 .type = QEMU_OPT_NUMBER,
536 .name = "maxmem",
537 .type = QEMU_OPT_SIZE,
539 { /* end of list */ }
543 static QemuOptsList qemu_icount_opts = {
544 .name = "icount",
545 .implied_opt_name = "shift",
546 .merge_lists = true,
547 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
548 .desc = {
550 .name = "shift",
551 .type = QEMU_OPT_STRING,
552 }, {
553 .name = "align",
554 .type = QEMU_OPT_BOOL,
556 { /* end of list */ }
561 * Get machine options
563 * Returns: machine options (never null).
565 QemuOpts *qemu_get_machine_opts(void)
567 return qemu_find_opts_singleton("machine");
570 const char *qemu_get_vm_name(void)
572 return qemu_name;
575 static void res_free(void)
577 if (boot_splash_filedata != NULL) {
578 g_free(boot_splash_filedata);
579 boot_splash_filedata = NULL;
583 static int default_driver_check(QemuOpts *opts, void *opaque)
585 const char *driver = qemu_opt_get(opts, "driver");
586 int i;
588 if (!driver)
589 return 0;
590 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
591 if (strcmp(default_list[i].driver, driver) != 0)
592 continue;
593 *(default_list[i].flag) = 0;
595 return 0;
598 /***********************************************************/
599 /* QEMU state */
601 static RunState current_run_state = RUN_STATE_PRELAUNCH;
603 /* We use RUN_STATE_MAX but any invalid value will do */
604 static RunState vmstop_requested = RUN_STATE_MAX;
605 static QemuMutex vmstop_lock;
607 typedef struct {
608 RunState from;
609 RunState to;
610 } RunStateTransition;
612 static const RunStateTransition runstate_transitions_def[] = {
613 /* from -> to */
614 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
615 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
618 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
620 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
621 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
624 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
627 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
629 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
630 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
633 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
634 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
636 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
637 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
639 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
641 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
642 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
643 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
644 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
645 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
646 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
647 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
648 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
649 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
650 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
652 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
654 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
655 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
657 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
658 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
659 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
660 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
662 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
663 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
665 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
666 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
668 { RUN_STATE_MAX, RUN_STATE_MAX },
671 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
673 bool runstate_check(RunState state)
675 return current_run_state == state;
678 static void runstate_init(void)
680 const RunStateTransition *p;
682 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
683 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
684 runstate_valid_transitions[p->from][p->to] = true;
687 qemu_mutex_init(&vmstop_lock);
690 /* This function will abort() on invalid state transitions */
691 void runstate_set(RunState new_state)
693 assert(new_state < RUN_STATE_MAX);
695 if (!runstate_valid_transitions[current_run_state][new_state]) {
696 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
697 RunState_lookup[current_run_state],
698 RunState_lookup[new_state]);
699 abort();
701 trace_runstate_set(new_state);
702 current_run_state = new_state;
705 int runstate_is_running(void)
707 return runstate_check(RUN_STATE_RUNNING);
710 bool runstate_needs_reset(void)
712 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
713 runstate_check(RUN_STATE_SHUTDOWN);
716 StatusInfo *qmp_query_status(Error **errp)
718 StatusInfo *info = g_malloc0(sizeof(*info));
720 info->running = runstate_is_running();
721 info->singlestep = singlestep;
722 info->status = current_run_state;
724 return info;
727 static bool qemu_vmstop_requested(RunState *r)
729 qemu_mutex_lock(&vmstop_lock);
730 *r = vmstop_requested;
731 vmstop_requested = RUN_STATE_MAX;
732 qemu_mutex_unlock(&vmstop_lock);
733 return *r < RUN_STATE_MAX;
736 void qemu_system_vmstop_request_prepare(void)
738 qemu_mutex_lock(&vmstop_lock);
741 void qemu_system_vmstop_request(RunState state)
743 vmstop_requested = state;
744 qemu_mutex_unlock(&vmstop_lock);
745 qemu_notify_event();
748 void vm_start(void)
750 RunState requested;
752 qemu_vmstop_requested(&requested);
753 if (runstate_is_running() && requested == RUN_STATE_MAX) {
754 return;
757 /* Ensure that a STOP/RESUME pair of events is emitted if a
758 * vmstop request was pending. The BLOCK_IO_ERROR event, for
759 * example, according to documentation is always followed by
760 * the STOP event.
762 if (runstate_is_running()) {
763 qapi_event_send_stop(&error_abort);
764 } else {
765 cpu_enable_ticks();
766 runstate_set(RUN_STATE_RUNNING);
767 vm_state_notify(1, RUN_STATE_RUNNING);
768 resume_all_vcpus();
771 qapi_event_send_resume(&error_abort);
775 /***********************************************************/
776 /* real time host monotonic timer */
778 /***********************************************************/
779 /* host time/date access */
780 void qemu_get_timedate(struct tm *tm, int offset)
782 time_t ti;
784 time(&ti);
785 ti += offset;
786 if (rtc_date_offset == -1) {
787 if (rtc_utc)
788 gmtime_r(&ti, tm);
789 else
790 localtime_r(&ti, tm);
791 } else {
792 ti -= rtc_date_offset;
793 gmtime_r(&ti, tm);
797 int qemu_timedate_diff(struct tm *tm)
799 time_t seconds;
801 if (rtc_date_offset == -1)
802 if (rtc_utc)
803 seconds = mktimegm(tm);
804 else {
805 struct tm tmp = *tm;
806 tmp.tm_isdst = -1; /* use timezone to figure it out */
807 seconds = mktime(&tmp);
809 else
810 seconds = mktimegm(tm) + rtc_date_offset;
812 return seconds - time(NULL);
815 static void configure_rtc_date_offset(const char *startdate, int legacy)
817 time_t rtc_start_date;
818 struct tm tm;
820 if (!strcmp(startdate, "now") && legacy) {
821 rtc_date_offset = -1;
822 } else {
823 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
824 &tm.tm_year,
825 &tm.tm_mon,
826 &tm.tm_mday,
827 &tm.tm_hour,
828 &tm.tm_min,
829 &tm.tm_sec) == 6) {
830 /* OK */
831 } else if (sscanf(startdate, "%d-%d-%d",
832 &tm.tm_year,
833 &tm.tm_mon,
834 &tm.tm_mday) == 3) {
835 tm.tm_hour = 0;
836 tm.tm_min = 0;
837 tm.tm_sec = 0;
838 } else {
839 goto date_fail;
841 tm.tm_year -= 1900;
842 tm.tm_mon--;
843 rtc_start_date = mktimegm(&tm);
844 if (rtc_start_date == -1) {
845 date_fail:
846 fprintf(stderr, "Invalid date format. Valid formats are:\n"
847 "'2006-06-17T16:01:21' or '2006-06-17'\n");
848 exit(1);
850 rtc_date_offset = time(NULL) - rtc_start_date;
854 static void configure_rtc(QemuOpts *opts)
856 const char *value;
858 value = qemu_opt_get(opts, "base");
859 if (value) {
860 if (!strcmp(value, "utc")) {
861 rtc_utc = 1;
862 } else if (!strcmp(value, "localtime")) {
863 rtc_utc = 0;
864 } else {
865 configure_rtc_date_offset(value, 0);
868 value = qemu_opt_get(opts, "clock");
869 if (value) {
870 if (!strcmp(value, "host")) {
871 rtc_clock = QEMU_CLOCK_HOST;
872 } else if (!strcmp(value, "rt")) {
873 rtc_clock = QEMU_CLOCK_REALTIME;
874 } else if (!strcmp(value, "vm")) {
875 rtc_clock = QEMU_CLOCK_VIRTUAL;
876 } else {
877 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
878 exit(1);
881 value = qemu_opt_get(opts, "driftfix");
882 if (value) {
883 if (!strcmp(value, "slew")) {
884 static GlobalProperty slew_lost_ticks[] = {
886 .driver = "mc146818rtc",
887 .property = "lost_tick_policy",
888 .value = "slew",
890 { /* end of list */ }
893 qdev_prop_register_global_list(slew_lost_ticks);
894 } else if (!strcmp(value, "none")) {
895 /* discard is default */
896 } else {
897 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
898 exit(1);
903 /***********************************************************/
904 /* Bluetooth support */
905 static int nb_hcis;
906 static int cur_hci;
907 static struct HCIInfo *hci_table[MAX_NICS];
909 struct HCIInfo *qemu_next_hci(void)
911 if (cur_hci == nb_hcis)
912 return &null_hci;
914 return hci_table[cur_hci++];
917 static int bt_hci_parse(const char *str)
919 struct HCIInfo *hci;
920 bdaddr_t bdaddr;
922 if (nb_hcis >= MAX_NICS) {
923 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
924 return -1;
927 hci = hci_init(str);
928 if (!hci)
929 return -1;
931 bdaddr.b[0] = 0x52;
932 bdaddr.b[1] = 0x54;
933 bdaddr.b[2] = 0x00;
934 bdaddr.b[3] = 0x12;
935 bdaddr.b[4] = 0x34;
936 bdaddr.b[5] = 0x56 + nb_hcis;
937 hci->bdaddr_set(hci, bdaddr.b);
939 hci_table[nb_hcis++] = hci;
941 return 0;
944 static void bt_vhci_add(int vlan_id)
946 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
948 if (!vlan->slave)
949 fprintf(stderr, "qemu: warning: adding a VHCI to "
950 "an empty scatternet %i\n", vlan_id);
952 bt_vhci_init(bt_new_hci(vlan));
955 static struct bt_device_s *bt_device_add(const char *opt)
957 struct bt_scatternet_s *vlan;
958 int vlan_id = 0;
959 char *endp = strstr(opt, ",vlan=");
960 int len = (endp ? endp - opt : strlen(opt)) + 1;
961 char devname[10];
963 pstrcpy(devname, MIN(sizeof(devname), len), opt);
965 if (endp) {
966 vlan_id = strtol(endp + 6, &endp, 0);
967 if (*endp) {
968 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
969 return 0;
973 vlan = qemu_find_bt_vlan(vlan_id);
975 if (!vlan->slave)
976 fprintf(stderr, "qemu: warning: adding a slave device to "
977 "an empty scatternet %i\n", vlan_id);
979 if (!strcmp(devname, "keyboard"))
980 return bt_keyboard_init(vlan);
982 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
983 return 0;
986 static int bt_parse(const char *opt)
988 const char *endp, *p;
989 int vlan;
991 if (strstart(opt, "hci", &endp)) {
992 if (!*endp || *endp == ',') {
993 if (*endp)
994 if (!strstart(endp, ",vlan=", 0))
995 opt = endp + 1;
997 return bt_hci_parse(opt);
999 } else if (strstart(opt, "vhci", &endp)) {
1000 if (!*endp || *endp == ',') {
1001 if (*endp) {
1002 if (strstart(endp, ",vlan=", &p)) {
1003 vlan = strtol(p, (char **) &endp, 0);
1004 if (*endp) {
1005 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1006 return 1;
1008 } else {
1009 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1010 return 1;
1012 } else
1013 vlan = 0;
1015 bt_vhci_add(vlan);
1016 return 0;
1018 } else if (strstart(opt, "device:", &endp))
1019 return !bt_device_add(endp);
1021 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1022 return 1;
1025 static int parse_sandbox(QemuOpts *opts, void *opaque)
1027 /* FIXME: change this to true for 1.3 */
1028 if (qemu_opt_get_bool(opts, "enable", false)) {
1029 #ifdef CONFIG_SECCOMP
1030 if (seccomp_start() < 0) {
1031 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1032 "failed to install seccomp syscall filter in the kernel");
1033 return -1;
1035 #else
1036 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1037 "sandboxing request but seccomp is not compiled into this build");
1038 return -1;
1039 #endif
1042 return 0;
1045 static int parse_name(QemuOpts *opts, void *opaque)
1047 const char *proc_name;
1049 if (qemu_opt_get(opts, "debug-threads")) {
1050 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1052 qemu_name = qemu_opt_get(opts, "guest");
1054 proc_name = qemu_opt_get(opts, "process");
1055 if (proc_name) {
1056 os_set_proc_name(proc_name);
1059 return 0;
1062 bool usb_enabled(bool default_usb)
1064 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1065 has_defaults && default_usb);
1068 #ifndef _WIN32
1069 static int parse_add_fd(QemuOpts *opts, void *opaque)
1071 int fd, dupfd, flags;
1072 int64_t fdset_id;
1073 const char *fd_opaque = NULL;
1075 fd = qemu_opt_get_number(opts, "fd", -1);
1076 fdset_id = qemu_opt_get_number(opts, "set", -1);
1077 fd_opaque = qemu_opt_get(opts, "opaque");
1079 if (fd < 0) {
1080 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1081 "fd option is required and must be non-negative");
1082 return -1;
1085 if (fd <= STDERR_FILENO) {
1086 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1087 "fd cannot be a standard I/O stream");
1088 return -1;
1092 * All fds inherited across exec() necessarily have FD_CLOEXEC
1093 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1095 flags = fcntl(fd, F_GETFD);
1096 if (flags == -1 || (flags & FD_CLOEXEC)) {
1097 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1098 "fd is not valid or already in use");
1099 return -1;
1102 if (fdset_id < 0) {
1103 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1104 "set option is required and must be non-negative");
1105 return -1;
1108 #ifdef F_DUPFD_CLOEXEC
1109 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1110 #else
1111 dupfd = dup(fd);
1112 if (dupfd != -1) {
1113 qemu_set_cloexec(dupfd);
1115 #endif
1116 if (dupfd == -1) {
1117 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1118 "Error duplicating fd: %s", strerror(errno));
1119 return -1;
1122 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1123 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1124 fd_opaque, NULL);
1126 return 0;
1129 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1131 int fd;
1133 fd = qemu_opt_get_number(opts, "fd", -1);
1134 close(fd);
1136 return 0;
1138 #endif
1140 /***********************************************************/
1141 /* QEMU Block devices */
1143 #define HD_OPTS "media=disk"
1144 #define CDROM_OPTS "media=cdrom"
1145 #define FD_OPTS ""
1146 #define PFLASH_OPTS ""
1147 #define MTD_OPTS ""
1148 #define SD_OPTS ""
1150 static int drive_init_func(QemuOpts *opts, void *opaque)
1152 BlockInterfaceType *block_default_type = opaque;
1154 return drive_new(opts, *block_default_type) == NULL;
1157 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1159 if (qemu_opt_get(opts, "snapshot") == NULL) {
1160 qemu_opt_set(opts, "snapshot", "on");
1162 return 0;
1165 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1166 int index, const char *optstr)
1168 QemuOpts *opts;
1169 DriveInfo *dinfo;
1171 if (!enable || drive_get_by_index(type, index)) {
1172 return;
1175 opts = drive_add(type, index, NULL, optstr);
1176 if (snapshot) {
1177 drive_enable_snapshot(opts, NULL);
1180 dinfo = drive_new(opts, type);
1181 if (!dinfo) {
1182 exit(1);
1184 dinfo->is_default = true;
1188 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1190 boot_set_handler = func;
1191 boot_set_opaque = opaque;
1194 int qemu_boot_set(const char *boot_order)
1196 if (!boot_set_handler) {
1197 return -EINVAL;
1199 return boot_set_handler(boot_set_opaque, boot_order);
1202 static void validate_bootdevices(const char *devices)
1204 /* We just do some generic consistency checks */
1205 const char *p;
1206 int bitmap = 0;
1208 for (p = devices; *p != '\0'; p++) {
1209 /* Allowed boot devices are:
1210 * a-b: floppy disk drives
1211 * c-f: IDE disk drives
1212 * g-m: machine implementation dependent drives
1213 * n-p: network devices
1214 * It's up to each machine implementation to check if the given boot
1215 * devices match the actual hardware implementation and firmware
1216 * features.
1218 if (*p < 'a' || *p > 'p') {
1219 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1220 exit(1);
1222 if (bitmap & (1 << (*p - 'a'))) {
1223 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1224 exit(1);
1226 bitmap |= 1 << (*p - 'a');
1230 static void restore_boot_order(void *opaque)
1232 char *normal_boot_order = opaque;
1233 static int first = 1;
1235 /* Restore boot order and remove ourselves after the first boot */
1236 if (first) {
1237 first = 0;
1238 return;
1241 qemu_boot_set(normal_boot_order);
1243 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1244 g_free(normal_boot_order);
1247 static QemuOptsList qemu_smp_opts = {
1248 .name = "smp-opts",
1249 .implied_opt_name = "cpus",
1250 .merge_lists = true,
1251 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1252 .desc = {
1254 .name = "cpus",
1255 .type = QEMU_OPT_NUMBER,
1256 }, {
1257 .name = "sockets",
1258 .type = QEMU_OPT_NUMBER,
1259 }, {
1260 .name = "cores",
1261 .type = QEMU_OPT_NUMBER,
1262 }, {
1263 .name = "threads",
1264 .type = QEMU_OPT_NUMBER,
1265 }, {
1266 .name = "maxcpus",
1267 .type = QEMU_OPT_NUMBER,
1269 { /*End of list */ }
1273 static void smp_parse(QemuOpts *opts)
1275 if (opts) {
1277 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1278 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1279 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1280 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1282 /* compute missing values, prefer sockets over cores over threads */
1283 if (cpus == 0 || sockets == 0) {
1284 sockets = sockets > 0 ? sockets : 1;
1285 cores = cores > 0 ? cores : 1;
1286 threads = threads > 0 ? threads : 1;
1287 if (cpus == 0) {
1288 cpus = cores * threads * sockets;
1290 } else {
1291 if (cores == 0) {
1292 threads = threads > 0 ? threads : 1;
1293 cores = cpus / (sockets * threads);
1294 } else {
1295 threads = cpus / (cores * sockets);
1299 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1301 smp_cpus = cpus;
1302 smp_cores = cores > 0 ? cores : 1;
1303 smp_threads = threads > 0 ? threads : 1;
1307 if (max_cpus == 0) {
1308 max_cpus = smp_cpus;
1311 if (max_cpus > MAX_CPUMASK_BITS) {
1312 fprintf(stderr, "Unsupported number of maxcpus\n");
1313 exit(1);
1315 if (max_cpus < smp_cpus) {
1316 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1317 exit(1);
1322 static void realtime_init(void)
1324 if (enable_mlock) {
1325 if (os_mlock() < 0) {
1326 fprintf(stderr, "qemu: locking memory failed\n");
1327 exit(1);
1333 static void configure_msg(QemuOpts *opts)
1335 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1338 /***********************************************************/
1339 /* USB devices */
1341 static int usb_device_add(const char *devname)
1343 USBDevice *dev = NULL;
1344 #ifndef CONFIG_LINUX
1345 const char *p;
1346 #endif
1348 if (!usb_enabled(false)) {
1349 return -1;
1352 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1353 dev = usbdevice_create(devname);
1354 if (dev)
1355 goto done;
1357 /* the other ones */
1358 #ifndef CONFIG_LINUX
1359 /* only the linux version is qdev-ified, usb-bsd still needs this */
1360 if (strstart(devname, "host:", &p)) {
1361 dev = usb_host_device_open(usb_bus_find(-1), p);
1363 #endif
1364 if (!dev)
1365 return -1;
1367 done:
1368 return 0;
1371 static int usb_device_del(const char *devname)
1373 int bus_num, addr;
1374 const char *p;
1376 if (strstart(devname, "host:", &p)) {
1377 return -1;
1380 if (!usb_enabled(false)) {
1381 return -1;
1384 p = strchr(devname, '.');
1385 if (!p)
1386 return -1;
1387 bus_num = strtoul(devname, NULL, 0);
1388 addr = strtoul(p + 1, NULL, 0);
1390 return usb_device_delete_addr(bus_num, addr);
1393 static int usb_parse(const char *cmdline)
1395 int r;
1396 r = usb_device_add(cmdline);
1397 if (r < 0) {
1398 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1400 return r;
1403 void do_usb_add(Monitor *mon, const QDict *qdict)
1405 const char *devname = qdict_get_str(qdict, "devname");
1406 if (usb_device_add(devname) < 0) {
1407 error_report("could not add USB device '%s'", devname);
1411 void do_usb_del(Monitor *mon, const QDict *qdict)
1413 const char *devname = qdict_get_str(qdict, "devname");
1414 if (usb_device_del(devname) < 0) {
1415 error_report("could not delete USB device '%s'", devname);
1419 /***********************************************************/
1420 /* machine registration */
1422 MachineState *current_machine;
1424 static void machine_class_init(ObjectClass *oc, void *data)
1426 MachineClass *mc = MACHINE_CLASS(oc);
1427 QEMUMachine *qm = data;
1429 mc->family = qm->family;
1430 mc->name = qm->name;
1431 mc->alias = qm->alias;
1432 mc->desc = qm->desc;
1433 mc->init = qm->init;
1434 mc->reset = qm->reset;
1435 mc->hot_add_cpu = qm->hot_add_cpu;
1436 mc->kvm_type = qm->kvm_type;
1437 mc->block_default_type = qm->block_default_type;
1438 mc->units_per_default_bus = qm->units_per_default_bus;
1439 mc->max_cpus = qm->max_cpus;
1440 mc->no_serial = qm->no_serial;
1441 mc->no_parallel = qm->no_parallel;
1442 mc->use_virtcon = qm->use_virtcon;
1443 mc->use_sclp = qm->use_sclp;
1444 mc->no_floppy = qm->no_floppy;
1445 mc->no_cdrom = qm->no_cdrom;
1446 mc->no_sdcard = qm->no_sdcard;
1447 mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1448 mc->is_default = qm->is_default;
1449 mc->default_machine_opts = qm->default_machine_opts;
1450 mc->default_boot_order = qm->default_boot_order;
1451 mc->default_display = qm->default_display;
1452 mc->compat_props = qm->compat_props;
1453 mc->hw_version = qm->hw_version;
1456 int qemu_register_machine(QEMUMachine *m)
1458 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1459 TypeInfo ti = {
1460 .name = name,
1461 .parent = TYPE_MACHINE,
1462 .class_init = machine_class_init,
1463 .class_data = (void *)m,
1466 type_register(&ti);
1467 g_free(name);
1469 return 0;
1472 static MachineClass *find_machine(const char *name)
1474 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1475 MachineClass *mc = NULL;
1477 for (el = machines; el; el = el->next) {
1478 MachineClass *temp = el->data;
1480 if (!strcmp(temp->name, name)) {
1481 mc = temp;
1482 break;
1484 if (temp->alias &&
1485 !strcmp(temp->alias, name)) {
1486 mc = temp;
1487 break;
1491 g_slist_free(machines);
1492 return mc;
1495 MachineClass *find_default_machine(void)
1497 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1498 MachineClass *mc = NULL;
1500 for (el = machines; el; el = el->next) {
1501 MachineClass *temp = el->data;
1503 if (temp->is_default) {
1504 mc = temp;
1505 break;
1509 g_slist_free(machines);
1510 return mc;
1513 MachineInfoList *qmp_query_machines(Error **errp)
1515 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1516 MachineInfoList *mach_list = NULL;
1518 for (el = machines; el; el = el->next) {
1519 MachineClass *mc = el->data;
1520 MachineInfoList *entry;
1521 MachineInfo *info;
1523 info = g_malloc0(sizeof(*info));
1524 if (mc->is_default) {
1525 info->has_is_default = true;
1526 info->is_default = true;
1529 if (mc->alias) {
1530 info->has_alias = true;
1531 info->alias = g_strdup(mc->alias);
1534 info->name = g_strdup(mc->name);
1535 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1537 entry = g_malloc0(sizeof(*entry));
1538 entry->value = info;
1539 entry->next = mach_list;
1540 mach_list = entry;
1543 g_slist_free(machines);
1544 return mach_list;
1547 /***********************************************************/
1548 /* main execution loop */
1550 struct vm_change_state_entry {
1551 VMChangeStateHandler *cb;
1552 void *opaque;
1553 QLIST_ENTRY (vm_change_state_entry) entries;
1556 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1558 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1559 void *opaque)
1561 VMChangeStateEntry *e;
1563 e = g_malloc0(sizeof (*e));
1565 e->cb = cb;
1566 e->opaque = opaque;
1567 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1568 return e;
1571 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1573 QLIST_REMOVE (e, entries);
1574 g_free (e);
1577 void vm_state_notify(int running, RunState state)
1579 VMChangeStateEntry *e, *next;
1581 trace_vm_state_notify(running, state);
1583 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1584 e->cb(e->opaque, running, state);
1588 /* reset/shutdown handler */
1590 typedef struct QEMUResetEntry {
1591 QTAILQ_ENTRY(QEMUResetEntry) entry;
1592 QEMUResetHandler *func;
1593 void *opaque;
1594 } QEMUResetEntry;
1596 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1597 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1598 static int reset_requested;
1599 static int shutdown_requested, shutdown_signal = -1;
1600 static pid_t shutdown_pid;
1601 static int powerdown_requested;
1602 static int debug_requested;
1603 static int suspend_requested;
1604 static WakeupReason wakeup_reason;
1605 static NotifierList powerdown_notifiers =
1606 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1607 static NotifierList suspend_notifiers =
1608 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1609 static NotifierList wakeup_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1611 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1613 int qemu_shutdown_requested_get(void)
1615 return shutdown_requested;
1618 int qemu_reset_requested_get(void)
1620 return reset_requested;
1623 static int qemu_shutdown_requested(void)
1625 return atomic_xchg(&shutdown_requested, 0);
1628 static void qemu_kill_report(void)
1630 if (!qtest_driver() && shutdown_signal != -1) {
1631 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1632 if (shutdown_pid == 0) {
1633 /* This happens for eg ^C at the terminal, so it's worth
1634 * avoiding printing an odd message in that case.
1636 fputc('\n', stderr);
1637 } else {
1638 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1640 shutdown_signal = -1;
1644 static int qemu_reset_requested(void)
1646 int r = reset_requested;
1647 reset_requested = 0;
1648 return r;
1651 static int qemu_suspend_requested(void)
1653 int r = suspend_requested;
1654 suspend_requested = 0;
1655 return r;
1658 static WakeupReason qemu_wakeup_requested(void)
1660 return wakeup_reason;
1663 static int qemu_powerdown_requested(void)
1665 int r = powerdown_requested;
1666 powerdown_requested = 0;
1667 return r;
1670 static int qemu_debug_requested(void)
1672 int r = debug_requested;
1673 debug_requested = 0;
1674 return r;
1677 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1679 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1681 re->func = func;
1682 re->opaque = opaque;
1683 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1686 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1688 QEMUResetEntry *re;
1690 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1691 if (re->func == func && re->opaque == opaque) {
1692 QTAILQ_REMOVE(&reset_handlers, re, entry);
1693 g_free(re);
1694 return;
1699 void qemu_devices_reset(void)
1701 QEMUResetEntry *re, *nre;
1703 /* reset all devices */
1704 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1705 re->func(re->opaque);
1709 void qemu_system_reset(bool report)
1711 MachineClass *mc;
1713 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1715 if (mc && mc->reset) {
1716 mc->reset();
1717 } else {
1718 qemu_devices_reset();
1720 if (report) {
1721 qapi_event_send_reset(&error_abort);
1723 cpu_synchronize_all_post_reset();
1726 void qemu_system_reset_request(void)
1728 if (no_reboot) {
1729 shutdown_requested = 1;
1730 } else {
1731 reset_requested = 1;
1733 cpu_stop_current();
1734 qemu_notify_event();
1737 static void qemu_system_suspend(void)
1739 pause_all_vcpus();
1740 notifier_list_notify(&suspend_notifiers, NULL);
1741 runstate_set(RUN_STATE_SUSPENDED);
1742 qapi_event_send_suspend(&error_abort);
1745 void qemu_system_suspend_request(void)
1747 if (runstate_check(RUN_STATE_SUSPENDED)) {
1748 return;
1750 suspend_requested = 1;
1751 cpu_stop_current();
1752 qemu_notify_event();
1755 void qemu_register_suspend_notifier(Notifier *notifier)
1757 notifier_list_add(&suspend_notifiers, notifier);
1760 void qemu_system_wakeup_request(WakeupReason reason)
1762 trace_system_wakeup_request(reason);
1764 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1765 return;
1767 if (!(wakeup_reason_mask & (1 << reason))) {
1768 return;
1770 runstate_set(RUN_STATE_RUNNING);
1771 wakeup_reason = reason;
1772 qemu_notify_event();
1775 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1777 if (enabled) {
1778 wakeup_reason_mask |= (1 << reason);
1779 } else {
1780 wakeup_reason_mask &= ~(1 << reason);
1784 void qemu_register_wakeup_notifier(Notifier *notifier)
1786 notifier_list_add(&wakeup_notifiers, notifier);
1789 void qemu_system_killed(int signal, pid_t pid)
1791 shutdown_signal = signal;
1792 shutdown_pid = pid;
1793 no_shutdown = 0;
1794 qemu_system_shutdown_request();
1797 void qemu_system_shutdown_request(void)
1799 trace_qemu_system_shutdown_request();
1800 shutdown_requested = 1;
1801 qemu_notify_event();
1804 static void qemu_system_powerdown(void)
1806 qapi_event_send_powerdown(&error_abort);
1807 notifier_list_notify(&powerdown_notifiers, NULL);
1810 void qemu_system_powerdown_request(void)
1812 trace_qemu_system_powerdown_request();
1813 powerdown_requested = 1;
1814 qemu_notify_event();
1817 void qemu_register_powerdown_notifier(Notifier *notifier)
1819 notifier_list_add(&powerdown_notifiers, notifier);
1822 void qemu_system_debug_request(void)
1824 debug_requested = 1;
1825 qemu_notify_event();
1828 static bool main_loop_should_exit(void)
1830 RunState r;
1831 if (qemu_debug_requested()) {
1832 vm_stop(RUN_STATE_DEBUG);
1834 if (qemu_suspend_requested()) {
1835 qemu_system_suspend();
1837 if (qemu_shutdown_requested()) {
1838 qemu_kill_report();
1839 qapi_event_send_shutdown(&error_abort);
1840 if (no_shutdown) {
1841 vm_stop(RUN_STATE_SHUTDOWN);
1842 } else {
1843 return true;
1846 if (qemu_reset_requested()) {
1847 pause_all_vcpus();
1848 cpu_synchronize_all_states();
1849 qemu_system_reset(VMRESET_REPORT);
1850 resume_all_vcpus();
1851 if (runstate_needs_reset()) {
1852 runstate_set(RUN_STATE_PAUSED);
1855 if (qemu_wakeup_requested()) {
1856 pause_all_vcpus();
1857 cpu_synchronize_all_states();
1858 qemu_system_reset(VMRESET_SILENT);
1859 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1860 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1861 resume_all_vcpus();
1862 qapi_event_send_wakeup(&error_abort);
1864 if (qemu_powerdown_requested()) {
1865 qemu_system_powerdown();
1867 if (qemu_vmstop_requested(&r)) {
1868 vm_stop(r);
1870 return false;
1873 static void main_loop(void)
1875 bool nonblocking;
1876 int last_io = 0;
1877 #ifdef CONFIG_PROFILER
1878 int64_t ti;
1879 #endif
1880 do {
1881 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1882 #ifdef CONFIG_PROFILER
1883 ti = profile_getclock();
1884 #endif
1885 last_io = main_loop_wait(nonblocking);
1886 #ifdef CONFIG_PROFILER
1887 dev_time += profile_getclock() - ti;
1888 #endif
1889 } while (!main_loop_should_exit());
1892 static void version(void)
1894 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1897 static void QEMU_NORETURN help(int exitcode)
1899 version();
1900 printf("usage: %s [options] [disk_image]\n\n"
1901 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1902 error_get_progname());
1904 #define QEMU_OPTIONS_GENERATE_HELP
1905 #include "qemu-options-wrapper.h"
1907 printf("\nDuring emulation, the following keys are useful:\n"
1908 "ctrl-alt-f toggle full screen\n"
1909 "ctrl-alt-n switch to virtual console 'n'\n"
1910 "ctrl-alt toggle mouse and keyboard grab\n"
1911 "\n"
1912 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1914 exit(exitcode);
1917 #define HAS_ARG 0x0001
1919 typedef struct QEMUOption {
1920 const char *name;
1921 int flags;
1922 int index;
1923 uint32_t arch_mask;
1924 } QEMUOption;
1926 static const QEMUOption qemu_options[] = {
1927 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1928 #define QEMU_OPTIONS_GENERATE_OPTIONS
1929 #include "qemu-options-wrapper.h"
1930 { NULL },
1933 static bool vga_available(void)
1935 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1938 static bool cirrus_vga_available(void)
1940 return object_class_by_name("cirrus-vga")
1941 || object_class_by_name("isa-cirrus-vga");
1944 static bool vmware_vga_available(void)
1946 return object_class_by_name("vmware-svga");
1949 static bool qxl_vga_available(void)
1951 return object_class_by_name("qxl-vga");
1954 static bool tcx_vga_available(void)
1956 return object_class_by_name("SUNW,tcx");
1959 static bool cg3_vga_available(void)
1961 return object_class_by_name("cgthree");
1964 static void select_vgahw (const char *p)
1966 const char *opts;
1968 assert(vga_interface_type == VGA_NONE);
1969 if (strstart(p, "std", &opts)) {
1970 if (vga_available()) {
1971 vga_interface_type = VGA_STD;
1972 } else {
1973 fprintf(stderr, "Error: standard VGA not available\n");
1974 exit(0);
1976 } else if (strstart(p, "cirrus", &opts)) {
1977 if (cirrus_vga_available()) {
1978 vga_interface_type = VGA_CIRRUS;
1979 } else {
1980 fprintf(stderr, "Error: Cirrus VGA not available\n");
1981 exit(0);
1983 } else if (strstart(p, "vmware", &opts)) {
1984 if (vmware_vga_available()) {
1985 vga_interface_type = VGA_VMWARE;
1986 } else {
1987 fprintf(stderr, "Error: VMWare SVGA not available\n");
1988 exit(0);
1990 } else if (strstart(p, "xenfb", &opts)) {
1991 vga_interface_type = VGA_XENFB;
1992 } else if (strstart(p, "qxl", &opts)) {
1993 if (qxl_vga_available()) {
1994 vga_interface_type = VGA_QXL;
1995 } else {
1996 fprintf(stderr, "Error: QXL VGA not available\n");
1997 exit(0);
1999 } else if (strstart(p, "tcx", &opts)) {
2000 if (tcx_vga_available()) {
2001 vga_interface_type = VGA_TCX;
2002 } else {
2003 fprintf(stderr, "Error: TCX framebuffer not available\n");
2004 exit(0);
2006 } else if (strstart(p, "cg3", &opts)) {
2007 if (cg3_vga_available()) {
2008 vga_interface_type = VGA_CG3;
2009 } else {
2010 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2011 exit(0);
2013 } else if (!strstart(p, "none", &opts)) {
2014 invalid_vga:
2015 fprintf(stderr, "Unknown vga type: %s\n", p);
2016 exit(1);
2018 while (*opts) {
2019 const char *nextopt;
2021 if (strstart(opts, ",retrace=", &nextopt)) {
2022 opts = nextopt;
2023 if (strstart(opts, "dumb", &nextopt))
2024 vga_retrace_method = VGA_RETRACE_DUMB;
2025 else if (strstart(opts, "precise", &nextopt))
2026 vga_retrace_method = VGA_RETRACE_PRECISE;
2027 else goto invalid_vga;
2028 } else goto invalid_vga;
2029 opts = nextopt;
2033 static DisplayType select_display(const char *p)
2035 const char *opts;
2036 DisplayType display = DT_DEFAULT;
2038 if (strstart(p, "sdl", &opts)) {
2039 #ifdef CONFIG_SDL
2040 display = DT_SDL;
2041 while (*opts) {
2042 const char *nextopt;
2044 if (strstart(opts, ",frame=", &nextopt)) {
2045 opts = nextopt;
2046 if (strstart(opts, "on", &nextopt)) {
2047 no_frame = 0;
2048 } else if (strstart(opts, "off", &nextopt)) {
2049 no_frame = 1;
2050 } else {
2051 goto invalid_sdl_args;
2053 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2054 opts = nextopt;
2055 if (strstart(opts, "on", &nextopt)) {
2056 alt_grab = 1;
2057 } else if (strstart(opts, "off", &nextopt)) {
2058 alt_grab = 0;
2059 } else {
2060 goto invalid_sdl_args;
2062 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2063 opts = nextopt;
2064 if (strstart(opts, "on", &nextopt)) {
2065 ctrl_grab = 1;
2066 } else if (strstart(opts, "off", &nextopt)) {
2067 ctrl_grab = 0;
2068 } else {
2069 goto invalid_sdl_args;
2071 } else if (strstart(opts, ",window_close=", &nextopt)) {
2072 opts = nextopt;
2073 if (strstart(opts, "on", &nextopt)) {
2074 no_quit = 0;
2075 } else if (strstart(opts, "off", &nextopt)) {
2076 no_quit = 1;
2077 } else {
2078 goto invalid_sdl_args;
2080 } else {
2081 invalid_sdl_args:
2082 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2083 exit(1);
2085 opts = nextopt;
2087 #else
2088 fprintf(stderr, "SDL support is disabled\n");
2089 exit(1);
2090 #endif
2091 } else if (strstart(p, "vnc", &opts)) {
2092 #ifdef CONFIG_VNC
2093 display_remote++;
2095 if (*opts) {
2096 const char *nextopt;
2098 if (strstart(opts, "=", &nextopt)) {
2099 vnc_display = nextopt;
2102 if (!vnc_display) {
2103 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2104 exit(1);
2106 #else
2107 fprintf(stderr, "VNC support is disabled\n");
2108 exit(1);
2109 #endif
2110 } else if (strstart(p, "curses", &opts)) {
2111 #ifdef CONFIG_CURSES
2112 display = DT_CURSES;
2113 #else
2114 fprintf(stderr, "Curses support is disabled\n");
2115 exit(1);
2116 #endif
2117 } else if (strstart(p, "gtk", &opts)) {
2118 #ifdef CONFIG_GTK
2119 display = DT_GTK;
2120 while (*opts) {
2121 const char *nextopt;
2123 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2124 opts = nextopt;
2125 if (strstart(opts, "on", &nextopt)) {
2126 grab_on_hover = true;
2127 } else if (strstart(opts, "off", &nextopt)) {
2128 grab_on_hover = false;
2129 } else {
2130 goto invalid_gtk_args;
2132 } else {
2133 invalid_gtk_args:
2134 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2135 exit(1);
2137 opts = nextopt;
2139 #else
2140 fprintf(stderr, "GTK support is disabled\n");
2141 exit(1);
2142 #endif
2143 } else if (strstart(p, "none", &opts)) {
2144 display = DT_NONE;
2145 } else {
2146 fprintf(stderr, "Unknown display type: %s\n", p);
2147 exit(1);
2150 return display;
2153 static int balloon_parse(const char *arg)
2155 QemuOpts *opts;
2157 if (strcmp(arg, "none") == 0) {
2158 return 0;
2161 if (!strncmp(arg, "virtio", 6)) {
2162 if (arg[6] == ',') {
2163 /* have params -> parse them */
2164 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2165 if (!opts)
2166 return -1;
2167 } else {
2168 /* create empty opts */
2169 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2170 &error_abort);
2172 qemu_opt_set(opts, "driver", "virtio-balloon");
2173 return 0;
2176 return -1;
2179 #if defined(CONFIG_RUBY)
2180 /* Ruby interface for QEMU. See README.EXT for programming example. */
2181 #warning mit ruby
2182 #include <ruby.h>
2184 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2186 int i;
2187 for (i = 0; i < argc; i++) {
2188 argv[i] = StringValue(argv[i]);
2189 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2191 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2192 //~ mode = 0666; /* default value */
2193 //~ }
2194 return INT2FIX(argc);
2197 void Init_qemu(void)
2199 printf("%s\n", __func__);
2200 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2201 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2202 #if 0
2203 rb_define_method();
2204 #endif
2206 #endif /* CONFIG_RUBY */
2208 #if defined(CONFIG_DLL)
2209 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2211 return FALSE;
2213 #endif /* CONFIG_DLL */
2215 char *qemu_find_file(int type, const char *name)
2217 int i;
2218 const char *subdir;
2219 char *buf;
2221 /* Try the name as a straight path first */
2222 if (access(name, R_OK) == 0) {
2223 trace_load_file(name, name);
2224 return g_strdup(name);
2227 switch (type) {
2228 case QEMU_FILE_TYPE_BIOS:
2229 subdir = "";
2230 break;
2231 case QEMU_FILE_TYPE_KEYMAP:
2232 subdir = "keymaps/";
2233 break;
2234 default:
2235 abort();
2238 for (i = 0; i < data_dir_idx; i++) {
2239 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2240 if (access(buf, R_OK) == 0) {
2241 trace_load_file(name, buf);
2242 return buf;
2244 g_free(buf);
2246 return NULL;
2249 static int device_help_func(QemuOpts *opts, void *opaque)
2251 return qdev_device_help(opts);
2254 static int device_init_func(QemuOpts *opts, void *opaque)
2256 DeviceState *dev;
2258 dev = qdev_device_add(opts);
2259 if (!dev)
2260 return -1;
2261 object_unref(OBJECT(dev));
2262 return 0;
2265 static int chardev_init_func(QemuOpts *opts, void *opaque)
2267 Error *local_err = NULL;
2269 qemu_chr_new_from_opts(opts, NULL, &local_err);
2270 if (local_err) {
2271 error_report("%s", error_get_pretty(local_err));
2272 error_free(local_err);
2273 return -1;
2275 return 0;
2278 #ifdef CONFIG_VIRTFS
2279 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2281 int ret;
2282 ret = qemu_fsdev_add(opts);
2284 return ret;
2286 #endif
2288 static int mon_init_func(QemuOpts *opts, void *opaque)
2290 CharDriverState *chr;
2291 const char *chardev;
2292 const char *mode;
2293 int flags;
2295 mode = qemu_opt_get(opts, "mode");
2296 if (mode == NULL) {
2297 mode = "readline";
2299 if (strcmp(mode, "readline") == 0) {
2300 flags = MONITOR_USE_READLINE;
2301 } else if (strcmp(mode, "control") == 0) {
2302 flags = MONITOR_USE_CONTROL;
2303 } else {
2304 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2305 exit(1);
2308 if (qemu_opt_get_bool(opts, "pretty", 0))
2309 flags |= MONITOR_USE_PRETTY;
2311 if (qemu_opt_get_bool(opts, "default", 0))
2312 flags |= MONITOR_IS_DEFAULT;
2314 chardev = qemu_opt_get(opts, "chardev");
2315 chr = qemu_chr_find(chardev);
2316 if (chr == NULL) {
2317 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2318 exit(1);
2321 qemu_chr_fe_claim_no_fail(chr);
2322 monitor_init(chr, flags);
2323 return 0;
2326 static void monitor_parse(const char *optarg, const char *mode)
2328 static int monitor_device_index = 0;
2329 QemuOpts *opts;
2330 const char *p;
2331 char label[32];
2332 int def = 0;
2334 if (strstart(optarg, "chardev:", &p)) {
2335 snprintf(label, sizeof(label), "%s", p);
2336 } else {
2337 snprintf(label, sizeof(label), "compat_monitor%d",
2338 monitor_device_index);
2339 if (monitor_device_index == 0) {
2340 def = 1;
2342 opts = qemu_chr_parse_compat(label, optarg);
2343 if (!opts) {
2344 fprintf(stderr, "parse error: %s\n", optarg);
2345 exit(1);
2349 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2350 if (!opts) {
2351 fprintf(stderr, "duplicate chardev: %s\n", label);
2352 exit(1);
2354 qemu_opt_set(opts, "mode", mode);
2355 qemu_opt_set(opts, "chardev", label);
2356 if (def)
2357 qemu_opt_set(opts, "default", "on");
2358 monitor_device_index++;
2361 struct device_config {
2362 enum {
2363 DEV_USB, /* -usbdevice */
2364 DEV_BT, /* -bt */
2365 DEV_SERIAL, /* -serial */
2366 DEV_PARALLEL, /* -parallel */
2367 DEV_VIRTCON, /* -virtioconsole */
2368 DEV_DEBUGCON, /* -debugcon */
2369 DEV_GDB, /* -gdb, -s */
2370 DEV_SCLP, /* s390 sclp */
2371 } type;
2372 const char *cmdline;
2373 Location loc;
2374 QTAILQ_ENTRY(device_config) next;
2377 static QTAILQ_HEAD(, device_config) device_configs =
2378 QTAILQ_HEAD_INITIALIZER(device_configs);
2380 static void add_device_config(int type, const char *cmdline)
2382 struct device_config *conf;
2384 conf = g_malloc0(sizeof(*conf));
2385 conf->type = type;
2386 conf->cmdline = cmdline;
2387 loc_save(&conf->loc);
2388 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2391 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2393 struct device_config *conf;
2394 int rc;
2396 QTAILQ_FOREACH(conf, &device_configs, next) {
2397 if (conf->type != type)
2398 continue;
2399 loc_push_restore(&conf->loc);
2400 rc = func(conf->cmdline);
2401 loc_pop(&conf->loc);
2402 if (rc) {
2403 return rc;
2406 return 0;
2409 static int serial_parse(const char *devname)
2411 static int index = 0;
2412 char label[32];
2414 if (strcmp(devname, "none") == 0)
2415 return 0;
2416 if (index == MAX_SERIAL_PORTS) {
2417 fprintf(stderr, "qemu: too many serial ports\n");
2418 exit(1);
2420 snprintf(label, sizeof(label), "serial%d", index);
2421 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2422 if (!serial_hds[index]) {
2423 fprintf(stderr, "qemu: could not connect serial device"
2424 " to character backend '%s'\n", devname);
2425 return -1;
2427 index++;
2428 return 0;
2431 static int parallel_parse(const char *devname)
2433 static int index = 0;
2434 char label[32];
2436 if (strcmp(devname, "none") == 0)
2437 return 0;
2438 if (index == MAX_PARALLEL_PORTS) {
2439 fprintf(stderr, "qemu: too many parallel ports\n");
2440 exit(1);
2442 snprintf(label, sizeof(label), "parallel%d", index);
2443 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2444 if (!parallel_hds[index]) {
2445 fprintf(stderr, "qemu: could not connect parallel device"
2446 " to character backend '%s'\n", devname);
2447 return -1;
2449 index++;
2450 return 0;
2453 static int virtcon_parse(const char *devname)
2455 QemuOptsList *device = qemu_find_opts("device");
2456 static int index = 0;
2457 char label[32];
2458 QemuOpts *bus_opts, *dev_opts;
2460 if (strcmp(devname, "none") == 0)
2461 return 0;
2462 if (index == MAX_VIRTIO_CONSOLES) {
2463 fprintf(stderr, "qemu: too many virtio consoles\n");
2464 exit(1);
2467 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2468 if (arch_type == QEMU_ARCH_S390X) {
2469 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2470 } else {
2471 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2474 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2475 qemu_opt_set(dev_opts, "driver", "virtconsole");
2477 snprintf(label, sizeof(label), "virtcon%d", index);
2478 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2479 if (!virtcon_hds[index]) {
2480 fprintf(stderr, "qemu: could not connect virtio console"
2481 " to character backend '%s'\n", devname);
2482 return -1;
2484 qemu_opt_set(dev_opts, "chardev", label);
2486 index++;
2487 return 0;
2490 static int sclp_parse(const char *devname)
2492 QemuOptsList *device = qemu_find_opts("device");
2493 static int index = 0;
2494 char label[32];
2495 QemuOpts *dev_opts;
2497 if (strcmp(devname, "none") == 0) {
2498 return 0;
2500 if (index == MAX_SCLP_CONSOLES) {
2501 fprintf(stderr, "qemu: too many sclp consoles\n");
2502 exit(1);
2505 assert(arch_type == QEMU_ARCH_S390X);
2507 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2508 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2510 snprintf(label, sizeof(label), "sclpcon%d", index);
2511 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2512 if (!sclp_hds[index]) {
2513 fprintf(stderr, "qemu: could not connect sclp console"
2514 " to character backend '%s'\n", devname);
2515 return -1;
2517 qemu_opt_set(dev_opts, "chardev", label);
2519 index++;
2520 return 0;
2523 static int debugcon_parse(const char *devname)
2525 QemuOpts *opts;
2527 if (!qemu_chr_new("debugcon", devname, NULL)) {
2528 exit(1);
2530 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2531 if (!opts) {
2532 fprintf(stderr, "qemu: already have a debugcon device\n");
2533 exit(1);
2535 qemu_opt_set(opts, "driver", "isa-debugcon");
2536 qemu_opt_set(opts, "chardev", "debugcon");
2537 return 0;
2540 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2542 const MachineClass *mc1 = a, *mc2 = b;
2543 int res;
2545 if (mc1->family == NULL) {
2546 if (mc2->family == NULL) {
2547 /* Compare standalone machine types against each other; they sort
2548 * in increasing order.
2550 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2551 object_class_get_name(OBJECT_CLASS(mc2)));
2554 /* Standalone machine types sort after families. */
2555 return 1;
2558 if (mc2->family == NULL) {
2559 /* Families sort before standalone machine types. */
2560 return -1;
2563 /* Families sort between each other alphabetically increasingly. */
2564 res = strcmp(mc1->family, mc2->family);
2565 if (res != 0) {
2566 return res;
2569 /* Within the same family, machine types sort in decreasing order. */
2570 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2571 object_class_get_name(OBJECT_CLASS(mc1)));
2574 static MachineClass *machine_parse(const char *name)
2576 MachineClass *mc = NULL;
2577 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2579 if (name) {
2580 mc = find_machine(name);
2582 if (mc) {
2583 return mc;
2585 if (name && !is_help_option(name)) {
2586 error_report("Unsupported machine type");
2587 error_printf("Use -machine help to list supported machines!\n");
2588 } else {
2589 printf("Supported machines are:\n");
2590 machines = g_slist_sort(machines, machine_class_cmp);
2591 for (el = machines; el; el = el->next) {
2592 MachineClass *mc = el->data;
2593 if (mc->alias) {
2594 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2596 printf("%-20s %s%s\n", mc->name, mc->desc,
2597 mc->is_default ? " (default)" : "");
2601 g_slist_free(machines);
2602 exit(!name || !is_help_option(name));
2605 void qemu_add_exit_notifier(Notifier *notify)
2607 notifier_list_add(&exit_notifiers, notify);
2610 void qemu_remove_exit_notifier(Notifier *notify)
2612 notifier_remove(notify);
2615 static void qemu_run_exit_notifiers(void)
2617 notifier_list_notify(&exit_notifiers, NULL);
2620 void qemu_add_machine_init_done_notifier(Notifier *notify)
2622 notifier_list_add(&machine_init_done_notifiers, notify);
2625 static void qemu_run_machine_init_done_notifiers(void)
2627 notifier_list_notify(&machine_init_done_notifiers, NULL);
2630 static const QEMUOption *lookup_opt(int argc, char **argv,
2631 const char **poptarg, int *poptind)
2633 const QEMUOption *popt;
2634 int optind = *poptind;
2635 char *r = argv[optind];
2636 const char *optarg;
2638 loc_set_cmdline(argv, optind, 1);
2639 optind++;
2640 /* Treat --foo the same as -foo. */
2641 if (r[1] == '-')
2642 r++;
2643 popt = qemu_options;
2644 for(;;) {
2645 if (!popt->name) {
2646 error_report("invalid option");
2647 exit(1);
2649 if (!strcmp(popt->name, r + 1))
2650 break;
2651 popt++;
2653 if (popt->flags & HAS_ARG) {
2654 if (optind >= argc) {
2655 error_report("requires an argument");
2656 exit(1);
2658 optarg = argv[optind++];
2659 loc_set_cmdline(argv, optind - 2, 2);
2660 } else {
2661 optarg = NULL;
2664 *poptarg = optarg;
2665 *poptind = optind;
2667 return popt;
2670 static gpointer malloc_and_trace(gsize n_bytes)
2672 void *ptr = malloc(n_bytes);
2673 trace_g_malloc(n_bytes, ptr);
2674 return ptr;
2677 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2679 void *ptr = realloc(mem, n_bytes);
2680 trace_g_realloc(mem, n_bytes, ptr);
2681 return ptr;
2684 static void free_and_trace(gpointer mem)
2686 trace_g_free(mem);
2687 free(mem);
2690 static int machine_set_property(const char *name, const char *value,
2691 void *opaque)
2693 Object *obj = OBJECT(opaque);
2694 StringInputVisitor *siv;
2695 Error *local_err = NULL;
2696 char *c, *qom_name;
2698 if (strcmp(name, "type") == 0) {
2699 return 0;
2702 qom_name = g_strdup(name);
2703 c = qom_name;
2704 while (*c++) {
2705 if (*c == '_') {
2706 *c = '-';
2710 siv = string_input_visitor_new(value);
2711 object_property_set(obj, string_input_get_visitor(siv), qom_name, &local_err);
2712 string_input_visitor_cleanup(siv);
2713 g_free(qom_name);
2715 if (local_err) {
2716 qerror_report_err(local_err);
2717 error_free(local_err);
2718 return -1;
2721 return 0;
2724 static int object_create(QemuOpts *opts, void *opaque)
2726 Error *err = NULL;
2727 char *type = NULL;
2728 char *id = NULL;
2729 void *dummy = NULL;
2730 OptsVisitor *ov;
2731 QDict *pdict;
2733 ov = opts_visitor_new(opts);
2734 pdict = qemu_opts_to_qdict(opts, NULL);
2736 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2737 if (err) {
2738 goto out;
2741 qdict_del(pdict, "qom-type");
2742 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2743 if (err) {
2744 goto out;
2747 qdict_del(pdict, "id");
2748 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2749 if (err) {
2750 goto out;
2753 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2754 if (err) {
2755 goto out;
2757 visit_end_struct(opts_get_visitor(ov), &err);
2758 if (err) {
2759 qmp_object_del(id, NULL);
2762 out:
2763 opts_visitor_cleanup(ov);
2765 QDECREF(pdict);
2766 g_free(id);
2767 g_free(type);
2768 g_free(dummy);
2769 if (err) {
2770 qerror_report_err(err);
2771 error_free(err);
2772 return -1;
2774 return 0;
2777 int main(int argc, char **argv)
2779 int i;
2780 int snapshot, linux_boot;
2781 const char *initrd_filename;
2782 const char *kernel_filename, *kernel_cmdline;
2783 const char *boot_order;
2784 DisplayState *ds;
2785 int cyls, heads, secs, translation;
2786 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2787 QemuOptsList *olist;
2788 int optind;
2789 const char *optarg;
2790 const char *loadvm = NULL;
2791 MachineClass *machine_class;
2792 const char *cpu_model;
2793 const char *vga_model = NULL;
2794 const char *qtest_chrdev = NULL;
2795 const char *qtest_log = NULL;
2796 const char *pid_file = NULL;
2797 const char *incoming = NULL;
2798 #ifdef CONFIG_VNC
2799 int show_vnc_port = 0;
2800 #endif
2801 bool defconfig = true;
2802 bool userconfig = true;
2803 const char *log_mask = NULL;
2804 const char *log_file = NULL;
2805 GMemVTable mem_trace = {
2806 .malloc = malloc_and_trace,
2807 .realloc = realloc_and_trace,
2808 .free = free_and_trace,
2810 const char *trace_events = NULL;
2811 const char *trace_file = NULL;
2812 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2813 1024 * 1024;
2814 ram_addr_t maxram_size = default_ram_size;
2815 uint64_t ram_slots = 0;
2816 FILE *vmstate_dump_file = NULL;
2817 Error *main_loop_err = NULL;
2819 atexit(qemu_run_exit_notifiers);
2820 error_set_progname(argv[0]);
2821 qemu_init_exec_dir(argv[0]);
2823 g_mem_set_vtable(&mem_trace);
2825 module_call_init(MODULE_INIT_QOM);
2827 qemu_add_opts(&qemu_drive_opts);
2828 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2829 qemu_add_drive_opts(&qemu_common_drive_opts);
2830 qemu_add_drive_opts(&qemu_drive_opts);
2831 qemu_add_opts(&qemu_chardev_opts);
2832 qemu_add_opts(&qemu_device_opts);
2833 qemu_add_opts(&qemu_netdev_opts);
2834 qemu_add_opts(&qemu_net_opts);
2835 qemu_add_opts(&qemu_rtc_opts);
2836 qemu_add_opts(&qemu_global_opts);
2837 qemu_add_opts(&qemu_mon_opts);
2838 qemu_add_opts(&qemu_trace_opts);
2839 qemu_add_opts(&qemu_option_rom_opts);
2840 qemu_add_opts(&qemu_machine_opts);
2841 qemu_add_opts(&qemu_mem_opts);
2842 qemu_add_opts(&qemu_smp_opts);
2843 qemu_add_opts(&qemu_boot_opts);
2844 qemu_add_opts(&qemu_sandbox_opts);
2845 qemu_add_opts(&qemu_add_fd_opts);
2846 qemu_add_opts(&qemu_object_opts);
2847 qemu_add_opts(&qemu_tpmdev_opts);
2848 qemu_add_opts(&qemu_realtime_opts);
2849 qemu_add_opts(&qemu_msg_opts);
2850 qemu_add_opts(&qemu_name_opts);
2851 qemu_add_opts(&qemu_numa_opts);
2852 qemu_add_opts(&qemu_icount_opts);
2854 runstate_init();
2856 rtc_clock = QEMU_CLOCK_HOST;
2858 QLIST_INIT (&vm_change_state_head);
2859 os_setup_early_signal_handling();
2861 module_call_init(MODULE_INIT_MACHINE);
2862 machine_class = 0;
2863 optarg = strrchr(argv[0], '/');
2864 if (optarg != 0) {
2865 machine_class = find_machine(optarg + 1);
2867 if (!machine_class) {
2868 machine_class = find_default_machine();
2870 cpu_model = NULL;
2871 ram_size = default_ram_size;
2872 snapshot = 0;
2873 cyls = heads = secs = 0;
2874 translation = BIOS_ATA_TRANSLATION_AUTO;
2876 for (i = 0; i < MAX_NODES; i++) {
2877 numa_info[i].node_mem = 0;
2878 numa_info[i].present = false;
2879 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2882 nb_numa_nodes = 0;
2883 max_numa_nodeid = 0;
2884 nb_nics = 0;
2886 bdrv_init_with_whitelist();
2888 autostart = 1;
2890 /* first pass of option parsing */
2891 optind = 1;
2892 while (optind < argc) {
2893 if (argv[optind][0] != '-') {
2894 /* disk image */
2895 optind++;
2896 } else {
2897 const QEMUOption *popt;
2899 popt = lookup_opt(argc, argv, &optarg, &optind);
2900 switch (popt->index) {
2901 case QEMU_OPTION_nodefconfig:
2902 defconfig = false;
2903 break;
2904 case QEMU_OPTION_nouserconfig:
2905 userconfig = false;
2906 break;
2911 if (defconfig) {
2912 int ret;
2913 ret = qemu_read_default_config_files(userconfig);
2914 if (ret < 0) {
2915 exit(1);
2919 /* second pass of option parsing */
2920 optind = 1;
2921 for(;;) {
2922 if (optind >= argc)
2923 break;
2924 if (argv[optind][0] != '-') {
2925 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2926 } else {
2927 const QEMUOption *popt;
2929 popt = lookup_opt(argc, argv, &optarg, &optind);
2930 if (!(popt->arch_mask & arch_type)) {
2931 printf("Option %s not supported for this target\n", popt->name);
2932 exit(1);
2934 switch(popt->index) {
2935 case QEMU_OPTION_M:
2936 machine_class = machine_parse(optarg);
2937 break;
2938 case QEMU_OPTION_no_kvm_irqchip: {
2939 olist = qemu_find_opts("machine");
2940 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2941 break;
2943 case QEMU_OPTION_cpu:
2944 /* hw initialization will check this */
2945 cpu_model = optarg;
2946 break;
2947 case QEMU_OPTION_hda:
2949 char buf[256];
2950 if (cyls == 0)
2951 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2952 else
2953 snprintf(buf, sizeof(buf),
2954 "%s,cyls=%d,heads=%d,secs=%d%s",
2955 HD_OPTS , cyls, heads, secs,
2956 translation == BIOS_ATA_TRANSLATION_LBA ?
2957 ",trans=lba" :
2958 translation == BIOS_ATA_TRANSLATION_NONE ?
2959 ",trans=none" : "");
2960 drive_add(IF_DEFAULT, 0, optarg, buf);
2961 break;
2963 case QEMU_OPTION_hdb:
2964 case QEMU_OPTION_hdc:
2965 case QEMU_OPTION_hdd:
2966 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2967 HD_OPTS);
2968 break;
2969 case QEMU_OPTION_drive:
2970 if (drive_def(optarg) == NULL) {
2971 exit(1);
2973 break;
2974 case QEMU_OPTION_set:
2975 if (qemu_set_option(optarg) != 0)
2976 exit(1);
2977 break;
2978 case QEMU_OPTION_global:
2979 if (qemu_global_option(optarg) != 0)
2980 exit(1);
2981 break;
2982 case QEMU_OPTION_mtdblock:
2983 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2984 break;
2985 case QEMU_OPTION_sd:
2986 drive_add(IF_SD, -1, optarg, SD_OPTS);
2987 break;
2988 case QEMU_OPTION_pflash:
2989 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2990 break;
2991 case QEMU_OPTION_snapshot:
2992 snapshot = 1;
2993 break;
2994 case QEMU_OPTION_hdachs:
2996 const char *p;
2997 p = optarg;
2998 cyls = strtol(p, (char **)&p, 0);
2999 if (cyls < 1 || cyls > 16383)
3000 goto chs_fail;
3001 if (*p != ',')
3002 goto chs_fail;
3003 p++;
3004 heads = strtol(p, (char **)&p, 0);
3005 if (heads < 1 || heads > 16)
3006 goto chs_fail;
3007 if (*p != ',')
3008 goto chs_fail;
3009 p++;
3010 secs = strtol(p, (char **)&p, 0);
3011 if (secs < 1 || secs > 63)
3012 goto chs_fail;
3013 if (*p == ',') {
3014 p++;
3015 if (!strcmp(p, "large")) {
3016 translation = BIOS_ATA_TRANSLATION_LARGE;
3017 } else if (!strcmp(p, "rechs")) {
3018 translation = BIOS_ATA_TRANSLATION_RECHS;
3019 } else if (!strcmp(p, "none")) {
3020 translation = BIOS_ATA_TRANSLATION_NONE;
3021 } else if (!strcmp(p, "lba")) {
3022 translation = BIOS_ATA_TRANSLATION_LBA;
3023 } else if (!strcmp(p, "auto")) {
3024 translation = BIOS_ATA_TRANSLATION_AUTO;
3025 } else {
3026 goto chs_fail;
3028 } else if (*p != '\0') {
3029 chs_fail:
3030 fprintf(stderr, "qemu: invalid physical CHS format\n");
3031 exit(1);
3033 if (hda_opts != NULL) {
3034 char num[16];
3035 snprintf(num, sizeof(num), "%d", cyls);
3036 qemu_opt_set(hda_opts, "cyls", num);
3037 snprintf(num, sizeof(num), "%d", heads);
3038 qemu_opt_set(hda_opts, "heads", num);
3039 snprintf(num, sizeof(num), "%d", secs);
3040 qemu_opt_set(hda_opts, "secs", num);
3041 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3042 qemu_opt_set(hda_opts, "trans", "large");
3043 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3044 qemu_opt_set(hda_opts, "trans", "rechs");
3045 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3046 qemu_opt_set(hda_opts, "trans", "lba");
3047 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3048 qemu_opt_set(hda_opts, "trans", "none");
3052 break;
3053 case QEMU_OPTION_numa:
3054 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3055 if (!opts) {
3056 exit(1);
3058 break;
3059 case QEMU_OPTION_display:
3060 display_type = select_display(optarg);
3061 break;
3062 case QEMU_OPTION_nographic:
3063 display_type = DT_NOGRAPHIC;
3064 break;
3065 case QEMU_OPTION_curses:
3066 #ifdef CONFIG_CURSES
3067 display_type = DT_CURSES;
3068 #else
3069 fprintf(stderr, "Curses support is disabled\n");
3070 exit(1);
3071 #endif
3072 break;
3073 case QEMU_OPTION_portrait:
3074 graphic_rotate = 90;
3075 break;
3076 case QEMU_OPTION_rotate:
3077 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3078 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3079 graphic_rotate != 180 && graphic_rotate != 270) {
3080 fprintf(stderr,
3081 "qemu: only 90, 180, 270 deg rotation is available\n");
3082 exit(1);
3084 break;
3085 case QEMU_OPTION_kernel:
3086 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3087 break;
3088 case QEMU_OPTION_initrd:
3089 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3090 break;
3091 case QEMU_OPTION_append:
3092 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3093 break;
3094 case QEMU_OPTION_dtb:
3095 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3096 break;
3097 case QEMU_OPTION_cdrom:
3098 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3099 break;
3100 case QEMU_OPTION_boot:
3101 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3102 if (!opts) {
3103 exit(1);
3105 break;
3106 case QEMU_OPTION_fda:
3107 case QEMU_OPTION_fdb:
3108 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3109 optarg, FD_OPTS);
3110 break;
3111 case QEMU_OPTION_no_fd_bootchk:
3112 fd_bootchk = 0;
3113 break;
3114 case QEMU_OPTION_netdev:
3115 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3116 exit(1);
3118 break;
3119 case QEMU_OPTION_net:
3120 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3121 exit(1);
3123 break;
3124 #ifdef CONFIG_LIBISCSI
3125 case QEMU_OPTION_iscsi:
3126 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3127 if (!opts) {
3128 exit(1);
3130 break;
3131 #endif
3132 #ifdef CONFIG_SLIRP
3133 case QEMU_OPTION_tftp:
3134 legacy_tftp_prefix = optarg;
3135 break;
3136 case QEMU_OPTION_bootp:
3137 legacy_bootp_filename = optarg;
3138 break;
3139 case QEMU_OPTION_redir:
3140 if (net_slirp_redir(optarg) < 0)
3141 exit(1);
3142 break;
3143 #endif
3144 case QEMU_OPTION_bt:
3145 add_device_config(DEV_BT, optarg);
3146 break;
3147 case QEMU_OPTION_audio_help:
3148 AUD_help ();
3149 exit (0);
3150 break;
3151 case QEMU_OPTION_soundhw:
3152 select_soundhw (optarg);
3153 break;
3154 case QEMU_OPTION_h:
3155 help(0);
3156 break;
3157 case QEMU_OPTION_version:
3158 version();
3159 exit(0);
3160 break;
3161 case QEMU_OPTION_m: {
3162 uint64_t sz;
3163 const char *mem_str;
3164 const char *maxmem_str, *slots_str;
3166 opts = qemu_opts_parse(qemu_find_opts("memory"),
3167 optarg, 1);
3168 if (!opts) {
3169 exit(EXIT_FAILURE);
3172 mem_str = qemu_opt_get(opts, "size");
3173 if (!mem_str) {
3174 error_report("invalid -m option, missing 'size' option");
3175 exit(EXIT_FAILURE);
3177 if (!*mem_str) {
3178 error_report("missing 'size' option value");
3179 exit(EXIT_FAILURE);
3182 sz = qemu_opt_get_size(opts, "size", ram_size);
3184 /* Fix up legacy suffix-less format */
3185 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3186 uint64_t overflow_check = sz;
3188 sz <<= 20;
3189 if ((sz >> 20) != overflow_check) {
3190 error_report("too large 'size' option value");
3191 exit(EXIT_FAILURE);
3195 /* backward compatibility behaviour for case "-m 0" */
3196 if (sz == 0) {
3197 sz = default_ram_size;
3200 sz = QEMU_ALIGN_UP(sz, 8192);
3201 ram_size = sz;
3202 if (ram_size != sz) {
3203 error_report("ram size too large");
3204 exit(EXIT_FAILURE);
3206 maxram_size = ram_size;
3208 maxmem_str = qemu_opt_get(opts, "maxmem");
3209 slots_str = qemu_opt_get(opts, "slots");
3210 if (maxmem_str && slots_str) {
3211 uint64_t slots;
3213 sz = qemu_opt_get_size(opts, "maxmem", 0);
3214 if (sz < ram_size) {
3215 error_report("invalid -m option value: maxmem "
3216 "(0x%" PRIx64 ") <= initial memory (0x"
3217 RAM_ADDR_FMT ")", sz, ram_size);
3218 exit(EXIT_FAILURE);
3221 slots = qemu_opt_get_number(opts, "slots", 0);
3222 if ((sz > ram_size) && !slots) {
3223 error_report("invalid -m option value: maxmem "
3224 "(0x%" PRIx64 ") more than initial memory (0x"
3225 RAM_ADDR_FMT ") but no hotplug slots where "
3226 "specified", sz, ram_size);
3227 exit(EXIT_FAILURE);
3230 if ((sz <= ram_size) && slots) {
3231 error_report("invalid -m option value: %"
3232 PRIu64 " hotplug slots where specified but "
3233 "maxmem (0x%" PRIx64 ") <= initial memory (0x"
3234 RAM_ADDR_FMT ")", slots, sz, ram_size);
3235 exit(EXIT_FAILURE);
3237 maxram_size = sz;
3238 ram_slots = slots;
3239 } else if ((!maxmem_str && slots_str) ||
3240 (maxmem_str && !slots_str)) {
3241 error_report("invalid -m option value: missing "
3242 "'%s' option", slots_str ? "maxmem" : "slots");
3243 exit(EXIT_FAILURE);
3245 break;
3247 #ifdef CONFIG_TPM
3248 case QEMU_OPTION_tpmdev:
3249 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3250 exit(1);
3252 break;
3253 #endif
3254 case QEMU_OPTION_mempath:
3255 mem_path = optarg;
3256 break;
3257 case QEMU_OPTION_mem_prealloc:
3258 mem_prealloc = 1;
3259 break;
3260 case QEMU_OPTION_d:
3261 log_mask = optarg;
3262 break;
3263 case QEMU_OPTION_D:
3264 log_file = optarg;
3265 break;
3266 case QEMU_OPTION_s:
3267 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3268 break;
3269 case QEMU_OPTION_gdb:
3270 add_device_config(DEV_GDB, optarg);
3271 break;
3272 case QEMU_OPTION_L:
3273 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3274 data_dir[data_dir_idx++] = optarg;
3276 break;
3277 case QEMU_OPTION_bios:
3278 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3279 break;
3280 case QEMU_OPTION_singlestep:
3281 singlestep = 1;
3282 break;
3283 case QEMU_OPTION_S:
3284 autostart = 0;
3285 break;
3286 case QEMU_OPTION_k:
3287 keyboard_layout = optarg;
3288 break;
3289 case QEMU_OPTION_localtime:
3290 rtc_utc = 0;
3291 break;
3292 case QEMU_OPTION_vga:
3293 vga_model = optarg;
3294 default_vga = 0;
3295 break;
3296 case QEMU_OPTION_g:
3298 const char *p;
3299 int w, h, depth;
3300 p = optarg;
3301 w = strtol(p, (char **)&p, 10);
3302 if (w <= 0) {
3303 graphic_error:
3304 fprintf(stderr, "qemu: invalid resolution or depth\n");
3305 exit(1);
3307 if (*p != 'x')
3308 goto graphic_error;
3309 p++;
3310 h = strtol(p, (char **)&p, 10);
3311 if (h <= 0)
3312 goto graphic_error;
3313 if (*p == 'x') {
3314 p++;
3315 depth = strtol(p, (char **)&p, 10);
3316 if (depth != 8 && depth != 15 && depth != 16 &&
3317 depth != 24 && depth != 32)
3318 goto graphic_error;
3319 } else if (*p == '\0') {
3320 depth = graphic_depth;
3321 } else {
3322 goto graphic_error;
3325 graphic_width = w;
3326 graphic_height = h;
3327 graphic_depth = depth;
3329 break;
3330 case QEMU_OPTION_echr:
3332 char *r;
3333 term_escape_char = strtol(optarg, &r, 0);
3334 if (r == optarg)
3335 printf("Bad argument to echr\n");
3336 break;
3338 case QEMU_OPTION_monitor:
3339 default_monitor = 0;
3340 if (strncmp(optarg, "none", 4)) {
3341 monitor_parse(optarg, "readline");
3343 break;
3344 case QEMU_OPTION_qmp:
3345 monitor_parse(optarg, "control");
3346 default_monitor = 0;
3347 break;
3348 case QEMU_OPTION_mon:
3349 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3350 if (!opts) {
3351 exit(1);
3353 default_monitor = 0;
3354 break;
3355 case QEMU_OPTION_chardev:
3356 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3357 if (!opts) {
3358 exit(1);
3360 break;
3361 case QEMU_OPTION_fsdev:
3362 olist = qemu_find_opts("fsdev");
3363 if (!olist) {
3364 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3365 exit(1);
3367 opts = qemu_opts_parse(olist, optarg, 1);
3368 if (!opts) {
3369 exit(1);
3371 break;
3372 case QEMU_OPTION_virtfs: {
3373 QemuOpts *fsdev;
3374 QemuOpts *device;
3375 const char *writeout, *sock_fd, *socket;
3377 olist = qemu_find_opts("virtfs");
3378 if (!olist) {
3379 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3380 exit(1);
3382 opts = qemu_opts_parse(olist, optarg, 1);
3383 if (!opts) {
3384 exit(1);
3387 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3388 qemu_opt_get(opts, "mount_tag") == NULL) {
3389 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3390 exit(1);
3392 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3393 qemu_opt_get(opts, "mount_tag"),
3394 1, NULL);
3395 if (!fsdev) {
3396 fprintf(stderr, "duplicate fsdev id: %s\n",
3397 qemu_opt_get(opts, "mount_tag"));
3398 exit(1);
3401 writeout = qemu_opt_get(opts, "writeout");
3402 if (writeout) {
3403 #ifdef CONFIG_SYNC_FILE_RANGE
3404 qemu_opt_set(fsdev, "writeout", writeout);
3405 #else
3406 fprintf(stderr, "writeout=immediate not supported on "
3407 "this platform\n");
3408 exit(1);
3409 #endif
3411 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3412 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3413 qemu_opt_set(fsdev, "security_model",
3414 qemu_opt_get(opts, "security_model"));
3415 socket = qemu_opt_get(opts, "socket");
3416 if (socket) {
3417 qemu_opt_set(fsdev, "socket", socket);
3419 sock_fd = qemu_opt_get(opts, "sock_fd");
3420 if (sock_fd) {
3421 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3424 qemu_opt_set_bool(fsdev, "readonly",
3425 qemu_opt_get_bool(opts, "readonly", 0));
3426 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3427 &error_abort);
3428 qemu_opt_set(device, "driver", "virtio-9p-pci");
3429 qemu_opt_set(device, "fsdev",
3430 qemu_opt_get(opts, "mount_tag"));
3431 qemu_opt_set(device, "mount_tag",
3432 qemu_opt_get(opts, "mount_tag"));
3433 break;
3435 case QEMU_OPTION_virtfs_synth: {
3436 QemuOpts *fsdev;
3437 QemuOpts *device;
3439 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3440 1, NULL);
3441 if (!fsdev) {
3442 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3443 exit(1);
3445 qemu_opt_set(fsdev, "fsdriver", "synth");
3447 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3448 &error_abort);
3449 qemu_opt_set(device, "driver", "virtio-9p-pci");
3450 qemu_opt_set(device, "fsdev", "v_synth");
3451 qemu_opt_set(device, "mount_tag", "v_synth");
3452 break;
3454 case QEMU_OPTION_serial:
3455 add_device_config(DEV_SERIAL, optarg);
3456 default_serial = 0;
3457 if (strncmp(optarg, "mon:", 4) == 0) {
3458 default_monitor = 0;
3460 break;
3461 case QEMU_OPTION_watchdog:
3462 if (watchdog) {
3463 fprintf(stderr,
3464 "qemu: only one watchdog option may be given\n");
3465 return 1;
3467 watchdog = optarg;
3468 break;
3469 case QEMU_OPTION_watchdog_action:
3470 if (select_watchdog_action(optarg) == -1) {
3471 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3472 exit(1);
3474 break;
3475 case QEMU_OPTION_virtiocon:
3476 add_device_config(DEV_VIRTCON, optarg);
3477 default_virtcon = 0;
3478 if (strncmp(optarg, "mon:", 4) == 0) {
3479 default_monitor = 0;
3481 break;
3482 case QEMU_OPTION_parallel:
3483 add_device_config(DEV_PARALLEL, optarg);
3484 default_parallel = 0;
3485 if (strncmp(optarg, "mon:", 4) == 0) {
3486 default_monitor = 0;
3488 break;
3489 case QEMU_OPTION_debugcon:
3490 add_device_config(DEV_DEBUGCON, optarg);
3491 break;
3492 case QEMU_OPTION_loadvm:
3493 loadvm = optarg;
3494 break;
3495 case QEMU_OPTION_full_screen:
3496 full_screen = 1;
3497 break;
3498 case QEMU_OPTION_no_frame:
3499 no_frame = 1;
3500 break;
3501 case QEMU_OPTION_alt_grab:
3502 alt_grab = 1;
3503 break;
3504 case QEMU_OPTION_ctrl_grab:
3505 ctrl_grab = 1;
3506 break;
3507 case QEMU_OPTION_no_quit:
3508 no_quit = 1;
3509 break;
3510 case QEMU_OPTION_sdl:
3511 #ifdef CONFIG_SDL
3512 display_type = DT_SDL;
3513 break;
3514 #else
3515 fprintf(stderr, "SDL support is disabled\n");
3516 exit(1);
3517 #endif
3518 case QEMU_OPTION_pidfile:
3519 pid_file = optarg;
3520 break;
3521 case QEMU_OPTION_win2k_hack:
3522 win2k_install_hack = 1;
3523 break;
3524 case QEMU_OPTION_rtc_td_hack: {
3525 static GlobalProperty slew_lost_ticks[] = {
3527 .driver = "mc146818rtc",
3528 .property = "lost_tick_policy",
3529 .value = "slew",
3531 { /* end of list */ }
3534 qdev_prop_register_global_list(slew_lost_ticks);
3535 break;
3537 case QEMU_OPTION_acpitable:
3538 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3539 if (!opts) {
3540 exit(1);
3542 do_acpitable_option(opts);
3543 break;
3544 case QEMU_OPTION_smbios:
3545 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3546 if (!opts) {
3547 exit(1);
3549 do_smbios_option(opts);
3550 break;
3551 case QEMU_OPTION_enable_kvm:
3552 olist = qemu_find_opts("machine");
3553 qemu_opts_parse(olist, "accel=kvm", 0);
3554 break;
3555 case QEMU_OPTION_machine:
3556 olist = qemu_find_opts("machine");
3557 opts = qemu_opts_parse(olist, optarg, 1);
3558 if (!opts) {
3559 exit(1);
3561 optarg = qemu_opt_get(opts, "type");
3562 if (optarg) {
3563 machine_class = machine_parse(optarg);
3565 break;
3566 case QEMU_OPTION_no_kvm:
3567 olist = qemu_find_opts("machine");
3568 qemu_opts_parse(olist, "accel=tcg", 0);
3569 break;
3570 case QEMU_OPTION_no_kvm_pit: {
3571 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3572 "separately.\n");
3573 break;
3575 case QEMU_OPTION_no_kvm_pit_reinjection: {
3576 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3578 .driver = "kvm-pit",
3579 .property = "lost_tick_policy",
3580 .value = "discard",
3582 { /* end of list */ }
3585 fprintf(stderr, "Warning: option deprecated, use "
3586 "lost_tick_policy property of kvm-pit instead.\n");
3587 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3588 break;
3590 case QEMU_OPTION_usb:
3591 olist = qemu_find_opts("machine");
3592 qemu_opts_parse(olist, "usb=on", 0);
3593 break;
3594 case QEMU_OPTION_usbdevice:
3595 olist = qemu_find_opts("machine");
3596 qemu_opts_parse(olist, "usb=on", 0);
3597 add_device_config(DEV_USB, optarg);
3598 break;
3599 case QEMU_OPTION_device:
3600 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3601 exit(1);
3603 break;
3604 case QEMU_OPTION_smp:
3605 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3606 exit(1);
3608 break;
3609 case QEMU_OPTION_vnc:
3610 #ifdef CONFIG_VNC
3611 display_remote++;
3612 vnc_display = optarg;
3613 #else
3614 fprintf(stderr, "VNC support is disabled\n");
3615 exit(1);
3616 #endif
3617 break;
3618 case QEMU_OPTION_no_acpi:
3619 acpi_enabled = 0;
3620 break;
3621 case QEMU_OPTION_no_hpet:
3622 no_hpet = 1;
3623 break;
3624 case QEMU_OPTION_balloon:
3625 if (balloon_parse(optarg) < 0) {
3626 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3627 exit(1);
3629 break;
3630 case QEMU_OPTION_no_reboot:
3631 no_reboot = 1;
3632 break;
3633 case QEMU_OPTION_no_shutdown:
3634 no_shutdown = 1;
3635 break;
3636 case QEMU_OPTION_show_cursor:
3637 cursor_hide = 0;
3638 break;
3639 case QEMU_OPTION_uuid:
3640 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3641 fprintf(stderr, "Fail to parse UUID string."
3642 " Wrong format.\n");
3643 exit(1);
3645 qemu_uuid_set = true;
3646 break;
3647 case QEMU_OPTION_option_rom:
3648 if (nb_option_roms >= MAX_OPTION_ROMS) {
3649 fprintf(stderr, "Too many option ROMs\n");
3650 exit(1);
3652 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3653 if (!opts) {
3654 exit(1);
3656 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3657 option_rom[nb_option_roms].bootindex =
3658 qemu_opt_get_number(opts, "bootindex", -1);
3659 if (!option_rom[nb_option_roms].name) {
3660 fprintf(stderr, "Option ROM file is not specified\n");
3661 exit(1);
3663 nb_option_roms++;
3664 break;
3665 case QEMU_OPTION_semihosting:
3666 semihosting_enabled = 1;
3667 break;
3668 case QEMU_OPTION_tdf:
3669 fprintf(stderr, "Warning: user space PIT time drift fix "
3670 "is no longer supported.\n");
3671 break;
3672 case QEMU_OPTION_name:
3673 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3674 if (!opts) {
3675 exit(1);
3677 break;
3678 case QEMU_OPTION_prom_env:
3679 if (nb_prom_envs >= MAX_PROM_ENVS) {
3680 fprintf(stderr, "Too many prom variables\n");
3681 exit(1);
3683 prom_envs[nb_prom_envs] = optarg;
3684 nb_prom_envs++;
3685 break;
3686 case QEMU_OPTION_old_param:
3687 old_param = 1;
3688 break;
3689 case QEMU_OPTION_clock:
3690 /* Clock options no longer exist. Keep this option for
3691 * backward compatibility.
3693 break;
3694 case QEMU_OPTION_startdate:
3695 configure_rtc_date_offset(optarg, 1);
3696 break;
3697 case QEMU_OPTION_rtc:
3698 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3699 if (!opts) {
3700 exit(1);
3702 configure_rtc(opts);
3703 break;
3704 case QEMU_OPTION_tb_size:
3705 tcg_tb_size = strtol(optarg, NULL, 0);
3706 if (tcg_tb_size < 0) {
3707 tcg_tb_size = 0;
3709 break;
3710 case QEMU_OPTION_icount:
3711 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3712 optarg, 1);
3713 if (!icount_opts) {
3714 exit(1);
3716 break;
3717 case QEMU_OPTION_incoming:
3718 incoming = optarg;
3719 runstate_set(RUN_STATE_INMIGRATE);
3720 break;
3721 case QEMU_OPTION_nodefaults:
3722 has_defaults = 0;
3723 break;
3724 case QEMU_OPTION_xen_domid:
3725 if (!(xen_available())) {
3726 printf("Option %s not supported for this target\n", popt->name);
3727 exit(1);
3729 xen_domid = atoi(optarg);
3730 break;
3731 case QEMU_OPTION_xen_create:
3732 if (!(xen_available())) {
3733 printf("Option %s not supported for this target\n", popt->name);
3734 exit(1);
3736 xen_mode = XEN_CREATE;
3737 break;
3738 case QEMU_OPTION_xen_attach:
3739 if (!(xen_available())) {
3740 printf("Option %s not supported for this target\n", popt->name);
3741 exit(1);
3743 xen_mode = XEN_ATTACH;
3744 break;
3745 case QEMU_OPTION_trace:
3747 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3748 if (!opts) {
3749 exit(1);
3751 trace_events = qemu_opt_get(opts, "events");
3752 trace_file = qemu_opt_get(opts, "file");
3753 break;
3755 case QEMU_OPTION_trace_unassigned:
3756 trace_unassigned = true;
3757 break;
3758 case QEMU_OPTION_readconfig:
3760 int ret = qemu_read_config_file(optarg);
3761 if (ret < 0) {
3762 fprintf(stderr, "read config %s: %s\n", optarg,
3763 strerror(-ret));
3764 exit(1);
3766 break;
3768 case QEMU_OPTION_spice:
3769 olist = qemu_find_opts("spice");
3770 if (!olist) {
3771 fprintf(stderr, "spice is not supported by this qemu build.\n");
3772 exit(1);
3774 opts = qemu_opts_parse(olist, optarg, 0);
3775 if (!opts) {
3776 exit(1);
3778 display_remote++;
3779 break;
3780 case QEMU_OPTION_writeconfig:
3782 FILE *fp;
3783 if (strcmp(optarg, "-") == 0) {
3784 fp = stdout;
3785 } else {
3786 fp = fopen(optarg, "w");
3787 if (fp == NULL) {
3788 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3789 exit(1);
3792 qemu_config_write(fp);
3793 if (fp != stdout) {
3794 fclose(fp);
3796 break;
3798 case QEMU_OPTION_qtest:
3799 qtest_chrdev = optarg;
3800 break;
3801 case QEMU_OPTION_qtest_log:
3802 qtest_log = optarg;
3803 break;
3804 case QEMU_OPTION_sandbox:
3805 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3806 if (!opts) {
3807 exit(1);
3809 break;
3810 case QEMU_OPTION_add_fd:
3811 #ifndef _WIN32
3812 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3813 if (!opts) {
3814 exit(1);
3816 #else
3817 error_report("File descriptor passing is disabled on this "
3818 "platform");
3819 exit(1);
3820 #endif
3821 break;
3822 case QEMU_OPTION_object:
3823 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3824 if (!opts) {
3825 exit(1);
3827 break;
3828 case QEMU_OPTION_realtime:
3829 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3830 if (!opts) {
3831 exit(1);
3833 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3834 break;
3835 case QEMU_OPTION_msg:
3836 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3837 if (!opts) {
3838 exit(1);
3840 configure_msg(opts);
3841 break;
3842 case QEMU_OPTION_dump_vmstate:
3843 if (vmstate_dump_file) {
3844 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3845 "option may be given\n");
3846 exit(1);
3848 vmstate_dump_file = fopen(optarg, "w");
3849 if (vmstate_dump_file == NULL) {
3850 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3851 exit(1);
3853 break;
3854 default:
3855 os_parse_cmd_args(popt->index, optarg);
3859 loc_set_none();
3861 os_daemonize();
3863 if (qemu_init_main_loop(&main_loop_err)) {
3864 error_report("%s", error_get_pretty(main_loop_err));
3865 exit(1);
3868 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3869 exit(1);
3872 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3873 exit(1);
3876 #ifndef _WIN32
3877 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3878 exit(1);
3881 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3882 exit(1);
3884 #endif
3886 if (machine_class == NULL) {
3887 fprintf(stderr, "No machine specified, and there is no default.\n"
3888 "Use -machine help to list supported machines!\n");
3889 exit(1);
3892 current_machine = MACHINE(object_new(object_class_get_name(
3893 OBJECT_CLASS(machine_class))));
3894 object_property_add_child(object_get_root(), "machine",
3895 OBJECT(current_machine), &error_abort);
3896 cpu_exec_init_all();
3898 if (machine_class->hw_version) {
3899 qemu_set_version(machine_class->hw_version);
3902 /* Init CPU def lists, based on config
3903 * - Must be called after all the qemu_read_config_file() calls
3904 * - Must be called before list_cpus()
3905 * - Must be called before machine->init()
3907 cpudef_init();
3909 if (cpu_model && is_help_option(cpu_model)) {
3910 list_cpus(stdout, &fprintf, cpu_model);
3911 exit(0);
3914 /* Open the logfile at this point, if necessary. We can't open the logfile
3915 * when encountering either of the logging options (-d or -D) because the
3916 * other one may be encountered later on the command line, changing the
3917 * location or level of logging.
3919 if (log_mask) {
3920 int mask;
3921 if (log_file) {
3922 qemu_set_log_filename(log_file);
3925 mask = qemu_str_to_log_mask(log_mask);
3926 if (!mask) {
3927 qemu_print_log_usage(stdout);
3928 exit(1);
3930 qemu_set_log(mask);
3933 if (!is_daemonized()) {
3934 if (!trace_init_backends(trace_events, trace_file)) {
3935 exit(1);
3939 /* If no data_dir is specified then try to find it relative to the
3940 executable path. */
3941 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3942 data_dir[data_dir_idx] = os_find_datadir();
3943 if (data_dir[data_dir_idx] != NULL) {
3944 data_dir_idx++;
3947 /* If all else fails use the install path specified when building. */
3948 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3949 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3952 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3954 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3955 if (smp_cpus > machine_class->max_cpus) {
3956 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3957 "supported by machine `%s' (%d)\n", smp_cpus,
3958 machine_class->name, machine_class->max_cpus);
3959 exit(1);
3963 * Get the default machine options from the machine if it is not already
3964 * specified either by the configuration file or by the command line.
3966 if (machine_class->default_machine_opts) {
3967 qemu_opts_set_defaults(qemu_find_opts("machine"),
3968 machine_class->default_machine_opts, 0);
3971 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3972 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3974 if (!vga_model && !default_vga) {
3975 vga_interface_type = VGA_DEVICE;
3977 if (!has_defaults || machine_class->no_serial) {
3978 default_serial = 0;
3980 if (!has_defaults || machine_class->no_parallel) {
3981 default_parallel = 0;
3983 if (!has_defaults || !machine_class->use_virtcon) {
3984 default_virtcon = 0;
3986 if (!has_defaults || !machine_class->use_sclp) {
3987 default_sclp = 0;
3989 if (!has_defaults || machine_class->no_floppy) {
3990 default_floppy = 0;
3992 if (!has_defaults || machine_class->no_cdrom) {
3993 default_cdrom = 0;
3995 if (!has_defaults || machine_class->no_sdcard) {
3996 default_sdcard = 0;
3998 if (!has_defaults) {
3999 default_monitor = 0;
4000 default_net = 0;
4001 default_vga = 0;
4004 if (is_daemonized()) {
4005 /* According to documentation and historically, -nographic redirects
4006 * serial port, parallel port and monitor to stdio, which does not work
4007 * with -daemonize. We can redirect these to null instead, but since
4008 * -nographic is legacy, let's just error out.
4009 * We disallow -nographic only if all other ports are not redirected
4010 * explicitly, to not break existing legacy setups which uses
4011 * -nographic _and_ redirects all ports explicitly - this is valid
4012 * usage, -nographic is just a no-op in this case.
4014 if (display_type == DT_NOGRAPHIC
4015 && (default_parallel || default_serial
4016 || default_monitor || default_virtcon)) {
4017 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4018 exit(1);
4020 #ifdef CONFIG_CURSES
4021 if (display_type == DT_CURSES) {
4022 fprintf(stderr, "curses display can not be used with -daemonize\n");
4023 exit(1);
4025 #endif
4028 if (display_type == DT_NOGRAPHIC) {
4029 if (default_parallel)
4030 add_device_config(DEV_PARALLEL, "null");
4031 if (default_serial && default_monitor) {
4032 add_device_config(DEV_SERIAL, "mon:stdio");
4033 } else if (default_virtcon && default_monitor) {
4034 add_device_config(DEV_VIRTCON, "mon:stdio");
4035 } else if (default_sclp && default_monitor) {
4036 add_device_config(DEV_SCLP, "mon:stdio");
4037 } else {
4038 if (default_serial)
4039 add_device_config(DEV_SERIAL, "stdio");
4040 if (default_virtcon)
4041 add_device_config(DEV_VIRTCON, "stdio");
4042 if (default_sclp) {
4043 add_device_config(DEV_SCLP, "stdio");
4045 if (default_monitor)
4046 monitor_parse("stdio", "readline");
4048 } else {
4049 if (default_serial)
4050 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4051 if (default_parallel)
4052 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4053 if (default_monitor)
4054 monitor_parse("vc:80Cx24C", "readline");
4055 if (default_virtcon)
4056 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4057 if (default_sclp) {
4058 add_device_config(DEV_SCLP, "vc:80Cx24C");
4062 if (display_type == DT_DEFAULT && !display_remote) {
4063 #if defined(CONFIG_GTK)
4064 display_type = DT_GTK;
4065 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4066 display_type = DT_SDL;
4067 #elif defined(CONFIG_VNC)
4068 vnc_display = "localhost:0,to=99";
4069 show_vnc_port = 1;
4070 #else
4071 display_type = DT_NONE;
4072 #endif
4075 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4076 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4077 "for SDL, ignoring option\n");
4079 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4080 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4081 "ignoring option\n");
4084 #if defined(CONFIG_GTK)
4085 if (display_type == DT_GTK) {
4086 early_gtk_display_init();
4088 #endif
4090 socket_init();
4092 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4093 exit(1);
4094 #ifdef CONFIG_VIRTFS
4095 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4096 exit(1);
4098 #endif
4100 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4101 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
4102 exit(1);
4105 /* store value for the future use */
4106 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4108 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4109 != 0) {
4110 exit(0);
4113 if (qemu_opts_foreach(qemu_find_opts("object"),
4114 object_create, NULL, 0) != 0) {
4115 exit(1);
4118 machine_opts = qemu_get_machine_opts();
4119 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4120 1) < 0) {
4121 object_unref(OBJECT(current_machine));
4122 exit(1);
4125 configure_accelerator(current_machine);
4127 if (qtest_chrdev) {
4128 Error *local_err = NULL;
4129 qtest_init(qtest_chrdev, qtest_log, &local_err);
4130 if (local_err) {
4131 error_report("%s", error_get_pretty(local_err));
4132 error_free(local_err);
4133 exit(1);
4137 machine_opts = qemu_get_machine_opts();
4138 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4139 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4140 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4141 bios_name = qemu_opt_get(machine_opts, "firmware");
4143 boot_order = machine_class->default_boot_order;
4144 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4145 if (opts) {
4146 char *normal_boot_order;
4147 const char *order, *once;
4149 order = qemu_opt_get(opts, "order");
4150 if (order) {
4151 validate_bootdevices(order);
4152 boot_order = order;
4155 once = qemu_opt_get(opts, "once");
4156 if (once) {
4157 validate_bootdevices(once);
4158 normal_boot_order = g_strdup(boot_order);
4159 boot_order = once;
4160 qemu_register_reset(restore_boot_order, normal_boot_order);
4163 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4164 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4167 if (!kernel_cmdline) {
4168 kernel_cmdline = "";
4169 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4172 linux_boot = (kernel_filename != NULL);
4174 if (!linux_boot && *kernel_cmdline != '\0') {
4175 fprintf(stderr, "-append only allowed with -kernel option\n");
4176 exit(1);
4179 if (!linux_boot && initrd_filename != NULL) {
4180 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4181 exit(1);
4184 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4185 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4186 exit(1);
4189 os_set_line_buffering();
4191 qemu_init_cpu_loop();
4192 qemu_mutex_lock_iothread();
4194 #ifdef CONFIG_SPICE
4195 /* spice needs the timers to be initialized by this point */
4196 qemu_spice_init();
4197 #endif
4199 cpu_ticks_init();
4200 if (icount_opts) {
4201 if (kvm_enabled() || xen_enabled()) {
4202 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4203 exit(1);
4205 configure_icount(icount_opts, &error_abort);
4206 qemu_opts_del(icount_opts);
4209 /* clean up network at qemu process termination */
4210 atexit(&net_cleanup);
4212 if (net_init_clients() < 0) {
4213 exit(1);
4216 #ifdef CONFIG_TPM
4217 if (tpm_init() < 0) {
4218 exit(1);
4220 #endif
4222 /* init the bluetooth world */
4223 if (foreach_device_config(DEV_BT, bt_parse))
4224 exit(1);
4226 if (!xen_enabled()) {
4227 /* On 32-bit hosts, QEMU is limited by virtual address space */
4228 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4229 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4230 exit(1);
4234 blk_mig_init();
4235 ram_mig_init();
4237 /* If the currently selected machine wishes to override the units-per-bus
4238 * property of its default HBA interface type, do so now. */
4239 if (machine_class->units_per_default_bus) {
4240 override_max_devs(machine_class->block_default_type,
4241 machine_class->units_per_default_bus);
4244 /* open the virtual block devices */
4245 if (snapshot)
4246 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4247 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4248 &machine_class->block_default_type, 1) != 0) {
4249 exit(1);
4252 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4253 CDROM_OPTS);
4254 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4255 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4257 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4258 NULL, 1) != 0) {
4259 exit(1);
4262 set_numa_nodes();
4264 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4265 exit(1);
4268 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4269 exit(1);
4270 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4271 exit(1);
4272 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4273 exit(1);
4274 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4275 exit(1);
4277 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4278 exit(1);
4280 /* If no default VGA is requested, the default is "none". */
4281 if (default_vga) {
4282 if (machine_class->default_display) {
4283 vga_model = machine_class->default_display;
4284 } else if (cirrus_vga_available()) {
4285 vga_model = "cirrus";
4286 } else if (vga_available()) {
4287 vga_model = "std";
4290 if (vga_model) {
4291 select_vgahw(vga_model);
4294 if (watchdog) {
4295 i = select_watchdog(watchdog);
4296 if (i > 0)
4297 exit (i == 1 ? 1 : 0);
4300 if (machine_class->compat_props) {
4301 qdev_prop_register_global_list(machine_class->compat_props);
4303 qemu_add_globals();
4305 qdev_machine_init();
4307 current_machine->ram_size = ram_size;
4308 current_machine->maxram_size = maxram_size;
4309 current_machine->ram_slots = ram_slots;
4310 current_machine->boot_order = boot_order;
4311 current_machine->cpu_model = cpu_model;
4313 machine_class->init(current_machine);
4315 realtime_init();
4317 audio_init();
4319 cpu_synchronize_all_post_init();
4321 set_numa_modes();
4323 /* init USB devices */
4324 if (usb_enabled(false)) {
4325 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4326 exit(1);
4329 /* init generic devices */
4330 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4331 exit(1);
4333 /* Did we create any drives that we failed to create a device for? */
4334 drive_check_orphaned();
4336 net_check_clients();
4338 ds = init_displaystate();
4340 /* init local displays */
4341 switch (display_type) {
4342 case DT_NOGRAPHIC:
4343 (void)ds; /* avoid warning if no display is configured */
4344 break;
4345 #if defined(CONFIG_CURSES)
4346 case DT_CURSES:
4347 curses_display_init(ds, full_screen);
4348 break;
4349 #endif
4350 #if defined(CONFIG_SDL)
4351 case DT_SDL:
4352 sdl_display_init(ds, full_screen, no_frame);
4353 break;
4354 #elif defined(CONFIG_COCOA)
4355 case DT_SDL:
4356 cocoa_display_init(ds, full_screen);
4357 break;
4358 #endif
4359 #if defined(CONFIG_GTK)
4360 case DT_GTK:
4361 gtk_display_init(ds, full_screen, grab_on_hover);
4362 break;
4363 #endif
4364 default:
4365 break;
4368 /* must be after terminal init, SDL library changes signal handlers */
4369 os_setup_signal_handling();
4371 #ifdef CONFIG_VNC
4372 /* init remote displays */
4373 if (vnc_display) {
4374 Error *local_err = NULL;
4375 vnc_display_init(ds);
4376 vnc_display_open(ds, vnc_display, &local_err);
4377 if (local_err != NULL) {
4378 error_report("Failed to start VNC server on `%s': %s",
4379 vnc_display, error_get_pretty(local_err));
4380 error_free(local_err);
4381 exit(1);
4384 if (show_vnc_port) {
4385 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4388 #endif
4389 #ifdef CONFIG_SPICE
4390 if (using_spice) {
4391 qemu_spice_display_init();
4393 #endif
4395 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4396 exit(1);
4399 qdev_machine_creation_done();
4401 if (rom_load_all() != 0) {
4402 fprintf(stderr, "rom loading failed\n");
4403 exit(1);
4406 /* TODO: once all bus devices are qdevified, this should be done
4407 * when bus is created by qdev.c */
4408 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4409 qemu_run_machine_init_done_notifiers();
4411 /* Done notifiers can load ROMs */
4412 rom_load_done();
4414 qemu_system_reset(VMRESET_SILENT);
4415 if (loadvm) {
4416 if (load_vmstate(loadvm) < 0) {
4417 autostart = 0;
4421 qdev_prop_check_globals();
4422 if (vmstate_dump_file) {
4423 /* dump and exit */
4424 dump_vmstate_json_to_file(vmstate_dump_file);
4425 return 0;
4428 if (incoming) {
4429 Error *local_err = NULL;
4430 qemu_start_incoming_migration(incoming, &local_err);
4431 if (local_err) {
4432 error_report("-incoming %s: %s", incoming,
4433 error_get_pretty(local_err));
4434 error_free(local_err);
4435 exit(1);
4437 } else if (autostart) {
4438 vm_start();
4441 os_setup_post();
4443 if (is_daemonized()) {
4444 if (!trace_init_backends(trace_events, trace_file)) {
4445 exit(1);
4449 main_loop();
4450 bdrv_close_all();
4451 pause_all_vcpus();
4452 res_free();
4453 #ifdef CONFIG_TPM
4454 tpm_cleanup();
4455 #endif
4457 return 0;