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