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
iplb_extended_needed(void * opaque)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 */
find_iplb_chain_addr(uint64_t bios_addr,uint16_t count)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
get_ipl_device(void)109 static S390IPLState *get_ipl_device(void)
110 {
111 return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
112 }
113
bios_translate_addr(void * opaque,uint64_t srcaddr)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
get_max_kernel_cmdline_size(void)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
s390_ipl_realize(DeviceState * dev,Error ** errp)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
s390_ipl_set_boot_menu(S390IPLState * ipl)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
s390_get_ccw_device(DeviceState * dev_st,int * devtype)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
s390_ipl_map_iplb_chain(IplParameterBlock * iplb_chain)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
s390_ipl_fmt_loadparm(uint8_t * loadparm,char * str,Error ** errp)419 void s390_ipl_fmt_loadparm(uint8_t *loadparm, char *str, Error **errp)
420 {
421 /* Initialize the loadparm with spaces */
422 memset(loadparm, ' ', LOADPARM_LEN);
423 qdev_prop_sanitize_s390x_loadparm(loadparm, str, errp);
424 }
425
s390_ipl_convert_loadparm(char * ascii_lp,uint8_t * ebcdic_lp)426 void s390_ipl_convert_loadparm(char *ascii_lp, uint8_t *ebcdic_lp)
427 {
428 int i;
429
430 /* Initialize the loadparm with EBCDIC spaces (0x40) */
431 memset(ebcdic_lp, '@', LOADPARM_LEN);
432 for (i = 0; i < LOADPARM_LEN && ascii_lp[i]; i++) {
433 ebcdic_lp[i] = ascii2ebcdic[(uint8_t) ascii_lp[i]];
434 }
435 }
436
s390_build_iplb(DeviceState * dev_st,IplParameterBlock * iplb)437 static bool s390_build_iplb(DeviceState *dev_st, IplParameterBlock *iplb)
438 {
439 CcwDevice *ccw_dev = NULL;
440 SCSIDevice *sd;
441 int devtype;
442 uint8_t *lp;
443 g_autofree void *scsi_lp = NULL;
444
445 /*
446 * Currently allow IPL only from CCW devices.
447 */
448 ccw_dev = s390_get_ccw_device(dev_st, &devtype);
449 if (ccw_dev) {
450 lp = ccw_dev->loadparm;
451
452 switch (devtype) {
453 case CCW_DEVTYPE_SCSI:
454 sd = SCSI_DEVICE(dev_st);
455 scsi_lp = object_property_get_str(OBJECT(sd), "loadparm", NULL);
456 if (scsi_lp && strlen(scsi_lp) > 0) {
457 lp = scsi_lp;
458 }
459 iplb->len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
460 iplb->blk0_len =
461 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
462 iplb->pbt = S390_IPL_TYPE_QEMU_SCSI;
463 iplb->scsi.lun = cpu_to_be32(sd->lun);
464 iplb->scsi.target = cpu_to_be16(sd->id);
465 iplb->scsi.channel = cpu_to_be16(sd->channel);
466 iplb->scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
467 iplb->scsi.ssid = ccw_dev->sch->ssid & 3;
468 break;
469 case CCW_DEVTYPE_VFIO:
470 iplb->len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
471 iplb->pbt = S390_IPL_TYPE_CCW;
472 iplb->ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
473 iplb->ccw.ssid = ccw_dev->sch->ssid & 3;
474 break;
475 case CCW_DEVTYPE_VIRTIO_NET:
476 case CCW_DEVTYPE_VIRTIO:
477 iplb->len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
478 iplb->blk0_len =
479 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
480 iplb->pbt = S390_IPL_TYPE_CCW;
481 iplb->ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
482 iplb->ccw.ssid = ccw_dev->sch->ssid & 3;
483 break;
484 }
485
486 /* If the device loadparm is empty use the global machine loadparm */
487 if (memcmp(lp, NO_LOADPARM, 8) == 0) {
488 lp = S390_CCW_MACHINE(qdev_get_machine())->loadparm;
489 }
490
491 s390_ipl_convert_loadparm((char *)lp, iplb->loadparm);
492 iplb->flags |= DIAG308_FLAGS_LP_VALID;
493
494 return true;
495 }
496
497 return false;
498 }
499
s390_rebuild_iplb(uint16_t dev_index,IplParameterBlock * iplb)500 void s390_rebuild_iplb(uint16_t dev_index, IplParameterBlock *iplb)
501 {
502 S390IPLState *ipl = get_ipl_device();
503 uint16_t index;
504 index = ipl->rebuilt_iplb ? ipl->iplb_index : dev_index;
505
506 ipl->rebuilt_iplb = s390_build_iplb(get_boot_device(index), iplb);
507 ipl->iplb_index = index;
508 }
509
s390_init_all_iplbs(S390IPLState * ipl)510 static bool s390_init_all_iplbs(S390IPLState *ipl)
511 {
512 int iplb_num = 0;
513 IplParameterBlock iplb_chain[7];
514 DeviceState *dev_st = get_boot_device(0);
515 Object *machine = qdev_get_machine();
516
517 /*
518 * Parse the boot devices. Generate an IPLB for only the first boot device
519 * which will later be set with DIAG308.
520 */
521 if (!dev_st) {
522 ipl->qipl.chain_len = 0;
523 return false;
524 }
525
526 /* If no machine loadparm was defined fill it with spaces */
527 if (memcmp(S390_CCW_MACHINE(machine)->loadparm, NO_LOADPARM, 8) == 0) {
528 object_property_set_str(machine, "loadparm", " ", NULL);
529 }
530
531 iplb_num = 1;
532 s390_build_iplb(dev_st, &ipl->iplb);
533
534 /* Index any fallback boot devices */
535 while (get_boot_device(iplb_num)) {
536 iplb_num++;
537 }
538
539 if (iplb_num > MAX_BOOT_DEVS) {
540 warn_report("Excess boot devices defined! %d boot devices found, "
541 "but only the first %d will be considered.",
542 iplb_num, MAX_BOOT_DEVS);
543
544 iplb_num = MAX_BOOT_DEVS;
545 }
546
547 ipl->qipl.chain_len = cpu_to_be16(iplb_num - 1);
548
549 /*
550 * Build fallback IPLBs for any boot devices above index 0, up to a
551 * maximum amount as defined in ipl.h
552 */
553 if (iplb_num > 1) {
554 /* Start at 1 because the IPLB for boot index 0 is not chained */
555 for (int i = 1; i < iplb_num; i++) {
556 dev_st = get_boot_device(i);
557 s390_build_iplb(dev_st, &iplb_chain[i - 1]);
558 }
559
560 ipl->qipl.next_iplb = cpu_to_be64(s390_ipl_map_iplb_chain(iplb_chain));
561 }
562
563 return iplb_num;
564 }
565
update_machine_ipl_properties(IplParameterBlock * iplb)566 static void update_machine_ipl_properties(IplParameterBlock *iplb)
567 {
568 Object *machine = qdev_get_machine();
569 Error *err = NULL;
570
571 /* Sync loadparm */
572 if (iplb->flags & DIAG308_FLAGS_LP_VALID) {
573 uint8_t *ebcdic_loadparm = iplb->loadparm;
574 char ascii_loadparm[9];
575 int i;
576
577 for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
578 ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
579 }
580 ascii_loadparm[i] = 0;
581 object_property_set_str(machine, "loadparm", ascii_loadparm, &err);
582 } else {
583 object_property_set_str(machine, "loadparm", " ", &err);
584 }
585 if (err) {
586 warn_report_err(err);
587 }
588 }
589
s390_ipl_update_diag308(IplParameterBlock * iplb)590 void s390_ipl_update_diag308(IplParameterBlock *iplb)
591 {
592 S390IPLState *ipl = get_ipl_device();
593
594 /*
595 * The IPLB set and retrieved by subcodes 8/9 is completely
596 * separate from the one managed via subcodes 5/6.
597 */
598 if (iplb->pbt == S390_IPL_TYPE_PV) {
599 ipl->iplb_pv = *iplb;
600 ipl->iplb_valid_pv = true;
601 } else {
602 ipl->iplb = *iplb;
603 ipl->iplb_valid = true;
604 }
605
606 update_machine_ipl_properties(iplb);
607 }
608
s390_ipl_get_iplb_pv(void)609 IplParameterBlock *s390_ipl_get_iplb_pv(void)
610 {
611 S390IPLState *ipl = get_ipl_device();
612
613 if (!ipl->iplb_valid_pv) {
614 return NULL;
615 }
616 return &ipl->iplb_pv;
617 }
618
s390_ipl_get_iplb(void)619 IplParameterBlock *s390_ipl_get_iplb(void)
620 {
621 S390IPLState *ipl = get_ipl_device();
622
623 if (!ipl->iplb_valid) {
624 return NULL;
625 }
626 return &ipl->iplb;
627 }
628
s390_ipl_reset_request(CPUState * cs,enum s390_reset reset_type)629 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
630 {
631 S390IPLState *ipl = get_ipl_device();
632 if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) {
633 /* use CPU 0 for full resets */
634 ipl->reset_cpu_index = 0;
635 } else {
636 ipl->reset_cpu_index = cs->cpu_index;
637 }
638
639 ipl->reset_type = reset_type;
640 if (reset_type == S390_RESET_MODIFIED_CLEAR ||
641 reset_type == S390_RESET_LOAD_NORMAL ||
642 reset_type == S390_RESET_PV) {
643 /* ignore -no-reboot, send no event */
644 qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
645 } else {
646 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
647 }
648 /* as this is triggered by a CPU, make sure to exit the loop */
649 if (tcg_enabled()) {
650 cpu_loop_exit(cs);
651 }
652 }
653
s390_ipl_get_reset_request(CPUState ** cs,enum s390_reset * reset_type)654 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
655 {
656 S390IPLState *ipl = get_ipl_device();
657
658 *cs = qemu_get_cpu(ipl->reset_cpu_index);
659 if (!*cs) {
660 /* use any CPU */
661 *cs = first_cpu;
662 }
663 *reset_type = ipl->reset_type;
664 }
665
s390_ipl_clear_reset_request(void)666 void s390_ipl_clear_reset_request(void)
667 {
668 S390IPLState *ipl = get_ipl_device();
669
670 ipl->reset_type = S390_RESET_EXTERNAL;
671 /* use CPU 0 for full resets */
672 ipl->reset_cpu_index = 0;
673 }
674
s390_ipl_prepare_qipl(S390CPU * cpu)675 static void s390_ipl_prepare_qipl(S390CPU *cpu)
676 {
677 S390IPLState *ipl = get_ipl_device();
678 uint8_t *addr;
679 uint64_t len = 4096;
680
681 addr = cpu_physical_memory_map(cpu->env.psa, &len, true);
682 if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
683 error_report("Cannot set QEMU IPL parameters");
684 return;
685 }
686 memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
687 cpu_physical_memory_unmap(addr, len, 1, len);
688 }
689
s390_ipl_prepare_pv_header(Error ** errp)690 int s390_ipl_prepare_pv_header(Error **errp)
691 {
692 IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
693 IPLBlockPV *ipib_pv = &ipib->pv;
694 void *hdr = g_malloc(ipib_pv->pv_header_len);
695 int rc;
696
697 cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
698 ipib_pv->pv_header_len);
699 rc = s390_pv_set_sec_parms((uintptr_t)hdr, ipib_pv->pv_header_len, errp);
700 g_free(hdr);
701 return rc;
702 }
703
s390_ipl_pv_unpack(void)704 int s390_ipl_pv_unpack(void)
705 {
706 IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
707 IPLBlockPV *ipib_pv = &ipib->pv;
708 int i, rc = 0;
709
710 for (i = 0; i < ipib_pv->num_comp; i++) {
711 rc = s390_pv_unpack(ipib_pv->components[i].addr,
712 TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
713 ipib_pv->components[i].tweak_pref);
714 if (rc) {
715 break;
716 }
717 }
718 return rc;
719 }
720
s390_ipl_prepare_cpu(S390CPU * cpu)721 void s390_ipl_prepare_cpu(S390CPU *cpu)
722 {
723 S390IPLState *ipl = get_ipl_device();
724
725 cpu->env.psw.addr = ipl->start_addr;
726 cpu->env.psw.mask = IPL_PSW_MASK;
727
728 if (!ipl->kernel || ipl->iplb_valid) {
729 cpu->env.psw.addr = ipl->bios_start_addr;
730 if (!ipl->iplb_valid) {
731 ipl->iplb_valid = s390_init_all_iplbs(ipl);
732 } else {
733 ipl->qipl.chain_len = 0;
734 }
735 }
736 s390_ipl_set_boot_menu(ipl);
737 s390_ipl_prepare_qipl(cpu);
738 }
739
s390_ipl_reset(DeviceState * dev)740 static void s390_ipl_reset(DeviceState *dev)
741 {
742 S390IPLState *ipl = S390_IPL(dev);
743
744 if (ipl->reset_type != S390_RESET_REIPL) {
745 ipl->iplb_valid = false;
746 memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
747 }
748 }
749
s390_ipl_class_init(ObjectClass * klass,void * data)750 static void s390_ipl_class_init(ObjectClass *klass, void *data)
751 {
752 DeviceClass *dc = DEVICE_CLASS(klass);
753
754 dc->realize = s390_ipl_realize;
755 device_class_set_props(dc, s390_ipl_properties);
756 device_class_set_legacy_reset(dc, s390_ipl_reset);
757 dc->vmsd = &vmstate_ipl;
758 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
759 /* Reason: Loads the ROMs and thus can only be used one time - internally */
760 dc->user_creatable = false;
761 }
762
763 static const TypeInfo s390_ipl_info = {
764 .class_init = s390_ipl_class_init,
765 .parent = TYPE_DEVICE,
766 .name = TYPE_S390_IPL,
767 .instance_size = sizeof(S390IPLState),
768 };
769
s390_ipl_register_types(void)770 static void s390_ipl_register_types(void)
771 {
772 type_register_static(&s390_ipl_info);
773 }
774
775 type_init(s390_ipl_register_types)
776