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