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