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