Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / vl.c
blobc2af2cc2215d28117981c4414a006289a5a7ddcf
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 "qemu/osdep.h"
25 #include "qemu-version.h"
26 #include "qemu/cutils.h"
27 #include "qemu/help_option.h"
29 #ifdef CONFIG_SECCOMP
30 #include "sysemu/seccomp.h"
31 #endif
33 #if defined(CONFIG_VDE)
34 #include <libvdeplug.h>
35 #endif
37 #ifdef CONFIG_SDL
38 #if defined(__APPLE__) || defined(main)
39 #include <SDL.h>
40 int qemu_main(int argc, char **argv);
41 int main(int argc, char **argv)
43 return qemu_main(argc, argv);
45 #undef main
46 #define main qemu_main
47 #endif
48 #endif /* CONFIG_SDL */
50 #ifdef CONFIG_COCOA
51 #undef main
52 #define main qemu_main
53 #endif /* CONFIG_COCOA */
56 #include "qemu/error-report.h"
57 #include "qemu/sockets.h"
58 #include "hw/hw.h"
59 #include "hw/boards.h"
60 #include "sysemu/accel.h"
61 #include "hw/usb.h"
62 #include "hw/i386/pc.h"
63 #include "hw/isa/isa.h"
64 #include "hw/bt.h"
65 #include "sysemu/watchdog.h"
66 #include "hw/smbios/smbios.h"
67 #include "hw/xen/xen.h"
68 #include "hw/qdev.h"
69 #include "hw/loader.h"
70 #include "monitor/qdev.h"
71 #include "sysemu/bt.h"
72 #include "net/net.h"
73 #include "net/slirp.h"
74 #include "monitor/monitor.h"
75 #include "ui/console.h"
76 #include "ui/input.h"
77 #include "sysemu/sysemu.h"
78 #include "sysemu/numa.h"
79 #include "exec/gdbstub.h"
80 #include "qemu/timer.h"
81 #include "sysemu/char.h"
82 #include "qemu/bitmap.h"
83 #include "qemu/log.h"
84 #include "sysemu/blockdev.h"
85 #include "hw/block/block.h"
86 #include "migration/block.h"
87 #include "sysemu/tpm.h"
88 #include "sysemu/dma.h"
89 #include "audio/audio.h"
90 #include "migration/migration.h"
91 #include "sysemu/cpus.h"
92 #include "sysemu/kvm.h"
93 #include "qapi/qmp/qjson.h"
94 #include "qemu/option.h"
95 #include "qemu/config-file.h"
96 #include "qemu-options.h"
97 #include "qmp-commands.h"
98 #include "qemu/main-loop.h"
99 #ifdef CONFIG_VIRTFS
100 #include "fsdev/qemu-fsdev.h"
101 #endif
102 #include "sysemu/qtest.h"
104 #include "disas/disas.h"
107 #include "slirp/libslirp.h"
109 #include "trace.h"
110 #include "trace/control.h"
111 #include "qemu/queue.h"
112 #include "sysemu/cpus.h"
113 #include "sysemu/arch_init.h"
115 #include "ui/qemu-spice.h"
116 #include "qapi/string-input-visitor.h"
117 #include "qapi/opts-visitor.h"
118 #include "qom/object_interfaces.h"
119 #include "qapi-event.h"
120 #include "exec/semihost.h"
121 #include "crypto/init.h"
122 #include "sysemu/replay.h"
123 #include "qapi/qmp/qerror.h"
125 #define MAX_VIRTIO_CONSOLES 1
126 #define MAX_SCLP_CONSOLES 1
128 static const char *data_dir[16];
129 static int data_dir_idx;
130 const char *bios_name = NULL;
131 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
132 int request_opengl = -1;
133 int display_opengl;
134 const char* keyboard_layout = NULL;
135 ram_addr_t ram_size;
136 const char *mem_path = NULL;
137 int mem_prealloc = 0; /* force preallocation of physical target memory */
138 bool enable_mlock = false;
139 int nb_nics;
140 NICInfo nd_table[MAX_NICS];
141 int autostart;
142 static int rtc_utc = 1;
143 static int rtc_date_offset = -1; /* -1 means no change */
144 QEMUClockType rtc_clock;
145 int vga_interface_type = VGA_NONE;
146 static int full_screen = 0;
147 static int no_frame = 0;
148 int no_quit = 0;
149 static bool grab_on_hover;
150 CharDriverState *serial_hds[MAX_SERIAL_PORTS];
151 CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
152 CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
153 CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
154 int win2k_install_hack = 0;
155 int singlestep = 0;
156 int smp_cpus = 1;
157 int max_cpus = 1;
158 int smp_cores = 1;
159 int smp_threads = 1;
160 int acpi_enabled = 1;
161 int no_hpet = 0;
162 int fd_bootchk = 1;
163 static int no_reboot;
164 int no_shutdown = 0;
165 int cursor_hide = 1;
166 int graphic_rotate = 0;
167 const char *watchdog;
168 QEMUOptionRom option_rom[MAX_OPTION_ROMS];
169 int nb_option_roms;
170 int old_param = 0;
171 const char *qemu_name;
172 int alt_grab = 0;
173 int ctrl_grab = 0;
174 unsigned int nb_prom_envs = 0;
175 const char *prom_envs[MAX_PROM_ENVS];
176 int boot_menu;
177 bool boot_strict;
178 uint8_t *boot_splash_filedata;
179 size_t boot_splash_filedata_size;
180 uint8_t qemu_extra_params_fw[2];
182 int icount_align_option;
184 /* The bytes in qemu_uuid[] are in the order specified by RFC4122, _not_ in the
185 * little-endian "wire format" described in the SMBIOS 2.6 specification.
187 uint8_t qemu_uuid[16];
188 bool qemu_uuid_set;
190 /* Trace unassigned memory or i/o accesses. */
191 bool trace_unassigned;
193 static NotifierList exit_notifiers =
194 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
196 static NotifierList machine_init_done_notifiers =
197 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
199 bool xen_allowed;
200 uint32_t xen_domid;
201 enum xen_mode xen_mode = XEN_EMULATE;
203 static int has_defaults = 1;
204 static int default_serial = 1;
205 static int default_parallel = 1;
206 static int default_virtcon = 1;
207 static int default_sclp = 1;
208 static int default_monitor = 1;
209 static int default_floppy = 1;
210 static int default_cdrom = 1;
211 static int default_sdcard = 1;
212 static int default_vga = 1;
213 static int default_net = 1;
215 static struct {
216 const char *driver;
217 int *flag;
218 } default_list[] = {
219 { .driver = "isa-serial", .flag = &default_serial },
220 { .driver = "isa-parallel", .flag = &default_parallel },
221 { .driver = "isa-fdc", .flag = &default_floppy },
222 { .driver = "ide-cd", .flag = &default_cdrom },
223 { .driver = "ide-hd", .flag = &default_cdrom },
224 { .driver = "ide-drive", .flag = &default_cdrom },
225 { .driver = "scsi-cd", .flag = &default_cdrom },
226 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
227 { .driver = "virtio-serial", .flag = &default_virtcon },
228 { .driver = "VGA", .flag = &default_vga },
229 { .driver = "isa-vga", .flag = &default_vga },
230 { .driver = "cirrus-vga", .flag = &default_vga },
231 { .driver = "isa-cirrus-vga", .flag = &default_vga },
232 { .driver = "vmware-svga", .flag = &default_vga },
233 { .driver = "qxl-vga", .flag = &default_vga },
234 { .driver = "virtio-vga", .flag = &default_vga },
237 static QemuOptsList qemu_rtc_opts = {
238 .name = "rtc",
239 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
240 .desc = {
242 .name = "base",
243 .type = QEMU_OPT_STRING,
245 .name = "clock",
246 .type = QEMU_OPT_STRING,
248 .name = "driftfix",
249 .type = QEMU_OPT_STRING,
251 { /* end of list */ }
255 static QemuOptsList qemu_sandbox_opts = {
256 .name = "sandbox",
257 .implied_opt_name = "enable",
258 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
259 .desc = {
261 .name = "enable",
262 .type = QEMU_OPT_BOOL,
264 { /* end of list */ }
268 static QemuOptsList qemu_option_rom_opts = {
269 .name = "option-rom",
270 .implied_opt_name = "romfile",
271 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
272 .desc = {
274 .name = "bootindex",
275 .type = QEMU_OPT_NUMBER,
276 }, {
277 .name = "romfile",
278 .type = QEMU_OPT_STRING,
280 { /* end of list */ }
284 static QemuOptsList qemu_machine_opts = {
285 .name = "machine",
286 .implied_opt_name = "type",
287 .merge_lists = true,
288 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
289 .desc = {
291 * no elements => accept any
292 * sanity checking will happen later
293 * when setting machine properties
299 static QemuOptsList qemu_boot_opts = {
300 .name = "boot-opts",
301 .implied_opt_name = "order",
302 .merge_lists = true,
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
304 .desc = {
306 .name = "order",
307 .type = QEMU_OPT_STRING,
308 }, {
309 .name = "once",
310 .type = QEMU_OPT_STRING,
311 }, {
312 .name = "menu",
313 .type = QEMU_OPT_BOOL,
314 }, {
315 .name = "splash",
316 .type = QEMU_OPT_STRING,
317 }, {
318 .name = "splash-time",
319 .type = QEMU_OPT_STRING,
320 }, {
321 .name = "reboot-timeout",
322 .type = QEMU_OPT_STRING,
323 }, {
324 .name = "strict",
325 .type = QEMU_OPT_BOOL,
327 { /*End of list */ }
331 static QemuOptsList qemu_add_fd_opts = {
332 .name = "add-fd",
333 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
334 .desc = {
336 .name = "fd",
337 .type = QEMU_OPT_NUMBER,
338 .help = "file descriptor of which a duplicate is added to fd set",
340 .name = "set",
341 .type = QEMU_OPT_NUMBER,
342 .help = "ID of the fd set to add fd to",
344 .name = "opaque",
345 .type = QEMU_OPT_STRING,
346 .help = "free-form string used to describe fd",
348 { /* end of list */ }
352 static QemuOptsList qemu_object_opts = {
353 .name = "object",
354 .implied_opt_name = "qom-type",
355 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
356 .desc = {
361 static QemuOptsList qemu_tpmdev_opts = {
362 .name = "tpmdev",
363 .implied_opt_name = "type",
364 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
365 .desc = {
366 /* options are defined in the TPM backends */
367 { /* end of list */ }
371 static QemuOptsList qemu_realtime_opts = {
372 .name = "realtime",
373 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
374 .desc = {
376 .name = "mlock",
377 .type = QEMU_OPT_BOOL,
379 { /* end of list */ }
383 static QemuOptsList qemu_msg_opts = {
384 .name = "msg",
385 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
386 .desc = {
388 .name = "timestamp",
389 .type = QEMU_OPT_BOOL,
391 { /* end of list */ }
395 static QemuOptsList qemu_name_opts = {
396 .name = "name",
397 .implied_opt_name = "guest",
398 .merge_lists = true,
399 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
400 .desc = {
402 .name = "guest",
403 .type = QEMU_OPT_STRING,
404 .help = "Sets the name of the guest.\n"
405 "This name will be displayed in the SDL window caption.\n"
406 "The name will also be used for the VNC server",
407 }, {
408 .name = "process",
409 .type = QEMU_OPT_STRING,
410 .help = "Sets the name of the QEMU process, as shown in top etc",
411 }, {
412 .name = "debug-threads",
413 .type = QEMU_OPT_BOOL,
414 .help = "When enabled, name the individual threads; defaults off.\n"
415 "NOTE: The thread names are for debugging and not a\n"
416 "stable API.",
418 { /* End of list */ }
422 static QemuOptsList qemu_mem_opts = {
423 .name = "memory",
424 .implied_opt_name = "size",
425 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
426 .merge_lists = true,
427 .desc = {
429 .name = "size",
430 .type = QEMU_OPT_SIZE,
433 .name = "slots",
434 .type = QEMU_OPT_NUMBER,
437 .name = "maxmem",
438 .type = QEMU_OPT_SIZE,
440 { /* end of list */ }
444 static QemuOptsList qemu_icount_opts = {
445 .name = "icount",
446 .implied_opt_name = "shift",
447 .merge_lists = true,
448 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
449 .desc = {
451 .name = "shift",
452 .type = QEMU_OPT_STRING,
453 }, {
454 .name = "align",
455 .type = QEMU_OPT_BOOL,
456 }, {
457 .name = "sleep",
458 .type = QEMU_OPT_BOOL,
459 }, {
460 .name = "rr",
461 .type = QEMU_OPT_STRING,
462 }, {
463 .name = "rrfile",
464 .type = QEMU_OPT_STRING,
466 { /* end of list */ }
470 static QemuOptsList qemu_semihosting_config_opts = {
471 .name = "semihosting-config",
472 .implied_opt_name = "enable",
473 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
474 .desc = {
476 .name = "enable",
477 .type = QEMU_OPT_BOOL,
478 }, {
479 .name = "target",
480 .type = QEMU_OPT_STRING,
481 }, {
482 .name = "arg",
483 .type = QEMU_OPT_STRING,
485 { /* end of list */ }
489 static QemuOptsList qemu_fw_cfg_opts = {
490 .name = "fw_cfg",
491 .implied_opt_name = "name",
492 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
493 .desc = {
495 .name = "name",
496 .type = QEMU_OPT_STRING,
497 .help = "Sets the fw_cfg name of the blob to be inserted",
498 }, {
499 .name = "file",
500 .type = QEMU_OPT_STRING,
501 .help = "Sets the name of the file from which\n"
502 "the fw_cfg blob will be loaded",
503 }, {
504 .name = "string",
505 .type = QEMU_OPT_STRING,
506 .help = "Sets content of the blob to be inserted from a string",
508 { /* end of list */ }
513 * Get machine options
515 * Returns: machine options (never null).
517 QemuOpts *qemu_get_machine_opts(void)
519 return qemu_find_opts_singleton("machine");
522 const char *qemu_get_vm_name(void)
524 return qemu_name;
527 static void res_free(void)
529 g_free(boot_splash_filedata);
530 boot_splash_filedata = NULL;
533 static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
535 const char *driver = qemu_opt_get(opts, "driver");
536 int i;
538 if (!driver)
539 return 0;
540 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
541 if (strcmp(default_list[i].driver, driver) != 0)
542 continue;
543 *(default_list[i].flag) = 0;
545 return 0;
548 /***********************************************************/
549 /* QEMU state */
551 static RunState current_run_state = RUN_STATE_PRELAUNCH;
553 /* We use RUN_STATE__MAX but any invalid value will do */
554 static RunState vmstop_requested = RUN_STATE__MAX;
555 static QemuMutex vmstop_lock;
557 typedef struct {
558 RunState from;
559 RunState to;
560 } RunStateTransition;
562 static const RunStateTransition runstate_transitions_def[] = {
563 /* from -> to */
564 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
565 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
566 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
568 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
569 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
570 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
571 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
572 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
573 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
574 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
575 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
576 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
577 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
578 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
580 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
581 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
582 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
584 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
585 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
586 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
588 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
589 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
590 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
592 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
593 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
594 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
596 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
597 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
598 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
600 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
601 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
602 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
604 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
605 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
607 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
608 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
609 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
610 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
611 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
612 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
613 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
614 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
615 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
616 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
618 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
620 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
621 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
622 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
624 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
625 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
626 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
627 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
628 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
630 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
631 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
632 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
634 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
635 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
636 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
638 { RUN_STATE__MAX, RUN_STATE__MAX },
641 static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
643 bool runstate_check(RunState state)
645 return current_run_state == state;
648 bool runstate_store(char *str, size_t size)
650 const char *state = RunState_lookup[current_run_state];
651 size_t len = strlen(state) + 1;
653 if (len > size) {
654 return false;
656 memcpy(str, state, len);
657 return true;
660 static void runstate_init(void)
662 const RunStateTransition *p;
664 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
665 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
666 runstate_valid_transitions[p->from][p->to] = true;
669 qemu_mutex_init(&vmstop_lock);
672 /* This function will abort() on invalid state transitions */
673 void runstate_set(RunState new_state)
675 assert(new_state < RUN_STATE__MAX);
677 if (current_run_state == new_state) {
678 return;
681 if (!runstate_valid_transitions[current_run_state][new_state]) {
682 error_report("invalid runstate transition: '%s' -> '%s'",
683 RunState_lookup[current_run_state],
684 RunState_lookup[new_state]);
685 abort();
687 trace_runstate_set(new_state);
688 current_run_state = new_state;
691 int runstate_is_running(void)
693 return runstate_check(RUN_STATE_RUNNING);
696 bool runstate_needs_reset(void)
698 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
699 runstate_check(RUN_STATE_SHUTDOWN);
702 StatusInfo *qmp_query_status(Error **errp)
704 StatusInfo *info = g_malloc0(sizeof(*info));
706 info->running = runstate_is_running();
707 info->singlestep = singlestep;
708 info->status = current_run_state;
710 return info;
713 static bool qemu_vmstop_requested(RunState *r)
715 qemu_mutex_lock(&vmstop_lock);
716 *r = vmstop_requested;
717 vmstop_requested = RUN_STATE__MAX;
718 qemu_mutex_unlock(&vmstop_lock);
719 return *r < RUN_STATE__MAX;
722 void qemu_system_vmstop_request_prepare(void)
724 qemu_mutex_lock(&vmstop_lock);
727 void qemu_system_vmstop_request(RunState state)
729 vmstop_requested = state;
730 qemu_mutex_unlock(&vmstop_lock);
731 qemu_notify_event();
734 void vm_start(void)
736 RunState requested;
738 qemu_vmstop_requested(&requested);
739 if (runstate_is_running() && requested == RUN_STATE__MAX) {
740 return;
743 /* Ensure that a STOP/RESUME pair of events is emitted if a
744 * vmstop request was pending. The BLOCK_IO_ERROR event, for
745 * example, according to documentation is always followed by
746 * the STOP event.
748 if (runstate_is_running()) {
749 qapi_event_send_stop(&error_abort);
750 } else {
751 cpu_enable_ticks();
752 runstate_set(RUN_STATE_RUNNING);
753 vm_state_notify(1, RUN_STATE_RUNNING);
754 resume_all_vcpus();
757 qapi_event_send_resume(&error_abort);
761 /***********************************************************/
762 /* real time host monotonic timer */
764 static time_t qemu_time(void)
766 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
769 /***********************************************************/
770 /* host time/date access */
771 void qemu_get_timedate(struct tm *tm, int offset)
773 time_t ti = qemu_time();
775 ti += offset;
776 if (rtc_date_offset == -1) {
777 if (rtc_utc)
778 gmtime_r(&ti, tm);
779 else
780 localtime_r(&ti, tm);
781 } else {
782 ti -= rtc_date_offset;
783 gmtime_r(&ti, tm);
787 int qemu_timedate_diff(struct tm *tm)
789 time_t seconds;
791 if (rtc_date_offset == -1)
792 if (rtc_utc)
793 seconds = mktimegm(tm);
794 else {
795 struct tm tmp = *tm;
796 tmp.tm_isdst = -1; /* use timezone to figure it out */
797 seconds = mktime(&tmp);
799 else
800 seconds = mktimegm(tm) + rtc_date_offset;
802 return seconds - qemu_time();
805 static void configure_rtc_date_offset(const char *startdate, int legacy)
807 time_t rtc_start_date;
808 struct tm tm;
810 if (!strcmp(startdate, "now") && legacy) {
811 rtc_date_offset = -1;
812 } else {
813 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
814 &tm.tm_year,
815 &tm.tm_mon,
816 &tm.tm_mday,
817 &tm.tm_hour,
818 &tm.tm_min,
819 &tm.tm_sec) == 6) {
820 /* OK */
821 } else if (sscanf(startdate, "%d-%d-%d",
822 &tm.tm_year,
823 &tm.tm_mon,
824 &tm.tm_mday) == 3) {
825 tm.tm_hour = 0;
826 tm.tm_min = 0;
827 tm.tm_sec = 0;
828 } else {
829 goto date_fail;
831 tm.tm_year -= 1900;
832 tm.tm_mon--;
833 rtc_start_date = mktimegm(&tm);
834 if (rtc_start_date == -1) {
835 date_fail:
836 error_report("invalid date format");
837 error_printf("valid formats: "
838 "'2006-06-17T16:01:21' or '2006-06-17'\n");
839 exit(1);
841 rtc_date_offset = qemu_time() - rtc_start_date;
845 static void configure_rtc(QemuOpts *opts)
847 const char *value;
849 value = qemu_opt_get(opts, "base");
850 if (value) {
851 if (!strcmp(value, "utc")) {
852 rtc_utc = 1;
853 } else if (!strcmp(value, "localtime")) {
854 Error *blocker = NULL;
855 rtc_utc = 0;
856 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
857 "-rtc base=localtime");
858 replay_add_blocker(blocker);
859 } else {
860 configure_rtc_date_offset(value, 0);
863 value = qemu_opt_get(opts, "clock");
864 if (value) {
865 if (!strcmp(value, "host")) {
866 rtc_clock = QEMU_CLOCK_HOST;
867 } else if (!strcmp(value, "rt")) {
868 rtc_clock = QEMU_CLOCK_REALTIME;
869 } else if (!strcmp(value, "vm")) {
870 rtc_clock = QEMU_CLOCK_VIRTUAL;
871 } else {
872 error_report("invalid option value '%s'", value);
873 exit(1);
876 value = qemu_opt_get(opts, "driftfix");
877 if (value) {
878 if (!strcmp(value, "slew")) {
879 static GlobalProperty slew_lost_ticks = {
880 .driver = "mc146818rtc",
881 .property = "lost_tick_policy",
882 .value = "slew",
885 qdev_prop_register_global(&slew_lost_ticks);
886 } else if (!strcmp(value, "none")) {
887 /* discard is default */
888 } else {
889 error_report("invalid option value '%s'", value);
890 exit(1);
895 /***********************************************************/
896 /* Bluetooth support */
897 static int nb_hcis;
898 static int cur_hci;
899 static struct HCIInfo *hci_table[MAX_NICS];
901 struct HCIInfo *qemu_next_hci(void)
903 if (cur_hci == nb_hcis)
904 return &null_hci;
906 return hci_table[cur_hci++];
909 static int bt_hci_parse(const char *str)
911 struct HCIInfo *hci;
912 bdaddr_t bdaddr;
914 if (nb_hcis >= MAX_NICS) {
915 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
916 return -1;
919 hci = hci_init(str);
920 if (!hci)
921 return -1;
923 bdaddr.b[0] = 0x52;
924 bdaddr.b[1] = 0x54;
925 bdaddr.b[2] = 0x00;
926 bdaddr.b[3] = 0x12;
927 bdaddr.b[4] = 0x34;
928 bdaddr.b[5] = 0x56 + nb_hcis;
929 hci->bdaddr_set(hci, bdaddr.b);
931 hci_table[nb_hcis++] = hci;
933 return 0;
936 static void bt_vhci_add(int vlan_id)
938 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
940 if (!vlan->slave)
941 error_report("warning: adding a VHCI to an empty scatternet %i",
942 vlan_id);
944 bt_vhci_init(bt_new_hci(vlan));
947 static struct bt_device_s *bt_device_add(const char *opt)
949 struct bt_scatternet_s *vlan;
950 int vlan_id = 0;
951 char *endp = strstr(opt, ",vlan=");
952 int len = (endp ? endp - opt : strlen(opt)) + 1;
953 char devname[10];
955 pstrcpy(devname, MIN(sizeof(devname), len), opt);
957 if (endp) {
958 vlan_id = strtol(endp + 6, &endp, 0);
959 if (*endp) {
960 error_report("unrecognised bluetooth vlan Id");
961 return 0;
965 vlan = qemu_find_bt_vlan(vlan_id);
967 if (!vlan->slave)
968 error_report("warning: adding a slave device to an empty scatternet %i",
969 vlan_id);
971 if (!strcmp(devname, "keyboard"))
972 return bt_keyboard_init(vlan);
974 error_report("unsupported bluetooth device '%s'", devname);
975 return 0;
978 static int bt_parse(const char *opt)
980 const char *endp, *p;
981 int vlan;
983 if (strstart(opt, "hci", &endp)) {
984 if (!*endp || *endp == ',') {
985 if (*endp)
986 if (!strstart(endp, ",vlan=", 0))
987 opt = endp + 1;
989 return bt_hci_parse(opt);
991 } else if (strstart(opt, "vhci", &endp)) {
992 if (!*endp || *endp == ',') {
993 if (*endp) {
994 if (strstart(endp, ",vlan=", &p)) {
995 vlan = strtol(p, (char **) &endp, 0);
996 if (*endp) {
997 error_report("bad scatternet '%s'", p);
998 return 1;
1000 } else {
1001 error_report("bad parameter '%s'", endp + 1);
1002 return 1;
1004 } else
1005 vlan = 0;
1007 bt_vhci_add(vlan);
1008 return 0;
1010 } else if (strstart(opt, "device:", &endp))
1011 return !bt_device_add(endp);
1013 error_report("bad bluetooth parameter '%s'", opt);
1014 return 1;
1017 static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
1019 /* FIXME: change this to true for 1.3 */
1020 if (qemu_opt_get_bool(opts, "enable", false)) {
1021 #ifdef CONFIG_SECCOMP
1022 if (seccomp_start() < 0) {
1023 error_report("failed to install seccomp syscall filter "
1024 "in the kernel");
1025 return -1;
1027 #else
1028 error_report("seccomp support is disabled");
1029 return -1;
1030 #endif
1033 return 0;
1036 static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
1038 const char *proc_name;
1040 if (qemu_opt_get(opts, "debug-threads")) {
1041 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1043 qemu_name = qemu_opt_get(opts, "guest");
1045 proc_name = qemu_opt_get(opts, "process");
1046 if (proc_name) {
1047 os_set_proc_name(proc_name);
1050 return 0;
1053 bool defaults_enabled(void)
1055 return has_defaults;
1058 #ifndef _WIN32
1059 static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1061 int fd, dupfd, flags;
1062 int64_t fdset_id;
1063 const char *fd_opaque = NULL;
1064 AddfdInfo *fdinfo;
1066 fd = qemu_opt_get_number(opts, "fd", -1);
1067 fdset_id = qemu_opt_get_number(opts, "set", -1);
1068 fd_opaque = qemu_opt_get(opts, "opaque");
1070 if (fd < 0) {
1071 error_report("fd option is required and must be non-negative");
1072 return -1;
1075 if (fd <= STDERR_FILENO) {
1076 error_report("fd cannot be a standard I/O stream");
1077 return -1;
1081 * All fds inherited across exec() necessarily have FD_CLOEXEC
1082 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1084 flags = fcntl(fd, F_GETFD);
1085 if (flags == -1 || (flags & FD_CLOEXEC)) {
1086 error_report("fd is not valid or already in use");
1087 return -1;
1090 if (fdset_id < 0) {
1091 error_report("set option is required and must be non-negative");
1092 return -1;
1095 #ifdef F_DUPFD_CLOEXEC
1096 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1097 #else
1098 dupfd = dup(fd);
1099 if (dupfd != -1) {
1100 qemu_set_cloexec(dupfd);
1102 #endif
1103 if (dupfd == -1) {
1104 error_report("error duplicating fd: %s", strerror(errno));
1105 return -1;
1108 /* add the duplicate fd, and optionally the opaque string, to the fd set */
1109 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1110 &error_abort);
1111 g_free(fdinfo);
1113 return 0;
1116 static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
1118 int fd;
1120 fd = qemu_opt_get_number(opts, "fd", -1);
1121 close(fd);
1123 return 0;
1125 #endif
1127 /***********************************************************/
1128 /* QEMU Block devices */
1130 #define HD_OPTS "media=disk"
1131 #define CDROM_OPTS "media=cdrom"
1132 #define FD_OPTS ""
1133 #define PFLASH_OPTS ""
1134 #define MTD_OPTS ""
1135 #define SD_OPTS ""
1137 static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
1139 BlockInterfaceType *block_default_type = opaque;
1141 return drive_new(opts, *block_default_type) == NULL;
1144 static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
1146 if (qemu_opt_get(opts, "snapshot") == NULL) {
1147 qemu_opt_set(opts, "snapshot", "on", &error_abort);
1149 return 0;
1152 static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1153 int index, const char *optstr)
1155 QemuOpts *opts;
1156 DriveInfo *dinfo;
1158 if (!enable || drive_get_by_index(type, index)) {
1159 return;
1162 opts = drive_add(type, index, NULL, optstr);
1163 if (snapshot) {
1164 drive_enable_snapshot(NULL, opts, NULL);
1167 dinfo = drive_new(opts, type);
1168 if (!dinfo) {
1169 exit(1);
1171 dinfo->is_default = true;
1175 static QemuOptsList qemu_smp_opts = {
1176 .name = "smp-opts",
1177 .implied_opt_name = "cpus",
1178 .merge_lists = true,
1179 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1180 .desc = {
1182 .name = "cpus",
1183 .type = QEMU_OPT_NUMBER,
1184 }, {
1185 .name = "sockets",
1186 .type = QEMU_OPT_NUMBER,
1187 }, {
1188 .name = "cores",
1189 .type = QEMU_OPT_NUMBER,
1190 }, {
1191 .name = "threads",
1192 .type = QEMU_OPT_NUMBER,
1193 }, {
1194 .name = "maxcpus",
1195 .type = QEMU_OPT_NUMBER,
1197 { /*End of list */ }
1201 static void smp_parse(QemuOpts *opts)
1203 if (opts) {
1204 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1205 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1206 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1207 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1209 /* compute missing values, prefer sockets over cores over threads */
1210 if (cpus == 0 || sockets == 0) {
1211 sockets = sockets > 0 ? sockets : 1;
1212 cores = cores > 0 ? cores : 1;
1213 threads = threads > 0 ? threads : 1;
1214 if (cpus == 0) {
1215 cpus = cores * threads * sockets;
1217 } else if (cores == 0) {
1218 threads = threads > 0 ? threads : 1;
1219 cores = cpus / (sockets * threads);
1220 cores = cores > 0 ? cores : 1;
1221 } else if (threads == 0) {
1222 threads = cpus / (cores * sockets);
1223 threads = threads > 0 ? threads : 1;
1224 } else if (sockets * cores * threads < cpus) {
1225 error_report("cpu topology: "
1226 "sockets (%u) * cores (%u) * threads (%u) < "
1227 "smp_cpus (%u)",
1228 sockets, cores, threads, cpus);
1229 exit(1);
1232 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
1234 if (max_cpus > MAX_CPUMASK_BITS) {
1235 error_report("unsupported number of maxcpus");
1236 exit(1);
1239 if (max_cpus < cpus) {
1240 error_report("maxcpus must be equal to or greater than smp");
1241 exit(1);
1244 if (sockets * cores * threads > max_cpus) {
1245 error_report("cpu topology: "
1246 "sockets (%u) * cores (%u) * threads (%u) > "
1247 "maxcpus (%u)",
1248 sockets, cores, threads, max_cpus);
1249 exit(1);
1252 smp_cpus = cpus;
1253 smp_cores = cores;
1254 smp_threads = threads;
1257 if (smp_cpus > 1) {
1258 Error *blocker = NULL;
1259 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1260 replay_add_blocker(blocker);
1264 static void realtime_init(void)
1266 if (enable_mlock) {
1267 if (os_mlock() < 0) {
1268 error_report("locking memory failed");
1269 exit(1);
1275 static void configure_msg(QemuOpts *opts)
1277 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1280 /***********************************************************/
1281 /* Semihosting */
1283 typedef struct SemihostingConfig {
1284 bool enabled;
1285 SemihostingTarget target;
1286 const char **argv;
1287 int argc;
1288 const char *cmdline; /* concatenated argv */
1289 } SemihostingConfig;
1291 static SemihostingConfig semihosting;
1293 bool semihosting_enabled(void)
1295 return semihosting.enabled;
1298 SemihostingTarget semihosting_get_target(void)
1300 return semihosting.target;
1303 const char *semihosting_get_arg(int i)
1305 if (i >= semihosting.argc) {
1306 return NULL;
1308 return semihosting.argv[i];
1311 int semihosting_get_argc(void)
1313 return semihosting.argc;
1316 const char *semihosting_get_cmdline(void)
1318 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1319 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1321 return semihosting.cmdline;
1324 static int add_semihosting_arg(void *opaque,
1325 const char *name, const char *val,
1326 Error **errp)
1328 SemihostingConfig *s = opaque;
1329 if (strcmp(name, "arg") == 0) {
1330 s->argc++;
1331 /* one extra element as g_strjoinv() expects NULL-terminated array */
1332 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1333 s->argv[s->argc - 1] = val;
1334 s->argv[s->argc] = NULL;
1336 return 0;
1339 /* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1340 static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1342 char *cmd_token;
1344 /* argv[0] */
1345 add_semihosting_arg(&semihosting, "arg", file, NULL);
1347 /* split -append and initialize argv[1..n] */
1348 cmd_token = strtok(g_strdup(cmd), " ");
1349 while (cmd_token) {
1350 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1351 cmd_token = strtok(NULL, " ");
1355 /* Now we still need this for compatibility with XEN. */
1356 bool has_igd_gfx_passthru;
1357 static void igd_gfx_passthru(void)
1359 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1362 /***********************************************************/
1363 /* USB devices */
1365 static int usb_device_add(const char *devname)
1367 USBDevice *dev = NULL;
1368 #ifndef CONFIG_LINUX
1369 const char *p;
1370 #endif
1372 if (!machine_usb(current_machine)) {
1373 return -1;
1376 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1377 dev = usbdevice_create(devname);
1378 if (dev)
1379 goto done;
1381 /* the other ones */
1382 #ifndef CONFIG_LINUX
1383 /* only the linux version is qdev-ified, usb-bsd still needs this */
1384 if (strstart(devname, "host:", &p)) {
1385 dev = usb_host_device_open(usb_bus_find(-1), p);
1387 #endif
1388 if (!dev)
1389 return -1;
1391 done:
1392 return 0;
1395 static int usb_device_del(const char *devname)
1397 int bus_num, addr;
1398 const char *p;
1400 if (strstart(devname, "host:", &p)) {
1401 return -1;
1404 if (!machine_usb(current_machine)) {
1405 return -1;
1408 p = strchr(devname, '.');
1409 if (!p)
1410 return -1;
1411 bus_num = strtoul(devname, NULL, 0);
1412 addr = strtoul(p + 1, NULL, 0);
1414 return usb_device_delete_addr(bus_num, addr);
1417 static int usb_parse(const char *cmdline)
1419 int r;
1420 r = usb_device_add(cmdline);
1421 if (r < 0) {
1422 error_report("could not add USB device '%s'", cmdline);
1424 return r;
1427 void hmp_usb_add(Monitor *mon, const QDict *qdict)
1429 const char *devname = qdict_get_str(qdict, "devname");
1430 if (usb_device_add(devname) < 0) {
1431 error_report("could not add USB device '%s'", devname);
1435 void hmp_usb_del(Monitor *mon, const QDict *qdict)
1437 const char *devname = qdict_get_str(qdict, "devname");
1438 if (usb_device_del(devname) < 0) {
1439 error_report("could not delete USB device '%s'", devname);
1443 /***********************************************************/
1444 /* machine registration */
1446 MachineState *current_machine;
1448 static MachineClass *find_machine(const char *name)
1450 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1451 MachineClass *mc = NULL;
1453 for (el = machines; el; el = el->next) {
1454 MachineClass *temp = el->data;
1456 if (!strcmp(temp->name, name)) {
1457 mc = temp;
1458 break;
1460 if (temp->alias &&
1461 !strcmp(temp->alias, name)) {
1462 mc = temp;
1463 break;
1467 g_slist_free(machines);
1468 return mc;
1471 MachineClass *find_default_machine(void)
1473 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1474 MachineClass *mc = NULL;
1476 for (el = machines; el; el = el->next) {
1477 MachineClass *temp = el->data;
1479 if (temp->is_default) {
1480 mc = temp;
1481 break;
1485 g_slist_free(machines);
1486 return mc;
1489 MachineInfoList *qmp_query_machines(Error **errp)
1491 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
1492 MachineInfoList *mach_list = NULL;
1494 for (el = machines; el; el = el->next) {
1495 MachineClass *mc = el->data;
1496 MachineInfoList *entry;
1497 MachineInfo *info;
1499 info = g_malloc0(sizeof(*info));
1500 if (mc->is_default) {
1501 info->has_is_default = true;
1502 info->is_default = true;
1505 if (mc->alias) {
1506 info->has_alias = true;
1507 info->alias = g_strdup(mc->alias);
1510 info->name = g_strdup(mc->name);
1511 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
1512 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
1514 entry = g_malloc0(sizeof(*entry));
1515 entry->value = info;
1516 entry->next = mach_list;
1517 mach_list = entry;
1520 g_slist_free(machines);
1521 return mach_list;
1524 static int machine_help_func(QemuOpts *opts, MachineState *machine)
1526 ObjectProperty *prop;
1527 ObjectPropertyIterator iter;
1529 if (!qemu_opt_has_help_opt(opts)) {
1530 return 0;
1533 object_property_iter_init(&iter, OBJECT(machine));
1534 while ((prop = object_property_iter_next(&iter))) {
1535 if (!prop->set) {
1536 continue;
1539 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1540 prop->name, prop->type);
1541 if (prop->description) {
1542 error_printf(" (%s)\n", prop->description);
1543 } else {
1544 error_printf("\n");
1548 return 1;
1551 /***********************************************************/
1552 /* main execution loop */
1554 struct vm_change_state_entry {
1555 VMChangeStateHandler *cb;
1556 void *opaque;
1557 QLIST_ENTRY (vm_change_state_entry) entries;
1560 static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
1562 VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1563 void *opaque)
1565 VMChangeStateEntry *e;
1567 e = g_malloc0(sizeof (*e));
1569 e->cb = cb;
1570 e->opaque = opaque;
1571 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
1572 return e;
1575 void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1577 QLIST_REMOVE (e, entries);
1578 g_free (e);
1581 void vm_state_notify(int running, RunState state)
1583 VMChangeStateEntry *e, *next;
1585 trace_vm_state_notify(running, state);
1587 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1588 e->cb(e->opaque, running, state);
1592 /* reset/shutdown handler */
1594 typedef struct QEMUResetEntry {
1595 QTAILQ_ENTRY(QEMUResetEntry) entry;
1596 QEMUResetHandler *func;
1597 void *opaque;
1598 } QEMUResetEntry;
1600 static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1601 QTAILQ_HEAD_INITIALIZER(reset_handlers);
1602 static int reset_requested;
1603 static int shutdown_requested, shutdown_signal = -1;
1604 static pid_t shutdown_pid;
1605 static int powerdown_requested;
1606 static int debug_requested;
1607 static int suspend_requested;
1608 static WakeupReason wakeup_reason;
1609 static NotifierList powerdown_notifiers =
1610 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
1611 static NotifierList suspend_notifiers =
1612 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1613 static NotifierList wakeup_notifiers =
1614 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
1615 static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
1617 int qemu_shutdown_requested_get(void)
1619 return shutdown_requested;
1622 int qemu_reset_requested_get(void)
1624 return reset_requested;
1627 static int qemu_shutdown_requested(void)
1629 return atomic_xchg(&shutdown_requested, 0);
1632 static void qemu_kill_report(void)
1634 if (!qtest_driver() && shutdown_signal != -1) {
1635 if (shutdown_pid == 0) {
1636 /* This happens for eg ^C at the terminal, so it's worth
1637 * avoiding printing an odd message in that case.
1639 error_report("terminating on signal %d", shutdown_signal);
1640 } else {
1641 error_report("terminating on signal %d from pid " FMT_pid,
1642 shutdown_signal, shutdown_pid);
1644 shutdown_signal = -1;
1648 static int qemu_reset_requested(void)
1650 int r = reset_requested;
1651 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1652 reset_requested = 0;
1653 return r;
1655 return false;
1658 static int qemu_suspend_requested(void)
1660 int r = suspend_requested;
1661 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1662 suspend_requested = 0;
1663 return r;
1665 return false;
1668 static WakeupReason qemu_wakeup_requested(void)
1670 return wakeup_reason;
1673 static int qemu_powerdown_requested(void)
1675 int r = powerdown_requested;
1676 powerdown_requested = 0;
1677 return r;
1680 static int qemu_debug_requested(void)
1682 int r = debug_requested;
1683 debug_requested = 0;
1684 return r;
1687 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1689 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
1691 re->func = func;
1692 re->opaque = opaque;
1693 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
1696 void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
1698 QEMUResetEntry *re;
1700 QTAILQ_FOREACH(re, &reset_handlers, entry) {
1701 if (re->func == func && re->opaque == opaque) {
1702 QTAILQ_REMOVE(&reset_handlers, re, entry);
1703 g_free(re);
1704 return;
1709 void qemu_devices_reset(void)
1711 QEMUResetEntry *re, *nre;
1713 /* reset all devices */
1714 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
1715 re->func(re->opaque);
1719 void qemu_system_reset(bool report)
1721 MachineClass *mc;
1723 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1725 cpu_synchronize_all_states();
1727 if (mc && mc->reset) {
1728 mc->reset();
1729 } else {
1730 qemu_devices_reset();
1732 if (report) {
1733 qapi_event_send_reset(&error_abort);
1735 cpu_synchronize_all_post_reset();
1738 void qemu_system_guest_panicked(void)
1740 if (current_cpu) {
1741 current_cpu->crash_occurred = true;
1743 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1744 vm_stop(RUN_STATE_GUEST_PANICKED);
1747 void qemu_system_reset_request(void)
1749 if (no_reboot) {
1750 shutdown_requested = 1;
1751 } else {
1752 reset_requested = 1;
1754 cpu_stop_current();
1755 qemu_notify_event();
1758 static void qemu_system_suspend(void)
1760 pause_all_vcpus();
1761 notifier_list_notify(&suspend_notifiers, NULL);
1762 runstate_set(RUN_STATE_SUSPENDED);
1763 qapi_event_send_suspend(&error_abort);
1766 void qemu_system_suspend_request(void)
1768 if (runstate_check(RUN_STATE_SUSPENDED)) {
1769 return;
1771 suspend_requested = 1;
1772 cpu_stop_current();
1773 qemu_notify_event();
1776 void qemu_register_suspend_notifier(Notifier *notifier)
1778 notifier_list_add(&suspend_notifiers, notifier);
1781 void qemu_system_wakeup_request(WakeupReason reason)
1783 trace_system_wakeup_request(reason);
1785 if (!runstate_check(RUN_STATE_SUSPENDED)) {
1786 return;
1788 if (!(wakeup_reason_mask & (1 << reason))) {
1789 return;
1791 runstate_set(RUN_STATE_RUNNING);
1792 wakeup_reason = reason;
1793 qemu_notify_event();
1796 void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1798 if (enabled) {
1799 wakeup_reason_mask |= (1 << reason);
1800 } else {
1801 wakeup_reason_mask &= ~(1 << reason);
1805 void qemu_register_wakeup_notifier(Notifier *notifier)
1807 notifier_list_add(&wakeup_notifiers, notifier);
1810 void qemu_system_killed(int signal, pid_t pid)
1812 shutdown_signal = signal;
1813 shutdown_pid = pid;
1814 no_shutdown = 0;
1816 /* Cannot call qemu_system_shutdown_request directly because
1817 * we are in a signal handler.
1819 shutdown_requested = 1;
1820 qemu_notify_event();
1823 void qemu_system_shutdown_request(void)
1825 trace_qemu_system_shutdown_request();
1826 replay_shutdown_request();
1827 shutdown_requested = 1;
1828 qemu_notify_event();
1831 static void qemu_system_powerdown(void)
1833 qapi_event_send_powerdown(&error_abort);
1834 notifier_list_notify(&powerdown_notifiers, NULL);
1837 void qemu_system_powerdown_request(void)
1839 trace_qemu_system_powerdown_request();
1840 powerdown_requested = 1;
1841 qemu_notify_event();
1844 void qemu_register_powerdown_notifier(Notifier *notifier)
1846 notifier_list_add(&powerdown_notifiers, notifier);
1849 void qemu_system_debug_request(void)
1851 debug_requested = 1;
1852 qemu_notify_event();
1855 static bool main_loop_should_exit(void)
1857 RunState r;
1858 if (qemu_debug_requested()) {
1859 vm_stop(RUN_STATE_DEBUG);
1861 if (qemu_suspend_requested()) {
1862 qemu_system_suspend();
1864 if (qemu_shutdown_requested()) {
1865 qemu_kill_report();
1866 qapi_event_send_shutdown(&error_abort);
1867 if (no_shutdown) {
1868 vm_stop(RUN_STATE_SHUTDOWN);
1869 } else {
1870 return true;
1873 if (qemu_reset_requested()) {
1874 pause_all_vcpus();
1875 qemu_system_reset(VMRESET_REPORT);
1876 resume_all_vcpus();
1877 if (!runstate_check(RUN_STATE_RUNNING) &&
1878 !runstate_check(RUN_STATE_INMIGRATE)) {
1879 runstate_set(RUN_STATE_PRELAUNCH);
1882 if (qemu_wakeup_requested()) {
1883 pause_all_vcpus();
1884 qemu_system_reset(VMRESET_SILENT);
1885 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1886 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
1887 resume_all_vcpus();
1888 qapi_event_send_wakeup(&error_abort);
1890 if (qemu_powerdown_requested()) {
1891 qemu_system_powerdown();
1893 if (qemu_vmstop_requested(&r)) {
1894 vm_stop(r);
1896 return false;
1899 static void main_loop(void)
1901 bool nonblocking;
1902 int last_io = 0;
1903 #ifdef CONFIG_PROFILER
1904 int64_t ti;
1905 #endif
1906 do {
1907 nonblocking = !kvm_enabled() && !xen_enabled() && last_io > 0;
1908 #ifdef CONFIG_PROFILER
1909 ti = profile_getclock();
1910 #endif
1911 last_io = main_loop_wait(nonblocking);
1912 #ifdef CONFIG_PROFILER
1913 dev_time += profile_getclock() - ti;
1914 #endif
1915 } while (!main_loop_should_exit());
1918 static void version(void)
1920 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", "
1921 QEMU_COPYRIGHT "\n");
1924 static void QEMU_NORETURN help(int exitcode)
1926 version();
1927 printf("usage: %s [options] [disk_image]\n\n"
1928 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1929 error_get_progname());
1931 #define QEMU_OPTIONS_GENERATE_HELP
1932 #include "qemu-options-wrapper.h"
1934 printf("\nDuring emulation, the following keys are useful:\n"
1935 "ctrl-alt-f toggle full screen\n"
1936 "ctrl-alt-n switch to virtual console 'n'\n"
1937 "ctrl-alt toggle mouse and keyboard grab\n"
1938 "\n"
1939 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1941 exit(exitcode);
1944 #define HAS_ARG 0x0001
1946 typedef struct QEMUOption {
1947 const char *name;
1948 int flags;
1949 int index;
1950 uint32_t arch_mask;
1951 } QEMUOption;
1953 static const QEMUOption qemu_options[] = {
1954 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1955 #define QEMU_OPTIONS_GENERATE_OPTIONS
1956 #include "qemu-options-wrapper.h"
1957 { NULL },
1960 typedef struct VGAInterfaceInfo {
1961 const char *opt_name; /* option name */
1962 const char *name; /* human-readable name */
1963 /* Class names indicating that support is available.
1964 * If no class is specified, the interface is always available */
1965 const char *class_names[2];
1966 } VGAInterfaceInfo;
1968 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1969 [VGA_NONE] = {
1970 .opt_name = "none",
1972 [VGA_STD] = {
1973 .opt_name = "std",
1974 .name = "standard VGA",
1975 .class_names = { "VGA", "isa-vga" },
1977 [VGA_CIRRUS] = {
1978 .opt_name = "cirrus",
1979 .name = "Cirrus VGA",
1980 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
1982 [VGA_VMWARE] = {
1983 .opt_name = "vmware",
1984 .name = "VMWare SVGA",
1985 .class_names = { "vmware-svga" },
1987 [VGA_VIRTIO] = {
1988 .opt_name = "virtio",
1989 .name = "Virtio VGA",
1990 .class_names = { "virtio-vga" },
1992 [VGA_QXL] = {
1993 .opt_name = "qxl",
1994 .name = "QXL VGA",
1995 .class_names = { "qxl-vga" },
1997 [VGA_TCX] = {
1998 .opt_name = "tcx",
1999 .name = "TCX framebuffer",
2000 .class_names = { "SUNW,tcx" },
2002 [VGA_CG3] = {
2003 .opt_name = "cg3",
2004 .name = "CG3 framebuffer",
2005 .class_names = { "cgthree" },
2007 [VGA_XENFB] = {
2008 .opt_name = "xenfb",
2012 static bool vga_interface_available(VGAInterfaceType t)
2014 VGAInterfaceInfo *ti = &vga_interfaces[t];
2016 assert(t < VGA_TYPE_MAX);
2017 return !ti->class_names[0] ||
2018 object_class_by_name(ti->class_names[0]) ||
2019 object_class_by_name(ti->class_names[1]);
2022 static void select_vgahw(const char *p)
2024 const char *opts;
2025 int t;
2027 assert(vga_interface_type == VGA_NONE);
2028 for (t = 0; t < VGA_TYPE_MAX; t++) {
2029 VGAInterfaceInfo *ti = &vga_interfaces[t];
2030 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
2031 if (!vga_interface_available(t)) {
2032 error_report("%s not available", ti->name);
2033 exit(1);
2035 vga_interface_type = t;
2036 break;
2039 if (t == VGA_TYPE_MAX) {
2040 invalid_vga:
2041 error_report("unknown vga type: %s", p);
2042 exit(1);
2044 while (*opts) {
2045 const char *nextopt;
2047 if (strstart(opts, ",retrace=", &nextopt)) {
2048 opts = nextopt;
2049 if (strstart(opts, "dumb", &nextopt))
2050 vga_retrace_method = VGA_RETRACE_DUMB;
2051 else if (strstart(opts, "precise", &nextopt))
2052 vga_retrace_method = VGA_RETRACE_PRECISE;
2053 else goto invalid_vga;
2054 } else goto invalid_vga;
2055 opts = nextopt;
2059 typedef enum DisplayType {
2060 DT_DEFAULT,
2061 DT_CURSES,
2062 DT_SDL,
2063 DT_COCOA,
2064 DT_GTK,
2065 DT_NONE,
2066 } DisplayType;
2068 static DisplayType select_display(const char *p)
2070 const char *opts;
2071 DisplayType display = DT_DEFAULT;
2073 if (strstart(p, "sdl", &opts)) {
2074 #ifdef CONFIG_SDL
2075 display = DT_SDL;
2076 while (*opts) {
2077 const char *nextopt;
2079 if (strstart(opts, ",frame=", &nextopt)) {
2080 opts = nextopt;
2081 if (strstart(opts, "on", &nextopt)) {
2082 no_frame = 0;
2083 } else if (strstart(opts, "off", &nextopt)) {
2084 no_frame = 1;
2085 } else {
2086 goto invalid_sdl_args;
2088 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2089 opts = nextopt;
2090 if (strstart(opts, "on", &nextopt)) {
2091 alt_grab = 1;
2092 } else if (strstart(opts, "off", &nextopt)) {
2093 alt_grab = 0;
2094 } else {
2095 goto invalid_sdl_args;
2097 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2098 opts = nextopt;
2099 if (strstart(opts, "on", &nextopt)) {
2100 ctrl_grab = 1;
2101 } else if (strstart(opts, "off", &nextopt)) {
2102 ctrl_grab = 0;
2103 } else {
2104 goto invalid_sdl_args;
2106 } else if (strstart(opts, ",window_close=", &nextopt)) {
2107 opts = nextopt;
2108 if (strstart(opts, "on", &nextopt)) {
2109 no_quit = 0;
2110 } else if (strstart(opts, "off", &nextopt)) {
2111 no_quit = 1;
2112 } else {
2113 goto invalid_sdl_args;
2115 } else if (strstart(opts, ",gl=", &nextopt)) {
2116 opts = nextopt;
2117 if (strstart(opts, "on", &nextopt)) {
2118 request_opengl = 1;
2119 } else if (strstart(opts, "off", &nextopt)) {
2120 request_opengl = 0;
2121 } else {
2122 goto invalid_sdl_args;
2124 } else {
2125 invalid_sdl_args:
2126 error_report("invalid SDL option string");
2127 exit(1);
2129 opts = nextopt;
2131 #else
2132 error_report("SDL support is disabled");
2133 exit(1);
2134 #endif
2135 } else if (strstart(p, "vnc", &opts)) {
2136 if (*opts == '=') {
2137 vnc_parse(opts + 1, &error_fatal);
2138 } else {
2139 error_report("VNC requires a display argument vnc=<display>");
2140 exit(1);
2142 } else if (strstart(p, "curses", &opts)) {
2143 #ifdef CONFIG_CURSES
2144 display = DT_CURSES;
2145 #else
2146 error_report("curses support is disabled");
2147 exit(1);
2148 #endif
2149 } else if (strstart(p, "gtk", &opts)) {
2150 #ifdef CONFIG_GTK
2151 display = DT_GTK;
2152 while (*opts) {
2153 const char *nextopt;
2155 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2156 opts = nextopt;
2157 if (strstart(opts, "on", &nextopt)) {
2158 grab_on_hover = true;
2159 } else if (strstart(opts, "off", &nextopt)) {
2160 grab_on_hover = false;
2161 } else {
2162 goto invalid_gtk_args;
2164 } else if (strstart(opts, ",gl=", &nextopt)) {
2165 opts = nextopt;
2166 if (strstart(opts, "on", &nextopt)) {
2167 request_opengl = 1;
2168 } else if (strstart(opts, "off", &nextopt)) {
2169 request_opengl = 0;
2170 } else {
2171 goto invalid_gtk_args;
2173 } else {
2174 invalid_gtk_args:
2175 error_report("invalid GTK option string");
2176 exit(1);
2178 opts = nextopt;
2180 #else
2181 error_report("GTK support is disabled");
2182 exit(1);
2183 #endif
2184 } else if (strstart(p, "none", &opts)) {
2185 display = DT_NONE;
2186 } else {
2187 error_report("unknown display type");
2188 exit(1);
2191 return display;
2194 static int balloon_parse(const char *arg)
2196 QemuOpts *opts;
2198 if (strcmp(arg, "none") == 0) {
2199 return 0;
2202 if (!strncmp(arg, "virtio", 6)) {
2203 if (arg[6] == ',') {
2204 /* have params -> parse them */
2205 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2206 false);
2207 if (!opts)
2208 return -1;
2209 } else {
2210 /* create empty opts */
2211 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2212 &error_abort);
2214 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
2215 return 0;
2218 return -1;
2221 #if defined(CONFIG_RUBY)
2222 /* Ruby interface for QEMU. See README.EXT for programming example. */
2223 #warning mit ruby
2224 #include <ruby.h>
2226 static VALUE qemu_open(int argc, VALUE *argv, VALUE klass)
2228 int i;
2229 for (i = 0; i < argc; i++) {
2230 argv[i] = StringValue(argv[i]);
2231 fprintf(stderr, "argv[%d] = %s\n", i, StringValuePtr(argv[i]));
2233 //~ if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
2234 //~ mode = 0666; /* default value */
2235 //~ }
2236 return INT2FIX(argc);
2239 void Init_qemu(void)
2241 printf("%s\n", __func__);
2242 VALUE cQEMU = rb_define_class("QEMU", rb_cObject);
2243 rb_define_singleton_method(cQEMU, "run", qemu_open, -1);
2244 #if 0
2245 rb_define_method();
2246 #endif
2248 #endif /* CONFIG_RUBY */
2250 #if defined(CONFIG_DLL)
2251 BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID data)
2253 return FALSE;
2255 #endif /* CONFIG_DLL */
2257 char *qemu_find_file(int type, const char *name)
2259 int i;
2260 const char *subdir;
2261 char *buf;
2263 /* Try the name as a straight path first */
2264 if (access(name, R_OK) == 0) {
2265 trace_load_file(name, name);
2266 return g_strdup(name);
2269 switch (type) {
2270 case QEMU_FILE_TYPE_BIOS:
2271 subdir = "";
2272 break;
2273 case QEMU_FILE_TYPE_KEYMAP:
2274 subdir = "keymaps/";
2275 break;
2276 default:
2277 abort();
2280 for (i = 0; i < data_dir_idx; i++) {
2281 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2282 if (access(buf, R_OK) == 0) {
2283 trace_load_file(name, buf);
2284 return buf;
2286 g_free(buf);
2288 return NULL;
2291 static inline bool nonempty_str(const char *str)
2293 return str && *str;
2296 static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2298 gchar *buf;
2299 size_t size;
2300 const char *name, *file, *str;
2301 FWCfgState *fw_cfg = (FWCfgState *) opaque;
2303 if (fw_cfg == NULL) {
2304 error_report("fw_cfg device not available");
2305 return -1;
2307 name = qemu_opt_get(opts, "name");
2308 file = qemu_opt_get(opts, "file");
2309 str = qemu_opt_get(opts, "string");
2311 /* we need name and either a file or the content string */
2312 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2313 error_report("invalid argument(s)");
2314 return -1;
2316 if (nonempty_str(file) && nonempty_str(str)) {
2317 error_report("file and string are mutually exclusive");
2318 return -1;
2320 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2321 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2322 return -1;
2324 if (strncmp(name, "opt/", 4) != 0) {
2325 error_report("warning: externally provided fw_cfg item names "
2326 "should be prefixed with \"opt/\"");
2328 if (nonempty_str(str)) {
2329 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2330 buf = g_memdup(str, size);
2331 } else {
2332 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2333 error_report("can't load %s", file);
2334 return -1;
2337 /* For legacy, keep user files in a specific global order. */
2338 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2339 fw_cfg_add_file(fw_cfg, name, buf, size);
2340 fw_cfg_reset_order_override(fw_cfg);
2341 return 0;
2344 static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
2346 return qdev_device_help(opts);
2349 static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
2351 Error *err = NULL;
2352 DeviceState *dev;
2354 dev = qdev_device_add(opts, &err);
2355 if (!dev) {
2356 error_report_err(err);
2357 return -1;
2359 object_unref(OBJECT(dev));
2360 return 0;
2363 static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2365 Error *local_err = NULL;
2367 qemu_chr_new_from_opts(opts, NULL, &local_err);
2368 if (local_err) {
2369 error_report_err(local_err);
2370 return -1;
2372 return 0;
2375 #ifdef CONFIG_VIRTFS
2376 static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
2378 return qemu_fsdev_add(opts);
2380 #endif
2382 static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
2384 CharDriverState *chr;
2385 const char *chardev;
2386 const char *mode;
2387 int flags;
2389 mode = qemu_opt_get(opts, "mode");
2390 if (mode == NULL) {
2391 mode = "readline";
2393 if (strcmp(mode, "readline") == 0) {
2394 flags = MONITOR_USE_READLINE;
2395 } else if (strcmp(mode, "control") == 0) {
2396 flags = MONITOR_USE_CONTROL;
2397 } else {
2398 error_report("unknown monitor mode \"%s\"", mode);
2399 exit(1);
2402 if (qemu_opt_get_bool(opts, "pretty", 0))
2403 flags |= MONITOR_USE_PRETTY;
2405 if (qemu_opt_get_bool(opts, "default", 0))
2406 flags |= MONITOR_IS_DEFAULT;
2408 chardev = qemu_opt_get(opts, "chardev");
2409 chr = qemu_chr_find(chardev);
2410 if (chr == NULL) {
2411 error_report("chardev \"%s\" not found", chardev);
2412 exit(1);
2415 qemu_chr_fe_claim_no_fail(chr);
2416 monitor_init(chr, flags);
2417 return 0;
2420 static void monitor_parse(const char *optarg, const char *mode, bool pretty)
2422 static int monitor_device_index = 0;
2423 QemuOpts *opts;
2424 const char *p;
2425 char label[32];
2426 int def = 0;
2428 if (strstart(optarg, "chardev:", &p)) {
2429 snprintf(label, sizeof(label), "%s", p);
2430 } else {
2431 snprintf(label, sizeof(label), "compat_monitor%d",
2432 monitor_device_index);
2433 if (monitor_device_index == 0) {
2434 def = 1;
2436 opts = qemu_chr_parse_compat(label, optarg);
2437 if (!opts) {
2438 error_report("parse error: %s", optarg);
2439 exit(1);
2443 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
2444 qemu_opt_set(opts, "mode", mode, &error_abort);
2445 qemu_opt_set(opts, "chardev", label, &error_abort);
2446 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
2447 if (def)
2448 qemu_opt_set(opts, "default", "on", &error_abort);
2449 monitor_device_index++;
2452 struct device_config {
2453 enum {
2454 DEV_USB, /* -usbdevice */
2455 DEV_BT, /* -bt */
2456 DEV_SERIAL, /* -serial */
2457 DEV_PARALLEL, /* -parallel */
2458 DEV_VIRTCON, /* -virtioconsole */
2459 DEV_DEBUGCON, /* -debugcon */
2460 DEV_GDB, /* -gdb, -s */
2461 DEV_SCLP, /* s390 sclp */
2462 } type;
2463 const char *cmdline;
2464 Location loc;
2465 QTAILQ_ENTRY(device_config) next;
2468 static QTAILQ_HEAD(, device_config) device_configs =
2469 QTAILQ_HEAD_INITIALIZER(device_configs);
2471 static void add_device_config(int type, const char *cmdline)
2473 struct device_config *conf;
2475 conf = g_malloc0(sizeof(*conf));
2476 conf->type = type;
2477 conf->cmdline = cmdline;
2478 loc_save(&conf->loc);
2479 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
2482 static int foreach_device_config(int type, int (*func)(const char *cmdline))
2484 struct device_config *conf;
2485 int rc;
2487 QTAILQ_FOREACH(conf, &device_configs, next) {
2488 if (conf->type != type)
2489 continue;
2490 loc_push_restore(&conf->loc);
2491 rc = func(conf->cmdline);
2492 loc_pop(&conf->loc);
2493 if (rc) {
2494 return rc;
2497 return 0;
2500 static int serial_parse(const char *devname)
2502 static int index = 0;
2503 char label[32];
2505 if (strcmp(devname, "none") == 0)
2506 return 0;
2507 if (index == MAX_SERIAL_PORTS) {
2508 error_report("too many serial ports");
2509 exit(1);
2511 snprintf(label, sizeof(label), "serial%d", index);
2512 serial_hds[index] = qemu_chr_new(label, devname, NULL);
2513 if (!serial_hds[index]) {
2514 error_report("could not connect serial device"
2515 " to character backend '%s'", devname);
2516 return -1;
2518 index++;
2519 return 0;
2522 static int parallel_parse(const char *devname)
2524 static int index = 0;
2525 char label[32];
2527 if (strcmp(devname, "none") == 0)
2528 return 0;
2529 if (index == MAX_PARALLEL_PORTS) {
2530 error_report("too many parallel ports");
2531 exit(1);
2533 snprintf(label, sizeof(label), "parallel%d", index);
2534 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
2535 if (!parallel_hds[index]) {
2536 error_report("could not connect parallel device"
2537 " to character backend '%s'", devname);
2538 return -1;
2540 index++;
2541 return 0;
2544 static int virtcon_parse(const char *devname)
2546 QemuOptsList *device = qemu_find_opts("device");
2547 static int index = 0;
2548 char label[32];
2549 QemuOpts *bus_opts, *dev_opts;
2551 if (strcmp(devname, "none") == 0)
2552 return 0;
2553 if (index == MAX_VIRTIO_CONSOLES) {
2554 error_report("too many virtio consoles");
2555 exit(1);
2558 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2559 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
2561 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
2562 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
2564 snprintf(label, sizeof(label), "virtcon%d", index);
2565 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
2566 if (!virtcon_hds[index]) {
2567 error_report("could not connect virtio console"
2568 " to character backend '%s'", devname);
2569 return -1;
2571 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2573 index++;
2574 return 0;
2577 static int sclp_parse(const char *devname)
2579 QemuOptsList *device = qemu_find_opts("device");
2580 static int index = 0;
2581 char label[32];
2582 QemuOpts *dev_opts;
2584 if (strcmp(devname, "none") == 0) {
2585 return 0;
2587 if (index == MAX_SCLP_CONSOLES) {
2588 error_report("too many sclp consoles");
2589 exit(1);
2592 assert(arch_type == QEMU_ARCH_S390X);
2594 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
2595 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
2597 snprintf(label, sizeof(label), "sclpcon%d", index);
2598 sclp_hds[index] = qemu_chr_new(label, devname, NULL);
2599 if (!sclp_hds[index]) {
2600 error_report("could not connect sclp console"
2601 " to character backend '%s'", devname);
2602 return -1;
2604 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
2606 index++;
2607 return 0;
2610 static int debugcon_parse(const char *devname)
2612 QemuOpts *opts;
2614 if (!qemu_chr_new("debugcon", devname, NULL)) {
2615 exit(1);
2617 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
2618 if (!opts) {
2619 error_report("already have a debugcon device");
2620 exit(1);
2622 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2623 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
2624 return 0;
2627 static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2629 const MachineClass *mc1 = a, *mc2 = b;
2630 int res;
2632 if (mc1->family == NULL) {
2633 if (mc2->family == NULL) {
2634 /* Compare standalone machine types against each other; they sort
2635 * in increasing order.
2637 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2638 object_class_get_name(OBJECT_CLASS(mc2)));
2641 /* Standalone machine types sort after families. */
2642 return 1;
2645 if (mc2->family == NULL) {
2646 /* Families sort before standalone machine types. */
2647 return -1;
2650 /* Families sort between each other alphabetically increasingly. */
2651 res = strcmp(mc1->family, mc2->family);
2652 if (res != 0) {
2653 return res;
2656 /* Within the same family, machine types sort in decreasing order. */
2657 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2658 object_class_get_name(OBJECT_CLASS(mc1)));
2661 static MachineClass *machine_parse(const char *name)
2663 MachineClass *mc = NULL;
2664 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
2666 if (name) {
2667 mc = find_machine(name);
2669 if (mc) {
2670 g_slist_free(machines);
2671 return mc;
2673 if (name && !is_help_option(name)) {
2674 error_report("unsupported machine type");
2675 error_printf("Use -machine help to list supported machines\n");
2676 } else {
2677 printf("Supported machines are:\n");
2678 machines = g_slist_sort(machines, machine_class_cmp);
2679 for (el = machines; el; el = el->next) {
2680 MachineClass *mc = el->data;
2681 if (mc->alias) {
2682 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
2684 printf("%-20s %s%s\n", mc->name, mc->desc,
2685 mc->is_default ? " (default)" : "");
2689 g_slist_free(machines);
2690 exit(!name || !is_help_option(name));
2693 void qemu_add_exit_notifier(Notifier *notify)
2695 notifier_list_add(&exit_notifiers, notify);
2698 void qemu_remove_exit_notifier(Notifier *notify)
2700 notifier_remove(notify);
2703 static void qemu_run_exit_notifiers(void)
2705 notifier_list_notify(&exit_notifiers, NULL);
2708 static bool machine_init_done;
2710 void qemu_add_machine_init_done_notifier(Notifier *notify)
2712 notifier_list_add(&machine_init_done_notifiers, notify);
2713 if (machine_init_done) {
2714 notify->notify(notify, NULL);
2718 void qemu_remove_machine_init_done_notifier(Notifier *notify)
2720 notifier_remove(notify);
2723 static void qemu_run_machine_init_done_notifiers(void)
2725 notifier_list_notify(&machine_init_done_notifiers, NULL);
2726 machine_init_done = true;
2729 static const QEMUOption *lookup_opt(int argc, char **argv,
2730 const char **poptarg, int *poptind)
2732 const QEMUOption *popt;
2733 int optind = *poptind;
2734 char *r = argv[optind];
2735 const char *optarg;
2737 loc_set_cmdline(argv, optind, 1);
2738 optind++;
2739 /* Treat --foo the same as -foo. */
2740 if (r[1] == '-')
2741 r++;
2742 popt = qemu_options;
2743 for(;;) {
2744 if (!popt->name) {
2745 error_report("invalid option");
2746 exit(1);
2748 if (!strcmp(popt->name, r + 1))
2749 break;
2750 popt++;
2752 if (popt->flags & HAS_ARG) {
2753 if (optind >= argc) {
2754 error_report("requires an argument");
2755 exit(1);
2757 optarg = argv[optind++];
2758 loc_set_cmdline(argv, optind - 2, 2);
2759 } else {
2760 optarg = NULL;
2763 *poptarg = optarg;
2764 *poptind = optind;
2766 return popt;
2769 static MachineClass *select_machine(void)
2771 MachineClass *machine_class = find_default_machine();
2772 const char *optarg;
2773 QemuOpts *opts;
2774 Location loc;
2776 loc_push_none(&loc);
2778 opts = qemu_get_machine_opts();
2779 qemu_opts_loc_restore(opts);
2781 optarg = qemu_opt_get(opts, "type");
2782 if (optarg) {
2783 machine_class = machine_parse(optarg);
2786 if (!machine_class) {
2787 error_report("No machine specified, and there is no default");
2788 error_printf("Use -machine help to list supported machines\n");
2789 exit(1);
2792 loc_pop(&loc);
2793 return machine_class;
2796 static int machine_set_property(void *opaque,
2797 const char *name, const char *value,
2798 Error **errp)
2800 Object *obj = OBJECT(opaque);
2801 Error *local_err = NULL;
2802 char *c, *qom_name;
2804 if (strcmp(name, "type") == 0) {
2805 return 0;
2808 qom_name = g_strdup(name);
2809 c = qom_name;
2810 while (*c++) {
2811 if (*c == '_') {
2812 *c = '-';
2816 object_property_parse(obj, value, qom_name, &local_err);
2817 g_free(qom_name);
2819 if (local_err) {
2820 error_report_err(local_err);
2821 return -1;
2824 return 0;
2829 * Initial object creation happens before all other
2830 * QEMU data types are created. The majority of objects
2831 * can be created at this point. The rng-egd object
2832 * cannot be created here, as it depends on the chardev
2833 * already existing.
2835 static bool object_create_initial(const char *type)
2837 if (g_str_equal(type, "rng-egd")) {
2838 return false;
2842 * return false for concrete netfilters since
2843 * they depend on netdevs already existing
2845 if (g_str_equal(type, "filter-buffer") ||
2846 g_str_equal(type, "filter-dump") ||
2847 g_str_equal(type, "filter-mirror") ||
2848 g_str_equal(type, "filter-redirector")) {
2849 return false;
2852 return true;
2857 * The remainder of object creation happens after the
2858 * creation of chardev, fsdev, net clients and device data types.
2860 static bool object_create_delayed(const char *type)
2862 return !object_create_initial(type);
2866 static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2867 MachineClass *mc)
2869 uint64_t sz;
2870 const char *mem_str;
2871 const char *maxmem_str, *slots_str;
2872 const ram_addr_t default_ram_size = mc->default_ram_size;
2873 QemuOpts *opts = qemu_find_opts_singleton("memory");
2874 Location loc;
2876 loc_push_none(&loc);
2877 qemu_opts_loc_restore(opts);
2879 sz = 0;
2880 mem_str = qemu_opt_get(opts, "size");
2881 if (mem_str) {
2882 if (!*mem_str) {
2883 error_report("missing 'size' option value");
2884 exit(EXIT_FAILURE);
2887 sz = qemu_opt_get_size(opts, "size", ram_size);
2889 /* Fix up legacy suffix-less format */
2890 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2891 uint64_t overflow_check = sz;
2893 sz <<= 20;
2894 if ((sz >> 20) != overflow_check) {
2895 error_report("too large 'size' option value");
2896 exit(EXIT_FAILURE);
2901 /* backward compatibility behaviour for case "-m 0" */
2902 if (sz == 0) {
2903 sz = default_ram_size;
2906 sz = QEMU_ALIGN_UP(sz, 8192);
2907 ram_size = sz;
2908 if (ram_size != sz) {
2909 error_report("ram size too large");
2910 exit(EXIT_FAILURE);
2913 /* store value for the future use */
2914 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
2915 *maxram_size = ram_size;
2917 maxmem_str = qemu_opt_get(opts, "maxmem");
2918 slots_str = qemu_opt_get(opts, "slots");
2919 if (maxmem_str && slots_str) {
2920 uint64_t slots;
2922 sz = qemu_opt_get_size(opts, "maxmem", 0);
2923 slots = qemu_opt_get_number(opts, "slots", 0);
2924 if (sz < ram_size) {
2925 error_report("invalid value of -m option maxmem: "
2926 "maximum memory size (0x%" PRIx64 ") must be at least "
2927 "the initial memory size (0x" RAM_ADDR_FMT ")",
2928 sz, ram_size);
2929 exit(EXIT_FAILURE);
2930 } else if (sz > ram_size) {
2931 if (!slots) {
2932 error_report("invalid value of -m option: maxmem was "
2933 "specified, but no hotplug slots were specified");
2934 exit(EXIT_FAILURE);
2936 } else if (slots) {
2937 error_report("invalid value of -m option maxmem: "
2938 "memory slots were specified but maximum memory size "
2939 "(0x%" PRIx64 ") is equal to the initial memory size "
2940 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
2941 exit(EXIT_FAILURE);
2944 *maxram_size = sz;
2945 *ram_slots = slots;
2946 } else if ((!maxmem_str && slots_str) ||
2947 (maxmem_str && !slots_str)) {
2948 error_report("invalid -m option value: missing "
2949 "'%s' option", slots_str ? "maxmem" : "slots");
2950 exit(EXIT_FAILURE);
2953 loc_pop(&loc);
2956 static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2958 GlobalProperty *g;
2960 g = g_malloc0(sizeof(*g));
2961 g->driver = qemu_opt_get(opts, "driver");
2962 g->property = qemu_opt_get(opts, "property");
2963 g->value = qemu_opt_get(opts, "value");
2964 g->user_provided = true;
2965 g->errp = &error_fatal;
2966 qdev_prop_register_global(g);
2967 return 0;
2970 int main(int argc, char **argv)
2972 int i;
2973 int snapshot, linux_boot;
2974 const char *initrd_filename;
2975 const char *kernel_filename, *kernel_cmdline;
2976 const char *boot_order = NULL;
2977 const char *boot_once = NULL;
2978 DisplayState *ds;
2979 int cyls, heads, secs, translation;
2980 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
2981 QemuOptsList *olist;
2982 int optind;
2983 const char *optarg;
2984 const char *loadvm = NULL;
2985 MachineClass *machine_class;
2986 const char *cpu_model;
2987 const char *vga_model = NULL;
2988 const char *qtest_chrdev = NULL;
2989 const char *qtest_log = NULL;
2990 const char *pid_file = NULL;
2991 const char *incoming = NULL;
2992 bool defconfig = true;
2993 bool userconfig = true;
2994 bool nographic = false;
2995 DisplayType display_type = DT_DEFAULT;
2996 int display_remote = 0;
2997 const char *log_mask = NULL;
2998 const char *log_file = NULL;
2999 char *trace_file = NULL;
3000 ram_addr_t maxram_size;
3001 uint64_t ram_slots = 0;
3002 FILE *vmstate_dump_file = NULL;
3003 Error *main_loop_err = NULL;
3004 Error *err = NULL;
3005 bool list_data_dirs = false;
3007 qemu_init_cpu_loop();
3008 qemu_mutex_lock_iothread();
3010 atexit(qemu_run_exit_notifiers);
3011 error_set_progname(argv[0]);
3012 qemu_init_exec_dir(argv[0]);
3014 module_call_init(MODULE_INIT_QOM);
3016 qemu_add_opts(&qemu_drive_opts);
3017 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3018 qemu_add_drive_opts(&qemu_common_drive_opts);
3019 qemu_add_drive_opts(&qemu_drive_opts);
3020 qemu_add_opts(&qemu_chardev_opts);
3021 qemu_add_opts(&qemu_device_opts);
3022 qemu_add_opts(&qemu_netdev_opts);
3023 qemu_add_opts(&qemu_net_opts);
3024 qemu_add_opts(&qemu_rtc_opts);
3025 qemu_add_opts(&qemu_global_opts);
3026 qemu_add_opts(&qemu_mon_opts);
3027 qemu_add_opts(&qemu_trace_opts);
3028 qemu_add_opts(&qemu_option_rom_opts);
3029 qemu_add_opts(&qemu_machine_opts);
3030 qemu_add_opts(&qemu_mem_opts);
3031 qemu_add_opts(&qemu_smp_opts);
3032 qemu_add_opts(&qemu_boot_opts);
3033 qemu_add_opts(&qemu_sandbox_opts);
3034 qemu_add_opts(&qemu_add_fd_opts);
3035 qemu_add_opts(&qemu_object_opts);
3036 qemu_add_opts(&qemu_tpmdev_opts);
3037 qemu_add_opts(&qemu_realtime_opts);
3038 qemu_add_opts(&qemu_msg_opts);
3039 qemu_add_opts(&qemu_name_opts);
3040 qemu_add_opts(&qemu_numa_opts);
3041 qemu_add_opts(&qemu_icount_opts);
3042 qemu_add_opts(&qemu_semihosting_config_opts);
3043 qemu_add_opts(&qemu_fw_cfg_opts);
3044 module_call_init(MODULE_INIT_OPTS);
3046 runstate_init();
3048 if (qcrypto_init(&err) < 0) {
3049 error_reportf_err(err, "cannot initialize crypto: ");
3050 exit(1);
3052 rtc_clock = QEMU_CLOCK_HOST;
3054 QLIST_INIT (&vm_change_state_head);
3055 os_setup_early_signal_handling();
3057 cpu_model = NULL;
3058 snapshot = 0;
3059 cyls = heads = secs = 0;
3060 translation = BIOS_ATA_TRANSLATION_AUTO;
3062 nb_nics = 0;
3064 bdrv_init_with_whitelist();
3066 autostart = 1;
3068 /* first pass of option parsing */
3069 optind = 1;
3070 while (optind < argc) {
3071 if (argv[optind][0] != '-') {
3072 /* disk image */
3073 optind++;
3074 } else {
3075 const QEMUOption *popt;
3077 popt = lookup_opt(argc, argv, &optarg, &optind);
3078 switch (popt->index) {
3079 case QEMU_OPTION_nodefconfig:
3080 defconfig = false;
3081 break;
3082 case QEMU_OPTION_nouserconfig:
3083 userconfig = false;
3084 break;
3089 if (defconfig) {
3090 int ret;
3091 ret = qemu_read_default_config_files(userconfig);
3092 if (ret < 0) {
3093 exit(1);
3097 /* second pass of option parsing */
3098 optind = 1;
3099 for(;;) {
3100 if (optind >= argc)
3101 break;
3102 if (argv[optind][0] != '-') {
3103 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
3104 } else {
3105 const QEMUOption *popt;
3107 popt = lookup_opt(argc, argv, &optarg, &optind);
3108 if (!(popt->arch_mask & arch_type)) {
3109 error_report("Option not supported for this target");
3110 exit(1);
3112 switch(popt->index) {
3113 case QEMU_OPTION_no_kvm_irqchip: {
3114 olist = qemu_find_opts("machine");
3115 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
3116 break;
3118 case QEMU_OPTION_cpu:
3119 /* hw initialization will check this */
3120 cpu_model = optarg;
3121 break;
3122 case QEMU_OPTION_hda:
3124 char buf[256];
3125 if (cyls == 0)
3126 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3127 else
3128 snprintf(buf, sizeof(buf),
3129 "%s,cyls=%d,heads=%d,secs=%d%s",
3130 HD_OPTS , cyls, heads, secs,
3131 translation == BIOS_ATA_TRANSLATION_LBA ?
3132 ",trans=lba" :
3133 translation == BIOS_ATA_TRANSLATION_NONE ?
3134 ",trans=none" : "");
3135 drive_add(IF_DEFAULT, 0, optarg, buf);
3136 break;
3138 case QEMU_OPTION_hdb:
3139 case QEMU_OPTION_hdc:
3140 case QEMU_OPTION_hdd:
3141 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3142 HD_OPTS);
3143 break;
3144 case QEMU_OPTION_drive:
3145 if (drive_def(optarg) == NULL) {
3146 exit(1);
3148 break;
3149 case QEMU_OPTION_set:
3150 if (qemu_set_option(optarg) != 0)
3151 exit(1);
3152 break;
3153 case QEMU_OPTION_global:
3154 if (qemu_global_option(optarg) != 0)
3155 exit(1);
3156 break;
3157 case QEMU_OPTION_mtdblock:
3158 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3159 break;
3160 case QEMU_OPTION_sd:
3161 drive_add(IF_SD, -1, optarg, SD_OPTS);
3162 break;
3163 case QEMU_OPTION_pflash:
3164 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
3165 break;
3166 case QEMU_OPTION_snapshot:
3167 snapshot = 1;
3168 break;
3169 case QEMU_OPTION_hdachs:
3171 const char *p;
3172 p = optarg;
3173 cyls = strtol(p, (char **)&p, 0);
3174 if (cyls < 1 || cyls > 16383)
3175 goto chs_fail;
3176 if (*p != ',')
3177 goto chs_fail;
3178 p++;
3179 heads = strtol(p, (char **)&p, 0);
3180 if (heads < 1 || heads > 16)
3181 goto chs_fail;
3182 if (*p != ',')
3183 goto chs_fail;
3184 p++;
3185 secs = strtol(p, (char **)&p, 0);
3186 if (secs < 1 || secs > 63)
3187 goto chs_fail;
3188 if (*p == ',') {
3189 p++;
3190 if (!strcmp(p, "large")) {
3191 translation = BIOS_ATA_TRANSLATION_LARGE;
3192 } else if (!strcmp(p, "rechs")) {
3193 translation = BIOS_ATA_TRANSLATION_RECHS;
3194 } else if (!strcmp(p, "none")) {
3195 translation = BIOS_ATA_TRANSLATION_NONE;
3196 } else if (!strcmp(p, "lba")) {
3197 translation = BIOS_ATA_TRANSLATION_LBA;
3198 } else if (!strcmp(p, "auto")) {
3199 translation = BIOS_ATA_TRANSLATION_AUTO;
3200 } else {
3201 goto chs_fail;
3203 } else if (*p != '\0') {
3204 chs_fail:
3205 error_report("invalid physical CHS format");
3206 exit(1);
3208 if (hda_opts != NULL) {
3209 qemu_opt_set_number(hda_opts, "cyls", cyls,
3210 &error_abort);
3211 qemu_opt_set_number(hda_opts, "heads", heads,
3212 &error_abort);
3213 qemu_opt_set_number(hda_opts, "secs", secs,
3214 &error_abort);
3215 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
3216 qemu_opt_set(hda_opts, "trans", "large",
3217 &error_abort);
3218 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
3219 qemu_opt_set(hda_opts, "trans", "rechs",
3220 &error_abort);
3221 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
3222 qemu_opt_set(hda_opts, "trans", "lba",
3223 &error_abort);
3224 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
3225 qemu_opt_set(hda_opts, "trans", "none",
3226 &error_abort);
3230 break;
3231 case QEMU_OPTION_numa:
3232 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3233 optarg, true);
3234 if (!opts) {
3235 exit(1);
3237 break;
3238 case QEMU_OPTION_display:
3239 display_type = select_display(optarg);
3240 break;
3241 case QEMU_OPTION_nographic:
3242 olist = qemu_find_opts("machine");
3243 qemu_opts_parse_noisily(olist, "graphics=off", false);
3244 nographic = true;
3245 display_type = DT_NONE;
3246 break;
3247 case QEMU_OPTION_curses:
3248 #ifdef CONFIG_CURSES
3249 display_type = DT_CURSES;
3250 #else
3251 error_report("curses support is disabled");
3252 exit(1);
3253 #endif
3254 break;
3255 case QEMU_OPTION_portrait:
3256 graphic_rotate = 90;
3257 break;
3258 case QEMU_OPTION_rotate:
3259 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3260 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3261 graphic_rotate != 180 && graphic_rotate != 270) {
3262 error_report("only 90, 180, 270 deg rotation is available");
3263 exit(1);
3265 break;
3266 case QEMU_OPTION_kernel:
3267 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3268 &error_abort);
3269 break;
3270 case QEMU_OPTION_initrd:
3271 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3272 &error_abort);
3273 break;
3274 case QEMU_OPTION_append:
3275 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3276 &error_abort);
3277 break;
3278 case QEMU_OPTION_dtb:
3279 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3280 &error_abort);
3281 break;
3282 case QEMU_OPTION_cdrom:
3283 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
3284 break;
3285 case QEMU_OPTION_boot:
3286 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3287 optarg, true);
3288 if (!opts) {
3289 exit(1);
3291 break;
3292 case QEMU_OPTION_fda:
3293 case QEMU_OPTION_fdb:
3294 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3295 optarg, FD_OPTS);
3296 break;
3297 case QEMU_OPTION_no_fd_bootchk:
3298 fd_bootchk = 0;
3299 break;
3300 case QEMU_OPTION_netdev:
3301 default_net = 0;
3302 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
3303 exit(1);
3305 break;
3306 case QEMU_OPTION_net:
3307 default_net = 0;
3308 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
3309 exit(1);
3311 break;
3312 #ifdef CONFIG_LIBISCSI
3313 case QEMU_OPTION_iscsi:
3314 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3315 optarg, false);
3316 if (!opts) {
3317 exit(1);
3319 break;
3320 #endif
3321 #ifdef CONFIG_SLIRP
3322 case QEMU_OPTION_tftp:
3323 error_report("The -tftp option is deprecated. "
3324 "Please use '-netdev user,tftp=...' instead.");
3325 legacy_tftp_prefix = optarg;
3326 break;
3327 case QEMU_OPTION_bootp:
3328 error_report("The -bootp option is deprecated. "
3329 "Please use '-netdev user,bootfile=...' instead.");
3330 legacy_bootp_filename = optarg;
3331 break;
3332 case QEMU_OPTION_redir:
3333 error_report("The -redir option is deprecated. "
3334 "Please use '-netdev user,hostfwd=...' instead.");
3335 if (net_slirp_redir(optarg) < 0)
3336 exit(1);
3337 break;
3338 #endif
3339 case QEMU_OPTION_bt:
3340 add_device_config(DEV_BT, optarg);
3341 break;
3342 case QEMU_OPTION_audio_help:
3343 AUD_help ();
3344 exit (0);
3345 break;
3346 case QEMU_OPTION_soundhw:
3347 select_soundhw (optarg);
3348 break;
3349 case QEMU_OPTION_h:
3350 help(0);
3351 break;
3352 case QEMU_OPTION_version:
3353 version();
3354 exit(0);
3355 break;
3356 case QEMU_OPTION_m:
3357 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3358 optarg, true);
3359 if (!opts) {
3360 exit(EXIT_FAILURE);
3362 break;
3363 #ifdef CONFIG_TPM
3364 case QEMU_OPTION_tpmdev:
3365 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3366 exit(1);
3368 break;
3369 #endif
3370 case QEMU_OPTION_mempath:
3371 mem_path = optarg;
3372 break;
3373 case QEMU_OPTION_mem_prealloc:
3374 mem_prealloc = 1;
3375 break;
3376 case QEMU_OPTION_d:
3377 log_mask = optarg;
3378 break;
3379 case QEMU_OPTION_D:
3380 log_file = optarg;
3381 break;
3382 case QEMU_OPTION_DFILTER:
3383 qemu_set_dfilter_ranges(optarg, &error_fatal);
3384 break;
3385 case QEMU_OPTION_s:
3386 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
3387 break;
3388 case QEMU_OPTION_gdb:
3389 add_device_config(DEV_GDB, optarg);
3390 break;
3391 case QEMU_OPTION_L:
3392 if (is_help_option(optarg)) {
3393 list_data_dirs = true;
3394 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
3395 data_dir[data_dir_idx++] = optarg;
3397 break;
3398 case QEMU_OPTION_bios:
3399 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3400 &error_abort);
3401 break;
3402 case QEMU_OPTION_singlestep:
3403 singlestep = 1;
3404 break;
3405 case QEMU_OPTION_S:
3406 autostart = 0;
3407 break;
3408 case QEMU_OPTION_k:
3409 keyboard_layout = optarg;
3410 break;
3411 case QEMU_OPTION_localtime:
3412 rtc_utc = 0;
3413 break;
3414 case QEMU_OPTION_vga:
3415 vga_model = optarg;
3416 default_vga = 0;
3417 break;
3418 case QEMU_OPTION_g:
3420 const char *p;
3421 int w, h, depth;
3422 p = optarg;
3423 w = strtol(p, (char **)&p, 10);
3424 if (w <= 0) {
3425 graphic_error:
3426 error_report("invalid resolution or depth");
3427 exit(1);
3429 if (*p != 'x')
3430 goto graphic_error;
3431 p++;
3432 h = strtol(p, (char **)&p, 10);
3433 if (h <= 0)
3434 goto graphic_error;
3435 if (*p == 'x') {
3436 p++;
3437 depth = strtol(p, (char **)&p, 10);
3438 if (depth != 8 && depth != 15 && depth != 16 &&
3439 depth != 24 && depth != 32)
3440 goto graphic_error;
3441 } else if (*p == '\0') {
3442 depth = graphic_depth;
3443 } else {
3444 goto graphic_error;
3447 graphic_width = w;
3448 graphic_height = h;
3449 graphic_depth = depth;
3451 break;
3452 case QEMU_OPTION_echr:
3454 char *r;
3455 term_escape_char = strtol(optarg, &r, 0);
3456 if (r == optarg)
3457 printf("Bad argument to echr\n");
3458 break;
3460 case QEMU_OPTION_monitor:
3461 default_monitor = 0;
3462 if (strncmp(optarg, "none", 4)) {
3463 monitor_parse(optarg, "readline", false);
3465 break;
3466 case QEMU_OPTION_qmp:
3467 monitor_parse(optarg, "control", false);
3468 default_monitor = 0;
3469 break;
3470 case QEMU_OPTION_qmp_pretty:
3471 monitor_parse(optarg, "control", true);
3472 default_monitor = 0;
3473 break;
3474 case QEMU_OPTION_mon:
3475 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3476 true);
3477 if (!opts) {
3478 exit(1);
3480 default_monitor = 0;
3481 break;
3482 case QEMU_OPTION_chardev:
3483 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3484 optarg, true);
3485 if (!opts) {
3486 exit(1);
3488 break;
3489 case QEMU_OPTION_fsdev:
3490 olist = qemu_find_opts("fsdev");
3491 if (!olist) {
3492 error_report("fsdev support is disabled");
3493 exit(1);
3495 opts = qemu_opts_parse_noisily(olist, optarg, true);
3496 if (!opts) {
3497 exit(1);
3499 break;
3500 case QEMU_OPTION_virtfs: {
3501 QemuOpts *fsdev;
3502 QemuOpts *device;
3503 const char *writeout, *sock_fd, *socket;
3505 olist = qemu_find_opts("virtfs");
3506 if (!olist) {
3507 error_report("virtfs support is disabled");
3508 exit(1);
3510 opts = qemu_opts_parse_noisily(olist, optarg, true);
3511 if (!opts) {
3512 exit(1);
3515 if (qemu_opt_get(opts, "fsdriver") == NULL ||
3516 qemu_opt_get(opts, "mount_tag") == NULL) {
3517 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
3518 exit(1);
3520 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3521 qemu_opt_get(opts, "mount_tag"),
3522 1, NULL);
3523 if (!fsdev) {
3524 error_report("duplicate fsdev id: %s",
3525 qemu_opt_get(opts, "mount_tag"));
3526 exit(1);
3529 writeout = qemu_opt_get(opts, "writeout");
3530 if (writeout) {
3531 #ifdef CONFIG_SYNC_FILE_RANGE
3532 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
3533 #else
3534 error_report("writeout=immediate not supported "
3535 "on this platform");
3536 exit(1);
3537 #endif
3539 qemu_opt_set(fsdev, "fsdriver",
3540 qemu_opt_get(opts, "fsdriver"), &error_abort);
3541 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3542 &error_abort);
3543 qemu_opt_set(fsdev, "security_model",
3544 qemu_opt_get(opts, "security_model"),
3545 &error_abort);
3546 socket = qemu_opt_get(opts, "socket");
3547 if (socket) {
3548 qemu_opt_set(fsdev, "socket", socket, &error_abort);
3550 sock_fd = qemu_opt_get(opts, "sock_fd");
3551 if (sock_fd) {
3552 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
3555 qemu_opt_set_bool(fsdev, "readonly",
3556 qemu_opt_get_bool(opts, "readonly", 0),
3557 &error_abort);
3558 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3559 &error_abort);
3560 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3561 qemu_opt_set(device, "fsdev",
3562 qemu_opt_get(opts, "mount_tag"), &error_abort);
3563 qemu_opt_set(device, "mount_tag",
3564 qemu_opt_get(opts, "mount_tag"), &error_abort);
3565 break;
3567 case QEMU_OPTION_virtfs_synth: {
3568 QemuOpts *fsdev;
3569 QemuOpts *device;
3571 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3572 1, NULL);
3573 if (!fsdev) {
3574 error_report("duplicate option: %s", "virtfs_synth");
3575 exit(1);
3577 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
3579 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3580 &error_abort);
3581 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3582 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3583 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
3584 break;
3586 case QEMU_OPTION_serial:
3587 add_device_config(DEV_SERIAL, optarg);
3588 default_serial = 0;
3589 if (strncmp(optarg, "mon:", 4) == 0) {
3590 default_monitor = 0;
3592 break;
3593 case QEMU_OPTION_watchdog:
3594 if (watchdog) {
3595 error_report("only one watchdog option may be given");
3596 return 1;
3598 watchdog = optarg;
3599 break;
3600 case QEMU_OPTION_watchdog_action:
3601 if (select_watchdog_action(optarg) == -1) {
3602 error_report("unknown -watchdog-action parameter");
3603 exit(1);
3605 break;
3606 case QEMU_OPTION_virtiocon:
3607 add_device_config(DEV_VIRTCON, optarg);
3608 default_virtcon = 0;
3609 if (strncmp(optarg, "mon:", 4) == 0) {
3610 default_monitor = 0;
3612 break;
3613 case QEMU_OPTION_parallel:
3614 add_device_config(DEV_PARALLEL, optarg);
3615 default_parallel = 0;
3616 if (strncmp(optarg, "mon:", 4) == 0) {
3617 default_monitor = 0;
3619 break;
3620 case QEMU_OPTION_debugcon:
3621 add_device_config(DEV_DEBUGCON, optarg);
3622 break;
3623 case QEMU_OPTION_loadvm:
3624 loadvm = optarg;
3625 break;
3626 case QEMU_OPTION_full_screen:
3627 full_screen = 1;
3628 break;
3629 case QEMU_OPTION_no_frame:
3630 no_frame = 1;
3631 break;
3632 case QEMU_OPTION_alt_grab:
3633 alt_grab = 1;
3634 break;
3635 case QEMU_OPTION_ctrl_grab:
3636 ctrl_grab = 1;
3637 break;
3638 case QEMU_OPTION_no_quit:
3639 no_quit = 1;
3640 break;
3641 case QEMU_OPTION_sdl:
3642 #ifdef CONFIG_SDL
3643 display_type = DT_SDL;
3644 break;
3645 #else
3646 error_report("SDL support is disabled");
3647 exit(1);
3648 #endif
3649 case QEMU_OPTION_pidfile:
3650 pid_file = optarg;
3651 break;
3652 case QEMU_OPTION_win2k_hack:
3653 win2k_install_hack = 1;
3654 break;
3655 case QEMU_OPTION_rtc_td_hack: {
3656 static GlobalProperty slew_lost_ticks = {
3657 .driver = "mc146818rtc",
3658 .property = "lost_tick_policy",
3659 .value = "slew",
3662 qdev_prop_register_global(&slew_lost_ticks);
3663 break;
3665 case QEMU_OPTION_acpitable:
3666 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3667 optarg, true);
3668 if (!opts) {
3669 exit(1);
3671 do_acpitable_option(opts);
3672 break;
3673 case QEMU_OPTION_smbios:
3674 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3675 optarg, false);
3676 if (!opts) {
3677 exit(1);
3679 do_smbios_option(opts);
3680 break;
3681 case QEMU_OPTION_fwcfg:
3682 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3683 optarg, true);
3684 if (opts == NULL) {
3685 exit(1);
3687 break;
3688 case QEMU_OPTION_enable_kvm:
3689 olist = qemu_find_opts("machine");
3690 qemu_opts_parse_noisily(olist, "accel=kvm", false);
3691 break;
3692 case QEMU_OPTION_M:
3693 case QEMU_OPTION_machine:
3694 olist = qemu_find_opts("machine");
3695 opts = qemu_opts_parse_noisily(olist, optarg, true);
3696 if (!opts) {
3697 exit(1);
3699 break;
3700 case QEMU_OPTION_no_kvm:
3701 olist = qemu_find_opts("machine");
3702 qemu_opts_parse_noisily(olist, "accel=tcg", false);
3703 break;
3704 case QEMU_OPTION_no_kvm_pit: {
3705 error_report("warning: ignoring deprecated option");
3706 break;
3708 case QEMU_OPTION_no_kvm_pit_reinjection: {
3709 static GlobalProperty kvm_pit_lost_tick_policy = {
3710 .driver = "kvm-pit",
3711 .property = "lost_tick_policy",
3712 .value = "discard",
3715 error_report("warning: deprecated, replaced by "
3716 "-global kvm-pit.lost_tick_policy=discard");
3717 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
3718 break;
3720 case QEMU_OPTION_usb:
3721 olist = qemu_find_opts("machine");
3722 qemu_opts_parse_noisily(olist, "usb=on", false);
3723 break;
3724 case QEMU_OPTION_usbdevice:
3725 olist = qemu_find_opts("machine");
3726 qemu_opts_parse_noisily(olist, "usb=on", false);
3727 add_device_config(DEV_USB, optarg);
3728 break;
3729 case QEMU_OPTION_device:
3730 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3731 optarg, true)) {
3732 exit(1);
3734 break;
3735 case QEMU_OPTION_smp:
3736 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3737 optarg, true)) {
3738 exit(1);
3740 break;
3741 case QEMU_OPTION_vnc:
3742 vnc_parse(optarg, &error_fatal);
3743 break;
3744 case QEMU_OPTION_no_acpi:
3745 acpi_enabled = 0;
3746 break;
3747 case QEMU_OPTION_no_hpet:
3748 no_hpet = 1;
3749 break;
3750 case QEMU_OPTION_balloon:
3751 if (balloon_parse(optarg) < 0) {
3752 error_report("unknown -balloon argument %s", optarg);
3753 exit(1);
3755 break;
3756 case QEMU_OPTION_no_reboot:
3757 no_reboot = 1;
3758 break;
3759 case QEMU_OPTION_no_shutdown:
3760 no_shutdown = 1;
3761 break;
3762 case QEMU_OPTION_show_cursor:
3763 cursor_hide = 0;
3764 break;
3765 case QEMU_OPTION_uuid:
3766 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3767 error_report("failed to parse UUID string: wrong format");
3768 exit(1);
3770 qemu_uuid_set = true;
3771 break;
3772 case QEMU_OPTION_option_rom:
3773 if (nb_option_roms >= MAX_OPTION_ROMS) {
3774 error_report("too many option ROMs");
3775 exit(1);
3777 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3778 optarg, true);
3779 if (!opts) {
3780 exit(1);
3782 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3783 option_rom[nb_option_roms].bootindex =
3784 qemu_opt_get_number(opts, "bootindex", -1);
3785 if (!option_rom[nb_option_roms].name) {
3786 error_report("Option ROM file is not specified");
3787 exit(1);
3789 nb_option_roms++;
3790 break;
3791 case QEMU_OPTION_semihosting:
3792 semihosting.enabled = true;
3793 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3794 break;
3795 case QEMU_OPTION_semihosting_config:
3796 semihosting.enabled = true;
3797 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3798 optarg, false);
3799 if (opts != NULL) {
3800 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
3801 true);
3802 const char *target = qemu_opt_get(opts, "target");
3803 if (target != NULL) {
3804 if (strcmp("native", target) == 0) {
3805 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
3806 } else if (strcmp("gdb", target) == 0) {
3807 semihosting.target = SEMIHOSTING_TARGET_GDB;
3808 } else if (strcmp("auto", target) == 0) {
3809 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3810 } else {
3811 error_report("unsupported semihosting-config %s",
3812 optarg);
3813 exit(1);
3815 } else {
3816 semihosting.target = SEMIHOSTING_TARGET_AUTO;
3818 /* Set semihosting argument count and vector */
3819 qemu_opt_foreach(opts, add_semihosting_arg,
3820 &semihosting, NULL);
3821 } else {
3822 error_report("unsupported semihosting-config %s", optarg);
3823 exit(1);
3825 break;
3826 case QEMU_OPTION_tdf:
3827 error_report("warning: ignoring deprecated option");
3828 break;
3829 case QEMU_OPTION_name:
3830 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3831 optarg, true);
3832 if (!opts) {
3833 exit(1);
3835 break;
3836 case QEMU_OPTION_prom_env:
3837 if (nb_prom_envs >= MAX_PROM_ENVS) {
3838 error_report("too many prom variables");
3839 exit(1);
3841 prom_envs[nb_prom_envs] = optarg;
3842 nb_prom_envs++;
3843 break;
3844 case QEMU_OPTION_old_param:
3845 old_param = 1;
3846 break;
3847 case QEMU_OPTION_clock:
3848 /* Clock options no longer exist. Keep this option for
3849 * backward compatibility.
3851 break;
3852 case QEMU_OPTION_startdate:
3853 configure_rtc_date_offset(optarg, 1);
3854 break;
3855 case QEMU_OPTION_rtc:
3856 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3857 false);
3858 if (!opts) {
3859 exit(1);
3861 configure_rtc(opts);
3862 break;
3863 case QEMU_OPTION_tb_size:
3864 tcg_tb_size = strtol(optarg, NULL, 0);
3865 if (tcg_tb_size < 0) {
3866 tcg_tb_size = 0;
3868 break;
3869 case QEMU_OPTION_icount:
3870 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3871 optarg, true);
3872 if (!icount_opts) {
3873 exit(1);
3875 break;
3876 case QEMU_OPTION_incoming:
3877 if (!incoming) {
3878 runstate_set(RUN_STATE_INMIGRATE);
3880 incoming = optarg;
3881 break;
3882 case QEMU_OPTION_nodefaults:
3883 has_defaults = 0;
3884 break;
3885 case QEMU_OPTION_xen_domid:
3886 if (!(xen_available())) {
3887 error_report("Option not supported for this target");
3888 exit(1);
3890 xen_domid = atoi(optarg);
3891 break;
3892 case QEMU_OPTION_xen_create:
3893 if (!(xen_available())) {
3894 error_report("Option not supported for this target");
3895 exit(1);
3897 xen_mode = XEN_CREATE;
3898 break;
3899 case QEMU_OPTION_xen_attach:
3900 if (!(xen_available())) {
3901 error_report("Option not supported for this target");
3902 exit(1);
3904 xen_mode = XEN_ATTACH;
3905 break;
3906 case QEMU_OPTION_trace:
3907 g_free(trace_file);
3908 trace_file = trace_opt_parse(optarg);
3909 break;
3910 case QEMU_OPTION_trace_unassigned:
3911 trace_unassigned = true;
3912 break;
3913 case QEMU_OPTION_readconfig:
3915 int ret = qemu_read_config_file(optarg);
3916 if (ret < 0) {
3917 error_report("read config %s: %s", optarg,
3918 strerror(-ret));
3919 exit(1);
3921 break;
3923 case QEMU_OPTION_spice:
3924 olist = qemu_find_opts("spice");
3925 if (!olist) {
3926 error_report("spice support is disabled");
3927 exit(1);
3929 opts = qemu_opts_parse_noisily(olist, optarg, false);
3930 if (!opts) {
3931 exit(1);
3933 display_remote++;
3934 break;
3935 case QEMU_OPTION_writeconfig:
3937 FILE *fp;
3938 if (strcmp(optarg, "-") == 0) {
3939 fp = stdout;
3940 } else {
3941 fp = fopen(optarg, "w");
3942 if (fp == NULL) {
3943 error_report("open %s: %s", optarg,
3944 strerror(errno));
3945 exit(1);
3948 qemu_config_write(fp);
3949 if (fp != stdout) {
3950 fclose(fp);
3952 break;
3954 case QEMU_OPTION_qtest:
3955 qtest_chrdev = optarg;
3956 break;
3957 case QEMU_OPTION_qtest_log:
3958 qtest_log = optarg;
3959 break;
3960 case QEMU_OPTION_sandbox:
3961 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3962 optarg, true);
3963 if (!opts) {
3964 exit(1);
3966 break;
3967 case QEMU_OPTION_add_fd:
3968 #ifndef _WIN32
3969 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3970 optarg, false);
3971 if (!opts) {
3972 exit(1);
3974 #else
3975 error_report("File descriptor passing is disabled on this "
3976 "platform");
3977 exit(1);
3978 #endif
3979 break;
3980 case QEMU_OPTION_object:
3981 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3982 optarg, true);
3983 if (!opts) {
3984 exit(1);
3986 break;
3987 case QEMU_OPTION_realtime:
3988 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3989 optarg, false);
3990 if (!opts) {
3991 exit(1);
3993 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
3994 break;
3995 case QEMU_OPTION_msg:
3996 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3997 false);
3998 if (!opts) {
3999 exit(1);
4001 configure_msg(opts);
4002 break;
4003 case QEMU_OPTION_dump_vmstate:
4004 if (vmstate_dump_file) {
4005 error_report("only one '-dump-vmstate' "
4006 "option may be given");
4007 exit(1);
4009 vmstate_dump_file = fopen(optarg, "w");
4010 if (vmstate_dump_file == NULL) {
4011 error_report("open %s: %s", optarg, strerror(errno));
4012 exit(1);
4014 break;
4015 default:
4016 os_parse_cmd_args(popt->index, optarg);
4021 * Clear error location left behind by the loop.
4022 * Best done right after the loop. Do not insert code here!
4024 loc_set_none();
4026 replay_configure(icount_opts);
4028 machine_class = select_machine();
4030 set_memory_options(&ram_slots, &maxram_size, machine_class);
4032 os_daemonize();
4034 if (qemu_init_main_loop(&main_loop_err)) {
4035 error_report_err(main_loop_err);
4036 exit(1);
4039 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4040 parse_sandbox, NULL, NULL)) {
4041 exit(1);
4044 if (qemu_opts_foreach(qemu_find_opts("name"),
4045 parse_name, NULL, NULL)) {
4046 exit(1);
4049 #ifndef _WIN32
4050 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4051 parse_add_fd, NULL, NULL)) {
4052 exit(1);
4055 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4056 cleanup_add_fd, NULL, NULL)) {
4057 exit(1);
4059 #endif
4061 current_machine = MACHINE(object_new(object_class_get_name(
4062 OBJECT_CLASS(machine_class))));
4063 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4064 exit(0);
4066 object_property_add_child(object_get_root(), "machine",
4067 OBJECT(current_machine), &error_abort);
4068 cpu_exec_init_all();
4070 if (machine_class->hw_version) {
4071 qemu_set_hw_version(machine_class->hw_version);
4074 if (cpu_model && is_help_option(cpu_model)) {
4075 list_cpus(stdout, &fprintf, cpu_model);
4076 exit(0);
4079 if (!trace_init_backends()) {
4080 exit(1);
4082 trace_init_file(trace_file);
4084 /* Open the logfile at this point and set the log mask if necessary.
4086 if (log_file) {
4087 qemu_set_log_filename(log_file, &error_fatal);
4090 if (log_mask) {
4091 int mask;
4092 mask = qemu_str_to_log_mask(log_mask);
4093 if (!mask) {
4094 qemu_print_log_usage(stdout);
4095 exit(1);
4097 qemu_set_log(mask);
4098 } else {
4099 qemu_set_log(0);
4102 /* If no data_dir is specified then try to find it relative to the
4103 executable path. */
4104 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4105 data_dir[data_dir_idx] = os_find_datadir();
4106 if (data_dir[data_dir_idx] != NULL) {
4107 data_dir_idx++;
4110 /* If all else fails use the install path specified when building. */
4111 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4112 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
4115 /* -L help lists the data directories and exits. */
4116 if (list_data_dirs) {
4117 for (i = 0; i < data_dir_idx; i++) {
4118 printf("%s\n", data_dir[i]);
4120 exit(0);
4123 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
4125 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
4126 if (max_cpus > machine_class->max_cpus) {
4127 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4128 "supported by machine '%s' (%d)", max_cpus,
4129 machine_class->name, machine_class->max_cpus);
4130 exit(1);
4134 * Get the default machine options from the machine if it is not already
4135 * specified either by the configuration file or by the command line.
4137 if (machine_class->default_machine_opts) {
4138 qemu_opts_set_defaults(qemu_find_opts("machine"),
4139 machine_class->default_machine_opts, 0);
4142 qemu_opts_foreach(qemu_find_opts("device"),
4143 default_driver_check, NULL, NULL);
4144 qemu_opts_foreach(qemu_find_opts("global"),
4145 default_driver_check, NULL, NULL);
4147 if (!vga_model && !default_vga) {
4148 vga_interface_type = VGA_DEVICE;
4150 if (!has_defaults || machine_class->no_serial) {
4151 default_serial = 0;
4153 if (!has_defaults || machine_class->no_parallel) {
4154 default_parallel = 0;
4156 if (!has_defaults || !machine_class->use_virtcon) {
4157 default_virtcon = 0;
4159 if (!has_defaults || !machine_class->use_sclp) {
4160 default_sclp = 0;
4162 if (!has_defaults || machine_class->no_floppy) {
4163 default_floppy = 0;
4165 if (!has_defaults || machine_class->no_cdrom) {
4166 default_cdrom = 0;
4168 if (!has_defaults || machine_class->no_sdcard) {
4169 default_sdcard = 0;
4171 if (!has_defaults) {
4172 default_monitor = 0;
4173 default_net = 0;
4174 default_vga = 0;
4177 if (is_daemonized()) {
4178 /* According to documentation and historically, -nographic redirects
4179 * serial port, parallel port and monitor to stdio, which does not work
4180 * with -daemonize. We can redirect these to null instead, but since
4181 * -nographic is legacy, let's just error out.
4182 * We disallow -nographic only if all other ports are not redirected
4183 * explicitly, to not break existing legacy setups which uses
4184 * -nographic _and_ redirects all ports explicitly - this is valid
4185 * usage, -nographic is just a no-op in this case.
4187 if (nographic
4188 && (default_parallel || default_serial
4189 || default_monitor || default_virtcon)) {
4190 error_report("-nographic cannot be used with -daemonize");
4191 exit(1);
4193 #ifdef CONFIG_CURSES
4194 if (display_type == DT_CURSES) {
4195 error_report("curses display cannot be used with -daemonize");
4196 exit(1);
4198 #endif
4201 if (nographic) {
4202 if (default_parallel)
4203 add_device_config(DEV_PARALLEL, "null");
4204 if (default_serial && default_monitor) {
4205 add_device_config(DEV_SERIAL, "mon:stdio");
4206 } else if (default_virtcon && default_monitor) {
4207 add_device_config(DEV_VIRTCON, "mon:stdio");
4208 } else if (default_sclp && default_monitor) {
4209 add_device_config(DEV_SCLP, "mon:stdio");
4210 } else {
4211 if (default_serial)
4212 add_device_config(DEV_SERIAL, "stdio");
4213 if (default_virtcon)
4214 add_device_config(DEV_VIRTCON, "stdio");
4215 if (default_sclp) {
4216 add_device_config(DEV_SCLP, "stdio");
4218 if (default_monitor)
4219 monitor_parse("stdio", "readline", false);
4221 } else {
4222 if (default_serial)
4223 add_device_config(DEV_SERIAL, "vc:80Cx24C");
4224 if (default_parallel)
4225 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
4226 if (default_monitor)
4227 monitor_parse("vc:80Cx24C", "readline", false);
4228 if (default_virtcon)
4229 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
4230 if (default_sclp) {
4231 add_device_config(DEV_SCLP, "vc:80Cx24C");
4235 #if defined(CONFIG_VNC)
4236 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4237 display_remote++;
4239 #endif
4240 if (display_type == DT_DEFAULT && !display_remote) {
4241 #if defined(CONFIG_GTK)
4242 display_type = DT_GTK;
4243 #elif defined(CONFIG_SDL)
4244 display_type = DT_SDL;
4245 #elif defined(CONFIG_COCOA)
4246 display_type = DT_COCOA;
4247 #elif defined(CONFIG_VNC)
4248 vnc_parse("localhost:0,to=99,id=default", &error_abort);
4249 #else
4250 display_type = DT_NONE;
4251 #endif
4254 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
4255 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4256 "for SDL, ignoring option");
4258 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
4259 error_report("-no-quit is only valid for GTK and SDL, "
4260 "ignoring option");
4263 if (display_type == DT_GTK) {
4264 early_gtk_display_init(request_opengl);
4267 if (display_type == DT_SDL) {
4268 sdl_display_early_init(request_opengl);
4271 if (request_opengl == 1 && display_opengl == 0) {
4272 #if defined(CONFIG_OPENGL)
4273 error_report("OpenGL is not supported by the display");
4274 #else
4275 error_report("OpenGL support is disabled");
4276 #endif
4277 exit(1);
4280 page_size_init();
4281 socket_init();
4283 if (qemu_opts_foreach(qemu_find_opts("object"),
4284 user_creatable_add_opts_foreach,
4285 object_create_initial, NULL)) {
4286 exit(1);
4289 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4290 chardev_init_func, NULL, NULL)) {
4291 exit(1);
4294 #ifdef CONFIG_VIRTFS
4295 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4296 fsdev_init_func, NULL, NULL)) {
4297 exit(1);
4299 #endif
4301 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4302 error_report("could not acquire pid file: %s", strerror(errno));
4303 exit(1);
4306 if (qemu_opts_foreach(qemu_find_opts("device"),
4307 device_help_func, NULL, NULL)) {
4308 exit(0);
4311 machine_opts = qemu_get_machine_opts();
4312 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4313 NULL)) {
4314 object_unref(OBJECT(current_machine));
4315 exit(1);
4318 configure_accelerator(current_machine);
4320 if (qtest_chrdev) {
4321 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
4324 machine_opts = qemu_get_machine_opts();
4325 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4326 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4327 kernel_cmdline = qemu_opt_get(machine_opts, "append");
4328 bios_name = qemu_opt_get(machine_opts, "firmware");
4330 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4331 if (opts) {
4332 boot_order = qemu_opt_get(opts, "order");
4333 if (boot_order) {
4334 validate_bootdevices(boot_order, &error_fatal);
4337 boot_once = qemu_opt_get(opts, "once");
4338 if (boot_once) {
4339 validate_bootdevices(boot_once, &error_fatal);
4342 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
4343 boot_strict = qemu_opt_get_bool(opts, "strict", false);
4346 if (!boot_order) {
4347 boot_order = machine_class->default_boot_order;
4350 if (!kernel_cmdline) {
4351 kernel_cmdline = "";
4352 current_machine->kernel_cmdline = (char *)kernel_cmdline;
4355 linux_boot = (kernel_filename != NULL);
4357 if (!linux_boot && *kernel_cmdline != '\0') {
4358 error_report("-append only allowed with -kernel option");
4359 exit(1);
4362 if (!linux_boot && initrd_filename != NULL) {
4363 error_report("-initrd only allowed with -kernel option");
4364 exit(1);
4367 if (!linux_boot && qemu_opt_get(machine_opts, "dtb")) {
4368 error_report("-dtb only allowed with -kernel option");
4369 exit(1);
4372 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4373 /* fall back to the -kernel/-append */
4374 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4377 os_set_line_buffering();
4379 /* spice needs the timers to be initialized by this point */
4380 qemu_spice_init();
4382 cpu_ticks_init();
4383 if (icount_opts) {
4384 if (kvm_enabled() || xen_enabled()) {
4385 error_report("-icount is not allowed with kvm or xen");
4386 exit(1);
4388 configure_icount(icount_opts, &error_abort);
4389 qemu_opts_del(icount_opts);
4392 if (default_net) {
4393 QemuOptsList *net = qemu_find_opts("net");
4394 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4395 #ifdef CONFIG_SLIRP
4396 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4397 #endif
4400 if (net_init_clients() < 0) {
4401 exit(1);
4404 if (qemu_opts_foreach(qemu_find_opts("object"),
4405 user_creatable_add_opts_foreach,
4406 object_create_delayed, NULL)) {
4407 exit(1);
4410 #ifdef CONFIG_TPM
4411 if (tpm_init() < 0) {
4412 exit(1);
4414 #endif
4416 /* init the bluetooth world */
4417 if (foreach_device_config(DEV_BT, bt_parse))
4418 exit(1);
4420 if (!xen_enabled()) {
4421 /* On 32-bit hosts, QEMU is limited by virtual address space */
4422 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
4423 error_report("at most 2047 MB RAM can be simulated");
4424 exit(1);
4428 blk_mig_init();
4429 ram_mig_init();
4431 /* If the currently selected machine wishes to override the units-per-bus
4432 * property of its default HBA interface type, do so now. */
4433 if (machine_class->units_per_default_bus) {
4434 override_max_devs(machine_class->block_default_type,
4435 machine_class->units_per_default_bus);
4438 /* open the virtual block devices */
4439 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4440 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4441 NULL, NULL);
4443 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
4444 &machine_class->block_default_type, NULL)) {
4445 exit(1);
4448 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
4449 CDROM_OPTS);
4450 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4451 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4453 parse_numa_opts(machine_class);
4455 if (qemu_opts_foreach(qemu_find_opts("mon"),
4456 mon_init_func, NULL, NULL)) {
4457 exit(1);
4460 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4461 exit(1);
4462 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4463 exit(1);
4464 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4465 exit(1);
4466 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4467 exit(1);
4469 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4470 exit(1);
4472 /* If no default VGA is requested, the default is "none". */
4473 if (default_vga) {
4474 if (machine_class->default_display) {
4475 vga_model = machine_class->default_display;
4476 } else if (vga_interface_available(VGA_CIRRUS)) {
4477 vga_model = "cirrus";
4478 } else if (vga_interface_available(VGA_STD)) {
4479 vga_model = "std";
4482 if (vga_model) {
4483 select_vgahw(vga_model);
4486 if (watchdog) {
4487 i = select_watchdog(watchdog);
4488 if (i > 0)
4489 exit (i == 1 ? 1 : 0);
4492 machine_register_compat_props(current_machine);
4494 qemu_opts_foreach(qemu_find_opts("global"),
4495 global_init_func, NULL, NULL);
4497 /* This checkpoint is required by replay to separate prior clock
4498 reading from the other reads, because timer polling functions query
4499 clock values from the log. */
4500 replay_checkpoint(CHECKPOINT_INIT);
4501 qdev_machine_init();
4503 current_machine->ram_size = ram_size;
4504 current_machine->maxram_size = maxram_size;
4505 current_machine->ram_slots = ram_slots;
4506 current_machine->boot_order = boot_order;
4507 current_machine->cpu_model = cpu_model;
4509 machine_class->init(current_machine);
4511 realtime_init();
4513 audio_init();
4515 cpu_synchronize_all_post_init();
4517 numa_post_machine_init();
4519 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4520 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4521 exit(1);
4524 /* init USB devices */
4525 if (machine_usb(current_machine)) {
4526 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4527 exit(1);
4530 /* Check if IGD GFX passthrough. */
4531 igd_gfx_passthru();
4533 /* init generic devices */
4534 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
4535 if (qemu_opts_foreach(qemu_find_opts("device"),
4536 device_init_func, NULL, NULL)) {
4537 exit(1);
4539 rom_reset_order_override();
4541 /* Did we create any drives that we failed to create a device for? */
4542 drive_check_orphaned();
4544 /* Don't warn about the default network setup that you get if
4545 * no command line -net or -netdev options are specified. There
4546 * are two cases that we would otherwise complain about:
4547 * (1) board doesn't support a NIC but the implicit "-net nic"
4548 * requested one
4549 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4550 * sets up a nic that isn't connected to anything.
4552 if (!default_net) {
4553 net_check_clients();
4557 if (boot_once) {
4558 qemu_boot_set(boot_once, &error_fatal);
4559 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4562 ds = init_displaystate();
4564 /* init local displays */
4565 switch (display_type) {
4566 case DT_CURSES:
4567 curses_display_init(ds, full_screen);
4568 break;
4569 case DT_SDL:
4570 sdl_display_init(ds, full_screen, no_frame);
4571 break;
4572 case DT_COCOA:
4573 cocoa_display_init(ds, full_screen);
4574 break;
4575 case DT_GTK:
4576 gtk_display_init(ds, full_screen, grab_on_hover);
4577 break;
4578 default:
4579 break;
4582 /* must be after terminal init, SDL library changes signal handlers */
4583 os_setup_signal_handling();
4585 /* init remote displays */
4586 #ifdef CONFIG_VNC
4587 qemu_opts_foreach(qemu_find_opts("vnc"),
4588 vnc_init_func, NULL, NULL);
4589 #endif
4591 if (using_spice) {
4592 qemu_spice_display_init();
4595 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
4596 exit(1);
4599 qdev_machine_creation_done();
4601 /* TODO: once all bus devices are qdevified, this should be done
4602 * when bus is created by qdev.c */
4603 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4604 qemu_run_machine_init_done_notifiers();
4606 if (rom_check_and_register_reset() != 0) {
4607 error_report("rom check and register reset failed");
4608 exit(1);
4611 replay_start();
4613 /* This checkpoint is required by replay to separate prior clock
4614 reading from the other reads, because timer polling functions query
4615 clock values from the log. */
4616 replay_checkpoint(CHECKPOINT_RESET);
4617 qemu_system_reset(VMRESET_SILENT);
4618 register_global_state();
4619 if (loadvm) {
4620 if (load_vmstate(loadvm) < 0) {
4621 autostart = 0;
4625 qdev_prop_check_globals();
4626 if (vmstate_dump_file) {
4627 /* dump and exit */
4628 dump_vmstate_json_to_file(vmstate_dump_file);
4629 return 0;
4632 if (incoming) {
4633 Error *local_err = NULL;
4634 qemu_start_incoming_migration(incoming, &local_err);
4635 if (local_err) {
4636 error_reportf_err(local_err, "-incoming %s: ", incoming);
4637 exit(1);
4639 } else if (autostart) {
4640 vm_start();
4643 os_setup_post();
4645 trace_init_vcpu_events();
4646 main_loop();
4647 replay_disable_events();
4649 bdrv_close_all();
4650 pause_all_vcpus();
4651 res_free();
4652 #ifdef CONFIG_TPM
4653 tpm_cleanup();
4654 #endif
4656 /* vhost-user must be cleaned up before chardevs. */
4657 net_cleanup();
4658 audio_cleanup();
4659 monitor_cleanup();
4660 qemu_chr_cleanup();
4662 return 0;