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