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