]> git.proxmox.com Git - mirror_qemu.git/blame - vl.c
qemu-img: remove dead check
[mirror_qemu.git] / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
0824d6fc 23 */
d38ea87a 24#include "qemu/osdep.h"
67a1de0d 25#include "qemu-version.h"
f348b6d1
VB
26#include "qemu/cutils.h"
27#include "qemu/help_option.h"
cea25275 28#include "qemu/uuid.h"
d40cdb10 29
452dfbef 30#ifdef CONFIG_SECCOMP
9c17d615 31#include "sysemu/seccomp.h"
452dfbef
EO
32#endif
33
8a16d273
TS
34#if defined(CONFIG_VDE)
35#include <libvdeplug.h>
36#endif
37
73332e5c 38#ifdef CONFIG_SDL
59a36a2f 39#if defined(__APPLE__) || defined(main)
6693665a 40#include <SDL.h>
880fec5d 41int qemu_main(int argc, char **argv, char **envp);
42int main(int argc, char **argv)
43{
59a36a2f 44 return qemu_main(argc, argv, NULL);
880fec5d 45}
46#undef main
47#define main qemu_main
96bcd4f8 48#endif
73332e5c 49#endif /* CONFIG_SDL */
0824d6fc 50
5b0753e0
FB
51#ifdef CONFIG_COCOA
52#undef main
53#define main qemu_main
54#endif /* CONFIG_COCOA */
55
69e5bb68 56
d49b6836 57#include "qemu/error-report.h"
c8897e8e 58#include "qemu/sockets.h"
511d2b14
BS
59#include "hw/hw.h"
60#include "hw/boards.h"
a1a9cb0c 61#include "sysemu/accel.h"
511d2b14 62#include "hw/usb.h"
0d09e41a
PB
63#include "hw/i386/pc.h"
64#include "hw/isa/isa.h"
511d2b14 65#include "hw/bt.h"
0d09e41a 66#include "sysemu/watchdog.h"
60d8f328 67#include "hw/smbios/smbios.h"
9f57061c 68#include "hw/acpi/acpi.h"
0d09e41a 69#include "hw/xen/xen.h"
bd3c948d 70#include "hw/qdev.h"
45a50b16 71#include "hw/loader.h"
b4a42f81 72#include "monitor/qdev.h"
dccfcd0e 73#include "sysemu/bt.h"
1422e32d 74#include "net/net.h"
68ac40d2 75#include "net/slirp.h"
83c9089e 76#include "monitor/monitor.h"
28ecbaee 77#include "ui/console.h"
e0d2bd51 78#include "ui/input.h"
9c17d615 79#include "sysemu/sysemu.h"
e35704ba 80#include "sysemu/numa.h"
022c62cb 81#include "exec/gdbstub.h"
1de7afc9 82#include "qemu/timer.h"
dccfcd0e 83#include "sysemu/char.h"
b33276a7 84#include "qemu/bitmap.h"
03dd024f 85#include "qemu/log.h"
9c17d615 86#include "sysemu/blockdev.h"
0d09e41a 87#include "hw/block/block.h"
caf71f86 88#include "migration/block.h"
bdee56f5 89#include "sysemu/tpm.h"
9c17d615 90#include "sysemu/dma.h"
511d2b14 91#include "audio/audio.h"
caf71f86 92#include "migration/migration.h"
4b4629d9 93#include "sysemu/cpus.h"
5821ebf9 94#include "migration/colo.h"
9c17d615 95#include "sysemu/kvm.h"
b0cb0a66 96#include "sysemu/hax.h"
7b1b5d19 97#include "qapi/qmp/qjson.h"
1de7afc9
PB
98#include "qemu/option.h"
99#include "qemu/config-file.h"
59a5264b 100#include "qemu-options.h"
1fa9a5e4 101#include "qmp-commands.h"
1de7afc9 102#include "qemu/main-loop.h"
758e8e38 103#ifdef CONFIG_VIRTFS
74db920c
GS
104#include "fsdev/qemu-fsdev.h"
105#endif
9c17d615 106#include "sysemu/qtest.h"
511d2b14 107
76cad711 108#include "disas/disas.h"
fc01f7e7 109
511d2b14 110
d918f23e 111#include "slirp/libslirp.h"
511d2b14 112
94b0b5ff 113#include "trace.h"
e4858974 114#include "trace/control.h"
1de7afc9 115#include "qemu/queue.h"
9c17d615 116#include "sysemu/arch_init.h"
72cf2d4f 117
29b0040b 118#include "ui/qemu-spice.h"
68d98d3e 119#include "qapi/string-input-visitor.h"
c4090f8e 120#include "qapi/opts-visitor.h"
84321831
WX
121#include "qom/object_interfaces.h"
122#include "qapi-event.h"
cfe67cef 123#include "exec/semihost.h"
ddbb0d09 124#include "crypto/init.h"
b60c48a7 125#include "sysemu/replay.h"
0194749a 126#include "qapi/qmp/qerror.h"
dce8921b 127#include "sysemu/iothread.h"
29b0040b 128
98b19252 129#define MAX_VIRTIO_CONSOLES 1
3ef669e1 130#define MAX_SCLP_CONSOLES 1
98b19252 131
4524051c
GH
132static const char *data_dir[16];
133static int data_dir_idx;
1192dad8 134const char *bios_name = NULL;
cb5a7aa8 135enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
0b71a5d5 136int request_opengl = -1;
da076ffe 137int display_opengl;
3d11d0eb 138const char* keyboard_layout = NULL;
c227f099 139ram_addr_t ram_size;
c902760f 140const char *mem_path = NULL;
c902760f 141int mem_prealloc = 0; /* force preallocation of physical target memory */
28d16f38 142bool enable_mlock = false;
c4b1fcc0 143int nb_nics;
7c9d8e07 144NICInfo nd_table[MAX_NICS];
d399f677 145int autostart;
f6503059
AZ
146static int rtc_utc = 1;
147static int rtc_date_offset = -1; /* -1 means no change */
884f17c2 148QEMUClockType rtc_clock;
64465297 149int vga_interface_type = VGA_NONE;
dbed7e40
BS
150static int full_screen = 0;
151static int no_frame = 0;
667accab 152int no_quit = 0;
881249c7 153static bool grab_on_hover;
8d11df9e 154CharDriverState *serial_hds[MAX_SERIAL_PORTS];
6508fe59 155CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
9ede2fde 156CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
3ef669e1 157CharDriverState *sclp_hds[MAX_SCLP_CONSOLES];
a09db21f 158int win2k_install_hack = 0;
1b530a6d 159int singlestep = 0;
6a00d601 160int smp_cpus = 1;
0544edd8 161int max_cpus = 1;
dc6b1c09
AP
162int smp_cores = 1;
163int smp_threads = 1;
6515b203 164int acpi_enabled = 1;
16b29ae1 165int no_hpet = 0;
52ca8d6a 166int fd_bootchk = 1;
4fdcac0e 167static int no_reboot;
b2f76161 168int no_shutdown = 0;
9467cd46 169int cursor_hide = 1;
a171fe39 170int graphic_rotate = 0;
09aaa160 171const char *watchdog;
2e55e842 172QEMUOptionRom option_rom[MAX_OPTION_ROMS];
9ae02555 173int nb_option_roms;
2b8f2d41 174int old_param = 0;
c35734b2 175const char *qemu_name;
3780e197 176int alt_grab = 0;
0ca9f8a4 177int ctrl_grab = 0;
66508601
BS
178unsigned int nb_prom_envs = 0;
179const char *prom_envs[MAX_PROM_ENVS];
95387491 180int boot_menu;
bc74112f 181bool boot_strict;
3d3b8303 182uint8_t *boot_splash_filedata;
d09acb9b 183size_t boot_splash_filedata_size;
3d3b8303 184uint8_t qemu_extra_params_fw[2];
0824d6fc 185
a8bfac37 186int icount_align_option;
1ca4d09a 187
9c5ce8db 188/* The bytes in qemu_uuid are in the order specified by RFC4122, _not_ in the
caad057b
EH
189 * little-endian "wire format" described in the SMBIOS 2.6 specification.
190 */
9c5ce8db 191QemuUUID qemu_uuid;
fc3b3295 192bool qemu_uuid_set;
8fcb1b90 193
fd42deeb
GH
194static NotifierList exit_notifiers =
195 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
196
4cab946a
GN
197static NotifierList machine_init_done_notifiers =
198 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
199
d5286af5 200bool xen_allowed;
d745bef8
BS
201uint32_t xen_domid;
202enum xen_mode xen_mode = XEN_EMULATE;
203
d44229c5 204static int has_defaults = 1;
998bbd74 205static int default_serial = 1;
6a5e8b0e 206static int default_parallel = 1;
986c5f78 207static int default_virtcon = 1;
3ef669e1 208static int default_sclp = 1;
abdeed06 209static int default_monitor = 1;
ac33f8fa
GH
210static int default_floppy = 1;
211static int default_cdrom = 1;
212static int default_sdcard = 1;
7f1b17f2 213static int default_vga = 1;
d30300f7 214static int default_net = 1;
998bbd74
GH
215
216static struct {
217 const char *driver;
218 int *flag;
219} default_list[] = {
6a5e8b0e
GH
220 { .driver = "isa-serial", .flag = &default_serial },
221 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf 222 { .driver = "isa-fdc", .flag = &default_floppy },
a92bd191 223 { .driver = "floppy", .flag = &default_floppy },
af6bf132
MA
224 { .driver = "ide-cd", .flag = &default_cdrom },
225 { .driver = "ide-hd", .flag = &default_cdrom },
d8bcbabf 226 { .driver = "ide-drive", .flag = &default_cdrom },
af6bf132 227 { .driver = "scsi-cd", .flag = &default_cdrom },
392ecf54 228 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
392ecf54 229 { .driver = "virtio-serial", .flag = &default_virtcon },
7f1b17f2
PB
230 { .driver = "VGA", .flag = &default_vga },
231 { .driver = "isa-vga", .flag = &default_vga },
232 { .driver = "cirrus-vga", .flag = &default_vga },
233 { .driver = "isa-cirrus-vga", .flag = &default_vga },
234 { .driver = "vmware-svga", .flag = &default_vga },
235 { .driver = "qxl-vga", .flag = &default_vga },
a94f0c5c 236 { .driver = "virtio-vga", .flag = &default_vga },
998bbd74
GH
237};
238
4d454574
PB
239static QemuOptsList qemu_rtc_opts = {
240 .name = "rtc",
241 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
242 .desc = {
243 {
244 .name = "base",
245 .type = QEMU_OPT_STRING,
246 },{
247 .name = "clock",
248 .type = QEMU_OPT_STRING,
249 },{
250 .name = "driftfix",
251 .type = QEMU_OPT_STRING,
252 },
253 { /* end of list */ }
254 },
255};
256
257static QemuOptsList qemu_sandbox_opts = {
258 .name = "sandbox",
259 .implied_opt_name = "enable",
260 .head = QTAILQ_HEAD_INITIALIZER(qemu_sandbox_opts.head),
261 .desc = {
262 {
263 .name = "enable",
264 .type = QEMU_OPT_BOOL,
265 },
266 { /* end of list */ }
267 },
268};
269
4d454574
PB
270static QemuOptsList qemu_option_rom_opts = {
271 .name = "option-rom",
272 .implied_opt_name = "romfile",
273 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
274 .desc = {
275 {
276 .name = "bootindex",
277 .type = QEMU_OPT_NUMBER,
278 }, {
279 .name = "romfile",
280 .type = QEMU_OPT_STRING,
281 },
282 { /* end of list */ }
283 },
284};
285
286static QemuOptsList qemu_machine_opts = {
287 .name = "machine",
288 .implied_opt_name = "type",
289 .merge_lists = true,
290 .head = QTAILQ_HEAD_INITIALIZER(qemu_machine_opts.head),
291 .desc = {
49d2e648
MA
292 /*
293 * no elements => accept any
294 * sanity checking will happen later
295 * when setting machine properties
296 */
297 { }
4d454574
PB
298 },
299};
300
301static QemuOptsList qemu_boot_opts = {
302 .name = "boot-opts",
6ef4716c
MA
303 .implied_opt_name = "order",
304 .merge_lists = true,
4d454574
PB
305 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
306 .desc = {
4d454574
PB
307 {
308 .name = "order",
309 .type = QEMU_OPT_STRING,
310 }, {
311 .name = "once",
312 .type = QEMU_OPT_STRING,
313 }, {
314 .name = "menu",
6ef4716c 315 .type = QEMU_OPT_BOOL,
4d454574
PB
316 }, {
317 .name = "splash",
318 .type = QEMU_OPT_STRING,
319 }, {
320 .name = "splash-time",
321 .type = QEMU_OPT_STRING,
322 }, {
323 .name = "reboot-timeout",
324 .type = QEMU_OPT_STRING,
c8a6ae8b
AK
325 }, {
326 .name = "strict",
e5187b56 327 .type = QEMU_OPT_BOOL,
4d454574
PB
328 },
329 { /*End of list */ }
330 },
331};
332
333static QemuOptsList qemu_add_fd_opts = {
334 .name = "add-fd",
335 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
336 .desc = {
337 {
338 .name = "fd",
339 .type = QEMU_OPT_NUMBER,
340 .help = "file descriptor of which a duplicate is added to fd set",
341 },{
342 .name = "set",
343 .type = QEMU_OPT_NUMBER,
344 .help = "ID of the fd set to add fd to",
345 },{
346 .name = "opaque",
347 .type = QEMU_OPT_STRING,
348 .help = "free-form string used to describe fd",
349 },
350 { /* end of list */ }
351 },
352};
353
354static QemuOptsList qemu_object_opts = {
355 .name = "object",
356 .implied_opt_name = "qom-type",
357 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
358 .desc = {
359 { }
360 },
361};
362
d1a0cf73
SB
363static QemuOptsList qemu_tpmdev_opts = {
364 .name = "tpmdev",
365 .implied_opt_name = "type",
366 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
367 .desc = {
bb716238 368 /* options are defined in the TPM backends */
d1a0cf73
SB
369 { /* end of list */ }
370 },
371};
372
888a6bc6
SM
373static QemuOptsList qemu_realtime_opts = {
374 .name = "realtime",
375 .head = QTAILQ_HEAD_INITIALIZER(qemu_realtime_opts.head),
376 .desc = {
377 {
378 .name = "mlock",
379 .type = QEMU_OPT_BOOL,
380 },
381 { /* end of list */ }
382 },
383};
384
5e2ac519
SA
385static QemuOptsList qemu_msg_opts = {
386 .name = "msg",
387 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
388 .desc = {
389 {
390 .name = "timestamp",
391 .type = QEMU_OPT_BOOL,
392 },
393 { /* end of list */ }
394 },
395};
396
5d12f961
DDAG
397static QemuOptsList qemu_name_opts = {
398 .name = "name",
399 .implied_opt_name = "guest",
400 .merge_lists = true,
401 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
402 .desc = {
403 {
404 .name = "guest",
405 .type = QEMU_OPT_STRING,
406 .help = "Sets the name of the guest.\n"
407 "This name will be displayed in the SDL window caption.\n"
408 "The name will also be used for the VNC server",
409 }, {
410 .name = "process",
411 .type = QEMU_OPT_STRING,
412 .help = "Sets the name of the QEMU process, as shown in top etc",
8f480de0
DDAG
413 }, {
414 .name = "debug-threads",
415 .type = QEMU_OPT_BOOL,
416 .help = "When enabled, name the individual threads; defaults off.\n"
417 "NOTE: The thread names are for debugging and not a\n"
418 "stable API.",
5d12f961
DDAG
419 },
420 { /* End of list */ }
421 },
422};
423
6e1d3c1c
IM
424static QemuOptsList qemu_mem_opts = {
425 .name = "memory",
426 .implied_opt_name = "size",
427 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
428 .merge_lists = true,
429 .desc = {
430 {
431 .name = "size",
432 .type = QEMU_OPT_SIZE,
433 },
c270fb9e
IM
434 {
435 .name = "slots",
436 .type = QEMU_OPT_NUMBER,
437 },
438 {
439 .name = "maxmem",
440 .type = QEMU_OPT_SIZE,
441 },
6e1d3c1c
IM
442 { /* end of list */ }
443 },
444};
445
1ad9580b
ST
446static QemuOptsList qemu_icount_opts = {
447 .name = "icount",
448 .implied_opt_name = "shift",
449 .merge_lists = true,
450 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
451 .desc = {
452 {
453 .name = "shift",
454 .type = QEMU_OPT_STRING,
a8bfac37
ST
455 }, {
456 .name = "align",
457 .type = QEMU_OPT_BOOL,
f1f4b57e
VC
458 }, {
459 .name = "sleep",
460 .type = QEMU_OPT_BOOL,
4c27b859
PD
461 }, {
462 .name = "rr",
463 .type = QEMU_OPT_STRING,
464 }, {
465 .name = "rrfile",
466 .type = QEMU_OPT_STRING,
1ad9580b
ST
467 },
468 { /* end of list */ }
469 },
470};
471
a38bb079
LI
472static QemuOptsList qemu_semihosting_config_opts = {
473 .name = "semihosting-config",
474 .implied_opt_name = "enable",
475 .head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
476 .desc = {
477 {
478 .name = "enable",
479 .type = QEMU_OPT_BOOL,
480 }, {
481 .name = "target",
482 .type = QEMU_OPT_STRING,
a59d31a1
LA
483 }, {
484 .name = "arg",
485 .type = QEMU_OPT_STRING,
a38bb079
LI
486 },
487 { /* end of list */ }
488 },
489};
490
81b2b810
GS
491static QemuOptsList qemu_fw_cfg_opts = {
492 .name = "fw_cfg",
493 .implied_opt_name = "name",
494 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
495 .desc = {
496 {
497 .name = "name",
498 .type = QEMU_OPT_STRING,
499 .help = "Sets the fw_cfg name of the blob to be inserted",
500 }, {
501 .name = "file",
502 .type = QEMU_OPT_STRING,
503 .help = "Sets the name of the file from which\n"
504 "the fw_cfg blob will be loaded",
6407d76e
GS
505 }, {
506 .name = "string",
507 .type = QEMU_OPT_STRING,
508 .help = "Sets content of the blob to be inserted from a string",
81b2b810
GS
509 },
510 { /* end of list */ }
511 },
512};
513
f57b4b5f
CL
514#ifdef CONFIG_LIBISCSI
515static QemuOptsList qemu_iscsi_opts = {
516 .name = "iscsi",
517 .head = QTAILQ_HEAD_INITIALIZER(qemu_iscsi_opts.head),
518 .desc = {
519 {
520 .name = "user",
521 .type = QEMU_OPT_STRING,
522 .help = "username for CHAP authentication to target",
523 },{
524 .name = "password",
525 .type = QEMU_OPT_STRING,
526 .help = "password for CHAP authentication to target",
527 },{
528 .name = "password-secret",
529 .type = QEMU_OPT_STRING,
530 .help = "ID of the secret providing password for CHAP "
531 "authentication to target",
532 },{
533 .name = "header-digest",
534 .type = QEMU_OPT_STRING,
535 .help = "HeaderDigest setting. "
536 "{CRC32C|CRC32C-NONE|NONE-CRC32C|NONE}",
537 },{
538 .name = "initiator-name",
539 .type = QEMU_OPT_STRING,
540 .help = "Initiator iqn name to use when connecting",
541 },{
542 .name = "timeout",
543 .type = QEMU_OPT_NUMBER,
544 .help = "Request timeout in seconds (default 0 = no timeout)",
545 },
546 { /* end of list */ }
547 },
548};
549#endif
550
7f9d6e54
MA
551/**
552 * Get machine options
553 *
554 * Returns: machine options (never null).
555 */
556QemuOpts *qemu_get_machine_opts(void)
557{
e96e5ae8 558 return qemu_find_opts_singleton("machine");
7f9d6e54
MA
559}
560
31459f46
RS
561const char *qemu_get_vm_name(void)
562{
563 return qemu_name;
564}
565
3d3b8303
WX
566static void res_free(void)
567{
012aef07
MA
568 g_free(boot_splash_filedata);
569 boot_splash_filedata = NULL;
3d3b8303
WX
570}
571
28d0de7a 572static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
998bbd74
GH
573{
574 const char *driver = qemu_opt_get(opts, "driver");
575 int i;
576
577 if (!driver)
578 return 0;
579 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
580 if (strcmp(default_list[i].driver, driver) != 0)
581 continue;
582 *(default_list[i].flag) = 0;
583 }
584 return 0;
585}
586
f5bbfba1
LC
587/***********************************************************/
588/* QEMU state */
589
0461d5a6 590static RunState current_run_state = RUN_STATE_PRELAUNCH;
f5bbfba1 591
7fb1cf16
EB
592/* We use RUN_STATE__MAX but any invalid value will do */
593static RunState vmstop_requested = RUN_STATE__MAX;
74892d24
PB
594static QemuMutex vmstop_lock;
595
5db9d4d1
LC
596typedef struct {
597 RunState from;
598 RunState to;
599} RunStateTransition;
600
601static const RunStateTransition runstate_transitions_def[] = {
602 /* from -> to */
0461d5a6 603 { RUN_STATE_DEBUG, RUN_STATE_RUNNING },
eca01d3a 604 { RUN_STATE_DEBUG, RUN_STATE_FINISH_MIGRATE },
7ec13c79 605 { RUN_STATE_DEBUG, RUN_STATE_PRELAUNCH },
5db9d4d1 606
ca3fc39e
JQ
607 { RUN_STATE_INMIGRATE, RUN_STATE_INTERNAL_ERROR },
608 { RUN_STATE_INMIGRATE, RUN_STATE_IO_ERROR },
29ed72f1 609 { RUN_STATE_INMIGRATE, RUN_STATE_PAUSED },
ca3fc39e
JQ
610 { RUN_STATE_INMIGRATE, RUN_STATE_RUNNING },
611 { RUN_STATE_INMIGRATE, RUN_STATE_SHUTDOWN },
612 { RUN_STATE_INMIGRATE, RUN_STATE_SUSPENDED },
613 { RUN_STATE_INMIGRATE, RUN_STATE_WATCHDOG },
614 { RUN_STATE_INMIGRATE, RUN_STATE_GUEST_PANICKED },
2a6e6e59 615 { RUN_STATE_INMIGRATE, RUN_STATE_FINISH_MIGRATE },
98799b0d
PB
616 { RUN_STATE_INMIGRATE, RUN_STATE_PRELAUNCH },
617 { RUN_STATE_INMIGRATE, RUN_STATE_POSTMIGRATE },
21142ba7 618 { RUN_STATE_INMIGRATE, RUN_STATE_COLO },
5db9d4d1 619
0461d5a6 620 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PAUSED },
8a9236f1 621 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_FINISH_MIGRATE },
7ec13c79 622 { RUN_STATE_INTERNAL_ERROR, RUN_STATE_PRELAUNCH },
5db9d4d1 623
0461d5a6 624 { RUN_STATE_IO_ERROR, RUN_STATE_RUNNING },
8a9236f1 625 { RUN_STATE_IO_ERROR, RUN_STATE_FINISH_MIGRATE },
7ec13c79 626 { RUN_STATE_IO_ERROR, RUN_STATE_PRELAUNCH },
5db9d4d1 627
0461d5a6 628 { RUN_STATE_PAUSED, RUN_STATE_RUNNING },
8a9236f1 629 { RUN_STATE_PAUSED, RUN_STATE_FINISH_MIGRATE },
7ec13c79 630 { RUN_STATE_PAUSED, RUN_STATE_PRELAUNCH },
21142ba7 631 { RUN_STATE_PAUSED, RUN_STATE_COLO},
5db9d4d1 632
0461d5a6 633 { RUN_STATE_POSTMIGRATE, RUN_STATE_RUNNING },
8a9236f1 634 { RUN_STATE_POSTMIGRATE, RUN_STATE_FINISH_MIGRATE },
7ec13c79 635 { RUN_STATE_POSTMIGRATE, RUN_STATE_PRELAUNCH },
5db9d4d1 636
0461d5a6 637 { RUN_STATE_PRELAUNCH, RUN_STATE_RUNNING },
8a9236f1 638 { RUN_STATE_PRELAUNCH, RUN_STATE_FINISH_MIGRATE },
0461d5a6 639 { RUN_STATE_PRELAUNCH, RUN_STATE_INMIGRATE },
5db9d4d1 640
0461d5a6
LC
641 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_RUNNING },
642 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_POSTMIGRATE },
7ec13c79 643 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_PRELAUNCH },
21142ba7 644 { RUN_STATE_FINISH_MIGRATE, RUN_STATE_COLO},
5db9d4d1 645
0461d5a6 646 { RUN_STATE_RESTORE_VM, RUN_STATE_RUNNING },
7ec13c79 647 { RUN_STATE_RESTORE_VM, RUN_STATE_PRELAUNCH },
5db9d4d1 648
21142ba7
HZ
649 { RUN_STATE_COLO, RUN_STATE_RUNNING },
650
0461d5a6
LC
651 { RUN_STATE_RUNNING, RUN_STATE_DEBUG },
652 { RUN_STATE_RUNNING, RUN_STATE_INTERNAL_ERROR },
653 { RUN_STATE_RUNNING, RUN_STATE_IO_ERROR },
654 { RUN_STATE_RUNNING, RUN_STATE_PAUSED },
655 { RUN_STATE_RUNNING, RUN_STATE_FINISH_MIGRATE },
656 { RUN_STATE_RUNNING, RUN_STATE_RESTORE_VM },
657 { RUN_STATE_RUNNING, RUN_STATE_SAVE_VM },
658 { RUN_STATE_RUNNING, RUN_STATE_SHUTDOWN },
659 { RUN_STATE_RUNNING, RUN_STATE_WATCHDOG },
ede085b3 660 { RUN_STATE_RUNNING, RUN_STATE_GUEST_PANICKED },
21142ba7 661 { RUN_STATE_RUNNING, RUN_STATE_COLO},
5db9d4d1 662
0461d5a6 663 { RUN_STATE_SAVE_VM, RUN_STATE_RUNNING },
5db9d4d1 664
0461d5a6 665 { RUN_STATE_SHUTDOWN, RUN_STATE_PAUSED },
8a9236f1 666 { RUN_STATE_SHUTDOWN, RUN_STATE_FINISH_MIGRATE },
7ec13c79 667 { RUN_STATE_SHUTDOWN, RUN_STATE_PRELAUNCH },
5db9d4d1 668
ad02b96a
LC
669 { RUN_STATE_DEBUG, RUN_STATE_SUSPENDED },
670 { RUN_STATE_RUNNING, RUN_STATE_SUSPENDED },
671 { RUN_STATE_SUSPENDED, RUN_STATE_RUNNING },
672 { RUN_STATE_SUSPENDED, RUN_STATE_FINISH_MIGRATE },
7ec13c79 673 { RUN_STATE_SUSPENDED, RUN_STATE_PRELAUNCH },
21142ba7 674 { RUN_STATE_SUSPENDED, RUN_STATE_COLO},
ad02b96a 675
0461d5a6 676 { RUN_STATE_WATCHDOG, RUN_STATE_RUNNING },
8a9236f1 677 { RUN_STATE_WATCHDOG, RUN_STATE_FINISH_MIGRATE },
7ec13c79 678 { RUN_STATE_WATCHDOG, RUN_STATE_PRELAUNCH },
21142ba7 679 { RUN_STATE_WATCHDOG, RUN_STATE_COLO},
5db9d4d1 680
df390768 681 { RUN_STATE_GUEST_PANICKED, RUN_STATE_RUNNING },
fd2a2e1c 682 { RUN_STATE_GUEST_PANICKED, RUN_STATE_FINISH_MIGRATE },
7ec13c79 683 { RUN_STATE_GUEST_PANICKED, RUN_STATE_PRELAUNCH },
ede085b3 684
7fb1cf16 685 { RUN_STATE__MAX, RUN_STATE__MAX },
5db9d4d1
LC
686};
687
7fb1cf16 688static bool runstate_valid_transitions[RUN_STATE__MAX][RUN_STATE__MAX];
0461d5a6 689
f5bbfba1
LC
690bool runstate_check(RunState state)
691{
692 return current_run_state == state;
693}
694
5e0f1940
JQ
695bool runstate_store(char *str, size_t size)
696{
697 const char *state = RunState_lookup[current_run_state];
698 size_t len = strlen(state) + 1;
699
700 if (len > size) {
701 return false;
702 }
703 memcpy(str, state, len);
704 return true;
705}
706
4fdcac0e 707static void runstate_init(void)
5db9d4d1
LC
708{
709 const RunStateTransition *p;
710
711 memset(&runstate_valid_transitions, 0, sizeof(runstate_valid_transitions));
7fb1cf16 712 for (p = &runstate_transitions_def[0]; p->from != RUN_STATE__MAX; p++) {
5db9d4d1
LC
713 runstate_valid_transitions[p->from][p->to] = true;
714 }
74892d24
PB
715
716 qemu_mutex_init(&vmstop_lock);
5db9d4d1
LC
717}
718
719/* This function will abort() on invalid state transitions */
f5bbfba1
LC
720void runstate_set(RunState new_state)
721{
7fb1cf16 722 assert(new_state < RUN_STATE__MAX);
207c5cd2 723
e92a2d9c
LZ
724 if (current_run_state == new_state) {
725 return;
726 }
727
207c5cd2 728 if (!runstate_valid_transitions[current_run_state][new_state]) {
f61eddcb
EH
729 error_report("invalid runstate transition: '%s' -> '%s'",
730 RunState_lookup[current_run_state],
731 RunState_lookup[new_state]);
5db9d4d1
LC
732 abort();
733 }
7e866003 734 trace_runstate_set(new_state);
f5bbfba1
LC
735 current_run_state = new_state;
736}
737
1fa9a5e4 738int runstate_is_running(void)
9e37b9dc 739{
1fa9a5e4 740 return runstate_check(RUN_STATE_RUNNING);
9e37b9dc
LC
741}
742
ede085b3
HT
743bool runstate_needs_reset(void)
744{
745 return runstate_check(RUN_STATE_INTERNAL_ERROR) ||
df390768 746 runstate_check(RUN_STATE_SHUTDOWN);
ede085b3
HT
747}
748
1fa9a5e4 749StatusInfo *qmp_query_status(Error **errp)
1354869c 750{
1fa9a5e4
LC
751 StatusInfo *info = g_malloc0(sizeof(*info));
752
753 info->running = runstate_is_running();
754 info->singlestep = singlestep;
755 info->status = current_run_state;
756
757 return info;
1354869c
LC
758}
759
74892d24
PB
760static bool qemu_vmstop_requested(RunState *r)
761{
762 qemu_mutex_lock(&vmstop_lock);
763 *r = vmstop_requested;
7fb1cf16 764 vmstop_requested = RUN_STATE__MAX;
74892d24 765 qemu_mutex_unlock(&vmstop_lock);
7fb1cf16 766 return *r < RUN_STATE__MAX;
74892d24
PB
767}
768
769void qemu_system_vmstop_request_prepare(void)
770{
771 qemu_mutex_lock(&vmstop_lock);
772}
773
774void qemu_system_vmstop_request(RunState state)
775{
776 vmstop_requested = state;
777 qemu_mutex_unlock(&vmstop_lock);
778 qemu_notify_event();
779}
780
781void vm_start(void)
782{
783 RunState requested;
784
785 qemu_vmstop_requested(&requested);
7fb1cf16 786 if (runstate_is_running() && requested == RUN_STATE__MAX) {
74892d24
PB
787 return;
788 }
789
790 /* Ensure that a STOP/RESUME pair of events is emitted if a
791 * vmstop request was pending. The BLOCK_IO_ERROR event, for
792 * example, according to documentation is always followed by
793 * the STOP event.
794 */
795 if (runstate_is_running()) {
591c48fb 796 qapi_event_send_stop(&error_abort);
74892d24 797 } else {
6d0ceb80 798 replay_enable_events();
74892d24
PB
799 cpu_enable_ticks();
800 runstate_set(RUN_STATE_RUNNING);
801 vm_state_notify(1, RUN_STATE_RUNNING);
802 resume_all_vcpus();
803 }
804
591c48fb 805 qapi_event_send_resume(&error_abort);
74892d24
PB
806}
807
808
8f0056b7
PB
809/***********************************************************/
810/* real time host monotonic timer */
09b26c5e 811
2ed1ebcf
PD
812static time_t qemu_time(void)
813{
814 return qemu_clock_get_ms(QEMU_CLOCK_HOST) / 1000;
815}
816
f6503059
AZ
817/***********************************************************/
818/* host time/date access */
819void qemu_get_timedate(struct tm *tm, int offset)
820{
2ed1ebcf 821 time_t ti = qemu_time();
f6503059 822
f6503059
AZ
823 ti += offset;
824 if (rtc_date_offset == -1) {
825 if (rtc_utc)
eb7ff6fb 826 gmtime_r(&ti, tm);
f6503059 827 else
eb7ff6fb 828 localtime_r(&ti, tm);
f6503059
AZ
829 } else {
830 ti -= rtc_date_offset;
eb7ff6fb 831 gmtime_r(&ti, tm);
f6503059 832 }
f6503059
AZ
833}
834
835int qemu_timedate_diff(struct tm *tm)
836{
837 time_t seconds;
838
839 if (rtc_date_offset == -1)
840 if (rtc_utc)
841 seconds = mktimegm(tm);
f54c556c
GN
842 else {
843 struct tm tmp = *tm;
844 tmp.tm_isdst = -1; /* use timezone to figure it out */
845 seconds = mktime(&tmp);
846 }
f6503059
AZ
847 else
848 seconds = mktimegm(tm) + rtc_date_offset;
849
2ed1ebcf 850 return seconds - qemu_time();
f6503059
AZ
851}
852
1ed2fc1f
JK
853static void configure_rtc_date_offset(const char *startdate, int legacy)
854{
855 time_t rtc_start_date;
856 struct tm tm;
857
858 if (!strcmp(startdate, "now") && legacy) {
859 rtc_date_offset = -1;
860 } else {
861 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
862 &tm.tm_year,
863 &tm.tm_mon,
864 &tm.tm_mday,
865 &tm.tm_hour,
866 &tm.tm_min,
867 &tm.tm_sec) == 6) {
868 /* OK */
869 } else if (sscanf(startdate, "%d-%d-%d",
870 &tm.tm_year,
871 &tm.tm_mon,
872 &tm.tm_mday) == 3) {
873 tm.tm_hour = 0;
874 tm.tm_min = 0;
875 tm.tm_sec = 0;
876 } else {
877 goto date_fail;
878 }
879 tm.tm_year -= 1900;
880 tm.tm_mon--;
881 rtc_start_date = mktimegm(&tm);
882 if (rtc_start_date == -1) {
883 date_fail:
f61eddcb
EH
884 error_report("invalid date format");
885 error_printf("valid formats: "
886 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1ed2fc1f
JK
887 exit(1);
888 }
2ed1ebcf 889 rtc_date_offset = qemu_time() - rtc_start_date;
1ed2fc1f
JK
890 }
891}
892
893static void configure_rtc(QemuOpts *opts)
894{
895 const char *value;
896
897 value = qemu_opt_get(opts, "base");
898 if (value) {
899 if (!strcmp(value, "utc")) {
900 rtc_utc = 1;
901 } else if (!strcmp(value, "localtime")) {
0194749a 902 Error *blocker = NULL;
1ed2fc1f 903 rtc_utc = 0;
0194749a
PD
904 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED,
905 "-rtc base=localtime");
906 replay_add_blocker(blocker);
1ed2fc1f
JK
907 } else {
908 configure_rtc_date_offset(value, 0);
909 }
910 }
6875204c
JK
911 value = qemu_opt_get(opts, "clock");
912 if (value) {
913 if (!strcmp(value, "host")) {
884f17c2 914 rtc_clock = QEMU_CLOCK_HOST;
78808141 915 } else if (!strcmp(value, "rt")) {
884f17c2 916 rtc_clock = QEMU_CLOCK_REALTIME;
6875204c 917 } else if (!strcmp(value, "vm")) {
884f17c2 918 rtc_clock = QEMU_CLOCK_VIRTUAL;
6875204c 919 } else {
f61eddcb 920 error_report("invalid option value '%s'", value);
6875204c
JK
921 exit(1);
922 }
923 }
1ed2fc1f
JK
924 value = qemu_opt_get(opts, "driftfix");
925 if (value) {
7e4c0336 926 if (!strcmp(value, "slew")) {
16714b16
EH
927 static GlobalProperty slew_lost_ticks = {
928 .driver = "mc146818rtc",
929 .property = "lost_tick_policy",
930 .value = "slew",
433acf0d
JK
931 };
932
16714b16 933 qdev_prop_register_global(&slew_lost_ticks);
7e4c0336 934 } else if (!strcmp(value, "none")) {
433acf0d 935 /* discard is default */
1ed2fc1f 936 } else {
f61eddcb 937 error_report("invalid option value '%s'", value);
1ed2fc1f
JK
938 exit(1);
939 }
940 }
1ed2fc1f
JK
941}
942
1ae26a18
AZ
943/***********************************************************/
944/* Bluetooth support */
945static int nb_hcis;
946static int cur_hci;
947static struct HCIInfo *hci_table[MAX_NICS];
dc72ac14 948
1ae26a18
AZ
949struct HCIInfo *qemu_next_hci(void)
950{
951 if (cur_hci == nb_hcis)
952 return &null_hci;
953
954 return hci_table[cur_hci++];
955}
956
dc72ac14
AZ
957static int bt_hci_parse(const char *str)
958{
959 struct HCIInfo *hci;
c227f099 960 bdaddr_t bdaddr;
dc72ac14
AZ
961
962 if (nb_hcis >= MAX_NICS) {
3e515373 963 error_report("too many bluetooth HCIs (max %i)", MAX_NICS);
dc72ac14
AZ
964 return -1;
965 }
966
967 hci = hci_init(str);
968 if (!hci)
969 return -1;
970
971 bdaddr.b[0] = 0x52;
972 bdaddr.b[1] = 0x54;
973 bdaddr.b[2] = 0x00;
974 bdaddr.b[3] = 0x12;
975 bdaddr.b[4] = 0x34;
976 bdaddr.b[5] = 0x56 + nb_hcis;
977 hci->bdaddr_set(hci, bdaddr.b);
978
979 hci_table[nb_hcis++] = hci;
980
981 return 0;
982}
983
984static void bt_vhci_add(int vlan_id)
985{
986 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
987
988 if (!vlan->slave)
f61eddcb
EH
989 error_report("warning: adding a VHCI to an empty scatternet %i",
990 vlan_id);
dc72ac14
AZ
991
992 bt_vhci_init(bt_new_hci(vlan));
993}
994
995static struct bt_device_s *bt_device_add(const char *opt)
996{
997 struct bt_scatternet_s *vlan;
998 int vlan_id = 0;
999 char *endp = strstr(opt, ",vlan=");
1000 int len = (endp ? endp - opt : strlen(opt)) + 1;
1001 char devname[10];
1002
1003 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1004
1005 if (endp) {
1006 vlan_id = strtol(endp + 6, &endp, 0);
1007 if (*endp) {
f61eddcb 1008 error_report("unrecognised bluetooth vlan Id");
dc72ac14
AZ
1009 return 0;
1010 }
1011 }
1012
1013 vlan = qemu_find_bt_vlan(vlan_id);
1014
1015 if (!vlan->slave)
f61eddcb
EH
1016 error_report("warning: adding a slave device to an empty scatternet %i",
1017 vlan_id);
dc72ac14
AZ
1018
1019 if (!strcmp(devname, "keyboard"))
1020 return bt_keyboard_init(vlan);
1021
4cd70f34 1022 error_report("unsupported bluetooth device '%s'", devname);
dc72ac14
AZ
1023 return 0;
1024}
1025
1026static int bt_parse(const char *opt)
1027{
1028 const char *endp, *p;
1029 int vlan;
1030
1031 if (strstart(opt, "hci", &endp)) {
1032 if (!*endp || *endp == ',') {
1033 if (*endp)
1034 if (!strstart(endp, ",vlan=", 0))
1035 opt = endp + 1;
1036
1037 return bt_hci_parse(opt);
1038 }
1039 } else if (strstart(opt, "vhci", &endp)) {
1040 if (!*endp || *endp == ',') {
1041 if (*endp) {
1042 if (strstart(endp, ",vlan=", &p)) {
1043 vlan = strtol(p, (char **) &endp, 0);
1044 if (*endp) {
f61eddcb 1045 error_report("bad scatternet '%s'", p);
dc72ac14
AZ
1046 return 1;
1047 }
1048 } else {
f61eddcb 1049 error_report("bad parameter '%s'", endp + 1);
dc72ac14
AZ
1050 return 1;
1051 }
1052 } else
1053 vlan = 0;
1054
1055 bt_vhci_add(vlan);
1056 return 0;
1057 }
1058 } else if (strstart(opt, "device:", &endp))
1059 return !bt_device_add(endp);
1060
f61eddcb 1061 error_report("bad bluetooth parameter '%s'", opt);
dc72ac14
AZ
1062 return 1;
1063}
1064
28d0de7a 1065static int parse_sandbox(void *opaque, QemuOpts *opts, Error **errp)
7d76ad4f
EO
1066{
1067 /* FIXME: change this to true for 1.3 */
1068 if (qemu_opt_get_bool(opts, "enable", false)) {
1069#ifdef CONFIG_SECCOMP
1070 if (seccomp_start() < 0) {
1459407e
MA
1071 error_report("failed to install seccomp syscall filter "
1072 "in the kernel");
7d76ad4f
EO
1073 return -1;
1074 }
1075#else
5dfdae81 1076 error_report("seccomp support is disabled");
7d76ad4f
EO
1077 return -1;
1078#endif
1079 }
1080
1081 return 0;
1082}
1083
28d0de7a 1084static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
5d12f961
DDAG
1085{
1086 const char *proc_name;
1087
8f480de0
DDAG
1088 if (qemu_opt_get(opts, "debug-threads")) {
1089 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
1090 }
5d12f961
DDAG
1091 qemu_name = qemu_opt_get(opts, "guest");
1092
1093 proc_name = qemu_opt_get(opts, "process");
1094 if (proc_name) {
1095 os_set_proc_name(proc_name);
1096 }
5b9d313e
DDAG
1097
1098 return 0;
5d12f961
DDAG
1099}
1100
f8b6f8ed
MA
1101bool defaults_enabled(void)
1102{
1103 return has_defaults;
1104}
1105
587ed6be 1106#ifndef _WIN32
28d0de7a 1107static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
1108{
1109 int fd, dupfd, flags;
1110 int64_t fdset_id;
1111 const char *fd_opaque = NULL;
636a30a8 1112 AddfdInfo *fdinfo;
587ed6be
CB
1113
1114 fd = qemu_opt_get_number(opts, "fd", -1);
1115 fdset_id = qemu_opt_get_number(opts, "set", -1);
1116 fd_opaque = qemu_opt_get(opts, "opaque");
1117
1118 if (fd < 0) {
1459407e 1119 error_report("fd option is required and must be non-negative");
587ed6be
CB
1120 return -1;
1121 }
1122
1123 if (fd <= STDERR_FILENO) {
1459407e 1124 error_report("fd cannot be a standard I/O stream");
587ed6be
CB
1125 return -1;
1126 }
1127
1128 /*
1129 * All fds inherited across exec() necessarily have FD_CLOEXEC
1130 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
1131 */
1132 flags = fcntl(fd, F_GETFD);
1133 if (flags == -1 || (flags & FD_CLOEXEC)) {
1459407e 1134 error_report("fd is not valid or already in use");
587ed6be
CB
1135 return -1;
1136 }
1137
1138 if (fdset_id < 0) {
1459407e 1139 error_report("set option is required and must be non-negative");
587ed6be
CB
1140 return -1;
1141 }
1142
1143#ifdef F_DUPFD_CLOEXEC
1144 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
1145#else
1146 dupfd = dup(fd);
1147 if (dupfd != -1) {
1148 qemu_set_cloexec(dupfd);
1149 }
1150#endif
1151 if (dupfd == -1) {
3e515373 1152 error_report("error duplicating fd: %s", strerror(errno));
587ed6be
CB
1153 return -1;
1154 }
1155
1156 /* add the duplicate fd, and optionally the opaque string, to the fd set */
636a30a8
PB
1157 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, !!fd_opaque, fd_opaque,
1158 &error_abort);
1159 g_free(fdinfo);
587ed6be
CB
1160
1161 return 0;
1162}
1163
28d0de7a 1164static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
1165{
1166 int fd;
1167
1168 fd = qemu_opt_get_number(opts, "fd", -1);
1169 close(fd);
1170
1171 return 0;
1172}
1173#endif
1174
1ae26a18
AZ
1175/***********************************************************/
1176/* QEMU Block devices */
1177
2292ddae
MA
1178#define HD_OPTS "media=disk"
1179#define CDROM_OPTS "media=cdrom"
1180#define FD_OPTS ""
1181#define PFLASH_OPTS ""
1182#define MTD_OPTS ""
1183#define SD_OPTS ""
e4bcb14c 1184
28d0de7a 1185static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 1186{
2d0d2837 1187 BlockInterfaceType *block_default_type = opaque;
9dfd7c7a 1188
60e19e06 1189 return drive_new(opts, *block_default_type) == NULL;
9dfd7c7a
GH
1190}
1191
28d0de7a 1192static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 1193{
28de2f88 1194 if (qemu_opt_get(opts, "snapshot") == NULL) {
f43e47db 1195 qemu_opt_set(opts, "snapshot", "on", &error_abort);
9dfd7c7a
GH
1196 }
1197 return 0;
1198}
1199
3c42ea66
CB
1200static void default_drive(int enable, int snapshot, BlockInterfaceType type,
1201 int index, const char *optstr)
4e5d9b57
MA
1202{
1203 QemuOpts *opts;
a66c9dc7 1204 DriveInfo *dinfo;
4e5d9b57 1205
4e5d9b57
MA
1206 if (!enable || drive_get_by_index(type, index)) {
1207 return;
1208 }
1209
1210 opts = drive_add(type, index, NULL, optstr);
1211 if (snapshot) {
28d0de7a 1212 drive_enable_snapshot(NULL, opts, NULL);
4e5d9b57 1213 }
a66c9dc7
JS
1214
1215 dinfo = drive_new(opts, type);
1216 if (!dinfo) {
4e5d9b57
MA
1217 exit(1);
1218 }
a66c9dc7
JS
1219 dinfo->is_default = true;
1220
4e5d9b57
MA
1221}
1222
12b7f57e
MT
1223static QemuOptsList qemu_smp_opts = {
1224 .name = "smp-opts",
1225 .implied_opt_name = "cpus",
1226 .merge_lists = true,
1227 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
1228 .desc = {
1229 {
1230 .name = "cpus",
1231 .type = QEMU_OPT_NUMBER,
1232 }, {
1233 .name = "sockets",
1234 .type = QEMU_OPT_NUMBER,
1235 }, {
1236 .name = "cores",
1237 .type = QEMU_OPT_NUMBER,
1238 }, {
1239 .name = "threads",
1240 .type = QEMU_OPT_NUMBER,
1241 }, {
1242 .name = "maxcpus",
1243 .type = QEMU_OPT_NUMBER,
1244 },
1245 { /*End of list */ }
1246 },
1247};
1248
1249static void smp_parse(QemuOpts *opts)
dc6b1c09 1250{
12b7f57e 1251 if (opts) {
12b7f57e
MT
1252 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
1253 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
1254 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
1255 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
1256
1257 /* compute missing values, prefer sockets over cores over threads */
1258 if (cpus == 0 || sockets == 0) {
1259 sockets = sockets > 0 ? sockets : 1;
1260 cores = cores > 0 ? cores : 1;
dc6b1c09 1261 threads = threads > 0 ? threads : 1;
12b7f57e
MT
1262 if (cpus == 0) {
1263 cpus = cores * threads * sockets;
1264 }
719cac1c
EH
1265 } else if (cores == 0) {
1266 threads = threads > 0 ? threads : 1;
1267 cores = cpus / (sockets * threads);
66f37d36 1268 cores = cores > 0 ? cores : 1;
ec2cbbdd 1269 } else if (threads == 0) {
719cac1c 1270 threads = cpus / (cores * sockets);
66f37d36 1271 threads = threads > 0 ? threads : 1;
ec2cbbdd 1272 } else if (sockets * cores * threads < cpus) {
f61eddcb
EH
1273 error_report("cpu topology: "
1274 "sockets (%u) * cores (%u) * threads (%u) < "
1275 "smp_cpus (%u)",
1276 sockets, cores, threads, cpus);
ec2cbbdd 1277 exit(1);
dc6b1c09 1278 }
12b7f57e 1279
a32ef3bf 1280 max_cpus = qemu_opt_get_number(opts, "maxcpus", cpus);
0544edd8 1281
0544edd8
AJ
1282 if (max_cpus < cpus) {
1283 error_report("maxcpus must be equal to or greater than smp");
1284 exit(1);
1285 }
1286
a32ef3bf 1287 if (sockets * cores * threads > max_cpus) {
f61eddcb
EH
1288 error_report("cpu topology: "
1289 "sockets (%u) * cores (%u) * threads (%u) > "
1290 "maxcpus (%u)",
1291 sockets, cores, threads, max_cpus);
a32ef3bf
TH
1292 exit(1);
1293 }
12b7f57e
MT
1294
1295 smp_cpus = cpus;
0544edd8
AJ
1296 smp_cores = cores;
1297 smp_threads = threads;
12b7f57e
MT
1298 }
1299
0544edd8 1300 if (smp_cpus > 1) {
0194749a
PD
1301 Error *blocker = NULL;
1302 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
1303 replay_add_blocker(blocker);
1304 }
dc6b1c09
AP
1305}
1306
28d16f38 1307static void realtime_init(void)
888a6bc6 1308{
888a6bc6
SM
1309 if (enable_mlock) {
1310 if (os_mlock() < 0) {
f61eddcb 1311 error_report("locking memory failed");
888a6bc6
SM
1312 exit(1);
1313 }
1314 }
1315}
1316
5e2ac519
SA
1317
1318static void configure_msg(QemuOpts *opts)
1319{
1320 enable_timestamp_msg = qemu_opt_get_bool(opts, "timestamp", true);
1321}
1322
cfe67cef
LA
1323/***********************************************************/
1324/* Semihosting */
1325
1326typedef struct SemihostingConfig {
1327 bool enabled;
1328 SemihostingTarget target;
a59d31a1
LA
1329 const char **argv;
1330 int argc;
1331 const char *cmdline; /* concatenated argv */
cfe67cef
LA
1332} SemihostingConfig;
1333
1334static SemihostingConfig semihosting;
1335
1336bool semihosting_enabled(void)
1337{
1338 return semihosting.enabled;
1339}
1340
1341SemihostingTarget semihosting_get_target(void)
1342{
1343 return semihosting.target;
1344}
1345
a59d31a1
LA
1346const char *semihosting_get_arg(int i)
1347{
1348 if (i >= semihosting.argc) {
1349 return NULL;
1350 }
1351 return semihosting.argv[i];
1352}
1353
1354int semihosting_get_argc(void)
1355{
1356 return semihosting.argc;
1357}
1358
1359const char *semihosting_get_cmdline(void)
1360{
1361 if (semihosting.cmdline == NULL && semihosting.argc > 0) {
1362 semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
1363 }
1364 return semihosting.cmdline;
1365}
1366
1367static int add_semihosting_arg(void *opaque,
1368 const char *name, const char *val,
1369 Error **errp)
1370{
1371 SemihostingConfig *s = opaque;
1372 if (strcmp(name, "arg") == 0) {
1373 s->argc++;
1374 /* one extra element as g_strjoinv() expects NULL-terminated array */
1375 s->argv = g_realloc(s->argv, (s->argc + 1) * sizeof(void *));
1376 s->argv[s->argc - 1] = val;
1377 s->argv[s->argc] = NULL;
1378 }
1379 return 0;
1380}
1381
1382/* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
1383static inline void semihosting_arg_fallback(const char *file, const char *cmd)
1384{
1385 char *cmd_token;
1386
1387 /* argv[0] */
1388 add_semihosting_arg(&semihosting, "arg", file, NULL);
1389
1390 /* split -append and initialize argv[1..n] */
1391 cmd_token = strtok(g_strdup(cmd), " ");
1392 while (cmd_token) {
1393 add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
1394 cmd_token = strtok(NULL, " ");
1395 }
1396}
1397
79814179
TC
1398/* Now we still need this for compatibility with XEN. */
1399bool has_igd_gfx_passthru;
1400static void igd_gfx_passthru(void)
1401{
1402 has_igd_gfx_passthru = current_machine->igd_gfx_passthru;
1403}
1404
a594cfbf
FB
1405/***********************************************************/
1406/* USB devices */
1407
fb08000c 1408static int usb_device_add(const char *devname)
a594cfbf 1409{
a5d2f727 1410 USBDevice *dev = NULL;
615fe4de
MR
1411#ifndef CONFIG_LINUX
1412 const char *p;
1413#endif
a594cfbf 1414
4bcbe0b6 1415 if (!machine_usb(current_machine)) {
a594cfbf 1416 return -1;
094b287f 1417 }
a594cfbf 1418
0958b4cc
GH
1419 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1420 dev = usbdevice_create(devname);
1421 if (dev)
1422 goto done;
1423
a5d2f727 1424 /* the other ones */
e447fc63
GH
1425#ifndef CONFIG_LINUX
1426 /* only the linux version is qdev-ified, usb-bsd still needs this */
a594cfbf 1427 if (strstart(devname, "host:", &p)) {
3741715c 1428 dev = usb_host_device_open(usb_bus_find(-1), p);
a594cfbf 1429 }
615fe4de 1430#endif
0d92ed30
PB
1431 if (!dev)
1432 return -1;
1433
a5d2f727 1434done:
a594cfbf
FB
1435 return 0;
1436}
1437
1f3870ab
AL
1438static int usb_device_del(const char *devname)
1439{
1440 int bus_num, addr;
1441 const char *p;
1442
1a3973b3
GH
1443 if (strstart(devname, "host:", &p)) {
1444 return -1;
1445 }
5d0c5750 1446
4bcbe0b6 1447 if (!machine_usb(current_machine)) {
1f3870ab 1448 return -1;
094b287f 1449 }
1f3870ab
AL
1450
1451 p = strchr(devname, '.');
1452 if (!p)
1453 return -1;
1454 bus_num = strtoul(devname, NULL, 0);
1455 addr = strtoul(p + 1, NULL, 0);
1456
a5d2f727 1457 return usb_device_delete_addr(bus_num, addr);
1f3870ab
AL
1458}
1459
bd3c948d
GH
1460static int usb_parse(const char *cmdline)
1461{
59d1c1c2 1462 int r;
fb08000c 1463 r = usb_device_add(cmdline);
59d1c1c2 1464 if (r < 0) {
f61eddcb 1465 error_report("could not add USB device '%s'", cmdline);
59d1c1c2
ST
1466 }
1467 return r;
bd3c948d
GH
1468}
1469
3e5a50d6 1470void hmp_usb_add(Monitor *mon, const QDict *qdict)
a594cfbf 1471{
59d1c1c2 1472 const char *devname = qdict_get_str(qdict, "devname");
fb08000c 1473 if (usb_device_add(devname) < 0) {
1ecda02b 1474 error_report("could not add USB device '%s'", devname);
59d1c1c2 1475 }
a594cfbf
FB
1476}
1477
3e5a50d6 1478void hmp_usb_del(Monitor *mon, const QDict *qdict)
a594cfbf 1479{
59d1c1c2
ST
1480 const char *devname = qdict_get_str(qdict, "devname");
1481 if (usb_device_del(devname) < 0) {
1ecda02b 1482 error_report("could not delete USB device '%s'", devname);
59d1c1c2 1483 }
a594cfbf
FB
1484}
1485
cc1daa40
FB
1486/***********************************************************/
1487/* machine registration */
1488
0056ae24 1489MachineState *current_machine;
cc1daa40 1490
0056ae24 1491static MachineClass *find_machine(const char *name)
cc1daa40 1492{
261747f1 1493 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
0056ae24 1494 MachineClass *mc = NULL;
cc1daa40 1495
261747f1 1496 for (el = machines; el; el = el->next) {
0056ae24 1497 MachineClass *temp = el->data;
261747f1 1498
958db90c 1499 if (!strcmp(temp->name, name)) {
0056ae24 1500 mc = temp;
261747f1
MA
1501 break;
1502 }
958db90c
MA
1503 if (temp->alias &&
1504 !strcmp(temp->alias, name)) {
0056ae24 1505 mc = temp;
261747f1
MA
1506 break;
1507 }
cc1daa40 1508 }
261747f1
MA
1509
1510 g_slist_free(machines);
0056ae24 1511 return mc;
cc1daa40
FB
1512}
1513
0056ae24 1514MachineClass *find_default_machine(void)
0c257437 1515{
261747f1 1516 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
0056ae24 1517 MachineClass *mc = NULL;
0c257437 1518
261747f1 1519 for (el = machines; el; el = el->next) {
0056ae24 1520 MachineClass *temp = el->data;
261747f1 1521
958db90c 1522 if (temp->is_default) {
0056ae24 1523 mc = temp;
261747f1 1524 break;
0c257437
AL
1525 }
1526 }
261747f1
MA
1527
1528 g_slist_free(machines);
0056ae24 1529 return mc;
0c257437
AL
1530}
1531
01d3c80d
AL
1532MachineInfoList *qmp_query_machines(Error **errp)
1533{
261747f1 1534 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
01d3c80d 1535 MachineInfoList *mach_list = NULL;
01d3c80d 1536
261747f1
MA
1537 for (el = machines; el; el = el->next) {
1538 MachineClass *mc = el->data;
01d3c80d
AL
1539 MachineInfoList *entry;
1540 MachineInfo *info;
1541
1542 info = g_malloc0(sizeof(*info));
958db90c 1543 if (mc->is_default) {
01d3c80d
AL
1544 info->has_is_default = true;
1545 info->is_default = true;
1546 }
1547
958db90c 1548 if (mc->alias) {
01d3c80d 1549 info->has_alias = true;
958db90c 1550 info->alias = g_strdup(mc->alias);
01d3c80d
AL
1551 }
1552
958db90c
MA
1553 info->name = g_strdup(mc->name);
1554 info->cpu_max = !mc->max_cpus ? 1 : mc->max_cpus;
62c9467d 1555 info->hotpluggable_cpus = !!mc->query_hotpluggable_cpus;
01d3c80d
AL
1556
1557 entry = g_malloc0(sizeof(*entry));
1558 entry->value = info;
1559 entry->next = mach_list;
1560 mach_list = entry;
1561 }
1562
261747f1 1563 g_slist_free(machines);
01d3c80d
AL
1564 return mach_list;
1565}
1566
52eb3dfd
MA
1567static int machine_help_func(QemuOpts *opts, MachineState *machine)
1568{
1569 ObjectProperty *prop;
7746abd8 1570 ObjectPropertyIterator iter;
52eb3dfd
MA
1571
1572 if (!qemu_opt_has_help_opt(opts)) {
1573 return 0;
1574 }
1575
7746abd8
DB
1576 object_property_iter_init(&iter, OBJECT(machine));
1577 while ((prop = object_property_iter_next(&iter))) {
52eb3dfd
MA
1578 if (!prop->set) {
1579 continue;
1580 }
1581
1582 error_printf("%s.%s=%s", MACHINE_GET_CLASS(machine)->name,
1583 prop->name, prop->type);
1584 if (prop->description) {
1585 error_printf(" (%s)\n", prop->description);
1586 } else {
1587 error_printf("\n");
1588 }
1589 }
1590
1591 return 1;
1592}
1593
8a7ddc38
FB
1594/***********************************************************/
1595/* main execution loop */
1596
0bd48850
FB
1597struct vm_change_state_entry {
1598 VMChangeStateHandler *cb;
1599 void *opaque;
72cf2d4f 1600 QLIST_ENTRY (vm_change_state_entry) entries;
0bd48850
FB
1601};
1602
72cf2d4f 1603static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
0bd48850
FB
1604
1605VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1606 void *opaque)
1607{
1608 VMChangeStateEntry *e;
1609
7267c094 1610 e = g_malloc0(sizeof (*e));
0bd48850
FB
1611
1612 e->cb = cb;
1613 e->opaque = opaque;
72cf2d4f 1614 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
0bd48850
FB
1615 return e;
1616}
1617
1618void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1619{
72cf2d4f 1620 QLIST_REMOVE (e, entries);
7267c094 1621 g_free (e);
0bd48850
FB
1622}
1623
1dfb4dd9 1624void vm_state_notify(int running, RunState state)
0bd48850 1625{
9b10ac86 1626 VMChangeStateEntry *e, *next;
0bd48850 1627
1dfb4dd9 1628 trace_vm_state_notify(running, state);
94b0b5ff 1629
9b10ac86 1630 QLIST_FOREACH_SAFE(e, &vm_change_state_head, entries, next) {
1dfb4dd9 1631 e->cb(e->opaque, running, state);
0bd48850
FB
1632 }
1633}
1634
bb0c6722 1635static int reset_requested;
f64622c4
GN
1636static int shutdown_requested, shutdown_signal = -1;
1637static pid_t shutdown_pid;
3475187d 1638static int powerdown_requested;
8cf71710 1639static int debug_requested;
95b363b5 1640static int suspend_requested;
4bc78a87 1641static WakeupReason wakeup_reason;
a9552c8e
IM
1642static NotifierList powerdown_notifiers =
1643 NOTIFIER_LIST_INITIALIZER(powerdown_notifiers);
95b363b5
GH
1644static NotifierList suspend_notifiers =
1645 NOTIFIER_LIST_INITIALIZER(suspend_notifiers);
1646static NotifierList wakeup_notifiers =
1647 NOTIFIER_LIST_INITIALIZER(wakeup_notifiers);
4bc78a87 1648static uint32_t wakeup_reason_mask = ~(1 << QEMU_WAKEUP_REASON_NONE);
bb0c6722 1649
1291eb35
AP
1650int qemu_shutdown_requested_get(void)
1651{
1652 return shutdown_requested;
1653}
1654
1655int qemu_reset_requested_get(void)
1656{
1657 return reset_requested;
1658}
1659
4fdcac0e 1660static int qemu_shutdown_requested(void)
cf7a2fe2 1661{
817ef04d 1662 return atomic_xchg(&shutdown_requested, 0);
cf7a2fe2
AJ
1663}
1664
4fdcac0e 1665static void qemu_kill_report(void)
f64622c4 1666{
35b30712 1667 if (!qtest_driver() && shutdown_signal != -1) {
f1d3fb04
PM
1668 if (shutdown_pid == 0) {
1669 /* This happens for eg ^C at the terminal, so it's worth
1670 * avoiding printing an odd message in that case.
1671 */
f61eddcb 1672 error_report("terminating on signal %d", shutdown_signal);
f1d3fb04 1673 } else {
fbe7e332
MP
1674 char *shutdown_cmd = qemu_get_pid_name(shutdown_pid);
1675
1676 error_report("terminating on signal %d from pid " FMT_pid " (%s)",
1677 shutdown_signal, shutdown_pid,
1678 shutdown_cmd ? shutdown_cmd : "<unknown process>");
1679 g_free(shutdown_cmd);
f1d3fb04 1680 }
f64622c4
GN
1681 shutdown_signal = -1;
1682 }
1683}
1684
4fdcac0e 1685static int qemu_reset_requested(void)
cf7a2fe2
AJ
1686{
1687 int r = reset_requested;
8bd7f71d
PD
1688 if (r && replay_checkpoint(CHECKPOINT_RESET_REQUESTED)) {
1689 reset_requested = 0;
1690 return r;
1691 }
1692 return false;
cf7a2fe2
AJ
1693}
1694
95b363b5
GH
1695static int qemu_suspend_requested(void)
1696{
1697 int r = suspend_requested;
8bd7f71d
PD
1698 if (r && replay_checkpoint(CHECKPOINT_SUSPEND_REQUESTED)) {
1699 suspend_requested = 0;
1700 return r;
1701 }
1702 return false;
95b363b5
GH
1703}
1704
4bc78a87 1705static WakeupReason qemu_wakeup_requested(void)
14058196 1706{
4bc78a87 1707 return wakeup_reason;
14058196
LC
1708}
1709
4fdcac0e 1710static int qemu_powerdown_requested(void)
cf7a2fe2
AJ
1711{
1712 int r = powerdown_requested;
1713 powerdown_requested = 0;
1714 return r;
1715}
1716
e568902a
AL
1717static int qemu_debug_requested(void)
1718{
1719 int r = debug_requested;
1720 debug_requested = 0;
1721 return r;
1722}
1723
be522029
DG
1724void qemu_system_reset(bool report)
1725{
0056ae24
MA
1726 MachineClass *mc;
1727
1728 mc = current_machine ? MACHINE_GET_CLASS(current_machine) : NULL;
1729
efdaf797
DG
1730 cpu_synchronize_all_states();
1731
958db90c
MA
1732 if (mc && mc->reset) {
1733 mc->reset();
be522029
DG
1734 } else {
1735 qemu_devices_reset();
1736 }
e063eb1f 1737 if (report) {
a6330785 1738 qapi_event_send_reset(&error_abort);
e063eb1f 1739 }
ea375f9a 1740 cpu_synchronize_all_post_reset();
bb0c6722
FB
1741}
1742
5f5b5942
AS
1743void qemu_system_guest_panicked(void)
1744{
bac05aa9
AS
1745 if (current_cpu) {
1746 current_cpu->crash_occurred = true;
1747 }
5f5b5942
AS
1748 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_PAUSE, &error_abort);
1749 vm_stop(RUN_STATE_GUEST_PANICKED);
864111f4
CB
1750 if (!no_shutdown) {
1751 qapi_event_send_guest_panicked(GUEST_PANIC_ACTION_POWEROFF,
1752 &error_abort);
1753 qemu_system_shutdown_request();
1754 }
5f5b5942
AS
1755}
1756
bb0c6722
FB
1757void qemu_system_reset_request(void)
1758{
d1beab82
FB
1759 if (no_reboot) {
1760 shutdown_requested = 1;
1761 } else {
1762 reset_requested = 1;
1763 }
b4a3d965 1764 cpu_stop_current();
d9f75a4e 1765 qemu_notify_event();
bb0c6722
FB
1766}
1767
95b363b5
GH
1768static void qemu_system_suspend(void)
1769{
1770 pause_all_vcpus();
1771 notifier_list_notify(&suspend_notifiers, NULL);
ad02b96a 1772 runstate_set(RUN_STATE_SUSPENDED);
1d11a95a 1773 qapi_event_send_suspend(&error_abort);
95b363b5
GH
1774}
1775
1776void qemu_system_suspend_request(void)
1777{
9abc62f6 1778 if (runstate_check(RUN_STATE_SUSPENDED)) {
95b363b5
GH
1779 return;
1780 }
1781 suspend_requested = 1;
1782 cpu_stop_current();
1783 qemu_notify_event();
1784}
1785
1786void qemu_register_suspend_notifier(Notifier *notifier)
1787{
1788 notifier_list_add(&suspend_notifiers, notifier);
1789}
1790
1791void qemu_system_wakeup_request(WakeupReason reason)
1792{
4fed9421
AK
1793 trace_system_wakeup_request(reason);
1794
9abc62f6 1795 if (!runstate_check(RUN_STATE_SUSPENDED)) {
95b363b5
GH
1796 return;
1797 }
1798 if (!(wakeup_reason_mask & (1 << reason))) {
1799 return;
1800 }
ad02b96a 1801 runstate_set(RUN_STATE_RUNNING);
4bc78a87 1802 wakeup_reason = reason;
95b363b5 1803 qemu_notify_event();
95b363b5
GH
1804}
1805
1806void qemu_system_wakeup_enable(WakeupReason reason, bool enabled)
1807{
1808 if (enabled) {
1809 wakeup_reason_mask |= (1 << reason);
1810 } else {
1811 wakeup_reason_mask &= ~(1 << reason);
1812 }
1813}
1814
1815void qemu_register_wakeup_notifier(Notifier *notifier)
1816{
1817 notifier_list_add(&wakeup_notifiers, notifier);
1818}
1819
f64622c4
GN
1820void qemu_system_killed(int signal, pid_t pid)
1821{
1822 shutdown_signal = signal;
1823 shutdown_pid = pid;
d9389b96 1824 no_shutdown = 0;
8bd7f71d
PD
1825
1826 /* Cannot call qemu_system_shutdown_request directly because
1827 * we are in a signal handler.
1828 */
1829 shutdown_requested = 1;
1830 qemu_notify_event();
f64622c4
GN
1831}
1832
bb0c6722
FB
1833void qemu_system_shutdown_request(void)
1834{
bc78cff9 1835 trace_qemu_system_shutdown_request();
b60c48a7 1836 replay_shutdown_request();
bb0c6722 1837 shutdown_requested = 1;
d9f75a4e 1838 qemu_notify_event();
bb0c6722
FB
1839}
1840
013c2f15
IM
1841static void qemu_system_powerdown(void)
1842{
0aab9ec3 1843 qapi_event_send_powerdown(&error_abort);
013c2f15
IM
1844 notifier_list_notify(&powerdown_notifiers, NULL);
1845}
1846
3475187d
FB
1847void qemu_system_powerdown_request(void)
1848{
bc78cff9 1849 trace_qemu_system_powerdown_request();
3475187d 1850 powerdown_requested = 1;
d9f75a4e
AL
1851 qemu_notify_event();
1852}
1853
a9552c8e
IM
1854void qemu_register_powerdown_notifier(Notifier *notifier)
1855{
1856 notifier_list_add(&powerdown_notifiers, notifier);
1857}
1858
8cf71710
JK
1859void qemu_system_debug_request(void)
1860{
1861 debug_requested = 1;
83f338f7 1862 qemu_notify_event();
8cf71710
JK
1863}
1864
99435906
PB
1865static bool main_loop_should_exit(void)
1866{
1867 RunState r;
1868 if (qemu_debug_requested()) {
1869 vm_stop(RUN_STATE_DEBUG);
1870 }
95b363b5
GH
1871 if (qemu_suspend_requested()) {
1872 qemu_system_suspend();
1873 }
99435906
PB
1874 if (qemu_shutdown_requested()) {
1875 qemu_kill_report();
84321831 1876 qapi_event_send_shutdown(&error_abort);
99435906
PB
1877 if (no_shutdown) {
1878 vm_stop(RUN_STATE_SHUTDOWN);
1879 } else {
1880 return true;
1881 }
1882 }
1883 if (qemu_reset_requested()) {
1884 pause_all_vcpus();
99435906
PB
1885 qemu_system_reset(VMRESET_REPORT);
1886 resume_all_vcpus();
7ec13c79
DL
1887 if (!runstate_check(RUN_STATE_RUNNING) &&
1888 !runstate_check(RUN_STATE_INMIGRATE)) {
1889 runstate_set(RUN_STATE_PRELAUNCH);
99435906
PB
1890 }
1891 }
14058196
LC
1892 if (qemu_wakeup_requested()) {
1893 pause_all_vcpus();
14058196 1894 qemu_system_reset(VMRESET_SILENT);
4bc78a87
LJ
1895 notifier_list_notify(&wakeup_notifiers, &wakeup_reason);
1896 wakeup_reason = QEMU_WAKEUP_REASON_NONE;
14058196 1897 resume_all_vcpus();
7a906f7f 1898 qapi_event_send_wakeup(&error_abort);
14058196 1899 }
99435906 1900 if (qemu_powerdown_requested()) {
013c2f15 1901 qemu_system_powerdown();
99435906
PB
1902 }
1903 if (qemu_vmstop_requested(&r)) {
1904 vm_stop(r);
1905 }
1906 return false;
1907}
1908
43b96858
AL
1909static void main_loop(void)
1910{
c9f711a5 1911 bool nonblocking;
99435906 1912 int last_io = 0;
8e1b90ec
JK
1913#ifdef CONFIG_PROFILER
1914 int64_t ti;
1915#endif
99435906 1916 do {
b0cb0a66 1917 nonblocking = tcg_enabled() && last_io > 0;
89bfc105 1918#ifdef CONFIG_PROFILER
46481d39 1919 ti = profile_getclock();
89bfc105 1920#endif
c9f711a5 1921 last_io = main_loop_wait(nonblocking);
89bfc105 1922#ifdef CONFIG_PROFILER
46481d39 1923 dev_time += profile_getclock() - ti;
89bfc105 1924#endif
99435906 1925 } while (!main_loop_should_exit());
b4608c04
FB
1926}
1927
9bd7e6d9
PB
1928static void version(void)
1929{
0781dd6e 1930 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION "\n"
d915b7bb 1931 QEMU_COPYRIGHT "\n");
9bd7e6d9
PB
1932}
1933
15f82208 1934static void help(int exitcode)
0824d6fc 1935{
a3adb7ad
ME
1936 version();
1937 printf("usage: %s [options] [disk_image]\n\n"
1938 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
1939 error_get_progname());
1940
77bd1119
ME
1941#define QEMU_OPTIONS_GENERATE_HELP
1942#include "qemu-options-wrapper.h"
a3adb7ad
ME
1943
1944 printf("\nDuring emulation, the following keys are useful:\n"
3f020d70 1945 "ctrl-alt-f toggle full screen\n"
1946 "ctrl-alt-n switch to virtual console 'n'\n"
1947 "ctrl-alt toggle mouse and keyboard grab\n"
1948 "\n"
a3adb7ad
ME
1949 "When using -nographic, press 'ctrl-a h' to get some help.\n");
1950
15f82208 1951 exit(exitcode);
0824d6fc
FB
1952}
1953
cd6f1169
FB
1954#define HAS_ARG 0x0001
1955
cd6f1169
FB
1956typedef struct QEMUOption {
1957 const char *name;
1958 int flags;
1959 int index;
ad96090a 1960 uint32_t arch_mask;
cd6f1169
FB
1961} QEMUOption;
1962
dbed7e40 1963static const QEMUOption qemu_options[] = {
ad96090a 1964 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
77bd1119
ME
1965#define QEMU_OPTIONS_GENERATE_OPTIONS
1966#include "qemu-options-wrapper.h"
cd6f1169 1967 { NULL },
fc01f7e7 1968};
a369da5f 1969
8c9a2b71
EH
1970typedef struct VGAInterfaceInfo {
1971 const char *opt_name; /* option name */
1972 const char *name; /* human-readable name */
c2c7b22d
EH
1973 /* Class names indicating that support is available.
1974 * If no class is specified, the interface is always available */
1975 const char *class_names[2];
8c9a2b71
EH
1976} VGAInterfaceInfo;
1977
1978static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
1979 [VGA_NONE] = {
1980 .opt_name = "none",
1981 },
1982 [VGA_STD] = {
1983 .opt_name = "std",
1984 .name = "standard VGA",
c2c7b22d 1985 .class_names = { "VGA", "isa-vga" },
8c9a2b71
EH
1986 },
1987 [VGA_CIRRUS] = {
1988 .opt_name = "cirrus",
1989 .name = "Cirrus VGA",
c2c7b22d 1990 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
8c9a2b71
EH
1991 },
1992 [VGA_VMWARE] = {
1993 .opt_name = "vmware",
1994 .name = "VMWare SVGA",
c2c7b22d 1995 .class_names = { "vmware-svga" },
8c9a2b71
EH
1996 },
1997 [VGA_VIRTIO] = {
1998 .opt_name = "virtio",
1999 .name = "Virtio VGA",
c2c7b22d 2000 .class_names = { "virtio-vga" },
8c9a2b71
EH
2001 },
2002 [VGA_QXL] = {
2003 .opt_name = "qxl",
2004 .name = "QXL VGA",
c2c7b22d 2005 .class_names = { "qxl-vga" },
8c9a2b71
EH
2006 },
2007 [VGA_TCX] = {
2008 .opt_name = "tcx",
2009 .name = "TCX framebuffer",
c2c7b22d 2010 .class_names = { "SUNW,tcx" },
8c9a2b71
EH
2011 },
2012 [VGA_CG3] = {
2013 .opt_name = "cg3",
2014 .name = "CG3 framebuffer",
c2c7b22d 2015 .class_names = { "cgthree" },
8c9a2b71
EH
2016 },
2017 [VGA_XENFB] = {
2018 .opt_name = "xenfb",
2019 },
2020};
2021
c2c7b22d
EH
2022static bool vga_interface_available(VGAInterfaceType t)
2023{
2024 VGAInterfaceInfo *ti = &vga_interfaces[t];
2025
2026 assert(t < VGA_TYPE_MAX);
2027 return !ti->class_names[0] ||
2028 object_class_by_name(ti->class_names[0]) ||
2029 object_class_by_name(ti->class_names[1]);
2030}
2031
8c9a2b71 2032static void select_vgahw(const char *p)
3893c124 2033{
2034 const char *opts;
8c9a2b71 2035 int t;
3893c124 2036
d44229c5 2037 assert(vga_interface_type == VGA_NONE);
8c9a2b71
EH
2038 for (t = 0; t < VGA_TYPE_MAX; t++) {
2039 VGAInterfaceInfo *ti = &vga_interfaces[t];
2040 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
c2c7b22d 2041 if (!vga_interface_available(t)) {
8c9a2b71
EH
2042 error_report("%s not available", ti->name);
2043 exit(1);
2044 }
2045 vga_interface_type = t;
2046 break;
af87bf29 2047 }
8c9a2b71
EH
2048 }
2049 if (t == VGA_TYPE_MAX) {
3893c124 2050 invalid_vga:
3e515373 2051 error_report("unknown vga type: %s", p);
3893c124 2052 exit(1);
2053 }
cb5a7aa8 2054 while (*opts) {
2055 const char *nextopt;
2056
2057 if (strstart(opts, ",retrace=", &nextopt)) {
2058 opts = nextopt;
2059 if (strstart(opts, "dumb", &nextopt))
2060 vga_retrace_method = VGA_RETRACE_DUMB;
2061 else if (strstart(opts, "precise", &nextopt))
2062 vga_retrace_method = VGA_RETRACE_PRECISE;
2063 else goto invalid_vga;
2064 } else goto invalid_vga;
2065 opts = nextopt;
2066 }
3893c124 2067}
2068
0cb48c46
EH
2069typedef enum DisplayType {
2070 DT_DEFAULT,
2071 DT_CURSES,
2072 DT_SDL,
2073 DT_COCOA,
2074 DT_GTK,
2075 DT_NONE,
2076} DisplayType;
2077
1472a95b
JS
2078static DisplayType select_display(const char *p)
2079{
2080 const char *opts;
2081 DisplayType display = DT_DEFAULT;
2082
2083 if (strstart(p, "sdl", &opts)) {
2084#ifdef CONFIG_SDL
2085 display = DT_SDL;
2086 while (*opts) {
2087 const char *nextopt;
2088
2089 if (strstart(opts, ",frame=", &nextopt)) {
2090 opts = nextopt;
2091 if (strstart(opts, "on", &nextopt)) {
2092 no_frame = 0;
2093 } else if (strstart(opts, "off", &nextopt)) {
2094 no_frame = 1;
2095 } else {
05175535 2096 goto invalid_sdl_args;
1472a95b
JS
2097 }
2098 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
2099 opts = nextopt;
2100 if (strstart(opts, "on", &nextopt)) {
2101 alt_grab = 1;
2102 } else if (strstart(opts, "off", &nextopt)) {
2103 alt_grab = 0;
2104 } else {
05175535 2105 goto invalid_sdl_args;
1472a95b
JS
2106 }
2107 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
2108 opts = nextopt;
2109 if (strstart(opts, "on", &nextopt)) {
2110 ctrl_grab = 1;
2111 } else if (strstart(opts, "off", &nextopt)) {
2112 ctrl_grab = 0;
2113 } else {
05175535 2114 goto invalid_sdl_args;
1472a95b
JS
2115 }
2116 } else if (strstart(opts, ",window_close=", &nextopt)) {
2117 opts = nextopt;
2118 if (strstart(opts, "on", &nextopt)) {
2119 no_quit = 0;
2120 } else if (strstart(opts, "off", &nextopt)) {
2121 no_quit = 1;
2122 } else {
05175535 2123 goto invalid_sdl_args;
1472a95b 2124 }
0b71a5d5
GH
2125 } else if (strstart(opts, ",gl=", &nextopt)) {
2126 opts = nextopt;
2127 if (strstart(opts, "on", &nextopt)) {
2128 request_opengl = 1;
2129 } else if (strstart(opts, "off", &nextopt)) {
2130 request_opengl = 0;
2131 } else {
2132 goto invalid_sdl_args;
2133 }
1472a95b 2134 } else {
05175535 2135 invalid_sdl_args:
3e515373 2136 error_report("invalid SDL option string");
05175535 2137 exit(1);
1472a95b
JS
2138 }
2139 opts = nextopt;
2140 }
2141#else
c6bf0f7f 2142 error_report("SDL support is disabled");
1472a95b
JS
2143 exit(1);
2144#endif
3264ff12 2145 } else if (strstart(p, "vnc", &opts)) {
4db14629 2146 if (*opts == '=') {
7b1ee0f2 2147 vnc_parse(opts + 1, &error_fatal);
4db14629 2148 } else {
c6bf0f7f 2149 error_report("VNC requires a display argument vnc=<display>");
3264ff12
JS
2150 exit(1);
2151 }
1472a95b
JS
2152 } else if (strstart(p, "curses", &opts)) {
2153#ifdef CONFIG_CURSES
2154 display = DT_CURSES;
2155#else
3e515373 2156 error_report("curses support is disabled");
1472a95b 2157 exit(1);
15546425
AL
2158#endif
2159 } else if (strstart(p, "gtk", &opts)) {
2160#ifdef CONFIG_GTK
2161 display = DT_GTK;
881249c7
JK
2162 while (*opts) {
2163 const char *nextopt;
2164
2165 if (strstart(opts, ",grab_on_hover=", &nextopt)) {
2166 opts = nextopt;
2167 if (strstart(opts, "on", &nextopt)) {
2168 grab_on_hover = true;
2169 } else if (strstart(opts, "off", &nextopt)) {
2170 grab_on_hover = false;
2171 } else {
2172 goto invalid_gtk_args;
2173 }
97edf3bd
GH
2174 } else if (strstart(opts, ",gl=", &nextopt)) {
2175 opts = nextopt;
2176 if (strstart(opts, "on", &nextopt)) {
2177 request_opengl = 1;
2178 } else if (strstart(opts, "off", &nextopt)) {
2179 request_opengl = 0;
2180 } else {
2181 goto invalid_gtk_args;
2182 }
881249c7
JK
2183 } else {
2184 invalid_gtk_args:
3e515373 2185 error_report("invalid GTK option string");
881249c7
JK
2186 exit(1);
2187 }
2188 opts = nextopt;
2189 }
15546425 2190#else
c6bf0f7f 2191 error_report("GTK support is disabled");
15546425 2192 exit(1);
1472a95b 2193#endif
4171d32e
JS
2194 } else if (strstart(p, "none", &opts)) {
2195 display = DT_NONE;
1472a95b 2196 } else {
3e515373 2197 error_report("unknown display type");
1472a95b
JS
2198 exit(1);
2199 }
2200
2201 return display;
2202}
2203
7d4c3d53
MA
2204static int balloon_parse(const char *arg)
2205{
382f0743 2206 QemuOpts *opts;
7d4c3d53 2207
382f0743
GH
2208 if (strcmp(arg, "none") == 0) {
2209 return 0;
2210 }
2211
2212 if (!strncmp(arg, "virtio", 6)) {
2213 if (arg[6] == ',') {
2214 /* have params -> parse them */
70b94331
MA
2215 opts = qemu_opts_parse_noisily(qemu_find_opts("device"), arg + 7,
2216 false);
382f0743
GH
2217 if (!opts)
2218 return -1;
2219 } else {
2220 /* create empty opts */
87ea75d5
PC
2221 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
2222 &error_abort);
7d4c3d53 2223 }
f43e47db 2224 qemu_opt_set(opts, "driver", "virtio-balloon", &error_abort);
382f0743 2225 return 0;
7d4c3d53 2226 }
382f0743
GH
2227
2228 return -1;
7d4c3d53 2229}
7d4c3d53 2230
5cea8590
PB
2231char *qemu_find_file(int type, const char *name)
2232{
4524051c 2233 int i;
5cea8590
PB
2234 const char *subdir;
2235 char *buf;
2236
31783203
PM
2237 /* Try the name as a straight path first */
2238 if (access(name, R_OK) == 0) {
4524051c 2239 trace_load_file(name, name);
7267c094 2240 return g_strdup(name);
5cea8590 2241 }
4524051c 2242
5cea8590
PB
2243 switch (type) {
2244 case QEMU_FILE_TYPE_BIOS:
2245 subdir = "";
2246 break;
2247 case QEMU_FILE_TYPE_KEYMAP:
2248 subdir = "keymaps/";
2249 break;
2250 default:
2251 abort();
2252 }
4524051c
GH
2253
2254 for (i = 0; i < data_dir_idx; i++) {
2255 buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
2256 if (access(buf, R_OK) == 0) {
2257 trace_load_file(name, buf);
2258 return buf;
2259 }
7267c094 2260 g_free(buf);
5cea8590 2261 }
4524051c 2262 return NULL;
5cea8590
PB
2263}
2264
6407d76e
GS
2265static inline bool nonempty_str(const char *str)
2266{
2267 return str && *str;
2268}
2269
81b2b810
GS
2270static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
2271{
2272 gchar *buf;
2273 size_t size;
6407d76e 2274 const char *name, *file, *str;
bab47d9a 2275 FWCfgState *fw_cfg = (FWCfgState *) opaque;
81b2b810 2276
bab47d9a 2277 if (fw_cfg == NULL) {
81b2b810
GS
2278 error_report("fw_cfg device not available");
2279 return -1;
2280 }
2281 name = qemu_opt_get(opts, "name");
2282 file = qemu_opt_get(opts, "file");
6407d76e
GS
2283 str = qemu_opt_get(opts, "string");
2284
2285 /* we need name and either a file or the content string */
2286 if (!(nonempty_str(name) && (nonempty_str(file) || nonempty_str(str)))) {
2287 error_report("invalid argument(s)");
2288 return -1;
2289 }
2290 if (nonempty_str(file) && nonempty_str(str)) {
2291 error_report("file and string are mutually exclusive");
81b2b810
GS
2292 return -1;
2293 }
2294 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
2295 error_report("name too long (max. %d char)", FW_CFG_MAX_FILE_PATH - 1);
2296 return -1;
2297 }
2298 if (strncmp(name, "opt/", 4) != 0) {
eb177ec1 2299 error_report("warning: externally provided fw_cfg item names "
8afb9000 2300 "should be prefixed with \"opt/\"");
81b2b810 2301 }
6407d76e
GS
2302 if (nonempty_str(str)) {
2303 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
2304 buf = g_memdup(str, size);
2305 } else {
2306 if (!g_file_get_contents(file, &buf, &size, NULL)) {
2307 error_report("can't load %s", file);
2308 return -1;
2309 }
81b2b810 2310 }
bab47d9a
GH
2311 /* For legacy, keep user files in a specific global order. */
2312 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
2313 fw_cfg_add_file(fw_cfg, name, buf, size);
2314 fw_cfg_reset_order_override(fw_cfg);
81b2b810
GS
2315 return 0;
2316}
2317
28d0de7a 2318static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
ff952ba2
MA
2319{
2320 return qdev_device_help(opts);
2321}
2322
28d0de7a 2323static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
f31d07d1 2324{
f006cf7f 2325 Error *err = NULL;
f31d07d1
GH
2326 DeviceState *dev;
2327
f006cf7f
MA
2328 dev = qdev_device_add(opts, &err);
2329 if (!dev) {
2330 error_report_err(err);
f31d07d1 2331 return -1;
f006cf7f 2332 }
b09995ae 2333 object_unref(OBJECT(dev));
f31d07d1
GH
2334 return 0;
2335}
2336
28d0de7a 2337static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1a688d3b 2338{
bd2d80b2 2339 Error *local_err = NULL;
1a688d3b 2340
b4948be9 2341 qemu_chr_new_from_opts(opts, &local_err);
84d18f06 2342 if (local_err) {
565f65d2 2343 error_report_err(local_err);
1a688d3b 2344 return -1;
bd2d80b2 2345 }
1a688d3b
GH
2346 return 0;
2347}
2348
758e8e38 2349#ifdef CONFIG_VIRTFS
28d0de7a 2350static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
74db920c 2351{
9be38598 2352 return qemu_fsdev_add(opts);
74db920c
GS
2353}
2354#endif
2355
28d0de7a 2356static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
88589343
GH
2357{
2358 CharDriverState *chr;
2359 const char *chardev;
2360 const char *mode;
2361 int flags;
2362
2363 mode = qemu_opt_get(opts, "mode");
2364 if (mode == NULL) {
2365 mode = "readline";
2366 }
2367 if (strcmp(mode, "readline") == 0) {
2368 flags = MONITOR_USE_READLINE;
2369 } else if (strcmp(mode, "control") == 0) {
2370 flags = MONITOR_USE_CONTROL;
2371 } else {
f61eddcb 2372 error_report("unknown monitor mode \"%s\"", mode);
88589343
GH
2373 exit(1);
2374 }
2375
39eaab9a
DB
2376 if (qemu_opt_get_bool(opts, "pretty", 0))
2377 flags |= MONITOR_USE_PRETTY;
2378
bdbcb547
MAL
2379 if (qemu_opt_get_bool(opts, "default", 0)) {
2380 error_report("option 'default' does nothing and is deprecated");
2381 }
88589343
GH
2382
2383 chardev = qemu_opt_get(opts, "chardev");
2384 chr = qemu_chr_find(chardev);
2385 if (chr == NULL) {
f61eddcb 2386 error_report("chardev \"%s\" not found", chardev);
88589343
GH
2387 exit(1);
2388 }
2389
2390 monitor_init(chr, flags);
2391 return 0;
2392}
2393
4821cd4c 2394static void monitor_parse(const char *optarg, const char *mode, bool pretty)
88589343
GH
2395{
2396 static int monitor_device_index = 0;
2397 QemuOpts *opts;
2398 const char *p;
2399 char label[32];
88589343
GH
2400
2401 if (strstart(optarg, "chardev:", &p)) {
2402 snprintf(label, sizeof(label), "%s", p);
2403 } else {
140e065d
JK
2404 snprintf(label, sizeof(label), "compat_monitor%d",
2405 monitor_device_index);
88589343
GH
2406 opts = qemu_chr_parse_compat(label, optarg);
2407 if (!opts) {
f61eddcb 2408 error_report("parse error: %s", optarg);
88589343
GH
2409 exit(1);
2410 }
2411 }
2412
822ac12d 2413 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
f43e47db
MA
2414 qemu_opt_set(opts, "mode", mode, &error_abort);
2415 qemu_opt_set(opts, "chardev", label, &error_abort);
cccb7967 2416 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
88589343
GH
2417 monitor_device_index++;
2418}
2419
bd3c948d
GH
2420struct device_config {
2421 enum {
aee1b935
GH
2422 DEV_USB, /* -usbdevice */
2423 DEV_BT, /* -bt */
2424 DEV_SERIAL, /* -serial */
2425 DEV_PARALLEL, /* -parallel */
2426 DEV_VIRTCON, /* -virtioconsole */
c9f398e5 2427 DEV_DEBUGCON, /* -debugcon */
ef0c4a0d 2428 DEV_GDB, /* -gdb, -s */
3ef669e1 2429 DEV_SCLP, /* s390 sclp */
bd3c948d
GH
2430 } type;
2431 const char *cmdline;
d9a5954d 2432 Location loc;
72cf2d4f 2433 QTAILQ_ENTRY(device_config) next;
bd3c948d 2434};
4fdcac0e
BS
2435
2436static QTAILQ_HEAD(, device_config) device_configs =
2437 QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
2438
2439static void add_device_config(int type, const char *cmdline)
2440{
2441 struct device_config *conf;
2442
7267c094 2443 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
2444 conf->type = type;
2445 conf->cmdline = cmdline;
d9a5954d 2446 loc_save(&conf->loc);
72cf2d4f 2447 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
2448}
2449
2450static int foreach_device_config(int type, int (*func)(const char *cmdline))
2451{
2452 struct device_config *conf;
2453 int rc;
2454
72cf2d4f 2455 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
2456 if (conf->type != type)
2457 continue;
d9a5954d 2458 loc_push_restore(&conf->loc);
bd3c948d 2459 rc = func(conf->cmdline);
d9a5954d 2460 loc_pop(&conf->loc);
28de2f88 2461 if (rc) {
bd3c948d 2462 return rc;
28de2f88 2463 }
bd3c948d
GH
2464 }
2465 return 0;
2466}
2467
998bbd74
GH
2468static int serial_parse(const char *devname)
2469{
2470 static int index = 0;
2471 char label[32];
2472
2473 if (strcmp(devname, "none") == 0)
2474 return 0;
2475 if (index == MAX_SERIAL_PORTS) {
f61eddcb 2476 error_report("too many serial ports");
998bbd74
GH
2477 exit(1);
2478 }
2479 snprintf(label, sizeof(label), "serial%d", index);
b4948be9 2480 serial_hds[index] = qemu_chr_new(label, devname);
998bbd74 2481 if (!serial_hds[index]) {
f61eddcb
EH
2482 error_report("could not connect serial device"
2483 " to character backend '%s'", devname);
998bbd74
GH
2484 return -1;
2485 }
2486 index++;
2487 return 0;
2488}
2489
6a5e8b0e
GH
2490static int parallel_parse(const char *devname)
2491{
2492 static int index = 0;
2493 char label[32];
2494
2495 if (strcmp(devname, "none") == 0)
2496 return 0;
2497 if (index == MAX_PARALLEL_PORTS) {
f61eddcb 2498 error_report("too many parallel ports");
6a5e8b0e
GH
2499 exit(1);
2500 }
2501 snprintf(label, sizeof(label), "parallel%d", index);
b4948be9 2502 parallel_hds[index] = qemu_chr_new(label, devname);
6a5e8b0e 2503 if (!parallel_hds[index]) {
f61eddcb
EH
2504 error_report("could not connect parallel device"
2505 " to character backend '%s'", devname);
6a5e8b0e
GH
2506 return -1;
2507 }
2508 index++;
2509 return 0;
2510}
2511
aee1b935
GH
2512static int virtcon_parse(const char *devname)
2513{
3329f07b 2514 QemuOptsList *device = qemu_find_opts("device");
aee1b935
GH
2515 static int index = 0;
2516 char label[32];
392ecf54 2517 QemuOpts *bus_opts, *dev_opts;
aee1b935
GH
2518
2519 if (strcmp(devname, "none") == 0)
2520 return 0;
2521 if (index == MAX_VIRTIO_CONSOLES) {
f61eddcb 2522 error_report("too many virtio consoles");
aee1b935
GH
2523 exit(1);
2524 }
392ecf54 2525
87ea75d5 2526 bus_opts = qemu_opts_create(device, NULL, 0, &error_abort);
7b3fdbd9 2527 qemu_opt_set(bus_opts, "driver", "virtio-serial", &error_abort);
392ecf54 2528
87ea75d5 2529 dev_opts = qemu_opts_create(device, NULL, 0, &error_abort);
f43e47db 2530 qemu_opt_set(dev_opts, "driver", "virtconsole", &error_abort);
392ecf54 2531
aee1b935 2532 snprintf(label, sizeof(label), "virtcon%d", index);
b4948be9 2533 virtcon_hds[index] = qemu_chr_new(label, devname);
aee1b935 2534 if (!virtcon_hds[index]) {
f61eddcb
EH
2535 error_report("could not connect virtio console"
2536 " to character backend '%s'", devname);
aee1b935
GH
2537 return -1;
2538 }
f43e47db 2539 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
392ecf54 2540
aee1b935
GH
2541 index++;
2542 return 0;
2543}
2544
3ef669e1
AG
2545static int sclp_parse(const char *devname)
2546{
2547 QemuOptsList *device = qemu_find_opts("device");
2548 static int index = 0;
2549 char label[32];
2550 QemuOpts *dev_opts;
2551
2552 if (strcmp(devname, "none") == 0) {
2553 return 0;
2554 }
2555 if (index == MAX_SCLP_CONSOLES) {
f61eddcb 2556 error_report("too many sclp consoles");
3ef669e1
AG
2557 exit(1);
2558 }
2559
2560 assert(arch_type == QEMU_ARCH_S390X);
2561
2562 dev_opts = qemu_opts_create(device, NULL, 0, NULL);
f43e47db 2563 qemu_opt_set(dev_opts, "driver", "sclpconsole", &error_abort);
3ef669e1
AG
2564
2565 snprintf(label, sizeof(label), "sclpcon%d", index);
b4948be9 2566 sclp_hds[index] = qemu_chr_new(label, devname);
3ef669e1 2567 if (!sclp_hds[index]) {
f61eddcb
EH
2568 error_report("could not connect sclp console"
2569 " to character backend '%s'", devname);
3ef669e1
AG
2570 return -1;
2571 }
f43e47db 2572 qemu_opt_set(dev_opts, "chardev", label, &error_abort);
3ef669e1
AG
2573
2574 index++;
2575 return 0;
2576}
2577
c9f398e5 2578static int debugcon_parse(const char *devname)
4d8b3c63 2579{
c9f398e5
PA
2580 QemuOpts *opts;
2581
b4948be9 2582 if (!qemu_chr_new("debugcon", devname)) {
c9f398e5
PA
2583 exit(1);
2584 }
8be7e7e4 2585 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
c9f398e5 2586 if (!opts) {
f61eddcb 2587 error_report("already have a debugcon device");
c9f398e5
PA
2588 exit(1);
2589 }
f43e47db
MA
2590 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
2591 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
c9f398e5
PA
2592 return 0;
2593}
2594
2709f263
LE
2595static gint machine_class_cmp(gconstpointer a, gconstpointer b)
2596{
2597 const MachineClass *mc1 = a, *mc2 = b;
2598 int res;
2599
2600 if (mc1->family == NULL) {
2601 if (mc2->family == NULL) {
2602 /* Compare standalone machine types against each other; they sort
2603 * in increasing order.
2604 */
2605 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
2606 object_class_get_name(OBJECT_CLASS(mc2)));
2607 }
2608
2609 /* Standalone machine types sort after families. */
2610 return 1;
2611 }
2612
2613 if (mc2->family == NULL) {
2614 /* Families sort before standalone machine types. */
2615 return -1;
2616 }
2617
2618 /* Families sort between each other alphabetically increasingly. */
2619 res = strcmp(mc1->family, mc2->family);
2620 if (res != 0) {
2621 return res;
2622 }
2623
2624 /* Within the same family, machine types sort in decreasing order. */
2625 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
2626 object_class_get_name(OBJECT_CLASS(mc1)));
2627}
2628
2629 static MachineClass *machine_parse(const char *name)
9052ea6b 2630{
0056ae24 2631 MachineClass *mc = NULL;
261747f1 2632 GSList *el, *machines = object_class_get_list(TYPE_MACHINE, false);
9052ea6b
JK
2633
2634 if (name) {
0056ae24 2635 mc = find_machine(name);
9052ea6b 2636 }
0056ae24 2637 if (mc) {
02f292d9 2638 g_slist_free(machines);
0056ae24 2639 return mc;
9052ea6b 2640 }
025172d5 2641 if (name && !is_help_option(name)) {
3e515373 2642 error_report("unsupported machine type");
8afb9000 2643 error_printf("Use -machine help to list supported machines\n");
025172d5
MR
2644 } else {
2645 printf("Supported machines are:\n");
2709f263 2646 machines = g_slist_sort(machines, machine_class_cmp);
025172d5
MR
2647 for (el = machines; el; el = el->next) {
2648 MachineClass *mc = el->data;
958db90c
MA
2649 if (mc->alias) {
2650 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
025172d5 2651 }
958db90c
MA
2652 printf("%-20s %s%s\n", mc->name, mc->desc,
2653 mc->is_default ? " (default)" : "");
9052ea6b 2654 }
9052ea6b 2655 }
261747f1
MA
2656
2657 g_slist_free(machines);
c8057f95 2658 exit(!name || !is_help_option(name));
9052ea6b
JK
2659}
2660
fd42deeb
GH
2661void qemu_add_exit_notifier(Notifier *notify)
2662{
2663 notifier_list_add(&exit_notifiers, notify);
2664}
2665
2666void qemu_remove_exit_notifier(Notifier *notify)
2667{
31552529 2668 notifier_remove(notify);
fd42deeb
GH
2669}
2670
2671static void qemu_run_exit_notifiers(void)
2672{
9e8dd451 2673 notifier_list_notify(&exit_notifiers, NULL);
fd42deeb
GH
2674}
2675
71cdd1cb
PB
2676static bool machine_init_done;
2677
4cab946a
GN
2678void qemu_add_machine_init_done_notifier(Notifier *notify)
2679{
2680 notifier_list_add(&machine_init_done_notifiers, notify);
71cdd1cb
PB
2681 if (machine_init_done) {
2682 notify->notify(notify, NULL);
2683 }
4cab946a
GN
2684}
2685
b86eacb8
MA
2686void qemu_remove_machine_init_done_notifier(Notifier *notify)
2687{
2688 notifier_remove(notify);
2689}
2690
4cab946a
GN
2691static void qemu_run_machine_init_done_notifiers(void)
2692{
9e8dd451 2693 notifier_list_notify(&machine_init_done_notifiers, NULL);
71cdd1cb 2694 machine_init_done = true;
4cab946a
GN
2695}
2696
6530a97b
AL
2697static const QEMUOption *lookup_opt(int argc, char **argv,
2698 const char **poptarg, int *poptind)
2699{
2700 const QEMUOption *popt;
2701 int optind = *poptind;
2702 char *r = argv[optind];
2703 const char *optarg;
2704
0f0bc3f1 2705 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
2706 optind++;
2707 /* Treat --foo the same as -foo. */
2708 if (r[1] == '-')
2709 r++;
2710 popt = qemu_options;
2711 for(;;) {
2712 if (!popt->name) {
0f0bc3f1 2713 error_report("invalid option");
6530a97b
AL
2714 exit(1);
2715 }
2716 if (!strcmp(popt->name, r + 1))
2717 break;
2718 popt++;
2719 }
2720 if (popt->flags & HAS_ARG) {
2721 if (optind >= argc) {
0f0bc3f1 2722 error_report("requires an argument");
6530a97b
AL
2723 exit(1);
2724 }
2725 optarg = argv[optind++];
0f0bc3f1 2726 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
2727 } else {
2728 optarg = NULL;
2729 }
2730
2731 *poptarg = optarg;
2732 *poptind = optind;
2733
2734 return popt;
2735}
2736
7580f231 2737static MachineClass *select_machine(void)
34f405ae 2738{
7580f231 2739 MachineClass *machine_class = find_default_machine();
34f405ae
MA
2740 const char *optarg;
2741 QemuOpts *opts;
2742 Location loc;
2743
2744 loc_push_none(&loc);
2745
2746 opts = qemu_get_machine_opts();
2747 qemu_opts_loc_restore(opts);
2748
2749 optarg = qemu_opt_get(opts, "type");
2750 if (optarg) {
7580f231 2751 machine_class = machine_parse(optarg);
34f405ae
MA
2752 }
2753
7580f231 2754 if (!machine_class) {
34f405ae
MA
2755 error_report("No machine specified, and there is no default");
2756 error_printf("Use -machine help to list supported machines\n");
2757 exit(1);
2758 }
2759
2760 loc_pop(&loc);
7580f231 2761 return machine_class;
34f405ae
MA
2762}
2763
71df1d83
MA
2764static int machine_set_property(void *opaque,
2765 const char *name, const char *value,
2766 Error **errp)
68d98d3e
AL
2767{
2768 Object *obj = OBJECT(opaque);
68d98d3e 2769 Error *local_err = NULL;
f279ee45 2770 char *p, *qom_name;
68d98d3e 2771
d2659e27 2772 if (strcmp(name, "type") == 0) {
68d98d3e
AL
2773 return 0;
2774 }
2775
b0ddb8bf 2776 qom_name = g_strdup(name);
f279ee45
MA
2777 for (p = qom_name; *p; p++) {
2778 if (*p == '_') {
2779 *p = '-';
b0ddb8bf
MA
2780 }
2781 }
2782
2e16898a 2783 object_property_parse(obj, value, qom_name, &local_err);
b0ddb8bf 2784 g_free(qom_name);
68d98d3e
AL
2785
2786 if (local_err) {
a720a390 2787 error_report_err(local_err);
68d98d3e
AL
2788 return -1;
2789 }
2790
2791 return 0;
2792}
2793
f08f9271
DB
2794
2795/*
2796 * Initial object creation happens before all other
2797 * QEMU data types are created. The majority of objects
2798 * can be created at this point. The rng-egd object
2799 * cannot be created here, as it depends on the chardev
2800 * already existing.
2801 */
2802static bool object_create_initial(const char *type)
2803{
2804 if (g_str_equal(type, "rng-egd")) {
2805 return false;
2806 }
7dbb11c8 2807
9d3e12e8
TH
2808 /*
2809 * return false for concrete netfilters since
2810 * they depend on netdevs already existing
2811 */
2812 if (g_str_equal(type, "filter-buffer") ||
f6d3afb5 2813 g_str_equal(type, "filter-dump") ||
d46f75b2 2814 g_str_equal(type, "filter-mirror") ||
7dce4e6f 2815 g_str_equal(type, "filter-redirector") ||
e6eee8ab 2816 g_str_equal(type, "colo-compare") ||
646c5478
PD
2817 g_str_equal(type, "filter-rewriter") ||
2818 g_str_equal(type, "filter-replay")) {
7dbb11c8
YH
2819 return false;
2820 }
2821
6546d0db
EH
2822 /* Memory allocation by backends needs to be done
2823 * after configure_accelerator() (due to the tcg_enabled()
2824 * checks at memory_region_init_*()).
2825 *
2826 * Also, allocation of large amounts of memory may delay
2827 * chardev initialization for too long, and trigger timeouts
2828 * on software that waits for a monitor socket to be created
2829 * (e.g. libvirt).
2830 */
2831 if (g_str_has_prefix(type, "memory-backend-")) {
2832 return false;
2833 }
2834
f08f9271
DB
2835 return true;
2836}
2837
2838
2839/*
2840 * The remainder of object creation happens after the
9abce56d 2841 * creation of chardev, fsdev, net clients and device data types.
f08f9271
DB
2842 */
2843static bool object_create_delayed(const char *type)
2844{
2845 return !object_create_initial(type);
2846}
2847
2848
076b35b5
ND
2849static void set_memory_options(uint64_t *ram_slots, ram_addr_t *maxram_size,
2850 MachineClass *mc)
3b9985e9
MA
2851{
2852 uint64_t sz;
2853 const char *mem_str;
2854 const char *maxmem_str, *slots_str;
076b35b5 2855 const ram_addr_t default_ram_size = mc->default_ram_size;
3b9985e9 2856 QemuOpts *opts = qemu_find_opts_singleton("memory");
bbe2d25c
EH
2857 Location loc;
2858
2859 loc_push_none(&loc);
2860 qemu_opts_loc_restore(opts);
3b9985e9
MA
2861
2862 sz = 0;
2863 mem_str = qemu_opt_get(opts, "size");
2864 if (mem_str) {
2865 if (!*mem_str) {
2866 error_report("missing 'size' option value");
2867 exit(EXIT_FAILURE);
2868 }
2869
2870 sz = qemu_opt_get_size(opts, "size", ram_size);
2871
2872 /* Fix up legacy suffix-less format */
2873 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
2874 uint64_t overflow_check = sz;
2875
2876 sz <<= 20;
2877 if ((sz >> 20) != overflow_check) {
2878 error_report("too large 'size' option value");
2879 exit(EXIT_FAILURE);
2880 }
2881 }
2882 }
2883
2884 /* backward compatibility behaviour for case "-m 0" */
2885 if (sz == 0) {
2886 sz = default_ram_size;
2887 }
2888
2889 sz = QEMU_ALIGN_UP(sz, 8192);
2890 ram_size = sz;
2891 if (ram_size != sz) {
2892 error_report("ram size too large");
2893 exit(EXIT_FAILURE);
2894 }
2895
2896 /* store value for the future use */
39101f25 2897 qemu_opt_set_number(opts, "size", ram_size, &error_abort);
3b9985e9
MA
2898 *maxram_size = ram_size;
2899
2900 maxmem_str = qemu_opt_get(opts, "maxmem");
2901 slots_str = qemu_opt_get(opts, "slots");
2902 if (maxmem_str && slots_str) {
2903 uint64_t slots;
2904
2905 sz = qemu_opt_get_size(opts, "maxmem", 0);
214224ad 2906 slots = qemu_opt_get_number(opts, "slots", 0);
3b9985e9 2907 if (sz < ram_size) {
214224ad
PK
2908 error_report("invalid value of -m option maxmem: "
2909 "maximum memory size (0x%" PRIx64 ") must be at least "
2910 "the initial memory size (0x" RAM_ADDR_FMT ")",
2911 sz, ram_size);
3b9985e9 2912 exit(EXIT_FAILURE);
214224ad
PK
2913 } else if (sz > ram_size) {
2914 if (!slots) {
2915 error_report("invalid value of -m option: maxmem was "
2916 "specified, but no hotplug slots were specified");
2917 exit(EXIT_FAILURE);
2918 }
2919 } else if (slots) {
2920 error_report("invalid value of -m option maxmem: "
2921 "memory slots were specified but maximum memory size "
2922 "(0x%" PRIx64 ") is equal to the initial memory size "
2923 "(0x" RAM_ADDR_FMT ")", sz, ram_size);
3b9985e9
MA
2924 exit(EXIT_FAILURE);
2925 }
2926
3b9985e9
MA
2927 *maxram_size = sz;
2928 *ram_slots = slots;
2929 } else if ((!maxmem_str && slots_str) ||
2930 (maxmem_str && !slots_str)) {
2931 error_report("invalid -m option value: missing "
2932 "'%s' option", slots_str ? "maxmem" : "slots");
2933 exit(EXIT_FAILURE);
2934 }
bbe2d25c
EH
2935
2936 loc_pop(&loc);
3b9985e9
MA
2937}
2938
8d76bfe8
EH
2939static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2940{
2941 GlobalProperty *g;
2942
2943 g = g_malloc0(sizeof(*g));
2944 g->driver = qemu_opt_get(opts, "driver");
2945 g->property = qemu_opt_get(opts, "property");
2946 g->value = qemu_opt_get(opts, "value");
2947 g->user_provided = true;
03f28efb 2948 g->errp = &error_fatal;
8d76bfe8
EH
2949 qdev_prop_register_global(g);
2950 return 0;
2951}
2952
726401be
EH
2953static int qemu_read_default_config_file(void)
2954{
2955 int ret;
2956
2957 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
2958 if (ret < 0 && ret != -ENOENT) {
2959 return ret;
2960 }
2961
2962 return 0;
2963}
2964
902b3d5c 2965int main(int argc, char **argv, char **envp)
0824d6fc 2966{
e4bcb14c 2967 int i;
da1fcfda 2968 int snapshot, linux_boot;
7f7f9873 2969 const char *initrd_filename;
a20dd508 2970 const char *kernel_filename, *kernel_cmdline;
f05f47bb
GA
2971 const char *boot_order = NULL;
2972 const char *boot_once = NULL;
3023f332 2973 DisplayState *ds;
9f227bc3 2974 int cyls, heads, secs, translation;
1ad9580b 2975 QemuOpts *hda_opts = NULL, *opts, *machine_opts, *icount_opts = NULL;
03b0ba70 2976 QemuOptsList *olist;
cd6f1169 2977 int optind;
6530a97b 2978 const char *optarg;
d63d307f 2979 const char *loadvm = NULL;
0056ae24 2980 MachineClass *machine_class;
94fc95cd 2981 const char *cpu_model;
d44229c5 2982 const char *vga_model = NULL;
d4fce24f
PB
2983 const char *qtest_chrdev = NULL;
2984 const char *qtest_log = NULL;
93815bc2 2985 const char *pid_file = NULL;
5bb7910a 2986 const char *incoming = NULL;
3ed2d9ee 2987 bool defconfig = true;
f29a5614 2988 bool userconfig = true;
cfc58cf3 2989 bool nographic = false;
d29345d0 2990 DisplayType display_type = DT_DEFAULT;
1f0dfe02 2991 int display_remote = 0;
c235d738
MF
2992 const char *log_mask = NULL;
2993 const char *log_file = NULL;
41fc57e4 2994 char *trace_file = NULL;
3b9985e9 2995 ram_addr_t maxram_size;
c270fb9e 2996 uint64_t ram_slots = 0;
abfd9ce3 2997 FILE *vmstate_dump_file = NULL;
2f78e491 2998 Error *main_loop_err = NULL;
ddbb0d09 2999 Error *err = NULL;
37146e7e 3000 bool list_data_dirs = false;
0b5538c3 3001
fe4db84d
DB
3002 module_call_init(MODULE_INIT_TRACE);
3003
267f685b 3004 qemu_init_cpu_list();
576a94d8
PB
3005 qemu_init_cpu_loop();
3006 qemu_mutex_lock_iothread();
3007
fd42deeb 3008 atexit(qemu_run_exit_notifiers);
65abca0a 3009 error_set_progname(argv[0]);
10f5bff6 3010 qemu_init_exec_dir(argv[0]);
65abca0a 3011
1b71f7c1 3012 module_call_init(MODULE_INIT_QOM);
c823501e 3013 module_call_init(MODULE_INIT_QAPI);
1b71f7c1 3014
4d454574 3015 qemu_add_opts(&qemu_drive_opts);
968854c8
AK
3016 qemu_add_drive_opts(&qemu_legacy_drive_opts);
3017 qemu_add_drive_opts(&qemu_common_drive_opts);
3018 qemu_add_drive_opts(&qemu_drive_opts);
c5f3014b 3019 qemu_add_drive_opts(&bdrv_runtime_opts);
4d454574
PB
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);
6e1d3c1c 3030 qemu_add_opts(&qemu_mem_opts);
12b7f57e 3031 qemu_add_opts(&qemu_smp_opts);
4d454574
PB
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);
d1a0cf73 3036 qemu_add_opts(&qemu_tpmdev_opts);
888a6bc6 3037 qemu_add_opts(&qemu_realtime_opts);
5e2ac519 3038 qemu_add_opts(&qemu_msg_opts);
5d12f961 3039 qemu_add_opts(&qemu_name_opts);
0042109a 3040 qemu_add_opts(&qemu_numa_opts);
1ad9580b 3041 qemu_add_opts(&qemu_icount_opts);
a38bb079 3042 qemu_add_opts(&qemu_semihosting_config_opts);
81b2b810 3043 qemu_add_opts(&qemu_fw_cfg_opts);
f57b4b5f
CL
3044#ifdef CONFIG_LIBISCSI
3045 qemu_add_opts(&qemu_iscsi_opts);
3046#endif
34294e2f 3047 module_call_init(MODULE_INIT_OPTS);
4d454574 3048
5db9d4d1
LC
3049 runstate_init();
3050
ddbb0d09 3051 if (qcrypto_init(&err) < 0) {
c29b77f9 3052 error_reportf_err(err, "cannot initialize crypto: ");
ddbb0d09
DB
3053 exit(1);
3054 }
884f17c2 3055 rtc_clock = QEMU_CLOCK_HOST;
6875204c 3056
72cf2d4f 3057 QLIST_INIT (&vm_change_state_head);
fe98ac14 3058 os_setup_early_signal_handling();
be995c27 3059
94fc95cd 3060 cpu_model = NULL;
33e3963e 3061 snapshot = 0;
9f227bc3 3062 cyls = heads = secs = 0;
3063 translation = BIOS_ATA_TRANSLATION_AUTO;
c4b1fcc0 3064
7c9d8e07 3065 nb_nics = 0;
3b46e624 3066
142c6b1a
PL
3067 bdrv_init_with_whitelist();
3068
2c02d1ad 3069 autostart = 1;
41bd639b 3070
292444cb
AL
3071 /* first pass of option parsing */
3072 optind = 1;
3073 while (optind < argc) {
3074 if (argv[optind][0] != '-') {
3075 /* disk image */
28e68d68 3076 optind++;
292444cb
AL
3077 } else {
3078 const QEMUOption *popt;
3079
3080 popt = lookup_opt(argc, argv, &optarg, &optind);
3081 switch (popt->index) {
3082 case QEMU_OPTION_nodefconfig:
3ed2d9ee 3083 defconfig = false;
292444cb 3084 break;
f29a5614
EH
3085 case QEMU_OPTION_nouserconfig:
3086 userconfig = false;
3087 break;
292444cb
AL
3088 }
3089 }
3090 }
3091
726401be
EH
3092 if (defconfig && userconfig) {
3093 if (qemu_read_default_config_file() < 0) {
dcfb0939 3094 exit(1);
292444cb
AL
3095 }
3096 }
3097
3098 /* second pass of option parsing */
cd6f1169 3099 optind = 1;
0824d6fc 3100 for(;;) {
cd6f1169 3101 if (optind >= argc)
0824d6fc 3102 break;
6530a97b 3103 if (argv[optind][0] != '-') {
99efa84d 3104 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
3105 } else {
3106 const QEMUOption *popt;
3107
6530a97b 3108 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a 3109 if (!(popt->arch_mask & arch_type)) {
da002526 3110 error_report("Option not supported for this target");
ad96090a
BS
3111 exit(1);
3112 }
cd6f1169 3113 switch(popt->index) {
e43d594e
JK
3114 case QEMU_OPTION_no_kvm_irqchip: {
3115 olist = qemu_find_opts("machine");
70b94331 3116 qemu_opts_parse_noisily(olist, "kernel_irqchip=off", false);
e43d594e
JK
3117 break;
3118 }
94fc95cd
JM
3119 case QEMU_OPTION_cpu:
3120 /* hw initialization will check this */
ecf40bea 3121 cpu_model = optarg;
94fc95cd 3122 break;
cd6f1169 3123 case QEMU_OPTION_hda:
9f227bc3 3124 {
3125 char buf[256];
3126 if (cyls == 0)
3127 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
3128 else
3129 snprintf(buf, sizeof(buf),
3130 "%s,cyls=%d,heads=%d,secs=%d%s",
3131 HD_OPTS , cyls, heads, secs,
3132 translation == BIOS_ATA_TRANSLATION_LBA ?
3133 ",trans=lba" :
3134 translation == BIOS_ATA_TRANSLATION_NONE ?
3135 ",trans=none" : "");
3136 drive_add(IF_DEFAULT, 0, optarg, buf);
3137 break;
3138 }
cd6f1169 3139 case QEMU_OPTION_hdb:
cc1daa40
FB
3140 case QEMU_OPTION_hdc:
3141 case QEMU_OPTION_hdd:
2292ddae
MA
3142 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
3143 HD_OPTS);
fc01f7e7 3144 break;
e4bcb14c 3145 case QEMU_OPTION_drive:
e2982c3a
MT
3146 if (drive_def(optarg) == NULL) {
3147 exit(1);
3148 }
99efa84d 3149 break;
d058fe03
GH
3150 case QEMU_OPTION_set:
3151 if (qemu_set_option(optarg) != 0)
3152 exit(1);
99efa84d 3153 break;
d0fef6fb
GH
3154 case QEMU_OPTION_global:
3155 if (qemu_global_option(optarg) != 0)
3156 exit(1);
99efa84d 3157 break;
3e3d5815 3158 case QEMU_OPTION_mtdblock:
2292ddae 3159 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 3160 break;
a1bb27b1 3161 case QEMU_OPTION_sd:
80f4d9fc 3162 drive_add(IF_SD, -1, optarg, SD_OPTS);
a1bb27b1 3163 break;
86f55663 3164 case QEMU_OPTION_pflash:
2292ddae 3165 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 3166 break;
cd6f1169 3167 case QEMU_OPTION_snapshot:
33e3963e
FB
3168 snapshot = 1;
3169 break;
cd6f1169 3170 case QEMU_OPTION_hdachs:
330d0414 3171 {
330d0414
FB
3172 const char *p;
3173 p = optarg;
3174 cyls = strtol(p, (char **)&p, 0);
46d4767d
FB
3175 if (cyls < 1 || cyls > 16383)
3176 goto chs_fail;
330d0414
FB
3177 if (*p != ',')
3178 goto chs_fail;
3179 p++;
3180 heads = strtol(p, (char **)&p, 0);
46d4767d
FB
3181 if (heads < 1 || heads > 16)
3182 goto chs_fail;
330d0414
FB
3183 if (*p != ',')
3184 goto chs_fail;
3185 p++;
3186 secs = strtol(p, (char **)&p, 0);
46d4767d
FB
3187 if (secs < 1 || secs > 63)
3188 goto chs_fail;
3189 if (*p == ',') {
3190 p++;
f31c41ff
PB
3191 if (!strcmp(p, "large")) {
3192 translation = BIOS_ATA_TRANSLATION_LARGE;
3193 } else if (!strcmp(p, "rechs")) {
3194 translation = BIOS_ATA_TRANSLATION_RECHS;
3195 } else if (!strcmp(p, "none")) {
46d4767d 3196 translation = BIOS_ATA_TRANSLATION_NONE;
f31c41ff 3197 } else if (!strcmp(p, "lba")) {
46d4767d 3198 translation = BIOS_ATA_TRANSLATION_LBA;
f31c41ff 3199 } else if (!strcmp(p, "auto")) {
46d4767d 3200 translation = BIOS_ATA_TRANSLATION_AUTO;
f31c41ff 3201 } else {
46d4767d 3202 goto chs_fail;
f31c41ff 3203 }
46d4767d 3204 } else if (*p != '\0') {
c4b1fcc0 3205 chs_fail:
f61eddcb 3206 error_report("invalid physical CHS format");
46d4767d 3207 exit(1);
c4b1fcc0 3208 }
99efa84d 3209 if (hda_opts != NULL) {
a8b18f8f
MA
3210 qemu_opt_set_number(hda_opts, "cyls", cyls,
3211 &error_abort);
3212 qemu_opt_set_number(hda_opts, "heads", heads,
3213 &error_abort);
3214 qemu_opt_set_number(hda_opts, "secs", secs,
3215 &error_abort);
f31c41ff 3216 if (translation == BIOS_ATA_TRANSLATION_LARGE) {
f43e47db
MA
3217 qemu_opt_set(hda_opts, "trans", "large",
3218 &error_abort);
f31c41ff 3219 } else if (translation == BIOS_ATA_TRANSLATION_RECHS) {
f43e47db
MA
3220 qemu_opt_set(hda_opts, "trans", "rechs",
3221 &error_abort);
f31c41ff 3222 } else if (translation == BIOS_ATA_TRANSLATION_LBA) {
f43e47db
MA
3223 qemu_opt_set(hda_opts, "trans", "lba",
3224 &error_abort);
f31c41ff 3225 } else if (translation == BIOS_ATA_TRANSLATION_NONE) {
f43e47db
MA
3226 qemu_opt_set(hda_opts, "trans", "none",
3227 &error_abort);
f31c41ff 3228 }
9dfd7c7a 3229 }
330d0414
FB
3230 }
3231 break;
268a362c 3232 case QEMU_OPTION_numa:
70b94331
MA
3233 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
3234 optarg, true);
0042109a
WG
3235 if (!opts) {
3236 exit(1);
3237 }
268a362c 3238 break;
1472a95b
JS
3239 case QEMU_OPTION_display:
3240 display_type = select_display(optarg);
3241 break;
cd6f1169 3242 case QEMU_OPTION_nographic:
cfc58cf3
EH
3243 olist = qemu_find_opts("machine");
3244 qemu_opts_parse_noisily(olist, "graphics=off", false);
3245 nographic = true;
3246 display_type = DT_NONE;
a20dd508 3247 break;
4d3b6f6e 3248 case QEMU_OPTION_curses:
47b05369 3249#ifdef CONFIG_CURSES
993fbfdb 3250 display_type = DT_CURSES;
47b05369 3251#else
3e515373 3252 error_report("curses support is disabled");
47b05369 3253 exit(1);
4d3b6f6e 3254#endif
47b05369 3255 break;
a171fe39 3256 case QEMU_OPTION_portrait:
9312805d
VK
3257 graphic_rotate = 90;
3258 break;
3259 case QEMU_OPTION_rotate:
3260 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
3261 if (graphic_rotate != 0 && graphic_rotate != 90 &&
3262 graphic_rotate != 180 && graphic_rotate != 270) {
f61eddcb 3263 error_report("only 90, 180, 270 deg rotation is available");
9312805d
VK
3264 exit(1);
3265 }
a171fe39 3266 break;
cd6f1169 3267 case QEMU_OPTION_kernel:
79087c78
MA
3268 qemu_opts_set(qemu_find_opts("machine"), 0, "kernel", optarg,
3269 &error_abort);
a0abe474
PM
3270 break;
3271 case QEMU_OPTION_initrd:
79087c78
MA
3272 qemu_opts_set(qemu_find_opts("machine"), 0, "initrd", optarg,
3273 &error_abort);
a20dd508 3274 break;
cd6f1169 3275 case QEMU_OPTION_append:
79087c78
MA
3276 qemu_opts_set(qemu_find_opts("machine"), 0, "append", optarg,
3277 &error_abort);
313aa567 3278 break;
412beee6 3279 case QEMU_OPTION_dtb:
79087c78
MA
3280 qemu_opts_set(qemu_find_opts("machine"), 0, "dtb", optarg,
3281 &error_abort);
412beee6 3282 break;
cd6f1169 3283 case QEMU_OPTION_cdrom:
2292ddae 3284 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 3285 break;
cd6f1169 3286 case QEMU_OPTION_boot:
70b94331
MA
3287 opts = qemu_opts_parse_noisily(qemu_find_opts("boot-opts"),
3288 optarg, true);
8281abd5
MA
3289 if (!opts) {
3290 exit(1);
36b486bb
FB
3291 }
3292 break;
cd6f1169 3293 case QEMU_OPTION_fda:
cd6f1169 3294 case QEMU_OPTION_fdb:
2292ddae
MA
3295 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
3296 optarg, FD_OPTS);
c45886db 3297 break;
52ca8d6a
FB
3298 case QEMU_OPTION_no_fd_bootchk:
3299 fd_bootchk = 0;
3300 break;
a1ea458f 3301 case QEMU_OPTION_netdev:
d30300f7 3302 default_net = 0;
3329f07b 3303 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
a1ea458f
MM
3304 exit(1);
3305 }
3306 break;
7c9d8e07 3307 case QEMU_OPTION_net:
d30300f7 3308 default_net = 0;
3329f07b 3309 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
c4b1fcc0
FB
3310 exit(1);
3311 }
702c651c 3312 break;
f9dadc98
RS
3313#ifdef CONFIG_LIBISCSI
3314 case QEMU_OPTION_iscsi:
70b94331
MA
3315 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
3316 optarg, false);
f9dadc98
RS
3317 if (!opts) {
3318 exit(1);
3319 }
3320 break;
3321#endif
c7f74643
FB
3322#ifdef CONFIG_SLIRP
3323 case QEMU_OPTION_tftp:
f853ac66
TH
3324 error_report("The -tftp option is deprecated. "
3325 "Please use '-netdev user,tftp=...' instead.");
ad196a9d 3326 legacy_tftp_prefix = optarg;
9bf05444 3327 break;
47d5d01a 3328 case QEMU_OPTION_bootp:
f853ac66
TH
3329 error_report("The -bootp option is deprecated. "
3330 "Please use '-netdev user,bootfile=...' instead.");
ad196a9d 3331 legacy_bootp_filename = optarg;
47d5d01a 3332 break;
9bf05444 3333 case QEMU_OPTION_redir:
f853ac66
TH
3334 error_report("The -redir option is deprecated. "
3335 "Please use '-netdev user,hostfwd=...' instead.");
0752706d
MA
3336 if (net_slirp_redir(optarg) < 0)
3337 exit(1);
9bf05444 3338 break;
c7f74643 3339#endif
dc72ac14 3340 case QEMU_OPTION_bt:
bd3c948d 3341 add_device_config(DEV_BT, optarg);
dc72ac14 3342 break;
1d14ffa9
FB
3343 case QEMU_OPTION_audio_help:
3344 AUD_help ();
3345 exit (0);
3346 break;
3347 case QEMU_OPTION_soundhw:
3348 select_soundhw (optarg);
3349 break;
cd6f1169 3350 case QEMU_OPTION_h:
15f82208 3351 help(0);
cd6f1169 3352 break;
9bd7e6d9
PB
3353 case QEMU_OPTION_version:
3354 version();
3355 exit(0);
3356 break;
3b9985e9 3357 case QEMU_OPTION_m:
70b94331
MA
3358 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"),
3359 optarg, true);
6e1d3c1c
IM
3360 if (!opts) {
3361 exit(EXIT_FAILURE);
3362 }
cd6f1169 3363 break;
d1a0cf73
SB
3364#ifdef CONFIG_TPM
3365 case QEMU_OPTION_tpmdev:
3366 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
3367 exit(1);
3368 }
3369 break;
3370#endif
c902760f
MT
3371 case QEMU_OPTION_mempath:
3372 mem_path = optarg;
3373 break;
c902760f
MT
3374 case QEMU_OPTION_mem_prealloc:
3375 mem_prealloc = 1;
3376 break;
cd6f1169 3377 case QEMU_OPTION_d:
c235d738
MF
3378 log_mask = optarg;
3379 break;
3380 case QEMU_OPTION_D:
3381 log_file = optarg;
cd6f1169 3382 break;
3514552e 3383 case QEMU_OPTION_DFILTER:
bd6fee9f 3384 qemu_set_dfilter_ranges(optarg, &error_fatal);
3514552e 3385 break;
cd6f1169 3386 case QEMU_OPTION_s:
ef0c4a0d 3387 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 3388 break;
59030a8c 3389 case QEMU_OPTION_gdb:
ef0c4a0d 3390 add_device_config(DEV_GDB, optarg);
cd6f1169 3391 break;
cd6f1169 3392 case QEMU_OPTION_L:
37146e7e
RJ
3393 if (is_help_option(optarg)) {
3394 list_data_dirs = true;
3395 } else if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4524051c
GH
3396 data_dir[data_dir_idx++] = optarg;
3397 }
cd6f1169 3398 break;
1192dad8 3399 case QEMU_OPTION_bios:
79087c78
MA
3400 qemu_opts_set(qemu_find_opts("machine"), 0, "firmware", optarg,
3401 &error_abort);
1192dad8 3402 break;
1b530a6d
AJ
3403 case QEMU_OPTION_singlestep:
3404 singlestep = 1;
3405 break;
cd6f1169 3406 case QEMU_OPTION_S:
3c07f8e8 3407 autostart = 0;
cd6f1169 3408 break;
99efa84d
MA
3409 case QEMU_OPTION_k:
3410 keyboard_layout = optarg;
3411 break;
ee22c2f7
FB
3412 case QEMU_OPTION_localtime:
3413 rtc_utc = 0;
3414 break;
3893c124 3415 case QEMU_OPTION_vga:
a369da5f 3416 vga_model = optarg;
7f1b17f2 3417 default_vga = 0;
1bfe856e 3418 break;
e9b137c2
FB
3419 case QEMU_OPTION_g:
3420 {
3421 const char *p;
3422 int w, h, depth;
3423 p = optarg;
3424 w = strtol(p, (char **)&p, 10);
3425 if (w <= 0) {
3426 graphic_error:
f61eddcb 3427 error_report("invalid resolution or depth");
e9b137c2
FB
3428 exit(1);
3429 }
3430 if (*p != 'x')
3431 goto graphic_error;
3432 p++;
3433 h = strtol(p, (char **)&p, 10);
3434 if (h <= 0)
3435 goto graphic_error;
3436 if (*p == 'x') {
3437 p++;
3438 depth = strtol(p, (char **)&p, 10);
5fafdf24 3439 if (depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
3440 depth != 24 && depth != 32)
3441 goto graphic_error;
3442 } else if (*p == '\0') {
3443 depth = graphic_depth;
3444 } else {
3445 goto graphic_error;
3446 }
3b46e624 3447
e9b137c2
FB
3448 graphic_width = w;
3449 graphic_height = h;
3450 graphic_depth = depth;
3451 }
3452 break;
20d8a3ed
TS
3453 case QEMU_OPTION_echr:
3454 {
3455 char *r;
3456 term_escape_char = strtol(optarg, &r, 0);
3457 if (r == optarg)
3458 printf("Bad argument to echr\n");
3459 break;
3460 }
82c643ff 3461 case QEMU_OPTION_monitor:
6ca5582d 3462 default_monitor = 0;
70e098af 3463 if (strncmp(optarg, "none", 4)) {
4821cd4c 3464 monitor_parse(optarg, "readline", false);
70e098af 3465 }
6ca5582d
GH
3466 break;
3467 case QEMU_OPTION_qmp:
4821cd4c
HR
3468 monitor_parse(optarg, "control", false);
3469 default_monitor = 0;
3470 break;
3471 case QEMU_OPTION_qmp_pretty:
3472 monitor_parse(optarg, "control", true);
2d114dc1 3473 default_monitor = 0;
82c643ff 3474 break;
22a0e04b 3475 case QEMU_OPTION_mon:
70b94331
MA
3476 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3477 true);
22a0e04b 3478 if (!opts) {
22a0e04b
GH
3479 exit(1);
3480 }
2d114dc1 3481 default_monitor = 0;
22a0e04b 3482 break;
191bc01b 3483 case QEMU_OPTION_chardev:
70b94331
MA
3484 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3485 optarg, true);
191bc01b 3486 if (!opts) {
191bc01b
GH
3487 exit(1);
3488 }
191bc01b 3489 break;
74db920c 3490 case QEMU_OPTION_fsdev:
03b0ba70
GH
3491 olist = qemu_find_opts("fsdev");
3492 if (!olist) {
5dfdae81 3493 error_report("fsdev support is disabled");
03b0ba70
GH
3494 exit(1);
3495 }
70b94331 3496 opts = qemu_opts_parse_noisily(olist, optarg, true);
74db920c 3497 if (!opts) {
74db920c
GS
3498 exit(1);
3499 }
3500 break;
3d54abc7 3501 case QEMU_OPTION_virtfs: {
e14ea479
SH
3502 QemuOpts *fsdev;
3503 QemuOpts *device;
84a87cc4 3504 const char *writeout, *sock_fd, *socket;
3d54abc7 3505
03b0ba70
GH
3506 olist = qemu_find_opts("virtfs");
3507 if (!olist) {
5dfdae81 3508 error_report("virtfs support is disabled");
03b0ba70
GH
3509 exit(1);
3510 }
70b94331 3511 opts = qemu_opts_parse_noisily(olist, optarg, true);
3d54abc7 3512 if (!opts) {
3d54abc7
GS
3513 exit(1);
3514 }
3515
fbcbf101 3516 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a 3517 qemu_opt_get(opts, "mount_tag") == NULL) {
8afb9000 3518 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
9ce56db6
VJ
3519 exit(1);
3520 }
e14ea479 3521 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
8be7e7e4
LC
3522 qemu_opt_get(opts, "mount_tag"),
3523 1, NULL);
e14ea479 3524 if (!fsdev) {
f61eddcb
EH
3525 error_report("duplicate fsdev id: %s",
3526 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
3527 exit(1);
3528 }
d3ab98e6
AK
3529
3530 writeout = qemu_opt_get(opts, "writeout");
3531 if (writeout) {
3532#ifdef CONFIG_SYNC_FILE_RANGE
f43e47db 3533 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
d3ab98e6 3534#else
f61eddcb
EH
3535 error_report("writeout=immediate not supported "
3536 "on this platform");
d3ab98e6
AK
3537 exit(1);
3538#endif
3539 }
f43e47db
MA
3540 qemu_opt_set(fsdev, "fsdriver",
3541 qemu_opt_get(opts, "fsdriver"), &error_abort);
3542 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"),
3543 &error_abort);
e14ea479 3544 qemu_opt_set(fsdev, "security_model",
f43e47db
MA
3545 qemu_opt_get(opts, "security_model"),
3546 &error_abort);
84a87cc4
MK
3547 socket = qemu_opt_get(opts, "socket");
3548 if (socket) {
f43e47db 3549 qemu_opt_set(fsdev, "socket", socket, &error_abort);
84a87cc4 3550 }
4c793dda
MK
3551 sock_fd = qemu_opt_get(opts, "sock_fd");
3552 if (sock_fd) {
f43e47db 3553 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
4c793dda 3554 }
e14ea479 3555
2c74c2cb 3556 qemu_opt_set_bool(fsdev, "readonly",
cccb7967
MA
3557 qemu_opt_get_bool(opts, "readonly", 0),
3558 &error_abort);
87ea75d5
PC
3559 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3560 &error_abort);
f43e47db 3561 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
e14ea479 3562 qemu_opt_set(device, "fsdev",
f43e47db 3563 qemu_opt_get(opts, "mount_tag"), &error_abort);
e14ea479 3564 qemu_opt_set(device, "mount_tag",
f43e47db 3565 qemu_opt_get(opts, "mount_tag"), &error_abort);
3d54abc7
GS
3566 break;
3567 }
9db221ae
AK
3568 case QEMU_OPTION_virtfs_synth: {
3569 QemuOpts *fsdev;
3570 QemuOpts *device;
3571
8be7e7e4
LC
3572 fsdev = qemu_opts_create(qemu_find_opts("fsdev"), "v_synth",
3573 1, NULL);
9db221ae 3574 if (!fsdev) {
f61eddcb 3575 error_report("duplicate option: %s", "virtfs_synth");
9db221ae
AK
3576 exit(1);
3577 }
f43e47db 3578 qemu_opt_set(fsdev, "fsdriver", "synth", &error_abort);
9db221ae 3579
87ea75d5
PC
3580 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3581 &error_abort);
f43e47db
MA
3582 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
3583 qemu_opt_set(device, "fsdev", "v_synth", &error_abort);
3584 qemu_opt_set(device, "mount_tag", "v_synth", &error_abort);
9db221ae
AK
3585 break;
3586 }
82c643ff 3587 case QEMU_OPTION_serial:
998bbd74
GH
3588 add_device_config(DEV_SERIAL, optarg);
3589 default_serial = 0;
18141ed6
JK
3590 if (strncmp(optarg, "mon:", 4) == 0) {
3591 default_monitor = 0;
3592 }
82c643ff 3593 break;
9dd986cc 3594 case QEMU_OPTION_watchdog:
09aaa160 3595 if (watchdog) {
f61eddcb 3596 error_report("only one watchdog option may be given");
09aaa160
MA
3597 return 1;
3598 }
3599 watchdog = optarg;
9dd986cc
RJ
3600 break;
3601 case QEMU_OPTION_watchdog_action:
3602 if (select_watchdog_action(optarg) == -1) {
3e515373 3603 error_report("unknown -watchdog-action parameter");
9dd986cc
RJ
3604 exit(1);
3605 }
3606 break;
51ecf136 3607 case QEMU_OPTION_virtiocon:
aee1b935
GH
3608 add_device_config(DEV_VIRTCON, optarg);
3609 default_virtcon = 0;
18141ed6
JK
3610 if (strncmp(optarg, "mon:", 4) == 0) {
3611 default_monitor = 0;
3612 }
51ecf136 3613 break;
6508fe59 3614 case QEMU_OPTION_parallel:
6a5e8b0e
GH
3615 add_device_config(DEV_PARALLEL, optarg);
3616 default_parallel = 0;
18141ed6
JK
3617 if (strncmp(optarg, "mon:", 4) == 0) {
3618 default_monitor = 0;
3619 }
6508fe59 3620 break;
c9f398e5
PA
3621 case QEMU_OPTION_debugcon:
3622 add_device_config(DEV_DEBUGCON, optarg);
3623 break;
99efa84d
MA
3624 case QEMU_OPTION_loadvm:
3625 loadvm = optarg;
3626 break;
d63d307f
FB
3627 case QEMU_OPTION_full_screen:
3628 full_screen = 1;
3629 break;
43523e93
TS
3630 case QEMU_OPTION_no_frame:
3631 no_frame = 1;
3632 break;
3780e197
TS
3633 case QEMU_OPTION_alt_grab:
3634 alt_grab = 1;
3635 break;
0ca9f8a4
DK
3636 case QEMU_OPTION_ctrl_grab:
3637 ctrl_grab = 1;
3638 break;
667accab
TS
3639 case QEMU_OPTION_no_quit:
3640 no_quit = 1;
3641 break;
7d957bd8 3642 case QEMU_OPTION_sdl:
24f6ff86 3643#ifdef CONFIG_SDL
993fbfdb 3644 display_type = DT_SDL;
7d957bd8 3645 break;
58fc096c 3646#else
f61eddcb 3647 error_report("SDL support is disabled");
58fc096c 3648 exit(1);
667accab 3649#endif
f7cce898 3650 case QEMU_OPTION_pidfile:
93815bc2 3651 pid_file = optarg;
f7cce898 3652 break;
a09db21f
FB
3653 case QEMU_OPTION_win2k_hack:
3654 win2k_install_hack = 1;
3655 break;
433acf0d 3656 case QEMU_OPTION_rtc_td_hack: {
16714b16
EH
3657 static GlobalProperty slew_lost_ticks = {
3658 .driver = "mc146818rtc",
3659 .property = "lost_tick_policy",
3660 .value = "slew",
433acf0d
JK
3661 };
3662
16714b16 3663 qdev_prop_register_global(&slew_lost_ticks);
73822ec8 3664 break;
433acf0d 3665 }
8a92ea2f 3666 case QEMU_OPTION_acpitable:
70b94331
MA
3667 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3668 optarg, true);
f46e720a
LE
3669 if (!opts) {
3670 exit(1);
3671 }
9f57061c 3672 acpi_table_add(opts, &error_fatal);
8a92ea2f 3673 break;
b6f6e3d3 3674 case QEMU_OPTION_smbios:
70b94331
MA
3675 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3676 optarg, false);
f46e720a
LE
3677 if (!opts) {
3678 exit(1);
3679 }
1007a37e 3680 smbios_entry_add(opts, &error_fatal);
b6f6e3d3 3681 break;
81b2b810 3682 case QEMU_OPTION_fwcfg:
70b94331
MA
3683 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3684 optarg, true);
81b2b810
GS
3685 if (opts == NULL) {
3686 exit(1);
3687 }
3688 break;
7ba1e619 3689 case QEMU_OPTION_enable_kvm:
303d4e86 3690 olist = qemu_find_opts("machine");
70b94331 3691 qemu_opts_parse_noisily(olist, "accel=kvm", false);
303d4e86 3692 break;
b0cb0a66
VP
3693 case QEMU_OPTION_enable_hax:
3694 olist = qemu_find_opts("machine");
3695 qemu_opts_parse_noisily(olist, "accel=hax", false);
3696 break;
364c3e6b 3697 case QEMU_OPTION_M:
303d4e86
AP
3698 case QEMU_OPTION_machine:
3699 olist = qemu_find_opts("machine");
70b94331 3700 opts = qemu_opts_parse_noisily(olist, optarg, true);
303d4e86 3701 if (!opts) {
303d4e86
AP
3702 exit(1);
3703 }
7ba1e619 3704 break;
a0dac021
JK
3705 case QEMU_OPTION_no_kvm:
3706 olist = qemu_find_opts("machine");
70b94331 3707 qemu_opts_parse_noisily(olist, "accel=tcg", false);
a0dac021 3708 break;
4086bde8 3709 case QEMU_OPTION_no_kvm_pit: {
515cb8ef 3710 error_report("warning: ignoring deprecated option");
4086bde8
JK
3711 break;
3712 }
c21fb4f8 3713 case QEMU_OPTION_no_kvm_pit_reinjection: {
16714b16
EH
3714 static GlobalProperty kvm_pit_lost_tick_policy = {
3715 .driver = "kvm-pit",
3716 .property = "lost_tick_policy",
3717 .value = "discard",
c21fb4f8
JK
3718 };
3719
515cb8ef
EH
3720 error_report("warning: deprecated, replaced by "
3721 "-global kvm-pit.lost_tick_policy=discard");
16714b16 3722 qdev_prop_register_global(&kvm_pit_lost_tick_policy);
c21fb4f8
JK
3723 break;
3724 }
bb36d470 3725 case QEMU_OPTION_usb:
fa5358c6 3726 olist = qemu_find_opts("machine");
70b94331 3727 qemu_opts_parse_noisily(olist, "usb=on", false);
bb36d470 3728 break;
a594cfbf 3729 case QEMU_OPTION_usbdevice:
fa5358c6 3730 olist = qemu_find_opts("machine");
70b94331 3731 qemu_opts_parse_noisily(olist, "usb=on", false);
bd3c948d
GH
3732 add_device_config(DEV_USB, optarg);
3733 break;
3734 case QEMU_OPTION_device:
70b94331
MA
3735 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3736 optarg, true)) {
f31d07d1
GH
3737 exit(1);
3738 }
a594cfbf 3739 break;
6a00d601 3740 case QEMU_OPTION_smp:
70b94331
MA
3741 if (!qemu_opts_parse_noisily(qemu_find_opts("smp-opts"),
3742 optarg, true)) {
6be68d7e
JS
3743 exit(1);
3744 }
6a00d601 3745 break;
99efa84d 3746 case QEMU_OPTION_vnc:
7b1ee0f2 3747 vnc_parse(optarg, &error_fatal);
821601ea 3748 break;
6515b203
FB
3749 case QEMU_OPTION_no_acpi:
3750 acpi_enabled = 0;
3751 break;
16b29ae1
AL
3752 case QEMU_OPTION_no_hpet:
3753 no_hpet = 1;
3754 break;
7d4c3d53
MA
3755 case QEMU_OPTION_balloon:
3756 if (balloon_parse(optarg) < 0) {
3e515373 3757 error_report("unknown -balloon argument %s", optarg);
7d4c3d53
MA
3758 exit(1);
3759 }
df97b920 3760 break;
d1beab82
FB
3761 case QEMU_OPTION_no_reboot:
3762 no_reboot = 1;
3763 break;
b2f76161
AJ
3764 case QEMU_OPTION_no_shutdown:
3765 no_shutdown = 1;
3766 break;
9467cd46
AZ
3767 case QEMU_OPTION_show_cursor:
3768 cursor_hide = 0;
3769 break;
8fcb1b90 3770 case QEMU_OPTION_uuid:
9c5ce8db 3771 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
4cd70f34 3772 error_report("failed to parse UUID string: wrong format");
8fcb1b90
BS
3773 exit(1);
3774 }
fc3b3295 3775 qemu_uuid_set = true;
8fcb1b90 3776 break;
99efa84d
MA
3777 case QEMU_OPTION_option_rom:
3778 if (nb_option_roms >= MAX_OPTION_ROMS) {
3e515373 3779 error_report("too many option ROMs");
99efa84d
MA
3780 exit(1);
3781 }
70b94331
MA
3782 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3783 optarg, true);
49295ebc
MA
3784 if (!opts) {
3785 exit(1);
3786 }
2e55e842
GN
3787 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3788 option_rom[nb_option_roms].bootindex =
3789 qemu_opt_get_number(opts, "bootindex", -1);
3790 if (!option_rom[nb_option_roms].name) {
f61eddcb 3791 error_report("Option ROM file is not specified");
2e55e842
GN
3792 exit(1);
3793 }
99efa84d
MA
3794 nb_option_roms++;
3795 break;
8e71621f 3796 case QEMU_OPTION_semihosting:
cfe67cef
LA
3797 semihosting.enabled = true;
3798 semihosting.target = SEMIHOSTING_TARGET_AUTO;
a38bb079
LI
3799 break;
3800 case QEMU_OPTION_semihosting_config:
cfe67cef 3801 semihosting.enabled = true;
70b94331
MA
3802 opts = qemu_opts_parse_noisily(qemu_find_opts("semihosting-config"),
3803 optarg, false);
a38bb079 3804 if (opts != NULL) {
cfe67cef 3805 semihosting.enabled = qemu_opt_get_bool(opts, "enable",
a38bb079
LI
3806 true);
3807 const char *target = qemu_opt_get(opts, "target");
3808 if (target != NULL) {
3809 if (strcmp("native", target) == 0) {
cfe67cef 3810 semihosting.target = SEMIHOSTING_TARGET_NATIVE;
a38bb079 3811 } else if (strcmp("gdb", target) == 0) {
cfe67cef 3812 semihosting.target = SEMIHOSTING_TARGET_GDB;
a38bb079 3813 } else if (strcmp("auto", target) == 0) {
cfe67cef 3814 semihosting.target = SEMIHOSTING_TARGET_AUTO;
a38bb079 3815 } else {
3e515373 3816 error_report("unsupported semihosting-config %s",
f61eddcb 3817 optarg);
a38bb079
LI
3818 exit(1);
3819 }
3820 } else {
cfe67cef 3821 semihosting.target = SEMIHOSTING_TARGET_AUTO;
a38bb079 3822 }
a59d31a1
LA
3823 /* Set semihosting argument count and vector */
3824 qemu_opt_foreach(opts, add_semihosting_arg,
3825 &semihosting, NULL);
a38bb079 3826 } else {
3e515373 3827 error_report("unsupported semihosting-config %s", optarg);
a38bb079
LI
3828 exit(1);
3829 }
8e71621f 3830 break;
88eed34a 3831 case QEMU_OPTION_tdf:
515cb8ef 3832 error_report("warning: ignoring deprecated option");
88eed34a 3833 break;
c35734b2 3834 case QEMU_OPTION_name:
70b94331
MA
3835 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3836 optarg, true);
5d12f961
DDAG
3837 if (!opts) {
3838 exit(1);
3839 }
c35734b2 3840 break;
66508601
BS
3841 case QEMU_OPTION_prom_env:
3842 if (nb_prom_envs >= MAX_PROM_ENVS) {
3e515373 3843 error_report("too many prom variables");
66508601
BS
3844 exit(1);
3845 }
3846 prom_envs[nb_prom_envs] = optarg;
3847 nb_prom_envs++;
3848 break;
2b8f2d41
AZ
3849 case QEMU_OPTION_old_param:
3850 old_param = 1;
05ebd537 3851 break;
f3dcfada 3852 case QEMU_OPTION_clock:
6d327171
AB
3853 /* Clock options no longer exist. Keep this option for
3854 * backward compatibility.
3855 */
f3dcfada 3856 break;
7e0af5d0 3857 case QEMU_OPTION_startdate:
1ed2fc1f
JK
3858 configure_rtc_date_offset(optarg, 1);
3859 break;
3860 case QEMU_OPTION_rtc:
70b94331
MA
3861 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3862 false);
1ed2fc1f 3863 if (!opts) {
1ed2fc1f 3864 exit(1);
7e0af5d0 3865 }
1ed2fc1f 3866 configure_rtc(opts);
7e0af5d0 3867 break;
26a5f13b 3868 case QEMU_OPTION_tb_size:
d5ab9713
JK
3869 tcg_tb_size = strtol(optarg, NULL, 0);
3870 if (tcg_tb_size < 0) {
3871 tcg_tb_size = 0;
3872 }
26a5f13b 3873 break;
2e70f6ef 3874 case QEMU_OPTION_icount:
70b94331
MA
3875 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3876 optarg, true);
1ad9580b
ST
3877 if (!icount_opts) {
3878 exit(1);
3879 }
2e70f6ef 3880 break;
5bb7910a 3881 case QEMU_OPTION_incoming:
7c76235a
DDAG
3882 if (!incoming) {
3883 runstate_set(RUN_STATE_INMIGRATE);
3884 }
5bb7910a
AL
3885 incoming = optarg;
3886 break;
d8c208dd 3887 case QEMU_OPTION_nodefaults:
d44229c5 3888 has_defaults = 0;
d8c208dd 3889 break;
e37630ca 3890 case QEMU_OPTION_xen_domid:
ad96090a 3891 if (!(xen_available())) {
da002526 3892 error_report("Option not supported for this target");
ad96090a
BS
3893 exit(1);
3894 }
e37630ca
AL
3895 xen_domid = atoi(optarg);
3896 break;
3897 case QEMU_OPTION_xen_create:
ad96090a 3898 if (!(xen_available())) {
da002526 3899 error_report("Option not supported for this target");
ad96090a
BS
3900 exit(1);
3901 }
e37630ca
AL
3902 xen_mode = XEN_CREATE;
3903 break;
3904 case QEMU_OPTION_xen_attach:
ad96090a 3905 if (!(xen_available())) {
da002526 3906 error_report("Option not supported for this target");
ad96090a
BS
3907 exit(1);
3908 }
e37630ca
AL
3909 xen_mode = XEN_ATTACH;
3910 break;
ab6540d5 3911 case QEMU_OPTION_trace:
e9e0bb2a
DL
3912 g_free(trace_file);
3913 trace_file = trace_opt_parse(optarg);
ab6540d5 3914 break;
715a664a
GH
3915 case QEMU_OPTION_readconfig:
3916 {
dcfb0939
KW
3917 int ret = qemu_read_config_file(optarg);
3918 if (ret < 0) {
f61eddcb
EH
3919 error_report("read config %s: %s", optarg,
3920 strerror(-ret));
715a664a
GH
3921 exit(1);
3922 }
715a664a
GH
3923 break;
3924 }
29b0040b
GH
3925 case QEMU_OPTION_spice:
3926 olist = qemu_find_opts("spice");
3927 if (!olist) {
5dfdae81 3928 error_report("spice support is disabled");
29b0040b
GH
3929 exit(1);
3930 }
70b94331 3931 opts = qemu_opts_parse_noisily(olist, optarg, false);
29b0040b 3932 if (!opts) {
29b0040b
GH
3933 exit(1);
3934 }
f963e4d0 3935 display_remote++;
29b0040b 3936 break;
715a664a
GH
3937 case QEMU_OPTION_writeconfig:
3938 {
3939 FILE *fp;
3940 if (strcmp(optarg, "-") == 0) {
3941 fp = stdout;
3942 } else {
3943 fp = fopen(optarg, "w");
3944 if (fp == NULL) {
f61eddcb
EH
3945 error_report("open %s: %s", optarg,
3946 strerror(errno));
715a664a
GH
3947 exit(1);
3948 }
3949 }
3950 qemu_config_write(fp);
7fb8b5d9
CG
3951 if (fp != stdout) {
3952 fclose(fp);
3953 }
715a664a
GH
3954 break;
3955 }
c7f0f3b1
AL
3956 case QEMU_OPTION_qtest:
3957 qtest_chrdev = optarg;
3958 break;
3959 case QEMU_OPTION_qtest_log:
3960 qtest_log = optarg;
3961 break;
7d76ad4f 3962 case QEMU_OPTION_sandbox:
70b94331
MA
3963 opts = qemu_opts_parse_noisily(qemu_find_opts("sandbox"),
3964 optarg, true);
7d76ad4f 3965 if (!opts) {
49295ebc 3966 exit(1);
7d76ad4f
EO
3967 }
3968 break;
587ed6be
CB
3969 case QEMU_OPTION_add_fd:
3970#ifndef _WIN32
70b94331
MA
3971 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3972 optarg, false);
587ed6be 3973 if (!opts) {
49295ebc 3974 exit(1);
587ed6be
CB
3975 }
3976#else
3977 error_report("File descriptor passing is disabled on this "
3978 "platform");
3979 exit(1);
3980#endif
3981 break;
68d98d3e 3982 case QEMU_OPTION_object:
70b94331
MA
3983 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
3984 optarg, true);
49295ebc
MA
3985 if (!opts) {
3986 exit(1);
3987 }
68d98d3e 3988 break;
888a6bc6 3989 case QEMU_OPTION_realtime:
70b94331
MA
3990 opts = qemu_opts_parse_noisily(qemu_find_opts("realtime"),
3991 optarg, false);
888a6bc6
SM
3992 if (!opts) {
3993 exit(1);
3994 }
28d16f38 3995 enable_mlock = qemu_opt_get_bool(opts, "mlock", true);
888a6bc6 3996 break;
5e2ac519 3997 case QEMU_OPTION_msg:
70b94331
MA
3998 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3999 false);
5e2ac519
SA
4000 if (!opts) {
4001 exit(1);
4002 }
4003 configure_msg(opts);
4004 break;
abfd9ce3 4005 case QEMU_OPTION_dump_vmstate:
522abf69 4006 if (vmstate_dump_file) {
f61eddcb
EH
4007 error_report("only one '-dump-vmstate' "
4008 "option may be given");
522abf69
GA
4009 exit(1);
4010 }
abfd9ce3
AS
4011 vmstate_dump_file = fopen(optarg, "w");
4012 if (vmstate_dump_file == NULL) {
f61eddcb 4013 error_report("open %s: %s", optarg, strerror(errno));
abfd9ce3
AS
4014 exit(1);
4015 }
4016 break;
59a5264b
JS
4017 default:
4018 os_parse_cmd_args(popt->index, optarg);
cd6f1169 4019 }
0824d6fc
FB
4020 }
4021 }
43fa1e0b
EH
4022 /*
4023 * Clear error location left behind by the loop.
4024 * Best done right after the loop. Do not insert code here!
4025 */
4026 loc_set_none();
364c3e6b 4027
4c27b859
PD
4028 replay_configure(icount_opts);
4029
7580f231 4030 machine_class = select_machine();
076b35b5
ND
4031
4032 set_memory_options(&ram_slots, &maxram_size, machine_class);
3b9985e9 4033
2d9c2725
IM
4034 os_daemonize();
4035
004c8e00
MT
4036 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
4037 error_report("could not acquire pid file: %s", strerror(errno));
4038 exit(1);
4039 }
4040
2f78e491 4041 if (qemu_init_main_loop(&main_loop_err)) {
565f65d2 4042 error_report_err(main_loop_err);
1c53786f
PB
4043 exit(1);
4044 }
4045
28d0de7a
MA
4046 if (qemu_opts_foreach(qemu_find_opts("sandbox"),
4047 parse_sandbox, NULL, NULL)) {
7d76ad4f
EO
4048 exit(1);
4049 }
4050
28d0de7a
MA
4051 if (qemu_opts_foreach(qemu_find_opts("name"),
4052 parse_name, NULL, NULL)) {
5b9d313e
DDAG
4053 exit(1);
4054 }
4055
587ed6be 4056#ifndef _WIN32
28d0de7a
MA
4057 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4058 parse_add_fd, NULL, NULL)) {
587ed6be
CB
4059 exit(1);
4060 }
4061
28d0de7a
MA
4062 if (qemu_opts_foreach(qemu_find_opts("add-fd"),
4063 cleanup_add_fd, NULL, NULL)) {
587ed6be
CB
4064 exit(1);
4065 }
4066#endif
4067
0056ae24
MA
4068 current_machine = MACHINE(object_new(object_class_get_name(
4069 OBJECT_CLASS(machine_class))));
52eb3dfd
MA
4070 if (machine_help_func(qemu_get_machine_opts(), current_machine)) {
4071 exit(0);
4072 }
0056ae24
MA
4073 object_property_add_child(object_get_root(), "machine",
4074 OBJECT(current_machine), &error_abort);
20bccb82
PM
4075
4076 if (machine_class->minimum_page_bits) {
4077 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
4078 /* This would be a board error: specifying a minimum smaller than
4079 * a target's compile-time fixed setting.
4080 */
4081 g_assert_not_reached();
4082 }
4083 }
4084
b5c2c3d0 4085 cpu_exec_init_all();
0056ae24 4086
958db90c 4087 if (machine_class->hw_version) {
35c2c8dc 4088 qemu_set_hw_version(machine_class->hw_version);
93bfef4c
CV
4089 }
4090
c8057f95 4091 if (cpu_model && is_help_option(cpu_model)) {
1d6528af 4092 list_cpus(stdout, &fprintf, cpu_model);
ecf40bea
EH
4093 exit(0);
4094 }
4095
62cb4145
DL
4096 if (!trace_init_backends()) {
4097 exit(1);
4098 }
41fc57e4
PB
4099 trace_init_file(trace_file);
4100
67633bb4 4101 /* Open the logfile at this point and set the log mask if necessary.
c235d738 4102 */
67633bb4 4103 if (log_file) {
daa76aa4 4104 qemu_set_log_filename(log_file, &error_fatal);
67633bb4
PK
4105 }
4106
c235d738 4107 if (log_mask) {
b946bffa 4108 int mask;
b946bffa
PM
4109 mask = qemu_str_to_log_mask(log_mask);
4110 if (!mask) {
4111 qemu_print_log_usage(stdout);
4112 exit(1);
4113 }
4114 qemu_set_log(mask);
ed7f5f1d
PB
4115 } else {
4116 qemu_set_log(0);
c235d738 4117 }
330d0414 4118
5cea8590
PB
4119 /* If no data_dir is specified then try to find it relative to the
4120 executable path. */
4524051c 4121 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
10f5bff6 4122 data_dir[data_dir_idx] = os_find_datadir();
4524051c
GH
4123 if (data_dir[data_dir_idx] != NULL) {
4124 data_dir_idx++;
4125 }
5cea8590 4126 }
60474fb5 4127 /* If all else fails use the install path specified when building. */
4524051c
GH
4128 if (data_dir_idx < ARRAY_SIZE(data_dir)) {
4129 data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR;
5cea8590
PB
4130 }
4131
37146e7e
RJ
4132 /* -L help lists the data directories and exits. */
4133 if (list_data_dirs) {
4134 for (i = 0; i < data_dir_idx; i++) {
4135 printf("%s\n", data_dir[i]);
4136 }
4137 exit(0);
4138 }
4139
12b7f57e 4140 smp_parse(qemu_opts_find(qemu_find_opts("smp-opts"), NULL));
6be68d7e 4141
958db90c 4142 machine_class->max_cpus = machine_class->max_cpus ?: 1; /* Default to UP */
c00cd995 4143 if (max_cpus > machine_class->max_cpus) {
f61eddcb
EH
4144 error_report("Number of SMP CPUs requested (%d) exceeds max CPUs "
4145 "supported by machine '%s' (%d)", max_cpus,
4146 machine_class->name, machine_class->max_cpus);
b2097003
AL
4147 exit(1);
4148 }
4149
67b724e6
AP
4150 /*
4151 * Get the default machine options from the machine if it is not already
4152 * specified either by the configuration file or by the command line.
4153 */
958db90c 4154 if (machine_class->default_machine_opts) {
25de5935 4155 qemu_opts_set_defaults(qemu_find_opts("machine"),
958db90c 4156 machine_class->default_machine_opts, 0);
67b724e6
AP
4157 }
4158
28d0de7a
MA
4159 qemu_opts_foreach(qemu_find_opts("device"),
4160 default_driver_check, NULL, NULL);
4161 qemu_opts_foreach(qemu_find_opts("global"),
4162 default_driver_check, NULL, NULL);
998bbd74 4163
d44229c5
MW
4164 if (!vga_model && !default_vga) {
4165 vga_interface_type = VGA_DEVICE;
4166 }
958db90c 4167 if (!has_defaults || machine_class->no_serial) {
986c5f78
GH
4168 default_serial = 0;
4169 }
958db90c 4170 if (!has_defaults || machine_class->no_parallel) {
986c5f78
GH
4171 default_parallel = 0;
4172 }
958db90c 4173 if (!has_defaults || !machine_class->use_virtcon) {
986c5f78
GH
4174 default_virtcon = 0;
4175 }
958db90c 4176 if (!has_defaults || !machine_class->use_sclp) {
3ef669e1
AG
4177 default_sclp = 0;
4178 }
958db90c 4179 if (!has_defaults || machine_class->no_floppy) {
ac33f8fa
GH
4180 default_floppy = 0;
4181 }
958db90c 4182 if (!has_defaults || machine_class->no_cdrom) {
ac33f8fa
GH
4183 default_cdrom = 0;
4184 }
958db90c 4185 if (!has_defaults || machine_class->no_sdcard) {
ac33f8fa
GH
4186 default_sdcard = 0;
4187 }
d44229c5
MW
4188 if (!has_defaults) {
4189 default_monitor = 0;
4190 default_net = 0;
4191 default_vga = 0;
4192 }
986c5f78 4193
ab51b1d5
MT
4194 if (is_daemonized()) {
4195 /* According to documentation and historically, -nographic redirects
4196 * serial port, parallel port and monitor to stdio, which does not work
4197 * with -daemonize. We can redirect these to null instead, but since
4198 * -nographic is legacy, let's just error out.
4199 * We disallow -nographic only if all other ports are not redirected
4200 * explicitly, to not break existing legacy setups which uses
4201 * -nographic _and_ redirects all ports explicitly - this is valid
4202 * usage, -nographic is just a no-op in this case.
4203 */
cfc58cf3 4204 if (nographic
ab51b1d5
MT
4205 && (default_parallel || default_serial
4206 || default_monitor || default_virtcon)) {
4cd70f34 4207 error_report("-nographic cannot be used with -daemonize");
ab51b1d5
MT
4208 exit(1);
4209 }
4210#ifdef CONFIG_CURSES
4211 if (display_type == DT_CURSES) {
4cd70f34 4212 error_report("curses display cannot be used with -daemonize");
ab51b1d5
MT
4213 exit(1);
4214 }
4215#endif
4216 }
4217
cfc58cf3 4218 if (nographic) {
6a5e8b0e
GH
4219 if (default_parallel)
4220 add_device_config(DEV_PARALLEL, "null");
e1c09175
GH
4221 if (default_serial && default_monitor) {
4222 add_device_config(DEV_SERIAL, "mon:stdio");
986c5f78
GH
4223 } else if (default_virtcon && default_monitor) {
4224 add_device_config(DEV_VIRTCON, "mon:stdio");
3ef669e1
AG
4225 } else if (default_sclp && default_monitor) {
4226 add_device_config(DEV_SCLP, "mon:stdio");
e1c09175
GH
4227 } else {
4228 if (default_serial)
4229 add_device_config(DEV_SERIAL, "stdio");
986c5f78
GH
4230 if (default_virtcon)
4231 add_device_config(DEV_VIRTCON, "stdio");
3ef669e1
AG
4232 if (default_sclp) {
4233 add_device_config(DEV_SCLP, "stdio");
4234 }
e1c09175 4235 if (default_monitor)
4821cd4c 4236 monitor_parse("stdio", "readline", false);
e1c09175 4237 }
998bbd74
GH
4238 } else {
4239 if (default_serial)
4240 add_device_config(DEV_SERIAL, "vc:80Cx24C");
6a5e8b0e
GH
4241 if (default_parallel)
4242 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
abdeed06 4243 if (default_monitor)
4821cd4c 4244 monitor_parse("vc:80Cx24C", "readline", false);
38536da1
AG
4245 if (default_virtcon)
4246 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
3ef669e1
AG
4247 if (default_sclp) {
4248 add_device_config(DEV_SCLP, "vc:80Cx24C");
4249 }
bc0129d9
AL
4250 }
4251
006decd4
GH
4252#if defined(CONFIG_VNC)
4253 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
4254 display_remote++;
4255 }
4256#endif
15546425
AL
4257 if (display_type == DT_DEFAULT && !display_remote) {
4258#if defined(CONFIG_GTK)
4259 display_type = DT_GTK;
7b7d2be5 4260#elif defined(CONFIG_SDL)
15546425 4261 display_type = DT_SDL;
7b7d2be5
EH
4262#elif defined(CONFIG_COCOA)
4263 display_type = DT_COCOA;
15546425 4264#elif defined(CONFIG_VNC)
70b94331 4265 vnc_parse("localhost:0,to=99,id=default", &error_abort);
15546425
AL
4266#else
4267 display_type = DT_NONE;
4268#endif
4269 }
4270
047d4e15 4271 if ((no_frame || alt_grab || ctrl_grab) && display_type != DT_SDL) {
f61eddcb
EH
4272 error_report("-no-frame, -alt-grab and -ctrl-grab are only valid "
4273 "for SDL, ignoring option");
047d4e15
PW
4274 }
4275 if (no_quit && (display_type != DT_GTK && display_type != DT_SDL)) {
f61eddcb
EH
4276 error_report("-no-quit is only valid for GTK and SDL, "
4277 "ignoring option");
047d4e15
PW
4278 }
4279
15546425 4280 if (display_type == DT_GTK) {
97edf3bd 4281 early_gtk_display_init(request_opengl);
15546425 4282 }
19a2c626 4283
0b71a5d5
GH
4284 if (display_type == DT_SDL) {
4285 sdl_display_early_init(request_opengl);
4286 }
476db081 4287
0b71a5d5
GH
4288 if (request_opengl == 1 && display_opengl == 0) {
4289#if defined(CONFIG_OPENGL)
8afb9000 4290 error_report("OpenGL is not supported by the display");
0b71a5d5 4291#else
5dfdae81 4292 error_report("OpenGL support is disabled");
0b71a5d5
GH
4293#endif
4294 exit(1);
4295 }
15546425 4296
87f50caa 4297 page_size_init();
a5829fd9
T
4298 socket_init();
4299
1c7ba94a 4300 if (qemu_opts_foreach(qemu_find_opts("object"),
90998d58 4301 user_creatable_add_opts_foreach,
51b9b478 4302 object_create_initial, NULL)) {
1a688d3b 4303 exit(1);
a4c7367f
MA
4304 }
4305
2c189a4e
DB
4306 if (qemu_opts_foreach(qemu_find_opts("chardev"),
4307 chardev_init_func, NULL, NULL)) {
4308 exit(1);
4309 }
4310
758e8e38 4311#ifdef CONFIG_VIRTFS
28d0de7a
MA
4312 if (qemu_opts_foreach(qemu_find_opts("fsdev"),
4313 fsdev_init_func, NULL, NULL)) {
74db920c
GS
4314 exit(1);
4315 }
4316#endif
1a688d3b 4317
28d0de7a
MA
4318 if (qemu_opts_foreach(qemu_find_opts("device"),
4319 device_help_func, NULL, NULL)) {
3d1d9652
BR
4320 exit(0);
4321 }
4322
6b1b1440 4323 machine_opts = qemu_get_machine_opts();
71df1d83
MA
4324 if (qemu_opt_foreach(machine_opts, machine_set_property, current_machine,
4325 NULL)) {
6b1b1440
MA
4326 object_unref(OBJECT(current_machine));
4327 exit(1);
4328 }
4329
f6a1ef64 4330 configure_accelerator(current_machine);
214910a7 4331
2c189a4e 4332 if (qtest_chrdev) {
007b0657 4333 qtest_init(qtest_chrdev, qtest_log, &error_fatal);
2c189a4e
DB
4334 }
4335
2ff3de68
MA
4336 machine_opts = qemu_get_machine_opts();
4337 kernel_filename = qemu_opt_get(machine_opts, "kernel");
4338 initrd_filename = qemu_opt_get(machine_opts, "initrd");
4339 kernel_cmdline = qemu_opt_get(machine_opts, "append");
1442d3e6 4340 bios_name = qemu_opt_get(machine_opts, "firmware");
967c0da7 4341
8281abd5
MA
4342 opts = qemu_opts_find(qemu_find_opts("boot-opts"), NULL);
4343 if (opts) {
f05f47bb
GA
4344 boot_order = qemu_opt_get(opts, "order");
4345 if (boot_order) {
007b0657 4346 validate_bootdevices(boot_order, &error_fatal);
8281abd5
MA
4347 }
4348
f05f47bb
GA
4349 boot_once = qemu_opt_get(opts, "once");
4350 if (boot_once) {
007b0657 4351 validate_bootdevices(boot_once, &error_fatal);
8281abd5
MA
4352 }
4353
4354 boot_menu = qemu_opt_get_bool(opts, "menu", boot_menu);
e5187b56 4355 boot_strict = qemu_opt_get_bool(opts, "strict", false);
8281abd5
MA
4356 }
4357
f05f47bb
GA
4358 if (!boot_order) {
4359 boot_order = machine_class->default_boot_order;
4360 }
4361
a0abe474
PM
4362 if (!kernel_cmdline) {
4363 kernel_cmdline = "";
6b1b1440 4364 current_machine->kernel_cmdline = (char *)kernel_cmdline;
a0abe474
PM
4365 }
4366
a20dd508 4367 linux_boot = (kernel_filename != NULL);
6c41b272 4368
f8d39c01 4369 if (!linux_boot && *kernel_cmdline != '\0') {
f61eddcb 4370 error_report("-append only allowed with -kernel option");
f8d39c01
TS
4371 exit(1);
4372 }
4373
4374 if (!linux_boot && initrd_filename != NULL) {
f61eddcb 4375 error_report("-initrd only allowed with -kernel option");
f8d39c01
TS
4376 exit(1);
4377 }
4378
a59d31a1
LA
4379 if (semihosting_enabled() && !semihosting_get_argc() && kernel_filename) {
4380 /* fall back to the -kernel/-append */
4381 semihosting_arg_fallback(kernel_filename, kernel_cmdline);
4382 }
4383
9156d763 4384 os_set_line_buffering();
3b46e624 4385
ad1be899
AL
4386 /* spice needs the timers to be initialized by this point */
4387 qemu_spice_init();
ad1be899 4388
4603ea01 4389 cpu_ticks_init();
1ad9580b 4390 if (icount_opts) {
b0cb0a66
VP
4391 if (!tcg_enabled()) {
4392 error_report("-icount is not allowed with hardware virtualization");
1ad9580b
ST
4393 exit(1);
4394 }
4395 configure_icount(icount_opts, &error_abort);
4396 qemu_opts_del(icount_opts);
0abe905d 4397 }
634fce96 4398
d30300f7
EH
4399 if (default_net) {
4400 QemuOptsList *net = qemu_find_opts("net");
4401 qemu_opts_set(net, NULL, "type", "nic", &error_abort);
4402#ifdef CONFIG_SLIRP
4403 qemu_opts_set(net, NULL, "type", "user", &error_abort);
4404#endif
4405 }
4406
5821ebf9
HZ
4407 colo_info_init();
4408
dc1c9fe8
MM
4409 if (net_init_clients() < 0) {
4410 exit(1);
702c651c 4411 }
f1510b2c 4412
9abce56d 4413 if (qemu_opts_foreach(qemu_find_opts("object"),
90998d58 4414 user_creatable_add_opts_foreach,
51b9b478 4415 object_create_delayed, NULL)) {
9abce56d
YH
4416 exit(1);
4417 }
4418
d1a0cf73
SB
4419#ifdef CONFIG_TPM
4420 if (tpm_init() < 0) {
4421 exit(1);
4422 }
4423#endif
4424
dc72ac14 4425 /* init the bluetooth world */
bd3c948d
GH
4426 if (foreach_device_config(DEV_BT, bt_parse))
4427 exit(1);
dc72ac14 4428
834e76ea
AP
4429 if (!xen_enabled()) {
4430 /* On 32-bit hosts, QEMU is limited by virtual address space */
4431 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
f61eddcb 4432 error_report("at most 2047 MB RAM can be simulated");
834e76ea
AP
4433 exit(1);
4434 }
4435 }
9ae02555 4436
c163b5ca 4437 blk_mig_init();
0d6ab3ab 4438 ram_mig_init();
c163b5ca 4439
16026518
JS
4440 /* If the currently selected machine wishes to override the units-per-bus
4441 * property of its default HBA interface type, do so now. */
4442 if (machine_class->units_per_default_bus) {
4443 override_max_devs(machine_class->block_default_type,
4444 machine_class->units_per_default_bus);
4445 }
4446
e4bcb14c 4447 /* open the virtual block devices */
4c27b859
PD
4448 if (snapshot || replay_mode != REPLAY_MODE_NONE) {
4449 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
4450 NULL, NULL);
4451 }
2d0d2837 4452 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
28d0de7a 4453 &machine_class->block_default_type, NULL)) {
9dfd7c7a 4454 exit(1);
2d0d2837 4455 }
3e3d5815 4456
958db90c 4457 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
3c42ea66
CB
4458 CDROM_OPTS);
4459 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
4460 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
4e5d9b57 4461
57924bcd 4462 parse_numa_opts(machine_class);
268a362c 4463
28d0de7a
MA
4464 if (qemu_opts_foreach(qemu_find_opts("mon"),
4465 mon_init_func, NULL, NULL)) {
157b9319
JK
4466 exit(1);
4467 }
4468
998bbd74
GH
4469 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
4470 exit(1);
6a5e8b0e
GH
4471 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
4472 exit(1);
aee1b935
GH
4473 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
4474 exit(1);
3ef669e1
AG
4475 if (foreach_device_config(DEV_SCLP, sclp_parse) < 0) {
4476 exit(1);
4477 }
c9f398e5
PA
4478 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
4479 exit(1);
2796dae0 4480
7f1b17f2 4481 /* If no default VGA is requested, the default is "none". */
3605ded5 4482 if (default_vga) {
6f00494a
GH
4483 if (machine_class->default_display) {
4484 vga_model = machine_class->default_display;
c2c7b22d 4485 } else if (vga_interface_available(VGA_CIRRUS)) {
3605ded5 4486 vga_model = "cirrus";
c2c7b22d 4487 } else if (vga_interface_available(VGA_STD)) {
3605ded5
AJ
4488 vga_model = "std";
4489 }
a369da5f 4490 }
d44229c5
MW
4491 if (vga_model) {
4492 select_vgahw(vga_model);
4493 }
a369da5f 4494
09aaa160
MA
4495 if (watchdog) {
4496 i = select_watchdog(watchdog);
4497 if (i > 0)
4498 exit (i == 1 ? 1 : 0);
4499 }
4500
39a3b377
EH
4501 machine_register_compat_props(current_machine);
4502
8d76bfe8
EH
4503 qemu_opts_foreach(qemu_find_opts("global"),
4504 global_init_func, NULL, NULL);
d0fef6fb 4505
8bd7f71d
PD
4506 /* This checkpoint is required by replay to separate prior clock
4507 reading from the other reads, because timer polling functions query
4508 clock values from the log. */
4509 replay_checkpoint(CHECKPOINT_INIT);
1de81d28
AL
4510 qdev_machine_init();
4511
3ef96221 4512 current_machine->ram_size = ram_size;
c270fb9e
IM
4513 current_machine->maxram_size = maxram_size;
4514 current_machine->ram_slots = ram_slots;
3ef96221 4515 current_machine->boot_order = boot_order;
3ef96221
MA
4516 current_machine->cpu_model = cpu_model;
4517
4518 machine_class->init(current_machine);
3023f332 4519
28d16f38
HZ
4520 realtime_init();
4521
b3e6d591
PB
4522 audio_init();
4523
ea375f9a 4524 cpu_synchronize_all_post_init();
268a362c 4525
b0cb0a66
VP
4526 if (hax_enabled()) {
4527 hax_sync_vcpus();
4528 }
4529
81b2b810
GS
4530 if (qemu_opts_foreach(qemu_find_opts("fw_cfg"),
4531 parse_fw_cfg, fw_cfg_find(), NULL) != 0) {
4532 exit(1);
4533 }
4534
3023f332 4535 /* init USB devices */
4bcbe0b6 4536 if (machine_usb(current_machine)) {
0752706d
MA
4537 if (foreach_device_config(DEV_USB, usb_parse) < 0)
4538 exit(1);
3023f332
AL
4539 }
4540
79814179
TC
4541 /* Check if IGD GFX passthrough. */
4542 igd_gfx_passthru();
4543
bd3c948d 4544 /* init generic devices */
bab47d9a 4545 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
28d0de7a
MA
4546 if (qemu_opts_foreach(qemu_find_opts("device"),
4547 device_init_func, NULL, NULL)) {
bd3c948d 4548 exit(1);
a4c7367f 4549 }
9c6703fe
DL
4550
4551 numa_post_machine_init();
4552
bab47d9a 4553 rom_reset_order_override();
bd3c948d 4554
a66c9dc7
JS
4555 /* Did we create any drives that we failed to create a device for? */
4556 drive_check_orphaned();
4557
d30300f7
EH
4558 /* Don't warn about the default network setup that you get if
4559 * no command line -net or -netdev options are specified. There
4560 * are two cases that we would otherwise complain about:
4561 * (1) board doesn't support a NIC but the implicit "-net nic"
4562 * requested one
4563 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
4564 * sets up a nic that isn't connected to anything.
4565 */
4566 if (!default_net) {
4567 net_check_clients();
4568 }
4569
668680f7 4570
f05f47bb 4571 if (boot_once) {
07d04a02 4572 qemu_boot_set(boot_once, &error_fatal);
f05f47bb
GA
4573 qemu_register_reset(restore_boot_order, g_strdup(boot_order));
4574 }
4575
64840c66 4576 ds = init_displaystate();
993fbfdb 4577
6b62dc2d 4578 /* init local displays */
993fbfdb 4579 switch (display_type) {
993fbfdb 4580 case DT_CURSES:
ab51b1d5 4581 curses_display_init(ds, full_screen);
993fbfdb 4582 break;
993fbfdb
AL
4583 case DT_SDL:
4584 sdl_display_init(ds, full_screen, no_frame);
4585 break;
7b7d2be5 4586 case DT_COCOA:
993fbfdb
AL
4587 cocoa_display_init(ds, full_screen);
4588 break;
15546425 4589 case DT_GTK:
881249c7 4590 gtk_display_init(ds, full_screen, grab_on_hover);
15546425 4591 break;
6b62dc2d
GH
4592 default:
4593 break;
4594 }
4595
0ce235a7
GN
4596 /* must be after terminal init, SDL library changes signal handlers */
4597 os_setup_signal_handling();
4598
6b62dc2d 4599 /* init remote displays */
a663fbd9 4600#ifdef CONFIG_VNC
28d0de7a
MA
4601 qemu_opts_foreach(qemu_find_opts("vnc"),
4602 vnc_init_func, NULL, NULL);
a663fbd9 4603#endif
f8c75b24 4604
9fa03286
GH
4605 if (using_spice) {
4606 qemu_spice_display_init();
a3e22260 4607 }
5b08fc10 4608
ef0c4a0d 4609 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
59030a8c 4610 exit(1);
45669e00 4611 }
45669e00 4612
3418bd25
GH
4613 qdev_machine_creation_done();
4614
80376c3f
IY
4615 /* TODO: once all bus devices are qdevified, this should be done
4616 * when bus is created by qdev.c */
4617 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
4cab946a
GN
4618 qemu_run_machine_init_done_notifiers();
4619
6b3f7f63 4620 if (rom_check_and_register_reset() != 0) {
f61eddcb 4621 error_report("rom check and register reset failed");
6b3f7f63
EA
4622 exit(1);
4623 }
d916b464 4624
7615936e
PD
4625 replay_start();
4626
8bd7f71d
PD
4627 /* This checkpoint is required by replay to separate prior clock
4628 reading from the other reads, because timer polling functions query
4629 clock values from the log. */
4630 replay_checkpoint(CHECKPOINT_RESET);
e063eb1f 4631 qemu_system_reset(VMRESET_SILENT);
48212d87 4632 register_global_state();
05f2401e 4633 if (loadvm) {
03cd4655 4634 if (load_vmstate(loadvm) < 0) {
05f2401e
JQ
4635 autostart = 0;
4636 }
4637 }
d63d307f 4638
d828c430 4639 qdev_prop_check_globals();
abfd9ce3
AS
4640 if (vmstate_dump_file) {
4641 /* dump and exit */
4642 dump_vmstate_json_to_file(vmstate_dump_file);
4643 return 0;
4644 }
9f9260a3 4645
2bb8c10c 4646 if (incoming) {
43eaae28
PB
4647 Error *local_err = NULL;
4648 qemu_start_incoming_migration(incoming, &local_err);
4649 if (local_err) {
c29b77f9 4650 error_reportf_err(local_err, "-incoming %s: ", incoming);
43eaae28 4651 exit(1);
8ca5e801 4652 }
6b99dadc 4653 } else if (autostart) {
c0f4ce77 4654 vm_start();
6b99dadc 4655 }
ffd843bc 4656
eb505be1 4657 os_setup_post();
71e3ceb8 4658
8a7ddc38 4659 main_loop();
7615936e 4660 replay_disable_events();
dce8921b 4661 iothread_stop_all();
7615936e 4662
99435906
PB
4663 bdrv_close_all();
4664 pause_all_vcpus();
3d3b8303 4665 res_free();
b46a8906 4666
aa5cb7f5 4667 /* vhost-user must be cleaned up before chardevs. */
8caf911d 4668 net_cleanup();
a384c205 4669 audio_cleanup();
2ef45716 4670 monitor_cleanup();
aa5cb7f5 4671 qemu_chr_cleanup();
8caf911d 4672
0824d6fc
FB
4673 return 0;
4674}