]> git.proxmox.com Git - mirror_qemu.git/blob - hw/s390x/ipl.c
Merge tag 'for-upstream' of https://gitlab.com/bonzini/qemu into staging
[mirror_qemu.git] / hw / s390x / ipl.c
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 "hw/s390x/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 "standard-headers/linux/virtio_ids.h"
38 #include "exec/exec-all.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 IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
50
51 static bool iplb_extended_needed(void *opaque)
52 {
53 S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
54
55 return ipl->iplbext_migration;
56 }
57
58 static const VMStateDescription vmstate_iplb_extended = {
59 .name = "ipl/iplb_extended",
60 .version_id = 0,
61 .minimum_version_id = 0,
62 .needed = iplb_extended_needed,
63 .fields = (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 = (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*[]) {
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 = (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 = (MIN(ms->ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
149
150 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->firmware);
151 if (bios_filename == NULL) {
152 error_setg(errp, "could not find stage1 bootloader");
153 return;
154 }
155
156 bios_size = load_elf(bios_filename, NULL,
157 bios_translate_addr, &fwbase,
158 &ipl->bios_start_addr, NULL, NULL, NULL, 1,
159 EM_S390, 0, 0);
160 if (bios_size > 0) {
161 /* Adjust ELF start address to final location */
162 ipl->bios_start_addr += fwbase;
163 } else {
164 /* Try to load non-ELF file */
165 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
166 4096);
167 ipl->bios_start_addr = ZIPL_IMAGE_START;
168 }
169 g_free(bios_filename);
170
171 if (bios_size == -1) {
172 error_setg(errp, "could not load bootloader '%s'", ipl->firmware);
173 return;
174 }
175
176 /* default boot target is the bios */
177 ipl->start_addr = ipl->bios_start_addr;
178 }
179
180 if (ipl->kernel) {
181 kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL,
182 &pentry, NULL,
183 NULL, NULL, 1, EM_S390, 0, 0);
184 if (kernel_size < 0) {
185 kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size);
186 if (kernel_size < 0) {
187 error_setg(errp, "could not load kernel '%s'", ipl->kernel);
188 return;
189 }
190 /* if this is Linux use KERN_IMAGE_START */
191 magic = rom_ptr(LINUX_MAGIC_ADDR, 6);
192 if (magic && !memcmp(magic, "S390EP", 6)) {
193 pentry = KERN_IMAGE_START;
194 } else {
195 /* if not Linux load the address of the (short) IPL PSW */
196 ipl_psw = rom_ptr(4, 4);
197 if (ipl_psw) {
198 pentry = be32_to_cpu(*ipl_psw) & PSW_MASK_SHORT_ADDR;
199 } else {
200 error_setg(errp, "Could not get IPL PSW");
201 return;
202 }
203 }
204 }
205 /*
206 * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
207 * kernel parameters here as well. Note: For old kernels (up to 3.2)
208 * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
209 * loader) and it won't work. For this case we force it to 0x10000, too.
210 */
211 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
212 size_t cmdline_size = strlen(ipl->cmdline) + 1;
213 char *parm_area = rom_ptr(KERN_PARM_AREA, cmdline_size);
214
215 ipl->start_addr = KERN_IMAGE_START;
216 /* Overwrite parameters in the kernel image, which are "rom" */
217 if (parm_area) {
218 uint64_t max_cmdline_size = get_max_kernel_cmdline_size();
219
220 if (cmdline_size > max_cmdline_size) {
221 error_setg(errp,
222 "kernel command line exceeds maximum size:"
223 " %zu > %" PRIu64,
224 cmdline_size, max_cmdline_size);
225 return;
226 }
227
228 strcpy(parm_area, ipl->cmdline);
229 }
230 } else {
231 ipl->start_addr = pentry;
232 }
233
234 if (ipl->initrd) {
235 ram_addr_t initrd_offset;
236 int initrd_size;
237 uint64_t *romptr;
238
239 initrd_offset = INITRD_START;
240 while (kernel_size + 0x100000 > initrd_offset) {
241 initrd_offset += 0x100000;
242 }
243 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
244 ms->ram_size - initrd_offset);
245 if (initrd_size == -1) {
246 error_setg(errp, "could not load initrd '%s'", ipl->initrd);
247 return;
248 }
249
250 /*
251 * we have to overwrite values in the kernel image,
252 * which are "rom"
253 */
254 romptr = rom_ptr(INITRD_PARM_START, 16);
255 if (romptr) {
256 stq_p(romptr, initrd_offset);
257 stq_p(romptr + 1, initrd_size);
258 }
259 }
260 }
261 /*
262 * Don't ever use the migrated values, they could come from a different
263 * BIOS and therefore don't work. But still migrate the values, so
264 * QEMUs relying on it don't break.
265 */
266 ipl->compat_start_addr = ipl->start_addr;
267 ipl->compat_bios_start_addr = ipl->bios_start_addr;
268 /*
269 * Because this Device is not on any bus in the qbus tree (it is
270 * not a sysbus device and it's not on some other bus like a PCI
271 * bus) it will not be automatically reset by the 'reset the
272 * sysbus' hook registered by vl.c like most devices. So we must
273 * manually register a reset hook for it.
274 * TODO: there should be a better way to do this.
275 */
276 qemu_register_reset(resettable_cold_reset_fn, dev);
277 }
278
279 static Property s390_ipl_properties[] = {
280 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
281 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
282 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
283 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
284 DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw),
285 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
286 DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration,
287 true),
288 DEFINE_PROP_END_OF_LIST(),
289 };
290
291 static void s390_ipl_set_boot_menu(S390IPLState *ipl)
292 {
293 QemuOptsList *plist = qemu_find_opts("boot-opts");
294 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
295 const char *tmp;
296 unsigned long splash_time = 0;
297
298 if (!get_boot_device(0)) {
299 if (boot_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 (!qemu_opt_get(opts, "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 (boot_menu) {
318 error_report("boot menu is not supported for this device type");
319 }
320 return;
321 }
322
323 if (!boot_menu) {
324 return;
325 }
326
327 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
328
329 tmp = qemu_opt_get(opts, "splash-time");
330
331 if (tmp && qemu_strtoul(tmp, NULL, 10, &splash_time)) {
332 error_report("splash-time is invalid, forcing it to 0");
333 ipl->qipl.boot_menu_timeout = 0;
334 return;
335 }
336
337 if (splash_time > 0xffffffff) {
338 error_report("splash-time is too large, forcing it to max value");
339 ipl->qipl.boot_menu_timeout = 0xffffffff;
340 return;
341 }
342
343 ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
344 }
345
346 #define CCW_DEVTYPE_NONE 0x00
347 #define CCW_DEVTYPE_VIRTIO 0x01
348 #define CCW_DEVTYPE_VIRTIO_NET 0x02
349 #define CCW_DEVTYPE_SCSI 0x03
350 #define CCW_DEVTYPE_VFIO 0x04
351
352 static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
353 {
354 CcwDevice *ccw_dev = NULL;
355 int tmp_dt = CCW_DEVTYPE_NONE;
356
357 if (dev_st) {
358 VirtIONet *virtio_net_dev = (VirtIONet *)
359 object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET);
360 VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *)
361 object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
362 TYPE_VIRTIO_CCW_DEVICE);
363 VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *)
364 object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW);
365
366 if (virtio_ccw_dev) {
367 ccw_dev = CCW_DEVICE(virtio_ccw_dev);
368 if (virtio_net_dev) {
369 tmp_dt = CCW_DEVTYPE_VIRTIO_NET;
370 } else {
371 tmp_dt = CCW_DEVTYPE_VIRTIO;
372 }
373 } else if (vfio_ccw_dev) {
374 ccw_dev = CCW_DEVICE(vfio_ccw_dev);
375 tmp_dt = CCW_DEVTYPE_VFIO;
376 } else {
377 SCSIDevice *sd = (SCSIDevice *)
378 object_dynamic_cast(OBJECT(dev_st),
379 TYPE_SCSI_DEVICE);
380 if (sd) {
381 SCSIBus *sbus = scsi_bus_from_device(sd);
382 VirtIODevice *vdev = (VirtIODevice *)
383 object_dynamic_cast(OBJECT(sbus->qbus.parent),
384 TYPE_VIRTIO_DEVICE);
385 if (vdev) {
386 ccw_dev = (CcwDevice *)
387 object_dynamic_cast(OBJECT(qdev_get_parent_bus(DEVICE(vdev))->parent),
388 TYPE_CCW_DEVICE);
389 if (ccw_dev) {
390 tmp_dt = CCW_DEVTYPE_SCSI;
391 }
392 }
393 }
394 }
395 }
396 if (devtype) {
397 *devtype = tmp_dt;
398 }
399 return ccw_dev;
400 }
401
402 static bool s390_gen_initial_iplb(S390IPLState *ipl)
403 {
404 DeviceState *dev_st;
405 CcwDevice *ccw_dev = NULL;
406 SCSIDevice *sd;
407 int devtype;
408
409 dev_st = get_boot_device(0);
410 if (dev_st) {
411 ccw_dev = s390_get_ccw_device(dev_st, &devtype);
412 }
413
414 /*
415 * Currently allow IPL only from CCW devices.
416 */
417 if (ccw_dev) {
418 switch (devtype) {
419 case CCW_DEVTYPE_SCSI:
420 sd = SCSI_DEVICE(dev_st);
421 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
422 ipl->iplb.blk0_len =
423 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
424 ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI;
425 ipl->iplb.scsi.lun = cpu_to_be32(sd->lun);
426 ipl->iplb.scsi.target = cpu_to_be16(sd->id);
427 ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
428 ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
429 ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
430 break;
431 case CCW_DEVTYPE_VFIO:
432 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
433 ipl->iplb.pbt = S390_IPL_TYPE_CCW;
434 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
435 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
436 break;
437 case CCW_DEVTYPE_VIRTIO_NET:
438 ipl->netboot = true;
439 /* Fall through to CCW_DEVTYPE_VIRTIO case */
440 case CCW_DEVTYPE_VIRTIO:
441 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
442 ipl->iplb.blk0_len =
443 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
444 ipl->iplb.pbt = S390_IPL_TYPE_CCW;
445 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
446 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
447 break;
448 }
449
450 if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
451 ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
452 }
453
454 return true;
455 }
456
457 return false;
458 }
459
460 int s390_ipl_set_loadparm(uint8_t *loadparm)
461 {
462 MachineState *machine = MACHINE(qdev_get_machine());
463 char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
464
465 if (lp) {
466 int i;
467
468 /* lp is an uppercase string without leading/embedded spaces */
469 for (i = 0; i < 8 && lp[i]; i++) {
470 loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]];
471 }
472
473 if (i < 8) {
474 memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */
475 }
476
477 g_free(lp);
478 return 0;
479 }
480
481 return -1;
482 }
483
484 static int load_netboot_image(Error **errp)
485 {
486 MachineState *ms = MACHINE(qdev_get_machine());
487 S390IPLState *ipl = get_ipl_device();
488 char *netboot_filename;
489 MemoryRegion *sysmem = get_system_memory();
490 MemoryRegion *mr = NULL;
491 void *ram_ptr = NULL;
492 int img_size = -1;
493
494 mr = memory_region_find(sysmem, 0, 1).mr;
495 if (!mr) {
496 error_setg(errp, "Failed to find memory region at address 0");
497 return -1;
498 }
499
500 ram_ptr = memory_region_get_ram_ptr(mr);
501 if (!ram_ptr) {
502 error_setg(errp, "No RAM found");
503 goto unref_mr;
504 }
505
506 netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw);
507 if (netboot_filename == NULL) {
508 error_setg(errp, "Could not find network bootloader '%s'",
509 ipl->netboot_fw);
510 goto unref_mr;
511 }
512
513 img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL,
514 &ipl->start_addr,
515 NULL, NULL, NULL, 1, EM_S390, 0, 0, NULL,
516 false);
517
518 if (img_size < 0) {
519 img_size = load_image_size(netboot_filename, ram_ptr, ms->ram_size);
520 ipl->start_addr = KERN_IMAGE_START;
521 }
522
523 if (img_size < 0) {
524 error_setg(errp, "Failed to load network bootloader");
525 }
526
527 g_free(netboot_filename);
528
529 unref_mr:
530 memory_region_unref(mr);
531 return img_size;
532 }
533
534 static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb,
535 int virtio_id)
536 {
537 uint8_t cssid;
538 uint8_t ssid;
539 uint16_t devno;
540 uint16_t schid;
541 SubchDev *sch = NULL;
542
543 if (iplb->pbt != S390_IPL_TYPE_CCW) {
544 return false;
545 }
546
547 devno = be16_to_cpu(iplb->ccw.devno);
548 ssid = iplb->ccw.ssid & 3;
549
550 for (schid = 0; schid < MAX_SCHID; schid++) {
551 for (cssid = 0; cssid < MAX_CSSID; cssid++) {
552 sch = css_find_subch(1, cssid, ssid, schid);
553
554 if (sch && sch->devno == devno) {
555 return sch->id.cu_model == virtio_id;
556 }
557 }
558 }
559 return false;
560 }
561
562 static bool is_virtio_net_device(IplParameterBlock *iplb)
563 {
564 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET);
565 }
566
567 static bool is_virtio_scsi_device(IplParameterBlock *iplb)
568 {
569 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI);
570 }
571
572 static void update_machine_ipl_properties(IplParameterBlock *iplb)
573 {
574 Object *machine = qdev_get_machine();
575 Error *err = NULL;
576
577 /* Sync loadparm */
578 if (iplb->flags & DIAG308_FLAGS_LP_VALID) {
579 uint8_t *ebcdic_loadparm = iplb->loadparm;
580 char ascii_loadparm[9];
581 int i;
582
583 for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) {
584 ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]];
585 }
586 ascii_loadparm[i] = 0;
587 object_property_set_str(machine, "loadparm", ascii_loadparm, &err);
588 } else {
589 object_property_set_str(machine, "loadparm", "", &err);
590 }
591 if (err) {
592 warn_report_err(err);
593 }
594 }
595
596 void s390_ipl_update_diag308(IplParameterBlock *iplb)
597 {
598 S390IPLState *ipl = get_ipl_device();
599
600 /*
601 * The IPLB set and retrieved by subcodes 8/9 is completely
602 * separate from the one managed via subcodes 5/6.
603 */
604 if (iplb->pbt == S390_IPL_TYPE_PV) {
605 ipl->iplb_pv = *iplb;
606 ipl->iplb_valid_pv = true;
607 } else {
608 ipl->iplb = *iplb;
609 ipl->iplb_valid = true;
610 }
611 ipl->netboot = is_virtio_net_device(iplb);
612 update_machine_ipl_properties(iplb);
613 }
614
615 IplParameterBlock *s390_ipl_get_iplb_pv(void)
616 {
617 S390IPLState *ipl = get_ipl_device();
618
619 if (!ipl->iplb_valid_pv) {
620 return NULL;
621 }
622 return &ipl->iplb_pv;
623 }
624
625 IplParameterBlock *s390_ipl_get_iplb(void)
626 {
627 S390IPLState *ipl = get_ipl_device();
628
629 if (!ipl->iplb_valid) {
630 return NULL;
631 }
632 return &ipl->iplb;
633 }
634
635 void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
636 {
637 S390IPLState *ipl = get_ipl_device();
638
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 ipl->reset_type = reset_type;
646
647 if (reset_type == S390_RESET_REIPL &&
648 ipl->iplb_valid &&
649 !ipl->netboot &&
650 ipl->iplb.pbt == S390_IPL_TYPE_CCW &&
651 is_virtio_scsi_device(&ipl->iplb)) {
652 CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL);
653
654 if (ccw_dev &&
655 cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno &&
656 (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) {
657 /*
658 * this is the original boot device's SCSI
659 * so restore IPL parameter info from it
660 */
661 ipl->iplb_valid = s390_gen_initial_iplb(ipl);
662 }
663 }
664 if (reset_type == S390_RESET_MODIFIED_CLEAR ||
665 reset_type == S390_RESET_LOAD_NORMAL ||
666 reset_type == S390_RESET_PV) {
667 /* ignore -no-reboot, send no event */
668 qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
669 } else {
670 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
671 }
672 /* as this is triggered by a CPU, make sure to exit the loop */
673 if (tcg_enabled()) {
674 cpu_loop_exit(cs);
675 }
676 }
677
678 void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
679 {
680 S390IPLState *ipl = get_ipl_device();
681
682 *cs = qemu_get_cpu(ipl->reset_cpu_index);
683 if (!*cs) {
684 /* use any CPU */
685 *cs = first_cpu;
686 }
687 *reset_type = ipl->reset_type;
688 }
689
690 void s390_ipl_clear_reset_request(void)
691 {
692 S390IPLState *ipl = get_ipl_device();
693
694 ipl->reset_type = S390_RESET_EXTERNAL;
695 /* use CPU 0 for full resets */
696 ipl->reset_cpu_index = 0;
697 }
698
699 static void s390_ipl_prepare_qipl(S390CPU *cpu)
700 {
701 S390IPLState *ipl = get_ipl_device();
702 uint8_t *addr;
703 uint64_t len = 4096;
704
705 addr = cpu_physical_memory_map(cpu->env.psa, &len, true);
706 if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
707 error_report("Cannot set QEMU IPL parameters");
708 return;
709 }
710 memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
711 cpu_physical_memory_unmap(addr, len, 1, len);
712 }
713
714 int s390_ipl_prepare_pv_header(void)
715 {
716 IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
717 IPLBlockPV *ipib_pv = &ipib->pv;
718 void *hdr = g_malloc(ipib_pv->pv_header_len);
719 int rc;
720
721 cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr,
722 ipib_pv->pv_header_len);
723 rc = s390_pv_set_sec_parms((uintptr_t)hdr,
724 ipib_pv->pv_header_len);
725 g_free(hdr);
726 return rc;
727 }
728
729 int s390_ipl_pv_unpack(void)
730 {
731 IplParameterBlock *ipib = s390_ipl_get_iplb_pv();
732 IPLBlockPV *ipib_pv = &ipib->pv;
733 int i, rc = 0;
734
735 for (i = 0; i < ipib_pv->num_comp; i++) {
736 rc = s390_pv_unpack(ipib_pv->components[i].addr,
737 TARGET_PAGE_ALIGN(ipib_pv->components[i].size),
738 ipib_pv->components[i].tweak_pref);
739 if (rc) {
740 break;
741 }
742 }
743 return rc;
744 }
745
746 void s390_ipl_prepare_cpu(S390CPU *cpu)
747 {
748 S390IPLState *ipl = get_ipl_device();
749
750 cpu->env.psw.addr = ipl->start_addr;
751 cpu->env.psw.mask = IPL_PSW_MASK;
752
753 if (!ipl->kernel || ipl->iplb_valid) {
754 cpu->env.psw.addr = ipl->bios_start_addr;
755 if (!ipl->iplb_valid) {
756 ipl->iplb_valid = s390_gen_initial_iplb(ipl);
757 }
758 }
759 if (ipl->netboot) {
760 load_netboot_image(&error_fatal);
761 ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
762 }
763 s390_ipl_set_boot_menu(ipl);
764 s390_ipl_prepare_qipl(cpu);
765 }
766
767 static void s390_ipl_reset(DeviceState *dev)
768 {
769 S390IPLState *ipl = S390_IPL(dev);
770
771 if (ipl->reset_type != S390_RESET_REIPL) {
772 ipl->iplb_valid = false;
773 memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
774 }
775 }
776
777 static void s390_ipl_class_init(ObjectClass *klass, void *data)
778 {
779 DeviceClass *dc = DEVICE_CLASS(klass);
780
781 dc->realize = s390_ipl_realize;
782 device_class_set_props(dc, s390_ipl_properties);
783 dc->reset = s390_ipl_reset;
784 dc->vmsd = &vmstate_ipl;
785 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
786 /* Reason: Loads the ROMs and thus can only be used one time - internally */
787 dc->user_creatable = false;
788 }
789
790 static const TypeInfo s390_ipl_info = {
791 .class_init = s390_ipl_class_init,
792 .parent = TYPE_DEVICE,
793 .name = TYPE_S390_IPL,
794 .instance_size = sizeof(S390IPLState),
795 };
796
797 static void s390_ipl_register_types(void)
798 {
799 type_register_static(&s390_ipl_info);
800 }
801
802 type_init(s390_ipl_register_types)