]> git.ipfire.org Git - thirdparty/qemu.git/blame - softmmu/vl.c
vt82c686 machines: Support machine-default audiodev with fallback
[thirdparty/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"
49f95221 26#include "qemu/help-texts.h"
2c65db5e 27#include "qemu/datadir.h"
3dc54b0e 28#include "qemu/units.h"
c5e3c918 29#include "exec/cpu-common.h"
ec5f7ca8 30#include "exec/page-vary.h"
a27bd6c7 31#include "hw/qdev-properties.h"
6dd75472 32#include "qapi/compat-policy.h"
e688df6b 33#include "qapi/error.h"
2a5ad60b 34#include "qapi/qmp/qdict.h"
e4383ca2 35#include "qapi/qmp/qstring.h"
009ff893 36#include "qapi/qmp/qjson.h"
67a1de0d 37#include "qemu-version.h"
f348b6d1
VB
38#include "qemu/cutils.h"
39#include "qemu/help_option.h"
15e09912 40#include "qemu/hw-version.h"
cea25275 41#include "qemu/uuid.h"
71e8a915 42#include "sysemu/reset.h"
54d31236 43#include "sysemu/runstate.h"
e6dba048 44#include "sysemu/runstate-action.h"
9c17d615 45#include "sysemu/seccomp.h"
14a48c1d 46#include "sysemu/tcg.h"
da278d58 47#include "sysemu/xen.h"
452dfbef 48
d49b6836 49#include "qemu/error-report.h"
c8897e8e 50#include "qemu/sockets.h"
940e43aa 51#include "qemu/accel.h"
8a768db1 52#include "qemu/async-teardown.h"
511d2b14 53#include "hw/usb.h"
0d09e41a 54#include "hw/isa/isa.h"
fb8b660e 55#include "hw/scsi/scsi.h"
866e2b37 56#include "hw/display/vga.h"
a2eb5c0c 57#include "hw/firmware/smbios.h"
9f57061c 58#include "hw/acpi/acpi.h"
0d09e41a 59#include "hw/xen/xen.h"
45a50b16 60#include "hw/loader.h"
b4a42f81 61#include "monitor/qdev.h"
1422e32d 62#include "net/net.h"
68ac40d2 63#include "net/slirp.h"
83c9089e 64#include "monitor/monitor.h"
28ecbaee 65#include "ui/console.h"
e0d2bd51 66#include "ui/input.h"
9c17d615 67#include "sysemu/sysemu.h"
e35704ba 68#include "sysemu/numa.h"
900c0ba3 69#include "sysemu/hostmem.h"
022c62cb 70#include "exec/gdbstub.h"
1de7afc9 71#include "qemu/timer.h"
8228e353 72#include "chardev/char.h"
b33276a7 73#include "qemu/bitmap.h"
03dd024f 74#include "qemu/log.h"
9c17d615 75#include "sysemu/blockdev.h"
0d09e41a 76#include "hw/block/block.h"
c5e3c918
PB
77#include "hw/i386/x86.h"
78#include "hw/i386/pc.h"
7b1e1a22 79#include "migration/misc.h"
5e22479a 80#include "migration/snapshot.h"
bdee56f5 81#include "sysemu/tpm.h"
9c17d615 82#include "sysemu/dma.h"
8a824e4d 83#include "hw/audio/soundhw.h"
511d2b14 84#include "audio/audio.h"
4b4629d9 85#include "sysemu/cpus.h"
740b1759 86#include "sysemu/cpu-timers.h"
5821ebf9 87#include "migration/colo.h"
1693c64c 88#include "migration/postcopy-ram.h"
9c17d615 89#include "sysemu/kvm.h"
42e5f393 90#include "qapi/qobject-input-visitor.h"
1de7afc9
PB
91#include "qemu/option.h"
92#include "qemu/config-file.h"
1de7afc9 93#include "qemu/main-loop.h"
758e8e38 94#ifdef CONFIG_VIRTFS
74db920c
GS
95#include "fsdev/qemu-fsdev.h"
96#endif
9c17d615 97#include "sysemu/qtest.h"
5584e2db
IL
98#ifdef CONFIG_TCG
99#include "accel/tcg/perf.h"
100#endif
511d2b14 101
76cad711 102#include "disas/disas.h"
fc01f7e7 103
8b7a5507 104#include "trace.h"
e4858974 105#include "trace/control.h"
42229a75 106#include "qemu/plugin.h"
1de7afc9 107#include "qemu/queue.h"
9c17d615 108#include "sysemu/arch_init.h"
abc27d42 109#include "exec/confidential-guest-support.h"
72cf2d4f 110
29b0040b 111#include "ui/qemu-spice.h"
68d98d3e 112#include "qapi/string-input-visitor.h"
c4090f8e 113#include "qapi/opts-visitor.h"
776d1344 114#include "qapi/clone-visitor.h"
84321831 115#include "qom/object_interfaces.h"
6b5fe137 116#include "semihosting/semihost.h"
ddbb0d09 117#include "crypto/init.h"
b60c48a7 118#include "sysemu/replay.h"
9af23989 119#include "qapi/qapi-events-run-state.h"
039a6837
PB
120#include "qapi/qapi-types-audio.h"
121#include "qapi/qapi-visit-audio.h"
9af23989 122#include "qapi/qapi-visit-block-core.h"
6dd75472 123#include "qapi/qapi-visit-compat.h"
aadfe320 124#include "qapi/qapi-visit-machine.h"
776d1344 125#include "qapi/qapi-visit-ui.h"
112ed241 126#include "qapi/qapi-commands-block-core.h"
e69d50d6 127#include "qapi/qapi-commands-migration.h"
ee55686e 128#include "qapi/qapi-commands-misc.h"
bc2f4fcb 129#include "qapi/qapi-visit-qom.h"
e1ca8f7e 130#include "qapi/qapi-commands-ui.h"
215aea0c 131#include "block/qdict.h"
0194749a 132#include "qapi/qmp/qerror.h"
dce8921b 133#include "sysemu/iothread.h"
9c09a251 134#include "qemu/guest-random.h"
9ca9c893 135#include "qemu/keyval.h"
29b0040b 136
98b19252
AS
137#define MAX_VIRTIO_CONSOLES 1
138
64418657
PB
139typedef struct BlockdevOptionsQueueEntry {
140 BlockdevOptions *bdo;
141 Location loc;
142 QSIMPLEQ_ENTRY(BlockdevOptionsQueueEntry) entry;
143} BlockdevOptionsQueueEntry;
144
145typedef QSIMPLEQ_HEAD(, BlockdevOptionsQueueEntry) BlockdevOptionsQueue;
146
bc2f4fcb
PB
147typedef struct ObjectOption {
148 ObjectOptions *opts;
149 QTAILQ_ENTRY(ObjectOption) next;
150} ObjectOption;
151
5dacda51
KW
152typedef struct DeviceOption {
153 QDict *opts;
154 Location loc;
155 QTAILQ_ENTRY(DeviceOption) next;
156} DeviceOption;
157
0546c060 158static const char *cpu_option;
d8e4de41 159static const char *mem_path;
58c91595 160static const char *incoming;
90285ec8 161static const char *loadvm;
d8fb7d09 162static const char *accelerators;
ce9d03fb 163static bool have_custom_ram_size;
26f88d84 164static const char *ram_memdev_id;
d8fb7d09 165static QDict *machine_opts_dict;
bc2f4fcb 166static QTAILQ_HEAD(, ObjectOption) object_opts = QTAILQ_HEAD_INITIALIZER(object_opts);
5dacda51 167static QTAILQ_HEAD(, DeviceOption) device_opts = QTAILQ_HEAD_INITIALIZER(device_opts);
f650266b 168static int display_remote;
64418657 169static int snapshot;
7691bdef 170static bool preconfig_requested;
8a745974 171static QemuPluginList plugin_list = QTAILQ_HEAD_INITIALIZER(plugin_list);
64418657 172static BlockdevOptionsQueue bdo_queue = QSIMPLEQ_HEAD_INITIALIZER(bdo_queue);
f650266b 173static bool nographic = false;
4d2c17b0 174static int mem_prealloc; /* force preallocation of physical target memory */
f650266b 175static const char *vga_model = NULL;
0c8d7065 176static DisplayOptions dpy;
b8d880ba
PM
177static int num_serial_hds;
178static Chardev **serial_hds;
0546c060
PB
179static const char *log_mask;
180static const char *log_file;
181static bool list_data_dirs;
eaf65114
TH
182static const char *qtest_chrdev;
183static const char *qtest_log;
3cfb0456 184static bool opt_one_insn_per_tb;
1ca4d09a 185
d44229c5 186static int has_defaults = 1;
998bbd74 187static int default_serial = 1;
6a5e8b0e 188static int default_parallel = 1;
abdeed06 189static int default_monitor = 1;
ac33f8fa
GH
190static int default_floppy = 1;
191static int default_cdrom = 1;
192static int default_sdcard = 1;
7f1b17f2 193static int default_vga = 1;
d30300f7 194static int default_net = 1;
998bbd74
GH
195
196static struct {
197 const char *driver;
198 int *flag;
199} default_list[] = {
6a5e8b0e
GH
200 { .driver = "isa-serial", .flag = &default_serial },
201 { .driver = "isa-parallel", .flag = &default_parallel },
d8bcbabf 202 { .driver = "isa-fdc", .flag = &default_floppy },
a92bd191 203 { .driver = "floppy", .flag = &default_floppy },
af6bf132
MA
204 { .driver = "ide-cd", .flag = &default_cdrom },
205 { .driver = "ide-hd", .flag = &default_cdrom },
af6bf132 206 { .driver = "scsi-cd", .flag = &default_cdrom },
f6f99b48 207 { .driver = "scsi-hd", .flag = &default_cdrom },
7f1b17f2
PB
208 { .driver = "VGA", .flag = &default_vga },
209 { .driver = "isa-vga", .flag = &default_vga },
210 { .driver = "cirrus-vga", .flag = &default_vga },
211 { .driver = "isa-cirrus-vga", .flag = &default_vga },
212 { .driver = "vmware-svga", .flag = &default_vga },
213 { .driver = "qxl-vga", .flag = &default_vga },
a94f0c5c 214 { .driver = "virtio-vga", .flag = &default_vga },
862b4a29 215 { .driver = "ati-vga", .flag = &default_vga },
267f6646 216 { .driver = "vhost-user-vga", .flag = &default_vga },
f29d5261 217 { .driver = "virtio-vga-gl", .flag = &default_vga },
998bbd74
GH
218};
219
4d454574
PB
220static QemuOptsList qemu_rtc_opts = {
221 .name = "rtc",
222 .head = QTAILQ_HEAD_INITIALIZER(qemu_rtc_opts.head),
eb6a5209 223 .merge_lists = true,
4d454574
PB
224 .desc = {
225 {
226 .name = "base",
227 .type = QEMU_OPT_STRING,
228 },{
229 .name = "clock",
230 .type = QEMU_OPT_STRING,
231 },{
232 .name = "driftfix",
233 .type = QEMU_OPT_STRING,
234 },
235 { /* end of list */ }
236 },
237};
238
4d454574
PB
239static QemuOptsList qemu_option_rom_opts = {
240 .name = "option-rom",
241 .implied_opt_name = "romfile",
242 .head = QTAILQ_HEAD_INITIALIZER(qemu_option_rom_opts.head),
243 .desc = {
244 {
245 .name = "bootindex",
246 .type = QEMU_OPT_NUMBER,
247 }, {
248 .name = "romfile",
249 .type = QEMU_OPT_STRING,
250 },
251 { /* end of list */ }
252 },
253};
254
8d4e9146
FK
255static QemuOptsList qemu_accel_opts = {
256 .name = "accel",
257 .implied_opt_name = "accel",
258 .head = QTAILQ_HEAD_INITIALIZER(qemu_accel_opts.head),
8d4e9146 259 .desc = {
12ceaef6
PB
260 /*
261 * no elements => accept any
262 * sanity checking will happen later
263 * when setting accelerator properties
264 */
265 { }
8d4e9146
FK
266 },
267};
268
4d454574
PB
269static QemuOptsList qemu_boot_opts = {
270 .name = "boot-opts",
6ef4716c
MA
271 .implied_opt_name = "order",
272 .merge_lists = true,
4d454574
PB
273 .head = QTAILQ_HEAD_INITIALIZER(qemu_boot_opts.head),
274 .desc = {
4d454574
PB
275 {
276 .name = "order",
277 .type = QEMU_OPT_STRING,
278 }, {
279 .name = "once",
280 .type = QEMU_OPT_STRING,
281 }, {
282 .name = "menu",
6ef4716c 283 .type = QEMU_OPT_BOOL,
4d454574
PB
284 }, {
285 .name = "splash",
286 .type = QEMU_OPT_STRING,
287 }, {
288 .name = "splash-time",
6912bb0b 289 .type = QEMU_OPT_NUMBER,
4d454574
PB
290 }, {
291 .name = "reboot-timeout",
ee5d0f89 292 .type = QEMU_OPT_NUMBER,
c8a6ae8b
AK
293 }, {
294 .name = "strict",
e5187b56 295 .type = QEMU_OPT_BOOL,
4d454574
PB
296 },
297 { /*End of list */ }
298 },
299};
300
301static QemuOptsList qemu_add_fd_opts = {
302 .name = "add-fd",
303 .head = QTAILQ_HEAD_INITIALIZER(qemu_add_fd_opts.head),
304 .desc = {
305 {
306 .name = "fd",
307 .type = QEMU_OPT_NUMBER,
308 .help = "file descriptor of which a duplicate is added to fd set",
309 },{
310 .name = "set",
311 .type = QEMU_OPT_NUMBER,
312 .help = "ID of the fd set to add fd to",
313 },{
314 .name = "opaque",
315 .type = QEMU_OPT_STRING,
316 .help = "free-form string used to describe fd",
317 },
318 { /* end of list */ }
319 },
320};
321
322static QemuOptsList qemu_object_opts = {
323 .name = "object",
324 .implied_opt_name = "qom-type",
325 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
326 .desc = {
327 { }
328 },
329};
330
d1a0cf73
SB
331static QemuOptsList qemu_tpmdev_opts = {
332 .name = "tpmdev",
333 .implied_opt_name = "type",
334 .head = QTAILQ_HEAD_INITIALIZER(qemu_tpmdev_opts.head),
335 .desc = {
bb716238 336 /* options are defined in the TPM backends */
d1a0cf73
SB
337 { /* end of list */ }
338 },
339};
340
6f131f13
MT
341static QemuOptsList qemu_overcommit_opts = {
342 .name = "overcommit",
343 .head = QTAILQ_HEAD_INITIALIZER(qemu_overcommit_opts.head),
344 .desc = {
345 {
346 .name = "mem-lock",
347 .type = QEMU_OPT_BOOL,
348 },
349 {
350 .name = "cpu-pm",
351 .type = QEMU_OPT_BOOL,
352 },
353 { /* end of list */ }
354 },
355};
356
5e2ac519
SA
357static QemuOptsList qemu_msg_opts = {
358 .name = "msg",
359 .head = QTAILQ_HEAD_INITIALIZER(qemu_msg_opts.head),
360 .desc = {
361 {
362 .name = "timestamp",
363 .type = QEMU_OPT_BOOL,
364 },
2880ffb0
MS
365 {
366 .name = "guest-name",
367 .type = QEMU_OPT_BOOL,
368 .help = "Prepends guest name for error messages but only if "
369 "-name guest is set otherwise option is ignored\n",
370 },
5e2ac519
SA
371 { /* end of list */ }
372 },
373};
374
5d12f961
DDAG
375static QemuOptsList qemu_name_opts = {
376 .name = "name",
377 .implied_opt_name = "guest",
378 .merge_lists = true,
379 .head = QTAILQ_HEAD_INITIALIZER(qemu_name_opts.head),
380 .desc = {
381 {
382 .name = "guest",
383 .type = QEMU_OPT_STRING,
384 .help = "Sets the name of the guest.\n"
385 "This name will be displayed in the SDL window caption.\n"
386 "The name will also be used for the VNC server",
387 }, {
388 .name = "process",
389 .type = QEMU_OPT_STRING,
390 .help = "Sets the name of the QEMU process, as shown in top etc",
8f480de0
DDAG
391 }, {
392 .name = "debug-threads",
393 .type = QEMU_OPT_BOOL,
394 .help = "When enabled, name the individual threads; defaults off.\n"
395 "NOTE: The thread names are for debugging and not a\n"
396 "stable API.",
5d12f961
DDAG
397 },
398 { /* End of list */ }
399 },
400};
401
6e1d3c1c
IM
402static QemuOptsList qemu_mem_opts = {
403 .name = "memory",
404 .implied_opt_name = "size",
405 .head = QTAILQ_HEAD_INITIALIZER(qemu_mem_opts.head),
406 .merge_lists = true,
407 .desc = {
408 {
409 .name = "size",
410 .type = QEMU_OPT_SIZE,
411 },
c270fb9e
IM
412 {
413 .name = "slots",
414 .type = QEMU_OPT_NUMBER,
415 },
416 {
417 .name = "maxmem",
418 .type = QEMU_OPT_SIZE,
419 },
6e1d3c1c
IM
420 { /* end of list */ }
421 },
422};
423
1ad9580b
ST
424static QemuOptsList qemu_icount_opts = {
425 .name = "icount",
426 .implied_opt_name = "shift",
427 .merge_lists = true,
428 .head = QTAILQ_HEAD_INITIALIZER(qemu_icount_opts.head),
429 .desc = {
430 {
431 .name = "shift",
432 .type = QEMU_OPT_STRING,
a8bfac37
ST
433 }, {
434 .name = "align",
435 .type = QEMU_OPT_BOOL,
f1f4b57e
VC
436 }, {
437 .name = "sleep",
438 .type = QEMU_OPT_BOOL,
4c27b859
PD
439 }, {
440 .name = "rr",
441 .type = QEMU_OPT_STRING,
442 }, {
443 .name = "rrfile",
444 .type = QEMU_OPT_STRING,
9c2037d0
PD
445 }, {
446 .name = "rrsnapshot",
447 .type = QEMU_OPT_STRING,
1ad9580b
ST
448 },
449 { /* end of list */ }
450 },
451};
452
81b2b810
GS
453static QemuOptsList qemu_fw_cfg_opts = {
454 .name = "fw_cfg",
455 .implied_opt_name = "name",
456 .head = QTAILQ_HEAD_INITIALIZER(qemu_fw_cfg_opts.head),
457 .desc = {
458 {
459 .name = "name",
460 .type = QEMU_OPT_STRING,
461 .help = "Sets the fw_cfg name of the blob to be inserted",
462 }, {
463 .name = "file",
464 .type = QEMU_OPT_STRING,
679be303 465 .help = "Sets the name of the file from which "
81b2b810 466 "the fw_cfg blob will be loaded",
6407d76e
GS
467 }, {
468 .name = "string",
469 .type = QEMU_OPT_STRING,
470 .help = "Sets content of the blob to be inserted from a string",
6552d87c
PMD
471 }, {
472 .name = "gen_id",
473 .type = QEMU_OPT_STRING,
474 .help = "Sets id of the object generating the fw_cfg blob "
475 "to be inserted",
81b2b810
GS
476 },
477 { /* end of list */ }
478 },
479};
480
2a5ad60b
AJ
481static QemuOptsList qemu_action_opts = {
482 .name = "action",
483 .merge_lists = true,
484 .head = QTAILQ_HEAD_INITIALIZER(qemu_action_opts.head),
485 .desc = {
486 {
487 .name = "shutdown",
488 .type = QEMU_OPT_STRING,
489 },{
490 .name = "reboot",
491 .type = QEMU_OPT_STRING,
c753e8e7
AJ
492 },{
493 .name = "panic",
494 .type = QEMU_OPT_STRING,
2a5ad60b
AJ
495 },{
496 .name = "watchdog",
497 .type = QEMU_OPT_STRING,
498 },
499 { /* end of list */ }
500 },
501};
502
31459f46
RS
503const char *qemu_get_vm_name(void)
504{
505 return qemu_name;
506}
507
5dacda51 508static void default_driver_disable(const char *driver)
998bbd74 509{
998bbd74
GH
510 int i;
511
5dacda51
KW
512 if (!driver) {
513 return;
514 }
515
998bbd74
GH
516 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
517 if (strcmp(default_list[i].driver, driver) != 0)
518 continue;
519 *(default_list[i].flag) = 0;
520 }
5dacda51
KW
521}
522
523static int default_driver_check(void *opaque, QemuOpts *opts, Error **errp)
524{
525 const char *driver = qemu_opt_get(opts, "driver");
526
527 default_driver_disable(driver);
998bbd74
GH
528 return 0;
529}
530
5dacda51
KW
531static void default_driver_check_json(void)
532{
533 DeviceOption *opt;
534
535 QTAILQ_FOREACH(opt, &device_opts, next) {
536 const char *driver = qdict_get_try_str(opt->opts, "driver");
537 default_driver_disable(driver);
538 }
539}
540
28d0de7a 541static int parse_name(void *opaque, QemuOpts *opts, Error **errp)
5d12f961
DDAG
542{
543 const char *proc_name;
544
8f480de0
DDAG
545 if (qemu_opt_get(opts, "debug-threads")) {
546 qemu_thread_naming(qemu_opt_get_bool(opts, "debug-threads", false));
547 }
5d12f961
DDAG
548 qemu_name = qemu_opt_get(opts, "guest");
549
550 proc_name = qemu_opt_get(opts, "process");
551 if (proc_name) {
552 os_set_proc_name(proc_name);
553 }
5b9d313e
DDAG
554
555 return 0;
5d12f961
DDAG
556}
557
f8b6f8ed
MA
558bool defaults_enabled(void)
559{
560 return has_defaults;
561}
562
587ed6be 563#ifndef _WIN32
28d0de7a 564static int parse_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
565{
566 int fd, dupfd, flags;
567 int64_t fdset_id;
568 const char *fd_opaque = NULL;
636a30a8 569 AddfdInfo *fdinfo;
587ed6be
CB
570
571 fd = qemu_opt_get_number(opts, "fd", -1);
572 fdset_id = qemu_opt_get_number(opts, "set", -1);
573 fd_opaque = qemu_opt_get(opts, "opaque");
574
575 if (fd < 0) {
6353218b 576 error_setg(errp, "fd option is required and must be non-negative");
587ed6be
CB
577 return -1;
578 }
579
580 if (fd <= STDERR_FILENO) {
6353218b 581 error_setg(errp, "fd cannot be a standard I/O stream");
587ed6be
CB
582 return -1;
583 }
584
585 /*
586 * All fds inherited across exec() necessarily have FD_CLOEXEC
587 * clear, while qemu sets FD_CLOEXEC on all other fds used internally.
588 */
589 flags = fcntl(fd, F_GETFD);
590 if (flags == -1 || (flags & FD_CLOEXEC)) {
6353218b 591 error_setg(errp, "fd is not valid or already in use");
587ed6be
CB
592 return -1;
593 }
594
595 if (fdset_id < 0) {
6353218b 596 error_setg(errp, "set option is required and must be non-negative");
587ed6be
CB
597 return -1;
598 }
599
600#ifdef F_DUPFD_CLOEXEC
601 dupfd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
602#else
603 dupfd = dup(fd);
604 if (dupfd != -1) {
605 qemu_set_cloexec(dupfd);
606 }
607#endif
608 if (dupfd == -1) {
6353218b 609 error_setg(errp, "error duplicating fd: %s", strerror(errno));
587ed6be
CB
610 return -1;
611 }
612
613 /* add the duplicate fd, and optionally the opaque string, to the fd set */
9492718b 614 fdinfo = monitor_fdset_add_fd(dupfd, true, fdset_id, fd_opaque,
636a30a8
PB
615 &error_abort);
616 g_free(fdinfo);
587ed6be
CB
617
618 return 0;
619}
620
28d0de7a 621static int cleanup_add_fd(void *opaque, QemuOpts *opts, Error **errp)
587ed6be
CB
622{
623 int fd;
624
625 fd = qemu_opt_get_number(opts, "fd", -1);
626 close(fd);
627
628 return 0;
629}
630#endif
631
1ae26a18
AZ
632/***********************************************************/
633/* QEMU Block devices */
634
2292ddae
MA
635#define HD_OPTS "media=disk"
636#define CDROM_OPTS "media=cdrom"
637#define FD_OPTS ""
638#define PFLASH_OPTS ""
639#define MTD_OPTS ""
640#define SD_OPTS ""
e4bcb14c 641
28d0de7a 642static int drive_init_func(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 643{
2d0d2837 644 BlockInterfaceType *block_default_type = opaque;
9dfd7c7a 645
c4f26c9f 646 return drive_new(opts, *block_default_type, errp) == NULL;
9dfd7c7a
GH
647}
648
28d0de7a 649static int drive_enable_snapshot(void *opaque, QemuOpts *opts, Error **errp)
9dfd7c7a 650{
28de2f88 651 if (qemu_opt_get(opts, "snapshot") == NULL) {
f43e47db 652 qemu_opt_set(opts, "snapshot", "on", &error_abort);
9dfd7c7a
GH
653 }
654 return 0;
655}
656
3c42ea66
CB
657static void default_drive(int enable, int snapshot, BlockInterfaceType type,
658 int index, const char *optstr)
4e5d9b57
MA
659{
660 QemuOpts *opts;
a66c9dc7 661 DriveInfo *dinfo;
4e5d9b57 662
4e5d9b57
MA
663 if (!enable || drive_get_by_index(type, index)) {
664 return;
665 }
666
667 opts = drive_add(type, index, NULL, optstr);
668 if (snapshot) {
28d0de7a 669 drive_enable_snapshot(NULL, opts, NULL);
4e5d9b57 670 }
a66c9dc7 671
c4f26c9f 672 dinfo = drive_new(opts, type, &error_abort);
a66c9dc7
JS
673 dinfo->is_default = true;
674
4e5d9b57
MA
675}
676
d11bf9bf
MA
677static void configure_blockdev(BlockdevOptionsQueue *bdo_queue,
678 MachineClass *machine_class, int snapshot)
679{
680 /*
681 * If the currently selected machine wishes to override the
682 * units-per-bus property of its default HBA interface type, do so
683 * now.
684 */
685 if (machine_class->units_per_default_bus) {
686 override_max_devs(machine_class->block_default_type,
687 machine_class->units_per_default_bus);
688 }
689
690 /* open the virtual block devices */
691 while (!QSIMPLEQ_EMPTY(bdo_queue)) {
692 BlockdevOptionsQueueEntry *bdo = QSIMPLEQ_FIRST(bdo_queue);
693
694 QSIMPLEQ_REMOVE_HEAD(bdo_queue, entry);
695 loc_push_restore(&bdo->loc);
696 qmp_blockdev_add(bdo->bdo, &error_fatal);
697 loc_pop(&bdo->loc);
698 qapi_free_BlockdevOptions(bdo->bdo);
699 g_free(bdo);
700 }
25863975 701 if (snapshot) {
d11bf9bf
MA
702 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot,
703 NULL, NULL);
704 }
705 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func,
706 &machine_class->block_default_type, &error_fatal)) {
707 /* We printed help */
708 exit(0);
709 }
710
711 default_drive(default_cdrom, snapshot, machine_class->block_default_type, 2,
712 CDROM_OPTS);
713 default_drive(default_floppy, snapshot, IF_FLOPPY, 0, FD_OPTS);
714 default_drive(default_sdcard, snapshot, IF_SD, 0, SD_OPTS);
715
716}
717
12b7f57e
MT
718static QemuOptsList qemu_smp_opts = {
719 .name = "smp-opts",
720 .implied_opt_name = "cpus",
721 .merge_lists = true,
722 .head = QTAILQ_HEAD_INITIALIZER(qemu_smp_opts.head),
723 .desc = {
724 {
725 .name = "cpus",
726 .type = QEMU_OPT_NUMBER,
727 }, {
728 .name = "sockets",
729 .type = QEMU_OPT_NUMBER,
1b458422
LX
730 }, {
731 .name = "dies",
732 .type = QEMU_OPT_NUMBER,
864c3b5c
YW
733 }, {
734 .name = "clusters",
735 .type = QEMU_OPT_NUMBER,
12b7f57e
MT
736 }, {
737 .name = "cores",
738 .type = QEMU_OPT_NUMBER,
739 }, {
740 .name = "threads",
741 .type = QEMU_OPT_NUMBER,
742 }, {
743 .name = "maxcpus",
744 .type = QEMU_OPT_NUMBER,
745 },
746 { /*End of list */ }
747 },
748};
749
8a768db1
MT
750#if defined(CONFIG_POSIX)
751static QemuOptsList qemu_run_with_opts = {
752 .name = "run-with",
753 .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head),
754 .desc = {
755#if defined(CONFIG_LINUX)
756 {
757 .name = "async-teardown",
758 .type = QEMU_OPT_BOOL,
759 },
760#endif
761 {
762 .name = "chroot",
763 .type = QEMU_OPT_STRING,
764 },
765 { /* end of list */ }
766 },
767};
768
769#define qemu_add_run_with_opts() qemu_add_opts(&qemu_run_with_opts)
770
771#else
772
773#define qemu_add_run_with_opts()
774
775#endif /* CONFIG_POSIX */
776
28d16f38 777static void realtime_init(void)
888a6bc6 778{
888a6bc6
SM
779 if (enable_mlock) {
780 if (os_mlock() < 0) {
f61eddcb 781 error_report("locking memory failed");
888a6bc6
SM
782 exit(1);
783 }
784 }
785}
786
5e2ac519
SA
787
788static void configure_msg(QemuOpts *opts)
789{
651d588f 790 message_with_timestamp = qemu_opt_get_bool(opts, "timestamp", false);
2880ffb0 791 error_with_guestname = qemu_opt_get_bool(opts, "guest-name", false);
5e2ac519
SA
792}
793
a59d31a1 794
a594cfbf
FB
795/***********************************************************/
796/* USB devices */
797
fb08000c 798static int usb_device_add(const char *devname)
a594cfbf 799{
a5d2f727 800 USBDevice *dev = NULL;
a594cfbf 801
4bcbe0b6 802 if (!machine_usb(current_machine)) {
a594cfbf 803 return -1;
094b287f 804 }
a594cfbf 805
0958b4cc 806 dev = usbdevice_create(devname);
0d92ed30
PB
807 if (!dev)
808 return -1;
809
a594cfbf
FB
810 return 0;
811}
812
bd3c948d
GH
813static int usb_parse(const char *cmdline)
814{
59d1c1c2 815 int r;
fb08000c 816 r = usb_device_add(cmdline);
59d1c1c2 817 if (r < 0) {
f61eddcb 818 error_report("could not add USB device '%s'", cmdline);
59d1c1c2
ST
819 }
820 return r;
bd3c948d
GH
821}
822
cc1daa40
FB
823/***********************************************************/
824/* machine registration */
825
c516cd1b 826static MachineClass *find_machine(const char *name, GSList *machines)
cc1daa40 827{
c516cd1b 828 GSList *el;
cc1daa40 829
261747f1 830 for (el = machines; el; el = el->next) {
f2c93021 831 MachineClass *mc = el->data;
261747f1 832
f2c93021
MA
833 if (!strcmp(mc->name, name) || !g_strcmp0(mc->alias, name)) {
834 return mc;
261747f1 835 }
cc1daa40 836 }
261747f1 837
f2c93021 838 return NULL;
cc1daa40
FB
839}
840
c516cd1b 841static MachineClass *find_default_machine(GSList *machines)
0c257437 842{
c516cd1b 843 GSList *el;
6db1857e 844 MachineClass *default_machineclass = NULL;
0c257437 845
261747f1 846 for (el = machines; el; el = el->next) {
f2c93021 847 MachineClass *mc = el->data;
261747f1 848
f2c93021 849 if (mc->is_default) {
6db1857e
PMD
850 assert(default_machineclass == NULL && "Multiple default machines");
851 default_machineclass = mc;
0c257437
AL
852 }
853 }
261747f1 854
6db1857e 855 return default_machineclass;
0c257437
AL
856}
857
9bd7e6d9
PB
858static void version(void)
859{
7e563bfb 860 printf("QEMU emulator version " QEMU_FULL_VERSION "\n"
d915b7bb 861 QEMU_COPYRIGHT "\n");
9bd7e6d9
PB
862}
863
15f82208 864static void help(int exitcode)
0824d6fc 865{
a3adb7ad
ME
866 version();
867 printf("usage: %s [options] [disk_image]\n\n"
868 "'disk_image' is a raw hard disk image for IDE hard disk 0\n\n",
336d354b 869 g_get_prgname());
a3adb7ad 870
fd5fc4b1
PB
871#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
872 if ((arch_mask) & arch_type) \
873 fputs(opt_help, stdout);
874
875#define ARCHHEADING(text, arch_mask) \
876 if ((arch_mask) & arch_type) \
877 puts(stringify(text));
878
879#define DEFHEADING(text) ARCHHEADING(text, QEMU_ARCH_ALL)
880
881#include "qemu-options.def"
a3adb7ad
ME
882
883 printf("\nDuring emulation, the following keys are useful:\n"
3f020d70 884 "ctrl-alt-f toggle full screen\n"
885 "ctrl-alt-n switch to virtual console 'n'\n"
886 "ctrl-alt toggle mouse and keyboard grab\n"
887 "\n"
f5048cb7
EB
888 "When using -nographic, press 'ctrl-a h' to get some help.\n"
889 "\n"
890 QEMU_HELP_BOTTOM "\n");
a3adb7ad 891
15f82208 892 exit(exitcode);
0824d6fc
FB
893}
894
1a1dd721
MT
895enum {
896
897#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
898 opt_enum,
899#define DEFHEADING(text)
900#define ARCHHEADING(text, arch_mask)
901
902#include "qemu-options.def"
903};
904
cd6f1169
FB
905#define HAS_ARG 0x0001
906
cd6f1169
FB
907typedef struct QEMUOption {
908 const char *name;
909 int flags;
910 int index;
ad96090a 911 uint32_t arch_mask;
cd6f1169
FB
912} QEMUOption;
913
dbed7e40 914static const QEMUOption qemu_options[] = {
ad96090a 915 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
fd5fc4b1
PB
916
917#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
918 { option, opt_arg, opt_enum, arch_mask },
919#define DEFHEADING(text)
920#define ARCHHEADING(text, arch_mask)
921
922#include "qemu-options.def"
feea81d8 923 { /* end of list */ }
fc01f7e7 924};
a369da5f 925
8c9a2b71
EH
926typedef struct VGAInterfaceInfo {
927 const char *opt_name; /* option name */
928 const char *name; /* human-readable name */
c2c7b22d
EH
929 /* Class names indicating that support is available.
930 * If no class is specified, the interface is always available */
931 const char *class_names[2];
8c9a2b71
EH
932} VGAInterfaceInfo;
933
53b93511 934static const VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
8c9a2b71
EH
935 [VGA_NONE] = {
936 .opt_name = "none",
d2fa65cd 937 .name = "no graphic card",
8c9a2b71
EH
938 },
939 [VGA_STD] = {
940 .opt_name = "std",
941 .name = "standard VGA",
c2c7b22d 942 .class_names = { "VGA", "isa-vga" },
8c9a2b71
EH
943 },
944 [VGA_CIRRUS] = {
945 .opt_name = "cirrus",
946 .name = "Cirrus VGA",
c2c7b22d 947 .class_names = { "cirrus-vga", "isa-cirrus-vga" },
8c9a2b71
EH
948 },
949 [VGA_VMWARE] = {
950 .opt_name = "vmware",
951 .name = "VMWare SVGA",
c2c7b22d 952 .class_names = { "vmware-svga" },
8c9a2b71
EH
953 },
954 [VGA_VIRTIO] = {
955 .opt_name = "virtio",
956 .name = "Virtio VGA",
c2c7b22d 957 .class_names = { "virtio-vga" },
8c9a2b71
EH
958 },
959 [VGA_QXL] = {
960 .opt_name = "qxl",
961 .name = "QXL VGA",
c2c7b22d 962 .class_names = { "qxl-vga" },
8c9a2b71
EH
963 },
964 [VGA_TCX] = {
965 .opt_name = "tcx",
966 .name = "TCX framebuffer",
e178113f 967 .class_names = { "sun-tcx" },
8c9a2b71
EH
968 },
969 [VGA_CG3] = {
970 .opt_name = "cg3",
971 .name = "CG3 framebuffer",
c2c7b22d 972 .class_names = { "cgthree" },
8c9a2b71 973 },
f07b3a83 974#ifdef CONFIG_XEN_BACKEND
8c9a2b71
EH
975 [VGA_XENFB] = {
976 .opt_name = "xenfb",
d2fa65cd 977 .name = "Xen paravirtualized framebuffer",
8c9a2b71 978 },
f07b3a83 979#endif
8c9a2b71
EH
980};
981
c2c7b22d
EH
982static bool vga_interface_available(VGAInterfaceType t)
983{
53b93511 984 const VGAInterfaceInfo *ti = &vga_interfaces[t];
c2c7b22d
EH
985
986 assert(t < VGA_TYPE_MAX);
987 return !ti->class_names[0] ||
7ab6e7fc
GH
988 module_object_class_by_name(ti->class_names[0]) ||
989 module_object_class_by_name(ti->class_names[1]);
c2c7b22d
EH
990}
991
dceb8852
MAL
992static const char *
993get_default_vga_model(const MachineClass *machine_class)
994{
995 if (machine_class->default_display) {
d563cc84
TH
996 for (int t = 0; t < VGA_TYPE_MAX; t++) {
997 const VGAInterfaceInfo *ti = &vga_interfaces[t];
998
999 if (ti->opt_name && vga_interface_available(t) &&
1000 g_str_equal(ti->opt_name, machine_class->default_display)) {
1001 return machine_class->default_display;
1002 }
1003 }
1004
1005 warn_report_once("Default display '%s' is not available in this binary",
1006 machine_class->default_display);
1007 return NULL;
dceb8852
MAL
1008 } else if (vga_interface_available(VGA_CIRRUS)) {
1009 return "cirrus";
1010 } else if (vga_interface_available(VGA_STD)) {
1011 return "std";
1012 }
1013
1014 return NULL;
1015}
1016
1017static void select_vgahw(const MachineClass *machine_class, const char *p)
3893c124 1018{
1019 const char *opts;
8c9a2b71 1020 int t;
3893c124 1021
dceb8852
MAL
1022 if (g_str_equal(p, "help")) {
1023 const char *def = get_default_vga_model(machine_class);
1024
1025 for (t = 0; t < VGA_TYPE_MAX; t++) {
1026 const VGAInterfaceInfo *ti = &vga_interfaces[t];
1027
1028 if (vga_interface_available(t) && ti->opt_name) {
1029 printf("%-20s %s%s\n", ti->opt_name, ti->name ?: "",
6985d8ed
GZ
1030 (def && g_str_equal(ti->opt_name, def)) ?
1031 " (default)" : "");
dceb8852
MAL
1032 }
1033 }
1034 exit(0);
1035 }
1036
d44229c5 1037 assert(vga_interface_type == VGA_NONE);
8c9a2b71 1038 for (t = 0; t < VGA_TYPE_MAX; t++) {
53b93511 1039 const VGAInterfaceInfo *ti = &vga_interfaces[t];
8c9a2b71 1040 if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
c2c7b22d 1041 if (!vga_interface_available(t)) {
8c9a2b71
EH
1042 error_report("%s not available", ti->name);
1043 exit(1);
1044 }
1045 vga_interface_type = t;
1046 break;
af87bf29 1047 }
8c9a2b71
EH
1048 }
1049 if (t == VGA_TYPE_MAX) {
3893c124 1050 invalid_vga:
3e515373 1051 error_report("unknown vga type: %s", p);
3893c124 1052 exit(1);
1053 }
cb5a7aa8 1054 while (*opts) {
1055 const char *nextopt;
1056
1057 if (strstart(opts, ",retrace=", &nextopt)) {
1058 opts = nextopt;
1059 if (strstart(opts, "dumb", &nextopt))
1060 vga_retrace_method = VGA_RETRACE_DUMB;
1061 else if (strstart(opts, "precise", &nextopt))
1062 vga_retrace_method = VGA_RETRACE_PRECISE;
1063 else goto invalid_vga;
1064 } else goto invalid_vga;
1065 opts = nextopt;
1066 }
3893c124 1067}
1068
776d1344
GH
1069static void parse_display_qapi(const char *optarg)
1070{
776d1344
GH
1071 DisplayOptions *opts;
1072 Visitor *v;
1073
50beeb68 1074 v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
776d1344
GH
1075
1076 visit_type_DisplayOptions(v, NULL, &opts, &error_fatal);
1077 QAPI_CLONE_MEMBERS(DisplayOptions, &dpy, opts);
1078
1079 qapi_free_DisplayOptions(opts);
1080 visit_free(v);
1081}
1082
e1ca8f7e
GH
1083DisplayOptions *qmp_query_display_options(Error **errp)
1084{
1085 return QAPI_CLONE(DisplayOptions, &dpy);
1086}
1087
e3af9f9a 1088static void parse_display(const char *p)
1472a95b
JS
1089{
1090 const char *opts;
1472a95b 1091
c388f408
TH
1092 if (is_help_option(p)) {
1093 qemu_display_help();
1094 exit(0);
1095 }
1096
9eafdeea 1097 if (strstart(p, "vnc", &opts)) {
7a61f438
GH
1098 /*
1099 * vnc isn't a (local) DisplayType but a protocol for remote
1100 * display access.
1101 */
4db14629 1102 if (*opts == '=') {
653c9747 1103 vnc_parse(opts + 1);
4db14629 1104 } else {
c6bf0f7f 1105 error_report("VNC requires a display argument vnc=<display>");
3264ff12
JS
1106 exit(1);
1107 }
1472a95b 1108 } else {
776d1344 1109 parse_display_qapi(p);
1472a95b 1110 }
1472a95b
JS
1111}
1112
6407d76e
GS
1113static inline bool nonempty_str(const char *str)
1114{
1115 return str && *str;
1116}
1117
81b2b810
GS
1118static int parse_fw_cfg(void *opaque, QemuOpts *opts, Error **errp)
1119{
1120 gchar *buf;
1121 size_t size;
6552d87c 1122 const char *name, *file, *str, *gen_id;
bab47d9a 1123 FWCfgState *fw_cfg = (FWCfgState *) opaque;
81b2b810 1124
bab47d9a 1125 if (fw_cfg == NULL) {
406b6367 1126 error_setg(errp, "fw_cfg device not available");
81b2b810
GS
1127 return -1;
1128 }
1129 name = qemu_opt_get(opts, "name");
1130 file = qemu_opt_get(opts, "file");
6407d76e 1131 str = qemu_opt_get(opts, "string");
6552d87c 1132 gen_id = qemu_opt_get(opts, "gen_id");
6407d76e 1133
6552d87c
PMD
1134 /* we need the name, and exactly one of: file, content string, gen_id */
1135 if (!nonempty_str(name) ||
1136 nonempty_str(file) + nonempty_str(str) + nonempty_str(gen_id) != 1) {
1137 error_setg(errp, "name, plus exactly one of file,"
1138 " string and gen_id, are needed");
81b2b810
GS
1139 return -1;
1140 }
1141 if (strlen(name) > FW_CFG_MAX_FILE_PATH - 1) {
406b6367
MA
1142 error_setg(errp, "name too long (max. %d char)",
1143 FW_CFG_MAX_FILE_PATH - 1);
81b2b810
GS
1144 return -1;
1145 }
f7d8afb1
PMD
1146 if (nonempty_str(gen_id)) {
1147 /*
1148 * In this particular case where the content is populated
1149 * internally, the "etc/" namespace protection is relaxed,
1150 * so do not emit a warning.
1151 */
1152 } else if (strncmp(name, "opt/", 4) != 0) {
3dc6f869
AF
1153 warn_report("externally provided fw_cfg item names "
1154 "should be prefixed with \"opt/\"");
81b2b810 1155 }
6407d76e
GS
1156 if (nonempty_str(str)) {
1157 size = strlen(str); /* NUL terminator NOT included in fw_cfg blob */
1158 buf = g_memdup(str, size);
6552d87c 1159 } else if (nonempty_str(gen_id)) {
07719518 1160 if (!fw_cfg_add_from_generator(fw_cfg, name, gen_id, errp)) {
6552d87c
PMD
1161 return -1;
1162 }
1163 return 0;
6407d76e 1164 } else {
353c7d58
LQ
1165 GError *err = NULL;
1166 if (!g_file_get_contents(file, &buf, &size, &err)) {
1167 error_setg(errp, "can't load %s: %s", file, err->message);
1168 g_error_free(err);
6407d76e
GS
1169 return -1;
1170 }
81b2b810 1171 }
bab47d9a
GH
1172 /* For legacy, keep user files in a specific global order. */
1173 fw_cfg_set_order_override(fw_cfg, FW_CFG_ORDER_OVERRIDE_USER);
1174 fw_cfg_add_file(fw_cfg, name, buf, size);
1175 fw_cfg_reset_order_override(fw_cfg);
81b2b810
GS
1176 return 0;
1177}
1178
28d0de7a 1179static int device_help_func(void *opaque, QemuOpts *opts, Error **errp)
ff952ba2
MA
1180{
1181 return qdev_device_help(opts);
1182}
1183
28d0de7a 1184static int device_init_func(void *opaque, QemuOpts *opts, Error **errp)
f31d07d1
GH
1185{
1186 DeviceState *dev;
1187
cd65f349 1188 dev = qdev_device_add(opts, errp);
f3a85056
JF
1189 if (!dev && *errp) {
1190 error_report_err(*errp);
f31d07d1 1191 return -1;
f3a85056
JF
1192 } else if (dev) {
1193 object_unref(OBJECT(dev));
f006cf7f 1194 }
f31d07d1
GH
1195 return 0;
1196}
1197
28d0de7a 1198static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)
1a688d3b 1199{
bd2d80b2 1200 Error *local_err = NULL;
1a688d3b 1201
4ad6f6cb 1202 if (!qemu_chr_new_from_opts(opts, NULL, &local_err)) {
0ec846bf 1203 if (local_err) {
dd4af977 1204 error_propagate(errp, local_err);
0ec846bf
AN
1205 return -1;
1206 }
1207 exit(0);
bd2d80b2 1208 }
1a688d3b
GH
1209 return 0;
1210}
1211
758e8e38 1212#ifdef CONFIG_VIRTFS
28d0de7a 1213static int fsdev_init_func(void *opaque, QemuOpts *opts, Error **errp)
74db920c 1214{
b836723d 1215 return qemu_fsdev_add(opts, errp);
74db920c
GS
1216}
1217#endif
1218
28d0de7a 1219static int mon_init_func(void *opaque, QemuOpts *opts, Error **errp)
88589343 1220{
c3e95551 1221 return monitor_init_opts(opts, errp);
88589343
GH
1222}
1223
4821cd4c 1224static void monitor_parse(const char *optarg, const char *mode, bool pretty)
88589343
GH
1225{
1226 static int monitor_device_index = 0;
1227 QemuOpts *opts;
1228 const char *p;
1229 char label[32];
88589343
GH
1230
1231 if (strstart(optarg, "chardev:", &p)) {
1232 snprintf(label, sizeof(label), "%s", p);
1233 } else {
140e065d
JK
1234 snprintf(label, sizeof(label), "compat_monitor%d",
1235 monitor_device_index);
95e30b2a 1236 opts = qemu_chr_parse_compat(label, optarg, true);
88589343 1237 if (!opts) {
f61eddcb 1238 error_report("parse error: %s", optarg);
88589343
GH
1239 exit(1);
1240 }
1241 }
1242
822ac12d 1243 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1, &error_fatal);
f43e47db
MA
1244 qemu_opt_set(opts, "mode", mode, &error_abort);
1245 qemu_opt_set(opts, "chardev", label, &error_abort);
3c45f625
KW
1246 if (!strcmp(mode, "control")) {
1247 qemu_opt_set_bool(opts, "pretty", pretty, &error_abort);
1248 } else {
1249 assert(pretty == false);
1250 }
88589343
GH
1251 monitor_device_index++;
1252}
1253
bd3c948d
GH
1254struct device_config {
1255 enum {
aee1b935 1256 DEV_USB, /* -usbdevice */
aee1b935
GH
1257 DEV_SERIAL, /* -serial */
1258 DEV_PARALLEL, /* -parallel */
c9f398e5 1259 DEV_DEBUGCON, /* -debugcon */
ef0c4a0d 1260 DEV_GDB, /* -gdb, -s */
3ef669e1 1261 DEV_SCLP, /* s390 sclp */
bd3c948d
GH
1262 } type;
1263 const char *cmdline;
d9a5954d 1264 Location loc;
72cf2d4f 1265 QTAILQ_ENTRY(device_config) next;
bd3c948d 1266};
4fdcac0e
BS
1267
1268static QTAILQ_HEAD(, device_config) device_configs =
1269 QTAILQ_HEAD_INITIALIZER(device_configs);
bd3c948d
GH
1270
1271static void add_device_config(int type, const char *cmdline)
1272{
1273 struct device_config *conf;
1274
7267c094 1275 conf = g_malloc0(sizeof(*conf));
bd3c948d
GH
1276 conf->type = type;
1277 conf->cmdline = cmdline;
d9a5954d 1278 loc_save(&conf->loc);
72cf2d4f 1279 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
bd3c948d
GH
1280}
1281
1282static int foreach_device_config(int type, int (*func)(const char *cmdline))
1283{
1284 struct device_config *conf;
1285 int rc;
1286
72cf2d4f 1287 QTAILQ_FOREACH(conf, &device_configs, next) {
bd3c948d
GH
1288 if (conf->type != type)
1289 continue;
d9a5954d 1290 loc_push_restore(&conf->loc);
bd3c948d 1291 rc = func(conf->cmdline);
d9a5954d 1292 loc_pop(&conf->loc);
28de2f88 1293 if (rc) {
bd3c948d 1294 return rc;
28de2f88 1295 }
bd3c948d
GH
1296 }
1297 return 0;
1298}
1299
f650266b
PB
1300static void qemu_disable_default_devices(void)
1301{
1302 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1303
5dacda51 1304 default_driver_check_json();
f650266b
PB
1305 qemu_opts_foreach(qemu_find_opts("device"),
1306 default_driver_check, NULL, NULL);
1307 qemu_opts_foreach(qemu_find_opts("global"),
1308 default_driver_check, NULL, NULL);
1309
1310 if (!vga_model && !default_vga) {
1311 vga_interface_type = VGA_DEVICE;
f9bcb2d6 1312 vga_interface_created = true;
f650266b
PB
1313 }
1314 if (!has_defaults || machine_class->no_serial) {
1315 default_serial = 0;
1316 }
1317 if (!has_defaults || machine_class->no_parallel) {
1318 default_parallel = 0;
1319 }
1320 if (!has_defaults || machine_class->no_floppy) {
1321 default_floppy = 0;
1322 }
1323 if (!has_defaults || machine_class->no_cdrom) {
1324 default_cdrom = 0;
1325 }
1326 if (!has_defaults || machine_class->no_sdcard) {
1327 default_sdcard = 0;
1328 }
1329 if (!has_defaults) {
1330 default_monitor = 0;
1331 default_net = 0;
1332 default_vga = 0;
26a0802d
TH
1333 } else {
1334 if (default_net && machine_class->default_nic &&
1335 !module_object_class_by_name(machine_class->default_nic)) {
1336 warn_report("Default NIC '%s' is not available in this binary",
1337 machine_class->default_nic);
1338 default_net = 0;
1339 }
f650266b
PB
1340 }
1341}
1342
1343static void qemu_create_default_devices(void)
1344{
1345 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
1346
1347 if (is_daemonized()) {
1348 /* According to documentation and historically, -nographic redirects
1349 * serial port, parallel port and monitor to stdio, which does not work
1350 * with -daemonize. We can redirect these to null instead, but since
1351 * -nographic is legacy, let's just error out.
1352 * We disallow -nographic only if all other ports are not redirected
1353 * explicitly, to not break existing legacy setups which uses
1354 * -nographic _and_ redirects all ports explicitly - this is valid
1355 * usage, -nographic is just a no-op in this case.
1356 */
1357 if (nographic
1358 && (default_parallel || default_serial || default_monitor)) {
1359 error_report("-nographic cannot be used with -daemonize");
1360 exit(1);
1361 }
1362 }
1363
1364 if (nographic) {
1365 if (default_parallel)
1366 add_device_config(DEV_PARALLEL, "null");
1367 if (default_serial && default_monitor) {
1368 add_device_config(DEV_SERIAL, "mon:stdio");
1369 } else {
1370 if (default_serial)
1371 add_device_config(DEV_SERIAL, "stdio");
1372 if (default_monitor)
1373 monitor_parse("stdio", "readline", false);
1374 }
1375 } else {
1376 if (default_serial)
1377 add_device_config(DEV_SERIAL, "vc:80Cx24C");
1378 if (default_parallel)
1379 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
1380 if (default_monitor)
1381 monitor_parse("vc:80Cx24C", "readline", false);
1382 }
1383
1384 if (default_net) {
1385 QemuOptsList *net = qemu_find_opts("net");
1386 qemu_opts_parse(net, "nic", true, &error_abort);
1387#ifdef CONFIG_SLIRP
1388 qemu_opts_parse(net, "user", true, &error_abort);
1389#endif
1390 }
1391
1392#if defined(CONFIG_VNC)
1393 if (!QTAILQ_EMPTY(&(qemu_find_opts("vnc")->head))) {
1394 display_remote++;
1395 }
1396#endif
1397 if (dpy.type == DISPLAY_TYPE_DEFAULT && !display_remote) {
1398 if (!qemu_display_find_default(&dpy)) {
1399 dpy.type = DISPLAY_TYPE_NONE;
1400#if defined(CONFIG_VNC)
653c9747 1401 vnc_parse("localhost:0,to=99,id=default");
f650266b
PB
1402#endif
1403 }
1404 }
1405 if (dpy.type == DISPLAY_TYPE_DEFAULT) {
1406 dpy.type = DISPLAY_TYPE_NONE;
1407 }
1408
1409 /* If no default VGA is requested, the default is "none". */
1410 if (default_vga) {
1411 vga_model = get_default_vga_model(machine_class);
1412 }
1413 if (vga_model) {
1414 select_vgahw(machine_class, vga_model);
1415 }
1416}
1417
998bbd74
GH
1418static int serial_parse(const char *devname)
1419{
6af2692e 1420 int index = num_serial_hds;
998bbd74
GH
1421 char label[32];
1422
1423 if (strcmp(devname, "none") == 0)
1424 return 0;
998bbd74 1425 snprintf(label, sizeof(label), "serial%d", index);
6af2692e
PM
1426 serial_hds = g_renew(Chardev *, serial_hds, index + 1);
1427
4ad6f6cb 1428 serial_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
998bbd74 1429 if (!serial_hds[index]) {
f61eddcb
EH
1430 error_report("could not connect serial device"
1431 " to character backend '%s'", devname);
998bbd74
GH
1432 return -1;
1433 }
6af2692e 1434 num_serial_hds++;
998bbd74
GH
1435 return 0;
1436}
1437
a8d78cd0
PM
1438Chardev *serial_hd(int i)
1439{
1440 assert(i >= 0);
6af2692e 1441 if (i < num_serial_hds) {
a8d78cd0
PM
1442 return serial_hds[i];
1443 }
1444 return NULL;
1445}
1446
6a5e8b0e
GH
1447static int parallel_parse(const char *devname)
1448{
1449 static int index = 0;
1450 char label[32];
1451
1452 if (strcmp(devname, "none") == 0)
1453 return 0;
1454 if (index == MAX_PARALLEL_PORTS) {
f61eddcb 1455 error_report("too many parallel ports");
6a5e8b0e
GH
1456 exit(1);
1457 }
1458 snprintf(label, sizeof(label), "parallel%d", index);
4ad6f6cb 1459 parallel_hds[index] = qemu_chr_new_mux_mon(label, devname, NULL);
6a5e8b0e 1460 if (!parallel_hds[index]) {
f61eddcb
EH
1461 error_report("could not connect parallel device"
1462 " to character backend '%s'", devname);
6a5e8b0e
GH
1463 return -1;
1464 }
1465 index++;
1466 return 0;
1467}
1468
c9f398e5 1469static int debugcon_parse(const char *devname)
4d8b3c63 1470{
c9f398e5
PA
1471 QemuOpts *opts;
1472
4ad6f6cb 1473 if (!qemu_chr_new_mux_mon("debugcon", devname, NULL)) {
d7ecf712 1474 error_report("invalid character backend '%s'", devname);
c9f398e5
PA
1475 exit(1);
1476 }
8be7e7e4 1477 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1, NULL);
c9f398e5 1478 if (!opts) {
f61eddcb 1479 error_report("already have a debugcon device");
c9f398e5
PA
1480 exit(1);
1481 }
f43e47db
MA
1482 qemu_opt_set(opts, "driver", "isa-debugcon", &error_abort);
1483 qemu_opt_set(opts, "chardev", "debugcon", &error_abort);
c9f398e5
PA
1484 return 0;
1485}
1486
2709f263
LE
1487static gint machine_class_cmp(gconstpointer a, gconstpointer b)
1488{
1489 const MachineClass *mc1 = a, *mc2 = b;
1490 int res;
1491
1492 if (mc1->family == NULL) {
1493 if (mc2->family == NULL) {
1494 /* Compare standalone machine types against each other; they sort
1495 * in increasing order.
1496 */
1497 return strcmp(object_class_get_name(OBJECT_CLASS(mc1)),
1498 object_class_get_name(OBJECT_CLASS(mc2)));
1499 }
1500
1501 /* Standalone machine types sort after families. */
1502 return 1;
1503 }
1504
1505 if (mc2->family == NULL) {
1506 /* Families sort before standalone machine types. */
1507 return -1;
1508 }
1509
1510 /* Families sort between each other alphabetically increasingly. */
1511 res = strcmp(mc1->family, mc2->family);
1512 if (res != 0) {
1513 return res;
1514 }
1515
1516 /* Within the same family, machine types sort in decreasing order. */
1517 return strcmp(object_class_get_name(OBJECT_CLASS(mc2)),
1518 object_class_get_name(OBJECT_CLASS(mc1)));
1519}
1520
d8fb7d09 1521static void machine_help_func(const QDict *qdict)
9052ea6b 1522{
d8fb7d09
PB
1523 GSList *machines, *el;
1524 const char *type = qdict_get_try_str(qdict, "type");
9052ea6b 1525
d8fb7d09
PB
1526 machines = object_class_get_list(TYPE_MACHINE, false);
1527 if (type) {
1528 ObjectClass *machine_class = OBJECT_CLASS(find_machine(type, machines));
1529 if (machine_class) {
1530 type_print_class_properties(object_class_get_name(machine_class));
1531 return;
9052ea6b 1532 }
9052ea6b 1533 }
261747f1 1534
d8fb7d09
PB
1535 printf("Supported machines are:\n");
1536 machines = g_slist_sort(machines, machine_class_cmp);
1537 for (el = machines; el; el = el->next) {
1538 MachineClass *mc = el->data;
1539 if (mc->alias) {
1540 printf("%-20s %s (alias of %s)\n", mc->alias, mc->desc, mc->name);
1541 }
1542 printf("%-20s %s%s%s\n", mc->name, mc->desc,
1543 mc->is_default ? " (default)" : "",
1544 mc->deprecation_reason ? " (deprecated)" : "");
12cb82fd 1545 }
9052ea6b
JK
1546}
1547
a3c2f128
PB
1548static void
1549machine_merge_property(const char *propname, QDict *prop, Error **errp)
1550{
1551 QDict *opts;
1552
1553 opts = qdict_new();
1554 /* Preserve the caller's reference to prop. */
1555 qobject_ref(prop);
1556 qdict_put(opts, propname, prop);
1557 keyval_merge(machine_opts_dict, opts, errp);
1558 qobject_unref(opts);
1559}
1560
fe68090e
PB
1561static void
1562machine_parse_property_opt(QemuOptsList *opts_list, const char *propname,
f9dfae9c 1563 const char *arg)
fe68090e 1564{
a3c2f128 1565 QDict *prop = NULL;
fe68090e 1566 bool help = false;
fe68090e 1567
f9dfae9c 1568 prop = keyval_parse(arg, opts_list->implied_opt_name, &help, &error_fatal);
fe68090e
PB
1569 if (help) {
1570 qemu_opts_print_help(opts_list, true);
3e61a13a 1571 exit(0);
fe68090e 1572 }
f9dfae9c 1573 machine_merge_property(propname, prop, &error_fatal);
a3c2f128 1574 qobject_unref(prop);
fe68090e
PB
1575}
1576
90a84d13 1577static const char *pid_file;
85c4bf8a
HR
1578struct UnlinkPidfileNotifier {
1579 Notifier notifier;
1580 char *pid_file_realpath;
1581};
1582static struct UnlinkPidfileNotifier qemu_unlink_pidfile_notifier;
90a84d13
MAL
1583
1584static void qemu_unlink_pidfile(Notifier *n, void *data)
1585{
85c4bf8a
HR
1586 struct UnlinkPidfileNotifier *upn;
1587
1588 upn = DO_UPCAST(struct UnlinkPidfileNotifier, notifier, n);
1589 unlink(upn->pid_file_realpath);
90a84d13
MAL
1590}
1591
6530a97b
AL
1592static const QEMUOption *lookup_opt(int argc, char **argv,
1593 const char **poptarg, int *poptind)
1594{
1595 const QEMUOption *popt;
1596 int optind = *poptind;
1597 char *r = argv[optind];
1598 const char *optarg;
1599
0f0bc3f1 1600 loc_set_cmdline(argv, optind, 1);
6530a97b
AL
1601 optind++;
1602 /* Treat --foo the same as -foo. */
1603 if (r[1] == '-')
1604 r++;
1605 popt = qemu_options;
1606 for(;;) {
1607 if (!popt->name) {
0f0bc3f1 1608 error_report("invalid option");
6530a97b
AL
1609 exit(1);
1610 }
1611 if (!strcmp(popt->name, r + 1))
1612 break;
1613 popt++;
1614 }
1615 if (popt->flags & HAS_ARG) {
1616 if (optind >= argc) {
0f0bc3f1 1617 error_report("requires an argument");
6530a97b
AL
1618 exit(1);
1619 }
1620 optarg = argv[optind++];
0f0bc3f1 1621 loc_set_cmdline(argv, optind - 2, 2);
6530a97b
AL
1622 } else {
1623 optarg = NULL;
1624 }
1625
1626 *poptarg = optarg;
1627 *poptind = optind;
1628
1629 return popt;
1630}
1631
d8fb7d09 1632static MachineClass *select_machine(QDict *qdict, Error **errp)
34f405ae 1633{
d8fb7d09 1634 const char *optarg = qdict_get_try_str(qdict, "type");
c516cd1b 1635 GSList *machines = object_class_get_list(TYPE_MACHINE, false);
d8fb7d09
PB
1636 MachineClass *machine_class;
1637 Error *local_err = NULL;
34f405ae 1638
34f405ae 1639 if (optarg) {
d8fb7d09
PB
1640 machine_class = find_machine(optarg, machines);
1641 qdict_del(qdict, "type");
1642 if (!machine_class) {
1643 error_setg(&local_err, "unsupported machine type");
1644 }
1645 } else {
1646 machine_class = find_default_machine(machines);
1647 if (!machine_class) {
1648 error_setg(&local_err, "No machine specified, and there is no default");
1649 }
34f405ae
MA
1650 }
1651
c516cd1b 1652 g_slist_free(machines);
d8fb7d09
PB
1653 if (local_err) {
1654 error_append_hint(&local_err, "Use -machine help to list supported machines\n");
1655 error_propagate(errp, local_err);
1656 }
7580f231 1657 return machine_class;
34f405ae
MA
1658}
1659
e5db4bd8
PB
1660static int object_parse_property_opt(Object *obj,
1661 const char *name, const char *value,
1662 const char *skip, Error **errp)
68d98d3e 1663{
e5db4bd8 1664 if (g_str_equal(name, skip)) {
68d98d3e
AL
1665 return 0;
1666 }
1667
668f62ec 1668 if (!object_property_parse(obj, name, value, errp)) {
68d98d3e
AL
1669 return -1;
1670 }
1671
1672 return 0;
1673}
1674
d8fb7d09
PB
1675/* *Non*recursively replace underscores with dashes in QDict keys. */
1676static void keyval_dashify(QDict *qdict, Error **errp)
e5db4bd8 1677{
d8fb7d09 1678 const QDictEntry *ent, *next;
e5db4bd8
PB
1679 char *p;
1680
d8fb7d09
PB
1681 for (ent = qdict_first(qdict); ent; ent = next) {
1682 g_autofree char *new_key = NULL;
1683
1684 next = qdict_next(qdict, ent);
1685 if (!strchr(ent->key, '_')) {
1686 continue;
1687 }
1688 new_key = g_strdup(ent->key);
1689 for (p = new_key; *p; p++) {
1690 if (*p == '_') {
1691 *p = '-';
1692 }
e5db4bd8 1693 }
d8fb7d09
PB
1694 if (qdict_haskey(qdict, new_key)) {
1695 error_setg(errp, "Conflict between '%s' and '%s'", ent->key, new_key);
1696 return;
1697 }
1698 qobject_ref(ent->value);
1699 qdict_put_obj(qdict, new_key, ent->value);
1700 qdict_del(qdict, ent->key);
e5db4bd8 1701 }
d8fb7d09
PB
1702}
1703
1704static void qemu_apply_legacy_machine_options(QDict *qdict)
1705{
1706 const char *value;
ce9d03fb 1707 QObject *prop;
d8fb7d09
PB
1708
1709 keyval_dashify(qdict, &error_fatal);
e5db4bd8 1710
6f6e1698 1711 /* Legacy options do not correspond to MachineState properties. */
d8fb7d09
PB
1712 value = qdict_get_try_str(qdict, "accel");
1713 if (value) {
1714 accelerators = g_strdup(value);
1715 qdict_del(qdict, "accel");
6f6e1698 1716 }
d8fb7d09
PB
1717
1718 value = qdict_get_try_str(qdict, "igd-passthru");
1719 if (value) {
1720 object_register_sugar_prop(ACCEL_CLASS_NAME("xen"), "igd-passthru", value,
a8dc82ce 1721 false);
d8fb7d09 1722 qdict_del(qdict, "igd-passthru");
46472d82 1723 }
d8fb7d09
PB
1724
1725 value = qdict_get_try_str(qdict, "kvm-shadow-mem");
1726 if (value) {
1727 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kvm-shadow-mem", value,
a8dc82ce 1728 false);
d8fb7d09 1729 qdict_del(qdict, "kvm-shadow-mem");
23b0898e 1730 }
d8fb7d09
PB
1731
1732 value = qdict_get_try_str(qdict, "kernel-irqchip");
1733 if (value) {
1734 object_register_sugar_prop(ACCEL_CLASS_NAME("kvm"), "kernel-irqchip", value,
a8dc82ce 1735 false);
d8fb7d09 1736 object_register_sugar_prop(ACCEL_CLASS_NAME("whpx"), "kernel-irqchip", value,
a8dc82ce 1737 false);
d8fb7d09 1738 qdict_del(qdict, "kernel-irqchip");
faf20793 1739 }
ce9d03fb 1740
26f88d84
PB
1741 value = qdict_get_try_str(qdict, "memory-backend");
1742 if (value) {
1743 if (mem_path) {
1744 error_report("'-mem-path' can't be used together with"
1745 "'-machine memory-backend'");
1746 exit(EXIT_FAILURE);
1747 }
1748
1749 /* Resolved later. */
1750 ram_memdev_id = g_strdup(value);
1751 qdict_del(qdict, "memory-backend");
1752 }
1753
ce9d03fb
PB
1754 prop = qdict_get(qdict, "memory");
1755 if (prop) {
1756 have_custom_ram_size =
1757 qobject_type(prop) == QTYPE_QDICT &&
1758 qdict_haskey(qobject_to(QDict, prop), "size");
1759 }
e5db4bd8 1760}
f08f9271 1761
bc2f4fcb
PB
1762static void object_option_foreach_add(bool (*type_opt_predicate)(const char *))
1763{
1764 ObjectOption *opt, *next;
1765
1766 QTAILQ_FOREACH_SAFE(opt, &object_opts, next, next) {
1767 const char *type = ObjectType_str(opt->opts->qom_type);
1768 if (type_opt_predicate(type)) {
1769 user_creatable_add_qapi(opt->opts, &error_fatal);
1770 qapi_free_ObjectOptions(opt->opts);
1771 QTAILQ_REMOVE(&object_opts, opt, next);
1772 g_free(opt);
1773 }
1774 }
1775}
1776
49e98769
PB
1777static void object_option_add_visitor(Visitor *v)
1778{
1779 ObjectOption *opt = g_new0(ObjectOption, 1);
1780 visit_type_ObjectOptions(v, NULL, &opt->opts, &error_fatal);
1781 QTAILQ_INSERT_TAIL(&object_opts, opt, next);
1782}
1783
bc2f4fcb
PB
1784static void object_option_parse(const char *optarg)
1785{
bc2f4fcb
PB
1786 QemuOpts *opts;
1787 const char *type;
1788 Visitor *v;
1789
009ff893
PB
1790 if (optarg[0] == '{') {
1791 QObject *obj = qobject_from_json(optarg, &error_fatal);
bc2f4fcb 1792
009ff893
PB
1793 v = qobject_input_visitor_new(obj);
1794 qobject_unref(obj);
1795 } else {
1796 opts = qemu_opts_parse_noisily(qemu_find_opts("object"),
1797 optarg, true);
1798 if (!opts) {
1799 exit(1);
1800 }
1801
1802 type = qemu_opt_get(opts, "qom-type");
1803 if (!type) {
1804 error_setg(&error_fatal, QERR_MISSING_PARAMETER, "qom-type");
1805 }
1806 if (user_creatable_print_help(type, opts)) {
1807 exit(0);
1808 }
1809
1810 v = opts_visitor_new(opts);
bc2f4fcb
PB
1811 }
1812
49e98769 1813 object_option_add_visitor(v);
bc2f4fcb 1814 visit_free(v);
bc2f4fcb
PB
1815}
1816
bd77c30d
DH
1817/*
1818 * Very early object creation, before the sandbox options have been activated.
1819 */
1820static bool object_create_pre_sandbox(const char *type)
1821{
1822 /*
1823 * Objects should in general not get initialized "too early" without
1824 * a reason. If you add one, state the reason in a comment!
1825 */
1826
1827 /*
1828 * Reason: -sandbox on,resourcecontrol=deny disallows setting CPU
1829 * affinity of threads.
1830 */
1831 if (g_str_equal(type, "thread-context")) {
1832 return true;
1833 }
1834
1835 return false;
1836}
1837
f08f9271
DB
1838/*
1839 * Initial object creation happens before all other
1840 * QEMU data types are created. The majority of objects
1841 * can be created at this point. The rng-egd object
1842 * cannot be created here, as it depends on the chardev
1843 * already existing.
1844 */
bc2f4fcb 1845static bool object_create_early(const char *type)
f08f9271 1846{
edfb4389
MA
1847 /*
1848 * Objects should not be made "delayed" without a reason. If you
1849 * add one, state the reason in a comment!
1850 */
1851
bd77c30d
DH
1852 /* Reason: already created. */
1853 if (object_create_pre_sandbox(type)) {
1854 return false;
1855 }
1856
6ba7ada3
PB
1857 /* Reason: property "chardev" */
1858 if (g_str_equal(type, "rng-egd") ||
1859 g_str_equal(type, "qtest")) {
f08f9271
DB
1860 return false;
1861 }
7dbb11c8 1862
042cea27 1863#if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
edfb4389 1864 /* Reason: cryptodev-vhost-user property "chardev" */
042cea27
GA
1865 if (g_str_equal(type, "cryptodev-vhost-user")) {
1866 return false;
1867 }
1868#endif
1869
3578389b
CX
1870 /* Reason: vhost-user-blk-server property "node-name" */
1871 if (g_str_equal(type, "vhost-user-blk-server")) {
1872 return false;
1873 }
9d3e12e8 1874 /*
edfb4389 1875 * Reason: filter-* property "netdev" etc.
9d3e12e8
TH
1876 */
1877 if (g_str_equal(type, "filter-buffer") ||
f6d3afb5 1878 g_str_equal(type, "filter-dump") ||
d46f75b2 1879 g_str_equal(type, "filter-mirror") ||
7dce4e6f 1880 g_str_equal(type, "filter-redirector") ||
e6eee8ab 1881 g_str_equal(type, "colo-compare") ||
646c5478
PD
1882 g_str_equal(type, "filter-rewriter") ||
1883 g_str_equal(type, "filter-replay")) {
7dbb11c8
YH
1884 return false;
1885 }
1886
b86f59c7
CF
1887 /*
1888 * Allocation of large amounts of memory may delay
6546d0db
EH
1889 * chardev initialization for too long, and trigger timeouts
1890 * on software that waits for a monitor socket to be created
1891 * (e.g. libvirt).
1892 */
1893 if (g_str_has_prefix(type, "memory-backend-")) {
1894 return false;
1895 }
1896
f08f9271
DB
1897 return true;
1898}
1899
d8fb7d09 1900static void qemu_apply_machine_options(QDict *qdict)
7a84268d 1901{
d8fb7d09 1902 object_set_properties_from_keyval(OBJECT(current_machine), qdict, false, &error_fatal);
7a84268d 1903
5202861b 1904 if (semihosting_enabled(false) && !semihosting_get_argc()) {
7a84268d 1905 /* fall back to the -kernel/-append */
d8fb7d09 1906 semihosting_arg_fallback(current_machine->kernel_filename, current_machine->kernel_cmdline);
7a84268d 1907 }
fe68090e
PB
1908
1909 if (current_machine->smp.cpus > 1) {
0ec8384f 1910 replay_add_blocker("smp");
fe68090e 1911 }
7a84268d
PB
1912}
1913
64418657
PB
1914static void qemu_create_early_backends(void)
1915{
1916 MachineClass *machine_class = MACHINE_GET_CLASS(current_machine);
66c2207f
TH
1917#if defined(CONFIG_SDL)
1918 const bool use_sdl = (dpy.type == DISPLAY_TYPE_SDL);
1919#else
1920 const bool use_sdl = false;
1921#endif
1922#if defined(CONFIG_GTK)
1923 const bool use_gtk = (dpy.type == DISPLAY_TYPE_GTK);
1924#else
1925 const bool use_gtk = false;
1926#endif
64418657 1927
66c2207f 1928 if (dpy.has_window_close && !use_gtk && !use_sdl) {
fe86fe23 1929 error_report("window-close is only valid for GTK and SDL, "
64418657
PB
1930 "ignoring option");
1931 }
1932
1933 qemu_display_early_init(&dpy);
1934 qemu_console_early_init();
1935
1936 if (dpy.has_gl && dpy.gl != DISPLAYGL_MODE_OFF && display_opengl == 0) {
1937#if defined(CONFIG_OPENGL)
1938 error_report("OpenGL is not supported by the display");
1939#else
1940 error_report("OpenGL support is disabled");
1941#endif
1942 exit(1);
1943 }
1944
bc2f4fcb 1945 object_option_foreach_add(object_create_early);
64418657
PB
1946
1947 /* spice needs the timers to be initialized by this point */
3d76c3b6 1948 /* spice must initialize before audio as it changes the default audiodev */
64418657
PB
1949 /* spice must initialize before chardevs (for spicevmc and spiceport) */
1950 qemu_spice.init();
1951
1952 qemu_opts_foreach(qemu_find_opts("chardev"),
1953 chardev_init_func, NULL, &error_fatal);
1954
1955#ifdef CONFIG_VIRTFS
1956 qemu_opts_foreach(qemu_find_opts("fsdev"),
1957 fsdev_init_func, NULL, &error_fatal);
1958#endif
1959
1960 /*
1961 * Note: we need to create audio and block backends before
d8fb7d09 1962 * setting machine properties, so they can be referred to.
64418657
PB
1963 */
1964 configure_blockdev(&bdo_queue, machine_class, snapshot);
0f957c53
MAL
1965 if (!audio_init_audiodevs()) {
1966 exit(1);
1967 }
64418657
PB
1968}
1969
f08f9271
DB
1970
1971/*
1972 * The remainder of object creation happens after the
9abce56d 1973 * creation of chardev, fsdev, net clients and device data types.
f08f9271 1974 */
bc2f4fcb 1975static bool object_create_late(const char *type)
f08f9271 1976{
bd77c30d 1977 return !object_create_early(type) && !object_create_pre_sandbox(type);
f08f9271
DB
1978}
1979
07a91b1a
PB
1980static void qemu_create_late_backends(void)
1981{
1982 if (qtest_chrdev) {
1983 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1984 }
1985
d63ef17b 1986 net_init_clients();
07a91b1a 1987
bc2f4fcb 1988 object_option_foreach_add(object_create_late);
07a91b1a
PB
1989
1990 if (tpm_init() < 0) {
1991 exit(1);
1992 }
1993
1994 qemu_opts_foreach(qemu_find_opts("mon"),
1995 mon_init_func, NULL, &error_fatal);
1996
1997 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1998 exit(1);
1999 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
2000 exit(1);
2001 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2002 exit(1);
2003
2004 /* now chardevs have been created we may have semihosting to connect */
fb08790b 2005 qemu_semihosting_chardev_init();
07a91b1a 2006}
f08f9271 2007
b24986e7
PB
2008static void qemu_resolve_machine_memdev(void)
2009{
26f88d84 2010 if (ram_memdev_id) {
b24986e7
PB
2011 Object *backend;
2012 ram_addr_t backend_size;
2013
26f88d84 2014 backend = object_resolve_path_type(ram_memdev_id,
b24986e7
PB
2015 TYPE_MEMORY_BACKEND, NULL);
2016 if (!backend) {
26f88d84 2017 error_report("Memory backend '%s' not found", ram_memdev_id);
b24986e7
PB
2018 exit(EXIT_FAILURE);
2019 }
fb56b7a0
PB
2020 if (!have_custom_ram_size) {
2021 backend_size = object_property_get_uint(backend, "size", &error_abort);
2022 current_machine->ram_size = backend_size;
b24986e7 2023 }
26f88d84
PB
2024 object_property_set_link(OBJECT(current_machine),
2025 "memory-backend", backend, &error_fatal);
b24986e7 2026 }
b24986e7
PB
2027}
2028
e12f0685 2029static void parse_memory_options(void)
3b9985e9 2030{
e12f0685 2031 QemuOpts *opts = qemu_find_opts_singleton("memory");
ce9d03fb 2032 QDict *dict, *prop;
3b9985e9 2033 const char *mem_str;
e12f0685 2034 Location loc;
bbe2d25c 2035
e12f0685
PB
2036 loc_push_none(&loc);
2037 qemu_opts_loc_restore(opts);
ce9d03fb
PB
2038
2039 prop = qdict_new();
3b9985e9 2040
ce9d03fb 2041 if (qemu_opt_get_size(opts, "size", 0) != 0) {
3b9985e9 2042 /* Fix up legacy suffix-less format */
69c05a23 2043 mem_str = qemu_opt_get(opts, "size");
3b9985e9 2044 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
ce9d03fb
PB
2045 g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
2046 qdict_put_str(prop, "size", mib_str);
2047 } else {
2048 qdict_put_str(prop, "size", mem_str);
3b9985e9
MA
2049 }
2050 }
2051
951f2269 2052 if (qemu_opt_get(opts, "maxmem")) {
ce9d03fb
PB
2053 qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
2054 }
2055 if (qemu_opt_get(opts, "slots")) {
2056 qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
3b9985e9 2057 }
bbe2d25c 2058
ce9d03fb
PB
2059 dict = qdict_new();
2060 qdict_put(dict, "memory", prop);
2061 keyval_merge(machine_opts_dict, dict, &error_fatal);
2062 qobject_unref(dict);
e12f0685 2063 loc_pop(&loc);
f5c9fcb8
PB
2064}
2065
d8fb7d09 2066static void qemu_create_machine(QDict *qdict)
f5c9fcb8 2067{
d8fb7d09 2068 MachineClass *machine_class = select_machine(qdict, &error_fatal);
f5c9fcb8
PB
2069 object_set_machine_compat_props(machine_class->compat_props);
2070
f5c9fcb8 2071 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
f5c9fcb8
PB
2072 object_property_add_child(object_get_root(), "machine",
2073 OBJECT(current_machine));
2074 object_property_add_child(container_get(OBJECT(current_machine),
2075 "/unattached"),
2076 "sysbus", OBJECT(sysbus_get_default()));
2077
2078 if (machine_class->minimum_page_bits) {
2079 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2080 /* This would be a board error: specifying a minimum smaller than
2081 * a target's compile-time fixed setting.
2082 */
2083 g_assert_not_reached();
2084 }
2085 }
2086
2087 cpu_exec_init_all();
2088 page_size_init();
2089
2090 if (machine_class->hw_version) {
2091 qemu_set_hw_version(machine_class->hw_version);
2092 }
2093
f5c9fcb8
PB
2094 /*
2095 * Get the default machine options from the machine if it is not already
2096 * specified either by the configuration file or by the command line.
2097 */
2098 if (machine_class->default_machine_opts) {
d8fb7d09
PB
2099 QDict *default_opts =
2100 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2101 &error_abort);
dadafe67 2102 qemu_apply_legacy_machine_options(default_opts);
d8fb7d09
PB
2103 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2104 false, &error_abort);
2105 qobject_unref(default_opts);
f5c9fcb8 2106 }
3b9985e9
MA
2107}
2108
8d76bfe8
EH
2109static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2110{
2111 GlobalProperty *g;
2112
2113 g = g_malloc0(sizeof(*g));
2114 g->driver = qemu_opt_get(opts, "driver");
2115 g->property = qemu_opt_get(opts, "property");
2116 g->value = qemu_opt_get(opts, "value");
8d76bfe8
EH
2117 qdev_prop_register_global(g);
2118 return 0;
2119}
2120
c0d4aa82
PB
2121/*
2122 * Return whether configuration group @group is stored in QemuOpts, or
2123 * recorded as one or more QDicts by qemu_record_config_group.
2124 */
2125static bool is_qemuopts_group(const char *group)
2126{
d8fb7d09 2127 if (g_str_equal(group, "object") ||
adf7f6b7 2128 g_str_equal(group, "audiodev") ||
e4383ca2 2129 g_str_equal(group, "machine") ||
8c4da4b5 2130 g_str_equal(group, "smp-opts") ||
e12f0685 2131 g_str_equal(group, "boot-opts")) {
49e98769
PB
2132 return false;
2133 }
c0d4aa82
PB
2134 return true;
2135}
2136
2137static void qemu_record_config_group(const char *group, QDict *dict,
2138 bool from_json, Error **errp)
2139{
49e98769
PB
2140 if (g_str_equal(group, "object")) {
2141 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2142 object_option_add_visitor(v);
2143 visit_free(v);
adf7f6b7
PB
2144
2145 } else if (g_str_equal(group, "audiodev")) {
2146 Audiodev *dev = NULL;
2147 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2148 if (visit_type_Audiodev(v, NULL, &dev, errp)) {
2149 audio_define(dev);
2150 }
2151 visit_free(v);
2152
d8fb7d09
PB
2153 } else if (g_str_equal(group, "machine")) {
2154 /*
2155 * Cannot merge string-valued and type-safe dictionaries, so JSON
2156 * is not accepted yet for -M.
2157 */
2158 assert(!from_json);
2159 keyval_merge(machine_opts_dict, dict, errp);
e4383ca2
PB
2160 } else if (g_str_equal(group, "smp-opts")) {
2161 machine_merge_property("smp", dict, &error_fatal);
8c4da4b5
PB
2162 } else if (g_str_equal(group, "boot-opts")) {
2163 machine_merge_property("boot", dict, &error_fatal);
49e98769
PB
2164 } else {
2165 abort();
2166 }
c0d4aa82
PB
2167}
2168
2169/*
2170 * Parse non-QemuOpts config file groups, pass the rest to
2171 * qemu_config_do_parse.
2172 */
2173static void qemu_parse_config_group(const char *group, QDict *qdict,
2174 void *opaque, Error **errp)
2175{
2176 QObject *crumpled;
2177 if (is_qemuopts_group(group)) {
2178 qemu_config_do_parse(group, qdict, opaque, errp);
2179 return;
2180 }
2181
2182 crumpled = qdict_crumple(qdict, errp);
2183 if (!crumpled) {
2184 return;
2185 }
838e3700
PB
2186 switch (qobject_type(crumpled)) {
2187 case QTYPE_QDICT:
2188 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2189 break;
2190 case QTYPE_QLIST:
c0d4aa82 2191 error_setg(errp, "Lists cannot be at top level of a configuration section");
838e3700
PB
2192 break;
2193 default:
2194 g_assert_not_reached();
c0d4aa82 2195 }
838e3700 2196 qobject_unref(crumpled);
c0d4aa82
PB
2197}
2198
f7544edc 2199static void qemu_read_default_config_file(Error **errp)
726401be 2200{
f7544edc 2201 ERRP_GUARD();
726401be 2202 int ret;
580e6ad6 2203 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
726401be 2204
c0d4aa82 2205 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
f7544edc
PB
2206 if (ret < 0) {
2207 if (ret == -ENOENT) {
2208 error_free(*errp);
2209 *errp = NULL;
2210 }
726401be 2211 }
726401be
EH
2212}
2213
c0d4aa82 2214static void qemu_set_option(const char *str, Error **errp)
ed7fa564 2215{
ed7fa564
PB
2216 char group[64], id[64], arg[64];
2217 QemuOptsList *list;
2218 QemuOpts *opts;
2219 int rc, offset;
2220
2221 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2222 if (rc < 3 || str[offset] != '=') {
c0d4aa82
PB
2223 error_setg(errp, "can't parse: \"%s\"", str);
2224 return;
ed7fa564
PB
2225 }
2226
c0d4aa82
PB
2227 if (!is_qemuopts_group(group)) {
2228 error_setg(errp, "-set is not supported with %s", group);
2229 } else {
2230 list = qemu_find_opts_err(group, errp);
2231 if (list) {
2232 opts = qemu_opts_find(list, id);
2233 if (!opts) {
2234 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2235 return;
2236 }
2237 qemu_opt_set(opts, arg, str + offset + 1, errp);
2238 }
ed7fa564 2239 }
ed7fa564
PB
2240}
2241
a0660e0b
PX
2242static void user_register_global_props(void)
2243{
2244 qemu_opts_foreach(qemu_find_opts("global"),
2245 global_init_func, NULL, NULL);
2246}
2247
7f8b6126
PB
2248static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2249{
8191d368 2250 icount_configure(opts, errp);
7f8b6126
PB
2251 return 0;
2252}
2253
12ceaef6
PB
2254static int accelerator_set_property(void *opaque,
2255 const char *name, const char *value,
2256 Error **errp)
2257{
2258 return object_parse_property_opt(opaque, name, value, "accel", errp);
2259}
2260
deda73e8
PB
2261static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2262{
6f6e1698
PB
2263 bool *p_init_failed = opaque;
2264 const char *acc = qemu_opt_get(opts, "accel");
2265 AccelClass *ac = accel_find(acc);
fc5cf826 2266 AccelState *accel;
6f6e1698 2267 int ret;
eaf65114
TH
2268 bool qtest_with_kvm;
2269
78901b50
PB
2270 if (!acc) {
2271 error_setg(errp, QERR_MISSING_PARAMETER, "accel");
2272 goto bad;
2273 }
2274
eaf65114 2275 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
6f6e1698
PB
2276
2277 if (!ac) {
eaf65114
TH
2278 if (!qtest_with_kvm) {
2279 error_report("invalid accelerator %s", acc);
2280 }
78901b50 2281 goto bad;
6f6e1698 2282 }
fc5cf826 2283 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
fe174132 2284 object_apply_compat_props(OBJECT(accel));
12ceaef6
PB
2285 qemu_opt_foreach(opts, accelerator_set_property,
2286 accel,
2287 &error_fatal);
3cfb0456
PM
2288 /*
2289 * If legacy -singlestep option is set, honour it for TCG and
2290 * silently ignore for any other accelerator (which is how this
2291 * option has always behaved).
2292 */
2293 if (opt_one_insn_per_tb) {
2294 /*
2295 * This will always succeed for TCG, and we want to ignore
2296 * the error from trying to set a nonexistent property
2297 * on any other accelerator.
2298 */
2299 object_property_set_bool(OBJECT(accel), "one-insn-per-tb", true, NULL);
2300 }
fc5cf826 2301 ret = accel_init_machine(accel, current_machine);
6f6e1698 2302 if (ret < 0) {
eaf65114
TH
2303 if (!qtest_with_kvm || ret != -ENOENT) {
2304 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2305 }
78901b50 2306 goto bad;
6f6e1698 2307 }
fe174132 2308
6f6e1698 2309 return 1;
78901b50
PB
2310
2311bad:
2312 *p_init_failed = true;
2313 return 0;
deda73e8
PB
2314}
2315
28a09617 2316static void configure_accelerators(const char *progname)
deda73e8 2317{
28a09617 2318 bool init_failed = false;
28a09617
PB
2319
2320 qemu_opts_foreach(qemu_find_opts("icount"),
2321 do_configure_icount, NULL, &error_fatal);
2322
6f6e1698 2323 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
60ee3552
RH
2324 char **accel_list, **tmp;
2325
7245ca74 2326 if (accelerators == NULL) {
6f6e1698 2327 /* Select the default accelerator */
755ee1f3
RH
2328 bool have_tcg = accel_find("tcg");
2329 bool have_kvm = accel_find("kvm");
2330
2331 if (have_tcg && have_kvm) {
2332 if (g_str_has_suffix(progname, "kvm")) {
6f6e1698 2333 /* If the program name ends with "kvm", we prefer KVM */
7245ca74 2334 accelerators = "kvm:tcg";
6f6e1698 2335 } else {
7245ca74 2336 accelerators = "tcg:kvm";
6f6e1698 2337 }
755ee1f3 2338 } else if (have_kvm) {
7245ca74 2339 accelerators = "kvm";
755ee1f3 2340 } else if (have_tcg) {
7245ca74 2341 accelerators = "tcg";
755ee1f3
RH
2342 } else {
2343 error_report("No accelerator selected and"
2344 " no default accelerator available");
2345 exit(1);
28a09617
PB
2346 }
2347 }
7245ca74 2348 accel_list = g_strsplit(accelerators, ":", 0);
28a09617 2349
a024b090 2350 for (tmp = accel_list; *tmp; tmp++) {
6f6e1698
PB
2351 /*
2352 * Filter invalid accelerators here, to prevent obscenities
2353 * such as "-machine accel=tcg,,thread=single".
2354 */
2355 if (accel_find(*tmp)) {
2356 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
8b90f1c5
PB
2357 } else {
2358 init_failed = true;
2359 error_report("invalid accelerator %s", *tmp);
6f6e1698 2360 }
28a09617 2361 }
adb464ff 2362 g_strfreev(accel_list);
6f6e1698 2363 } else {
7245ca74 2364 if (accelerators != NULL) {
6f6e1698
PB
2365 error_report("The -accel and \"-machine accel=\" options are incompatible");
2366 exit(1);
28a09617
PB
2367 }
2368 }
28a09617 2369
6f6e1698
PB
2370 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2371 do_configure_accelerator, &init_failed, &error_fatal)) {
28a09617 2372 if (!init_failed) {
6f6e1698 2373 error_report("no accelerator found");
28a09617
PB
2374 }
2375 exit(1);
2376 }
2377
eaf65114 2378 if (init_failed && !qtest_chrdev) {
55bd445c 2379 error_report("falling back to %s", current_accel_name());
28a09617
PB
2380 }
2381
740b1759 2382 if (icount_enabled() && !tcg_enabled()) {
deda73e8
PB
2383 error_report("-icount is not allowed with hardware virtualization");
2384 exit(1);
2385 }
2386}
2387
d8fb7d09 2388static void qemu_validate_options(const QDict *machine_opts)
58c91595 2389{
d8fb7d09
PB
2390 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2391 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2392 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
58c91595
PB
2393
2394 if (kernel_filename == NULL) {
2395 if (kernel_cmdline != NULL) {
2396 error_report("-append only allowed with -kernel option");
2397 exit(1);
2398 }
2399
2400 if (initrd_filename != NULL) {
2401 error_report("-initrd only allowed with -kernel option");
2402 exit(1);
2403 }
2404 }
2405
7691bdef 2406 if (loadvm && preconfig_requested) {
90285ec8
PB
2407 error_report("'preconfig' and 'loadvm' options are "
2408 "mutually exclusive");
2409 exit(EXIT_FAILURE);
2410 }
8fafaa04
PB
2411 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2412 error_report("'preconfig' supports '-incoming defer' only");
58c91595
PB
2413 exit(EXIT_FAILURE);
2414 }
2415
2416#ifdef CONFIG_CURSES
2417 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2418 error_report("curses display cannot be used with -daemonize");
2419 exit(1);
2420 }
2421#endif
2422}
2423
4d2c17b0
PB
2424static void qemu_process_sugar_options(void)
2425{
2426 if (mem_prealloc) {
e4383ca2
PB
2427 QObject *smp = qdict_get(machine_opts_dict, "smp");
2428 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2429 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2430 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2431 const char *val = qstring_get_str(qobject_to(QString, cpus));
2432 object_register_sugar_prop("memory-backend", "prealloc-threads",
2433 val, false);
2434 }
2435 }
a8dc82ce 2436 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
4d2c17b0 2437 }
4d2c17b0
PB
2438}
2439
2a5ad60b
AJ
2440/* -action processing */
2441
2442/*
2443 * Process all the -action parameters parsed from cmdline.
2444 */
2445static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2446{
2447 Error *local_err = NULL;
2448 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2449 QObject *ret = NULL;
2450 qmp_marshal_set_action(qdict, &ret, &local_err);
2451 qobject_unref(ret);
2452 qobject_unref(qdict);
2453 if (local_err) {
2454 error_propagate(errp, local_err);
2455 return 1;
2456 }
2457 return 0;
2458}
2459
0546c060
PB
2460static void qemu_process_early_options(void)
2461{
bd77c30d
DH
2462 qemu_opts_foreach(qemu_find_opts("name"),
2463 parse_name, NULL, &error_fatal);
2464
2465 object_option_foreach_add(object_create_pre_sandbox);
2466
0546c060
PB
2467#ifdef CONFIG_SECCOMP
2468 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2469 if (olist) {
2470 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2471 }
2472#endif
2473
2a5ad60b
AJ
2474 if (qemu_opts_foreach(qemu_find_opts("action"),
2475 process_runstate_actions, NULL, &error_fatal)) {
2476 exit(1);
2477 }
2478
0546c060
PB
2479#ifndef _WIN32
2480 qemu_opts_foreach(qemu_find_opts("add-fd"),
2481 parse_add_fd, NULL, &error_fatal);
2482
2483 qemu_opts_foreach(qemu_find_opts("add-fd"),
2484 cleanup_add_fd, NULL, &error_fatal);
2485#endif
2486
0546c060 2487 /* Open the logfile at this point and set the log mask if necessary. */
b2528af1
RH
2488 {
2489 int mask = 0;
2490 if (log_mask) {
2491 mask = qemu_str_to_log_mask(log_mask);
2492 if (!mask) {
2493 qemu_print_log_usage(stdout);
2494 exit(1);
2495 }
0546c060 2496 }
b2528af1 2497 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
0546c060
PB
2498 }
2499
2c65db5e 2500 qemu_add_default_firmwarepath();
0546c060
PB
2501}
2502
2503static void qemu_process_help_options(void)
2504{
0546c060
PB
2505 /*
2506 * Check for -cpu help and -device help before we call select_machine(),
2507 * which will return an error if the architecture has no default machine
2508 * type and the user did not specify one, so that the user doesn't need
2509 * to say '-cpu help -machine something'.
2510 */
2511 if (cpu_option && is_help_option(cpu_option)) {
c138c3b8 2512 list_cpus();
0546c060
PB
2513 exit(0);
2514 }
2515
2516 if (qemu_opts_foreach(qemu_find_opts("device"),
2517 device_help_func, NULL, NULL)) {
2518 exit(0);
2519 }
2520
2521 /* -L help lists the data directories and exits. */
2522 if (list_data_dirs) {
2c65db5e 2523 qemu_list_data_dirs();
0546c060
PB
2524 exit(0);
2525 }
2526}
2527
2528static void qemu_maybe_daemonize(const char *pid_file)
2529{
38f71349 2530 Error *err = NULL;
0546c060
PB
2531
2532 os_daemonize();
2533 rcu_disable_atfork();
2534
eed29d49 2535 if (pid_file) {
85c4bf8a
HR
2536 char *pid_file_realpath = NULL;
2537
eed29d49
HR
2538 if (!qemu_write_pidfile(pid_file, &err)) {
2539 error_reportf_err(err, "cannot create PID file: ");
2540 exit(1);
2541 }
0546c060 2542
85c4bf8a
HR
2543 pid_file_realpath = g_malloc0(PATH_MAX);
2544 if (!realpath(pid_file, pid_file_realpath)) {
dee2a4d4
FE
2545 if (errno != ENOENT) {
2546 warn_report("not removing PID file on exit: cannot resolve PID "
2547 "file path: %s: %s", pid_file, strerror(errno));
2548 }
2549 return;
85c4bf8a
HR
2550 }
2551
2552 qemu_unlink_pidfile_notifier = (struct UnlinkPidfileNotifier) {
2553 .notifier = {
2554 .notify = qemu_unlink_pidfile,
2555 },
2556 .pid_file_realpath = pid_file_realpath,
2557 };
2558 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier.notifier);
eed29d49 2559 }
0546c060
PB
2560}
2561
7691bdef
PB
2562static void qemu_init_displays(void)
2563{
2564 DisplayState *ds;
2565
2566 /* init local displays */
2567 ds = init_displaystate();
2568 qemu_display_init(ds, &dpy);
2569
2570 /* must be after terminal init, SDL library changes signal handlers */
2571 os_setup_signal_handling();
2572
2573 /* init remote displays */
2574#ifdef CONFIG_VNC
2575 qemu_opts_foreach(qemu_find_opts("vnc"),
2576 vnc_init_func, NULL, &error_fatal);
2577#endif
2578
2579 if (using_spice) {
2580 qemu_spice.display_init();
2581 }
2582}
2583
d8e4de41
PB
2584static void qemu_init_board(void)
2585{
8a745974 2586 /* process plugin before CPUs are created, but once -smp has been parsed */
0572f558 2587 qemu_plugin_load_list(&plugin_list, &error_fatal);
8a745974 2588
2f181fbd 2589 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
26f88d84 2590 machine_run_board_init(current_machine, mem_path, &error_fatal);
d8e4de41 2591
fe9f70a1 2592 drive_check_orphaned();
d8e4de41
PB
2593
2594 realtime_init();
d8e4de41
PB
2595}
2596
2597static void qemu_create_cli_devices(void)
2598{
5dacda51
KW
2599 DeviceOption *opt;
2600
d8e4de41
PB
2601 soundhw_init();
2602
2603 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2604 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2605
2606 /* init USB devices */
2607 if (machine_usb(current_machine)) {
2608 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2609 exit(1);
2610 }
2611
2612 /* init generic devices */
2613 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2614 qemu_opts_foreach(qemu_find_opts("device"),
2615 device_init_func, NULL, &error_fatal);
5dacda51 2616 QTAILQ_FOREACH(opt, &device_opts, next) {
64b4529a 2617 DeviceState *dev;
5dacda51
KW
2618 loc_push_restore(&opt->loc);
2619 /*
2620 * TODO Eventually we should call qmp_device_add() here to make sure it
2621 * behaves the same, but QMP still has to accept incorrectly typed
2622 * options until libvirt is fixed and we want to be strict on the CLI
2623 * from the start, so call qdev_device_add_from_qdict() directly for
2624 * now.
2625 */
64b4529a
DB
2626 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2627 object_unref(OBJECT(dev));
5dacda51
KW
2628 loc_pop(&opt->loc);
2629 }
d8e4de41
PB
2630 rom_reset_order_override();
2631}
2632
2633static void qemu_machine_creation_done(void)
2634{
abc27d42
DG
2635 MachineState *machine = MACHINE(qdev_get_machine());
2636
d8e4de41
PB
2637 /* Did we create any drives that we failed to create a device for? */
2638 drive_check_orphaned();
2639
2640 /* Don't warn about the default network setup that you get if
2641 * no command line -net or -netdev options are specified. There
2642 * are two cases that we would otherwise complain about:
2643 * (1) board doesn't support a NIC but the implicit "-net nic"
2644 * requested one
2645 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2646 * sets up a nic that isn't connected to anything.
2647 */
2648 if (!default_net && (!qtest_enabled() || has_defaults)) {
2649 net_check_clients();
2650 }
2651
cca686b4
PB
2652 qdev_prop_check_globals();
2653
d8e4de41
PB
2654 qdev_machine_creation_done();
2655
abc27d42
DG
2656 if (machine->cgs) {
2657 /*
2658 * Verify that Confidential Guest Support has actually been initialized
2659 */
2660 assert(machine->cgs->ready);
2661 }
2662
f66dc873 2663 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
d8e4de41
PB
2664 exit(1);
2665 }
f9bcb2d6
GA
2666 if (!vga_interface_created && !default_vga &&
2667 vga_interface_type != VGA_NONE) {
2668 warn_report("A -vga option was passed but this machine "
2669 "type does not use that option; "
2670 "No VGA device has been created");
2671 }
d8e4de41
PB
2672}
2673
b4e1a342
PB
2674void qmp_x_exit_preconfig(Error **errp)
2675{
2f181fbd 2676 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
b4e1a342
PB
2677 error_setg(errp, "The command is permitted only before machine initialization");
2678 return;
2679 }
2680
2681 qemu_init_board();
2682 qemu_create_cli_devices();
2683 qemu_machine_creation_done();
2684
2685 if (loadvm) {
f9734d5d 2686 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
b4e1a342
PB
2687 }
2688 if (replay_mode != REPLAY_MODE_NONE) {
2689 replay_vmstate_init();
2690 }
2691
2692 if (incoming) {
2693 Error *local_err = NULL;
2694 if (strcmp(incoming, "defer") != 0) {
2695 qmp_migrate_incoming(incoming, &local_err);
2696 if (local_err) {
2697 error_reportf_err(local_err, "-incoming %s: ", incoming);
2698 exit(1);
2699 }
2700 }
2701 } else if (autostart) {
2702 qmp_cont(NULL);
2703 }
2704}
2705
bab6a301 2706void qemu_init(int argc, char **argv)
0824d6fc 2707{
7a84268d 2708 QemuOpts *opts;
d1cdd92e 2709 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
03b0ba70 2710 QemuOptsList *olist;
cd6f1169 2711 int optind;
6530a97b 2712 const char *optarg;
0056ae24 2713 MachineClass *machine_class;
f29a5614 2714 bool userconfig = true;
abfd9ce3 2715 FILE *vmstate_dump_file = NULL;
0b5538c3 2716
4d454574 2717 qemu_add_opts(&qemu_drive_opts);
968854c8
AK
2718 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2719 qemu_add_drive_opts(&qemu_common_drive_opts);
2720 qemu_add_drive_opts(&qemu_drive_opts);
c5f3014b 2721 qemu_add_drive_opts(&bdrv_runtime_opts);
4d454574
PB
2722 qemu_add_opts(&qemu_chardev_opts);
2723 qemu_add_opts(&qemu_device_opts);
2724 qemu_add_opts(&qemu_netdev_opts);
78cd6f7b 2725 qemu_add_opts(&qemu_nic_opts);
4d454574
PB
2726 qemu_add_opts(&qemu_net_opts);
2727 qemu_add_opts(&qemu_rtc_opts);
2728 qemu_add_opts(&qemu_global_opts);
2729 qemu_add_opts(&qemu_mon_opts);
2730 qemu_add_opts(&qemu_trace_opts);
42229a75 2731 qemu_plugin_add_opts();
4d454574 2732 qemu_add_opts(&qemu_option_rom_opts);
8d4e9146 2733 qemu_add_opts(&qemu_accel_opts);
6e1d3c1c 2734 qemu_add_opts(&qemu_mem_opts);
12b7f57e 2735 qemu_add_opts(&qemu_smp_opts);
4d454574 2736 qemu_add_opts(&qemu_boot_opts);
4d454574
PB
2737 qemu_add_opts(&qemu_add_fd_opts);
2738 qemu_add_opts(&qemu_object_opts);
d1a0cf73 2739 qemu_add_opts(&qemu_tpmdev_opts);
1fdd4748 2740 qemu_add_opts(&qemu_overcommit_opts);
5e2ac519 2741 qemu_add_opts(&qemu_msg_opts);
5d12f961 2742 qemu_add_opts(&qemu_name_opts);
0042109a 2743 qemu_add_opts(&qemu_numa_opts);
1ad9580b 2744 qemu_add_opts(&qemu_icount_opts);
a38bb079 2745 qemu_add_opts(&qemu_semihosting_config_opts);
81b2b810 2746 qemu_add_opts(&qemu_fw_cfg_opts);
2a5ad60b 2747 qemu_add_opts(&qemu_action_opts);
8a768db1 2748 qemu_add_run_with_opts();
34294e2f 2749 module_call_init(MODULE_INIT_OPTS);
4d454574 2750
efd7ab22
PB
2751 error_init(argv[0]);
2752 qemu_init_exec_dir(argv[0]);
142c6b1a 2753
c80cafa0 2754 qemu_init_arch_modules();
5ebbfecc 2755
efd7ab22 2756 qemu_init_subsystems();
41bd639b 2757
292444cb
AL
2758 /* first pass of option parsing */
2759 optind = 1;
2760 while (optind < argc) {
2761 if (argv[optind][0] != '-') {
2762 /* disk image */
28e68d68 2763 optind++;
292444cb
AL
2764 } else {
2765 const QEMUOption *popt;
2766
2767 popt = lookup_opt(argc, argv, &optarg, &optind);
2768 switch (popt->index) {
f29a5614
EH
2769 case QEMU_OPTION_nouserconfig:
2770 userconfig = false;
2771 break;
292444cb
AL
2772 }
2773 }
2774 }
2775
d8fb7d09 2776 machine_opts_dict = qdict_new();
1ea06c39 2777 if (userconfig) {
f7544edc 2778 qemu_read_default_config_file(&error_fatal);
292444cb
AL
2779 }
2780
2781 /* second pass of option parsing */
cd6f1169 2782 optind = 1;
0824d6fc 2783 for(;;) {
cd6f1169 2784 if (optind >= argc)
0824d6fc 2785 break;
6530a97b 2786 if (argv[optind][0] != '-') {
17f30eae 2787 loc_set_cmdline(argv, optind, 1);
d1cdd92e 2788 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2789 } else {
2790 const QEMUOption *popt;
2791
6530a97b 2792 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a 2793 if (!(popt->arch_mask & arch_type)) {
da002526 2794 error_report("Option not supported for this target");
ad96090a
BS
2795 exit(1);
2796 }
cd6f1169 2797 switch(popt->index) {
94fc95cd
JM
2798 case QEMU_OPTION_cpu:
2799 /* hw initialization will check this */
c1c8cfe5 2800 cpu_option = optarg;
94fc95cd 2801 break;
cd6f1169 2802 case QEMU_OPTION_hda:
cd6f1169 2803 case QEMU_OPTION_hdb:
cc1daa40
FB
2804 case QEMU_OPTION_hdc:
2805 case QEMU_OPTION_hdd:
2292ddae
MA
2806 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2807 HD_OPTS);
fc01f7e7 2808 break;
42e5f393
MA
2809 case QEMU_OPTION_blockdev:
2810 {
2811 Visitor *v;
651af51c 2812 BlockdevOptionsQueueEntry *bdo;
42e5f393 2813
92917cd2
MA
2814 v = qobject_input_visitor_new_str(optarg, "driver",
2815 &error_fatal);
42e5f393 2816
651af51c 2817 bdo = g_new(BlockdevOptionsQueueEntry, 1);
42e5f393
MA
2818 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2819 &error_fatal);
2820 visit_free(v);
2821 loc_save(&bdo->loc);
2822 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2823 break;
2824 }
e4bcb14c 2825 case QEMU_OPTION_drive:
cc67f28e
EGE
2826 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2827 optarg, false);
2828 if (opts == NULL) {
e2982c3a
MT
2829 exit(1);
2830 }
99efa84d 2831 break;
d058fe03 2832 case QEMU_OPTION_set:
c0d4aa82 2833 qemu_set_option(optarg, &error_fatal);
99efa84d 2834 break;
d0fef6fb
GH
2835 case QEMU_OPTION_global:
2836 if (qemu_global_option(optarg) != 0)
2837 exit(1);
99efa84d 2838 break;
3e3d5815 2839 case QEMU_OPTION_mtdblock:
2292ddae 2840 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2841 break;
a1bb27b1 2842 case QEMU_OPTION_sd:
80f4d9fc 2843 drive_add(IF_SD, -1, optarg, SD_OPTS);
a1bb27b1 2844 break;
86f55663 2845 case QEMU_OPTION_pflash:
2292ddae 2846 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2847 break;
cd6f1169 2848 case QEMU_OPTION_snapshot:
0ec8384f
MA
2849 snapshot = 1;
2850 replay_add_blocker("-snapshot");
33e3963e 2851 break;
268a362c 2852 case QEMU_OPTION_numa:
70b94331
MA
2853 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2854 optarg, true);
0042109a
WG
2855 if (!opts) {
2856 exit(1);
2857 }
268a362c 2858 break;
1472a95b 2859 case QEMU_OPTION_display:
e3af9f9a 2860 parse_display(optarg);
1472a95b 2861 break;
cd6f1169 2862 case QEMU_OPTION_nographic:
d8fb7d09 2863 qdict_put_str(machine_opts_dict, "graphics", "off");
cfc58cf3 2864 nographic = true;
0c8d7065 2865 dpy.type = DISPLAY_TYPE_NONE;
a20dd508 2866 break;
a171fe39 2867 case QEMU_OPTION_portrait:
9312805d
VK
2868 graphic_rotate = 90;
2869 break;
2870 case QEMU_OPTION_rotate:
2871 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2872 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2873 graphic_rotate != 180 && graphic_rotate != 270) {
f61eddcb 2874 error_report("only 90, 180, 270 deg rotation is available");
9312805d
VK
2875 exit(1);
2876 }
a171fe39 2877 break;
cd6f1169 2878 case QEMU_OPTION_kernel:
d8fb7d09 2879 qdict_put_str(machine_opts_dict, "kernel", optarg);
a0abe474
PM
2880 break;
2881 case QEMU_OPTION_initrd:
d8fb7d09 2882 qdict_put_str(machine_opts_dict, "initrd", optarg);
a20dd508 2883 break;
cd6f1169 2884 case QEMU_OPTION_append:
d8fb7d09 2885 qdict_put_str(machine_opts_dict, "append", optarg);
313aa567 2886 break;
412beee6 2887 case QEMU_OPTION_dtb:
d8fb7d09 2888 qdict_put_str(machine_opts_dict, "dtb", optarg);
412beee6 2889 break;
cd6f1169 2890 case QEMU_OPTION_cdrom:
2292ddae 2891 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2892 break;
cd6f1169 2893 case QEMU_OPTION_boot:
8c4da4b5 2894 machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
36b486bb 2895 break;
cd6f1169 2896 case QEMU_OPTION_fda:
cd6f1169 2897 case QEMU_OPTION_fdb:
2292ddae
MA
2898 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2899 optarg, FD_OPTS);
c45886db 2900 break;
52ca8d6a
FB
2901 case QEMU_OPTION_no_fd_bootchk:
2902 fd_bootchk = 0;
2903 break;
a1ea458f 2904 case QEMU_OPTION_netdev:
d30300f7 2905 default_net = 0;
f3eedcdd
LV
2906 if (netdev_is_modern(optarg)) {
2907 netdev_parse_modern(optarg);
2908 } else {
2909 net_client_parse(qemu_find_opts("netdev"), optarg);
2910 }
a1ea458f 2911 break;
78cd6f7b
TH
2912 case QEMU_OPTION_nic:
2913 default_net = 0;
21fccb2c 2914 net_client_parse(qemu_find_opts("nic"), optarg);
78cd6f7b 2915 break;
7c9d8e07 2916 case QEMU_OPTION_net:
d30300f7 2917 default_net = 0;
21fccb2c 2918 net_client_parse(qemu_find_opts("net"), optarg);
702c651c 2919 break;
f9dadc98
RS
2920#ifdef CONFIG_LIBISCSI
2921 case QEMU_OPTION_iscsi:
70b94331
MA
2922 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2923 optarg, false);
f9dadc98
RS
2924 if (!opts) {
2925 exit(1);
2926 }
2927 break;
c7f74643 2928#endif
71830221
KZ
2929 case QEMU_OPTION_audiodev:
2930 audio_parse_option(optarg);
2931 break;
039a6837 2932 case QEMU_OPTION_audio: {
5e03b6da 2933 bool help;
039a6837
PB
2934 char *model;
2935 Audiodev *dev = NULL;
2936 Visitor *v;
5e03b6da
CF
2937 QDict *dict = keyval_parse(optarg, "driver", &help, &error_fatal);
2938 if (help || (qdict_haskey(dict, "driver") &&
2939 is_help_option(qdict_get_str(dict, "driver")))) {
2940 audio_help();
2941 exit(EXIT_SUCCESS);
2942 }
039a6837
PB
2943 if (!qdict_haskey(dict, "id")) {
2944 qdict_put_str(dict, "id", "audiodev0");
2945 }
2946 if (!qdict_haskey(dict, "model")) {
2947 error_setg(&error_fatal, "Parameter 'model' is missing");
2948 }
2949 model = g_strdup(qdict_get_str(dict, "model"));
2950 qdict_del(dict, "model");
2951 if (is_help_option(model)) {
67aaa96a
PB
2952 show_valid_soundhw();
2953 exit(0);
2954 }
039a6837
PB
2955 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2956 qobject_unref(dict);
2957 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2958 visit_free(v);
2959 audio_define(dev);
2960 select_soundhw(model, dev->id);
2961 g_free(model);
1d14ffa9 2962 break;
039a6837 2963 }
cd6f1169 2964 case QEMU_OPTION_h:
15f82208 2965 help(0);
cd6f1169 2966 break;
9bd7e6d9
PB
2967 case QEMU_OPTION_version:
2968 version();
2969 exit(0);
2970 break;
3b9985e9 2971 case QEMU_OPTION_m:
e12f0685
PB
2972 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
2973 if (opts == NULL) {
2974 exit(1);
2975 }
cd6f1169 2976 break;
d1a0cf73
SB
2977#ifdef CONFIG_TPM
2978 case QEMU_OPTION_tpmdev:
2979 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2980 exit(1);
2981 }
2982 break;
2983#endif
c902760f
MT
2984 case QEMU_OPTION_mempath:
2985 mem_path = optarg;
2986 break;
c902760f
MT
2987 case QEMU_OPTION_mem_prealloc:
2988 mem_prealloc = 1;
2989 break;
cd6f1169 2990 case QEMU_OPTION_d:
c235d738
MF
2991 log_mask = optarg;
2992 break;
2993 case QEMU_OPTION_D:
2994 log_file = optarg;
cd6f1169 2995 break;
3514552e 2996 case QEMU_OPTION_DFILTER:
bd6fee9f 2997 qemu_set_dfilter_ranges(optarg, &error_fatal);
3514552e 2998 break;
5584e2db
IL
2999#if defined(CONFIG_TCG) && defined(CONFIG_LINUX)
3000 case QEMU_OPTION_perfmap:
3001 perf_enable_perfmap();
3002 break;
3003 case QEMU_OPTION_jitdump:
3004 perf_enable_jitdump();
3005 break;
3006#endif
9c09a251
RH
3007 case QEMU_OPTION_seed:
3008 qemu_guest_random_seed_main(optarg, &error_fatal);
3009 break;
cd6f1169 3010 case QEMU_OPTION_s:
ef0c4a0d 3011 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 3012 break;
59030a8c 3013 case QEMU_OPTION_gdb:
ef0c4a0d 3014 add_device_config(DEV_GDB, optarg);
cd6f1169 3015 break;
cd6f1169 3016 case QEMU_OPTION_L:
37146e7e
RJ
3017 if (is_help_option(optarg)) {
3018 list_data_dirs = true;
2a1cce90 3019 } else {
ea1edcd7 3020 qemu_add_data_dir(g_strdup(optarg));
4524051c 3021 }
cd6f1169 3022 break;
1192dad8 3023 case QEMU_OPTION_bios:
d8fb7d09 3024 qdict_put_str(machine_opts_dict, "firmware", optarg);
1192dad8 3025 break;
1b530a6d 3026 case QEMU_OPTION_singlestep:
3cfb0456 3027 opt_one_insn_per_tb = true;
1b530a6d 3028 break;
cd6f1169 3029 case QEMU_OPTION_S:
3c07f8e8 3030 autostart = 0;
cd6f1169 3031 break;
99efa84d
MA
3032 case QEMU_OPTION_k:
3033 keyboard_layout = optarg;
3034 break;
3893c124 3035 case QEMU_OPTION_vga:
a369da5f 3036 vga_model = optarg;
7f1b17f2 3037 default_vga = 0;
1bfe856e 3038 break;
e9b137c2
FB
3039 case QEMU_OPTION_g:
3040 {
3041 const char *p;
3042 int w, h, depth;
3043 p = optarg;
3044 w = strtol(p, (char **)&p, 10);
3045 if (w <= 0) {
3046 graphic_error:
f61eddcb 3047 error_report("invalid resolution or depth");
e9b137c2
FB
3048 exit(1);
3049 }
3050 if (*p != 'x')
3051 goto graphic_error;
3052 p++;
3053 h = strtol(p, (char **)&p, 10);
3054 if (h <= 0)
3055 goto graphic_error;
3056 if (*p == 'x') {
3057 p++;
3058 depth = strtol(p, (char **)&p, 10);
8ac919a0
LV
3059 if (depth != 1 && depth != 2 && depth != 4 &&
3060 depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
3061 depth != 24 && depth != 32)
3062 goto graphic_error;
3063 } else if (*p == '\0') {
3064 depth = graphic_depth;
3065 } else {
3066 goto graphic_error;
3067 }
3b46e624 3068
e9b137c2
FB
3069 graphic_width = w;
3070 graphic_height = h;
3071 graphic_depth = depth;
3072 }
3073 break;
20d8a3ed
TS
3074 case QEMU_OPTION_echr:
3075 {
3076 char *r;
3077 term_escape_char = strtol(optarg, &r, 0);
3078 if (r == optarg)
3079 printf("Bad argument to echr\n");
3080 break;
3081 }
82c643ff 3082 case QEMU_OPTION_monitor:
6ca5582d 3083 default_monitor = 0;
70e098af 3084 if (strncmp(optarg, "none", 4)) {
4821cd4c 3085 monitor_parse(optarg, "readline", false);
70e098af 3086 }
6ca5582d
GH
3087 break;
3088 case QEMU_OPTION_qmp:
4821cd4c
HR
3089 monitor_parse(optarg, "control", false);
3090 default_monitor = 0;
3091 break;
3092 case QEMU_OPTION_qmp_pretty:
3093 monitor_parse(optarg, "control", true);
2d114dc1 3094 default_monitor = 0;
82c643ff 3095 break;
22a0e04b 3096 case QEMU_OPTION_mon:
70b94331
MA
3097 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3098 true);
22a0e04b 3099 if (!opts) {
22a0e04b
GH
3100 exit(1);
3101 }
2d114dc1 3102 default_monitor = 0;
22a0e04b 3103 break;
191bc01b 3104 case QEMU_OPTION_chardev:
70b94331
MA
3105 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3106 optarg, true);
191bc01b 3107 if (!opts) {
191bc01b
GH
3108 exit(1);
3109 }
191bc01b 3110 break;
74db920c 3111 case QEMU_OPTION_fsdev:
03b0ba70
GH
3112 olist = qemu_find_opts("fsdev");
3113 if (!olist) {
5dfdae81 3114 error_report("fsdev support is disabled");
03b0ba70
GH
3115 exit(1);
3116 }
70b94331 3117 opts = qemu_opts_parse_noisily(olist, optarg, true);
74db920c 3118 if (!opts) {
74db920c
GS
3119 exit(1);
3120 }
3121 break;
3d54abc7 3122 case QEMU_OPTION_virtfs: {
e14ea479
SH
3123 QemuOpts *fsdev;
3124 QemuOpts *device;
1a6ed33c
AM
3125 const char *writeout, *sock_fd, *socket, *path, *security_model,
3126 *multidevs;
3d54abc7 3127
03b0ba70
GH
3128 olist = qemu_find_opts("virtfs");
3129 if (!olist) {
5dfdae81 3130 error_report("virtfs support is disabled");
03b0ba70
GH
3131 exit(1);
3132 }
70b94331 3133 opts = qemu_opts_parse_noisily(olist, optarg, true);
3d54abc7 3134 if (!opts) {
3d54abc7
GS
3135 exit(1);
3136 }
3137
fbcbf101 3138 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a 3139 qemu_opt_get(opts, "mount_tag") == NULL) {
8afb9000 3140 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
9ce56db6
VJ
3141 exit(1);
3142 }
e14ea479 3143 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3baa0a6a 3144 qemu_opts_id(opts) ?:
8be7e7e4
LC
3145 qemu_opt_get(opts, "mount_tag"),
3146 1, NULL);
e14ea479 3147 if (!fsdev) {
3baa0a6a 3148 error_report("duplicate or invalid fsdev id: %s",
f61eddcb 3149 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
3150 exit(1);
3151 }
d3ab98e6
AK
3152
3153 writeout = qemu_opt_get(opts, "writeout");
3154 if (writeout) {
3155#ifdef CONFIG_SYNC_FILE_RANGE
f43e47db 3156 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
d3ab98e6 3157#else
f61eddcb
EH
3158 error_report("writeout=immediate not supported "
3159 "on this platform");
d3ab98e6
AK
3160 exit(1);
3161#endif
3162 }
f43e47db
MA
3163 qemu_opt_set(fsdev, "fsdriver",
3164 qemu_opt_get(opts, "fsdriver"), &error_abort);
32b69436
GK
3165 path = qemu_opt_get(opts, "path");
3166 if (path) {
3167 qemu_opt_set(fsdev, "path", path, &error_abort);
3168 }
3169 security_model = qemu_opt_get(opts, "security_model");
3170 if (security_model) {
3171 qemu_opt_set(fsdev, "security_model", security_model,
3172 &error_abort);
3173 }
84a87cc4
MK
3174 socket = qemu_opt_get(opts, "socket");
3175 if (socket) {
f43e47db 3176 qemu_opt_set(fsdev, "socket", socket, &error_abort);
84a87cc4 3177 }
4c793dda
MK
3178 sock_fd = qemu_opt_get(opts, "sock_fd");
3179 if (sock_fd) {
f43e47db 3180 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
4c793dda 3181 }
e14ea479 3182
2c74c2cb 3183 qemu_opt_set_bool(fsdev, "readonly",
cccb7967
MA
3184 qemu_opt_get_bool(opts, "readonly", 0),
3185 &error_abort);
1a6ed33c
AM
3186 multidevs = qemu_opt_get(opts, "multidevs");
3187 if (multidevs) {
3188 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3189 }
87ea75d5
PC
3190 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3191 &error_abort);
f43e47db 3192 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
e14ea479 3193 qemu_opt_set(device, "fsdev",
3baa0a6a 3194 qemu_opts_id(fsdev), &error_abort);
e14ea479 3195 qemu_opt_set(device, "mount_tag",
f43e47db 3196 qemu_opt_get(opts, "mount_tag"), &error_abort);
3d54abc7
GS
3197 break;
3198 }
82c643ff 3199 case QEMU_OPTION_serial:
998bbd74
GH
3200 add_device_config(DEV_SERIAL, optarg);
3201 default_serial = 0;
18141ed6
JK
3202 if (strncmp(optarg, "mon:", 4) == 0) {
3203 default_monitor = 0;
3204 }
82c643ff 3205 break;
2a5ad60b
AJ
3206 case QEMU_OPTION_action:
3207 olist = qemu_find_opts("action");
3208 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3209 exit(1);
3210 }
3211 break;
22afb46e 3212 case QEMU_OPTION_watchdog_action: {
22afb46e
PB
3213 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3214 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
9dd986cc 3215 break;
22afb46e 3216 }
6508fe59 3217 case QEMU_OPTION_parallel:
6a5e8b0e
GH
3218 add_device_config(DEV_PARALLEL, optarg);
3219 default_parallel = 0;
18141ed6
JK
3220 if (strncmp(optarg, "mon:", 4) == 0) {
3221 default_monitor = 0;
3222 }
6508fe59 3223 break;
c9f398e5
PA
3224 case QEMU_OPTION_debugcon:
3225 add_device_config(DEV_DEBUGCON, optarg);
3226 break;
99efa84d
MA
3227 case QEMU_OPTION_loadvm:
3228 loadvm = optarg;
3229 break;
d63d307f 3230 case QEMU_OPTION_full_screen:
0c8d7065
GH
3231 dpy.has_full_screen = true;
3232 dpy.full_screen = true;
d63d307f 3233 break;
f7cce898 3234 case QEMU_OPTION_pidfile:
93815bc2 3235 pid_file = optarg;
f7cce898 3236 break;
a09db21f
FB
3237 case QEMU_OPTION_win2k_hack:
3238 win2k_install_hack = 1;
3239 break;
8a92ea2f 3240 case QEMU_OPTION_acpitable:
70b94331
MA
3241 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3242 optarg, true);
f46e720a
LE
3243 if (!opts) {
3244 exit(1);
3245 }
9f57061c 3246 acpi_table_add(opts, &error_fatal);
8a92ea2f 3247 break;
b6f6e3d3 3248 case QEMU_OPTION_smbios:
70b94331
MA
3249 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3250 optarg, false);
f46e720a
LE
3251 if (!opts) {
3252 exit(1);
3253 }
1007a37e 3254 smbios_entry_add(opts, &error_fatal);
b6f6e3d3 3255 break;
81b2b810 3256 case QEMU_OPTION_fwcfg:
70b94331
MA
3257 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3258 optarg, true);
81b2b810
GS
3259 if (opts == NULL) {
3260 exit(1);
3261 }
3262 break;
047f7038 3263 case QEMU_OPTION_preconfig:
7691bdef 3264 preconfig_requested = true;
047f7038 3265 break;
7ba1e619 3266 case QEMU_OPTION_enable_kvm:
d8fb7d09 3267 qdict_put_str(machine_opts_dict, "accel", "kvm");
303d4e86 3268 break;
364c3e6b 3269 case QEMU_OPTION_M:
303d4e86 3270 case QEMU_OPTION_machine:
d8fb7d09
PB
3271 {
3272 bool help;
3273
3274 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3275 if (help) {
3276 machine_help_func(machine_opts_dict);
3277 exit(EXIT_SUCCESS);
3278 }
3279 break;
303d4e86 3280 }
a20fa79f 3281 case QEMU_OPTION_accel:
8d4e9146
FK
3282 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3283 optarg, true);
3284 optarg = qemu_opt_get(accel_opts, "accel");
bde4d920 3285 if (!optarg || is_help_option(optarg)) {
cbe6d636
WSM
3286 printf("Accelerators supported in QEMU binary:\n");
3287 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3288 false);
3289 for (el = accel_list; el; el = el->next) {
3290 gchar *typename = g_strdup(object_class_get_name(
3291 OBJECT_CLASS(el->data)));
3292 /* omit qtest which is used for tests only */
3293 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3294 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3295 gchar **optname = g_strsplit(typename,
3296 ACCEL_CLASS_SUFFIX, 0);
3297 printf("%s\n", optname[0]);
4903602e 3298 g_strfreev(optname);
cbe6d636
WSM
3299 }
3300 g_free(typename);
3301 }
3302 g_slist_free(accel_list);
a20fa79f 3303 exit(0);
8d4e9146
FK
3304 }
3305 break;
bb36d470 3306 case QEMU_OPTION_usb:
d8fb7d09 3307 qdict_put_str(machine_opts_dict, "usb", "on");
bb36d470 3308 break;
a594cfbf 3309 case QEMU_OPTION_usbdevice:
d8fb7d09 3310 qdict_put_str(machine_opts_dict, "usb", "on");
bd3c948d
GH
3311 add_device_config(DEV_USB, optarg);
3312 break;
3313 case QEMU_OPTION_device:
5dacda51
KW
3314 if (optarg[0] == '{') {
3315 QObject *obj = qobject_from_json(optarg, &error_fatal);
3316 DeviceOption *opt = g_new0(DeviceOption, 1);
3317 opt->opts = qobject_to(QDict, obj);
3318 loc_save(&opt->loc);
3319 assert(opt->opts != NULL);
3320 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3321 } else {
3322 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3323 optarg, true)) {
3324 exit(1);
3325 }
f31d07d1 3326 }
a594cfbf 3327 break;
6a00d601 3328 case QEMU_OPTION_smp:
f9dfae9c
MA
3329 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3330 "smp", optarg);
6a00d601 3331 break;
99efa84d 3332 case QEMU_OPTION_vnc:
653c9747 3333 vnc_parse(optarg);
821601ea 3334 break;
6515b203 3335 case QEMU_OPTION_no_acpi:
fffa36b6 3336 warn_report("-no-acpi is deprecated, use '-machine acpi=off' instead");
d8fb7d09 3337 qdict_put_str(machine_opts_dict, "acpi", "off");
6515b203 3338 break;
16b29ae1 3339 case QEMU_OPTION_no_hpet:
df37330c 3340 warn_report("-no-hpet is deprecated, use '-machine hpet=off' instead");
d8fb7d09 3341 qdict_put_str(machine_opts_dict, "hpet", "off");
16b29ae1 3342 break;
d1beab82 3343 case QEMU_OPTION_no_reboot:
2a5ad60b
AJ
3344 olist = qemu_find_opts("action");
3345 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
d1beab82 3346 break;
b2f76161 3347 case QEMU_OPTION_no_shutdown:
2a5ad60b 3348 olist = qemu_find_opts("action");
c27025e0 3349 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
b2f76161 3350 break;
8fcb1b90 3351 case QEMU_OPTION_uuid:
9c5ce8db 3352 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
4cd70f34 3353 error_report("failed to parse UUID string: wrong format");
8fcb1b90
BS
3354 exit(1);
3355 }
fc3b3295 3356 qemu_uuid_set = true;
8fcb1b90 3357 break;
99efa84d
MA
3358 case QEMU_OPTION_option_rom:
3359 if (nb_option_roms >= MAX_OPTION_ROMS) {
3e515373 3360 error_report("too many option ROMs");
99efa84d
MA
3361 exit(1);
3362 }
70b94331
MA
3363 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3364 optarg, true);
49295ebc
MA
3365 if (!opts) {
3366 exit(1);
3367 }
2e55e842
GN
3368 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3369 option_rom[nb_option_roms].bootindex =
3370 qemu_opt_get_number(opts, "bootindex", -1);
3371 if (!option_rom[nb_option_roms].name) {
f61eddcb 3372 error_report("Option ROM file is not specified");
2e55e842
GN
3373 exit(1);
3374 }
99efa84d
MA
3375 nb_option_roms++;
3376 break;
8e71621f 3377 case QEMU_OPTION_semihosting:
f1672e6f 3378 qemu_semihosting_enable();
a38bb079
LI
3379 break;
3380 case QEMU_OPTION_semihosting_config:
f1672e6f 3381 if (qemu_semihosting_config_options(optarg) != 0) {
a38bb079
LI
3382 exit(1);
3383 }
8e71621f 3384 break;
c35734b2 3385 case QEMU_OPTION_name:
70b94331
MA
3386 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3387 optarg, true);
5d12f961
DDAG
3388 if (!opts) {
3389 exit(1);
3390 }
2880ffb0
MS
3391 /* Capture guest name if -msg guest-name is used later */
3392 error_guest_name = qemu_opt_get(opts, "guest");
c35734b2 3393 break;
66508601
BS
3394 case QEMU_OPTION_prom_env:
3395 if (nb_prom_envs >= MAX_PROM_ENVS) {
3e515373 3396 error_report("too many prom variables");
66508601
BS
3397 exit(1);
3398 }
3399 prom_envs[nb_prom_envs] = optarg;
3400 nb_prom_envs++;
3401 break;
2b8f2d41
AZ
3402 case QEMU_OPTION_old_param:
3403 old_param = 1;
05ebd537 3404 break;
1ed2fc1f 3405 case QEMU_OPTION_rtc:
70b94331
MA
3406 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3407 false);
1ed2fc1f 3408 if (!opts) {
1ed2fc1f 3409 exit(1);
7e0af5d0
FB
3410 }
3411 break;
2e70f6ef 3412 case QEMU_OPTION_icount:
70b94331
MA
3413 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3414 optarg, true);
1ad9580b
ST
3415 if (!icount_opts) {
3416 exit(1);
3417 }
2e70f6ef 3418 break;
5bb7910a 3419 case QEMU_OPTION_incoming:
7c76235a
DDAG
3420 if (!incoming) {
3421 runstate_set(RUN_STATE_INMIGRATE);
3422 }
5bb7910a
AL
3423 incoming = optarg;
3424 break;
d15c05fc 3425 case QEMU_OPTION_only_migratable:
811f8652 3426 only_migratable = 1;
d15c05fc 3427 break;
d8c208dd 3428 case QEMU_OPTION_nodefaults:
d44229c5 3429 has_defaults = 0;
d8c208dd 3430 break;
e37630ca 3431 case QEMU_OPTION_xen_domid:
7058fa02 3432 if (!(accel_find("xen")) && !(accel_find("kvm"))) {
da002526 3433 error_report("Option not supported for this target");
ad96090a
BS
3434 exit(1);
3435 }
e37630ca
AL
3436 xen_domid = atoi(optarg);
3437 break;
e37630ca 3438 case QEMU_OPTION_xen_attach:
6773fbf8 3439 if (!(accel_find("xen"))) {
da002526 3440 error_report("Option not supported for this target");
ad96090a
BS
3441 exit(1);
3442 }
e37630ca
AL
3443 xen_mode = XEN_ATTACH;
3444 break;
1c599472 3445 case QEMU_OPTION_xen_domid_restrict:
6773fbf8 3446 if (!(accel_find("xen"))) {
1c599472
PD
3447 error_report("Option not supported for this target");
3448 exit(1);
3449 }
3450 xen_domid_restrict = true;
3451 break;
ab6540d5 3452 case QEMU_OPTION_trace:
92eecfff 3453 trace_opt_parse(optarg);
ab6540d5 3454 break;
42229a75
LV
3455 case QEMU_OPTION_plugin:
3456 qemu_plugin_opt_parse(optarg, &plugin_list);
3457 break;
715a664a 3458 case QEMU_OPTION_readconfig:
c0d4aa82 3459 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
f7544edc 3460 break;
5324e3e9 3461#ifdef CONFIG_SPICE
29b0040b 3462 case QEMU_OPTION_spice:
9ed345a1 3463 olist = qemu_find_opts_err("spice", NULL);
29b0040b 3464 if (!olist) {
5dfdae81 3465 error_report("spice support is disabled");
29b0040b
GH
3466 exit(1);
3467 }
70b94331 3468 opts = qemu_opts_parse_noisily(olist, optarg, false);
29b0040b 3469 if (!opts) {
29b0040b
GH
3470 exit(1);
3471 }
f963e4d0 3472 display_remote++;
29b0040b 3473 break;
5324e3e9 3474#endif
c7f0f3b1
AL
3475 case QEMU_OPTION_qtest:
3476 qtest_chrdev = optarg;
3477 break;
3478 case QEMU_OPTION_qtest_log:
3479 qtest_log = optarg;
3480 break;
7d76ad4f 3481 case QEMU_OPTION_sandbox:
2bb814a4
MAL
3482 olist = qemu_find_opts("sandbox");
3483 if (!olist) {
3484#ifndef CONFIG_SECCOMP
3485 error_report("-sandbox support is not enabled "
3486 "in this QEMU binary");
3487#endif
3488 exit(1);
3489 }
3490
3491 opts = qemu_opts_parse_noisily(olist, optarg, true);
7d76ad4f 3492 if (!opts) {
49295ebc 3493 exit(1);
7d76ad4f
EO
3494 }
3495 break;
587ed6be
CB
3496 case QEMU_OPTION_add_fd:
3497#ifndef _WIN32
70b94331
MA
3498 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3499 optarg, false);
587ed6be 3500 if (!opts) {
49295ebc 3501 exit(1);
587ed6be
CB
3502 }
3503#else
3504 error_report("File descriptor passing is disabled on this "
3505 "platform");
3506 exit(1);
3507#endif
3508 break;
68d98d3e 3509 case QEMU_OPTION_object:
bc2f4fcb 3510 object_option_parse(optarg);
68d98d3e 3511 break;
6f131f13
MT
3512 case QEMU_OPTION_overcommit:
3513 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3514 optarg, false);
3515 if (!opts) {
3516 exit(1);
3517 }
c8c9dc42 3518 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
6f131f13 3519 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
888a6bc6 3520 break;
6dd75472
MA
3521 case QEMU_OPTION_compat:
3522 {
c431ffd4 3523 CompatPolicy *opts_policy;
6dd75472
MA
3524 Visitor *v;
3525
3526 v = qobject_input_visitor_new_str(optarg, NULL,
3527 &error_fatal);
3528
c431ffd4
PB
3529 visit_type_CompatPolicy(v, NULL, &opts_policy, &error_fatal);
3530 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts_policy);
6dd75472 3531
c431ffd4 3532 qapi_free_CompatPolicy(opts_policy);
6dd75472
MA
3533 visit_free(v);
3534 break;
3535 }
5e2ac519 3536 case QEMU_OPTION_msg:
70b94331
MA
3537 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3538 false);
5e2ac519
SA
3539 if (!opts) {
3540 exit(1);
3541 }
3542 configure_msg(opts);
3543 break;
abfd9ce3 3544 case QEMU_OPTION_dump_vmstate:
522abf69 3545 if (vmstate_dump_file) {
f61eddcb
EH
3546 error_report("only one '-dump-vmstate' "
3547 "option may be given");
522abf69
GA
3548 exit(1);
3549 }
abfd9ce3
AS
3550 vmstate_dump_file = fopen(optarg, "w");
3551 if (vmstate_dump_file == NULL) {
f61eddcb 3552 error_report("open %s: %s", optarg, strerror(errno));
abfd9ce3
AS
3553 exit(1);
3554 }
12df189d
EC
3555 break;
3556 case QEMU_OPTION_enable_sync_profile:
3557 qsp_enable();
abfd9ce3 3558 break;
aec0d0e1
MP
3559 case QEMU_OPTION_nouserconfig:
3560 /* Nothing to be parsed here. Especially, do not error out below. */
3561 break;
8a768db1
MT
3562#if defined(CONFIG_POSIX)
3563 case QEMU_OPTION_runas:
3564 if (!os_set_runas(optarg)) {
3565 error_report("User \"%s\" doesn't exist"
3566 " (and is not <uid>:<gid>)",
3567 optarg);
1217d6ca
TH
3568 exit(1);
3569 }
8a768db1
MT
3570 break;
3571 case QEMU_OPTION_chroot:
3572 warn_report("option is deprecated,"
3573 " use '-run-with chroot=...' instead");
3574 os_set_chroot(optarg);
3575 break;
3576 case QEMU_OPTION_daemonize:
3577 os_set_daemonize(true);
3578 break;
3579#if defined(CONFIG_LINUX)
3580 /* deprecated */
3581 case QEMU_OPTION_asyncteardown:
3582 init_async_teardown();
3583 break;
3584#endif
3585 case QEMU_OPTION_run_with: {
3586 const char *str;
3587 opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
3588 optarg, false);
3589 if (!opts) {
1217d6ca
TH
3590 exit(1);
3591 }
8a768db1
MT
3592#if defined(CONFIG_LINUX)
3593 if (qemu_opt_get_bool(opts, "async-teardown", false)) {
3594 init_async_teardown();
3595 }
3596#endif
3597 str = qemu_opt_get(opts, "chroot");
3598 if (str) {
3599 os_set_chroot(str);
3600 }
3601 break;
3602 }
3603#endif /* CONFIG_POSIX */
3604
3605 default:
3606 error_report("Option not supported in this build");
3607 exit(1);
cd6f1169 3608 }
0824d6fc
FB
3609 }
3610 }
43fa1e0b
EH
3611 /*
3612 * Clear error location left behind by the loop.
3613 * Best done right after the loop. Do not insert code here!
3614 */
3615 loc_set_none();
364c3e6b 3616
d8fb7d09 3617 qemu_validate_options(machine_opts_dict);
4d2c17b0 3618 qemu_process_sugar_options();
58c91595 3619
3df261b6 3620 /*
0546c060
PB
3621 * These options affect everything else and should be processed
3622 * before daemonizing.
3df261b6 3623 */
0546c060 3624 qemu_process_early_options();
3df261b6 3625
0546c060
PB
3626 qemu_process_help_options();
3627 qemu_maybe_daemonize(pid_file);
3628
10b6ee16
DHB
3629 /*
3630 * The trace backend must be initialized after daemonizing.
3631 * trace_init_backends() will call st_init(), which will create the
3632 * trace thread in the parent, and also register st_flush_trace_buffer()
3633 * in atexit(). This function will force the parent to wait for the
3634 * writeout thread to finish, which will not occur, and the parent
3635 * process will be left in the host.
3636 */
3637 if (!trace_init_backends()) {
3638 exit(1);
3639 }
3640 trace_init_file();
3641
efd7ab22
PB
3642 qemu_init_main_loop(&error_fatal);
3643 cpu_timers_init();
3df261b6 3644
fc4a4734 3645 user_register_global_props();
4c27b859
PD
3646 replay_configure(icount_opts);
3647
eb6a5209
AP
3648 configure_rtc(qemu_find_opts_singleton("rtc"));
3649
e12f0685
PB
3650 /* Transfer QemuOpts options into machine options */
3651 parse_memory_options();
3652
d8fb7d09 3653 qemu_create_machine(machine_opts_dict);
67b724e6 3654
5a1ee607
PB
3655 suspend_mux_open();
3656
f650266b
PB
3657 qemu_disable_default_devices();
3658 qemu_create_default_devices();
64418657 3659 qemu_create_early_backends();
cda4aa9a 3660
d8fb7d09
PB
3661 qemu_apply_legacy_machine_options(machine_opts_dict);
3662 qemu_apply_machine_options(machine_opts_dict);
3663 qobject_unref(machine_opts_dict);
2f181fbd 3664 phase_advance(PHASE_MACHINE_CREATED);
6b1b1440 3665
0427b625
MA
3666 /*
3667 * Note: uses machine properties such as kernel-irqchip, must run
d8fb7d09 3668 * after qemu_apply_machine_options.
0427b625 3669 */
28a09617 3670 configure_accelerators(argv[0]);
2f181fbd 3671 phase_advance(PHASE_ACCEL_CREATED);
214910a7 3672
0427b625
MA
3673 /*
3674 * Beware, QOM objects created before this point miss global and
3675 * compat properties.
3676 *
3677 * Global properties get set up by qdev_prop_register_global(),
3678 * called from user_register_global_props(), and certain option
3679 * desugaring. Also in CPU feature desugaring (buried in
c1c8cfe5 3680 * parse_cpu_option()), which happens below this point, but may
0427b625 3681 * only target the CPU type, which can only be created after
c1c8cfe5 3682 * parse_cpu_option() returned the type.
0427b625
MA
3683 *
3684 * Machine compat properties: object_set_machine_compat_props().
3685 * Accelerator compat props: object_set_accelerator_compat_props(),
b86f59c7 3686 * called from do_configure_accelerator().
0427b625
MA
3687 */
3688
f5c9fcb8 3689 machine_class = MACHINE_GET_CLASS(current_machine);
08fe6824 3690 if (!qtest_enabled() && machine_class->deprecation_reason) {
7adb75d6 3691 warn_report("Machine type '%s' is deprecated: %s",
08fe6824
TH
3692 machine_class->name, machine_class->deprecation_reason);
3693 }
3694
cb9d8b8c
PX
3695 /*
3696 * Create backends before creating migration objects, so that it can
3697 * check against compatibilities on the backend memories (e.g. postcopy
3698 * over memory-backend-file objects).
3699 */
3700 qemu_create_late_backends();
3701
2fa23277 3702 /*
0427b625
MA
3703 * Note: creates a QOM object, must run only after global and
3704 * compat properties have been set up.
2fa23277
MA
3705 */
3706 migration_object_init();
3707
6063d4c0 3708 /* parse features once if machine provides default cpu_type */
2278b939 3709 current_machine->cpu_type = machine_class->default_cpu_type;
c1c8cfe5
EH
3710 if (cpu_option) {
3711 current_machine->cpu_type = parse_cpu_option(cpu_option);
6063d4c0 3712 }
b86f59c7 3713 /* NB: for machine none cpu_type could STILL be NULL here! */
a1b18df9 3714
b24986e7 3715 qemu_resolve_machine_memdev();
d342eb76 3716 parse_numa_opts(current_machine);
6063d4c0 3717
abfd9ce3
AS
3718 if (vmstate_dump_file) {
3719 /* dump and exit */
4067691a 3720 module_load_qom_all();
abfd9ce3 3721 dump_vmstate_json_to_file(vmstate_dump_file);
7b733862 3722 exit(0);
abfd9ce3 3723 }
ffd843bc 3724
b4e1a342
PB
3725 if (!preconfig_requested) {
3726 qmp_x_exit_preconfig(&error_fatal);
3727 }
facf7c60 3728 qemu_init_displays();
7a64c17f 3729 accel_setup_post(current_machine);
eb505be1 3730 os_setup_post();
5a1ee607 3731 resume_mux_open();
7b733862 3732}