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