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