]>
Commit | Line | Data |
---|---|---|
e674a49a CB |
1 | /* |
2 | * bootloader support | |
3 | * | |
c3347ed0 | 4 | * Copyright IBM, Corp. 2012, 2020 |
e674a49a CB |
5 | * |
6 | * Authors: | |
7 | * Christian Borntraeger <borntraeger@de.ibm.com> | |
c3347ed0 | 8 | * Janosch Frank <frankja@linux.ibm.com> |
e674a49a CB |
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 | ||
9615495a | 15 | #include "qemu/osdep.h" |
2c65db5e | 16 | #include "qemu/datadir.h" |
da34e65c | 17 | #include "qapi/error.h" |
71e8a915 | 18 | #include "sysemu/reset.h" |
54d31236 | 19 | #include "sysemu/runstate.h" |
14a48c1d | 20 | #include "sysemu/tcg.h" |
e674a49a CB |
21 | #include "elf.h" |
22 | #include "hw/loader.h" | |
a27bd6c7 | 23 | #include "hw/qdev-properties.h" |
bd1badf4 | 24 | #include "hw/boards.h" |
ba1509c0 | 25 | #include "hw/s390x/virtio-ccw.h" |
44445d86 | 26 | #include "hw/s390x/vfio-ccw.h" |
ba1509c0 | 27 | #include "hw/s390x/css.h" |
bd1badf4 | 28 | #include "hw/s390x/ebcdic.h" |
c3347ed0 | 29 | #include "hw/s390x/pv.h" |
a43de798 PB |
30 | #include "hw/scsi/scsi.h" |
31 | #include "hw/virtio/virtio-net.h" | |
df75a4e2 | 32 | #include "ipl.h" |
f38b5b7f | 33 | #include "qemu/error-report.h" |
26b2a2a4 CW |
34 | #include "qemu/config-file.h" |
35 | #include "qemu/cutils.h" | |
36 | #include "qemu/option.h" | |
a43de798 | 37 | #include "standard-headers/linux/virtio_ids.h" |
a30fb811 | 38 | #include "exec/exec-all.h" |
e674a49a CB |
39 | |
40 | #define KERN_IMAGE_START 0x010000UL | |
acd7ef83 | 41 | #define LINUX_MAGIC_ADDR 0x010008UL |
b2173046 | 42 | #define KERN_PARM_AREA_SIZE_ADDR 0x010430UL |
e674a49a | 43 | #define KERN_PARM_AREA 0x010480UL |
b2173046 | 44 | #define LEGACY_KERN_PARM_AREA_SIZE 0x000380UL |
e674a49a CB |
45 | #define INITRD_START 0x800000UL |
46 | #define INITRD_PARM_START 0x010408UL | |
e674a49a | 47 | #define PARMFILE_START 0x001000UL |
e674a49a CB |
48 | #define ZIPL_IMAGE_START 0x009000UL |
49 | #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64) | |
50 | ||
04ca4b92 AY |
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 | ||
2e13fbe4 FZ |
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() | |
04ca4b92 AY |
78 | }, |
79 | .subsections = (const VMStateDescription*[]) { | |
80 | &vmstate_iplb_extended, | |
81 | NULL | |
2e13fbe4 FZ |
82 | } |
83 | }; | |
84 | ||
85 | static const VMStateDescription vmstate_ipl = { | |
86 | .name = "ipl", | |
87 | .version_id = 0, | |
88 | .minimum_version_id = 0, | |
89 | .fields = (VMStateField[]) { | |
bb099546 DH |
90 | VMSTATE_UINT64(compat_start_addr, S390IPLState), |
91 | VMSTATE_UINT64(compat_bios_start_addr, S390IPLState), | |
2e13fbe4 FZ |
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 | }; | |
e674a49a | 100 | |
feacc6c2 DH |
101 | static S390IPLState *get_ipl_device(void) |
102 | { | |
103 | return S390_IPL(object_resolve_path_type("", TYPE_S390_IPL, NULL)); | |
104 | } | |
105 | ||
d884c86d TH |
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 | ||
b2173046 MH |
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 | ||
04fccf10 | 131 | static void s390_ipl_realize(DeviceState *dev, Error **errp) |
e674a49a | 132 | { |
382a04af | 133 | MachineState *ms = MACHINE(qdev_get_machine()); |
e674a49a | 134 | S390IPLState *ipl = S390_IPL(dev); |
acd7ef83 CB |
135 | uint32_t *ipl_psw; |
136 | uint64_t pentry; | |
137 | char *magic; | |
376827d4 | 138 | int kernel_size; |
e674a49a | 139 | |
f0180f91 FZ |
140 | int bios_size; |
141 | char *bios_filename; | |
e674a49a | 142 | |
f0180f91 FZ |
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) { | |
382a04af | 148 | uint64_t fwbase = (MIN(ms->ram_size, 0x80000000U) - 0x200000) & ~0xffffUL; |
d884c86d | 149 | |
f0344395 | 150 | bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->firmware); |
1f7de853 | 151 | if (bios_filename == NULL) { |
dcfe4805 MA |
152 | error_setg(errp, "could not find stage1 bootloader"); |
153 | return; | |
1f7de853 DD |
154 | } |
155 | ||
4366e1db LM |
156 | bios_size = load_elf(bios_filename, NULL, |
157 | bios_translate_addr, &fwbase, | |
6cdda0ff | 158 | &ipl->bios_start_addr, NULL, NULL, NULL, 1, |
7ef295ea | 159 | EM_S390, 0, 0); |
d884c86d TH |
160 | if (bios_size > 0) { |
161 | /* Adjust ELF start address to final location */ | |
162 | ipl->bios_start_addr += fwbase; | |
163 | } else { | |
f35c1f66 | 164 | /* Try to load non-ELF file */ |
33259956 AG |
165 | bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START, |
166 | 4096); | |
f0180f91 | 167 | ipl->bios_start_addr = ZIPL_IMAGE_START; |
33259956 | 168 | } |
e674a49a CB |
169 | g_free(bios_filename); |
170 | ||
376827d4 | 171 | if (bios_size == -1) { |
f0344395 | 172 | error_setg(errp, "could not load bootloader '%s'", ipl->firmware); |
dcfe4805 | 173 | return; |
e674a49a | 174 | } |
7691993c | 175 | |
f0180f91 FZ |
176 | /* default boot target is the bios */ |
177 | ipl->start_addr = ipl->bios_start_addr; | |
e674a49a | 178 | } |
7691993c | 179 | |
f0180f91 | 180 | if (ipl->kernel) { |
4366e1db LM |
181 | kernel_size = load_elf(ipl->kernel, NULL, NULL, NULL, |
182 | &pentry, NULL, | |
6cdda0ff | 183 | NULL, NULL, 1, EM_S390, 0, 0); |
f0180f91 | 184 | if (kernel_size < 0) { |
382a04af | 185 | kernel_size = load_image_targphys(ipl->kernel, 0, ms->ram_size); |
acd7ef83 | 186 | if (kernel_size < 0) { |
dcfe4805 MA |
187 | error_setg(errp, "could not load kernel '%s'", ipl->kernel); |
188 | return; | |
acd7ef83 CB |
189 | } |
190 | /* if this is Linux use KERN_IMAGE_START */ | |
0f0f8b61 | 191 | magic = rom_ptr(LINUX_MAGIC_ADDR, 6); |
acd7ef83 CB |
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 */ | |
0f0f8b61 | 196 | ipl_psw = rom_ptr(4, 4); |
acd7ef83 | 197 | if (ipl_psw) { |
b6c2dbd7 | 198 | pentry = be32_to_cpu(*ipl_psw) & PSW_MASK_SHORT_ADDR; |
acd7ef83 | 199 | } else { |
dcfe4805 MA |
200 | error_setg(errp, "Could not get IPL PSW"); |
201 | return; | |
acd7ef83 CB |
202 | } |
203 | } | |
e674a49a | 204 | } |
f0180f91 FZ |
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) { | |
f1420101 MH |
212 | size_t cmdline_size = strlen(ipl->cmdline) + 1; |
213 | char *parm_area = rom_ptr(KERN_PARM_AREA, cmdline_size); | |
214 | ||
f0180f91 FZ |
215 | ipl->start_addr = KERN_IMAGE_START; |
216 | /* Overwrite parameters in the kernel image, which are "rom" */ | |
0f0f8b61 | 217 | if (parm_area) { |
b2173046 MH |
218 | uint64_t max_cmdline_size = get_max_kernel_cmdline_size(); |
219 | ||
220 | if (cmdline_size > max_cmdline_size) { | |
f1420101 | 221 | error_setg(errp, |
b2173046 MH |
222 | "kernel command line exceeds maximum size:" |
223 | " %zu > %" PRIu64, | |
224 | cmdline_size, max_cmdline_size); | |
f1420101 MH |
225 | return; |
226 | } | |
227 | ||
0f0f8b61 TH |
228 | strcpy(parm_area, ipl->cmdline); |
229 | } | |
f0180f91 FZ |
230 | } else { |
231 | ipl->start_addr = pentry; | |
e674a49a CB |
232 | } |
233 | ||
f0180f91 FZ |
234 | if (ipl->initrd) { |
235 | ram_addr_t initrd_offset; | |
236 | int initrd_size; | |
0f0f8b61 | 237 | uint64_t *romptr; |
f0180f91 FZ |
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, | |
382a04af | 244 | ms->ram_size - initrd_offset); |
f0180f91 | 245 | if (initrd_size == -1) { |
dcfe4805 MA |
246 | error_setg(errp, "could not load initrd '%s'", ipl->initrd); |
247 | return; | |
f0180f91 | 248 | } |
e674a49a | 249 | |
f0180f91 FZ |
250 | /* |
251 | * we have to overwrite values in the kernel image, | |
252 | * which are "rom" | |
253 | */ | |
0f0f8b61 TH |
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 | } | |
f0180f91 FZ |
259 | } |
260 | } | |
bb099546 DH |
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; | |
cd45c506 DH |
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); | |
e674a49a CB |
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), | |
d0249ce5 | 283 | DEFINE_PROP_STRING("firmware", S390IPLState, firmware), |
5f31ade0 | 284 | DEFINE_PROP_STRING("netboot_fw", S390IPLState, netboot_fw), |
f0180f91 | 285 | DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false), |
04ca4b92 AY |
286 | DEFINE_PROP_BOOL("iplbext_migration", S390IPLState, iplbext_migration, |
287 | true), | |
e674a49a CB |
288 | DEFINE_PROP_END_OF_LIST(), |
289 | }; | |
290 | ||
26b2a2a4 CW |
291 | static void s390_ipl_set_boot_menu(S390IPLState *ipl) |
292 | { | |
26b2a2a4 CW |
293 | unsigned long splash_time = 0; |
294 | ||
295 | if (!get_boot_device(0)) { | |
97ec4d21 | 296 | if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) { |
26b2a2a4 | 297 | error_report("boot menu requires a bootindex to be specified for " |
56000869 | 298 | "the IPL device"); |
26b2a2a4 CW |
299 | } |
300 | return; | |
301 | } | |
302 | ||
303 | switch (ipl->iplb.pbt) { | |
304 | case S390_IPL_TYPE_CCW: | |
53b310ce | 305 | /* In the absence of -boot menu, use zipl parameters */ |
97ec4d21 | 306 | if (!current_machine->boot_config.has_menu) { |
5d45a332 | 307 | ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_ZIPL; |
53b310ce CW |
308 | return; |
309 | } | |
26b2a2a4 | 310 | break; |
ffb4a1c8 CW |
311 | case S390_IPL_TYPE_QEMU_SCSI: |
312 | break; | |
26b2a2a4 | 313 | default: |
97ec4d21 | 314 | if (current_machine->boot_config.has_menu && current_machine->boot_config.menu) { |
56000869 CW |
315 | error_report("boot menu is not supported for this device type"); |
316 | } | |
26b2a2a4 CW |
317 | return; |
318 | } | |
319 | ||
97ec4d21 | 320 | if (!current_machine->boot_config.has_menu || !current_machine->boot_config.menu) { |
26b2a2a4 CW |
321 | return; |
322 | } | |
323 | ||
5d45a332 | 324 | ipl->qipl.qipl_flags |= QIPL_FLAG_BM_OPTS_CMD; |
26b2a2a4 | 325 | |
97ec4d21 PB |
326 | if (current_machine->boot_config.has_splash_time) { |
327 | splash_time = current_machine->boot_config.splash_time; | |
26b2a2a4 | 328 | } |
26b2a2a4 | 329 | if (splash_time > 0xffffffff) { |
56000869 | 330 | error_report("splash-time is too large, forcing it to max value"); |
5d45a332 | 331 | ipl->qipl.boot_menu_timeout = 0xffffffff; |
26b2a2a4 CW |
332 | return; |
333 | } | |
334 | ||
5d45a332 | 335 | ipl->qipl.boot_menu_timeout = cpu_to_be32(splash_time); |
26b2a2a4 CW |
336 | } |
337 | ||
44445d86 JH |
338 | #define CCW_DEVTYPE_NONE 0x00 |
339 | #define CCW_DEVTYPE_VIRTIO 0x01 | |
340 | #define CCW_DEVTYPE_VIRTIO_NET 0x02 | |
341 | #define CCW_DEVTYPE_SCSI 0x03 | |
342 | #define CCW_DEVTYPE_VFIO 0x04 | |
343 | ||
344 | static CcwDevice *s390_get_ccw_device(DeviceState *dev_st, int *devtype) | |
11ef6d50 VM |
345 | { |
346 | CcwDevice *ccw_dev = NULL; | |
44445d86 | 347 | int tmp_dt = CCW_DEVTYPE_NONE; |
11ef6d50 VM |
348 | |
349 | if (dev_st) { | |
44445d86 JH |
350 | VirtIONet *virtio_net_dev = (VirtIONet *) |
351 | object_dynamic_cast(OBJECT(dev_st), TYPE_VIRTIO_NET); | |
11ef6d50 VM |
352 | VirtioCcwDevice *virtio_ccw_dev = (VirtioCcwDevice *) |
353 | object_dynamic_cast(OBJECT(qdev_get_parent_bus(dev_st)->parent), | |
354 | TYPE_VIRTIO_CCW_DEVICE); | |
44445d86 JH |
355 | VFIOCCWDevice *vfio_ccw_dev = (VFIOCCWDevice *) |
356 | object_dynamic_cast(OBJECT(dev_st), TYPE_VFIO_CCW); | |
357 | ||
11ef6d50 VM |
358 | if (virtio_ccw_dev) { |
359 | ccw_dev = CCW_DEVICE(virtio_ccw_dev); | |
44445d86 JH |
360 | if (virtio_net_dev) { |
361 | tmp_dt = CCW_DEVTYPE_VIRTIO_NET; | |
362 | } else { | |
363 | tmp_dt = CCW_DEVTYPE_VIRTIO; | |
364 | } | |
365 | } else if (vfio_ccw_dev) { | |
366 | ccw_dev = CCW_DEVICE(vfio_ccw_dev); | |
367 | tmp_dt = CCW_DEVTYPE_VFIO; | |
11ef6d50 VM |
368 | } else { |
369 | SCSIDevice *sd = (SCSIDevice *) | |
370 | object_dynamic_cast(OBJECT(dev_st), | |
371 | TYPE_SCSI_DEVICE); | |
372 | if (sd) { | |
7d2eb76d PB |
373 | SCSIBus *sbus = scsi_bus_from_device(sd); |
374 | VirtIODevice *vdev = (VirtIODevice *) | |
375 | object_dynamic_cast(OBJECT(sbus->qbus.parent), | |
376 | TYPE_VIRTIO_DEVICE); | |
377 | if (vdev) { | |
378 | ccw_dev = (CcwDevice *) | |
379 | object_dynamic_cast(OBJECT(qdev_get_parent_bus(DEVICE(vdev))->parent), | |
380 | TYPE_CCW_DEVICE); | |
381 | if (ccw_dev) { | |
382 | tmp_dt = CCW_DEVTYPE_SCSI; | |
383 | } | |
384 | } | |
11ef6d50 VM |
385 | } |
386 | } | |
387 | } | |
44445d86 JH |
388 | if (devtype) { |
389 | *devtype = tmp_dt; | |
390 | } | |
11ef6d50 VM |
391 | return ccw_dev; |
392 | } | |
393 | ||
010d45d2 | 394 | static bool s390_gen_initial_iplb(S390IPLState *ipl) |
df75a4e2 FZ |
395 | { |
396 | DeviceState *dev_st; | |
11ef6d50 | 397 | CcwDevice *ccw_dev = NULL; |
44445d86 JH |
398 | SCSIDevice *sd; |
399 | int devtype; | |
df75a4e2 | 400 | |
df75a4e2 FZ |
401 | dev_st = get_boot_device(0); |
402 | if (dev_st) { | |
44445d86 | 403 | ccw_dev = s390_get_ccw_device(dev_st, &devtype); |
11ef6d50 VM |
404 | } |
405 | ||
406 | /* | |
407 | * Currently allow IPL only from CCW devices. | |
408 | */ | |
409 | if (ccw_dev) { | |
44445d86 JH |
410 | switch (devtype) { |
411 | case CCW_DEVTYPE_SCSI: | |
216bdd27 | 412 | sd = SCSI_DEVICE(dev_st); |
e468b673 AY |
413 | ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN); |
414 | ipl->iplb.blk0_len = | |
415 | cpu_to_be32(S390_IPLB_MIN_QEMU_SCSI_LEN - S390_IPLB_HEADER_LEN); | |
416 | ipl->iplb.pbt = S390_IPL_TYPE_QEMU_SCSI; | |
417 | ipl->iplb.scsi.lun = cpu_to_be32(sd->lun); | |
418 | ipl->iplb.scsi.target = cpu_to_be16(sd->id); | |
419 | ipl->iplb.scsi.channel = cpu_to_be16(sd->channel); | |
b804e8a6 JL |
420 | ipl->iplb.scsi.devno = cpu_to_be16(ccw_dev->sch->devno); |
421 | ipl->iplb.scsi.ssid = ccw_dev->sch->ssid & 3; | |
44445d86 JH |
422 | break; |
423 | case CCW_DEVTYPE_VFIO: | |
424 | ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN); | |
425 | ipl->iplb.pbt = S390_IPL_TYPE_CCW; | |
426 | ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno); | |
427 | ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3; | |
428 | break; | |
429 | case CCW_DEVTYPE_VIRTIO_NET: | |
430 | ipl->netboot = true; | |
431 | /* Fall through to CCW_DEVTYPE_VIRTIO case */ | |
432 | case CCW_DEVTYPE_VIRTIO: | |
11ef6d50 VM |
433 | ipl->iplb.len = cpu_to_be32(S390_IPLB_MIN_CCW_LEN); |
434 | ipl->iplb.blk0_len = | |
435 | cpu_to_be32(S390_IPLB_MIN_CCW_LEN - S390_IPLB_HEADER_LEN); | |
436 | ipl->iplb.pbt = S390_IPL_TYPE_CCW; | |
437 | ipl->iplb.ccw.devno = cpu_to_be16(ccw_dev->sch->devno); | |
438 | ipl->iplb.ccw.ssid = ccw_dev->sch->ssid & 3; | |
44445d86 | 439 | break; |
bd1badf4 FA |
440 | } |
441 | ||
442 | if (!s390_ipl_set_loadparm(ipl->iplb.loadparm)) { | |
443 | ipl->iplb.flags |= DIAG308_FLAGS_LP_VALID; | |
df75a4e2 | 444 | } |
11ef6d50 | 445 | |
bd1badf4 | 446 | return true; |
df75a4e2 FZ |
447 | } |
448 | ||
010d45d2 | 449 | return false; |
df75a4e2 FZ |
450 | } |
451 | ||
bd1badf4 FA |
452 | int s390_ipl_set_loadparm(uint8_t *loadparm) |
453 | { | |
454 | MachineState *machine = MACHINE(qdev_get_machine()); | |
455 | char *lp = object_property_get_str(OBJECT(machine), "loadparm", NULL); | |
456 | ||
457 | if (lp) { | |
458 | int i; | |
459 | ||
460 | /* lp is an uppercase string without leading/embedded spaces */ | |
461 | for (i = 0; i < 8 && lp[i]; i++) { | |
462 | loadparm[i] = ascii2ebcdic[(uint8_t) lp[i]]; | |
463 | } | |
464 | ||
074afe60 CW |
465 | if (i < 8) { |
466 | memset(loadparm + i, 0x40, 8 - i); /* fill with EBCDIC spaces */ | |
467 | } | |
468 | ||
bd1badf4 FA |
469 | g_free(lp); |
470 | return 0; | |
471 | } | |
472 | ||
473 | return -1; | |
474 | } | |
475 | ||
f38b5b7f FA |
476 | static int load_netboot_image(Error **errp) |
477 | { | |
382a04af | 478 | MachineState *ms = MACHINE(qdev_get_machine()); |
f38b5b7f FA |
479 | S390IPLState *ipl = get_ipl_device(); |
480 | char *netboot_filename; | |
481 | MemoryRegion *sysmem = get_system_memory(); | |
482 | MemoryRegion *mr = NULL; | |
483 | void *ram_ptr = NULL; | |
484 | int img_size = -1; | |
485 | ||
486 | mr = memory_region_find(sysmem, 0, 1).mr; | |
487 | if (!mr) { | |
488 | error_setg(errp, "Failed to find memory region at address 0"); | |
489 | return -1; | |
490 | } | |
491 | ||
492 | ram_ptr = memory_region_get_ram_ptr(mr); | |
493 | if (!ram_ptr) { | |
494 | error_setg(errp, "No RAM found"); | |
495 | goto unref_mr; | |
496 | } | |
497 | ||
498 | netboot_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, ipl->netboot_fw); | |
499 | if (netboot_filename == NULL) { | |
c575fa67 TH |
500 | error_setg(errp, "Could not find network bootloader '%s'", |
501 | ipl->netboot_fw); | |
f38b5b7f FA |
502 | goto unref_mr; |
503 | } | |
504 | ||
4366e1db LM |
505 | img_size = load_elf_ram(netboot_filename, NULL, NULL, NULL, |
506 | &ipl->start_addr, | |
6cdda0ff AM |
507 | NULL, NULL, NULL, 1, EM_S390, 0, 0, NULL, |
508 | false); | |
f38b5b7f FA |
509 | |
510 | if (img_size < 0) { | |
382a04af | 511 | img_size = load_image_size(netboot_filename, ram_ptr, ms->ram_size); |
f38b5b7f FA |
512 | ipl->start_addr = KERN_IMAGE_START; |
513 | } | |
514 | ||
515 | if (img_size < 0) { | |
516 | error_setg(errp, "Failed to load network bootloader"); | |
517 | } | |
518 | ||
519 | g_free(netboot_filename); | |
520 | ||
521 | unref_mr: | |
522 | memory_region_unref(mr); | |
523 | return img_size; | |
524 | } | |
525 | ||
789b5a40 VM |
526 | static bool is_virtio_ccw_device_of_type(IplParameterBlock *iplb, |
527 | int virtio_id) | |
f38b5b7f FA |
528 | { |
529 | uint8_t cssid; | |
530 | uint8_t ssid; | |
531 | uint16_t devno; | |
532 | uint16_t schid; | |
533 | SubchDev *sch = NULL; | |
534 | ||
535 | if (iplb->pbt != S390_IPL_TYPE_CCW) { | |
536 | return false; | |
537 | } | |
538 | ||
539 | devno = be16_to_cpu(iplb->ccw.devno); | |
540 | ssid = iplb->ccw.ssid & 3; | |
541 | ||
542 | for (schid = 0; schid < MAX_SCHID; schid++) { | |
543 | for (cssid = 0; cssid < MAX_CSSID; cssid++) { | |
544 | sch = css_find_subch(1, cssid, ssid, schid); | |
545 | ||
546 | if (sch && sch->devno == devno) { | |
789b5a40 | 547 | return sch->id.cu_model == virtio_id; |
f38b5b7f FA |
548 | } |
549 | } | |
550 | } | |
551 | return false; | |
552 | } | |
553 | ||
789b5a40 VM |
554 | static bool is_virtio_net_device(IplParameterBlock *iplb) |
555 | { | |
556 | return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_NET); | |
557 | } | |
558 | ||
559 | static bool is_virtio_scsi_device(IplParameterBlock *iplb) | |
560 | { | |
561 | return is_virtio_ccw_device_of_type(iplb, VIRTIO_ID_SCSI); | |
562 | } | |
563 | ||
0a01e082 HP |
564 | static void update_machine_ipl_properties(IplParameterBlock *iplb) |
565 | { | |
566 | Object *machine = qdev_get_machine(); | |
567 | Error *err = NULL; | |
568 | ||
569 | /* Sync loadparm */ | |
570 | if (iplb->flags & DIAG308_FLAGS_LP_VALID) { | |
571 | uint8_t *ebcdic_loadparm = iplb->loadparm; | |
77228373 | 572 | char ascii_loadparm[9]; |
0a01e082 HP |
573 | int i; |
574 | ||
575 | for (i = 0; i < 8 && ebcdic_loadparm[i]; i++) { | |
576 | ascii_loadparm[i] = ebcdic2ascii[(uint8_t) ebcdic_loadparm[i]]; | |
577 | } | |
578 | ascii_loadparm[i] = 0; | |
5325cc34 | 579 | object_property_set_str(machine, "loadparm", ascii_loadparm, &err); |
0a01e082 | 580 | } else { |
5325cc34 | 581 | object_property_set_str(machine, "loadparm", "", &err); |
0a01e082 HP |
582 | } |
583 | if (err) { | |
584 | warn_report_err(err); | |
585 | } | |
586 | } | |
587 | ||
feacc6c2 | 588 | void s390_ipl_update_diag308(IplParameterBlock *iplb) |
df75a4e2 | 589 | { |
feacc6c2 | 590 | S390IPLState *ipl = get_ipl_device(); |
df75a4e2 | 591 | |
c3347ed0 JF |
592 | /* |
593 | * The IPLB set and retrieved by subcodes 8/9 is completely | |
594 | * separate from the one managed via subcodes 5/6. | |
595 | */ | |
596 | if (iplb->pbt == S390_IPL_TYPE_PV) { | |
597 | ipl->iplb_pv = *iplb; | |
598 | ipl->iplb_valid_pv = true; | |
599 | } else { | |
600 | ipl->iplb = *iplb; | |
601 | ipl->iplb_valid = true; | |
602 | } | |
f38b5b7f | 603 | ipl->netboot = is_virtio_net_device(iplb); |
0a01e082 | 604 | update_machine_ipl_properties(iplb); |
df75a4e2 FZ |
605 | } |
606 | ||
c3347ed0 JF |
607 | IplParameterBlock *s390_ipl_get_iplb_pv(void) |
608 | { | |
609 | S390IPLState *ipl = get_ipl_device(); | |
610 | ||
611 | if (!ipl->iplb_valid_pv) { | |
612 | return NULL; | |
613 | } | |
614 | return &ipl->iplb_pv; | |
615 | } | |
616 | ||
df75a4e2 FZ |
617 | IplParameterBlock *s390_ipl_get_iplb(void) |
618 | { | |
feacc6c2 | 619 | S390IPLState *ipl = get_ipl_device(); |
df75a4e2 | 620 | |
feacc6c2 | 621 | if (!ipl->iplb_valid) { |
df75a4e2 FZ |
622 | return NULL; |
623 | } | |
624 | return &ipl->iplb; | |
625 | } | |
626 | ||
a30fb811 | 627 | void s390_ipl_reset_request(CPUState *cs, enum s390_reset reset_type) |
e91e972c | 628 | { |
feacc6c2 | 629 | S390IPLState *ipl = get_ipl_device(); |
e91e972c | 630 | |
a30fb811 DH |
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 | ipl->reset_type = reset_type; | |
638 | ||
639 | if (reset_type == S390_RESET_REIPL && | |
640 | ipl->iplb_valid && | |
789b5a40 VM |
641 | !ipl->netboot && |
642 | ipl->iplb.pbt == S390_IPL_TYPE_CCW && | |
643 | is_virtio_scsi_device(&ipl->iplb)) { | |
44445d86 | 644 | CcwDevice *ccw_dev = s390_get_ccw_device(get_boot_device(0), NULL); |
789b5a40 VM |
645 | |
646 | if (ccw_dev && | |
647 | cpu_to_be16(ccw_dev->sch->devno) == ipl->iplb.ccw.devno && | |
648 | (ccw_dev->sch->ssid & 3) == ipl->iplb.ccw.ssid) { | |
649 | /* | |
650 | * this is the original boot device's SCSI | |
651 | * so restore IPL parameter info from it | |
652 | */ | |
653 | ipl->iplb_valid = s390_gen_initial_iplb(ipl); | |
654 | } | |
655 | } | |
76ed4b18 | 656 | if (reset_type == S390_RESET_MODIFIED_CLEAR || |
d1bb69db CB |
657 | reset_type == S390_RESET_LOAD_NORMAL || |
658 | reset_type == S390_RESET_PV) { | |
76ed4b18 CB |
659 | /* ignore -no-reboot, send no event */ |
660 | qemu_system_reset_request(SHUTDOWN_CAUSE_SUBSYSTEM_RESET); | |
661 | } else { | |
662 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); | |
663 | } | |
a30fb811 DH |
664 | /* as this is triggered by a CPU, make sure to exit the loop */ |
665 | if (tcg_enabled()) { | |
666 | cpu_loop_exit(cs); | |
667 | } | |
668 | } | |
669 | ||
670 | void s390_ipl_get_reset_request(CPUState **cs, enum s390_reset *reset_type) | |
671 | { | |
672 | S390IPLState *ipl = get_ipl_device(); | |
673 | ||
674 | *cs = qemu_get_cpu(ipl->reset_cpu_index); | |
675 | if (!*cs) { | |
676 | /* use any CPU */ | |
677 | *cs = first_cpu; | |
678 | } | |
679 | *reset_type = ipl->reset_type; | |
680 | } | |
681 | ||
682 | void s390_ipl_clear_reset_request(void) | |
683 | { | |
684 | S390IPLState *ipl = get_ipl_device(); | |
685 | ||
686 | ipl->reset_type = S390_RESET_EXTERNAL; | |
687 | /* use CPU 0 for full resets */ | |
688 | ipl->reset_cpu_index = 0; | |
e91e972c FZ |
689 | } |
690 | ||
118ee80f CW |
691 | static void s390_ipl_prepare_qipl(S390CPU *cpu) |
692 | { | |
693 | S390IPLState *ipl = get_ipl_device(); | |
694 | uint8_t *addr; | |
695 | uint64_t len = 4096; | |
696 | ||
85eb7c18 | 697 | addr = cpu_physical_memory_map(cpu->env.psa, &len, true); |
118ee80f CW |
698 | if (!addr || len < QIPL_ADDRESS + sizeof(QemuIplParameters)) { |
699 | error_report("Cannot set QEMU IPL parameters"); | |
700 | return; | |
701 | } | |
702 | memcpy(addr + QIPL_ADDRESS, &ipl->qipl, sizeof(QemuIplParameters)); | |
703 | cpu_physical_memory_unmap(addr, len, 1, len); | |
704 | } | |
705 | ||
c3347ed0 JF |
706 | int s390_ipl_prepare_pv_header(void) |
707 | { | |
708 | IplParameterBlock *ipib = s390_ipl_get_iplb_pv(); | |
709 | IPLBlockPV *ipib_pv = &ipib->pv; | |
710 | void *hdr = g_malloc(ipib_pv->pv_header_len); | |
711 | int rc; | |
712 | ||
713 | cpu_physical_memory_read(ipib_pv->pv_header_addr, hdr, | |
714 | ipib_pv->pv_header_len); | |
715 | rc = s390_pv_set_sec_parms((uintptr_t)hdr, | |
716 | ipib_pv->pv_header_len); | |
717 | g_free(hdr); | |
718 | return rc; | |
719 | } | |
720 | ||
721 | int s390_ipl_pv_unpack(void) | |
722 | { | |
723 | IplParameterBlock *ipib = s390_ipl_get_iplb_pv(); | |
724 | IPLBlockPV *ipib_pv = &ipib->pv; | |
725 | int i, rc = 0; | |
726 | ||
727 | for (i = 0; i < ipib_pv->num_comp; i++) { | |
728 | rc = s390_pv_unpack(ipib_pv->components[i].addr, | |
729 | TARGET_PAGE_ALIGN(ipib_pv->components[i].size), | |
730 | ipib_pv->components[i].tweak_pref); | |
731 | if (rc) { | |
732 | break; | |
733 | } | |
734 | } | |
735 | return rc; | |
736 | } | |
737 | ||
db3b2566 DH |
738 | void s390_ipl_prepare_cpu(S390CPU *cpu) |
739 | { | |
740 | S390IPLState *ipl = get_ipl_device(); | |
741 | ||
742 | cpu->env.psw.addr = ipl->start_addr; | |
743 | cpu->env.psw.mask = IPL_PSW_MASK; | |
744 | ||
745 | if (!ipl->kernel || ipl->iplb_valid) { | |
746 | cpu->env.psw.addr = ipl->bios_start_addr; | |
010d45d2 AY |
747 | if (!ipl->iplb_valid) { |
748 | ipl->iplb_valid = s390_gen_initial_iplb(ipl); | |
749 | } | |
db3b2566 | 750 | } |
f38b5b7f | 751 | if (ipl->netboot) { |
f9734d5d | 752 | load_netboot_image(&error_fatal); |
118ee80f | 753 | ipl->qipl.netboot_start_addr = cpu_to_be64(ipl->start_addr); |
f38b5b7f | 754 | } |
26b2a2a4 | 755 | s390_ipl_set_boot_menu(ipl); |
118ee80f | 756 | s390_ipl_prepare_qipl(cpu); |
db3b2566 DH |
757 | } |
758 | ||
e674a49a CB |
759 | static void s390_ipl_reset(DeviceState *dev) |
760 | { | |
761 | S390IPLState *ipl = S390_IPL(dev); | |
ba1509c0 | 762 | |
a30fb811 | 763 | if (ipl->reset_type != S390_RESET_REIPL) { |
e91e972c | 764 | ipl->iplb_valid = false; |
6aed9589 | 765 | memset(&ipl->iplb, 0, sizeof(IplParameterBlock)); |
e91e972c | 766 | } |
e674a49a CB |
767 | } |
768 | ||
769 | static void s390_ipl_class_init(ObjectClass *klass, void *data) | |
770 | { | |
771 | DeviceClass *dc = DEVICE_CLASS(klass); | |
e674a49a | 772 | |
04fccf10 | 773 | dc->realize = s390_ipl_realize; |
4f67d30b | 774 | device_class_set_props(dc, s390_ipl_properties); |
e674a49a | 775 | dc->reset = s390_ipl_reset; |
2e13fbe4 | 776 | dc->vmsd = &vmstate_ipl; |
b4ab4572 | 777 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
0d4fa499 TH |
778 | /* Reason: Loads the ROMs and thus can only be used one time - internally */ |
779 | dc->user_creatable = false; | |
e674a49a CB |
780 | } |
781 | ||
49973ebc | 782 | static const TypeInfo s390_ipl_info = { |
e674a49a | 783 | .class_init = s390_ipl_class_init, |
04fccf10 DH |
784 | .parent = TYPE_DEVICE, |
785 | .name = TYPE_S390_IPL, | |
e674a49a CB |
786 | .instance_size = sizeof(S390IPLState), |
787 | }; | |
788 | ||
789 | static void s390_ipl_register_types(void) | |
790 | { | |
791 | type_register_static(&s390_ipl_info); | |
792 | } | |
793 | ||
794 | type_init(s390_ipl_register_types) |