Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob4c8a75d4201939847eab674dd90fc208bf4560b2
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "sysemu/accel.h"
65 #include "hw/usb.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "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 /* Trace unassigned memory or i/o accesses. */
196 bool trace_unassigned;
198 static NotifierList exit_notifiers =
199 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
201 static NotifierList machine_init_done_notifiers =
202 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
204 bool xen_allowed;
205 uint32_t xen_domid;
206 enum xen_mode xen_mode = XEN_EMULATE;
208 static int has_defaults = 1;
209 static int default_serial = 1;
210 static int default_parallel = 1;
211 static int default_virtcon = 1;
212 static int default_sclp = 1;
213 static int default_monitor = 1;
214 static int default_floppy = 1;
215 static int default_cdrom = 1;
216 static int default_sdcard = 1;
217 static int default_vga = 1;
219 static struct {
220 const char *driver;
221 int *flag;
222 } default_list[] = {
223 { .driver = "isa-serial", .flag = &default_serial },
224 { .driver = "isa-parallel", .flag = &default_parallel },
225 { .driver = "isa-fdc", .flag = &default_floppy },
226 { .driver = "ide-cd", .flag = &default_cdrom },
227 { .driver = "ide-hd", .flag = &default_cdrom },
228 { .driver = "ide-drive", .flag = &default_cdrom },
229 { .driver = "scsi-cd", .flag = &default_cdrom },
230 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
231 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
232 { .driver = "virtio-serial", .flag = &default_virtcon },
233 { .driver = "VGA", .flag = &default_vga },
234 { .driver = "isa-vga", .flag = &default_vga },
235 { .driver = "cirrus-vga", .flag = &default_vga },
236 { .driver = "isa-cirrus-vga", .flag = &default_vga },
237 { .driver = "vmware-svga", .flag = &default_vga },
238 { .driver = "qxl-vga", .flag = &default_vga },
241 static QemuOptsList qemu_rtc_opts = {
242 .name = "rtc",
243 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
244 .desc = {
246 .name = "base",
247 .type = QEMU_OPT_STRING,
249 .name = "clock",
250 .type = QEMU_OPT_STRING,
252 .name = "driftfix",
253 .type = QEMU_OPT_STRING,
255 { /* end of list */ }
259 static QemuOptsList qemu_sandbox_opts = {
260 .name = "sandbox",
261 .implied_opt_name = "enable",
262 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
263 .desc = {
265 .name = "enable",
266 .type = QEMU_OPT_BOOL,
268 { /* end of list */ }
272 static QemuOptsList qemu_trace_opts = {
273 .name = "trace",
274 .implied_opt_name = "trace",
275 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
276 .desc = {
278 .name = "events",
279 .type = QEMU_OPT_STRING,
281 .name = "file",
282 .type = QEMU_OPT_STRING,
284 { /* end of list */ }
288 static QemuOptsList qemu_option_rom_opts = {
289 .name = "option-rom",
290 .implied_opt_name = "romfile",
291 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
292 .desc = {
294 .name = "bootindex",
295 .type = QEMU_OPT_NUMBER,
296 }, {
297 .name = "romfile",
298 .type = QEMU_OPT_STRING,
300 { /* end of list */ }
304 static QemuOptsList qemu_machine_opts = {
305 .name = "machine",
306 .implied_opt_name = "type",
307 .merge_lists = true,
308 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
309 .desc = {
311 * no elements => accept any
312 * sanity checking will happen later
313 * when setting machine properties
319 static QemuOptsList qemu_boot_opts = {
320 .name = "boot-opts",
321 .implied_opt_name = "order",
322 .merge_lists = true,
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
324 .desc = {
326 .name = "order",
327 .type = QEMU_OPT_STRING,
328 }, {
329 .name = "once",
330 .type = QEMU_OPT_STRING,
331 }, {
332 .name = "menu",
333 .type = QEMU_OPT_BOOL,
334 }, {
335 .name = "splash",
336 .type = QEMU_OPT_STRING,
337 }, {
338 .name = "splash-time",
339 .type = QEMU_OPT_STRING,
340 }, {
341 .name = "reboot-timeout",
342 .type = QEMU_OPT_STRING,
343 }, {
344 .name = "strict",
345 .type = QEMU_OPT_BOOL,
347 { /*End of list */ }
351 static QemuOptsList qemu_add_fd_opts = {
352 .name = "add-fd",
353 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
354 .desc = {
356 .name = "fd",
357 .type = QEMU_OPT_NUMBER,
358 .help = "file descriptor of which a duplicate is added to fd set",
360 .name = "set",
361 .type = QEMU_OPT_NUMBER,
362 .help = "ID of the fd set to add fd to",
364 .name = "opaque",
365 .type = QEMU_OPT_STRING,
366 .help = "free-form string used to describe fd",
368 { /* end of list */ }
372 static QemuOptsList qemu_object_opts = {
373 .name = "object",
374 .implied_opt_name = "qom-type",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
376 .desc = {
381 static QemuOptsList qemu_tpmdev_opts = {
382 .name = "tpmdev",
383 .implied_opt_name = "type",
384 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
385 .desc = {
386 /* options are defined in the TPM backends */
387 { /* end of list */ }
391 static QemuOptsList qemu_realtime_opts = {
392 .name = "realtime",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
394 .desc = {
396 .name = "mlock",
397 .type = QEMU_OPT_BOOL,
399 { /* end of list */ }
403 static QemuOptsList qemu_msg_opts = {
404 .name = "msg",
405 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
406 .desc = {
408 .name = "timestamp",
409 .type = QEMU_OPT_BOOL,
411 { /* end of list */ }
415 static QemuOptsList qemu_name_opts = {
416 .name = "name",
417 .implied_opt_name = "guest",
418 .merge_lists = true,
419 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
420 .desc = {
422 .name = "guest",
423 .type = QEMU_OPT_STRING,
424 .help = "Sets the name of the guest.\n"
425 "This name will be displayed in the SDL window caption.\n"
426 "The name will also be used for the VNC server",
427 }, {
428 .name = "process",
429 .type = QEMU_OPT_STRING,
430 .help = "Sets the name of the QEMU process, as shown in top etc",
431 }, {
432 .name = "debug-threads",
433 .type = QEMU_OPT_BOOL,
434 .help = "When enabled, name the individual threads; defaults off.\n"
435 "NOTE: The thread names are for debugging and not a\n"
436 "stable API.",
438 { /* End of list */ }
442 static QemuOptsList qemu_mem_opts = {
443 .name = "memory",
444 .implied_opt_name = "size",
445 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
446 .merge_lists = true,
447 .desc = {
449 .name = "size",
450 .type = QEMU_OPT_SIZE,
453 .name = "slots",
454 .type = QEMU_OPT_NUMBER,
457 .name = "maxmem",
458 .type = QEMU_OPT_SIZE,
460 { /* end of list */ }
464 static QemuOptsList qemu_icount_opts = {
465 .name = "icount",
466 .implied_opt_name = "shift",
467 .merge_lists = true,
468 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
469 .desc = {
471 .name = "shift",
472 .type = QEMU_OPT_STRING,
473 }, {
474 .name = "align",
475 .type = QEMU_OPT_BOOL,
477 { /* end of list */ }
481 static QemuOptsList qemu_semihosting_config_opts = {
482 .name = "semihosting-config",
483 .implied_opt_name = "enable",
484 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
485 .desc = {
487 .name = "enable",
488 .type = QEMU_OPT_BOOL,
489 }, {
490 .name = "target",
491 .type = QEMU_OPT_STRING,
493 { /* end of list */ }
498 * Get machine options
500 * Returns: machine options (never null).
502 QemuOpts *qemu_get_machine_opts(void)
504 return qemu_find_opts_singleton("machine");
507 const char *qemu_get_vm_name(void)
509 return qemu_name;
512 static void res_free(void)
514 if (boot_splash_filedata != NULL) {
515 g_free(boot_splash_filedata);
516 boot_splash_filedata = NULL;
520 static int default_driver_check(QemuOpts *opts, void *opaque)
522 const char *driver = qemu_opt_get(opts, "driver");
523 int i;
525 if (!driver)
526 return 0;
527 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
528 if (strcmp(default_list[i].driver, driver) != 0)
529 continue;
530 *(default_list[i].flag) = 0;
532 return 0;
535 /***********************************************************/
536 /* QEMU state */
538 static RunState current_run_state = RUN_STATE_PRELAUNCH;
540 /* We use RUN_STATE_MAX but any invalid value will do */
541 static RunState vmstop_requested = RUN_STATE_MAX;
542 static QemuMutex vmstop_lock;
544 typedef struct {
545 RunState from;
546 RunState to;
547 } RunStateTransition;
549 static const RunStateTransition runstate_transitions_def[] = {
550 /* from -> to */
551 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
552 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
554 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
555 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
557 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
558 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
560 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
561 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
563 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
564 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
566 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
567 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
569 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
570 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
571 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
573 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
574 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
576 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
578 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
579 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
580 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
581 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
582 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
583 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
584 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
585 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
586 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
587 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
589 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
591 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
592 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
594 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
595 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
596 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
597 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
600 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
603 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_MAX, RUN_STATE_MAX },
608 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
610 bool runstate_check(RunState state)
612 return current_run_state == state;
615 static void runstate_init(void)
617 const RunStateTransition *p;
619 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
620 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
621 runstate_valid_transitions[p->from][p->to] = true;
624 qemu_mutex_init(&vmstop_lock);
627 /* This function will abort() on invalid state transitions */
628 void runstate_set(RunState new_state)
630 assert(new_state < RUN_STATE_MAX);
632 if (!runstate_valid_transitions[current_run_state][new_state]) {
633 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
634 RunState_lookup[current_run_state],
635 RunState_lookup[new_state]);
636 abort();
638 trace_runstate_set(new_state);
639 current_run_state = new_state;
642 int runstate_is_running(void)
644 return runstate_check(RUN_STATE_RUNNING);
647 bool runstate_needs_reset(void)
649 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
650 runstate_check(RUN_STATE_SHUTDOWN);
653 StatusInfo *qmp_query_status(Error **errp)
655 StatusInfo *info = g_malloc0(sizeof(*info));
657 info->running = runstate_is_running();
658 info->singlestep = singlestep;
659 info->status = current_run_state;
661 return info;
664 static bool qemu_vmstop_requested(RunState *r)
666 qemu_mutex_lock(&vmstop_lock);
667 *r = vmstop_requested;
668 vmstop_requested = RUN_STATE_MAX;
669 qemu_mutex_unlock(&vmstop_lock);
670 return *r < RUN_STATE_MAX;
673 void qemu_system_vmstop_request_prepare(void)
675 qemu_mutex_lock(&vmstop_lock);
678 void qemu_system_vmstop_request(RunState state)
680 vmstop_requested = state;
681 qemu_mutex_unlock(&vmstop_lock);
682 qemu_notify_event();
685 void vm_start(void)
687 RunState requested;
689 qemu_vmstop_requested(&requested);
690 if (runstate_is_running() && requested == RUN_STATE_MAX) {
691 return;
694 /* Ensure that a STOP/RESUME pair of events is emitted if a
695 * vmstop request was pending. The BLOCK_IO_ERROR event, for
696 * example, according to documentation is always followed by
697 * the STOP event.
699 if (runstate_is_running()) {
700 qapi_event_send_stop(&error_abort);
701 } else {
702 cpu_enable_ticks();
703 runstate_set(RUN_STATE_RUNNING);
704 vm_state_notify(1, RUN_STATE_RUNNING);
705 resume_all_vcpus();
708 qapi_event_send_resume(&error_abort);
712 /***********************************************************/
713 /* real time host monotonic timer */
715 static time_t qemu_time(void)
717 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
720 /***********************************************************/
721 /* host time/date access */
722 void qemu_get_timedate(struct tm *tm, int offset)
724 time_t ti = qemu_time();
726 ti += offset;
727 if (rtc_date_offset == -1) {
728 if (rtc_utc)
729 gmtime_r(&ti, tm);
730 else
731 localtime_r(&ti, tm);
732 } else {
733 ti -= rtc_date_offset;
734 gmtime_r(&ti, tm);
738 int qemu_timedate_diff(struct tm *tm)
740 time_t seconds;
742 if (rtc_date_offset == -1)
743 if (rtc_utc)
744 seconds = mktimegm(tm);
745 else {
746 struct tm tmp = *tm;
747 tmp.tm_isdst = -1; /* use timezone to figure it out */
748 seconds = mktime(&tmp);
750 else
751 seconds = mktimegm(tm) + rtc_date_offset;
753 return seconds - qemu_time();
756 static void configure_rtc_date_offset(const char *startdate, int legacy)
758 time_t rtc_start_date;
759 struct tm tm;
761 if (!strcmp(startdate, "now") && legacy) {
762 rtc_date_offset = -1;
763 } else {
764 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
765 &tm.tm_year,
766 &tm.tm_mon,
767 &tm.tm_mday,
768 &tm.tm_hour,
769 &tm.tm_min,
770 &tm.tm_sec) == 6) {
771 /* OK */
772 } else if (sscanf(startdate, "%d-%d-%d",
773 &tm.tm_year,
774 &tm.tm_mon,
775 &tm.tm_mday) == 3) {
776 tm.tm_hour = 0;
777 tm.tm_min = 0;
778 tm.tm_sec = 0;
779 } else {
780 goto date_fail;
782 tm.tm_year -= 1900;
783 tm.tm_mon--;
784 rtc_start_date = mktimegm(&tm);
785 if (rtc_start_date == -1) {
786 date_fail:
787 fprintf(stderr, "Invalid date format. Valid formats are:\n"
788 "'2006-06-17T16:01:21' or '2006-06-17'\n");
789 exit(1);
791 rtc_date_offset = qemu_time() - rtc_start_date;
795 static void configure_rtc(QemuOpts *opts)
797 const char *value;
799 value = qemu_opt_get(opts, "base");
800 if (value) {
801 if (!strcmp(value, "utc")) {
802 rtc_utc = 1;
803 } else if (!strcmp(value, "localtime")) {
804 rtc_utc = 0;
805 } else {
806 configure_rtc_date_offset(value, 0);
809 value = qemu_opt_get(opts, "clock");
810 if (value) {
811 if (!strcmp(value, "host")) {
812 rtc_clock = QEMU_CLOCK_HOST;
813 } else if (!strcmp(value, "rt")) {
814 rtc_clock = QEMU_CLOCK_REALTIME;
815 } else if (!strcmp(value, "vm")) {
816 rtc_clock = QEMU_CLOCK_VIRTUAL;
817 } else {
818 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
819 exit(1);
822 value = qemu_opt_get(opts, "driftfix");
823 if (value) {
824 if (!strcmp(value, "slew")) {
825 static GlobalProperty slew_lost_ticks[] = {
827 .driver = "mc146818rtc",
828 .property = "lost_tick_policy",
829 .value = "slew",
831 { /* end of list */ }
834 qdev_prop_register_global_list(slew_lost_ticks);
835 } else if (!strcmp(value, "none")) {
836 /* discard is default */
837 } else {
838 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
839 exit(1);
844 /***********************************************************/
845 /* Bluetooth support */
846 static int nb_hcis;
847 static int cur_hci;
848 static struct HCIInfo *hci_table[MAX_NICS];
850 struct HCIInfo *qemu_next_hci(void)
852 if (cur_hci == nb_hcis)
853 return &null_hci;
855 return hci_table[cur_hci++];
858 static int bt_hci_parse(const char *str)
860 struct HCIInfo *hci;
861 bdaddr_t bdaddr;
863 if (nb_hcis >= MAX_NICS) {
864 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
865 return -1;
868 hci = hci_init(str);
869 if (!hci)
870 return -1;
872 bdaddr.b[0] = 0x52;
873 bdaddr.b[1] = 0x54;
874 bdaddr.b[2] = 0x00;
875 bdaddr.b[3] = 0x12;
876 bdaddr.b[4] = 0x34;
877 bdaddr.b[5] = 0x56 + nb_hcis;
878 hci->bdaddr_set(hci, bdaddr.b);
880 hci_table[nb_hcis++] = hci;
882 return 0;
885 static void bt_vhci_add(int vlan_id)
887 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
889 if (!vlan->slave)
890 fprintf(stderr, "qemu: warning: adding a VHCI to "
891 "an empty scatternet %i\n", vlan_id);
893 bt_vhci_init(bt_new_hci(vlan));
896 static struct bt_device_s *bt_device_add(const char *opt)
898 struct bt_scatternet_s *vlan;
899 int vlan_id = 0;
900 char *endp = strstr(opt, ",vlan=");
901 int len = (endp ? endp - opt : strlen(opt)) + 1;
902 char devname[10];
904 pstrcpy(devname, MIN(sizeof(devname), len), opt);
906 if (endp) {
907 vlan_id = strtol(endp + 6, &endp, 0);
908 if (*endp) {
909 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
910 return 0;
914 vlan = qemu_find_bt_vlan(vlan_id);
916 if (!vlan->slave)
917 fprintf(stderr, "qemu: warning: adding a slave device to "
918 "an empty scatternet %i\n", vlan_id);
920 if (!strcmp(devname, "keyboard"))
921 return bt_keyboard_init(vlan);
923 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
924 return 0;
927 static int bt_parse(const char *opt)
929 const char *endp, *p;
930 int vlan;
932 if (strstart(opt, "hci", &endp)) {
933 if (!*endp || *endp == ',') {
934 if (*endp)
935 if (!strstart(endp, ",vlan=", 0))
936 opt = endp + 1;
938 return bt_hci_parse(opt);
940 } else if (strstart(opt, "vhci", &endp)) {
941 if (!*endp || *endp == ',') {
942 if (*endp) {
943 if (strstart(endp, ",vlan=", &p)) {
944 vlan = strtol(p, (char **) &endp, 0);
945 if (*endp) {
946 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
947 return 1;
949 } else {
950 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
951 return 1;
953 } else
954 vlan = 0;
956 bt_vhci_add(vlan);
957 return 0;
959 } else if (strstart(opt, "device:", &endp))
960 return !bt_device_add(endp);
962 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
963 return 1;
966 static int parse_sandbox(QemuOpts *opts, void *opaque)
968 /* FIXME: change this to true for 1.3 */
969 if (qemu_opt_get_bool(opts, "enable", false)) {
970 #ifdef CONFIG_SECCOMP
971 if (seccomp_start() < 0) {
972 qerror_report(ERROR_CLASS_GENERIC_ERROR,
973 "failed to install seccomp syscall filter in the kernel");
974 return -1;
976 #else
977 qerror_report(ERROR_CLASS_GENERIC_ERROR,
978 "sandboxing request but seccomp is not compiled into this build");
979 return -1;
980 #endif
983 return 0;
986 static int parse_name(QemuOpts *opts, void *opaque)
988 const char *proc_name;
990 if (qemu_opt_get(opts, "debug-threads")) {
991 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
993 qemu_name = qemu_opt_get(opts, "guest");
995 proc_name = qemu_opt_get(opts, "process");
996 if (proc_name) {
997 os_set_proc_name(proc_name);
1000 return 0;
1003 bool defaults_enabled(void)
1005 return has_defaults;
1008 bool usb_enabled(void)
1010 return machine_usb(current_machine);
1013 #ifndef _WIN32
1014 static int parse_add_fd(QemuOpts *opts, void *opaque)
1016 int fd, dupfd, flags;
1017 int64_t fdset_id;
1018 const char *fd_opaque = NULL;
1019 AddfdInfo *fdinfo;
1021 fd = qemu_opt_get_number(opts, "fd", -1);
1022 fdset_id = qemu_opt_get_number(opts, "set", -1);
1023 fd_opaque = qemu_opt_get(opts, "opaque");
1025 if (fd < 0) {
1026 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1027 "fd option is required and must be non-negative");
1028 return -1;
1031 if (fd <= STDERR_FILENO) {
1032 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1033 "fd cannot be a standard I/O stream");
1034 return -1;
1038 * All fds inherited across exec() necessarily have FD_CLOEXEC
1039 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1041 flags = fcntl(fd, F_GETFD);
1042 if (flags == -1 || (flags & FD_CLOEXEC)) {
1043 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1044 "fd is not valid or already in use");
1045 return -1;
1048 if (fdset_id < 0) {
1049 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1050 "set option is required and must be non-negative");
1051 return -1;
1054 #ifdef F_DUPFD_CLOEXEC
1055 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1056 #else
1057 dupfd = dup(fd);
1058 if (dupfd != -1) {
1059 qemu_set_cloexec(dupfd);
1061 #endif
1062 if (dupfd == -1) {
1063 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1064 "Error duplicating fd: %s", strerror(errno));
1065 return -1;
1068 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1069 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1070 &error_abort);
1071 g_free(fdinfo);
1073 return 0;
1076 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1078 int fd;
1080 fd = qemu_opt_get_number(opts, "fd", -1);
1081 close(fd);
1083 return 0;
1085 #endif
1087 /***********************************************************/
1088 /* QEMU Block devices */
1090 #define HD_OPTS "media=disk"
1091 #define CDROM_OPTS "media=cdrom"
1092 #define FD_OPTS ""
1093 #define PFLASH_OPTS ""
1094 #define MTD_OPTS ""
1095 #define SD_OPTS ""
1097 static int drive_init_func(QemuOpts *opts, void *opaque)
1099 BlockInterfaceType *block_default_type = opaque;
1101 return drive_new(opts, *block_default_type) == NULL;
1104 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1106 if (qemu_opt_get(opts, "snapshot") == NULL) {
1107 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1109 return 0;
1112 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1113 int index, const char *optstr)
1115 QemuOpts *opts;
1116 DriveInfo *dinfo;
1118 if (!enable || drive_get_by_index(type, index)) {
1119 return;
1122 opts = drive_add(type, index, NULL, optstr);
1123 if (snapshot) {
1124 drive_enable_snapshot(opts, NULL);
1127 dinfo = drive_new(opts, type);
1128 if (!dinfo) {
1129 exit(1);
1131 dinfo->is_default = true;
1135 static QemuOptsList qemu_smp_opts = {
1136 .name = "smp-opts",
1137 .implied_opt_name = "cpus",
1138 .merge_lists = true,
1139 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1140 .desc = {
1142 .name = "cpus",
1143 .type = QEMU_OPT_NUMBER,
1144 }, {
1145 .name = "sockets",
1146 .type = QEMU_OPT_NUMBER,
1147 }, {
1148 .name = "cores",
1149 .type = QEMU_OPT_NUMBER,
1150 }, {
1151 .name = "threads",
1152 .type = QEMU_OPT_NUMBER,
1153 }, {
1154 .name = "maxcpus",
1155 .type = QEMU_OPT_NUMBER,
1157 { /*End of list */ }
1161 static void smp_parse(QemuOpts *opts)
1163 if (opts) {
1165 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1166 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1167 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1168 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1170 /* compute missing values, prefer sockets over cores over threads */
1171 if (cpus == 0 || sockets == 0) {
1172 sockets = sockets > 0 ? sockets : 1;
1173 cores = cores > 0 ? cores : 1;
1174 threads = threads > 0 ? threads : 1;
1175 if (cpus == 0) {
1176 cpus = cores * threads * sockets;
1178 } else if (cores == 0) {
1179 threads = threads > 0 ? threads : 1;
1180 cores = cpus / (sockets * threads);
1181 } else if (threads == 0) {
1182 threads = cpus / (cores * sockets);
1183 } else if (sockets * cores * threads < cpus) {
1184 fprintf(stderr, "cpu topology: error: "
1185 "sockets (%u) * cores (%u) * threads (%u) < "
1186 "smp_cpus (%u)\n",
1187 sockets, cores, threads, cpus);
1188 exit(1);
1191 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1193 smp_cpus = cpus;
1194 smp_cores = cores > 0 ? cores : 1;
1195 smp_threads = threads > 0 ? threads : 1;
1199 if (max_cpus == 0) {
1200 max_cpus = smp_cpus;
1203 if (max_cpus > MAX_CPUMASK_BITS) {
1204 fprintf(stderr, "Unsupported number of maxcpus\n");
1205 exit(1);
1207 if (max_cpus < smp_cpus) {
1208 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1209 exit(1);
1214 static void realtime_init(void)
1216 if (enable_mlock) {
1217 if (os_mlock() < 0) {
1218 fprintf(stderr, "qemu: locking memory failed\n");
1219 exit(1);
1225 static void configure_msg(QemuOpts *opts)
1227 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1230 /***********************************************************/
1231 /* USB devices */
1233 static int usb_device_add(const char *devname)
1235 USBDevice *dev = NULL;
1236 #ifndef CONFIG_LINUX
1237 const char *p;
1238 #endif
1240 if (!usb_enabled()) {
1241 return -1;
1244 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1245 dev = usbdevice_create(devname);
1246 if (dev)
1247 goto done;
1249 /* the other ones */
1250 #ifndef CONFIG_LINUX
1251 /* only the linux version is qdev-ified, usb-bsd still needs this */
1252 if (strstart(devname, "host:", &p)) {
1253 dev = usb_host_device_open(usb_bus_find(-1), p);
1255 #endif
1256 if (!dev)
1257 return -1;
1259 done:
1260 return 0;
1263 static int usb_device_del(const char *devname)
1265 int bus_num, addr;
1266 const char *p;
1268 if (strstart(devname, "host:", &p)) {
1269 return -1;
1272 if (!usb_enabled()) {
1273 return -1;
1276 p = strchr(devname, '.');
1277 if (!p)
1278 return -1;
1279 bus_num = strtoul(devname, NULL, 0);
1280 addr = strtoul(p + 1, NULL, 0);
1282 return usb_device_delete_addr(bus_num, addr);
1285 static int usb_parse(const char *cmdline)
1287 int r;
1288 r = usb_device_add(cmdline);
1289 if (r < 0) {
1290 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1292 return r;
1295 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1297 const char *devname = qdict_get_str(qdict, "devname");
1298 if (usb_device_add(devname) < 0) {
1299 error_report("could not add USB device '%s'", devname);
1303 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1305 const char *devname = qdict_get_str(qdict, "devname");
1306 if (usb_device_del(devname) < 0) {
1307 error_report("could not delete USB device '%s'", devname);
1311 /***********************************************************/
1312 /* machine registration */
1314 MachineState *current_machine;
1316 static void machine_class_init(ObjectClass *oc, void *data)
1318 MachineClass *mc = MACHINE_CLASS(oc);
1319 QEMUMachine *qm = data;
1321 mc->family = qm->family;
1322 mc->name = qm->name;
1323 mc->alias = qm->alias;
1324 mc->desc = qm->desc;
1325 mc->init = qm->init;
1326 mc->reset = qm->reset;
1327 mc->hot_add_cpu = qm->hot_add_cpu;
1328 mc->kvm_type = qm->kvm_type;
1329 mc->block_default_type = qm->block_default_type;
1330 mc->units_per_default_bus = qm->units_per_default_bus;
1331 mc->max_cpus = qm->max_cpus;
1332 mc->no_serial = qm->no_serial;
1333 mc->no_parallel = qm->no_parallel;
1334 mc->use_virtcon = qm->use_virtcon;
1335 mc->use_sclp = qm->use_sclp;
1336 mc->no_floppy = qm->no_floppy;
1337 mc->no_cdrom = qm->no_cdrom;
1338 mc->no_sdcard = qm->no_sdcard;
1339 mc->has_dynamic_sysbus = qm->has_dynamic_sysbus;
1340 mc->is_default = qm->is_default;
1341 mc->default_machine_opts = qm->default_machine_opts;
1342 mc->default_boot_order = qm->default_boot_order;
1343 mc->default_display = qm->default_display;
1344 mc->compat_props = qm->compat_props;
1345 mc->hw_version = qm->hw_version;
1348 int qemu_register_machine(QEMUMachine *m)
1350 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1351 TypeInfo ti = {
1352 .name = name,
1353 .parent = TYPE_MACHINE,
1354 .class_init = machine_class_init,
1355 .class_data = (void *)m,
1358 type_register(&ti);
1359 g_free(name);
1361 return 0;
1364 static MachineClass *find_machine(const char *name)
1366 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1367 MachineClass *mc = NULL;
1369 for (el = machines; el; el = el->next) {
1370 MachineClass *temp = el->data;
1372 if (!strcmp(temp->name, name)) {
1373 mc = temp;
1374 break;
1376 if (temp->alias &&
1377 !strcmp(temp->alias, name)) {
1378 mc = temp;
1379 break;
1383 g_slist_free(machines);
1384 return mc;
1387 MachineClass *find_default_machine(void)
1389 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1390 MachineClass *mc = NULL;
1392 for (el = machines; el; el = el->next) {
1393 MachineClass *temp = el->data;
1395 if (temp->is_default) {
1396 mc = temp;
1397 break;
1401 g_slist_free(machines);
1402 return mc;
1405 MachineInfoList *qmp_query_machines(Error **errp)
1407 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1408 MachineInfoList *mach_list = NULL;
1410 for (el = machines; el; el = el->next) {
1411 MachineClass *mc = el->data;
1412 MachineInfoList *entry;
1413 MachineInfo *info;
1415 info = g_malloc0(sizeof(*info));
1416 if (mc->is_default) {
1417 info->has_is_default = true;
1418 info->is_default = true;
1421 if (mc->alias) {
1422 info->has_alias = true;
1423 info->alias = g_strdup(mc->alias);
1426 info->name = g_strdup(mc->name);
1427 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1429 entry = g_malloc0(sizeof(*entry));
1430 entry->value = info;
1431 entry->next = mach_list;
1432 mach_list = entry;
1435 g_slist_free(machines);
1436 return mach_list;
1439 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1441 ObjectProperty *prop;
1443 if (!qemu_opt_has_help_opt(opts)) {
1444 return 0;
1447 QTAILQ_FOREACH(prop, &OBJECT(machine)->properties, node) {
1448 if (!prop->set) {
1449 continue;
1452 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1453 prop->name, prop->type);
1454 if (prop->description) {
1455 error_printf(" (%s)\n", prop->description);
1456 } else {
1457 error_printf("\n");
1461 return 1;
1464 /***********************************************************/
1465 /* main execution loop */
1467 struct vm_change_state_entry {
1468 VMChangeStateHandler *cb;
1469 void *opaque;
1470 QLIST_ENTRY (vm_change_state_entry) entries;
1473 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1475 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1476 void *opaque)
1478 VMChangeStateEntry *e;
1480 e = g_malloc0(sizeof (*e));
1482 e->cb = cb;
1483 e->opaque = opaque;
1484 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1485 return e;
1488 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1490 QLIST_REMOVE (e, entries);
1491 g_free (e);
1494 void vm_state_notify(int running, RunState state)
1496 VMChangeStateEntry *e, *next;
1498 trace_vm_state_notify(running, state);
1500 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1501 e->cb(e->opaque, running, state);
1505 /* reset/shutdown handler */
1507 typedef struct QEMUResetEntry {
1508 QTAILQ_ENTRY(QEMUResetEntry) entry;
1509 QEMUResetHandler *func;
1510 void *opaque;
1511 } QEMUResetEntry;
1513 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1514 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1515 static int reset_requested;
1516 static int shutdown_requested, shutdown_signal = -1;
1517 static pid_t shutdown_pid;
1518 static int powerdown_requested;
1519 static int debug_requested;
1520 static int suspend_requested;
1521 static WakeupReason wakeup_reason;
1522 static NotifierList powerdown_notifiers =
1523 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1524 static NotifierList suspend_notifiers =
1525 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1526 static NotifierList wakeup_notifiers =
1527 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1528 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1530 int qemu_shutdown_requested_get(void)
1532 return shutdown_requested;
1535 int qemu_reset_requested_get(void)
1537 return reset_requested;
1540 static int qemu_shutdown_requested(void)
1542 return atomic_xchg(&shutdown_requested, 0);
1545 static void qemu_kill_report(void)
1547 if (!qtest_driver() && shutdown_signal != -1) {
1548 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1549 if (shutdown_pid == 0) {
1550 /* This happens for eg ^C at the terminal, so it's worth
1551 * avoiding printing an odd message in that case.
1553 fputc('\n', stderr);
1554 } else {
1555 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1557 shutdown_signal = -1;
1561 static int qemu_reset_requested(void)
1563 int r = reset_requested;
1564 reset_requested = 0;
1565 return r;
1568 static int qemu_suspend_requested(void)
1570 int r = suspend_requested;
1571 suspend_requested = 0;
1572 return r;
1575 static WakeupReason qemu_wakeup_requested(void)
1577 return wakeup_reason;
1580 static int qemu_powerdown_requested(void)
1582 int r = powerdown_requested;
1583 powerdown_requested = 0;
1584 return r;
1587 static int qemu_debug_requested(void)
1589 int r = debug_requested;
1590 debug_requested = 0;
1591 return r;
1594 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1596 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1598 re->func = func;
1599 re->opaque = opaque;
1600 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1603 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1605 QEMUResetEntry *re;
1607 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1608 if (re->func == func && re->opaque == opaque) {
1609 QTAILQ_REMOVE(&reset_handlers, re, entry);
1610 g_free(re);
1611 return;
1616 void qemu_devices_reset(void)
1618 QEMUResetEntry *re, *nre;
1620 /* reset all devices */
1621 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1622 re->func(re->opaque);
1626 void qemu_system_reset(bool report)
1628 MachineClass *mc;
1630 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1632 if (mc && mc->reset) {
1633 mc->reset();
1634 } else {
1635 qemu_devices_reset();
1637 if (report) {
1638 qapi_event_send_reset(&error_abort);
1640 cpu_synchronize_all_post_reset();
1643 void qemu_system_reset_request(void)
1645 if (no_reboot) {
1646 shutdown_requested = 1;
1647 } else {
1648 reset_requested = 1;
1650 cpu_stop_current();
1651 qemu_notify_event();
1654 static void qemu_system_suspend(void)
1656 pause_all_vcpus();
1657 notifier_list_notify(&suspend_notifiers, NULL);
1658 runstate_set(RUN_STATE_SUSPENDED);
1659 qapi_event_send_suspend(&error_abort);
1662 void qemu_system_suspend_request(void)
1664 if (runstate_check(RUN_STATE_SUSPENDED)) {
1665 return;
1667 suspend_requested = 1;
1668 cpu_stop_current();
1669 qemu_notify_event();
1672 void qemu_register_suspend_notifier(Notifier *notifier)
1674 notifier_list_add(&suspend_notifiers, notifier);
1677 void qemu_system_wakeup_request(WakeupReason reason)
1679 trace_system_wakeup_request(reason);
1681 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1682 return;
1684 if (!(wakeup_reason_mask & (1 << reason))) {
1685 return;
1687 runstate_set(RUN_STATE_RUNNING);
1688 wakeup_reason = reason;
1689 qemu_notify_event();
1692 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1694 if (enabled) {
1695 wakeup_reason_mask |= (1 << reason);
1696 } else {
1697 wakeup_reason_mask &= ~(1 << reason);
1701 void qemu_register_wakeup_notifier(Notifier *notifier)
1703 notifier_list_add(&wakeup_notifiers, notifier);
1706 void qemu_system_killed(int signal, pid_t pid)
1708 shutdown_signal = signal;
1709 shutdown_pid = pid;
1710 no_shutdown = 0;
1711 qemu_system_shutdown_request();
1714 void qemu_system_shutdown_request(void)
1716 trace_qemu_system_shutdown_request();
1717 shutdown_requested = 1;
1718 qemu_notify_event();
1721 static void qemu_system_powerdown(void)
1723 qapi_event_send_powerdown(&error_abort);
1724 notifier_list_notify(&powerdown_notifiers, NULL);
1727 void qemu_system_powerdown_request(void)
1729 trace_qemu_system_powerdown_request();
1730 powerdown_requested = 1;
1731 qemu_notify_event();
1734 void qemu_register_powerdown_notifier(Notifier *notifier)
1736 notifier_list_add(&powerdown_notifiers, notifier);
1739 void qemu_system_debug_request(void)
1741 debug_requested = 1;
1742 qemu_notify_event();
1745 static bool main_loop_should_exit(void)
1747 RunState r;
1748 if (qemu_debug_requested()) {
1749 vm_stop(RUN_STATE_DEBUG);
1751 if (qemu_suspend_requested()) {
1752 qemu_system_suspend();
1754 if (qemu_shutdown_requested()) {
1755 qemu_kill_report();
1756 qapi_event_send_shutdown(&error_abort);
1757 if (no_shutdown) {
1758 vm_stop(RUN_STATE_SHUTDOWN);
1759 } else {
1760 return true;
1763 if (qemu_reset_requested()) {
1764 pause_all_vcpus();
1765 cpu_synchronize_all_states();
1766 qemu_system_reset(VMRESET_REPORT);
1767 resume_all_vcpus();
1768 if (runstate_needs_reset()) {
1769 runstate_set(RUN_STATE_PAUSED);
1772 if (qemu_wakeup_requested()) {
1773 pause_all_vcpus();
1774 cpu_synchronize_all_states();
1775 qemu_system_reset(VMRESET_SILENT);
1776 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1777 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1778 resume_all_vcpus();
1779 qapi_event_send_wakeup(&error_abort);
1781 if (qemu_powerdown_requested()) {
1782 qemu_system_powerdown();
1784 if (qemu_vmstop_requested(&r)) {
1785 vm_stop(r);
1787 return false;
1790 static void main_loop(void)
1792 bool nonblocking;
1793 int last_io = 0;
1794 #ifdef CONFIG_PROFILER
1795 int64_t ti;
1796 #endif
1797 do {
1798 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1799 #ifdef CONFIG_PROFILER
1800 ti = profile_getclock();
1801 #endif
1802 last_io = main_loop_wait(nonblocking);
1803 #ifdef CONFIG_PROFILER
1804 dev_time += profile_getclock() - ti;
1805 #endif
1806 } while (!main_loop_should_exit());
1809 static void version(void)
1811 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
1814 static void QEMU_NORETURN help(int exitcode)
1816 version();
1817 printf("usage: %s [options] [disk_image]\n\n"
1818 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1819 error_get_progname());
1821 #define QEMU_OPTIONS_GENERATE_HELP
1822 #include "qemu-options-wrapper.h"
1824 printf("\nDuring emulation, the following keys are useful:\n"
1825 "ctrl-alt-f toggle full screen\n"
1826 "ctrl-alt-n switch to virtual console 'n'\n"
1827 "ctrl-alt toggle mouse and keyboard grab\n"
1828 "\n"
1829 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1831 exit(exitcode);
1834 #define HAS_ARG 0x0001
1836 typedef struct QEMUOption {
1837 const char *name;
1838 int flags;
1839 int index;
1840 uint32_t arch_mask;
1841 } QEMUOption;
1843 static const QEMUOption qemu_options[] = {
1844 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1845 #define QEMU_OPTIONS_GENERATE_OPTIONS
1846 #include "qemu-options-wrapper.h"
1847 { NULL },
1850 static bool vga_available(void)
1852 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
1855 static bool cirrus_vga_available(void)
1857 return object_class_by_name("cirrus-vga")
1858 || object_class_by_name("isa-cirrus-vga");
1861 static bool vmware_vga_available(void)
1863 return object_class_by_name("vmware-svga");
1866 static bool qxl_vga_available(void)
1868 return object_class_by_name("qxl-vga");
1871 static bool tcx_vga_available(void)
1873 return object_class_by_name("SUNW,tcx");
1876 static bool cg3_vga_available(void)
1878 return object_class_by_name("cgthree");
1881 static void select_vgahw (const char *p)
1883 const char *opts;
1885 assert(vga_interface_type == VGA_NONE);
1886 if (strstart(p, "std", &opts)) {
1887 if (vga_available()) {
1888 vga_interface_type = VGA_STD;
1889 } else {
1890 fprintf(stderr, "Error: standard VGA not available\n");
1891 exit(0);
1893 } else if (strstart(p, "cirrus", &opts)) {
1894 if (cirrus_vga_available()) {
1895 vga_interface_type = VGA_CIRRUS;
1896 } else {
1897 fprintf(stderr, "Error: Cirrus VGA not available\n");
1898 exit(0);
1900 } else if (strstart(p, "vmware", &opts)) {
1901 if (vmware_vga_available()) {
1902 vga_interface_type = VGA_VMWARE;
1903 } else {
1904 fprintf(stderr, "Error: VMWare SVGA not available\n");
1905 exit(0);
1907 } else if (strstart(p, "xenfb", &opts)) {
1908 vga_interface_type = VGA_XENFB;
1909 } else if (strstart(p, "qxl", &opts)) {
1910 if (qxl_vga_available()) {
1911 vga_interface_type = VGA_QXL;
1912 } else {
1913 fprintf(stderr, "Error: QXL VGA not available\n");
1914 exit(0);
1916 } else if (strstart(p, "tcx", &opts)) {
1917 if (tcx_vga_available()) {
1918 vga_interface_type = VGA_TCX;
1919 } else {
1920 fprintf(stderr, "Error: TCX framebuffer not available\n");
1921 exit(0);
1923 } else if (strstart(p, "cg3", &opts)) {
1924 if (cg3_vga_available()) {
1925 vga_interface_type = VGA_CG3;
1926 } else {
1927 fprintf(stderr, "Error: CG3 framebuffer not available\n");
1928 exit(0);
1930 } else if (!strstart(p, "none", &opts)) {
1931 invalid_vga:
1932 fprintf(stderr, "Unknown vga type: %s\n", p);
1933 exit(1);
1935 while (*opts) {
1936 const char *nextopt;
1938 if (strstart(opts, ",retrace=", &nextopt)) {
1939 opts = nextopt;
1940 if (strstart(opts, "dumb", &nextopt))
1941 vga_retrace_method = VGA_RETRACE_DUMB;
1942 else if (strstart(opts, "precise", &nextopt))
1943 vga_retrace_method = VGA_RETRACE_PRECISE;
1944 else goto invalid_vga;
1945 } else goto invalid_vga;
1946 opts = nextopt;
1950 static DisplayType select_display(const char *p)
1952 const char *opts;
1953 DisplayType display = DT_DEFAULT;
1955 if (strstart(p, "sdl", &opts)) {
1956 #ifdef CONFIG_SDL
1957 display = DT_SDL;
1958 while (*opts) {
1959 const char *nextopt;
1961 if (strstart(opts, ",frame=", &nextopt)) {
1962 opts = nextopt;
1963 if (strstart(opts, "on", &nextopt)) {
1964 no_frame = 0;
1965 } else if (strstart(opts, "off", &nextopt)) {
1966 no_frame = 1;
1967 } else {
1968 goto invalid_sdl_args;
1970 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1971 opts = nextopt;
1972 if (strstart(opts, "on", &nextopt)) {
1973 alt_grab = 1;
1974 } else if (strstart(opts, "off", &nextopt)) {
1975 alt_grab = 0;
1976 } else {
1977 goto invalid_sdl_args;
1979 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1980 opts = nextopt;
1981 if (strstart(opts, "on", &nextopt)) {
1982 ctrl_grab = 1;
1983 } else if (strstart(opts, "off", &nextopt)) {
1984 ctrl_grab = 0;
1985 } else {
1986 goto invalid_sdl_args;
1988 } else if (strstart(opts, ",window_close=", &nextopt)) {
1989 opts = nextopt;
1990 if (strstart(opts, "on", &nextopt)) {
1991 no_quit = 0;
1992 } else if (strstart(opts, "off", &nextopt)) {
1993 no_quit = 1;
1994 } else {
1995 goto invalid_sdl_args;
1997 } else if (strstart(opts, ",gl=", &nextopt)) {
1998 opts = nextopt;
1999 if (strstart(opts, "on", &nextopt)) {
2000 request_opengl = 1;
2001 } else if (strstart(opts, "off", &nextopt)) {
2002 request_opengl = 0;
2003 } else {
2004 goto invalid_sdl_args;
2006 } else {
2007 invalid_sdl_args:
2008 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2009 exit(1);
2011 opts = nextopt;
2013 #else
2014 fprintf(stderr, "SDL support is disabled\n");
2015 exit(1);
2016 #endif
2017 } else if (strstart(p, "vnc", &opts)) {
2018 #ifdef CONFIG_VNC
2019 if (*opts == '=') {
2020 if (vnc_parse_func(opts+1) == NULL) {
2021 exit(1);
2023 } else {
2024 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2025 exit(1);
2027 #else
2028 fprintf(stderr, "VNC support is disabled\n");
2029 exit(1);
2030 #endif
2031 } else if (strstart(p, "curses", &opts)) {
2032 #ifdef CONFIG_CURSES
2033 display = DT_CURSES;
2034 #else
2035 fprintf(stderr, "Curses support is disabled\n");
2036 exit(1);
2037 #endif
2038 } else if (strstart(p, "gtk", &opts)) {
2039 #ifdef CONFIG_GTK
2040 display = DT_GTK;
2041 while (*opts) {
2042 const char *nextopt;
2044 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2045 opts = nextopt;
2046 if (strstart(opts, "on", &nextopt)) {
2047 grab_on_hover = true;
2048 } else if (strstart(opts, "off", &nextopt)) {
2049 grab_on_hover = false;
2050 } else {
2051 goto invalid_gtk_args;
2053 } else {
2054 invalid_gtk_args:
2055 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2056 exit(1);
2058 opts = nextopt;
2060 #else
2061 fprintf(stderr, "GTK support is disabled\n");
2062 exit(1);
2063 #endif
2064 } else if (strstart(p, "none", &opts)) {
2065 display = DT_NONE;
2066 } else {
2067 fprintf(stderr, "Unknown display type: %s\n", p);
2068 exit(1);
2071 return display;
2074 static int balloon_parse(const char *arg)
2076 QemuOpts *opts;
2078 if (strcmp(arg, "none") == 0) {
2079 return 0;
2082 if (!strncmp(arg, "virtio", 6)) {
2083 if (arg[6] == ',') {
2084 /* have params -> parse them */
2085 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2086 if (!opts)
2087 return -1;
2088 } else {
2089 /* create empty opts */
2090 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2091 &error_abort);
2093 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2094 return 0;
2097 return -1;
2100 #if defined(CONFIG_RUBY)
2101 /* Ruby interface for QEMU. See README.EXT for programming example. */
2102 #warning mit ruby
2103 #include <ruby.h>
2105 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2107 int i;
2108 for (i = 0; i < argc; i++) {
2109 argv[i] = StringValue(argv[i]);
2110 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2112 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2113 //~ mode = 0666; /* default value */
2114 //~ }
2115 return INT2FIX(argc);
2118 void Init_qemu(void)
2120 printf("%s\n", __func__);
2121 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2122 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2123 #if 0
2124 rb_define_method();
2125 #endif
2127 #endif /* CONFIG_RUBY */
2129 #if defined(CONFIG_DLL)
2130 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2132 return FALSE;
2134 #endif /* CONFIG_DLL */
2136 char *qemu_find_file(int type, const char *name)
2138 int i;
2139 const char *subdir;
2140 char *buf;
2142 /* Try the name as a straight path first */
2143 if (access(name, R_OK) == 0) {
2144 trace_load_file(name, name);
2145 return g_strdup(name);
2148 switch (type) {
2149 case QEMU_FILE_TYPE_BIOS:
2150 subdir = "";
2151 break;
2152 case QEMU_FILE_TYPE_KEYMAP:
2153 subdir = "keymaps/";
2154 break;
2155 default:
2156 abort();
2159 for (i = 0; i < data_dir_idx; i++) {
2160 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2161 if (access(buf, R_OK) == 0) {
2162 trace_load_file(name, buf);
2163 return buf;
2165 g_free(buf);
2167 return NULL;
2170 static int device_help_func(QemuOpts *opts, void *opaque)
2172 return qdev_device_help(opts);
2175 static int device_init_func(QemuOpts *opts, void *opaque)
2177 DeviceState *dev;
2179 dev = qdev_device_add(opts);
2180 if (!dev)
2181 return -1;
2182 object_unref(OBJECT(dev));
2183 return 0;
2186 static int chardev_init_func(QemuOpts *opts, void *opaque)
2188 Error *local_err = NULL;
2190 qemu_chr_new_from_opts(opts, NULL, &local_err);
2191 if (local_err) {
2192 error_report_err(local_err);
2193 return -1;
2195 return 0;
2198 #ifdef CONFIG_VIRTFS
2199 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2201 int ret;
2202 ret = qemu_fsdev_add(opts);
2204 return ret;
2206 #endif
2208 static int mon_init_func(QemuOpts *opts, void *opaque)
2210 CharDriverState *chr;
2211 const char *chardev;
2212 const char *mode;
2213 int flags;
2215 mode = qemu_opt_get(opts, "mode");
2216 if (mode == NULL) {
2217 mode = "readline";
2219 if (strcmp(mode, "readline") == 0) {
2220 flags = MONITOR_USE_READLINE;
2221 } else if (strcmp(mode, "control") == 0) {
2222 flags = MONITOR_USE_CONTROL;
2223 } else {
2224 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2225 exit(1);
2228 if (qemu_opt_get_bool(opts, "pretty", 0))
2229 flags |= MONITOR_USE_PRETTY;
2231 if (qemu_opt_get_bool(opts, "default", 0))
2232 flags |= MONITOR_IS_DEFAULT;
2234 chardev = qemu_opt_get(opts, "chardev");
2235 chr = qemu_chr_find(chardev);
2236 if (chr == NULL) {
2237 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2238 exit(1);
2241 qemu_chr_fe_claim_no_fail(chr);
2242 monitor_init(chr, flags);
2243 return 0;
2246 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2248 static int monitor_device_index = 0;
2249 Error *local_err = NULL;
2250 QemuOpts *opts;
2251 const char *p;
2252 char label[32];
2253 int def = 0;
2255 if (strstart(optarg, "chardev:", &p)) {
2256 snprintf(label, sizeof(label), "%s", p);
2257 } else {
2258 snprintf(label, sizeof(label), "compat_monitor%d",
2259 monitor_device_index);
2260 if (monitor_device_index == 0) {
2261 def = 1;
2263 opts = qemu_chr_parse_compat(label, optarg);
2264 if (!opts) {
2265 fprintf(stderr, "parse error: %s\n", optarg);
2266 exit(1);
2270 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &local_err);
2271 if (!opts) {
2272 error_report_err(local_err);
2273 exit(1);
2275 qemu_opt_set(opts, "mode", mode, &error_abort);
2276 qemu_opt_set(opts, "chardev", label, &error_abort);
2277 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2278 if (def)
2279 qemu_opt_set(opts, "default", "on", &error_abort);
2280 monitor_device_index++;
2283 struct device_config {
2284 enum {
2285 DEV_USB, /* -usbdevice */
2286 DEV_BT, /* -bt */
2287 DEV_SERIAL, /* -serial */
2288 DEV_PARALLEL, /* -parallel */
2289 DEV_VIRTCON, /* -virtioconsole */
2290 DEV_DEBUGCON, /* -debugcon */
2291 DEV_GDB, /* -gdb, -s */
2292 DEV_SCLP, /* s390 sclp */
2293 } type;
2294 const char *cmdline;
2295 Location loc;
2296 QTAILQ_ENTRY(device_config) next;
2299 static QTAILQ_HEAD(, device_config) device_configs =
2300 QTAILQ_HEAD_INITIALIZER(device_configs);
2302 static void add_device_config(int type, const char *cmdline)
2304 struct device_config *conf;
2306 conf = g_malloc0(sizeof(*conf));
2307 conf->type = type;
2308 conf->cmdline = cmdline;
2309 loc_save(&conf->loc);
2310 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2313 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2315 struct device_config *conf;
2316 int rc;
2318 QTAILQ_FOREACH(conf, &device_configs, next) {
2319 if (conf->type != type)
2320 continue;
2321 loc_push_restore(&conf->loc);
2322 rc = func(conf->cmdline);
2323 loc_pop(&conf->loc);
2324 if (rc) {
2325 return rc;
2328 return 0;
2331 static int serial_parse(const char *devname)
2333 static int index = 0;
2334 char label[32];
2336 if (strcmp(devname, "none") == 0)
2337 return 0;
2338 if (index == MAX_SERIAL_PORTS) {
2339 fprintf(stderr, "qemu: too many serial ports\n");
2340 exit(1);
2342 snprintf(label, sizeof(label), "serial%d", index);
2343 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2344 if (!serial_hds[index]) {
2345 fprintf(stderr, "qemu: could not connect serial device"
2346 " to character backend '%s'\n", devname);
2347 return -1;
2349 index++;
2350 return 0;
2353 static int parallel_parse(const char *devname)
2355 static int index = 0;
2356 char label[32];
2358 if (strcmp(devname, "none") == 0)
2359 return 0;
2360 if (index == MAX_PARALLEL_PORTS) {
2361 fprintf(stderr, "qemu: too many parallel ports\n");
2362 exit(1);
2364 snprintf(label, sizeof(label), "parallel%d", index);
2365 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2366 if (!parallel_hds[index]) {
2367 fprintf(stderr, "qemu: could not connect parallel device"
2368 " to character backend '%s'\n", devname);
2369 return -1;
2371 index++;
2372 return 0;
2375 static int virtcon_parse(const char *devname)
2377 QemuOptsList *device = qemu_find_opts("device");
2378 static int index = 0;
2379 char label[32];
2380 QemuOpts *bus_opts, *dev_opts;
2382 if (strcmp(devname, "none") == 0)
2383 return 0;
2384 if (index == MAX_VIRTIO_CONSOLES) {
2385 fprintf(stderr, "qemu: too many virtio consoles\n");
2386 exit(1);
2389 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2390 if (arch_type == QEMU_ARCH_S390X) {
2391 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390", &error_abort);
2392 } else {
2393 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci", &error_abort);
2396 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2397 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2399 snprintf(label, sizeof(label), "virtcon%d", index);
2400 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2401 if (!virtcon_hds[index]) {
2402 fprintf(stderr, "qemu: could not connect virtio console"
2403 " to character backend '%s'\n", devname);
2404 return -1;
2406 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2408 index++;
2409 return 0;
2412 static int sclp_parse(const char *devname)
2414 QemuOptsList *device = qemu_find_opts("device");
2415 static int index = 0;
2416 char label[32];
2417 QemuOpts *dev_opts;
2419 if (strcmp(devname, "none") == 0) {
2420 return 0;
2422 if (index == MAX_SCLP_CONSOLES) {
2423 fprintf(stderr, "qemu: too many sclp consoles\n");
2424 exit(1);
2427 assert(arch_type == QEMU_ARCH_S390X);
2429 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2430 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2432 snprintf(label, sizeof(label), "sclpcon%d", index);
2433 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2434 if (!sclp_hds[index]) {
2435 fprintf(stderr, "qemu: could not connect sclp console"
2436 " to character backend '%s'\n", devname);
2437 return -1;
2439 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2441 index++;
2442 return 0;
2445 static int debugcon_parse(const char *devname)
2447 QemuOpts *opts;
2449 if (!qemu_chr_new("debugcon", devname, NULL)) {
2450 exit(1);
2452 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2453 if (!opts) {
2454 fprintf(stderr, "qemu: already have a debugcon device\n");
2455 exit(1);
2457 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2458 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2459 return 0;
2462 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2464 const MachineClass *mc1 = a, *mc2 = b;
2465 int res;
2467 if (mc1->family == NULL) {
2468 if (mc2->family == NULL) {
2469 /* Compare standalone machine types against each other; they sort
2470 * in increasing order.
2472 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2473 object_class_get_name(OBJECT_CLASS(mc2)));
2476 /* Standalone machine types sort after families. */
2477 return 1;
2480 if (mc2->family == NULL) {
2481 /* Families sort before standalone machine types. */
2482 return -1;
2485 /* Families sort between each other alphabetically increasingly. */
2486 res = strcmp(mc1->family, mc2->family);
2487 if (res != 0) {
2488 return res;
2491 /* Within the same family, machine types sort in decreasing order. */
2492 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2493 object_class_get_name(OBJECT_CLASS(mc1)));
2496 static MachineClass *machine_parse(const char *name)
2498 MachineClass *mc = NULL;
2499 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2501 if (name) {
2502 mc = find_machine(name);
2504 if (mc) {
2505 g_slist_free(machines);
2506 return mc;
2508 if (name && !is_help_option(name)) {
2509 error_report("Unsupported machine type");
2510 error_printf("Use -machine help to list supported machines!\n");
2511 } else {
2512 printf("Supported machines are:\n");
2513 machines = g_slist_sort(machines, machine_class_cmp);
2514 for (el = machines; el; el = el->next) {
2515 MachineClass *mc = el->data;
2516 if (mc->alias) {
2517 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2519 printf("%-20s %s%s\n", mc->name, mc->desc,
2520 mc->is_default ? " (default)" : "");
2524 g_slist_free(machines);
2525 exit(!name || !is_help_option(name));
2528 void qemu_add_exit_notifier(Notifier *notify)
2530 notifier_list_add(&exit_notifiers, notify);
2533 void qemu_remove_exit_notifier(Notifier *notify)
2535 notifier_remove(notify);
2538 static void qemu_run_exit_notifiers(void)
2540 notifier_list_notify(&exit_notifiers, NULL);
2543 void qemu_add_machine_init_done_notifier(Notifier *notify)
2545 notifier_list_add(&machine_init_done_notifiers, notify);
2548 static void qemu_run_machine_init_done_notifiers(void)
2550 notifier_list_notify(&machine_init_done_notifiers, NULL);
2553 static const QEMUOption *lookup_opt(int argc, char **argv,
2554 const char **poptarg, int *poptind)
2556 const QEMUOption *popt;
2557 int optind = *poptind;
2558 char *r = argv[optind];
2559 const char *optarg;
2561 loc_set_cmdline(argv, optind, 1);
2562 optind++;
2563 /* Treat --foo the same as -foo. */
2564 if (r[1] == '-')
2565 r++;
2566 popt = qemu_options;
2567 for(;;) {
2568 if (!popt->name) {
2569 error_report("invalid option");
2570 exit(1);
2572 if (!strcmp(popt->name, r + 1))
2573 break;
2574 popt++;
2576 if (popt->flags & HAS_ARG) {
2577 if (optind >= argc) {
2578 error_report("requires an argument");
2579 exit(1);
2581 optarg = argv[optind++];
2582 loc_set_cmdline(argv, optind - 2, 2);
2583 } else {
2584 optarg = NULL;
2587 *poptarg = optarg;
2588 *poptind = optind;
2590 return popt;
2593 static gpointer malloc_and_trace(gsize n_bytes)
2595 void *ptr = malloc(n_bytes);
2596 trace_g_malloc(n_bytes, ptr);
2597 return ptr;
2600 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2602 void *ptr = realloc(mem, n_bytes);
2603 trace_g_realloc(mem, n_bytes, ptr);
2604 return ptr;
2607 static void free_and_trace(gpointer mem)
2609 trace_g_free(mem);
2610 free(mem);
2613 static int machine_set_property(const char *name, const char *value,
2614 void *opaque)
2616 Object *obj = OBJECT(opaque);
2617 Error *local_err = NULL;
2618 char *c, *qom_name;
2620 if (strcmp(name, "type") == 0) {
2621 return 0;
2624 qom_name = g_strdup(name);
2625 c = qom_name;
2626 while (*c++) {
2627 if (*c == '_') {
2628 *c = '-';
2632 object_property_parse(obj, value, qom_name, &local_err);
2633 g_free(qom_name);
2635 if (local_err) {
2636 error_report_err(local_err);
2637 return -1;
2640 return 0;
2643 static int object_create(QemuOpts *opts, void *opaque)
2645 Error *err = NULL;
2646 char *type = NULL;
2647 char *id = NULL;
2648 void *dummy = NULL;
2649 OptsVisitor *ov;
2650 QDict *pdict;
2652 ov = opts_visitor_new(opts);
2653 pdict = qemu_opts_to_qdict(opts, NULL);
2655 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2656 if (err) {
2657 goto out;
2660 qdict_del(pdict, "qom-type");
2661 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2662 if (err) {
2663 goto out;
2666 qdict_del(pdict, "id");
2667 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2668 if (err) {
2669 goto out;
2672 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2673 if (err) {
2674 goto out;
2676 visit_end_struct(opts_get_visitor(ov), &err);
2677 if (err) {
2678 qmp_object_del(id, NULL);
2681 out:
2682 opts_visitor_cleanup(ov);
2684 QDECREF(pdict);
2685 g_free(id);
2686 g_free(type);
2687 g_free(dummy);
2688 if (err) {
2689 error_report_err(err);
2690 return -1;
2692 return 0;
2695 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size)
2697 uint64_t sz;
2698 const char *mem_str;
2699 const char *maxmem_str, *slots_str;
2700 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2701 1024 * 1024;
2702 QemuOpts *opts = qemu_find_opts_singleton("memory");
2704 sz = 0;
2705 mem_str = qemu_opt_get(opts, "size");
2706 if (mem_str) {
2707 if (!*mem_str) {
2708 error_report("missing 'size' option value");
2709 exit(EXIT_FAILURE);
2712 sz = qemu_opt_get_size(opts, "size", ram_size);
2714 /* Fix up legacy suffix-less format */
2715 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2716 uint64_t overflow_check = sz;
2718 sz <<= 20;
2719 if ((sz >> 20) != overflow_check) {
2720 error_report("too large 'size' option value");
2721 exit(EXIT_FAILURE);
2726 /* backward compatibility behaviour for case "-m 0" */
2727 if (sz == 0) {
2728 sz = default_ram_size;
2731 sz = QEMU_ALIGN_UP(sz, 8192);
2732 ram_size = sz;
2733 if (ram_size != sz) {
2734 error_report("ram size too large");
2735 exit(EXIT_FAILURE);
2738 /* store value for the future use */
2739 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2740 *maxram_size = ram_size;
2742 maxmem_str = qemu_opt_get(opts, "maxmem");
2743 slots_str = qemu_opt_get(opts, "slots");
2744 if (maxmem_str && slots_str) {
2745 uint64_t slots;
2747 sz = qemu_opt_get_size(opts, "maxmem", 0);
2748 slots = qemu_opt_get_number(opts, "slots", 0);
2749 if (sz < ram_size) {
2750 error_report("invalid value of -m option maxmem: "
2751 "maximum memory size (0x%" PRIx64 ") must be at least "
2752 "the initial memory size (0x" RAM_ADDR_FMT ")",
2753 sz, ram_size);
2754 exit(EXIT_FAILURE);
2755 } else if (sz > ram_size) {
2756 if (!slots) {
2757 error_report("invalid value of -m option: maxmem was "
2758 "specified, but no hotplug slots were specified");
2759 exit(EXIT_FAILURE);
2761 } else if (slots) {
2762 error_report("invalid value of -m option maxmem: "
2763 "memory slots were specified but maximum memory size "
2764 "(0x%" PRIx64 ") is equal to the initial memory size "
2765 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2766 exit(EXIT_FAILURE);
2769 *maxram_size = sz;
2770 *ram_slots = slots;
2771 } else if ((!maxmem_str && slots_str) ||
2772 (maxmem_str && !slots_str)) {
2773 error_report("invalid -m option value: missing "
2774 "'%s' option", slots_str ? "maxmem" : "slots");
2775 exit(EXIT_FAILURE);
2779 int main(int argc, char **argv)
2781 int i;
2782 int snapshot, linux_boot;
2783 const char *initrd_filename;
2784 const char *kernel_filename, *kernel_cmdline;
2785 const char *boot_order = NULL;
2786 const char *boot_once = NULL;
2787 DisplayState *ds;
2788 int cyls, heads, secs, translation;
2789 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2790 QemuOptsList *olist;
2791 int optind;
2792 const char *optarg;
2793 const char *loadvm = NULL;
2794 MachineClass *machine_class;
2795 const char *cpu_model;
2796 const char *vga_model = NULL;
2797 const char *qtest_chrdev = NULL;
2798 const char *qtest_log = NULL;
2799 const char *pid_file = NULL;
2800 const char *incoming = NULL;
2801 #ifdef CONFIG_VNC
2802 int show_vnc_port = 0;
2803 #endif
2804 bool defconfig = true;
2805 bool userconfig = true;
2806 const char *log_mask = NULL;
2807 const char *log_file = NULL;
2808 GMemVTable mem_trace = {
2809 .malloc = malloc_and_trace,
2810 .realloc = realloc_and_trace,
2811 .free = free_and_trace,
2813 const char *trace_events = NULL;
2814 const char *trace_file = NULL;
2815 ram_addr_t maxram_size;
2816 uint64_t ram_slots = 0;
2817 FILE *vmstate_dump_file = NULL;
2818 Error *main_loop_err = NULL;
2820 qemu_init_cpu_loop();
2821 qemu_mutex_lock_iothread();
2823 atexit(qemu_run_exit_notifiers);
2824 error_set_progname(argv[0]);
2825 qemu_init_exec_dir(argv[0]);
2827 g_mem_set_vtable(&mem_trace);
2829 module_call_init(MODULE_INIT_QOM);
2831 qemu_add_opts(&qemu_drive_opts);
2832 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2833 qemu_add_drive_opts(&qemu_common_drive_opts);
2834 qemu_add_drive_opts(&qemu_drive_opts);
2835 qemu_add_opts(&qemu_chardev_opts);
2836 qemu_add_opts(&qemu_device_opts);
2837 qemu_add_opts(&qemu_netdev_opts);
2838 qemu_add_opts(&qemu_net_opts);
2839 qemu_add_opts(&qemu_rtc_opts);
2840 qemu_add_opts(&qemu_global_opts);
2841 qemu_add_opts(&qemu_mon_opts);
2842 qemu_add_opts(&qemu_trace_opts);
2843 qemu_add_opts(&qemu_option_rom_opts);
2844 qemu_add_opts(&qemu_machine_opts);
2845 qemu_add_opts(&qemu_mem_opts);
2846 qemu_add_opts(&qemu_smp_opts);
2847 qemu_add_opts(&qemu_boot_opts);
2848 qemu_add_opts(&qemu_sandbox_opts);
2849 qemu_add_opts(&qemu_add_fd_opts);
2850 qemu_add_opts(&qemu_object_opts);
2851 qemu_add_opts(&qemu_tpmdev_opts);
2852 qemu_add_opts(&qemu_realtime_opts);
2853 qemu_add_opts(&qemu_msg_opts);
2854 qemu_add_opts(&qemu_name_opts);
2855 qemu_add_opts(&qemu_numa_opts);
2856 qemu_add_opts(&qemu_icount_opts);
2857 qemu_add_opts(&qemu_semihosting_config_opts);
2859 runstate_init();
2861 rtc_clock = QEMU_CLOCK_HOST;
2863 QLIST_INIT (&vm_change_state_head);
2864 os_setup_early_signal_handling();
2866 module_call_init(MODULE_INIT_MACHINE);
2867 machine_class = 0;
2868 optarg = strrchr(argv[0], '/');
2869 if (optarg != 0) {
2870 machine_class = find_machine(optarg + 1);
2872 if (!machine_class) {
2873 machine_class = find_default_machine();
2875 cpu_model = NULL;
2876 snapshot = 0;
2877 cyls = heads = secs = 0;
2878 translation = BIOS_ATA_TRANSLATION_AUTO;
2880 nb_nics = 0;
2882 bdrv_init_with_whitelist();
2884 autostart = 1;
2886 /* first pass of option parsing */
2887 optind = 1;
2888 while (optind < argc) {
2889 if (argv[optind][0] != '-') {
2890 /* disk image */
2891 optind++;
2892 } else {
2893 const QEMUOption *popt;
2895 popt = lookup_opt(argc, argv, &optarg, &optind);
2896 switch (popt->index) {
2897 case QEMU_OPTION_nodefconfig:
2898 defconfig = false;
2899 break;
2900 case QEMU_OPTION_nouserconfig:
2901 userconfig = false;
2902 break;
2907 if (defconfig) {
2908 int ret;
2909 ret = qemu_read_default_config_files(userconfig);
2910 if (ret < 0) {
2911 exit(1);
2915 /* second pass of option parsing */
2916 optind = 1;
2917 for(;;) {
2918 if (optind >= argc)
2919 break;
2920 if (argv[optind][0] != '-') {
2921 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2922 } else {
2923 const QEMUOption *popt;
2925 popt = lookup_opt(argc, argv, &optarg, &optind);
2926 if (!(popt->arch_mask & arch_type)) {
2927 printf("Option %s not supported for this target\n", popt->name);
2928 exit(1);
2930 switch(popt->index) {
2931 case QEMU_OPTION_no_kvm_irqchip: {
2932 olist = qemu_find_opts("machine");
2933 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2934 break;
2936 case QEMU_OPTION_cpu:
2937 /* hw initialization will check this */
2938 cpu_model = optarg;
2939 break;
2940 case QEMU_OPTION_hda:
2942 char buf[256];
2943 if (cyls == 0)
2944 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2945 else
2946 snprintf(buf, sizeof(buf),
2947 "%s,cyls=%d,heads=%d,secs=%d%s",
2948 HD_OPTS , cyls, heads, secs,
2949 translation == BIOS_ATA_TRANSLATION_LBA ?
2950 ",trans=lba" :
2951 translation == BIOS_ATA_TRANSLATION_NONE ?
2952 ",trans=none" : "");
2953 drive_add(IF_DEFAULT, 0, optarg, buf);
2954 break;
2956 case QEMU_OPTION_hdb:
2957 case QEMU_OPTION_hdc:
2958 case QEMU_OPTION_hdd:
2959 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2960 HD_OPTS);
2961 break;
2962 case QEMU_OPTION_drive:
2963 if (drive_def(optarg) == NULL) {
2964 exit(1);
2966 break;
2967 case QEMU_OPTION_set:
2968 if (qemu_set_option(optarg) != 0)
2969 exit(1);
2970 break;
2971 case QEMU_OPTION_global:
2972 if (qemu_global_option(optarg) != 0)
2973 exit(1);
2974 break;
2975 case QEMU_OPTION_mtdblock:
2976 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
2977 break;
2978 case QEMU_OPTION_sd:
2979 drive_add(IF_SD, -1, optarg, SD_OPTS);
2980 break;
2981 case QEMU_OPTION_pflash:
2982 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
2983 break;
2984 case QEMU_OPTION_snapshot:
2985 snapshot = 1;
2986 break;
2987 case QEMU_OPTION_hdachs:
2989 const char *p;
2990 p = optarg;
2991 cyls = strtol(p, (char **)&p, 0);
2992 if (cyls < 1 || cyls > 16383)
2993 goto chs_fail;
2994 if (*p != ',')
2995 goto chs_fail;
2996 p++;
2997 heads = strtol(p, (char **)&p, 0);
2998 if (heads < 1 || heads > 16)
2999 goto chs_fail;
3000 if (*p != ',')
3001 goto chs_fail;
3002 p++;
3003 secs = strtol(p, (char **)&p, 0);
3004 if (secs < 1 || secs > 63)
3005 goto chs_fail;
3006 if (*p == ',') {
3007 p++;
3008 if (!strcmp(p, "large")) {
3009 translation = BIOS_ATA_TRANSLATION_LARGE;
3010 } else if (!strcmp(p, "rechs")) {
3011 translation = BIOS_ATA_TRANSLATION_RECHS;
3012 } else if (!strcmp(p, "none")) {
3013 translation = BIOS_ATA_TRANSLATION_NONE;
3014 } else if (!strcmp(p, "lba")) {
3015 translation = BIOS_ATA_TRANSLATION_LBA;
3016 } else if (!strcmp(p, "auto")) {
3017 translation = BIOS_ATA_TRANSLATION_AUTO;
3018 } else {
3019 goto chs_fail;
3021 } else if (*p != '\0') {
3022 chs_fail:
3023 fprintf(stderr, "qemu: invalid physical CHS format\n");
3024 exit(1);
3026 if (hda_opts != NULL) {
3027 qemu_opt_set_number(hda_opts, "cyls", cyls,
3028 &error_abort);
3029 qemu_opt_set_number(hda_opts, "heads", heads,
3030 &error_abort);
3031 qemu_opt_set_number(hda_opts, "secs", secs,
3032 &error_abort);
3033 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3034 qemu_opt_set(hda_opts, "trans", "large",
3035 &error_abort);
3036 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3037 qemu_opt_set(hda_opts, "trans", "rechs",
3038 &error_abort);
3039 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3040 qemu_opt_set(hda_opts, "trans", "lba",
3041 &error_abort);
3042 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3043 qemu_opt_set(hda_opts, "trans", "none",
3044 &error_abort);
3048 break;
3049 case QEMU_OPTION_numa:
3050 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3051 if (!opts) {
3052 exit(1);
3054 break;
3055 case QEMU_OPTION_display:
3056 display_type = select_display(optarg);
3057 break;
3058 case QEMU_OPTION_nographic:
3059 display_type = DT_NOGRAPHIC;
3060 break;
3061 case QEMU_OPTION_curses:
3062 #ifdef CONFIG_CURSES
3063 display_type = DT_CURSES;
3064 #else
3065 fprintf(stderr, "Curses support is disabled\n");
3066 exit(1);
3067 #endif
3068 break;
3069 case QEMU_OPTION_portrait:
3070 graphic_rotate = 90;
3071 break;
3072 case QEMU_OPTION_rotate:
3073 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3074 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3075 graphic_rotate != 180 && graphic_rotate != 270) {
3076 fprintf(stderr,
3077 "qemu: only 90, 180, 270 deg rotation is available\n");
3078 exit(1);
3080 break;
3081 case QEMU_OPTION_kernel:
3082 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3083 &error_abort);
3084 break;
3085 case QEMU_OPTION_initrd:
3086 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3087 &error_abort);
3088 break;
3089 case QEMU_OPTION_append:
3090 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3091 &error_abort);
3092 break;
3093 case QEMU_OPTION_dtb:
3094 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3095 &error_abort);
3096 break;
3097 case QEMU_OPTION_cdrom:
3098 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3099 break;
3100 case QEMU_OPTION_boot:
3101 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3102 if (!opts) {
3103 exit(1);
3105 break;
3106 case QEMU_OPTION_fda:
3107 case QEMU_OPTION_fdb:
3108 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3109 optarg, FD_OPTS);
3110 break;
3111 case QEMU_OPTION_no_fd_bootchk:
3112 fd_bootchk = 0;
3113 break;
3114 case QEMU_OPTION_netdev:
3115 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3116 exit(1);
3118 break;
3119 case QEMU_OPTION_net:
3120 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3121 exit(1);
3123 break;
3124 #ifdef CONFIG_LIBISCSI
3125 case QEMU_OPTION_iscsi:
3126 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3127 if (!opts) {
3128 exit(1);
3130 break;
3131 #endif
3132 #ifdef CONFIG_SLIRP
3133 case QEMU_OPTION_tftp:
3134 legacy_tftp_prefix = optarg;
3135 break;
3136 case QEMU_OPTION_bootp:
3137 legacy_bootp_filename = optarg;
3138 break;
3139 case QEMU_OPTION_redir:
3140 if (net_slirp_redir(optarg) < 0)
3141 exit(1);
3142 break;
3143 #endif
3144 case QEMU_OPTION_bt:
3145 add_device_config(DEV_BT, optarg);
3146 break;
3147 case QEMU_OPTION_audio_help:
3148 AUD_help ();
3149 exit (0);
3150 break;
3151 case QEMU_OPTION_soundhw:
3152 select_soundhw (optarg);
3153 break;
3154 case QEMU_OPTION_h:
3155 help(0);
3156 break;
3157 case QEMU_OPTION_version:
3158 version();
3159 exit(0);
3160 break;
3161 case QEMU_OPTION_m:
3162 opts = qemu_opts_parse(qemu_find_opts("memory"),
3163 optarg, 1);
3164 if (!opts) {
3165 exit(EXIT_FAILURE);
3167 break;
3168 #ifdef CONFIG_TPM
3169 case QEMU_OPTION_tpmdev:
3170 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3171 exit(1);
3173 break;
3174 #endif
3175 case QEMU_OPTION_mempath:
3176 mem_path = optarg;
3177 break;
3178 case QEMU_OPTION_mem_prealloc:
3179 mem_prealloc = 1;
3180 break;
3181 case QEMU_OPTION_d:
3182 log_mask = optarg;
3183 break;
3184 case QEMU_OPTION_D:
3185 log_file = optarg;
3186 break;
3187 case QEMU_OPTION_s:
3188 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3189 break;
3190 case QEMU_OPTION_gdb:
3191 add_device_config(DEV_GDB, optarg);
3192 break;
3193 case QEMU_OPTION_L:
3194 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3195 data_dir[data_dir_idx++] = optarg;
3197 break;
3198 case QEMU_OPTION_bios:
3199 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3200 &error_abort);
3201 break;
3202 case QEMU_OPTION_singlestep:
3203 singlestep = 1;
3204 break;
3205 case QEMU_OPTION_S:
3206 autostart = 0;
3207 break;
3208 case QEMU_OPTION_k:
3209 keyboard_layout = optarg;
3210 break;
3211 case QEMU_OPTION_localtime:
3212 rtc_utc = 0;
3213 break;
3214 case QEMU_OPTION_vga:
3215 vga_model = optarg;
3216 default_vga = 0;
3217 break;
3218 case QEMU_OPTION_g:
3220 const char *p;
3221 int w, h, depth;
3222 p = optarg;
3223 w = strtol(p, (char **)&p, 10);
3224 if (w <= 0) {
3225 graphic_error:
3226 fprintf(stderr, "qemu: invalid resolution or depth\n");
3227 exit(1);
3229 if (*p != 'x')
3230 goto graphic_error;
3231 p++;
3232 h = strtol(p, (char **)&p, 10);
3233 if (h <= 0)
3234 goto graphic_error;
3235 if (*p == 'x') {
3236 p++;
3237 depth = strtol(p, (char **)&p, 10);
3238 if (depth != 8 && depth != 15 && depth != 16 &&
3239 depth != 24 && depth != 32)
3240 goto graphic_error;
3241 } else if (*p == '\0') {
3242 depth = graphic_depth;
3243 } else {
3244 goto graphic_error;
3247 graphic_width = w;
3248 graphic_height = h;
3249 graphic_depth = depth;
3251 break;
3252 case QEMU_OPTION_echr:
3254 char *r;
3255 term_escape_char = strtol(optarg, &r, 0);
3256 if (r == optarg)
3257 printf("Bad argument to echr\n");
3258 break;
3260 case QEMU_OPTION_monitor:
3261 default_monitor = 0;
3262 if (strncmp(optarg, "none", 4)) {
3263 monitor_parse(optarg, "readline", false);
3265 break;
3266 case QEMU_OPTION_qmp:
3267 monitor_parse(optarg, "control", false);
3268 default_monitor = 0;
3269 break;
3270 case QEMU_OPTION_qmp_pretty:
3271 monitor_parse(optarg, "control", true);
3272 default_monitor = 0;
3273 break;
3274 case QEMU_OPTION_mon:
3275 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3276 if (!opts) {
3277 exit(1);
3279 default_monitor = 0;
3280 break;
3281 case QEMU_OPTION_chardev:
3282 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3283 if (!opts) {
3284 exit(1);
3286 break;
3287 case QEMU_OPTION_fsdev:
3288 olist = qemu_find_opts("fsdev");
3289 if (!olist) {
3290 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3291 exit(1);
3293 opts = qemu_opts_parse(olist, optarg, 1);
3294 if (!opts) {
3295 exit(1);
3297 break;
3298 case QEMU_OPTION_virtfs: {
3299 QemuOpts *fsdev;
3300 QemuOpts *device;
3301 const char *writeout, *sock_fd, *socket;
3303 olist = qemu_find_opts("virtfs");
3304 if (!olist) {
3305 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3306 exit(1);
3308 opts = qemu_opts_parse(olist, optarg, 1);
3309 if (!opts) {
3310 exit(1);
3313 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3314 qemu_opt_get(opts, "mount_tag") == NULL) {
3315 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3316 exit(1);
3318 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3319 qemu_opt_get(opts, "mount_tag"),
3320 1, NULL);
3321 if (!fsdev) {
3322 fprintf(stderr, "duplicate fsdev id: %s\n",
3323 qemu_opt_get(opts, "mount_tag"));
3324 exit(1);
3327 writeout = qemu_opt_get(opts, "writeout");
3328 if (writeout) {
3329 #ifdef CONFIG_SYNC_FILE_RANGE
3330 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3331 #else
3332 fprintf(stderr, "writeout=immediate not supported on "
3333 "this platform\n");
3334 exit(1);
3335 #endif
3337 qemu_opt_set(fsdev, "fsdriver",
3338 qemu_opt_get(opts, "fsdriver"), &error_abort);
3339 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3340 &error_abort);
3341 qemu_opt_set(fsdev, "security_model",
3342 qemu_opt_get(opts, "security_model"),
3343 &error_abort);
3344 socket = qemu_opt_get(opts, "socket");
3345 if (socket) {
3346 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3348 sock_fd = qemu_opt_get(opts, "sock_fd");
3349 if (sock_fd) {
3350 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3353 qemu_opt_set_bool(fsdev, "readonly",
3354 qemu_opt_get_bool(opts, "readonly", 0),
3355 &error_abort);
3356 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3357 &error_abort);
3358 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3359 qemu_opt_set(device, "fsdev",
3360 qemu_opt_get(opts, "mount_tag"), &error_abort);
3361 qemu_opt_set(device, "mount_tag",
3362 qemu_opt_get(opts, "mount_tag"), &error_abort);
3363 break;
3365 case QEMU_OPTION_virtfs_synth: {
3366 QemuOpts *fsdev;
3367 QemuOpts *device;
3369 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3370 1, NULL);
3371 if (!fsdev) {
3372 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3373 exit(1);
3375 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3377 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3378 &error_abort);
3379 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3380 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3381 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3382 break;
3384 case QEMU_OPTION_serial:
3385 add_device_config(DEV_SERIAL, optarg);
3386 default_serial = 0;
3387 if (strncmp(optarg, "mon:", 4) == 0) {
3388 default_monitor = 0;
3390 break;
3391 case QEMU_OPTION_watchdog:
3392 if (watchdog) {
3393 fprintf(stderr,
3394 "qemu: only one watchdog option may be given\n");
3395 return 1;
3397 watchdog = optarg;
3398 break;
3399 case QEMU_OPTION_watchdog_action:
3400 if (select_watchdog_action(optarg) == -1) {
3401 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3402 exit(1);
3404 break;
3405 case QEMU_OPTION_virtiocon:
3406 add_device_config(DEV_VIRTCON, optarg);
3407 default_virtcon = 0;
3408 if (strncmp(optarg, "mon:", 4) == 0) {
3409 default_monitor = 0;
3411 break;
3412 case QEMU_OPTION_parallel:
3413 add_device_config(DEV_PARALLEL, optarg);
3414 default_parallel = 0;
3415 if (strncmp(optarg, "mon:", 4) == 0) {
3416 default_monitor = 0;
3418 break;
3419 case QEMU_OPTION_debugcon:
3420 add_device_config(DEV_DEBUGCON, optarg);
3421 break;
3422 case QEMU_OPTION_loadvm:
3423 loadvm = optarg;
3424 break;
3425 case QEMU_OPTION_full_screen:
3426 full_screen = 1;
3427 break;
3428 case QEMU_OPTION_no_frame:
3429 no_frame = 1;
3430 break;
3431 case QEMU_OPTION_alt_grab:
3432 alt_grab = 1;
3433 break;
3434 case QEMU_OPTION_ctrl_grab:
3435 ctrl_grab = 1;
3436 break;
3437 case QEMU_OPTION_no_quit:
3438 no_quit = 1;
3439 break;
3440 case QEMU_OPTION_sdl:
3441 #ifdef CONFIG_SDL
3442 display_type = DT_SDL;
3443 break;
3444 #else
3445 fprintf(stderr, "SDL support is disabled\n");
3446 exit(1);
3447 #endif
3448 case QEMU_OPTION_pidfile:
3449 pid_file = optarg;
3450 break;
3451 case QEMU_OPTION_win2k_hack:
3452 win2k_install_hack = 1;
3453 break;
3454 case QEMU_OPTION_rtc_td_hack: {
3455 static GlobalProperty slew_lost_ticks[] = {
3457 .driver = "mc146818rtc",
3458 .property = "lost_tick_policy",
3459 .value = "slew",
3461 { /* end of list */ }
3464 qdev_prop_register_global_list(slew_lost_ticks);
3465 break;
3467 case QEMU_OPTION_acpitable:
3468 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3469 if (!opts) {
3470 exit(1);
3472 do_acpitable_option(opts);
3473 break;
3474 case QEMU_OPTION_smbios:
3475 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3476 if (!opts) {
3477 exit(1);
3479 do_smbios_option(opts);
3480 break;
3481 case QEMU_OPTION_enable_kvm:
3482 olist = qemu_find_opts("machine");
3483 qemu_opts_parse(olist, "accel=kvm", 0);
3484 break;
3485 case QEMU_OPTION_M:
3486 case QEMU_OPTION_machine:
3487 olist = qemu_find_opts("machine");
3488 opts = qemu_opts_parse(olist, optarg, 1);
3489 if (!opts) {
3490 exit(1);
3492 break;
3493 case QEMU_OPTION_no_kvm:
3494 olist = qemu_find_opts("machine");
3495 qemu_opts_parse(olist, "accel=tcg", 0);
3496 break;
3497 case QEMU_OPTION_no_kvm_pit: {
3498 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3499 "separately.\n");
3500 break;
3502 case QEMU_OPTION_no_kvm_pit_reinjection: {
3503 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3505 .driver = "kvm-pit",
3506 .property = "lost_tick_policy",
3507 .value = "discard",
3509 { /* end of list */ }
3512 fprintf(stderr, "Warning: option deprecated, use "
3513 "lost_tick_policy property of kvm-pit instead.\n");
3514 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3515 break;
3517 case QEMU_OPTION_usb:
3518 olist = qemu_find_opts("machine");
3519 qemu_opts_parse(olist, "usb=on", 0);
3520 break;
3521 case QEMU_OPTION_usbdevice:
3522 olist = qemu_find_opts("machine");
3523 qemu_opts_parse(olist, "usb=on", 0);
3524 add_device_config(DEV_USB, optarg);
3525 break;
3526 case QEMU_OPTION_device:
3527 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3528 exit(1);
3530 break;
3531 case QEMU_OPTION_smp:
3532 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3533 exit(1);
3535 break;
3536 case QEMU_OPTION_vnc:
3537 #ifdef CONFIG_VNC
3538 if (vnc_parse_func(optarg) == NULL) {
3539 exit(1);
3541 #else
3542 fprintf(stderr, "VNC support is disabled\n");
3543 exit(1);
3544 #endif
3545 break;
3546 case QEMU_OPTION_no_acpi:
3547 acpi_enabled = 0;
3548 break;
3549 case QEMU_OPTION_no_hpet:
3550 no_hpet = 1;
3551 break;
3552 case QEMU_OPTION_balloon:
3553 if (balloon_parse(optarg) < 0) {
3554 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3555 exit(1);
3557 break;
3558 case QEMU_OPTION_no_reboot:
3559 no_reboot = 1;
3560 break;
3561 case QEMU_OPTION_no_shutdown:
3562 no_shutdown = 1;
3563 break;
3564 case QEMU_OPTION_show_cursor:
3565 cursor_hide = 0;
3566 break;
3567 case QEMU_OPTION_uuid:
3568 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3569 fprintf(stderr, "Fail to parse UUID string."
3570 " Wrong format.\n");
3571 exit(1);
3573 qemu_uuid_set = true;
3574 break;
3575 case QEMU_OPTION_option_rom:
3576 if (nb_option_roms >= MAX_OPTION_ROMS) {
3577 fprintf(stderr, "Too many option ROMs\n");
3578 exit(1);
3580 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3581 if (!opts) {
3582 exit(1);
3584 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3585 option_rom[nb_option_roms].bootindex =
3586 qemu_opt_get_number(opts, "bootindex", -1);
3587 if (!option_rom[nb_option_roms].name) {
3588 fprintf(stderr, "Option ROM file is not specified\n");
3589 exit(1);
3591 nb_option_roms++;
3592 break;
3593 case QEMU_OPTION_semihosting:
3594 semihosting_enabled = 1;
3595 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3596 break;
3597 case QEMU_OPTION_semihosting_config:
3598 semihosting_enabled = 1;
3599 opts = qemu_opts_parse(qemu_find_opts("semihosting-config"),
3600 optarg, 0);
3601 if (opts != NULL) {
3602 semihosting_enabled = qemu_opt_get_bool(opts, "enable",
3603 true);
3604 const char *target = qemu_opt_get(opts, "target");
3605 if (target != NULL) {
3606 if (strcmp("native", target) == 0) {
3607 semihosting_target = SEMIHOSTING_TARGET_NATIVE;
3608 } else if (strcmp("gdb", target) == 0) {
3609 semihosting_target = SEMIHOSTING_TARGET_GDB;
3610 } else if (strcmp("auto", target) == 0) {
3611 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3612 } else {
3613 fprintf(stderr, "Unsupported semihosting-config"
3614 " %s\n",
3615 optarg);
3616 exit(1);
3618 } else {
3619 semihosting_target = SEMIHOSTING_TARGET_AUTO;
3621 } else {
3622 fprintf(stderr, "Unsupported semihosting-config %s\n",
3623 optarg);
3624 exit(1);
3626 break;
3627 case QEMU_OPTION_tdf:
3628 fprintf(stderr, "Warning: user space PIT time drift fix "
3629 "is no longer supported.\n");
3630 break;
3631 case QEMU_OPTION_name:
3632 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3633 if (!opts) {
3634 exit(1);
3636 break;
3637 case QEMU_OPTION_prom_env:
3638 if (nb_prom_envs >= MAX_PROM_ENVS) {
3639 fprintf(stderr, "Too many prom variables\n");
3640 exit(1);
3642 prom_envs[nb_prom_envs] = optarg;
3643 nb_prom_envs++;
3644 break;
3645 case QEMU_OPTION_old_param:
3646 old_param = 1;
3647 break;
3648 case QEMU_OPTION_clock:
3649 /* Clock options no longer exist. Keep this option for
3650 * backward compatibility.
3652 break;
3653 case QEMU_OPTION_startdate:
3654 configure_rtc_date_offset(optarg, 1);
3655 break;
3656 case QEMU_OPTION_rtc:
3657 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3658 if (!opts) {
3659 exit(1);
3661 configure_rtc(opts);
3662 break;
3663 case QEMU_OPTION_tb_size:
3664 tcg_tb_size = strtol(optarg, NULL, 0);
3665 if (tcg_tb_size < 0) {
3666 tcg_tb_size = 0;
3668 break;
3669 case QEMU_OPTION_icount:
3670 icount_opts = qemu_opts_parse(qemu_find_opts("icount"),
3671 optarg, 1);
3672 if (!icount_opts) {
3673 exit(1);
3675 break;
3676 case QEMU_OPTION_incoming:
3677 if (!incoming) {
3678 runstate_set(RUN_STATE_INMIGRATE);
3680 incoming = optarg;
3681 break;
3682 case QEMU_OPTION_nodefaults:
3683 has_defaults = 0;
3684 break;
3685 case QEMU_OPTION_xen_domid:
3686 if (!(xen_available())) {
3687 printf("Option %s not supported for this target\n", popt->name);
3688 exit(1);
3690 xen_domid = atoi(optarg);
3691 break;
3692 case QEMU_OPTION_xen_create:
3693 if (!(xen_available())) {
3694 printf("Option %s not supported for this target\n", popt->name);
3695 exit(1);
3697 xen_mode = XEN_CREATE;
3698 break;
3699 case QEMU_OPTION_xen_attach:
3700 if (!(xen_available())) {
3701 printf("Option %s not supported for this target\n", popt->name);
3702 exit(1);
3704 xen_mode = XEN_ATTACH;
3705 break;
3706 case QEMU_OPTION_trace:
3708 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3709 if (!opts) {
3710 exit(1);
3712 trace_events = qemu_opt_get(opts, "events");
3713 trace_file = qemu_opt_get(opts, "file");
3714 break;
3716 case QEMU_OPTION_trace_unassigned:
3717 trace_unassigned = true;
3718 break;
3719 case QEMU_OPTION_readconfig:
3721 int ret = qemu_read_config_file(optarg);
3722 if (ret < 0) {
3723 fprintf(stderr, "read config %s: %s\n", optarg,
3724 strerror(-ret));
3725 exit(1);
3727 break;
3729 case QEMU_OPTION_spice:
3730 olist = qemu_find_opts("spice");
3731 if (!olist) {
3732 fprintf(stderr, "spice is not supported by this qemu build.\n");
3733 exit(1);
3735 opts = qemu_opts_parse(olist, optarg, 0);
3736 if (!opts) {
3737 exit(1);
3739 display_remote++;
3740 break;
3741 case QEMU_OPTION_writeconfig:
3743 FILE *fp;
3744 if (strcmp(optarg, "-") == 0) {
3745 fp = stdout;
3746 } else {
3747 fp = fopen(optarg, "w");
3748 if (fp == NULL) {
3749 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3750 exit(1);
3753 qemu_config_write(fp);
3754 if (fp != stdout) {
3755 fclose(fp);
3757 break;
3759 case QEMU_OPTION_qtest:
3760 qtest_chrdev = optarg;
3761 break;
3762 case QEMU_OPTION_qtest_log:
3763 qtest_log = optarg;
3764 break;
3765 case QEMU_OPTION_sandbox:
3766 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3767 if (!opts) {
3768 exit(1);
3770 break;
3771 case QEMU_OPTION_add_fd:
3772 #ifndef _WIN32
3773 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3774 if (!opts) {
3775 exit(1);
3777 #else
3778 error_report("File descriptor passing is disabled on this "
3779 "platform");
3780 exit(1);
3781 #endif
3782 break;
3783 case QEMU_OPTION_object:
3784 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3785 if (!opts) {
3786 exit(1);
3788 break;
3789 case QEMU_OPTION_realtime:
3790 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3791 if (!opts) {
3792 exit(1);
3794 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3795 break;
3796 case QEMU_OPTION_msg:
3797 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3798 if (!opts) {
3799 exit(1);
3801 configure_msg(opts);
3802 break;
3803 case QEMU_OPTION_dump_vmstate:
3804 if (vmstate_dump_file) {
3805 fprintf(stderr, "qemu: only one '-dump-vmstate' "
3806 "option may be given\n");
3807 exit(1);
3809 vmstate_dump_file = fopen(optarg, "w");
3810 if (vmstate_dump_file == NULL) {
3811 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3812 exit(1);
3814 break;
3815 default:
3816 os_parse_cmd_args(popt->index, optarg);
3821 opts = qemu_get_machine_opts();
3822 optarg = qemu_opt_get(opts, "type");
3823 if (optarg) {
3824 machine_class = machine_parse(optarg);
3827 set_memory_options(&ram_slots, &maxram_size);
3829 loc_set_none();
3831 os_daemonize();
3833 if (qemu_init_main_loop(&main_loop_err)) {
3834 error_report_err(main_loop_err);
3835 exit(1);
3838 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3839 exit(1);
3842 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3843 exit(1);
3846 #ifndef _WIN32
3847 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3848 exit(1);
3851 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3852 exit(1);
3854 #endif
3856 if (machine_class == NULL) {
3857 fprintf(stderr, "No machine specified, and there is no default.\n"
3858 "Use -machine help to list supported machines!\n");
3859 exit(1);
3862 current_machine = MACHINE(object_new(object_class_get_name(
3863 OBJECT_CLASS(machine_class))));
3864 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
3865 exit(0);
3867 object_property_add_child(object_get_root(), "machine",
3868 OBJECT(current_machine), &error_abort);
3869 cpu_exec_init_all();
3871 if (machine_class->hw_version) {
3872 qemu_set_version(machine_class->hw_version);
3875 /* Init CPU def lists, based on config
3876 * - Must be called after all the qemu_read_config_file() calls
3877 * - Must be called before list_cpus()
3878 * - Must be called before machine->init()
3880 cpudef_init();
3882 if (cpu_model && is_help_option(cpu_model)) {
3883 list_cpus(stdout, &fprintf, cpu_model);
3884 exit(0);
3887 /* Open the logfile at this point, if necessary. We can't open the logfile
3888 * when encountering either of the logging options (-d or -D) because the
3889 * other one may be encountered later on the command line, changing the
3890 * location or level of logging.
3892 if (log_mask) {
3893 int mask;
3894 if (log_file) {
3895 qemu_set_log_filename(log_file);
3898 mask = qemu_str_to_log_mask(log_mask);
3899 if (!mask) {
3900 qemu_print_log_usage(stdout);
3901 exit(1);
3903 qemu_set_log(mask);
3906 if (!is_daemonized()) {
3907 if (!trace_init_backends(trace_events, trace_file)) {
3908 exit(1);
3912 /* If no data_dir is specified then try to find it relative to the
3913 executable path. */
3914 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3915 data_dir[data_dir_idx] = os_find_datadir();
3916 if (data_dir[data_dir_idx] != NULL) {
3917 data_dir_idx++;
3920 /* If all else fails use the install path specified when building. */
3921 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3922 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3925 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3927 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
3928 if (max_cpus > machine_class->max_cpus) {
3929 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3930 "supported by machine `%s' (%d)\n", max_cpus,
3931 machine_class->name, machine_class->max_cpus);
3932 exit(1);
3936 * Get the default machine options from the machine if it is not already
3937 * specified either by the configuration file or by the command line.
3939 if (machine_class->default_machine_opts) {
3940 qemu_opts_set_defaults(qemu_find_opts("machine"),
3941 machine_class->default_machine_opts, 0);
3944 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3945 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3947 if (!vga_model && !default_vga) {
3948 vga_interface_type = VGA_DEVICE;
3950 if (!has_defaults || machine_class->no_serial) {
3951 default_serial = 0;
3953 if (!has_defaults || machine_class->no_parallel) {
3954 default_parallel = 0;
3956 if (!has_defaults || !machine_class->use_virtcon) {
3957 default_virtcon = 0;
3959 if (!has_defaults || !machine_class->use_sclp) {
3960 default_sclp = 0;
3962 if (!has_defaults || machine_class->no_floppy) {
3963 default_floppy = 0;
3965 if (!has_defaults || machine_class->no_cdrom) {
3966 default_cdrom = 0;
3968 if (!has_defaults || machine_class->no_sdcard) {
3969 default_sdcard = 0;
3971 if (!has_defaults) {
3972 default_monitor = 0;
3973 default_net = 0;
3974 default_vga = 0;
3977 if (is_daemonized()) {
3978 /* According to documentation and historically, -nographic redirects
3979 * serial port, parallel port and monitor to stdio, which does not work
3980 * with -daemonize. We can redirect these to null instead, but since
3981 * -nographic is legacy, let's just error out.
3982 * We disallow -nographic only if all other ports are not redirected
3983 * explicitly, to not break existing legacy setups which uses
3984 * -nographic _and_ redirects all ports explicitly - this is valid
3985 * usage, -nographic is just a no-op in this case.
3987 if (display_type == DT_NOGRAPHIC
3988 && (default_parallel || default_serial
3989 || default_monitor || default_virtcon)) {
3990 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3991 exit(1);
3993 #ifdef CONFIG_CURSES
3994 if (display_type == DT_CURSES) {
3995 fprintf(stderr, "curses display can not be used with -daemonize\n");
3996 exit(1);
3998 #endif
4001 if (display_type == DT_NOGRAPHIC) {
4002 if (default_parallel)
4003 add_device_config(DEV_PARALLEL, "null");
4004 if (default_serial && default_monitor) {
4005 add_device_config(DEV_SERIAL, "mon:stdio");
4006 } else if (default_virtcon && default_monitor) {
4007 add_device_config(DEV_VIRTCON, "mon:stdio");
4008 } else if (default_sclp && default_monitor) {
4009 add_device_config(DEV_SCLP, "mon:stdio");
4010 } else {
4011 if (default_serial)
4012 add_device_config(DEV_SERIAL, "stdio");
4013 if (default_virtcon)
4014 add_device_config(DEV_VIRTCON, "stdio");
4015 if (default_sclp) {
4016 add_device_config(DEV_SCLP, "stdio");
4018 if (default_monitor)
4019 monitor_parse("stdio", "readline", false);
4021 } else {
4022 if (default_serial)
4023 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4024 if (default_parallel)
4025 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4026 if (default_monitor)
4027 monitor_parse("vc:80Cx24C", "readline", false);
4028 if (default_virtcon)
4029 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4030 if (default_sclp) {
4031 add_device_config(DEV_SCLP, "vc:80Cx24C");
4035 #if defined(CONFIG_VNC)
4036 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4037 display_remote++;
4039 #endif
4040 if (display_type == DT_DEFAULT && !display_remote) {
4041 #if defined(CONFIG_GTK)
4042 display_type = DT_GTK;
4043 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4044 display_type = DT_SDL;
4045 #elif defined(CONFIG_VNC)
4046 vnc_parse_func("localhost:0,to=99,id=default");
4047 show_vnc_port = 1;
4048 #else
4049 display_type = DT_NONE;
4050 #endif
4053 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4054 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4055 "for SDL, ignoring option\n");
4057 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4058 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4059 "ignoring option\n");
4062 #if defined(CONFIG_GTK)
4063 if (display_type == DT_GTK) {
4064 early_gtk_display_init();
4066 #endif
4067 #if defined(CONFIG_SDL)
4068 if (display_type == DT_SDL) {
4069 sdl_display_early_init(request_opengl);
4071 #endif
4072 if (request_opengl == 1 && display_opengl == 0) {
4073 #if defined(CONFIG_OPENGL)
4074 fprintf(stderr, "OpenGL is not supported by the display.\n");
4075 #else
4076 fprintf(stderr, "QEMU was built without opengl support.\n");
4077 #endif
4078 exit(1);
4081 socket_init();
4083 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4084 exit(1);
4085 #ifdef CONFIG_VIRTFS
4086 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4087 exit(1);
4089 #endif
4091 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4092 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
4093 exit(1);
4096 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4097 != 0) {
4098 exit(0);
4101 if (qemu_opts_foreach(qemu_find_opts("object"),
4102 object_create, NULL, 0) != 0) {
4103 exit(1);
4106 machine_opts = qemu_get_machine_opts();
4107 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4108 1) < 0) {
4109 object_unref(OBJECT(current_machine));
4110 exit(1);
4113 configure_accelerator(current_machine);
4115 if (qtest_chrdev) {
4116 Error *local_err = NULL;
4117 qtest_init(qtest_chrdev, qtest_log, &local_err);
4118 if (local_err) {
4119 error_report_err(local_err);
4120 exit(1);
4124 machine_opts = qemu_get_machine_opts();
4125 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4126 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4127 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4128 bios_name = qemu_opt_get(machine_opts, "firmware");
4130 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4131 if (opts) {
4132 Error *local_err = NULL;
4134 boot_order = qemu_opt_get(opts, "order");
4135 if (boot_order) {
4136 validate_bootdevices(boot_order, &local_err);
4137 if (local_err) {
4138 error_report_err(local_err);
4139 exit(1);
4143 boot_once = qemu_opt_get(opts, "once");
4144 if (boot_once) {
4145 validate_bootdevices(boot_once, &local_err);
4146 if (local_err) {
4147 error_report_err(local_err);
4148 exit(1);
4152 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4153 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4156 if (!boot_order) {
4157 boot_order = machine_class->default_boot_order;
4160 if (!kernel_cmdline) {
4161 kernel_cmdline = "";
4162 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4165 linux_boot = (kernel_filename != NULL);
4167 if (!linux_boot && *kernel_cmdline != '\0') {
4168 fprintf(stderr, "-append only allowed with -kernel option\n");
4169 exit(1);
4172 if (!linux_boot && initrd_filename != NULL) {
4173 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4174 exit(1);
4177 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4178 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4179 exit(1);
4182 os_set_line_buffering();
4184 #ifdef CONFIG_SPICE
4185 /* spice needs the timers to be initialized by this point */
4186 qemu_spice_init();
4187 #endif
4189 cpu_ticks_init();
4190 if (icount_opts) {
4191 if (kvm_enabled() || xen_enabled()) {
4192 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4193 exit(1);
4195 configure_icount(icount_opts, &error_abort);
4196 qemu_opts_del(icount_opts);
4199 /* clean up network at qemu process termination */
4200 atexit(&net_cleanup);
4202 if (net_init_clients() < 0) {
4203 exit(1);
4206 #ifdef CONFIG_TPM
4207 if (tpm_init() < 0) {
4208 exit(1);
4210 #endif
4212 /* init the bluetooth world */
4213 if (foreach_device_config(DEV_BT, bt_parse))
4214 exit(1);
4216 if (!xen_enabled()) {
4217 /* On 32-bit hosts, QEMU is limited by virtual address space */
4218 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4219 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4220 exit(1);
4224 blk_mig_init();
4225 ram_mig_init();
4227 /* If the currently selected machine wishes to override the units-per-bus
4228 * property of its default HBA interface type, do so now. */
4229 if (machine_class->units_per_default_bus) {
4230 override_max_devs(machine_class->block_default_type,
4231 machine_class->units_per_default_bus);
4234 /* open the virtual block devices */
4235 if (snapshot)
4236 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4237 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4238 &machine_class->block_default_type, 1) != 0) {
4239 exit(1);
4242 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4243 CDROM_OPTS);
4244 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4245 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4247 parse_numa_opts(machine_class);
4249 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4250 exit(1);
4253 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4254 exit(1);
4255 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4256 exit(1);
4257 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4258 exit(1);
4259 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4260 exit(1);
4262 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4263 exit(1);
4265 /* If no default VGA is requested, the default is "none". */
4266 if (default_vga) {
4267 if (machine_class->default_display) {
4268 vga_model = machine_class->default_display;
4269 } else if (cirrus_vga_available()) {
4270 vga_model = "cirrus";
4271 } else if (vga_available()) {
4272 vga_model = "std";
4275 if (vga_model) {
4276 select_vgahw(vga_model);
4279 if (watchdog) {
4280 i = select_watchdog(watchdog);
4281 if (i > 0)
4282 exit (i == 1 ? 1 : 0);
4285 if (machine_class->compat_props) {
4286 qdev_prop_register_global_list(machine_class->compat_props);
4288 qemu_add_globals();
4290 qdev_machine_init();
4292 current_machine->ram_size = ram_size;
4293 current_machine->maxram_size = maxram_size;
4294 current_machine->ram_slots = ram_slots;
4295 current_machine->boot_order = boot_order;
4296 current_machine->cpu_model = cpu_model;
4298 machine_class->init(current_machine);
4300 realtime_init();
4302 audio_init();
4304 cpu_synchronize_all_post_init();
4306 numa_post_machine_init();
4308 /* init USB devices */
4309 if (usb_enabled()) {
4310 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4311 exit(1);
4314 /* init generic devices */
4315 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4316 exit(1);
4318 /* Did we create any drives that we failed to create a device for? */
4319 drive_check_orphaned();
4321 net_check_clients();
4323 if (boot_once) {
4324 Error *local_err = NULL;
4325 qemu_boot_set(boot_once, &local_err);
4326 if (local_err) {
4327 error_report("%s", error_get_pretty(local_err));
4328 exit(1);
4330 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4333 ds = init_displaystate();
4335 /* init local displays */
4336 switch (display_type) {
4337 case DT_NOGRAPHIC:
4338 (void)ds; /* avoid warning if no display is configured */
4339 break;
4340 #if defined(CONFIG_CURSES)
4341 case DT_CURSES:
4342 curses_display_init(ds, full_screen);
4343 break;
4344 #endif
4345 #if defined(CONFIG_SDL)
4346 case DT_SDL:
4347 sdl_display_init(ds, full_screen, no_frame);
4348 break;
4349 #elif defined(CONFIG_COCOA)
4350 case DT_SDL:
4351 cocoa_display_init(ds, full_screen);
4352 break;
4353 #endif
4354 #if defined(CONFIG_GTK)
4355 case DT_GTK:
4356 gtk_display_init(ds, full_screen, grab_on_hover);
4357 break;
4358 #endif
4359 default:
4360 break;
4363 /* must be after terminal init, SDL library changes signal handlers */
4364 os_setup_signal_handling();
4366 #ifdef CONFIG_VNC
4367 /* init remote displays */
4368 qemu_opts_foreach(qemu_find_opts("vnc"), vnc_init_func, NULL, 0);
4369 if (show_vnc_port) {
4370 printf("VNC server running on `%s'\n",
4371 vnc_display_local_addr("default"));
4373 #endif
4374 #ifdef CONFIG_SPICE
4375 if (using_spice) {
4376 qemu_spice_display_init();
4378 #endif
4380 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4381 exit(1);
4384 qdev_machine_creation_done();
4386 if (rom_load_all() != 0) {
4387 fprintf(stderr, "rom loading failed\n");
4388 exit(1);
4391 /* TODO: once all bus devices are qdevified, this should be done
4392 * when bus is created by qdev.c */
4393 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4394 qemu_run_machine_init_done_notifiers();
4396 /* Done notifiers can load ROMs */
4397 rom_load_done();
4399 qemu_system_reset(VMRESET_SILENT);
4400 if (loadvm) {
4401 if (load_vmstate(loadvm) < 0) {
4402 autostart = 0;
4406 qdev_prop_check_globals();
4407 if (vmstate_dump_file) {
4408 /* dump and exit */
4409 dump_vmstate_json_to_file(vmstate_dump_file);
4410 return 0;
4413 if (incoming) {
4414 Error *local_err = NULL;
4415 qemu_start_incoming_migration(incoming, &local_err);
4416 if (local_err) {
4417 error_report("-incoming %s: %s", incoming,
4418 error_get_pretty(local_err));
4419 error_free(local_err);
4420 exit(1);
4422 } else if (autostart) {
4423 vm_start();
4426 os_setup_post();
4428 if (is_daemonized()) {
4429 if (!trace_init_backends(trace_events, trace_file)) {
4430 exit(1);
4434 main_loop();
4435 bdrv_close_all();
4436 pause_all_vcpus();
4437 res_free();
4438 #ifdef CONFIG_TPM
4439 tpm_cleanup();
4440 #endif
4442 return 0;