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