virtio: make features 64bit wide
[qemu/ar7.git] / vl.c
blob1d4c0890f22c914826368f52bd139d8eb3fa8fd2
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 DEFAULT_RAM_SIZE 128
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 DisplayType display_type = DT_DEFAULT;
133 int request_opengl = -1;
134 int display_opengl;
135 static int display_remote;
136 const char* keyboard_layout = NULL;
137 ram_addr_t ram_size;
138 const char *mem_path = NULL;
139 int mem_prealloc = 0; /* force preallocation of physical target memory */
140 bool enable_mlock = false;
141 int nb_nics;
142 NICInfo nd_table[MAX_NICS];
143 int autostart;
144 static int rtc_utc = 1;
145 static int rtc_date_offset = -1; /* -1 means no change */
146 QEMUClockType rtc_clock;
147 int vga_interface_type = VGA_NONE;
148 static int full_screen = 0;
149 static int no_frame = 0;
150 int no_quit = 0;
151 #ifdef CONFIG_GTK
152 static bool grab_on_hover;
153 #endif
154 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
155 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
156 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
157 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
158 int win2k_install_hack = 0;
159 int singlestep = 0;
160 int smp_cpus = 1;
161 int max_cpus = 0;
162 int smp_cores = 1;
163 int smp_threads = 1;
164 int acpi_enabled = 1;
165 int no_hpet = 0;
166 int fd_bootchk = 1;
167 static int no_reboot;
168 int no_shutdown = 0;
169 int cursor_hide = 1;
170 int graphic_rotate = 0;
171 const char *watchdog;
172 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
173 int nb_option_roms;
174 int semihosting_enabled = 0;
175 int old_param = 0;
176 const char *qemu_name;
177 int alt_grab = 0;
178 int ctrl_grab = 0;
179 unsigned int nb_prom_envs = 0;
180 const char *prom_envs[MAX_PROM_ENVS];
181 int boot_menu;
182 bool boot_strict;
183 uint8_t *boot_splash_filedata;
184 size_t boot_splash_filedata_size;
185 uint8_t qemu_extra_params_fw[2];
187 int icount_align_option;
189 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
190 * little-endian "wire format" described in the SMBIOS 2.6 specification.
192 uint8_t qemu_uuid[16];
193 bool qemu_uuid_set;
195 static NotifierList exit_notifiers =
196 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
198 static NotifierList machine_init_done_notifiers =
199 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
201 bool xen_allowed;
202 uint32_t xen_domid;
203 enum xen_mode xen_mode = XEN_EMULATE;
205 static int has_defaults = 1;
206 static int default_serial = 1;
207 static int default_parallel = 1;
208 static int default_virtcon = 1;
209 static int default_sclp = 1;
210 static int default_monitor = 1;
211 static int default_floppy = 1;
212 static int default_cdrom = 1;
213 static int default_sdcard = 1;
214 static int default_vga = 1;
216 static struct {
217 const char *driver;
218 int *flag;
219 } default_list[] = {
220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
222 { .driver = "isa-fdc", .flag = &default_floppy },
223 { .driver = "ide-cd", .flag = &default_cdrom },
224 { .driver = "ide-hd", .flag = &default_cdrom },
225 { .driver = "ide-drive", .flag = &default_cdrom },
226 { .driver = "scsi-cd", .flag = &default_cdrom },
227 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
228 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
229 { .driver = "virtio-serial", .flag = &default_virtcon },
230 { .driver = "VGA", .flag = &default_vga },
231 { .driver = "isa-vga", .flag = &default_vga },
232 { .driver = "cirrus-vga", .flag = &default_vga },
233 { .driver = "isa-cirrus-vga", .flag = &default_vga },
234 { .driver = "vmware-svga", .flag = &default_vga },
235 { .driver = "qxl-vga", .flag = &default_vga },
238 static QemuOptsList qemu_rtc_opts = {
239 .name = "rtc",
240 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
241 .desc = {
243 .name = "base",
244 .type = QEMU_OPT_STRING,
246 .name = "clock",
247 .type = QEMU_OPT_STRING,
249 .name = "driftfix",
250 .type = QEMU_OPT_STRING,
252 { /* end of list */ }
256 static QemuOptsList qemu_sandbox_opts = {
257 .name = "sandbox",
258 .implied_opt_name = "enable",
259 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
260 .desc = {
262 .name = "enable",
263 .type = QEMU_OPT_BOOL,
265 { /* end of list */ }
269 static QemuOptsList qemu_trace_opts = {
270 .name = "trace",
271 .implied_opt_name = "trace",
272 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
273 .desc = {
275 .name = "events",
276 .type = QEMU_OPT_STRING,
278 .name = "file",
279 .type = QEMU_OPT_STRING,
281 { /* end of list */ }
285 static QemuOptsList qemu_option_rom_opts = {
286 .name = "option-rom",
287 .implied_opt_name = "romfile",
288 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
289 .desc = {
291 .name = "bootindex",
292 .type = QEMU_OPT_NUMBER,
293 }, {
294 .name = "romfile",
295 .type = QEMU_OPT_STRING,
297 { /* end of list */ }
301 static QemuOptsList qemu_machine_opts = {
302 .name = "machine",
303 .implied_opt_name = "type",
304 .merge_lists = true,
305 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
306 .desc = {
308 * no elements => accept any
309 * sanity checking will happen later
310 * when setting machine properties
316 static QemuOptsList qemu_boot_opts = {
317 .name = "boot-opts",
318 .implied_opt_name = "order",
319 .merge_lists = true,
320 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
321 .desc = {
323 .name = "order",
324 .type = QEMU_OPT_STRING,
325 }, {
326 .name = "once",
327 .type = QEMU_OPT_STRING,
328 }, {
329 .name = "menu",
330 .type = QEMU_OPT_BOOL,
331 }, {
332 .name = "splash",
333 .type = QEMU_OPT_STRING,
334 }, {
335 .name = "splash-time",
336 .type = QEMU_OPT_STRING,
337 }, {
338 .name = "reboot-timeout",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "strict",
342 .type = QEMU_OPT_BOOL,
344 { /*End of list */ }
348 static QemuOptsList qemu_add_fd_opts = {
349 .name = "add-fd",
350 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
351 .desc = {
353 .name = "fd",
354 .type = QEMU_OPT_NUMBER,
355 .help = "file descriptor of which a duplicate is added to fd set",
357 .name = "set",
358 .type = QEMU_OPT_NUMBER,
359 .help = "ID of the fd set to add fd to",
361 .name = "opaque",
362 .type = QEMU_OPT_STRING,
363 .help = "free-form string used to describe fd",
365 { /* end of list */ }
369 static QemuOptsList qemu_object_opts = {
370 .name = "object",
371 .implied_opt_name = "qom-type",
372 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
373 .desc = {
378 static QemuOptsList qemu_tpmdev_opts = {
379 .name = "tpmdev",
380 .implied_opt_name = "type",
381 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
382 .desc = {
383 /* options are defined in the TPM backends */
384 { /* end of list */ }
388 static QemuOptsList qemu_realtime_opts = {
389 .name = "realtime",
390 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
391 .desc = {
393 .name = "mlock",
394 .type = QEMU_OPT_BOOL,
396 { /* end of list */ }
400 static QemuOptsList qemu_msg_opts = {
401 .name = "msg",
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
403 .desc = {
405 .name = "timestamp",
406 .type = QEMU_OPT_BOOL,
408 { /* end of list */ }
412 static QemuOptsList qemu_name_opts = {
413 .name = "name",
414 .implied_opt_name = "guest",
415 .merge_lists = true,
416 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
417 .desc = {
419 .name = "guest",
420 .type = QEMU_OPT_STRING,
421 .help = "Sets the name of the guest.\n"
422 "This name will be displayed in the SDL window caption.\n"
423 "The name will also be used for the VNC server",
424 }, {
425 .name = "process",
426 .type = QEMU_OPT_STRING,
427 .help = "Sets the name of the QEMU process, as shown in top etc",
428 }, {
429 .name = "debug-threads",
430 .type = QEMU_OPT_BOOL,
431 .help = "When enabled, name the individual threads; defaults off.\n"
432 "NOTE: The thread names are for debugging and not a\n"
433 "stable API.",
435 { /* End of list */ }
439 static QemuOptsList qemu_mem_opts = {
440 .name = "memory",
441 .implied_opt_name = "size",
442 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
443 .merge_lists = true,
444 .desc = {
446 .name = "size",
447 .type = QEMU_OPT_SIZE,
450 .name = "slots",
451 .type = QEMU_OPT_NUMBER,
454 .name = "maxmem",
455 .type = QEMU_OPT_SIZE,
457 { /* end of list */ }
461 static QemuOptsList qemu_icount_opts = {
462 .name = "icount",
463 .implied_opt_name = "shift",
464 .merge_lists = true,
465 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
466 .desc = {
468 .name = "shift",
469 .type = QEMU_OPT_STRING,
470 }, {
471 .name = "align",
472 .type = QEMU_OPT_BOOL,
474 { /* end of list */ }
478 static QemuOptsList qemu_semihosting_config_opts = {
479 .name = "semihosting-config",
480 .implied_opt_name = "enable",
481 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
482 .desc = {
484 .name = "enable",
485 .type = QEMU_OPT_BOOL,
486 }, {
487 .name = "target",
488 .type = QEMU_OPT_STRING,
490 { /* end of list */ }
495 * Get machine options
497 * Returns: machine options (never null).
499 QemuOpts *qemu_get_machine_opts(void)
501 return qemu_find_opts_singleton("machine");
504 const char *qemu_get_vm_name(void)
506 return qemu_name;
509 static void res_free(void)
511 if (boot_splash_filedata != NULL) {
512 g_free(boot_splash_filedata);
513 boot_splash_filedata = NULL;
517 static int default_driver_check(QemuOpts *opts, void *opaque)
519 const char *driver = qemu_opt_get(opts, "driver");
520 int i;
522 if (!driver)
523 return 0;
524 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
525 if (strcmp(default_list[i].driver, driver) != 0)
526 continue;
527 *(default_list[i].flag) = 0;
529 return 0;
532 /***********************************************************/
533 /* QEMU state */
535 static RunState current_run_state = RUN_STATE_PRELAUNCH;
537 /* We use RUN_STATE_MAX but any invalid value will do */
538 static RunState vmstop_requested = RUN_STATE_MAX;
539 static QemuMutex vmstop_lock;
541 typedef struct {
542 RunState from;
543 RunState to;
544 } RunStateTransition;
546 static const RunStateTransition runstate_transitions_def[] = {
547 /* from -> to */
548 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
549 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
551 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
552 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
554 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
555 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
557 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
558 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
560 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
561 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
563 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
564 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
566 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
567 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
568 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
570 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
571 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
573 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
575 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
576 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
577 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
578 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
579 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
580 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
581 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
582 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
583 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
584 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
586 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
588 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
589 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
591 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
592 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
593 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
594 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
596 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
597 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
600 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_MAX, RUN_STATE_MAX },
605 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
607 bool runstate_check(RunState state)
609 return current_run_state == state;
612 static void runstate_init(void)
614 const RunStateTransition *p;
616 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
617 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
618 runstate_valid_transitions[p->from][p->to] = true;
621 qemu_mutex_init(&vmstop_lock);
624 /* This function will abort() on invalid state transitions */
625 void runstate_set(RunState new_state)
627 assert(new_state < RUN_STATE_MAX);
629 if (!runstate_valid_transitions[current_run_state][new_state]) {
630 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
631 RunState_lookup[current_run_state],
632 RunState_lookup[new_state]);
633 abort();
635 trace_runstate_set(new_state);
636 current_run_state = new_state;
639 int runstate_is_running(void)
641 return runstate_check(RUN_STATE_RUNNING);
644 bool runstate_needs_reset(void)
646 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
647 runstate_check(RUN_STATE_SHUTDOWN);
650 StatusInfo *qmp_query_status(Error **errp)
652 StatusInfo *info = g_malloc0(sizeof(*info));
654 info->running = runstate_is_running();
655 info->singlestep = singlestep;
656 info->status = current_run_state;
658 return info;
661 static bool qemu_vmstop_requested(RunState *r)
663 qemu_mutex_lock(&vmstop_lock);
664 *r = vmstop_requested;
665 vmstop_requested = RUN_STATE_MAX;
666 qemu_mutex_unlock(&vmstop_lock);
667 return *r < RUN_STATE_MAX;
670 void qemu_system_vmstop_request_prepare(void)
672 qemu_mutex_lock(&vmstop_lock);
675 void qemu_system_vmstop_request(RunState state)
677 vmstop_requested = state;
678 qemu_mutex_unlock(&vmstop_lock);
679 qemu_notify_event();
682 void vm_start(void)
684 RunState requested;
686 qemu_vmstop_requested(&requested);
687 if (runstate_is_running() && requested == RUN_STATE_MAX) {
688 return;
691 /* Ensure that a STOP/RESUME pair of events is emitted if a
692 * vmstop request was pending. The BLOCK_IO_ERROR event, for
693 * example, according to documentation is always followed by
694 * the STOP event.
696 if (runstate_is_running()) {
697 qapi_event_send_stop(&error_abort);
698 } else {
699 cpu_enable_ticks();
700 runstate_set(RUN_STATE_RUNNING);
701 vm_state_notify(1, RUN_STATE_RUNNING);
702 resume_all_vcpus();
705 qapi_event_send_resume(&error_abort);
709 /***********************************************************/
710 /* real time host monotonic timer */
712 static time_t qemu_time(void)
714 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
717 /***********************************************************/
718 /* host time/date access */
719 void qemu_get_timedate(struct tm *tm, int offset)
721 time_t ti = qemu_time();
723 ti += offset;
724 if (rtc_date_offset == -1) {
725 if (rtc_utc)
726 gmtime_r(&ti, tm);
727 else
728 localtime_r(&ti, tm);
729 } else {
730 ti -= rtc_date_offset;
731 gmtime_r(&ti, tm);
735 int qemu_timedate_diff(struct tm *tm)
737 time_t seconds;
739 if (rtc_date_offset == -1)
740 if (rtc_utc)
741 seconds = mktimegm(tm);
742 else {
743 struct tm tmp = *tm;
744 tmp.tm_isdst = -1; /* use timezone to figure it out */
745 seconds = mktime(&tmp);
747 else
748 seconds = mktimegm(tm) + rtc_date_offset;
750 return seconds - qemu_time();
753 static void configure_rtc_date_offset(const char *startdate, int legacy)
755 time_t rtc_start_date;
756 struct tm tm;
758 if (!strcmp(startdate, "now") && legacy) {
759 rtc_date_offset = -1;
760 } else {
761 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
762 &tm.tm_year,
763 &tm.tm_mon,
764 &tm.tm_mday,
765 &tm.tm_hour,
766 &tm.tm_min,
767 &tm.tm_sec) == 6) {
768 /* OK */
769 } else if (sscanf(startdate, "%d-%d-%d",
770 &tm.tm_year,
771 &tm.tm_mon,
772 &tm.tm_mday) == 3) {
773 tm.tm_hour = 0;
774 tm.tm_min = 0;
775 tm.tm_sec = 0;
776 } else {
777 goto date_fail;
779 tm.tm_year -= 1900;
780 tm.tm_mon--;
781 rtc_start_date = mktimegm(&tm);
782 if (rtc_start_date == -1) {
783 date_fail:
784 fprintf(stderr, "Invalid date format. Valid formats are:\n"
785 "'2006-06-17T16:01:21' or '2006-06-17'\n");
786 exit(1);
788 rtc_date_offset = qemu_time() - rtc_start_date;
792 static void configure_rtc(QemuOpts *opts)
794 const char *value;
796 value = qemu_opt_get(opts, "base");
797 if (value) {
798 if (!strcmp(value, "utc")) {
799 rtc_utc = 1;
800 } else if (!strcmp(value, "localtime")) {
801 rtc_utc = 0;
802 } else {
803 configure_rtc_date_offset(value, 0);
806 value = qemu_opt_get(opts, "clock");
807 if (value) {
808 if (!strcmp(value, "host")) {
809 rtc_clock = QEMU_CLOCK_HOST;
810 } else if (!strcmp(value, "rt")) {
811 rtc_clock = QEMU_CLOCK_REALTIME;
812 } else if (!strcmp(value, "vm")) {
813 rtc_clock = QEMU_CLOCK_VIRTUAL;
814 } else {
815 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
816 exit(1);
819 value = qemu_opt_get(opts, "driftfix");
820 if (value) {
821 if (!strcmp(value, "slew")) {
822 static GlobalProperty slew_lost_ticks[] = {
824 .driver = "mc146818rtc",
825 .property = "lost_tick_policy",
826 .value = "slew",
828 { /* end of list */ }
831 qdev_prop_register_global_list(slew_lost_ticks);
832 } else if (!strcmp(value, "none")) {
833 /* discard is default */
834 } else {
835 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
836 exit(1);
841 /***********************************************************/
842 /* Bluetooth support */
843 static int nb_hcis;
844 static int cur_hci;
845 static struct HCIInfo *hci_table[MAX_NICS];
847 struct HCIInfo *qemu_next_hci(void)
849 if (cur_hci == nb_hcis)
850 return &null_hci;
852 return hci_table[cur_hci++];
855 static int bt_hci_parse(const char *str)
857 struct HCIInfo *hci;
858 bdaddr_t bdaddr;
860 if (nb_hcis >= MAX_NICS) {
861 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
862 return -1;
865 hci = hci_init(str);
866 if (!hci)
867 return -1;
869 bdaddr.b[0] = 0x52;
870 bdaddr.b[1] = 0x54;
871 bdaddr.b[2] = 0x00;
872 bdaddr.b[3] = 0x12;
873 bdaddr.b[4] = 0x34;
874 bdaddr.b[5] = 0x56 + nb_hcis;
875 hci->bdaddr_set(hci, bdaddr.b);
877 hci_table[nb_hcis++] = hci;
879 return 0;
882 static void bt_vhci_add(int vlan_id)
884 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
886 if (!vlan->slave)
887 fprintf(stderr, "qemu: warning: adding a VHCI to "
888 "an empty scatternet %i\n", vlan_id);
890 bt_vhci_init(bt_new_hci(vlan));
893 static struct bt_device_s *bt_device_add(const char *opt)
895 struct bt_scatternet_s *vlan;
896 int vlan_id = 0;
897 char *endp = strstr(opt, ",vlan=");
898 int len = (endp ? endp - opt : strlen(opt)) + 1;
899 char devname[10];
901 pstrcpy(devname, MIN(sizeof(devname), len), opt);
903 if (endp) {
904 vlan_id = strtol(endp + 6, &endp, 0);
905 if (*endp) {
906 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
907 return 0;
911 vlan = qemu_find_bt_vlan(vlan_id);
913 if (!vlan->slave)
914 fprintf(stderr, "qemu: warning: adding a slave device to "
915 "an empty scatternet %i\n", vlan_id);
917 if (!strcmp(devname, "keyboard"))
918 return bt_keyboard_init(vlan);
920 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
921 return 0;
924 static int bt_parse(const char *opt)
926 const char *endp, *p;
927 int vlan;
929 if (strstart(opt, "hci", &endp)) {
930 if (!*endp || *endp == ',') {
931 if (*endp)
932 if (!strstart(endp, ",vlan=", 0))
933 opt = endp + 1;
935 return bt_hci_parse(opt);
937 } else if (strstart(opt, "vhci", &endp)) {
938 if (!*endp || *endp == ',') {
939 if (*endp) {
940 if (strstart(endp, ",vlan=", &p)) {
941 vlan = strtol(p, (char **) &endp, 0);
942 if (*endp) {
943 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
944 return 1;
946 } else {
947 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
948 return 1;
950 } else
951 vlan = 0;
953 bt_vhci_add(vlan);
954 return 0;
956 } else if (strstart(opt, "device:", &endp))
957 return !bt_device_add(endp);
959 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
960 return 1;
963 static int parse_sandbox(QemuOpts *opts, void *opaque)
965 /* FIXME: change this to true for 1.3 */
966 if (qemu_opt_get_bool(opts, "enable", false)) {
967 #ifdef CONFIG_SECCOMP
968 if (seccomp_start() < 0) {
969 qerror_report(ERROR_CLASS_GENERIC_ERROR,
970 "failed to install seccomp syscall filter in the kernel");
971 return -1;
973 #else
974 qerror_report(ERROR_CLASS_GENERIC_ERROR,
975 "sandboxing request but seccomp is not compiled into this build");
976 return -1;
977 #endif
980 return 0;
983 static int parse_name(QemuOpts *opts, void *opaque)
985 const char *proc_name;
987 if (qemu_opt_get(opts, "debug-threads")) {
988 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
990 qemu_name = qemu_opt_get(opts, "guest");
992 proc_name = qemu_opt_get(opts, "process");
993 if (proc_name) {
994 os_set_proc_name(proc_name);
997 return 0;
1000 bool defaults_enabled(void)
1002 return has_defaults;
1005 bool usb_enabled(void)
1007 return machine_usb(current_machine);
1010 #ifndef _WIN32
1011 static int parse_add_fd(QemuOpts *opts, void *opaque)
1013 int fd, dupfd, flags;
1014 int64_t fdset_id;
1015 const char *fd_opaque = NULL;
1016 AddfdInfo *fdinfo;
1018 fd = qemu_opt_get_number(opts, "fd", -1);
1019 fdset_id = qemu_opt_get_number(opts, "set", -1);
1020 fd_opaque = qemu_opt_get(opts, "opaque");
1022 if (fd < 0) {
1023 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1024 "fd option is required and must be non-negative");
1025 return -1;
1028 if (fd <= STDERR_FILENO) {
1029 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1030 "fd cannot be a standard I/O stream");
1031 return -1;
1035 * All fds inherited across exec() necessarily have FD_CLOEXEC
1036 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1038 flags = fcntl(fd, F_GETFD);
1039 if (flags == -1 || (flags & FD_CLOEXEC)) {
1040 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1041 "fd is not valid or already in use");
1042 return -1;
1045 if (fdset_id < 0) {
1046 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1047 "set option is required and must be non-negative");
1048 return -1;
1051 #ifdef F_DUPFD_CLOEXEC
1052 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1053 #else
1054 dupfd = dup(fd);
1055 if (dupfd != -1) {
1056 qemu_set_cloexec(dupfd);
1058 #endif
1059 if (dupfd == -1) {
1060 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1061 "Error duplicating fd: %s", strerror(errno));
1062 return -1;
1065 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1066 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1067 &error_abort);
1068 g_free(fdinfo);
1070 return 0;
1073 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1075 int fd;
1077 fd = qemu_opt_get_number(opts, "fd", -1);
1078 close(fd);
1080 return 0;
1082 #endif
1084 /***********************************************************/
1085 /* QEMU Block devices */
1087 #define HD_OPTS "media=disk"
1088 #define CDROM_OPTS "media=cdrom"
1089 #define FD_OPTS ""
1090 #define PFLASH_OPTS ""
1091 #define MTD_OPTS ""
1092 #define SD_OPTS ""
1094 static int drive_init_func(QemuOpts *opts, void *opaque)
1096 BlockInterfaceType *block_default_type = opaque;
1098 return drive_new(opts, *block_default_type) == NULL;
1101 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1103 if (qemu_opt_get(opts, "snapshot") == NULL) {
1104 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1106 return 0;
1109 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1110 int index, const char *optstr)
1112 QemuOpts *opts;
1113 DriveInfo *dinfo;
1115 if (!enable || drive_get_by_index(type, index)) {
1116 return;
1119 opts = drive_add(type, index, NULL, optstr);
1120 if (snapshot) {
1121 drive_enable_snapshot(opts, NULL);
1124 dinfo = drive_new(opts, type);
1125 if (!dinfo) {
1126 exit(1);
1128 dinfo->is_default = true;
1132 static QemuOptsList qemu_smp_opts = {
1133 .name = "smp-opts",
1134 .implied_opt_name = "cpus",
1135 .merge_lists = true,
1136 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1137 .desc = {
1139 .name = "cpus",
1140 .type = QEMU_OPT_NUMBER,
1141 }, {
1142 .name = "sockets",
1143 .type = QEMU_OPT_NUMBER,
1144 }, {
1145 .name = "cores",
1146 .type = QEMU_OPT_NUMBER,
1147 }, {
1148 .name = "threads",
1149 .type = QEMU_OPT_NUMBER,
1150 }, {
1151 .name = "maxcpus",
1152 .type = QEMU_OPT_NUMBER,
1154 { /*End of list */ }
1158 static void smp_parse(QemuOpts *opts)
1160 if (opts) {
1162 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1163 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1164 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1165 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1167 /* compute missing values, prefer sockets over cores over threads */
1168 if (cpus == 0 || sockets == 0) {
1169 sockets = sockets > 0 ? sockets : 1;
1170 cores = cores > 0 ? cores : 1;
1171 threads = threads > 0 ? threads : 1;
1172 if (cpus == 0) {
1173 cpus = cores * threads * sockets;
1175 } else if (cores == 0) {
1176 threads = threads > 0 ? threads : 1;
1177 cores = cpus / (sockets * threads);
1178 } else if (threads == 0) {
1179 threads = cpus / (cores * sockets);
1180 } else if (sockets * cores * threads < cpus) {
1181 fprintf(stderr, "cpu topology: error: "
1182 "sockets (%u) * cores (%u) * threads (%u) < "
1183 "smp_cpus (%u)\n",
1184 sockets, cores, threads, cpus);
1185 exit(1);
1188 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1190 smp_cpus = cpus;
1191 smp_cores = cores > 0 ? cores : 1;
1192 smp_threads = threads > 0 ? threads : 1;
1196 if (max_cpus == 0) {
1197 max_cpus = smp_cpus;
1200 if (max_cpus > MAX_CPUMASK_BITS) {
1201 fprintf(stderr, "Unsupported number of maxcpus\n");
1202 exit(1);
1204 if (max_cpus < smp_cpus) {
1205 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1206 exit(1);
1211 static void realtime_init(void)
1213 if (enable_mlock) {
1214 if (os_mlock() < 0) {
1215 fprintf(stderr, "qemu: locking memory failed\n");
1216 exit(1);
1222 static void configure_msg(QemuOpts *opts)
1224 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1227 /***********************************************************/
1228 /* USB devices */
1230 static int usb_device_add(const char *devname)
1232 USBDevice *dev = NULL;
1233 #ifndef CONFIG_LINUX
1234 const char *p;
1235 #endif
1237 if (!usb_enabled()) {
1238 return -1;
1241 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1242 dev = usbdevice_create(devname);
1243 if (dev)
1244 goto done;
1246 /* the other ones */
1247 #ifndef CONFIG_LINUX
1248 /* only the linux version is qdev-ified, usb-bsd still needs this */
1249 if (strstart(devname, "host:", &p)) {
1250 dev = usb_host_device_open(usb_bus_find(-1), p);
1252 #endif
1253 if (!dev)
1254 return -1;
1256 done:
1257 return 0;
1260 static int usb_device_del(const char *devname)
1262 int bus_num, addr;
1263 const char *p;
1265 if (strstart(devname, "host:", &p)) {
1266 return -1;
1269 if (!usb_enabled()) {
1270 return -1;
1273 p = strchr(devname, '.');
1274 if (!p)
1275 return -1;
1276 bus_num = strtoul(devname, NULL, 0);
1277 addr = strtoul(p + 1, NULL, 0);
1279 return usb_device_delete_addr(bus_num, addr);
1282 static int usb_parse(const char *cmdline)
1284 int r;
1285 r = usb_device_add(cmdline);
1286 if (r < 0) {
1287 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1289 return r;
1292 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1294 const char *devname = qdict_get_str(qdict, "devname");
1295 if (usb_device_add(devname) < 0) {
1296 error_report("could not add USB device '%s'", devname);
1300 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1302 const char *devname = qdict_get_str(qdict, "devname");
1303 if (usb_device_del(devname) < 0) {
1304 error_report("could not delete USB device '%s'", devname);
1308 /***********************************************************/
1309 /* machine registration */
1311 MachineState *current_machine;
1313 static void machine_class_init(ObjectClass *oc, void *data)
1315 MachineClass *mc = MACHINE_CLASS(oc);
1316 QEMUMachine *qm = data;
1317 mc->name = qm->name;
1318 mc->desc = qm->desc;
1319 mc->init = qm->init;
1320 mc->kvm_type = qm->kvm_type;
1321 mc->block_default_type = qm->block_default_type;
1322 mc->max_cpus = qm->max_cpus;
1323 mc->no_sdcard = qm->no_sdcard;
1324 mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1325 mc->is_default = qm->is_default;
1326 mc->default_machine_opts = qm->default_machine_opts;
1327 mc->default_boot_order = qm->default_boot_order;
1330 int qemu_register_machine(QEMUMachine *m)
1332 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1333 TypeInfo ti = {
1334 .name = name,
1335 .parent = TYPE_MACHINE,
1336 .class_init = machine_class_init,
1337 .class_data = (void *)m,
1340 type_register(&ti);
1341 g_free(name);
1343 return 0;
1346 static MachineClass *find_machine(const char *name)
1348 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1349 MachineClass *mc = NULL;
1351 for (el = machines; el; el = el->next) {
1352 MachineClass *temp = el->data;
1354 if (!strcmp(temp->name, name)) {
1355 mc = temp;
1356 break;
1358 if (temp->alias &&
1359 !strcmp(temp->alias, name)) {
1360 mc = temp;
1361 break;
1365 g_slist_free(machines);
1366 return mc;
1369 MachineClass *find_default_machine(void)
1371 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1372 MachineClass *mc = NULL;
1374 for (el = machines; el; el = el->next) {
1375 MachineClass *temp = el->data;
1377 if (temp->is_default) {
1378 mc = temp;
1379 break;
1383 g_slist_free(machines);
1384 return mc;
1387 MachineInfoList *qmp_query_machines(Error **errp)
1389 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1390 MachineInfoList *mach_list = NULL;
1392 for (el = machines; el; el = el->next) {
1393 MachineClass *mc = el->data;
1394 MachineInfoList *entry;
1395 MachineInfo *info;
1397 info = g_malloc0(sizeof(*info));
1398 if (mc->is_default) {
1399 info->has_is_default = true;
1400 info->is_default = true;
1403 if (mc->alias) {
1404 info->has_alias = true;
1405 info->alias = g_strdup(mc->alias);
1408 info->name = g_strdup(mc->name);
1409 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1411 entry = g_malloc0(sizeof(*entry));
1412 entry->value = info;
1413 entry->next = mach_list;
1414 mach_list = entry;
1417 g_slist_free(machines);
1418 return mach_list;
1421 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1423 ObjectProperty *prop;
1425 if (!qemu_opt_has_help_opt(opts)) {
1426 return 0;
1429 QTAILQ_FOREACH(prop, &OBJECT(machine)->properties, node) {
1430 if (!prop->set) {
1431 continue;
1434 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1435 prop->name, prop->type);
1436 if (prop->description) {
1437 error_printf(" (%s)\n", prop->description);
1438 } else {
1439 error_printf("\n");
1443 return 1;
1446 /***********************************************************/
1447 /* main execution loop */
1449 struct vm_change_state_entry {
1450 VMChangeStateHandler *cb;
1451 void *opaque;
1452 QLIST_ENTRY (vm_change_state_entry) entries;
1455 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1457 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1458 void *opaque)
1460 VMChangeStateEntry *e;
1462 e = g_malloc0(sizeof (*e));
1464 e->cb = cb;
1465 e->opaque = opaque;
1466 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1467 return e;
1470 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1472 QLIST_REMOVE (e, entries);
1473 g_free (e);
1476 void vm_state_notify(int running, RunState state)
1478 VMChangeStateEntry *e, *next;
1480 trace_vm_state_notify(running, state);
1482 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1483 e->cb(e->opaque, running, state);
1487 /* reset/shutdown handler */
1489 typedef struct QEMUResetEntry {
1490 QTAILQ_ENTRY(QEMUResetEntry) entry;
1491 QEMUResetHandler *func;
1492 void *opaque;
1493 } QEMUResetEntry;
1495 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1496 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1497 static int reset_requested;
1498 static int shutdown_requested, shutdown_signal = -1;
1499 static pid_t shutdown_pid;
1500 static int powerdown_requested;
1501 static int debug_requested;
1502 static int suspend_requested;
1503 static WakeupReason wakeup_reason;
1504 static NotifierList powerdown_notifiers =
1505 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1506 static NotifierList suspend_notifiers =
1507 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1508 static NotifierList wakeup_notifiers =
1509 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1510 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1512 int qemu_shutdown_requested_get(void)
1514 return shutdown_requested;
1517 int qemu_reset_requested_get(void)
1519 return reset_requested;
1522 static int qemu_shutdown_requested(void)
1524 return atomic_xchg(&shutdown_requested, 0);
1527 static void qemu_kill_report(void)
1529 if (!qtest_driver() && shutdown_signal != -1) {
1530 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1531 if (shutdown_pid == 0) {
1532 /* This happens for eg ^C at the terminal, so it's worth
1533 * avoiding printing an odd message in that case.
1535 fputc('\n', stderr);
1536 } else {
1537 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1539 shutdown_signal = -1;
1543 static int qemu_reset_requested(void)
1545 int r = reset_requested;
1546 reset_requested = 0;
1547 return r;
1550 static int qemu_suspend_requested(void)
1552 int r = suspend_requested;
1553 suspend_requested = 0;
1554 return r;
1557 static WakeupReason qemu_wakeup_requested(void)
1559 return wakeup_reason;
1562 static int qemu_powerdown_requested(void)
1564 int r = powerdown_requested;
1565 powerdown_requested = 0;
1566 return r;
1569 static int qemu_debug_requested(void)
1571 int r = debug_requested;
1572 debug_requested = 0;
1573 return r;
1576 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1578 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1580 re->func = func;
1581 re->opaque = opaque;
1582 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1585 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1587 QEMUResetEntry *re;
1589 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1590 if (re->func == func && re->opaque == opaque) {
1591 QTAILQ_REMOVE(&reset_handlers, re, entry);
1592 g_free(re);
1593 return;
1598 void qemu_devices_reset(void)
1600 QEMUResetEntry *re, *nre;
1602 /* reset all devices */
1603 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1604 re->func(re->opaque);
1608 void qemu_system_reset(bool report)
1610 MachineClass *mc;
1612 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1614 if (mc && mc->reset) {
1615 mc->reset();
1616 } else {
1617 qemu_devices_reset();
1619 if (report) {
1620 qapi_event_send_reset(&error_abort);
1622 cpu_synchronize_all_post_reset();
1625 void qemu_system_reset_request(void)
1627 if (no_reboot) {
1628 shutdown_requested = 1;
1629 } else {
1630 reset_requested = 1;
1632 cpu_stop_current();
1633 qemu_notify_event();
1636 static void qemu_system_suspend(void)
1638 pause_all_vcpus();
1639 notifier_list_notify(&suspend_notifiers, NULL);
1640 runstate_set(RUN_STATE_SUSPENDED);
1641 qapi_event_send_suspend(&error_abort);
1644 void qemu_system_suspend_request(void)
1646 if (runstate_check(RUN_STATE_SUSPENDED)) {
1647 return;
1649 suspend_requested = 1;
1650 cpu_stop_current();
1651 qemu_notify_event();
1654 void qemu_register_suspend_notifier(Notifier *notifier)
1656 notifier_list_add(&suspend_notifiers, notifier);
1659 void qemu_system_wakeup_request(WakeupReason reason)
1661 trace_system_wakeup_request(reason);
1663 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1664 return;
1666 if (!(wakeup_reason_mask & (1 << reason))) {
1667 return;
1669 runstate_set(RUN_STATE_RUNNING);
1670 wakeup_reason = reason;
1671 qemu_notify_event();
1674 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1676 if (enabled) {
1677 wakeup_reason_mask |= (1 << reason);
1678 } else {
1679 wakeup_reason_mask &= ~(1 << reason);
1683 void qemu_register_wakeup_notifier(Notifier *notifier)
1685 notifier_list_add(&wakeup_notifiers, notifier);
1688 void qemu_system_killed(int signal, pid_t pid)
1690 shutdown_signal = signal;
1691 shutdown_pid = pid;
1692 no_shutdown = 0;
1693 qemu_system_shutdown_request();
1696 void qemu_system_shutdown_request(void)
1698 trace_qemu_system_shutdown_request();
1699 shutdown_requested = 1;
1700 qemu_notify_event();
1703 static void qemu_system_powerdown(void)
1705 qapi_event_send_powerdown(&error_abort);
1706 notifier_list_notify(&powerdown_notifiers, NULL);
1709 void qemu_system_powerdown_request(void)
1711 trace_qemu_system_powerdown_request();
1712 powerdown_requested = 1;
1713 qemu_notify_event();
1716 void qemu_register_powerdown_notifier(Notifier *notifier)
1718 notifier_list_add(&powerdown_notifiers, notifier);
1721 void qemu_system_debug_request(void)
1723 debug_requested = 1;
1724 qemu_notify_event();
1727 static bool main_loop_should_exit(void)
1729 RunState r;
1730 if (qemu_debug_requested()) {
1731 vm_stop(RUN_STATE_DEBUG);
1733 if (qemu_suspend_requested()) {
1734 qemu_system_suspend();
1736 if (qemu_shutdown_requested()) {
1737 qemu_kill_report();
1738 qapi_event_send_shutdown(&error_abort);
1739 if (no_shutdown) {
1740 vm_stop(RUN_STATE_SHUTDOWN);
1741 } else {
1742 return true;
1745 if (qemu_reset_requested()) {
1746 pause_all_vcpus();
1747 cpu_synchronize_all_states();
1748 qemu_system_reset(VMRESET_REPORT);
1749 resume_all_vcpus();
1750 if (runstate_needs_reset()) {
1751 runstate_set(RUN_STATE_PAUSED);
1754 if (qemu_wakeup_requested()) {
1755 pause_all_vcpus();
1756 cpu_synchronize_all_states();
1757 qemu_system_reset(VMRESET_SILENT);
1758 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1759 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1760 resume_all_vcpus();
1761 qapi_event_send_wakeup(&error_abort);
1763 if (qemu_powerdown_requested()) {
1764 qemu_system_powerdown();
1766 if (qemu_vmstop_requested(&r)) {
1767 vm_stop(r);
1769 return false;
1772 static void main_loop(void)
1774 bool nonblocking;
1775 int last_io = 0;
1776 #ifdef CONFIG_PROFILER
1777 int64_t ti;
1778 #endif
1779 do {
1780 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1781 #ifdef CONFIG_PROFILER
1782 ti = profile_getclock();
1783 #endif
1784 last_io = main_loop_wait(nonblocking);
1785 #ifdef CONFIG_PROFILER
1786 dev_time += profile_getclock() - ti;
1787 #endif
1788 } while (!main_loop_should_exit());
1791 static void version(void)
1793 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1796 static void help(int exitcode)
1798 version();
1799 printf("usage: %s [options] [disk_image]\n\n"
1800 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1801 error_get_progname());
1803 #define QEMU_OPTIONS_GENERATE_HELP
1804 #include "qemu-options-wrapper.h"
1806 printf("\nDuring emulation, the following keys are useful:\n"
1807 "ctrl-alt-f toggle full screen\n"
1808 "ctrl-alt-n switch to virtual console 'n'\n"
1809 "ctrl-alt toggle mouse and keyboard grab\n"
1810 "\n"
1811 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1813 exit(exitcode);
1816 #define HAS_ARG 0x0001
1818 typedef struct QEMUOption {
1819 const char *name;
1820 int flags;
1821 int index;
1822 uint32_t arch_mask;
1823 } QEMUOption;
1825 static const QEMUOption qemu_options[] = {
1826 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1827 #define QEMU_OPTIONS_GENERATE_OPTIONS
1828 #include "qemu-options-wrapper.h"
1829 { NULL },
1832 static bool vga_available(void)
1834 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1837 static bool cirrus_vga_available(void)
1839 return object_class_by_name("cirrus-vga")
1840 || object_class_by_name("isa-cirrus-vga");
1843 static bool vmware_vga_available(void)
1845 return object_class_by_name("vmware-svga");
1848 static bool qxl_vga_available(void)
1850 return object_class_by_name("qxl-vga");
1853 static bool tcx_vga_available(void)
1855 return object_class_by_name("SUNW,tcx");
1858 static bool cg3_vga_available(void)
1860 return object_class_by_name("cgthree");
1863 static void select_vgahw (const char *p)
1865 const char *opts;
1867 assert(vga_interface_type == VGA_NONE);
1868 if (strstart(p, "std", &opts)) {
1869 if (vga_available()) {
1870 vga_interface_type = VGA_STD;
1871 } else {
1872 fprintf(stderr, "Error: standard VGA not available\n");
1873 exit(0);
1875 } else if (strstart(p, "cirrus", &opts)) {
1876 if (cirrus_vga_available()) {
1877 vga_interface_type = VGA_CIRRUS;
1878 } else {
1879 fprintf(stderr, "Error: Cirrus VGA not available\n");
1880 exit(0);
1882 } else if (strstart(p, "vmware", &opts)) {
1883 if (vmware_vga_available()) {
1884 vga_interface_type = VGA_VMWARE;
1885 } else {
1886 fprintf(stderr, "Error: VMWare SVGA not available\n");
1887 exit(0);
1889 } else if (strstart(p, "xenfb", &opts)) {
1890 vga_interface_type = VGA_XENFB;
1891 } else if (strstart(p, "qxl", &opts)) {
1892 if (qxl_vga_available()) {
1893 vga_interface_type = VGA_QXL;
1894 } else {
1895 fprintf(stderr, "Error: QXL VGA not available\n");
1896 exit(0);
1898 } else if (strstart(p, "tcx", &opts)) {
1899 if (tcx_vga_available()) {
1900 vga_interface_type = VGA_TCX;
1901 } else {
1902 fprintf(stderr, "Error: TCX framebuffer not available\n");
1903 exit(0);
1905 } else if (strstart(p, "cg3", &opts)) {
1906 if (cg3_vga_available()) {
1907 vga_interface_type = VGA_CG3;
1908 } else {
1909 fprintf(stderr, "Error: CG3 framebuffer not available\n");
1910 exit(0);
1912 } else if (!strstart(p, "none", &opts)) {
1913 invalid_vga:
1914 fprintf(stderr, "Unknown vga type: %s\n", p);
1915 exit(1);
1917 while (*opts) {
1918 const char *nextopt;
1920 if (strstart(opts, ",retrace=", &nextopt)) {
1921 opts = nextopt;
1922 if (strstart(opts, "dumb", &nextopt))
1923 vga_retrace_method = VGA_RETRACE_DUMB;
1924 else if (strstart(opts, "precise", &nextopt))
1925 vga_retrace_method = VGA_RETRACE_PRECISE;
1926 else goto invalid_vga;
1927 } else goto invalid_vga;
1928 opts = nextopt;
1932 static DisplayType select_display(const char *p)
1934 const char *opts;
1935 DisplayType display = DT_DEFAULT;
1937 if (strstart(p, "sdl", &opts)) {
1938 #ifdef CONFIG_SDL
1939 display = DT_SDL;
1940 while (*opts) {
1941 const char *nextopt;
1943 if (strstart(opts, ",frame=", &nextopt)) {
1944 opts = nextopt;
1945 if (strstart(opts, "on", &nextopt)) {
1946 no_frame = 0;
1947 } else if (strstart(opts, "off", &nextopt)) {
1948 no_frame = 1;
1949 } else {
1950 goto invalid_sdl_args;
1952 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1953 opts = nextopt;
1954 if (strstart(opts, "on", &nextopt)) {
1955 alt_grab = 1;
1956 } else if (strstart(opts, "off", &nextopt)) {
1957 alt_grab = 0;
1958 } else {
1959 goto invalid_sdl_args;
1961 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1962 opts = nextopt;
1963 if (strstart(opts, "on", &nextopt)) {
1964 ctrl_grab = 1;
1965 } else if (strstart(opts, "off", &nextopt)) {
1966 ctrl_grab = 0;
1967 } else {
1968 goto invalid_sdl_args;
1970 } else if (strstart(opts, ",window_close=", &nextopt)) {
1971 opts = nextopt;
1972 if (strstart(opts, "on", &nextopt)) {
1973 no_quit = 0;
1974 } else if (strstart(opts, "off", &nextopt)) {
1975 no_quit = 1;
1976 } else {
1977 goto invalid_sdl_args;
1979 } else if (strstart(opts, ",gl=", &nextopt)) {
1980 opts = nextopt;
1981 if (strstart(opts, "on", &nextopt)) {
1982 request_opengl = 1;
1983 } else if (strstart(opts, "off", &nextopt)) {
1984 request_opengl = 0;
1985 } else {
1986 goto invalid_sdl_args;
1988 } else {
1989 invalid_sdl_args:
1990 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1991 exit(1);
1993 opts = nextopt;
1995 #else
1996 fprintf(stderr, "SDL support is disabled\n");
1997 exit(1);
1998 #endif
1999 } else if (strstart(p, "vnc", &opts)) {
2000 #ifdef CONFIG_VNC
2001 if (*opts == '=') {
2002 if (vnc_parse_func(opts+1) == NULL) {
2003 exit(1);
2005 } else {
2006 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2007 exit(1);
2009 #else
2010 fprintf(stderr, "VNC support is disabled\n");
2011 exit(1);
2012 #endif
2013 } else if (strstart(p, "curses", &opts)) {
2014 #ifdef CONFIG_CURSES
2015 display = DT_CURSES;
2016 #else
2017 fprintf(stderr, "Curses support is disabled\n");
2018 exit(1);
2019 #endif
2020 } else if (strstart(p, "gtk", &opts)) {
2021 #ifdef CONFIG_GTK
2022 display = DT_GTK;
2023 while (*opts) {
2024 const char *nextopt;
2026 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2027 opts = nextopt;
2028 if (strstart(opts, "on", &nextopt)) {
2029 grab_on_hover = true;
2030 } else if (strstart(opts, "off", &nextopt)) {
2031 grab_on_hover = false;
2032 } else {
2033 goto invalid_gtk_args;
2035 } else if (strstart(opts, ",gl=", &nextopt)) {
2036 opts = nextopt;
2037 if (strstart(opts, "on", &nextopt)) {
2038 request_opengl = 1;
2039 } else if (strstart(opts, "off", &nextopt)) {
2040 request_opengl = 0;
2041 } else {
2042 goto invalid_gtk_args;
2044 } else {
2045 invalid_gtk_args:
2046 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2047 exit(1);
2049 opts = nextopt;
2051 #else
2052 fprintf(stderr, "GTK support is disabled\n");
2053 exit(1);
2054 #endif
2055 } else if (strstart(p, "none", &opts)) {
2056 display = DT_NONE;
2057 } else {
2058 fprintf(stderr, "Unknown display type: %s\n", p);
2059 exit(1);
2062 return display;
2065 static int balloon_parse(const char *arg)
2067 QemuOpts *opts;
2069 if (strcmp(arg, "none") == 0) {
2070 return 0;
2073 if (!strncmp(arg, "virtio", 6)) {
2074 if (arg[6] == ',') {
2075 /* have params -> parse them */
2076 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2077 if (!opts)
2078 return -1;
2079 } else {
2080 /* create empty opts */
2081 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2082 &error_abort);
2084 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2085 return 0;
2088 return -1;
2091 char *qemu_find_file(int type, const char *name)
2093 int i;
2094 const char *subdir;
2095 char *buf;
2097 /* Try the name as a straight path first */
2098 if (access(name, R_OK) == 0) {
2099 trace_load_file(name, name);
2100 return g_strdup(name);
2103 switch (type) {
2104 case QEMU_FILE_TYPE_BIOS:
2105 subdir = "";
2106 break;
2107 case QEMU_FILE_TYPE_KEYMAP:
2108 subdir = "keymaps/";
2109 break;
2110 default:
2111 abort();
2114 for (i = 0; i < data_dir_idx; i++) {
2115 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2116 if (access(buf, R_OK) == 0) {
2117 trace_load_file(name, buf);
2118 return buf;
2120 g_free(buf);
2122 return NULL;
2125 static int device_help_func(QemuOpts *opts, void *opaque)
2127 return qdev_device_help(opts);
2130 static int device_init_func(QemuOpts *opts, void *opaque)
2132 DeviceState *dev;
2134 dev = qdev_device_add(opts);
2135 if (!dev)
2136 return -1;
2137 object_unref(OBJECT(dev));
2138 return 0;
2141 static int chardev_init_func(QemuOpts *opts, void *opaque)
2143 Error *local_err = NULL;
2145 qemu_chr_new_from_opts(opts, NULL, &local_err);
2146 if (local_err) {
2147 error_report_err(local_err);
2148 return -1;
2150 return 0;
2153 #ifdef CONFIG_VIRTFS
2154 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2156 int ret;
2157 ret = qemu_fsdev_add(opts);
2159 return ret;
2161 #endif
2163 static int mon_init_func(QemuOpts *opts, void *opaque)
2165 CharDriverState *chr;
2166 const char *chardev;
2167 const char *mode;
2168 int flags;
2170 mode = qemu_opt_get(opts, "mode");
2171 if (mode == NULL) {
2172 mode = "readline";
2174 if (strcmp(mode, "readline") == 0) {
2175 flags = MONITOR_USE_READLINE;
2176 } else if (strcmp(mode, "control") == 0) {
2177 flags = MONITOR_USE_CONTROL;
2178 } else {
2179 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2180 exit(1);
2183 if (qemu_opt_get_bool(opts, "pretty", 0))
2184 flags |= MONITOR_USE_PRETTY;
2186 if (qemu_opt_get_bool(opts, "default", 0))
2187 flags |= MONITOR_IS_DEFAULT;
2189 chardev = qemu_opt_get(opts, "chardev");
2190 chr = qemu_chr_find(chardev);
2191 if (chr == NULL) {
2192 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2193 exit(1);
2196 qemu_chr_fe_claim_no_fail(chr);
2197 monitor_init(chr, flags);
2198 return 0;
2201 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2203 static int monitor_device_index = 0;
2204 Error *local_err = NULL;
2205 QemuOpts *opts;
2206 const char *p;
2207 char label[32];
2208 int def = 0;
2210 if (strstart(optarg, "chardev:", &p)) {
2211 snprintf(label, sizeof(label), "%s", p);
2212 } else {
2213 snprintf(label, sizeof(label), "compat_monitor%d",
2214 monitor_device_index);
2215 if (monitor_device_index == 0) {
2216 def = 1;
2218 opts = qemu_chr_parse_compat(label, optarg);
2219 if (!opts) {
2220 fprintf(stderr, "parse error: %s\n", optarg);
2221 exit(1);
2225 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2226 if (!opts) {
2227 error_report_err(local_err);
2228 exit(1);
2230 qemu_opt_set(opts, "mode", mode, &error_abort);
2231 qemu_opt_set(opts, "chardev", label, &error_abort);
2232 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2233 if (def)
2234 qemu_opt_set(opts, "default", "on", &error_abort);
2235 monitor_device_index++;
2238 struct device_config {
2239 enum {
2240 DEV_USB, /* -usbdevice */
2241 DEV_BT, /* -bt */
2242 DEV_SERIAL, /* -serial */
2243 DEV_PARALLEL, /* -parallel */
2244 DEV_VIRTCON, /* -virtioconsole */
2245 DEV_DEBUGCON, /* -debugcon */
2246 DEV_GDB, /* -gdb, -s */
2247 DEV_SCLP, /* s390 sclp */
2248 } type;
2249 const char *cmdline;
2250 Location loc;
2251 QTAILQ_ENTRY(device_config) next;
2254 static QTAILQ_HEAD(, device_config) device_configs =
2255 QTAILQ_HEAD_INITIALIZER(device_configs);
2257 static void add_device_config(int type, const char *cmdline)
2259 struct device_config *conf;
2261 conf = g_malloc0(sizeof(*conf));
2262 conf->type = type;
2263 conf->cmdline = cmdline;
2264 loc_save(&conf->loc);
2265 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2268 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2270 struct device_config *conf;
2271 int rc;
2273 QTAILQ_FOREACH(conf, &device_configs, next) {
2274 if (conf->type != type)
2275 continue;
2276 loc_push_restore(&conf->loc);
2277 rc = func(conf->cmdline);
2278 loc_pop(&conf->loc);
2279 if (rc) {
2280 return rc;
2283 return 0;
2286 static int serial_parse(const char *devname)
2288 static int index = 0;
2289 char label[32];
2291 if (strcmp(devname, "none") == 0)
2292 return 0;
2293 if (index == MAX_SERIAL_PORTS) {
2294 fprintf(stderr, "qemu: too many serial ports\n");
2295 exit(1);
2297 snprintf(label, sizeof(label), "serial%d", index);
2298 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2299 if (!serial_hds[index]) {
2300 fprintf(stderr, "qemu: could not connect serial device"
2301 " to character backend '%s'\n", devname);
2302 return -1;
2304 index++;
2305 return 0;
2308 static int parallel_parse(const char *devname)
2310 static int index = 0;
2311 char label[32];
2313 if (strcmp(devname, "none") == 0)
2314 return 0;
2315 if (index == MAX_PARALLEL_PORTS) {
2316 fprintf(stderr, "qemu: too many parallel ports\n");
2317 exit(1);
2319 snprintf(label, sizeof(label), "parallel%d", index);
2320 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2321 if (!parallel_hds[index]) {
2322 fprintf(stderr, "qemu: could not connect parallel device"
2323 " to character backend '%s'\n", devname);
2324 return -1;
2326 index++;
2327 return 0;
2330 static int virtcon_parse(const char *devname)
2332 QemuOptsList *device = qemu_find_opts("device");
2333 static int index = 0;
2334 char label[32];
2335 QemuOpts *bus_opts, *dev_opts;
2337 if (strcmp(devname, "none") == 0)
2338 return 0;
2339 if (index == MAX_VIRTIO_CONSOLES) {
2340 fprintf(stderr, "qemu: too many virtio consoles\n");
2341 exit(1);
2344 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2345 if (arch_type == QEMU_ARCH_S390X) {
2346 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390", &error_abort);
2347 } else {
2348 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci", &error_abort);
2351 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2352 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2354 snprintf(label, sizeof(label), "virtcon%d", index);
2355 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2356 if (!virtcon_hds[index]) {
2357 fprintf(stderr, "qemu: could not connect virtio console"
2358 " to character backend '%s'\n", devname);
2359 return -1;
2361 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2363 index++;
2364 return 0;
2367 static int sclp_parse(const char *devname)
2369 QemuOptsList *device = qemu_find_opts("device");
2370 static int index = 0;
2371 char label[32];
2372 QemuOpts *dev_opts;
2374 if (strcmp(devname, "none") == 0) {
2375 return 0;
2377 if (index == MAX_SCLP_CONSOLES) {
2378 fprintf(stderr, "qemu: too many sclp consoles\n");
2379 exit(1);
2382 assert(arch_type == QEMU_ARCH_S390X);
2384 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2385 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2387 snprintf(label, sizeof(label), "sclpcon%d", index);
2388 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2389 if (!sclp_hds[index]) {
2390 fprintf(stderr, "qemu: could not connect sclp console"
2391 " to character backend '%s'\n", devname);
2392 return -1;
2394 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2396 index++;
2397 return 0;
2400 static int debugcon_parse(const char *devname)
2402 QemuOpts *opts;
2404 if (!qemu_chr_new("debugcon", devname, NULL)) {
2405 exit(1);
2407 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2408 if (!opts) {
2409 fprintf(stderr, "qemu: already have a debugcon device\n");
2410 exit(1);
2412 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2413 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2414 return 0;
2417 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2419 const MachineClass *mc1 = a, *mc2 = b;
2420 int res;
2422 if (mc1->family == NULL) {
2423 if (mc2->family == NULL) {
2424 /* Compare standalone machine types against each other; they sort
2425 * in increasing order.
2427 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2428 object_class_get_name(OBJECT_CLASS(mc2)));
2431 /* Standalone machine types sort after families. */
2432 return 1;
2435 if (mc2->family == NULL) {
2436 /* Families sort before standalone machine types. */
2437 return -1;
2440 /* Families sort between each other alphabetically increasingly. */
2441 res = strcmp(mc1->family, mc2->family);
2442 if (res != 0) {
2443 return res;
2446 /* Within the same family, machine types sort in decreasing order. */
2447 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2448 object_class_get_name(OBJECT_CLASS(mc1)));
2451 static MachineClass *machine_parse(const char *name)
2453 MachineClass *mc = NULL;
2454 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2456 if (name) {
2457 mc = find_machine(name);
2459 if (mc) {
2460 g_slist_free(machines);
2461 return mc;
2463 if (name && !is_help_option(name)) {
2464 error_report("Unsupported machine type");
2465 error_printf("Use -machine help to list supported machines!\n");
2466 } else {
2467 printf("Supported machines are:\n");
2468 machines = g_slist_sort(machines, machine_class_cmp);
2469 for (el = machines; el; el = el->next) {
2470 MachineClass *mc = el->data;
2471 if (mc->alias) {
2472 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2474 printf("%-20s %s%s\n", mc->name, mc->desc,
2475 mc->is_default ? " (default)" : "");
2479 g_slist_free(machines);
2480 exit(!name || !is_help_option(name));
2483 void qemu_add_exit_notifier(Notifier *notify)
2485 notifier_list_add(&exit_notifiers, notify);
2488 void qemu_remove_exit_notifier(Notifier *notify)
2490 notifier_remove(notify);
2493 static void qemu_run_exit_notifiers(void)
2495 notifier_list_notify(&exit_notifiers, NULL);
2498 void qemu_add_machine_init_done_notifier(Notifier *notify)
2500 notifier_list_add(&machine_init_done_notifiers, notify);
2503 static void qemu_run_machine_init_done_notifiers(void)
2505 notifier_list_notify(&machine_init_done_notifiers, NULL);
2508 static const QEMUOption *lookup_opt(int argc, char **argv,
2509 const char **poptarg, int *poptind)
2511 const QEMUOption *popt;
2512 int optind = *poptind;
2513 char *r = argv[optind];
2514 const char *optarg;
2516 loc_set_cmdline(argv, optind, 1);
2517 optind++;
2518 /* Treat --foo the same as -foo. */
2519 if (r[1] == '-')
2520 r++;
2521 popt = qemu_options;
2522 for(;;) {
2523 if (!popt->name) {
2524 error_report("invalid option");
2525 exit(1);
2527 if (!strcmp(popt->name, r + 1))
2528 break;
2529 popt++;
2531 if (popt->flags & HAS_ARG) {
2532 if (optind >= argc) {
2533 error_report("requires an argument");
2534 exit(1);
2536 optarg = argv[optind++];
2537 loc_set_cmdline(argv, optind - 2, 2);
2538 } else {
2539 optarg = NULL;
2542 *poptarg = optarg;
2543 *poptind = optind;
2545 return popt;
2548 static gpointer malloc_and_trace(gsize n_bytes)
2550 void *ptr = malloc(n_bytes);
2551 trace_g_malloc(n_bytes, ptr);
2552 return ptr;
2555 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2557 void *ptr = realloc(mem, n_bytes);
2558 trace_g_realloc(mem, n_bytes, ptr);
2559 return ptr;
2562 static void free_and_trace(gpointer mem)
2564 trace_g_free(mem);
2565 free(mem);
2568 static int machine_set_property(const char *name, const char *value,
2569 void *opaque)
2571 Object *obj = OBJECT(opaque);
2572 Error *local_err = NULL;
2573 char *c, *qom_name;
2575 if (strcmp(name, "type") == 0) {
2576 return 0;
2579 qom_name = g_strdup(name);
2580 c = qom_name;
2581 while (*c++) {
2582 if (*c == '_') {
2583 *c = '-';
2587 object_property_parse(obj, value, qom_name, &local_err);
2588 g_free(qom_name);
2590 if (local_err) {
2591 error_report_err(local_err);
2592 return -1;
2595 return 0;
2598 static int object_create(QemuOpts *opts, void *opaque)
2600 Error *err = NULL;
2601 char *type = NULL;
2602 char *id = NULL;
2603 void *dummy = NULL;
2604 OptsVisitor *ov;
2605 QDict *pdict;
2607 ov = opts_visitor_new(opts);
2608 pdict = qemu_opts_to_qdict(opts, NULL);
2610 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2611 if (err) {
2612 goto out;
2615 qdict_del(pdict, "qom-type");
2616 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2617 if (err) {
2618 goto out;
2621 qdict_del(pdict, "id");
2622 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2623 if (err) {
2624 goto out;
2627 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2628 if (err) {
2629 goto out;
2631 visit_end_struct(opts_get_visitor(ov), &err);
2632 if (err) {
2633 qmp_object_del(id, NULL);
2636 out:
2637 opts_visitor_cleanup(ov);
2639 QDECREF(pdict);
2640 g_free(id);
2641 g_free(type);
2642 g_free(dummy);
2643 if (err) {
2644 error_report_err(err);
2645 return -1;
2647 return 0;
2650 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size)
2652 uint64_t sz;
2653 const char *mem_str;
2654 const char *maxmem_str, *slots_str;
2655 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2656 1024 * 1024;
2657 QemuOpts *opts = qemu_find_opts_singleton("memory");
2659 sz = 0;
2660 mem_str = qemu_opt_get(opts, "size");
2661 if (mem_str) {
2662 if (!*mem_str) {
2663 error_report("missing 'size' option value");
2664 exit(EXIT_FAILURE);
2667 sz = qemu_opt_get_size(opts, "size", ram_size);
2669 /* Fix up legacy suffix-less format */
2670 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2671 uint64_t overflow_check = sz;
2673 sz <<= 20;
2674 if ((sz >> 20) != overflow_check) {
2675 error_report("too large 'size' option value");
2676 exit(EXIT_FAILURE);
2681 /* backward compatibility behaviour for case "-m 0" */
2682 if (sz == 0) {
2683 sz = default_ram_size;
2686 sz = QEMU_ALIGN_UP(sz, 8192);
2687 ram_size = sz;
2688 if (ram_size != sz) {
2689 error_report("ram size too large");
2690 exit(EXIT_FAILURE);
2693 /* store value for the future use */
2694 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2695 *maxram_size = ram_size;
2697 maxmem_str = qemu_opt_get(opts, "maxmem");
2698 slots_str = qemu_opt_get(opts, "slots");
2699 if (maxmem_str && slots_str) {
2700 uint64_t slots;
2702 sz = qemu_opt_get_size(opts, "maxmem", 0);
2703 slots = qemu_opt_get_number(opts, "slots", 0);
2704 if (sz < ram_size) {
2705 error_report("invalid value of -m option maxmem: "
2706 "maximum memory size (0x%" PRIx64 ") must be at least "
2707 "the initial memory size (0x" RAM_ADDR_FMT ")",
2708 sz, ram_size);
2709 exit(EXIT_FAILURE);
2710 } else if (sz > ram_size) {
2711 if (!slots) {
2712 error_report("invalid value of -m option: maxmem was "
2713 "specified, but no hotplug slots were specified");
2714 exit(EXIT_FAILURE);
2716 } else if (slots) {
2717 error_report("invalid value of -m option maxmem: "
2718 "memory slots were specified but maximum memory size "
2719 "(0x%" PRIx64 ") is equal to the initial memory size "
2720 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2721 exit(EXIT_FAILURE);
2724 *maxram_size = sz;
2725 *ram_slots = slots;
2726 } else if ((!maxmem_str && slots_str) ||
2727 (maxmem_str && !slots_str)) {
2728 error_report("invalid -m option value: missing "
2729 "'%s' option", slots_str ? "maxmem" : "slots");
2730 exit(EXIT_FAILURE);
2734 int main(int argc, char **argv, char **envp)
2736 int i;
2737 int snapshot, linux_boot;
2738 const char *initrd_filename;
2739 const char *kernel_filename, *kernel_cmdline;
2740 const char *boot_order = NULL;
2741 const char *boot_once = NULL;
2742 DisplayState *ds;
2743 int cyls, heads, secs, translation;
2744 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2745 QemuOptsList *olist;
2746 int optind;
2747 const char *optarg;
2748 const char *loadvm = NULL;
2749 MachineClass *machine_class;
2750 const char *cpu_model;
2751 const char *vga_model = NULL;
2752 const char *qtest_chrdev = NULL;
2753 const char *qtest_log = NULL;
2754 const char *pid_file = NULL;
2755 const char *incoming = NULL;
2756 #ifdef CONFIG_VNC
2757 int show_vnc_port = 0;
2758 #endif
2759 bool defconfig = true;
2760 bool userconfig = true;
2761 const char *log_mask = NULL;
2762 const char *log_file = NULL;
2763 GMemVTable mem_trace = {
2764 .malloc = malloc_and_trace,
2765 .realloc = realloc_and_trace,
2766 .free = free_and_trace,
2768 const char *trace_events = NULL;
2769 const char *trace_file = NULL;
2770 ram_addr_t maxram_size;
2771 uint64_t ram_slots = 0;
2772 FILE *vmstate_dump_file = NULL;
2773 Error *main_loop_err = NULL;
2775 qemu_init_cpu_loop();
2776 qemu_mutex_lock_iothread();
2778 atexit(qemu_run_exit_notifiers);
2779 error_set_progname(argv[0]);
2780 qemu_init_exec_dir(argv[0]);
2782 g_mem_set_vtable(&mem_trace);
2784 module_call_init(MODULE_INIT_QOM);
2786 qemu_add_opts(&qemu_drive_opts);
2787 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2788 qemu_add_drive_opts(&qemu_common_drive_opts);
2789 qemu_add_drive_opts(&qemu_drive_opts);
2790 qemu_add_opts(&qemu_chardev_opts);
2791 qemu_add_opts(&qemu_device_opts);
2792 qemu_add_opts(&qemu_netdev_opts);
2793 qemu_add_opts(&qemu_net_opts);
2794 qemu_add_opts(&qemu_rtc_opts);
2795 qemu_add_opts(&qemu_global_opts);
2796 qemu_add_opts(&qemu_mon_opts);
2797 qemu_add_opts(&qemu_trace_opts);
2798 qemu_add_opts(&qemu_option_rom_opts);
2799 qemu_add_opts(&qemu_machine_opts);
2800 qemu_add_opts(&qemu_mem_opts);
2801 qemu_add_opts(&qemu_smp_opts);
2802 qemu_add_opts(&qemu_boot_opts);
2803 qemu_add_opts(&qemu_sandbox_opts);
2804 qemu_add_opts(&qemu_add_fd_opts);
2805 qemu_add_opts(&qemu_object_opts);
2806 qemu_add_opts(&qemu_tpmdev_opts);
2807 qemu_add_opts(&qemu_realtime_opts);
2808 qemu_add_opts(&qemu_msg_opts);
2809 qemu_add_opts(&qemu_name_opts);
2810 qemu_add_opts(&qemu_numa_opts);
2811 qemu_add_opts(&qemu_icount_opts);
2812 qemu_add_opts(&qemu_semihosting_config_opts);
2814 runstate_init();
2816 rtc_clock = QEMU_CLOCK_HOST;
2818 QLIST_INIT (&vm_change_state_head);
2819 os_setup_early_signal_handling();
2821 module_call_init(MODULE_INIT_MACHINE);
2822 machine_class = find_default_machine();
2823 cpu_model = NULL;
2824 snapshot = 0;
2825 cyls = heads = secs = 0;
2826 translation = BIOS_ATA_TRANSLATION_AUTO;
2828 nb_nics = 0;
2830 bdrv_init_with_whitelist();
2832 autostart = 1;
2834 /* first pass of option parsing */
2835 optind = 1;
2836 while (optind < argc) {
2837 if (argv[optind][0] != '-') {
2838 /* disk image */
2839 optind++;
2840 } else {
2841 const QEMUOption *popt;
2843 popt = lookup_opt(argc, argv, &optarg, &optind);
2844 switch (popt->index) {
2845 case QEMU_OPTION_nodefconfig:
2846 defconfig = false;
2847 break;
2848 case QEMU_OPTION_nouserconfig:
2849 userconfig = false;
2850 break;
2855 if (defconfig) {
2856 int ret;
2857 ret = qemu_read_default_config_files(userconfig);
2858 if (ret < 0) {
2859 exit(1);
2863 /* second pass of option parsing */
2864 optind = 1;
2865 for(;;) {
2866 if (optind >= argc)
2867 break;
2868 if (argv[optind][0] != '-') {
2869 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2870 } else {
2871 const QEMUOption *popt;
2873 popt = lookup_opt(argc, argv, &optarg, &optind);
2874 if (!(popt->arch_mask & arch_type)) {
2875 printf("Option %s not supported for this target\n", popt->name);
2876 exit(1);
2878 switch(popt->index) {
2879 case QEMU_OPTION_no_kvm_irqchip: {
2880 olist = qemu_find_opts("machine");
2881 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2882 break;
2884 case QEMU_OPTION_cpu:
2885 /* hw initialization will check this */
2886 cpu_model = optarg;
2887 break;
2888 case QEMU_OPTION_hda:
2890 char buf[256];
2891 if (cyls == 0)
2892 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2893 else
2894 snprintf(buf, sizeof(buf),
2895 "%s,cyls=%d,heads=%d,secs=%d%s",
2896 HD_OPTS , cyls, heads, secs,
2897 translation == BIOS_ATA_TRANSLATION_LBA ?
2898 ",trans=lba" :
2899 translation == BIOS_ATA_TRANSLATION_NONE ?
2900 ",trans=none" : "");
2901 drive_add(IF_DEFAULT, 0, optarg, buf);
2902 break;
2904 case QEMU_OPTION_hdb:
2905 case QEMU_OPTION_hdc:
2906 case QEMU_OPTION_hdd:
2907 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2908 HD_OPTS);
2909 break;
2910 case QEMU_OPTION_drive:
2911 if (drive_def(optarg) == NULL) {
2912 exit(1);
2914 break;
2915 case QEMU_OPTION_set:
2916 if (qemu_set_option(optarg) != 0)
2917 exit(1);
2918 break;
2919 case QEMU_OPTION_global:
2920 if (qemu_global_option(optarg) != 0)
2921 exit(1);
2922 break;
2923 case QEMU_OPTION_mtdblock:
2924 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2925 break;
2926 case QEMU_OPTION_sd:
2927 drive_add(IF_SD, -1, optarg, SD_OPTS);
2928 break;
2929 case QEMU_OPTION_pflash:
2930 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2931 break;
2932 case QEMU_OPTION_snapshot:
2933 snapshot = 1;
2934 break;
2935 case QEMU_OPTION_hdachs:
2937 const char *p;
2938 p = optarg;
2939 cyls = strtol(p, (char **)&p, 0);
2940 if (cyls < 1 || cyls > 16383)
2941 goto chs_fail;
2942 if (*p != ',')
2943 goto chs_fail;
2944 p++;
2945 heads = strtol(p, (char **)&p, 0);
2946 if (heads < 1 || heads > 16)
2947 goto chs_fail;
2948 if (*p != ',')
2949 goto chs_fail;
2950 p++;
2951 secs = strtol(p, (char **)&p, 0);
2952 if (secs < 1 || secs > 63)
2953 goto chs_fail;
2954 if (*p == ',') {
2955 p++;
2956 if (!strcmp(p, "large")) {
2957 translation = BIOS_ATA_TRANSLATION_LARGE;
2958 } else if (!strcmp(p, "rechs")) {
2959 translation = BIOS_ATA_TRANSLATION_RECHS;
2960 } else if (!strcmp(p, "none")) {
2961 translation = BIOS_ATA_TRANSLATION_NONE;
2962 } else if (!strcmp(p, "lba")) {
2963 translation = BIOS_ATA_TRANSLATION_LBA;
2964 } else if (!strcmp(p, "auto")) {
2965 translation = BIOS_ATA_TRANSLATION_AUTO;
2966 } else {
2967 goto chs_fail;
2969 } else if (*p != '\0') {
2970 chs_fail:
2971 fprintf(stderr, "qemu: invalid physical CHS format\n");
2972 exit(1);
2974 if (hda_opts != NULL) {
2975 qemu_opt_set_number(hda_opts, "cyls", cyls,
2976 &error_abort);
2977 qemu_opt_set_number(hda_opts, "heads", heads,
2978 &error_abort);
2979 qemu_opt_set_number(hda_opts, "secs", secs,
2980 &error_abort);
2981 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
2982 qemu_opt_set(hda_opts, "trans", "large",
2983 &error_abort);
2984 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
2985 qemu_opt_set(hda_opts, "trans", "rechs",
2986 &error_abort);
2987 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
2988 qemu_opt_set(hda_opts, "trans", "lba",
2989 &error_abort);
2990 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
2991 qemu_opt_set(hda_opts, "trans", "none",
2992 &error_abort);
2996 break;
2997 case QEMU_OPTION_numa:
2998 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
2999 if (!opts) {
3000 exit(1);
3002 break;
3003 case QEMU_OPTION_display:
3004 display_type = select_display(optarg);
3005 break;
3006 case QEMU_OPTION_nographic:
3007 display_type = DT_NOGRAPHIC;
3008 break;
3009 case QEMU_OPTION_curses:
3010 #ifdef CONFIG_CURSES
3011 display_type = DT_CURSES;
3012 #else
3013 fprintf(stderr, "Curses support is disabled\n");
3014 exit(1);
3015 #endif
3016 break;
3017 case QEMU_OPTION_portrait:
3018 graphic_rotate = 90;
3019 break;
3020 case QEMU_OPTION_rotate:
3021 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3022 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3023 graphic_rotate != 180 && graphic_rotate != 270) {
3024 fprintf(stderr,
3025 "qemu: only 90, 180, 270 deg rotation is available\n");
3026 exit(1);
3028 break;
3029 case QEMU_OPTION_kernel:
3030 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3031 &error_abort);
3032 break;
3033 case QEMU_OPTION_initrd:
3034 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3035 &error_abort);
3036 break;
3037 case QEMU_OPTION_append:
3038 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3039 &error_abort);
3040 break;
3041 case QEMU_OPTION_dtb:
3042 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3043 &error_abort);
3044 break;
3045 case QEMU_OPTION_cdrom:
3046 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3047 break;
3048 case QEMU_OPTION_boot:
3049 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3050 if (!opts) {
3051 exit(1);
3053 break;
3054 case QEMU_OPTION_fda:
3055 case QEMU_OPTION_fdb:
3056 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3057 optarg, FD_OPTS);
3058 break;
3059 case QEMU_OPTION_no_fd_bootchk:
3060 fd_bootchk = 0;
3061 break;
3062 case QEMU_OPTION_netdev:
3063 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3064 exit(1);
3066 break;
3067 case QEMU_OPTION_net:
3068 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3069 exit(1);
3071 break;
3072 #ifdef CONFIG_LIBISCSI
3073 case QEMU_OPTION_iscsi:
3074 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3075 if (!opts) {
3076 exit(1);
3078 break;
3079 #endif
3080 #ifdef CONFIG_SLIRP
3081 case QEMU_OPTION_tftp:
3082 legacy_tftp_prefix = optarg;
3083 break;
3084 case QEMU_OPTION_bootp:
3085 legacy_bootp_filename = optarg;
3086 break;
3087 case QEMU_OPTION_redir:
3088 if (net_slirp_redir(optarg) < 0)
3089 exit(1);
3090 break;
3091 #endif
3092 case QEMU_OPTION_bt:
3093 add_device_config(DEV_BT, optarg);
3094 break;
3095 case QEMU_OPTION_audio_help:
3096 AUD_help ();
3097 exit (0);
3098 break;
3099 case QEMU_OPTION_soundhw:
3100 select_soundhw (optarg);
3101 break;
3102 case QEMU_OPTION_h:
3103 help(0);
3104 break;
3105 case QEMU_OPTION_version:
3106 version();
3107 exit(0);
3108 break;
3109 case QEMU_OPTION_m:
3110 opts = qemu_opts_parse(qemu_find_opts("memory"),
3111 optarg, 1);
3112 if (!opts) {
3113 exit(EXIT_FAILURE);
3115 break;
3116 #ifdef CONFIG_TPM
3117 case QEMU_OPTION_tpmdev:
3118 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3119 exit(1);
3121 break;
3122 #endif
3123 case QEMU_OPTION_mempath:
3124 mem_path = optarg;
3125 break;
3126 case QEMU_OPTION_mem_prealloc:
3127 mem_prealloc = 1;
3128 break;
3129 case QEMU_OPTION_d:
3130 log_mask = optarg;
3131 break;
3132 case QEMU_OPTION_D:
3133 log_file = optarg;
3134 break;
3135 case QEMU_OPTION_s:
3136 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3137 break;
3138 case QEMU_OPTION_gdb:
3139 add_device_config(DEV_GDB, optarg);
3140 break;
3141 case QEMU_OPTION_L:
3142 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3143 data_dir[data_dir_idx++] = optarg;
3145 break;
3146 case QEMU_OPTION_bios:
3147 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3148 &error_abort);
3149 break;
3150 case QEMU_OPTION_singlestep:
3151 singlestep = 1;
3152 break;
3153 case QEMU_OPTION_S:
3154 autostart = 0;
3155 break;
3156 case QEMU_OPTION_k:
3157 keyboard_layout = optarg;
3158 break;
3159 case QEMU_OPTION_localtime:
3160 rtc_utc = 0;
3161 break;
3162 case QEMU_OPTION_vga:
3163 vga_model = optarg;
3164 default_vga = 0;
3165 break;
3166 case QEMU_OPTION_g:
3168 const char *p;
3169 int w, h, depth;
3170 p = optarg;
3171 w = strtol(p, (char **)&p, 10);
3172 if (w <= 0) {
3173 graphic_error:
3174 fprintf(stderr, "qemu: invalid resolution or depth\n");
3175 exit(1);
3177 if (*p != 'x')
3178 goto graphic_error;
3179 p++;
3180 h = strtol(p, (char **)&p, 10);
3181 if (h <= 0)
3182 goto graphic_error;
3183 if (*p == 'x') {
3184 p++;
3185 depth = strtol(p, (char **)&p, 10);
3186 if (depth != 8 && depth != 15 && depth != 16 &&
3187 depth != 24 && depth != 32)
3188 goto graphic_error;
3189 } else if (*p == '\0') {
3190 depth = graphic_depth;
3191 } else {
3192 goto graphic_error;
3195 graphic_width = w;
3196 graphic_height = h;
3197 graphic_depth = depth;
3199 break;
3200 case QEMU_OPTION_echr:
3202 char *r;
3203 term_escape_char = strtol(optarg, &r, 0);
3204 if (r == optarg)
3205 printf("Bad argument to echr\n");
3206 break;
3208 case QEMU_OPTION_monitor:
3209 default_monitor = 0;
3210 if (strncmp(optarg, "none", 4)) {
3211 monitor_parse(optarg, "readline", false);
3213 break;
3214 case QEMU_OPTION_qmp:
3215 monitor_parse(optarg, "control", false);
3216 default_monitor = 0;
3217 break;
3218 case QEMU_OPTION_qmp_pretty:
3219 monitor_parse(optarg, "control", true);
3220 default_monitor = 0;
3221 break;
3222 case QEMU_OPTION_mon:
3223 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3224 if (!opts) {
3225 exit(1);
3227 default_monitor = 0;
3228 break;
3229 case QEMU_OPTION_chardev:
3230 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3231 if (!opts) {
3232 exit(1);
3234 break;
3235 case QEMU_OPTION_fsdev:
3236 olist = qemu_find_opts("fsdev");
3237 if (!olist) {
3238 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3239 exit(1);
3241 opts = qemu_opts_parse(olist, optarg, 1);
3242 if (!opts) {
3243 exit(1);
3245 break;
3246 case QEMU_OPTION_virtfs: {
3247 QemuOpts *fsdev;
3248 QemuOpts *device;
3249 const char *writeout, *sock_fd, *socket;
3251 olist = qemu_find_opts("virtfs");
3252 if (!olist) {
3253 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3254 exit(1);
3256 opts = qemu_opts_parse(olist, optarg, 1);
3257 if (!opts) {
3258 exit(1);
3261 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3262 qemu_opt_get(opts, "mount_tag") == NULL) {
3263 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3264 exit(1);
3266 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3267 qemu_opt_get(opts, "mount_tag"),
3268 1, NULL);
3269 if (!fsdev) {
3270 fprintf(stderr, "duplicate fsdev id: %s\n",
3271 qemu_opt_get(opts, "mount_tag"));
3272 exit(1);
3275 writeout = qemu_opt_get(opts, "writeout");
3276 if (writeout) {
3277 #ifdef CONFIG_SYNC_FILE_RANGE
3278 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3279 #else
3280 fprintf(stderr, "writeout=immediate not supported on "
3281 "this platform\n");
3282 exit(1);
3283 #endif
3285 qemu_opt_set(fsdev, "fsdriver",
3286 qemu_opt_get(opts, "fsdriver"), &error_abort);
3287 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3288 &error_abort);
3289 qemu_opt_set(fsdev, "security_model",
3290 qemu_opt_get(opts, "security_model"),
3291 &error_abort);
3292 socket = qemu_opt_get(opts, "socket");
3293 if (socket) {
3294 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3296 sock_fd = qemu_opt_get(opts, "sock_fd");
3297 if (sock_fd) {
3298 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3301 qemu_opt_set_bool(fsdev, "readonly",
3302 qemu_opt_get_bool(opts, "readonly", 0),
3303 &error_abort);
3304 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3305 &error_abort);
3306 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3307 qemu_opt_set(device, "fsdev",
3308 qemu_opt_get(opts, "mount_tag"), &error_abort);
3309 qemu_opt_set(device, "mount_tag",
3310 qemu_opt_get(opts, "mount_tag"), &error_abort);
3311 break;
3313 case QEMU_OPTION_virtfs_synth: {
3314 QemuOpts *fsdev;
3315 QemuOpts *device;
3317 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3318 1, NULL);
3319 if (!fsdev) {
3320 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3321 exit(1);
3323 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3325 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3326 &error_abort);
3327 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3328 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3329 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3330 break;
3332 case QEMU_OPTION_serial:
3333 add_device_config(DEV_SERIAL, optarg);
3334 default_serial = 0;
3335 if (strncmp(optarg, "mon:", 4) == 0) {
3336 default_monitor = 0;
3338 break;
3339 case QEMU_OPTION_watchdog:
3340 if (watchdog) {
3341 fprintf(stderr,
3342 "qemu: only one watchdog option may be given\n");
3343 return 1;
3345 watchdog = optarg;
3346 break;
3347 case QEMU_OPTION_watchdog_action:
3348 if (select_watchdog_action(optarg) == -1) {
3349 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3350 exit(1);
3352 break;
3353 case QEMU_OPTION_virtiocon:
3354 add_device_config(DEV_VIRTCON, optarg);
3355 default_virtcon = 0;
3356 if (strncmp(optarg, "mon:", 4) == 0) {
3357 default_monitor = 0;
3359 break;
3360 case QEMU_OPTION_parallel:
3361 add_device_config(DEV_PARALLEL, optarg);
3362 default_parallel = 0;
3363 if (strncmp(optarg, "mon:", 4) == 0) {
3364 default_monitor = 0;
3366 break;
3367 case QEMU_OPTION_debugcon:
3368 add_device_config(DEV_DEBUGCON, optarg);
3369 break;
3370 case QEMU_OPTION_loadvm:
3371 loadvm = optarg;
3372 break;
3373 case QEMU_OPTION_full_screen:
3374 full_screen = 1;
3375 break;
3376 case QEMU_OPTION_no_frame:
3377 no_frame = 1;
3378 break;
3379 case QEMU_OPTION_alt_grab:
3380 alt_grab = 1;
3381 break;
3382 case QEMU_OPTION_ctrl_grab:
3383 ctrl_grab = 1;
3384 break;
3385 case QEMU_OPTION_no_quit:
3386 no_quit = 1;
3387 break;
3388 case QEMU_OPTION_sdl:
3389 #ifdef CONFIG_SDL
3390 display_type = DT_SDL;
3391 break;
3392 #else
3393 fprintf(stderr, "SDL support is disabled\n");
3394 exit(1);
3395 #endif
3396 case QEMU_OPTION_pidfile:
3397 pid_file = optarg;
3398 break;
3399 case QEMU_OPTION_win2k_hack:
3400 win2k_install_hack = 1;
3401 break;
3402 case QEMU_OPTION_rtc_td_hack: {
3403 static GlobalProperty slew_lost_ticks[] = {
3405 .driver = "mc146818rtc",
3406 .property = "lost_tick_policy",
3407 .value = "slew",
3409 { /* end of list */ }
3412 qdev_prop_register_global_list(slew_lost_ticks);
3413 break;
3415 case QEMU_OPTION_acpitable:
3416 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3417 if (!opts) {
3418 exit(1);
3420 do_acpitable_option(opts);
3421 break;
3422 case QEMU_OPTION_smbios:
3423 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3424 if (!opts) {
3425 exit(1);
3427 do_smbios_option(opts);
3428 break;
3429 case QEMU_OPTION_enable_kvm:
3430 olist = qemu_find_opts("machine");
3431 qemu_opts_parse(olist, "accel=kvm", 0);
3432 break;
3433 case QEMU_OPTION_M:
3434 case QEMU_OPTION_machine:
3435 olist = qemu_find_opts("machine");
3436 opts = qemu_opts_parse(olist, optarg, 1);
3437 if (!opts) {
3438 exit(1);
3440 break;
3441 case QEMU_OPTION_no_kvm:
3442 olist = qemu_find_opts("machine");
3443 qemu_opts_parse(olist, "accel=tcg", 0);
3444 break;
3445 case QEMU_OPTION_no_kvm_pit: {
3446 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3447 "separately.\n");
3448 break;
3450 case QEMU_OPTION_no_kvm_pit_reinjection: {
3451 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3453 .driver = "kvm-pit",
3454 .property = "lost_tick_policy",
3455 .value = "discard",
3457 { /* end of list */ }
3460 fprintf(stderr, "Warning: option deprecated, use "
3461 "lost_tick_policy property of kvm-pit instead.\n");
3462 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3463 break;
3465 case QEMU_OPTION_usb:
3466 olist = qemu_find_opts("machine");
3467 qemu_opts_parse(olist, "usb=on", 0);
3468 break;
3469 case QEMU_OPTION_usbdevice:
3470 olist = qemu_find_opts("machine");
3471 qemu_opts_parse(olist, "usb=on", 0);
3472 add_device_config(DEV_USB, optarg);
3473 break;
3474 case QEMU_OPTION_device:
3475 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3476 exit(1);
3478 break;
3479 case QEMU_OPTION_smp:
3480 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3481 exit(1);
3483 break;
3484 case QEMU_OPTION_vnc:
3485 #ifdef CONFIG_VNC
3486 if (vnc_parse_func(optarg) == NULL) {
3487 exit(1);
3489 #else
3490 fprintf(stderr, "VNC support is disabled\n");
3491 exit(1);
3492 #endif
3493 break;
3494 case QEMU_OPTION_no_acpi:
3495 acpi_enabled = 0;
3496 break;
3497 case QEMU_OPTION_no_hpet:
3498 no_hpet = 1;
3499 break;
3500 case QEMU_OPTION_balloon:
3501 if (balloon_parse(optarg) < 0) {
3502 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3503 exit(1);
3505 break;
3506 case QEMU_OPTION_no_reboot:
3507 no_reboot = 1;
3508 break;
3509 case QEMU_OPTION_no_shutdown:
3510 no_shutdown = 1;
3511 break;
3512 case QEMU_OPTION_show_cursor:
3513 cursor_hide = 0;
3514 break;
3515 case QEMU_OPTION_uuid:
3516 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3517 fprintf(stderr, "Fail to parse UUID string."
3518 " Wrong format.\n");
3519 exit(1);
3521 qemu_uuid_set = true;
3522 break;
3523 case QEMU_OPTION_option_rom:
3524 if (nb_option_roms >= MAX_OPTION_ROMS) {
3525 fprintf(stderr, "Too many option ROMs\n");
3526 exit(1);
3528 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3529 if (!opts) {
3530 exit(1);
3532 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3533 option_rom[nb_option_roms].bootindex =
3534 qemu_opt_get_number(opts, "bootindex", -1);
3535 if (!option_rom[nb_option_roms].name) {
3536 fprintf(stderr, "Option ROM file is not specified\n");
3537 exit(1);
3539 nb_option_roms++;
3540 break;
3541 case QEMU_OPTION_semihosting:
3542 semihosting_enabled = 1;
3543 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3544 break;
3545 case QEMU_OPTION_semihosting_config:
3546 semihosting_enabled = 1;
3547 opts = qemu_opts_parse(qemu_find_opts("semihosting-config"),
3548 optarg, 0);
3549 if (opts != NULL) {
3550 semihosting_enabled = qemu_opt_get_bool(opts, "enable",
3551 true);
3552 const char *target = qemu_opt_get(opts, "target");
3553 if (target != NULL) {
3554 if (strcmp("native", target) == 0) {
3555 semihosting_target = SEMIHOSTING_TARGET_NATIVE;
3556 } else if (strcmp("gdb", target) == 0) {
3557 semihosting_target = SEMIHOSTING_TARGET_GDB;
3558 } else if (strcmp("auto", target) == 0) {
3559 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3560 } else {
3561 fprintf(stderr, "Unsupported semihosting-config"
3562 " %s\n",
3563 optarg);
3564 exit(1);
3566 } else {
3567 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3569 } else {
3570 fprintf(stderr, "Unsupported semihosting-config %s\n",
3571 optarg);
3572 exit(1);
3574 break;
3575 case QEMU_OPTION_tdf:
3576 fprintf(stderr, "Warning: user space PIT time drift fix "
3577 "is no longer supported.\n");
3578 break;
3579 case QEMU_OPTION_name:
3580 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3581 if (!opts) {
3582 exit(1);
3584 break;
3585 case QEMU_OPTION_prom_env:
3586 if (nb_prom_envs >= MAX_PROM_ENVS) {
3587 fprintf(stderr, "Too many prom variables\n");
3588 exit(1);
3590 prom_envs[nb_prom_envs] = optarg;
3591 nb_prom_envs++;
3592 break;
3593 case QEMU_OPTION_old_param:
3594 old_param = 1;
3595 break;
3596 case QEMU_OPTION_clock:
3597 /* Clock options no longer exist. Keep this option for
3598 * backward compatibility.
3600 break;
3601 case QEMU_OPTION_startdate:
3602 configure_rtc_date_offset(optarg, 1);
3603 break;
3604 case QEMU_OPTION_rtc:
3605 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3606 if (!opts) {
3607 exit(1);
3609 configure_rtc(opts);
3610 break;
3611 case QEMU_OPTION_tb_size:
3612 tcg_tb_size = strtol(optarg, NULL, 0);
3613 if (tcg_tb_size < 0) {
3614 tcg_tb_size = 0;
3616 break;
3617 case QEMU_OPTION_icount:
3618 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3619 optarg, 1);
3620 if (!icount_opts) {
3621 exit(1);
3623 break;
3624 case QEMU_OPTION_incoming:
3625 if (!incoming) {
3626 runstate_set(RUN_STATE_INMIGRATE);
3628 incoming = optarg;
3629 break;
3630 case QEMU_OPTION_nodefaults:
3631 has_defaults = 0;
3632 break;
3633 case QEMU_OPTION_xen_domid:
3634 if (!(xen_available())) {
3635 printf("Option %s not supported for this target\n", popt->name);
3636 exit(1);
3638 xen_domid = atoi(optarg);
3639 break;
3640 case QEMU_OPTION_xen_create:
3641 if (!(xen_available())) {
3642 printf("Option %s not supported for this target\n", popt->name);
3643 exit(1);
3645 xen_mode = XEN_CREATE;
3646 break;
3647 case QEMU_OPTION_xen_attach:
3648 if (!(xen_available())) {
3649 printf("Option %s not supported for this target\n", popt->name);
3650 exit(1);
3652 xen_mode = XEN_ATTACH;
3653 break;
3654 case QEMU_OPTION_trace:
3656 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3657 if (!opts) {
3658 exit(1);
3660 trace_events = qemu_opt_get(opts, "events");
3661 trace_file = qemu_opt_get(opts, "file");
3662 break;
3664 case QEMU_OPTION_readconfig:
3666 int ret = qemu_read_config_file(optarg);
3667 if (ret < 0) {
3668 fprintf(stderr, "read config %s: %s\n", optarg,
3669 strerror(-ret));
3670 exit(1);
3672 break;
3674 case QEMU_OPTION_spice:
3675 olist = qemu_find_opts("spice");
3676 if (!olist) {
3677 fprintf(stderr, "spice is not supported by this qemu build.\n");
3678 exit(1);
3680 opts = qemu_opts_parse(olist, optarg, 0);
3681 if (!opts) {
3682 exit(1);
3684 display_remote++;
3685 break;
3686 case QEMU_OPTION_writeconfig:
3688 FILE *fp;
3689 if (strcmp(optarg, "-") == 0) {
3690 fp = stdout;
3691 } else {
3692 fp = fopen(optarg, "w");
3693 if (fp == NULL) {
3694 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3695 exit(1);
3698 qemu_config_write(fp);
3699 if (fp != stdout) {
3700 fclose(fp);
3702 break;
3704 case QEMU_OPTION_qtest:
3705 qtest_chrdev = optarg;
3706 break;
3707 case QEMU_OPTION_qtest_log:
3708 qtest_log = optarg;
3709 break;
3710 case QEMU_OPTION_sandbox:
3711 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3712 if (!opts) {
3713 exit(1);
3715 break;
3716 case QEMU_OPTION_add_fd:
3717 #ifndef _WIN32
3718 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3719 if (!opts) {
3720 exit(1);
3722 #else
3723 error_report("File descriptor passing is disabled on this "
3724 "platform");
3725 exit(1);
3726 #endif
3727 break;
3728 case QEMU_OPTION_object:
3729 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3730 if (!opts) {
3731 exit(1);
3733 break;
3734 case QEMU_OPTION_realtime:
3735 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3736 if (!opts) {
3737 exit(1);
3739 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3740 break;
3741 case QEMU_OPTION_msg:
3742 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3743 if (!opts) {
3744 exit(1);
3746 configure_msg(opts);
3747 break;
3748 case QEMU_OPTION_dump_vmstate:
3749 if (vmstate_dump_file) {
3750 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3751 "option may be given\n");
3752 exit(1);
3754 vmstate_dump_file = fopen(optarg, "w");
3755 if (vmstate_dump_file == NULL) {
3756 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3757 exit(1);
3759 break;
3760 default:
3761 os_parse_cmd_args(popt->index, optarg);
3766 opts = qemu_get_machine_opts();
3767 optarg = qemu_opt_get(opts, "type");
3768 if (optarg) {
3769 machine_class = machine_parse(optarg);
3772 set_memory_options(&ram_slots, &maxram_size);
3774 loc_set_none();
3776 os_daemonize();
3778 if (qemu_init_main_loop(&main_loop_err)) {
3779 error_report_err(main_loop_err);
3780 exit(1);
3783 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3784 exit(1);
3787 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3788 exit(1);
3791 #ifndef _WIN32
3792 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3793 exit(1);
3796 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3797 exit(1);
3799 #endif
3801 if (machine_class == NULL) {
3802 fprintf(stderr, "No machine specified, and there is no default.\n"
3803 "Use -machine help to list supported machines!\n");
3804 exit(1);
3807 current_machine = MACHINE(object_new(object_class_get_name(
3808 OBJECT_CLASS(machine_class))));
3809 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3810 exit(0);
3812 object_property_add_child(object_get_root(), "machine",
3813 OBJECT(current_machine), &error_abort);
3814 cpu_exec_init_all();
3816 if (machine_class->hw_version) {
3817 qemu_set_version(machine_class->hw_version);
3820 /* Init CPU def lists, based on config
3821 * - Must be called after all the qemu_read_config_file() calls
3822 * - Must be called before list_cpus()
3823 * - Must be called before machine->init()
3825 cpudef_init();
3827 if (cpu_model && is_help_option(cpu_model)) {
3828 list_cpus(stdout, &fprintf, cpu_model);
3829 exit(0);
3832 /* Open the logfile at this point, if necessary. We can't open the logfile
3833 * when encountering either of the logging options (-d or -D) because the
3834 * other one may be encountered later on the command line, changing the
3835 * location or level of logging.
3837 if (log_mask) {
3838 int mask;
3839 if (log_file) {
3840 qemu_set_log_filename(log_file);
3843 mask = qemu_str_to_log_mask(log_mask);
3844 if (!mask) {
3845 qemu_print_log_usage(stdout);
3846 exit(1);
3848 qemu_set_log(mask);
3851 if (!is_daemonized()) {
3852 if (!trace_init_backends(trace_events, trace_file)) {
3853 exit(1);
3857 /* If no data_dir is specified then try to find it relative to the
3858 executable path. */
3859 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3860 data_dir[data_dir_idx] = os_find_datadir();
3861 if (data_dir[data_dir_idx] != NULL) {
3862 data_dir_idx++;
3865 /* If all else fails use the install path specified when building. */
3866 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3867 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3870 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3872 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3873 if (max_cpus > machine_class->max_cpus) {
3874 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3875 "supported by machine `%s' (%d)\n", max_cpus,
3876 machine_class->name, machine_class->max_cpus);
3877 exit(1);
3881 * Get the default machine options from the machine if it is not already
3882 * specified either by the configuration file or by the command line.
3884 if (machine_class->default_machine_opts) {
3885 qemu_opts_set_defaults(qemu_find_opts("machine"),
3886 machine_class->default_machine_opts, 0);
3889 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3890 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3892 if (!vga_model && !default_vga) {
3893 vga_interface_type = VGA_DEVICE;
3895 if (!has_defaults || machine_class->no_serial) {
3896 default_serial = 0;
3898 if (!has_defaults || machine_class->no_parallel) {
3899 default_parallel = 0;
3901 if (!has_defaults || !machine_class->use_virtcon) {
3902 default_virtcon = 0;
3904 if (!has_defaults || !machine_class->use_sclp) {
3905 default_sclp = 0;
3907 if (!has_defaults || machine_class->no_floppy) {
3908 default_floppy = 0;
3910 if (!has_defaults || machine_class->no_cdrom) {
3911 default_cdrom = 0;
3913 if (!has_defaults || machine_class->no_sdcard) {
3914 default_sdcard = 0;
3916 if (!has_defaults) {
3917 default_monitor = 0;
3918 default_net = 0;
3919 default_vga = 0;
3922 if (is_daemonized()) {
3923 /* According to documentation and historically, -nographic redirects
3924 * serial port, parallel port and monitor to stdio, which does not work
3925 * with -daemonize. We can redirect these to null instead, but since
3926 * -nographic is legacy, let's just error out.
3927 * We disallow -nographic only if all other ports are not redirected
3928 * explicitly, to not break existing legacy setups which uses
3929 * -nographic _and_ redirects all ports explicitly - this is valid
3930 * usage, -nographic is just a no-op in this case.
3932 if (display_type == DT_NOGRAPHIC
3933 && (default_parallel || default_serial
3934 || default_monitor || default_virtcon)) {
3935 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3936 exit(1);
3938 #ifdef CONFIG_CURSES
3939 if (display_type == DT_CURSES) {
3940 fprintf(stderr, "curses display can not be used with -daemonize\n");
3941 exit(1);
3943 #endif
3946 if (display_type == DT_NOGRAPHIC) {
3947 if (default_parallel)
3948 add_device_config(DEV_PARALLEL, "null");
3949 if (default_serial && default_monitor) {
3950 add_device_config(DEV_SERIAL, "mon:stdio");
3951 } else if (default_virtcon && default_monitor) {
3952 add_device_config(DEV_VIRTCON, "mon:stdio");
3953 } else if (default_sclp && default_monitor) {
3954 add_device_config(DEV_SCLP, "mon:stdio");
3955 } else {
3956 if (default_serial)
3957 add_device_config(DEV_SERIAL, "stdio");
3958 if (default_virtcon)
3959 add_device_config(DEV_VIRTCON, "stdio");
3960 if (default_sclp) {
3961 add_device_config(DEV_SCLP, "stdio");
3963 if (default_monitor)
3964 monitor_parse("stdio", "readline", false);
3966 } else {
3967 if (default_serial)
3968 add_device_config(DEV_SERIAL, "vc:80Cx24C");
3969 if (default_parallel)
3970 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
3971 if (default_monitor)
3972 monitor_parse("vc:80Cx24C", "readline", false);
3973 if (default_virtcon)
3974 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3975 if (default_sclp) {
3976 add_device_config(DEV_SCLP, "vc:80Cx24C");
3980 #if defined(CONFIG_VNC)
3981 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
3982 display_remote++;
3984 #endif
3985 if (display_type == DT_DEFAULT && !display_remote) {
3986 #if defined(CONFIG_GTK)
3987 display_type = DT_GTK;
3988 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
3989 display_type = DT_SDL;
3990 #elif defined(CONFIG_VNC)
3991 vnc_parse_func("localhost:0,to=99,id=default");
3992 show_vnc_port = 1;
3993 #else
3994 display_type = DT_NONE;
3995 #endif
3998 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
3999 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4000 "for SDL, ignoring option\n");
4002 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4003 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4004 "ignoring option\n");
4007 #if defined(CONFIG_GTK)
4008 if (display_type == DT_GTK) {
4009 early_gtk_display_init(request_opengl);
4011 #endif
4012 #if defined(CONFIG_SDL)
4013 if (display_type == DT_SDL) {
4014 sdl_display_early_init(request_opengl);
4016 #endif
4017 if (request_opengl == 1 && display_opengl == 0) {
4018 #if defined(CONFIG_OPENGL)
4019 fprintf(stderr, "OpenGL is not supported by the display.\n");
4020 #else
4021 fprintf(stderr, "QEMU was built without opengl support.\n");
4022 #endif
4023 exit(1);
4026 socket_init();
4028 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4029 exit(1);
4030 #ifdef CONFIG_VIRTFS
4031 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4032 exit(1);
4034 #endif
4036 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4037 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
4038 exit(1);
4041 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4042 != 0) {
4043 exit(0);
4046 if (qemu_opts_foreach(qemu_find_opts("object"),
4047 object_create, NULL, 0) != 0) {
4048 exit(1);
4051 machine_opts = qemu_get_machine_opts();
4052 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4053 1) < 0) {
4054 object_unref(OBJECT(current_machine));
4055 exit(1);
4058 configure_accelerator(current_machine);
4060 if (qtest_chrdev) {
4061 Error *local_err = NULL;
4062 qtest_init(qtest_chrdev, qtest_log, &local_err);
4063 if (local_err) {
4064 error_report_err(local_err);
4065 exit(1);
4069 machine_opts = qemu_get_machine_opts();
4070 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4071 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4072 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4073 bios_name = qemu_opt_get(machine_opts, "firmware");
4075 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4076 if (opts) {
4077 Error *local_err = NULL;
4079 boot_order = qemu_opt_get(opts, "order");
4080 if (boot_order) {
4081 validate_bootdevices(boot_order, &local_err);
4082 if (local_err) {
4083 error_report_err(local_err);
4084 exit(1);
4088 boot_once = qemu_opt_get(opts, "once");
4089 if (boot_once) {
4090 validate_bootdevices(boot_once, &local_err);
4091 if (local_err) {
4092 error_report_err(local_err);
4093 exit(1);
4097 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4098 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4101 if (!boot_order) {
4102 boot_order = machine_class->default_boot_order;
4105 if (!kernel_cmdline) {
4106 kernel_cmdline = "";
4107 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4110 linux_boot = (kernel_filename != NULL);
4112 if (!linux_boot && *kernel_cmdline != '\0') {
4113 fprintf(stderr, "-append only allowed with -kernel option\n");
4114 exit(1);
4117 if (!linux_boot && initrd_filename != NULL) {
4118 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4119 exit(1);
4122 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4123 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4124 exit(1);
4127 os_set_line_buffering();
4129 #ifdef CONFIG_SPICE
4130 /* spice needs the timers to be initialized by this point */
4131 qemu_spice_init();
4132 #endif
4134 cpu_ticks_init();
4135 if (icount_opts) {
4136 if (kvm_enabled() || xen_enabled()) {
4137 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4138 exit(1);
4140 configure_icount(icount_opts, &error_abort);
4141 qemu_opts_del(icount_opts);
4144 /* clean up network at qemu process termination */
4145 atexit(&net_cleanup);
4147 if (net_init_clients() < 0) {
4148 exit(1);
4151 #ifdef CONFIG_TPM
4152 if (tpm_init() < 0) {
4153 exit(1);
4155 #endif
4157 /* init the bluetooth world */
4158 if (foreach_device_config(DEV_BT, bt_parse))
4159 exit(1);
4161 if (!xen_enabled()) {
4162 /* On 32-bit hosts, QEMU is limited by virtual address space */
4163 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4164 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4165 exit(1);
4169 blk_mig_init();
4170 ram_mig_init();
4172 /* If the currently selected machine wishes to override the units-per-bus
4173 * property of its default HBA interface type, do so now. */
4174 if (machine_class->units_per_default_bus) {
4175 override_max_devs(machine_class->block_default_type,
4176 machine_class->units_per_default_bus);
4179 /* open the virtual block devices */
4180 if (snapshot)
4181 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4182 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4183 &machine_class->block_default_type, 1) != 0) {
4184 exit(1);
4187 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4188 CDROM_OPTS);
4189 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4190 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4192 parse_numa_opts(machine_class);
4194 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4195 exit(1);
4198 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4199 exit(1);
4200 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4201 exit(1);
4202 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4203 exit(1);
4204 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4205 exit(1);
4207 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4208 exit(1);
4210 /* If no default VGA is requested, the default is "none". */
4211 if (default_vga) {
4212 if (machine_class->default_display) {
4213 vga_model = machine_class->default_display;
4214 } else if (cirrus_vga_available()) {
4215 vga_model = "cirrus";
4216 } else if (vga_available()) {
4217 vga_model = "std";
4220 if (vga_model) {
4221 select_vgahw(vga_model);
4224 if (watchdog) {
4225 i = select_watchdog(watchdog);
4226 if (i > 0)
4227 exit (i == 1 ? 1 : 0);
4230 if (machine_class->compat_props) {
4231 qdev_prop_register_global_list(machine_class->compat_props);
4233 qemu_add_globals();
4235 qdev_machine_init();
4237 current_machine->ram_size = ram_size;
4238 current_machine->maxram_size = maxram_size;
4239 current_machine->ram_slots = ram_slots;
4240 current_machine->boot_order = boot_order;
4241 current_machine->cpu_model = cpu_model;
4243 machine_class->init(current_machine);
4245 realtime_init();
4247 audio_init();
4249 cpu_synchronize_all_post_init();
4251 numa_post_machine_init();
4253 /* init USB devices */
4254 if (usb_enabled()) {
4255 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4256 exit(1);
4259 /* init generic devices */
4260 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4261 exit(1);
4263 /* Did we create any drives that we failed to create a device for? */
4264 drive_check_orphaned();
4266 net_check_clients();
4268 if (boot_once) {
4269 Error *local_err = NULL;
4270 qemu_boot_set(boot_once, &local_err);
4271 if (local_err) {
4272 error_report("%s", error_get_pretty(local_err));
4273 exit(1);
4275 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4278 ds = init_displaystate();
4280 /* init local displays */
4281 switch (display_type) {
4282 case DT_NOGRAPHIC:
4283 (void)ds; /* avoid warning if no display is configured */
4284 break;
4285 #if defined(CONFIG_CURSES)
4286 case DT_CURSES:
4287 curses_display_init(ds, full_screen);
4288 break;
4289 #endif
4290 #if defined(CONFIG_SDL)
4291 case DT_SDL:
4292 sdl_display_init(ds, full_screen, no_frame);
4293 break;
4294 #elif defined(CONFIG_COCOA)
4295 case DT_SDL:
4296 cocoa_display_init(ds, full_screen);
4297 break;
4298 #endif
4299 #if defined(CONFIG_GTK)
4300 case DT_GTK:
4301 gtk_display_init(ds, full_screen, grab_on_hover);
4302 break;
4303 #endif
4304 default:
4305 break;
4308 /* must be after terminal init, SDL library changes signal handlers */
4309 os_setup_signal_handling();
4311 #ifdef CONFIG_VNC
4312 /* init remote displays */
4313 qemu_opts_foreach(qemu_find_opts("vnc"), vnc_init_func, NULL, 0);
4314 if (show_vnc_port) {
4315 printf("VNC server running on `%s'\n",
4316 vnc_display_local_addr("default"));
4318 #endif
4319 #ifdef CONFIG_SPICE
4320 if (using_spice) {
4321 qemu_spice_display_init();
4323 #endif
4325 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4326 exit(1);
4329 qdev_machine_creation_done();
4331 if (rom_load_all() != 0) {
4332 fprintf(stderr, "rom loading failed\n");
4333 exit(1);
4336 /* TODO: once all bus devices are qdevified, this should be done
4337 * when bus is created by qdev.c */
4338 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4339 qemu_run_machine_init_done_notifiers();
4341 /* Done notifiers can load ROMs */
4342 rom_load_done();
4344 qemu_system_reset(VMRESET_SILENT);
4345 if (loadvm) {
4346 if (load_vmstate(loadvm) < 0) {
4347 autostart = 0;
4351 qdev_prop_check_globals();
4352 if (vmstate_dump_file) {
4353 /* dump and exit */
4354 dump_vmstate_json_to_file(vmstate_dump_file);
4355 return 0;
4358 if (incoming) {
4359 Error *local_err = NULL;
4360 qemu_start_incoming_migration(incoming, &local_err);
4361 if (local_err) {
4362 error_report("-incoming %s: %s", incoming,
4363 error_get_pretty(local_err));
4364 error_free(local_err);
4365 exit(1);
4367 } else if (autostart) {
4368 vm_start();
4371 os_setup_post();
4373 if (is_daemonized()) {
4374 if (!trace_init_backends(trace_events, trace_file)) {
4375 exit(1);
4379 main_loop();
4380 bdrv_close_all();
4381 pause_all_vcpus();
4382 res_free();
4383 #ifdef CONFIG_TPM
4384 tpm_cleanup();
4385 #endif
4387 return 0;