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