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