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