Merge tag 'ntb-5.10' of git://github.com/jonmason/ntb
[linux-2.6-microblaze.git] / arch / s390 / boot / startup.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
3 #include <linux/elf.h>
4 #include <asm/sections.h>
5 #include <asm/setup.h>
6 #include <asm/kexec.h>
7 #include <asm/sclp.h>
8 #include <asm/diag.h>
9 #include <asm/uv.h>
10 #include "compressed/decompressor.h"
11 #include "boot.h"
12
13 extern char __boot_data_start[], __boot_data_end[];
14 extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
15 unsigned long __bootdata_preserved(__kaslr_offset);
16
17 /*
18  * Some code and data needs to stay below 2 GB, even when the kernel would be
19  * relocated above 2 GB, because it has to use 31 bit addresses.
20  * Such code and data is part of the .dma section, and its location is passed
21  * over to the decompressed / relocated kernel via the .boot.preserved.data
22  * section.
23  */
24 extern char _sdma[], _edma[];
25 extern char _stext_dma[], _etext_dma[];
26 extern struct exception_table_entry _start_dma_ex_table[];
27 extern struct exception_table_entry _stop_dma_ex_table[];
28 unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma);
29 unsigned long __bootdata_preserved(__edma) = __pa(&_edma);
30 unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma);
31 unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma);
32 struct exception_table_entry *
33         __bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table;
34 struct exception_table_entry *
35         __bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table;
36
37 int _diag210_dma(struct diag210 *addr);
38 int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode);
39 int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode);
40 void _diag0c_dma(struct hypfs_diag0c_entry *entry);
41 void _diag308_reset_dma(void);
42 struct diag_ops __bootdata_preserved(diag_dma_ops) = {
43         .diag210 = _diag210_dma,
44         .diag26c = _diag26c_dma,
45         .diag14 = _diag14_dma,
46         .diag0c = _diag0c_dma,
47         .diag308_reset = _diag308_reset_dma
48 };
49 static struct diag210 _diag210_tmp_dma __section(.dma.data);
50 struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma;
51
52 void error(char *x)
53 {
54         sclp_early_printk("\n\n");
55         sclp_early_printk(x);
56         sclp_early_printk("\n\n -- System halted");
57
58         disabled_wait();
59 }
60
61 #ifdef CONFIG_KERNEL_UNCOMPRESSED
62 unsigned long mem_safe_offset(void)
63 {
64         return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
65 }
66 #endif
67
68 static void rescue_initrd(unsigned long addr)
69 {
70         if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
71                 return;
72         if (!INITRD_START || !INITRD_SIZE)
73                 return;
74         if (addr <= INITRD_START)
75                 return;
76         memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE);
77         INITRD_START = addr;
78 }
79
80 static void copy_bootdata(void)
81 {
82         if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
83                 error(".boot.data section size mismatch");
84         memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
85         if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
86                 error(".boot.preserved.data section size mismatch");
87         memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
88 }
89
90 static void handle_relocs(unsigned long offset)
91 {
92         Elf64_Rela *rela_start, *rela_end, *rela;
93         int r_type, r_sym, rc;
94         Elf64_Addr loc, val;
95         Elf64_Sym *dynsym;
96
97         rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
98         rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
99         dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
100         for (rela = rela_start; rela < rela_end; rela++) {
101                 loc = rela->r_offset + offset;
102                 val = rela->r_addend;
103                 r_sym = ELF64_R_SYM(rela->r_info);
104                 if (r_sym) {
105                         if (dynsym[r_sym].st_shndx != SHN_UNDEF)
106                                 val += dynsym[r_sym].st_value + offset;
107                 } else {
108                         /*
109                          * 0 == undefined symbol table index (STN_UNDEF),
110                          * used for R_390_RELATIVE, only add KASLR offset
111                          */
112                         val += offset;
113                 }
114                 r_type = ELF64_R_TYPE(rela->r_info);
115                 rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
116                 if (rc)
117                         error("Unknown relocation type");
118         }
119 }
120
121 /*
122  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
123  */
124 static void clear_bss_section(void)
125 {
126         memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
127 }
128
129 void startup_kernel(void)
130 {
131         unsigned long random_lma;
132         unsigned long safe_addr;
133         void *img;
134
135         store_ipl_parmblock();
136         safe_addr = mem_safe_offset();
137         safe_addr = read_ipl_report(safe_addr);
138         uv_query_info();
139         rescue_initrd(safe_addr);
140         sclp_early_read_info();
141         setup_boot_command_line();
142         parse_boot_command_line();
143         setup_memory_end();
144         detect_memory();
145
146         random_lma = __kaslr_offset = 0;
147         if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
148                 random_lma = get_random_base(safe_addr);
149                 if (random_lma) {
150                         __kaslr_offset = random_lma - vmlinux.default_lma;
151                         img = (void *)vmlinux.default_lma;
152                         vmlinux.default_lma += __kaslr_offset;
153                         vmlinux.entry += __kaslr_offset;
154                         vmlinux.bootdata_off += __kaslr_offset;
155                         vmlinux.bootdata_preserved_off += __kaslr_offset;
156                         vmlinux.rela_dyn_start += __kaslr_offset;
157                         vmlinux.rela_dyn_end += __kaslr_offset;
158                         vmlinux.dynsym_start += __kaslr_offset;
159                 }
160         }
161
162         if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
163                 img = decompress_kernel();
164                 memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
165         } else if (__kaslr_offset)
166                 memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
167
168         clear_bss_section();
169         copy_bootdata();
170         if (IS_ENABLED(CONFIG_RELOCATABLE))
171                 handle_relocs(__kaslr_offset);
172
173         if (__kaslr_offset) {
174                 /*
175                  * Save KASLR offset for early dumps, before vmcore_info is set.
176                  * Mark as uneven to distinguish from real vmcore_info pointer.
177                  */
178                 S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
179                 /* Clear non-relocated kernel */
180                 if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
181                         memset(img, 0, vmlinux.image_size);
182         }
183         vmlinux.entry();
184 }