qom: Make enum string tables const-correct
[qemu/cris-port.git] / vl.c
blobc8390716b56a9a246227fbd4b56d7cb50b0834b1
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "sysemu/numa.h"
82 #include "exec/gdbstub.h"
83 #include "qemu/timer.h"
84 #include "sysemu/char.h"
85 #include "qemu/bitmap.h"
86 #include "sysemu/blockdev.h"
87 #include "hw/block/block.h"
88 #include "migration/block.h"
89 #include "sysemu/tpm.h"
90 #include "sysemu/dma.h"
91 #include "audio/audio.h"
92 #include "migration/migration.h"
93 #include "sysemu/kvm.h"
94 #include "qapi/qmp/qjson.h"
95 #include "qemu/option.h"
96 #include "qemu/config-file.h"
97 #include "qemu-options.h"
98 #include "qmp-commands.h"
99 #include "qemu/main-loop.h"
100 #ifdef CONFIG_VIRTFS
101 #include "fsdev/qemu-fsdev.h"
102 #endif
103 #include "sysemu/qtest.h"
105 #include "disas/disas.h"
108 #include "slirp/libslirp.h"
110 #include "trace.h"
111 #include "trace/control.h"
112 #include "qemu/queue.h"
113 #include "sysemu/cpus.h"
114 #include "sysemu/arch_init.h"
115 #include "qemu/osdep.h"
117 #include "ui/qemu-spice.h"
118 #include "qapi/string-input-visitor.h"
119 #include "qapi/opts-visitor.h"
120 #include "qom/object_interfaces.h"
121 #include "qapi-event.h"
123 #define MAX_VIRTIO_CONSOLES 1
124 #define MAX_SCLP_CONSOLES 1
126 static const char *data_dir[16];
127 static int data_dir_idx;
128 const char *bios_name = NULL;
129 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
130 DisplayType display_type = DT_DEFAULT;
131 int request_opengl = -1;
132 int display_opengl;
133 static int display_remote;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
139 int nb_nics;
140 NICInfo nd_table[MAX_NICS];
141 int autostart;
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
148 int no_quit = 0;
149 #ifdef CONFIG_GTK
150 static bool grab_on_hover;
151 #endif
152 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
153 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
154 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
155 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
156 int win2k_install_hack = 0;
157 int singlestep = 0;
158 int smp_cpus = 1;
159 int max_cpus = 0;
160 int smp_cores = 1;
161 int smp_threads = 1;
162 int acpi_enabled = 1;
163 int no_hpet = 0;
164 int fd_bootchk = 1;
165 static int no_reboot;
166 int no_shutdown = 0;
167 int cursor_hide = 1;
168 int graphic_rotate = 0;
169 const char *watchdog;
170 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
171 int nb_option_roms;
172 int semihosting_enabled = 0;
173 int old_param = 0;
174 const char *qemu_name;
175 int alt_grab = 0;
176 int ctrl_grab = 0;
177 unsigned int nb_prom_envs = 0;
178 const char *prom_envs[MAX_PROM_ENVS];
179 int boot_menu;
180 bool boot_strict;
181 uint8_t *boot_splash_filedata;
182 size_t boot_splash_filedata_size;
183 uint8_t qemu_extra_params_fw[2];
185 int icount_align_option;
187 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
188 * little-endian "wire format" described in the SMBIOS 2.6 specification.
190 uint8_t qemu_uuid[16];
191 bool qemu_uuid_set;
193 static NotifierList exit_notifiers =
194 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
196 static NotifierList machine_init_done_notifiers =
197 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
199 bool xen_allowed;
200 uint32_t xen_domid;
201 enum xen_mode xen_mode = XEN_EMULATE;
203 static int has_defaults = 1;
204 static int default_serial = 1;
205 static int default_parallel = 1;
206 static int default_virtcon = 1;
207 static int default_sclp = 1;
208 static int default_monitor = 1;
209 static int default_floppy = 1;
210 static int default_cdrom = 1;
211 static int default_sdcard = 1;
212 static int default_vga = 1;
214 static struct {
215 const char *driver;
216 int *flag;
217 } default_list[] = {
218 { .driver = "isa-serial", .flag = &default_serial },
219 { .driver = "isa-parallel", .flag = &default_parallel },
220 { .driver = "isa-fdc", .flag = &default_floppy },
221 { .driver = "ide-cd", .flag = &default_cdrom },
222 { .driver = "ide-hd", .flag = &default_cdrom },
223 { .driver = "ide-drive", .flag = &default_cdrom },
224 { .driver = "scsi-cd", .flag = &default_cdrom },
225 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
226 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
227 { .driver = "virtio-serial", .flag = &default_virtcon },
228 { .driver = "VGA", .flag = &default_vga },
229 { .driver = "isa-vga", .flag = &default_vga },
230 { .driver = "cirrus-vga", .flag = &default_vga },
231 { .driver = "isa-cirrus-vga", .flag = &default_vga },
232 { .driver = "vmware-svga", .flag = &default_vga },
233 { .driver = "qxl-vga", .flag = &default_vga },
234 { .driver = "virtio-vga", .flag = &default_vga },
237 static QemuOptsList qemu_rtc_opts = {
238 .name = "rtc",
239 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
240 .desc = {
242 .name = "base",
243 .type = QEMU_OPT_STRING,
245 .name = "clock",
246 .type = QEMU_OPT_STRING,
248 .name = "driftfix",
249 .type = QEMU_OPT_STRING,
251 { /* end of list */ }
255 static QemuOptsList qemu_sandbox_opts = {
256 .name = "sandbox",
257 .implied_opt_name = "enable",
258 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
259 .desc = {
261 .name = "enable",
262 .type = QEMU_OPT_BOOL,
264 { /* end of list */ }
268 static QemuOptsList qemu_trace_opts = {
269 .name = "trace",
270 .implied_opt_name = "trace",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
272 .desc = {
274 .name = "events",
275 .type = QEMU_OPT_STRING,
277 .name = "file",
278 .type = QEMU_OPT_STRING,
280 { /* end of list */ }
284 static QemuOptsList qemu_option_rom_opts = {
285 .name = "option-rom",
286 .implied_opt_name = "romfile",
287 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
288 .desc = {
290 .name = "bootindex",
291 .type = QEMU_OPT_NUMBER,
292 }, {
293 .name = "romfile",
294 .type = QEMU_OPT_STRING,
296 { /* end of list */ }
300 static QemuOptsList qemu_machine_opts = {
301 .name = "machine",
302 .implied_opt_name = "type",
303 .merge_lists = true,
304 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
305 .desc = {
307 * no elements => accept any
308 * sanity checking will happen later
309 * when setting machine properties
315 static QemuOptsList qemu_boot_opts = {
316 .name = "boot-opts",
317 .implied_opt_name = "order",
318 .merge_lists = true,
319 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
320 .desc = {
322 .name = "order",
323 .type = QEMU_OPT_STRING,
324 }, {
325 .name = "once",
326 .type = QEMU_OPT_STRING,
327 }, {
328 .name = "menu",
329 .type = QEMU_OPT_BOOL,
330 }, {
331 .name = "splash",
332 .type = QEMU_OPT_STRING,
333 }, {
334 .name = "splash-time",
335 .type = QEMU_OPT_STRING,
336 }, {
337 .name = "reboot-timeout",
338 .type = QEMU_OPT_STRING,
339 }, {
340 .name = "strict",
341 .type = QEMU_OPT_BOOL,
343 { /*End of list */ }
347 static QemuOptsList qemu_add_fd_opts = {
348 .name = "add-fd",
349 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
350 .desc = {
352 .name = "fd",
353 .type = QEMU_OPT_NUMBER,
354 .help = "file descriptor of which a duplicate is added to fd set",
356 .name = "set",
357 .type = QEMU_OPT_NUMBER,
358 .help = "ID of the fd set to add fd to",
360 .name = "opaque",
361 .type = QEMU_OPT_STRING,
362 .help = "free-form string used to describe fd",
364 { /* end of list */ }
368 static QemuOptsList qemu_object_opts = {
369 .name = "object",
370 .implied_opt_name = "qom-type",
371 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
372 .desc = {
377 static QemuOptsList qemu_tpmdev_opts = {
378 .name = "tpmdev",
379 .implied_opt_name = "type",
380 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
381 .desc = {
382 /* options are defined in the TPM backends */
383 { /* end of list */ }
387 static QemuOptsList qemu_realtime_opts = {
388 .name = "realtime",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
390 .desc = {
392 .name = "mlock",
393 .type = QEMU_OPT_BOOL,
395 { /* end of list */ }
399 static QemuOptsList qemu_msg_opts = {
400 .name = "msg",
401 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
402 .desc = {
404 .name = "timestamp",
405 .type = QEMU_OPT_BOOL,
407 { /* end of list */ }
411 static QemuOptsList qemu_name_opts = {
412 .name = "name",
413 .implied_opt_name = "guest",
414 .merge_lists = true,
415 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
416 .desc = {
418 .name = "guest",
419 .type = QEMU_OPT_STRING,
420 .help = "Sets the name of the guest.\n"
421 "This name will be displayed in the SDL window caption.\n"
422 "The name will also be used for the VNC server",
423 }, {
424 .name = "process",
425 .type = QEMU_OPT_STRING,
426 .help = "Sets the name of the QEMU process, as shown in top etc",
427 }, {
428 .name = "debug-threads",
429 .type = QEMU_OPT_BOOL,
430 .help = "When enabled, name the individual threads; defaults off.\n"
431 "NOTE: The thread names are for debugging and not a\n"
432 "stable API.",
434 { /* End of list */ }
438 static QemuOptsList qemu_mem_opts = {
439 .name = "memory",
440 .implied_opt_name = "size",
441 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
442 .merge_lists = true,
443 .desc = {
445 .name = "size",
446 .type = QEMU_OPT_SIZE,
449 .name = "slots",
450 .type = QEMU_OPT_NUMBER,
453 .name = "maxmem",
454 .type = QEMU_OPT_SIZE,
456 { /* end of list */ }
460 static QemuOptsList qemu_icount_opts = {
461 .name = "icount",
462 .implied_opt_name = "shift",
463 .merge_lists = true,
464 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
465 .desc = {
467 .name = "shift",
468 .type = QEMU_OPT_STRING,
469 }, {
470 .name = "align",
471 .type = QEMU_OPT_BOOL,
472 }, {
473 .name = "sleep",
474 .type = QEMU_OPT_BOOL,
476 { /* end of list */ }
480 static QemuOptsList qemu_semihosting_config_opts = {
481 .name = "semihosting-config",
482 .implied_opt_name = "enable",
483 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
484 .desc = {
486 .name = "enable",
487 .type = QEMU_OPT_BOOL,
488 }, {
489 .name = "target",
490 .type = QEMU_OPT_STRING,
492 { /* end of list */ }
496 static QemuOptsList qemu_fw_cfg_opts = {
497 .name = "fw_cfg",
498 .implied_opt_name = "name",
499 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
500 .desc = {
502 .name = "name",
503 .type = QEMU_OPT_STRING,
504 .help = "Sets the fw_cfg name of the blob to be inserted",
505 }, {
506 .name = "file",
507 .type = QEMU_OPT_STRING,
508 .help = "Sets the name of the file from which\n"
509 "the fw_cfg blob will be loaded",
511 { /* end of list */ }
516 * Get machine options
518 * Returns: machine options (never null).
520 QemuOpts *qemu_get_machine_opts(void)
522 return qemu_find_opts_singleton("machine");
525 const char *qemu_get_vm_name(void)
527 return qemu_name;
530 static void res_free(void)
532 if (boot_splash_filedata != NULL) {
533 g_free(boot_splash_filedata);
534 boot_splash_filedata = NULL;
538 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
540 const char *driver = qemu_opt_get(opts, "driver");
541 int i;
543 if (!driver)
544 return 0;
545 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
546 if (strcmp(default_list[i].driver, driver) != 0)
547 continue;
548 *(default_list[i].flag) = 0;
550 return 0;
553 /***********************************************************/
554 /* QEMU state */
556 static RunState current_run_state = RUN_STATE_PRELAUNCH;
558 /* We use RUN_STATE_MAX but any invalid value will do */
559 static RunState vmstop_requested = RUN_STATE_MAX;
560 static QemuMutex vmstop_lock;
562 typedef struct {
563 RunState from;
564 RunState to;
565 } RunStateTransition;
567 static const RunStateTransition runstate_transitions_def[] = {
568 /* from -> to */
569 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
570 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
572 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
573 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
575 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
576 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
578 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
579 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
581 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
582 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
584 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
585 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
587 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
588 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
589 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
591 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
592 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
594 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
596 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
597 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
598 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
599 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
600 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
601 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
602 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
603 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
604 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
605 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
607 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
609 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
610 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
613 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
614 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
615 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
618 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
621 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
623 { RUN_STATE_MAX, RUN_STATE_MAX },
626 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
628 bool runstate_check(RunState state)
630 return current_run_state == state;
633 static void runstate_init(void)
635 const RunStateTransition *p;
637 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
638 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
639 runstate_valid_transitions[p->from][p->to] = true;
642 qemu_mutex_init(&vmstop_lock);
645 /* This function will abort() on invalid state transitions */
646 void runstate_set(RunState new_state)
648 assert(new_state < RUN_STATE_MAX);
650 if (!runstate_valid_transitions[current_run_state][new_state]) {
651 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
652 RunState_lookup[current_run_state],
653 RunState_lookup[new_state]);
654 abort();
656 trace_runstate_set(new_state);
657 current_run_state = new_state;
660 int runstate_is_running(void)
662 return runstate_check(RUN_STATE_RUNNING);
665 bool runstate_needs_reset(void)
667 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
668 runstate_check(RUN_STATE_SHUTDOWN);
671 StatusInfo *qmp_query_status(Error **errp)
673 StatusInfo *info = g_malloc0(sizeof(*info));
675 info->running = runstate_is_running();
676 info->singlestep = singlestep;
677 info->status = current_run_state;
679 return info;
682 static bool qemu_vmstop_requested(RunState *r)
684 qemu_mutex_lock(&vmstop_lock);
685 *r = vmstop_requested;
686 vmstop_requested = RUN_STATE_MAX;
687 qemu_mutex_unlock(&vmstop_lock);
688 return *r < RUN_STATE_MAX;
691 void qemu_system_vmstop_request_prepare(void)
693 qemu_mutex_lock(&vmstop_lock);
696 void qemu_system_vmstop_request(RunState state)
698 vmstop_requested = state;
699 qemu_mutex_unlock(&vmstop_lock);
700 qemu_notify_event();
703 void vm_start(void)
705 RunState requested;
707 qemu_vmstop_requested(&requested);
708 if (runstate_is_running() && requested == RUN_STATE_MAX) {
709 return;
712 /* Ensure that a STOP/RESUME pair of events is emitted if a
713 * vmstop request was pending. The BLOCK_IO_ERROR event, for
714 * example, according to documentation is always followed by
715 * the STOP event.
717 if (runstate_is_running()) {
718 qapi_event_send_stop(&error_abort);
719 } else {
720 cpu_enable_ticks();
721 runstate_set(RUN_STATE_RUNNING);
722 vm_state_notify(1, RUN_STATE_RUNNING);
723 resume_all_vcpus();
726 qapi_event_send_resume(&error_abort);
730 /***********************************************************/
731 /* real time host monotonic timer */
733 static time_t qemu_time(void)
735 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
738 /***********************************************************/
739 /* host time/date access */
740 void qemu_get_timedate(struct tm *tm, int offset)
742 time_t ti = qemu_time();
744 ti += offset;
745 if (rtc_date_offset == -1) {
746 if (rtc_utc)
747 gmtime_r(&ti, tm);
748 else
749 localtime_r(&ti, tm);
750 } else {
751 ti -= rtc_date_offset;
752 gmtime_r(&ti, tm);
756 int qemu_timedate_diff(struct tm *tm)
758 time_t seconds;
760 if (rtc_date_offset == -1)
761 if (rtc_utc)
762 seconds = mktimegm(tm);
763 else {
764 struct tm tmp = *tm;
765 tmp.tm_isdst = -1; /* use timezone to figure it out */
766 seconds = mktime(&tmp);
768 else
769 seconds = mktimegm(tm) + rtc_date_offset;
771 return seconds - qemu_time();
774 static void configure_rtc_date_offset(const char *startdate, int legacy)
776 time_t rtc_start_date;
777 struct tm tm;
779 if (!strcmp(startdate, "now") && legacy) {
780 rtc_date_offset = -1;
781 } else {
782 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
783 &tm.tm_year,
784 &tm.tm_mon,
785 &tm.tm_mday,
786 &tm.tm_hour,
787 &tm.tm_min,
788 &tm.tm_sec) == 6) {
789 /* OK */
790 } else if (sscanf(startdate, "%d-%d-%d",
791 &tm.tm_year,
792 &tm.tm_mon,
793 &tm.tm_mday) == 3) {
794 tm.tm_hour = 0;
795 tm.tm_min = 0;
796 tm.tm_sec = 0;
797 } else {
798 goto date_fail;
800 tm.tm_year -= 1900;
801 tm.tm_mon--;
802 rtc_start_date = mktimegm(&tm);
803 if (rtc_start_date == -1) {
804 date_fail:
805 fprintf(stderr, "Invalid date format. Valid formats are:\n"
806 "'2006-06-17T16:01:21' or '2006-06-17'\n");
807 exit(1);
809 rtc_date_offset = qemu_time() - rtc_start_date;
813 static void configure_rtc(QemuOpts *opts)
815 const char *value;
817 value = qemu_opt_get(opts, "base");
818 if (value) {
819 if (!strcmp(value, "utc")) {
820 rtc_utc = 1;
821 } else if (!strcmp(value, "localtime")) {
822 rtc_utc = 0;
823 } else {
824 configure_rtc_date_offset(value, 0);
827 value = qemu_opt_get(opts, "clock");
828 if (value) {
829 if (!strcmp(value, "host")) {
830 rtc_clock = QEMU_CLOCK_HOST;
831 } else if (!strcmp(value, "rt")) {
832 rtc_clock = QEMU_CLOCK_REALTIME;
833 } else if (!strcmp(value, "vm")) {
834 rtc_clock = QEMU_CLOCK_VIRTUAL;
835 } else {
836 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
837 exit(1);
840 value = qemu_opt_get(opts, "driftfix");
841 if (value) {
842 if (!strcmp(value, "slew")) {
843 static GlobalProperty slew_lost_ticks[] = {
845 .driver = "mc146818rtc",
846 .property = "lost_tick_policy",
847 .value = "slew",
849 { /* end of list */ }
852 qdev_prop_register_global_list(slew_lost_ticks);
853 } else if (!strcmp(value, "none")) {
854 /* discard is default */
855 } else {
856 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
857 exit(1);
862 /***********************************************************/
863 /* Bluetooth support */
864 static int nb_hcis;
865 static int cur_hci;
866 static struct HCIInfo *hci_table[MAX_NICS];
868 struct HCIInfo *qemu_next_hci(void)
870 if (cur_hci == nb_hcis)
871 return &null_hci;
873 return hci_table[cur_hci++];
876 static int bt_hci_parse(const char *str)
878 struct HCIInfo *hci;
879 bdaddr_t bdaddr;
881 if (nb_hcis >= MAX_NICS) {
882 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
883 return -1;
886 hci = hci_init(str);
887 if (!hci)
888 return -1;
890 bdaddr.b[0] = 0x52;
891 bdaddr.b[1] = 0x54;
892 bdaddr.b[2] = 0x00;
893 bdaddr.b[3] = 0x12;
894 bdaddr.b[4] = 0x34;
895 bdaddr.b[5] = 0x56 + nb_hcis;
896 hci->bdaddr_set(hci, bdaddr.b);
898 hci_table[nb_hcis++] = hci;
900 return 0;
903 static void bt_vhci_add(int vlan_id)
905 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
907 if (!vlan->slave)
908 fprintf(stderr, "qemu: warning: adding a VHCI to "
909 "an empty scatternet %i\n", vlan_id);
911 bt_vhci_init(bt_new_hci(vlan));
914 static struct bt_device_s *bt_device_add(const char *opt)
916 struct bt_scatternet_s *vlan;
917 int vlan_id = 0;
918 char *endp = strstr(opt, ",vlan=");
919 int len = (endp ? endp - opt : strlen(opt)) + 1;
920 char devname[10];
922 pstrcpy(devname, MIN(sizeof(devname), len), opt);
924 if (endp) {
925 vlan_id = strtol(endp + 6, &endp, 0);
926 if (*endp) {
927 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
928 return 0;
932 vlan = qemu_find_bt_vlan(vlan_id);
934 if (!vlan->slave)
935 fprintf(stderr, "qemu: warning: adding a slave device to "
936 "an empty scatternet %i\n", vlan_id);
938 if (!strcmp(devname, "keyboard"))
939 return bt_keyboard_init(vlan);
941 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
942 return 0;
945 static int bt_parse(const char *opt)
947 const char *endp, *p;
948 int vlan;
950 if (strstart(opt, "hci", &endp)) {
951 if (!*endp || *endp == ',') {
952 if (*endp)
953 if (!strstart(endp, ",vlan=", 0))
954 opt = endp + 1;
956 return bt_hci_parse(opt);
958 } else if (strstart(opt, "vhci", &endp)) {
959 if (!*endp || *endp == ',') {
960 if (*endp) {
961 if (strstart(endp, ",vlan=", &p)) {
962 vlan = strtol(p, (char **) &endp, 0);
963 if (*endp) {
964 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
965 return 1;
967 } else {
968 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
969 return 1;
971 } else
972 vlan = 0;
974 bt_vhci_add(vlan);
975 return 0;
977 } else if (strstart(opt, "device:", &endp))
978 return !bt_device_add(endp);
980 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
981 return 1;
984 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
986 /* FIXME: change this to true for 1.3 */
987 if (qemu_opt_get_bool(opts, "enable", false)) {
988 #ifdef CONFIG_SECCOMP
989 if (seccomp_start() < 0) {
990 qerror_report(ERROR_CLASS_GENERIC_ERROR,
991 "failed to install seccomp syscall filter in the kernel");
992 return -1;
994 #else
995 qerror_report(ERROR_CLASS_GENERIC_ERROR,
996 "sandboxing request but seccomp is not compiled into this build");
997 return -1;
998 #endif
1001 return 0;
1004 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1006 const char *proc_name;
1008 if (qemu_opt_get(opts, "debug-threads")) {
1009 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1011 qemu_name = qemu_opt_get(opts, "guest");
1013 proc_name = qemu_opt_get(opts, "process");
1014 if (proc_name) {
1015 os_set_proc_name(proc_name);
1018 return 0;
1021 bool defaults_enabled(void)
1023 return has_defaults;
1026 bool usb_enabled(void)
1028 return machine_usb(current_machine);
1031 #ifndef _WIN32
1032 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1034 int fd, dupfd, flags;
1035 int64_t fdset_id;
1036 const char *fd_opaque = NULL;
1037 AddfdInfo *fdinfo;
1039 fd = qemu_opt_get_number(opts, "fd", -1);
1040 fdset_id = qemu_opt_get_number(opts, "set", -1);
1041 fd_opaque = qemu_opt_get(opts, "opaque");
1043 if (fd < 0) {
1044 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1045 "fd option is required and must be non-negative");
1046 return -1;
1049 if (fd <= STDERR_FILENO) {
1050 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1051 "fd cannot be a standard I/O stream");
1052 return -1;
1056 * All fds inherited across exec() necessarily have FD_CLOEXEC
1057 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1059 flags = fcntl(fd, F_GETFD);
1060 if (flags == -1 || (flags & FD_CLOEXEC)) {
1061 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1062 "fd is not valid or already in use");
1063 return -1;
1066 if (fdset_id < 0) {
1067 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1068 "set option is required and must be non-negative");
1069 return -1;
1072 #ifdef F_DUPFD_CLOEXEC
1073 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1074 #else
1075 dupfd = dup(fd);
1076 if (dupfd != -1) {
1077 qemu_set_cloexec(dupfd);
1079 #endif
1080 if (dupfd == -1) {
1081 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1082 "Error duplicating fd: %s", strerror(errno));
1083 return -1;
1086 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1087 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1088 &error_abort);
1089 g_free(fdinfo);
1091 return 0;
1094 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1096 int fd;
1098 fd = qemu_opt_get_number(opts, "fd", -1);
1099 close(fd);
1101 return 0;
1103 #endif
1105 /***********************************************************/
1106 /* QEMU Block devices */
1108 #define HD_OPTS "media=disk"
1109 #define CDROM_OPTS "media=cdrom"
1110 #define FD_OPTS ""
1111 #define PFLASH_OPTS ""
1112 #define MTD_OPTS ""
1113 #define SD_OPTS ""
1115 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1117 BlockInterfaceType *block_default_type = opaque;
1119 return drive_new(opts, *block_default_type) == NULL;
1122 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1124 if (qemu_opt_get(opts, "snapshot") == NULL) {
1125 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1127 return 0;
1130 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1131 int index, const char *optstr)
1133 QemuOpts *opts;
1134 DriveInfo *dinfo;
1136 if (!enable || drive_get_by_index(type, index)) {
1137 return;
1140 opts = drive_add(type, index, NULL, optstr);
1141 if (snapshot) {
1142 drive_enable_snapshot(NULL, opts, NULL);
1145 dinfo = drive_new(opts, type);
1146 if (!dinfo) {
1147 exit(1);
1149 dinfo->is_default = true;
1153 static QemuOptsList qemu_smp_opts = {
1154 .name = "smp-opts",
1155 .implied_opt_name = "cpus",
1156 .merge_lists = true,
1157 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1158 .desc = {
1160 .name = "cpus",
1161 .type = QEMU_OPT_NUMBER,
1162 }, {
1163 .name = "sockets",
1164 .type = QEMU_OPT_NUMBER,
1165 }, {
1166 .name = "cores",
1167 .type = QEMU_OPT_NUMBER,
1168 }, {
1169 .name = "threads",
1170 .type = QEMU_OPT_NUMBER,
1171 }, {
1172 .name = "maxcpus",
1173 .type = QEMU_OPT_NUMBER,
1175 { /*End of list */ }
1179 static void smp_parse(QemuOpts *opts)
1181 if (opts) {
1183 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1184 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1185 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1186 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1188 /* compute missing values, prefer sockets over cores over threads */
1189 if (cpus == 0 || sockets == 0) {
1190 sockets = sockets > 0 ? sockets : 1;
1191 cores = cores > 0 ? cores : 1;
1192 threads = threads > 0 ? threads : 1;
1193 if (cpus == 0) {
1194 cpus = cores * threads * sockets;
1196 } else if (cores == 0) {
1197 threads = threads > 0 ? threads : 1;
1198 cores = cpus / (sockets * threads);
1199 } else if (threads == 0) {
1200 threads = cpus / (cores * sockets);
1201 } else if (sockets * cores * threads < cpus) {
1202 fprintf(stderr, "cpu topology: error: "
1203 "sockets (%u) * cores (%u) * threads (%u) < "
1204 "smp_cpus (%u)\n",
1205 sockets, cores, threads, cpus);
1206 exit(1);
1209 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1211 smp_cpus = cpus;
1212 smp_cores = cores > 0 ? cores : 1;
1213 smp_threads = threads > 0 ? threads : 1;
1217 if (max_cpus == 0) {
1218 max_cpus = smp_cpus;
1221 if (max_cpus > MAX_CPUMASK_BITS) {
1222 fprintf(stderr, "Unsupported number of maxcpus\n");
1223 exit(1);
1225 if (max_cpus < smp_cpus) {
1226 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1227 exit(1);
1232 static void realtime_init(void)
1234 if (enable_mlock) {
1235 if (os_mlock() < 0) {
1236 fprintf(stderr, "qemu: locking memory failed\n");
1237 exit(1);
1243 static void configure_msg(QemuOpts *opts)
1245 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1248 /***********************************************************/
1249 /* USB devices */
1251 static int usb_device_add(const char *devname)
1253 USBDevice *dev = NULL;
1254 #ifndef CONFIG_LINUX
1255 const char *p;
1256 #endif
1258 if (!usb_enabled()) {
1259 return -1;
1262 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1263 dev = usbdevice_create(devname);
1264 if (dev)
1265 goto done;
1267 /* the other ones */
1268 #ifndef CONFIG_LINUX
1269 /* only the linux version is qdev-ified, usb-bsd still needs this */
1270 if (strstart(devname, "host:", &p)) {
1271 dev = usb_host_device_open(usb_bus_find(-1), p);
1273 #endif
1274 if (!dev)
1275 return -1;
1277 done:
1278 return 0;
1281 static int usb_device_del(const char *devname)
1283 int bus_num, addr;
1284 const char *p;
1286 if (strstart(devname, "host:", &p)) {
1287 return -1;
1290 if (!usb_enabled()) {
1291 return -1;
1294 p = strchr(devname, '.');
1295 if (!p)
1296 return -1;
1297 bus_num = strtoul(devname, NULL, 0);
1298 addr = strtoul(p + 1, NULL, 0);
1300 return usb_device_delete_addr(bus_num, addr);
1303 static int usb_parse(const char *cmdline)
1305 int r;
1306 r = usb_device_add(cmdline);
1307 if (r < 0) {
1308 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1310 return r;
1313 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1315 const char *devname = qdict_get_str(qdict, "devname");
1316 if (usb_device_add(devname) < 0) {
1317 error_report("could not add USB device '%s'", devname);
1321 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1323 const char *devname = qdict_get_str(qdict, "devname");
1324 if (usb_device_del(devname) < 0) {
1325 error_report("could not delete USB device '%s'", devname);
1329 /***********************************************************/
1330 /* machine registration */
1332 MachineState *current_machine;
1335 * Transitional class registration/init used for converting from
1336 * legacy QEMUMachine to MachineClass.
1338 static void qemu_machine_class_init(ObjectClass *oc, void *data)
1340 MachineClass *mc = MACHINE_CLASS(oc);
1341 QEMUMachine *qm = data;
1342 mc->name = qm->name;
1343 mc->desc = qm->desc;
1344 mc->init = qm->init;
1345 mc->kvm_type = qm->kvm_type;
1346 mc->block_default_type = qm->block_default_type;
1347 mc->max_cpus = qm->max_cpus;
1348 mc->no_sdcard = qm->no_sdcard;
1349 mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1350 mc->is_default = qm->is_default;
1351 mc->default_machine_opts = qm->default_machine_opts;
1352 mc->default_boot_order = qm->default_boot_order;
1355 int qemu_register_machine(QEMUMachine *m)
1357 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1358 TypeInfo ti = {
1359 .name = name,
1360 .parent = TYPE_MACHINE,
1361 .class_init = qemu_machine_class_init,
1362 .class_data = (void *)m,
1365 type_register(&ti);
1366 g_free(name);
1368 return 0;
1371 static MachineClass *find_machine(const char *name)
1373 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1374 MachineClass *mc = NULL;
1376 for (el = machines; el; el = el->next) {
1377 MachineClass *temp = el->data;
1379 if (!strcmp(temp->name, name)) {
1380 mc = temp;
1381 break;
1383 if (temp->alias &&
1384 !strcmp(temp->alias, name)) {
1385 mc = temp;
1386 break;
1390 g_slist_free(machines);
1391 return mc;
1394 MachineClass *find_default_machine(void)
1396 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1397 MachineClass *mc = NULL;
1399 for (el = machines; el; el = el->next) {
1400 MachineClass *temp = el->data;
1402 if (temp->is_default) {
1403 mc = temp;
1404 break;
1408 g_slist_free(machines);
1409 return mc;
1412 MachineInfoList *qmp_query_machines(Error **errp)
1414 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1415 MachineInfoList *mach_list = NULL;
1417 for (el = machines; el; el = el->next) {
1418 MachineClass *mc = el->data;
1419 MachineInfoList *entry;
1420 MachineInfo *info;
1422 info = g_malloc0(sizeof(*info));
1423 if (mc->is_default) {
1424 info->has_is_default = true;
1425 info->is_default = true;
1428 if (mc->alias) {
1429 info->has_alias = true;
1430 info->alias = g_strdup(mc->alias);
1433 info->name = g_strdup(mc->name);
1434 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1436 entry = g_malloc0(sizeof(*entry));
1437 entry->value = info;
1438 entry->next = mach_list;
1439 mach_list = entry;
1442 g_slist_free(machines);
1443 return mach_list;
1446 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1448 ObjectProperty *prop;
1450 if (!qemu_opt_has_help_opt(opts)) {
1451 return 0;
1454 QTAILQ_FOREACH(prop, &OBJECT(machine)->properties, node) {
1455 if (!prop->set) {
1456 continue;
1459 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1460 prop->name, prop->type);
1461 if (prop->description) {
1462 error_printf(" (%s)\n", prop->description);
1463 } else {
1464 error_printf("\n");
1468 return 1;
1471 /***********************************************************/
1472 /* main execution loop */
1474 struct vm_change_state_entry {
1475 VMChangeStateHandler *cb;
1476 void *opaque;
1477 QLIST_ENTRY (vm_change_state_entry) entries;
1480 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1482 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1483 void *opaque)
1485 VMChangeStateEntry *e;
1487 e = g_malloc0(sizeof (*e));
1489 e->cb = cb;
1490 e->opaque = opaque;
1491 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1492 return e;
1495 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1497 QLIST_REMOVE (e, entries);
1498 g_free (e);
1501 void vm_state_notify(int running, RunState state)
1503 VMChangeStateEntry *e, *next;
1505 trace_vm_state_notify(running, state);
1507 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1508 e->cb(e->opaque, running, state);
1512 /* reset/shutdown handler */
1514 typedef struct QEMUResetEntry {
1515 QTAILQ_ENTRY(QEMUResetEntry) entry;
1516 QEMUResetHandler *func;
1517 void *opaque;
1518 } QEMUResetEntry;
1520 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1521 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1522 static int reset_requested;
1523 static int shutdown_requested, shutdown_signal = -1;
1524 static pid_t shutdown_pid;
1525 static int powerdown_requested;
1526 static int debug_requested;
1527 static int suspend_requested;
1528 static WakeupReason wakeup_reason;
1529 static NotifierList powerdown_notifiers =
1530 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1531 static NotifierList suspend_notifiers =
1532 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1533 static NotifierList wakeup_notifiers =
1534 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1535 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1537 int qemu_shutdown_requested_get(void)
1539 return shutdown_requested;
1542 int qemu_reset_requested_get(void)
1544 return reset_requested;
1547 static int qemu_shutdown_requested(void)
1549 return atomic_xchg(&shutdown_requested, 0);
1552 static void qemu_kill_report(void)
1554 if (!qtest_driver() && shutdown_signal != -1) {
1555 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1556 if (shutdown_pid == 0) {
1557 /* This happens for eg ^C at the terminal, so it's worth
1558 * avoiding printing an odd message in that case.
1560 fputc('\n', stderr);
1561 } else {
1562 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1564 shutdown_signal = -1;
1568 static int qemu_reset_requested(void)
1570 int r = reset_requested;
1571 reset_requested = 0;
1572 return r;
1575 static int qemu_suspend_requested(void)
1577 int r = suspend_requested;
1578 suspend_requested = 0;
1579 return r;
1582 static WakeupReason qemu_wakeup_requested(void)
1584 return wakeup_reason;
1587 static int qemu_powerdown_requested(void)
1589 int r = powerdown_requested;
1590 powerdown_requested = 0;
1591 return r;
1594 static int qemu_debug_requested(void)
1596 int r = debug_requested;
1597 debug_requested = 0;
1598 return r;
1601 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1603 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1605 re->func = func;
1606 re->opaque = opaque;
1607 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1610 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1612 QEMUResetEntry *re;
1614 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1615 if (re->func == func && re->opaque == opaque) {
1616 QTAILQ_REMOVE(&reset_handlers, re, entry);
1617 g_free(re);
1618 return;
1623 void qemu_devices_reset(void)
1625 QEMUResetEntry *re, *nre;
1627 /* reset all devices */
1628 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1629 re->func(re->opaque);
1633 void qemu_system_reset(bool report)
1635 MachineClass *mc;
1637 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1639 if (mc && mc->reset) {
1640 mc->reset();
1641 } else {
1642 qemu_devices_reset();
1644 if (report) {
1645 qapi_event_send_reset(&error_abort);
1647 cpu_synchronize_all_post_reset();
1650 void qemu_system_reset_request(void)
1652 if (no_reboot) {
1653 shutdown_requested = 1;
1654 } else {
1655 reset_requested = 1;
1657 cpu_stop_current();
1658 qemu_notify_event();
1661 static void qemu_system_suspend(void)
1663 pause_all_vcpus();
1664 notifier_list_notify(&suspend_notifiers, NULL);
1665 runstate_set(RUN_STATE_SUSPENDED);
1666 qapi_event_send_suspend(&error_abort);
1669 void qemu_system_suspend_request(void)
1671 if (runstate_check(RUN_STATE_SUSPENDED)) {
1672 return;
1674 suspend_requested = 1;
1675 cpu_stop_current();
1676 qemu_notify_event();
1679 void qemu_register_suspend_notifier(Notifier *notifier)
1681 notifier_list_add(&suspend_notifiers, notifier);
1684 void qemu_system_wakeup_request(WakeupReason reason)
1686 trace_system_wakeup_request(reason);
1688 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1689 return;
1691 if (!(wakeup_reason_mask & (1 << reason))) {
1692 return;
1694 runstate_set(RUN_STATE_RUNNING);
1695 wakeup_reason = reason;
1696 qemu_notify_event();
1699 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1701 if (enabled) {
1702 wakeup_reason_mask |= (1 << reason);
1703 } else {
1704 wakeup_reason_mask &= ~(1 << reason);
1708 void qemu_register_wakeup_notifier(Notifier *notifier)
1710 notifier_list_add(&wakeup_notifiers, notifier);
1713 void qemu_system_killed(int signal, pid_t pid)
1715 shutdown_signal = signal;
1716 shutdown_pid = pid;
1717 no_shutdown = 0;
1718 qemu_system_shutdown_request();
1721 void qemu_system_shutdown_request(void)
1723 trace_qemu_system_shutdown_request();
1724 shutdown_requested = 1;
1725 qemu_notify_event();
1728 static void qemu_system_powerdown(void)
1730 qapi_event_send_powerdown(&error_abort);
1731 notifier_list_notify(&powerdown_notifiers, NULL);
1734 void qemu_system_powerdown_request(void)
1736 trace_qemu_system_powerdown_request();
1737 powerdown_requested = 1;
1738 qemu_notify_event();
1741 void qemu_register_powerdown_notifier(Notifier *notifier)
1743 notifier_list_add(&powerdown_notifiers, notifier);
1746 void qemu_system_debug_request(void)
1748 debug_requested = 1;
1749 qemu_notify_event();
1752 static bool main_loop_should_exit(void)
1754 RunState r;
1755 if (qemu_debug_requested()) {
1756 vm_stop(RUN_STATE_DEBUG);
1758 if (qemu_suspend_requested()) {
1759 qemu_system_suspend();
1761 if (qemu_shutdown_requested()) {
1762 qemu_kill_report();
1763 qapi_event_send_shutdown(&error_abort);
1764 if (no_shutdown) {
1765 vm_stop(RUN_STATE_SHUTDOWN);
1766 } else {
1767 return true;
1770 if (qemu_reset_requested()) {
1771 pause_all_vcpus();
1772 cpu_synchronize_all_states();
1773 qemu_system_reset(VMRESET_REPORT);
1774 resume_all_vcpus();
1775 if (runstate_needs_reset()) {
1776 runstate_set(RUN_STATE_PAUSED);
1779 if (qemu_wakeup_requested()) {
1780 pause_all_vcpus();
1781 cpu_synchronize_all_states();
1782 qemu_system_reset(VMRESET_SILENT);
1783 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1784 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1785 resume_all_vcpus();
1786 qapi_event_send_wakeup(&error_abort);
1788 if (qemu_powerdown_requested()) {
1789 qemu_system_powerdown();
1791 if (qemu_vmstop_requested(&r)) {
1792 vm_stop(r);
1794 return false;
1797 static void main_loop(void)
1799 bool nonblocking;
1800 int last_io = 0;
1801 #ifdef CONFIG_PROFILER
1802 int64_t ti;
1803 #endif
1804 do {
1805 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1806 #ifdef CONFIG_PROFILER
1807 ti = profile_getclock();
1808 #endif
1809 last_io = main_loop_wait(nonblocking);
1810 #ifdef CONFIG_PROFILER
1811 dev_time += profile_getclock() - ti;
1812 #endif
1813 } while (!main_loop_should_exit());
1816 static void version(void)
1818 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1821 static void help(int exitcode)
1823 version();
1824 printf("usage: %s [options] [disk_image]\n\n"
1825 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1826 error_get_progname());
1828 #define QEMU_OPTIONS_GENERATE_HELP
1829 #include "qemu-options-wrapper.h"
1831 printf("\nDuring emulation, the following keys are useful:\n"
1832 "ctrl-alt-f toggle full screen\n"
1833 "ctrl-alt-n switch to virtual console 'n'\n"
1834 "ctrl-alt toggle mouse and keyboard grab\n"
1835 "\n"
1836 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1838 exit(exitcode);
1841 #define HAS_ARG 0x0001
1843 typedef struct QEMUOption {
1844 const char *name;
1845 int flags;
1846 int index;
1847 uint32_t arch_mask;
1848 } QEMUOption;
1850 static const QEMUOption qemu_options[] = {
1851 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1852 #define QEMU_OPTIONS_GENERATE_OPTIONS
1853 #include "qemu-options-wrapper.h"
1854 { NULL },
1857 static bool vga_available(void)
1859 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1862 static bool cirrus_vga_available(void)
1864 return object_class_by_name("cirrus-vga")
1865 || object_class_by_name("isa-cirrus-vga");
1868 static bool vmware_vga_available(void)
1870 return object_class_by_name("vmware-svga");
1873 static bool qxl_vga_available(void)
1875 return object_class_by_name("qxl-vga");
1878 static bool tcx_vga_available(void)
1880 return object_class_by_name("SUNW,tcx");
1883 static bool cg3_vga_available(void)
1885 return object_class_by_name("cgthree");
1888 static bool virtio_vga_available(void)
1890 return object_class_by_name("virtio-vga");
1893 static void select_vgahw (const char *p)
1895 const char *opts;
1897 assert(vga_interface_type == VGA_NONE);
1898 if (strstart(p, "std", &opts)) {
1899 if (vga_available()) {
1900 vga_interface_type = VGA_STD;
1901 } else {
1902 fprintf(stderr, "Error: standard VGA not available\n");
1903 exit(0);
1905 } else if (strstart(p, "cirrus", &opts)) {
1906 if (cirrus_vga_available()) {
1907 vga_interface_type = VGA_CIRRUS;
1908 } else {
1909 fprintf(stderr, "Error: Cirrus VGA not available\n");
1910 exit(0);
1912 } else if (strstart(p, "vmware", &opts)) {
1913 if (vmware_vga_available()) {
1914 vga_interface_type = VGA_VMWARE;
1915 } else {
1916 fprintf(stderr, "Error: VMWare SVGA not available\n");
1917 exit(0);
1919 } else if (strstart(p, "virtio", &opts)) {
1920 if (virtio_vga_available()) {
1921 vga_interface_type = VGA_VIRTIO;
1922 } else {
1923 fprintf(stderr, "Error: Virtio VGA not available\n");
1924 exit(0);
1926 } else if (strstart(p, "xenfb", &opts)) {
1927 vga_interface_type = VGA_XENFB;
1928 } else if (strstart(p, "qxl", &opts)) {
1929 if (qxl_vga_available()) {
1930 vga_interface_type = VGA_QXL;
1931 } else {
1932 fprintf(stderr, "Error: QXL VGA not available\n");
1933 exit(0);
1935 } else if (strstart(p, "tcx", &opts)) {
1936 if (tcx_vga_available()) {
1937 vga_interface_type = VGA_TCX;
1938 } else {
1939 fprintf(stderr, "Error: TCX framebuffer not available\n");
1940 exit(0);
1942 } else if (strstart(p, "cg3", &opts)) {
1943 if (cg3_vga_available()) {
1944 vga_interface_type = VGA_CG3;
1945 } else {
1946 fprintf(stderr, "Error: CG3 framebuffer not available\n");
1947 exit(0);
1949 } else if (!strstart(p, "none", &opts)) {
1950 invalid_vga:
1951 fprintf(stderr, "Unknown vga type: %s\n", p);
1952 exit(1);
1954 while (*opts) {
1955 const char *nextopt;
1957 if (strstart(opts, ",retrace=", &nextopt)) {
1958 opts = nextopt;
1959 if (strstart(opts, "dumb", &nextopt))
1960 vga_retrace_method = VGA_RETRACE_DUMB;
1961 else if (strstart(opts, "precise", &nextopt))
1962 vga_retrace_method = VGA_RETRACE_PRECISE;
1963 else goto invalid_vga;
1964 } else goto invalid_vga;
1965 opts = nextopt;
1969 static DisplayType select_display(const char *p)
1971 const char *opts;
1972 DisplayType display = DT_DEFAULT;
1974 if (strstart(p, "sdl", &opts)) {
1975 #ifdef CONFIG_SDL
1976 display = DT_SDL;
1977 while (*opts) {
1978 const char *nextopt;
1980 if (strstart(opts, ",frame=", &nextopt)) {
1981 opts = nextopt;
1982 if (strstart(opts, "on", &nextopt)) {
1983 no_frame = 0;
1984 } else if (strstart(opts, "off", &nextopt)) {
1985 no_frame = 1;
1986 } else {
1987 goto invalid_sdl_args;
1989 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1990 opts = nextopt;
1991 if (strstart(opts, "on", &nextopt)) {
1992 alt_grab = 1;
1993 } else if (strstart(opts, "off", &nextopt)) {
1994 alt_grab = 0;
1995 } else {
1996 goto invalid_sdl_args;
1998 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1999 opts = nextopt;
2000 if (strstart(opts, "on", &nextopt)) {
2001 ctrl_grab = 1;
2002 } else if (strstart(opts, "off", &nextopt)) {
2003 ctrl_grab = 0;
2004 } else {
2005 goto invalid_sdl_args;
2007 } else if (strstart(opts, ",window_close=", &nextopt)) {
2008 opts = nextopt;
2009 if (strstart(opts, "on", &nextopt)) {
2010 no_quit = 0;
2011 } else if (strstart(opts, "off", &nextopt)) {
2012 no_quit = 1;
2013 } else {
2014 goto invalid_sdl_args;
2016 } else if (strstart(opts, ",gl=", &nextopt)) {
2017 opts = nextopt;
2018 if (strstart(opts, "on", &nextopt)) {
2019 request_opengl = 1;
2020 } else if (strstart(opts, "off", &nextopt)) {
2021 request_opengl = 0;
2022 } else {
2023 goto invalid_sdl_args;
2025 } else {
2026 invalid_sdl_args:
2027 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2028 exit(1);
2030 opts = nextopt;
2032 #else
2033 fprintf(stderr, "SDL support is disabled\n");
2034 exit(1);
2035 #endif
2036 } else if (strstart(p, "vnc", &opts)) {
2037 #ifdef CONFIG_VNC
2038 if (*opts == '=') {
2039 if (vnc_parse_func(opts+1) == NULL) {
2040 exit(1);
2042 } else {
2043 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2044 exit(1);
2046 #else
2047 fprintf(stderr, "VNC support is disabled\n");
2048 exit(1);
2049 #endif
2050 } else if (strstart(p, "curses", &opts)) {
2051 #ifdef CONFIG_CURSES
2052 display = DT_CURSES;
2053 #else
2054 fprintf(stderr, "Curses support is disabled\n");
2055 exit(1);
2056 #endif
2057 } else if (strstart(p, "gtk", &opts)) {
2058 #ifdef CONFIG_GTK
2059 display = DT_GTK;
2060 while (*opts) {
2061 const char *nextopt;
2063 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2064 opts = nextopt;
2065 if (strstart(opts, "on", &nextopt)) {
2066 grab_on_hover = true;
2067 } else if (strstart(opts, "off", &nextopt)) {
2068 grab_on_hover = false;
2069 } else {
2070 goto invalid_gtk_args;
2072 } else if (strstart(opts, ",gl=", &nextopt)) {
2073 opts = nextopt;
2074 if (strstart(opts, "on", &nextopt)) {
2075 request_opengl = 1;
2076 } else if (strstart(opts, "off", &nextopt)) {
2077 request_opengl = 0;
2078 } else {
2079 goto invalid_gtk_args;
2081 } else {
2082 invalid_gtk_args:
2083 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2084 exit(1);
2086 opts = nextopt;
2088 #else
2089 fprintf(stderr, "GTK support is disabled\n");
2090 exit(1);
2091 #endif
2092 } else if (strstart(p, "none", &opts)) {
2093 display = DT_NONE;
2094 } else {
2095 fprintf(stderr, "Unknown display type: %s\n", p);
2096 exit(1);
2099 return display;
2102 static int balloon_parse(const char *arg)
2104 QemuOpts *opts;
2106 if (strcmp(arg, "none") == 0) {
2107 return 0;
2110 if (!strncmp(arg, "virtio", 6)) {
2111 if (arg[6] == ',') {
2112 /* have params -> parse them */
2113 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2114 if (!opts)
2115 return -1;
2116 } else {
2117 /* create empty opts */
2118 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2119 &error_abort);
2121 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2122 return 0;
2125 return -1;
2128 char *qemu_find_file(int type, const char *name)
2130 int i;
2131 const char *subdir;
2132 char *buf;
2134 /* Try the name as a straight path first */
2135 if (access(name, R_OK) == 0) {
2136 trace_load_file(name, name);
2137 return g_strdup(name);
2140 switch (type) {
2141 case QEMU_FILE_TYPE_BIOS:
2142 subdir = "";
2143 break;
2144 case QEMU_FILE_TYPE_KEYMAP:
2145 subdir = "keymaps/";
2146 break;
2147 default:
2148 abort();
2151 for (i = 0; i < data_dir_idx; i++) {
2152 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2153 if (access(buf, R_OK) == 0) {
2154 trace_load_file(name, buf);
2155 return buf;
2157 g_free(buf);
2159 return NULL;
2162 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2164 gchar *buf;
2165 size_t size;
2166 const char *name, *file;
2168 if (opaque == NULL) {
2169 error_report("fw_cfg device not available");
2170 return -1;
2172 name = qemu_opt_get(opts, "name");
2173 file = qemu_opt_get(opts, "file");
2174 if (name == NULL || *name == '\0' || file == NULL || *file == '\0') {
2175 error_report("invalid argument value");
2176 return -1;
2178 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2179 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2180 return -1;
2182 if (strncmp(name, "opt/", 4) != 0) {
2183 error_report("WARNING: externally provided fw_cfg item names "
2184 "should be prefixed with \"opt/\"!");
2186 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2187 error_report("can't load %s", file);
2188 return -1;
2190 fw_cfg_add_file((FWCfgState *)opaque, name, buf, size);
2191 return 0;
2194 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2196 return qdev_device_help(opts);
2199 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2201 DeviceState *dev;
2203 dev = qdev_device_add(opts);
2204 if (!dev)
2205 return -1;
2206 object_unref(OBJECT(dev));
2207 return 0;
2210 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2212 Error *local_err = NULL;
2214 qemu_chr_new_from_opts(opts, NULL, &local_err);
2215 if (local_err) {
2216 error_report_err(local_err);
2217 return -1;
2219 return 0;
2222 #ifdef CONFIG_VIRTFS
2223 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2225 int ret;
2226 ret = qemu_fsdev_add(opts);
2228 return ret;
2230 #endif
2232 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2234 CharDriverState *chr;
2235 const char *chardev;
2236 const char *mode;
2237 int flags;
2239 mode = qemu_opt_get(opts, "mode");
2240 if (mode == NULL) {
2241 mode = "readline";
2243 if (strcmp(mode, "readline") == 0) {
2244 flags = MONITOR_USE_READLINE;
2245 } else if (strcmp(mode, "control") == 0) {
2246 flags = MONITOR_USE_CONTROL;
2247 } else {
2248 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2249 exit(1);
2252 if (qemu_opt_get_bool(opts, "pretty", 0))
2253 flags |= MONITOR_USE_PRETTY;
2255 if (qemu_opt_get_bool(opts, "default", 0))
2256 flags |= MONITOR_IS_DEFAULT;
2258 chardev = qemu_opt_get(opts, "chardev");
2259 chr = qemu_chr_find(chardev);
2260 if (chr == NULL) {
2261 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2262 exit(1);
2265 qemu_chr_fe_claim_no_fail(chr);
2266 monitor_init(chr, flags);
2267 return 0;
2270 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2272 static int monitor_device_index = 0;
2273 Error *local_err = NULL;
2274 QemuOpts *opts;
2275 const char *p;
2276 char label[32];
2277 int def = 0;
2279 if (strstart(optarg, "chardev:", &p)) {
2280 snprintf(label, sizeof(label), "%s", p);
2281 } else {
2282 snprintf(label, sizeof(label), "compat_monitor%d",
2283 monitor_device_index);
2284 if (monitor_device_index == 0) {
2285 def = 1;
2287 opts = qemu_chr_parse_compat(label, optarg);
2288 if (!opts) {
2289 fprintf(stderr, "parse error: %s\n", optarg);
2290 exit(1);
2294 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2295 if (!opts) {
2296 error_report_err(local_err);
2297 exit(1);
2299 qemu_opt_set(opts, "mode", mode, &error_abort);
2300 qemu_opt_set(opts, "chardev", label, &error_abort);
2301 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2302 if (def)
2303 qemu_opt_set(opts, "default", "on", &error_abort);
2304 monitor_device_index++;
2307 struct device_config {
2308 enum {
2309 DEV_USB, /* -usbdevice */
2310 DEV_BT, /* -bt */
2311 DEV_SERIAL, /* -serial */
2312 DEV_PARALLEL, /* -parallel */
2313 DEV_VIRTCON, /* -virtioconsole */
2314 DEV_DEBUGCON, /* -debugcon */
2315 DEV_GDB, /* -gdb, -s */
2316 DEV_SCLP, /* s390 sclp */
2317 } type;
2318 const char *cmdline;
2319 Location loc;
2320 QTAILQ_ENTRY(device_config) next;
2323 static QTAILQ_HEAD(, device_config) device_configs =
2324 QTAILQ_HEAD_INITIALIZER(device_configs);
2326 static void add_device_config(int type, const char *cmdline)
2328 struct device_config *conf;
2330 conf = g_malloc0(sizeof(*conf));
2331 conf->type = type;
2332 conf->cmdline = cmdline;
2333 loc_save(&conf->loc);
2334 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2337 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2339 struct device_config *conf;
2340 int rc;
2342 QTAILQ_FOREACH(conf, &device_configs, next) {
2343 if (conf->type != type)
2344 continue;
2345 loc_push_restore(&conf->loc);
2346 rc = func(conf->cmdline);
2347 loc_pop(&conf->loc);
2348 if (rc) {
2349 return rc;
2352 return 0;
2355 static int serial_parse(const char *devname)
2357 static int index = 0;
2358 char label[32];
2360 if (strcmp(devname, "none") == 0)
2361 return 0;
2362 if (index == MAX_SERIAL_PORTS) {
2363 fprintf(stderr, "qemu: too many serial ports\n");
2364 exit(1);
2366 snprintf(label, sizeof(label), "serial%d", index);
2367 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2368 if (!serial_hds[index]) {
2369 fprintf(stderr, "qemu: could not connect serial device"
2370 " to character backend '%s'\n", devname);
2371 return -1;
2373 index++;
2374 return 0;
2377 static int parallel_parse(const char *devname)
2379 static int index = 0;
2380 char label[32];
2382 if (strcmp(devname, "none") == 0)
2383 return 0;
2384 if (index == MAX_PARALLEL_PORTS) {
2385 fprintf(stderr, "qemu: too many parallel ports\n");
2386 exit(1);
2388 snprintf(label, sizeof(label), "parallel%d", index);
2389 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2390 if (!parallel_hds[index]) {
2391 fprintf(stderr, "qemu: could not connect parallel device"
2392 " to character backend '%s'\n", devname);
2393 return -1;
2395 index++;
2396 return 0;
2399 static int virtcon_parse(const char *devname)
2401 QemuOptsList *device = qemu_find_opts("device");
2402 static int index = 0;
2403 char label[32];
2404 QemuOpts *bus_opts, *dev_opts;
2406 if (strcmp(devname, "none") == 0)
2407 return 0;
2408 if (index == MAX_VIRTIO_CONSOLES) {
2409 fprintf(stderr, "qemu: too many virtio consoles\n");
2410 exit(1);
2413 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2414 if (arch_type == QEMU_ARCH_S390X) {
2415 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390", &error_abort);
2416 } else {
2417 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci", &error_abort);
2420 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2421 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2423 snprintf(label, sizeof(label), "virtcon%d", index);
2424 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2425 if (!virtcon_hds[index]) {
2426 fprintf(stderr, "qemu: could not connect virtio console"
2427 " to character backend '%s'\n", devname);
2428 return -1;
2430 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2432 index++;
2433 return 0;
2436 static int sclp_parse(const char *devname)
2438 QemuOptsList *device = qemu_find_opts("device");
2439 static int index = 0;
2440 char label[32];
2441 QemuOpts *dev_opts;
2443 if (strcmp(devname, "none") == 0) {
2444 return 0;
2446 if (index == MAX_SCLP_CONSOLES) {
2447 fprintf(stderr, "qemu: too many sclp consoles\n");
2448 exit(1);
2451 assert(arch_type == QEMU_ARCH_S390X);
2453 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2454 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2456 snprintf(label, sizeof(label), "sclpcon%d", index);
2457 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2458 if (!sclp_hds[index]) {
2459 fprintf(stderr, "qemu: could not connect sclp console"
2460 " to character backend '%s'\n", devname);
2461 return -1;
2463 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2465 index++;
2466 return 0;
2469 static int debugcon_parse(const char *devname)
2471 QemuOpts *opts;
2473 if (!qemu_chr_new("debugcon", devname, NULL)) {
2474 exit(1);
2476 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2477 if (!opts) {
2478 fprintf(stderr, "qemu: already have a debugcon device\n");
2479 exit(1);
2481 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2482 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2483 return 0;
2486 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2488 const MachineClass *mc1 = a, *mc2 = b;
2489 int res;
2491 if (mc1->family == NULL) {
2492 if (mc2->family == NULL) {
2493 /* Compare standalone machine types against each other; they sort
2494 * in increasing order.
2496 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2497 object_class_get_name(OBJECT_CLASS(mc2)));
2500 /* Standalone machine types sort after families. */
2501 return 1;
2504 if (mc2->family == NULL) {
2505 /* Families sort before standalone machine types. */
2506 return -1;
2509 /* Families sort between each other alphabetically increasingly. */
2510 res = strcmp(mc1->family, mc2->family);
2511 if (res != 0) {
2512 return res;
2515 /* Within the same family, machine types sort in decreasing order. */
2516 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2517 object_class_get_name(OBJECT_CLASS(mc1)));
2520 static MachineClass *machine_parse(const char *name)
2522 MachineClass *mc = NULL;
2523 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2525 if (name) {
2526 mc = find_machine(name);
2528 if (mc) {
2529 g_slist_free(machines);
2530 return mc;
2532 if (name && !is_help_option(name)) {
2533 error_report("Unsupported machine type");
2534 error_printf("Use -machine help to list supported machines!\n");
2535 } else {
2536 printf("Supported machines are:\n");
2537 machines = g_slist_sort(machines, machine_class_cmp);
2538 for (el = machines; el; el = el->next) {
2539 MachineClass *mc = el->data;
2540 if (mc->alias) {
2541 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2543 printf("%-20s %s%s\n", mc->name, mc->desc,
2544 mc->is_default ? " (default)" : "");
2548 g_slist_free(machines);
2549 exit(!name || !is_help_option(name));
2552 void qemu_add_exit_notifier(Notifier *notify)
2554 notifier_list_add(&exit_notifiers, notify);
2557 void qemu_remove_exit_notifier(Notifier *notify)
2559 notifier_remove(notify);
2562 static void qemu_run_exit_notifiers(void)
2564 notifier_list_notify(&exit_notifiers, NULL);
2567 static bool machine_init_done;
2569 void qemu_add_machine_init_done_notifier(Notifier *notify)
2571 notifier_list_add(&machine_init_done_notifiers, notify);
2572 if (machine_init_done) {
2573 notify->notify(notify, NULL);
2577 static void qemu_run_machine_init_done_notifiers(void)
2579 notifier_list_notify(&machine_init_done_notifiers, NULL);
2580 machine_init_done = true;
2583 static const QEMUOption *lookup_opt(int argc, char **argv,
2584 const char **poptarg, int *poptind)
2586 const QEMUOption *popt;
2587 int optind = *poptind;
2588 char *r = argv[optind];
2589 const char *optarg;
2591 loc_set_cmdline(argv, optind, 1);
2592 optind++;
2593 /* Treat --foo the same as -foo. */
2594 if (r[1] == '-')
2595 r++;
2596 popt = qemu_options;
2597 for(;;) {
2598 if (!popt->name) {
2599 error_report("invalid option");
2600 exit(1);
2602 if (!strcmp(popt->name, r + 1))
2603 break;
2604 popt++;
2606 if (popt->flags & HAS_ARG) {
2607 if (optind >= argc) {
2608 error_report("requires an argument");
2609 exit(1);
2611 optarg = argv[optind++];
2612 loc_set_cmdline(argv, optind - 2, 2);
2613 } else {
2614 optarg = NULL;
2617 *poptarg = optarg;
2618 *poptind = optind;
2620 return popt;
2623 static gpointer malloc_and_trace(gsize n_bytes)
2625 void *ptr = malloc(n_bytes);
2626 trace_g_malloc(n_bytes, ptr);
2627 return ptr;
2630 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2632 void *ptr = realloc(mem, n_bytes);
2633 trace_g_realloc(mem, n_bytes, ptr);
2634 return ptr;
2637 static void free_and_trace(gpointer mem)
2639 trace_g_free(mem);
2640 free(mem);
2643 static int machine_set_property(void *opaque,
2644 const char *name, const char *value,
2645 Error **errp)
2647 Object *obj = OBJECT(opaque);
2648 Error *local_err = NULL;
2649 char *c, *qom_name;
2651 if (strcmp(name, "type") == 0) {
2652 return 0;
2655 qom_name = g_strdup(name);
2656 c = qom_name;
2657 while (*c++) {
2658 if (*c == '_') {
2659 *c = '-';
2663 object_property_parse(obj, value, qom_name, &local_err);
2664 g_free(qom_name);
2666 if (local_err) {
2667 error_report_err(local_err);
2668 return -1;
2671 return 0;
2676 * Initial object creation happens before all other
2677 * QEMU data types are created. The majority of objects
2678 * can be created at this point. The rng-egd object
2679 * cannot be created here, as it depends on the chardev
2680 * already existing.
2682 static bool object_create_initial(const char *type)
2684 if (g_str_equal(type, "rng-egd")) {
2685 return false;
2687 return true;
2692 * The remainder of object creation happens after the
2693 * creation of chardev, fsdev and device data types.
2695 static bool object_create_delayed(const char *type)
2697 return !object_create_initial(type);
2701 static int object_create(void *opaque, QemuOpts *opts, Error **errp)
2703 Error *err = NULL;
2704 char *type = NULL;
2705 char *id = NULL;
2706 void *dummy = NULL;
2707 OptsVisitor *ov;
2708 QDict *pdict;
2709 bool (*type_predicate)(const char *) = opaque;
2711 ov = opts_visitor_new(opts);
2712 pdict = qemu_opts_to_qdict(opts, NULL);
2714 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2715 if (err) {
2716 goto out;
2719 qdict_del(pdict, "qom-type");
2720 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2721 if (err) {
2722 goto out;
2724 if (!type_predicate(type)) {
2725 goto out;
2728 qdict_del(pdict, "id");
2729 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2730 if (err) {
2731 goto out;
2734 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2735 if (err) {
2736 goto out;
2738 visit_end_struct(opts_get_visitor(ov), &err);
2739 if (err) {
2740 qmp_object_del(id, NULL);
2743 out:
2744 opts_visitor_cleanup(ov);
2746 QDECREF(pdict);
2747 g_free(id);
2748 g_free(type);
2749 g_free(dummy);
2750 if (err) {
2751 error_report_err(err);
2752 return -1;
2754 return 0;
2757 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2758 MachineClass *mc)
2760 uint64_t sz;
2761 const char *mem_str;
2762 const char *maxmem_str, *slots_str;
2763 const ram_addr_t default_ram_size = mc->default_ram_size;
2764 QemuOpts *opts = qemu_find_opts_singleton("memory");
2766 sz = 0;
2767 mem_str = qemu_opt_get(opts, "size");
2768 if (mem_str) {
2769 if (!*mem_str) {
2770 error_report("missing 'size' option value");
2771 exit(EXIT_FAILURE);
2774 sz = qemu_opt_get_size(opts, "size", ram_size);
2776 /* Fix up legacy suffix-less format */
2777 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2778 uint64_t overflow_check = sz;
2780 sz <<= 20;
2781 if ((sz >> 20) != overflow_check) {
2782 error_report("too large 'size' option value");
2783 exit(EXIT_FAILURE);
2788 /* backward compatibility behaviour for case "-m 0" */
2789 if (sz == 0) {
2790 sz = default_ram_size;
2793 sz = QEMU_ALIGN_UP(sz, 8192);
2794 ram_size = sz;
2795 if (ram_size != sz) {
2796 error_report("ram size too large");
2797 exit(EXIT_FAILURE);
2800 /* store value for the future use */
2801 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2802 *maxram_size = ram_size;
2804 maxmem_str = qemu_opt_get(opts, "maxmem");
2805 slots_str = qemu_opt_get(opts, "slots");
2806 if (maxmem_str && slots_str) {
2807 uint64_t slots;
2809 sz = qemu_opt_get_size(opts, "maxmem", 0);
2810 slots = qemu_opt_get_number(opts, "slots", 0);
2811 if (sz < ram_size) {
2812 error_report("invalid value of -m option maxmem: "
2813 "maximum memory size (0x%" PRIx64 ") must be at least "
2814 "the initial memory size (0x" RAM_ADDR_FMT ")",
2815 sz, ram_size);
2816 exit(EXIT_FAILURE);
2817 } else if (sz > ram_size) {
2818 if (!slots) {
2819 error_report("invalid value of -m option: maxmem was "
2820 "specified, but no hotplug slots were specified");
2821 exit(EXIT_FAILURE);
2823 } else if (slots) {
2824 error_report("invalid value of -m option maxmem: "
2825 "memory slots were specified but maximum memory size "
2826 "(0x%" PRIx64 ") is equal to the initial memory size "
2827 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2828 exit(EXIT_FAILURE);
2831 *maxram_size = sz;
2832 *ram_slots = slots;
2833 } else if ((!maxmem_str && slots_str) ||
2834 (maxmem_str && !slots_str)) {
2835 error_report("invalid -m option value: missing "
2836 "'%s' option", slots_str ? "maxmem" : "slots");
2837 exit(EXIT_FAILURE);
2841 int main(int argc, char **argv, char **envp)
2843 int i;
2844 int snapshot, linux_boot;
2845 const char *initrd_filename;
2846 const char *kernel_filename, *kernel_cmdline;
2847 const char *boot_order = NULL;
2848 const char *boot_once = NULL;
2849 DisplayState *ds;
2850 int cyls, heads, secs, translation;
2851 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2852 QemuOptsList *olist;
2853 int optind;
2854 const char *optarg;
2855 const char *loadvm = NULL;
2856 MachineClass *machine_class;
2857 const char *cpu_model;
2858 const char *vga_model = NULL;
2859 const char *qtest_chrdev = NULL;
2860 const char *qtest_log = NULL;
2861 const char *pid_file = NULL;
2862 const char *incoming = NULL;
2863 #ifdef CONFIG_VNC
2864 int show_vnc_port = 0;
2865 #endif
2866 bool defconfig = true;
2867 bool userconfig = true;
2868 const char *log_mask = NULL;
2869 const char *log_file = NULL;
2870 GMemVTable mem_trace = {
2871 .malloc = malloc_and_trace,
2872 .realloc = realloc_and_trace,
2873 .free = free_and_trace,
2875 const char *trace_events = NULL;
2876 const char *trace_file = NULL;
2877 ram_addr_t maxram_size;
2878 uint64_t ram_slots = 0;
2879 FILE *vmstate_dump_file = NULL;
2880 Error *main_loop_err = NULL;
2882 qemu_init_cpu_loop();
2883 qemu_mutex_lock_iothread();
2885 atexit(qemu_run_exit_notifiers);
2886 error_set_progname(argv[0]);
2887 qemu_init_exec_dir(argv[0]);
2889 g_mem_set_vtable(&mem_trace);
2891 module_call_init(MODULE_INIT_QOM);
2893 qemu_add_opts(&qemu_drive_opts);
2894 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2895 qemu_add_drive_opts(&qemu_common_drive_opts);
2896 qemu_add_drive_opts(&qemu_drive_opts);
2897 qemu_add_opts(&qemu_chardev_opts);
2898 qemu_add_opts(&qemu_device_opts);
2899 qemu_add_opts(&qemu_netdev_opts);
2900 qemu_add_opts(&qemu_net_opts);
2901 qemu_add_opts(&qemu_rtc_opts);
2902 qemu_add_opts(&qemu_global_opts);
2903 qemu_add_opts(&qemu_mon_opts);
2904 qemu_add_opts(&qemu_trace_opts);
2905 qemu_add_opts(&qemu_option_rom_opts);
2906 qemu_add_opts(&qemu_machine_opts);
2907 qemu_add_opts(&qemu_mem_opts);
2908 qemu_add_opts(&qemu_smp_opts);
2909 qemu_add_opts(&qemu_boot_opts);
2910 qemu_add_opts(&qemu_sandbox_opts);
2911 qemu_add_opts(&qemu_add_fd_opts);
2912 qemu_add_opts(&qemu_object_opts);
2913 qemu_add_opts(&qemu_tpmdev_opts);
2914 qemu_add_opts(&qemu_realtime_opts);
2915 qemu_add_opts(&qemu_msg_opts);
2916 qemu_add_opts(&qemu_name_opts);
2917 qemu_add_opts(&qemu_numa_opts);
2918 qemu_add_opts(&qemu_icount_opts);
2919 qemu_add_opts(&qemu_semihosting_config_opts);
2920 qemu_add_opts(&qemu_fw_cfg_opts);
2922 runstate_init();
2924 rtc_clock = QEMU_CLOCK_HOST;
2926 QLIST_INIT (&vm_change_state_head);
2927 os_setup_early_signal_handling();
2929 module_call_init(MODULE_INIT_MACHINE);
2930 machine_class = find_default_machine();
2931 cpu_model = NULL;
2932 snapshot = 0;
2933 cyls = heads = secs = 0;
2934 translation = BIOS_ATA_TRANSLATION_AUTO;
2936 nb_nics = 0;
2938 bdrv_init_with_whitelist();
2940 autostart = 1;
2942 /* first pass of option parsing */
2943 optind = 1;
2944 while (optind < argc) {
2945 if (argv[optind][0] != '-') {
2946 /* disk image */
2947 optind++;
2948 } else {
2949 const QEMUOption *popt;
2951 popt = lookup_opt(argc, argv, &optarg, &optind);
2952 switch (popt->index) {
2953 case QEMU_OPTION_nodefconfig:
2954 defconfig = false;
2955 break;
2956 case QEMU_OPTION_nouserconfig:
2957 userconfig = false;
2958 break;
2963 if (defconfig) {
2964 int ret;
2965 ret = qemu_read_default_config_files(userconfig);
2966 if (ret < 0) {
2967 exit(1);
2971 /* second pass of option parsing */
2972 optind = 1;
2973 for(;;) {
2974 if (optind >= argc)
2975 break;
2976 if (argv[optind][0] != '-') {
2977 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2978 } else {
2979 const QEMUOption *popt;
2981 popt = lookup_opt(argc, argv, &optarg, &optind);
2982 if (!(popt->arch_mask & arch_type)) {
2983 printf("Option %s not supported for this target\n", popt->name);
2984 exit(1);
2986 switch(popt->index) {
2987 case QEMU_OPTION_no_kvm_irqchip: {
2988 olist = qemu_find_opts("machine");
2989 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2990 break;
2992 case QEMU_OPTION_cpu:
2993 /* hw initialization will check this */
2994 cpu_model = optarg;
2995 break;
2996 case QEMU_OPTION_hda:
2998 char buf[256];
2999 if (cyls == 0)
3000 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3001 else
3002 snprintf(buf, sizeof(buf),
3003 "%s,cyls=%d,heads=%d,secs=%d%s",
3004 HD_OPTS , cyls, heads, secs,
3005 translation == BIOS_ATA_TRANSLATION_LBA ?
3006 ",trans=lba" :
3007 translation == BIOS_ATA_TRANSLATION_NONE ?
3008 ",trans=none" : "");
3009 drive_add(IF_DEFAULT, 0, optarg, buf);
3010 break;
3012 case QEMU_OPTION_hdb:
3013 case QEMU_OPTION_hdc:
3014 case QEMU_OPTION_hdd:
3015 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3016 HD_OPTS);
3017 break;
3018 case QEMU_OPTION_drive:
3019 if (drive_def(optarg) == NULL) {
3020 exit(1);
3022 break;
3023 case QEMU_OPTION_set:
3024 if (qemu_set_option(optarg) != 0)
3025 exit(1);
3026 break;
3027 case QEMU_OPTION_global:
3028 if (qemu_global_option(optarg) != 0)
3029 exit(1);
3030 break;
3031 case QEMU_OPTION_mtdblock:
3032 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3033 break;
3034 case QEMU_OPTION_sd:
3035 drive_add(IF_SD, -1, optarg, SD_OPTS);
3036 break;
3037 case QEMU_OPTION_pflash:
3038 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3039 break;
3040 case QEMU_OPTION_snapshot:
3041 snapshot = 1;
3042 break;
3043 case QEMU_OPTION_hdachs:
3045 const char *p;
3046 p = optarg;
3047 cyls = strtol(p, (char **)&p, 0);
3048 if (cyls < 1 || cyls > 16383)
3049 goto chs_fail;
3050 if (*p != ',')
3051 goto chs_fail;
3052 p++;
3053 heads = strtol(p, (char **)&p, 0);
3054 if (heads < 1 || heads > 16)
3055 goto chs_fail;
3056 if (*p != ',')
3057 goto chs_fail;
3058 p++;
3059 secs = strtol(p, (char **)&p, 0);
3060 if (secs < 1 || secs > 63)
3061 goto chs_fail;
3062 if (*p == ',') {
3063 p++;
3064 if (!strcmp(p, "large")) {
3065 translation = BIOS_ATA_TRANSLATION_LARGE;
3066 } else if (!strcmp(p, "rechs")) {
3067 translation = BIOS_ATA_TRANSLATION_RECHS;
3068 } else if (!strcmp(p, "none")) {
3069 translation = BIOS_ATA_TRANSLATION_NONE;
3070 } else if (!strcmp(p, "lba")) {
3071 translation = BIOS_ATA_TRANSLATION_LBA;
3072 } else if (!strcmp(p, "auto")) {
3073 translation = BIOS_ATA_TRANSLATION_AUTO;
3074 } else {
3075 goto chs_fail;
3077 } else if (*p != '\0') {
3078 chs_fail:
3079 fprintf(stderr, "qemu: invalid physical CHS format\n");
3080 exit(1);
3082 if (hda_opts != NULL) {
3083 qemu_opt_set_number(hda_opts, "cyls", cyls,
3084 &error_abort);
3085 qemu_opt_set_number(hda_opts, "heads", heads,
3086 &error_abort);
3087 qemu_opt_set_number(hda_opts, "secs", secs,
3088 &error_abort);
3089 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3090 qemu_opt_set(hda_opts, "trans", "large",
3091 &error_abort);
3092 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3093 qemu_opt_set(hda_opts, "trans", "rechs",
3094 &error_abort);
3095 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3096 qemu_opt_set(hda_opts, "trans", "lba",
3097 &error_abort);
3098 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3099 qemu_opt_set(hda_opts, "trans", "none",
3100 &error_abort);
3104 break;
3105 case QEMU_OPTION_numa:
3106 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3107 if (!opts) {
3108 exit(1);
3110 break;
3111 case QEMU_OPTION_display:
3112 display_type = select_display(optarg);
3113 break;
3114 case QEMU_OPTION_nographic:
3115 display_type = DT_NOGRAPHIC;
3116 break;
3117 case QEMU_OPTION_curses:
3118 #ifdef CONFIG_CURSES
3119 display_type = DT_CURSES;
3120 #else
3121 fprintf(stderr, "Curses support is disabled\n");
3122 exit(1);
3123 #endif
3124 break;
3125 case QEMU_OPTION_portrait:
3126 graphic_rotate = 90;
3127 break;
3128 case QEMU_OPTION_rotate:
3129 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3130 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3131 graphic_rotate != 180 && graphic_rotate != 270) {
3132 fprintf(stderr,
3133 "qemu: only 90, 180, 270 deg rotation is available\n");
3134 exit(1);
3136 break;
3137 case QEMU_OPTION_kernel:
3138 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3139 &error_abort);
3140 break;
3141 case QEMU_OPTION_initrd:
3142 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3143 &error_abort);
3144 break;
3145 case QEMU_OPTION_append:
3146 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3147 &error_abort);
3148 break;
3149 case QEMU_OPTION_dtb:
3150 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3151 &error_abort);
3152 break;
3153 case QEMU_OPTION_cdrom:
3154 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3155 break;
3156 case QEMU_OPTION_boot:
3157 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3158 if (!opts) {
3159 exit(1);
3161 break;
3162 case QEMU_OPTION_fda:
3163 case QEMU_OPTION_fdb:
3164 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3165 optarg, FD_OPTS);
3166 break;
3167 case QEMU_OPTION_no_fd_bootchk:
3168 fd_bootchk = 0;
3169 break;
3170 case QEMU_OPTION_netdev:
3171 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3172 exit(1);
3174 break;
3175 case QEMU_OPTION_net:
3176 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3177 exit(1);
3179 break;
3180 #ifdef CONFIG_LIBISCSI
3181 case QEMU_OPTION_iscsi:
3182 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3183 if (!opts) {
3184 exit(1);
3186 break;
3187 #endif
3188 #ifdef CONFIG_SLIRP
3189 case QEMU_OPTION_tftp:
3190 legacy_tftp_prefix = optarg;
3191 break;
3192 case QEMU_OPTION_bootp:
3193 legacy_bootp_filename = optarg;
3194 break;
3195 case QEMU_OPTION_redir:
3196 if (net_slirp_redir(optarg) < 0)
3197 exit(1);
3198 break;
3199 #endif
3200 case QEMU_OPTION_bt:
3201 add_device_config(DEV_BT, optarg);
3202 break;
3203 case QEMU_OPTION_audio_help:
3204 AUD_help ();
3205 exit (0);
3206 break;
3207 case QEMU_OPTION_soundhw:
3208 select_soundhw (optarg);
3209 break;
3210 case QEMU_OPTION_h:
3211 help(0);
3212 break;
3213 case QEMU_OPTION_version:
3214 version();
3215 exit(0);
3216 break;
3217 case QEMU_OPTION_m:
3218 opts = qemu_opts_parse(qemu_find_opts("memory"),
3219 optarg, 1);
3220 if (!opts) {
3221 exit(EXIT_FAILURE);
3223 break;
3224 #ifdef CONFIG_TPM
3225 case QEMU_OPTION_tpmdev:
3226 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3227 exit(1);
3229 break;
3230 #endif
3231 case QEMU_OPTION_mempath:
3232 mem_path = optarg;
3233 break;
3234 case QEMU_OPTION_mem_prealloc:
3235 mem_prealloc = 1;
3236 break;
3237 case QEMU_OPTION_d:
3238 log_mask = optarg;
3239 break;
3240 case QEMU_OPTION_D:
3241 log_file = optarg;
3242 break;
3243 case QEMU_OPTION_s:
3244 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3245 break;
3246 case QEMU_OPTION_gdb:
3247 add_device_config(DEV_GDB, optarg);
3248 break;
3249 case QEMU_OPTION_L:
3250 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3251 data_dir[data_dir_idx++] = optarg;
3253 break;
3254 case QEMU_OPTION_bios:
3255 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3256 &error_abort);
3257 break;
3258 case QEMU_OPTION_singlestep:
3259 singlestep = 1;
3260 break;
3261 case QEMU_OPTION_S:
3262 autostart = 0;
3263 break;
3264 case QEMU_OPTION_k:
3265 keyboard_layout = optarg;
3266 break;
3267 case QEMU_OPTION_localtime:
3268 rtc_utc = 0;
3269 break;
3270 case QEMU_OPTION_vga:
3271 vga_model = optarg;
3272 default_vga = 0;
3273 break;
3274 case QEMU_OPTION_g:
3276 const char *p;
3277 int w, h, depth;
3278 p = optarg;
3279 w = strtol(p, (char **)&p, 10);
3280 if (w <= 0) {
3281 graphic_error:
3282 fprintf(stderr, "qemu: invalid resolution or depth\n");
3283 exit(1);
3285 if (*p != 'x')
3286 goto graphic_error;
3287 p++;
3288 h = strtol(p, (char **)&p, 10);
3289 if (h <= 0)
3290 goto graphic_error;
3291 if (*p == 'x') {
3292 p++;
3293 depth = strtol(p, (char **)&p, 10);
3294 if (depth != 8 && depth != 15 && depth != 16 &&
3295 depth != 24 && depth != 32)
3296 goto graphic_error;
3297 } else if (*p == '\0') {
3298 depth = graphic_depth;
3299 } else {
3300 goto graphic_error;
3303 graphic_width = w;
3304 graphic_height = h;
3305 graphic_depth = depth;
3307 break;
3308 case QEMU_OPTION_echr:
3310 char *r;
3311 term_escape_char = strtol(optarg, &r, 0);
3312 if (r == optarg)
3313 printf("Bad argument to echr\n");
3314 break;
3316 case QEMU_OPTION_monitor:
3317 default_monitor = 0;
3318 if (strncmp(optarg, "none", 4)) {
3319 monitor_parse(optarg, "readline", false);
3321 break;
3322 case QEMU_OPTION_qmp:
3323 monitor_parse(optarg, "control", false);
3324 default_monitor = 0;
3325 break;
3326 case QEMU_OPTION_qmp_pretty:
3327 monitor_parse(optarg, "control", true);
3328 default_monitor = 0;
3329 break;
3330 case QEMU_OPTION_mon:
3331 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3332 if (!opts) {
3333 exit(1);
3335 default_monitor = 0;
3336 break;
3337 case QEMU_OPTION_chardev:
3338 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3339 if (!opts) {
3340 exit(1);
3342 break;
3343 case QEMU_OPTION_fsdev:
3344 olist = qemu_find_opts("fsdev");
3345 if (!olist) {
3346 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3347 exit(1);
3349 opts = qemu_opts_parse(olist, optarg, 1);
3350 if (!opts) {
3351 exit(1);
3353 break;
3354 case QEMU_OPTION_virtfs: {
3355 QemuOpts *fsdev;
3356 QemuOpts *device;
3357 const char *writeout, *sock_fd, *socket;
3359 olist = qemu_find_opts("virtfs");
3360 if (!olist) {
3361 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3362 exit(1);
3364 opts = qemu_opts_parse(olist, optarg, 1);
3365 if (!opts) {
3366 exit(1);
3369 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3370 qemu_opt_get(opts, "mount_tag") == NULL) {
3371 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3372 exit(1);
3374 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3375 qemu_opt_get(opts, "mount_tag"),
3376 1, NULL);
3377 if (!fsdev) {
3378 fprintf(stderr, "duplicate fsdev id: %s\n",
3379 qemu_opt_get(opts, "mount_tag"));
3380 exit(1);
3383 writeout = qemu_opt_get(opts, "writeout");
3384 if (writeout) {
3385 #ifdef CONFIG_SYNC_FILE_RANGE
3386 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3387 #else
3388 fprintf(stderr, "writeout=immediate not supported on "
3389 "this platform\n");
3390 exit(1);
3391 #endif
3393 qemu_opt_set(fsdev, "fsdriver",
3394 qemu_opt_get(opts, "fsdriver"), &error_abort);
3395 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3396 &error_abort);
3397 qemu_opt_set(fsdev, "security_model",
3398 qemu_opt_get(opts, "security_model"),
3399 &error_abort);
3400 socket = qemu_opt_get(opts, "socket");
3401 if (socket) {
3402 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3404 sock_fd = qemu_opt_get(opts, "sock_fd");
3405 if (sock_fd) {
3406 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3409 qemu_opt_set_bool(fsdev, "readonly",
3410 qemu_opt_get_bool(opts, "readonly", 0),
3411 &error_abort);
3412 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3413 &error_abort);
3414 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3415 qemu_opt_set(device, "fsdev",
3416 qemu_opt_get(opts, "mount_tag"), &error_abort);
3417 qemu_opt_set(device, "mount_tag",
3418 qemu_opt_get(opts, "mount_tag"), &error_abort);
3419 break;
3421 case QEMU_OPTION_virtfs_synth: {
3422 QemuOpts *fsdev;
3423 QemuOpts *device;
3425 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3426 1, NULL);
3427 if (!fsdev) {
3428 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3429 exit(1);
3431 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3433 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3434 &error_abort);
3435 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3436 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3437 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3438 break;
3440 case QEMU_OPTION_serial:
3441 add_device_config(DEV_SERIAL, optarg);
3442 default_serial = 0;
3443 if (strncmp(optarg, "mon:", 4) == 0) {
3444 default_monitor = 0;
3446 break;
3447 case QEMU_OPTION_watchdog:
3448 if (watchdog) {
3449 fprintf(stderr,
3450 "qemu: only one watchdog option may be given\n");
3451 return 1;
3453 watchdog = optarg;
3454 break;
3455 case QEMU_OPTION_watchdog_action:
3456 if (select_watchdog_action(optarg) == -1) {
3457 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3458 exit(1);
3460 break;
3461 case QEMU_OPTION_virtiocon:
3462 add_device_config(DEV_VIRTCON, optarg);
3463 default_virtcon = 0;
3464 if (strncmp(optarg, "mon:", 4) == 0) {
3465 default_monitor = 0;
3467 break;
3468 case QEMU_OPTION_parallel:
3469 add_device_config(DEV_PARALLEL, optarg);
3470 default_parallel = 0;
3471 if (strncmp(optarg, "mon:", 4) == 0) {
3472 default_monitor = 0;
3474 break;
3475 case QEMU_OPTION_debugcon:
3476 add_device_config(DEV_DEBUGCON, optarg);
3477 break;
3478 case QEMU_OPTION_loadvm:
3479 loadvm = optarg;
3480 break;
3481 case QEMU_OPTION_full_screen:
3482 full_screen = 1;
3483 break;
3484 case QEMU_OPTION_no_frame:
3485 no_frame = 1;
3486 break;
3487 case QEMU_OPTION_alt_grab:
3488 alt_grab = 1;
3489 break;
3490 case QEMU_OPTION_ctrl_grab:
3491 ctrl_grab = 1;
3492 break;
3493 case QEMU_OPTION_no_quit:
3494 no_quit = 1;
3495 break;
3496 case QEMU_OPTION_sdl:
3497 #ifdef CONFIG_SDL
3498 display_type = DT_SDL;
3499 break;
3500 #else
3501 fprintf(stderr, "SDL support is disabled\n");
3502 exit(1);
3503 #endif
3504 case QEMU_OPTION_pidfile:
3505 pid_file = optarg;
3506 break;
3507 case QEMU_OPTION_win2k_hack:
3508 win2k_install_hack = 1;
3509 break;
3510 case QEMU_OPTION_rtc_td_hack: {
3511 static GlobalProperty slew_lost_ticks[] = {
3513 .driver = "mc146818rtc",
3514 .property = "lost_tick_policy",
3515 .value = "slew",
3517 { /* end of list */ }
3520 qdev_prop_register_global_list(slew_lost_ticks);
3521 break;
3523 case QEMU_OPTION_acpitable:
3524 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3525 if (!opts) {
3526 exit(1);
3528 do_acpitable_option(opts);
3529 break;
3530 case QEMU_OPTION_smbios:
3531 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3532 if (!opts) {
3533 exit(1);
3535 do_smbios_option(opts);
3536 break;
3537 case QEMU_OPTION_fwcfg:
3538 opts = qemu_opts_parse(qemu_find_opts("fw_cfg"), optarg, 1);
3539 if (opts == NULL) {
3540 exit(1);
3542 break;
3543 case QEMU_OPTION_enable_kvm:
3544 olist = qemu_find_opts("machine");
3545 qemu_opts_parse(olist, "accel=kvm", 0);
3546 break;
3547 case QEMU_OPTION_M:
3548 case QEMU_OPTION_machine:
3549 olist = qemu_find_opts("machine");
3550 opts = qemu_opts_parse(olist, optarg, 1);
3551 if (!opts) {
3552 exit(1);
3554 break;
3555 case QEMU_OPTION_no_kvm:
3556 olist = qemu_find_opts("machine");
3557 qemu_opts_parse(olist, "accel=tcg", 0);
3558 break;
3559 case QEMU_OPTION_no_kvm_pit: {
3560 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3561 "separately.\n");
3562 break;
3564 case QEMU_OPTION_no_kvm_pit_reinjection: {
3565 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3567 .driver = "kvm-pit",
3568 .property = "lost_tick_policy",
3569 .value = "discard",
3571 { /* end of list */ }
3574 fprintf(stderr, "Warning: option deprecated, use "
3575 "lost_tick_policy property of kvm-pit instead.\n");
3576 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3577 break;
3579 case QEMU_OPTION_usb:
3580 olist = qemu_find_opts("machine");
3581 qemu_opts_parse(olist, "usb=on", 0);
3582 break;
3583 case QEMU_OPTION_usbdevice:
3584 olist = qemu_find_opts("machine");
3585 qemu_opts_parse(olist, "usb=on", 0);
3586 add_device_config(DEV_USB, optarg);
3587 break;
3588 case QEMU_OPTION_device:
3589 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3590 exit(1);
3592 break;
3593 case QEMU_OPTION_smp:
3594 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3595 exit(1);
3597 break;
3598 case QEMU_OPTION_vnc:
3599 #ifdef CONFIG_VNC
3600 if (vnc_parse_func(optarg) == NULL) {
3601 exit(1);
3603 #else
3604 fprintf(stderr, "VNC support is disabled\n");
3605 exit(1);
3606 #endif
3607 break;
3608 case QEMU_OPTION_no_acpi:
3609 acpi_enabled = 0;
3610 break;
3611 case QEMU_OPTION_no_hpet:
3612 no_hpet = 1;
3613 break;
3614 case QEMU_OPTION_balloon:
3615 if (balloon_parse(optarg) < 0) {
3616 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3617 exit(1);
3619 break;
3620 case QEMU_OPTION_no_reboot:
3621 no_reboot = 1;
3622 break;
3623 case QEMU_OPTION_no_shutdown:
3624 no_shutdown = 1;
3625 break;
3626 case QEMU_OPTION_show_cursor:
3627 cursor_hide = 0;
3628 break;
3629 case QEMU_OPTION_uuid:
3630 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3631 fprintf(stderr, "Fail to parse UUID string."
3632 " Wrong format.\n");
3633 exit(1);
3635 qemu_uuid_set = true;
3636 break;
3637 case QEMU_OPTION_option_rom:
3638 if (nb_option_roms >= MAX_OPTION_ROMS) {
3639 fprintf(stderr, "Too many option ROMs\n");
3640 exit(1);
3642 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3643 if (!opts) {
3644 exit(1);
3646 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3647 option_rom[nb_option_roms].bootindex =
3648 qemu_opt_get_number(opts, "bootindex", -1);
3649 if (!option_rom[nb_option_roms].name) {
3650 fprintf(stderr, "Option ROM file is not specified\n");
3651 exit(1);
3653 nb_option_roms++;
3654 break;
3655 case QEMU_OPTION_semihosting:
3656 semihosting_enabled = 1;
3657 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3658 break;
3659 case QEMU_OPTION_semihosting_config:
3660 semihosting_enabled = 1;
3661 opts = qemu_opts_parse(qemu_find_opts("semihosting-config"),
3662 optarg, 0);
3663 if (opts != NULL) {
3664 semihosting_enabled = qemu_opt_get_bool(opts, "enable",
3665 true);
3666 const char *target = qemu_opt_get(opts, "target");
3667 if (target != NULL) {
3668 if (strcmp("native", target) == 0) {
3669 semihosting_target = SEMIHOSTING_TARGET_NATIVE;
3670 } else if (strcmp("gdb", target) == 0) {
3671 semihosting_target = SEMIHOSTING_TARGET_GDB;
3672 } else if (strcmp("auto", target) == 0) {
3673 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3674 } else {
3675 fprintf(stderr, "Unsupported semihosting-config"
3676 " %s\n",
3677 optarg);
3678 exit(1);
3680 } else {
3681 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3683 } else {
3684 fprintf(stderr, "Unsupported semihosting-config %s\n",
3685 optarg);
3686 exit(1);
3688 break;
3689 case QEMU_OPTION_tdf:
3690 fprintf(stderr, "Warning: user space PIT time drift fix "
3691 "is no longer supported.\n");
3692 break;
3693 case QEMU_OPTION_name:
3694 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3695 if (!opts) {
3696 exit(1);
3698 break;
3699 case QEMU_OPTION_prom_env:
3700 if (nb_prom_envs >= MAX_PROM_ENVS) {
3701 fprintf(stderr, "Too many prom variables\n");
3702 exit(1);
3704 prom_envs[nb_prom_envs] = optarg;
3705 nb_prom_envs++;
3706 break;
3707 case QEMU_OPTION_old_param:
3708 old_param = 1;
3709 break;
3710 case QEMU_OPTION_clock:
3711 /* Clock options no longer exist. Keep this option for
3712 * backward compatibility.
3714 break;
3715 case QEMU_OPTION_startdate:
3716 configure_rtc_date_offset(optarg, 1);
3717 break;
3718 case QEMU_OPTION_rtc:
3719 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3720 if (!opts) {
3721 exit(1);
3723 configure_rtc(opts);
3724 break;
3725 case QEMU_OPTION_tb_size:
3726 tcg_tb_size = strtol(optarg, NULL, 0);
3727 if (tcg_tb_size < 0) {
3728 tcg_tb_size = 0;
3730 break;
3731 case QEMU_OPTION_icount:
3732 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3733 optarg, 1);
3734 if (!icount_opts) {
3735 exit(1);
3737 break;
3738 case QEMU_OPTION_incoming:
3739 if (!incoming) {
3740 runstate_set(RUN_STATE_INMIGRATE);
3742 incoming = optarg;
3743 break;
3744 case QEMU_OPTION_nodefaults:
3745 has_defaults = 0;
3746 break;
3747 case QEMU_OPTION_xen_domid:
3748 if (!(xen_available())) {
3749 printf("Option %s not supported for this target\n", popt->name);
3750 exit(1);
3752 xen_domid = atoi(optarg);
3753 break;
3754 case QEMU_OPTION_xen_create:
3755 if (!(xen_available())) {
3756 printf("Option %s not supported for this target\n", popt->name);
3757 exit(1);
3759 xen_mode = XEN_CREATE;
3760 break;
3761 case QEMU_OPTION_xen_attach:
3762 if (!(xen_available())) {
3763 printf("Option %s not supported for this target\n", popt->name);
3764 exit(1);
3766 xen_mode = XEN_ATTACH;
3767 break;
3768 case QEMU_OPTION_trace:
3770 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3771 if (!opts) {
3772 exit(1);
3774 trace_events = qemu_opt_get(opts, "events");
3775 trace_file = qemu_opt_get(opts, "file");
3776 break;
3778 case QEMU_OPTION_readconfig:
3780 int ret = qemu_read_config_file(optarg);
3781 if (ret < 0) {
3782 fprintf(stderr, "read config %s: %s\n", optarg,
3783 strerror(-ret));
3784 exit(1);
3786 break;
3788 case QEMU_OPTION_spice:
3789 olist = qemu_find_opts("spice");
3790 if (!olist) {
3791 fprintf(stderr, "spice is not supported by this qemu build.\n");
3792 exit(1);
3794 opts = qemu_opts_parse(olist, optarg, 0);
3795 if (!opts) {
3796 exit(1);
3798 display_remote++;
3799 break;
3800 case QEMU_OPTION_writeconfig:
3802 FILE *fp;
3803 if (strcmp(optarg, "-") == 0) {
3804 fp = stdout;
3805 } else {
3806 fp = fopen(optarg, "w");
3807 if (fp == NULL) {
3808 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3809 exit(1);
3812 qemu_config_write(fp);
3813 if (fp != stdout) {
3814 fclose(fp);
3816 break;
3818 case QEMU_OPTION_qtest:
3819 qtest_chrdev = optarg;
3820 break;
3821 case QEMU_OPTION_qtest_log:
3822 qtest_log = optarg;
3823 break;
3824 case QEMU_OPTION_sandbox:
3825 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3826 if (!opts) {
3827 exit(1);
3829 break;
3830 case QEMU_OPTION_add_fd:
3831 #ifndef _WIN32
3832 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3833 if (!opts) {
3834 exit(1);
3836 #else
3837 error_report("File descriptor passing is disabled on this "
3838 "platform");
3839 exit(1);
3840 #endif
3841 break;
3842 case QEMU_OPTION_object:
3843 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3844 if (!opts) {
3845 exit(1);
3847 break;
3848 case QEMU_OPTION_realtime:
3849 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3850 if (!opts) {
3851 exit(1);
3853 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3854 break;
3855 case QEMU_OPTION_msg:
3856 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3857 if (!opts) {
3858 exit(1);
3860 configure_msg(opts);
3861 break;
3862 case QEMU_OPTION_dump_vmstate:
3863 if (vmstate_dump_file) {
3864 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3865 "option may be given\n");
3866 exit(1);
3868 vmstate_dump_file = fopen(optarg, "w");
3869 if (vmstate_dump_file == NULL) {
3870 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3871 exit(1);
3873 break;
3874 default:
3875 os_parse_cmd_args(popt->index, optarg);
3880 opts = qemu_get_machine_opts();
3881 optarg = qemu_opt_get(opts, "type");
3882 if (optarg) {
3883 machine_class = machine_parse(optarg);
3886 if (machine_class == NULL) {
3887 fprintf(stderr, "No machine specified, and there is no default.\n"
3888 "Use -machine help to list supported machines!\n");
3889 exit(1);
3892 set_memory_options(&ram_slots, &maxram_size, machine_class);
3894 loc_set_none();
3896 os_daemonize();
3898 if (qemu_init_main_loop(&main_loop_err)) {
3899 error_report_err(main_loop_err);
3900 exit(1);
3903 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
3904 parse_sandbox, NULL, NULL)) {
3905 exit(1);
3908 if (qemu_opts_foreach(qemu_find_opts("name"),
3909 parse_name, NULL, NULL)) {
3910 exit(1);
3913 #ifndef _WIN32
3914 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
3915 parse_add_fd, NULL, NULL)) {
3916 exit(1);
3919 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
3920 cleanup_add_fd, NULL, NULL)) {
3921 exit(1);
3923 #endif
3925 current_machine = MACHINE(object_new(object_class_get_name(
3926 OBJECT_CLASS(machine_class))));
3927 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3928 exit(0);
3930 object_property_add_child(object_get_root(), "machine",
3931 OBJECT(current_machine), &error_abort);
3932 cpu_exec_init_all();
3934 if (machine_class->hw_version) {
3935 qemu_set_version(machine_class->hw_version);
3938 /* Init CPU def lists, based on config
3939 * - Must be called after all the qemu_read_config_file() calls
3940 * - Must be called before list_cpus()
3941 * - Must be called before machine->init()
3943 cpudef_init();
3945 if (cpu_model && is_help_option(cpu_model)) {
3946 list_cpus(stdout, &fprintf, cpu_model);
3947 exit(0);
3950 /* Open the logfile at this point, if necessary. We can't open the logfile
3951 * when encountering either of the logging options (-d or -D) because the
3952 * other one may be encountered later on the command line, changing the
3953 * location or level of logging.
3955 if (log_mask) {
3956 int mask;
3957 if (log_file) {
3958 qemu_set_log_filename(log_file);
3961 mask = qemu_str_to_log_mask(log_mask);
3962 if (!mask) {
3963 qemu_print_log_usage(stdout);
3964 exit(1);
3966 qemu_set_log(mask);
3969 if (!is_daemonized()) {
3970 if (!trace_init_backends(trace_events, trace_file)) {
3971 exit(1);
3975 /* If no data_dir is specified then try to find it relative to the
3976 executable path. */
3977 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3978 data_dir[data_dir_idx] = os_find_datadir();
3979 if (data_dir[data_dir_idx] != NULL) {
3980 data_dir_idx++;
3983 /* If all else fails use the install path specified when building. */
3984 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3985 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3988 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3990 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3991 if (max_cpus > machine_class->max_cpus) {
3992 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3993 "supported by machine `%s' (%d)\n", max_cpus,
3994 machine_class->name, machine_class->max_cpus);
3995 exit(1);
3999 * Get the default machine options from the machine if it is not already
4000 * specified either by the configuration file or by the command line.
4002 if (machine_class->default_machine_opts) {
4003 qemu_opts_set_defaults(qemu_find_opts("machine"),
4004 machine_class->default_machine_opts, 0);
4007 qemu_opts_foreach(qemu_find_opts("device"),
4008 default_driver_check, NULL, NULL);
4009 qemu_opts_foreach(qemu_find_opts("global"),
4010 default_driver_check, NULL, NULL);
4012 if (!vga_model && !default_vga) {
4013 vga_interface_type = VGA_DEVICE;
4015 if (!has_defaults || machine_class->no_serial) {
4016 default_serial = 0;
4018 if (!has_defaults || machine_class->no_parallel) {
4019 default_parallel = 0;
4021 if (!has_defaults || !machine_class->use_virtcon) {
4022 default_virtcon = 0;
4024 if (!has_defaults || !machine_class->use_sclp) {
4025 default_sclp = 0;
4027 if (!has_defaults || machine_class->no_floppy) {
4028 default_floppy = 0;
4030 if (!has_defaults || machine_class->no_cdrom) {
4031 default_cdrom = 0;
4033 if (!has_defaults || machine_class->no_sdcard) {
4034 default_sdcard = 0;
4036 if (!has_defaults) {
4037 default_monitor = 0;
4038 default_net = 0;
4039 default_vga = 0;
4042 if (is_daemonized()) {
4043 /* According to documentation and historically, -nographic redirects
4044 * serial port, parallel port and monitor to stdio, which does not work
4045 * with -daemonize. We can redirect these to null instead, but since
4046 * -nographic is legacy, let's just error out.
4047 * We disallow -nographic only if all other ports are not redirected
4048 * explicitly, to not break existing legacy setups which uses
4049 * -nographic _and_ redirects all ports explicitly - this is valid
4050 * usage, -nographic is just a no-op in this case.
4052 if (display_type == DT_NOGRAPHIC
4053 && (default_parallel || default_serial
4054 || default_monitor || default_virtcon)) {
4055 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4056 exit(1);
4058 #ifdef CONFIG_CURSES
4059 if (display_type == DT_CURSES) {
4060 fprintf(stderr, "curses display can not be used with -daemonize\n");
4061 exit(1);
4063 #endif
4066 if (display_type == DT_NOGRAPHIC) {
4067 if (default_parallel)
4068 add_device_config(DEV_PARALLEL, "null");
4069 if (default_serial && default_monitor) {
4070 add_device_config(DEV_SERIAL, "mon:stdio");
4071 } else if (default_virtcon && default_monitor) {
4072 add_device_config(DEV_VIRTCON, "mon:stdio");
4073 } else if (default_sclp && default_monitor) {
4074 add_device_config(DEV_SCLP, "mon:stdio");
4075 } else {
4076 if (default_serial)
4077 add_device_config(DEV_SERIAL, "stdio");
4078 if (default_virtcon)
4079 add_device_config(DEV_VIRTCON, "stdio");
4080 if (default_sclp) {
4081 add_device_config(DEV_SCLP, "stdio");
4083 if (default_monitor)
4084 monitor_parse("stdio", "readline", false);
4086 } else {
4087 if (default_serial)
4088 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4089 if (default_parallel)
4090 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4091 if (default_monitor)
4092 monitor_parse("vc:80Cx24C", "readline", false);
4093 if (default_virtcon)
4094 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4095 if (default_sclp) {
4096 add_device_config(DEV_SCLP, "vc:80Cx24C");
4100 #if defined(CONFIG_VNC)
4101 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4102 display_remote++;
4104 #endif
4105 if (display_type == DT_DEFAULT && !display_remote) {
4106 #if defined(CONFIG_GTK)
4107 display_type = DT_GTK;
4108 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4109 display_type = DT_SDL;
4110 #elif defined(CONFIG_VNC)
4111 vnc_parse_func("localhost:0,to=99,id=default");
4112 show_vnc_port = 1;
4113 #else
4114 display_type = DT_NONE;
4115 #endif
4118 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4119 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4120 "for SDL, ignoring option\n");
4122 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4123 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4124 "ignoring option\n");
4127 #if defined(CONFIG_GTK)
4128 if (display_type == DT_GTK) {
4129 early_gtk_display_init(request_opengl);
4131 #endif
4132 #if defined(CONFIG_SDL)
4133 if (display_type == DT_SDL) {
4134 sdl_display_early_init(request_opengl);
4136 #endif
4137 if (request_opengl == 1 && display_opengl == 0) {
4138 #if defined(CONFIG_OPENGL)
4139 fprintf(stderr, "OpenGL is not supported by the display.\n");
4140 #else
4141 fprintf(stderr, "QEMU was built without opengl support.\n");
4142 #endif
4143 exit(1);
4146 socket_init();
4148 if (qemu_opts_foreach(qemu_find_opts("object"),
4149 object_create,
4150 object_create_initial, NULL)) {
4151 exit(1);
4154 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4155 chardev_init_func, NULL, NULL)) {
4156 exit(1);
4159 #ifdef CONFIG_VIRTFS
4160 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4161 fsdev_init_func, NULL, NULL)) {
4162 exit(1);
4164 #endif
4166 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4167 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
4168 exit(1);
4171 if (qemu_opts_foreach(qemu_find_opts("device"),
4172 device_help_func, NULL, NULL)) {
4173 exit(0);
4176 if (qemu_opts_foreach(qemu_find_opts("object"),
4177 object_create,
4178 object_create_delayed, NULL)) {
4179 exit(1);
4182 machine_opts = qemu_get_machine_opts();
4183 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4184 NULL)) {
4185 object_unref(OBJECT(current_machine));
4186 exit(1);
4189 configure_accelerator(current_machine);
4191 if (qtest_chrdev) {
4192 Error *local_err = NULL;
4193 qtest_init(qtest_chrdev, qtest_log, &local_err);
4194 if (local_err) {
4195 error_report_err(local_err);
4196 exit(1);
4200 machine_opts = qemu_get_machine_opts();
4201 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4202 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4203 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4204 bios_name = qemu_opt_get(machine_opts, "firmware");
4206 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4207 if (opts) {
4208 Error *local_err = NULL;
4210 boot_order = qemu_opt_get(opts, "order");
4211 if (boot_order) {
4212 validate_bootdevices(boot_order, &local_err);
4213 if (local_err) {
4214 error_report_err(local_err);
4215 exit(1);
4219 boot_once = qemu_opt_get(opts, "once");
4220 if (boot_once) {
4221 validate_bootdevices(boot_once, &local_err);
4222 if (local_err) {
4223 error_report_err(local_err);
4224 exit(1);
4228 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4229 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4232 if (!boot_order) {
4233 boot_order = machine_class->default_boot_order;
4236 if (!kernel_cmdline) {
4237 kernel_cmdline = "";
4238 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4241 linux_boot = (kernel_filename != NULL);
4243 if (!linux_boot && *kernel_cmdline != '\0') {
4244 fprintf(stderr, "-append only allowed with -kernel option\n");
4245 exit(1);
4248 if (!linux_boot && initrd_filename != NULL) {
4249 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4250 exit(1);
4253 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4254 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4255 exit(1);
4258 os_set_line_buffering();
4260 #ifdef CONFIG_SPICE
4261 /* spice needs the timers to be initialized by this point */
4262 qemu_spice_init();
4263 #endif
4265 cpu_ticks_init();
4266 if (icount_opts) {
4267 if (kvm_enabled() || xen_enabled()) {
4268 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4269 exit(1);
4271 configure_icount(icount_opts, &error_abort);
4272 qemu_opts_del(icount_opts);
4275 /* clean up network at qemu process termination */
4276 atexit(&net_cleanup);
4278 if (net_init_clients() < 0) {
4279 exit(1);
4282 #ifdef CONFIG_TPM
4283 if (tpm_init() < 0) {
4284 exit(1);
4286 #endif
4288 /* init the bluetooth world */
4289 if (foreach_device_config(DEV_BT, bt_parse))
4290 exit(1);
4292 if (!xen_enabled()) {
4293 /* On 32-bit hosts, QEMU is limited by virtual address space */
4294 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4295 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4296 exit(1);
4300 blk_mig_init();
4301 ram_mig_init();
4303 /* If the currently selected machine wishes to override the units-per-bus
4304 * property of its default HBA interface type, do so now. */
4305 if (machine_class->units_per_default_bus) {
4306 override_max_devs(machine_class->block_default_type,
4307 machine_class->units_per_default_bus);
4310 /* open the virtual block devices */
4311 if (snapshot)
4312 qemu_opts_foreach(qemu_find_opts("drive"),
4313 drive_enable_snapshot, NULL, NULL);
4314 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4315 &machine_class->block_default_type, NULL)) {
4316 exit(1);
4319 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4320 CDROM_OPTS);
4321 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4322 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4324 parse_numa_opts(machine_class);
4326 if (qemu_opts_foreach(qemu_find_opts("mon"),
4327 mon_init_func, NULL, NULL)) {
4328 exit(1);
4331 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4332 exit(1);
4333 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4334 exit(1);
4335 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4336 exit(1);
4337 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4338 exit(1);
4340 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4341 exit(1);
4343 /* If no default VGA is requested, the default is "none". */
4344 if (default_vga) {
4345 if (machine_class->default_display) {
4346 vga_model = machine_class->default_display;
4347 } else if (cirrus_vga_available()) {
4348 vga_model = "cirrus";
4349 } else if (vga_available()) {
4350 vga_model = "std";
4353 if (vga_model) {
4354 select_vgahw(vga_model);
4357 if (watchdog) {
4358 i = select_watchdog(watchdog);
4359 if (i > 0)
4360 exit (i == 1 ? 1 : 0);
4363 if (machine_class->compat_props) {
4364 qdev_prop_register_global_list(machine_class->compat_props);
4366 qemu_add_globals();
4368 qdev_machine_init();
4370 current_machine->ram_size = ram_size;
4371 current_machine->maxram_size = maxram_size;
4372 current_machine->ram_slots = ram_slots;
4373 current_machine->boot_order = boot_order;
4374 current_machine->cpu_model = cpu_model;
4376 machine_class->init(current_machine);
4378 realtime_init();
4380 audio_init();
4382 cpu_synchronize_all_post_init();
4384 numa_post_machine_init();
4386 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4387 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4388 exit(1);
4391 /* init USB devices */
4392 if (usb_enabled()) {
4393 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4394 exit(1);
4397 /* init generic devices */
4398 if (qemu_opts_foreach(qemu_find_opts("device"),
4399 device_init_func, NULL, NULL)) {
4400 exit(1);
4403 /* Did we create any drives that we failed to create a device for? */
4404 drive_check_orphaned();
4406 net_check_clients();
4408 if (boot_once) {
4409 Error *local_err = NULL;
4410 qemu_boot_set(boot_once, &local_err);
4411 if (local_err) {
4412 error_report("%s", error_get_pretty(local_err));
4413 exit(1);
4415 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4418 ds = init_displaystate();
4420 /* init local displays */
4421 switch (display_type) {
4422 case DT_NOGRAPHIC:
4423 (void)ds; /* avoid warning if no display is configured */
4424 break;
4425 #if defined(CONFIG_CURSES)
4426 case DT_CURSES:
4427 curses_display_init(ds, full_screen);
4428 break;
4429 #endif
4430 #if defined(CONFIG_SDL)
4431 case DT_SDL:
4432 sdl_display_init(ds, full_screen, no_frame);
4433 break;
4434 #elif defined(CONFIG_COCOA)
4435 case DT_SDL:
4436 cocoa_display_init(ds, full_screen);
4437 break;
4438 #endif
4439 #if defined(CONFIG_GTK)
4440 case DT_GTK:
4441 gtk_display_init(ds, full_screen, grab_on_hover);
4442 break;
4443 #endif
4444 default:
4445 break;
4448 /* must be after terminal init, SDL library changes signal handlers */
4449 os_setup_signal_handling();
4451 #ifdef CONFIG_VNC
4452 /* init remote displays */
4453 qemu_opts_foreach(qemu_find_opts("vnc"),
4454 vnc_init_func, NULL, NULL);
4455 if (show_vnc_port) {
4456 char *ret = vnc_display_local_addr("default");
4457 printf("VNC server running on `%s'\n", ret);
4458 g_free(ret);
4460 #endif
4461 #ifdef CONFIG_SPICE
4462 if (using_spice) {
4463 qemu_spice_display_init();
4465 #endif
4467 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4468 exit(1);
4471 qdev_machine_creation_done();
4473 if (rom_load_all() != 0) {
4474 fprintf(stderr, "rom loading failed\n");
4475 exit(1);
4478 /* TODO: once all bus devices are qdevified, this should be done
4479 * when bus is created by qdev.c */
4480 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4481 qemu_run_machine_init_done_notifiers();
4483 /* Done notifiers can load ROMs */
4484 rom_load_done();
4486 qemu_system_reset(VMRESET_SILENT);
4487 if (loadvm) {
4488 if (load_vmstate(loadvm) < 0) {
4489 autostart = 0;
4493 qdev_prop_check_globals();
4494 if (vmstate_dump_file) {
4495 /* dump and exit */
4496 dump_vmstate_json_to_file(vmstate_dump_file);
4497 return 0;
4500 if (incoming) {
4501 Error *local_err = NULL;
4502 qemu_start_incoming_migration(incoming, &local_err);
4503 if (local_err) {
4504 error_report("-incoming %s: %s", incoming,
4505 error_get_pretty(local_err));
4506 error_free(local_err);
4507 exit(1);
4509 } else if (autostart) {
4510 vm_start();
4513 os_setup_post();
4515 if (is_daemonized()) {
4516 if (!trace_init_backends(trace_events, trace_file)) {
4517 exit(1);
4521 main_loop();
4522 bdrv_close_all();
4523 pause_all_vcpus();
4524 res_free();
4525 #ifdef CONFIG_TPM
4526 tpm_cleanup();
4527 #endif
4529 return 0;