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