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