tcg-ppc: Merge cache-utils into the backend
[qemu.git] / vl.c
blob4315dd2fa149dc97ac56be3dba298351518ed1c0
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv, char **envp);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv, NULL);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "hw/usb.h"
65 #include "hw/pcmcia.h"
66 #include "hw/i386/pc.h"
67 #include "hw/isa/isa.h"
68 #include "hw/bt.h"
69 #include "sysemu/watchdog.h"
70 #include "hw/i386/smbios.h"
71 #include "hw/xen/xen.h"
72 #include "hw/qdev.h"
73 #include "hw/loader.h"
74 #include "monitor/qdev.h"
75 #include "sysemu/bt.h"
76 #include "net/net.h"
77 #include "net/slirp.h"
78 #include "monitor/monitor.h"
79 #include "ui/console.h"
80 #include "sysemu/sysemu.h"
81 #include "exec/gdbstub.h"
82 #include "qemu/timer.h"
83 #include "sysemu/char.h"
84 #include "qemu/bitmap.h"
85 #include "sysemu/blockdev.h"
86 #include "hw/block/block.h"
87 #include "migration/block.h"
88 #include "sysemu/tpm.h"
89 #include "sysemu/dma.h"
90 #include "audio/audio.h"
91 #include "migration/migration.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
114 #include "qemu/osdep.h"
116 #include "ui/qemu-spice.h"
117 #include "qapi/string-input-visitor.h"
118 #include "qapi/opts-visitor.h"
120 #define DEFAULT_RAM_SIZE 128
122 #define MAX_VIRTIO_CONSOLES 1
123 #define MAX_SCLP_CONSOLES 1
125 static const char *data_dir[16];
126 static int data_dir_idx;
127 const char *bios_name = NULL;
128 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
129 DisplayType display_type = DT_DEFAULT;
130 static int display_remote;
131 const char* keyboard_layout = NULL;
132 ram_addr_t ram_size;
133 const char *mem_path = NULL;
134 int mem_prealloc = 0; /* force preallocation of physical target memory */
135 int nb_nics;
136 NICInfo nd_table[MAX_NICS];
137 int autostart;
138 static int rtc_utc = 1;
139 static int rtc_date_offset = -1; /* -1 means no change */
140 QEMUClockType rtc_clock;
141 int vga_interface_type = VGA_NONE;
142 static int full_screen = 0;
143 static int no_frame = 0;
144 int no_quit = 0;
145 #ifdef CONFIG_GTK
146 static bool grab_on_hover;
147 #endif
148 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
149 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
150 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
151 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
152 int win2k_install_hack = 0;
153 int singlestep = 0;
154 int smp_cpus = 1;
155 int max_cpus = 0;
156 int smp_cores = 1;
157 int smp_threads = 1;
158 #ifdef CONFIG_VNC
159 const char *vnc_display;
160 #endif
161 int acpi_enabled = 1;
162 int no_hpet = 0;
163 int fd_bootchk = 1;
164 static int no_reboot;
165 int no_shutdown = 0;
166 int cursor_hide = 1;
167 int graphic_rotate = 0;
168 const char *watchdog;
169 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
170 int nb_option_roms;
171 int semihosting_enabled = 0;
172 int old_param = 0;
173 const char *qemu_name;
174 int alt_grab = 0;
175 int ctrl_grab = 0;
176 unsigned int nb_prom_envs = 0;
177 const char *prom_envs[MAX_PROM_ENVS];
178 int boot_menu;
179 static bool boot_strict;
180 uint8_t *boot_splash_filedata;
181 size_t boot_splash_filedata_size;
182 uint8_t qemu_extra_params_fw[2];
184 typedef struct FWBootEntry FWBootEntry;
186 struct FWBootEntry {
187 QTAILQ_ENTRY(FWBootEntry) link;
188 int32_t bootindex;
189 DeviceState *dev;
190 char *suffix;
193 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
194 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
196 int nb_numa_nodes;
197 NodeInfo numa_info[MAX_NODES];
199 uint8_t qemu_uuid[16];
200 bool qemu_uuid_set;
202 static QEMUBootSetHandler *boot_set_handler;
203 static void *boot_set_opaque;
205 static NotifierList exit_notifiers =
206 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
208 static NotifierList machine_init_done_notifiers =
209 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
211 static bool tcg_allowed = true;
212 bool xen_allowed;
213 uint32_t xen_domid;
214 enum xen_mode xen_mode = XEN_EMULATE;
215 static int tcg_tb_size;
217 static int has_defaults = 1;
218 static int default_serial = 1;
219 static int default_parallel = 1;
220 static int default_virtcon = 1;
221 static int default_sclp = 1;
222 static int default_monitor = 1;
223 static int default_floppy = 1;
224 static int default_cdrom = 1;
225 static int default_sdcard = 1;
226 static int default_vga = 1;
228 static struct {
229 const char *driver;
230 int *flag;
231 } default_list[] = {
232 { .driver = "isa-serial", .flag = &default_serial },
233 { .driver = "isa-parallel", .flag = &default_parallel },
234 { .driver = "isa-fdc", .flag = &default_floppy },
235 { .driver = "ide-cd", .flag = &default_cdrom },
236 { .driver = "ide-hd", .flag = &default_cdrom },
237 { .driver = "ide-drive", .flag = &default_cdrom },
238 { .driver = "scsi-cd", .flag = &default_cdrom },
239 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
240 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
241 { .driver = "virtio-serial", .flag = &default_virtcon },
242 { .driver = "VGA", .flag = &default_vga },
243 { .driver = "isa-vga", .flag = &default_vga },
244 { .driver = "cirrus-vga", .flag = &default_vga },
245 { .driver = "isa-cirrus-vga", .flag = &default_vga },
246 { .driver = "vmware-svga", .flag = &default_vga },
247 { .driver = "qxl-vga", .flag = &default_vga },
250 static QemuOptsList qemu_rtc_opts = {
251 .name = "rtc",
252 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
253 .desc = {
255 .name = "base",
256 .type = QEMU_OPT_STRING,
258 .name = "clock",
259 .type = QEMU_OPT_STRING,
261 .name = "driftfix",
262 .type = QEMU_OPT_STRING,
264 { /* end of list */ }
268 static QemuOptsList qemu_sandbox_opts = {
269 .name = "sandbox",
270 .implied_opt_name = "enable",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
272 .desc = {
274 .name = "enable",
275 .type = QEMU_OPT_BOOL,
277 { /* end of list */ }
281 static QemuOptsList qemu_trace_opts = {
282 .name = "trace",
283 .implied_opt_name = "trace",
284 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
285 .desc = {
287 .name = "events",
288 .type = QEMU_OPT_STRING,
290 .name = "file",
291 .type = QEMU_OPT_STRING,
293 { /* end of list */ }
297 static QemuOptsList qemu_option_rom_opts = {
298 .name = "option-rom",
299 .implied_opt_name = "romfile",
300 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
301 .desc = {
303 .name = "bootindex",
304 .type = QEMU_OPT_NUMBER,
305 }, {
306 .name = "romfile",
307 .type = QEMU_OPT_STRING,
309 { /* end of list */ }
313 static QemuOptsList qemu_machine_opts = {
314 .name = "machine",
315 .implied_opt_name = "type",
316 .merge_lists = true,
317 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
318 .desc = {
320 .name = "type",
321 .type = QEMU_OPT_STRING,
322 .help = "emulated machine"
323 }, {
324 .name = "accel",
325 .type = QEMU_OPT_STRING,
326 .help = "accelerator list",
327 }, {
328 .name = "kernel_irqchip",
329 .type = QEMU_OPT_BOOL,
330 .help = "use KVM in-kernel irqchip",
331 }, {
332 .name = "kvm_shadow_mem",
333 .type = QEMU_OPT_SIZE,
334 .help = "KVM shadow MMU size",
335 }, {
336 .name = "kernel",
337 .type = QEMU_OPT_STRING,
338 .help = "Linux kernel image file",
339 }, {
340 .name = "initrd",
341 .type = QEMU_OPT_STRING,
342 .help = "Linux initial ramdisk file",
343 }, {
344 .name = "append",
345 .type = QEMU_OPT_STRING,
346 .help = "Linux kernel command line",
347 }, {
348 .name = "dtb",
349 .type = QEMU_OPT_STRING,
350 .help = "Linux kernel device tree file",
351 }, {
352 .name = "dumpdtb",
353 .type = QEMU_OPT_STRING,
354 .help = "Dump current dtb to a file and quit",
355 }, {
356 .name = "phandle_start",
357 .type = QEMU_OPT_NUMBER,
358 .help = "The first phandle ID we may generate dynamically",
359 }, {
360 .name = "dt_compatible",
361 .type = QEMU_OPT_STRING,
362 .help = "Overrides the \"compatible\" property of the dt root node",
363 }, {
364 .name = "dump-guest-core",
365 .type = QEMU_OPT_BOOL,
366 .help = "Include guest memory in a core dump",
367 }, {
368 .name = "mem-merge",
369 .type = QEMU_OPT_BOOL,
370 .help = "enable/disable memory merge support",
372 .name = "usb",
373 .type = QEMU_OPT_BOOL,
374 .help = "Set on/off to enable/disable usb",
376 .name = "firmware",
377 .type = QEMU_OPT_STRING,
378 .help = "firmware image",
380 .name = "kvm-type",
381 .type = QEMU_OPT_STRING,
382 .help = "Specifies the KVM virtualization mode (HV, PR)",
384 { /* End of list */ }
388 static QemuOptsList qemu_boot_opts = {
389 .name = "boot-opts",
390 .implied_opt_name = "order",
391 .merge_lists = true,
392 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
393 .desc = {
395 .name = "order",
396 .type = QEMU_OPT_STRING,
397 }, {
398 .name = "once",
399 .type = QEMU_OPT_STRING,
400 }, {
401 .name = "menu",
402 .type = QEMU_OPT_BOOL,
403 }, {
404 .name = "splash",
405 .type = QEMU_OPT_STRING,
406 }, {
407 .name = "splash-time",
408 .type = QEMU_OPT_STRING,
409 }, {
410 .name = "reboot-timeout",
411 .type = QEMU_OPT_STRING,
412 }, {
413 .name = "strict",
414 .type = QEMU_OPT_BOOL,
416 { /*End of list */ }
420 static QemuOptsList qemu_add_fd_opts = {
421 .name = "add-fd",
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
423 .desc = {
425 .name = "fd",
426 .type = QEMU_OPT_NUMBER,
427 .help = "file descriptor of which a duplicate is added to fd set",
429 .name = "set",
430 .type = QEMU_OPT_NUMBER,
431 .help = "ID of the fd set to add fd to",
433 .name = "opaque",
434 .type = QEMU_OPT_STRING,
435 .help = "free-form string used to describe fd",
437 { /* end of list */ }
441 static QemuOptsList qemu_object_opts = {
442 .name = "object",
443 .implied_opt_name = "qom-type",
444 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
445 .desc = {
450 static QemuOptsList qemu_tpmdev_opts = {
451 .name = "tpmdev",
452 .implied_opt_name = "type",
453 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
454 .desc = {
455 /* options are defined in the TPM backends */
456 { /* end of list */ }
460 static QemuOptsList qemu_realtime_opts = {
461 .name = "realtime",
462 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
463 .desc = {
465 .name = "mlock",
466 .type = QEMU_OPT_BOOL,
468 { /* end of list */ }
472 static QemuOptsList qemu_msg_opts = {
473 .name = "msg",
474 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
475 .desc = {
477 .name = "timestamp",
478 .type = QEMU_OPT_BOOL,
480 { /* end of list */ }
484 static QemuOptsList qemu_name_opts = {
485 .name = "name",
486 .implied_opt_name = "guest",
487 .merge_lists = true,
488 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
489 .desc = {
491 .name = "guest",
492 .type = QEMU_OPT_STRING,
493 .help = "Sets the name of the guest.\n"
494 "This name will be displayed in the SDL window caption.\n"
495 "The name will also be used for the VNC server",
496 }, {
497 .name = "process",
498 .type = QEMU_OPT_STRING,
499 .help = "Sets the name of the QEMU process, as shown in top etc",
500 }, {
501 .name = "debug-threads",
502 .type = QEMU_OPT_BOOL,
503 .help = "When enabled, name the individual threads; defaults off.\n"
504 "NOTE: The thread names are for debugging and not a\n"
505 "stable API.",
507 { /* End of list */ }
511 static QemuOptsList qemu_mem_opts = {
512 .name = "memory",
513 .implied_opt_name = "size",
514 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
515 .merge_lists = true,
516 .desc = {
518 .name = "size",
519 .type = QEMU_OPT_SIZE,
522 .name = "slots",
523 .type = QEMU_OPT_NUMBER,
526 .name = "maxmem",
527 .type = QEMU_OPT_SIZE,
529 { /* end of list */ }
534 * Get machine options
536 * Returns: machine options (never null).
538 QemuOpts *qemu_get_machine_opts(void)
540 return qemu_find_opts_singleton("machine");
543 const char *qemu_get_vm_name(void)
545 return qemu_name;
548 static void res_free(void)
550 if (boot_splash_filedata != NULL) {
551 g_free(boot_splash_filedata);
552 boot_splash_filedata = NULL;
556 static int default_driver_check(QemuOpts *opts, void *opaque)
558 const char *driver = qemu_opt_get(opts, "driver");
559 int i;
561 if (!driver)
562 return 0;
563 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
564 if (strcmp(default_list[i].driver, driver) != 0)
565 continue;
566 *(default_list[i].flag) = 0;
568 return 0;
571 /***********************************************************/
572 /* QEMU state */
574 static RunState current_run_state = RUN_STATE_PRELAUNCH;
576 /* We use RUN_STATE_MAX but any invalid value will do */
577 static RunState vmstop_requested = RUN_STATE_MAX;
578 static QemuMutex vmstop_lock;
580 typedef struct {
581 RunState from;
582 RunState to;
583 } RunStateTransition;
585 static const RunStateTransition runstate_transitions_def[] = {
586 /* from -> to */
587 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
588 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
591 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
593 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
594 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
596 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
597 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
599 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
600 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
602 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
603 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
606 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
607 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
609 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
610 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
612 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
614 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
615 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
616 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
617 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
618 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
619 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
620 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
621 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
622 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
623 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
625 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
627 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
628 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
630 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
631 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
632 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
633 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
635 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
636 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
638 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
639 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
641 { RUN_STATE_MAX, RUN_STATE_MAX },
644 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
646 bool runstate_check(RunState state)
648 return current_run_state == state;
651 static void runstate_init(void)
653 const RunStateTransition *p;
655 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
656 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
657 runstate_valid_transitions[p->from][p->to] = true;
660 qemu_mutex_init(&vmstop_lock);
663 /* This function will abort() on invalid state transitions */
664 void runstate_set(RunState new_state)
666 assert(new_state < RUN_STATE_MAX);
668 if (!runstate_valid_transitions[current_run_state][new_state]) {
669 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
670 RunState_lookup[current_run_state],
671 RunState_lookup[new_state]);
672 abort();
674 trace_runstate_set(new_state);
675 current_run_state = new_state;
678 int runstate_is_running(void)
680 return runstate_check(RUN_STATE_RUNNING);
683 bool runstate_needs_reset(void)
685 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
686 runstate_check(RUN_STATE_SHUTDOWN);
689 StatusInfo *qmp_query_status(Error **errp)
691 StatusInfo *info = g_malloc0(sizeof(*info));
693 info->running = runstate_is_running();
694 info->singlestep = singlestep;
695 info->status = current_run_state;
697 return info;
700 static bool qemu_vmstop_requested(RunState *r)
702 qemu_mutex_lock(&vmstop_lock);
703 *r = vmstop_requested;
704 vmstop_requested = RUN_STATE_MAX;
705 qemu_mutex_unlock(&vmstop_lock);
706 return *r < RUN_STATE_MAX;
709 void qemu_system_vmstop_request_prepare(void)
711 qemu_mutex_lock(&vmstop_lock);
714 void qemu_system_vmstop_request(RunState state)
716 vmstop_requested = state;
717 qemu_mutex_unlock(&vmstop_lock);
718 qemu_notify_event();
721 void vm_start(void)
723 RunState requested;
725 qemu_vmstop_requested(&requested);
726 if (runstate_is_running() && requested == RUN_STATE_MAX) {
727 return;
730 /* Ensure that a STOP/RESUME pair of events is emitted if a
731 * vmstop request was pending. The BLOCK_IO_ERROR event, for
732 * example, according to documentation is always followed by
733 * the STOP event.
735 if (runstate_is_running()) {
736 monitor_protocol_event(QEVENT_STOP, NULL);
737 } else {
738 cpu_enable_ticks();
739 runstate_set(RUN_STATE_RUNNING);
740 vm_state_notify(1, RUN_STATE_RUNNING);
741 resume_all_vcpus();
744 monitor_protocol_event(QEVENT_RESUME, NULL);
748 /***********************************************************/
749 /* real time host monotonic timer */
751 /***********************************************************/
752 /* host time/date access */
753 void qemu_get_timedate(struct tm *tm, int offset)
755 time_t ti;
757 time(&ti);
758 ti += offset;
759 if (rtc_date_offset == -1) {
760 if (rtc_utc)
761 gmtime_r(&ti, tm);
762 else
763 localtime_r(&ti, tm);
764 } else {
765 ti -= rtc_date_offset;
766 gmtime_r(&ti, tm);
770 int qemu_timedate_diff(struct tm *tm)
772 time_t seconds;
774 if (rtc_date_offset == -1)
775 if (rtc_utc)
776 seconds = mktimegm(tm);
777 else {
778 struct tm tmp = *tm;
779 tmp.tm_isdst = -1; /* use timezone to figure it out */
780 seconds = mktime(&tmp);
782 else
783 seconds = mktimegm(tm) + rtc_date_offset;
785 return seconds - time(NULL);
788 void rtc_change_mon_event(struct tm *tm)
790 QObject *data;
792 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
793 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
794 qobject_decref(data);
797 static void configure_rtc_date_offset(const char *startdate, int legacy)
799 time_t rtc_start_date;
800 struct tm tm;
802 if (!strcmp(startdate, "now") && legacy) {
803 rtc_date_offset = -1;
804 } else {
805 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
806 &tm.tm_year,
807 &tm.tm_mon,
808 &tm.tm_mday,
809 &tm.tm_hour,
810 &tm.tm_min,
811 &tm.tm_sec) == 6) {
812 /* OK */
813 } else if (sscanf(startdate, "%d-%d-%d",
814 &tm.tm_year,
815 &tm.tm_mon,
816 &tm.tm_mday) == 3) {
817 tm.tm_hour = 0;
818 tm.tm_min = 0;
819 tm.tm_sec = 0;
820 } else {
821 goto date_fail;
823 tm.tm_year -= 1900;
824 tm.tm_mon--;
825 rtc_start_date = mktimegm(&tm);
826 if (rtc_start_date == -1) {
827 date_fail:
828 fprintf(stderr, "Invalid date format. Valid formats are:\n"
829 "'2006-06-17T16:01:21' or '2006-06-17'\n");
830 exit(1);
832 rtc_date_offset = time(NULL) - rtc_start_date;
836 static void configure_rtc(QemuOpts *opts)
838 const char *value;
840 value = qemu_opt_get(opts, "base");
841 if (value) {
842 if (!strcmp(value, "utc")) {
843 rtc_utc = 1;
844 } else if (!strcmp(value, "localtime")) {
845 rtc_utc = 0;
846 } else {
847 configure_rtc_date_offset(value, 0);
850 value = qemu_opt_get(opts, "clock");
851 if (value) {
852 if (!strcmp(value, "host")) {
853 rtc_clock = QEMU_CLOCK_HOST;
854 } else if (!strcmp(value, "rt")) {
855 rtc_clock = QEMU_CLOCK_REALTIME;
856 } else if (!strcmp(value, "vm")) {
857 rtc_clock = QEMU_CLOCK_VIRTUAL;
858 } else {
859 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
860 exit(1);
863 value = qemu_opt_get(opts, "driftfix");
864 if (value) {
865 if (!strcmp(value, "slew")) {
866 static GlobalProperty slew_lost_ticks[] = {
868 .driver = "mc146818rtc",
869 .property = "lost_tick_policy",
870 .value = "slew",
872 { /* end of list */ }
875 qdev_prop_register_global_list(slew_lost_ticks);
876 } else if (!strcmp(value, "none")) {
877 /* discard is default */
878 } else {
879 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
880 exit(1);
885 /***********************************************************/
886 /* Bluetooth support */
887 static int nb_hcis;
888 static int cur_hci;
889 static struct HCIInfo *hci_table[MAX_NICS];
891 struct HCIInfo *qemu_next_hci(void)
893 if (cur_hci == nb_hcis)
894 return &null_hci;
896 return hci_table[cur_hci++];
899 static int bt_hci_parse(const char *str)
901 struct HCIInfo *hci;
902 bdaddr_t bdaddr;
904 if (nb_hcis >= MAX_NICS) {
905 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
906 return -1;
909 hci = hci_init(str);
910 if (!hci)
911 return -1;
913 bdaddr.b[0] = 0x52;
914 bdaddr.b[1] = 0x54;
915 bdaddr.b[2] = 0x00;
916 bdaddr.b[3] = 0x12;
917 bdaddr.b[4] = 0x34;
918 bdaddr.b[5] = 0x56 + nb_hcis;
919 hci->bdaddr_set(hci, bdaddr.b);
921 hci_table[nb_hcis++] = hci;
923 return 0;
926 static void bt_vhci_add(int vlan_id)
928 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
930 if (!vlan->slave)
931 fprintf(stderr, "qemu: warning: adding a VHCI to "
932 "an empty scatternet %i\n", vlan_id);
934 bt_vhci_init(bt_new_hci(vlan));
937 static struct bt_device_s *bt_device_add(const char *opt)
939 struct bt_scatternet_s *vlan;
940 int vlan_id = 0;
941 char *endp = strstr(opt, ",vlan=");
942 int len = (endp ? endp - opt : strlen(opt)) + 1;
943 char devname[10];
945 pstrcpy(devname, MIN(sizeof(devname), len), opt);
947 if (endp) {
948 vlan_id = strtol(endp + 6, &endp, 0);
949 if (*endp) {
950 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
951 return 0;
955 vlan = qemu_find_bt_vlan(vlan_id);
957 if (!vlan->slave)
958 fprintf(stderr, "qemu: warning: adding a slave device to "
959 "an empty scatternet %i\n", vlan_id);
961 if (!strcmp(devname, "keyboard"))
962 return bt_keyboard_init(vlan);
964 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
965 return 0;
968 static int bt_parse(const char *opt)
970 const char *endp, *p;
971 int vlan;
973 if (strstart(opt, "hci", &endp)) {
974 if (!*endp || *endp == ',') {
975 if (*endp)
976 if (!strstart(endp, ",vlan=", 0))
977 opt = endp + 1;
979 return bt_hci_parse(opt);
981 } else if (strstart(opt, "vhci", &endp)) {
982 if (!*endp || *endp == ',') {
983 if (*endp) {
984 if (strstart(endp, ",vlan=", &p)) {
985 vlan = strtol(p, (char **) &endp, 0);
986 if (*endp) {
987 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
988 return 1;
990 } else {
991 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
992 return 1;
994 } else
995 vlan = 0;
997 bt_vhci_add(vlan);
998 return 0;
1000 } else if (strstart(opt, "device:", &endp))
1001 return !bt_device_add(endp);
1003 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1004 return 1;
1007 static int parse_sandbox(QemuOpts *opts, void *opaque)
1009 /* FIXME: change this to true for 1.3 */
1010 if (qemu_opt_get_bool(opts, "enable", false)) {
1011 #ifdef CONFIG_SECCOMP
1012 if (seccomp_start() < 0) {
1013 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1014 "failed to install seccomp syscall filter in the kernel");
1015 return -1;
1017 #else
1018 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1019 "sandboxing request but seccomp is not compiled into this build");
1020 return -1;
1021 #endif
1024 return 0;
1027 static int parse_name(QemuOpts *opts, void *opaque)
1029 const char *proc_name;
1031 if (qemu_opt_get(opts, "debug-threads")) {
1032 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1034 qemu_name = qemu_opt_get(opts, "guest");
1036 proc_name = qemu_opt_get(opts, "process");
1037 if (proc_name) {
1038 os_set_proc_name(proc_name);
1041 return 0;
1044 bool usb_enabled(bool default_usb)
1046 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1047 has_defaults && default_usb);
1050 #ifndef _WIN32
1051 static int parse_add_fd(QemuOpts *opts, void *opaque)
1053 int fd, dupfd, flags;
1054 int64_t fdset_id;
1055 const char *fd_opaque = NULL;
1057 fd = qemu_opt_get_number(opts, "fd", -1);
1058 fdset_id = qemu_opt_get_number(opts, "set", -1);
1059 fd_opaque = qemu_opt_get(opts, "opaque");
1061 if (fd < 0) {
1062 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1063 "fd option is required and must be non-negative");
1064 return -1;
1067 if (fd <= STDERR_FILENO) {
1068 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1069 "fd cannot be a standard I/O stream");
1070 return -1;
1074 * All fds inherited across exec() necessarily have FD_CLOEXEC
1075 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1077 flags = fcntl(fd, F_GETFD);
1078 if (flags == -1 || (flags & FD_CLOEXEC)) {
1079 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1080 "fd is not valid or already in use");
1081 return -1;
1084 if (fdset_id < 0) {
1085 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1086 "set option is required and must be non-negative");
1087 return -1;
1090 #ifdef F_DUPFD_CLOEXEC
1091 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1092 #else
1093 dupfd = dup(fd);
1094 if (dupfd != -1) {
1095 qemu_set_cloexec(dupfd);
1097 #endif
1098 if (dupfd == -1) {
1099 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1100 "Error duplicating fd: %s", strerror(errno));
1101 return -1;
1104 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1105 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1106 fd_opaque, NULL);
1108 return 0;
1111 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1113 int fd;
1115 fd = qemu_opt_get_number(opts, "fd", -1);
1116 close(fd);
1118 return 0;
1120 #endif
1122 /***********************************************************/
1123 /* QEMU Block devices */
1125 #define HD_OPTS "media=disk"
1126 #define CDROM_OPTS "media=cdrom"
1127 #define FD_OPTS ""
1128 #define PFLASH_OPTS ""
1129 #define MTD_OPTS ""
1130 #define SD_OPTS ""
1132 static int drive_init_func(QemuOpts *opts, void *opaque)
1134 BlockInterfaceType *block_default_type = opaque;
1136 return drive_new(opts, *block_default_type) == NULL;
1139 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1141 if (NULL == qemu_opt_get(opts, "snapshot")) {
1142 qemu_opt_set(opts, "snapshot", "on");
1144 return 0;
1147 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1148 int index, const char *optstr)
1150 QemuOpts *opts;
1152 if (!enable || drive_get_by_index(type, index)) {
1153 return;
1156 opts = drive_add(type, index, NULL, optstr);
1157 if (snapshot) {
1158 drive_enable_snapshot(opts, NULL);
1160 if (!drive_new(opts, type)) {
1161 exit(1);
1165 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1167 boot_set_handler = func;
1168 boot_set_opaque = opaque;
1171 int qemu_boot_set(const char *boot_order)
1173 if (!boot_set_handler) {
1174 return -EINVAL;
1176 return boot_set_handler(boot_set_opaque, boot_order);
1179 static void validate_bootdevices(const char *devices)
1181 /* We just do some generic consistency checks */
1182 const char *p;
1183 int bitmap = 0;
1185 for (p = devices; *p != '\0'; p++) {
1186 /* Allowed boot devices are:
1187 * a-b: floppy disk drives
1188 * c-f: IDE disk drives
1189 * g-m: machine implementation dependent drives
1190 * n-p: network devices
1191 * It's up to each machine implementation to check if the given boot
1192 * devices match the actual hardware implementation and firmware
1193 * features.
1195 if (*p < 'a' || *p > 'p') {
1196 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1197 exit(1);
1199 if (bitmap & (1 << (*p - 'a'))) {
1200 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1201 exit(1);
1203 bitmap |= 1 << (*p - 'a');
1207 static void restore_boot_order(void *opaque)
1209 char *normal_boot_order = opaque;
1210 static int first = 1;
1212 /* Restore boot order and remove ourselves after the first boot */
1213 if (first) {
1214 first = 0;
1215 return;
1218 qemu_boot_set(normal_boot_order);
1220 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1221 g_free(normal_boot_order);
1224 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1225 const char *suffix)
1227 FWBootEntry *node, *i;
1229 if (bootindex < 0) {
1230 return;
1233 assert(dev != NULL || suffix != NULL);
1235 node = g_malloc0(sizeof(FWBootEntry));
1236 node->bootindex = bootindex;
1237 node->suffix = g_strdup(suffix);
1238 node->dev = dev;
1240 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1241 if (i->bootindex == bootindex) {
1242 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1243 exit(1);
1244 } else if (i->bootindex < bootindex) {
1245 continue;
1247 QTAILQ_INSERT_BEFORE(i, node, link);
1248 return;
1250 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1253 DeviceState *get_boot_device(uint32_t position)
1255 uint32_t counter = 0;
1256 FWBootEntry *i = NULL;
1257 DeviceState *res = NULL;
1259 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1260 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1261 if (counter == position) {
1262 res = i->dev;
1263 break;
1265 counter++;
1268 return res;
1272 * This function returns null terminated string that consist of new line
1273 * separated device paths.
1275 * memory pointed by "size" is assigned total length of the array in bytes
1278 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1280 FWBootEntry *i;
1281 size_t total = 0;
1282 char *list = NULL;
1284 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1285 char *devpath = NULL, *bootpath;
1286 size_t len;
1288 if (i->dev) {
1289 devpath = qdev_get_fw_dev_path(i->dev);
1290 assert(devpath);
1293 if (i->suffix && !ignore_suffixes && devpath) {
1294 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1296 bootpath = g_malloc(bootpathlen);
1297 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1298 g_free(devpath);
1299 } else if (devpath) {
1300 bootpath = devpath;
1301 } else if (!ignore_suffixes) {
1302 assert(i->suffix);
1303 bootpath = g_strdup(i->suffix);
1304 } else {
1305 bootpath = g_strdup("");
1308 if (total) {
1309 list[total-1] = '\n';
1311 len = strlen(bootpath) + 1;
1312 list = g_realloc(list, total + len);
1313 memcpy(&list[total], bootpath, len);
1314 total += len;
1315 g_free(bootpath);
1318 *size = total;
1320 if (boot_strict && *size > 0) {
1321 list[total-1] = '\n';
1322 list = g_realloc(list, total + 5);
1323 memcpy(&list[total], "HALT", 5);
1324 *size = total + 5;
1326 return list;
1329 static QemuOptsList qemu_smp_opts = {
1330 .name = "smp-opts",
1331 .implied_opt_name = "cpus",
1332 .merge_lists = true,
1333 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1334 .desc = {
1336 .name = "cpus",
1337 .type = QEMU_OPT_NUMBER,
1338 }, {
1339 .name = "sockets",
1340 .type = QEMU_OPT_NUMBER,
1341 }, {
1342 .name = "cores",
1343 .type = QEMU_OPT_NUMBER,
1344 }, {
1345 .name = "threads",
1346 .type = QEMU_OPT_NUMBER,
1347 }, {
1348 .name = "maxcpus",
1349 .type = QEMU_OPT_NUMBER,
1351 { /*End of list */ }
1355 static void smp_parse(QemuOpts *opts)
1357 if (opts) {
1359 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1360 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1361 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1362 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1364 /* compute missing values, prefer sockets over cores over threads */
1365 if (cpus == 0 || sockets == 0) {
1366 sockets = sockets > 0 ? sockets : 1;
1367 cores = cores > 0 ? cores : 1;
1368 threads = threads > 0 ? threads : 1;
1369 if (cpus == 0) {
1370 cpus = cores * threads * sockets;
1372 } else {
1373 if (cores == 0) {
1374 threads = threads > 0 ? threads : 1;
1375 cores = cpus / (sockets * threads);
1376 } else {
1377 threads = cpus / (cores * sockets);
1381 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1383 smp_cpus = cpus;
1384 smp_cores = cores > 0 ? cores : 1;
1385 smp_threads = threads > 0 ? threads : 1;
1389 if (max_cpus == 0) {
1390 max_cpus = smp_cpus;
1393 if (max_cpus > MAX_CPUMASK_BITS) {
1394 fprintf(stderr, "Unsupported number of maxcpus\n");
1395 exit(1);
1397 if (max_cpus < smp_cpus) {
1398 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1399 exit(1);
1404 static void configure_realtime(QemuOpts *opts)
1406 bool enable_mlock;
1408 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1410 if (enable_mlock) {
1411 if (os_mlock() < 0) {
1412 fprintf(stderr, "qemu: locking memory failed\n");
1413 exit(1);
1419 static void configure_msg(QemuOpts *opts)
1421 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1424 /***********************************************************/
1425 /* USB devices */
1427 static int usb_device_add(const char *devname)
1429 USBDevice *dev = NULL;
1430 #ifndef CONFIG_LINUX
1431 const char *p;
1432 #endif
1434 if (!usb_enabled(false)) {
1435 return -1;
1438 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1439 dev = usbdevice_create(devname);
1440 if (dev)
1441 goto done;
1443 /* the other ones */
1444 #ifndef CONFIG_LINUX
1445 /* only the linux version is qdev-ified, usb-bsd still needs this */
1446 if (strstart(devname, "host:", &p)) {
1447 dev = usb_host_device_open(usb_bus_find(-1), p);
1449 #endif
1450 if (!dev)
1451 return -1;
1453 done:
1454 return 0;
1457 static int usb_device_del(const char *devname)
1459 int bus_num, addr;
1460 const char *p;
1462 if (strstart(devname, "host:", &p)) {
1463 return -1;
1466 if (!usb_enabled(false)) {
1467 return -1;
1470 p = strchr(devname, '.');
1471 if (!p)
1472 return -1;
1473 bus_num = strtoul(devname, NULL, 0);
1474 addr = strtoul(p + 1, NULL, 0);
1476 return usb_device_delete_addr(bus_num, addr);
1479 static int usb_parse(const char *cmdline)
1481 int r;
1482 r = usb_device_add(cmdline);
1483 if (r < 0) {
1484 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1486 return r;
1489 void do_usb_add(Monitor *mon, const QDict *qdict)
1491 const char *devname = qdict_get_str(qdict, "devname");
1492 if (usb_device_add(devname) < 0) {
1493 error_report("could not add USB device '%s'", devname);
1497 void do_usb_del(Monitor *mon, const QDict *qdict)
1499 const char *devname = qdict_get_str(qdict, "devname");
1500 if (usb_device_del(devname) < 0) {
1501 error_report("could not delete USB device '%s'", devname);
1505 /***********************************************************/
1506 /* PCMCIA/Cardbus */
1508 static struct pcmcia_socket_entry_s {
1509 PCMCIASocket *socket;
1510 struct pcmcia_socket_entry_s *next;
1511 } *pcmcia_sockets = 0;
1513 void pcmcia_socket_register(PCMCIASocket *socket)
1515 struct pcmcia_socket_entry_s *entry;
1517 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1518 entry->socket = socket;
1519 entry->next = pcmcia_sockets;
1520 pcmcia_sockets = entry;
1523 void pcmcia_socket_unregister(PCMCIASocket *socket)
1525 struct pcmcia_socket_entry_s *entry, **ptr;
1527 ptr = &pcmcia_sockets;
1528 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1529 if (entry->socket == socket) {
1530 *ptr = entry->next;
1531 g_free(entry);
1535 void pcmcia_info(Monitor *mon, const QDict *qdict)
1537 struct pcmcia_socket_entry_s *iter;
1539 if (!pcmcia_sockets)
1540 monitor_printf(mon, "No PCMCIA sockets\n");
1542 for (iter = pcmcia_sockets; iter; iter = iter->next)
1543 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1544 iter->socket->attached ? iter->socket->card_string :
1545 "Empty");
1548 /***********************************************************/
1549 /* machine registration */
1551 MachineState *current_machine;
1553 static void machine_class_init(ObjectClass *oc, void *data)
1555 MachineClass *mc = MACHINE_CLASS(oc);
1556 QEMUMachine *qm = data;
1558 mc->name = qm->name;
1559 mc->alias = qm->alias;
1560 mc->desc = qm->desc;
1561 mc->init = qm->init;
1562 mc->reset = qm->reset;
1563 mc->hot_add_cpu = qm->hot_add_cpu;
1564 mc->kvm_type = qm->kvm_type;
1565 mc->block_default_type = qm->block_default_type;
1566 mc->max_cpus = qm->max_cpus;
1567 mc->no_serial = qm->no_serial;
1568 mc->no_parallel = qm->no_parallel;
1569 mc->use_virtcon = qm->use_virtcon;
1570 mc->use_sclp = qm->use_sclp;
1571 mc->no_floppy = qm->no_floppy;
1572 mc->no_cdrom = qm->no_cdrom;
1573 mc->no_sdcard = qm->no_sdcard;
1574 mc->is_default = qm->is_default;
1575 mc->default_machine_opts = qm->default_machine_opts;
1576 mc->default_boot_order = qm->default_boot_order;
1577 mc->compat_props = qm->compat_props;
1578 mc->hw_version = qm->hw_version;
1581 int qemu_register_machine(QEMUMachine *m)
1583 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1584 TypeInfo ti = {
1585 .name = name,
1586 .parent = TYPE_MACHINE,
1587 .class_init = machine_class_init,
1588 .class_data = (void *)m,
1591 type_register(&ti);
1592 g_free(name);
1594 return 0;
1597 static MachineClass *find_machine(const char *name)
1599 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1600 MachineClass *mc = NULL;
1602 for (el = machines; el; el = el->next) {
1603 MachineClass *temp = el->data;
1605 if (!strcmp(temp->name, name)) {
1606 mc = temp;
1607 break;
1609 if (temp->alias &&
1610 !strcmp(temp->alias, name)) {
1611 mc = temp;
1612 break;
1616 g_slist_free(machines);
1617 return mc;
1620 MachineClass *find_default_machine(void)
1622 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1623 MachineClass *mc = NULL;
1625 for (el = machines; el; el = el->next) {
1626 MachineClass *temp = el->data;
1628 if (temp->is_default) {
1629 mc = temp;
1630 break;
1634 g_slist_free(machines);
1635 return mc;
1638 MachineInfoList *qmp_query_machines(Error **errp)
1640 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1641 MachineInfoList *mach_list = NULL;
1643 for (el = machines; el; el = el->next) {
1644 MachineClass *mc = el->data;
1645 MachineInfoList *entry;
1646 MachineInfo *info;
1648 info = g_malloc0(sizeof(*info));
1649 if (mc->is_default) {
1650 info->has_is_default = true;
1651 info->is_default = true;
1654 if (mc->alias) {
1655 info->has_alias = true;
1656 info->alias = g_strdup(mc->alias);
1659 info->name = g_strdup(mc->name);
1660 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1662 entry = g_malloc0(sizeof(*entry));
1663 entry->value = info;
1664 entry->next = mach_list;
1665 mach_list = entry;
1668 g_slist_free(machines);
1669 return mach_list;
1672 /***********************************************************/
1673 /* main execution loop */
1675 struct vm_change_state_entry {
1676 VMChangeStateHandler *cb;
1677 void *opaque;
1678 QLIST_ENTRY (vm_change_state_entry) entries;
1681 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1683 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1684 void *opaque)
1686 VMChangeStateEntry *e;
1688 e = g_malloc0(sizeof (*e));
1690 e->cb = cb;
1691 e->opaque = opaque;
1692 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1693 return e;
1696 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1698 QLIST_REMOVE (e, entries);
1699 g_free (e);
1702 void vm_state_notify(int running, RunState state)
1704 VMChangeStateEntry *e;
1706 trace_vm_state_notify(running, state);
1708 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1709 e->cb(e->opaque, running, state);
1713 /* reset/shutdown handler */
1715 typedef struct QEMUResetEntry {
1716 QTAILQ_ENTRY(QEMUResetEntry) entry;
1717 QEMUResetHandler *func;
1718 void *opaque;
1719 } QEMUResetEntry;
1721 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1722 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1723 static int reset_requested;
1724 static int shutdown_requested, shutdown_signal = -1;
1725 static pid_t shutdown_pid;
1726 static int powerdown_requested;
1727 static int debug_requested;
1728 static int suspend_requested;
1729 static WakeupReason wakeup_reason;
1730 static NotifierList powerdown_notifiers =
1731 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1732 static NotifierList suspend_notifiers =
1733 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1734 static NotifierList wakeup_notifiers =
1735 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1736 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1738 int qemu_shutdown_requested_get(void)
1740 return shutdown_requested;
1743 int qemu_reset_requested_get(void)
1745 return reset_requested;
1748 static int qemu_shutdown_requested(void)
1750 int r = shutdown_requested;
1751 shutdown_requested = 0;
1752 return r;
1755 static void qemu_kill_report(void)
1757 if (!qtest_driver() && shutdown_signal != -1) {
1758 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1759 if (shutdown_pid == 0) {
1760 /* This happens for eg ^C at the terminal, so it's worth
1761 * avoiding printing an odd message in that case.
1763 fputc('\n', stderr);
1764 } else {
1765 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1767 shutdown_signal = -1;
1771 static int qemu_reset_requested(void)
1773 int r = reset_requested;
1774 reset_requested = 0;
1775 return r;
1778 static int qemu_suspend_requested(void)
1780 int r = suspend_requested;
1781 suspend_requested = 0;
1782 return r;
1785 static WakeupReason qemu_wakeup_requested(void)
1787 return wakeup_reason;
1790 static int qemu_powerdown_requested(void)
1792 int r = powerdown_requested;
1793 powerdown_requested = 0;
1794 return r;
1797 static int qemu_debug_requested(void)
1799 int r = debug_requested;
1800 debug_requested = 0;
1801 return r;
1804 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1806 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1808 re->func = func;
1809 re->opaque = opaque;
1810 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1813 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1815 QEMUResetEntry *re;
1817 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1818 if (re->func == func && re->opaque == opaque) {
1819 QTAILQ_REMOVE(&reset_handlers, re, entry);
1820 g_free(re);
1821 return;
1826 void qemu_devices_reset(void)
1828 QEMUResetEntry *re, *nre;
1830 /* reset all devices */
1831 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1832 re->func(re->opaque);
1836 void qemu_system_reset(bool report)
1838 MachineClass *mc;
1840 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1842 if (mc && mc->reset) {
1843 mc->reset();
1844 } else {
1845 qemu_devices_reset();
1847 if (report) {
1848 monitor_protocol_event(QEVENT_RESET, NULL);
1850 cpu_synchronize_all_post_reset();
1853 void qemu_system_reset_request(void)
1855 if (no_reboot) {
1856 shutdown_requested = 1;
1857 } else {
1858 reset_requested = 1;
1860 cpu_stop_current();
1861 qemu_notify_event();
1864 static void qemu_system_suspend(void)
1866 pause_all_vcpus();
1867 notifier_list_notify(&suspend_notifiers, NULL);
1868 runstate_set(RUN_STATE_SUSPENDED);
1869 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1872 void qemu_system_suspend_request(void)
1874 if (runstate_check(RUN_STATE_SUSPENDED)) {
1875 return;
1877 suspend_requested = 1;
1878 cpu_stop_current();
1879 qemu_notify_event();
1882 void qemu_register_suspend_notifier(Notifier *notifier)
1884 notifier_list_add(&suspend_notifiers, notifier);
1887 void qemu_system_wakeup_request(WakeupReason reason)
1889 trace_system_wakeup_request(reason);
1891 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1892 return;
1894 if (!(wakeup_reason_mask & (1 << reason))) {
1895 return;
1897 runstate_set(RUN_STATE_RUNNING);
1898 wakeup_reason = reason;
1899 qemu_notify_event();
1902 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1904 if (enabled) {
1905 wakeup_reason_mask |= (1 << reason);
1906 } else {
1907 wakeup_reason_mask &= ~(1 << reason);
1911 void qemu_register_wakeup_notifier(Notifier *notifier)
1913 notifier_list_add(&wakeup_notifiers, notifier);
1916 void qemu_system_killed(int signal, pid_t pid)
1918 shutdown_signal = signal;
1919 shutdown_pid = pid;
1920 no_shutdown = 0;
1921 qemu_system_shutdown_request();
1924 void qemu_system_shutdown_request(void)
1926 shutdown_requested = 1;
1927 qemu_notify_event();
1930 static void qemu_system_powerdown(void)
1932 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1933 notifier_list_notify(&powerdown_notifiers, NULL);
1936 void qemu_system_powerdown_request(void)
1938 powerdown_requested = 1;
1939 qemu_notify_event();
1942 void qemu_register_powerdown_notifier(Notifier *notifier)
1944 notifier_list_add(&powerdown_notifiers, notifier);
1947 void qemu_system_debug_request(void)
1949 debug_requested = 1;
1950 qemu_notify_event();
1953 static bool main_loop_should_exit(void)
1955 RunState r;
1956 if (qemu_debug_requested()) {
1957 vm_stop(RUN_STATE_DEBUG);
1959 if (qemu_suspend_requested()) {
1960 qemu_system_suspend();
1962 if (qemu_shutdown_requested()) {
1963 qemu_kill_report();
1964 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1965 if (no_shutdown) {
1966 vm_stop(RUN_STATE_SHUTDOWN);
1967 } else {
1968 return true;
1971 if (qemu_reset_requested()) {
1972 pause_all_vcpus();
1973 cpu_synchronize_all_states();
1974 qemu_system_reset(VMRESET_REPORT);
1975 resume_all_vcpus();
1976 if (runstate_needs_reset()) {
1977 runstate_set(RUN_STATE_PAUSED);
1980 if (qemu_wakeup_requested()) {
1981 pause_all_vcpus();
1982 cpu_synchronize_all_states();
1983 qemu_system_reset(VMRESET_SILENT);
1984 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1985 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1986 resume_all_vcpus();
1987 monitor_protocol_event(QEVENT_WAKEUP, NULL);
1989 if (qemu_powerdown_requested()) {
1990 qemu_system_powerdown();
1992 if (qemu_vmstop_requested(&r)) {
1993 vm_stop(r);
1995 return false;
1998 static void main_loop(void)
2000 bool nonblocking;
2001 int last_io = 0;
2002 #ifdef CONFIG_PROFILER
2003 int64_t ti;
2004 #endif
2005 do {
2006 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2007 #ifdef CONFIG_PROFILER
2008 ti = profile_getclock();
2009 #endif
2010 last_io = main_loop_wait(nonblocking);
2011 #ifdef CONFIG_PROFILER
2012 dev_time += profile_getclock() - ti;
2013 #endif
2014 } while (!main_loop_should_exit());
2017 static void version(void)
2019 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2022 static void help(int exitcode)
2024 version();
2025 printf("usage: %s [options] [disk_image]\n\n"
2026 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2027 error_get_progname());
2029 #define QEMU_OPTIONS_GENERATE_HELP
2030 #include "qemu-options-wrapper.h"
2032 printf("\nDuring emulation, the following keys are useful:\n"
2033 "ctrl-alt-f toggle full screen\n"
2034 "ctrl-alt-n switch to virtual console 'n'\n"
2035 "ctrl-alt toggle mouse and keyboard grab\n"
2036 "\n"
2037 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2039 exit(exitcode);
2042 #define HAS_ARG 0x0001
2044 typedef struct QEMUOption {
2045 const char *name;
2046 int flags;
2047 int index;
2048 uint32_t arch_mask;
2049 } QEMUOption;
2051 static const QEMUOption qemu_options[] = {
2052 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2053 #define QEMU_OPTIONS_GENERATE_OPTIONS
2054 #include "qemu-options-wrapper.h"
2055 { NULL },
2058 static bool vga_available(void)
2060 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2063 static bool cirrus_vga_available(void)
2065 return object_class_by_name("cirrus-vga")
2066 || object_class_by_name("isa-cirrus-vga");
2069 static bool vmware_vga_available(void)
2071 return object_class_by_name("vmware-svga");
2074 static bool qxl_vga_available(void)
2076 return object_class_by_name("qxl-vga");
2079 static bool tcx_vga_available(void)
2081 return object_class_by_name("SUNW,tcx");
2084 static bool cg3_vga_available(void)
2086 return object_class_by_name("cgthree");
2089 static void select_vgahw (const char *p)
2091 const char *opts;
2093 assert(vga_interface_type == VGA_NONE);
2094 if (strstart(p, "std", &opts)) {
2095 if (vga_available()) {
2096 vga_interface_type = VGA_STD;
2097 } else {
2098 fprintf(stderr, "Error: standard VGA not available\n");
2099 exit(0);
2101 } else if (strstart(p, "cirrus", &opts)) {
2102 if (cirrus_vga_available()) {
2103 vga_interface_type = VGA_CIRRUS;
2104 } else {
2105 fprintf(stderr, "Error: Cirrus VGA not available\n");
2106 exit(0);
2108 } else if (strstart(p, "vmware", &opts)) {
2109 if (vmware_vga_available()) {
2110 vga_interface_type = VGA_VMWARE;
2111 } else {
2112 fprintf(stderr, "Error: VMWare SVGA not available\n");
2113 exit(0);
2115 } else if (strstart(p, "xenfb", &opts)) {
2116 vga_interface_type = VGA_XENFB;
2117 } else if (strstart(p, "qxl", &opts)) {
2118 if (qxl_vga_available()) {
2119 vga_interface_type = VGA_QXL;
2120 } else {
2121 fprintf(stderr, "Error: QXL VGA not available\n");
2122 exit(0);
2124 } else if (strstart(p, "tcx", &opts)) {
2125 if (tcx_vga_available()) {
2126 vga_interface_type = VGA_TCX;
2127 } else {
2128 fprintf(stderr, "Error: TCX framebuffer not available\n");
2129 exit(0);
2131 } else if (strstart(p, "cg3", &opts)) {
2132 if (cg3_vga_available()) {
2133 vga_interface_type = VGA_CG3;
2134 } else {
2135 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2136 exit(0);
2138 } else if (!strstart(p, "none", &opts)) {
2139 invalid_vga:
2140 fprintf(stderr, "Unknown vga type: %s\n", p);
2141 exit(1);
2143 while (*opts) {
2144 const char *nextopt;
2146 if (strstart(opts, ",retrace=", &nextopt)) {
2147 opts = nextopt;
2148 if (strstart(opts, "dumb", &nextopt))
2149 vga_retrace_method = VGA_RETRACE_DUMB;
2150 else if (strstart(opts, "precise", &nextopt))
2151 vga_retrace_method = VGA_RETRACE_PRECISE;
2152 else goto invalid_vga;
2153 } else goto invalid_vga;
2154 opts = nextopt;
2158 static DisplayType select_display(const char *p)
2160 const char *opts;
2161 DisplayType display = DT_DEFAULT;
2163 if (strstart(p, "sdl", &opts)) {
2164 #ifdef CONFIG_SDL
2165 display = DT_SDL;
2166 while (*opts) {
2167 const char *nextopt;
2169 if (strstart(opts, ",frame=", &nextopt)) {
2170 opts = nextopt;
2171 if (strstart(opts, "on", &nextopt)) {
2172 no_frame = 0;
2173 } else if (strstart(opts, "off", &nextopt)) {
2174 no_frame = 1;
2175 } else {
2176 goto invalid_sdl_args;
2178 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2179 opts = nextopt;
2180 if (strstart(opts, "on", &nextopt)) {
2181 alt_grab = 1;
2182 } else if (strstart(opts, "off", &nextopt)) {
2183 alt_grab = 0;
2184 } else {
2185 goto invalid_sdl_args;
2187 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2188 opts = nextopt;
2189 if (strstart(opts, "on", &nextopt)) {
2190 ctrl_grab = 1;
2191 } else if (strstart(opts, "off", &nextopt)) {
2192 ctrl_grab = 0;
2193 } else {
2194 goto invalid_sdl_args;
2196 } else if (strstart(opts, ",window_close=", &nextopt)) {
2197 opts = nextopt;
2198 if (strstart(opts, "on", &nextopt)) {
2199 no_quit = 0;
2200 } else if (strstart(opts, "off", &nextopt)) {
2201 no_quit = 1;
2202 } else {
2203 goto invalid_sdl_args;
2205 } else {
2206 invalid_sdl_args:
2207 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2208 exit(1);
2210 opts = nextopt;
2212 #else
2213 fprintf(stderr, "SDL support is disabled\n");
2214 exit(1);
2215 #endif
2216 } else if (strstart(p, "vnc", &opts)) {
2217 #ifdef CONFIG_VNC
2218 display_remote++;
2220 if (*opts) {
2221 const char *nextopt;
2223 if (strstart(opts, "=", &nextopt)) {
2224 vnc_display = nextopt;
2227 if (!vnc_display) {
2228 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2229 exit(1);
2231 #else
2232 fprintf(stderr, "VNC support is disabled\n");
2233 exit(1);
2234 #endif
2235 } else if (strstart(p, "curses", &opts)) {
2236 #ifdef CONFIG_CURSES
2237 display = DT_CURSES;
2238 #else
2239 fprintf(stderr, "Curses support is disabled\n");
2240 exit(1);
2241 #endif
2242 } else if (strstart(p, "gtk", &opts)) {
2243 #ifdef CONFIG_GTK
2244 display = DT_GTK;
2245 while (*opts) {
2246 const char *nextopt;
2248 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2249 opts = nextopt;
2250 if (strstart(opts, "on", &nextopt)) {
2251 grab_on_hover = true;
2252 } else if (strstart(opts, "off", &nextopt)) {
2253 grab_on_hover = false;
2254 } else {
2255 goto invalid_gtk_args;
2257 } else {
2258 invalid_gtk_args:
2259 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2260 exit(1);
2262 opts = nextopt;
2264 #else
2265 fprintf(stderr, "GTK support is disabled\n");
2266 exit(1);
2267 #endif
2268 } else if (strstart(p, "none", &opts)) {
2269 display = DT_NONE;
2270 } else {
2271 fprintf(stderr, "Unknown display type: %s\n", p);
2272 exit(1);
2275 return display;
2278 static int balloon_parse(const char *arg)
2280 QemuOpts *opts;
2282 if (strcmp(arg, "none") == 0) {
2283 return 0;
2286 if (!strncmp(arg, "virtio", 6)) {
2287 if (arg[6] == ',') {
2288 /* have params -> parse them */
2289 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2290 if (!opts)
2291 return -1;
2292 } else {
2293 /* create empty opts */
2294 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2295 &error_abort);
2297 qemu_opt_set(opts, "driver", "virtio-balloon");
2298 return 0;
2301 return -1;
2304 char *qemu_find_file(int type, const char *name)
2306 int i;
2307 const char *subdir;
2308 char *buf;
2310 /* Try the name as a straight path first */
2311 if (access(name, R_OK) == 0) {
2312 trace_load_file(name, name);
2313 return g_strdup(name);
2316 switch (type) {
2317 case QEMU_FILE_TYPE_BIOS:
2318 subdir = "";
2319 break;
2320 case QEMU_FILE_TYPE_KEYMAP:
2321 subdir = "keymaps/";
2322 break;
2323 default:
2324 abort();
2327 for (i = 0; i < data_dir_idx; i++) {
2328 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2329 if (access(buf, R_OK) == 0) {
2330 trace_load_file(name, buf);
2331 return buf;
2333 g_free(buf);
2335 return NULL;
2338 static int device_help_func(QemuOpts *opts, void *opaque)
2340 return qdev_device_help(opts);
2343 static int device_init_func(QemuOpts *opts, void *opaque)
2345 DeviceState *dev;
2347 dev = qdev_device_add(opts);
2348 if (!dev)
2349 return -1;
2350 object_unref(OBJECT(dev));
2351 return 0;
2354 static int chardev_init_func(QemuOpts *opts, void *opaque)
2356 Error *local_err = NULL;
2358 qemu_chr_new_from_opts(opts, NULL, &local_err);
2359 if (local_err) {
2360 error_report("%s", error_get_pretty(local_err));
2361 error_free(local_err);
2362 return -1;
2364 return 0;
2367 #ifdef CONFIG_VIRTFS
2368 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2370 int ret;
2371 ret = qemu_fsdev_add(opts);
2373 return ret;
2375 #endif
2377 static int mon_init_func(QemuOpts *opts, void *opaque)
2379 CharDriverState *chr;
2380 const char *chardev;
2381 const char *mode;
2382 int flags;
2384 mode = qemu_opt_get(opts, "mode");
2385 if (mode == NULL) {
2386 mode = "readline";
2388 if (strcmp(mode, "readline") == 0) {
2389 flags = MONITOR_USE_READLINE;
2390 } else if (strcmp(mode, "control") == 0) {
2391 flags = MONITOR_USE_CONTROL;
2392 } else {
2393 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2394 exit(1);
2397 if (qemu_opt_get_bool(opts, "pretty", 0))
2398 flags |= MONITOR_USE_PRETTY;
2400 if (qemu_opt_get_bool(opts, "default", 0))
2401 flags |= MONITOR_IS_DEFAULT;
2403 chardev = qemu_opt_get(opts, "chardev");
2404 chr = qemu_chr_find(chardev);
2405 if (chr == NULL) {
2406 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2407 exit(1);
2410 qemu_chr_fe_claim_no_fail(chr);
2411 monitor_init(chr, flags);
2412 return 0;
2415 static void monitor_parse(const char *optarg, const char *mode)
2417 static int monitor_device_index = 0;
2418 QemuOpts *opts;
2419 const char *p;
2420 char label[32];
2421 int def = 0;
2423 if (strstart(optarg, "chardev:", &p)) {
2424 snprintf(label, sizeof(label), "%s", p);
2425 } else {
2426 snprintf(label, sizeof(label), "compat_monitor%d",
2427 monitor_device_index);
2428 if (monitor_device_index == 0) {
2429 def = 1;
2431 opts = qemu_chr_parse_compat(label, optarg);
2432 if (!opts) {
2433 fprintf(stderr, "parse error: %s\n", optarg);
2434 exit(1);
2438 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2439 if (!opts) {
2440 fprintf(stderr, "duplicate chardev: %s\n", label);
2441 exit(1);
2443 qemu_opt_set(opts, "mode", mode);
2444 qemu_opt_set(opts, "chardev", label);
2445 if (def)
2446 qemu_opt_set(opts, "default", "on");
2447 monitor_device_index++;
2450 struct device_config {
2451 enum {
2452 DEV_USB, /* -usbdevice */
2453 DEV_BT, /* -bt */
2454 DEV_SERIAL, /* -serial */
2455 DEV_PARALLEL, /* -parallel */
2456 DEV_VIRTCON, /* -virtioconsole */
2457 DEV_DEBUGCON, /* -debugcon */
2458 DEV_GDB, /* -gdb, -s */
2459 DEV_SCLP, /* s390 sclp */
2460 } type;
2461 const char *cmdline;
2462 Location loc;
2463 QTAILQ_ENTRY(device_config) next;
2466 static QTAILQ_HEAD(, device_config) device_configs =
2467 QTAILQ_HEAD_INITIALIZER(device_configs);
2469 static void add_device_config(int type, const char *cmdline)
2471 struct device_config *conf;
2473 conf = g_malloc0(sizeof(*conf));
2474 conf->type = type;
2475 conf->cmdline = cmdline;
2476 loc_save(&conf->loc);
2477 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2480 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2482 struct device_config *conf;
2483 int rc;
2485 QTAILQ_FOREACH(conf, &device_configs, next) {
2486 if (conf->type != type)
2487 continue;
2488 loc_push_restore(&conf->loc);
2489 rc = func(conf->cmdline);
2490 loc_pop(&conf->loc);
2491 if (0 != rc)
2492 return rc;
2494 return 0;
2497 static int serial_parse(const char *devname)
2499 static int index = 0;
2500 char label[32];
2502 if (strcmp(devname, "none") == 0)
2503 return 0;
2504 if (index == MAX_SERIAL_PORTS) {
2505 fprintf(stderr, "qemu: too many serial ports\n");
2506 exit(1);
2508 snprintf(label, sizeof(label), "serial%d", index);
2509 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2510 if (!serial_hds[index]) {
2511 fprintf(stderr, "qemu: could not connect serial device"
2512 " to character backend '%s'\n", devname);
2513 return -1;
2515 index++;
2516 return 0;
2519 static int parallel_parse(const char *devname)
2521 static int index = 0;
2522 char label[32];
2524 if (strcmp(devname, "none") == 0)
2525 return 0;
2526 if (index == MAX_PARALLEL_PORTS) {
2527 fprintf(stderr, "qemu: too many parallel ports\n");
2528 exit(1);
2530 snprintf(label, sizeof(label), "parallel%d", index);
2531 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2532 if (!parallel_hds[index]) {
2533 fprintf(stderr, "qemu: could not connect parallel device"
2534 " to character backend '%s'\n", devname);
2535 return -1;
2537 index++;
2538 return 0;
2541 static int virtcon_parse(const char *devname)
2543 QemuOptsList *device = qemu_find_opts("device");
2544 static int index = 0;
2545 char label[32];
2546 QemuOpts *bus_opts, *dev_opts;
2548 if (strcmp(devname, "none") == 0)
2549 return 0;
2550 if (index == MAX_VIRTIO_CONSOLES) {
2551 fprintf(stderr, "qemu: too many virtio consoles\n");
2552 exit(1);
2555 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2556 if (arch_type == QEMU_ARCH_S390X) {
2557 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2558 } else {
2559 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2562 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2563 qemu_opt_set(dev_opts, "driver", "virtconsole");
2565 snprintf(label, sizeof(label), "virtcon%d", index);
2566 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2567 if (!virtcon_hds[index]) {
2568 fprintf(stderr, "qemu: could not connect virtio console"
2569 " to character backend '%s'\n", devname);
2570 return -1;
2572 qemu_opt_set(dev_opts, "chardev", label);
2574 index++;
2575 return 0;
2578 static int sclp_parse(const char *devname)
2580 QemuOptsList *device = qemu_find_opts("device");
2581 static int index = 0;
2582 char label[32];
2583 QemuOpts *dev_opts;
2585 if (strcmp(devname, "none") == 0) {
2586 return 0;
2588 if (index == MAX_SCLP_CONSOLES) {
2589 fprintf(stderr, "qemu: too many sclp consoles\n");
2590 exit(1);
2593 assert(arch_type == QEMU_ARCH_S390X);
2595 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2596 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2598 snprintf(label, sizeof(label), "sclpcon%d", index);
2599 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2600 if (!sclp_hds[index]) {
2601 fprintf(stderr, "qemu: could not connect sclp console"
2602 " to character backend '%s'\n", devname);
2603 return -1;
2605 qemu_opt_set(dev_opts, "chardev", label);
2607 index++;
2608 return 0;
2611 static int debugcon_parse(const char *devname)
2613 QemuOpts *opts;
2615 if (!qemu_chr_new("debugcon", devname, NULL)) {
2616 exit(1);
2618 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2619 if (!opts) {
2620 fprintf(stderr, "qemu: already have a debugcon device\n");
2621 exit(1);
2623 qemu_opt_set(opts, "driver", "isa-debugcon");
2624 qemu_opt_set(opts, "chardev", "debugcon");
2625 return 0;
2628 static MachineClass *machine_parse(const char *name)
2630 MachineClass *mc = NULL;
2631 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2633 if (name) {
2634 mc = find_machine(name);
2636 if (mc) {
2637 return mc;
2639 if (name && !is_help_option(name)) {
2640 error_report("Unsupported machine type");
2641 error_printf("Use -machine help to list supported machines!\n");
2642 } else {
2643 printf("Supported machines are:\n");
2644 for (el = machines; el; el = el->next) {
2645 MachineClass *mc = el->data;
2646 if (mc->alias) {
2647 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2649 printf("%-20s %s%s\n", mc->name, mc->desc,
2650 mc->is_default ? " (default)" : "");
2654 g_slist_free(machines);
2655 exit(!name || !is_help_option(name));
2658 static int tcg_init(MachineClass *mc)
2660 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2661 return 0;
2664 static struct {
2665 const char *opt_name;
2666 const char *name;
2667 int (*available)(void);
2668 int (*init)(MachineClass *mc);
2669 bool *allowed;
2670 } accel_list[] = {
2671 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2672 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2673 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2674 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2677 static int configure_accelerator(MachineClass *mc)
2679 const char *p;
2680 char buf[10];
2681 int i, ret;
2682 bool accel_initialised = false;
2683 bool init_failed = false;
2685 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2686 if (p == NULL) {
2687 /* Use the default "accelerator", tcg */
2688 p = "tcg";
2691 while (!accel_initialised && *p != '\0') {
2692 if (*p == ':') {
2693 p++;
2695 p = get_opt_name(buf, sizeof (buf), p, ':');
2696 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2697 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2698 if (!accel_list[i].available()) {
2699 printf("%s not supported for this target\n",
2700 accel_list[i].name);
2701 break;
2703 *(accel_list[i].allowed) = true;
2704 ret = accel_list[i].init(mc);
2705 if (ret < 0) {
2706 init_failed = true;
2707 fprintf(stderr, "failed to initialize %s: %s\n",
2708 accel_list[i].name,
2709 strerror(-ret));
2710 *(accel_list[i].allowed) = false;
2711 } else {
2712 accel_initialised = true;
2714 break;
2717 if (i == ARRAY_SIZE(accel_list)) {
2718 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2722 if (!accel_initialised) {
2723 if (!init_failed) {
2724 fprintf(stderr, "No accelerator found!\n");
2726 exit(1);
2729 if (init_failed) {
2730 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2733 return !accel_initialised;
2736 void qemu_add_exit_notifier(Notifier *notify)
2738 notifier_list_add(&exit_notifiers, notify);
2741 void qemu_remove_exit_notifier(Notifier *notify)
2743 notifier_remove(notify);
2746 static void qemu_run_exit_notifiers(void)
2748 notifier_list_notify(&exit_notifiers, NULL);
2751 void qemu_add_machine_init_done_notifier(Notifier *notify)
2753 notifier_list_add(&machine_init_done_notifiers, notify);
2756 static void qemu_run_machine_init_done_notifiers(void)
2758 notifier_list_notify(&machine_init_done_notifiers, NULL);
2761 static const QEMUOption *lookup_opt(int argc, char **argv,
2762 const char **poptarg, int *poptind)
2764 const QEMUOption *popt;
2765 int optind = *poptind;
2766 char *r = argv[optind];
2767 const char *optarg;
2769 loc_set_cmdline(argv, optind, 1);
2770 optind++;
2771 /* Treat --foo the same as -foo. */
2772 if (r[1] == '-')
2773 r++;
2774 popt = qemu_options;
2775 for(;;) {
2776 if (!popt->name) {
2777 error_report("invalid option");
2778 exit(1);
2780 if (!strcmp(popt->name, r + 1))
2781 break;
2782 popt++;
2784 if (popt->flags & HAS_ARG) {
2785 if (optind >= argc) {
2786 error_report("requires an argument");
2787 exit(1);
2789 optarg = argv[optind++];
2790 loc_set_cmdline(argv, optind - 2, 2);
2791 } else {
2792 optarg = NULL;
2795 *poptarg = optarg;
2796 *poptind = optind;
2798 return popt;
2801 static gpointer malloc_and_trace(gsize n_bytes)
2803 void *ptr = malloc(n_bytes);
2804 trace_g_malloc(n_bytes, ptr);
2805 return ptr;
2808 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2810 void *ptr = realloc(mem, n_bytes);
2811 trace_g_realloc(mem, n_bytes, ptr);
2812 return ptr;
2815 static void free_and_trace(gpointer mem)
2817 trace_g_free(mem);
2818 free(mem);
2821 static int object_set_property(const char *name, const char *value, void *opaque)
2823 Object *obj = OBJECT(opaque);
2824 StringInputVisitor *siv;
2825 Error *local_err = NULL;
2827 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2828 strcmp(name, "type") == 0) {
2829 return 0;
2832 siv = string_input_visitor_new(value);
2833 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2834 string_input_visitor_cleanup(siv);
2836 if (local_err) {
2837 qerror_report_err(local_err);
2838 error_free(local_err);
2839 return -1;
2842 return 0;
2845 static int object_create(QemuOpts *opts, void *opaque)
2847 Error *err = NULL;
2848 char *type = NULL;
2849 char *id = NULL;
2850 void *dummy = NULL;
2851 OptsVisitor *ov;
2852 QDict *pdict;
2854 ov = opts_visitor_new(opts);
2855 pdict = qemu_opts_to_qdict(opts, NULL);
2857 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2858 if (err) {
2859 goto out;
2862 qdict_del(pdict, "qom-type");
2863 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2864 if (err) {
2865 goto out;
2868 qdict_del(pdict, "id");
2869 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2870 if (err) {
2871 goto out;
2874 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2875 if (err) {
2876 goto out;
2878 visit_end_struct(opts_get_visitor(ov), &err);
2879 if (err) {
2880 qmp_object_del(id, NULL);
2883 out:
2884 opts_visitor_cleanup(ov);
2886 QDECREF(pdict);
2887 g_free(id);
2888 g_free(type);
2889 g_free(dummy);
2890 if (err) {
2891 qerror_report_err(err);
2892 return -1;
2894 return 0;
2897 int main(int argc, char **argv, char **envp)
2899 int i;
2900 int snapshot, linux_boot;
2901 const char *icount_option = NULL;
2902 const char *initrd_filename;
2903 const char *kernel_filename, *kernel_cmdline;
2904 const char *boot_order;
2905 DisplayState *ds;
2906 int cyls, heads, secs, translation;
2907 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2908 QemuOptsList *olist;
2909 int optind;
2910 const char *optarg;
2911 const char *loadvm = NULL;
2912 MachineClass *machine_class;
2913 const char *cpu_model;
2914 const char *vga_model = NULL;
2915 const char *qtest_chrdev = NULL;
2916 const char *qtest_log = NULL;
2917 const char *pid_file = NULL;
2918 const char *incoming = NULL;
2919 #ifdef CONFIG_VNC
2920 int show_vnc_port = 0;
2921 #endif
2922 bool defconfig = true;
2923 bool userconfig = true;
2924 const char *log_mask = NULL;
2925 const char *log_file = NULL;
2926 GMemVTable mem_trace = {
2927 .malloc = malloc_and_trace,
2928 .realloc = realloc_and_trace,
2929 .free = free_and_trace,
2931 const char *trace_events = NULL;
2932 const char *trace_file = NULL;
2933 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2934 1024 * 1024;
2935 ram_addr_t maxram_size = default_ram_size;
2936 uint64_t ram_slots = 0;
2938 atexit(qemu_run_exit_notifiers);
2939 error_set_progname(argv[0]);
2940 qemu_init_exec_dir(argv[0]);
2942 g_mem_set_vtable(&mem_trace);
2944 module_call_init(MODULE_INIT_QOM);
2946 qemu_add_opts(&qemu_drive_opts);
2947 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2948 qemu_add_drive_opts(&qemu_common_drive_opts);
2949 qemu_add_drive_opts(&qemu_drive_opts);
2950 qemu_add_opts(&qemu_chardev_opts);
2951 qemu_add_opts(&qemu_device_opts);
2952 qemu_add_opts(&qemu_netdev_opts);
2953 qemu_add_opts(&qemu_net_opts);
2954 qemu_add_opts(&qemu_rtc_opts);
2955 qemu_add_opts(&qemu_global_opts);
2956 qemu_add_opts(&qemu_mon_opts);
2957 qemu_add_opts(&qemu_trace_opts);
2958 qemu_add_opts(&qemu_option_rom_opts);
2959 qemu_add_opts(&qemu_machine_opts);
2960 qemu_add_opts(&qemu_mem_opts);
2961 qemu_add_opts(&qemu_smp_opts);
2962 qemu_add_opts(&qemu_boot_opts);
2963 qemu_add_opts(&qemu_sandbox_opts);
2964 qemu_add_opts(&qemu_add_fd_opts);
2965 qemu_add_opts(&qemu_object_opts);
2966 qemu_add_opts(&qemu_tpmdev_opts);
2967 qemu_add_opts(&qemu_realtime_opts);
2968 qemu_add_opts(&qemu_msg_opts);
2969 qemu_add_opts(&qemu_name_opts);
2970 qemu_add_opts(&qemu_numa_opts);
2972 runstate_init();
2974 rtc_clock = QEMU_CLOCK_HOST;
2976 QLIST_INIT (&vm_change_state_head);
2977 os_setup_early_signal_handling();
2979 module_call_init(MODULE_INIT_MACHINE);
2980 machine_class = find_default_machine();
2981 cpu_model = NULL;
2982 ram_size = default_ram_size;
2983 snapshot = 0;
2984 cyls = heads = secs = 0;
2985 translation = BIOS_ATA_TRANSLATION_AUTO;
2987 for (i = 0; i < MAX_NODES; i++) {
2988 numa_info[i].node_mem = 0;
2989 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
2992 nb_numa_nodes = 0;
2993 nb_nics = 0;
2995 bdrv_init_with_whitelist();
2997 autostart = 1;
2999 /* first pass of option parsing */
3000 optind = 1;
3001 while (optind < argc) {
3002 if (argv[optind][0] != '-') {
3003 /* disk image */
3004 optind++;
3005 } else {
3006 const QEMUOption *popt;
3008 popt = lookup_opt(argc, argv, &optarg, &optind);
3009 switch (popt->index) {
3010 case QEMU_OPTION_nodefconfig:
3011 defconfig = false;
3012 break;
3013 case QEMU_OPTION_nouserconfig:
3014 userconfig = false;
3015 break;
3020 if (defconfig) {
3021 int ret;
3022 ret = qemu_read_default_config_files(userconfig);
3023 if (ret < 0) {
3024 exit(1);
3028 /* second pass of option parsing */
3029 optind = 1;
3030 for(;;) {
3031 if (optind >= argc)
3032 break;
3033 if (argv[optind][0] != '-') {
3034 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3035 } else {
3036 const QEMUOption *popt;
3038 popt = lookup_opt(argc, argv, &optarg, &optind);
3039 if (!(popt->arch_mask & arch_type)) {
3040 printf("Option %s not supported for this target\n", popt->name);
3041 exit(1);
3043 switch(popt->index) {
3044 case QEMU_OPTION_M:
3045 machine_class = machine_parse(optarg);
3046 break;
3047 case QEMU_OPTION_no_kvm_irqchip: {
3048 olist = qemu_find_opts("machine");
3049 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3050 break;
3052 case QEMU_OPTION_cpu:
3053 /* hw initialization will check this */
3054 cpu_model = optarg;
3055 break;
3056 case QEMU_OPTION_hda:
3058 char buf[256];
3059 if (cyls == 0)
3060 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3061 else
3062 snprintf(buf, sizeof(buf),
3063 "%s,cyls=%d,heads=%d,secs=%d%s",
3064 HD_OPTS , cyls, heads, secs,
3065 translation == BIOS_ATA_TRANSLATION_LBA ?
3066 ",trans=lba" :
3067 translation == BIOS_ATA_TRANSLATION_NONE ?
3068 ",trans=none" : "");
3069 drive_add(IF_DEFAULT, 0, optarg, buf);
3070 break;
3072 case QEMU_OPTION_hdb:
3073 case QEMU_OPTION_hdc:
3074 case QEMU_OPTION_hdd:
3075 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3076 HD_OPTS);
3077 break;
3078 case QEMU_OPTION_drive:
3079 if (drive_def(optarg) == NULL) {
3080 exit(1);
3082 break;
3083 case QEMU_OPTION_set:
3084 if (qemu_set_option(optarg) != 0)
3085 exit(1);
3086 break;
3087 case QEMU_OPTION_global:
3088 if (qemu_global_option(optarg) != 0)
3089 exit(1);
3090 break;
3091 case QEMU_OPTION_mtdblock:
3092 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3093 break;
3094 case QEMU_OPTION_sd:
3095 drive_add(IF_SD, -1, optarg, SD_OPTS);
3096 break;
3097 case QEMU_OPTION_pflash:
3098 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3099 break;
3100 case QEMU_OPTION_snapshot:
3101 snapshot = 1;
3102 break;
3103 case QEMU_OPTION_hdachs:
3105 const char *p;
3106 p = optarg;
3107 cyls = strtol(p, (char **)&p, 0);
3108 if (cyls < 1 || cyls > 16383)
3109 goto chs_fail;
3110 if (*p != ',')
3111 goto chs_fail;
3112 p++;
3113 heads = strtol(p, (char **)&p, 0);
3114 if (heads < 1 || heads > 16)
3115 goto chs_fail;
3116 if (*p != ',')
3117 goto chs_fail;
3118 p++;
3119 secs = strtol(p, (char **)&p, 0);
3120 if (secs < 1 || secs > 63)
3121 goto chs_fail;
3122 if (*p == ',') {
3123 p++;
3124 if (!strcmp(p, "large")) {
3125 translation = BIOS_ATA_TRANSLATION_LARGE;
3126 } else if (!strcmp(p, "rechs")) {
3127 translation = BIOS_ATA_TRANSLATION_RECHS;
3128 } else if (!strcmp(p, "none")) {
3129 translation = BIOS_ATA_TRANSLATION_NONE;
3130 } else if (!strcmp(p, "lba")) {
3131 translation = BIOS_ATA_TRANSLATION_LBA;
3132 } else if (!strcmp(p, "auto")) {
3133 translation = BIOS_ATA_TRANSLATION_AUTO;
3134 } else {
3135 goto chs_fail;
3137 } else if (*p != '\0') {
3138 chs_fail:
3139 fprintf(stderr, "qemu: invalid physical CHS format\n");
3140 exit(1);
3142 if (hda_opts != NULL) {
3143 char num[16];
3144 snprintf(num, sizeof(num), "%d", cyls);
3145 qemu_opt_set(hda_opts, "cyls", num);
3146 snprintf(num, sizeof(num), "%d", heads);
3147 qemu_opt_set(hda_opts, "heads", num);
3148 snprintf(num, sizeof(num), "%d", secs);
3149 qemu_opt_set(hda_opts, "secs", num);
3150 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3151 qemu_opt_set(hda_opts, "trans", "large");
3152 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3153 qemu_opt_set(hda_opts, "trans", "rechs");
3154 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3155 qemu_opt_set(hda_opts, "trans", "lba");
3156 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3157 qemu_opt_set(hda_opts, "trans", "none");
3161 break;
3162 case QEMU_OPTION_numa:
3163 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3164 if (!opts) {
3165 exit(1);
3167 break;
3168 case QEMU_OPTION_display:
3169 display_type = select_display(optarg);
3170 break;
3171 case QEMU_OPTION_nographic:
3172 display_type = DT_NOGRAPHIC;
3173 break;
3174 case QEMU_OPTION_curses:
3175 #ifdef CONFIG_CURSES
3176 display_type = DT_CURSES;
3177 #else
3178 fprintf(stderr, "Curses support is disabled\n");
3179 exit(1);
3180 #endif
3181 break;
3182 case QEMU_OPTION_portrait:
3183 graphic_rotate = 90;
3184 break;
3185 case QEMU_OPTION_rotate:
3186 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3187 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3188 graphic_rotate != 180 && graphic_rotate != 270) {
3189 fprintf(stderr,
3190 "qemu: only 90, 180, 270 deg rotation is available\n");
3191 exit(1);
3193 break;
3194 case QEMU_OPTION_kernel:
3195 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3196 break;
3197 case QEMU_OPTION_initrd:
3198 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3199 break;
3200 case QEMU_OPTION_append:
3201 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3202 break;
3203 case QEMU_OPTION_dtb:
3204 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3205 break;
3206 case QEMU_OPTION_cdrom:
3207 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3208 break;
3209 case QEMU_OPTION_boot:
3210 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3211 if (!opts) {
3212 exit(1);
3214 break;
3215 case QEMU_OPTION_fda:
3216 case QEMU_OPTION_fdb:
3217 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3218 optarg, FD_OPTS);
3219 break;
3220 case QEMU_OPTION_no_fd_bootchk:
3221 fd_bootchk = 0;
3222 break;
3223 case QEMU_OPTION_netdev:
3224 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3225 exit(1);
3227 break;
3228 case QEMU_OPTION_net:
3229 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3230 exit(1);
3232 break;
3233 #ifdef CONFIG_LIBISCSI
3234 case QEMU_OPTION_iscsi:
3235 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3236 if (!opts) {
3237 exit(1);
3239 break;
3240 #endif
3241 #ifdef CONFIG_SLIRP
3242 case QEMU_OPTION_tftp:
3243 legacy_tftp_prefix = optarg;
3244 break;
3245 case QEMU_OPTION_bootp:
3246 legacy_bootp_filename = optarg;
3247 break;
3248 case QEMU_OPTION_redir:
3249 if (net_slirp_redir(optarg) < 0)
3250 exit(1);
3251 break;
3252 #endif
3253 case QEMU_OPTION_bt:
3254 add_device_config(DEV_BT, optarg);
3255 break;
3256 case QEMU_OPTION_audio_help:
3257 AUD_help ();
3258 exit (0);
3259 break;
3260 case QEMU_OPTION_soundhw:
3261 select_soundhw (optarg);
3262 break;
3263 case QEMU_OPTION_h:
3264 help(0);
3265 break;
3266 case QEMU_OPTION_version:
3267 version();
3268 exit(0);
3269 break;
3270 case QEMU_OPTION_m: {
3271 uint64_t sz;
3272 const char *mem_str;
3273 const char *maxmem_str, *slots_str;
3275 opts = qemu_opts_parse(qemu_find_opts("memory"),
3276 optarg, 1);
3277 if (!opts) {
3278 exit(EXIT_FAILURE);
3281 mem_str = qemu_opt_get(opts, "size");
3282 if (!mem_str) {
3283 error_report("invalid -m option, missing 'size' option");
3284 exit(EXIT_FAILURE);
3286 if (!*mem_str) {
3287 error_report("missing 'size' option value");
3288 exit(EXIT_FAILURE);
3291 sz = qemu_opt_get_size(opts, "size", ram_size);
3293 /* Fix up legacy suffix-less format */
3294 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3295 uint64_t overflow_check = sz;
3297 sz <<= 20;
3298 if ((sz >> 20) != overflow_check) {
3299 error_report("too large 'size' option value");
3300 exit(EXIT_FAILURE);
3304 /* backward compatibility behaviour for case "-m 0" */
3305 if (sz == 0) {
3306 sz = default_ram_size;
3309 sz = QEMU_ALIGN_UP(sz, 8192);
3310 ram_size = sz;
3311 if (ram_size != sz) {
3312 error_report("ram size too large");
3313 exit(EXIT_FAILURE);
3316 maxmem_str = qemu_opt_get(opts, "maxmem");
3317 slots_str = qemu_opt_get(opts, "slots");
3318 if (maxmem_str && slots_str) {
3319 uint64_t slots;
3321 sz = qemu_opt_get_size(opts, "maxmem", 0);
3322 if (sz < ram_size) {
3323 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3324 "(%" PRIu64 ") <= initial memory ("
3325 RAM_ADDR_FMT ")\n", sz, ram_size);
3326 exit(EXIT_FAILURE);
3329 slots = qemu_opt_get_number(opts, "slots", 0);
3330 if ((sz > ram_size) && !slots) {
3331 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3332 "(%" PRIu64 ") more than initial memory ("
3333 RAM_ADDR_FMT ") but no hotplug slots where "
3334 "specified\n", sz, ram_size);
3335 exit(EXIT_FAILURE);
3338 if ((sz <= ram_size) && slots) {
3339 fprintf(stderr, "qemu: invalid -m option value: %"
3340 PRIu64 " hotplug slots where specified but "
3341 "maxmem (%" PRIu64 ") <= initial memory ("
3342 RAM_ADDR_FMT ")\n", slots, sz, ram_size);
3343 exit(EXIT_FAILURE);
3345 maxram_size = sz;
3346 ram_slots = slots;
3347 } else if ((!maxmem_str && slots_str) ||
3348 (maxmem_str && !slots_str)) {
3349 fprintf(stderr, "qemu: invalid -m option value: missing "
3350 "'%s' option\n", slots_str ? "maxmem" : "slots");
3351 exit(EXIT_FAILURE);
3353 break;
3355 #ifdef CONFIG_TPM
3356 case QEMU_OPTION_tpmdev:
3357 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3358 exit(1);
3360 break;
3361 #endif
3362 case QEMU_OPTION_mempath:
3363 mem_path = optarg;
3364 break;
3365 case QEMU_OPTION_mem_prealloc:
3366 mem_prealloc = 1;
3367 break;
3368 case QEMU_OPTION_d:
3369 log_mask = optarg;
3370 break;
3371 case QEMU_OPTION_D:
3372 log_file = optarg;
3373 break;
3374 case QEMU_OPTION_s:
3375 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3376 break;
3377 case QEMU_OPTION_gdb:
3378 add_device_config(DEV_GDB, optarg);
3379 break;
3380 case QEMU_OPTION_L:
3381 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3382 data_dir[data_dir_idx++] = optarg;
3384 break;
3385 case QEMU_OPTION_bios:
3386 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3387 break;
3388 case QEMU_OPTION_singlestep:
3389 singlestep = 1;
3390 break;
3391 case QEMU_OPTION_S:
3392 autostart = 0;
3393 break;
3394 case QEMU_OPTION_k:
3395 keyboard_layout = optarg;
3396 break;
3397 case QEMU_OPTION_localtime:
3398 rtc_utc = 0;
3399 break;
3400 case QEMU_OPTION_vga:
3401 vga_model = optarg;
3402 default_vga = 0;
3403 break;
3404 case QEMU_OPTION_g:
3406 const char *p;
3407 int w, h, depth;
3408 p = optarg;
3409 w = strtol(p, (char **)&p, 10);
3410 if (w <= 0) {
3411 graphic_error:
3412 fprintf(stderr, "qemu: invalid resolution or depth\n");
3413 exit(1);
3415 if (*p != 'x')
3416 goto graphic_error;
3417 p++;
3418 h = strtol(p, (char **)&p, 10);
3419 if (h <= 0)
3420 goto graphic_error;
3421 if (*p == 'x') {
3422 p++;
3423 depth = strtol(p, (char **)&p, 10);
3424 if (depth != 8 && depth != 15 && depth != 16 &&
3425 depth != 24 && depth != 32)
3426 goto graphic_error;
3427 } else if (*p == '\0') {
3428 depth = graphic_depth;
3429 } else {
3430 goto graphic_error;
3433 graphic_width = w;
3434 graphic_height = h;
3435 graphic_depth = depth;
3437 break;
3438 case QEMU_OPTION_echr:
3440 char *r;
3441 term_escape_char = strtol(optarg, &r, 0);
3442 if (r == optarg)
3443 printf("Bad argument to echr\n");
3444 break;
3446 case QEMU_OPTION_monitor:
3447 default_monitor = 0;
3448 if (strncmp(optarg, "none", 4)) {
3449 monitor_parse(optarg, "readline");
3451 break;
3452 case QEMU_OPTION_qmp:
3453 monitor_parse(optarg, "control");
3454 default_monitor = 0;
3455 break;
3456 case QEMU_OPTION_mon:
3457 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3458 if (!opts) {
3459 exit(1);
3461 default_monitor = 0;
3462 break;
3463 case QEMU_OPTION_chardev:
3464 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3465 if (!opts) {
3466 exit(1);
3468 break;
3469 case QEMU_OPTION_fsdev:
3470 olist = qemu_find_opts("fsdev");
3471 if (!olist) {
3472 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3473 exit(1);
3475 opts = qemu_opts_parse(olist, optarg, 1);
3476 if (!opts) {
3477 exit(1);
3479 break;
3480 case QEMU_OPTION_virtfs: {
3481 QemuOpts *fsdev;
3482 QemuOpts *device;
3483 const char *writeout, *sock_fd, *socket;
3485 olist = qemu_find_opts("virtfs");
3486 if (!olist) {
3487 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3488 exit(1);
3490 opts = qemu_opts_parse(olist, optarg, 1);
3491 if (!opts) {
3492 exit(1);
3495 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3496 qemu_opt_get(opts, "mount_tag") == NULL) {
3497 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3498 exit(1);
3500 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3501 qemu_opt_get(opts, "mount_tag"),
3502 1, NULL);
3503 if (!fsdev) {
3504 fprintf(stderr, "duplicate fsdev id: %s\n",
3505 qemu_opt_get(opts, "mount_tag"));
3506 exit(1);
3509 writeout = qemu_opt_get(opts, "writeout");
3510 if (writeout) {
3511 #ifdef CONFIG_SYNC_FILE_RANGE
3512 qemu_opt_set(fsdev, "writeout", writeout);
3513 #else
3514 fprintf(stderr, "writeout=immediate not supported on "
3515 "this platform\n");
3516 exit(1);
3517 #endif
3519 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3520 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3521 qemu_opt_set(fsdev, "security_model",
3522 qemu_opt_get(opts, "security_model"));
3523 socket = qemu_opt_get(opts, "socket");
3524 if (socket) {
3525 qemu_opt_set(fsdev, "socket", socket);
3527 sock_fd = qemu_opt_get(opts, "sock_fd");
3528 if (sock_fd) {
3529 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3532 qemu_opt_set_bool(fsdev, "readonly",
3533 qemu_opt_get_bool(opts, "readonly", 0));
3534 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3535 &error_abort);
3536 qemu_opt_set(device, "driver", "virtio-9p-pci");
3537 qemu_opt_set(device, "fsdev",
3538 qemu_opt_get(opts, "mount_tag"));
3539 qemu_opt_set(device, "mount_tag",
3540 qemu_opt_get(opts, "mount_tag"));
3541 break;
3543 case QEMU_OPTION_virtfs_synth: {
3544 QemuOpts *fsdev;
3545 QemuOpts *device;
3547 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3548 1, NULL);
3549 if (!fsdev) {
3550 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3551 exit(1);
3553 qemu_opt_set(fsdev, "fsdriver", "synth");
3555 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3556 &error_abort);
3557 qemu_opt_set(device, "driver", "virtio-9p-pci");
3558 qemu_opt_set(device, "fsdev", "v_synth");
3559 qemu_opt_set(device, "mount_tag", "v_synth");
3560 break;
3562 case QEMU_OPTION_serial:
3563 add_device_config(DEV_SERIAL, optarg);
3564 default_serial = 0;
3565 if (strncmp(optarg, "mon:", 4) == 0) {
3566 default_monitor = 0;
3568 break;
3569 case QEMU_OPTION_watchdog:
3570 if (watchdog) {
3571 fprintf(stderr,
3572 "qemu: only one watchdog option may be given\n");
3573 return 1;
3575 watchdog = optarg;
3576 break;
3577 case QEMU_OPTION_watchdog_action:
3578 if (select_watchdog_action(optarg) == -1) {
3579 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3580 exit(1);
3582 break;
3583 case QEMU_OPTION_virtiocon:
3584 add_device_config(DEV_VIRTCON, optarg);
3585 default_virtcon = 0;
3586 if (strncmp(optarg, "mon:", 4) == 0) {
3587 default_monitor = 0;
3589 break;
3590 case QEMU_OPTION_parallel:
3591 add_device_config(DEV_PARALLEL, optarg);
3592 default_parallel = 0;
3593 if (strncmp(optarg, "mon:", 4) == 0) {
3594 default_monitor = 0;
3596 break;
3597 case QEMU_OPTION_debugcon:
3598 add_device_config(DEV_DEBUGCON, optarg);
3599 break;
3600 case QEMU_OPTION_loadvm:
3601 loadvm = optarg;
3602 break;
3603 case QEMU_OPTION_full_screen:
3604 full_screen = 1;
3605 break;
3606 case QEMU_OPTION_no_frame:
3607 no_frame = 1;
3608 break;
3609 case QEMU_OPTION_alt_grab:
3610 alt_grab = 1;
3611 break;
3612 case QEMU_OPTION_ctrl_grab:
3613 ctrl_grab = 1;
3614 break;
3615 case QEMU_OPTION_no_quit:
3616 no_quit = 1;
3617 break;
3618 case QEMU_OPTION_sdl:
3619 #ifdef CONFIG_SDL
3620 display_type = DT_SDL;
3621 break;
3622 #else
3623 fprintf(stderr, "SDL support is disabled\n");
3624 exit(1);
3625 #endif
3626 case QEMU_OPTION_pidfile:
3627 pid_file = optarg;
3628 break;
3629 case QEMU_OPTION_win2k_hack:
3630 win2k_install_hack = 1;
3631 break;
3632 case QEMU_OPTION_rtc_td_hack: {
3633 static GlobalProperty slew_lost_ticks[] = {
3635 .driver = "mc146818rtc",
3636 .property = "lost_tick_policy",
3637 .value = "slew",
3639 { /* end of list */ }
3642 qdev_prop_register_global_list(slew_lost_ticks);
3643 break;
3645 case QEMU_OPTION_acpitable:
3646 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3647 if (!opts) {
3648 exit(1);
3650 do_acpitable_option(opts);
3651 break;
3652 case QEMU_OPTION_smbios:
3653 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3654 if (!opts) {
3655 exit(1);
3657 do_smbios_option(opts);
3658 break;
3659 case QEMU_OPTION_enable_kvm:
3660 olist = qemu_find_opts("machine");
3661 qemu_opts_parse(olist, "accel=kvm", 0);
3662 break;
3663 case QEMU_OPTION_machine:
3664 olist = qemu_find_opts("machine");
3665 opts = qemu_opts_parse(olist, optarg, 1);
3666 if (!opts) {
3667 exit(1);
3669 optarg = qemu_opt_get(opts, "type");
3670 if (optarg) {
3671 machine_class = machine_parse(optarg);
3673 break;
3674 case QEMU_OPTION_no_kvm:
3675 olist = qemu_find_opts("machine");
3676 qemu_opts_parse(olist, "accel=tcg", 0);
3677 break;
3678 case QEMU_OPTION_no_kvm_pit: {
3679 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3680 "separately.\n");
3681 break;
3683 case QEMU_OPTION_no_kvm_pit_reinjection: {
3684 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3686 .driver = "kvm-pit",
3687 .property = "lost_tick_policy",
3688 .value = "discard",
3690 { /* end of list */ }
3693 fprintf(stderr, "Warning: option deprecated, use "
3694 "lost_tick_policy property of kvm-pit instead.\n");
3695 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3696 break;
3698 case QEMU_OPTION_usb:
3699 olist = qemu_find_opts("machine");
3700 qemu_opts_parse(olist, "usb=on", 0);
3701 break;
3702 case QEMU_OPTION_usbdevice:
3703 olist = qemu_find_opts("machine");
3704 qemu_opts_parse(olist, "usb=on", 0);
3705 add_device_config(DEV_USB, optarg);
3706 break;
3707 case QEMU_OPTION_device:
3708 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3709 exit(1);
3711 break;
3712 case QEMU_OPTION_smp:
3713 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3714 exit(1);
3716 break;
3717 case QEMU_OPTION_vnc:
3718 #ifdef CONFIG_VNC
3719 display_remote++;
3720 vnc_display = optarg;
3721 #else
3722 fprintf(stderr, "VNC support is disabled\n");
3723 exit(1);
3724 #endif
3725 break;
3726 case QEMU_OPTION_no_acpi:
3727 acpi_enabled = 0;
3728 break;
3729 case QEMU_OPTION_no_hpet:
3730 no_hpet = 1;
3731 break;
3732 case QEMU_OPTION_balloon:
3733 if (balloon_parse(optarg) < 0) {
3734 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3735 exit(1);
3737 break;
3738 case QEMU_OPTION_no_reboot:
3739 no_reboot = 1;
3740 break;
3741 case QEMU_OPTION_no_shutdown:
3742 no_shutdown = 1;
3743 break;
3744 case QEMU_OPTION_show_cursor:
3745 cursor_hide = 0;
3746 break;
3747 case QEMU_OPTION_uuid:
3748 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3749 fprintf(stderr, "Fail to parse UUID string."
3750 " Wrong format.\n");
3751 exit(1);
3753 qemu_uuid_set = true;
3754 break;
3755 case QEMU_OPTION_option_rom:
3756 if (nb_option_roms >= MAX_OPTION_ROMS) {
3757 fprintf(stderr, "Too many option ROMs\n");
3758 exit(1);
3760 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3761 if (!opts) {
3762 exit(1);
3764 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3765 option_rom[nb_option_roms].bootindex =
3766 qemu_opt_get_number(opts, "bootindex", -1);
3767 if (!option_rom[nb_option_roms].name) {
3768 fprintf(stderr, "Option ROM file is not specified\n");
3769 exit(1);
3771 nb_option_roms++;
3772 break;
3773 case QEMU_OPTION_semihosting:
3774 semihosting_enabled = 1;
3775 break;
3776 case QEMU_OPTION_tdf:
3777 fprintf(stderr, "Warning: user space PIT time drift fix "
3778 "is no longer supported.\n");
3779 break;
3780 case QEMU_OPTION_name:
3781 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3782 if (!opts) {
3783 exit(1);
3785 break;
3786 case QEMU_OPTION_prom_env:
3787 if (nb_prom_envs >= MAX_PROM_ENVS) {
3788 fprintf(stderr, "Too many prom variables\n");
3789 exit(1);
3791 prom_envs[nb_prom_envs] = optarg;
3792 nb_prom_envs++;
3793 break;
3794 case QEMU_OPTION_old_param:
3795 old_param = 1;
3796 break;
3797 case QEMU_OPTION_clock:
3798 /* Clock options no longer exist. Keep this option for
3799 * backward compatibility.
3801 break;
3802 case QEMU_OPTION_startdate:
3803 configure_rtc_date_offset(optarg, 1);
3804 break;
3805 case QEMU_OPTION_rtc:
3806 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3807 if (!opts) {
3808 exit(1);
3810 configure_rtc(opts);
3811 break;
3812 case QEMU_OPTION_tb_size:
3813 tcg_tb_size = strtol(optarg, NULL, 0);
3814 if (tcg_tb_size < 0) {
3815 tcg_tb_size = 0;
3817 break;
3818 case QEMU_OPTION_icount:
3819 icount_option = optarg;
3820 break;
3821 case QEMU_OPTION_incoming:
3822 incoming = optarg;
3823 runstate_set(RUN_STATE_INMIGRATE);
3824 break;
3825 case QEMU_OPTION_nodefaults:
3826 has_defaults = 0;
3827 break;
3828 case QEMU_OPTION_xen_domid:
3829 if (!(xen_available())) {
3830 printf("Option %s not supported for this target\n", popt->name);
3831 exit(1);
3833 xen_domid = atoi(optarg);
3834 break;
3835 case QEMU_OPTION_xen_create:
3836 if (!(xen_available())) {
3837 printf("Option %s not supported for this target\n", popt->name);
3838 exit(1);
3840 xen_mode = XEN_CREATE;
3841 break;
3842 case QEMU_OPTION_xen_attach:
3843 if (!(xen_available())) {
3844 printf("Option %s not supported for this target\n", popt->name);
3845 exit(1);
3847 xen_mode = XEN_ATTACH;
3848 break;
3849 case QEMU_OPTION_trace:
3851 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3852 if (!opts) {
3853 exit(1);
3855 trace_events = qemu_opt_get(opts, "events");
3856 trace_file = qemu_opt_get(opts, "file");
3857 break;
3859 case QEMU_OPTION_readconfig:
3861 int ret = qemu_read_config_file(optarg);
3862 if (ret < 0) {
3863 fprintf(stderr, "read config %s: %s\n", optarg,
3864 strerror(-ret));
3865 exit(1);
3867 break;
3869 case QEMU_OPTION_spice:
3870 olist = qemu_find_opts("spice");
3871 if (!olist) {
3872 fprintf(stderr, "spice is not supported by this qemu build.\n");
3873 exit(1);
3875 opts = qemu_opts_parse(olist, optarg, 0);
3876 if (!opts) {
3877 exit(1);
3879 display_remote++;
3880 break;
3881 case QEMU_OPTION_writeconfig:
3883 FILE *fp;
3884 if (strcmp(optarg, "-") == 0) {
3885 fp = stdout;
3886 } else {
3887 fp = fopen(optarg, "w");
3888 if (fp == NULL) {
3889 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3890 exit(1);
3893 qemu_config_write(fp);
3894 if (fp != stdout) {
3895 fclose(fp);
3897 break;
3899 case QEMU_OPTION_qtest:
3900 qtest_chrdev = optarg;
3901 break;
3902 case QEMU_OPTION_qtest_log:
3903 qtest_log = optarg;
3904 break;
3905 case QEMU_OPTION_sandbox:
3906 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3907 if (!opts) {
3908 exit(1);
3910 break;
3911 case QEMU_OPTION_add_fd:
3912 #ifndef _WIN32
3913 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3914 if (!opts) {
3915 exit(1);
3917 #else
3918 error_report("File descriptor passing is disabled on this "
3919 "platform");
3920 exit(1);
3921 #endif
3922 break;
3923 case QEMU_OPTION_object:
3924 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3925 if (!opts) {
3926 exit(1);
3928 break;
3929 case QEMU_OPTION_realtime:
3930 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3931 if (!opts) {
3932 exit(1);
3934 configure_realtime(opts);
3935 break;
3936 case QEMU_OPTION_msg:
3937 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3938 if (!opts) {
3939 exit(1);
3941 configure_msg(opts);
3942 break;
3943 default:
3944 os_parse_cmd_args(popt->index, optarg);
3948 loc_set_none();
3950 os_daemonize();
3952 if (qemu_init_main_loop()) {
3953 fprintf(stderr, "qemu_init_main_loop failed\n");
3954 exit(1);
3957 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3958 exit(1);
3961 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
3962 exit(1);
3965 #ifndef _WIN32
3966 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3967 exit(1);
3970 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3971 exit(1);
3973 #endif
3975 if (machine_class == NULL) {
3976 fprintf(stderr, "No machine specified, and there is no default.\n"
3977 "Use -machine help to list supported machines!\n");
3978 exit(1);
3981 cpu_exec_init_all();
3983 current_machine = MACHINE(object_new(object_class_get_name(
3984 OBJECT_CLASS(machine_class))));
3985 object_property_add_child(object_get_root(), "machine",
3986 OBJECT(current_machine), &error_abort);
3988 if (machine_class->hw_version) {
3989 qemu_set_version(machine_class->hw_version);
3992 if (qemu_opts_foreach(qemu_find_opts("object"),
3993 object_create, NULL, 0) != 0) {
3994 exit(1);
3997 /* Init CPU def lists, based on config
3998 * - Must be called after all the qemu_read_config_file() calls
3999 * - Must be called before list_cpus()
4000 * - Must be called before machine->init()
4002 cpudef_init();
4004 if (cpu_model && is_help_option(cpu_model)) {
4005 list_cpus(stdout, &fprintf, cpu_model);
4006 exit(0);
4009 /* Open the logfile at this point, if necessary. We can't open the logfile
4010 * when encountering either of the logging options (-d or -D) because the
4011 * other one may be encountered later on the command line, changing the
4012 * location or level of logging.
4014 if (log_mask) {
4015 int mask;
4016 if (log_file) {
4017 qemu_set_log_filename(log_file);
4020 mask = qemu_str_to_log_mask(log_mask);
4021 if (!mask) {
4022 qemu_print_log_usage(stdout);
4023 exit(1);
4025 qemu_set_log(mask);
4028 if (!is_daemonized()) {
4029 if (!trace_init_backends(trace_events, trace_file)) {
4030 exit(1);
4034 /* If no data_dir is specified then try to find it relative to the
4035 executable path. */
4036 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4037 data_dir[data_dir_idx] = os_find_datadir();
4038 if (data_dir[data_dir_idx] != NULL) {
4039 data_dir_idx++;
4042 /* If all else fails use the install path specified when building. */
4043 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4044 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4047 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4049 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4050 if (smp_cpus > machine_class->max_cpus) {
4051 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4052 "supported by machine `%s' (%d)\n", smp_cpus,
4053 machine_class->name, machine_class->max_cpus);
4054 exit(1);
4058 * Get the default machine options from the machine if it is not already
4059 * specified either by the configuration file or by the command line.
4061 if (machine_class->default_machine_opts) {
4062 qemu_opts_set_defaults(qemu_find_opts("machine"),
4063 machine_class->default_machine_opts, 0);
4066 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4067 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4069 if (!vga_model && !default_vga) {
4070 vga_interface_type = VGA_DEVICE;
4072 if (!has_defaults || machine_class->no_serial) {
4073 default_serial = 0;
4075 if (!has_defaults || machine_class->no_parallel) {
4076 default_parallel = 0;
4078 if (!has_defaults || !machine_class->use_virtcon) {
4079 default_virtcon = 0;
4081 if (!has_defaults || !machine_class->use_sclp) {
4082 default_sclp = 0;
4084 if (!has_defaults || machine_class->no_floppy) {
4085 default_floppy = 0;
4087 if (!has_defaults || machine_class->no_cdrom) {
4088 default_cdrom = 0;
4090 if (!has_defaults || machine_class->no_sdcard) {
4091 default_sdcard = 0;
4093 if (!has_defaults) {
4094 default_monitor = 0;
4095 default_net = 0;
4096 default_vga = 0;
4099 if (is_daemonized()) {
4100 /* According to documentation and historically, -nographic redirects
4101 * serial port, parallel port and monitor to stdio, which does not work
4102 * with -daemonize. We can redirect these to null instead, but since
4103 * -nographic is legacy, let's just error out.
4104 * We disallow -nographic only if all other ports are not redirected
4105 * explicitly, to not break existing legacy setups which uses
4106 * -nographic _and_ redirects all ports explicitly - this is valid
4107 * usage, -nographic is just a no-op in this case.
4109 if (display_type == DT_NOGRAPHIC
4110 && (default_parallel || default_serial
4111 || default_monitor || default_virtcon)) {
4112 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4113 exit(1);
4115 #ifdef CONFIG_CURSES
4116 if (display_type == DT_CURSES) {
4117 fprintf(stderr, "curses display can not be used with -daemonize\n");
4118 exit(1);
4120 #endif
4123 if (display_type == DT_NOGRAPHIC) {
4124 if (default_parallel)
4125 add_device_config(DEV_PARALLEL, "null");
4126 if (default_serial && default_monitor) {
4127 add_device_config(DEV_SERIAL, "mon:stdio");
4128 } else if (default_virtcon && default_monitor) {
4129 add_device_config(DEV_VIRTCON, "mon:stdio");
4130 } else if (default_sclp && default_monitor) {
4131 add_device_config(DEV_SCLP, "mon:stdio");
4132 } else {
4133 if (default_serial)
4134 add_device_config(DEV_SERIAL, "stdio");
4135 if (default_virtcon)
4136 add_device_config(DEV_VIRTCON, "stdio");
4137 if (default_sclp) {
4138 add_device_config(DEV_SCLP, "stdio");
4140 if (default_monitor)
4141 monitor_parse("stdio", "readline");
4143 } else {
4144 if (default_serial)
4145 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4146 if (default_parallel)
4147 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4148 if (default_monitor)
4149 monitor_parse("vc:80Cx24C", "readline");
4150 if (default_virtcon)
4151 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4152 if (default_sclp) {
4153 add_device_config(DEV_SCLP, "vc:80Cx24C");
4157 if (display_type == DT_DEFAULT && !display_remote) {
4158 #if defined(CONFIG_GTK)
4159 display_type = DT_GTK;
4160 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4161 display_type = DT_SDL;
4162 #elif defined(CONFIG_VNC)
4163 vnc_display = "localhost:0,to=99";
4164 show_vnc_port = 1;
4165 #else
4166 display_type = DT_NONE;
4167 #endif
4170 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4171 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4172 "for SDL, ignoring option\n");
4174 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4175 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4176 "ignoring option\n");
4179 #if defined(CONFIG_GTK)
4180 if (display_type == DT_GTK) {
4181 early_gtk_display_init();
4183 #endif
4185 socket_init();
4187 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4188 exit(1);
4189 #ifdef CONFIG_VIRTFS
4190 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4191 exit(1);
4193 #endif
4195 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4196 os_pidfile_error();
4197 exit(1);
4200 /* store value for the future use */
4201 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4203 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4204 != 0) {
4205 exit(0);
4208 machine_opts = qemu_get_machine_opts();
4209 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4210 1) < 0) {
4211 object_unref(OBJECT(current_machine));
4212 exit(1);
4215 configure_accelerator(machine_class);
4217 if (qtest_chrdev) {
4218 Error *local_err = NULL;
4219 qtest_init(qtest_chrdev, qtest_log, &local_err);
4220 if (local_err) {
4221 error_report("%s", error_get_pretty(local_err));
4222 error_free(local_err);
4223 exit(1);
4227 machine_opts = qemu_get_machine_opts();
4228 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4229 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4230 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4231 bios_name = qemu_opt_get(machine_opts, "firmware");
4233 boot_order = machine_class->default_boot_order;
4234 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4235 if (opts) {
4236 char *normal_boot_order;
4237 const char *order, *once;
4239 order = qemu_opt_get(opts, "order");
4240 if (order) {
4241 validate_bootdevices(order);
4242 boot_order = order;
4245 once = qemu_opt_get(opts, "once");
4246 if (once) {
4247 validate_bootdevices(once);
4248 normal_boot_order = g_strdup(boot_order);
4249 boot_order = once;
4250 qemu_register_reset(restore_boot_order, normal_boot_order);
4253 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4254 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4257 if (!kernel_cmdline) {
4258 kernel_cmdline = "";
4259 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4262 linux_boot = (kernel_filename != NULL);
4264 if (!linux_boot && *kernel_cmdline != '\0') {
4265 fprintf(stderr, "-append only allowed with -kernel option\n");
4266 exit(1);
4269 if (!linux_boot && initrd_filename != NULL) {
4270 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4271 exit(1);
4274 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4275 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4276 exit(1);
4279 os_set_line_buffering();
4281 qemu_init_cpu_loop();
4282 qemu_mutex_lock_iothread();
4284 #ifdef CONFIG_SPICE
4285 /* spice needs the timers to be initialized by this point */
4286 qemu_spice_init();
4287 #endif
4289 if (icount_option && (kvm_enabled() || xen_enabled())) {
4290 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4291 exit(1);
4293 configure_icount(icount_option);
4295 /* clean up network at qemu process termination */
4296 atexit(&net_cleanup);
4298 if (net_init_clients() < 0) {
4299 exit(1);
4302 #ifdef CONFIG_TPM
4303 if (tpm_init() < 0) {
4304 exit(1);
4306 #endif
4308 /* init the bluetooth world */
4309 if (foreach_device_config(DEV_BT, bt_parse))
4310 exit(1);
4312 if (!xen_enabled()) {
4313 /* On 32-bit hosts, QEMU is limited by virtual address space */
4314 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4315 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4316 exit(1);
4320 blk_mig_init();
4321 ram_mig_init();
4323 /* open the virtual block devices */
4324 if (snapshot)
4325 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4326 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4327 &machine_class->block_default_type, 1) != 0) {
4328 exit(1);
4331 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4332 CDROM_OPTS);
4333 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4334 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4336 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4337 NULL, 1) != 0) {
4338 exit(1);
4341 set_numa_nodes();
4343 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4344 exit(1);
4347 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4348 exit(1);
4349 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4350 exit(1);
4351 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4352 exit(1);
4353 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4354 exit(1);
4356 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4357 exit(1);
4359 /* If no default VGA is requested, the default is "none". */
4360 if (default_vga) {
4361 if (cirrus_vga_available()) {
4362 vga_model = "cirrus";
4363 } else if (vga_available()) {
4364 vga_model = "std";
4367 if (vga_model) {
4368 select_vgahw(vga_model);
4371 if (watchdog) {
4372 i = select_watchdog(watchdog);
4373 if (i > 0)
4374 exit (i == 1 ? 1 : 0);
4377 if (machine_class->compat_props) {
4378 qdev_prop_register_global_list(machine_class->compat_props);
4380 qemu_add_globals();
4382 qdev_machine_init();
4384 current_machine->ram_size = ram_size;
4385 current_machine->maxram_size = maxram_size;
4386 current_machine->ram_slots = ram_slots;
4387 current_machine->boot_order = boot_order;
4388 current_machine->cpu_model = cpu_model;
4390 machine_class->init(current_machine);
4392 audio_init();
4394 cpu_synchronize_all_post_init();
4396 set_numa_modes();
4398 /* init USB devices */
4399 if (usb_enabled(false)) {
4400 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4401 exit(1);
4404 /* init generic devices */
4405 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4406 exit(1);
4408 net_check_clients();
4410 ds = init_displaystate();
4412 /* init local displays */
4413 switch (display_type) {
4414 case DT_NOGRAPHIC:
4415 (void)ds; /* avoid warning if no display is configured */
4416 break;
4417 #if defined(CONFIG_CURSES)
4418 case DT_CURSES:
4419 curses_display_init(ds, full_screen);
4420 break;
4421 #endif
4422 #if defined(CONFIG_SDL)
4423 case DT_SDL:
4424 sdl_display_init(ds, full_screen, no_frame);
4425 break;
4426 #elif defined(CONFIG_COCOA)
4427 case DT_SDL:
4428 cocoa_display_init(ds, full_screen);
4429 break;
4430 #endif
4431 #if defined(CONFIG_GTK)
4432 case DT_GTK:
4433 gtk_display_init(ds, full_screen, grab_on_hover);
4434 break;
4435 #endif
4436 default:
4437 break;
4440 /* must be after terminal init, SDL library changes signal handlers */
4441 os_setup_signal_handling();
4443 #ifdef CONFIG_VNC
4444 /* init remote displays */
4445 if (vnc_display) {
4446 Error *local_err = NULL;
4447 vnc_display_init(ds);
4448 vnc_display_open(ds, vnc_display, &local_err);
4449 if (local_err != NULL) {
4450 error_report("Failed to start VNC server on `%s': %s",
4451 vnc_display, error_get_pretty(local_err));
4452 error_free(local_err);
4453 exit(1);
4456 if (show_vnc_port) {
4457 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4460 #endif
4461 #ifdef CONFIG_SPICE
4462 if (using_spice) {
4463 qemu_spice_display_init();
4465 #endif
4467 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4468 exit(1);
4471 qdev_machine_creation_done();
4473 if (rom_load_all() != 0) {
4474 fprintf(stderr, "rom loading failed\n");
4475 exit(1);
4478 /* TODO: once all bus devices are qdevified, this should be done
4479 * when bus is created by qdev.c */
4480 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4481 qemu_run_machine_init_done_notifiers();
4483 /* Done notifiers can load ROMs */
4484 rom_load_done();
4486 qemu_system_reset(VMRESET_SILENT);
4487 if (loadvm) {
4488 if (load_vmstate(loadvm) < 0) {
4489 autostart = 0;
4493 qdev_prop_check_global();
4495 if (incoming) {
4496 Error *local_err = NULL;
4497 qemu_start_incoming_migration(incoming, &local_err);
4498 if (local_err) {
4499 error_report("-incoming %s: %s", incoming,
4500 error_get_pretty(local_err));
4501 error_free(local_err);
4502 exit(1);
4504 } else if (autostart) {
4505 vm_start();
4508 os_setup_post();
4510 if (is_daemonized()) {
4511 if (!trace_init_backends(trace_events, trace_file)) {
4512 exit(1);
4516 main_loop();
4517 bdrv_close_all();
4518 pause_all_vcpus();
4519 res_free();
4520 #ifdef CONFIG_TPM
4521 tpm_cleanup();
4522 #endif
4524 return 0;