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