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