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