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