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