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