1
2
3
4
5
6
7
8
9
10
11
12
13
14#include "qemu/osdep.h"
15#include "qemu-common.h"
16#include "qapi/error.h"
17#include "sysemu/sysemu.h"
18#include "sysemu/tcg.h"
19#include "cpu.h"
20#include "elf.h"
21#include "hw/loader.h"
22#include "hw/boards.h"
23#include "hw/s390x/virtio-ccw.h"
24#include "hw/s390x/vfio-ccw.h"
25#include "hw/s390x/css.h"
26#include "hw/s390x/ebcdic.h"
27#include "ipl.h"
28#include "qemu/error-report.h"
29#include "qemu/config-file.h"
30#include "qemu/cutils.h"
31#include "qemu/option.h"
32#include "exec/exec-all.h"
33
34#define KERN_IMAGE_START 0x010000UL
35#define LINUX_MAGIC_ADDR 0x010008UL
36#define KERN_PARM_AREA 0x010480UL
37#define INITRD_START 0x800000UL
38#define INITRD_PARM_START 0x010408UL
39#define PARMFILE_START 0x001000UL
40#define ZIPL_IMAGE_START 0x009000UL
41#define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
42
43static bool iplb_extended_needed(void *opaque)
44{
45 S390IPLState *ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
46
47 return ipl->iplbext_migration;
48}
49
50static const VMStateDescription vmstate_iplb_extended = {
51 .name = "ipl/iplb_extended",
52 .version_id = 0,
53 .minimum_version_id = 0,
54 .needed = iplb_extended_needed,
55 .fields = (VMStateField[]) {
56 VMSTATE_UINT8_ARRAY(reserved_ext, IplParameterBlock, 4096 - 200),
57 VMSTATE_END_OF_LIST()
58 }
59};
60
61static const VMStateDescription vmstate_iplb = {
62 .name = "ipl/iplb",
63 .version_id = 0,
64 .minimum_version_id = 0,
65 .fields = (VMStateField[]) {
66 VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110),
67 VMSTATE_UINT16(devno, IplParameterBlock),
68 VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88),
69 VMSTATE_END_OF_LIST()
70 },
71 .subsections = (const VMStateDescription*[]) {
72 &vmstate_iplb_extended,
73 NULL
74 }
75};
76
77static const VMStateDescription vmstate_ipl = {
78 .name = "ipl",
79 .version_id = 0,
80 .minimum_version_id = 0,
81 .fields = (VMStateField[]) {
82 VMSTATE_UINT64(compat_start_addr, S390IPLState),
83 VMSTATE_UINT64(compat_bios_start_addr, S390IPLState),
84 VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock),
85 VMSTATE_BOOL(iplb_valid, S390IPLState),
86 VMSTATE_UINT8(cssid, S390IPLState),
87 VMSTATE_UINT8(ssid, S390IPLState),
88 VMSTATE_UINT16(devno, S390IPLState),
89 VMSTATE_END_OF_LIST()
90 }
91};
92
93static S390IPLState *get_ipl_device(void)
94{
95 return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL));
96}
97
98static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
99{
100 uint64_t dstaddr = *(uint64_t *) opaque;
101
102
103
104
105 return srcaddr + dstaddr;
106}
107
108static void s390_ipl_realize(DeviceState *dev, Error **errp)
109{
110 S390IPLState *ipl = S390_IPL(dev);
111 uint32_t *ipl_psw;
112 uint64_t pentry;
113 char *magic;
114 int kernel_size;
115 Error *err = NULL;
116
117 int bios_size;
118 char *bios_filename;
119
120
121
122
123
124 if (!ipl->kernel || ipl->enforce_bios) {
125 uint64_t fwbase = (MIN(ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
126
127 if (bios_name == NULL) {
128 bios_name = ipl->firmware;
129 }
130
131 bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
132 if (bios_filename == NULL) {
133 error_setg(&err, "could not find stage1 bootloader");
134 goto error;
135 }
136
137 bios_size = load_elf(bios_filename, NULL,
138 bios_translate_addr, &fwbase,
139 &ipl->bios_start_addr, NULL, NULL, 1,
140 EM_S390, 0, 0);
141 if (bios_size > 0) {
142
143 ipl->bios_start_addr += fwbase;
144 } else {
145
146 bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
147 4096);
148 ipl->bios_start_addr = ZIPL_IMAGE_START;
149 }
150 g_free(bios_filename);
151
152 if (bios_size == -1) {
153 error_setg(&err, "could not load bootloader '%s'", bios_name);
154 goto error;
155 }
156
157
158 ipl->start_addr = ipl->bios_start_addr;
159 }
160
161 if (ipl->kernel) {
162 kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL,
163 &pentry, NULL,
164 NULL, 1, EM_S390, 0, 0);
165 if (kernel_size < 0) {
166 kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
167 if (kernel_size < 0) {
168 error_setg(&err, "could not load kernel '%s'", ipl->kernel);
169 goto error;
170 }
171
172 magic = rom_ptr(LINUX_MAGIC_ADDR, 6);
173 if (magic && !memcmp(magic, "S390EP", 6)) {
174 pentry = KERN_IMAGE_START;
175 } else {
176
177 ipl_psw = rom_ptr(4, 4);
178 if (ipl_psw) {
179 pentry = be32_to_cpu(*ipl_psw) & 0x7fffffffUL;
180 } else {
181 error_setg(&err, "Could not get IPL PSW");
182 goto error;
183 }
184 }
185 }
186
187
188
189
190
191
192 if (pentry == KERN_IMAGE_START || pentry == 0x800) {
193 char *parm_area = rom_ptr(KERN_PARM_AREA, strlen(ipl->cmdline) + 1);
194 ipl->start_addr = KERN_IMAGE_START;
195
196 if (parm_area) {
197 strcpy(parm_area, ipl->cmdline);
198 }
199 } else {
200 ipl->start_addr = pentry;
201 }
202
203 if (ipl->initrd) {
204 ram_addr_t initrd_offset;
205 int initrd_size;
206 uint64_t *romptr;
207
208 initrd_offset = INITRD_START;
209 while (kernel_size + 0x100000 > initrd_offset) {
210 initrd_offset += 0x100000;
211 }
212 initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
213 ram_size - initrd_offset);
214 if (initrd_size == -1) {
215 error_setg(&err, "could not load initrd '%s'", ipl->initrd);
216 goto error;
217 }
218
219
220
221
222
223 romptr = rom_ptr(INITRD_PARM_START, 16);
224 if (romptr) {
225 stq_p(romptr, initrd_offset);
226 stq_p(romptr + 1, initrd_size);
227 }
228 }
229 }
230
231
232
233
234
235 ipl->compat_start_addr = ipl->start_addr;
236 ipl->compat_bios_start_addr = ipl->bios_start_addr;
237 qemu_register_reset(qdev_reset_all_fn, dev);
238error:
239 error_propagate(errp, err);
240}
241
242static Property s390_ipl_properties[] = {
243 DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
244 DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
245 DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
246 DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
247 DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw),
248 DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
249 DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration,
250 true),
251 DEFINE_PROP_END_OF_LIST(),
252};
253
254static void s390_ipl_set_boot_menu(S390IPLState *ipl)
255{
256 QemuOptsList *plist = qemu_find_opts("boot-opts");
257 QemuOpts *opts = QTAILQ_FIRST(&plist->head);
258 const char *tmp;
259 unsigned long splash_time = 0;
260
261 if (!get_boot_device(0)) {
262 if (boot_menu) {
263 error_report("boot menu requires a bootindex to be specified for "
264 "the IPL device");
265 }
266 return;
267 }
268
269 switch (ipl->iplb.pbt) {
270 case S390_IPL_TYPE_CCW:
271
272 if (!qemu_opt_get(opts, "menu")) {
273 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL;
274 return;
275 }
276 break;
277 case S390_IPL_TYPE_QEMU_SCSI:
278 break;
279 default:
280 if (boot_menu) {
281 error_report("boot menu is not supported for this device type");
282 }
283 return;
284 }
285
286 if (!boot_menu) {
287 return;
288 }
289
290 ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD;
291
292 tmp = qemu_opt_get(opts, "splash-time");
293
294 if (tmp && qemu_strtoul(tmp, NULL, 10, &splash_time)) {
295 error_report("splash-time is invalid, forcing it to 0");
296 ipl->qipl.boot_menu_timeout = 0;
297 return;
298 }
299
300 if (splash_time > 0xffffffff) {
301 error_report("splash-time is too large, forcing it to max value");
302 ipl->qipl.boot_menu_timeout = 0xffffffff;
303 return;
304 }
305
306 ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time);
307}
308
309#define CCW_DEVTYPE_NONE 0x00
310#define CCW_DEVTYPE_VIRTIO 0x01
311#define CCW_DEVTYPE_VIRTIO_NET 0x02
312#define CCW_DEVTYPE_SCSI 0x03
313#define CCW_DEVTYPE_VFIO 0x04
314
315static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype)
316{
317 CcwDevice *ccw_dev = NULL;
318 int tmp_dt = CCW_DEVTYPE_NONE;
319
320 if (dev_st) {
321 VirtIONet *virtio_net_dev = (VirtIONet *)
322 object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET);
323 VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *)
324 object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent),
325 TYPE_VIRTIO_CCW_DEVICE);
326 VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *)
327 object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW);
328
329 if (virtio_ccw_dev) {
330 ccw_dev = CCW_DEVICE(virtio_ccw_dev);
331 if (virtio_net_dev) {
332 tmp_dt = CCW_DEVTYPE_VIRTIO_NET;
333 } else {
334 tmp_dt = CCW_DEVTYPE_VIRTIO;
335 }
336 } else if (vfio_ccw_dev) {
337 ccw_dev = CCW_DEVICE(vfio_ccw_dev);
338 tmp_dt = CCW_DEVTYPE_VFIO;
339 } else {
340 SCSIDevice *sd = (SCSIDevice *)
341 object_dynamic_cast(OBJECT(dev_st),
342 TYPE_SCSI_DEVICE);
343 if (sd) {
344 SCSIBus *bus = scsi_bus_from_device(sd);
345 VirtIOSCSI *vdev = container_of(bus, VirtIOSCSI, bus);
346 VirtIOSCSICcw *scsi_ccw = container_of(vdev, VirtIOSCSICcw,
347 vdev);
348
349 ccw_dev = (CcwDevice *)object_dynamic_cast(OBJECT(scsi_ccw),
350 TYPE_CCW_DEVICE);
351 tmp_dt = CCW_DEVTYPE_SCSI;
352 }
353 }
354 }
355 if (devtype) {
356 *devtype = tmp_dt;
357 }
358 return ccw_dev;
359}
360
361static bool s390_gen_initial_iplb(S390IPLState *ipl)
362{
363 DeviceState *dev_st;
364 CcwDevice *ccw_dev = NULL;
365 SCSIDevice *sd;
366 int devtype;
367
368 dev_st = get_boot_device(0);
369 if (dev_st) {
370 ccw_dev = s390_get_ccw_device(dev_st, &devtype);
371 }
372
373
374
375
376 if (ccw_dev) {
377 switch (devtype) {
378 case CCW_DEVTYPE_SCSI:
379 sd = SCSI_DEVICE(dev_st);
380 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN);
381 ipl->iplb.blk0_len =
382 cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN);
383 ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI;
384 ipl->iplb.scsi.lun = cpu_to_be32(sd->lun);
385 ipl->iplb.scsi.target = cpu_to_be16(sd->id);
386 ipl->iplb.scsi.channel = cpu_to_be16(sd->channel);
387 ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno);
388 ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3;
389 break;
390 case CCW_DEVTYPE_VFIO:
391 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
392 ipl->iplb.pbt = S390_IPL_TYPE_CCW;
393 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
394 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
395 break;
396 case CCW_DEVTYPE_VIRTIO_NET:
397 ipl->netboot = true;
398
399 case CCW_DEVTYPE_VIRTIO:
400 ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN);
401 ipl->iplb.blk0_len =
402 cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN);
403 ipl->iplb.pbt = S390_IPL_TYPE_CCW;
404 ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno);
405 ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3;
406 break;
407 }
408
409 if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) {
410 ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID;
411 }
412
413 return true;
414 }
415
416 return false;
417}
418
419int s390_ipl_set_loadparm(uint8_t *loadparm)
420{
421 MachineState *machine = MACHINE(qdev_get_machine());
422 char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL);
423
424 if (lp) {
425 int i;
426
427
428 for (i = 0; i < 8 && lp[i]; i++) {
429 loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]];
430 }
431
432 if (i < 8) {
433 memset(loadparm + i, 0x40, 8 - i);
434 }
435
436 g_free(lp);
437 return 0;
438 }
439
440 return -1;
441}
442
443static int load_netboot_image(Error **errp)
444{
445 S390IPLState *ipl = get_ipl_device();
446 char *netboot_filename;
447 MemoryRegion *sysmem = get_system_memory();
448 MemoryRegion *mr = NULL;
449 void *ram_ptr = NULL;
450 int img_size = -1;
451
452 mr = memory_region_find(sysmem, 0, 1).mr;
453 if (!mr) {
454 error_setg(errp, "Failed to find memory region at address 0");
455 return -1;
456 }
457
458 ram_ptr = memory_region_get_ram_ptr(mr);
459 if (!ram_ptr) {
460 error_setg(errp, "No RAM found");
461 goto unref_mr;
462 }
463
464 netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw);
465 if (netboot_filename == NULL) {
466 error_setg(errp, "Could not find network bootloader '%s'",
467 ipl->netboot_fw);
468 goto unref_mr;
469 }
470
471 img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL,
472 &ipl->start_addr,
473 NULL, NULL, 1, EM_S390, 0, 0, NULL, false);
474
475 if (img_size < 0) {
476 img_size = load_image_size(netboot_filename, ram_ptr, ram_size);
477 ipl->start_addr = KERN_IMAGE_START;
478 }
479
480 if (img_size < 0) {
481 error_setg(errp, "Failed to load network bootloader");
482 }
483
484 g_free(netboot_filename);
485
486unref_mr:
487 memory_region_unref(mr);
488 return img_size;
489}
490
491static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb,
492 int virtio_id)
493{
494 uint8_t cssid;
495 uint8_t ssid;
496 uint16_t devno;
497 uint16_t schid;
498 SubchDev *sch = NULL;
499
500 if (iplb->pbt != S390_IPL_TYPE_CCW) {
501 return false;
502 }
503
504 devno = be16_to_cpu(iplb->ccw.devno);
505 ssid = iplb->ccw.ssid & 3;
506
507 for (schid = 0; schid < MAX_SCHID; schid++) {
508 for (cssid = 0; cssid < MAX_CSSID; cssid++) {
509 sch = css_find_subch(1, cssid, ssid, schid);
510
511 if (sch && sch->devno == devno) {
512 return sch->id.cu_model == virtio_id;
513 }
514 }
515 }
516 return false;
517}
518
519static bool is_virtio_net_device(IplParameterBlock *iplb)
520{
521 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET);
522}
523
524static bool is_virtio_scsi_device(IplParameterBlock *iplb)
525{
526 return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI);
527}
528
529void s390_ipl_update_diag308(IplParameterBlock *iplb)
530{
531 S390IPLState *ipl = get_ipl_device();
532
533 ipl->iplb = *iplb;
534 ipl->iplb_valid = true;
535 ipl->netboot = is_virtio_net_device(iplb);
536}
537
538IplParameterBlock *s390_ipl_get_iplb(void)
539{
540 S390IPLState *ipl = get_ipl_device();
541
542 if (!ipl->iplb_valid) {
543 return NULL;
544 }
545 return &ipl->iplb;
546}
547
548void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type)
549{
550 S390IPLState *ipl = get_ipl_device();
551
552 if (reset_type == S390_RESET_EXTERNAL || reset_type == S390_RESET_REIPL) {
553
554 ipl->reset_cpu_index = 0;
555 } else {
556 ipl->reset_cpu_index = cs->cpu_index;
557 }
558 ipl->reset_type = reset_type;
559
560 if (reset_type == S390_RESET_REIPL &&
561 ipl->iplb_valid &&
562 !ipl->netboot &&
563 ipl->iplb.pbt == S390_IPL_TYPE_CCW &&
564 is_virtio_scsi_device(&ipl->iplb)) {
565 CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL);
566
567 if (ccw_dev &&
568 cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno &&
569 (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) {
570
571
572
573
574 ipl->iplb_valid = s390_gen_initial_iplb(ipl);
575 }
576 }
577 if (reset_type == S390_RESET_MODIFIED_CLEAR ||
578 reset_type == S390_RESET_LOAD_NORMAL) {
579
580 qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET);
581 } else {
582 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
583 }
584
585 if (tcg_enabled()) {
586 cpu_loop_exit(cs);
587 }
588}
589
590void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type)
591{
592 S390IPLState *ipl = get_ipl_device();
593
594 *cs = qemu_get_cpu(ipl->reset_cpu_index);
595 if (!*cs) {
596
597 *cs = first_cpu;
598 }
599 *reset_type = ipl->reset_type;
600}
601
602void s390_ipl_clear_reset_request(void)
603{
604 S390IPLState *ipl = get_ipl_device();
605
606 ipl->reset_type = S390_RESET_EXTERNAL;
607
608 ipl->reset_cpu_index = 0;
609}
610
611static void s390_ipl_prepare_qipl(S390CPU *cpu)
612{
613 S390IPLState *ipl = get_ipl_device();
614 uint8_t *addr;
615 uint64_t len = 4096;
616
617 addr = cpu_physical_memory_map(cpu->env.psa, &len, 1);
618 if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) {
619 error_report("Cannot set QEMU IPL parameters");
620 return;
621 }
622 memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters));
623 cpu_physical_memory_unmap(addr, len, 1, len);
624}
625
626void s390_ipl_prepare_cpu(S390CPU *cpu)
627{
628 S390IPLState *ipl = get_ipl_device();
629 Error *err = NULL;
630
631 cpu->env.psw.addr = ipl->start_addr;
632 cpu->env.psw.mask = IPL_PSW_MASK;
633
634 if (!ipl->kernel || ipl->iplb_valid) {
635 cpu->env.psw.addr = ipl->bios_start_addr;
636 if (!ipl->iplb_valid) {
637 ipl->iplb_valid = s390_gen_initial_iplb(ipl);
638 }
639 }
640 if (ipl->netboot) {
641 if (load_netboot_image(&err) < 0) {
642 error_report_err(err);
643 exit(1);
644 }
645 ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr);
646 }
647 s390_ipl_set_boot_menu(ipl);
648 s390_ipl_prepare_qipl(cpu);
649}
650
651static void s390_ipl_reset(DeviceState *dev)
652{
653 S390IPLState *ipl = S390_IPL(dev);
654
655 if (ipl->reset_type != S390_RESET_REIPL) {
656 ipl->iplb_valid = false;
657 memset(&ipl->iplb, 0, sizeof(IplParameterBlock));
658 }
659}
660
661static void s390_ipl_class_init(ObjectClass *klass, void *data)
662{
663 DeviceClass *dc = DEVICE_CLASS(klass);
664
665 dc->realize = s390_ipl_realize;
666 dc->props = s390_ipl_properties;
667 dc->reset = s390_ipl_reset;
668 dc->vmsd = &vmstate_ipl;
669 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
670
671 dc->user_creatable = false;
672}
673
674static const TypeInfo s390_ipl_info = {
675 .class_init = s390_ipl_class_init,
676 .parent = TYPE_DEVICE,
677 .name = TYPE_S390_IPL,
678 .instance_size = sizeof(S390IPLState),
679};
680
681static void s390_ipl_register_types(void)
682{
683 type_register_static(&s390_ipl_info);
684}
685
686type_init(s390_ipl_register_types)
687