Add support for cancelling of a TPM command
[qemu/agraf.git] / vl.c
blob3822d99425f430ef8f7886e21d8814ebc007e309
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 #ifdef _WIN32
93 #include <windows.h>
94 #endif
96 #ifdef CONFIG_SDL
97 #if defined(__APPLE__) || defined(main)
98 #include <SDL.h>
99 int qemu_main(int argc, char **argv, char **envp);
100 int main(int argc, char **argv)
102 return qemu_main(argc, argv, NULL);
104 #undef main
105 #define main qemu_main
106 #endif
107 #endif /* CONFIG_SDL */
109 #ifdef CONFIG_COCOA
110 #undef main
111 #define main qemu_main
112 #endif /* CONFIG_COCOA */
114 #include <glib.h>
116 #include "hw/hw.h"
117 #include "hw/boards.h"
118 #include "hw/usb.h"
119 #include "hw/pcmcia.h"
120 #include "hw/pc.h"
121 #include "hw/isa.h"
122 #include "hw/bt.h"
123 #include "hw/watchdog.h"
124 #include "hw/smbios.h"
125 #include "hw/xen.h"
126 #include "hw/qdev.h"
127 #include "hw/loader.h"
128 #include "monitor/qdev.h"
129 #include "bt/bt.h"
130 #include "net/net.h"
131 #include "net/slirp.h"
132 #include "monitor/monitor.h"
133 #include "ui/console.h"
134 #include "sysemu/sysemu.h"
135 #include "exec/gdbstub.h"
136 #include "qemu/timer.h"
137 #include "char/char.h"
138 #include "qemu/cache-utils.h"
139 #include "sysemu/blockdev.h"
140 #include "hw/block-common.h"
141 #include "migration/block.h"
142 #include "tpm/tpm.h"
143 #include "sysemu/dma.h"
144 #include "audio/audio.h"
145 #include "migration/migration.h"
146 #include "sysemu/kvm.h"
147 #include "qapi/qmp/qjson.h"
148 #include "qemu/option.h"
149 #include "qemu/config-file.h"
150 #include "qemu-options.h"
151 #include "qmp-commands.h"
152 #include "qemu/main-loop.h"
153 #ifdef CONFIG_VIRTFS
154 #include "fsdev/qemu-fsdev.h"
155 #endif
156 #include "sysemu/qtest.h"
158 #include "disas/disas.h"
160 #include "qemu/sockets.h"
162 #include "slirp/libslirp.h"
164 #include "trace.h"
165 #include "trace/control.h"
166 #include "qemu/queue.h"
167 #include "sysemu/cpus.h"
168 #include "sysemu/arch_init.h"
169 #include "qemu/osdep.h"
171 #include "ui/qemu-spice.h"
172 #include "qapi/string-input-visitor.h"
174 //#define DEBUG_NET
175 //#define DEBUG_SLIRP
177 #define DEFAULT_RAM_SIZE 128
179 #define MAX_VIRTIO_CONSOLES 1
180 #define MAX_SCLP_CONSOLES 1
182 static const char *data_dir;
183 const char *bios_name = NULL;
184 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
185 DisplayType display_type = DT_DEFAULT;
186 static int display_remote;
187 const char* keyboard_layout = NULL;
188 ram_addr_t ram_size;
189 const char *mem_path = NULL;
190 #ifdef MAP_POPULATE
191 int mem_prealloc = 0; /* force preallocation of physical target memory */
192 #endif
193 int nb_nics;
194 NICInfo nd_table[MAX_NICS];
195 int autostart;
196 static int rtc_utc = 1;
197 static int rtc_date_offset = -1; /* -1 means no change */
198 QEMUClock *rtc_clock;
199 int vga_interface_type = VGA_NONE;
200 static int full_screen = 0;
201 #ifdef CONFIG_SDL
202 static int no_frame = 0;
203 #endif
204 int no_quit = 0;
205 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
206 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
207 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
208 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
209 int win2k_install_hack = 0;
210 int singlestep = 0;
211 int smp_cpus = 1;
212 int max_cpus = 0;
213 int smp_cores = 1;
214 int smp_threads = 1;
215 #ifdef CONFIG_VNC
216 const char *vnc_display;
217 #endif
218 int acpi_enabled = 1;
219 int no_hpet = 0;
220 int fd_bootchk = 1;
221 static int no_reboot;
222 int no_shutdown = 0;
223 int cursor_hide = 1;
224 int graphic_rotate = 0;
225 const char *watchdog;
226 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
227 int nb_option_roms;
228 int semihosting_enabled = 0;
229 int old_param = 0;
230 const char *qemu_name;
231 int alt_grab = 0;
232 int ctrl_grab = 0;
233 unsigned int nb_prom_envs = 0;
234 const char *prom_envs[MAX_PROM_ENVS];
235 int boot_menu;
236 uint8_t *boot_splash_filedata;
237 size_t boot_splash_filedata_size;
238 uint8_t qemu_extra_params_fw[2];
240 typedef struct FWBootEntry FWBootEntry;
242 struct FWBootEntry {
243 QTAILQ_ENTRY(FWBootEntry) link;
244 int32_t bootindex;
245 DeviceState *dev;
246 char *suffix;
249 static QTAILQ_HEAD(, FWBootEntry) fw_boot_order =
250 QTAILQ_HEAD_INITIALIZER(fw_boot_order);
252 int nb_numa_nodes;
253 uint64_t node_mem[MAX_NODES];
254 unsigned long *node_cpumask[MAX_NODES];
256 uint8_t qemu_uuid[16];
258 static QEMUBootSetHandler *boot_set_handler;
259 static void *boot_set_opaque;
261 static NotifierList exit_notifiers =
262 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
264 static NotifierList machine_init_done_notifiers =
265 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
267 static bool tcg_allowed = true;
268 bool kvm_allowed;
269 bool xen_allowed;
270 uint32_t xen_domid;
271 enum xen_mode xen_mode = XEN_EMULATE;
272 static int tcg_tb_size;
274 static int default_serial = 1;
275 static int default_parallel = 1;
276 static int default_virtcon = 1;
277 static int default_sclp = 1;
278 static int default_monitor = 1;
279 static int default_floppy = 1;
280 static int default_cdrom = 1;
281 static int default_sdcard = 1;
282 static int default_vga = 1;
284 static struct {
285 const char *driver;
286 int *flag;
287 } default_list[] = {
288 { .driver = "isa-serial", .flag = &default_serial },
289 { .driver = "isa-parallel", .flag = &default_parallel },
290 { .driver = "isa-fdc", .flag = &default_floppy },
291 { .driver = "ide-cd", .flag = &default_cdrom },
292 { .driver = "ide-hd", .flag = &default_cdrom },
293 { .driver = "ide-drive", .flag = &default_cdrom },
294 { .driver = "scsi-cd", .flag = &default_cdrom },
295 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
296 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
297 { .driver = "virtio-serial", .flag = &default_virtcon },
298 { .driver = "VGA", .flag = &default_vga },
299 { .driver = "isa-vga", .flag = &default_vga },
300 { .driver = "cirrus-vga", .flag = &default_vga },
301 { .driver = "isa-cirrus-vga", .flag = &default_vga },
302 { .driver = "vmware-svga", .flag = &default_vga },
303 { .driver = "qxl-vga", .flag = &default_vga },
306 static QemuOptsList qemu_rtc_opts = {
307 .name = "rtc",
308 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
309 .desc = {
311 .name = "base",
312 .type = QEMU_OPT_STRING,
314 .name = "clock",
315 .type = QEMU_OPT_STRING,
317 .name = "driftfix",
318 .type = QEMU_OPT_STRING,
320 { /* end of list */ }
324 static QemuOptsList qemu_sandbox_opts = {
325 .name = "sandbox",
326 .implied_opt_name = "enable",
327 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
328 .desc = {
330 .name = "enable",
331 .type = QEMU_OPT_BOOL,
333 { /* end of list */ }
337 static QemuOptsList qemu_trace_opts = {
338 .name = "trace",
339 .implied_opt_name = "trace",
340 .head = QTAILQ_HEAD_INITIALIZER(qemu_trace_opts.head),
341 .desc = {
343 .name = "events",
344 .type = QEMU_OPT_STRING,
346 .name = "file",
347 .type = QEMU_OPT_STRING,
349 { /* end of list */ }
353 static QemuOptsList qemu_option_rom_opts = {
354 .name = "option-rom",
355 .implied_opt_name = "romfile",
356 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
357 .desc = {
359 .name = "bootindex",
360 .type = QEMU_OPT_NUMBER,
361 }, {
362 .name = "romfile",
363 .type = QEMU_OPT_STRING,
365 { /* end of list */ }
369 static QemuOptsList qemu_machine_opts = {
370 .name = "machine",
371 .implied_opt_name = "type",
372 .merge_lists = true,
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
374 .desc = {
376 .name = "type",
377 .type = QEMU_OPT_STRING,
378 .help = "emulated machine"
379 }, {
380 .name = "accel",
381 .type = QEMU_OPT_STRING,
382 .help = "accelerator list",
383 }, {
384 .name = "kernel_irqchip",
385 .type = QEMU_OPT_BOOL,
386 .help = "use KVM in-kernel irqchip",
387 }, {
388 .name = "kvm_shadow_mem",
389 .type = QEMU_OPT_SIZE,
390 .help = "KVM shadow MMU size",
391 }, {
392 .name = "kernel",
393 .type = QEMU_OPT_STRING,
394 .help = "Linux kernel image file",
395 }, {
396 .name = "initrd",
397 .type = QEMU_OPT_STRING,
398 .help = "Linux initial ramdisk file",
399 }, {
400 .name = "append",
401 .type = QEMU_OPT_STRING,
402 .help = "Linux kernel command line",
403 }, {
404 .name = "dtb",
405 .type = QEMU_OPT_STRING,
406 .help = "Linux kernel device tree file",
407 }, {
408 .name = "dumpdtb",
409 .type = QEMU_OPT_STRING,
410 .help = "Dump current dtb to a file and quit",
411 }, {
412 .name = "phandle_start",
413 .type = QEMU_OPT_STRING,
414 .help = "The first phandle ID we may generate dynamically",
415 }, {
416 .name = "dt_compatible",
417 .type = QEMU_OPT_STRING,
418 .help = "Overrides the \"compatible\" property of the dt root node",
419 }, {
420 .name = "dump-guest-core",
421 .type = QEMU_OPT_BOOL,
422 .help = "Include guest memory in a core dump",
423 }, {
424 .name = "mem-merge",
425 .type = QEMU_OPT_BOOL,
426 .help = "enable/disable memory merge support",
428 .name = "usb",
429 .type = QEMU_OPT_BOOL,
430 .help = "Set on/off to enable/disable usb",
432 { /* End of list */ }
436 static QemuOptsList qemu_boot_opts = {
437 .name = "boot-opts",
438 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
439 .desc = {
440 /* the three names below are not used now */
442 .name = "order",
443 .type = QEMU_OPT_STRING,
444 }, {
445 .name = "once",
446 .type = QEMU_OPT_STRING,
447 }, {
448 .name = "menu",
449 .type = QEMU_OPT_STRING,
450 /* following are really used */
451 }, {
452 .name = "splash",
453 .type = QEMU_OPT_STRING,
454 }, {
455 .name = "splash-time",
456 .type = QEMU_OPT_STRING,
457 }, {
458 .name = "reboot-timeout",
459 .type = QEMU_OPT_STRING,
461 { /*End of list */ }
465 static QemuOptsList qemu_add_fd_opts = {
466 .name = "add-fd",
467 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
468 .desc = {
470 .name = "fd",
471 .type = QEMU_OPT_NUMBER,
472 .help = "file descriptor of which a duplicate is added to fd set",
474 .name = "set",
475 .type = QEMU_OPT_NUMBER,
476 .help = "ID of the fd set to add fd to",
478 .name = "opaque",
479 .type = QEMU_OPT_STRING,
480 .help = "free-form string used to describe fd",
482 { /* end of list */ }
486 static QemuOptsList qemu_object_opts = {
487 .name = "object",
488 .implied_opt_name = "qom-type",
489 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
490 .desc = {
495 static QemuOptsList qemu_tpmdev_opts = {
496 .name = "tpmdev",
497 .implied_opt_name = "type",
498 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
499 .desc = {
501 .name = "type",
502 .type = QEMU_OPT_STRING,
503 .help = "Type of TPM backend",
506 .name = "cancel-path",
507 .type = QEMU_OPT_STRING,
508 .help = "Sysfs file entry for canceling TPM commands",
511 .name = "path",
512 .type = QEMU_OPT_STRING,
513 .help = "Path to TPM device on the host",
515 { /* end of list */ }
519 const char *qemu_get_vm_name(void)
521 return qemu_name;
524 static void res_free(void)
526 if (boot_splash_filedata != NULL) {
527 g_free(boot_splash_filedata);
528 boot_splash_filedata = NULL;
532 static int default_driver_check(QemuOpts *opts, void *opaque)
534 const char *driver = qemu_opt_get(opts, "driver");
535 int i;
537 if (!driver)
538 return 0;
539 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
540 if (strcmp(default_list[i].driver, driver) != 0)
541 continue;
542 *(default_list[i].flag) = 0;
544 return 0;
547 /***********************************************************/
548 /* QEMU state */
550 static RunState current_run_state = RUN_STATE_PRELAUNCH;
552 typedef struct {
553 RunState from;
554 RunState to;
555 } RunStateTransition;
557 static const RunStateTransition runstate_transitions_def[] = {
558 /* from -> to */
559 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
561 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
562 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
564 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
565 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
567 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
568 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
570 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
571 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
573 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
574 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
576 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
577 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
578 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
580 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
581 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
583 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
585 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
586 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
587 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
588 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
589 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
591 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
592 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
593 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
595 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
597 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
598 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
600 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
601 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
602 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
603 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
605 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
606 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
608 { RUN_STATE_MAX, RUN_STATE_MAX },
611 static bool runstate_valid_transitions[RUN_STATE_MAX][RUN_STATE_MAX];
613 bool runstate_check(RunState state)
615 return current_run_state == state;
618 static void runstate_init(void)
620 const RunStateTransition *p;
622 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
624 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE_MAX; p++) {
625 runstate_valid_transitions[p->from][p->to] = true;
629 /* This function will abort() on invalid state transitions */
630 void runstate_set(RunState new_state)
632 assert(new_state < RUN_STATE_MAX);
634 if (!runstate_valid_transitions[current_run_state][new_state]) {
635 fprintf(stderr, "ERROR: invalid runstate transition: '%s' -> '%s'\n",
636 RunState_lookup[current_run_state],
637 RunState_lookup[new_state]);
638 abort();
641 current_run_state = new_state;
644 int runstate_is_running(void)
646 return runstate_check(RUN_STATE_RUNNING);
649 StatusInfo *qmp_query_status(Error **errp)
651 StatusInfo *info = g_malloc0(sizeof(*info));
653 info->running = runstate_is_running();
654 info->singlestep = singlestep;
655 info->status = current_run_state;
657 return info;
660 /***********************************************************/
661 /* real time host monotonic timer */
663 /***********************************************************/
664 /* host time/date access */
665 void qemu_get_timedate(struct tm *tm, int offset)
667 time_t ti;
669 time(&ti);
670 ti += offset;
671 if (rtc_date_offset == -1) {
672 if (rtc_utc)
673 gmtime_r(&ti, tm);
674 else
675 localtime_r(&ti, tm);
676 } else {
677 ti -= rtc_date_offset;
678 gmtime_r(&ti, tm);
682 int qemu_timedate_diff(struct tm *tm)
684 time_t seconds;
686 if (rtc_date_offset == -1)
687 if (rtc_utc)
688 seconds = mktimegm(tm);
689 else {
690 struct tm tmp = *tm;
691 tmp.tm_isdst = -1; /* use timezone to figure it out */
692 seconds = mktime(&tmp);
694 else
695 seconds = mktimegm(tm) + rtc_date_offset;
697 return seconds - time(NULL);
700 void rtc_change_mon_event(struct tm *tm)
702 QObject *data;
704 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
705 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
706 qobject_decref(data);
709 static void configure_rtc_date_offset(const char *startdate, int legacy)
711 time_t rtc_start_date;
712 struct tm tm;
714 if (!strcmp(startdate, "now") && legacy) {
715 rtc_date_offset = -1;
716 } else {
717 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
718 &tm.tm_year,
719 &tm.tm_mon,
720 &tm.tm_mday,
721 &tm.tm_hour,
722 &tm.tm_min,
723 &tm.tm_sec) == 6) {
724 /* OK */
725 } else if (sscanf(startdate, "%d-%d-%d",
726 &tm.tm_year,
727 &tm.tm_mon,
728 &tm.tm_mday) == 3) {
729 tm.tm_hour = 0;
730 tm.tm_min = 0;
731 tm.tm_sec = 0;
732 } else {
733 goto date_fail;
735 tm.tm_year -= 1900;
736 tm.tm_mon--;
737 rtc_start_date = mktimegm(&tm);
738 if (rtc_start_date == -1) {
739 date_fail:
740 fprintf(stderr, "Invalid date format. Valid formats are:\n"
741 "'2006-06-17T16:01:21' or '2006-06-17'\n");
742 exit(1);
744 rtc_date_offset = time(NULL) - rtc_start_date;
748 static void configure_rtc(QemuOpts *opts)
750 const char *value;
752 value = qemu_opt_get(opts, "base");
753 if (value) {
754 if (!strcmp(value, "utc")) {
755 rtc_utc = 1;
756 } else if (!strcmp(value, "localtime")) {
757 rtc_utc = 0;
758 } else {
759 configure_rtc_date_offset(value, 0);
762 value = qemu_opt_get(opts, "clock");
763 if (value) {
764 if (!strcmp(value, "host")) {
765 rtc_clock = host_clock;
766 } else if (!strcmp(value, "rt")) {
767 rtc_clock = rt_clock;
768 } else if (!strcmp(value, "vm")) {
769 rtc_clock = vm_clock;
770 } else {
771 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
772 exit(1);
775 value = qemu_opt_get(opts, "driftfix");
776 if (value) {
777 if (!strcmp(value, "slew")) {
778 static GlobalProperty slew_lost_ticks[] = {
780 .driver = "mc146818rtc",
781 .property = "lost_tick_policy",
782 .value = "slew",
784 { /* end of list */ }
787 qdev_prop_register_global_list(slew_lost_ticks);
788 } else if (!strcmp(value, "none")) {
789 /* discard is default */
790 } else {
791 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
792 exit(1);
797 /***********************************************************/
798 /* Bluetooth support */
799 static int nb_hcis;
800 static int cur_hci;
801 static struct HCIInfo *hci_table[MAX_NICS];
803 static struct bt_vlan_s {
804 struct bt_scatternet_s net;
805 int id;
806 struct bt_vlan_s *next;
807 } *first_bt_vlan;
809 /* find or alloc a new bluetooth "VLAN" */
810 static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
812 struct bt_vlan_s **pvlan, *vlan;
813 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
814 if (vlan->id == id)
815 return &vlan->net;
817 vlan = g_malloc0(sizeof(struct bt_vlan_s));
818 vlan->id = id;
819 pvlan = &first_bt_vlan;
820 while (*pvlan != NULL)
821 pvlan = &(*pvlan)->next;
822 *pvlan = vlan;
823 return &vlan->net;
826 static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
830 static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
832 return -ENOTSUP;
835 static struct HCIInfo null_hci = {
836 .cmd_send = null_hci_send,
837 .sco_send = null_hci_send,
838 .acl_send = null_hci_send,
839 .bdaddr_set = null_hci_addr_set,
842 struct HCIInfo *qemu_next_hci(void)
844 if (cur_hci == nb_hcis)
845 return &null_hci;
847 return hci_table[cur_hci++];
850 static struct HCIInfo *hci_init(const char *str)
852 char *endp;
853 struct bt_scatternet_s *vlan = 0;
855 if (!strcmp(str, "null"))
856 /* null */
857 return &null_hci;
858 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
859 /* host[:hciN] */
860 return bt_host_hci(str[4] ? str + 5 : "hci0");
861 else if (!strncmp(str, "hci", 3)) {
862 /* hci[,vlan=n] */
863 if (str[3]) {
864 if (!strncmp(str + 3, ",vlan=", 6)) {
865 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
866 if (*endp)
867 vlan = 0;
869 } else
870 vlan = qemu_find_bt_vlan(0);
871 if (vlan)
872 return bt_new_hci(vlan);
875 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
877 return 0;
880 static int bt_hci_parse(const char *str)
882 struct HCIInfo *hci;
883 bdaddr_t bdaddr;
885 if (nb_hcis >= MAX_NICS) {
886 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
887 return -1;
890 hci = hci_init(str);
891 if (!hci)
892 return -1;
894 bdaddr.b[0] = 0x52;
895 bdaddr.b[1] = 0x54;
896 bdaddr.b[2] = 0x00;
897 bdaddr.b[3] = 0x12;
898 bdaddr.b[4] = 0x34;
899 bdaddr.b[5] = 0x56 + nb_hcis;
900 hci->bdaddr_set(hci, bdaddr.b);
902 hci_table[nb_hcis++] = hci;
904 return 0;
907 static void bt_vhci_add(int vlan_id)
909 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
911 if (!vlan->slave)
912 fprintf(stderr, "qemu: warning: adding a VHCI to "
913 "an empty scatternet %i\n", vlan_id);
915 bt_vhci_init(bt_new_hci(vlan));
918 static struct bt_device_s *bt_device_add(const char *opt)
920 struct bt_scatternet_s *vlan;
921 int vlan_id = 0;
922 char *endp = strstr(opt, ",vlan=");
923 int len = (endp ? endp - opt : strlen(opt)) + 1;
924 char devname[10];
926 pstrcpy(devname, MIN(sizeof(devname), len), opt);
928 if (endp) {
929 vlan_id = strtol(endp + 6, &endp, 0);
930 if (*endp) {
931 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
932 return 0;
936 vlan = qemu_find_bt_vlan(vlan_id);
938 if (!vlan->slave)
939 fprintf(stderr, "qemu: warning: adding a slave device to "
940 "an empty scatternet %i\n", vlan_id);
942 if (!strcmp(devname, "keyboard"))
943 return bt_keyboard_init(vlan);
945 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
946 return 0;
949 static int bt_parse(const char *opt)
951 const char *endp, *p;
952 int vlan;
954 if (strstart(opt, "hci", &endp)) {
955 if (!*endp || *endp == ',') {
956 if (*endp)
957 if (!strstart(endp, ",vlan=", 0))
958 opt = endp + 1;
960 return bt_hci_parse(opt);
962 } else if (strstart(opt, "vhci", &endp)) {
963 if (!*endp || *endp == ',') {
964 if (*endp) {
965 if (strstart(endp, ",vlan=", &p)) {
966 vlan = strtol(p, (char **) &endp, 0);
967 if (*endp) {
968 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
969 return 1;
971 } else {
972 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
973 return 1;
975 } else
976 vlan = 0;
978 bt_vhci_add(vlan);
979 return 0;
981 } else if (strstart(opt, "device:", &endp))
982 return !bt_device_add(endp);
984 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
985 return 1;
988 static int parse_sandbox(QemuOpts *opts, void *opaque)
990 /* FIXME: change this to true for 1.3 */
991 if (qemu_opt_get_bool(opts, "enable", false)) {
992 #ifdef CONFIG_SECCOMP
993 if (seccomp_start() < 0) {
994 qerror_report(ERROR_CLASS_GENERIC_ERROR,
995 "failed to install seccomp syscall filter in the kernel");
996 return -1;
998 #else
999 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1000 "sandboxing request but seccomp is not compiled into this build");
1001 return -1;
1002 #endif
1005 return 0;
1008 /*********QEMU USB setting******/
1009 bool usb_enabled(bool default_usb)
1011 QemuOpts *mach_opts;
1012 mach_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
1013 if (mach_opts) {
1014 return qemu_opt_get_bool(mach_opts, "usb", default_usb);
1016 return default_usb;
1019 #ifndef _WIN32
1020 static int parse_add_fd(QemuOpts *opts, void *opaque)
1022 int fd, dupfd, flags;
1023 int64_t fdset_id;
1024 const char *fd_opaque = NULL;
1026 fd = qemu_opt_get_number(opts, "fd", -1);
1027 fdset_id = qemu_opt_get_number(opts, "set", -1);
1028 fd_opaque = qemu_opt_get(opts, "opaque");
1030 if (fd < 0) {
1031 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1032 "fd option is required and must be non-negative");
1033 return -1;
1036 if (fd <= STDERR_FILENO) {
1037 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1038 "fd cannot be a standard I/O stream");
1039 return -1;
1043 * All fds inherited across exec() necessarily have FD_CLOEXEC
1044 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1046 flags = fcntl(fd, F_GETFD);
1047 if (flags == -1 || (flags & FD_CLOEXEC)) {
1048 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1049 "fd is not valid or already in use");
1050 return -1;
1053 if (fdset_id < 0) {
1054 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1055 "set option is required and must be non-negative");
1056 return -1;
1059 #ifdef F_DUPFD_CLOEXEC
1060 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1061 #else
1062 dupfd = dup(fd);
1063 if (dupfd != -1) {
1064 qemu_set_cloexec(dupfd);
1066 #endif
1067 if (dupfd == -1) {
1068 qerror_report(ERROR_CLASS_GENERIC_ERROR,
1069 "Error duplicating fd: %s", strerror(errno));
1070 return -1;
1073 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1074 monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque ? true : false,
1075 fd_opaque, NULL);
1077 return 0;
1080 static int cleanup_add_fd(QemuOpts *opts, void *opaque)
1082 int fd;
1084 fd = qemu_opt_get_number(opts, "fd", -1);
1085 close(fd);
1087 return 0;
1089 #endif
1091 /***********************************************************/
1092 /* QEMU Block devices */
1094 #define HD_OPTS "media=disk"
1095 #define CDROM_OPTS "media=cdrom"
1096 #define FD_OPTS ""
1097 #define PFLASH_OPTS ""
1098 #define MTD_OPTS ""
1099 #define SD_OPTS ""
1101 static int drive_init_func(QemuOpts *opts, void *opaque)
1103 BlockInterfaceType *block_default_type = opaque;
1105 return drive_init(opts, *block_default_type) == NULL;
1108 static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1110 if (NULL == qemu_opt_get(opts, "snapshot")) {
1111 qemu_opt_set(opts, "snapshot", "on");
1113 return 0;
1116 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1117 int index, const char *optstr)
1119 QemuOpts *opts;
1121 if (!enable || drive_get_by_index(type, index)) {
1122 return;
1125 opts = drive_add(type, index, NULL, optstr);
1126 if (snapshot) {
1127 drive_enable_snapshot(opts, NULL);
1129 if (!drive_init(opts, type)) {
1130 exit(1);
1134 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1136 boot_set_handler = func;
1137 boot_set_opaque = opaque;
1140 int qemu_boot_set(const char *boot_devices)
1142 if (!boot_set_handler) {
1143 return -EINVAL;
1145 return boot_set_handler(boot_set_opaque, boot_devices);
1148 static void validate_bootdevices(char *devices)
1150 /* We just do some generic consistency checks */
1151 const char *p;
1152 int bitmap = 0;
1154 for (p = devices; *p != '\0'; p++) {
1155 /* Allowed boot devices are:
1156 * a-b: floppy disk drives
1157 * c-f: IDE disk drives
1158 * g-m: machine implementation dependent drives
1159 * n-p: network devices
1160 * It's up to each machine implementation to check if the given boot
1161 * devices match the actual hardware implementation and firmware
1162 * features.
1164 if (*p < 'a' || *p > 'p') {
1165 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1166 exit(1);
1168 if (bitmap & (1 << (*p - 'a'))) {
1169 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1170 exit(1);
1172 bitmap |= 1 << (*p - 'a');
1176 static void restore_boot_devices(void *opaque)
1178 char *standard_boot_devices = opaque;
1179 static int first = 1;
1181 /* Restore boot order and remove ourselves after the first boot */
1182 if (first) {
1183 first = 0;
1184 return;
1187 qemu_boot_set(standard_boot_devices);
1189 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1190 g_free(standard_boot_devices);
1193 void add_boot_device_path(int32_t bootindex, DeviceState *dev,
1194 const char *suffix)
1196 FWBootEntry *node, *i;
1198 if (bootindex < 0) {
1199 return;
1202 assert(dev != NULL || suffix != NULL);
1204 node = g_malloc0(sizeof(FWBootEntry));
1205 node->bootindex = bootindex;
1206 node->suffix = suffix ? g_strdup(suffix) : NULL;
1207 node->dev = dev;
1209 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1210 if (i->bootindex == bootindex) {
1211 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
1212 exit(1);
1213 } else if (i->bootindex < bootindex) {
1214 continue;
1216 QTAILQ_INSERT_BEFORE(i, node, link);
1217 return;
1219 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
1223 * This function returns null terminated string that consist of new line
1224 * separated device paths.
1226 * memory pointed by "size" is assigned total length of the array in bytes
1229 char *get_boot_devices_list(size_t *size)
1231 FWBootEntry *i;
1232 size_t total = 0;
1233 char *list = NULL;
1235 QTAILQ_FOREACH(i, &fw_boot_order, link) {
1236 char *devpath = NULL, *bootpath;
1237 size_t len;
1239 if (i->dev) {
1240 devpath = qdev_get_fw_dev_path(i->dev);
1241 assert(devpath);
1244 if (i->suffix && devpath) {
1245 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
1247 bootpath = g_malloc(bootpathlen);
1248 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
1249 g_free(devpath);
1250 } else if (devpath) {
1251 bootpath = devpath;
1252 } else {
1253 assert(i->suffix);
1254 bootpath = g_strdup(i->suffix);
1257 if (total) {
1258 list[total-1] = '\n';
1260 len = strlen(bootpath) + 1;
1261 list = g_realloc(list, total + len);
1262 memcpy(&list[total], bootpath, len);
1263 total += len;
1264 g_free(bootpath);
1267 *size = total;
1269 return list;
1272 static void numa_node_parse_cpus(int nodenr, const char *cpus)
1274 char *endptr;
1275 unsigned long long value, endvalue;
1277 /* Empty CPU range strings will be considered valid, they will simply
1278 * not set any bit in the CPU bitmap.
1280 if (!*cpus) {
1281 return;
1284 if (parse_uint(cpus, &value, &endptr, 10) < 0) {
1285 goto error;
1287 if (*endptr == '-') {
1288 if (parse_uint_full(endptr + 1, &endvalue, 10) < 0) {
1289 goto error;
1291 } else if (*endptr == '\0') {
1292 endvalue = value;
1293 } else {
1294 goto error;
1297 if (endvalue >= MAX_CPUMASK_BITS) {
1298 endvalue = MAX_CPUMASK_BITS - 1;
1299 fprintf(stderr,
1300 "qemu: NUMA: A max of %d VCPUs are supported\n",
1301 MAX_CPUMASK_BITS);
1304 if (endvalue < value) {
1305 goto error;
1308 bitmap_set(node_cpumask[nodenr], value, endvalue-value+1);
1309 return;
1311 error:
1312 fprintf(stderr, "qemu: Invalid NUMA CPU range: %s\n", cpus);
1313 exit(1);
1316 static void numa_add(const char *optarg)
1318 char option[128];
1319 char *endptr;
1320 unsigned long long nodenr;
1322 optarg = get_opt_name(option, 128, optarg, ',');
1323 if (*optarg == ',') {
1324 optarg++;
1326 if (!strcmp(option, "node")) {
1328 if (nb_numa_nodes >= MAX_NODES) {
1329 fprintf(stderr, "qemu: too many NUMA nodes\n");
1330 exit(1);
1333 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1334 nodenr = nb_numa_nodes;
1335 } else {
1336 if (parse_uint_full(option, &nodenr, 10) < 0) {
1337 fprintf(stderr, "qemu: Invalid NUMA nodeid: %s\n", option);
1338 exit(1);
1342 if (nodenr >= MAX_NODES) {
1343 fprintf(stderr, "qemu: invalid NUMA nodeid: %llu\n", nodenr);
1344 exit(1);
1347 if (get_param_value(option, 128, "mem", optarg) == 0) {
1348 node_mem[nodenr] = 0;
1349 } else {
1350 int64_t sval;
1351 sval = strtosz(option, &endptr);
1352 if (sval < 0 || *endptr) {
1353 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
1354 exit(1);
1356 node_mem[nodenr] = sval;
1358 if (get_param_value(option, 128, "cpus", optarg) != 0) {
1359 numa_node_parse_cpus(nodenr, option);
1361 nb_numa_nodes++;
1362 } else {
1363 fprintf(stderr, "Invalid -numa option: %s\n", option);
1364 exit(1);
1368 static void smp_parse(const char *optarg)
1370 int smp, sockets = 0, threads = 0, cores = 0;
1371 char *endptr;
1372 char option[128];
1374 smp = strtoul(optarg, &endptr, 10);
1375 if (endptr != optarg) {
1376 if (*endptr == ',') {
1377 endptr++;
1380 if (get_param_value(option, 128, "sockets", endptr) != 0)
1381 sockets = strtoull(option, NULL, 10);
1382 if (get_param_value(option, 128, "cores", endptr) != 0)
1383 cores = strtoull(option, NULL, 10);
1384 if (get_param_value(option, 128, "threads", endptr) != 0)
1385 threads = strtoull(option, NULL, 10);
1386 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1387 max_cpus = strtoull(option, NULL, 10);
1389 /* compute missing values, prefer sockets over cores over threads */
1390 if (smp == 0 || sockets == 0) {
1391 sockets = sockets > 0 ? sockets : 1;
1392 cores = cores > 0 ? cores : 1;
1393 threads = threads > 0 ? threads : 1;
1394 if (smp == 0) {
1395 smp = cores * threads * sockets;
1397 } else {
1398 if (cores == 0) {
1399 threads = threads > 0 ? threads : 1;
1400 cores = smp / (sockets * threads);
1401 } else {
1402 threads = smp / (cores * sockets);
1405 smp_cpus = smp;
1406 smp_cores = cores > 0 ? cores : 1;
1407 smp_threads = threads > 0 ? threads : 1;
1408 if (max_cpus == 0)
1409 max_cpus = smp_cpus;
1412 /***********************************************************/
1413 /* USB devices */
1415 static int usb_device_add(const char *devname)
1417 const char *p;
1418 USBDevice *dev = NULL;
1420 if (!usb_enabled(false)) {
1421 return -1;
1424 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1425 dev = usbdevice_create(devname);
1426 if (dev)
1427 goto done;
1429 /* the other ones */
1430 #ifndef CONFIG_LINUX
1431 /* only the linux version is qdev-ified, usb-bsd still needs this */
1432 if (strstart(devname, "host:", &p)) {
1433 dev = usb_host_device_open(usb_bus_find(-1), p);
1434 } else
1435 #endif
1436 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1437 dev = usb_bt_init(usb_bus_find(-1),
1438 devname[2] ? hci_init(p)
1439 : bt_new_hci(qemu_find_bt_vlan(0)));
1440 } else {
1441 return -1;
1443 if (!dev)
1444 return -1;
1446 done:
1447 return 0;
1450 static int usb_device_del(const char *devname)
1452 int bus_num, addr;
1453 const char *p;
1455 if (strstart(devname, "host:", &p)) {
1456 return -1;
1459 if (!usb_enabled(false)) {
1460 return -1;
1463 p = strchr(devname, '.');
1464 if (!p)
1465 return -1;
1466 bus_num = strtoul(devname, NULL, 0);
1467 addr = strtoul(p + 1, NULL, 0);
1469 return usb_device_delete_addr(bus_num, addr);
1472 static int usb_parse(const char *cmdline)
1474 int r;
1475 r = usb_device_add(cmdline);
1476 if (r < 0) {
1477 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1479 return r;
1482 void do_usb_add(Monitor *mon, const QDict *qdict)
1484 const char *devname = qdict_get_str(qdict, "devname");
1485 if (usb_device_add(devname) < 0) {
1486 error_report("could not add USB device '%s'", devname);
1490 void do_usb_del(Monitor *mon, const QDict *qdict)
1492 const char *devname = qdict_get_str(qdict, "devname");
1493 if (usb_device_del(devname) < 0) {
1494 error_report("could not delete USB device '%s'", devname);
1498 /***********************************************************/
1499 /* PCMCIA/Cardbus */
1501 static struct pcmcia_socket_entry_s {
1502 PCMCIASocket *socket;
1503 struct pcmcia_socket_entry_s *next;
1504 } *pcmcia_sockets = 0;
1506 void pcmcia_socket_register(PCMCIASocket *socket)
1508 struct pcmcia_socket_entry_s *entry;
1510 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
1511 entry->socket = socket;
1512 entry->next = pcmcia_sockets;
1513 pcmcia_sockets = entry;
1516 void pcmcia_socket_unregister(PCMCIASocket *socket)
1518 struct pcmcia_socket_entry_s *entry, **ptr;
1520 ptr = &pcmcia_sockets;
1521 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1522 if (entry->socket == socket) {
1523 *ptr = entry->next;
1524 g_free(entry);
1528 void pcmcia_info(Monitor *mon, const QDict *qdict)
1530 struct pcmcia_socket_entry_s *iter;
1532 if (!pcmcia_sockets)
1533 monitor_printf(mon, "No PCMCIA sockets\n");
1535 for (iter = pcmcia_sockets; iter; iter = iter->next)
1536 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1537 iter->socket->attached ? iter->socket->card_string :
1538 "Empty");
1541 /***********************************************************/
1542 /* machine registration */
1544 static QEMUMachine *first_machine = NULL;
1545 QEMUMachine *current_machine = NULL;
1547 int qemu_register_machine(QEMUMachine *m)
1549 QEMUMachine **pm;
1550 pm = &first_machine;
1551 while (*pm != NULL)
1552 pm = &(*pm)->next;
1553 m->next = NULL;
1554 *pm = m;
1555 return 0;
1558 static QEMUMachine *find_machine(const char *name)
1560 QEMUMachine *m;
1562 for(m = first_machine; m != NULL; m = m->next) {
1563 if (!strcmp(m->name, name))
1564 return m;
1565 if (m->alias && !strcmp(m->alias, name))
1566 return m;
1568 return NULL;
1571 QEMUMachine *find_default_machine(void)
1573 QEMUMachine *m;
1575 for(m = first_machine; m != NULL; m = m->next) {
1576 if (m->is_default) {
1577 return m;
1580 return NULL;
1583 MachineInfoList *qmp_query_machines(Error **errp)
1585 MachineInfoList *mach_list = NULL;
1586 QEMUMachine *m;
1588 for (m = first_machine; m; m = m->next) {
1589 MachineInfoList *entry;
1590 MachineInfo *info;
1592 info = g_malloc0(sizeof(*info));
1593 if (m->is_default) {
1594 info->has_is_default = true;
1595 info->is_default = true;
1598 if (m->alias) {
1599 info->has_alias = true;
1600 info->alias = g_strdup(m->alias);
1603 info->name = g_strdup(m->name);
1605 entry = g_malloc0(sizeof(*entry));
1606 entry->value = info;
1607 entry->next = mach_list;
1608 mach_list = entry;
1611 return mach_list;
1614 /***********************************************************/
1615 /* main execution loop */
1617 static void gui_update(void *opaque)
1619 uint64_t interval = GUI_REFRESH_INTERVAL;
1620 DisplayState *ds = opaque;
1621 DisplayChangeListener *dcl;
1623 dpy_refresh(ds);
1625 QLIST_FOREACH(dcl, &ds->listeners, next) {
1626 if (dcl->gui_timer_interval &&
1627 dcl->gui_timer_interval < interval)
1628 interval = dcl->gui_timer_interval;
1630 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
1633 void gui_setup_refresh(DisplayState *ds)
1635 DisplayChangeListener *dcl;
1636 bool need_timer = false;
1637 bool have_gfx = false;
1638 bool have_text = false;
1640 QLIST_FOREACH(dcl, &ds->listeners, next) {
1641 if (dcl->dpy_refresh != NULL) {
1642 need_timer = true;
1644 if (dcl->dpy_gfx_update != NULL) {
1645 have_gfx = true;
1647 if (dcl->dpy_text_update != NULL) {
1648 have_text = true;
1652 if (need_timer && ds->gui_timer == NULL) {
1653 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
1654 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
1656 if (!need_timer && ds->gui_timer != NULL) {
1657 qemu_del_timer(ds->gui_timer);
1658 qemu_free_timer(ds->gui_timer);
1659 ds->gui_timer = NULL;
1662 ds->have_gfx = have_gfx;
1663 ds->have_text = have_text;
1666 struct vm_change_state_entry {
1667 VMChangeStateHandler *cb;
1668 void *opaque;
1669 QLIST_ENTRY (vm_change_state_entry) entries;
1672 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1674 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1675 void *opaque)
1677 VMChangeStateEntry *e;
1679 e = g_malloc0(sizeof (*e));
1681 e->cb = cb;
1682 e->opaque = opaque;
1683 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1684 return e;
1687 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1689 QLIST_REMOVE (e, entries);
1690 g_free (e);
1693 void vm_state_notify(int running, RunState state)
1695 VMChangeStateEntry *e;
1697 trace_vm_state_notify(running, state);
1699 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
1700 e->cb(e->opaque, running, state);
1704 void vm_start(void)
1706 if (!runstate_is_running()) {
1707 cpu_enable_ticks();
1708 runstate_set(RUN_STATE_RUNNING);
1709 vm_state_notify(1, RUN_STATE_RUNNING);
1710 resume_all_vcpus();
1711 monitor_protocol_event(QEVENT_RESUME, NULL);
1715 /* reset/shutdown handler */
1717 typedef struct QEMUResetEntry {
1718 QTAILQ_ENTRY(QEMUResetEntry) entry;
1719 QEMUResetHandler *func;
1720 void *opaque;
1721 } QEMUResetEntry;
1723 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1724 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1725 static int reset_requested;
1726 static int shutdown_requested, shutdown_signal = -1;
1727 static pid_t shutdown_pid;
1728 static int powerdown_requested;
1729 static int debug_requested;
1730 static int suspend_requested;
1731 static int wakeup_requested;
1732 static NotifierList powerdown_notifiers =
1733 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1734 static NotifierList suspend_notifiers =
1735 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1736 static NotifierList wakeup_notifiers =
1737 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1738 static uint32_t wakeup_reason_mask = ~0;
1739 static RunState vmstop_requested = RUN_STATE_MAX;
1741 int qemu_shutdown_requested_get(void)
1743 return shutdown_requested;
1746 int qemu_reset_requested_get(void)
1748 return reset_requested;
1751 static int qemu_shutdown_requested(void)
1753 int r = shutdown_requested;
1754 shutdown_requested = 0;
1755 return r;
1758 static void qemu_kill_report(void)
1760 if (!qtest_enabled() && shutdown_signal != -1) {
1761 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1762 if (shutdown_pid == 0) {
1763 /* This happens for eg ^C at the terminal, so it's worth
1764 * avoiding printing an odd message in that case.
1766 fputc('\n', stderr);
1767 } else {
1768 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
1770 shutdown_signal = -1;
1774 static int qemu_reset_requested(void)
1776 int r = reset_requested;
1777 reset_requested = 0;
1778 return r;
1781 static int qemu_suspend_requested(void)
1783 int r = suspend_requested;
1784 suspend_requested = 0;
1785 return r;
1788 static int qemu_wakeup_requested(void)
1790 int r = wakeup_requested;
1791 wakeup_requested = 0;
1792 return r;
1795 static int qemu_powerdown_requested(void)
1797 int r = powerdown_requested;
1798 powerdown_requested = 0;
1799 return r;
1802 static int qemu_debug_requested(void)
1804 int r = debug_requested;
1805 debug_requested = 0;
1806 return r;
1809 /* We use RUN_STATE_MAX but any invalid value will do */
1810 static bool qemu_vmstop_requested(RunState *r)
1812 if (vmstop_requested < RUN_STATE_MAX) {
1813 *r = vmstop_requested;
1814 vmstop_requested = RUN_STATE_MAX;
1815 return true;
1818 return false;
1821 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1823 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1825 re->func = func;
1826 re->opaque = opaque;
1827 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1830 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1832 QEMUResetEntry *re;
1834 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1835 if (re->func == func && re->opaque == opaque) {
1836 QTAILQ_REMOVE(&reset_handlers, re, entry);
1837 g_free(re);
1838 return;
1843 void qemu_devices_reset(void)
1845 QEMUResetEntry *re, *nre;
1847 /* reset all devices */
1848 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1849 re->func(re->opaque);
1853 void qemu_system_reset(bool report)
1855 if (current_machine && current_machine->reset) {
1856 current_machine->reset();
1857 } else {
1858 qemu_devices_reset();
1860 if (report) {
1861 monitor_protocol_event(QEVENT_RESET, NULL);
1863 cpu_synchronize_all_post_reset();
1866 void qemu_system_reset_request(void)
1868 if (no_reboot) {
1869 shutdown_requested = 1;
1870 } else {
1871 reset_requested = 1;
1873 cpu_stop_current();
1874 qemu_notify_event();
1877 static void qemu_system_suspend(void)
1879 pause_all_vcpus();
1880 notifier_list_notify(&suspend_notifiers, NULL);
1881 runstate_set(RUN_STATE_SUSPENDED);
1882 monitor_protocol_event(QEVENT_SUSPEND, NULL);
1885 void qemu_system_suspend_request(void)
1887 if (runstate_check(RUN_STATE_SUSPENDED)) {
1888 return;
1890 suspend_requested = 1;
1891 cpu_stop_current();
1892 qemu_notify_event();
1895 void qemu_register_suspend_notifier(Notifier *notifier)
1897 notifier_list_add(&suspend_notifiers, notifier);
1900 void qemu_system_wakeup_request(WakeupReason reason)
1902 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1903 return;
1905 if (!(wakeup_reason_mask & (1 << reason))) {
1906 return;
1908 runstate_set(RUN_STATE_RUNNING);
1909 notifier_list_notify(&wakeup_notifiers, &reason);
1910 wakeup_requested = 1;
1911 qemu_notify_event();
1914 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1916 if (enabled) {
1917 wakeup_reason_mask |= (1 << reason);
1918 } else {
1919 wakeup_reason_mask &= ~(1 << reason);
1923 void qemu_register_wakeup_notifier(Notifier *notifier)
1925 notifier_list_add(&wakeup_notifiers, notifier);
1928 void qemu_system_killed(int signal, pid_t pid)
1930 shutdown_signal = signal;
1931 shutdown_pid = pid;
1932 no_shutdown = 0;
1933 qemu_system_shutdown_request();
1936 void qemu_system_shutdown_request(void)
1938 shutdown_requested = 1;
1939 qemu_notify_event();
1942 static void qemu_system_powerdown(void)
1944 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
1945 notifier_list_notify(&powerdown_notifiers, NULL);
1948 void qemu_system_powerdown_request(void)
1950 powerdown_requested = 1;
1951 qemu_notify_event();
1954 void qemu_register_powerdown_notifier(Notifier *notifier)
1956 notifier_list_add(&powerdown_notifiers, notifier);
1959 void qemu_system_debug_request(void)
1961 debug_requested = 1;
1962 qemu_notify_event();
1965 void qemu_system_vmstop_request(RunState state)
1967 vmstop_requested = state;
1968 qemu_notify_event();
1971 static bool main_loop_should_exit(void)
1973 RunState r;
1974 if (qemu_debug_requested()) {
1975 vm_stop(RUN_STATE_DEBUG);
1977 if (qemu_suspend_requested()) {
1978 qemu_system_suspend();
1980 if (qemu_shutdown_requested()) {
1981 qemu_kill_report();
1982 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
1983 if (no_shutdown) {
1984 vm_stop(RUN_STATE_SHUTDOWN);
1985 } else {
1986 return true;
1989 if (qemu_reset_requested()) {
1990 pause_all_vcpus();
1991 cpu_synchronize_all_states();
1992 qemu_system_reset(VMRESET_REPORT);
1993 resume_all_vcpus();
1994 if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1995 runstate_check(RUN_STATE_SHUTDOWN)) {
1996 runstate_set(RUN_STATE_PAUSED);
1999 if (qemu_wakeup_requested()) {
2000 pause_all_vcpus();
2001 cpu_synchronize_all_states();
2002 qemu_system_reset(VMRESET_SILENT);
2003 resume_all_vcpus();
2004 monitor_protocol_event(QEVENT_WAKEUP, NULL);
2006 if (qemu_powerdown_requested()) {
2007 qemu_system_powerdown();
2009 if (qemu_vmstop_requested(&r)) {
2010 vm_stop(r);
2012 return false;
2015 static void main_loop(void)
2017 bool nonblocking;
2018 int last_io = 0;
2019 #ifdef CONFIG_PROFILER
2020 int64_t ti;
2021 #endif
2022 do {
2023 nonblocking = !kvm_enabled() && last_io > 0;
2024 #ifdef CONFIG_PROFILER
2025 ti = profile_getclock();
2026 #endif
2027 last_io = main_loop_wait(nonblocking);
2028 #ifdef CONFIG_PROFILER
2029 dev_time += profile_getclock() - ti;
2030 #endif
2031 } while (!main_loop_should_exit());
2034 static void version(void)
2036 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
2039 static void help(int exitcode)
2041 version();
2042 printf("usage: %s [options] [disk_image]\n\n"
2043 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
2044 error_get_progname());
2046 #define QEMU_OPTIONS_GENERATE_HELP
2047 #include "qemu-options-wrapper.h"
2049 printf("\nDuring emulation, the following keys are useful:\n"
2050 "ctrl-alt-f toggle full screen\n"
2051 "ctrl-alt-n switch to virtual console 'n'\n"
2052 "ctrl-alt toggle mouse and keyboard grab\n"
2053 "\n"
2054 "When using -nographic, press 'ctrl-a h' to get some help.\n");
2056 exit(exitcode);
2059 #define HAS_ARG 0x0001
2061 typedef struct QEMUOption {
2062 const char *name;
2063 int flags;
2064 int index;
2065 uint32_t arch_mask;
2066 } QEMUOption;
2068 static const QEMUOption qemu_options[] = {
2069 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2070 #define QEMU_OPTIONS_GENERATE_OPTIONS
2071 #include "qemu-options-wrapper.h"
2072 { NULL },
2075 static bool vga_available(void)
2077 return object_class_by_name("VGA") || object_class_by_name("isa-vga");
2080 static bool cirrus_vga_available(void)
2082 return object_class_by_name("cirrus-vga")
2083 || object_class_by_name("isa-cirrus-vga");
2086 static bool vmware_vga_available(void)
2088 return object_class_by_name("vmware-svga");
2091 static bool qxl_vga_available(void)
2093 return object_class_by_name("qxl-vga");
2096 static void select_vgahw (const char *p)
2098 const char *opts;
2100 vga_interface_type = VGA_NONE;
2101 if (strstart(p, "std", &opts)) {
2102 if (vga_available()) {
2103 vga_interface_type = VGA_STD;
2104 } else {
2105 fprintf(stderr, "Error: standard VGA not available\n");
2106 exit(0);
2108 } else if (strstart(p, "cirrus", &opts)) {
2109 if (cirrus_vga_available()) {
2110 vga_interface_type = VGA_CIRRUS;
2111 } else {
2112 fprintf(stderr, "Error: Cirrus VGA not available\n");
2113 exit(0);
2115 } else if (strstart(p, "vmware", &opts)) {
2116 if (vmware_vga_available()) {
2117 vga_interface_type = VGA_VMWARE;
2118 } else {
2119 fprintf(stderr, "Error: VMWare SVGA not available\n");
2120 exit(0);
2122 } else if (strstart(p, "xenfb", &opts)) {
2123 vga_interface_type = VGA_XENFB;
2124 } else if (strstart(p, "qxl", &opts)) {
2125 if (qxl_vga_available()) {
2126 vga_interface_type = VGA_QXL;
2127 } else {
2128 fprintf(stderr, "Error: QXL VGA not available\n");
2129 exit(0);
2131 } else if (!strstart(p, "none", &opts)) {
2132 invalid_vga:
2133 fprintf(stderr, "Unknown vga type: %s\n", p);
2134 exit(1);
2136 while (*opts) {
2137 const char *nextopt;
2139 if (strstart(opts, ",retrace=", &nextopt)) {
2140 opts = nextopt;
2141 if (strstart(opts, "dumb", &nextopt))
2142 vga_retrace_method = VGA_RETRACE_DUMB;
2143 else if (strstart(opts, "precise", &nextopt))
2144 vga_retrace_method = VGA_RETRACE_PRECISE;
2145 else goto invalid_vga;
2146 } else goto invalid_vga;
2147 opts = nextopt;
2151 static DisplayType select_display(const char *p)
2153 const char *opts;
2154 DisplayType display = DT_DEFAULT;
2156 if (strstart(p, "sdl", &opts)) {
2157 #ifdef CONFIG_SDL
2158 display = DT_SDL;
2159 while (*opts) {
2160 const char *nextopt;
2162 if (strstart(opts, ",frame=", &nextopt)) {
2163 opts = nextopt;
2164 if (strstart(opts, "on", &nextopt)) {
2165 no_frame = 0;
2166 } else if (strstart(opts, "off", &nextopt)) {
2167 no_frame = 1;
2168 } else {
2169 goto invalid_sdl_args;
2171 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2172 opts = nextopt;
2173 if (strstart(opts, "on", &nextopt)) {
2174 alt_grab = 1;
2175 } else if (strstart(opts, "off", &nextopt)) {
2176 alt_grab = 0;
2177 } else {
2178 goto invalid_sdl_args;
2180 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2181 opts = nextopt;
2182 if (strstart(opts, "on", &nextopt)) {
2183 ctrl_grab = 1;
2184 } else if (strstart(opts, "off", &nextopt)) {
2185 ctrl_grab = 0;
2186 } else {
2187 goto invalid_sdl_args;
2189 } else if (strstart(opts, ",window_close=", &nextopt)) {
2190 opts = nextopt;
2191 if (strstart(opts, "on", &nextopt)) {
2192 no_quit = 0;
2193 } else if (strstart(opts, "off", &nextopt)) {
2194 no_quit = 1;
2195 } else {
2196 goto invalid_sdl_args;
2198 } else {
2199 invalid_sdl_args:
2200 fprintf(stderr, "Invalid SDL option string: %s\n", p);
2201 exit(1);
2203 opts = nextopt;
2205 #else
2206 fprintf(stderr, "SDL support is disabled\n");
2207 exit(1);
2208 #endif
2209 } else if (strstart(p, "vnc", &opts)) {
2210 #ifdef CONFIG_VNC
2211 display_remote++;
2213 if (*opts) {
2214 const char *nextopt;
2216 if (strstart(opts, "=", &nextopt)) {
2217 vnc_display = nextopt;
2220 if (!vnc_display) {
2221 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
2222 exit(1);
2224 #else
2225 fprintf(stderr, "VNC support is disabled\n");
2226 exit(1);
2227 #endif
2228 } else if (strstart(p, "curses", &opts)) {
2229 #ifdef CONFIG_CURSES
2230 display = DT_CURSES;
2231 #else
2232 fprintf(stderr, "Curses support is disabled\n");
2233 exit(1);
2234 #endif
2235 } else if (strstart(p, "gtk", &opts)) {
2236 #ifdef CONFIG_GTK
2237 display = DT_GTK;
2238 #else
2239 fprintf(stderr, "GTK support is disabled\n");
2240 exit(1);
2241 #endif
2242 } else if (strstart(p, "none", &opts)) {
2243 display = DT_NONE;
2244 } else {
2245 fprintf(stderr, "Unknown display type: %s\n", p);
2246 exit(1);
2249 return display;
2252 static int balloon_parse(const char *arg)
2254 QemuOpts *opts;
2256 if (strcmp(arg, "none") == 0) {
2257 return 0;
2260 if (!strncmp(arg, "virtio", 6)) {
2261 if (arg[6] == ',') {
2262 /* have params -> parse them */
2263 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
2264 if (!opts)
2265 return -1;
2266 } else {
2267 /* create empty opts */
2268 opts = qemu_opts_create_nofail(qemu_find_opts("device"));
2270 qemu_opt_set(opts, "driver", "virtio-balloon");
2271 return 0;
2274 return -1;
2277 char *qemu_find_file(int type, const char *name)
2279 int len;
2280 const char *subdir;
2281 char *buf;
2283 /* Try the name as a straight path first */
2284 if (access(name, R_OK) == 0) {
2285 return g_strdup(name);
2287 switch (type) {
2288 case QEMU_FILE_TYPE_BIOS:
2289 subdir = "";
2290 break;
2291 case QEMU_FILE_TYPE_KEYMAP:
2292 subdir = "keymaps/";
2293 break;
2294 default:
2295 abort();
2297 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2298 buf = g_malloc0(len);
2299 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
2300 if (access(buf, R_OK)) {
2301 g_free(buf);
2302 return NULL;
2304 return buf;
2307 static int device_help_func(QemuOpts *opts, void *opaque)
2309 return qdev_device_help(opts);
2312 static int device_init_func(QemuOpts *opts, void *opaque)
2314 DeviceState *dev;
2316 dev = qdev_device_add(opts);
2317 if (!dev)
2318 return -1;
2319 object_unref(OBJECT(dev));
2320 return 0;
2323 static int chardev_init_func(QemuOpts *opts, void *opaque)
2325 Error *local_err = NULL;
2327 qemu_chr_new_from_opts(opts, NULL, &local_err);
2328 if (error_is_set(&local_err)) {
2329 fprintf(stderr, "%s\n", error_get_pretty(local_err));
2330 error_free(local_err);
2331 return -1;
2333 return 0;
2336 #ifdef CONFIG_VIRTFS
2337 static int fsdev_init_func(QemuOpts *opts, void *opaque)
2339 int ret;
2340 ret = qemu_fsdev_add(opts);
2342 return ret;
2344 #endif
2346 static int mon_init_func(QemuOpts *opts, void *opaque)
2348 CharDriverState *chr;
2349 const char *chardev;
2350 const char *mode;
2351 int flags;
2353 mode = qemu_opt_get(opts, "mode");
2354 if (mode == NULL) {
2355 mode = "readline";
2357 if (strcmp(mode, "readline") == 0) {
2358 flags = MONITOR_USE_READLINE;
2359 } else if (strcmp(mode, "control") == 0) {
2360 flags = MONITOR_USE_CONTROL;
2361 } else {
2362 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2363 exit(1);
2366 if (qemu_opt_get_bool(opts, "pretty", 0))
2367 flags |= MONITOR_USE_PRETTY;
2369 if (qemu_opt_get_bool(opts, "default", 0))
2370 flags |= MONITOR_IS_DEFAULT;
2372 chardev = qemu_opt_get(opts, "chardev");
2373 chr = qemu_chr_find(chardev);
2374 if (chr == NULL) {
2375 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2376 exit(1);
2379 monitor_init(chr, flags);
2380 return 0;
2383 static void monitor_parse(const char *optarg, const char *mode)
2385 static int monitor_device_index = 0;
2386 QemuOpts *opts;
2387 const char *p;
2388 char label[32];
2389 int def = 0;
2391 if (strstart(optarg, "chardev:", &p)) {
2392 snprintf(label, sizeof(label), "%s", p);
2393 } else {
2394 snprintf(label, sizeof(label), "compat_monitor%d",
2395 monitor_device_index);
2396 if (monitor_device_index == 0) {
2397 def = 1;
2399 opts = qemu_chr_parse_compat(label, optarg);
2400 if (!opts) {
2401 fprintf(stderr, "parse error: %s\n", optarg);
2402 exit(1);
2406 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, NULL);
2407 if (!opts) {
2408 fprintf(stderr, "duplicate chardev: %s\n", label);
2409 exit(1);
2411 qemu_opt_set(opts, "mode", mode);
2412 qemu_opt_set(opts, "chardev", label);
2413 if (def)
2414 qemu_opt_set(opts, "default", "on");
2415 monitor_device_index++;
2418 struct device_config {
2419 enum {
2420 DEV_USB, /* -usbdevice */
2421 DEV_BT, /* -bt */
2422 DEV_SERIAL, /* -serial */
2423 DEV_PARALLEL, /* -parallel */
2424 DEV_VIRTCON, /* -virtioconsole */
2425 DEV_DEBUGCON, /* -debugcon */
2426 DEV_GDB, /* -gdb, -s */
2427 DEV_SCLP, /* s390 sclp */
2428 } type;
2429 const char *cmdline;
2430 Location loc;
2431 QTAILQ_ENTRY(device_config) next;
2434 static QTAILQ_HEAD(, device_config) device_configs =
2435 QTAILQ_HEAD_INITIALIZER(device_configs);
2437 static void add_device_config(int type, const char *cmdline)
2439 struct device_config *conf;
2441 conf = g_malloc0(sizeof(*conf));
2442 conf->type = type;
2443 conf->cmdline = cmdline;
2444 loc_save(&conf->loc);
2445 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2448 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2450 struct device_config *conf;
2451 int rc;
2453 QTAILQ_FOREACH(conf, &device_configs, next) {
2454 if (conf->type != type)
2455 continue;
2456 loc_push_restore(&conf->loc);
2457 rc = func(conf->cmdline);
2458 loc_pop(&conf->loc);
2459 if (0 != rc)
2460 return rc;
2462 return 0;
2465 static int serial_parse(const char *devname)
2467 static int index = 0;
2468 char label[32];
2470 if (strcmp(devname, "none") == 0)
2471 return 0;
2472 if (index == MAX_SERIAL_PORTS) {
2473 fprintf(stderr, "qemu: too many serial ports\n");
2474 exit(1);
2476 snprintf(label, sizeof(label), "serial%d", index);
2477 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2478 if (!serial_hds[index]) {
2479 fprintf(stderr, "qemu: could not connect serial device"
2480 " to character backend '%s'\n", devname);
2481 return -1;
2483 index++;
2484 return 0;
2487 static int parallel_parse(const char *devname)
2489 static int index = 0;
2490 char label[32];
2492 if (strcmp(devname, "none") == 0)
2493 return 0;
2494 if (index == MAX_PARALLEL_PORTS) {
2495 fprintf(stderr, "qemu: too many parallel ports\n");
2496 exit(1);
2498 snprintf(label, sizeof(label), "parallel%d", index);
2499 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2500 if (!parallel_hds[index]) {
2501 fprintf(stderr, "qemu: could not connect parallel device"
2502 " to character backend '%s'\n", devname);
2503 return -1;
2505 index++;
2506 return 0;
2509 static int virtcon_parse(const char *devname)
2511 QemuOptsList *device = qemu_find_opts("device");
2512 static int index = 0;
2513 char label[32];
2514 QemuOpts *bus_opts, *dev_opts;
2516 if (strcmp(devname, "none") == 0)
2517 return 0;
2518 if (index == MAX_VIRTIO_CONSOLES) {
2519 fprintf(stderr, "qemu: too many virtio consoles\n");
2520 exit(1);
2523 bus_opts = qemu_opts_create_nofail(device);
2524 if (arch_type == QEMU_ARCH_S390X) {
2525 qemu_opt_set(bus_opts, "driver", "virtio-serial-s390");
2526 } else {
2527 qemu_opt_set(bus_opts, "driver", "virtio-serial-pci");
2530 dev_opts = qemu_opts_create_nofail(device);
2531 qemu_opt_set(dev_opts, "driver", "virtconsole");
2533 snprintf(label, sizeof(label), "virtcon%d", index);
2534 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2535 if (!virtcon_hds[index]) {
2536 fprintf(stderr, "qemu: could not connect virtio console"
2537 " to character backend '%s'\n", devname);
2538 return -1;
2540 qemu_opt_set(dev_opts, "chardev", label);
2542 index++;
2543 return 0;
2546 static int sclp_parse(const char *devname)
2548 QemuOptsList *device = qemu_find_opts("device");
2549 static int index = 0;
2550 char label[32];
2551 QemuOpts *dev_opts;
2553 if (strcmp(devname, "none") == 0) {
2554 return 0;
2556 if (index == MAX_SCLP_CONSOLES) {
2557 fprintf(stderr, "qemu: too many sclp consoles\n");
2558 exit(1);
2561 assert(arch_type == QEMU_ARCH_S390X);
2563 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2564 qemu_opt_set(dev_opts, "driver", "sclpconsole");
2566 snprintf(label, sizeof(label), "sclpcon%d", index);
2567 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2568 if (!sclp_hds[index]) {
2569 fprintf(stderr, "qemu: could not connect sclp console"
2570 " to character backend '%s'\n", devname);
2571 return -1;
2573 qemu_opt_set(dev_opts, "chardev", label);
2575 index++;
2576 return 0;
2579 static int debugcon_parse(const char *devname)
2581 QemuOpts *opts;
2583 if (!qemu_chr_new("debugcon", devname, NULL)) {
2584 exit(1);
2586 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2587 if (!opts) {
2588 fprintf(stderr, "qemu: already have a debugcon device\n");
2589 exit(1);
2591 qemu_opt_set(opts, "driver", "isa-debugcon");
2592 qemu_opt_set(opts, "chardev", "debugcon");
2593 return 0;
2596 static QEMUMachine *machine_parse(const char *name)
2598 QEMUMachine *m, *machine = NULL;
2600 if (name) {
2601 machine = find_machine(name);
2603 if (machine) {
2604 return machine;
2606 printf("Supported machines are:\n");
2607 for (m = first_machine; m != NULL; m = m->next) {
2608 if (m->alias) {
2609 printf("%-20s %s (alias of %s)\n", m->alias, m->desc, m->name);
2611 printf("%-20s %s%s\n", m->name, m->desc,
2612 m->is_default ? " (default)" : "");
2614 exit(!name || !is_help_option(name));
2617 static int tcg_init(void)
2619 tcg_exec_init(tcg_tb_size * 1024 * 1024);
2620 return 0;
2623 static struct {
2624 const char *opt_name;
2625 const char *name;
2626 int (*available)(void);
2627 int (*init)(void);
2628 bool *allowed;
2629 } accel_list[] = {
2630 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
2631 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
2632 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2633 { "qtest", "QTest", qtest_available, qtest_init, &qtest_allowed },
2636 static int configure_accelerator(void)
2638 const char *p = NULL;
2639 char buf[10];
2640 int i, ret;
2641 bool accel_initialised = false;
2642 bool init_failed = false;
2644 QemuOptsList *list = qemu_find_opts("machine");
2645 if (!QTAILQ_EMPTY(&list->head)) {
2646 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2649 if (p == NULL) {
2650 /* Use the default "accelerator", tcg */
2651 p = "tcg";
2654 while (!accel_initialised && *p != '\0') {
2655 if (*p == ':') {
2656 p++;
2658 p = get_opt_name(buf, sizeof (buf), p, ':');
2659 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2660 if (strcmp(accel_list[i].opt_name, buf) == 0) {
2661 if (!accel_list[i].available()) {
2662 printf("%s not supported for this target\n",
2663 accel_list[i].name);
2664 continue;
2666 *(accel_list[i].allowed) = true;
2667 ret = accel_list[i].init();
2668 if (ret < 0) {
2669 init_failed = true;
2670 fprintf(stderr, "failed to initialize %s: %s\n",
2671 accel_list[i].name,
2672 strerror(-ret));
2673 *(accel_list[i].allowed) = false;
2674 } else {
2675 accel_initialised = true;
2677 break;
2680 if (i == ARRAY_SIZE(accel_list)) {
2681 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2685 if (!accel_initialised) {
2686 if (!init_failed) {
2687 fprintf(stderr, "No accelerator found!\n");
2689 exit(1);
2692 if (init_failed) {
2693 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2696 return !accel_initialised;
2699 void qemu_add_exit_notifier(Notifier *notify)
2701 notifier_list_add(&exit_notifiers, notify);
2704 void qemu_remove_exit_notifier(Notifier *notify)
2706 notifier_remove(notify);
2709 static void qemu_run_exit_notifiers(void)
2711 notifier_list_notify(&exit_notifiers, NULL);
2714 void qemu_add_machine_init_done_notifier(Notifier *notify)
2716 notifier_list_add(&machine_init_done_notifiers, notify);
2719 static void qemu_run_machine_init_done_notifiers(void)
2721 notifier_list_notify(&machine_init_done_notifiers, NULL);
2724 static const QEMUOption *lookup_opt(int argc, char **argv,
2725 const char **poptarg, int *poptind)
2727 const QEMUOption *popt;
2728 int optind = *poptind;
2729 char *r = argv[optind];
2730 const char *optarg;
2732 loc_set_cmdline(argv, optind, 1);
2733 optind++;
2734 /* Treat --foo the same as -foo. */
2735 if (r[1] == '-')
2736 r++;
2737 popt = qemu_options;
2738 for(;;) {
2739 if (!popt->name) {
2740 error_report("invalid option");
2741 exit(1);
2743 if (!strcmp(popt->name, r + 1))
2744 break;
2745 popt++;
2747 if (popt->flags & HAS_ARG) {
2748 if (optind >= argc) {
2749 error_report("requires an argument");
2750 exit(1);
2752 optarg = argv[optind++];
2753 loc_set_cmdline(argv, optind - 2, 2);
2754 } else {
2755 optarg = NULL;
2758 *poptarg = optarg;
2759 *poptind = optind;
2761 return popt;
2764 static gpointer malloc_and_trace(gsize n_bytes)
2766 void *ptr = malloc(n_bytes);
2767 trace_g_malloc(n_bytes, ptr);
2768 return ptr;
2771 static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2773 void *ptr = realloc(mem, n_bytes);
2774 trace_g_realloc(mem, n_bytes, ptr);
2775 return ptr;
2778 static void free_and_trace(gpointer mem)
2780 trace_g_free(mem);
2781 free(mem);
2784 static int object_set_property(const char *name, const char *value, void *opaque)
2786 Object *obj = OBJECT(opaque);
2787 StringInputVisitor *siv;
2788 Error *local_err = NULL;
2790 if (strcmp(name, "qom-type") == 0 || strcmp(name, "id") == 0) {
2791 return 0;
2794 siv = string_input_visitor_new(value);
2795 object_property_set(obj, string_input_get_visitor(siv), name, &local_err);
2796 string_input_visitor_cleanup(siv);
2798 if (local_err) {
2799 qerror_report_err(local_err);
2800 error_free(local_err);
2801 return -1;
2804 return 0;
2807 static int object_create(QemuOpts *opts, void *opaque)
2809 const char *type = qemu_opt_get(opts, "qom-type");
2810 const char *id = qemu_opts_id(opts);
2811 Object *obj;
2813 g_assert(type != NULL);
2815 if (id == NULL) {
2816 qerror_report(QERR_MISSING_PARAMETER, "id");
2817 return -1;
2820 obj = object_new(type);
2821 if (qemu_opt_foreach(opts, object_set_property, obj, 1) < 0) {
2822 return -1;
2825 object_property_add_child(container_get(object_get_root(), "/objects"),
2826 id, obj, NULL);
2828 return 0;
2831 int main(int argc, char **argv, char **envp)
2833 int i;
2834 int snapshot, linux_boot;
2835 const char *icount_option = NULL;
2836 const char *initrd_filename;
2837 const char *kernel_filename, *kernel_cmdline;
2838 char boot_devices[33] = "";
2839 DisplayState *ds;
2840 int cyls, heads, secs, translation;
2841 QemuOpts *hda_opts = NULL, *opts, *machine_opts;
2842 QemuOptsList *olist;
2843 int optind;
2844 const char *optarg;
2845 const char *loadvm = NULL;
2846 QEMUMachine *machine;
2847 const char *cpu_model;
2848 const char *vga_model = "none";
2849 const char *pid_file = NULL;
2850 const char *incoming = NULL;
2851 #ifdef CONFIG_VNC
2852 int show_vnc_port = 0;
2853 #endif
2854 bool defconfig = true;
2855 bool userconfig = true;
2856 const char *log_mask = NULL;
2857 const char *log_file = NULL;
2858 GMemVTable mem_trace = {
2859 .malloc = malloc_and_trace,
2860 .realloc = realloc_and_trace,
2861 .free = free_and_trace,
2863 const char *trace_events = NULL;
2864 const char *trace_file = NULL;
2866 atexit(qemu_run_exit_notifiers);
2867 error_set_progname(argv[0]);
2869 g_mem_set_vtable(&mem_trace);
2870 if (!g_thread_supported()) {
2871 #if !GLIB_CHECK_VERSION(2, 31, 0)
2872 g_thread_init(NULL);
2873 #else
2874 fprintf(stderr, "glib threading failed to initialize.\n");
2875 exit(1);
2876 #endif
2879 module_call_init(MODULE_INIT_QOM);
2881 qemu_add_opts(&qemu_drive_opts);
2882 qemu_add_opts(&qemu_chardev_opts);
2883 qemu_add_opts(&qemu_device_opts);
2884 qemu_add_opts(&qemu_netdev_opts);
2885 qemu_add_opts(&qemu_net_opts);
2886 qemu_add_opts(&qemu_rtc_opts);
2887 qemu_add_opts(&qemu_global_opts);
2888 qemu_add_opts(&qemu_mon_opts);
2889 qemu_add_opts(&qemu_trace_opts);
2890 qemu_add_opts(&qemu_option_rom_opts);
2891 qemu_add_opts(&qemu_machine_opts);
2892 qemu_add_opts(&qemu_boot_opts);
2893 qemu_add_opts(&qemu_sandbox_opts);
2894 qemu_add_opts(&qemu_add_fd_opts);
2895 qemu_add_opts(&qemu_object_opts);
2896 qemu_add_opts(&qemu_tpmdev_opts);
2898 runstate_init();
2900 init_clocks();
2901 rtc_clock = host_clock;
2903 qemu_cache_utils_init(envp);
2905 QLIST_INIT (&vm_change_state_head);
2906 os_setup_early_signal_handling();
2908 module_call_init(MODULE_INIT_MACHINE);
2909 machine = find_default_machine();
2910 cpu_model = NULL;
2911 ram_size = 0;
2912 snapshot = 0;
2913 cyls = heads = secs = 0;
2914 translation = BIOS_ATA_TRANSLATION_AUTO;
2916 for (i = 0; i < MAX_NODES; i++) {
2917 node_mem[i] = 0;
2918 node_cpumask[i] = bitmap_new(MAX_CPUMASK_BITS);
2921 nb_numa_nodes = 0;
2922 nb_nics = 0;
2924 autostart= 1;
2926 /* first pass of option parsing */
2927 optind = 1;
2928 while (optind < argc) {
2929 if (argv[optind][0] != '-') {
2930 /* disk image */
2931 optind++;
2932 continue;
2933 } else {
2934 const QEMUOption *popt;
2936 popt = lookup_opt(argc, argv, &optarg, &optind);
2937 switch (popt->index) {
2938 case QEMU_OPTION_nodefconfig:
2939 defconfig = false;
2940 break;
2941 case QEMU_OPTION_nouserconfig:
2942 userconfig = false;
2943 break;
2948 if (defconfig) {
2949 int ret;
2950 ret = qemu_read_default_config_files(userconfig);
2951 if (ret < 0) {
2952 exit(1);
2956 /* second pass of option parsing */
2957 optind = 1;
2958 for(;;) {
2959 if (optind >= argc)
2960 break;
2961 if (argv[optind][0] != '-') {
2962 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
2963 } else {
2964 const QEMUOption *popt;
2966 popt = lookup_opt(argc, argv, &optarg, &optind);
2967 if (!(popt->arch_mask & arch_type)) {
2968 printf("Option %s not supported for this target\n", popt->name);
2969 exit(1);
2971 switch(popt->index) {
2972 case QEMU_OPTION_M:
2973 machine = machine_parse(optarg);
2974 break;
2975 case QEMU_OPTION_no_kvm_irqchip: {
2976 olist = qemu_find_opts("machine");
2977 qemu_opts_parse(olist, "kernel_irqchip=off", 0);
2978 break;
2980 case QEMU_OPTION_cpu:
2981 /* hw initialization will check this */
2982 cpu_model = optarg;
2983 break;
2984 case QEMU_OPTION_hda:
2986 char buf[256];
2987 if (cyls == 0)
2988 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2989 else
2990 snprintf(buf, sizeof(buf),
2991 "%s,cyls=%d,heads=%d,secs=%d%s",
2992 HD_OPTS , cyls, heads, secs,
2993 translation == BIOS_ATA_TRANSLATION_LBA ?
2994 ",trans=lba" :
2995 translation == BIOS_ATA_TRANSLATION_NONE ?
2996 ",trans=none" : "");
2997 drive_add(IF_DEFAULT, 0, optarg, buf);
2998 break;
3000 case QEMU_OPTION_hdb:
3001 case QEMU_OPTION_hdc:
3002 case QEMU_OPTION_hdd:
3003 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3004 HD_OPTS);
3005 break;
3006 case QEMU_OPTION_drive:
3007 if (drive_def(optarg) == NULL) {
3008 exit(1);
3010 break;
3011 case QEMU_OPTION_set:
3012 if (qemu_set_option(optarg) != 0)
3013 exit(1);
3014 break;
3015 case QEMU_OPTION_global:
3016 if (qemu_global_option(optarg) != 0)
3017 exit(1);
3018 break;
3019 case QEMU_OPTION_mtdblock:
3020 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3021 break;
3022 case QEMU_OPTION_sd:
3023 drive_add(IF_SD, -1, optarg, SD_OPTS);
3024 break;
3025 case QEMU_OPTION_pflash:
3026 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3027 break;
3028 case QEMU_OPTION_snapshot:
3029 snapshot = 1;
3030 break;
3031 case QEMU_OPTION_hdachs:
3033 const char *p;
3034 p = optarg;
3035 cyls = strtol(p, (char **)&p, 0);
3036 if (cyls < 1 || cyls > 16383)
3037 goto chs_fail;
3038 if (*p != ',')
3039 goto chs_fail;
3040 p++;
3041 heads = strtol(p, (char **)&p, 0);
3042 if (heads < 1 || heads > 16)
3043 goto chs_fail;
3044 if (*p != ',')
3045 goto chs_fail;
3046 p++;
3047 secs = strtol(p, (char **)&p, 0);
3048 if (secs < 1 || secs > 63)
3049 goto chs_fail;
3050 if (*p == ',') {
3051 p++;
3052 if (!strcmp(p, "none"))
3053 translation = BIOS_ATA_TRANSLATION_NONE;
3054 else if (!strcmp(p, "lba"))
3055 translation = BIOS_ATA_TRANSLATION_LBA;
3056 else if (!strcmp(p, "auto"))
3057 translation = BIOS_ATA_TRANSLATION_AUTO;
3058 else
3059 goto chs_fail;
3060 } else if (*p != '\0') {
3061 chs_fail:
3062 fprintf(stderr, "qemu: invalid physical CHS format\n");
3063 exit(1);
3065 if (hda_opts != NULL) {
3066 char num[16];
3067 snprintf(num, sizeof(num), "%d", cyls);
3068 qemu_opt_set(hda_opts, "cyls", num);
3069 snprintf(num, sizeof(num), "%d", heads);
3070 qemu_opt_set(hda_opts, "heads", num);
3071 snprintf(num, sizeof(num), "%d", secs);
3072 qemu_opt_set(hda_opts, "secs", num);
3073 if (translation == BIOS_ATA_TRANSLATION_LBA)
3074 qemu_opt_set(hda_opts, "trans", "lba");
3075 if (translation == BIOS_ATA_TRANSLATION_NONE)
3076 qemu_opt_set(hda_opts, "trans", "none");
3079 break;
3080 case QEMU_OPTION_numa:
3081 numa_add(optarg);
3082 break;
3083 case QEMU_OPTION_display:
3084 display_type = select_display(optarg);
3085 break;
3086 case QEMU_OPTION_nographic:
3087 display_type = DT_NOGRAPHIC;
3088 break;
3089 case QEMU_OPTION_curses:
3090 #ifdef CONFIG_CURSES
3091 display_type = DT_CURSES;
3092 #else
3093 fprintf(stderr, "Curses support is disabled\n");
3094 exit(1);
3095 #endif
3096 break;
3097 case QEMU_OPTION_portrait:
3098 graphic_rotate = 90;
3099 break;
3100 case QEMU_OPTION_rotate:
3101 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3102 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3103 graphic_rotate != 180 && graphic_rotate != 270) {
3104 fprintf(stderr,
3105 "qemu: only 90, 180, 270 deg rotation is available\n");
3106 exit(1);
3108 break;
3109 case QEMU_OPTION_kernel:
3110 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg);
3111 break;
3112 case QEMU_OPTION_initrd:
3113 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg);
3114 break;
3115 case QEMU_OPTION_append:
3116 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg);
3117 break;
3118 case QEMU_OPTION_dtb:
3119 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg);
3120 break;
3121 case QEMU_OPTION_cdrom:
3122 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3123 break;
3124 case QEMU_OPTION_boot:
3126 static const char * const params[] = {
3127 "order", "once", "menu",
3128 "splash", "splash-time",
3129 "reboot-timeout", NULL
3131 char buf[sizeof(boot_devices)];
3132 char *standard_boot_devices;
3133 int legacy = 0;
3135 if (!strchr(optarg, '=')) {
3136 legacy = 1;
3137 pstrcpy(buf, sizeof(buf), optarg);
3138 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
3139 fprintf(stderr,
3140 "qemu: unknown boot parameter '%s' in '%s'\n",
3141 buf, optarg);
3142 exit(1);
3145 if (legacy ||
3146 get_param_value(buf, sizeof(buf), "order", optarg)) {
3147 validate_bootdevices(buf);
3148 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3150 if (!legacy) {
3151 if (get_param_value(buf, sizeof(buf),
3152 "once", optarg)) {
3153 validate_bootdevices(buf);
3154 standard_boot_devices = g_strdup(boot_devices);
3155 pstrcpy(boot_devices, sizeof(boot_devices), buf);
3156 qemu_register_reset(restore_boot_devices,
3157 standard_boot_devices);
3159 if (get_param_value(buf, sizeof(buf),
3160 "menu", optarg)) {
3161 if (!strcmp(buf, "on")) {
3162 boot_menu = 1;
3163 } else if (!strcmp(buf, "off")) {
3164 boot_menu = 0;
3165 } else {
3166 fprintf(stderr,
3167 "qemu: invalid option value '%s'\n",
3168 buf);
3169 exit(1);
3172 if (!qemu_opts_parse(qemu_find_opts("boot-opts"),
3173 optarg, 0)) {
3174 exit(1);
3178 break;
3179 case QEMU_OPTION_fda:
3180 case QEMU_OPTION_fdb:
3181 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3182 optarg, FD_OPTS);
3183 break;
3184 case QEMU_OPTION_no_fd_bootchk:
3185 fd_bootchk = 0;
3186 break;
3187 case QEMU_OPTION_netdev:
3188 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3189 exit(1);
3191 break;
3192 case QEMU_OPTION_net:
3193 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3194 exit(1);
3196 break;
3197 #ifdef CONFIG_LIBISCSI
3198 case QEMU_OPTION_iscsi:
3199 opts = qemu_opts_parse(qemu_find_opts("iscsi"), optarg, 0);
3200 if (!opts) {
3201 exit(1);
3203 break;
3204 #endif
3205 #ifdef CONFIG_SLIRP
3206 case QEMU_OPTION_tftp:
3207 legacy_tftp_prefix = optarg;
3208 break;
3209 case QEMU_OPTION_bootp:
3210 legacy_bootp_filename = optarg;
3211 break;
3212 case QEMU_OPTION_redir:
3213 if (net_slirp_redir(optarg) < 0)
3214 exit(1);
3215 break;
3216 #endif
3217 case QEMU_OPTION_bt:
3218 add_device_config(DEV_BT, optarg);
3219 break;
3220 case QEMU_OPTION_audio_help:
3221 if (!(audio_available())) {
3222 printf("Option %s not supported for this target\n", popt->name);
3223 exit(1);
3225 AUD_help ();
3226 exit (0);
3227 break;
3228 case QEMU_OPTION_soundhw:
3229 if (!(audio_available())) {
3230 printf("Option %s not supported for this target\n", popt->name);
3231 exit(1);
3233 select_soundhw (optarg);
3234 break;
3235 case QEMU_OPTION_h:
3236 help(0);
3237 break;
3238 case QEMU_OPTION_version:
3239 version();
3240 exit(0);
3241 break;
3242 case QEMU_OPTION_m: {
3243 int64_t value;
3244 uint64_t sz;
3245 char *end;
3247 value = strtosz(optarg, &end);
3248 if (value < 0 || *end) {
3249 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
3250 exit(1);
3252 sz = QEMU_ALIGN_UP((uint64_t)value, 8192);
3253 ram_size = sz;
3254 if (ram_size != sz) {
3255 fprintf(stderr, "qemu: ram size too large\n");
3256 exit(1);
3258 break;
3260 #ifdef CONFIG_TPM
3261 case QEMU_OPTION_tpmdev:
3262 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3263 exit(1);
3265 break;
3266 #endif
3267 case QEMU_OPTION_mempath:
3268 mem_path = optarg;
3269 break;
3270 #ifdef MAP_POPULATE
3271 case QEMU_OPTION_mem_prealloc:
3272 mem_prealloc = 1;
3273 break;
3274 #endif
3275 case QEMU_OPTION_d:
3276 log_mask = optarg;
3277 break;
3278 case QEMU_OPTION_D:
3279 log_file = optarg;
3280 break;
3281 case QEMU_OPTION_s:
3282 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3283 break;
3284 case QEMU_OPTION_gdb:
3285 add_device_config(DEV_GDB, optarg);
3286 break;
3287 case QEMU_OPTION_L:
3288 data_dir = optarg;
3289 break;
3290 case QEMU_OPTION_bios:
3291 bios_name = optarg;
3292 break;
3293 case QEMU_OPTION_singlestep:
3294 singlestep = 1;
3295 break;
3296 case QEMU_OPTION_S:
3297 autostart = 0;
3298 break;
3299 case QEMU_OPTION_k:
3300 keyboard_layout = optarg;
3301 break;
3302 case QEMU_OPTION_localtime:
3303 rtc_utc = 0;
3304 break;
3305 case QEMU_OPTION_vga:
3306 vga_model = optarg;
3307 default_vga = 0;
3308 break;
3309 case QEMU_OPTION_g:
3311 const char *p;
3312 int w, h, depth;
3313 p = optarg;
3314 w = strtol(p, (char **)&p, 10);
3315 if (w <= 0) {
3316 graphic_error:
3317 fprintf(stderr, "qemu: invalid resolution or depth\n");
3318 exit(1);
3320 if (*p != 'x')
3321 goto graphic_error;
3322 p++;
3323 h = strtol(p, (char **)&p, 10);
3324 if (h <= 0)
3325 goto graphic_error;
3326 if (*p == 'x') {
3327 p++;
3328 depth = strtol(p, (char **)&p, 10);
3329 if (depth != 8 && depth != 15 && depth != 16 &&
3330 depth != 24 && depth != 32)
3331 goto graphic_error;
3332 } else if (*p == '\0') {
3333 depth = graphic_depth;
3334 } else {
3335 goto graphic_error;
3338 graphic_width = w;
3339 graphic_height = h;
3340 graphic_depth = depth;
3342 break;
3343 case QEMU_OPTION_echr:
3345 char *r;
3346 term_escape_char = strtol(optarg, &r, 0);
3347 if (r == optarg)
3348 printf("Bad argument to echr\n");
3349 break;
3351 case QEMU_OPTION_monitor:
3352 monitor_parse(optarg, "readline");
3353 default_monitor = 0;
3354 break;
3355 case QEMU_OPTION_qmp:
3356 monitor_parse(optarg, "control");
3357 default_monitor = 0;
3358 break;
3359 case QEMU_OPTION_mon:
3360 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
3361 if (!opts) {
3362 exit(1);
3364 default_monitor = 0;
3365 break;
3366 case QEMU_OPTION_chardev:
3367 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
3368 if (!opts) {
3369 exit(1);
3371 break;
3372 case QEMU_OPTION_fsdev:
3373 olist = qemu_find_opts("fsdev");
3374 if (!olist) {
3375 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
3376 exit(1);
3378 opts = qemu_opts_parse(olist, optarg, 1);
3379 if (!opts) {
3380 exit(1);
3382 break;
3383 case QEMU_OPTION_virtfs: {
3384 QemuOpts *fsdev;
3385 QemuOpts *device;
3386 const char *writeout, *sock_fd, *socket;
3388 olist = qemu_find_opts("virtfs");
3389 if (!olist) {
3390 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
3391 exit(1);
3393 opts = qemu_opts_parse(olist, optarg, 1);
3394 if (!opts) {
3395 exit(1);
3398 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3399 qemu_opt_get(opts, "mount_tag") == NULL) {
3400 fprintf(stderr, "Usage: -virtfs fsdriver,mount_tag=tag.\n");
3401 exit(1);
3403 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3404 qemu_opt_get(opts, "mount_tag"),
3405 1, NULL);
3406 if (!fsdev) {
3407 fprintf(stderr, "duplicate fsdev id: %s\n",
3408 qemu_opt_get(opts, "mount_tag"));
3409 exit(1);
3412 writeout = qemu_opt_get(opts, "writeout");
3413 if (writeout) {
3414 #ifdef CONFIG_SYNC_FILE_RANGE
3415 qemu_opt_set(fsdev, "writeout", writeout);
3416 #else
3417 fprintf(stderr, "writeout=immediate not supported on "
3418 "this platform\n");
3419 exit(1);
3420 #endif
3422 qemu_opt_set(fsdev, "fsdriver", qemu_opt_get(opts, "fsdriver"));
3423 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
3424 qemu_opt_set(fsdev, "security_model",
3425 qemu_opt_get(opts, "security_model"));
3426 socket = qemu_opt_get(opts, "socket");
3427 if (socket) {
3428 qemu_opt_set(fsdev, "socket", socket);
3430 sock_fd = qemu_opt_get(opts, "sock_fd");
3431 if (sock_fd) {
3432 qemu_opt_set(fsdev, "sock_fd", sock_fd);
3435 qemu_opt_set_bool(fsdev, "readonly",
3436 qemu_opt_get_bool(opts, "readonly", 0));
3437 device = qemu_opts_create_nofail(qemu_find_opts("device"));
3438 qemu_opt_set(device, "driver", "virtio-9p-pci");
3439 qemu_opt_set(device, "fsdev",
3440 qemu_opt_get(opts, "mount_tag"));
3441 qemu_opt_set(device, "mount_tag",
3442 qemu_opt_get(opts, "mount_tag"));
3443 break;
3445 case QEMU_OPTION_virtfs_synth: {
3446 QemuOpts *fsdev;
3447 QemuOpts *device;
3449 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3450 1, NULL);
3451 if (!fsdev) {
3452 fprintf(stderr, "duplicate option: %s\n", "virtfs_synth");
3453 exit(1);
3455 qemu_opt_set(fsdev, "fsdriver", "synth");
3457 device = qemu_opts_create_nofail(qemu_find_opts("device"));
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 #ifdef CONFIG_SDL
3508 case QEMU_OPTION_no_frame:
3509 no_frame = 1;
3510 break;
3511 case QEMU_OPTION_alt_grab:
3512 alt_grab = 1;
3513 break;
3514 case QEMU_OPTION_ctrl_grab:
3515 ctrl_grab = 1;
3516 break;
3517 case QEMU_OPTION_no_quit:
3518 no_quit = 1;
3519 break;
3520 case QEMU_OPTION_sdl:
3521 display_type = DT_SDL;
3522 break;
3523 #else
3524 case QEMU_OPTION_no_frame:
3525 case QEMU_OPTION_alt_grab:
3526 case QEMU_OPTION_ctrl_grab:
3527 case QEMU_OPTION_no_quit:
3528 case QEMU_OPTION_sdl:
3529 fprintf(stderr, "SDL support is disabled\n");
3530 exit(1);
3531 #endif
3532 case QEMU_OPTION_pidfile:
3533 pid_file = optarg;
3534 break;
3535 case QEMU_OPTION_win2k_hack:
3536 win2k_install_hack = 1;
3537 break;
3538 case QEMU_OPTION_rtc_td_hack: {
3539 static GlobalProperty slew_lost_ticks[] = {
3541 .driver = "mc146818rtc",
3542 .property = "lost_tick_policy",
3543 .value = "slew",
3545 { /* end of list */ }
3548 qdev_prop_register_global_list(slew_lost_ticks);
3549 break;
3551 case QEMU_OPTION_acpitable:
3552 do_acpitable_option(optarg);
3553 break;
3554 case QEMU_OPTION_smbios:
3555 do_smbios_option(optarg);
3556 break;
3557 case QEMU_OPTION_enable_kvm:
3558 olist = qemu_find_opts("machine");
3559 qemu_opts_parse(olist, "accel=kvm", 0);
3560 break;
3561 case QEMU_OPTION_machine:
3562 olist = qemu_find_opts("machine");
3563 opts = qemu_opts_parse(olist, optarg, 1);
3564 if (!opts) {
3565 exit(1);
3567 optarg = qemu_opt_get(opts, "type");
3568 if (optarg) {
3569 machine = machine_parse(optarg);
3571 break;
3572 case QEMU_OPTION_no_kvm:
3573 olist = qemu_find_opts("machine");
3574 qemu_opts_parse(olist, "accel=tcg", 0);
3575 break;
3576 case QEMU_OPTION_no_kvm_pit: {
3577 fprintf(stderr, "Warning: KVM PIT can no longer be disabled "
3578 "separately.\n");
3579 break;
3581 case QEMU_OPTION_no_kvm_pit_reinjection: {
3582 static GlobalProperty kvm_pit_lost_tick_policy[] = {
3584 .driver = "kvm-pit",
3585 .property = "lost_tick_policy",
3586 .value = "discard",
3588 { /* end of list */ }
3591 fprintf(stderr, "Warning: option deprecated, use "
3592 "lost_tick_policy property of kvm-pit instead.\n");
3593 qdev_prop_register_global_list(kvm_pit_lost_tick_policy);
3594 break;
3596 case QEMU_OPTION_usb:
3597 olist = qemu_find_opts("machine");
3598 qemu_opts_parse(olist, "usb=on", 0);
3599 break;
3600 case QEMU_OPTION_usbdevice:
3601 olist = qemu_find_opts("machine");
3602 qemu_opts_parse(olist, "usb=on", 0);
3603 add_device_config(DEV_USB, optarg);
3604 break;
3605 case QEMU_OPTION_device:
3606 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
3607 exit(1);
3609 break;
3610 case QEMU_OPTION_smp:
3611 smp_parse(optarg);
3612 if (smp_cpus < 1) {
3613 fprintf(stderr, "Invalid number of CPUs\n");
3614 exit(1);
3616 if (max_cpus < smp_cpus) {
3617 fprintf(stderr, "maxcpus must be equal to or greater than "
3618 "smp\n");
3619 exit(1);
3621 if (max_cpus > 255) {
3622 fprintf(stderr, "Unsupported number of maxcpus\n");
3623 exit(1);
3625 break;
3626 case QEMU_OPTION_vnc:
3627 #ifdef CONFIG_VNC
3628 display_remote++;
3629 vnc_display = optarg;
3630 #else
3631 fprintf(stderr, "VNC support is disabled\n");
3632 exit(1);
3633 #endif
3634 break;
3635 case QEMU_OPTION_no_acpi:
3636 acpi_enabled = 0;
3637 break;
3638 case QEMU_OPTION_no_hpet:
3639 no_hpet = 1;
3640 break;
3641 case QEMU_OPTION_balloon:
3642 if (balloon_parse(optarg) < 0) {
3643 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3644 exit(1);
3646 break;
3647 case QEMU_OPTION_no_reboot:
3648 no_reboot = 1;
3649 break;
3650 case QEMU_OPTION_no_shutdown:
3651 no_shutdown = 1;
3652 break;
3653 case QEMU_OPTION_show_cursor:
3654 cursor_hide = 0;
3655 break;
3656 case QEMU_OPTION_uuid:
3657 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3658 fprintf(stderr, "Fail to parse UUID string."
3659 " Wrong format.\n");
3660 exit(1);
3662 break;
3663 case QEMU_OPTION_option_rom:
3664 if (nb_option_roms >= MAX_OPTION_ROMS) {
3665 fprintf(stderr, "Too many option ROMs\n");
3666 exit(1);
3668 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
3669 if (!opts) {
3670 exit(1);
3672 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3673 option_rom[nb_option_roms].bootindex =
3674 qemu_opt_get_number(opts, "bootindex", -1);
3675 if (!option_rom[nb_option_roms].name) {
3676 fprintf(stderr, "Option ROM file is not specified\n");
3677 exit(1);
3679 nb_option_roms++;
3680 break;
3681 case QEMU_OPTION_semihosting:
3682 semihosting_enabled = 1;
3683 break;
3684 case QEMU_OPTION_tdf:
3685 fprintf(stderr, "Warning: user space PIT time drift fix "
3686 "is no longer supported.\n");
3687 break;
3688 case QEMU_OPTION_name:
3689 qemu_name = g_strdup(optarg);
3691 char *p = strchr(qemu_name, ',');
3692 if (p != NULL) {
3693 *p++ = 0;
3694 if (strncmp(p, "process=", 8)) {
3695 fprintf(stderr, "Unknown subargument %s to -name\n", p);
3696 exit(1);
3698 p += 8;
3699 os_set_proc_name(p);
3702 break;
3703 case QEMU_OPTION_prom_env:
3704 if (nb_prom_envs >= MAX_PROM_ENVS) {
3705 fprintf(stderr, "Too many prom variables\n");
3706 exit(1);
3708 prom_envs[nb_prom_envs] = optarg;
3709 nb_prom_envs++;
3710 break;
3711 case QEMU_OPTION_old_param:
3712 old_param = 1;
3713 break;
3714 case QEMU_OPTION_clock:
3715 configure_alarms(optarg);
3716 break;
3717 case QEMU_OPTION_startdate:
3718 configure_rtc_date_offset(optarg, 1);
3719 break;
3720 case QEMU_OPTION_rtc:
3721 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
3722 if (!opts) {
3723 exit(1);
3725 configure_rtc(opts);
3726 break;
3727 case QEMU_OPTION_tb_size:
3728 tcg_tb_size = strtol(optarg, NULL, 0);
3729 if (tcg_tb_size < 0) {
3730 tcg_tb_size = 0;
3732 break;
3733 case QEMU_OPTION_icount:
3734 icount_option = optarg;
3735 break;
3736 case QEMU_OPTION_incoming:
3737 incoming = optarg;
3738 runstate_set(RUN_STATE_INMIGRATE);
3739 break;
3740 case QEMU_OPTION_nodefaults:
3741 default_serial = 0;
3742 default_parallel = 0;
3743 default_virtcon = 0;
3744 default_sclp = 0;
3745 default_monitor = 0;
3746 default_net = 0;
3747 default_floppy = 0;
3748 default_cdrom = 0;
3749 default_sdcard = 0;
3750 default_vga = 0;
3751 break;
3752 case QEMU_OPTION_xen_domid:
3753 if (!(xen_available())) {
3754 printf("Option %s not supported for this target\n", popt->name);
3755 exit(1);
3757 xen_domid = atoi(optarg);
3758 break;
3759 case QEMU_OPTION_xen_create:
3760 if (!(xen_available())) {
3761 printf("Option %s not supported for this target\n", popt->name);
3762 exit(1);
3764 xen_mode = XEN_CREATE;
3765 break;
3766 case QEMU_OPTION_xen_attach:
3767 if (!(xen_available())) {
3768 printf("Option %s not supported for this target\n", popt->name);
3769 exit(1);
3771 xen_mode = XEN_ATTACH;
3772 break;
3773 case QEMU_OPTION_trace:
3775 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3776 if (!opts) {
3777 exit(1);
3779 trace_events = qemu_opt_get(opts, "events");
3780 trace_file = qemu_opt_get(opts, "file");
3781 break;
3783 case QEMU_OPTION_readconfig:
3785 int ret = qemu_read_config_file(optarg);
3786 if (ret < 0) {
3787 fprintf(stderr, "read config %s: %s\n", optarg,
3788 strerror(-ret));
3789 exit(1);
3791 break;
3793 case QEMU_OPTION_spice:
3794 olist = qemu_find_opts("spice");
3795 if (!olist) {
3796 fprintf(stderr, "spice is not supported by this qemu build.\n");
3797 exit(1);
3799 opts = qemu_opts_parse(olist, optarg, 0);
3800 if (!opts) {
3801 exit(1);
3803 display_remote++;
3804 break;
3805 case QEMU_OPTION_writeconfig:
3807 FILE *fp;
3808 if (strcmp(optarg, "-") == 0) {
3809 fp = stdout;
3810 } else {
3811 fp = fopen(optarg, "w");
3812 if (fp == NULL) {
3813 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3814 exit(1);
3817 qemu_config_write(fp);
3818 fclose(fp);
3819 break;
3821 case QEMU_OPTION_qtest:
3822 qtest_chrdev = optarg;
3823 break;
3824 case QEMU_OPTION_qtest_log:
3825 qtest_log = optarg;
3826 break;
3827 case QEMU_OPTION_sandbox:
3828 opts = qemu_opts_parse(qemu_find_opts("sandbox"), optarg, 1);
3829 if (!opts) {
3830 exit(1);
3832 break;
3833 case QEMU_OPTION_add_fd:
3834 #ifndef _WIN32
3835 opts = qemu_opts_parse(qemu_find_opts("add-fd"), optarg, 0);
3836 if (!opts) {
3837 exit(1);
3839 #else
3840 error_report("File descriptor passing is disabled on this "
3841 "platform");
3842 exit(1);
3843 #endif
3844 break;
3845 case QEMU_OPTION_object:
3846 opts = qemu_opts_parse(qemu_find_opts("object"), optarg, 1);
3847 if (!opts) {
3848 exit(1);
3850 break;
3851 default:
3852 os_parse_cmd_args(popt->index, optarg);
3856 loc_set_none();
3858 if (qemu_init_main_loop()) {
3859 fprintf(stderr, "qemu_init_main_loop failed\n");
3860 exit(1);
3863 if (qemu_opts_foreach(qemu_find_opts("sandbox"), parse_sandbox, NULL, 0)) {
3864 exit(1);
3867 #ifndef _WIN32
3868 if (qemu_opts_foreach(qemu_find_opts("add-fd"), parse_add_fd, NULL, 1)) {
3869 exit(1);
3872 if (qemu_opts_foreach(qemu_find_opts("add-fd"), cleanup_add_fd, NULL, 1)) {
3873 exit(1);
3875 #endif
3877 if (machine == NULL) {
3878 fprintf(stderr, "No machine found.\n");
3879 exit(1);
3882 if (machine->hw_version) {
3883 qemu_set_version(machine->hw_version);
3886 if (qemu_opts_foreach(qemu_find_opts("object"),
3887 object_create, NULL, 0) != 0) {
3888 exit(1);
3891 /* Init CPU def lists, based on config
3892 * - Must be called after all the qemu_read_config_file() calls
3893 * - Must be called before list_cpus()
3894 * - Must be called before machine->init()
3896 cpudef_init();
3898 if (cpu_model && is_help_option(cpu_model)) {
3899 list_cpus(stdout, &fprintf, cpu_model);
3900 exit(0);
3903 /* Open the logfile at this point, if necessary. We can't open the logfile
3904 * when encountering either of the logging options (-d or -D) because the
3905 * other one may be encountered later on the command line, changing the
3906 * location or level of logging.
3908 if (log_mask) {
3909 int mask;
3910 if (log_file) {
3911 qemu_set_log_filename(log_file);
3914 mask = qemu_str_to_log_mask(log_mask);
3915 if (!mask) {
3916 qemu_print_log_usage(stdout);
3917 exit(1);
3919 qemu_set_log(mask);
3922 if (!trace_backend_init(trace_events, trace_file)) {
3923 exit(1);
3926 /* If no data_dir is specified then try to find it relative to the
3927 executable path. */
3928 if (!data_dir) {
3929 data_dir = os_find_datadir(argv[0]);
3931 /* If all else fails use the install path specified when building. */
3932 if (!data_dir) {
3933 data_dir = CONFIG_QEMU_DATADIR;
3937 * Default to max_cpus = smp_cpus, in case the user doesn't
3938 * specify a max_cpus value.
3940 if (!max_cpus)
3941 max_cpus = smp_cpus;
3943 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
3944 if (smp_cpus > machine->max_cpus) {
3945 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3946 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3947 machine->max_cpus);
3948 exit(1);
3952 * Get the default machine options from the machine if it is not already
3953 * specified either by the configuration file or by the command line.
3955 if (machine->default_machine_opts) {
3956 qemu_opts_set_defaults(qemu_find_opts("machine"),
3957 machine->default_machine_opts, 0);
3960 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3961 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
3963 if (machine->no_serial) {
3964 default_serial = 0;
3966 if (machine->no_parallel) {
3967 default_parallel = 0;
3969 if (!machine->use_virtcon) {
3970 default_virtcon = 0;
3972 if (!machine->use_sclp) {
3973 default_sclp = 0;
3975 if (machine->no_floppy) {
3976 default_floppy = 0;
3978 if (machine->no_cdrom) {
3979 default_cdrom = 0;
3981 if (machine->no_sdcard) {
3982 default_sdcard = 0;
3985 if (is_daemonized()) {
3986 /* According to documentation and historically, -nographic redirects
3987 * serial port, parallel port and monitor to stdio, which does not work
3988 * with -daemonize. We can redirect these to null instead, but since
3989 * -nographic is legacy, let's just error out.
3990 * We disallow -nographic only if all other ports are not redirected
3991 * explicitly, to not break existing legacy setups which uses
3992 * -nographic _and_ redirects all ports explicitly - this is valid
3993 * usage, -nographic is just a no-op in this case.
3995 if (display_type == DT_NOGRAPHIC
3996 && (default_parallel || default_serial
3997 || default_monitor || default_virtcon)) {
3998 fprintf(stderr, "-nographic can not be used with -daemonize\n");
3999 exit(1);
4001 #ifdef CONFIG_CURSES
4002 if (display_type == DT_CURSES) {
4003 fprintf(stderr, "curses display can not be used with -daemonize\n");
4004 exit(1);
4006 #endif
4009 if (display_type == DT_NOGRAPHIC) {
4010 if (default_parallel)
4011 add_device_config(DEV_PARALLEL, "null");
4012 if (default_serial && default_monitor) {
4013 add_device_config(DEV_SERIAL, "mon:stdio");
4014 } else if (default_virtcon && default_monitor) {
4015 add_device_config(DEV_VIRTCON, "mon:stdio");
4016 } else if (default_sclp && default_monitor) {
4017 add_device_config(DEV_SCLP, "mon:stdio");
4018 } else {
4019 if (default_serial)
4020 add_device_config(DEV_SERIAL, "stdio");
4021 if (default_virtcon)
4022 add_device_config(DEV_VIRTCON, "stdio");
4023 if (default_sclp) {
4024 add_device_config(DEV_SCLP, "stdio");
4026 if (default_monitor)
4027 monitor_parse("stdio", "readline");
4029 } else {
4030 if (default_serial)
4031 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4032 if (default_parallel)
4033 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4034 if (default_monitor)
4035 monitor_parse("vc:80Cx24C", "readline");
4036 if (default_virtcon)
4037 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4038 if (default_sclp) {
4039 add_device_config(DEV_SCLP, "vc:80Cx24C");
4043 if (display_type == DT_DEFAULT && !display_remote) {
4044 #if defined(CONFIG_GTK)
4045 display_type = DT_GTK;
4046 #elif defined(CONFIG_SDL) || defined(CONFIG_COCOA)
4047 display_type = DT_SDL;
4048 #elif defined(CONFIG_VNC)
4049 vnc_display = "localhost:0,to=99";
4050 show_vnc_port = 1;
4051 #else
4052 display_type = DT_NONE;
4053 #endif
4056 #if defined(CONFIG_GTK)
4057 if (display_type == DT_GTK) {
4058 early_gtk_display_init();
4060 #endif
4062 socket_init();
4064 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
4065 exit(1);
4066 #ifdef CONFIG_VIRTFS
4067 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
4068 exit(1);
4070 #endif
4072 os_daemonize();
4074 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4075 os_pidfile_error();
4076 exit(1);
4079 /* init the memory */
4080 if (ram_size == 0) {
4081 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
4084 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0)
4085 != 0) {
4086 exit(0);
4089 configure_accelerator();
4091 machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
4092 if (machine_opts) {
4093 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4094 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4095 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4096 } else {
4097 kernel_filename = initrd_filename = kernel_cmdline = NULL;
4100 if (!kernel_cmdline) {
4101 kernel_cmdline = "";
4104 linux_boot = (kernel_filename != NULL);
4106 if (!linux_boot && *kernel_cmdline != '\0') {
4107 fprintf(stderr, "-append only allowed with -kernel option\n");
4108 exit(1);
4111 if (!linux_boot && initrd_filename != NULL) {
4112 fprintf(stderr, "-initrd only allowed with -kernel option\n");
4113 exit(1);
4116 if (!linux_boot && machine_opts && qemu_opt_get(machine_opts, "dtb")) {
4117 fprintf(stderr, "-dtb only allowed with -kernel option\n");
4118 exit(1);
4121 os_set_line_buffering();
4123 qemu_init_cpu_loop();
4124 qemu_mutex_lock_iothread();
4126 #ifdef CONFIG_SPICE
4127 /* spice needs the timers to be initialized by this point */
4128 qemu_spice_init();
4129 #endif
4131 if (icount_option && (kvm_enabled() || xen_enabled())) {
4132 fprintf(stderr, "-icount is not allowed with kvm or xen\n");
4133 exit(1);
4135 configure_icount(icount_option);
4137 /* clean up network at qemu process termination */
4138 atexit(&net_cleanup);
4140 if (net_init_clients() < 0) {
4141 exit(1);
4144 #ifdef CONFIG_TPM
4145 if (tpm_init() < 0) {
4146 exit(1);
4148 #endif
4150 /* init the bluetooth world */
4151 if (foreach_device_config(DEV_BT, bt_parse))
4152 exit(1);
4154 if (!xen_enabled()) {
4155 /* On 32-bit hosts, QEMU is limited by virtual address space */
4156 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4157 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4158 exit(1);
4162 cpu_exec_init_all();
4164 bdrv_init_with_whitelist();
4166 blk_mig_init();
4168 /* open the virtual block devices */
4169 if (snapshot)
4170 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
4171 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4172 &machine->block_default_type, 1) != 0) {
4173 exit(1);
4176 default_drive(default_cdrom, snapshot, machine->block_default_type, 2,
4177 CDROM_OPTS);
4178 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4179 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4181 register_savevm_live(NULL, "ram", 0, 4, &savevm_ram_handlers, NULL);
4183 if (nb_numa_nodes > 0) {
4184 int i;
4186 if (nb_numa_nodes > MAX_NODES) {
4187 nb_numa_nodes = MAX_NODES;
4190 /* If no memory size if given for any node, assume the default case
4191 * and distribute the available memory equally across all nodes
4193 for (i = 0; i < nb_numa_nodes; i++) {
4194 if (node_mem[i] != 0)
4195 break;
4197 if (i == nb_numa_nodes) {
4198 uint64_t usedmem = 0;
4200 /* On Linux, the each node's border has to be 8MB aligned,
4201 * the final node gets the rest.
4203 for (i = 0; i < nb_numa_nodes - 1; i++) {
4204 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
4205 usedmem += node_mem[i];
4207 node_mem[i] = ram_size - usedmem;
4210 for (i = 0; i < nb_numa_nodes; i++) {
4211 if (!bitmap_empty(node_cpumask[i], MAX_CPUMASK_BITS)) {
4212 break;
4215 /* assigning the VCPUs round-robin is easier to implement, guest OSes
4216 * must cope with this anyway, because there are BIOSes out there in
4217 * real machines which also use this scheme.
4219 if (i == nb_numa_nodes) {
4220 for (i = 0; i < max_cpus; i++) {
4221 set_bit(i, node_cpumask[i % nb_numa_nodes]);
4226 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
4227 exit(1);
4230 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4231 exit(1);
4232 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4233 exit(1);
4234 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4235 exit(1);
4236 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4237 exit(1);
4239 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4240 exit(1);
4242 /* If no default VGA is requested, the default is "none". */
4243 if (default_vga) {
4244 if (cirrus_vga_available()) {
4245 vga_model = "cirrus";
4246 } else if (vga_available()) {
4247 vga_model = "std";
4250 select_vgahw(vga_model);
4252 if (watchdog) {
4253 i = select_watchdog(watchdog);
4254 if (i > 0)
4255 exit (i == 1 ? 1 : 0);
4258 if (machine->compat_props) {
4259 qdev_prop_register_global_list(machine->compat_props);
4261 qemu_add_globals();
4263 qdev_machine_init();
4265 QEMUMachineInitArgs args = { .ram_size = ram_size,
4266 .boot_device = (boot_devices[0] == '\0') ?
4267 machine->boot_order :
4268 boot_devices,
4269 .kernel_filename = kernel_filename,
4270 .kernel_cmdline = kernel_cmdline,
4271 .initrd_filename = initrd_filename,
4272 .cpu_model = cpu_model };
4273 machine->init(&args);
4275 cpu_synchronize_all_post_init();
4277 set_numa_modes();
4279 current_machine = machine;
4281 /* init USB devices */
4282 if (usb_enabled(false)) {
4283 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4284 exit(1);
4287 /* init generic devices */
4288 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
4289 exit(1);
4291 net_check_clients();
4293 /* just use the first displaystate for the moment */
4294 ds = get_displaystate();
4296 /* init local displays */
4297 switch (display_type) {
4298 case DT_NOGRAPHIC:
4299 break;
4300 #if defined(CONFIG_CURSES)
4301 case DT_CURSES:
4302 curses_display_init(ds, full_screen);
4303 break;
4304 #endif
4305 #if defined(CONFIG_SDL)
4306 case DT_SDL:
4307 sdl_display_init(ds, full_screen, no_frame);
4308 break;
4309 #elif defined(CONFIG_COCOA)
4310 case DT_SDL:
4311 cocoa_display_init(ds, full_screen);
4312 break;
4313 #endif
4314 #if defined(CONFIG_GTK)
4315 case DT_GTK:
4316 gtk_display_init(ds);
4317 break;
4318 #endif
4319 default:
4320 break;
4323 /* must be after terminal init, SDL library changes signal handlers */
4324 os_setup_signal_handling();
4326 #ifdef CONFIG_VNC
4327 /* init remote displays */
4328 if (vnc_display) {
4329 Error *local_err = NULL;
4330 vnc_display_init(ds);
4331 vnc_display_open(ds, vnc_display, &local_err);
4332 if (local_err != NULL) {
4333 fprintf(stderr, "Failed to start VNC server on `%s': %s\n",
4334 vnc_display, error_get_pretty(local_err));
4335 error_free(local_err);
4336 exit(1);
4339 if (show_vnc_port) {
4340 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
4343 #endif
4344 #ifdef CONFIG_SPICE
4345 if (using_spice && !qxl_enabled) {
4346 qemu_spice_display_init(ds);
4348 #endif
4350 /* display setup */
4351 text_consoles_set_display(ds);
4353 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4354 exit(1);
4357 qdev_machine_creation_done();
4359 if (rom_load_all() != 0) {
4360 fprintf(stderr, "rom loading failed\n");
4361 exit(1);
4364 /* TODO: once all bus devices are qdevified, this should be done
4365 * when bus is created by qdev.c */
4366 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4367 qemu_run_machine_init_done_notifiers();
4369 qemu_system_reset(VMRESET_SILENT);
4370 if (loadvm) {
4371 if (load_vmstate(loadvm) < 0) {
4372 autostart = 0;
4376 if (incoming) {
4377 Error *local_err = NULL;
4378 qemu_start_incoming_migration(incoming, &local_err);
4379 if (local_err) {
4380 fprintf(stderr, "-incoming %s: %s\n", incoming, error_get_pretty(local_err));
4381 error_free(local_err);
4382 exit(1);
4384 } else if (autostart) {
4385 vm_start();
4388 os_setup_post();
4390 resume_all_vcpus();
4391 main_loop();
4392 bdrv_close_all();
4393 pause_all_vcpus();
4394 res_free();
4395 #ifdef CONFIG_TPM
4396 tpm_cleanup();
4397 #endif
4399 return 0;