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