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