1 /*
2 * QEMU s390-ccw firmware - jump to IPL code
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2 or (at
5 * your option) any later version. See the COPYING file in the top-level
6 * directory.
7 */
8
9 #include <string.h>
10 #include <stdio.h>
11 #include "s390-ccw.h"
12 #include "s390-arch.h"
13
14 #define KERN_IMAGE_START 0x010000UL
15 #define RESET_PSW_MASK (PSW_MASK_SHORTPSW | PSW_MASK_64)
16 #define RESET_PSW ((uint64_t)&jump_to_IPL_addr | RESET_PSW_MASK)
17
18 static uint64_t *reset_psw = 0, save_psw, ipl_continue;
19
write_reset_psw(uint64_t psw)20 void write_reset_psw(uint64_t psw)
21 {
22 *reset_psw = psw;
23 }
24
jump_to_IPL_addr(void)25 static void jump_to_IPL_addr(void)
26 {
27 __attribute__((noreturn)) void (*ipl)(void) = (void *)ipl_continue;
28
29 /* Restore reset PSW */
30 write_reset_psw(save_psw);
31
32 ipl();
33 /* should not return */
34 }
35
jump_to_IPL_code(uint64_t address)36 int jump_to_IPL_code(uint64_t address)
37 {
38 /* store the subsystem information _after_ the bootmap was loaded */
39 write_subsystem_identification();
40 write_iplb_location();
41
42 /*
43 * The IPLB for QEMU SCSI type devices must be rebuilt during re-ipl. The
44 * iplb.devno is set to the boot position of the target SCSI device.
45 */
46 if (iplb.pbt == S390_IPL_TYPE_QEMU_SCSI) {
47 iplb.devno = qipl.index;
48 }
49
50 if (have_iplb && !set_iplb(&iplb)) {
51 panic("Failed to set IPLB");
52 }
53
54 /*
55 * The IPL PSW is at address 0. We also must not overwrite the
56 * content of non-BIOS memory after we loaded the guest, so we
57 * save the original content and restore it in jump_to_IPL_2.
58 */
59 if (address) {
60 save_psw = *reset_psw;
61 write_reset_psw(RESET_PSW);
62 ipl_continue = address;
63 }
64 debug_print_int("set IPL addr to", address ?: *reset_psw & PSW_MASK_SHORT_ADDR);
65
66 /* Ensure the guest output starts fresh */
67 printf("\n");
68
69 /*
70 * HACK ALERT.
71 * We use the load normal reset to keep r15 unchanged. jump_to_IPL_2
72 * can then use r15 as its stack pointer.
73 */
74 asm volatile("lghi %%r1,1\n\t"
75 "diag %%r1,%%r1,0x308\n\t"
76 : : : "1", "memory");
77 puts("IPL code jump failed");
78 return -1;
79 }
80
jump_to_low_kernel(void)81 void jump_to_low_kernel(void)
82 {
83 /*
84 * If it looks like a Linux binary, i.e. there is the "S390EP" magic from
85 * arch/s390/kernel/head.S here, then let's jump to the well-known Linux
86 * kernel start address (when jumping to the PSW-at-zero address instead,
87 * the kernel startup code fails when we booted from a network device).
88 */
89 if (!memcmp((char *)S390EP, "S390EP", 6)) {
90 jump_to_IPL_code(KERN_IMAGE_START);
91 }
92
93 /* Trying to get PSW at zero address (pointed to by reset_psw) */
94 if (*reset_psw & RESET_PSW_MASK) {
95 /*
96 * Surely nobody will try running directly from lowcore, so
97 * let's use 0 as an indication that we want to load the reset
98 * psw at 0x0 and not jump to the entry.
99 */
100 jump_to_IPL_code(0);
101 }
102
103 /* No other option left, so use the Linux kernel start address */
104 jump_to_IPL_code(KERN_IMAGE_START);
105 }
106