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