Merge tag 'dt-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[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/boot_data.h>
5 #include <asm/sections.h>
6 #include <asm/cpu_mf.h>
7 #include <asm/setup.h>
8 #include <asm/kasan.h>
9 #include <asm/kexec.h>
10 #include <asm/sclp.h>
11 #include <asm/diag.h>
12 #include <asm/uv.h>
13 #include "compressed/decompressor.h"
14 #include "boot.h"
15 #include "uv.h"
16
17 unsigned long __bootdata_preserved(__kaslr_offset);
18 unsigned long __bootdata_preserved(VMALLOC_START);
19 unsigned long __bootdata_preserved(VMALLOC_END);
20 struct page *__bootdata_preserved(vmemmap);
21 unsigned long __bootdata_preserved(vmemmap_size);
22 unsigned long __bootdata_preserved(MODULES_VADDR);
23 unsigned long __bootdata_preserved(MODULES_END);
24 unsigned long __bootdata(ident_map_size);
25 int __bootdata(is_full_image) = 1;
26 struct initrd_data __bootdata(initrd_data);
27
28 u64 __bootdata_preserved(stfle_fac_list[16]);
29 u64 __bootdata_preserved(alt_stfle_fac_list[16]);
30 struct oldmem_data __bootdata_preserved(oldmem_data);
31
32 void error(char *x)
33 {
34         sclp_early_printk("\n\n");
35         sclp_early_printk(x);
36         sclp_early_printk("\n\n -- System halted");
37
38         disabled_wait();
39 }
40
41 static void setup_lpp(void)
42 {
43         S390_lowcore.current_pid = 0;
44         S390_lowcore.lpp = LPP_MAGIC;
45         if (test_facility(40))
46                 lpp(&S390_lowcore.lpp);
47 }
48
49 #ifdef CONFIG_KERNEL_UNCOMPRESSED
50 unsigned long mem_safe_offset(void)
51 {
52         return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
53 }
54 #endif
55
56 static void rescue_initrd(unsigned long addr)
57 {
58         if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
59                 return;
60         if (!initrd_data.start || !initrd_data.size)
61                 return;
62         if (addr <= initrd_data.start)
63                 return;
64         memmove((void *)addr, (void *)initrd_data.start, initrd_data.size);
65         initrd_data.start = addr;
66 }
67
68 static void copy_bootdata(void)
69 {
70         if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
71                 error(".boot.data section size mismatch");
72         memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
73         if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
74                 error(".boot.preserved.data section size mismatch");
75         memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
76 }
77
78 static void handle_relocs(unsigned long offset)
79 {
80         Elf64_Rela *rela_start, *rela_end, *rela;
81         int r_type, r_sym, rc;
82         Elf64_Addr loc, val;
83         Elf64_Sym *dynsym;
84
85         rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
86         rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
87         dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
88         for (rela = rela_start; rela < rela_end; rela++) {
89                 loc = rela->r_offset + offset;
90                 val = rela->r_addend;
91                 r_sym = ELF64_R_SYM(rela->r_info);
92                 if (r_sym) {
93                         if (dynsym[r_sym].st_shndx != SHN_UNDEF)
94                                 val += dynsym[r_sym].st_value + offset;
95                 } else {
96                         /*
97                          * 0 == undefined symbol table index (STN_UNDEF),
98                          * used for R_390_RELATIVE, only add KASLR offset
99                          */
100                         val += offset;
101                 }
102                 r_type = ELF64_R_TYPE(rela->r_info);
103                 rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
104                 if (rc)
105                         error("Unknown relocation type");
106         }
107 }
108
109 /*
110  * Merge information from several sources into a single ident_map_size value.
111  * "ident_map_size" represents the upper limit of physical memory we may ever
112  * reach. It might not be all online memory, but also include standby (offline)
113  * memory. "ident_map_size" could be lower then actual standby or even online
114  * memory present, due to limiting factors. We should never go above this limit.
115  * It is the size of our identity mapping.
116  *
117  * Consider the following factors:
118  * 1. max_physmem_end - end of physical memory online or standby.
119  *    Always <= end of the last online memory block (get_mem_detect_end()).
120  * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the
121  *    kernel is able to support.
122  * 3. "mem=" kernel command line option which limits physical memory usage.
123  * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as
124  *    crash kernel.
125  * 5. "hsa" size which is a memory limit when the kernel is executed during
126  *    zfcp/nvme dump.
127  */
128 static void setup_ident_map_size(unsigned long max_physmem_end)
129 {
130         unsigned long hsa_size;
131
132         ident_map_size = max_physmem_end;
133         if (memory_limit)
134                 ident_map_size = min(ident_map_size, memory_limit);
135         ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS);
136
137 #ifdef CONFIG_CRASH_DUMP
138         if (oldmem_data.start) {
139                 kaslr_enabled = 0;
140                 ident_map_size = min(ident_map_size, oldmem_data.size);
141         } else if (ipl_block_valid && is_ipl_block_dump()) {
142                 kaslr_enabled = 0;
143                 if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size)
144                         ident_map_size = min(ident_map_size, hsa_size);
145         }
146 #endif
147 }
148
149 static void setup_kernel_memory_layout(void)
150 {
151         bool vmalloc_size_verified = false;
152         unsigned long vmemmap_off;
153         unsigned long vspace_left;
154         unsigned long rte_size;
155         unsigned long pages;
156         unsigned long vmax;
157
158         pages = ident_map_size / PAGE_SIZE;
159         /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
160         vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page);
161
162         /* choose kernel address space layout: 4 or 3 levels. */
163         vmemmap_off = round_up(ident_map_size, _REGION3_SIZE);
164         if (IS_ENABLED(CONFIG_KASAN) ||
165             vmalloc_size > _REGION2_SIZE ||
166             vmemmap_off + vmemmap_size + vmalloc_size + MODULES_LEN > _REGION2_SIZE)
167                 vmax = _REGION1_SIZE;
168         else
169                 vmax = _REGION2_SIZE;
170
171         /* keep vmemmap_off aligned to a top level region table entry */
172         rte_size = vmax == _REGION1_SIZE ? _REGION2_SIZE : _REGION3_SIZE;
173         MODULES_END = vmax;
174         if (is_prot_virt_host()) {
175                 /*
176                  * forcing modules and vmalloc area under the ultravisor
177                  * secure storage limit, so that any vmalloc allocation
178                  * we do could be used to back secure guest storage.
179                  */
180                 adjust_to_uv_max(&MODULES_END);
181         }
182
183 #ifdef CONFIG_KASAN
184         if (MODULES_END < vmax) {
185                 /* force vmalloc and modules below kasan shadow */
186                 MODULES_END = min(MODULES_END, KASAN_SHADOW_START);
187         } else {
188                 /*
189                  * leave vmalloc and modules above kasan shadow but make
190                  * sure they don't overlap with it
191                  */
192                 vmalloc_size = min(vmalloc_size, vmax - KASAN_SHADOW_END - MODULES_LEN);
193                 vmalloc_size_verified = true;
194                 vspace_left = KASAN_SHADOW_START;
195         }
196 #endif
197         MODULES_VADDR = MODULES_END - MODULES_LEN;
198         VMALLOC_END = MODULES_VADDR;
199
200         if (vmalloc_size_verified) {
201                 VMALLOC_START = VMALLOC_END - vmalloc_size;
202         } else {
203                 vmemmap_off = round_up(ident_map_size, rte_size);
204
205                 if (vmemmap_off + vmemmap_size > VMALLOC_END ||
206                     vmalloc_size > VMALLOC_END - vmemmap_off - vmemmap_size) {
207                         /*
208                          * allow vmalloc area to occupy up to 1/2 of
209                          * the rest virtual space left.
210                          */
211                         vmalloc_size = min(vmalloc_size, VMALLOC_END / 2);
212                 }
213                 VMALLOC_START = VMALLOC_END - vmalloc_size;
214                 vspace_left = VMALLOC_START;
215         }
216
217         pages = vspace_left / (PAGE_SIZE + sizeof(struct page));
218         pages = SECTION_ALIGN_UP(pages);
219         vmemmap_off = round_up(vspace_left - pages * sizeof(struct page), rte_size);
220         /* keep vmemmap left most starting from a fresh region table entry */
221         vmemmap_off = min(vmemmap_off, round_up(ident_map_size, rte_size));
222         /* take care that identity map is lower then vmemmap */
223         ident_map_size = min(ident_map_size, vmemmap_off);
224         vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page);
225         VMALLOC_START = max(vmemmap_off + vmemmap_size, VMALLOC_START);
226         vmemmap = (struct page *)vmemmap_off;
227 }
228
229 /*
230  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
231  */
232 static void clear_bss_section(void)
233 {
234         memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
235 }
236
237 /*
238  * Set vmalloc area size to an 8th of (potential) physical memory
239  * size, unless size has been set by kernel command line parameter.
240  */
241 static void setup_vmalloc_size(void)
242 {
243         unsigned long size;
244
245         if (vmalloc_size_set)
246                 return;
247         size = round_up(ident_map_size / 8, _SEGMENT_SIZE);
248         vmalloc_size = max(size, vmalloc_size);
249 }
250
251 static void offset_vmlinux_info(unsigned long offset)
252 {
253         vmlinux.default_lma += offset;
254         *(unsigned long *)(&vmlinux.entry) += offset;
255         vmlinux.bootdata_off += offset;
256         vmlinux.bootdata_preserved_off += offset;
257         vmlinux.rela_dyn_start += offset;
258         vmlinux.rela_dyn_end += offset;
259         vmlinux.dynsym_start += offset;
260 }
261
262 void startup_kernel(void)
263 {
264         unsigned long random_lma;
265         unsigned long safe_addr;
266         void *img;
267
268         initrd_data.start = parmarea.initrd_start;
269         initrd_data.size = parmarea.initrd_size;
270         oldmem_data.start = parmarea.oldmem_base;
271         oldmem_data.size = parmarea.oldmem_size;
272
273         setup_lpp();
274         store_ipl_parmblock();
275         safe_addr = mem_safe_offset();
276         safe_addr = read_ipl_report(safe_addr);
277         uv_query_info();
278         rescue_initrd(safe_addr);
279         sclp_early_read_info();
280         setup_boot_command_line();
281         parse_boot_command_line();
282         sanitize_prot_virt_host();
283         setup_ident_map_size(detect_memory());
284         setup_vmalloc_size();
285         setup_kernel_memory_layout();
286
287         if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
288                 random_lma = get_random_base(safe_addr);
289                 if (random_lma) {
290                         __kaslr_offset = random_lma - vmlinux.default_lma;
291                         img = (void *)vmlinux.default_lma;
292                         offset_vmlinux_info(__kaslr_offset);
293                 }
294         }
295
296         if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
297                 img = decompress_kernel();
298                 memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
299         } else if (__kaslr_offset)
300                 memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
301
302         clear_bss_section();
303         copy_bootdata();
304         if (IS_ENABLED(CONFIG_RELOCATABLE))
305                 handle_relocs(__kaslr_offset);
306
307         if (__kaslr_offset) {
308                 /*
309                  * Save KASLR offset for early dumps, before vmcore_info is set.
310                  * Mark as uneven to distinguish from real vmcore_info pointer.
311                  */
312                 S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
313                 /* Clear non-relocated kernel */
314                 if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
315                         memset(img, 0, vmlinux.image_size);
316         }
317         vmlinux.entry();
318 }