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