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