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