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