xref: /qemu/pc-bios/s390-ccw/jump2ipl.c (revision 9a848adf45d6732e62551decb3c0255173090767)
1*9a848adfSThomas Huth /*
2*9a848adfSThomas Huth  * QEMU s390-ccw firmware - jump to IPL code
3*9a848adfSThomas Huth  *
4*9a848adfSThomas Huth  * This work is licensed under the terms of the GNU GPL, version 2 or (at
5*9a848adfSThomas Huth  * your option) any later version. See the COPYING file in the top-level
6*9a848adfSThomas Huth  * directory.
7*9a848adfSThomas Huth  */
8*9a848adfSThomas Huth 
9*9a848adfSThomas Huth #include "libc.h"
10*9a848adfSThomas Huth #include "s390-ccw.h"
11*9a848adfSThomas Huth 
12*9a848adfSThomas Huth #define KERN_IMAGE_START 0x010000UL
13*9a848adfSThomas Huth #define PSW_MASK_64 0x0000000100000000ULL
14*9a848adfSThomas Huth #define PSW_MASK_32 0x0000000080000000ULL
15*9a848adfSThomas Huth #define IPL_PSW_MASK (PSW_MASK_32 | PSW_MASK_64)
16*9a848adfSThomas Huth 
17*9a848adfSThomas Huth typedef struct ResetInfo {
18*9a848adfSThomas Huth     uint32_t ipl_mask;
19*9a848adfSThomas Huth     uint32_t ipl_addr;
20*9a848adfSThomas Huth     uint32_t ipl_continue;
21*9a848adfSThomas Huth } ResetInfo;
22*9a848adfSThomas Huth 
23*9a848adfSThomas Huth static ResetInfo save;
24*9a848adfSThomas Huth 
25*9a848adfSThomas Huth static void jump_to_IPL_2(void)
26*9a848adfSThomas Huth {
27*9a848adfSThomas Huth     ResetInfo *current = 0;
28*9a848adfSThomas Huth 
29*9a848adfSThomas Huth     void (*ipl)(void) = (void *) (uint64_t) current->ipl_continue;
30*9a848adfSThomas Huth     *current = save;
31*9a848adfSThomas Huth     ipl(); /* should not return */
32*9a848adfSThomas Huth }
33*9a848adfSThomas Huth 
34*9a848adfSThomas Huth void jump_to_IPL_code(uint64_t address)
35*9a848adfSThomas Huth {
36*9a848adfSThomas Huth     /* store the subsystem information _after_ the bootmap was loaded */
37*9a848adfSThomas Huth     write_subsystem_identification();
38*9a848adfSThomas Huth 
39*9a848adfSThomas Huth     /* prevent unknown IPL types in the guest */
40*9a848adfSThomas Huth     if (iplb.pbt == S390_IPL_TYPE_QEMU_SCSI) {
41*9a848adfSThomas Huth         iplb.pbt = S390_IPL_TYPE_CCW;
42*9a848adfSThomas Huth         set_iplb(&iplb);
43*9a848adfSThomas Huth     }
44*9a848adfSThomas Huth 
45*9a848adfSThomas Huth     /*
46*9a848adfSThomas Huth      * The IPL PSW is at address 0. We also must not overwrite the
47*9a848adfSThomas Huth      * content of non-BIOS memory after we loaded the guest, so we
48*9a848adfSThomas Huth      * save the original content and restore it in jump_to_IPL_2.
49*9a848adfSThomas Huth      */
50*9a848adfSThomas Huth     ResetInfo *current = 0;
51*9a848adfSThomas Huth 
52*9a848adfSThomas Huth     save = *current;
53*9a848adfSThomas Huth     current->ipl_addr = (uint32_t) (uint64_t) &jump_to_IPL_2;
54*9a848adfSThomas Huth     current->ipl_continue = address & 0x7fffffff;
55*9a848adfSThomas Huth 
56*9a848adfSThomas Huth     debug_print_int("set IPL addr to", current->ipl_continue);
57*9a848adfSThomas Huth 
58*9a848adfSThomas Huth     /* Ensure the guest output starts fresh */
59*9a848adfSThomas Huth     sclp_print("\n");
60*9a848adfSThomas Huth 
61*9a848adfSThomas Huth     /*
62*9a848adfSThomas Huth      * HACK ALERT.
63*9a848adfSThomas Huth      * We use the load normal reset to keep r15 unchanged. jump_to_IPL_2
64*9a848adfSThomas Huth      * can then use r15 as its stack pointer.
65*9a848adfSThomas Huth      */
66*9a848adfSThomas Huth     asm volatile("lghi 1,1\n\t"
67*9a848adfSThomas Huth                  "diag 1,1,0x308\n\t"
68*9a848adfSThomas Huth                  : : : "1", "memory");
69*9a848adfSThomas Huth     panic("\n! IPL returns !\n");
70*9a848adfSThomas Huth }
71*9a848adfSThomas Huth 
72*9a848adfSThomas Huth void jump_to_low_kernel(void)
73*9a848adfSThomas Huth {
74*9a848adfSThomas Huth     /*
75*9a848adfSThomas Huth      * If it looks like a Linux binary, i.e. there is the "S390EP" magic from
76*9a848adfSThomas Huth      * arch/s390/kernel/head.S here, then let's jump to the well-known Linux
77*9a848adfSThomas Huth      * kernel start address (when jumping to the PSW-at-zero address instead,
78*9a848adfSThomas Huth      * the kernel startup code fails when we booted from a network device).
79*9a848adfSThomas Huth      */
80*9a848adfSThomas Huth     if (!memcmp((char *)0x10008, "S390EP", 6)) {
81*9a848adfSThomas Huth         jump_to_IPL_code(KERN_IMAGE_START);
82*9a848adfSThomas Huth     }
83*9a848adfSThomas Huth 
84*9a848adfSThomas Huth     /* Trying to get PSW at zero address */
85*9a848adfSThomas Huth     if (*((uint64_t *)0) & IPL_PSW_MASK) {
86*9a848adfSThomas Huth         jump_to_IPL_code((*((uint64_t *)0)) & 0x7fffffff);
87*9a848adfSThomas Huth     }
88*9a848adfSThomas Huth 
89*9a848adfSThomas Huth     /* No other option left, so use the Linux kernel start address */
90*9a848adfSThomas Huth     jump_to_IPL_code(KERN_IMAGE_START);
91*9a848adfSThomas Huth }
92