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