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