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