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