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