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