]> git.proxmox.com Git - mirror_qemu.git/blame - softmmu/vl.c
Merge tag 'q800-for-8.2-pull-request' of https://github.com/vivier/qemu-m68k into...
[mirror_qemu.git] / softmmu / vl.c
CommitLineData
0824d6fc 1/*
80cabfad 2 * QEMU System Emulator
5fafdf24 3 *
68d0f70e 4 * Copyright (c) 2003-2008 Fabrice Bellard
5fafdf24 5 *
1df912cf
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
0824d6fc 23 */
e688df6b 24
d38ea87a 25#include "qemu/osdep.h"
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
48176a1d 1069static void parse_display_qapi(const char *str)
776d1344 1070{
776d1344
GH
1071 DisplayOptions *opts;
1072 Visitor *v;
1073
48176a1d 1074 v = qobject_input_visitor_new_str(str, "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
48176a1d 1224static void monitor_parse(const char *str, 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 1230
48176a1d 1231 if (strstart(str, "chardev:", &p)) {
88589343
GH
1232 snprintf(label, sizeof(label), "%s", p);
1233 } else {
140e065d
JK
1234 snprintf(label, sizeof(label), "compat_monitor%d",
1235 monitor_device_index);
48176a1d 1236 opts = qemu_chr_parse_compat(label, str, true);
88589343 1237 if (!opts) {
48176a1d 1238 error_report("parse error: %s", str);
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{
48176a1d 1634 const char *machine_type = 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
48176a1d
PMD
1639 if (machine_type) {
1640 machine_class = find_machine(machine_type, machines);
d8fb7d09
PB
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
48176a1d 1784static void object_option_parse(const char *str)
bc2f4fcb 1785{
bc2f4fcb
PB
1786 QemuOpts *opts;
1787 const char *type;
1788 Visitor *v;
1789
48176a1d
PMD
1790 if (str[0] == '{') {
1791 QObject *obj = qobject_from_json(str, &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"),
48176a1d 1797 str, true);
009ff893
PB
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);
cb94ff5f 1965 audio_init_audiodevs();
64418657
PB
1966}
1967
f08f9271
DB
1968
1969/*
1970 * The remainder of object creation happens after the
9abce56d 1971 * creation of chardev, fsdev, net clients and device data types.
f08f9271 1972 */
bc2f4fcb 1973static bool object_create_late(const char *type)
f08f9271 1974{
bd77c30d 1975 return !object_create_early(type) && !object_create_pre_sandbox(type);
f08f9271
DB
1976}
1977
07a91b1a
PB
1978static void qemu_create_late_backends(void)
1979{
1980 if (qtest_chrdev) {
1981 qtest_server_init(qtest_chrdev, qtest_log, &error_fatal);
1982 }
1983
d63ef17b 1984 net_init_clients();
07a91b1a 1985
bc2f4fcb 1986 object_option_foreach_add(object_create_late);
07a91b1a
PB
1987
1988 if (tpm_init() < 0) {
1989 exit(1);
1990 }
1991
1992 qemu_opts_foreach(qemu_find_opts("mon"),
1993 mon_init_func, NULL, &error_fatal);
1994
1995 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
1996 exit(1);
1997 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
1998 exit(1);
1999 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
2000 exit(1);
2001
2002 /* now chardevs have been created we may have semihosting to connect */
fb08790b 2003 qemu_semihosting_chardev_init();
07a91b1a 2004}
f08f9271 2005
b24986e7
PB
2006static void qemu_resolve_machine_memdev(void)
2007{
26f88d84 2008 if (ram_memdev_id) {
b24986e7
PB
2009 Object *backend;
2010 ram_addr_t backend_size;
2011
26f88d84 2012 backend = object_resolve_path_type(ram_memdev_id,
b24986e7
PB
2013 TYPE_MEMORY_BACKEND, NULL);
2014 if (!backend) {
26f88d84 2015 error_report("Memory backend '%s' not found", ram_memdev_id);
b24986e7
PB
2016 exit(EXIT_FAILURE);
2017 }
fb56b7a0
PB
2018 if (!have_custom_ram_size) {
2019 backend_size = object_property_get_uint(backend, "size", &error_abort);
2020 current_machine->ram_size = backend_size;
b24986e7 2021 }
26f88d84
PB
2022 object_property_set_link(OBJECT(current_machine),
2023 "memory-backend", backend, &error_fatal);
b24986e7 2024 }
b24986e7
PB
2025}
2026
e12f0685 2027static void parse_memory_options(void)
3b9985e9 2028{
e12f0685 2029 QemuOpts *opts = qemu_find_opts_singleton("memory");
ce9d03fb 2030 QDict *dict, *prop;
3b9985e9 2031 const char *mem_str;
e12f0685 2032 Location loc;
bbe2d25c 2033
e12f0685
PB
2034 loc_push_none(&loc);
2035 qemu_opts_loc_restore(opts);
ce9d03fb
PB
2036
2037 prop = qdict_new();
3b9985e9 2038
ce9d03fb 2039 if (qemu_opt_get_size(opts, "size", 0) != 0) {
3b9985e9 2040 /* Fix up legacy suffix-less format */
69c05a23 2041 mem_str = qemu_opt_get(opts, "size");
3b9985e9 2042 if (g_ascii_isdigit(mem_str[strlen(mem_str) - 1])) {
ce9d03fb
PB
2043 g_autofree char *mib_str = g_strdup_printf("%sM", mem_str);
2044 qdict_put_str(prop, "size", mib_str);
2045 } else {
2046 qdict_put_str(prop, "size", mem_str);
3b9985e9
MA
2047 }
2048 }
2049
951f2269 2050 if (qemu_opt_get(opts, "maxmem")) {
ce9d03fb
PB
2051 qdict_put_str(prop, "max-size", qemu_opt_get(opts, "maxmem"));
2052 }
2053 if (qemu_opt_get(opts, "slots")) {
2054 qdict_put_str(prop, "slots", qemu_opt_get(opts, "slots"));
3b9985e9 2055 }
bbe2d25c 2056
ce9d03fb
PB
2057 dict = qdict_new();
2058 qdict_put(dict, "memory", prop);
2059 keyval_merge(machine_opts_dict, dict, &error_fatal);
2060 qobject_unref(dict);
e12f0685 2061 loc_pop(&loc);
f5c9fcb8
PB
2062}
2063
d8fb7d09 2064static void qemu_create_machine(QDict *qdict)
f5c9fcb8 2065{
d8fb7d09 2066 MachineClass *machine_class = select_machine(qdict, &error_fatal);
f5c9fcb8
PB
2067 object_set_machine_compat_props(machine_class->compat_props);
2068
f5c9fcb8 2069 current_machine = MACHINE(object_new_with_class(OBJECT_CLASS(machine_class)));
f5c9fcb8
PB
2070 object_property_add_child(object_get_root(), "machine",
2071 OBJECT(current_machine));
2072 object_property_add_child(container_get(OBJECT(current_machine),
2073 "/unattached"),
2074 "sysbus", OBJECT(sysbus_get_default()));
2075
2076 if (machine_class->minimum_page_bits) {
2077 if (!set_preferred_target_page_bits(machine_class->minimum_page_bits)) {
2078 /* This would be a board error: specifying a minimum smaller than
2079 * a target's compile-time fixed setting.
2080 */
2081 g_assert_not_reached();
2082 }
2083 }
2084
2085 cpu_exec_init_all();
2086 page_size_init();
2087
2088 if (machine_class->hw_version) {
2089 qemu_set_hw_version(machine_class->hw_version);
2090 }
2091
f5c9fcb8
PB
2092 /*
2093 * Get the default machine options from the machine if it is not already
2094 * specified either by the configuration file or by the command line.
2095 */
2096 if (machine_class->default_machine_opts) {
d8fb7d09
PB
2097 QDict *default_opts =
2098 keyval_parse(machine_class->default_machine_opts, NULL, NULL,
2099 &error_abort);
dadafe67 2100 qemu_apply_legacy_machine_options(default_opts);
d8fb7d09
PB
2101 object_set_properties_from_keyval(OBJECT(current_machine), default_opts,
2102 false, &error_abort);
2103 qobject_unref(default_opts);
f5c9fcb8 2104 }
3b9985e9
MA
2105}
2106
8d76bfe8
EH
2107static int global_init_func(void *opaque, QemuOpts *opts, Error **errp)
2108{
2109 GlobalProperty *g;
2110
2111 g = g_malloc0(sizeof(*g));
2112 g->driver = qemu_opt_get(opts, "driver");
2113 g->property = qemu_opt_get(opts, "property");
2114 g->value = qemu_opt_get(opts, "value");
8d76bfe8
EH
2115 qdev_prop_register_global(g);
2116 return 0;
2117}
2118
c0d4aa82
PB
2119/*
2120 * Return whether configuration group @group is stored in QemuOpts, or
2121 * recorded as one or more QDicts by qemu_record_config_group.
2122 */
2123static bool is_qemuopts_group(const char *group)
2124{
d8fb7d09 2125 if (g_str_equal(group, "object") ||
adf7f6b7 2126 g_str_equal(group, "audiodev") ||
e4383ca2 2127 g_str_equal(group, "machine") ||
8c4da4b5 2128 g_str_equal(group, "smp-opts") ||
e12f0685 2129 g_str_equal(group, "boot-opts")) {
49e98769
PB
2130 return false;
2131 }
c0d4aa82
PB
2132 return true;
2133}
2134
2135static void qemu_record_config_group(const char *group, QDict *dict,
2136 bool from_json, Error **errp)
2137{
49e98769
PB
2138 if (g_str_equal(group, "object")) {
2139 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2140 object_option_add_visitor(v);
2141 visit_free(v);
adf7f6b7
PB
2142
2143 } else if (g_str_equal(group, "audiodev")) {
2144 Audiodev *dev = NULL;
2145 Visitor *v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2146 if (visit_type_Audiodev(v, NULL, &dev, errp)) {
2147 audio_define(dev);
2148 }
2149 visit_free(v);
2150
d8fb7d09
PB
2151 } else if (g_str_equal(group, "machine")) {
2152 /*
2153 * Cannot merge string-valued and type-safe dictionaries, so JSON
2154 * is not accepted yet for -M.
2155 */
2156 assert(!from_json);
2157 keyval_merge(machine_opts_dict, dict, errp);
e4383ca2
PB
2158 } else if (g_str_equal(group, "smp-opts")) {
2159 machine_merge_property("smp", dict, &error_fatal);
8c4da4b5
PB
2160 } else if (g_str_equal(group, "boot-opts")) {
2161 machine_merge_property("boot", dict, &error_fatal);
49e98769
PB
2162 } else {
2163 abort();
2164 }
c0d4aa82
PB
2165}
2166
2167/*
2168 * Parse non-QemuOpts config file groups, pass the rest to
2169 * qemu_config_do_parse.
2170 */
2171static void qemu_parse_config_group(const char *group, QDict *qdict,
2172 void *opaque, Error **errp)
2173{
2174 QObject *crumpled;
2175 if (is_qemuopts_group(group)) {
2176 qemu_config_do_parse(group, qdict, opaque, errp);
2177 return;
2178 }
2179
2180 crumpled = qdict_crumple(qdict, errp);
2181 if (!crumpled) {
2182 return;
2183 }
838e3700
PB
2184 switch (qobject_type(crumpled)) {
2185 case QTYPE_QDICT:
2186 qemu_record_config_group(group, qobject_to(QDict, crumpled), false, errp);
2187 break;
2188 case QTYPE_QLIST:
c0d4aa82 2189 error_setg(errp, "Lists cannot be at top level of a configuration section");
838e3700
PB
2190 break;
2191 default:
2192 g_assert_not_reached();
c0d4aa82 2193 }
838e3700 2194 qobject_unref(crumpled);
c0d4aa82
PB
2195}
2196
f7544edc 2197static void qemu_read_default_config_file(Error **errp)
726401be 2198{
f7544edc 2199 ERRP_GUARD();
726401be 2200 int ret;
580e6ad6 2201 g_autofree char *file = get_relocated_path(CONFIG_QEMU_CONFDIR "/qemu.conf");
726401be 2202
c0d4aa82 2203 ret = qemu_read_config_file(file, qemu_parse_config_group, errp);
f7544edc
PB
2204 if (ret < 0) {
2205 if (ret == -ENOENT) {
2206 error_free(*errp);
2207 *errp = NULL;
2208 }
726401be 2209 }
726401be
EH
2210}
2211
c0d4aa82 2212static void qemu_set_option(const char *str, Error **errp)
ed7fa564 2213{
ed7fa564
PB
2214 char group[64], id[64], arg[64];
2215 QemuOptsList *list;
2216 QemuOpts *opts;
2217 int rc, offset;
2218
2219 rc = sscanf(str, "%63[^.].%63[^.].%63[^=]%n", group, id, arg, &offset);
2220 if (rc < 3 || str[offset] != '=') {
c0d4aa82
PB
2221 error_setg(errp, "can't parse: \"%s\"", str);
2222 return;
ed7fa564
PB
2223 }
2224
c0d4aa82
PB
2225 if (!is_qemuopts_group(group)) {
2226 error_setg(errp, "-set is not supported with %s", group);
2227 } else {
2228 list = qemu_find_opts_err(group, errp);
2229 if (list) {
2230 opts = qemu_opts_find(list, id);
2231 if (!opts) {
2232 error_setg(errp, "there is no %s \"%s\" defined", group, id);
2233 return;
2234 }
2235 qemu_opt_set(opts, arg, str + offset + 1, errp);
2236 }
ed7fa564 2237 }
ed7fa564
PB
2238}
2239
a0660e0b
PX
2240static void user_register_global_props(void)
2241{
2242 qemu_opts_foreach(qemu_find_opts("global"),
2243 global_init_func, NULL, NULL);
2244}
2245
7f8b6126
PB
2246static int do_configure_icount(void *opaque, QemuOpts *opts, Error **errp)
2247{
8191d368 2248 icount_configure(opts, errp);
7f8b6126
PB
2249 return 0;
2250}
2251
12ceaef6
PB
2252static int accelerator_set_property(void *opaque,
2253 const char *name, const char *value,
2254 Error **errp)
2255{
2256 return object_parse_property_opt(opaque, name, value, "accel", errp);
2257}
2258
deda73e8
PB
2259static int do_configure_accelerator(void *opaque, QemuOpts *opts, Error **errp)
2260{
6f6e1698
PB
2261 bool *p_init_failed = opaque;
2262 const char *acc = qemu_opt_get(opts, "accel");
2263 AccelClass *ac = accel_find(acc);
fc5cf826 2264 AccelState *accel;
6f6e1698 2265 int ret;
eaf65114
TH
2266 bool qtest_with_kvm;
2267
78901b50
PB
2268 if (!acc) {
2269 error_setg(errp, QERR_MISSING_PARAMETER, "accel");
2270 goto bad;
2271 }
2272
eaf65114 2273 qtest_with_kvm = g_str_equal(acc, "kvm") && qtest_chrdev != NULL;
6f6e1698
PB
2274
2275 if (!ac) {
eaf65114
TH
2276 if (!qtest_with_kvm) {
2277 error_report("invalid accelerator %s", acc);
2278 }
78901b50 2279 goto bad;
6f6e1698 2280 }
fc5cf826 2281 accel = ACCEL(object_new_with_class(OBJECT_CLASS(ac)));
fe174132 2282 object_apply_compat_props(OBJECT(accel));
12ceaef6
PB
2283 qemu_opt_foreach(opts, accelerator_set_property,
2284 accel,
2285 &error_fatal);
3cfb0456
PM
2286 /*
2287 * If legacy -singlestep option is set, honour it for TCG and
2288 * silently ignore for any other accelerator (which is how this
2289 * option has always behaved).
2290 */
2291 if (opt_one_insn_per_tb) {
2292 /*
2293 * This will always succeed for TCG, and we want to ignore
2294 * the error from trying to set a nonexistent property
2295 * on any other accelerator.
2296 */
2297 object_property_set_bool(OBJECT(accel), "one-insn-per-tb", true, NULL);
2298 }
fc5cf826 2299 ret = accel_init_machine(accel, current_machine);
6f6e1698 2300 if (ret < 0) {
eaf65114
TH
2301 if (!qtest_with_kvm || ret != -ENOENT) {
2302 error_report("failed to initialize %s: %s", acc, strerror(-ret));
2303 }
78901b50 2304 goto bad;
6f6e1698 2305 }
fe174132 2306
6f6e1698 2307 return 1;
78901b50
PB
2308
2309bad:
2310 *p_init_failed = true;
2311 return 0;
deda73e8
PB
2312}
2313
28a09617 2314static void configure_accelerators(const char *progname)
deda73e8 2315{
28a09617 2316 bool init_failed = false;
28a09617
PB
2317
2318 qemu_opts_foreach(qemu_find_opts("icount"),
2319 do_configure_icount, NULL, &error_fatal);
2320
6f6e1698 2321 if (QTAILQ_EMPTY(&qemu_accel_opts.head)) {
60ee3552
RH
2322 char **accel_list, **tmp;
2323
7245ca74 2324 if (accelerators == NULL) {
6f6e1698 2325 /* Select the default accelerator */
755ee1f3
RH
2326 bool have_tcg = accel_find("tcg");
2327 bool have_kvm = accel_find("kvm");
2328
2329 if (have_tcg && have_kvm) {
2330 if (g_str_has_suffix(progname, "kvm")) {
6f6e1698 2331 /* If the program name ends with "kvm", we prefer KVM */
7245ca74 2332 accelerators = "kvm:tcg";
6f6e1698 2333 } else {
7245ca74 2334 accelerators = "tcg:kvm";
6f6e1698 2335 }
755ee1f3 2336 } else if (have_kvm) {
7245ca74 2337 accelerators = "kvm";
755ee1f3 2338 } else if (have_tcg) {
7245ca74 2339 accelerators = "tcg";
755ee1f3
RH
2340 } else {
2341 error_report("No accelerator selected and"
2342 " no default accelerator available");
2343 exit(1);
28a09617
PB
2344 }
2345 }
7245ca74 2346 accel_list = g_strsplit(accelerators, ":", 0);
28a09617 2347
a024b090 2348 for (tmp = accel_list; *tmp; tmp++) {
6f6e1698
PB
2349 /*
2350 * Filter invalid accelerators here, to prevent obscenities
2351 * such as "-machine accel=tcg,,thread=single".
2352 */
2353 if (accel_find(*tmp)) {
2354 qemu_opts_parse_noisily(qemu_find_opts("accel"), *tmp, true);
8b90f1c5
PB
2355 } else {
2356 init_failed = true;
2357 error_report("invalid accelerator %s", *tmp);
6f6e1698 2358 }
28a09617 2359 }
adb464ff 2360 g_strfreev(accel_list);
6f6e1698 2361 } else {
7245ca74 2362 if (accelerators != NULL) {
6f6e1698
PB
2363 error_report("The -accel and \"-machine accel=\" options are incompatible");
2364 exit(1);
28a09617
PB
2365 }
2366 }
28a09617 2367
6f6e1698
PB
2368 if (!qemu_opts_foreach(qemu_find_opts("accel"),
2369 do_configure_accelerator, &init_failed, &error_fatal)) {
28a09617 2370 if (!init_failed) {
6f6e1698 2371 error_report("no accelerator found");
28a09617
PB
2372 }
2373 exit(1);
2374 }
2375
eaf65114 2376 if (init_failed && !qtest_chrdev) {
55bd445c 2377 error_report("falling back to %s", current_accel_name());
28a09617
PB
2378 }
2379
740b1759 2380 if (icount_enabled() && !tcg_enabled()) {
deda73e8
PB
2381 error_report("-icount is not allowed with hardware virtualization");
2382 exit(1);
2383 }
2384}
2385
d8fb7d09 2386static void qemu_validate_options(const QDict *machine_opts)
58c91595 2387{
d8fb7d09
PB
2388 const char *kernel_filename = qdict_get_try_str(machine_opts, "kernel");
2389 const char *initrd_filename = qdict_get_try_str(machine_opts, "initrd");
2390 const char *kernel_cmdline = qdict_get_try_str(machine_opts, "append");
58c91595
PB
2391
2392 if (kernel_filename == NULL) {
2393 if (kernel_cmdline != NULL) {
2394 error_report("-append only allowed with -kernel option");
2395 exit(1);
2396 }
2397
2398 if (initrd_filename != NULL) {
2399 error_report("-initrd only allowed with -kernel option");
2400 exit(1);
2401 }
2402 }
2403
7691bdef 2404 if (loadvm && preconfig_requested) {
90285ec8
PB
2405 error_report("'preconfig' and 'loadvm' options are "
2406 "mutually exclusive");
2407 exit(EXIT_FAILURE);
2408 }
8fafaa04
PB
2409 if (incoming && preconfig_requested && strcmp(incoming, "defer") != 0) {
2410 error_report("'preconfig' supports '-incoming defer' only");
58c91595
PB
2411 exit(EXIT_FAILURE);
2412 }
2413
2414#ifdef CONFIG_CURSES
2415 if (is_daemonized() && dpy.type == DISPLAY_TYPE_CURSES) {
2416 error_report("curses display cannot be used with -daemonize");
2417 exit(1);
2418 }
2419#endif
2420}
2421
4d2c17b0
PB
2422static void qemu_process_sugar_options(void)
2423{
2424 if (mem_prealloc) {
e4383ca2
PB
2425 QObject *smp = qdict_get(machine_opts_dict, "smp");
2426 if (smp && qobject_type(smp) == QTYPE_QDICT) {
2427 QObject *cpus = qdict_get(qobject_to(QDict, smp), "cpus");
2428 if (cpus && qobject_type(cpus) == QTYPE_QSTRING) {
2429 const char *val = qstring_get_str(qobject_to(QString, cpus));
2430 object_register_sugar_prop("memory-backend", "prealloc-threads",
2431 val, false);
2432 }
2433 }
a8dc82ce 2434 object_register_sugar_prop("memory-backend", "prealloc", "on", false);
4d2c17b0 2435 }
4d2c17b0
PB
2436}
2437
2a5ad60b
AJ
2438/* -action processing */
2439
2440/*
2441 * Process all the -action parameters parsed from cmdline.
2442 */
2443static int process_runstate_actions(void *opaque, QemuOpts *opts, Error **errp)
2444{
2445 Error *local_err = NULL;
2446 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
2447 QObject *ret = NULL;
2448 qmp_marshal_set_action(qdict, &ret, &local_err);
2449 qobject_unref(ret);
2450 qobject_unref(qdict);
2451 if (local_err) {
2452 error_propagate(errp, local_err);
2453 return 1;
2454 }
2455 return 0;
2456}
2457
0546c060
PB
2458static void qemu_process_early_options(void)
2459{
bd77c30d
DH
2460 qemu_opts_foreach(qemu_find_opts("name"),
2461 parse_name, NULL, &error_fatal);
2462
2463 object_option_foreach_add(object_create_pre_sandbox);
2464
0546c060
PB
2465#ifdef CONFIG_SECCOMP
2466 QemuOptsList *olist = qemu_find_opts_err("sandbox", NULL);
2467 if (olist) {
2468 qemu_opts_foreach(olist, parse_sandbox, NULL, &error_fatal);
2469 }
2470#endif
2471
2a5ad60b
AJ
2472 if (qemu_opts_foreach(qemu_find_opts("action"),
2473 process_runstate_actions, NULL, &error_fatal)) {
2474 exit(1);
2475 }
2476
0546c060
PB
2477#ifndef _WIN32
2478 qemu_opts_foreach(qemu_find_opts("add-fd"),
2479 parse_add_fd, NULL, &error_fatal);
2480
2481 qemu_opts_foreach(qemu_find_opts("add-fd"),
2482 cleanup_add_fd, NULL, &error_fatal);
2483#endif
2484
0546c060 2485 /* Open the logfile at this point and set the log mask if necessary. */
b2528af1
RH
2486 {
2487 int mask = 0;
2488 if (log_mask) {
2489 mask = qemu_str_to_log_mask(log_mask);
2490 if (!mask) {
2491 qemu_print_log_usage(stdout);
2492 exit(1);
2493 }
0546c060 2494 }
b2528af1 2495 qemu_set_log_filename_flags(log_file, mask, &error_fatal);
0546c060
PB
2496 }
2497
2c65db5e 2498 qemu_add_default_firmwarepath();
0546c060
PB
2499}
2500
2501static void qemu_process_help_options(void)
2502{
0546c060
PB
2503 /*
2504 * Check for -cpu help and -device help before we call select_machine(),
2505 * which will return an error if the architecture has no default machine
2506 * type and the user did not specify one, so that the user doesn't need
2507 * to say '-cpu help -machine something'.
2508 */
2509 if (cpu_option && is_help_option(cpu_option)) {
c138c3b8 2510 list_cpus();
0546c060
PB
2511 exit(0);
2512 }
2513
2514 if (qemu_opts_foreach(qemu_find_opts("device"),
2515 device_help_func, NULL, NULL)) {
2516 exit(0);
2517 }
2518
2519 /* -L help lists the data directories and exits. */
2520 if (list_data_dirs) {
2c65db5e 2521 qemu_list_data_dirs();
0546c060
PB
2522 exit(0);
2523 }
2524}
2525
2526static void qemu_maybe_daemonize(const char *pid_file)
2527{
38f71349 2528 Error *err = NULL;
0546c060
PB
2529
2530 os_daemonize();
2531 rcu_disable_atfork();
2532
eed29d49 2533 if (pid_file) {
85c4bf8a
HR
2534 char *pid_file_realpath = NULL;
2535
eed29d49
HR
2536 if (!qemu_write_pidfile(pid_file, &err)) {
2537 error_reportf_err(err, "cannot create PID file: ");
2538 exit(1);
2539 }
0546c060 2540
85c4bf8a
HR
2541 pid_file_realpath = g_malloc0(PATH_MAX);
2542 if (!realpath(pid_file, pid_file_realpath)) {
dee2a4d4
FE
2543 if (errno != ENOENT) {
2544 warn_report("not removing PID file on exit: cannot resolve PID "
2545 "file path: %s: %s", pid_file, strerror(errno));
2546 }
2547 return;
85c4bf8a
HR
2548 }
2549
2550 qemu_unlink_pidfile_notifier = (struct UnlinkPidfileNotifier) {
2551 .notifier = {
2552 .notify = qemu_unlink_pidfile,
2553 },
2554 .pid_file_realpath = pid_file_realpath,
2555 };
2556 qemu_add_exit_notifier(&qemu_unlink_pidfile_notifier.notifier);
eed29d49 2557 }
0546c060
PB
2558}
2559
7691bdef
PB
2560static void qemu_init_displays(void)
2561{
2562 DisplayState *ds;
2563
2564 /* init local displays */
2565 ds = init_displaystate();
2566 qemu_display_init(ds, &dpy);
2567
2568 /* must be after terminal init, SDL library changes signal handlers */
2569 os_setup_signal_handling();
2570
2571 /* init remote displays */
2572#ifdef CONFIG_VNC
2573 qemu_opts_foreach(qemu_find_opts("vnc"),
2574 vnc_init_func, NULL, &error_fatal);
2575#endif
2576
2577 if (using_spice) {
2578 qemu_spice.display_init();
2579 }
2580}
2581
d8e4de41
PB
2582static void qemu_init_board(void)
2583{
8a745974 2584 /* process plugin before CPUs are created, but once -smp has been parsed */
0572f558 2585 qemu_plugin_load_list(&plugin_list, &error_fatal);
8a745974 2586
2f181fbd 2587 /* From here on we enter MACHINE_PHASE_INITIALIZED. */
26f88d84 2588 machine_run_board_init(current_machine, mem_path, &error_fatal);
d8e4de41 2589
fe9f70a1 2590 drive_check_orphaned();
d8e4de41
PB
2591
2592 realtime_init();
d8e4de41
PB
2593}
2594
2595static void qemu_create_cli_devices(void)
2596{
5dacda51
KW
2597 DeviceOption *opt;
2598
d8e4de41
PB
2599 soundhw_init();
2600
2601 qemu_opts_foreach(qemu_find_opts("fw_cfg"),
2602 parse_fw_cfg, fw_cfg_find(), &error_fatal);
2603
2604 /* init USB devices */
2605 if (machine_usb(current_machine)) {
2606 if (foreach_device_config(DEV_USB, usb_parse) < 0)
2607 exit(1);
2608 }
2609
2610 /* init generic devices */
2611 rom_set_order_override(FW_CFG_ORDER_OVERRIDE_DEVICE);
2612 qemu_opts_foreach(qemu_find_opts("device"),
2613 device_init_func, NULL, &error_fatal);
5dacda51 2614 QTAILQ_FOREACH(opt, &device_opts, next) {
64b4529a 2615 DeviceState *dev;
5dacda51
KW
2616 loc_push_restore(&opt->loc);
2617 /*
2618 * TODO Eventually we should call qmp_device_add() here to make sure it
2619 * behaves the same, but QMP still has to accept incorrectly typed
2620 * options until libvirt is fixed and we want to be strict on the CLI
2621 * from the start, so call qdev_device_add_from_qdict() directly for
2622 * now.
2623 */
64b4529a
DB
2624 dev = qdev_device_add_from_qdict(opt->opts, true, &error_fatal);
2625 object_unref(OBJECT(dev));
5dacda51
KW
2626 loc_pop(&opt->loc);
2627 }
d8e4de41
PB
2628 rom_reset_order_override();
2629}
2630
2631static void qemu_machine_creation_done(void)
2632{
abc27d42
DG
2633 MachineState *machine = MACHINE(qdev_get_machine());
2634
d8e4de41
PB
2635 /* Did we create any drives that we failed to create a device for? */
2636 drive_check_orphaned();
2637
2638 /* Don't warn about the default network setup that you get if
2639 * no command line -net or -netdev options are specified. There
2640 * are two cases that we would otherwise complain about:
2641 * (1) board doesn't support a NIC but the implicit "-net nic"
2642 * requested one
2643 * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
2644 * sets up a nic that isn't connected to anything.
2645 */
2646 if (!default_net && (!qtest_enabled() || has_defaults)) {
2647 net_check_clients();
2648 }
2649
cca686b4
PB
2650 qdev_prop_check_globals();
2651
d8e4de41
PB
2652 qdev_machine_creation_done();
2653
abc27d42
DG
2654 if (machine->cgs) {
2655 /*
2656 * Verify that Confidential Guest Support has actually been initialized
2657 */
2658 assert(machine->cgs->ready);
2659 }
2660
f66dc873 2661 if (foreach_device_config(DEV_GDB, gdbserver_start) < 0) {
d8e4de41
PB
2662 exit(1);
2663 }
f9bcb2d6
GA
2664 if (!vga_interface_created && !default_vga &&
2665 vga_interface_type != VGA_NONE) {
2666 warn_report("A -vga option was passed but this machine "
2667 "type does not use that option; "
2668 "No VGA device has been created");
2669 }
d8e4de41
PB
2670}
2671
b4e1a342
PB
2672void qmp_x_exit_preconfig(Error **errp)
2673{
2f181fbd 2674 if (phase_check(PHASE_MACHINE_INITIALIZED)) {
b4e1a342
PB
2675 error_setg(errp, "The command is permitted only before machine initialization");
2676 return;
2677 }
2678
2679 qemu_init_board();
2680 qemu_create_cli_devices();
2681 qemu_machine_creation_done();
2682
2683 if (loadvm) {
f9734d5d 2684 load_snapshot(loadvm, NULL, false, NULL, &error_fatal);
b4e1a342
PB
2685 }
2686 if (replay_mode != REPLAY_MODE_NONE) {
2687 replay_vmstate_init();
2688 }
2689
2690 if (incoming) {
2691 Error *local_err = NULL;
2692 if (strcmp(incoming, "defer") != 0) {
2693 qmp_migrate_incoming(incoming, &local_err);
2694 if (local_err) {
2695 error_reportf_err(local_err, "-incoming %s: ", incoming);
2696 exit(1);
2697 }
2698 }
2699 } else if (autostart) {
2700 qmp_cont(NULL);
2701 }
2702}
2703
bab6a301 2704void qemu_init(int argc, char **argv)
0824d6fc 2705{
7a84268d 2706 QemuOpts *opts;
d1cdd92e 2707 QemuOpts *icount_opts = NULL, *accel_opts = NULL;
03b0ba70 2708 QemuOptsList *olist;
cd6f1169 2709 int optind;
6530a97b 2710 const char *optarg;
0056ae24 2711 MachineClass *machine_class;
f29a5614 2712 bool userconfig = true;
abfd9ce3 2713 FILE *vmstate_dump_file = NULL;
0b5538c3 2714
4d454574 2715 qemu_add_opts(&qemu_drive_opts);
968854c8
AK
2716 qemu_add_drive_opts(&qemu_legacy_drive_opts);
2717 qemu_add_drive_opts(&qemu_common_drive_opts);
2718 qemu_add_drive_opts(&qemu_drive_opts);
c5f3014b 2719 qemu_add_drive_opts(&bdrv_runtime_opts);
4d454574
PB
2720 qemu_add_opts(&qemu_chardev_opts);
2721 qemu_add_opts(&qemu_device_opts);
2722 qemu_add_opts(&qemu_netdev_opts);
78cd6f7b 2723 qemu_add_opts(&qemu_nic_opts);
4d454574
PB
2724 qemu_add_opts(&qemu_net_opts);
2725 qemu_add_opts(&qemu_rtc_opts);
2726 qemu_add_opts(&qemu_global_opts);
2727 qemu_add_opts(&qemu_mon_opts);
2728 qemu_add_opts(&qemu_trace_opts);
42229a75 2729 qemu_plugin_add_opts();
4d454574 2730 qemu_add_opts(&qemu_option_rom_opts);
8d4e9146 2731 qemu_add_opts(&qemu_accel_opts);
6e1d3c1c 2732 qemu_add_opts(&qemu_mem_opts);
12b7f57e 2733 qemu_add_opts(&qemu_smp_opts);
4d454574 2734 qemu_add_opts(&qemu_boot_opts);
4d454574
PB
2735 qemu_add_opts(&qemu_add_fd_opts);
2736 qemu_add_opts(&qemu_object_opts);
d1a0cf73 2737 qemu_add_opts(&qemu_tpmdev_opts);
1fdd4748 2738 qemu_add_opts(&qemu_overcommit_opts);
5e2ac519 2739 qemu_add_opts(&qemu_msg_opts);
5d12f961 2740 qemu_add_opts(&qemu_name_opts);
0042109a 2741 qemu_add_opts(&qemu_numa_opts);
1ad9580b 2742 qemu_add_opts(&qemu_icount_opts);
a38bb079 2743 qemu_add_opts(&qemu_semihosting_config_opts);
81b2b810 2744 qemu_add_opts(&qemu_fw_cfg_opts);
2a5ad60b 2745 qemu_add_opts(&qemu_action_opts);
8a768db1 2746 qemu_add_run_with_opts();
34294e2f 2747 module_call_init(MODULE_INIT_OPTS);
4d454574 2748
efd7ab22
PB
2749 error_init(argv[0]);
2750 qemu_init_exec_dir(argv[0]);
142c6b1a 2751
c80cafa0 2752 qemu_init_arch_modules();
5ebbfecc 2753
efd7ab22 2754 qemu_init_subsystems();
41bd639b 2755
292444cb
AL
2756 /* first pass of option parsing */
2757 optind = 1;
2758 while (optind < argc) {
2759 if (argv[optind][0] != '-') {
2760 /* disk image */
28e68d68 2761 optind++;
292444cb
AL
2762 } else {
2763 const QEMUOption *popt;
2764
2765 popt = lookup_opt(argc, argv, &optarg, &optind);
2766 switch (popt->index) {
f29a5614
EH
2767 case QEMU_OPTION_nouserconfig:
2768 userconfig = false;
2769 break;
292444cb
AL
2770 }
2771 }
2772 }
2773
d8fb7d09 2774 machine_opts_dict = qdict_new();
1ea06c39 2775 if (userconfig) {
f7544edc 2776 qemu_read_default_config_file(&error_fatal);
292444cb
AL
2777 }
2778
2779 /* second pass of option parsing */
cd6f1169 2780 optind = 1;
0824d6fc 2781 for(;;) {
cd6f1169 2782 if (optind >= argc)
0824d6fc 2783 break;
6530a97b 2784 if (argv[optind][0] != '-') {
17f30eae 2785 loc_set_cmdline(argv, optind, 1);
d1cdd92e 2786 drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
cd6f1169
FB
2787 } else {
2788 const QEMUOption *popt;
2789
6530a97b 2790 popt = lookup_opt(argc, argv, &optarg, &optind);
ad96090a 2791 if (!(popt->arch_mask & arch_type)) {
da002526 2792 error_report("Option not supported for this target");
ad96090a
BS
2793 exit(1);
2794 }
cd6f1169 2795 switch(popt->index) {
94fc95cd
JM
2796 case QEMU_OPTION_cpu:
2797 /* hw initialization will check this */
c1c8cfe5 2798 cpu_option = optarg;
94fc95cd 2799 break;
cd6f1169 2800 case QEMU_OPTION_hda:
cd6f1169 2801 case QEMU_OPTION_hdb:
cc1daa40
FB
2802 case QEMU_OPTION_hdc:
2803 case QEMU_OPTION_hdd:
2292ddae
MA
2804 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2805 HD_OPTS);
fc01f7e7 2806 break;
42e5f393
MA
2807 case QEMU_OPTION_blockdev:
2808 {
2809 Visitor *v;
651af51c 2810 BlockdevOptionsQueueEntry *bdo;
42e5f393 2811
92917cd2
MA
2812 v = qobject_input_visitor_new_str(optarg, "driver",
2813 &error_fatal);
42e5f393 2814
651af51c 2815 bdo = g_new(BlockdevOptionsQueueEntry, 1);
42e5f393
MA
2816 visit_type_BlockdevOptions(v, NULL, &bdo->bdo,
2817 &error_fatal);
2818 visit_free(v);
2819 loc_save(&bdo->loc);
2820 QSIMPLEQ_INSERT_TAIL(&bdo_queue, bdo, entry);
2821 break;
2822 }
e4bcb14c 2823 case QEMU_OPTION_drive:
cc67f28e
EGE
2824 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"),
2825 optarg, false);
2826 if (opts == NULL) {
e2982c3a
MT
2827 exit(1);
2828 }
99efa84d 2829 break;
d058fe03 2830 case QEMU_OPTION_set:
c0d4aa82 2831 qemu_set_option(optarg, &error_fatal);
99efa84d 2832 break;
d0fef6fb
GH
2833 case QEMU_OPTION_global:
2834 if (qemu_global_option(optarg) != 0)
2835 exit(1);
99efa84d 2836 break;
3e3d5815 2837 case QEMU_OPTION_mtdblock:
2292ddae 2838 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
3e3d5815 2839 break;
a1bb27b1 2840 case QEMU_OPTION_sd:
80f4d9fc 2841 drive_add(IF_SD, -1, optarg, SD_OPTS);
a1bb27b1 2842 break;
86f55663 2843 case QEMU_OPTION_pflash:
2292ddae 2844 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
86f55663 2845 break;
cd6f1169 2846 case QEMU_OPTION_snapshot:
0ec8384f
MA
2847 snapshot = 1;
2848 replay_add_blocker("-snapshot");
33e3963e 2849 break;
268a362c 2850 case QEMU_OPTION_numa:
70b94331
MA
2851 opts = qemu_opts_parse_noisily(qemu_find_opts("numa"),
2852 optarg, true);
0042109a
WG
2853 if (!opts) {
2854 exit(1);
2855 }
268a362c 2856 break;
1472a95b 2857 case QEMU_OPTION_display:
e3af9f9a 2858 parse_display(optarg);
1472a95b 2859 break;
cd6f1169 2860 case QEMU_OPTION_nographic:
d8fb7d09 2861 qdict_put_str(machine_opts_dict, "graphics", "off");
cfc58cf3 2862 nographic = true;
0c8d7065 2863 dpy.type = DISPLAY_TYPE_NONE;
a20dd508 2864 break;
a171fe39 2865 case QEMU_OPTION_portrait:
9312805d
VK
2866 graphic_rotate = 90;
2867 break;
2868 case QEMU_OPTION_rotate:
2869 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2870 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2871 graphic_rotate != 180 && graphic_rotate != 270) {
f61eddcb 2872 error_report("only 90, 180, 270 deg rotation is available");
9312805d
VK
2873 exit(1);
2874 }
a171fe39 2875 break;
cd6f1169 2876 case QEMU_OPTION_kernel:
d8fb7d09 2877 qdict_put_str(machine_opts_dict, "kernel", optarg);
a0abe474
PM
2878 break;
2879 case QEMU_OPTION_initrd:
d8fb7d09 2880 qdict_put_str(machine_opts_dict, "initrd", optarg);
a20dd508 2881 break;
cd6f1169 2882 case QEMU_OPTION_append:
d8fb7d09 2883 qdict_put_str(machine_opts_dict, "append", optarg);
313aa567 2884 break;
412beee6 2885 case QEMU_OPTION_dtb:
d8fb7d09 2886 qdict_put_str(machine_opts_dict, "dtb", optarg);
412beee6 2887 break;
cd6f1169 2888 case QEMU_OPTION_cdrom:
2292ddae 2889 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
36b486bb 2890 break;
cd6f1169 2891 case QEMU_OPTION_boot:
8c4da4b5 2892 machine_parse_property_opt(qemu_find_opts("boot-opts"), "boot", optarg);
36b486bb 2893 break;
cd6f1169 2894 case QEMU_OPTION_fda:
cd6f1169 2895 case QEMU_OPTION_fdb:
2292ddae
MA
2896 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2897 optarg, FD_OPTS);
c45886db 2898 break;
52ca8d6a
FB
2899 case QEMU_OPTION_no_fd_bootchk:
2900 fd_bootchk = 0;
2901 break;
a1ea458f 2902 case QEMU_OPTION_netdev:
d30300f7 2903 default_net = 0;
f3eedcdd
LV
2904 if (netdev_is_modern(optarg)) {
2905 netdev_parse_modern(optarg);
2906 } else {
2907 net_client_parse(qemu_find_opts("netdev"), optarg);
2908 }
a1ea458f 2909 break;
78cd6f7b
TH
2910 case QEMU_OPTION_nic:
2911 default_net = 0;
21fccb2c 2912 net_client_parse(qemu_find_opts("nic"), optarg);
78cd6f7b 2913 break;
7c9d8e07 2914 case QEMU_OPTION_net:
d30300f7 2915 default_net = 0;
21fccb2c 2916 net_client_parse(qemu_find_opts("net"), optarg);
702c651c 2917 break;
f9dadc98
RS
2918#ifdef CONFIG_LIBISCSI
2919 case QEMU_OPTION_iscsi:
70b94331
MA
2920 opts = qemu_opts_parse_noisily(qemu_find_opts("iscsi"),
2921 optarg, false);
f9dadc98
RS
2922 if (!opts) {
2923 exit(1);
2924 }
2925 break;
c7f74643 2926#endif
71830221
KZ
2927 case QEMU_OPTION_audiodev:
2928 audio_parse_option(optarg);
2929 break;
039a6837 2930 case QEMU_OPTION_audio: {
5e03b6da 2931 bool help;
039a6837
PB
2932 char *model;
2933 Audiodev *dev = NULL;
2934 Visitor *v;
5e03b6da
CF
2935 QDict *dict = keyval_parse(optarg, "driver", &help, &error_fatal);
2936 if (help || (qdict_haskey(dict, "driver") &&
2937 is_help_option(qdict_get_str(dict, "driver")))) {
2938 audio_help();
2939 exit(EXIT_SUCCESS);
2940 }
039a6837
PB
2941 if (!qdict_haskey(dict, "id")) {
2942 qdict_put_str(dict, "id", "audiodev0");
2943 }
2944 if (!qdict_haskey(dict, "model")) {
2945 error_setg(&error_fatal, "Parameter 'model' is missing");
2946 }
2947 model = g_strdup(qdict_get_str(dict, "model"));
2948 qdict_del(dict, "model");
2949 if (is_help_option(model)) {
67aaa96a
PB
2950 show_valid_soundhw();
2951 exit(0);
2952 }
039a6837
PB
2953 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
2954 qobject_unref(dict);
2955 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2956 visit_free(v);
2957 audio_define(dev);
2958 select_soundhw(model, dev->id);
2959 g_free(model);
1d14ffa9 2960 break;
039a6837 2961 }
cd6f1169 2962 case QEMU_OPTION_h:
15f82208 2963 help(0);
cd6f1169 2964 break;
9bd7e6d9
PB
2965 case QEMU_OPTION_version:
2966 version();
2967 exit(0);
2968 break;
3b9985e9 2969 case QEMU_OPTION_m:
e12f0685
PB
2970 opts = qemu_opts_parse_noisily(qemu_find_opts("memory"), optarg, true);
2971 if (opts == NULL) {
2972 exit(1);
2973 }
cd6f1169 2974 break;
d1a0cf73
SB
2975#ifdef CONFIG_TPM
2976 case QEMU_OPTION_tpmdev:
2977 if (tpm_config_parse(qemu_find_opts("tpmdev"), optarg) < 0) {
2978 exit(1);
2979 }
2980 break;
2981#endif
c902760f
MT
2982 case QEMU_OPTION_mempath:
2983 mem_path = optarg;
2984 break;
c902760f
MT
2985 case QEMU_OPTION_mem_prealloc:
2986 mem_prealloc = 1;
2987 break;
cd6f1169 2988 case QEMU_OPTION_d:
c235d738
MF
2989 log_mask = optarg;
2990 break;
2991 case QEMU_OPTION_D:
2992 log_file = optarg;
cd6f1169 2993 break;
3514552e 2994 case QEMU_OPTION_DFILTER:
bd6fee9f 2995 qemu_set_dfilter_ranges(optarg, &error_fatal);
3514552e 2996 break;
5584e2db
IL
2997#if defined(CONFIG_TCG) && defined(CONFIG_LINUX)
2998 case QEMU_OPTION_perfmap:
2999 perf_enable_perfmap();
3000 break;
3001 case QEMU_OPTION_jitdump:
3002 perf_enable_jitdump();
3003 break;
3004#endif
9c09a251
RH
3005 case QEMU_OPTION_seed:
3006 qemu_guest_random_seed_main(optarg, &error_fatal);
3007 break;
cd6f1169 3008 case QEMU_OPTION_s:
ef0c4a0d 3009 add_device_config(DEV_GDB, "tcp::" DEFAULT_GDBSTUB_PORT);
cd6f1169 3010 break;
59030a8c 3011 case QEMU_OPTION_gdb:
ef0c4a0d 3012 add_device_config(DEV_GDB, optarg);
cd6f1169 3013 break;
cd6f1169 3014 case QEMU_OPTION_L:
37146e7e
RJ
3015 if (is_help_option(optarg)) {
3016 list_data_dirs = true;
2a1cce90 3017 } else {
ea1edcd7 3018 qemu_add_data_dir(g_strdup(optarg));
4524051c 3019 }
cd6f1169 3020 break;
1192dad8 3021 case QEMU_OPTION_bios:
d8fb7d09 3022 qdict_put_str(machine_opts_dict, "firmware", optarg);
1192dad8 3023 break;
1b530a6d 3024 case QEMU_OPTION_singlestep:
3cfb0456 3025 opt_one_insn_per_tb = true;
1b530a6d 3026 break;
cd6f1169 3027 case QEMU_OPTION_S:
3c07f8e8 3028 autostart = 0;
cd6f1169 3029 break;
99efa84d
MA
3030 case QEMU_OPTION_k:
3031 keyboard_layout = optarg;
3032 break;
3893c124 3033 case QEMU_OPTION_vga:
a369da5f 3034 vga_model = optarg;
7f1b17f2 3035 default_vga = 0;
1bfe856e 3036 break;
e9b137c2
FB
3037 case QEMU_OPTION_g:
3038 {
3039 const char *p;
3040 int w, h, depth;
3041 p = optarg;
3042 w = strtol(p, (char **)&p, 10);
3043 if (w <= 0) {
3044 graphic_error:
f61eddcb 3045 error_report("invalid resolution or depth");
e9b137c2
FB
3046 exit(1);
3047 }
3048 if (*p != 'x')
3049 goto graphic_error;
3050 p++;
3051 h = strtol(p, (char **)&p, 10);
3052 if (h <= 0)
3053 goto graphic_error;
3054 if (*p == 'x') {
3055 p++;
3056 depth = strtol(p, (char **)&p, 10);
8ac919a0
LV
3057 if (depth != 1 && depth != 2 && depth != 4 &&
3058 depth != 8 && depth != 15 && depth != 16 &&
e9b137c2
FB
3059 depth != 24 && depth != 32)
3060 goto graphic_error;
3061 } else if (*p == '\0') {
3062 depth = graphic_depth;
3063 } else {
3064 goto graphic_error;
3065 }
3b46e624 3066
e9b137c2
FB
3067 graphic_width = w;
3068 graphic_height = h;
3069 graphic_depth = depth;
3070 }
3071 break;
20d8a3ed
TS
3072 case QEMU_OPTION_echr:
3073 {
3074 char *r;
3075 term_escape_char = strtol(optarg, &r, 0);
3076 if (r == optarg)
3077 printf("Bad argument to echr\n");
3078 break;
3079 }
82c643ff 3080 case QEMU_OPTION_monitor:
6ca5582d 3081 default_monitor = 0;
70e098af 3082 if (strncmp(optarg, "none", 4)) {
4821cd4c 3083 monitor_parse(optarg, "readline", false);
70e098af 3084 }
6ca5582d
GH
3085 break;
3086 case QEMU_OPTION_qmp:
4821cd4c
HR
3087 monitor_parse(optarg, "control", false);
3088 default_monitor = 0;
3089 break;
3090 case QEMU_OPTION_qmp_pretty:
3091 monitor_parse(optarg, "control", true);
2d114dc1 3092 default_monitor = 0;
82c643ff 3093 break;
22a0e04b 3094 case QEMU_OPTION_mon:
70b94331
MA
3095 opts = qemu_opts_parse_noisily(qemu_find_opts("mon"), optarg,
3096 true);
22a0e04b 3097 if (!opts) {
22a0e04b
GH
3098 exit(1);
3099 }
2d114dc1 3100 default_monitor = 0;
22a0e04b 3101 break;
191bc01b 3102 case QEMU_OPTION_chardev:
70b94331
MA
3103 opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
3104 optarg, true);
191bc01b 3105 if (!opts) {
191bc01b
GH
3106 exit(1);
3107 }
191bc01b 3108 break;
74db920c 3109 case QEMU_OPTION_fsdev:
03b0ba70
GH
3110 olist = qemu_find_opts("fsdev");
3111 if (!olist) {
5dfdae81 3112 error_report("fsdev support is disabled");
03b0ba70
GH
3113 exit(1);
3114 }
70b94331 3115 opts = qemu_opts_parse_noisily(olist, optarg, true);
74db920c 3116 if (!opts) {
74db920c
GS
3117 exit(1);
3118 }
3119 break;
3d54abc7 3120 case QEMU_OPTION_virtfs: {
e14ea479
SH
3121 QemuOpts *fsdev;
3122 QemuOpts *device;
1a6ed33c
AM
3123 const char *writeout, *sock_fd, *socket, *path, *security_model,
3124 *multidevs;
3d54abc7 3125
03b0ba70
GH
3126 olist = qemu_find_opts("virtfs");
3127 if (!olist) {
5dfdae81 3128 error_report("virtfs support is disabled");
03b0ba70
GH
3129 exit(1);
3130 }
70b94331 3131 opts = qemu_opts_parse_noisily(olist, optarg, true);
3d54abc7 3132 if (!opts) {
3d54abc7
GS
3133 exit(1);
3134 }
3135
fbcbf101 3136 if (qemu_opt_get(opts, "fsdriver") == NULL ||
99519f0a 3137 qemu_opt_get(opts, "mount_tag") == NULL) {
8afb9000 3138 error_report("Usage: -virtfs fsdriver,mount_tag=tag");
9ce56db6
VJ
3139 exit(1);
3140 }
e14ea479 3141 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
3baa0a6a 3142 qemu_opts_id(opts) ?:
8be7e7e4
LC
3143 qemu_opt_get(opts, "mount_tag"),
3144 1, NULL);
e14ea479 3145 if (!fsdev) {
3baa0a6a 3146 error_report("duplicate or invalid fsdev id: %s",
f61eddcb 3147 qemu_opt_get(opts, "mount_tag"));
3d54abc7
GS
3148 exit(1);
3149 }
d3ab98e6
AK
3150
3151 writeout = qemu_opt_get(opts, "writeout");
3152 if (writeout) {
3153#ifdef CONFIG_SYNC_FILE_RANGE
f43e47db 3154 qemu_opt_set(fsdev, "writeout", writeout, &error_abort);
d3ab98e6 3155#else
f61eddcb
EH
3156 error_report("writeout=immediate not supported "
3157 "on this platform");
d3ab98e6
AK
3158 exit(1);
3159#endif
3160 }
f43e47db
MA
3161 qemu_opt_set(fsdev, "fsdriver",
3162 qemu_opt_get(opts, "fsdriver"), &error_abort);
32b69436
GK
3163 path = qemu_opt_get(opts, "path");
3164 if (path) {
3165 qemu_opt_set(fsdev, "path", path, &error_abort);
3166 }
3167 security_model = qemu_opt_get(opts, "security_model");
3168 if (security_model) {
3169 qemu_opt_set(fsdev, "security_model", security_model,
3170 &error_abort);
3171 }
84a87cc4
MK
3172 socket = qemu_opt_get(opts, "socket");
3173 if (socket) {
f43e47db 3174 qemu_opt_set(fsdev, "socket", socket, &error_abort);
84a87cc4 3175 }
4c793dda
MK
3176 sock_fd = qemu_opt_get(opts, "sock_fd");
3177 if (sock_fd) {
f43e47db 3178 qemu_opt_set(fsdev, "sock_fd", sock_fd, &error_abort);
4c793dda 3179 }
e14ea479 3180
2c74c2cb 3181 qemu_opt_set_bool(fsdev, "readonly",
cccb7967
MA
3182 qemu_opt_get_bool(opts, "readonly", 0),
3183 &error_abort);
1a6ed33c
AM
3184 multidevs = qemu_opt_get(opts, "multidevs");
3185 if (multidevs) {
3186 qemu_opt_set(fsdev, "multidevs", multidevs, &error_abort);
3187 }
87ea75d5
PC
3188 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0,
3189 &error_abort);
f43e47db 3190 qemu_opt_set(device, "driver", "virtio-9p-pci", &error_abort);
e14ea479 3191 qemu_opt_set(device, "fsdev",
3baa0a6a 3192 qemu_opts_id(fsdev), &error_abort);
e14ea479 3193 qemu_opt_set(device, "mount_tag",
f43e47db 3194 qemu_opt_get(opts, "mount_tag"), &error_abort);
3d54abc7
GS
3195 break;
3196 }
82c643ff 3197 case QEMU_OPTION_serial:
998bbd74
GH
3198 add_device_config(DEV_SERIAL, optarg);
3199 default_serial = 0;
18141ed6
JK
3200 if (strncmp(optarg, "mon:", 4) == 0) {
3201 default_monitor = 0;
3202 }
82c643ff 3203 break;
2a5ad60b
AJ
3204 case QEMU_OPTION_action:
3205 olist = qemu_find_opts("action");
3206 if (!qemu_opts_parse_noisily(olist, optarg, false)) {
3207 exit(1);
3208 }
3209 break;
22afb46e 3210 case QEMU_OPTION_watchdog_action: {
22afb46e
PB
3211 opts = qemu_opts_create(qemu_find_opts("action"), NULL, 0, &error_abort);
3212 qemu_opt_set(opts, "watchdog", optarg, &error_abort);
9dd986cc 3213 break;
22afb46e 3214 }
6508fe59 3215 case QEMU_OPTION_parallel:
6a5e8b0e
GH
3216 add_device_config(DEV_PARALLEL, optarg);
3217 default_parallel = 0;
18141ed6
JK
3218 if (strncmp(optarg, "mon:", 4) == 0) {
3219 default_monitor = 0;
3220 }
6508fe59 3221 break;
c9f398e5
PA
3222 case QEMU_OPTION_debugcon:
3223 add_device_config(DEV_DEBUGCON, optarg);
3224 break;
99efa84d
MA
3225 case QEMU_OPTION_loadvm:
3226 loadvm = optarg;
3227 break;
d63d307f 3228 case QEMU_OPTION_full_screen:
0c8d7065
GH
3229 dpy.has_full_screen = true;
3230 dpy.full_screen = true;
d63d307f 3231 break;
f7cce898 3232 case QEMU_OPTION_pidfile:
93815bc2 3233 pid_file = optarg;
f7cce898 3234 break;
a09db21f
FB
3235 case QEMU_OPTION_win2k_hack:
3236 win2k_install_hack = 1;
3237 break;
8a92ea2f 3238 case QEMU_OPTION_acpitable:
70b94331
MA
3239 opts = qemu_opts_parse_noisily(qemu_find_opts("acpi"),
3240 optarg, true);
f46e720a
LE
3241 if (!opts) {
3242 exit(1);
3243 }
9f57061c 3244 acpi_table_add(opts, &error_fatal);
8a92ea2f 3245 break;
b6f6e3d3 3246 case QEMU_OPTION_smbios:
70b94331
MA
3247 opts = qemu_opts_parse_noisily(qemu_find_opts("smbios"),
3248 optarg, false);
f46e720a
LE
3249 if (!opts) {
3250 exit(1);
3251 }
1007a37e 3252 smbios_entry_add(opts, &error_fatal);
b6f6e3d3 3253 break;
81b2b810 3254 case QEMU_OPTION_fwcfg:
70b94331
MA
3255 opts = qemu_opts_parse_noisily(qemu_find_opts("fw_cfg"),
3256 optarg, true);
81b2b810
GS
3257 if (opts == NULL) {
3258 exit(1);
3259 }
3260 break;
047f7038 3261 case QEMU_OPTION_preconfig:
7691bdef 3262 preconfig_requested = true;
047f7038 3263 break;
7ba1e619 3264 case QEMU_OPTION_enable_kvm:
d8fb7d09 3265 qdict_put_str(machine_opts_dict, "accel", "kvm");
303d4e86 3266 break;
364c3e6b 3267 case QEMU_OPTION_M:
303d4e86 3268 case QEMU_OPTION_machine:
d8fb7d09
PB
3269 {
3270 bool help;
3271
3272 keyval_parse_into(machine_opts_dict, optarg, "type", &help, &error_fatal);
3273 if (help) {
3274 machine_help_func(machine_opts_dict);
3275 exit(EXIT_SUCCESS);
3276 }
3277 break;
303d4e86 3278 }
a20fa79f 3279 case QEMU_OPTION_accel:
8d4e9146
FK
3280 accel_opts = qemu_opts_parse_noisily(qemu_find_opts("accel"),
3281 optarg, true);
3282 optarg = qemu_opt_get(accel_opts, "accel");
bde4d920 3283 if (!optarg || is_help_option(optarg)) {
cbe6d636
WSM
3284 printf("Accelerators supported in QEMU binary:\n");
3285 GSList *el, *accel_list = object_class_get_list(TYPE_ACCEL,
3286 false);
3287 for (el = accel_list; el; el = el->next) {
3288 gchar *typename = g_strdup(object_class_get_name(
3289 OBJECT_CLASS(el->data)));
3290 /* omit qtest which is used for tests only */
3291 if (g_strcmp0(typename, ACCEL_CLASS_NAME("qtest")) &&
3292 g_str_has_suffix(typename, ACCEL_CLASS_SUFFIX)) {
3293 gchar **optname = g_strsplit(typename,
3294 ACCEL_CLASS_SUFFIX, 0);
3295 printf("%s\n", optname[0]);
4903602e 3296 g_strfreev(optname);
cbe6d636
WSM
3297 }
3298 g_free(typename);
3299 }
3300 g_slist_free(accel_list);
a20fa79f 3301 exit(0);
8d4e9146
FK
3302 }
3303 break;
bb36d470 3304 case QEMU_OPTION_usb:
d8fb7d09 3305 qdict_put_str(machine_opts_dict, "usb", "on");
bb36d470 3306 break;
a594cfbf 3307 case QEMU_OPTION_usbdevice:
d8fb7d09 3308 qdict_put_str(machine_opts_dict, "usb", "on");
bd3c948d
GH
3309 add_device_config(DEV_USB, optarg);
3310 break;
3311 case QEMU_OPTION_device:
5dacda51
KW
3312 if (optarg[0] == '{') {
3313 QObject *obj = qobject_from_json(optarg, &error_fatal);
3314 DeviceOption *opt = g_new0(DeviceOption, 1);
3315 opt->opts = qobject_to(QDict, obj);
3316 loc_save(&opt->loc);
3317 assert(opt->opts != NULL);
3318 QTAILQ_INSERT_TAIL(&device_opts, opt, next);
3319 } else {
3320 if (!qemu_opts_parse_noisily(qemu_find_opts("device"),
3321 optarg, true)) {
3322 exit(1);
3323 }
f31d07d1 3324 }
a594cfbf 3325 break;
6a00d601 3326 case QEMU_OPTION_smp:
f9dfae9c
MA
3327 machine_parse_property_opt(qemu_find_opts("smp-opts"),
3328 "smp", optarg);
6a00d601 3329 break;
99efa84d 3330 case QEMU_OPTION_vnc:
653c9747 3331 vnc_parse(optarg);
821601ea 3332 break;
6515b203 3333 case QEMU_OPTION_no_acpi:
fffa36b6 3334 warn_report("-no-acpi is deprecated, use '-machine acpi=off' instead");
d8fb7d09 3335 qdict_put_str(machine_opts_dict, "acpi", "off");
6515b203 3336 break;
16b29ae1 3337 case QEMU_OPTION_no_hpet:
df37330c 3338 warn_report("-no-hpet is deprecated, use '-machine hpet=off' instead");
d8fb7d09 3339 qdict_put_str(machine_opts_dict, "hpet", "off");
16b29ae1 3340 break;
d1beab82 3341 case QEMU_OPTION_no_reboot:
2a5ad60b
AJ
3342 olist = qemu_find_opts("action");
3343 qemu_opts_parse_noisily(olist, "reboot=shutdown", false);
d1beab82 3344 break;
b2f76161 3345 case QEMU_OPTION_no_shutdown:
2a5ad60b 3346 olist = qemu_find_opts("action");
c27025e0 3347 qemu_opts_parse_noisily(olist, "shutdown=pause", false);
b2f76161 3348 break;
8fcb1b90 3349 case QEMU_OPTION_uuid:
9c5ce8db 3350 if (qemu_uuid_parse(optarg, &qemu_uuid) < 0) {
4cd70f34 3351 error_report("failed to parse UUID string: wrong format");
8fcb1b90
BS
3352 exit(1);
3353 }
fc3b3295 3354 qemu_uuid_set = true;
8fcb1b90 3355 break;
99efa84d
MA
3356 case QEMU_OPTION_option_rom:
3357 if (nb_option_roms >= MAX_OPTION_ROMS) {
3e515373 3358 error_report("too many option ROMs");
99efa84d
MA
3359 exit(1);
3360 }
70b94331
MA
3361 opts = qemu_opts_parse_noisily(qemu_find_opts("option-rom"),
3362 optarg, true);
49295ebc
MA
3363 if (!opts) {
3364 exit(1);
3365 }
2e55e842
GN
3366 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
3367 option_rom[nb_option_roms].bootindex =
3368 qemu_opt_get_number(opts, "bootindex", -1);
3369 if (!option_rom[nb_option_roms].name) {
f61eddcb 3370 error_report("Option ROM file is not specified");
2e55e842
GN
3371 exit(1);
3372 }
99efa84d
MA
3373 nb_option_roms++;
3374 break;
8e71621f 3375 case QEMU_OPTION_semihosting:
f1672e6f 3376 qemu_semihosting_enable();
a38bb079
LI
3377 break;
3378 case QEMU_OPTION_semihosting_config:
f1672e6f 3379 if (qemu_semihosting_config_options(optarg) != 0) {
a38bb079
LI
3380 exit(1);
3381 }
8e71621f 3382 break;
c35734b2 3383 case QEMU_OPTION_name:
70b94331
MA
3384 opts = qemu_opts_parse_noisily(qemu_find_opts("name"),
3385 optarg, true);
5d12f961
DDAG
3386 if (!opts) {
3387 exit(1);
3388 }
2880ffb0
MS
3389 /* Capture guest name if -msg guest-name is used later */
3390 error_guest_name = qemu_opt_get(opts, "guest");
c35734b2 3391 break;
66508601
BS
3392 case QEMU_OPTION_prom_env:
3393 if (nb_prom_envs >= MAX_PROM_ENVS) {
3e515373 3394 error_report("too many prom variables");
66508601
BS
3395 exit(1);
3396 }
3397 prom_envs[nb_prom_envs] = optarg;
3398 nb_prom_envs++;
3399 break;
2b8f2d41
AZ
3400 case QEMU_OPTION_old_param:
3401 old_param = 1;
05ebd537 3402 break;
1ed2fc1f 3403 case QEMU_OPTION_rtc:
70b94331
MA
3404 opts = qemu_opts_parse_noisily(qemu_find_opts("rtc"), optarg,
3405 false);
1ed2fc1f 3406 if (!opts) {
1ed2fc1f 3407 exit(1);
7e0af5d0
FB
3408 }
3409 break;
2e70f6ef 3410 case QEMU_OPTION_icount:
70b94331
MA
3411 icount_opts = qemu_opts_parse_noisily(qemu_find_opts("icount"),
3412 optarg, true);
1ad9580b
ST
3413 if (!icount_opts) {
3414 exit(1);
3415 }
2e70f6ef 3416 break;
5bb7910a 3417 case QEMU_OPTION_incoming:
7c76235a
DDAG
3418 if (!incoming) {
3419 runstate_set(RUN_STATE_INMIGRATE);
3420 }
5bb7910a
AL
3421 incoming = optarg;
3422 break;
d15c05fc 3423 case QEMU_OPTION_only_migratable:
811f8652 3424 only_migratable = 1;
d15c05fc 3425 break;
d8c208dd 3426 case QEMU_OPTION_nodefaults:
d44229c5 3427 has_defaults = 0;
d8c208dd 3428 break;
e37630ca 3429 case QEMU_OPTION_xen_domid:
7058fa02 3430 if (!(accel_find("xen")) && !(accel_find("kvm"))) {
da002526 3431 error_report("Option not supported for this target");
ad96090a
BS
3432 exit(1);
3433 }
e37630ca
AL
3434 xen_domid = atoi(optarg);
3435 break;
e37630ca 3436 case QEMU_OPTION_xen_attach:
6773fbf8 3437 if (!(accel_find("xen"))) {
da002526 3438 error_report("Option not supported for this target");
ad96090a
BS
3439 exit(1);
3440 }
e37630ca
AL
3441 xen_mode = XEN_ATTACH;
3442 break;
1c599472 3443 case QEMU_OPTION_xen_domid_restrict:
6773fbf8 3444 if (!(accel_find("xen"))) {
1c599472
PD
3445 error_report("Option not supported for this target");
3446 exit(1);
3447 }
3448 xen_domid_restrict = true;
3449 break;
ab6540d5 3450 case QEMU_OPTION_trace:
92eecfff 3451 trace_opt_parse(optarg);
ab6540d5 3452 break;
42229a75
LV
3453 case QEMU_OPTION_plugin:
3454 qemu_plugin_opt_parse(optarg, &plugin_list);
3455 break;
715a664a 3456 case QEMU_OPTION_readconfig:
c0d4aa82 3457 qemu_read_config_file(optarg, qemu_parse_config_group, &error_fatal);
f7544edc 3458 break;
5324e3e9 3459#ifdef CONFIG_SPICE
29b0040b 3460 case QEMU_OPTION_spice:
9ed345a1 3461 olist = qemu_find_opts_err("spice", NULL);
29b0040b 3462 if (!olist) {
5dfdae81 3463 error_report("spice support is disabled");
29b0040b
GH
3464 exit(1);
3465 }
70b94331 3466 opts = qemu_opts_parse_noisily(olist, optarg, false);
29b0040b 3467 if (!opts) {
29b0040b
GH
3468 exit(1);
3469 }
f963e4d0 3470 display_remote++;
29b0040b 3471 break;
5324e3e9 3472#endif
c7f0f3b1
AL
3473 case QEMU_OPTION_qtest:
3474 qtest_chrdev = optarg;
3475 break;
3476 case QEMU_OPTION_qtest_log:
3477 qtest_log = optarg;
3478 break;
7d76ad4f 3479 case QEMU_OPTION_sandbox:
2bb814a4
MAL
3480 olist = qemu_find_opts("sandbox");
3481 if (!olist) {
3482#ifndef CONFIG_SECCOMP
3483 error_report("-sandbox support is not enabled "
3484 "in this QEMU binary");
3485#endif
3486 exit(1);
3487 }
3488
3489 opts = qemu_opts_parse_noisily(olist, optarg, true);
7d76ad4f 3490 if (!opts) {
49295ebc 3491 exit(1);
7d76ad4f
EO
3492 }
3493 break;
587ed6be
CB
3494 case QEMU_OPTION_add_fd:
3495#ifndef _WIN32
70b94331
MA
3496 opts = qemu_opts_parse_noisily(qemu_find_opts("add-fd"),
3497 optarg, false);
587ed6be 3498 if (!opts) {
49295ebc 3499 exit(1);
587ed6be
CB
3500 }
3501#else
3502 error_report("File descriptor passing is disabled on this "
3503 "platform");
3504 exit(1);
3505#endif
3506 break;
68d98d3e 3507 case QEMU_OPTION_object:
bc2f4fcb 3508 object_option_parse(optarg);
68d98d3e 3509 break;
6f131f13
MT
3510 case QEMU_OPTION_overcommit:
3511 opts = qemu_opts_parse_noisily(qemu_find_opts("overcommit"),
3512 optarg, false);
3513 if (!opts) {
3514 exit(1);
3515 }
c8c9dc42 3516 enable_mlock = qemu_opt_get_bool(opts, "mem-lock", false);
6f131f13 3517 enable_cpu_pm = qemu_opt_get_bool(opts, "cpu-pm", false);
888a6bc6 3518 break;
6dd75472
MA
3519 case QEMU_OPTION_compat:
3520 {
c431ffd4 3521 CompatPolicy *opts_policy;
6dd75472
MA
3522 Visitor *v;
3523
3524 v = qobject_input_visitor_new_str(optarg, NULL,
3525 &error_fatal);
3526
c431ffd4
PB
3527 visit_type_CompatPolicy(v, NULL, &opts_policy, &error_fatal);
3528 QAPI_CLONE_MEMBERS(CompatPolicy, &compat_policy, opts_policy);
6dd75472 3529
c431ffd4 3530 qapi_free_CompatPolicy(opts_policy);
6dd75472
MA
3531 visit_free(v);
3532 break;
3533 }
5e2ac519 3534 case QEMU_OPTION_msg:
70b94331
MA
3535 opts = qemu_opts_parse_noisily(qemu_find_opts("msg"), optarg,
3536 false);
5e2ac519
SA
3537 if (!opts) {
3538 exit(1);
3539 }
3540 configure_msg(opts);
3541 break;
abfd9ce3 3542 case QEMU_OPTION_dump_vmstate:
522abf69 3543 if (vmstate_dump_file) {
f61eddcb
EH
3544 error_report("only one '-dump-vmstate' "
3545 "option may be given");
522abf69
GA
3546 exit(1);
3547 }
abfd9ce3
AS
3548 vmstate_dump_file = fopen(optarg, "w");
3549 if (vmstate_dump_file == NULL) {
f61eddcb 3550 error_report("open %s: %s", optarg, strerror(errno));
abfd9ce3
AS
3551 exit(1);
3552 }
12df189d
EC
3553 break;
3554 case QEMU_OPTION_enable_sync_profile:
3555 qsp_enable();
abfd9ce3 3556 break;
aec0d0e1
MP
3557 case QEMU_OPTION_nouserconfig:
3558 /* Nothing to be parsed here. Especially, do not error out below. */
3559 break;
8a768db1
MT
3560#if defined(CONFIG_POSIX)
3561 case QEMU_OPTION_runas:
3562 if (!os_set_runas(optarg)) {
3563 error_report("User \"%s\" doesn't exist"
3564 " (and is not <uid>:<gid>)",
3565 optarg);
1217d6ca
TH
3566 exit(1);
3567 }
8a768db1
MT
3568 break;
3569 case QEMU_OPTION_chroot:
3570 warn_report("option is deprecated,"
3571 " use '-run-with chroot=...' instead");
3572 os_set_chroot(optarg);
3573 break;
3574 case QEMU_OPTION_daemonize:
3575 os_set_daemonize(true);
3576 break;
3577#if defined(CONFIG_LINUX)
3578 /* deprecated */
3579 case QEMU_OPTION_asyncteardown:
3580 init_async_teardown();
3581 break;
3582#endif
3583 case QEMU_OPTION_run_with: {
3584 const char *str;
3585 opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
3586 optarg, false);
3587 if (!opts) {
1217d6ca
TH
3588 exit(1);
3589 }
8a768db1
MT
3590#if defined(CONFIG_LINUX)
3591 if (qemu_opt_get_bool(opts, "async-teardown", false)) {
3592 init_async_teardown();
3593 }
3594#endif
3595 str = qemu_opt_get(opts, "chroot");
3596 if (str) {
3597 os_set_chroot(str);
3598 }
3599 break;
3600 }
3601#endif /* CONFIG_POSIX */
3602
3603 default:
3604 error_report("Option not supported in this build");
3605 exit(1);
cd6f1169 3606 }
0824d6fc
FB
3607 }
3608 }
43fa1e0b
EH
3609 /*
3610 * Clear error location left behind by the loop.
3611 * Best done right after the loop. Do not insert code here!
3612 */
3613 loc_set_none();
364c3e6b 3614
d8fb7d09 3615 qemu_validate_options(machine_opts_dict);
4d2c17b0 3616 qemu_process_sugar_options();
58c91595 3617
3df261b6 3618 /*
0546c060
PB
3619 * These options affect everything else and should be processed
3620 * before daemonizing.
3df261b6 3621 */
0546c060 3622 qemu_process_early_options();
3df261b6 3623
0546c060
PB
3624 qemu_process_help_options();
3625 qemu_maybe_daemonize(pid_file);
3626
10b6ee16
DHB
3627 /*
3628 * The trace backend must be initialized after daemonizing.
3629 * trace_init_backends() will call st_init(), which will create the
3630 * trace thread in the parent, and also register st_flush_trace_buffer()
3631 * in atexit(). This function will force the parent to wait for the
3632 * writeout thread to finish, which will not occur, and the parent
3633 * process will be left in the host.
3634 */
3635 if (!trace_init_backends()) {
3636 exit(1);
3637 }
3638 trace_init_file();
3639
efd7ab22
PB
3640 qemu_init_main_loop(&error_fatal);
3641 cpu_timers_init();
3df261b6 3642
fc4a4734 3643 user_register_global_props();
4c27b859
PD
3644 replay_configure(icount_opts);
3645
eb6a5209
AP
3646 configure_rtc(qemu_find_opts_singleton("rtc"));
3647
e12f0685
PB
3648 /* Transfer QemuOpts options into machine options */
3649 parse_memory_options();
3650
d8fb7d09 3651 qemu_create_machine(machine_opts_dict);
67b724e6 3652
5a1ee607
PB
3653 suspend_mux_open();
3654
f650266b
PB
3655 qemu_disable_default_devices();
3656 qemu_create_default_devices();
64418657 3657 qemu_create_early_backends();
cda4aa9a 3658
d8fb7d09
PB
3659 qemu_apply_legacy_machine_options(machine_opts_dict);
3660 qemu_apply_machine_options(machine_opts_dict);
3661 qobject_unref(machine_opts_dict);
2f181fbd 3662 phase_advance(PHASE_MACHINE_CREATED);
6b1b1440 3663
0427b625
MA
3664 /*
3665 * Note: uses machine properties such as kernel-irqchip, must run
d8fb7d09 3666 * after qemu_apply_machine_options.
0427b625 3667 */
28a09617 3668 configure_accelerators(argv[0]);
2f181fbd 3669 phase_advance(PHASE_ACCEL_CREATED);
214910a7 3670
0427b625
MA
3671 /*
3672 * Beware, QOM objects created before this point miss global and
3673 * compat properties.
3674 *
3675 * Global properties get set up by qdev_prop_register_global(),
3676 * called from user_register_global_props(), and certain option
3677 * desugaring. Also in CPU feature desugaring (buried in
c1c8cfe5 3678 * parse_cpu_option()), which happens below this point, but may
0427b625 3679 * only target the CPU type, which can only be created after
c1c8cfe5 3680 * parse_cpu_option() returned the type.
0427b625
MA
3681 *
3682 * Machine compat properties: object_set_machine_compat_props().
3683 * Accelerator compat props: object_set_accelerator_compat_props(),
b86f59c7 3684 * called from do_configure_accelerator().
0427b625
MA
3685 */
3686
f5c9fcb8 3687 machine_class = MACHINE_GET_CLASS(current_machine);
08fe6824 3688 if (!qtest_enabled() && machine_class->deprecation_reason) {
7adb75d6 3689 warn_report("Machine type '%s' is deprecated: %s",
08fe6824
TH
3690 machine_class->name, machine_class->deprecation_reason);
3691 }
3692
cb9d8b8c
PX
3693 /*
3694 * Create backends before creating migration objects, so that it can
3695 * check against compatibilities on the backend memories (e.g. postcopy
3696 * over memory-backend-file objects).
3697 */
3698 qemu_create_late_backends();
3699
2fa23277 3700 /*
0427b625
MA
3701 * Note: creates a QOM object, must run only after global and
3702 * compat properties have been set up.
2fa23277
MA
3703 */
3704 migration_object_init();
3705
6063d4c0 3706 /* parse features once if machine provides default cpu_type */
2278b939 3707 current_machine->cpu_type = machine_class->default_cpu_type;
c1c8cfe5
EH
3708 if (cpu_option) {
3709 current_machine->cpu_type = parse_cpu_option(cpu_option);
6063d4c0 3710 }
b86f59c7 3711 /* NB: for machine none cpu_type could STILL be NULL here! */
a1b18df9 3712
b24986e7 3713 qemu_resolve_machine_memdev();
d342eb76 3714 parse_numa_opts(current_machine);
6063d4c0 3715
abfd9ce3
AS
3716 if (vmstate_dump_file) {
3717 /* dump and exit */
4067691a 3718 module_load_qom_all();
abfd9ce3 3719 dump_vmstate_json_to_file(vmstate_dump_file);
7b733862 3720 exit(0);
abfd9ce3 3721 }
ffd843bc 3722
b4e1a342
PB
3723 if (!preconfig_requested) {
3724 qmp_x_exit_preconfig(&error_fatal);
3725 }
facf7c60 3726 qemu_init_displays();
7a64c17f 3727 accel_setup_post(current_machine);
eb505be1 3728 os_setup_post();
5a1ee607 3729 resume_mux_open();
7b733862 3730}