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