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