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