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