Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blob168e5ae0754009ea082bbdfd3b19e6b5b421acc5
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>
30 #include <zlib.h>
31 #include "qemu/bitmap.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "config-host.h"
36 #ifndef _WIN32
37 #include <libgen.h>
38 #include <sys/times.h>
39 #include <sys/wait.h>
40 #include <termios.h>
41 #include <sys/mman.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <net/if.h>
47 #include <arpa/inet.h>
48 #include <dirent.h>
49 #include <netdb.h>
50 #include <sys/select.h>
52 #ifdef CONFIG_BSD
53 #include <sys/stat.h>
54 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
55 #include <sys/sysctl.h>
56 #else
57 #include <util.h>
58 #endif
59 #else
60 #ifdef __linux__
61 #include <malloc.h>
63 #include <linux/ppdev.h>
64 #include <linux/parport.h>
65 #endif
67 #ifdef CONFIG_SECCOMP
68 #include "sysemu/seccomp.h"
69 #endif
71 #ifdef __sun__
72 #include <sys/stat.h>
73 #include <sys/ethernet.h>
74 #include <sys/sockio.h>
75 #include <netinet/arp.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip_icmp.h> // must come after ip.h
79 #include <netinet/udp.h>
80 #include <netinet/tcp.h>
81 #include <net/if.h>
82 #include <syslog.h>
83 #include <stropts.h>
84 #endif
85 #endif
86 #endif
88 #if defined(CONFIG_VDE)
89 #include <libvdeplug.h>
90 #endif
92 #ifndef _WIN32
93 # define CONFIG_CHROOT
94 #endif
96 #ifdef _WIN32
97 #include <windows.h>
98 #endif
100 #ifdef CONFIG_SDL
101 #if defined(__APPLE__) || defined(main)
102 #include <SDL.h>
103 int qemu_main(int argc, char **argv);
104 int main(int argc, char **argv)
106 return qemu_main(argc, argv);
108 #undef main
109 #define main qemu_main
110 #endif
111 #endif /* CONFIG_SDL */
113 #ifdef CONFIG_COCOA
114 #undef main
115 #define main qemu_main
116 #endif /* CONFIG_COCOA */
118 #include <glib.h>
120 #include "hw/hw.h"
121 #include "hw/boards.h"
122 #include "hw/usb.h"
123 #include "hw/pcmcia.h"
124 #include "hw/i386/pc.h"
125 #include "hw/isa/isa.h"
126 #include "hw/bt.h"
127 #include "sysemu/watchdog.h"
128 #include "hw/i386/smbios.h"
129 #include "hw/xen/xen.h"
130 #include "hw/qdev.h"
131 #include "hw/loader.h"
132 #include "monitor/qdev.h"
133 #include "sysemu/bt.h"
134 #include "net/net.h"
135 #include "net/slirp.h"
136 #include "monitor/monitor.h"
137 #include "ui/console.h"
138 #include "sysemu/sysemu.h"
139 #include "exec/gdbstub.h"
140 #include "qemu/timer.h"
141 #include "sysemu/char.h"
142 #include "qemu/cache-utils.h"
143 #include "sysemu/blockdev.h"
144 #include "hw/block/block.h"
145 #include "migration/block.h"
146 #include "sysemu/tpm.h"
147 #include "sysemu/dma.h"
148 #include "audio/audio.h"
149 #include "migration/migration.h"
150 #include "sysemu/kvm.h"
151 #include "qapi/qmp/qjson.h"
152 #include "qemu/option.h"
153 #include "qemu/config-file.h"
154 #include "qemu-options.h"
155 #include "qmp-commands.h"
156 #include "qemu/main-loop.h"
157 #ifdef CONFIG_VIRTFS
158 #include "fsdev/qemu-fsdev.h"
159 #endif
160 #include "sysemu/qtest.h"
162 #include "disas/disas.h"
164 #include "qemu/sockets.h"
166 #include "slirp/libslirp.h"
168 #include "trace.h"
169 #include "trace/control.h"
170 #include "qemu/queue.h"
171 #include "sysemu/cpus.h"
172 #include "sysemu/arch_init.h"
173 #include "qemu/osdep.h"
175 #include "ui/qemu-spice.h"
176 #include "qapi/string-input-visitor.h"
178 //#define DEBUG_NET
179 //#define DEBUG_SLIRP
181 #define DEFAULT_RAM_SIZE 128
183 #define MAX_VIRTIO_CONSOLES 1
184 #define MAX_SCLP_CONSOLES 1
186 static const char *data_dir[16];
187 static int data_dir_idx;
188 const char *bios_name = NULL;
189 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
190 DisplayType display_type = DT_DEFAULT;
191 static int display_remote;
192 const char* keyboard_layout = NULL;
193 ram_addr_t ram_size;
194 const char *mem_path = NULL;
195 int mem_prealloc = 0; /* force preallocation of physical target memory */
196 int nb_nics;
197 NICInfo nd_table[MAX_NICS];
198 int autostart;
199 static int rtc_utc = 1;
200 static int rtc_date_offset = -1; /* -1 means no change */
201 QEMUClockType rtc_clock;
202 int vga_interface_type = VGA_NONE;
203 static int full_screen = 0;
204 static int no_frame = 0;
205 int no_quit = 0;
206 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
207 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
208 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
209 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
210 int win2k_install_hack = 0;
211 int singlestep = 0;
212 int smp_cpus = 1;
213 int max_cpus = 0;
214 int smp_cores = 1;
215 int smp_threads = 1;
216 #ifdef CONFIG_VNC
217 const char *vnc_display;
218 #endif
219 int acpi_enabled = 1;
220 int no_hpet = 0;
221 int fd_bootchk = 1;
222 static int no_reboot;
223 int no_shutdown = 0;
224 int cursor_hide = 1;
225 int graphic_rotate = 0;
226 const char *watchdog;
227 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
228 int nb_option_roms;
229 int semihosting_enabled = 0;
230 int old_param = 0;
231 const char *qemu_name;
232 int alt_grab = 0;
233 int ctrl_grab = 0;
234 unsigned int nb_prom_envs = 0;
235 const char *prom_envs[MAX_PROM_ENVS];
236 int boot_menu;
237 static bool boot_strict;
238 uint8_t *boot_splash_filedata;
239 size_t boot_splash_filedata_size;
240 uint8_t qemu_extra_params_fw[2];
242 typedef struct FWBootEntry FWBootEntry;
244 struct FWBootEntry {
245 QTAILQ_ENTRY(FWBootEntry) link;
246 int32_t bootindex;
247 DeviceState *dev;
248 char *suffix;
251 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
252 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
254 int nb_numa_nodes;
255 uint64_t node_mem[MAX_NODES];
256 unsigned long *node_cpumask[MAX_NODES];
258 uint8_t qemu_uuid[16];
259 bool qemu_uuid_set;
261 /* Trace unassigned memory or i/o accesses. */
262 bool trace_unassigned;
264 static QEMUBootSetHandler *boot_set_handler;
265 static void *boot_set_opaque;
267 static NotifierList exit_notifiers =
268 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
270 static NotifierList machine_init_done_notifiers =
271 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
273 static bool tcg_allowed = true;
274 bool xen_allowed;
275 uint32_t xen_domid;
276 enum xen_mode xen_mode = XEN_EMULATE;
277 static int tcg_tb_size;
279 static int default_serial = 1;
280 static int default_parallel = 1;
281 static int default_virtcon = 1;
282 static int default_sclp = 1;
283 static int default_monitor = 1;
284 static int default_floppy = 1;
285 static int default_cdrom = 1;
286 static int default_sdcard = 1;
287 static int default_vga = 1;
289 static struct {
290 const char *driver;
291 int *flag;
292 } default_list[] = {
293 { .driver = "isa-serial", .flag = &default_serial },
294 { .driver = "isa-parallel", .flag = &default_parallel },
295 { .driver = "isa-fdc", .flag = &default_floppy },
296 { .driver = "ide-cd", .flag = &default_cdrom },
297 { .driver = "ide-hd", .flag = &default_cdrom },
298 { .driver = "ide-drive", .flag = &default_cdrom },
299 { .driver = "scsi-cd", .flag = &default_cdrom },
300 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
301 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
302 { .driver = "virtio-serial", .flag = &default_virtcon },
303 { .driver = "VGA", .flag = &default_vga },
304 { .driver = "isa-vga", .flag = &default_vga },
305 { .driver = "cirrus-vga", .flag = &default_vga },
306 { .driver = "isa-cirrus-vga", .flag = &default_vga },
307 { .driver = "vmware-svga", .flag = &default_vga },
308 { .driver = "qxl-vga", .flag = &default_vga },
311 static QemuOptsList qemu_rtc_opts = {
312 .name = "rtc",
313 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
314 .desc = {
316 .name = "base",
317 .type = QEMU_OPT_STRING,
319 .name = "clock",
320 .type = QEMU_OPT_STRING,
322 .name = "driftfix",
323 .type = QEMU_OPT_STRING,
325 { /* end of list */ }
329 static QemuOptsList qemu_sandbox_opts = {
330 .name = "sandbox",
331 .implied_opt_name = "enable",
332 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
333 .desc = {
335 .name = "enable",
336 .type = QEMU_OPT_BOOL,
338 { /* end of list */ }
342 static QemuOptsList qemu_trace_opts = {
343 .name = "trace",
344 .implied_opt_name = "trace",
345 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
346 .desc = {
348 .name = "events",
349 .type = QEMU_OPT_STRING,
351 .name = "file",
352 .type = QEMU_OPT_STRING,
354 { /* end of list */ }
358 static QemuOptsList qemu_option_rom_opts = {
359 .name = "option-rom",
360 .implied_opt_name = "romfile",
361 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
362 .desc = {
364 .name = "bootindex",
365 .type = QEMU_OPT_NUMBER,
366 }, {
367 .name = "romfile",
368 .type = QEMU_OPT_STRING,
370 { /* end of list */ }
374 static QemuOptsList qemu_machine_opts = {
375 .name = "machine",
376 .implied_opt_name = "type",
377 .merge_lists = true,
378 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
379 .desc = {
381 .name = "type",
382 .type = QEMU_OPT_STRING,
383 .help = "emulated machine"
384 }, {
385 .name = "accel",
386 .type = QEMU_OPT_STRING,
387 .help = "accelerator list",
388 }, {
389 .name = "kernel_irqchip",
390 .type = QEMU_OPT_BOOL,
391 .help = "use KVM in-kernel irqchip",
392 }, {
393 .name = "kvm_shadow_mem",
394 .type = QEMU_OPT_SIZE,
395 .help = "KVM shadow MMU size",
396 }, {
397 .name = "kernel",
398 .type = QEMU_OPT_STRING,
399 .help = "Linux kernel image file",
400 }, {
401 .name = "initrd",
402 .type = QEMU_OPT_STRING,
403 .help = "Linux initial ramdisk file",
404 }, {
405 .name = "append",
406 .type = QEMU_OPT_STRING,
407 .help = "Linux kernel command line",
408 }, {
409 .name = "dtb",
410 .type = QEMU_OPT_STRING,
411 .help = "Linux kernel device tree file",
412 }, {
413 .name = "dumpdtb",
414 .type = QEMU_OPT_STRING,
415 .help = "Dump current dtb to a file and quit",
416 }, {
417 .name = "phandle_start",
418 .type = QEMU_OPT_NUMBER,
419 .help = "The first phandle ID we may generate dynamically",
420 }, {
421 .name = "dt_compatible",
422 .type = QEMU_OPT_STRING,
423 .help = "Overrides the \"compatible\" property of the dt root node",
424 }, {
425 .name = "dump-guest-core",
426 .type = QEMU_OPT_BOOL,
427 .help = "Include guest memory in a core dump",
428 }, {
429 .name = "mem-merge",
430 .type = QEMU_OPT_BOOL,
431 .help = "enable/disable memory merge support",
433 .name = "usb",
434 .type = QEMU_OPT_BOOL,
435 .help = "Set on/off to enable/disable usb",
437 .name = "firmware",
438 .type = QEMU_OPT_STRING,
439 .help = "firmware image",
441 { /* End of list */ }
445 static QemuOptsList qemu_boot_opts = {
446 .name = "boot-opts",
447 .implied_opt_name = "order",
448 .merge_lists = true,
449 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
450 .desc = {
452 .name = "order",
453 .type = QEMU_OPT_STRING,
454 }, {
455 .name = "once",
456 .type = QEMU_OPT_STRING,
457 }, {
458 .name = "menu",
459 .type = QEMU_OPT_BOOL,
460 }, {
461 .name = "splash",
462 .type = QEMU_OPT_STRING,
463 }, {
464 .name = "splash-time",
465 .type = QEMU_OPT_STRING,
466 }, {
467 .name = "reboot-timeout",
468 .type = QEMU_OPT_STRING,
469 }, {
470 .name = "strict",
471 .type = QEMU_OPT_BOOL,
473 { /*End of list */ }
477 static QemuOptsList qemu_add_fd_opts = {
478 .name = "add-fd",
479 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
480 .desc = {
482 .name = "fd",
483 .type = QEMU_OPT_NUMBER,
484 .help = "file descriptor of which a duplicate is added to fd set",
486 .name = "set",
487 .type = QEMU_OPT_NUMBER,
488 .help = "ID of the fd set to add fd to",
490 .name = "opaque",
491 .type = QEMU_OPT_STRING,
492 .help = "free-form string used to describe fd",
494 { /* end of list */ }
498 static QemuOptsList qemu_object_opts = {
499 .name = "object",
500 .implied_opt_name = "qom-type",
501 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
502 .desc = {
507 static QemuOptsList qemu_tpmdev_opts = {
508 .name = "tpmdev",
509 .implied_opt_name = "type",
510 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
511 .desc = {
512 /* options are defined in the TPM backends */
513 { /* end of list */ }
517 static QemuOptsList qemu_realtime_opts = {
518 .name = "realtime",
519 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
520 .desc = {
522 .name = "mlock",
523 .type = QEMU_OPT_BOOL,
525 { /* end of list */ }
529 static QemuOptsList qemu_msg_opts = {
530 .name = "msg",
531 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
532 .desc = {
534 .name = "timestamp",
535 .type = QEMU_OPT_BOOL,
537 { /* end of list */ }
542 * Get machine options
544 * Returns: machine options (never null).
546 QemuOpts *qemu_get_machine_opts(void)
548 QemuOptsList *list;
549 QemuOpts *opts;
551 list = qemu_find_opts("machine");
552 assert(list);
553 opts = qemu_opts_find(list, NULL);
554 if (!opts) {
555 opts = qemu_opts_create(list, NULL, 0, &error_abort);
557 return opts;
560 const char *qemu_get_vm_name(void)
562 return qemu_name;
565 static void res_free(void)
567 if (boot_splash_filedata != NULL) {
568 g_free(boot_splash_filedata);
569 boot_splash_filedata = NULL;
573 static int default_driver_check(QemuOpts *opts, void *opaque)
575 const char *driver = qemu_opt_get(opts, "driver");
576 int i;
578 if (!driver)
579 return 0;
580 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
581 if (strcmp(default_list[i].driver, driver) != 0)
582 continue;
583 *(default_list[i].flag) = 0;
585 return 0;
588 /***********************************************************/
589 /* QEMU state */
591 static RunState current_run_state = RUN_STATE_PRELAUNCH;
593 typedef struct {
594 RunState from;
595 RunState to;
596 } RunStateTransition;
598 static const RunStateTransition runstate_transitions_def[] = {
599 /* from -> to */
600 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
601 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
603 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
604 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
606 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
607 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
609 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
610 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
613 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
615 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
616 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
618 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
619 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
620 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
622 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
623 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
625 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
627 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
628 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
629 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
630 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
631 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
633 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
634 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
635 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
636 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
638 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
640 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
641 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
643 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
644 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
645 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
646 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
648 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
649 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
651 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
652 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
654 { RUN_STATE_MAX, RUN_STATE_MAX },
657 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
659 bool runstate_check(RunState state)
661 return current_run_state == state;
664 static void runstate_init(void)
666 const RunStateTransition *p;
668 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
670 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
671 runstate_valid_transitions[p->from][p->to] = true;
675 /* This function will abort() on invalid state transitions */
676 void runstate_set(RunState new_state)
678 assert(new_state < RUN_STATE_MAX);
680 if (!runstate_valid_transitions[current_run_state][new_state]) {
681 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
682 RunState_lookup[current_run_state],
683 RunState_lookup[new_state]);
684 abort();
686 trace_runstate_set(new_state);
687 current_run_state = new_state;
690 int runstate_is_running(void)
692 return runstate_check(RUN_STATE_RUNNING);
695 bool runstate_needs_reset(void)
697 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
698 runstate_check(RUN_STATE_SHUTDOWN);
701 StatusInfo *qmp_query_status(Error **errp)
703 StatusInfo *info = g_malloc0(sizeof(*info));
705 info->running = runstate_is_running();
706 info->singlestep = singlestep;
707 info->status = current_run_state;
709 return info;
712 /***********************************************************/
713 /* real time host monotonic timer */
715 /***********************************************************/
716 /* host time/date access */
717 void qemu_get_timedate(struct tm *tm, int offset)
719 time_t ti;
721 time(&ti);
722 ti += offset;
723 if (rtc_date_offset == -1) {
724 if (rtc_utc)
725 gmtime_r(&ti, tm);
726 else
727 localtime_r(&ti, tm);
728 } else {
729 ti -= rtc_date_offset;
730 gmtime_r(&ti, tm);
734 int qemu_timedate_diff(struct tm *tm)
736 time_t seconds;
738 if (rtc_date_offset == -1)
739 if (rtc_utc)
740 seconds = mktimegm(tm);
741 else {
742 struct tm tmp = *tm;
743 tmp.tm_isdst = -1; /* use timezone to figure it out */
744 seconds = mktime(&tmp);
746 else
747 seconds = mktimegm(tm) + rtc_date_offset;
749 return seconds - time(NULL);
752 void rtc_change_mon_event(struct tm *tm)
754 QObject *data;
756 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
757 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
758 qobject_decref(data);
761 static void configure_rtc_date_offset(const char *startdate, int legacy)
763 time_t rtc_start_date;
764 struct tm tm;
766 if (!strcmp(startdate, "now") && legacy) {
767 rtc_date_offset = -1;
768 } else {
769 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
770 &tm.tm_year,
771 &tm.tm_mon,
772 &tm.tm_mday,
773 &tm.tm_hour,
774 &tm.tm_min,
775 &tm.tm_sec) == 6) {
776 /* OK */
777 } else if (sscanf(startdate, "%d-%d-%d",
778 &tm.tm_year,
779 &tm.tm_mon,
780 &tm.tm_mday) == 3) {
781 tm.tm_hour = 0;
782 tm.tm_min = 0;
783 tm.tm_sec = 0;
784 } else {
785 goto date_fail;
787 tm.tm_year -= 1900;
788 tm.tm_mon--;
789 rtc_start_date = mktimegm(&tm);
790 if (rtc_start_date == -1) {
791 date_fail:
792 fprintf(stderr, "Invalid date format. Valid formats are:\n"
793 "'2006-06-17T16:01:21' or '2006-06-17'\n");
794 exit(1);
796 rtc_date_offset = time(NULL) - rtc_start_date;
800 static void configure_rtc(QemuOpts *opts)
802 const char *value;
804 value = qemu_opt_get(opts, "base");
805 if (value) {
806 if (!strcmp(value, "utc")) {
807 rtc_utc = 1;
808 } else if (!strcmp(value, "localtime")) {
809 rtc_utc = 0;
810 } else {
811 configure_rtc_date_offset(value, 0);
814 value = qemu_opt_get(opts, "clock");
815 if (value) {
816 if (!strcmp(value, "host")) {
817 rtc_clock = QEMU_CLOCK_HOST;
818 } else if (!strcmp(value, "rt")) {
819 rtc_clock = QEMU_CLOCK_REALTIME;
820 } else if (!strcmp(value, "vm")) {
821 rtc_clock = QEMU_CLOCK_VIRTUAL;
822 } else {
823 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
824 exit(1);
827 value = qemu_opt_get(opts, "driftfix");
828 if (value) {
829 if (!strcmp(value, "slew")) {
830 static GlobalProperty slew_lost_ticks[] = {
832 .driver = "mc146818rtc",
833 .property = "lost_tick_policy",
834 .value = "slew",
836 { /* end of list */ }
839 qdev_prop_register_global_list(slew_lost_ticks);
840 } else if (!strcmp(value, "none")) {
841 /* discard is default */
842 } else {
843 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
844 exit(1);
849 /***********************************************************/
850 /* Bluetooth support */
851 static int nb_hcis;
852 static int cur_hci;
853 static struct HCIInfo *hci_table[MAX_NICS];
855 struct HCIInfo *qemu_next_hci(void)
857 if (cur_hci == nb_hcis)
858 return &null_hci;
860 return hci_table[cur_hci++];
863 static int bt_hci_parse(const char *str)
865 struct HCIInfo *hci;
866 bdaddr_t bdaddr;
868 if (nb_hcis >= MAX_NICS) {
869 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
870 return -1;
873 hci = hci_init(str);
874 if (!hci)
875 return -1;
877 bdaddr.b[0] = 0x52;
878 bdaddr.b[1] = 0x54;
879 bdaddr.b[2] = 0x00;
880 bdaddr.b[3] = 0x12;
881 bdaddr.b[4] = 0x34;
882 bdaddr.b[5] = 0x56 + nb_hcis;
883 hci->bdaddr_set(hci, bdaddr.b);
885 hci_table[nb_hcis++] = hci;
887 return 0;
890 static void bt_vhci_add(int vlan_id)
892 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
894 if (!vlan->slave)
895 fprintf(stderr, "qemu: warning: adding a VHCI to "
896 "an empty scatternet %i\n", vlan_id);
898 bt_vhci_init(bt_new_hci(vlan));
901 static struct bt_device_s *bt_device_add(const char *opt)
903 struct bt_scatternet_s *vlan;
904 int vlan_id = 0;
905 char *endp = strstr(opt, ",vlan=");
906 int len = (endp ? endp - opt : strlen(opt)) + 1;
907 char devname[10];
909 pstrcpy(devname, MIN(sizeof(devname), len), opt);
911 if (endp) {
912 vlan_id = strtol(endp + 6, &endp, 0);
913 if (*endp) {
914 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
915 return 0;
919 vlan = qemu_find_bt_vlan(vlan_id);
921 if (!vlan->slave)
922 fprintf(stderr, "qemu: warning: adding a slave device to "
923 "an empty scatternet %i\n", vlan_id);
925 if (!strcmp(devname, "keyboard"))
926 return bt_keyboard_init(vlan);
928 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
929 return 0;
932 static int bt_parse(const char *opt)
934 const char *endp, *p;
935 int vlan;
937 if (strstart(opt, "hci", &endp)) {
938 if (!*endp || *endp == ',') {
939 if (*endp)
940 if (!strstart(endp, ",vlan=", 0))
941 opt = endp + 1;
943 return bt_hci_parse(opt);
945 } else if (strstart(opt, "vhci", &endp)) {
946 if (!*endp || *endp == ',') {
947 if (*endp) {
948 if (strstart(endp, ",vlan=", &p)) {
949 vlan = strtol(p, (char **) &endp, 0);
950 if (*endp) {
951 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
952 return 1;
954 } else {
955 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
956 return 1;
958 } else
959 vlan = 0;
961 bt_vhci_add(vlan);
962 return 0;
964 } else if (strstart(opt, "device:", &endp))
965 return !bt_device_add(endp);
967 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
968 return 1;
971 static int parse_sandbox(QemuOpts *opts, void *opaque)
973 /* FIXME: change this to true for 1.3 */
974 if (qemu_opt_get_bool(opts, "enable", false)) {
975 #ifdef CONFIG_SECCOMP
976 if (seccomp_start() < 0) {
977 qerror_report(ERROR_CLASS_GENERIC_ERROR,
978 "failed to install seccomp syscall filter in the kernel");
979 return -1;
981 #else
982 qerror_report(ERROR_CLASS_GENERIC_ERROR,
983 "sandboxing request but seccomp is not compiled into this build");
984 return -1;
985 #endif
988 return 0;
991 bool usb_enabled(bool default_usb)
993 return qemu_opt_get_bool(qemu_get_machine_opts(), "usb", default_usb);
996 #ifndef _WIN32
997 static int parse_add_fd(QemuOpts *opts, void *opaque)
999 int fd, dupfd, flags;
1000 int64_t fdset_id;
1001 const char *fd_opaque = NULL;
1003 fd = qemu_opt_get_number(opts, "fd", -1);
1004 fdset_id = qemu_opt_get_number(opts, "set", -1);
1005 fd_opaque = qemu_opt_get(opts, "opaque");
1007 if (fd < 0) {
1008 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1009 "fd option is required and must be non-negative");
1010 return -1;
1013 if (fd <= STDERR_FILENO) {
1014 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1015 "fd cannot be a standard I/O stream");
1016 return -1;
1020 * All fds inherited across exec() necessarily have FD_CLOEXEC
1021 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1023 flags = fcntl(fd, F_GETFD);
1024 if (flags == -1 || (flags & FD_CLOEXEC)) {
1025 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1026 "fd is not valid or already in use");
1027 return -1;
1030 if (fdset_id < 0) {
1031 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1032 "set option is required and must be non-negative");
1033 return -1;
1036 #ifdef F_DUPFD_CLOEXEC
1037 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1038 #else
1039 dupfd = dup(fd);
1040 if (dupfd != -1) {
1041 qemu_set_cloexec(dupfd);
1043 #endif
1044 if (dupfd == -1) {
1045 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1046 "Error duplicating fd: %s", strerror(errno));
1047 return -1;
1050 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1051 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1052 fd_opaque, NULL);
1054 return 0;
1057 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1059 int fd;
1061 fd = qemu_opt_get_number(opts, "fd", -1);
1062 close(fd);
1064 return 0;
1066 #endif
1068 /***********************************************************/
1069 /* QEMU Block devices */
1071 #define HD_OPTS "media=disk"
1072 #define CDROM_OPTS "media=cdrom"
1073 #define FD_OPTS ""
1074 #define PFLASH_OPTS ""
1075 #define MTD_OPTS ""
1076 #define SD_OPTS ""
1078 static int drive_init_func(QemuOpts *opts, void *opaque)
1080 BlockInterfaceType *block_default_type = opaque;
1082 return drive_init(opts, *block_default_type) == NULL;
1085 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1087 if (NULL == qemu_opt_get(opts, "snapshot")) {
1088 qemu_opt_set(opts, "snapshot", "on");
1090 return 0;
1093 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1094 int index, const char *optstr)
1096 QemuOpts *opts;
1098 if (!enable || drive_get_by_index(type, index)) {
1099 return;
1102 opts = drive_add(type, index, NULL, optstr);
1103 if (snapshot) {
1104 drive_enable_snapshot(opts, NULL);
1106 if (!drive_init(opts, type)) {
1107 exit(1);
1111 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1113 boot_set_handler = func;
1114 boot_set_opaque = opaque;
1117 int qemu_boot_set(const char *boot_order)
1119 if (!boot_set_handler) {
1120 return -EINVAL;
1122 return boot_set_handler(boot_set_opaque, boot_order);
1125 static void validate_bootdevices(const char *devices)
1127 /* We just do some generic consistency checks */
1128 const char *p;
1129 int bitmap = 0;
1131 for (p = devices; *p != '\0'; p++) {
1132 /* Allowed boot devices are:
1133 * a-b: floppy disk drives
1134 * c-f: IDE disk drives
1135 * g-m: machine implementation dependent drives
1136 * n-p: network devices
1137 * It's up to each machine implementation to check if the given boot
1138 * devices match the actual hardware implementation and firmware
1139 * features.
1141 if (*p < 'a' || *p > 'p') {
1142 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1143 exit(1);
1145 if (bitmap & (1 << (*p - 'a'))) {
1146 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1147 exit(1);
1149 bitmap |= 1 << (*p - 'a');
1153 static void restore_boot_order(void *opaque)
1155 char *normal_boot_order = opaque;
1156 static int first = 1;
1158 /* Restore boot order and remove ourselves after the first boot */
1159 if (first) {
1160 first = 0;
1161 return;
1164 qemu_boot_set(normal_boot_order);
1166 qemu_unregister_reset(restore_boot_order, normal_boot_order);
1167 g_free(normal_boot_order);
1170 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1171 const char *suffix)
1173 FWBootEntry *node, *i;
1175 if (bootindex < 0) {
1176 return;
1179 assert(dev != NULL || suffix != NULL);
1181 node = g_malloc0(sizeof(FWBootEntry));
1182 node->bootindex = bootindex;
1183 node->suffix = g_strdup(suffix);
1184 node->dev = dev;
1186 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1187 if (i->bootindex == bootindex) {
1188 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1189 exit(1);
1190 } else if (i->bootindex < bootindex) {
1191 continue;
1193 QTAILQ_INSERT_BEFORE(i, node, link);
1194 return;
1196 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1199 DeviceState *get_boot_device(uint32_t position)
1201 uint32_t counter = 0;
1202 FWBootEntry *i = NULL;
1203 DeviceState *res = NULL;
1205 if (!QTAILQ_EMPTY(&fw_boot_order)) {
1206 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1207 if (counter == position) {
1208 res = i->dev;
1209 break;
1211 counter++;
1214 return res;
1218 * This function returns null terminated string that consist of new line
1219 * separated device paths.
1221 * memory pointed by "size" is assigned total length of the array in bytes
1224 char *get_boot_devices_list(size_t *size)
1226 FWBootEntry *i;
1227 size_t total = 0;
1228 char *list = NULL;
1230 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1231 char *devpath = NULL, *bootpath;
1232 size_t len;
1234 if (i->dev) {
1235 devpath = qdev_get_fw_dev_path(i->dev);
1236 assert(devpath);
1239 if (i->suffix && devpath) {
1240 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1242 bootpath = g_malloc(bootpathlen);
1243 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1244 g_free(devpath);
1245 } else if (devpath) {
1246 bootpath = devpath;
1247 } else {
1248 assert(i->suffix);
1249 bootpath = g_strdup(i->suffix);
1252 if (total) {
1253 list[total-1] = '\n';
1255 len = strlen(bootpath) + 1;
1256 list = g_realloc(list, total + len);
1257 memcpy(&list[total], bootpath, len);
1258 total += len;
1259 g_free(bootpath);
1262 *size = total;
1264 if (boot_strict && *size > 0) {
1265 list[total-1] = '\n';
1266 list = g_realloc(list, total + 5);
1267 memcpy(&list[total], "HALT", 5);
1268 *size = total + 5;
1270 return list;
1273 static void numa_node_parse_cpus(int nodenr, const char *cpus)
1275 char *endptr;
1276 unsigned long long value, endvalue;
1278 /* Empty CPU range strings will be considered valid, they will simply
1279 * not set any bit in the CPU bitmap.
1281 if (!*cpus) {
1282 return;
1285 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1286 goto error;
1288 if (*endptr == '-') {
1289 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1290 goto error;
1292 } else if (*endptr == '\0') {
1293 endvalue = value;
1294 } else {
1295 goto error;
1298 if (endvalue >= MAX_CPUMASK_BITS) {
1299 endvalue = MAX_CPUMASK_BITS - 1;
1300 fprintf(stderr,
1301 "qemu: NUMA: A max of %d VCPUs are supported\n",
1302 MAX_CPUMASK_BITS);
1305 if (endvalue < value) {
1306 goto error;
1309 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1310 return;
1312 error:
1313 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1314 exit(1);
1317 static void numa_add(const char *optarg)
1319 char option[128];
1320 char *endptr;
1321 unsigned long long nodenr;
1323 optarg = get_opt_name(option, 128, optarg, ',');
1324 if (*optarg == ',') {
1325 optarg++;
1327 if (!strcmp(option, "node")) {
1329 if (nb_numa_nodes >= MAX_NODES) {
1330 fprintf(stderr, "qemu: too many NUMA nodes\n");
1331 exit(1);
1334 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1335 nodenr = nb_numa_nodes;
1336 } else {
1337 if (parse_uint_full(option, &nodenr, 10) < 0) {
1338 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1339 exit(1);
1343 if (nodenr >= MAX_NODES) {
1344 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1345 exit(1);
1348 if (get_param_value(option, 128, "mem", optarg) == 0) {
1349 node_mem[nodenr] = 0;
1350 } else {
1351 int64_t sval;
1352 sval = strtosz(option, &endptr);
1353 if (sval < 0 || *endptr) {
1354 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1355 exit(1);
1357 node_mem[nodenr] = sval;
1359 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1360 numa_node_parse_cpus(nodenr, option);
1362 nb_numa_nodes++;
1363 } else {
1364 fprintf(stderr, "Invalid -numa option: %s\n", option);
1365 exit(1);
1369 static QemuOptsList qemu_smp_opts = {
1370 .name = "smp-opts",
1371 .implied_opt_name = "cpus",
1372 .merge_lists = true,
1373 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1374 .desc = {
1376 .name = "cpus",
1377 .type = QEMU_OPT_NUMBER,
1378 }, {
1379 .name = "sockets",
1380 .type = QEMU_OPT_NUMBER,
1381 }, {
1382 .name = "cores",
1383 .type = QEMU_OPT_NUMBER,
1384 }, {
1385 .name = "threads",
1386 .type = QEMU_OPT_NUMBER,
1387 }, {
1388 .name = "maxcpus",
1389 .type = QEMU_OPT_NUMBER,
1391 { /*End of list */ }
1395 static void smp_parse(QemuOpts *opts)
1397 if (opts) {
1399 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1400 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1401 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1402 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1404 /* compute missing values, prefer sockets over cores over threads */
1405 if (cpus == 0 || sockets == 0) {
1406 sockets = sockets > 0 ? sockets : 1;
1407 cores = cores > 0 ? cores : 1;
1408 threads = threads > 0 ? threads : 1;
1409 if (cpus == 0) {
1410 cpus = cores * threads * sockets;
1412 } else {
1413 if (cores == 0) {
1414 threads = threads > 0 ? threads : 1;
1415 cores = cpus / (sockets * threads);
1416 } else {
1417 threads = cpus / (cores * sockets);
1421 max_cpus = qemu_opt_get_number(opts, "maxcpus", 0);
1423 smp_cpus = cpus;
1424 smp_cores = cores > 0 ? cores : 1;
1425 smp_threads = threads > 0 ? threads : 1;
1429 if (max_cpus == 0) {
1430 max_cpus = smp_cpus;
1433 if (max_cpus > 255) {
1434 fprintf(stderr, "Unsupported number of maxcpus\n");
1435 exit(1);
1437 if (max_cpus < smp_cpus) {
1438 fprintf(stderr, "maxcpus must be equal to or greater than smp\n");
1439 exit(1);
1444 static void configure_realtime(QemuOpts *opts)
1446 bool enable_mlock;
1448 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
1450 if (enable_mlock) {
1451 if (os_mlock() < 0) {
1452 fprintf(stderr, "qemu: locking memory failed\n");
1453 exit(1);
1459 static void configure_msg(QemuOpts *opts)
1461 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1464 /***********************************************************/
1465 /* USB devices */
1467 static int usb_device_add(const char *devname)
1469 USBDevice *dev = NULL;
1470 #ifndef CONFIG_LINUX
1471 const char *p;
1472 #endif
1474 if (!usb_enabled(false)) {
1475 return -1;
1478 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1479 dev = usbdevice_create(devname);
1480 if (dev)
1481 goto done;
1483 /* the other ones */
1484 #ifndef CONFIG_LINUX
1485 /* only the linux version is qdev-ified, usb-bsd still needs this */
1486 if (strstart(devname, "host:", &p)) {
1487 dev = usb_host_device_open(usb_bus_find(-1), p);
1489 #endif
1490 if (!dev)
1491 return -1;
1493 done:
1494 return 0;
1497 static int usb_device_del(const char *devname)
1499 int bus_num, addr;
1500 const char *p;
1502 if (strstart(devname, "host:", &p)) {
1503 return -1;
1506 if (!usb_enabled(false)) {
1507 return -1;
1510 p = strchr(devname, '.');
1511 if (!p)
1512 return -1;
1513 bus_num = strtoul(devname, NULL, 0);
1514 addr = strtoul(p + 1, NULL, 0);
1516 return usb_device_delete_addr(bus_num, addr);
1519 static int usb_parse(const char *cmdline)
1521 int r;
1522 r = usb_device_add(cmdline);
1523 if (r < 0) {
1524 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1526 return r;
1529 void do_usb_add(Monitor *mon, const QDict *qdict)
1531 const char *devname = qdict_get_str(qdict, "devname");
1532 if (usb_device_add(devname) < 0) {
1533 error_report("could not add USB device '%s'", devname);
1537 void do_usb_del(Monitor *mon, const QDict *qdict)
1539 const char *devname = qdict_get_str(qdict, "devname");
1540 if (usb_device_del(devname) < 0) {
1541 error_report("could not delete USB device '%s'", devname);
1545 /***********************************************************/
1546 /* PCMCIA/Cardbus */
1548 static struct pcmcia_socket_entry_s {
1549 PCMCIASocket *socket;
1550 struct pcmcia_socket_entry_s *next;
1551 } *pcmcia_sockets = 0;
1553 void pcmcia_socket_register(PCMCIASocket *socket)
1555 struct pcmcia_socket_entry_s *entry;
1557 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1558 entry->socket = socket;
1559 entry->next = pcmcia_sockets;
1560 pcmcia_sockets = entry;
1563 void pcmcia_socket_unregister(PCMCIASocket *socket)
1565 struct pcmcia_socket_entry_s *entry, **ptr;
1567 ptr = &pcmcia_sockets;
1568 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1569 if (entry->socket == socket) {
1570 *ptr = entry->next;
1571 g_free(entry);
1575 void pcmcia_info(Monitor *mon, const QDict *qdict)
1577 struct pcmcia_socket_entry_s *iter;
1579 if (!pcmcia_sockets)
1580 monitor_printf(mon, "No PCMCIA sockets\n");
1582 for (iter = pcmcia_sockets; iter; iter = iter->next)
1583 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1584 iter->socket->attached ? iter->socket->card_string :
1585 "Empty");
1588 /***********************************************************/
1589 /* machine registration */
1591 static QEMUMachine *first_machine = NULL;
1592 QEMUMachine *current_machine = NULL;
1594 int qemu_register_machine(QEMUMachine *m)
1596 QEMUMachine **pm;
1597 pm = &first_machine;
1598 while (*pm != NULL)
1599 pm = &(*pm)->next;
1600 m->next = NULL;
1601 *pm = m;
1602 return 0;
1605 static QEMUMachine *find_machine(const char *name)
1607 QEMUMachine *m;
1609 for(m = first_machine; m != NULL; m = m->next) {
1610 if (!strcmp(m->name, name))
1611 return m;
1612 if (m->alias && !strcmp(m->alias, name))
1613 return m;
1615 return NULL;
1618 QEMUMachine *find_default_machine(void)
1620 QEMUMachine *m;
1622 for(m = first_machine; m != NULL; m = m->next) {
1623 if (m->is_default) {
1624 return m;
1627 return NULL;
1630 MachineInfoList *qmp_query_machines(Error **errp)
1632 MachineInfoList *mach_list = NULL;
1633 QEMUMachine *m;
1635 for (m = first_machine; m; m = m->next) {
1636 MachineInfoList *entry;
1637 MachineInfo *info;
1639 info = g_malloc0(sizeof(*info));
1640 if (m->is_default) {
1641 info->has_is_default = true;
1642 info->is_default = true;
1645 if (m->alias) {
1646 info->has_alias = true;
1647 info->alias = g_strdup(m->alias);
1650 info->name = g_strdup(m->name);
1651 info->cpu_max = !m->max_cpus ? 1 : m->max_cpus;
1653 entry = g_malloc0(sizeof(*entry));
1654 entry->value = info;
1655 entry->next = mach_list;
1656 mach_list = entry;
1659 return mach_list;
1662 /***********************************************************/
1663 /* main execution loop */
1665 struct vm_change_state_entry {
1666 VMChangeStateHandler *cb;
1667 void *opaque;
1668 QLIST_ENTRY (vm_change_state_entry) entries;
1671 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1673 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1674 void *opaque)
1676 VMChangeStateEntry *e;
1678 e = g_malloc0(sizeof (*e));
1680 e->cb = cb;
1681 e->opaque = opaque;
1682 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1683 return e;
1686 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1688 QLIST_REMOVE (e, entries);
1689 g_free (e);
1692 void vm_state_notify(int running, RunState state)
1694 VMChangeStateEntry *e;
1696 trace_vm_state_notify(running, state);
1698 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1699 e->cb(e->opaque, running, state);
1703 void vm_start(void)
1705 if (!runstate_is_running()) {
1706 cpu_enable_ticks();
1707 runstate_set(RUN_STATE_RUNNING);
1708 vm_state_notify(1, RUN_STATE_RUNNING);
1709 resume_all_vcpus();
1710 monitor_protocol_event(QEVENT_RESUME, NULL);
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);
1738 static RunState vmstop_requested = RUN_STATE_MAX;
1740 int qemu_shutdown_requested_get(void)
1742 return shutdown_requested;
1745 int qemu_reset_requested_get(void)
1747 return reset_requested;
1750 static int qemu_shutdown_requested(void)
1752 int r = shutdown_requested;
1753 shutdown_requested = 0;
1754 return r;
1757 static void qemu_kill_report(void)
1759 if (!qtest_enabled() && shutdown_signal != -1) {
1760 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1761 if (shutdown_pid == 0) {
1762 /* This happens for eg ^C at the terminal, so it's worth
1763 * avoiding printing an odd message in that case.
1765 fputc('\n', stderr);
1766 } else {
1767 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1769 shutdown_signal = -1;
1773 static int qemu_reset_requested(void)
1775 int r = reset_requested;
1776 reset_requested = 0;
1777 return r;
1780 static int qemu_suspend_requested(void)
1782 int r = suspend_requested;
1783 suspend_requested = 0;
1784 return r;
1787 static WakeupReason qemu_wakeup_requested(void)
1789 return wakeup_reason;
1792 static int qemu_powerdown_requested(void)
1794 int r = powerdown_requested;
1795 powerdown_requested = 0;
1796 return r;
1799 static int qemu_debug_requested(void)
1801 int r = debug_requested;
1802 debug_requested = 0;
1803 return r;
1806 /* We use RUN_STATE_MAX but any invalid value will do */
1807 static bool qemu_vmstop_requested(RunState *r)
1809 if (vmstop_requested < RUN_STATE_MAX) {
1810 *r = vmstop_requested;
1811 vmstop_requested = RUN_STATE_MAX;
1812 return true;
1815 return false;
1818 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1820 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1822 re->func = func;
1823 re->opaque = opaque;
1824 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1827 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1829 QEMUResetEntry *re;
1831 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1832 if (re->func == func && re->opaque == opaque) {
1833 QTAILQ_REMOVE(&reset_handlers, re, entry);
1834 g_free(re);
1835 return;
1840 void qemu_devices_reset(void)
1842 QEMUResetEntry *re, *nre;
1844 /* reset all devices */
1845 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1846 re->func(re->opaque);
1850 void qemu_system_reset(bool report)
1852 if (current_machine && current_machine->reset) {
1853 current_machine->reset();
1854 } else {
1855 qemu_devices_reset();
1857 if (report) {
1858 monitor_protocol_event(QEVENT_RESET, NULL);
1860 cpu_synchronize_all_post_reset();
1863 void qemu_system_reset_request(void)
1865 if (no_reboot) {
1866 shutdown_requested = 1;
1867 } else {
1868 reset_requested = 1;
1870 cpu_stop_current();
1871 qemu_notify_event();
1874 static void qemu_system_suspend(void)
1876 pause_all_vcpus();
1877 notifier_list_notify(&suspend_notifiers, NULL);
1878 runstate_set(RUN_STATE_SUSPENDED);
1879 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1882 void qemu_system_suspend_request(void)
1884 if (runstate_check(RUN_STATE_SUSPENDED)) {
1885 return;
1887 suspend_requested = 1;
1888 cpu_stop_current();
1889 qemu_notify_event();
1892 void qemu_register_suspend_notifier(Notifier *notifier)
1894 notifier_list_add(&suspend_notifiers, notifier);
1897 void qemu_system_wakeup_request(WakeupReason reason)
1899 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1900 return;
1902 if (!(wakeup_reason_mask & (1 << reason))) {
1903 return;
1905 runstate_set(RUN_STATE_RUNNING);
1906 wakeup_reason = reason;
1907 qemu_notify_event();
1910 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1912 if (enabled) {
1913 wakeup_reason_mask |= (1 << reason);
1914 } else {
1915 wakeup_reason_mask &= ~(1 << reason);
1919 void qemu_register_wakeup_notifier(Notifier *notifier)
1921 notifier_list_add(&wakeup_notifiers, notifier);
1924 void qemu_system_killed(int signal, pid_t pid)
1926 shutdown_signal = signal;
1927 shutdown_pid = pid;
1928 no_shutdown = 0;
1929 qemu_system_shutdown_request();
1932 void qemu_system_shutdown_request(void)
1934 shutdown_requested = 1;
1935 qemu_notify_event();
1938 static void qemu_system_powerdown(void)
1940 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1941 notifier_list_notify(&powerdown_notifiers, NULL);
1944 void qemu_system_powerdown_request(void)
1946 powerdown_requested = 1;
1947 qemu_notify_event();
1950 void qemu_register_powerdown_notifier(Notifier *notifier)
1952 notifier_list_add(&powerdown_notifiers, notifier);
1955 void qemu_system_debug_request(void)
1957 debug_requested = 1;
1958 qemu_notify_event();
1961 void qemu_system_vmstop_request(RunState state)
1963 vmstop_requested = state;
1964 qemu_notify_event();
1967 static bool main_loop_should_exit(void)
1969 RunState r;
1970 if (qemu_debug_requested()) {
1971 vm_stop(RUN_STATE_DEBUG);
1973 if (qemu_suspend_requested()) {
1974 qemu_system_suspend();
1976 if (qemu_shutdown_requested()) {
1977 qemu_kill_report();
1978 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1979 if (no_shutdown) {
1980 vm_stop(RUN_STATE_SHUTDOWN);
1981 } else {
1982 return true;
1985 if (qemu_reset_requested()) {
1986 pause_all_vcpus();
1987 cpu_synchronize_all_states();
1988 qemu_system_reset(VMRESET_REPORT);
1989 resume_all_vcpus();
1990 if (runstate_needs_reset()) {
1991 runstate_set(RUN_STATE_PAUSED);
1994 if (qemu_wakeup_requested()) {
1995 pause_all_vcpus();
1996 cpu_synchronize_all_states();
1997 qemu_system_reset(VMRESET_SILENT);
1998 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1999 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
2000 resume_all_vcpus();
2001 monitor_protocol_event(QEVENT_WAKEUP, NULL);
2003 if (qemu_powerdown_requested()) {
2004 qemu_system_powerdown();
2006 if (qemu_vmstop_requested(&r)) {
2007 vm_stop(r);
2009 return false;
2012 static void main_loop(void)
2014 bool nonblocking;
2015 int last_io = 0;
2016 #ifdef CONFIG_PROFILER
2017 int64_t ti;
2018 #endif
2019 do {
2020 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
2021 #ifdef CONFIG_PROFILER
2022 ti = profile_getclock();
2023 #endif
2024 last_io = main_loop_wait(nonblocking);
2025 #ifdef CONFIG_PROFILER
2026 dev_time += profile_getclock() - ti;
2027 #endif
2028 } while (!main_loop_should_exit());
2031 static void version(void)
2033 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2036 static void QEMU_NORETURN help(int exitcode)
2038 version();
2039 printf("usage: %s [options] [disk_image]\n\n"
2040 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2041 error_get_progname());
2043 #define QEMU_OPTIONS_GENERATE_HELP
2044 #include "qemu-options-wrapper.h"
2046 printf("\nDuring emulation, the following keys are useful:\n"
2047 "ctrl-alt-f toggle full screen\n"
2048 "ctrl-alt-n switch to virtual console 'n'\n"
2049 "ctrl-alt toggle mouse and keyboard grab\n"
2050 "\n"
2051 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2053 exit(exitcode);
2056 #define HAS_ARG 0x0001
2058 typedef struct QEMUOption {
2059 const char *name;
2060 int flags;
2061 int index;
2062 uint32_t arch_mask;
2063 } QEMUOption;
2065 static const QEMUOption qemu_options[] = {
2066 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2067 #define QEMU_OPTIONS_GENERATE_OPTIONS
2068 #include "qemu-options-wrapper.h"
2069 { NULL },
2072 static bool vga_available(void)
2074 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2077 static bool cirrus_vga_available(void)
2079 return object_class_by_name("cirrus-vga")
2080 || object_class_by_name("isa-cirrus-vga");
2083 static bool vmware_vga_available(void)
2085 return object_class_by_name("vmware-svga");
2088 static bool qxl_vga_available(void)
2090 return object_class_by_name("qxl-vga");
2093 static void select_vgahw (const char *p)
2095 const char *opts;
2097 vga_interface_type = VGA_NONE;
2098 if (strstart(p, "std", &opts)) {
2099 if (vga_available()) {
2100 vga_interface_type = VGA_STD;
2101 } else {
2102 fprintf(stderr, "Error: standard VGA not available\n");
2103 exit(0);
2105 } else if (strstart(p, "cirrus", &opts)) {
2106 if (cirrus_vga_available()) {
2107 vga_interface_type = VGA_CIRRUS;
2108 } else {
2109 fprintf(stderr, "Error: Cirrus VGA not available\n");
2110 exit(0);
2112 } else if (strstart(p, "vmware", &opts)) {
2113 if (vmware_vga_available()) {
2114 vga_interface_type = VGA_VMWARE;
2115 } else {
2116 fprintf(stderr, "Error: VMWare SVGA not available\n");
2117 exit(0);
2119 } else if (strstart(p, "xenfb", &opts)) {
2120 vga_interface_type = VGA_XENFB;
2121 } else if (strstart(p, "qxl", &opts)) {
2122 if (qxl_vga_available()) {
2123 vga_interface_type = VGA_QXL;
2124 } else {
2125 fprintf(stderr, "Error: QXL VGA not available\n");
2126 exit(0);
2128 } else if (!strstart(p, "none", &opts)) {
2129 invalid_vga:
2130 fprintf(stderr, "Unknown vga type: %s\n", p);
2131 exit(1);
2133 while (*opts) {
2134 const char *nextopt;
2136 if (strstart(opts, ",retrace=", &nextopt)) {
2137 opts = nextopt;
2138 if (strstart(opts, "dumb", &nextopt))
2139 vga_retrace_method = VGA_RETRACE_DUMB;
2140 else if (strstart(opts, "precise", &nextopt))
2141 vga_retrace_method = VGA_RETRACE_PRECISE;
2142 else goto invalid_vga;
2143 } else goto invalid_vga;
2144 opts = nextopt;
2148 static DisplayType select_display(const char *p)
2150 const char *opts;
2151 DisplayType display = DT_DEFAULT;
2153 if (strstart(p, "sdl", &opts)) {
2154 #ifdef CONFIG_SDL
2155 display = DT_SDL;
2156 while (*opts) {
2157 const char *nextopt;
2159 if (strstart(opts, ",frame=", &nextopt)) {
2160 opts = nextopt;
2161 if (strstart(opts, "on", &nextopt)) {
2162 no_frame = 0;
2163 } else if (strstart(opts, "off", &nextopt)) {
2164 no_frame = 1;
2165 } else {
2166 goto invalid_sdl_args;
2168 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2169 opts = nextopt;
2170 if (strstart(opts, "on", &nextopt)) {
2171 alt_grab = 1;
2172 } else if (strstart(opts, "off", &nextopt)) {
2173 alt_grab = 0;
2174 } else {
2175 goto invalid_sdl_args;
2177 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2178 opts = nextopt;
2179 if (strstart(opts, "on", &nextopt)) {
2180 ctrl_grab = 1;
2181 } else if (strstart(opts, "off", &nextopt)) {
2182 ctrl_grab = 0;
2183 } else {
2184 goto invalid_sdl_args;
2186 } else if (strstart(opts, ",window_close=", &nextopt)) {
2187 opts = nextopt;
2188 if (strstart(opts, "on", &nextopt)) {
2189 no_quit = 0;
2190 } else if (strstart(opts, "off", &nextopt)) {
2191 no_quit = 1;
2192 } else {
2193 goto invalid_sdl_args;
2195 } else {
2196 invalid_sdl_args:
2197 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2198 exit(1);
2200 opts = nextopt;
2202 #else
2203 fprintf(stderr, "SDL support is disabled\n");
2204 exit(1);
2205 #endif
2206 } else if (strstart(p, "vnc", &opts)) {
2207 #ifdef CONFIG_VNC
2208 display_remote++;
2210 if (*opts) {
2211 const char *nextopt;
2213 if (strstart(opts, "=", &nextopt)) {
2214 vnc_display = nextopt;
2217 if (!vnc_display) {
2218 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2219 exit(1);
2221 #else
2222 fprintf(stderr, "VNC support is disabled\n");
2223 exit(1);
2224 #endif
2225 } else if (strstart(p, "curses", &opts)) {
2226 #ifdef CONFIG_CURSES
2227 display = DT_CURSES;
2228 #else
2229 fprintf(stderr, "Curses support is disabled\n");
2230 exit(1);
2231 #endif
2232 } else if (strstart(p, "gtk", &opts)) {
2233 #ifdef CONFIG_GTK
2234 display = DT_GTK;
2235 #else
2236 fprintf(stderr, "GTK support is disabled\n");
2237 exit(1);
2238 #endif
2239 } else if (strstart(p, "none", &opts)) {
2240 display = DT_NONE;
2241 } else {
2242 fprintf(stderr, "Unknown display type: %s\n", p);
2243 exit(1);
2246 return display;
2249 static int balloon_parse(const char *arg)
2251 QemuOpts *opts;
2253 if (strcmp(arg, "none") == 0) {
2254 return 0;
2257 if (!strncmp(arg, "virtio", 6)) {
2258 if (arg[6] == ',') {
2259 /* have params -> parse them */
2260 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2261 if (!opts)
2262 return -1;
2263 } else {
2264 /* create empty opts */
2265 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2266 &error_abort);
2268 qemu_opt_set(opts, "driver", "virtio-balloon");
2269 return 0;
2272 return -1;
2275 #if defined(CONFIG_RUBY)
2276 /* Ruby interface for QEMU. See README.EXT for programming example. */
2277 #warning mit ruby
2278 #include <ruby.h>
2280 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2282 int i;
2283 for (i = 0; i < argc; i++) {
2284 argv[i] = StringValue(argv[i]);
2285 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2287 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2288 //~ mode = 0666; /* default value */
2289 //~ }
2290 return INT2FIX(argc);
2293 void Init_qemu(void)
2295 printf("%s\n", __func__);
2296 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2297 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2298 #if 0
2299 rb_define_method();
2300 #endif
2302 #endif /* CONFIG_RUBY */
2304 #if defined(CONFIG_DLL)
2305 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2307 return FALSE;
2309 #endif /* CONFIG_DLL */
2311 char *qemu_find_file(int type, const char *name)
2313 int i;
2314 const char *subdir;
2315 char *buf;
2317 /* Try the name as a straight path first */
2318 if (access(name, R_OK) == 0) {
2319 trace_load_file(name, name);
2320 return g_strdup(name);
2323 switch (type) {
2324 case QEMU_FILE_TYPE_BIOS:
2325 subdir = "";
2326 break;
2327 case QEMU_FILE_TYPE_KEYMAP:
2328 subdir = "keymaps/";
2329 break;
2330 default:
2331 abort();
2334 for (i = 0; i < data_dir_idx; i++) {
2335 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2336 if (access(buf, R_OK) == 0) {
2337 trace_load_file(name, buf);
2338 return buf;
2340 g_free(buf);
2342 return NULL;
2345 static int device_help_func(QemuOpts *opts, void *opaque)
2347 return qdev_device_help(opts);
2350 static int device_init_func(QemuOpts *opts, void *opaque)
2352 DeviceState *dev;
2354 dev = qdev_device_add(opts);
2355 if (!dev)
2356 return -1;
2357 object_unref(OBJECT(dev));
2358 return 0;
2361 static int chardev_init_func(QemuOpts *opts, void *opaque)
2363 Error *local_err = NULL;
2365 qemu_chr_new_from_opts(opts, NULL, &local_err);
2366 if (error_is_set(&local_err)) {
2367 error_report("%s", error_get_pretty(local_err));
2368 error_free(local_err);
2369 return -1;
2371 return 0;
2374 #ifdef CONFIG_VIRTFS
2375 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2377 int ret;
2378 ret = qemu_fsdev_add(opts);
2380 return ret;
2382 #endif
2384 static int mon_init_func(QemuOpts *opts, void *opaque)
2386 CharDriverState *chr;
2387 const char *chardev;
2388 const char *mode;
2389 int flags;
2391 mode = qemu_opt_get(opts, "mode");
2392 if (mode == NULL) {
2393 mode = "readline";
2395 if (strcmp(mode, "readline") == 0) {
2396 flags = MONITOR_USE_READLINE;
2397 } else if (strcmp(mode, "control") == 0) {
2398 flags = MONITOR_USE_CONTROL;
2399 } else {
2400 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2401 exit(1);
2404 if (qemu_opt_get_bool(opts, "pretty", 0))
2405 flags |= MONITOR_USE_PRETTY;
2407 if (qemu_opt_get_bool(opts, "default", 0))
2408 flags |= MONITOR_IS_DEFAULT;
2410 chardev = qemu_opt_get(opts, "chardev");
2411 chr = qemu_chr_find(chardev);
2412 if (chr == NULL) {
2413 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2414 exit(1);
2417 qemu_chr_fe_claim_no_fail(chr);
2418 monitor_init(chr, flags);
2419 return 0;
2422 static void monitor_parse(const char *optarg, const char *mode)
2424 static int monitor_device_index = 0;
2425 QemuOpts *opts;
2426 const char *p;
2427 char label[32];
2428 int def = 0;
2430 if (strstart(optarg, "chardev:", &p)) {
2431 snprintf(label, sizeof(label), "%s", p);
2432 } else {
2433 snprintf(label, sizeof(label), "compat_monitor%d",
2434 monitor_device_index);
2435 if (monitor_device_index == 0) {
2436 def = 1;
2438 opts = qemu_chr_parse_compat(label, optarg);
2439 if (!opts) {
2440 fprintf(stderr, "parse error: %s\n", optarg);
2441 exit(1);
2445 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2446 if (!opts) {
2447 fprintf(stderr, "duplicate chardev: %s\n", label);
2448 exit(1);
2450 qemu_opt_set(opts, "mode", mode);
2451 qemu_opt_set(opts, "chardev", label);
2452 if (def)
2453 qemu_opt_set(opts, "default", "on");
2454 monitor_device_index++;
2457 struct device_config {
2458 enum {
2459 DEV_USB, /* -usbdevice */
2460 DEV_BT, /* -bt */
2461 DEV_SERIAL, /* -serial */
2462 DEV_PARALLEL, /* -parallel */
2463 DEV_VIRTCON, /* -virtioconsole */
2464 DEV_DEBUGCON, /* -debugcon */
2465 DEV_GDB, /* -gdb, -s */
2466 DEV_SCLP, /* s390 sclp */
2467 } type;
2468 const char *cmdline;
2469 Location loc;
2470 QTAILQ_ENTRY(device_config) next;
2473 static QTAILQ_HEAD(, device_config) device_configs =
2474 QTAILQ_HEAD_INITIALIZER(device_configs);
2476 static void add_device_config(int type, const char *cmdline)
2478 struct device_config *conf;
2480 conf = g_malloc0(sizeof(*conf));
2481 conf->type = type;
2482 conf->cmdline = cmdline;
2483 loc_save(&conf->loc);
2484 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2487 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2489 struct device_config *conf;
2490 int rc;
2492 QTAILQ_FOREACH(conf, &device_configs, next) {
2493 if (conf->type != type)
2494 continue;
2495 loc_push_restore(&conf->loc);
2496 rc = func(conf->cmdline);
2497 loc_pop(&conf->loc);
2498 if (0 != rc)
2499 return rc;
2501 return 0;
2504 static int serial_parse(const char *devname)
2506 static int index = 0;
2507 char label[32];
2509 if (strcmp(devname, "none") == 0)
2510 return 0;
2511 if (index == MAX_SERIAL_PORTS) {
2512 fprintf(stderr, "qemu: too many serial ports\n");
2513 exit(1);
2515 snprintf(label, sizeof(label), "serial%d", index);
2516 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2517 if (!serial_hds[index]) {
2518 fprintf(stderr, "qemu: could not connect serial device"
2519 " to character backend '%s'\n", devname);
2520 return -1;
2522 index++;
2523 return 0;
2526 static int parallel_parse(const char *devname)
2528 static int index = 0;
2529 char label[32];
2531 if (strcmp(devname, "none") == 0)
2532 return 0;
2533 if (index == MAX_PARALLEL_PORTS) {
2534 fprintf(stderr, "qemu: too many parallel ports\n");
2535 exit(1);
2537 snprintf(label, sizeof(label), "parallel%d", index);
2538 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2539 if (!parallel_hds[index]) {
2540 fprintf(stderr, "qemu: could not connect parallel device"
2541 " to character backend '%s'\n", devname);
2542 return -1;
2544 index++;
2545 return 0;
2548 static int virtcon_parse(const char *devname)
2550 QemuOptsList *device = qemu_find_opts("device");
2551 static int index = 0;
2552 char label[32];
2553 QemuOpts *bus_opts, *dev_opts;
2555 if (strcmp(devname, "none") == 0)
2556 return 0;
2557 if (index == MAX_VIRTIO_CONSOLES) {
2558 fprintf(stderr, "qemu: too many virtio consoles\n");
2559 exit(1);
2562 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2563 if (arch_type == QEMU_ARCH_S390X) {
2564 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2565 } else {
2566 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2569 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2570 qemu_opt_set(dev_opts, "driver", "virtconsole");
2572 snprintf(label, sizeof(label), "virtcon%d", index);
2573 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2574 if (!virtcon_hds[index]) {
2575 fprintf(stderr, "qemu: could not connect virtio console"
2576 " to character backend '%s'\n", devname);
2577 return -1;
2579 qemu_opt_set(dev_opts, "chardev", label);
2581 index++;
2582 return 0;
2585 static int sclp_parse(const char *devname)
2587 QemuOptsList *device = qemu_find_opts("device");
2588 static int index = 0;
2589 char label[32];
2590 QemuOpts *dev_opts;
2592 if (strcmp(devname, "none") == 0) {
2593 return 0;
2595 if (index == MAX_SCLP_CONSOLES) {
2596 fprintf(stderr, "qemu: too many sclp consoles\n");
2597 exit(1);
2600 assert(arch_type == QEMU_ARCH_S390X);
2602 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2603 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2605 snprintf(label, sizeof(label), "sclpcon%d", index);
2606 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2607 if (!sclp_hds[index]) {
2608 fprintf(stderr, "qemu: could not connect sclp console"
2609 " to character backend '%s'\n", devname);
2610 return -1;
2612 qemu_opt_set(dev_opts, "chardev", label);
2614 index++;
2615 return 0;
2618 static int debugcon_parse(const char *devname)
2620 QemuOpts *opts;
2622 if (!qemu_chr_new("debugcon", devname, NULL)) {
2623 exit(1);
2625 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2626 if (!opts) {
2627 fprintf(stderr, "qemu: already have a debugcon device\n");
2628 exit(1);
2630 qemu_opt_set(opts, "driver", "isa-debugcon");
2631 qemu_opt_set(opts, "chardev", "debugcon");
2632 return 0;
2635 static QEMUMachine *machine_parse(const char *name)
2637 QEMUMachine *m, *machine = NULL;
2639 if (name) {
2640 machine = find_machine(name);
2642 if (machine) {
2643 return machine;
2645 printf("Supported machines are:\n");
2646 for (m = first_machine; m != NULL; m = m->next) {
2647 if (m->alias) {
2648 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2650 printf("%-20s %s%s\n", m->name, m->desc,
2651 m->is_default ? " (default)" : "");
2653 exit(!name || !is_help_option(name));
2656 static int tcg_init(void)
2658 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2659 return 0;
2662 static struct {
2663 const char *opt_name;
2664 const char *name;
2665 int (*available)(void);
2666 int (*init)(void);
2667 bool *allowed;
2668 } accel_list[] = {
2669 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2670 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2671 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2672 { "qtest", "QTest", qtest_available, qtest_init_accel, &qtest_allowed },
2675 static int configure_accelerator(void)
2677 const char *p;
2678 char buf[10];
2679 int i, ret;
2680 bool accel_initialised = false;
2681 bool init_failed = false;
2683 p = qemu_opt_get(qemu_get_machine_opts(), "accel");
2684 if (p == NULL) {
2685 /* Use the default "accelerator", tcg */
2686 p = "tcg";
2689 while (!accel_initialised && *p != '\0') {
2690 if (*p == ':') {
2691 p++;
2693 p = get_opt_name(buf, sizeof (buf), p, ':');
2694 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2695 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2696 if (!accel_list[i].available()) {
2697 printf("%s not supported for this target\n",
2698 accel_list[i].name);
2699 continue;
2701 *(accel_list[i].allowed) = true;
2702 ret = accel_list[i].init();
2703 if (ret < 0) {
2704 init_failed = true;
2705 fprintf(stderr, "failed to initialize %s: %s\n",
2706 accel_list[i].name,
2707 strerror(-ret));
2708 *(accel_list[i].allowed) = false;
2709 } else {
2710 accel_initialised = true;
2712 break;
2715 if (i == ARRAY_SIZE(accel_list)) {
2716 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2720 if (!accel_initialised) {
2721 if (!init_failed) {
2722 fprintf(stderr, "No accelerator found!\n");
2724 exit(1);
2727 if (init_failed) {
2728 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2731 return !accel_initialised;
2734 void qemu_add_exit_notifier(Notifier *notify)
2736 notifier_list_add(&exit_notifiers, notify);
2739 void qemu_remove_exit_notifier(Notifier *notify)
2741 notifier_remove(notify);
2744 static void qemu_run_exit_notifiers(void)
2746 notifier_list_notify(&exit_notifiers, NULL);
2749 void qemu_add_machine_init_done_notifier(Notifier *notify)
2751 notifier_list_add(&machine_init_done_notifiers, notify);
2754 static void qemu_run_machine_init_done_notifiers(void)
2756 notifier_list_notify(&machine_init_done_notifiers, NULL);
2759 static const QEMUOption *lookup_opt(int argc, char **argv,
2760 const char **poptarg, int *poptind)
2762 const QEMUOption *popt;
2763 int optind = *poptind;
2764 char *r = argv[optind];
2765 const char *optarg;
2767 loc_set_cmdline(argv, optind, 1);
2768 optind++;
2769 /* Treat --foo the same as -foo. */
2770 if (r[1] == '-')
2771 r++;
2772 popt = qemu_options;
2773 for(;;) {
2774 if (!popt->name) {
2775 error_report("invalid option");
2776 exit(1);
2778 if (!strcmp(popt->name, r + 1))
2779 break;
2780 popt++;
2782 if (popt->flags & HAS_ARG) {
2783 if (optind >= argc) {
2784 error_report("requires an argument");
2785 exit(1);
2787 optarg = argv[optind++];
2788 loc_set_cmdline(argv, optind - 2, 2);
2789 } else {
2790 optarg = NULL;
2793 *poptarg = optarg;
2794 *poptind = optind;
2796 return popt;
2799 static gpointer malloc_and_trace(gsize n_bytes)
2801 void *ptr = malloc(n_bytes);
2802 trace_g_malloc(n_bytes, ptr);
2803 return ptr;
2806 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2808 void *ptr = realloc(mem, n_bytes);
2809 trace_g_realloc(mem, n_bytes, ptr);
2810 return ptr;
2813 static void free_and_trace(gpointer mem)
2815 trace_g_free(mem);
2816 free(mem);
2819 static int object_set_property(const char *name, const char *value, void *opaque)
2821 Object *obj = OBJECT(opaque);
2822 StringInputVisitor *siv;
2823 Error *local_err = NULL;
2825 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2826 return 0;
2829 siv = string_input_visitor_new(value);
2830 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2831 string_input_visitor_cleanup(siv);
2833 if (local_err) {
2834 qerror_report_err(local_err);
2835 error_free(local_err);
2836 return -1;
2839 return 0;
2842 static int object_create(QemuOpts *opts, void *opaque)
2844 const char *type = qemu_opt_get(opts, "qom-type");
2845 const char *id = qemu_opts_id(opts);
2846 Object *obj;
2848 g_assert(type != NULL);
2850 if (id == NULL) {
2851 qerror_report(QERR_MISSING_PARAMETER, "id");
2852 return -1;
2855 obj = object_new(type);
2856 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2857 object_unref(obj);
2858 return -1;
2861 object_property_add_child(container_get(object_get_root(), "/objects"),
2862 id, obj, NULL);
2863 object_unref(obj);
2864 return 0;
2867 int main(int argc, char **argv)
2869 int i;
2870 int snapshot, linux_boot;
2871 const char *icount_option = NULL;
2872 const char *initrd_filename;
2873 const char *kernel_filename, *kernel_cmdline;
2874 const char *boot_order;
2875 DisplayState *ds;
2876 int cyls, heads, secs, translation;
2877 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2878 QemuOptsList *olist;
2879 int optind;
2880 const char *optarg;
2881 const char *loadvm = NULL;
2882 QEMUMachine *machine;
2883 const char *cpu_model;
2884 const char *vga_model = "none";
2885 const char *qtest_chrdev = NULL;
2886 const char *qtest_log = NULL;
2887 const char *pid_file = NULL;
2888 const char *incoming = NULL;
2889 #ifdef CONFIG_VNC
2890 int show_vnc_port = 0;
2891 #endif
2892 bool defconfig = true;
2893 bool userconfig = true;
2894 const char *log_mask = NULL;
2895 const char *log_file = NULL;
2896 GMemVTable mem_trace = {
2897 .malloc = malloc_and_trace,
2898 .realloc = realloc_and_trace,
2899 .free = free_and_trace,
2901 const char *trace_events = NULL;
2902 const char *trace_file = NULL;
2904 atexit(qemu_run_exit_notifiers);
2905 error_set_progname(argv[0]);
2907 g_mem_set_vtable(&mem_trace);
2908 if (!g_thread_supported()) {
2909 #if !GLIB_CHECK_VERSION(2, 31, 0)
2910 g_thread_init(NULL);
2911 #else
2912 fprintf(stderr, "glib threading failed to initialize.\n");
2913 exit(1);
2914 #endif
2917 module_call_init(MODULE_INIT_QOM);
2919 qemu_add_opts(&qemu_drive_opts);
2920 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2921 qemu_add_drive_opts(&qemu_common_drive_opts);
2922 qemu_add_drive_opts(&qemu_drive_opts);
2923 qemu_add_opts(&qemu_chardev_opts);
2924 qemu_add_opts(&qemu_device_opts);
2925 qemu_add_opts(&qemu_netdev_opts);
2926 qemu_add_opts(&qemu_net_opts);
2927 qemu_add_opts(&qemu_rtc_opts);
2928 qemu_add_opts(&qemu_global_opts);
2929 qemu_add_opts(&qemu_mon_opts);
2930 qemu_add_opts(&qemu_trace_opts);
2931 qemu_add_opts(&qemu_option_rom_opts);
2932 qemu_add_opts(&qemu_machine_opts);
2933 qemu_add_opts(&qemu_smp_opts);
2934 qemu_add_opts(&qemu_boot_opts);
2935 qemu_add_opts(&qemu_sandbox_opts);
2936 qemu_add_opts(&qemu_add_fd_opts);
2937 qemu_add_opts(&qemu_object_opts);
2938 qemu_add_opts(&qemu_tpmdev_opts);
2939 qemu_add_opts(&qemu_realtime_opts);
2940 qemu_add_opts(&qemu_msg_opts);
2942 runstate_init();
2944 init_clocks();
2945 rtc_clock = QEMU_CLOCK_HOST;
2947 qemu_cache_utils_init();
2949 QLIST_INIT (&vm_change_state_head);
2950 os_setup_early_signal_handling();
2952 module_call_init(MODULE_INIT_MACHINE);
2953 machine = 0;
2954 optarg = strrchr(argv[0], '/');
2955 if (optarg != 0) {
2956 machine = find_machine(optarg + 1);
2958 if (!machine) {
2959 machine = find_default_machine();
2961 cpu_model = NULL;
2962 ram_size = 0;
2963 snapshot = 0;
2964 cyls = heads = secs = 0;
2965 translation = BIOS_ATA_TRANSLATION_AUTO;
2967 for (i = 0; i < MAX_NODES; i++) {
2968 node_mem[i] = 0;
2969 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2972 nb_numa_nodes = 0;
2973 nb_nics = 0;
2975 bdrv_init_with_whitelist();
2977 autostart = 1;
2979 /* first pass of option parsing */
2980 optind = 1;
2981 while (optind < argc) {
2982 if (argv[optind][0] != '-') {
2983 /* disk image */
2984 optind++;
2985 continue;
2986 } else {
2987 const QEMUOption *popt;
2989 popt = lookup_opt(argc, argv, &optarg, &optind);
2990 switch (popt->index) {
2991 case QEMU_OPTION_nodefconfig:
2992 defconfig = false;
2993 break;
2994 case QEMU_OPTION_nouserconfig:
2995 userconfig = false;
2996 break;
3001 if (defconfig) {
3002 int ret;
3003 ret = qemu_read_default_config_files(userconfig);
3004 if (ret < 0) {
3005 exit(1);
3009 /* second pass of option parsing */
3010 optind = 1;
3011 for(;;) {
3012 if (optind >= argc)
3013 break;
3014 if (argv[optind][0] != '-') {
3015 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3016 } else {
3017 const QEMUOption *popt;
3019 popt = lookup_opt(argc, argv, &optarg, &optind);
3020 if (!(popt->arch_mask & arch_type)) {
3021 printf("Option %s not supported for this target\n", popt->name);
3022 exit(1);
3024 switch(popt->index) {
3025 case QEMU_OPTION_M:
3026 machine = machine_parse(optarg);
3027 break;
3028 case QEMU_OPTION_no_kvm_irqchip: {
3029 olist = qemu_find_opts("machine");
3030 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
3031 break;
3033 case QEMU_OPTION_cpu:
3034 /* hw initialization will check this */
3035 cpu_model = optarg;
3036 break;
3037 case QEMU_OPTION_hda:
3039 char buf[256];
3040 if (cyls == 0)
3041 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3042 else
3043 snprintf(buf, sizeof(buf),
3044 "%s,cyls=%d,heads=%d,secs=%d%s",
3045 HD_OPTS , cyls, heads, secs,
3046 translation == BIOS_ATA_TRANSLATION_LBA ?
3047 ",trans=lba" :
3048 translation == BIOS_ATA_TRANSLATION_NONE ?
3049 ",trans=none" : "");
3050 drive_add(IF_DEFAULT, 0, optarg, buf);
3051 break;
3053 case QEMU_OPTION_hdb:
3054 case QEMU_OPTION_hdc:
3055 case QEMU_OPTION_hdd:
3056 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3057 HD_OPTS);
3058 break;
3059 case QEMU_OPTION_drive:
3060 if (drive_def(optarg) == NULL) {
3061 exit(1);
3063 break;
3064 case QEMU_OPTION_set:
3065 if (qemu_set_option(optarg) != 0)
3066 exit(1);
3067 break;
3068 case QEMU_OPTION_global:
3069 if (qemu_global_option(optarg) != 0)
3070 exit(1);
3071 break;
3072 case QEMU_OPTION_mtdblock:
3073 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3074 break;
3075 case QEMU_OPTION_sd:
3076 drive_add(IF_SD, -1, optarg, SD_OPTS);
3077 break;
3078 case QEMU_OPTION_pflash:
3079 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3080 break;
3081 case QEMU_OPTION_snapshot:
3082 snapshot = 1;
3083 break;
3084 case QEMU_OPTION_hdachs:
3086 const char *p;
3087 p = optarg;
3088 cyls = strtol(p, (char **)&p, 0);
3089 if (cyls < 1 || cyls > 16383)
3090 goto chs_fail;
3091 if (*p != ',')
3092 goto chs_fail;
3093 p++;
3094 heads = strtol(p, (char **)&p, 0);
3095 if (heads < 1 || heads > 16)
3096 goto chs_fail;
3097 if (*p != ',')
3098 goto chs_fail;
3099 p++;
3100 secs = strtol(p, (char **)&p, 0);
3101 if (secs < 1 || secs > 63)
3102 goto chs_fail;
3103 if (*p == ',') {
3104 p++;
3105 if (!strcmp(p, "none"))
3106 translation = BIOS_ATA_TRANSLATION_NONE;
3107 else if (!strcmp(p, "lba"))
3108 translation = BIOS_ATA_TRANSLATION_LBA;
3109 else if (!strcmp(p, "auto"))
3110 translation = BIOS_ATA_TRANSLATION_AUTO;
3111 else
3112 goto chs_fail;
3113 } else if (*p != '\0') {
3114 chs_fail:
3115 fprintf(stderr, "qemu: invalid physical CHS format\n");
3116 exit(1);
3118 if (hda_opts != NULL) {
3119 char num[16];
3120 snprintf(num, sizeof(num), "%d", cyls);
3121 qemu_opt_set(hda_opts, "cyls", num);
3122 snprintf(num, sizeof(num), "%d", heads);
3123 qemu_opt_set(hda_opts, "heads", num);
3124 snprintf(num, sizeof(num), "%d", secs);
3125 qemu_opt_set(hda_opts, "secs", num);
3126 if (translation == BIOS_ATA_TRANSLATION_LBA)
3127 qemu_opt_set(hda_opts, "trans", "lba");
3128 if (translation == BIOS_ATA_TRANSLATION_NONE)
3129 qemu_opt_set(hda_opts, "trans", "none");
3132 break;
3133 case QEMU_OPTION_numa:
3134 numa_add(optarg);
3135 break;
3136 case QEMU_OPTION_display:
3137 display_type = select_display(optarg);
3138 break;
3139 case QEMU_OPTION_nographic:
3140 display_type = DT_NOGRAPHIC;
3141 break;
3142 case QEMU_OPTION_curses:
3143 #ifdef CONFIG_CURSES
3144 display_type = DT_CURSES;
3145 #else
3146 fprintf(stderr, "Curses support is disabled\n");
3147 exit(1);
3148 #endif
3149 break;
3150 case QEMU_OPTION_portrait:
3151 graphic_rotate = 90;
3152 break;
3153 case QEMU_OPTION_rotate:
3154 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3155 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3156 graphic_rotate != 180 && graphic_rotate != 270) {
3157 fprintf(stderr,
3158 "qemu: only 90, 180, 270 deg rotation is available\n");
3159 exit(1);
3161 break;
3162 case QEMU_OPTION_kernel:
3163 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3164 break;
3165 case QEMU_OPTION_initrd:
3166 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3167 break;
3168 case QEMU_OPTION_append:
3169 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3170 break;
3171 case QEMU_OPTION_dtb:
3172 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3173 break;
3174 case QEMU_OPTION_cdrom:
3175 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3176 break;
3177 case QEMU_OPTION_boot:
3178 opts = qemu_opts_parse(qemu_find_opts("boot-opts"), optarg, 1);
3179 if (!opts) {
3180 exit(1);
3182 break;
3183 case QEMU_OPTION_fda:
3184 case QEMU_OPTION_fdb:
3185 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3186 optarg, FD_OPTS);
3187 break;
3188 case QEMU_OPTION_no_fd_bootchk:
3189 fd_bootchk = 0;
3190 break;
3191 case QEMU_OPTION_netdev:
3192 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3193 exit(1);
3195 break;
3196 case QEMU_OPTION_net:
3197 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3198 exit(1);
3200 break;
3201 #ifdef CONFIG_LIBISCSI
3202 case QEMU_OPTION_iscsi:
3203 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3204 if (!opts) {
3205 exit(1);
3207 break;
3208 #endif
3209 #ifdef CONFIG_SLIRP
3210 case QEMU_OPTION_tftp:
3211 legacy_tftp_prefix = optarg;
3212 break;
3213 case QEMU_OPTION_bootp:
3214 legacy_bootp_filename = optarg;
3215 break;
3216 case QEMU_OPTION_redir:
3217 if (net_slirp_redir(optarg) < 0)
3218 exit(1);
3219 break;
3220 #endif
3221 case QEMU_OPTION_bt:
3222 add_device_config(DEV_BT, optarg);
3223 break;
3224 case QEMU_OPTION_audio_help:
3225 AUD_help ();
3226 exit (0);
3227 break;
3228 case QEMU_OPTION_soundhw:
3229 select_soundhw (optarg);
3230 break;
3231 case QEMU_OPTION_h:
3232 help(0);
3233 break;
3234 case QEMU_OPTION_version:
3235 version();
3236 exit(0);
3237 break;
3238 case QEMU_OPTION_m: {
3239 int64_t value;
3240 uint64_t sz;
3241 char *end;
3243 value = strtosz(optarg, &end);
3244 if (value < 0 || *end) {
3245 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
3246 exit(1);
3248 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3249 ram_size = sz;
3250 if (ram_size != sz) {
3251 fprintf(stderr, "qemu: ram size too large\n");
3252 exit(1);
3254 break;
3256 #ifdef CONFIG_TPM
3257 case QEMU_OPTION_tpmdev:
3258 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3259 exit(1);
3261 break;
3262 #endif
3263 case QEMU_OPTION_mempath:
3264 mem_path = optarg;
3265 break;
3266 case QEMU_OPTION_mem_prealloc:
3267 mem_prealloc = 1;
3268 break;
3269 case QEMU_OPTION_d:
3270 log_mask = optarg;
3271 break;
3272 case QEMU_OPTION_D:
3273 log_file = optarg;
3274 break;
3275 case QEMU_OPTION_s:
3276 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3277 break;
3278 case QEMU_OPTION_gdb:
3279 add_device_config(DEV_GDB, optarg);
3280 break;
3281 case QEMU_OPTION_L:
3282 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3283 data_dir[data_dir_idx++] = optarg;
3285 break;
3286 case QEMU_OPTION_bios:
3287 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg);
3288 break;
3289 case QEMU_OPTION_singlestep:
3290 singlestep = 1;
3291 break;
3292 case QEMU_OPTION_S:
3293 autostart = 0;
3294 break;
3295 case QEMU_OPTION_k:
3296 keyboard_layout = optarg;
3297 break;
3298 case QEMU_OPTION_localtime:
3299 rtc_utc = 0;
3300 break;
3301 case QEMU_OPTION_vga:
3302 vga_model = optarg;
3303 default_vga = 0;
3304 break;
3305 case QEMU_OPTION_g:
3307 const char *p;
3308 int w, h, depth;
3309 p = optarg;
3310 w = strtol(p, (char **)&p, 10);
3311 if (w <= 0) {
3312 graphic_error:
3313 fprintf(stderr, "qemu: invalid resolution or depth\n");
3314 exit(1);
3316 if (*p != 'x')
3317 goto graphic_error;
3318 p++;
3319 h = strtol(p, (char **)&p, 10);
3320 if (h <= 0)
3321 goto graphic_error;
3322 if (*p == 'x') {
3323 p++;
3324 depth = strtol(p, (char **)&p, 10);
3325 if (depth != 8 && depth != 15 && depth != 16 &&
3326 depth != 24 && depth != 32)
3327 goto graphic_error;
3328 } else if (*p == '\0') {
3329 depth = graphic_depth;
3330 } else {
3331 goto graphic_error;
3334 graphic_width = w;
3335 graphic_height = h;
3336 graphic_depth = depth;
3338 break;
3339 case QEMU_OPTION_echr:
3341 char *r;
3342 term_escape_char = strtol(optarg, &r, 0);
3343 if (r == optarg)
3344 printf("Bad argument to echr\n");
3345 break;
3347 case QEMU_OPTION_monitor:
3348 default_monitor = 0;
3349 if (strncmp(optarg, "none", 4)) {
3350 monitor_parse(optarg, "readline");
3352 break;
3353 case QEMU_OPTION_qmp:
3354 monitor_parse(optarg, "control");
3355 default_monitor = 0;
3356 break;
3357 case QEMU_OPTION_mon:
3358 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3359 if (!opts) {
3360 exit(1);
3362 default_monitor = 0;
3363 break;
3364 case QEMU_OPTION_chardev:
3365 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3366 if (!opts) {
3367 exit(1);
3369 break;
3370 case QEMU_OPTION_fsdev:
3371 olist = qemu_find_opts("fsdev");
3372 if (!olist) {
3373 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3374 exit(1);
3376 opts = qemu_opts_parse(olist, optarg, 1);
3377 if (!opts) {
3378 exit(1);
3380 break;
3381 case QEMU_OPTION_virtfs: {
3382 QemuOpts *fsdev;
3383 QemuOpts *device;
3384 const char *writeout, *sock_fd, *socket;
3386 olist = qemu_find_opts("virtfs");
3387 if (!olist) {
3388 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3389 exit(1);
3391 opts = qemu_opts_parse(olist, optarg, 1);
3392 if (!opts) {
3393 exit(1);
3396 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3397 qemu_opt_get(opts, "mount_tag") == NULL) {
3398 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3399 exit(1);
3401 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3402 qemu_opt_get(opts, "mount_tag"),
3403 1, NULL);
3404 if (!fsdev) {
3405 fprintf(stderr, "duplicate fsdev id: %s\n",
3406 qemu_opt_get(opts, "mount_tag"));
3407 exit(1);
3410 writeout = qemu_opt_get(opts, "writeout");
3411 if (writeout) {
3412 #ifdef CONFIG_SYNC_FILE_RANGE
3413 qemu_opt_set(fsdev, "writeout", writeout);
3414 #else
3415 fprintf(stderr, "writeout=immediate not supported on "
3416 "this platform\n");
3417 exit(1);
3418 #endif
3420 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3421 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3422 qemu_opt_set(fsdev, "security_model",
3423 qemu_opt_get(opts, "security_model"));
3424 socket = qemu_opt_get(opts, "socket");
3425 if (socket) {
3426 qemu_opt_set(fsdev, "socket", socket);
3428 sock_fd = qemu_opt_get(opts, "sock_fd");
3429 if (sock_fd) {
3430 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3433 qemu_opt_set_bool(fsdev, "readonly",
3434 qemu_opt_get_bool(opts, "readonly", 0));
3435 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3436 &error_abort);
3437 qemu_opt_set(device, "driver", "virtio-9p-pci");
3438 qemu_opt_set(device, "fsdev",
3439 qemu_opt_get(opts, "mount_tag"));
3440 qemu_opt_set(device, "mount_tag",
3441 qemu_opt_get(opts, "mount_tag"));
3442 break;
3444 case QEMU_OPTION_virtfs_synth: {
3445 QemuOpts *fsdev;
3446 QemuOpts *device;
3448 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3449 1, NULL);
3450 if (!fsdev) {
3451 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3452 exit(1);
3454 qemu_opt_set(fsdev, "fsdriver", "synth");
3456 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3457 &error_abort);
3458 qemu_opt_set(device, "driver", "virtio-9p-pci");
3459 qemu_opt_set(device, "fsdev", "v_synth");
3460 qemu_opt_set(device, "mount_tag", "v_synth");
3461 break;
3463 case QEMU_OPTION_serial:
3464 add_device_config(DEV_SERIAL, optarg);
3465 default_serial = 0;
3466 if (strncmp(optarg, "mon:", 4) == 0) {
3467 default_monitor = 0;
3469 break;
3470 case QEMU_OPTION_watchdog:
3471 if (watchdog) {
3472 fprintf(stderr,
3473 "qemu: only one watchdog option may be given\n");
3474 return 1;
3476 watchdog = optarg;
3477 break;
3478 case QEMU_OPTION_watchdog_action:
3479 if (select_watchdog_action(optarg) == -1) {
3480 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3481 exit(1);
3483 break;
3484 case QEMU_OPTION_virtiocon:
3485 add_device_config(DEV_VIRTCON, optarg);
3486 default_virtcon = 0;
3487 if (strncmp(optarg, "mon:", 4) == 0) {
3488 default_monitor = 0;
3490 break;
3491 case QEMU_OPTION_parallel:
3492 add_device_config(DEV_PARALLEL, optarg);
3493 default_parallel = 0;
3494 if (strncmp(optarg, "mon:", 4) == 0) {
3495 default_monitor = 0;
3497 break;
3498 case QEMU_OPTION_debugcon:
3499 add_device_config(DEV_DEBUGCON, optarg);
3500 break;
3501 case QEMU_OPTION_loadvm:
3502 loadvm = optarg;
3503 break;
3504 case QEMU_OPTION_full_screen:
3505 full_screen = 1;
3506 break;
3507 case QEMU_OPTION_no_frame:
3508 no_frame = 1;
3509 break;
3510 case QEMU_OPTION_alt_grab:
3511 alt_grab = 1;
3512 break;
3513 case QEMU_OPTION_ctrl_grab:
3514 ctrl_grab = 1;
3515 break;
3516 case QEMU_OPTION_no_quit:
3517 no_quit = 1;
3518 break;
3519 case QEMU_OPTION_sdl:
3520 #ifdef CONFIG_SDL
3521 display_type = DT_SDL;
3522 break;
3523 #else
3524 fprintf(stderr, "SDL support is disabled\n");
3525 exit(1);
3526 #endif
3527 case QEMU_OPTION_pidfile:
3528 pid_file = optarg;
3529 break;
3530 case QEMU_OPTION_win2k_hack:
3531 win2k_install_hack = 1;
3532 break;
3533 case QEMU_OPTION_rtc_td_hack: {
3534 static GlobalProperty slew_lost_ticks[] = {
3536 .driver = "mc146818rtc",
3537 .property = "lost_tick_policy",
3538 .value = "slew",
3540 { /* end of list */ }
3543 qdev_prop_register_global_list(slew_lost_ticks);
3544 break;
3546 case QEMU_OPTION_acpitable:
3547 opts = qemu_opts_parse(qemu_find_opts("acpi"), optarg, 1);
3548 if (!opts) {
3549 exit(1);
3551 do_acpitable_option(opts);
3552 break;
3553 case QEMU_OPTION_smbios:
3554 opts = qemu_opts_parse(qemu_find_opts("smbios"), optarg, 0);
3555 if (!opts) {
3556 exit(1);
3558 do_smbios_option(opts);
3559 break;
3560 case QEMU_OPTION_enable_kvm:
3561 olist = qemu_find_opts("machine");
3562 qemu_opts_parse(olist, "accel=kvm", 0);
3563 break;
3564 case QEMU_OPTION_machine:
3565 olist = qemu_find_opts("machine");
3566 opts = qemu_opts_parse(olist, optarg, 1);
3567 if (!opts) {
3568 exit(1);
3570 optarg = qemu_opt_get(opts, "type");
3571 if (optarg) {
3572 machine = machine_parse(optarg);
3574 break;
3575 case QEMU_OPTION_no_kvm:
3576 olist = qemu_find_opts("machine");
3577 qemu_opts_parse(olist, "accel=tcg", 0);
3578 break;
3579 case QEMU_OPTION_no_kvm_pit: {
3580 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3581 "separately.\n");
3582 break;
3584 case QEMU_OPTION_no_kvm_pit_reinjection: {
3585 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3587 .driver = "kvm-pit",
3588 .property = "lost_tick_policy",
3589 .value = "discard",
3591 { /* end of list */ }
3594 fprintf(stderr, "Warning: option deprecated, use "
3595 "lost_tick_policy property of kvm-pit instead.\n");
3596 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3597 break;
3599 case QEMU_OPTION_usb:
3600 olist = qemu_find_opts("machine");
3601 qemu_opts_parse(olist, "usb=on", 0);
3602 break;
3603 case QEMU_OPTION_usbdevice:
3604 olist = qemu_find_opts("machine");
3605 qemu_opts_parse(olist, "usb=on", 0);
3606 add_device_config(DEV_USB, optarg);
3607 break;
3608 case QEMU_OPTION_device:
3609 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3610 exit(1);
3612 break;
3613 case QEMU_OPTION_smp:
3614 if (!qemu_opts_parse(qemu_find_opts("smp-opts"), optarg, 1)) {
3615 exit(1);
3617 break;
3618 case QEMU_OPTION_vnc:
3619 #ifdef CONFIG_VNC
3620 display_remote++;
3621 vnc_display = optarg;
3622 #else
3623 fprintf(stderr, "VNC support is disabled\n");
3624 exit(1);
3625 #endif
3626 break;
3627 case QEMU_OPTION_no_acpi:
3628 acpi_enabled = 0;
3629 break;
3630 case QEMU_OPTION_no_hpet:
3631 no_hpet = 1;
3632 break;
3633 case QEMU_OPTION_balloon:
3634 if (balloon_parse(optarg) < 0) {
3635 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3636 exit(1);
3638 break;
3639 case QEMU_OPTION_no_reboot:
3640 no_reboot = 1;
3641 break;
3642 case QEMU_OPTION_no_shutdown:
3643 no_shutdown = 1;
3644 break;
3645 case QEMU_OPTION_show_cursor:
3646 cursor_hide = 0;
3647 break;
3648 case QEMU_OPTION_uuid:
3649 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3650 fprintf(stderr, "Fail to parse UUID string."
3651 " Wrong format.\n");
3652 exit(1);
3654 qemu_uuid_set = true;
3655 break;
3656 case QEMU_OPTION_option_rom:
3657 if (nb_option_roms >= MAX_OPTION_ROMS) {
3658 fprintf(stderr, "Too many option ROMs\n");
3659 exit(1);
3661 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3662 if (!opts) {
3663 exit(1);
3665 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3666 option_rom[nb_option_roms].bootindex =
3667 qemu_opt_get_number(opts, "bootindex", -1);
3668 if (!option_rom[nb_option_roms].name) {
3669 fprintf(stderr, "Option ROM file is not specified\n");
3670 exit(1);
3672 nb_option_roms++;
3673 break;
3674 case QEMU_OPTION_semihosting:
3675 semihosting_enabled = 1;
3676 break;
3677 case QEMU_OPTION_tdf:
3678 fprintf(stderr, "Warning: user space PIT time drift fix "
3679 "is no longer supported.\n");
3680 break;
3681 case QEMU_OPTION_name:
3682 qemu_name = g_strdup(optarg);
3684 char *p = strchr(qemu_name, ',');
3685 if (p != NULL) {
3686 *p++ = 0;
3687 if (strncmp(p, "process=", 8)) {
3688 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3689 exit(1);
3691 p += 8;
3692 os_set_proc_name(p);
3695 break;
3696 case QEMU_OPTION_prom_env:
3697 if (nb_prom_envs >= MAX_PROM_ENVS) {
3698 fprintf(stderr, "Too many prom variables\n");
3699 exit(1);
3701 prom_envs[nb_prom_envs] = optarg;
3702 nb_prom_envs++;
3703 break;
3704 case QEMU_OPTION_old_param:
3705 old_param = 1;
3706 break;
3707 case QEMU_OPTION_clock:
3708 /* Clock options no longer exist. Keep this option for
3709 * backward compatibility.
3711 break;
3712 case QEMU_OPTION_startdate:
3713 configure_rtc_date_offset(optarg, 1);
3714 break;
3715 case QEMU_OPTION_rtc:
3716 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3717 if (!opts) {
3718 exit(1);
3720 configure_rtc(opts);
3721 break;
3722 case QEMU_OPTION_tb_size:
3723 tcg_tb_size = strtol(optarg, NULL, 0);
3724 if (tcg_tb_size < 0) {
3725 tcg_tb_size = 0;
3727 break;
3728 case QEMU_OPTION_icount:
3729 icount_option = optarg;
3730 break;
3731 case QEMU_OPTION_incoming:
3732 incoming = optarg;
3733 runstate_set(RUN_STATE_INMIGRATE);
3734 break;
3735 case QEMU_OPTION_nodefaults:
3736 default_serial = 0;
3737 default_parallel = 0;
3738 default_virtcon = 0;
3739 default_sclp = 0;
3740 default_monitor = 0;
3741 default_net = 0;
3742 default_floppy = 0;
3743 default_cdrom = 0;
3744 default_sdcard = 0;
3745 default_vga = 0;
3746 break;
3747 case QEMU_OPTION_xen_domid:
3748 if (!(xen_available())) {
3749 printf("Option %s not supported for this target\n", popt->name);
3750 exit(1);
3752 xen_domid = atoi(optarg);
3753 break;
3754 case QEMU_OPTION_xen_create:
3755 if (!(xen_available())) {
3756 printf("Option %s not supported for this target\n", popt->name);
3757 exit(1);
3759 xen_mode = XEN_CREATE;
3760 break;
3761 case QEMU_OPTION_xen_attach:
3762 if (!(xen_available())) {
3763 printf("Option %s not supported for this target\n", popt->name);
3764 exit(1);
3766 xen_mode = XEN_ATTACH;
3767 break;
3768 case QEMU_OPTION_trace:
3770 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3771 if (!opts) {
3772 exit(1);
3774 trace_events = qemu_opt_get(opts, "events");
3775 trace_file = qemu_opt_get(opts, "file");
3776 break;
3778 case QEMU_OPTION_trace_unassigned:
3779 trace_unassigned = true;
3780 break;
3781 case QEMU_OPTION_readconfig:
3783 int ret = qemu_read_config_file(optarg);
3784 if (ret < 0) {
3785 fprintf(stderr, "read config %s: %s\n", optarg,
3786 strerror(-ret));
3787 exit(1);
3789 break;
3791 case QEMU_OPTION_spice:
3792 olist = qemu_find_opts("spice");
3793 if (!olist) {
3794 fprintf(stderr, "spice is not supported by this qemu build.\n");
3795 exit(1);
3797 opts = qemu_opts_parse(olist, optarg, 0);
3798 if (!opts) {
3799 exit(1);
3801 display_remote++;
3802 break;
3803 case QEMU_OPTION_writeconfig:
3805 FILE *fp;
3806 if (strcmp(optarg, "-") == 0) {
3807 fp = stdout;
3808 } else {
3809 fp = fopen(optarg, "w");
3810 if (fp == NULL) {
3811 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3812 exit(1);
3815 qemu_config_write(fp);
3816 fclose(fp);
3817 break;
3819 case QEMU_OPTION_qtest:
3820 qtest_chrdev = optarg;
3821 break;
3822 case QEMU_OPTION_qtest_log:
3823 qtest_log = optarg;
3824 break;
3825 case QEMU_OPTION_sandbox:
3826 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3827 if (!opts) {
3828 exit(1);
3830 break;
3831 case QEMU_OPTION_add_fd:
3832 #ifndef _WIN32
3833 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3834 if (!opts) {
3835 exit(1);
3837 #else
3838 error_report("File descriptor passing is disabled on this "
3839 "platform");
3840 exit(1);
3841 #endif
3842 break;
3843 case QEMU_OPTION_object:
3844 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3845 if (!opts) {
3846 exit(1);
3848 break;
3849 case QEMU_OPTION_realtime:
3850 opts = qemu_opts_parse(qemu_find_opts("realtime"), optarg, 0);
3851 if (!opts) {
3852 exit(1);
3854 configure_realtime(opts);
3855 break;
3856 case QEMU_OPTION_msg:
3857 opts = qemu_opts_parse(qemu_find_opts("msg"), optarg, 0);
3858 if (!opts) {
3859 exit(1);
3861 configure_msg(opts);
3862 break;
3863 default:
3864 os_parse_cmd_args(popt->index, optarg);
3868 loc_set_none();
3870 if (qemu_init_main_loop()) {
3871 fprintf(stderr, "qemu_init_main_loop failed\n");
3872 exit(1);
3875 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3876 exit(1);
3879 #ifndef _WIN32
3880 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3881 exit(1);
3884 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3885 exit(1);
3887 #endif
3889 if (machine == NULL) {
3890 fprintf(stderr, "No machine found.\n");
3891 exit(1);
3894 if (machine->hw_version) {
3895 qemu_set_version(machine->hw_version);
3898 if (qemu_opts_foreach(qemu_find_opts("object"),
3899 object_create, NULL, 0) != 0) {
3900 exit(1);
3903 /* Init CPU def lists, based on config
3904 * - Must be called after all the qemu_read_config_file() calls
3905 * - Must be called before list_cpus()
3906 * - Must be called before machine->init()
3908 cpudef_init();
3910 if (cpu_model && is_help_option(cpu_model)) {
3911 list_cpus(stdout, &fprintf, cpu_model);
3912 exit(0);
3915 /* Open the logfile at this point, if necessary. We can't open the logfile
3916 * when encountering either of the logging options (-d or -D) because the
3917 * other one may be encountered later on the command line, changing the
3918 * location or level of logging.
3920 if (log_mask) {
3921 int mask;
3922 if (log_file) {
3923 qemu_set_log_filename(log_file);
3926 mask = qemu_str_to_log_mask(log_mask);
3927 if (!mask) {
3928 qemu_print_log_usage(stdout);
3929 exit(1);
3931 qemu_set_log(mask);
3934 if (!trace_backend_init(trace_events, trace_file)) {
3935 exit(1);
3938 /* If no data_dir is specified then try to find it relative to the
3939 executable path. */
3940 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3941 data_dir[data_dir_idx] = os_find_datadir(argv[0]);
3942 if (data_dir[data_dir_idx] != NULL) {
3943 data_dir_idx++;
3946 /* If all else fails use the install path specified when building. */
3947 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3948 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
3951 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
3953 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3954 if (smp_cpus > machine->max_cpus) {
3955 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3956 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3957 machine->max_cpus);
3958 exit(1);
3962 * Get the default machine options from the machine if it is not already
3963 * specified either by the configuration file or by the command line.
3965 if (machine->default_machine_opts) {
3966 qemu_opts_set_defaults(qemu_find_opts("machine"),
3967 machine->default_machine_opts, 0);
3970 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3971 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3973 if (machine->no_serial) {
3974 default_serial = 0;
3976 if (machine->no_parallel) {
3977 default_parallel = 0;
3979 if (!machine->use_virtcon) {
3980 default_virtcon = 0;
3982 if (!machine->use_sclp) {
3983 default_sclp = 0;
3985 if (machine->no_floppy) {
3986 default_floppy = 0;
3988 if (machine->no_cdrom) {
3989 default_cdrom = 0;
3991 if (machine->no_sdcard) {
3992 default_sdcard = 0;
3995 if (is_daemonized()) {
3996 /* According to documentation and historically, -nographic redirects
3997 * serial port, parallel port and monitor to stdio, which does not work
3998 * with -daemonize. We can redirect these to null instead, but since
3999 * -nographic is legacy, let's just error out.
4000 * We disallow -nographic only if all other ports are not redirected
4001 * explicitly, to not break existing legacy setups which uses
4002 * -nographic _and_ redirects all ports explicitly - this is valid
4003 * usage, -nographic is just a no-op in this case.
4005 if (display_type == DT_NOGRAPHIC
4006 && (default_parallel || default_serial
4007 || default_monitor || default_virtcon)) {
4008 fprintf(stderr, "-nographic can not be used with -daemonize\n");
4009 exit(1);
4011 #ifdef CONFIG_CURSES
4012 if (display_type == DT_CURSES) {
4013 fprintf(stderr, "curses display can not be used with -daemonize\n");
4014 exit(1);
4016 #endif
4019 if (display_type == DT_NOGRAPHIC) {
4020 if (default_parallel)
4021 add_device_config(DEV_PARALLEL, "null");
4022 if (default_serial && default_monitor) {
4023 add_device_config(DEV_SERIAL, "mon:stdio");
4024 } else if (default_virtcon && default_monitor) {
4025 add_device_config(DEV_VIRTCON, "mon:stdio");
4026 } else if (default_sclp && default_monitor) {
4027 add_device_config(DEV_SCLP, "mon:stdio");
4028 } else {
4029 if (default_serial)
4030 add_device_config(DEV_SERIAL, "stdio");
4031 if (default_virtcon)
4032 add_device_config(DEV_VIRTCON, "stdio");
4033 if (default_sclp) {
4034 add_device_config(DEV_SCLP, "stdio");
4036 if (default_monitor)
4037 monitor_parse("stdio", "readline");
4039 } else {
4040 if (default_serial)
4041 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4042 if (default_parallel)
4043 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4044 if (default_monitor)
4045 monitor_parse("vc:80Cx24C", "readline");
4046 if (default_virtcon)
4047 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4048 if (default_sclp) {
4049 add_device_config(DEV_SCLP, "vc:80Cx24C");
4053 if (display_type == DT_DEFAULT && !display_remote) {
4054 #if defined(CONFIG_GTK)
4055 display_type = DT_GTK;
4056 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4057 display_type = DT_SDL;
4058 #elif defined(CONFIG_VNC)
4059 vnc_display = "localhost:0,to=99";
4060 show_vnc_port = 1;
4061 #else
4062 display_type = DT_NONE;
4063 #endif
4066 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4067 fprintf(stderr, "-no-frame, -alt-grab and -ctrl-grab are only valid "
4068 "for SDL, ignoring option\n");
4070 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4071 fprintf(stderr, "-no-quit is only valid for GTK and SDL, "
4072 "ignoring option\n");
4075 #if defined(CONFIG_GTK)
4076 if (display_type == DT_GTK) {
4077 early_gtk_display_init();
4079 #endif
4081 socket_init();
4083 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4084 exit(1);
4085 #ifdef CONFIG_VIRTFS
4086 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4087 exit(1);
4089 #endif
4091 os_daemonize();
4093 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4094 os_pidfile_error();
4095 exit(1);
4098 /* init the memory */
4099 if (ram_size == 0) {
4100 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4103 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4104 != 0) {
4105 exit(0);
4108 configure_accelerator();
4110 if (qtest_chrdev) {
4111 qtest_init(qtest_chrdev, qtest_log);
4114 machine_opts = qemu_get_machine_opts();
4115 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4116 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4117 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4118 bios_name = qemu_opt_get(machine_opts, "firmware");
4120 boot_order = machine->default_boot_order;
4121 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4122 if (opts) {
4123 char *normal_boot_order;
4124 const char *order, *once;
4126 order = qemu_opt_get(opts, "order");
4127 if (order) {
4128 validate_bootdevices(order);
4129 boot_order = order;
4132 once = qemu_opt_get(opts, "once");
4133 if (once) {
4134 validate_bootdevices(once);
4135 normal_boot_order = g_strdup(boot_order);
4136 boot_order = once;
4137 qemu_register_reset(restore_boot_order, normal_boot_order);
4140 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4141 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4144 if (!kernel_cmdline) {
4145 kernel_cmdline = "";
4148 linux_boot = (kernel_filename != NULL);
4150 if (!linux_boot && *kernel_cmdline != '\0') {
4151 fprintf(stderr, "-append only allowed with -kernel option\n");
4152 exit(1);
4155 if (!linux_boot && initrd_filename != NULL) {
4156 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4157 exit(1);
4160 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4161 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4162 exit(1);
4165 os_set_line_buffering();
4167 qemu_init_cpu_loop();
4168 qemu_mutex_lock_iothread();
4170 #ifdef CONFIG_SPICE
4171 /* spice needs the timers to be initialized by this point */
4172 qemu_spice_init();
4173 #endif
4175 if (icount_option && (kvm_enabled() || xen_enabled())) {
4176 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4177 exit(1);
4179 configure_icount(icount_option);
4181 /* clean up network at qemu process termination */
4182 atexit(&net_cleanup);
4184 if (net_init_clients() < 0) {
4185 exit(1);
4188 #ifdef CONFIG_TPM
4189 if (tpm_init() < 0) {
4190 exit(1);
4192 #endif
4194 /* init the bluetooth world */
4195 if (foreach_device_config(DEV_BT, bt_parse))
4196 exit(1);
4198 if (!xen_enabled()) {
4199 /* On 32-bit hosts, QEMU is limited by virtual address space */
4200 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4201 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4202 exit(1);
4206 cpu_exec_init_all();
4208 blk_mig_init();
4210 /* open the virtual block devices */
4211 if (snapshot)
4212 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4213 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4214 &machine->block_default_type, 1) != 0) {
4215 exit(1);
4218 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4219 CDROM_OPTS);
4220 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4221 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4223 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
4225 if (nb_numa_nodes > 0) {
4226 int i;
4228 if (nb_numa_nodes > MAX_NODES) {
4229 nb_numa_nodes = MAX_NODES;
4232 /* If no memory size if given for any node, assume the default case
4233 * and distribute the available memory equally across all nodes
4235 for (i = 0; i < nb_numa_nodes; i++) {
4236 if (node_mem[i] != 0)
4237 break;
4239 if (i == nb_numa_nodes) {
4240 uint64_t usedmem = 0;
4242 /* On Linux, the each node's border has to be 8MB aligned,
4243 * the final node gets the rest.
4245 for (i = 0; i < nb_numa_nodes - 1; i++) {
4246 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4247 usedmem += node_mem[i];
4249 node_mem[i] = ram_size - usedmem;
4252 for (i = 0; i < nb_numa_nodes; i++) {
4253 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
4254 break;
4257 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4258 * must cope with this anyway, because there are BIOSes out there in
4259 * real machines which also use this scheme.
4261 if (i == nb_numa_nodes) {
4262 for (i = 0; i < max_cpus; i++) {
4263 set_bit(i, node_cpumask[i % nb_numa_nodes]);
4268 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4269 exit(1);
4272 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4273 exit(1);
4274 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4275 exit(1);
4276 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4277 exit(1);
4278 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4279 exit(1);
4281 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4282 exit(1);
4284 /* If no default VGA is requested, the default is "none". */
4285 if (default_vga) {
4286 if (cirrus_vga_available()) {
4287 vga_model = "cirrus";
4288 } else if (vga_available()) {
4289 vga_model = "std";
4292 select_vgahw(vga_model);
4294 if (watchdog) {
4295 i = select_watchdog(watchdog);
4296 if (i > 0)
4297 exit (i == 1 ? 1 : 0);
4300 if (machine->compat_props) {
4301 qdev_prop_register_global_list(machine->compat_props);
4303 qemu_add_globals();
4305 qdev_machine_init();
4307 QEMUMachineInitArgs args = { .machine = machine,
4308 .ram_size = ram_size,
4309 .boot_order = boot_order,
4310 .kernel_filename = kernel_filename,
4311 .kernel_cmdline = kernel_cmdline,
4312 .initrd_filename = initrd_filename,
4313 .cpu_model = cpu_model };
4314 machine->init(&args);
4316 audio_init();
4318 cpu_synchronize_all_post_init();
4320 set_numa_modes();
4322 current_machine = machine;
4324 /* init USB devices */
4325 if (usb_enabled(false)) {
4326 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4327 exit(1);
4330 /* init generic devices */
4331 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4332 exit(1);
4334 net_check_clients();
4336 ds = init_displaystate();
4338 /* init local displays */
4339 switch (display_type) {
4340 case DT_NOGRAPHIC:
4341 (void)ds; /* avoid warning if no display is configured */
4342 break;
4343 #if defined(CONFIG_CURSES)
4344 case DT_CURSES:
4345 curses_display_init(ds, full_screen);
4346 break;
4347 #endif
4348 #if defined(CONFIG_SDL)
4349 case DT_SDL:
4350 sdl_display_init(ds, full_screen, no_frame);
4351 break;
4352 #elif defined(CONFIG_COCOA)
4353 case DT_SDL:
4354 cocoa_display_init(ds, full_screen);
4355 break;
4356 #endif
4357 #if defined(CONFIG_GTK)
4358 case DT_GTK:
4359 gtk_display_init(ds, full_screen);
4360 break;
4361 #endif
4362 default:
4363 break;
4366 /* must be after terminal init, SDL library changes signal handlers */
4367 os_setup_signal_handling();
4369 #ifdef CONFIG_VNC
4370 /* init remote displays */
4371 if (vnc_display) {
4372 Error *local_err = NULL;
4373 vnc_display_init(ds);
4374 vnc_display_open(ds, vnc_display, &local_err);
4375 if (local_err != NULL) {
4376 error_report("Failed to start VNC server on `%s': %s",
4377 vnc_display, error_get_pretty(local_err));
4378 error_free(local_err);
4379 exit(1);
4382 if (show_vnc_port) {
4383 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4386 #endif
4387 #ifdef CONFIG_SPICE
4388 if (using_spice) {
4389 qemu_spice_display_init();
4391 #endif
4393 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4394 exit(1);
4397 qdev_machine_creation_done();
4399 if (rom_load_all() != 0) {
4400 fprintf(stderr, "rom loading failed\n");
4401 exit(1);
4404 /* TODO: once all bus devices are qdevified, this should be done
4405 * when bus is created by qdev.c */
4406 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4407 qemu_run_machine_init_done_notifiers();
4409 /* Done notifiers can load ROMs */
4410 rom_load_done();
4412 qemu_system_reset(VMRESET_SILENT);
4413 if (loadvm) {
4414 if (load_vmstate(loadvm) < 0) {
4415 autostart = 0;
4419 if (incoming) {
4420 Error *local_err = NULL;
4421 qemu_start_incoming_migration(incoming, &local_err);
4422 if (local_err) {
4423 error_report("-incoming %s: %s", incoming,
4424 error_get_pretty(local_err));
4425 error_free(local_err);
4426 exit(1);
4428 } else if (autostart) {
4429 vm_start();
4432 os_setup_post();
4434 main_loop();
4435 bdrv_close_all();
4436 pause_all_vcpus();
4437 res_free();
4438 #ifdef CONFIG_TPM
4439 tpm_cleanup();
4440 #endif
4442 return 0;