makefile: fix w32 install target for qemu-ga
[qemu/ar7.git] / vl.c
blob18086b890bda47e2a6da48020361908ba4705cf5
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/error-report.h"
62 #include "qemu/sockets.h"
63 #include "hw/hw.h"
64 #include "hw/boards.h"
65 #include "sysemu/accel.h"
66 #include "hw/usb.h"
67 #include "hw/i386/pc.h"
68 #include "hw/isa/isa.h"
69 #include "hw/bt.h"
70 #include "sysemu/watchdog.h"
71 #include "hw/smbios/smbios.h"
72 #include "hw/xen/xen.h"
73 #include "hw/qdev.h"
74 #include "hw/loader.h"
75 #include "monitor/qdev.h"
76 #include "sysemu/bt.h"
77 #include "net/net.h"
78 #include "net/slirp.h"
79 #include "monitor/monitor.h"
80 #include "ui/console.h"
81 #include "sysemu/sysemu.h"
82 #include "sysemu/numa.h"
83 #include "exec/gdbstub.h"
84 #include "qemu/timer.h"
85 #include "sysemu/char.h"
86 #include "qemu/bitmap.h"
87 #include "sysemu/blockdev.h"
88 #include "hw/block/block.h"
89 #include "migration/block.h"
90 #include "sysemu/tpm.h"
91 #include "sysemu/dma.h"
92 #include "audio/audio.h"
93 #include "migration/migration.h"
94 #include "sysemu/kvm.h"
95 #include "qapi/qmp/qjson.h"
96 #include "qemu/option.h"
97 #include "qemu/config-file.h"
98 #include "qemu-options.h"
99 #include "qmp-commands.h"
100 #include "qemu/main-loop.h"
101 #ifdef CONFIG_VIRTFS
102 #include "fsdev/qemu-fsdev.h"
103 #endif
104 #include "sysemu/qtest.h"
106 #include "disas/disas.h"
109 #include "slirp/libslirp.h"
111 #include "trace.h"
112 #include "trace/control.h"
113 #include "qemu/queue.h"
114 #include "sysemu/cpus.h"
115 #include "sysemu/arch_init.h"
116 #include "qemu/osdep.h"
118 #include "ui/qemu-spice.h"
119 #include "qapi/string-input-visitor.h"
120 #include "qapi/opts-visitor.h"
121 #include "qom/object_interfaces.h"
122 #include "qapi-event.h"
123 #include "exec/semihost.h"
124 #include "crypto/init.h"
125 #include "sysemu/replay.h"
126 #include "qapi/qmp/qerror.h"
128 #define MAX_VIRTIO_CONSOLES 1
129 #define MAX_SCLP_CONSOLES 1
131 static const char *data_dir[16];
132 static int data_dir_idx;
133 const char *bios_name = NULL;
134 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
135 DisplayType display_type = DT_DEFAULT;
136 int request_opengl = -1;
137 int display_opengl;
138 static int display_remote;
139 const char* keyboard_layout = NULL;
140 ram_addr_t ram_size;
141 const char *mem_path = NULL;
142 int mem_prealloc = 0; /* force preallocation of physical target memory */
143 bool enable_mlock = false;
144 int nb_nics;
145 NICInfo nd_table[MAX_NICS];
146 int autostart;
147 static int rtc_utc = 1;
148 static int rtc_date_offset = -1; /* -1 means no change */
149 QEMUClockType rtc_clock;
150 int vga_interface_type = VGA_NONE;
151 static int full_screen = 0;
152 static int no_frame = 0;
153 int no_quit = 0;
154 #ifdef CONFIG_GTK
155 static bool grab_on_hover;
156 #endif
157 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
158 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
159 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
160 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
161 int win2k_install_hack = 0;
162 int singlestep = 0;
163 int smp_cpus = 1;
164 int max_cpus = 0;
165 int smp_cores = 1;
166 int smp_threads = 1;
167 int acpi_enabled = 1;
168 int no_hpet = 0;
169 int fd_bootchk = 1;
170 static int no_reboot;
171 int no_shutdown = 0;
172 int cursor_hide = 1;
173 int graphic_rotate = 0;
174 const char *watchdog;
175 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
176 int nb_option_roms;
177 int old_param = 0;
178 const char *qemu_name;
179 int alt_grab = 0;
180 int ctrl_grab = 0;
181 unsigned int nb_prom_envs = 0;
182 const char *prom_envs[MAX_PROM_ENVS];
183 int boot_menu;
184 bool boot_strict;
185 uint8_t *boot_splash_filedata;
186 size_t boot_splash_filedata_size;
187 uint8_t qemu_extra_params_fw[2];
189 int icount_align_option;
191 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
192 * little-endian "wire format" described in the SMBIOS 2.6 specification.
194 uint8_t qemu_uuid[16];
195 bool qemu_uuid_set;
197 /* Trace unassigned memory or i/o accesses. */
198 bool trace_unassigned;
200 static NotifierList exit_notifiers =
201 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
203 static NotifierList machine_init_done_notifiers =
204 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
206 bool xen_allowed;
207 uint32_t xen_domid;
208 enum xen_mode xen_mode = XEN_EMULATE;
210 static int has_defaults = 1;
211 static int default_serial = 1;
212 static int default_parallel = 1;
213 static int default_virtcon = 1;
214 static int default_sclp = 1;
215 static int default_monitor = 1;
216 static int default_floppy = 1;
217 static int default_cdrom = 1;
218 static int default_sdcard = 1;
219 static int default_vga = 1;
221 static struct {
222 const char *driver;
223 int *flag;
224 } default_list[] = {
225 { .driver = "isa-serial", .flag = &default_serial },
226 { .driver = "isa-parallel", .flag = &default_parallel },
227 { .driver = "isa-fdc", .flag = &default_floppy },
228 { .driver = "ide-cd", .flag = &default_cdrom },
229 { .driver = "ide-hd", .flag = &default_cdrom },
230 { .driver = "ide-drive", .flag = &default_cdrom },
231 { .driver = "scsi-cd", .flag = &default_cdrom },
232 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
233 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
234 { .driver = "virtio-serial", .flag = &default_virtcon },
235 { .driver = "VGA", .flag = &default_vga },
236 { .driver = "isa-vga", .flag = &default_vga },
237 { .driver = "cirrus-vga", .flag = &default_vga },
238 { .driver = "isa-cirrus-vga", .flag = &default_vga },
239 { .driver = "vmware-svga", .flag = &default_vga },
240 { .driver = "qxl-vga", .flag = &default_vga },
241 { .driver = "virtio-vga", .flag = &default_vga },
244 static QemuOptsList qemu_rtc_opts = {
245 .name = "rtc",
246 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
247 .desc = {
249 .name = "base",
250 .type = QEMU_OPT_STRING,
252 .name = "clock",
253 .type = QEMU_OPT_STRING,
255 .name = "driftfix",
256 .type = QEMU_OPT_STRING,
258 { /* end of list */ }
262 static QemuOptsList qemu_sandbox_opts = {
263 .name = "sandbox",
264 .implied_opt_name = "enable",
265 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
266 .desc = {
268 .name = "enable",
269 .type = QEMU_OPT_BOOL,
271 { /* end of list */ }
275 static QemuOptsList qemu_trace_opts = {
276 .name = "trace",
277 .implied_opt_name = "trace",
278 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
279 .desc = {
281 .name = "events",
282 .type = QEMU_OPT_STRING,
284 .name = "file",
285 .type = QEMU_OPT_STRING,
287 { /* end of list */ }
291 static QemuOptsList qemu_option_rom_opts = {
292 .name = "option-rom",
293 .implied_opt_name = "romfile",
294 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
295 .desc = {
297 .name = "bootindex",
298 .type = QEMU_OPT_NUMBER,
299 }, {
300 .name = "romfile",
301 .type = QEMU_OPT_STRING,
303 { /* end of list */ }
307 static QemuOptsList qemu_machine_opts = {
308 .name = "machine",
309 .implied_opt_name = "type",
310 .merge_lists = true,
311 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
312 .desc = {
314 * no elements => accept any
315 * sanity checking will happen later
316 * when setting machine properties
322 static QemuOptsList qemu_boot_opts = {
323 .name = "boot-opts",
324 .implied_opt_name = "order",
325 .merge_lists = true,
326 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
327 .desc = {
329 .name = "order",
330 .type = QEMU_OPT_STRING,
331 }, {
332 .name = "once",
333 .type = QEMU_OPT_STRING,
334 }, {
335 .name = "menu",
336 .type = QEMU_OPT_BOOL,
337 }, {
338 .name = "splash",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "splash-time",
342 .type = QEMU_OPT_STRING,
343 }, {
344 .name = "reboot-timeout",
345 .type = QEMU_OPT_STRING,
346 }, {
347 .name = "strict",
348 .type = QEMU_OPT_BOOL,
350 { /*End of list */ }
354 static QemuOptsList qemu_add_fd_opts = {
355 .name = "add-fd",
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
357 .desc = {
359 .name = "fd",
360 .type = QEMU_OPT_NUMBER,
361 .help = "file descriptor of which a duplicate is added to fd set",
363 .name = "set",
364 .type = QEMU_OPT_NUMBER,
365 .help = "ID of the fd set to add fd to",
367 .name = "opaque",
368 .type = QEMU_OPT_STRING,
369 .help = "free-form string used to describe fd",
371 { /* end of list */ }
375 static QemuOptsList qemu_object_opts = {
376 .name = "object",
377 .implied_opt_name = "qom-type",
378 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
379 .desc = {
384 static QemuOptsList qemu_tpmdev_opts = {
385 .name = "tpmdev",
386 .implied_opt_name = "type",
387 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
388 .desc = {
389 /* options are defined in the TPM backends */
390 { /* end of list */ }
394 static QemuOptsList qemu_realtime_opts = {
395 .name = "realtime",
396 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
397 .desc = {
399 .name = "mlock",
400 .type = QEMU_OPT_BOOL,
402 { /* end of list */ }
406 static QemuOptsList qemu_msg_opts = {
407 .name = "msg",
408 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
409 .desc = {
411 .name = "timestamp",
412 .type = QEMU_OPT_BOOL,
414 { /* end of list */ }
418 static QemuOptsList qemu_name_opts = {
419 .name = "name",
420 .implied_opt_name = "guest",
421 .merge_lists = true,
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
423 .desc = {
425 .name = "guest",
426 .type = QEMU_OPT_STRING,
427 .help = "Sets the name of the guest.\n"
428 "This name will be displayed in the SDL window caption.\n"
429 "The name will also be used for the VNC server",
430 }, {
431 .name = "process",
432 .type = QEMU_OPT_STRING,
433 .help = "Sets the name of the QEMU process, as shown in top etc",
434 }, {
435 .name = "debug-threads",
436 .type = QEMU_OPT_BOOL,
437 .help = "When enabled, name the individual threads; defaults off.\n"
438 "NOTE: The thread names are for debugging and not a\n"
439 "stable API.",
441 { /* End of list */ }
445 static QemuOptsList qemu_mem_opts = {
446 .name = "memory",
447 .implied_opt_name = "size",
448 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
449 .merge_lists = true,
450 .desc = {
452 .name = "size",
453 .type = QEMU_OPT_SIZE,
456 .name = "slots",
457 .type = QEMU_OPT_NUMBER,
460 .name = "maxmem",
461 .type = QEMU_OPT_SIZE,
463 { /* end of list */ }
467 static QemuOptsList qemu_icount_opts = {
468 .name = "icount",
469 .implied_opt_name = "shift",
470 .merge_lists = true,
471 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
472 .desc = {
474 .name = "shift",
475 .type = QEMU_OPT_STRING,
476 }, {
477 .name = "align",
478 .type = QEMU_OPT_BOOL,
479 }, {
480 .name = "sleep",
481 .type = QEMU_OPT_BOOL,
482 }, {
483 .name = "rr",
484 .type = QEMU_OPT_STRING,
485 }, {
486 .name = "rrfile",
487 .type = QEMU_OPT_STRING,
489 { /* end of list */ }
493 static QemuOptsList qemu_semihosting_config_opts = {
494 .name = "semihosting-config",
495 .implied_opt_name = "enable",
496 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
497 .desc = {
499 .name = "enable",
500 .type = QEMU_OPT_BOOL,
501 }, {
502 .name = "target",
503 .type = QEMU_OPT_STRING,
504 }, {
505 .name = "arg",
506 .type = QEMU_OPT_STRING,
508 { /* end of list */ }
512 static QemuOptsList qemu_fw_cfg_opts = {
513 .name = "fw_cfg",
514 .implied_opt_name = "name",
515 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
516 .desc = {
518 .name = "name",
519 .type = QEMU_OPT_STRING,
520 .help = "Sets the fw_cfg name of the blob to be inserted",
521 }, {
522 .name = "file",
523 .type = QEMU_OPT_STRING,
524 .help = "Sets the name of the file from which\n"
525 "the fw_cfg blob will be loaded",
526 }, {
527 .name = "string",
528 .type = QEMU_OPT_STRING,
529 .help = "Sets content of the blob to be inserted from a string",
531 { /* end of list */ }
536 * Get machine options
538 * Returns: machine options (never null).
540 QemuOpts *qemu_get_machine_opts(void)
542 return qemu_find_opts_singleton("machine");
545 const char *qemu_get_vm_name(void)
547 return qemu_name;
550 static void res_free(void)
552 g_free(boot_splash_filedata);
553 boot_splash_filedata = NULL;
556 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
558 const char *driver = qemu_opt_get(opts, "driver");
559 int i;
561 if (!driver)
562 return 0;
563 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
564 if (strcmp(default_list[i].driver, driver) != 0)
565 continue;
566 *(default_list[i].flag) = 0;
568 return 0;
571 /***********************************************************/
572 /* QEMU state */
574 static RunState current_run_state = RUN_STATE_PRELAUNCH;
576 /* We use RUN_STATE_MAX but any invalid value will do */
577 static RunState vmstop_requested = RUN_STATE_MAX;
578 static QemuMutex vmstop_lock;
580 typedef struct {
581 RunState from;
582 RunState to;
583 } RunStateTransition;
585 static const RunStateTransition runstate_transitions_def[] = {
586 /* from -> to */
587 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
588 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
591 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
592 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
593 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
594 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
595 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
596 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
597 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
598 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
601 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
603 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
604 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
606 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
607 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
610 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
613 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
614 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
616 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
617 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
619 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
621 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
622 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
623 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
624 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
625 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
626 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
627 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
628 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
629 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
630 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
632 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
634 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
635 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
637 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
638 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
639 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
640 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
642 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
643 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
646 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_MAX, RUN_STATE_MAX },
651 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
653 bool runstate_check(RunState state)
655 return current_run_state == state;
658 bool runstate_store(char *str, size_t size)
660 const char *state = RunState_lookup[current_run_state];
661 size_t len = strlen(state) + 1;
663 if (len > size) {
664 return false;
666 memcpy(str, state, len);
667 return true;
670 static void runstate_init(void)
672 const RunStateTransition *p;
674 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
675 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
676 runstate_valid_transitions[p->from][p->to] = true;
679 qemu_mutex_init(&vmstop_lock);
682 /* This function will abort() on invalid state transitions */
683 void runstate_set(RunState new_state)
685 assert(new_state < RUN_STATE_MAX);
687 if (!runstate_valid_transitions[current_run_state][new_state]) {
688 error_report("invalid runstate transition: '%s' -> '%s'",
689 RunState_lookup[current_run_state],
690 RunState_lookup[new_state]);
691 abort();
693 trace_runstate_set(new_state);
694 current_run_state = new_state;
697 int runstate_is_running(void)
699 return runstate_check(RUN_STATE_RUNNING);
702 bool runstate_needs_reset(void)
704 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
705 runstate_check(RUN_STATE_SHUTDOWN);
708 StatusInfo *qmp_query_status(Error **errp)
710 StatusInfo *info = g_malloc0(sizeof(*info));
712 info->running = runstate_is_running();
713 info->singlestep = singlestep;
714 info->status = current_run_state;
716 return info;
719 static bool qemu_vmstop_requested(RunState *r)
721 qemu_mutex_lock(&vmstop_lock);
722 *r = vmstop_requested;
723 vmstop_requested = RUN_STATE_MAX;
724 qemu_mutex_unlock(&vmstop_lock);
725 return *r < RUN_STATE_MAX;
728 void qemu_system_vmstop_request_prepare(void)
730 qemu_mutex_lock(&vmstop_lock);
733 void qemu_system_vmstop_request(RunState state)
735 vmstop_requested = state;
736 qemu_mutex_unlock(&vmstop_lock);
737 qemu_notify_event();
740 void vm_start(void)
742 RunState requested;
744 qemu_vmstop_requested(&requested);
745 if (runstate_is_running() && requested == RUN_STATE_MAX) {
746 return;
749 /* Ensure that a STOP/RESUME pair of events is emitted if a
750 * vmstop request was pending. The BLOCK_IO_ERROR event, for
751 * example, according to documentation is always followed by
752 * the STOP event.
754 if (runstate_is_running()) {
755 qapi_event_send_stop(&error_abort);
756 } else {
757 cpu_enable_ticks();
758 runstate_set(RUN_STATE_RUNNING);
759 vm_state_notify(1, RUN_STATE_RUNNING);
760 resume_all_vcpus();
763 qapi_event_send_resume(&error_abort);
767 /***********************************************************/
768 /* real time host monotonic timer */
770 static time_t qemu_time(void)
772 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
775 /***********************************************************/
776 /* host time/date access */
777 void qemu_get_timedate(struct tm *tm, int offset)
779 time_t ti = qemu_time();
781 ti += offset;
782 if (rtc_date_offset == -1) {
783 if (rtc_utc)
784 gmtime_r(&ti, tm);
785 else
786 localtime_r(&ti, tm);
787 } else {
788 ti -= rtc_date_offset;
789 gmtime_r(&ti, tm);
793 int qemu_timedate_diff(struct tm *tm)
795 time_t seconds;
797 if (rtc_date_offset == -1)
798 if (rtc_utc)
799 seconds = mktimegm(tm);
800 else {
801 struct tm tmp = *tm;
802 tmp.tm_isdst = -1; /* use timezone to figure it out */
803 seconds = mktime(&tmp);
805 else
806 seconds = mktimegm(tm) + rtc_date_offset;
808 return seconds - qemu_time();
811 static void configure_rtc_date_offset(const char *startdate, int legacy)
813 time_t rtc_start_date;
814 struct tm tm;
816 if (!strcmp(startdate, "now") && legacy) {
817 rtc_date_offset = -1;
818 } else {
819 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
820 &tm.tm_year,
821 &tm.tm_mon,
822 &tm.tm_mday,
823 &tm.tm_hour,
824 &tm.tm_min,
825 &tm.tm_sec) == 6) {
826 /* OK */
827 } else if (sscanf(startdate, "%d-%d-%d",
828 &tm.tm_year,
829 &tm.tm_mon,
830 &tm.tm_mday) == 3) {
831 tm.tm_hour = 0;
832 tm.tm_min = 0;
833 tm.tm_sec = 0;
834 } else {
835 goto date_fail;
837 tm.tm_year -= 1900;
838 tm.tm_mon--;
839 rtc_start_date = mktimegm(&tm);
840 if (rtc_start_date == -1) {
841 date_fail:
842 error_report("invalid date format");
843 error_printf("valid formats: "
844 "'2006-06-17T16:01:21' or '2006-06-17'\n");
845 exit(1);
847 rtc_date_offset = qemu_time() - rtc_start_date;
851 static void configure_rtc(QemuOpts *opts)
853 const char *value;
855 value = qemu_opt_get(opts, "base");
856 if (value) {
857 if (!strcmp(value, "utc")) {
858 rtc_utc = 1;
859 } else if (!strcmp(value, "localtime")) {
860 Error *blocker = NULL;
861 rtc_utc = 0;
862 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
863 "-rtc base=localtime");
864 replay_add_blocker(blocker);
865 } else {
866 configure_rtc_date_offset(value, 0);
869 value = qemu_opt_get(opts, "clock");
870 if (value) {
871 if (!strcmp(value, "host")) {
872 rtc_clock = QEMU_CLOCK_HOST;
873 } else if (!strcmp(value, "rt")) {
874 rtc_clock = QEMU_CLOCK_REALTIME;
875 } else if (!strcmp(value, "vm")) {
876 rtc_clock = QEMU_CLOCK_VIRTUAL;
877 } else {
878 error_report("invalid option value '%s'", value);
879 exit(1);
882 value = qemu_opt_get(opts, "driftfix");
883 if (value) {
884 if (!strcmp(value, "slew")) {
885 static GlobalProperty slew_lost_ticks[] = {
887 .driver = "mc146818rtc",
888 .property = "lost_tick_policy",
889 .value = "slew",
891 { /* end of list */ }
894 qdev_prop_register_global_list(slew_lost_ticks);
895 } else if (!strcmp(value, "none")) {
896 /* discard is default */
897 } else {
898 error_report("invalid option value '%s'", value);
899 exit(1);
904 /***********************************************************/
905 /* Bluetooth support */
906 static int nb_hcis;
907 static int cur_hci;
908 static struct HCIInfo *hci_table[MAX_NICS];
910 struct HCIInfo *qemu_next_hci(void)
912 if (cur_hci == nb_hcis)
913 return &null_hci;
915 return hci_table[cur_hci++];
918 static int bt_hci_parse(const char *str)
920 struct HCIInfo *hci;
921 bdaddr_t bdaddr;
923 if (nb_hcis >= MAX_NICS) {
924 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
925 return -1;
928 hci = hci_init(str);
929 if (!hci)
930 return -1;
932 bdaddr.b[0] = 0x52;
933 bdaddr.b[1] = 0x54;
934 bdaddr.b[2] = 0x00;
935 bdaddr.b[3] = 0x12;
936 bdaddr.b[4] = 0x34;
937 bdaddr.b[5] = 0x56 + nb_hcis;
938 hci->bdaddr_set(hci, bdaddr.b);
940 hci_table[nb_hcis++] = hci;
942 return 0;
945 static void bt_vhci_add(int vlan_id)
947 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
949 if (!vlan->slave)
950 error_report("warning: adding a VHCI to an empty scatternet %i",
951 vlan_id);
953 bt_vhci_init(bt_new_hci(vlan));
956 static struct bt_device_s *bt_device_add(const char *opt)
958 struct bt_scatternet_s *vlan;
959 int vlan_id = 0;
960 char *endp = strstr(opt, ",vlan=");
961 int len = (endp ? endp - opt : strlen(opt)) + 1;
962 char devname[10];
964 pstrcpy(devname, MIN(sizeof(devname), len), opt);
966 if (endp) {
967 vlan_id = strtol(endp + 6, &endp, 0);
968 if (*endp) {
969 error_report("unrecognised bluetooth vlan Id");
970 return 0;
974 vlan = qemu_find_bt_vlan(vlan_id);
976 if (!vlan->slave)
977 error_report("warning: adding a slave device to an empty scatternet %i",
978 vlan_id);
980 if (!strcmp(devname, "keyboard"))
981 return bt_keyboard_init(vlan);
983 error_report("unsupported bluetooth device '%s'", devname);
984 return 0;
987 static int bt_parse(const char *opt)
989 const char *endp, *p;
990 int vlan;
992 if (strstart(opt, "hci", &endp)) {
993 if (!*endp || *endp == ',') {
994 if (*endp)
995 if (!strstart(endp, ",vlan=", 0))
996 opt = endp + 1;
998 return bt_hci_parse(opt);
1000 } else if (strstart(opt, "vhci", &endp)) {
1001 if (!*endp || *endp == ',') {
1002 if (*endp) {
1003 if (strstart(endp, ",vlan=", &p)) {
1004 vlan = strtol(p, (char **) &endp, 0);
1005 if (*endp) {
1006 error_report("bad scatternet '%s'", p);
1007 return 1;
1009 } else {
1010 error_report("bad parameter '%s'", endp + 1);
1011 return 1;
1013 } else
1014 vlan = 0;
1016 bt_vhci_add(vlan);
1017 return 0;
1019 } else if (strstart(opt, "device:", &endp))
1020 return !bt_device_add(endp);
1022 error_report("bad bluetooth parameter '%s'", opt);
1023 return 1;
1026 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1028 /* FIXME: change this to true for 1.3 */
1029 if (qemu_opt_get_bool(opts, "enable", false)) {
1030 #ifdef CONFIG_SECCOMP
1031 if (seccomp_start() < 0) {
1032 error_report("failed to install seccomp syscall filter "
1033 "in the kernel");
1034 return -1;
1036 #else
1037 error_report("seccomp support is disabled");
1038 return -1;
1039 #endif
1042 return 0;
1045 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
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 defaults_enabled(void)
1064 return has_defaults;
1067 bool usb_enabled(void)
1069 return machine_usb(current_machine);
1072 #ifndef _WIN32
1073 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1075 int fd, dupfd, flags;
1076 int64_t fdset_id;
1077 const char *fd_opaque = NULL;
1078 AddfdInfo *fdinfo;
1080 fd = qemu_opt_get_number(opts, "fd", -1);
1081 fdset_id = qemu_opt_get_number(opts, "set", -1);
1082 fd_opaque = qemu_opt_get(opts, "opaque");
1084 if (fd < 0) {
1085 error_report("fd option is required and must be non-negative");
1086 return -1;
1089 if (fd <= STDERR_FILENO) {
1090 error_report("fd cannot be a standard I/O stream");
1091 return -1;
1095 * All fds inherited across exec() necessarily have FD_CLOEXEC
1096 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1098 flags = fcntl(fd, F_GETFD);
1099 if (flags == -1 || (flags & FD_CLOEXEC)) {
1100 error_report("fd is not valid or already in use");
1101 return -1;
1104 if (fdset_id < 0) {
1105 error_report("set option is required and must be non-negative");
1106 return -1;
1109 #ifdef F_DUPFD_CLOEXEC
1110 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1111 #else
1112 dupfd = dup(fd);
1113 if (dupfd != -1) {
1114 qemu_set_cloexec(dupfd);
1116 #endif
1117 if (dupfd == -1) {
1118 error_report("error duplicating fd: %s", strerror(errno));
1119 return -1;
1122 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1123 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1124 &error_abort);
1125 g_free(fdinfo);
1127 return 0;
1130 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1132 int fd;
1134 fd = qemu_opt_get_number(opts, "fd", -1);
1135 close(fd);
1137 return 0;
1139 #endif
1141 /***********************************************************/
1142 /* QEMU Block devices */
1144 #define HD_OPTS "media=disk"
1145 #define CDROM_OPTS "media=cdrom"
1146 #define FD_OPTS ""
1147 #define PFLASH_OPTS ""
1148 #define MTD_OPTS ""
1149 #define SD_OPTS ""
1151 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1153 BlockInterfaceType *block_default_type = opaque;
1155 return drive_new(opts, *block_default_type) == NULL;
1158 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1160 if (qemu_opt_get(opts, "snapshot") == NULL) {
1161 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1163 return 0;
1166 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1167 int index, const char *optstr)
1169 QemuOpts *opts;
1170 DriveInfo *dinfo;
1172 if (!enable || drive_get_by_index(type, index)) {
1173 return;
1176 opts = drive_add(type, index, NULL, optstr);
1177 if (snapshot) {
1178 drive_enable_snapshot(NULL, opts, NULL);
1181 dinfo = drive_new(opts, type);
1182 if (!dinfo) {
1183 exit(1);
1185 dinfo->is_default = true;
1189 static QemuOptsList qemu_smp_opts = {
1190 .name = "smp-opts",
1191 .implied_opt_name = "cpus",
1192 .merge_lists = true,
1193 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1194 .desc = {
1196 .name = "cpus",
1197 .type = QEMU_OPT_NUMBER,
1198 }, {
1199 .name = "sockets",
1200 .type = QEMU_OPT_NUMBER,
1201 }, {
1202 .name = "cores",
1203 .type = QEMU_OPT_NUMBER,
1204 }, {
1205 .name = "threads",
1206 .type = QEMU_OPT_NUMBER,
1207 }, {
1208 .name = "maxcpus",
1209 .type = QEMU_OPT_NUMBER,
1211 { /*End of list */ }
1215 static void smp_parse(QemuOpts *opts)
1217 if (opts) {
1219 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1220 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1221 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1222 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1224 /* compute missing values, prefer sockets over cores over threads */
1225 if (cpus == 0 || sockets == 0) {
1226 sockets = sockets > 0 ? sockets : 1;
1227 cores = cores > 0 ? cores : 1;
1228 threads = threads > 0 ? threads : 1;
1229 if (cpus == 0) {
1230 cpus = cores * threads * sockets;
1232 } else if (cores == 0) {
1233 threads = threads > 0 ? threads : 1;
1234 cores = cpus / (sockets * threads);
1235 } else if (threads == 0) {
1236 threads = cpus / (cores * sockets);
1237 } else if (sockets * cores * threads < cpus) {
1238 error_report("cpu topology: "
1239 "sockets (%u) * cores (%u) * threads (%u) < "
1240 "smp_cpus (%u)",
1241 sockets, cores, threads, cpus);
1242 exit(1);
1245 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1246 if (sockets * cores * threads > max_cpus) {
1247 error_report("cpu topology: "
1248 "sockets (%u) * cores (%u) * threads (%u) > "
1249 "maxcpus (%u)",
1250 sockets, cores, threads, max_cpus);
1251 exit(1);
1254 smp_cpus = cpus;
1255 smp_cores = cores > 0 ? cores : 1;
1256 smp_threads = threads > 0 ? threads : 1;
1260 if (max_cpus == 0) {
1261 max_cpus = smp_cpus;
1264 if (max_cpus > MAX_CPUMASK_BITS) {
1265 error_report("unsupported number of maxcpus");
1266 exit(1);
1268 if (max_cpus < smp_cpus) {
1269 error_report("maxcpus must be equal to or greater than smp");
1270 exit(1);
1273 if (smp_cpus > 1 || smp_cores > 1 || smp_threads > 1) {
1274 Error *blocker = NULL;
1275 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1276 replay_add_blocker(blocker);
1280 static void realtime_init(void)
1282 if (enable_mlock) {
1283 if (os_mlock() < 0) {
1284 error_report("locking memory failed");
1285 exit(1);
1291 static void configure_msg(QemuOpts *opts)
1293 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1296 /***********************************************************/
1297 /* Semihosting */
1299 typedef struct SemihostingConfig {
1300 bool enabled;
1301 SemihostingTarget target;
1302 const char **argv;
1303 int argc;
1304 const char *cmdline; /* concatenated argv */
1305 } SemihostingConfig;
1307 static SemihostingConfig semihosting;
1309 bool semihosting_enabled(void)
1311 return semihosting.enabled;
1314 SemihostingTarget semihosting_get_target(void)
1316 return semihosting.target;
1319 const char *semihosting_get_arg(int i)
1321 if (i >= semihosting.argc) {
1322 return NULL;
1324 return semihosting.argv[i];
1327 int semihosting_get_argc(void)
1329 return semihosting.argc;
1332 const char *semihosting_get_cmdline(void)
1334 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1335 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1337 return semihosting.cmdline;
1340 static int add_semihosting_arg(void *opaque,
1341 const char *name, const char *val,
1342 Error **errp)
1344 SemihostingConfig *s = opaque;
1345 if (strcmp(name, "arg") == 0) {
1346 s->argc++;
1347 /* one extra element as g_strjoinv() expects NULL-terminated array */
1348 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1349 s->argv[s->argc - 1] = val;
1350 s->argv[s->argc] = NULL;
1352 return 0;
1355 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1356 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1358 char *cmd_token;
1360 /* argv[0] */
1361 add_semihosting_arg(&semihosting, "arg", file, NULL);
1363 /* split -append and initialize argv[1..n] */
1364 cmd_token = strtok(g_strdup(cmd), " ");
1365 while (cmd_token) {
1366 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1367 cmd_token = strtok(NULL, " ");
1371 /* Now we still need this for compatibility with XEN. */
1372 bool has_igd_gfx_passthru;
1373 static void igd_gfx_passthru(void)
1375 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1378 /***********************************************************/
1379 /* USB devices */
1381 static int usb_device_add(const char *devname)
1383 USBDevice *dev = NULL;
1384 #ifndef CONFIG_LINUX
1385 const char *p;
1386 #endif
1388 if (!usb_enabled()) {
1389 return -1;
1392 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1393 dev = usbdevice_create(devname);
1394 if (dev)
1395 goto done;
1397 /* the other ones */
1398 #ifndef CONFIG_LINUX
1399 /* only the linux version is qdev-ified, usb-bsd still needs this */
1400 if (strstart(devname, "host:", &p)) {
1401 dev = usb_host_device_open(usb_bus_find(-1), p);
1403 #endif
1404 if (!dev)
1405 return -1;
1407 done:
1408 return 0;
1411 static int usb_device_del(const char *devname)
1413 int bus_num, addr;
1414 const char *p;
1416 if (strstart(devname, "host:", &p)) {
1417 return -1;
1420 if (!usb_enabled()) {
1421 return -1;
1424 p = strchr(devname, '.');
1425 if (!p)
1426 return -1;
1427 bus_num = strtoul(devname, NULL, 0);
1428 addr = strtoul(p + 1, NULL, 0);
1430 return usb_device_delete_addr(bus_num, addr);
1433 static int usb_parse(const char *cmdline)
1435 int r;
1436 r = usb_device_add(cmdline);
1437 if (r < 0) {
1438 error_report("could not add USB device '%s'", cmdline);
1440 return r;
1443 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1445 const char *devname = qdict_get_str(qdict, "devname");
1446 if (usb_device_add(devname) < 0) {
1447 error_report("could not add USB device '%s'", devname);
1451 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1453 const char *devname = qdict_get_str(qdict, "devname");
1454 if (usb_device_del(devname) < 0) {
1455 error_report("could not delete USB device '%s'", devname);
1459 /***********************************************************/
1460 /* machine registration */
1462 MachineState *current_machine;
1464 static MachineClass *find_machine(const char *name)
1466 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1467 MachineClass *mc = NULL;
1469 for (el = machines; el; el = el->next) {
1470 MachineClass *temp = el->data;
1472 if (!strcmp(temp->name, name)) {
1473 mc = temp;
1474 break;
1476 if (temp->alias &&
1477 !strcmp(temp->alias, name)) {
1478 mc = temp;
1479 break;
1483 g_slist_free(machines);
1484 return mc;
1487 MachineClass *find_default_machine(void)
1489 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1490 MachineClass *mc = NULL;
1492 for (el = machines; el; el = el->next) {
1493 MachineClass *temp = el->data;
1495 if (temp->is_default) {
1496 mc = temp;
1497 break;
1501 g_slist_free(machines);
1502 return mc;
1505 MachineInfoList *qmp_query_machines(Error **errp)
1507 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1508 MachineInfoList *mach_list = NULL;
1510 for (el = machines; el; el = el->next) {
1511 MachineClass *mc = el->data;
1512 MachineInfoList *entry;
1513 MachineInfo *info;
1515 info = g_malloc0(sizeof(*info));
1516 if (mc->is_default) {
1517 info->has_is_default = true;
1518 info->is_default = true;
1521 if (mc->alias) {
1522 info->has_alias = true;
1523 info->alias = g_strdup(mc->alias);
1526 info->name = g_strdup(mc->name);
1527 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1529 entry = g_malloc0(sizeof(*entry));
1530 entry->value = info;
1531 entry->next = mach_list;
1532 mach_list = entry;
1535 g_slist_free(machines);
1536 return mach_list;
1539 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1541 ObjectProperty *prop;
1543 if (!qemu_opt_has_help_opt(opts)) {
1544 return 0;
1547 QTAILQ_FOREACH(prop, &OBJECT(machine)->properties, node) {
1548 if (!prop->set) {
1549 continue;
1552 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1553 prop->name, prop->type);
1554 if (prop->description) {
1555 error_printf(" (%s)\n", prop->description);
1556 } else {
1557 error_printf("\n");
1561 return 1;
1564 /***********************************************************/
1565 /* main execution loop */
1567 struct vm_change_state_entry {
1568 VMChangeStateHandler *cb;
1569 void *opaque;
1570 QLIST_ENTRY (vm_change_state_entry) entries;
1573 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1575 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1576 void *opaque)
1578 VMChangeStateEntry *e;
1580 e = g_malloc0(sizeof (*e));
1582 e->cb = cb;
1583 e->opaque = opaque;
1584 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1585 return e;
1588 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1590 QLIST_REMOVE (e, entries);
1591 g_free (e);
1594 void vm_state_notify(int running, RunState state)
1596 VMChangeStateEntry *e, *next;
1598 trace_vm_state_notify(running, state);
1600 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1601 e->cb(e->opaque, running, state);
1605 /* reset/shutdown handler */
1607 typedef struct QEMUResetEntry {
1608 QTAILQ_ENTRY(QEMUResetEntry) entry;
1609 QEMUResetHandler *func;
1610 void *opaque;
1611 } QEMUResetEntry;
1613 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1614 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1615 static int reset_requested;
1616 static int shutdown_requested, shutdown_signal = -1;
1617 static pid_t shutdown_pid;
1618 static int powerdown_requested;
1619 static int debug_requested;
1620 static int suspend_requested;
1621 static WakeupReason wakeup_reason;
1622 static NotifierList powerdown_notifiers =
1623 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1624 static NotifierList suspend_notifiers =
1625 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1626 static NotifierList wakeup_notifiers =
1627 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1628 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1630 int qemu_shutdown_requested_get(void)
1632 return shutdown_requested;
1635 int qemu_reset_requested_get(void)
1637 return reset_requested;
1640 static int qemu_shutdown_requested(void)
1642 return atomic_xchg(&shutdown_requested, 0);
1645 static void qemu_kill_report(void)
1647 if (!qtest_driver() && shutdown_signal != -1) {
1648 if (shutdown_pid == 0) {
1649 /* This happens for eg ^C at the terminal, so it's worth
1650 * avoiding printing an odd message in that case.
1652 error_report("terminating on signal %d", shutdown_signal);
1653 } else {
1654 error_report("terminating on signal %d from pid " FMT_pid,
1655 shutdown_signal, shutdown_pid);
1657 shutdown_signal = -1;
1661 static int qemu_reset_requested(void)
1663 int r = reset_requested;
1664 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1665 reset_requested = 0;
1666 return r;
1668 return false;
1671 static int qemu_suspend_requested(void)
1673 int r = suspend_requested;
1674 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1675 suspend_requested = 0;
1676 return r;
1678 return false;
1681 static WakeupReason qemu_wakeup_requested(void)
1683 return wakeup_reason;
1686 static int qemu_powerdown_requested(void)
1688 int r = powerdown_requested;
1689 powerdown_requested = 0;
1690 return r;
1693 static int qemu_debug_requested(void)
1695 int r = debug_requested;
1696 debug_requested = 0;
1697 return r;
1700 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1702 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1704 re->func = func;
1705 re->opaque = opaque;
1706 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1709 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1711 QEMUResetEntry *re;
1713 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1714 if (re->func == func && re->opaque == opaque) {
1715 QTAILQ_REMOVE(&reset_handlers, re, entry);
1716 g_free(re);
1717 return;
1722 void qemu_devices_reset(void)
1724 QEMUResetEntry *re, *nre;
1726 /* reset all devices */
1727 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1728 re->func(re->opaque);
1732 void qemu_system_reset(bool report)
1734 MachineClass *mc;
1736 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1738 if (mc && mc->reset) {
1739 mc->reset();
1740 } else {
1741 qemu_devices_reset();
1743 if (report) {
1744 qapi_event_send_reset(&error_abort);
1746 cpu_synchronize_all_post_reset();
1749 void qemu_system_guest_panicked(void)
1751 if (current_cpu) {
1752 current_cpu->crash_occurred = true;
1754 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1755 vm_stop(RUN_STATE_GUEST_PANICKED);
1758 void qemu_system_reset_request(void)
1760 if (no_reboot) {
1761 shutdown_requested = 1;
1762 } else {
1763 reset_requested = 1;
1765 cpu_stop_current();
1766 qemu_notify_event();
1769 static void qemu_system_suspend(void)
1771 pause_all_vcpus();
1772 notifier_list_notify(&suspend_notifiers, NULL);
1773 runstate_set(RUN_STATE_SUSPENDED);
1774 qapi_event_send_suspend(&error_abort);
1777 void qemu_system_suspend_request(void)
1779 if (runstate_check(RUN_STATE_SUSPENDED)) {
1780 return;
1782 suspend_requested = 1;
1783 cpu_stop_current();
1784 qemu_notify_event();
1787 void qemu_register_suspend_notifier(Notifier *notifier)
1789 notifier_list_add(&suspend_notifiers, notifier);
1792 void qemu_system_wakeup_request(WakeupReason reason)
1794 trace_system_wakeup_request(reason);
1796 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1797 return;
1799 if (!(wakeup_reason_mask & (1 << reason))) {
1800 return;
1802 runstate_set(RUN_STATE_RUNNING);
1803 wakeup_reason = reason;
1804 qemu_notify_event();
1807 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1809 if (enabled) {
1810 wakeup_reason_mask |= (1 << reason);
1811 } else {
1812 wakeup_reason_mask &= ~(1 << reason);
1816 void qemu_register_wakeup_notifier(Notifier *notifier)
1818 notifier_list_add(&wakeup_notifiers, notifier);
1821 void qemu_system_killed(int signal, pid_t pid)
1823 shutdown_signal = signal;
1824 shutdown_pid = pid;
1825 no_shutdown = 0;
1827 /* Cannot call qemu_system_shutdown_request directly because
1828 * we are in a signal handler.
1830 shutdown_requested = 1;
1831 qemu_notify_event();
1834 void qemu_system_shutdown_request(void)
1836 trace_qemu_system_shutdown_request();
1837 replay_shutdown_request();
1838 shutdown_requested = 1;
1839 qemu_notify_event();
1842 static void qemu_system_powerdown(void)
1844 qapi_event_send_powerdown(&error_abort);
1845 notifier_list_notify(&powerdown_notifiers, NULL);
1848 void qemu_system_powerdown_request(void)
1850 trace_qemu_system_powerdown_request();
1851 powerdown_requested = 1;
1852 qemu_notify_event();
1855 void qemu_register_powerdown_notifier(Notifier *notifier)
1857 notifier_list_add(&powerdown_notifiers, notifier);
1860 void qemu_system_debug_request(void)
1862 debug_requested = 1;
1863 qemu_notify_event();
1866 static bool main_loop_should_exit(void)
1868 RunState r;
1869 if (qemu_debug_requested()) {
1870 vm_stop(RUN_STATE_DEBUG);
1872 if (qemu_suspend_requested()) {
1873 qemu_system_suspend();
1875 if (qemu_shutdown_requested()) {
1876 qemu_kill_report();
1877 qapi_event_send_shutdown(&error_abort);
1878 if (no_shutdown) {
1879 vm_stop(RUN_STATE_SHUTDOWN);
1880 } else {
1881 return true;
1884 if (qemu_reset_requested()) {
1885 pause_all_vcpus();
1886 cpu_synchronize_all_states();
1887 qemu_system_reset(VMRESET_REPORT);
1888 resume_all_vcpus();
1889 if (runstate_needs_reset()) {
1890 runstate_set(RUN_STATE_PAUSED);
1893 if (qemu_wakeup_requested()) {
1894 pause_all_vcpus();
1895 cpu_synchronize_all_states();
1896 qemu_system_reset(VMRESET_SILENT);
1897 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1898 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1899 resume_all_vcpus();
1900 qapi_event_send_wakeup(&error_abort);
1902 if (qemu_powerdown_requested()) {
1903 qemu_system_powerdown();
1905 if (qemu_vmstop_requested(&r)) {
1906 vm_stop(r);
1908 return false;
1911 static void main_loop(void)
1913 bool nonblocking;
1914 int last_io = 0;
1915 #ifdef CONFIG_PROFILER
1916 int64_t ti;
1917 #endif
1918 do {
1919 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1920 #ifdef CONFIG_PROFILER
1921 ti = profile_getclock();
1922 #endif
1923 last_io = main_loop_wait(nonblocking);
1924 #ifdef CONFIG_PROFILER
1925 dev_time += profile_getclock() - ti;
1926 #endif
1927 } while (!main_loop_should_exit());
1930 static void version(void)
1932 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1935 static void QEMU_NORETURN help(int exitcode)
1937 version();
1938 printf("usage: %s [options] [disk_image]\n\n"
1939 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1940 error_get_progname());
1942 #define QEMU_OPTIONS_GENERATE_HELP
1943 #include "qemu-options-wrapper.h"
1945 printf("\nDuring emulation, the following keys are useful:\n"
1946 "ctrl-alt-f toggle full screen\n"
1947 "ctrl-alt-n switch to virtual console 'n'\n"
1948 "ctrl-alt toggle mouse and keyboard grab\n"
1949 "\n"
1950 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1952 exit(exitcode);
1955 #define HAS_ARG 0x0001
1957 typedef struct QEMUOption {
1958 const char *name;
1959 int flags;
1960 int index;
1961 uint32_t arch_mask;
1962 } QEMUOption;
1964 static const QEMUOption qemu_options[] = {
1965 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1966 #define QEMU_OPTIONS_GENERATE_OPTIONS
1967 #include "qemu-options-wrapper.h"
1968 { NULL },
1971 static bool vga_available(void)
1973 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1976 static bool cirrus_vga_available(void)
1978 return object_class_by_name("cirrus-vga")
1979 || object_class_by_name("isa-cirrus-vga");
1982 static bool vmware_vga_available(void)
1984 return object_class_by_name("vmware-svga");
1987 static bool qxl_vga_available(void)
1989 return object_class_by_name("qxl-vga");
1992 static bool tcx_vga_available(void)
1994 return object_class_by_name("SUNW,tcx");
1997 static bool cg3_vga_available(void)
1999 return object_class_by_name("cgthree");
2002 static bool virtio_vga_available(void)
2004 return object_class_by_name("virtio-vga");
2007 static void select_vgahw (const char *p)
2009 const char *opts;
2011 assert(vga_interface_type == VGA_NONE);
2012 if (strstart(p, "std", &opts)) {
2013 if (vga_available()) {
2014 vga_interface_type = VGA_STD;
2015 } else {
2016 error_report("standard VGA not available");
2017 exit(0);
2019 } else if (strstart(p, "cirrus", &opts)) {
2020 if (cirrus_vga_available()) {
2021 vga_interface_type = VGA_CIRRUS;
2022 } else {
2023 error_report("Cirrus VGA not available");
2024 exit(0);
2026 } else if (strstart(p, "vmware", &opts)) {
2027 if (vmware_vga_available()) {
2028 vga_interface_type = VGA_VMWARE;
2029 } else {
2030 error_report("VMWare SVGA not available");
2031 exit(0);
2033 } else if (strstart(p, "virtio", &opts)) {
2034 if (virtio_vga_available()) {
2035 vga_interface_type = VGA_VIRTIO;
2036 } else {
2037 error_report("Virtio VGA not available");
2038 exit(0);
2040 } else if (strstart(p, "xenfb", &opts)) {
2041 vga_interface_type = VGA_XENFB;
2042 } else if (strstart(p, "qxl", &opts)) {
2043 if (qxl_vga_available()) {
2044 vga_interface_type = VGA_QXL;
2045 } else {
2046 error_report("QXL VGA not available");
2047 exit(0);
2049 } else if (strstart(p, "tcx", &opts)) {
2050 if (tcx_vga_available()) {
2051 vga_interface_type = VGA_TCX;
2052 } else {
2053 error_report("TCX framebuffer not available");
2054 exit(0);
2056 } else if (strstart(p, "cg3", &opts)) {
2057 if (cg3_vga_available()) {
2058 vga_interface_type = VGA_CG3;
2059 } else {
2060 error_report("CG3 framebuffer not available");
2061 exit(0);
2063 } else if (!strstart(p, "none", &opts)) {
2064 invalid_vga:
2065 error_report("unknown vga type: %s", p);
2066 exit(1);
2068 while (*opts) {
2069 const char *nextopt;
2071 if (strstart(opts, ",retrace=", &nextopt)) {
2072 opts = nextopt;
2073 if (strstart(opts, "dumb", &nextopt))
2074 vga_retrace_method = VGA_RETRACE_DUMB;
2075 else if (strstart(opts, "precise", &nextopt))
2076 vga_retrace_method = VGA_RETRACE_PRECISE;
2077 else goto invalid_vga;
2078 } else goto invalid_vga;
2079 opts = nextopt;
2083 static DisplayType select_display(const char *p)
2085 const char *opts;
2086 DisplayType display = DT_DEFAULT;
2088 if (strstart(p, "sdl", &opts)) {
2089 #ifdef CONFIG_SDL
2090 display = DT_SDL;
2091 while (*opts) {
2092 const char *nextopt;
2094 if (strstart(opts, ",frame=", &nextopt)) {
2095 opts = nextopt;
2096 if (strstart(opts, "on", &nextopt)) {
2097 no_frame = 0;
2098 } else if (strstart(opts, "off", &nextopt)) {
2099 no_frame = 1;
2100 } else {
2101 goto invalid_sdl_args;
2103 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2104 opts = nextopt;
2105 if (strstart(opts, "on", &nextopt)) {
2106 alt_grab = 1;
2107 } else if (strstart(opts, "off", &nextopt)) {
2108 alt_grab = 0;
2109 } else {
2110 goto invalid_sdl_args;
2112 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2113 opts = nextopt;
2114 if (strstart(opts, "on", &nextopt)) {
2115 ctrl_grab = 1;
2116 } else if (strstart(opts, "off", &nextopt)) {
2117 ctrl_grab = 0;
2118 } else {
2119 goto invalid_sdl_args;
2121 } else if (strstart(opts, ",window_close=", &nextopt)) {
2122 opts = nextopt;
2123 if (strstart(opts, "on", &nextopt)) {
2124 no_quit = 0;
2125 } else if (strstart(opts, "off", &nextopt)) {
2126 no_quit = 1;
2127 } else {
2128 goto invalid_sdl_args;
2130 } else if (strstart(opts, ",gl=", &nextopt)) {
2131 opts = nextopt;
2132 if (strstart(opts, "on", &nextopt)) {
2133 request_opengl = 1;
2134 } else if (strstart(opts, "off", &nextopt)) {
2135 request_opengl = 0;
2136 } else {
2137 goto invalid_sdl_args;
2139 } else {
2140 invalid_sdl_args:
2141 error_report("invalid SDL option string");
2142 exit(1);
2144 opts = nextopt;
2146 #else
2147 error_report("SDL support is disabled");
2148 exit(1);
2149 #endif
2150 } else if (strstart(p, "vnc", &opts)) {
2151 #ifdef CONFIG_VNC
2152 if (*opts == '=') {
2153 Error *err = NULL;
2154 if (vnc_parse(opts + 1, &err) == NULL) {
2155 error_report_err(err);
2156 exit(1);
2158 } else {
2159 error_report("VNC requires a display argument vnc=<display>");
2160 exit(1);
2162 #else
2163 error_report("VNC support is disabled");
2164 exit(1);
2165 #endif
2166 } else if (strstart(p, "curses", &opts)) {
2167 #ifdef CONFIG_CURSES
2168 display = DT_CURSES;
2169 #else
2170 error_report("curses support is disabled");
2171 exit(1);
2172 #endif
2173 } else if (strstart(p, "gtk", &opts)) {
2174 #ifdef CONFIG_GTK
2175 display = DT_GTK;
2176 while (*opts) {
2177 const char *nextopt;
2179 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2180 opts = nextopt;
2181 if (strstart(opts, "on", &nextopt)) {
2182 grab_on_hover = true;
2183 } else if (strstart(opts, "off", &nextopt)) {
2184 grab_on_hover = false;
2185 } else {
2186 goto invalid_gtk_args;
2188 } else if (strstart(opts, ",gl=", &nextopt)) {
2189 opts = nextopt;
2190 if (strstart(opts, "on", &nextopt)) {
2191 request_opengl = 1;
2192 } else if (strstart(opts, "off", &nextopt)) {
2193 request_opengl = 0;
2194 } else {
2195 goto invalid_gtk_args;
2197 } else {
2198 invalid_gtk_args:
2199 error_report("invalid GTK option string");
2200 exit(1);
2202 opts = nextopt;
2204 #else
2205 error_report("GTK support is disabled");
2206 exit(1);
2207 #endif
2208 } else if (strstart(p, "none", &opts)) {
2209 display = DT_NONE;
2210 } else {
2211 error_report("unknown display type");
2212 exit(1);
2215 return display;
2218 static int balloon_parse(const char *arg)
2220 QemuOpts *opts;
2222 if (strcmp(arg, "none") == 0) {
2223 return 0;
2226 if (!strncmp(arg, "virtio", 6)) {
2227 if (arg[6] == ',') {
2228 /* have params -> parse them */
2229 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2230 false);
2231 if (!opts)
2232 return -1;
2233 } else {
2234 /* create empty opts */
2235 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2236 &error_abort);
2238 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2239 return 0;
2242 return -1;
2245 #if defined(CONFIG_RUBY)
2246 /* Ruby interface for QEMU. See README.EXT for programming example. */
2247 #warning mit ruby
2248 #include <ruby.h>
2250 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2252 int i;
2253 for (i = 0; i < argc; i++) {
2254 argv[i] = StringValue(argv[i]);
2255 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2257 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2258 //~ mode = 0666; /* default value */
2259 //~ }
2260 return INT2FIX(argc);
2263 void Init_qemu(void)
2265 printf("%s\n", __func__);
2266 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2267 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2268 #if 0
2269 rb_define_method();
2270 #endif
2272 #endif /* CONFIG_RUBY */
2274 #if defined(CONFIG_DLL)
2275 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2277 return FALSE;
2279 #endif /* CONFIG_DLL */
2281 char *qemu_find_file(int type, const char *name)
2283 int i;
2284 const char *subdir;
2285 char *buf;
2287 /* Try the name as a straight path first */
2288 if (access(name, R_OK) == 0) {
2289 trace_load_file(name, name);
2290 return g_strdup(name);
2293 switch (type) {
2294 case QEMU_FILE_TYPE_BIOS:
2295 subdir = "";
2296 break;
2297 case QEMU_FILE_TYPE_KEYMAP:
2298 subdir = "keymaps/";
2299 break;
2300 default:
2301 abort();
2304 for (i = 0; i < data_dir_idx; i++) {
2305 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2306 if (access(buf, R_OK) == 0) {
2307 trace_load_file(name, buf);
2308 return buf;
2310 g_free(buf);
2312 return NULL;
2315 static inline bool nonempty_str(const char *str)
2317 return str && *str;
2320 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2322 gchar *buf;
2323 size_t size;
2324 const char *name, *file, *str;
2326 if (opaque == NULL) {
2327 error_report("fw_cfg device not available");
2328 return -1;
2330 name = qemu_opt_get(opts, "name");
2331 file = qemu_opt_get(opts, "file");
2332 str = qemu_opt_get(opts, "string");
2334 /* we need name and either a file or the content string */
2335 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2336 error_report("invalid argument(s)");
2337 return -1;
2339 if (nonempty_str(file) && nonempty_str(str)) {
2340 error_report("file and string are mutually exclusive");
2341 return -1;
2343 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2344 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2345 return -1;
2347 if (strncmp(name, "opt/", 4) != 0) {
2348 error_report("warning: externally provided fw_cfg item names "
2349 "should be prefixed with \"opt/\"");
2351 if (nonempty_str(str)) {
2352 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2353 buf = g_memdup(str, size);
2354 } else {
2355 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2356 error_report("can't load %s", file);
2357 return -1;
2360 fw_cfg_add_file((FWCfgState *)opaque, name, buf, size);
2361 return 0;
2364 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2366 return qdev_device_help(opts);
2369 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2371 Error *err = NULL;
2372 DeviceState *dev;
2374 dev = qdev_device_add(opts, &err);
2375 if (!dev) {
2376 error_report_err(err);
2377 return -1;
2379 object_unref(OBJECT(dev));
2380 return 0;
2383 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2385 Error *local_err = NULL;
2387 qemu_chr_new_from_opts(opts, NULL, &local_err);
2388 if (local_err) {
2389 error_report_err(local_err);
2390 return -1;
2392 return 0;
2395 #ifdef CONFIG_VIRTFS
2396 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2398 int ret;
2399 ret = qemu_fsdev_add(opts);
2401 return ret;
2403 #endif
2405 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2407 CharDriverState *chr;
2408 const char *chardev;
2409 const char *mode;
2410 int flags;
2412 mode = qemu_opt_get(opts, "mode");
2413 if (mode == NULL) {
2414 mode = "readline";
2416 if (strcmp(mode, "readline") == 0) {
2417 flags = MONITOR_USE_READLINE;
2418 } else if (strcmp(mode, "control") == 0) {
2419 flags = MONITOR_USE_CONTROL;
2420 } else {
2421 error_report("unknown monitor mode \"%s\"", mode);
2422 exit(1);
2425 if (qemu_opt_get_bool(opts, "pretty", 0))
2426 flags |= MONITOR_USE_PRETTY;
2428 if (qemu_opt_get_bool(opts, "default", 0))
2429 flags |= MONITOR_IS_DEFAULT;
2431 chardev = qemu_opt_get(opts, "chardev");
2432 chr = qemu_chr_find(chardev);
2433 if (chr == NULL) {
2434 error_report("chardev \"%s\" not found", chardev);
2435 exit(1);
2438 qemu_chr_fe_claim_no_fail(chr);
2439 monitor_init(chr, flags);
2440 return 0;
2443 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2445 static int monitor_device_index = 0;
2446 Error *local_err = NULL;
2447 QemuOpts *opts;
2448 const char *p;
2449 char label[32];
2450 int def = 0;
2452 if (strstart(optarg, "chardev:", &p)) {
2453 snprintf(label, sizeof(label), "%s", p);
2454 } else {
2455 snprintf(label, sizeof(label), "compat_monitor%d",
2456 monitor_device_index);
2457 if (monitor_device_index == 0) {
2458 def = 1;
2460 opts = qemu_chr_parse_compat(label, optarg);
2461 if (!opts) {
2462 error_report("parse error: %s", optarg);
2463 exit(1);
2467 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2468 if (!opts) {
2469 error_report_err(local_err);
2470 exit(1);
2472 qemu_opt_set(opts, "mode", mode, &error_abort);
2473 qemu_opt_set(opts, "chardev", label, &error_abort);
2474 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2475 if (def)
2476 qemu_opt_set(opts, "default", "on", &error_abort);
2477 monitor_device_index++;
2480 struct device_config {
2481 enum {
2482 DEV_USB, /* -usbdevice */
2483 DEV_BT, /* -bt */
2484 DEV_SERIAL, /* -serial */
2485 DEV_PARALLEL, /* -parallel */
2486 DEV_VIRTCON, /* -virtioconsole */
2487 DEV_DEBUGCON, /* -debugcon */
2488 DEV_GDB, /* -gdb, -s */
2489 DEV_SCLP, /* s390 sclp */
2490 } type;
2491 const char *cmdline;
2492 Location loc;
2493 QTAILQ_ENTRY(device_config) next;
2496 static QTAILQ_HEAD(, device_config) device_configs =
2497 QTAILQ_HEAD_INITIALIZER(device_configs);
2499 static void add_device_config(int type, const char *cmdline)
2501 struct device_config *conf;
2503 conf = g_malloc0(sizeof(*conf));
2504 conf->type = type;
2505 conf->cmdline = cmdline;
2506 loc_save(&conf->loc);
2507 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2510 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2512 struct device_config *conf;
2513 int rc;
2515 QTAILQ_FOREACH(conf, &device_configs, next) {
2516 if (conf->type != type)
2517 continue;
2518 loc_push_restore(&conf->loc);
2519 rc = func(conf->cmdline);
2520 loc_pop(&conf->loc);
2521 if (rc) {
2522 return rc;
2525 return 0;
2528 static int serial_parse(const char *devname)
2530 static int index = 0;
2531 char label[32];
2533 if (strcmp(devname, "none") == 0)
2534 return 0;
2535 if (index == MAX_SERIAL_PORTS) {
2536 error_report("too many serial ports");
2537 exit(1);
2539 snprintf(label, sizeof(label), "serial%d", index);
2540 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2541 if (!serial_hds[index]) {
2542 error_report("could not connect serial device"
2543 " to character backend '%s'", devname);
2544 return -1;
2546 index++;
2547 return 0;
2550 static int parallel_parse(const char *devname)
2552 static int index = 0;
2553 char label[32];
2555 if (strcmp(devname, "none") == 0)
2556 return 0;
2557 if (index == MAX_PARALLEL_PORTS) {
2558 error_report("too many parallel ports");
2559 exit(1);
2561 snprintf(label, sizeof(label), "parallel%d", index);
2562 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2563 if (!parallel_hds[index]) {
2564 error_report("could not connect parallel device"
2565 " to character backend '%s'", devname);
2566 return -1;
2568 index++;
2569 return 0;
2572 static int virtcon_parse(const char *devname)
2574 QemuOptsList *device = qemu_find_opts("device");
2575 static int index = 0;
2576 char label[32];
2577 QemuOpts *bus_opts, *dev_opts;
2579 if (strcmp(devname, "none") == 0)
2580 return 0;
2581 if (index == MAX_VIRTIO_CONSOLES) {
2582 error_report("too many virtio consoles");
2583 exit(1);
2586 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2587 if (arch_type == QEMU_ARCH_S390X) {
2588 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390", &error_abort);
2589 } else {
2590 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci", &error_abort);
2593 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2594 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2596 snprintf(label, sizeof(label), "virtcon%d", index);
2597 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2598 if (!virtcon_hds[index]) {
2599 error_report("could not connect virtio console"
2600 " to character backend '%s'", devname);
2601 return -1;
2603 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2605 index++;
2606 return 0;
2609 static int sclp_parse(const char *devname)
2611 QemuOptsList *device = qemu_find_opts("device");
2612 static int index = 0;
2613 char label[32];
2614 QemuOpts *dev_opts;
2616 if (strcmp(devname, "none") == 0) {
2617 return 0;
2619 if (index == MAX_SCLP_CONSOLES) {
2620 error_report("too many sclp consoles");
2621 exit(1);
2624 assert(arch_type == QEMU_ARCH_S390X);
2626 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2627 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2629 snprintf(label, sizeof(label), "sclpcon%d", index);
2630 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2631 if (!sclp_hds[index]) {
2632 error_report("could not connect sclp console"
2633 " to character backend '%s'", devname);
2634 return -1;
2636 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2638 index++;
2639 return 0;
2642 static int debugcon_parse(const char *devname)
2644 QemuOpts *opts;
2646 if (!qemu_chr_new("debugcon", devname, NULL)) {
2647 exit(1);
2649 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2650 if (!opts) {
2651 error_report("already have a debugcon device");
2652 exit(1);
2654 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2655 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2656 return 0;
2659 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2661 const MachineClass *mc1 = a, *mc2 = b;
2662 int res;
2664 if (mc1->family == NULL) {
2665 if (mc2->family == NULL) {
2666 /* Compare standalone machine types against each other; they sort
2667 * in increasing order.
2669 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2670 object_class_get_name(OBJECT_CLASS(mc2)));
2673 /* Standalone machine types sort after families. */
2674 return 1;
2677 if (mc2->family == NULL) {
2678 /* Families sort before standalone machine types. */
2679 return -1;
2682 /* Families sort between each other alphabetically increasingly. */
2683 res = strcmp(mc1->family, mc2->family);
2684 if (res != 0) {
2685 return res;
2688 /* Within the same family, machine types sort in decreasing order. */
2689 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2690 object_class_get_name(OBJECT_CLASS(mc1)));
2693 static MachineClass *machine_parse(const char *name)
2695 MachineClass *mc = NULL;
2696 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2698 if (name) {
2699 mc = find_machine(name);
2701 if (mc) {
2702 g_slist_free(machines);
2703 return mc;
2705 if (name && !is_help_option(name)) {
2706 error_report("unsupported machine type");
2707 error_printf("Use -machine help to list supported machines\n");
2708 } else {
2709 printf("Supported machines are:\n");
2710 machines = g_slist_sort(machines, machine_class_cmp);
2711 for (el = machines; el; el = el->next) {
2712 MachineClass *mc = el->data;
2713 if (mc->alias) {
2714 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2716 printf("%-20s %s%s\n", mc->name, mc->desc,
2717 mc->is_default ? " (default)" : "");
2721 g_slist_free(machines);
2722 exit(!name || !is_help_option(name));
2725 void qemu_add_exit_notifier(Notifier *notify)
2727 notifier_list_add(&exit_notifiers, notify);
2730 void qemu_remove_exit_notifier(Notifier *notify)
2732 notifier_remove(notify);
2735 static void qemu_run_exit_notifiers(void)
2737 notifier_list_notify(&exit_notifiers, NULL);
2740 static bool machine_init_done;
2742 void qemu_add_machine_init_done_notifier(Notifier *notify)
2744 notifier_list_add(&machine_init_done_notifiers, notify);
2745 if (machine_init_done) {
2746 notify->notify(notify, NULL);
2750 static void qemu_run_machine_init_done_notifiers(void)
2752 notifier_list_notify(&machine_init_done_notifiers, NULL);
2753 machine_init_done = true;
2756 static const QEMUOption *lookup_opt(int argc, char **argv,
2757 const char **poptarg, int *poptind)
2759 const QEMUOption *popt;
2760 int optind = *poptind;
2761 char *r = argv[optind];
2762 const char *optarg;
2764 loc_set_cmdline(argv, optind, 1);
2765 optind++;
2766 /* Treat --foo the same as -foo. */
2767 if (r[1] == '-')
2768 r++;
2769 popt = qemu_options;
2770 for(;;) {
2771 if (!popt->name) {
2772 error_report("invalid option");
2773 exit(1);
2775 if (!strcmp(popt->name, r + 1))
2776 break;
2777 popt++;
2779 if (popt->flags & HAS_ARG) {
2780 if (optind >= argc) {
2781 error_report("requires an argument");
2782 exit(1);
2784 optarg = argv[optind++];
2785 loc_set_cmdline(argv, optind - 2, 2);
2786 } else {
2787 optarg = NULL;
2790 *poptarg = optarg;
2791 *poptind = optind;
2793 return popt;
2796 static int machine_set_property(void *opaque,
2797 const char *name, const char *value,
2798 Error **errp)
2800 Object *obj = OBJECT(opaque);
2801 Error *local_err = NULL;
2802 char *c, *qom_name;
2804 if (strcmp(name, "type") == 0) {
2805 return 0;
2808 qom_name = g_strdup(name);
2809 c = qom_name;
2810 while (*c++) {
2811 if (*c == '_') {
2812 *c = '-';
2816 object_property_parse(obj, value, qom_name, &local_err);
2817 g_free(qom_name);
2819 if (local_err) {
2820 error_report_err(local_err);
2821 return -1;
2824 return 0;
2829 * Initial object creation happens before all other
2830 * QEMU data types are created. The majority of objects
2831 * can be created at this point. The rng-egd object
2832 * cannot be created here, as it depends on the chardev
2833 * already existing.
2835 static bool object_create_initial(const char *type)
2837 if (g_str_equal(type, "rng-egd")) {
2838 return false;
2842 * return false for concrete netfilters since
2843 * they depend on netdevs already existing
2845 if (g_str_equal(type, "filter-buffer") ||
2846 g_str_equal(type, "filter-dump")) {
2847 return false;
2850 return true;
2855 * The remainder of object creation happens after the
2856 * creation of chardev, fsdev, net clients and device data types.
2858 static bool object_create_delayed(const char *type)
2860 return !object_create_initial(type);
2864 static int object_create(void *opaque, QemuOpts *opts, Error **errp)
2866 Error *err = NULL;
2867 char *type = NULL;
2868 char *id = NULL;
2869 void *dummy = NULL;
2870 OptsVisitor *ov;
2871 QDict *pdict;
2872 bool (*type_predicate)(const char *) = opaque;
2874 ov = opts_visitor_new(opts);
2875 pdict = qemu_opts_to_qdict(opts, NULL);
2877 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2878 if (err) {
2879 goto out;
2882 qdict_del(pdict, "qom-type");
2883 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2884 if (err) {
2885 goto out;
2887 if (!type_predicate(type)) {
2888 goto out;
2891 qdict_del(pdict, "id");
2892 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2893 if (err) {
2894 goto out;
2897 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2898 if (err) {
2899 goto out;
2901 visit_end_struct(opts_get_visitor(ov), &err);
2902 if (err) {
2903 qmp_object_del(id, NULL);
2906 out:
2907 opts_visitor_cleanup(ov);
2909 QDECREF(pdict);
2910 g_free(id);
2911 g_free(type);
2912 g_free(dummy);
2913 if (err) {
2914 error_report_err(err);
2915 return -1;
2917 return 0;
2920 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2921 MachineClass *mc)
2923 uint64_t sz;
2924 const char *mem_str;
2925 const char *maxmem_str, *slots_str;
2926 const ram_addr_t default_ram_size = mc->default_ram_size;
2927 QemuOpts *opts = qemu_find_opts_singleton("memory");
2929 sz = 0;
2930 mem_str = qemu_opt_get(opts, "size");
2931 if (mem_str) {
2932 if (!*mem_str) {
2933 error_report("missing 'size' option value");
2934 exit(EXIT_FAILURE);
2937 sz = qemu_opt_get_size(opts, "size", ram_size);
2939 /* Fix up legacy suffix-less format */
2940 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2941 uint64_t overflow_check = sz;
2943 sz <<= 20;
2944 if ((sz >> 20) != overflow_check) {
2945 error_report("too large 'size' option value");
2946 exit(EXIT_FAILURE);
2951 /* backward compatibility behaviour for case "-m 0" */
2952 if (sz == 0) {
2953 sz = default_ram_size;
2956 sz = QEMU_ALIGN_UP(sz, 8192);
2957 ram_size = sz;
2958 if (ram_size != sz) {
2959 error_report("ram size too large");
2960 exit(EXIT_FAILURE);
2963 /* store value for the future use */
2964 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2965 *maxram_size = ram_size;
2967 maxmem_str = qemu_opt_get(opts, "maxmem");
2968 slots_str = qemu_opt_get(opts, "slots");
2969 if (maxmem_str && slots_str) {
2970 uint64_t slots;
2972 sz = qemu_opt_get_size(opts, "maxmem", 0);
2973 slots = qemu_opt_get_number(opts, "slots", 0);
2974 if (sz < ram_size) {
2975 error_report("invalid value of -m option maxmem: "
2976 "maximum memory size (0x%" PRIx64 ") must be at least "
2977 "the initial memory size (0x" RAM_ADDR_FMT ")",
2978 sz, ram_size);
2979 exit(EXIT_FAILURE);
2980 } else if (sz > ram_size) {
2981 if (!slots) {
2982 error_report("invalid value of -m option: maxmem was "
2983 "specified, but no hotplug slots were specified");
2984 exit(EXIT_FAILURE);
2986 } else if (slots) {
2987 error_report("invalid value of -m option maxmem: "
2988 "memory slots were specified but maximum memory size "
2989 "(0x%" PRIx64 ") is equal to the initial memory size "
2990 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2991 exit(EXIT_FAILURE);
2994 *maxram_size = sz;
2995 *ram_slots = slots;
2996 } else if ((!maxmem_str && slots_str) ||
2997 (maxmem_str && !slots_str)) {
2998 error_report("invalid -m option value: missing "
2999 "'%s' option", slots_str ? "maxmem" : "slots");
3000 exit(EXIT_FAILURE);
3004 int main(int argc, char **argv)
3006 int i;
3007 int snapshot, linux_boot;
3008 const char *initrd_filename;
3009 const char *kernel_filename, *kernel_cmdline;
3010 const char *boot_order = NULL;
3011 const char *boot_once = NULL;
3012 DisplayState *ds;
3013 int cyls, heads, secs, translation;
3014 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
3015 QemuOptsList *olist;
3016 int optind;
3017 const char *optarg;
3018 const char *loadvm = NULL;
3019 MachineClass *machine_class;
3020 const char *cpu_model;
3021 const char *vga_model = NULL;
3022 const char *qtest_chrdev = NULL;
3023 const char *qtest_log = NULL;
3024 const char *pid_file = NULL;
3025 const char *incoming = NULL;
3026 #ifdef CONFIG_VNC
3027 int show_vnc_port = 0;
3028 #endif
3029 bool defconfig = true;
3030 bool userconfig = true;
3031 const char *log_mask = NULL;
3032 const char *log_file = NULL;
3033 const char *trace_events = NULL;
3034 const char *trace_file = NULL;
3035 ram_addr_t maxram_size;
3036 uint64_t ram_slots = 0;
3037 FILE *vmstate_dump_file = NULL;
3038 Error *main_loop_err = NULL;
3039 Error *err = NULL;
3041 qemu_init_cpu_loop();
3042 qemu_mutex_lock_iothread();
3044 atexit(qemu_run_exit_notifiers);
3045 error_set_progname(argv[0]);
3046 qemu_init_exec_dir(argv[0]);
3048 module_call_init(MODULE_INIT_QOM);
3050 qemu_add_opts(&qemu_drive_opts);
3051 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3052 qemu_add_drive_opts(&qemu_common_drive_opts);
3053 qemu_add_drive_opts(&qemu_drive_opts);
3054 qemu_add_opts(&qemu_chardev_opts);
3055 qemu_add_opts(&qemu_device_opts);
3056 qemu_add_opts(&qemu_netdev_opts);
3057 qemu_add_opts(&qemu_net_opts);
3058 qemu_add_opts(&qemu_rtc_opts);
3059 qemu_add_opts(&qemu_global_opts);
3060 qemu_add_opts(&qemu_mon_opts);
3061 qemu_add_opts(&qemu_trace_opts);
3062 qemu_add_opts(&qemu_option_rom_opts);
3063 qemu_add_opts(&qemu_machine_opts);
3064 qemu_add_opts(&qemu_mem_opts);
3065 qemu_add_opts(&qemu_smp_opts);
3066 qemu_add_opts(&qemu_boot_opts);
3067 qemu_add_opts(&qemu_sandbox_opts);
3068 qemu_add_opts(&qemu_add_fd_opts);
3069 qemu_add_opts(&qemu_object_opts);
3070 qemu_add_opts(&qemu_tpmdev_opts);
3071 qemu_add_opts(&qemu_realtime_opts);
3072 qemu_add_opts(&qemu_msg_opts);
3073 qemu_add_opts(&qemu_name_opts);
3074 qemu_add_opts(&qemu_numa_opts);
3075 qemu_add_opts(&qemu_icount_opts);
3076 qemu_add_opts(&qemu_semihosting_config_opts);
3077 qemu_add_opts(&qemu_fw_cfg_opts);
3079 runstate_init();
3081 if (qcrypto_init(&err) < 0) {
3082 error_report("cannot initialize crypto: %s", error_get_pretty(err));
3083 exit(1);
3085 rtc_clock = QEMU_CLOCK_HOST;
3087 QLIST_INIT (&vm_change_state_head);
3088 os_setup_early_signal_handling();
3090 module_call_init(MODULE_INIT_MACHINE);
3091 machine_class = 0;
3092 optarg = strrchr(argv[0], '/');
3093 if (optarg != 0) {
3094 machine_class = find_machine(optarg + 1);
3096 if (!machine_class) {
3097 machine_class = find_default_machine();
3099 cpu_model = NULL;
3100 snapshot = 0;
3101 cyls = heads = secs = 0;
3102 translation = BIOS_ATA_TRANSLATION_AUTO;
3104 nb_nics = 0;
3106 bdrv_init_with_whitelist();
3108 autostart = 1;
3110 /* first pass of option parsing */
3111 optind = 1;
3112 while (optind < argc) {
3113 if (argv[optind][0] != '-') {
3114 /* disk image */
3115 optind++;
3116 } else {
3117 const QEMUOption *popt;
3119 popt = lookup_opt(argc, argv, &optarg, &optind);
3120 switch (popt->index) {
3121 case QEMU_OPTION_nodefconfig:
3122 defconfig = false;
3123 break;
3124 case QEMU_OPTION_nouserconfig:
3125 userconfig = false;
3126 break;
3131 if (defconfig) {
3132 int ret;
3133 ret = qemu_read_default_config_files(userconfig);
3134 if (ret < 0) {
3135 exit(1);
3139 /* second pass of option parsing */
3140 optind = 1;
3141 for(;;) {
3142 if (optind >= argc)
3143 break;
3144 if (argv[optind][0] != '-') {
3145 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3146 } else {
3147 const QEMUOption *popt;
3149 popt = lookup_opt(argc, argv, &optarg, &optind);
3150 if (!(popt->arch_mask & arch_type)) {
3151 printf("Option %s not supported for this target\n", popt->name);
3152 exit(1);
3154 switch(popt->index) {
3155 case QEMU_OPTION_no_kvm_irqchip: {
3156 olist = qemu_find_opts("machine");
3157 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3158 break;
3160 case QEMU_OPTION_cpu:
3161 /* hw initialization will check this */
3162 cpu_model = optarg;
3163 break;
3164 case QEMU_OPTION_hda:
3166 char buf[256];
3167 if (cyls == 0)
3168 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3169 else
3170 snprintf(buf, sizeof(buf),
3171 "%s,cyls=%d,heads=%d,secs=%d%s",
3172 HD_OPTS , cyls, heads, secs,
3173 translation == BIOS_ATA_TRANSLATION_LBA ?
3174 ",trans=lba" :
3175 translation == BIOS_ATA_TRANSLATION_NONE ?
3176 ",trans=none" : "");
3177 drive_add(IF_DEFAULT, 0, optarg, buf);
3178 break;
3180 case QEMU_OPTION_hdb:
3181 case QEMU_OPTION_hdc:
3182 case QEMU_OPTION_hdd:
3183 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3184 HD_OPTS);
3185 break;
3186 case QEMU_OPTION_drive:
3187 if (drive_def(optarg) == NULL) {
3188 exit(1);
3190 break;
3191 case QEMU_OPTION_set:
3192 if (qemu_set_option(optarg) != 0)
3193 exit(1);
3194 break;
3195 case QEMU_OPTION_global:
3196 if (qemu_global_option(optarg) != 0)
3197 exit(1);
3198 break;
3199 case QEMU_OPTION_mtdblock:
3200 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3201 break;
3202 case QEMU_OPTION_sd:
3203 drive_add(IF_SD, -1, optarg, SD_OPTS);
3204 break;
3205 case QEMU_OPTION_pflash:
3206 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3207 break;
3208 case QEMU_OPTION_snapshot:
3209 snapshot = 1;
3210 break;
3211 case QEMU_OPTION_hdachs:
3213 const char *p;
3214 p = optarg;
3215 cyls = strtol(p, (char **)&p, 0);
3216 if (cyls < 1 || cyls > 16383)
3217 goto chs_fail;
3218 if (*p != ',')
3219 goto chs_fail;
3220 p++;
3221 heads = strtol(p, (char **)&p, 0);
3222 if (heads < 1 || heads > 16)
3223 goto chs_fail;
3224 if (*p != ',')
3225 goto chs_fail;
3226 p++;
3227 secs = strtol(p, (char **)&p, 0);
3228 if (secs < 1 || secs > 63)
3229 goto chs_fail;
3230 if (*p == ',') {
3231 p++;
3232 if (!strcmp(p, "large")) {
3233 translation = BIOS_ATA_TRANSLATION_LARGE;
3234 } else if (!strcmp(p, "rechs")) {
3235 translation = BIOS_ATA_TRANSLATION_RECHS;
3236 } else if (!strcmp(p, "none")) {
3237 translation = BIOS_ATA_TRANSLATION_NONE;
3238 } else if (!strcmp(p, "lba")) {
3239 translation = BIOS_ATA_TRANSLATION_LBA;
3240 } else if (!strcmp(p, "auto")) {
3241 translation = BIOS_ATA_TRANSLATION_AUTO;
3242 } else {
3243 goto chs_fail;
3245 } else if (*p != '\0') {
3246 chs_fail:
3247 error_report("invalid physical CHS format");
3248 exit(1);
3250 if (hda_opts != NULL) {
3251 qemu_opt_set_number(hda_opts, "cyls", cyls,
3252 &error_abort);
3253 qemu_opt_set_number(hda_opts, "heads", heads,
3254 &error_abort);
3255 qemu_opt_set_number(hda_opts, "secs", secs,
3256 &error_abort);
3257 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3258 qemu_opt_set(hda_opts, "trans", "large",
3259 &error_abort);
3260 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3261 qemu_opt_set(hda_opts, "trans", "rechs",
3262 &error_abort);
3263 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3264 qemu_opt_set(hda_opts, "trans", "lba",
3265 &error_abort);
3266 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3267 qemu_opt_set(hda_opts, "trans", "none",
3268 &error_abort);
3272 break;
3273 case QEMU_OPTION_numa:
3274 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3275 optarg, true);
3276 if (!opts) {
3277 exit(1);
3279 break;
3280 case QEMU_OPTION_display:
3281 display_type = select_display(optarg);
3282 break;
3283 case QEMU_OPTION_nographic:
3284 display_type = DT_NOGRAPHIC;
3285 break;
3286 case QEMU_OPTION_curses:
3287 #ifdef CONFIG_CURSES
3288 display_type = DT_CURSES;
3289 #else
3290 error_report("curses support is disabled");
3291 exit(1);
3292 #endif
3293 break;
3294 case QEMU_OPTION_portrait:
3295 graphic_rotate = 90;
3296 break;
3297 case QEMU_OPTION_rotate:
3298 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3299 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3300 graphic_rotate != 180 && graphic_rotate != 270) {
3301 error_report("only 90, 180, 270 deg rotation is available");
3302 exit(1);
3304 break;
3305 case QEMU_OPTION_kernel:
3306 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3307 &error_abort);
3308 break;
3309 case QEMU_OPTION_initrd:
3310 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3311 &error_abort);
3312 break;
3313 case QEMU_OPTION_append:
3314 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3315 &error_abort);
3316 break;
3317 case QEMU_OPTION_dtb:
3318 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3319 &error_abort);
3320 break;
3321 case QEMU_OPTION_cdrom:
3322 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3323 break;
3324 case QEMU_OPTION_boot:
3325 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3326 optarg, true);
3327 if (!opts) {
3328 exit(1);
3330 break;
3331 case QEMU_OPTION_fda:
3332 case QEMU_OPTION_fdb:
3333 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3334 optarg, FD_OPTS);
3335 break;
3336 case QEMU_OPTION_no_fd_bootchk:
3337 fd_bootchk = 0;
3338 break;
3339 case QEMU_OPTION_netdev:
3340 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3341 exit(1);
3343 break;
3344 case QEMU_OPTION_net:
3345 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3346 exit(1);
3348 break;
3349 #ifdef CONFIG_LIBISCSI
3350 case QEMU_OPTION_iscsi:
3351 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3352 optarg, false);
3353 if (!opts) {
3354 exit(1);
3356 break;
3357 #endif
3358 #ifdef CONFIG_SLIRP
3359 case QEMU_OPTION_tftp:
3360 legacy_tftp_prefix = optarg;
3361 break;
3362 case QEMU_OPTION_bootp:
3363 legacy_bootp_filename = optarg;
3364 break;
3365 case QEMU_OPTION_redir:
3366 if (net_slirp_redir(optarg) < 0)
3367 exit(1);
3368 break;
3369 #endif
3370 case QEMU_OPTION_bt:
3371 add_device_config(DEV_BT, optarg);
3372 break;
3373 case QEMU_OPTION_audio_help:
3374 AUD_help ();
3375 exit (0);
3376 break;
3377 case QEMU_OPTION_soundhw:
3378 select_soundhw (optarg);
3379 break;
3380 case QEMU_OPTION_h:
3381 help(0);
3382 break;
3383 case QEMU_OPTION_version:
3384 version();
3385 exit(0);
3386 break;
3387 case QEMU_OPTION_m:
3388 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3389 optarg, true);
3390 if (!opts) {
3391 exit(EXIT_FAILURE);
3393 break;
3394 #ifdef CONFIG_TPM
3395 case QEMU_OPTION_tpmdev:
3396 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3397 exit(1);
3399 break;
3400 #endif
3401 case QEMU_OPTION_mempath:
3402 mem_path = optarg;
3403 break;
3404 case QEMU_OPTION_mem_prealloc:
3405 mem_prealloc = 1;
3406 break;
3407 case QEMU_OPTION_d:
3408 log_mask = optarg;
3409 break;
3410 case QEMU_OPTION_D:
3411 log_file = optarg;
3412 break;
3413 case QEMU_OPTION_s:
3414 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3415 break;
3416 case QEMU_OPTION_gdb:
3417 add_device_config(DEV_GDB, optarg);
3418 break;
3419 case QEMU_OPTION_L:
3420 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3421 data_dir[data_dir_idx++] = optarg;
3423 break;
3424 case QEMU_OPTION_bios:
3425 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3426 &error_abort);
3427 break;
3428 case QEMU_OPTION_singlestep:
3429 singlestep = 1;
3430 break;
3431 case QEMU_OPTION_S:
3432 autostart = 0;
3433 break;
3434 case QEMU_OPTION_k:
3435 keyboard_layout = optarg;
3436 break;
3437 case QEMU_OPTION_localtime:
3438 rtc_utc = 0;
3439 break;
3440 case QEMU_OPTION_vga:
3441 vga_model = optarg;
3442 default_vga = 0;
3443 break;
3444 case QEMU_OPTION_g:
3446 const char *p;
3447 int w, h, depth;
3448 p = optarg;
3449 w = strtol(p, (char **)&p, 10);
3450 if (w <= 0) {
3451 graphic_error:
3452 error_report("invalid resolution or depth");
3453 exit(1);
3455 if (*p != 'x')
3456 goto graphic_error;
3457 p++;
3458 h = strtol(p, (char **)&p, 10);
3459 if (h <= 0)
3460 goto graphic_error;
3461 if (*p == 'x') {
3462 p++;
3463 depth = strtol(p, (char **)&p, 10);
3464 if (depth != 8 && depth != 15 && depth != 16 &&
3465 depth != 24 && depth != 32)
3466 goto graphic_error;
3467 } else if (*p == '\0') {
3468 depth = graphic_depth;
3469 } else {
3470 goto graphic_error;
3473 graphic_width = w;
3474 graphic_height = h;
3475 graphic_depth = depth;
3477 break;
3478 case QEMU_OPTION_echr:
3480 char *r;
3481 term_escape_char = strtol(optarg, &r, 0);
3482 if (r == optarg)
3483 printf("Bad argument to echr\n");
3484 break;
3486 case QEMU_OPTION_monitor:
3487 default_monitor = 0;
3488 if (strncmp(optarg, "none", 4)) {
3489 monitor_parse(optarg, "readline", false);
3491 break;
3492 case QEMU_OPTION_qmp:
3493 monitor_parse(optarg, "control", false);
3494 default_monitor = 0;
3495 break;
3496 case QEMU_OPTION_qmp_pretty:
3497 monitor_parse(optarg, "control", true);
3498 default_monitor = 0;
3499 break;
3500 case QEMU_OPTION_mon:
3501 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3502 true);
3503 if (!opts) {
3504 exit(1);
3506 default_monitor = 0;
3507 break;
3508 case QEMU_OPTION_chardev:
3509 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3510 optarg, true);
3511 if (!opts) {
3512 exit(1);
3514 break;
3515 case QEMU_OPTION_fsdev:
3516 olist = qemu_find_opts("fsdev");
3517 if (!olist) {
3518 error_report("fsdev support is disabled");
3519 exit(1);
3521 opts = qemu_opts_parse_noisily(olist, optarg, true);
3522 if (!opts) {
3523 exit(1);
3525 break;
3526 case QEMU_OPTION_virtfs: {
3527 QemuOpts *fsdev;
3528 QemuOpts *device;
3529 const char *writeout, *sock_fd, *socket;
3531 olist = qemu_find_opts("virtfs");
3532 if (!olist) {
3533 error_report("virtfs support is disabled");
3534 exit(1);
3536 opts = qemu_opts_parse_noisily(olist, optarg, true);
3537 if (!opts) {
3538 exit(1);
3541 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3542 qemu_opt_get(opts, "mount_tag") == NULL) {
3543 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3544 exit(1);
3546 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3547 qemu_opt_get(opts, "mount_tag"),
3548 1, NULL);
3549 if (!fsdev) {
3550 error_report("duplicate fsdev id: %s",
3551 qemu_opt_get(opts, "mount_tag"));
3552 exit(1);
3555 writeout = qemu_opt_get(opts, "writeout");
3556 if (writeout) {
3557 #ifdef CONFIG_SYNC_FILE_RANGE
3558 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3559 #else
3560 error_report("writeout=immediate not supported "
3561 "on this platform");
3562 exit(1);
3563 #endif
3565 qemu_opt_set(fsdev, "fsdriver",
3566 qemu_opt_get(opts, "fsdriver"), &error_abort);
3567 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3568 &error_abort);
3569 qemu_opt_set(fsdev, "security_model",
3570 qemu_opt_get(opts, "security_model"),
3571 &error_abort);
3572 socket = qemu_opt_get(opts, "socket");
3573 if (socket) {
3574 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3576 sock_fd = qemu_opt_get(opts, "sock_fd");
3577 if (sock_fd) {
3578 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3581 qemu_opt_set_bool(fsdev, "readonly",
3582 qemu_opt_get_bool(opts, "readonly", 0),
3583 &error_abort);
3584 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3585 &error_abort);
3586 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3587 qemu_opt_set(device, "fsdev",
3588 qemu_opt_get(opts, "mount_tag"), &error_abort);
3589 qemu_opt_set(device, "mount_tag",
3590 qemu_opt_get(opts, "mount_tag"), &error_abort);
3591 break;
3593 case QEMU_OPTION_virtfs_synth: {
3594 QemuOpts *fsdev;
3595 QemuOpts *device;
3597 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3598 1, NULL);
3599 if (!fsdev) {
3600 error_report("duplicate option: %s", "virtfs_synth");
3601 exit(1);
3603 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3605 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3606 &error_abort);
3607 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3608 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3609 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3610 break;
3612 case QEMU_OPTION_serial:
3613 add_device_config(DEV_SERIAL, optarg);
3614 default_serial = 0;
3615 if (strncmp(optarg, "mon:", 4) == 0) {
3616 default_monitor = 0;
3618 break;
3619 case QEMU_OPTION_watchdog:
3620 if (watchdog) {
3621 error_report("only one watchdog option may be given");
3622 return 1;
3624 watchdog = optarg;
3625 break;
3626 case QEMU_OPTION_watchdog_action:
3627 if (select_watchdog_action(optarg) == -1) {
3628 error_report("unknown -watchdog-action parameter");
3629 exit(1);
3631 break;
3632 case QEMU_OPTION_virtiocon:
3633 add_device_config(DEV_VIRTCON, optarg);
3634 default_virtcon = 0;
3635 if (strncmp(optarg, "mon:", 4) == 0) {
3636 default_monitor = 0;
3638 break;
3639 case QEMU_OPTION_parallel:
3640 add_device_config(DEV_PARALLEL, optarg);
3641 default_parallel = 0;
3642 if (strncmp(optarg, "mon:", 4) == 0) {
3643 default_monitor = 0;
3645 break;
3646 case QEMU_OPTION_debugcon:
3647 add_device_config(DEV_DEBUGCON, optarg);
3648 break;
3649 case QEMU_OPTION_loadvm:
3650 loadvm = optarg;
3651 break;
3652 case QEMU_OPTION_full_screen:
3653 full_screen = 1;
3654 break;
3655 case QEMU_OPTION_no_frame:
3656 no_frame = 1;
3657 break;
3658 case QEMU_OPTION_alt_grab:
3659 alt_grab = 1;
3660 break;
3661 case QEMU_OPTION_ctrl_grab:
3662 ctrl_grab = 1;
3663 break;
3664 case QEMU_OPTION_no_quit:
3665 no_quit = 1;
3666 break;
3667 case QEMU_OPTION_sdl:
3668 #ifdef CONFIG_SDL
3669 display_type = DT_SDL;
3670 break;
3671 #else
3672 error_report("SDL support is disabled");
3673 exit(1);
3674 #endif
3675 case QEMU_OPTION_pidfile:
3676 pid_file = optarg;
3677 break;
3678 case QEMU_OPTION_win2k_hack:
3679 win2k_install_hack = 1;
3680 break;
3681 case QEMU_OPTION_rtc_td_hack: {
3682 static GlobalProperty slew_lost_ticks[] = {
3684 .driver = "mc146818rtc",
3685 .property = "lost_tick_policy",
3686 .value = "slew",
3688 { /* end of list */ }
3691 qdev_prop_register_global_list(slew_lost_ticks);
3692 break;
3694 case QEMU_OPTION_acpitable:
3695 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3696 optarg, true);
3697 if (!opts) {
3698 exit(1);
3700 do_acpitable_option(opts);
3701 break;
3702 case QEMU_OPTION_smbios:
3703 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3704 optarg, false);
3705 if (!opts) {
3706 exit(1);
3708 do_smbios_option(opts);
3709 break;
3710 case QEMU_OPTION_fwcfg:
3711 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3712 optarg, true);
3713 if (opts == NULL) {
3714 exit(1);
3716 break;
3717 case QEMU_OPTION_enable_kvm:
3718 olist = qemu_find_opts("machine");
3719 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3720 break;
3721 case QEMU_OPTION_M:
3722 case QEMU_OPTION_machine:
3723 olist = qemu_find_opts("machine");
3724 opts = qemu_opts_parse_noisily(olist, optarg, true);
3725 if (!opts) {
3726 exit(1);
3728 break;
3729 case QEMU_OPTION_no_kvm:
3730 olist = qemu_find_opts("machine");
3731 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3732 break;
3733 case QEMU_OPTION_no_kvm_pit: {
3734 error_report("warning: ignoring deprecated option");
3735 break;
3737 case QEMU_OPTION_no_kvm_pit_reinjection: {
3738 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3740 .driver = "kvm-pit",
3741 .property = "lost_tick_policy",
3742 .value = "discard",
3744 { /* end of list */ }
3747 error_report("warning: deprecated, replaced by "
3748 "-global kvm-pit.lost_tick_policy=discard");
3749 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3750 break;
3752 case QEMU_OPTION_usb:
3753 olist = qemu_find_opts("machine");
3754 qemu_opts_parse_noisily(olist, "usb=on", false);
3755 break;
3756 case QEMU_OPTION_usbdevice:
3757 olist = qemu_find_opts("machine");
3758 qemu_opts_parse_noisily(olist, "usb=on", false);
3759 add_device_config(DEV_USB, optarg);
3760 break;
3761 case QEMU_OPTION_device:
3762 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3763 optarg, true)) {
3764 exit(1);
3766 break;
3767 case QEMU_OPTION_smp:
3768 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3769 optarg, true)) {
3770 exit(1);
3772 break;
3773 case QEMU_OPTION_vnc:
3775 #ifdef CONFIG_VNC
3776 Error *local_err = NULL;
3778 if (vnc_parse(optarg, &local_err) == NULL) {
3779 error_report_err(local_err);
3780 exit(1);
3782 #else
3783 error_report("VNC support is disabled");
3784 exit(1);
3785 #endif
3786 break;
3788 case QEMU_OPTION_no_acpi:
3789 acpi_enabled = 0;
3790 break;
3791 case QEMU_OPTION_no_hpet:
3792 no_hpet = 1;
3793 break;
3794 case QEMU_OPTION_balloon:
3795 if (balloon_parse(optarg) < 0) {
3796 error_report("unknown -balloon argument %s", optarg);
3797 exit(1);
3799 break;
3800 case QEMU_OPTION_no_reboot:
3801 no_reboot = 1;
3802 break;
3803 case QEMU_OPTION_no_shutdown:
3804 no_shutdown = 1;
3805 break;
3806 case QEMU_OPTION_show_cursor:
3807 cursor_hide = 0;
3808 break;
3809 case QEMU_OPTION_uuid:
3810 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3811 error_report("failed to parse UUID string: wrong format");
3812 exit(1);
3814 qemu_uuid_set = true;
3815 break;
3816 case QEMU_OPTION_option_rom:
3817 if (nb_option_roms >= MAX_OPTION_ROMS) {
3818 error_report("too many option ROMs");
3819 exit(1);
3821 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3822 optarg, true);
3823 if (!opts) {
3824 exit(1);
3826 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3827 option_rom[nb_option_roms].bootindex =
3828 qemu_opt_get_number(opts, "bootindex", -1);
3829 if (!option_rom[nb_option_roms].name) {
3830 error_report("Option ROM file is not specified");
3831 exit(1);
3833 nb_option_roms++;
3834 break;
3835 case QEMU_OPTION_semihosting:
3836 semihosting.enabled = true;
3837 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3838 break;
3839 case QEMU_OPTION_semihosting_config:
3840 semihosting.enabled = true;
3841 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3842 optarg, false);
3843 if (opts != NULL) {
3844 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3845 true);
3846 const char *target = qemu_opt_get(opts, "target");
3847 if (target != NULL) {
3848 if (strcmp("native", target) == 0) {
3849 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3850 } else if (strcmp("gdb", target) == 0) {
3851 semihosting.target = SEMIHOSTING_TARGET_GDB;
3852 } else if (strcmp("auto", target) == 0) {
3853 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3854 } else {
3855 error_report("unsupported semihosting-config %s",
3856 optarg);
3857 exit(1);
3859 } else {
3860 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3862 /* Set semihosting argument count and vector */
3863 qemu_opt_foreach(opts, add_semihosting_arg,
3864 &semihosting, NULL);
3865 } else {
3866 error_report("unsupported semihosting-config %s", optarg);
3867 exit(1);
3869 break;
3870 case QEMU_OPTION_tdf:
3871 error_report("warning: ignoring deprecated option");
3872 break;
3873 case QEMU_OPTION_name:
3874 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3875 optarg, true);
3876 if (!opts) {
3877 exit(1);
3879 break;
3880 case QEMU_OPTION_prom_env:
3881 if (nb_prom_envs >= MAX_PROM_ENVS) {
3882 error_report("too many prom variables");
3883 exit(1);
3885 prom_envs[nb_prom_envs] = optarg;
3886 nb_prom_envs++;
3887 break;
3888 case QEMU_OPTION_old_param:
3889 old_param = 1;
3890 break;
3891 case QEMU_OPTION_clock:
3892 /* Clock options no longer exist. Keep this option for
3893 * backward compatibility.
3895 break;
3896 case QEMU_OPTION_startdate:
3897 configure_rtc_date_offset(optarg, 1);
3898 break;
3899 case QEMU_OPTION_rtc:
3900 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3901 false);
3902 if (!opts) {
3903 exit(1);
3905 configure_rtc(opts);
3906 break;
3907 case QEMU_OPTION_tb_size:
3908 tcg_tb_size = strtol(optarg, NULL, 0);
3909 if (tcg_tb_size < 0) {
3910 tcg_tb_size = 0;
3912 break;
3913 case QEMU_OPTION_icount:
3914 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3915 optarg, true);
3916 if (!icount_opts) {
3917 exit(1);
3919 break;
3920 case QEMU_OPTION_incoming:
3921 if (!incoming) {
3922 runstate_set(RUN_STATE_INMIGRATE);
3924 incoming = optarg;
3925 break;
3926 case QEMU_OPTION_nodefaults:
3927 has_defaults = 0;
3928 break;
3929 case QEMU_OPTION_xen_domid:
3930 if (!(xen_available())) {
3931 printf("Option %s not supported for this target\n", popt->name);
3932 exit(1);
3934 xen_domid = atoi(optarg);
3935 break;
3936 case QEMU_OPTION_xen_create:
3937 if (!(xen_available())) {
3938 printf("Option %s not supported for this target\n", popt->name);
3939 exit(1);
3941 xen_mode = XEN_CREATE;
3942 break;
3943 case QEMU_OPTION_xen_attach:
3944 if (!(xen_available())) {
3945 printf("Option %s not supported for this target\n", popt->name);
3946 exit(1);
3948 xen_mode = XEN_ATTACH;
3949 break;
3950 case QEMU_OPTION_trace:
3952 opts = qemu_opts_parse_noisily(qemu_find_opts("trace"),
3953 optarg, false);
3954 if (!opts) {
3955 exit(1);
3957 trace_events = qemu_opt_get(opts, "events");
3958 trace_file = qemu_opt_get(opts, "file");
3959 break;
3961 case QEMU_OPTION_trace_unassigned:
3962 trace_unassigned = true;
3963 break;
3964 case QEMU_OPTION_readconfig:
3966 int ret = qemu_read_config_file(optarg);
3967 if (ret < 0) {
3968 error_report("read config %s: %s", optarg,
3969 strerror(-ret));
3970 exit(1);
3972 break;
3974 case QEMU_OPTION_spice:
3975 olist = qemu_find_opts("spice");
3976 if (!olist) {
3977 error_report("spice support is disabled");
3978 exit(1);
3980 opts = qemu_opts_parse_noisily(olist, optarg, false);
3981 if (!opts) {
3982 exit(1);
3984 display_remote++;
3985 break;
3986 case QEMU_OPTION_writeconfig:
3988 FILE *fp;
3989 if (strcmp(optarg, "-") == 0) {
3990 fp = stdout;
3991 } else {
3992 fp = fopen(optarg, "w");
3993 if (fp == NULL) {
3994 error_report("open %s: %s", optarg,
3995 strerror(errno));
3996 exit(1);
3999 qemu_config_write(fp);
4000 if (fp != stdout) {
4001 fclose(fp);
4003 break;
4005 case QEMU_OPTION_qtest:
4006 qtest_chrdev = optarg;
4007 break;
4008 case QEMU_OPTION_qtest_log:
4009 qtest_log = optarg;
4010 break;
4011 case QEMU_OPTION_sandbox:
4012 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
4013 optarg, true);
4014 if (!opts) {
4015 exit(1);
4017 break;
4018 case QEMU_OPTION_add_fd:
4019 #ifndef _WIN32
4020 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
4021 optarg, false);
4022 if (!opts) {
4023 exit(1);
4025 #else
4026 error_report("File descriptor passing is disabled on this "
4027 "platform");
4028 exit(1);
4029 #endif
4030 break;
4031 case QEMU_OPTION_object:
4032 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
4033 optarg, true);
4034 if (!opts) {
4035 exit(1);
4037 break;
4038 case QEMU_OPTION_realtime:
4039 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
4040 optarg, false);
4041 if (!opts) {
4042 exit(1);
4044 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
4045 break;
4046 case QEMU_OPTION_msg:
4047 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
4048 false);
4049 if (!opts) {
4050 exit(1);
4052 configure_msg(opts);
4053 break;
4054 case QEMU_OPTION_dump_vmstate:
4055 if (vmstate_dump_file) {
4056 error_report("only one '-dump-vmstate' "
4057 "option may be given");
4058 exit(1);
4060 vmstate_dump_file = fopen(optarg, "w");
4061 if (vmstate_dump_file == NULL) {
4062 error_report("open %s: %s", optarg, strerror(errno));
4063 exit(1);
4065 break;
4066 default:
4067 os_parse_cmd_args(popt->index, optarg);
4072 replay_configure(icount_opts);
4074 opts = qemu_get_machine_opts();
4075 optarg = qemu_opt_get(opts, "type");
4076 if (optarg) {
4077 machine_class = machine_parse(optarg);
4080 if (machine_class == NULL) {
4081 error_report("No machine specified, and there is no default");
4082 error_printf("Use -machine help to list supported machines\n");
4083 exit(1);
4086 set_memory_options(&ram_slots, &maxram_size, machine_class);
4088 loc_set_none();
4090 os_daemonize();
4092 if (qemu_init_main_loop(&main_loop_err)) {
4093 error_report_err(main_loop_err);
4094 exit(1);
4097 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4098 parse_sandbox, NULL, NULL)) {
4099 exit(1);
4102 if (qemu_opts_foreach(qemu_find_opts("name"),
4103 parse_name, NULL, NULL)) {
4104 exit(1);
4107 #ifndef _WIN32
4108 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4109 parse_add_fd, NULL, NULL)) {
4110 exit(1);
4113 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4114 cleanup_add_fd, NULL, NULL)) {
4115 exit(1);
4117 #endif
4119 current_machine = MACHINE(object_new(object_class_get_name(
4120 OBJECT_CLASS(machine_class))));
4121 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4122 exit(0);
4124 object_property_add_child(object_get_root(), "machine",
4125 OBJECT(current_machine), &error_abort);
4126 cpu_exec_init_all();
4128 if (machine_class->hw_version) {
4129 qemu_set_hw_version(machine_class->hw_version);
4132 /* Init CPU def lists, based on config
4133 * - Must be called after all the qemu_read_config_file() calls
4134 * - Must be called before list_cpus()
4135 * - Must be called before machine->init()
4137 cpudef_init();
4139 if (cpu_model && is_help_option(cpu_model)) {
4140 list_cpus(stdout, &fprintf, cpu_model);
4141 exit(0);
4144 /* Open the logfile at this point and set the log mask if necessary.
4146 if (log_file) {
4147 qemu_set_log_filename(log_file);
4150 if (log_mask) {
4151 int mask;
4152 mask = qemu_str_to_log_mask(log_mask);
4153 if (!mask) {
4154 qemu_print_log_usage(stdout);
4155 exit(1);
4157 qemu_set_log(mask);
4160 if (!is_daemonized()) {
4161 if (!trace_init_backends(trace_events, trace_file)) {
4162 exit(1);
4166 /* If no data_dir is specified then try to find it relative to the
4167 executable path. */
4168 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4169 data_dir[data_dir_idx] = os_find_datadir();
4170 if (data_dir[data_dir_idx] != NULL) {
4171 data_dir_idx++;
4174 /* If all else fails use the install path specified when building. */
4175 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4176 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4179 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4181 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4182 if (max_cpus > machine_class->max_cpus) {
4183 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4184 "supported by machine '%s' (%d)", max_cpus,
4185 machine_class->name, machine_class->max_cpus);
4186 exit(1);
4190 * Get the default machine options from the machine if it is not already
4191 * specified either by the configuration file or by the command line.
4193 if (machine_class->default_machine_opts) {
4194 qemu_opts_set_defaults(qemu_find_opts("machine"),
4195 machine_class->default_machine_opts, 0);
4198 qemu_opts_foreach(qemu_find_opts("device"),
4199 default_driver_check, NULL, NULL);
4200 qemu_opts_foreach(qemu_find_opts("global"),
4201 default_driver_check, NULL, NULL);
4203 if (!vga_model && !default_vga) {
4204 vga_interface_type = VGA_DEVICE;
4206 if (!has_defaults || machine_class->no_serial) {
4207 default_serial = 0;
4209 if (!has_defaults || machine_class->no_parallel) {
4210 default_parallel = 0;
4212 if (!has_defaults || !machine_class->use_virtcon) {
4213 default_virtcon = 0;
4215 if (!has_defaults || !machine_class->use_sclp) {
4216 default_sclp = 0;
4218 if (!has_defaults || machine_class->no_floppy) {
4219 default_floppy = 0;
4221 if (!has_defaults || machine_class->no_cdrom) {
4222 default_cdrom = 0;
4224 if (!has_defaults || machine_class->no_sdcard) {
4225 default_sdcard = 0;
4227 if (!has_defaults) {
4228 default_monitor = 0;
4229 default_net = 0;
4230 default_vga = 0;
4233 if (is_daemonized()) {
4234 /* According to documentation and historically, -nographic redirects
4235 * serial port, parallel port and monitor to stdio, which does not work
4236 * with -daemonize. We can redirect these to null instead, but since
4237 * -nographic is legacy, let's just error out.
4238 * We disallow -nographic only if all other ports are not redirected
4239 * explicitly, to not break existing legacy setups which uses
4240 * -nographic _and_ redirects all ports explicitly - this is valid
4241 * usage, -nographic is just a no-op in this case.
4243 if (display_type == DT_NOGRAPHIC
4244 && (default_parallel || default_serial
4245 || default_monitor || default_virtcon)) {
4246 error_report("-nographic cannot be used with -daemonize");
4247 exit(1);
4249 #ifdef CONFIG_CURSES
4250 if (display_type == DT_CURSES) {
4251 error_report("curses display cannot be used with -daemonize");
4252 exit(1);
4254 #endif
4257 if (display_type == DT_NOGRAPHIC) {
4258 if (default_parallel)
4259 add_device_config(DEV_PARALLEL, "null");
4260 if (default_serial && default_monitor) {
4261 add_device_config(DEV_SERIAL, "mon:stdio");
4262 } else if (default_virtcon && default_monitor) {
4263 add_device_config(DEV_VIRTCON, "mon:stdio");
4264 } else if (default_sclp && default_monitor) {
4265 add_device_config(DEV_SCLP, "mon:stdio");
4266 } else {
4267 if (default_serial)
4268 add_device_config(DEV_SERIAL, "stdio");
4269 if (default_virtcon)
4270 add_device_config(DEV_VIRTCON, "stdio");
4271 if (default_sclp) {
4272 add_device_config(DEV_SCLP, "stdio");
4274 if (default_monitor)
4275 monitor_parse("stdio", "readline", false);
4277 } else {
4278 if (default_serial)
4279 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4280 if (default_parallel)
4281 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4282 if (default_monitor)
4283 monitor_parse("vc:80Cx24C", "readline", false);
4284 if (default_virtcon)
4285 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4286 if (default_sclp) {
4287 add_device_config(DEV_SCLP, "vc:80Cx24C");
4291 #if defined(CONFIG_VNC)
4292 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4293 display_remote++;
4295 #endif
4296 if (display_type == DT_DEFAULT && !display_remote) {
4297 #if defined(CONFIG_GTK)
4298 display_type = DT_GTK;
4299 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4300 display_type = DT_SDL;
4301 #elif defined(CONFIG_VNC)
4302 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4303 show_vnc_port = 1;
4304 #else
4305 display_type = DT_NONE;
4306 #endif
4309 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4310 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4311 "for SDL, ignoring option");
4313 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4314 error_report("-no-quit is only valid for GTK and SDL, "
4315 "ignoring option");
4318 #if defined(CONFIG_GTK)
4319 if (display_type == DT_GTK) {
4320 early_gtk_display_init(request_opengl);
4322 #endif
4323 #if defined(CONFIG_SDL)
4324 if (display_type == DT_SDL) {
4325 sdl_display_early_init(request_opengl);
4327 #endif
4328 if (request_opengl == 1 && display_opengl == 0) {
4329 #if defined(CONFIG_OPENGL)
4330 error_report("OpenGL is not supported by the display");
4331 #else
4332 error_report("OpenGL support is disabled");
4333 #endif
4334 exit(1);
4337 page_size_init();
4338 socket_init();
4340 if (qemu_opts_foreach(qemu_find_opts("object"),
4341 object_create,
4342 object_create_initial, NULL)) {
4343 exit(1);
4346 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4347 chardev_init_func, NULL, NULL)) {
4348 exit(1);
4351 #ifdef CONFIG_VIRTFS
4352 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4353 fsdev_init_func, NULL, NULL)) {
4354 exit(1);
4356 #endif
4358 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4359 error_report("could not acquire pid file: %s", strerror(errno));
4360 exit(1);
4363 if (qemu_opts_foreach(qemu_find_opts("device"),
4364 device_help_func, NULL, NULL)) {
4365 exit(0);
4368 machine_opts = qemu_get_machine_opts();
4369 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4370 NULL)) {
4371 object_unref(OBJECT(current_machine));
4372 exit(1);
4375 configure_accelerator(current_machine);
4377 if (qtest_chrdev) {
4378 Error *local_err = NULL;
4379 qtest_init(qtest_chrdev, qtest_log, &local_err);
4380 if (local_err) {
4381 error_report_err(local_err);
4382 exit(1);
4386 machine_opts = qemu_get_machine_opts();
4387 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4388 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4389 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4390 bios_name = qemu_opt_get(machine_opts, "firmware");
4392 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4393 if (opts) {
4394 Error *local_err = NULL;
4396 boot_order = qemu_opt_get(opts, "order");
4397 if (boot_order) {
4398 validate_bootdevices(boot_order, &local_err);
4399 if (local_err) {
4400 error_report_err(local_err);
4401 exit(1);
4405 boot_once = qemu_opt_get(opts, "once");
4406 if (boot_once) {
4407 validate_bootdevices(boot_once, &local_err);
4408 if (local_err) {
4409 error_report_err(local_err);
4410 exit(1);
4414 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4415 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4418 if (!boot_order) {
4419 boot_order = machine_class->default_boot_order;
4422 if (!kernel_cmdline) {
4423 kernel_cmdline = "";
4424 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4427 linux_boot = (kernel_filename != NULL);
4429 if (!linux_boot && *kernel_cmdline != '\0') {
4430 error_report("-append only allowed with -kernel option");
4431 exit(1);
4434 if (!linux_boot && initrd_filename != NULL) {
4435 error_report("-initrd only allowed with -kernel option");
4436 exit(1);
4439 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4440 error_report("-dtb only allowed with -kernel option");
4441 exit(1);
4444 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4445 /* fall back to the -kernel/-append */
4446 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4449 os_set_line_buffering();
4451 #ifdef CONFIG_SPICE
4452 /* spice needs the timers to be initialized by this point */
4453 qemu_spice_init();
4454 #endif
4456 cpu_ticks_init();
4457 if (icount_opts) {
4458 if (kvm_enabled() || xen_enabled()) {
4459 error_report("-icount is not allowed with kvm or xen");
4460 exit(1);
4462 configure_icount(icount_opts, &error_abort);
4463 qemu_opts_del(icount_opts);
4466 /* clean up network at qemu process termination */
4467 atexit(&net_cleanup);
4469 if (net_init_clients() < 0) {
4470 exit(1);
4473 if (qemu_opts_foreach(qemu_find_opts("object"),
4474 object_create,
4475 object_create_delayed, NULL)) {
4476 exit(1);
4479 #ifdef CONFIG_TPM
4480 if (tpm_init() < 0) {
4481 exit(1);
4483 #endif
4485 /* init the bluetooth world */
4486 if (foreach_device_config(DEV_BT, bt_parse))
4487 exit(1);
4489 if (!xen_enabled()) {
4490 /* On 32-bit hosts, QEMU is limited by virtual address space */
4491 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4492 error_report("at most 2047 MB RAM can be simulated");
4493 exit(1);
4497 blk_mig_init();
4498 ram_mig_init();
4500 /* If the currently selected machine wishes to override the units-per-bus
4501 * property of its default HBA interface type, do so now. */
4502 if (machine_class->units_per_default_bus) {
4503 override_max_devs(machine_class->block_default_type,
4504 machine_class->units_per_default_bus);
4507 /* open the virtual block devices */
4508 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4509 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4510 NULL, NULL);
4512 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4513 &machine_class->block_default_type, NULL)) {
4514 exit(1);
4517 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4518 CDROM_OPTS);
4519 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4520 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4522 parse_numa_opts(machine_class);
4524 if (qemu_opts_foreach(qemu_find_opts("mon"),
4525 mon_init_func, NULL, NULL)) {
4526 exit(1);
4529 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4530 exit(1);
4531 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4532 exit(1);
4533 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4534 exit(1);
4535 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4536 exit(1);
4538 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4539 exit(1);
4541 /* If no default VGA is requested, the default is "none". */
4542 if (default_vga) {
4543 if (machine_class->default_display) {
4544 vga_model = machine_class->default_display;
4545 } else if (cirrus_vga_available()) {
4546 vga_model = "cirrus";
4547 } else if (vga_available()) {
4548 vga_model = "std";
4551 if (vga_model) {
4552 select_vgahw(vga_model);
4555 if (watchdog) {
4556 i = select_watchdog(watchdog);
4557 if (i > 0)
4558 exit (i == 1 ? 1 : 0);
4561 if (machine_class->compat_props) {
4562 qdev_prop_register_global_list(machine_class->compat_props);
4564 qemu_add_globals();
4566 /* This checkpoint is required by replay to separate prior clock
4567 reading from the other reads, because timer polling functions query
4568 clock values from the log. */
4569 replay_checkpoint(CHECKPOINT_INIT);
4570 qdev_machine_init();
4572 current_machine->ram_size = ram_size;
4573 current_machine->maxram_size = maxram_size;
4574 current_machine->ram_slots = ram_slots;
4575 current_machine->boot_order = boot_order;
4576 current_machine->cpu_model = cpu_model;
4578 machine_class->init(current_machine);
4580 realtime_init();
4582 audio_init();
4584 cpu_synchronize_all_post_init();
4586 numa_post_machine_init();
4588 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4589 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4590 exit(1);
4593 /* init USB devices */
4594 if (usb_enabled()) {
4595 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4596 exit(1);
4599 /* Check if IGD GFX passthrough. */
4600 igd_gfx_passthru();
4602 /* init generic devices */
4603 if (qemu_opts_foreach(qemu_find_opts("device"),
4604 device_init_func, NULL, NULL)) {
4605 exit(1);
4608 /* Did we create any drives that we failed to create a device for? */
4609 drive_check_orphaned();
4611 net_check_clients();
4613 if (boot_once) {
4614 Error *local_err = NULL;
4615 qemu_boot_set(boot_once, &local_err);
4616 if (local_err) {
4617 error_report("%s", error_get_pretty(local_err));
4618 exit(1);
4620 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4623 ds = init_displaystate();
4625 /* init local displays */
4626 switch (display_type) {
4627 case DT_NOGRAPHIC:
4628 (void)ds; /* avoid warning if no display is configured */
4629 break;
4630 #if defined(CONFIG_CURSES)
4631 case DT_CURSES:
4632 curses_display_init(ds, full_screen);
4633 break;
4634 #endif
4635 #if defined(CONFIG_SDL)
4636 case DT_SDL:
4637 sdl_display_init(ds, full_screen, no_frame);
4638 break;
4639 #elif defined(CONFIG_COCOA)
4640 case DT_SDL:
4641 cocoa_display_init(ds, full_screen);
4642 break;
4643 #endif
4644 #if defined(CONFIG_GTK)
4645 case DT_GTK:
4646 gtk_display_init(ds, full_screen, grab_on_hover);
4647 break;
4648 #endif
4649 default:
4650 break;
4653 /* must be after terminal init, SDL library changes signal handlers */
4654 os_setup_signal_handling();
4656 #ifdef CONFIG_VNC
4657 /* init remote displays */
4658 qemu_opts_foreach(qemu_find_opts("vnc"),
4659 vnc_init_func, NULL, NULL);
4660 if (show_vnc_port) {
4661 char *ret = vnc_display_local_addr("default");
4662 printf("VNC server running on '%s'\n", ret);
4663 g_free(ret);
4665 #endif
4666 #ifdef CONFIG_SPICE
4667 if (using_spice) {
4668 qemu_spice_display_init();
4670 #endif
4672 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4673 exit(1);
4676 qdev_machine_creation_done();
4678 /* TODO: once all bus devices are qdevified, this should be done
4679 * when bus is created by qdev.c */
4680 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4681 qemu_run_machine_init_done_notifiers();
4683 if (rom_check_and_register_reset() != 0) {
4684 error_report("rom check and register reset failed");
4685 exit(1);
4688 replay_start();
4690 /* This checkpoint is required by replay to separate prior clock
4691 reading from the other reads, because timer polling functions query
4692 clock values from the log. */
4693 replay_checkpoint(CHECKPOINT_RESET);
4694 qemu_system_reset(VMRESET_SILENT);
4695 register_global_state();
4696 if (loadvm) {
4697 if (load_vmstate(loadvm) < 0) {
4698 autostart = 0;
4702 qdev_prop_check_globals();
4703 if (vmstate_dump_file) {
4704 /* dump and exit */
4705 dump_vmstate_json_to_file(vmstate_dump_file);
4706 return 0;
4709 if (incoming) {
4710 Error *local_err = NULL;
4711 qemu_start_incoming_migration(incoming, &local_err);
4712 if (local_err) {
4713 error_report("-incoming %s: %s", incoming,
4714 error_get_pretty(local_err));
4715 error_free(local_err);
4716 exit(1);
4718 } else if (autostart) {
4719 vm_start();
4722 os_setup_post();
4724 if (is_daemonized()) {
4725 if (!trace_init_backends(trace_events, trace_file)) {
4726 exit(1);
4730 main_loop();
4731 replay_disable_events();
4733 bdrv_close_all();
4734 pause_all_vcpus();
4735 res_free();
4736 #ifdef CONFIG_TPM
4737 tpm_cleanup();
4738 #endif
4740 return 0;