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