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