xref: /qemu/hw/s390x/ipl.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
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 "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 "qemu/ctype.h"
38 #include "standard-headers/linux/virtio_ids.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 BIOS_MAX_SIZE                   0x300000UL
50 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
51 
52 /* Place the IPLB chain immediately before the BIOS in memory */
53 static uint64_t find_iplb_chain_addr(uint64_t bios_addr, uint16_t count)
54 {
55     return (bios_addr & TARGET_PAGE_MASK)
56             - (count * sizeof(IplParameterBlock));
57 }
58 
59 static const VMStateDescription vmstate_iplb_extended = {
60     .name = "ipl/iplb_extended",
61     .version_id = 0,
62     .minimum_version_id = 0,
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,
166                              ELFDATA2MSB, 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, ELFDATA2MSB, 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 const 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_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
292 };
293 
294 static void s390_ipl_set_boot_menu(S390IPLState *ipl)
295 {
296     unsigned long splash_time = 0;
297 
298     if (!get_boot_device(0)) {
299         if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) {
300             error_report("boot menu requires a bootindex to be specified for "
301                          "the IPL device");
302         }
303         return;
304     }
305 
306     switch (ipl->iplb.pbt) {
307     case S390_IPL_TYPE_CCW:
308         /* In the absence of -boot menu, use zipl parameters */
309         if (!current_machine->boot_config.has_menu) {
310             ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL;
311             return;
312         }
313         break;
314     case S390_IPL_TYPE_QEMU_SCSI:
315         break;
316     default:
317         if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) {
318             error_report("boot menu is not supported for this device type");
319         }
320         return;
321     }
322 
323     if (!current_machine->boot_config.has_menu || !current_machine->boot_config.menu) {
324         return;
325     }
326 
327     ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
328 
329     if (current_machine->boot_config.has_splash_time) {
330         splash_time = current_machine->boot_config.splash_time;
331     }
332     if (splash_time > 0xffffffff) {
333         error_report("splash-time is too large, forcing it to max value");
334         ipl->qipl.boot_menu_timeout = 0xffffffff;
335         return;
336     }
337 
338     ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
339 }
340 
341 #define CCW_DEVTYPE_NONE        0x00
342 #define CCW_DEVTYPE_VIRTIO      0x01
343 #define CCW_DEVTYPE_VIRTIO_NET  0x02
344 #define CCW_DEVTYPE_SCSI        0x03
345 #define CCW_DEVTYPE_VFIO        0x04
346 
347 static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
348 {
349     CcwDevice *ccw_dev = NULL;
350     int tmp_dt = CCW_DEVTYPE_NONE;
351 
352     if (dev_st) {
353         VirtIONet *virtio_net_dev = (VirtIONet *)
354             object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET);
355         VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *)
356             object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
357                                 TYPE_VIRTIO_CCW_DEVICE);
358         VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *)
359             object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW);
360 
361         if (virtio_ccw_dev) {
362             ccw_dev = CCW_DEVICE(virtio_ccw_dev);
363             if (virtio_net_dev) {
364                 tmp_dt = CCW_DEVTYPE_VIRTIO_NET;
365             } else {
366                 tmp_dt = CCW_DEVTYPE_VIRTIO;
367             }
368         } else if (vfio_ccw_dev) {
369             ccw_dev = CCW_DEVICE(vfio_ccw_dev);
370             tmp_dt = CCW_DEVTYPE_VFIO;
371         } else {
372             SCSIDevice *sd = (SCSIDevice *)
373                 object_dynamic_cast(OBJECT(dev_st),
374                                     TYPE_SCSI_DEVICE);
375             if (sd) {
376                 SCSIBus *sbus = scsi_bus_from_device(sd);
377                 VirtIODevice *vdev = (VirtIODevice *)
378                     object_dynamic_cast(OBJECT(sbus->qbus.parent),
379                                         TYPE_VIRTIO_DEVICE);
380                 if (vdev) {
381                     ccw_dev = (CcwDevice *)
382                         object_dynamic_cast(OBJECT(qdev_get_parent_bus(DEVICE(vdev))->parent),
383                                             TYPE_CCW_DEVICE);
384                     if (ccw_dev) {
385                         tmp_dt = CCW_DEVTYPE_SCSI;
386                     }
387                 }
388             }
389         }
390     }
391     if (devtype) {
392         *devtype = tmp_dt;
393     }
394     return ccw_dev;
395 }
396 
397 static uint64_t s390_ipl_map_iplb_chain(IplParameterBlock *iplb_chain)
398 {
399     S390IPLState *ipl = get_ipl_device();
400     uint16_t count = be16_to_cpu(ipl->qipl.chain_len);
401     uint64_t len = sizeof(IplParameterBlock) * count;
402     uint64_t chain_addr = find_iplb_chain_addr(ipl->bios_start_addr, count);
403 
404     cpu_physical_memory_write(chain_addr, iplb_chain, len);
405     return chain_addr;
406 }
407 
408 void s390_ipl_fmt_loadparm(uint8_t *loadparm, char *str, Error **errp)
409 {
410     /* Initialize the loadparm with spaces */
411     memset(loadparm, ' ', LOADPARM_LEN);
412     qdev_prop_sanitize_s390x_loadparm(loadparm, str, errp);
413 }
414 
415 void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp)
416 {
417     int i;
418 
419     /* Initialize the loadparm with EBCDIC spaces (0x40) */
420     memset(ebcdic_lp, '@', LOADPARM_LEN);
421     for (i = 0; i < LOADPARM_LEN && ascii_lp[i]; i++) {
422         ebcdic_lp[i] = ascii2ebcdic[(uint8_t) ascii_lp[i]];
423     }
424 }
425 
426 static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)
427 {
428     CcwDevice *ccw_dev = NULL;
429     SCSIDevice *sd;
430     int devtype;
431     uint8_t *lp;
432     g_autofree void *scsi_lp = NULL;
433 
434     /*
435      * Currently allow IPL only from CCW devices.
436      */
437     ccw_dev = s390_get_ccw_device(dev_st, &devtype);
438     if (ccw_dev) {
439         lp = ccw_dev->loadparm;
440 
441         switch (devtype) {
442         case CCW_DEVTYPE_SCSI:
443             sd = SCSI_DEVICE(dev_st);
444             scsi_lp = object_property_get_str(OBJECT(sd), "loadparm", NULL);
445             if (scsi_lp && strlen(scsi_lp) > 0) {
446                 lp = scsi_lp;
447             }
448             iplb->len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
449             iplb->blk0_len =
450                 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
451             iplb->pbt = S390_IPL_TYPE_QEMU_SCSI;
452             iplb->scsi.lun = cpu_to_be32(sd->lun);
453             iplb->scsi.target = cpu_to_be16(sd->id);
454             iplb->scsi.channel = cpu_to_be16(sd->channel);
455             iplb->scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
456             iplb->scsi.ssid = ccw_dev->sch->ssid & 3;
457             break;
458         case CCW_DEVTYPE_VFIO:
459             iplb->len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
460             iplb->pbt = S390_IPL_TYPE_CCW;
461             iplb->ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
462             iplb->ccw.ssid = ccw_dev->sch->ssid & 3;
463             break;
464         case CCW_DEVTYPE_VIRTIO_NET:
465         case CCW_DEVTYPE_VIRTIO:
466             iplb->len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
467             iplb->blk0_len =
468                 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
469             iplb->pbt = S390_IPL_TYPE_CCW;
470             iplb->ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
471             iplb->ccw.ssid = ccw_dev->sch->ssid & 3;
472             break;
473         }
474 
475         /* If the device loadparm is empty use the global machine loadparm */
476         if (memcmp(lp, NO_LOADPARM, 8) == 0) {
477             lp = S390_CCW_MACHINE(qdev_get_machine())->loadparm;
478         }
479 
480         s390_ipl_convert_loadparm((char *)lp, iplb->loadparm);
481         iplb->flags |= DIAG308_FLAGS_LP_VALID;
482 
483         return true;
484     }
485 
486     return false;
487 }
488 
489 void s390_rebuild_iplb(uint16_t dev_index, IplParameterBlock *iplb)
490 {
491     S390IPLState *ipl = get_ipl_device();
492     uint16_t index;
493     index = ipl->rebuilt_iplb ? ipl->iplb_index : dev_index;
494 
495     ipl->rebuilt_iplb = s390_build_iplb(get_boot_device(index), iplb);
496     ipl->iplb_index = index;
497 }
498 
499 static bool s390_init_all_iplbs(S390IPLState *ipl)
500 {
501     int iplb_num = 0;
502     IplParameterBlock iplb_chain[7];
503     DeviceState *dev_st = get_boot_device(0);
504     Object *machine = qdev_get_machine();
505 
506     /*
507      * Parse the boot devices.  Generate an IPLB for only the first boot device
508      * which will later be set with DIAG308.
509      */
510     if (!dev_st) {
511         ipl->qipl.chain_len = 0;
512         return false;
513     }
514 
515     /* If no machine loadparm was defined fill it with spaces */
516     if (memcmp(S390_CCW_MACHINE(machine)->loadparm, NO_LOADPARM, 8) == 0) {
517         object_property_set_str(machine, "loadparm", "        ", NULL);
518     }
519 
520     iplb_num = 1;
521     s390_build_iplb(dev_st, &ipl->iplb);
522 
523     /*  Index any fallback boot devices */
524     while (get_boot_device(iplb_num)) {
525         iplb_num++;
526     }
527 
528     if (iplb_num > MAX_BOOT_DEVS) {
529         warn_report("Excess boot devices defined! %d boot devices found, "
530                     "but only the first %d will be considered.",
531                     iplb_num, MAX_BOOT_DEVS);
532 
533         iplb_num = MAX_BOOT_DEVS;
534     }
535 
536     ipl->qipl.chain_len = cpu_to_be16(iplb_num - 1);
537 
538     /*
539      * Build fallback IPLBs for any boot devices above index 0, up to a
540      * maximum amount as defined in ipl.h
541      */
542     if (iplb_num > 1) {
543         /* Start at 1 because the IPLB for boot index 0 is not chained */
544         for (int i = 1; i < iplb_num; i++) {
545             dev_st = get_boot_device(i);
546             s390_build_iplb(dev_st, &iplb_chain[i - 1]);
547         }
548 
549         ipl->qipl.next_iplb = cpu_to_be64(s390_ipl_map_iplb_chain(iplb_chain));
550     }
551 
552     return iplb_num;
553 }
554 
555 static void update_machine_ipl_properties(IplParameterBlock *iplb)
556 {
557     Object *machine = qdev_get_machine();
558     Error *err = NULL;
559 
560     /* Sync loadparm */
561     if (iplb->flags & DIAG308_FLAGS_LP_VALID) {
562         uint8_t *ebcdic_loadparm = iplb->loadparm;
563         char ascii_loadparm[9];
564         int i;
565 
566         for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
567             ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
568         }
569         ascii_loadparm[i] = 0;
570         object_property_set_str(machine, "loadparm", ascii_loadparm, &err);
571     } else {
572         object_property_set_str(machine, "loadparm", "        ", &err);
573     }
574     if (err) {
575         warn_report_err(err);
576     }
577 }
578 
579 void s390_ipl_update_diag308(IplParameterBlock *iplb)
580 {
581     S390IPLState *ipl = get_ipl_device();
582 
583     /*
584      * The IPLB set and retrieved by subcodes 8/9 is completely
585      * separate from the one managed via subcodes 5/6.
586      */
587     if (iplb->pbt == S390_IPL_TYPE_PV) {
588         ipl->iplb_pv = *iplb;
589         ipl->iplb_valid_pv = true;
590     } else {
591         ipl->iplb = *iplb;
592         ipl->iplb_valid = true;
593     }
594 
595     update_machine_ipl_properties(iplb);
596 }
597 
598 IplParameterBlock *s390_ipl_get_iplb_pv(void)
599 {
600     S390IPLState *ipl = get_ipl_device();
601 
602     if (!ipl->iplb_valid_pv) {
603         return NULL;
604     }
605     return &ipl->iplb_pv;
606 }
607 
608 IplParameterBlock *s390_ipl_get_iplb(void)
609 {
610     S390IPLState *ipl = get_ipl_device();
611 
612     if (!ipl->iplb_valid) {
613         return NULL;
614     }
615     return &ipl->iplb;
616 }
617 
618 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
619 {
620     S390IPLState *ipl = get_ipl_device();
621     if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) {
622         /* use CPU 0 for full resets */
623         ipl->reset_cpu_index = 0;
624     } else {
625         ipl->reset_cpu_index = cs->cpu_index;
626     }
627 
628     ipl->reset_type = reset_type;
629     if (reset_type == S390_RESET_MODIFIED_CLEAR ||
630         reset_type == S390_RESET_LOAD_NORMAL ||
631         reset_type == S390_RESET_PV) {
632         /* ignore -no-reboot, send no event  */
633         qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
634     } else {
635         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
636     }
637     /* as this is triggered by a CPU, make sure to exit the loop */
638     if (tcg_enabled()) {
639         cpu_loop_exit(cs);
640     }
641 }
642 
643 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
644 {
645     S390IPLState *ipl = get_ipl_device();
646 
647     *cs = qemu_get_cpu(ipl->reset_cpu_index);
648     if (!*cs) {
649         /* use any CPU */
650         *cs = first_cpu;
651     }
652     *reset_type = ipl->reset_type;
653 }
654 
655 void s390_ipl_clear_reset_request(void)
656 {
657     S390IPLState *ipl = get_ipl_device();
658 
659     ipl->reset_type = S390_RESET_EXTERNAL;
660     /* use CPU 0 for full resets */
661     ipl->reset_cpu_index = 0;
662 }
663 
664 static void s390_ipl_prepare_qipl(S390CPU *cpu)
665 {
666     S390IPLState *ipl = get_ipl_device();
667     uint8_t *addr;
668     uint64_t len = 4096;
669 
670     addr = cpu_physical_memory_map(cpu->env.psa, &len, true);
671     if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
672         error_report("Cannot set QEMU IPL parameters");
673         return;
674     }
675     memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
676     cpu_physical_memory_unmap(addr, len, 1, len);
677 }
678 
679 int s390_ipl_prepare_pv_header(Error **errp)
680 {
681     IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
682     IPLBlockPV *ipib_pv = &ipib->pv;
683     void *hdr = g_malloc(ipib_pv->pv_header_len);
684     int rc;
685 
686     cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
687                              ipib_pv->pv_header_len);
688     rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len, errp);
689     g_free(hdr);
690     return rc;
691 }
692 
693 int s390_ipl_pv_unpack(void)
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         if (rc) {
704             break;
705         }
706     }
707     return rc;
708 }
709 
710 void s390_ipl_prepare_cpu(S390CPU *cpu)
711 {
712     S390IPLState *ipl = get_ipl_device();
713 
714     cpu->env.psw.addr = ipl->start_addr;
715     cpu->env.psw.mask = IPL_PSW_MASK;
716 
717     if (!ipl->kernel || ipl->iplb_valid) {
718         cpu->env.psw.addr = ipl->bios_start_addr;
719         if (!ipl->iplb_valid) {
720             ipl->iplb_valid = s390_init_all_iplbs(ipl);
721         } else {
722             ipl->qipl.chain_len = 0;
723         }
724     }
725     s390_ipl_set_boot_menu(ipl);
726     s390_ipl_prepare_qipl(cpu);
727 }
728 
729 static void s390_ipl_reset(DeviceState *dev)
730 {
731     S390IPLState *ipl = S390_IPL(dev);
732 
733     if (ipl->reset_type != S390_RESET_REIPL) {
734         ipl->iplb_valid = false;
735         memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
736     }
737 }
738 
739 static void s390_ipl_class_init(ObjectClass *klass, void *data)
740 {
741     DeviceClass *dc = DEVICE_CLASS(klass);
742 
743     dc->realize = s390_ipl_realize;
744     device_class_set_props(dc, s390_ipl_properties);
745     device_class_set_legacy_reset(dc, s390_ipl_reset);
746     dc->vmsd = &vmstate_ipl;
747     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
748     /* Reason: Loads the ROMs and thus can only be used one time - internally */
749     dc->user_creatable = false;
750 }
751 
752 static const TypeInfo s390_ipl_info = {
753     .class_init = s390_ipl_class_init,
754     .parent = TYPE_DEVICE,
755     .name  = TYPE_S390_IPL,
756     .instance_size  = sizeof(S390IPLState),
757 };
758 
759 static void s390_ipl_register_types(void)
760 {
761     type_register_static(&s390_ipl_info);
762 }
763 
764 type_init(s390_ipl_register_types)
765