1 /* 2 * bootloader support 3 * 4 * Copyright IBM, Corp. 2012, 2020 5 * 6 * Authors: 7 * Christian Borntraeger <borntraeger@de.ibm.com> 8 * Janosch Frank <frankja@linux.ibm.com> 9 * 10 * This work is licensed under the terms of the GNU GPL, version 2 or (at your 11 * option) any later version. See the COPYING file in the top-level directory. 12 * 13 */ 14 15 #include "qemu/osdep.h" 16 #include "qemu/datadir.h" 17 #include "qapi/error.h" 18 #include "sysemu/reset.h" 19 #include "sysemu/runstate.h" 20 #include "sysemu/tcg.h" 21 #include "elf.h" 22 #include "hw/loader.h" 23 #include "hw/qdev-properties.h" 24 #include "hw/boards.h" 25 #include "hw/s390x/virtio-ccw.h" 26 #include "hw/s390x/vfio-ccw.h" 27 #include "hw/s390x/css.h" 28 #include "hw/s390x/ebcdic.h" 29 #include "target/s390x/kvm/pv.h" 30 #include "hw/scsi/scsi.h" 31 #include "hw/virtio/virtio-net.h" 32 #include "ipl.h" 33 #include "qemu/error-report.h" 34 #include "qemu/config-file.h" 35 #include "qemu/cutils.h" 36 #include "qemu/option.h" 37 #include "standard-headers/linux/virtio_ids.h" 38 39 #define KERN_IMAGE_START 0x010000UL 40 #define LINUX_MAGIC_ADDR 0x010008UL 41 #define KERN_PARM_AREA_SIZE_ADDR 0x010430UL 42 #define KERN_PARM_AREA 0x010480UL 43 #define LEGACY_KERN_PARM_AREA_SIZE 0x000380UL 44 #define INITRD_START 0x800000UL 45 #define INITRD_PARM_START 0x010408UL 46 #define PARMFILE_START 0x001000UL 47 #define ZIPL_IMAGE_START 0x009000UL 48 #define BIOS_MAX_SIZE 0x300000UL 49 #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64) 50 51 static bool iplb_extended_needed(void *opaque) 52 { 53 S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL)); 54 55 return ipl->iplbext_migration; 56 } 57 58 static const VMStateDescription vmstate_iplb_extended = { 59 .name = "ipl/iplb_extended", 60 .version_id = 0, 61 .minimum_version_id = 0, 62 .needed = iplb_extended_needed, 63 .fields = (const VMStateField[]) { 64 VMSTATE_UINT8_ARRAY(reserved_ext, IplParameterBlock, 4096 - 200), 65 VMSTATE_END_OF_LIST() 66 } 67 }; 68 69 static const VMStateDescription vmstate_iplb = { 70 .name = "ipl/iplb", 71 .version_id = 0, 72 .minimum_version_id = 0, 73 .fields = (const VMStateField[]) { 74 VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110), 75 VMSTATE_UINT16(devno, IplParameterBlock), 76 VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88), 77 VMSTATE_END_OF_LIST() 78 }, 79 .subsections = (const VMStateDescription * const []) { 80 &vmstate_iplb_extended, 81 NULL 82 } 83 }; 84 85 static const VMStateDescription vmstate_ipl = { 86 .name = "ipl", 87 .version_id = 0, 88 .minimum_version_id = 0, 89 .fields = (const VMStateField[]) { 90 VMSTATE_UINT64(compat_start_addr, S390IPLState), 91 VMSTATE_UINT64(compat_bios_start_addr, S390IPLState), 92 VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock), 93 VMSTATE_BOOL(iplb_valid, S390IPLState), 94 VMSTATE_UINT8(cssid, S390IPLState), 95 VMSTATE_UINT8(ssid, S390IPLState), 96 VMSTATE_UINT16(devno, S390IPLState), 97 VMSTATE_END_OF_LIST() 98 } 99 }; 100 101 static S390IPLState *get_ipl_device(void) 102 { 103 return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL)); 104 } 105 106 static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr) 107 { 108 uint64_t dstaddr = *(uint64_t *) opaque; 109 /* 110 * Assuming that our s390-ccw.img was linked for starting at address 0, 111 * we can simply add the destination address for the final location 112 */ 113 return srcaddr + dstaddr; 114 } 115 116 static uint64_t get_max_kernel_cmdline_size(void) 117 { 118 uint64_t *size_ptr = rom_ptr(KERN_PARM_AREA_SIZE_ADDR, sizeof(*size_ptr)); 119 120 if (size_ptr) { 121 uint64_t size; 122 123 size = be64_to_cpu(*size_ptr); 124 if (size) { 125 return size; 126 } 127 } 128 return LEGACY_KERN_PARM_AREA_SIZE; 129 } 130 131 static void s390_ipl_realize(DeviceState *dev, Error **errp) 132 { 133 MachineState *ms = MACHINE(qdev_get_machine()); 134 S390IPLState *ipl = S390_IPL(dev); 135 uint32_t *ipl_psw; 136 uint64_t pentry; 137 char *magic; 138 int kernel_size; 139 140 int bios_size; 141 char *bios_filename; 142 143 /* 144 * Always load the bios if it was enforced, 145 * even if an external kernel has been defined. 146 */ 147 if (!ipl->kernel || ipl->enforce_bios) { 148 uint64_t fwbase; 149 150 if (ms->ram_size < BIOS_MAX_SIZE) { 151 error_setg(errp, "not enough RAM to load the BIOS file"); 152 return; 153 } 154 155 fwbase = (MIN(ms->ram_size, 0x80000000U) - BIOS_MAX_SIZE) & ~0xffffUL; 156 157 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->firmware); 158 if (bios_filename == NULL) { 159 error_setg(errp, "could not find stage1 bootloader"); 160 return; 161 } 162 163 bios_size = load_elf(bios_filename, NULL, 164 bios_translate_addr, &fwbase, 165 &ipl->bios_start_addr, NULL, NULL, NULL, 1, 166 EM_S390, 0, 0); 167 if (bios_size > 0) { 168 /* Adjust ELF start address to final location */ 169 ipl->bios_start_addr += fwbase; 170 } else { 171 /* Try to load non-ELF file */ 172 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START, 173 4096); 174 ipl->bios_start_addr = ZIPL_IMAGE_START; 175 } 176 g_free(bios_filename); 177 178 if (bios_size == -1) { 179 error_setg(errp, "could not load bootloader '%s'", ipl->firmware); 180 return; 181 } 182 183 /* default boot target is the bios */ 184 ipl->start_addr = ipl->bios_start_addr; 185 } 186 187 if (ipl->kernel) { 188 kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL, 189 &pentry, NULL, 190 NULL, NULL, 1, EM_S390, 0, 0); 191 if (kernel_size < 0) { 192 kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size); 193 if (kernel_size < 0) { 194 error_setg(errp, "could not load kernel '%s'", ipl->kernel); 195 return; 196 } 197 /* if this is Linux use KERN_IMAGE_START */ 198 magic = rom_ptr(LINUX_MAGIC_ADDR, 6); 199 if (magic && !memcmp(magic, "S390EP", 6)) { 200 pentry = KERN_IMAGE_START; 201 } else { 202 /* if not Linux load the address of the (short) IPL PSW */ 203 ipl_psw = rom_ptr(4, 4); 204 if (ipl_psw) { 205 pentry = be32_to_cpu(*ipl_psw) & PSW_MASK_SHORT_ADDR; 206 } else { 207 error_setg(errp, "Could not get IPL PSW"); 208 return; 209 } 210 } 211 } 212 /* 213 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the 214 * kernel parameters here as well. Note: For old kernels (up to 3.2) 215 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL 216 * loader) and it won't work. For this case we force it to 0x10000, too. 217 */ 218 if (pentry == KERN_IMAGE_START || pentry == 0x800) { 219 size_t cmdline_size = strlen(ipl->cmdline) + 1; 220 char *parm_area = rom_ptr(KERN_PARM_AREA, cmdline_size); 221 222 ipl->start_addr = KERN_IMAGE_START; 223 /* Overwrite parameters in the kernel image, which are "rom" */ 224 if (parm_area) { 225 uint64_t max_cmdline_size = get_max_kernel_cmdline_size(); 226 227 if (cmdline_size > max_cmdline_size) { 228 error_setg(errp, 229 "kernel command line exceeds maximum size:" 230 " %zu > %" PRIu64, 231 cmdline_size, max_cmdline_size); 232 return; 233 } 234 235 strcpy(parm_area, ipl->cmdline); 236 } 237 } else { 238 ipl->start_addr = pentry; 239 } 240 241 if (ipl->initrd) { 242 ram_addr_t initrd_offset; 243 int initrd_size; 244 uint64_t *romptr; 245 246 initrd_offset = INITRD_START; 247 while (kernel_size + 0x100000 > initrd_offset) { 248 initrd_offset += 0x100000; 249 } 250 initrd_size = load_image_targphys(ipl->initrd, initrd_offset, 251 ms->ram_size - initrd_offset); 252 if (initrd_size == -1) { 253 error_setg(errp, "could not load initrd '%s'", ipl->initrd); 254 return; 255 } 256 257 /* 258 * we have to overwrite values in the kernel image, 259 * which are "rom" 260 */ 261 romptr = rom_ptr(INITRD_PARM_START, 16); 262 if (romptr) { 263 stq_be_p(romptr, initrd_offset); 264 stq_be_p(romptr + 1, initrd_size); 265 } 266 } 267 } 268 /* 269 * Don't ever use the migrated values, they could come from a different 270 * BIOS and therefore don't work. But still migrate the values, so 271 * QEMUs relying on it don't break. 272 */ 273 ipl->compat_start_addr = ipl->start_addr; 274 ipl->compat_bios_start_addr = ipl->bios_start_addr; 275 /* 276 * Because this Device is not on any bus in the qbus tree (it is 277 * not a sysbus device and it's not on some other bus like a PCI 278 * bus) it will not be automatically reset by the 'reset the 279 * sysbus' hook registered by vl.c like most devices. So we must 280 * manually register a reset hook for it. 281 * TODO: there should be a better way to do this. 282 */ 283 qemu_register_reset(resettable_cold_reset_fn, dev); 284 } 285 286 static Property s390_ipl_properties[] = { 287 DEFINE_PROP_STRING("kernel", S390IPLState, kernel), 288 DEFINE_PROP_STRING("initrd", S390IPLState, initrd), 289 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline), 290 DEFINE_PROP_STRING("firmware", S390IPLState, firmware), 291 DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw), 292 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false), 293 DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration, 294 true), 295 DEFINE_PROP_END_OF_LIST(), 296 }; 297 298 static void s390_ipl_set_boot_menu(S390IPLState *ipl) 299 { 300 unsigned long splash_time = 0; 301 302 if (!get_boot_device(0)) { 303 if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) { 304 error_report("boot menu requires a bootindex to be specified for " 305 "the IPL device"); 306 } 307 return; 308 } 309 310 switch (ipl->iplb.pbt) { 311 case S390_IPL_TYPE_CCW: 312 /* In the absence of -boot menu, use zipl parameters */ 313 if (!current_machine->boot_config.has_menu) { 314 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL; 315 return; 316 } 317 break; 318 case S390_IPL_TYPE_QEMU_SCSI: 319 break; 320 default: 321 if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) { 322 error_report("boot menu is not supported for this device type"); 323 } 324 return; 325 } 326 327 if (!current_machine->boot_config.has_menu || !current_machine->boot_config.menu) { 328 return; 329 } 330 331 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD; 332 333 if (current_machine->boot_config.has_splash_time) { 334 splash_time = current_machine->boot_config.splash_time; 335 } 336 if (splash_time > 0xffffffff) { 337 error_report("splash-time is too large, forcing it to max value"); 338 ipl->qipl.boot_menu_timeout = 0xffffffff; 339 return; 340 } 341 342 ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time); 343 } 344 345 #define CCW_DEVTYPE_NONE 0x00 346 #define CCW_DEVTYPE_VIRTIO 0x01 347 #define CCW_DEVTYPE_VIRTIO_NET 0x02 348 #define CCW_DEVTYPE_SCSI 0x03 349 #define CCW_DEVTYPE_VFIO 0x04 350 351 static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype) 352 { 353 CcwDevice *ccw_dev = NULL; 354 int tmp_dt = CCW_DEVTYPE_NONE; 355 356 if (dev_st) { 357 VirtIONet *virtio_net_dev = (VirtIONet *) 358 object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET); 359 VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *) 360 object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent), 361 TYPE_VIRTIO_CCW_DEVICE); 362 VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *) 363 object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW); 364 365 if (virtio_ccw_dev) { 366 ccw_dev = CCW_DEVICE(virtio_ccw_dev); 367 if (virtio_net_dev) { 368 tmp_dt = CCW_DEVTYPE_VIRTIO_NET; 369 } else { 370 tmp_dt = CCW_DEVTYPE_VIRTIO; 371 } 372 } else if (vfio_ccw_dev) { 373 ccw_dev = CCW_DEVICE(vfio_ccw_dev); 374 tmp_dt = CCW_DEVTYPE_VFIO; 375 } else { 376 SCSIDevice *sd = (SCSIDevice *) 377 object_dynamic_cast(OBJECT(dev_st), 378 TYPE_SCSI_DEVICE); 379 if (sd) { 380 SCSIBus *sbus = scsi_bus_from_device(sd); 381 VirtIODevice *vdev = (VirtIODevice *) 382 object_dynamic_cast(OBJECT(sbus->qbus.parent), 383 TYPE_VIRTIO_DEVICE); 384 if (vdev) { 385 ccw_dev = (CcwDevice *) 386 object_dynamic_cast(OBJECT(qdev_get_parent_bus(DEVICE(vdev))->parent), 387 TYPE_CCW_DEVICE); 388 if (ccw_dev) { 389 tmp_dt = CCW_DEVTYPE_SCSI; 390 } 391 } 392 } 393 } 394 } 395 if (devtype) { 396 *devtype = tmp_dt; 397 } 398 return ccw_dev; 399 } 400 401 static bool s390_gen_initial_iplb(S390IPLState *ipl) 402 { 403 DeviceState *dev_st; 404 CcwDevice *ccw_dev = NULL; 405 SCSIDevice *sd; 406 int devtype; 407 408 dev_st = get_boot_device(0); 409 if (dev_st) { 410 ccw_dev = s390_get_ccw_device(dev_st, &devtype); 411 } 412 413 /* 414 * Currently allow IPL only from CCW devices. 415 */ 416 if (ccw_dev) { 417 switch (devtype) { 418 case CCW_DEVTYPE_SCSI: 419 sd = SCSI_DEVICE(dev_st); 420 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN); 421 ipl->iplb.blk0_len = 422 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN); 423 ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI; 424 ipl->iplb.scsi.lun = cpu_to_be32(sd->lun); 425 ipl->iplb.scsi.target = cpu_to_be16(sd->id); 426 ipl->iplb.scsi.channel = cpu_to_be16(sd->channel); 427 ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno); 428 ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3; 429 break; 430 case CCW_DEVTYPE_VFIO: 431 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN); 432 ipl->iplb.pbt = S390_IPL_TYPE_CCW; 433 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno); 434 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3; 435 break; 436 case CCW_DEVTYPE_VIRTIO_NET: 437 ipl->netboot = true; 438 /* Fall through to CCW_DEVTYPE_VIRTIO case */ 439 case CCW_DEVTYPE_VIRTIO: 440 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN); 441 ipl->iplb.blk0_len = 442 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN); 443 ipl->iplb.pbt = S390_IPL_TYPE_CCW; 444 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno); 445 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3; 446 break; 447 } 448 449 if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) { 450 ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID; 451 } 452 453 return true; 454 } 455 456 return false; 457 } 458 459 int s390_ipl_set_loadparm(uint8_t *loadparm) 460 { 461 MachineState *machine = MACHINE(qdev_get_machine()); 462 char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL); 463 464 if (lp) { 465 int i; 466 467 /* lp is an uppercase string without leading/embedded spaces */ 468 for (i = 0; i < 8 && lp[i]; i++) { 469 loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]]; 470 } 471 472 if (i < 8) { 473 memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */ 474 } 475 476 g_free(lp); 477 return 0; 478 } 479 480 return -1; 481 } 482 483 static int load_netboot_image(Error **errp) 484 { 485 MachineState *ms = MACHINE(qdev_get_machine()); 486 S390IPLState *ipl = get_ipl_device(); 487 char *netboot_filename; 488 MemoryRegion *sysmem = get_system_memory(); 489 MemoryRegion *mr = NULL; 490 void *ram_ptr = NULL; 491 int img_size = -1; 492 493 mr = memory_region_find(sysmem, 0, 1).mr; 494 if (!mr) { 495 error_setg(errp, "Failed to find memory region at address 0"); 496 return -1; 497 } 498 499 ram_ptr = memory_region_get_ram_ptr(mr); 500 if (!ram_ptr) { 501 error_setg(errp, "No RAM found"); 502 goto unref_mr; 503 } 504 505 netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw); 506 if (netboot_filename == NULL) { 507 error_setg(errp, "Could not find network bootloader '%s'", 508 ipl->netboot_fw); 509 goto unref_mr; 510 } 511 512 img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL, 513 &ipl->start_addr, 514 NULL, NULL, NULL, 1, EM_S390, 0, 0, NULL, 515 false); 516 517 if (img_size < 0) { 518 img_size = load_image_size(netboot_filename, ram_ptr, ms->ram_size); 519 ipl->start_addr = KERN_IMAGE_START; 520 } 521 522 if (img_size < 0) { 523 error_setg(errp, "Failed to load network bootloader"); 524 } 525 526 g_free(netboot_filename); 527 528 unref_mr: 529 memory_region_unref(mr); 530 return img_size; 531 } 532 533 static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb, 534 int virtio_id) 535 { 536 uint8_t cssid; 537 uint8_t ssid; 538 uint16_t devno; 539 uint16_t schid; 540 SubchDev *sch = NULL; 541 542 if (iplb->pbt != S390_IPL_TYPE_CCW) { 543 return false; 544 } 545 546 devno = be16_to_cpu(iplb->ccw.devno); 547 ssid = iplb->ccw.ssid & 3; 548 549 for (schid = 0; schid < MAX_SCHID; schid++) { 550 for (cssid = 0; cssid < MAX_CSSID; cssid++) { 551 sch = css_find_subch(1, cssid, ssid, schid); 552 553 if (sch && sch->devno == devno) { 554 return sch->id.cu_model == virtio_id; 555 } 556 } 557 } 558 return false; 559 } 560 561 static bool is_virtio_net_device(IplParameterBlock *iplb) 562 { 563 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET); 564 } 565 566 static bool is_virtio_scsi_device(IplParameterBlock *iplb) 567 { 568 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI); 569 } 570 571 static void update_machine_ipl_properties(IplParameterBlock *iplb) 572 { 573 Object *machine = qdev_get_machine(); 574 Error *err = NULL; 575 576 /* Sync loadparm */ 577 if (iplb->flags & DIAG308_FLAGS_LP_VALID) { 578 uint8_t *ebcdic_loadparm = iplb->loadparm; 579 char ascii_loadparm[9]; 580 int i; 581 582 for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) { 583 ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]]; 584 } 585 ascii_loadparm[i] = 0; 586 object_property_set_str(machine, "loadparm", ascii_loadparm, &err); 587 } else { 588 object_property_set_str(machine, "loadparm", "", &err); 589 } 590 if (err) { 591 warn_report_err(err); 592 } 593 } 594 595 void s390_ipl_update_diag308(IplParameterBlock *iplb) 596 { 597 S390IPLState *ipl = get_ipl_device(); 598 599 /* 600 * The IPLB set and retrieved by subcodes 8/9 is completely 601 * separate from the one managed via subcodes 5/6. 602 */ 603 if (iplb->pbt == S390_IPL_TYPE_PV) { 604 ipl->iplb_pv = *iplb; 605 ipl->iplb_valid_pv = true; 606 } else { 607 ipl->iplb = *iplb; 608 ipl->iplb_valid = true; 609 } 610 ipl->netboot = is_virtio_net_device(iplb); 611 update_machine_ipl_properties(iplb); 612 } 613 614 IplParameterBlock *s390_ipl_get_iplb_pv(void) 615 { 616 S390IPLState *ipl = get_ipl_device(); 617 618 if (!ipl->iplb_valid_pv) { 619 return NULL; 620 } 621 return &ipl->iplb_pv; 622 } 623 624 IplParameterBlock *s390_ipl_get_iplb(void) 625 { 626 S390IPLState *ipl = get_ipl_device(); 627 628 if (!ipl->iplb_valid) { 629 return NULL; 630 } 631 return &ipl->iplb; 632 } 633 634 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type) 635 { 636 S390IPLState *ipl = get_ipl_device(); 637 638 if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) { 639 /* use CPU 0 for full resets */ 640 ipl->reset_cpu_index = 0; 641 } else { 642 ipl->reset_cpu_index = cs->cpu_index; 643 } 644 ipl->reset_type = reset_type; 645 646 if (reset_type == S390_RESET_REIPL && 647 ipl->iplb_valid && 648 !ipl->netboot && 649 ipl->iplb.pbt == S390_IPL_TYPE_CCW && 650 is_virtio_scsi_device(&ipl->iplb)) { 651 CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL); 652 653 if (ccw_dev && 654 cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno && 655 (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) { 656 /* 657 * this is the original boot device's SCSI 658 * so restore IPL parameter info from it 659 */ 660 ipl->iplb_valid = s390_gen_initial_iplb(ipl); 661 } 662 } 663 if (reset_type == S390_RESET_MODIFIED_CLEAR || 664 reset_type == S390_RESET_LOAD_NORMAL || 665 reset_type == S390_RESET_PV) { 666 /* ignore -no-reboot, send no event */ 667 qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET); 668 } else { 669 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); 670 } 671 /* as this is triggered by a CPU, make sure to exit the loop */ 672 if (tcg_enabled()) { 673 cpu_loop_exit(cs); 674 } 675 } 676 677 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type) 678 { 679 S390IPLState *ipl = get_ipl_device(); 680 681 *cs = qemu_get_cpu(ipl->reset_cpu_index); 682 if (!*cs) { 683 /* use any CPU */ 684 *cs = first_cpu; 685 } 686 *reset_type = ipl->reset_type; 687 } 688 689 void s390_ipl_clear_reset_request(void) 690 { 691 S390IPLState *ipl = get_ipl_device(); 692 693 ipl->reset_type = S390_RESET_EXTERNAL; 694 /* use CPU 0 for full resets */ 695 ipl->reset_cpu_index = 0; 696 } 697 698 static void s390_ipl_prepare_qipl(S390CPU *cpu) 699 { 700 S390IPLState *ipl = get_ipl_device(); 701 uint8_t *addr; 702 uint64_t len = 4096; 703 704 addr = cpu_physical_memory_map(cpu->env.psa, &len, true); 705 if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) { 706 error_report("Cannot set QEMU IPL parameters"); 707 return; 708 } 709 memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters)); 710 cpu_physical_memory_unmap(addr, len, 1, len); 711 } 712 713 int s390_ipl_prepare_pv_header(Error **errp) 714 { 715 IplParameterBlock *ipib = s390_ipl_get_iplb_pv(); 716 IPLBlockPV *ipib_pv = &ipib->pv; 717 void *hdr = g_malloc(ipib_pv->pv_header_len); 718 int rc; 719 720 cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr, 721 ipib_pv->pv_header_len); 722 rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len, errp); 723 g_free(hdr); 724 return rc; 725 } 726 727 int s390_ipl_pv_unpack(void) 728 { 729 IplParameterBlock *ipib = s390_ipl_get_iplb_pv(); 730 IPLBlockPV *ipib_pv = &ipib->pv; 731 int i, rc = 0; 732 733 for (i = 0; i < ipib_pv->num_comp; i++) { 734 rc = s390_pv_unpack(ipib_pv->components[i].addr, 735 TARGET_PAGE_ALIGN(ipib_pv->components[i].size), 736 ipib_pv->components[i].tweak_pref); 737 if (rc) { 738 break; 739 } 740 } 741 return rc; 742 } 743 744 void s390_ipl_prepare_cpu(S390CPU *cpu) 745 { 746 S390IPLState *ipl = get_ipl_device(); 747 748 cpu->env.psw.addr = ipl->start_addr; 749 cpu->env.psw.mask = IPL_PSW_MASK; 750 751 if (!ipl->kernel || ipl->iplb_valid) { 752 cpu->env.psw.addr = ipl->bios_start_addr; 753 if (!ipl->iplb_valid) { 754 ipl->iplb_valid = s390_gen_initial_iplb(ipl); 755 } 756 } 757 if (ipl->netboot) { 758 load_netboot_image(&error_fatal); 759 ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr); 760 } 761 s390_ipl_set_boot_menu(ipl); 762 s390_ipl_prepare_qipl(cpu); 763 } 764 765 static void s390_ipl_reset(DeviceState *dev) 766 { 767 S390IPLState *ipl = S390_IPL(dev); 768 769 if (ipl->reset_type != S390_RESET_REIPL) { 770 ipl->iplb_valid = false; 771 memset(&ipl->iplb, 0, sizeof(IplParameterBlock)); 772 } 773 } 774 775 static void s390_ipl_class_init(ObjectClass *klass, void *data) 776 { 777 DeviceClass *dc = DEVICE_CLASS(klass); 778 779 dc->realize = s390_ipl_realize; 780 device_class_set_props(dc, s390_ipl_properties); 781 device_class_set_legacy_reset(dc, s390_ipl_reset); 782 dc->vmsd = &vmstate_ipl; 783 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 784 /* Reason: Loads the ROMs and thus can only be used one time - internally */ 785 dc->user_creatable = false; 786 } 787 788 static const TypeInfo s390_ipl_info = { 789 .class_init = s390_ipl_class_init, 790 .parent = TYPE_DEVICE, 791 .name = TYPE_S390_IPL, 792 .instance_size = sizeof(S390IPLState), 793 }; 794 795 static void s390_ipl_register_types(void) 796 { 797 type_register_static(&s390_ipl_info); 798 } 799 800 type_init(s390_ipl_register_types) 801