Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob40dafb0e3f042fc256ae05df418d80d3d758e05d
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
31 #include "config-host.h"
33 #ifdef CONFIG_SECCOMP
34 #include "sysemu/seccomp.h"
35 #endif
37 #if defined(CONFIG_VDE)
38 #include <libvdeplug.h>
39 #endif
41 #ifdef CONFIG_SDL
42 #if defined(__APPLE__) || defined(main)
43 #include <SDL.h>
44 int qemu_main(int argc, char **argv);
45 int main(int argc, char **argv)
47 return qemu_main(argc, argv);
49 #undef main
50 #define main qemu_main
51 #endif
52 #endif /* CONFIG_SDL */
54 #ifdef CONFIG_COCOA
55 #undef main
56 #define main qemu_main
57 #endif /* CONFIG_COCOA */
59 #include <glib.h>
61 #include "qemu/sockets.h"
62 #include "hw/hw.h"
63 #include "hw/boards.h"
64 #include "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"
119 #include "qom/object_interfaces.h"
120 #include "qapi-event.h"
122 #define DEFAULT_RAM_SIZE 128
124 #define MAX_VIRTIO_CONSOLES 1
125 #define MAX_SCLP_CONSOLES 1
127 static const char *data_dir[16];
128 static int data_dir_idx;
129 const char *bios_name = NULL;
130 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
131 DisplayType display_type = DT_DEFAULT;
132 static int display_remote;
133 const char* keyboard_layout = NULL;
134 ram_addr_t ram_size;
135 const char *mem_path = NULL;
136 int mem_prealloc = 0; /* force preallocation of physical target memory */
137 int nb_nics;
138 NICInfo nd_table[MAX_NICS];
139 int autostart;
140 static int rtc_utc = 1;
141 static int rtc_date_offset = -1; /* -1 means no change */
142 QEMUClockType rtc_clock;
143 int vga_interface_type = VGA_NONE;
144 static int full_screen = 0;
145 static int no_frame = 0;
146 int no_quit = 0;
147 #ifdef CONFIG_GTK
148 static bool grab_on_hover;
149 #endif
150 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
151 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
152 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
153 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
154 int win2k_install_hack = 0;
155 int singlestep = 0;
156 int smp_cpus = 1;
157 int max_cpus = 0;
158 int smp_cores = 1;
159 int smp_threads = 1;
160 #ifdef CONFIG_VNC
161 const char *vnc_display;
162 #endif
163 int acpi_enabled = 1;
164 int no_hpet = 0;
165 int fd_bootchk = 1;
166 static int no_reboot;
167 int no_shutdown = 0;
168 int cursor_hide = 1;
169 int graphic_rotate = 0;
170 const char *watchdog;
171 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
172 int nb_option_roms;
173 int semihosting_enabled = 0;
174 int old_param = 0;
175 const char *qemu_name;
176 int alt_grab = 0;
177 int ctrl_grab = 0;
178 unsigned int nb_prom_envs = 0;
179 const char *prom_envs[MAX_PROM_ENVS];
180 int boot_menu;
181 static bool boot_strict;
182 uint8_t *boot_splash_filedata;
183 size_t boot_splash_filedata_size;
184 uint8_t qemu_extra_params_fw[2];
186 typedef struct FWBootEntry FWBootEntry;
188 struct FWBootEntry {
189 QTAILQ_ENTRY(FWBootEntry) link;
190 int32_t bootindex;
191 DeviceState *dev;
192 char *suffix;
195 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
196 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
198 int nb_numa_nodes;
199 int max_numa_nodeid;
200 NodeInfo numa_info[MAX_NODES];
202 uint8_t qemu_uuid[16];
203 bool qemu_uuid_set;
205 /* Trace unassigned memory or i/o accesses. */
206 bool trace_unassigned;
208 static QEMUBootSetHandler *boot_set_handler;
209 static void *boot_set_opaque;
211 static NotifierList exit_notifiers =
212 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
214 static NotifierList machine_init_done_notifiers =
215 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
217 static bool tcg_allowed = true;
218 bool xen_allowed;
219 uint32_t xen_domid;
220 enum xen_mode xen_mode = XEN_EMULATE;
221 static int tcg_tb_size;
223 static int has_defaults = 1;
224 static int default_serial = 1;
225 static int default_parallel = 1;
226 static int default_virtcon = 1;
227 static int default_sclp = 1;
228 static int default_monitor = 1;
229 static int default_floppy = 1;
230 static int default_cdrom = 1;
231 static int default_sdcard = 1;
232 static int default_vga = 1;
234 static struct {
235 const char *driver;
236 int *flag;
237 } default_list[] = {
238 { .driver = "isa-serial", .flag = &default_serial },
239 { .driver = "isa-parallel", .flag = &default_parallel },
240 { .driver = "isa-fdc", .flag = &default_floppy },
241 { .driver = "ide-cd", .flag = &default_cdrom },
242 { .driver = "ide-hd", .flag = &default_cdrom },
243 { .driver = "ide-drive", .flag = &default_cdrom },
244 { .driver = "scsi-cd", .flag = &default_cdrom },
245 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
246 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
247 { .driver = "virtio-serial", .flag = &default_virtcon },
248 { .driver = "VGA", .flag = &default_vga },
249 { .driver = "isa-vga", .flag = &default_vga },
250 { .driver = "cirrus-vga", .flag = &default_vga },
251 { .driver = "isa-cirrus-vga", .flag = &default_vga },
252 { .driver = "vmware-svga", .flag = &default_vga },
253 { .driver = "qxl-vga", .flag = &default_vga },
256 static QemuOptsList qemu_rtc_opts = {
257 .name = "rtc",
258 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
259 .desc = {
261 .name = "base",
262 .type = QEMU_OPT_STRING,
264 .name = "clock",
265 .type = QEMU_OPT_STRING,
267 .name = "driftfix",
268 .type = QEMU_OPT_STRING,
270 { /* end of list */ }
274 static QemuOptsList qemu_sandbox_opts = {
275 .name = "sandbox",
276 .implied_opt_name = "enable",
277 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
278 .desc = {
280 .name = "enable",
281 .type = QEMU_OPT_BOOL,
283 { /* end of list */ }
287 static QemuOptsList qemu_trace_opts = {
288 .name = "trace",
289 .implied_opt_name = "trace",
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
291 .desc = {
293 .name = "events",
294 .type = QEMU_OPT_STRING,
296 .name = "file",
297 .type = QEMU_OPT_STRING,
299 { /* end of list */ }
303 static QemuOptsList qemu_option_rom_opts = {
304 .name = "option-rom",
305 .implied_opt_name = "romfile",
306 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
307 .desc = {
309 .name = "bootindex",
310 .type = QEMU_OPT_NUMBER,
311 }, {
312 .name = "romfile",
313 .type = QEMU_OPT_STRING,
315 { /* end of list */ }
319 static QemuOptsList qemu_machine_opts = {
320 .name = "machine",
321 .implied_opt_name = "type",
322 .merge_lists = true,
323 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
324 .desc = {
326 .name = "type",
327 .type = QEMU_OPT_STRING,
328 .help = "emulated machine"
329 }, {
330 .name = "accel",
331 .type = QEMU_OPT_STRING,
332 .help = "accelerator list",
333 }, {
334 .name = "kernel_irqchip",
335 .type = QEMU_OPT_BOOL,
336 .help = "use KVM in-kernel irqchip",
337 }, {
338 .name = "kvm_shadow_mem",
339 .type = QEMU_OPT_SIZE,
340 .help = "KVM shadow MMU size",
341 }, {
342 .name = "kernel",
343 .type = QEMU_OPT_STRING,
344 .help = "Linux kernel image file",
345 }, {
346 .name = "initrd",
347 .type = QEMU_OPT_STRING,
348 .help = "Linux initial ramdisk file",
349 }, {
350 .name = "append",
351 .type = QEMU_OPT_STRING,
352 .help = "Linux kernel command line",
353 }, {
354 .name = "dtb",
355 .type = QEMU_OPT_STRING,
356 .help = "Linux kernel device tree file",
357 }, {
358 .name = "dumpdtb",
359 .type = QEMU_OPT_STRING,
360 .help = "Dump current dtb to a file and quit",
361 }, {
362 .name = "phandle_start",
363 .type = QEMU_OPT_NUMBER,
364 .help = "The first phandle ID we may generate dynamically",
365 }, {
366 .name = "dt_compatible",
367 .type = QEMU_OPT_STRING,
368 .help = "Overrides the \"compatible\" property of the dt root node",
369 }, {
370 .name = "dump-guest-core",
371 .type = QEMU_OPT_BOOL,
372 .help = "Include guest memory in a core dump",
373 }, {
374 .name = "mem-merge",
375 .type = QEMU_OPT_BOOL,
376 .help = "enable/disable memory merge support",
378 .name = "usb",
379 .type = QEMU_OPT_BOOL,
380 .help = "Set on/off to enable/disable usb",
382 .name = "firmware",
383 .type = QEMU_OPT_STRING,
384 .help = "firmware image",
386 .name = "kvm-type",
387 .type = QEMU_OPT_STRING,
388 .help = "Specifies the KVM virtualization mode (HV, PR)",
390 .name = PC_MACHINE_MAX_RAM_BELOW_4G,
391 .type = QEMU_OPT_SIZE,
392 .help = "maximum ram below the 4G boundary (32bit boundary)",
394 { /* End of list */ }
398 static QemuOptsList qemu_boot_opts = {
399 .name = "boot-opts",
400 .implied_opt_name = "order",
401 .merge_lists = true,
402 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
403 .desc = {
405 .name = "order",
406 .type = QEMU_OPT_STRING,
407 }, {
408 .name = "once",
409 .type = QEMU_OPT_STRING,
410 }, {
411 .name = "menu",
412 .type = QEMU_OPT_BOOL,
413 }, {
414 .name = "splash",
415 .type = QEMU_OPT_STRING,
416 }, {
417 .name = "splash-time",
418 .type = QEMU_OPT_STRING,
419 }, {
420 .name = "reboot-timeout",
421 .type = QEMU_OPT_STRING,
422 }, {
423 .name = "strict",
424 .type = QEMU_OPT_BOOL,
426 { /*End of list */ }
430 static QemuOptsList qemu_add_fd_opts = {
431 .name = "add-fd",
432 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
433 .desc = {
435 .name = "fd",
436 .type = QEMU_OPT_NUMBER,
437 .help = "file descriptor of which a duplicate is added to fd set",
439 .name = "set",
440 .type = QEMU_OPT_NUMBER,
441 .help = "ID of the fd set to add fd to",
443 .name = "opaque",
444 .type = QEMU_OPT_STRING,
445 .help = "free-form string used to describe fd",
447 { /* end of list */ }
451 static QemuOptsList qemu_object_opts = {
452 .name = "object",
453 .implied_opt_name = "qom-type",
454 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
455 .desc = {
460 static QemuOptsList qemu_tpmdev_opts = {
461 .name = "tpmdev",
462 .implied_opt_name = "type",
463 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
464 .desc = {
465 /* options are defined in the TPM backends */
466 { /* end of list */ }
470 static QemuOptsList qemu_realtime_opts = {
471 .name = "realtime",
472 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
473 .desc = {
475 .name = "mlock",
476 .type = QEMU_OPT_BOOL,
478 { /* end of list */ }
482 static QemuOptsList qemu_msg_opts = {
483 .name = "msg",
484 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
485 .desc = {
487 .name = "timestamp",
488 .type = QEMU_OPT_BOOL,
490 { /* end of list */ }
494 static QemuOptsList qemu_name_opts = {
495 .name = "name",
496 .implied_opt_name = "guest",
497 .merge_lists = true,
498 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
499 .desc = {
501 .name = "guest",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the name of the guest.\n"
504 "This name will be displayed in the SDL window caption.\n"
505 "The name will also be used for the VNC server",
506 }, {
507 .name = "process",
508 .type = QEMU_OPT_STRING,
509 .help = "Sets the name of the QEMU process, as shown in top etc",
510 }, {
511 .name = "debug-threads",
512 .type = QEMU_OPT_BOOL,
513 .help = "When enabled, name the individual threads; defaults off.\n"
514 "NOTE: The thread names are for debugging and not a\n"
515 "stable API.",
517 { /* End of list */ }
521 static QemuOptsList qemu_mem_opts = {
522 .name = "memory",
523 .implied_opt_name = "size",
524 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
525 .merge_lists = true,
526 .desc = {
528 .name = "size",
529 .type = QEMU_OPT_SIZE,
532 .name = "slots",
533 .type = QEMU_OPT_NUMBER,
536 .name = "maxmem",
537 .type = QEMU_OPT_SIZE,
539 { /* end of list */ }
544 * Get machine options
546 * Returns: machine options (never null).
548 QemuOpts *qemu_get_machine_opts(void)
550 return qemu_find_opts_singleton("machine");
553 const char *qemu_get_vm_name(void)
555 return qemu_name;
558 static void res_free(void)
560 if (boot_splash_filedata != NULL) {
561 g_free(boot_splash_filedata);
562 boot_splash_filedata = NULL;
566 static int default_driver_check(QemuOpts *opts, void *opaque)
568 const char *driver = qemu_opt_get(opts, "driver");
569 int i;
571 if (!driver)
572 return 0;
573 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
574 if (strcmp(default_list[i].driver, driver) != 0)
575 continue;
576 *(default_list[i].flag) = 0;
578 return 0;
581 /***********************************************************/
582 /* QEMU state */
584 static RunState current_run_state = RUN_STATE_PRELAUNCH;
586 /* We use RUN_STATE_MAX but any invalid value will do */
587 static RunState vmstop_requested = RUN_STATE_MAX;
588 static QemuMutex vmstop_lock;
590 typedef struct {
591 RunState from;
592 RunState to;
593 } RunStateTransition;
595 static const RunStateTransition runstate_transitions_def[] = {
596 /* from -> to */
597 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
598 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
601 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
603 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
604 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
606 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
607 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
610 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
613 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
616 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
617 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
619 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
620 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
622 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
624 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
625 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
626 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
627 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
628 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
629 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
630 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
631 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
632 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
633 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
635 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
637 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
638 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
640 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
641 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
642 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
643 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
645 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
646 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
649 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
651 { RUN_STATE_MAX, RUN_STATE_MAX },
654 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
656 bool runstate_check(RunState state)
658 return current_run_state == state;
661 static void runstate_init(void)
663 const RunStateTransition *p;
665 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
666 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
667 runstate_valid_transitions[p->from][p->to] = true;
670 qemu_mutex_init(&vmstop_lock);
673 /* This function will abort() on invalid state transitions */
674 void runstate_set(RunState new_state)
676 assert(new_state < RUN_STATE_MAX);
678 if (!runstate_valid_transitions[current_run_state][new_state]) {
679 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
680 RunState_lookup[current_run_state],
681 RunState_lookup[new_state]);
682 abort();
684 trace_runstate_set(new_state);
685 current_run_state = new_state;
688 int runstate_is_running(void)
690 return runstate_check(RUN_STATE_RUNNING);
693 bool runstate_needs_reset(void)
695 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
696 runstate_check(RUN_STATE_SHUTDOWN);
699 StatusInfo *qmp_query_status(Error **errp)
701 StatusInfo *info = g_malloc0(sizeof(*info));
703 info->running = runstate_is_running();
704 info->singlestep = singlestep;
705 info->status = current_run_state;
707 return info;
710 static bool qemu_vmstop_requested(RunState *r)
712 qemu_mutex_lock(&vmstop_lock);
713 *r = vmstop_requested;
714 vmstop_requested = RUN_STATE_MAX;
715 qemu_mutex_unlock(&vmstop_lock);
716 return *r < RUN_STATE_MAX;
719 void qemu_system_vmstop_request_prepare(void)
721 qemu_mutex_lock(&vmstop_lock);
724 void qemu_system_vmstop_request(RunState state)
726 vmstop_requested = state;
727 qemu_mutex_unlock(&vmstop_lock);
728 qemu_notify_event();
731 void vm_start(void)
733 RunState requested;
735 qemu_vmstop_requested(&requested);
736 if (runstate_is_running() && requested == RUN_STATE_MAX) {
737 return;
740 /* Ensure that a STOP/RESUME pair of events is emitted if a
741 * vmstop request was pending. The BLOCK_IO_ERROR event, for
742 * example, according to documentation is always followed by
743 * the STOP event.
745 if (runstate_is_running()) {
746 qapi_event_send_stop(&error_abort);
747 } else {
748 cpu_enable_ticks();
749 runstate_set(RUN_STATE_RUNNING);
750 vm_state_notify(1, RUN_STATE_RUNNING);
751 resume_all_vcpus();
754 qapi_event_send_resume(&error_abort);
758 /***********************************************************/
759 /* real time host monotonic timer */
761 /***********************************************************/
762 /* host time/date access */
763 void qemu_get_timedate(struct tm *tm, int offset)
765 time_t ti;
767 time(&ti);
768 ti += offset;
769 if (rtc_date_offset == -1) {
770 if (rtc_utc)
771 gmtime_r(&ti, tm);
772 else
773 localtime_r(&ti, tm);
774 } else {
775 ti -= rtc_date_offset;
776 gmtime_r(&ti, tm);
780 int qemu_timedate_diff(struct tm *tm)
782 time_t seconds;
784 if (rtc_date_offset == -1)
785 if (rtc_utc)
786 seconds = mktimegm(tm);
787 else {
788 struct tm tmp = *tm;
789 tmp.tm_isdst = -1; /* use timezone to figure it out */
790 seconds = mktime(&tmp);
792 else
793 seconds = mktimegm(tm) + rtc_date_offset;
795 return seconds - time(NULL);
798 static void configure_rtc_date_offset(const char *startdate, int legacy)
800 time_t rtc_start_date;
801 struct tm tm;
803 if (!strcmp(startdate, "now") && legacy) {
804 rtc_date_offset = -1;
805 } else {
806 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
807 &tm.tm_year,
808 &tm.tm_mon,
809 &tm.tm_mday,
810 &tm.tm_hour,
811 &tm.tm_min,
812 &tm.tm_sec) == 6) {
813 /* OK */
814 } else if (sscanf(startdate, "%d-%d-%d",
815 &tm.tm_year,
816 &tm.tm_mon,
817 &tm.tm_mday) == 3) {
818 tm.tm_hour = 0;
819 tm.tm_min = 0;
820 tm.tm_sec = 0;
821 } else {
822 goto date_fail;
824 tm.tm_year -= 1900;
825 tm.tm_mon--;
826 rtc_start_date = mktimegm(&tm);
827 if (rtc_start_date == -1) {
828 date_fail:
829 fprintf(stderr, "Invalid date format. Valid formats are:\n"
830 "'2006-06-17T16:01:21' or '2006-06-17'\n");
831 exit(1);
833 rtc_date_offset = time(NULL) - rtc_start_date;
837 static void configure_rtc(QemuOpts *opts)
839 const char *value;
841 value = qemu_opt_get(opts, "base");
842 if (value) {
843 if (!strcmp(value, "utc")) {
844 rtc_utc = 1;
845 } else if (!strcmp(value, "localtime")) {
846 rtc_utc = 0;
847 } else {
848 configure_rtc_date_offset(value, 0);
851 value = qemu_opt_get(opts, "clock");
852 if (value) {
853 if (!strcmp(value, "host")) {
854 rtc_clock = QEMU_CLOCK_HOST;
855 } else if (!strcmp(value, "rt")) {
856 rtc_clock = QEMU_CLOCK_REALTIME;
857 } else if (!strcmp(value, "vm")) {
858 rtc_clock = QEMU_CLOCK_VIRTUAL;
859 } else {
860 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
861 exit(1);
864 value = qemu_opt_get(opts, "driftfix");
865 if (value) {
866 if (!strcmp(value, "slew")) {
867 static GlobalProperty slew_lost_ticks[] = {
869 .driver = "mc146818rtc",
870 .property = "lost_tick_policy",
871 .value = "slew",
873 { /* end of list */ }
876 qdev_prop_register_global_list(slew_lost_ticks);
877 } else if (!strcmp(value, "none")) {
878 /* discard is default */
879 } else {
880 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
881 exit(1);
886 /***********************************************************/
887 /* Bluetooth support */
888 static int nb_hcis;
889 static int cur_hci;
890 static struct HCIInfo *hci_table[MAX_NICS];
892 struct HCIInfo *qemu_next_hci(void)
894 if (cur_hci == nb_hcis)
895 return &null_hci;
897 return hci_table[cur_hci++];
900 static int bt_hci_parse(const char *str)
902 struct HCIInfo *hci;
903 bdaddr_t bdaddr;
905 if (nb_hcis >= MAX_NICS) {
906 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
907 return -1;
910 hci = hci_init(str);
911 if (!hci)
912 return -1;
914 bdaddr.b[0] = 0x52;
915 bdaddr.b[1] = 0x54;
916 bdaddr.b[2] = 0x00;
917 bdaddr.b[3] = 0x12;
918 bdaddr.b[4] = 0x34;
919 bdaddr.b[5] = 0x56 + nb_hcis;
920 hci->bdaddr_set(hci, bdaddr.b);
922 hci_table[nb_hcis++] = hci;
924 return 0;
927 static void bt_vhci_add(int vlan_id)
929 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
931 if (!vlan->slave)
932 fprintf(stderr, "qemu: warning: adding a VHCI to "
933 "an empty scatternet %i\n", vlan_id);
935 bt_vhci_init(bt_new_hci(vlan));
938 static struct bt_device_s *bt_device_add(const char *opt)
940 struct bt_scatternet_s *vlan;
941 int vlan_id = 0;
942 char *endp = strstr(opt, ",vlan=");
943 int len = (endp ? endp - opt : strlen(opt)) + 1;
944 char devname[10];
946 pstrcpy(devname, MIN(sizeof(devname), len), opt);
948 if (endp) {
949 vlan_id = strtol(endp + 6, &endp, 0);
950 if (*endp) {
951 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
952 return 0;
956 vlan = qemu_find_bt_vlan(vlan_id);
958 if (!vlan->slave)
959 fprintf(stderr, "qemu: warning: adding a slave device to "
960 "an empty scatternet %i\n", vlan_id);
962 if (!strcmp(devname, "keyboard"))
963 return bt_keyboard_init(vlan);
965 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
966 return 0;
969 static int bt_parse(const char *opt)
971 const char *endp, *p;
972 int vlan;
974 if (strstart(opt, "hci", &endp)) {
975 if (!*endp || *endp == ',') {
976 if (*endp)
977 if (!strstart(endp, ",vlan=", 0))
978 opt = endp + 1;
980 return bt_hci_parse(opt);
982 } else if (strstart(opt, "vhci", &endp)) {
983 if (!*endp || *endp == ',') {
984 if (*endp) {
985 if (strstart(endp, ",vlan=", &p)) {
986 vlan = strtol(p, (char **) &endp, 0);
987 if (*endp) {
988 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
989 return 1;
991 } else {
992 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
993 return 1;
995 } else
996 vlan = 0;
998 bt_vhci_add(vlan);
999 return 0;
1001 } else if (strstart(opt, "device:", &endp))
1002 return !bt_device_add(endp);
1004 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1005 return 1;
1008 static int parse_sandbox(QemuOpts *opts, void *opaque)
1010 /* FIXME: change this to true for 1.3 */
1011 if (qemu_opt_get_bool(opts, "enable", false)) {
1012 #ifdef CONFIG_SECCOMP
1013 if (seccomp_start() < 0) {
1014 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1015 "failed to install seccomp syscall filter in the kernel");
1016 return -1;
1018 #else
1019 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1020 "sandboxing request but seccomp is not compiled into this build");
1021 return -1;
1022 #endif
1025 return 0;
1028 static int parse_name(QemuOpts *opts, void *opaque)
1030 const char *proc_name;
1032 if (qemu_opt_get(opts, "debug-threads")) {
1033 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1035 qemu_name = qemu_opt_get(opts, "guest");
1037 proc_name = qemu_opt_get(opts, "process");
1038 if (proc_name) {
1039 os_set_proc_name(proc_name);
1042 return 0;
1045 bool usb_enabled(bool default_usb)
1047 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb",
1048 has_defaults && default_usb);
1051 #ifndef _WIN32
1052 static int parse_add_fd(QemuOpts *opts, void *opaque)
1054 int fd, dupfd, flags;
1055 int64_t fdset_id;
1056 const char *fd_opaque = NULL;
1058 fd = qemu_opt_get_number(opts, "fd", -1);
1059 fdset_id = qemu_opt_get_number(opts, "set", -1);
1060 fd_opaque = qemu_opt_get(opts, "opaque");
1062 if (fd < 0) {
1063 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1064 "fd option is required and must be non-negative");
1065 return -1;
1068 if (fd <= STDERR_FILENO) {
1069 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1070 "fd cannot be a standard I/O stream");
1071 return -1;
1075 * All fds inherited across exec() necessarily have FD_CLOEXEC
1076 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1078 flags = fcntl(fd, F_GETFD);
1079 if (flags == -1 || (flags & FD_CLOEXEC)) {
1080 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1081 "fd is not valid or already in use");
1082 return -1;
1085 if (fdset_id < 0) {
1086 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1087 "set option is required and must be non-negative");
1088 return -1;
1091 #ifdef F_DUPFD_CLOEXEC
1092 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1093 #else
1094 dupfd = dup(fd);
1095 if (dupfd != -1) {
1096 qemu_set_cloexec(dupfd);
1098 #endif
1099 if (dupfd == -1) {
1100 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1101 "Error duplicating fd: %s", strerror(errno));
1102 return -1;
1105 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1106 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1107 fd_opaque, NULL);
1109 return 0;
1112 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1114 int fd;
1116 fd = qemu_opt_get_number(opts, "fd", -1);
1117 close(fd);
1119 return 0;
1121 #endif
1123 /***********************************************************/
1124 /* QEMU Block devices */
1126 #define HD_OPTS "media=disk"
1127 #define CDROM_OPTS "media=cdrom"
1128 #define FD_OPTS ""
1129 #define PFLASH_OPTS ""
1130 #define MTD_OPTS ""
1131 #define SD_OPTS ""
1133 static int drive_init_func(QemuOpts *opts, void *opaque)
1135 BlockInterfaceType *block_default_type = opaque;
1137 return drive_new(opts, *block_default_type) == NULL;
1140 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1142 if (NULL == qemu_opt_get(opts, "snapshot")) {
1143 qemu_opt_set(opts, "snapshot", "on");
1145 return 0;
1148 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1149 int index, const char *optstr)
1151 QemuOpts *opts;
1153 if (!enable || drive_get_by_index(type, index)) {
1154 return;
1157 opts = drive_add(type, index, NULL, optstr);
1158 if (snapshot) {
1159 drive_enable_snapshot(opts, NULL);
1161 if (!drive_new(opts, type)) {
1162 exit(1);
1166 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1168 boot_set_handler = func;
1169 boot_set_opaque = opaque;
1172 int qemu_boot_set(const char *boot_order)
1174 if (!boot_set_handler) {
1175 return -EINVAL;
1177 return boot_set_handler(boot_set_opaque, boot_order);
1180 static void validate_bootdevices(const char *devices)
1182 /* We just do some generic consistency checks */
1183 const char *p;
1184 int bitmap = 0;
1186 for (p = devices; *p != '\0'; p++) {
1187 /* Allowed boot devices are:
1188 * a-b: floppy disk drives
1189 * c-f: IDE disk drives
1190 * g-m: machine implementation dependent drives
1191 * n-p: network devices
1192 * It's up to each machine implementation to check if the given boot
1193 * devices match the actual hardware implementation and firmware
1194 * features.
1196 if (*p < 'a' || *p > 'p') {
1197 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1198 exit(1);
1200 if (bitmap & (1 << (*p - 'a'))) {
1201 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1202 exit(1);
1204 bitmap |= 1 << (*p - 'a');
1208 static void restore_boot_order(void *opaque)
1210 char *normal_boot_order = opaque;
1211 static int first = 1;
1213 /* Restore boot order and remove ourselves after the first boot */
1214 if (first) {
1215 first = 0;
1216 return;
1219 qemu_boot_set(normal_boot_order);
1221 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1222 g_free(normal_boot_order);
1225 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1226 const char *suffix)
1228 FWBootEntry *node, *i;
1230 if (bootindex < 0) {
1231 return;
1234 assert(dev != NULL || suffix != NULL);
1236 node = g_malloc0(sizeof(FWBootEntry));
1237 node->bootindex = bootindex;
1238 node->suffix = g_strdup(suffix);
1239 node->dev = dev;
1241 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1242 if (i->bootindex == bootindex) {
1243 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1244 exit(1);
1245 } else if (i->bootindex < bootindex) {
1246 continue;
1248 QTAILQ_INSERT_BEFORE(i, node, link);
1249 return;
1251 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1254 DeviceState *get_boot_device(uint32_t position)
1256 uint32_t counter = 0;
1257 FWBootEntry *i = NULL;
1258 DeviceState *res = NULL;
1260 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1261 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1262 if (counter == position) {
1263 res = i->dev;
1264 break;
1266 counter++;
1269 return res;
1273 * This function returns null terminated string that consist of new line
1274 * separated device paths.
1276 * memory pointed by "size" is assigned total length of the array in bytes
1279 char *get_boot_devices_list(size_t *size, bool ignore_suffixes)
1281 FWBootEntry *i;
1282 size_t total = 0;
1283 char *list = NULL;
1285 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1286 char *devpath = NULL, *bootpath;
1287 size_t len;
1289 if (i->dev) {
1290 devpath = qdev_get_fw_dev_path(i->dev);
1291 assert(devpath);
1294 if (i->suffix && !ignore_suffixes && devpath) {
1295 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1297 bootpath = g_malloc(bootpathlen);
1298 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1299 g_free(devpath);
1300 } else if (devpath) {
1301 bootpath = devpath;
1302 } else if (!ignore_suffixes) {
1303 assert(i->suffix);
1304 bootpath = g_strdup(i->suffix);
1305 } else {
1306 bootpath = g_strdup("");
1309 if (total) {
1310 list[total-1] = '\n';
1312 len = strlen(bootpath) + 1;
1313 list = g_realloc(list, total + len);
1314 memcpy(&list[total], bootpath, len);
1315 total += len;
1316 g_free(bootpath);
1319 *size = total;
1321 if (boot_strict && *size > 0) {
1322 list[total-1] = '\n';
1323 list = g_realloc(list, total + 5);
1324 memcpy(&list[total], "HALT", 5);
1325 *size = total + 5;
1327 return list;
1330 static QemuOptsList qemu_smp_opts = {
1331 .name = "smp-opts",
1332 .implied_opt_name = "cpus",
1333 .merge_lists = true,
1334 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1335 .desc = {
1337 .name = "cpus",
1338 .type = QEMU_OPT_NUMBER,
1339 }, {
1340 .name = "sockets",
1341 .type = QEMU_OPT_NUMBER,
1342 }, {
1343 .name = "cores",
1344 .type = QEMU_OPT_NUMBER,
1345 }, {
1346 .name = "threads",
1347 .type = QEMU_OPT_NUMBER,
1348 }, {
1349 .name = "maxcpus",
1350 .type = QEMU_OPT_NUMBER,
1352 { /*End of list */ }
1356 static void smp_parse(QemuOpts *opts)
1358 if (opts) {
1360 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1361 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1362 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1363 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1365 /* compute missing values, prefer sockets over cores over threads */
1366 if (cpus == 0 || sockets == 0) {
1367 sockets = sockets > 0 ? sockets : 1;
1368 cores = cores > 0 ? cores : 1;
1369 threads = threads > 0 ? threads : 1;
1370 if (cpus == 0) {
1371 cpus = cores * threads * sockets;
1373 } else {
1374 if (cores == 0) {
1375 threads = threads > 0 ? threads : 1;
1376 cores = cpus / (sockets * threads);
1377 } else {
1378 threads = cpus / (cores * sockets);
1382 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1384 smp_cpus = cpus;
1385 smp_cores = cores > 0 ? cores : 1;
1386 smp_threads = threads > 0 ? threads : 1;
1390 if (max_cpus == 0) {
1391 max_cpus = smp_cpus;
1394 if (max_cpus > MAX_CPUMASK_BITS) {
1395 fprintf(stderr, "Unsupported number of maxcpus\n");
1396 exit(1);
1398 if (max_cpus < smp_cpus) {
1399 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1400 exit(1);
1405 static void configure_realtime(QemuOpts *opts)
1407 bool enable_mlock;
1409 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1411 if (enable_mlock) {
1412 if (os_mlock() < 0) {
1413 fprintf(stderr, "qemu: locking memory failed\n");
1414 exit(1);
1420 static void configure_msg(QemuOpts *opts)
1422 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1425 /***********************************************************/
1426 /* USB devices */
1428 static int usb_device_add(const char *devname)
1430 USBDevice *dev = NULL;
1431 #ifndef CONFIG_LINUX
1432 const char *p;
1433 #endif
1435 if (!usb_enabled(false)) {
1436 return -1;
1439 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1440 dev = usbdevice_create(devname);
1441 if (dev)
1442 goto done;
1444 /* the other ones */
1445 #ifndef CONFIG_LINUX
1446 /* only the linux version is qdev-ified, usb-bsd still needs this */
1447 if (strstart(devname, "host:", &p)) {
1448 dev = usb_host_device_open(usb_bus_find(-1), p);
1450 #endif
1451 if (!dev)
1452 return -1;
1454 done:
1455 return 0;
1458 static int usb_device_del(const char *devname)
1460 int bus_num, addr;
1461 const char *p;
1463 if (strstart(devname, "host:", &p)) {
1464 return -1;
1467 if (!usb_enabled(false)) {
1468 return -1;
1471 p = strchr(devname, '.');
1472 if (!p)
1473 return -1;
1474 bus_num = strtoul(devname, NULL, 0);
1475 addr = strtoul(p + 1, NULL, 0);
1477 return usb_device_delete_addr(bus_num, addr);
1480 static int usb_parse(const char *cmdline)
1482 int r;
1483 r = usb_device_add(cmdline);
1484 if (r < 0) {
1485 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1487 return r;
1490 void do_usb_add(Monitor *mon, const QDict *qdict)
1492 const char *devname = qdict_get_str(qdict, "devname");
1493 if (usb_device_add(devname) < 0) {
1494 error_report("could not add USB device '%s'", devname);
1498 void do_usb_del(Monitor *mon, const QDict *qdict)
1500 const char *devname = qdict_get_str(qdict, "devname");
1501 if (usb_device_del(devname) < 0) {
1502 error_report("could not delete USB device '%s'", devname);
1506 /***********************************************************/
1507 /* PCMCIA/Cardbus */
1509 static struct pcmcia_socket_entry_s {
1510 PCMCIASocket *socket;
1511 struct pcmcia_socket_entry_s *next;
1512 } *pcmcia_sockets = 0;
1514 void pcmcia_socket_register(PCMCIASocket *socket)
1516 struct pcmcia_socket_entry_s *entry;
1518 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1519 entry->socket = socket;
1520 entry->next = pcmcia_sockets;
1521 pcmcia_sockets = entry;
1524 void pcmcia_socket_unregister(PCMCIASocket *socket)
1526 struct pcmcia_socket_entry_s *entry, **ptr;
1528 ptr = &pcmcia_sockets;
1529 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1530 if (entry->socket == socket) {
1531 *ptr = entry->next;
1532 g_free(entry);
1536 void pcmcia_info(Monitor *mon, const QDict *qdict)
1538 struct pcmcia_socket_entry_s *iter;
1540 if (!pcmcia_sockets)
1541 monitor_printf(mon, "No PCMCIA sockets\n");
1543 for (iter = pcmcia_sockets; iter; iter = iter->next)
1544 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1545 iter->socket->attached ? iter->socket->card_string :
1546 "Empty");
1549 /***********************************************************/
1550 /* machine registration */
1552 MachineState *current_machine;
1554 static void machine_class_init(ObjectClass *oc, void *data)
1556 MachineClass *mc = MACHINE_CLASS(oc);
1557 QEMUMachine *qm = data;
1559 mc->name = qm->name;
1560 mc->alias = qm->alias;
1561 mc->desc = qm->desc;
1562 mc->init = qm->init;
1563 mc->reset = qm->reset;
1564 mc->hot_add_cpu = qm->hot_add_cpu;
1565 mc->kvm_type = qm->kvm_type;
1566 mc->block_default_type = qm->block_default_type;
1567 mc->max_cpus = qm->max_cpus;
1568 mc->no_serial = qm->no_serial;
1569 mc->no_parallel = qm->no_parallel;
1570 mc->use_virtcon = qm->use_virtcon;
1571 mc->use_sclp = qm->use_sclp;
1572 mc->no_floppy = qm->no_floppy;
1573 mc->no_cdrom = qm->no_cdrom;
1574 mc->no_sdcard = qm->no_sdcard;
1575 mc->is_default = qm->is_default;
1576 mc->default_machine_opts = qm->default_machine_opts;
1577 mc->default_boot_order = qm->default_boot_order;
1578 mc->compat_props = qm->compat_props;
1579 mc->hw_version = qm->hw_version;
1582 int qemu_register_machine(QEMUMachine *m)
1584 char *name = g_strconcat(m->name, TYPE_MACHINE_SUFFIX, NULL);
1585 TypeInfo ti = {
1586 .name = name,
1587 .parent = TYPE_MACHINE,
1588 .class_init = machine_class_init,
1589 .class_data = (void *)m,
1592 type_register(&ti);
1593 g_free(name);
1595 return 0;
1598 static MachineClass *find_machine(const char *name)
1600 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1601 MachineClass *mc = NULL;
1603 for (el = machines; el; el = el->next) {
1604 MachineClass *temp = el->data;
1606 if (!strcmp(temp->name, name)) {
1607 mc = temp;
1608 break;
1610 if (temp->alias &&
1611 !strcmp(temp->alias, name)) {
1612 mc = temp;
1613 break;
1617 g_slist_free(machines);
1618 return mc;
1621 MachineClass *find_default_machine(void)
1623 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1624 MachineClass *mc = NULL;
1626 for (el = machines; el; el = el->next) {
1627 MachineClass *temp = el->data;
1629 if (temp->is_default) {
1630 mc = temp;
1631 break;
1635 g_slist_free(machines);
1636 return mc;
1639 MachineInfoList *qmp_query_machines(Error **errp)
1641 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1642 MachineInfoList *mach_list = NULL;
1644 for (el = machines; el; el = el->next) {
1645 MachineClass *mc = el->data;
1646 MachineInfoList *entry;
1647 MachineInfo *info;
1649 info = g_malloc0(sizeof(*info));
1650 if (mc->is_default) {
1651 info->has_is_default = true;
1652 info->is_default = true;
1655 if (mc->alias) {
1656 info->has_alias = true;
1657 info->alias = g_strdup(mc->alias);
1660 info->name = g_strdup(mc->name);
1661 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1663 entry = g_malloc0(sizeof(*entry));
1664 entry->value = info;
1665 entry->next = mach_list;
1666 mach_list = entry;
1669 g_slist_free(machines);
1670 return mach_list;
1673 /***********************************************************/
1674 /* main execution loop */
1676 struct vm_change_state_entry {
1677 VMChangeStateHandler *cb;
1678 void *opaque;
1679 QLIST_ENTRY (vm_change_state_entry) entries;
1682 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1684 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1685 void *opaque)
1687 VMChangeStateEntry *e;
1689 e = g_malloc0(sizeof (*e));
1691 e->cb = cb;
1692 e->opaque = opaque;
1693 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1694 return e;
1697 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1699 QLIST_REMOVE (e, entries);
1700 g_free (e);
1703 void vm_state_notify(int running, RunState state)
1705 VMChangeStateEntry *e;
1707 trace_vm_state_notify(running, state);
1709 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1710 e->cb(e->opaque, running, state);
1714 /* reset/shutdown handler */
1716 typedef struct QEMUResetEntry {
1717 QTAILQ_ENTRY(QEMUResetEntry) entry;
1718 QEMUResetHandler *func;
1719 void *opaque;
1720 } QEMUResetEntry;
1722 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1723 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1724 static int reset_requested;
1725 static int shutdown_requested, shutdown_signal = -1;
1726 static pid_t shutdown_pid;
1727 static int powerdown_requested;
1728 static int debug_requested;
1729 static int suspend_requested;
1730 static WakeupReason wakeup_reason;
1731 static NotifierList powerdown_notifiers =
1732 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1733 static NotifierList suspend_notifiers =
1734 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1735 static NotifierList wakeup_notifiers =
1736 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1737 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1739 int qemu_shutdown_requested_get(void)
1741 return shutdown_requested;
1744 int qemu_reset_requested_get(void)
1746 return reset_requested;
1749 static int qemu_shutdown_requested(void)
1751 int r = shutdown_requested;
1752 shutdown_requested = 0;
1753 return r;
1756 static void qemu_kill_report(void)
1758 if (!qtest_driver() && shutdown_signal != -1) {
1759 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1760 if (shutdown_pid == 0) {
1761 /* This happens for eg ^C at the terminal, so it's worth
1762 * avoiding printing an odd message in that case.
1764 fputc('\n', stderr);
1765 } else {
1766 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1768 shutdown_signal = -1;
1772 static int qemu_reset_requested(void)
1774 int r = reset_requested;
1775 reset_requested = 0;
1776 return r;
1779 static int qemu_suspend_requested(void)
1781 int r = suspend_requested;
1782 suspend_requested = 0;
1783 return r;
1786 static WakeupReason qemu_wakeup_requested(void)
1788 return wakeup_reason;
1791 static int qemu_powerdown_requested(void)
1793 int r = powerdown_requested;
1794 powerdown_requested = 0;
1795 return r;
1798 static int qemu_debug_requested(void)
1800 int r = debug_requested;
1801 debug_requested = 0;
1802 return r;
1805 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1807 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1809 re->func = func;
1810 re->opaque = opaque;
1811 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1814 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1816 QEMUResetEntry *re;
1818 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1819 if (re->func == func && re->opaque == opaque) {
1820 QTAILQ_REMOVE(&reset_handlers, re, entry);
1821 g_free(re);
1822 return;
1827 void qemu_devices_reset(void)
1829 QEMUResetEntry *re, *nre;
1831 /* reset all devices */
1832 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1833 re->func(re->opaque);
1837 void qemu_system_reset(bool report)
1839 MachineClass *mc;
1841 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1843 if (mc && mc->reset) {
1844 mc->reset();
1845 } else {
1846 qemu_devices_reset();
1848 if (report) {
1849 qapi_event_send_reset(&error_abort);
1851 cpu_synchronize_all_post_reset();
1854 void qemu_system_reset_request(void)
1856 if (no_reboot) {
1857 shutdown_requested = 1;
1858 } else {
1859 reset_requested = 1;
1861 cpu_stop_current();
1862 qemu_notify_event();
1865 static void qemu_system_suspend(void)
1867 pause_all_vcpus();
1868 notifier_list_notify(&suspend_notifiers, NULL);
1869 runstate_set(RUN_STATE_SUSPENDED);
1870 qapi_event_send_suspend(&error_abort);
1873 void qemu_system_suspend_request(void)
1875 if (runstate_check(RUN_STATE_SUSPENDED)) {
1876 return;
1878 suspend_requested = 1;
1879 cpu_stop_current();
1880 qemu_notify_event();
1883 void qemu_register_suspend_notifier(Notifier *notifier)
1885 notifier_list_add(&suspend_notifiers, notifier);
1888 void qemu_system_wakeup_request(WakeupReason reason)
1890 trace_system_wakeup_request(reason);
1892 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1893 return;
1895 if (!(wakeup_reason_mask & (1 << reason))) {
1896 return;
1898 runstate_set(RUN_STATE_RUNNING);
1899 wakeup_reason = reason;
1900 qemu_notify_event();
1903 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1905 if (enabled) {
1906 wakeup_reason_mask |= (1 << reason);
1907 } else {
1908 wakeup_reason_mask &= ~(1 << reason);
1912 void qemu_register_wakeup_notifier(Notifier *notifier)
1914 notifier_list_add(&wakeup_notifiers, notifier);
1917 void qemu_system_killed(int signal, pid_t pid)
1919 shutdown_signal = signal;
1920 shutdown_pid = pid;
1921 no_shutdown = 0;
1922 qemu_system_shutdown_request();
1925 void qemu_system_shutdown_request(void)
1927 trace_qemu_system_shutdown_request();
1928 shutdown_requested = 1;
1929 qemu_notify_event();
1932 static void qemu_system_powerdown(void)
1934 qapi_event_send_powerdown(&error_abort);
1935 notifier_list_notify(&powerdown_notifiers, NULL);
1938 void qemu_system_powerdown_request(void)
1940 trace_qemu_system_powerdown_request();
1941 powerdown_requested = 1;
1942 qemu_notify_event();
1945 void qemu_register_powerdown_notifier(Notifier *notifier)
1947 notifier_list_add(&powerdown_notifiers, notifier);
1950 void qemu_system_debug_request(void)
1952 debug_requested = 1;
1953 qemu_notify_event();
1956 static bool main_loop_should_exit(void)
1958 RunState r;
1959 if (qemu_debug_requested()) {
1960 vm_stop(RUN_STATE_DEBUG);
1962 if (qemu_suspend_requested()) {
1963 qemu_system_suspend();
1965 if (qemu_shutdown_requested()) {
1966 qemu_kill_report();
1967 qapi_event_send_shutdown(&error_abort);
1968 if (no_shutdown) {
1969 vm_stop(RUN_STATE_SHUTDOWN);
1970 } else {
1971 return true;
1974 if (qemu_reset_requested()) {
1975 pause_all_vcpus();
1976 cpu_synchronize_all_states();
1977 qemu_system_reset(VMRESET_REPORT);
1978 resume_all_vcpus();
1979 if (runstate_needs_reset()) {
1980 runstate_set(RUN_STATE_PAUSED);
1983 if (qemu_wakeup_requested()) {
1984 pause_all_vcpus();
1985 cpu_synchronize_all_states();
1986 qemu_system_reset(VMRESET_SILENT);
1987 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1988 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1989 resume_all_vcpus();
1990 qapi_event_send_wakeup(&error_abort);
1992 if (qemu_powerdown_requested()) {
1993 qemu_system_powerdown();
1995 if (qemu_vmstop_requested(&r)) {
1996 vm_stop(r);
1998 return false;
2001 static void main_loop(void)
2003 bool nonblocking;
2004 int last_io = 0;
2005 #ifdef CONFIG_PROFILER
2006 int64_t ti;
2007 #endif
2008 do {
2009 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2010 #ifdef CONFIG_PROFILER
2011 ti = profile_getclock();
2012 #endif
2013 last_io = main_loop_wait(nonblocking);
2014 #ifdef CONFIG_PROFILER
2015 dev_time += profile_getclock() - ti;
2016 #endif
2017 } while (!main_loop_should_exit());
2020 static void version(void)
2022 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2025 static void QEMU_NORETURN help(int exitcode)
2027 version();
2028 printf("usage: %s [options] [disk_image]\n\n"
2029 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2030 error_get_progname());
2032 #define QEMU_OPTIONS_GENERATE_HELP
2033 #include "qemu-options-wrapper.h"
2035 printf("\nDuring emulation, the following keys are useful:\n"
2036 "ctrl-alt-f toggle full screen\n"
2037 "ctrl-alt-n switch to virtual console 'n'\n"
2038 "ctrl-alt toggle mouse and keyboard grab\n"
2039 "\n"
2040 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2042 exit(exitcode);
2045 #define HAS_ARG 0x0001
2047 typedef struct QEMUOption {
2048 const char *name;
2049 int flags;
2050 int index;
2051 uint32_t arch_mask;
2052 } QEMUOption;
2054 static const QEMUOption qemu_options[] = {
2055 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2056 #define QEMU_OPTIONS_GENERATE_OPTIONS
2057 #include "qemu-options-wrapper.h"
2058 { NULL },
2061 static bool vga_available(void)
2063 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2066 static bool cirrus_vga_available(void)
2068 return object_class_by_name("cirrus-vga")
2069 || object_class_by_name("isa-cirrus-vga");
2072 static bool vmware_vga_available(void)
2074 return object_class_by_name("vmware-svga");
2077 static bool qxl_vga_available(void)
2079 return object_class_by_name("qxl-vga");
2082 static bool tcx_vga_available(void)
2084 return object_class_by_name("SUNW,tcx");
2087 static bool cg3_vga_available(void)
2089 return object_class_by_name("cgthree");
2092 static void select_vgahw (const char *p)
2094 const char *opts;
2096 assert(vga_interface_type == VGA_NONE);
2097 if (strstart(p, "std", &opts)) {
2098 if (vga_available()) {
2099 vga_interface_type = VGA_STD;
2100 } else {
2101 fprintf(stderr, "Error: standard VGA not available\n");
2102 exit(0);
2104 } else if (strstart(p, "cirrus", &opts)) {
2105 if (cirrus_vga_available()) {
2106 vga_interface_type = VGA_CIRRUS;
2107 } else {
2108 fprintf(stderr, "Error: Cirrus VGA not available\n");
2109 exit(0);
2111 } else if (strstart(p, "vmware", &opts)) {
2112 if (vmware_vga_available()) {
2113 vga_interface_type = VGA_VMWARE;
2114 } else {
2115 fprintf(stderr, "Error: VMWare SVGA not available\n");
2116 exit(0);
2118 } else if (strstart(p, "xenfb", &opts)) {
2119 vga_interface_type = VGA_XENFB;
2120 } else if (strstart(p, "qxl", &opts)) {
2121 if (qxl_vga_available()) {
2122 vga_interface_type = VGA_QXL;
2123 } else {
2124 fprintf(stderr, "Error: QXL VGA not available\n");
2125 exit(0);
2127 } else if (strstart(p, "tcx", &opts)) {
2128 if (tcx_vga_available()) {
2129 vga_interface_type = VGA_TCX;
2130 } else {
2131 fprintf(stderr, "Error: TCX framebuffer not available\n");
2132 exit(0);
2134 } else if (strstart(p, "cg3", &opts)) {
2135 if (cg3_vga_available()) {
2136 vga_interface_type = VGA_CG3;
2137 } else {
2138 fprintf(stderr, "Error: CG3 framebuffer not available\n");
2139 exit(0);
2141 } else if (!strstart(p, "none", &opts)) {
2142 invalid_vga:
2143 fprintf(stderr, "Unknown vga type: %s\n", p);
2144 exit(1);
2146 while (*opts) {
2147 const char *nextopt;
2149 if (strstart(opts, ",retrace=", &nextopt)) {
2150 opts = nextopt;
2151 if (strstart(opts, "dumb", &nextopt))
2152 vga_retrace_method = VGA_RETRACE_DUMB;
2153 else if (strstart(opts, "precise", &nextopt))
2154 vga_retrace_method = VGA_RETRACE_PRECISE;
2155 else goto invalid_vga;
2156 } else goto invalid_vga;
2157 opts = nextopt;
2161 static DisplayType select_display(const char *p)
2163 const char *opts;
2164 DisplayType display = DT_DEFAULT;
2166 if (strstart(p, "sdl", &opts)) {
2167 #ifdef CONFIG_SDL
2168 display = DT_SDL;
2169 while (*opts) {
2170 const char *nextopt;
2172 if (strstart(opts, ",frame=", &nextopt)) {
2173 opts = nextopt;
2174 if (strstart(opts, "on", &nextopt)) {
2175 no_frame = 0;
2176 } else if (strstart(opts, "off", &nextopt)) {
2177 no_frame = 1;
2178 } else {
2179 goto invalid_sdl_args;
2181 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2182 opts = nextopt;
2183 if (strstart(opts, "on", &nextopt)) {
2184 alt_grab = 1;
2185 } else if (strstart(opts, "off", &nextopt)) {
2186 alt_grab = 0;
2187 } else {
2188 goto invalid_sdl_args;
2190 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2191 opts = nextopt;
2192 if (strstart(opts, "on", &nextopt)) {
2193 ctrl_grab = 1;
2194 } else if (strstart(opts, "off", &nextopt)) {
2195 ctrl_grab = 0;
2196 } else {
2197 goto invalid_sdl_args;
2199 } else if (strstart(opts, ",window_close=", &nextopt)) {
2200 opts = nextopt;
2201 if (strstart(opts, "on", &nextopt)) {
2202 no_quit = 0;
2203 } else if (strstart(opts, "off", &nextopt)) {
2204 no_quit = 1;
2205 } else {
2206 goto invalid_sdl_args;
2208 } else {
2209 invalid_sdl_args:
2210 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2211 exit(1);
2213 opts = nextopt;
2215 #else
2216 fprintf(stderr, "SDL support is disabled\n");
2217 exit(1);
2218 #endif
2219 } else if (strstart(p, "vnc", &opts)) {
2220 #ifdef CONFIG_VNC
2221 display_remote++;
2223 if (*opts) {
2224 const char *nextopt;
2226 if (strstart(opts, "=", &nextopt)) {
2227 vnc_display = nextopt;
2230 if (!vnc_display) {
2231 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2232 exit(1);
2234 #else
2235 fprintf(stderr, "VNC support is disabled\n");
2236 exit(1);
2237 #endif
2238 } else if (strstart(p, "curses", &opts)) {
2239 #ifdef CONFIG_CURSES
2240 display = DT_CURSES;
2241 #else
2242 fprintf(stderr, "Curses support is disabled\n");
2243 exit(1);
2244 #endif
2245 } else if (strstart(p, "gtk", &opts)) {
2246 #ifdef CONFIG_GTK
2247 display = DT_GTK;
2248 while (*opts) {
2249 const char *nextopt;
2251 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2252 opts = nextopt;
2253 if (strstart(opts, "on", &nextopt)) {
2254 grab_on_hover = true;
2255 } else if (strstart(opts, "off", &nextopt)) {
2256 grab_on_hover = false;
2257 } else {
2258 goto invalid_gtk_args;
2260 } else {
2261 invalid_gtk_args:
2262 fprintf(stderr, "Invalid GTK option string: %s\n", p);
2263 exit(1);
2265 opts = nextopt;
2267 #else
2268 fprintf(stderr, "GTK support is disabled\n");
2269 exit(1);
2270 #endif
2271 } else if (strstart(p, "none", &opts)) {
2272 display = DT_NONE;
2273 } else {
2274 fprintf(stderr, "Unknown display type: %s\n", p);
2275 exit(1);
2278 return display;
2281 static int balloon_parse(const char *arg)
2283 QemuOpts *opts;
2285 if (strcmp(arg, "none") == 0) {
2286 return 0;
2289 if (!strncmp(arg, "virtio", 6)) {
2290 if (arg[6] == ',') {
2291 /* have params -> parse them */
2292 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2293 if (!opts)
2294 return -1;
2295 } else {
2296 /* create empty opts */
2297 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2298 &error_abort);
2300 qemu_opt_set(opts, "driver", "virtio-balloon");
2301 return 0;
2304 return -1;
2307 #if defined(CONFIG_RUBY)
2308 /* Ruby interface for QEMU. See README.EXT for programming example. */
2309 #warning mit ruby
2310 #include <ruby.h>
2312 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2314 int i;
2315 for (i = 0; i < argc; i++) {
2316 argv[i] = StringValue(argv[i]);
2317 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2319 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2320 //~ mode = 0666; /* default value */
2321 //~ }
2322 return INT2FIX(argc);
2325 void Init_qemu(void)
2327 printf("%s\n", __func__);
2328 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2329 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2330 #if 0
2331 rb_define_method();
2332 #endif
2334 #endif /* CONFIG_RUBY */
2336 #if defined(CONFIG_DLL)
2337 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2339 return FALSE;
2341 #endif /* CONFIG_DLL */
2343 char *qemu_find_file(int type, const char *name)
2345 int i;
2346 const char *subdir;
2347 char *buf;
2349 /* Try the name as a straight path first */
2350 if (access(name, R_OK) == 0) {
2351 trace_load_file(name, name);
2352 return g_strdup(name);
2355 switch (type) {
2356 case QEMU_FILE_TYPE_BIOS:
2357 subdir = "";
2358 break;
2359 case QEMU_FILE_TYPE_KEYMAP:
2360 subdir = "keymaps/";
2361 break;
2362 default:
2363 abort();
2366 for (i = 0; i < data_dir_idx; i++) {
2367 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2368 if (access(buf, R_OK) == 0) {
2369 trace_load_file(name, buf);
2370 return buf;
2372 g_free(buf);
2374 return NULL;
2377 static int device_help_func(QemuOpts *opts, void *opaque)
2379 return qdev_device_help(opts);
2382 static int device_init_func(QemuOpts *opts, void *opaque)
2384 DeviceState *dev;
2386 dev = qdev_device_add(opts);
2387 if (!dev)
2388 return -1;
2389 object_unref(OBJECT(dev));
2390 return 0;
2393 static int chardev_init_func(QemuOpts *opts, void *opaque)
2395 Error *local_err = NULL;
2397 qemu_chr_new_from_opts(opts, NULL, &local_err);
2398 if (local_err) {
2399 error_report("%s", error_get_pretty(local_err));
2400 error_free(local_err);
2401 return -1;
2403 return 0;
2406 #ifdef CONFIG_VIRTFS
2407 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2409 int ret;
2410 ret = qemu_fsdev_add(opts);
2412 return ret;
2414 #endif
2416 static int mon_init_func(QemuOpts *opts, void *opaque)
2418 CharDriverState *chr;
2419 const char *chardev;
2420 const char *mode;
2421 int flags;
2423 mode = qemu_opt_get(opts, "mode");
2424 if (mode == NULL) {
2425 mode = "readline";
2427 if (strcmp(mode, "readline") == 0) {
2428 flags = MONITOR_USE_READLINE;
2429 } else if (strcmp(mode, "control") == 0) {
2430 flags = MONITOR_USE_CONTROL;
2431 } else {
2432 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2433 exit(1);
2436 if (qemu_opt_get_bool(opts, "pretty", 0))
2437 flags |= MONITOR_USE_PRETTY;
2439 if (qemu_opt_get_bool(opts, "default", 0))
2440 flags |= MONITOR_IS_DEFAULT;
2442 chardev = qemu_opt_get(opts, "chardev");
2443 chr = qemu_chr_find(chardev);
2444 if (chr == NULL) {
2445 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2446 exit(1);
2449 qemu_chr_fe_claim_no_fail(chr);
2450 monitor_init(chr, flags);
2451 return 0;
2454 static void monitor_parse(const char *optarg, const char *mode)
2456 static int monitor_device_index = 0;
2457 QemuOpts *opts;
2458 const char *p;
2459 char label[32];
2460 int def = 0;
2462 if (strstart(optarg, "chardev:", &p)) {
2463 snprintf(label, sizeof(label), "%s", p);
2464 } else {
2465 snprintf(label, sizeof(label), "compat_monitor%d",
2466 monitor_device_index);
2467 if (monitor_device_index == 0) {
2468 def = 1;
2470 opts = qemu_chr_parse_compat(label, optarg);
2471 if (!opts) {
2472 fprintf(stderr, "parse error: %s\n", optarg);
2473 exit(1);
2477 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2478 if (!opts) {
2479 fprintf(stderr, "duplicate chardev: %s\n", label);
2480 exit(1);
2482 qemu_opt_set(opts, "mode", mode);
2483 qemu_opt_set(opts, "chardev", label);
2484 if (def)
2485 qemu_opt_set(opts, "default", "on");
2486 monitor_device_index++;
2489 struct device_config {
2490 enum {
2491 DEV_USB, /* -usbdevice */
2492 DEV_BT, /* -bt */
2493 DEV_SERIAL, /* -serial */
2494 DEV_PARALLEL, /* -parallel */
2495 DEV_VIRTCON, /* -virtioconsole */
2496 DEV_DEBUGCON, /* -debugcon */
2497 DEV_GDB, /* -gdb, -s */
2498 DEV_SCLP, /* s390 sclp */
2499 } type;
2500 const char *cmdline;
2501 Location loc;
2502 QTAILQ_ENTRY(device_config) next;
2505 static QTAILQ_HEAD(, device_config) device_configs =
2506 QTAILQ_HEAD_INITIALIZER(device_configs);
2508 static void add_device_config(int type, const char *cmdline)
2510 struct device_config *conf;
2512 conf = g_malloc0(sizeof(*conf));
2513 conf->type = type;
2514 conf->cmdline = cmdline;
2515 loc_save(&conf->loc);
2516 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2519 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2521 struct device_config *conf;
2522 int rc;
2524 QTAILQ_FOREACH(conf, &device_configs, next) {
2525 if (conf->type != type)
2526 continue;
2527 loc_push_restore(&conf->loc);
2528 rc = func(conf->cmdline);
2529 loc_pop(&conf->loc);
2530 if (0 != rc)
2531 return rc;
2533 return 0;
2536 static int serial_parse(const char *devname)
2538 static int index = 0;
2539 char label[32];
2541 if (strcmp(devname, "none") == 0)
2542 return 0;
2543 if (index == MAX_SERIAL_PORTS) {
2544 fprintf(stderr, "qemu: too many serial ports\n");
2545 exit(1);
2547 snprintf(label, sizeof(label), "serial%d", index);
2548 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2549 if (!serial_hds[index]) {
2550 fprintf(stderr, "qemu: could not connect serial device"
2551 " to character backend '%s'\n", devname);
2552 return -1;
2554 index++;
2555 return 0;
2558 static int parallel_parse(const char *devname)
2560 static int index = 0;
2561 char label[32];
2563 if (strcmp(devname, "none") == 0)
2564 return 0;
2565 if (index == MAX_PARALLEL_PORTS) {
2566 fprintf(stderr, "qemu: too many parallel ports\n");
2567 exit(1);
2569 snprintf(label, sizeof(label), "parallel%d", index);
2570 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2571 if (!parallel_hds[index]) {
2572 fprintf(stderr, "qemu: could not connect parallel device"
2573 " to character backend '%s'\n", devname);
2574 return -1;
2576 index++;
2577 return 0;
2580 static int virtcon_parse(const char *devname)
2582 QemuOptsList *device = qemu_find_opts("device");
2583 static int index = 0;
2584 char label[32];
2585 QemuOpts *bus_opts, *dev_opts;
2587 if (strcmp(devname, "none") == 0)
2588 return 0;
2589 if (index == MAX_VIRTIO_CONSOLES) {
2590 fprintf(stderr, "qemu: too many virtio consoles\n");
2591 exit(1);
2594 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2595 if (arch_type == QEMU_ARCH_S390X) {
2596 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2597 } else {
2598 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2601 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2602 qemu_opt_set(dev_opts, "driver", "virtconsole");
2604 snprintf(label, sizeof(label), "virtcon%d", index);
2605 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2606 if (!virtcon_hds[index]) {
2607 fprintf(stderr, "qemu: could not connect virtio console"
2608 " to character backend '%s'\n", devname);
2609 return -1;
2611 qemu_opt_set(dev_opts, "chardev", label);
2613 index++;
2614 return 0;
2617 static int sclp_parse(const char *devname)
2619 QemuOptsList *device = qemu_find_opts("device");
2620 static int index = 0;
2621 char label[32];
2622 QemuOpts *dev_opts;
2624 if (strcmp(devname, "none") == 0) {
2625 return 0;
2627 if (index == MAX_SCLP_CONSOLES) {
2628 fprintf(stderr, "qemu: too many sclp consoles\n");
2629 exit(1);
2632 assert(arch_type == QEMU_ARCH_S390X);
2634 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2635 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2637 snprintf(label, sizeof(label), "sclpcon%d", index);
2638 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2639 if (!sclp_hds[index]) {
2640 fprintf(stderr, "qemu: could not connect sclp console"
2641 " to character backend '%s'\n", devname);
2642 return -1;
2644 qemu_opt_set(dev_opts, "chardev", label);
2646 index++;
2647 return 0;
2650 static int debugcon_parse(const char *devname)
2652 QemuOpts *opts;
2654 if (!qemu_chr_new("debugcon", devname, NULL)) {
2655 exit(1);
2657 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2658 if (!opts) {
2659 fprintf(stderr, "qemu: already have a debugcon device\n");
2660 exit(1);
2662 qemu_opt_set(opts, "driver", "isa-debugcon");
2663 qemu_opt_set(opts, "chardev", "debugcon");
2664 return 0;
2667 static MachineClass *machine_parse(const char *name)
2669 MachineClass *mc = NULL;
2670 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2672 if (name) {
2673 mc = find_machine(name);
2675 if (mc) {
2676 return mc;
2678 if (name && !is_help_option(name)) {
2679 error_report("Unsupported machine type");
2680 error_printf("Use -machine help to list supported machines!\n");
2681 } else {
2682 printf("Supported machines are:\n");
2683 for (el = machines; el; el = el->next) {
2684 MachineClass *mc = el->data;
2685 if (mc->alias) {
2686 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2688 printf("%-20s %s%s\n", mc->name, mc->desc,
2689 mc->is_default ? " (default)" : "");
2693 g_slist_free(machines);
2694 exit(!name || !is_help_option(name));
2697 static int tcg_init(MachineClass *mc)
2699 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2700 return 0;
2703 static struct {
2704 const char *opt_name;
2705 const char *name;
2706 int (*available)(void);
2707 int (*init)(MachineClass *mc);
2708 bool *allowed;
2709 } accel_list[] = {
2710 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2711 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2712 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2713 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2716 static int configure_accelerator(MachineClass *mc)
2718 const char *p;
2719 char buf[10];
2720 int i, ret;
2721 bool accel_initialised = false;
2722 bool init_failed = false;
2724 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2725 if (p == NULL) {
2726 /* Use the default "accelerator", tcg */
2727 p = "tcg";
2730 while (!accel_initialised && *p != '\0') {
2731 if (*p == ':') {
2732 p++;
2734 p = get_opt_name(buf, sizeof (buf), p, ':');
2735 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2736 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2737 if (!accel_list[i].available()) {
2738 printf("%s not supported for this target\n",
2739 accel_list[i].name);
2740 break;
2742 *(accel_list[i].allowed) = true;
2743 ret = accel_list[i].init(mc);
2744 if (ret < 0) {
2745 init_failed = true;
2746 fprintf(stderr, "failed to initialize %s: %s\n",
2747 accel_list[i].name,
2748 strerror(-ret));
2749 *(accel_list[i].allowed) = false;
2750 } else {
2751 accel_initialised = true;
2753 break;
2756 if (i == ARRAY_SIZE(accel_list)) {
2757 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2761 if (!accel_initialised) {
2762 if (!init_failed) {
2763 fprintf(stderr, "No accelerator found!\n");
2765 exit(1);
2768 if (init_failed) {
2769 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2772 return !accel_initialised;
2775 void qemu_add_exit_notifier(Notifier *notify)
2777 notifier_list_add(&exit_notifiers, notify);
2780 void qemu_remove_exit_notifier(Notifier *notify)
2782 notifier_remove(notify);
2785 static void qemu_run_exit_notifiers(void)
2787 notifier_list_notify(&exit_notifiers, NULL);
2790 void qemu_add_machine_init_done_notifier(Notifier *notify)
2792 notifier_list_add(&machine_init_done_notifiers, notify);
2795 static void qemu_run_machine_init_done_notifiers(void)
2797 notifier_list_notify(&machine_init_done_notifiers, NULL);
2800 static const QEMUOption *lookup_opt(int argc, char **argv,
2801 const char **poptarg, int *poptind)
2803 const QEMUOption *popt;
2804 int optind = *poptind;
2805 char *r = argv[optind];
2806 const char *optarg;
2808 loc_set_cmdline(argv, optind, 1);
2809 optind++;
2810 /* Treat --foo the same as -foo. */
2811 if (r[1] == '-')
2812 r++;
2813 popt = qemu_options;
2814 for(;;) {
2815 if (!popt->name) {
2816 error_report("invalid option");
2817 exit(1);
2819 if (!strcmp(popt->name, r + 1))
2820 break;
2821 popt++;
2823 if (popt->flags & HAS_ARG) {
2824 if (optind >= argc) {
2825 error_report("requires an argument");
2826 exit(1);
2828 optarg = argv[optind++];
2829 loc_set_cmdline(argv, optind - 2, 2);
2830 } else {
2831 optarg = NULL;
2834 *poptarg = optarg;
2835 *poptind = optind;
2837 return popt;
2840 static gpointer malloc_and_trace(gsize n_bytes)
2842 void *ptr = malloc(n_bytes);
2843 trace_g_malloc(n_bytes, ptr);
2844 return ptr;
2847 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2849 void *ptr = realloc(mem, n_bytes);
2850 trace_g_realloc(mem, n_bytes, ptr);
2851 return ptr;
2854 static void free_and_trace(gpointer mem)
2856 trace_g_free(mem);
2857 free(mem);
2860 static int object_set_property(const char *name, const char *value, void *opaque)
2862 Object *obj = OBJECT(opaque);
2863 StringInputVisitor *siv;
2864 Error *local_err = NULL;
2866 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0 ||
2867 strcmp(name, "type") == 0) {
2868 return 0;
2871 siv = string_input_visitor_new(value);
2872 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2873 string_input_visitor_cleanup(siv);
2875 if (local_err) {
2876 qerror_report_err(local_err);
2877 error_free(local_err);
2878 return -1;
2881 return 0;
2884 static int object_create(QemuOpts *opts, void *opaque)
2886 Error *err = NULL;
2887 char *type = NULL;
2888 char *id = NULL;
2889 void *dummy = NULL;
2890 OptsVisitor *ov;
2891 QDict *pdict;
2893 ov = opts_visitor_new(opts);
2894 pdict = qemu_opts_to_qdict(opts, NULL);
2896 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
2897 if (err) {
2898 goto out;
2901 qdict_del(pdict, "qom-type");
2902 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
2903 if (err) {
2904 goto out;
2907 qdict_del(pdict, "id");
2908 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
2909 if (err) {
2910 goto out;
2913 object_add(type, id, pdict, opts_get_visitor(ov), &err);
2914 if (err) {
2915 goto out;
2917 visit_end_struct(opts_get_visitor(ov), &err);
2918 if (err) {
2919 qmp_object_del(id, NULL);
2922 out:
2923 opts_visitor_cleanup(ov);
2925 QDECREF(pdict);
2926 g_free(id);
2927 g_free(type);
2928 g_free(dummy);
2929 if (err) {
2930 qerror_report_err(err);
2931 return -1;
2933 return 0;
2936 int main(int argc, char **argv)
2938 int i;
2939 int snapshot, linux_boot;
2940 const char *icount_option = NULL;
2941 const char *initrd_filename;
2942 const char *kernel_filename, *kernel_cmdline;
2943 const char *boot_order;
2944 DisplayState *ds;
2945 int cyls, heads, secs, translation;
2946 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2947 QemuOptsList *olist;
2948 int optind;
2949 const char *optarg;
2950 const char *loadvm = NULL;
2951 MachineClass *machine_class;
2952 const char *cpu_model;
2953 const char *vga_model = NULL;
2954 const char *qtest_chrdev = NULL;
2955 const char *qtest_log = NULL;
2956 const char *pid_file = NULL;
2957 const char *incoming = NULL;
2958 #ifdef CONFIG_VNC
2959 int show_vnc_port = 0;
2960 #endif
2961 bool defconfig = true;
2962 bool userconfig = true;
2963 const char *log_mask = NULL;
2964 const char *log_file = NULL;
2965 GMemVTable mem_trace = {
2966 .malloc = malloc_and_trace,
2967 .realloc = realloc_and_trace,
2968 .free = free_and_trace,
2970 const char *trace_events = NULL;
2971 const char *trace_file = NULL;
2972 const ram_addr_t default_ram_size = (ram_addr_t)DEFAULT_RAM_SIZE *
2973 1024 * 1024;
2974 ram_addr_t maxram_size = default_ram_size;
2975 uint64_t ram_slots = 0;
2976 FILE *vmstate_dump_file = NULL;
2978 atexit(qemu_run_exit_notifiers);
2979 error_set_progname(argv[0]);
2980 qemu_init_exec_dir(argv[0]);
2982 g_mem_set_vtable(&mem_trace);
2984 module_call_init(MODULE_INIT_QOM);
2986 qemu_add_opts(&qemu_drive_opts);
2987 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2988 qemu_add_drive_opts(&qemu_common_drive_opts);
2989 qemu_add_drive_opts(&qemu_drive_opts);
2990 qemu_add_opts(&qemu_chardev_opts);
2991 qemu_add_opts(&qemu_device_opts);
2992 qemu_add_opts(&qemu_netdev_opts);
2993 qemu_add_opts(&qemu_net_opts);
2994 qemu_add_opts(&qemu_rtc_opts);
2995 qemu_add_opts(&qemu_global_opts);
2996 qemu_add_opts(&qemu_mon_opts);
2997 qemu_add_opts(&qemu_trace_opts);
2998 qemu_add_opts(&qemu_option_rom_opts);
2999 qemu_add_opts(&qemu_machine_opts);
3000 qemu_add_opts(&qemu_mem_opts);
3001 qemu_add_opts(&qemu_smp_opts);
3002 qemu_add_opts(&qemu_boot_opts);
3003 qemu_add_opts(&qemu_sandbox_opts);
3004 qemu_add_opts(&qemu_add_fd_opts);
3005 qemu_add_opts(&qemu_object_opts);
3006 qemu_add_opts(&qemu_tpmdev_opts);
3007 qemu_add_opts(&qemu_realtime_opts);
3008 qemu_add_opts(&qemu_msg_opts);
3009 qemu_add_opts(&qemu_name_opts);
3010 qemu_add_opts(&qemu_numa_opts);
3012 runstate_init();
3014 rtc_clock = QEMU_CLOCK_HOST;
3016 QLIST_INIT (&vm_change_state_head);
3017 os_setup_early_signal_handling();
3019 module_call_init(MODULE_INIT_MACHINE);
3020 machine_class = 0;
3021 optarg = strrchr(argv[0], '/');
3022 if (optarg != 0) {
3023 machine_class = find_machine(optarg + 1);
3025 if (!machine_class) {
3026 machine_class = find_default_machine();
3028 cpu_model = NULL;
3029 ram_size = default_ram_size;
3030 snapshot = 0;
3031 cyls = heads = secs = 0;
3032 translation = BIOS_ATA_TRANSLATION_AUTO;
3034 for (i = 0; i < MAX_NODES; i++) {
3035 numa_info[i].node_mem = 0;
3036 numa_info[i].present = false;
3037 bitmap_zero(numa_info[i].node_cpu, MAX_CPUMASK_BITS);
3040 nb_numa_nodes = 0;
3041 max_numa_nodeid = 0;
3042 nb_nics = 0;
3044 bdrv_init_with_whitelist();
3046 autostart = 1;
3048 /* first pass of option parsing */
3049 optind = 1;
3050 while (optind < argc) {
3051 if (argv[optind][0] != '-') {
3052 /* disk image */
3053 optind++;
3054 } else {
3055 const QEMUOption *popt;
3057 popt = lookup_opt(argc, argv, &optarg, &optind);
3058 switch (popt->index) {
3059 case QEMU_OPTION_nodefconfig:
3060 defconfig = false;
3061 break;
3062 case QEMU_OPTION_nouserconfig:
3063 userconfig = false;
3064 break;
3069 if (defconfig) {
3070 int ret;
3071 ret = qemu_read_default_config_files(userconfig);
3072 if (ret < 0) {
3073 exit(1);
3077 /* second pass of option parsing */
3078 optind = 1;
3079 for(;;) {
3080 if (optind >= argc)
3081 break;
3082 if (argv[optind][0] != '-') {
3083 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3084 } else {
3085 const QEMUOption *popt;
3087 popt = lookup_opt(argc, argv, &optarg, &optind);
3088 if (!(popt->arch_mask & arch_type)) {
3089 printf("Option %s not supported for this target\n", popt->name);
3090 exit(1);
3092 switch(popt->index) {
3093 case QEMU_OPTION_M:
3094 machine_class = machine_parse(optarg);
3095 break;
3096 case QEMU_OPTION_no_kvm_irqchip: {
3097 olist = qemu_find_opts("machine");
3098 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3099 break;
3101 case QEMU_OPTION_cpu:
3102 /* hw initialization will check this */
3103 cpu_model = optarg;
3104 break;
3105 case QEMU_OPTION_hda:
3107 char buf[256];
3108 if (cyls == 0)
3109 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3110 else
3111 snprintf(buf, sizeof(buf),
3112 "%s,cyls=%d,heads=%d,secs=%d%s",
3113 HD_OPTS , cyls, heads, secs,
3114 translation == BIOS_ATA_TRANSLATION_LBA ?
3115 ",trans=lba" :
3116 translation == BIOS_ATA_TRANSLATION_NONE ?
3117 ",trans=none" : "");
3118 drive_add(IF_DEFAULT, 0, optarg, buf);
3119 break;
3121 case QEMU_OPTION_hdb:
3122 case QEMU_OPTION_hdc:
3123 case QEMU_OPTION_hdd:
3124 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3125 HD_OPTS);
3126 break;
3127 case QEMU_OPTION_drive:
3128 if (drive_def(optarg) == NULL) {
3129 exit(1);
3131 break;
3132 case QEMU_OPTION_set:
3133 if (qemu_set_option(optarg) != 0)
3134 exit(1);
3135 break;
3136 case QEMU_OPTION_global:
3137 if (qemu_global_option(optarg) != 0)
3138 exit(1);
3139 break;
3140 case QEMU_OPTION_mtdblock:
3141 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3142 break;
3143 case QEMU_OPTION_sd:
3144 drive_add(IF_SD, -1, optarg, SD_OPTS);
3145 break;
3146 case QEMU_OPTION_pflash:
3147 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3148 break;
3149 case QEMU_OPTION_snapshot:
3150 snapshot = 1;
3151 break;
3152 case QEMU_OPTION_hdachs:
3154 const char *p;
3155 p = optarg;
3156 cyls = strtol(p, (char **)&p, 0);
3157 if (cyls < 1 || cyls > 16383)
3158 goto chs_fail;
3159 if (*p != ',')
3160 goto chs_fail;
3161 p++;
3162 heads = strtol(p, (char **)&p, 0);
3163 if (heads < 1 || heads > 16)
3164 goto chs_fail;
3165 if (*p != ',')
3166 goto chs_fail;
3167 p++;
3168 secs = strtol(p, (char **)&p, 0);
3169 if (secs < 1 || secs > 63)
3170 goto chs_fail;
3171 if (*p == ',') {
3172 p++;
3173 if (!strcmp(p, "large")) {
3174 translation = BIOS_ATA_TRANSLATION_LARGE;
3175 } else if (!strcmp(p, "rechs")) {
3176 translation = BIOS_ATA_TRANSLATION_RECHS;
3177 } else if (!strcmp(p, "none")) {
3178 translation = BIOS_ATA_TRANSLATION_NONE;
3179 } else if (!strcmp(p, "lba")) {
3180 translation = BIOS_ATA_TRANSLATION_LBA;
3181 } else if (!strcmp(p, "auto")) {
3182 translation = BIOS_ATA_TRANSLATION_AUTO;
3183 } else {
3184 goto chs_fail;
3186 } else if (*p != '\0') {
3187 chs_fail:
3188 fprintf(stderr, "qemu: invalid physical CHS format\n");
3189 exit(1);
3191 if (hda_opts != NULL) {
3192 char num[16];
3193 snprintf(num, sizeof(num), "%d", cyls);
3194 qemu_opt_set(hda_opts, "cyls", num);
3195 snprintf(num, sizeof(num), "%d", heads);
3196 qemu_opt_set(hda_opts, "heads", num);
3197 snprintf(num, sizeof(num), "%d", secs);
3198 qemu_opt_set(hda_opts, "secs", num);
3199 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3200 qemu_opt_set(hda_opts, "trans", "large");
3201 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3202 qemu_opt_set(hda_opts, "trans", "rechs");
3203 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3204 qemu_opt_set(hda_opts, "trans", "lba");
3205 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3206 qemu_opt_set(hda_opts, "trans", "none");
3210 break;
3211 case QEMU_OPTION_numa:
3212 opts = qemu_opts_parse(qemu_find_opts("numa"), optarg, 1);
3213 if (!opts) {
3214 exit(1);
3216 break;
3217 case QEMU_OPTION_display:
3218 display_type = select_display(optarg);
3219 break;
3220 case QEMU_OPTION_nographic:
3221 display_type = DT_NOGRAPHIC;
3222 break;
3223 case QEMU_OPTION_curses:
3224 #ifdef CONFIG_CURSES
3225 display_type = DT_CURSES;
3226 #else
3227 fprintf(stderr, "Curses support is disabled\n");
3228 exit(1);
3229 #endif
3230 break;
3231 case QEMU_OPTION_portrait:
3232 graphic_rotate = 90;
3233 break;
3234 case QEMU_OPTION_rotate:
3235 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3236 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3237 graphic_rotate != 180 && graphic_rotate != 270) {
3238 fprintf(stderr,
3239 "qemu: only 90, 180, 270 deg rotation is available\n");
3240 exit(1);
3242 break;
3243 case QEMU_OPTION_kernel:
3244 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3245 break;
3246 case QEMU_OPTION_initrd:
3247 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3248 break;
3249 case QEMU_OPTION_append:
3250 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3251 break;
3252 case QEMU_OPTION_dtb:
3253 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3254 break;
3255 case QEMU_OPTION_cdrom:
3256 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3257 break;
3258 case QEMU_OPTION_boot:
3259 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3260 if (!opts) {
3261 exit(1);
3263 break;
3264 case QEMU_OPTION_fda:
3265 case QEMU_OPTION_fdb:
3266 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3267 optarg, FD_OPTS);
3268 break;
3269 case QEMU_OPTION_no_fd_bootchk:
3270 fd_bootchk = 0;
3271 break;
3272 case QEMU_OPTION_netdev:
3273 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3274 exit(1);
3276 break;
3277 case QEMU_OPTION_net:
3278 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3279 exit(1);
3281 break;
3282 #ifdef CONFIG_LIBISCSI
3283 case QEMU_OPTION_iscsi:
3284 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3285 if (!opts) {
3286 exit(1);
3288 break;
3289 #endif
3290 #ifdef CONFIG_SLIRP
3291 case QEMU_OPTION_tftp:
3292 legacy_tftp_prefix = optarg;
3293 break;
3294 case QEMU_OPTION_bootp:
3295 legacy_bootp_filename = optarg;
3296 break;
3297 case QEMU_OPTION_redir:
3298 if (net_slirp_redir(optarg) < 0)
3299 exit(1);
3300 break;
3301 #endif
3302 case QEMU_OPTION_bt:
3303 add_device_config(DEV_BT, optarg);
3304 break;
3305 case QEMU_OPTION_audio_help:
3306 AUD_help ();
3307 exit (0);
3308 break;
3309 case QEMU_OPTION_soundhw:
3310 select_soundhw (optarg);
3311 break;
3312 case QEMU_OPTION_h:
3313 help(0);
3314 break;
3315 case QEMU_OPTION_version:
3316 version();
3317 exit(0);
3318 break;
3319 case QEMU_OPTION_m: {
3320 uint64_t sz;
3321 const char *mem_str;
3322 const char *maxmem_str, *slots_str;
3324 opts = qemu_opts_parse(qemu_find_opts("memory"),
3325 optarg, 1);
3326 if (!opts) {
3327 exit(EXIT_FAILURE);
3330 mem_str = qemu_opt_get(opts, "size");
3331 if (!mem_str) {
3332 error_report("invalid -m option, missing 'size' option");
3333 exit(EXIT_FAILURE);
3335 if (!*mem_str) {
3336 error_report("missing 'size' option value");
3337 exit(EXIT_FAILURE);
3340 sz = qemu_opt_get_size(opts, "size", ram_size);
3342 /* Fix up legacy suffix-less format */
3343 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
3344 uint64_t overflow_check = sz;
3346 sz <<= 20;
3347 if ((sz >> 20) != overflow_check) {
3348 error_report("too large 'size' option value");
3349 exit(EXIT_FAILURE);
3353 /* backward compatibility behaviour for case "-m 0" */
3354 if (sz == 0) {
3355 sz = default_ram_size;
3358 sz = QEMU_ALIGN_UP(sz, 8192);
3359 ram_size = sz;
3360 if (ram_size != sz) {
3361 error_report("ram size too large");
3362 exit(EXIT_FAILURE);
3365 maxmem_str = qemu_opt_get(opts, "maxmem");
3366 slots_str = qemu_opt_get(opts, "slots");
3367 if (maxmem_str && slots_str) {
3368 uint64_t slots;
3370 sz = qemu_opt_get_size(opts, "maxmem", 0);
3371 if (sz < ram_size) {
3372 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3373 "(%" PRIu64 ") <= initial memory ("
3374 RAM_ADDR_FMT ")\n", sz, ram_size);
3375 exit(EXIT_FAILURE);
3378 slots = qemu_opt_get_number(opts, "slots", 0);
3379 if ((sz > ram_size) && !slots) {
3380 fprintf(stderr, "qemu: invalid -m option value: maxmem "
3381 "(%" PRIu64 ") more than initial memory ("
3382 RAM_ADDR_FMT ") but no hotplug slots where "
3383 "specified\n", sz, ram_size);
3384 exit(EXIT_FAILURE);
3387 if ((sz <= ram_size) && slots) {
3388 fprintf(stderr, "qemu: invalid -m option value: %"
3389 PRIu64 " hotplug slots where specified but "
3390 "maxmem (%" PRIu64 ") <= initial memory ("
3391 RAM_ADDR_FMT ")\n", slots, sz, ram_size);
3392 exit(EXIT_FAILURE);
3394 maxram_size = sz;
3395 ram_slots = slots;
3396 } else if ((!maxmem_str && slots_str) ||
3397 (maxmem_str && !slots_str)) {
3398 fprintf(stderr, "qemu: invalid -m option value: missing "
3399 "'%s' option\n", slots_str ? "maxmem" : "slots");
3400 exit(EXIT_FAILURE);
3402 break;
3404 #ifdef CONFIG_TPM
3405 case QEMU_OPTION_tpmdev:
3406 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3407 exit(1);
3409 break;
3410 #endif
3411 case QEMU_OPTION_mempath:
3412 mem_path = optarg;
3413 break;
3414 case QEMU_OPTION_mem_prealloc:
3415 mem_prealloc = 1;
3416 break;
3417 case QEMU_OPTION_d:
3418 log_mask = optarg;
3419 break;
3420 case QEMU_OPTION_D:
3421 log_file = optarg;
3422 break;
3423 case QEMU_OPTION_s:
3424 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3425 break;
3426 case QEMU_OPTION_gdb:
3427 add_device_config(DEV_GDB, optarg);
3428 break;
3429 case QEMU_OPTION_L:
3430 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3431 data_dir[data_dir_idx++] = optarg;
3433 break;
3434 case QEMU_OPTION_bios:
3435 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3436 break;
3437 case QEMU_OPTION_singlestep:
3438 singlestep = 1;
3439 break;
3440 case QEMU_OPTION_S:
3441 autostart = 0;
3442 break;
3443 case QEMU_OPTION_k:
3444 keyboard_layout = optarg;
3445 break;
3446 case QEMU_OPTION_localtime:
3447 rtc_utc = 0;
3448 break;
3449 case QEMU_OPTION_vga:
3450 vga_model = optarg;
3451 default_vga = 0;
3452 break;
3453 case QEMU_OPTION_g:
3455 const char *p;
3456 int w, h, depth;
3457 p = optarg;
3458 w = strtol(p, (char **)&p, 10);
3459 if (w <= 0) {
3460 graphic_error:
3461 fprintf(stderr, "qemu: invalid resolution or depth\n");
3462 exit(1);
3464 if (*p != 'x')
3465 goto graphic_error;
3466 p++;
3467 h = strtol(p, (char **)&p, 10);
3468 if (h <= 0)
3469 goto graphic_error;
3470 if (*p == 'x') {
3471 p++;
3472 depth = strtol(p, (char **)&p, 10);
3473 if (depth != 8 && depth != 15 && depth != 16 &&
3474 depth != 24 && depth != 32)
3475 goto graphic_error;
3476 } else if (*p == '\0') {
3477 depth = graphic_depth;
3478 } else {
3479 goto graphic_error;
3482 graphic_width = w;
3483 graphic_height = h;
3484 graphic_depth = depth;
3486 break;
3487 case QEMU_OPTION_echr:
3489 char *r;
3490 term_escape_char = strtol(optarg, &r, 0);
3491 if (r == optarg)
3492 printf("Bad argument to echr\n");
3493 break;
3495 case QEMU_OPTION_monitor:
3496 default_monitor = 0;
3497 if (strncmp(optarg, "none", 4)) {
3498 monitor_parse(optarg, "readline");
3500 break;
3501 case QEMU_OPTION_qmp:
3502 monitor_parse(optarg, "control");
3503 default_monitor = 0;
3504 break;
3505 case QEMU_OPTION_mon:
3506 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3507 if (!opts) {
3508 exit(1);
3510 default_monitor = 0;
3511 break;
3512 case QEMU_OPTION_chardev:
3513 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3514 if (!opts) {
3515 exit(1);
3517 break;
3518 case QEMU_OPTION_fsdev:
3519 olist = qemu_find_opts("fsdev");
3520 if (!olist) {
3521 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3522 exit(1);
3524 opts = qemu_opts_parse(olist, optarg, 1);
3525 if (!opts) {
3526 exit(1);
3528 break;
3529 case QEMU_OPTION_virtfs: {
3530 QemuOpts *fsdev;
3531 QemuOpts *device;
3532 const char *writeout, *sock_fd, *socket;
3534 olist = qemu_find_opts("virtfs");
3535 if (!olist) {
3536 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3537 exit(1);
3539 opts = qemu_opts_parse(olist, optarg, 1);
3540 if (!opts) {
3541 exit(1);
3544 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3545 qemu_opt_get(opts, "mount_tag") == NULL) {
3546 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3547 exit(1);
3549 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3550 qemu_opt_get(opts, "mount_tag"),
3551 1, NULL);
3552 if (!fsdev) {
3553 fprintf(stderr, "duplicate fsdev id: %s\n",
3554 qemu_opt_get(opts, "mount_tag"));
3555 exit(1);
3558 writeout = qemu_opt_get(opts, "writeout");
3559 if (writeout) {
3560 #ifdef CONFIG_SYNC_FILE_RANGE
3561 qemu_opt_set(fsdev, "writeout", writeout);
3562 #else
3563 fprintf(stderr, "writeout=immediate not supported on "
3564 "this platform\n");
3565 exit(1);
3566 #endif
3568 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3569 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3570 qemu_opt_set(fsdev, "security_model",
3571 qemu_opt_get(opts, "security_model"));
3572 socket = qemu_opt_get(opts, "socket");
3573 if (socket) {
3574 qemu_opt_set(fsdev, "socket", socket);
3576 sock_fd = qemu_opt_get(opts, "sock_fd");
3577 if (sock_fd) {
3578 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3581 qemu_opt_set_bool(fsdev, "readonly",
3582 qemu_opt_get_bool(opts, "readonly", 0));
3583 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3584 &error_abort);
3585 qemu_opt_set(device, "driver", "virtio-9p-pci");
3586 qemu_opt_set(device, "fsdev",
3587 qemu_opt_get(opts, "mount_tag"));
3588 qemu_opt_set(device, "mount_tag",
3589 qemu_opt_get(opts, "mount_tag"));
3590 break;
3592 case QEMU_OPTION_virtfs_synth: {
3593 QemuOpts *fsdev;
3594 QemuOpts *device;
3596 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3597 1, NULL);
3598 if (!fsdev) {
3599 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3600 exit(1);
3602 qemu_opt_set(fsdev, "fsdriver", "synth");
3604 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3605 &error_abort);
3606 qemu_opt_set(device, "driver", "virtio-9p-pci");
3607 qemu_opt_set(device, "fsdev", "v_synth");
3608 qemu_opt_set(device, "mount_tag", "v_synth");
3609 break;
3611 case QEMU_OPTION_serial:
3612 add_device_config(DEV_SERIAL, optarg);
3613 default_serial = 0;
3614 if (strncmp(optarg, "mon:", 4) == 0) {
3615 default_monitor = 0;
3617 break;
3618 case QEMU_OPTION_watchdog:
3619 if (watchdog) {
3620 fprintf(stderr,
3621 "qemu: only one watchdog option may be given\n");
3622 return 1;
3624 watchdog = optarg;
3625 break;
3626 case QEMU_OPTION_watchdog_action:
3627 if (select_watchdog_action(optarg) == -1) {
3628 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3629 exit(1);
3631 break;
3632 case QEMU_OPTION_virtiocon:
3633 add_device_config(DEV_VIRTCON, optarg);
3634 default_virtcon = 0;
3635 if (strncmp(optarg, "mon:", 4) == 0) {
3636 default_monitor = 0;
3638 break;
3639 case QEMU_OPTION_parallel:
3640 add_device_config(DEV_PARALLEL, optarg);
3641 default_parallel = 0;
3642 if (strncmp(optarg, "mon:", 4) == 0) {
3643 default_monitor = 0;
3645 break;
3646 case QEMU_OPTION_debugcon:
3647 add_device_config(DEV_DEBUGCON, optarg);
3648 break;
3649 case QEMU_OPTION_loadvm:
3650 loadvm = optarg;
3651 break;
3652 case QEMU_OPTION_full_screen:
3653 full_screen = 1;
3654 break;
3655 case QEMU_OPTION_no_frame:
3656 no_frame = 1;
3657 break;
3658 case QEMU_OPTION_alt_grab:
3659 alt_grab = 1;
3660 break;
3661 case QEMU_OPTION_ctrl_grab:
3662 ctrl_grab = 1;
3663 break;
3664 case QEMU_OPTION_no_quit:
3665 no_quit = 1;
3666 break;
3667 case QEMU_OPTION_sdl:
3668 #ifdef CONFIG_SDL
3669 display_type = DT_SDL;
3670 break;
3671 #else
3672 fprintf(stderr, "SDL support is disabled\n");
3673 exit(1);
3674 #endif
3675 case QEMU_OPTION_pidfile:
3676 pid_file = optarg;
3677 break;
3678 case QEMU_OPTION_win2k_hack:
3679 win2k_install_hack = 1;
3680 break;
3681 case QEMU_OPTION_rtc_td_hack: {
3682 static GlobalProperty slew_lost_ticks[] = {
3684 .driver = "mc146818rtc",
3685 .property = "lost_tick_policy",
3686 .value = "slew",
3688 { /* end of list */ }
3691 qdev_prop_register_global_list(slew_lost_ticks);
3692 break;
3694 case QEMU_OPTION_acpitable:
3695 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3696 if (!opts) {
3697 exit(1);
3699 do_acpitable_option(opts);
3700 break;
3701 case QEMU_OPTION_smbios:
3702 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3703 if (!opts) {
3704 exit(1);
3706 do_smbios_option(opts);
3707 break;
3708 case QEMU_OPTION_enable_kvm:
3709 olist = qemu_find_opts("machine");
3710 qemu_opts_parse(olist, "accel=kvm", 0);
3711 break;
3712 case QEMU_OPTION_machine:
3713 olist = qemu_find_opts("machine");
3714 opts = qemu_opts_parse(olist, optarg, 1);
3715 if (!opts) {
3716 exit(1);
3718 optarg = qemu_opt_get(opts, "type");
3719 if (optarg) {
3720 machine_class = machine_parse(optarg);
3722 break;
3723 case QEMU_OPTION_no_kvm:
3724 olist = qemu_find_opts("machine");
3725 qemu_opts_parse(olist, "accel=tcg", 0);
3726 break;
3727 case QEMU_OPTION_no_kvm_pit: {
3728 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3729 "separately.\n");
3730 break;
3732 case QEMU_OPTION_no_kvm_pit_reinjection: {
3733 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3735 .driver = "kvm-pit",
3736 .property = "lost_tick_policy",
3737 .value = "discard",
3739 { /* end of list */ }
3742 fprintf(stderr, "Warning: option deprecated, use "
3743 "lost_tick_policy property of kvm-pit instead.\n");
3744 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3745 break;
3747 case QEMU_OPTION_usb:
3748 olist = qemu_find_opts("machine");
3749 qemu_opts_parse(olist, "usb=on", 0);
3750 break;
3751 case QEMU_OPTION_usbdevice:
3752 olist = qemu_find_opts("machine");
3753 qemu_opts_parse(olist, "usb=on", 0);
3754 add_device_config(DEV_USB, optarg);
3755 break;
3756 case QEMU_OPTION_device:
3757 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3758 exit(1);
3760 break;
3761 case QEMU_OPTION_smp:
3762 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3763 exit(1);
3765 break;
3766 case QEMU_OPTION_vnc:
3767 #ifdef CONFIG_VNC
3768 display_remote++;
3769 vnc_display = optarg;
3770 #else
3771 fprintf(stderr, "VNC support is disabled\n");
3772 exit(1);
3773 #endif
3774 break;
3775 case QEMU_OPTION_no_acpi:
3776 acpi_enabled = 0;
3777 break;
3778 case QEMU_OPTION_no_hpet:
3779 no_hpet = 1;
3780 break;
3781 case QEMU_OPTION_balloon:
3782 if (balloon_parse(optarg) < 0) {
3783 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3784 exit(1);
3786 break;
3787 case QEMU_OPTION_no_reboot:
3788 no_reboot = 1;
3789 break;
3790 case QEMU_OPTION_no_shutdown:
3791 no_shutdown = 1;
3792 break;
3793 case QEMU_OPTION_show_cursor:
3794 cursor_hide = 0;
3795 break;
3796 case QEMU_OPTION_uuid:
3797 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3798 fprintf(stderr, "Fail to parse UUID string."
3799 " Wrong format.\n");
3800 exit(1);
3802 qemu_uuid_set = true;
3803 break;
3804 case QEMU_OPTION_option_rom:
3805 if (nb_option_roms >= MAX_OPTION_ROMS) {
3806 fprintf(stderr, "Too many option ROMs\n");
3807 exit(1);
3809 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3810 if (!opts) {
3811 exit(1);
3813 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3814 option_rom[nb_option_roms].bootindex =
3815 qemu_opt_get_number(opts, "bootindex", -1);
3816 if (!option_rom[nb_option_roms].name) {
3817 fprintf(stderr, "Option ROM file is not specified\n");
3818 exit(1);
3820 nb_option_roms++;
3821 break;
3822 case QEMU_OPTION_semihosting:
3823 semihosting_enabled = 1;
3824 break;
3825 case QEMU_OPTION_tdf:
3826 fprintf(stderr, "Warning: user space PIT time drift fix "
3827 "is no longer supported.\n");
3828 break;
3829 case QEMU_OPTION_name:
3830 opts = qemu_opts_parse(qemu_find_opts("name"), optarg, 1);
3831 if (!opts) {
3832 exit(1);
3834 break;
3835 case QEMU_OPTION_prom_env:
3836 if (nb_prom_envs >= MAX_PROM_ENVS) {
3837 fprintf(stderr, "Too many prom variables\n");
3838 exit(1);
3840 prom_envs[nb_prom_envs] = optarg;
3841 nb_prom_envs++;
3842 break;
3843 case QEMU_OPTION_old_param:
3844 old_param = 1;
3845 break;
3846 case QEMU_OPTION_clock:
3847 /* Clock options no longer exist. Keep this option for
3848 * backward compatibility.
3850 break;
3851 case QEMU_OPTION_startdate:
3852 configure_rtc_date_offset(optarg, 1);
3853 break;
3854 case QEMU_OPTION_rtc:
3855 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3856 if (!opts) {
3857 exit(1);
3859 configure_rtc(opts);
3860 break;
3861 case QEMU_OPTION_tb_size:
3862 tcg_tb_size = strtol(optarg, NULL, 0);
3863 if (tcg_tb_size < 0) {
3864 tcg_tb_size = 0;
3866 break;
3867 case QEMU_OPTION_icount:
3868 icount_option = optarg;
3869 break;
3870 case QEMU_OPTION_incoming:
3871 incoming = optarg;
3872 runstate_set(RUN_STATE_INMIGRATE);
3873 break;
3874 case QEMU_OPTION_nodefaults:
3875 has_defaults = 0;
3876 break;
3877 case QEMU_OPTION_xen_domid:
3878 if (!(xen_available())) {
3879 printf("Option %s not supported for this target\n", popt->name);
3880 exit(1);
3882 xen_domid = atoi(optarg);
3883 break;
3884 case QEMU_OPTION_xen_create:
3885 if (!(xen_available())) {
3886 printf("Option %s not supported for this target\n", popt->name);
3887 exit(1);
3889 xen_mode = XEN_CREATE;
3890 break;
3891 case QEMU_OPTION_xen_attach:
3892 if (!(xen_available())) {
3893 printf("Option %s not supported for this target\n", popt->name);
3894 exit(1);
3896 xen_mode = XEN_ATTACH;
3897 break;
3898 case QEMU_OPTION_trace:
3900 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3901 if (!opts) {
3902 exit(1);
3904 trace_events = qemu_opt_get(opts, "events");
3905 trace_file = qemu_opt_get(opts, "file");
3906 break;
3908 case QEMU_OPTION_trace_unassigned:
3909 trace_unassigned = true;
3910 break;
3911 case QEMU_OPTION_readconfig:
3913 int ret = qemu_read_config_file(optarg);
3914 if (ret < 0) {
3915 fprintf(stderr, "read config %s: %s\n", optarg,
3916 strerror(-ret));
3917 exit(1);
3919 break;
3921 case QEMU_OPTION_spice:
3922 olist = qemu_find_opts("spice");
3923 if (!olist) {
3924 fprintf(stderr, "spice is not supported by this qemu build.\n");
3925 exit(1);
3927 opts = qemu_opts_parse(olist, optarg, 0);
3928 if (!opts) {
3929 exit(1);
3931 display_remote++;
3932 break;
3933 case QEMU_OPTION_writeconfig:
3935 FILE *fp;
3936 if (strcmp(optarg, "-") == 0) {
3937 fp = stdout;
3938 } else {
3939 fp = fopen(optarg, "w");
3940 if (fp == NULL) {
3941 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3942 exit(1);
3945 qemu_config_write(fp);
3946 if (fp != stdout) {
3947 fclose(fp);
3949 break;
3951 case QEMU_OPTION_qtest:
3952 qtest_chrdev = optarg;
3953 break;
3954 case QEMU_OPTION_qtest_log:
3955 qtest_log = optarg;
3956 break;
3957 case QEMU_OPTION_sandbox:
3958 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3959 if (!opts) {
3960 exit(1);
3962 break;
3963 case QEMU_OPTION_add_fd:
3964 #ifndef _WIN32
3965 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3966 if (!opts) {
3967 exit(1);
3969 #else
3970 error_report("File descriptor passing is disabled on this "
3971 "platform");
3972 exit(1);
3973 #endif
3974 break;
3975 case QEMU_OPTION_object:
3976 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3977 if (!opts) {
3978 exit(1);
3980 break;
3981 case QEMU_OPTION_realtime:
3982 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3983 if (!opts) {
3984 exit(1);
3986 configure_realtime(opts);
3987 break;
3988 case QEMU_OPTION_msg:
3989 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3990 if (!opts) {
3991 exit(1);
3993 configure_msg(opts);
3994 break;
3995 case QEMU_OPTION_dump_vmstate:
3996 vmstate_dump_file = fopen(optarg, "w");
3997 if (vmstate_dump_file == NULL) {
3998 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3999 exit(1);
4001 break;
4002 default:
4003 os_parse_cmd_args(popt->index, optarg);
4007 loc_set_none();
4009 os_daemonize();
4011 if (qemu_init_main_loop()) {
4012 fprintf(stderr, "qemu_init_main_loop failed\n");
4013 exit(1);
4016 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
4017 exit(1);
4020 if (qemu_opts_foreach(qemu_find_opts("name"), parse_name, NULL, 1)) {
4021 exit(1);
4024 #ifndef _WIN32
4025 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
4026 exit(1);
4029 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
4030 exit(1);
4032 #endif
4034 if (machine_class == NULL) {
4035 fprintf(stderr, "No machine specified, and there is no default.\n"
4036 "Use -machine help to list supported machines!\n");
4037 exit(1);
4040 current_machine = MACHINE(object_new(object_class_get_name(
4041 OBJECT_CLASS(machine_class))));
4042 object_property_add_child(object_get_root(), "machine",
4043 OBJECT(current_machine), &error_abort);
4044 cpu_exec_init_all();
4046 if (machine_class->hw_version) {
4047 qemu_set_version(machine_class->hw_version);
4050 if (qemu_opts_foreach(qemu_find_opts("object"),
4051 object_create, NULL, 0) != 0) {
4052 exit(1);
4055 /* Init CPU def lists, based on config
4056 * - Must be called after all the qemu_read_config_file() calls
4057 * - Must be called before list_cpus()
4058 * - Must be called before machine->init()
4060 cpudef_init();
4062 if (cpu_model && is_help_option(cpu_model)) {
4063 list_cpus(stdout, &fprintf, cpu_model);
4064 exit(0);
4067 /* Open the logfile at this point, if necessary. We can't open the logfile
4068 * when encountering either of the logging options (-d or -D) because the
4069 * other one may be encountered later on the command line, changing the
4070 * location or level of logging.
4072 if (log_mask) {
4073 int mask;
4074 if (log_file) {
4075 qemu_set_log_filename(log_file);
4078 mask = qemu_str_to_log_mask(log_mask);
4079 if (!mask) {
4080 qemu_print_log_usage(stdout);
4081 exit(1);
4083 qemu_set_log(mask);
4086 if (!is_daemonized()) {
4087 if (!trace_init_backends(trace_events, trace_file)) {
4088 exit(1);
4092 /* If no data_dir is specified then try to find it relative to the
4093 executable path. */
4094 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4095 data_dir[data_dir_idx] = os_find_datadir();
4096 if (data_dir[data_dir_idx] != NULL) {
4097 data_dir_idx++;
4100 /* If all else fails use the install path specified when building. */
4101 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4102 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4105 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4107 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4108 if (smp_cpus > machine_class->max_cpus) {
4109 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
4110 "supported by machine `%s' (%d)\n", smp_cpus,
4111 machine_class->name, machine_class->max_cpus);
4112 exit(1);
4116 * Get the default machine options from the machine if it is not already
4117 * specified either by the configuration file or by the command line.
4119 if (machine_class->default_machine_opts) {
4120 qemu_opts_set_defaults(qemu_find_opts("machine"),
4121 machine_class->default_machine_opts, 0);
4124 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
4125 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
4127 if (!vga_model && !default_vga) {
4128 vga_interface_type = VGA_DEVICE;
4130 if (!has_defaults || machine_class->no_serial) {
4131 default_serial = 0;
4133 if (!has_defaults || machine_class->no_parallel) {
4134 default_parallel = 0;
4136 if (!has_defaults || !machine_class->use_virtcon) {
4137 default_virtcon = 0;
4139 if (!has_defaults || !machine_class->use_sclp) {
4140 default_sclp = 0;
4142 if (!has_defaults || machine_class->no_floppy) {
4143 default_floppy = 0;
4145 if (!has_defaults || machine_class->no_cdrom) {
4146 default_cdrom = 0;
4148 if (!has_defaults || machine_class->no_sdcard) {
4149 default_sdcard = 0;
4151 if (!has_defaults) {
4152 default_monitor = 0;
4153 default_net = 0;
4154 default_vga = 0;
4157 if (is_daemonized()) {
4158 /* According to documentation and historically, -nographic redirects
4159 * serial port, parallel port and monitor to stdio, which does not work
4160 * with -daemonize. We can redirect these to null instead, but since
4161 * -nographic is legacy, let's just error out.
4162 * We disallow -nographic only if all other ports are not redirected
4163 * explicitly, to not break existing legacy setups which uses
4164 * -nographic _and_ redirects all ports explicitly - this is valid
4165 * usage, -nographic is just a no-op in this case.
4167 if (display_type == DT_NOGRAPHIC
4168 && (default_parallel || default_serial
4169 || default_monitor || default_virtcon)) {
4170 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4171 exit(1);
4173 #ifdef CONFIG_CURSES
4174 if (display_type == DT_CURSES) {
4175 fprintf(stderr, "curses display can not be used with -daemonize\n");
4176 exit(1);
4178 #endif
4181 if (display_type == DT_NOGRAPHIC) {
4182 if (default_parallel)
4183 add_device_config(DEV_PARALLEL, "null");
4184 if (default_serial && default_monitor) {
4185 add_device_config(DEV_SERIAL, "mon:stdio");
4186 } else if (default_virtcon && default_monitor) {
4187 add_device_config(DEV_VIRTCON, "mon:stdio");
4188 } else if (default_sclp && default_monitor) {
4189 add_device_config(DEV_SCLP, "mon:stdio");
4190 } else {
4191 if (default_serial)
4192 add_device_config(DEV_SERIAL, "stdio");
4193 if (default_virtcon)
4194 add_device_config(DEV_VIRTCON, "stdio");
4195 if (default_sclp) {
4196 add_device_config(DEV_SCLP, "stdio");
4198 if (default_monitor)
4199 monitor_parse("stdio", "readline");
4201 } else {
4202 if (default_serial)
4203 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4204 if (default_parallel)
4205 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4206 if (default_monitor)
4207 monitor_parse("vc:80Cx24C", "readline");
4208 if (default_virtcon)
4209 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4210 if (default_sclp) {
4211 add_device_config(DEV_SCLP, "vc:80Cx24C");
4215 if (display_type == DT_DEFAULT && !display_remote) {
4216 #if defined(CONFIG_GTK)
4217 display_type = DT_GTK;
4218 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4219 display_type = DT_SDL;
4220 #elif defined(CONFIG_VNC)
4221 vnc_display = "localhost:0,to=99";
4222 show_vnc_port = 1;
4223 #else
4224 display_type = DT_NONE;
4225 #endif
4228 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4229 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4230 "for SDL, ignoring option\n");
4232 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4233 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4234 "ignoring option\n");
4237 #if defined(CONFIG_GTK)
4238 if (display_type == DT_GTK) {
4239 early_gtk_display_init();
4241 #endif
4243 socket_init();
4245 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4246 exit(1);
4247 #ifdef CONFIG_VIRTFS
4248 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4249 exit(1);
4251 #endif
4253 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4254 os_pidfile_error();
4255 exit(1);
4258 /* store value for the future use */
4259 qemu_opt_set_number(qemu_find_opts_singleton("memory"), "size", ram_size);
4261 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4262 != 0) {
4263 exit(0);
4266 machine_opts = qemu_get_machine_opts();
4267 if (qemu_opt_foreach(machine_opts, object_set_property, current_machine,
4268 1) < 0) {
4269 object_unref(OBJECT(current_machine));
4270 exit(1);
4273 configure_accelerator(machine_class);
4275 if (qtest_chrdev) {
4276 Error *local_err = NULL;
4277 qtest_init(qtest_chrdev, qtest_log, &local_err);
4278 if (local_err) {
4279 error_report("%s", error_get_pretty(local_err));
4280 error_free(local_err);
4281 exit(1);
4285 machine_opts = qemu_get_machine_opts();
4286 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4287 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4288 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4289 bios_name = qemu_opt_get(machine_opts, "firmware");
4291 boot_order = machine_class->default_boot_order;
4292 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4293 if (opts) {
4294 char *normal_boot_order;
4295 const char *order, *once;
4297 order = qemu_opt_get(opts, "order");
4298 if (order) {
4299 validate_bootdevices(order);
4300 boot_order = order;
4303 once = qemu_opt_get(opts, "once");
4304 if (once) {
4305 validate_bootdevices(once);
4306 normal_boot_order = g_strdup(boot_order);
4307 boot_order = once;
4308 qemu_register_reset(restore_boot_order, normal_boot_order);
4311 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4312 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4315 if (!kernel_cmdline) {
4316 kernel_cmdline = "";
4317 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4320 linux_boot = (kernel_filename != NULL);
4322 if (!linux_boot && *kernel_cmdline != '\0') {
4323 fprintf(stderr, "-append only allowed with -kernel option\n");
4324 exit(1);
4327 if (!linux_boot && initrd_filename != NULL) {
4328 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4329 exit(1);
4332 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4333 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4334 exit(1);
4337 os_set_line_buffering();
4339 qemu_init_cpu_loop();
4340 qemu_mutex_lock_iothread();
4342 #ifdef CONFIG_SPICE
4343 /* spice needs the timers to be initialized by this point */
4344 qemu_spice_init();
4345 #endif
4347 if (icount_option && (kvm_enabled() || xen_enabled())) {
4348 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4349 exit(1);
4351 configure_icount(icount_option);
4353 /* clean up network at qemu process termination */
4354 atexit(&net_cleanup);
4356 if (net_init_clients() < 0) {
4357 exit(1);
4360 #ifdef CONFIG_TPM
4361 if (tpm_init() < 0) {
4362 exit(1);
4364 #endif
4366 /* init the bluetooth world */
4367 if (foreach_device_config(DEV_BT, bt_parse))
4368 exit(1);
4370 if (!xen_enabled()) {
4371 /* On 32-bit hosts, QEMU is limited by virtual address space */
4372 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4373 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4374 exit(1);
4378 blk_mig_init();
4379 ram_mig_init();
4381 /* open the virtual block devices */
4382 if (snapshot)
4383 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4384 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4385 &machine_class->block_default_type, 1) != 0) {
4386 exit(1);
4389 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4390 CDROM_OPTS);
4391 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4392 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4394 if (qemu_opts_foreach(qemu_find_opts("numa"), numa_init_func,
4395 NULL, 1) != 0) {
4396 exit(1);
4399 set_numa_nodes();
4401 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4402 exit(1);
4405 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4406 exit(1);
4407 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4408 exit(1);
4409 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4410 exit(1);
4411 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4412 exit(1);
4414 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4415 exit(1);
4417 /* If no default VGA is requested, the default is "none". */
4418 if (default_vga) {
4419 if (cirrus_vga_available()) {
4420 vga_model = "cirrus";
4421 } else if (vga_available()) {
4422 vga_model = "std";
4425 if (vga_model) {
4426 select_vgahw(vga_model);
4429 if (watchdog) {
4430 i = select_watchdog(watchdog);
4431 if (i > 0)
4432 exit (i == 1 ? 1 : 0);
4435 if (machine_class->compat_props) {
4436 qdev_prop_register_global_list(machine_class->compat_props);
4438 qemu_add_globals();
4440 qdev_machine_init();
4442 current_machine->ram_size = ram_size;
4443 current_machine->maxram_size = maxram_size;
4444 current_machine->ram_slots = ram_slots;
4445 current_machine->boot_order = boot_order;
4446 current_machine->cpu_model = cpu_model;
4448 machine_class->init(current_machine);
4450 audio_init();
4452 cpu_synchronize_all_post_init();
4454 set_numa_modes();
4456 /* init USB devices */
4457 if (usb_enabled(false)) {
4458 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4459 exit(1);
4462 /* init generic devices */
4463 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4464 exit(1);
4466 net_check_clients();
4468 ds = init_displaystate();
4470 /* init local displays */
4471 switch (display_type) {
4472 case DT_NOGRAPHIC:
4473 (void)ds; /* avoid warning if no display is configured */
4474 break;
4475 #if defined(CONFIG_CURSES)
4476 case DT_CURSES:
4477 curses_display_init(ds, full_screen);
4478 break;
4479 #endif
4480 #if defined(CONFIG_SDL)
4481 case DT_SDL:
4482 sdl_display_init(ds, full_screen, no_frame);
4483 break;
4484 #elif defined(CONFIG_COCOA)
4485 case DT_SDL:
4486 cocoa_display_init(ds, full_screen);
4487 break;
4488 #endif
4489 #if defined(CONFIG_GTK)
4490 case DT_GTK:
4491 gtk_display_init(ds, full_screen, grab_on_hover);
4492 break;
4493 #endif
4494 default:
4495 break;
4498 /* must be after terminal init, SDL library changes signal handlers */
4499 os_setup_signal_handling();
4501 #ifdef CONFIG_VNC
4502 /* init remote displays */
4503 if (vnc_display) {
4504 Error *local_err = NULL;
4505 vnc_display_init(ds);
4506 vnc_display_open(ds, vnc_display, &local_err);
4507 if (local_err != NULL) {
4508 error_report("Failed to start VNC server on `%s': %s",
4509 vnc_display, error_get_pretty(local_err));
4510 error_free(local_err);
4511 exit(1);
4514 if (show_vnc_port) {
4515 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4518 #endif
4519 #ifdef CONFIG_SPICE
4520 if (using_spice) {
4521 qemu_spice_display_init();
4523 #endif
4525 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4526 exit(1);
4529 qdev_machine_creation_done();
4531 if (rom_load_all() != 0) {
4532 fprintf(stderr, "rom loading failed\n");
4533 exit(1);
4536 /* TODO: once all bus devices are qdevified, this should be done
4537 * when bus is created by qdev.c */
4538 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4539 qemu_run_machine_init_done_notifiers();
4541 /* Done notifiers can load ROMs */
4542 rom_load_done();
4544 qemu_system_reset(VMRESET_SILENT);
4545 if (loadvm) {
4546 if (load_vmstate(loadvm) < 0) {
4547 autostart = 0;
4551 qdev_prop_check_global();
4552 if (vmstate_dump_file) {
4553 /* dump and exit */
4554 dump_vmstate_json_to_file(vmstate_dump_file);
4555 return 0;
4558 if (incoming) {
4559 Error *local_err = NULL;
4560 qemu_start_incoming_migration(incoming, &local_err);
4561 if (local_err) {
4562 error_report("-incoming %s: %s", incoming,
4563 error_get_pretty(local_err));
4564 error_free(local_err);
4565 exit(1);
4567 } else if (autostart) {
4568 vm_start();
4571 os_setup_post();
4573 if (is_daemonized()) {
4574 if (!trace_init_backends(trace_events, trace_file)) {
4575 exit(1);
4579 main_loop();
4580 bdrv_close_all();
4581 pause_all_vcpus();
4582 res_free();
4583 #ifdef CONFIG_TPM
4584 tpm_cleanup();
4585 #endif
4587 return 0;